git: a80b9ee15aa0 - main - atomic(9): Implement atomic_testand(clear|set)_ptr

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Tue, 19 Nov 2024 15:28:45 UTC
The branch main has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=a80b9ee15aa0f2da9f9f0c48e13324e0e5f2e840

commit a80b9ee15aa0f2da9f9f0c48e13324e0e5f2e840
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2024-11-19 15:24:50 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2024-11-19 15:24:50 +0000

    atomic(9): Implement atomic_testand(clear|set)_ptr
    
    For current architectures, these are just aliases for the existing
    operation on the relevant scalar integer.
    
    Reviewed by:    imp, kib
    Obtained from:  CheriBSD
    Sponsored by:   AFRL, DARPA
    Differential Revision:  https://reviews.freebsd.org/D47631
---
 share/man/man9/atomic.9      |  1 +
 sys/amd64/include/atomic.h   |  2 ++
 sys/arm/include/atomic.h     |  2 ++
 sys/arm64/include/atomic.h   |  2 ++
 sys/i386/include/atomic.h    |  4 ++++
 sys/kern/subr_asan.c         |  2 ++
 sys/kern/subr_csan.c         |  2 --
 sys/kern/subr_msan.c         |  2 ++
 sys/powerpc/include/atomic.h | 10 ++++++++++
 sys/riscv/include/atomic.h   |  2 ++
 10 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/share/man/man9/atomic.9 b/share/man/man9/atomic.9
index 436cfb7ee225..581aac9878c8 100644
--- a/share/man/man9/atomic.9
+++ b/share/man/man9/atomic.9
@@ -481,6 +481,7 @@ and
 functions are only implemented for the types
 .Dq Li int ,
 .Dq Li long ,
+.Dq ptr ,
 .Dq Li 32 ,
 and
 .Dq Li 64
diff --git a/sys/amd64/include/atomic.h b/sys/amd64/include/atomic.h
index 75a88e03290e..a8faedd58123 100644
--- a/sys/amd64/include/atomic.h
+++ b/sys/amd64/include/atomic.h
@@ -589,6 +589,8 @@ atomic_swap_long(volatile u_long *p, u_long v)
 #define	atomic_fcmpset_rel_ptr	atomic_fcmpset_rel_long
 #define	atomic_swap_ptr		atomic_swap_long
 #define	atomic_readandclear_ptr	atomic_readandclear_long
+#define	atomic_testandset_ptr	atomic_testandset_long
+#define	atomic_testandclear_ptr	atomic_testandclear_long
 
 #endif /* !WANT_FUNCTIONS */
 
diff --git a/sys/arm/include/atomic.h b/sys/arm/include/atomic.h
index 1fef40aea2e8..33116d0a6aee 100644
--- a/sys/arm/include/atomic.h
+++ b/sys/arm/include/atomic.h
@@ -1076,6 +1076,8 @@ atomic_thread_fence_seq_cst(void)
 #define atomic_load_acq_ptr		atomic_load_acq_32
 #define atomic_store_rel_ptr		atomic_store_rel_32
 #define atomic_swap_ptr			atomic_swap_32
+#define	atomic_testandset_ptr		atomic_testandset_32
+#define	atomic_testandclear_ptr		atomic_testandclear_32
 
 #define atomic_add_int			atomic_add_32
 #define atomic_add_acq_int		atomic_add_acq_32
diff --git a/sys/arm64/include/atomic.h b/sys/arm64/include/atomic.h
index c7aaa76d9de9..f7018f2f9e0b 100644
--- a/sys/arm64/include/atomic.h
+++ b/sys/arm64/include/atomic.h
@@ -609,6 +609,8 @@ _ATOMIC_STORE_REL_IMPL(64,  ,  )
 #define	atomic_set_ptr			atomic_set_64
 #define	atomic_swap_ptr			atomic_swap_64
 #define	atomic_subtract_ptr		atomic_subtract_64
+#define	atomic_testandclear_ptr		atomic_testandclear_64
+#define	atomic_testandset_ptr		atomic_testandset_64
 
 #define	atomic_add_acq_long		atomic_add_acq_64
 #define	atomic_fcmpset_acq_long		atomic_fcmpset_acq_64
diff --git a/sys/i386/include/atomic.h b/sys/i386/include/atomic.h
index 144b8f1d114c..f48ad55b8029 100644
--- a/sys/i386/include/atomic.h
+++ b/sys/i386/include/atomic.h
@@ -865,6 +865,10 @@ atomic_swap_long(volatile u_long *p, u_long v)
 	atomic_swap_int((volatile u_int *)(p), (u_int)(v))
 #define	atomic_readandclear_ptr(p) \
 	atomic_readandclear_int((volatile u_int *)(p))
+#define	atomic_testandclear_ptr(p, val) \
+	atomic_testandclear_int((volatile u_int *)(p), (val))
+#define	atomic_testandset_ptr(p, val) \
+	atomic_testandset_int((volatile u_int *)(p), (val))
 
 #endif /* !WANT_FUNCTIONS */
 
diff --git a/sys/kern/subr_asan.c b/sys/kern/subr_asan.c
index 75d9e75c531a..61466ffbf2ab 100644
--- a/sys/kern/subr_asan.c
+++ b/sys/kern/subr_asan.c
@@ -827,11 +827,13 @@ ASAN_ATOMIC_FUNC_TESTANDCLEAR(32, uint32_t);
 ASAN_ATOMIC_FUNC_TESTANDCLEAR(64, uint64_t);
 ASAN_ATOMIC_FUNC_TESTANDCLEAR(int, u_int);
 ASAN_ATOMIC_FUNC_TESTANDCLEAR(long, u_long);
+ASAN_ATOMIC_FUNC_TESTANDCLEAR(ptr, uintptr_t);
 
 ASAN_ATOMIC_FUNC_TESTANDSET(32, uint32_t);
 ASAN_ATOMIC_FUNC_TESTANDSET(64, uint64_t);
 ASAN_ATOMIC_FUNC_TESTANDSET(int, u_int);
 ASAN_ATOMIC_FUNC_TESTANDSET(long, u_long);
+ASAN_ATOMIC_FUNC_TESTANDSET(ptr, uintptr_t);
 
 ASAN_ATOMIC_FUNC_SWAP(32, uint32_t);
 ASAN_ATOMIC_FUNC_SWAP(64, uint64_t);
diff --git a/sys/kern/subr_csan.c b/sys/kern/subr_csan.c
index 580e632b05fd..5d0e6607403c 100644
--- a/sys/kern/subr_csan.c
+++ b/sys/kern/subr_csan.c
@@ -658,10 +658,8 @@ CSAN_ATOMIC_FUNC_SET(ptr, uintptr_t)
 CSAN_ATOMIC_FUNC_SUBTRACT(ptr, uintptr_t)
 CSAN_ATOMIC_FUNC_STORE(ptr, uintptr_t)
 CSAN_ATOMIC_FUNC_SWAP(ptr, uintptr_t)
-#if 0
 CSAN_ATOMIC_FUNC_TESTANDCLEAR(ptr, uintptr_t)
 CSAN_ATOMIC_FUNC_TESTANDSET(ptr, uintptr_t)
-#endif
 
 #define	CSAN_ATOMIC_FUNC_THREAD_FENCE(name)				\
 	void kcsan_atomic_thread_fence_##name(void)			\
diff --git a/sys/kern/subr_msan.c b/sys/kern/subr_msan.c
index ee60d6baf3fa..52ec5ce13bd1 100644
--- a/sys/kern/subr_msan.c
+++ b/sys/kern/subr_msan.c
@@ -1289,11 +1289,13 @@ MSAN_ATOMIC_FUNC_TESTANDCLEAR(32, uint32_t);
 MSAN_ATOMIC_FUNC_TESTANDCLEAR(64, uint64_t);
 MSAN_ATOMIC_FUNC_TESTANDCLEAR(int, u_int);
 MSAN_ATOMIC_FUNC_TESTANDCLEAR(long, u_long);
+MSAN_ATOMIC_FUNC_TESTANDCLEAR(ptr, uintptr_t);
 
 MSAN_ATOMIC_FUNC_TESTANDSET(32, uint32_t);
 MSAN_ATOMIC_FUNC_TESTANDSET(64, uint64_t);
 MSAN_ATOMIC_FUNC_TESTANDSET(int, u_int);
 MSAN_ATOMIC_FUNC_TESTANDSET(long, u_long);
+MSAN_ATOMIC_FUNC_TESTANDSET(ptr, uintptr_t);
 
 MSAN_ATOMIC_FUNC_SWAP(32, uint32_t);
 MSAN_ATOMIC_FUNC_SWAP(64, uint64_t);
diff --git a/sys/powerpc/include/atomic.h b/sys/powerpc/include/atomic.h
index 47fd9ce30fd6..0c3a57698342 100644
--- a/sys/powerpc/include/atomic.h
+++ b/sys/powerpc/include/atomic.h
@@ -1093,6 +1093,16 @@ atomic_testandset_acq_long(volatile u_long *p, u_int v)
 	return (a);
 }
 
+#ifdef __powerpc64__
+#define	atomic_testandclear_ptr		atomic_testandclear_long
+#define	atomic_testandset_ptr		atomic_testandset_long
+#else
+#define	atomic_testandclear_ptr(p,v)					\
+	atomic_testandclear_32((volatile u_int *)(p), v)
+#define	atomic_testandset_ptr(p,v)					\
+	atomic_testandset_32((volatile u_int *)(p), v)
+#endif
+
 static __inline void
 atomic_thread_fence_acq(void)
 {
diff --git a/sys/riscv/include/atomic.h b/sys/riscv/include/atomic.h
index 38261e95cf31..aaa7add6894b 100644
--- a/sys/riscv/include/atomic.h
+++ b/sys/riscv/include/atomic.h
@@ -557,6 +557,8 @@ atomic_swap_64(volatile uint64_t *p, uint64_t val)
 #define	atomic_set_ptr			atomic_set_64
 #define	atomic_subtract_ptr		atomic_subtract_64
 #define	atomic_swap_ptr			atomic_swap_64
+#define	atomic_testandclear_ptr		atomic_testandclear_64
+#define	atomic_testandset_ptr		atomic_testandset_64
 
 ATOMIC_ACQ_REL(set, 64)
 ATOMIC_ACQ_REL(clear, 64)