]> 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>
Sun, 8 Nov 2026 06:07:55 +0000 (01:07 -0500)
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>
(cherry picked from btrfs for-next commit 9065b20db1369c882b790f1fb94faad7ef871028)

fs/btrfs/scrub.c

index 8f65f569ac1a9173396d31e8a8ec90c07e35bc14..d582c1c81599d2662492a90d97b8c5e1d133d9f9 100644 (file)
@@ -3298,8 +3298,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);