diff options
| author | Qu Wenruo <wqu@suse.com> | 2026-11-05 11:38:00 +1030 |
|---|---|---|
| committer | David Sterba <dsterba@suse.com> | 2026-11-23 18:44:56 +0100 |
| commit | 3b112e8542862db4a92978385e0b9d3fb8064e7a (patch) | |
| tree | 3f6954778d62e0c1cb7e0c46d03eb5bffcb23570 /tools/perf/scripts/python/export-to-postgresql.py | |
| parent | aec8248642ce734dcc2991c069e8edfcf90770c8 (diff) | |
btrfs: fix a race between reflink and compressed writes
[BUG]
There is a bug report with a reproducer that can lead to transaction
abort. The reproducer requires either large folios or page size larger
than block size and compression:
BTRFS info (device dm-2): use zstd compression, level 3
------------[ cut here ]------------
WARNING: fs/btrfs/extent_map.c:443 at btrfs_unpin_extent_cache+0x12a/0x1a0 [btrfs], CPU#0: kworker/u41:1/1073
CPU: 0 UID: 0 PID: 1073 Comm: kworker/u41:1 Tainted: G W E 7.3.0-rc7-custom+ #111 PREEMPT(full) 181cfbec4e757bdd6c595e90a917484279074a92
Workqueue: btrfs-endio-write btrfs_work_helper [btrfs]
RIP: 0010:btrfs_unpin_extent_cache+0x12a/0x1a0 [btrfs]
Call Trace:
<TASK>
btrfs_finish_one_ordered+0x8f9/0xce0 [btrfs 78b39de6ff5f8aabf4d2477512804f86456deeca]
btrfs_work_helper+0xc5/0x3a0 [btrfs 78b39de6ff5f8aabf4d2477512804f86456deeca]
process_scheduled_works+0x20b/0x480
worker_thread+0x1f7/0x2f0
kthread+0x101/0x130
ret_from_fork+0xd9/0x250
ret_from_fork_asm+0x11/0x20
</TASK>
---[ end trace 0000000000000000 ]---
BTRFS warning (device dm-2): found extent map for inode 257 (root 5) with unexpected start offset 8192 when unpinning extent range [4096, 12288), generation 12
------------[ cut here ]------------
WARNING: fs/btrfs/inode.c:3321 at btrfs_finish_one_ordered+0xc4b/0xce0 [btrfs], CPU#0: kworker/u41:1/1073
CPU: 0 UID: 0 PID: 1073 Comm: kworker/u41:1 Tainted: G W E 7.3.0-rc7-custom+ #111 PREEMPT(full) 181cfbec4e757bdd6c595e90a917484279074a92
Workqueue: btrfs-endio-write btrfs_work_helper [btrfs]
RIP: 0010:btrfs_finish_one_ordered+0xc4b/0xce0 [btrfs]
Call Trace:
<TASK>
btrfs_work_helper+0xc5/0x3a0 [btrfs 78b39de6ff5f8aabf4d2477512804f86456deeca]
process_scheduled_works+0x20b/0x480
worker_thread+0x1f7/0x2f0
kthread+0x101/0x130
ret_from_fork+0xd9/0x250
ret_from_fork_asm+0x11/0x20
</TASK>
---[ end trace 0000000000000000 ]---
BTRFS error (device dm-2 state A): Transaction 12 aborted (-EUCLEAN)
BTRFS: error (device dm-2 state A) in btrfs_finish_one_ordered:3321: errno=-117 Filesystem corrupted
[CAUSE]
There is a race between compressed write and regular write inside the
same folio, that can lead to incorrect PAGECACHE_TAG_DIRTY tag to be
cleared too early.
In this example, we have a large folio containing 8 pages (4K page size,
4K block size), with the following layout:
0 8K 16K 24K 32K
|//////////| |///|
Where [4K, 12K) is dirty, and [20K, 24K) is also dirty.
Writeback | Compression worker
----------------------------------+------------------------------------
writepage_delalloc() |
|- run_delalloc_compressed() |
| | Queueing [4K, 12K) for |
| | compression. |
| \- btrfs_queue_work() |
| | compress_file_range()
| | |- extent_range_clear_dirty_for_io()
| | \- btrfs_folio_clamp_clear_dirty()
| | Now range [4K, 12K) is no longer
| | dirty, but also without an OE.
\- run_delalloc_cow() |
| For the [20K, 24K) |
\- submit_one_sector() |
|- btrfs_folio_clear_dirty()|
| Since [20K, 24K) is the |
| last dirty block, the |
| folio is no longer dirty |
| |
|- btrfs_folio_set_writeback()
Since the folio is no |
longer dirty, the |
PAGECACHE_TAG_DIRTY tag |
is also cleared. |
The above race will lead to a 32K folio that contains [4K, 12K) range
that is still under compression (and should still be counted dirty), but
without an OE.
Then later in the reflink:
Reflink | Compression worker
----------------------------------+------------------------------------
btrfs_clone_files() |
| Which is to replace [4K, 8K) |
| |
|- btrfs_wait_ordered_range() |
| |- btrfs_fdatawrite_range() |
| | Nothing to do, since no |
| | DIRTY tag for the folio |
| | |
| |- filemap_fdatawait_range() |
| | The [20K, 24K) finished, |
| | So no WRITEBACK tag |
| | |
| \- lookup OE |
| No OE is created for range |
| [4K, 12K) |
| |
| | submit_compressed_extents
| | \- submit_one_async_extent
| | \- btrfs_alloc_ordered_extent()
| | OE for [4K, 12K) created, but
| | it's too late.
\- btrfs_clone() |
Replace file extents for range |
[4K, 8K), which will conflict |
with the new compressed range |
[4K, 12K) |
| end_bbio_data_write([4k, 12K))
| \- btrfs_finish_ordered()
|
|
| finish_ordered_fn()
| \- btrfs_finish_one_ordered()
| \- btrfs_unpin_extent_cache()
| em start 8K != 4K
return -EUCLEAN, transaction aborted
[FIX]
Do not clear the folio dirty early during compress_file_range().
The premature clearing is done through extent_range_clear_dirty_for_io(),
so remove the folio dirty clearing completely from it.
And since what's left is only checking the folio write protection,
rename the function to extent_range_check_write_protected() instead, and
slightly clean it up, so it doesn't need to return an int.
Finally since the range is still dirty now, all run_delalloc_cow()
callers pass @pages_dirty = true, we can remove that parameter along other
involved functions.
Fixes: 1d2fbb7f1f9e ("btrfs: allow compression even if the range is not page aligned")
Reported-by: Breno Leitao <leitao@debian.org>
Link: https://lore.kernel.org/linux-btrfs/ause7k_HjzUWT3D-@gmail.com/
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Tested-by: Breno Leitao <leitao@debian.org>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
Diffstat (limited to 'tools/perf/scripts/python/export-to-postgresql.py')
0 files changed, 0 insertions, 0 deletions
