From: Baokun Li <libaokun@linux.alibaba.com>
To: linux-ext4@vger.kernel.org
Cc: linux-fscrypt@vger.kernel.org, linux-block@vger.kernel.org,
linux-fsdevel@vger.kernel.org, linux-xfs@vger.kernel.org,
ebiggers@kernel.org, jaegeuk@kernel.org, axboe@kernel.dk,
hch@lst.de, djwong@kernel.org, brauner@kernel.org, tytso@mit.edu,
berto@igalia.com, adilger.kernel@dilger.ca, jack@suse.cz,
yi.zhang@huawei.com, ojaswin@linux.ibm.com,
ritesh.list@gmail.com, stable@vger.kernel.org
Subject: [PATCH 08/13] block: split encrypted bios at data unit boundaries
Date: Mon, 12 Oct 2026 01:09:53 +0800 [thread overview]
Message-ID: <20261011170958.1759230-9-libaokun@linux.alibaba.com> (raw)
In-Reply-To: <20261011170958.1759230-1-libaokun@linux.alibaba.com>
Splitting a bio advances the remaining bio's data unit number by the
number of whole data units that were split off:
bio_split()
└─ bio_advance()
└─ bio_crypt_advance()
└─ bio_crypt_dun_increment(dun, bytes >> data_unit_size_bits)
The shift truncates, so splitting inside a data unit leaves the
remainder with the wrong data unit number and silently corrupts data.
The split size is already rounded down to the logical block size, so
this is only possible when the data unit size is larger than it, e.g.
a fscrypt filesystem with a 4K block size on a device with a 512-byte
logical block size.
Fix this by rounding the split size down to a data unit boundary. The
split alignment becomes the maximum of the logical block size, the
data unit size, and the zone write granularity, so a zoned write still
meets all three requirements.
Split bios can be up to one data unit smaller than the queue limits
allow, so a bio can be split a little more often. The number of
segments for the split bio is still counted up to the size before the
rounding, so it can be larger than needed; it is only used for
accounting.
Fixes: a892c8d52c02 ("block: Inline encryption support for blk-mq")
Cc: stable@vger.kernel.org
Signed-off-by: Baokun Li <libaokun@linux.alibaba.com>
---
block/blk-merge.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/block/blk-merge.c b/block/blk-merge.c
index 258a726071d1..f92460c283e7 100644
--- a/block/blk-merge.c
+++ b/block/blk-merge.c
@@ -308,9 +308,20 @@ static bool bvec_split_segs(const struct queue_limits *lim,
static unsigned int bio_split_alignment(struct bio *bio,
const struct queue_limits *lim)
{
+ const struct bio_crypt_ctx *bc = bio_crypt_ctx(bio);
+ unsigned int align = lim->logical_block_size;
+
+ /*
+ * A split advances the remaining bio's data unit number by whole
+ * data units only (see bio_crypt_advance()), so a split inside a
+ * data unit would make it use the wrong data unit numbers.
+ */
+ if (bc)
+ align = max(align, bc->bc_key->crypto_cfg.data_unit_size);
if (op_is_write(bio_op(bio)) && lim->zone_write_granularity)
- return lim->zone_write_granularity;
- return lim->logical_block_size;
+ align = max(align, lim->zone_write_granularity);
+
+ return align;
}
static inline unsigned int bvec_seg_gap(struct bio_vec *bvprv,
--
2.43.7
next prev parent reply other threads:[~2026-10-11 17:10 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-11 17:09 [PATCH 00/13] block, ext4: allow encryption with a block size larger than the page size Baokun Li
2026-10-11 17:09 ` [PATCH 01/13] ext4: refuse encryption when block size > " Baokun Li
2026-10-11 17:09 ` [PATCH 02/13] ext4: reject encrypted inodes " Baokun Li
2026-10-11 17:09 ` [PATCH 03/13] blk-crypto: reject fallback data unit sizes it can't handle Baokun Li
[not found] ` <sashiko-outbox-167341@kernel.org>
2026-10-12 7:50 ` Baokun Li
2026-10-11 17:09 ` [PATCH 04/13] blk-crypto: decrypt one data unit per crypto request Baokun Li
2026-10-11 17:09 ` [PATCH 05/13] blk-crypto: encrypt " Baokun Li
2026-10-11 17:09 ` [PATCH 06/13] blk-crypto: support data units larger than a page Baokun Li
2026-10-11 17:09 ` [PATCH 07/13] iomap: align the direct I/O bios of encrypted files to the block size Baokun Li
2026-10-11 17:09 ` Baokun Li [this message]
2026-10-11 17:09 ` [PATCH 09/13] block: warn about bios that can't be split Baokun Li
2026-10-11 17:09 ` [PATCH 10/13] block: don't require data unit aligned bio_vecs for encrypted bios Baokun Li
[not found] ` <sashiko-outbox-167346@kernel.org>
2026-10-12 8:09 ` Baokun Li
2026-10-11 17:09 ` [PATCH 11/13] ext4: allow encryption with a block size larger than the page size Baokun Li
2026-10-11 17:09 ` [PATCH 12/13] ext4: send folios that cover a wraparound to the buffer_head path Baokun Li
2026-10-11 17:09 ` [PATCH 13/13] ext4: allow large folios for encrypted files Baokun Li
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=20261011170958.1759230-9-libaokun@linux.alibaba.com \
--to=libaokun@linux.alibaba.com \
--cc=adilger.kernel@dilger.ca \
--cc=axboe@kernel.dk \
--cc=berto@igalia.com \
--cc=brauner@kernel.org \
--cc=djwong@kernel.org \
--cc=ebiggers@kernel.org \
--cc=hch@lst.de \
--cc=jack@suse.cz \
--cc=jaegeuk@kernel.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-fscrypt@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-xfs@vger.kernel.org \
--cc=ojaswin@linux.ibm.com \
--cc=ritesh.list@gmail.com \
--cc=stable@vger.kernel.org \
--cc=tytso@mit.edu \
--cc=yi.zhang@huawei.com \
/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