block: add a bio_add_virt_nofail helper
authorChristoph Hellwig <hch@lst.de>
Wed, 7 May 2025 12:04:25 +0000 (14:04 +0200)
committerJens Axboe <axboe@kernel.dk>
Wed, 7 May 2025 13:31:07 +0000 (07:31 -0600)
Add a helper to add a directly mapped kernel virtual address to a
bio so that callers don't have to convert to pages or folios.

For now only the _nofail variant is provided as that is what all the
obvious callers want.

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

index 1e42aefc7377ab1501e6986cb842dd5b80e97d51..bd3d048d0a728683e4c1c29385f07a4c48d9be5b 100644 (file)
@@ -991,6 +991,22 @@ void __bio_add_page(struct bio *bio, struct page *page,
 }
 EXPORT_SYMBOL_GPL(__bio_add_page);
 
+/**
+ * bio_add_virt_nofail - add data in the direct kernel mapping to a bio
+ * @bio: destination bio
+ * @vaddr: data to add
+ * @len: length of the data to add, may cross pages
+ *
+ * Add the data at @vaddr to @bio.  The caller must have ensure a segment
+ * is available for the added data.  No merging into an existing segment
+ * will be performed.
+ */
+void bio_add_virt_nofail(struct bio *bio, void *vaddr, unsigned len)
+{
+       __bio_add_page(bio, virt_to_page(vaddr), len, offset_in_page(vaddr));
+}
+EXPORT_SYMBOL_GPL(bio_add_virt_nofail);
+
 /**
  *     bio_add_page    -       attempt to add page(s) to bio
  *     @bio: destination bio
index cafc7c215de8bece6c2cb4532ba7c2c9ec85e0cc..acca7464080c215a80e75b355ceedf89c461ae16 100644 (file)
@@ -417,6 +417,8 @@ void __bio_add_page(struct bio *bio, struct page *page,
                unsigned int len, unsigned int off);
 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);
+
 int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter);
 void bio_iov_bvec_set(struct bio *bio, const struct iov_iter *iter);
 void __bio_release_pages(struct bio *bio, bool mark_dirty);