From: Hector Martin <marcan@marcan.st>
To: Linus Torvalds <torvalds@linux-foundation.org>,
Herbert Xu <herbert@gondor.apana.org.au>,
Will Deacon <will@kernel.org>
Cc: Tejun Heo <tj@kernel.org>,
peterz@infradead.org, jirislaby@kernel.org, maz@kernel.org,
mark.rutland@arm.com, boqun.feng@gmail.com,
catalin.marinas@arm.com, oneukum@suse.com,
roman.penyaev@profitbricks.com, asahi@lists.linux.dev,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH] workqueue: Fix memory ordering race in queue_work*()
Date: Tue, 16 Aug 2022 14:36:31 +0900 [thread overview]
Message-ID: <cd51b422-89f3-1856-5d3b-d6e5b0029085@marcan.st> (raw)
In-Reply-To: <CAHk-=wgSNiT5qJX53RHtWECsUiFq6d6VWYNAvu71ViOEan07yw@mail.gmail.com>
On 16/08/2022 14.27, Linus Torvalds wrote:
> On Mon, Aug 15, 2022 at 9:15 PM Herbert Xu <herbert@gondor.apana.org.au> wrote:
>>
>> Please revert this as test_and_set_bit was always supposed to be
>> a full memory barrier. This is an arch bug.
>
<snip>
> From looking at it, the asm-generic implementation is a bit
> questionable, though. In particular, it does
>
> if (READ_ONCE(*p) & mask)
> return 1;
>
> so it's *entirely* unordered for the "bit was already set" case.
These ops are documented in Documentation/atomic_bitops.txt as being
unordered in the failure ("bit was already set" case), and that matches
the generic implementation (which arm64 uses).
On the other hand, Twitter just pointed out that contradicting
documentation exists (I believe this was the source of the third party
doc I found that claimed it's always a barrier):
include/asm-generic/bitops/instrumented-atomic.h
So either one doc and the implementation are wrong, or the other doc is
wrong.
> --- a/include/asm-generic/bitops/atomic.h
> +++ b/include/asm-generic/bitops/atomic.h
> @@ -39,9 +39,6 @@ arch_test_and_set_bit(
> unsigned long mask = BIT_MASK(nr);
>
> p += BIT_WORD(nr);
> - if (READ_ONCE(*p) & mask)
> - return 1;
> -
> old = arch_atomic_long_fetch_or(mask, (atomic_long_t *)p);
> return !!(old & mask);
> }
> @@ -53,9 +50,6 @@ arch_test_and_clear_bit
> unsigned long mask = BIT_MASK(nr);
>
> p += BIT_WORD(nr);
> - if (!(READ_ONCE(*p) & mask))
> - return 0;
> -
> old = arch_atomic_long_fetch_andnot(mask, (atomic_long_t *)p);
> return !!(old & mask);
> }
>
> but the above is not just whitespace-damaged, it's entirely untested
> and based purely on me looking at that code.
That does work, I in fact tried that fix first to prove that the
early-exit was the problem.
I don't have a particularly strong opinion on which way is right here, I
just saw the implementation matched the docs (that I found) and Will
commented on the other thread implying this was all deliberate.
- Hector
next prev parent reply other threads:[~2022-08-16 8:11 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-15 17:58 Hector Martin
2022-08-15 19:10 ` Tejun Heo
2022-08-16 4:15 ` Herbert Xu
2022-08-16 5:27 ` Linus Torvalds
2022-08-16 5:36 ` Hector Martin [this message]
2022-08-16 5:52 ` Linus Torvalds
2022-08-16 6:28 ` Hector Martin
2022-08-16 7:48 ` Herbert Xu
2022-08-16 8:01 ` Hector Martin
2022-08-16 5:48 ` Herbert Xu
2022-08-16 6:03 ` Linus Torvalds
2022-08-16 13:41 ` Will Deacon
2022-08-16 14:55 ` Boqun Feng
2022-08-16 16:22 ` Hector Martin
2022-08-16 16:43 ` Boqun Feng
2022-08-16 16:58 ` Linus Torvalds
2022-08-16 16:41 ` Linus Torvalds
2022-08-17 5:05 ` Herbert Xu
2022-08-16 17:01 ` Tejun Heo
2022-08-16 16:26 ` Tejun Heo
2022-08-16 17:21 ` Hector Martin
2022-08-16 4:14 ` Herbert Xu
2022-08-16 5:37 ` Hector Martin
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=cd51b422-89f3-1856-5d3b-d6e5b0029085@marcan.st \
--to=marcan@marcan.st \
--cc=asahi@lists.linux.dev \
--cc=boqun.feng@gmail.com \
--cc=catalin.marinas@arm.com \
--cc=herbert@gondor.apana.org.au \
--cc=jirislaby@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=oneukum@suse.com \
--cc=peterz@infradead.org \
--cc=roman.penyaev@profitbricks.com \
--cc=stable@vger.kernel.org \
--cc=tj@kernel.org \
--cc=torvalds@linux-foundation.org \
--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
all inboxes | Powered by JetHome®