From: Hungyu Lin <dennylin0707@gmail.com>
To: ast@kernel.org, bpf@vger.kernel.org
Cc: andrii@kernel.org, qmo@kernel.org, daniel@iogearbox.net,
yonghong.song@linux.dev, eddyz87@gmail.com,
ihor.solodrai@linux.dev, shuah@kernel.org,
linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
Hungyu Lin <dennylin0707@gmail.com>
Subject: [PATCH bpf-next v3 1/3] libbpf: Normalize enum values according to signedness
Date: Sun, 1 Nov 2026 10:32:19 +0000 [thread overview]
Message-ID: <20261101103221.51534-2-dennylin0707@gmail.com> (raw)
In-Reply-To: <20261101103221.51534-1-dennylin0707@gmail.com>
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@kernel.org>
Link: https://lore.kernel.org/bpf/DMJG1R2YIJRK.16CZLV9S5U7Z7@gmail.com/
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
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)
+{
+ 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
next prev parent reply other threads:[~2026-11-01 10:32 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-01 10:32 [PATCH bpf-next v3 0/3] Normalize enum values in libbpf and bpftool Hungyu Lin
2026-11-01 10:32 ` Hungyu Lin [this message]
2026-11-01 10:32 ` [PATCH bpf-next v3 2/3] bpftool: Preserve unsigned enum values in data dumps Hungyu Lin
2026-11-01 10:32 ` [PATCH bpf-next v3 3/3] selftests/bpf: Cover enum signedness and legacy name matching Hungyu Lin
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=20261101103221.51534-2-dennylin0707@gmail.com \
--to=dennylin0707@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=ihor.solodrai@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=qmo@kernel.org \
--cc=shuah@kernel.org \
--cc=yonghong.song@linux.dev \
/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
all inboxes | Powered by JetHome®