From: Qu Wenruo <wqu@suse.com>
To: fdmanana@kernel.org, linux-btrfs@vger.kernel.org
Subject: Re: [PATCH] btrfs: annotate block group access with data_race() when sorting for reclaim
Date: Thu, 11 Sep 2025 20:42:12 +0930 [thread overview]
Message-ID: <4e254b82-b4de-4b16-a469-9c58c805938f@suse.com> (raw)
In-Reply-To: <456b17e9620d5118fcca2674b365e0770b1d1fc1.1757332833.git.fdmanana@suse.com>
在 2025/9/8 21:35, fdmanana@kernel.org 写道:
> From: Filipe Manana <fdmanana@suse.com>
>
> When sorting the block group list for reclaim we are using a block group's
> used bytes counter without taking the block group's spinlock, so we can
> race with a concurrent task updating it (at btrfs_update_block_group()),
> which makes tools like KCSAN unhappy and report a race.
>
> Since the sorting is not strictly needed from a functional perspective
> and such races should rarely cause any ordering changes (only load/store
> tearing could cause them), not to mention that after the sorting the
> ordering may no longer be accurate due to concurrent allocations and
> deallocations of extents in a block group, annotate the accesses to the
> used counter with data_race() to silence KCSAN and similar tools.
>
> Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Thanks,
Qu
> ---
> fs/btrfs/block-group.c | 9 ++++++++-
> 1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
> index 239cbb01f83f..548483a84466 100644
> --- a/fs/btrfs/block-group.c
> +++ b/fs/btrfs/block-group.c
> @@ -1795,7 +1795,14 @@ static int reclaim_bgs_cmp(void *unused, const struct list_head *a,
> bg1 = list_entry(a, struct btrfs_block_group, bg_list);
> bg2 = list_entry(b, struct btrfs_block_group, bg_list);
>
> - return bg1->used > bg2->used;
> + /*
> + * Some other task may be updating the ->used field concurrently, but it
> + * is not serious if we get a stale value or load/store tearing issues,
> + * as sorting the list of block groups to reclaim is not critical and an
> + * occasional imperfect order is ok. So silence KCSAN and avoid the
> + * overhead of locking or any other synchronization.
> + */
> + return data_race(bg1->used > bg2->used);
> }
>
> static inline bool btrfs_should_reclaim(const struct btrfs_fs_info *fs_info)
next prev parent reply other threads:[~2025-09-11 11:12 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-08 12:05 [PATCH] btrfs: annotate block group access with data_race() when sorting for reclaim fdmanana
2025-09-11 11:12 ` Qu Wenruo [this message]
2025-09-11 11:12 ` Johannes Thumshirn
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=4e254b82-b4de-4b16-a469-9c58c805938f@suse.com \
--to=wqu@suse.com \
--cc=fdmanana@kernel.org \
--cc=linux-btrfs@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox