From: David Sterba <dsterba@suse.cz>
To: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
Cc: dsterba@suse.cz, Chris Mason <clm@fb.com>,
Josef Bacik <josef@toxicpanda.com>,
David Sterba <dsterba@suse.com>,
linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org,
kernel-team@fb.com
Subject: Re: [PATCH v4] btrfs: add fs state details to error messages.
Date: Thu, 24 Feb 2022 19:42:31 +0100 [thread overview]
Message-ID: <20220224184231.GZ12643@twin.jikos.cz> (raw)
In-Reply-To: <284ccc08-8de7-9188-19d8-20f4eda56cb4@dorminy.me>
On Thu, Feb 24, 2022 at 11:10:59AM -0500, Sweet Tea Dorminy wrote:
> Awesome, thank you. I realized this morning that it might be technically
> slightly racy actually and would propose something like the following
>
> static void btrfs_state_to_string(const struct btrfs_fs_info *info, char *buf)
> {
> unsigned int bit;
> + unsigned long fs_state = READ_ONCE(info->fs_state);
> unsigned int states_printed = 0;
> char *curr = buf;
>
> memcpy(curr, STATE_STRING_PREFACE, sizeof(STATE_STRING_PREFACE));
> curr += sizeof(STATE_STRING_PREFACE) - 1;
>
> - for_each_set_bit(bit, &info->fs_state, sizeof(info->fs_state)) {
> + for_each_set_bit(bit, fs_state, sizeof(fs_state)) {
>
>
> All the other interactions with info->fs_state are test/set/clear_bit,
> which treat the argument as volatile and are therefore safe to do from
> multiple threads. Without the READ_ONCE (reading it as a volatile),
> the compiler or cpu could turn the reads of info->fs_state in
> for_each_set_bit() into writes of random stuff into info->fs_state,
> potentially clearing the state bits or filling them with garbage.
I'm not sure I'm missing something, but I find the above hard to
believe. Concurrent access to a variable from multiple threads may not
produce consistent results, but random writes should not happen when
we're just reading. The worst thing that could happen is a missing
status bit reported, which is not a problem.
> Even if this is right, it'd be rare, but it would be exceeding weird
> for a message to be logged listing an error and then future messages
> be logged without any such state, or with a random collection of
> garbage states.
How would that happen? The volatile keyword is only a compiler hint not
to do optimizations on the variable, what actually happens on the CPU
level depends if the instruction is locked or not, so different threads
may read different bits.
You seem to imply that once a variable is not used with volatile
semantics, even just for read, the result could lead to random writes
because it's otherwise undefined.
next prev parent reply other threads:[~2022-02-24 18:46 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-23 19:38 Sweet Tea Dorminy
2022-02-24 13:22 ` David Sterba
2022-02-24 16:10 ` Sweet Tea Dorminy
2022-02-24 18:42 ` David Sterba [this message]
2022-02-24 20:09 ` Sweet Tea Dorminy
2022-02-24 20:17 ` Omar Sandoval
2022-02-28 16:48 ` 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=20220224184231.GZ12643@twin.jikos.cz \
--to=dsterba@suse.cz \
--cc=clm@fb.com \
--cc=dsterba@suse.com \
--cc=josef@toxicpanda.com \
--cc=kernel-team@fb.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=sweettea-kernel@dorminy.me \
/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
all inboxes | Powered by JetHome®