Skip to content

Commit 441e8c6

Browse files
tohojoAlexei Starovoitov
authored andcommitted
bpf: Return target info when a tracing bpf_link is queried
There is currently no way to discover the target of a tracing program attachment after the fact. Add this information to bpf_link_info and return it when querying the bpf_link fd. Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20210413091607.58945-1-toke@redhat.com
1 parent db16c1f commit 441e8c6

4 files changed

Lines changed: 16 additions & 0 deletions

File tree

‎include/linux/bpf_verifier.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,15 @@ static inline u64 bpf_trampoline_compute_key(const struct bpf_prog *tgt_prog,
487487
return ((u64)btf_obj_id(btf) << 32) | 0x80000000 | btf_id;
488488
}
489489

490+
/* unpack the IDs from the key as constructed above */
491+
static inline void bpf_trampoline_unpack_key(u64 key, u32 *obj_id, u32 *btf_id)
492+
{
493+
if (obj_id)
494+
*obj_id = key >> 32;
495+
if (btf_id)
496+
*btf_id = key & 0x7FFFFFFF;
497+
}
498+
490499
int bpf_check_attach_target(struct bpf_verifier_log *log,
491500
const struct bpf_prog *prog,
492501
const struct bpf_prog *tgt_prog,

‎include/uapi/linux/bpf.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5416,6 +5416,8 @@ struct bpf_link_info {
54165416
} raw_tracepoint;
54175417
struct {
54185418
__u32 attach_type;
5419+
__u32 target_obj_id; /* prog_id for PROG_EXT, otherwise btf object id */
5420+
__u32 target_btf_id; /* BTF type id inside the object */
54195421
} tracing;
54205422
struct {
54215423
__u64 cgroup_id;

‎kernel/bpf/syscall.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,6 +2551,9 @@ static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
25512551
container_of(link, struct bpf_tracing_link, link);
25522552

25532553
info->tracing.attach_type = tr_link->attach_type;
2554+
bpf_trampoline_unpack_key(tr_link->trampoline->key,
2555+
&info->tracing.target_obj_id,
2556+
&info->tracing.target_btf_id);
25542557

25552558
return 0;
25562559
}

‎tools/include/uapi/linux/bpf.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5416,6 +5416,8 @@ struct bpf_link_info {
54165416
} raw_tracepoint;
54175417
struct {
54185418
__u32 attach_type;
5419+
__u32 target_obj_id; /* prog_id for PROG_EXT, otherwise btf object id */
5420+
__u32 target_btf_id; /* BTF type id inside the object */
54195421
} tracing;
54205422
struct {
54215423
__u64 cgroup_id;

0 commit comments

Comments
 (0)