Btrfs: do not wait after queue async work for delaye refs
PATCH 1/1 Btrfs: do not wait after queue async work for delaye refs
If metadata space is hungry, how fast flush_space() can run determines the latency we spend in reserve_metadata_space().
flush_space()
case FLUSH_DELAYED_ITEMS:
...
btrfs_end_transaction()
case ALLOC_CHUNK:
...
btrfs_end_transaction()btrfs_end_transaction()
btrfs_async_run_delayed_refs()
// queue a work to process delayed refs
...
if (wait)
wait_for_completion()Although processing delayed refs can add to pinned bytes, pinned bytes can only be used after committing transaction, so waiting async in flush_space() doesn't help.
In fact we don't have to wait for asynchronous delayed refs processing as delayed refs are not blocking the subsequent operations(unless within committing transaction we need to make sure filesystem is consistent).
--- fs/btrfs/ctree.h | 2 +- fs/btrfs/extent-tree.c | 18 ++---------------- fs/btrfs/inode.c | 2 +- fs/btrfs/transaction.c | 3 +-- 4 files changed, 5 insertions(+), 20 deletions(-)
fs/btrfs/ctree.h +1 -1
@@ -2620,7 +2620,7 @@ void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,int btrfs_run_delayed_refs (struct btrfs_trans_handle *trans,unsigned long count);int btrfs_async_run_delayed_refs (struct btrfs_fs_info *fs_info, -unsigned long count,u64 transid,int wait); +unsigned long count,u64 transid);int btrfs_lookup_data_extent (struct btrfs_fs_info *fs_info,u64 start,u64 len); int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, struct btrfs_fs_info *fs_info, u64 bytenr,
fs/btrfs/extent-tree.c +2 -16
@@ -2852,14 +2852,11 @@ static void delayed_ref_async_start(struct btrfs_work *work)if (ret && !async->error) async->error = ret; done: -if (async->sync) -complete (&async->wait); -else -kfree (async); +kfree (async); }int btrfs_async_run_delayed_refs (struct btrfs_fs_info *fs_info, -unsigned long count,u64 transid,int wait) +unsigned long count,u64 transid) {struct async_delayed_refs *async;int ret;
@@ -2872,23 +2869,12 @@ int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info, async->count = count; async->error =0 ; async->transid = transid; -if (wait) - async->sync =1 ; -else - async->sync =0 ; -init_completion (&async->wait);btrfs_init_work (&async->work, btrfs_extent_refs_helper, delayed_ref_async_start,NULL ,NULL );btrfs_queue_work (fs_info->extent_workers, &async->work); -if (wait) { -wait_for_completion (&async->wait); - ret = async->error; -kfree (async); -return ret; - }return 0 ; }
fs/btrfs/inode.c +1 -1
@@ -4736,7 +4736,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,if (btrfs_should_throttle_delayed_refs (trans, fs_info))btrfs_async_run_delayed_refs (fs_info, trans->delayed_ref_updates *2 , - trans->transid,0 ); + trans->transid);if (be_nice) {if (truncate_space_check (trans, root, extent_num_bytes)) {
fs/btrfs/transaction.c +1 -2
@@ -889,8 +889,7 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,kmem_cache_free (btrfs_trans_handle_cachep, trans);if (must_run_delayed_refs) { -btrfs_async_run_delayed_refs (info, cur, transid, - must_run_delayed_refs ==1 ); +btrfs_async_run_delayed_refs (info, cur, transid); }return err; }
Signature · 1 line
1.8.3.1
[Adding Josef to CC]
Quoted 13 lines · Liu Bo
If metadata space is hungry, how fast
flush_space()can run determines the latency we spend inreserve_metadata_space().
flush_space()
caseFLUSH_DELAYED_ITEMS:... btrfs_end_transaction() case ALLOC_CHUNK: ... btrfs_end_transaction()
btrfs_end_transaction()
btrfs_async_run_delayed_refs()
Quoted 8 lines
// queue a work to process delayed refs ... if (wait) wait_for_completion()Although processing delayed refs can add to pinned bytes, pinned bytes can only be used after committing transaction, so waiting async in
flush_space()doesn't help.
But not waiting for the async delayed refs to complete don't we introduce a race where checking the pinned bytes might indicate there are not enough bytes to commit at the same time we will have delayed refs which haven't yet run but might add enough pinned bytes to make sense to commit?
Quoted 89 lines
In fact we don't have to wait for asynchronous delayed refs processing as delayed refs are not blocking the subsequent operations(unless within committing transaction we need to make sure filesystem is consistent).
Signed-off-by Liu Bo <[email protected]>--- fs/btrfs/ctree.h | 2 +- fs/btrfs/extent-tree.c | 18 ++---------------- fs/btrfs/inode.c | 2 +- fs/btrfs/transaction.c | 3 +-- 4 files changed, 5 insertions(+), 20 deletions(-)fs/btrfs/ctree.h +1
-1@@ -2620,7 +2620,7 @@ void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,int btrfs_run_delayed_refs (struct btrfs_trans_handle *trans,unsigned long count);int btrfs_async_run_delayed_refs (struct btrfs_fs_info *fs_info, -unsigned long count,u64 transid,int wait); +unsigned long count,u64 transid);int btrfs_lookup_data_extent (struct btrfs_fs_info *fs_info,u64 start,u64 len); int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, struct btrfs_fs_info *fs_info, u64 bytenr,fs/btrfs/extent-tree.c +2
-16@@ -2852,14 +2852,11 @@ static void delayed_ref_async_start(struct btrfs_work *work)if (ret && !async->error) async->error = ret; done: -if (async->sync) -complete (&async->wait); -else -kfree (async); +kfree (async); }int btrfs_async_run_delayed_refs (struct btrfs_fs_info *fs_info, -unsigned long count,u64 transid,int wait) +unsigned long count,u64 transid) {struct async_delayed_refs *async;int ret;@@ -2872,23 +2869,12 @@ int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info, async->count = count; async->error =0 ; async->transid = transid; -if (wait) - async->sync =1 ; -else - async->sync =0 ; -init_completion (&async->wait);btrfs_init_work (&async->work, btrfs_extent_refs_helper, delayed_ref_async_start,NULL ,NULL );btrfs_queue_work (fs_info->extent_workers, &async->work); -if (wait) { -wait_for_completion (&async->wait); - ret = async->error; -kfree (async); -return ret; - }return 0 ; }fs/btrfs/inode.c +1
-1@@ -4736,7 +4736,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,if (btrfs_should_throttle_delayed_refs (trans, fs_info))btrfs_async_run_delayed_refs (fs_info, trans->delayed_ref_updates *2 , - trans->transid,0 ); + trans->transid);if (be_nice) {if (truncate_space_check (trans, root, extent_num_bytes)) {fs/btrfs/transaction.c +0
-0
fs/btrfs/transaction.c static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, · line 893
Quoted 7 lines
fs/btrfs/transaction.c +1
-2@@ -889,8 +889,7 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,kmem_cache_free (btrfs_trans_handle_cachep, trans);if (must_run_delayed_refs) { -btrfs_async_run_delayed_refs (info, cur, transid, - must_run_delayed_refs ==1 ); +btrfs_async_run_delayed_refs (info, cur, transid); }
If you do this, this enables you do also simplify the if (!trans->sync) case in __btrfs_end_transaction since now we won't care what's the type of transaction and only care about must_run_delayed_refs being set and entirely remove the trans->type check.
Quoted 3 lines
fs/btrfs/transaction.c +0
-0return err; }
Josef Bacik
NAKQuoted 24 lines · Nikolay Borisov
[Adding Josef to CC]
Quoted 21 lines · Liu Bo
If metadata space is hungry, how fast
flush_space()can run determines the latency we spend inreserve_metadata_space().
flush_space()
caseFLUSH_DELAYED_ITEMS:... btrfs_end_transaction() case ALLOC_CHUNK: ... btrfs_end_transaction()
btrfs_end_transaction()
btrfs_async_run_delayed_refs()// queue a work to process delayed refs ... if (wait) wait_for_completion()Although processing delayed refs can add to pinned bytes, pinned bytes can only be used after committing transaction, so waiting async in
flush_space()doesn't help.
Nack, but just because I get rid of all of this code in my delayed refs rsv patchset. Thanks,
Josef