BPF List
 help / color / mirror / Atom feed
From: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org,
	daniel@iogearbox.net, kafai@meta.com, kernel-team@meta.com,
	eddyz87@gmail.com, memxor@gmail.com,
	Mykyta Yatsenko <yatsenko@meta.com>
Subject: Re: [RFC PATCH v1 10/10] selftests/bpf: add file dynptr tests
Date: Mon, 6 Oct 2025 12:50:19 +0100	[thread overview]
Message-ID: <62376422-6d60-42b9-b09e-393396fb7302@gmail.com> (raw)
In-Reply-To: <CAEf4Bzbw+udD6Fud2WshVrCK=mGqisjagZrapsQwM=0G9ipesg@mail.gmail.com>

On 10/3/25 21:02, Andrii Nakryiko wrote:
> On Fri, Oct 3, 2025 at 9:04 AM Mykyta Yatsenko
> <mykyta.yatsenko5@gmail.com> wrote:
>> From: Mykyta Yatsenko <yatsenko@meta.com>
>>
>> Introducing selftests for validating file-backed dynptr works as
>> expected.
>>   * validate implementation supports dynptr slice and read operations
>>   * validate destructors should be paired with initializers
>>
>> Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
>> ---
>>   .../selftests/bpf/prog_tests/file_reader.c    |  81 ++++++
>>   .../testing/selftests/bpf/progs/file_reader.c | 241 ++++++++++++++++++
>>   .../selftests/bpf/progs/file_reader_fail.c    |  57 +++++
>>   3 files changed, 379 insertions(+)
>>   create mode 100644 tools/testing/selftests/bpf/prog_tests/file_reader.c
>>   create mode 100644 tools/testing/selftests/bpf/progs/file_reader.c
>>   create mode 100644 tools/testing/selftests/bpf/progs/file_reader_fail.c
> Non-sleepable file dynptr can fail to read, so this test is a bit
> fragile. Let's have a sleepable test (fentry.s or something like
> that?)
This is sleepable, because it runs in task work callback.
>
> Plus, can you please add a test that validates that we do page in file
> contents even if it was not physically in memory? see madvise(addr,
> page_sz, MADV_PAGEOUT) in selftests
I tried to tell kernel to evict those pages via: posix_fadvise(fd, 0, 0, 
POSIX_FADV_DONTNEED);
I'll rework to madvise(addr, page_sz, MADV_PAGEOUT)
>
>> +int err;
>> +void *user_ptr;
>> +char buf[1024];
>> +char *user_buf;
>> +volatile const __u32 user_buf_sz;
>> +volatile const __s32 test_type = -1;
>> +
>> +static int process_vma(struct task_struct *task, struct vm_area_struct *vma, void *data);
>> +static int search_elf(struct file *file);
>> +static int validate_large_file_read(struct file *file);
>> +static int task_work_callback(struct bpf_map *map, void *key, void *value);
>> +
>> +SEC("raw_tp/sys_enter")
>> +int on_getpid(void *ctx)
>> +{
>> +       struct task_struct *task = bpf_get_current_task_btf();
>> +       struct elem *work;
>> +       int key = 0;
> this will be called for every syscall in the system, regardless of the
> process, so you probably need to filter this by process ID?
>
>> +
>> +       work = bpf_map_lookup_elem(&arrmap, &key);
>> +       if (!work) {
>> +               err = 1;
>> +               return 0;
>> +       }
>> +       bpf_task_work_schedule_signal(task, &work->tw, &arrmap, task_work_callback, NULL);
>> +       return 0;
>> +}
>> +
> [...]
>
>> +static long process_vma_unreleased_ref(struct task_struct *task, struct vm_area_struct *vma,
>> +                                      void *data)
>> +{
>> +       struct bpf_dynptr dynptr;
>> +
>> +       if (!vma->vm_file)
>> +               return 1;
>> +
>> +       err = bpf_dynptr_from_file(vma->vm_file, 0, &dynptr);
>> +       return err ? 1 : 0;
>> +}
>> +
>> +SEC("fentry.s/" SYS_PREFIX "sys_nanosleep")
>> +__failure __msg("Unreleased reference id=") int on_nanosleep_unreleased_ref(void *ctx)
> nit: keep annotations on separate line from the function itself
>
>> +{
>> +       struct task_struct *task = bpf_get_current_task_btf();
>> +
>> +       bpf_find_vma(task, (unsigned long)user_ptr, process_vma_unreleased_ref, NULL, 0);
>> +       return 0;
>> +}
>> +
>> +SEC("xdp")
>> +__failure __msg("Expected a dynptr of type file as arg #0")
>> +int xdp_wrong_dynptr_type(struct xdp_md *xdp)
>> +{
>> +       struct bpf_dynptr dynptr;
>> +
>> +       bpf_dynptr_from_xdp(xdp, 0, &dynptr);
>> +       bpf_dynptr_file_discard(&dynptr);
>> +       return 0;
>> +}
>> +
>> +SEC("xdp")
>> +__failure __msg("Expected an initialized dynptr as arg #0")
>> +int xdp_no_dynptr_type(struct xdp_md *xdp)
>> +{
>> +       struct bpf_dynptr dynptr;
>> +
>> +       bpf_dynptr_file_discard(&dynptr);
>> +       return 0;
>> +}
>> --
>> 2.51.0
>>


  reply	other threads:[~2025-10-06 11:50 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-03 16:04 [RFC PATCH v1 00/10] bpf: Introduce file dynptr Mykyta Yatsenko
2025-10-03 16:04 ` [RFC PATCH v1 01/10] selftests/bpf: remove unnecessary kfunc prototypes Mykyta Yatsenko
2025-10-03 17:46   ` Eduard Zingerman
2025-10-03 16:04 ` [RFC PATCH v1 02/10] bpf: widen dynptr size/offset to 64 bit Mykyta Yatsenko
2025-10-03 18:16   ` Andrii Nakryiko
2025-10-03 18:40   ` Eduard Zingerman
2025-10-03 18:53     ` Mykyta Yatsenko
2025-10-03 16:04 ` [RFC PATCH v1 03/10] lib: extract freader into a separate files Mykyta Yatsenko
2025-10-03 18:16   ` Andrii Nakryiko
2025-10-03 20:04   ` Alexei Starovoitov
2025-10-03 16:04 ` [RFC PATCH v1 04/10] lib/freader: support reading more than 2 folios Mykyta Yatsenko
2025-10-03 18:16   ` Andrii Nakryiko
2025-10-03 18:29     ` Mykyta Yatsenko
2025-10-03 18:46       ` Andrii Nakryiko
2025-10-03 16:04 ` [RFC PATCH v1 05/10] bpf: verifier: centralize const dynptr check in unmark_stack_slots_dynptr() Mykyta Yatsenko
2025-10-03 18:18   ` Andrii Nakryiko
2025-10-03 19:02   ` Eduard Zingerman
2025-10-03 16:04 ` [RFC PATCH v1 06/10] bpf: add plumbing for file-backed dynptr Mykyta Yatsenko
2025-10-03 18:38   ` Andrii Nakryiko
2025-10-03 20:55   ` Eduard Zingerman
2025-10-03 16:04 ` [RFC PATCH v1 07/10] bpf: add kfuncs and helpers support for file dynptrs Mykyta Yatsenko
2025-10-03 18:38   ` Andrii Nakryiko
2025-10-03 18:59     ` Mykyta Yatsenko
2025-10-03 21:35   ` Eduard Zingerman
2025-10-08  0:25     ` Mykyta Yatsenko
2025-10-03 16:04 ` [RFC PATCH v1 08/10] bpf: verifier: refactor kfunc specialization Mykyta Yatsenko
2025-10-03 22:08   ` Eduard Zingerman
2025-10-08  0:35     ` Mykyta Yatsenko
2025-10-08 18:27     ` Mykyta Yatsenko
2025-10-08 19:15       ` Eduard Zingerman
2025-10-03 16:04 ` [RFC PATCH v1 09/10] bpf: dispatch to sleepable file dynptr Mykyta Yatsenko
2025-10-03 18:45   ` Andrii Nakryiko
2025-10-03 20:10   ` Alexei Starovoitov
2025-10-03 22:17   ` Eduard Zingerman
2025-10-03 16:04 ` [RFC PATCH v1 10/10] selftests/bpf: add file dynptr tests Mykyta Yatsenko
2025-10-03 20:02   ` Andrii Nakryiko
2025-10-06 11:50     ` Mykyta Yatsenko [this message]
2025-10-06 16:15       ` Andrii Nakryiko
2025-10-03 22:24   ` Eduard Zingerman
2025-10-06 11:54     ` Mykyta Yatsenko
2025-10-08  0:39     ` Mykyta Yatsenko

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=62376422-6d60-42b9-b09e-393396fb7302@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=memxor@gmail.com \
    --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