From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.70.183.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796054439; b=QjsFgLG1pCPdBX2o3uboDzuKxJPK7WlTl5ddFa/adywIpvFfLMD6kGKx0qP3UBS6hXJk ET6reEMZeanzADsAVfjAy241M+zSA5N4EV6TtlY74ZTHEmpQfh+QLOj+vgNChAYU7DYip EdIBtauCaoSyaeJDUvSi6XtrOefg9NepoWCTD49p1bvG/+HWD4NBlDY2+aaaOudW042BS m8BJc0CTmAImrLWHzf6sst2+xqsYVFG2su/A8GXMQsai3G1oR3tRDGQhE6UTKkQ6qRlnW fm/Tit6BvQMYbpSO/Yv2cGzlaUiZpd0gyudHwjSHWjO6BHMzlJ8g603CueaL2fFCahA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796054439; h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:In-Reply-To: References:User-Agent:Date:Message-ID:MIME-Version:Content-Type: X-GND-Sasl:X-GND-Score:X-GND-Cause:X-GND-State; bh=RCpSvyoHqzin/0CqyhUgLxa4MiOg2iX6BwKcWawMV18=; b=1E3UYkAh07r625utHgrd3pFYFJvbD5vEs2xBKPg32pStvruLu4M4wW/VzqEt1dFRLNAx 73fvcrfHbMHHRxaGgEIH+WdVvR3QJjMLd6Wu14q1RXQxGwTkzbNzuQ1LnF0kEknvNl8t9 bZdwfIR7kA0+HiYhu8elj2ZmzVo37p0crxYGgUXdqFGChKGucm+m02nciaNb4Er7KAlaF ICDWKhtns4Mv2ZYOaTyJHDWn+Tt78q5OQVy4gMbGe8Es84vyDZ9Df5+Sf/DZrZrmJOIEq rdlP1z9hQfNT8T0HjwKpxXdmAQ37OYxAnNn4BPSxXg8fHageF31Spk6/p8BRlohAXZA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=xenomai.org; spf=pass smtp.mailfrom=xenomai.org; dkim=pass header.d=xenomai.org header.i=@xenomai.org header.a=rsa-sha256 header.s=gm1 header.b=PaHsaG88; arc=none smtp.remote-ip=217.70.183.198 Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E9FB1606A31C for ; Mon, 30 Nov 2026 16:00:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E9FB1606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xenomai.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xenomai.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=xenomai.org header.i=@xenomai.org header.a=rsa-sha256 header.s=gm1 header.b=PaHsaG88 Received: by mail.gandi.net (Postfix) with ESMTPSA id 16E1F3EC10; Mon, 30 Nov 2026 16:00:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=xenomai.org; s=gm1; t=1796054430; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=RCpSvyoHqzin/0CqyhUgLxa4MiOg2iX6BwKcWawMV18=; b=PaHsaG88dW91+rPSfIZwr4Sy605C6HjWWfXXsNMwhc5UHejMEquqFbfT77dUTfcoZ6tneJ Z9iX3FiPodMif5zBpHliRu//T8pFOSwaHf/Eei/KReS1wfnQOUD8X/T3nttsCiIAiHBYip yNA1dVAlZOTEzQyYYuwzIUIq3XIVGdzpX03MHictuvMBENKeUjtcSBt9rAY9kSrs+Lhphd 3ID2XGrvV8I74Lu2LVLQsxgCWsctIENxFbmsTP0yfDEl+MfrcMmO1lgMjIxehJexkoPDwL HJ/Jtgp9I25iHbvgKRaU+6eOg4tY4JmP2DOJXq8gJlQOIC9UN7CYzoZNAbt5bA== From: Philippe Gerum To: Tobias Schaffner 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 In-Reply-To: <20261128210852.1175429-1-tobias.schaffner@siemens.com> (Tobias Schaffner's message of "Sat, 28 Nov 2026 22:08:52 +0100") References: <20261128210852.1175429-1-tobias.schaffner@siemens.com> User-Agent: mu4e 1.12.12; emacs 30.2 Date: Mon, 30 Nov 2026 17:00:28 +0100 Message-ID: <87cxrmmhr7.fsf@xenomai.org> X-Mailing-List: xenomai@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain X-GND-Sasl: rpm@xenomai.org X-GND-Score: -100 X-GND-Cause: dmFkZTG2+16n2wyMcHKmWEReANVRJ2wpxty1ebZNWsZ+0KcRwGc226rQQFVDTySLTgnSlSJsflho6tnBPHa2+SA6u89eZhjPMvNAOveAVL0rXDuuEYwyD+6ulZfXOywffbWRhPihsnklIwFTzvOvsu+zPCt5WIR25lwI3aDmryeOal8fMani4hnmsdIajdIIZB0iQQPJuE9B0P3Y1dt7MBYTG01A9B4OCaVCctwgCLVS0J4uQdP3OvW8X5MUVc1rIqABE8UsFHmWqr4H3qCydgYm0dwOTg+zdVPpXJbC6XICumAunZvFC+/GLfFrmXPgxQ4SY846rLeBft3uu6p/EbMdm8aYsdxocRV2T1D/m2K5Y/uZ5ElQTKpn/taWn50ZiMfrV+NjGBjqv2j3oYq5g9LGu16Uykd+40zxI9Uj/Kq9u1eSa/1WPD9enJsOFdNRWn0vQfAbUBCz34lzZW++TKucsaDRNJ4Z2HQNbSmM1k8Js6JI8zBRxyHw7sFZm0Z+D+3QoZNBvXgogjhiXKSk6fnpECqX58oYlAQJlYms3E1siYZTFjvnitbsFkzdf4yALdAUsSHG5H4HwHTVk8N/vvlSKZoGfFKFDPgYux3nirZi3Cr0OC8oT3sllyV/y+A1fWCaN+mxHpyRgat2xDkoBDBzi+87KymQb2UWTm2I5pUtLUoR7w X-GND-State: clean Tobias Schaffner 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 > --- > 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.