Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

libbpf: Implement bpf_usdt_arg_size BPF function

Information about USDT argument size is implicitly stored in
__bpf_usdt_arg_spec, but currently it's not accessbile to BPF programs
that use USDT.

Implement bpf_sdt_arg_size() that returns the size of an USDT argument
in bytes.

v1->v2:
* do not add __bpf_usdt_arg_spec() helper

v1: https://lore.kernel.org/bpf/20250220215904.3362709-1-ihor.solodrai@linux.dev/

Suggested-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Reviewed-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/bpf/20250224235756.2612606-1-ihor.solodrai@linux.dev

authored by

Ihor Solodrai and committed by
Andrii Nakryiko
b62dff14 4580f4e0

+32
+32
tools/lib/bpf/usdt.bpf.h
··· 108 108 return spec->arg_cnt; 109 109 } 110 110 111 + /* Returns the size in bytes of the #*arg_num* (zero-indexed) USDT argument. 112 + * Returns negative error if argument is not found or arg_num is invalid. 113 + */ 114 + static __always_inline 115 + int bpf_usdt_arg_size(struct pt_regs *ctx, __u64 arg_num) 116 + { 117 + struct __bpf_usdt_arg_spec *arg_spec; 118 + struct __bpf_usdt_spec *spec; 119 + int spec_id; 120 + 121 + spec_id = __bpf_usdt_spec_id(ctx); 122 + if (spec_id < 0) 123 + return -ESRCH; 124 + 125 + spec = bpf_map_lookup_elem(&__bpf_usdt_specs, &spec_id); 126 + if (!spec) 127 + return -ESRCH; 128 + 129 + if (arg_num >= BPF_USDT_MAX_ARG_CNT) 130 + return -ENOENT; 131 + barrier_var(arg_num); 132 + if (arg_num >= spec->arg_cnt) 133 + return -ENOENT; 134 + 135 + arg_spec = &spec->args[arg_num]; 136 + 137 + /* arg_spec->arg_bitshift = 64 - arg_sz * 8 138 + * so: arg_sz = (64 - arg_spec->arg_bitshift) / 8 139 + */ 140 + return (unsigned int)(64 - arg_spec->arg_bitshift) / 8; 141 + } 142 + 111 143 /* Fetch USDT argument #*arg_num* (zero-indexed) and put its value into *res. 112 144 * Returns 0 on success; negative error, otherwise. 113 145 * On error *res is guaranteed to be set to zero.