RFC sparc32: kernel assisted compare-and-swap, and futex on SMP
Get series with b4
b4 am [email protected]Versions newest first
- v1 RFC Latest5 patches 0 repliesNew
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/5 | sparc32: detect the compare-and-swap instruction at bootarch/ arch/ arch/ +3 more | +95 | 0 replies | |
| 2/5 | sparc32: add a kernel assisted compare-and-swaparch/ arch/ arch/ +4 more | +156 | 0 replies | |
| 3/5 | sparc32: implement futex atomic ops with the compare-and-swap locksarch/ init/ | +135 | 0 replies | |
| 4/5 | sparc32: advertise the compare-and-swap trap in AT_HWCAParch/ arch/ | +9 | 0 replies | |
| 5/5 | sparc32: document the compare-and-swap trap ABIDocumentation/ Documentation/ | +164 | 0 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.
Cover letter [RFC PATCH 0/5] sparc32: kernel assisted compare-and-swap, and futex on SMP
This series is part of my effort to get modern Linux kernels and a current glibc-based userland running on my SPARCstations. With the SILO and kernel fixes I've been working on, these machines can now boot recent kernels again. Booting the kernel is only part of the work,
though: running a current glibc-based userland also requires working futex support and userspace atomics that cooperate with it.
This series addresses the kernel side by providing a kernel-assisted 32-bit compare-and-swap operation and implementing the architecture futex atomic operations needed to enable futex support on sparc32 SMP. On machines without hardware compare-and-swap, the proposed userspace trap and the architecture futex helpers use the same kernel-owned locks, allowing their operations on a shared word to participate in the same serialization mechanism.
A companion glibc series addresses the userspace side, with the aim of restoring baseline SPARC V8 builds. It uses native compare-and-swap where available and the proposed kernel service otherwise, including for libc's internal atomic stores and exchanges so that they participate in the same protocol.
CONFIG_FUTEX depends on !(SPARC32 && SMP), so an SMP sparc32 kernel has no futex at all, and glibc has relied on futex for internal locking for about a decade: such a kernel cannot support a current glibc userland correctly. Arnd Bergmann observed the same in the 2023 "sunset sun4m and sun4d" thread [1], and on what a v8 versus a leon build actually uses [2].
The generic futex helpers must update a word in user memory atomically, and the baseline v8 instruction set lacks the general compare-and-swap operation that needs. A kernel-private lock does not fix it, the kernel does get_user() then put_user(), and userspace holds no such lock, so the mechanism has to be one both sides share. parisc does this with its LWS compare-and-swap, the model followed here.
The ground is not new. David Miller prototyped a sparc CAS trap in 2016 and Andreas Larsson proposed kernel-assisted atomics again in 2019 [3], including compiler builtins trapping into the kernel [4]. This is a new ABI along those lines rather than a completion of that one; patch 2 has the detail. Software trap 0x11 gives trap type 0x91, immediately following the Linux syscall trap at software trap 0x10, trap type 0x90. sparc64 compat mode is deliberately unsupported, since ta 0x11 is the old 64-bit system call trap there.
Patch 1 detects the v9 casa extension at boot. Patch 2 adds the trap - userspace executes ta 0x11, since sparc adds 0x80 to form the type. Patch 3 puts the futex helpers on it and drops the Kconfig dependency, 4 advertises it in AT_HWCAP, 5 documents the ABI.
Patch 5 documents the limits, which are worth reading first. On a cpu without casa, storage that participates in this protocol must have every atomic write built from the trap, because an ordinary store does not take the kernel's lock; that is Andreas Larsson's 2016 point, and why parisc ships a kernel-assisted atomic store beside its LWS CAS. Replacing the __atomic_* symbols is not sufficient, since gcc can expand some of them inline on v8 - and covering libc's own uses is a different scope from covering application code built with atomic builtins. One aligned 32-bit word is also not general byte and halfword atomics. So this is a kernel-assisted 32-bit compare-and-swap enabling futex for cooperating userspace, not arbitrary sparcv8 userland support.
One kernel path is worth raising. futex_robust_unlock() clears the futex word with unsafe_atomic_store_release_user(), before the hash bucket is locked and not through either architecture helper. SPARC32 uses the generic definition of that macro, which ends in a plain user store, so on a no-CASA SMP system it does not acquire the lock the software CAS uses. It can land between that CAS's load and its conditional store, letting the CAS overwrite the unlock and still report success. Dropping the Kconfig dependency is what exposes this on SPARC32 SMP, and it has not been observed in practice.
The requirement is only that the two agree on how they serialise access to the word. The macro is overridable, so SPARC32 could supply its own definition and keep this within the architecture; whether that is preferable to a futex-specific interface is the question I would like an answer to.
Choosing between lock and instruction follows the hardware, not a build option, because userspace's choice does too:
hardware trap-aware userland userland uses kernel uses no casa v8 trap 0x11 lock array casa leon3 casa casa casa v8 trap 0x11 casa
Row three is why the trap handler uses casa where it exists, and why the futex read-modify-write is a casa retry loop rather than a locked read and
write: a binary using casa inline holds no lock of ours, so between our two accesses its update can be lost while both appear to succeed.
Applies and builds standalone on v7.3-rc1, cross-built for the SPARCstation 20, no new warnings. Boot-tested only; the trap itself has not been issued by a test, and the concurrency, fault-path and cross-process cases have no runtime results yet. The casa path has never executed here, this hardware has no casa.
Boot testing on my SPARCstation 20 used these prerequisite series, since that machine does not boot current mainline without them:
sparc32: relocatable kernel / phys_base + Viking fixes
https://lore.kernel.org/sparclinux/[email protected]/T/#t sparc32: replace sp_banks with memblock (v3) https://lore.kernel.org/sparclinux/[email protected]/ sparc32: SuperSPARC SMP synchronization fixes (v2) https://lore.kernel.org/sparclinux/[email protected]/
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://inbox.sourceware.org/libc-alpha/[email protected]/
[4] https://inbox.sourceware.org/libc-alpha/[email protected]/
Magnus Lindholm (5):
sparc32: detect the compare-and-swap instruction at boot
sparc32: add a kernel assisted compare-and-swap
sparc32: implement futex atomic ops with the compare-and-swap locks
sparc32: advertise the compare-and-swap trap in AT_HWCAP
sparc32: document the compare-and-swap trap ABI
Documentation/arch/sparc/cas-trap.rst | 163 ++++++++++++++++++++++++++ Documentation/arch/sparc/index.rst | 1 + arch/sparc/include/asm/cas_32.h | 33 ++++++ arch/sparc/include/asm/cpu_type.h | 7 ++ arch/sparc/include/asm/elf_32.h | 7 +- arch/sparc/include/asm/elf_64.h | 3 + arch/sparc/include/asm/futex_32.h | 137 +++++++++++++++++++++- arch/sparc/include/uapi/asm/traps.h | 3 +- arch/sparc/kernel/Makefile | 1 + arch/sparc/kernel/cas_32.c | 96 +++++++++++++++ arch/sparc/kernel/cpu.c | 24 ++++ arch/sparc/kernel/entry.S | 17 +++ arch/sparc/kernel/traps_32.c | 16 ++- arch/sparc/kernel/ttable_32.S | 9 +- arch/sparc/lib/Makefile | 5 + arch/sparc/lib/casa_32.S | 46 ++++++++ init/Kconfig | 1 - 17 files changed, 559 insertions(+), 10 deletions(-) create mode 100644 Documentation/arch/sparc/cas-trap.rst create mode 100644 arch/sparc/include/asm/cas_32.h create mode 100644 arch/sparc/kernel/cas_32.c create mode 100644 arch/sparc/lib/casa_32.S
base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
Signature · 1 line
2.43.0