From: Yonghong Song <yonghong.song@linux.dev>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Eduard Zingerman <eddyz87@gmail.com>,
kernel-team@fb.com
Subject: [PATCH bpf-next v5 3/3] selftests/bpf: Test CO-RE relocations of signed enum values without kflag
Date: Fri, 20 Nov 2026 13:50:12 -0800 [thread overview]
Message-ID: <20261120215012.870499-1-yonghong.song@linux.dev> (raw)
In-Reply-To: <20261120214957.868409-1-yonghong.song@linux.dev>
The libbpf fix takes a 32-bit target enum value with the kflag 0 as
signed if the local enum is signed, since the BTF of a kernel before
v6.0 has the kflag 0 on every enum, signed or not. Test it.
test_core_reloc_enumval gets an optional signed local enum,
named_int_enum, with the values -1 and 1, so the enum is signed while
one of its values is not negative. The enumval target has the same enum
with the values -2 and -3, which are expected sign-extended in the
64-bit outputs.
The new test case enumval___no_kflag has the same target types, but its
target BTF gets the kflag of every enum cleared, as in the BTF of a
kernel before v6.0. The signed enum then gets its signedness from the
local enum. The test writes the modified BTF to a temporary file: one
it creates itself in core_reloc, or the one btfgen already created in
core_reloc_btfgen. The cleanup now removes the temporary file if the
test case clears the kflag, not only if btfgen runs.
GCC's __builtin_preserve_enum_value() returns int, so with GCC the
stores sign-extend the values anyway.
Without taking the signedness from the local enum, the upper 32 bits of
both values are zero in enumval___no_kflag:
run_core_reloc_tests:FAIL:check_result input/output data don't match
output byte #44: EXP 0xff GOT 0x00
...
output byte #55: EXP 0xff GOT 0x00
#122/72 core_reloc/enumval___no_kflag:FAIL
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
.../selftests/bpf/prog_tests/core_reloc.c | 90 ++++++++++++++++++-
.../btf__core_reloc_enumval___no_kflag.c | 3 +
.../selftests/bpf/progs/core_reloc_types.h | 16 ++++
.../bpf/progs/test_core_reloc_enumval.c | 14 +++
4 files changed, 121 insertions(+), 2 deletions(-)
create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___no_kflag.c
diff --git a/tools/testing/selftests/bpf/prog_tests/core_reloc.c b/tools/testing/selftests/bpf/prog_tests/core_reloc.c
index 98b3c5fb1028..1c502e7f0c60 100644
--- a/tools/testing/selftests/bpf/prog_tests/core_reloc.c
+++ b/tools/testing/selftests/bpf/prog_tests/core_reloc.c
@@ -370,6 +370,14 @@ static int duration = 0;
.fails = true, \
}
+#define ENUMVAL_NO_KFLAG_CASE(name, ...) { \
+ ENUMVAL_CASE_COMMON(name), \
+ .output = STRUCT_TO_CHAR_PTR(core_reloc_enumval_output) \
+ __VA_ARGS__, \
+ .output_len = sizeof(struct core_reloc_enumval_output), \
+ .clear_enum_kflag = true, \
+}
+
#define ENUM64VAL_CASE_COMMON(name) \
.case_name = #name, \
.bpf_obj_file = "test_core_reloc_enum64val.bpf.o", \
@@ -406,6 +414,7 @@ struct core_reloc_test_case {
bool run_btfgen_fails;
bool needs_testmod;
bool relaxed_core_relocs;
+ bool clear_enum_kflag;
const char *prog_name;
const char *raw_tp_name;
setup_test_fn setup;
@@ -906,6 +915,24 @@ static const struct core_reloc_test_case test_cases[] = {
.anon_val2 = 0x20,
.uint_val1 = 0x80000001,
.uint_val2 = 0xffffffff,
+ .int_val1 = -2,
+ .int_val2 = -3,
+ }),
+ ENUMVAL_NO_KFLAG_CASE(enumval___no_kflag, {
+ .named_val1_exists = true,
+ .named_val2_exists = true,
+ .named_val3_exists = true,
+ .anon_val1_exists = true,
+ .anon_val2_exists = true,
+ .anon_val3_exists = true,
+ .named_val1 = 1,
+ .named_val2 = 2,
+ .anon_val1 = 0x10,
+ .anon_val2 = 0x20,
+ .uint_val1 = 0x80000001,
+ .uint_val2 = 0xffffffff,
+ .int_val1 = -2,
+ .int_val2 = -3,
}),
ENUMVAL_CASE(enumval___diff, {
.named_val1_exists = true,
@@ -999,6 +1026,46 @@ static int run_btfgen(const char *src_btf, const char *dst_btf, const char *objp
return run_bpftool_command(args);
}
+/*
+ * Write the BTF of src to dst with the kflag of every enum cleared, as in the
+ * BTF of a kernel before v6.0, where the kflag 0 doesn't mean unsigned.
+ */
+static int write_btf_no_enum_kflag(const char *src, const char *dst)
+{
+ const void *raw_data;
+ struct btf_type *t;
+ struct btf *btf;
+ __u32 i, n, raw_sz;
+ FILE *f = NULL;
+ int err = -1;
+
+ btf = btf__parse(src, NULL);
+ if (!ASSERT_OK_PTR(btf, "btf_parse"))
+ return -1;
+
+ n = btf__type_cnt(btf);
+ for (i = 1; i < n; i++) {
+ t = (struct btf_type *)btf__type_by_id(btf, i);
+ if (btf_is_enum(t))
+ t->info &= ~(1U << 31); /* the kflag */
+ }
+
+ raw_data = btf__raw_data(btf, &raw_sz);
+ if (!ASSERT_OK_PTR(raw_data, "btf_raw_data"))
+ goto out;
+ f = fopen(dst, "w");
+ if (!ASSERT_OK_PTR(f, "btf_fopen"))
+ goto out;
+ if (!ASSERT_EQ(fwrite(raw_data, 1, raw_sz, f), raw_sz, "btf_write"))
+ goto out;
+ err = 0;
+out:
+ if (f)
+ fclose(f);
+ btf__free(btf);
+ return err;
+}
+
static void run_core_reloc_tests(bool use_btfgen)
{
const size_t mmap_sz = roundup_page(sizeof(struct data));
@@ -1050,6 +1117,25 @@ static void run_core_reloc_tests(bool use_btfgen)
test_case->btf_src_file = btf_file;
}
+ /*
+ * Without btfgen, create the temporary file here; with btfgen,
+ * rewrite the one btfgen made.
+ */
+ if (test_case->clear_enum_kflag) {
+ if (!use_btfgen) {
+ fd = mkstemp(btf_file);
+ if (!ASSERT_GE(fd, 0, "btf_tmp"))
+ continue;
+ close(fd); /* we only need the path */
+ }
+ err = write_btf_no_enum_kflag(test_case->btf_src_file, btf_file);
+ if (err) {
+ remove(btf_file);
+ continue;
+ }
+ test_case->btf_src_file = btf_file;
+ }
+
if (test_case->setup) {
err = test_case->setup(test_case);
if (CHECK(err, "test_setup", "test #%d setup failed: %d\n", i, err))
@@ -1141,8 +1227,8 @@ static void run_core_reloc_tests(bool use_btfgen)
CHECK_FAIL(munmap(mmap_data, mmap_sz));
mmap_data = NULL;
}
- if (use_btfgen)
- remove(test_case->btf_src_file);
+ if (use_btfgen || test_case->clear_enum_kflag)
+ remove(btf_file);
bpf_link__destroy(link);
link = NULL;
bpf_object__close(obj);
diff --git a/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___no_kflag.c b/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___no_kflag.c
new file mode 100644
index 000000000000..f34e3e342efb
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___no_kflag.c
@@ -0,0 +1,3 @@
+#include "core_reloc_types.h"
+
+void f(struct core_reloc_enumval___no_kflag x) {}
diff --git a/tools/testing/selftests/bpf/progs/core_reloc_types.h b/tools/testing/selftests/bpf/progs/core_reloc_types.h
index 6d758e74bd58..35beb76068bd 100644
--- a/tools/testing/selftests/bpf/progs/core_reloc_types.h
+++ b/tools/testing/selftests/bpf/progs/core_reloc_types.h
@@ -1217,6 +1217,8 @@ struct core_reloc_enumval_output {
int anon_val2;
long uint_val1;
long uint_val2;
+ long int_val1;
+ long int_val2;
};
struct core_reloc_enum64val_output {
@@ -1250,10 +1252,24 @@ enum named_uint_enum {
UINT_ENUM_VAL2 = 0xffffffff,
};
+enum named_int_enum {
+ INT_ENUM_VAL1 = -2,
+ INT_ENUM_VAL2 = -3,
+};
+
struct core_reloc_enumval {
enum named_enum f1;
anon_enum f2;
enum named_uint_enum f3;
+ enum named_int_enum f4;
+};
+
+/* the same types, whose BTF gets the kflag of every enum cleared */
+struct core_reloc_enumval___no_kflag {
+ enum named_enum f1;
+ anon_enum f2;
+ enum named_uint_enum f3;
+ enum named_int_enum f4;
};
enum named_unsigned_enum64 {
diff --git a/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c b/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c
index 8b8abbaaf533..a524e5028c77 100644
--- a/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c
+++ b/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c
@@ -33,6 +33,12 @@ enum named_uint_enum {
UINT_ENUM_VAL2 = 0xfffffff0,
};
+/* signed 4-byte enum, with a non-negative value too */
+enum named_int_enum {
+ INT_ENUM_VAL1 = -1,
+ INT_ENUM_VAL2 = 1,
+};
+
struct core_reloc_enumval_output {
bool named_val1_exists;
bool named_val2_exists;
@@ -47,6 +53,8 @@ struct core_reloc_enumval_output {
int anon_val2;
long uint_val1;
long uint_val2;
+ long int_val1;
+ long int_val2;
};
#ifdef __clang__
@@ -85,6 +93,12 @@ int test_core_enumval(void *ctx)
out->uint_val1 = UINT_ENUM_VALUE(UINT_ENUM_VAL1);
if (bpf_core_enum_value_exists(enum named_uint_enum, UINT_ENUM_VAL2))
out->uint_val2 = UINT_ENUM_VALUE(UINT_ENUM_VAL2);
+
+ /* enum named_int_enum is optional */
+ if (bpf_core_enum_value_exists(enum named_int_enum, INT_ENUM_VAL1))
+ out->int_val1 = bpf_core_enum_value(enum named_int_enum, INT_ENUM_VAL1);
+ if (bpf_core_enum_value_exists(enum named_int_enum, INT_ENUM_VAL2))
+ out->int_val2 = bpf_core_enum_value(enum named_int_enum, INT_ENUM_VAL2);
#else
data.skip = true;
#endif
--
2.53.1-Meta
next prev parent reply other threads:[~2026-11-20 21:50 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 21:49 [PATCH bpf-next v5 0/3] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values Yonghong Song
2026-11-20 21:50 ` [PATCH bpf-next v5 1/3] " Yonghong Song
2026-11-20 21:50 ` [PATCH bpf-next v5 2/3] selftests/bpf: Test " Yonghong Song
2026-11-20 21:50 ` Yonghong Song [this message]
2026-11-20 22:39 ` [PATCH bpf-next v5 3/3] selftests/bpf: Test CO-RE relocations of signed enum values without kflag bot+bpf-ci
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=20261120215012.870499-1-yonghong.song@linux.dev \
--to=yonghong.song@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=kernel-team@fb.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox