From: Bilal Hashim <AeeSec@126.com>
To: Coly Li <colyli@fygo.io>, Kent Overstreet <kent.overstreet@linux.dev>
Cc: linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] bcache: wait for RCU grace period before rehashing btree node
Date: Wed, 02 Dec 2026 00:34:05 +0800 [thread overview]
Message-ID: <179614284577.19856.1541329382006666124@126.com> (raw)
In mca_alloc(), when a cached struct btree node is reused from
c->btree_cache_freeable, c->btree_cache_freed, or cannibalized from
c->btree_cache via mca_cannibalize(), b->hash is unlinked with
hlist_del_init_rcu() (either earlier in mca_bucket_free() or at the
out: label in mca_alloc()) and immediately re-inserted into
c->bucket_hash[] with hlist_add_head_rcu(&b->hash, mca_hash(c, k))
without waiting for an RCU grace period. In addition, bkey_copy(&b->key, k)
overwrites b->key before b->hash is unlinked.
Meanwhile, bch_btree_node_get() looks up nodes via mca_find() under
rcu_read_lock() without holding c->bucket_lock, traversing
c->bucket_hash[] with hlist_for_each_entry_rcu() and comparing
PTR_HASH(c, &b->key).
Reusing an hlist_node without a grace period overwrites b->hash.next
while concurrent RCU readers may still be traversing the old hash bucket.
This can cause mca_find() to jump into a different hash bucket (missing
valid cached nodes) or loop indefinitely inside an RCU read-side critical
section (RCU stall / soft lockup).
For reused btree nodes in mca_alloc(), unlink b->hash with
hlist_del_init_rcu() and wait for an RCU grace period with
synchronize_rcu() before overwriting b->key and inserting b->hash into
the new bucket. Freshly allocated nodes from mca_bucket_alloc() were
never hashed and skip the grace period.
Found by manual code inspection. Verified in a 4-vCPU QEMU guest with
CONFIG_BCACHE=y, CONFIG_PROVE_LOCKING=y, and CONFIG_DEBUG_ATOMIC_SLEEP=y
under concurrent writeback I/O and drop_caches shrinker pressure without
lockdep, RCU, or sleeping-in-atomic warnings.
Fixes: cafe56359144 ("bcache: A block layer cache")
Cc: stable@vger.kernel.org
Signed-off-by: Bilal Hashim <AeeSec@126.com>
---
drivers/md/bcache/btree.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index 27a129d..7d4a11c 100644
--- a/drivers/md/bcache/btree.c
+++ b/drivers/md/bcache/btree.c
@@ -923,6 +923,7 @@ static struct btree *mca_alloc(struct cache_set *c, struct btree_op *op,
struct bkey *k, int level)
{
struct btree *b;
+ bool reused = true;
BUG_ON(current->bio_list);
@@ -957,12 +958,17 @@ static struct btree *mca_alloc(struct cache_set *c, struct btree_op *op,
BUG_ON(!down_write_trylock(&b->lock));
if (!b->keys.set->data)
goto err;
+ reused = false;
out:
BUG_ON(b->io_mutex.count != 1);
+ if (reused) {
+ hlist_del_init_rcu(&b->hash);
+ synchronize_rcu();
+ }
+
bkey_copy(&b->key, k);
list_move(&b->list, &c->btree_cache);
- hlist_del_init_rcu(&b->hash);
hlist_add_head_rcu(&b->hash, mca_hash(c, k));
lock_set_subclass(&b->lock.dep_map, level + 1, _THIS_IP_);
next reply other threads:[~2026-12-01 16:34 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 16:34 Bilal Hashim [this message]
2026-12-03 15:25 ` Coly Li
2026-12-04 0:44 ` Bilal Hashim
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=179614284577.19856.1541329382006666124@126.com \
--to=aeesec@126.com \
--cc=colyli@fygo.io \
--cc=kent.overstreet@linux.dev \
--cc=linux-bcache@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®