Skip to contentSkip to conversation

xsk: Fixes for AF_XDP fragment handling

PATCH Superseded Nikhil P. Rao 3 participants 5 messages Feb 17

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover xsk: Fixes for AF_XDP fragment handling

This series fixes two issues in AF_XDP zero-copy fragment handling:

Patch 1 fixes a buffer leak caused by incorrect list node handling after commit b692bf9a7543. The list_node field is now reused for both the xskb pool list and the buffer free list. Using list_del() instead of list_del_init() causes list_empty() checks in xp_free() to fail, preventing buffers from being added to the free list.

Patch 2 fixes partial packet delivery to userspace. In the zero-copy path, if the Rx queue fills up while enqueuing fragments, the remaining fragments are dropped, causing the application to receive incomplete packets. The fix ensures the Rx queue has sufficient space for all fragments before starting to enqueue them.

v3 changes:
- Patch 1: Carried Acked-by tags from v1 on patch 1
- Patch 2:
* Check for free space only for the multi-buffer case, this preserves single buffer performance (Maciej)
* Fix return without freeing buffer when sufficient space for all fragments is not available

v2 changes:
- Fix indentation issue reported by kernel test robot [1]

[1] https://lore.kernel.org/oe-kbuild-all/[email protected]/

Nikhil P. Rao (2):
xsk: Fix fragment node deletion to prevent buffer leak
xsk: Fix zero-copy AF_XDP fragment drop

 include/net/xdp_sock_drv.h |  6 +++---
 net/xdp/xsk.c              | 24 +++++++++++++++---------
 2 files changed, 18 insertions(+), 12 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/2 xsk: Fix fragment node deletion to prevent buffer leak

After commit b692bf9a7543 ("xsk: Get rid of xdp_buff_xsk::xskb_list_node"), the list_node field is reused for both the xskb pool list and the buffer free list, this causes a buffer leak as described below.

xp_free() checks if a buffer is already on the free list using list_empty(&xskb->list_node). When list_del() is used to remove a node from the xskb pool list, it doesn't reinitialize the node pointers. This means list_empty() will return false even after the node has been removed, causing xp_free() to incorrectly skip adding the buffer to the free list.

Fix this by using list_del_init() instead of list_del() in all fragment handling paths, this ensures the list node is reinitialized after removal, allowing the list_empty() to work correctly.

Fixes b692bf9a7543 ("xsk: Get rid of xdp_buff_xsk::xskb_list_node")
---
 include/net/xdp_sock_drv.h | 6 +++---
 net/xdp/xsk.c              | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
include/net/xdp_sock_drv.h +3 -3
index 242e34f771cc..aefc368449d5 100644
@@ -122,7 +122,7 @@ static inline void xsk_buff_free(struct xdp_buff *xdp)
 		goto out;
 
 	list_for_each_entry_safe(pos, tmp, xskb_list, list_node) {
-		list_del(&pos->list_node);
+		list_del_init(&pos->list_node);
 		xp_free(pos);
 	}
 
@@ -157,7 +157,7 @@ static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first)
 	frag = list_first_entry_or_null(&xskb->pool->xskb_list,
 					struct xdp_buff_xsk, list_node);
 	if (frag) {
-		list_del(&frag->list_node);
+		list_del_init(&frag->list_node);
 		ret = &frag->xdp;
 	}
 
@@ -168,7 +168,7 @@ static inline void xsk_buff_del_frag(struct xdp_buff *xdp)
 {
 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
 
-	list_del(&xskb->list_node);
+	list_del_init(&xskb->list_node);
 }
 
 static inline struct xdp_buff *xsk_buff_get_head(struct xdp_buff *first)
net/xdp/xsk.c +1 -1
index f093c3453f64..f2ec4f78bbb6 100644
@@ -186,7 +186,7 @@ static int xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
 		err = __xsk_rcv_zc(xs, pos, len, contd);
 		if (err)
 			goto err;
-		list_del(&pos->list_node);
+		list_del_init(&pos->list_node);
 	}
 
 	return 0;
Signature · 1 line
2.43.0
#2 lore raw

PATCH v3 2/2 xsk: Fix zero-copy AF_XDP fragment drop

In reply to Nikhil P. Rao

AF_XDP should ensure that only a complete packet is sent to application. In the zero-copy case, if the Rx queue gets full as fragments are being enqueued, the remaining fragments are dropped.

For the multi-buffer case, add a check to ensure that the Rx queue has enough space for all fragments of a packet before starting to enqueue them.

Fixes 24ea50127ecf ("xsk: support mbuf on ZC RX")
---
 net/xdp/xsk.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
net/xdp/xsk.c +14 -8
index f2ec4f78bbb6..78ca343de080 100644
@@ -167,25 +167,31 @@ static int xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
 	struct xdp_buff_xsk *pos, *tmp;
 	struct list_head *xskb_list;
 	u32 contd = 0;
+	u32 num_desc;
 	int err;
 
-	if (frags)
+	if (frags) {
+		num_desc = xdp_get_shared_info_from_buff(xdp)->nr_frags + 1;
 		contd = XDP_PKT_CONTD;
+	} else {
+		err = __xsk_rcv_zc(xs, xskb, len, contd);
+		if (err)
+			goto err;
+		return 0;
+	}
 
-	err = __xsk_rcv_zc(xs, xskb, len, contd);
-	if (err)
+	if (xskq_prod_nb_free(xs->rx, num_desc) < num_desc) {
+		xs->rx_queue_full++;
 		goto err;
-	if (likely(!frags))
-		return 0;
+	}
 
+	__xsk_rcv_zc(xs, xskb, len, contd);
 	xskb_list = &xskb->pool->xskb_list;
 	list_for_each_entry_safe(pos, tmp, xskb_list, list_node) {
 		if (list_is_singular(xskb_list))
 			contd = 0;
 		len = pos->xdp.data_end - pos->xdp.data;
-		err = __xsk_rcv_zc(xs, pos, len, contd);
-		if (err)
-			goto err;
+		__xsk_rcv_zc(xs, pos, len, contd);
 		list_del_init(&pos->list_node);
 	}
 
Signature · 1 line
2.43.0
maintainer Acked-by on v3 2/2
#3 lore raw

Re: [PATCH net v3 2/2] xsk: Fix zero-copy AF_XDP fragment drop

Quoted 10 lines · Nikhil P. Rao

AF_XDP should ensure that only a complete packet is sent to application. In the zero-copy case, if the Rx queue gets full as fragments are being enqueued, the remaining fragments are dropped.

For the multi-buffer case, add a check to ensure that the Rx queue has enough space for all fragments of a packet before starting to enqueue them.

Fixes 24ea50127ecf ("xsk: support mbuf on ZC RX")

Thanks!

Quoted 51 lines
---
 net/xdp/xsk.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
net/xdp/xsk.c +14 -8
index f2ec4f78bbb6..78ca343de080 100644
@@ -167,25 +167,31 @@ static int xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
 	struct xdp_buff_xsk *pos, *tmp;
 	struct list_head *xskb_list;
 	u32 contd = 0;
+	u32 num_desc;
 	int err;
 
-	if (frags)
+	if (frags) {
+		num_desc = xdp_get_shared_info_from_buff(xdp)->nr_frags + 1;
 		contd = XDP_PKT_CONTD;
+	} else {
+		err = __xsk_rcv_zc(xs, xskb, len, contd);
+		if (err)
+			goto err;
+		return 0;
+	}
 
-	err = __xsk_rcv_zc(xs, xskb, len, contd);
-	if (err)
+	if (xskq_prod_nb_free(xs->rx, num_desc) < num_desc) {
+		xs->rx_queue_full++;
 		goto err;
-	if (likely(!frags))
-		return 0;
+	}
 
+	__xsk_rcv_zc(xs, xskb, len, contd);
 	xskb_list = &xskb->pool->xskb_list;
 	list_for_each_entry_safe(pos, tmp, xskb_list, list_node) {
 		if (list_is_singular(xskb_list))
 			contd = 0;
 		len = pos->xdp.data_end - pos->xdp.data;
-		err = __xsk_rcv_zc(xs, pos, len, contd);
-		if (err)
-			goto err;
+		__xsk_rcv_zc(xs, pos, len, contd);
 		list_del_init(&pos->list_node);
 	}
 
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH net v3 2/2] xsk: Fix zero-copy AF_XDP fragment drop

In reply to Nikhil P. Rao
kernel test robot noticed the following build warnings:

Hi Nikhil,

kernel test robot noticed the following build warnings:

[auto build test WARNING on net/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Nikhil-P-Rao/xsk-Fix-fragment-node-deletion-to-prevent-buffer-leak/20260217-092448
base:   net/main
patch link:    https://lore.kernel.org/r/20260217012346.22468-3-nikhil.rao%40amd.com
patch subject: [PATCH net v3 2/2] xsk: Fix zero-copy AF_XDP fragment drop
config: powerpc64-randconfig-002-20260217 (https://download.01.org/0day-ci/archive/20260217/[email protected]/config)
compiler: clang version 23.0.0git (https://github.com/llvm/llvm-project e86750b29fa0ff207cd43213d66dabe565417638)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260217/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> net/xdp/xsk.c:183:6: warning: variable 'err' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
     183 |         if (xskq_prod_nb_free(xs->rx, num_desc) < num_desc) {
         |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   net/xdp/xsk.c:201:9: note: uninitialized use occurs here
     201 |         return err;
         |                ^~~
   net/xdp/xsk.c:183:2: note: remove the 'if' if its condition is always false
     183 |         if (xskq_prod_nb_free(xs->rx, num_desc) < num_desc) {
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     184 |                 xs->rx_queue_full++;
         |                 ~~~~~~~~~~~~~~~~~~~~
     185 |                 goto err;
         |                 ~~~~~~~~~
     186 |         }
         |         ~
   net/xdp/xsk.c:171:9: note: initialize the variable 'err' to silence this warning
     171 |         int err;
         |                ^
         |                 = 0

1 warning generated.

vim +183 net/xdp/xsk.c

162
163 static int xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
164 {
165 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
166 u32 frags = xdp_buff_has_frags(xdp);
167 struct xdp_buff_xsk *pos, *tmp;
168 struct list_head *xskb_list;
169 u32 contd = 0;
170 u32 num_desc;
171 int err;
172
173 if (frags) {
174 num_desc = xdp_get_shared_info_from_buff(xdp)->nr_frags + 1;
175 contd = XDP_PKT_CONTD;
176 } else {
177 err = __xsk_rcv_zc(xs, xskb, len, contd);
178 if (err)
179 goto err;
180 return 0;
181 }
182

 > 183		if (xskq_prod_nb_free(xs->rx, num_desc) < num_desc) {

184 xs->rx_queue_full++;
185 goto err;
186 }
187
188 __xsk_rcv_zc(xs, xskb, len, contd);
189 xskb_list = &xskb->pool->xskb_list;
190 list_for_each_entry_safe(pos, tmp, xskb_list, list_node) {
191 if (list_is_singular(xskb_list))
192 contd = 0;
193 len = pos->xdp.data_end - pos->xdp.data;
194 __xsk_rcv_zc(xs, pos, len, contd);
195 list_del_init(&pos->list_node);
196 }
197
198 return 0;
199 err:
200 xsk_buff_free(xdp);
201 return err;
202 }
203

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki