From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.127.231.55 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796386687; b=IGlCgyDTI4yHGcDVDdrDbl+UMWUo6BaAQU3sBhTDlmhrJp7qlM3qp0LqvgmfFeBifAfi EEcvVUAzNwg0KbDpL5ME9GeeVFoTwK0zbDdRcE/UeUf5qymGs8nllJeCxstelSap3N90n ipBDXbU0LJnSMfoank45/QV54cGxVsTPZ0SPnhnt3GLNEWmkqx3h9okJvSjJGgfiDktlz Xdyqts+xr6cmDJsGh/jgXCjYH/vPY/Nm45IfQtbwJz8fNv++UT3yGZGX/oJqArZ5099vf Pkrine/VpNJJlsocToG3sLik+Hzkie3dbbI4h1OGxr4xhZ8COzvarVadcVezMilwx0A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796386687; h=DMARC-Filter:DKIM-Signature:Date:X-Lms-Return-Path:Cc: X-Original-From:Subject:Mime-Version:Content-Transfer-Encoding: References:Content-Type:Content-Disposition:Message-Id:From:Received: In-Reply-To:To; bh=SAuvP+CSTD8AzTfh0kpfdOUhh2r0agsV6/NfVo7Cd+U=; b=DWuRaVPj/fkcyEpJdTVP/rwt2nByoEHWtUBDgPKG5G7zzE+MFgfEv9PmVrpW2fwzthxR imcp25C9WtgbQPwL8Fs4oLIHQffUN2S+92QwvvwksA+4C2pcqQI5sWRs7AeJpjak2G08D HRgWoaVFynmSl4phVa3iA0SSgNKj51z8lPMHwttS4YLzS9dWPDgm56vn9GfjwsFafiRlc 00J9E6JOjcvxwgW+uxSwikcjrqrIBtq1/3nf60HB9gmYmi/CI+CcyXTBfQJ5TDoEqMFIs esDJONzplb9O4uxv21bq3w26cbyaYCJ43QiFgV1Yuq0o8HvXOFFwHsIDdFzq93P/o9A== 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=M5ryLgFV; arc=none smtp.remote-ip=209.127.231.55 Received: from va-2-55.ptr.blmpb.com (va-2-55.ptr.blmpb.com [209.127.231.55]) (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 EFA58606A322 for ; Fri, 4 Dec 2026 12:18:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EFA58606A322 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, unprotected) 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=M5ryLgFV DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=s1; d=fygo-io.20200929.dkim.larksuite.com; t=1796386676; h=from:subject:mime-version:from:date:message-id:subject:to:cc: reply-to:content-type:mime-version:in-reply-to:message-id; bh=SAuvP+CSTD8AzTfh0kpfdOUhh2r0agsV6/NfVo7Cd+U=; b=M5ryLgFVDJmYo9QlGKBI75erYEXHwgQ8oyi3FnGTWNp77uSYrpsUOfCkQG7/6YFXJgotLy QEJIPIeQjg7YSUiNfnTcNsApFhAzA8REPWyiNLkPSo6T1xtL8OfeZ3gKLxUwDD2YQXujy7 5Y+fo2+Ngb9gVG/j/HdcJ4cn+LfZ+7Jb84AKIPUwFCqRxLeD4ZsKco9/owSh6gHtc4zPWJ HFQ22E/L2BGkHhvfMviTG5NIHXIKKkUz3b5KGK6p6Z5ZfuU3J14wPirIMyfxgO89tsTFIG j3G/0OYej3G2m5elj/iw2b21waW2IFobG5yTi7lImeOiJ7n0WeAPHbVYiyKVfQ== Date: Fri, 4 Dec 2026 20:17:53 +0800 X-Lms-Return-Path: Cc: , , X-Original-From: Coly Li Subject: Re: [PATCH] bcache: fix available bucket accounting in bucket allocation 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 Content-Transfer-Encoding: quoted-printable References: <20261103070407.1211071-1-robertpang@google.com> Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Message-Id: From: "Coly Li" Received: from studio.local ([120.245.64.92]) by smtp.larksuite.com with ESMTPS; Fri, 04 Dec 2026 12:17:55 +0000 In-Reply-To: To: "Robert Pang" On Thu, Dec 03, 2026 at 03:03:56PM +0800, Robert Pang wrote: > Hi Coly >=20 > Thank you for your review. Please see my comments and question below. >=20 > On Thu, Dec 3, 2026 at 7:15=E2=80=AFAM Coly Li wrote: > > > > Hi Robert, > > > > NACK this patch. Let me explain inline. > > > > On Mon, Nov 02, 2026 at 11:04:06PM +0800, Robert Pang wrote: > > > In bch_bucket_alloc(), a cache set's avail_nbuckets is decremented wh= enever a > > > bucket is allocated, assuming that the bucket becomes unavailable onc= e > > > allocated. However, when a bucket is allocated but the GC mark is uns= et or > > > reclaimable, it is available still. This false counter decrement lead= s to an > > > inflated in-use measurement, which triggers cache bypasses once CUTOF= F_CACHE_ADD > > > is reached, persisting until the next garbage collection cycle. > > > > > > Fix this by ensuring avail_nbuckets is only decremented when the allo= cated > > > bucket is already marked and not in the GC_MARK_RECLAIMABLE state. > > > > > > Signed-off-by: Robert Pang > > > --- > > > drivers/md/bcache/alloc.c | 3 ++- > > > 1 file changed, 2 insertions(+), 1 deletion(-) > > > > > > diff --git a/drivers/md/bcache/alloc.c b/drivers/md/bcache/alloc.c > > > index 3ec240ec685d..2d0cb9b414ac 100644 > > > --- a/drivers/md/bcache/alloc.c > > > +++ b/drivers/md/bcache/alloc.c > > > @@ -446,7 +446,8 @@ long bch_bucket_alloc(struct cache *ca, unsigned = int reserve, bool wait) > > > b->prio =3D INITIAL_PRIO; > > > } > > > > > > - if (ca->set->avail_nbuckets > 0) { > > > + if (ca->set->avail_nbuckets > 0 && > > > + GC_MARK(b) && GC_MARK(b) !=3D GC_MARK_RECLAIMABLE) { > > ca->set->avail_nbuckets--; > > bch_update_bucket_in_use(ca->set, &ca->set->gc_stats); > > } > > > > An allocated bucket can still have GC_MARK(b) as GC_MARK_RECLAIMABLE. > > Because current bcache code permits a bucket to be reused (if b->pin > > is 0) before gc thread finishes whole garbage collection procedure. > > In this case, the condition > > 'GC_MARK(b) && GC_MARK(b) !=3D GC_MARK_RECLAIMABLE)' > > will be false, so a bucket allocated but avail_nbuckets counter isn't > > decreased. >=20 > I've been digging into the bcache allocation path and noticed we don't > have any explicit documentation on avail_nbuckets in bcache.h. >=20 > To map out its actual behavior, I traced how the field is handled > across different functions. Below, I see it is reset and rebuilt like > this: >=20 > bch_btree_gc_finish(): >=20 > c->avail_nbuckets =3D 0; > for_each_bucket(b, ca) { > if (atomic_read(&b->pin)) > continue; >=20 > BUG_ON(!GC_MARK(b) && GC_SECTORS_USED(b)); > if (!GC_MARK(b) || GC_MARK(b) =3D=3D GC_MARK_RECLAIMABLE) > c->avail_nbuckets++; > } >=20 > And below, it is used to calculate dirty buckets: >=20 > __update_writeback_rate(): >=20 > int64_t dirty_buckets =3D c->nbuckets - c->avail_nbuckets; >=20 > Looking at these two pieces together, my takeaway is that > avail_nbuckets is meant to track buckets that are materially > available=E2=80=94meaning both completely free buckets (!GC_MARK) and tho= se > that are allocated but reclaimable (not dirty and not holding > metadata). > Yes, this is the usage of c->avail_nbuckets. Another location is gc_stats.in_use, it contributes to decide the bypass behavior. And yes, it is the buckets number of not-in-btree buckets and reclaimable buckets. But this value is not a realtime counter, that measn, the latest value is just when gc finished. Then most of the time this counter doesn't represent the current buckets status. =20 > The issue I'm seeing is that the way we decrement avail_nbuckets in > bch_bucket_alloc() can drift and get out of sync with this definition. > I wanted to run my analysis by you. Does this interpretation of > avail_nbuckets and therefore the proposed patch match its intent? >=20 > > > ca->set->avail_nbuckets--; > > > bch_update_bucket_in_use(ca->set, &ca->set->gc_stats); > > > } I would treat it as best effort, or have to. When inc/dec c->avail_nbuckets, c->bucket_lock should be acquired. So if you want to keep c->avail_nbuckets to be accurate, you also need to inc c->avail_nbuckets when the dirty bucket to be cleaned after writeback, and dec c->avail_nbuckets when mark a bucket to GC_MARK_DIRTY by writing data to an empty bucket. Otherwise, this counter is always inaccurate. This is why c->avail_buckets changes when, - Resets to latest state after gc finishes. - Increases when free a bucket - Decreases when allocate a bucket Because keeping this counter always updated without locking cost is hard, so I treat it as a best effort to update c->avail_buckets. And current code results more aggressive dirty data writeback, gc trigger and data bypass, IMHO this is intended as design. Just my own thought, FYI. Thanks. Coly Li