]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_alloc: count refused rbio pins and track rbio cache high water
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 4 Oct 2026 16:26:45 +0000 (12:26 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:37:53 +0000 (18:37 -0500)
The rbio cache keeps a stripe its run has not finished writing pinned, at
most RBIO_PIN_MAX (256) of them; the next pin is refused and that stripe
is cached evictable.  If it is evicted before its run's next write into
it, that write reads back the sectors already written, unverified: the
case the pin exists to avoid.  Nothing counted the refusals, so whether
the limit is ever reached was unknown.

stripe_park_stats gains:
  rbio_pin_refused   pins refused at RBIO_PIN_MAX (every attempt)
  rbio_pinned_now    pinned rbios now (nr_pinned: what the limit counts)
  rbio_pinned_live   of those, pins still needed: re-checked against the
                     runs at read time, as the prune does.  A pin whose run
                     has finished the stripe stays counted until its entry
                     is evicted or cached again.
  rbio_pinned_hwm    high water of pinned rbios since mount
  rbio_cache_now     cached rbios now
  rbio_cache_hwm     high water of cached rbios since mount (above
                     RBIO_CACHE_SIZE only by live pins, which the prune
                     keeps)
  meta_rmw_reads     read-modify-writes of metadata/system stripes that had
                     to read (data_rmw counts these with the data ones).
                     The pin is what keeps a stripe of fsync log blocks
                     cached for the next log commit; a refused pin can bring
                     that read back.

The high water marks are only raised under the cache lock.  Reading the
file walks the cache once under the cache lock (1024 entries at most).

nr_pinned counts a pin until its entry is evicted or cached again, also
after its run has finished the stripe, so the limit filled with stale pins
within seconds under any fsync-heavy load (13k-87k refusals a minute)
while buffered writeback needed 3-14.  Two things release pins:

- A sweep, rbio_unpin_stale(), re-checks every pin against its run and
  clears the stale ones under the cache lock.  It runs when a pin is about
  to be taken with RBIO_PIN_LOW (3/4 of the limit) or more counted, at
  most once per RBIO_PIN_SWEEP_INTERVAL from that path, and at every
  commit right after btrfs_retire_open_stripes(), which ends every run.
- If the pins left after a sweep (all live) are still RBIO_PIN_LOW or
  more, the transaction kthread is asked to commit now, once per
  transaction.  After the commit a stripe's earlier writes are no longer
  at risk from a read-back: data checksums are committed and the log
  blocks are dead.

stripe_park_stats also has rbio_pin_swept and rbio_pin_commit_requests.

Assisted-by: Claude:claude-opus-5-5
Assisted-by: Claude:claude-fable-5
fs/btrfs/fs.h
fs/btrfs/raid56.c
fs/btrfs/raid56.h
fs/btrfs/sysfs.c
fs/btrfs/transaction.c

index e32819ade071a0f0d188207b474274a1f72ded19..8a21d9004e265a413e95925ee3b3e52833a904e8 100644 (file)
@@ -859,6 +859,25 @@ struct btrfs_fs_info {
                atomic64_t congestion_short;
                /* Stripes pinned in the rbio cache (rbio_needs_pin()). */
                atomic64_t rbio_pinned;
+               /*
+                * Pins refused because RBIO_PIN_MAX stripes were already
+                * pinned: the stripe was cached evictable instead, and a later
+                * write into it may read back the part already written.
+                */
+               atomic64_t rbio_pin_refused;
+               /* High water since mount of pinned and of all cached rbios. */
+               atomic64_t rbio_pinned_hwm;
+               atomic64_t rbio_cache_hwm;
+               /* Stale pins cleared by a sweep (rbio_unpin_stale()). */
+               atomic64_t rbio_pin_swept;
+               /* Commits requested because live pins ran low. */
+               atomic64_t rbio_pin_commit_requests;
+               /*
+                * Read-modify-writes of metadata or system stripes that had to
+                * read (data_rmw counts them with the data ones): the fsync log
+                * stripes the pin keeps cached read back when it is refused.
+                */
+               atomic64_t meta_rmw_reads;
                /* Completed-write sectors not live in their run (should be ~0). */
                atomic64_t written_not_live;
        } stripe_park_stats;
index 5f56d1e19e749fdaa4665687ad7b5b4ac6de10f8..81ba7c826438227fddb1e45b84a1cd7d9600660d 100644 (file)
  */
 #define RBIO_PINNED_BIT                9
 
-/* At most this many pinned stripes; normally about one per stripe run. */
+/*
+ * At most this many pinned stripes: about as many as stripes with writes in
+ * flight, but fsync-heavy work caches a partial stripe on nearly every write.
+ * A pin goes stale when its run finishes the stripe and is only uncounted
+ * when a sweep, an eviction or a re-cache finds it, so pinning past
+ * RBIO_PIN_LOW sweeps first, and if the pins left are still live, asks for a
+ * commit: the commit retires the runs (btrfs_retire_open_stripes(), and
+ * btrfs_retire_meta_stripes() for metadata), which makes their pins
+ * unnecessary, and its sweep releases them.
+ */
 #define RBIO_PIN_MAX           256
+#define RBIO_PIN_LOW           (RBIO_PIN_MAX * 3 / 4)
+/* At most one sweep per this interval from the pin path (commits always sweep). */
+#define RBIO_PIN_SWEEP_INTERVAL        (HZ / 20)
 
 #define RBIO_CACHE_SIZE 1024
 
@@ -182,6 +194,9 @@ struct btrfs_stripe_hash_table {
        int cache_size;
        /* Cached rbios with RBIO_PINNED_BIT set, live or not. */
        int nr_pinned;
+       /* Last pin sweep, and the generation the last commit request was for. */
+       unsigned long pin_sweep_jiffies;
+       u64 pin_commit_gen;
        /*
         * Parked partial write rbios (RBIO_PARKED_BIT), in park order.
         * parked_lock nests inside the hash and bio_list locks.
@@ -719,19 +734,31 @@ static void rbio_mark_written(struct btrfs_raid_bio *rbio)
        }
 }
 
+static void rbio_stat_hwm(atomic64_t *hwm, int now)
+{
+       /* Only ever raised under the cache lock: no lost update. */
+       if (now > atomic64_read(hwm))
+               atomic64_set(hwm, now);
+}
+
 /*
  * Mark @rbio pinned or not; caller holds the cache lock and @rbio is in the
- * cache.
+ * cache.  A pin refused at RBIO_PIN_MAX is counted at every attempt.
  */
 static void rbio_set_pin(struct btrfs_stripe_hash_table *table,
                         struct btrfs_raid_bio *rbio, bool pin)
 {
        if (pin && table->nr_pinned >= RBIO_PIN_MAX &&
-           !test_bit(RBIO_PINNED_BIT, &rbio->flags))
+           !test_bit(RBIO_PINNED_BIT, &rbio->flags)) {
+               atomic64_inc(&table->fs_info->stripe_park_stats.rbio_pin_refused);
                pin = false;
+       }
        if (pin) {
-               if (!test_and_set_bit(RBIO_PINNED_BIT, &rbio->flags))
+               if (!test_and_set_bit(RBIO_PINNED_BIT, &rbio->flags)) {
                        table->nr_pinned += 1;
+                       rbio_stat_hwm(&table->fs_info->stripe_park_stats.rbio_pinned_hwm,
+                                     table->nr_pinned);
+               }
                atomic64_inc(&rbio->bioc->fs_info->stripe_park_stats.rbio_pinned);
        } else if (test_and_clear_bit(RBIO_PINNED_BIT, &rbio->flags)) {
                table->nr_pinned -= 1;
@@ -756,6 +783,102 @@ static void rbio_cache_prune(struct btrfs_stripe_hash_table *table,
                /* __remove_rbio_from_cache() drops a stale pin's count. */
                __remove_rbio_from_cache(found);
        }
+       /* Above the limit only by live pins, which the prune keeps. */
+       rbio_stat_hwm(&table->fs_info->stripe_park_stats.rbio_cache_hwm,
+                     table->cache_size);
+}
+
+/*
+ * Clear the pins no longer needed (their run has written all of the stripe,
+ * or is gone).  Caller holds the cache lock, the same context the prune
+ * re-checks pins in.  Returns how many pins are left: all of them live.
+ */
+static int rbio_unpin_stale(struct btrfs_stripe_hash_table *table)
+{
+       struct btrfs_raid_bio *rbio;
+       int swept = 0;
+
+       table->pin_sweep_jiffies = jiffies;
+       list_for_each_entry(rbio, &table->stripe_cache, stripe_cache) {
+               if (!test_bit(RBIO_PINNED_BIT, &rbio->flags) || rbio_needs_pin(rbio))
+                       continue;
+               clear_bit(RBIO_PINNED_BIT, &rbio->flags);
+               table->nr_pinned -= 1;
+               swept++;
+       }
+       if (swept)
+               atomic64_add(swept, &table->fs_info->stripe_park_stats.rbio_pin_swept);
+       return table->nr_pinned;
+}
+
+/*
+ * About to pin with the pins running low: sweep the stale ones, and if the
+ * live ones are still many, ask for a commit (once per transaction), which
+ * ends every run and so every pin.  Caller holds the cache lock.
+ */
+static void rbio_pins_low(struct btrfs_stripe_hash_table *table)
+{
+       struct btrfs_fs_info *fs_info = table->fs_info;
+       u64 gen;
+
+       /*
+        * Rate limited: a sweep walks every pin, and only a commit frees live
+        * ones.
+        */
+       if (!time_after(jiffies, table->pin_sweep_jiffies + RBIO_PIN_SWEEP_INTERVAL))
+               return;
+       if (rbio_unpin_stale(table) < RBIO_PIN_LOW)
+               return;
+       gen = btrfs_get_fs_generation(fs_info);
+       if (table->pin_commit_gen == gen)
+               return;
+       table->pin_commit_gen = gen;
+       atomic64_inc(&fs_info->stripe_park_stats.rbio_pin_commit_requests);
+       /* As btrfs_commit_transaction_async(): the kthread commits now. */
+       set_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags);
+       wake_up_process(fs_info->transaction_kthread);
+}
+
+/*
+ * A commit has retired every open stripe run: release the pins that leaves
+ * stale.  Pins of runs opened since then are re-checked, not dropped.
+ */
+void btrfs_rbio_unpin_stale(struct btrfs_fs_info *fs_info)
+{
+       struct btrfs_stripe_hash_table *table = fs_info->stripe_hash_table;
+
+       if (!table || !btrfs_test_opt(fs_info, STRIPE_ALLOC))
+               return;
+       spin_lock(&table->cache_lock);
+       if (table->nr_pinned)
+               rbio_unpin_stale(table);
+       spin_unlock(&table->cache_lock);
+}
+
+/*
+ * Cached and pinned rbios now, for sysfs stripe_park_stats.  @live re-checks
+ * each pin against its run, as the prune does: a pin whose run has finished
+ * writing the stripe is still counted in nr_pinned until a sweep, an
+ * eviction or a re-cache finds it.
+ */
+void btrfs_rbio_cache_usage(struct btrfs_fs_info *fs_info, int *cached,
+                           int *pinned, int *live)
+{
+       struct btrfs_stripe_hash_table *table = fs_info->stripe_hash_table;
+       struct btrfs_raid_bio *rbio;
+
+       *cached = 0;
+       *pinned = 0;
+       *live = 0;
+       if (!table)
+               return;
+       spin_lock(&table->cache_lock);
+       *cached = table->cache_size;
+       *pinned = table->nr_pinned;
+       list_for_each_entry(rbio, &table->stripe_cache, stripe_cache)
+               if (rbio_pin_live(rbio))
+                       *live += 1;
+       spin_unlock(&table->cache_lock);
 }
 
 /*
@@ -781,6 +904,9 @@ static void cache_rbio(struct btrfs_raid_bio *rbio)
        table = rbio->bioc->fs_info->stripe_hash_table;
 
        spin_lock(&table->cache_lock);
+       if (pin && table->nr_pinned >= RBIO_PIN_LOW &&
+           !test_bit(RBIO_PINNED_BIT, &rbio->flags))
+               rbio_pins_low(table);
        spin_lock(&rbio->bio_list_lock);
 
        /* bump our ref if we were not in the list before */
@@ -3139,6 +3265,8 @@ static void rmw_rbio(struct btrfs_raid_bio *rbio)
                 * count that should be zero.
                 */
                atomic64_inc(&fs_info->stripe_park_stats.data_rmw);
+               if (!(rbio->bioc->map_type & BTRFS_BLOCK_GROUP_DATA))
+                       atomic64_inc(&fs_info->stripe_park_stats.meta_rmw_reads);
                /* A parked-then-flushed write that still had to read. */
                if (test_bit(RBIO_WAS_PARKED_BIT, &rbio->flags))
                        atomic64_inc(&fs_info->stripe_park_stats.rmw_reads);
index 2d0c7f9751a717198466a5592d22d282f6865341..b0cb4e0dbc4bb1a16e003efa78d0e5539766207b 100644 (file)
@@ -301,5 +301,8 @@ void btrfs_free_stripe_hash_table(struct btrfs_fs_info *info);
 void btrfs_flush_parked_rbios(struct btrfs_fs_info *fs_info, u64 start,
                              u64 num_bytes, bool force);
 void btrfs_stop_parked_rbios(struct btrfs_fs_info *fs_info);
+void btrfs_rbio_cache_usage(struct btrfs_fs_info *fs_info, int *cached,
+                           int *pinned, int *live);
+void btrfs_rbio_unpin_stale(struct btrfs_fs_info *fs_info);
 
 #endif
index 102abae2fe1595abfe570a960b80ee221c0749dd..f77bdecee174626974597bb13bfa52e8f78566ef 100644 (file)
@@ -25,6 +25,7 @@
 #include "misc.h"
 #include "fs.h"
 #include "accessors.h"
+#include "raid56.h"
 
 /*
  * Structure name                       Path
@@ -1235,7 +1236,9 @@ static ssize_t btrfs_stripe_park_stats_show(struct kobject *kobj,
                                            struct kobj_attribute *a, char *buf)
 {
        struct btrfs_fs_info *fs_info = to_fs_info(kobj);
+       int cached, pinned, live;
 
+       btrfs_rbio_cache_usage(fs_info, &cached, &pinned, &live);
        return sysfs_emit(buf,
                "parked %lld\n"
                "merged_bytes %lld\n"
@@ -1246,7 +1249,16 @@ static ssize_t btrfs_stripe_park_stats_show(struct kobject *kobj,
                "stuck %lld\n"
                "padded %lld\n"
                "padded_unwritten %lld\n"
-               "rmw_reads %lld\n",
+               "rmw_reads %lld\n"
+               "rbio_pin_refused %lld\n"
+               "rbio_pinned_now %d\n"
+               "rbio_pinned_live %d\n"
+               "rbio_pinned_hwm %lld\n"
+               "rbio_cache_now %d\n"
+               "rbio_cache_hwm %lld\n"
+               "rbio_pin_swept %lld\n"
+               "rbio_pin_commit_requests %lld\n"
+               "meta_rmw_reads %lld\n",
                atomic64_read(&fs_info->stripe_park_stats.parked),
                atomic64_read(&fs_info->stripe_park_stats.merged_bytes),
                atomic64_read(&fs_info->stripe_park_stats.filled),
@@ -1256,7 +1268,16 @@ static ssize_t btrfs_stripe_park_stats_show(struct kobject *kobj,
                atomic64_read(&fs_info->stripe_park_stats.stuck),
                atomic64_read(&fs_info->stripe_park_stats.padded),
                atomic64_read(&fs_info->stripe_park_stats.padded_unwritten),
-               atomic64_read(&fs_info->stripe_park_stats.rmw_reads));
+               atomic64_read(&fs_info->stripe_park_stats.rmw_reads),
+               atomic64_read(&fs_info->stripe_park_stats.rbio_pin_refused),
+               pinned,
+               live,
+               atomic64_read(&fs_info->stripe_park_stats.rbio_pinned_hwm),
+               cached,
+               atomic64_read(&fs_info->stripe_park_stats.rbio_cache_hwm),
+               atomic64_read(&fs_info->stripe_park_stats.rbio_pin_swept),
+               atomic64_read(&fs_info->stripe_park_stats.rbio_pin_commit_requests),
+               atomic64_read(&fs_info->stripe_park_stats.meta_rmw_reads));
 }
 BTRFS_ATTR(, stripe_park_stats, btrfs_stripe_park_stats_show);
 
index 0dca96f99c5b7a30376760ebc16e846b7a09c167..32ff990ec04d2393857cd858523dc4ba921587e7 100644 (file)
@@ -23,6 +23,7 @@
 #include "dev-replace.h"
 #include "qgroup.h"
 #include "block-group.h"
+#include "raid56.h"
 #include "space-info.h"
 #include "fs.h"
 #include "accessors.h"
@@ -2459,6 +2460,8 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
         * no-op when no stripe runs exist.
         */
        btrfs_retire_open_stripes(fs_info, cur_trans);
+       /* Every run just closed: the rbio cache's pins on them are stale. */
+       btrfs_rbio_unpin_stale(fs_info);
 
        btrfs_scrub_pause(fs_info);
        /*