]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
block: add a bio_add_max_vecs helper
authorChristoph Hellwig <hch@lst.de>
Wed, 7 May 2025 12:04:27 +0000 (14:04 +0200)
committerJens Axboe <axboe@kernel.dk>
Wed, 7 May 2025 13:31:07 +0000 (07:31 -0600)
Add a helper to check how many bio_vecs are needed to add a kernel
virtual address range to a bio, accounting for the always contiguous
direct mapping and vmalloc mappings that usually need a bio_vec
per page sized chunk.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Link: https://lore.kernel.org/r/20250507120451.4000627-4-hch@lst.de
Signed-off-by: Jens Axboe <axboe@kernel.dk>
include/linux/bio.h

index ad54e6af20dcf7336ca91a2b0e40a877444de5d6..128b1c6ca6489560da828f0742e75008b919f044 100644 (file)
@@ -418,6 +418,21 @@ void bio_add_folio_nofail(struct bio *bio, struct folio *folio, size_t len,
                          size_t off);
 void bio_add_virt_nofail(struct bio *bio, void *vaddr, unsigned len);
 
+/**
+ * bio_add_max_vecs - number of bio_vecs needed to add data to a bio
+ * @kaddr: kernel virtual address to add
+ * @len: length in bytes to add
+ *
+ * Calculate how many bio_vecs need to be allocated to add the kernel virtual
+ * address range in [@kaddr:@len] in the worse case.
+ */
+static inline unsigned int bio_add_max_vecs(void *kaddr, unsigned int len)
+{
+       if (is_vmalloc_addr(kaddr))
+               return DIV_ROUND_UP(offset_in_page(kaddr) + len, PAGE_SIZE);
+       return 1;
+}
+
 int submit_bio_wait(struct bio *bio);
 int bdev_rw_virt(struct block_device *bdev, sector_t sector, void *data,
                size_t len, enum req_op op);