]> git.hungrycats.org Git - linux/commitdiff
btrfs: scrub: fix race between scrub and scrub progress ioctl
authorFilipe Manana <fdmanana@suse.com>
Fri, 16 Oct 2026 17:02:23 +0000 (18:02 +0100)
committerDavid Sterba <dsterba@suse.com>
Fri, 30 Oct 2026 20:36:17 +0000 (21:36 +0100)
The scrub progress ioctl is copying the scrub progress stats without
acquiring the spinlock that protects it and a concurrent scrub can be
updating the stats at the same time which can lead to load tearing
issues. So take the spinlock when copying the progress stats.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Daniel Vacek <neelx@suse.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/scrub.c

index 680b209065f00048a3b14a504811bd187a36e8b3..bf91a6130e4613d8d620dea006e7003adbf004ac 100644 (file)
@@ -3296,8 +3296,11 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
        if (dev) {
                mutex_lock(&fs_info->scrub_lock);
                sctx = dev->scrub_ctx;
-               if (sctx)
+               if (sctx) {
+                       spin_lock(&sctx->stat_lock);
                        memcpy(progress, &sctx->stat, sizeof(*progress));
+                       spin_unlock(&sctx->stat_lock);
+               }
                mutex_unlock(&fs_info->scrub_lock);
        }
        mutex_unlock(&fs_info->fs_devices->device_list_mutex);