Skip to contentSkip to conversation

locking/atomic: Implement atomic{,64,_long}_{fetch_,}{andnot_or}{,_relaxed,_acquire,_release}()

RFC PATCH In review Rui Wang 5 participants 8 messages Jul 29, 2021 – Jul 30, 2021

Subsystems (3)
lore
Share & tools
lore

Rui Wang

author
#0 lore raw

RFC PATCH v3 1/1 locking/atomic: Implement atomic{,64,_long}_{fetch_,}{andnot_or}{,_relaxed,_acquire,_release}()

This patch introduce a new atomic primitive andnot_or:

* atomic_andnot_or
* atomic_fetch_andnot_or
* atomic_fetch_andnot_or_relaxed
* atomic_fetch_andnot_or_acquire
* atomic_fetch_andnot_or_release
* atomic64_andnot_or
* atomic64_fetch_andnot_or
* atomic64_fetch_andnot_or_relaxed
* atomic64_fetch_andnot_or_acquire
* atomic64_fetch_andnot_or_release
* atomic_long_andnot_or
* atomic_long_fetch_andnot_or
* atomic_long_fetch_andnot_or_relaxed
* atomic_long_fetch_andnot_or_acquire
* atomic_long_fetch_andnot_or_release

---
 include/asm-generic/atomic-instrumented.h |  72 +++++-
 include/asm-generic/atomic-long.h         |  62 ++++-
 include/linux/atomic-arch-fallback.h      | 262 +++++++++++++++++++++-
 lib/atomic64_test.c                       |  92 ++++----
 scripts/atomic/atomics.tbl                |   1 +
 scripts/atomic/fallbacks/andnot_or        |  25 +++
 6 files changed, 471 insertions(+), 43 deletions(-)
 create mode 100755 scripts/atomic/fallbacks/andnot_or
include/asm-generic/atomic-instrumented.h +71 -1
index bc45af52c93b..8f5efade88b7 100644
@@ -599,6 +599,41 @@ atomic_dec_if_positive(atomic_t *v)
 	return arch_atomic_dec_if_positive(v);
 }
 
+static __always_inline void
+atomic_andnot_or(int m, int o, atomic_t *v)
+{
+	instrument_atomic_read_write(v, sizeof(*v));
+	arch_atomic_andnot_or(m, o, v);
+}
+
+static __always_inline int
+atomic_fetch_andnot_or(int m, int o, atomic_t *v)
+{
+	instrument_atomic_read_write(v, sizeof(*v));
+	return arch_atomic_fetch_andnot_or(m, o, v);
+}
+
+static __always_inline int
+atomic_fetch_andnot_or_acquire(int m, int o, atomic_t *v)
+{
+	instrument_atomic_read_write(v, sizeof(*v));
+	return arch_atomic_fetch_andnot_or_acquire(m, o, v);
+}
+
+static __always_inline int
+atomic_fetch_andnot_or_release(int m, int o, atomic_t *v)
+{
+	instrument_atomic_read_write(v, sizeof(*v));
+	return arch_atomic_fetch_andnot_or_release(m, o, v);
+}
+
+static __always_inline int
+atomic_fetch_andnot_or_relaxed(int m, int o, atomic_t *v)
+{
+	instrument_atomic_read_write(v, sizeof(*v));
+	return arch_atomic_fetch_andnot_or_relaxed(m, o, v);
+}
+
 static __always_inline s64
 atomic64_read(const atomic64_t *v)
 {
@@ -1177,6 +1212,41 @@ atomic64_dec_if_positive(atomic64_t *v)
 	return arch_atomic64_dec_if_positive(v);
 }
 
+static __always_inline void
+atomic64_andnot_or(s64 m, s64 o, atomic64_t *v)
+{
+	instrument_atomic_read_write(v, sizeof(*v));
+	arch_atomic64_andnot_or(m, o, v);
+}
+
+static __always_inline s64
+atomic64_fetch_andnot_or(s64 m, s64 o, atomic64_t *v)
+{
+	instrument_atomic_read_write(v, sizeof(*v));
+	return arch_atomic64_fetch_andnot_or(m, o, v);
+}
+
+static __always_inline s64
+atomic64_fetch_andnot_or_acquire(s64 m, s64 o, atomic64_t *v)
+{
+	instrument_atomic_read_write(v, sizeof(*v));
+	return arch_atomic64_fetch_andnot_or_acquire(m, o, v);
+}
+
+static __always_inline s64
+atomic64_fetch_andnot_or_release(s64 m, s64 o, atomic64_t *v)
+{
+	instrument_atomic_read_write(v, sizeof(*v));
+	return arch_atomic64_fetch_andnot_or_release(m, o, v);
+}
+
+static __always_inline s64
+atomic64_fetch_andnot_or_relaxed(s64 m, s64 o, atomic64_t *v)
+{
+	instrument_atomic_read_write(v, sizeof(*v));
+	return arch_atomic64_fetch_andnot_or_relaxed(m, o, v);
+}
+
 #define xchg(ptr, ...) \
 ({ \
 	typeof(ptr) __ai_ptr = (ptr); \
@@ -1334,4 +1404,4 @@ atomic64_dec_if_positive(atomic64_t *v)
 })
 
 #endif /* _ASM_GENERIC_ATOMIC_INSTRUMENTED_H */
-// 1d7c3a25aca5c7fb031c307be4c3d24c7b48fcd5
+// 9c9792d0dcd1fb3de8eeda1225ebbd0d811fb941
include/asm-generic/atomic-long.h +61 -1

73 lines not on this page · raw message

include/linux/atomic-arch-fallback.h +261 -1

272 lines not on this page · raw message

lib/atomic64_test.c +52 -40

155 lines not on this page · raw message

scripts/atomic/atomics.tbl +1 -0
index fbee2f6190d9..db6fe1dfcdb4 100755
@@ -39,3 +39,4 @@ inc_not_zero		b	v
 inc_unless_negative	b	v
 dec_unless_positive	b	v
 dec_if_positive		i	v
+andnot_or		vF	i:m	i:o	v
scripts/atomic/fallbacks/andnot_or new file +25 -0
new file mode 100755
index 000000000000..a835045208de
@@ -0,0 +1,25 @@
+local try_order=${order}
+
+#
+# non-value returning atomics are implicity relaxed
+#
+if [ -z "${retstmt}" ]; then
+	try_order="_relaxed"
+	retstmt="(void)"
+fi
+
+cat <<EOF
+static __always_inline ${ret}
+${arch}${atomic}_${pfx}andnot_or${sfx}${order}(${int} m, ${int} o, ${atomic}_t *v)
+{
+	${retstmt}({
+		${int} N, O = ${arch}${atomic}_read(v);
+		do {
+			N = O;
+			N &= ~m;
+			N |= o;
+		} while (!${arch}${atomic}_try_cmpxchg${try_order}(v, &O, N));
+		O;
+	});
+}
+EOF
Signature · 1 line
2.32.0
maintainer
#1 lore raw
Quoted 20 lines · Rui Wang

This patch introduce a new atomic primitive andnot_or:

* atomic_andnot_or
* atomic_fetch_andnot_or
* atomic_fetch_andnot_or_relaxed
* atomic_fetch_andnot_or_acquire
* atomic_fetch_andnot_or_release
* atomic64_andnot_or
* atomic64_fetch_andnot_or
* atomic64_fetch_andnot_or_relaxed
* atomic64_fetch_andnot_or_acquire
* atomic64_fetch_andnot_or_release
* atomic_long_andnot_or
* atomic_long_fetch_andnot_or
* atomic_long_fetch_andnot_or_relaxed
* atomic_long_fetch_andnot_or_acquire
* atomic_long_fetch_andnot_or_release

Quoted 9 lines
---
 include/asm-generic/atomic-instrumented.h |  72 +++++-
 include/asm-generic/atomic-long.h         |  62 ++++-
 include/linux/atomic-arch-fallback.h      | 262 +++++++++++++++++++++-
 lib/atomic64_test.c                       |  92 ++++----
 scripts/atomic/atomics.tbl                |   1 +
 scripts/atomic/fallbacks/andnot_or        |  25 +++
 6 files changed, 471 insertions(+), 43 deletions(-)
 create mode 100755 scripts/atomic/fallbacks/andnot_or

Please see my other comments on the other patches you posted:

https://lore.kernel.org/r/20210729093923.GD21151@willie-the-truck

Overall, I'm not thrilled to bits by extending the atomics API with operations that cannot be implemented efficiently on any (?) architectures and are only used by the qspinlock slowpath on machines with more than 16K CPUs.

I also think we're lacking documentation justifying when you would use this new primitive over e.g. a sub-word WRITE_ONCE() on architectures that support those, especially for the non-returning variants.

Will

#2 lore raw

Hi, Will,

Quoted 41 lines · Will Deacon
Quoted 20 lines · Rui Wang

This patch introduce a new atomic primitive andnot_or:

* atomic_andnot_or
* atomic_fetch_andnot_or
* atomic_fetch_andnot_or_relaxed
* atomic_fetch_andnot_or_acquire
* atomic_fetch_andnot_or_release
* atomic64_andnot_or
* atomic64_fetch_andnot_or
* atomic64_fetch_andnot_or_relaxed
* atomic64_fetch_andnot_or_acquire
* atomic64_fetch_andnot_or_release
* atomic_long_andnot_or
* atomic_long_fetch_andnot_or
* atomic_long_fetch_andnot_or_relaxed
* atomic_long_fetch_andnot_or_acquire
* atomic_long_fetch_andnot_or_release

Quoted 9 lines
---
 include/asm-generic/atomic-instrumented.h |  72 +++++-
 include/asm-generic/atomic-long.h         |  62 ++++-
 include/linux/atomic-arch-fallback.h      | 262 +++++++++++++++++++++-
 lib/atomic64_test.c                       |  92 ++++----
 scripts/atomic/atomics.tbl                |   1 +
 scripts/atomic/fallbacks/andnot_or        |  25 +++
 6 files changed, 471 insertions(+), 43 deletions(-)
 create mode 100755 scripts/atomic/fallbacks/andnot_or

Please see my other comments on the other patches you posted:

https://lore.kernel.org/r/20210729093923.GD21151@willie-the-truck

Overall, I'm not thrilled to bits by extending the atomics API with operations that cannot be implemented efficiently on any (?) architectures and are only used by the qspinlock slowpath on machines with more than 16K CPUs.

Quoted 5 lines

I also think we're lacking documentation justifying when you would use this new primitive over e.g. a sub-word WRITE_ONCE() on architectures that support those, especially for the non-returning variants.

Will

I have tried to explain in another thread. At the beginning, I thought about implementing xchg_mask for the sub-word xchg, but now I agree that atomic andnot_or is clearer and more general.

Peter, what do you think?

Regards,
Rui

maintainer
#3 lore raw
In reply to Will Deacon
Quoted 4 lines · Will Deacon

Overall, I'm not thrilled to bits by extending the atomics API with operations that cannot be implemented efficiently on any (?) architectures and are only used by the qspinlock slowpath on machines with more than 16K CPUs.

My rationale for proposing this primitive is similar to the existence of other composite atomic ops from the Misc (and refcount) class (as per atomic_t.txt). They're common/performance sensitive operations that, on LL/SC platforms, can be better implemented than a cmpxchg() loop.

Specifically here, it can be used to implement short xchg() in an architecturally neutral way, but more importantly it provides fwd progress on LL/SC, while most LL/SC based cmpxchg() implementations are arguably broken there.

People seem to really struggle to implement that sanely.

It's such a shame we can't have the compiler generate sane composite atomics for us..

Quoted 3 lines

I also think we're lacking documentation justifying when you would use this new primitive over e.g. a sub-word WRITE_ONCE() on architectures that support those, especially for the non-returning variants.

Given the sub-word ordering 'fun', this might come in handy somewhere :-) But yes, it's existence is more of a completeness/symmetry argument than anything else.

maintainer
#4 lore raw
Quoted 12 lines · Peter Zijlstra
Quoted 4 lines · Will Deacon

Overall, I'm not thrilled to bits by extending the atomics API with operations that cannot be implemented efficiently on any (?) architectures and are only used by the qspinlock slowpath on machines with more than 16K CPUs.

My rationale for proposing this primitive is similar to the existence of other composite atomic ops from the Misc (and refcount) class (as per atomic_t.txt). They're common/performance sensitive operations that, on LL/SC platforms, can be better implemented than a cmpxchg() loop.

Quoted 4 lines

Specifically here, it can be used to implement short xchg() in an architecturally neutral way, but more importantly it provides fwd progress on LL/SC, while most LL/SC based cmpxchg() implementations are arguably broken there.

Well, assuming the CPU provides forward progress for LL/SC which is _very_ rare (i.e. Power). If you implement LL/SC in your L1 it's really hard to get forward progress guarantees once your micro-architecture starts being aggressive about speculation.

For arm64, I would prefer the CAS loop to the LL/SC version, but we actually have short xchg() so I would much prefer that people used that! So my worry is that we start seeing users of this new thing crop up all over the place and it's not at all obvious that it's much worse than xchg().

Will

maintainer
#5 lore raw
In reply to Will Deacon
Quoted 11 lines · Will Deacon
Quoted 1 line · Rui Wang

This patch introduce a new atomic primitive andnot_or:

Please see my other comments on the other patches you posted:

https://lore.kernel.org/r/20210729093923.GD21151@willie-the-truck

Overall, I'm not thrilled to bits by extending the atomics API with operations that cannot be implemented efficiently on any (?) architectures and are only used by the qspinlock slowpath on machines with more than 16K CPUs.

Wouldn't this also help improve set_mask_bits()? That one at least has a handful of users in the kernel.

Arnd

maintainer
#6 lore raw
Quoted 8 lines · Arnd Bergmann
Quoted 7 lines · Will Deacon
Quoted 1 line · Rui Wang

This patch introduce a new atomic primitive andnot_or:

Please see my other comments on the other patches you posted:

https://lore.kernel.org/r/20210729093923.GD21151@willie-the-truck

Quoted 7 lines
Quoted 4 lines

Overall, I'm not thrilled to bits by extending the atomics API with operations that cannot be implemented efficiently on any (?) architectures and are only used by the qspinlock slowpath on machines with more than 16K CPUs.

Wouldn't this also help improve set_mask_bits()? That one at least has a handful of users in the kernel.

For pure LL/SC architectures, yes, but I don't think it helps anybody else.

Afaict, an architecture can already override set_mask_bits, so why do we need to add this primitive to the atomic API?

Will

#7 lore raw

Hi,

Quoted 18 lines · Will Deacon
Quoted 8 lines · Arnd Bergmann
Quoted 7 lines · Will Deacon
Quoted 1 line · Rui Wang

This patch introduce a new atomic primitive andnot_or:

Please see my other comments on the other patches you posted:

https://lore.kernel.org/r/20210729093923.GD21151@willie-the-truck

Quoted 7 lines
Quoted 4 lines

Overall, I'm not thrilled to bits by extending the atomics API with operations that cannot be implemented efficiently on any (?) architectures and are only used by the qspinlock slowpath on machines with more than 16K CPUs.

Wouldn't this also help improve set_mask_bits()? That one at least has a handful of users in the kernel.

Quoted 6 lines

For pure LL/SC architectures, yes, but I don't think it helps anybody else.

Afaict, an architecture can already override set_mask_bits, so why do we need to add this primitive to the atomic API?

Will

So what's next?

Now the set_mask_bits return oldval, return newval before 5.0. :-D commit 1db604f676b("include/linux/bitops.h: set_mask_bits() to return old value")

Regards,
Rui