bpftool: Compute map size of light skeletons at runtime
PATCH v3 1/1 bpftool: Compute map size of light skeletons at runtime
bpftool rounds memory-mapped data map sizes to the host page size when generating a light skeleton. The generated code therefore uses a 64K mapping size when bpftool runs on a 64K-page host, even if the skeleton runs on a 4K-page target. The target rejects the oversized map mmap(), causing failure of loading the light skeleton.
When try to run 64K-page selftests on 4K-page VM, the error message does not provide the reason about page size.
test_atomics:PASS:atomics skeleton open 0 nsec test_atomics:FAIL:atomics skeleton load unexpected error: -12 (errno 22)
#15 atomics:FAIL
Pass the original map value size and max entries to the generated code and round mmap size to the runtime page size in the user-space light skeleton helpers. This keeps generated light skeletons independent of the build host page size.
---
v2 -> v3:
* Hoist 'sysconf(_SC_PAGE_SIZE)' and the inner 'roundup()' in skel_map_mmap_sz(). (bot+bpf-ci)
* v2: https://lore.kernel.org/bpf/[email protected]/
v1 -> v2:
* Pass max entries alongside value size.
* Use the computed mmap size for mprotect() in userspace's skel_protect_map_data().
* Round up the mmap size to page size with keeping the original multiplying max_entries logic. (Andrii)
* v1: https://lore.kernel.org/bpf/[email protected]/
--- tools/bpf/bpftool/gen.c | 26 ++++++------------- tools/lib/bpf/skel_internal.h | 48 +++++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 32 deletions(-)
tools/bpf/bpftool/gen.c +8 -18
@@ -500,16 +500,6 @@ static void print_hex(const char *data, int data_sz) } } -static size_t bpf_map_mmap_sz (const struct bpf_map *map) -{ -long page_sz =sysconf (_SC_PAGE_SIZE); -size_t map_sz; - - map_sz = (size_t )roundup (bpf_map__value_size (map),8 ) *bpf_map__max_entries (map); - map_sz =roundup (map_sz, page_sz); -return map_sz; -} -/* Emit type size asserts for all top-level fields in memory-mapped internal maps. */ static void codegen_asserts (struct bpf_object *obj,const char *obj_name) {
@@ -686,8 +676,8 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)if (!get_map_ident (map, ident,sizeof (ident)))continue ;if (is_skel_data (map, ident,sizeof (ident))) -printf ("\tskel_free_map_data(skel->%1$s, skel->maps.%1$s.initial_value, %2$zu);\n" , - ident,bpf_map_mmap_sz (map)); +printf ("\tskel_free_map_data(skel->%1$s, skel->maps.%1$s.initial_value, %2$u, %3$u);\n" , + ident,bpf_map__value_size (map),bpf_map__max_entries (map)); codegen("\ \n\ skel_closenz(skel->maps.%1$s.map_fd); \n\
@@ -771,13 +761,13 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h \n\ \"; \n\\n\ -skel->%1$s = (__typeof__(skel->%1$s))skel_prep_map_data((void *)data, %2$zd,\n\ +skel->%1$s = (__typeof__(skel->%1$s))skel_prep_map_data((void *)data, %2$u, %3$u,\n\ sizeof(data) - 1);\n\ if (!skel->%1$s) \n\ goto cleanup; \n\ skel->maps.%1$s.initial_value = (__u64) (long) skel->%1$s;\n\ } \n\ - ", ident, bpf_map_mmap_sz(map)); + ", ident, bpf_map__value_size(map), bpf_map__max_entries(map)); } codegen("\ \n\
@@ -871,14 +861,14 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *hif (bpf_map__type (map) ==BPF_MAP_TYPE_PERCPU_ARRAY ) {codegen ("\ \n\ -err = skel_protect_map_data(skel->%1$s, &skel->maps.%1$s.initial_value, %2$zd);\n\ +err = skel_protect_map_data(skel->%1$s, &skel->maps.%1$s.initial_value, %2$u, %3$u);\n\ if (err) \n\ return err; \n\ #ifdef __KERNEL__ \n\ skel->%1$s = NULL; \n\ #endif \n\ " , - ident,bpf_map_mmap_sz (map)); + ident,bpf_map__value_size (map),bpf_map__max_entries (map));continue ; }
@@ -890,11 +880,11 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *hcodegen ("\ \n\ skel->%1$s = (__typeof__(skel->%1$s))skel_finalize_map_data(&skel->maps.%1$s.initial_value,\n\ -%2$zd, %3$s, skel->maps.%1$s.map_fd);\n\ +%2$u, %3$u, %4$s, skel->maps.%1$s.map_fd);\n\ if (!skel->%1$s) \n\ return -ENOMEM; \n\ " , - ident,bpf_map_mmap_sz (map), mmap_flags); + ident,bpf_map__value_size (map),bpf_map__max_entries (map), mmap_flags); } codegen("\ \n\
tools/lib/bpf/skel_internal.h +34 -14
110 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] bpf: compilation failure in skel_internal.h due to non-standard roundup macro --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] bpf: compilation failure in skel_internal.h due to non-standard roundup macro --
commit cf9410def9715a95832e67f7002ebd4dabf2d7ad
Author: Leon Hwang <[email protected]>
bpftool: Compute map size of light skeletons at runtime
This commit addresses an issue with generating light skeletons on a host with a different page size from the target. It accomplishes this by passing the original map dimensions and dynamically computing the mmap size at runtime.
Quoted 4 lines
tools/lib/bpf/skel_internal.h +0
-0
[ ... ]
tools/lib/bpf/skel_internal.h static inline void skel_free(void *p) · line 206
Quoted 13 lines
tools/lib/bpf/skel_internal.h +6
-2@@ -193,25 +197,39 @@ static inline void skel_free(void *p)free (p); } -static inline void skel_free_map_data (void *p,__u64 addr,size_t sz) +static inline size_t skel_map_mmap_sz (size_t val_sz,__u32 max_entries) { -munmap (p, sz); +const long page_sz =sysconf (_SC_PAGE_SIZE); +size_t mmap_sz; + + mmap_sz =roundup (val_sz,8 ) * max_entries; + mmap_sz =roundup (mmap_sz, page_sz);
[Severity: Low]
Does this code rely on the non-standard roundup macro from <sys/param.h>?
When users include the generated light skeleton header in a project compiled with strict C standards (e.g., -std=c99 or -std=c11) without _DEFAULT_SOURCE or _GNU_SOURCE, the roundup macro might not be defined. This can cause a compilation error due to an implicit declaration of function.
Quoted 2 lines
tools/lib/bpf/skel_internal.h +1
-0+return mmap_sz; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewThis isn't a bug, but would naming these something like value_sz and init_sz make it harder to mix up the map value size and the embedded blob length? The two names differ by three characters and…
Quoted 4 lines
tools/bpf/bpftool/gen.c +0
-0
[ ... ]
Quoted 29 lines
tools/lib/bpf/skel_internal.h +4
-2@@ -11,6 +11,7 @@#include <linux/bpf.h> #else #include <unistd.h> +#include <sys/param.h> #include <sys/syscall.h> #include <sys/mman.h> #include <linux/keyctl.h> @@ -127,7 +128,7 @@ static inline void skel_free(const void *p)* either bpf_probe_read_kernel() or bpf_copy_from_user() from initial_value * depending on bpf_loader_ctx->flags. */ -static inline void skel_free_map_data (void *p,__u64 addr,size_t sz) +static inline void skel_free_map_data (void *p,__u64 addr,size_t val_sz,__u32 max_entries) {if (addr != ~0ULL )kvfree (p);@@ -138,18 +139,20 @@ static inline void skel_free_map_data(void *p, __u64 addr, size_t sz)*/ } -static inline void *skel_prep_map_data (const void *val,size_t mmap_sz,size_t val_sz) +static inline void *skel_prep_map_data (const void *val,size_t val_sz,__u32 max_entries, +size_t data_sz) {
tools/lib/bpf/skel_internal.h static inline void skel_free_map_data(void *p, __u64 addr, size_t sz) · line 152
Quoted 10 lines
tools/lib/bpf/skel_internal.h +2
-2void *addr; - addr =kvmalloc (val_sz,GFP_KERNEL ); + addr =kvmalloc (data_sz,GFP_KERNEL );if (!addr)return NULL ; -memcpy (addr, val, val_sz); +memcpy (addr, val, data_sz);return addr; }
This isn't a bug, but would naming these something like value_sz and init_sz make it harder to mix up the map value size and the embedded blob length? The two names differ by three characters and describe different quantities, and in the __KERNEL__ variant above val_sz is not referenced at all while data_sz drives both the kvmalloc() and the memcpy(). A future edit that reaches for the wrong one would size an allocation or a copy incorrectly.
Quoted 4 lines
tools/lib/bpf/skel_internal.h +2
-1-static inline void *skel_finalize_map_data (__u64 *init_val,size_t mmap_sz,int flags,int fd) +static inline void *skel_finalize_map_data (__u64 *init_val,size_t val_sz,__u32 max_entries, +int flags,int fd)
[ ... ]
Quoted 3 lines
tools/lib/bpf/skel_internal.h +0
-0@@ -193,25 +197,39 @@ static inline void skel_free(void *p)free (p); }
[ ... ]
Quoted 6 lines
tools/lib/bpf/skel_internal.h +3
-1-static inline void *skel_prep_map_data (const void *val,size_t mmap_sz,size_t val_sz) +static inline void *skel_prep_map_data (const void *val,size_t val_sz,__u32 max_entries, +size_t data_sz) { +size_t mmap_sz =skel_map_mmap_sz (val_sz, max_entries);
tools/lib/bpf/skel_internal.h static inline void skel_free(void *p) · line 227
Quoted 10 lines
tools/lib/bpf/skel_internal.h +1
-1void *addr; addr =mmap (NULL , mmap_sz,PROT_READ |PROT_WRITE ,MAP_SHARED |MAP_ANONYMOUS ,-1 ,0 );if (addr == (void *)-1 )return NULL ; -memcpy (addr, val, val_sz); +memcpy (addr, val, data_sz);return addr; }
[ ... ]
commit 417792b69014155cfa0ecc10cdffc4c2e54b9e60
Author: Leon Hwang <[email protected]>
Quoted 8 lines
bpftool: Compute map size of light skeletons at runtime
bpftool rounds memory-mapped data map sizes to the host page size when generating a light skeleton. The generated code therefore uses a 64K mapping size when bpftool runs on a 64K-page host, even if the skeleton runs on a 4K-page target. The target rejects the oversized map
mmap(), causing failure of loading the light skeleton.
Quoted 6 lines
When try to run 64K-page selftests on 4K-page VM, the error message does not provide the reason about page size.
test_atomics:PASS:atomics skeleton open 0 nsec test_atomics:FAIL:atomics skeleton load unexpected error: -12 (errno 22)
#15 atomics:FAIL
This isn't a bug, but could this sentence be reworded ("When running 64K-page selftests on a 4K-page VM ...") or dropped, since the patch does not change the error message itself? The sentence is missing a subject and describes a diagnostics shortcoming that this patch does not address.
---
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/34994524877
Quoted 36 lines · On 2026/9/16 00:28, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] bpf: compilation failure inskel_internal.hdue to non-standard roundup macro --commit
cf9410def9715a95832e67f7002ebd4dabf2d7ad
Author: Leon Hwang <[email protected]>bpftool: Compute map size of light skeletons at runtime
This commit addresses an issue with generating light skeletons on a host with a different page size from the target. It accomplishes this by passing the original map dimensions and dynamically computing the mmap size at runtime.
Quoted 4 lines
tools/lib/bpf/skel_internal.h +0
-0[ ... ]
tools/lib/bpf/skel_internal.h static inline void skel_free(void *p) · line 206
Quoted 13 lines
tools/lib/bpf/skel_internal.h +6
-2@@ -193,25 +197,39 @@ static inline void skel_free(void *p)free (p); } -static inline void skel_free_map_data (void *p,__u64 addr,size_t sz) +static inline size_t skel_map_mmap_sz (size_t val_sz,__u32 max_entries) { -munmap (p, sz); +const long page_sz =sysconf (_SC_PAGE_SIZE); +size_t mmap_sz; + + mmap_sz =roundup (val_sz,8 ) * max_entries; + mmap_sz =roundup (mmap_sz, page_sz);[Severity: Low]
Does this code rely on the non-standard roundup macro from <sys/param.h>?
Quoted 4 lines
When users include the generated light skeleton header in a project compiled with strict C standards (e.g., -std=c99 or -std=c11) without
_DEFAULT_SOURCEor_GNU_SOURCE, the roundup macro might not be defined. This can cause a compilation error due to an implicit declaration of function.
False positive.
By looking into the source code of <sys/param.h>, there's no _DEFAULT_SOURCE or _GNU_SOURCE guard for the roundup macro.
Thanks,
Leon
2026-09-16 00:14 UTC+0800 ~ Leon Hwang <[email protected]>
Quoted 13 lines
bpftool rounds memory-mapped data map sizes to the host page size when generating a light skeleton. The generated code therefore uses a 64K mapping size when bpftool runs on a 64K-page host, even if the skeleton runs on a 4K-page target. The target rejects the oversized map
mmap(), causing failure of loading the light skeleton.When try to run 64K-page selftests on 4K-page VM, the error message does not provide the reason about page size.
test_atomics:PASS:atomics skeleton open 0 nsec test_atomics:FAIL:atomics skeleton load unexpected error: -12 (errno 22)
#15 atomics:FAIL
Quoted 7 lines
Pass the original map value size and max entries to the generated code and round mmap size to the runtime page size in the user-space light skeleton helpers. This keeps generated light skeletons independent of the build host page size.
Fixes d510296d331a ("bpftool: Use syscall/loader program in "prog load" and "gen skeleton" command.")Signed-off-by Leon Hwang <[email protected]>
Looks good as far as I can tell, thank you!
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 10 lines · On Wed, 16 Sep 2026 00:14:50 +0800 you wrote:
bpftool rounds memory-mapped data map sizes to the host page size when generating a light skeleton. The generated code therefore uses a 64K mapping size when bpftool runs on a 64K-page host, even if the skeleton runs on a 4K-page target. The target rejects the oversized map
mmap(), causing failure of loading the light skeleton.When try to run 64K-page selftests on 4K-page VM, the error message does not provide the reason about page size.
[...]
Here is the summary with links:
- [bpf-next,v3] bpftool: Compute map size of light skeletons at runtime
https://git.kernel.org/bpf/bpf-next/c/a885470df10b
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html