Skip to content

btrfs: keep the extent map referenced until a compressed read is submitted - #264

Merged
RyanTheTide merged 1 commit into
aurora-wipfrom
j700/btrfs-compressed-read-em
Oct 11, 2026
Merged

RyanTheTide merged 1 commit into
aurora-wipfrom
j700/btrfs-compressed-read-em

Conversation

@RyanTheTide

Copy link
Copy Markdown
Contributor

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 compressed
extent is in the bio. btrfs_submit_compressed_read() then looks the extent map up again in
the 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_ctrl hold a reference on the extent map of the compressed extent
its 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 the
    stress 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.max 131,614 times with 0 OOM kills, and the
    shrinker removed 127,303 extent maps (btrfs_extent_map_shrinker_remove_em). No btrfs errors or
    warnings 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 check clean, root file system device stats all zero.

  • Build: J700 configuration (arm64, LLVM), no new warnings; W=1 clean for
    fs/btrfs/extent_io.o and fs/btrfs/compression.o.

  • KVM guest (x86_64, 4 KiB sectors), fresh btrfs mounted compress-force=zstd:1, 32 x 16 MiB
    files 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 on
    btrfs_bio_end_io() counts failed btrfs bios, and the
    btrfs_extent_map_shrinker_scan_exit tracepoint counts shrinker runs:

    kernel duration shrinker runs extent maps dropped failed btrfs bios EIO seen by userspace
    before fix 180 s 581 118710 28 0
    with fix 180 s 564 133105 0 0
    before fix 300 s 6466 1252719 687 1 (buffered write)
    with fix 300 s 7250 1303914 0 0
    before fix 180 s 10265 1872843 1233 0
    with fix 180 s 9444 1680464 0 0
  • arm64 guest with 16 KiB pages (4 KiB sectors, the subpage read path), 16 x 8 MiB files,
    300 s:

    kernel shrinker runs extent maps dropped failed btrfs bios EIO seen by userspace
    before fix 3150 1521652 522 1 read, 108 buffered writes
    with fix 3331 1566570 0 0

    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 that
    the 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_map objects are left.

🤖 Generated with Claude Code

…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant