* [PATCH libevl] lib/ring_ptr: riscv: do not swap entry half of a cell alone
@ 2026-11-28 21:08 Tobias Schaffner
2026-11-30 15:29 ` Florian Bezdeka
2026-11-30 16:00 ` Philippe Gerum
0 siblings, 2 replies; 3+ messages in thread
From: Tobias Schaffner @ 2026-11-28 21:08 UTC (permalink / raw)
To: xenomai; +Cc: rpm, jan.kiszka, Tobias Schaffner
Each ring cell is a 16-byte pair of entry and pointer. Enqueue updates
the whole pair with a 16-byte copy and swap operation. Dequeue only the
swaps entry half. The SiFive U74 does not have the zacas extension and
therefore has no 16-byte copy and swap operation. As a result libatomic
emulates it under a lock.
Still the 8-byte deque is running native on the U74 as 8byte copy and
swap operations are supported and therefore do not need a lock. The
libatomic 16byte operations lock but that lock is not used on the
dequeue side.
Thats why the dequeue can slip between the two libatomic operations on
enque. Items get lost and ring-stress livelocks.
Update the entry with a 16-byte copy and swap of the whole pair instead,
also for the dequeue so all writers of a cell take the same lock.
x86 and arm64 have a native 16-byte CAS and their generated code is
unchanged.
Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
---
evl/include/evl/ring_ptr.h | 26 +++++++++++++++++++++++---
1 file changed, 23 insertions(+), 3 deletions(-)
diff --git a/evl/include/evl/ring_ptr.h b/evl/include/evl/ring_ptr.h
index d938b8f..76262b0 100644
--- a/evl/include/evl/ring_ptr.h
+++ b/evl/include/evl/ring_ptr.h
@@ -108,6 +108,27 @@ static inline lfatomic_big_t __evl_ringptr_load(_Atomic(lfatomic_big_t) *cell)
return pair;
}
+static __always_inline
+bool __evl_ringptr_cmpxchg_entry(_Atomic(lfatomic_big_t) *cell,
+ lfatomic_t *entry, lfatomic_t entry_new)
+{
+#ifdef __riscv
+ lfatomic_t ptr = __evl_ringptr_pointer(__evl_ringptr_load(cell));
+ lfatomic_big_t pair = __evl_ringptr_pair(*entry, ptr);
+
+ if (atomic_try_cmpxchg_weak(cell, &pair,
+ __evl_ringptr_pair(entry_new, ptr)))
+ return true;
+
+ *entry = __evl_ringptr_entry(pair);
+
+ return false;
+#else
+ return atomic_try_cmpxchg_weak(__evl_ringptr_array_entry(cell),
+ entry, entry_new);
+#endif
+}
+
static inline size_t __evl_ringptr_map(lfatomic_t idx, size_t order, size_t n)
{
return __evl_ring_map(idx, order, n, EVL_RINGPTR_MINORDER);
@@ -206,9 +227,8 @@ bool __evl_ringptr_dequeue(struct __evl_ringptr *ring, size_t order,
entry_new = hcycle | (entry & 0x2);
}
} while (__evl_ring_cmp(ecycle, <, hcycle) &&
- !atomic_try_cmpxchg_weak(
- __evl_ringptr_array_entry(&ring->array[hidx]),
- &entry, entry_new));
+ !__evl_ringptr_cmpxchg_entry(&ring->array[hidx],
+ &entry, entry_new));
tail = atomic_read(&ring->base.tail);
if (__evl_ring_cmp(tail, <=, head + 1)) {
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH libevl] lib/ring_ptr: riscv: do not swap entry half of a cell alone
2026-11-28 21:08 [PATCH libevl] lib/ring_ptr: riscv: do not swap entry half of a cell alone Tobias Schaffner
@ 2026-11-30 15:29 ` Florian Bezdeka
2026-11-30 16:00 ` Philippe Gerum
1 sibling, 0 replies; 3+ messages in thread
From: Florian Bezdeka @ 2026-11-30 15:29 UTC (permalink / raw)
To: Tobias Schaffner, xenomai; +Cc: rpm, jan.kiszka
On Sat, 2026-11-28 at 22:08 +0100, Tobias Schaffner wrote:
> Each ring cell is a 16-byte pair of entry and pointer. Enqueue updates
> the whole pair with a 16-byte copy and swap operation. Dequeue only the
> swaps entry half. The SiFive U74 does not have the zacas extension and
> therefore has no 16-byte copy and swap operation. As a result libatomic
> emulates it under a lock.
>
> Still the 8-byte deque is running native on the U74 as 8byte copy and
> swap operations are supported and therefore do not need a lock. The
> libatomic 16byte operations lock but that lock is not used on the
> dequeue side.
>
> Thats why the dequeue can slip between the two libatomic operations on
> enque. Items get lost and ring-stress livelocks.
>
> Update the entry with a 16-byte copy and swap of the whole pair instead,
> also for the dequeue so all writers of a cell take the same lock.
>
> x86 and arm64 have a native 16-byte CAS and their generated code is
> unchanged.
>
> Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
> ---
> evl/include/evl/ring_ptr.h | 26 +++++++++++++++++++++++---
> 1 file changed, 23 insertions(+), 3 deletions(-)
>
> diff --git a/evl/include/evl/ring_ptr.h b/evl/include/evl/ring_ptr.h
> index d938b8f..76262b0 100644
> --- a/evl/include/evl/ring_ptr.h
> +++ b/evl/include/evl/ring_ptr.h
> @@ -108,6 +108,27 @@ static inline lfatomic_big_t __evl_ringptr_load(_Atomic(lfatomic_big_t) *cell)
> return pair;
> }
>
> +static __always_inline
> +bool __evl_ringptr_cmpxchg_entry(_Atomic(lfatomic_big_t) *cell,
> + lfatomic_t *entry, lfatomic_t entry_new)
> +{
> +#ifdef __riscv
This is true for all riscv systems, not only for those without zacas
extension.
This likely needs a build configuration setting - assumption: compiler
does not know about available extensions.
> + lfatomic_t ptr = __evl_ringptr_pointer(__evl_ringptr_load(cell));
> + lfatomic_big_t pair = __evl_ringptr_pair(*entry, ptr);
> +
> + if (atomic_try_cmpxchg_weak(cell, &pair,
> + __evl_ringptr_pair(entry_new, ptr)))
> + return true;
> +
> + *entry = __evl_ringptr_entry(pair);
> +
> + return false;
> +#else
> + return atomic_try_cmpxchg_weak(__evl_ringptr_array_entry(cell),
> + entry, entry_new);
> +#endif
> +}
> +
> static inline size_t __evl_ringptr_map(lfatomic_t idx, size_t order, size_t n)
> {
> return __evl_ring_map(idx, order, n, EVL_RINGPTR_MINORDER);
> @@ -206,9 +227,8 @@ bool __evl_ringptr_dequeue(struct __evl_ringptr *ring, size_t order,
> entry_new = hcycle | (entry & 0x2);
> }
> } while (__evl_ring_cmp(ecycle, <, hcycle) &&
> - !atomic_try_cmpxchg_weak(
> - __evl_ringptr_array_entry(&ring->array[hidx]),
> - &entry, entry_new));
> + !__evl_ringptr_cmpxchg_entry(&ring->array[hidx],
> + &entry, entry_new));
>
> tail = atomic_read(&ring->base.tail);
> if (__evl_ring_cmp(tail, <=, head + 1)) {
> --
> 2.53.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH libevl] lib/ring_ptr: riscv: do not swap entry half of a cell alone
2026-11-28 21:08 [PATCH libevl] lib/ring_ptr: riscv: do not swap entry half of a cell alone Tobias Schaffner
2026-11-30 15:29 ` Florian Bezdeka
@ 2026-11-30 16:00 ` Philippe Gerum
1 sibling, 0 replies; 3+ messages in thread
From: Philippe Gerum @ 2026-11-30 16:00 UTC (permalink / raw)
To: Tobias Schaffner; +Cc: xenomai, jan.kiszka
Tobias Schaffner <tobias.schaffner@siemens.com> writes:
> Each ring cell is a 16-byte pair of entry and pointer. Enqueue updates
> the whole pair with a 16-byte copy and swap operation. Dequeue only the
> swaps entry half. The SiFive U74 does not have the zacas extension and
> therefore has no 16-byte copy and swap operation. As a result libatomic
> emulates it under a lock.
>
> Still the 8-byte deque is running native on the U74 as 8byte copy and
> swap operations are supported and therefore do not need a lock. The
> libatomic 16byte operations lock but that lock is not used on the
> dequeue side.
>
> Thats why the dequeue can slip between the two libatomic operations on
> enque. Items get lost and ring-stress livelocks.
>
> Update the entry with a 16-byte copy and swap of the whole pair instead,
> also for the dequeue so all writers of a cell take the same lock.
>
> x86 and arm64 have a native 16-byte CAS and their generated code is
> unchanged.
>
> Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
> ---
> evl/include/evl/ring_ptr.h | 26 +++++++++++++++++++++++---
> 1 file changed, 23 insertions(+), 3 deletions(-)
>
> diff --git a/evl/include/evl/ring_ptr.h b/evl/include/evl/ring_ptr.h
> index d938b8f..76262b0 100644
> --- a/evl/include/evl/ring_ptr.h
> +++ b/evl/include/evl/ring_ptr.h
> @@ -108,6 +108,27 @@ static inline lfatomic_big_t __evl_ringptr_load(_Atomic(lfatomic_big_t) *cell)
> return pair;
> }
>
> +static __always_inline
> +bool __evl_ringptr_cmpxchg_entry(_Atomic(lfatomic_big_t) *cell,
> + lfatomic_t *entry, lfatomic_t entry_new)
> +{
> +#ifdef __riscv
> + lfatomic_t ptr = __evl_ringptr_pointer(__evl_ringptr_load(cell));
> + lfatomic_big_t pair = __evl_ringptr_pair(*entry, ptr);
> +
> + if (atomic_try_cmpxchg_weak(cell, &pair,
> + __evl_ringptr_pair(entry_new, ptr)))
> + return true;
> +
> + *entry = __evl_ringptr_entry(pair);
> +
> + return false;
> +#else
> + return atomic_try_cmpxchg_weak(__evl_ringptr_array_entry(cell),
> + entry, entry_new);
> +#endif
> +}
> +
> static inline size_t __evl_ringptr_map(lfatomic_t idx, size_t order, size_t n)
> {
> return __evl_ring_map(idx, order, n, EVL_RINGPTR_MINORDER);
> @@ -206,9 +227,8 @@ bool __evl_ringptr_dequeue(struct __evl_ringptr *ring, size_t order,
> entry_new = hcycle | (entry & 0x2);
> }
> } while (__evl_ring_cmp(ecycle, <, hcycle) &&
> - !atomic_try_cmpxchg_weak(
> - __evl_ringptr_array_entry(&ring->array[hidx]),
> - &entry, entry_new));
> + !__evl_ringptr_cmpxchg_entry(&ring->array[hidx],
> + &entry, entry_new));
>
> tail = atomic_read(&ring->base.tail);
> if (__evl_ring_cmp(tail, <=, head + 1)) {
Nitpicking: I would rather provide a riscv-specific definition of
__evl_ringptr_cmpxchg_entry in
lib/arch/riscv/include/asm/evl/ring_ptr.h, and a generic fallback if not
defined by the architecture.
--
Philippe.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-30 16:00 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 21:08 [PATCH libevl] lib/ring_ptr: riscv: do not swap entry half of a cell alone Tobias Schaffner
2026-11-30 15:29 ` Florian Bezdeka
2026-11-30 16:00 ` Philippe Gerum
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.