Skip to contentSkip to conversation

btrfs: scrub: fix scrub_lock

PATCH Superseded Anand Jain 2 participants 6 messages Nov 29, 2018 – Nov 30, 2018 Btrfs file system

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Anand Jain

author
#0 lore raw

v2 cover btrfs: scrub: fix scrub_lock

Idea was to fix the circular locking dependency warning as in patch 2/3, and in the process also fixes the other identified cleanups patches 1/3,3/3 and they aren't dependent on 2ttch /3.

Anand Jain (3):
btrfs: scrub: maintain the unlock order in scrub thread
btrfs: scrub: fix circular locking dependency warning
btrfs: add lockdep check for scrub_lock in scrub_workers_get

 fs/btrfs/scrub.c   | 22 +++++++++++-----------
 3 files changed, 13 insertions(+), 13 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH v2 1/3 btrfs: scrub: maintain the unlock order in scrub thread

The device_list_mutex and scrub_lock creates a nested locks in btrfs_scrub_dev().

During lock the order is device_list_mutex and then scrub_lock, and during unlock, the order is device_list_mutex and then scrub_lock. Fix this to the lock order of scrub_lock and then device_list_mutex.

---
v1->v2: change the order of lock acquire first scrub_lock and then
device_list_mutex, which matches with the order of unlock. The extra line which are now in the scrub_lock are ok to be under the scrub_lock.

 fs/btrfs/scrub.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
fs/btrfs/scrub.c +7 -6
index 902819d3cf41..a9d6fc3b01d4 100644
@@ -3813,28 +3813,29 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
 		return -EINVAL;
 	}
 
-
+	mutex_lock(&fs_info->scrub_lock);
 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
 	dev = btrfs_find_device(fs_info, devid, NULL, NULL);
 	if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
 		     !is_dev_replace)) {
 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
+		mutex_unlock(&fs_info->scrub_lock);
 		return -ENODEV;
 	}
 
 	if (!is_dev_replace && !readonly &&
 	    !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
+		mutex_unlock(&fs_info->scrub_lock);
 		btrfs_err_in_rcu(fs_info, "scrub: device %s is not writable",
 				rcu_str_deref(dev->name));
 		return -EROFS;
 	}
 
-	mutex_lock(&fs_info->scrub_lock);
 	if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
 	    test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) {
-		mutex_unlock(&fs_info->scrub_lock);
 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
+		mutex_unlock(&fs_info->scrub_lock);
 		return -EIO;
 	}
 
@@ -3843,23 +3844,23 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
 	    (!is_dev_replace &&
 	     btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
 		btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
-		mutex_unlock(&fs_info->scrub_lock);
 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
+		mutex_unlock(&fs_info->scrub_lock);
 		return -EINPROGRESS;
 	}
 	btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
 
 	ret = scrub_workers_get(fs_info, is_dev_replace);
 	if (ret) {
-		mutex_unlock(&fs_info->scrub_lock);
 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
+		mutex_unlock(&fs_info->scrub_lock);
 		return ret;
 	}
 
 	sctx = scrub_setup_ctx(dev, is_dev_replace);
 	if (IS_ERR(sctx)) {
-		mutex_unlock(&fs_info->scrub_lock);
 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
+		mutex_unlock(&fs_info->scrub_lock);
 		scrub_workers_put(fs_info);
 		return PTR_ERR(sctx);
 	}
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [PATCH v2 1/3] btrfs: scrub: maintain the unlock order in scrub thread

Quoted 8 lines · Anand Jain

The device_list_mutex and scrub_lock creates a nested locks in btrfs_scrub_dev().

During lock the order is device_list_mutex and then scrub_lock, and during unlock, the order is device_list_mutex and then scrub_lock. Fix this to the lock order of scrub_lock and then device_list_mutex.

Quoted 6 lines

---
v1->v2: change the order of lock acquire first scrub_lock and then
device_list_mutex, which matches with the order of unlock. The extra line which are now in the scrub_lock are ok to be under the scrub_lock.

I don't get it.
What problem does this patch fixes?
Doesn't seem any functional fix to me, nor performance gain (by the contrary, the scrub_lock is now held for a longer time than needed), nor makes anything more readable or "beautiful".

Quoted 69 lines
 fs/btrfs/scrub.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
fs/btrfs/scrub.c +7 -7
index 902819d3cf41..a9d6fc3b01d4 100644
@@ -3813,28 +3813,29 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
                return -EINVAL;
        }
 
-
+       mutex_lock(&fs_info->scrub_lock);
        mutex_lock(&fs_info->fs_devices->device_list_mutex);
        dev = btrfs_find_device(fs_info, devid, NULL, NULL);
        if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
                     !is_dev_replace)) {
                mutex_unlock(&fs_info->fs_devices->device_list_mutex);
+               mutex_unlock(&fs_info->scrub_lock);
                return -ENODEV;
        }
 
        if (!is_dev_replace && !readonly &&
            !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
                mutex_unlock(&fs_info->fs_devices->device_list_mutex);
+               mutex_unlock(&fs_info->scrub_lock);
                btrfs_err_in_rcu(fs_info, "scrub: device %s is not writable",
                                rcu_str_deref(dev->name));
                return -EROFS;
        }
 
-       mutex_lock(&fs_info->scrub_lock);
        if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
            test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) {
-               mutex_unlock(&fs_info->scrub_lock);
                mutex_unlock(&fs_info->fs_devices->device_list_mutex);
+               mutex_unlock(&fs_info->scrub_lock);
                return -EIO;
        }
 
@@ -3843,23 +3844,23 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
            (!is_dev_replace &&
             btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
                btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
-               mutex_unlock(&fs_info->scrub_lock);
                mutex_unlock(&fs_info->fs_devices->device_list_mutex);
+               mutex_unlock(&fs_info->scrub_lock);
                return -EINPROGRESS;
        }
        btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
 
        ret = scrub_workers_get(fs_info, is_dev_replace);
        if (ret) {
-               mutex_unlock(&fs_info->scrub_lock);
                mutex_unlock(&fs_info->fs_devices->device_list_mutex);
+               mutex_unlock(&fs_info->scrub_lock);
                return ret;
        }
 
        sctx = scrub_setup_ctx(dev, is_dev_replace);
        if (IS_ERR(sctx)) {
-               mutex_unlock(&fs_info->scrub_lock);
                mutex_unlock(&fs_info->fs_devices->device_list_mutex);
+               mutex_unlock(&fs_info->scrub_lock);
                scrub_workers_put(fs_info);
                return PTR_ERR(sctx);
        }
--

1.8.3.1

Signature · 3 lines
Filipe David Manana,

“Whether you think you can, or you think you can't — you're right.”
author
#3 lore raw

Re: [PATCH v2 1/3] btrfs: scrub: maintain the unlock order in scrub thread

Quoted 16 lines · Filipe Manana
Quoted 8 lines · Anand Jain

The device_list_mutex and scrub_lock creates a nested locks in btrfs_scrub_dev().

During lock the order is device_list_mutex and then scrub_lock, and during unlock, the order is device_list_mutex and then scrub_lock. Fix this to the lock order of scrub_lock and then device_list_mutex.

Quoted 6 lines

---
v1->v2: change the order of lock acquire first scrub_lock and then
device_list_mutex, which matches with the order of unlock. The extra line which are now in the scrub_lock are ok to be under the scrub_lock.

Quoted 5 lines

I don't get it.
What problem does this patch fixes?
Doesn't seem any functional fix to me, nor performance gain (by the contrary, the scrub_lock is now held for a longer time than needed), nor makes anything more readable or "beautiful".

btrfs_scrub_dev() isn't following the lock and unlock FILO order. Such as lock-a lock-b .. unlock-b unlock-a. So this patch is trying to fix it.

This patch fixes the order but I think you mean to say as __scrub_blocked_if_needed() calls unlock scrub_lock. oops my bad this patch is wrong.

Scrub concurrency needs overhaul including the dependency on the user land btrfs-progs, which I was trying to avoid. but looks like its better to fix that as well. As of now I am NACK this patch.

Thanks, Anand

Quoted 72 lines
Quoted 69 lines
  fs/btrfs/scrub.c | 13 +++++++------
  1 file changed, 7 insertions(+), 6 deletions(-)
fs/btrfs/scrub.c +7 -7
index 902819d3cf41..a9d6fc3b01d4 100644
@@ -3813,28 +3813,29 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
                 return -EINVAL;
         }
 
-
+       mutex_lock(&fs_info->scrub_lock);
         mutex_lock(&fs_info->fs_devices->device_list_mutex);
         dev = btrfs_find_device(fs_info, devid, NULL, NULL);
         if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
                      !is_dev_replace)) {
                 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
+               mutex_unlock(&fs_info->scrub_lock);
                 return -ENODEV;
         }
 
         if (!is_dev_replace && !readonly &&
             !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
                 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
+               mutex_unlock(&fs_info->scrub_lock);
                 btrfs_err_in_rcu(fs_info, "scrub: device %s is not writable",
                                 rcu_str_deref(dev->name));
                 return -EROFS;
         }
 
-       mutex_lock(&fs_info->scrub_lock);
         if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
             test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) {
-               mutex_unlock(&fs_info->scrub_lock);
                 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
+               mutex_unlock(&fs_info->scrub_lock);
                 return -EIO;
         }
 
@@ -3843,23 +3844,23 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
             (!is_dev_replace &&
              btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
                 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
-               mutex_unlock(&fs_info->scrub_lock);
                 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
+               mutex_unlock(&fs_info->scrub_lock);
                 return -EINPROGRESS;
         }
         btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
 
         ret = scrub_workers_get(fs_info, is_dev_replace);
         if (ret) {
-               mutex_unlock(&fs_info->scrub_lock);
                 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
+               mutex_unlock(&fs_info->scrub_lock);
                 return ret;
         }
 
         sctx = scrub_setup_ctx(dev, is_dev_replace);
         if (IS_ERR(sctx)) {
-               mutex_unlock(&fs_info->scrub_lock);
                 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
+               mutex_unlock(&fs_info->scrub_lock);
                 scrub_workers_put(fs_info);
                 return PTR_ERR(sctx);
         }
--

1.8.3.1

author
#4 lore raw

PATCH v2 2/3 btrfs: scrub: fix circular locking dependency warning

In reply to Anand Jain

Circular locking dependency check reports warning[1], that's because the btrfs_scrub_dev() calls the stack #0 below with, the fs_info::scrub_lock held. The test case leading to this warning..

  mkfs.btrfs -fq /dev/sdb && mount /dev/sdb /btrfs
  btrfs scrub start -B /btrfs

In fact we have fs_info::scrub_workers_refcnt to tack if the init and destroy of the scrub workers are needed. So once we have incremented and decremented the fs_info::scrub_workers_refcnt value in the thread, its ok to drop the scrub_lock, and then actually do the btrfs_destroy_workqueue() part. So this patch drops the scrub_lock before calling btrfs_destroy_workqueue().

[1]

[   76.146826] ======================================================
[   76.147086] WARNING: possible circular locking dependency detected
[   76.147316] 4.20.0-rc3+ #41 Not tainted
[   76.147489] ------------------------------------------------------
[   76.147722] btrfs/4065 is trying to acquire lock:
[   76.147984] 0000000038593bc0 ((wq_completion)"%s-%s""btrfs",
name){+.+.}, at: flush_workqueue+0x70/0x4d0
[   76.148337]
but task is already holding lock:
[   76.148594] 0000000062392ab7 (&fs_info->scrub_lock){+.+.}, at:
btrfs_scrub_dev+0x316/0x5d0 [btrfs]
[   76.148909]

which lock already depends on the new lock.

[   76.149191]
the existing dependency chain (in reverse order) is:
[   76.149446]
-> #3 (&fs_info->scrub_lock){+.+.}:
[   76.149707]        btrfs_scrub_dev+0x11f/0x5d0 [btrfs]
[   76.149924]        btrfs_ioctl+0x1ac3/0x2d80 [btrfs]
[   76.150216]        do_vfs_ioctl+0xa9/0x6d0
[   76.150468]        ksys_ioctl+0x60/0x90
[   76.150716]        __x64_sys_ioctl+0x16/0x20
[   76.150911]        do_syscall_64+0x50/0x180
[   76.151182]        entry_SYSCALL_64_after_hwframe+0x49/0xbe
[   76.151469]
-> #2 (&fs_devs->device_list_mutex){+.+.}:
[   76.151851]        reada_start_machine_worker+0xca/0x3f0 [btrfs]
[   76.152195]        normal_work_helper+0xf0/0x4c0 [btrfs]
[   76.152489]        process_one_work+0x1f4/0x520
[   76.152751]        worker_thread+0x46/0x3d0
[   76.153715]        kthread+0xf8/0x130
[   76.153912]        ret_from_fork+0x3a/0x50
[   76.154178]
-> #1 ((work_completion)(&work->normal_work)){+.+.}:
[   76.154575]        worker_thread+0x46/0x3d0
[   76.154828]        kthread+0xf8/0x130
[   76.155108]        ret_from_fork+0x3a/0x50
[   76.155357]
-> #0 ((wq_completion)"%s-%s""btrfs", name){+.+.}:
[   76.155751]        flush_workqueue+0x9a/0x4d0
[   76.155911]        drain_workqueue+0xca/0x1a0
[   76.156182]        destroy_workqueue+0x17/0x230
[   76.156455]        btrfs_destroy_workqueue+0x5d/0x1c0 [btrfs]
[   76.156756]        scrub_workers_put+0x2e/0x60 [btrfs]
[   76.156931]        btrfs_scrub_dev+0x329/0x5d0 [btrfs]
[   76.157219]        btrfs_ioctl+0x1ac3/0x2d80 [btrfs]
[   76.157491]        do_vfs_ioctl+0xa9/0x6d0
[   76.157742]        ksys_ioctl+0x60/0x90
[   76.157910]        __x64_sys_ioctl+0x16/0x20
[   76.158177]        do_syscall_64+0x50/0x180
[   76.158429]        entry_SYSCALL_64_after_hwframe+0x49/0xbe
[   76.158716]

other info that might help us debug this:

[   76.158908] Chain exists of:
  (wq_completion)"%s-%s""btrfs", name --> &fs_devs->device_list_mutex

--> &fs_info->scrub_lock

[   76.159629]  Possible unsafe locking scenario:
[   76.160607]        CPU0                    CPU1
[   76.160934]        ----                    ----
[   76.161210]   lock(&fs_info->scrub_lock);
[   76.161458]
lock(&fs_devs->device_list_mutex);
[   76.161805]
lock(&fs_info->scrub_lock);
[   76.161909]   lock((wq_completion)"%s-%s""btrfs", name);
[   76.162201]
 *** DEADLOCK ***
[   76.162627] 2 locks held by btrfs/4065:
[   76.162897]  #0: 00000000bef2775b (sb_writers#12){.+.+}, at:
mnt_want_write_file+0x24/0x50
[   76.163335]  #1: 0000000062392ab7 (&fs_info->scrub_lock){+.+.}, at:
btrfs_scrub_dev+0x316/0x5d0 [btrfs]
[   76.163796]
stack backtrace:
[   76.163911] CPU: 1 PID: 4065 Comm: btrfs Not tainted 4.20.0-rc3+ #41
[   76.164228] Hardware name: innotek GmbH VirtualBox/VirtualBox, BIOS
VirtualBox 12/01/2006
[   76.164646] Call Trace:
[   76.164872]  dump_stack+0x5e/0x8b
[   76.165128]  print_circular_bug.isra.37+0x1f1/0x1fe
[   76.165398]  __lock_acquire+0x14aa/0x1620
[   76.165652]  lock_acquire+0xb0/0x190
[   76.165910]  ? flush_workqueue+0x70/0x4d0
[   76.166175]  flush_workqueue+0x9a/0x4d0
[   76.166420]  ? flush_workqueue+0x70/0x4d0
[   76.166671]  ? drain_workqueue+0x52/0x1a0
[   76.166911]  drain_workqueue+0xca/0x1a0
[   76.167167]  destroy_workqueue+0x17/0x230
[   76.167428]  btrfs_destroy_workqueue+0x5d/0x1c0 [btrfs]
[   76.167720]  scrub_workers_put+0x2e/0x60 [btrfs]
[   76.168233]  btrfs_scrub_dev+0x329/0x5d0 [btrfs]
[   76.168504]  ? __sb_start_write+0x121/0x1b0
[   76.168759]  ? mnt_want_write_file+0x24/0x50
[   76.169654]  btrfs_ioctl+0x1ac3/0x2d80 [btrfs]
[   76.169934]  ? find_held_lock+0x2d/0x90
[   76.170204]  ? find_held_lock+0x2d/0x90
[   76.170450]  do_vfs_ioctl+0xa9/0x6d0
[   76.170690]  ? __fget+0x101/0x1f0
[   76.170910]  ? __fget+0x5/0x1f0
[   76.171157]  ksys_ioctl+0x60/0x90
[   76.171391]  __x64_sys_ioctl+0x16/0x20
[   76.171634]  do_syscall_64+0x50/0x180
[   76.171892]  entry_SYSCALL_64_after_hwframe+0x49/0xbe
[   76.172186] RIP: 0033:0x7f61d422e567
[   76.172425] Code: 44 00 00 48 8b 05 29 09 2d 00 64 c7 00 26 00 00 00
48 c7 c0 ff ff ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 b8 10 00 00 00 0f
05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d f9 08 2d 00 f7 d8 64 89 01 48
[   76.172911] RSP: 002b:00007f61d3936d68 EFLAGS: 00000246 ORIG_RAX:
0000000000000010
[   76.173328] RAX: ffffffffffffffda RBX: 00000000019026b0 RCX:
00007f61d422e567
[   76.173649] RDX: 00000000019026b0 RSI: 00000000c400941b RDI:
0000000000000003
[   76.173909] RBP: 0000000000000000 R08: 00007f61d3937700 R09:
0000000000000000
[   76.174244] R10: 00007f61d3937700 R11: 0000000000000246 R12:
0000000000000000
[   76.174566] R13: 0000000000801000 R14: 0000000000000000 R15:
00007f61d3937700
[   76.175217] btrfs (4065) used greatest stack depth: 11424 bytes left

---
v1->v2: none

 fs/btrfs/scrub.c | 3 +++
 1 file changed, 3 insertions(+)
fs/btrfs/scrub.c +3 -0
index b5a19ba38ab7..9ade0659f017 100644
@@ -3757,10 +3757,13 @@ static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
 
 static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
 {
+	lockdep_assert_held(&fs_info->scrub_lock);
 	if (--fs_info->scrub_workers_refcnt == 0) {
+		mutex_unlock(&fs_info->scrub_lock);
 		btrfs_destroy_workqueue(fs_info->scrub_workers);
 		btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
 		btrfs_destroy_workqueue(fs_info->scrub_parity_workers);
+		mutex_lock(&fs_info->scrub_lock);
 	}
 	WARN_ON(fs_info->scrub_workers_refcnt < 0);
 }
Signature · 1 line
1.8.3.1
author
#5 lore raw

PATCH v2 3/3 btrfs: scrub: add scrub_lock lockdep check in scrub_workers_get

In reply to Anand Jain

scrub_workers_refcnt is protected by scrub_lock, add lockdep_assert_held() function in scrub_workers_get().

---
v2: born

 fs/btrfs/scrub.c | 2 ++
 1 file changed, 2 insertions(+)
fs/btrfs/scrub.c +2 -0
index 9ade0659f017..84ef1f0d371e 100644
@@ -3726,6 +3726,8 @@ static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
 	unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
 	int max_active = fs_info->thread_pool_size;
 
+	lockdep_assert_held(&fs_info->scrub_lock);
+
 	if (fs_info->scrub_workers_refcnt == 0) {
 		fs_info->scrub_workers = btrfs_alloc_workqueue(fs_info, "scrub",
 				flags, is_dev_replace ? 1 : max_active, 4);
Signature · 1 line
1.8.3.1