btrfs: warn if EXTENT_BUFFER_UPTODATE is set while reading
authorTavian Barnes <tavianator@tavianator.com>
Mon, 18 Mar 2024 13:56:54 +0000 (09:56 -0400)
committerDavid Sterba <dsterba@suse.com>
Tue, 7 May 2024 19:31:00 +0000 (21:31 +0200)
We recently tracked down a race condition that triggered a read for an
extent buffer with EXTENT_BUFFER_UPTODATE already set.  While this read
was in progress, other concurrent readers would see the UPTODATE bit and
return early as if the read was already complete, making accesses to the
extent buffer conflict with the read operation that was overwriting it.

Add a WARN_ON() to end_bbio_meta_read() for this situation to make
similar races easier to spot in the future.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Tavian Barnes <tavianator@tavianator.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/extent_io.c

index 23bdd05..ecb18a8 100644 (file)
@@ -4275,6 +4275,13 @@ static void end_bbio_meta_read(struct btrfs_bio *bbio)
        struct folio_iter fi;
        u32 bio_offset = 0;
 
+       /*
+        * If the extent buffer is marked UPTODATE before the read operation
+        * completes, other calls to read_extent_buffer_pages() will return
+        * early without waiting for the read to finish, causing data races.
+        */
+       WARN_ON(test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags));
+
        eb->read_mirror = bbio->mirror_num;
 
        if (uptodate &&