Skip to contentSkip to conversation

sfc: handle NULL returned by xdp_convert_buff_to_frame()

PATCH In review Chenyuan Yang 2 participants 2 messages Jul 26, 2025 – Jul 31, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 sfc: handle NULL returned by xdp_convert_buff_to_frame()

The xdp_convert_buff_to_frame() function can return NULL when there is insufficient headroom in the buffer to store the xdp_frame structure or when the driver didn't reserve enough tailroom for skb_shared_info.

Currently, the sfc driver does not check for this NULL return value in the XDP_TX case within efx_do_xdp(). While the efx_xdp_tx_buffers() function has some defensive checks, passing a NULL xdpf can still lead to undefined behavior when the function tries to access xdpf->len and xdpf->data.

Fix by adding a proper NULL check in the XDP_TX case, following the suggestions of the developers.

Fixes 1b698fa5d8ef ("xdp: Rename convert_to_xdp_frame in xdp_convert_buff_to_frame")
Cc Kunwu Chan <[email protected]>
Cc Edward Cree <[email protected]>
---
 drivers/net/ethernet/sfc/rx.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/net/ethernet/sfc/rx.c +9 -3
index ffca82207e47..b56457c23f66 100644
@@ -308,14 +308,20 @@ static bool efx_do_xdp(struct efx_nic *efx, struct efx_channel *channel,
 	case XDP_TX:
 		/* Buffer ownership passes to tx on success. */
 		xdpf = xdp_convert_buff_to_frame(&xdp);
-		err = efx_xdp_tx_buffers(efx, 1, &xdpf, true);
+		if (unlikely(!xdpf))
+			err = -ENOBUFS;
+		else
+			err = efx_xdp_tx_buffers(efx, 1, &xdpf, true);
+
 		if (unlikely(err != 1)) {
 			efx_free_rx_buffers(rx_queue, rx_buf, 1);
 			if (net_ratelimit())
 				netif_err(efx, rx_err, efx->net_dev,
-					  "XDP TX failed (%d)\n", err);
+					  "XDP TX failed (%d)%s\n", err,
+					  err == -ENOBUFS ? " [frame conversion]" : "");
 			channel->n_rx_xdp_bad_drops++;
-			trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
+			if (err != -ENOBUFS)
+				trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
 		} else {
 			channel->n_rx_xdp_tx++;
 		}
Signature · 1 line
2.34.1
#1 lore raw
Quoted 10 lines · Chenyuan Yang

The xdp_convert_buff_to_frame() function can return NULL when there is insufficient headroom in the buffer to store the xdp_frame structure or when the driver didn't reserve enough tailroom for skb_shared_info.

Currently, the sfc driver does not check for this NULL return value in the XDP_TX case within efx_do_xdp(). While the efx_xdp_tx_buffers() function has some defensive checks, passing a NULL xdpf can still lead to undefined behavior when the function tries to access xdpf->len and xdpf->data.

The XDP_TX case is only for driver local frames/packets. And Edward says the sfc driver reserves both enough headroom and tailroom.

Quoted 2 lines

Fix by adding a proper NULL check in the XDP_TX case, following the suggestions of the developers.

In [V1] reply I question if this is possible
- [v1]
https://lore.kernel.org/all/[email protected]/

Hmm... have you actually tested that XDP/BPF can adjust headroom so much that xdp_convert_buff_to_frame() function fails?

I really doubt this possible for BPF-progs to violate this.

The XDP BPF-prog can only adjust the headroom via the helpers bpf_xdp_adjust_head() and bpf_xdp_adjust_meta(). These helpers reserve room for sizeof(struct xdp_frame).

The tailroom can be adjusted via helper bpf_xdp_adjust_tail() and it also reserve room for sizeof(struct skb_shared_info) such that BPF-progs cannot get access to this area. See define for xdp_data_hard_end.

Quoted 36 lines
Fixes 1b698fa5d8ef ("xdp: Rename convert_to_xdp_frame in xdp_convert_buff_to_frame")
Cc Kunwu Chan <[email protected]>
Cc Edward Cree <[email protected]>
---
  drivers/net/ethernet/sfc/rx.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
drivers/net/ethernet/sfc/rx.c +9 -3
index ffca82207e47..b56457c23f66 100644
@@ -308,14 +308,20 @@ static bool efx_do_xdp(struct efx_nic *efx, struct efx_channel *channel,
  	case XDP_TX:
  		/* Buffer ownership passes to tx on success. */
  		xdpf = xdp_convert_buff_to_frame(&xdp);
-		err = efx_xdp_tx_buffers(efx, 1, &xdpf, true);
+		if (unlikely(!xdpf))
+			err = -ENOBUFS;
+		else
+			err = efx_xdp_tx_buffers(efx, 1, &xdpf, true);
+
  		if (unlikely(err != 1)) {
  			efx_free_rx_buffers(rx_queue, rx_buf, 1);
  			if (net_ratelimit())
  				netif_err(efx, rx_err, efx->net_dev,
-					  "XDP TX failed (%d)\n", err);
+					  "XDP TX failed (%d)%s\n", err,
+					  err == -ENOBUFS ? " [frame conversion]" : "");
  			channel->n_rx_xdp_bad_drops++;
-			trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
+			if (err != -ENOBUFS)
+				trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
  		} else {
  			channel->n_rx_xdp_tx++;
  		}