Skip to contentSkip to conversation

libbpf: Add gating for arena globals relocation feature

PATCH Applied Emil Tsalapatis 3 participants 4 messages Feb 10

Subsystems (2)
lore
Share & tools
lore

Emil Tsalapatis

author reviewer
#0 lore raw

PATCH v4 1/1 libbpf: Add gating for arena globals relocation feature

Add feature gating for the arena globals relocation introduced in commit c1f61171d44b. The commit depends on a previous commit in the same patchset that is absent from older kernels (12a1fe6e12db, "bpf/verifier: Do not limit maximum direct offset into arena map"). Without this commit, arena globals relocation with arenas >= 512MiB fails to load and breaks libbpf's backwards compatibility.

Introduce a libbpf feature to check whether the running kernel includes patch 12a1fe6e12db, and only relocate arena globals if it does.

CHANGELOG
---------

v3 -> v4 (https://lore.kernel.org/bpf/[email protected]/)

Incorporate feedback from Andrii:
- Remove BTF and func_info
- Use TRACEPOINT instead of SYSCALL to avoid using the SLEEPABLE flag
- Improve string handling sizeof(log_buf) for libbpf buffer

v2 -> v3 (https://lore.kernel.org/bpf/[email protected]/)

- Rename feature to better specify the verifier feature it probes for (Andrii)

v1 -> v2 (https://lore.kernel.org/bpf/[email protected]/)

- Reworked test to use simpler array-based program (Andrii)

Fixes c1f61171d44b ("libbpf: Move arena globals to the end of the arena")
---
 tools/lib/bpf/features.c        | 65 +++++++++++++++++++++++++++++++++
 tools/lib/bpf/libbpf.c          |  7 +++-
 tools/lib/bpf/libbpf_internal.h |  2 +
 3 files changed, 72 insertions(+), 2 deletions(-)
tools/lib/bpf/features.c +65 -0
index b842b83e2480..9689e408ac58 100644
@@ -506,6 +506,68 @@ static int probe_kern_arg_ctx_tag(int token_fd)
 	return probe_fd(prog_fd);
 }
 
+static int probe_ldimm64_full_range_off(int token_fd)
+{
+	char log_buf[1024];
+	int prog_fd, map_fd;
+	int ret;
+	LIBBPF_OPTS(bpf_map_create_opts, map_opts,
+		.token_fd = token_fd,
+		.map_flags = token_fd ? BPF_F_TOKEN_FD : 0,
+	);
+	LIBBPF_OPTS(bpf_prog_load_opts, prog_opts,
+		.token_fd = token_fd,
+		.prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
+		.log_buf = log_buf,
+		.log_size = sizeof(log_buf),
+	);
+	struct bpf_insn insns[] = {
+		BPF_LD_MAP_VALUE(BPF_REG_1, 0, 1UL << 30),
+		BPF_EXIT_INSN(),
+	};
+	int insn_cnt = ARRAY_SIZE(insns);
+
+	map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "arr", sizeof(int), 1, 1, &map_opts);
+	if (map_fd < 0) {
+		ret = -errno;
+		pr_warn("Error in %s(): %s. Couldn't create simple array map.\n",
+			__func__, errstr(ret));
+		return ret;
+	}
+	insns[0].imm = map_fd;
+
+	prog_fd = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, "global_reloc", "GPL", insns, insn_cnt, &prog_opts);
+	ret = -errno;
+
+	close(map_fd);
+	close(prog_fd);
+
+	if (prog_fd >= 0) {
+		pr_warn("Error in %s(): Program loading unexpectedly succeeded.\n",
+			__func__);
+		return -EINVAL;
+	}
+
+	/*
+	 * Feature is allowed if we're not failing with the error message
+	 * "direct value offset of %u is not allowed" removed in 12a1fe6e12db
+	 * ("bpf/verifier: Do not limit maximum direct offset into arena map").
+	 * We should instead fail with "invalid access to map value pointer".
+	 * Ensure we match with one of the two and we're not failing with a
+	 * different, unexpected message.
+	 */
+	if (strstr(log_buf, "direct value offset of"))
+		return 0;
+
+	if (!strstr(log_buf, "invalid access to map value pointer")) {
+		pr_warn("Error in %s(): Program unexpectedly failed with message: %s.\n",
+			__func__, log_buf);
+		return ret;
+	}
+
+	return 1;
+}
+
 typedef int (*feature_probe_fn)(int /* token_fd */);
 
 static struct kern_feature_cache feature_cache;
@@ -581,6 +643,9 @@ static struct kern_feature_desc {
 	[FEAT_BTF_QMARK_DATASEC] = {
 		"BTF DATASEC names starting from '?'", probe_kern_btf_qmark_datasec,
 	},
+	[FEAT_LDIMM64_FULL_RANGE_OFF] = {
+		"Full range LDIMM64 support", probe_ldimm64_full_range_off,
+	},
 };
 
 bool feat_supported(struct kern_feature_cache *cache, enum kern_feature_id feat_id)
tools/lib/bpf/libbpf.c +5 -2
index 0c8bf0b5cce4..93e59ed8d9a1 100644
@@ -3009,8 +3009,11 @@ static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map,
 	memcpy(obj->arena_data, data, data_sz);
 	obj->arena_data_sz = data_sz;
 
-	/* place globals at the end of the arena */
-	obj->arena_data_off = mmap_sz - data_alloc_sz;
+	/* place globals at the end of the arena (if supported) */
+	if (kernel_supports(obj, FEAT_LDIMM64_FULL_RANGE_OFF))
+		obj->arena_data_off = mmap_sz - data_alloc_sz;
+	else
+		obj->arena_data_off = 0;
 
 	/* make bpf_map__init_value() work for ARENA maps */
 	map->mmaped = obj->arena_data;
tools/lib/bpf/libbpf_internal.h +2 -0
index fc59b21b51b5..974147e8a8aa 100644
@@ -392,6 +392,8 @@ enum kern_feature_id {
 	FEAT_ARG_CTX_TAG,
 	/* Kernel supports '?' at the front of datasec names */
 	FEAT_BTF_QMARK_DATASEC,
+	/* Kernel supports LDIMM64 imm offsets past 512 MiB. */
+	FEAT_LDIMM64_FULL_RANGE_OFF,
 	__FEAT_CNT,
 };
 
Signature · 1 line
2.49.0
#1 lore raw
Andrii requested in v3 review that the description should start with lowercase since it appears mid-sentence in error messages like "Detection of kernel %s support failed". Other feature…
Quoted 4 lines
tools/lib/bpf/features.c +0 -0
index b842b83e2480..9689e408ac58 100644

[ ... ]

tools/lib/bpf/features.c static struct kern_feature_desc { · line 647

Quoted 6 lines
tools/lib/bpf/features.c +2 -0
@@ -581,6 +643,9 @@ static struct kern_feature_desc {
 	[FEAT_BTF_QMARK_DATASEC] = {
 		"BTF DATASEC names starting from '?'", probe_kern_btf_qmark_datasec,
 	},
+	[FEAT_LDIMM64_FULL_RANGE_OFF] = {
+		"Full range LDIMM64 support", probe_ldimm64_full_range_off,
                 ^

Andrii requested in v3 review that the description should start with lowercase since it appears mid-sentence in error messages like "Detection of kernel %s support failed". Other feature descriptions that don't start with acronyms use lowercase (e.g., "global variables", "minimal BTF", "kernel-side __arg_ctx tag").

Should this be "full range LDIMM64 support" instead?

See: https://lore.kernel.org/bpf/CAEf4BzaLqWT1oj-EXibyAfE0ExmiaCCqhosRujDi7Y0Nyho6dA@mail.gmail.com/

tools/lib/bpf/features.c static struct kern_feature_desc { · line 649

Quoted 2 lines
tools/lib/bpf/features.c +1 -0
+	},
 };

---
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/21878398162

AI-authorship-score: low
AI-authorship-explanation: The code follows standard libbpf feature probe patterns, includes iterative review responses documented in changelog, and shows natural debugging artifacts from review cycles.
issues-found: 1
issue-severity-score: low
issue-severity-explanation: Minor style issue - feature description capitalization should be lowercase to match other feature descriptions.

#2 lore raw
Quoted 16 lines · On Tue, Feb 10, 2026 at 11:19 AM <[email protected]> wrote:
Quoted 5 lines
tools/lib/bpf/features.c +0 -0
index b842b83e2480..9689e408ac58 100644

[ ... ]

tools/lib/bpf/features.c static struct kern_feature_desc { · line 647

Quoted 6 lines
tools/lib/bpf/features.c +2 -0
@@ -581,6 +643,9 @@ static struct kern_feature_desc {
      [FEAT_BTF_QMARK_DATASEC] = {
              "BTF DATASEC names starting from '?'", probe_kern_btf_qmark_datasec,
      },
+     [FEAT_LDIMM64_FULL_RANGE_OFF] = {
+             "Full range LDIMM64 support", probe_ldimm64_full_range_off,
                 ^
Quoted 7 lines

Andrii requested in v3 review that the description should start with lowercase since it appears mid-sentence in error messages like "Detection of kernel %s support failed". Other feature descriptions that don't start with acronyms use lowercase (e.g., "global variables", "minimal BTF", "kernel-side __arg_ctx tag").

Should this be "full range LDIMM64 support" instead?

that's ok, I'll fix it up while applying, no need for v4

Quoted 18 lines

See: https://lore.kernel.org/bpf/CAEf4BzaLqWT1oj-EXibyAfE0ExmiaCCqhosRujDi7Y0Nyho6dA@mail.gmail.com/

tools/lib/bpf/features.c static struct kern_feature_desc { · line 649

Quoted 2 lines
tools/lib/bpf/features.c +1 -0
+     },
 };

---
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/21878398162

AI-authorship-score: low
AI-authorship-explanation: The code follows standard libbpf feature probe patterns, includes iterative review responses documented in changelog, and shows natural debugging artifacts from review cycles.
issues-found: 1
issue-severity-score: low
issue-severity-explanation: Minor style issue - feature description capitalization should be lowercase to match other feature descriptions.

#3 lore raw
Quoted 24 lines · Andrii Nakryiko
Quoted 22 lines · On Tue, Feb 10, 2026 at 11:19 AM <[email protected]> wrote:
Quoted 5 lines
tools/lib/bpf/features.c +0 -0
index b842b83e2480..9689e408ac58 100644

[ ... ]

tools/lib/bpf/features.c static struct kern_feature_desc { · line 647

Quoted 6 lines
tools/lib/bpf/features.c +2 -0
@@ -581,6 +643,9 @@ static struct kern_feature_desc {
      [FEAT_BTF_QMARK_DATASEC] = {
              "BTF DATASEC names starting from '?'", probe_kern_btf_qmark_datasec,
      },
+     [FEAT_LDIMM64_FULL_RANGE_OFF] = {
+             "Full range LDIMM64 support", probe_ldimm64_full_range_off,
                 ^

Andrii requested in v3 review that the description should start with lowercase since it appears mid-sentence in error messages like "Detection of kernel %s support failed". Other feature descriptions that don't start with acronyms use lowercase (e.g., "global variables", "minimal BTF", "kernel-side __arg_ctx tag").

Quoted 4 lines
Quoted 1 line

Should this be "full range LDIMM64 support" instead?

that's ok, I'll fix it up while applying, no need for v4

applied to bpf tree, thanks

Quoted 18 lines
Quoted 18 lines

See: https://lore.kernel.org/bpf/CAEf4BzaLqWT1oj-EXibyAfE0ExmiaCCqhosRujDi7Y0Nyho6dA@mail.gmail.com/

tools/lib/bpf/features.c static struct kern_feature_desc { · line 649

Quoted 2 lines
tools/lib/bpf/features.c +1 -0
+     },
 };

---
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/21878398162

AI-authorship-score: low
AI-authorship-explanation: The code follows standard libbpf feature probe patterns, includes iterative review responses documented in changelog, and shows natural debugging artifacts from review cycles.
issues-found: 1
issue-severity-score: low
issue-severity-explanation: Minor style issue - feature description capitalization should be lowercase to match other feature descriptions.