> 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.
Agreed for non-threaded NAPI: the poll then runs in the low priority
task at local_bh_enable(), and the patch changes nothing there. It
matters with threaded NAPI, where __napi_schedule() only wakes the NAPI
thread. On PREEMPT_RT, threaded NAPI is how the receive path gets a
priority and a CPU of its own; without it, the poll runs at the
priority of whichever task scheduled it, which is your point. The
backlog follows the same logic on PREEMPT_RT, where its NAPI always
runs in the backlog_napi threads. I will state this scope in the
commit message.
> I *think* if you have critical sender in your environment then it should
> not run at low priority.
[...]
> I would argue that you shouldn't have a nice-19 task sending packets.
The nice 19 sender is in the reproducer on purpose: it stands for the
traffic we cannot control. Our critical sender runs SCHED_FIFO, but the
same host also runs ordinary tasks, and any of them can send ARP, ND or
MLD frames that reach the reader's veth. No configuration can make all
of them RT.
On PREEMPT_RT, these ordinary tasks also carry frames that are not
theirs. Without PREEMPT_RT_NEEDS_BH_LOCK, a task that leaves its
outermost BH section runs the pending softirqs of its CPU. In our setup
the frames reach the reader's veth from a veth without NAPI, so they go
through the per-CPU backlog, and whichever task runs NET_RX next on
that CPU forwards them and calls veth_xmit() on the reader's veth. With
ftrace histograms we counted a SCHED_OTHER thread of the publishing
process, which sends nothing on that link, doing about 200 such calls
per reader in two hours. The set of tasks that call veth_xmit() on a
given veth follows from who runs the softirq on each CPU, and the
application cannot keep it to RT tasks.
To show the inversion without the widened window of the reproducer, I
ran it on the unmodified net tree (90ae4042bd9f) and on the same tree
with this patch only, same config, both guests at once on separate
host cores. The FIFO 10 task on the nice 19 sender's CPU ran bursts of
1, 4 and 10 ms, 15 min each. Worst delay of the SCHED_FIFO periodic
stream, and in brackets the episodes of 0.5 ms or more:
FIFO 10 bursts unpatched patched
1 ms every 1.2 ms 1.6 ms (14) 0.28 ms (0)
4 ms every 5 ms 3.9 ms (1) 1.1 ms (1)
10 ms every 12 ms 9.8 ms (4) 1.0 ms (4)
Without the patch, the worst delay follows the length of the bursts of
a task that has nothing to do with networking: the FIFO 50 stream
waits for the FIFO 10 task, through the nice 19 sender that holds the
wake-up. With the patch, it stays within the noise of the guests,
which reached 1.5 ms (patched) and 4.7 ms (unpatched) in a first phase
without any load. On the system described in the notes (Debian 7.3.4
PREEMPT_RT, no test change), the patch took the reader from 6 late
(3 over 3 ms) and 2 lost frames in 30 min to none in 60 min.
> With real hardware, the traffic is usually split into high and low
> priority. For that NICs provide queues and you can configure them.
veth has queues as well, and steering the periodic frames to an rx
queue of their own would keep the ordinary traffic away from that
queue's NAPI. Its producers would still be the tasks that happen to
carry these frames, as above. Our setup also has event frames, just as
critical, that RT threads send from housekeeping CPUs: there, any
ordinary task that leaves a BH section can carry them to veth_xmit().
On a real NIC, the NAPI of an rx queue is scheduled by that queue's
interrupt, from hardirq or from its RT irq thread. On veth, every
sender schedules the peer's NAPI at its own priority, and nothing
boosts the one that set NAPI_STATE_SCHED.
Without PREEMPT_RT the patch changes nothing, since BH disabled already
implies preemption disabled. With PREEMPT_RT the non-preemptible
section covers napi_schedule_prep() and the wakeup of the NAPI thread.
For v2 I would use preempt_disable_nested(), as
Documentation/core-api/real-time/differences.rst suggests for code that
relies on BH disabling preemption; it is a no-op without PREEMPT_RT.
Would a v2 with the commit message limited to the threaded NAPI case
work for you?
Thanks,
Florent
On Fri, Dec 4, 2026 at 10:20 AM Sebastian Andrzej Siewior
<bige…@linutronix.de> wrote:
>
> On 2026-12-04 08:51:52 [+0100], Florent Carli via B4 Relay wrote:
> > From: Florent Carli <fcar…@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