From: Florent Carli via B4 Relay <devnull+fcarli.gmail.com@kernel.org>
To: Andrew Lunn <andrew+netdev@lunn.ch>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>,
Paolo Abeni <pabeni@redhat.com>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Clark Williams <clrkwllms@kernel.org>,
Steven Rostedt <rostedt@goodmis.org>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Jesper Dangaard Brouer <hawk@kernel.org>,
John Fastabend <john.fastabend@gmail.com>,
Stanislav Fomichev <sdf@fomichev.me>
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>
Subject: [PATCH net] veth: disable preemption between the NAPI mark and its wake-up
Date: Fri, 04 Dec 2026 08:51:52 +0100 [thread overview]
Message-ID: <20261204-veth-napi-rt-v1-1-02b900f75067@gmail.com> (raw)
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>
reply other threads:[~2026-12-04 7:52 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261204-veth-napi-rt-v1-1-02b900f75067@gmail.com \
--to=devnull+fcarli.gmail.com@kernel.org \
--cc=andrew+netdev@lunn.ch \
--cc=ast@kernel.org \
--cc=bigeasy@linutronix.de \
--cc=bpf@vger.kernel.org \
--cc=clrkwllms@kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=fcarli@gmail.com \
--cc=hawk@kernel.org \
--cc=john.fastabend@gmail.com \
--cc=kuba@kernel.org \
--cc=linux-rt-devel@lists.linux.dev \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=rostedt@goodmis.org \
--cc=sdf@fomichev.me \
--cc=stable@vger.kernel.org \
--cc=toshiaki.makita1@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox