From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.127.231.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796311548; b=jOtfVh8vh0WnYhwkGtVk/3Zze9WBXGZTtrR+Z8KW4YjUyfMvcy4rhFtQfv5y7vJm7/o2 K7zHxDeMunI2xLLb+SKSL/Ttos1N1UG5Ku1C+yPZusbtdvMJsXwhxThkg+ICsrZW2eIpz YN6xJ3+46S0zjabpU8NtnrelS9523weBrubRqetduJXNSAyWfRA47WaguY4Hv1ez0PEt6 W3y7BwoU2ASnOYPjCpieHGAsaBonv0uyvSXwVSpnsBPYEpsUQKYx9ttMRvxVqlGBAC0Qw /S7atcgyWQeHOb7eWAyWaZKPObd7jYXZUJ0FrIwyLO+qVcGspf33HfFEdykL274vmBQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796311548; h=DMARC-Filter:DKIM-Signature:Cc:Subject:In-Reply-To:References: Content-Disposition:Content-Type:From:Mime-Version:X-Lms-Return-Path: Received:X-Original-From:Content-Transfer-Encoding:To:Date: Message-Id; bh=2wlXFHh0Jry+CYsQIWT8lm8uaL7U7e29cVdREbZWjjc=; b=nZPYejMDZwNPP05JZvFxv/AgNCVj9MZJBcx5uBA0LSn46Ay1fCMBKEYPXUwrIwP6jQJD 72iyElagsEm9XPDpKNlhNuQsBwX2ntN2fyPA9jrdnpnqvMzX6P+RbLqWJwr+21RUxt6k4 7upSbTJFkketDIo8+/ns/g6rKGdhOAwWwgQiIoNBKNoppgTdnsklhXMIAnYAb3o7BdnEW 4DGEDa6FqNVXXzoOL7bJWQGKcvLQz1S1zkM71BVsbCWgz7AZjHGNxnYiohaFwmebXf35O dIFL8iopZHkdhoCecYdaazcSApIMZ4K7yOc8ERaADDTmIv5i6hv85YLdb+og3M2bb5w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=fygo.io; spf=pass smtp.mailfrom=fygo.io; dkim=pass header.d=fygo-io.20200929.dkim.larksuite.com header.i=@fygo-io.20200929.dkim.larksuite.com header.a=rsa-sha256 header.s=s1 header.b="b1n/g6n7"; arc=none smtp.remote-ip=209.127.231.47 Received: from va-2-47.ptr.blmpb.com (va-2-47.ptr.blmpb.com [209.127.231.47]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CCE3F606A32F for ; Thu, 3 Dec 2026 15:25:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CCE3F606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=fygo.io Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=fygo.io Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=fygo-io.20200929.dkim.larksuite.com header.i=@fygo-io.20200929.dkim.larksuite.com header.a=rsa-sha256 header.s=s1 header.b=b1n/g6n7 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=s1; d=fygo-io.20200929.dkim.larksuite.com; t=1796311540; h=from:subject:mime-version:from:date:message-id:subject:to:cc: reply-to:content-type:mime-version:in-reply-to:message-id; bh=2wlXFHh0Jry+CYsQIWT8lm8uaL7U7e29cVdREbZWjjc=; b=b1n/g6n7ydTBIHs5/m9W53/GatgvuCPdxzE63XerOzrrfMrHtCTfs74zv4j1iT2ddgh+B4 2RodAI7hEmsCIb5fqwz80CbIYHs3FZCERhgRbkwrWB0q3CyO5FCUeAAeuAk0RAVM82myR/ B6+m5FOmHM0wICWO2sp5r33pVSu5Z9BaR19DICV0CHm5V80z7vJh11irva4Qym1dC1wYiD L13rZw4FcaDVJ2fWdSZpMynS6hv4TisFq5hvb7yDe40p4DuGxdLvYnzk5XefAtD9mYT7Bc zLd8FETfIPV1+Ipuy4xK9b5pdHTH3Ct69z0s/Us5BxFJT2IAJn+DHcVXGu62oA== Cc: "Kent Overstreet" , , Subject: Re: [PATCH] bcache: wait for RCU grace period before rehashing btree node In-Reply-To: <179614284577.19856.1541329382006666124@126.com> References: <179614284577.19856.1541329382006666124@126.com> Content-Disposition: inline Content-Type: text/plain; charset=UTF-8 From: "Coly Li" X-Mailing-List: linux-bcache@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Lms-Return-Path: Received: from studio.local ([120.245.64.92]) by smtp.larksuite.com with ESMTPS; Thu, 03 Dec 2026 15:25:39 +0000 X-Original-From: Coly Li Content-Transfer-Encoding: 7bit To: "Bilal Hashim" Date: Thu, 3 Dec 2026 23:25:37 +0800 Message-Id: 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 > --- > 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_);