Convert to an rwsem for our tree locking
cover Convert to an rwsem for our tree locking
tl;dr:
- Cleaner code.
- dbench 200 3x as fast.
- fio with < nr_cpus is 10% slower.
- fio with 2 * nr_cpus is 10% faster.
- fs_mark is the same ish (results have a 10% variance between runs).
When Chris originally wrote the locking code for Btrfs the performance of rwsem was atroctios compared to his locking scheme. In btrfs we can usually spin on most accesses, because generally we won't need to block. If we do need to block we can simply flip over to a blocking lock. This flexibility has meant that our locking scheme generally outperforms any of the standard locking. We also have a unique usecase where in a specific case (the space cache) we need to be able to recurse a tree lock. It is safe to do this because we're just reading, and we would currently be holding a write lock, but this is not something that the generic locking infrastructure provides.
There are a few downsides
1) The code is complicated. This generally isn't an issue because we don't really have to touch the code that often.
2) We lose lockdep help. As you can see the first 4 patches in this series are fixing deadlocks that exist that were caught by having lockdep properly working with our tree locking.
3) We actually perform really poorly in the highly contended case. Because we don't really do anything proper locks do like dealing with writer/read starvation, we have a series of waitqueues that mass get woken up when we unlock blocking. This means our context switch counts can be stupid high, as we'll wake everybody up, and whoever wins the race gets the lock. Implementing proper exclusive waitqueues is a possiblity, but would further complicate the code, and likely introduce problems we'd spend the next 6 months figuring out.
It's #3 that actually brought me to this patch series, as changing to an rwsem significantly improved a usecase I was investigating. However the rwsem isn't without drawbacks. Namely that in less highly contended cases we can be slower than baseline, around 9-10% on my test box. However in the very highly contended cases (> nr_cpus) we are the same, if not better.
Filipe and I have discussed this a bit over the last few days, we're both on the fence about it currently. On one hand we could likely make up the lost performance in other areas, and there's certainly things that could be investigated to see why exactly we're slower with the rwsem and maybe figure out a way to fix those. We also gain a lot with this new code. But 9-10% isn't nothing, so it needs to be taken into consideration.
The following is the set of benchmarks that I've run on my 80 CPU, 256 GIB of ram, 2 TIB NVME drive machine. If there's anything else people would like to see I can easily do A/B testing to see.
fio was N threads with 1gib files, 64kib blocksize with a fsync after every write. The fs_mark was just create empty files with 16 threads.
PATCHED UNPATCHED % DIFF
dbench 200 699.011 Mb/s 213.568 Mb/s +227%fs_mark 223 seconds 197 seconds -11.65%
fio 64 threads 562 Mb/s 624 Mb/s -9.9%
fio 100 threads 566 Mb/s 566 Mb/s 0.0%
fio 160 threads 593 Mb/s 576 Mb/s +2.95%
Another thing not shown by the raw numbers is the number of context switches. Because of the nature of our locking we wake up everything constantly, so our context switch counts for the fio jobs are consistently 2-3x with the old scheme vs the rwsem. This is why my particular workload performed so poorly, the context switching was actually quite painful for that workload.
The diffstat is as follows
fs/btrfs/backref.c | 13 +- fs/btrfs/ctree.c | 168 ++++++--------- fs/btrfs/ctree.h | 10 +- fs/btrfs/delayed-inode.c | 11 - fs/btrfs/dir-item.c | 1 - fs/btrfs/disk-io.c | 13 +- fs/btrfs/export.c | 1 - fs/btrfs/extent-tree.c | 41 ++-- fs/btrfs/extent_io.c | 24 +-- fs/btrfs/extent_io.h | 27 +-- fs/btrfs/file-item.c | 4 - fs/btrfs/file.c | 3 +- fs/btrfs/free-space-tree.c | 2 - fs/btrfs/inode-item.c | 6 - fs/btrfs/inode.c | 13 +- fs/btrfs/ioctl.c | 31 ++- fs/btrfs/locking.c | 469 +++++++----------------------------------- fs/btrfs/locking.h | 80 ++++++- fs/btrfs/print-tree.c | 11 +- fs/btrfs/qgroup.c | 11 +- fs/btrfs/ref-verify.c | 6 +- fs/btrfs/reflink.c | 3 - fs/btrfs/relocation.c | 15 +- fs/btrfs/scrub.c | 122 ++++++----- fs/btrfs/super.c | 2 - fs/btrfs/tests/qgroup-tests.c | 4 - fs/btrfs/transaction.c | 7 +- fs/btrfs/tree-defrag.c | 1 - fs/btrfs/tree-log.c | 3 - fs/btrfs/volumes.c | 8 +- 30 files changed, 357 insertions(+), 753 deletions(-)
Thanks,
Josef
PATCH 1/17 btrfs: drop path before adding new uuid tree entry
With the conversion to the rwsemaphore I got the following lockdep splat
======================================================
WARNING: possible circular locking dependency detected 5.8.0-rc7-00167-g0d7ba0c5b375-dirty #925 Not tainted
------------------------------------------------------
btrfs-uuid/7955 is trying to acquire lock:ffff88bfbafec0f8 (btrfs-root-00){++++}-{3:3}, at: __btrfs_tree_read_lock+0x39/0x180
but task is already holding lock:ffff88bfbafef2a8 (btrfs-uuid-00){++++}-{3:3}, at: __btrfs_tree_read_lock+0x39/0x180
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (btrfs-uuid-00){++++}-{3:3}:
down_read_nested+0x3e/0x140
__btrfs_tree_read_lock+0x39/0x180
__btrfs_read_lock_root_node+0x3a/0x50
btrfs_search_slot+0x4bd/0x990
btrfs_uuid_tree_add+0x89/0x2d0
btrfs_uuid_scan_kthread+0x330/0x390
kthread+0x133/0x150
ret_from_fork+0x1f/0x30-> #0 (btrfs-root-00){++++}-{3:3}:
__lock_acquire+0x1272/0x2310
lock_acquire+0x9e/0x360
down_read_nested+0x3e/0x140
__btrfs_tree_read_lock+0x39/0x180
__btrfs_read_lock_root_node+0x3a/0x50
btrfs_search_slot+0x4bd/0x990
btrfs_find_root+0x45/0x1b0
btrfs_read_tree_root+0x61/0x100
btrfs_get_root_ref.part.50+0x143/0x630
btrfs_uuid_tree_iterate+0x207/0x314
btrfs_uuid_rescan_kthread+0x12/0x50
kthread+0x133/0x150
ret_from_fork+0x1f/0x30other info that might help us debug this:
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(btrfs-uuid-00);
lock(btrfs-root-00);
lock(btrfs-uuid-00);lock(btrfs-root-00);
*** DEADLOCK ***
1 lock held by btrfs-uuid/7955:
#0: ffff88bfbafef2a8 (btrfs-uuid-00){++++}-{3:3}, at: __btrfs_tree_read_lock+0x39/0x180
stack backtrace: CPU: 73 PID: 7955 Comm: btrfs-uuid Kdump: loaded Not tainted 5.8.0-rc7-00167-g0d7ba0c5b375-dirty #925 Hardware name: Quanta Tioga Pass Single Side 01-0030993006/Tioga Pass Single Side, BIOS F08_3A18 12/20/2018 Call Trace: dump_stack+0x78/0xa0 check_noncircular+0x165/0x180 __lock_acquire+0x1272/0x2310 lock_acquire+0x9e/0x360 ? __btrfs_tree_read_lock+0x39/0x180 ? btrfs_root_node+0x1c/0x1d0 down_read_nested+0x3e/0x140 ? __btrfs_tree_read_lock+0x39/0x180 __btrfs_tree_read_lock+0x39/0x180 __btrfs_read_lock_root_node+0x3a/0x50 btrfs_search_slot+0x4bd/0x990 btrfs_find_root+0x45/0x1b0 btrfs_read_tree_root+0x61/0x100 btrfs_get_root_ref.part.50+0x143/0x630 btrfs_uuid_tree_iterate+0x207/0x314 ? btree_readpage+0x20/0x20 btrfs_uuid_rescan_kthread+0x12/0x50 kthread+0x133/0x150 ? kthread_create_on_node+0x60/0x60 ret_from_fork+0x1f/0x30
This problem exists because we have two different rescan threads, btrfs_uuid_scan_kthread which creates the uuid tree, and btrfs_uuid_tree_iterate that goes through and updates or deletes any out of date roots. The problem is they both do things in different order. btrfs_uuid_scan_kthread() reads the tree_root, and then inserts entries into the uuid_root. btrfs_uuid_tree_iterate() scans the uuid_root, but then does a btrfs_get_fs_root() which can read from the tree_root.
It's actually easy enough to not be holding the path in btrfs_uuid_scan_kthread() when we add a uuid entry, as we already drop it further down and re-start the search when we loop. So simply move the path release before we add our entry to the uuid tree.
This also fixes a problem where we're holding a path open after we do btrfs_end_transaction(), which has it's own problems.
--- fs/btrfs/volumes.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
fs/btrfs/volumes.c +2 -1
@@ -4462,6 +4462,7 @@ int btrfs_uuid_scan_kthread(void *data) goto skip; } update_tree: +btrfs_release_path (path);if (!btrfs_is_empty_uuid (root_item.uuid)) { ret = btrfs_uuid_tree_add(trans, root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
@@ -4486,6 +4487,7 @@ int btrfs_uuid_scan_kthread(void *data) } skip: +btrfs_release_path (path);if (trans) { ret =btrfs_end_transaction (trans); trans =NULL ;
@@ -4493,7 +4495,6 @@ int btrfs_uuid_scan_kthread(void *data)break ; } -btrfs_release_path (path);if (key.offset < (u64)-1 ) { key.offset++; }else if (key.type <BTRFS_ROOT_ITEM_KEY ) {
Signature · 1 line
2.24.1
Re: [PATCH 01/17] btrfs: drop path before adding new uuid tree entry
Quoted 83 lines · Josef Bacik
With the conversion to the rwsemaphore I got the following lockdep splat
======================================================
WARNING: possible circular locking dependency detected 5.8.0-rc7-00167-g0d7ba0c5b375-dirty #925 Not tainted
------------------------------------------------------
btrfs-uuid/7955 is trying to acquire lock:ffff88bfbafec0f8(btrfs-root-00){++++}-{3:3}, at:__btrfs_tree_read_lock+0x39/0x180but task is already holding lock:
ffff88bfbafef2a8(btrfs-uuid-00){++++}-{3:3}, at:__btrfs_tree_read_lock+0x39/0x180which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (btrfs-uuid-00){++++}-{3:3}:
down_read_nested+0x3e/0x140 __btrfs_tree_read_lock+0x39/0x180 __btrfs_read_lock_root_node+0x3a/0x50 btrfs_search_slot+0x4bd/0x990 btrfs_uuid_tree_add+0x89/0x2d0 btrfs_uuid_scan_kthread+0x330/0x390 kthread+0x133/0x150 ret_from_fork+0x1f/0x30-> #0 (btrfs-root-00){++++}-{3:3}:
__lock_acquire+0x1272/0x2310 lock_acquire+0x9e/0x360 down_read_nested+0x3e/0x140 __btrfs_tree_read_lock+0x39/0x180 __btrfs_read_lock_root_node+0x3a/0x50 btrfs_search_slot+0x4bd/0x990 btrfs_find_root+0x45/0x1b0 btrfs_read_tree_root+0x61/0x100 btrfs_get_root_ref.part.50+0x143/0x630 btrfs_uuid_tree_iterate+0x207/0x314 btrfs_uuid_rescan_kthread+0x12/0x50 kthread+0x133/0x150 ret_from_fork+0x1f/0x30other info that might help us debug this:
Possible unsafe locking scenario:CPU0 CPU1 ---- ---- lock(btrfs-uuid-00); lock(btrfs-root-00); lock(btrfs-uuid-00);lock(btrfs-root-00);
*** DEADLOCK ***
1 lock held by btrfs-uuid/7955:
#0:ffff88bfbafef2a8(btrfs-uuid-00){++++}-{3:3}, at:__btrfs_tree_read_lock+0x39/0x180stack backtrace: CPU: 73 PID: 7955 Comm: btrfs-uuid Kdump: loaded Not tainted 5.8.0-rc7-00167-g0d7ba0c5b375-dirty #925 Hardware name: Quanta Tioga Pass Single Side 01-0030993006/Tioga Pass Single Side, BIOS F08_3A18 12/20/2018 Call Trace: dump_stack+0x78/0xa0 check_noncircular+0x165/0x180 __lock_acquire+0x1272/0x2310 lock_acquire+0x9e/0x360 ? __btrfs_tree_read_lock+0x39/0x180 ? btrfs_root_node+0x1c/0x1d0 down_read_nested+0x3e/0x140 ? __btrfs_tree_read_lock+0x39/0x180 __btrfs_tree_read_lock+0x39/0x180 __btrfs_read_lock_root_node+0x3a/0x50 btrfs_search_slot+0x4bd/0x990 btrfs_find_root+0x45/0x1b0 btrfs_read_tree_root+0x61/0x100 btrfs_get_root_ref.part.50+0x143/0x630 btrfs_uuid_tree_iterate+0x207/0x314 ? btree_readpage+0x20/0x20 btrfs_uuid_rescan_kthread+0x12/0x50 kthread+0x133/0x150 ? kthread_create_on_node+0x60/0x60 ret_from_fork+0x1f/0x30
Quoted 7 lines
This problem exists because we have two different rescan threads,
btrfs_uuid_scan_kthreadwhich creates the uuid tree, andbtrfs_uuid_tree_iteratethat goes through and updates or deletes any out of date roots. The problem is they both do things in different order.btrfs_uuid_scan_kthread()reads thetree_root, and then inserts entries into theuuid_root.btrfs_uuid_tree_iterate()scans theuuid_root, but then does abtrfs_get_fs_root()which can read from thetree_root.
Ok, the get_fs_root() is through btrfs_check_uuid_tree_entry().
Quoted 19 lines
It's actually easy enough to not be holding the path in
btrfs_uuid_scan_kthread()when we add a uuid entry, as we already drop it further down and re-start the search when we loop. So simply move the path release before we add our entry to the uuid tree.This also fixes a problem where we're holding a path open after we do
btrfs_end_transaction(), which has it's own problems.Signed-off-by Josef Bacik <[email protected]>--- fs/btrfs/volumes.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)fs/btrfs/volumes.c +0
-0
fs/btrfs/volumes.c int btrfs_uuid_scan_kthread(void *data) · line 4465
Quoted 4 lines
fs/btrfs/volumes.c +1
-0@@ -4462,6 +4462,7 @@ int btrfs_uuid_scan_kthread(void *data) goto skip; } update_tree: +btrfs_release_path (path);
We can actually do the release right after reading from the eb, so we avoid 3 path releases in different places.
The end result would be:
Either way, it looks good to me.
Thanks.
Quoted 22 lines
fs/btrfs/volumes.c +1
-2if (!btrfs_is_empty_uuid (root_item.uuid)) { ret = btrfs_uuid_tree_add(trans, root_item.uuid, BTRFS_UUID_KEY_SUBVOL,@@ -4486,6 +4487,7 @@ int btrfs_uuid_scan_kthread(void *data) } skip: +btrfs_release_path (path);if (trans) { ret =btrfs_end_transaction (trans); trans =NULL ;@@ -4493,7 +4495,6 @@ int btrfs_uuid_scan_kthread(void *data)break ; } -btrfs_release_path (path);if (key.offset < (u64)-1 ) { key.offset++; }else if (key.type <BTRFS_ROOT_ITEM_KEY ) { --2.24.1
Signature · 3 lines
Filipe David Manana, “Whether you think you can, or you think you can't — you're right.”
Re: [PATCH 01/17] btrfs: drop path before adding new uuid tree entry
Quoted 123 lines · Filipe Manana
Quoted 83 lines · Josef Bacik
With the conversion to the rwsemaphore I got the following lockdep splat
======================================================
WARNING: possible circular locking dependency detected 5.8.0-rc7-00167-g0d7ba0c5b375-dirty #925 Not tainted
------------------------------------------------------
btrfs-uuid/7955 is trying to acquire lock:ffff88bfbafec0f8(btrfs-root-00){++++}-{3:3}, at:__btrfs_tree_read_lock+0x39/0x180but task is already holding lock:
ffff88bfbafef2a8(btrfs-uuid-00){++++}-{3:3}, at:__btrfs_tree_read_lock+0x39/0x180which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (btrfs-uuid-00){++++}-{3:3}:
down_read_nested+0x3e/0x140 __btrfs_tree_read_lock+0x39/0x180 __btrfs_read_lock_root_node+0x3a/0x50 btrfs_search_slot+0x4bd/0x990 btrfs_uuid_tree_add+0x89/0x2d0 btrfs_uuid_scan_kthread+0x330/0x390 kthread+0x133/0x150 ret_from_fork+0x1f/0x30-> #0 (btrfs-root-00){++++}-{3:3}:
__lock_acquire+0x1272/0x2310 lock_acquire+0x9e/0x360 down_read_nested+0x3e/0x140 __btrfs_tree_read_lock+0x39/0x180 __btrfs_read_lock_root_node+0x3a/0x50 btrfs_search_slot+0x4bd/0x990 btrfs_find_root+0x45/0x1b0 btrfs_read_tree_root+0x61/0x100 btrfs_get_root_ref.part.50+0x143/0x630 btrfs_uuid_tree_iterate+0x207/0x314 btrfs_uuid_rescan_kthread+0x12/0x50 kthread+0x133/0x150 ret_from_fork+0x1f/0x30other info that might help us debug this:
Possible unsafe locking scenario:CPU0 CPU1 ---- ---- lock(btrfs-uuid-00); lock(btrfs-root-00); lock(btrfs-uuid-00);lock(btrfs-root-00);
*** DEADLOCK ***
1 lock held by btrfs-uuid/7955:
#0:ffff88bfbafef2a8(btrfs-uuid-00){++++}-{3:3}, at:__btrfs_tree_read_lock+0x39/0x180stack backtrace: CPU: 73 PID: 7955 Comm: btrfs-uuid Kdump: loaded Not tainted 5.8.0-rc7-00167-g0d7ba0c5b375-dirty #925 Hardware name: Quanta Tioga Pass Single Side 01-0030993006/Tioga Pass Single Side, BIOS F08_3A18 12/20/2018 Call Trace: dump_stack+0x78/0xa0 check_noncircular+0x165/0x180 __lock_acquire+0x1272/0x2310 lock_acquire+0x9e/0x360 ? __btrfs_tree_read_lock+0x39/0x180 ? btrfs_root_node+0x1c/0x1d0 down_read_nested+0x3e/0x140 ? __btrfs_tree_read_lock+0x39/0x180 __btrfs_tree_read_lock+0x39/0x180 __btrfs_read_lock_root_node+0x3a/0x50 btrfs_search_slot+0x4bd/0x990 btrfs_find_root+0x45/0x1b0 btrfs_read_tree_root+0x61/0x100 btrfs_get_root_ref.part.50+0x143/0x630 btrfs_uuid_tree_iterate+0x207/0x314 ? btree_readpage+0x20/0x20 btrfs_uuid_rescan_kthread+0x12/0x50 kthread+0x133/0x150 ? kthread_create_on_node+0x60/0x60 ret_from_fork+0x1f/0x30Quoted 7 lines
This problem exists because we have two different rescan threads,
btrfs_uuid_scan_kthreadwhich creates the uuid tree, andbtrfs_uuid_tree_iteratethat goes through and updates or deletes any out of date roots. The problem is they both do things in different order.btrfs_uuid_scan_kthread()reads thetree_root, and then inserts entries into theuuid_root.btrfs_uuid_tree_iterate()scans theuuid_root, but then does abtrfs_get_fs_root()which can read from thetree_root.Ok, the
get_fs_root()is throughbtrfs_check_uuid_tree_entry().Quoted 19 lines
It's actually easy enough to not be holding the path in
btrfs_uuid_scan_kthread()when we add a uuid entry, as we already drop it further down and re-start the search when we loop. So simply move the path release before we add our entry to the uuid tree.This also fixes a problem where we're holding a path open after we do
btrfs_end_transaction(), which has it's own problems.Signed-off-by Josef Bacik <[email protected]>--- fs/btrfs/volumes.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)fs/btrfs/volumes.c +0
-0fs/btrfs/volumes.c int btrfs_uuid_scan_kthread(void *data) · line 4465
Quoted 4 lines
fs/btrfs/volumes.c +1
-0@@ -4462,6 +4462,7 @@ int btrfs_uuid_scan_kthread(void *data) goto skip; } update_tree: +btrfs_release_path (path);We can actually do the release right after reading from the eb, so we avoid 3 path releases in different places.
The end result would be:
Quoted 1 line
Well, it should be https://pastebin.com/t5nDBhVR
Quoted 35 lines
Either way, it looks good to me.
Reviewed-by Filipe Manana <[email protected]>Thanks.
Quoted 20 lines
fs/btrfs/volumes.c +1
-2if (!btrfs_is_empty_uuid (root_item.uuid)) { ret = btrfs_uuid_tree_add(trans, root_item.uuid, BTRFS_UUID_KEY_SUBVOL,@@ -4486,6 +4487,7 @@ int btrfs_uuid_scan_kthread(void *data) } skip: +btrfs_release_path (path);if (trans) { ret =btrfs_end_transaction (trans); trans =NULL ;@@ -4493,7 +4495,6 @@ int btrfs_uuid_scan_kthread(void *data)break ; } -btrfs_release_path (path);if (key.offset < (u64)-1 ) { key.offset++; }else if (key.type <BTRFS_ROOT_ITEM_KEY ) { --Quoted 2 lines
2.24.1
--
Filipe David Manana,“Whether you think you can, or you think you can't — you're right.”
Signature · 3 lines
Filipe David Manana, “Whether you think you can, or you think you can't — you're right.”
Re: [PATCH 01/17] btrfs: drop path before adding new uuid tree entry
In reply to Filipe MananaQuoted 122 lines · Filipe Manana
Quoted 83 lines · Josef Bacik
With the conversion to the rwsemaphore I got the following lockdep splat
======================================================
WARNING: possible circular locking dependency detected 5.8.0-rc7-00167-g0d7ba0c5b375-dirty #925 Not tainted
------------------------------------------------------
btrfs-uuid/7955 is trying to acquire lock:ffff88bfbafec0f8(btrfs-root-00){++++}-{3:3}, at:__btrfs_tree_read_lock+0x39/0x180but task is already holding lock:
ffff88bfbafef2a8(btrfs-uuid-00){++++}-{3:3}, at:__btrfs_tree_read_lock+0x39/0x180which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (btrfs-uuid-00){++++}-{3:3}:
down_read_nested+0x3e/0x140 __btrfs_tree_read_lock+0x39/0x180 __btrfs_read_lock_root_node+0x3a/0x50 btrfs_search_slot+0x4bd/0x990 btrfs_uuid_tree_add+0x89/0x2d0 btrfs_uuid_scan_kthread+0x330/0x390 kthread+0x133/0x150 ret_from_fork+0x1f/0x30-> #0 (btrfs-root-00){++++}-{3:3}:
__lock_acquire+0x1272/0x2310 lock_acquire+0x9e/0x360 down_read_nested+0x3e/0x140 __btrfs_tree_read_lock+0x39/0x180 __btrfs_read_lock_root_node+0x3a/0x50 btrfs_search_slot+0x4bd/0x990 btrfs_find_root+0x45/0x1b0 btrfs_read_tree_root+0x61/0x100 btrfs_get_root_ref.part.50+0x143/0x630 btrfs_uuid_tree_iterate+0x207/0x314 btrfs_uuid_rescan_kthread+0x12/0x50 kthread+0x133/0x150 ret_from_fork+0x1f/0x30other info that might help us debug this:
Possible unsafe locking scenario:CPU0 CPU1 ---- ---- lock(btrfs-uuid-00); lock(btrfs-root-00); lock(btrfs-uuid-00);lock(btrfs-root-00);
*** DEADLOCK ***
1 lock held by btrfs-uuid/7955:
#0:ffff88bfbafef2a8(btrfs-uuid-00){++++}-{3:3}, at:__btrfs_tree_read_lock+0x39/0x180stack backtrace: CPU: 73 PID: 7955 Comm: btrfs-uuid Kdump: loaded Not tainted 5.8.0-rc7-00167-g0d7ba0c5b375-dirty #925 Hardware name: Quanta Tioga Pass Single Side 01-0030993006/Tioga Pass Single Side, BIOS F08_3A18 12/20/2018 Call Trace: dump_stack+0x78/0xa0 check_noncircular+0x165/0x180 __lock_acquire+0x1272/0x2310 lock_acquire+0x9e/0x360 ? __btrfs_tree_read_lock+0x39/0x180 ? btrfs_root_node+0x1c/0x1d0 down_read_nested+0x3e/0x140 ? __btrfs_tree_read_lock+0x39/0x180 __btrfs_tree_read_lock+0x39/0x180 __btrfs_read_lock_root_node+0x3a/0x50 btrfs_search_slot+0x4bd/0x990 btrfs_find_root+0x45/0x1b0 btrfs_read_tree_root+0x61/0x100 btrfs_get_root_ref.part.50+0x143/0x630 btrfs_uuid_tree_iterate+0x207/0x314 ? btree_readpage+0x20/0x20 btrfs_uuid_rescan_kthread+0x12/0x50 kthread+0x133/0x150 ? kthread_create_on_node+0x60/0x60 ret_from_fork+0x1f/0x30Quoted 7 lines
This problem exists because we have two different rescan threads,
btrfs_uuid_scan_kthreadwhich creates the uuid tree, andbtrfs_uuid_tree_iteratethat goes through and updates or deletes any out of date roots. The problem is they both do things in different order.btrfs_uuid_scan_kthread()reads thetree_root, and then inserts entries into theuuid_root.btrfs_uuid_tree_iterate()scans theuuid_root, but then does abtrfs_get_fs_root()which can read from thetree_root.Ok, the
get_fs_root()is throughbtrfs_check_uuid_tree_entry().Quoted 19 lines
It's actually easy enough to not be holding the path in
btrfs_uuid_scan_kthread()when we add a uuid entry, as we already drop it further down and re-start the search when we loop. So simply move the path release before we add our entry to the uuid tree.This also fixes a problem where we're holding a path open after we do
btrfs_end_transaction(), which has it's own problems.Signed-off-by Josef Bacik <[email protected]>--- fs/btrfs/volumes.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)fs/btrfs/volumes.c +0
-0fs/btrfs/volumes.c int btrfs_uuid_scan_kthread(void *data) · line 4465
Quoted 4 lines
fs/btrfs/volumes.c +1
-0@@ -4462,6 +4462,7 @@ int btrfs_uuid_scan_kthread(void *data) goto skip; } update_tree: +btrfs_release_path (path);We can actually do the release right after reading from the eb, so we avoid 3 path releases in different places.
The end result would be:
Quoted 3 lines
Either way, it looks good to me.
We need it at the skip: spot because we'll go straight to skip in some cases and not release the path, and then deadlock. Fun things I discovered in testing ;).
Josef
PATCH 2/17 btrfs: fix potential deadlock in the search ioctl
In reply to Josef BacikWhen I converted the locking over to using a rwsem I got the following lockdep splat
======================================================
WARNING: possible circular locking dependency detected 5.8.0-rc7-00165-g04ec4da5f45f-dirty #922 Not tainted
------------------------------------------------------
compsize/11122 is trying to acquire lock:ffff889fabca8768 (&mm->mmap_lock#2){++++}-{3:3}, at: __might_fault+0x3e/0x90
but task is already holding lock:ffff889fe720fe40 (btrfs-fs-00){++++}-{3:3}, at: __btrfs_tree_read_lock+0x39/0x180
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #2 (btrfs-fs-00){++++}-{3:3}:
down_write_nested+0x3b/0x70
__btrfs_tree_lock+0x24/0x120
btrfs_search_slot+0x756/0x990
btrfs_lookup_inode+0x3a/0xb4
__btrfs_update_delayed_inode+0x93/0x270
btrfs_async_run_delayed_root+0x168/0x230
btrfs_work_helper+0xd4/0x570
process_one_work+0x2ad/0x5f0
worker_thread+0x3a/0x3d0
kthread+0x133/0x150
ret_from_fork+0x1f/0x30-> #1 (&delayed_node->mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930
btrfs_delayed_update_inode+0x50/0x440
btrfs_update_inode+0x8a/0xf0
btrfs_dirty_inode+0x5b/0xd0
touch_atime+0xa1/0xd0
btrfs_file_mmap+0x3f/0x60
mmap_region+0x3a4/0x640
do_mmap+0x376/0x580
vm_mmap_pgoff+0xd5/0x120
ksys_mmap_pgoff+0x193/0x230
do_syscall_64+0x50/0x90
entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #0 (&mm->mmap_lock#2){++++}-{3:3}:
__lock_acquire+0x1272/0x2310
lock_acquire+0x9e/0x360
__might_fault+0x68/0x90
_copy_to_user+0x1e/0x80
copy_to_sk.isra.32+0x121/0x300
search_ioctl+0x106/0x200
btrfs_ioctl_tree_search_v2+0x7b/0xf0
btrfs_ioctl+0x106f/0x30a0
ksys_ioctl+0x83/0xc0
__x64_sys_ioctl+0x16/0x20
do_syscall_64+0x50/0x90
entry_SYSCALL_64_after_hwframe+0x44/0xa9other info that might help us debug this:
Chain exists of: &mm->mmap_lock#2 --> &delayed_node->mutex --> btrfs-fs-00 Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(btrfs-fs-00);
lock(&delayed_node->mutex);
lock(btrfs-fs-00); lock(&mm->mmap_lock#2);
*** DEADLOCK ***
1 lock held by compsize/11122:
#0: ffff889fe720fe40 (btrfs-fs-00){++++}-{3:3}, at: __btrfs_tree_read_lock+0x39/0x180
stack backtrace: CPU: 17 PID: 11122 Comm: compsize Kdump: loaded Not tainted 5.8.0-rc7-00165-g04ec4da5f45f-dirty #922 Hardware name: Quanta Tioga Pass Single Side 01-0030993006/Tioga Pass Single Side, BIOS F08_3A18 12/20/2018 Call Trace: dump_stack+0x78/0xa0 check_noncircular+0x165/0x180 __lock_acquire+0x1272/0x2310 lock_acquire+0x9e/0x360 ? __might_fault+0x3e/0x90 ? find_held_lock+0x72/0x90 __might_fault+0x68/0x90 ? __might_fault+0x3e/0x90 _copy_to_user+0x1e/0x80 copy_to_sk.isra.32+0x121/0x300 ? btrfs_search_forward+0x2a6/0x360 search_ioctl+0x106/0x200 btrfs_ioctl_tree_search_v2+0x7b/0xf0 btrfs_ioctl+0x106f/0x30a0 ? __do_sys_newfstat+0x5a/0x70 ? ksys_ioctl+0x83/0xc0 ksys_ioctl+0x83/0xc0 __x64_sys_ioctl+0x16/0x20 do_syscall_64+0x50/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9
The problem is we're doing a copy_to_user() while holding tree locks, which can deadlock if we have to do a page fault for the copy_to_user(). This exists even without my locking changes, so it needs to be fixed. Rework the search ioctl to do the pre-fault and then copy_to_user_nofault for the copying.
--- fs/btrfs/extent_io.c | 8 ++++---- fs/btrfs/extent_io.h | 6 +++--- fs/btrfs/ioctl.c | 27 ++++++++++++++++++++------- 3 files changed, 27 insertions(+), 14 deletions(-)
fs/btrfs/extent_io.c +4 -4
@@ -5653,9 +5653,9 @@ void read_extent_buffer(const struct extent_buffer *eb, void *dstv, } } -int read_extent_buffer_to_user (const struct extent_buffer *eb, -void __user *dstv, -unsigned long start,unsigned long len) +int read_extent_buffer_to_user_nofault (const struct extent_buffer *eb, +void __user *dstv, +unsigned long start,unsigned long len) {size_t cur;size_t offset;
@@ -5675,7 +5675,7 @@ int read_extent_buffer_to_user(const struct extent_buffer *eb, cur =min (len, (PAGE_SIZE - offset)); kaddr =page_address (page); -if (copy_to_user (dst, kaddr + offset, cur)) { +if (copy_to_user_nofault (dst, kaddr + offset, cur)) { ret = -EFAULT ;break ; }
fs/btrfs/extent_io.h +3 -3
@@ -241,9 +241,9 @@ int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,void read_extent_buffer (const struct extent_buffer *eb,void *dst,unsigned long start,unsigned long len); -int read_extent_buffer_to_user (const struct extent_buffer *eb, -void __user *dst,unsigned long start, -unsigned long len); +int read_extent_buffer_to_user_nofault (const struct extent_buffer *eb, +void __user *dst,unsigned long start, +unsigned long len);void write_extent_buffer_fsid (const struct extent_buffer *eb,const void *src);void write_extent_buffer_chunk_tree_uuid (const struct extent_buffer *eb,const void *src);
fs/btrfs/ioctl.c +20 -7
@@ -2086,9 +2086,14 @@ static noinline int copy_to_sk(struct btrfs_path *path, sh.len = item_len; sh.transid = found_transid; -/* copy search result header */ -if (copy_to_user (ubuf + *sk_offset, &sh,sizeof (sh))) { - ret = -EFAULT ; +/* +* copy search result header, if we fault loop again so we can +* fault in the pages and -EFAULT there if there's a problem, +* otherwise we'll fault and then copy the buffer in properly +* this next time through +*/ +if (copy_to_user_nofault (ubuf + *sk_offset, &sh,sizeof (sh))) { + ret =0 ; goto out; }
@@ -2096,10 +2101,14 @@ static noinline int copy_to_sk(struct btrfs_path *path,if (item_len) {char __user *up = ubuf + *sk_offset; -/* copy the item */ -if (read_extent_buffer_to_user (leaf, up, - item_off, item_len)) { - ret = -EFAULT ; +/* copy the item, same behavior as above, but reset the +* sk_offset so we copy the full thing again. +*/ +if (read_extent_buffer_to_user_nofault (leaf, up, + item_off, + item_len)) { + ret =0 ; + *sk_offset -=sizeof (sh); goto out; }
@@ -2184,6 +2193,10 @@ static noinline int search_ioctl(struct inode *inode, key.offset = sk->min_offset;while (1 ) { + ret =fault_in_pages_writeable (ubuf, *buf_size - sk_offset); +if (ret) +break ; + ret =btrfs_search_forward (root, &key, path, sk->min_transid);if (ret !=0 ) {if (ret >0 )
Signature · 1 line
2.24.1
Re: [PATCH 02/17] btrfs: fix potential deadlock in the search ioctl
Quoted 103 lines · Josef Bacik
When I converted the locking over to using a rwsem I got the following lockdep splat
======================================================
WARNING: possible circular locking dependency detected 5.8.0-rc7-00165-g04ec4da5f45f-dirty #922 Not tainted
------------------------------------------------------
compsize/11122 is trying to acquire lock:ffff889fabca8768(&mm->mmap_lock#2){++++}-{3:3}, at:__might_fault+0x3e/0x90but task is already holding lock:
ffff889fe720fe40(btrfs-fs-00){++++}-{3:3}, at:__btrfs_tree_read_lock+0x39/0x180which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #2 (btrfs-fs-00){++++}-{3:3}:
down_write_nested+0x3b/0x70 __btrfs_tree_lock+0x24/0x120 btrfs_search_slot+0x756/0x990 btrfs_lookup_inode+0x3a/0xb4 __btrfs_update_delayed_inode+0x93/0x270 btrfs_async_run_delayed_root+0x168/0x230 btrfs_work_helper+0xd4/0x570 process_one_work+0x2ad/0x5f0 worker_thread+0x3a/0x3d0 kthread+0x133/0x150 ret_from_fork+0x1f/0x30-> #1 (&delayed_node->mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930 btrfs_delayed_update_inode+0x50/0x440 btrfs_update_inode+0x8a/0xf0 btrfs_dirty_inode+0x5b/0xd0 touch_atime+0xa1/0xd0 btrfs_file_mmap+0x3f/0x60 mmap_region+0x3a4/0x640 do_mmap+0x376/0x580 vm_mmap_pgoff+0xd5/0x120 ksys_mmap_pgoff+0x193/0x230 do_syscall_64+0x50/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #0 (&mm->mmap_lock#2){++++}-{3:3}:
__lock_acquire+0x1272/0x2310 lock_acquire+0x9e/0x360 __might_fault+0x68/0x90 _copy_to_user+0x1e/0x80 copy_to_sk.isra.32+0x121/0x300 search_ioctl+0x106/0x200 btrfs_ioctl_tree_search_v2+0x7b/0xf0 btrfs_ioctl+0x106f/0x30a0 ksys_ioctl+0x83/0xc0 __x64_sys_ioctl+0x16/0x20 do_syscall_64+0x50/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9other info that might help us debug this:
Chain exists of: &mm->mmap_lock#2 --> &delayed_node->mutex --> btrfs-fs-00 Possible unsafe locking scenario:CPU0 CPU1 ---- ---- lock(btrfs-fs-00); lock(&delayed_node->mutex); lock(btrfs-fs-00);
lock(&mm->mmap_lock#2);*** DEADLOCK ***
1 lock held by compsize/11122:
#0:ffff889fe720fe40(btrfs-fs-00){++++}-{3:3}, at:__btrfs_tree_read_lock+0x39/0x180stack backtrace: CPU: 17 PID: 11122 Comm: compsize Kdump: loaded Not tainted 5.8.0-rc7-00165-g04ec4da5f45f-dirty #922 Hardware name: Quanta Tioga Pass Single Side 01-0030993006/Tioga Pass Single Side, BIOS F08_3A18 12/20/2018 Call Trace: dump_stack+0x78/0xa0 check_noncircular+0x165/0x180 __lock_acquire+0x1272/0x2310 lock_acquire+0x9e/0x360 ? __might_fault+0x3e/0x90 ? find_held_lock+0x72/0x90 __might_fault+0x68/0x90 ? __might_fault+0x3e/0x90 _copy_to_user+0x1e/0x80 copy_to_sk.isra.32+0x121/0x300 ? btrfs_search_forward+0x2a6/0x360 search_ioctl+0x106/0x200 btrfs_ioctl_tree_search_v2+0x7b/0xf0 btrfs_ioctl+0x106f/0x30a0 ? __do_sys_newfstat+0x5a/0x70 ? ksys_ioctl+0x83/0xc0 ksys_ioctl+0x83/0xc0 __x64_sys_ioctl+0x16/0x20 do_syscall_64+0x50/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9
Quoted 7 lines
The problem is we're doing a
copy_to_user()while holding tree locks, which can deadlock if we have to do a page fault for thecopy_to_user(). This exists even without my locking changes, so it needs to be fixed. Rework the search ioctl to do the pre-fault and thencopy_to_user_nofaultfor the copying.Signed-off-by Josef Bacik <[email protected]>
Looks good, thanks.
Quoted 104 lines
--- fs/btrfs/extent_io.c | 8 ++++---- fs/btrfs/extent_io.h | 6 +++--- fs/btrfs/ioctl.c | 27 ++++++++++++++++++++------- 3 files changed, 27 insertions(+), 14 deletions(-)fs/btrfs/extent_io.c +4
-4@@ -5653,9 +5653,9 @@ void read_extent_buffer(const struct extent_buffer *eb, void *dstv, } } -int read_extent_buffer_to_user (const struct extent_buffer *eb, -void __user *dstv, -unsigned long start,unsigned long len) +int read_extent_buffer_to_user_nofault (const struct extent_buffer *eb, +void __user *dstv, +unsigned long start,unsigned long len) {size_t cur;size_t offset;@@ -5675,7 +5675,7 @@ int read_extent_buffer_to_user(const struct extent_buffer *eb, cur =min (len, (PAGE_SIZE - offset)); kaddr =page_address (page); -if (copy_to_user (dst, kaddr + offset, cur)) { +if (copy_to_user_nofault (dst, kaddr + offset, cur)) { ret = -EFAULT ;break ; }fs/btrfs/extent_io.h +3
-3@@ -241,9 +241,9 @@ int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,void read_extent_buffer (const struct extent_buffer *eb,void *dst,unsigned long start,unsigned long len); -int read_extent_buffer_to_user (const struct extent_buffer *eb, -void __user *dst,unsigned long start, -unsigned long len); +int read_extent_buffer_to_user_nofault (const struct extent_buffer *eb, +void __user *dst,unsigned long start, +unsigned long len);void write_extent_buffer_fsid (const struct extent_buffer *eb,const void *src);void write_extent_buffer_chunk_tree_uuid (const struct extent_buffer *eb,const void *src);fs/btrfs/ioctl.c +20
-8@@ -2086,9 +2086,14 @@ static noinline int copy_to_sk(struct btrfs_path *path, sh.len = item_len; sh.transid = found_transid; -/* copy search result header */ -if (copy_to_user (ubuf + *sk_offset, &sh,sizeof (sh))) { - ret = -EFAULT ; +/* +* copy search result header, if we fault loop again so we can +* fault in the pages and -EFAULT there if there's a problem, +* otherwise we'll fault and then copy the buffer in properly +* this next time through +*/ +if (copy_to_user_nofault (ubuf + *sk_offset, &sh,sizeof (sh))) { + ret =0 ; goto out; }@@ -2096,10 +2101,14 @@ static noinline int copy_to_sk(struct btrfs_path *path,if (item_len) {char __user *up = ubuf + *sk_offset; -/* copy the item */ -if (read_extent_buffer_to_user (leaf, up, - item_off, item_len)) { - ret = -EFAULT ; +/* copy the item, same behavior as above, but reset the +* sk_offset so we copy the full thing again. +*/ +if (read_extent_buffer_to_user_nofault (leaf, up, + item_off, + item_len)) { + ret =0 ; + *sk_offset -=sizeof (sh); goto out; }@@ -2184,6 +2193,10 @@ static noinline int search_ioctl(struct inode *inode, key.offset = sk->min_offset;while (1 ) { + ret =fault_in_pages_writeable (ubuf, *buf_size - sk_offset); +if (ret) +break ; + ret =btrfs_search_forward (root, &key, path, sk->min_transid);if (ret !=0 ) {if (ret >0 ) --2.24.1
Signature · 3 lines
Filipe David Manana, “Whether you think you can, or you think you can't — you're right.”
PATCH 3/17 btrfs: do not hold device_list_mutex when closing devices
In reply to Josef BacikThe following lockdep splat
======================================================
WARNING: possible circular locking dependency detected 5.8.0-rc7-00169-g87212851a027-dirty #929 Not tainted
------------------------------------------------------
fsstress/8739 is trying to acquire lock:ffff88bfd0eb0c90 (&fs_info->reloc_mutex){+.+.}-{3:3}, at: btrfs_record_root_in_trans+0x43/0x70
but task is already holding lock:ffff88bfbd16e538 (sb_pagefaults){.+.+}-{0:0}, at: btrfs_page_mkwrite+0x6a/0x4a0
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #10 (sb_pagefaults){.+.+}-{0:0}:
__sb_start_write+0x129/0x210
btrfs_page_mkwrite+0x6a/0x4a0
do_page_mkwrite+0x4d/0xc0
handle_mm_fault+0x103c/0x1730
exc_page_fault+0x340/0x660
asm_exc_page_fault+0x1e/0x30-> #9 (&mm->mmap_lock#2){++++}-{3:3}:
__might_fault+0x68/0x90
_copy_to_user+0x1e/0x80
perf_read+0x141/0x2c0
vfs_read+0xad/0x1b0
ksys_read+0x5f/0xe0
do_syscall_64+0x50/0x90
entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #8 (&cpuctx_mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930
perf_event_init_cpu+0x88/0x150
perf_event_init+0x1db/0x20b
start_kernel+0x3ae/0x53c
secondary_startup_64+0xa4/0xb0-> #7 (pmus_lock){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930
perf_event_init_cpu+0x4f/0x150
cpuhp_invoke_callback+0xb1/0x900
_cpu_up.constprop.26+0x9f/0x130
cpu_up+0x7b/0xc0
bringup_nonboot_cpus+0x4f/0x60
smp_init+0x26/0x71
kernel_init_freeable+0x110/0x258
kernel_init+0xa/0x103
ret_from_fork+0x1f/0x30-> #6 (cpu_hotplug_lock){++++}-{0:0}:
cpus_read_lock+0x39/0xb0
kmem_cache_create_usercopy+0x28/0x230
kmem_cache_create+0x12/0x20
bioset_init+0x15e/0x2b0
init_bio+0xa3/0xaa
do_one_initcall+0x5a/0x2e0
kernel_init_freeable+0x1f4/0x258
kernel_init+0xa/0x103
ret_from_fork+0x1f/0x30-> #5 (bio_slab_lock){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930
bioset_init+0xbc/0x2b0
__blk_alloc_queue+0x6f/0x2d0
blk_mq_init_queue_data+0x1b/0x70
loop_add+0x110/0x290 [loop]
fq_codel_tcf_block+0x12/0x20 [sch_fq_codel]
do_one_initcall+0x5a/0x2e0
do_init_module+0x5a/0x220
load_module+0x2459/0x26e0
__do_sys_finit_module+0xba/0xe0
do_syscall_64+0x50/0x90
entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #4 (loop_ctl_mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930
lo_open+0x18/0x50 [loop]
__blkdev_get+0xec/0x570
blkdev_get+0xe8/0x150
do_dentry_open+0x167/0x410
path_openat+0x7c9/0xa80
do_filp_open+0x93/0x100
do_sys_openat2+0x22a/0x2e0
do_sys_open+0x4b/0x80
do_syscall_64+0x50/0x90
entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #3 (&bdev->bd_mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930
blkdev_put+0x1d/0x120
close_fs_devices.part.31+0x84/0x130
btrfs_close_devices+0x44/0xb0
close_ctree+0x296/0x2b2
generic_shutdown_super+0x69/0x100
kill_anon_super+0xe/0x30
btrfs_kill_super+0x12/0x20
deactivate_locked_super+0x29/0x60
cleanup_mnt+0xb8/0x140
task_work_run+0x6d/0xb0
__prepare_exit_to_usermode+0x1cc/0x1e0
do_syscall_64+0x5c/0x90
entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #2 (&fs_devs->device_list_mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930
btrfs_run_dev_stats+0x49/0x480
commit_cowonly_roots+0xb5/0x2a0
btrfs_commit_transaction+0x516/0xa60
sync_filesystem+0x6b/0x90
generic_shutdown_super+0x22/0x100
kill_anon_super+0xe/0x30
btrfs_kill_super+0x12/0x20
deactivate_locked_super+0x29/0x60
cleanup_mnt+0xb8/0x140
task_work_run+0x6d/0xb0
__prepare_exit_to_usermode+0x1cc/0x1e0
do_syscall_64+0x5c/0x90
entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #1 (&fs_info->tree_log_mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930
btrfs_commit_transaction+0x4bb/0xa60
sync_filesystem+0x6b/0x90
generic_shutdown_super+0x22/0x100
kill_anon_super+0xe/0x30
btrfs_kill_super+0x12/0x20
deactivate_locked_super+0x29/0x60
cleanup_mnt+0xb8/0x140
task_work_run+0x6d/0xb0
__prepare_exit_to_usermode+0x1cc/0x1e0
do_syscall_64+0x5c/0x90
entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #0 (&fs_info->reloc_mutex){+.+.}-{3:3}:
__lock_acquire+0x1272/0x2310
lock_acquire+0x9e/0x360
__mutex_lock+0x9f/0x930
btrfs_record_root_in_trans+0x43/0x70
start_transaction+0xd1/0x5d0
btrfs_dirty_inode+0x42/0xd0
file_update_time+0xc8/0x110
btrfs_page_mkwrite+0x10c/0x4a0
do_page_mkwrite+0x4d/0xc0
handle_mm_fault+0x103c/0x1730
exc_page_fault+0x340/0x660
asm_exc_page_fault+0x1e/0x30other info that might help us debug this:
Chain exists of: &fs_info->reloc_mutex --> &mm->mmap_lock#2 --> sb_pagefaults Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(sb_pagefaults);
lock(&mm->mmap_lock#2);
lock(sb_pagefaults); lock(&fs_info->reloc_mutex);
*** DEADLOCK ***
3 locks held by fsstress/8739:
#0: ffff88bee66eeb68 (&mm->mmap_lock#2){++++}-{3:3}, at: exc_page_fault+0x173/0x660 #1: ffff88bfbd16e538 (sb_pagefaults){.+.+}-{0:0}, at: btrfs_page_mkwrite+0x6a/0x4a0 #2: ffff88bfbd16e630 (sb_internal){.+.+}-{0:0}, at: start_transaction+0x3da/0x5d0
stack backtrace: CPU: 17 PID: 8739 Comm: fsstress Kdump: loaded Not tainted 5.8.0-rc7-00169-g87212851a027-dirty #929 Hardware name: Quanta Tioga Pass Single Side 01-0030993006/Tioga Pass Single Side, BIOS F08_3A18 12/20/2018 Call Trace: dump_stack+0x78/0xa0 check_noncircular+0x165/0x180 __lock_acquire+0x1272/0x2310 ? btrfs_get_alloc_profile+0x150/0x210 lock_acquire+0x9e/0x360 ? btrfs_record_root_in_trans+0x43/0x70 __mutex_lock+0x9f/0x930 ? btrfs_record_root_in_trans+0x43/0x70 ? lock_acquire+0x9e/0x360 ? join_transaction+0x5d/0x450 ? find_held_lock+0x2d/0x90 ? btrfs_record_root_in_trans+0x43/0x70 ? join_transaction+0x3d5/0x450 ? btrfs_record_root_in_trans+0x43/0x70 btrfs_record_root_in_trans+0x43/0x70 start_transaction+0xd1/0x5d0 btrfs_dirty_inode+0x42/0xd0 file_update_time+0xc8/0x110 btrfs_page_mkwrite+0x10c/0x4a0 ? handle_mm_fault+0x5e/0x1730 do_page_mkwrite+0x4d/0xc0 ? __do_fault+0x32/0x150 handle_mm_fault+0x103c/0x1730 exc_page_fault+0x340/0x660 ? asm_exc_page_fault+0x8/0x30 asm_exc_page_fault+0x1e/0x30 RIP: 0033:0x7faa6c9969c4
Was seen in testing. The fix is similar to that of
btrfs: open device without device_list_mutex
where we're holding the device_list_mutex and then grab the bd_mutex, which pulls in a bunch of dependencies under the bd_mutex. We only ever call btrfs_close_devices() on mount failure or unmount, so we're save to not have the device_list_mutex here. We're already holding the uuid_mutex which keeps us safe from any external modification of the fs_devices.
--- fs/btrfs/volumes.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
fs/btrfs/volumes.c +1 -4
@@ -1155,11 +1155,8 @@ static int close_fs_devices(struct btrfs_fs_devices *fs_devices)if (--fs_devices->opened >0 )return 0 ; -mutex_lock (&fs_devices->device_list_mutex); -list_for_each_entry_safe (device, tmp, &fs_devices->devices, dev_list) { +list_for_each_entry_safe (device, tmp, &fs_devices->devices, dev_list)btrfs_close_one_device (device); - } -mutex_unlock (&fs_devices->device_list_mutex);WARN_ON (fs_devices->open_devices);WARN_ON (fs_devices->rw_devices);
Signature · 1 line
2.24.1
Re: [PATCH 03/17] btrfs: do not hold device_list_mutex when closing devices
Quoted 206 lines · Josef Bacik
The following lockdep splat
======================================================
WARNING: possible circular locking dependency detected 5.8.0-rc7-00169-g87212851a027-dirty #929 Not tainted
------------------------------------------------------
fsstress/8739 is trying to acquire lock:ffff88bfd0eb0c90(&fs_info->reloc_mutex){+.+.}-{3:3}, at:btrfs_record_root_in_trans+0x43/0x70but task is already holding lock:
ffff88bfbd16e538(sb_pagefaults){.+.+}-{0:0}, at:btrfs_page_mkwrite+0x6a/0x4a0which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #10 (sb_pagefaults){.+.+}-{0:0}:
__sb_start_write+0x129/0x210 btrfs_page_mkwrite+0x6a/0x4a0 do_page_mkwrite+0x4d/0xc0 handle_mm_fault+0x103c/0x1730 exc_page_fault+0x340/0x660 asm_exc_page_fault+0x1e/0x30-> #9 (&mm->mmap_lock#2){++++}-{3:3}:
__might_fault+0x68/0x90 _copy_to_user+0x1e/0x80 perf_read+0x141/0x2c0 vfs_read+0xad/0x1b0 ksys_read+0x5f/0xe0 do_syscall_64+0x50/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #8 (&cpuctx_mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930 perf_event_init_cpu+0x88/0x150 perf_event_init+0x1db/0x20b start_kernel+0x3ae/0x53c secondary_startup_64+0xa4/0xb0-> #7 (pmus_lock){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930 perf_event_init_cpu+0x4f/0x150 cpuhp_invoke_callback+0xb1/0x900 _cpu_up.constprop.26+0x9f/0x130 cpu_up+0x7b/0xc0 bringup_nonboot_cpus+0x4f/0x60 smp_init+0x26/0x71 kernel_init_freeable+0x110/0x258 kernel_init+0xa/0x103 ret_from_fork+0x1f/0x30-> #6 (cpu_hotplug_lock){++++}-{0:0}:
cpus_read_lock+0x39/0xb0 kmem_cache_create_usercopy+0x28/0x230 kmem_cache_create+0x12/0x20 bioset_init+0x15e/0x2b0 init_bio+0xa3/0xaa do_one_initcall+0x5a/0x2e0 kernel_init_freeable+0x1f4/0x258 kernel_init+0xa/0x103 ret_from_fork+0x1f/0x30-> #5 (bio_slab_lock){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930 bioset_init+0xbc/0x2b0 __blk_alloc_queue+0x6f/0x2d0 blk_mq_init_queue_data+0x1b/0x70 loop_add+0x110/0x290 [loop] fq_codel_tcf_block+0x12/0x20 [sch_fq_codel] do_one_initcall+0x5a/0x2e0 do_init_module+0x5a/0x220 load_module+0x2459/0x26e0 __do_sys_finit_module+0xba/0xe0 do_syscall_64+0x50/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #4 (loop_ctl_mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930 lo_open+0x18/0x50 [loop] __blkdev_get+0xec/0x570 blkdev_get+0xe8/0x150 do_dentry_open+0x167/0x410 path_openat+0x7c9/0xa80 do_filp_open+0x93/0x100 do_sys_openat2+0x22a/0x2e0 do_sys_open+0x4b/0x80 do_syscall_64+0x50/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #3 (&bdev->bd_mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930 blkdev_put+0x1d/0x120 close_fs_devices.part.31+0x84/0x130 btrfs_close_devices+0x44/0xb0 close_ctree+0x296/0x2b2 generic_shutdown_super+0x69/0x100 kill_anon_super+0xe/0x30 btrfs_kill_super+0x12/0x20 deactivate_locked_super+0x29/0x60 cleanup_mnt+0xb8/0x140 task_work_run+0x6d/0xb0 __prepare_exit_to_usermode+0x1cc/0x1e0 do_syscall_64+0x5c/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #2 (&fs_devs->device_list_mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930 btrfs_run_dev_stats+0x49/0x480 commit_cowonly_roots+0xb5/0x2a0 btrfs_commit_transaction+0x516/0xa60 sync_filesystem+0x6b/0x90 generic_shutdown_super+0x22/0x100 kill_anon_super+0xe/0x30 btrfs_kill_super+0x12/0x20 deactivate_locked_super+0x29/0x60 cleanup_mnt+0xb8/0x140 task_work_run+0x6d/0xb0 __prepare_exit_to_usermode+0x1cc/0x1e0 do_syscall_64+0x5c/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #1 (&fs_info->tree_log_mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930 btrfs_commit_transaction+0x4bb/0xa60 sync_filesystem+0x6b/0x90 generic_shutdown_super+0x22/0x100 kill_anon_super+0xe/0x30 btrfs_kill_super+0x12/0x20 deactivate_locked_super+0x29/0x60 cleanup_mnt+0xb8/0x140 task_work_run+0x6d/0xb0 __prepare_exit_to_usermode+0x1cc/0x1e0 do_syscall_64+0x5c/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #0 (&fs_info->reloc_mutex){+.+.}-{3:3}:
__lock_acquire+0x1272/0x2310 lock_acquire+0x9e/0x360 __mutex_lock+0x9f/0x930 btrfs_record_root_in_trans+0x43/0x70 start_transaction+0xd1/0x5d0 btrfs_dirty_inode+0x42/0xd0 file_update_time+0xc8/0x110 btrfs_page_mkwrite+0x10c/0x4a0 do_page_mkwrite+0x4d/0xc0 handle_mm_fault+0x103c/0x1730 exc_page_fault+0x340/0x660 asm_exc_page_fault+0x1e/0x30other info that might help us debug this:
Chain exists of: &fs_info->reloc_mutex --> &mm->mmap_lock#2 --> sb_pagefaults Possible unsafe locking scenario:CPU0 CPU1 ---- ---- lock(sb_pagefaults); lock(&mm->mmap_lock#2); lock(sb_pagefaults);
lock(&fs_info->reloc_mutex);*** DEADLOCK ***
3 locks held by fsstress/8739:
#0:ffff88bee66eeb68(&mm->mmap_lock#2){++++}-{3:3}, at:exc_page_fault+0x173/0x660#1:ffff88bfbd16e538(sb_pagefaults){.+.+}-{0:0}, at:btrfs_page_mkwrite+0x6a/0x4a0#2:ffff88bfbd16e630(sb_internal){.+.+}-{0:0}, at:start_transaction+0x3da/0x5d0stack backtrace: CPU: 17 PID: 8739 Comm: fsstress Kdump: loaded Not tainted 5.8.0-rc7-00169-g87212851a027-dirty #929 Hardware name: Quanta Tioga Pass Single Side 01-0030993006/Tioga Pass Single Side, BIOS F08_3A18 12/20/2018 Call Trace: dump_stack+0x78/0xa0 check_noncircular+0x165/0x180 __lock_acquire+0x1272/0x2310 ? btrfs_get_alloc_profile+0x150/0x210 lock_acquire+0x9e/0x360 ? btrfs_record_root_in_trans+0x43/0x70 __mutex_lock+0x9f/0x930 ? btrfs_record_root_in_trans+0x43/0x70 ? lock_acquire+0x9e/0x360 ? join_transaction+0x5d/0x450 ? find_held_lock+0x2d/0x90 ? btrfs_record_root_in_trans+0x43/0x70 ? join_transaction+0x3d5/0x450 ? btrfs_record_root_in_trans+0x43/0x70 btrfs_record_root_in_trans+0x43/0x70 start_transaction+0xd1/0x5d0 btrfs_dirty_inode+0x42/0xd0 file_update_time+0xc8/0x110 btrfs_page_mkwrite+0x10c/0x4a0 ? handle_mm_fault+0x5e/0x1730 do_page_mkwrite+0x4d/0xc0 ? __do_fault+0x32/0x150 handle_mm_fault+0x103c/0x1730 exc_page_fault+0x340/0x660 ? asm_exc_page_fault+0x8/0x30 asm_exc_page_fault+0x1e/0x30 RIP: 0033:0x7faa6c9969c4Was seen in testing. The fix is similar to that of
Quoted 8 lines
btrfs: open device without
device_list_mutexwhere we're holding the
device_list_mutexand then grab thebd_mutex, which pulls in a bunch of dependencies under thebd_mutex. We only ever callbtrfs_close_devices()on mount failure or unmount, so we're save to not have thedevice_list_mutexhere. We're already holding theuuid_mutexwhich keeps us safe from any external modification of thefs_devices.
We aren't holding the uuid_mutex at btrfs_rm_dev_replace_free_srcdev(). Will it not be a problem?
Thanks.
Quoted 26 lines
Signed-off-by Josef Bacik <[email protected]>--- fs/btrfs/volumes.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)fs/btrfs/volumes.c +1
-5@@ -1155,11 +1155,8 @@ static int close_fs_devices(struct btrfs_fs_devices *fs_devices)if (--fs_devices->opened >0 )return 0 ; -mutex_lock (&fs_devices->device_list_mutex); -list_for_each_entry_safe (device, tmp, &fs_devices->devices, dev_list) { +list_for_each_entry_safe (device, tmp, &fs_devices->devices, dev_list)btrfs_close_one_device (device); - } -mutex_unlock (&fs_devices->device_list_mutex);WARN_ON (fs_devices->open_devices);WARN_ON (fs_devices->rw_devices); --2.24.1
Signature · 3 lines
Filipe David Manana, “Whether you think you can, or you think you can't — you're right.”
Re: [PATCH 03/17] btrfs: do not hold device_list_mutex when closing devices
In reply to Josef BacikQuoted 14 lines · Josef Bacik
not have the
device_list_mutexhere. We're already holding theuuid_mutexwhich keeps us safe from any external modification of thefs_devices.Signed-off-by Josef Bacik <[email protected]>--- fs/btrfs/volumes.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)fs/btrfs/volumes.c +0
-0
fs/btrfs/volumes.c static int close_fs_devices(struct btrfs_fs_devices *fs_devices) · line 1162
Quoted 9 lines
fs/btrfs/volumes.c +1
-4@@ -1155,11 +1155,8 @@ static int close_fs_devices(struct btrfs_fs_devices *fs_devices)if (--fs_devices->opened >0 )return 0 ; -mutex_lock (&fs_devices->device_list_mutex); -list_for_each_entry_safe (device, tmp, &fs_devices->devices, dev_list) { +list_for_each_entry_safe (device, tmp, &fs_devices->devices, dev_list)btrfs_close_one_device (device); - } -mutex_unlock (&fs_devices->device_list_mutex);
As Filipe pointed out, uuid mutex is not held in the dev replace function, I suggest to add
lockdep_assert_held(&uuid_mutex);
when locks are removed and we rely on callers to make sure the right locks are taken.
PATCH 4/17 btrfs: allocate scrub workqueues outside of locks
In reply to Josef BacikI got the following lockdep splat while testing
======================================================
WARNING: possible circular locking dependency detected 5.8.0-rc7-00172-g021118712e59 #932 Not tainted
------------------------------------------------------
btrfs/229626 is trying to acquire lock:ffffffff828513f0 (cpu_hotplug_lock){++++}-{0:0}, at: alloc_workqueue+0x378/0x450
but task is already holding lock:ffff889dd3889518 (&fs_info->scrub_lock){+.+.}-{3:3}, at: btrfs_scrub_dev+0x11c/0x630
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #7 (&fs_info->scrub_lock){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930
btrfs_scrub_dev+0x11c/0x630
btrfs_dev_replace_by_ioctl.cold.21+0x10a/0x1d4
btrfs_ioctl+0x2799/0x30a0
ksys_ioctl+0x83/0xc0
__x64_sys_ioctl+0x16/0x20
do_syscall_64+0x50/0x90
entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #6 (&fs_devs->device_list_mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930
btrfs_run_dev_stats+0x49/0x480
commit_cowonly_roots+0xb5/0x2a0
btrfs_commit_transaction+0x516/0xa60
sync_filesystem+0x6b/0x90
generic_shutdown_super+0x22/0x100
kill_anon_super+0xe/0x30
btrfs_kill_super+0x12/0x20
deactivate_locked_super+0x29/0x60
cleanup_mnt+0xb8/0x140
task_work_run+0x6d/0xb0
__prepare_exit_to_usermode+0x1cc/0x1e0
do_syscall_64+0x5c/0x90
entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #5 (&fs_info->tree_log_mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930
btrfs_commit_transaction+0x4bb/0xa60
sync_filesystem+0x6b/0x90
generic_shutdown_super+0x22/0x100
kill_anon_super+0xe/0x30
btrfs_kill_super+0x12/0x20
deactivate_locked_super+0x29/0x60
cleanup_mnt+0xb8/0x140
task_work_run+0x6d/0xb0
__prepare_exit_to_usermode+0x1cc/0x1e0
do_syscall_64+0x5c/0x90
entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #4 (&fs_info->reloc_mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930
btrfs_record_root_in_trans+0x43/0x70
start_transaction+0xd1/0x5d0
btrfs_dirty_inode+0x42/0xd0
touch_atime+0xa1/0xd0
btrfs_file_mmap+0x3f/0x60
mmap_region+0x3a4/0x640
do_mmap+0x376/0x580
vm_mmap_pgoff+0xd5/0x120
ksys_mmap_pgoff+0x193/0x230
do_syscall_64+0x50/0x90
entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #3 (&mm->mmap_lock#2){++++}-{3:3}:
__might_fault+0x68/0x90
_copy_to_user+0x1e/0x80
perf_read+0x141/0x2c0
vfs_read+0xad/0x1b0
ksys_read+0x5f/0xe0
do_syscall_64+0x50/0x90
entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #2 (&cpuctx_mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930
perf_event_init_cpu+0x88/0x150
perf_event_init+0x1db/0x20b
start_kernel+0x3ae/0x53c
secondary_startup_64+0xa4/0xb0-> #1 (pmus_lock){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930
perf_event_init_cpu+0x4f/0x150
cpuhp_invoke_callback+0xb1/0x900
_cpu_up.constprop.26+0x9f/0x130
cpu_up+0x7b/0xc0
bringup_nonboot_cpus+0x4f/0x60
smp_init+0x26/0x71
kernel_init_freeable+0x110/0x258
kernel_init+0xa/0x103
ret_from_fork+0x1f/0x30-> #0 (cpu_hotplug_lock){++++}-{0:0}:
__lock_acquire+0x1272/0x2310
lock_acquire+0x9e/0x360
cpus_read_lock+0x39/0xb0
alloc_workqueue+0x378/0x450
__btrfs_alloc_workqueue+0x15d/0x200
btrfs_alloc_workqueue+0x51/0x160
scrub_workers_get+0x5a/0x170
btrfs_scrub_dev+0x18c/0x630
btrfs_dev_replace_by_ioctl.cold.21+0x10a/0x1d4
btrfs_ioctl+0x2799/0x30a0
ksys_ioctl+0x83/0xc0
__x64_sys_ioctl+0x16/0x20
do_syscall_64+0x50/0x90
entry_SYSCALL_64_after_hwframe+0x44/0xa9other info that might help us debug this:
Chain exists of: cpu_hotplug_lock --> &fs_devs->device_list_mutex --> &fs_info->scrub_lock Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(&fs_info->scrub_lock);
lock(&fs_devs->device_list_mutex);
lock(&fs_info->scrub_lock); lock(cpu_hotplug_lock);
*** DEADLOCK ***
2 locks held by btrfs/229626:
#0: ffff88bfe8bb86e0 (&fs_devs->device_list_mutex){+.+.}-{3:3}, at: btrfs_scrub_dev+0xbd/0x630 #1: ffff889dd3889518 (&fs_info->scrub_lock){+.+.}-{3:3}, at: btrfs_scrub_dev+0x11c/0x630
stack backtrace: CPU: 15 PID: 229626 Comm: btrfs Kdump: loaded Not tainted 5.8.0-rc7-00172-g021118712e59 #932 Hardware name: Quanta Tioga Pass Single Side 01-0030993006/Tioga Pass Single Side, BIOS F08_3A18 12/20/2018 Call Trace: dump_stack+0x78/0xa0 check_noncircular+0x165/0x180 __lock_acquire+0x1272/0x2310 lock_acquire+0x9e/0x360 ? alloc_workqueue+0x378/0x450 cpus_read_lock+0x39/0xb0 ? alloc_workqueue+0x378/0x450 alloc_workqueue+0x378/0x450 ? rcu_read_lock_sched_held+0x52/0x80 __btrfs_alloc_workqueue+0x15d/0x200 btrfs_alloc_workqueue+0x51/0x160 scrub_workers_get+0x5a/0x170 btrfs_scrub_dev+0x18c/0x630 ? start_transaction+0xd1/0x5d0 btrfs_dev_replace_by_ioctl.cold.21+0x10a/0x1d4 btrfs_ioctl+0x2799/0x30a0 ? do_sigaction+0x102/0x250 ? lockdep_hardirqs_on_prepare+0xca/0x160 ? _raw_spin_unlock_irq+0x24/0x30 ? trace_hardirqs_on+0x1c/0xe0 ? _raw_spin_unlock_irq+0x24/0x30 ? do_sigaction+0x102/0x250 ? ksys_ioctl+0x83/0xc0 ksys_ioctl+0x83/0xc0 __x64_sys_ioctl+0x16/0x20 do_syscall_64+0x50/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9
This happens because we're allocating the scrub workqueues under the scrub and device list mutex, which brings in a whole host of other dependencies. Fix this by moving the actual allocation outside of the scrub lock, and then only take the lock once we're ready to actually assign them to the fs_info. We'll now have to cleanup the workqueues in a few more places, so I've added a helper to do the refcount dance to safely free the workqueues.
--- fs/btrfs/scrub.c | 122 +++++++++++++++++++++++++++-------------------- 1 file changed, 70 insertions(+), 52 deletions(-)
fs/btrfs/scrub.c +70 -52
@@ -3716,50 +3716,84 @@ static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,return 0 ; } +static void scrub_workers_put (struct btrfs_fs_info *fs_info) +{ +if (refcount_dec_and_mutex_lock (&fs_info->scrub_workers_refcnt, + &fs_info->scrub_lock)) { +struct btrfs_workqueue *scrub_workers =NULL ; +struct btrfs_workqueue *scrub_wr_comp =NULL ; +struct btrfs_workqueue *scrub_parity =NULL ; + + scrub_workers = fs_info->scrub_workers; + scrub_wr_comp = fs_info->scrub_wr_completion_workers; + scrub_parity = fs_info->scrub_parity_workers; + + fs_info->scrub_workers =NULL ; + fs_info->scrub_wr_completion_workers =NULL ; + fs_info->scrub_parity_workers =NULL ; +mutex_unlock (&fs_info->scrub_lock); + +btrfs_destroy_workqueue (scrub_workers); +btrfs_destroy_workqueue (scrub_wr_comp); +btrfs_destroy_workqueue (scrub_parity); + } +} +/* * get a reference count on fs_info->scrub_workers. start worker if necessary */ static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,int is_dev_replace) { +struct btrfs_workqueue *scrub_workers =NULL ; +struct btrfs_workqueue *scrub_wr_comp =NULL ; +struct btrfs_workqueue *scrub_parity =NULL ;unsigned int flags =WQ_FREEZABLE |WQ_UNBOUND ;int max_active = fs_info->thread_pool_size; +int ret = -ENOMEM ; -lockdep_assert_held (&fs_info->scrub_lock); +if (refcount_inc_not_zero (&fs_info->scrub_workers_refcnt)) +return 0 ; -if (refcount_read (&fs_info->scrub_workers_refcnt) ==0 ) { -ASSERT (fs_info->scrub_workers ==NULL ); - fs_info->scrub_workers =btrfs_alloc_workqueue (fs_info,"scrub" , - flags, is_dev_replace ?1 : max_active,4 ); -if (!fs_info->scrub_workers) - goto fail_scrub_workers; - -ASSERT (fs_info->scrub_wr_completion_workers ==NULL ); - fs_info->scrub_wr_completion_workers = -btrfs_alloc_workqueue (fs_info,"scrubwrc" , flags, - max_active,2 ); -if (!fs_info->scrub_wr_completion_workers) - goto fail_scrub_wr_completion_workers; + scrub_workers =btrfs_alloc_workqueue (fs_info,"scrub" , flags, + is_dev_replace ?1 : max_active, +4 ); +if (!scrub_workers) + goto fail_scrub_workers; -ASSERT (fs_info->scrub_parity_workers ==NULL ); - fs_info->scrub_parity_workers = -btrfs_alloc_workqueue (fs_info,"scrubparity" , flags, + scrub_wr_comp =btrfs_alloc_workqueue (fs_info,"scrubwrc" , flags, max_active,2 ); -if (!fs_info->scrub_parity_workers) - goto fail_scrub_parity_workers; +if (!scrub_wr_comp) + goto fail_scrub_wr_completion_workers; + scrub_parity =btrfs_alloc_workqueue (fs_info,"scrubparity" , flags, + max_active,2 ); +if (!scrub_parity) + goto fail_scrub_parity_workers; + +mutex_lock (&fs_info->scrub_lock); +if (refcount_read (&fs_info->scrub_workers_refcnt) ==0 ) { +ASSERT (fs_info->scrub_workers ==NULL && + fs_info->scrub_wr_completion_workers ==NULL && + fs_info->scrub_parity_workers ==NULL ); + fs_info->scrub_workers = scrub_workers; + fs_info->scrub_wr_completion_workers = scrub_wr_comp; + fs_info->scrub_parity_workers = scrub_parity;refcount_set (&fs_info->scrub_workers_refcnt,1 ); - }else { -refcount_inc (&fs_info->scrub_workers_refcnt); +mutex_unlock (&fs_info->scrub_lock); +return 0 ; } -return 0 ; +refcount_inc (&fs_info->scrub_workers_refcnt); +mutex_unlock (&fs_info->scrub_lock); + ret =0 ; +btrfs_destroy_workqueue (scrub_parity); fail_scrub_parity_workers: -btrfs_destroy_workqueue (fs_info->scrub_wr_completion_workers); +btrfs_destroy_workqueue (scrub_wr_comp); fail_scrub_wr_completion_workers: -btrfs_destroy_workqueue (fs_info->scrub_workers); +btrfs_destroy_workqueue (scrub_workers); fail_scrub_workers: -return -ENOMEM ; +return ret; } int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
@@ -3770,9 +3804,6 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,int ret;struct btrfs_device *dev;unsigned int nofs_flag; -struct btrfs_workqueue *scrub_workers =NULL ; -struct btrfs_workqueue *scrub_wr_comp =NULL ; -struct btrfs_workqueue *scrub_parity =NULL ;if (btrfs_fs_closing (fs_info))return -EAGAIN ;
@@ -3819,13 +3850,17 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,if (IS_ERR (sctx))return PTR_ERR (sctx); + ret =scrub_workers_get (fs_info, is_dev_replace); +if (ret) + goto out_free_ctx; +mutex_lock (&fs_info->fs_devices->device_list_mutex); dev =btrfs_find_device (fs_info->fs_devices, devid,NULL ,NULL , true);if (!dev || (test_bit (BTRFS_DEV_STATE_MISSING , &dev->dev_state) && !is_dev_replace)) {mutex_unlock (&fs_info->fs_devices->device_list_mutex); ret = -ENODEV ; - goto out_free_ctx; + goto out; } if (!is_dev_replace && !readonly &&
@@ -3834,7 +3869,7 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,btrfs_err_in_rcu (fs_info,"scrub: device %s is not writable" ,rcu_str_deref (dev->name)); ret = -EROFS ; - goto out_free_ctx; + goto out; }mutex_lock (&fs_info->scrub_lock);
@@ -3843,7 +3878,7 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,mutex_unlock (&fs_info->scrub_lock);mutex_unlock (&fs_info->fs_devices->device_list_mutex); ret = -EIO ; - goto out_free_ctx; + goto out; }down_read (&fs_info->dev_replace.rwsem);
@@ -3854,17 +3889,10 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,mutex_unlock (&fs_info->scrub_lock);mutex_unlock (&fs_info->fs_devices->device_list_mutex); ret = -EINPROGRESS ; - goto out_free_ctx; + goto out; }up_read (&fs_info->dev_replace.rwsem); - 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); - goto out_free_ctx; - } - sctx->readonly = readonly; dev->scrub_ctx = sctx;mutex_unlock (&fs_info->fs_devices->device_list_mutex);
@@ -3917,24 +3945,14 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,mutex_lock (&fs_info->scrub_lock); dev->scrub_ctx =NULL ; -if (refcount_dec_and_test (&fs_info->scrub_workers_refcnt)) { - scrub_workers = fs_info->scrub_workers; - scrub_wr_comp = fs_info->scrub_wr_completion_workers; - scrub_parity = fs_info->scrub_parity_workers; - - fs_info->scrub_workers =NULL ; - fs_info->scrub_wr_completion_workers =NULL ; - fs_info->scrub_parity_workers =NULL ; - }mutex_unlock (&fs_info->scrub_lock); -btrfs_destroy_workqueue (scrub_workers); -btrfs_destroy_workqueue (scrub_wr_comp); -btrfs_destroy_workqueue (scrub_parity); +scrub_workers_put (fs_info);scrub_put_ctx (sctx);return ret; - +out: +scrub_workers_put (fs_info); out_free_ctx:scrub_free_ctx (sctx);
Signature · 1 line
2.24.1
Re: [PATCH 04/17] btrfs: allocate scrub workqueues outside of locks
Quoted 167 lines · Josef Bacik
I got the following lockdep splat while testing
======================================================
WARNING: possible circular locking dependency detected 5.8.0-rc7-00172-g021118712e59 #932 Not tainted
------------------------------------------------------
btrfs/229626 is trying to acquire lock:ffffffff828513f0(cpu_hotplug_lock){++++}-{0:0}, at:alloc_workqueue+0x378/0x450but task is already holding lock:
ffff889dd3889518(&fs_info->scrub_lock){+.+.}-{3:3}, at:btrfs_scrub_dev+0x11c/0x630which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #7 (&fs_info->scrub_lock){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930 btrfs_scrub_dev+0x11c/0x630 btrfs_dev_replace_by_ioctl.cold.21+0x10a/0x1d4 btrfs_ioctl+0x2799/0x30a0 ksys_ioctl+0x83/0xc0 __x64_sys_ioctl+0x16/0x20 do_syscall_64+0x50/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #6 (&fs_devs->device_list_mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930 btrfs_run_dev_stats+0x49/0x480 commit_cowonly_roots+0xb5/0x2a0 btrfs_commit_transaction+0x516/0xa60 sync_filesystem+0x6b/0x90 generic_shutdown_super+0x22/0x100 kill_anon_super+0xe/0x30 btrfs_kill_super+0x12/0x20 deactivate_locked_super+0x29/0x60 cleanup_mnt+0xb8/0x140 task_work_run+0x6d/0xb0 __prepare_exit_to_usermode+0x1cc/0x1e0 do_syscall_64+0x5c/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #5 (&fs_info->tree_log_mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930 btrfs_commit_transaction+0x4bb/0xa60 sync_filesystem+0x6b/0x90 generic_shutdown_super+0x22/0x100 kill_anon_super+0xe/0x30 btrfs_kill_super+0x12/0x20 deactivate_locked_super+0x29/0x60 cleanup_mnt+0xb8/0x140 task_work_run+0x6d/0xb0 __prepare_exit_to_usermode+0x1cc/0x1e0 do_syscall_64+0x5c/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #4 (&fs_info->reloc_mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930 btrfs_record_root_in_trans+0x43/0x70 start_transaction+0xd1/0x5d0 btrfs_dirty_inode+0x42/0xd0 touch_atime+0xa1/0xd0 btrfs_file_mmap+0x3f/0x60 mmap_region+0x3a4/0x640 do_mmap+0x376/0x580 vm_mmap_pgoff+0xd5/0x120 ksys_mmap_pgoff+0x193/0x230 do_syscall_64+0x50/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #3 (&mm->mmap_lock#2){++++}-{3:3}:
__might_fault+0x68/0x90 _copy_to_user+0x1e/0x80 perf_read+0x141/0x2c0 vfs_read+0xad/0x1b0 ksys_read+0x5f/0xe0 do_syscall_64+0x50/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9-> #2 (&cpuctx_mutex){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930 perf_event_init_cpu+0x88/0x150 perf_event_init+0x1db/0x20b start_kernel+0x3ae/0x53c secondary_startup_64+0xa4/0xb0-> #1 (pmus_lock){+.+.}-{3:3}:
__mutex_lock+0x9f/0x930 perf_event_init_cpu+0x4f/0x150 cpuhp_invoke_callback+0xb1/0x900 _cpu_up.constprop.26+0x9f/0x130 cpu_up+0x7b/0xc0 bringup_nonboot_cpus+0x4f/0x60 smp_init+0x26/0x71 kernel_init_freeable+0x110/0x258 kernel_init+0xa/0x103 ret_from_fork+0x1f/0x30-> #0 (cpu_hotplug_lock){++++}-{0:0}:
__lock_acquire+0x1272/0x2310 lock_acquire+0x9e/0x360 cpus_read_lock+0x39/0xb0 alloc_workqueue+0x378/0x450 __btrfs_alloc_workqueue+0x15d/0x200 btrfs_alloc_workqueue+0x51/0x160 scrub_workers_get+0x5a/0x170 btrfs_scrub_dev+0x18c/0x630 btrfs_dev_replace_by_ioctl.cold.21+0x10a/0x1d4 btrfs_ioctl+0x2799/0x30a0 ksys_ioctl+0x83/0xc0 __x64_sys_ioctl+0x16/0x20 do_syscall_64+0x50/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9other info that might help us debug this:
Chain exists of: cpu_hotplug_lock --> &fs_devs->device_list_mutex --> &fs_info->scrub_lock Possible unsafe locking scenario:CPU0 CPU1 ---- ---- lock(&fs_info->scrub_lock); lock(&fs_devs->device_list_mutex); lock(&fs_info->scrub_lock);
lock(cpu_hotplug_lock);*** DEADLOCK ***
2 locks held by btrfs/229626:
#0:ffff88bfe8bb86e0(&fs_devs->device_list_mutex){+.+.}-{3:3}, at:btrfs_scrub_dev+0xbd/0x630#1:ffff889dd3889518(&fs_info->scrub_lock){+.+.}-{3:3}, at:btrfs_scrub_dev+0x11c/0x630stack backtrace: CPU: 15 PID: 229626 Comm: btrfs Kdump: loaded Not tainted 5.8.0-rc7-00172-g021118712e59 #932 Hardware name: Quanta Tioga Pass Single Side 01-0030993006/Tioga Pass Single Side, BIOS F08_3A18 12/20/2018 Call Trace: dump_stack+0x78/0xa0 check_noncircular+0x165/0x180 __lock_acquire+0x1272/0x2310 lock_acquire+0x9e/0x360 ? alloc_workqueue+0x378/0x450 cpus_read_lock+0x39/0xb0 ? alloc_workqueue+0x378/0x450 alloc_workqueue+0x378/0x450 ? rcu_read_lock_sched_held+0x52/0x80 __btrfs_alloc_workqueue+0x15d/0x200 btrfs_alloc_workqueue+0x51/0x160 scrub_workers_get+0x5a/0x170 btrfs_scrub_dev+0x18c/0x630 ? start_transaction+0xd1/0x5d0 btrfs_dev_replace_by_ioctl.cold.21+0x10a/0x1d4 btrfs_ioctl+0x2799/0x30a0 ? do_sigaction+0x102/0x250 ? lockdep_hardirqs_on_prepare+0xca/0x160 ? _raw_spin_unlock_irq+0x24/0x30 ? trace_hardirqs_on+0x1c/0xe0 ? _raw_spin_unlock_irq+0x24/0x30 ? do_sigaction+0x102/0x250 ? ksys_ioctl+0x83/0xc0 ksys_ioctl+0x83/0xc0 __x64_sys_ioctl+0x16/0x20 do_syscall_64+0x50/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9
Quoted 3 lines
This happens because we're allocating the scrub workqueues under the scrub and device list mutex, which brings in a whole host of other dependencies.
Because the work queue allocation is done with GFP_KERNEL, it can trigger reclaim, which can lead to a transaction commit, which in turns needs the device_list_mutex,
it can lead to a deadlock. A different problem for which this fix is a solution. Maybe worth mentioning as well.
Quoted 7 lines
Fix this by moving the actual allocation outside of the scrub lock, and then only take the lock once we're ready to actually assign them to the
fs_info. We'll now have to cleanup the workqueues in a few more places, so I've added a helper to do the refcount dance to safely free the workqueues.Signed-off-by Josef Bacik <[email protected]>
Looks good, thanks.
Quoted 215 lines
--- fs/btrfs/scrub.c | 122 +++++++++++++++++++++++++++-------------------- 1 file changed, 70 insertions(+), 52 deletions(-)fs/btrfs/scrub.c +70
-53@@ -3716,50 +3716,84 @@ static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,return 0 ; } +static void scrub_workers_put (struct btrfs_fs_info *fs_info) +{ +if (refcount_dec_and_mutex_lock (&fs_info->scrub_workers_refcnt, + &fs_info->scrub_lock)) { +struct btrfs_workqueue *scrub_workers =NULL ; +struct btrfs_workqueue *scrub_wr_comp =NULL ; +struct btrfs_workqueue *scrub_parity =NULL ; + + scrub_workers = fs_info->scrub_workers; + scrub_wr_comp = fs_info->scrub_wr_completion_workers; + scrub_parity = fs_info->scrub_parity_workers; + + fs_info->scrub_workers =NULL ; + fs_info->scrub_wr_completion_workers =NULL ; + fs_info->scrub_parity_workers =NULL ; +mutex_unlock (&fs_info->scrub_lock); + +btrfs_destroy_workqueue (scrub_workers); +btrfs_destroy_workqueue (scrub_wr_comp); +btrfs_destroy_workqueue (scrub_parity); + } +} +/* * get a reference count on fs_info->scrub_workers. start worker if necessary */ static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,int is_dev_replace) { +struct btrfs_workqueue *scrub_workers =NULL ; +struct btrfs_workqueue *scrub_wr_comp =NULL ; +struct btrfs_workqueue *scrub_parity =NULL ;unsigned int flags =WQ_FREEZABLE |WQ_UNBOUND ;int max_active = fs_info->thread_pool_size; +int ret = -ENOMEM ; -lockdep_assert_held (&fs_info->scrub_lock); +if (refcount_inc_not_zero (&fs_info->scrub_workers_refcnt)) +return 0 ; -if (refcount_read (&fs_info->scrub_workers_refcnt) ==0 ) { -ASSERT (fs_info->scrub_workers ==NULL ); - fs_info->scrub_workers =btrfs_alloc_workqueue (fs_info,"scrub" , - flags, is_dev_replace ?1 : max_active,4 ); -if (!fs_info->scrub_workers) - goto fail_scrub_workers; - -ASSERT (fs_info->scrub_wr_completion_workers ==NULL ); - fs_info->scrub_wr_completion_workers = -btrfs_alloc_workqueue (fs_info,"scrubwrc" , flags, - max_active,2 ); -if (!fs_info->scrub_wr_completion_workers) - goto fail_scrub_wr_completion_workers; + scrub_workers =btrfs_alloc_workqueue (fs_info,"scrub" , flags, + is_dev_replace ?1 : max_active, +4 ); +if (!scrub_workers) + goto fail_scrub_workers; -ASSERT (fs_info->scrub_parity_workers ==NULL ); - fs_info->scrub_parity_workers = -btrfs_alloc_workqueue (fs_info,"scrubparity" , flags, + scrub_wr_comp =btrfs_alloc_workqueue (fs_info,"scrubwrc" , flags, max_active,2 ); -if (!fs_info->scrub_parity_workers) - goto fail_scrub_parity_workers; +if (!scrub_wr_comp) + goto fail_scrub_wr_completion_workers; + scrub_parity =btrfs_alloc_workqueue (fs_info,"scrubparity" , flags, + max_active,2 ); +if (!scrub_parity) + goto fail_scrub_parity_workers; + +mutex_lock (&fs_info->scrub_lock); +if (refcount_read (&fs_info->scrub_workers_refcnt) ==0 ) { +ASSERT (fs_info->scrub_workers ==NULL && + fs_info->scrub_wr_completion_workers ==NULL && + fs_info->scrub_parity_workers ==NULL ); + fs_info->scrub_workers = scrub_workers; + fs_info->scrub_wr_completion_workers = scrub_wr_comp; + fs_info->scrub_parity_workers = scrub_parity;refcount_set (&fs_info->scrub_workers_refcnt,1 ); - }else { -refcount_inc (&fs_info->scrub_workers_refcnt); +mutex_unlock (&fs_info->scrub_lock); +return 0 ; } -return 0 ; +refcount_inc (&fs_info->scrub_workers_refcnt); +mutex_unlock (&fs_info->scrub_lock); + ret =0 ; +btrfs_destroy_workqueue (scrub_parity); fail_scrub_parity_workers: -btrfs_destroy_workqueue (fs_info->scrub_wr_completion_workers); +btrfs_destroy_workqueue (scrub_wr_comp); fail_scrub_wr_completion_workers: -btrfs_destroy_workqueue (fs_info->scrub_workers); +btrfs_destroy_workqueue (scrub_workers); fail_scrub_workers: -return -ENOMEM ; +return ret; } int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,@@ -3770,9 +3804,6 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,int ret;struct btrfs_device *dev;unsigned int nofs_flag; -struct btrfs_workqueue *scrub_workers =NULL ; -struct btrfs_workqueue *scrub_wr_comp =NULL ; -struct btrfs_workqueue *scrub_parity =NULL ;if (btrfs_fs_closing (fs_info))return -EAGAIN ;@@ -3819,13 +3850,17 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,if (IS_ERR (sctx))return PTR_ERR (sctx); + ret =scrub_workers_get (fs_info, is_dev_replace); +if (ret) + goto out_free_ctx; +mutex_lock (&fs_info->fs_devices->device_list_mutex); dev =btrfs_find_device (fs_info->fs_devices, devid,NULL ,NULL , true);if (!dev || (test_bit (BTRFS_DEV_STATE_MISSING , &dev->dev_state) && !is_dev_replace)) {mutex_unlock (&fs_info->fs_devices->device_list_mutex); ret = -ENODEV ; - goto out_free_ctx; + goto out; } if (!is_dev_replace && !readonly &&@@ -3834,7 +3869,7 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,btrfs_err_in_rcu (fs_info,"scrub: device %s is not writable" ,rcu_str_deref (dev->name)); ret = -EROFS ; - goto out_free_ctx; + goto out; }mutex_lock (&fs_info->scrub_lock);@@ -3843,7 +3878,7 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,mutex_unlock (&fs_info->scrub_lock);mutex_unlock (&fs_info->fs_devices->device_list_mutex); ret = -EIO ; - goto out_free_ctx; + goto out; }down_read (&fs_info->dev_replace.rwsem);@@ -3854,17 +3889,10 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,mutex_unlock (&fs_info->scrub_lock);mutex_unlock (&fs_info->fs_devices->device_list_mutex); ret = -EINPROGRESS ; - goto out_free_ctx; + goto out; }up_read (&fs_info->dev_replace.rwsem); - 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); - goto out_free_ctx; - } - sctx->readonly = readonly; dev->scrub_ctx = sctx;mutex_unlock (&fs_info->fs_devices->device_list_mutex);@@ -3917,24 +3945,14 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,mutex_lock (&fs_info->scrub_lock); dev->scrub_ctx =NULL ; -if (refcount_dec_and_test (&fs_info->scrub_workers_refcnt)) { - scrub_workers = fs_info->scrub_workers; - scrub_wr_comp = fs_info->scrub_wr_completion_workers; - scrub_parity = fs_info->scrub_parity_workers; - - fs_info->scrub_workers =NULL ; - fs_info->scrub_wr_completion_workers =NULL ; - fs_info->scrub_parity_workers =NULL ; - }mutex_unlock (&fs_info->scrub_lock); -btrfs_destroy_workqueue (scrub_workers); -btrfs_destroy_workqueue (scrub_wr_comp); -btrfs_destroy_workqueue (scrub_parity); +scrub_workers_put (fs_info);scrub_put_ctx (sctx);return ret; - +out: +scrub_workers_put (fs_info); out_free_ctx:scrub_free_ctx (sctx); --2.24.1
Signature · 3 lines
Filipe David Manana, “Whether you think you can, or you think you can't — you're right.”
PATCH 5/17 btrfs: set the correct lockdep class for new nodes
In reply to Josef BacikWhen flipping over to the rw_semaphore I noticed I'd get a lockdep splat in replace_path(), which is weird because we're swapping the reloc root with the actual target root. Turns out this is because we're using the root->root_key.objectid as the root id for the newly allocated tree block when setting the lockdep class, however we need to be using the actual owner of this new block, which is saved in owner.
--- fs/btrfs/extent-tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent-tree.c +1 -1
@@ -4522,7 +4522,7 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,return ERR_PTR (-EUCLEAN ); } -btrfs_set_buffer_lockdep_class (root->root_key.objectid, buf, level); +btrfs_set_buffer_lockdep_class (owner, buf, level);btrfs_tree_lock (buf);btrfs_clean_tree_block (buf);clear_bit (EXTENT_BUFFER_STALE , &buf->bflags);
Signature · 1 line
2.24.1
Re: [PATCH 05/17] btrfs: set the correct lockdep class for new nodes
Quoted 9 lines · Josef Bacik
When flipping over to the
rw_semaphoreI noticed I'd get a lockdep splat inreplace_path(), which is weird because we're swapping the reloc root with the actual target root. Turns out this is because we're using theroot->root_key.objectidas the root id for the newly allocated tree block when setting the lockdep class, however we need to be using the actual owner of this new block, which is saved in owner.Signed-off-by Josef Bacik <[email protected]>
Looks good, thanks.
Quoted 20 lines
--- fs/btrfs/extent-tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/btrfs/extent-tree.c +1
-2@@ -4522,7 +4522,7 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,return ERR_PTR (-EUCLEAN ); } -btrfs_set_buffer_lockdep_class (root->root_key.objectid, buf, level); +btrfs_set_buffer_lockdep_class (owner, buf, level);btrfs_tree_lock (buf);btrfs_clean_tree_block (buf);clear_bit (EXTENT_BUFFER_STALE , &buf->bflags); --2.24.1
Signature · 3 lines
Filipe David Manana, “Whether you think you can, or you think you can't — you're right.”
PATCH 6/17 btrfs: set the lockdep class for log tree extent buffers
In reply to Josef BacikThese are special extent buffers that get rewound in order to lookup the state of the tree at a specific point in time. As such they do not go through the normal initialization paths that set their lockdep class, so handle them appropriately when they are created and before they are locked.
--- fs/btrfs/ctree.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
fs/btrfs/ctree.c +5 -1
@@ -1297,6 +1297,8 @@ tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,btrfs_tree_read_unlock_blocking (eb);free_extent_buffer (eb); +btrfs_set_buffer_lockdep_class (btrfs_header_owner (eb_rewin), + eb_rewin,btrfs_header_level (eb_rewin));btrfs_tree_read_lock (eb_rewin);__tree_mod_log_rewind (fs_info, eb_rewin, time_seq, tm); WARN_ON(btrfs_header_nritems(eb_rewin) >
@@ -1370,7 +1372,6 @@ get_old_root(struct btrfs_root *root, u64 time_seq)if (!eb)return NULL ; -btrfs_tree_read_lock (eb);if (old_root) {btrfs_set_header_bytenr (eb, eb->start);btrfs_set_header_backref_rev (eb,BTRFS_MIXED_BACKREF_REV );
@@ -1378,6 +1379,9 @@ get_old_root(struct btrfs_root *root, u64 time_seq)btrfs_set_header_level (eb, old_root->level);btrfs_set_header_generation (eb, old_generation); } +btrfs_set_buffer_lockdep_class (btrfs_header_owner (eb), eb, +btrfs_header_level (eb)); +btrfs_tree_read_lock (eb);if (tm)__tree_mod_log_rewind (fs_info, eb, time_seq, tm);else
Signature · 1 line
2.24.1
Re: [PATCH 06/17] btrfs: set the lockdep class for log tree extent buffers
Quoted 8 lines · Josef Bacik
These are special extent buffers that get rewound in order to lookup the state of the tree at a specific point in time. As such they do not go through the normal initialization paths that set their lockdep class, so handle them appropriately when they are created and before they are locked.
Signed-off-by Josef Bacik <[email protected]>
Looks good, thanks.
Quoted 38 lines
--- fs/btrfs/ctree.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)fs/btrfs/ctree.c +5
-2@@ -1297,6 +1297,8 @@ tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,btrfs_tree_read_unlock_blocking (eb);free_extent_buffer (eb); +btrfs_set_buffer_lockdep_class (btrfs_header_owner (eb_rewin), + eb_rewin,btrfs_header_level (eb_rewin));btrfs_tree_read_lock (eb_rewin);__tree_mod_log_rewind (fs_info, eb_rewin, time_seq, tm); WARN_ON(btrfs_header_nritems(eb_rewin) >@@ -1370,7 +1372,6 @@ get_old_root(struct btrfs_root *root, u64 time_seq)if (!eb)return NULL ; -btrfs_tree_read_lock (eb);if (old_root) {btrfs_set_header_bytenr (eb, eb->start);btrfs_set_header_backref_rev (eb,BTRFS_MIXED_BACKREF_REV );@@ -1378,6 +1379,9 @@ get_old_root(struct btrfs_root *root, u64 time_seq)btrfs_set_header_level (eb, old_root->level);btrfs_set_header_generation (eb, old_generation); } +btrfs_set_buffer_lockdep_class (btrfs_header_owner (eb), eb, +btrfs_header_level (eb)); +btrfs_tree_read_lock (eb);if (tm)__tree_mod_log_rewind (fs_info, eb, time_seq, tm);else --2.24.1
Signature · 3 lines
Filipe David Manana, “Whether you think you can, or you think you can't — you're right.”
PATCH 7/17 btrfs: rename eb->lock_nested to eb->lock_recursed
In reply to Josef BacikNested locking with lockdep and everything else refers to lock hierarchy within the same lock map. This is how we indicate the same locks for different objects are ok to take in a specific order, for our use case that would be to take the lock on a leaf and then take a lock on an adjacent leaf.
What ->lock_nested _actually_ refers to is if we happen to already be holding the write lock on the extent buffer and we're allowing a read lock to be taken on that extent buffer, which is recursion. Rename this so we don't get confused when we switch to a rwsem and have to start using the _nested helpers.
--- fs/btrfs/extent_io.c | 2 +- fs/btrfs/extent_io.h | 2 +- fs/btrfs/locking.c | 22 +++++++++++----------- 3 files changed, 13 insertions(+), 13 deletions(-)
fs/btrfs/extent_io.c +1 -1
@@ -4988,7 +4988,7 @@ __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,rwlock_init (&eb->lock);atomic_set (&eb->blocking_readers,0 ); eb->blocking_writers =0 ; - eb->lock_nested = false; + eb->lock_recursed = false;init_waitqueue_head (&eb->write_lock_wq);init_waitqueue_head (&eb->read_lock_wq);
fs/btrfs/extent_io.h +1 -1
@@ -102,7 +102,7 @@ struct extent_buffer {int blocking_writers;atomic_t blocking_readers; -bool lock_nested; +bool lock_recursed;/* >= 0 if eb belongs to a log tree, -1 otherwise */ short log_index;
fs/btrfs/locking.c +11 -11
@@ -57,7 +57,7 @@* performance reasons. * * -* Lock nesting +* Lock recursion * ------------ * * A write operation on a tree might indirectly start a look up on the same
@@ -201,7 +201,7 @@ void btrfs_set_lock_blocking_read(struct extent_buffer *eb)* lock, but it won't change to or away from us. If we have the write * lock, we are the owner and it'll never change. */ -if (eb->lock_nested && current->pid == eb->lock_owner) +if (eb->lock_recursed && current->pid == eb->lock_owner)return ;btrfs_assert_tree_read_locked (eb);atomic_inc (&eb->blocking_readers);
@@ -225,7 +225,7 @@ void btrfs_set_lock_blocking_write(struct extent_buffer *eb)* lock, but it won't change to or away from us. If we have the write * lock, we are the owner and it'll never change. */ -if (eb->lock_nested && current->pid == eb->lock_owner) +if (eb->lock_recursed && current->pid == eb->lock_owner)return ;if (eb->blocking_writers ==0 ) {btrfs_assert_spinning_writers_put (eb);
@@ -263,8 +263,8 @@ void btrfs_tree_read_lock(struct extent_buffer *eb)* depends on this as it may be called on a partly * (write-)locked tree. */ -BUG_ON (eb->lock_nested); - eb->lock_nested = true; +BUG_ON (eb->lock_recursed); + eb->lock_recursed = true;read_unlock (&eb->lock);trace_btrfs_tree_read_lock (eb, start_ns);return ;
@@ -362,11 +362,11 @@ void btrfs_tree_read_unlock(struct extent_buffer *eb)/* * if we're nested, we have the write lock. No new locking * is needed as long as we are the lock owner. -* The write unlock will do a barrier for us, and the lock_nested +* The write unlock will do a barrier for us, and the lock_recursed * field only matters to the lock owner. */ -if (eb->lock_nested && current->pid == eb->lock_owner) { - eb->lock_nested = false; +if (eb->lock_recursed && current->pid == eb->lock_owner) { + eb->lock_recursed = false;return ; }btrfs_assert_tree_read_locked (eb);
@@ -388,11 +388,11 @@ void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb)/* * if we're nested, we have the write lock. No new locking * is needed as long as we are the lock owner. -* The write unlock will do a barrier for us, and the lock_nested +* The write unlock will do a barrier for us, and the lock_recursed * field only matters to the lock owner. */ -if (eb->lock_nested && current->pid == eb->lock_owner) { - eb->lock_nested = false; +if (eb->lock_recursed && current->pid == eb->lock_owner) { + eb->lock_recursed = false;return ; }btrfs_assert_tree_read_locked (eb);
Signature · 1 line
2.24.1
PATCH 8/17 btrfs: introduce path->recurse
In reply to Josef BacikOur current tree locking stuff allows us to recurse with read locks if we're already holding the write lock. This is necessary for the space cache inode, as we could be holding a lock on the root_tree root when we need to cache a block group, and thus need to be able to read down the root_tree to read in the inode cache.
We can get away with this in our current locking, but we won't be able to with a rwsem. Handle this by purposefully annotating the places where we require recursion, so that in the future we can maybe come up with a way to avoid the recursion. In the case of the free space inode, this will be superseded by the free space tree.
--- fs/btrfs/ctree.c | 8 ++++---- fs/btrfs/ctree.h | 3 +-- fs/btrfs/inode.c | 2 ++ fs/btrfs/locking.c | 13 ++++++++++--- fs/btrfs/locking.h | 10 ++++++++++ 5 files changed, 27 insertions(+), 9 deletions(-)
fs/btrfs/ctree.c +4 -4
@@ -2601,7 +2601,7 @@ static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,* We don't know the level of the root node until we actually * have it read locked */ - b =btrfs_read_lock_root_node (root); + b =__btrfs_read_lock_root_node (root, p->recurse); level =btrfs_header_level (b);if (level > write_lock_level) goto out;
@@ -2875,7 +2875,7 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root, }else {if (!btrfs_tree_read_lock_atomic (b)) {btrfs_set_path_blocking (p); -btrfs_tree_read_lock (b); +__btrfs_tree_read_lock (b, p->recurse); } p->locks[level] =BTRFS_READ_LOCK ; }
@@ -5379,7 +5379,7 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, }if (!ret) {btrfs_set_path_blocking (path); -btrfs_tree_read_lock (next); +__btrfs_tree_read_lock (next, path->recurse); } next_rw_lock =BTRFS_READ_LOCK ; }
@@ -5414,7 +5414,7 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, ret =btrfs_try_tree_read_lock (next);if (!ret) {btrfs_set_path_blocking (path); -btrfs_tree_read_lock (next); +__btrfs_tree_read_lock (next, path->recurse); } next_rw_lock =BTRFS_READ_LOCK ; }
fs/btrfs/ctree.h +1 -2
@@ -374,6 +374,7 @@ struct btrfs_path { unsigned int search_commit_root:1; unsigned int need_commit_sem:1; unsigned int skip_release_on_error:1; + unsigned int recurse:1; };#define BTRFS_MAX_EXTENT_ITEM_SIZE (r) ((BTRFS_LEAF_DATA_SIZE(r->fs_info) >> 4) - \ sizeof(struct btrfs_item))
@@ -2651,8 +2652,6 @@ void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,struct btrfs_path *path, const struct btrfs_key *new_key);struct extent_buffer *btrfs_root_node (struct btrfs_root *root); -struct extent_buffer *btrfs_lock_root_node (struct btrfs_root *root); -struct extent_buffer *btrfs_read_lock_root_node (struct btrfs_root *root);int btrfs_find_next_key (struct btrfs_root *root,struct btrfs_path *path,struct btrfs_key *key,int lowest_level,u64 min_trans);
fs/btrfs/inode.c +2 -0
@@ -6591,6 +6591,8 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,*/ path->leave_spinning =1 ; + path->recurse =btrfs_is_free_space_inode (inode); + ret =btrfs_lookup_file_extent (NULL , root, path, objectid, start,0 );if (ret <0 ) { err = ret;
fs/btrfs/locking.c +10 -3
@@ -244,7 +244,7 @@ void btrfs_set_lock_blocking_write(struct extent_buffer *eb)* * The rwlock is held upon exit. */ -void btrfs_tree_read_lock (struct extent_buffer *eb) +void __btrfs_tree_read_lock (struct extent_buffer *eb,bool recurse) {u64 start_ns =0 ;
@@ -263,6 +263,7 @@ void btrfs_tree_read_lock(struct extent_buffer *eb)* depends on this as it may be called on a partly * (write-)locked tree. */ +WARN_ON (!recurse);BUG_ON (eb->lock_recursed); eb->lock_recursed = true;read_unlock (&eb->lock);
@@ -279,6 +280,11 @@ void btrfs_tree_read_lock(struct extent_buffer *eb)trace_btrfs_tree_read_lock (eb, start_ns); } +void btrfs_tree_read_lock (struct extent_buffer *eb) +{ +__btrfs_tree_read_lock (eb, false); +} +/* * Lock extent buffer for read, optimistically expecting that there are no * contending blocking writers. If there are, don't wait.
@@ -552,13 +558,14 @@ struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)* * Return: root extent buffer with read lock held */ -struct extent_buffer *btrfs_read_lock_root_node (struct btrfs_root *root) +struct extent_buffer *__btrfs_read_lock_root_node (struct btrfs_root *root, +bool recurse) {struct extent_buffer *eb;while (1 ) { eb =btrfs_root_node (root); -btrfs_tree_read_lock (eb); +__btrfs_tree_read_lock (eb, recurse);if (eb == root->node)break ;btrfs_tree_read_unlock (eb);
fs/btrfs/locking.h +10 -0
@@ -21,6 +21,7 @@ struct btrfs_path;void btrfs_tree_lock (struct extent_buffer *eb);void btrfs_tree_unlock (struct extent_buffer *eb); +void __btrfs_tree_read_lock (struct extent_buffer *eb,bool recurse);void btrfs_tree_read_lock (struct extent_buffer *eb);void btrfs_tree_read_unlock (struct extent_buffer *eb);void btrfs_tree_read_unlock_blocking (struct extent_buffer *eb);
@@ -29,6 +30,15 @@ void btrfs_set_lock_blocking_write(struct extent_buffer *eb);int btrfs_try_tree_read_lock (struct extent_buffer *eb);int btrfs_try_tree_write_lock (struct extent_buffer *eb);int btrfs_tree_read_lock_atomic (struct extent_buffer *eb); +struct extent_buffer *btrfs_lock_root_node (struct btrfs_root *root); +struct extent_buffer *__btrfs_read_lock_root_node (struct btrfs_root *root, +bool recurse); + +static inline struct extent_buffer * +btrfs_read_lock_root_node (struct btrfs_root *root) +{ +return __btrfs_read_lock_root_node (root, false); +}#ifdef CONFIG_BTRFS_DEBUG static inline void btrfs_assert_tree_locked (struct extent_buffer *eb) {
Signature · 1 line
2.24.1
PATCH 9/17 btrfs: add nesting tags to the locking helpers
In reply to Josef BacikWe will need these when we switch to an rwsem, so plumb in the infrastructure here to use later on. I violate the 80 character limit some here because it'll be cleaned up later.
--- fs/btrfs/ctree.c | 11 ++++++++--- fs/btrfs/locking.c | 14 ++++++++++---- fs/btrfs/locking.h | 8 +++++++- 3 files changed, 25 insertions(+), 8 deletions(-)
fs/btrfs/ctree.c +8 -3
@@ -2875,7 +2875,8 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root, }else {if (!btrfs_tree_read_lock_atomic (b)) {btrfs_set_path_blocking (p); -__btrfs_tree_read_lock (b, p->recurse); +__btrfs_tree_read_lock (b,BTRFS_NESTING_NORMAL , + p->recurse); } p->locks[level] =BTRFS_READ_LOCK ; }
@@ -5379,7 +5380,9 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, }if (!ret) {btrfs_set_path_blocking (path); -__btrfs_tree_read_lock (next, path->recurse); +__btrfs_tree_read_lock (next, +BTRFS_NESTING_NORMAL , + path->recurse); } next_rw_lock =BTRFS_READ_LOCK ; }
@@ -5414,7 +5417,9 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, ret =btrfs_try_tree_read_lock (next);if (!ret) {btrfs_set_path_blocking (path); -__btrfs_tree_read_lock (next, path->recurse); +__btrfs_tree_read_lock (next, +BTRFS_NESTING_NORMAL , + path->recurse); } next_rw_lock =BTRFS_READ_LOCK ; }
fs/btrfs/locking.c +10 -4
@@ -244,7 +244,8 @@ void btrfs_set_lock_blocking_write(struct extent_buffer *eb)* * The rwlock is held upon exit. */ -void __btrfs_tree_read_lock (struct extent_buffer *eb,bool recurse) +void __btrfs_tree_read_lock (struct extent_buffer *eb,enum btrfs_lock_nesting nest, +bool recurse) {u64 start_ns =0 ;
@@ -282,7 +283,7 @@ void __btrfs_tree_read_lock(struct extent_buffer *eb, bool recurse)void btrfs_tree_read_lock (struct extent_buffer *eb) { -__btrfs_tree_read_lock (eb, false); +__btrfs_tree_read_lock (eb,BTRFS_NESTING_NORMAL , false); }/*
@@ -415,7 +416,7 @@ void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb)* * The rwlock is held for write upon exit. */ -void btrfs_tree_lock (struct extent_buffer *eb) +void __btrfs_tree_lock (struct extent_buffer *eb,enum btrfs_lock_nesting nest) __acquires(&eb->lock) {u64 start_ns =0 ;
@@ -440,6 +441,11 @@ void btrfs_tree_lock(struct extent_buffer *eb)trace_btrfs_tree_lock (eb, start_ns); } +void btrfs_tree_lock (struct extent_buffer *eb) +{ +__btrfs_tree_lock (eb,BTRFS_NESTING_NORMAL ); +} +/* * Release the write lock, either blocking or spinning (ie. there's no need * for an explicit blocking unlock, like btrfs_tree_read_unlock_blocking).
@@ -565,7 +571,7 @@ struct extent_buffer *__btrfs_read_lock_root_node(struct btrfs_root *root,while (1 ) { eb =btrfs_root_node (root); -__btrfs_tree_read_lock (eb, recurse); +__btrfs_tree_read_lock (eb,BTRFS_NESTING_NORMAL , recurse);if (eb == root->node)break ;btrfs_tree_read_unlock (eb);
fs/btrfs/locking.h +7 -1
@@ -16,12 +16,18 @@#define BTRFS_WRITE_LOCK_BLOCKING 3#define BTRFS_READ_LOCK_BLOCKING 4 +enum btrfs_lock_nesting { +BTRFS_NESTING_NORMAL =0 , +}; +struct btrfs_path ; +void __btrfs_tree_lock (struct extent_buffer *eb,enum btrfs_lock_nesting nest);void btrfs_tree_lock (struct extent_buffer *eb);void btrfs_tree_unlock (struct extent_buffer *eb); -void __btrfs_tree_read_lock (struct extent_buffer *eb,bool recurse); +void __btrfs_tree_read_lock (struct extent_buffer *eb,enum btrfs_lock_nesting nest, +bool recurse);void btrfs_tree_read_lock (struct extent_buffer *eb);void btrfs_tree_read_unlock (struct extent_buffer *eb);void btrfs_tree_read_unlock_blocking (struct extent_buffer *eb);
Signature · 1 line
2.24.1
Re: [PATCH 09/17] btrfs: add nesting tags to the locking helpers
fs/btrfs/locking.h line 20
Quoted 8 lines · Josef Bacik
fs/btrfs/locking.h +2
-0@@ -16,12 +16,18 @@#define BTRFS_WRITE_LOCK_BLOCKING 3#define BTRFS_READ_LOCK_BLOCKING 4 +enum btrfs_lock_nesting { +BTRFS_NESTING_NORMAL =0 ,
It should be safe to use the enum auto numbering, that starts from 0.
PATCH 10/17 btrfs: introduce BTRFS_NESTING_COW for cow'ing blocks
In reply to Josef BacikWhen we COW a block we are holding a lock on the original block, and then we lock the new COW block. Because our lockdep maps are based on root + level, this will make lockdep complain. We need a way to indicate a subclass for locking the COW'ed block, so plumb through our btrfs_lock_nesting from btrfs_cow_block down to the btrfs_init_buffer, and then introduce BTRFS_NESTING_COW to be used for cow'ing blocks.
The reason I've added all this extra infrastructure is because there will be need of different nesting classes in follow up patches.
--- fs/btrfs/ctree.c | 52 ++++++++++++++++++++++++++---------------- fs/btrfs/ctree.h | 6 +++-- fs/btrfs/disk-io.c | 5 ++-- fs/btrfs/extent-tree.c | 12 ++++++---- fs/btrfs/ioctl.c | 3 ++- fs/btrfs/locking.h | 8 +++++++ fs/btrfs/relocation.c | 11 +++++---- fs/btrfs/transaction.c | 5 ++-- 8 files changed, 66 insertions(+), 36 deletions(-)
fs/btrfs/ctree.c +32 -20
@@ -198,7 +198,7 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,btrfs_node_key (buf, &disk_key,0 ); cow =btrfs_alloc_tree_block (trans, root,0 , new_root_objectid, - &disk_key, level, buf->start,0 ); + &disk_key, level, buf->start,0 ,BTRFS_NESTING_NORMAL );if (IS_ERR (cow))return PTR_ERR (cow);
@@ -957,7 +957,8 @@ static struct extent_buffer *alloc_tree_block_no_bg_flush(const struct btrfs_disk_key *disk_key, int level, u64 hint, - u64 empty_size) + u64 empty_size, + enum btrfs_lock_nesting nest) { struct btrfs_fs_info *fs_info = root->fs_info;struct extent_buffer *ret;
@@ -986,7 +987,7 @@ static struct extent_buffer *alloc_tree_block_no_bg_flush( ret =btrfs_alloc_tree_block (trans, root, parent_start, root->root_key.objectid, disk_key, level, - hint, empty_size); + hint, empty_size, nest); trans->can_flush_pending_bgs = true;return ret;
@@ -1009,7 +1010,8 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,struct extent_buffer *buf, struct extent_buffer *parent, int parent_slot, struct extent_buffer **cow_ret, - u64 search_start, u64 empty_size) + u64 search_start, u64 empty_size, + enum btrfs_lock_nesting nest) { struct btrfs_fs_info *fs_info = root->fs_info;struct btrfs_disk_key disk_key;
@@ -1040,7 +1042,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, parent_start = parent->start; cow =alloc_tree_block_no_bg_flush (trans, root, parent_start, &disk_key, - level, search_start, empty_size); + level, search_start, empty_size, nest);if (IS_ERR (cow))return PTR_ERR (cow);
@@ -1446,7 +1448,8 @@ static inline int should_cow_block(struct btrfs_trans_handle *trans, noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,struct btrfs_root *root,struct extent_buffer *buf,struct extent_buffer *parent,int parent_slot, -struct extent_buffer **cow_ret) +struct extent_buffer **cow_ret, +enum btrfs_lock_nesting nest) {struct btrfs_fs_info *fs_info = root->fs_info;u64 search_start;
@@ -1485,7 +1488,7 @@ noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,*/ btrfs_qgroup_trace_subtree_after_cow (trans, root, buf); ret =__btrfs_cow_block (trans, root, buf, parent, - parent_slot, cow_ret, search_start,0 ); + parent_slot, cow_ret, search_start,0 , nest);trace_btrfs_cow_block (root, buf, *cow_ret);
@@ -1657,7 +1660,8 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans, err =__btrfs_cow_block (trans, root, cur, parent, i, &cur, search_start,min (16 * blocksize, - (end_slot - i) * blocksize)); + (end_slot - i) * blocksize), +BTRFS_NESTING_COW );if (err) {btrfs_tree_unlock (cur);free_extent_buffer (cur);
@@ -1855,7 +1859,8 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,btrfs_tree_lock (child);btrfs_set_lock_blocking_write (child); - ret =btrfs_cow_block (trans, root, child, mid,0 , &child); + ret =btrfs_cow_block (trans, root, child, mid,0 , &child, +BTRFS_NESTING_COW );if (ret) {btrfs_tree_unlock (child);free_extent_buffer (child);
@@ -1894,7 +1899,8 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,btrfs_tree_lock (left);btrfs_set_lock_blocking_write (left); wret =btrfs_cow_block (trans, root, left, - parent, pslot -1 , &left); + parent, pslot -1 , &left, +BTRFS_NESTING_COW );if (wret) { ret = wret; goto enospc;
@@ -1909,7 +1915,8 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,btrfs_tree_lock (right);btrfs_set_lock_blocking_write (right); wret =btrfs_cow_block (trans, root, right, - parent, pslot +1 , &right); + parent, pslot +1 , &right, +BTRFS_NESTING_COW );if (wret) { ret = wret; goto enospc;
@@ -2077,7 +2084,8 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, wret =1 ; }else { ret =btrfs_cow_block (trans, root, left, parent, - pslot -1 , &left); + pslot -1 , &left, +BTRFS_NESTING_COW );if (ret) wret =1 ;else {
@@ -2132,7 +2140,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, }else { ret =btrfs_cow_block (trans, root, right, parent, pslot +1 , - &right); + &right,BTRFS_NESTING_COW );if (ret) wret =1 ;else {
@@ -2740,11 +2748,13 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,btrfs_set_path_blocking (p);if (last_level) err =btrfs_cow_block (trans, root, b,NULL ,0 , - &b); + &b, +BTRFS_NESTING_COW );else err =btrfs_cow_block (trans, root, b, p->nodes[level +1 ], - p->slots[level +1 ], &b); + p->slots[level +1 ], &b, +BTRFS_NESTING_COW );if (err) { ret = err; goto done;
@@ -3332,7 +3342,8 @@ static noinline int insert_new_root(struct btrfs_trans_handle *trans,btrfs_node_key (lower, &lower_key,0 ); c =alloc_tree_block_no_bg_flush (trans, root,0 , &lower_key, level, - root->node->start,0 ); + root->node->start,0 , +BTRFS_NESTING_NORMAL );if (IS_ERR (c))return PTR_ERR (c);
@@ -3462,7 +3473,7 @@ static noinline int split_node(struct btrfs_trans_handle *trans,btrfs_node_key (c, &disk_key, mid); split =alloc_tree_block_no_bg_flush (trans, root,0 , &disk_key, level, - c->start,0 ); + c->start,0 ,BTRFS_NESTING_NORMAL );if (IS_ERR (split))return PTR_ERR (split);
@@ -3740,7 +3751,7 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root/* cow and double check */ ret =btrfs_cow_block (trans, root, right, upper, - slot +1 , &right); + slot +1 , &right,BTRFS_NESTING_COW );if (ret) goto out_unlock;
@@ -3975,7 +3986,8 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root/* cow and double check */ ret =btrfs_cow_block (trans, root, left, - path->nodes[1 ], slot -1 , &left); + path->nodes[1 ], slot -1 , &left, +BTRFS_NESTING_COW );if (ret) {/* we hit -ENOSPC, but it isn't fatal here */ if (ret == -ENOSPC )
@@ -4238,7 +4250,7 @@ static noinline int split_leaf(struct btrfs_trans_handle *trans,btrfs_item_key (l, &disk_key, mid); right =alloc_tree_block_no_bg_flush (trans, root,0 , &disk_key,0 , - l->start,0 ); + l->start,0 ,BTRFS_NESTING_NORMAL );if (IS_ERR (right))return PTR_ERR (right);
fs/btrfs/ctree.h +4 -2
@@ -2525,7 +2525,8 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, u64 parent, u64 root_objectid, const struct btrfs_disk_key *key, int level, u64 hint, - u64 empty_size); + u64 empty_size, + enum btrfs_lock_nesting nest); void btrfs_free_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct extent_buffer *buf,
@@ -2664,7 +2665,8 @@ struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,int btrfs_cow_block (struct btrfs_trans_handle *trans,struct btrfs_root *root,struct extent_buffer *buf,struct extent_buffer *parent,int parent_slot, -struct extent_buffer **cow_ret); +struct extent_buffer **cow_ret, +enum btrfs_lock_nesting nest); int btrfs_copy_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct extent_buffer *buf,
fs/btrfs/disk-io.c +3 -2
@@ -1209,7 +1209,8 @@ struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, root->root_key.type =BTRFS_ROOT_ITEM_KEY ; root->root_key.offset =0 ; - leaf =btrfs_alloc_tree_block (trans, root,0 , objectid,NULL ,0 ,0 ,0 ); + leaf =btrfs_alloc_tree_block (trans, root,0 , objectid,NULL ,0 ,0 ,0 , +BTRFS_NESTING_NORMAL );if (IS_ERR (leaf)) { ret =PTR_ERR (leaf); leaf =NULL ;
@@ -1281,7 +1282,7 @@ static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,*/ leaf =btrfs_alloc_tree_block (trans, root,0 ,BTRFS_TREE_LOG_OBJECTID , -NULL ,0 ,0 ,0 ); +NULL ,0 ,0 ,0 ,BTRFS_NESTING_NORMAL );if (IS_ERR (leaf)) {btrfs_put_root (root);return ERR_CAST (leaf);
fs/btrfs/extent-tree.c +7 -5
@@ -4500,7 +4500,8 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,static struct extent_buffer *btrfs_init_new_buffer (struct btrfs_trans_handle *trans,struct btrfs_root *root, -u64 bytenr,int level,u64 owner) +u64 bytenr,int level,u64 owner, +enum btrfs_lock_nesting nest) {struct btrfs_fs_info *fs_info = root->fs_info;struct extent_buffer *buf;
@@ -4523,7 +4524,7 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root, }btrfs_set_buffer_lockdep_class (owner, buf, level); -btrfs_tree_lock (buf); +__btrfs_tree_lock (buf, nest);btrfs_clean_tree_block (buf);clear_bit (EXTENT_BUFFER_STALE , &buf->bflags);
@@ -4569,7 +4570,8 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, u64 parent, u64 root_objectid, const struct btrfs_disk_key *key, int level, u64 hint, - u64 empty_size) + u64 empty_size, + enum btrfs_lock_nesting nest) { struct btrfs_fs_info *fs_info = root->fs_info;struct btrfs_key ins;
@@ -4585,7 +4587,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS if (btrfs_is_testing (fs_info)) { buf =btrfs_init_new_buffer (trans, root, root->alloc_bytenr, - level, root_objectid); + level, root_objectid, nest);if (!IS_ERR (buf)) root->alloc_bytenr += blocksize;return buf;
@@ -4602,7 +4604,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, goto out_unuse; buf =btrfs_init_new_buffer (trans, root, ins.objectid, level, - root_objectid); + root_objectid, nest);if (IS_ERR (buf)) { ret =PTR_ERR (buf); goto out_free_reserved;
fs/btrfs/ioctl.c +2 -1
@@ -628,7 +628,8 @@ static noinline int create_subvol(struct inode *dir,if (ret) goto fail; - leaf =btrfs_alloc_tree_block (trans, root,0 , objectid,NULL ,0 ,0 ,0 ); + leaf =btrfs_alloc_tree_block (trans, root,0 , objectid,NULL ,0 ,0 ,0 , +BTRFS_NESTING_NORMAL );if (IS_ERR (leaf)) { ret =PTR_ERR (leaf); goto fail;
fs/btrfs/locking.h +8 -0
@@ -18,6 +18,14 @@enum btrfs_lock_nesting {BTRFS_NESTING_NORMAL =0 , + +/* +* When we COW a block we are holding the lock on the original block, +* and since our lockdep maps are rootid+level, this confuses lockdep +* when we lock the newly allocated COW'd block. Handle this by having +* a subclass for COW'ed blocks so that lockdep doesn't complain. +*/ +BTRFS_NESTING_COW , };struct btrfs_path ;
fs/btrfs/relocation.c +7 -4
@@ -1206,7 +1206,8 @@ int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc, }if (cow) { - ret =btrfs_cow_block (trans, dest, eb,NULL ,0 , &eb); + ret =btrfs_cow_block (trans, dest, eb,NULL ,0 , &eb, +BTRFS_NESTING_COW );BUG_ON (ret); }btrfs_set_lock_blocking_write (eb);
@@ -1274,7 +1275,8 @@ int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc,btrfs_tree_lock (eb);if (cow) { ret =btrfs_cow_block (trans, dest, eb, parent, - slot, &eb); + slot, &eb, +BTRFS_NESTING_COW );BUG_ON (ret); }btrfs_set_lock_blocking_write (eb);
@@ -1781,7 +1783,8 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,* relocated and the block is tree root. */ leaf =btrfs_lock_root_node (root); - ret =btrfs_cow_block (trans, root, leaf,NULL ,0 , &leaf); + ret =btrfs_cow_block (trans, root, leaf,NULL ,0 , &leaf, +BTRFS_NESTING_COW );btrfs_tree_unlock (leaf);free_extent_buffer (leaf);if (ret <0 )
@@ -2308,7 +2311,7 @@ static int do_relocation(struct btrfs_trans_handle *trans,if (!node->eb) { ret =btrfs_cow_block (trans, root, eb, upper->eb, - slot, &eb); + slot, &eb,BTRFS_NESTING_COW );btrfs_tree_unlock (eb);free_extent_buffer (eb);if (ret <0 ) {
fs/btrfs/transaction.c +3 -2
@@ -1182,7 +1182,7 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans) eb =btrfs_lock_root_node (fs_info->tree_root); ret =btrfs_cow_block (trans, fs_info->tree_root, eb,NULL , -0 , &eb); +0 , &eb,BTRFS_NESTING_COW );btrfs_tree_unlock (eb);free_extent_buffer (eb);
@@ -1587,7 +1587,8 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,btrfs_set_root_otransid (new_root_item, trans->transid); old =btrfs_lock_root_node (root); - ret =btrfs_cow_block (trans, root, old,NULL ,0 , &old); + ret =btrfs_cow_block (trans, root, old,NULL ,0 , &old, +BTRFS_NESTING_COW );if (ret) {btrfs_tree_unlock (old);free_extent_buffer (old);
Signature · 1 line
2.24.1
PATCH 11/17 btrfs: introduce BTRFS_NESTING_LEFT/BTRFS_NESTING_RIGHT
In reply to Josef BacikOur lockdep maps are based on rootid+level, however in some cases we will lock adjacent blocks on the same level, namely in searching forward or in split/balance. Because of this lockdep will complain, so we need a separate subclass to indicate to lockdep that these are different locks.
lock leaf -> BTRFS_NESTING_NORMAL
cow leaf -> BTRFS_NESTING_COW
split leaf
lock left -> BTRFS_NESTING_LEFT
lock right -> BTRFS_NESTING_RIGHTThe above graph illustrates the need for this new nesting subclass.
--- fs/btrfs/ctree.c | 16 ++++++++-------- fs/btrfs/locking.h | 12 ++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-)
fs/btrfs/ctree.c +8 -8
@@ -1896,7 +1896,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans, left =NULL ;if (left) { -btrfs_tree_lock (left); +__btrfs_tree_lock (left,BTRFS_NESTING_LEFT );btrfs_set_lock_blocking_write (left); wret = btrfs_cow_block(trans, root, left, parent, pslot - 1, &left,
@@ -1912,7 +1912,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans, right =NULL ;if (right) { -btrfs_tree_lock (right); +__btrfs_tree_lock (right,BTRFS_NESTING_RIGHT );btrfs_set_lock_blocking_write (right); wret = btrfs_cow_block(trans, root, right, parent, pslot + 1, &right,
@@ -2076,7 +2076,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,if (left) {u32 left_nr; -btrfs_tree_lock (left); +__btrfs_tree_lock (left,BTRFS_NESTING_LEFT );btrfs_set_lock_blocking_write (left); left_nr =btrfs_header_nritems (left);
@@ -2131,7 +2131,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,if (right) {u32 right_nr; -btrfs_tree_lock (right); +__btrfs_tree_lock (right,BTRFS_NESTING_RIGHT );btrfs_set_lock_blocking_write (right); right_nr =btrfs_header_nritems (right);
@@ -3742,7 +3742,7 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_rootif (IS_ERR (right))return 1 ; -btrfs_tree_lock (right); +__btrfs_tree_lock (right,BTRFS_NESTING_RIGHT );btrfs_set_lock_blocking_write (right); free_space =btrfs_leaf_free_space (right);
@@ -3975,7 +3975,7 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_rootif (IS_ERR (left))return 1 ; -btrfs_tree_lock (left); +__btrfs_tree_lock (left,BTRFS_NESTING_LEFT );btrfs_set_lock_blocking_write (left); free_space =btrfs_leaf_free_space (left);
@@ -5393,7 +5393,7 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,if (!ret) {btrfs_set_path_blocking (path);__btrfs_tree_read_lock (next, -BTRFS_NESTING_NORMAL , +BTRFS_NESTING_RIGHT , path->recurse); } next_rw_lock =BTRFS_READ_LOCK ;
@@ -5430,7 +5430,7 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,if (!ret) {btrfs_set_path_blocking (path);__btrfs_tree_read_lock (next, -BTRFS_NESTING_NORMAL , +BTRFS_NESTING_RIGHT , path->recurse); } next_rw_lock =BTRFS_READ_LOCK ;
fs/btrfs/locking.h +12 -0
@@ -26,6 +26,18 @@ enum btrfs_lock_nesting {* a subclass for COW'ed blocks so that lockdep doesn't complain. */ BTRFS_NESTING_COW , + +/* +* Oftentimes we need to lock adjacent nodes on the same level while +* still holding the lock on the original node we searched to, such as +* for searching forward or for split/balance. +* +* Because of this we need to indicate to lockdep that this is +* acceptable by having a different subclass for each of these +* operations. +*/ +BTRFS_NESTING_LEFT , + BTRFS_NESTING_RIGHT, };struct btrfs_path ;
Signature · 1 line
2.24.1
PATCH 12/17 btrfs: introduce BTRFS_NESTING_LEFT/RIGHT_COW
In reply to Josef BacikFor similar reasons as BTRFS_NESTING_COW, we need BTRFS_NESTING_LEFT/RIGHT_COW. The pattern is this
lock leaf -> BTRFS_NESTING_NORMAL
cow leaf -> BTRFS_NESTING_COW
split leaf
lock left -> BTRFS_NESTING_LEFT
cow left -> BTRFS_NESTING_LEFT_COWWe need this in order to indicate to lockdep that these locks are discrete and are being taken in a safe order.
--- fs/btrfs/ctree.c | 12 ++++++------ fs/btrfs/locking.h | 8 ++++++++ 2 files changed, 14 insertions(+), 6 deletions(-)
fs/btrfs/ctree.c +6 -6
@@ -1900,7 +1900,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,btrfs_set_lock_blocking_write (left); wret =btrfs_cow_block (trans, root, left, parent, pslot -1 , &left, -BTRFS_NESTING_COW ); +BTRFS_NESTING_LEFT_COW );if (wret) { ret = wret; goto enospc;
@@ -1916,7 +1916,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,btrfs_set_lock_blocking_write (right); wret =btrfs_cow_block (trans, root, right, parent, pslot +1 , &right, -BTRFS_NESTING_COW ); +BTRFS_NESTING_RIGHT_COW );if (wret) { ret = wret; goto enospc;
@@ -2085,7 +2085,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, }else { ret =btrfs_cow_block (trans, root, left, parent, pslot -1 , &left, -BTRFS_NESTING_COW ); +BTRFS_NESTING_LEFT_COW );if (ret) wret =1 ;else {
@@ -2140,7 +2140,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, }else { ret =btrfs_cow_block (trans, root, right, parent, pslot +1 , - &right,BTRFS_NESTING_COW ); + &right,BTRFS_NESTING_RIGHT_COW );if (ret) wret =1 ;else {
@@ -3751,7 +3751,7 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root/* cow and double check */ ret =btrfs_cow_block (trans, root, right, upper, - slot +1 , &right,BTRFS_NESTING_COW ); + slot +1 , &right,BTRFS_NESTING_RIGHT_COW );if (ret) goto out_unlock;
@@ -3987,7 +3987,7 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root/* cow and double check */ ret =btrfs_cow_block (trans, root, left, path->nodes[1 ], slot -1 , &left, -BTRFS_NESTING_COW ); +BTRFS_NESTING_LEFT_COW );if (ret) {/* we hit -ENOSPC, but it isn't fatal here */ if (ret == -ENOSPC )
fs/btrfs/locking.h +8 -0
@@ -38,6 +38,14 @@ enum btrfs_lock_nesting {*/ BTRFS_NESTING_LEFT ,BTRFS_NESTING_RIGHT , + +/* +* When splitting we will be holding a lock on the left/right node when +* we need to cow that node, thus we need a new set of subclasses for +* these two operations. +*/ +BTRFS_NESTING_LEFT_COW , + BTRFS_NESTING_RIGHT_COW, };struct btrfs_path ;
Signature · 1 line
2.24.1
PATCH 13/17 btrfs: introduce BTRFS_NESTING_SPLIT for split blocks
In reply to Josef BacikIf we are splitting a leaf/node, we could do something like the following
lock(leaf) BTRFS_NESTING_NORMAL
lock(left) BTRFS_NESTING_LEFT + BTRFS_NESTING_COW
push from leaf -> left
reset path to point to left
split left
allocate new block, lock block BTRFS_NESTING_SPLIT
at the new block point we need to have a different nesting level, because we have already used either BTRFS_NESTING_LEFT or BTRFS_NESTING_RIGHT when pushing items from the original leaf into the adjacent leaves.
--- fs/btrfs/ctree.c | 4 ++-- fs/btrfs/locking.h | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-)
fs/btrfs/ctree.c +2 -2
@@ -3473,7 +3473,7 @@ static noinline int split_node(struct btrfs_trans_handle *trans,btrfs_node_key (c, &disk_key, mid); split =alloc_tree_block_no_bg_flush (trans, root,0 , &disk_key, level, - c->start,0 ,BTRFS_NESTING_NORMAL ); + c->start,0 ,BTRFS_NESTING_SPLIT );if (IS_ERR (split))return PTR_ERR (split);
@@ -4250,7 +4250,7 @@ static noinline int split_leaf(struct btrfs_trans_handle *trans,btrfs_item_key (l, &disk_key, mid); right =alloc_tree_block_no_bg_flush (trans, root,0 , &disk_key,0 , - l->start,0 ,BTRFS_NESTING_NORMAL ); + l->start,0 ,BTRFS_NESTING_SPLIT );if (IS_ERR (right))return PTR_ERR (right);
fs/btrfs/locking.h +9 -0
@@ -46,6 +46,15 @@ enum btrfs_lock_nesting {*/ BTRFS_NESTING_LEFT_COW ,BTRFS_NESTING_RIGHT_COW , + +/* +* When splitting we may push nodes to the left or right, but still use +* the subsequent nodes in our path, keeping our locks on those adjacent +* blocks. Thus when we go to allocate a new split block we've already +* used up all of our available subclasses, so this subclass exists to +* handle this case where we need to allocate a new split block. +*/ + BTRFS_NESTING_SPLIT, };struct btrfs_path ;
Signature · 1 line
2.24.1
PATCH 14/17 btrfs: introduce BTRFS_NESTING_NEW_ROOT for adding new roots
In reply to Josef BacikThe way we add new roots is confusing from a locking perspective for lockdep. We generally have the rule that we lock things in order from highest level to lowest, but in the case of adding a new level to the tree we actually allocate a new block for the root, which makes the locking go in reverse. A similar issue exists for snapshotting, we cow the original root for the root of a new tree, however they're at the same level. Address this by using BTRFS_NESTING_NEW_ROOT for these operations.
--- fs/btrfs/ctree.c | 5 +++-- fs/btrfs/locking.h | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-)
fs/btrfs/ctree.c +3 -2
@@ -198,7 +198,8 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,btrfs_node_key (buf, &disk_key,0 ); cow =btrfs_alloc_tree_block (trans, root,0 , new_root_objectid, - &disk_key, level, buf->start,0 ,BTRFS_NESTING_NORMAL ); + &disk_key, level, buf->start,0 , +BTRFS_NESTING_NEW_ROOT );if (IS_ERR (cow))return PTR_ERR (cow);
@@ -3343,7 +3344,7 @@ static noinline int insert_new_root(struct btrfs_trans_handle *trans, c =alloc_tree_block_no_bg_flush (trans, root,0 , &lower_key, level, root->node->start,0 , -BTRFS_NESTING_NORMAL ); +BTRFS_NESTING_NEW_ROOT );if (IS_ERR (c))return PTR_ERR (c);
fs/btrfs/locking.h +14 -0
@@ -16,6 +16,11 @@#define BTRFS_WRITE_LOCK_BLOCKING 3#define BTRFS_READ_LOCK_BLOCKING 4 +/* +* We are limited in number of subclasses by MAX_LOCKDEP_SUBCLASSES, which at +* the time of this patch is 8, which is how many we're using here. Keep this +* in mind if you decide you want to add another subclass. +*/ enum btrfs_lock_nesting {BTRFS_NESTING_NORMAL =0 ,
@@ -55,6 +60,15 @@ enum btrfs_lock_nesting {* handle this case where we need to allocate a new split block. */ BTRFS_NESTING_SPLIT , + +/* +* When promoting a new block to a root we need to have a special +* subclass so we don't confuse lockdep, as it will appear that we are +* locking a higher level node before a lower level one. Copying also +* has this problem as it appears we're locking the same block again +* when we make a snapshot of an existing root. +*/ + BTRFS_NESTING_NEW_ROOT, };struct btrfs_path ;
Signature · 1 line
2.24.1
Re: [PATCH 14/17] btrfs: introduce BTRFS_NESTING_NEW_ROOT for adding new roots
Quoted 18 lines · Josef Bacik
fs/btrfs/locking.h +5
-0@@ -16,6 +16,11 @@#define BTRFS_WRITE_LOCK_BLOCKING 3#define BTRFS_READ_LOCK_BLOCKING 4 +/* +* We are limited in number of subclasses by MAX_LOCKDEP_SUBCLASSES, which at +* the time of this patch is 8, which is how many we're using here. Keep this +* in mind if you decide you want to add another subclass. +*/ enum btrfs_lock_nesting {BTRFS_NESTING_NORMAL =0 ,@@ -55,6 +60,15 @@ enum btrfs_lock_nesting {* handle this case where we need to allocate a new split block. */ BTRFS_NESTING_SPLIT,
fs/btrfs/locking.h enum btrfs_lock_nesting { · line 72
Quoted 10 lines
fs/btrfs/locking.h +9
-0+ +/* +* When promoting a new block to a root we need to have a special +* subclass so we don't confuse lockdep, as it will appear that we are +* locking a higher level node before a lower level one. Copying also +* has this problem as it appears we're locking the same block again +* when we make a snapshot of an existing root. +*/ + BTRFS_NESTING_NEW_ROOT, };
To prevent accidental addition (and not reading the comment about the maximum count), I suggest to use the static_assert with one extra definition like
fs/btrfs/locking.h +11 -5
@@ -14,11 +14,6 @@#define BTRFS_WRITE_LOCK 1#define BTRFS_READ_LOCK 2 -/* -* We are limited in number of subclasses by MAX_LOCKDEP_SUBCLASSES, which at -* the time of this patch is 8, which is how many we're using here. Keep this -* in mind if you decide you want to add another subclass. -*/ enum btrfs_lock_nesting {BTRFS_NESTING_NORMAL =0 ,
@@ -67,8 +62,19 @@ enum btrfs_lock_nesting {* when we make a snapshot of an existing root. */ BTRFS_NESTING_NEW_ROOT , + +/* +* We are limited in number of subclasses by MAX_LOCKDEP_SUBCLASSES, +* which at the time of this patch is 8, which is how many we're using +* here. Keep this in mind if you decide you want to add another +* subclass. +*/ + BTRFS_NESTING_MAX }; +static_assert (BTRFS_NESTING_MAX <=MAX_LOCKDEP_SUBCLASSES , +"too many lock subclasses defined" ); +struct btrfs_path ;void __btrfs_tree_lock (struct extent_buffer *eb,enum btrfs_lock_nesting nest);
---
PATCH 15/17 btrfs: change our extent buffer lock to a rw_semaphore
In reply to Josef BacikHistorically we've implemented our own locking because we wanted to be able to selectively spin or sleep based on what we were doing in the tree. For instance, if all of our nodes were in cache then there's rarely a reason to need to sleep waiting for node locks, as they'll likely become available soon. At the time this code was written the rw_semaphore didn't do adaptive spinning, and thus was orders of magnitude slower than our home grown locking.
However now the opposite is the case. There are a few problems with how we implement blocking locks, namely that we use a normal waitqueue and simply wake everybody up in reverse sleep order. This leads to some suboptimal performance behavior, and a lot of context switches in highly contended cases. rw_semaphores actually do this properly, and also have adaptive spinning that works relatively well.
The locking code is also a bit of a bear to understand, and we lose the benefit of lockdep for the most part because the blocking states of the lock are simply ad-hoc and not mapped into lockdep.
So rework the locking code to drop all of this custom locking stuff, and simply use a rw_semaphore for everything. This makes the locking much simpler for everything, as we can now drop a lot of cruft and blocking transitions. The performance numbers vary depending on the workload, because generally speaking there doesn't tend to be a lot of contention on the btree. However on my test system which is an 80 core single socket system with 256gib of RAM and a 2tib NVME drive I get the following results (with all debug options off)
dbench 200 baseline
Throughput 216.056 MB/sec 200 clients 200 procs max_latency=1471.197 ms
dbench 200 with patch
Throughput 737.188 MB/sec 200 clients 200 procs max_latency=714.346 ms
Previously we also used fs_mark to test this sort of contention, and those results are far less impressive, mostly because there's not enough tasks to really stress the locking
fs_mark -d /d[0-15] -S 0 -L 20 -n 100000 -s 0 -t 16
baseline
verage Files/sec: 160166.7
p50 Files/sec: 165832
p90 Files/sec: 123886
p99 Files/sec: 123495
real 3m26.527s user 2m19.223s sys 48m21.856s
patched
Average Files/sec: 164135.7
p50 Files/sec: 171095
p90 Files/sec: 122889
p99 Files/sec: 113819
real 3m29.660s user 2m19.990s sys 44m12.259s
--- fs/btrfs/extent_io.c | 13 +- fs/btrfs/extent_io.h | 21 +-- fs/btrfs/locking.c | 378 +++++++----------------------------------- fs/btrfs/locking.h | 2 +- fs/btrfs/print-tree.c | 11 +- 5 files changed, 67 insertions(+), 358 deletions(-)
fs/btrfs/extent_io.c +1 -12
@@ -4985,12 +4985,8 @@ __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, eb->len = len; eb->fs_info = fs_info; eb->bflags =0 ; -rwlock_init (&eb->lock); -atomic_set (&eb->blocking_readers,0 ); - eb->blocking_writers =0 ; +init_rwsem (&eb->lock); eb->lock_recursed = false; -init_waitqueue_head (&eb->write_lock_wq); -init_waitqueue_head (&eb->read_lock_wq);btrfs_leak_debug_add (&fs_info->eb_leak_lock, &eb->leak_list, &fs_info->allocated_ebs);
@@ -5006,13 +5002,6 @@ __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, > MAX_INLINE_EXTENT_BUFFER_SIZE);BUG_ON (len >MAX_INLINE_EXTENT_BUFFER_SIZE ); -#ifdef CONFIG_BTRFS_DEBUG - eb->spinning_writers =0 ; -atomic_set (&eb->spinning_readers,0 ); -atomic_set (&eb->read_locks,0 ); - eb->write_locks =0 ; -#endif -return eb; }
fs/btrfs/extent_io.h +2 -19
@@ -99,31 +99,14 @@ struct extent_buffer {int read_mirror;struct rcu_head rcu_head;pid_t lock_owner; - -int blocking_writers; -atomic_t blocking_readers;bool lock_recursed; +struct rw_semaphore lock; +/* >= 0 if eb belongs to a log tree, -1 otherwise */ short log_index; -/* protects write locks */ -rwlock_t lock; - -/* readers use lock_wq while they wait for the write -* lock holders to unlock -*/ -wait_queue_head_t write_lock_wq; - -/* writers use read_lock_wq while they wait for readers -* to unlock -*/ -wait_queue_head_t read_lock_wq;struct page *pages[INLINE_EXTENT_BUFFER_PAGES ];#ifdef CONFIG_BTRFS_DEBUG -int spinning_writers; -atomic_t spinning_readers; -atomic_t read_locks; -int write_locks;struct list_head leak_list;#endif };
fs/btrfs/locking.c +61 -317
477 lines not on this page · raw message
fs/btrfs/locking.h +1 -1
@@ -99,7 +99,7 @@ btrfs_read_lock_root_node(struct btrfs_root *root)#ifdef CONFIG_BTRFS_DEBUG static inline void btrfs_assert_tree_locked (struct extent_buffer *eb) { -BUG_ON (!eb->write_locks); +lockdep_assert_held (&eb->lock); }#else static inline void btrfs_assert_tree_locked (struct extent_buffer *eb) { }
fs/btrfs/print-tree.c +2 -9
@@ -151,15 +151,8 @@ static void print_uuid_item(struct extent_buffer *l, unsigned long offset,static void print_eb_refs_lock (struct extent_buffer *eb) {#ifdef CONFIG_BTRFS_DEBUG -btrfs_info (eb->fs_info, -"refs %u lock (w:%d r:%d bw:%d br:%d sw:%d sr:%d) lock_owner %u current %u" , -atomic_read (&eb->refs), eb->write_locks, -atomic_read (&eb->read_locks), - eb->blocking_writers, -atomic_read (&eb->blocking_readers), - eb->spinning_writers, -atomic_read (&eb->spinning_readers), - eb->lock_owner, current->pid); +btrfs_info (eb->fs_info,"refs %u lock_owner %u current %u" , +atomic_read (&eb->refs), eb->lock_owner, current->pid);#endif }
Signature · 1 line
2.24.1
PATCH 16/17 btrfs: remove all of the blocking helpers
In reply to Josef BacikNow that we're using a rw_semaphore we no longer need to indicate if a lock is blocking or not, nor do we need to flip the entire path from blocking to spinning. Remove these helpers and all the places they are called.
--- fs/btrfs/backref.c | 10 ++--- fs/btrfs/ctree.c | 91 ++++++---------------------------------- fs/btrfs/delayed-inode.c | 7 ---- fs/btrfs/disk-io.c | 8 +--- fs/btrfs/extent-tree.c | 19 +++------ fs/btrfs/file.c | 3 +- fs/btrfs/inode.c | 1 - fs/btrfs/locking.c | 74 -------------------------------- fs/btrfs/locking.h | 11 +---- fs/btrfs/qgroup.c | 9 ++-- fs/btrfs/ref-verify.c | 6 +-- fs/btrfs/relocation.c | 4 -- fs/btrfs/transaction.c | 2 - fs/btrfs/tree-defrag.c | 1 - fs/btrfs/tree-log.c | 3 -- 15 files changed, 30 insertions(+), 219 deletions(-)
fs/btrfs/backref.c +3 -7
@@ -1330,14 +1330,12 @@ static int find_parent_nodes(struct btrfs_trans_handle *trans, goto out; } -if (!path->skip_locking) { +if (!path->skip_locking)btrfs_tree_read_lock (eb); -btrfs_set_lock_blocking_read (eb); - } ret =find_extent_in_eb (eb, bytenr, *extent_item_pos, &eie, ignore_offset);if (!path->skip_locking) -btrfs_tree_read_unlock_blocking (eb); +btrfs_tree_read_unlock (eb);free_extent_buffer (eb);if (ret <0 ) goto out;
@@ -1674,7 +1672,7 @@ char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, name_off, name_len);if (eb != eb_in) {if (!path->skip_locking) -btrfs_tree_read_unlock_blocking (eb); +btrfs_tree_read_unlock (eb);free_extent_buffer (eb); } ret = btrfs_find_item(fs_root, path, parent, 0,
@@ -1694,8 +1692,6 @@ char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, eb = path->nodes[0 ];/* make sure we can use eb after releasing the path */ if (eb != eb_in) { -if (!path->skip_locking) -btrfs_set_lock_blocking_read (eb); path->nodes[0 ] =NULL ; path->locks[0 ] =0 ; }
fs/btrfs/ctree.c +12 -79
325 lines not on this page · raw message
fs/btrfs/delayed-inode.c +0 -7
@@ -741,13 +741,6 @@ static int btrfs_batch_insert_items(struct btrfs_root *root, goto out; } -/* -* we need allocate some memory space, but it might cause the task -* to sleep, so we set all locked nodes in the path to blocking locks -* first. -*/ -btrfs_set_path_blocking (path); - keys =kmalloc_array (nitems,sizeof (struct btrfs_key ),GFP_NOFS );if (!keys) { ret = -ENOMEM ;
fs/btrfs/disk-io.c +2 -6
@@ -296,10 +296,8 @@ static int verify_parent_transid(struct extent_io_tree *io_tree,if (atomic)return -EAGAIN ; -if (need_lock) { +if (need_lock)btrfs_tree_read_lock (eb); -btrfs_set_lock_blocking_read (eb); - }lock_extent_bits (io_tree, eb->start, eb->start + eb->len -1 , &cached_state);
@@ -328,7 +326,7 @@ static int verify_parent_transid(struct extent_io_tree *io_tree,unlock_extent_cached (io_tree, eb->start, eb->start + eb->len -1 , &cached_state);if (need_lock) -btrfs_tree_read_unlock_blocking (eb); +btrfs_tree_read_unlock (eb);return ret; }
@@ -1071,8 +1069,6 @@ void btrfs_clean_tree_block(struct extent_buffer *buf)percpu_counter_add_batch (&fs_info->dirty_metadata_bytes, -buf->len, fs_info->dirty_metadata_batch); -/* ugh, clear_extent_buffer_dirty needs to lock the page */ -btrfs_set_lock_blocking_write (buf);clear_extent_buffer_dirty (buf); } }
fs/btrfs/extent-tree.c +6 -13
@@ -4528,7 +4528,6 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,btrfs_clean_tree_block (buf);clear_bit (EXTENT_BUFFER_STALE , &buf->bflags); -btrfs_set_lock_blocking_write (buf);set_extent_buffer_uptodate (buf);memzero_extent_buffer (buf,0 ,sizeof (struct btrfs_header ));
@@ -4917,7 +4916,6 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans, reada =1 ; }btrfs_tree_lock (next); -btrfs_set_lock_blocking_write (next); ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1, &wc->refs[level - 1],
@@ -4977,7 +4975,6 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,return -EIO ; }btrfs_tree_lock (next); -btrfs_set_lock_blocking_write (next); } level--;
@@ -4989,7 +4986,7 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans, } path->nodes[level] = next; path->slots[level] =0 ; - path->locks[level] =BTRFS_WRITE_LOCK_BLOCKING ; + path->locks[level] =BTRFS_WRITE_LOCK ; wc->level = level;if (wc->level ==1 ) wc->reada_slot =0 ;
@@ -5117,8 +5114,7 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,if (!path->locks[level]) {BUG_ON (level ==0 );btrfs_tree_lock (eb); -btrfs_set_lock_blocking_write (eb); - path->locks[level] =BTRFS_WRITE_LOCK_BLOCKING ; + path->locks[level] =BTRFS_WRITE_LOCK ; ret = btrfs_lookup_extent_info(trans, fs_info, eb->start, level, 1,
@@ -5161,8 +5157,7 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,if (!path->locks[level] &&btrfs_header_generation (eb) == trans->transid) {btrfs_tree_lock (eb); -btrfs_set_lock_blocking_write (eb); - path->locks[level] =BTRFS_WRITE_LOCK_BLOCKING ; + path->locks[level] =BTRFS_WRITE_LOCK ; }btrfs_clean_tree_block (eb); }
@@ -5330,9 +5325,8 @@ int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref, int for_reloc)if (btrfs_disk_key_objectid (&root_item->drop_progress) ==0 ) { level =btrfs_header_level (root->node); path->nodes[level] =btrfs_lock_root_node (root); -btrfs_set_lock_blocking_write (path->nodes[level]); path->slots[level] =0 ; - path->locks[level] =BTRFS_WRITE_LOCK_BLOCKING ; + path->locks[level] =BTRFS_WRITE_LOCK ;memset (&wc->update_progress,0 ,sizeof (wc->update_progress)); }else {
@@ -5360,8 +5354,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref, int for_reloc) level =btrfs_header_level (root->node);while (1 ) {btrfs_tree_lock (path->nodes[level]); -btrfs_set_lock_blocking_write (path->nodes[level]); - path->locks[level] =BTRFS_WRITE_LOCK_BLOCKING ; + path->locks[level] =BTRFS_WRITE_LOCK ; ret = btrfs_lookup_extent_info(trans, fs_info, path->nodes[level]->start,
@@ -5548,7 +5541,7 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans, level =btrfs_header_level (node); path->nodes[level] = node; path->slots[level] =0 ; - path->locks[level] =BTRFS_WRITE_LOCK_BLOCKING ; + path->locks[level] =BTRFS_WRITE_LOCK ; wc->refs[parent_level] =1 ; wc->flags[parent_level] =BTRFS_BLOCK_FLAG_FULL_BACKREF ;
fs/btrfs/file.c +1 -2
@@ -1042,8 +1042,7 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans,* write lock. */ if (!ret && replace_extent && leafs_visited ==1 && - (path->locks[0 ] ==BTRFS_WRITE_LOCK_BLOCKING || - path->locks[0 ] ==BTRFS_WRITE_LOCK ) && + path->locks[0 ] ==BTRFS_WRITE_LOCK &&btrfs_leaf_free_space (leaf) >=sizeof (struct btrfs_item ) + extent_item_size) {
fs/btrfs/inode.c +0 -1
@@ -6694,7 +6694,6 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, em->orig_start = em->start; ptr =btrfs_file_extent_inline_start (item) + extent_offset; -btrfs_set_path_blocking (path);if (!PageUptodate (page)) {if (btrfs_file_extent_compression (leaf, item) !=BTRFS_COMPRESS_NONE ) {
fs/btrfs/locking.c +0 -74
98 lines not on this page · raw message
fs/btrfs/locking.h +1 -10
@@ -13,8 +13,6 @@#define BTRFS_WRITE_LOCK 1#define BTRFS_READ_LOCK 2 -#define BTRFS_WRITE_LOCK_BLOCKING 3 -#define BTRFS_READ_LOCK_BLOCKING 4/* * We are limited in number of subclasses by MAX_LOCKDEP_SUBCLASSES, which at
@@ -81,12 +79,8 @@ void __btrfs_tree_read_lock(struct extent_buffer *eb, enum btrfs_lock_nesting ne bool recurse);void btrfs_tree_read_lock (struct extent_buffer *eb);void btrfs_tree_read_unlock (struct extent_buffer *eb); -void btrfs_tree_read_unlock_blocking (struct extent_buffer *eb); -void btrfs_set_lock_blocking_read (struct extent_buffer *eb); -void btrfs_set_lock_blocking_write (struct extent_buffer *eb);int btrfs_try_tree_read_lock (struct extent_buffer *eb);int btrfs_try_tree_write_lock (struct extent_buffer *eb); -int btrfs_tree_read_lock_atomic (struct extent_buffer *eb);struct extent_buffer *btrfs_lock_root_node (struct btrfs_root *root);struct extent_buffer *__btrfs_read_lock_root_node (struct btrfs_root *root,bool recurse);
@@ -105,15 +99,12 @@ static inline void btrfs_assert_tree_locked(struct extent_buffer *eb) {static inline void btrfs_assert_tree_locked (struct extent_buffer *eb) { }#endif -void btrfs_set_path_blocking (struct btrfs_path *p);void btrfs_unlock_up_safe (struct btrfs_path *path,int level);static inline void btrfs_tree_unlock_rw (struct extent_buffer *eb,int rw) { -if (rw ==BTRFS_WRITE_LOCK || rw ==BTRFS_WRITE_LOCK_BLOCKING ) +if (rw ==BTRFS_WRITE_LOCK )btrfs_tree_unlock (eb); -else if (rw ==BTRFS_READ_LOCK_BLOCKING ) -btrfs_tree_read_unlock_blocking (eb);else if (rw ==BTRFS_READ_LOCK )btrfs_tree_read_unlock (eb);else
fs/btrfs/qgroup.c +3 -6
@@ -1902,8 +1902,7 @@ static int qgroup_trace_extent_swap(struct btrfs_trans_handle* trans, src_path->nodes[cur_level] = eb;btrfs_tree_read_lock (eb); -btrfs_set_lock_blocking_read (eb); - src_path->locks[cur_level] =BTRFS_READ_LOCK_BLOCKING ; + src_path->locks[cur_level] =BTRFS_READ_LOCK ; } src_path->slots[cur_level] = dst_path->slots[cur_level];
@@ -2043,8 +2042,7 @@ static int qgroup_trace_new_subtree_blocks(struct btrfs_trans_handle* trans, dst_path->slots[cur_level] =0 ;btrfs_tree_read_lock (eb); -btrfs_set_lock_blocking_read (eb); - dst_path->locks[cur_level] =BTRFS_READ_LOCK_BLOCKING ; + dst_path->locks[cur_level] =BTRFS_READ_LOCK ; need_cleanup = true; }
@@ -2218,8 +2216,7 @@ int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans, path->slots[level] =0 ;btrfs_tree_read_lock (eb); -btrfs_set_lock_blocking_read (eb); - path->locks[level] =BTRFS_READ_LOCK_BLOCKING ; + path->locks[level] =BTRFS_READ_LOCK ; ret = btrfs_qgroup_trace_extent(trans, child_bytenr, fs_info->nodesize,
fs/btrfs/ref-verify.c +2 -4
@@ -575,10 +575,9 @@ static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,return -EIO ; }btrfs_tree_read_lock (eb); -btrfs_set_lock_blocking_read (eb); path->nodes[level-1 ] = eb; path->slots[level-1 ] =0 ; - path->locks[level-1 ] =BTRFS_READ_LOCK_BLOCKING ; + path->locks[level-1 ] =BTRFS_READ_LOCK ; }else { ret =process_leaf (root, path, bytenr, num_bytes);if (ret)
@@ -1000,11 +999,10 @@ int btrfs_build_ref_tree(struct btrfs_fs_info *fs_info)return -ENOMEM ; eb =btrfs_read_lock_root_node (fs_info->extent_root); -btrfs_set_lock_blocking_read (eb); level =btrfs_header_level (eb); path->nodes[level] = eb; path->slots[level] =0 ; - path->locks[level] =BTRFS_READ_LOCK_BLOCKING ; + path->locks[level] =BTRFS_READ_LOCK ;while (1 ) {/*
fs/btrfs/relocation.c +0 -4
@@ -1196,7 +1196,6 @@ int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc,btrfs_node_key_to_cpu (path->nodes[lowest_level], &key, slot); eb =btrfs_lock_root_node (dest); -btrfs_set_lock_blocking_write (eb); level =btrfs_header_level (eb);if (level < lowest_level) {
@@ -1210,7 +1209,6 @@ int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc, BTRFS_NESTING_COW);BUG_ON (ret); } -btrfs_set_lock_blocking_write (eb);if (next_key) { next_key->objectid = (u64)-1 ;
@@ -1279,7 +1277,6 @@ int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc, BTRFS_NESTING_COW);BUG_ON (ret); } -btrfs_set_lock_blocking_write (eb);btrfs_tree_unlock (parent);free_extent_buffer (parent);
@@ -2307,7 +2304,6 @@ static int do_relocation(struct btrfs_trans_handle *trans, goto next; }btrfs_tree_lock (eb); -btrfs_set_lock_blocking_write (eb);if (!node->eb) { ret = btrfs_cow_block(trans, root, eb, upper->eb,
fs/btrfs/transaction.c +0 -2
@@ -1596,8 +1596,6 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, goto fail; } -btrfs_set_lock_blocking_write (old); - ret =btrfs_copy_root (trans, root, old, &tmp, objectid);/* clean up in any case */ btrfs_tree_unlock (old);
fs/btrfs/tree-defrag.c +0 -1
@@ -52,7 +52,6 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,u32 nritems; root_node =btrfs_lock_root_node (root); -btrfs_set_lock_blocking_write (root_node); nritems =btrfs_header_nritems (root_node); root->defrag_max.objectid =0 ;/* from above we know this is not a leaf */
fs/btrfs/tree-log.c +0 -3
@@ -2736,7 +2736,6 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,if (trans) {btrfs_tree_lock (next); -btrfs_set_lock_blocking_write (next);btrfs_clean_tree_block (next);btrfs_wait_tree_block_writeback (next);btrfs_tree_unlock (next);
@@ -2805,7 +2804,6 @@ static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,if (trans) {btrfs_tree_lock (next); -btrfs_set_lock_blocking_write (next);btrfs_clean_tree_block (next);btrfs_wait_tree_block_writeback (next);btrfs_tree_unlock (next);
@@ -2887,7 +2885,6 @@ static int walk_log_tree(struct btrfs_trans_handle *trans,if (trans) {btrfs_tree_lock (next); -btrfs_set_lock_blocking_write (next);btrfs_clean_tree_block (next);btrfs_wait_tree_block_writeback (next);btrfs_tree_unlock (next);
Signature · 1 line
2.24.1
PATCH 17/17 btrfs: rip out path->leave_spinning
In reply to Josef BacikWe no longer distinguish between blocking and spinning, so rip out all of this code.
--- fs/btrfs/backref.c | 3 --- fs/btrfs/ctree.c | 3 --- fs/btrfs/ctree.h | 1 - fs/btrfs/delayed-inode.c | 4 ---- fs/btrfs/dir-item.c | 1 - fs/btrfs/export.c | 1 - fs/btrfs/extent-tree.c | 8 -------- fs/btrfs/extent_io.c | 1 - fs/btrfs/file-item.c | 4 ---- fs/btrfs/free-space-tree.c | 2 -- fs/btrfs/inode-item.c | 6 ------ fs/btrfs/inode.c | 12 ------------ fs/btrfs/ioctl.c | 1 - fs/btrfs/qgroup.c | 2 -- fs/btrfs/reflink.c | 3 --- fs/btrfs/super.c | 2 -- fs/btrfs/tests/qgroup-tests.c | 4 ---- 17 files changed, 58 deletions(-)
fs/btrfs/backref.c +0 -3
@@ -1658,13 +1658,11 @@ char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,s64 bytes_left = ((s64 )size) -1 ;struct extent_buffer *eb = eb_in;struct btrfs_key found_key; -int leave_spinning = path->leave_spinning;struct btrfs_inode_ref *iref;if (bytes_left >=0 ) dest[bytes_left] ='\0' ; - path->leave_spinning =1 ;while (1 ) { bytes_left -= name_len;if (bytes_left >=0 )
@@ -1708,7 +1706,6 @@ char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, }btrfs_release_path (path); - path->leave_spinning = leave_spinning;if (ret)return ERR_PTR (ret);
fs/btrfs/ctree.c +0 -3
@@ -5227,7 +5227,6 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,struct btrfs_key key;u32 nritems;int ret; -int old_spinning = path->leave_spinning;int next_rw_lock =0 ; nritems =btrfs_header_nritems (path->nodes[0 ]);
@@ -5242,7 +5241,6 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,btrfs_release_path (path); path->keep_locks =1 ; - path->leave_spinning =1 ;if (time_seq) ret =btrfs_search_old_slot (root, &key, path, time_seq);
@@ -5377,7 +5375,6 @@ int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, ret =0 ; done:unlock_up (path,0 ,1 ,0 ,NULL ); - path->leave_spinning = old_spinning;return ret; }
fs/btrfs/ctree.h +0 -1
@@ -370,7 +370,6 @@ struct btrfs_path { unsigned int search_for_split:1; unsigned int keep_locks:1; unsigned int skip_locking:1; - unsigned int leave_spinning:1; unsigned int search_commit_root:1; unsigned int need_commit_sem:1; unsigned int skip_release_on_error:1;
fs/btrfs/delayed-inode.c +0 -4
@@ -1149,7 +1149,6 @@ static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans, int nr) path =btrfs_alloc_path ();if (!path)return -ENOMEM ; - path->leave_spinning =1 ; block_rsv = trans->block_rsv; trans->block_rsv = &fs_info->delayed_block_rsv;
@@ -1214,7 +1213,6 @@ int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,btrfs_release_delayed_node (delayed_node);return -ENOMEM ; } - path->leave_spinning =1 ; block_rsv = trans->block_rsv; trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv;
@@ -1259,7 +1257,6 @@ int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode) ret = -ENOMEM ; goto trans_out; } - path->leave_spinning =1 ; block_rsv = trans->block_rsv; trans->block_rsv = &fs_info->delayed_block_rsv;
@@ -1328,7 +1325,6 @@ static void btrfs_async_run_delayed_root(struct btrfs_work *work)if (!delayed_node)break ; - path->leave_spinning =1 ; root = delayed_node->root; trans =btrfs_join_transaction (root);
fs/btrfs/dir-item.c +0 -1
@@ -127,7 +127,6 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, const char *name, path =btrfs_alloc_path ();if (!path)return -ENOMEM ; - path->leave_spinning =1 ;btrfs_cpu_key_to_disk (&disk_key, location);
fs/btrfs/export.c +0 -1
@@ -222,7 +222,6 @@ static int btrfs_get_name(struct dentry *parent, char *name, path =btrfs_alloc_path ();if (!path)return -ENOMEM ; - path->leave_spinning =1 ;if (ino ==BTRFS_FIRST_FREE_OBJECTID ) { key.objectid =BTRFS_I (inode)->root->root_key.objectid;
fs/btrfs/extent-tree.c +0 -8
@@ -1447,7 +1447,6 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ;/* this will setup the path even if it fails to insert the back ref */ ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes, parent, root_objectid, owner,
@@ -1471,7 +1470,6 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,btrfs_mark_buffer_dirty (leaf);btrfs_release_path (path); - path->leave_spinning =1 ;/* now insert the actual backref */ if (owner <BTRFS_FIRST_FREE_OBJECTID ) {BUG_ON (refs_to_add !=1 );
@@ -1587,7 +1585,6 @@ static int run_delayed_extent_op(struct btrfs_trans_handle *trans, } again: - path->leave_spinning =1 ; ret =btrfs_search_slot (trans, fs_info->extent_root, &key, path,0 ,1 );if (ret <0 ) { err = ret;
@@ -2959,8 +2956,6 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; - is_data = owner_objectid >=BTRFS_FIRST_FREE_OBJECTID ;BUG_ON (!is_data && refs_to_drop !=1 );
@@ -3002,7 +2997,6 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans, goto out; }btrfs_release_path (path); - path->leave_spinning =1 ; key.objectid = bytenr; key.type =BTRFS_EXTENT_ITEM_KEY ;
@@ -4292,7 +4286,6 @@ static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; ret =btrfs_insert_empty_item (trans, fs_info->extent_root, path, ins, size);if (ret) {
@@ -4377,7 +4370,6 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; ret =btrfs_insert_empty_item (trans, fs_info->extent_root, path, &extent_key, size);if (ret) {
fs/btrfs/extent_io.c +0 -1
@@ -4725,7 +4725,6 @@ int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, path =btrfs_alloc_path ();if (!path)return -ENOMEM ; - path->leave_spinning =1 ; roots =ulist_alloc (GFP_KERNEL ); tmp_ulist =ulist_alloc (GFP_KERNEL );
fs/btrfs/file-item.c +0 -4
@@ -142,7 +142,6 @@ int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, file_key.offset = pos; file_key.type =BTRFS_EXTENT_DATA_KEY ; - path->leave_spinning =1 ; ret =btrfs_insert_empty_item (trans, root, path, &file_key,sizeof (*item));if (ret <0 )
@@ -706,7 +705,6 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans, key.offset = end_byte -1 ; key.type =BTRFS_EXTENT_CSUM_KEY ; - path->leave_spinning =1 ; ret =btrfs_search_slot (trans, root, &key, path,-1 ,1 );if (ret >0 ) {if (path->slots[0 ] ==0 )
@@ -991,10 +989,8 @@ int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, }else { ins_size = csum_size; } - path->leave_spinning =1 ; ret =btrfs_insert_empty_item (trans, root, path, &file_key, ins_size); - path->leave_spinning =0 ;if (ret <0 ) goto out;if (WARN_ON (ret !=0 ))
fs/btrfs/free-space-tree.c +0 -2
@@ -1191,8 +1191,6 @@ static int clear_free_space_tree(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; - key.objectid =0 ; key.type =0 ; key.offset =0 ;
fs/btrfs/inode-item.c +0 -6
@@ -119,8 +119,6 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; - ret =btrfs_search_slot (trans, root, &key, path,-1 ,1 );if (ret >0 ) ret = -ENOENT ;
@@ -193,8 +191,6 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; - ret =btrfs_search_slot (trans, root, &key, path,-1 ,1 );if (ret >0 ) { ret = -ENOENT ;
@@ -270,7 +266,6 @@ static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; ret =btrfs_insert_empty_item (trans, root, path, &key, ins_len);if (ret == -EEXIST ) {
@@ -327,7 +322,6 @@ int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; path->skip_release_on_error =1 ; ret =btrfs_insert_empty_item (trans, root, path, &key, ins_len);
fs/btrfs/inode.c +0 -12
@@ -196,7 +196,6 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans, key.type =BTRFS_EXTENT_DATA_KEY ; datasize =btrfs_file_extent_calc_inline_size (cur_size); - path->leave_spinning =1 ; ret =btrfs_insert_empty_item (trans, root, path, &key, datasize);if (ret)
@@ -2498,7 +2497,6 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, ins.offset = file_pos; ins.type =BTRFS_EXTENT_DATA_KEY ; - path->leave_spinning =1 ; ret =btrfs_insert_empty_item (trans, root, path, &ins,sizeof (*stack_fi));if (ret)
@@ -3480,7 +3478,6 @@ static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; ret =btrfs_lookup_inode (trans, root, path, &BTRFS_I (inode)->location,1 );if (ret) {
@@ -3569,7 +3566,6 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, goto out; } - path->leave_spinning =1 ; di =btrfs_lookup_dir_item (trans, root, path, dir_ino, name, name_len,-1 );if (IS_ERR_OR_NULL (di)) {
@@ -6025,7 +6021,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, goto fail; } - path->leave_spinning =1 ; ret =btrfs_insert_empty_items (trans, root, path, key, sizes, nitems);if (ret !=0 ) goto fail_unlock;
@@ -6584,13 +6579,6 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,/* Chances are we'll be called again, so go ahead and do readahead */ path->reada =READA_FORWARD ; - -/* -* Unless we're going to uncompress the inline extent, no sleep would -* happen. -*/ - path->leave_spinning =1 ; - path->recurse =btrfs_is_free_space_inode (inode); ret =btrfs_lookup_file_extent (NULL , root, path, objectid, start,0 );
fs/btrfs/ioctl.c +0 -1
@@ -3382,7 +3382,6 @@ static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) ret = -ENOMEM ; goto out_free; } - path->leave_spinning =1 ; trans =btrfs_start_transaction (root,1 );if (IS_ERR (trans)) {
fs/btrfs/qgroup.c +0 -2
@@ -893,8 +893,6 @@ static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,if (!path)return -ENOMEM ; - path->leave_spinning =1 ; - key.objectid =0 ; key.offset =0 ; key.type =0 ;
fs/btrfs/reflink.c +0 -3
@@ -347,7 +347,6 @@ static int btrfs_clone(struct inode *src, struct inode *inode,u64 drop_start;/* Note the key will change type as we walk through the tree */ - path->leave_spinning =1 ; ret =btrfs_search_slot (NULL ,BTRFS_I (src)->root, &key, path,0 ,0 );if (ret <0 )
@@ -417,7 +416,6 @@ static int btrfs_clone(struct inode *src, struct inode *inode, size);btrfs_release_path (path); - path->leave_spinning =0 ;memcpy (&new_key, &key,sizeof (new_key)); new_key.objectid =btrfs_ino (BTRFS_I (inode));
@@ -531,7 +529,6 @@ static int btrfs_clone(struct inode *src, struct inode *inode,* mixing buffered and direct IO writes against this file. */ btrfs_release_path (path); - path->leave_spinning =0 ; ret =btrfs_punch_hole_range (inode, path, last_dest_end, destoff + len -1 ,NULL , &trans);
fs/btrfs/super.c +0 -2
@@ -1126,7 +1126,6 @@ char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info, ret = -ENOMEM ; goto err; } - path->leave_spinning =1 ; name =kmalloc (PATH_MAX ,GFP_KERNEL );if (!name) {
@@ -1255,7 +1254,6 @@ static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objec path =btrfs_alloc_path ();if (!path)return -ENOMEM ; - path->leave_spinning =1 ;/* * Find the "default" dir item which points to the root item that we
fs/btrfs/tests/qgroup-tests.c +0 -4
@@ -36,7 +36,6 @@ static int insert_normal_tree_ref(struct btrfs_root *root, u64 bytenr,return -ENOMEM ; } - path->leave_spinning =1 ; ret =btrfs_insert_empty_item (&trans, root, path, &ins, size);if (ret) {test_err ("couldn't insert ref %d" , ret);
@@ -86,7 +85,6 @@ static int add_tree_ref(struct btrfs_root *root, u64 bytenr, u64 num_bytes,return -ENOMEM ; } - path->leave_spinning =1 ; ret =btrfs_search_slot (&trans, root, &key, path,0 ,1 );if (ret) {test_err ("couldn't find extent ref" );
@@ -135,7 +133,6 @@ static int remove_extent_item(struct btrfs_root *root, u64 bytenr,test_std_err (TEST_ALLOC_ROOT );return -ENOMEM ; } - path->leave_spinning =1 ; ret =btrfs_search_slot (&trans, root, &key, path,-1 ,1 );if (ret) {
@@ -170,7 +167,6 @@ static int remove_extent_ref(struct btrfs_root *root, u64 bytenr,return -ENOMEM ; } - path->leave_spinning =1 ; ret =btrfs_search_slot (&trans, root, &key, path,0 ,1 );if (ret) {test_err ("couldn't find extent ref" );
Signature · 1 line
2.24.1
On Mon, Aug 10, 2020 at 11:42:25AM -0400, Josef Bacik wrote:
The lockdep fixes are independent so I'd like to take them first, that's patches 1-6. I haven't looked at the rest yet.