From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796370749; b=ZDQTl6a1mTbmXbTHghMe0zMEL4/RxD7gFb8mmDR0jRR9paPEcZYsuOZrFUta/ujPVi8n GJ2GCfLf3FiW+2uFAQW9zuimY/jqveU9hhe7Jo2WgevFzG6u7a+biMq/CrhmSUzSW6yd+ SnuhMoIEUybjCV3fkgR8d9/Up6YcUHX2fLZANlQEbkd6DMWgH0VKMmOiGssjr8aNYDPEw PQcbCBd7wUA/IJ0lazNoAOE5hIpQEhjj0iNTaDF6MVA3PbBRLShob2HMLXuJeKnZtuWup dDP2D9wsgjQuH6qvZV5Jwce05hofcWMG+3C+sQQFpytk0JEl7RcNDQiBLOkZQvGCDyQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796370749; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=lFb85jbBu8wDonhlsosHS7ZUgKAgeAbYlqCCqJPU+Dk=; b=DPV+TLsVBOFncZppGfYNGSdcMqv5niYT0UfRMOiMBXJz76YjGXx0p+Ti8wt2Yz7gg5MI zi6Oh3/MeWRU6qVJoHlQ2CrYrnJMVnE4RyKlLJbOxmLAR5LNafF0wveocF0mLsSuOs4ZR A74QoW5WAKeXJC/a7GLNPmHmFs0RpctU7yDKfuJXhBkOm3fhvVGgInmXgSJL7Q+ZDjQdP VJln4iiDG2fGdB0QXk9P8RpRoLJ90WRsiDB8GwWxRheqH/+ZntOFRpK/H9il1nB1oDF9/ ZzawOSvQZHvQ+MAK5jrgOohDOC70eIfF0D9VXbnoRtbF70VR4a/EWWf+JZUCYSJGF6Q== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=fQQBcNCS; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9C7B8606A32D; Fri, 4 Dec 2026 07:52:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=fQQBcNCS Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796370749; b=08s3eM7B6THN6NrsS8u1RFcLRvMAT/HY5jmBEV8fwyOu4DYgqzR3+smXiyBogWIBKByn uoNjF14YJW8wKt4sNJHadYD+X8QlPZkmeKGya6GEL1A94mNZIJVZ5JM6j/iK23ju9+c9c lV3awPUMkLJBpaRUitCy9JEPydCly6Erb+d9Flbtg0r0gOcrFj9UrdYnXNz6U1u4SY6iz swBvUln98LqNUj/YbhWxJiWfIJMBj4kZ1WYWl2OAF+RdieNurhJnrn3/0mwVlmn+iMFL8 yD5pYwFpPc4fxkqB6GhvhVTfxGArAkTi5MygI70nS17tjoQ37ugl+CuUdg88Xj1ZTYQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796370749; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=lFb85jbBu8wDonhlsosHS7ZUgKAgeAbYlqCCqJPU+Dk=; b=udFqcCBVRsnZMZdQLD0i+sJp2CEnggKvuy9I54u74img9IFNc+wgHIbAR1e/VZom+s0s vdhP81fLA0vCSTqb5NCKOLqTI2t5OQNo0bmdgwaBVQegygaR6tHz6KJ6FPRIyy+CawvMS MUGGfls9v9N/Qkrwex/XkRAVWXOIlb0H5IuBn6MTnourryGJb/zBofJyaRBSJ8rscDC1E jpuScfgcFN5aQcFw0v9DGcEig5h0w6bwbl4fU41InXsdyOesDwh0vbSLOUuS8GCcDdSw5 C0mXj67NQ80p4bGWBIH4Cif4j5POx4vZHc1N09oeGT1JqY5XTeaZrJ9EJZsvTvJD3qw== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (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.kernel.org (Postfix) with ESMTPS id 90E491F000CE; Fri, 4 Dec 2026 07:52:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796370749; bh=lFb85jbBu8wDonhlsosHS7ZUgKAgeAbYlqCCqJPU+Dk=; h=From:Date:Subject:To:Cc:Reply-To; b=fQQBcNCSGaspL1OkIUasRLVanxMlDOcBlOeNZKdpJm1WRYEMUmcQwly00gA5BdWxO g9c2xFonitUSEdTKO2BV9M8JZXMaPFdfw4eWE98BZN+HDSfqWWX7soEzCrOjqCsOz7 eLDCje9WLiSlc12ZMJVp7WYQyN14gwU2hhNcGELcnZOYFJIUReVZZdEq2gD0A0rA08 zp3fdeyBwWFV+erv+lzJkosuI3Qn4KNNQhp61BMIjFCcAkrZRhyfQ2HR0TZdlJ3Z9B /ec6n7RRd5lK0VMZJ/3C3p7Gz4VHD9eWr6Re1ng1OevaXdmUqKlCvb9jRz91openRT cxrOeQMsKSzeQ== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 1E16F4D9B0; Fri, 4 Dec 2026 07:52:28 +0000 (UTC) From: Florent Carli via B4 Relay Date: Fri, 04 Dec 2026 08:51:52 +0100 Subject: [PATCH net] veth: disable preemption between the NAPI mark and its wake-up X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261204-veth-napi-rt-v1-1-02b900f75067@gmail.com> X-B4-Tracking: v=1; b=H4sIABdxEmsC/x3MTQqAIBBA4avIrBtQC/u5SrSQHHM2JioRRHdPW n6L9x4olJkKLOKBTBcXPmOD6gTswcaDkF0zaKmN0rLHi2rAaBNjrqj8bMbJO1J2gJakTJ7vf7d CpArb+37UVWB+YwAAAA== X-Change-ID: 20261203-veth-napi-rt-1f9678fde1a4 To: Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Stanislav Fomichev Cc: netdev@vger.kernel.org, linux-rt-devel@lists.linux.dev, bpf@vger.kernel.org, Toshiaki Makita , stable@vger.kernel.org, Florent Carli X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796370747; l=5935; i=fcarli@gmail.com; s=20261204; h=from:subject:message-id; bh=62HU5rw5vt2qm+A9AZ7Z6beck0FqlsYlSKLpL+jZdZs=; b=yRALK0HfblqIlFMRqiPBRjG5mY24KpHa55OAbFfxUFL6tjXN7H8WQBcruj/wHQA5yfxn5sxCl D9C5fTR9zMBBs24pYBvRSgJZv45nz6HLkcuRhi1Ui3PbheNAYtvDftO X-Developer-Key: i=fcarli@gmail.com; a=ed25519; pk=LtXAbmnVRRI6IWayDMMvVruZ5HRJ3iDEu1u1zxGXq1Q= X-Endpoint-Received: by B4 Relay for fcarli@gmail.com/20261204 with auth_id=1740 X-Original-From: Florent Carli Reply-To: fcarli@gmail.com Content-Transfer-Encoding: 7bit From: Florent Carli On PREEMPT_RT the callers of __veth_xdp_flush() are preemptible: the BH-disabled sections of the transmit path do not disable preemption there. A producer can be preempted after napi_schedule_prep() has marked the peer's NAPI as scheduled, before __napi_schedule() wakes it up. Every later producer then sees rx_notify_masked or NAPI_STATE_SCHED set, queues its packet in the ptr_ring and returns, and the NAPI sleeps until the preempted producer runs again. No lock is held across that window, so priority inheritance cannot shorten it. A low priority sender can thus hold the packets of every higher priority sender of the same veth for as long as it stays preempted. We hit this with IEC 61850 sampled values delivered through a veth whose peer runs threaded NAPI, to a real-time reader on an isolated CPU: a nice 19 sender was preempted for 4.33 ms between the two calls, the peer's NAPI thread was not woken during that time, then was woken 4 us after the sender ran again and polled 23 packets at once. Mark and wake with preemption disabled, in a helper used by both places that schedule the NAPI: __veth_xdp_flush() and the re-schedule at the end of veth_poll(), which without threaded NAPI runs in whatever task processes the softirq and has the same window on PREEMPT_RT. __napi_schedule() takes raw locks only and masks interrupts itself, so this is fine on PREEMPT_RT. Without PREEMPT_RT the callers already run with BH, hence preemption, disabled and nothing changes. Reproducer, in a PREEMPT_RT guest of this tree (4 vCPUs, each on its own host CPU): a veth pair, the peer with GRO on and threaded NAPI, its thread SCHED_FIFO 50 on CPU 2; a SCHED_FIFO 50 task sends a timestamped packet every 250 us from CPU 1; a nice 19 task sends 100k packets/s on the same veth from CPU 3, where a SCHED_FIFO 10 task runs 1 ms out of every 1.2 ms; a SCHED_FIFO 60 packet socket reader on the peer measures the delay of each periodic packet, 480000 per run. To make the race frequent, the test kernels also delay 50 us between napi_schedule_prep() and __napi_schedule() (not part of this patch): > 1 ms 0.5-1 ms max window widened, before 20744 182358 4.5 ms window widened, after 0 0 0.34 ms window widened, no preemption, before 0 0 0.14 ms actual window, before 2 7 1.2 ms actual window, after 0 3 0.97 ms The guest alone, without the other senders, showed 0 to 8 packets over 1 ms in the same time. Tested on this tree with PREEMPT_RT: the reproducer above, and tools/testing/selftests/net/veth.sh in a 4-vCPU guest (all 60 checks pass). Fixes: 948d4f214fde ("veth: Add driver XDP") Cc: stable@vger.kernel.org Signed-off-by: Florent Carli --- Notes (not part of the commit message): - napi_schedule() chains the same two steps and has the same window when called from a preemptible context on PREEMPT_RT. Fixing it there would cover other drivers too; this patch keeps to veth, whose producers run in task context. I can look at the generic helper instead if that is preferred. - Also tested on the system where we hit it: the Debian 7.3.4 PREEMPT_RT kernel with only veth.ko rebuilt with this patch, and a nice 19 task sending 2000 packets/s through the same veth as the sampled values. Without the patch: 6 late (3 over 3 ms) and 2 lost sampled values in 30 min, the first after 14 min. With it: none in 60 min. - The reproducer (a small C program and the scripts that build and boot the guest) can be posted if useful. - The analysis, the reproducer and this patch were prepared with an AI coding assistant (Claude); I reviewed and tested them and stand by them. --- drivers/net/veth.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/drivers/net/veth.c b/drivers/net/veth.c index faa837acea7b7ec14445ce54708bb4dd2b9d7fd7..a67a608e9ed1afe999be6d2cc7107a8bad40593b 100644 --- a/drivers/net/veth.c +++ b/drivers/net/veth.c @@ -299,15 +299,26 @@ static void veth_ptr_free(void *ptr) kfree_skb(ptr); } -static void __veth_xdp_flush(struct veth_rq *rq) +/* Mark the NAPI scheduled and wake it up. Once it is marked, the other + * producers leave the wake-up to this caller. On PREEMPT_RT the callers + * are preemptible: do not let a preemption hold the wake-up back. + */ +static void veth_napi_schedule(struct veth_rq *rq) { - /* Write ptr_ring before reading rx_notify_masked */ - smp_mb(); - if (!READ_ONCE(rq->rx_notify_masked) && - napi_schedule_prep(&rq->xdp_napi)) { + preempt_disable(); + if (napi_schedule_prep(&rq->xdp_napi)) { WRITE_ONCE(rq->rx_notify_masked, true); __napi_schedule(&rq->xdp_napi); } + preempt_enable(); +} + +static void __veth_xdp_flush(struct veth_rq *rq) +{ + /* Write ptr_ring before reading rx_notify_masked */ + smp_mb(); + if (!READ_ONCE(rq->rx_notify_masked)) + veth_napi_schedule(rq); } static int veth_xdp_rx(struct veth_rq *rq, struct sk_buff *skb) @@ -999,12 +1010,8 @@ static int veth_poll(struct napi_struct *napi, int budget) if (done < budget && napi_complete_done(napi, done)) { /* Write rx_notify_masked before reading ptr_ring */ smp_store_mb(rq->rx_notify_masked, false); - if (unlikely(!__ptr_ring_empty(&rq->xdp_ring))) { - if (napi_schedule_prep(&rq->xdp_napi)) { - WRITE_ONCE(rq->rx_notify_masked, true); - __napi_schedule(&rq->xdp_napi); - } - } + if (unlikely(!__ptr_ring_empty(&rq->xdp_ring))) + veth_napi_schedule(rq); } /* Release backpressure per NAPI poll */ --- base-commit: 90ae4042bd9f35a9098d14b1a12a43df9b326e2b change-id: 20261203-veth-napi-rt-1f9678fde1a4 Best regards, -- Florent Carli