BPF List
 help / color / mirror / Atom feed
From: hawk@kernel.org
To: netdev@vger.kernel.org
Cc: bpf@vger.kernel.org, mkroemeke@cloudflare.com,
	matt@readmodwrite.com, kernel-team@cloudflare.com,
	hawk@kernel.org, simon.schippers@tu-dortmund.de,
	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>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	John Fastabend <john.fastabend@gmail.com>,
	Stanislav Fomichev <sdf@fomichev.me>,
	Toshiaki Makita <toshiaki.makita1@gmail.com>
Subject: [PATCH net 3/3] veth: order rx_notify_masked store in the veth_poll() re-arm path
Date: Mon, 30 Nov 2026 10:40:00 +0100	[thread overview]
Message-ID: <20261130094000.2022376-4-hawk@kernel.org> (raw)
In-Reply-To: <20261130094000.2022376-1-hawk@kernel.org>

From: Jesper Dangaard Brouer <hawk@kernel.org>

The re-arm path at the end of veth_poll() has the same store sequence
as __veth_xdp_flush(), without the barrier added in the previous patch.

Normally this is safe. The next poll runs in the same NAPI kthread, or
in softirq on the same CPU, so program order keeps the two stores in
order. Busy polling breaks this. veth GRO sets the NAPI ID on the skb,
so a socket can busy-poll the veth NAPI from another CPU. When the busy
poll loop ends, busy_poll_stop() calls veth_poll() on the busy-polling
CPU, and the re-arm hands NAPI to the kthread on another CPU:

  busy-poll CPU                   NAPI kthread
  -------------                   ------------
  busy_poll_stop()
    veth_poll()
      napi_schedule_prep()
      WRITE_ONCE(masked, true)
      set_bit(SCHED_THREADED)
                                  test_bit(SCHED_THREADED)
                                  veth_poll()
                                    napi_complete_done()
                                    smp_store_mb(masked, false)
  store masked = true lands, overwrites false

The race needs all three: threaded NAPI, busy polling on the same
queue, and an awake kthread. A kthread that sleeps is woken by
wake_up_process(), which orders the stores. The result is the same
latch as in the previous patch. We found this by code review, not in
production. herd7 confirms it: with the barrier only in
__veth_xdp_flush(), the veth-busypoll-rearm litmus tests still reach
the latch. With the helper, they do not. The tests are here:

  https://github.com/netoptimizer/veth-backpressure-performance-testing/pull/15

Move the schedule sequence into a helper, veth_napi_schedule(), and use
it in both places. Both callers schedule NAPI the same way, so the
ordering rule now lives in one place.

No functional change for __veth_xdp_flush().

Fixes: 68468d8c4cd4 ("veth: fix races around rq->rx_notify_masked")
Signed-off-by: Jesper Dangaard Brouer <hawk@kernel.org>
---
 drivers/net/veth.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/drivers/net/veth.c b/drivers/net/veth.c
index 0988c7aadb5e..b0fd49dc839a 100644
--- a/drivers/net/veth.c
+++ b/drivers/net/veth.c
@@ -299,17 +299,23 @@ static void veth_ptr_free(void *ptr)
 		kfree_skb(ptr);
 }
 
+static void veth_napi_schedule(struct veth_rq *rq)
+{
+	if (!napi_schedule_prep(&rq->xdp_napi))
+		return;
+
+	WRITE_ONCE(rq->rx_notify_masked, true);
+	/* Order rx_notify_masked before NAPI_STATE_SCHED_THREADED */
+	smp_wmb();
+	__napi_schedule(&rq->xdp_napi);
+}
+
 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) &&
-	    napi_schedule_prep(&rq->xdp_napi)) {
-		WRITE_ONCE(rq->rx_notify_masked, true);
-		/* Order rx_notify_masked before NAPI_STATE_SCHED_THREADED */
-		smp_wmb();
-		__napi_schedule(&rq->xdp_napi);
-	}
+	if (!READ_ONCE(rq->rx_notify_masked))
+		veth_napi_schedule(rq);
 }
 
 static int veth_xdp_rx(struct veth_rq *rq, struct sk_buff *skb)
@@ -1001,12 +1007,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 */
-- 
2.43.0


      parent reply	other threads:[~2026-11-30  9:40 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  9:39 [PATCH net 0/3] veth: fix lost NAPI wakeup with threaded NAPI hawk
2026-11-30  9:39 ` [PATCH net 1/3] veth: enable dev_watchdog and recover stalled peer NAPI hawk
2026-11-30  9:39 ` [PATCH net 2/3] veth: order rx_notify_masked store before __napi_schedule() hawk
2026-11-30  9:40 ` hawk [this message]

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=20261130094000.2022376-4-hawk@kernel.org \
    --to=hawk@kernel.org \
    --cc=andrew+netdev@lunn.ch \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=john.fastabend@gmail.com \
    --cc=kernel-team@cloudflare.com \
    --cc=kuba@kernel.org \
    --cc=matt@readmodwrite.com \
    --cc=mkroemeke@cloudflare.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sdf@fomichev.me \
    --cc=simon.schippers@tu-dortmund.de \
    --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