Re: [PATCH bpf-next v3 1/3] libbpf: Normalize enum values according to signedness

From: Andrii Nakryiko

Date: Thu Nov 05 2026 - 17:17:04 EST


On Sun, Nov 1, 2026 at 2:32 AM Hungyu Lin <dennylin0707@xxxxxxxxx> wrote:
>
> BTF_KIND_ENUM stores enumerators in a signed 32-bit field even for
> unsigned enums. btf_dump_enum_data() compares unsigned data against that
> signed field, so values with bit 31 set fail to match their names.
> get_map_field_long() also sign-extends the field: for example,
> __ulong(map_extra, 0x80000000) becomes 0xffffffff80000000 instead of the
> requested arena mapping address.
>
> Add btf_enum_value() to extend an enumerator according to kflag and use
> it in both places.
>
> For data dumps, keep the original comparison as well. Older BTF producers
> did not record enum signedness; an eight-byte BTF_KIND_ENUM can contain
> signed data such as -1 with kflag clear. Using only the helper would lose
> an existing name match. This compatibility comparison does not truncate
> the data value or change the numeric fallback.
>
> For an eight-byte BTF_KIND_ENUM with kflag clear, a negative stored
> enumerator can therefore match both its sign- and zero-extended values.
> The record cannot distinguish legacy signed metadata from unsigned
> metadata with the same bits.
>
> Fixes: d90ec262b35b ("libbpf: Add enum64 support for btf_dump")
> Fixes: d147357e2e59 ("libbpf: Allow specifying 64-bit integers in map BTF.")
> Suggested-by: Alexei Starovoitov <ast@xxxxxxxxxx>
> Link: https://lore.kernel.org/bpf/DMJG1R2YIJRK.16CZLV9S5U7Z7@xxxxxxxxx/
> Signed-off-by: Hungyu Lin <dennylin0707@xxxxxxxxx>
> ---
> tools/lib/bpf/btf.h | 6 ++++++
> tools/lib/bpf/btf_dump.c | 3 ++-
> tools/lib/bpf/libbpf.c | 2 +-
> 3 files changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h
> index 57f12630c5d1..eeb4984d6c57 100644
> --- a/tools/lib/bpf/btf.h
> +++ b/tools/lib/bpf/btf.h
> @@ -636,6 +636,12 @@ static inline struct btf_enum *btf_enum(const struct btf_type *t)
> return (struct btf_enum *)(t + 1);
> }
>
> +/* btf_enum->val is signed even for unsigned enums; kflag records the sign. */
> +static inline __s64 btf_enum_value(const struct btf_type *t, const struct btf_enum *e)

btf_enum64_value() return __u64, why not make btf_enum_value() return
__u64 for consistency, and then cast to s64 in callers if kflag=1?

pw-bot: cr

> +{
> + return btf_kflag(t) ? (__s64)e->val : (__u32)e->val;
> +}
> +
> struct btf_enum64;
>
> static inline struct btf_enum64 *btf_enum64(const struct btf_type *t)
> diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
> index ea9a34db4c00..b636dc4185a0 100644
> --- a/tools/lib/bpf/btf_dump.c
> +++ b/tools/lib/bpf/btf_dump.c
> @@ -2373,7 +2373,8 @@ static int btf_dump_enum_data(struct btf_dump *d,
> const struct btf_enum *e;
>
> for (i = 0, e = btf_enum(t); i < btf_vlen(t); i++, e++) {
> - if (value != e->val)
> + /* Keep names from legacy signed enums without kflag. */
> + if (value != e->val && value != btf_enum_value(t, e))
> continue;
> btf_dump_type_values(d, "%s", btf_name_of(d, e->name_off));
> return 0;
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 02e75b183ed0..cecd6b5f27de 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -2627,7 +2627,7 @@ static bool get_map_field_long(const char *map_name, const struct btf *btf,
> if (btf_is_enum(t)) {
> const struct btf_enum *e = btf_enum(t);
>
> - *res = e->val;
> + *res = btf_enum_value(t, e);
> } else {
> const struct btf_enum64 *e = btf_enum64(t);
>
> --
> 2.43.0
>