Skip to content

Commit a1356ac

Browse files
e.kubanskikuba-moo
authored andcommitted
xsk: Fix race condition in AF_XDP generic RX path
Move rx_lock from xsk_socket to xsk_buff_pool. Fix synchronization for shared umem mode in generic RX path where multiple sockets share single xsk_buff_pool. RX queue is exclusive to xsk_socket, while FILL queue can be shared between multiple sockets. This could result in race condition where two CPU cores access RX path of two different sockets sharing the same umem. Protect both queues by acquiring spinlock in shared xsk_buff_pool. Lock contention may be minimized in the future by some per-thread FQ buffering. It's safe and necessary to move spin_lock_bh(rx_lock) after xsk_rcv_check(): * xs->pool and spinlock_init is synchronized by xsk_bind() -> xsk_is_bound() memory barriers. * xsk_rcv_check() may return true at the moment of xsk_release() or xsk_unbind_dev(), however this will not cause any data races or race conditions. xsk_unbind_dev() removes xdp socket from all maps and waits for completion of all outstanding rx operations. Packets in RX path will either complete safely or drop. Signed-off-by: Eryk Kubanski <[email protected]> Fixes: bf0bdd1 ("xdp: fix race on generic receive path") Acked-by: Magnus Karlsson <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 087a9eb commit a1356ac

File tree

4 files changed

+6
-6
lines changed

4 files changed

+6
-6
lines changed

include/net/xdp_sock.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ struct xdp_sock {
7171
*/
7272
u32 tx_budget_spent;
7373

74-
/* Protects generic receive. */
75-
spinlock_t rx_lock;
76-
7774
/* Statistics */
7875
u64 rx_dropped;
7976
u64 rx_queue_full;

include/net/xsk_buff_pool.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ struct xsk_buff_pool {
5353
refcount_t users;
5454
struct xdp_umem *umem;
5555
struct work_struct work;
56+
/* Protects generic receive in shared and non-shared umem mode. */
57+
spinlock_t rx_lock;
5658
struct list_head free_list;
5759
struct list_head xskb_list;
5860
u32 heads_cnt;

net/xdp/xsk.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,14 @@ int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
338338
u32 len = xdp_get_buff_len(xdp);
339339
int err;
340340

341-
spin_lock_bh(&xs->rx_lock);
342341
err = xsk_rcv_check(xs, xdp, len);
343342
if (!err) {
343+
spin_lock_bh(&xs->pool->rx_lock);
344344
err = __xsk_rcv(xs, xdp, len);
345345
xsk_flush(xs);
346+
spin_unlock_bh(&xs->pool->rx_lock);
346347
}
347-
spin_unlock_bh(&xs->rx_lock);
348+
348349
return err;
349350
}
350351

@@ -1734,7 +1735,6 @@ static int xsk_create(struct net *net, struct socket *sock, int protocol,
17341735
xs = xdp_sk(sk);
17351736
xs->state = XSK_READY;
17361737
mutex_init(&xs->mutex);
1737-
spin_lock_init(&xs->rx_lock);
17381738

17391739
INIT_LIST_HEAD(&xs->map_list);
17401740
spin_lock_init(&xs->map_list_lock);

net/xdp/xsk_buff_pool.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
8989
pool->addrs = umem->addrs;
9090
pool->tx_metadata_len = umem->tx_metadata_len;
9191
pool->tx_sw_csum = umem->flags & XDP_UMEM_TX_SW_CSUM;
92+
spin_lock_init(&pool->rx_lock);
9293
INIT_LIST_HEAD(&pool->free_list);
9394
INIT_LIST_HEAD(&pool->xskb_list);
9495
INIT_LIST_HEAD(&pool->xsk_tx_list);

0 commit comments

Comments
 (0)