linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: Christoph Hellwig <hch@lst.de>
Cc: Jens Axboe <axboe@kernel.dk>,
	linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	linux-fscrypt@vger.kernel.org
Subject: Re: [PATCH 7/9] blk-crypto: use mempool_alloc_bulk for encrypted bio page allocation
Date: Fri, 19 Dec 2025 12:25:33 -0800	[thread overview]
Message-ID: <20251219202533.GA397103@sol> (raw)
In-Reply-To: <20251219200244.GE1602@sol>

On Fri, Dec 19, 2025 at 12:02:44PM -0800, Eric Biggers wrote:
> On Wed, Dec 17, 2025 at 07:06:50AM +0100, Christoph Hellwig wrote:
> >  new_bio:
> > -	enc_bio = blk_crypto_alloc_enc_bio(src_bio, nr_segs);
> > +	enc_bio = blk_crypto_alloc_enc_bio(src_bio, nr_segs, &enc_pages);
> >  	enc_idx = 0;
> >  	for (;;) {
> >  		struct bio_vec src_bv =
> >  			bio_iter_iovec(src_bio, src_bio->bi_iter);
> > -		struct page *enc_page;
> > +		struct page *enc_page = enc_pages[enc_idx];
> >  
> > -		enc_page = mempool_alloc(blk_crypto_bounce_page_pool,
> > -				GFP_NOIO);
> >  		__bio_add_page(enc_bio, enc_page, src_bv.bv_len,
> >  				src_bv.bv_offset);
> >  
> > @@ -246,10 +284,8 @@ static void __blk_crypto_fallback_encrypt_bio(struct bio *src_bio,
> >  		/* Encrypt each data unit in this page */
> >  		for (j = 0; j < src_bv.bv_len; j += data_unit_size) {
> >  			blk_crypto_dun_to_iv(curr_dun, &iv);
> > -			if (crypto_skcipher_encrypt(ciph_req)) {
> > -				enc_idx++;
> > -				goto out_free_bounce_pages;
> > -			}
> > +			if (crypto_skcipher_encrypt(ciph_req))
> > +				goto out_free_enc_bio;
> >  			bio_crypt_dun_increment(curr_dun, 1);
> >  			src.offset += data_unit_size;
> >  			dst.offset += data_unit_size;
> > @@ -278,9 +314,9 @@ static void __blk_crypto_fallback_encrypt_bio(struct bio *src_bio,
> >  	submit_bio(enc_bio);
> >  	return;
> >  
> > -out_free_bounce_pages:
> > -	while (enc_idx > 0)
> > -		mempool_free(enc_bio->bi_io_vec[--enc_idx].bv_page,
> > +out_free_enc_bio:
> > +	for (enc_idx = 0; enc_idx < enc_bio->bi_max_vecs; enc_idx++)
> > +		mempool_free(enc_bio->bi_io_vec[enc_idx].bv_page,
> >  			     blk_crypto_bounce_page_pool);
> >  	bio_put(enc_bio);
> >  	cmpxchg(&src_bio->bi_status, 0, BLK_STS_IOERR);
> 
> The error handling at out_free_enc_bio is still broken, I'm afraid.
> It's not taking into account that some of the pages may have been moved
> into bvecs and some have not.
> 
> I think it needs something like the following:
> 
> diff --git a/block/blk-crypto-fallback.c b/block/blk-crypto-fallback.c
> index 23e097197450..d6760404b76c 100644
> --- a/block/blk-crypto-fallback.c
> +++ b/block/blk-crypto-fallback.c
> @@ -272,7 +272,7 @@ static void __blk_crypto_fallback_encrypt_bio(struct bio *src_bio,
>  	for (;;) {
>  		struct bio_vec src_bv =
>  			bio_iter_iovec(src_bio, src_bio->bi_iter);
> -		struct page *enc_page = enc_pages[enc_idx];
> +		struct page *enc_page;
>  
>  		if (!IS_ALIGNED(src_bv.bv_len | src_bv.bv_offset,
>  				data_unit_size)) {
> @@ -280,6 +280,7 @@ static void __blk_crypto_fallback_encrypt_bio(struct bio *src_bio,
>  			goto out_free_enc_bio;
>  		}
>  
> +		enc_page = enc_pages[enc_idx++];
>  		__bio_add_page(enc_bio, enc_page, src_bv.bv_len,
>  				src_bv.bv_offset);
>  
> @@ -305,7 +306,7 @@ static void __blk_crypto_fallback_encrypt_bio(struct bio *src_bio,
>  			break;
>  
>  		nr_segs--;
> -		if (++enc_idx == enc_bio->bi_max_vecs) {
> +		if (enc_idx == enc_bio->bi_max_vecs) {
>  			/*
>  			 * For each additional encrypted bio submitted,
>  			 * increment the source bio's remaining count.  Each
> @@ -323,9 +324,11 @@ static void __blk_crypto_fallback_encrypt_bio(struct bio *src_bio,
>  	return;
>  
>  out_free_enc_bio:
> -	for (enc_idx = 0; enc_idx < enc_bio->bi_max_vecs; enc_idx++)
> +	for (j = 0; j < enc_idx; j++)
>  		mempool_free(enc_bio->bi_io_vec[j].bv_page,
>  			     blk_crypto_bounce_page_pool);
> +	for (; j < enc_bio->bi_max_vecs; j++)
> +		mempool_free(enc_pages[j], blk_crypto_bounce_page_pool);
>  	bio_put(enc_bio);
>  	bio_endio(src_bio);
>  }

Also, this shows that the decrement of 'nr_segs' is a bit out-of-place
(as was 'enc_idx').  nr_segs is used only when allocating a bio, so it
could be decremented only when starting a new one:

        submit_bio(enc_bio);
        nr_segs -= nr_enc_pages;                                 
        goto new_bio;                                            

- Eric

  reply	other threads:[~2025-12-19 20:25 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-17  6:06 move blk-crypto-fallback to sit above the block layer v3 Christoph Hellwig
2025-12-17  6:06 ` [PATCH 1/9] fscrypt: pass a real sector_t to fscrypt_zeroout_range_inline_crypt Christoph Hellwig
2025-12-17  6:06 ` [PATCH 2/9] fscrypt: keep multiple bios in flight in fscrypt_zeroout_range_inline_crypt Christoph Hellwig
2025-12-17  6:06 ` [PATCH 3/9] blk-crypto: add a bio_crypt_ctx() helper Christoph Hellwig
2025-12-19 19:50   ` Eric Biggers
2025-12-17  6:06 ` [PATCH 4/9] blk-crypto: submit the encrypted bio in blk_crypto_fallback_bio_prep Christoph Hellwig
2025-12-19 19:50   ` Eric Biggers
2025-12-17  6:06 ` [PATCH 5/9] blk-crypto: optimize bio splitting in blk_crypto_fallback_encrypt_bio Christoph Hellwig
2025-12-19 20:08   ` Eric Biggers
2025-12-22 22:12     ` Christoph Hellwig
2025-12-17  6:06 ` [PATCH 6/9] blk-crypto: use on-stack skcipher requests for fallback en/decryption Christoph Hellwig
2025-12-17  6:06 ` [PATCH 7/9] blk-crypto: use mempool_alloc_bulk for encrypted bio page allocation Christoph Hellwig
2025-12-19 20:02   ` Eric Biggers
2025-12-19 20:25     ` Eric Biggers [this message]
2025-12-22 22:16       ` Christoph Hellwig
2025-12-22 22:18     ` Christoph Hellwig
2026-01-06  7:39       ` Christoph Hellwig
2025-12-17  6:06 ` [PATCH 8/9] blk-crypto: optimize data unit alignment checking Christoph Hellwig
2025-12-19 20:14   ` Eric Biggers
2025-12-17  6:06 ` [PATCH 9/9] blk-crypto: handle the fallback above the block layer Christoph Hellwig
  -- strict thread matches above, loose matches on Subject: below --
2026-01-06  7:36 move blk-crypto-fallback to sit above the block layer v4 Christoph Hellwig
2026-01-06  7:36 ` [PATCH 7/9] blk-crypto: use mempool_alloc_bulk for encrypted bio page allocation Christoph Hellwig
2025-12-10 15:23 move blk-crypto-fallback to sit above the block layer v2 Christoph Hellwig
2025-12-10 15:23 ` [PATCH 7/9] blk-crypto: use mempool_alloc_bulk for encrypted bio page allocation Christoph Hellwig
2025-12-13  1:21   ` Eric Biggers
2025-12-15  6:01     ` Christoph Hellwig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251219202533.GA397103@sol \
    --to=ebiggers@kernel.org \
    --cc=axboe@kernel.dk \
    --cc=hch@lst.de \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-fscrypt@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).