BPF List
 help / color / mirror / Atom feed
From: Leon Hwang <leon.hwang@linux.dev>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org,
	daniel@iogearbox.net, menglong8.dong@gmail.com
Subject: Re: [RFC PATCH bpf-next v2 2/6] libbpf: Add support for extended bpf syscall
Date: Tue, 23 Sep 2025 23:36:29 +0800	[thread overview]
Message-ID: <d535ef7e-a7fb-41be-8550-bb0c0af045f9@linux.dev> (raw)
In-Reply-To: <CAEf4BzZp8vb3EYwvSCbewdZi0eKZjW5sJkDnm6YfPqaRbjf2NA@mail.gmail.com>



On 2025/9/17 08:06, Andrii Nakryiko wrote:
> On Thu, Sep 11, 2025 at 9:33 AM Leon Hwang <leon.hwang@linux.dev> wrote:
>>
>> To support the extended 'bpf()' syscall introduced in the previous commit,
>> this patch adds the following APIs:
>>
>> 1. *Internal:*
>>
>>    * 'sys_bpf_extended()'
>>    * 'sys_bpf_fd_extended()'
>>      These wrap the raw 'syscall()' interface to support passing extended
>>      attributes.
>>
>> 2. *Exported:*
>>
>>    * 'probe_sys_bpf_extended()'
>>      This function checks whether the running kernel supports the extended
>>      'bpf()' syscall with common attributes.
>>
>> Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
>> ---
>>  tools/lib/bpf/bpf.c             | 45 +++++++++++++++++++++++++++++++++
>>  tools/lib/bpf/bpf.h             |  1 +
>>  tools/lib/bpf/features.c        |  8 ++++++
>>  tools/lib/bpf/libbpf.map        |  2 ++
>>  tools/lib/bpf/libbpf_internal.h |  2 ++
>>  5 files changed, 58 insertions(+)
>>
>
> (ran out of time, will continue reviewing the rest of patches
> tomorrow, so please don't yet send new revision)
>
>> diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
>> index ab40dbf9f020f..27845e287dd5c 100644
>> --- a/tools/lib/bpf/bpf.c
>> +++ b/tools/lib/bpf/bpf.c
>> @@ -69,6 +69,51 @@ static inline __u64 ptr_to_u64(const void *ptr)
>>         return (__u64) (unsigned long) ptr;
>>  }
>>
>> +static inline int sys_bpf_extended(enum bpf_cmd cmd, union bpf_attr *attr,
>> +                                  unsigned int size,
>> +                                  struct bpf_common_attr *common_attrs,
>> +                                  unsigned int size_common)
>> +{
>> +       cmd = common_attrs ? cmd | BPF_COMMON_ATTRS : cmd & ~BPF_COMMON_ATTRS;
>> +       return syscall(__NR_bpf, cmd, attr, size, common_attrs, size_common);
>> +}
>> +
>> +static inline int sys_bpf_fd_extended(enum bpf_cmd cmd, union bpf_attr *attr,
>
> please shorten to sys_bpf_ext() and sys_bpf_ext_fd() (also note ext before fd)
>

The short ones look good to me.

>
>> +                                     unsigned int size,
>> +                                     struct bpf_common_attr *common_attrs,
>> +                                     unsigned int size_common)
>> +{
>> +       int fd;
>> +
>> +       fd = sys_bpf_extended(cmd, attr, size, common_attrs, size_common);
>> +       return ensure_good_fd(fd);
>> +}
>> +
>> +int probe_sys_bpf_extended(int token_fd)
>> +{
>> +       const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd);
>> +       struct bpf_common_attr common_attrs;
>> +       struct bpf_insn insns[] = {
>> +               BPF_MOV64_IMM(BPF_REG_0, 0),
>> +               BPF_EXIT_INSN(),
>> +       };
>> +       union bpf_attr attr;
>> +
>> +       memset(&attr, 0, attr_sz);
>> +       attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
>> +       attr.license = ptr_to_u64("GPL");
>> +       attr.insns = ptr_to_u64(insns);
>> +       attr.insn_cnt = (__u32)ARRAY_SIZE(insns);
>> +       attr.prog_token_fd = token_fd;
>> +       if (token_fd)
>> +               attr.prog_flags |= BPF_F_TOKEN_FD;
>> +       libbpf_strlcpy(attr.prog_name, "libbpf_sysbpftest", sizeof(attr.prog_name));
>> +       memset(&common_attrs, 0, sizeof(common_attrs));
>> +
>> +       return sys_bpf_fd_extended(BPF_PROG_LOAD, &attr, attr_sz, &common_attrs,
>> +                                  sizeof(common_attrs));
>
> I think we can set up this feature detector such that we get -EINVAL
> due to BPF_COMMON_ATTRS not supported on old kernels, while -EFAULT on
> newer kernels due to NULL passed in common_attrs. This would be cheap
> and simple. Try it.
>

Let me give that a try.

>> +}
>> +
>>  static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
>>                           unsigned int size)
>>  {
>> diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
>> index 7252150e7ad35..38819071ecbe7 100644
>> --- a/tools/lib/bpf/bpf.h
>> +++ b/tools/lib/bpf/bpf.h
>> @@ -35,6 +35,7 @@
>>  extern "C" {
>>  #endif
>>
>> +LIBBPF_API int probe_sys_bpf_extended(int token_fd);
>
> why adding this as a public UAPI?
>

If we don’t mark it with LIBBPF_API, the build fails when compiling libbpf.

My intention here wasn’t to introduce a new public UAPI, but simply to
provide a way for 'features.c' to probe whether the kernel supports the
extended BPF syscall, without directly exposing 'sys_bpf_fd_extended()'.

Do you have a suggestion on how we can perform this probe without
introducing a new LIBBPF_API symbol?

Thanks,
Leon

  reply	other threads:[~2025-09-23 15:36 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-11 16:33 [RFC PATCH bpf-next v2 0/6] bpf: Extend bpf syscall with common attributes support Leon Hwang
2025-09-11 16:33 ` [RFC PATCH bpf-next v2 1/6] " Leon Hwang
2025-09-17  0:06   ` Andrii Nakryiko
2025-09-23 15:23     ` Leon Hwang
2025-09-11 16:33 ` [RFC PATCH bpf-next v2 2/6] libbpf: Add support for extended bpf syscall Leon Hwang
2025-09-17  0:06   ` Andrii Nakryiko
2025-09-23 15:36     ` Leon Hwang [this message]
2025-09-24 23:57       ` Andrii Nakryiko
2025-09-11 16:33 ` [RFC PATCH bpf-next v2 3/6] bpf: Add common attr support for prog_load and btf_load Leon Hwang
2025-09-17 21:12   ` Andrii Nakryiko
2025-09-23 15:50     ` Leon Hwang
2025-09-25  0:00       ` Andrii Nakryiko
2025-09-11 16:33 ` [RFC PATCH bpf-next v2 4/6] bpf: Add common attr support for map_create Leon Hwang
2025-09-17 21:39   ` Andrii Nakryiko
2025-09-17 21:49     ` Alexei Starovoitov
2025-09-23 15:52       ` Leon Hwang
2025-09-23 16:27     ` Leon Hwang
2025-09-18 23:29   ` Eduard Zingerman
2025-09-23 16:31     ` Leon Hwang
2025-09-11 16:33 ` [RFC PATCH bpf-next v2 5/6] libbpf: " Leon Hwang
2025-09-17 21:45   ` Andrii Nakryiko
2025-09-17 21:46     ` Andrii Nakryiko
2025-09-23 16:40       ` Leon Hwang
2025-09-25  0:02         ` Andrii Nakryiko
2025-09-11 16:33 ` [RFC PATCH bpf-next v2 6/6] selftests/bpf: Add cases to test map create failure log Leon Hwang

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=d535ef7e-a7fb-41be-8550-bb0c0af045f9@linux.dev \
    --to=leon.hwang@linux.dev \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=menglong8.dong@gmail.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