* [PATCH] bcache: fix available bucket accounting in bucket allocation
@ 2026-11-03 7:04 Robert Pang
2026-11-03 22:27 ` Robert Pang
0 siblings, 1 reply; 2+ 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] 2+ 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
0 siblings, 0 replies; 2+ 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] 2+ messages in thread
end of thread, other threads:[~2026-11-03 22:27 UTC | newest]
Thread overview: 2+ 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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox