All of lore.kernel.org
 help / color / mirror / Atom feed
From: Keith Busch <kbusch@kernel.org>
To: Chris Mason <clm@meta.com>
Cc: Christoph Hellwig <hch@lst.de>, Jens Axboe <axboe@kernel.dk>,
	Carlos Maiolino <cem@kernel.org>,
	Damien Le Moal <dlemoal@kernel.org>,
	Hans Holmberg <hans.holmberg@wdc.com>,
	linux-block@vger.kernel.org, linux-xfs@vger.kernel.org,
	Carlos Maiolino <cmaiolino@redhat.com>
Subject: Re: [PATCH 3/3] xfs: rework zone GC buffer management
Date: Mon, 26 Jan 2026 13:30:54 -0700	[thread overview]
Message-ID: <aXfO_ghd0yoKK8dm@kbusch-mbp> (raw)
In-Reply-To: <20260125000314.561545-1-clm@meta.com>

On Sat, Jan 24, 2026 at 04:03:04PM -0800, Chris Mason wrote:
> On Wed, 14 Jan 2026 14:06:43 +0100 Christoph Hellwig <hch@lst.de> wrote:
> > @@ -590,7 +578,12 @@ xfs_zone_gc_ensure_target(
> >  xfs_zone_gc_scratch_available(
> >  	struct xfs_zone_gc_data	*data)
> >  {
> > -	return XFS_GC_CHUNK_SIZE - data->scratch[data->scratch_idx].offset;
> > +	if (!data->scratch_tail)
> > +		return data->scratch_size - data->scratch_head;
> > +
> > +	if (!data->scratch_head)
> > +		return data->scratch_tail;
> > +	return (data->scratch_size - data->scratch_head) + data->scratch_tail;
> >  }
> 
> Can this function correctly distinguish between an empty and full ring
> buffer?
> 
> When scratch_head wraps back to 0 and scratch_tail is also 0 (because no
> I/O has completed yet), the first condition returns scratch_size (2MB),
> indicating the buffer is empty.  But if the buffer just filled completely
> and head wrapped to 0, the buffer is actually full, and the available
> space should be 0.
> 
> Consider this sequence in xfs_zone_gc_handle_work():
>   - Initial: head=0, tail=0, available=2MB (correct, empty)
>   - After 1MB chunk: head=1MB, tail=0, available=1MB (correct)
>   - After another 1MB chunk: head=0 (wrapped), tail=0, available=2MB (wrong)
> 
> The buffer is full but the function reports it as empty.  The while loop
> in xfs_zone_gc_handle_work() calling xfs_zone_gc_start_chunk() would then
> continue adding more data, overwriting the buffer contents from the first
> chunk before its I/O completes.

I think you're right that ring wrap can't distinguish full vs. empty here.
 
> A common solution is to track a separate count, or to sacrifice one slot
> so head == tail always means empty, and head == tail-1 means full.

The buffer size is a power of two, so I suggest just let scratch_head
and scratch_tail only increment without modulo, and rely on the unsigned
int wrapping. We can get the actual offset by masking the head with the
scratch size.

---
diff --git a/fs/xfs/xfs_zone_gc.c b/fs/xfs/xfs_zone_gc.c
index ba4f8e011e36c..7d2bd0dc8b322 100644
--- a/fs/xfs/xfs_zone_gc.c
+++ b/fs/xfs/xfs_zone_gc.c
@@ -578,12 +578,7 @@ static unsigned int
 xfs_zone_gc_scratch_available(
 	struct xfs_zone_gc_data	*data)
 {
-	if (!data->scratch_tail)
-		return data->scratch_size - data->scratch_head;
-
-	if (!data->scratch_head)
-		return data->scratch_tail;
-	return (data->scratch_size - data->scratch_head) + data->scratch_tail;
+	return data->scratch_size - (data->scratch_head - data->scratch_tail);
 }
 
 static bool
@@ -663,7 +658,7 @@ xfs_zone_gc_add_data(
 {
 	struct xfs_zone_gc_data	*data = chunk->data;
 	unsigned int		len = chunk->len;
-	unsigned int		off = data->scratch_head;
+	unsigned int		off = data->scratch_head & (data->scratch_size - 1);
 
 	do {
 		unsigned int	this_off = off % XFS_GC_BUF_SIZE;
@@ -729,7 +724,7 @@ xfs_zone_gc_start_chunk(
 	bio->bi_iter.bi_sector = xfs_rtb_to_daddr(mp, chunk->old_startblock);
 	bio->bi_end_io = xfs_zone_gc_end_io;
 	xfs_zone_gc_add_data(chunk);
-	data->scratch_head = (data->scratch_head + len) % data->scratch_size;
+	data->scratch_head = data->scratch_head + len;
 
 	WRITE_ONCE(chunk->state, XFS_GC_BIO_NEW);
 	list_add_tail(&chunk->entry, &data->reading);
@@ -860,8 +855,7 @@ xfs_zone_gc_finish_chunk(
 		return;
 	}
 
-	data->scratch_tail =
-		(data->scratch_tail + chunk->len) % data->scratch_size;
+	data->scratch_tail = data->scratch_tail + chunk->len;
 
 	/*
 	 * Cycle through the iolock and wait for direct I/O and layouts to

--

  reply	other threads:[~2026-01-26 20:30 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-14 13:06 improve zoned XFS GC buffer management v4 Christoph Hellwig
2026-01-14 13:06 ` [PATCH 1/3] block: add a bio_reuse helper Christoph Hellwig
2026-01-14 13:06 ` [PATCH 2/3] xfs: use bio_reuse in the zone GC code Christoph Hellwig
2026-01-14 19:50   ` Darrick J. Wong
2026-01-14 13:06 ` [PATCH 3/3] xfs: rework zone GC buffer management Christoph Hellwig
2026-01-14 19:59   ` Darrick J. Wong
2026-01-15  6:12     ` Christoph Hellwig
2026-01-25  0:03   ` Chris Mason
2026-01-26 20:30     ` Keith Busch [this message]
2026-01-27  6:53       ` Christoph Hellwig
2026-01-21 12:21 ` improve zoned XFS GC buffer management v4 Carlos Maiolino
  -- strict thread matches above, loose matches on Subject: below --
2026-01-13  7:19 improve zoned XFS GC buffer management v3 Christoph Hellwig
2026-01-13  7:19 ` [PATCH 3/3] xfs: rework zone GC buffer management Christoph Hellwig
2026-01-06  7:58 improve zoned XFS GC buffer management v2 Christoph Hellwig
2026-01-06  7:58 ` [PATCH 3/3] xfs: rework zone GC buffer management Christoph Hellwig
2026-01-09 12:24   ` Carlos Maiolino
2025-12-18  6:31 improve zoned XFS " Christoph Hellwig
2025-12-18  6:31 ` [PATCH 3/3] xfs: rework zone " Christoph Hellwig
2025-12-18  7:12   ` Damien Le Moal
2025-12-19  8:06   ` Hans Holmberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aXfO_ghd0yoKK8dm@kbusch-mbp \
    --to=kbusch@kernel.org \
    --cc=axboe@kernel.dk \
    --cc=cem@kernel.org \
    --cc=clm@meta.com \
    --cc=cmaiolino@redhat.com \
    --cc=dlemoal@kernel.org \
    --cc=hans.holmberg@wdc.com \
    --cc=hch@lst.de \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-xfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.