libbpf: Add gating for arena globals relocation feature
Emil Tsalapatis
reviewerPATCH v3 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
---------
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)
--- tools/lib/bpf/features.c | 92 +++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.c | 7 ++- tools/lib/bpf/libbpf_internal.h | 2 + 3 files changed, 99 insertions(+), 2 deletions(-)
tools/lib/bpf/features.c +92 -0
@@ -506,6 +506,95 @@ 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) +{ +const size_t bufsz =1024 ; +int btf_fd, prog_fd, map; +char log_buf[bufsz]; +int ret; +static const char strs[] ="\0f\0" ; +const __u32 types[] = { +/* [1] FUNC_PROTO `void(void)` */ +BTF_TYPE_ENC (0 ,BTF_INFO_ENC (BTF_KIND_FUNC_PROTO ,0 ,0 ),0 ), +/* [2] FUNC 'f' -> FUNC_PROTO (main prog) */ +BTF_TYPE_ENC (1 /* "f" */ ,BTF_INFO_ENC (BTF_KIND_FUNC ,0 ,BTF_FUNC_GLOBAL ),1 ), + }; +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 = BPF_F_SLEEPABLE | (token_fd ? BPF_F_TOKEN_FD : 0), + .log_buf = log_buf, + .log_size = bufsz, + ); +struct bpf_insn insns[] = { +BPF_LD_MAP_VALUE (BPF_REG_1 ,0 ,1UL <<30 ), +BPF_EXIT_INSN (), + }; +const struct bpf_func_info_min func_infos[] = { + {0 ,2 },/* main prog -> FUNC 'f' */ + }; +int insn_cnt =ARRAY_SIZE (insns); + + btf_fd =libbpf__load_raw_btf ((char *)types,sizeof (types), strs,sizeof (strs), token_fd); +if (btf_fd <0 ) +return 0 ;/* BTF not supported at all */ + + map =bpf_map_create (BPF_MAP_TYPE_ARRAY ,"arr" ,sizeof (int ),1 ,1 , &map_opts); +if (map <0 ) { + ret = -errno; +pr_warn ("Error in %s(): %s. Couldn't create simple array map.\n" , + __func__,errstr (ret)); +close (btf_fd); +return ret; + } + insns[0 ].imm = map; + + prog_opts.prog_btf_fd = btf_fd; + prog_opts.func_info = &func_infos; + prog_opts.func_info_cnt =ARRAY_SIZE (func_infos); + prog_opts.func_info_rec_size =sizeof (func_infos[0 ]); + + prog_fd =bpf_prog_load (BPF_PROG_TYPE_SYSCALL ,"global_reloc" ,"GPL" , insns, insn_cnt, &prog_opts); + + ret = -errno; +close (map); +close (btf_fd); + +if (prog_fd >=0 ) { +pr_warn ("Error in %s(): Program loading unexpectedly succeeded.\n" , + __func__); +close (prog_fd); +return -EINVAL ; + } + +/* Be conservative and NULL terminate the buffer to ensure strstr terminates. */ + log_buf[bufsz -1 ] ='\0' ; + +/* +* Feature is allowed if we're not failing with the error message "direct value +* offset of %u is not allowed that was removed in 12a1fe6e12db +* ("bpf/verifier: Do not limit maximum direct offset into arena map"). +* Instead, we should be failing with the message "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" ) ==NULL ) { +pr_warn ("Error in %s(): Program unexpectedly failed with message: %s.\n" , + __func__, log_buf); +return ret; + } + +/* Feature is on. */ + +return 1 ; +} +typedef int (*feature_probe_fn )(int /* token_fd */ );static struct kern_feature_cache feature_cache;
@@ -581,6 +670,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
@@ -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
@@ -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
Quoted 36 lines · Emil Tsalapatis
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
---------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")Signed-off-by Emil Tsalapatis <[email protected]>--- tools/lib/bpf/features.c | 92 +++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.c | 7 ++- tools/lib/bpf/libbpf_internal.h | 2 + 3 files changed, 99 insertions(+), 2 deletions(-)tools/lib/bpf/features.c +0
-0
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 511
Quoted 6 lines
tools/lib/bpf/features.c +3
-0@@ -506,6 +506,95 @@ 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) +{ +const size_t bufsz =1024 ;
buf_sz, but really just
char log_buf[1024]; and use sizeof(log_buf) below
Quoted 7 lines
tools/lib/bpf/features.c +7
-0+int btf_fd, prog_fd, map; +char log_buf[bufsz]; +int ret; +static const char strs[] ="\0f\0" ; +const __u32 types[] = { +/* [1] FUNC_PROTO `void(void)` */ +BTF_TYPE_ENC (0 ,BTF_INFO_ENC (BTF_KIND_FUNC_PROTO ,0 ,0 ),0 ),
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 528
Quoted 10 lines
tools/lib/bpf/features.c +10
-0+/* [2] FUNC 'f' -> FUNC_PROTO (main prog) */ +BTF_TYPE_ENC (1 /* "f" */ ,BTF_INFO_ENC (BTF_KIND_FUNC ,0 ,BTF_FUNC_GLOBAL ),1 ), + }; +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 = BPF_F_SLEEPABLE | (token_fd ? BPF_F_TOKEN_FD : 0),
SLEEPABLE just for SYSCALL program? let's use TRACEPOINT and keep it simple
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 538
Quoted 10 lines
tools/lib/bpf/features.c +10
-0+ .log_buf = log_buf, + .log_size = bufsz, + ); +struct bpf_insn insns[] = { +BPF_LD_MAP_VALUE (BPF_REG_1 ,0 ,1UL <<30 ), +BPF_EXIT_INSN (), + }; +const struct bpf_func_info_min func_infos[] = { + {0 ,2 },/* main prog -> FUNC 'f' */ + };
unnecesasry
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 544
Quoted 6 lines
tools/lib/bpf/features.c +6
-0+int insn_cnt =ARRAY_SIZE (insns); + + btf_fd =libbpf__load_raw_btf ((char *)types,sizeof (types), strs,sizeof (strs), token_fd); +if (btf_fd <0 ) +return 0 ;/* BTF not supported at all */ +
Do we need BTF at all? Have you tried this without it?
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 545
Quoted 1 line
tools/lib/bpf/features.c +1
-0+ map =bpf_map_create (BPF_MAP_TYPE_ARRAY ,"arr" ,sizeof (int ),1 ,1 , &map_opts);
nit: map_fd for consistency with prog_fd
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 558
Quoted 13 lines
tools/lib/bpf/features.c +13
-0+if (map <0 ) { + ret = -errno; +pr_warn ("Error in %s(): %s. Couldn't create simple array map.\n" , + __func__,errstr (ret)); +close (btf_fd); +return ret; + } + insns[0 ].imm = map; + + prog_opts.prog_btf_fd = btf_fd; + prog_opts.func_info = &func_infos; + prog_opts.func_info_cnt =ARRAY_SIZE (func_infos); + prog_opts.func_info_rec_size =sizeof (func_infos[0 ]);
unnecessary
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 561
Quoted 3 lines
tools/lib/bpf/features.c +3
-0+ + prog_fd =bpf_prog_load (BPF_PROG_TYPE_SYSCALL ,"global_reloc" ,"GPL" , insns, insn_cnt, &prog_opts); +
let's use TRACEPOINT as one of the most basic program types
also "global_reloc" as a name doesn't match, really
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 574
Quoted 13 lines
tools/lib/bpf/features.c +13
-0+ ret = -errno; +close (map); +close (btf_fd); + +if (prog_fd >=0 ) { +pr_warn ("Error in %s(): Program loading unexpectedly succeeded.\n" , + __func__); +close (prog_fd); +return -EINVAL ; + } + +/* Be conservative and NULL terminate the buffer to ensure strstr terminates. */ + log_buf[bufsz -1 ] ='\0' ;
let's not be conservative, trust the verifier :)
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 578
Quoted 4 lines
tools/lib/bpf/features.c +4
-0+ +/* +* Feature is allowed if we're not failing with the error message "direct value +* offset of %u is not allowed that was removed in 12a1fe6e12db
missing quote after "is not allowed", also try to keep commit hash + subject on single line if possible (see my diff below, it looks pretty straightforward to achieve that)
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 587
Quoted 9 lines
tools/lib/bpf/features.c +9
-0+* ("bpf/verifier: Do not limit maximum direct offset into arena map"). +* Instead, we should be failing with the message "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" ) ==NULL ) {
!strstr()
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 594
Quoted 7 lines
tools/lib/bpf/features.c +7
-0+pr_warn ("Error in %s(): Program unexpectedly failed with message: %s.\n" , + __func__, log_buf); +return ret; + } + +/* Feature is on. */ +
unnecessary comment
tools/lib/bpf/features.c static struct kern_feature_desc { · line 674
Quoted 12 lines
tools/lib/bpf/features.c +5
-0+return 1 ; +} +typedef int (*feature_probe_fn )(int /* token_fd */ );static struct kern_feature_cache feature_cache;@@ -581,6 +670,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,
this description is used in the middle of sentence, it should start with lowercase (see other descriptions where the first word is not some abbreviation like BTF).
Quoted 27 lines
tools/lib/bpf/features.c +1
-0+ }, };bool feat_supported (struct kern_feature_cache *cache,enum kern_feature_id feat_id)tools/lib/bpf/libbpf.c +5
-2@@ -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 +0
-0
tools/lib/bpf/libbpf_internal.h enum kern_feature_id { · line 399
Quoted 8 lines
tools/lib/bpf/libbpf_internal.h +2
-0@@ -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, };
So overall this patch gets the job done, but I think we should tighten it up. I was curious why you have so much ceremony, so applied your patch locally and tried to strip everything I thought unnecessary (plus reformatted that big comment to be a bit more greppable). Here's what I got and it seems to do the job...
pw-bot: cr
tools/lib/bpf/features.c +2 -9
@@ -508,90 +508,64 @@ static int probe_kern_arg_ctx_tag(int token_fd)static int probe_ldimm64_full_range_off (int token_fd) { - const size_t bufsz = 1024; - int btf_fd, prog_fd, map; - char log_buf[bufsz]; +int prog_fd, map_fd; +char log_buf[1024 ];int ret; - static const char strs[] = "\0f\0"; - const __u32 types[] = { - /* [1] FUNC_PROTO `void(void)` */ - BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 0), 0), - /* [2] FUNC 'f' -> FUNC_PROTO (main prog) */ - BTF_TYPE_ENC(1 /* "f" */, BTF_INFO_ENC(BTF_KIND_FUNC,
0, BTF_FUNC_GLOBAL), 1),
- };
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 = BPF_F_SLEEPABLE | (token_fd ? BPF_F_TOKEN_FD : 0),
+ .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
.log_buf = log_buf,
- .log_size = bufsz,
+ .log_size = sizeof(log_buf),
);
struct bpf_insn insns[] = {
BPF_LD_MAP_VALUE(BPF_REG_1, 0, 1UL << 30),
BPF_EXIT_INSN(),
};
- const struct bpf_func_info_min func_infos[] = {
- { 0, 2 }, /* main prog -> FUNC 'f' */
- };
int insn_cnt = ARRAY_SIZE(insns);- btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types),
strs, sizeof(strs), token_fd);
- if (btf_fd < 0)
- return 0; /* BTF not supported at all */
-
- map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "arr", sizeof(int),
1, 1, &map_opts);
- if (map < 0) {
+ 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));
- close(btf_fd);
return ret;
}
- insns[0].imm = map;
-
- prog_opts.prog_btf_fd = btf_fd;
- prog_opts.func_info = &func_infos;
- prog_opts.func_info_cnt = ARRAY_SIZE(func_infos);
- prog_opts.func_info_rec_size = sizeof(func_infos[0]);-
- prog_fd = bpf_prog_load(BPF_PROG_TYPE_SYSCALL, "global_reloc",
"GPL", insns, insn_cnt, &prog_opts);
+ insns[0].imm = map_fd;
+ prog_fd = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, "ldimm64_off", "GPL",
+ insns, insn_cnt, &prog_opts);
ret = -errno;
- close(map);
- close(btf_fd);
+ close(map_fd); if (prog_fd >= 0) {- pr_warn("Error in %s(): Program loading unexpectedly
succeeded.\n",
- __func__);
+ pr_warn("Error in %s(): Program loading unexpectedly
succeeded.\n", __func__);
close(prog_fd);
return -EINVAL;
}- /* Be conservative and NULL terminate the buffer to ensure
strstr terminates. */
- log_buf[bufsz - 1] = '\0';
/*
- * Feature is allowed if we're not failing with the error
message "direct value
- * offset of %u is not allowed that was removed in 12a1fe6e12db
- * ("bpf/verifier: Do not limit maximum direct offset into arena map").
- * Instead, we should be failing with the message "invalid
access to map value
- * pointer". Ensure we match with one of the two and we're not
failing with a
+ * Feature is allowed if we're not failing with the error message
+ * "direct value offset of %u is not allowed" that was removed in
+ * 12a1fe6e12db ("bpf/verifier: Do not limit maximum directoffset into arena map").
+ * Instead, we should be failing with the message "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") == NULL) {
+ 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;
}- /* Feature is on. */
-
return 1;
}
Quoted 3 lines
--
2.49.0
Emil Tsalapatis
reviewerQuoted 223 lines · Andrii Nakryiko
Quoted 36 lines · Emil Tsalapatis
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
---------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")Signed-off-by Emil Tsalapatis <[email protected]>--- tools/lib/bpf/features.c | 92 +++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.c | 7 ++- tools/lib/bpf/libbpf_internal.h | 2 + 3 files changed, 99 insertions(+), 2 deletions(-)tools/lib/bpf/features.c +0
-0tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 511
Quoted 6 lines
tools/lib/bpf/features.c +3
-0@@ -506,6 +506,95 @@ 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) +{ +const size_t bufsz =1024 ;
buf_sz, but really justchar log_buf[1024]; and use
sizeof(log_buf)belowQuoted 7 lines
tools/lib/bpf/features.c +7
-0+int btf_fd, prog_fd, map; +char log_buf[bufsz]; +int ret; +static const char strs[] ="\0f\0" ; +const __u32 types[] = { +/* [1] FUNC_PROTO `void(void)` */ +BTF_TYPE_ENC (0 ,BTF_INFO_ENC (BTF_KIND_FUNC_PROTO ,0 ,0 ),0 ),tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 528
Quoted 10 lines
tools/lib/bpf/features.c +10
-0+/* [2] FUNC 'f' -> FUNC_PROTO (main prog) */ +BTF_TYPE_ENC (1 /* "f" */ ,BTF_INFO_ENC (BTF_KIND_FUNC ,0 ,BTF_FUNC_GLOBAL ),1 ), + }; +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 = BPF_F_SLEEPABLE | (token_fd ? BPF_F_TOKEN_FD : 0),SLEEPABLE just for SYSCALL program? let's use TRACEPOINT and keep it simple
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 538
Quoted 10 lines
tools/lib/bpf/features.c +10
-0+ .log_buf = log_buf, + .log_size = bufsz, + ); +struct bpf_insn insns[] = { +BPF_LD_MAP_VALUE (BPF_REG_1 ,0 ,1UL <<30 ), +BPF_EXIT_INSN (), + }; +const struct bpf_func_info_min func_infos[] = { + {0 ,2 },/* main prog -> FUNC 'f' */ + };unnecesasry
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 544
Quoted 6 lines
tools/lib/bpf/features.c +6
-0+int insn_cnt =ARRAY_SIZE (insns); + + btf_fd =libbpf__load_raw_btf ((char *)types,sizeof (types), strs,sizeof (strs), token_fd); +if (btf_fd <0 ) +return 0 ;/* BTF not supported at all */ +Do we need BTF at all? Have you tried this without it?
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 545
Quoted 1 line
tools/lib/bpf/features.c +1
-0+ map =bpf_map_create (BPF_MAP_TYPE_ARRAY ,"arr" ,sizeof (int ),1 ,1 , &map_opts);nit:
map_fdfor consistency withprog_fdtools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 558
Quoted 13 lines
tools/lib/bpf/features.c +13
-0+if (map <0 ) { + ret = -errno; +pr_warn ("Error in %s(): %s. Couldn't create simple array map.\n" , + __func__,errstr (ret)); +close (btf_fd); +return ret; + } + insns[0 ].imm = map; + + prog_opts.prog_btf_fd = btf_fd; + prog_opts.func_info = &func_infos; + prog_opts.func_info_cnt =ARRAY_SIZE (func_infos); + prog_opts.func_info_rec_size =sizeof (func_infos[0 ]);unnecessary
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 561
Quoted 3 lines
tools/lib/bpf/features.c +3
-0+ + prog_fd =bpf_prog_load (BPF_PROG_TYPE_SYSCALL ,"global_reloc" ,"GPL" , insns, insn_cnt, &prog_opts); +let's use TRACEPOINT as one of the most basic program types
also "
global_reloc" as a name doesn't match, reallytools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 574
Quoted 13 lines
tools/lib/bpf/features.c +13
-0+ ret = -errno; +close (map); +close (btf_fd); + +if (prog_fd >=0 ) { +pr_warn ("Error in %s(): Program loading unexpectedly succeeded.\n" , + __func__); +close (prog_fd); +return -EINVAL ; + } + +/* Be conservative and NULL terminate the buffer to ensure strstr terminates. */ + log_buf[bufsz -1 ] ='\0' ;let's not be conservative, trust the verifier :)
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 578
Quoted 4 lines
tools/lib/bpf/features.c +4
-0+ +/* +* Feature is allowed if we're not failing with the error message "direct value +* offset of %u is not allowed that was removed in 12a1fe6e12db missing quote after "is not allowed", also try to keep commit hash + subject on single line if possible (see my diff below, it looks pretty straightforward to achieve that)
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 587
Quoted 9 lines
tools/lib/bpf/features.c +9
-0+* ("bpf/verifier: Do not limit maximum direct offset into arena map"). +* Instead, we should be failing with the message "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" ) ==NULL ) {!strstr()
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 594
Quoted 7 lines
tools/lib/bpf/features.c +7
-0+pr_warn ("Error in %s(): Program unexpectedly failed with message: %s.\n" , + __func__, log_buf); +return ret; + } + +/* Feature is on. */ +unnecessary comment
tools/lib/bpf/features.c static struct kern_feature_desc { · line 674
Quoted 12 lines
tools/lib/bpf/features.c +5
-0+return 1 ; +} +typedef int (*feature_probe_fn )(int /* token_fd */ );static struct kern_feature_cache feature_cache;@@ -581,6 +670,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,this description is used in the middle of sentence, it should start with lowercase (see other descriptions where the first word is not some abbreviation like BTF).
Quoted 27 lines
tools/lib/bpf/features.c +1
-0+ }, };bool feat_supported (struct kern_feature_cache *cache,enum kern_feature_id feat_id)tools/lib/bpf/libbpf.c +5
-2@@ -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 +0
-0tools/lib/bpf/libbpf_internal.h enum kern_feature_id { · line 399
Quoted 8 lines
tools/lib/bpf/libbpf_internal.h +2
-0@@ -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, };So overall this patch gets the job done, but I think we should tighten it up. I was curious why you have so much ceremony, so applied your patch locally and tried to strip everything I thought unnecessary (plus reformatted that big comment to be a bit more greppable). Here's what I got and it seems to do the job...
Quoted 2 lines
pw-bot: cr
So to recap:
a) Remove BTF as it's not necessary
b) func_infos is also not needed
c) Use sizeof(log_buf) to avoid having a separate variable
d) Use a TRACEPOINT program to avoid the SLEEPABLE flag
Will update with those.
For two of the changes I have questions:
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 511
Quoted 13 lines
tools/lib/bpf/features.c +1
-3@@ -508,90 +508,64 @@ static int probe_kern_arg_ctx_tag(int token_fd)static int probe_ldimm64_full_range_off (int token_fd) { -const size_t bufsz =1024 ; -int btf_fd, prog_fd, map; -char log_buf[bufsz]; +int prog_fd, map_fd;
Should we rename here? Every test program in this file uses map for map. No strong opinions one way or the other.
Quoted 78 lines
tools/lib/bpf/features.c +10
-32+char log_buf[1024 ];int ret; -static const char strs[] ="\0f\0" ; -const __u32 types[] = { -/* [1] FUNC_PROTO `void(void)` */ -BTF_TYPE_ENC (0 ,BTF_INFO_ENC (BTF_KIND_FUNC_PROTO ,0 ,0 ),0 ), -/* [2] FUNC 'f' -> FUNC_PROTO (main prog) */ -BTF_TYPE_ENC (1 /* "f" */ ,BTF_INFO_ENC (BTF_KIND_FUNC , 0, BTF_FUNC_GLOBAL), 1), - };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 = BPF_F_SLEEPABLE | (token_fd ? BPF_F_TOKEN_FD : 0), + .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0, .log_buf = log_buf, - .log_size = bufsz, + .log_size = sizeof(log_buf), );struct bpf_insn insns[] = {BPF_LD_MAP_VALUE (BPF_REG_1 ,0 ,1UL <<30 ),BPF_EXIT_INSN (), }; -const struct bpf_func_info_min func_infos[] = { - {0 ,2 },/* main prog -> FUNC 'f' */ - };int insn_cnt =ARRAY_SIZE (insns); - btf_fd =libbpf__load_raw_btf ((char *)types,sizeof (types), strs, sizeof(strs), token_fd); -if (btf_fd <0 ) -return 0 ;/* BTF not supported at all */ - - map =bpf_map_create (BPF_MAP_TYPE_ARRAY ,"arr" ,sizeof (int ), 1, 1, &map_opts); -if (map <0 ) { + 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)); -close (btf_fd);return ret; } - insns[0 ].imm = map; - - prog_opts.prog_btf_fd = btf_fd; - prog_opts.func_info = &func_infos; - prog_opts.func_info_cnt =ARRAY_SIZE (func_infos); - prog_opts.func_info_rec_size =sizeof (func_infos[0 ]); - - prog_fd =bpf_prog_load (BPF_PROG_TYPE_SYSCALL ,"global_reloc" , "GPL", insns, insn_cnt, &prog_opts); + insns[0 ].imm = map_fd; + prog_fd =bpf_prog_load (BPF_PROG_TYPE_TRACEPOINT ,"ldimm64_off" ,"GPL" , + insns, insn_cnt, &prog_opts); ret = -errno; -close (map); -close (btf_fd); +close (map_fd);if (prog_fd >=0 ) { - pr_warn("Error in %s(): Program loading unexpectedly succeeded.\n", - __func__); + pr_warn("Error in %s(): Program loading unexpectedly succeeded.\n", __func__);close (prog_fd);return -EINVAL ; } -/* Be conservative and NULL terminate the buffer to ensure
Quoted 3 lines
strstr terminates. */
- log_buf[bufsz - 1] = '\0';
Should we remove this? If there is no guarantee the buffer has a NUL character somewhere, the strstr can fly off past the end of the array.
Quoted 37 lines
/*- * Feature is allowed if we're not failing with the error
message "direct valuetools/lib/bpf/features.c +0
-3-* offset of %u is not allowed that was removed in 12a1fe6e12db -* ("bpf/verifier: Do not limit maximum direct offset into arena map"). -* Instead, we should be failing with the message "invalid access to map value
- * pointer". Ensure we match with one of the two and we're not
failing with atools/lib/bpf/features.c +3
-0+* Feature is allowed if we're not failing with the error message +* "direct value offset of %u is not allowed" that was removed in +* 12a1fe6e12db ("bpf/verifier: Do not limit maximum direct offset into arena map").
+ * Instead, we should be failing with the message "invalid
access to map value pointer".tools/lib/bpf/features.c +2
-1+* 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" ) ==NULL ) { +if (!strstr (log_buf,"invalid access to map value pointer" )) { pr_warn("Error in %s(): Program unexpectedly failedwith message: %s.\n",
__func__, log_buf); return ret; }tools/lib/bpf/features.c +0
-2-/* Feature is on. */ -return 1 ; }Quoted 4 lines
--
2.49.0
Quoted 34 lines · Emil Tsalapatis
Quoted 32 lines · Andrii Nakryiko
Quoted 31 lines · Emil Tsalapatis
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
---------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")Signed-off-by Emil Tsalapatis <[email protected]>--- tools/lib/bpf/features.c | 92 +++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.c | 7 ++- tools/lib/bpf/libbpf_internal.h | 2 + 3 files changed, 99 insertions(+), 2 deletions(-)
[...]
Quoted 23 lines
Quoted 20 lines
tools/lib/bpf/libbpf_internal.h enum kern_feature_id { · line 399
Quoted 13 lines
tools/lib/bpf/libbpf_internal.h +2
-0@@ -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, };So overall this patch gets the job done, but I think we should tighten it up. I was curious why you have so much ceremony, so applied your patch locally and tried to strip everything I thought unnecessary (plus reformatted that big comment to be a bit more greppable). Here's what I got and it seems to do the job...
Quoted 2 lines
pw-bot: cr
Quoted 9 lines
So to recap:
a) Remove BTF as it's not necessary
b)func_infosis also not needed
c) Usesizeof(log_buf)to avoid having a separate variable
d) Use a TRACEPOINT program to avoid the SLEEPABLE flagWill update with those.
ack, thanks
Quoted 9 lines
For two of the changes I have questions:
Quoted 7 lines
tools/lib/bpf/features.c +0
-0@@ -508,90 +508,64 @@ static int probe_kern_arg_ctx_tag(int token_fd)
Quoted 9 lines
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 511
Quoted 6 lines
tools/lib/bpf/features.c +1
-3static int probe_ldimm64_full_range_off (int token_fd) { -const size_t bufsz =1024 ; -int btf_fd, prog_fd, map; -char log_buf[bufsz]; +int prog_fd, map_fd;Should we rename here? Every test program in this file uses map for map. No strong opinions one way or the other.
given we have prog_fd, yeah, let's use map_fd here as well
Quoted 8 lines
Quoted 8 lines
tools/lib/bpf/features.c +1
-5+char log_buf[1024 ];int ret; -static const char strs[] ="\0f\0" ; -const __u32 types[] = { -/* [1] FUNC_PROTO `void(void)` */ -BTF_TYPE_ENC (0 ,BTF_INFO_ENC (BTF_KIND_FUNC_PROTO ,0 ,0 ),0 ), -/* [2] FUNC 'f' -> FUNC_PROTO (main prog) */
[...]
Quoted 8 lines
Quoted 5 lines
- /* Be conservative and NULL terminate the buffer to ensure
strstr terminates. */
- log_buf[bufsz - 1] = '\0';Should we remove this? If there is no guarantee the buffer has a NUL character somewhere, the strstr can fly off past the end of the array.
verifier log on error will be properly zero terminated, that's part of verifier contract, we shouldn't doubt it
[...]
Emil Tsalapatis
reviewerQuoted 92 lines · Andrii Nakryiko
Quoted 34 lines · Emil Tsalapatis
Quoted 32 lines · Andrii Nakryiko
Quoted 31 lines · Emil Tsalapatis
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
---------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")Signed-off-by Emil Tsalapatis <[email protected]>--- tools/lib/bpf/features.c | 92 +++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.c | 7 ++- tools/lib/bpf/libbpf_internal.h | 2 + 3 files changed, 99 insertions(+), 2 deletions(-)[...]
Quoted 23 lines
Quoted 20 lines
tools/lib/bpf/libbpf_internal.h enum kern_feature_id { · line 399
Quoted 13 lines
tools/lib/bpf/libbpf_internal.h +2
-0@@ -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, };So overall this patch gets the job done, but I think we should tighten it up. I was curious why you have so much ceremony, so applied your patch locally and tried to strip everything I thought unnecessary (plus reformatted that big comment to be a bit more greppable). Here's what I got and it seems to do the job...
Quoted 2 lines
pw-bot: cr
Quoted 9 lines
So to recap:
a) Remove BTF as it's not necessary
b)func_infosis also not needed
c) Usesizeof(log_buf)to avoid having a separate variable
d) Use a TRACEPOINT program to avoid the SLEEPABLE flagWill update with those.
ack, thanks
Quoted 9 lines
For two of the changes I have questions:
Quoted 7 lines
tools/lib/bpf/features.c +0
-0@@ -508,90 +508,64 @@ static int probe_kern_arg_ctx_tag(int token_fd)Quoted 9 lines
tools/lib/bpf/features.c static int probe_kern_arg_ctx_tag(int token_fd) · line 511
Quoted 6 lines
tools/lib/bpf/features.c +1
-3static int probe_ldimm64_full_range_off (int token_fd) { -const size_t bufsz =1024 ; -int btf_fd, prog_fd, map; -char log_buf[bufsz]; +int prog_fd, map_fd;Should we rename here? Every test program in this file uses map for map. No strong opinions one way or the other.
Quoted 2 lines
given we have
prog_fd, yeah, let's usemap_fdhere as well
Ack, will do.
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
tools/lib/bpf/features.c +1
-5+char log_buf[1024 ];int ret; -static const char strs[] ="\0f\0" ; -const __u32 types[] = { -/* [1] FUNC_PROTO `void(void)` */ -BTF_TYPE_ENC (0 ,BTF_INFO_ENC (BTF_KIND_FUNC_PROTO ,0 ,0 ),0 ), -/* [2] FUNC 'f' -> FUNC_PROTO (main prog) */ [...]
Quoted 6 lines
Quoted 5 lines
- /* Be conservative and NULL terminate the buffer to ensure
strstr terminates. */
- log_buf[bufsz - 1] = '\0';
Quoted 6 lines
Quoted 2 lines
Should we remove this? If there is no guarantee the buffer has a NUL character somewhere, the strstr can fly off past the end of the array.
verifier log on error will be properly zero terminated, that's part of verifier contract, we shouldn't doubt it
TIL, thanks!
Quoted 1 line
[...]