Skip to content

block, ext4: allow encryption with a block size larger than the page size

Baokun Li linux-block linux-btrfs linux-ext4 linux-fscrypt linux-fsdevel linux-xfs sashiko-reviews stable 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest13 patches 9 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/13ext4: refuse encryption when block size > page sizefs/ext4/crypto.c+9 -00 replies
2/13ext4: reject encrypted inodes when block size > page sizefs/ext4/inode.c+6 -00 replies
3/13blk-crypto: reject fallback data unit sizes it can't handleblock/blk-crypto-fallback.c block/blk-crypto-internal.h block/blk-crypto.c+25 -133 replies
4/13blk-crypto: decrypt one data unit per crypto requestblock/blk-crypto-fallback.c+135 -270 replies
5/13blk-crypto: encrypt one data unit per crypto requestblock/blk-crypto-fallback.c+85 -460 replies
6/13blk-crypto: support data units larger than a pageblock/blk-crypto-fallback.c+140 -310 replies
7/13iomap: align the direct I/O bios of encrypted files to the block sizefs/iomap/direct-io.c+10 -00 replies
8/13block: split encrypted bios at data unit boundariesblock/blk-merge.c+13 -20 replies
9/13block: warn about bios that can't be splitblock/blk-merge.c+13 -60 replies
10/13block: don't require data unit aligned bio_vecs for encrypted biosDocumentation/block/inline-encryption.rst block/blk-crypto.c block/blk-merge.c +1 more+48 -81 reply
11/13ext4: allow encryption with a block size larger than the page sizefs/ext4/crypto.c fs/ext4/inode.c fs/ext4/super.c+3 -370 replies
12/13ext4: send folios that cover a wraparound to the buffer_head pathfs/ext4/readpage.c+11 -00 replies
13/13ext4: allow large folios for encrypted filesfs/ext4/super.c+1 -20 replies

Reviews trailers per patch

2 of 13 patches have a Reviewed-by or Acked-by.

PatchBaokun LiEric Biggers
1/13R-b R-b
2/13R-b R-b
3/13
4/13
5/13
6/13
7/13
8/13
9/13
10/13
11/13
12/13
13/13

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [PATCH 00/13] block, ext4: allow encryption with a block size larger than the page size

Motivation
==========

ext4 supports a block size larger than the page size (LBS), e.g. a 32k block size on a machine with 4k pages [1]. Encryption, however, is still refused on such a filesystem: file contents used to be encrypted page by page in the fs-layer path, which cannot handle a folio that covers several data units, so the checks that refuse encryption with such a block size stayed in place.

Since commit 5acc6f649a68 ("fscrypt: Always use blk-crypto for contents on block-based filesystems") file contents are always encrypted through blk-crypto. Inline encryption hardware can already en/decrypt a data unit that spans several pages, but the fallback and the block layer cannot carry such a data unit:

1. blk-crypto-fallback en/decrypts a data unit with the bio_vecs of a single page-clamped segment, so a larger data unit fails with BLK_STS_INVAL;
2. the block layer requires every bio_vec of an encrypted bio to be data unit aligned, and splits such a bio without rounding the split size down to the data unit size;
3. iomap cuts the direct I/O bios of an encrypted file only at the logical block size of the device, so a bio can end in the middle of a data unit.

This series adds the missing pieces on all three layers, removes the refusals, and enables large folios for encrypted files on the way.

[1]: https://lore.kernel.org/all/[email protected]

Patch Overview
==============

* Patches 1-2 (ext4, from Alberto Garcia) [2]: Refuse to set an encryption policy when the block size is larger than the page size, and reject encrypted inodes of such a filesystem. They are the fix for the stable branches; patch 11 removes the refusals again for mainline. They are carried along unchanged so that the series applies cleanly on its own and avoids adaptation conflicts with the stable fix.

* Patch 3 (blk-crypto): Reject the data unit sizes that blk-crypto-fallback can't handle when the key is set up, instead of failing every I/O with BLK_STS_INVAL.

* Patches 4-6 (blk-crypto-fallback): En/decrypt one data unit per crypto request, so that a data unit can span several bio_vecs, and support the data units that are larger than a page, whose bio_vecs and scatterlists come from a mempool.

* Patch 7 (iomap): Cut the direct I/O bios of encrypted files at the block size, so that they cover whole data units.

* Patches 8-10 (block): Round the split size of an encrypted bio down to the data unit size, warn when a bio that must be split cannot be split into a valid request, and drop the per-bio_vec data unit alignment requirement, which a data unit larger than a page cannot satisfy. The contract is documented: an encrypted bio starts at a data unit boundary and covers whole data units.

* Patches 11-13 (ext4): Remove the refusals, route the folios that cover a data unit number wraparound to the buffer_head read path, and enable large folios for encrypted files.

[2]: https://lore.kernel.org/r/[email protected]

Benchmark
=========

Hardware: 96-core machine, 3.84T NVMe, 512G memory
Filesystem: ext4, encryption on the fallback path

Buffered sequential 1M writes and reads (1G, fsync) in MiB/s; medians of 3 runs.

Base tree, 4k-block filesystem:

  job         4k plain    4k enc
  --------    --------    ------

seqwrite 1966 1134
seqread 1878 996

With LBS enabled, encrypted 4k-block vs 32k-block filesystem:

  job         4k enc    32k enc    32k plain    4k->32k
  --------    ------    -------    ---------    -------
  seqwrite      1044       1399         2062       +34%
  seqread        931       1793         1791       +93%

Buffered sequential writes (1G, fsync) of the encrypted 4k-block filesystem across request sizes, base tree vs this series:

  bs      Before    After    gain
  ----    ------    -----    ----
  4k        1118     1162     +4%
  16k       1260     1756    +39%
  64k       1272     1977    +55%
  256k      1308     2081    +59%
  1M        1260     1455    +15%

Testing
=======

* kvm-xfstests -c ext4/all -g encrypt (which includes ext4/32k) has been executed with no new failures.

* Manually verified read/write, fallocate -z and punch hole on 4k, 8k, 16k and 32k block sizes, and direct I/O with a properly aligned user buffer on 4k, 8k and 32k block sizes.

* Manually verified fsx and fsstress on a 64k block size, and direct I/O to an encrypted file on a 64k block size with a user buffer backed by large folios.

Comments and questions are, as always, welcome.

Thanks,
Baokun

Alberto Garcia (2):
ext4: refuse encryption when block size > page size
ext4: reject encrypted inodes when block size > page size

Baokun Li (11):
blk-crypto: reject fallback data unit sizes it can't handle
blk-crypto: decrypt one data unit per crypto request
blk-crypto: encrypt one data unit per crypto request
blk-crypto: support data units larger than a page
iomap: align the direct I/O bios of encrypted files to the block size
block: split encrypted bios at data unit boundaries
block: warn about bios that can't be split
block: don't require data unit aligned bio_vecs for encrypted bios
ext4: allow encryption with a block size larger than the page size
ext4: send folios that cover a wraparound to the buffer_head path
ext4: allow large folios for encrypted files

 Documentation/block/inline-encryption.rst |  13 +
 block/blk-crypto-fallback.c               | 446 +++++++++++++++++-----
 block/blk-crypto-internal.h               |   4 +-
 block/blk-crypto.c                        |  40 +-
 block/blk-merge.c                         |  48 ++-
 fs/crypto/block.c                         |   3 +
 fs/ext4/readpage.c                        |  11 +
 fs/ext4/super.c                           |  22 +-
 fs/iomap/direct-io.c                      |  10 +
 9 files changed, 462 insertions(+), 135 deletions(-)

base-commit: fd73f4a6659897191fa0d40695fe370925dd3780

Signature · 1 line
2.43.7