]> 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)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Thu, 5 Nov 2026 04:21:56 +0000 (23:21 -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 ab268571b210d4d49c5f03e96189c3b760989c3a)

fs/btrfs/scrub.c

index 9c43abbd1de4f281851873e0d0cddc5f8255bfb0..86450cfd31685b845d71a8df87635e3babcf121f 100644 (file)
@@ -3301,8 +3301,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);