Skip to contentSkip to conversation

Fix partial copy of non-linear test_run output

PATCH Superseded Sun Jian 4 participants 11 messages Jun 17 – Jun 19

Subsystems (5)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Sun Jian

author
#0 lore raw

v3 cover Fix partial copy of non-linear test_run output

When BPF_PROG_TEST_RUN returns non-linear output and userspace provides a short data_out buffer, bpf_test_finish() can return -ENOSPC before copying the packet prefix or updating data_size_out.

Fix this by deriving the linear copy length from the packet layout rather than from the already-clamped copy_size. Add selftest coverage for both non-linear skb and XDP frags paths.

Changes in v3:

* Keep the fix patch minimal by leaving the existing offset declaration unchanged.
* Drop unnecessary memset() calls from the new selftests.
* Keep the pass-through TC program and larger test packet for the skb case. pkt_v4 is too small once the short IPv4 input check is accounted for, and the existing packet-access program fails before reaching the partial copy-out path with such a short linear area.

Changes in v2:

* Fix the Fixes tag to point to the commit that introduced the shared non-linear copy-out logic.
* Drop skb-specific wording from the fix commit.
* Move the selftest from skb_load_bytes.c to prog_run_opts.c.
* Add XDP frags coverage in addition to non-linear skb coverage.

v2:
https://lore.kernel.org/bpf/[email protected]/

v1:
https://lore.kernel.org/bpf/[email protected]/

Tested with:
./test_progs -t prog_run_opts -v
./test_progs -t skb_load_bytes -v
./test_progs -t xdp_pull_data -v

Sun Jian (2):
bpf: Fix partial copy of non-linear test_run output selftests/bpf: Cover partial copy of non-linear test_run output

 net/bpf/test_run.c                            |  8 +--
 .../selftests/bpf/prog_tests/prog_run_opts.c  | 70 +++++++++++++++++++
 .../selftests/bpf/progs/test_pkt_access.c     | 12 ++++
 3 files changed, 84 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v3 1/2 bpf: Fix partial copy of non-linear test_run output

For non-linear test_run output, bpf_test_finish() derives the linear data copy length from copy_size - frag_size. This only matches the linear data length when copy_size is the full packet size.

When userspace provides a short data_out buffer, copy_size is clamped to that buffer size. If copy_size is smaller than frag_size, the computed length becomes negative and bpf_test_finish() returns -ENOSPC before copying the packet prefix or updating data_size_out.

Compute the linear data length from the packet layout instead, and clamp the linear copy length to copy_size. This preserves the expected partial-copy semantics: return -ENOSPC, copy the packet prefix that fits in data_out, and report the full packet length through data_size_out.

Fixes 7855e0db150ad ("bpf: test_run: add xdp_shared_info pointer in bpf_test_finish signature")
---
 net/bpf/test_run.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
net/bpf/test_run.c +2 -6
index 2bc04feadfab..f15c613aaa4e 100644
@@ -453,12 +453,8 @@ static int bpf_test_finish(const union bpf_attr *kattr,
 	}
 
 	if (data_out) {
-		int len = sinfo ? copy_size - frag_size : copy_size;
-
-		if (len < 0) {
-			err = -ENOSPC;
-			goto out;
-		}
+		u32 head_len = size - frag_size;
+		u32 len = min(copy_size, head_len);
 
 		if (copy_to_user(data_out, data, len))
 			goto out;
Signature · 1 line
2.43.0
Acked-by on v3 1/2
#2 lore raw

Re: [PATCH bpf v3 1/2] bpf: Fix partial copy of non-linear test_run output

Quoted 29 lines · Sun Jian

For non-linear test_run output, bpf_test_finish() derives the linear data copy length from copy_size - frag_size. This only matches the linear data length when copy_size is the full packet size.

When userspace provides a short data_out buffer, copy_size is clamped to that buffer size. If copy_size is smaller than frag_size, the computed length becomes negative and bpf_test_finish() returns -ENOSPC before copying the packet prefix or updating data_size_out.

Compute the linear data length from the packet layout instead, and clamp the linear copy length to copy_size. This preserves the expected partial-copy semantics: return -ENOSPC, copy the packet prefix that fits in data_out, and report the full packet length through data_size_out.

Fixes 7855e0db150ad ("bpf: test_run: add xdp_shared_info pointer in bpf_test_finish signature")
---
 net/bpf/test_run.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
net/bpf/test_run.c +0 -1
index 2bc04feadfab..f15c613aaa4e 100644
@@ -453,12 +453,8 @@ static int bpf_test_finish(const union bpf_attr *kattr,
 	}
 
 	if (data_out) {
-		int len = sinfo ? copy_size - frag_size : copy_size;

net/bpf/test_run.c static int bpf_test_finish(const union bpf_attr *kattr, · line 460

Quoted 10 lines
net/bpf/test_run.c +2 -5
-
-		if (len < 0) {
-			err = -ENOSPC;
-			goto out;
-		}
+		u32 head_len = size - frag_size;
+		u32 len = min(copy_size, head_len);
 
 		if (copy_to_user(data_out, data, len))
 			goto out;
author
#3 lore raw

PATCH v3 2/2 selftests/bpf: Cover partial copy of non-linear test_run output

In reply to Sun Jian

prog_run_opts already verifies that BPF_PROG_TEST_RUN returns -ENOSPC for a short data_out buffer while still reporting the full output size through data_size_out.

Add the same coverage for non-linear test_run output. Use pass-through TC and XDP programs with a 9000-byte packet, a 64-byte linear data area, and a 100-byte data_out buffer. The expected output spans both the linear data and the first fragment.

Verify that test_run returns -ENOSPC, reports the full packet length through data_size_out, and copies the packet prefix into data_out for both non-linear skb and XDP frags paths.

---
 .../selftests/bpf/prog_tests/prog_run_opts.c  | 70 +++++++++++++++++++
 .../selftests/bpf/progs/test_pkt_access.c     | 12 ++++
 2 files changed, 82 insertions(+)
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +70 -0
index 01f1d1b6715a..9cc898e6a9f7 100644
@@ -4,6 +4,10 @@
 
 #include "test_pkt_access.skel.h"
 
+#define NONLINEAR_PKT_LEN 9000
+#define NONLINEAR_LINEAR_DATA_LEN 64
+#define SHORT_OUT_LEN 100
+
 static const __u32 duration;
 
 static void check_run_cnt(int prog_fd, __u64 run_cnt)
@@ -20,6 +24,69 @@ static void check_run_cnt(int prog_fd, __u64 run_cnt)
 	      "incorrect number of repetitions, want %llu have %llu\n", run_cnt, info.run_cnt);
 }
 
+static void init_pkt(__u8 *pkt, size_t len)
+{
+	size_t i;
+
+	for (i = 0; i < len; i++)
+		pkt[i] = i & 0xff;
+}
+
+static void test_skb_nonlinear_data_out_partial(struct test_pkt_access *skel)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+	__u8 pkt[NONLINEAR_PKT_LEN];
+	__u8 out[SHORT_OUT_LEN];
+	struct __sk_buff skb = {};
+	int prog_fd, err;
+
+	init_pkt(pkt, sizeof(pkt));
+
+	skb.data_end = NONLINEAR_LINEAR_DATA_LEN;
+
+	topts.data_in = pkt;
+	topts.data_size_in = sizeof(pkt);
+	topts.data_out = out;
+	topts.data_size_out = sizeof(out);
+	topts.ctx_in = &skb;
+	topts.ctx_size_in = sizeof(skb);
+
+	prog_fd = bpf_program__fd(skel->progs.tc_pass_prog);
+	err = bpf_prog_test_run_opts(prog_fd, &topts);
+
+	ASSERT_EQ(err, -ENOSPC, "skb_nonlinear_partial_err");
+	ASSERT_EQ(topts.data_size_out, sizeof(pkt), "skb_nonlinear_partial_data_size_out");
+	ASSERT_OK(memcmp(out, pkt, sizeof(out)), "skb_nonlinear_partial_data_out");
+}
+
+static void test_xdp_nonlinear_data_out_partial(struct test_pkt_access *skel)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+	__u8 pkt[NONLINEAR_PKT_LEN];
+	__u8 out[SHORT_OUT_LEN];
+	struct xdp_md ctx = {};
+	int prog_fd, err;
+
+	init_pkt(pkt, sizeof(pkt));
+
+	ctx.data = 0;
+	ctx.data_end = NONLINEAR_LINEAR_DATA_LEN;
+
+	topts.data_in = pkt;
+	topts.data_size_in = sizeof(pkt);
+	topts.data_out = out;
+	topts.data_size_out = sizeof(out);
+	topts.ctx_in = &ctx;
+	topts.ctx_size_in = sizeof(ctx);
+
+	prog_fd = bpf_program__fd(skel->progs.xdp_frags_pass_prog);
+	err = bpf_prog_test_run_opts(prog_fd, &topts);
+
+	ASSERT_EQ(err, -ENOSPC, "xdp_nonlinear_partial_err");
+	ASSERT_EQ(topts.data_size_out, sizeof(pkt), "xdp_nonlinear_partial_data_size_out");
+	ASSERT_OK(memcmp(out, pkt, sizeof(out)), "xdp_nonlinear_partial_data_out");
+}
+
 void test_prog_run_opts(void)
 {
 	struct test_pkt_access *skel;
@@ -69,6 +136,9 @@ void test_prog_run_opts(void)
 	run_cnt += topts.repeat;
 	check_run_cnt(prog_fd, run_cnt);
 
+	test_skb_nonlinear_data_out_partial(skel);
+	test_xdp_nonlinear_data_out_partial(skel);
+
 cleanup:
 	if (skel)
 		test_pkt_access__destroy(skel);
tools/testing/selftests/bpf/progs/test_pkt_access.c +12 -0
index bce7173152c6..cd284401eebd 100644
@@ -150,3 +150,15 @@ int test_pkt_access(struct __sk_buff *skb)
 
 	return TC_ACT_UNSPEC;
 }
+
+SEC("tc")
+int tc_pass_prog(struct __sk_buff *skb)
+{
+	return TC_ACT_OK;
+}
+
+SEC("xdp.frags")
+int xdp_frags_pass_prog(struct xdp_md *ctx)
+{
+	return XDP_PASS;
+}
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH bpf v3 2/2] selftests/bpf: Cover partial copy of non-linear test_run output

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Uninitialized stack buffer `out` is read if the BPF test run fails. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Uninitialized stack buffer out is read if the BPF test run fails. --

commit f204128d45018a80c7b9b142c6d9a253204b22bd
Author: Sun Jian <[email protected]>

selftests/bpf: Cover partial copy of non-linear test_run output

This commit adds selftest coverage for partial copies of non-linear test_run output for TC and XDP programs. It verifies that test_run returns -ENOSPC, reports the full packet length, and copies the packet prefix into the provided buffer.

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +0 -0
index 01f1d1b6715a6..9cc898e6a9f71 100644

[ ... ]

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +3 -0
@@ -20,6 +24,69 @@ static void check_run_cnt(int prog_fd, __u64 run_cnt)
 	      "incorrect number of repetitions, want %llu have %llu\n", run_cnt, info.run_cnt);
 }
 
+static void init_pkt(__u8 *pkt, size_t len)
+{
+	size_t i;

tools/testing/selftests/bpf/prog_tests/prog_run_opts.c static void check_run_cnt(int prog_fd, __u64 run_cnt) · line 39

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +10 -0
+
+	for (i = 0; i < len; i++)
+		pkt[i] = i & 0xff;
+}
+
+static void test_skb_nonlinear_data_out_partial(struct test_pkt_access *skel)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+	__u8 pkt[NONLINEAR_PKT_LEN];
+	__u8 out[SHORT_OUT_LEN];

[Severity: Medium]
Should out[] be explicitly zero-initialized?

If bpf_prog_test_run_opts() fails with an unexpected error and doesn't write to the userspace buffer, the ASSERT_EQ macros below won't abort the test. This means the subsequent ASSERT_OK(memcmp(...)) call will read uninitialized stack memory, which can trigger undefined behavior and Memory Sanitizer (MSAN) warnings.

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +3 -0
+	struct __sk_buff skb = {};
+	int prog_fd, err;
+

[ ... ]

tools/testing/selftests/bpf/prog_tests/prog_run_opts.c static void check_run_cnt(int prog_fd, __u64 run_cnt) · line 66

Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +13 -0
+	prog_fd = bpf_program__fd(skel->progs.tc_pass_prog);
+	err = bpf_prog_test_run_opts(prog_fd, &topts);
+
+	ASSERT_EQ(err, -ENOSPC, "skb_nonlinear_partial_err");
+	ASSERT_EQ(topts.data_size_out, sizeof(pkt), "skb_nonlinear_partial_data_size_out");
+	ASSERT_OK(memcmp(out, pkt, sizeof(out)), "skb_nonlinear_partial_data_out");
+}
+
+static void test_xdp_nonlinear_data_out_partial(struct test_pkt_access *skel)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+	__u8 pkt[NONLINEAR_PKT_LEN];
+	__u8 out[SHORT_OUT_LEN];

[Severity: Medium]
Could we also zero-initialize this buffer for the same reason?

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +3 -0
+	struct xdp_md ctx = {};
+	int prog_fd, err;
+

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw

Re: [PATCH bpf v3 2/2] selftests/bpf: Cover partial copy of non-linear test_run output

In reply to Sun Jian
A question was raised on v2 about whether pkt_v4 could be reused by reducing the linear area to ETH_HLEN, rather than introducing a custom init_pkt() with a 9000-byte stack packet.
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +4 -0
index 01f1d1b6715a..9cc898e6a9f7 100644
@@ -4,6 +4,10 @@
 
 #include "test_pkt_access.skel.h"
 
+#define NONLINEAR_PKT_LEN 9000
+#define NONLINEAR_LINEAR_DATA_LEN 64
+#define SHORT_OUT_LEN 100
+

[ ... ]

tools/testing/selftests/bpf/prog_tests/prog_run_opts.c static void check_run_cnt(int prog_fd, __u64 run_cnt) · line 33

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +7 -0
@@ -20,6 +24,69 @@ static void check_run_cnt(int prog_fd, __u64 run_cnt)
 	      "incorrect number of repetitions, want %llu have %llu\n", run_cnt, info.run_cnt);
 }
 
+static void init_pkt(__u8 *pkt, size_t len)
+{
+	size_t i;
+
+	for (i = 0; i < len; i++)
+		pkt[i] = i & 0xff;
+}

A question was raised on v2 about whether pkt_v4 could be reused by reducing the linear area to ETH_HLEN, rather than introducing a custom init_pkt() with a 9000-byte stack packet.

Can't we reuse pkt_v4 here by reducing the linear area to ETH_HLEN? The v3 still adds init_pkt() and the NONLINEAR_PKT_LEN packet, so this doesn't seem to have been picked up.

Quoted 21 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +21 -0
+
+static void test_skb_nonlinear_data_out_partial(struct test_pkt_access *skel)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+	__u8 pkt[NONLINEAR_PKT_LEN];
+	__u8 out[SHORT_OUT_LEN];
+	struct __sk_buff skb = {};
+	int prog_fd, err;
+
+	init_pkt(pkt, sizeof(pkt));
+
+	skb.data_end = NONLINEAR_LINEAR_DATA_LEN;
+
+	topts.data_in = pkt;
+	topts.data_size_in = sizeof(pkt);
+	topts.data_out = out;
+	topts.data_size_out = sizeof(out);
+	topts.ctx_in = &skb;
+	topts.ctx_size_in = sizeof(skb);
+
+	prog_fd = bpf_program__fd(skel->progs.tc_pass_prog);

[ ... ]

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_pkt_access.c +2 -0
index bce7173152c6..cd284401eebd 100644
@@ -150,3 +150,15 @@ int test_pkt_access(struct __sk_buff *skb)
 
 	return TC_ACT_UNSPEC;
 }
+
+SEC("tc")

tools/testing/selftests/bpf/progs/test_pkt_access.c int test_pkt_access(struct __sk_buff *skb) · line 164

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_pkt_access.c +10 -0
+int tc_pass_prog(struct __sk_buff *skb)
+{
+	return TC_ACT_OK;
+}
+
+SEC("xdp.frags")
+int xdp_frags_pass_prog(struct xdp_md *ctx)
+{
+	return XDP_PASS;
+}

A related suggestion on v2 was that, once pkt_v4 is reused, the existing BPF program could be reused instead of adding new pass-through programs.

Could tc_pass_prog and xdp_frags_pass_prog be dropped in favour of the existing program? The v3 still adds both of these, so this point also seems to be open.

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/27680511802

author
#6 lore raw

Re: [PATCH bpf v3 2/2] selftests/bpf: Cover partial copy of non-linear test_run output

Quoted 89 lines · On Wed, Jun 17, 2026 at 6:31 PM <[email protected]> wrote:
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +4 -0
index 01f1d1b6715a..9cc898e6a9f7 100644
@@ -4,6 +4,10 @@
 
 #include "test_pkt_access.skel.h"
 
+#define NONLINEAR_PKT_LEN 9000
+#define NONLINEAR_LINEAR_DATA_LEN 64
+#define SHORT_OUT_LEN 100
+

[ ... ]

tools/testing/selftests/bpf/prog_tests/prog_run_opts.c static void check_run_cnt(int prog_fd, __u64 run_cnt) · line 33

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +7 -0
@@ -20,6 +24,69 @@ static void check_run_cnt(int prog_fd, __u64 run_cnt)
            "incorrect number of repetitions, want %llu have %llu\n", run_cnt, info.run_cnt);
 }
 
+static void init_pkt(__u8 *pkt, size_t len)
+{
+     size_t i;
+
+     for (i = 0; i < len; i++)
+             pkt[i] = i & 0xff;
+}

A question was raised on v2 about whether pkt_v4 could be reused by reducing the linear area to ETH_HLEN, rather than introducing a custom init_pkt() with a 9000-byte stack packet.

Can't we reuse pkt_v4 here by reducing the linear area to ETH_HLEN? The v3 still adds init_pkt() and the NONLINEAR_PKT_LEN packet, so this doesn't seem to have been picked up.

Quoted 21 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +21 -0
+
+static void test_skb_nonlinear_data_out_partial(struct test_pkt_access *skel)
+{
+     LIBBPF_OPTS(bpf_test_run_opts, topts);
+     __u8 pkt[NONLINEAR_PKT_LEN];
+     __u8 out[SHORT_OUT_LEN];
+     struct __sk_buff skb = {};
+     int prog_fd, err;
+
+     init_pkt(pkt, sizeof(pkt));
+
+     skb.data_end = NONLINEAR_LINEAR_DATA_LEN;
+
+     topts.data_in = pkt;
+     topts.data_size_in = sizeof(pkt);
+     topts.data_out = out;
+     topts.data_size_out = sizeof(out);
+     topts.ctx_in = &skb;
+     topts.ctx_size_in = sizeof(skb);
+
+     prog_fd = bpf_program__fd(skel->progs.tc_pass_prog);

[ ... ]

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_pkt_access.c +2 -0
index bce7173152c6..cd284401eebd 100644
@@ -150,3 +150,15 @@ int test_pkt_access(struct __sk_buff *skb)
 
      return TC_ACT_UNSPEC;
 }
+
+SEC("tc")

tools/testing/selftests/bpf/progs/test_pkt_access.c int test_pkt_access(struct __sk_buff *skb) · line 164

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_pkt_access.c +10 -0
+int tc_pass_prog(struct __sk_buff *skb)
+{
+     return TC_ACT_OK;
+}
+
+SEC("xdp.frags")
+int xdp_frags_pass_prog(struct xdp_md *ctx)
+{
+     return XDP_PASS;
+}

A related suggestion on v2 was that, once pkt_v4 is reused, the existing BPF program could be reused instead of adding new pass-through programs.

Could tc_pass_prog and xdp_frags_pass_prog be dropped in favour of the existing program? The v3 still adds both of these, so this point also seems to be open.

Quoted 5 lines

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/27680511802

Hi,

Thanks for checking this.

I tried reusing pkt_v4 and the existing TC program, but they do not fit the skb case this test is trying to cover.

For skb test_run, IPv4/IPv6 inputs with a too-short L3 header in the linear area are rejected before bpf_test_finish(). With pkt_v4 and a linear area of ETH_HLEN, the test fails with -EINVAL before reaching the partial copy-out path. If the linear area is increased enough to pass the IPv4 check, pkt_v4 is too small to both trigger the old copy_size - frag_size path and verify that the copied prefix spans the linear data and the first fragment. pkt_v6 has the same issue: after making the IPv6 header linear, only 20 bytes remain in frags.

The existing test_pkt_access program has its own packet-access coverage goals and is not just a pass-through carrier. With such a short linear area or small packet fixture, it can fail before the test hits the bpf_test_finish()'s partial copy-out path. A pass-through TC program is therefore a better fit, because it keeps the test focused on the bpf_test_finish() copy-out semantics.

For XDP, this object does not have an existing xdp.frags pass-through program, so the small XDP frags program is needed to cover the other caller of the shared bpf_test_finish() path.

Thanks,
Sun Jian

#7 lore raw

Re: [PATCH bpf v3 2/2] selftests/bpf: Cover partial copy of non-linear test_run output

Quoted 94 lines · sun jian
Quoted 93 lines · On Wed, Jun 17, 2026 at 6:31 PM <[email protected]> wrote:
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +4 -0
index 01f1d1b6715a..9cc898e6a9f7 100644
@@ -4,6 +4,10 @@
 
 #include "test_pkt_access.skel.h"
 
+#define NONLINEAR_PKT_LEN 9000
+#define NONLINEAR_LINEAR_DATA_LEN 64
+#define SHORT_OUT_LEN 100
+

[ ... ]

tools/testing/selftests/bpf/prog_tests/prog_run_opts.c static void check_run_cnt(int prog_fd, __u64 run_cnt) · line 33

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +7 -0
@@ -20,6 +24,69 @@ static void check_run_cnt(int prog_fd, __u64 run_cnt)
            "incorrect number of repetitions, want %llu have %llu\n", run_cnt, info.run_cnt);
 }
 
+static void init_pkt(__u8 *pkt, size_t len)
+{
+     size_t i;
+
+     for (i = 0; i < len; i++)
+             pkt[i] = i & 0xff;
+}

A question was raised on v2 about whether pkt_v4 could be reused by reducing the linear area to ETH_HLEN, rather than introducing a custom init_pkt() with a 9000-byte stack packet.

Can't we reuse pkt_v4 here by reducing the linear area to ETH_HLEN? The v3 still adds init_pkt() and the NONLINEAR_PKT_LEN packet, so this doesn't seem to have been picked up.

Quoted 21 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +21 -0
+
+static void test_skb_nonlinear_data_out_partial(struct test_pkt_access *skel)
+{
+     LIBBPF_OPTS(bpf_test_run_opts, topts);
+     __u8 pkt[NONLINEAR_PKT_LEN];
+     __u8 out[SHORT_OUT_LEN];
+     struct __sk_buff skb = {};
+     int prog_fd, err;
+
+     init_pkt(pkt, sizeof(pkt));
+
+     skb.data_end = NONLINEAR_LINEAR_DATA_LEN;
+
+     topts.data_in = pkt;
+     topts.data_size_in = sizeof(pkt);
+     topts.data_out = out;
+     topts.data_size_out = sizeof(out);
+     topts.ctx_in = &skb;
+     topts.ctx_size_in = sizeof(skb);
+
+     prog_fd = bpf_program__fd(skel->progs.tc_pass_prog);

[ ... ]

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_pkt_access.c +2 -0
index bce7173152c6..cd284401eebd 100644
@@ -150,3 +150,15 @@ int test_pkt_access(struct __sk_buff *skb)
 
      return TC_ACT_UNSPEC;
 }
+
+SEC("tc")

tools/testing/selftests/bpf/progs/test_pkt_access.c int test_pkt_access(struct __sk_buff *skb) · line 164

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_pkt_access.c +10 -0
+int tc_pass_prog(struct __sk_buff *skb)
+{
+     return TC_ACT_OK;
+}
+
+SEC("xdp.frags")
+int xdp_frags_pass_prog(struct xdp_md *ctx)
+{
+     return XDP_PASS;
+}

A related suggestion on v2 was that, once pkt_v4 is reused, the existing BPF program could be reused instead of adding new pass-through programs.

Could tc_pass_prog and xdp_frags_pass_prog be dropped in favour of the existing program? The v3 still adds both of these, so this point also seems to be open.

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

Quoted 5 lines
Quoted 1 line

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/27680511802

Hi,

Thanks for checking this.

Hi Sun Jian,

It would help if you could reply inline instead of at the end of the messages, especially when there are multiple comments. See [1] for an explanation of how that works.

1: https://kernelnewbies.org/FirstKernelPatch#Responding_inline

Quoted 13 lines

I tried reusing pkt_v4 and the existing TC program, but they do not fit the skb case this test is trying to cover.

For skb test_run, IPv4/IPv6 inputs with a too-short L3 header in the linear area are rejected before bpf_test_finish(). With pkt_v4 and a linear area of ETH_HLEN, the test fails with -EINVAL before reaching the partial copy-out path. If the linear area is increased enough to pass the IPv4 check, pkt_v4 is too small to both trigger the old copy_size - frag_size path and verify that the copied prefix spans the linear data and the first fragment. pkt_v6 has the same issue: after making the IPv6 header linear, only 20 bytes remain in frags.

Quoted 6 lines

The existing test_pkt_access program has its own packet-access coverage goals and is not just a pass-through carrier. With such a short linear area or small packet fixture, it can fail before the test hits the bpf_test_finish()'s partial copy-out path. A pass-through TC program is therefore a better fit, because it keeps the test focused on the bpf_test_finish() copy-out semantics.

If we're keeping tc_pass_prog() then can't we use pkt_v4 and get rid of init_pkt?

Quoted 7 lines

For XDP, this object does not have an existing xdp.frags pass-through program, so the small XDP frags program is needed to cover the other caller of the shared bpf_test_finish() path.

Thanks,
Sun Jian

author
#8 lore raw

Re: [PATCH bpf v3 2/2] selftests/bpf: Cover partial copy of non-linear test_run output

Quoted 102 lines · Paul Chaignon
Quoted 94 lines · sun jian
Quoted 93 lines · On Wed, Jun 17, 2026 at 6:31 PM <[email protected]> wrote:
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +4 -0
index 01f1d1b6715a..9cc898e6a9f7 100644
@@ -4,6 +4,10 @@
 
 #include "test_pkt_access.skel.h"
 
+#define NONLINEAR_PKT_LEN 9000
+#define NONLINEAR_LINEAR_DATA_LEN 64
+#define SHORT_OUT_LEN 100
+

[ ... ]

tools/testing/selftests/bpf/prog_tests/prog_run_opts.c static void check_run_cnt(int prog_fd, __u64 run_cnt) · line 33

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +7 -0
@@ -20,6 +24,69 @@ static void check_run_cnt(int prog_fd, __u64 run_cnt)
            "incorrect number of repetitions, want %llu have %llu\n", run_cnt, info.run_cnt);
 }
 
+static void init_pkt(__u8 *pkt, size_t len)
+{
+     size_t i;
+
+     for (i = 0; i < len; i++)
+             pkt[i] = i & 0xff;
+}

A question was raised on v2 about whether pkt_v4 could be reused by reducing the linear area to ETH_HLEN, rather than introducing a custom init_pkt() with a 9000-byte stack packet.

Can't we reuse pkt_v4 here by reducing the linear area to ETH_HLEN? The v3 still adds init_pkt() and the NONLINEAR_PKT_LEN packet, so this doesn't seem to have been picked up.

Quoted 21 lines
tools/testing/selftests/bpf/prog_tests/prog_run_opts.c +21 -0
+
+static void test_skb_nonlinear_data_out_partial(struct test_pkt_access *skel)
+{
+     LIBBPF_OPTS(bpf_test_run_opts, topts);
+     __u8 pkt[NONLINEAR_PKT_LEN];
+     __u8 out[SHORT_OUT_LEN];
+     struct __sk_buff skb = {};
+     int prog_fd, err;
+
+     init_pkt(pkt, sizeof(pkt));
+
+     skb.data_end = NONLINEAR_LINEAR_DATA_LEN;
+
+     topts.data_in = pkt;
+     topts.data_size_in = sizeof(pkt);
+     topts.data_out = out;
+     topts.data_size_out = sizeof(out);
+     topts.ctx_in = &skb;
+     topts.ctx_size_in = sizeof(skb);
+
+     prog_fd = bpf_program__fd(skel->progs.tc_pass_prog);

[ ... ]

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_pkt_access.c +2 -0
index bce7173152c6..cd284401eebd 100644
@@ -150,3 +150,15 @@ int test_pkt_access(struct __sk_buff *skb)
 
      return TC_ACT_UNSPEC;
 }
+
+SEC("tc")

tools/testing/selftests/bpf/progs/test_pkt_access.c int test_pkt_access(struct __sk_buff *skb) · line 164

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_pkt_access.c +10 -0
+int tc_pass_prog(struct __sk_buff *skb)
+{
+     return TC_ACT_OK;
+}
+
+SEC("xdp.frags")
+int xdp_frags_pass_prog(struct xdp_md *ctx)
+{
+     return XDP_PASS;
+}

A related suggestion on v2 was that, once pkt_v4 is reused, the existing BPF program could be reused instead of adding new pass-through programs.

Could tc_pass_prog and xdp_frags_pass_prog be dropped in favour of the existing program? The v3 still adds both of these, so this point also seems to be open.

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

Quoted 5 lines
Quoted 1 line

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/27680511802

Hi,

Thanks for checking this.

Quoted 7 lines

Hi Sun Jian,

It would help if you could reply inline instead of at the end of the messages, especially when there are multiple comments. See [1] for an explanation of how that works.

1: https://kernelnewbies.org/FirstKernelPatch#Responding_inline

Acknowledged, I will reply inline.

Quoted 21 lines
Quoted 14 lines

I tried reusing pkt_v4 and the existing TC program, but they do not fit the skb case this test is trying to cover.

For skb test_run, IPv4/IPv6 inputs with a too-short L3 header in the linear area are rejected before bpf_test_finish(). With pkt_v4 and a linear area of ETH_HLEN, the test fails with -EINVAL before reaching the partial copy-out path. If the linear area is increased enough to pass the IPv4 check, pkt_v4 is too small to both trigger the old copy_size - frag_size path and verify that the copied prefix spans the linear data and the first fragment. pkt_v6 has the same issue: after making the IPv6 header linear, only 20 bytes remain in frags.

Quoted 6 lines

The existing test_pkt_access program has its own packet-access coverage goals and is not just a pass-through carrier. With such a short linear area or small packet fixture, it can fail before the test hits the bpf_test_finish()'s partial copy-out path. A pass-through TC program is therefore a better fit, because it keeps the test focused on the bpf_test_finish() copy-out semantics.

Quoted 3 lines

If we're keeping tc_pass_prog() then can't we use pkt_v4 and get rid of init_pkt?

pkt_v4 is too small to construct a meaningful nonlinear skb with a stable linear/frag split while still exercising the partial copy-out boundary in bpf_test_finish().

With pkt_v4, we either do not reach a fragmented layout, or lose control over the linear/frag boundary needed to exercise the regression path.

This test uses a 9000B packet so it does not depend on small-packet allocation details. Smaller packets might work depending on allocation state, but 9000B reliably gives us a non-linear skb with page frags and a stable linear/frag boundary for the copy-out regression.

init_pkt() is needed to ensure deterministic byte content across both linear and fragmented regions so that the memcmp-based validation is stable.

Thanks,
Sun Jian

Quoted 7 lines
Quoted 7 lines

For XDP, this object does not have an existing xdp.frags pass-through program, so the small XDP frags program is needed to cover the other caller of the shared bpf_test_finish() path.

Thanks,
Sun Jian

#9 lore raw

Re: [PATCH bpf v3 2/2] selftests/bpf: Cover partial copy of non-linear test_run output

Quoted 4 lines · sun jian
Quoted 3 lines · Paul Chaignon
Quoted 1 line · sun jian

On Wed, Jun 17, 2026 at 6:31 PM <[email protected]> wrote:

[...]

Quoted 23 lines
Quoted 19 lines
Quoted 12 lines

I tried reusing pkt_v4 and the existing TC program, but they do not fit the skb case this test is trying to cover.

For skb test_run, IPv4/IPv6 inputs with a too-short L3 header in the linear area are rejected before bpf_test_finish(). With pkt_v4 and a linear area of ETH_HLEN, the test fails with -EINVAL before reaching the partial copy-out path. If the linear area is increased enough to pass the IPv4 check, pkt_v4 is too small to both trigger the old copy_size - frag_size path and verify that the copied prefix spans the linear data and the first fragment. pkt_v6 has the same issue: after making the IPv6 header linear, only 20 bytes remain in frags.

Quoted 6 lines

The existing test_pkt_access program has its own packet-access coverage goals and is not just a pass-through carrier. With such a short linear area or small packet fixture, it can fail before the test hits the bpf_test_finish()'s partial copy-out path. A pass-through TC program is therefore a better fit, because it keeps the test focused on the bpf_test_finish() copy-out semantics.

Quoted 3 lines

If we're keeping tc_pass_prog() then can't we use pkt_v4 and get rid of init_pkt?

Quoted 6 lines

pkt_v4 is too small to construct a meaningful nonlinear skb with a stable linear/frag split while still exercising the partial copy-out boundary in bpf_test_finish().

With pkt_v4, we either do not reach a fragmented layout, or lose control over the linear/frag boundary needed to exercise the regression path.

I think I'm missing something. Why can't we use pkt_v4 with tc_pass_prog() and a linear area of ETH_HLEN? That would leave 42 bytes of non-linear area, so a SHORT_OUT_LEN of 30 should work to trigger the bug, no?

Quoted 20 lines

This test uses a 9000B packet so it does not depend on small-packet allocation details. Smaller packets might work depending on allocation state, but 9000B reliably gives us a non-linear skb with page frags and a stable linear/frag boundary for the copy-out regression.

init_pkt() is needed to ensure deterministic byte content across both linear and fragmented regions so that the memcmp-based validation is stable.

Thanks,
Sun Jian

Quoted 7 lines
Quoted 7 lines

For XDP, this object does not have an existing xdp.frags pass-through program, so the small XDP frags program is needed to cover the other caller of the shared bpf_test_finish() path.

Thanks,
Sun Jian

author
#10 lore raw

Re: [PATCH bpf v3 2/2] selftests/bpf: Cover partial copy of non-linear test_run output

Quoted 36 lines · Paul Chaignon
Quoted 4 lines · sun jian
Quoted 3 lines · Paul Chaignon
Quoted 1 line · sun jian

On Wed, Jun 17, 2026 at 6:31 PM <[email protected]> wrote:

[...]

Quoted 27 lines
Quoted 19 lines
Quoted 12 lines

I tried reusing pkt_v4 and the existing TC program, but they do not fit the skb case this test is trying to cover.

For skb test_run, IPv4/IPv6 inputs with a too-short L3 header in the linear area are rejected before bpf_test_finish(). With pkt_v4 and a linear area of ETH_HLEN, the test fails with -EINVAL before reaching the partial copy-out path. If the linear area is increased enough to pass the IPv4 check, pkt_v4 is too small to both trigger the old copy_size - frag_size path and verify that the copied prefix spans the linear data and the first fragment. pkt_v6 has the same issue: after making the IPv6 header linear, only 20 bytes remain in frags.

Quoted 6 lines

The existing test_pkt_access program has its own packet-access coverage goals and is not just a pass-through carrier. With such a short linear area or small packet fixture, it can fail before the test hits the bpf_test_finish()'s partial copy-out path. A pass-through TC program is therefore a better fit, because it keeps the test focused on the bpf_test_finish() copy-out semantics.

Quoted 3 lines

If we're keeping tc_pass_prog() then can't we use pkt_v4 and get rid of init_pkt?

pkt_v4 is too small to construct a meaningful nonlinear skb with a stable linear/frag split while still exercising the partial copy-out boundary in bpf_test_finish().

Quoted 8 lines
Quoted 2 lines

With pkt_v4, we either do not reach a fragmented layout, or lose control over the linear/frag boundary needed to exercise the regression path.

I think I'm missing something. Why can't we use pkt_v4 with tc_pass_prog() and a linear area of ETH_HLEN? That would leave 42 bytes of non-linear area, so a SHORT_OUT_LEN of 30 should work to trigger the bug, no?

I should have made that part clearer. With a linear area of ETH_HLEN, the skb test_run path fails before reaching bpf_test_finish(). After eth_type_trans(), skb->data is advanced past the Ethernet header, so the remaining linear L3 area is 0. The short IPv4 input check then rejects the packet with -EINVAL.

To pass that check with pkt_v4, the linear area has to include the IPv4 header as well, i.e. ETH_HLEN + sizeof(struct iphdr). That leaves only about 20 bytes in frags, so a SHORT_OUT_LEN of 30 no longer triggers the old copy_size - frag_size < 0 path.

Quoted 20 lines
Quoted 20 lines

This test uses a 9000B packet so it does not depend on small-packet allocation details. Smaller packets might work depending on allocation state, but 9000B reliably gives us a non-linear skb with page frags and a stable linear/frag boundary for the copy-out regression.

init_pkt() is needed to ensure deterministic byte content across both linear and fragmented regions so that the memcmp-based validation is stable.

Thanks,
Sun Jian

Quoted 7 lines
Quoted 7 lines

For XDP, this object does not have an existing xdp.frags pass-through program, so the small XDP frags program is needed to cover the other caller of the shared bpf_test_finish() path.

Thanks,
Sun Jian