Re: io_uring/zcrx: copy statistics lose updates when one ifq is shared by multiple rings (Data race)
From: Clément Léger
Date: Tue Nov 24 2026 - 12:17:25 EST
On 11/23/26 14:50, Pavel Begunkov wrote:
> On 11/22/26 07:39, 徐晗翔 wrote:An io_uring zero-copy RX ifq can be exported by one ring and imported byIt is racy, and IIRC Clement dropped atomicity at some point, perhaps
another. Both rings then perform receive-zc on their own TCP sockets,
but they share the same struct io_zcrx_ifq and therefore the same
ifq->notif_stats. The statistics update is not atomic:
static void zcrx_stat_add(__u64 *p, s64 v)
{
WRITE_ONCE(*p, READ_ONCE(*p) + v);
}
io_uring/zcrx.c:1337-1340. It is called for copy_count and copy_bytes
from io_zcrx_copy_frag() at zcrx.c:1770-1771. There is no ifq-level
lock in this path. lock_sock() in io_zcrx_tcp_recvmsg() is per TCP
socket, the ring's uring_lock is per ring, alloc_lock only protects
fallback niov allocation and is released before the copy, and
ctx_lock/notification is taken after the statistics update and is not
involved when event_desc.type_mask == 0. Two CPUs can therefore read
the same old value, add their own increments, and write back the same
new value: one update is lost.
The effect is limited to the UAPI cumulative copy statistics
(struct zcrx_stats at include/uapi/linux/io_uring/zcrx.h:83-86). The
values are user-mappable diagnostic data; the race does not control
buffer lifetime, DMA, reference counts, or packet contents.
for performance reasons. I don't think it really matters though, the
main purposes of the stat is to be a flag saying "something is wrong,
setup steering rules". What's your use case? For precise statistics
I'd rather prefer to add a trace point, which would be able to do it
the right way (e.g. per-cpu counters).
Indeed, I used atomic first but then we dropped it. I don't remember exactly but we didn't consider export/import at that time. It make sense though so atomic would be fine as well although with an additional cost. I'll let Pavel be the judge but agreed that we do not really care about the value itself (there is nothing the userspace can really do about it
except check that "it's a lot").
Clément