UMEM registration still refuses XDP_UMEM_EXPLICIT_RETURN. Accept it with aligned page-sized chunks and no headroom, and install the provider when a socket with such a UMEM binds. A bind that cannot install it fails rather than fall back. Only the first socket with an RX ring receives, and later ones get -EBUSY until the provider goes, so the NAPI never sees the receiver replaced. Sockets sharing the UMEM may transmit, in copy mode. The provider goes with the queue's last socket, from the freezable buffer pool teardown, so unbind never restarts the queue of a suspended device. Until then, a queue without a receiver drops what it redirects. Returns need no wakeup, so recvmsg and poll wake the NAPI only for an empty FILL ring. A wakeup fails with -ENETDOWN once the provider or NAPI is gone. Once its fd is closed and its rings unmapped, the receiving socket waits out the NAPI polls and hand-offs that may write to its RX ring. Later hand-offs return chunks without descriptors. Signed-off-by: Björn Töpel --- Notes (v2changes): Changes in v2: - Rename from "xsk: Receive in the device's buffer layout". - Move the XDP_USE_NEED_WAKEUP requirement to patch 5 (Magnus). - Let the provider go with the last socket of the queue, from the deferred teardown of the buffer pool on a freezable workqueue, so that unbind never restarts the queue of a suspended device. v1 let it go with the receiving socket, but a queue that only TX-only sockets used kept it until its pool went (Sashiko). A program detach right after the last close may now fail with EBUSY for a short time. - Hand-offs and wakeups follow the single owner of a queue, see patch 6; need_wakeup reports only an empty FILL ring. Read the NAPI of the queue under RCU, and mark its writes (Sashiko). - Once the receiving socket closes, give the chunks that the kernel owes it back to the application without descriptors, and drop what the queue redirects until the provider goes (Sashiko). include/net/netdev_rx_queue.h | 5 +++ net/core/dev.c | 3 +- net/xdp/xdp_umem.c | 5 +++ net/xdp/xsk.c | 82 ++++++++++++++++++++++++++++----- net/xdp/xsk_mp.c | 85 ++++++++++++++++++++++++++++++++--- net/xdp/xsk_mp.h | 4 +- 6 files changed, 163 insertions(+), 21 deletions(-) diff --git a/include/net/netdev_rx_queue.h b/include/net/netdev_rx_queue.h index 9415a94d333d..c5cc9d10f0c2 100644 --- a/include/net/netdev_rx_queue.h +++ b/include/net/netdev_rx_queue.h @@ -28,6 +28,11 @@ struct netdev_rx_queue { #ifdef CONFIG_XDP_SOCKETS struct xsk_buff_pool *pool; #endif + /* Readers without the instance lock, such as AF_XDP wakeups, use + * READ_ONCE() under RCU. Drivers clear it before netif_napi_del(), + * and wait for a grace period before they free the NAPI, as they + * already must for napi_by_id(), so the NAPI stays valid for them. + */ struct napi_struct *napi; struct netdev_queue_config qcfg; struct pp_memory_provider_params mp_params; diff --git a/net/core/dev.c b/net/core/dev.c index 12e852cc3855..42ed8e2514d2 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -7631,7 +7631,8 @@ void netif_queue_set_napi(struct net_device *dev, unsigned int queue_index, switch (type) { case NETDEV_QUEUE_TYPE_RX: rxq = __netif_get_rx_queue(dev, queue_index); - rxq->napi = napi; + /* See &netdev_rx_queue.napi */ + WRITE_ONCE(rxq->napi, napi); return; case NETDEV_QUEUE_TYPE_TX: txq = netdev_get_tx_queue(dev, queue_index); diff --git a/net/xdp/xdp_umem.c b/net/xdp/xdp_umem.c index de8a2747ac9f..84caa001ff6a 100644 --- a/net/xdp/xdp_umem.c +++ b/net/xdp/xdp_umem.c @@ -145,6 +145,7 @@ static int xdp_umem_account_pages(struct xdp_umem *umem) XDP_UMEM_UNALIGNED_CHUNK_FLAG | \ XDP_UMEM_TX_SW_CSUM | \ XDP_UMEM_TX_METADATA_LEN | \ + XDP_UMEM_EXPLICIT_RETURN | \ 0) static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr) @@ -198,6 +199,10 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr) if (headroom > chunk_size - XDP_PACKET_HEADROOM - 128) return -EINVAL; + if (mr->flags & XDP_UMEM_EXPLICIT_RETURN && + (unaligned_chunks || chunk_size != PAGE_SIZE || headroom)) + return -EINVAL; + if (mr->flags & XDP_UMEM_TX_METADATA_LEN) { if (mr->tx_metadata_len >= 256 || mr->tx_metadata_len % 8) return -EINVAL; diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c index 482bb2cd94bf..33d4cbee658d 100644 --- a/net/xdp/xsk.c +++ b/net/xdp/xsk.c @@ -55,7 +55,8 @@ void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool) return; pool->fq->ring->flags |= XDP_RING_NEED_WAKEUP; - pool->cached_need_wakeup |= XDP_WAKEUP_RX; + WRITE_ONCE(pool->cached_need_wakeup, + pool->cached_need_wakeup | XDP_WAKEUP_RX); } EXPORT_SYMBOL(xsk_set_rx_need_wakeup); @@ -72,7 +73,8 @@ void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool) } rcu_read_unlock(); - pool->cached_need_wakeup |= XDP_WAKEUP_TX; + WRITE_ONCE(pool->cached_need_wakeup, + pool->cached_need_wakeup | XDP_WAKEUP_TX); } EXPORT_SYMBOL(xsk_set_tx_need_wakeup); @@ -82,7 +84,8 @@ void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool) return; pool->fq->ring->flags &= ~XDP_RING_NEED_WAKEUP; - pool->cached_need_wakeup &= ~XDP_WAKEUP_RX; + WRITE_ONCE(pool->cached_need_wakeup, + pool->cached_need_wakeup & ~XDP_WAKEUP_RX); } EXPORT_SYMBOL(xsk_clear_rx_need_wakeup); @@ -99,7 +102,8 @@ void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool) } rcu_read_unlock(); - pool->cached_need_wakeup &= ~XDP_WAKEUP_TX; + WRITE_ONCE(pool->cached_need_wakeup, + pool->cached_need_wakeup & ~XDP_WAKEUP_TX); } EXPORT_SYMBOL(xsk_clear_tx_need_wakeup); @@ -800,9 +804,16 @@ u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 nb_pkts) } EXPORT_SYMBOL(xsk_tx_peek_release_desc_batch); +/* Context: RCU read side, see xsk_mp_unbind() for a provider */ static int xsk_wakeup(struct xdp_sock *xs, u8 flags) { struct net_device *dev = xs->dev; + struct xsk_mp *mp; + + if (xsk_explicit_return(xs)) { + mp = READ_ONCE(xs->pool->mp); + return mp ? xsk_mp_wakeup(mp) : -ENETDOWN; + } return dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id, flags); } @@ -1404,7 +1415,7 @@ static int __xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len return 0; pool = xs->pool; - if (pool->cached_need_wakeup & XDP_WAKEUP_TX) { + if (READ_ONCE(pool->cached_need_wakeup) & XDP_WAKEUP_TX) { if (xs->zc) return xsk_wakeup(xs, XDP_WAKEUP_TX); return xsk_generic_xmit(sk); @@ -1444,7 +1455,9 @@ static int __xsk_recvmsg(struct socket *sock, struct msghdr *m, size_t len, int if (xsk_no_wakeup(sk)) return 0; - if (xs->pool->cached_need_wakeup & XDP_WAKEUP_RX && xs->zc) + /* A socket with explicit return receives without driver zero copy */ + if (READ_ONCE(xs->pool->cached_need_wakeup) & XDP_WAKEUP_RX && + (xs->zc || xsk_explicit_return(xs))) return xsk_wakeup(xs, XDP_WAKEUP_RX); return 0; } @@ -1467,6 +1480,7 @@ static __poll_t xsk_poll(struct file *file, struct socket *sock, struct sock *sk = sock->sk; struct xdp_sock *xs = xdp_sk(sk); struct xsk_buff_pool *pool; + u8 need_wakeup; sock_poll_wait(file, sock, wait); @@ -1475,13 +1489,20 @@ static __poll_t xsk_poll(struct file *file, struct socket *sock, goto out; pool = xs->pool; + /* Drivers change it from their NAPI */ + need_wakeup = READ_ONCE(pool->cached_need_wakeup); - if (pool->cached_need_wakeup) { - if (xs->zc) - xsk_wakeup(xs, pool->cached_need_wakeup); - else if (xs->tx) - /* Poll needs to drive Tx also in copy mode */ - xsk_generic_xmit(sk); + if (need_wakeup) { + if (xs->zc) { + xsk_wakeup(xs, need_wakeup); + } else { + if (xsk_explicit_return(xs) && + need_wakeup & XDP_WAKEUP_RX) + xsk_wakeup(xs, XDP_WAKEUP_RX); + if (xs->tx) + /* Poll needs to drive Tx also in copy mode */ + xsk_generic_xmit(sk); + } } if (xs->rx && !xskq_prod_is_empty(xs->rx)) @@ -1571,6 +1592,28 @@ static void xsk_delete_from_maps(struct xdp_sock *xs) } } +/* The pool, and with it the provider, may outlive the socket, see + * xp_put_pool() + */ +static void xsk_mp_release(struct xdp_sock *xs) +{ + struct xsk_buff_pool *pool = READ_ONCE(xs->pool); + struct net_device *dev; + + if (!pool || !READ_ONCE(pool->mp)) + return; + + rtnl_lock(); + dev = pool->netdev; + if (dev) { + netdev_lock_ops(dev); + if (pool->mp) + xsk_mp_del_sock(pool->mp, xs); + netdev_unlock_ops(dev); + } + rtnl_unlock(); +} + static int xsk_release(struct socket *sock) { struct sock *sk = sock->sk; @@ -1592,6 +1635,7 @@ static int xsk_release(struct socket *sock) sock_prot_inuse_add(net, sk->sk_prot, -1); xsk_delete_from_maps(xs); + xsk_mp_release(xs); mutex_lock(&xs->mutex); xsk_unbind_dev(xs); mutex_unlock(&xs->mutex); @@ -1744,6 +1788,18 @@ static int xsk_bind(struct socket *sock, struct sockaddr_unsized *addr, int addr goto out_unlock; } + /* A queue with explicit return has one + * receiving socket, and none once that one went + */ + if (xs->rx && + umem_xs->umem->flags & XDP_UMEM_EXPLICIT_RETURN && + (!umem_xs->pool->mp || + xsk_mp_rx_taken(umem_xs->pool->mp))) { + err = -EBUSY; + sockfd_put(sock); + goto out_unlock; + } + xp_get_pool(umem_xs->pool); xs->pool = umem_xs->pool; @@ -1796,6 +1852,8 @@ static int xsk_bind(struct socket *sock, struct sockaddr_unsized *addr, int addr xs->sg = !!(xs->umem->flags & XDP_UMEM_SG_FLAG); xs->queue_id = qid; xp_add_xsk(xs->pool, xs); + if (xs->pool->mp) + xsk_mp_add_sock(xs->pool->mp, xs); if (qid < dev->real_num_rx_queues) { struct netdev_rx_queue *rxq; diff --git a/net/xdp/xsk_mp.c b/net/xdp/xsk_mp.c index f7db0d6f788a..fa5e715233ad 100644 --- a/net/xdp/xsk_mp.c +++ b/net/xdp/xsk_mp.c @@ -93,8 +93,10 @@ struct xsk_mp { * for, see xsk_mp_rcv() */ u32 owed; - /* Set once: see xsk_mp_add_sock() */ + /* The receiving socket, see xsk_mp_add_sock() */ struct xdp_sock *xs; + /* A socket received on the queue, so no other may */ + u8 rx_taken; /* One node per chunk: chunks that page pools released, from any * context */ @@ -385,10 +387,27 @@ static bool xsk_mp_hand_off(struct page_pool *pp, netmem_ref netmem) struct xdp_sock *xs; niov->mp_hand_off = 0; - /* A buffer of the chunk reached the socket, which stays */ + /* An owner outside the NAPI, as a queue restart or an RX error + * recovery, runs in process context. The read section keeps the + * socket and its RX ring until this is done, see xsk_mp_del_sock(). + */ + rcu_read_lock(); xs = READ_ONCE(mp->xs); + if (!xs) { + /* Without a receiver the chunk is the application's again, + * and counts against @pp no longer. @pp, and with it the + * provider, may go once it does, so touch neither after that. + */ + /* Pairs with xsk_mp_take_fill() */ + smp_store_release(&niov->mp_state, XSK_MP_USER); + net_mp_release_page_pool(pp, 1); + rcu_read_unlock(); + return true; + } + xsk_mp_return(mp, xs, niov); xsk_mp_publish(mp, xs); + rcu_read_unlock(); return true; } @@ -410,7 +429,14 @@ static void xsk_mp_stop(struct xsk_mp *mp) WRITE_ONCE(mp->rxq, NULL); WRITE_ONCE(mp->fq, NULL); - /* Wait for wakeups that found the provider or the queue */ + /* Later hand-offs return chunks without descriptors, also if the + * receiving socket closes after its device went away and finds no + * provider to leave, see xsk_mp_release() + */ + WRITE_ONCE(mp->xs, NULL); + /* Wait for wakeups that found the provider or the queue, and for + * hand-offs that found the socket + */ synchronize_net(); if (fq) xp_dma_unmap(mp->pool, XSK_MP_DMA_ATTR); @@ -586,18 +612,39 @@ int xsk_mp_bind(struct xsk_buff_pool *pool, struct net_device *dev, */ void xsk_mp_add_sock(struct xsk_mp *mp, struct xdp_sock *xs) { - if (xs->rx && !mp->xs) { + if (xs->rx && !mp->rx_taken) { + mp->rx_taken = 1; /* The NAPI acquires the socket once it is set up */ smp_store_release(&mp->xs, xs); } } -/* The receiving socket, if any. +/* Whether a socket received on the queue. * Context: RTNL and the instance lock of the device */ -struct xdp_sock *xsk_mp_sock(struct xsk_mp *mp) +bool xsk_mp_rx_taken(struct xsk_mp *mp) { - return mp->xs; + return mp->rx_taken; +} + +/* The provider stays until the pool goes, see xp_release_deferred(), so + * that the queue restarts from a freezable workqueue. A receiving socket + * that goes before leaves the queue to drop what it redirects, and + * xsk_unbind_dev() waits for the NAPI before its RX ring goes. + * + * Context: RTNL and the instance lock of the device + */ +void xsk_mp_del_sock(struct xsk_mp *mp, struct xdp_sock *xs) +{ + if (mp->xs != xs) + return; + WRITE_ONCE(mp->xs, NULL); + /* Waits for the NAPI polls and the hand-offs that may still write to + * the RX ring of the socket. Later hand-offs give chunks back without + * a descriptor, so the kernel owes the socket nothing. + */ + synchronize_net(); + mp->owed = 0; } /* Context: RTNL and the instance lock of the device */ @@ -618,6 +665,30 @@ void xsk_mp_unbind(struct xsk_buff_pool *pool) xsk_mp_detach(pool); } +/* Schedules the queue NAPI, which refills from FILL. If the NAPI runs, + * NAPI_STATE_MISSED makes it poll again. Returned chunks need no wakeup, as + * their owner posts them as it lets go of them. + * + * Return: 0, or -ENETDOWN without a NAPI. + */ +int xsk_mp_wakeup(struct xsk_mp *mp) +{ + struct netdev_rx_queue *rxq; + struct napi_struct *napi; + + /* See &netdev_rx_queue.napi */ + rcu_read_lock(); + rxq = READ_ONCE(mp->rxq); + napi = rxq ? READ_ONCE(rxq->napi) : NULL; + if (napi) { + local_bh_disable(); + napi_schedule(napi); + local_bh_enable(); + } + rcu_read_unlock(); + return napi ? 0 : -ENETDOWN; +} + /* The net_iov of @netmem if it is one of ours; tells by its address */ static struct net_iov *xsk_mp_niov(struct xsk_mp *mp, netmem_ref netmem) { diff --git a/net/xdp/xsk_mp.h b/net/xdp/xsk_mp.h index 46d73fdb40a1..b2e3909f282f 100644 --- a/net/xdp/xsk_mp.h +++ b/net/xdp/xsk_mp.h @@ -12,8 +12,10 @@ struct xsk_mp; int xsk_mp_bind(struct xsk_buff_pool *pool, struct net_device *dev, u16 queue_id, u16 flags); void xsk_mp_unbind(struct xsk_buff_pool *pool); +int xsk_mp_wakeup(struct xsk_mp *mp); void xsk_mp_flush(struct xsk_mp *mp); void xsk_mp_add_sock(struct xsk_mp *mp, struct xdp_sock *xs); -struct xdp_sock *xsk_mp_sock(struct xsk_mp *mp); +void xsk_mp_del_sock(struct xsk_mp *mp, struct xdp_sock *xs); +bool xsk_mp_rx_taken(struct xsk_mp *mp); #endif /* XSK_MP_H_ */ -- 2.56.1