Skip to contentSkip to conversation

mtd: fix lock hierarchy in deregister_mtd_blktrans

PATCH Applied Desmond Cheong Zhi Xi 2 participants 3 messages Jul 17, 2021 – Aug 6, 2021 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: fix lock hierarchy in deregister_mtd_blktrans

There is a lock hierarchy of major_names_lock --> mtd_table_mutex. One existing chain is as follows:

1. major_names_lock --> loop_ctl_mutex (when blk_request_module calls loop_probe)

2. loop_ctl_mutex --> bdev->bd_mutex (when loop_control_ioctl calls loop_remove, which then calls del_gendisk)

3. bdev->bd_mutex --> mtd_table_mutex (when blkdev_get_by_dev calls __blkdev_get, which then calls blktrans_open)

Since unregister_blkdev grabs the major_names_lock, we need to call it outside the critical section for mtd_table_mutex, otherwise we invert the lock hierarchy.

---
 drivers/mtd/mtd_blkdevs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/mtd_blkdevs.c +1 -1
index 7d26cfe24d05..87e26788ef60 100644
@@ -563,8 +563,8 @@ int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
 	list_for_each_entry_safe(dev, next, &tr->devs, list)
 		tr->remove_dev(dev);
 
-	unregister_blkdev(tr->major, tr->name);
 	mutex_unlock(&mtd_table_mutex);
+	unregister_blkdev(tr->major, tr->name);
 
 	BUG_ON(!list_empty(&tr->devs));
 	return 0;
Signature · 6 lines
2.25.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#1 lore raw
Quoted 28 lines · Desmond Cheong Zhi Xi

There is a lock hierarchy of major_names_lock --> mtd_table_mutex. One existing chain is as follows:

1. major_names_lock --> loop_ctl_mutex (when blk_request_module calls loop_probe)

2. loop_ctl_mutex --> bdev->bd_mutex (when loop_control_ioctl calls loop_remove, which then calls del_gendisk)

3. bdev->bd_mutex --> mtd_table_mutex (when blkdev_get_by_dev calls __blkdev_get, which then calls blktrans_open)

Since unregister_blkdev grabs the major_names_lock, we need to call it outside the critical section for mtd_table_mutex, otherwise we invert the lock hierarchy.

---
  drivers/mtd/mtd_blkdevs.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/mtd_blkdevs.c +0 -0
index 7d26cfe24d05..87e26788ef60 100644

drivers/mtd/mtd_blkdevs.c int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr) · line 570

Quoted 9 lines
drivers/mtd/mtd_blkdevs.c +1 -1
@@ -563,8 +563,8 @@ int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
  	list_for_each_entry_safe(dev, next, &tr->devs, list)
  		tr->remove_dev(dev);
 
-	unregister_blkdev(tr->major, tr->name);
  	mutex_unlock(&mtd_table_mutex);
+	unregister_blkdev(tr->major, tr->name);
 
  	BUG_ON(!list_empty(&tr->devs));
  	return 0;

Hi Miquèl,

Just a friendly ping, this patch is part 2 of fixing the lock hierarchy inversion between major_names_lock and mtd_table_mutex that was identified by Hillf Danton.

Best wishes,
Desmond

maintainer Applied
#2 lore raw
In reply to Desmond Cheong Zhi Xi
Quoted 12 lines · Desmond Cheong Zhi Xi

There is a lock hierarchy of major_names_lock --> mtd_table_mutex. One existing chain is as follows:

1. major_names_lock --> loop_ctl_mutex (when blk_request_module calls loop_probe)

2. loop_ctl_mutex --> bdev->bd_mutex (when loop_control_ioctl calls loop_remove, which then calls del_gendisk)

3. bdev->bd_mutex --> mtd_table_mutex (when blkdev_get_by_dev calls __blkdev_get, which then calls blktrans_open)

Quoted 6 lines

Since unregister_blkdev grabs the major_names_lock, we need to call it outside the critical section for mtd_table_mutex, otherwise we invert the lock hierarchy.

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/fixes, thanks.

Miquel