From: David Sterba <dsterba@suse.cz>
To: Filipe Manana <fdmanana@kernel.org>
Cc: haoran zheng <zhenghaoran154@gmail.com>,
Daniel Vacek <neelx@suse.com>,
clm@fb.com, josef@toxicpanda.com, dsterba@suse.com,
linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org,
baijiaju1990@gmail.com, 21371365@buaa.edu.cn
Subject: Re: [PATCH] btrfs: fix data race when accessing the block_group's used field
Date: Wed, 26 Feb 2025 16:00:41 +0100 [thread overview]
Message-ID: <20250226150041.GV5777@suse.cz> (raw)
In-Reply-To: <Z72s4bDZghGhcWzN@debian0.Home>
On Tue, Feb 25, 2025 at 11:43:29AM +0000, Filipe Manana wrote:
> > > > > > > static inline u64 btrfs_block_group_used(struct btrfs_block_group *bg)
> > > > > > > {
> > > > > > > u64 ret;
> > > > > > >
> > > > > > > spin_lock(&bg->lock);
> > > > > > > ret = bg->used;
> > > > > > > spin_unlock(&bg->lock);
> > > > > > >
> > > > > > > return ret;
> > > > > > > }
> >
> > I understand that using lock to protect block_group->used
> > in discard.c file is feasible. In addition, I looked at the code
> > of block-group.c and found that locks have been added in
> > some places where block_group->used are used. , it
> > seems that it is not appropriate to call
> > btrfs_block_group_used again to obtain (because it will
> > cause deadlock).
>
> In places where we are reading it while holding the block group's
> spinlock, there's nothing that needs to be changed.
>
> Also we can't call it btrfs_block_group_used() since there's already
> an accessor function for struct btrfs_block_group_item with that name
> (defined through macros at accessors.h).
>
> I took a closer look at the cases in discard.c, and it's safe to use
> data_race() instead, even if load/store tearing happens or we get stale
> values, nothing harmful happens, only a few things can be done later or
> unnecessarily without side effects - like adding a non-empty block group
> to the list of unused block groups, which is fine since the we won't
> delete it the cleaner kthread in case it's not empty, or delay the discard.
>
> Either give it another name like btrfs_get_block_group_used() or directly
> use data_race() in discard.c - I don't like much either of them, the first
> because there's the similar named accessor for block group items, the
> second due to spreading data_race(), but I don't see any more elegant
> alternative.
Agreed, this looks like a resonable "solution". It's not really a bug,
we can live with data_race annotation, namely when it filters out known
cases so the code analysis tools may find the real problems.
> So a sample patch:
[...]
Looks good to me.
prev parent reply other threads:[~2025-02-26 15:00 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-08 7:38 [PATCH] btrfs: fix data race when accessing the block_group's used field Hao-ran Zheng
2025-02-10 11:09 ` Filipe Manana
2025-02-18 8:08 ` Daniel Vacek
2025-02-18 10:18 ` Filipe Manana
2025-02-18 16:13 ` Daniel Vacek
2025-02-18 16:28 ` Filipe Manana
2025-02-19 7:47 ` Daniel Vacek
2025-02-19 8:52 ` haoran zheng
2025-02-25 11:43 ` Filipe Manana
2025-02-26 15:00 ` David Sterba [this message]
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=20250226150041.GV5777@suse.cz \
--to=dsterba@suse.cz \
--cc=21371365@buaa.edu.cn \
--cc=baijiaju1990@gmail.com \
--cc=clm@fb.com \
--cc=dsterba@suse.com \
--cc=fdmanana@kernel.org \
--cc=josef@toxicpanda.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=neelx@suse.com \
--cc=zhenghaoran154@gmail.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