]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_alloc: tunable park deadlines, and say why padding declined
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sat, 19 Sep 2026 17:17:50 +0000 (13:17 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:37:47 +0000 (18:37 -0500)
The deadline constants are meant to be judged "from measurements rather
than taste", but they are compile-time, so every data point costs a kernel
build and a reboot.  Measuring a buffered fill -- dd, no fsync, so nothing
kicks the park and the backstop is the only exit -- found 1861 of 3984
parks expiring at the 100ms deadline, which is precisely the no-waiter case
the constant exists for and the one with no numbers behind it.

Expose both deadlines as writable sysfs files, clamped to 60s because a
parked rbio holds its stripe lock, with 0 disabling parking for that class
(useful as an experiment in itself):

  /sys/fs/btrfs/<uuid>/stripe_park_timeout_ms       (default 100)
  /sys/fs/btrfs/<uuid>/stripe_park_sync_timeout_ms  (default 3)

Add the counters the stats file cannot supply.  rmw_reads counts only
parked writes that still had to read, so it cannot answer "does this
workload read-modify-write at all"; data_rmw counts every data RMW, which
with allow_rmw empty on a covered block group is the number the
stripe-exclusive claim is about, and it should be zero.

And record why padding refused, because the reasons are different defects:
the rest of the stripe is allocated and its data has not arrived yet
(wait longer, or kick when the frontier advances); no open run covers the
stripe, so the run closed before its own write went down; or the frontier
never reached the stripe at all.  Attributing them turns "some RMW
remains" into a specific thing to fix.

No behaviour change at the defaults.

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

index c3ceb8ef87ebdb2ea66cc4651d2b7383aa771fcb..0365faa1c0cf15ccc1983db674b90efa335e066c 100644 (file)
@@ -975,14 +975,19 @@ bool btrfs_stripe_run_pad_start(struct btrfs_fs_info *fs_info,
 {
        struct btrfs_block_group *bg;
        struct btrfs_open_stripe_run *run;
+       atomic64_t *decline = NULL;
        unsigned long flags;
        bool ret = false;
 
-       if (list_empty_careful(&fs_info->open_stripe_bgs))
+       if (list_empty_careful(&fs_info->open_stripe_bgs)) {
+               atomic64_inc(&fs_info->stripe_park_stats.pad_decline_no_run);
                return false;
+       }
        bg = btrfs_lookup_block_group(fs_info, stripe_start);
-       if (!bg)
+       if (!bg) {
+               atomic64_inc(&fs_info->stripe_park_stats.pad_decline_no_run);
                return false;
+       }
 
        spin_lock_irqsave(&bg->stripe_run_lock, flags);
        list_for_each_entry(run, &bg->open_stripe_runs, list) {
@@ -992,10 +997,23 @@ bool btrfs_stripe_run_pad_start(struct btrfs_fs_info *fs_info,
                    run->offset < stripe_start + stripe_len) {
                        *pad_from = run->offset;
                        ret = true;
+               } else if (run->offset >= stripe_start + stripe_len) {
+                       /*
+                        * The frontier is past this stripe: every sector of
+                        * it is allocated, so there is nothing dead to pad
+                        * and the write is waiting on a neighbour's data.
+                        */
+                       decline = &fs_info->stripe_park_stats.pad_decline_live;
+               } else {
+                       /* The frontier has not reached the stripe at all. */
+                       decline = &fs_info->stripe_park_stats.pad_decline_unallocated;
                }
                break;
        }
        spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+       if (!ret)
+               atomic64_inc(decline ? decline :
+                            &fs_info->stripe_park_stats.pad_decline_no_run);
        btrfs_put_block_group(bg);
        return ret;
 }
index 6142f0f3d598fdd63e3e071cfa4564c33a9ea29b..d461066c858d697b309e29dbb126acbdb32ba4fd 100644 (file)
@@ -2792,6 +2792,8 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
        INIT_LIST_HEAD(&fs_info->delalloc_roots);
        INIT_LIST_HEAD(&fs_info->caching_block_groups);
        INIT_LIST_HEAD(&fs_info->open_stripe_bgs);
+       fs_info->stripe_park_timeout_ms = BTRFS_RBIO_PARK_TIMEOUT_MS;
+       fs_info->stripe_park_sync_timeout_ms = BTRFS_RBIO_PARK_SYNC_TIMEOUT_MS;
        spin_lock_init(&fs_info->open_stripe_lock);
        spin_lock_init(&fs_info->delalloc_root_lock);
        spin_lock_init(&fs_info->trans_lock);
index 4d5d73531d0b92445dd2be7d8b34cd0eab985496..cf062f8aad12ddae2c57d81d1c6f9c2b3caa740b 100644 (file)
@@ -501,6 +501,17 @@ struct btrfs_delayed_root {
 struct btrfs_free_space_ctl;
 struct btrfs_free_space;
 
+/*
+ * Default park deadlines, ms (sysfs stripe_park_{,sync_}timeout_ms).  Sync
+ * writes (REQ_SYNC) have a waiter blocked on them, so their deadline is
+ * just long enough for the rest of one writeback pass over the stripe to
+ * merge in.
+ */
+#define BTRFS_RBIO_PARK_TIMEOUT_MS     100
+#define BTRFS_RBIO_PARK_SYNC_TIMEOUT_MS        3
+/* Refuse absurd deadlines: a parked rbio holds its stripe lock. */
+#define BTRFS_RBIO_PARK_TIMEOUT_MS_MAX 60000
+
 struct btrfs_fs_info {
        u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
        unsigned long flags;
@@ -807,7 +818,28 @@ struct btrfs_fs_info {
                atomic64_t kicked;
                atomic64_t rmw_reads;
                atomic64_t padded;
+               /*
+                * Data read-modify-writes in a covered block group, parked or
+                * not; zero with allow_rmw empty.  rmw_reads counts only the
+                * parked ones.
+                */
+               atomic64_t data_rmw;
+               /*
+                * Why padding declined: live (the rest of the stripe is
+                * allocated and not yet written), no_run (no open run covers
+                * the stripe), unallocated (the run's frontier has not reached
+                * the stripe).
+                */
+               atomic64_t pad_decline_live;
+               atomic64_t pad_decline_no_run;
+               atomic64_t pad_decline_unallocated;
        } stripe_park_stats;
+       /*
+        * Park deadlines, ms (sysfs); 0 disables parking for that class.  See
+        * rbio_try_park().
+        */
+       u32 stripe_park_timeout_ms;
+       u32 stripe_park_sync_timeout_ms;
 
        /* Private scrub information */
        struct mutex scrub_lock;
index 1cde0837951d0e04849701ed6ccdfe2a32d2e207..887ff3776ebd558c0ad5dcc15de4b83bf4aaea55 100644 (file)
@@ -67,8 +67,6 @@
 
 #define RBIO_CACHE_SIZE 1024
 
-#define BTRFS_RBIO_PARK_TIMEOUT_MS     100
-#define BTRFS_RBIO_PARK_SYNC_TIMEOUT_MS        3
 /* Scan period of the park timer while anything is parked. */
 #define BTRFS_RBIO_PARK_SCAN_MS                3
 
@@ -805,16 +803,22 @@ static bool rbio_try_park(struct btrfs_raid_bio *rbio)
 {
        struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;
        struct btrfs_stripe_hash_table *table = fs_info->stripe_hash_table;
-       unsigned int timeout_ms = BTRFS_RBIO_PARK_TIMEOUT_MS;
+       const unsigned int sync_timeout_ms =
+               READ_ONCE(fs_info->stripe_park_sync_timeout_ms);
+       unsigned int timeout_ms = READ_ONCE(fs_info->stripe_park_timeout_ms);
 
        if (!btrfs_stripe_in_open_run(fs_info, rbio->bioc->full_stripe_logical))
                return false;
 
        spin_lock(&rbio->bio_list_lock);
        if (rbio_has_sync_bio(rbio))
-               timeout_ms = BTRFS_RBIO_PARK_SYNC_TIMEOUT_MS;
+               timeout_ms = sync_timeout_ms;
        spin_unlock(&rbio->bio_list_lock);
 
+       /* A zeroed deadline disables parking for that class outright. */
+       if (timeout_ms == 0)
+               return false;
+
        /*
         * A sync rbio whose submission batch was already merged by its
         * unplug callback has nothing more coming from its submitter; the
@@ -824,7 +828,7 @@ static bool rbio_try_park(struct btrfs_raid_bio *rbio)
         * path kicks it as soon as they have all been submitted.)
         */
        if (test_bit(RBIO_BATCH_DONE_BIT, &rbio->flags) &&
-           timeout_ms == BTRFS_RBIO_PARK_SYNC_TIMEOUT_MS)
+           timeout_ms == sync_timeout_ms)
                return false;
 
        spin_lock(&table->parked_lock);
@@ -1093,7 +1097,8 @@ static noinline int lock_stripe_add(struct btrfs_raid_bio *rbio)
                                        struct btrfs_stripe_hash_table *table =
                                                cur->bioc->fs_info->stripe_hash_table;
                                        unsigned long dl = jiffies +
-               msecs_to_jiffies(BTRFS_RBIO_PARK_SYNC_TIMEOUT_MS);
+               msecs_to_jiffies(READ_ONCE(cur->bioc->fs_info->
+                                          stripe_park_sync_timeout_ms));
 
                                        spin_lock(&table->parked_lock);
                                        if (test_bit(RBIO_PARKED_BIT,
@@ -2866,10 +2871,17 @@ static void rmw_rbio(struct btrfs_raid_bio *rbio)
         */
        if (!rbio_is_full(rbio) && !rmw_try_pad_full(rbio) &&
            need_read_stripe_sectors(rbio)) {
+               struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;
+
+               /*
+                * Every data read-modify-write, parked or not.  With
+                * allow_rmw empty and a covered block group this is the
+                * count that should be zero.
+                */
+               atomic64_inc(&fs_info->stripe_park_stats.data_rmw);
                /* A parked-then-flushed write that still had to read. */
                if (test_bit(RBIO_WAS_PARKED_BIT, &rbio->flags))
-                       atomic64_inc(&rbio->bioc->fs_info->
-                                    stripe_park_stats.rmw_reads);
+                       atomic64_inc(&fs_info->stripe_park_stats.rmw_reads);
                /*
                 * Now we're doing sub-stripe write, also need all data stripes
                 * to do the full RMW.
index f79a9b1a7c72455db3a3d17c59bb32d2c8fa1b40..9f91baea8f37006ea4fde0a88539d77690a2f41e 100644 (file)
@@ -1250,6 +1250,41 @@ static ssize_t btrfs_stripe_park_stats_show(struct kobject *kobj,
 }
 BTRFS_ATTR(, stripe_park_stats, btrfs_stripe_park_stats_show);
 
+/*
+ * Park deadlines, ms; 0 disables parking for that class.  See
+ * rbio_try_park().
+ */
+#define BTRFS_PARK_TIMEOUT_ATTR(_name, _field)                         \
+static ssize_t btrfs_##_name##_show(struct kobject *kobj,              \
+                                   struct kobj_attribute *a, char *buf)\
+{                                                                      \
+       struct btrfs_fs_info *fs_info = to_fs_info(kobj);               \
+                                                                       \
+       return sysfs_emit(buf, "%u\n", READ_ONCE(fs_info->_field));     \
+}                                                                      \
+static ssize_t btrfs_##_name##_store(struct kobject *kobj,             \
+                                    struct kobj_attribute *a,          \
+                                    const char *buf, size_t len)       \
+{                                                                      \
+       struct btrfs_fs_info *fs_info = to_fs_info(kobj);               \
+       unsigned int val;                                               \
+       int ret;                                                        \
+                                                                       \
+       ret = kstrtouint(buf, 10, &val);                                \
+       if (ret)                                                        \
+               return ret;                                             \
+       if (val > BTRFS_RBIO_PARK_TIMEOUT_MS_MAX)                       \
+               return -EINVAL;                                         \
+       WRITE_ONCE(fs_info->_field, val);                               \
+       return len;                                                     \
+}                                                                      \
+BTRFS_ATTR_RW(, _name, btrfs_##_name##_show, btrfs_##_name##_store)
+
+BTRFS_PARK_TIMEOUT_ATTR(stripe_park_timeout_ms, stripe_park_timeout_ms);
+BTRFS_PARK_TIMEOUT_ATTR(stripe_park_sync_timeout_ms, stripe_park_sync_timeout_ms);
+
+
+
 static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
                                struct kobj_attribute *a, char *buf)
 {
@@ -1620,6 +1655,8 @@ static const struct attribute *btrfs_attrs[] = {
        BTRFS_ATTR_PTR(, bg_reclaim_threshold),
        BTRFS_ATTR_PTR(, commit_stats),
        BTRFS_ATTR_PTR(, stripe_park_stats),
+       BTRFS_ATTR_PTR(, stripe_park_timeout_ms),
+       BTRFS_ATTR_PTR(, stripe_park_sync_timeout_ms),
        BTRFS_ATTR_PTR(, temp_fsid),
        NULL,
 };