]> git.hungrycats.org Git - linux/commitdiff
btrfs: scrub: fix lost wakeup and hang when cancelling a scrub that is finishing
authorFilipe Manana <fdmanana@suse.com>
Fri, 16 Oct 2026 16:14:49 +0000 (17:14 +0100)
committerDavid Sterba <dsterba@suse.com>
Mon, 19 Oct 2026 15:08:24 +0000 (17:08 +0200)
When scrub finishes, it does a wakeup on the fs_info->scrub_pause_wait
wait queue and then sets dev->scrub_sctx to NULL. On the other hand the
scrub cancel code (btrfs_scrub_cancel_dev()) waits on that wait queue
for dev->scrub_sctx to be NULL. So this means if btrfs_scrub_cancel_dev()
is called right when scrub is finishing and sees the wakeup before the
NULL is assigned to dev->scrub_sctx by btrfs_scrub_dev(), it hangs.

Fix this by doing the wakeup in btrfs_scrub_dev() after setting
dev->scrub_sctx to NULL.

Fixes: a2de733c78fa ("btrfs: scrub")
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/scrub.c

index 5434a613f39349034b0b1c1dc33f5242a0748202..dd844074e71435e1f7aabb5d6c4ed003491fc239 100644 (file)
@@ -3171,7 +3171,6 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
        memalloc_nofs_restore(nofs_flag);
 
        atomic_dec(&fs_info->scrubs_running);
-       wake_up(&fs_info->scrub_pause_wait);
 
        if (progress)
                memcpy(progress, &sctx->stat, sizeof(*progress));
@@ -3184,6 +3183,13 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
        dev->scrub_ctx = NULL;
        mutex_unlock(&fs_info->scrub_lock);
 
+       /*
+        * We have set dev->scrub_ctx to NULL, now do the wakeup as a concurrent
+        * scrub cancel (btrfs_scrub_cancel_dev()) waits for dev->scrub_ctx to
+        * be set to NULL.
+        */
+       wake_up(&fs_info->scrub_pause_wait);
+
        scrub_workers_put(fs_info);
        scrub_put_ctx(sctx);