Re: [PATCH 07/19] block: simplify bio_map_kern

Damien Le Moal
2025-05-01T18:56:11Z

On 5/1/25 06:21, Christoph Hellwig wrote:
> Rewrite bio_map_kern using the new bio_add_* helpers and drop the
> kerneldoc comment that is superfluous for an internal helper.
>
> Signed-off-by: Christoph Hellwig <hch@lst.de>

Looks good to me.

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

One nit below.

> ---
> block/blk-map.c | 56 ++++++++-----------------------------------------
> 1 file changed, 9 insertions(+), 47 deletions(-)
>
> diff --git a/block/blk-map.c b/block/blk-map.c
> index ca6b55ac0da1..0bc823b168e4 100644
> --- a/block/blk-map.c
> +++ b/block/blk-map.c
> @@ -317,64 +317,26 @@ static void bio_map_kern_endio(struct bio *bio)
> kfree(bio);
> }
>
> -/**
> - * bio_map_kern - map kernel address into bio
> - * @data: pointer to buffer to map
> - * @len: length in bytes
> - * @op: bio/request operation
> - * @gfp_mask: allocation flags for bio allocation
> - *
> - * Map the kernel address into a bio suitable for io to a block
> - * device. Returns an error pointer in case of error.
> - */
> -static struct bio *bio_map_kern(void *data, unsigned int len,
> - enum req_op op, gfp_t gfp_mask)
> +static struct bio *bio_map_kern(void *data, unsigned int len, enum req_op op,
> + gfp_t gfp_mask)
> {
> - unsigned long kaddr = (unsigned long)data;
> - unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
> - unsigned long start = kaddr >> PAGE_SHIFT;
> - const int nr_pages = end - start;
> - bool is_vmalloc = is_vmalloc_addr(data);
> - struct page *page;
> - int offset, i;
> + unsigned int nr_vecs = bio_add_max_vecs(data, len);
> struct bio *bio;
>
> - bio = bio_kmalloc(nr_pages, gfp_mask);
> + bio = bio_kmalloc(nr_vecs, gfp_mask);

This may also fail if nr_vecs is larger than UIO_MAXIOV, in which case, the
ENOMEM error may not really be appropriate. I guess we can sort this out
separately though.

> if (!bio)
> return ERR_PTR(-ENOMEM);
> - bio_init(bio, NULL, bio->bi_inline_vecs, nr_pages, op);

--
Damien Le Moal
Western Digital Research