Repository navigation
btrfs: keep the extent map referenced until a compressed read is submitted - #264
Merged
Merged
Conversation
…itted When btrfs_do_readpage() adds blocks of a compressed extent to a bio it drops its reference on the extent map, and btrfs_submit_compressed_read() looks the extent map up again in the inode's extent map tree, by the bio's file offset, to learn where the extent starts in the file and how it is compressed. If the lookup misses, the whole bio fails with -EIO. The lookup can miss. The bio is submitted when a later block does not fit it, or at the end of the read_folio/readahead call after the range has been unlocked, and the extent map shrinker can remove the extent map from the tree in between: apart from pinned extent maps it drops whatever it finds, regardless of locked io tree ranges or of references held by readers. Under memory pressure, in particular in a memory cgroup at its limit whose reclaim keeps invoking the fs shrinker, this shows up as spurious -EIO on reads of compressed files, and on buffered writes that have to read a partial folio first. Have the bio_ctrl hold a reference on the extent map of the compressed extent its bio reads, handed over by btrfs_do_readpage() once the block is in the bio and dropped once the bio is submitted, and pass it to btrfs_submit_compressed_read() instead of looking it up again. Fixes: 956a17d ("btrfs: add a shrinker for extent maps") Assisted-by: LLM Signed-off-by: Ryan Murray <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On a btrfs with compressed data, reads can fail with -EIO under memory pressure. A buffered
write that has to read a partial folio first fails with it too.
btrfs_do_readpage()drops its reference on the extent map once a block of a compressedextent is in the bio.
btrfs_submit_compressed_read()then looks the extent map up again inthe inode's extent map tree, by the bio's file offset, and fails the whole bio with -EIO on a
miss. The bio is only submitted when a later block does not fit it, or at the end of the
read_folio/readahead call after the range has been unlocked. In between, the extent map
shrinker can remove the extent map from the tree: apart from pinned extent maps it drops
whatever it finds, whether or not the range is locked or a reader holds a reference.
The fix has the
btrfs_bio_ctrlhold a reference on the extent map of the compressed extentits bio reads.
btrfs_do_readpage()hands the reference over once the block is in the bio,it is dropped once the bio is submitted, and it is passed to
btrfs_submit_compressed_read()instead of a second lookup.
As of this writing, mainline and the btrfs for-next tree still do the second lookup.
Testing
Written for this tree; tested on a MacBook Neo (J700, 16 KiB pages, 4 KiB sectors), a MacBook Air (M2)
and a MacBook Pro (M1 Pro), plus the guests below:
J700, scratch btrfs on a direct-I/O loop device mounted
compress-force=zstd:1, 10 minutes of thestress below in a 192 MiB memory cgroup: 632,265 verified reads and 470,589 sub-block writes, 0 read
or write errors, 0 mismatches; the cgroup hit
memory.max131,614 times with 0 OOM kills, and theshrinker removed 127,303 extent maps (
btrfs_extent_map_shrinker_remove_em). No btrfs errors orwarnings in the kernel log. A read-only scrub of the 104 GiB root file system afterwards found no errors.
M2 and M1 Pro: write/drop caches/read-verify of 8 x 32 MiB on a compressed scratch file system,
btrfs checkclean, root file system device stats all zero.Build: J700 configuration (arm64, LLVM), no new warnings;
W=1clean forfs/btrfs/extent_io.oandfs/btrfs/compression.o.KVM guest (x86_64, 4 KiB sectors), fresh btrfs mounted
compress-force=zstd:1, 32 x 16 MiBfiles of verifiable text. The workload is 8 readers, 4 sub-block writers and 2
anonymous-memory churners, all in a cgroup with
memory.max= 48 MiB. A kprobe onbtrfs_bio_end_io()counts failed btrfs bios, and thebtrfs_extent_map_shrinker_scan_exittracepoint counts shrinker runs:arm64 guest with 16 KiB pages (4 KiB sectors, the subpage read path), 16 x 8 MiB files,
300 s:
Before the fix, every failed bio comes from the -EIO exit of
btrfs_submit_compressed_read(). With 4 KiB pages most failures hit readahead bios thatthe page cache retries, so userspace rarely sees them; with 16 KiB pages many reach
userspace. No data mismatches on either kernel.
The last pair also reads back holes, inline extents (compressed and not), incompressible data,
and a reflinked part of a compressed extent mapped by two file ranges. After unmount, no
btrfs_extent_mapobjects are left.🤖 Generated with Claude Code