bpf: BTF support for ksyms
Subsystems (10)
v3 cover bpf: BTF support for ksyms
v2 -> v3:
- Rename functions and variables in verifier for better readability.
- Stick to logging message convention in libbpf.
- Move bpf_per_cpu_ptr and bpf_this_cpu_ptr from trace-specific helper set to base helper set.
- More specific test in ksyms_btf.
- Fix return type cast in bpf_*_cpu_ptr.
- Fix btf leak in ksyms_btf selftest.
- Fix return error code for kallsyms_find().
v1 -> v2:
- Move check_pseudo_btf_id from check_ld_imm() to replace_map_fd_with_map_ptr() and rename the latter.
- Add bpf_this_cpu_ptr().
- Use bpf_core_types_are_compat() in libbpf.c for checking type compatibility.
- Rewrite typed ksym extern type in BTF with int to save space.
- Minor revision of bpf_per_cpu_ptr()'s comments.
- Avoid using long in tests that use skeleton.
- Refactored test_ksyms.c by moving kallsyms_find() to trace_helpers.c
- Fold the patches that sync include/linux/uapi and tools/include/linux/uapi.
rfc -> v1:
- Encode VAR's btf_id for PSEUDO_BTF_ID.
- More checks in verifier. Checking the btf_id passed as PSEUDO_BTF_ID is valid VAR, its name and type.
- Checks in libbpf on type compatibility of ksyms.
- Add bpf_per_cpu_ptr() to access kernel percpu vars. Introduced new ARG and RET types for this helper.
This patch series extends the previously added __ksym externs with btf support.
Right now the __ksym externs are treated as pure 64-bit scalar value. Libbpf replaces ld_imm64 insn of __ksym by its kernel address at load time. This patch series extend those externs with their btf info. Note that btf support for __ksym must come with the kernel btf that has VARs encoded to work properly. The corresponding chagnes in pahole is available at [1] (with a fix at [2] for gcc 4.9+).
The first 3 patches in this series add support for general kernel global variables, which include verifier checking (01/06), libpf support (02/06) and selftests for getting typed ksym extern's kernel address (03/06).
The next 3 patches extends that capability further by introducing helpers bpf_per_cpu_ptr() and bpf_this_cpu_ptr(), which allows accessing kernel percpu variables correctly (04/06 and 05/06).
The tests of this feature were performed against pahole that is extended with [1] and [2]. For kernel BTF that does not have VARs encoded, the selftests will be skipped.
[1] https://git.kernel.org/pub/scm/devel/pahole/pahole.git/commit/?id=f3d9054ba8ff1df0fc44e507e3a01c0964cabd42
[2] https://www.spinics.net/lists/dwarves/msg00451.html
Hao Luo (6):
bpf: Introduce pseudo_btf_id
bpf/libbpf: BTF support for typed ksyms selftests/bpf: ksyms_btf to test typed ksyms
bpf: Introduce bpf_per_cpu_ptr()
bpf: Introduce bpf_this_cpu_ptr()
bpf/selftests: Test for bpf_per_cpu_ptr() and bpf_this_cpu_ptr()
include/linux/bpf.h | 6 + include/linux/bpf_verifier.h | 7 + include/linux/btf.h | 26 +++ include/uapi/linux/bpf.h | 67 +++++- kernel/bpf/btf.c | 25 --- kernel/bpf/helpers.c | 32 +++ kernel/bpf/verifier.c | 190 ++++++++++++++++-- kernel/trace/bpf_trace.c | 4 + tools/include/uapi/linux/bpf.h | 67 +++++- tools/lib/bpf/libbpf.c | 112 +++++++++-- .../testing/selftests/bpf/prog_tests/ksyms.c | 38 ++-- .../selftests/bpf/prog_tests/ksyms_btf.c | 88 ++++++++ .../selftests/bpf/progs/test_ksyms_btf.c | 55 +++++ tools/testing/selftests/bpf/trace_helpers.c | 27 +++ tools/testing/selftests/bpf/trace_helpers.h | 4 + 15 files changed, 653 insertions(+), 95 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/ksyms_btf.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_btf.c
Signature · 1 line
2.28.0.618.gf4bc123cb7-goog
PATCH v3 1/6 bpf: Introduce pseudo_btf_id
Pseudo_btf_id is a type of ld_imm insn that associates a btf_id to a ksym so that further dereferences on the ksym can use the BTF info to validate accesses. Internally, when seeing a pseudo_btf_id ld insn, the verifier reads the btf_id stored in the insn[0]'s imm field and marks the dst_reg as PTR_TO_BTF_ID. The btf_id points to a VAR_KIND, which is encoded in btf_vminux by pahole. If the VAR is not of a struct type, the dst reg will be marked as PTR_TO_MEM instead of PTR_TO_BTF_ID and the mem_size is resolved to the size of the VAR's type.
From the VAR btf_id, the verifier can also read the address of the ksym's corresponding kernel var from kallsyms and use that to fill dst_reg.
Therefore, the proper functionality of pseudo_btf_id depends on (1) kallsyms and (2) the encoding of kernel global VARs in pahole, which should be available since pahole v1.18.
--- include/linux/bpf_verifier.h | 7 ++ include/linux/btf.h | 15 ++++ include/uapi/linux/bpf.h | 36 +++++++--- kernel/bpf/btf.c | 15 ---- kernel/bpf/verifier.c | 125 +++++++++++++++++++++++++++++---- tools/include/uapi/linux/bpf.h | 36 +++++++--- 6 files changed, 188 insertions(+), 46 deletions(-)
include/linux/bpf_verifier.h +7 -0
@@ -308,6 +308,13 @@ struct bpf_insn_aux_data {u32 map_index;/* index into used_maps[] */ u32 map_off;/* offset from value base address */ }; +struct { +u32 reg_type;/* type of pseudo_btf_id */ +union { +u32 btf_id;/* btf_id for struct typed var */ +u32 mem_size;/* mem_size for non-struct typed var */ + }; + } btf_var; };u64 map_key_state;/* constant (32 bit) key tracking for maps */ int ctx_field_size;/* the ctx field size for load insn, maybe 0 */
include/linux/btf.h +15 -0
@@ -106,6 +106,21 @@ static inline bool btf_type_is_func_proto(const struct btf_type *t)return BTF_INFO_KIND (t->info) ==BTF_KIND_FUNC_PROTO ; } +static inline bool btf_type_is_var (const struct btf_type *t) +{ +return BTF_INFO_KIND (t->info) ==BTF_KIND_VAR ; +} + +/* union is only a special case of struct: +* all its offsetof(member) == 0 +*/ +static inline bool btf_type_is_struct (const struct btf_type *t) +{ +u8 kind =BTF_INFO_KIND (t->info); + +return kind ==BTF_KIND_STRUCT || kind ==BTF_KIND_UNION ; +} +static inline u16 btf_type_vlen (const struct btf_type *t) {return BTF_INFO_VLEN (t->info);
include/uapi/linux/bpf.h +27 -9
@@ -356,18 +356,36 @@ enum bpf_link_type {#define BPF_F_SLEEPABLE (1U << 4)/* When BPF ldimm64's insn[0].src_reg != 0 then this can have -* two extensions: -* -* insn[0].src_reg: BPF_PSEUDO_MAP_FD BPF_PSEUDO_MAP_VALUE -* insn[0].imm: map fd map fd -* insn[1].imm: 0 offset into value -* insn[0].off: 0 0 -* insn[1].off: 0 0 -* ldimm64 rewrite: address of map address of map[0]+offset -* verifier type: CONST_PTR_TO_MAP PTR_TO_MAP_VALUE +* the following extensions: +* +* insn[0].src_reg: BPF_PSEUDO_MAP_FD +* insn[0].imm: map fd +* insn[1].imm: 0 +* insn[0].off: 0 +* insn[1].off: 0 +* ldimm64 rewrite: address of map +* verifier type: CONST_PTR_TO_MAP */ #define BPF_PSEUDO_MAP_FD 1 +/* insn[0].src_reg: BPF_PSEUDO_MAP_VALUE +* insn[0].imm: map fd +* insn[1].imm: offset into value +* insn[0].off: 0 +* insn[1].off: 0 +* ldimm64 rewrite: address of map[0]+offset +* verifier type: PTR_TO_MAP_VALUE +*/ #define BPF_PSEUDO_MAP_VALUE 2 +/* insn[0].src_reg: BPF_PSEUDO_BTF_ID +* insn[0].imm: kernel btd id of VAR +* insn[1].imm: 0 +* insn[0].off: 0 +* insn[1].off: 0 +* ldimm64 rewrite: address of the kernel variable +* verifier type: PTR_TO_BTF_ID or PTR_TO_MEM, depending on whether the var +* is struct/union. +*/ +#define BPF_PSEUDO_BTF_ID 3/* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative * offset to another bpf function
kernel/bpf/btf.c +0 -15
@@ -355,16 +355,6 @@ static bool btf_type_nosize_or_null(const struct btf_type *t)return !t ||btf_type_nosize (t); } -/* union is only a special case of struct: -* all its offsetof(member) == 0 -*/ -static bool btf_type_is_struct (const struct btf_type *t) -{ -u8 kind =BTF_INFO_KIND (t->info); - -return kind ==BTF_KIND_STRUCT || kind ==BTF_KIND_UNION ; -} -static bool __btf_type_is_struct (const struct btf_type *t) {return BTF_INFO_KIND (t->info) ==BTF_KIND_STRUCT ;
@@ -375,11 +365,6 @@ static bool btf_type_is_array(const struct btf_type *t)return BTF_INFO_KIND (t->info) ==BTF_KIND_ARRAY ; } -static bool btf_type_is_var (const struct btf_type *t) -{ -return BTF_INFO_KIND (t->info) ==BTF_KIND_VAR ; -} -static bool btf_type_is_datasec (const struct btf_type *t) {return BTF_INFO_KIND (t->info) ==BTF_KIND_DATASEC ;
kernel/bpf/verifier.c +112 -13
181 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +27 -9
@@ -356,18 +356,36 @@ enum bpf_link_type {#define BPF_F_SLEEPABLE (1U << 4)/* When BPF ldimm64's insn[0].src_reg != 0 then this can have -* two extensions: -* -* insn[0].src_reg: BPF_PSEUDO_MAP_FD BPF_PSEUDO_MAP_VALUE -* insn[0].imm: map fd map fd -* insn[1].imm: 0 offset into value -* insn[0].off: 0 0 -* insn[1].off: 0 0 -* ldimm64 rewrite: address of map address of map[0]+offset -* verifier type: CONST_PTR_TO_MAP PTR_TO_MAP_VALUE +* the following extensions: +* +* insn[0].src_reg: BPF_PSEUDO_MAP_FD +* insn[0].imm: map fd +* insn[1].imm: 0 +* insn[0].off: 0 +* insn[1].off: 0 +* ldimm64 rewrite: address of map +* verifier type: CONST_PTR_TO_MAP */ #define BPF_PSEUDO_MAP_FD 1 +/* insn[0].src_reg: BPF_PSEUDO_MAP_VALUE +* insn[0].imm: map fd +* insn[1].imm: offset into value +* insn[0].off: 0 +* insn[1].off: 0 +* ldimm64 rewrite: address of map[0]+offset +* verifier type: PTR_TO_MAP_VALUE +*/ #define BPF_PSEUDO_MAP_VALUE 2 +/* insn[0].src_reg: BPF_PSEUDO_BTF_ID +* insn[0].imm: kernel btd id of VAR +* insn[1].imm: 0 +* insn[0].off: 0 +* insn[1].off: 0 +* ldimm64 rewrite: address of the kernel variable +* verifier type: PTR_TO_BTF_ID or PTR_TO_MEM, depending on whether the var +* is struct/union. +*/ +#define BPF_PSEUDO_BTF_ID 3/* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative * offset to another bpf function
Signature · 1 line
2.28.0.618.gf4bc123cb7-goog
Re: [PATCH bpf-next v3 1/6] bpf: Introduce pseudo_btf_id
Quoted 14 lines · Hao Luo
Pseudo_btf_idis a type ofld_imminsn that associates abtf_idto a ksym so that further dereferences on the ksym can use the BTF info to validate accesses. Internally, when seeing apseudo_btf_idld insn, the verifier reads thebtf_idstored in the insn[0]'s imm field and marks thedst_regasPTR_TO_BTF_ID. Thebtf_idpoints to aVAR_KIND, which is encoded inbtf_vminuxby pahole. If the VAR is not of a struct type, the dst reg will be marked asPTR_TO_MEMinstead ofPTR_TO_BTF_IDand themem_sizeis resolved to the size of the VAR's type.From the VAR
btf_id, the verifier can also read the address of the ksym's corresponding kernel var from kallsyms and use that to filldst_reg.
Quoted 6 lines
Therefore, the proper functionality of
pseudo_btf_iddepends on (1) kallsyms and (2) the encoding of kernel global VARs in pahole, which should be available since pahole v1.18.Signed-off-by Hao Luo <[email protected]>---
Looks good, few minor nits if you are going to post another version anyways. Assuming BPF offload change I mentioned is ok:
Quoted 13 lines
include/linux/bpf_verifier.h | 7 ++ include/linux/btf.h | 15 ++++ include/uapi/linux/bpf.h | 36 +++++++--- kernel/bpf/btf.c | 15 ---- kernel/bpf/verifier.c | 125 +++++++++++++++++++++++++++++---- tools/include/uapi/linux/bpf.h | 36 +++++++--- 6 files changed, 188 insertions(+), 46 deletions(-)include/linux/bpf_verifier.h +0
-0
include/linux/bpf_verifier.h struct bpf_insn_aux_data { · line 312
Quoted 5 lines
include/linux/bpf_verifier.h +2
-0@@ -308,6 +308,13 @@ struct bpf_insn_aux_data {u32 map_index;/* index into used_maps[] */ u32 map_off;/* offset from value base address */ }; +struct { +u32 reg_type;/* type of pseudo_btf_id */
nit: there is an explicit enum for this: enum bpf_reg_type, which matches struct bpf_reg_state's type field as well.
Quoted 8 lines
include/linux/bpf_verifier.h +5
-0+union { +u32 btf_id;/* btf_id for struct typed var */ +u32 mem_size;/* mem_size for non-struct typed var */ + }; + } btf_var; };u64 map_key_state;/* constant (32 bit) key tracking for maps */ int ctx_field_size;/* the ctx field size for load insn, maybe 0 */
[...]
Quoted 15 lines
include/linux/bpf_verifier.h +15
-0+/* replace pseudo btf_id with kernel symbol address */ +static int check_pseudo_btf_id (struct bpf_verifier_env *env, +struct bpf_insn *insn, +struct bpf_insn_aux_data *aux) +{ +u32 type, id = insn->imm; +const struct btf_type *t; +const char *sym_name; +u64 addr; + +if (!btf_vmlinux) { +verbose (env,"kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n" ); +return -EINVAL ; + } +
include/linux/bpf_verifier.h
Quoted 10 lines
include/linux/bpf_verifier.h +10
-0+ t =btf_type_by_id (btf_vmlinux, id); +if (!t) { +verbose (env,"ldimm64 insn specifies invalid btf_id %d.\n" , id); +return -ENOENT ; + } + +if (insn[1 ].imm !=0 ) { +verbose (env,"reserved field (insn[1].imm) is used in pseudo_btf_id ldimm64 insn.\n" ); +return -EINVAL ; + }
nit: I'd do this check first, before you look up type and check all other type-related conditions
Quoted 18 lines
include/linux/bpf_verifier.h +18
-0+ +if (!btf_type_is_var (t)) { +verbose (env,"pseudo btf_id %d in ldimm64 isn't KIND_VAR.\n" , + id); +return -EINVAL ; + } + + sym_name =btf_name_by_offset (btf_vmlinux, t->name_off); + addr =kallsyms_lookup_name (sym_name); +if (!addr) { +verbose (env,"ldimm64 failed to find the address for kernel symbol '%s'.\n" , + sym_name); +return -ENOENT ; + } + + insn[0 ].imm = (u32 )addr; + insn[1 ].imm = addr >>32 ; +
[...]
Quoted 16 lines
include/linux/bpf_verifier.h +8
-0@@ -9442,6 +9533,14 @@ static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)/* valid generic load 64-bit imm */ goto next_insn; +if (insn[0 ].src_reg ==BPF_PSEUDO_BTF_ID ) { + aux = &env->insn_aux_data[i]; + err =check_pseudo_btf_id (env, insn, aux); +if (err) +return err; + goto next_insn; + } +/* In final convert_pseudo_ld_imm64() step, this is * converted into regular 64-bit imm load insn. */
include/linux/bpf_verifier.h int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, · line 11397
Quoted 6 lines
include/linux/bpf_verifier.h +0
-3@@ -11392,10 +11491,6 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,if (is_priv) env->test_state_freq = attr->prog_flags &BPF_F_TEST_STATE_FREQ ; - ret =replace_map_fd_with_map_ptr (env); -if (ret <0 ) - goto skip_full_check;
I'm not familiar with BPF offload stuff, so just flagging a change
here: previously offloaded BPF programs, when passed to offload's verifier prep routine would already have ldimm64 processes, now they won't. This might not be an issue and not an expectation we want, but it would be nice if someone who knows something about offload stuff confirms that this is ok.
Quoted 4 lines
include/linux/bpf_verifier.h +0
-1-if (bpf_prog_is_dev_bound (env->prog->aux)) { ret =bpf_prog_offload_verifier_prep (env->prog);if (ret)
[...]
PATCH v3 2/6 bpf/libbpf: BTF support for typed ksyms
In reply to Hao LuoIf a ksym is defined with a type, libbpf will try to find the ksym's btf information from kernel btf. If a valid btf entry for the ksym is found, libbpf can pass in the found btf id to the verifier, which validates the ksym's type and value.
Typeless ksyms (i.e. those defined as 'void') will not have such btf_id, but it has the symbol's address (read from kallsyms) and its value is treated as a raw pointer.
--- tools/lib/bpf/libbpf.c | 112 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 99 insertions(+), 13 deletions(-)
tools/lib/bpf/libbpf.c +99 -13
@@ -390,6 +390,12 @@ struct extern_desc { } kcfg;struct {unsigned long long addr; + +/* target btf_id of the corresponding kernel var. */ +int vmlinux_btf_id; + +/* local btf_id of the ksym extern's type. */ +__u32 type_id; } ksym; }; };
@@ -2522,12 +2528,23 @@ static int bpf_object__load_vmlinux_btf(struct bpf_object *obj) {bool need_vmlinux_btf = false;struct bpf_program *prog; -int err; +int i, err;/* CO-RE relocations need kernel BTF */ if (obj->btf_ext && obj->btf_ext->core_relo_info.len) need_vmlinux_btf = true; +/* Support for typed ksyms needs kernel BTF */ +for (i =0 ; i < obj->nr_extern; i++) { +const struct extern_desc *ext; + + ext = &obj->externs[i]; +if (ext->type ==EXT_KSYM && ext->ksym.type_id) { + need_vmlinux_btf = true; +break ; + } + } +bpf_object__for_each_program (prog, obj) {if (!prog->load)continue ;
@@ -3156,16 +3173,10 @@ static int bpf_object__collect_externs(struct bpf_object *obj)return -ENOTSUP ; } }else if (strcmp (sec_name,KSYMS_SEC ) ==0 ) { -const struct btf_type *vt; - ksym_sec = sec; ext->type =EXT_KSYM ; - - vt =skip_mods_and_typedefs (obj->btf, t->type,NULL ); -if (!btf_is_void (vt)) { -pr_warn ("extern (ksym) '%s' is not typeless (void)\n" , ext_name); -return -ENOTSUP ; - } +skip_mods_and_typedefs (obj->btf, t->type, + &ext->ksym.type_id); }else {pr_warn ("unrecognized extern section '%s'\n" , sec_name);return -ENOTSUP ;
@@ -5800,8 +5811,13 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) insn[0 ].imm = obj->maps[obj->kconfig_map_idx].fd; insn[1 ].imm = ext->kcfg.data_off; }else /* EXT_KSYM */ { - insn[0 ].imm = (__u32 )ext->ksym.addr; - insn[1 ].imm = ext->ksym.addr >>32 ; +if (ext->ksym.type_id) {/* typed ksyms */ + insn[0 ].src_reg =BPF_PSEUDO_BTF_ID ; + insn[0 ].imm = ext->ksym.vmlinux_btf_id; + }else {/* typeless ksyms */ + insn[0 ].imm = (__u32 )ext->ksym.addr; + insn[1 ].imm = ext->ksym.addr >>32 ; + } } relo->processed = true;break ;
@@ -6933,10 +6949,72 @@ static int bpf_object__read_kallsyms_file(struct bpf_object *obj)return err; } +static int bpf_object__resolve_ksyms_btf_id (struct bpf_object *obj) +{ +struct extern_desc *ext; +int i, id; + +for (i =0 ; i < obj->nr_extern; i++) { +const struct btf_type *targ_var, *targ_type; +__u32 targ_type_id, local_type_id; +const char *targ_var_name; +int ret; + + ext = &obj->externs[i]; +if (ext->type !=EXT_KSYM || !ext->ksym.type_id) +continue ; + + id =btf__find_by_name_kind (obj->btf_vmlinux, ext->name, +BTF_KIND_VAR ); +if (id <=0 ) { +pr_warn ("extern (ksym) '%s': failed to find BTF ID in vmlinux BTF.\n" , + ext->name); +return -ESRCH ; + } + +/* find local type_id */ + local_type_id = ext->ksym.type_id; + +/* find target type_id */ + targ_var =btf__type_by_id (obj->btf_vmlinux, id); + targ_var_name =btf__name_by_offset (obj->btf_vmlinux, + targ_var->name_off); + targ_type =skip_mods_and_typedefs (obj->btf_vmlinux, + targ_var->type, + &targ_type_id); + + ret =bpf_core_types_are_compat (obj->btf, local_type_id, + obj->btf_vmlinux, targ_type_id); +if (ret <=0 ) { +const struct btf_type *local_type; +const char *targ_name, *local_name; + + local_type =btf__type_by_id (obj->btf, local_type_id); + local_name =btf__name_by_offset (obj->btf, + local_type->name_off); + targ_name =btf__name_by_offset (obj->btf_vmlinux, + targ_type->name_off); + +pr_warn ("extern (ksym) '%s': incompatible types, expected [%d] %s %s, but kernel has [%d] %s %s\n" , + ext->name, local_type_id, +btf_kind_str (local_type), local_name, targ_type_id, +btf_kind_str (targ_type), targ_name); +return -EINVAL ; + } + + ext->is_set = true; + ext->ksym.vmlinux_btf_id = id; +pr_debug ("extern (ksym) '%s': resolved to [%d] %s %s\n" , + ext->name, id,btf_kind_str (targ_var), targ_var_name); + } +return 0 ; +} +static int bpf_object__resolve_externs (struct bpf_object *obj,const char *extra_kconfig) {bool need_config = false, need_kallsyms = false; +bool need_vmlinux_btf = false;struct extern_desc *ext;void *kcfg_data =NULL ;int err, i;
@@ -6967,7 +7045,10 @@ static int bpf_object__resolve_externs(struct bpf_object *obj, strncmp(ext->name, "CONFIG_", 7) == 0) { need_config = true; } else if (ext->type == EXT_KSYM) { - need_kallsyms = true; +if (ext->ksym.type_id) + need_vmlinux_btf = true; +else + need_kallsyms = true; } else {pr_warn ("unrecognized extern '%s'\n" , ext->name);return -EINVAL ;
@@ -6996,6 +7077,11 @@ static int bpf_object__resolve_externs(struct bpf_object *obj,if (err)return -EINVAL ; } +if (need_vmlinux_btf) { + err =bpf_object__resolve_ksyms_btf_id (obj); +if (err) +return -EINVAL ; + }for (i =0 ; i < obj->nr_extern; i++) { ext = &obj->externs[i];
@@ -7028,10 +7114,10 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr) } err =bpf_object__probe_loading (obj); + err = err ? :bpf_object__load_vmlinux_btf (obj); err = err ? :bpf_object__resolve_externs (obj, obj->kconfig); err = err ? :bpf_object__sanitize_and_load_btf (obj); err = err ? :bpf_object__sanitize_maps (obj); - err = err ? :bpf_object__load_vmlinux_btf (obj); err = err ? :bpf_object__init_kern_struct_ops_maps (obj); err = err ? :bpf_object__create_maps (obj); err = err ? :bpf_object__relocate (obj, attr->target_btf_path);
Signature · 1 line
2.28.0.618.gf4bc123cb7-goog
Re: [PATCH bpf-next v3 2/6] bpf/libbpf: BTF support for typed ksyms
Quoted 6 lines · Hao Luo
If a ksym is defined with a type, libbpf will try to find the ksym's btf information from kernel btf. If a valid btf entry for the ksym is found, libbpf can pass in the found btf id to the verifier, which validates the ksym's type and value.
Quoted 6 lines
Typeless ksyms (i.e. those defined as 'void') will not have such
btf_id, but it has the symbol's address (read from kallsyms) and its value is treated as a raw pointer.Signed-off-by Hao Luo <[email protected]>---
This looks nice and clean, thanks!
Quoted 3 lines
tools/lib/bpf/libbpf.c | 112 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 99 insertions(+), 13 deletions(-)
[...]
PATCH v3 3/6 selftests/bpf: ksyms_btf to test typed ksyms
In reply to Hao LuoSelftests for typed ksyms. Tests two types of ksyms: one is a struct, the other is a plain int. This tests two paths in the kernel. Struct ksyms will be converted into PTR_TO_BTF_ID by the verifier while int typed ksyms will be converted into PTR_TO_MEM.
--- .../testing/selftests/bpf/prog_tests/ksyms.c | 38 ++++------ .../selftests/bpf/prog_tests/ksyms_btf.c | 70 +++++++++++++++++++ .../selftests/bpf/progs/test_ksyms_btf.c | 23 ++++++ tools/testing/selftests/bpf/trace_helpers.c | 27 +++++++ tools/testing/selftests/bpf/trace_helpers.h | 4 ++ 5 files changed, 137 insertions(+), 25 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/ksyms_btf.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_btf.c
tools/testing/selftests/bpf/prog_tests/ksyms.c +13 -25
@@ -7,40 +7,28 @@static int duration; -static __u64 kallsyms_find (const char *sym) -{ -char type, name[500 ]; -__u64 addr, res =0 ; -FILE *f; - - f =fopen ("/proc/kallsyms" ,"r" ); -if (CHECK (!f,"kallsyms_fopen" ,"failed to open: %d\n" , errno)) -return 0 ; - -while (fscanf (f,"%llx %c %499s%*[^\n]\n" , &addr, &type, name) >0 ) { -if (strcmp (name, sym) ==0 ) { - res = addr; - goto out; - } - } - -CHECK (false,"not_found" ,"symbol %s not found\n" , sym); -out: -fclose (f); -return res; -} -void test_ksyms (void ) { -__u64 per_cpu_start_addr =kallsyms_find ("__per_cpu_start" ); -__u64 link_fops_addr =kallsyms_find ("bpf_link_fops" );const char *btf_path ="/sys/kernel/btf/vmlinux" ;struct test_ksyms *skel;struct test_ksyms__data *data; +__u64 link_fops_addr, per_cpu_start_addr;struct stat st;__u64 btf_size;int err; + err =kallsyms_find ("bpf_link_fops" , &link_fops_addr); +if (CHECK (err == -EINVAL ,"kallsyms_fopen" ,"failed to open: %d\n" , errno)) +return ; +if (CHECK (err == -ENOENT ,"ksym_find" ,"symbol 'bpf_link_fops' not found\n" )) +return ; + + err =kallsyms_find ("__per_cpu_start" , &per_cpu_start_addr); +if (CHECK (err == -EINVAL ,"kallsyms_fopen" ,"failed to open: %d\n" , errno)) +return ; +if (CHECK (err == -ENOENT ,"ksym_find" ,"symbol 'per_cpu_start' not found\n" )) +return ; +if (CHECK (stat (btf_path, &st),"stat_btf" ,"err %d\n" , errno))return ; btf_size = st.st_size;
tools/testing/selftests/bpf/prog_tests/ksyms_btf.c new file +70 -0
70 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_ksyms_btf.c new file +23 -0
@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2020 Google */ + +#include "vmlinux.h" + +#include <bpf/bpf_helpers.h> + +__u64 out__runqueues_addr =-1 ; +__u64 out__bpf_prog_active_addr =-1 ; + +extern const struct rq runqueues __ksym; /* struct type global var. */ +extern const int bpf_prog_active __ksym; /* int type global var. */ + +SEC("raw_tp/sys_enter") +int handler (const void *ctx) +{ + out__runqueues_addr = (__u64)&runqueues; + out__bpf_prog_active_addr = (__u64)&bpf_prog_active; + +return 0 ; +} + +char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/trace_helpers.c +27 -0
@@ -90,6 +90,33 @@ long ksym_get_addr(const char *name)return 0 ; } +/* open kallsyms and read symbol addresses on the fly. Without caching all symbols, +* this is faster than load + find. +*/ +int kallsyms_find (const char *sym,unsigned long long *addr) +{ +char type, name[500 ]; +unsigned long long value; +int err =0 ; +FILE *f; + + f =fopen ("/proc/kallsyms" ,"r" ); +if (!f) +return -EINVAL ; + +while (fscanf (f,"%llx %c %499s%*[^\n]\n" , &value, &type, name) >0 ) { +if (strcmp (name, sym) ==0 ) { + *addr = value; + goto out; + } + } + err = -ENOENT ; + +out: +fclose (f); +return err; +} +void read_trace_pipe (void ) {int trace_fd;
tools/testing/selftests/bpf/trace_helpers.h +4 -0
@@ -12,6 +12,10 @@ struct ksym {int load_kallsyms (void );struct ksym *ksym_search (long key);long ksym_get_addr (const char *name); + +/* open kallsyms and find addresses on the fly, faster than load + search. */ +int kallsyms_find (const char *sym,unsigned long long *addr); +void read_trace_pipe (void );#endif
Signature · 1 line
2.28.0.618.gf4bc123cb7-goog
Re: [PATCH bpf-next v3 3/6] selftests/bpf: ksyms_btf to test typed ksyms
Quoted 8 lines · Hao Luo
Selftests for typed ksyms. Tests two types of ksyms: one is a struct, the other is a plain int. This tests two paths in the kernel. Struct ksyms will be converted into
PTR_TO_BTF_IDby the verifier while int typed ksyms will be converted intoPTR_TO_MEM.Signed-off-by Hao Luo <[email protected]>---
LGTM.
Quoted 9 lines
.../testing/selftests/bpf/prog_tests/ksyms.c | 38 ++++------ .../selftests/bpf/prog_tests/ksyms_btf.c | 70 +++++++++++++++++++ .../selftests/bpf/progs/test_ksyms_btf.c | 23 ++++++ tools/testing/selftests/bpf/trace_helpers.c | 27 +++++++ tools/testing/selftests/bpf/trace_helpers.h | 4 ++ 5 files changed, 137 insertions(+), 25 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/ksyms_btf.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_btf.c
[...]
PATCH v3 4/6 bpf: Introduce bpf_per_cpu_ptr()
In reply to Hao LuoAdd bpf_per_cpu_ptr() to help bpf programs access percpu vars. bpf_per_cpu_ptr() has the same semantic as per_cpu_ptr() in the kernel except that it may return NULL. This happens when the cpu parameter is out of range. So the caller must check the returned value.
--- include/linux/bpf.h | 4 +++ include/linux/btf.h | 11 ++++++ include/uapi/linux/bpf.h | 18 ++++++++++ kernel/bpf/btf.c | 10 ------ kernel/bpf/helpers.c | 18 ++++++++++ kernel/bpf/verifier.c | 64 ++++++++++++++++++++++++++++++++-- kernel/trace/bpf_trace.c | 2 ++ tools/include/uapi/linux/bpf.h | 18 ++++++++++ 8 files changed, 132 insertions(+), 13 deletions(-)
include/linux/bpf.h +4 -0
@@ -292,6 +292,7 @@ enum bpf_arg_type {ARG_PTR_TO_ALLOC_MEM ,/* pointer to dynamically allocated memory */ ARG_PTR_TO_ALLOC_MEM_OR_NULL ,/* pointer to dynamically allocated memory or NULL */ ARG_CONST_ALLOC_SIZE_OR_ZERO ,/* number of allocated bytes requested */ + ARG_PTR_TO_PERCPU_BTF_ID, /* pointer to in-kernel percpu type */ };/* type of values returned from helper functions */
@@ -305,6 +306,7 @@ enum bpf_return_type {RET_PTR_TO_SOCK_COMMON_OR_NULL ,/* returns a pointer to a sock_common or NULL */ RET_PTR_TO_ALLOC_MEM_OR_NULL ,/* returns a pointer to dynamically allocated memory or NULL */ RET_PTR_TO_BTF_ID_OR_NULL ,/* returns a pointer to a btf_id or NULL */ + RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL, /* returns a pointer to a valid memory or a btf_id or NULL */ };/* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
@@ -385,6 +387,7 @@ enum bpf_reg_type {PTR_TO_RDONLY_BUF_OR_NULL ,/* reg points to a readonly buffer or NULL */ PTR_TO_RDWR_BUF ,/* reg points to a read/write buffer */ PTR_TO_RDWR_BUF_OR_NULL ,/* reg points to a read/write buffer or NULL */ + PTR_TO_PERCPU_BTF_ID, /* reg points to a percpu kernel variable */ };/* The information passed from prog-specific *_is_valid_access
@@ -1786,6 +1789,7 @@ extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;extern const struct bpf_func_proto bpf_copy_from_user_proto; +extern const struct bpf_func_proto bpf_per_cpu_ptr_proto;const struct bpf_func_proto *bpf_tracing_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog);
include/linux/btf.h +11 -0
@@ -71,6 +71,11 @@ btf_resolve_size(const struct btf *btf, const struct btf_type *type, i <btf_type_vlen (struct_type); \ i++, member++) +#define for_each_vsi (i, datasec_type, member) \ + for (i = 0, member = btf_type_var_secinfo(datasec_type); \ + i < btf_type_vlen(datasec_type); \ + i++, member++) +static inline bool btf_type_is_ptr (const struct btf_type *t) {return BTF_INFO_KIND (t->info) ==BTF_KIND_PTR ;
@@ -155,6 +160,12 @@ static inline const struct btf_member *btf_type_member(const struct btf_type *t)return (const struct btf_member *)(t +1 ); } +static inline const struct btf_var_secinfo *btf_type_var_secinfo ( +const struct btf_type *t) +{ +return (const struct btf_var_secinfo *)(t +1 ); +} +#ifdef CONFIG_BPF_SYSCALL const struct btf_type *btf_type_by_id (const struct btf *btf,u32 type_id);const char *btf_name_by_offset (const struct btf *btf,u32 offset);
include/uapi/linux/bpf.h +18 -0
@@ -3604,6 +3604,23 @@ union bpf_attr {* the data in *dst*. This is a wrapper of **copy_from_user**\ (). * Return * 0 on success, or a negative error in case of failure. +* +* void *bpf_per_cpu_ptr(const void *percpu_ptr, u32 cpu) +* Description +* Take a pointer to a percpu ksym, *percpu_ptr*, and return a +* pointer to the percpu kernel variable on *cpu*. A ksym is an +* extern variable decorated with '__ksym'. For ksym, there is a +* global var (either static or global) defined of the same name +* in the kernel. The ksym is percpu if the global var is percpu. +* The returned pointer points to the global percpu var on *cpu*. +* +* bpf_per_cpu_ptr() has the same semantic as per_cpu_ptr() in the +* kernel, except that bpf_per_cpu_ptr() may return NULL. This +* happens if *cpu* is larger than nr_cpu_ids. The caller of +* bpf_per_cpu_ptr() must check the returned value. +* Return +* A pointer pointing to the kernel percpu variable on *cpu*, or +* NULL, if *cpu* is invalid. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -3755,6 +3772,7 @@ union bpf_attr {FN (inode_storage_delete), \FN (d_path), \FN (copy_from_user), \ +FN (bpf_per_cpu_ptr), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/btf.c +0 -10
@@ -188,11 +188,6 @@ i <btf_type_vlen (struct_type); \ i++, member++) -#define for_each_vsi (i, struct_type, member) \ - for (i = 0, member = btf_type_var_secinfo(struct_type); \ - i < btf_type_vlen(struct_type); \ - i++, member++) -#define for_each_vsi_from (i, from, struct_type, member) \ for (i = from, member = btf_type_var_secinfo(struct_type) + from; \ i < btf_type_vlen(struct_type); \
@@ -513,11 +508,6 @@ static const struct btf_var *btf_type_var(const struct btf_type *t)return (const struct btf_var *)(t +1 ); } -static const struct btf_var_secinfo *btf_type_var_secinfo (const struct btf_type *t) -{ -return (const struct btf_var_secinfo *)(t +1 ); -} -static const struct btf_kind_operations *btf_type_ops (const struct btf_type *t) {return kind_ops[BTF_INFO_KIND (t->info)];
kernel/bpf/helpers.c +18 -0
@@ -623,6 +623,22 @@ const struct bpf_func_proto bpf_copy_from_user_proto = { .arg3_type = ARG_ANYTHING, }; +BPF_CALL_2(bpf_per_cpu_ptr, const void *, ptr, u32, cpu) +{ +if (cpu >= nr_cpu_ids) +return (unsigned long )NULL ; + +return (unsigned long )per_cpu_ptr (ptr, cpu); +} + +const struct bpf_func_proto bpf_per_cpu_ptr_proto = { + .func = bpf_per_cpu_ptr, + .gpl_only = false, + .ret_type =RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL , + .arg1_type =ARG_PTR_TO_PERCPU_BTF_ID , + .arg2_type =ARG_ANYTHING , +}; +const struct bpf_func_proto bpf_get_current_task_proto__weak ;const struct bpf_func_proto bpf_probe_read_user_proto__weak ;const struct bpf_func_proto bpf_probe_read_user_str_proto__weak ;
@@ -685,6 +701,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)return bpf_get_trace_printk_proto ();case BPF_FUNC_jiffies64:return &bpf_jiffies64_proto; +case BPF_FUNC_bpf_per_cpu_ptr: +return &bpf_per_cpu_ptr_proto;default :break ; }
kernel/bpf/verifier.c +61 -3
126 lines not on this page · raw message
kernel/trace/bpf_trace.c +2 -0
@@ -1230,6 +1230,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_get_task_stack_proto;case BPF_FUNC_copy_from_user:return prog->aux->sleepable ? &bpf_copy_from_user_proto :NULL ; +case BPF_FUNC_bpf_per_cpu_ptr: +return &bpf_per_cpu_ptr_proto;default :return NULL ; }
tools/include/uapi/linux/bpf.h +18 -0
@@ -3604,6 +3604,23 @@ union bpf_attr {* the data in *dst*. This is a wrapper of **copy_from_user**\ (). * Return * 0 on success, or a negative error in case of failure. +* +* void *bpf_per_cpu_ptr(const void *percpu_ptr, u32 cpu) +* Description +* Take a pointer to a percpu ksym, *percpu_ptr*, and return a +* pointer to the percpu kernel variable on *cpu*. A ksym is an +* extern variable decorated with '__ksym'. For ksym, there is a +* global var (either static or global) defined of the same name +* in the kernel. The ksym is percpu if the global var is percpu. +* The returned pointer points to the global percpu var on *cpu*. +* +* bpf_per_cpu_ptr() has the same semantic as per_cpu_ptr() in the +* kernel, except that bpf_per_cpu_ptr() may return NULL. This +* happens if *cpu* is larger than nr_cpu_ids. The caller of +* bpf_per_cpu_ptr() must check the returned value. +* Return +* A pointer pointing to the kernel percpu variable on *cpu*, or +* NULL, if *cpu* is invalid. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -3755,6 +3772,7 @@ union bpf_attr {FN (inode_storage_delete), \FN (d_path), \FN (copy_from_user), \ +FN (bpf_per_cpu_ptr), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.28.0.618.gf4bc123cb7-goog
kernel test robot
build botRe: [PATCH bpf-next v3 4/6] bpf: Introduce bpf_per_cpu_ptr()
Thank you for the patch! Perhaps something to improve:
Hi Hao,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on bpf-next/master]
url: https://github.com/0day-ci/linux/commits/Hao-Luo/bpf-BTF-support-for-ksyms/20200917-064052 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master config: powerpc-randconfig-s032-20200916 (attached as .config) compiler: powerpc64-linux-gcc (GCC) 9.3.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.2-201-g24bdaac6-dirty
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=powerpc
If you fix the issue, kindly add following tag as appropriate
sparse warnings: (new ones prefixed by >>)
>> kernel/bpf/helpers.c:631:31: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected void const [noderef] __percpu *__vpp_verify @@ got void const * @@ >> kernel/bpf/helpers.c:631:31: sparse: expected void const [noderef] __percpu *__vpp_verify >> kernel/bpf/helpers.c:631:31: sparse: got void const *
# https://github.com/0day-ci/linux/commit/3f6ea3c1c73efe466a96ff7499219fe3b03b8f48
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Hao-Luo/bpf-BTF-support-for-ksyms/20200917-064052 git checkout 3f6ea3c1c73efe466a96ff7499219fe3b03b8f48
vim +631 kernel/bpf/helpers.c
625
626 BPF_CALL_2(bpf_per_cpu_ptr, const void *, ptr, u32, cpu)
627 {
628 if (cpu >= nr_cpu_ids)
629 return (unsigned long)NULL;
630
> 631 return (unsigned long)per_cpu_ptr(ptr, cpu);
632 }
633
634 const struct bpf_func_proto bpf_per_cpu_ptr_proto = {
635 .func = bpf_per_cpu_ptr,
636 .gpl_only = false,
637 .ret_type = RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL,
638 .arg1_type = ARG_PTR_TO_PERCPU_BTF_ID,
639 .arg2_type = ARG_ANYTHING,
640 };
641
> 642 const struct bpf_func_proto bpf_get_current_task_proto __weak; 643 const struct bpf_func_proto bpf_probe_read_user_proto __weak;
644 const struct bpf_func_proto bpf_probe_read_user_str_proto __weak;
645 const struct bpf_func_proto bpf_probe_read_kernel_proto __weak;
646 const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak;
647
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Re: [PATCH bpf-next v3 4/6] bpf: Introduce bpf_per_cpu_ptr()
I need to cast the pointer to "const void __percpu *" before passing into per_cpu_ptr. I will update and resend.
Quoted 62 lines · kernel test robot
Hi Hao,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on bpf-next/master]
url: https://github.com/0day-ci/linux/commits/Hao-Luo/bpf-BTF-support-for-ksyms/20200917-064052 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master config: powerpc-randconfig-s032-20200916 (attached as .config) compiler: powerpc64-linux-gcc (GCC) 9.3.0reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.2-201-g24bdaac6-dirty
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=powerpcIf you fix the issue, kindly add following tag as appropriate
Reported-by kernel test robot <[email protected]>sparse warnings: (new ones prefixed by >>)
Quoted 3 lines
Quoted 3 lines
kernel/bpf/helpers.c:631:31: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected void const [noderef]__percpu*__vpp_verify@@ got void const * @@kernel/bpf/helpers.c:631:31: sparse: expected void const [noderef]__percpu*__vpp_verifykernel/bpf/helpers.c:631:31: sparse: got void const *# https://github.com/0day-ci/linux/commit/3f6ea3c1c73efe466a96ff7499219fe3b03b8f48
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Hao-Luo/bpf-BTF-support-for-ksyms/20200917-064052 git checkout3f6ea3c1c73efe466a96ff7499219fe3b03b8f48
vim +631kernel/bpf/helpers.c625 626 BPF_CALL_2(bpf_per_cpu_ptr, const void *, ptr, u32, cpu) 627 { 628 if (cpu >= nr_cpu_ids) 629 return (unsigned long)NULL; 630Quoted 1 line
631 return (unsigned long)per_cpu_ptr(ptr, cpu);
632 }
633
634 const structbpf_func_protobpf_per_cpu_ptr_proto= {635 .func = bpf_per_cpu_ptr, 636 .gpl_only = false, 637 .ret_type = RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL, 638 .arg1_type = ARG_PTR_TO_PERCPU_BTF_ID, 639 .arg2_type = ARG_ANYTHING, 640 }; 641Quoted 1 line
642 const struct
bpf_func_protobpf_get_current_task_proto__weak;643 const struct bpf_func_proto bpf_probe_read_user_proto __weak; 644 const struct bpf_func_proto bpf_probe_read_user_str_proto __weak; 645 const struct bpf_func_proto bpf_probe_read_kernel_proto __weak; 646 const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak;647
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Re: [PATCH bpf-next v3 4/6] bpf: Introduce bpf_per_cpu_ptr()
Quoted 3 lines · Hao Luo
I need to cast the pointer to "const void
__percpu*" before passing intoper_cpu_ptr. I will update and resend.
You can try just declaring it as __percpu in BPF_CALL_2 macro. That might work, or not, depending on how exactly BPF_CALL macros are implemented (I haven't checked).
Quoted 64 lines · kernel test robot
Quoted 62 lines
Hi Hao,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on bpf-next/master]
url: https://github.com/0day-ci/linux/commits/Hao-Luo/bpf-BTF-support-for-ksyms/20200917-064052 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master config: powerpc-randconfig-s032-20200916 (attached as .config) compiler: powerpc64-linux-gcc (GCC) 9.3.0reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.2-201-g24bdaac6-dirty
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=powerpcIf you fix the issue, kindly add following tag as appropriate
Reported-by kernel test robot <[email protected]>sparse warnings: (new ones prefixed by >>)
Quoted 3 lines
Quoted 3 lines
kernel/bpf/helpers.c:631:31: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected void const [noderef]__percpu*__vpp_verify@@ got void const * @@kernel/bpf/helpers.c:631:31: sparse: expected void const [noderef]__percpu*__vpp_verifykernel/bpf/helpers.c:631:31: sparse: got void const *# https://github.com/0day-ci/linux/commit/3f6ea3c1c73efe466a96ff7499219fe3b03b8f48
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Hao-Luo/bpf-BTF-support-for-ksyms/20200917-064052 git checkout3f6ea3c1c73efe466a96ff7499219fe3b03b8f48
vim +631kernel/bpf/helpers.c625 626 BPF_CALL_2(bpf_per_cpu_ptr, const void *, ptr, u32, cpu) 627 { 628 if (cpu >= nr_cpu_ids) 629 return (unsigned long)NULL; 630Quoted 1 line
631 return (unsigned long)per_cpu_ptr(ptr, cpu);
632 }
633
634 const structbpf_func_protobpf_per_cpu_ptr_proto= {635 .func = bpf_per_cpu_ptr, 636 .gpl_only = false, 637 .ret_type = RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL, 638 .arg1_type = ARG_PTR_TO_PERCPU_BTF_ID, 639 .arg2_type = ARG_ANYTHING, 640 }; 641Quoted 1 line
642 const struct
bpf_func_protobpf_get_current_task_proto__weak;643 const struct bpf_func_proto bpf_probe_read_user_proto __weak; 644 const struct bpf_func_proto bpf_probe_read_user_str_proto __weak; 645 const struct bpf_func_proto bpf_probe_read_kernel_proto __weak; 646 const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak;647
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]
Re: [PATCH bpf-next v3 4/6] bpf: Introduce bpf_per_cpu_ptr()
Quoted 10 lines · Andrii Nakryiko
Quoted 3 lines · Hao Luo
I need to cast the pointer to "const void
__percpu*" before passing intoper_cpu_ptr. I will update and resend.You can try just declaring it as
__percpuinBPF_CALL_2macro. That might work, or not, depending on how exactlyBPF_CALLmacros are implemented (I haven't checked).
ACK. IMO it's probably better cast inside, rather than depending on BPF_CALL macros. The parameters are not true percpu pointers anyway and potential changes on BPF_CALL may break this, I'm afraid.
Quoted 64 lines
Quoted 64 lines · kernel test robot
Quoted 62 lines
Hi Hao,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on bpf-next/master]
url: https://github.com/0day-ci/linux/commits/Hao-Luo/bpf-BTF-support-for-ksyms/20200917-064052 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master config: powerpc-randconfig-s032-20200916 (attached as .config) compiler: powerpc64-linux-gcc (GCC) 9.3.0reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.2-201-g24bdaac6-dirty
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=powerpcIf you fix the issue, kindly add following tag as appropriate
Reported-by kernel test robot <[email protected]>sparse warnings: (new ones prefixed by >>)
Quoted 3 lines
Quoted 3 lines
kernel/bpf/helpers.c:631:31: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected void const [noderef]__percpu*__vpp_verify@@ got void const * @@kernel/bpf/helpers.c:631:31: sparse: expected void const [noderef]__percpu*__vpp_verifykernel/bpf/helpers.c:631:31: sparse: got void const *# https://github.com/0day-ci/linux/commit/3f6ea3c1c73efe466a96ff7499219fe3b03b8f48
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Hao-Luo/bpf-BTF-support-for-ksyms/20200917-064052 git checkout3f6ea3c1c73efe466a96ff7499219fe3b03b8f48
vim +631kernel/bpf/helpers.c625 626 BPF_CALL_2(bpf_per_cpu_ptr, const void *, ptr, u32, cpu) 627 { 628 if (cpu >= nr_cpu_ids) 629 return (unsigned long)NULL; 630Quoted 1 line
631 return (unsigned long)per_cpu_ptr(ptr, cpu);
632 }
633
634 const structbpf_func_protobpf_per_cpu_ptr_proto= {635 .func = bpf_per_cpu_ptr, 636 .gpl_only = false, 637 .ret_type = RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL, 638 .arg1_type = ARG_PTR_TO_PERCPU_BTF_ID, 639 .arg2_type = ARG_ANYTHING, 640 }; 641Quoted 1 line
642 const struct
bpf_func_protobpf_get_current_task_proto__weak;643 const struct bpf_func_proto bpf_probe_read_user_proto __weak; 644 const struct bpf_func_proto bpf_probe_read_user_str_proto __weak; 645 const struct bpf_func_proto bpf_probe_read_kernel_proto __weak; 646 const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak;647
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Re: [PATCH bpf-next v3 4/6] bpf: Introduce bpf_per_cpu_ptr()
In reply to Hao LuoQuoted 8 lines · Hao Luo
Add
bpf_per_cpu_ptr()to help bpf programs access percpu vars.bpf_per_cpu_ptr()has the same semantic asper_cpu_ptr()in the kernel except that it may return NULL. This happens when the cpu parameter is out of range. So the caller must check the returned value.Acked-by Andrii Nakryiko <[email protected]>Signed-off-by Hao Luo <[email protected]>
Quoted 11 lines
--- include/linux/bpf.h | 4 +++ include/linux/btf.h | 11 ++++++ include/uapi/linux/bpf.h | 18 ++++++++++ kernel/bpf/btf.c | 10 ------ kernel/bpf/helpers.c | 18 ++++++++++ kernel/bpf/verifier.c | 64 ++++++++++++++++++++++++++++++++-- kernel/trace/bpf_trace.c | 2 ++ tools/include/uapi/linux/bpf.h | 18 ++++++++++ 8 files changed, 132 insertions(+), 13 deletions(-)
I already acked this, but see my concern about O(N) look up for .data..percpu. Feel free to follow up on this with a separate patch. Thanks!
[...]
kernel/bpf/verifier.c static int check_func_arg(struct bpf_verifier_env *env, u32 arg, · line 4019
Quoted 13 lines
kernel/bpf/verifier.c +9
-0@@ -4003,6 +4008,15 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,if (type != expected_type) goto err_type; } + }else if (arg_type ==ARG_PTR_TO_PERCPU_BTF_ID ) { + expected_type =PTR_TO_PERCPU_BTF_ID ; +if (type != expected_type) + goto err_type; +if (!reg->btf_id) { +verbose (env,"Helper has invalid btf_id in R%d\n" , regno); +return -EACCES ; + } + meta->ret_btf_id = reg->btf_id;
FYI, this will conflict with Lorenz's refactoring, so you might need to rebase and solve the conflicts if his patch set lands first.
Quoted 59 lines
kernel/bpf/verifier.c +30
-1}else if (arg_type ==ARG_PTR_TO_BTF_ID ) {bool ids_match = false;@@ -5002,6 +5016,30 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn regs[BPF_REG_0 ].type =PTR_TO_MEM_OR_NULL ; regs[BPF_REG_0 ].id = ++env->id_gen; regs[BPF_REG_0 ].mem_size = meta.mem_size; + }else if (fn->ret_type ==RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL ) { +const struct btf_type *t; + +mark_reg_known_zero (env, regs,BPF_REG_0 ); + t =btf_type_skip_modifiers (btf_vmlinux, meta.ret_btf_id,NULL ); +if (!btf_type_is_struct (t)) { +u32 tsize; +const struct btf_type *ret; +const char *tname; + +/* resolve the type size of ksym. */ + ret =btf_resolve_size (btf_vmlinux, t, &tsize); +if (IS_ERR (ret)) { + tname =btf_name_by_offset (btf_vmlinux, t->name_off); +verbose (env,"unable to resolve the size of type '%s': %ld\n" , + tname,PTR_ERR (ret)); +return -EINVAL ; + } + regs[BPF_REG_0 ].type =PTR_TO_MEM_OR_NULL ; + regs[BPF_REG_0 ].mem_size = tsize; + }else { + regs[BPF_REG_0 ].type =PTR_TO_BTF_ID_OR_NULL ; + regs[BPF_REG_0 ].btf_id = meta.ret_btf_id; + } }else if (fn->ret_type ==RET_PTR_TO_BTF_ID_OR_NULL ) {int ret_btf_id;@@ -7413,6 +7451,7 @@ static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) dst_reg->mem_size = aux->btf_var.mem_size;break ;case PTR_TO_BTF_ID : +case PTR_TO_PERCPU_BTF_ID : dst_reg->btf_id = aux->btf_var.btf_id;break ;default :@@ -9313,10 +9352,14 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env,struct bpf_insn *insn, struct bpf_insn_aux_data *aux) { - u32 type, id = insn->imm; + u32 datasec_id, type, id = insn->imm; +const struct btf_var_secinfo *vsi; +const struct btf_type *datasec;const struct btf_type *t;const char *sym_name; +bool percpu = false;u64 addr; +int i;if (!btf_vmlinux) {verbose (env,"kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n" );
kernel/bpf/verifier.c static int check_pseudo_btf_id(struct bpf_verifier_env *env, · line 9395
Quoted 5 lines
kernel/bpf/verifier.c +2
-0@@ -9348,12 +9391,27 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env,return -ENOENT ; } + datasec_id =btf_find_by_name_kind (btf_vmlinux,".data..percpu" , +BTF_KIND_DATASEC );
this is a relatively expensive O(N) operation, it probably makes sense to cache it (there are about 80'000 types now in BTF for my typical kernel config, so iterating that much for every single ldimm64 for ksym is kind of expensive.
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+if (datasec_id >0 ) { + datasec =btf_type_by_id (btf_vmlinux, datasec_id); +for_each_vsi (i, datasec, vsi) { +if (vsi->type == id) { + percpu = true; +break ; + } + } + } +
[...]
Re: [PATCH bpf-next v3 4/6] bpf: Introduce bpf_per_cpu_ptr()
Hi, Andrii,
Thanks for taking a look. Sorry for the late reply. Spent some time on rebasing and fixing a build issue in my development environment that started happening in v5.9.
Quoted 28 lines · Andrii Nakryiko
Quoted 8 lines · Hao Luo
Add
bpf_per_cpu_ptr()to help bpf programs access percpu vars.bpf_per_cpu_ptr()has the same semantic asper_cpu_ptr()in the kernel except that it may return NULL. This happens when the cpu parameter is out of range. So the caller must check the returned value.Acked-by Andrii Nakryiko <[email protected]>Signed-off-by Hao Luo <[email protected]>Quoted 11 lines
--- include/linux/bpf.h | 4 +++ include/linux/btf.h | 11 ++++++ include/uapi/linux/bpf.h | 18 ++++++++++ kernel/bpf/btf.c | 10 ------ kernel/bpf/helpers.c | 18 ++++++++++ kernel/bpf/verifier.c | 64 ++++++++++++++++++++++++++++++++-- kernel/trace/bpf_trace.c | 2 ++ tools/include/uapi/linux/bpf.h | 18 ++++++++++ 8 files changed, 132 insertions(+), 13 deletions(-)I already acked this, but see my concern about O(N) look up for .data..percpu. Feel free to follow up on this with a separate patch. Thanks!
[...]
Quoted 17 lines
kernel/bpf/verifier.c static int check_func_arg(struct bpf_verifier_env *env, u32 arg, · line 4019
Quoted 13 lines
kernel/bpf/verifier.c +9
-0@@ -4003,6 +4008,15 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,if (type != expected_type) goto err_type; } + }else if (arg_type ==ARG_PTR_TO_PERCPU_BTF_ID ) { + expected_type =PTR_TO_PERCPU_BTF_ID ; +if (type != expected_type) + goto err_type; +if (!reg->btf_id) { +verbose (env,"Helper has invalid btf_id in R%d\n" , regno); +return -EACCES ; + } + meta->ret_btf_id = reg->btf_id;FYI, this will conflict with Lorenz's refactoring, so you might need to rebase and solve the conflicts if his patch set lands first.
Indeed. Do hit this while rebasing but managed to resolve it. Please take a look and let me know if you have comments there in v4
Quoted 28 lines
Quoted 28 lines
kernel/bpf/verifier.c +6
-1@@ -7413,6 +7451,7 @@ static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) dst_reg->mem_size = aux->btf_var.mem_size;break ;case PTR_TO_BTF_ID : +case PTR_TO_PERCPU_BTF_ID : dst_reg->btf_id = aux->btf_var.btf_id;break ;default :@@ -9313,10 +9352,14 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env,struct bpf_insn *insn, struct bpf_insn_aux_data *aux) { - u32 type, id = insn->imm; + u32 datasec_id, type, id = insn->imm; +const struct btf_var_secinfo *vsi; +const struct btf_type *datasec;const struct btf_type *t;const char *sym_name; +bool percpu = false;u64 addr; +int i;if (!btf_vmlinux) {verbose (env,"kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n" );@@ -9348,12 +9391,27 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env,return -ENOENT ; }
Quoted 8 lines
kernel/bpf/verifier.c static int check_pseudo_btf_id(struct bpf_verifier_env *env, · line 9395
Quoted 2 lines
kernel/bpf/verifier.c +2
-0+ datasec_id =btf_find_by_name_kind (btf_vmlinux,".data..percpu" , +BTF_KIND_DATASEC );this is a relatively expensive O(N) operation, it probably makes sense to cache it (there are about 80'000 types now in BTF for my typical kernel config, so iterating that much for every single ldimm64 for ksym is kind of expensive.
ACK. This currently works. I can do it in another patch.
Hao
PATCH v3 5/6 bpf: Introduce bpf_this_cpu_ptr()
In reply to Hao LuoAdd bpf_this_cpu_ptr() to help access percpu var on this cpu. This helper always returns a valid pointer, therefore no need to check returned value for NULL. Also note that all programs run with preemption disabled, which means that the returned pointer is stable during all the execution of the program.
--- include/linux/bpf.h | 2 ++ include/uapi/linux/bpf.h | 13 +++++++++++++ kernel/bpf/helpers.c | 14 ++++++++++++++ kernel/bpf/verifier.c | 11 ++++++++--- kernel/trace/bpf_trace.c | 2 ++ tools/include/uapi/linux/bpf.h | 13 +++++++++++++ 6 files changed, 52 insertions(+), 3 deletions(-)
include/linux/bpf.h +2 -0
@@ -307,6 +307,7 @@ enum bpf_return_type {RET_PTR_TO_ALLOC_MEM_OR_NULL ,/* returns a pointer to dynamically allocated memory or NULL */ RET_PTR_TO_BTF_ID_OR_NULL ,/* returns a pointer to a btf_id or NULL */ RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL ,/* returns a pointer to a valid memory or a btf_id or NULL */ + RET_PTR_TO_MEM_OR_BTF_ID, /* returns a pointer to a valid memory or a btf_id */ };/* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
@@ -1790,6 +1791,7 @@ extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;extern const struct bpf_func_proto bpf_copy_from_user_proto;extern const struct bpf_func_proto bpf_per_cpu_ptr_proto; +extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;const struct bpf_func_proto *bpf_tracing_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog);
include/uapi/linux/bpf.h +13 -0
@@ -3621,6 +3621,18 @@ union bpf_attr {* Return * A pointer pointing to the kernel percpu variable on *cpu*, or * NULL, if *cpu* is invalid. +* +* void *bpf_this_cpu_ptr(const void *percpu_ptr) +* Description +* Take a pointer to a percpu ksym, *percpu_ptr*, and return a +* pointer to the percpu kernel variable on this cpu. See the +* description of 'ksym' in **bpf_per_cpu_ptr**\ (). +* +* bpf_this_cpu_ptr() has the same semantic as this_cpu_ptr() in +* the kernel. Different from **bpf_per_cpu_ptr**\ (), it would +* never return NULL. +* Return +* A pointer pointing to the kernel percpu variable on this cpu. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -3773,6 +3785,7 @@ union bpf_attr {FN (d_path), \FN (copy_from_user), \FN (bpf_per_cpu_ptr), \ +FN (bpf_this_cpu_ptr), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/helpers.c +14 -0
@@ -639,6 +639,18 @@ const struct bpf_func_proto bpf_per_cpu_ptr_proto = { .arg2_type = ARG_ANYTHING, }; +BPF_CALL_1(bpf_this_cpu_ptr, const void *, percpu_ptr) +{ +return (unsigned long )this_cpu_ptr (percpu_ptr); +} + +const struct bpf_func_proto bpf_this_cpu_ptr_proto = { + .func = bpf_this_cpu_ptr, + .gpl_only = false, + .ret_type =RET_PTR_TO_MEM_OR_BTF_ID , + .arg1_type =ARG_PTR_TO_PERCPU_BTF_ID , +}; +const struct bpf_func_proto bpf_get_current_task_proto__weak ;const struct bpf_func_proto bpf_probe_read_user_proto__weak ;const struct bpf_func_proto bpf_probe_read_user_str_proto__weak ;
@@ -703,6 +715,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)return &bpf_jiffies64_proto;case BPF_FUNC_bpf_per_cpu_ptr:return &bpf_per_cpu_ptr_proto; +case BPF_FUNC_bpf_this_cpu_ptr: +return &bpf_this_cpu_ptr_proto;default :break ; }
kernel/bpf/verifier.c +8 -3
@@ -5016,7 +5016,8 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn regs[BPF_REG_0 ].type =PTR_TO_MEM_OR_NULL ; regs[BPF_REG_0 ].id = ++env->id_gen; regs[BPF_REG_0 ].mem_size = meta.mem_size; - }else if (fn->ret_type ==RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL ) { + }else if (fn->ret_type ==RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL || + fn->ret_type ==RET_PTR_TO_MEM_OR_BTF_ID ) {const struct btf_type *t;mark_reg_known_zero (env, regs,BPF_REG_0 );
@@ -5034,10 +5035,14 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn tname, PTR_ERR(ret));return -EINVAL ; } - regs[BPF_REG_0 ].type =PTR_TO_MEM_OR_NULL ; + regs[BPF_REG_0 ].type = + fn->ret_type ==RET_PTR_TO_MEM_OR_BTF_ID ? +PTR_TO_MEM :PTR_TO_MEM_OR_NULL ; regs[BPF_REG_0 ].mem_size = tsize; }else { - regs[BPF_REG_0 ].type =PTR_TO_BTF_ID_OR_NULL ; + regs[BPF_REG_0 ].type = + fn->ret_type ==RET_PTR_TO_MEM_OR_BTF_ID ? +PTR_TO_BTF_ID :PTR_TO_BTF_ID_OR_NULL ; regs[BPF_REG_0 ].btf_id = meta.ret_btf_id; } }else if (fn->ret_type ==RET_PTR_TO_BTF_ID_OR_NULL ) {
kernel/trace/bpf_trace.c +2 -0
@@ -1232,6 +1232,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return prog->aux->sleepable ? &bpf_copy_from_user_proto :NULL ;case BPF_FUNC_bpf_per_cpu_ptr:return &bpf_per_cpu_ptr_proto; +case BPF_FUNC_bpf_this_cpu_ptr: +return &bpf_this_cpu_ptr_proto;default :return NULL ; }
tools/include/uapi/linux/bpf.h +13 -0
@@ -3621,6 +3621,18 @@ union bpf_attr {* Return * A pointer pointing to the kernel percpu variable on *cpu*, or * NULL, if *cpu* is invalid. +* +* void *bpf_this_cpu_ptr(const void *percpu_ptr) +* Description +* Take a pointer to a percpu ksym, *percpu_ptr*, and return a +* pointer to the percpu kernel variable on this cpu. See the +* description of 'ksym' in **bpf_per_cpu_ptr**\ (). +* +* bpf_this_cpu_ptr() has the same semantic as this_cpu_ptr() in +* the kernel. Different from **bpf_per_cpu_ptr**\ (), it would +* never return NULL. +* Return +* A pointer pointing to the kernel percpu variable on this cpu. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -3773,6 +3785,7 @@ union bpf_attr {FN (d_path), \FN (copy_from_user), \FN (bpf_per_cpu_ptr), \ +FN (bpf_this_cpu_ptr), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.28.0.618.gf4bc123cb7-goog
PATCH v3 6/6 bpf/selftests: Test for bpf_per_cpu_ptr() and bpf_this_cpu_ptr()
In reply to Hao LuoTest bpf_per_cpu_ptr() and bpf_this_cpu_ptr(). Test two paths in the kernel. If the base pointer points to a struct, the returned reg is of type PTR_TO_BTF_ID. Direct pointer dereference can be applied on the returned variable. If the base pointer isn't a struct, the returned reg is of type PTR_TO_MEM, which also supports direct pointer dereference.
--- .../selftests/bpf/prog_tests/ksyms_btf.c | 18 +++++++++++ .../selftests/bpf/progs/test_ksyms_btf.c | 32 +++++++++++++++++++ 2 files changed, 50 insertions(+)
tools/testing/selftests/bpf/prog_tests/ksyms_btf.c +18 -0
@@ -11,6 +11,8 @@ static int duration;void test_ksyms_btf (void ) {__u64 runqueues_addr, bpf_prog_active_addr; +__u32 this_rq_cpu; +int this_bpf_prog_active;struct test_ksyms_btf *skel =NULL ;struct test_ksyms_btf__data *data;struct btf *btf;
@@ -64,6 +66,22 @@ void test_ksyms_btf(void) (unsigned long long )data->out__bpf_prog_active_addr, (unsigned long long)bpf_prog_active_addr); +CHECK (data->out__rq_cpu ==-1 ,"rq_cpu" , +"got %u, exp != -1\n" , data->out__rq_cpu); +CHECK (data->out__bpf_prog_active <0 ,"bpf_prog_active" , +"got %d, exp >= 0\n" , data->out__bpf_prog_active); +CHECK (data->out__cpu_0_rq_cpu !=0 ,"cpu_rq(0)->cpu" , +"got %u, exp 0\n" , data->out__cpu_0_rq_cpu); + + this_rq_cpu = data->out__this_rq_cpu; +CHECK (this_rq_cpu != data->out__rq_cpu,"this_rq_cpu" , +"got %u, exp %u\n" , this_rq_cpu, data->out__rq_cpu); + + this_bpf_prog_active = data->out__this_bpf_prog_active; +CHECK (this_bpf_prog_active != data->out__bpf_prog_active,"this_bpf_prog_active" , +"got %d, exp %d\n" , this_bpf_prog_active, + data->out__bpf_prog_active); + cleanup:btf__free (btf);test_ksyms_btf__destroy (skel);
tools/testing/selftests/bpf/progs/test_ksyms_btf.c +32 -0
@@ -8,15 +8,47 @@__u64 out__runqueues_addr =-1 ;__u64 out__bpf_prog_active_addr =-1 ; +__u32 out__rq_cpu =-1 ;/* percpu struct fields */ +int out__bpf_prog_active =-1 ;/* percpu int */ + +__u32 out__this_rq_cpu =-1 ; +int out__this_bpf_prog_active =-1 ; + +__u32 out__cpu_0_rq_cpu =-1 ;/* cpu_rq(0)->cpu */ + extern const struct rq runqueues __ksym; /* struct type global var. */ extern const int bpf_prog_active __ksym; /* int type global var. */ SEC("raw_tp/sys_enter")int handler (const void *ctx) { +struct rq *rq; +int *active; +__u32 cpu; + out__runqueues_addr = (__u64)&runqueues; out__bpf_prog_active_addr = (__u64)&bpf_prog_active; + cpu =bpf_get_smp_processor_id (); + +/* test bpf_per_cpu_ptr() */ + rq = (struct rq *)bpf_per_cpu_ptr (&runqueues, cpu); +if (rq) + out__rq_cpu = rq->cpu; + active = (int *)bpf_per_cpu_ptr (&bpf_prog_active, cpu); +if (active) + out__bpf_prog_active = *active; + + rq = (struct rq *)bpf_per_cpu_ptr (&runqueues,0 ); +if (rq)/* should always be valid, but we can't spare the check. */ + out__cpu_0_rq_cpu = rq->cpu; + +/* test bpf_this_cpu_ptr */ + rq = (struct rq *)bpf_this_cpu_ptr (&runqueues); + out__this_rq_cpu = rq->cpu; + active = (int *)bpf_this_cpu_ptr (&bpf_prog_active); + out__this_bpf_prog_active = *active; +return 0 ; }
Signature · 1 line
2.28.0.618.gf4bc123cb7-goog