From: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>, martin.lau@linux.dev
Cc: bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org,
daniel@iogearbox.net, kafai@meta.com, kernel-team@meta.com,
eddyz87@gmail.com, Mykyta Yatsenko <yatsenko@meta.com>
Subject: Re: [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy
Date: Fri, 21 Feb 2025 12:08:39 +0000 [thread overview]
Message-ID: <9eaf9945-9a5c-4313-b449-cfa8144975e0@gmail.com> (raw)
In-Reply-To: <CAEf4BzaxYL1y4wR0KuSouDzmrt610CBwtv0dKp4xbO9LD-t9qg@mail.gmail.com>
On 21/02/2025 00:52, Andrii Nakryiko wrote:
> On Tue, Feb 18, 2025 at 11:01 AM Mykyta Yatsenko
> <mykyta.yatsenko5@gmail.com> wrote:
>> From: Mykyta Yatsenko <yatsenko@meta.com>
>>
>> Add XDP setup type for dynptr tests, enabling testing for
>> non-contiguous buffer.
>> Add 2 tests:
>> - test_dynptr_copy - verify correctness for the fast (contiguous
>> buffer) code path.
>> - test_dynptr_copy_xdp - verifies code paths that handle
>> non-contiguous buffer.
>>
>> Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
>> ---
>> tools/testing/selftests/bpf/bpf_kfuncs.h | 8 ++
>> .../testing/selftests/bpf/prog_tests/dynptr.c | 25 ++++++
>> .../selftests/bpf/progs/dynptr_success.c | 77 +++++++++++++++++++
>> 3 files changed, 110 insertions(+)
>>
>> diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
>> index 8215c9b3115e..e9c193036c82 100644
>> --- a/tools/testing/selftests/bpf/bpf_kfuncs.h
>> +++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
>> @@ -43,6 +43,14 @@ extern bool bpf_dynptr_is_rdonly(const struct bpf_dynptr *ptr) __ksym __weak;
>> extern __u32 bpf_dynptr_size(const struct bpf_dynptr *ptr) __ksym __weak;
>> extern int bpf_dynptr_clone(const struct bpf_dynptr *ptr, struct bpf_dynptr *clone__init) __ksym __weak;
>>
>> +/* Description
>> + * Copy data from one dynptr to another
>> + * Returns
>> + * Error code
>> + */
>> +extern int bpf_dynptr_copy(struct bpf_dynptr *dst, __u32 dst_off,
>> + struct bpf_dynptr *src, __u32 src_off, __u32 size) __ksym __weak;
>> +
> Do we *need* this? Doesn't all this come from vmlinux.h nowadays?
>
>> /* Description
>> * Modify the address of a AF_UNIX sockaddr.
>> * Returns
>> diff --git a/tools/testing/selftests/bpf/prog_tests/dynptr.c b/tools/testing/selftests/bpf/prog_tests/dynptr.c
>> index b614a5272dfd..247618958155 100644
>> --- a/tools/testing/selftests/bpf/prog_tests/dynptr.c
>> +++ b/tools/testing/selftests/bpf/prog_tests/dynptr.c
>> @@ -10,6 +10,7 @@ enum test_setup_type {
>> SETUP_SYSCALL_SLEEP,
>> SETUP_SKB_PROG,
>> SETUP_SKB_PROG_TP,
>> + SETUP_XDP_PROG,
>> };
>>
>> static struct {
>> @@ -18,6 +19,8 @@ static struct {
>> } success_tests[] = {
>> {"test_read_write", SETUP_SYSCALL_SLEEP},
>> {"test_dynptr_data", SETUP_SYSCALL_SLEEP},
>> + {"test_dynptr_copy", SETUP_SYSCALL_SLEEP},
>> + {"test_dynptr_copy_xdp", SETUP_XDP_PROG},
>> {"test_ringbuf", SETUP_SYSCALL_SLEEP},
>> {"test_skb_readonly", SETUP_SKB_PROG},
>> {"test_dynptr_skb_data", SETUP_SKB_PROG},
>> @@ -120,6 +123,28 @@ static void verify_success(const char *prog_name, enum test_setup_type setup_typ
>>
>> break;
>> }
>> + case SETUP_XDP_PROG:
>> + {
>> + char data[5000];
>> + int err, prog_fd;
>> +
> no empty line here, opts is a variable
>
>> + LIBBPF_OPTS(bpf_test_run_opts, opts,
>> + .data_in = &data,
>> + .data_size_in = sizeof(data),
>> + .repeat = 1,
>> + );
>> +
>> + prog_fd = bpf_program__fd(prog);
>> + if (!ASSERT_GE(prog_fd, 0, "prog_fd"))
>> + goto cleanup;
>
> we shouldn't check this, if program loaded successfully this will
> always be true (and yeah, I know that existing code does that, we
> should remove or at least not duplicate this)
>
>> +
>> + err = bpf_prog_test_run_opts(prog_fd, &opts);
>> +
>> + if (!ASSERT_OK(err, "test_run"))
>> + goto cleanup;
>> +
>> + break;
>> + }
>> }
>>
>> ASSERT_EQ(skel->bss->err, 0, "err");
>> diff --git a/tools/testing/selftests/bpf/progs/dynptr_success.c b/tools/testing/selftests/bpf/progs/dynptr_success.c
>> index bfcc85686cf0..8a6b35418e39 100644
>> --- a/tools/testing/selftests/bpf/progs/dynptr_success.c
>> +++ b/tools/testing/selftests/bpf/progs/dynptr_success.c
>> @@ -567,3 +567,80 @@ int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location)
>>
>> return 1;
>> }
>> +
>> +SEC("?tp/syscalls/sys_enter_nanosleep")
>> +int test_dynptr_copy(void *ctx)
>> +{
>> + char *data = "hello there, world!!";
>> + char buf[32] = {'\0'};
>> + __u32 sz = strlen(data);
> this is fragile, this is not guaranteed to work (only if compiler just
> substituted a constant value). maybe just use data[] = "hello
> there..." and use sizeof(data) then?
>
>> + struct bpf_dynptr src, dst;
>> +
>> + bpf_ringbuf_reserve_dynptr(&ringbuf, sz, 0, &src);
>> + bpf_ringbuf_reserve_dynptr(&ringbuf, sz, 0, &dst);
>> +
>> + err = bpf_dynptr_write(&src, 0, data, sz, 0);
>> + err = err ?: bpf_dynptr_copy(&dst, 0, &src, 0, sz);
>> + err = err ?: bpf_dynptr_read(buf, sz, &dst, 0, 0);
>> + err = err ?: __builtin_memcmp(data, buf, sz);
>> +
>> + err = err ?: bpf_dynptr_copy(&dst, 3, &src, 5, sz - 5);
>> + err = err ?: bpf_dynptr_read(buf, sz - 5, &dst, 3, 0);
>> + err = err ?: __builtin_memcmp(data + 5, buf, sz - 5);
>> +
>> + bpf_ringbuf_discard_dynptr(&src, 0);
>> + bpf_ringbuf_discard_dynptr(&dst, 0);
>> + return 0;
>> +}
>> +
>> +SEC("xdp")
>> +int test_dynptr_copy_xdp(struct xdp_md *xdp)
>> +{
>> + struct bpf_dynptr ptr_buf, ptr_xdp;
>> + char *data = "qwertyuiopasdfghjkl;";
>> + char buf[32] = {'\0'};
>> + __u32 len = strlen(data);
> ditto
>
>> + int i, chunks = 200;
>> +
>> + bpf_dynptr_from_xdp(xdp, 0, &ptr_xdp);
>> + bpf_ringbuf_reserve_dynptr(&ringbuf, len * chunks, 0, &ptr_buf);
>> +
>> + bpf_for(i, 0, chunks) {
>> + err = err ?: bpf_dynptr_write(&ptr_buf, i * len, data, len, 0);
>> + }
>> +
>> + err = err ?: bpf_dynptr_copy(&ptr_xdp, 0, &ptr_buf, 0, len * chunks);
>> +
>> + bpf_for(i, 0, chunks) {
>> + memset(buf, 0, sizeof(buf));
> __builtin_memset(), memset() works only because compiler optimizes it
> to built-in, but let's not rely on that
>
>> + err = err ?: bpf_dynptr_read(&buf, len, &ptr_xdp, i * len, 0);
>> + err = err ?: memcmp(data, buf, len);
> __builtin_memcmp() and all the other cases below, please
>
>> + }
>> +
>> + memset(buf, 0, sizeof(buf));
>> + bpf_for(i, 0, chunks) {
>> + err = err ?: bpf_dynptr_write(&ptr_buf, i * len, buf, len, 0);
>> + }
>> +
>> + err = err ?: bpf_dynptr_copy(&ptr_buf, 0, &ptr_xdp, 0, len * chunks);
>> +
>> + bpf_for(i, 0, chunks) {
>> + memset(buf, 0, sizeof(buf));
>> + err = err ?: bpf_dynptr_read(&buf, len, &ptr_buf, i * len, 0);
>> + err = err ?: memcmp(data, buf, len);
>> + }
>> +
>> + bpf_ringbuf_discard_dynptr(&ptr_buf, 0);
>> +
>> + err = err ?: bpf_dynptr_copy(&ptr_xdp, 2, &ptr_xdp, len, len * (chunks - 1));
>> +
>> + bpf_for(i, 0, chunks - 1) {
>> + memset(buf, 0, sizeof(buf));
>> + err = err ?: bpf_dynptr_read(&buf, len, &ptr_xdp, 2 + i * len, 0);
>> + err = err ?: memcmp(data, buf, len);
>> + }
>> +
>> + err = err ?: (bpf_dynptr_copy(&ptr_xdp, 2000, &ptr_xdp, 0, len * chunks) == -E2BIG ? 0 : 1);
> overdoing it a bit with the whole `err ?: ` pattern, IMO
>
>
> BTW, more questions to networking folks (maybe Martin knows). Is there
> a way to setup SKB or XDP packet with a non-linear region for testing?
The setup I made in dynptr.c for XDP makes non-linear region.
It looks like maximum linear mem size is 4k, so when going above that,
it creates multiple frags in xdp_buff:
```
char data[5000];
...
.data_in = &data,
```
I verified that in this test xdp_buff is non-linear, and all non-fast
code paths of
the bpf_dynptr_copy are tested.
>
>> +
>> + return XDP_DROP;
>> +}
>> --
>> 2.48.1
>>
Thanks for findings, fixing them in v2.
next prev parent reply other threads:[~2025-02-21 12:08 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-18 19:00 [PATCH bpf-next 0/3] introduce bpf_dynptr_copy kfunc Mykyta Yatsenko
2025-02-18 19:00 ` [PATCH bpf-next 1/3] bpf/helpers: refactor bpf_dynptr_read and bpf_dynptr_write Mykyta Yatsenko
2025-02-18 19:00 ` [PATCH bpf-next 2/3] bpf/helpers: introduce bpf_dynptr_copy kfunc Mykyta Yatsenko
2025-02-19 1:01 ` Eduard Zingerman
2025-02-21 0:42 ` Andrii Nakryiko
2025-02-18 19:00 ` [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy Mykyta Yatsenko
2025-02-19 1:04 ` Eduard Zingerman
2025-02-21 0:52 ` Andrii Nakryiko
2025-02-21 0:56 ` Eduard Zingerman
2025-02-21 1:03 ` Andrii Nakryiko
2025-02-21 12:08 ` Mykyta Yatsenko [this message]
2025-02-21 17:16 ` Andrii Nakryiko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=9eaf9945-9a5c-4313-b449-cfa8144975e0@gmail.com \
--to=mykyta.yatsenko5@gmail.com \
--cc=andrii.nakryiko@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=kafai@meta.com \
--cc=kernel-team@meta.com \
--cc=martin.lau@linux.dev \
--cc=yatsenko@meta.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox