bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kui-Feng Lee <sinquersw@gmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>, thinker.li@gmail.com
Cc: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev,
	song@kernel.org, kernel-team@meta.com, andrii@kernel.org,
	drosen@google.com, kuifeng@meta.com
Subject: Re: [PATCH bpf-next v5 7/9] libbpf: Find correct module BTFs for struct_ops maps and progs.
Date: Tue, 17 Oct 2023 19:25:36 -0700	[thread overview]
Message-ID: <4fabd00b-3b72-49cb-a00d-6507a74dff72@gmail.com> (raw)
In-Reply-To: <CAEf4BzY9jYfK4Z7bAhmX458mZcGi+SLgGe4VK3WQYz2toOgdOA@mail.gmail.com>



On 10/17/23 14:49, Andrii Nakryiko wrote:
> On Tue, Oct 17, 2023 at 9:23 AM <thinker.li@gmail.com> wrote:
>>
>> From: Kui-Feng Lee <thinker.li@gmail.com>
>>
>> Locate the module BTFs for struct_ops maps and progs and pass them to the
>> kernel. This ensures that the kernel correctly resolves type IDs from the
>> appropriate module BTFs.
>>
>> For the map of a struct_ops object, mod_btf is added to bpf_map to keep a
>> reference to the module BTF. The FD of the module BTF is passed to the
>> kernel as mod_btf_fd when the struct_ops object is loaded.
>>
>> For a bpf_struct_ops prog, attach_btf_obj_fd of bpf_prog is the FD of a
>> module BTF in the kernel.
>>
>> Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
>> ---
>>   tools/lib/bpf/bpf.c    |  4 ++-
>>   tools/lib/bpf/bpf.h    |  5 +++-
>>   tools/lib/bpf/libbpf.c | 68 +++++++++++++++++++++++++++---------------
>>   3 files changed, 51 insertions(+), 26 deletions(-)
>>
> 
> I have a few nits, please accommodate them, and with that please add
> my ack on libbpf side of things
> 
> Acked-by: Andrii Nakryiko <andrii@kernel.org>


Thanks for reviewing!

> 
>> diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
>> index b0f1913763a3..af46488e4ea9 100644
>> --- a/tools/lib/bpf/bpf.c
>> +++ b/tools/lib/bpf/bpf.c
>> @@ -169,7 +169,8 @@ int bpf_map_create(enum bpf_map_type map_type,
>>                     __u32 max_entries,
>>                     const struct bpf_map_create_opts *opts)
>>   {
>> -       const size_t attr_sz = offsetofend(union bpf_attr, map_extra);
>> +       const size_t attr_sz = offsetofend(union bpf_attr,
>> +                                          value_type_btf_obj_fd);
>>          union bpf_attr attr;
>>          int fd;
>>
>> @@ -191,6 +192,7 @@ int bpf_map_create(enum bpf_map_type map_type,
>>          attr.btf_key_type_id = OPTS_GET(opts, btf_key_type_id, 0);
>>          attr.btf_value_type_id = OPTS_GET(opts, btf_value_type_id, 0);
>>          attr.btf_vmlinux_value_type_id = OPTS_GET(opts, btf_vmlinux_value_type_id, 0);
>> +       attr.value_type_btf_obj_fd = OPTS_GET(opts, value_type_btf_obj_fd, 0);
>>
>>          attr.inner_map_fd = OPTS_GET(opts, inner_map_fd, 0);
>>          attr.map_flags = OPTS_GET(opts, map_flags, 0);
>> diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
>> index 74c2887cfd24..1733cdc21241 100644
>> --- a/tools/lib/bpf/bpf.h
>> +++ b/tools/lib/bpf/bpf.h
>> @@ -51,8 +51,11 @@ struct bpf_map_create_opts {
>>
>>          __u32 numa_node;
>>          __u32 map_ifindex;
>> +
>> +       __u32 value_type_btf_obj_fd;
>> +       size_t:0;
>>   };
>> -#define bpf_map_create_opts__last_field map_ifindex
>> +#define bpf_map_create_opts__last_field value_type_btf_obj_fd
>>
>>   LIBBPF_API int bpf_map_create(enum bpf_map_type map_type,
>>                                const char *map_name,
>> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>> index 3a6108e3238b..d8a60fb52f5c 100644
>> --- a/tools/lib/bpf/libbpf.c
>> +++ b/tools/lib/bpf/libbpf.c
>> @@ -519,6 +519,7 @@ struct bpf_map {
>>          struct bpf_map_def def;
>>          __u32 numa_node;
>>          __u32 btf_var_idx;
>> +       int mod_btf_fd;
>>          __u32 btf_key_type_id;
>>          __u32 btf_value_type_id;
>>          __u32 btf_vmlinux_value_type_id;
>> @@ -893,6 +894,8 @@ bpf_object__add_programs(struct bpf_object *obj, Elf_Data *sec_data,
>>          return 0;
>>   }
>>
>> +static int load_module_btfs(struct bpf_object *obj);
>> +
> 
> you don't need this forward declaration, do you?


I will remove it.

> 
>>   static const struct btf_member *
>>   find_member_by_offset(const struct btf_type *t, __u32 bit_offset)
>>   {
>> @@ -922,22 +925,29 @@ find_member_by_name(const struct btf *btf, const struct btf_type *t,
>>          return NULL;
>>   }
>>
> 
> [...]
> 
>>          if (obj->btf && btf__fd(obj->btf) >= 0) {
>>                  create_attr.btf_fd = btf__fd(obj->btf);
>> @@ -7700,9 +7718,9 @@ static int bpf_object__read_kallsyms_file(struct bpf_object *obj)
>>          return libbpf_kallsyms_parse(kallsyms_cb, obj);
>>   }
>>
>> -static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name,
>> -                           __u16 kind, struct btf **res_btf,
>> -                           struct module_btf **res_mod_btf)
>> +static int find_module_btf_id(struct bpf_object *obj, const char *kern_name,
>> +                             __u16 kind, struct btf **res_btf,
>> +                             struct module_btf **res_mod_btf)
> 
> I actually find "find_module" terminology confusing, because it might
> not be in the module after all, right? I think "find_ksym_btf_id" is a
> totally appropriate name, and it's just that in some cases that kernel
> symbol (ksym) will be found in the kernel module instead of in vmlinux
> image itself. Still, it's a kernel. Let's keep the name?

Agree!

> 
>>   {
>>          struct module_btf *mod_btf;
>>          struct btf *btf;
> 
> [...]

  reply	other threads:[~2023-10-18  2:25 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-17 16:22 [PATCH bpf-next v5 0/9] Registrating struct_ops types from modules thinker.li
2023-10-17 16:22 ` [PATCH bpf-next v5 1/9] bpf: refactory struct_ops type initialization to a function thinker.li
2023-10-17 16:22 ` [PATCH bpf-next v5 2/9] bpf: add struct_ops_tab to btf thinker.li
2023-10-19  0:00   ` Martin KaFai Lau
2023-10-19  0:33     ` Kui-Feng Lee
2023-10-19  2:28   ` Martin KaFai Lau
2023-10-19 16:15     ` Kui-Feng Lee
2023-10-17 16:23 ` [PATCH bpf-next v5 3/9] bpf: hold module for bpf_struct_ops_map thinker.li
2023-10-19  0:36   ` Martin KaFai Lau
2023-10-19 16:29     ` Kui-Feng Lee
2023-10-20  5:07       ` Kui-Feng Lee
2023-10-20 21:37         ` Martin KaFai Lau
2023-10-20 22:28           ` Kui-Feng Lee
2023-10-17 16:23 ` [PATCH bpf-next v5 4/9] bpf: validate value_type thinker.li
2023-10-17 16:23 ` [PATCH bpf-next v5 5/9] bpf: pass attached BTF to the bpf_struct_ops subsystem thinker.li
2023-10-17 16:23 ` [PATCH bpf-next v5 6/9] bpf, net: switch to dynamic registration thinker.li
2023-10-19  1:49   ` Martin KaFai Lau
2023-10-20 15:12     ` Kui-Feng Lee
2023-10-20 17:53       ` Kui-Feng Lee
2023-10-17 16:23 ` [PATCH bpf-next v5 7/9] libbpf: Find correct module BTFs for struct_ops maps and progs thinker.li
2023-10-17 21:49   ` Andrii Nakryiko
2023-10-18  2:25     ` Kui-Feng Lee [this message]
2023-10-19  2:43   ` Martin KaFai Lau
2023-10-19 16:31     ` Kui-Feng Lee
2023-10-17 16:23 ` [PATCH bpf-next v5 8/9] bpf: export btf_ctx_access to modules thinker.li
2023-10-17 16:23 ` [PATCH bpf-next v5 9/9] selftests/bpf: test case for register_bpf_struct_ops() thinker.li
2023-10-17 18:03   ` kernel test robot

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=4fabd00b-3b72-49cb-a00d-6507a74dff72@gmail.com \
    --to=sinquersw@gmail.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=drosen@google.com \
    --cc=kernel-team@meta.com \
    --cc=kuifeng@meta.com \
    --cc=martin.lau@linux.dev \
    --cc=song@kernel.org \
    --cc=thinker.li@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;
as well as URLs for NNTP newsgroup(s).