diff mbox series

[bpf,2/3] net: fix usage of multi-buffer BPF helper for ZC AF_XDP

Message ID 20231208112945.313687-3-maciej.fijalkowski@intel.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series net: bpf_xdp_adjust_tail() fixes | expand

Checks

Context Check Description
bpf/vmtest-bpf-VM_Test-2 success Logs for Validate matrix.py
bpf/vmtest-bpf-VM_Test-3 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-VM_Test-8 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-VM_Test-26 success Logs for x86_64-llvm-16 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-17 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-25 success Logs for x86_64-llvm-16 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-16 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-VM_Test-18 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-22 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-21 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-23 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-15 success Logs for set-matrix
bpf/vmtest-bpf-VM_Test-24 success Logs for x86_64-llvm-16 / build / build for x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-27 success Logs for x86_64-llvm-16 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-29 success Logs for x86_64-llvm-16 / veristat
bpf/vmtest-bpf-VM_Test-4 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-20 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-5 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-6 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-14 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-VM_Test-7 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-9 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-VM_Test-28 success Logs for x86_64-llvm-16 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-19 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-13 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-VM_Test-12 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-VM_Test-11 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-PR success PR summary
bpf/vmtest-bpf-VM_Test-10 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf
netdev/ynl success SINGLE THREAD; Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1145 this patch: 1145
netdev/cc_maintainers warning 13 maintainers not CCed: kpsingh@kernel.org sdf@google.com yonghong.song@linux.dev martin.lau@linux.dev song@kernel.org jonathan.lemon@gmail.com kuba@kernel.org haoluo@google.com pabeni@redhat.com hawk@kernel.org jolsa@kernel.org john.fastabend@gmail.com edumazet@google.com
netdev/build_clang success Errors and warnings before: 1143 this patch: 1143
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 1172 this patch: 1172
netdev/checkpatch warning WARNING: line length of 81 exceeds 80 columns WARNING: line length of 82 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Maciej Fijalkowski Dec. 8, 2023, 11:29 a.m. UTC
Currently when packet is shrunk via bpf_xdp_adjust_tail(), null ptr
dereference happens:

[1136314.192256] BUG: kernel NULL pointer dereference, address:
0000000000000034
[1136314.203943] #PF: supervisor read access in kernel mode
[1136314.213768] #PF: error_code(0x0000) - not-present page
[1136314.223550] PGD 0 P4D 0
[1136314.230684] Oops: 0000 [#1] PREEMPT SMP NOPTI
[1136314.239621] CPU: 8 PID: 54203 Comm: xdpsock Not tainted 6.6.0+ #257
[1136314.250469] Hardware name: Intel Corporation S2600WFT/S2600WFT,
BIOS SE5C620.86B.02.01.0008.031920191559 03/19/2019
[1136314.265615] RIP: 0010:__xdp_return+0x6c/0x210
[1136314.274653] Code: ad 00 48 8b 47 08 49 89 f8 a8 01 0f 85 9b 01 00 00 0f 1f 44 00 00 f0 41 ff 48 34 75 32 4c 89 c7 e9 79 cd 80 ff 83 fe 03 75 17 <f6> 41 34 01 0f 85 02 01 00 00 48 89 cf e9 22 cc 1e 00 e9 3d d2 86
[1136314.302907] RSP: 0018:ffffc900089f8db0 EFLAGS: 00010246
[1136314.312967] RAX: ffffc9003168aed0 RBX: ffff8881c3300000 RCX:
0000000000000000
[1136314.324953] RDX: 0000000000000000 RSI: 0000000000000003 RDI:
ffffc9003168c000
[1136314.336929] RBP: 0000000000000ae0 R08: 0000000000000002 R09:
0000000000010000
[1136314.348844] R10: ffffc9000e495000 R11: 0000000000000040 R12:
0000000000000001
[1136314.360706] R13: 0000000000000524 R14: ffffc9003168aec0 R15:
0000000000000001
[1136314.373298] FS:  00007f8df8bbcb80(0000) GS:ffff8897e0e00000(0000)
knlGS:0000000000000000
[1136314.386105] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[1136314.396532] CR2: 0000000000000034 CR3: 00000001aa912002 CR4:
00000000007706f0
[1136314.408377] DR0: 0000000000000000 DR1: 0000000000000000 DR2:
0000000000000000
[1136314.420173] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7:
0000000000000400
[1136314.431890] PKRU: 55555554
[1136314.439143] Call Trace:
[1136314.446058]  <IRQ>
[1136314.452465]  ? __die+0x20/0x70
[1136314.459881]  ? page_fault_oops+0x15b/0x440
[1136314.468305]  ? exc_page_fault+0x6a/0x150
[1136314.476491]  ? asm_exc_page_fault+0x22/0x30
[1136314.484927]  ? __xdp_return+0x6c/0x210
[1136314.492863]  bpf_xdp_adjust_tail+0x155/0x1d0
[1136314.501269]  bpf_prog_ccc47ae29d3b6570_xdp_sock_prog+0x15/0x60
[1136314.511263]  ice_clean_rx_irq_zc+0x206/0xc60 [ice]
[1136314.520222]  ? ice_xmit_zc+0x6e/0x150 [ice]
[1136314.528506]  ice_napi_poll+0x467/0x670 [ice]
[1136314.536858]  ? ttwu_do_activate.constprop.0+0x8f/0x1a0
[1136314.546010]  __napi_poll+0x29/0x1b0
[1136314.553462]  net_rx_action+0x133/0x270
[1136314.561619]  __do_softirq+0xbe/0x28e
[1136314.569303]  do_softirq+0x3f/0x60

This comes from __xdp_return() call with xdp_buff argument passed as
NULL which is supposed to be consumed by xsk_buff_free() call.

To address this properly, in ZC case, a node that represents the frag
being removed has to be pulled out of xskb_list. Introduce
appriopriate xsk helpers to do such node operation and use them
accordingly within bpf_xdp_adjust_tail().

Fixes: 24ea50127ecf ("xsk: support mbuf on ZC RX")
Signed-off-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
---
 include/net/xdp_sock_drv.h | 17 ++++++++++++++++
 net/core/filter.c          | 41 +++++++++++++++++++++++++++++---------
 2 files changed, 49 insertions(+), 9 deletions(-)

Comments

kernel test robot Dec. 8, 2023, 5:29 p.m. UTC | #1
Hi Maciej,

kernel test robot noticed the following build errors:

[auto build test ERROR on bpf/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Maciej-Fijalkowski/xsk-recycle-buffer-in-case-Rx-queue-was-full/20231208-193306
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git master
patch link:    https://lore.kernel.org/r/20231208112945.313687-3-maciej.fijalkowski%40intel.com
patch subject: [PATCH bpf 2/3] net: fix usage of multi-buffer BPF helper for ZC AF_XDP
config: i386-buildonly-randconfig-003-20231208 (https://download.01.org/0day-ci/archive/20231209/202312090104.6EOsoGVa-lkp@intel.com/config)
compiler: clang version 16.0.4 (https://github.com/llvm/llvm-project.git ae42196bc493ffe877a7e3dff8be32035dea4d07)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231209/202312090104.6EOsoGVa-lkp@intel.com/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 <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202312090104.6EOsoGVa-lkp@intel.com/

All errors (new ones prefixed by >>):

>> net/core/filter.c:4102:3: error: call to undeclared function 'xsk_buff_get_tail'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
                   xsk_buff_get_tail(xdp)->data_end -= shrink;
                   ^
   net/core/filter.c:4102:3: note: did you mean 'xsk_buff_get_frag'?
   include/net/xdp_sock_drv.h:324:32: note: 'xsk_buff_get_frag' declared here
   static inline struct xdp_buff *xsk_buff_get_frag(struct xdp_buff *first)
                                  ^
>> net/core/filter.c:4102:27: error: member reference type 'int' is not a pointer
                   xsk_buff_get_tail(xdp)->data_end -= shrink;
                   ~~~~~~~~~~~~~~~~~~~~~~  ^
   net/core/filter.c:4115:14: error: call to undeclared function 'xsk_buff_get_tail'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
                           zc_frag = xsk_buff_get_tail(xdp);
                                     ^
>> net/core/filter.c:4115:12: error: incompatible integer to pointer conversion assigning to 'struct xdp_buff *' from 'int' [-Wint-conversion]
                           zc_frag = xsk_buff_get_tail(xdp);
                                   ^ ~~~~~~~~~~~~~~~~~~~~~~
>> net/core/filter.c:4117:4: error: call to undeclared function 'xsk_buff_tail_del'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
                           xsk_buff_tail_del(zc_frag);
                           ^
   5 errors generated.


vim +/xsk_buff_get_tail +4102 net/core/filter.c

  4097	
  4098	static void __shrink_data(struct xdp_buff *xdp, struct xdp_mem_info *mem_info,
  4099				  skb_frag_t *frag, int shrink)
  4100	{
  4101		if (mem_info->type == MEM_TYPE_XSK_BUFF_POOL)
> 4102			xsk_buff_get_tail(xdp)->data_end -= shrink;
  4103		skb_frag_size_sub(frag, shrink);
  4104	}
  4105	
  4106	static bool shrink_data(struct xdp_buff *xdp, skb_frag_t *frag, int shrink)
  4107	{
  4108		struct xdp_mem_info *mem_info = &xdp->rxq->mem;
  4109	
  4110		if (skb_frag_size(frag) == shrink) {
  4111			struct page *page = skb_frag_page(frag);
  4112			struct xdp_buff *zc_frag = NULL;
  4113	
  4114			if (mem_info->type == MEM_TYPE_XSK_BUFF_POOL) {
> 4115				zc_frag = xsk_buff_get_tail(xdp);
  4116				xdp_buff_clear_frags_flag(zc_frag);
> 4117				xsk_buff_tail_del(zc_frag);
  4118			}
  4119	
  4120			__xdp_return(page_address(page), mem_info, false, zc_frag);
  4121			return true;
  4122		}
  4123		__shrink_data(xdp, mem_info, frag, shrink);
  4124		return false;
  4125	}
  4126
kernel test robot Dec. 8, 2023, 5:53 p.m. UTC | #2
Hi Maciej,

kernel test robot noticed the following build warnings:

[auto build test WARNING on bpf/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Maciej-Fijalkowski/xsk-recycle-buffer-in-case-Rx-queue-was-full/20231208-193306
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git master
patch link:    https://lore.kernel.org/r/20231208112945.313687-3-maciej.fijalkowski%40intel.com
patch subject: [PATCH bpf 2/3] net: fix usage of multi-buffer BPF helper for ZC AF_XDP
config: i386-randconfig-013-20231208 (https://download.01.org/0day-ci/archive/20231209/202312090126.Zap0AFox-lkp@intel.com/config)
compiler: gcc-7 (Ubuntu 7.5.0-6ubuntu2) 7.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231209/202312090126.Zap0AFox-lkp@intel.com/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 <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202312090126.Zap0AFox-lkp@intel.com/

All warnings (new ones prefixed by >>):

   net/core/filter.c: In function '__shrink_data':
   net/core/filter.c:4102:3: error: implicit declaration of function 'xsk_buff_get_tail'; did you mean 'xsk_buff_get_frag'? [-Werror=implicit-function-declaration]
      xsk_buff_get_tail(xdp)->data_end -= shrink;
      ^~~~~~~~~~~~~~~~~
      xsk_buff_get_frag
   net/core/filter.c:4102:25: error: invalid type argument of '->' (have 'int')
      xsk_buff_get_tail(xdp)->data_end -= shrink;
                            ^~
   net/core/filter.c: In function 'shrink_data':
>> net/core/filter.c:4115:12: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
       zc_frag = xsk_buff_get_tail(xdp);
               ^
   net/core/filter.c:4117:4: error: implicit declaration of function 'xsk_buff_tail_del'; did you mean 'xsk_buff_alloc'? [-Werror=implicit-function-declaration]
       xsk_buff_tail_del(zc_frag);
       ^~~~~~~~~~~~~~~~~
       xsk_buff_alloc
   cc1: some warnings being treated as errors


vim +4115 net/core/filter.c

  4105	
  4106	static bool shrink_data(struct xdp_buff *xdp, skb_frag_t *frag, int shrink)
  4107	{
  4108		struct xdp_mem_info *mem_info = &xdp->rxq->mem;
  4109	
  4110		if (skb_frag_size(frag) == shrink) {
  4111			struct page *page = skb_frag_page(frag);
  4112			struct xdp_buff *zc_frag = NULL;
  4113	
  4114			if (mem_info->type == MEM_TYPE_XSK_BUFF_POOL) {
> 4115				zc_frag = xsk_buff_get_tail(xdp);
  4116				xdp_buff_clear_frags_flag(zc_frag);
  4117				xsk_buff_tail_del(zc_frag);
  4118			}
  4119	
  4120			__xdp_return(page_address(page), mem_info, false, zc_frag);
  4121			return true;
  4122		}
  4123		__shrink_data(xdp, mem_info, frag, shrink);
  4124		return false;
  4125	}
  4126
kernel test robot Dec. 8, 2023, 5:53 p.m. UTC | #3
Hi Maciej,

kernel test robot noticed the following build warnings:

[auto build test WARNING on bpf/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Maciej-Fijalkowski/xsk-recycle-buffer-in-case-Rx-queue-was-full/20231208-193306
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git master
patch link:    https://lore.kernel.org/r/20231208112945.313687-3-maciej.fijalkowski%40intel.com
patch subject: [PATCH bpf 2/3] net: fix usage of multi-buffer BPF helper for ZC AF_XDP
config: i386-randconfig-014-20231208 (https://download.01.org/0day-ci/archive/20231209/202312090113.kESWoklw-lkp@intel.com/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231209/202312090113.kESWoklw-lkp@intel.com/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 <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202312090113.kESWoklw-lkp@intel.com/

All warnings (new ones prefixed by >>):

   net/core/filter.c: In function '__shrink_data':
   net/core/filter.c:4102:3: error: implicit declaration of function 'xsk_buff_get_tail'; did you mean 'xsk_buff_get_frag'? [-Werror=implicit-function-declaration]
    4102 |   xsk_buff_get_tail(xdp)->data_end -= shrink;
         |   ^~~~~~~~~~~~~~~~~
         |   xsk_buff_get_frag
   net/core/filter.c:4102:25: error: invalid type argument of '->' (have 'int')
    4102 |   xsk_buff_get_tail(xdp)->data_end -= shrink;
         |                         ^~
   net/core/filter.c: In function 'shrink_data':
>> net/core/filter.c:4115:12: warning: assignment to 'struct xdp_buff *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
    4115 |    zc_frag = xsk_buff_get_tail(xdp);
         |            ^
   net/core/filter.c:4117:4: error: implicit declaration of function 'xsk_buff_tail_del'; did you mean 'xsk_buff_alloc'? [-Werror=implicit-function-declaration]
    4117 |    xsk_buff_tail_del(zc_frag);
         |    ^~~~~~~~~~~~~~~~~
         |    xsk_buff_alloc
   cc1: some warnings being treated as errors


vim +4115 net/core/filter.c

  4097	
  4098	static void __shrink_data(struct xdp_buff *xdp, struct xdp_mem_info *mem_info,
  4099				  skb_frag_t *frag, int shrink)
  4100	{
  4101		if (mem_info->type == MEM_TYPE_XSK_BUFF_POOL)
> 4102			xsk_buff_get_tail(xdp)->data_end -= shrink;
  4103		skb_frag_size_sub(frag, shrink);
  4104	}
  4105	
  4106	static bool shrink_data(struct xdp_buff *xdp, skb_frag_t *frag, int shrink)
  4107	{
  4108		struct xdp_mem_info *mem_info = &xdp->rxq->mem;
  4109	
  4110		if (skb_frag_size(frag) == shrink) {
  4111			struct page *page = skb_frag_page(frag);
  4112			struct xdp_buff *zc_frag = NULL;
  4113	
  4114			if (mem_info->type == MEM_TYPE_XSK_BUFF_POOL) {
> 4115				zc_frag = xsk_buff_get_tail(xdp);
  4116				xdp_buff_clear_frags_flag(zc_frag);
  4117				xsk_buff_tail_del(zc_frag);
  4118			}
  4119	
  4120			__xdp_return(page_address(page), mem_info, false, zc_frag);
  4121			return true;
  4122		}
  4123		__shrink_data(xdp, mem_info, frag, shrink);
  4124		return false;
  4125	}
  4126
diff mbox series

Patch

diff --git a/include/net/xdp_sock_drv.h b/include/net/xdp_sock_drv.h
index 81e02de3f453..bb953bb0ab3e 100644
--- a/include/net/xdp_sock_drv.h
+++ b/include/net/xdp_sock_drv.h
@@ -147,6 +147,23 @@  static inline struct xdp_buff *xsk_buff_get_frag(struct xdp_buff *first)
 	return ret;
 }
 
+static inline void xsk_buff_tail_del(struct xdp_buff *tail)
+{
+	struct xdp_buff_xsk *xskb = container_of(tail, struct xdp_buff_xsk, xdp);
+
+	list_del(&xskb->xskb_list_node);
+}
+
+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);
+	struct xdp_buff_xsk *frag;
+
+	frag = list_last_entry(&xskb->pool->xskb_list, struct xdp_buff_xsk,
+			       xskb_list_node);
+	return &frag->xdp;
+}
+
 static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size)
 {
 	xdp->data = xdp->data_hard_start + XDP_PACKET_HEADROOM;
diff --git a/net/core/filter.c b/net/core/filter.c
index 0adaa4afa35f..5409254eb3f5 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -83,6 +83,7 @@ 
 #include <net/netfilter/nf_conntrack_bpf.h>
 #include <net/netkit.h>
 #include <linux/un.h>
+#include <net/xdp_sock_drv.h>
 
 #include "dev.h"
 
@@ -4075,6 +4076,35 @@  static int bpf_xdp_frags_increase_tail(struct xdp_buff *xdp, int offset)
 	return 0;
 }
 
+static void __shrink_data(struct xdp_buff *xdp, struct xdp_mem_info *mem_info,
+			  skb_frag_t *frag, int shrink)
+{
+	if (mem_info->type == MEM_TYPE_XSK_BUFF_POOL)
+		xsk_buff_get_tail(xdp)->data_end -= shrink;
+	skb_frag_size_sub(frag, shrink);
+}
+
+static bool shrink_data(struct xdp_buff *xdp, skb_frag_t *frag, int shrink)
+{
+	struct xdp_mem_info *mem_info = &xdp->rxq->mem;
+
+	if (skb_frag_size(frag) == shrink) {
+		struct page *page = skb_frag_page(frag);
+		struct xdp_buff *zc_frag = NULL;
+
+		if (mem_info->type == MEM_TYPE_XSK_BUFF_POOL) {
+			zc_frag = xsk_buff_get_tail(xdp);
+			xdp_buff_clear_frags_flag(zc_frag);
+			xsk_buff_tail_del(zc_frag);
+		}
+
+		__xdp_return(page_address(page), mem_info, false, zc_frag);
+		return true;
+	}
+	__shrink_data(xdp, mem_info, frag, shrink);
+	return false;
+}
+
 static int bpf_xdp_frags_shrink_tail(struct xdp_buff *xdp, int offset)
 {
 	struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
@@ -4089,17 +4119,10 @@  static int bpf_xdp_frags_shrink_tail(struct xdp_buff *xdp, int offset)
 
 		len_free += shrink;
 		offset -= shrink;
-
-		if (skb_frag_size(frag) == shrink) {
-			struct page *page = skb_frag_page(frag);
-
-			__xdp_return(page_address(page), &xdp->rxq->mem,
-				     false, NULL);
+		if (shrink_data(xdp, frag, shrink))
 			n_frags_free++;
-		} else {
-			skb_frag_size_sub(frag, shrink);
+		else
 			break;
-		}
 	}
 	sinfo->nr_frags -= n_frags_free;
 	sinfo->xdp_frags_size -= len_free;