Re: [PATCH 9/9] blk-crypto: use mempool_alloc_bulk for encrypted bio page allocation

Vlastimil Babka
2025-11-05T15:12:31Z

On 10/31/25 10:34, Christoph Hellwig wrote:
> @@ -192,6 +205,29 @@ static struct bio *blk_crypto_alloc_enc_bio(struct bio *bio_src,
> bio->bi_write_stream = bio_src->bi_write_stream;
> bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector;
> bio_clone_blkg_association(bio, bio_src);
> +
> + /*
> + * Move page array up in the allocated memory for the bio vecs as far as
> + * possible so that we can start filling biovecs from the beginning
> + * without overwriting the temporary page array.
> + */
> + static_assert(PAGE_PTRS_PER_BVEC > 1);
> + pages = (struct page **)bio->bi_io_vec;
> + pages += nr_segs * (PAGE_PTRS_PER_BVEC - 1);
> +
> + /*
> + * Try a bulk allocation first. This could leave random pages in the
> + * array unallocated, but we'll fix that up later in mempool_alloc_bulk.
> + *
> + * Note: alloc_pages_bulk needs the array to be zeroed, as it assumes
> + * any non-zero slot already contains a valid allocation.
> + */
> + memset(pages, 0, sizeof(struct page *) * nr_segs);
> + if (alloc_pages_bulk(GFP_NOFS, nr_segs, pages) < nr_segs) {
> + mempool_alloc_bulk(blk_crypto_bounce_page_pool, (void **)pages,
> + nr_segs, GFP_NOIO);

Why do the GFP flags differ?

> + }
> + *pages_ret = pages;
> return bio;
> }
>
> @@ -234,6 +270,7 @@ static blk_status_t __blk_crypto_fallback_encrypt_bio(struct bio *src_bio,
> struct scatterlist src, dst;
> union blk_crypto_iv iv;
> struct bio *enc_bio = NULL;
> + struct page **enc_pages;
> unsigned int nr_segs;
> unsigned int enc_idx = 0;
> unsigned int j;
> @@ -259,11 +296,10 @@ static blk_status_t __blk_crypto_fallback_encrypt_bio(struct bio *src_bio,
>
> if (!enc_bio) {
> enc_bio = blk_crypto_alloc_enc_bio(src_bio,
> - min(nr_segs, BIO_MAX_VECS));
> + min(nr_segs, BIO_MAX_VECS), &enc_pages);
> }
>
> - enc_page = mempool_alloc(blk_crypto_bounce_page_pool,
> - GFP_NOIO);
> + enc_page = enc_pages[enc_idx];
> __bio_add_page(enc_bio, enc_page, src_bv.bv_len,
> src_bv.bv_offset);
>