netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
To: Amery Hung <ameryhung@gmail.com>
Cc: <bpf@vger.kernel.org>, <netdev@vger.kernel.org>,
	<alexei.starovoitov@gmail.com>, <andrii@kernel.org>,
	<daniel@iogearbox.net>, <paul.chaignon@gmail.com>,
	<kuba@kernel.org>, <stfomichev@gmail.com>,
	<martin.lau@kernel.org>, <mohsin.bashr@gmail.com>,
	<noren@nvidia.com>, <dtatulea@nvidia.com>, <saeedm@nvidia.com>,
	<tariqt@nvidia.com>, <mbloch@nvidia.com>, <kernel-team@meta.com>
Subject: Re: [PATCH bpf-next v4 1/6] bpf: Allow bpf_xdp_shrink_data to shrink a frag from head and tail
Date: Thu, 18 Sep 2025 10:52:08 +0200	[thread overview]
Message-ID: <aMvIONMZ9CFqyNnM@boxer> (raw)
In-Reply-To: <20250917225513.3388199-2-ameryhung@gmail.com>

On Wed, Sep 17, 2025 at 03:55:08PM -0700, Amery Hung wrote:
> Move skb_frag_t adjustment into bpf_xdp_shrink_data() and extend its
> functionality to be able to shrink an xdp fragment from both head and
> tail. In a later patch, bpf_xdp_pull_data() will reuse it to shrink an
> xdp fragment from head.
> 
> Additionally, in bpf_xdp_frags_shrink_tail(), breaking the loop when
> bpf_xdp_shrink_data() returns false (i.e., not releasing the current
> fragment) is not necessary as the loop condition, offset > 0, has the
> same effect. Remove the else branch to simplify the code.
> 
> Signed-off-by: Amery Hung <ameryhung@gmail.com>
> ---
>  include/net/xdp_sock_drv.h | 21 ++++++++++++++++++---
>  net/core/filter.c          | 28 +++++++++++++++++-----------
>  2 files changed, 35 insertions(+), 14 deletions(-)
> 
> diff --git a/include/net/xdp_sock_drv.h b/include/net/xdp_sock_drv.h
> index 513c8e9704f6..4f2d3268a676 100644
> --- a/include/net/xdp_sock_drv.h
> +++ b/include/net/xdp_sock_drv.h
> @@ -160,13 +160,23 @@ static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first)
>  	return ret;
>  }
>  
> -static inline void xsk_buff_del_tail(struct xdp_buff *tail)
> +static inline void xsk_buff_del_frag(struct xdp_buff *xdp)
>  {
> -	struct xdp_buff_xsk *xskb = container_of(tail, struct xdp_buff_xsk, xdp);
> +	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
>  
>  	list_del(&xskb->list_node);
>  }
>  
> +static inline struct xdp_buff *xsk_buff_get_head(struct xdp_buff *first)
> +{
> +	struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp);
> +	struct xdp_buff_xsk *frag;
> +
> +	frag = list_first_entry(&xskb->pool->xskb_list, struct xdp_buff_xsk,
> +				list_node);
> +	return &frag->xdp;
> +}
> +
>  static inline struct xdp_buff *xsk_buff_get_tail(struct xdp_buff *first)
>  {
>  	struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp);
> @@ -389,8 +399,13 @@ static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first)
>  	return NULL;
>  }
>  
> -static inline void xsk_buff_del_tail(struct xdp_buff *tail)
> +static inline void xsk_buff_del_frag(struct xdp_buff *xdp)
> +{
> +}
> +
> +static inline struct xdp_buff *xsk_buff_get_head(struct xdp_buff *first)
>  {
> +	return NULL;
>  }
>  
>  static inline struct xdp_buff *xsk_buff_get_tail(struct xdp_buff *first)
> diff --git a/net/core/filter.c b/net/core/filter.c
> index 63f3baee2daf..0b82cb348ce0 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -4153,27 +4153,31 @@ static int bpf_xdp_frags_increase_tail(struct xdp_buff *xdp, int offset)
>  	return 0;
>  }
>  
> -static void bpf_xdp_shrink_data_zc(struct xdp_buff *xdp, int shrink,
> +static void bpf_xdp_shrink_data_zc(struct xdp_buff *xdp, int shrink, bool tail,
>  				   enum xdp_mem_type mem_type, bool release)
>  {
> -	struct xdp_buff *zc_frag = xsk_buff_get_tail(xdp);
> +	struct xdp_buff *zc_frag = tail ? xsk_buff_get_tail(xdp) :
> +					  xsk_buff_get_head(xdp);
>  
>  	if (release) {
> -		xsk_buff_del_tail(zc_frag);
> +		xsk_buff_del_frag(zc_frag);
>  		__xdp_return(0, mem_type, false, zc_frag);
>  	} else {
> -		zc_frag->data_end -= shrink;
> +		if (tail)
> +			zc_frag->data_end -= shrink;
> +		else
> +			zc_frag->data += shrink;
>  	}
>  }
>  
>  static bool bpf_xdp_shrink_data(struct xdp_buff *xdp, skb_frag_t *frag,
> -				int shrink)
> +				int shrink, bool tail)
>  {
>  	enum xdp_mem_type mem_type = xdp->rxq->mem.type;
>  	bool release = skb_frag_size(frag) == shrink;
>  
>  	if (mem_type == MEM_TYPE_XSK_BUFF_POOL) {
> -		bpf_xdp_shrink_data_zc(xdp, shrink, mem_type, release);
> +		bpf_xdp_shrink_data_zc(xdp, shrink, tail, mem_type, release);
>  		goto out;
>  	}
>  
> @@ -4181,6 +4185,12 @@ static bool bpf_xdp_shrink_data(struct xdp_buff *xdp, skb_frag_t *frag,
>  		__xdp_return(skb_frag_netmem(frag), mem_type, false, NULL);
>  
>  out:
> +	if (!release) {
> +		if (!tail)
> +			skb_frag_off_add(frag, shrink);
> +		skb_frag_size_sub(frag, shrink);
> +	}

Hi Amery,

it feels a bit off to have separate conditions around @release. How about
something below?


diff --git a/net/core/filter.c b/net/core/filter.c
index 0b82cb348ce0..b1fca279c1de 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -4175,20 +4175,17 @@ static bool bpf_xdp_shrink_data(struct xdp_buff *xdp, skb_frag_t *frag,
 {
 	enum xdp_mem_type mem_type = xdp->rxq->mem.type;
 	bool release = skb_frag_size(frag) == shrink;
+	bool zc = mem_type == MEM_TYPE_XSK_BUFF_POOL;
 
-	if (mem_type == MEM_TYPE_XSK_BUFF_POOL) {
+	if (zc)
 		bpf_xdp_shrink_data_zc(xdp, shrink, tail, mem_type, release);
-		goto out;
-	}
-
-	if (release)
-		__xdp_return(skb_frag_netmem(frag), mem_type, false, NULL);
 
-out:
 	if (!release) {
 		if (!tail)
 			skb_frag_off_add(frag, shrink);
 		skb_frag_size_sub(frag, shrink);
+	} else if (!zc) {
+		__xdp_return(skb_frag_netmem(frag), mem_type, false, NULL);
 	}
 
 	return release;

> +
>  	return release;
>  }
>  
> @@ -4198,12 +4208,8 @@ static int bpf_xdp_frags_shrink_tail(struct xdp_buff *xdp, int offset)
>  
>  		len_free += shrink;
>  		offset -= shrink;
> -		if (bpf_xdp_shrink_data(xdp, frag, shrink)) {
> +		if (bpf_xdp_shrink_data(xdp, frag, shrink, true))
>  			n_frags_free++;
> -		} else {
> -			skb_frag_size_sub(frag, shrink);
> -			break;
> -		}
>  	}
>  	sinfo->nr_frags -= n_frags_free;
>  	sinfo->xdp_frags_size -= len_free;
> -- 
> 2.47.3
> 

  reply	other threads:[~2025-09-18  8:52 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-17 22:55 [PATCH bpf-next v4 0/6] Add kfunc bpf_xdp_pull_data Amery Hung
2025-09-17 22:55 ` [PATCH bpf-next v4 1/6] bpf: Allow bpf_xdp_shrink_data to shrink a frag from head and tail Amery Hung
2025-09-18  8:52   ` Maciej Fijalkowski [this message]
2025-09-18 17:50     ` Amery Hung
2025-09-17 22:55 ` [PATCH bpf-next v4 2/6] bpf: Support pulling non-linear xdp data Amery Hung
2025-09-18  9:11   ` Maciej Fijalkowski
2025-09-18 17:56     ` Amery Hung
2025-09-18 20:19       ` Amery Hung
2025-09-17 22:55 ` [PATCH bpf-next v4 3/6] bpf: Clear packet pointers after changing packet data in kfuncs Amery Hung
2025-09-17 22:55 ` [PATCH bpf-next v4 4/6] bpf: Support specifying linear xdp packet data size for BPF_PROG_TEST_RUN Amery Hung
2025-09-17 22:55 ` [PATCH bpf-next v4 5/6] selftests/bpf: Test bpf_xdp_pull_data Amery Hung
2025-09-18 11:33   ` Maciej Fijalkowski
2025-09-18 19:43     ` Amery Hung
2025-09-17 22:55 ` [PATCH bpf-next v4 6/6] selftests: drv-net: Pull data before parsing headers Amery Hung
  -- strict thread matches above, loose matches on Subject: below --
2025-09-19 18:09 [PATCH bpf-next v4 0/6] Add kfunc bpf_xdp_pull_data Amery Hung
2025-09-19 18:09 ` [PATCH bpf-next v4 1/6] bpf: Allow bpf_xdp_shrink_data to shrink a frag from head and tail Amery Hung

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aMvIONMZ9CFqyNnM@boxer \
    --to=maciej.fijalkowski@intel.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=ameryhung@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=dtatulea@nvidia.com \
    --cc=kernel-team@meta.com \
    --cc=kuba@kernel.org \
    --cc=martin.lau@kernel.org \
    --cc=mbloch@nvidia.com \
    --cc=mohsin.bashr@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=noren@nvidia.com \
    --cc=paul.chaignon@gmail.com \
    --cc=saeedm@nvidia.com \
    --cc=stfomichev@gmail.com \
    --cc=tariqt@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).