]> git.hungrycats.org Git - linux/commit
DO-NOT-UPSTREAM: btrfs: let queued exclusive operations in between block group reclaims
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Thu, 22 Oct 2026 08:02:53 +0000 (04:02 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:16:34 +0000 (21:16 -0500)
commitba9838662d3d0ff0632b7637ef3a63e0fbd394d1
treeb889a099213d24bf5f525b91c88a95e3b5dfaa99
parent895645f016b3e048f1ab45b7f7a5e056bb501e5f
DO-NOT-UPSTREAM: btrfs: let queued exclusive operations in between block group reclaims

DO-NOT-UPSTREAM: this works around the reclaim worker starving queued
exclusive operations.  Whether upstream wants to do anything about that
starvation, and what, is upstream's decision; this stays on our lanes.

The block group reclaim worker runs under the balance exclusive operation.
At the fill edge, with writers active, each relocation takes tens of
seconds (2 to 61 s measured) and the list keeps refilling, so the
worker held the operation for most of the time writers ran: 84-90% of a
600 s churn in reclaim-futile-test.sh, and for an hour of fsstress in the
octopus P1 pass, where a resize and a replace could not start.

btrfs-progs makes this worse than a plain refusal: before it calls the
ioctl it reads sysfs exclusive_operation.  Without --enqueue it gives up
there; with --enqueue it waits for the notification that
btrfs_exclop_finish() sends and then calls the ioctl.  Either way the
kernel never sees a refusal, and the worker retakes the operation right
after each group, before a waiter can get in: on the lane a queued resize
waited the whole churn (0 of 40 one-shot resizes succeeded, the longest
refusal 601 s).

After each group that was relocated (or failed to be), release the
operation (btrfs_exclop_finish() sends the sysfs notification) and, if
more groups are queued, leave it free for BTRFS_RECLAIM_GAP (12 s)
before taking it back; if someone took it meanwhile, leave the rest of
the list for a later run.  A group that was skipped or dropped (the
backoff, or the checks before relocation) held the operation only
briefly and gets no gap.  Neither does a filesystem that is closing or
has failed: the worker only drops the rest of the list there, and
close_ctree() waits for it in cancel_work_sync().  Nor does the last
group a limited call may reclaim (the zoned RECLAIM_ZONES flush): the
loop ends there and releases the operation anyway.  12 s comes from
btrfs-progs: after the notification, check_running_fs_exclop() reopens
the file and sleeps in a second select() for (remaining % 10) + 1 s "to
randomize the order" before it re-reads the operation, so a waiter can
take up to 10 s, plus wakeup latency under load, to get to the ioctl;
with a 5 s gap about half the queued waiters woke after the worker had
retaken the operation and waited another group.  A progs change that
re-reads right after the notification (and randomizes only when it loses
a race) would allow a much shorter gap.  With nobody waiting the gap
costs only reclaim throughput: reclaim resumes after it.

A caller that does reach btrfs_exclop_start() while the worker holds
the operation (an ioctl user that does not pre-check sysfs) opens a
BTRFS_RECLAIM_YIELD_WINDOW (60 s, longer than such a caller's retry
interval and short next to the five-minute reclaim hysteresis) in which
the worker neither starts nor takes another group.  Both are backoffs
only: no drain or accounting wait is bounded by them.

A group in progress is not interrupted, so a queued operation waits for
the rest of the current group plus at most one gap.  Measured (before
the gap was limited to relocated groups) on a stripe_alloc filesystem,
whose trapped space keeps the reclaim list full at the fill edge, with
reclaim-futile-test.sh (6 x 1 GiB raid5 at the fill edge, 128 MiB per
member added, "all" written to stripe_reclaim_now, 600 s of fsstress, a
no-op `btrfs filesystem resize` every 15 s and a `--enqueue` one every
60 s), three traced runs per kernel:

  without this patch: the worker held the operation for a whole sweep
  (184, 176 and 616 s); the first queued resize waited 184, 176 and about
  655 s, i.e. for every queued group; one-shot resizes 27/40, 28/40, 0/40.

  with it: every queued resize got in at the first hand-off after it
  arrived (one sat through three short groups, 23 s), 0.3-9.4 s after the
  worker's finish; longest queued waits 82, 11 and 23 s (the 82 s one sat
  behind a single 127 s group); one-shot resizes 12/40, 31/40, 27/40.

Reclaim kept its pace: 10, 10 and 14 groups per 10 minutes without the
patch, 13, 11 and 10 with it.

Assisted-by: Claude:claude-opus-5-5
fs/btrfs/block-group.c
fs/btrfs/fs.c
fs/btrfs/fs.h