# Re: \[PATCH\] mtd: fix lock hierarchy in deregister\_mtd\_blktrans

- From: Miquel Raynal \<miquel.raynal@bootlin.com\>
- Date: 2021-08-06 19:46:02 
- List: linux-kernel-mentees
- Page: https://ratatoskr.run/linux-kernel-mentees/2021/08/1470534
- Thread: https://ratatoskr.run/lkml/2021/07/1448101/t.md

```text
On Sat, 2021-07-17 at 10:07:19 UTC, Desmond Cheong Zhi Xi wrote:
> 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.
> 
> Reported-by: Hillf Danton <redacted>
> Signed-off-by: Desmond Cheong Zhi Xi <redacted>

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

Miquel
_______________________________________________
Linux-kernel-mentees mailing list
Linux-kernel-mentees@lists.linuxfoundation.org
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
```
