diff mbox series

dm crypt: fix sysfs name collision when reloading table

Message ID 20201224114617.77530-1-jefflexu@linux.alibaba.com (mailing list archive)
State Rejected, archived
Delegated to: Mike Snitzer
Headers show
Series dm crypt: fix sysfs name collision when reloading table | expand

Commit Message

Jingbo Xu Dec. 24, 2020, 11:46 a.m. UTC
The table reload routine will create new table, with the old table
preserved. In this case, the duplicate name collision will happen when
creating the sysfs directory for the workqueue of the newly created
table, since the old table has not been destroyed at that time.

One workaround for this issue is to add unique hash string to the name
of the sysfs directory. The value of dm_target pointer is used to
generate the hash.

This commit also fixes one hidden bug. Prior to this commit, 'devname'
is used to compensate the name of the workqueue. 'devname' represents
the name of the mapped device, such as '253:0', so there will be name
collision if one mapped device corresponds to multiple target devices.
Since currently a string hashed from dm_target pointer is added to the
name, different target device will have unique name now.

Since then, the exported sysfs directory will be named like
'kcryptd-252:0-3a512302'.

It is worth noting that some details need to be handled specifically.
The %p format of printk will print a hexadecimal string after hashing.
In 64-bit architecture, the output will be 16 bytes long, with the
previous 8 bytes zeroed, such as '00000000abcdef12'. Since the length
of the name of workqueue is limited to WQ_NAME_LEN, i.e. 24 bytes, the
previous 8 bytes zeroed need to be discarded.

Reported-by: Ignat Korchagin <ignat@cloudflare.com>
Fixes: a2b8b2d97567 ("dm crypt: export sysfs of kcryptd workqueue")
Signed-off-by: Jeffle Xu <jefflexu@linux.alibaba.com>
---
 drivers/md/dm-crypt.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c
index 5f9f9b3a226d..31c153ca6f6c 100644
--- a/drivers/md/dm-crypt.c
+++ b/drivers/md/dm-crypt.c
@@ -3169,10 +3169,11 @@  static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 		cc->crypt_queue = alloc_workqueue("kcryptd-%s", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM,
 						  1, devname);
 	else
-		cc->crypt_queue = alloc_workqueue("kcryptd-%s",
+		cc->crypt_queue = alloc_workqueue("kcryptd-%s-%8p",
 						  WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM |
 						  WQ_UNBOUND | WQ_SYSFS,
-						  num_online_cpus(), devname);
+						  num_online_cpus(), devname, ti);
+
 	if (!cc->crypt_queue) {
 		ti->error = "Couldn't create kcryptd queue";
 		goto bad;