From: Philippe Gerum <rpm@xenomai.org>
To: Tobias Schaffner <tobias.schaffner@siemens.com>
Cc: xenomai@lists.linux.dev, jan.kiszka@siemens.com
Subject: Re: [PATCH libevl] lib/ring_ptr: riscv: do not swap entry half of a cell alone
Date: Mon, 30 Nov 2026 17:00:28 +0100 [thread overview]
Message-ID: <87cxrmmhr7.fsf@xenomai.org> (raw)
In-Reply-To: <20261128210852.1175429-1-tobias.schaffner@siemens.com> (Tobias Schaffner's message of "Sat, 28 Nov 2026 22:08:52 +0100")
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.
prev parent reply other threads:[~2026-11-30 16:00 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
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 message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87cxrmmhr7.fsf@xenomai.org \
--to=rpm@xenomai.org \
--cc=jan.kiszka@siemens.com \
--cc=tobias.schaffner@siemens.com \
--cc=xenomai@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.