]> git.hungrycats.org Git - linux/commitdiff
btrfs: raid56: adaptive park deadline from the parked-rbio backlog
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sat, 19 Sep 2026 17:17:55 +0000 (13:17 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:37:49 +0000 (18:37 -0500)
Full-stripe batching parks a sub-stripe write's rbio for a deadline so the
writes that fill the rest of its stripe can merge into it, turning a
read-modify-write into one full-stripe write.  The deadline is a fixed
per-filesystem value.  It is long enough to catch those merges, but at a
small-file fill to ENOSPC -- where a run's stripes never fill because each
file's neighbours land in the next file's stripe rather than this one --
every partial write waits the deadline out, and then its 10x stuck cap, on
an arrival that never comes before padding to a full stripe and going down.
Lowering the deadline globally fixes that fill but throws away the merges
the deadline exists to catch on ordinary moderate writes.

Feed the deadline back from the parking machinery's own backlog instead of
guessing at the workload.  Track stripe_parked_now, the number of
currently-parked rbios, incremented as a partial write parks and
decremented as it unparks.  When it runs ahead of the stripe_park_congestion
knob (0 disables it, the default), a new async partial write takes the short
sync deadline through the existing sync-park path rather than the full one.
This is negative feedback: the shorter deadline drains the backlog, so once
it falls back below the knob later writes regain the full deadline and full-
stripe batching.  No space-state estimate is needed -- a small-file flood
builds the backlog directly, while a large sequential fill (already full
stripes, never parked) and a moderate steady stream do not.

Measured on an eight-device raid5.  A small-file balance-reclaim soak that
times out at the default deadline completes at a congestion of 4; on a
throttled moderate stream, where the backlog stays low, a congestion of 32
never trips, keeps the full deadline, and merges three times as much with
38% fewer RMW reads as the same filesystem under a flat 3ms deadline.  The
gauge and a congestion_short counter join the stripe_park_stats sysfs file
so the knob can be set from measurement.  Off by default, and no data-path
change: a shortened park still pads or reads exactly as it would have, only
sooner.

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

index 70ff959e3b6f1fc9a35ec299ff60c3341eff3fa0..1e8e47f6ecd097a1b74689f00382a18d2e020db7 100644 (file)
@@ -851,6 +851,7 @@ struct btrfs_fs_info {
                 * stripe.
                 */
                atomic64_t pad_decline_uncovered;
+               atomic64_t congestion_short;
        } stripe_park_stats;
        /*
         * Park deadlines, ms (sysfs); 0 disables parking for that class.  See
@@ -858,6 +859,10 @@ struct btrfs_fs_info {
         */
        u32 stripe_park_timeout_ms;
        u32 stripe_park_sync_timeout_ms;
+       /* Parked-rbio backlog above which parks go short; 0 = off. */
+       u32 stripe_park_congestion;
+       /* Live count of currently-parked rbios (the backlog gauge). */
+       atomic_t stripe_parked_now;
 
        /* Private scrub information */
        struct mutex scrub_lock;
index 4361c5d09864e759bcde458db6648c2c8df5b42e..16a97215df3ac3ddc0e2b060dcd2fa9f662d421e 100644 (file)
@@ -787,6 +787,7 @@ static void rbio_unpark_locked(struct btrfs_raid_bio *rbio)
 {
        list_del_init(&rbio->parked_node);
        clear_bit(RBIO_PARKED_BIT, &rbio->flags);
+       atomic_dec(&rbio->bioc->fs_info->stripe_parked_now);
 }
 
 /* Does any bio gathered in @rbio carry a sync hint (a blocked waiter)? */
@@ -861,6 +862,25 @@ static bool rbio_try_park(struct btrfs_raid_bio *rbio)
            timeout_ms == sync_timeout_ms)
                return false;
 
+       /*
+        * Park-congestion feedback: when more rbios are parked than
+        * stripe_park_congestion allows (0 = off), merges are not arriving fast
+        * enough to clear the backlog, so give this write the short sync
+        * deadline (which also skips the stuck cap).  Once the backlog drains
+        * below the knob, new writes get the full deadline again.
+        */
+       {
+               unsigned int congestion =
+                       READ_ONCE(fs_info->stripe_park_congestion);
+
+               if (timeout_ms != sync_timeout_ms && sync_timeout_ms != 0 &&
+                   congestion &&
+                   atomic_read(&fs_info->stripe_parked_now) > (int)congestion) {
+                       timeout_ms = sync_timeout_ms;
+                       atomic64_inc(&fs_info->stripe_park_stats.congestion_short);
+               }
+       }
+
        spin_lock(&table->parked_lock);
        set_bit(RBIO_PARKED_BIT, &rbio->flags);
        set_bit(RBIO_WAS_PARKED_BIT, &rbio->flags);
@@ -878,6 +898,7 @@ static bool rbio_try_park(struct btrfs_raid_bio *rbio)
         */
        refcount_inc(&rbio->refs);
        list_add_tail(&rbio->parked_node, &table->parked);
+       atomic_inc(&fs_info->stripe_parked_now);
        spin_unlock(&table->parked_lock);
        atomic64_inc(&fs_info->stripe_park_stats.parked);
        queue_delayed_work(system_percpu_wq, &table->parked_work,
index d254f3cc9e64d9a00e0ee4c96e6d550fef16dc2f..9dfc4e92e48bf5970ec9c5b6bc7f56f22b9b273f 100644 (file)
@@ -1288,6 +1288,7 @@ 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);
+BTRFS_PARK_TIMEOUT_ATTR(stripe_park_congestion, stripe_park_congestion);
 
 
 
@@ -1663,6 +1664,7 @@ static const struct attribute *btrfs_attrs[] = {
        BTRFS_ATTR_PTR(, stripe_park_stats),
        BTRFS_ATTR_PTR(, stripe_park_timeout_ms),
        BTRFS_ATTR_PTR(, stripe_park_sync_timeout_ms),
+       BTRFS_ATTR_PTR(, stripe_park_congestion),
        BTRFS_ATTR_PTR(, temp_fsid),
        NULL,
 };