From: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
To: 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 15:09:08 -0500 [thread overview]
Message-ID: <20f14d85-6a07-e66d-4711-c16c6930c2a3@dorminy.me> (raw)
In-Reply-To: <20220224184231.GZ12643@twin.jikos.cz>
>> 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.
Maybe I've been reading too many articles about the things compilers are
technically allowed to do. But as per the following link, the C standard
does permit compilers inventing writes except to atomics and volatiles:
https://lwn.net/Articles/793253/#Invented%20Stores
>
>> 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.
Pretty much; once a variable is read without READ_ONCE, it's unsafe to
write a new value on another thread that depends on the old value.
Imagine a compiler which invents stores; then if you are both reading
and setting a variable 'a' on different threads, the following could happen:
thread 1 (reads) thread 2 (modifies)
reads a into tmp
stores junk into a
reads junk from a
stores tmp into a
writes junk | 2 to a
Now a contains junk indefinitely.
But if it's too theoretical, I'm happy to drop it and amend my paranoia
level.
(Thanks for fixing the !CONFIG_PRINTK warning that btrfs_state_to_string
was unused; sorry I missed it.)
Sweet Tea
next prev parent reply other threads:[~2022-02-24 20:09 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
2022-02-24 20:09 ` Sweet Tea Dorminy [this message]
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=20f14d85-6a07-e66d-4711-c16c6930c2a3@dorminy.me \
--to=sweettea-kernel@dorminy.me \
--cc=clm@fb.com \
--cc=dsterba@suse.com \
--cc=dsterba@suse.cz \
--cc=josef@toxicpanda.com \
--cc=kernel-team@fb.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@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
all inboxes | Powered by JetHome®