]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_alloc: attribute each data read-modify-write to why padding declined
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Wed, 30 Sep 2026 22:07:52 +0000 (18:07 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:16:30 +0000 (21:16 -0500)
data_rmw counts read-modify-writes, and the pad_decline_* counters count
the pad oracle's refusals, but a refusal does not always lead to a read
(a full stripe or a cached one does not read), so the two cannot be
subtracted: nothing says which RMWs the allocator explains.  With
allow_rmw empty, a data RMW in a stripe_alloc group is expected in two
places only:

 - at the open run's own frontier (the pad verdict UNCOVERED): the
   sectors it reads belong to the same run, in the same transaction,
   and are not durable yet;

 - from relocation writing into its own preallocated extents, which no
   root but the data reloc tree references until relocation moves the
   pointers after writeback.

Record the pad oracle's verdict on the rbio (rmw_try_pad_full()) and,
at the RMW site, split data_rmw by it (data_rmw_<reason>, with
data_rmw_not_tried when the oracle was not consulted and
data_rmw_pad_ok_nomem when it agreed but the zero pages could not be
allocated).  NO_RUN is the oracle's catch-all exit, so split it further
by the exit taken (data_rmw_norun_*) and count those in a read-only
group (data_rmw_norun_bg_ro).  Count the data RMWs written by the data
reloc inode outside the frontier (data_rmw_reloc_writer, disjoint from
data_rmw_uncovered).  All in stripe_park_stats.

A test can then assert that data_rmw - data_rmw_uncovered -
data_rmw_reloc_writer is zero on a stripe_alloc filesystem, on a
production kernel, with reclaim relocating during the run -- which it
could not do from the totals alone.

The cost is a byte of state per rbio and a few counter increments per
RMW, plus one block group lookup for NO_RUN RMWs.

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

index 6ecfd21c3e2d783535d87df3bff411a82c925053..6bef4d25d18e189aa92de0eeac92f5d0cf2d4bd2 100644 (file)
@@ -1699,13 +1699,19 @@ bool btrfs_stripe_run_pad_mask(struct btrfs_fs_info *fs_info, u64 stripe_start,
 
        if (list_empty_careful(&fs_info->open_stripe_bgs)) {
                atomic64_inc(&fs_info->stripe_park_stats.pad_decline_no_run);
+               if (info)
+                       info->norun = BTRFS_STRIPE_NORUN_NO_BGS;
                return false;
        }
        bg = btrfs_lookup_block_group(fs_info, stripe_start);
        if (!bg) {
                atomic64_inc(&fs_info->stripe_park_stats.pad_decline_no_run);
+               if (info)
+                       info->norun = BTRFS_STRIPE_NORUN_NO_BG;
                return false;
        }
+       if (info)
+               info->norun = BTRFS_STRIPE_NORUN_NO_COVER;
 
        spin_lock_irqsave(&bg->stripe_run_lock, flags);
        list_for_each_entry(run, &bg->open_stripe_runs, list) {
@@ -1718,6 +1724,7 @@ bool btrfs_stripe_run_pad_mask(struct btrfs_fs_info *fs_info, u64 stripe_start,
                        info->run_end = run->end;
                        info->run_offset = run->offset;
                        info->inflight_bytes = run->inflight_bytes;
+                       info->norun = BTRFS_STRIPE_NORUN_NO_LIVE;
                }
                if (!run->live || !run->live_nbits)
                        break;
@@ -1731,8 +1738,11 @@ bool btrfs_stripe_run_pad_mask(struct btrfs_fs_info *fs_info, u64 stripe_start,
                 * needs to pad.  An open run's end is stripe aligned, so
                 * overhang there means a stripe the run does not own.
                 */
-               if (stripe_start + stripe_len > run->end && run->open)
+               if (stripe_start + stripe_len > run->end && run->open) {
+                       if (info)
+                               info->norun = BTRFS_STRIPE_NORUN_OVERHANG;
                        break;
+               }
 
                first = (stripe_start - run->start) >> bits;
                /*
@@ -1741,8 +1751,11 @@ bool btrfs_stripe_run_pad_mask(struct btrfs_fs_info *fs_info, u64 stripe_start,
                 * which sectors are dead, and padding acts on that by
                 * overwriting them.
                 */
-               if (first + nsectors > run->live_nbits)
+               if (first + nsectors > run->live_nbits) {
+                       if (info)
+                               info->norun = BTRFS_STRIPE_NORUN_PAST_MAP;
                        break;
+               }
                bitmap_zero(dead, nsectors);
                if (unwritten)
                        bitmap_zero(unwritten, nsectors);
index 7f4227e488fbd8dd294454c55be96cdb42198937..4df2396ffc3b25a1388f5eb781d150e7ca93606b 100644 (file)
@@ -532,6 +532,22 @@ enum btrfs_stripe_pad_reason {
        BTRFS_STRIPE_PAD_OK = 0,
        BTRFS_STRIPE_PAD_NO_RUN,        /* no open run covers the stripe */
        BTRFS_STRIPE_PAD_UNCOVERED,     /* a sector below the frontier is not covered */
+       BTRFS_STRIPE_PAD_NR_REASONS,
+};
+
+/*
+ * Which exit of btrfs_stripe_run_pad_mask() gave BTRFS_STRIPE_PAD_NO_RUN: a
+ * catch-all for every refusal, including "a run covers the stripe but its
+ * map cannot answer for it".
+ */
+enum btrfs_stripe_pad_norun {
+       BTRFS_STRIPE_NORUN_NO_BGS = 0,  /* no block group has open runs */
+       BTRFS_STRIPE_NORUN_NO_BG,       /* block group lookup failed */
+       BTRFS_STRIPE_NORUN_NO_COVER,    /* no run's span covers the stripe */
+       BTRFS_STRIPE_NORUN_NO_LIVE,     /* covering run has no live map */
+       BTRFS_STRIPE_NORUN_OVERHANG,    /* open run's end cuts the stripe */
+       BTRFS_STRIPE_NORUN_PAST_MAP,    /* stripe runs past the live map */
+       BTRFS_STRIPE_NORUN_NR,
 };
 
 struct btrfs_stripe_pad_info {
@@ -540,6 +556,7 @@ struct btrfs_stripe_pad_info {
        u64 run_offset;                 /* the allocation frontier */
        u64 inflight_bytes;
        u8 reason;
+       u8 norun;                       /* enum btrfs_stripe_pad_norun */
 };
 
 u64 btrfs_stripe_run_alloc_ceiling(struct btrfs_fs_info *fs_info,
index 10c70314e9126065da9ceeb0d554eb6f488fa162..9d637c75ae38dce1d6d35f2f3f72ede7e7e21c10 100644 (file)
@@ -873,6 +873,21 @@ struct btrfs_fs_info {
                 * stripe.
                 */
                atomic64_t pad_decline_uncovered;
+               /*
+                * data_rmw split by why padding declined for the rbio that
+                * did it: 0..2 are enum btrfs_stripe_pad_reason (0: the pad
+                * oracle agreed but the data page allocation failed), 3: not
+                * tried (stripe_alloc off, or no memory for the bitmap).
+                * data_rmw_norun splits the NO_RUN entry by enum
+                * btrfs_stripe_pad_norun; norun_ro counts those in a
+                * read-only (relocating) group.  reloc_writer counts data RMWs
+                * written by the data reloc inode outside the open run's
+                * frontier, disjoint from data_rmw_by[UNCOVERED].
+                */
+               atomic64_t data_rmw_by[4];
+               atomic64_t data_rmw_norun[6];
+               atomic64_t data_rmw_norun_ro;
+               atomic64_t data_rmw_reloc_writer;
                atomic64_t meta_rmw;
                atomic64_t congestion_short;
                /* Stripes pinned in the rbio cache (rbio_needs_pin()). */
index 546ae059589b853024cfe8ce54ca5f98f0e392d4..d1142d64e2c0ac45cf0deb988186e5e9ce56cd9a 100644 (file)
@@ -22,6 +22,7 @@
 #include "file-item.h"
 #include "btrfs_inode.h"
 #include "block-group.h"
+#include "bio.h"
 
 /* set when additional merges to this rbio are not allowed */
 #define RBIO_RMW_LOCKED_BIT    1
@@ -65,6 +66,9 @@
 /* Set at park time and never cleared: this rbio once parked (stats). */
 #define RBIO_WAS_PARKED_BIT    7
 
+/* rbio->pad_reason when rmw_try_pad_full() did not consult the pad oracle. */
+#define RBIO_PAD_NOT_TRIED     0xff
+
 /*
  * Set when a parked rbio carries (or merged) a REQ_SYNC bio: a waiter
  * is blocked on it, so deadlines stay authoritative and neither the
@@ -3020,6 +3024,65 @@ static void submit_write_bios(struct btrfs_raid_bio *rbio,
        }
 }
 
+static void rbio_record_pad(struct btrfs_raid_bio *rbio,
+                           const struct btrfs_stripe_pad_info *info)
+{
+       rbio->pad_reason = info->reason;
+       rbio->pad_norun = info->norun;
+}
+
+static_assert(BTRFS_STRIPE_PAD_NR_REASONS + 1 ==
+             ARRAY_SIZE(((struct btrfs_fs_info *)0)->stripe_park_stats.data_rmw_by));
+static_assert(BTRFS_STRIPE_NORUN_NR ==
+             ARRAY_SIZE(((struct btrfs_fs_info *)0)->stripe_park_stats.data_rmw_norun));
+
+/*
+ * Attribute a read-modify-write to why padding declined for this rbio.  With
+ * allow_rmw empty, a data RMW in a stripe_alloc group is expected in two
+ * places only: at the open run's own frontier (UNCOVERED: the sectors it
+ * reads are the same run's, in the same transaction, not yet durable), and
+ * from relocation writing into its own preallocated extents, which nothing
+ * but the data reloc inode references until relocation moves the pointers
+ * after writeback.  Any other data RMW in a covered group is unexplained.
+ */
+static void rbio_count_rmw_reason(struct btrfs_raid_bio *rbio)
+{
+       struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;
+       u8 idx = rbio->pad_reason;
+       bool reloc_writer = false;
+       struct bio *bio;
+
+       if (idx >= BTRFS_STRIPE_PAD_NR_REASONS)
+               idx = BTRFS_STRIPE_PAD_NR_REASONS;      /* not tried */
+       atomic64_inc(&fs_info->stripe_park_stats.data_rmw_by[idx]);
+       if (!(rbio->bioc->map_type & BTRFS_BLOCK_GROUP_DATA))
+               return;
+
+       if (idx == BTRFS_STRIPE_PAD_NO_RUN) {
+               struct btrfs_block_group *bg;
+
+               if (rbio->pad_norun < BTRFS_STRIPE_NORUN_NR)
+                       atomic64_inc(&fs_info->stripe_park_stats.data_rmw_norun[rbio->pad_norun]);
+               bg = btrfs_lookup_block_group(fs_info,
+                                             rbio->bioc->full_stripe_logical);
+               if (bg) {
+                       if (data_race(bg->ro))
+                               atomic64_inc(&fs_info->stripe_park_stats.data_rmw_norun_ro);
+                       btrfs_put_block_group(bg);
+               }
+       }
+
+       /* The writer: the first bio's inode. */
+       spin_lock(&rbio->bio_list_lock);
+       bio = bio_list_peek(&rbio->bio_list);
+       if (bio && btrfs_bio(bio)->inode)
+               reloc_writer = btrfs_is_data_reloc_root(btrfs_bio(bio)->inode->root);
+       spin_unlock(&rbio->bio_list_lock);
+       /* Disjoint from the frontier count, so the two can be summed. */
+       if (reloc_writer && idx != BTRFS_STRIPE_PAD_UNCOVERED)
+               atomic64_inc(&fs_info->stripe_park_stats.data_rmw_reloc_writer);
+}
+
 /*
  * Try to avoid the RMW read phase of a sub-stripe write by zero-filling the
  * uncovered data sectors.  Legal only where the stripe's run says a sector
@@ -3042,6 +3105,7 @@ static bool rmw_try_pad_full(struct btrfs_raid_bio *rbio)
        bool ok = false;
        int i;
 
+       rbio->pad_reason = RBIO_PAD_NOT_TRIED;
        if (!btrfs_test_opt(fs_info, STRIPE_ALLOC))
                return false;
 
@@ -3054,6 +3118,7 @@ static bool rmw_try_pad_full(struct btrfs_raid_bio *rbio)
        }
        if (!btrfs_stripe_run_pad_mask(fs_info, stripe_start, stripe_len, dead,
                                       unwritten, &info)) {
+               rbio_record_pad(rbio, &info);
                trace_btrfs_stripe_pad_decline(fs_info, stripe_start,
                                               stripe_len, 0, 0, nsectors,
                                               info.run_start, info.run_end,
@@ -3082,6 +3147,7 @@ static bool rmw_try_pad_full(struct btrfs_raid_bio *rbio)
                if (!test_bit(i, dead)) {
                        atomic64_inc(&fs_info->stripe_park_stats.pad_decline_uncovered);
                        info.reason = BTRFS_STRIPE_PAD_UNCOVERED;
+                       rbio_record_pad(rbio, &info);
                        trace_btrfs_stripe_pad_decline(fs_info, stripe_start,
                                                       stripe_len, 0, covered,
                                                       nsectors,
@@ -3102,6 +3168,8 @@ out:
        if (!ok)
                return false;
 
+       /* The oracle agreed: only a page allocation failure refuses now. */
+       rbio->pad_reason = BTRFS_STRIPE_PAD_OK;
        if (alloc_rbio_data_pages(rbio) < 0)
                return false;
 
@@ -3230,6 +3298,7 @@ static void rmw_rbio(struct btrfs_raid_bio *rbio)
                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);
+               rbio_count_rmw_reason(rbio);
                /* 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 b1913e70a21871474db5d009f210173d48d701ce..d33baefc34b138783e1fd0b3d6cfcd5ebd095c44 100644 (file)
@@ -144,6 +144,15 @@ struct btrfs_raid_bio {
        /* Flags that tell us if it is safe to merge with this bio. */
        unsigned long flags;
 
+       /*
+        * Why the last rmw_try_pad_full() on this rbio did not pad, so the
+        * read-modify-write it leads to can be attributed: enum
+        * btrfs_stripe_pad_reason (OK here means the data page allocation
+        * failed), or RBIO_PAD_NOT_TRIED; and for NO_RUN, which exit.
+        */
+       u8 pad_reason;
+       u8 pad_norun;
+
        /*
         * Set if we're doing a parity rebuild for a read from higher up, which
         * is handled differently from a parity rebuild as part of RMW.