[PATCH 5/5] xtensa: Emulate two-byte cmpxchg

From: Bradley Morgan

Date: Tue Sep 22 2026 - 13:41:39 EST


Xtensa has no two-byte atomic compare and swap, so the __cmpxchg()
switch lets case 2 fall through to __cmpxchg_called_with_bad_pointer(),
which is declared but never defined, so a two-byte cmpxchg() fails at
link time. Route case 2 through the new cmpxchg_emu_u16(), which takes
the old and new values as unsigned long, so the (unsigned long) casts
on _o_ and _n_ move off the call and into _old_ and _new_ declarations
that type check the old and new arguments against *ptr through
(unsigned long)(0 ? *ptr : (o)), the idiom David Laight suggested,
so cmpxchg(&p, 4, 5) no longer compiles silently.

Signed-off-by: Bradley Morgan <brads@xxxxxxxxxxxxxx>
---
arch/xtensa/include/asm/cmpxchg.h | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/arch/xtensa/include/asm/cmpxchg.h b/arch/xtensa/include/asm/cmpxchg.h
index b6db4838b175..f6bed4221b43 100644
--- a/arch/xtensa/include/asm/cmpxchg.h
+++ b/arch/xtensa/include/asm/cmpxchg.h
@@ -76,6 +76,7 @@ __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
{
switch (size) {
case 1: return cmpxchg_emu_u8(ptr, old, new);
+ case 2: return cmpxchg_emu_u16(ptr, old, new);
case 4: return __cmpxchg_u32(ptr, old, new);
default: __cmpxchg_called_with_bad_pointer();
return old;
@@ -83,10 +84,10 @@ __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
}

#define arch_cmpxchg(ptr,o,n) \
- ({ __typeof__(*(ptr)) _o_ = (o); \
- __typeof__(*(ptr)) _n_ = (n); \
- (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
- (unsigned long)_n_, sizeof (*(ptr))); \
+ ({ unsigned long _old_ = (unsigned long)(0 ? *(ptr) : (o)); \
+ unsigned long _new_ = (unsigned long)(0 ? *(ptr) : (n)); \
+ (__typeof__(*(ptr))) __cmpxchg((ptr), _old_, \
+ _new_, sizeof (*(ptr))); \
})

#include <asm-generic/cmpxchg-local.h>
--
2.47.3