]> git.hungrycats.org Git - linux/commitdiff
DO-NOT-UPSTREAM: btrfs: raid56: debug knob for the rbio stripe cache size 7.3/topics/test-knobs
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Thu, 1 Oct 2026 07:54:55 +0000 (03:54 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:38:19 +0000 (18:38 -0500)
Test fixture, not for submission.  A read-modify-write whose stripe is
still in the rbio stripe cache takes the other strips from memory instead
of reading them, so a test that injects faults into reads (dm-stripfault)
cannot reach the RMW read path for recently written stripes.

With CONFIG_BTRFS_DEBUG, /sys/fs/btrfs/<uuid>/debug/rbio_cache_size
replaces the RBIO_CACHE_SIZE limit (default unchanged, 1024); 0 disables
the cache, except for the stripes stripe_alloc pins there until their run
has written all of them.  Lowering it empties the cache, and cache_rbio()
passes the limit to rbio_cache_prune(), which prunes as far as needed, so
a lowered limit holds.

It sits in topics/test-knobs, on top of topics/stripe-alloc, whose rbio
pins and cache prune it adapts to.

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

index ad0569249e8f2e2d859091260d39eca5c65254d8..6ad529825d9139c128033060ec625adce6e86ee1 100644 (file)
@@ -673,6 +673,10 @@ struct btrfs_fs_info {
         * trying to mod the same stripe at the same time.
         */
        struct btrfs_stripe_hash_table *stripe_hash_table;
+#ifdef CONFIG_BTRFS_DEBUG
+       /* Size limit of the rbio stripe cache (debug/rbio_cache_size). */
+       unsigned int rbio_cache_size;
+#endif
 
        /*
         * This protects the ordered operations list only while we are
index 7e573b61f5f6b04c04d67d1483a52064f8e0c01b..57d5d45c044f624f8b0b02fe6922f91d12fa9f9f 100644 (file)
@@ -279,6 +279,9 @@ int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info)
        struct btrfs_stripe_hash *h;
        unsigned int num_entries = 1U << BTRFS_STRIPE_HASH_TABLE_BITS;
 
+#ifdef CONFIG_BTRFS_DEBUG
+       WRITE_ONCE(info->rbio_cache_size, RBIO_CACHE_SIZE);
+#endif
        if (info->stripe_hash_table)
                return 0;
 
@@ -644,6 +647,23 @@ static void btrfs_clear_rbio_cache(struct btrfs_fs_info *info)
        spin_unlock(&table->cache_lock);
 }
 
+#ifdef CONFIG_BTRFS_DEBUG
+/*
+ * Test knob (debug/rbio_cache_size): with a small or zero limit, a
+ * read-modify-write cannot take its stripe from the cache and must read it,
+ * which is what a fault-injecting test needs to see.  Lowering it empties the
+ * cache, so the new limit holds from the next rbio on.
+ */
+void btrfs_set_rbio_cache_size(struct btrfs_fs_info *info, unsigned int size)
+{
+       unsigned int old = READ_ONCE(info->rbio_cache_size);
+
+       WRITE_ONCE(info->rbio_cache_size, size);
+       if (size < old && info->stripe_hash_table)
+               btrfs_clear_rbio_cache(info);
+}
+#endif
+
 /*
  * Send every parked rbio down and stop the park timer, before the raid56
  * workqueue is destroyed: the timer queues its expired rbios there.  By
@@ -909,15 +929,29 @@ void btrfs_rbio_cache_usage(struct btrfs_fs_info *fs_info, int *cached,
  * If the size of the rbio cache is too big, we
  * prune an item.
  */
+/* How many rbios the stripe cache keeps (0: none). */
+static unsigned int rbio_cache_limit(const struct btrfs_fs_info *fs_info)
+{
+#ifdef CONFIG_BTRFS_DEBUG
+       return READ_ONCE(fs_info->rbio_cache_size);
+#else
+       return RBIO_CACHE_SIZE;
+#endif
+}
+
 static void cache_rbio(struct btrfs_raid_bio *rbio)
 {
        struct btrfs_stripe_hash_table *table;
+       const unsigned int limit = rbio_cache_limit(rbio->bioc->fs_info);
        bool pin;
 
        if (!test_bit(RBIO_CACHE_READY_BIT, &rbio->flags))
                return;
-
        pin = rbio_needs_pin(rbio);
+       /* Even a disabled cache keeps a stripe its run has not finished. */
+       if (!limit && !pin)
+               return;
+
        table = rbio->bioc->fs_info->stripe_hash_table;
 
        spin_lock(&table->cache_lock);
@@ -940,7 +974,8 @@ static void cache_rbio(struct btrfs_raid_bio *rbio)
 
        spin_unlock(&rbio->bio_list_lock);
 
-       rbio_cache_prune(table, rbio, RBIO_CACHE_SIZE);
+       /* Prunes as far as needed: the limit can have been lowered. */
+       rbio_cache_prune(table, rbio, limit);
 
        spin_unlock(&table->cache_lock);
 }
index 78be1ac524b07d315eabef49ecb1678c252c22fe..7c39b28ec5c81d62c58e2f24f9ba8215f491e9f3 100644 (file)
@@ -328,5 +328,8 @@ 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 *live_by_class);
 void btrfs_rbio_unpin_stale(struct btrfs_fs_info *fs_info);
+#ifdef CONFIG_BTRFS_DEBUG
+void btrfs_set_rbio_cache_size(struct btrfs_fs_info *info, unsigned int size);
+#endif
 
 #endif
index 60b83167907affaa8e2e43c66f377d3f7a16b1ec..bbccbf13e3aafff8576888d58bbc7b8cb0bae3be 100644 (file)
@@ -634,6 +634,32 @@ static const struct attribute *discard_attrs[] = {
 
 #ifdef CONFIG_BTRFS_DEBUG
 
+/* The raid56 stripe cache limit (raid56.c), 0 to disable the cache. */
+static ssize_t btrfs_rbio_cache_size_show(struct kobject *kobj,
+                                         struct kobj_attribute *a, char *buf)
+{
+       struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
+
+       return sysfs_emit(buf, "%u\n", READ_ONCE(fs_info->rbio_cache_size));
+}
+
+static ssize_t btrfs_rbio_cache_size_store(struct kobject *kobj,
+                                          struct kobj_attribute *a,
+                                          const char *buf, size_t len)
+{
+       struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
+       unsigned int val;
+       int ret;
+
+       ret = kstrtouint(buf, 10, &val);
+       if (ret)
+               return ret;
+       btrfs_set_rbio_cache_size(fs_info, val);
+       return len;
+}
+BTRFS_ATTR_RW(debug_mount, rbio_cache_size, btrfs_rbio_cache_size_show,
+             btrfs_rbio_cache_size_store);
+
 /*
  * Audit every data read-modify-write against the committed extent tree
  * (raid56.c): a tree search per RMW, so off by default.
@@ -701,6 +727,7 @@ BTRFS_ATTR(debug_mount, stripe_rmw_audit_stats,
 static const struct attribute *btrfs_debug_mount_attrs[] = {
        BTRFS_ATTR_PTR(debug_mount, stripe_rmw_audit),
        BTRFS_ATTR_PTR(debug_mount, stripe_rmw_audit_stats),
+       BTRFS_ATTR_PTR(debug_mount, rbio_cache_size),
        NULL,
 };