]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_alloc: count open stripe run remainders against data reservations
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sat, 19 Sep 2026 17:17:45 +0000 (13:17 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:37:45 +0000 (18:37 -0500)
Close the remaining reservation-vs-allocation windows the commit-time
stripe_unusable rescan cannot see:

- bytes_stripe_open (new): the sum of open runs' unallocated
  remainders, maintained per block group under stripe_run_lock at
  every open/alloc/grow/close, synced into the space_info after each
  mutation, and counted in btrfs_space_info_used().  Claimed bytes are
  invisible to the free space cache and will either be allocated or
  become trapped tails at close, so reservations must not be admitted
  against them.

A closed run's returned tail is still only counted as trapped by the
commit-time rescan, so reservations can race the retire-to-rescan window
inside a commit and be admitted against freshly trapped tails.  The next
patch closes that window from the free space cache side, where every
returning range is seen and no call site can be missed.

Measured on the fill-to-ENOSPC rolling test before this change (with
only the bytes_stripe_unusable accounting): tree 'b' still lost 3.3%
(8532 blocks) and tree 'c' 30% (62371 blocks) to silent writeback
allocation failure as trapping compounded.

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

index 7532a2d99f9850e825dab47fc9c8277da4e656eb..9a13ee9e3d936798ffcd58917439a49c59d30eb2 100644 (file)
@@ -548,11 +548,46 @@ static u64 close_open_stripe_run(struct btrfs_block_group *bg,
        *tail_start = run->offset;
        tail_len = run->end - run->offset;
        run->end = run->offset;
+       bg->stripe_open_remainder -= tail_len;
        if (open_stripe_run_drained(run))
                free_open_stripe_run(bg, run);
        return tail_len;
 }
 
+/*
+ * Fold this block group's open-run remainder delta into the space_info
+ * counter that reservations see.  Bytes inside an open run are invisible
+ * to the free space cache (the claim removed them) and will either be
+ * allocated or become trapped tails when the run closes, so the data
+ * reservation path must not admit writes against them: a reservation
+ * that cannot allocate at writeback time discards the pages silently.
+ * Called after every remainder mutation.  Compute and apply are one
+ * critical section (sinfo->lock nests inside stripe_run_lock): applied
+ * out of order, a negative delta could overtake the positive it was
+ * computed against and transiently underflow the space_info sum.
+ */
+static void stripe_open_remainder_sync(struct btrfs_block_group *bg)
+{
+       struct btrfs_space_info *sinfo = bg->space_info;
+       unsigned long flags;
+       s64 delta;
+
+       spin_lock_irqsave(&bg->stripe_run_lock, flags);
+       delta = (s64)(bg->stripe_open_remainder - bg->stripe_open_reported);
+       bg->stripe_open_reported = bg->stripe_open_remainder;
+       if (delta && sinfo) {
+               spin_lock(&sinfo->lock);
+               if (delta < 0 && sinfo->bytes_stripe_open < (u64)-delta) {
+                       WARN_ON_ONCE(1);
+                       sinfo->bytes_stripe_open = 0;
+               } else {
+                       sinfo->bytes_stripe_open += delta;
+               }
+               spin_unlock(&sinfo->lock);
+       }
+       spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+}
+
 /*
  * Install @run (not currently in any band slot) into its remaining-space band.
  * If a run already occupies that band, keep whichever has more remaining -- it
@@ -643,6 +678,7 @@ int btrfs_alloc_from_open_stripe(struct btrfs_block_group *bg, u64 num_bytes,
                *ret_offset = run->offset;
                run->offset += num_bytes;
                run->inflight_bytes += num_bytes;
+               bg->stripe_open_remainder -= num_bytes;
                if (run->offset == run->end)
                        run->open = false;      /* full: closed, no tail */
                else
@@ -650,6 +686,7 @@ int btrfs_alloc_from_open_stripe(struct btrfs_block_group *bg, u64 num_bytes,
                spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
                if (tail_len)
                        btrfs_add_free_space(bg, tail_start, tail_len);
+               stripe_open_remainder_sync(bg);
                return 0;
        }
        spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
@@ -724,6 +761,8 @@ int btrfs_alloc_from_open_stripe(struct btrfs_block_group *bg, u64 num_bytes,
                r->end = start + len;
                r->offset += num_bytes;
                r->inflight_bytes += num_bytes;
+               bg->stripe_open_remainder += len;
+               bg->stripe_open_remainder -= num_bytes;
                if (r->offset == r->end)
                        r->open = false;        /* full: closed, no tail */
                else
@@ -732,6 +771,7 @@ int btrfs_alloc_from_open_stripe(struct btrfs_block_group *bg, u64 num_bytes,
                spin_unlock(&fs_info->open_stripe_lock);
                if (tail_len)
                        btrfs_add_free_space(bg, tail_start, tail_len);
+               stripe_open_remainder_sync(bg);
                ret = 0;
                goto out;       /* frees the unused new_run */
        }
@@ -745,6 +785,7 @@ int btrfs_alloc_from_open_stripe(struct btrfs_block_group *bg, u64 num_bytes,
        new_run->open_seq = fs_info->stripe_retire_seq;
        new_run->open = (new_run->offset != new_run->end);
        list_add_tail(&new_run->list, &bg->open_stripe_runs);
+       bg->stripe_open_remainder += new_run->end - new_run->offset;
        /*
         * Place the new run in its remaining-space band, closing whichever run
         * loses a band collision (the last, fuller opener wins).  Other bands'
@@ -757,6 +798,7 @@ int btrfs_alloc_from_open_stripe(struct btrfs_block_group *bg, u64 num_bytes,
 
        if (tail_len)
                btrfs_add_free_space(bg, tail_start, tail_len);
+       stripe_open_remainder_sync(bg);
        *ret_offset = start;
        new_run = NULL;
        ret = 0;
@@ -809,17 +851,21 @@ int btrfs_alloc_from_inode_stripe_run(struct btrfs_block_group *bg, u64 ino,
                        *ret_offset = run->offset;
                        run->offset += num_bytes;
                        run->inflight_bytes += num_bytes;
+                       bg->stripe_open_remainder -= num_bytes;
                        if (run->offset == run->end)
                                run->open = false; /* full: closed, no tail */
                        spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+                       stripe_open_remainder_sync(bg);
                        return 0;
                }
                tail_len = close_open_stripe_run(bg, run, &tail_start);
                break;
        }
        spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
-       if (tail_len)
+       if (tail_len) {
                btrfs_add_free_space(bg, tail_start, tail_len);
+               stripe_open_remainder_sync(bg);
+       }
 
        new_run = kmalloc(sizeof(*new_run), GFP_NOFS);
        if (!new_run)
@@ -864,9 +910,11 @@ int btrfs_alloc_from_inode_stripe_run(struct btrfs_block_group *bg, u64 ino,
        new_run->open_seq = fs_info->stripe_retire_seq;
        new_run->open = (new_run->offset != new_run->end);
        list_add_tail(&new_run->list, &bg->open_stripe_runs);
+       bg->stripe_open_remainder += new_run->end - new_run->offset;
        spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
        spin_unlock(&fs_info->open_stripe_lock);
 
+       stripe_open_remainder_sync(bg);
        *ret_offset = start;
        new_run = NULL;
        ret = 0;
@@ -1227,6 +1275,7 @@ restart:
                spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
                if (tail_len)
                        btrfs_add_free_space(bg, tail_start, tail_len);
+               stripe_open_remainder_sync(bg);
                goto restart;
        }
        spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
index b2871b6f7443b7cf63c83fd6b3b52ca6b219a4c2..afc11c2249ac5f2745e46148f1d010de1c92d3d7 100644 (file)
@@ -251,6 +251,14 @@ struct btrfs_block_group {
        struct list_head open_stripe_runs;
        struct btrfs_open_stripe_run
                *open_stripe[BTRFS_STRIPE_RUN_NR_CLASSES][BTRFS_STRIPE_RUN_NR_BANDS];
+       /*
+        * Sum of the open runs' unallocated remainders (end - offset), and
+        * the portion of it already folded into the space_info's
+        * bytes_stripe_open.  Both under stripe_run_lock; synced by
+        * stripe_open_remainder_sync() after every mutation.
+        */
+       u64 stripe_open_remainder;
+       u64 stripe_open_reported;
        /*
         * Membership in fs_info->open_stripe_bgs, protected by
         * fs_info->open_stripe_lock; holds a block group reference.
index b6f0bf87b58a59af1c720a2ef70fe94dd29cf901..344ce480099c6e71534df4cdf1c2c86d562482a8 100644 (file)
@@ -154,12 +154,20 @@ struct btrfs_space_info {
        u64 bytes_readonly;     /* total bytes that are read only */
        u64 bytes_zone_unusable;        /* total bytes that are unusable until
                                           resetting the device zone */
-       u64 bytes_stripe_unusable;      /* total free bytes trapped in partially
-                                          filled stripes of stripe_alloc block
-                                          groups; free but not allocatable until
-                                          the whole stripe frees.  Informational
-                                          only (subtracted in statfs); NOT part
-                                          of btrfs_space_info_used(). */
+       u64 bytes_stripe_unusable;      /* free bytes stripe_alloc and
+                                          stripe_meta groups cannot allocate:
+                                          partially filled stripes, and whole
+                                          stripes stranded behind live runs.
+                                          Counted as used, and subtracted in
+                                          statfs. */
+       u64 bytes_stripe_open;          /* sum of open stripe runs' unallocated
+                                          remainders.  Claimed out of the free
+                                          space cache but not yet allocated;
+                                          will either be allocated or become
+                                          trapped tails when the runs close, so
+                                          reservations must not count on it
+                                          either.  Synced from the block groups'
+                                          stripe_open_remainder. */
 
        u64 max_extent_size;    /* This will hold the maximum extent size of
                                   the space info if we had an ENOSPC in the
@@ -319,7 +327,7 @@ static inline u64 btrfs_space_info_used(const struct btrfs_space_info *s_info,
        return s_info->bytes_used + s_info->bytes_reserved +
                s_info->bytes_pinned + s_info->bytes_readonly +
                s_info->bytes_zone_unusable +
-               s_info->bytes_stripe_unusable +
+               s_info->bytes_stripe_unusable + s_info->bytes_stripe_open +
                (may_use_included ? s_info->bytes_may_use : 0);
 }