btrfs async discard fixes & improvements
v2 cover btrfs async discard fixes & improvements
v2: fix async discard stalls [1/3]
Two others are same but rebased on top. [2/3] fixes not important stats torn reads, [3/3] saves a bit of locking.
note: based on for-next
Pavel Begunkov (3):
btrfs: fix async discard stall
btrfs: fix racy access to discard_ctl data
btrfs: don't overabuse discard lock
fs/btrfs/discard.c | 66 +++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 33 deletions(-)
Signature · 1 line
2.24.0
PATCH v2 1/3 btrfs: fix async discard stall
It might happen that bg->discard_eligible_time was changed without rescheduling, so btrfs_discard_workfn() would be awaken before that eligible time, peek_discard_list() returns null, and all discarding going to sleep until somewhat else calls for rescheduling again.
Fix the stall by keeping btrfs_discard_workfn() going by always calling btrfs_discard_schedule_work() when there is any block group in the discard lists.
--- fs/btrfs/discard.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
fs/btrfs/discard.c +7 -6
@@ -199,10 +199,9 @@ static struct btrfs_block_group *find_next_block_group(static struct btrfs_block_group *peek_discard_list (struct btrfs_discard_ctl *discard_ctl,enum btrfs_discard_state *discard_state, -int *discard_index) +int *discard_index,u64 now) {struct btrfs_block_group *block_group; -const u64 now =ktime_get_ns ();spin_lock (&discard_ctl->lock); again:
@@ -224,10 +223,7 @@ static struct btrfs_block_group *peek_discard_list( discard_ctl->block_group = block_group; *discard_state = block_group->discard_state; *discard_index = block_group->discard_index; - }else { - block_group =NULL ; } -spin_unlock (&discard_ctl->lock);return block_group;
@@ -438,13 +434,18 @@ static void btrfs_discard_workfn(struct work_struct *work)int discard_index =0 ;u64 trimmed =0 ;u64 minlen =0 ; +u64 now =ktime_get_ns (); discard_ctl = container_of(work, struct btrfs_discard_ctl, work.work); block_group =peek_discard_list (discard_ctl, &discard_state, - &discard_index); + &discard_index, now);if (!block_group || !btrfs_run_discard_work (discard_ctl))return ; +if (now < block_group->discard_eligible_time) { +btrfs_discard_schedule_work (discard_ctl, false); +return ; + }/* Perform discarding */ minlen = discard_minlen[discard_index];
Signature · 1 line
2.24.0
PATCH v2 2/3 btrfs: fix racy access to discard_ctl data
In reply to Pavel BegunkovBecause only one discard worker may be running at any given point, it could have been safe to modify ->prev_discard, etc. without synchronisation, if not for @override flag in btrfs_discard_schedule_work() and delayed_work_pending() returning false while workfn is running.
That may lead lead to torn reads of u64 for some architectures, but that's not a big problem as only slightly affects the discard rate.
--- fs/btrfs/discard.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
fs/btrfs/discard.c +3 -7
@@ -475,13 +475,6 @@ static void btrfs_discard_workfn(struct work_struct *work) discard_ctl->discard_extent_bytes += trimmed; } -/* -* Updated without locks as this is inside the workfn and nothing else -* is reading the values -*/ - discard_ctl->prev_discard = trimmed; - discard_ctl->prev_discard_time =ktime_get_ns (); -/* Determine next steps for a block_group */ if (block_group->discard_cursor >=btrfs_block_group_end (block_group)) {if (discard_state ==BTRFS_DISCARD_BITMAPS ) {
@@ -497,7 +490,10 @@ static void btrfs_discard_workfn(struct work_struct *work) } } + now =ktime_get_ns ();spin_lock (&discard_ctl->lock); + discard_ctl->prev_discard = trimmed; + discard_ctl->prev_discard_time = now; discard_ctl->block_group =NULL ;spin_unlock (&discard_ctl->lock);
Signature · 1 line
2.24.0
PATCH v2 3/3 btrfs: don't overabuse discard lock
In reply to Pavel Begunkovbtrfs_discard_workfn() drops discard_ctl->lock just to take it again in a moment in btrfs_discard_schedule_work(). Avoid that and also reuse ktime.
--- fs/btrfs/discard.c | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-)
fs/btrfs/discard.c +23 -20
@@ -326,28 +326,15 @@ void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl,btrfs_discard_schedule_work (discard_ctl, false); } -/** -* btrfs_discard_schedule_work - responsible for scheduling the discard work -* @discard_ctl: discard control -* @override: override the current timer -* -* Discards are issued by a delayed workqueue item. @override is used to -* update the current delay as the baseline delay interval is reevaluated on -* transaction commit. This is also maxed with any other rate limit. -*/ -void btrfs_discard_schedule_work (struct btrfs_discard_ctl *discard_ctl, -bool override) +static void __btrfs_discard_schedule_work (struct btrfs_discard_ctl *discard_ctl, +u64 now,bool override) {struct btrfs_block_group *block_group; -const u64 now =ktime_get_ns (); - -spin_lock (&discard_ctl->lock);if (!btrfs_run_discard_work (discard_ctl)) - goto out; - +return ;if (!override &&delayed_work_pending (&discard_ctl->work)) - goto out; +return ; block_group =find_next_block_group (discard_ctl, now);if (block_group) {
@@ -389,7 +376,24 @@ void btrfs_discard_schedule_work(struct btrfs_discard_ctl *discard_ctl,mod_delayed_work (discard_ctl->discard_workers, &discard_ctl->work,nsecs_to_jiffies (delay)); } -out: +} + +/** +* btrfs_discard_schedule_work - responsible for scheduling the discard work +* @discard_ctl: discard control +* @override: override the current timer +* +* Discards are issued by a delayed workqueue item. @override is used to +* update the current delay as the baseline delay interval is reevaluated on +* transaction commit. This is also maxed with any other rate limit. +*/ +void btrfs_discard_schedule_work (struct btrfs_discard_ctl *discard_ctl, +bool override) +{ +u64 now =ktime_get_ns (); + +spin_lock (&discard_ctl->lock); +__btrfs_discard_schedule_work (discard_ctl, now, override);spin_unlock (&discard_ctl->lock); }
@@ -495,9 +499,8 @@ static void btrfs_discard_workfn(struct work_struct *work) discard_ctl->prev_discard = trimmed; discard_ctl->prev_discard_time = now; discard_ctl->block_group =NULL ; +__btrfs_discard_schedule_work (discard_ctl, now, false);spin_unlock (&discard_ctl->lock); - -btrfs_discard_schedule_work (discard_ctl, false); }/**
Signature · 1 line
2.24.0
Quoted 7 lines · Pavel Begunkov
v2: fix async discard stalls [1/3]
Two others are same but rebased on top. [2/3] fixes not important stats torn reads, [3/3] saves a bit of locking.
note: based on for-next
Quoted 8 lines
Pavel Begunkov (3):
btrfs: fix async discard stall
btrfs: fix racy access todiscard_ctldata
btrfs: don't overabuse discard lockfs/btrfs/discard.c | 66 +++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 33 deletions(-)
Thanks,
Josef