🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #26823

[PATCH net] veth: disable preemption between the NAPI mark and its wake-up

#26823
From: Florent Carli via B4 Relay
Date: Fri, 04 Dec 2026 07:51
137 lines
16370 bytes
From: Florent Carli <fcarli@gmail.com>

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 <fcarli@gmail.com>
---
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 <fcarli@gmail.com>
Technical Details
Message-ID: <20261204-veth-napi-rt-v1-1-02b900f75067@gmail.com>
Path: mbox-mo1.pugleaf.net!news-wdc.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-377174-linux-kernel-netdev=dewy.ru

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-wdc.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-377174-linux-kernel-netdev=dewy.ru
From: devnull+fcarli.gmail.com@kernel.org (Florent Carli via B4 Relay)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net] veth: disable preemption between the NAPI mark and its
 wake-up
Date: Fri, 04 Dec 2026 08:51:52 +0100
Organization: Usenet Network
Message-ID: <20261204-veth-napi-rt-v1-1-02b900f75067@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
Injection-Info: usenet.network;
	logging-data="2709065"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:TjRps7WJ/f4S4hxqrYXNuH1+F6U= sha256:urxrIz2KI/i5+B7XSkjTKD6Kn5fEML90YCfHsJ+acBg=
	sha1:k7E9cPKEmjVbdnla8hvwZo33pS4= sha256:LeGNbibO7OBeC/KeWdZ/9nfPyxilijTWOnc0r9XabJc=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 684BA117B5D; Fri,  4 Dec 2026 10:52:52 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.739
X-Spam-Status: No, score=-3.739 tagged_above=-9999 required=6.31
	tests=[DKIMWL_WL_HIGH=-0.24, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1,
	RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001,
	URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id dqcvbUe-BMm1;
	Fri,  4 Dec 2026 10:52:45 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.232.135.74; helo=sto.lore.kernel.org; envelope-from=netdev+bounces-377174-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sto.lore.kernel.org (sto.lore.kernel.org [172.232.135.74])
	by mail.dewy.ru (Postfix) with ESMTPS id 443FE117B4D; Fri,  4 Dec 2026 10:52:36 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sto.lore.kernel.org (Postfix) with ESMTP id E262F601350; Fri,  4 Dec 2026 07:52:35 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796370755;
	b=DbAazbTwJzKV+IWjlCmCl/heUVNqAtupW4srmW1TdwECkKxbyTE/4Z3QRe2u6UEOBWk8
	 6kOUbAASApLjZtddbYu1yW6QtGc/dU/0NDNDWhRRYhW/Xy/GSFvSsnRUG8bWd8kstANYW
	 cbLZ11Ut0AgURSxepGqWDqoqb0O3doNQayBD39D7PUQhFGhFBU36jv/PlTmePn2WN5/Jl
	 5xronTEdDqTqcrg91NMVq2QqjJtYO326l+8amK8AvQTLZhzw6+DdnBK+3/SDCYCAMoN1y
	 tJWXmmDQ5WI2NFZ9zL+NacKVyf7YFnOqgmtPGM7/EfzhplgaCuOLxxm4cUgvTAdbpRg==
X-ARC-Message-Signature: i=3; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796370755;
	h=X-Original-To:Received:Received:DKIM-Signature:Received:From:Date:
	 Subject:X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence: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=n6txZX37wDnUuT/XPWUFtDDG4ZFg2eawFoFS7JfN1aR2qGO+r5mEcCe6wqFCXZn8fKQ5
	 YzgVXnMe/kugV7wq4HJorQjrrU4/Hc35+Y5dkKD5jz6Oe3AOZKCQSPEwK6lHAAanhaD20
	 K3/1CzqqSXvkqoBmLl9A5Ihg0Ut/Qay1qmoyZj6cqO1PaWLyIN3O2IIkNdlYnq8ucbabs
	 fyWh7prjaV7IiN7/SrPoH5YNTnerQMh0vlVekd4NPXx+vj9KLq+DhC93lbUAAw6c8v8K+
	 O+ZdP6XwURmiqnQ2J4t2+yC1KceLPgrm7dPDiXB6zc0WbEzUa8cbOxb5t2Cark8vDKw==
X-ARC-Authentication-Results: i=3; 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=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 80210606A322;
	Fri,  4 Dec 2026 07:52:35 +0000 (UTC)
X-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
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18
X-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==
X-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==
X-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
X-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)
X-Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77
X-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==
X-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==
X-ARC-Authentication-Results: i=1; smtp.kernel.org; 
	arc=none smtp.remote-ip=100.80.115.77
X-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)
X-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==
X-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)
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list
X-B4-Tracking: v=1; b=H4sIABdxEmsC/x3MTQqAIBBA4avIrBtQC/u5SrSQHHM2JioRRHdPW
 n6L9x4olJkKLOKBTBcXPmOD6gTswcaDkF0zaKmN0rLHi2rAaBNjrqj8bMbJO1J2gJakTJ7vf7d
 CpArb+37UVWB+YwAAAA==
X-Change-ID: 20261203-veth-napi-rt-1f9678fde1a4
X-Cc: netdev@vger.kernel.org, linux-rt-devel@lists.linux.dev, 
 bpf@vger.kernel.org, Toshiaki Makita <toshiaki.makita1@gmail.com>, 
 stable@vger.kernel.org, Florent Carli <fcarli@gmail.com>
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 <fcarli@gmail.com>
X-Reply-To: fcarli@gmail.com