xref: /linux/net/core/net_test.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #include <kunit/test.h>
4 
5 /* GSO */
6 
7 #include <linux/skbuff.h>
8 #include <net/gso.h>
9 
10 static const char hdr[] = "abcdefgh";
11 #define GSO_TEST_SIZE 1000
12 
13 static void __init_skb(struct sk_buff *skb)
14 {
15 	skb_reset_mac_header(skb);
16 	memcpy(skb_mac_header(skb), hdr, sizeof(hdr));
17 
18 	/* skb_segment expects skb->data at start of payload */
19 	skb_pull(skb, sizeof(hdr));
20 	skb_reset_network_header(skb);
21 	skb_reset_transport_header(skb);
22 
23 	/* proto is arbitrary, as long as not ETH_P_TEB or vlan */
24 	skb->protocol = htons(ETH_P_ATALK);
25 	skb_shinfo(skb)->gso_size = GSO_TEST_SIZE;
26 }
27 
28 enum gso_test_nr {
29 	GSO_TEST_LINEAR,
30 	GSO_TEST_NO_GSO,
31 	GSO_TEST_FRAGS,
32 	GSO_TEST_FRAGS_PURE,
33 	GSO_TEST_GSO_PARTIAL,
34 	GSO_TEST_FRAG_LIST,
35 	GSO_TEST_FRAG_LIST_PURE,
36 	GSO_TEST_FRAG_LIST_NON_UNIFORM,
37 	GSO_TEST_FRAG_LIST_HEAD_FRAG,
38 	GSO_TEST_FRAG_LIST_NO_HEAD_FRAG,
39 	GSO_TEST_GSO_BY_FRAGS,
40 	GSO_TEST_RESEGMENT,
41 	GSO_TEST_RESEGMENT_MULTI,
42 	GSO_TEST_RESEGMENT_TAIL,
43 	GSO_TEST_RESEGMENT_ONE_MSS,
44 };
45 
46 struct gso_test_case {
47 	enum gso_test_nr id;
48 	const char *name;
49 
50 	/* input */
51 	unsigned int linear_len;
52 	unsigned int nr_frags;
53 	const unsigned int *frags;
54 	unsigned int nr_frag_skbs;
55 	const unsigned int *frag_skbs;
56 	unsigned int max_segs;
57 	const unsigned int *frag_skb_frags;
58 	unsigned int gso_type;
59 	/* skbs from alloc_skb() instead of build_skb(), head_frag clear */
60 	bool no_head_frag;
61 
62 	/* output as expected */
63 	unsigned int nr_segs;
64 	const unsigned int *segs;
65 	const unsigned int *gso_segs;
66 };
67 
68 static struct gso_test_case cases[] = {
69 	{
70 		.id = GSO_TEST_NO_GSO,
71 		.name = "no_gso",
72 		.linear_len = GSO_TEST_SIZE,
73 		.nr_segs = 1,
74 		.segs = (const unsigned int[]) { GSO_TEST_SIZE },
75 	},
76 	{
77 		.id = GSO_TEST_LINEAR,
78 		.name = "linear",
79 		.linear_len = GSO_TEST_SIZE + GSO_TEST_SIZE + 1,
80 		.nr_segs = 3,
81 		.segs = (const unsigned int[]) { GSO_TEST_SIZE, GSO_TEST_SIZE, 1 },
82 	},
83 	{
84 		.id = GSO_TEST_FRAGS,
85 		.name = "frags",
86 		.linear_len = GSO_TEST_SIZE,
87 		.nr_frags = 2,
88 		.frags = (const unsigned int[]) { GSO_TEST_SIZE, 1 },
89 		.nr_segs = 3,
90 		.segs = (const unsigned int[]) { GSO_TEST_SIZE, GSO_TEST_SIZE, 1 },
91 	},
92 	{
93 		.id = GSO_TEST_FRAGS_PURE,
94 		.name = "frags_pure",
95 		.nr_frags = 3,
96 		.frags = (const unsigned int[]) { GSO_TEST_SIZE, GSO_TEST_SIZE, 2 },
97 		.nr_segs = 3,
98 		.segs = (const unsigned int[]) { GSO_TEST_SIZE, GSO_TEST_SIZE, 2 },
99 	},
100 	{
101 		.id = GSO_TEST_GSO_PARTIAL,
102 		.name = "gso_partial",
103 		.linear_len = GSO_TEST_SIZE,
104 		.nr_frags = 2,
105 		.frags = (const unsigned int[]) { GSO_TEST_SIZE, 3 },
106 		.nr_segs = 2,
107 		.segs = (const unsigned int[]) { 2 * GSO_TEST_SIZE, 3 },
108 	},
109 	{
110 		/* commit 89319d3801d1: frag_list on mss boundaries */
111 		.id = GSO_TEST_FRAG_LIST,
112 		.name = "frag_list",
113 		.linear_len = GSO_TEST_SIZE,
114 		.nr_frag_skbs = 2,
115 		.frag_skbs = (const unsigned int[]) { GSO_TEST_SIZE, GSO_TEST_SIZE },
116 		.nr_segs = 3,
117 		.segs = (const unsigned int[]) { GSO_TEST_SIZE, GSO_TEST_SIZE, GSO_TEST_SIZE },
118 	},
119 	{
120 		.id = GSO_TEST_FRAG_LIST_PURE,
121 		.name = "frag_list_pure",
122 		.nr_frag_skbs = 2,
123 		.frag_skbs = (const unsigned int[]) { GSO_TEST_SIZE, GSO_TEST_SIZE },
124 		.nr_segs = 2,
125 		.segs = (const unsigned int[]) { GSO_TEST_SIZE, GSO_TEST_SIZE },
126 	},
127 	{
128 		/* commit 43170c4e0ba7: GRO of frag_list trains */
129 		.id = GSO_TEST_FRAG_LIST_NON_UNIFORM,
130 		.name = "frag_list_non_uniform",
131 		.linear_len = GSO_TEST_SIZE,
132 		.nr_frag_skbs = 4,
133 		.frag_skbs = (const unsigned int[]) { GSO_TEST_SIZE, 1, GSO_TEST_SIZE, 2 },
134 		.nr_segs = 4,
135 		.segs = (const unsigned int[]) { GSO_TEST_SIZE, GSO_TEST_SIZE, GSO_TEST_SIZE, 3 },
136 	},
137 	{
138 		/* commit 13acc94eff12: frag_list members with linear data in a
139 		 * page fragment, shared as a page frag of the segment
140 		 */
141 		.id = GSO_TEST_FRAG_LIST_HEAD_FRAG,
142 		.name = "frag_list_head_frag",
143 		.linear_len = 8,
144 		.nr_frags = 1,
145 		.frags = (const unsigned int[]) { 64 },
146 		.nr_frag_skbs = 1,
147 		.frag_skbs = (const unsigned int[]) { 8 },
148 		.frag_skb_frags = (const unsigned int[]) { 64 },
149 		.gso_type = SKB_GSO_DODGY,
150 		.nr_segs = 1,
151 		.segs = (const unsigned int[]) { 144 },
152 	},
153 	{
154 		/* commit 3dcbdb134f32: gso_size smaller than the linear data,
155 		 * as after bpf_skb_change_proto() grew the header, with
156 		 * frag_list members whose linear data is not in a page fragment
157 		 */
158 		.id = GSO_TEST_FRAG_LIST_NO_HEAD_FRAG,
159 		.name = "frag_list_no_head_frag",
160 		.linear_len = GSO_TEST_SIZE + 20,
161 		.nr_frag_skbs = 1,
162 		.frag_skbs = (const unsigned int[]) { GSO_TEST_SIZE + 20 },
163 		.gso_type = SKB_GSO_DODGY,
164 		.no_head_frag = true,
165 		.nr_segs = 3,
166 		.segs = (const unsigned int[]) { GSO_TEST_SIZE, GSO_TEST_SIZE, 40 },
167 	},
168 	{
169 		/* commit 3953c46c3ac7 ("sk_buff: allow segmenting based on frag sizes") and
170 		 * commit 90017accff61 ("sctp: Add GSO support")
171 		 *
172 		 * "there will be a cover skb with protocol headers and
173 		 *  children ones containing the actual segments"
174 		 */
175 		.id = GSO_TEST_GSO_BY_FRAGS,
176 		.name = "gso_by_frags",
177 		.nr_frag_skbs = 4,
178 		.frag_skbs = (const unsigned int[]) { 100, 200, 300, 400 },
179 		.nr_segs = 4,
180 		.segs = (const unsigned int[]) { 100, 200, 300, 400 },
181 	},
182 	{
183 		.id = GSO_TEST_RESEGMENT,
184 		.name = "resegment",
185 		.linear_len = GSO_TEST_SIZE,
186 		.nr_frags = 3,
187 		.frags = (const unsigned int[]) {
188 			GSO_TEST_SIZE, GSO_TEST_SIZE, 3,
189 		},
190 		.max_segs = 2,
191 		.nr_segs = 2,
192 		.segs = (const unsigned int[]) {
193 			2 * GSO_TEST_SIZE, GSO_TEST_SIZE + 3,
194 		},
195 		.gso_segs = (const unsigned int[]) { 2, 2 },
196 	},
197 	{
198 		.id = GSO_TEST_RESEGMENT_MULTI,
199 		.name = "resegment_multi",
200 		.linear_len = 2 * GSO_TEST_SIZE,
201 		.nr_frags = 4,
202 		.frags = (const unsigned int[]) {
203 			GSO_TEST_SIZE, GSO_TEST_SIZE, GSO_TEST_SIZE, 3,
204 		},
205 		.max_segs = 2,
206 		.nr_segs = 3,
207 		.segs = (const unsigned int[]) {
208 			2 * GSO_TEST_SIZE, 2 * GSO_TEST_SIZE, GSO_TEST_SIZE + 3,
209 		},
210 		.gso_segs = (const unsigned int[]) { 2, 2, 2 },
211 	},
212 	{
213 		/* The last output is not a GSO skb. */
214 		.id = GSO_TEST_RESEGMENT_TAIL,
215 		.name = "resegment_tail",
216 		.linear_len = GSO_TEST_SIZE,
217 		.nr_frags = 2,
218 		.frags = (const unsigned int[]) {
219 			GSO_TEST_SIZE, 400,
220 		},
221 		.max_segs = 2,
222 		.nr_segs = 2,
223 		.segs = (const unsigned int[]) {
224 			2 * GSO_TEST_SIZE, 400,
225 		},
226 		.gso_segs = (const unsigned int[]) { 2, 0 },
227 	},
228 	{
229 		/* max_segs of 1 is the same as no limit. */
230 		.id = GSO_TEST_RESEGMENT_ONE_MSS,
231 		.name = "resegment_one_mss",
232 		.linear_len = GSO_TEST_SIZE,
233 		.nr_frags = 3,
234 		.frags = (const unsigned int[]) {
235 			GSO_TEST_SIZE, GSO_TEST_SIZE, 3,
236 		},
237 		.max_segs = 1,
238 		.nr_segs = 4,
239 		.segs = (const unsigned int[]) {
240 			GSO_TEST_SIZE, GSO_TEST_SIZE, GSO_TEST_SIZE, 3,
241 		},
242 		.gso_segs = (const unsigned int[]) { 0, 0, 0, 0 },
243 	},
244 };
245 
246 static void gso_test_case_to_desc(struct gso_test_case *t, char *desc)
247 {
248 	sprintf(desc, "%s", t->name);
249 }
250 
251 KUNIT_ARRAY_PARAM(gso_test, cases, gso_test_case_to_desc);
252 
253 static void gso_test_func(struct kunit *test)
254 {
255 	const int shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
256 	struct sk_buff *skb, *segs, *cur, *next, *last;
257 	const struct gso_test_case *tcase;
258 	netdev_features_t features;
259 	struct page *page;
260 	int i;
261 
262 	tcase = test->param_value;
263 
264 	if (tcase->no_head_frag) {
265 		skb = alloc_skb(sizeof(hdr) + tcase->linear_len, GFP_KERNEL);
266 	} else {
267 		page = alloc_page(GFP_KERNEL);
268 		KUNIT_ASSERT_NOT_NULL(test, page);
269 		skb = build_skb(page_address(page), sizeof(hdr) + tcase->linear_len + shinfo_size);
270 	}
271 	KUNIT_ASSERT_NOT_NULL(test, skb);
272 	__skb_put(skb, sizeof(hdr) + tcase->linear_len);
273 
274 	__init_skb(skb);
275 	skb_shinfo(skb)->gso_type = tcase->gso_type;
276 
277 	if (tcase->nr_frags) {
278 		unsigned int pg_off = 0;
279 
280 		page = alloc_page(GFP_KERNEL);
281 		KUNIT_ASSERT_NOT_NULL(test, page);
282 		page_ref_add(page, tcase->nr_frags - 1);
283 
284 		for (i = 0; i < tcase->nr_frags; i++) {
285 			skb_fill_page_desc(skb, i, page, pg_off, tcase->frags[i]);
286 			pg_off += tcase->frags[i];
287 		}
288 
289 		KUNIT_ASSERT_LE(test, pg_off, PAGE_SIZE);
290 
291 		skb->data_len = pg_off;
292 		skb->len += skb->data_len;
293 		skb->truesize += skb->data_len;
294 	}
295 
296 	if (tcase->frag_skbs) {
297 		unsigned int total_size = 0, total_true_size = 0;
298 		struct sk_buff *frag_skb, *prev = NULL;
299 
300 		for (i = 0; i < tcase->nr_frag_skbs; i++) {
301 			unsigned int frag_size, frag_len;
302 
303 			frag_size = tcase->frag_skbs[i];
304 			if (tcase->no_head_frag) {
305 				frag_skb = alloc_skb(frag_size, GFP_KERNEL);
306 			} else {
307 				page = alloc_page(GFP_KERNEL);
308 				KUNIT_ASSERT_NOT_NULL(test, page);
309 				frag_skb = build_skb(page_address(page),
310 						     frag_size + shinfo_size);
311 			}
312 			KUNIT_ASSERT_NOT_NULL(test, frag_skb);
313 			__skb_put(frag_skb, frag_size);
314 
315 			frag_len = tcase->frag_skb_frags ? tcase->frag_skb_frags[i] : 0;
316 			if (frag_len) {
317 				page = alloc_page(GFP_KERNEL);
318 				KUNIT_ASSERT_NOT_NULL(test, page);
319 				skb_fill_page_desc(frag_skb, 0, page, 0, frag_len);
320 
321 				frag_skb->data_len = frag_len;
322 				frag_skb->len += frag_len;
323 				frag_skb->truesize += frag_len;
324 			}
325 
326 			if (prev)
327 				prev->next = frag_skb;
328 			else
329 				skb_shinfo(skb)->frag_list = frag_skb;
330 			prev = frag_skb;
331 
332 			total_size += frag_skb->len;
333 			total_true_size += frag_skb->truesize;
334 		}
335 
336 		skb->len += total_size;
337 		skb->data_len += total_size;
338 		skb->truesize += total_true_size;
339 
340 		if (tcase->id == GSO_TEST_GSO_BY_FRAGS)
341 			skb_shinfo(skb)->gso_size = GSO_BY_FRAGS;
342 	}
343 
344 	features = NETIF_F_SG | NETIF_F_HW_CSUM;
345 	if (tcase->id == GSO_TEST_GSO_PARTIAL)
346 		features |= NETIF_F_GSO_PARTIAL;
347 
348 	/* TODO: this should also work with SG,
349 	 * rather than hit BUG_ON(i >= nfrags)
350 	 */
351 	if (tcase->id == GSO_TEST_FRAG_LIST_NON_UNIFORM)
352 		features &= ~NETIF_F_SG;
353 
354 	SKB_GSO_CB(skb)->max_segs = tcase->max_segs;
355 	segs = skb_segment(skb, features);
356 	if (IS_ERR(segs)) {
357 		KUNIT_FAIL(test, "segs error %pe", segs);
358 		goto free_gso_skb;
359 	} else if (!segs) {
360 		KUNIT_FAIL(test, "no segments");
361 		goto free_gso_skb;
362 	}
363 
364 	last = segs->prev;
365 	for (cur = segs, i = 0; cur; cur = next, i++) {
366 		next = cur->next;
367 
368 		if (i >= tcase->nr_segs)
369 			goto consume;
370 
371 		KUNIT_ASSERT_EQ(test, cur->len, sizeof(hdr) + tcase->segs[i]);
372 
373 		/* segs have skb->data pointing to the mac header */
374 		KUNIT_ASSERT_PTR_EQ(test, skb_mac_header(cur), cur->data);
375 		KUNIT_ASSERT_PTR_EQ(test, skb_network_header(cur), cur->data + sizeof(hdr));
376 
377 		/* header was copied to all segs */
378 		KUNIT_ASSERT_EQ(test, memcmp(skb_mac_header(cur), hdr, sizeof(hdr)), 0);
379 		if (tcase->gso_segs) {
380 			unsigned int gso_segs = tcase->gso_segs[i];
381 
382 			KUNIT_EXPECT_EQ(test, skb_shinfo(cur)->gso_segs,
383 					gso_segs);
384 			if (!gso_segs) {
385 				KUNIT_EXPECT_FALSE(test, skb_is_gso(cur));
386 			} else {
387 				KUNIT_EXPECT_TRUE(test, skb_is_gso(cur));
388 				KUNIT_EXPECT_EQ(test, skb_shinfo(cur)->gso_size,
389 						GSO_TEST_SIZE);
390 			}
391 		}
392 
393 		/* last seg can be found through segs->prev pointer */
394 		if (!next)
395 			KUNIT_ASSERT_PTR_EQ(test, cur, last);
396 
397 consume:
398 		consume_skb(cur);
399 	}
400 
401 	KUNIT_ASSERT_EQ(test, i, tcase->nr_segs);
402 
403 free_gso_skb:
404 	consume_skb(skb);
405 }
406 
407 /* IP tunnel flags */
408 
409 #include <net/ip_tunnels.h>
410 
411 struct ip_tunnel_flags_test {
412 	const char	*name;
413 
414 	const u16	*src_bits;
415 	const u16	*exp_bits;
416 	u8		src_num;
417 	u8		exp_num;
418 
419 	__be16		exp_val;
420 	bool		exp_comp;
421 };
422 
423 #define IP_TUNNEL_FLAGS_TEST(n, src, comp, eval, exp) {	\
424 	.name		= (n),				\
425 	.src_bits	= (src),			\
426 	.src_num	= ARRAY_SIZE(src),		\
427 	.exp_comp	= (comp),			\
428 	.exp_val	= (eval),			\
429 	.exp_bits	= (exp),			\
430 	.exp_num	= ARRAY_SIZE(exp),		\
431 }
432 
433 /* These are __be16-compatible and can be compared as is */
434 static const u16 ip_tunnel_flags_1[] = {
435 	IP_TUNNEL_KEY_BIT,
436 	IP_TUNNEL_STRICT_BIT,
437 	IP_TUNNEL_ERSPAN_OPT_BIT,
438 };
439 
440 /* Due to the previous flags design limitation, setting either
441  * ``IP_TUNNEL_CSUM_BIT`` (on Big Endian) or ``IP_TUNNEL_DONT_FRAGMENT_BIT``
442  * (on Little) also sets VTI/ISATAP bit. In the bitmap implementation, they
443  * correspond to ``BIT(16)``, which is bigger than ``U16_MAX``, but still is
444  * backward-compatible.
445  */
446 #ifdef __LITTLE_ENDIAN
447 #define IP_TUNNEL_CONFLICT_BIT	IP_TUNNEL_DONT_FRAGMENT_BIT
448 #else
449 #define IP_TUNNEL_CONFLICT_BIT	IP_TUNNEL_CSUM_BIT
450 #endif
451 
452 static const u16 ip_tunnel_flags_2_src[] = {
453 	IP_TUNNEL_CONFLICT_BIT,
454 };
455 
456 static const u16 ip_tunnel_flags_2_exp[] = {
457 	IP_TUNNEL_CONFLICT_BIT,
458 	IP_TUNNEL_SIT_ISATAP_BIT,
459 };
460 
461 /* Bits 17 and higher are not compatible with __be16 flags */
462 static const u16 ip_tunnel_flags_3_src[] = {
463 	IP_TUNNEL_VXLAN_OPT_BIT,
464 	17,
465 	18,
466 	20,
467 };
468 
469 static const u16 ip_tunnel_flags_3_exp[] = {
470 	IP_TUNNEL_VXLAN_OPT_BIT,
471 };
472 
473 static const struct ip_tunnel_flags_test ip_tunnel_flags_test[] = {
474 	IP_TUNNEL_FLAGS_TEST("compat", ip_tunnel_flags_1, true,
475 			     cpu_to_be16(BIT(IP_TUNNEL_KEY_BIT) |
476 					 BIT(IP_TUNNEL_STRICT_BIT) |
477 					 BIT(IP_TUNNEL_ERSPAN_OPT_BIT)),
478 			     ip_tunnel_flags_1),
479 	IP_TUNNEL_FLAGS_TEST("conflict", ip_tunnel_flags_2_src, true,
480 			     VTI_ISVTI, ip_tunnel_flags_2_exp),
481 	IP_TUNNEL_FLAGS_TEST("new", ip_tunnel_flags_3_src, false,
482 			     cpu_to_be16(BIT(IP_TUNNEL_VXLAN_OPT_BIT)),
483 			     ip_tunnel_flags_3_exp),
484 };
485 
486 static void
487 ip_tunnel_flags_test_case_to_desc(const struct ip_tunnel_flags_test *t,
488 				  char *desc)
489 {
490 	strscpy(desc, t->name, KUNIT_PARAM_DESC_SIZE);
491 }
492 KUNIT_ARRAY_PARAM(ip_tunnel_flags_test, ip_tunnel_flags_test,
493 		  ip_tunnel_flags_test_case_to_desc);
494 
495 static void ip_tunnel_flags_test_run(struct kunit *test)
496 {
497 	const struct ip_tunnel_flags_test *t = test->param_value;
498 	IP_TUNNEL_DECLARE_FLAGS(src) = { };
499 	IP_TUNNEL_DECLARE_FLAGS(exp) = { };
500 	IP_TUNNEL_DECLARE_FLAGS(out);
501 
502 	for (u32 j = 0; j < t->src_num; j++)
503 		__set_bit(t->src_bits[j], src);
504 	for (u32 j = 0; j < t->exp_num; j++)
505 		__set_bit(t->exp_bits[j], exp);
506 
507 	KUNIT_ASSERT_EQ(test, t->exp_comp,
508 			ip_tunnel_flags_is_be16_compat(src));
509 	KUNIT_ASSERT_EQ(test, (__force u16)t->exp_val,
510 			(__force u16)ip_tunnel_flags_to_be16(src));
511 
512 	ip_tunnel_flags_from_be16(out, t->exp_val);
513 	KUNIT_ASSERT_TRUE(test, __ipt_flag_op(bitmap_equal, exp, out));
514 }
515 
516 /* Netmem & SKB fragment refcounting */
517 
518 #include <linux/skbuff_ref.h>
519 #include <net/netmem.h>
520 #include <net/netdev_rx_queue.h>
521 #include <net/page_pool/helpers.h>
522 #include <net/page_pool/memory_provider.h>
523 #include <net/tcp.h>
524 #include "devmem.h"
525 #include "netmem_priv.h"
526 
527 static void netmem_ref_test_page(struct kunit *test)
528 {
529 	struct sk_buff *skb;
530 	netmem_ref netmem;
531 	struct page *page;
532 	int base_ref;
533 
534 	page = alloc_page(GFP_KERNEL);
535 	KUNIT_ASSERT_NOT_NULL(test, page);
536 	netmem = page_to_netmem(page);
537 	base_ref = page_ref_count(page);
538 
539 	/* Layer 1: netmem_inc_ref / netmem_dec_ref use page non-pp refcount */
540 	netmem_inc_ref(netmem);
541 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref + 1);
542 	netmem_dec_ref(netmem);
543 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
544 
545 #ifdef CONFIG_PAGE_POOL
546 	/* Layer 2: napi_pp_get/put_page return false on non-PP page */
547 	KUNIT_EXPECT_FALSE(test, napi_pp_get_page(netmem));
548 	KUNIT_EXPECT_FALSE(test, napi_pp_put_page(netmem));
549 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
550 #endif
551 
552 	/* Layer 3: skb_netmem_ref / unref fall back to non-pp on non-PP page */
553 	skb_netmem_ref(netmem, false);
554 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref + 1);
555 	skb_netmem_unref(netmem, false);
556 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
557 
558 	skb_netmem_ref(netmem, true);
559 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref + 1);
560 	skb_netmem_unref(netmem, true);
561 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
562 
563 	/* Layer 4: skb_frag_ref / skb_frag_unref on non-PP page */
564 	skb = alloc_skb(64, GFP_KERNEL);
565 	KUNIT_ASSERT_NOT_NULL(test, skb);
566 	skb_fill_netmem_desc(skb, 0, netmem, 0, 64);
567 
568 	skb->pp_recycle = 1;
569 	skb_frag_ref(skb, 0);
570 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref + 1);
571 	skb_frag_unref(skb, 0);
572 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
573 
574 	skb_shinfo(skb)->nr_frags = 0;
575 	consume_skb(skb);
576 	__free_page(page);
577 }
578 
579 #ifdef CONFIG_PAGE_POOL
580 static void netmem_ref_test_pp_page(struct kunit *test)
581 {
582 	struct page_pool_params pp_params = {
583 		.order = 0,
584 		.pool_size = 4,
585 		.nid = NUMA_NO_NODE,
586 	};
587 	struct page_pool *pool;
588 	struct sk_buff *skb, *copy, *clone, *split;
589 	netmem_ref netmem;
590 	struct page *page;
591 	long base_pp_ref;
592 	int base_ref;
593 
594 	pool = page_pool_create(&pp_params);
595 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pool);
596 
597 	page = page_pool_dev_alloc_pages(pool);
598 	KUNIT_ASSERT_NOT_NULL(test, page);
599 	netmem = page_to_netmem(page);
600 	base_ref = page_ref_count(page);
601 	base_pp_ref = atomic_long_read(&page->pp_ref_count);
602 
603 	/* Layer 1: netmem_inc/dec_ref always use non-pp refcount */
604 	netmem_inc_ref(netmem);
605 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref + 1);
606 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
607 			base_pp_ref);
608 	netmem_dec_ref(netmem);
609 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
610 
611 	/* Layer 2: napi_pp_get/put_page use pp_ref_count */
612 	KUNIT_EXPECT_TRUE(test, napi_pp_get_page(netmem));
613 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
614 			base_pp_ref + 1);
615 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
616 	KUNIT_EXPECT_TRUE(test, napi_pp_put_page(netmem));
617 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
618 			base_pp_ref);
619 
620 	/* Layer 3: skb_netmem_ref / unref obey recycle flag */
621 	skb_netmem_ref(netmem, false);
622 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref + 1);
623 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
624 			base_pp_ref);
625 	skb_netmem_unref(netmem, false);
626 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
627 
628 	skb_netmem_ref(netmem, true);
629 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
630 			base_pp_ref + 1);
631 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
632 	skb_netmem_unref(netmem, true);
633 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
634 			base_pp_ref);
635 
636 	/* Layer 4: skb_frag_ref / skb_frag_unref match on pp_recycle=1 & 0 */
637 	skb = alloc_skb(64, GFP_KERNEL);
638 	KUNIT_ASSERT_NOT_NULL(test, skb);
639 	skb_fill_netmem_desc(skb, 0, netmem, 0, 64);
640 	skb->len = 64;
641 	skb->data_len = 64;
642 
643 	skb->pp_recycle = 1;
644 	skb_frag_ref(skb, 0);
645 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
646 			base_pp_ref + 1);
647 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
648 	skb_frag_unref(skb, 0);
649 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
650 			base_pp_ref);
651 
652 	/* pskb_copy on pp_recycle=1 skb propagates pp_recycle & pp_ref_count */
653 	copy = pskb_copy(skb, GFP_KERNEL);
654 	KUNIT_ASSERT_NOT_NULL(test, copy);
655 	KUNIT_EXPECT_TRUE(test, copy->pp_recycle);
656 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
657 			base_pp_ref + 1);
658 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
659 	consume_skb(copy);
660 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
661 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
662 			base_pp_ref);
663 
664 	/* skb_zerocopy on pp_recycle=1 skb propagates pp_ref_count */
665 	copy = alloc_skb(0, GFP_KERNEL);
666 	KUNIT_ASSERT_NOT_NULL(test, copy);
667 	skb_put(copy, skb_tailroom(copy));
668 	skb->head_frag = 1;
669 	KUNIT_ASSERT_EQ(test, skb_zerocopy(copy, skb, skb->len, 0), 0);
670 	skb->head_frag = 0;
671 	KUNIT_EXPECT_TRUE(test, copy->pp_recycle);
672 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
673 			base_pp_ref + 1);
674 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
675 	consume_skb(copy);
676 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
677 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
678 			base_pp_ref);
679 
680 	/* skb_clone + pskb_expand_head preserves pp_recycle and pp_ref_count */
681 	clone = skb_clone(skb, GFP_KERNEL);
682 	KUNIT_ASSERT_NOT_NULL(test, clone);
683 	KUNIT_ASSERT_EQ(test, pskb_expand_head(clone, 32, 0, GFP_KERNEL), 0);
684 	KUNIT_EXPECT_TRUE(test, clone->pp_recycle);
685 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
686 			base_pp_ref + 1);
687 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
688 	consume_skb(clone);
689 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
690 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
691 			base_pp_ref);
692 
693 	/* skb_split inside linear header & inside nonlinear page_pool frags */
694 	skb_shinfo(skb)->nr_frags = 0;
695 	skb->len = 0;
696 	skb->data_len = 0;
697 	skb_reset_tail_pointer(skb);
698 	skb_put(skb, 32);
699 	skb_fill_netmem_desc(skb, 0, netmem, 0, 64);
700 	skb->len += 64;
701 	skb->data_len = 64;
702 	split = alloc_skb(32, GFP_KERNEL);
703 	KUNIT_ASSERT_NOT_NULL(test, split);
704 	skb_split(skb, split, 16);
705 	KUNIT_EXPECT_TRUE(test, split->pp_recycle);
706 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
707 			base_pp_ref);
708 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
709 	skb_shinfo(split)->nr_frags = 0;
710 	consume_skb(split);
711 	skb->len = 0;
712 	skb->data_len = 0;
713 	skb_reset_tail_pointer(skb);
714 	skb_fill_netmem_desc(skb, 0, netmem, 0, 64);
715 	skb->len = 64;
716 	skb->data_len = 64;
717 
718 	split = alloc_skb(0, GFP_KERNEL);
719 	KUNIT_ASSERT_NOT_NULL(test, split);
720 	skb_split(skb, split, 32);
721 	KUNIT_EXPECT_TRUE(test, split->pp_recycle);
722 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
723 			base_pp_ref + 1);
724 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
725 	consume_skb(split);
726 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
727 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
728 			base_pp_ref);
729 
730 	/* skb_segment on pp_recycle=1 skb propagates pp_recycle to segments */
731 	skb->protocol = htons(ETH_P_IP);
732 	skb_reset_mac_header(skb);
733 	skb_reset_network_header(skb);
734 	skb_shinfo(skb)->gso_size = 16;
735 	split = skb_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
736 	KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(split));
737 	KUNIT_EXPECT_TRUE(test, split->pp_recycle);
738 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
739 			base_pp_ref + 2);
740 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
741 	kfree_skb_list(split);
742 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
743 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
744 			base_pp_ref);
745 
746 	/* skb_shift balances pp_ref_count across split, partial coalesce,
747 	 * multi-frag coalesce + partial next-frag shift, and full merge.
748 	 */
749 	skb_fill_netmem_desc(skb, 0, netmem, 0, 32);
750 	skb_fill_netmem_desc(skb, 1, netmem, 64, 32);
751 	page_pool_ref_page(page);
752 	skb->len = 64;
753 	skb->data_len = 64;
754 	split = alloc_skb(0, GFP_KERNEL);
755 	KUNIT_ASSERT_NOT_NULL(test, split);
756 	split->pp_recycle = 1;
757 	KUNIT_EXPECT_EQ(test, skb_shift(split, skb, 16), 16);
758 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
759 			base_pp_ref + 2);
760 	KUNIT_EXPECT_EQ(test, skb_shift(split, skb, 8), 8);
761 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
762 			base_pp_ref + 2);
763 	KUNIT_EXPECT_EQ(test, skb_shift(split, skb, 24), 24);
764 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
765 			base_pp_ref + 2);
766 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
767 	KUNIT_EXPECT_EQ(test, skb_shift(split, skb, 16), 16);
768 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
769 			base_pp_ref + 1);
770 	skb_shinfo(split)->nr_frags = 0;
771 	consume_skb(split);
772 	page_pool_put_full_page(pool, page, false);
773 	skb_fill_netmem_desc(skb, 0, netmem, 0, 64);
774 	skb->len = 64;
775 	skb->data_len = 64;
776 
777 	skb->pp_recycle = 0;
778 	skb_frag_ref(skb, 0);
779 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref + 1);
780 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
781 			base_pp_ref);
782 	skb_frag_unref(skb, 0);
783 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
784 
785 	skb_shinfo(skb)->nr_frags = 0;
786 	consume_skb(skb);
787 	page_pool_put_full_page(pool, page, false);
788 	page_pool_destroy(pool);
789 
790 	/* Order-1 compound page_pool page: tail netmem resolves to head &
791 	 * build_skb(head_frag=1, pp_recycle=1) skb_zerocopy uses pp_ref_count.
792 	 */
793 	pp_params.order = 1;
794 	pool = page_pool_create(&pp_params);
795 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pool);
796 	page = page_pool_dev_alloc_pages(pool);
797 	KUNIT_ASSERT_NOT_NULL(test, page);
798 	base_ref = page_ref_count(page);
799 	base_pp_ref = atomic_long_read(&page->pp_ref_count);
800 
801 	netmem = page_to_netmem(page + 1);
802 	KUNIT_EXPECT_TRUE(test, napi_pp_get_page(netmem));
803 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
804 			base_pp_ref + 1);
805 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
806 	KUNIT_EXPECT_TRUE(test, napi_pp_put_page(netmem));
807 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
808 			base_pp_ref);
809 
810 	skb = build_skb(page_address(page), PAGE_SIZE);
811 	KUNIT_ASSERT_NOT_NULL(test, skb);
812 	skb->pp_recycle = 1;
813 	skb_put(skb, 64);
814 	page_pool_ref_page(page);
815 
816 	copy = alloc_skb(0, GFP_KERNEL);
817 	KUNIT_ASSERT_NOT_NULL(test, copy);
818 	skb_put(copy, skb_tailroom(copy));
819 	KUNIT_ASSERT_EQ(test, skb_zerocopy(copy, skb, skb->len, 0), 0);
820 	KUNIT_EXPECT_TRUE(test, copy->pp_recycle);
821 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
822 			base_pp_ref + 2);
823 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
824 	consume_skb(copy);
825 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
826 			base_pp_ref + 1);
827 	consume_skb(skb);
828 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
829 			base_pp_ref);
830 
831 	page_pool_put_full_page(pool, page, false);
832 	page_pool_destroy(pool);
833 }
834 #endif
835 
836 #if defined(CONFIG_PAGE_POOL) && defined(CONFIG_NET_DEVMEM)
837 static struct net_devmem_dmabuf_binding binding;
838 
839 static void dummy_percpu_ref_release(struct percpu_ref *ref)
840 {
841 }
842 
843 static void netmem_ref_test_net_iov(struct kunit *test)
844 {
845 	struct page_pool_params pp_params = {
846 		.order = 0,
847 		.pool_size = 4,
848 		.nid = NUMA_NO_NODE,
849 	};
850 	struct sk_buff *skb, *clone, *copy;
851 	struct page_pool *pool;
852 	struct net_iov niov = {};
853 	netmem_ref netmem;
854 	long base_ref;
855 	int err;
856 
857 	memset(&binding, 0, sizeof(binding));
858 	err = percpu_ref_init(&binding.ref, dummy_percpu_ref_release,
859 			      PERCPU_REF_INIT_ATOMIC, GFP_KERNEL);
860 	KUNIT_ASSERT_EQ(test, err, 0);
861 
862 	net_iov_init(&niov, &binding.area, NET_IOV_DMABUF);
863 	netmem = net_iov_to_netmem(&niov);
864 	base_ref = atomic_long_read(&binding.ref.data->count);
865 
866 	/* Non-PP net_iov: net_iov_inc/dec_ref & netmem_inc/dec_ref */
867 	net_iov_inc_ref(&niov);
868 	KUNIT_EXPECT_EQ(test, atomic_long_read(&binding.ref.data->count),
869 			base_ref + 1);
870 	net_iov_dec_ref(&niov);
871 	KUNIT_EXPECT_EQ(test, atomic_long_read(&binding.ref.data->count),
872 			base_ref);
873 
874 	KUNIT_EXPECT_FALSE(test, napi_pp_get_page(netmem));
875 	KUNIT_EXPECT_FALSE(test, napi_pp_put_page(netmem));
876 
877 	skb_netmem_ref(netmem, true);
878 	KUNIT_EXPECT_EQ(test, atomic_long_read(&binding.ref.data->count),
879 			base_ref + 1);
880 	skb_netmem_unref(netmem, true);
881 	KUNIT_EXPECT_EQ(test, atomic_long_read(&binding.ref.data->count),
882 			base_ref);
883 
884 	/* Attach net_iov to a page_pool to test PP net_iov behavior */
885 	pool = page_pool_create(&pp_params);
886 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pool);
887 	netmem_set_pp(netmem, pool);
888 	netmem_or_pp_magic(netmem, PP_SIGNATURE);
889 	atomic_long_set(&niov.desc.pp_ref_count, 2);
890 
891 	/* Layer 0 & 1: net_iov/netmem_inc_ref grab non-pp ref on PP */
892 	net_iov_inc_ref(&niov);
893 	KUNIT_EXPECT_EQ(test, atomic_long_read(&binding.ref.data->count),
894 			base_ref + 1);
895 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 2L);
896 	net_iov_dec_ref(&niov);
897 	KUNIT_EXPECT_EQ(test, atomic_long_read(&binding.ref.data->count),
898 			base_ref);
899 
900 	/* Layer 2: napi_pp_get/put_page use pp_ref_count */
901 	KUNIT_EXPECT_TRUE(test, napi_pp_get_page(netmem));
902 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 3L);
903 	KUNIT_EXPECT_EQ(test, atomic_long_read(&binding.ref.data->count),
904 			base_ref);
905 	KUNIT_EXPECT_TRUE(test, napi_pp_put_page(netmem));
906 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 2L);
907 
908 	/* Layer 4: skb_frag_ref / skb_frag_unref on PP net_iov */
909 	skb = alloc_skb(64, GFP_KERNEL);
910 	KUNIT_ASSERT_NOT_NULL(test, skb);
911 	skb_fill_netmem_desc(skb, 0, netmem, 0, 64);
912 	skb->len = 64;
913 	skb->data_len = 64;
914 
915 	skb->pp_recycle = 1;
916 	skb_frag_ref(skb, 0);
917 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 3L);
918 	KUNIT_EXPECT_EQ(test, atomic_long_read(&binding.ref.data->count),
919 			base_ref);
920 	skb_frag_unref(skb, 0);
921 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 2L);
922 
923 	/* pskb_copy on pp_recycle=1 net_iov skb keeps pp_ref_count */
924 	copy = pskb_copy(skb, GFP_KERNEL);
925 	KUNIT_ASSERT_NOT_NULL(test, copy);
926 	KUNIT_EXPECT_TRUE(test, copy->pp_recycle);
927 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 3L);
928 	KUNIT_EXPECT_EQ(test, atomic_long_read(&binding.ref.data->count),
929 			base_ref);
930 	consume_skb(copy);
931 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 2L);
932 
933 	/* skb_zerocopy on pp_recycle=1 net_iov skb keeps pp_ref_count */
934 	copy = alloc_skb(0, GFP_KERNEL);
935 	KUNIT_ASSERT_NOT_NULL(test, copy);
936 	skb_put(copy, skb_tailroom(copy));
937 	skb->head_frag = 1;
938 	skb->unreadable = 1;
939 	KUNIT_ASSERT_EQ(test, skb_zerocopy(copy, skb, skb->len, 0), 0);
940 	skb->head_frag = 0;
941 	KUNIT_EXPECT_TRUE(test, copy->pp_recycle);
942 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 3L);
943 	KUNIT_EXPECT_EQ(test, atomic_long_read(&binding.ref.data->count),
944 			base_ref);
945 	consume_skb(copy);
946 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 2L);
947 
948 	/* skb_shift balances pp_ref_count on split and merge */
949 	copy = alloc_skb(0, GFP_KERNEL);
950 	KUNIT_ASSERT_NOT_NULL(test, copy);
951 	copy->pp_recycle = 1;
952 	KUNIT_EXPECT_EQ(test, skb_shift(copy, skb, 16), 16);
953 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 3L);
954 	KUNIT_EXPECT_EQ(test, atomic_long_read(&binding.ref.data->count),
955 			base_ref);
956 	KUNIT_EXPECT_EQ(test, skb_shift(copy, skb, 48), 48);
957 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 2L);
958 	skb_shinfo(copy)->nr_frags = 0;
959 	consume_skb(copy);
960 	skb_fill_netmem_desc(skb, 0, netmem, 0, 64);
961 	skb->len = 64;
962 	skb->data_len = 64;
963 
964 	/* Uncloning a cloned pp_recycle=1 net_iov skb keeps pp_ref_count */
965 	clone = skb_clone(skb, GFP_KERNEL);
966 	KUNIT_ASSERT_NOT_NULL(test, clone);
967 	KUNIT_ASSERT_EQ(test, pskb_expand_head(skb, 32, 0, GFP_KERNEL), 0);
968 	KUNIT_EXPECT_TRUE(test, skb->pp_recycle);
969 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 3L);
970 	KUNIT_EXPECT_EQ(test, atomic_long_read(&binding.ref.data->count),
971 			base_ref);
972 	consume_skb(clone);
973 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 2L);
974 
975 	skb->pp_recycle = 0;
976 	skb_frag_ref(skb, 0);
977 	KUNIT_EXPECT_EQ(test, atomic_long_read(&binding.ref.data->count),
978 			base_ref + 1);
979 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 2L);
980 	skb_frag_unref(skb, 0);
981 	KUNIT_EXPECT_EQ(test, atomic_long_read(&binding.ref.data->count),
982 			base_ref);
983 
984 	skb_shinfo(skb)->nr_frags = 0;
985 	consume_skb(skb);
986 	netmem_clear_pp_magic(netmem);
987 	netmem_set_pp(netmem, NULL);
988 	page_pool_destroy(pool);
989 	percpu_ref_exit(&binding.ref);
990 }
991 
992 static void netmem_ref_test_release_data(struct kunit *test)
993 {
994 	struct page_pool_params pp_params = {
995 		.order = 0,
996 		.pool_size = 4,
997 		.nid = NUMA_NO_NODE,
998 	};
999 	struct sk_buff *skb, *clone1, *clone2, *ext;
1000 	struct net_iov niov = {};
1001 	struct page_pool *pool;
1002 	struct page *page;
1003 	netmem_ref netmem;
1004 	long base_pp_ref;
1005 	int base_ref;
1006 	int i;
1007 
1008 	memset(&binding, 0, sizeof(binding));
1009 	pool = page_pool_create(&pp_params);
1010 	KUNIT_ASSERT_FALSE(test, IS_ERR(pool));
1011 	page = page_pool_dev_alloc_pages(pool);
1012 	KUNIT_ASSERT_NOT_NULL(test, page);
1013 	netmem = page_to_netmem(page);
1014 	page_pool_ref_netmem(netmem);
1015 
1016 	/* 3-way clone chain: only last clone drops pp_ref_count */
1017 	skb = alloc_skb(64, GFP_KERNEL);
1018 	KUNIT_ASSERT_NOT_NULL(test, skb);
1019 	skb_put(skb, 32);
1020 	skb->pp_recycle = 1;
1021 	skb_fill_netmem_desc(skb, 0, netmem, 0, 64);
1022 	skb->len += 64;
1023 	skb->data_len = 64;
1024 	base_ref = page_ref_count(page);
1025 	base_pp_ref = atomic_long_read(&page->pp_ref_count);
1026 
1027 	page_pool_ref_netmem(netmem);
1028 	clone1 = skb_clone(skb, GFP_KERNEL);
1029 	KUNIT_ASSERT_NOT_NULL(test, clone1);
1030 	clone2 = skb_clone(clone1, GFP_KERNEL);
1031 	KUNIT_ASSERT_NOT_NULL(test, clone2);
1032 
1033 	/* Freeing head skb first leaves clone1/clone2 pp_recycle=1 intact */
1034 	consume_skb(skb);
1035 	KUNIT_EXPECT_TRUE(test, clone1->pp_recycle);
1036 	KUNIT_EXPECT_TRUE(test, clone2->pp_recycle);
1037 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
1038 			base_pp_ref + 1);
1039 	consume_skb(clone2);
1040 	KUNIT_EXPECT_TRUE(test, clone1->pp_recycle);
1041 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
1042 			base_pp_ref + 1);
1043 	consume_skb(clone1);
1044 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
1045 			base_pp_ref);
1046 
1047 	/* All 4 combinations of (unclone skb vs clone) x (free order) */
1048 	for (i = 0; i < 4; i++) {
1049 		skb = alloc_skb(64, GFP_KERNEL);
1050 		KUNIT_ASSERT_NOT_NULL(test, skb);
1051 		skb_put(skb, 32);
1052 		skb->pp_recycle = 1;
1053 		skb_fill_netmem_desc(skb, 0, netmem, 0, 64);
1054 		skb->len += 64;
1055 		skb->data_len = 64;
1056 		page_pool_ref_netmem(netmem);
1057 
1058 		clone1 = skb_clone(skb, GFP_KERNEL);
1059 		KUNIT_ASSERT_NOT_NULL(test, clone1);
1060 		KUNIT_ASSERT_EQ(test,
1061 				pskb_expand_head((i & 1) ? clone1 : skb,
1062 						 32, 0, GFP_KERNEL), 0);
1063 		KUNIT_EXPECT_TRUE(test, skb->pp_recycle);
1064 		KUNIT_EXPECT_TRUE(test, clone1->pp_recycle);
1065 		KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
1066 				base_pp_ref + 2);
1067 		KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
1068 
1069 		if (i & 2) {
1070 			consume_skb(clone1);
1071 			KUNIT_EXPECT_EQ(test,
1072 					atomic_long_read(&page->pp_ref_count),
1073 					base_pp_ref + 1);
1074 			consume_skb(skb);
1075 		} else {
1076 			consume_skb(skb);
1077 			KUNIT_EXPECT_EQ(test,
1078 					atomic_long_read(&page->pp_ref_count),
1079 					base_pp_ref + 1);
1080 			consume_skb(clone1);
1081 		}
1082 		KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
1083 				base_pp_ref);
1084 		KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
1085 	}
1086 
1087 	/* pskb_extract carving inside header & inside nonlinear frags */
1088 	skb = alloc_skb(64, GFP_KERNEL);
1089 	KUNIT_ASSERT_NOT_NULL(test, skb);
1090 	skb_put(skb, 32);
1091 	skb->pp_recycle = 1;
1092 	skb_fill_netmem_desc(skb, 0, netmem, 0, 64);
1093 	skb->len += 64;
1094 	skb->data_len = 64;
1095 	page_pool_ref_netmem(netmem);
1096 
1097 	/* Carve inside header keeping frag -> pp_ref_count +1 until free */
1098 	ext = pskb_extract(skb, 16, skb->len - 16, GFP_KERNEL);
1099 	KUNIT_ASSERT_NOT_NULL(test, ext);
1100 	KUNIT_EXPECT_TRUE(test, ext->pp_recycle);
1101 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
1102 			base_pp_ref + 2);
1103 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
1104 	consume_skb(ext);
1105 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
1106 			base_pp_ref + 1);
1107 
1108 	/* Carve + trim + skb_condense ref/unrefs pp_ref_count cleanly */
1109 	ext = pskb_extract(skb, 16, 48, GFP_KERNEL);
1110 	KUNIT_ASSERT_NOT_NULL(test, ext);
1111 	KUNIT_EXPECT_TRUE(test, ext->pp_recycle);
1112 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
1113 			base_pp_ref + 1);
1114 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
1115 	consume_skb(ext);
1116 
1117 	/* Carve inside nonlinear keeping frag -> pp_ref_count +1 until free */
1118 	ext = pskb_extract(skb, 48, skb->len - 48, GFP_KERNEL);
1119 	KUNIT_ASSERT_NOT_NULL(test, ext);
1120 	KUNIT_EXPECT_TRUE(test, ext->pp_recycle);
1121 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
1122 			base_pp_ref + 2);
1123 	KUNIT_EXPECT_EQ(test, page_ref_count(page), base_ref);
1124 	consume_skb(ext);
1125 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
1126 			base_pp_ref + 1);
1127 
1128 	/* skb_morph drops old skb data via skb_release_data */
1129 	page_pool_ref_netmem(netmem);
1130 	clone1 = alloc_skb(64, GFP_KERNEL);
1131 	KUNIT_ASSERT_NOT_NULL(test, clone1);
1132 	clone1->pp_recycle = 1;
1133 	skb_fill_netmem_desc(clone1, 0, netmem, 0, 64);
1134 	clone1->len = 64;
1135 	clone1->data_len = 64;
1136 	skb_morph(clone1, skb);
1137 	KUNIT_EXPECT_TRUE(test, clone1->pp_recycle);
1138 	KUNIT_EXPECT_EQ(test, atomic_long_read(&page->pp_ref_count),
1139 			base_pp_ref + 1);
1140 	consume_skb(clone1);
1141 	kfree_skb(skb);
1142 	page_pool_put_full_page(pool, page, false);
1143 	page_pool_put_full_page(pool, page, false);
1144 
1145 	/* Also verify pskb_extract on page_pool net_iov */
1146 	KUNIT_ASSERT_EQ(test,
1147 			percpu_ref_init(&binding.ref, dummy_percpu_ref_release,
1148 					PERCPU_REF_INIT_ATOMIC, GFP_KERNEL),
1149 			0);
1150 	net_iov_init(&niov, &binding.area, NET_IOV_DMABUF);
1151 	netmem = net_iov_to_netmem(&niov);
1152 	netmem_set_pp(netmem, pool);
1153 	netmem_or_pp_magic(netmem, PP_SIGNATURE);
1154 	atomic_long_set(&niov.desc.pp_ref_count, 2);
1155 
1156 	skb = alloc_skb(64, GFP_KERNEL);
1157 	KUNIT_ASSERT_NOT_NULL(test, skb);
1158 	skb_put(skb, 32);
1159 	skb->pp_recycle = 1;
1160 	skb_fill_netmem_desc(skb, 0, netmem, 0, 64);
1161 	skb->len += 64;
1162 	skb->data_len = 64;
1163 
1164 	ext = pskb_extract(skb, 48, skb->len - 48, GFP_KERNEL);
1165 	KUNIT_ASSERT_NOT_NULL(test, ext);
1166 	KUNIT_EXPECT_TRUE(test, ext->pp_recycle);
1167 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 3L);
1168 	consume_skb(ext);
1169 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 2L);
1170 
1171 	skb_shinfo(skb)->nr_frags = 0;
1172 	consume_skb(skb);
1173 	netmem_clear_pp_magic(netmem);
1174 	netmem_set_pp(netmem, NULL);
1175 	page_pool_destroy(pool);
1176 	percpu_ref_exit(&binding.ref);
1177 }
1178 
1179 static void netmem_ref_test_skb_segment(struct kunit *test)
1180 {
1181 	struct page_pool_params pp_params = {
1182 		.order = 0,
1183 		.pool_size = 4,
1184 		.nid = NUMA_NO_NODE,
1185 	};
1186 	struct ubuf_info uarg = {
1187 		.flags = SKBFL_ZEROCOPY_ENABLE,
1188 	};
1189 	struct sk_buff *head, *list, *segs, *seg;
1190 	struct net_iov niov = {};
1191 	struct page *pp_page, *reg_page;
1192 	netmem_ref pp_nm, niov_nm;
1193 	struct page_pool *pool;
1194 	long base_pp_ref, base_iov_ref;
1195 	int base_pp_page_ref, base_reg_ref;
1196 
1197 	memset(&binding, 0, sizeof(binding));
1198 	pool = page_pool_create(&pp_params);
1199 	KUNIT_ASSERT_FALSE(test, IS_ERR(pool));
1200 	pp_page = page_pool_dev_alloc_pages(pool);
1201 	KUNIT_ASSERT_NOT_NULL(test, pp_page);
1202 	pp_nm = page_to_netmem(pp_page);
1203 	page_pool_ref_netmem(pp_nm);
1204 	reg_page = alloc_page(GFP_KERNEL);
1205 	KUNIT_ASSERT_NOT_NULL(test, reg_page);
1206 
1207 	KUNIT_ASSERT_EQ(test,
1208 			percpu_ref_init(&binding.ref, dummy_percpu_ref_release,
1209 					PERCPU_REF_INIT_ATOMIC, GFP_KERNEL),
1210 			0);
1211 	net_iov_init(&niov, &binding.area, NET_IOV_DMABUF);
1212 	niov_nm = net_iov_to_netmem(&niov);
1213 	netmem_set_pp(niov_nm, pool);
1214 	netmem_or_pp_magic(niov_nm, PP_SIGNATURE);
1215 	atomic_long_set(&niov.desc.pp_ref_count, 2);
1216 
1217 	base_pp_ref = atomic_long_read(&pp_page->pp_ref_count);
1218 	base_pp_page_ref = page_ref_count(pp_page);
1219 	base_reg_ref = page_ref_count(reg_page);
1220 	base_iov_ref = atomic_long_read(&binding.ref.data->count);
1221 
1222 	/* 1. Single GSO skb with page_pool net_iov frags */
1223 	head = alloc_skb(64, GFP_KERNEL);
1224 	KUNIT_ASSERT_NOT_NULL(test, head);
1225 	head->pp_recycle = 1;
1226 	head->protocol = htons(ETH_P_IP);
1227 	skb_reset_mac_header(head);
1228 	skb_reset_network_header(head);
1229 	skb_fill_netmem_desc(head, 0, niov_nm, 0, 64);
1230 	head->len = 64;
1231 	head->data_len = 64;
1232 	skb_shinfo(head)->gso_size = 16;
1233 
1234 	segs = skb_segment(head, NETIF_F_SG | NETIF_F_HW_CSUM);
1235 	KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(segs));
1236 	for (seg = segs; seg; seg = seg->next)
1237 		KUNIT_EXPECT_TRUE(test, seg->pp_recycle);
1238 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 6L);
1239 	KUNIT_EXPECT_EQ(test, atomic_long_read(&binding.ref.data->count),
1240 			base_iov_ref);
1241 	kfree_skb_list(segs);
1242 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 2L);
1243 	skb_shinfo(head)->nr_frags = 0;
1244 	consume_skb(head);
1245 
1246 	/* 2. GRO frag_list fast path (skb_clone of pp_recycle=1 list_skb) */
1247 	head = alloc_skb(64, GFP_KERNEL);
1248 	list = alloc_skb(64, GFP_KERNEL);
1249 	KUNIT_ASSERT_NOT_NULL(test, head);
1250 	KUNIT_ASSERT_NOT_NULL(test, list);
1251 	head->pp_recycle = 1;
1252 	list->pp_recycle = 1;
1253 	head->protocol = htons(ETH_P_IP);
1254 	skb_reset_mac_header(head);
1255 	skb_reset_network_header(head);
1256 	skb_put(head, 16);
1257 	skb_fill_netmem_desc(head, 0, pp_nm, 0, 16);
1258 	head->len += 16;
1259 	head->data_len = 16;
1260 	page_pool_ref_netmem(pp_nm);
1261 
1262 	skb_put(list, 16);
1263 	skb_fill_netmem_desc(list, 0, pp_nm, 16, 16);
1264 	list->len += 16;
1265 	list->data_len = 16;
1266 	page_pool_ref_netmem(pp_nm);
1267 
1268 	skb_shinfo(head)->frag_list = list;
1269 	head->len += list->len;
1270 	head->data_len += list->len;
1271 	skb_shinfo(head)->gso_size = 32;
1272 
1273 	segs = skb_segment(head, NETIF_F_SG | NETIF_F_HW_CSUM);
1274 	KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(segs));
1275 	for (seg = segs; seg; seg = seg->next)
1276 		KUNIT_EXPECT_TRUE(test, seg->pp_recycle);
1277 	KUNIT_EXPECT_EQ(test, atomic_long_read(&pp_page->pp_ref_count),
1278 			base_pp_ref + 4);
1279 	KUNIT_EXPECT_EQ(test, page_ref_count(pp_page), base_pp_page_ref);
1280 	kfree_skb_list(segs);
1281 	KUNIT_EXPECT_EQ(test, atomic_long_read(&pp_page->pp_ref_count),
1282 			base_pp_ref + 2);
1283 	consume_skb(head);
1284 	KUNIT_EXPECT_EQ(test, atomic_long_read(&pp_page->pp_ref_count),
1285 			base_pp_ref);
1286 
1287 	/* 3. Mixed GRO frag_list (head pp_recycle=1 + list pp_recycle=0):
1288 	 * straddling segment linearizes its pp_recycle=1 prefix before
1289 	 * attaching the pp_recycle=0 frag so no nskb mixes incompatible
1290 	 * fragment refcount types; non-straddling segments stay zero-copy SG.
1291 	 */
1292 	head = alloc_skb(64, GFP_KERNEL);
1293 	list = alloc_skb(0, GFP_KERNEL);
1294 	KUNIT_ASSERT_NOT_NULL(test, head);
1295 	KUNIT_ASSERT_NOT_NULL(test, list);
1296 	head->pp_recycle = 1;
1297 	list->pp_recycle = 0;
1298 	head->protocol = htons(ETH_P_IP);
1299 	skb_reset_mac_header(head);
1300 	skb_reset_network_header(head);
1301 	skb_fill_netmem_desc(head, 0, pp_nm, 0, 24);
1302 	head->len = 24;
1303 	head->data_len = 24;
1304 	page_pool_ref_netmem(pp_nm);
1305 
1306 	skb_fill_page_desc(list, 0, reg_page, 0, 24);
1307 	get_page(reg_page);
1308 	list->len = 24;
1309 	list->data_len = 24;
1310 
1311 	skb_shinfo(head)->frag_list = list;
1312 	head->len += list->len;
1313 	head->data_len += list->len;
1314 	skb_shinfo(head)->gso_size = 16;
1315 
1316 	segs = skb_segment(head, NETIF_F_SG | NETIF_F_HW_CSUM);
1317 	KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(segs));
1318 	KUNIT_EXPECT_TRUE(test, segs->pp_recycle);
1319 	KUNIT_EXPECT_FALSE(test, segs->next->pp_recycle);
1320 	KUNIT_EXPECT_FALSE(test, segs->next->next->pp_recycle);
1321 	KUNIT_EXPECT_EQ(test, atomic_long_read(&pp_page->pp_ref_count),
1322 			base_pp_ref + 2);
1323 	KUNIT_EXPECT_EQ(test, page_ref_count(pp_page), base_pp_page_ref);
1324 	KUNIT_EXPECT_EQ(test, page_ref_count(reg_page), base_reg_ref + 3);
1325 	kfree_skb_list(segs);
1326 	consume_skb(head);
1327 	KUNIT_EXPECT_EQ(test, atomic_long_read(&pp_page->pp_ref_count),
1328 			base_pp_ref);
1329 	KUNIT_EXPECT_EQ(test, page_ref_count(pp_page), base_pp_page_ref);
1330 	KUNIT_EXPECT_EQ(test, page_ref_count(reg_page), base_reg_ref);
1331 
1332 	/* 4. Reverse mixed frag_list (head pp_recycle=0 + list pp_recycle=1):
1333 	 * Crossing from pp_recycle=0 head into pp_recycle=1 list_skb must not
1334 	 * elevate page_ref_count(pp_page) instead of pp_ref_count.
1335 	 */
1336 	head = alloc_skb(64, GFP_KERNEL);
1337 	list = alloc_skb(0, GFP_KERNEL);
1338 	KUNIT_ASSERT_NOT_NULL(test, head);
1339 	KUNIT_ASSERT_NOT_NULL(test, list);
1340 	head->pp_recycle = 0;
1341 	list->pp_recycle = 1;
1342 	head->protocol = htons(ETH_P_IP);
1343 	skb_reset_mac_header(head);
1344 	skb_reset_network_header(head);
1345 	skb_fill_page_desc(head, 0, reg_page, 0, 24);
1346 	get_page(reg_page);
1347 	head->len = 24;
1348 	head->data_len = 24;
1349 
1350 	skb_fill_netmem_desc(list, 0, pp_nm, 0, 24);
1351 	page_pool_ref_netmem(pp_nm);
1352 	list->len = 24;
1353 	list->data_len = 24;
1354 
1355 	skb_shinfo(head)->frag_list = list;
1356 	head->len += list->len;
1357 	head->data_len += list->len;
1358 	skb_shinfo(head)->gso_size = 16;
1359 
1360 	segs = skb_segment(head, NETIF_F_SG | NETIF_F_HW_CSUM);
1361 	KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(segs));
1362 	KUNIT_EXPECT_FALSE(test, segs->pp_recycle);
1363 	KUNIT_EXPECT_TRUE(test, segs->next->pp_recycle);
1364 	KUNIT_EXPECT_TRUE(test, segs->next->next->pp_recycle);
1365 	KUNIT_EXPECT_EQ(test, atomic_long_read(&pp_page->pp_ref_count),
1366 			base_pp_ref + 3);
1367 	KUNIT_EXPECT_EQ(test, page_ref_count(pp_page), base_pp_page_ref);
1368 	KUNIT_EXPECT_EQ(test, page_ref_count(reg_page), base_reg_ref + 2);
1369 	kfree_skb_list(segs);
1370 	consume_skb(head);
1371 	KUNIT_EXPECT_EQ(test, atomic_long_read(&pp_page->pp_ref_count),
1372 			base_pp_ref);
1373 	KUNIT_EXPECT_EQ(test, page_ref_count(pp_page), base_pp_page_ref);
1374 	KUNIT_EXPECT_EQ(test, page_ref_count(reg_page), base_reg_ref);
1375 
1376 	/* 5. Linear head_skb (nr_frags=0) + mixed pp_recycle frag_list where
1377 	 * member A has linear + frag (pp_recycle=1) and tail member B has
1378 	 * linear len == skb_headlen(A) (pp_recycle=0): keeps SG clone path
1379 	 * synchronized without hitting BUG_ON(i >= nfrags).
1380 	 */
1381 	head = alloc_skb(64, GFP_KERNEL);
1382 	list = alloc_skb(64, GFP_KERNEL);
1383 	seg = alloc_skb(64, GFP_KERNEL);
1384 	KUNIT_ASSERT_NOT_NULL(test, head);
1385 	KUNIT_ASSERT_NOT_NULL(test, list);
1386 	KUNIT_ASSERT_NOT_NULL(test, seg);
1387 	head->pp_recycle = 0;
1388 	list->pp_recycle = 1;
1389 	seg->pp_recycle = 0;
1390 	head->protocol = htons(ETH_P_IP);
1391 	skb_reset_mac_header(head);
1392 	skb_reset_network_header(head);
1393 	skb_put(head, 32);
1394 	skb_put(list, 8);
1395 	skb_fill_netmem_desc(list, 0, pp_nm, 0, 24);
1396 	page_pool_ref_netmem(pp_nm);
1397 	list->len += 24;
1398 	list->data_len = 24;
1399 	skb_put(seg, 8);
1400 	list->next = seg;
1401 	skb_shinfo(head)->frag_list = list;
1402 	head->len += list->len + seg->len;
1403 	head->data_len = list->len + seg->len;
1404 	skb_shinfo(head)->gso_size = 32;
1405 
1406 	segs = skb_segment(head, NETIF_F_SG | NETIF_F_HW_CSUM);
1407 	KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(segs));
1408 	KUNIT_EXPECT_FALSE(test, segs->pp_recycle);
1409 	KUNIT_EXPECT_TRUE(test, segs->next->pp_recycle);
1410 	KUNIT_EXPECT_FALSE(test, segs->next->next->pp_recycle);
1411 	KUNIT_EXPECT_EQ(test, atomic_long_read(&pp_page->pp_ref_count),
1412 			base_pp_ref + 2);
1413 	KUNIT_EXPECT_EQ(test, page_ref_count(pp_page), base_pp_page_ref);
1414 	kfree_skb_list(segs);
1415 	consume_skb(head);
1416 	KUNIT_EXPECT_EQ(test, atomic_long_read(&pp_page->pp_ref_count),
1417 			base_pp_ref);
1418 
1419 	/* 6. skb_zerocopy -EFAULT rollback when from->head_frag (pp_page) is
1420 	 * followed by unreadable net_iov frag drops pp_ref_count via
1421 	 * skb_frag_unref(to, 0).
1422 	 */
1423 	head = build_skb(page_address(pp_page), PAGE_SIZE);
1424 	KUNIT_ASSERT_NOT_NULL(test, head);
1425 	head->pp_recycle = 1;
1426 	skb_put(head, 32);
1427 	page_pool_ref_netmem(pp_nm);
1428 	skb_fill_netmem_desc(head, 0, niov_nm, 0, 32);
1429 	napi_pp_get_page(niov_nm);
1430 	head->len += 32;
1431 	head->data_len += 32;
1432 
1433 	seg = alloc_skb(0, GFP_KERNEL);
1434 	KUNIT_ASSERT_NOT_NULL(test, seg);
1435 	skb_put(seg, skb_tailroom(seg));
1436 	KUNIT_EXPECT_EQ(test, skb_zerocopy(seg, head, head->len, 0), -EFAULT);
1437 	KUNIT_EXPECT_EQ(test, atomic_long_read(&pp_page->pp_ref_count),
1438 			base_pp_ref + 1);
1439 	skb_shinfo(seg)->nr_frags = 0;
1440 	consume_skb(seg);
1441 	consume_skb(head);
1442 	KUNIT_EXPECT_EQ(test, atomic_long_read(&pp_page->pp_ref_count),
1443 			base_pp_ref);
1444 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 2L);
1445 
1446 	/* 7. skb_zerocopy -ENOMEM rollback when skb_orphan_frags() fails on
1447 	 * shared zerocopy skb drops pp_ref_count via skb_frag_unref(to, 0).
1448 	 */
1449 	head = build_skb(page_address(pp_page), PAGE_SIZE);
1450 	KUNIT_ASSERT_NOT_NULL(test, head);
1451 	head->pp_recycle = 1;
1452 	skb_put(head, 32);
1453 	page_pool_ref_netmem(pp_nm);
1454 	skb_zcopy_init(head, &uarg);
1455 	skb_get(head);
1456 
1457 	seg = alloc_skb(0, GFP_KERNEL);
1458 	KUNIT_ASSERT_NOT_NULL(test, seg);
1459 	skb_put(seg, skb_tailroom(seg));
1460 	KUNIT_EXPECT_EQ(test, skb_zerocopy(seg, head, head->len, 0), -ENOMEM);
1461 	KUNIT_EXPECT_EQ(test, atomic_long_read(&pp_page->pp_ref_count),
1462 			base_pp_ref + 1);
1463 	KUNIT_EXPECT_EQ(test, page_ref_count(pp_page), base_pp_page_ref);
1464 	skb_shinfo(seg)->nr_frags = 0;
1465 	consume_skb(seg);
1466 	consume_skb(head);
1467 	skb_shinfo(head)->flags &= ~SKBFL_ZEROCOPY_ENABLE;
1468 	consume_skb(head);
1469 	KUNIT_EXPECT_EQ(test, atomic_long_read(&pp_page->pp_ref_count),
1470 			base_pp_ref);
1471 
1472 	page_pool_put_full_page(pool, pp_page, false);
1473 	page_pool_put_full_page(pool, pp_page, false);
1474 	__free_page(reg_page);
1475 	netmem_clear_pp_magic(niov_nm);
1476 	netmem_set_pp(niov_nm, NULL);
1477 	page_pool_destroy(pool);
1478 	percpu_ref_exit(&binding.ref);
1479 }
1480 
1481 static void netmem_ref_test_try_coalesce(struct kunit *test)
1482 {
1483 	struct page_pool_params pp_params = {
1484 		.order = 0,
1485 		.pool_size = 4,
1486 		.nid = NUMA_NO_NODE,
1487 	};
1488 	struct sk_buff *to, *from, *clone;
1489 	struct net_iov niov = {};
1490 	struct page *pp_page, *reg_page;
1491 	netmem_ref pp_nm, niov_nm;
1492 	struct page_pool *pool;
1493 	long base_pp_ref, base_iov_ref;
1494 	int base_pp_page_ref, base_reg_ref;
1495 	bool fragstolen;
1496 	int delta;
1497 
1498 	memset(&binding, 0, sizeof(binding));
1499 	pool = page_pool_create(&pp_params);
1500 	KUNIT_ASSERT_FALSE(test, IS_ERR(pool));
1501 	pp_page = page_pool_dev_alloc_pages(pool);
1502 	KUNIT_ASSERT_NOT_NULL(test, pp_page);
1503 	pp_nm = page_to_netmem(pp_page);
1504 	page_pool_ref_netmem(pp_nm);
1505 
1506 	reg_page = alloc_page(GFP_KERNEL);
1507 	KUNIT_ASSERT_NOT_NULL(test, reg_page);
1508 
1509 	KUNIT_ASSERT_EQ(test,
1510 			percpu_ref_init(&binding.ref, dummy_percpu_ref_release,
1511 					PERCPU_REF_INIT_ATOMIC, GFP_KERNEL),
1512 			0);
1513 	net_iov_init(&niov, &binding.area, NET_IOV_DMABUF);
1514 	niov_nm = net_iov_to_netmem(&niov);
1515 	netmem_set_pp(niov_nm, pool);
1516 	netmem_or_pp_magic(niov_nm, PP_SIGNATURE);
1517 	atomic_long_set(&niov.desc.pp_ref_count, 2);
1518 
1519 	base_pp_ref = atomic_long_read(&pp_page->pp_ref_count);
1520 	base_pp_page_ref = page_ref_count(pp_page);
1521 	base_reg_ref = page_ref_count(reg_page);
1522 	base_iov_ref = atomic_long_read(&binding.ref.data->count);
1523 
1524 	/* 1. Uncloned pp_recycle=1 from: frags stolen without extra ref */
1525 	to = alloc_skb(0, GFP_KERNEL);
1526 	from = alloc_skb(0, GFP_KERNEL);
1527 	KUNIT_ASSERT_NOT_NULL(test, to);
1528 	KUNIT_ASSERT_NOT_NULL(test, from);
1529 	skb_put(to, skb_tailroom(to));
1530 	to->pp_recycle = 1;
1531 	from->pp_recycle = 1;
1532 	skb_add_rx_frag(from, 0, pp_page, 0, 64, PAGE_SIZE);
1533 	page_pool_ref_netmem(pp_nm);
1534 	KUNIT_EXPECT_TRUE(test, skb_try_coalesce(to, from, &fragstolen,
1535 						 &delta));
1536 	KUNIT_EXPECT_EQ(test, skb_shinfo(from)->nr_frags, 0);
1537 	KUNIT_EXPECT_EQ(test, atomic_long_read(&pp_page->pp_ref_count),
1538 			base_pp_ref + 1);
1539 	consume_skb(from);
1540 	consume_skb(to);
1541 	KUNIT_EXPECT_EQ(test, atomic_long_read(&pp_page->pp_ref_count),
1542 			base_pp_ref);
1543 
1544 	/* 2. Cloned pp_recycle=1 from (pp_page + non-PP reg_page fallback):
1545 	 * skb_frag_ref(from, i) increments pp_ref_count for pp_page and
1546 	 * page_ref_count for reg_page.
1547 	 */
1548 	to = alloc_skb(0, GFP_KERNEL);
1549 	from = alloc_skb(0, GFP_KERNEL);
1550 	KUNIT_ASSERT_NOT_NULL(test, to);
1551 	KUNIT_ASSERT_NOT_NULL(test, from);
1552 	skb_put(to, skb_tailroom(to));
1553 	to->pp_recycle = 1;
1554 	from->pp_recycle = 1;
1555 	skb_add_rx_frag(from, 0, pp_page, 0, 32, PAGE_SIZE);
1556 	page_pool_ref_netmem(pp_nm);
1557 	skb_add_rx_frag(from, 1, reg_page, 0, 32, PAGE_SIZE);
1558 	get_page(reg_page);
1559 
1560 	clone = skb_clone(from, GFP_KERNEL);
1561 	KUNIT_ASSERT_NOT_NULL(test, clone);
1562 	KUNIT_EXPECT_TRUE(test, skb_try_coalesce(to, from, &fragstolen,
1563 						 &delta));
1564 	KUNIT_EXPECT_EQ(test, atomic_long_read(&pp_page->pp_ref_count),
1565 			base_pp_ref + 2);
1566 	KUNIT_EXPECT_EQ(test, page_ref_count(pp_page), base_pp_page_ref);
1567 	KUNIT_EXPECT_EQ(test, page_ref_count(reg_page), base_reg_ref + 2);
1568 	consume_skb(from);
1569 	consume_skb(clone);
1570 	consume_skb(to);
1571 	KUNIT_EXPECT_EQ(test, atomic_long_read(&pp_page->pp_ref_count),
1572 			base_pp_ref);
1573 	KUNIT_EXPECT_EQ(test, page_ref_count(pp_page), base_pp_page_ref);
1574 	KUNIT_EXPECT_EQ(test, page_ref_count(reg_page), base_reg_ref);
1575 
1576 	/* 3. Cloned unreadable pp_recycle=1 net_iov skb: skb_try_coalesce
1577 	 * increments niov.desc.pp_ref_count via skb_frag_ref(from, i).
1578 	 */
1579 	to = alloc_skb(0, GFP_KERNEL);
1580 	from = alloc_skb(0, GFP_KERNEL);
1581 	KUNIT_ASSERT_NOT_NULL(test, to);
1582 	KUNIT_ASSERT_NOT_NULL(test, from);
1583 	skb_put(to, skb_tailroom(to));
1584 	to->pp_recycle = 1;
1585 	to->unreadable = 1;
1586 	from->pp_recycle = 1;
1587 	skb_add_rx_frag_netmem(from, 0, niov_nm, 0, 64, PAGE_SIZE);
1588 	napi_pp_get_page(niov_nm);
1589 
1590 	clone = skb_clone(from, GFP_KERNEL);
1591 	KUNIT_ASSERT_NOT_NULL(test, clone);
1592 	KUNIT_EXPECT_TRUE(test, skb_try_coalesce(to, from, &fragstolen,
1593 						 &delta));
1594 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 4L);
1595 	KUNIT_EXPECT_EQ(test, atomic_long_read(&binding.ref.data->count),
1596 			base_iov_ref);
1597 	consume_skb(from);
1598 	consume_skb(clone);
1599 	consume_skb(to);
1600 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 2L);
1601 
1602 	page_pool_put_full_page(pool, pp_page, false);
1603 	page_pool_put_full_page(pool, pp_page, false);
1604 	__free_page(reg_page);
1605 	netmem_clear_pp_magic(niov_nm);
1606 	netmem_set_pp(niov_nm, NULL);
1607 	page_pool_destroy(pool);
1608 	percpu_ref_exit(&binding.ref);
1609 }
1610 
1611 #ifdef CONFIG_INET
1612 static void netmem_ref_test_tcp_devmem_recv(struct kunit *test)
1613 {
1614 	char cmsg_buf[CMSG_SPACE(sizeof(struct dmabuf_cmsg))]
1615 		__aligned(__alignof__(struct cmsghdr)) = {};
1616 	struct page_pool_params pp_params = {
1617 		.order = 0,
1618 		.pool_size = 4,
1619 		.nid = NUMA_NO_NODE,
1620 	};
1621 	struct msghdr msg = {};
1622 	struct dmabuf_token token;
1623 	struct dmabuf_cmsg *cmsg;
1624 	struct net_iov niov = {};
1625 	struct page_pool *pool;
1626 	struct cmsghdr *cm;
1627 	struct socket *sock;
1628 	struct sk_buff *skb;
1629 	struct kvec iov = {};
1630 	netmem_ref niov_nm;
1631 	long base_iov_ref;
1632 	int ret;
1633 
1634 	memset(&binding, 0, sizeof(binding));
1635 	KUNIT_ASSERT_EQ(test,
1636 			sock_create_kern(&init_net, AF_INET, SOCK_STREAM,
1637 					 IPPROTO_TCP, &sock),
1638 			0);
1639 	sock->sk->sk_state = TCP_ESTABLISHED;
1640 	inet_csk(sock->sk)->icsk_ack.rcv_mss = 64;
1641 	tcp_sk(sock->sk)->copied_seq = 0;
1642 
1643 	pool = page_pool_create(&pp_params);
1644 	if (IS_ERR(pool)) {
1645 		sock->sk->sk_state = TCP_CLOSE;
1646 		sock_release(sock);
1647 		KUNIT_ASSERT_FALSE(test, IS_ERR(pool));
1648 	}
1649 
1650 	ret = percpu_ref_init(&binding.ref, dummy_percpu_ref_release,
1651 			      PERCPU_REF_INIT_ATOMIC, GFP_KERNEL);
1652 	if (ret) {
1653 		page_pool_destroy(pool);
1654 		sock->sk->sk_state = TCP_CLOSE;
1655 		sock_release(sock);
1656 		KUNIT_ASSERT_EQ(test, ret, 0);
1657 	}
1658 	binding.area.niovs = &niov;
1659 	net_iov_init(&niov, &binding.area, NET_IOV_DMABUF);
1660 	niov_nm = net_iov_to_netmem(&niov);
1661 	netmem_set_pp(niov_nm, pool);
1662 	netmem_or_pp_magic(niov_nm, PP_SIGNATURE);
1663 	atomic_long_set(&niov.desc.pp_ref_count, 2);
1664 	base_iov_ref = atomic_long_read(&binding.ref.data->count);
1665 
1666 	/* 1. Receive via tcp_recvmsg_dmabuf -> SO_DEVMEM_DONTNEED release */
1667 	skb = alloc_skb(0, GFP_KERNEL);
1668 	KUNIT_ASSERT_NOT_NULL(test, skb);
1669 	skb->pp_recycle = 1;
1670 	skb_add_rx_frag_netmem(skb, 0, niov_nm, 0, 64, PAGE_SIZE);
1671 	napi_pp_get_page(niov_nm);
1672 	TCP_SKB_CB(skb)->seq = 0;
1673 	TCP_SKB_CB(skb)->end_seq = 64;
1674 	skb_queue_tail(&sock->sk->sk_receive_queue, skb);
1675 
1676 	msg.msg_control = cmsg_buf;
1677 	msg.msg_controllen = sizeof(cmsg_buf);
1678 	ret = kernel_recvmsg(sock, &msg, &iov, 0, 64,
1679 			     MSG_SOCK_DEVMEM | MSG_DONTWAIT);
1680 	KUNIT_ASSERT_EQ(test, ret, 64);
1681 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 3L);
1682 	KUNIT_EXPECT_EQ(test, atomic_long_read(&binding.ref.data->count),
1683 			base_iov_ref);
1684 
1685 	cm = (struct cmsghdr *)cmsg_buf;
1686 	cmsg = CMSG_DATA(cm);
1687 	token.token_start = cmsg->frag_token;
1688 	token.token_count = 1;
1689 	KUNIT_EXPECT_EQ(test,
1690 			sock_setsockopt(sock, SOL_SOCKET, SO_DEVMEM_DONTNEED,
1691 					KERNEL_SOCKPTR(&token), sizeof(token)),
1692 			1);
1693 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 2L);
1694 
1695 	/* 2. Receive second packet & destroy socket to test
1696 	 * tcp_release_user_frags() -> napi_pp_put_page().
1697 	 */
1698 	skb = alloc_skb(0, GFP_KERNEL);
1699 	KUNIT_ASSERT_NOT_NULL(test, skb);
1700 	skb->pp_recycle = 1;
1701 	skb_add_rx_frag_netmem(skb, 0, niov_nm, 0, 64, PAGE_SIZE);
1702 	napi_pp_get_page(niov_nm);
1703 	TCP_SKB_CB(skb)->seq = 64;
1704 	TCP_SKB_CB(skb)->end_seq = 128;
1705 	skb_queue_tail(&sock->sk->sk_receive_queue, skb);
1706 
1707 	msg.msg_control = cmsg_buf;
1708 	msg.msg_controllen = sizeof(cmsg_buf);
1709 	ret = kernel_recvmsg(sock, &msg, &iov, 0, 64,
1710 			     MSG_SOCK_DEVMEM | MSG_DONTWAIT);
1711 	KUNIT_ASSERT_EQ(test, ret, 64);
1712 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 3L);
1713 
1714 	sock->sk->sk_state = TCP_CLOSE;
1715 	sock_release(sock);
1716 	KUNIT_EXPECT_EQ(test, atomic_long_read(&niov.desc.pp_ref_count), 2L);
1717 
1718 	netmem_clear_pp_magic(niov_nm);
1719 	netmem_set_pp(niov_nm, NULL);
1720 	page_pool_destroy(pool);
1721 	percpu_ref_exit(&binding.ref);
1722 }
1723 #endif
1724 #endif
1725 
1726 static struct kunit_case net_test_cases[] = {
1727 	KUNIT_CASE_PARAM(gso_test_func, gso_test_gen_params),
1728 	KUNIT_CASE_PARAM(ip_tunnel_flags_test_run,
1729 			 ip_tunnel_flags_test_gen_params),
1730 	KUNIT_CASE(netmem_ref_test_page),
1731 #ifdef CONFIG_PAGE_POOL
1732 	KUNIT_CASE(netmem_ref_test_pp_page),
1733 #endif
1734 #if defined(CONFIG_PAGE_POOL) && defined(CONFIG_NET_DEVMEM)
1735 	KUNIT_CASE(netmem_ref_test_net_iov),
1736 	KUNIT_CASE(netmem_ref_test_release_data),
1737 	KUNIT_CASE(netmem_ref_test_skb_segment),
1738 	KUNIT_CASE(netmem_ref_test_try_coalesce),
1739 #ifdef CONFIG_INET
1740 	KUNIT_CASE(netmem_ref_test_tcp_devmem_recv),
1741 #endif
1742 #endif
1743 	{ },
1744 };
1745 
1746 static struct kunit_suite net_test_suite = {
1747 	.name		= "net_core",
1748 	.test_cases	= net_test_cases,
1749 };
1750 kunit_test_suite(net_test_suite);
1751 
1752 MODULE_DESCRIPTION("KUnit tests for networking core");
1753 MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
1754 MODULE_LICENSE("GPL");
1755