dm crypt: Always disable discard zeroes data flag
PATCH 1/1 dm crypt: Always disable discard zeroes data flag
If optional discard support in dm-crypt is enabled, discards requests bypass crypt queue and blocks of the underlying device are discarded.
But for read path, discarded blocks are handled as the same as normal ciphertext blocks, thus decrypted.
So if underlying device announces discarded regions return zeroes, dm-crypt must disable this flag because after decryption there is just random noise instead of zeroes.
--- drivers/md/dm-crypt.c | 1 + drivers/md/dm-table.c | 19 +++++++++++++++++++ include/linux/device-mapper.h | 5 +++++ 3 files changed, 25 insertions(+), 0 deletions(-)
drivers/md/dm-crypt.c +1 -0
@@ -1698,6 +1698,7 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv) } ti->num_flush_requests =1 ; + ti->discard_zeroes_unsupported =1 ;return 0 ; bad:
drivers/md/dm-table.c +19 -0
@@ -1330,6 +1330,22 @@ static bool dm_table_is_nonrot(struct dm_table *t)return 1 ; } +static bool dm_table_discard_zeroes (struct dm_table *t) +{ +struct dm_target *ti; +unsigned i =0 ; + +/* Ensure that all targets supports discard zeroes. */ +while (i <dm_table_get_num_targets (t)) { + ti =dm_table_get_target (t, i++); + +if (ti->discard_zeroes_unsupported) +return 0 ; + } + +return 1 ; +} +void dm_table_set_restrictions (struct dm_table *t,struct request_queue *q,struct queue_limits *limits) {
@@ -1357,6 +1373,9 @@ void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,else queue_flag_clear_unlocked (QUEUE_FLAG_NONROT , q); +if (!dm_table_discard_zeroes (t)) + q->limits.discard_zeroes_data =0 ; +dm_table_set_integrity (t);/*
include/linux/device-mapper.h +5 -0
@@ -208,6 +208,11 @@ struct dm_target {* whether or not its underlying devices have support. */ unsigned discards_supported:1; + +/* +* Set if this target does not return zeroes on discarded blocks. +*/ + unsigned discard_zeroes_unsupported:1; };/* Each target can link one of these into the table */
Signature · 1 line
1.7.5.4
Quoted 23 lines · Milan Broz
If optional discard support in dm-crypt is enabled, discards requests bypass crypt queue and blocks of the underlying device are discarded.
But for read path, discarded blocks are handled as the same as normal ciphertext blocks, thus decrypted.
So if underlying device announces discarded regions return zeroes, dm-crypt must disable this flag because after decryption there is just random noise instead of zeroes.
Signed-off-by Milan Broz <[email protected]>--- drivers/md/dm-crypt.c | 1 + drivers/md/dm-table.c | 19 +++++++++++++++++++ include/linux/device-mapper.h | 5 +++++ 3 files changed, 25 insertions(+), 0 deletions(-)drivers/md/dm-crypt.c +0
-0
drivers/md/dm-crypt.c static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv) · line 1701
Quoted 4 lines
drivers/md/dm-crypt.c +1
-0@@ -1698,6 +1698,7 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv) } ti->num_flush_requests =1 ; + ti->discard_zeroes_unsupported =1 ;
I'd prefer 'discard_zeroes_data_unsupported'
drivers/md/dm-table.c static bool dm_table_is_nonrot(struct dm_table *t) · line 1333
Quoted 12 lines
drivers/md/dm-crypt.c +0
-0return 0 ; bad:drivers/md/dm-table.c +1
-0@@ -1330,6 +1330,22 @@ static bool dm_table_is_nonrot(struct dm_table *t)return 1 ; } +static bool dm_table_discard_zeroes (struct dm_table *t)
Likewise, dm_table_discard_zeroes_data().
Quoted 51 lines
drivers/md/dm-table.c +18
-0+{ +struct dm_target *ti; +unsigned i =0 ; + +/* Ensure that all targets supports discard zeroes. */ +while (i <dm_table_get_num_targets (t)) { + ti =dm_table_get_target (t, i++); + +if (ti->discard_zeroes_unsupported) +return 0 ; + } + +return 1 ; +} +void dm_table_set_restrictions (struct dm_table *t,struct request_queue *q,struct queue_limits *limits) {@@ -1357,6 +1373,9 @@ void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,else queue_flag_clear_unlocked (QUEUE_FLAG_NONROT , q); +if (!dm_table_discard_zeroes (t)) + q->limits.discard_zeroes_data =0 ; +dm_table_set_integrity (t);/* include/linux/device-mapper.h +5
-0@@ -208,6 +208,11 @@ struct dm_target {* whether or not its underlying devices have support. */ unsigned discards_supported:1; + +/* +* Set if this target does not return zeroes on discarded blocks. +*/ + unsigned discard_zeroes_unsupported:1; };/* Each target can link one of these into the table */ Signature · 6 lines
1.7.5.4 -- dm-devel mailing list [email protected] https://www.redhat.com/mailman/listinfo/dm-devel
Quoted 3 lines · Mike Snitzer
drivers/md/dm-crypt.c static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv) · line 1701
Quoted 1 line
drivers/md/dm-crypt.c +1
-0+ ti->discard_zeroes_unsupported =1 ;I'd prefer '
discard_zeroes_data_unsupported'
It was too long and I am lazy typist :-)
In fact, I know Alasdair will rename it anyway so
I do not care about the final name.
Milan