* [PATCH] bcache: fix available bucket accounting in bucket allocation @ 2026-11-03 7:04 Robert Pang 2026-11-03 22:27 ` Robert Pang 2026-12-03 15:15 ` Coly Li 0 siblings, 2 replies; 5+ messages in thread From: Robert Pang @ 2026-11-03 7:04 UTC (permalink / raw) To: colyli, kent.overstreet; +Cc: linux-bcache, linux-kernel, robertpang In bch_bucket_alloc(), a cache set's avail_nbuckets is decremented whenever a bucket is allocated, assuming that the bucket becomes unavailable once allocated. However, when a bucket is allocated but the GC mark is unset or reclaimable, it is available still. This false counter decrement leads to an inflated in-use measurement, which triggers cache bypasses once CUTOFF_CACHE_ADD is reached, persisting until the next garbage collection cycle. Fix this by ensuring avail_nbuckets is only decremented when the allocated bucket is already marked and not in the GC_MARK_RECLAIMABLE state. Signed-off-by: Robert Pang <robertpang@google.com> --- 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 = INITIAL_PRIO; } - if (ca->set->avail_nbuckets > 0) { + if (ca->set->avail_nbuckets > 0 && + GC_MARK(b) && GC_MARK(b) != GC_MARK_RECLAIMABLE) { ca->set->avail_nbuckets--; bch_update_bucket_in_use(ca->set, &ca->set->gc_stats); } -- 2.56.0.398.g882cbad275-goog ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] bcache: fix available bucket accounting in bucket allocation 2026-11-03 7:04 [PATCH] bcache: fix available bucket accounting in bucket allocation Robert Pang @ 2026-11-03 22:27 ` Robert Pang 2026-12-03 15:15 ` Coly Li 1 sibling, 0 replies; 5+ messages in thread From: Robert Pang @ 2026-11-03 22:27 UTC (permalink / raw) To: colyli, kent.overstreet; +Cc: linux-bcache, linux-kernel On Mon, Nov 2, 2026 at 11:04 PM Robert Pang <robertpang@google.com> wrote: > > In bch_bucket_alloc(), a cache set's avail_nbuckets is decremented whenever a > bucket is allocated, assuming that the bucket becomes unavailable once > allocated. However, when a bucket is allocated but the GC mark is unset or > reclaimable, it is available still. This false counter decrement leads to an > inflated in-use measurement, which triggers cache bypasses once CUTOFF_CACHE_ADD > is reached, persisting until the next garbage collection cycle. > > Fix this by ensuring avail_nbuckets is only decremented when the allocated > bucket is already marked and not in the GC_MARK_RECLAIMABLE state. > > Signed-off-by: Robert Pang <robertpang@google.com> > --- > 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 = INITIAL_PRIO; > } > > - if (ca->set->avail_nbuckets > 0) { > + if (ca->set->avail_nbuckets > 0 && > + GC_MARK(b) && GC_MARK(b) != GC_MARK_RECLAIMABLE) { > ca->set->avail_nbuckets--; > bch_update_bucket_in_use(ca->set, &ca->set->gc_stats); > } > -- > 2.56.0.398.g882cbad275-goog > Hi all, Here is some more context on this fix. This issue was observed during the investigation of [1]. We believe this is a latent issue exposed by longer garbage collection durations following commit 009073806fbc ("bcache: reduce gc latency by processing less nodes and sleep less time") to reduce front IO tail latency during GC. Best regards, Robert Pang [1] https://lore.kernel.org/linux-bcache/CAJhEC04+1tu_vGVNfc0TicEb9T2wPpFPLqmQRqX0G_k7M3sBwQ@mail.gmail.com/T/#m0be3c1a4a63427ed7b233a3f753a55fcded69d05 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] bcache: fix available bucket accounting in bucket allocation 2026-11-03 7:04 [PATCH] bcache: fix available bucket accounting in bucket allocation Robert Pang 2026-11-03 22:27 ` Robert Pang @ 2026-12-03 15:15 ` Coly Li 2026-12-03 23:03 ` Robert Pang 1 sibling, 1 reply; 5+ messages in thread From: Coly Li @ 2026-12-03 15:15 UTC (permalink / raw) To: Robert Pang; +Cc: kent.overstreet, linux-bcache, linux-kernel 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 whenever a > bucket is allocated, assuming that the bucket becomes unavailable once > allocated. However, when a bucket is allocated but the GC mark is unset or > reclaimable, it is available still. This false counter decrement leads to an > inflated in-use measurement, which triggers cache bypasses once CUTOFF_CACHE_ADD > is reached, persisting until the next garbage collection cycle. > > Fix this by ensuring avail_nbuckets is only decremented when the allocated > bucket is already marked and not in the GC_MARK_RECLAIMABLE state. > > Signed-off-by: Robert Pang <robertpang@google.com> > --- > 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 = INITIAL_PRIO; > } > > - if (ca->set->avail_nbuckets > 0) { > + if (ca->set->avail_nbuckets > 0 && > + GC_MARK(b) && GC_MARK(b) != GC_MARK_RECLAIMABLE) { 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) != GC_MARK_RECLAIMABLE)' will be false, so a bucket allocated but avail_nbuckets counter isn't decreased. > ca->set->avail_nbuckets--; > bch_update_bucket_in_use(ca->set, &ca->set->gc_stats); > } Thanks. Coly Li ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] bcache: fix available bucket accounting in bucket allocation 2026-12-03 15:15 ` Coly Li @ 2026-12-03 23:03 ` Robert Pang 2026-12-04 12:17 ` Coly Li 0 siblings, 1 reply; 5+ messages in thread From: Robert Pang @ 2026-12-03 23:03 UTC (permalink / raw) To: Coly Li; +Cc: kent.overstreet, linux-bcache, linux-kernel Hi Coly Thank you for your review. Please see my comments and question below. On Thu, Dec 3, 2026 at 7:15 AM Coly Li <colyli@fygo.io> 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 whenever a > > bucket is allocated, assuming that the bucket becomes unavailable once > > allocated. However, when a bucket is allocated but the GC mark is unset or > > reclaimable, it is available still. This false counter decrement leads to an > > inflated in-use measurement, which triggers cache bypasses once CUTOFF_CACHE_ADD > > is reached, persisting until the next garbage collection cycle. > > > > Fix this by ensuring avail_nbuckets is only decremented when the allocated > > bucket is already marked and not in the GC_MARK_RECLAIMABLE state. > > > > Signed-off-by: Robert Pang <robertpang@google.com> > > --- > > 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 = INITIAL_PRIO; > > } > > > > - if (ca->set->avail_nbuckets > 0) { > > + if (ca->set->avail_nbuckets > 0 && > > + GC_MARK(b) && GC_MARK(b) != 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) != GC_MARK_RECLAIMABLE)' > will be false, so a bucket allocated but avail_nbuckets counter isn't > decreased. I've been digging into the bcache allocation path and noticed we don't have any explicit documentation on avail_nbuckets in bcache.h. 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: bch_btree_gc_finish(): c->avail_nbuckets = 0; for_each_bucket(b, ca) { if (atomic_read(&b->pin)) continue; BUG_ON(!GC_MARK(b) && GC_SECTORS_USED(b)); if (!GC_MARK(b) || GC_MARK(b) == GC_MARK_RECLAIMABLE) c->avail_nbuckets++; } And below, it is used to calculate dirty buckets: __update_writeback_rate(): int64_t dirty_buckets = c->nbuckets - c->avail_nbuckets; Looking at these two pieces together, my takeaway is that avail_nbuckets is meant to track buckets that are materially available—meaning both completely free buckets (!GC_MARK) and those that are allocated but reclaimable (not dirty and not holding metadata). 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? > > ca->set->avail_nbuckets--; > > bch_update_bucket_in_use(ca->set, &ca->set->gc_stats); > > } Best regards Robert Pang ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] bcache: fix available bucket accounting in bucket allocation 2026-12-03 23:03 ` Robert Pang @ 2026-12-04 12:17 ` Coly Li 0 siblings, 0 replies; 5+ messages in thread From: Coly Li @ 2026-12-04 12:17 UTC (permalink / raw) To: Robert Pang; +Cc: kent.overstreet, linux-bcache, linux-kernel On Thu, Dec 03, 2026 at 03:03:56PM +0800, Robert Pang wrote: > Hi Coly > > Thank you for your review. Please see my comments and question below. > > On Thu, Dec 3, 2026 at 7:15 AM Coly Li <colyli@fygo.io> 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 whenever a > > > bucket is allocated, assuming that the bucket becomes unavailable once > > > allocated. However, when a bucket is allocated but the GC mark is unset or > > > reclaimable, it is available still. This false counter decrement leads to an > > > inflated in-use measurement, which triggers cache bypasses once CUTOFF_CACHE_ADD > > > is reached, persisting until the next garbage collection cycle. > > > > > > Fix this by ensuring avail_nbuckets is only decremented when the allocated > > > bucket is already marked and not in the GC_MARK_RECLAIMABLE state. > > > > > > Signed-off-by: Robert Pang <robertpang@google.com> > > > --- > > > 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 = INITIAL_PRIO; > > > } > > > > > > - if (ca->set->avail_nbuckets > 0) { > > > + if (ca->set->avail_nbuckets > 0 && > > > + GC_MARK(b) && GC_MARK(b) != 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) != GC_MARK_RECLAIMABLE)' > > will be false, so a bucket allocated but avail_nbuckets counter isn't > > decreased. > > I've been digging into the bcache allocation path and noticed we don't > have any explicit documentation on avail_nbuckets in bcache.h. > > 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: > > bch_btree_gc_finish(): > > c->avail_nbuckets = 0; > for_each_bucket(b, ca) { > if (atomic_read(&b->pin)) > continue; > > BUG_ON(!GC_MARK(b) && GC_SECTORS_USED(b)); > if (!GC_MARK(b) || GC_MARK(b) == GC_MARK_RECLAIMABLE) > c->avail_nbuckets++; > } > > And below, it is used to calculate dirty buckets: > > __update_writeback_rate(): > > int64_t dirty_buckets = c->nbuckets - c->avail_nbuckets; > > Looking at these two pieces together, my takeaway is that > avail_nbuckets is meant to track buckets that are materially > available—meaning both completely free buckets (!GC_MARK) and those > 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. > 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? > > > > 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 ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-04 12:18 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-03 7:04 [PATCH] bcache: fix available bucket accounting in bucket allocation Robert Pang 2026-11-03 22:27 ` Robert Pang 2026-12-03 15:15 ` Coly Li 2026-12-03 23:03 ` Robert Pang 2026-12-04 12:17 ` Coly Li
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox