All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] locking/atomic: Make test_and_*_bit() ordered on failure
@ 2022-08-16  7:03 ` Hector Martin
  0 siblings, 0 replies; 30+ messages in thread
From: Hector Martin @ 2022-08-16  7:03 UTC (permalink / raw)
  To: Will Deacon, Peter Zijlstra, Arnd Bergmann, Ingo Molnar
  Cc: Alan Stern, Andrea Parri, Boqun Feng, Nicholas Piggin,
	David Howells, Jade Alglave, Luc Maranget, Paul E. McKenney,
	Akira Yokosawa, Daniel Lustig, Joel Fernandes, Mark Rutland,
	Jonathan Corbet, Tejun Heo, jirislaby, Marc Zyngier,
	Catalin Marinas, Oliver Neukum, Linus Torvalds, linux-kernel,
	linux-arch, linux-doc, linux-arm-kernel, Asahi Linux,
	Hector Martin, stable

These operations are documented as always ordered in
include/asm-generic/bitops/instrumented-atomic.h, and producer-consumer
type use cases where one side needs to ensure a flag is left pending
after some shared data was updated rely on this ordering, even in the
failure case.

This is the case with the workqueue code, which currently suffers from a
reproducible ordering violation on Apple M1 platforms (which are
notoriously out-of-order) that ends up causing the TTY layer to fail to
deliver data to userspace properly under the right conditions. This
change fixes that bug.

Change the documentation to restrict the "no order on failure" story to
the _lock() variant (for which it makes sense), and remove the
early-exit from the generic implementation, which is what causes the
missing barrier semantics in that case. Without this, the remaining
atomic op is fully ordered (including on ARM64 LSE, as of recent
versions of the architecture spec).

Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: stable@vger.kernel.org
Fixes: e986a0d6cb36 ("locking/atomics, asm-generic/bitops/atomic.h: Rewrite using atomic_*() APIs")
Fixes: 61e02392d3c7 ("locking/atomic/bitops: Document and clarify ordering semantics for failed test_and_{}_bit()")
Signed-off-by: Hector Martin <marcan@marcan.st>
---
 Documentation/atomic_bitops.txt     | 2 +-
 include/asm-generic/bitops/atomic.h | 6 ------
 2 files changed, 1 insertion(+), 7 deletions(-)

diff --git a/Documentation/atomic_bitops.txt b/Documentation/atomic_bitops.txt
index 093cdaefdb37..d8b101c97031 100644
--- a/Documentation/atomic_bitops.txt
+++ b/Documentation/atomic_bitops.txt
@@ -59,7 +59,7 @@ Like with atomic_t, the rule of thumb is:
  - RMW operations that have a return value are fully ordered.
 
  - RMW operations that are conditional are unordered on FAILURE,
-   otherwise the above rules apply. In the case of test_and_{}_bit() operations,
+   otherwise the above rules apply. In the case of test_and_set_bit_lock(),
    if the bit in memory is unchanged by the operation then it is deemed to have
    failed.
 
diff --git a/include/asm-generic/bitops/atomic.h b/include/asm-generic/bitops/atomic.h
index 3096f086b5a3..71ab4ba9c25d 100644
--- a/include/asm-generic/bitops/atomic.h
+++ b/include/asm-generic/bitops/atomic.h
@@ -39,9 +39,6 @@ arch_test_and_set_bit(unsigned int nr, volatile unsigned long *p)
 	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 int nr, volatile unsigned long *p)
 	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);
 }
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2022-08-17  8:21 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-16  7:03 [PATCH] locking/atomic: Make test_and_*_bit() ordered on failure Hector Martin
2022-08-16  7:03 ` Hector Martin
2022-08-16  8:16 ` Arnd Bergmann
2022-08-16  8:16   ` Arnd Bergmann
2022-08-16 12:29   ` Jon Nettleton
2022-08-16 12:29     ` Jon Nettleton
2022-08-16 13:00     ` Will Deacon
2022-08-16 13:00       ` Will Deacon
2022-08-16 13:05       ` Jon Nettleton
2022-08-16 13:05         ` Jon Nettleton
2022-08-16 13:23         ` Marc Zyngier
2022-08-16 13:23           ` Marc Zyngier
2022-08-16 14:06   ` Will Deacon
2022-08-16 14:06     ` Will Deacon
2022-08-16 18:14     ` Matthew Wilcox
2022-08-16 18:14       ` Matthew Wilcox
2022-08-16 14:04 ` Will Deacon
2022-08-16 14:04   ` Will Deacon
2022-08-16 14:30   ` Hector Martin
2022-08-16 14:30     ` Hector Martin
2022-08-16 17:36     ` Will Deacon
2022-08-16 17:36       ` Will Deacon
2022-08-16 17:49       ` Jon Nettleton
2022-08-16 17:49         ` Jon Nettleton
2022-08-16 18:02         ` Linus Torvalds
2022-08-16 18:02           ` Linus Torvalds
2022-08-17  5:40           ` Jon Nettleton
2022-08-17  5:40             ` Jon Nettleton
2022-08-17  8:20 ` David Laight
2022-08-17  8:20   ` David Laight

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.