]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_alloc: break rbio pins and open stripe runs down by run class
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 4 Oct 2026 21:46:19 +0000 (17:46 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:38:08 +0000 (18:38 -0500)
Under fsync load the rbio cache holds up to ~190 pins its runs still need,
against 35-55 with the same load on raid1 metadata, so most of them look
like metadata stripes: tree-log blocks share the transaction's META run
with the tree blocks COWed in it, a log commit writes its part of a stripe
and the rest waits for the transaction commit.  Data runs are private per
fsyncing inode and closed at every log commit, so they should be many and
short.  Say which, instead of inferring it.

stripe_park_stats gains rbio_pinned_live_<class> (live pins by the class of
the run that still needs the stripe) and open_runs_<class> (open runs by
class), for class cow, reloc, log, nocow and meta.
btrfs_stripe_run_incomplete() becomes a wrapper around
btrfs_stripe_run_incomplete_class(), which also says which run's class.

[zygo: 7.3 port: 7.3 has no per-chunk stripe length (map->stripe_len,
bioc->stripe_len, the two-argument btrfs_stripe_nr_to_offset()):
BTRFS_STRIPE_LEN is used instead; the change itself is unchanged]

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

index 05177ba63a406a764e90b16545cc5beee5a936b1..30c7cfb73c346e46ba7a2f97aac10f855c5d7cad 100644 (file)
@@ -2477,28 +2477,29 @@ static bool run_unwritten_in(struct btrfs_open_stripe_run *run, u64 start,
        return false;
 }
 
-bool btrfs_stripe_run_incomplete(struct btrfs_fs_info *fs_info,
-                                u64 stripe_start, u64 stripe_len)
+/*
+ * As btrfs_stripe_run_incomplete(), returning the class of the run that
+ * leaves the stripe incomplete, or -1 if none does.
+ */
+int btrfs_stripe_run_incomplete_class(struct btrfs_fs_info *fs_info,
+                                     u64 stripe_start, u64 stripe_len)
 {
        struct btrfs_block_group *bg;
        struct btrfs_open_stripe_run *run;
        unsigned long flags;
-       bool ret = false;
+       int ret = -1;
 
        if (list_empty_careful(&fs_info->open_stripe_bgs))
-               return false;
+               return -1;
        bg = btrfs_lookup_block_group(fs_info, stripe_start);
        if (!bg)
-               return false;
+               return -1;
        spin_lock_irqsave(&bg->stripe_run_lock, flags);
        list_for_each_entry(run, &bg->open_stripe_runs, list) {
-               if (run->open && run->offset > stripe_start &&
-                   run->offset < stripe_start + stripe_len) {
-                       ret = true;
-                       break;
-               }
-               if (run_unwritten_in(run, stripe_start, stripe_len)) {
-                       ret = true;
+               if ((run->open && run->offset > stripe_start &&
+                    run->offset < stripe_start + stripe_len) ||
+                   run_unwritten_in(run, stripe_start, stripe_len)) {
+                       ret = run->class;
                        break;
                }
        }
@@ -2507,6 +2508,51 @@ bool btrfs_stripe_run_incomplete(struct btrfs_fs_info *fs_info,
        return ret;
 }
 
+/*
+ * Is [stripe_start, stripe_start + stripe_len) still waiting for writes from
+ * a stripe run?  Two ways:
+ *
+ * - an open run's allocation frontier lies strictly inside it: the run will
+ *   allocate and write the rest of the stripe later, although every sector
+ *   allocated so far may already be written;
+ * - a run covering it has sectors allocated (live) but not yet written: the
+ *   frontier has moved past, and the writes are still in flight (writeback
+ *   allocates a whole range before any of its bios complete).
+ *
+ * Either way the stripe has been written in part, and the rest will arrive
+ * as a sub-stripe write that needs the part already written.
+ */
+bool btrfs_stripe_run_incomplete(struct btrfs_fs_info *fs_info,
+                                u64 stripe_start, u64 stripe_len)
+{
+       return btrfs_stripe_run_incomplete_class(fs_info, stripe_start,
+                                                stripe_len) >= 0;
+}
+
+/* Open stripe runs by class, for sysfs: @counts has NR_CLASSES entries. */
+void btrfs_count_open_stripe_runs(struct btrfs_fs_info *fs_info, int *counts)
+{
+       struct btrfs_block_group *bg;
+       struct btrfs_open_stripe_run *run;
+       unsigned long flags;
+
+       memset(counts, 0, sizeof(*counts) * BTRFS_STRIPE_RUN_NR_CLASSES);
+       spin_lock(&fs_info->open_stripe_lock);
+       list_for_each_entry(bg, &fs_info->open_stripe_bgs, open_stripe_bg_list) {
+               spin_lock_irqsave(&bg->stripe_run_lock, flags);
+               list_for_each_entry(run, &bg->open_stripe_runs, list)
+                       if (run->open && run->class < BTRFS_STRIPE_RUN_NR_CLASSES)
+                               counts[run->class]++;
+               spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+       }
+       spin_unlock(&fs_info->open_stripe_lock);
+}
+
+/*
+ * Like btrfs_stripe_in_open_run(), also reporting the covering run's
+ * class: the raid56 parking gate treats NOCOW-class runs specially
+ * (sync writes into them never park; see rbio_try_park()).
+ */
 bool btrfs_stripe_open_run_class(struct btrfs_fs_info *fs_info, u64 logical,
                                 enum btrfs_stripe_run_class *class)
 {
index f9ada851e6f6f80161b7ecec647e32eec71173d9..fd82a81bdd9095dcacf7408515dbb57aaef2dbd8 100644 (file)
@@ -528,6 +528,9 @@ void btrfs_stripe_run_mark_written(struct btrfs_fs_info *fs_info, u64 start,
                                   u64 len);
 bool btrfs_stripe_run_incomplete(struct btrfs_fs_info *fs_info,
                                 u64 stripe_start, u64 stripe_len);
+int btrfs_stripe_run_incomplete_class(struct btrfs_fs_info *fs_info,
+                                     u64 stripe_start, u64 stripe_len);
+void btrfs_count_open_stripe_runs(struct btrfs_fs_info *fs_info, int *counts);
 bool btrfs_stripe_open_run_class(struct btrfs_fs_info *fs_info, u64 logical,
                                 enum btrfs_stripe_run_class *class);
 u64 btrfs_stripe_run_stranded_stripes(struct btrfs_block_group *bg, u32 *freep,
index ee880b3ff162090c2d0ae0fb06d19c919320d334..707c772201cbf2c2b2ee7bfde7dd6d96ce84056e 100644 (file)
@@ -867,22 +867,34 @@ void btrfs_rbio_unpin_stale(struct btrfs_fs_info *fs_info)
  * eviction or a re-cache finds it.
  */
 void btrfs_rbio_cache_usage(struct btrfs_fs_info *fs_info, int *cached,
-                           int *pinned, int *live)
+                           int *pinned, int *live, int *live_by_class)
 {
        struct btrfs_stripe_hash_table *table = fs_info->stripe_hash_table;
        struct btrfs_raid_bio *rbio;
+       int class;
 
        *cached = 0;
        *pinned = 0;
        *live = 0;
+       memset(live_by_class, 0, sizeof(*live_by_class) * BTRFS_STRIPE_RUN_NR_CLASSES);
        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;
+       list_for_each_entry(rbio, &table->stripe_cache, stripe_cache) {
+               if (!test_bit(RBIO_PINNED_BIT, &rbio->flags) ||
+                   !btrfs_test_opt(fs_info, STRIPE_ALLOC))
+                       continue;
+               class = btrfs_stripe_run_incomplete_class(fs_info,
+                               rbio->bioc->full_stripe_logical,
+                               (u64)rbio->nr_data * BTRFS_STRIPE_LEN);
+               if (class < 0)
+                       continue;
+               *live += 1;
+               if (class < BTRFS_STRIPE_RUN_NR_CLASSES)
+                       live_by_class[class] += 1;
+       }
        spin_unlock(&table->cache_lock);
 }
 
index b09d5ce6aafbaf309b662e17b58cb42276668e2b..313c19591bf6233e3fad77c51132e045f57260b2 100644 (file)
@@ -319,7 +319,7 @@ 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);
+                           int *pinned, int *live, int *live_by_class);
 void btrfs_rbio_unpin_stale(struct btrfs_fs_info *fs_info);
 
 #endif
index 0cd5d4e7e4b0a751e761df699b976c2e2174ee98..ac62fb5c4adff3b3e58e3bbf6671c432e3944758 100644 (file)
@@ -1299,10 +1299,19 @@ 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,
+       /* Indexed by enum btrfs_stripe_run_class. */
+       static const char * const class_names[] = {
+               "cow", "reloc", "log", "nocow",
+       };
+       int live_by_class[BTRFS_STRIPE_RUN_NR_CLASSES];
+       int open_runs[BTRFS_STRIPE_RUN_NR_CLASSES];
+       int cached, pinned, live, class;
+       ssize_t len;
+
+       BUILD_BUG_ON(ARRAY_SIZE(class_names) != BTRFS_STRIPE_RUN_NR_CLASSES);
+       btrfs_rbio_cache_usage(fs_info, &cached, &pinned, &live, live_by_class);
+       btrfs_count_open_stripe_runs(fs_info, open_runs);
+       len = sysfs_emit(buf,
                "parked %lld\n"
                "merged_bytes %lld\n"
                "filled %lld\n"
@@ -1343,6 +1352,13 @@ static ssize_t btrfs_stripe_park_stats_show(struct kobject *kobj,
                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));
+       for (class = 0; class < BTRFS_STRIPE_RUN_NR_CLASSES; class++)
+               len += sysfs_emit_at(buf, len, "rbio_pinned_live_%s %d\n",
+                                    class_names[class], live_by_class[class]);
+       for (class = 0; class < BTRFS_STRIPE_RUN_NR_CLASSES; class++)
+               len += sysfs_emit_at(buf, len, "open_runs_%s %d\n",
+                                    class_names[class], open_runs[class]);
+       return len;
 }
 BTRFS_ATTR(, stripe_park_stats, btrfs_stripe_park_stats_show);