From nobody Fri Sep 25 02:02:28 2026 Received: from mail.mainlining.org (mail.mainlining.org [5.75.144.95]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 496C35326D5; Thu, 17 Sep 2026 16:38:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=5.75.144.95 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789663118; cv=none; b=cBGgp7u4DFcq5fe2JwdNZvZdzNlz83F8sLqTHrEXi1BTuyPXzHZQQfAtEbjCCAiaFu2mRLSCynKJaikVSyVejhtJD0WN/MSCEvXyIgJnxKJmSQ4PWV6aL/dU1QispviAlRkhOckywQObUIREQMTOxTJd19JPipcBFo4dPUY7JxI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789663118; c=relaxed/simple; bh=Q3a7nffzh4TnjwGqjLdckQ0m6RsFoKo/58dZkbxxriE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KeAo4DLZFs5qdP2XNaN4qL2AwbbUbuxn4BTO9qF1qMc58pWoIjZGRQVuxOLbD5EWR/JNOXz67RIk91hS5L7JZrW8xGEb/leeBPOrWXseTbm5e55bD6aVLsVYzxbrn2K4BVjkM3vmKUz7Og/e0Ikx/eWbtaUvBv1y41s3ca5d2+c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mainlining.org; spf=pass smtp.mailfrom=mainlining.org; dkim=pass (2048-bit key) header.d=mainlining.org header.i=@mainlining.org header.b=mavJU5Ux; dkim=permerror (0-bit key) header.d=mainlining.org header.i=@mainlining.org header.b=1gfHML2R; arc=none smtp.client-ip=5.75.144.95 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mainlining.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mainlining.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mainlining.org header.i=@mainlining.org header.b="mavJU5Ux"; dkim=permerror (0-bit key) header.d=mainlining.org header.i=@mainlining.org header.b="1gfHML2R" DKIM-Signature: v=1; a=rsa-sha256; s=202507r; d=mainlining.org; c=relaxed/relaxed; h=Message-ID:Date:Subject:To:From; t=1789663110; bh=3RvxnExRr/i5KZDzKSfMkA5 w2CfSpqJW35Z/SWmaY2g=; b=mavJU5UxEV8BUwL3X8Q3WN5WEJY2eRju5jxKcshQHv7K3lfuQ1 b2S8WHhugFwCMPjBRCLRKXRi4ni8kKxptKKbgGDh07yvVMNuViOIx6/CnKfahTQAU9fYOnzE87h 2ojZepIf/xJHn78KdWO4O3V3YGuhHjBsL4iq8HQVPl00ylxvTWjZf6Q58x0hD5I33hZsE3jP/pW 3gfno69YS0GXlVvqjCtty8f2c+A6iQ8sD5YoNK8t9sWpbWPdGLVfoBTpm1x4lPb0H60B6TMZPUz fGl8EZl/LaBLBAXgopK8C8yey5iZwqDytD+xrKOVv0irLTf//LjRjjXKyny+OJQvRCg==; DKIM-Signature: v=1; a=ed25519-sha256; s=202507e; d=mainlining.org; c=relaxed/relaxed; h=Message-ID:Date:Subject:To:From; t=1789663110; bh=3RvxnExRr/i5KZDzKSfMkA5 w2CfSpqJW35Z/SWmaY2g=; b=1gfHML2RRTH3V9hBrigdMnz+gaI97gHJK6V0ovv+sg0p56cx1s 1OQuOzAuP4zKnEgy87RReNAcrr0smG/1wVAw==; From: Bradley Morgan To: Andrew Morton , Vineet Gupta , Guo Ren , Yoshinori Sato , Rich Felker , Chris Zankel , Max Filippov Cc: Arnd Bergmann , "Paul E . McKenney" , David Laight , John Paul Adrian Glaubitz , linux-snps-arc@lists.infradead.org, linux-csky@vger.kernel.org, linux-sh@vger.kernel.org, linux-kernel@vger.kernel.org, Bradley Morgan Subject: [PATCH v3 1/5] lib: Add two-byte cmpxchg emulation function Date: Thu, 17 Sep 2026 16:38:26 +0000 Message-ID: <20260917163830.3748-2-brads@mainlining.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260917163830.3748-1-brads@mainlining.org> References: <20260917163830.3748-1-brads@mainlining.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Unlike cmpxchg_emu_u8() it casts the old and new values to u16 internally and returns unsigned long, taking the old and new values as unsigned long, per the suggestion from David Laight. The switch statements in the architecture macros instantiate every size case, so a cmpxchg() on a pointer type checks the two-byte case as well, and a u16 parameter or return would make the macro casts and return conversions warn there. With unsigned long parameters and return the call sites need no narrowing casts, pointer exchanges compile warning free, and the function still compares and returns exactly the 16 bits the caller asked for, which matches the hardware cmpxchg r16 behaviour where a 16-bit compare only looks at the low 16 bits of the register. 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 Suggested-by: David Laight Signed-off-by: Bradley Morgan --- arch/Kconfig | 2 +- arch/arc/Kconfig | 2 +- arch/csky/Kconfig | 2 +- arch/sh/Kconfig | 2 +- arch/xtensa/Kconfig | 2 +- include/linux/cmpxchg-emu.h | 4 +++- lib/Makefile | 2 +- lib/cmpxchg-emu.c | 40 ++++++++++++++++++++++++++++++++----- 8 files changed, 44 insertions(+), 12 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 =20 -config ARCH_NEED_CMPXCHG_1_EMU +config ARCH_NEED_CMPXCHG_1_2_EMU bool =20 config ARCH_WANTS_PRE_LINK_VMLINUX diff --git a/arch/arc/Kconfig b/arch/arc/Kconfig index 80e61175bf52..0e91d254eccb 100644 --- a/arch/arc/Kconfig +++ b/arch/arc/Kconfig @@ -14,7 +14,7 @@ config ARC select ARCH_HAS_SETUP_DMA_OPS select ARCH_HAS_SYNC_DMA_FOR_CPU select ARCH_HAS_SYNC_DMA_FOR_DEVICE - select ARCH_NEED_CMPXCHG_1_EMU + select ARCH_NEED_CMPXCHG_1_2_EMU select ARCH_SUPPORTS_ATOMIC_RMW if ARC_HAS_LLSC select ARCH_32BIT_OFF_T select BUILDTIME_TABLE_SORT diff --git a/arch/csky/Kconfig b/arch/csky/Kconfig index 4331313a42ff..5f26e8615d7d 100644 --- a/arch/csky/Kconfig +++ b/arch/csky/Kconfig @@ -37,7 +37,7 @@ config CSKY select ARCH_INLINE_SPIN_UNLOCK_BH if !PREEMPTION select ARCH_INLINE_SPIN_UNLOCK_IRQ if !PREEMPTION select ARCH_INLINE_SPIN_UNLOCK_IRQRESTORE if !PREEMPTION - select ARCH_NEED_CMPXCHG_1_EMU + select ARCH_NEED_CMPXCHG_1_2_EMU select ARCH_WANT_FRAME_POINTERS if !CPU_CK610 && $(cc-option,-mbacktrace) select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT select COMMON_CLK diff --git a/arch/sh/Kconfig b/arch/sh/Kconfig index d60f1d5a94c0..09e556fced01 100644 --- a/arch/sh/Kconfig +++ b/arch/sh/Kconfig @@ -14,7 +14,7 @@ config SUPERH select ARCH_HIBERNATION_POSSIBLE if MMU select ARCH_MIGHT_HAVE_PC_PARPORT select ARCH_WANT_IPC_PARSE_VERSION - select ARCH_NEED_CMPXCHG_1_EMU + select ARCH_NEED_CMPXCHG_1_2_EMU select CPU_NO_EFFICIENT_FFS select DMA_DECLARE_COHERENT select GENERIC_ATOMIC64 diff --git a/arch/xtensa/Kconfig b/arch/xtensa/Kconfig index f2f9cd9cde50..62019fd444d4 100644 --- a/arch/xtensa/Kconfig +++ b/arch/xtensa/Kconfig @@ -14,7 +14,7 @@ config XTENSA select ARCH_HAS_DMA_SET_UNCACHED if MMU select ARCH_HAS_STRNCPY_FROM_USER if !KASAN select ARCH_HAS_STRNLEN_USER - select ARCH_NEED_CMPXCHG_1_EMU + select ARCH_NEED_CMPXCHG_1_2_EMU select ARCH_USE_MEMTEST select ARCH_USE_QUEUED_RWLOCKS select ARCH_USE_QUEUED_SPINLOCKS diff --git a/include/linux/cmpxchg-emu.h b/include/linux/cmpxchg-emu.h index 998deec67740..2db70f1e3925 100644 --- a/include/linux/cmpxchg-emu.h +++ b/include/linux/cmpxchg-emu.h @@ -4,12 +4,14 @@ * lacking direct support for these sizes. These are implemented in terms * of 4-byte cmpxchg operations. * - * Copyright (C) 2024 Paul E. McKenney. + * Copyright (C) 2024 Paul E. McKenney + * Copyright (C) 2026 Bradley Morgan */ =20 #ifndef __LINUX_CMPXCHG_EMU_H #define __LINUX_CMPXCHG_EMU_H =20 uintptr_t cmpxchg_emu_u8(volatile u8 *p, uintptr_t old, uintptr_t new); +unsigned long cmpxchg_emu_u16(volatile u16 *p, unsigned long old, unsigned= long new); =20 #endif /* __LINUX_CMPXCHG_EMU_H */ diff --git a/lib/Makefile b/lib/Makefile index 43421c39d21b..d0e69312176a 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -202,7 +202,7 @@ obj-$(CONFIG_CODE_TAGGING) +=3D codetag.o lib-$(CONFIG_GENERIC_BUG) +=3D bug.o =20 obj-$(CONFIG_HAVE_ARCH_TRACEHOOK) +=3D syscall.o -obj-$(CONFIG_ARCH_NEED_CMPXCHG_1_EMU) +=3D cmpxchg-emu.o +obj-$(CONFIG_ARCH_NEED_CMPXCHG_1_2_EMU) +=3D cmpxchg-emu.o =20 obj-$(CONFIG_DYNAMIC_DEBUG_CORE) +=3D dynamic_debug.o #ensure exported functions have prototypes diff --git a/lib/cmpxchg-emu.c b/lib/cmpxchg-emu.c index 27f6f97cb60d..ac1c84383887 100644 --- a/lib/cmpxchg-emu.c +++ b/lib/cmpxchg-emu.c @@ -1,10 +1,11 @@ // SPDX-License-Identifier: GPL-2.0+ /* - * Emulated 1-byte cmpxchg operation for architectures lacking direct - * support for this size. This is implemented in terms of 4-byte cmpxchg - * operations. + * Emulated 1-byte and 2-byte cmpxchg operations for architectures lacking + * direct support for these sizes. These are implemented in terms of + * 4-byte cmpxchg operations. * - * Copyright (C) 2024 Paul E. McKenney. + * Copyright (C) 2024 Paul E. McKenney + * Copyright (C) 2026 Bradley Morgan */ =20 #include @@ -40,6 +41,35 @@ uintptr_t cmpxchg_emu_u8(volatile u8 *p, uintptr_t old, = uintptr_t new) instrument_atomic_read_write(p, 1); ret =3D data_race(cmpxchg(p32, old32.w, new32.w)); // Overridden above. } while (ret !=3D old32.w); - return old; + return (u16)old; } EXPORT_SYMBOL_GPL(cmpxchg_emu_u8); + +union u16_32 { + u16 h[2]; + u32 w; +}; + +/* Emulate two-byte cmpxchg() in terms of 4-byte cmpxchg. */ +unsigned long cmpxchg_emu_u16(volatile u16 *p, unsigned long old, unsigned= long new) +{ + u32 *p32 =3D (u32 *)(((uintptr_t)p) & ~0x3); + int i =3D (((uintptr_t)p) & 0x2) / 2; + union u16_32 old32; + union u16_32 new32; + u32 ret; + + WARN_ON_ONCE(((uintptr_t)p) & 0x1); + ret =3D READ_ONCE(*p32); + do { + old32.w =3D ret; + if (old32.h[i] !=3D (u16)old) + return (unsigned long)old32.h[i]; + new32.w =3D old32.w; + new32.h[i] =3D (u16)new; + instrument_atomic_read_write(p, 2); + ret =3D data_race(cmpxchg(p32, old32.w, new32.w)); // Overridden above. + } while (ret !=3D old32.w); + return (u16)old; +} +EXPORT_SYMBOL_GPL(cmpxchg_emu_u16); --=20 2.47.3 From nobody Fri Sep 25 02:02:28 2026 Received: from mail.mainlining.org (mail.mainlining.org [5.75.144.95]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1D49E4F4CED; Thu, 17 Sep 2026 16:40:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=5.75.144.95 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789663259; cv=none; b=K9kEhNKadyQiJc6itRmYmzFexGxqSrRDj5dKiHfdn3bmv5FlznYbiHu+ghC5U75CoWhgTatume8WiDVWd9EX5x++sTrweYcBiHFID2+X9IAYGfog9pV4jd7qvzWlP2/Qt+wJKgD3oqGMoYG689y48m/3jxNc9y59tdLGnxIdElo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789663259; c=relaxed/simple; bh=qFn7dLToUkvIgv0m6saPs5B0Agok+SF6dJ4kaifw88U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=W3RWcTb1s4GloAFvvxKoFO1lTzns7BHf2CWxq/AfgoFcq3jJEj1vStG6/hIBOypLV2YZh6o4r0Tm5Ctz7C2N9YuVeggy3HMH9q1/nkdlYt63ckOTMJJ+fdDwX+CrT4rj0fYhtrRSv6FUg7wsODrl51/3G3OL1JFx/Tw+eMH5SaE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mainlining.org; spf=pass smtp.mailfrom=mainlining.org; dkim=pass (2048-bit key) header.d=mainlining.org header.i=@mainlining.org header.b=rJO3Ncd/; dkim=permerror (0-bit key) header.d=mainlining.org header.i=@mainlining.org header.b=1W+mBsO/; arc=none smtp.client-ip=5.75.144.95 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mainlining.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mainlining.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mainlining.org header.i=@mainlining.org header.b="rJO3Ncd/"; dkim=permerror (0-bit key) header.d=mainlining.org header.i=@mainlining.org header.b="1W+mBsO/" DKIM-Signature: v=1; a=rsa-sha256; s=202507r; d=mainlining.org; c=relaxed/relaxed; h=Message-ID:Date:Subject:To:From; t=1789663110; bh=aoBMHO5+/N93x6VJ0ikS7kV mlAkKZm9tlGAN4/+qBSY=; b=rJO3Ncd/TnwsSG6RKws52boGwVzJTIPvSJEp4C7cL4OTLvpZqN IeFlOtZKzBFgxVuy03X79ASjrTRcxSsaBaD9ZdJTxYTO0Sierje8pTwulB9tiUTFE2kuMV4XHQk XE82T6ODkRn5X2pqSoOl3W5l5cOQQn5TOT48lwwWroD3Vt/yLF2+Wf7O+/ohZ8pz6AXLBx9CoF2 FnR/Eylo5MjcHXjnT3NVHCcFMC7Z6gpgTF+4Lgu/K3KhQynA7F1Ttxb7PQmRf0TucjKBUzr7wvb H9lCObzaTfulg5yPgAF4gXSyoJ4OLQDlj4KxJmDr/tWhzqb+K/p4xnksRCWK8fPJ4Pw==; DKIM-Signature: v=1; a=ed25519-sha256; s=202507e; d=mainlining.org; c=relaxed/relaxed; h=Message-ID:Date:Subject:To:From; t=1789663110; bh=aoBMHO5+/N93x6VJ0ikS7kV mlAkKZm9tlGAN4/+qBSY=; b=1W+mBsO/mmKw5OVcobpC6PP5Ha7M073LKARMu5sMFMTpULj9s4 M4vkGskgCo9QyIECUmxAYNPQ1IJNLExTN4BQ==; From: Bradley Morgan To: Andrew Morton , Vineet Gupta , Guo Ren , Yoshinori Sato , Rich Felker , Chris Zankel , Max Filippov Cc: Arnd Bergmann , "Paul E . McKenney" , David Laight , John Paul Adrian Glaubitz , linux-snps-arc@lists.infradead.org, linux-csky@vger.kernel.org, linux-sh@vger.kernel.org, linux-kernel@vger.kernel.org, Bradley Morgan Subject: [PATCH v3 2/5] ARC: Emulate two-byte cmpxchg Date: Thu, 17 Sep 2026 16:38:27 +0000 Message-ID: <20260917163830.3748-3-brads@mainlining.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260917163830.3748-1-brads@mainlining.org> References: <20260917163830.3748-1-brads@mainlining.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" ARC has no two-byte atomic compare and swap, so the __cmpxchg() macro switch lets case 2 fall through to BUILD_BUG() via default. Route case 2 through the new cmpxchg_emu_u16(), which takes the old and new values as unsigned long and narrows them itself, so the (uintptr_t) casts on _o_ and _n_ are no longer needed and the case 2 call passes them straight. Signed-off-by: Bradley Morgan --- arch/arc/include/asm/cmpxchg.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/arc/include/asm/cmpxchg.h b/arch/arc/include/asm/cmpxchg.h index 76f43db0890f..9b48b8fe0db2 100644 --- a/arch/arc/include/asm/cmpxchg.h +++ b/arch/arc/include/asm/cmpxchg.h @@ -50,6 +50,9 @@ case 1: \ _prev_ =3D (__typeof__(*(ptr)))cmpxchg_emu_u8((volatile u8 *__force)_p_,= (uintptr_t)_o_, (uintptr_t)_n_); \ break; \ + case 2: \ + _prev_ =3D (__typeof__(*(ptr)))cmpxchg_emu_u16((volatile u16 *__force)_p= _, (uintptr_t)_o_, (uintptr_t)_n_); \ + break; \ case 4: \ _prev_ =3D __cmpxchg(_p_, _o_, _n_); \ break; \ --=20 2.47.3 From nobody Fri Sep 25 02:02:28 2026 Received: from mail.mainlining.org (mail.mainlining.org [5.75.144.95]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D033E4FD27E; Thu, 17 Sep 2026 16:40:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=5.75.144.95 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789663261; cv=none; b=mxsTkrYkQ8+iyCVB/Ru3PY5kfo1EPNBXL8UpV7Cl3l3K+Otc0ZWi5HtVZ6+DBeG4co4KwU5SrIrzn1a+BDZK2Kgg6ceMqkKEXojbNLwedV5JtMMcBn31/cZ3kOtR1VZ6wourgDWE1pIw/Apb96vFv10Nn2FIAd3oCdZt8F5CnZg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789663261; c=relaxed/simple; bh=FJPfu7LFjRjc6ZJbnsAto0bfKwxBduCiy3SP0uqRsFM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FRhuJpNu8jYdzPoLJAkG9nRPsogVMjFjWIyDjvPjahTMQn8Hzk3WoSiMFzzp37KkBXD+4XWDxZ6sne5ByUiOBfQYbaAaOh5a8Zcc1MxZUUzX0VZK90RFIKI4yggmqV5rWCR7oTHre0o+QJDiwc/QCvVqA83m4fpezjl4OBkU5kY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mainlining.org; spf=pass smtp.mailfrom=mainlining.org; dkim=pass (2048-bit key) header.d=mainlining.org header.i=@mainlining.org header.b=YTDbLKwM; dkim=permerror (0-bit key) header.d=mainlining.org header.i=@mainlining.org header.b=vOxLdiXe; arc=none smtp.client-ip=5.75.144.95 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mainlining.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mainlining.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mainlining.org header.i=@mainlining.org header.b="YTDbLKwM"; dkim=permerror (0-bit key) header.d=mainlining.org header.i=@mainlining.org header.b="vOxLdiXe" DKIM-Signature: v=1; a=rsa-sha256; s=202507r; d=mainlining.org; c=relaxed/relaxed; h=Message-ID:Date:Subject:To:From; t=1789663111; bh=gF0jpOzqVIhOvYSoNgdvd4z PpHZVOqEDRyAYG3KMg0M=; b=YTDbLKwMGcQTYm5dxY3iKNEBtq5PZYfPplCnBNArkKE9tM2Zz/ eu0hGtCPX/qsIDEUpNe8nt4G/I4+qgrRuNu+McSLl6zKJMldhvE96d1mjoPQh3y5tNVgmqY/9t3 eZQO5dUgjPoXBcHFiuuFtvcAnt2gwzwhDspUmcltwll5RVMTRsENWLQkDWZyxbt8Lk7YPRf3hh2 iPLD+UyvG7kmcQb6W3Cytg+q4b0y1i4qwne+cS7AVS+7n72nbPFCe/4UckhR2ozTMVo2YMsNL8G uaDIWKGcjWM1b2QrFKX6p2Ebu9KYCS8TB47+v++JA8rGv0Rd9i1+88LM6pACdnY9CoA==; DKIM-Signature: v=1; a=ed25519-sha256; s=202507e; d=mainlining.org; c=relaxed/relaxed; h=Message-ID:Date:Subject:To:From; t=1789663111; bh=gF0jpOzqVIhOvYSoNgdvd4z PpHZVOqEDRyAYG3KMg0M=; b=vOxLdiXeRsfXKHFjjuEd8N46x17rPZMf9Y7yTn2qUgVQuicb97 nSPzupPG62E/fI1TAHC6dAzXDxySnyWkQGAw==; From: Bradley Morgan To: Andrew Morton , Vineet Gupta , Guo Ren , Yoshinori Sato , Rich Felker , Chris Zankel , Max Filippov Cc: Arnd Bergmann , "Paul E . McKenney" , David Laight , John Paul Adrian Glaubitz , linux-snps-arc@lists.infradead.org, linux-csky@vger.kernel.org, linux-sh@vger.kernel.org, linux-kernel@vger.kernel.org, Bradley Morgan Subject: [PATCH v3 3/5] csky: Emulate two-byte cmpxchg Date: Thu, 17 Sep 2026 16:38:28 +0000 Message-ID: <20260917163830.3748-4-brads@mainlining.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260917163830.3748-1-brads@mainlining.org> References: <20260917163830.3748-1-brads@mainlining.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" CSKY has no two-byte atomic compare and swap, so the __cmpxchg() switches in cmpxchg.h let case 2 fall through to the undefined __cmpxchg_called_with_bad_pointer(), failing at link time. Route case 2 through the new cmpxchg_emu_u16(), which takes the old and new values as unsigned long and narrows them itself, so the (uintptr_t) casts on __old and __new come off in all three switch instances. The (u16) casts are gone for the same reason. The __old and __new declarations were typed __typeof__(old) and __typeof__(new), which skips the pointer-integer type check, so cmpxchg(&p, 4, 5) compiled silently. Typing them through (unsigned long)(0 ? *(ptr) : (old)) keeps the value conversion while making the compiler reject mismatched types, the idiom David Laight suggested. The same check is added for new. Signed-off-by: Bradley Morgan --- arch/csky/include/asm/cmpxchg.h | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/arch/csky/include/asm/cmpxchg.h b/arch/csky/include/asm/cmpxch= g.h index db6dda47184e..29dc56e4b7f1 100644 --- a/arch/csky/include/asm/cmpxchg.h +++ b/arch/csky/include/asm/cmpxchg.h @@ -57,13 +57,16 @@ #define __cmpxchg_relaxed(ptr, old, new, size) \ ({ \ __typeof__(ptr) __ptr =3D (ptr); \ - __typeof__(new) __new =3D (new); \ - __typeof__(new) __tmp; \ - __typeof__(old) __old =3D (old); \ + unsigned long __old =3D (unsigned long)(0 ? *(ptr) : (old)); \ + unsigned long __new =3D (unsigned long)(0 ? *(ptr) : (new)); \ + unsigned long __tmp; \ __typeof__(*(ptr)) __ret; \ switch (size) { \ case 1: \ - __ret =3D (__typeof__(*(ptr)))cmpxchg_emu_u8((volatile u8 *)__ptr, (uint= ptr_t)__old, (uintptr_t)__new); \ + __ret =3D (__typeof__(*(ptr)))cmpxchg_emu_u8((volatile u8 *)__ptr, __old= , __new); \ + break; \ + case 2: \ + __ret =3D (__typeof__(*(ptr)))cmpxchg_emu_u16((volatile u16 *)__ptr, __o= ld, __new); \ break; \ case 4: \ asm volatile ( \ @@ -90,13 +93,16 @@ #define __cmpxchg_acquire(ptr, old, new, size) \ ({ \ __typeof__(ptr) __ptr =3D (ptr); \ - __typeof__(new) __new =3D (new); \ - __typeof__(new) __tmp; \ - __typeof__(old) __old =3D (old); \ + unsigned long __old =3D (unsigned long)(0 ? *(ptr) : (old)); \ + unsigned long __new =3D (unsigned long)(0 ? *(ptr) : (new)); \ + unsigned long __tmp; \ __typeof__(*(ptr)) __ret; \ switch (size) { \ case 1: \ - __ret =3D (__typeof__(*(ptr)))cmpxchg_emu_u8((volatile u8 *)__ptr, (uint= ptr_t)__old, (uintptr_t)__new); \ + __ret =3D (__typeof__(*(ptr)))cmpxchg_emu_u8((volatile u8 *)__ptr, __old= , __new); \ + break; \ + case 2: \ + __ret =3D (__typeof__(*(ptr)))cmpxchg_emu_u16((volatile u16 *)__ptr, __o= ld, __new); \ break; \ case 4: \ asm volatile ( \ @@ -124,13 +130,16 @@ #define __cmpxchg(ptr, old, new, size) \ ({ \ __typeof__(ptr) __ptr =3D (ptr); \ - __typeof__(new) __new =3D (new); \ - __typeof__(new) __tmp; \ - __typeof__(old) __old =3D (old); \ + unsigned long __old =3D (unsigned long)(0 ? *(ptr) : (old)); \ + unsigned long __new =3D (unsigned long)(0 ? *(ptr) : (new)); \ + unsigned long __tmp; \ __typeof__(*(ptr)) __ret; \ switch (size) { \ case 1: \ - __ret =3D (__typeof__(*(ptr)))cmpxchg_emu_u8((volatile u8 *)__ptr, (uint= ptr_t)__old, (uintptr_t)__new); \ + __ret =3D (__typeof__(*(ptr)))cmpxchg_emu_u8((volatile u8 *)__ptr, __old= , __new); \ + break; \ + case 2: \ + __ret =3D (__typeof__(*(ptr)))cmpxchg_emu_u16((volatile u16 *)__ptr, __o= ld, __new); \ break; \ case 4: \ asm volatile ( \ --=20 2.47.3 From nobody Fri Sep 25 02:02:28 2026 Received: from mail.mainlining.org (mail.mainlining.org [5.75.144.95]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 18D87518142; Thu, 17 Sep 2026 16:38:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=5.75.144.95 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789663132; cv=none; b=L9a8owRfa6D8/K7Zr/XVXWy4IpGFnjb4R+5Ge+QJglFBYIzmPBlKw38/jDYh949EXGvtvZRirIHzNkoZk6AeilkDxNVaTtsseY6bH9mzI4H52mMXCiFHpjt6PMjL4WXwR+QCxVL163sMe7R/Uonp+AUasRy3nL6HiqFQhJdmdiI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789663132; c=relaxed/simple; bh=yQbHi2/qjWQm7R28Ag7bKayxGrtMhrNYW+yci+1KRH8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=vFZUqKyBQaxFjJtHwgS0ML9hL8Z5HYTSQZLRFlITsxZ81V6TvuyegGgh+FMVzWmGWSf5L0AA0Km81iJDe6B1Y7CESh3OtcjM5YM00pO+Ywf5bbd1/4mrzv7gClTssUMI4eJlZ/HkCO1m+bCeRae5hQX1F+vkA98slXyRZHx0uzI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mainlining.org; spf=pass smtp.mailfrom=mainlining.org; dkim=pass (2048-bit key) header.d=mainlining.org header.i=@mainlining.org header.b=fyB1YvSK; dkim=permerror (0-bit key) header.d=mainlining.org header.i=@mainlining.org header.b=aqA1APFo; arc=none smtp.client-ip=5.75.144.95 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mainlining.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mainlining.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mainlining.org header.i=@mainlining.org header.b="fyB1YvSK"; dkim=permerror (0-bit key) header.d=mainlining.org header.i=@mainlining.org header.b="aqA1APFo" DKIM-Signature: v=1; a=rsa-sha256; s=202507r; d=mainlining.org; c=relaxed/relaxed; h=Message-ID:Date:Subject:To:From; t=1789663111; bh=ghYvDCJ6wWgl/DWKvRlZLjc r4JmsCO6Diem7VtlYvoo=; b=fyB1YvSKvGg1LWKCQZhUBc492tVVzKUf+KZBnI5LNuvCJ5g+ZU E6bkZfZWBE+ZQIYJNva6H1UqybZAs2u6MSsWz7m3aJKEZMUommjJq2vgXFTYVszJusLk7IevwrV 3uAVll3tWQSPCu8psXyYvDa346sjiFeMpOTxP5FVJ2OXn8ivF7Ar9zMblkFo0Sc1qIgzQTEoMnz eECEFt8PMgT7hUncGGx7FjraHbNbzdqgoitO2DlTK+xF0xBeNMnkBZrDOGfDZLxGj/YwQIn8g2u k6hg6hp3BRJs3diJxFQC1cKrT8U/XjrLCX9avh/i1pVEwYhevJmc5z5MoF9+JtxIuPA==; DKIM-Signature: v=1; a=ed25519-sha256; s=202507e; d=mainlining.org; c=relaxed/relaxed; h=Message-ID:Date:Subject:To:From; t=1789663111; bh=ghYvDCJ6wWgl/DWKvRlZLjc r4JmsCO6Diem7VtlYvoo=; b=aqA1APFosR9li9xYj4aOMoBwNnHa27+/AJWqeXDYbSCs4I8uW1 aFOhtFdK/xx/jVETv72FWV94mClzvdy8f5Bg==; From: Bradley Morgan To: Andrew Morton , Vineet Gupta , Guo Ren , Yoshinori Sato , Rich Felker , Chris Zankel , Max Filippov Cc: Arnd Bergmann , "Paul E . McKenney" , David Laight , John Paul Adrian Glaubitz , linux-snps-arc@lists.infradead.org, linux-csky@vger.kernel.org, linux-sh@vger.kernel.org, linux-kernel@vger.kernel.org, Bradley Morgan Subject: [PATCH v3 4/5] sh: Emulate two-byte cmpxchg Date: Thu, 17 Sep 2026 16:38:29 +0000 Message-ID: <20260917163830.3748-5-brads@mainlining.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260917163830.3748-1-brads@mainlining.org> References: <20260917163830.3748-1-brads@mainlining.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" SH has no byte or halfword atomic memory operations, so the __cmpxchg() switch routes case 1 through cmpxchg_emu_u8() and 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 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. Acked-by: John Paul Adrian Glaubitz Signed-off-by: Bradley Morgan --- arch/sh/include/asm/cmpxchg.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/arch/sh/include/asm/cmpxchg.h b/arch/sh/include/asm/cmpxchg.h index 1e5dc5ccf7bf..b87f59107b4e 100644 --- a/arch/sh/include/asm/cmpxchg.h +++ b/arch/sh/include/asm/cmpxchg.h @@ -59,6 +59,8 @@ static inline unsigned long __cmpxchg(volatile void * ptr= , unsigned long old, 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); } @@ -70,8 +72,10 @@ static inline unsigned long __cmpxchg(volatile void * pt= r, unsigned long old, ({ \ __typeof__(*(ptr)) _o_ =3D (o); \ __typeof__(*(ptr)) _n_ =3D (n); \ - (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \ - (unsigned long)_n_, sizeof(*(ptr))); \ + unsigned long _old_ =3D (unsigned long)(0 ? *ptr : _o_); \ + unsigned long _new_ =3D (unsigned long)(0 ? *ptr : _n_); \ + (__typeof__(*(ptr))) __cmpxchg((ptr), _old_, \ + _new_, sizeof(*(ptr))); \ }) =20 #include --=20 2.47.3 From nobody Fri Sep 25 02:02:28 2026 Received: from mail.mainlining.org (mail.mainlining.org [5.75.144.95]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C90284F85AF; Thu, 17 Sep 2026 16:39:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=5.75.144.95 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789663148; cv=none; b=IqM7BRERLIXtqyZjZUHFJLxL3pDCOGF2wME93d2gv9otZBV1vEEK/DrlCYkyvvNhorDSKAC8XfaH23qt4AV3OJ77/dPB7CGEPpQuhT1XrAXe8rGkPgCccdltRh1hF/7HTnorzwgUXO4Pfz527GfIJWw3WSqi/1s8Y1TUqVZFy/Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789663148; c=relaxed/simple; bh=kcK6nhh7W+FtR/GkcJBP7Jmd3mCfA61aItwWSBreHuA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fTTIzocmkXt2fr5KToxe6Oo7eYkbu4S/IqjmMVh3w60fReD711A9wEofyehOF1lcJVtXCQKVzVszcphr9B8s9Nk7ET874KXc3g/uivFLUWwIdlxr1OZ3Ntk1jc6sOwjPy9Cyv5L4ALLWPT51BhL6booRp/b+R/TQVmm8h6qoPao= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mainlining.org; spf=pass smtp.mailfrom=mainlining.org; dkim=pass (2048-bit key) header.d=mainlining.org header.i=@mainlining.org header.b=VxQkqBBW; dkim=permerror (0-bit key) header.d=mainlining.org header.i=@mainlining.org header.b=E5BykGjy; arc=none smtp.client-ip=5.75.144.95 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mainlining.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mainlining.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mainlining.org header.i=@mainlining.org header.b="VxQkqBBW"; dkim=permerror (0-bit key) header.d=mainlining.org header.i=@mainlining.org header.b="E5BykGjy" DKIM-Signature: v=1; a=rsa-sha256; s=202507r; d=mainlining.org; c=relaxed/relaxed; h=Message-ID:Date:Subject:To:From; t=1789663143; bh=3ywXA6YlVC+E1c3gGiCHQc0 l4jiAYrAVWl7TP3MSFhU=; b=VxQkqBBW9sRnf40WfNQy0UNwy7EMU86vhigeDr60kGk1Nkrdhg efJJWIXVw1XFP1+OQSou8wBMpJ9mm7utA+U58oJvMTgL7aCyLwY9vELXF2bepXamAugyOl5Q3Rw /ShVX1cJmj1JGkGzZZaQjWvWUaQvEwu4Q0aWkDhKD0PURLdFoIJhf7CE+aV+nA19KvyDAmT4Vzk WhwmpmZH3IcLOH1pjWBpzcNTzAY6HTOdje8GXQ0/7Z7chafYH6OnCyiwBsJK0s+rfANzP7ZIqVg V2GzCs5/N/I7Aw3n+mVYXxJcQRqknj88K/zJ6/wkxQPUrnlgrvr9SIXo5miscXITElA==; DKIM-Signature: v=1; a=ed25519-sha256; s=202507e; d=mainlining.org; c=relaxed/relaxed; h=Message-ID:Date:Subject:To:From; t=1789663143; bh=3ywXA6YlVC+E1c3gGiCHQc0 l4jiAYrAVWl7TP3MSFhU=; b=E5BykGjyMCtNsWQZJZqRlWMkluZBFfyP4tpAHl1lcCVJ0wx2pw I0nlPir4hT7n7G2VxUHQtJjYQzuxss6cIZDQ==; From: Bradley Morgan To: Andrew Morton , Vineet Gupta , Guo Ren , Yoshinori Sato , Rich Felker , Chris Zankel , Max Filippov Cc: Arnd Bergmann , "Paul E . McKenney" , David Laight , John Paul Adrian Glaubitz , linux-snps-arc@lists.infradead.org, linux-csky@vger.kernel.org, linux-sh@vger.kernel.org, linux-kernel@vger.kernel.org, Bradley Morgan Subject: [PATCH v3 5/5] xtensa: Emulate two-byte cmpxchg Date: Thu, 17 Sep 2026 16:38:30 +0000 Message-ID: <20260917163830.3748-6-brads@mainlining.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260917163830.3748-1-brads@mainlining.org> References: <20260917163830.3748-1-brads@mainlining.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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_ at the call move into the function and the macro's __typeof__(*(ptr)) typing of _o_ and _n_ remains the pointer-integer type check. Signed-off-by: Bradley Morgan --- arch/xtensa/include/asm/cmpxchg.h | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/xtensa/include/asm/cmpxchg.h b/arch/xtensa/include/asm/cm= pxchg.h index b6db4838b175..8dea8e357fc0 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; --=20 2.47.3