Re: [PATCH bpf-next v11 01/13] bpf: refactory struct_ops type initialization to a function.

Martin KaFai Lau
2023-11-10T01:11:17Z

On 11/6/23 12:12 PM, thinker.li@gmail.com wrote:
> diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c
> index db6176fb64dc..627cf1ea840a 100644
> --- a/kernel/bpf/bpf_struct_ops.c
> +++ b/kernel/bpf/bpf_struct_ops.c
> @@ -110,102 +110,111 @@ const struct bpf_prog_ops bpf_struct_ops_prog_ops = {
>
> static const struct btf_type *module_type;
>
> -void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log)
> +static void bpf_struct_ops_init_one(struct bpf_struct_ops *st_ops,
> + struct btf *btf,
> + struct bpf_verifier_log *log)
> {
> - s32 type_id, value_id, module_id;
> const struct btf_member *member;
> - struct bpf_struct_ops *st_ops;
> const struct btf_type *t;
> + s32 type_id, value_id;
> char value_name[128];
> const char *mname;
> - u32 i, j;
> + int i;
>
> - /* Ensure BTF type is emitted for "struct bpf_struct_ops_##_name" */
> -#define BPF_STRUCT_OPS_TYPE(_name) BTF_TYPE_EMIT(struct bpf_struct_ops_##_name);
> -#include "bpf_struct_ops_types.h"
> -#undef BPF_STRUCT_OPS_TYPE
> + if (strlen(st_ops->name) + VALUE_PREFIX_LEN >=
> + sizeof(value_name)) {
> + pr_warn("struct_ops name %s is too long\n",
> + st_ops->name);
> + return;
> + }
> + sprintf(value_name, "%s%s", VALUE_PREFIX, st_ops->name);
>
> - module_id = btf_find_by_name_kind(btf, "module", BTF_KIND_STRUCT);
> - if (module_id < 0) {
> - pr_warn("Cannot find struct module in btf_vmlinux\n");
> + value_id = btf_find_by_name_kind(btf, value_name,
> + BTF_KIND_STRUCT);
> + if (value_id < 0) {
> + pr_warn("Cannot find struct %s in btf_vmlinux\n",
> + value_name);

"btf_vmlinux" needs to change in the pr_warn(). It should be btf->name but that
may need a helper function to return btf->name.

> return;
> }
> - module_type = btf_type_by_id(btf, module_id);
>
> - for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
> - st_ops = bpf_struct_ops[i];
> + type_id = btf_find_by_name_kind(btf, st_ops->name,
> + BTF_KIND_STRUCT);
> + if (type_id < 0) {
> + pr_warn("Cannot find struct %s in btf_vmlinux\n",

Same here.