@@ -214,7 +214,8 @@ struct crypt_config {
214214
215215 unsigned int integrity_tag_size ;
216216 unsigned int integrity_iv_size ;
217- unsigned int on_disk_tag_size ;
217+ unsigned int used_tag_size ;
218+ unsigned int tuple_size ;
218219
219220 /*
220221 * pool for per bio private data, crypto requests,
@@ -241,6 +242,31 @@ static unsigned int dm_crypt_clients_n;
241242static volatile unsigned long dm_crypt_pages_per_client ;
242243#define DM_CRYPT_MEMORY_PERCENT 2
243244#define DM_CRYPT_MIN_PAGES_PER_CLIENT (BIO_MAX_VECS * 16)
245+ #define DM_CRYPT_DEFAULT_MAX_READ_SIZE 131072
246+ #define DM_CRYPT_DEFAULT_MAX_WRITE_SIZE 131072
247+
248+ static unsigned int max_read_size = 0 ;
249+ module_param (max_read_size , uint , 0644 );
250+ MODULE_PARM_DESC (max_read_size , "Maximum size of a read request" );
251+ static unsigned int max_write_size = 0 ;
252+ module_param (max_write_size , uint , 0644 );
253+ MODULE_PARM_DESC (max_write_size , "Maximum size of a write request" );
254+ static unsigned get_max_request_size (struct crypt_config * cc , bool wrt )
255+ {
256+ unsigned val , sector_align ;
257+ val = !wrt ? READ_ONCE (max_read_size ) : READ_ONCE (max_write_size );
258+ if (likely (!val ))
259+ val = !wrt ? DM_CRYPT_DEFAULT_MAX_READ_SIZE : DM_CRYPT_DEFAULT_MAX_WRITE_SIZE ;
260+ if (wrt || cc -> used_tag_size ) {
261+ if (unlikely (val > BIO_MAX_VECS << PAGE_SHIFT ))
262+ val = BIO_MAX_VECS << PAGE_SHIFT ;
263+ }
264+ sector_align = max (bdev_logical_block_size (cc -> dev -> bdev ), (unsigned )cc -> sector_size );
265+ val = round_down (val , sector_align );
266+ if (unlikely (!val ))
267+ val = sector_align ;
268+ return val >> SECTOR_SHIFT ;
269+ }
244270
245271static void crypt_endio (struct bio * clone );
246272static void kcryptd_queue_crypt (struct dm_crypt_io * io );
@@ -1151,14 +1177,14 @@ static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio)
11511177 unsigned int tag_len ;
11521178 int ret ;
11531179
1154- if (!bio_sectors (bio ) || !io -> cc -> on_disk_tag_size )
1180+ if (!bio_sectors (bio ) || !io -> cc -> tuple_size )
11551181 return 0 ;
11561182
11571183 bip = bio_integrity_alloc (bio , GFP_NOIO , 1 );
11581184 if (IS_ERR (bip ))
11591185 return PTR_ERR (bip );
11601186
1161- tag_len = io -> cc -> on_disk_tag_size * (bio_sectors (bio ) >> io -> cc -> sector_shift );
1187+ tag_len = io -> cc -> tuple_size * (bio_sectors (bio ) >> io -> cc -> sector_shift );
11621188
11631189 bip -> bip_iter .bi_sector = io -> cc -> start + io -> sector ;
11641190
@@ -1182,18 +1208,18 @@ static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti)
11821208 return - EINVAL ;
11831209 }
11841210
1185- if (bi -> tag_size != cc -> on_disk_tag_size ||
1186- bi -> tuple_size != cc -> on_disk_tag_size ) {
1211+ if (bi -> tuple_size < cc -> used_tag_size ) {
11871212 ti -> error = "Integrity profile tag size mismatch." ;
11881213 return - EINVAL ;
11891214 }
1215+ cc -> tuple_size = bi -> tuple_size ;
11901216 if (1 << bi -> interval_exp != cc -> sector_size ) {
11911217 ti -> error = "Integrity profile sector size mismatch." ;
11921218 return - EINVAL ;
11931219 }
11941220
11951221 if (crypt_integrity_aead (cc )) {
1196- cc -> integrity_tag_size = cc -> on_disk_tag_size - cc -> integrity_iv_size ;
1222+ cc -> integrity_tag_size = cc -> used_tag_size - cc -> integrity_iv_size ;
11971223 DMDEBUG ("%s: Integrity AEAD, tag size %u, IV size %u." , dm_device_name (md ),
11981224 cc -> integrity_tag_size , cc -> integrity_iv_size );
11991225
@@ -1205,7 +1231,7 @@ static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti)
12051231 DMDEBUG ("%s: Additional per-sector space %u bytes for IV." , dm_device_name (md ),
12061232 cc -> integrity_iv_size );
12071233
1208- if ((cc -> integrity_tag_size + cc -> integrity_iv_size ) != bi -> tag_size ) {
1234+ if ((cc -> integrity_tag_size + cc -> integrity_iv_size ) > cc -> tuple_size ) {
12091235 ti -> error = "Not enough space for integrity tag in the profile." ;
12101236 return - EINVAL ;
12111237 }
@@ -1284,7 +1310,7 @@ static void *tag_from_dmreq(struct crypt_config *cc,
12841310 struct dm_crypt_io * io = container_of (ctx , struct dm_crypt_io , ctx );
12851311
12861312 return & io -> integrity_metadata [* org_tag_of_dmreq (cc , dmreq ) *
1287- cc -> on_disk_tag_size ];
1313+ cc -> tuple_size ];
12881314}
12891315
12901316static void * iv_tag_from_dmreq (struct crypt_config * cc ,
@@ -1365,9 +1391,9 @@ static int crypt_convert_block_aead(struct crypt_config *cc,
13651391 aead_request_set_crypt (req , dmreq -> sg_in , dmreq -> sg_out ,
13661392 cc -> sector_size , iv );
13671393 r = crypto_aead_encrypt (req );
1368- if (cc -> integrity_tag_size + cc -> integrity_iv_size != cc -> on_disk_tag_size )
1394+ if (cc -> integrity_tag_size + cc -> integrity_iv_size != cc -> tuple_size )
13691395 memset (tag + cc -> integrity_tag_size + cc -> integrity_iv_size , 0 ,
1370- cc -> on_disk_tag_size - (cc -> integrity_tag_size + cc -> integrity_iv_size ));
1396+ cc -> tuple_size - (cc -> integrity_tag_size + cc -> integrity_iv_size ));
13711397 } else {
13721398 aead_request_set_crypt (req , dmreq -> sg_in , dmreq -> sg_out ,
13731399 cc -> sector_size + cc -> integrity_tag_size , iv );
@@ -1797,7 +1823,7 @@ static void crypt_dec_pending(struct dm_crypt_io *io)
17971823 return ;
17981824
17991825 if (likely (!io -> ctx .aead_recheck ) && unlikely (io -> ctx .aead_failed ) &&
1800- cc -> on_disk_tag_size && bio_data_dir (base_bio ) == READ ) {
1826+ cc -> used_tag_size && bio_data_dir (base_bio ) == READ ) {
18011827 io -> ctx .aead_recheck = true;
18021828 io -> ctx .aead_failed = false;
18031829 io -> error = 0 ;
@@ -3181,7 +3207,7 @@ static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **ar
31813207 ti -> error = "Invalid integrity arguments" ;
31823208 return - EINVAL ;
31833209 }
3184- cc -> on_disk_tag_size = val ;
3210+ cc -> used_tag_size = val ;
31853211 sval = strchr (opt_string + strlen ("integrity:" ), ':' ) + 1 ;
31863212 if (!strcasecmp (sval , "aead" )) {
31873213 set_bit (CRYPT_MODE_INTEGRITY_AEAD , & cc -> cipher_flags );
@@ -3393,12 +3419,12 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
33933419 if (ret )
33943420 goto bad ;
33953421
3396- cc -> tag_pool_max_sectors = POOL_ENTRY_SIZE / cc -> on_disk_tag_size ;
3422+ cc -> tag_pool_max_sectors = POOL_ENTRY_SIZE / cc -> tuple_size ;
33973423 if (!cc -> tag_pool_max_sectors )
33983424 cc -> tag_pool_max_sectors = 1 ;
33993425
34003426 ret = mempool_init_kmalloc_pool (& cc -> tag_pool , MIN_IOS ,
3401- cc -> tag_pool_max_sectors * cc -> on_disk_tag_size );
3427+ cc -> tag_pool_max_sectors * cc -> tuple_size );
34023428 if (ret ) {
34033429 ti -> error = "Cannot allocate integrity tags mempool" ;
34043430 goto bad ;
@@ -3474,6 +3500,7 @@ static int crypt_map(struct dm_target *ti, struct bio *bio)
34743500{
34753501 struct dm_crypt_io * io ;
34763502 struct crypt_config * cc = ti -> private ;
3503+ unsigned max_sectors ;
34773504
34783505 /*
34793506 * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
@@ -3492,9 +3519,9 @@ static int crypt_map(struct dm_target *ti, struct bio *bio)
34923519 /*
34933520 * Check if bio is too large, split as needed.
34943521 */
3495- if ( unlikely (bio -> bi_iter . bi_size > ( BIO_MAX_VECS << PAGE_SHIFT )) &&
3496- ( bio_data_dir ( bio ) == WRITE || cc -> on_disk_tag_size ))
3497- dm_accept_partial_bio (bio , (( BIO_MAX_VECS << PAGE_SHIFT ) >> SECTOR_SHIFT ) );
3522+ max_sectors = get_max_request_size ( cc , bio_data_dir (bio ) == WRITE );
3523+ if ( unlikely ( bio_sectors ( bio ) > max_sectors ))
3524+ dm_accept_partial_bio (bio , max_sectors );
34983525
34993526 /*
35003527 * Ensure that bio is a multiple of internal sector encryption size
@@ -3509,8 +3536,8 @@ static int crypt_map(struct dm_target *ti, struct bio *bio)
35093536 io = dm_per_bio_data (bio , cc -> per_bio_data_size );
35103537 crypt_io_init (io , cc , bio , dm_target_offset (ti , bio -> bi_iter .bi_sector ));
35113538
3512- if (cc -> on_disk_tag_size ) {
3513- unsigned int tag_len = cc -> on_disk_tag_size * (bio_sectors (bio ) >> cc -> sector_shift );
3539+ if (cc -> tuple_size ) {
3540+ unsigned int tag_len = cc -> tuple_size * (bio_sectors (bio ) >> cc -> sector_shift );
35143541
35153542 if (unlikely (tag_len > KMALLOC_MAX_SIZE ))
35163543 io -> integrity_metadata = NULL ;
@@ -3582,7 +3609,7 @@ static void crypt_status(struct dm_target *ti, status_type_t type,
35823609 num_feature_args += test_bit (DM_CRYPT_NO_WRITE_WORKQUEUE , & cc -> flags );
35833610 num_feature_args += cc -> sector_size != (1 << SECTOR_SHIFT );
35843611 num_feature_args += test_bit (CRYPT_IV_LARGE_SECTORS , & cc -> cipher_flags );
3585- if (cc -> on_disk_tag_size )
3612+ if (cc -> used_tag_size )
35863613 num_feature_args ++ ;
35873614 if (num_feature_args ) {
35883615 DMEMIT (" %d" , num_feature_args );
@@ -3598,8 +3625,8 @@ static void crypt_status(struct dm_target *ti, status_type_t type,
35983625 DMEMIT (" no_read_workqueue" );
35993626 if (test_bit (DM_CRYPT_NO_WRITE_WORKQUEUE , & cc -> flags ))
36003627 DMEMIT (" no_write_workqueue" );
3601- if (cc -> on_disk_tag_size )
3602- DMEMIT (" integrity:%u:%s" , cc -> on_disk_tag_size , cc -> cipher_auth );
3628+ if (cc -> used_tag_size )
3629+ DMEMIT (" integrity:%u:%s" , cc -> used_tag_size , cc -> cipher_auth );
36033630 if (cc -> sector_size != (1 << SECTOR_SHIFT ))
36043631 DMEMIT (" sector_size:%d" , cc -> sector_size );
36053632 if (test_bit (CRYPT_IV_LARGE_SECTORS , & cc -> cipher_flags ))
@@ -3621,9 +3648,9 @@ static void crypt_status(struct dm_target *ti, status_type_t type,
36213648 DMEMIT (",iv_large_sectors=%c" , test_bit (CRYPT_IV_LARGE_SECTORS , & cc -> cipher_flags ) ?
36223649 'y' : 'n' );
36233650
3624- if (cc -> on_disk_tag_size )
3651+ if (cc -> used_tag_size )
36253652 DMEMIT (",integrity_tag_size=%u,cipher_auth=%s" ,
3626- cc -> on_disk_tag_size , cc -> cipher_auth );
3653+ cc -> used_tag_size , cc -> cipher_auth );
36273654 if (cc -> sector_size != (1 << SECTOR_SHIFT ))
36283655 DMEMIT (",sector_size=%d" , cc -> sector_size );
36293656 if (cc -> cipher_string )
@@ -3731,7 +3758,7 @@ static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
37313758
37323759static struct target_type crypt_target = {
37333760 .name = "crypt" ,
3734- .version = {1 , 26 , 0 },
3761+ .version = {1 , 27 , 0 },
37353762 .module = THIS_MODULE ,
37363763 .ctr = crypt_ctr ,
37373764 .dtr = crypt_dtr ,
0 commit comments