From: Peter Zijlstra <peterz@infradead.org>
To: Marco Elver <elver@google.com>
Cc: Qian Cai <cai@lca.pw>, Ingo Molnar <mingo@redhat.com>,
Will Deacon <will@kernel.org>,
LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH -next] locking/osq_lock: annotate a data race in osq_lock
Date: Tue, 11 Feb 2020 13:47:53 +0100 [thread overview]
Message-ID: <20200211124753.GP14914@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <CANpmjNPWCu+w3O8cg++X4=viVFsWNehTXzTuqbwV8-DcXXpFng@mail.gmail.com>
On Tue, Feb 11, 2020 at 11:16:05AM +0100, Marco Elver wrote:
> On Tue, 11 Feb 2020 at 05:07, Qian Cai <cai@lca.pw> wrote:
> >
> > prev->next could be accessed concurrently as noticed by KCSAN,
> >
> > write (marked) to 0xffff9d3370dbbe40 of 8 bytes by task 3294 on cpu 107:
> > osq_lock+0x25f/0x350
> > osq_wait_next at kernel/locking/osq_lock.c:79
> > (inlined by) osq_lock at kernel/locking/osq_lock.c:185
> > rwsem_optimistic_spin
> > <snip>
> >
> > read to 0xffff9d3370dbbe40 of 8 bytes by task 3398 on cpu 100:
> > osq_lock+0x196/0x350
> > osq_lock at kernel/locking/osq_lock.c:157
> > rwsem_optimistic_spin
> > <snip>
> >
> > Since the write only stores NULL to prev->next and the read tests if
> > prev->next equals to this_cpu_ptr(&osq_node). Even if the value is
> > shattered, the code is still working correctly. Thus, mark it as an
> > intentional data race using the data_race() macro.
>
> I have said this before: we're not just guarding against load/store
> tearing, although on their own, they make it deceptively easy to
> reason about data races.
>
> The case here seems to be another instance of a C-CAS, to avoid
> unnecessarily dirtying a cacheline.
>
> Here, the loop would make me suspicious, because a compiler could
> optimize out re-loading the value. Due to the smp_load_acquire,
> however, at the least we have 1 implied compiler barrier in this loop
> which means that will likely not happen.
The loop has cpu_relax() (as any spin loop should have), that implies a
compiler barrier() and should disallow the compiler from being funny.
That said; I feel it would be very good to mandate a comment with every
use of data_race(), just like we mandate a comment with memory barriers.
This comment can then explain why the data_race() annotation is correct.
prev parent reply other threads:[~2020-02-11 12:48 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-11 4:06 [PATCH -next] locking/osq_lock: annotate a data race in osq_lock Qian Cai
2020-02-11 10:16 ` Marco Elver
2020-02-11 11:57 ` Qian Cai
2020-02-11 12:47 ` Peter Zijlstra [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=20200211124753.GP14914@hirez.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=cai@lca.pw \
--cc=elver@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=will@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;
as well as URLs for NNTP newsgroup(s).