Sidong Yang d4aef1e122 btrfs: qgroup: fix deadlock between rescan worker and remove qgroup
The commit e804861bd4e6 ("btrfs: fix deadlock between quota disable and
qgroup rescan worker") by Kawasaki resolves deadlock between quota
disable and qgroup rescan worker. But also there is a deadlock case like
it. It's about enabling or disabling quota and creating or removing
qgroup. It can be reproduced in simple script below.

for i in {1..100}
do
    btrfs quota enable /mnt &
    btrfs qgroup create 1/0 /mnt &
    btrfs qgroup destroy 1/0 /mnt &
    btrfs quota disable /mnt &
done

Here's why the deadlock happens:

1) The quota rescan task is running.

2) Task A calls btrfs_quota_disable(), locks the qgroup_ioctl_lock
   mutex, and then calls btrfs_qgroup_wait_for_completion(), to wait for
   the quota rescan task to complete.

3) Task B calls btrfs_remove_qgroup() and it blocks when trying to lock
   the qgroup_ioctl_lock mutex, because it's being held by task A. At that
   point task B is holding a transaction handle for the current transaction.

4) The quota rescan task calls btrfs_commit_transaction(). This results
   in it waiting for all other tasks to release their handles on the
   transaction, but task B is blocked on the qgroup_ioctl_lock mutex
   while holding a handle on the transaction, and that mutex is being held
   by task A, which is waiting for the quota rescan task to complete,
   resulting in a deadlock between these 3 tasks.

To resolve this issue, the thread disabling quota should unlock
qgroup_ioctl_lock before waiting rescan completion. Move
btrfs_qgroup_wait_for_completion() after unlock of qgroup_ioctl_lock.

Fixes: e804861bd4e6 ("btrfs: fix deadlock between quota disable and qgroup rescan worker")
CC: stable@vger.kernel.org # 5.4+
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com>
Signed-off-by: Sidong Yang <realwakka@gmail.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-03-02 16:53:04 +01:00
..
2021-11-10 21:16:56 +00:00
2021-12-16 09:10:13 -08:00
2021-08-19 09:02:55 +09:00
2021-11-09 10:02:51 -08:00
2021-10-25 19:11:50 -07:00
2021-04-12 15:04:29 +02:00
2021-11-13 15:32:30 -08:00
2021-11-01 10:25:27 -07:00
2021-12-02 12:41:10 +01:00
2021-08-26 22:28:02 +02:00
2021-11-03 09:23:25 -07:00
2021-12-07 15:47:09 +00:00
2021-11-27 10:33:55 -08:00
2021-12-18 17:11:06 -05:00
2021-11-09 10:51:12 -08:00
\n
2021-11-06 16:40:48 -07:00
\n
2021-11-06 16:40:48 -07:00
2021-11-09 12:53:58 +01:00
2021-08-19 09:02:55 +09:00
2021-12-17 16:56:35 +09:00
2021-11-17 09:26:09 +01:00
2021-10-18 14:43:22 -06:00
2021-08-31 11:13:35 -07:00
2021-09-09 13:25:49 -07:00
2021-11-17 10:36:35 -05:00
2021-10-19 05:49:54 -06:00
2021-08-31 11:06:32 -07:00
2021-10-19 14:11:39 -04:00
2021-11-01 09:06:53 -07:00
2021-12-09 10:49:56 -08:00
2021-08-10 17:57:22 +02:00