Skip to contentSkip to conversation

eal/riscv: add support for zawrs extension

RFC PATCH In review Daniel Gregory 4 participants 7 messages May 2, 2024 – Nov 8

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 eal/riscv: add support for zawrs extension

The zawrs extension adds a pair of instructions that stall a core until a memory location is written to. This patch uses one of them to implement RISCV-specific versions of the rte_wait_until_equal_* functions. This is potentially more energy efficient than the default implementation that uses rte_pause/Zihintpause.

The technique works as follows:

* Create a reservation set containing the address we want to wait on using an atomic load (lr.dw)
* Call wrs.nto - this blocks until the reservation set is invalidated by someone else writing to that address
* Execution can also resume arbitrarily, so we still need to check whether a change occurred and loop if not

Due to RISC-V atomics only supporting naturally aligned word (32 bit) and double word (64 bit) loads, I've used pointer rounding and bit shifting to implement waiting on 16-bit values.

This new functionality is controlled by a Meson flag that is disabled by default.

---

Posting as an RFC to get early feedback and enable testing by others with Zawrs-enabled hardware. Whilst I have been able to test it compiles & passes tests using QEMU, I am waiting on some Zawrs-enabled hardware to become available before I carry out performance tests.

Nonetheless, I would be glad to hear any feedback on the general approach. Thanks, Daniel

 config/riscv/meson.build          |   5 ++
 lib/eal/riscv/include/rte_pause.h | 139 ++++++++++++++++++++++++++++++
 2 files changed, 144 insertions(+)
config/riscv/meson.build +5 -0
index 07d7d9da23..4cfdc42ecb 100644
@@ -26,6 +26,11 @@ flags_common = [
     # read from /proc/device-tree/cpus/timebase-frequency. This property is
     # guaranteed on Linux, as riscv time_init() requires it.
     ['RTE_RISCV_TIME_FREQ', 0],
+
+    # Enable use of RISC-V Wait-on-Reservation-Set extension (Zawrs)
+    # Mitigates looping when polling on memory locations
+    # Make sure to add '_zawrs' to your target's -march below
+    ['RTE_RISCV_ZAWRS', false]
 ]
 
 ## SoC-specific options.
lib/eal/riscv/include/rte_pause.h +139 -0

151 lines not on this page · raw message

Signature · 1 line
2.39.2
#1 lore raw

I'm terribly sorry for the delay in review. I'll try to get to it in the next 2 days.

Quoted 215 lines · Daniel Gregory

The zawrs extension adds a pair of instructions that stall a core until a memory location is written to. This patch uses one of them to implement RISCV-specific versions of the rte_wait_until_equal_* functions. This is potentially more energy efficient than the default implementation that uses rte_pause/Zihintpause.

The technique works as follows:

* Create a reservation set containing the address we want to wait on using an atomic load (lr.dw)
* Call wrs.nto - this blocks until the reservation set is invalidated by someone else writing to that address
* Execution can also resume arbitrarily, so we still need to check whether a change occurred and loop if not

Due to RISC-V atomics only supporting naturally aligned word (32 bit) and double word (64 bit) loads, I've used pointer rounding and bit shifting to implement waiting on 16-bit values.

This new functionality is controlled by a Meson flag that is disabled by default.

---

Posting as an RFC to get early feedback and enable testing by others with Zawrs-enabled hardware. Whilst I have been able to test it compiles & passes tests using QEMU, I am waiting on some Zawrs-enabled hardware to become available before I carry out performance tests.

Nonetheless, I would be glad to hear any feedback on the general approach. Thanks, Daniel

 config/riscv/meson.build          |   5 ++
 lib/eal/riscv/include/rte_pause.h | 139 ++++++++++++++++++++++++++++++
 2 files changed, 144 insertions(+)
config/riscv/meson.build +5 -0
index 07d7d9da23..4cfdc42ecb 100644
@@ -26,6 +26,11 @@ flags_common = [
     # read from /proc/device-tree/cpus/timebase-frequency. This property is
     # guaranteed on Linux, as riscv time_init() requires it.
     ['RTE_RISCV_TIME_FREQ', 0],
+
+    # Enable use of RISC-V Wait-on-Reservation-Set extension (Zawrs)
+    # Mitigates looping when polling on memory locations
+    # Make sure to add '_zawrs' to your target's -march below
+    ['RTE_RISCV_ZAWRS', false]
 ]
 
 ## SoC-specific options.
lib/eal/riscv/include/rte_pause.h +139 -1
index cb8e9ca52d..e7b43dffa3 100644
@@ -11,6 +11,12 @@
 extern "C" {
 #endif
 
+#ifdef RTE_RISCV_ZAWRS
+#define RTE_WAIT_UNTIL_EQUAL_ARCH_DEFINED
+#endif
+
+#include <rte_debug.h>
+
 #include "rte_atomic.h"
 
 #include "generic/rte_pause.h"
@@ -24,6 +30,139 @@ static inline void rte_pause(void)
        asm volatile(".int 0x0100000F" : : : "memory");
 }
 
+#ifdef RTE_RISCV_ZAWRS
+
+/*
+ * Atomic load from an address, it returns either a sign-extended word or
+ * doubleword and creates a 'reservation set' containing the read memory
+ * location. When someone else writes to the reservation set, it is invalidated,
+ * causing any stalled WRS instructions to resume.
+ *
+ * Address needs to be naturally aligned.
+ */
+#define __RTE_RISCV_LR_32(src, dst, memorder) do {                \
+       if ((memorder) == rte_memory_order_relaxed) {             \
+               asm volatile("lr.w %0, (%1)"                      \
+                               : "=r" (dst)                      \
+                               : "r" (src)                       \
+                               : "memory");                      \
+       } else {                                                  \
+               asm volatile("lr.w.aq %0, (%1)"                   \
+                               : "=r" (dst)                      \
+                               : "r" (src)                       \
+                               : "memory");                      \
+       } } while (0)
+#define __RTE_RISCV_LR_64(src, dst, memorder) do {                \
+       if ((memorder) == rte_memory_order_relaxed) {             \
+               asm volatile("lr.d %0, (%1)"                      \
+                               : "=r" (dst)                      \
+                               : "r" (src)                       \
+                               : "memory");                      \
+       } else {                                                  \
+               asm volatile("lr.d.aq %0, (%1)"                   \
+                               : "=r" (dst)                      \
+                               : "r" (src)                       \
+                               : "memory");                      \
+       } } while (0)
+
+/*
+ * There's not a RISC-V atomic load primitive for halfwords, so cast up to a
+ * _naturally aligned_ word and extract the halfword we want
+ */
+#define __RTE_RISCV_LR_16(src, dst, memorder) do {                      \
+       uint32_t word;                                                  \
+       __RTE_RISCV_LR_32(((uintptr_t)(src) & (~3)), word, (memorder)); \
+       if ((size_t)(src) & 3)                                          \
+               (dst) = (typeof(dst))(word >> 16);                      \
+       else                                                            \
+               (dst) = (typeof(dst))(word & 0xFFFF);                   \
+} while (0)
+
+#define __RTE_RISCV_LR(src, dst, memorder, size) {                \
+       RTE_BUILD_BUG_ON(size != 16 && size != 32 && size != 64); \
+       if (size == 16)                                           \
+               __RTE_RISCV_LR_16(src, dst, memorder);            \
+       else if (size == 32)                                      \
+               __RTE_RISCV_LR_32(src, dst, memorder);            \
+       else if (size == 64)                                      \
+               __RTE_RISCV_LR_64(src, dst, memorder);            \
+}
+
+/*
+ * Wait-on-Reservation-Set extension instruction, it stalls execution until the
+ * reservation set is invalidated or an interrupt is observed.
+ * A loop is likely still needed as it may stop stalling arbitrarily.
+ */
+#define __RTE_RISCV_WRS_NTO() { asm volatile("wrs.nto" : : : "memory"); }
+
+static __rte_always_inline void
+rte_wait_until_equal_16(volatile uint16_t *addr, uint16_t expected,
+               int memorder)
+{
+       uint16_t value;
+
+       RTE_ASSERT(memorder == rte_memory_order_acquire ||
+               memorder == rte_memory_order_relaxed);
+       RTE_ASSERT(((size_t)addr & 1) == 0);
+
+       __RTE_RISCV_LR_16(addr, value, memorder);
+       while (value != expected) {
+               __RTE_RISCV_WRS_NTO();
+               __RTE_RISCV_LR_16(addr, value, memorder);
+       }
+}
+
+static __rte_always_inline void
+rte_wait_until_equal_32(volatile uint32_t *addr, uint32_t expected,
+               int memorder)
+{
+       uint32_t value;
+
+       RTE_ASSERT(memorder == rte_memory_order_acquire ||
+               memorder == rte_memory_order_relaxed);
+       RTE_ASSERT(((size_t)addr & 3) == 0);
+
+       __RTE_RISCV_LR_32(addr, value, memorder);
+       while (value != expected) {
+               __RTE_RISCV_WRS_NTO();
+               __RTE_RISCV_LR_32(addr, value, memorder);
+       }
+}
+
+static __rte_always_inline void
+rte_wait_until_equal_64(volatile uint64_t *addr, uint64_t expected,
+               int memorder)
+{
+       uint64_t value;
+
+       RTE_ASSERT(memorder == rte_memory_order_acquire ||
+               memorder == rte_memory_order_relaxed);
+       RTE_ASSERT(((size_t)addr & 7) == 0);
+
+       __RTE_RISCV_LR_64(addr, value, memorder);
+       while (value != expected) {
+               __RTE_RISCV_WRS_NTO();
+               __RTE_RISCV_LR_64(addr, value, memorder);
+       }
+}
+
+#define RTE_WAIT_UNTIL_MASKED(addr, mask, cond, expected, memorder) do { \
+       RTE_BUILD_BUG_ON(!__builtin_constant_p(memorder));               \
+       RTE_BUILD_BUG_ON(memorder != rte_memory_order_acquire &&         \
+               memorder != rte_memory_order_relaxed);                   \
+       RTE_ASSERT(((size_t)(addr) & (sizeof(*(addr)) - 1)) != 0);       \
+       const uint32_t size = sizeof(*(addr)) << 3;                      \
+       typeof(*(addr)) expected_value = (expected);                     \
+       typeof(*(addr)) value;                                           \
+       __RTE_RISCV_LR((addr), value, memorder, size);                   \
+       while (!((value & (mask)) cond expected_value)) {                \
+               __RTE_RISCV_WRS_NTO();                                   \
+               __RTE_RISCV_LR((addr), value, memorder, size);           \
+       }                                                                \
+} while (0)
+
+#endif /* RTE_RISCV_ZAWRS */
+
 #ifdef __cplusplus
 }
 #endif
--

2.39.2

Reviewed-by on v1 1/1
#2 lore raw
In reply to Daniel Gregory
Quoted 44 lines · Daniel Gregory

The zawrs extension adds a pair of instructions that stall a core until a memory location is written to. This patch uses one of them to implement RISCV-specific versions of the rte_wait_until_equal_* functions. This is potentially more energy efficient than the default implementation that uses rte_pause/Zihintpause.

The technique works as follows:

* Create a reservation set containing the address we want to wait on using an atomic load (lr.dw)
* Call wrs.nto - this blocks until the reservation set is invalidated by someone else writing to that address
* Execution can also resume arbitrarily, so we still need to check whether a change occurred and loop if not

Due to RISC-V atomics only supporting naturally aligned word (32 bit) and double word (64 bit) loads, I've used pointer rounding and bit shifting to implement waiting on 16-bit values.

This new functionality is controlled by a Meson flag that is disabled by default.

---

Posting as an RFC to get early feedback and enable testing by others with Zawrs-enabled hardware. Whilst I have been able to test it compiles & passes tests using QEMU, I am waiting on some Zawrs-enabled hardware to become available before I carry out performance tests.

Nonetheless, I would be glad to hear any feedback on the general approach. Thanks, Daniel

 config/riscv/meson.build          |   5 ++
 lib/eal/riscv/include/rte_pause.h | 139 ++++++++++++++++++++++++++++++
 2 files changed, 144 insertions(+)
config/riscv/meson.build +0 -0
index 07d7d9da23..4cfdc42ecb 100644

config/riscv/meson.build flags_common = [ · line 33

Quoted 8 lines
config/riscv/meson.build +5 -0
@@ -26,6 +26,11 @@ flags_common = [
     # read from /proc/device-tree/cpus/timebase-frequency. This property is
     # guaranteed on Linux, as riscv time_init() requires it.
     ['RTE_RISCV_TIME_FREQ', 0],
+
+    # Enable use of RISC-V Wait-on-Reservation-Set extension (Zawrs)
+    # Mitigates looping when polling on memory locations
+    # Make sure to add '_zawrs' to your target's -march below
+    ['RTE_RISCV_ZAWRS', false]

A bit orthogonal to this patch (or maybe not?)
Should we perhaps add a Qemu target in meson.build which would have the modified -march for what qemu supports now?
Or perhaps add machine detection logic based either on the "riscv,isa" cpu@0 property in the DT or RHCT ACPI table?
Or add perhaps some other way we could specify the extension list suffix for -march?

Quoted 150 lines
config/riscv/meson.build +0 -0
 ]
 
 ## SoC-specific options.
lib/eal/riscv/include/rte_pause.h +132 -0
index cb8e9ca52d..e7b43dffa3 100644
@@ -11,6 +11,12 @@
 extern "C" {
 #endif
 
+#ifdef RTE_RISCV_ZAWRS
+#define RTE_WAIT_UNTIL_EQUAL_ARCH_DEFINED
+#endif
+
+#include <rte_debug.h>
+
 #include "rte_atomic.h"
 
 #include "generic/rte_pause.h"
@@ -24,6 +30,139 @@ static inline void rte_pause(void)
        asm volatile(".int 0x0100000F" : : : "memory");
 }
 
+#ifdef RTE_RISCV_ZAWRS
+
+/*
+ * Atomic load from an address, it returns either a sign-extended word or
+ * doubleword and creates a 'reservation set' containing the read memory
+ * location. When someone else writes to the reservation set, it is invalidated,
+ * causing any stalled WRS instructions to resume.
+ *
+ * Address needs to be naturally aligned.
+ */
+#define __RTE_RISCV_LR_32(src, dst, memorder) do {                \
+       if ((memorder) == rte_memory_order_relaxed) {             \
+               asm volatile("lr.w %0, (%1)"                      \
+                               : "=r" (dst)                      \
+                               : "r" (src)                       \
+                               : "memory");                      \
+       } else {                                                  \
+               asm volatile("lr.w.aq %0, (%1)"                   \
+                               : "=r" (dst)                      \
+                               : "r" (src)                       \
+                               : "memory");                      \
+       } } while (0)
+#define __RTE_RISCV_LR_64(src, dst, memorder) do {                \
+       if ((memorder) == rte_memory_order_relaxed) {             \
+               asm volatile("lr.d %0, (%1)"                      \
+                               : "=r" (dst)                      \
+                               : "r" (src)                       \
+                               : "memory");                      \
+       } else {                                                  \
+               asm volatile("lr.d.aq %0, (%1)"                   \
+                               : "=r" (dst)                      \
+                               : "r" (src)                       \
+                               : "memory");                      \
+       } } while (0)
+
+/*
+ * There's not a RISC-V atomic load primitive for halfwords, so cast up to a
+ * _naturally aligned_ word and extract the halfword we want
+ */
+#define __RTE_RISCV_LR_16(src, dst, memorder) do {                      \
+       uint32_t word;                                                  \
+       __RTE_RISCV_LR_32(((uintptr_t)(src) & (~3)), word, (memorder)); \
+       if ((size_t)(src) & 3)                                          \
+               (dst) = (typeof(dst))(word >> 16);                      \
+       else                                                            \
+               (dst) = (typeof(dst))(word & 0xFFFF);                   \
+} while (0)
+
+#define __RTE_RISCV_LR(src, dst, memorder, size) {                \
+       RTE_BUILD_BUG_ON(size != 16 && size != 32 && size != 64); \
+       if (size == 16)                                           \
+               __RTE_RISCV_LR_16(src, dst, memorder);            \
+       else if (size == 32)                                      \
+               __RTE_RISCV_LR_32(src, dst, memorder);            \
+       else if (size == 64)                                      \
+               __RTE_RISCV_LR_64(src, dst, memorder);            \
+}
+
+/*
+ * Wait-on-Reservation-Set extension instruction, it stalls execution until the
+ * reservation set is invalidated or an interrupt is observed.
+ * A loop is likely still needed as it may stop stalling arbitrarily.
+ */
+#define __RTE_RISCV_WRS_NTO() { asm volatile("wrs.nto" : : : "memory"); }
+
+static __rte_always_inline void
+rte_wait_until_equal_16(volatile uint16_t *addr, uint16_t expected,
+               int memorder)
+{
+       uint16_t value;
+
+       RTE_ASSERT(memorder == rte_memory_order_acquire ||
+               memorder == rte_memory_order_relaxed);
+       RTE_ASSERT(((size_t)addr & 1) == 0);
+
+       __RTE_RISCV_LR_16(addr, value, memorder);
+       while (value != expected) {
+               __RTE_RISCV_WRS_NTO();
+               __RTE_RISCV_LR_16(addr, value, memorder);
+       }
+}
+
+static __rte_always_inline void
+rte_wait_until_equal_32(volatile uint32_t *addr, uint32_t expected,
+               int memorder)
+{
+       uint32_t value;
+
+       RTE_ASSERT(memorder == rte_memory_order_acquire ||
+               memorder == rte_memory_order_relaxed);
+       RTE_ASSERT(((size_t)addr & 3) == 0);
+
+       __RTE_RISCV_LR_32(addr, value, memorder);
+       while (value != expected) {
+               __RTE_RISCV_WRS_NTO();
+               __RTE_RISCV_LR_32(addr, value, memorder);
+       }
+}
+
+static __rte_always_inline void
+rte_wait_until_equal_64(volatile uint64_t *addr, uint64_t expected,
+               int memorder)
+{
+       uint64_t value;
+
+       RTE_ASSERT(memorder == rte_memory_order_acquire ||
+               memorder == rte_memory_order_relaxed);
+       RTE_ASSERT(((size_t)addr & 7) == 0);
+
+       __RTE_RISCV_LR_64(addr, value, memorder);
+       while (value != expected) {
+               __RTE_RISCV_WRS_NTO();
+               __RTE_RISCV_LR_64(addr, value, memorder);
+       }
+}
+
+#define RTE_WAIT_UNTIL_MASKED(addr, mask, cond, expected, memorder) do { \
+       RTE_BUILD_BUG_ON(!__builtin_constant_p(memorder));               \
+       RTE_BUILD_BUG_ON(memorder != rte_memory_order_acquire &&         \
+               memorder != rte_memory_order_relaxed);                   \
+       RTE_ASSERT(((size_t)(addr) & (sizeof(*(addr)) - 1)) != 0);       \
+       const uint32_t size = sizeof(*(addr)) << 3;                      \
+       typeof(*(addr)) expected_value = (expected);                     \
+       typeof(*(addr)) value;                                           \
+       __RTE_RISCV_LR((addr), value, memorder, size);                   \
+       while (!((value & (mask)) cond expected_value)) {                \

lib/eal/riscv/include/rte_pause.h static inline void rte_pause(void) · line 168

Quoted 10 lines
lib/eal/riscv/include/rte_pause.h +7 -0
+               __RTE_RISCV_WRS_NTO();                                   \
+               __RTE_RISCV_LR((addr), value, memorder, size);           \
+       }                                                                \
+} while (0)
+
+#endif /* RTE_RISCV_ZAWRS */
+
 #ifdef __cplusplus
 }
 #endif
Quoted 167 lines

--
2.39.2

]

## SoC-specific options.

lib/eal/riscv/include/rte_pause.h +139 -1
index cb8e9ca52d..e7b43dffa3 100644
@@ -11,6 +11,12 @@
 extern "C" {
 #endif
 
+#ifdef RTE_RISCV_ZAWRS
+#define RTE_WAIT_UNTIL_EQUAL_ARCH_DEFINED
+#endif
+
+#include <rte_debug.h>
+
 #include "rte_atomic.h"
 
 #include "generic/rte_pause.h"
@@ -24,6 +30,139 @@ static inline void rte_pause(void)
        asm volatile(".int 0x0100000F" : : : "memory");
 }
 
+#ifdef RTE_RISCV_ZAWRS
+
+/*
+ * Atomic load from an address, it returns either a sign-extended word or
+ * doubleword and creates a 'reservation set' containing the read memory
+ * location. When someone else writes to the reservation set, it is invalidated,
+ * causing any stalled WRS instructions to resume.
+ *
+ * Address needs to be naturally aligned.
+ */
+#define __RTE_RISCV_LR_32(src, dst, memorder) do {                \
+       if ((memorder) == rte_memory_order_relaxed) {             \
+               asm volatile("lr.w %0, (%1)"                      \
+                               : "=r" (dst)                      \
+                               : "r" (src)                       \
+                               : "memory");                      \
+       } else {                                                  \
+               asm volatile("lr.w.aq %0, (%1)"                   \
+                               : "=r" (dst)                      \
+                               : "r" (src)                       \
+                               : "memory");                      \
+       } } while (0)
+#define __RTE_RISCV_LR_64(src, dst, memorder) do {                \
+       if ((memorder) == rte_memory_order_relaxed) {             \
+               asm volatile("lr.d %0, (%1)"                      \
+                               : "=r" (dst)                      \
+                               : "r" (src)                       \
+                               : "memory");                      \
+       } else {                                                  \
+               asm volatile("lr.d.aq %0, (%1)"                   \
+                               : "=r" (dst)                      \
+                               : "r" (src)                       \
+                               : "memory");                      \
+       } } while (0)
+
+/*
+ * There's not a RISC-V atomic load primitive for halfwords, so cast up to a
+ * _naturally aligned_ word and extract the halfword we want
+ */
+#define __RTE_RISCV_LR_16(src, dst, memorder) do {                      \
+       uint32_t word;                                                  \
+       __RTE_RISCV_LR_32(((uintptr_t)(src) & (~3)), word, (memorder)); \
+       if ((size_t)(src) & 3)                                          \
+               (dst) = (typeof(dst))(word >> 16);                      \
+       else                                                            \
+               (dst) = (typeof(dst))(word & 0xFFFF);                   \
+} while (0)
+
+#define __RTE_RISCV_LR(src, dst, memorder, size) {                \
+       RTE_BUILD_BUG_ON(size != 16 && size != 32 && size != 64); \
+       if (size == 16)                                           \
+               __RTE_RISCV_LR_16(src, dst, memorder);            \
+       else if (size == 32)                                      \
+               __RTE_RISCV_LR_32(src, dst, memorder);            \
+       else if (size == 64)                                      \
+               __RTE_RISCV_LR_64(src, dst, memorder);            \
+}
+
+/*
+ * Wait-on-Reservation-Set extension instruction, it stalls execution until the
+ * reservation set is invalidated or an interrupt is observed.
+ * A loop is likely still needed as it may stop stalling arbitrarily.
+ */
+#define __RTE_RISCV_WRS_NTO() { asm volatile("wrs.nto" : : : "memory"); }
+
+static __rte_always_inline void
+rte_wait_until_equal_16(volatile uint16_t *addr, uint16_t expected,
+               int memorder)
+{
+       uint16_t value;
+
+       RTE_ASSERT(memorder == rte_memory_order_acquire ||
+               memorder == rte_memory_order_relaxed);
+       RTE_ASSERT(((size_t)addr & 1) == 0);
+
+       __RTE_RISCV_LR_16(addr, value, memorder);
+       while (value != expected) {
+               __RTE_RISCV_WRS_NTO();
+               __RTE_RISCV_LR_16(addr, value, memorder);
+       }
+}
+
+static __rte_always_inline void
+rte_wait_until_equal_32(volatile uint32_t *addr, uint32_t expected,
+               int memorder)
+{
+       uint32_t value;
+
+       RTE_ASSERT(memorder == rte_memory_order_acquire ||
+               memorder == rte_memory_order_relaxed);
+       RTE_ASSERT(((size_t)addr & 3) == 0);
+
+       __RTE_RISCV_LR_32(addr, value, memorder);
+       while (value != expected) {
+               __RTE_RISCV_WRS_NTO();
+               __RTE_RISCV_LR_32(addr, value, memorder);
+       }
+}
+
+static __rte_always_inline void
+rte_wait_until_equal_64(volatile uint64_t *addr, uint64_t expected,
+               int memorder)
+{
+       uint64_t value;
+
+       RTE_ASSERT(memorder == rte_memory_order_acquire ||
+               memorder == rte_memory_order_relaxed);
+       RTE_ASSERT(((size_t)addr & 7) == 0);
+
+       __RTE_RISCV_LR_64(addr, value, memorder);
+       while (value != expected) {
+               __RTE_RISCV_WRS_NTO();
+               __RTE_RISCV_LR_64(addr, value, memorder);
+       }
+}
+
+#define RTE_WAIT_UNTIL_MASKED(addr, mask, cond, expected, memorder) do { \
+       RTE_BUILD_BUG_ON(!__builtin_constant_p(memorder));               \
+       RTE_BUILD_BUG_ON(memorder != rte_memory_order_acquire &&         \
+               memorder != rte_memory_order_relaxed);                   \
+       RTE_ASSERT(((size_t)(addr) & (sizeof(*(addr)) - 1)) != 0);       \
+       const uint32_t size = sizeof(*(addr)) << 3;                      \
+       typeof(*(addr)) expected_value = (expected);                     \
+       typeof(*(addr)) value;                                           \
+       __RTE_RISCV_LR((addr), value, memorder, size);                   \
+       while (!((value & (mask)) cond expected_value)) {                \
+               __RTE_RISCV_WRS_NTO();                                   \
+               __RTE_RISCV_LR((addr), value, memorder, size);           \
+       }                                                                \
+} while (0)
+
+#endif /* RTE_RISCV_ZAWRS */
+
 #ifdef __cplusplus
 }
 #endif
--

2.39.2

#3 lore raw
Quoted 18 lines · Stanisław Kardach

config/riscv/meson.build flags_common = [ · line 33

Quoted 13 lines · Daniel Gregory
config/riscv/meson.build +5 -0
index 07d7d9da23..4cfdc42ecb 100644
@@ -26,6 +26,11 @@ flags_common = [
     # read from /proc/device-tree/cpus/timebase-frequency. This property is
     # guaranteed on Linux, as riscv time_init() requires it.
     ['RTE_RISCV_TIME_FREQ', 0],
+
+    # Enable use of RISC-V Wait-on-Reservation-Set extension (Zawrs)
+    # Mitigates looping when polling on memory locations
+    # Make sure to add '_zawrs' to your target's -march below
+    ['RTE_RISCV_ZAWRS', false]

A bit orthogonal to this patch (or maybe not?)
Should we perhaps add a Qemu target in meson.build which would have the modified -march for what qemu supports now?

Yes, I can see that being worth doing as part of this patch. In addition to Zawrs for this patch, GCC 13+ should generate prefetch instructions for __builtin_prefetch() (lib/eal/include/generic/rte_prefetch.h) if the Zicbop extension is enabled. Any more in particular you think would benefit or would it be best to add every extension GCC 14 supports?

Quoted 2 lines

Or perhaps add machine detection logic based either on the "riscv,isa" cpu@0 property in the DT or RHCT ACPI table?

I have had a look and, at least on QEMU 9, this seems non-trivial. The RHCT acpi table at /proc/cpuinfo doesn't list every extension present (eg. it's missing Zawrs), and the DT, whilst complete, can't be fed directly into GCC because QEMU reports several newer and non-ratified extensions that GCC doesn't support yet.

Quoted 2 lines

Or add perhaps some other way we could specify the extension list suffix for -march?

Setting -Dcpu_instruction_set to some arbitrary ISA could work with somes minor changes to the build script to not discard it in favour of rv64gc. Then, we could add a map from ISA extensions to flags that are enabled when that extension is present in cpu_instruction_set?

Thanks for your review,
Daniel

#4 lore raw
In reply to Stanisław Kardach
Quoted 51 lines · Stanisław Kardach
Quoted 49 lines · Daniel Gregory

The zawrs extension adds a pair of instructions that stall a core until a memory location is written to. This patch uses one of them to implement RISCV-specific versions of the rte_wait_until_equal_* functions. This is potentially more energy efficient than the default implementation that uses rte_pause/Zihintpause.

The technique works as follows:

* Create a reservation set containing the address we want to wait on using an atomic load (lr.dw)
* Call wrs.nto - this blocks until the reservation set is invalidated by someone else writing to that address
* Execution can also resume arbitrarily, so we still need to check whether a change occurred and loop if not

Due to RISC-V atomics only supporting naturally aligned word (32 bit) and double word (64 bit) loads, I've used pointer rounding and bit shifting to implement waiting on 16-bit values.

This new functionality is controlled by a Meson flag that is disabled by default.

---

Posting as an RFC to get early feedback and enable testing by others with Zawrs-enabled hardware. Whilst I have been able to test it compiles & passes tests using QEMU, I am waiting on some Zawrs-enabled hardware to become available before I carry out performance tests.

Nonetheless, I would be glad to hear any feedback on the general approach. Thanks, Daniel

 config/riscv/meson.build          |   5 ++
 lib/eal/riscv/include/rte_pause.h | 139 ++++++++++++++++++++++++++++++
 2 files changed, 144 insertions(+)
config/riscv/meson.build +2 -0
index 07d7d9da23..4cfdc42ecb 100644
@@ -26,6 +26,11 @@ flags_common = [
     # read from /proc/device-tree/cpus/timebase-frequency. This property is
     # guaranteed on Linux, as riscv time_init() requires it.
     ['RTE_RISCV_TIME_FREQ', 0],
+
+    # Enable use of RISC-V Wait-on-Reservation-Set extension (Zawrs)
Quoted 8 lines

config/riscv/meson.build flags_common = [ · line 33

Quoted 3 lines
config/riscv/meson.build +3 -0
+    # Mitigates looping when polling on memory locations
+    # Make sure to add '_zawrs' to your target's -march below
+    ['RTE_RISCV_ZAWRS', false]

A bit orthogonal to this patch (or maybe not?)
Should we perhaps add a Qemu target in meson.build which would have the modified -march for what qemu supports now?
Or perhaps add machine detection logic based either on the "riscv,isa" cpu@0 property in the DT or RHCT ACPI table?

Compile time feature detection doesn't add a lot of benefit - it doesn't work in cross builds environments - which is the common way things are built for RISC-V at the moment. Also it doesn't work for distros where a single build is used across a broad set of machines.

Quoted 2 lines

Or add perhaps some other way we could specify the extension list suffix for -march?

Making it easier to specify the required extensions during the build does make sense. Though this is an orthogonal change and is better done in follow-on patches.

[...]

#5 lore raw
In reply to Daniel Gregory

Hi Daniel,

This RFC has been sitting for a while. I tested it on an RVA23 board (SpacemiT K3, Zawrs present) and it works: over a 200 ms wait, wrs.nto woke up 53 times vs 14.6M spins with pause, hand-off latency is the same, and the lock/ring/atomic autotests pass.

To move it forward it needs:

- rebase
- build-time detection via __riscv_zawrs, like the V extension in config/riscv/meson.build, instead of the manual meson flag; this also answers Stanisław's question about extension detection
- int memorder -> rte_memory_order memorder
- RTE_WAIT_UNTIL_MASKED: the alignment assert is inverted (!= 0 -> == 0)
- do a plain load before taking the reservation; always starting with lr costs ~10% on the uncontended MP/MC ring path (ring_perf_autotest)
- release notes and a note in cross_build_dpdk_for_riscv.rst

If you are still interested, please send a v2 within a month (a short window only because the patch has been idle for over two years; if you need more time, just say so). Otherwise I will send a v2 with these changes, keeping your authorship.

Thanks,
Sun Yuechi

#6 lore raw

PATCH v2 1/1 eal/riscv: add support for zawrs extension

In reply to Daniel Gregory

The Zawrs extension adds a pair of instructions that stall a core until a memory location is written to. This patch uses one of them to implement RISC-V specific versions of the rte_wait_until_equal_* functions. This is potentially more energy efficient than the default implementation that uses rte_pause/Zihintpause.

The technique works as follows:

* Check the value with a plain load first and return immediately if it already matches, as the generic implementation does
* Otherwise create a reservation set containing the address we want to wait on using an atomic load (lr.dw)
* Call wrs.nto - this blocks until the reservation set is invalidated by someone else writing to that address
* Execution can also resume arbitrarily, so we still need to check whether a change occurred and loop if not

Due to RISC-V atomics only supporting naturally aligned word (32 bit) and double word (64 bit) loads, I've used pointer rounding and bit shifting to implement waiting on 16-bit values.

The code is enabled at build time when the target ISA string contains the Zawrs extension, detected through the compiler's __riscv_zawrs architecture test macro, e.g. -Dcpu_instruction_set=rv64gcv_zawrs.

This is based on an earlier RFC by Daniel Gregory.

---
v2:
* rebased on main
* use build-time detection via __riscv_zawrs instead of a manual meson flag
* change memorder parameter type from int to rte_memory_order
* fix inverted alignment assert in RTE_WAIT_UNTIL_MASKED
* make the size in RTE_WAIT_UNTIL_MASKED a compile-time constant
* add a plain load before taking the reservation
* add release notes and cross-build documentation

 config/riscv/meson.build                      |   9 +
 .../linux_gsg/cross_build_dpdk_for_riscv.rst  |  26 +++
 doc/guides/rel_notes/release_26_11.rst        |   8 +
 lib/eal/riscv/include/rte_pause.h             | 159 ++++++++++++++++++
 4 files changed, 202 insertions(+)
config/riscv/meson.build +9 -0
index 7f3e3301bb..5370aeac9e 100644
@@ -153,6 +153,15 @@ if (riscv_extension_macros and
     endif
 endif
 
+# detect Zawrs (wait-on-reservation-set) extension, used by rte_wait_until_equal_*
+# to stall the hart until the polled memory location is written instead of spinning
+if (riscv_extension_macros and
+        (cc.get_define('__riscv_zawrs', args: machine_args) != ''))
+    message('Compiling with the Zawrs extension')
+    machine_args += ['-DRTE_RISCV_FEATURE_ZAWRS']
+    dpdk_flags += [['RTE_RISCV_FEATURE_ZAWRS', 1],]
+endif
+
 # apply flags
 foreach flag: dpdk_flags
     if flag.length() > 0
doc/guides/linux_gsg/cross_build_dpdk_for_riscv.rst +26 -0
index bcba12a604..c7be173b9e 100644
@@ -115,3 +115,29 @@ Currently the following targets are supported:
 To add a new target support, ``config/riscv/meson.build`` has to be modified by
 adding a new vendor/architecture id and a corresponding cross-file has to be
 added to ``config/riscv`` directory.
+
+
+Optional ISA extensions
+-----------------------
+
+Some optimisations are enabled at build time when the target ISA string
+includes the extension.
+The build looks at the per-extension macros the compiler defines when
+``__riscv_arch_test`` is available, such as ``__riscv_zawrs``:
+
+``v``
+   Vector implementations (``RTE_RISCV_FEATURE_V``).
+
+``zawrs``
+   ``rte_wait_until_equal_*`` stall the hart with ``wrs.nto``
+   instead of spinning (``RTE_RISCV_FEATURE_ZAWRS``).
+
+Pass the full ISA string to enable them.
+For native builds use ``-Dcpu_instruction_set``::
+
+   meson setup build -Dcpu_instruction_set=rv64gcv_zawrs
+
+For cross builds, set ``cpu`` in the cross-file instead::
+
+   [host_machine]
+   cpu = 'rv64gcv_zawrs'
doc/guides/rel_notes/release_26_11.rst +8 -0
index 8ca8379cc6..4cce0b478a 100644
@@ -68,6 +68,14 @@ New Features
   and use pointer arithmetic instead of integer casts to enable compiler optimizations.
   See API changes below.
 
+* **Added RISC-V Zawrs support for wait-until-equal helpers.**
+
+  On RISC-V targets built with the Zawrs extension in the ISA string
+  (for example ``-Dcpu_instruction_set=rv64gcv_zawrs``),
+  ``rte_wait_until_equal_*`` and ``RTE_WAIT_UNTIL_MASKED`` now stall the hart
+  with ``wrs.nto`` until the polled address is written instead of spinning
+  on ``pause``.
+
 * **Added API to get CPU socket ID.**
 
   Added the experimental ``rte_cpu_socket_id()`` function
lib/eal/riscv/include/rte_pause.h +159 -0

171 lines not on this page · raw message

Signature · 1 line
2.56.0