This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 7d01175b9cb3231c85e8a46bccd2fd3859ba0a1d Author: ouyangxiangzhen <[email protected]> AuthorDate: Tue Nov 3 10:04:33 2026 +0800 spinlock: make DSB before SEV arch-specific UP_SEV() is empty on RISC-V, x86_64, Xtensa and sim, but UP_DSB() is fence iorw,iorw on RISC-V and mfence on x86_64, so every spinlock release paid a full memory barrier that only the ARM WFE/SEV lock sequence needs. Move the DSB into the Arm UP_SEV() definitions and drop the unconditional UP_DSB() calls from the spinlock release paths. SEV is not a memory access and is not subject to memory ordering, so a waiter may observe the event before the lock release store becomes visible, re-read the stale lock and sleep again with the event already consumed (missed wakeup). DSB makes the store visible before SEV. From ARM Reference Manual DDI 0406C, "Use of WFE and SEV with Locks". Also unify the UP_SEV() definition: drop the per-profile overrides from the armv7-a/armv7-r/armv8-r barriers.h and provide it once under ARM_HAVE_WFE_SEV, which armv7-r and armv8-r now select. Assisted-By: mimo-v2.6-pro Signed-off-by: ouyangxiangzhen <[email protected]> --- arch/arm/Kconfig | 2 ++ arch/arm/include/arch.h | 17 ++++++++++++++++- arch/arm/include/armv7-a/barriers.h | 2 -- arch/arm/include/armv7-r/barriers.h | 2 -- arch/arm/include/armv8-r/barriers.h | 2 -- arch/arm64/include/arch.h | 19 ++++++++++++++++++- include/nuttx/spinlock.h | 4 ---- 7 files changed, 36 insertions(+), 12 deletions(-) diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index b29cce4cc74..e5eb807107c 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -1234,6 +1234,7 @@ config ARCH_ARMV7R select ARCH_HAVE_CPUINFO select ARCH_HAVE_PERF_EVENTS select ARCH_HAVE_PERF_EVENTS_USER_ACCESS + select ARM_HAVE_WFE_SEV config ARCH_CORTEXR4 bool @@ -1359,6 +1360,7 @@ config ARCH_ARMV8R select ONESHOT select ONESHOT_COUNT select ALARM_ARCH + select ARM_HAVE_WFE_SEV config ARCH_FAMILY diff --git a/arch/arm/include/arch.h b/arch/arm/include/arch.h index 3ca833fd4a7..23e347e65fc 100644 --- a/arch/arm/include/arch.h +++ b/arch/arm/include/arch.h @@ -157,7 +157,22 @@ static inline void up_fdpic_invoke(uintptr_t arg, uintptr_t entry, #ifdef CONFIG_ARM_HAVE_WFE_SEV # define UP_WFE() __asm__ __volatile__ ("wfe" : : : "memory") -# define UP_SEV() __asm__ __volatile__ ("sev" : : : "memory") + +/* SEV is not a memory access and is not subject to memory ordering, + * so a waiter may observe the event before the lock release store + * becomes visible. The waiter then re-reads the stale lock and sleeps + * again with the event already consumed (missed wakeup). DSB makes + * the store visible before SEV. From ARM Reference Manual DDI 0406C, + * "Use of WFE and SEV with Locks". + */ + +# define UP_SEV() \ + do \ + { \ + UP_DSB(); \ + __asm__ __volatile__ ("sev" : : : "memory"); \ + } \ + while (0) #endif /**************************************************************************** diff --git a/arch/arm/include/armv7-a/barriers.h b/arch/arm/include/armv7-a/barriers.h index 14f2288c784..e8f09721a33 100644 --- a/arch/arm/include/armv7-a/barriers.h +++ b/arch/arm/include/armv7-a/barriers.h @@ -37,7 +37,6 @@ #define arm_dmb(n) __asm__ __volatile__ ("dmb " #n : : : "memory") #define arm_isb() __asm__ __volatile__ ("isb " : : : "memory") #define arm_nop() __asm__ __volatile__ ("nop\n") -#define arm_sev() __asm__ __volatile__ ("sev\n") #define UP_DSB() arm_dsb(15) @@ -47,6 +46,5 @@ #define UP_ISB() arm_isb() #define UP_NOP() arm_nop() -#define UP_SEV() arm_sev() #endif /* __ARCH_ARM_INCLUDE_ARMV7_A_BARRIERS_H */ diff --git a/arch/arm/include/armv7-r/barriers.h b/arch/arm/include/armv7-r/barriers.h index 76f460bfce9..fd0d0d88217 100644 --- a/arch/arm/include/armv7-r/barriers.h +++ b/arch/arm/include/armv7-r/barriers.h @@ -39,7 +39,6 @@ #define arm_wmb() __asm__ __volatile__ ("dmb ishst" : : : "memory") #define arm_isb() __asm__ __volatile__ ("isb " : : : "memory") #define arm_nop() __asm__ __volatile__ ("nop\n") -#define arm_sev() __asm__ __volatile__ ("sev\n") #define UP_DSB() arm_dsb(15) @@ -49,6 +48,5 @@ #define UP_ISB() arm_isb() #define UP_NOP() arm_nop() -#define UP_SEV() arm_sev() #endif /* __ARCH_ARM_INCLUDE_ARMV7_R_BARRIERS_H */ diff --git a/arch/arm/include/armv8-r/barriers.h b/arch/arm/include/armv8-r/barriers.h index 39d2ff08f2a..036e3dc9b19 100644 --- a/arch/arm/include/armv8-r/barriers.h +++ b/arch/arm/include/armv8-r/barriers.h @@ -39,7 +39,6 @@ #define arm_wmb() __asm__ __volatile__ ("dmb ishst" : : : "memory") #define arm_isb() __asm__ __volatile__ ("isb " : : : "memory") #define arm_nop() __asm__ __volatile__ ("nop\n") -#define arm_sev() __asm__ __volatile__ ("sev\n") #define UP_DSB() arm_dsb(15) @@ -49,6 +48,5 @@ #define UP_ISB() arm_isb() #define UP_NOP() arm_nop() -#define UP_SEV() arm_sev() #endif /* __ARCH_ARM_INCLUDE_ARMV8_R_BARRIERS_H */ diff --git a/arch/arm64/include/arch.h b/arch/arm64/include/arch.h index 7340778dd58..22e6486d92a 100644 --- a/arch/arm64/include/arch.h +++ b/arch/arm64/include/arch.h @@ -55,7 +55,24 @@ #endif /* CONFIG_ARCH_ADDRENV */ #define UP_WFE() __asm__ __volatile__ ("wfe" : : : "memory") -#define UP_SEV() __asm__ __volatile__ ("sev" : : : "memory") + +/* SEV is not a memory access and is not subject to memory ordering, + * so a waiter may observe the event before the lock release store + * becomes visible, re-read the stale lock and sleep again with the + * event already consumed (missed wakeup). DSB makes the store visible + * before SEV. A waiter sleeping with an exclusive monitor armed is an + * exception: it wakes on the monitor clear from the store itself, so it + * needs neither SEV nor DSB. From ARM Reference Manual DDI 0406C, + * "Use of WFE and SEV with Locks". + */ + +#define UP_SEV() \ + do \ + { \ + UP_DSB(); \ + __asm__ __volatile__ ("sev" : : : "memory"); \ + } \ + while (0) /* The kernel keeps the address of the running thread's TLS info in * TPIDR_EL0, so user code finds it whatever stack it runs on. diff --git a/include/nuttx/spinlock.h b/include/nuttx/spinlock.h index 440b245edf5..c4bac30e2cf 100644 --- a/include/nuttx/spinlock.h +++ b/include/nuttx/spinlock.h @@ -338,7 +338,6 @@ spin_unlock_notrace(FAR volatile spinlock_t *lock) #else atomic_set_release((FAR atomic_t *)lock, SP_UNLOCKED); #endif - UP_DSB(); UP_SEV(); } #else @@ -924,7 +923,6 @@ bool rspin_unlock(FAR rspinlock_t *lock) if (RSPINLOCK_COUNT(val) == 1u) { atomic_set_release(&lock->val, 0u); - UP_DSB(); UP_SEV(); ret = true; } @@ -1139,7 +1137,6 @@ static inline_function void read_unlock(FAR volatile rwlock_t *lock) DEBUGASSERT(atomic_read(lock) >= RW_SP_READ_LOCKED); atomic_sub_release(lock, 1); - UP_DSB(); UP_SEV(); } @@ -1249,7 +1246,6 @@ static inline_function void write_unlock(FAR volatile rwlock_t *lock) DEBUGASSERT(atomic_read(lock) == RW_SP_WRITE_LOCKED); atomic_set_release(lock, RW_SP_UNLOCKED); - UP_DSB(); UP_SEV(); }
