public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Josef Bacik <josef@toxicpanda.com>
To: Filipe Manana <fdmanana@kernel.org>
Cc: Naohiro Aota <naohiro.aota@wdc.com>, linux-btrfs@vger.kernel.org
Subject: Re: [PATCH] btrfs: zoned: properly take lock to read/update BG's zoned variables
Date: Mon, 29 Jul 2024 10:41:50 -0400	[thread overview]
Message-ID: <20240729144150.GA3589837@perftesting> (raw)
In-Reply-To: <CAL3q7H6AZOGvYbf=BmEVEE_qWZYk86Li1s=jrfyOoUKAHhtDdw@mail.gmail.com>

On Mon, Jul 29, 2024 at 12:46:48PM +0100, Filipe Manana wrote:
> On Mon, Jul 29, 2024 at 9:33 AM Naohiro Aota <naohiro.aota@wdc.com> wrote:
> >
> > __btrfs_add_free_space_zoned() references and modifies BG's alloc_offset,
> > ro, and zone_unusable, but without taking the lock. It is mostly safe
> > because they monotonically increase (at least for now) and this function is
> > mostly called by a transaction commit, which is serialized by itself.
> >
> > Still, taking the lock is a safer and correct option and I'm going to add a
> > change to reset zone_unusable while a block group is still alive. So, add
> > locking around the operations.
> >
> > Fixes: 169e0da91a21 ("btrfs: zoned: track unusable bytes for zones")
> > CC: stable@vger.kernel.org # 5.15+
> > Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
> > ---
> >  fs/btrfs/free-space-cache.c | 15 ++++++++-------
> >  1 file changed, 8 insertions(+), 7 deletions(-)
> >
> > diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
> > index f5996a43db24..51263d6dac36 100644
> > --- a/fs/btrfs/free-space-cache.c
> > +++ b/fs/btrfs/free-space-cache.c
> > @@ -2697,15 +2697,16 @@ static int __btrfs_add_free_space_zoned(struct btrfs_block_group *block_group,
> >         u64 offset = bytenr - block_group->start;
> >         u64 to_free, to_unusable;
> >         int bg_reclaim_threshold = 0;
> > -       bool initial = ((size == block_group->length) && (block_group->alloc_offset == 0));
> > +       bool initial;
> >         u64 reclaimable_unusable;
> >
> > -       WARN_ON(!initial && offset + size > block_group->zone_capacity);
> > +       guard(spinlock)(&block_group->lock);
> 
> What's this guard thing and why do we use it only here? We don't use
> it anywhere else in btrfs' code base.
> A quick search in the Documentation directory of the kernel and I
> can't find anything there.
> In the fs/ directory there's only two users of it.
> 

It's relatively new, it's like the C++ guards.  If you look in the VFS we've
started using it pretty heavily there.

But it does need to be documented, if you look at include/linux/cleanup.h it has
documentation about it.

> Why not the usual spin_lock(&block_group->lock) call?

Because this is nice for error handling.  Here it doesn't look as helpful, but
look at d842379313a2 ("fs: use guard for namespace_sem in statmount()") for an
example of how much it cleans up the error paths.

FWIW one of the tasks I have for one of our new people is to come through and
utilize some of this new infrastructure to cleanup our error paths, so while it
doesn't exist yet inside btrfs, I hope it gets used pretty liberally.  Thanks,

Josef

  reply	other threads:[~2024-07-29 14:41 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-29  8:32 [PATCH] btrfs: zoned: properly take lock to read/update BG's zoned variables Naohiro Aota
2024-07-29 11:46 ` Filipe Manana
2024-07-29 14:41   ` Josef Bacik [this message]
2024-07-29 14:48     ` Filipe Manana
2024-07-29 14:58       ` Josef Bacik
2024-07-31  3:03         ` Naohiro Aota
2024-07-31 16:07     ` David Sterba

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=20240729144150.GA3589837@perftesting \
    --to=josef@toxicpanda.com \
    --cc=fdmanana@kernel.org \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=naohiro.aota@wdc.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox