From: Martin KaFai Lau <martin.lau@linux.dev>
To: thinker.li@gmail.com
Cc: sinquersw@gmail.com, kuifeng@meta.com, bpf@vger.kernel.org,
ast@kernel.org, song@kernel.org, kernel-team@meta.com,
andrii@kernel.org
Subject: Re: [RFC bpf-next v3 02/11] bpf: add struct_ops_tab to btf.
Date: Mon, 25 Sep 2023 14:10:12 -0700 [thread overview]
Message-ID: <c77c5a5d-7174-4770-4ffb-ee297a28f025@linux.dev> (raw)
In-Reply-To: <20230920155923.151136-3-thinker.li@gmail.com>
On 9/20/23 8:59 AM, thinker.li@gmail.com wrote:
> From: Kui-Feng Lee <thinker.li@gmail.com>
>
> struct_ops_tab will be used to restore registered struct_ops.
>
> Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
> ---
> include/linux/btf.h | 9 +++++
> kernel/bpf/btf.c | 84 +++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 93 insertions(+)
>
> diff --git a/include/linux/btf.h b/include/linux/btf.h
> index 928113a80a95..5fabe23aedd2 100644
> --- a/include/linux/btf.h
> +++ b/include/linux/btf.h
> @@ -571,4 +571,13 @@ static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type
> return btf_type_is_struct(t);
> }
>
> +struct bpf_struct_ops;
> +
> +int btf_add_struct_ops_btf(struct bpf_struct_ops *st_ops,
> + struct btf *btf);
> +int btf_add_struct_ops(struct bpf_struct_ops *st_ops,
> + struct module *owner);
> +const struct bpf_struct_ops **
> +btf_get_struct_ops(struct btf *btf, u32 *ret_cnt);
> +
> #endif
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index f93e835d90af..3fb9964f8672 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -241,6 +241,12 @@ struct btf_id_dtor_kfunc_tab {
> struct btf_id_dtor_kfunc dtors[];
> };
>
> +struct btf_struct_ops_tab {
> + u32 cnt;
> + u32 capacity;
> + struct bpf_struct_ops *ops[];
> +};
> +
> struct btf {
> void *data;
> struct btf_type **types;
> @@ -258,6 +264,7 @@ struct btf {
> struct btf_kfunc_set_tab *kfunc_set_tab;
> struct btf_id_dtor_kfunc_tab *dtor_kfunc_tab;
> struct btf_struct_metas *struct_meta_tab;
> + struct btf_struct_ops_tab *struct_ops_tab;
>
> /* split BTF support */
> struct btf *base_btf;
> @@ -1688,11 +1695,20 @@ static void btf_free_struct_meta_tab(struct btf *btf)
> btf->struct_meta_tab = NULL;
> }
>
> +static void btf_free_struct_ops_tab(struct btf *btf)
> +{
> + struct btf_struct_ops_tab *tab = btf->struct_ops_tab;
> +
> + kfree(tab);
> + btf->struct_ops_tab = NULL;
> +}
> +
> static void btf_free(struct btf *btf)
> {
> btf_free_struct_meta_tab(btf);
> btf_free_dtor_kfunc_tab(btf);
> btf_free_kfunc_set_tab(btf);
> + btf_free_struct_ops_tab(btf);
> kvfree(btf->types);
> kvfree(btf->resolved_sizes);
> kvfree(btf->resolved_ids);
> @@ -8601,3 +8617,71 @@ bool btf_type_ids_nocast_alias(struct bpf_verifier_log *log,
>
> return !strncmp(reg_name, arg_name, cmp_len);
> }
> +
> +int btf_add_struct_ops_btf(struct bpf_struct_ops *st_ops, struct btf *btf)
A few nits.
'struct btf *btf' as the first argument, to be consistent with other similar btf
functions.
This new function is not used outside of this file, so at least static. I would
just fold this into btf_add_struct_ops() below which currently is mostly empty
other than a btf_get/put.
> +{
> + struct btf_struct_ops_tab *tab;
> + int i;
> +
> + /* Assume this function is called for a module when the module is
> + * loading.
> + */
> +
> + tab = btf->struct_ops_tab;
> + if (!tab) {
> + tab = kzalloc(sizeof(*tab) +
> + sizeof(struct bpf_struct_ops *) * 4,
> + GFP_KERNEL);
nit. offsetof(struct bpf_struct_ops_tab, ops[4]).
> + if (!tab)
> + return -ENOMEM;
> + tab->capacity = 4;
> + btf->struct_ops_tab = tab;
> + }
> +
> + for (i = 0; i < tab->cnt; i++)
> + if (tab->ops[i] == st_ops)
> + return -EEXIST;
> +
> + if (tab->cnt == tab->capacity) {
> + struct btf_struct_ops_tab *new_tab;
> +
> + new_tab = krealloc(tab, sizeof(*tab) +
> + sizeof(struct bpf_struct_ops *) *
> + tab->capacity * 2, GFP_KERNEL);
> + if (!new_tab)
> + return -ENOMEM;
> + tab = new_tab;
> + tab->capacity *= 2;
> + btf->struct_ops_tab = tab;
> + }
> +
> + btf->struct_ops_tab->ops[btf->struct_ops_tab->cnt++] = st_ops;
> +
> + return 0;
> +}
> +
> +int btf_add_struct_ops(struct bpf_struct_ops *st_ops, struct module *owner)
> +{
> + struct btf *btf = btf_get_module_btf(owner);
> + int ret;
> +
> + if (!btf)
> + return -ENOENT;
> +
> + ret = btf_add_struct_ops_btf(st_ops, btf);
> +
> + btf_put(btf);
> +
> + return ret;
> +}
> +
> +const struct bpf_struct_ops **btf_get_struct_ops(struct btf *btf, u32 *ret_cnt)
> +{
> + if (!btf)
> + return NULL;
> + if (!btf->struct_ops_tab)
> + return NULL;
> +
> + *ret_cnt = btf->struct_ops_tab->cnt;
> + return (const struct bpf_struct_ops **)btf->struct_ops_tab->ops;
> +}
next prev parent reply other threads:[~2023-09-25 21:10 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-20 15:59 [RFC bpf-next v3 00/11] Registrating struct_ops types from modules thinker.li
2023-09-20 15:59 ` [RFC bpf-next v3 01/11] bpf: refactory struct_ops type initialization to a function thinker.li
2023-09-20 15:59 ` [RFC bpf-next v3 02/11] bpf: add struct_ops_tab to btf thinker.li
2023-09-25 21:10 ` Martin KaFai Lau [this message]
2023-09-25 21:45 ` Kui-Feng Lee
2023-09-20 15:59 ` [RFC bpf-next v3 03/11] bpf: add register and unregister functions for struct_ops thinker.li
2023-09-25 23:07 ` Martin KaFai Lau
2023-09-25 23:13 ` Kui-Feng Lee
2023-09-25 23:31 ` Martin KaFai Lau
2023-09-26 0:19 ` Kui-Feng Lee
2023-09-20 15:59 ` [RFC bpf-next v3 04/11] bpf: attach a module BTF to a bpf_struct_ops thinker.li
2023-09-25 22:57 ` Martin KaFai Lau
2023-09-25 23:25 ` Kui-Feng Lee
2023-09-20 15:59 ` [RFC bpf-next v3 05/11] bpf: hold module for bpf_struct_ops_map thinker.li
2023-09-25 23:23 ` Martin KaFai Lau
2023-09-25 23:42 ` Kui-Feng Lee
2023-09-20 15:59 ` [RFC bpf-next v3 06/11] bpf: validate value_type thinker.li
2023-09-26 1:03 ` Martin KaFai Lau
2023-09-27 20:27 ` Kui-Feng Lee
2023-09-20 15:59 ` [RFC bpf-next v3 07/11] bpf, net: switch to storing struct_ops in btf thinker.li
2023-09-20 23:45 ` kernel test robot
2023-09-24 3:23 ` kernel test robot
2023-09-25 8:30 ` kernel test robot
2023-09-26 0:02 ` Martin KaFai Lau
2023-09-26 0:18 ` Kui-Feng Lee
2023-09-20 15:59 ` [RFC bpf-next v3 08/11] bpf: pass attached BTF to find correct type info of struct_ops progs thinker.li
2023-09-25 22:58 ` Andrii Nakryiko
2023-09-25 23:50 ` Kui-Feng Lee
2023-09-26 0:24 ` Martin KaFai Lau
2023-09-26 0:58 ` Kui-Feng Lee
2023-09-20 15:59 ` [RFC bpf-next v3 09/11] libbpf: Find correct module BTFs for struct_ops maps and progs thinker.li
2023-09-25 23:09 ` Andrii Nakryiko
2023-09-26 0:12 ` Kui-Feng Lee
2023-09-20 15:59 ` [RFC bpf-next v3 10/11] bpf: export btf_ctx_access to modules thinker.li
2023-09-20 15:59 ` [RFC bpf-next v3 11/11] selftests/bpf: test case for register_bpf_struct_ops() thinker.li
2023-09-26 1:19 ` Martin KaFai Lau
2023-09-26 1:33 ` [RFC bpf-next v3 00/11] Registrating struct_ops types from modules Martin KaFai Lau
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=c77c5a5d-7174-4770-4ffb-ee297a28f025@linux.dev \
--to=martin.lau@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=kernel-team@meta.com \
--cc=kuifeng@meta.com \
--cc=sinquersw@gmail.com \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.