tools/bpf: sync kernel btf.h header
Subsystems (7)
PATCH v4 5/13 bpf: get better bpf_prog ksyms based on btf func type_id
In reply to Yonghong SongThis patch added interface to load a program with the following additional information:
. prog_btf_fd
. func_info, func_info_rec_size and func_info_cnt
where func_info will provide function range and type_id corresponding to each function.
The func_info_rec_size is introduced in the UAPI to specify struct bpf_func_info size passed from user space. This intends to make bpf_func_info structure growable in the future. If the kernel gets a different bpf_func_info size from userspace, it will try to handle user request with part of bpf_func_info it can understand. In this patch, kernel can understand
struct bpf_func_info {
__u32 insn_offset;
__u32 type_id; };
If user passed a bpf func_info record size of 16 bytes, the kernel can still handle part of records with the above definition.
If verifier agrees with function range provided by the user, the bpf_prog ksym for each function will use the func name provided in the type_id, which is supposed to provide better encoding as it is not limited by 16 bytes program name limitation and this is better for bpf program which contains multiple subprograms.
The bpf_prog_info interface is also extended to
return btf_id, func_info, func_info_rec_size and func_info_cnt to userspace, so userspace can print out the function prototype for each xlated function. The insn_offset in the returned func_info corresponds to the insn offset for xlated functions. With other jit related fields in bpf_prog_info, userspace can also print out function prototypes for each jited function.
--- include/linux/bpf.h | 5 +- include/linux/bpf_verifier.h | 1 + include/linux/btf.h | 2 + include/uapi/linux/bpf.h | 13 ++++ kernel/bpf/btf.c | 4 +- kernel/bpf/core.c | 13 ++++ kernel/bpf/syscall.c | 59 +++++++++++++++-- kernel/bpf/verifier.c | 121 ++++++++++++++++++++++++++++++++++- 8 files changed, 210 insertions(+), 8 deletions(-)
include/linux/bpf.h +4 -1
@@ -312,6 +312,8 @@ struct bpf_prog_aux {void *security;#endif struct bpf_prog_offload *offload; +struct btf *btf; +u32 type_id;/* type id for this prog/func */ union {struct work_struct work;struct rcu_head rcu;
@@ -523,7 +525,8 @@ static inline void bpf_long_memcpy(void *dst, const void *src, u32 size) }/* verify correctness of eBPF program */ -int bpf_check (struct bpf_prog **fp,union bpf_attr *attr); +int bpf_check (struct bpf_prog **fp,union bpf_attr *attr, +union bpf_attr __user *uattr);void bpf_patch_call_args (struct bpf_insn *insn,u32 stack_depth);/* Map specifics */
include/linux/bpf_verifier.h +1 -0
@@ -204,6 +204,7 @@ static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log)struct bpf_subprog_info {u32 start;/* insn idx of function entry point */ u16 stack_depth;/* max. stack depth used by this function */ +u32 type_id;/* btf type_id for this subprog */ };/* single container for all structs
include/linux/btf.h +2 -0
@@ -46,5 +46,7 @@ void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, struct seq_file *m);int btf_get_fd_by_id (u32 id);u32 btf_id (const struct btf *btf); +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);#endif
include/uapi/linux/bpf.h +13 -0
@@ -335,6 +335,10 @@ union bpf_attr {* (context accesses, allowed helpers, etc). */ __u32 expected_attach_type; +__u32 prog_btf_fd;/* fd pointing to BTF type data */ +__u32 func_info_rec_size;/* userspace bpf_func_info size */ +__aligned_u64 func_info;/* func info */ +__u32 func_info_cnt;/* number of bpf_func_info records */ };struct {/* anonymous struct used by BPF_OBJ_* commands */
@@ -2631,6 +2635,10 @@ struct bpf_prog_info {__u32 nr_jited_func_lens;__aligned_u64 jited_ksyms;__aligned_u64 jited_func_lens; +__u32 btf_id; +__u32 func_info_rec_size; +__aligned_u64 func_info; +__u32 func_info_cnt; } __attribute__((aligned(8)));struct bpf_map_info {
@@ -2942,4 +2950,9 @@ struct bpf_flow_keys { }; }; +struct bpf_func_info { +__u32 insn_offset; +__u32 type_id; +}; +#endif /* _UAPI__LINUX_BPF_H__ */
kernel/bpf/btf.c +2 -2
@@ -477,7 +477,7 @@ static bool btf_name_valid_identifier(const struct btf *btf, u32 offset)return !*src; } -static const char *btf_name_by_offset (const struct btf *btf,u32 offset) +const char *btf_name_by_offset (const struct btf *btf,u32 offset) {if (!offset)return "(anon)" ;
@@ -487,7 +487,7 @@ static const char *btf_name_by_offset(const struct btf *btf, u32 offset)return "(invalid-name-offset)" ; } -static const struct btf_type *btf_type_by_id (const struct btf *btf,u32 type_id) +const struct btf_type *btf_type_by_id (const struct btf *btf,u32 type_id) {if (type_id > btf->nr_types)return NULL ;
kernel/bpf/core.c +13 -0
@@ -21,12 +21,14 @@* Kris Katterjohn - Added many additional checks in bpf_check_classic() */ +#include <uapi/linux/btf.h> #include <linux/filter.h> #include <linux/skbuff.h> #include <linux/vmalloc.h> #include <linux/random.h> #include <linux/moduleloader.h> #include <linux/bpf.h> +#include <linux/btf.h> #include <linux/frame.h> #include <linux/rbtree_latch.h> #include <linux/kallsyms.h>
@@ -390,6 +392,8 @@ bpf_get_prog_addr_region(const struct bpf_prog *prog,static void bpf_get_prog_name (const struct bpf_prog *prog,char *sym) {const char *end = sym +KSYM_NAME_LEN ; +const struct btf_type *type; +const char *func_name; BUILD_BUG_ON(sizeof("bpf_prog_") + sizeof(prog->tag) * 2 +
@@ -404,6 +408,15 @@ static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym) sym +=snprintf (sym,KSYM_NAME_LEN ,"bpf_prog_" ); sym =bin2hex (sym, prog->tag,sizeof (prog->tag)); + +/* prog->aux->name will be ignored if full btf name is available */ +if (prog->aux->btf) { + type =btf_type_by_id (prog->aux->btf, prog->aux->type_id); + func_name =btf_name_by_offset (prog->aux->btf, type->name_off); +snprintf (sym, (size_t )(end - sym),"_%s" , func_name); +return ; + } +if (prog->aux->name[0 ])snprintf (sym, (size_t )(end - sym),"_%s" , prog->aux->name);else
kernel/bpf/syscall.c +55 -4
96 lines not on this page · raw message
kernel/bpf/verifier.c +120 -1
155 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v4 6/13 tools/bpf: sync kernel uapi bpf.h header to tools directory
In reply to Yonghong SongThe kernel uapi bpf.h is synced to tools directory.
--- tools/include/uapi/linux/bpf.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)
tools/include/uapi/linux/bpf.h +13 -0
@@ -335,6 +335,10 @@ union bpf_attr {* (context accesses, allowed helpers, etc). */ __u32 expected_attach_type; +__u32 prog_btf_fd;/* fd pointing to BTF type data */ +__u32 func_info_rec_size;/* userspace bpf_func_info size */ +__aligned_u64 func_info;/* func info */ +__u32 func_info_cnt;/* number of bpf_func_info records */ };struct {/* anonymous struct used by BPF_OBJ_* commands */
@@ -2631,6 +2635,10 @@ struct bpf_prog_info {__u32 nr_jited_func_lens;__aligned_u64 jited_ksyms;__aligned_u64 jited_func_lens; +__u32 btf_id; +__u32 func_info_rec_size; +__aligned_u64 func_info; +__u32 func_info_cnt; } __attribute__((aligned(8)));struct bpf_map_info {
@@ -2942,4 +2950,9 @@ struct bpf_flow_keys { }; }; +struct bpf_func_info { +__u32 insn_offset; +__u32 type_id; +}; +#endif /* _UAPI__LINUX_BPF_H__ */
Signature · 1 line
2.17.1
PATCH v4 3/13 tools/bpf: sync kernel btf.h header
The kernel uapi btf.h is synced to the tools directory.
--- tools/include/uapi/linux/btf.h | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
tools/include/uapi/linux/btf.h +14 -3
@@ -40,7 +40,8 @@ struct btf_type {/* "size" is used by INT, ENUM, STRUCT and UNION. * "size" tells the size of the type it is describing. * -* "type" is used by PTR, TYPEDEF, VOLATILE, CONST and RESTRICT. +* "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT +* and FUNC. * "type" is a type_id referring to another type. */ union {
@@ -64,8 +65,9 @@ struct btf_type {#define BTF_KIND_VOLATILE 9/* Volatile */ #define BTF_KIND_CONST 10/* Const */ #define BTF_KIND_RESTRICT 11/* Restrict */ -#define BTF_KIND_MAX 11 -#define NR_BTF_KINDS 12 +#define BTF_KIND_FUNC 12/* Function */ +#define BTF_KIND_MAX 12 +#define NR_BTF_KINDS 13/* For some specific BTF_KIND, "struct btf_type" is immediately * followed by extra data.
@@ -110,4 +112,13 @@ struct btf_member { __u32 offset; /* offset in bits */ }; +/* BTF_KIND_FUNC is followed by multiple "struct btf_param". +* The exact number of btf_param is stored in the vlen (of the +* info in "struct btf_type"). +*/ +struct btf_param { +__u32 name_off; +__u32 type; +}; +#endif /* _UAPI__LINUX_BTF_H__ */
Signature · 1 line
2.17.1
PATCH v4 4/13 tools/bpf: add btf func unit tests in selftest test_btf
Add several BTF_KIND_FUNC unit tests in bpf selftest test_btf.
--- tools/lib/bpf/btf.c | 3 + tools/testing/selftests/bpf/test_btf.c | 262 +++++++++++++++++++++++++ 2 files changed, 265 insertions(+)
tools/lib/bpf/btf.c +3 -0
@@ -165,6 +165,9 @@ static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log)case BTF_KIND_ENUM : next_type += vlen *sizeof (struct btf_enum );break ; +case BTF_KIND_FUNC : + next_type += vlen *sizeof (struct btf_param ); +break ;case BTF_KIND_TYPEDEF :case BTF_KIND_PTR :case BTF_KIND_FWD :
tools/testing/selftests/bpf/test_btf.c +262 -0
268 lines not on this page · raw message
Signature · 1 line
2.17.1