[PATCH 1/6] lib: Add two-byte cmpxchg emulation function

Paul E. McKenney paulmck at kernel.org
Wed Sep 23 10:00:33 PDT 2026


On Wed, Sep 23, 2026 at 11:43:13AM +0100, David Laight wrote:
> On Fri, 11 Sep 2026 19:25:34 +0000
> Bradley Morgan <brads at mainlining.org> wrote:
> 
> > cmpxchg_emu_u8() emulates one-byte cmpxchg() in terms of four-byte
> > cmpxchg() for the architectures lacking native one-byte atomics.
> > The same architectures also lack native two-byte cmpxchg(), where
> > such an operation is not supported and either fails to compile via
> > BUILD_BUG() or fails to link, because the bad pointer sentinels
> > these architectures declare are never defined.
> > 
> > Add cmpxchg_emu_u16(), the two-byte sibling. It reads the enclosing
> > word with READ_ONCE(), splices the two target bytes through a union
> > and loops on cmpxchg() of the full word until the compare succeeds.
> > Like cmpxchg_emu_u8() it is fully ordered.
> > 
> > The Kconfig symbol gating this file is renamed from
> > ARCH_NEED_CMPXCHG_1_EMU to ARCH_NEED_CMPXCHG_1_2_EMU, as it now
> > selects both the one-byte and the two-byte emulation.
> > 
> > Suggested-by: Paul E. McKenney <paulmck at kernel.org>
> > Signed-off-by: Bradley Morgan <brads at mainlining.org>
> > ---
> >  arch/Kconfig                |  2 +-
> >  arch/arc/Kconfig            |  2 +-
> >  arch/arm/Kconfig            |  2 +-
> >  arch/csky/Kconfig           |  2 +-
> >  arch/sh/Kconfig             |  2 +-
> >  arch/xtensa/Kconfig         |  2 +-
> >  include/linux/cmpxchg-emu.h |  1 +
> >  lib/Makefile                |  2 +-
> >  lib/cmpxchg-emu.c           | 35 ++++++++++++++++++++++++++++++++---
> >  9 files changed, 40 insertions(+), 10 deletions(-)
> > 
> > diff --git a/arch/Kconfig b/arch/Kconfig
> > index 3bb2e568f5b1..d56064797e09 100644
> > --- a/arch/Kconfig
> > +++ b/arch/Kconfig
> > @@ -1817,7 +1817,7 @@ config CC_HAS_SANE_FUNCTION_ALIGNMENT
> >  	# strict alignment always, even with -falign-functions.
> >  	def_bool CC_HAS_MIN_FUNCTION_ALIGNMENT || CC_IS_CLANG
> >  
> > -config ARCH_NEED_CMPXCHG_1_EMU
> > +config ARCH_NEED_CMPXCHG_1_2_EMU
> 
> Is it actually worth the churn of changing the name?
> Maybe just a comment that say it adds both 1 and 2 byte emulation?

The churn is only a few lines of code, and the potential confusion from
the name would be forever.  I agree that if we were stuck with the old
name, the comment would help, but why not just rip the bandage off and
be done with it?  ;-)

							Thanx, Paul



More information about the linux-arm-kernel mailing list