From: Marco Elver <elver@google.com>
To: Josef Bacik <josef@toxicpanda.com>
Cc: Jan Kara <jack@suse.cz>,
linux-fsdevel@vger.kernel.org,
Amir Goldstein <amir73il@gmail.com>,
Dmitry Vyukov <dvyukov@google.com>,
syzbot+701037856c25b143f1ad@syzkaller.appspotmail.com
Subject: Re: [PATCH v2] fsnotify: Avoid data race between fsnotify_recalc_mask() and fsnotify_object_watched()
Date: Mon, 15 Jul 2024 16:48:31 +0200 [thread overview]
Message-ID: <ZpU2v28viO_I_1zd@elver.google.com> (raw)
In-Reply-To: <20240715142203.GA1649877@perftesting>
On Mon, Jul 15, 2024 at 10:22AM -0400, Josef Bacik wrote:
> On Mon, Jul 15, 2024 at 03:04:10PM +0200, Jan Kara wrote:
> > When __fsnotify_recalc_mask() recomputes the mask on the watched object,
> > the compiler can "optimize" the code to perform partial updates to the
> > mask (including zeroing it at the beginning). Thus places checking
> > the object mask without conn->lock such as fsnotify_object_watched()
> > could see invalid states of the mask. Make sure the mask update is
> > performed by one memory store using WRITE_ONCE().
> >
> > Reported-by: syzbot+701037856c25b143f1ad@syzkaller.appspotmail.com
> > Reported-by: Dmitry Vyukov <dvyukov@google.com>
> > Link: https://lore.kernel.org/all/CACT4Y+Zk0ohwwwHSD63U2-PQ=UuamXczr1mKBD6xtj2dyYKBvA@mail.gmail.com
> > Signed-off-by: Jan Kara <jack@suse.cz>
>
> I'm still hazy on the rules here and what KCSAN expects, but if we're using
> READ_ONCE/WRITE_ONCE on a thing, do we have to use them everywhere we access
> that member? Because there's a few accesses in include/linux/fsnotify_backend.h
> that were missed if so. Thanks,
Only if they can be concurrently read + written. Per Linux's memory
model, plain concurrent accesses, where at least one is a write, are
data races. Data races are hard to reason about, because the compiler is
free to shuffle unmarked accesses around as it pleases, along with other
transformations that break atomicity and any ordering assumptions. (Data
races can also be a symptom of missing locking or other missed
serialization, but that does not seem to the case here.)
This is a reasonable overview:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/memory-model/Documentation/access-marking.txt
I don't know the code well enough here, but the above commit message
also mentions a "conn->lock". E.g. if all accesses under the lock are
serialized, they won't need to be marked with ONCE. But the lock won't
help if e.g. there are accesses within a lock critical section, but also
accesses outside the critical section, in which case both sides need to
be marked again (even the ones under the lock).
A good concurrent stress test + KCSAN is pretty good about quickly
telling you about data races.
next prev parent reply other threads:[~2024-07-15 14:48 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-15 13:04 [PATCH v2] fsnotify: Avoid data race between fsnotify_recalc_mask() and fsnotify_object_watched() Jan Kara
2024-07-15 14:22 ` Josef Bacik
2024-07-15 14:48 ` Marco Elver [this message]
2024-07-17 13:16 ` Jan Kara
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=ZpU2v28viO_I_1zd@elver.google.com \
--to=elver@google.com \
--cc=amir73il@gmail.com \
--cc=dvyukov@google.com \
--cc=jack@suse.cz \
--cc=josef@toxicpanda.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=syzbot+701037856c25b143f1ad@syzkaller.appspotmail.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;
as well as URLs for NNTP newsgroup(s).