From: Jesper Dangaard Brouer 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 --- 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