From nobody Thu Apr 2 14:11:19 2026 Received: from mail115-63.sinamail.sina.com.cn (mail115-63.sinamail.sina.com.cn [218.30.115.63]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 381DF33F38D for ; Tue, 10 Feb 2026 09:13:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=218.30.115.63 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770714791; cv=none; b=j1MAtp5ZMB1bvs/5BQ6UUlzH8NtcZhhUkVxfIJJS2XcvOJcWPKNV96gOuobDFmAABpmAVOG7GjiPW08R8NEa326BxVvlB1duO3Ntl8jpKl8UYu14f74nwTi1+CNi/3caLqPpt5LBXkxZr/WJ6/oWaYLYDKSwMunsvoQjwyFnmTo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770714791; c=relaxed/simple; bh=RW9o8qXMDmFr2cTNtgGcRS0WMtbL2a+7DAZq/tKMdWU=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=kjT6MZqIGhd6nZ0PPZM8XXvdt9NTksSgkkzQKocyvnMsfrDDtQaFsqOccy8cMO8Y0YK6Yy1Euj/ebiWxEl3I5OMdGgaCHWCz2/I+ghtSG7HDalRxGVE2y9aopnkwXM/Q9ezEZhWZyKW4biVqBxU5xBUiSd1hN8Bag7mqNOAU61I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sina.cn; spf=pass smtp.mailfrom=sina.cn; dkim=pass (1024-bit key) header.d=sina.cn header.i=@sina.cn header.b=E+yz8YdV; arc=none smtp.client-ip=218.30.115.63 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sina.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sina.cn Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=sina.cn header.i=@sina.cn header.b="E+yz8YdV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sina.cn; s=201208; t=1770714787; bh=AwwrFyXVRggsvgwH85FXnzJjrnjYzB5017/O7KRuerI=; h=From:Subject:Date:Message-Id; b=E+yz8YdVgg+Yd9e3YgS56tVmTMGCet2CCC8yNXpYDAnXfhOafcy5fPNky9Ddinvsl U2grwilmu22UBUIGZAorsZG5j3JwNYgGQ0wJaTEh9a457I/7eQo63QuUjhsNuqfXY6 zfFJlm+gP0mNa14QmKY2jzOcLgWheBy0VqneOSy8= X-SMAIL-HELO: NTT-kernel-dev Received: from unknown (HELO NTT-kernel-dev)([60.247.85.88]) by sina.cn (10.185.250.22) with ESMTP id 698AF693000015A4; Tue, 10 Feb 2026 17:12:58 +0800 (CST) X-Sender: jianqkang@sina.cn X-Auth-ID: jianqkang@sina.cn Authentication-Results: sina.cn; spf=none smtp.mailfrom=jianqkang@sina.cn; dkim=none header.i=none; dmarc=none action=none header.from=jianqkang@sina.cn X-SMAIL-MID: 3316687602327 X-SMAIL-UIID: BA8516D35CD3404984D84B981A4C8F07-20260210-171258-1 From: Jianqiang kang To: gregkh@linuxfoundation.org, stable@vger.kernel.org, e.kubanski@partner.samsung.com Cc: patches@lists.linux.dev, linux-kernel@vger.kernel.org, bjorn@kernel.org, magnus.karlsson@intel.com, maciej.fijalkowski@intel.com, jonathan.lemon@gmail.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, ast@kernel.org, daniel@iogearbox.net, hawk@kernel.org, john.fastabend@gmail.com, i.maximets@samsung.com, netdev@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH 6.1.y] xsk: Fix race condition in AF_XDP generic RX path Date: Tue, 10 Feb 2026 17:12:51 +0800 Message-Id: <20260210091251.1690056-1-jianqkang@sina.cn> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "e.kubanski" [ Upstream commit a1356ac7749cafc4e27aa62c0c4604b5dca4983e ] 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 Fixes: bf0bdd1343efb ("xdp: fix race on generic receive path") Acked-by: Magnus Karlsson Link: https://patch.msgid.link/20250416101908.10919-1-e.kubanski@partner.sa= msung.com Signed-off-by: Jakub Kicinski [ Conflict is resolved when backporting this fix. ] Signed-off-by: Jianqiang kang --- include/net/xdp_sock.h | 2 -- include/net/xsk_buff_pool.h | 2 ++ net/xdp/xsk.c | 6 +++--- net/xdp/xsk_buff_pool.c | 1 + 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/include/net/xdp_sock.h b/include/net/xdp_sock.h index 3057e1a4a11c..b8001d24106a 100644 --- a/include/net/xdp_sock.h +++ b/include/net/xdp_sock.h @@ -59,8 +59,6 @@ struct xdp_sock { =20 struct xsk_queue *tx ____cacheline_aligned_in_smp; struct list_head tx_list; - /* Protects generic receive. */ - spinlock_t rx_lock; =20 /* Statistics */ u64 rx_dropped; diff --git a/include/net/xsk_buff_pool.h b/include/net/xsk_buff_pool.h index 996eaf1ef1a1..e436363c2192 100644 --- a/include/net/xsk_buff_pool.h +++ b/include/net/xsk_buff_pool.h @@ -48,6 +48,8 @@ struct xsk_buff_pool { refcount_t users; struct xdp_umem *umem; struct work_struct work; + /* Protects generic receive in shared and non-shared umem mode. */ + spinlock_t rx_lock; struct list_head free_list; u32 heads_cnt; u16 queue_id; diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c index e3bdfc517424..e55e153377c8 100644 --- a/net/xdp/xsk.c +++ b/net/xdp/xsk.c @@ -237,13 +237,14 @@ int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_b= uff *xdp) { int err; =20 - spin_lock_bh(&xs->rx_lock); err =3D xsk_rcv_check(xs, xdp); if (!err) { + spin_lock_bh(&xs->pool->rx_lock); err =3D __xsk_rcv(xs, xdp); xsk_flush(xs); + spin_unlock_bh(&xs->pool->rx_lock); } - spin_unlock_bh(&xs->rx_lock); + return err; } =20 @@ -1448,7 +1449,6 @@ static int xsk_create(struct net *net, struct socket = *sock, int protocol, xs =3D xdp_sk(sk); xs->state =3D XSK_READY; mutex_init(&xs->mutex); - spin_lock_init(&xs->rx_lock); =20 INIT_LIST_HEAD(&xs->map_list); spin_lock_init(&xs->map_list_lock); diff --git a/net/xdp/xsk_buff_pool.c b/net/xdp/xsk_buff_pool.c index 21d5fdba47c4..b2004a8bf67f 100644 --- a/net/xdp/xsk_buff_pool.c +++ b/net/xdp/xsk_buff_pool.c @@ -85,6 +85,7 @@ struct xsk_buff_pool *xp_create_and_assign_umem(struct xd= p_sock *xs, XDP_PACKET_HEADROOM; pool->umem =3D umem; pool->addrs =3D umem->addrs; + spin_lock_init(&pool->rx_lock); INIT_LIST_HEAD(&pool->free_list); INIT_LIST_HEAD(&pool->xsk_tx_list); spin_lock_init(&pool->xsk_tx_list_lock); --=20 2.34.1