Skip to contentSkip to conversation

xdp: pass flags to xdp_update_skb_shared_info() directly

RFC In review Jakub Kicinski 5 participants 8 messages Aug 12, 2025 – Aug 14, 2025

Subsystems (10)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

RFC 1/1 xdp: pass flags to xdp_update_skb_shared_info() directly

xdp_update_skb_shared_info() needs to update skb state which was maintained in xdp_buff / frame. Pass full flags into it, instead of breaking it out bit by bit. We will need to add a bit for unreadable frags (even tho XDP doesn't support those the driver paths may be common), at which point almost all call sites would become:

    xdp_update_skb_shared_info(skb, num_frags,
                               sinfo->xdp_frags_size,
                               MY_PAGE_SIZE * num_frags,
                               xdp_buff_is_frag_pfmemalloc(xdp),
                               xdp_buff_is_frag_unreadable(xdp));

Keep a helper for accessing the flags, in case we need to transform them somehow in the future (e.g. to cover up xdp_buff vs xdp_frame differences).

---
Does anyone prefer the current form of the API, or can we change as prosposed?

Bonus question: while Im messing with this API could I rename xdp_update_skb_shared_info()? Maybe to xdp_update_skb_state() ? Not sure why the function name has "shared_info" when most of what it updates is skb fields.

---
 include/net/xdp.h                             | 21 +++++++++----------
 drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c |  2 +-
 drivers/net/ethernet/intel/i40e/i40e_txrx.c   |  4 ++--
 drivers/net/ethernet/intel/ice/ice_txrx.c     |  4 ++--
 drivers/net/ethernet/marvell/mvneta.c         |  2 +-
 .../net/ethernet/mellanox/mlx5/core/en_rx.c   |  7 +++----
 drivers/net/virtio_net.c                      |  2 +-
 net/core/xdp.c                                | 11 +++++-----
 8 files changed, 26 insertions(+), 27 deletions(-)
include/net/xdp.h +10 -11
index b40f1f96cb11..2ff47f53ba26 100644
@@ -104,17 +104,16 @@ static __always_inline void xdp_buff_clear_frags_flag(struct xdp_buff *xdp)
 	xdp->flags &= ~XDP_FLAGS_HAS_FRAGS;
 }
 
-static __always_inline bool
-xdp_buff_is_frag_pfmemalloc(const struct xdp_buff *xdp)
-{
-	return !!(xdp->flags & XDP_FLAGS_FRAGS_PF_MEMALLOC);
-}
-
 static __always_inline void xdp_buff_set_frag_pfmemalloc(struct xdp_buff *xdp)
 {
 	xdp->flags |= XDP_FLAGS_FRAGS_PF_MEMALLOC;
 }
 
+static __always_inline u32 xdp_buff_get_skb_flags(const struct xdp_buff *xdp)
+{
+	return xdp->flags;
+}
+
 static __always_inline void
 xdp_init_buff(struct xdp_buff *xdp, u32 frame_sz, struct xdp_rxq_info *rxq)
 {
@@ -272,10 +271,10 @@ static __always_inline bool xdp_frame_has_frags(const struct xdp_frame *frame)
 	return !!(frame->flags & XDP_FLAGS_HAS_FRAGS);
 }
 
-static __always_inline bool
-xdp_frame_is_frag_pfmemalloc(const struct xdp_frame *frame)
+static __always_inline u32
+xdp_frame_get_skb_flags(const struct xdp_frame *frame)
 {
-	return !!(frame->flags & XDP_FLAGS_FRAGS_PF_MEMALLOC);
+	return frame->flags;
 }
 
 #define XDP_BULK_QUEUE_SIZE	16
@@ -314,7 +313,7 @@ static inline void xdp_scrub_frame(struct xdp_frame *frame)
 static inline void
 xdp_update_skb_shared_info(struct sk_buff *skb, u8 nr_frags,
 			   unsigned int size, unsigned int truesize,
-			   bool pfmemalloc)
+			   u32 skb_flags)
 {
 	struct skb_shared_info *sinfo = skb_shinfo(skb);
 
@@ -328,7 +327,7 @@ xdp_update_skb_shared_info(struct sk_buff *skb, u8 nr_frags,
 	skb->len += size;
 	skb->data_len += size;
 	skb->truesize += truesize;
-	skb->pfmemalloc |= pfmemalloc;
+	skb->pfmemalloc |= skb_flags & XDP_FLAGS_FRAGS_PF_MEMALLOC;
 }
 
 /* Avoids inlining WARN macro in fast-path */
drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c +1 -1
index 58d579dca3f1..b35d4a8a8dac 100644
@@ -471,6 +471,6 @@ bnxt_xdp_build_skb(struct bnxt *bp, struct sk_buff *skb, u8 num_frags,
 	xdp_update_skb_shared_info(skb, num_frags,
 				   sinfo->xdp_frags_size,
 				   BNXT_RX_PAGE_SIZE * num_frags,
-				   xdp_buff_is_frag_pfmemalloc(xdp));
+				   xdp_buff_get_skb_flags(xdp));
 	return skb;
 }
drivers/net/ethernet/intel/i40e/i40e_txrx.c +2 -2
index 048c33039130..9cbd614a0d57 100644
@@ -2154,7 +2154,7 @@ static struct sk_buff *i40e_construct_skb(struct i40e_ring *rx_ring,
 		xdp_update_skb_shared_info(skb, skinfo->nr_frags + nr_frags,
 					   sinfo->xdp_frags_size,
 					   nr_frags * xdp->frame_sz,
-					   xdp_buff_is_frag_pfmemalloc(xdp));
+					   xdp_buff_get_skb_flags(xdp));
 
 		/* First buffer has already been processed, so bump ntc */
 		if (++rx_ring->next_to_clean == rx_ring->count)
@@ -2209,7 +2209,7 @@ static struct sk_buff *i40e_build_skb(struct i40e_ring *rx_ring,
 		xdp_update_skb_shared_info(skb, nr_frags,
 					   sinfo->xdp_frags_size,
 					   nr_frags * xdp->frame_sz,
-					   xdp_buff_is_frag_pfmemalloc(xdp));
+					   xdp_buff_get_skb_flags(xdp));
 
 		i40e_process_rx_buffs(rx_ring, I40E_XDP_PASS, xdp);
 	} else {
drivers/net/ethernet/intel/ice/ice_txrx.c +2 -2
index 29e0088ab6b2..014b321e487e 100644
@@ -1038,7 +1038,7 @@ ice_build_skb(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp)
 		xdp_update_skb_shared_info(skb, nr_frags,
 					   sinfo->xdp_frags_size,
 					   nr_frags * xdp->frame_sz,
-					   xdp_buff_is_frag_pfmemalloc(xdp));
+					   xdp_buff_get_skb_flags(xdp));
 
 	return skb;
 }
@@ -1118,7 +1118,7 @@ ice_construct_skb(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp)
 		xdp_update_skb_shared_info(skb, skinfo->nr_frags + nr_frags,
 					   sinfo->xdp_frags_size,
 					   nr_frags * xdp->frame_sz,
-					   xdp_buff_is_frag_pfmemalloc(xdp));
+					   xdp_buff_get_skb_flags(xdp));
 	}
 
 	return skb;
drivers/net/ethernet/marvell/mvneta.c +1 -1
index 476e73e502fe..79a6bd530619 100644
@@ -2419,7 +2419,7 @@ mvneta_swbm_build_skb(struct mvneta_port *pp, struct page_pool *pool,
 		xdp_update_skb_shared_info(skb, num_frags,
 					   sinfo->xdp_frags_size,
 					   num_frags * xdp->frame_sz,
-					   xdp_buff_is_frag_pfmemalloc(xdp));
+					   xdp_buff_get_skb_flags(xdp));
 
 	return skb;
 }
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +3 -4
index b8c609d91d11..abbe24f71f6a 100644
@@ -1798,8 +1798,7 @@ mlx5e_skb_from_cqe_nonlinear(struct mlx5e_rq *rq, struct mlx5e_wqe_frag_info *wi
 		/* sinfo->nr_frags is reset by build_skb, calculate again. */
 		xdp_update_skb_shared_info(skb, wi - head_wi - 1,
 					   sinfo->xdp_frags_size, truesize,
-					   xdp_buff_is_frag_pfmemalloc(
-						&mxbuf->xdp));
+					   xdp_buff_get_skb_flags(&mxbuf->xdp));
 
 		for (struct mlx5e_wqe_frag_info *pwi = head_wi + 1; pwi < wi; pwi++)
 			pwi->frag_page->frags++;
@@ -2107,7 +2106,7 @@ mlx5e_skb_from_cqe_mpwrq_nonlinear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *w
 			/* sinfo->nr_frags is reset by build_skb, calculate again. */
 			xdp_update_skb_shared_info(skb, frag_page - head_page,
 						   sinfo->xdp_frags_size, truesize,
-						   xdp_buff_is_frag_pfmemalloc(
+						   xdp_buff_get_skb_flags(
 							&mxbuf->xdp));
 
 			pagep = head_page;
@@ -2124,7 +2123,7 @@ mlx5e_skb_from_cqe_mpwrq_nonlinear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *w
 
 			xdp_update_skb_shared_info(skb, sinfo->nr_frags,
 						   sinfo->xdp_frags_size, truesize,
-						   xdp_buff_is_frag_pfmemalloc(
+						   xdp_buff_get_skb_flags(
 							&mxbuf->xdp));
 
 			pagep = frag_page - sinfo->nr_frags;
drivers/net/virtio_net.c +1 -1
index d14e6d602273..152b0d5c2122 100644
@@ -2188,7 +2188,7 @@ static struct sk_buff *build_skb_from_xdp_buff(struct net_device *dev,
 		xdp_update_skb_shared_info(skb, nr_frags,
 					   sinfo->xdp_frags_size,
 					   xdp_frags_truesz,
-					   xdp_buff_is_frag_pfmemalloc(xdp));
+					   xdp_buff_get_skb_flags(xdp));
 
 	return skb;
 }
net/core/xdp.c +6 -5
index 491334b9b8be..789051763209 100644
@@ -665,7 +665,7 @@ struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp)
 		tsize = sinfo->xdp_frags_truesize ? : nr_frags * xdp->frame_sz;
 		xdp_update_skb_shared_info(skb, nr_frags,
 					   sinfo->xdp_frags_size, tsize,
-					   xdp_buff_is_frag_pfmemalloc(xdp));
+					   xdp_buff_get_skb_flags(xdp));
 	}
 
 	skb->protocol = eth_type_trans(skb, rxq->dev);
@@ -692,7 +692,7 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
 	struct skb_shared_info *sinfo = skb_shinfo(skb);
 	const struct skb_shared_info *xinfo;
 	u32 nr_frags, tsize = 0;
-	bool pfmemalloc = false;
+	u32 flags = 0;
 
 	xinfo = xdp_get_shared_info_from_buff(xdp);
 	nr_frags = xinfo->nr_frags;
@@ -714,11 +714,12 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
 		__skb_fill_page_desc_noacc(sinfo, i, page, offset, len);
 
 		tsize += truesize;
-		pfmemalloc |= page_is_pfmemalloc(page);
+		if (page_is_pfmemalloc(page))
+			flags |= XDP_FLAGS_FRAGS_PF_MEMALLOC;
 	}
 
 	xdp_update_skb_shared_info(skb, nr_frags, xinfo->xdp_frags_size,
-				   tsize, pfmemalloc);
+				   tsize, flags);
 
 	return true;
 }
@@ -826,7 +827,7 @@ struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
 		xdp_update_skb_shared_info(skb, nr_frags,
 					   sinfo->xdp_frags_size,
 					   nr_frags * xdpf->frame_sz,
-					   xdp_frame_is_frag_pfmemalloc(xdpf));
+					   xdp_frame_get_skb_flags(xdpf));
 
 	/* Essential SKB info: protocol and skb->dev */
 	skb->protocol = eth_type_trans(skb, dev);
Signature · 1 line
2.50.1
author maintainer
#1 lore raw
Quoted 13 lines · On Tue, 12 Aug 2025 09:15:28 -0700 Jakub Kicinski wrote:

xdp_update_skb_shared_info() needs to update skb state which was maintained in xdp_buff / frame. Pass full flags into it, instead of breaking it out bit by bit. We will need to add a bit for unreadable frags (even tho XDP doesn't support those the driver paths may be common), at which point almost all call sites would become:

    xdp_update_skb_shared_info(skb, num_frags,
                               sinfo->xdp_frags_size,
                               MY_PAGE_SIZE * num_frags,
                               xdp_buff_is_frag_pfmemalloc(xdp),
                               xdp_buff_is_frag_unreadable(xdp));
Quoted 3 lines

Keep a helper for accessing the flags, in case we need to transform them somehow in the future (e.g. to cover up xdp_buff vs xdp_frame differences).

CC Olek

https://lore.kernel.org/all/[email protected]/

#2 lore raw
In reply to Jakub Kicinski

From: Jakub Kicinski <[email protected]>
Date: Tue, 12 Aug 2025 09:15:28 -0700

Quoted 7 lines

xdp_update_skb_shared_info() needs to update skb state which was maintained in xdp_buff / frame. Pass full flags into it, instead of breaking it out bit by bit. We will need to add a bit for unreadable frags (even tho XDP doesn't support those the driver paths may be common), at which point almost all call sites would become:

Quoted 5 lines
    xdp_update_skb_shared_info(skb, num_frags,
                               sinfo->xdp_frags_size,
                               MY_PAGE_SIZE * num_frags,
                               xdp_buff_is_frag_pfmemalloc(xdp),
                               xdp_buff_is_frag_unreadable(xdp));

Yeah I think this doesn't make sense, it just doesn't scale. We can make more flags in future and adding a new argument for each is not a good idea, even if more drivers would switch to generic xdp_build_skb_from_buff().

Quoted 6 lines

Keep a helper for accessing the flags, in case we need to transform them somehow in the future (e.g. to cover up xdp_buff vs xdp_frame differences).

Quoted 8 lines

---
Does anyone prefer the current form of the API, or can we change as prosposed?

Bonus question: while Im messing with this API could I rename xdp_update_skb_shared_info()? Maybe to xdp_update_skb_state() ? Not sure why the function name has "shared_info" when most of what it updates is skb fields.

I can only suspect that the author decided to name it this way due to that it's only used when xdp_buff has frags (and frags are in shinfo). But I agree it's not the best choice. xdp_update_skb_state() sounds fine to me, but given that it's all about frags, maybe something like xdp_update_skb_frags_info/state() or so?

Quoted 16 lines
Quoted 10 lines
---
 include/net/xdp.h                             | 21 +++++++++----------
 drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c |  2 +-
 drivers/net/ethernet/intel/i40e/i40e_txrx.c   |  4 ++--
 drivers/net/ethernet/intel/ice/ice_txrx.c     |  4 ++--
 drivers/net/ethernet/marvell/mvneta.c         |  2 +-
 .../net/ethernet/mellanox/mlx5/core/en_rx.c   |  7 +++----
 drivers/net/virtio_net.c                      |  2 +-
 net/core/xdp.c                                | 11 +++++-----
 8 files changed, 26 insertions(+), 27 deletions(-)

Thanks,
Olek

#3 lore raw
Quoted 16 lines · Alexander Lobakin

From: Jakub Kicinski <[email protected]>
Date: Tue, 12 Aug 2025 09:15:28 -0700

Quoted 7 lines

xdp_update_skb_shared_info() needs to update skb state which was maintained in xdp_buff / frame. Pass full flags into it, instead of breaking it out bit by bit. We will need to add a bit for unreadable frags (even tho XDP doesn't support those the driver paths may be common), at which point almost all call sites would become:

Quoted 5 lines
     xdp_update_skb_shared_info(skb, num_frags,
                                sinfo->xdp_frags_size,
                                MY_PAGE_SIZE * num_frags,
                                xdp_buff_is_frag_pfmemalloc(xdp),
                                xdp_buff_is_frag_unreadable(xdp));
Quoted 5 lines

Yeah I think this doesn't make sense, it just doesn't scale. We can make more flags in future and adding a new argument for each is not a good idea, even if more drivers would switch to generic xdp_build_skb_from_buff().

I agree. And good reminder that some driver have already switched to the generic xdp_build_skb_from_buff().

Quoted 10 lines
Quoted 10 lines

Keep a helper for accessing the flags, in case we need to transform them somehow in the future (e.g. to cover up xdp_buff vs xdp_frame differences).

---
Does anyone prefer the current form of the API, or can we change as prosposed?

I like the proposed change.
The only thing that confuses me was that the u32 flags is named "skb_flags" and not "xdp_flags".

@@ -314,7 +313,7 @@
static inline void
xdp_update_skb_shared_info(struct sk_buff *skb, u8 nr_frags,
unsigned int size, unsigned int truesize,
- bool pfmemalloc)
+ u32 skb_flags)

Quoted 11 lines
Quoted 4 lines

Bonus question: while Im messing with this API could I rename xdp_update_skb_shared_info()? Maybe to xdp_update_skb_state() ? Not sure why the function name has "shared_info" when most of what it updates is skb fields.

I can only suspect that the author decided to name it this way due to that it's only used when xdp_buff has frags (and frags are in shinfo). But I agree it's not the best choice. xdp_update_skb_state() sounds fine to me, but given that it's all about frags, maybe something like xdp_update_skb_frags_info/state() or so?

Yes, function is only used when skb_shared_info have already been touched.

Performance wise it can be expensive to touch the cache-line for skb_shared_info, so the code carefully checks xdp_buff_has_frags() (flag XDP_FLAGS_HAS_FRAGS) before deref of skb_shared_info memory area.

Calling it xdp_update_skb_state() seems misleading. As Olek says, this is about updating the "skb_frags". The original intent is that xdp_buff/xdp_frame is using same skb_shared_info area as SKB, and when transitioning to a "full" SKB then we need to do some adjustments. (Looking at function code, it is of-cause confusing that it doesn't touch sinfo->frags[] array, but that is because we don't need to, as non-linear XDP and SKB have same layout.).

--Jesper

Quoted 26 lines
Quoted 26 lines
---
  include/net/xdp.h                             | 21 +++++++++----------
  drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c |  2 +-
  drivers/net/ethernet/intel/i40e/i40e_txrx.c   |  4 ++--
  drivers/net/ethernet/intel/ice/ice_txrx.c     |  4 ++--
  drivers/net/ethernet/marvell/mvneta.c         |  2 +-
  .../net/ethernet/mellanox/mlx5/core/en_rx.c   |  7 +++----
  drivers/net/virtio_net.c                      |  2 +-
  net/core/xdp.c                                | 11 +++++-----
  8 files changed, 26 insertions(+), 27 deletions(-)
author maintainer
#4 lore raw

include/net/xdp.h static inline void xdp_scrub_frame(struct xdp_frame *frame) · line 316

Quoted 13 lines · On Wed, 13 Aug 2025 10:43:21 +0200 Jesper Dangaard Brouer wrote:
Quoted 2 lines
Quoted 2 lines

Does anyone prefer the current form of the API, or can we change as prosposed?

I like the proposed change.
The only thing that confuses me was that the u32 flags is named "skb_flags" and not "xdp_flags".

include/net/xdp.h +1 -1
@@ -314,7 +313,7 @@
  static inline void
  xdp_update_skb_shared_info(struct sk_buff *skb, u8 nr_frags,
  			   unsigned int size, unsigned int truesize,
-			   bool pfmemalloc)
+			   u32 skb_flags)

It was matching the helper names: xdp_buff_get_skb_flags()

If we rename it to xdp_flags here do you want me to keep the helpers (xdp_buff_get_flags()?) or access buf->flags directly in the caller?

The idea was that the helper could filter / transform the flags to whatever the update function takes. And the skb_ in the helper name was matching the skb_ of the arg.

Quoted 17 lines
Quoted 10 lines
Quoted 4 lines

Bonus question: while Im messing with this API could I rename xdp_update_skb_shared_info()? Maybe to xdp_update_skb_state() ? Not sure why the function name has "shared_info" when most of what it updates is skb fields.

I can only suspect that the author decided to name it this way due to that it's only used when xdp_buff has frags (and frags are in shinfo). But I agree it's not the best choice. xdp_update_skb_state() sounds fine to me, but given that it's all about frags, maybe something like xdp_update_skb_frags_info/state() or so?

Yes, function is only used when skb_shared_info have already been touched.

Performance wise it can be expensive to touch the cache-line for skb_shared_info, so the code carefully checks xdp_buff_has_frags() (flag XDP_FLAGS_HAS_FRAGS) before deref of skb_shared_info memory area.

Quoted 7 lines

Calling it xdp_update_skb_state() seems misleading. As Olek says, this is about updating the "skb_frags". The original intent is that xdp_buff/xdp_frame is using same skb_shared_info area as SKB, and when transitioning to a "full" SKB then we need to do some adjustments. (Looking at function code, it is of-cause confusing that it doesn't touch sinfo->frags[] array, but that is because we don't need to, as non-linear XDP and SKB have same layout.).

Let's go with xdp_update_skb_frags_info(), then.

#5 lore raw
Quoted 17 lines · Jakub Kicinski

include/net/xdp.h static inline void xdp_scrub_frame(struct xdp_frame *frame) · line 316

Quoted 13 lines · On Wed, 13 Aug 2025 10:43:21 +0200 Jesper Dangaard Brouer wrote:
Quoted 2 lines
Quoted 2 lines

Does anyone prefer the current form of the API, or can we change as prosposed?

I like the proposed change.
The only thing that confuses me was that the u32 flags is named "skb_flags" and not "xdp_flags".

include/net/xdp.h +1 -1
@@ -314,7 +313,7 @@
   static inline void
   xdp_update_skb_shared_info(struct sk_buff *skb, u8 nr_frags,
   			   unsigned int size, unsigned int truesize,
-			   bool pfmemalloc)
+			   u32 skb_flags)

It was matching the helper names: xdp_buff_get_skb_flags()

Quoted 8 lines

If we rename it to xdp_flags here do you want me to keep the helpers (xdp_buff_get_flags()?) or access buf->flags directly in the caller?

The idea was that the helper could filter / transform the flags to whatever the update function takes. And the skb_ in the helper name was matching the skb_ of the arg.

It makes sense to have a helper, as you argue.

Quoted 25 lines
Quoted 17 lines
Quoted 10 lines
Quoted 4 lines

Bonus question: while Im messing with this API could I rename xdp_update_skb_shared_info()? Maybe to xdp_update_skb_state() ? Not sure why the function name has "shared_info" when most of what it updates is skb fields.

I can only suspect that the author decided to name it this way due to that it's only used when xdp_buff has frags (and frags are in shinfo). But I agree it's not the best choice. xdp_update_skb_state() sounds fine to me, but given that it's all about frags, maybe something like xdp_update_skb_frags_info/state() or so?

Yes, function is only used when skb_shared_info have already been touched.

Performance wise it can be expensive to touch the cache-line for skb_shared_info, so the code carefully checks xdp_buff_has_frags() (flag XDP_FLAGS_HAS_FRAGS) before deref of skb_shared_info memory area.

Quoted 7 lines

Calling it xdp_update_skb_state() seems misleading. As Olek says, this is about updating the "skb_frags". The original intent is that xdp_buff/xdp_frame is using same skb_shared_info area as SKB, and when transitioning to a "full" SKB then we need to do some adjustments. (Looking at function code, it is of-cause confusing that it doesn't touch sinfo->frags[] array, but that is because we don't need to, as non-linear XDP and SKB have same layout.).

Quoted 1 line

Let's go with xdp_update_skb_frags_info(), then.

Fine with me. It was Olek's naming suggestions (and I liked both).

Thanks
--Jesper

#6 lore raw
In reply to Jakub Kicinski
Quoted 13 lines · Jakub Kicinski

xdp_update_skb_shared_info() needs to update skb state which was maintained in xdp_buff / frame. Pass full flags into it, instead of breaking it out bit by bit. We will need to add a bit for unreadable frags (even tho XDP doesn't support those the driver paths may be common), at which point almost all call sites would become:

    xdp_update_skb_shared_info(skb, num_frags,
                               sinfo->xdp_frags_size,
                               MY_PAGE_SIZE * num_frags,
                               xdp_buff_is_frag_pfmemalloc(xdp),
                               xdp_buff_is_frag_unreadable(xdp));
Quoted 8 lines

Keep a helper for accessing the flags, in case we need to transform them somehow in the future (e.g. to cover up xdp_buff vs xdp_frame differences).

---
Does anyone prefer the current form of the API, or can we change as prosposed?

I prefer the new proposed APIs if we are adding new bits there.

Regards,
Lorenzo

Quoted 254 lines

Bonus question: while Im messing with this API could I rename xdp_update_skb_shared_info()? Maybe to xdp_update_skb_state() ? Not sure why the function name has "shared_info" when most of what it updates is skb fields.

---
 include/net/xdp.h                             | 21 +++++++++----------
 drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c |  2 +-
 drivers/net/ethernet/intel/i40e/i40e_txrx.c   |  4 ++--
 drivers/net/ethernet/intel/ice/ice_txrx.c     |  4 ++--
 drivers/net/ethernet/marvell/mvneta.c         |  2 +-
 .../net/ethernet/mellanox/mlx5/core/en_rx.c   |  7 +++----
 drivers/net/virtio_net.c                      |  2 +-
 net/core/xdp.c                                | 11 +++++-----
 8 files changed, 26 insertions(+), 27 deletions(-)
include/net/xdp.h +10 -11
index b40f1f96cb11..2ff47f53ba26 100644
@@ -104,17 +104,16 @@ static __always_inline void xdp_buff_clear_frags_flag(struct xdp_buff *xdp)
 	xdp->flags &= ~XDP_FLAGS_HAS_FRAGS;
 }
 
-static __always_inline bool
-xdp_buff_is_frag_pfmemalloc(const struct xdp_buff *xdp)
-{
-	return !!(xdp->flags & XDP_FLAGS_FRAGS_PF_MEMALLOC);
-}
-
 static __always_inline void xdp_buff_set_frag_pfmemalloc(struct xdp_buff *xdp)
 {
 	xdp->flags |= XDP_FLAGS_FRAGS_PF_MEMALLOC;
 }
 
+static __always_inline u32 xdp_buff_get_skb_flags(const struct xdp_buff *xdp)
+{
+	return xdp->flags;
+}
+
 static __always_inline void
 xdp_init_buff(struct xdp_buff *xdp, u32 frame_sz, struct xdp_rxq_info *rxq)
 {
@@ -272,10 +271,10 @@ static __always_inline bool xdp_frame_has_frags(const struct xdp_frame *frame)
 	return !!(frame->flags & XDP_FLAGS_HAS_FRAGS);
 }
 
-static __always_inline bool
-xdp_frame_is_frag_pfmemalloc(const struct xdp_frame *frame)
+static __always_inline u32
+xdp_frame_get_skb_flags(const struct xdp_frame *frame)
 {
-	return !!(frame->flags & XDP_FLAGS_FRAGS_PF_MEMALLOC);
+	return frame->flags;
 }
 
 #define XDP_BULK_QUEUE_SIZE	16
@@ -314,7 +313,7 @@ static inline void xdp_scrub_frame(struct xdp_frame *frame)
 static inline void
 xdp_update_skb_shared_info(struct sk_buff *skb, u8 nr_frags,
 			   unsigned int size, unsigned int truesize,
-			   bool pfmemalloc)
+			   u32 skb_flags)
 {
 	struct skb_shared_info *sinfo = skb_shinfo(skb);
 
@@ -328,7 +327,7 @@ xdp_update_skb_shared_info(struct sk_buff *skb, u8 nr_frags,
 	skb->len += size;
 	skb->data_len += size;
 	skb->truesize += truesize;
-	skb->pfmemalloc |= pfmemalloc;
+	skb->pfmemalloc |= skb_flags & XDP_FLAGS_FRAGS_PF_MEMALLOC;
 }
 
 /* Avoids inlining WARN macro in fast-path */
drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c +1 -1
index 58d579dca3f1..b35d4a8a8dac 100644
@@ -471,6 +471,6 @@ bnxt_xdp_build_skb(struct bnxt *bp, struct sk_buff *skb, u8 num_frags,
 	xdp_update_skb_shared_info(skb, num_frags,
 				   sinfo->xdp_frags_size,
 				   BNXT_RX_PAGE_SIZE * num_frags,
-				   xdp_buff_is_frag_pfmemalloc(xdp));
+				   xdp_buff_get_skb_flags(xdp));
 	return skb;
 }
drivers/net/ethernet/intel/i40e/i40e_txrx.c +2 -2
index 048c33039130..9cbd614a0d57 100644
@@ -2154,7 +2154,7 @@ static struct sk_buff *i40e_construct_skb(struct i40e_ring *rx_ring,
 		xdp_update_skb_shared_info(skb, skinfo->nr_frags + nr_frags,
 					   sinfo->xdp_frags_size,
 					   nr_frags * xdp->frame_sz,
-					   xdp_buff_is_frag_pfmemalloc(xdp));
+					   xdp_buff_get_skb_flags(xdp));
 
 		/* First buffer has already been processed, so bump ntc */
 		if (++rx_ring->next_to_clean == rx_ring->count)
@@ -2209,7 +2209,7 @@ static struct sk_buff *i40e_build_skb(struct i40e_ring *rx_ring,
 		xdp_update_skb_shared_info(skb, nr_frags,
 					   sinfo->xdp_frags_size,
 					   nr_frags * xdp->frame_sz,
-					   xdp_buff_is_frag_pfmemalloc(xdp));
+					   xdp_buff_get_skb_flags(xdp));
 
 		i40e_process_rx_buffs(rx_ring, I40E_XDP_PASS, xdp);
 	} else {
drivers/net/ethernet/intel/ice/ice_txrx.c +2 -2
index 29e0088ab6b2..014b321e487e 100644
@@ -1038,7 +1038,7 @@ ice_build_skb(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp)
 		xdp_update_skb_shared_info(skb, nr_frags,
 					   sinfo->xdp_frags_size,
 					   nr_frags * xdp->frame_sz,
-					   xdp_buff_is_frag_pfmemalloc(xdp));
+					   xdp_buff_get_skb_flags(xdp));
 
 	return skb;
 }
@@ -1118,7 +1118,7 @@ ice_construct_skb(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp)
 		xdp_update_skb_shared_info(skb, skinfo->nr_frags + nr_frags,
 					   sinfo->xdp_frags_size,
 					   nr_frags * xdp->frame_sz,
-					   xdp_buff_is_frag_pfmemalloc(xdp));
+					   xdp_buff_get_skb_flags(xdp));
 	}
 
 	return skb;
drivers/net/ethernet/marvell/mvneta.c +1 -1
index 476e73e502fe..79a6bd530619 100644
@@ -2419,7 +2419,7 @@ mvneta_swbm_build_skb(struct mvneta_port *pp, struct page_pool *pool,
 		xdp_update_skb_shared_info(skb, num_frags,
 					   sinfo->xdp_frags_size,
 					   num_frags * xdp->frame_sz,
-					   xdp_buff_is_frag_pfmemalloc(xdp));
+					   xdp_buff_get_skb_flags(xdp));
 
 	return skb;
 }
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +3 -4
index b8c609d91d11..abbe24f71f6a 100644
@@ -1798,8 +1798,7 @@ mlx5e_skb_from_cqe_nonlinear(struct mlx5e_rq *rq, struct mlx5e_wqe_frag_info *wi
 		/* sinfo->nr_frags is reset by build_skb, calculate again. */
 		xdp_update_skb_shared_info(skb, wi - head_wi - 1,
 					   sinfo->xdp_frags_size, truesize,
-					   xdp_buff_is_frag_pfmemalloc(
-						&mxbuf->xdp));
+					   xdp_buff_get_skb_flags(&mxbuf->xdp));
 
 		for (struct mlx5e_wqe_frag_info *pwi = head_wi + 1; pwi < wi; pwi++)
 			pwi->frag_page->frags++;
@@ -2107,7 +2106,7 @@ mlx5e_skb_from_cqe_mpwrq_nonlinear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *w
 			/* sinfo->nr_frags is reset by build_skb, calculate again. */
 			xdp_update_skb_shared_info(skb, frag_page - head_page,
 						   sinfo->xdp_frags_size, truesize,
-						   xdp_buff_is_frag_pfmemalloc(
+						   xdp_buff_get_skb_flags(
 							&mxbuf->xdp));
 
 			pagep = head_page;
@@ -2124,7 +2123,7 @@ mlx5e_skb_from_cqe_mpwrq_nonlinear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *w
 
 			xdp_update_skb_shared_info(skb, sinfo->nr_frags,
 						   sinfo->xdp_frags_size, truesize,
-						   xdp_buff_is_frag_pfmemalloc(
+						   xdp_buff_get_skb_flags(
 							&mxbuf->xdp));
 
 			pagep = frag_page - sinfo->nr_frags;
drivers/net/virtio_net.c +1 -1
index d14e6d602273..152b0d5c2122 100644
@@ -2188,7 +2188,7 @@ static struct sk_buff *build_skb_from_xdp_buff(struct net_device *dev,
 		xdp_update_skb_shared_info(skb, nr_frags,
 					   sinfo->xdp_frags_size,
 					   xdp_frags_truesz,
-					   xdp_buff_is_frag_pfmemalloc(xdp));
+					   xdp_buff_get_skb_flags(xdp));
 
 	return skb;
 }
net/core/xdp.c +6 -5
index 491334b9b8be..789051763209 100644
@@ -665,7 +665,7 @@ struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp)
 		tsize = sinfo->xdp_frags_truesize ? : nr_frags * xdp->frame_sz;
 		xdp_update_skb_shared_info(skb, nr_frags,
 					   sinfo->xdp_frags_size, tsize,
-					   xdp_buff_is_frag_pfmemalloc(xdp));
+					   xdp_buff_get_skb_flags(xdp));
 	}
 
 	skb->protocol = eth_type_trans(skb, rxq->dev);
@@ -692,7 +692,7 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
 	struct skb_shared_info *sinfo = skb_shinfo(skb);
 	const struct skb_shared_info *xinfo;
 	u32 nr_frags, tsize = 0;
-	bool pfmemalloc = false;
+	u32 flags = 0;
 
 	xinfo = xdp_get_shared_info_from_buff(xdp);
 	nr_frags = xinfo->nr_frags;
@@ -714,11 +714,12 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
 		__skb_fill_page_desc_noacc(sinfo, i, page, offset, len);
 
 		tsize += truesize;
-		pfmemalloc |= page_is_pfmemalloc(page);
+		if (page_is_pfmemalloc(page))
+			flags |= XDP_FLAGS_FRAGS_PF_MEMALLOC;
 	}
 
 	xdp_update_skb_shared_info(skb, nr_frags, xinfo->xdp_frags_size,
-				   tsize, pfmemalloc);
+				   tsize, flags);
 
 	return true;
 }
@@ -826,7 +827,7 @@ struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
 		xdp_update_skb_shared_info(skb, nr_frags,
 					   sinfo->xdp_frags_size,
 					   nr_frags * xdpf->frame_sz,
-					   xdp_frame_is_frag_pfmemalloc(xdpf));
+					   xdp_frame_get_skb_flags(xdpf));
 
 	/* Essential SKB info: protocol and skb->dev */
 	skb->protocol = eth_type_trans(skb, dev);
Signature · 1 line
2.50.1
#7 lore raw
In reply to Jakub Kicinski
Quoted 13 lines · Jakub Kicinski

xdp_update_skb_shared_info() needs to update skb state which was maintained in xdp_buff / frame. Pass full flags into it, instead of breaking it out bit by bit. We will need to add a bit for unreadable frags (even tho XDP doesn't support those the driver paths may be common), at which point almost all call sites would become:

    xdp_update_skb_shared_info(skb, num_frags,
                               sinfo->xdp_frags_size,
                               MY_PAGE_SIZE * num_frags,
                               xdp_buff_is_frag_pfmemalloc(xdp),
                               xdp_buff_is_frag_unreadable(xdp));
Quoted 8 lines

Keep a helper for accessing the flags, in case we need to transform them somehow in the future (e.g. to cover up xdp_buff vs xdp_frame differences).

---
Does anyone prefer the current form of the API, or can we change as prosposed?

I think the change is fine, but I agree with Jesper that it's a bit weird to call them skb_flags. Maybe just xdp_buff_get_flags()?

-Toke