Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20251030161720.96854-1-pincheng.plct@isrc.iscas.ac.cn>
Date: Fri, 31 Oct 2025 00:17:19 +0800
From: Pincheng Wang <pincheng.plct@...c.iscas.ac.cn>
To: musl@...ts.openwall.com
Cc: pincheng.plct@...c.iscas.ac.cn
Subject: [PATCH v2 0/1] riscv: Add support for Zacas in atomic

Hi all,

This patch adds support for the RISC-V Zacas (Atomic Compare-and-Swap)
extension in musl's atomic operations for both riscv64 and riscv32.

Changes from v1:
- Fixed #ifdef formatting; added missing newlines and removed redundant
  comments.
- Changed inline asm constraint from +A to match the earlier CAS
  implementation. The generated assembly is identical, and using r
  instead of +A avoids aliasing an lvalue onto memory.

Currently, musl implements a_cas using a Load-Reserved/Store-Conditional
(lr/sc) loop that:
- Requires at least four instructions (lr+bne+sc+bnez) per CAS
  operation.
- Contains a retry loop under contention.
- Incurs branch penalties that may cause pipeline stalls.

Zacas introduces amocas.w.aqrl/amocas.d.aqrl instructions that perform
CAS atomically in a single instruction, eliminating retry loops and
conditional branches.

Due to hardware limitations, we evaluated this change under QEMU using
both mcycle and minstret counters. The results show clear benefits:

Metric											              lr/sc	    Zacas	  Improvement
Instr. per CAS (50k ops average)				      15.04	    8.36	  -44.4%
Instr. per op (single-thread)					        23.61	    14.25	  -39.6%
Instr. per op (multi-thread, high contention)	528.24	  251.14	-52.5%

The patch automatically falls back to the lr/sc implementation on
systems where Zacas is not available, preserving full backward
compatibility.

The Zacas extension was ratified in November 2023. CPUs such as the
XuanTie C930 already support this extension. The recent RVA23 profile
document also listed Zacas as a development option and states that it
"is intended to become mandatory in the future RVA profile", suggesting
broader adoption.

Thanks,
Pincheng Wang

Pincheng Wang (1):
  riscv: add Zacas extension support for atomic CAS

 arch/riscv32/atomic_arch.h | 18 ++++++++++++++++++
 arch/riscv64/atomic_arch.h | 30 ++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)

-- 
2.39.5

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.