]> git.hungrycats.org Git - linux/commitdiff
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)
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

index 4333b3067a44f59b2f686bf98d932106a8dd1818..183d31408b2c0671e4ee15a186237551670c92db 100644 (file)
@@ -5695,6 +5695,23 @@ static void reclaim_note_result(struct btrfs_block_group *bg, bool enospc, u64 n
                            streak, bg->start, space_info->flags);
 }
 
+/* A refused operation's retry window is open: see btrfs_exclop_start(). */
+static bool reclaim_yielding(const struct btrfs_fs_info *fs_info)
+{
+       const unsigned long until = READ_ONCE(fs_info->reclaim_yield_until);
+
+       return until && time_before(jiffies, until);
+}
+
+/*
+ * Unmounting, or read-only after an error: every group taken from here on is
+ * dropped (see btrfs_need_cleaner_sleep()), so do not wait out a gap for them.
+ */
+static bool reclaim_stopping(const struct btrfs_fs_info *fs_info)
+{
+       return btrfs_need_cleaner_sleep(fs_info) || BTRFS_FS_ERROR(fs_info);
+}
+
 void btrfs_reclaim_block_groups(struct btrfs_fs_info *fs_info, unsigned int limit)
 {
        struct btrfs_block_group *bg;
@@ -5712,9 +5729,12 @@ void btrfs_reclaim_block_groups(struct btrfs_fs_info *fs_info, unsigned int limi
        if (!btrfs_should_reclaim(fs_info))
                return;
 
+       if (reclaim_yielding(fs_info))
+               return;
+
        guard(super_write)(fs_info->sb);
 
-       if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE))
+       if (!btrfs_exclop_start_reclaim(fs_info))
                return;
 
        /*
@@ -5735,6 +5755,7 @@ void btrfs_reclaim_block_groups(struct btrfs_fs_info *fs_info, unsigned int limi
        list_sort(NULL, &fs_info->reclaim_bgs, reclaim_bgs_cmp);
        while (!list_empty(&fs_info->reclaim_bgs)) {
                u64 need;
+               bool handoff = false;
                int ret;
 
                bg = list_first_entry(&fs_info->reclaim_bgs,
@@ -5759,6 +5780,13 @@ void btrfs_reclaim_block_groups(struct btrfs_fs_info *fs_info, unsigned int limi
                        ret = btrfs_reclaim_block_group(bg, &reclaimed);
                        if (ret == -ENOSPC || reclaimed > before)
                                reclaim_note_result(bg, ret == -ENOSPC, need);
+                       /*
+                        * It was relocated, or failed to be (an error also
+                        * comes from setting it read-only, which is quick).
+                        * Once the limit is reached the loop ends below and
+                        * releases the operation anyway: no gap.
+                        */
+                       handoff = (ret || reclaimed > before) && reclaimed < limit;
                }
 
                /* An ENOSPC failure is not retried at once; see above. */
@@ -5769,11 +5797,46 @@ void btrfs_reclaim_block_groups(struct btrfs_fs_info *fs_info, unsigned int limi
                btrfs_put_block_group(bg);
 
                mutex_unlock(&fs_info->reclaim_bgs_lock);
+               /*
+                * After a group that was relocated, let a balance, replace or
+                * resize that is waiting for the exclusive operation have it: a
+                * long reclaim list must not lock them out for its whole run.
+                * A group that was skipped or dropped held it only briefly.
+                */
+               if (handoff)
+                       btrfs_exclop_finish(fs_info);
                /*
                 * Reclaiming all the block groups in the list can take really
                 * long.  Prioritize cleaning up unused block groups.
                 */
                btrfs_delete_unused_bgs(fs_info);
+               if (handoff) {
+                       if (reclaim_stopping(fs_info))
+                               goto out;
+                       /*
+                        * Leave the exclusive operation free for a moment
+                        * before the next group.  btrfs-progs reads sysfs
+                        * exclusive_operation before it calls the ioctl:
+                        * without --enqueue it gives up there, and with it it
+                        * waits for the notification that btrfs_exclop_finish()
+                        * just sent.  Either way the refusal never reaches
+                        * btrfs_exclop_start(), so only a gap lets a balance,
+                        * replace or resize in while the list is long.
+                        */
+                       if (!list_empty_careful(&fs_info->reclaim_bgs))
+                               schedule_timeout_idle(BTRFS_RECLAIM_GAP);
+                       /*
+                        * Someone was refused the exclusive operation while
+                        * we held it: leave the rest of the list to a run
+                        * after their retry; stop as well if the filesystem
+                        * started closing or failed meanwhile.
+                        */
+                       if (reclaim_yielding(fs_info) ||
+                           reclaim_stopping(fs_info))
+                               goto out;
+                       if (!btrfs_exclop_start_reclaim(fs_info))
+                               goto out;
+               }
                /*
                 * If we are interrupted by a balance, we can just bail out. The
                 * cleaner thread restart again if necessary.
@@ -5787,10 +5850,11 @@ void btrfs_reclaim_block_groups(struct btrfs_fs_info *fs_info, unsigned int limi
        spin_unlock(&fs_info->unused_bgs_lock);
        mutex_unlock(&fs_info->reclaim_bgs_lock);
 end:
+       btrfs_exclop_finish(fs_info);
+out:
        spin_lock(&fs_info->unused_bgs_lock);
        list_splice_tail(&retry_list, &fs_info->reclaim_bgs);
        spin_unlock(&fs_info->unused_bgs_lock);
-       btrfs_exclop_finish(fs_info);
 }
 
 void btrfs_reclaim_bgs_work(struct work_struct *work)
index 75a1217727a7bb84f7a82da58d2316358b95665a..9a36fbd24e318fadc7343a334bc067abb0ece5ee 100644 (file)
@@ -183,6 +183,34 @@ bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
        if (fs_info->exclusive_operation == BTRFS_EXCLOP_NONE) {
                fs_info->exclusive_operation = type;
                ret = true;
+       } else if (test_bit(BTRFS_FS_RECLAIM_EXCLOP, &fs_info->flags)) {
+               /*
+                * The block group reclaim worker holds it.  It is background
+                * work: have it stand aside after its current group, long
+                * enough for the caller to retry.
+                */
+               WRITE_ONCE(fs_info->reclaim_yield_until,
+                          (jiffies + BTRFS_RECLAIM_YIELD_WINDOW) ?: 1);
+       }
+       spin_unlock(&fs_info->super_lock);
+
+       return ret;
+}
+
+/*
+ * The block group reclaim worker's balance exclusive operation: marked as
+ * the worker's, so that an operation it refuses makes it yield (see
+ * btrfs_exclop_start()).
+ */
+bool btrfs_exclop_start_reclaim(struct btrfs_fs_info *fs_info)
+{
+       bool ret = false;
+
+       spin_lock(&fs_info->super_lock);
+       if (fs_info->exclusive_operation == BTRFS_EXCLOP_NONE) {
+               fs_info->exclusive_operation = BTRFS_EXCLOP_BALANCE;
+               set_bit(BTRFS_FS_RECLAIM_EXCLOP, &fs_info->flags);
+               ret = true;
        }
        spin_unlock(&fs_info->super_lock);
 
@@ -222,6 +250,7 @@ void btrfs_exclop_finish(struct btrfs_fs_info *fs_info)
 {
        spin_lock(&fs_info->super_lock);
        WRITE_ONCE(fs_info->exclusive_operation, BTRFS_EXCLOP_NONE);
+       clear_bit(BTRFS_FS_RECLAIM_EXCLOP, &fs_info->flags);
        spin_unlock(&fs_info->super_lock);
        sysfs_notify(&fs_info->fs_devices->fsid_kobj, NULL, "exclusive_operation");
 }
index edb2202d17c4b8491fa2347af7f759e0530992ed..1bce054bd4896d74392e350b2ce8ca531652322e 100644 (file)
@@ -241,6 +241,9 @@ enum {
         */
        BTRFS_FS_STRIPE_ALLOC_ENABLING,
 
+       /* The block group reclaim worker holds the exclusive operation. */
+       BTRFS_FS_RECLAIM_EXCLOP,
+
 #if BITS_PER_LONG == 32
        /* Indicate if we have error/warn message printed on 32bit systems */
        BTRFS_FS_32BIT_ERROR,
@@ -1125,6 +1128,13 @@ struct btrfs_fs_info {
 
        /* Reclaim partially filled block groups in the background */
        struct work_struct reclaim_bgs_work;
+       /*
+        * A balance, replace, resize or device add/remove was refused while
+        * the reclaim worker held the exclusive operation: until this jiffies
+        * value the worker neither starts nor takes another group, so that the
+        * caller's retry gets the operation.
+        */
+       unsigned long reclaim_yield_until;
        /* Protected by unused_bgs_lock. */
        struct list_head reclaim_bgs;
        int bg_reclaim_threshold;
@@ -1368,6 +1378,12 @@ static inline unsigned int btrfs_blocks_per_folio(const struct btrfs_fs_info *fs
 }
 
 bool __attribute_const__ btrfs_supported_blocksize(u32 blocksize);
+/* How long the reclaim worker stands aside after a refused operation. */
+#define BTRFS_RECLAIM_YIELD_WINDOW     (60 * HZ)
+/* How long it leaves the exclusive operation free between two groups. */
+#define BTRFS_RECLAIM_GAP              (12 * HZ)
+
+bool btrfs_exclop_start_reclaim(struct btrfs_fs_info *fs_info);
 bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
                        enum btrfs_exclusive_operation type);
 bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info,