* [PATCH net] veth: disable preemption between the NAPI mark and its wake-up
@ 2026-12-04 7:51 Florent Carli via B4 Relay
2026-12-04 9:20 ` Sebastian Andrzej Siewior
0 siblings, 1 reply; 2+ messages in thread
From: Florent Carli via B4 Relay @ 2026-12-04 7:51 UTC (permalink / raw)
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, linux-rt-devel, bpf, Toshiaki Makita, stable,
Florent Carli
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>
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH net] veth: disable preemption between the NAPI mark and its wake-up
2026-12-04 7:51 [PATCH net] veth: disable preemption between the NAPI mark and its wake-up Florent Carli via B4 Relay
@ 2026-12-04 9:20 ` Sebastian Andrzej Siewior
0 siblings, 0 replies; 2+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-12-04 9:20 UTC (permalink / raw)
To: fcarli
Cc: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Clark Williams, Steven Rostedt, Alexei Starovoitov,
Daniel Borkmann, Jesper Dangaard Brouer, John Fastabend,
Stanislav Fomichev, netdev, linux-rt-devel, bpf, Toshiaki Makita,
stable
On 2026-12-04 08:51:52 [+0100], Florent Carli via B4 Relay wrote:
> 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.
Right but if you schedule NAPI at low priority in a BH disabled section,
then the NAPI callback is invoked once BH is enabled again. So there is
no gain.
> 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.
I *think* if you have critical sender in your environment then it should
not run at low priority.
…
> 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
so this needed, threaded NAPI. Otherwise the NAPI task would remain in
your context and you gain nothing. With this change you push it to
another thread.
> 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):
You have low priority sender and high prio sender and when the low
priority one owns NAPI and the high priority one stuffs packets into
queue without processing.
I would argue that you shouldn't have a nice-19 task sending packets.
With real hardware, the traffic is usually split into high and low
priority. For that NICs provide queues and you can configure them.
This is something I would expect one to do.
Sebastian
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-04 9:34 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 7:51 [PATCH net] veth: disable preemption between the NAPI mark and its wake-up Florent Carli via B4 Relay
2026-12-04 9:20 ` Sebastian Andrzej Siewior
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox