Skip to content

Commit 1ea2872

Browse files
isilencekdave
authored andcommitted
btrfs: fix racy access to discard_ctl data
Because only one discard worker may be running at any given point, it could have been safe to modify ->prev_discard, etc. without synchronization, if not for @OverRide flag in btrfs_discard_schedule_work() and delayed_work_pending() returning false while workfn is running. That may lead to torn reads of u64 for some architectures, but that's not a big problem as only slightly affects the discard rate. Suggested-by: Josef Bacik <[email protected]> Reviewed-by: Josef Bacik <[email protected]> Signed-off-by: Pavel Begunkov <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent ea9ed87 commit 1ea2872

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

fs/btrfs/discard.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -477,13 +477,6 @@ static void btrfs_discard_workfn(struct work_struct *work)
477477
discard_ctl->discard_extent_bytes += trimmed;
478478
}
479479

480-
/*
481-
* Updated without locks as this is inside the workfn and nothing else
482-
* is reading the values
483-
*/
484-
discard_ctl->prev_discard = trimmed;
485-
discard_ctl->prev_discard_time = ktime_get_ns();
486-
487480
/* Determine next steps for a block_group */
488481
if (block_group->discard_cursor >= btrfs_block_group_end(block_group)) {
489482
if (discard_state == BTRFS_DISCARD_BITMAPS) {
@@ -499,7 +492,10 @@ static void btrfs_discard_workfn(struct work_struct *work)
499492
}
500493
}
501494

495+
now = ktime_get_ns();
502496
spin_lock(&discard_ctl->lock);
497+
discard_ctl->prev_discard = trimmed;
498+
discard_ctl->prev_discard_time = now;
503499
discard_ctl->block_group = NULL;
504500
spin_unlock(&discard_ctl->lock);
505501

0 commit comments

Comments
 (0)