]> 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>
Mon, 2 Nov 2026 02:16:30 +0000 (21:16 -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.

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 4072a83823f56e9b8206cebe59f50281a8fcbcee..e59e3826d9bb855814c880a03135cb37c9af9128 100644 (file)
@@ -2467,28 +2467,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;
                }
        }
@@ -2497,6 +2498,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 c6dc7c1cdf1a56e9991961d31a669e0d8134bdd8..75775cb52ae8ab005ddb7e6cb82a28755f9050ee 100644 (file)
@@ -515,6 +515,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 24f13a68716166e15f0a38cd1f42f75fc51b778c..ea85d8e41e01ab89bc63da5cf4ef70733161d15c 100644 (file)
@@ -796,22 +796,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 * rbio->bioc->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 60e058c6fffc19b91f95837ff602cab887ce40ea..22687b6194f93adc3c680dbc59aa1b879ed2eacc 100644 (file)
@@ -314,7 +314,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 f6e83d2789c2ff18285f803ff2b712f26872e3d7..609e3e4462a1c4e1e83ecb156570716fe4ba771a 100644 (file)
@@ -1186,10 +1186,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"
@@ -1230,6 +1239,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);