bpf, btf: store BTF dict id for kernel modules
PATCH 1/1 bpf, btf: store BTF dict id for kernel modules
BPF requires a file descriptor (provided by the kernel) to be associated with BTF type ids for loadble kernel modules. This file descriptor can only be obtained from the kernel based on a BTF dict id for the module, and these ids are assigned dynamically at runtime when modules are loaded.
After kernel modules are initialized for DTrace, the corresponding BTF dict ids are associated with their modules.
--- libdtrace/dt_bpf.c | 58 +++++++++++++++++++++++++++++++++++++++++++-- libdtrace/dt_bpf.h | 4 ++++ libdtrace/dt_btf.c | 55 +++++++++++++++++++++++++++++++++++++++++- libdtrace/dt_btf.h | 5 +++- libdtrace/dt_impl.h | 3 +++ libdtrace/dt_open.c | 2 +- 6 files changed, 122 insertions(+), 5 deletions(-)
libdtrace/dt_bpf.c +54 -0
@@ -121,6 +121,60 @@ dt_bpf_prog_load(const dt_probe_t *prp, const dtrace_difo_t *dp, lvl, buf, sz); } +/* +* Get BTF dict information based on its fd. +*/ +int +dt_bpf_btf_get_info_by_fd (int fd,btf_info_t *info,uint32_t *size) +{ +union bpf_attr attr; +int rc; + +memset (&attr,0 ,sizeof (attr)); + attr.info.bpf_fd = fd; + attr.info.info = (uint64_t )info; + attr.info.info_len = *size; + + rc =dt_bpf (BPF_OBJ_GET_INFO_BY_FD , &attr); +if (rc ==0 ) + *size = attr.info.info_len; + +return rc; +} + +/* +* Get a file descriptor for a BTF dict based on its id. +*/ +int +dt_bpf_btf_get_fd_by_id (uint32_t id) +{ +union bpf_attr attr; + +memset (&attr,0 ,sizeof (attr)); + attr.btf_id = id; + +return dt_bpf (BPF_BTF_GET_FD_BY_ID , &attr); +} + +/* +* Get the id for the "next" BTF dict based on a given id (0 to get the first). +*/ +int +dt_bpf_btf_get_next_id (uint32_t curr,uint32_t *next) +{ +union bpf_attr attr; +int rc; + +memset (&attr,0 ,sizeof (attr)); + attr.start_id = curr; + + rc =dt_bpf (BPF_BTF_GET_NEXT_ID , &attr); +if (rc ==0 ) + *next = attr.next_id; + +return rc; +} +/* * Create a named BPF map. */
libdtrace/dt_bpf.h +4 -0
@@ -12,6 +12,7 @@#include <linux/bpf.h> #include <linux/perf_event.h> #include <dtrace/difo.h> +#include <dt_btf.h> #include <dt_impl.h> struct dtrace_hdl ;
@@ -65,6 +66,9 @@ extern int dt_bpf(enum bpf_cmd cmd, union bpf_attr *attr);extern int dt_bpf_gmap_create (struct dtrace_hdl *);extern int dt_bpf_lockmem_error (struct dtrace_hdl *dtp,const char *msg); +extern int dt_bpf_btf_get_info_by_fd (int fd,btf_info_t *info,uint32_t *size); +extern int dt_bpf_btf_get_fd_by_id (uint32_t id); +extern int dt_bpf_btf_get_next_id (uint32_t curr,uint32_t *next);extern int dt_bpf_map_lookup (int fd,const void *key,void *val);extern int dt_bpf_map_next_key (int fd,const void *key,void *nxt);extern int dt_bpf_map_update (int fd,const void *key,const void *val);
libdtrace/dt_btf.c +54 -1
64 lines not on this page · raw message
libdtrace/dt_btf.h +4 -1
@@ -15,7 +15,8 @@extern "C" {#endif -typedef struct dt_btf dt_btf_t ; +typedef struct dt_btf dt_btf_t ; +typedef struct bpf_btf_info btf_info_t ;extern void dt_btf_destroy (dtrace_hdl_t *,dt_btf_t *);extern dt_btf_t *dt_btf_load_module (dtrace_hdl_t *,dt_module_t *);
@@ -23,6 +24,8 @@ extern ctf_dict_t *dt_btf_module_ctf(dtrace_hdl_t *, dt_module_t *);extern const char *dt_btf_get_string (dtrace_hdl_t *,dt_btf_t *,uint32_t );extern int32_t dt_btf_lookup_name_kind (dtrace_hdl_t *,dt_btf_t *,const char *,uint32_t ); +extern int dt_btf_get_module_ids (dtrace_hdl_t *); +extern int dt_btf_module_fd (const dt_module_t *); #ifdef __cplusplus }
libdtrace/dt_impl.h +3 -0
@@ -159,6 +159,7 @@ typedef struct dt_module {ctf_sect_t dm_ctdata;/* CTF data for module */ ctf_sect_t dm_symtab;/* symbol table */ ctf_sect_t dm_strtab;/* string table */ +uint32_t dm_btf_id;/* BTF dict id */ struct dt_btf *dm_btf;/* BTF data for module */ /*
@@ -339,6 +340,8 @@ struct dtrace_hdl {char *dt_ctfa_path;/* path to vmlinux.ctfa */ ctf_archive_t *dt_ctfa;/* ctf archive for the entire kernel tree */ struct dt_btf *dt_shared_btf;/* BTF data for the kernel (shared) */ +int dt_btf_cnt;/* BTF id count */ +int *dt_btf_fds;/* BTF id-to-fd mapping */ ctf_file_t *dt_shared_ctf;/* Handle to the shared CTF */ dt_htab_t *dt_kernpaths;/* hash table of dt_kern_path_t's */ dt_module_t *dt_exec;/* pointer to executable module */
libdtrace/dt_open.c +1 -1
@@ -29,7 +29,6 @@#include <dt_impl.h> #include <dt_bpf.h> -#include <dt_btf.h> #include <dt_pcap.h> #include <dt_program.h> #include <dt_module.h>
@@ -1155,6 +1154,7 @@ dt_vopen(int version, int flags, int *errp,return set_open_errno (dtp, errp, dtp->dt_errno);dt_bpf_init (dtp); +dt_btf_get_module_ids (dtp);return dtp; }
Signature · 1 line
2.42.0
Quoted 2 lines · Kris Van Hees
BPF requires a file descriptor (provided by the kernel) to be associated with BTF type ids for loadble kernel modules. This file descriptor can
s/loadble/loadable/
Quoted 3 lines
only be obtained from the kernel based on a BTF dict id for the module, and these ids are assigned dynamically at runtime when modules are loaded.
... were they *trying* to make this unnecessarily painful to use?
Quoted 1 line
Signed-off-by Kris Van Hees <[email protected]>
with a few niggles
libdtrace/dt_btf.h line 19
Quoted 11 lines
libdtrace/dt_btf.h +2
-1@@ -15,7 +15,8 @@extern "C" {#endif -typedef struct dt_btf dt_btf_t ; +typedef struct dt_btf dt_btf_t ; +typedef struct bpf_btf_info btf_info_t ;
This sort of thing will start throwing compiler errors whenever any user #includes the header that gives the concrete definition of any of those types, because we're probably typedeffing them there too, and unlike struct definitions you cannot repeat typedefs.
(Not a problem now, but just noting that this sort of pattern tends to store up trouble for the future.)
libdtrace/dt_btf.h struct dtrace_hdl { · line 347
Quoted 9 lines
libdtrace/dt_btf.h +2
-0@@ -339,6 +340,8 @@ struct dtrace_hdl {char *dt_ctfa_path;/* path to vmlinux.ctfa */ ctf_archive_t *dt_ctfa;/* ctf archive for the entire kernel tree */ struct dt_btf *dt_shared_btf;/* BTF data for the kernel (shared) */ +int dt_btf_cnt;/* BTF id count */ +int *dt_btf_fds;/* BTF id-to-fd mapping */ ctf_file_t *dt_shared_ctf;/* Handle to the shared CTF */ dt_htab_t *dt_kernpaths;/* hash table of dt_kern_path_t's */ dt_module_t *dt_exec;/* pointer to executable module */
These don't seem to be used anywhere in this patch: did you swap between storing globally and storing per-module?
Signature · 1 line
NULL && (void)