Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Hector Martin <marcan@marcan.st>
Cc: Will Deacon <will@kernel.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	Tejun Heo <tj@kernel.org>,
	peterz@infradead.org, jirislaby@kernel.org, maz@kernel.org,
	mark.rutland@arm.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 09:43:29 -0700	[thread overview]
Message-ID: <YvvJMSGLtIZgP/Qd@boqun-archlinux> (raw)
In-Reply-To: <74559da4-5cd4-7cc4-0303-ab5f6a8b92ae@marcan.st>

On Wed, Aug 17, 2022 at 01:22:09AM +0900, Hector Martin wrote:
> On 16/08/2022 23.55, Boqun Feng wrote:
> > On Tue, Aug 16, 2022 at 02:41:57PM +0100, Will Deacon wrote:
> >> It's worth noting that with the spinlock-based implementation (i.e.
> >> prior to e986a0d6cb36) then we would have the same problem on
> >> architectures that implement spinlocks with acquire/release semantics;
> >> accesses from outside of the critical section can drift in and reorder
> >> with each other there, so the conversion looked legitimate to me in
> >> isolation and I vaguely remember going through callers looking for
> >> potential issues. Alas, I obviously missed this case.
> >>
> > 
> > I just to want to mention that although spinlock-based atomic bitops
> > don't provide the full barrier in test_and_set_bit(), but they don't
> > have the problem spotted by Hector, because test_and_set_bit() and
> > clear_bit() sync with each other via locks:
> > 
> > 	test_and_set_bit():
> > 	  lock(..);
> > 	  old = *p; // mask is already set by other test_and_set_bit()
> > 	  *p = old | mask;
> > 	  unlock(...);
> > 				clear_bit():
> > 				  lock(..);
> > 				  *p ~= mask;
> > 				  unlock(..);
> > 
> > so "having a full barrier before test_and_set_bit()" may not be the
> > exact thing we need here, as long as a test_and_set_bit() can sync with
> > a clear_bit() uncondiontally, then the world is safe. For example, we
> > can make test_and_set_bit() RELEASE, and clear_bit() ACQUIRE on ARM64:
> > 
> > 	test_and_set_bit():
> > 	  atomic_long_fetch_or_release(..); // pair with clear_bit()
> > 	  				    // guarantee everything is
> > 					    // observed.
> > 	  			clear_bit():
> > 				  atomic_long_fetch_andnot_acquire(..);
> > 	  
> > , maybe that's somewhat cheaper than a full barrier implementation.
> > 
> > Thoughts? Just to find the exact ordering requirement for bitops.
> 
> It's worth pointing out that the workqueue code does *not* pair
> test_and_set_bit() with clear_bit(). It does an atomic_long_set()
> instead (and then there are explicit barriers around it, which are
> expected to pair with the implicit barrier in test_and_set_bit()). If we
> define test_and_set_bit() to only sync with clear_bit() and not
> necessarily be a true barrier, that breaks the usage of the workqueue code.
> 

Ah, I miss that, but that means the old spinlock-based atomics are
totally broken unless spinlock means full barriers on these archs.

But still, if we define test_and_set_bit() as RELEASE atomic instead of 
a full barrier + atomic, it should work for workqueue, right? Do we
actually need extra ordering here?

	WRITE_ONCE(*x, 1); // A
	test_and_set_bit(..); // a full barrier will order A & B
	WRITE_ONCE(*y, 1); // B

That's something I want to figure out.

Regards,
Boqun
> - Hector

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  reply	other threads:[~2022-08-16 16:45 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-15 17:58 [PATCH] workqueue: Fix memory ordering race in queue_work*() 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
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 [this message]
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=YvvJMSGLtIZgP/Qd@boqun-archlinux \
    --to=boqun.feng@gmail.com \
    --cc=asahi@lists.linux.dev \
    --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=marcan@marcan.st \
    --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