The "sector_size:<bytes>" option is capped at 4096 bytes, the smallest PAGE_SIZE of any supported architecture. A kernel with a larger PAGE_SIZE can use a larger encryption unit, which turns several crypto requests per page into a single one.
That only pays off when a request carries a large fixed cost, which is the case for drivers that offload to hardware over DMA: setting the transfer up dominates, so doing it once per 64 KiB instead of sixteen times is worth a lot. On an arm64 64K-page system driving the in-tree qce driver, dm-crypt throughput rose from 13-27 MB/s to about 580 MB/s when the encryption sector size was raised from 4096 to 65536. A CPU cipher has no such fixed cost and gains little: 9-16% measured with xts-aes-ce on NVMe. Raise the cap to min(PAGE_SIZE, BLK_MAX_BLOCK_SIZE). dm-verity already bounds its data block size the same way, rejecting "num > PAGE_SIZE" in verity_ctr(), so this is the bound dm targets already use rather than a new kind of limit. PAGE_SIZE is the ceiling of the current conversion path: a sector is passed to the crypto API as a single scatterlist entry, bio_iter_iovec() never returns more than PAGE_SIZE bytes, and crypt_alloc_buffer() may fall back to order-0 pages for the write bounce buffer. BLK_MAX_BLOCK_SIZE is the block layer's own cap on the logical block size that crypt_io_hints() announces. It does not lower the limit today - it is 64K only when transparent hugepages are enabled, and no architecture that can enable them has a PAGE_SIZE above 64K, while without them it is PAGE_SIZE - so the effective bound is PAGE_SIZE. It is in the expression so that this target cannot announce a block size blk_validate_limits() would reject should that ever change. Widen sector_size to unsigned int so that it can hold 65536. That also makes the option reject values that %hu silently truncated: an argument of 69632 currently wraps to 4096 and is accepted as a 4096-byte sector. Apart from that, every table accepted before is still accepted. The larger sizes are opt-in - the default stays 512 bytes - and nothing changes at all where PAGE_SIZE is 4096. A mapping above 4096 bytes can only be activated where PAGE_SIZE allows, so it is not suitable for portable on-disk formats such as LUKS. Bump the target version so that userspace can detect the new limit. Assisted-by: LLM Signed-off-by: Itai Handler <[email protected]> --- .../admin-guide/device-mapper/dm-crypt.rst | 8 ++++- drivers/md/dm-crypt.c | 30 +++++++++++++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/Documentation/admin-guide/device-mapper/dm-crypt.rst b/Documentation/admin-guide/device-mapper/dm-crypt.rst index 4467f6d..250da7e 100644 --- a/Documentation/admin-guide/device-mapper/dm-crypt.rst +++ b/Documentation/admin-guide/device-mapper/dm-crypt.rst @@ -153,9 +153,15 @@ integrity_key_size:<bytes> sector_size:<bytes> Use <bytes> as the encryption unit instead of 512 bytes sectors. - This option can be in range 512 - 4096 bytes and must be power of two. + This option can be in range 512 - PAGE_SIZE bytes, with an upper bound + of 65536, and must be power of two. Virtual device will announce this size as a minimal IO and logical sector. + An encryption unit larger than 4096 bytes can only be used on a system + whose PAGE_SIZE is at least that large, so such a mapping is not + portable across architectures and is unsuitable for portable on-disk + formats such as LUKS. + iv_large_sectors IV generators will use sector number counted in <sector_size> units instead of default 512 bytes sectors. diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c index 9e170de..0f087c5 100644 --- a/drivers/md/dm-crypt.c +++ b/drivers/md/dm-crypt.c @@ -182,7 +182,7 @@ struct crypt_config { } iv_gen_private; u64 iv_offset; unsigned int iv_size; - unsigned short sector_size; + unsigned int sector_size; unsigned char sector_shift; union { @@ -241,6 +241,24 @@ struct crypt_config { #define MAX_TAG_SIZE 480 #define POOL_ENTRY_SIZE 512 +/* + * Largest encryption sector size that can be requested with the + * "sector_size:<bytes>" option. + * + * A sector is handed to the crypto API as a single scatterlist entry, so it + * has to be covered by one bio_vec. bio_iter_iovec() never returns more than + * PAGE_SIZE bytes, and crypt_alloc_buffer() may fall back to order-0 pages + * for the write bounce buffer, so PAGE_SIZE is the ceiling. + * + * crypt_io_hints() announces the sector size as the logical block size, which + * the block layer caps at BLK_MAX_BLOCK_SIZE. That cap is never below + * PAGE_SIZE in any configuration today, so it does not lower the limit; take + * the minimum anyway so that this target cannot announce a block size + * blk_validate_limits() would reject. + */ +#define DM_CRYPT_MAX_SECTOR_SIZE min_t(unsigned int, PAGE_SIZE, \ + BLK_MAX_BLOCK_SIZE) + static DEFINE_SPINLOCK(dm_crypt_clients_lock); static unsigned int dm_crypt_clients_n; static volatile unsigned long dm_crypt_pages_per_client; @@ -3134,9 +3152,9 @@ static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **ar } cc->key_mac_size = val; set_bit(CRYPT_KEY_MAC_SIZE_SET, &cc->cipher_flags); - } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) { + } else if (sscanf(opt_string, "sector_size:%u%c", &cc->sector_size, &dummy) == 1) { if (cc->sector_size < (1 << SECTOR_SHIFT) || - cc->sector_size > 4096 || + cc->sector_size > DM_CRYPT_MAX_SECTOR_SIZE || (cc->sector_size & (cc->sector_size - 1))) { ti->error = "Invalid feature value for sector_size"; return -EINVAL; @@ -3556,7 +3574,7 @@ static void crypt_status(struct dm_target *ti, status_type_t type, if (cc->used_tag_size) DMEMIT(" integrity:%u:%s", cc->used_tag_size, cc->cipher_auth); if (cc->sector_size != (1 << SECTOR_SHIFT)) - DMEMIT(" sector_size:%d", cc->sector_size); + DMEMIT(" sector_size:%u", cc->sector_size); if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) DMEMIT(" iv_large_sectors"); if (test_bit(CRYPT_KEY_MAC_SIZE_SET, &cc->cipher_flags)) @@ -3582,7 +3600,7 @@ static void crypt_status(struct dm_target *ti, status_type_t type, DMEMIT(",integrity_tag_size=%u,cipher_auth=%s", cc->used_tag_size, cc->cipher_auth); if (cc->sector_size != (1 << SECTOR_SHIFT)) - DMEMIT(",sector_size=%d", cc->sector_size); + DMEMIT(",sector_size=%u", cc->sector_size); if (cc->cipher_string) DMEMIT(",cipher_string=%s", cc->cipher_string); @@ -3700,7 +3718,7 @@ static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits) static struct target_type crypt_target = { .name = "crypt", - .version = {1, 29, 0}, + .version = {1, 30, 0}, .module = THIS_MODULE, .ctr = crypt_ctr, .dtr = crypt_dtr, -- 2.34.1

