* [PATCH] bcache: wait for RCU grace period before rehashing btree node
@ 2026-12-01 16:34 Bilal Hashim
2026-12-03 15:25 ` Coly Li
0 siblings, 1 reply; 2+ messages in thread
From: Bilal Hashim @ 2026-12-01 16:34 UTC (permalink / raw)
To: Coly Li, Kent Overstreet; +Cc: linux-bcache, linux-kernel
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_);
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] bcache: wait for RCU grace period before rehashing btree node
2026-12-01 16:34 [PATCH] bcache: wait for RCU grace period before rehashing btree node Bilal Hashim
@ 2026-12-03 15:25 ` Coly Li
0 siblings, 0 replies; 2+ messages in thread
From: Coly Li @ 2026-12-03 15:25 UTC (permalink / raw)
To: Bilal Hashim; +Cc: Kent Overstreet, linux-bcache, linux-kernel
Hmm, I am not able to identify whether you are real human being ...
Coly Li
On Wed, Dec 02, 2026 at 12:34:05AM +0800, Bilal Hashim wrote:
> 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_);
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-03 15:25 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 16:34 [PATCH] bcache: wait for RCU grace period before rehashing btree node Bilal Hashim
2026-12-03 15:25 ` Coly Li
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox