Re: io_uring/zcrx: copy statistics lose updates when one ifq is shared by multiple rings (Data race)

From: Pavel Begunkov

Date: Mon Nov 23 2026 - 08:50:55 EST


On 11/22/26 07:39, 徐晗翔 wrote:
An io_uring zero-copy RX ifq can be exported by one ring and imported by
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.
It is racy, and IIRC Clement dropped atomicity at some point, perhaps
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).

--
Pavel Begunkov