Re: [PATCH bpf-next v1 1/2] resolve_btfids: Emit arena attributes from kfunc parameter suffixes

[email protected]
Newsgroups org.kernel.vger.bpf
Message-ID <dc4f01fc137a1c4c66bb2eb39f721654bf539f065a623af7dd05b90c32b6f569@mail.kernel.org>
> diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/main.c
> index d2e4176339da..3c88ea192f7b 100644
> --- a/tools/bpf/resolve_btfids/main.c
> +++ b/tools/bpf/resolve_btfids/main.c

[ ... ]

> @@ -1067,6 +1070,22 @@ static int collect_decl_tags(struct btf2btf_context *ctx)
>  	return 0;
>  }
>
> +static bool param_name_has_suffix(const char *name, const char *suffix)
> +{
> +	size_t name_len = strlen(name);
> +	size_t suffix_len = strlen(suffix);
> +
> +	return name_len >= suffix_len && !strcmp(name + name_len - suffix_len, suffix);
> +}

Should this be `name_len > suffix_len` instead of `name_len >= suffix_len`?

The kernel's btf_param_match_suffix() in kernel/bpf/btf.c rejects a parameter
whose name is exactly equal to the suffix:

kernel/bpf/btf.c:btf_param_match_suffix() {
    ...
    len = strlen(param_name);
    if (len <= suffix_len)
        return false;
    param_name += len - suffix_len;
    return !strncmp(param_name, suffix, suffix_len);
}

That `len <= suffix_len` early return is what both is_kfunc_arg_arena() in
kernel/bpf/verifier.c and btf_func_model_flags() in kernel/bpf/btf.c rely on.
The same helper is also used by kernel/bpf/bpf_struct_ops.c for arena suffix
matching.

With the current implementation, a parameter named exactly "__arena" would be
treated as an arena pointer by resolve_btfids (causing address_space(1) to be
emitted in vmlinux.h), but would be treated as an ordinary kernel pointer by
the verifier's is_kfunc_arg_arena().

No in-tree kfunc currently uses a parameter named exactly "__arena" or
"__arena__nullable" (checked kernel/bpf/arena.c, test_kmods/bpf_testmod.c,
and btf_data.c), and the selftests added by commit 52b915d08490 only use
names like "a__arena", so this would be a latent inconsistency rather than a
currently-firing failure.

> +
> +static bool is_arena_param(const struct btf *btf, const struct btf_param *param)
> +{
> +	const char *name = btf__name_by_offset(btf, param->name_off);
> +
> +	return param_name_has_suffix(name, PARAM_SUFFIX_ARENA) ||
> +	       param_name_has_suffix(name, PARAM_SUFFIX_ARENA_NULLABLE);
> +}

[ ... ]


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/31305217088
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.