* [PATCH bpf-next v2 1/2] libbpf: implement bpf_usdt_arg_size BPF function
@ 2025-02-24 23:57 Ihor Solodrai
2025-02-24 23:57 ` [PATCH bpf-next v2 2/2] selftests/bpf: test bpf_usdt_arg_size() function Ihor Solodrai
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Ihor Solodrai @ 2025-02-24 23:57 UTC (permalink / raw)
To: bpf; +Cc: andrii, ast, daniel, eddyz87, mykolal, kernel-team
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>
---
tools/lib/bpf/usdt.bpf.h | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/tools/lib/bpf/usdt.bpf.h b/tools/lib/bpf/usdt.bpf.h
index b811f754939f..2a7865c8e3fe 100644
--- a/tools/lib/bpf/usdt.bpf.h
+++ b/tools/lib/bpf/usdt.bpf.h
@@ -108,6 +108,38 @@ int bpf_usdt_arg_cnt(struct pt_regs *ctx)
return spec->arg_cnt;
}
+/* Returns the size in bytes of the #*arg_num* (zero-indexed) USDT argument.
+ * Returns negative error if argument is not found or arg_num is invalid.
+ */
+static __always_inline
+int bpf_usdt_arg_size(struct pt_regs *ctx, __u64 arg_num)
+{
+ struct __bpf_usdt_arg_spec *arg_spec;
+ struct __bpf_usdt_spec *spec;
+ int spec_id;
+
+ spec_id = __bpf_usdt_spec_id(ctx);
+ if (spec_id < 0)
+ return -ESRCH;
+
+ spec = bpf_map_lookup_elem(&__bpf_usdt_specs, &spec_id);
+ if (!spec)
+ return -ESRCH;
+
+ if (arg_num >= BPF_USDT_MAX_ARG_CNT)
+ return -ENOENT;
+ barrier_var(arg_num);
+ if (arg_num >= spec->arg_cnt)
+ return -ENOENT;
+
+ arg_spec = &spec->args[arg_num];
+
+ /* arg_spec->arg_bitshift = 64 - arg_sz * 8
+ * so: arg_sz = (64 - arg_spec->arg_bitshift) / 8
+ */
+ return (unsigned int)(64 - arg_spec->arg_bitshift) / 8;
+}
+
/* Fetch USDT argument #*arg_num* (zero-indexed) and put its value into *res.
* Returns 0 on success; negative error, otherwise.
* On error *res is guaranteed to be set to zero.
--
2.48.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH bpf-next v2 2/2] selftests/bpf: test bpf_usdt_arg_size() function
2025-02-24 23:57 [PATCH bpf-next v2 1/2] libbpf: implement bpf_usdt_arg_size BPF function Ihor Solodrai
@ 2025-02-24 23:57 ` Ihor Solodrai
2025-02-26 14:36 ` Jiri Olsa
2025-02-26 14:34 ` [PATCH bpf-next v2 1/2] libbpf: implement bpf_usdt_arg_size BPF function Jiri Olsa
2025-02-26 18:20 ` patchwork-bot+netdevbpf
2 siblings, 1 reply; 5+ messages in thread
From: Ihor Solodrai @ 2025-02-24 23:57 UTC (permalink / raw)
To: bpf; +Cc: andrii, ast, daniel, eddyz87, mykolal, kernel-team
Update usdt tests to also check for correct behavior of
bpf_usdt_arg_size().
Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
---
tools/testing/selftests/bpf/prog_tests/usdt.c | 11 ++++++++++-
tools/testing/selftests/bpf/progs/test_usdt.c | 14 ++++++++++++++
2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/usdt.c b/tools/testing/selftests/bpf/prog_tests/usdt.c
index 56ed1eb9b527..495d66414b57 100644
--- a/tools/testing/selftests/bpf/prog_tests/usdt.c
+++ b/tools/testing/selftests/bpf/prog_tests/usdt.c
@@ -45,7 +45,7 @@ static void subtest_basic_usdt(void)
LIBBPF_OPTS(bpf_usdt_opts, opts);
struct test_usdt *skel;
struct test_usdt__bss *bss;
- int err;
+ int err, i;
skel = test_usdt__open_and_load();
if (!ASSERT_OK_PTR(skel, "skel_open"))
@@ -75,6 +75,7 @@ static void subtest_basic_usdt(void)
ASSERT_EQ(bss->usdt0_cookie, 0xcafedeadbeeffeed, "usdt0_cookie");
ASSERT_EQ(bss->usdt0_arg_cnt, 0, "usdt0_arg_cnt");
ASSERT_EQ(bss->usdt0_arg_ret, -ENOENT, "usdt0_arg_ret");
+ ASSERT_EQ(bss->usdt0_arg_size, -ENOENT, "usdt0_arg_size");
/* auto-attached usdt3 gets default zero cookie value */
ASSERT_EQ(bss->usdt3_cookie, 0, "usdt3_cookie");
@@ -86,6 +87,9 @@ static void subtest_basic_usdt(void)
ASSERT_EQ(bss->usdt3_args[0], 1, "usdt3_arg1");
ASSERT_EQ(bss->usdt3_args[1], 42, "usdt3_arg2");
ASSERT_EQ(bss->usdt3_args[2], (uintptr_t)&bla, "usdt3_arg3");
+ ASSERT_EQ(bss->usdt3_arg_sizes[0], 4, "usdt3_arg1_size");
+ ASSERT_EQ(bss->usdt3_arg_sizes[1], 8, "usdt3_arg2_size");
+ ASSERT_EQ(bss->usdt3_arg_sizes[2], 8, "usdt3_arg3_size");
/* auto-attached usdt12 gets default zero cookie value */
ASSERT_EQ(bss->usdt12_cookie, 0, "usdt12_cookie");
@@ -104,6 +108,11 @@ static void subtest_basic_usdt(void)
ASSERT_EQ(bss->usdt12_args[10], nums[idx], "usdt12_arg11");
ASSERT_EQ(bss->usdt12_args[11], t1.y, "usdt12_arg12");
+ int usdt12_expected_arg_sizes[12] = { 4, 4, 8, 8, 4, 8, 8, 8, 4, 2, 2, 1 };
+
+ for (i = 0; i < 12; i++)
+ ASSERT_EQ(bss->usdt12_arg_sizes[i], usdt12_expected_arg_sizes[i], "usdt12_arg_size");
+
/* trigger_func() is marked __always_inline, so USDT invocations will be
* inlined in two different places, meaning that each USDT will have
* at least 2 different places to be attached to. This verifies that
diff --git a/tools/testing/selftests/bpf/progs/test_usdt.c b/tools/testing/selftests/bpf/progs/test_usdt.c
index 505aab9a5234..096488f47fbc 100644
--- a/tools/testing/selftests/bpf/progs/test_usdt.c
+++ b/tools/testing/selftests/bpf/progs/test_usdt.c
@@ -11,6 +11,7 @@ int usdt0_called;
u64 usdt0_cookie;
int usdt0_arg_cnt;
int usdt0_arg_ret;
+int usdt0_arg_size;
SEC("usdt")
int usdt0(struct pt_regs *ctx)
@@ -26,6 +27,7 @@ int usdt0(struct pt_regs *ctx)
usdt0_arg_cnt = bpf_usdt_arg_cnt(ctx);
/* should return -ENOENT for any arg_num */
usdt0_arg_ret = bpf_usdt_arg(ctx, bpf_get_prandom_u32(), &tmp);
+ usdt0_arg_size = bpf_usdt_arg_size(ctx, bpf_get_prandom_u32());
return 0;
}
@@ -34,6 +36,7 @@ u64 usdt3_cookie;
int usdt3_arg_cnt;
int usdt3_arg_rets[3];
u64 usdt3_args[3];
+int usdt3_arg_sizes[3];
SEC("usdt//proc/self/exe:test:usdt3")
int usdt3(struct pt_regs *ctx)
@@ -50,12 +53,15 @@ int usdt3(struct pt_regs *ctx)
usdt3_arg_rets[0] = bpf_usdt_arg(ctx, 0, &tmp);
usdt3_args[0] = (int)tmp;
+ usdt3_arg_sizes[0] = bpf_usdt_arg_size(ctx, 0);
usdt3_arg_rets[1] = bpf_usdt_arg(ctx, 1, &tmp);
usdt3_args[1] = (long)tmp;
+ usdt3_arg_sizes[1] = bpf_usdt_arg_size(ctx, 1);
usdt3_arg_rets[2] = bpf_usdt_arg(ctx, 2, &tmp);
usdt3_args[2] = (uintptr_t)tmp;
+ usdt3_arg_sizes[2] = bpf_usdt_arg_size(ctx, 2);
return 0;
}
@@ -64,12 +70,15 @@ int usdt12_called;
u64 usdt12_cookie;
int usdt12_arg_cnt;
u64 usdt12_args[12];
+int usdt12_arg_sizes[12];
SEC("usdt//proc/self/exe:test:usdt12")
int BPF_USDT(usdt12, int a1, int a2, long a3, long a4, unsigned a5,
long a6, __u64 a7, uintptr_t a8, int a9, short a10,
short a11, signed char a12)
{
+ int i;
+
if (my_pid != (bpf_get_current_pid_tgid() >> 32))
return 0;
@@ -90,6 +99,11 @@ int BPF_USDT(usdt12, int a1, int a2, long a3, long a4, unsigned a5,
usdt12_args[9] = a10;
usdt12_args[10] = a11;
usdt12_args[11] = a12;
+
+ bpf_for(i, 0, 12) {
+ usdt12_arg_sizes[i] = bpf_usdt_arg_size(ctx, i);
+ }
+
return 0;
}
--
2.48.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH bpf-next v2 1/2] libbpf: implement bpf_usdt_arg_size BPF function
2025-02-24 23:57 [PATCH bpf-next v2 1/2] libbpf: implement bpf_usdt_arg_size BPF function Ihor Solodrai
2025-02-24 23:57 ` [PATCH bpf-next v2 2/2] selftests/bpf: test bpf_usdt_arg_size() function Ihor Solodrai
@ 2025-02-26 14:34 ` Jiri Olsa
2025-02-26 18:20 ` patchwork-bot+netdevbpf
2 siblings, 0 replies; 5+ messages in thread
From: Jiri Olsa @ 2025-02-26 14:34 UTC (permalink / raw)
To: Ihor Solodrai; +Cc: bpf, andrii, ast, daniel, eddyz87, mykolal, kernel-team
On Mon, Feb 24, 2025 at 03:57:55PM -0800, Ihor Solodrai wrote:
> 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
Reviewed-by: Jiri Olsa <jolsa@kernel.org>
jirka
>
> 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>
> ---
> tools/lib/bpf/usdt.bpf.h | 32 ++++++++++++++++++++++++++++++++
> 1 file changed, 32 insertions(+)
>
> diff --git a/tools/lib/bpf/usdt.bpf.h b/tools/lib/bpf/usdt.bpf.h
> index b811f754939f..2a7865c8e3fe 100644
> --- a/tools/lib/bpf/usdt.bpf.h
> +++ b/tools/lib/bpf/usdt.bpf.h
> @@ -108,6 +108,38 @@ int bpf_usdt_arg_cnt(struct pt_regs *ctx)
> return spec->arg_cnt;
> }
>
> +/* Returns the size in bytes of the #*arg_num* (zero-indexed) USDT argument.
> + * Returns negative error if argument is not found or arg_num is invalid.
> + */
> +static __always_inline
> +int bpf_usdt_arg_size(struct pt_regs *ctx, __u64 arg_num)
> +{
> + struct __bpf_usdt_arg_spec *arg_spec;
> + struct __bpf_usdt_spec *spec;
> + int spec_id;
> +
> + spec_id = __bpf_usdt_spec_id(ctx);
> + if (spec_id < 0)
> + return -ESRCH;
> +
> + spec = bpf_map_lookup_elem(&__bpf_usdt_specs, &spec_id);
> + if (!spec)
> + return -ESRCH;
> +
> + if (arg_num >= BPF_USDT_MAX_ARG_CNT)
> + return -ENOENT;
> + barrier_var(arg_num);
> + if (arg_num >= spec->arg_cnt)
> + return -ENOENT;
> +
> + arg_spec = &spec->args[arg_num];
> +
> + /* arg_spec->arg_bitshift = 64 - arg_sz * 8
> + * so: arg_sz = (64 - arg_spec->arg_bitshift) / 8
> + */
> + return (unsigned int)(64 - arg_spec->arg_bitshift) / 8;
> +}
> +
> /* Fetch USDT argument #*arg_num* (zero-indexed) and put its value into *res.
> * Returns 0 on success; negative error, otherwise.
> * On error *res is guaranteed to be set to zero.
> --
> 2.48.1
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH bpf-next v2 2/2] selftests/bpf: test bpf_usdt_arg_size() function
2025-02-24 23:57 ` [PATCH bpf-next v2 2/2] selftests/bpf: test bpf_usdt_arg_size() function Ihor Solodrai
@ 2025-02-26 14:36 ` Jiri Olsa
0 siblings, 0 replies; 5+ messages in thread
From: Jiri Olsa @ 2025-02-26 14:36 UTC (permalink / raw)
To: Ihor Solodrai; +Cc: bpf, andrii, ast, daniel, eddyz87, mykolal, kernel-team
On Mon, Feb 24, 2025 at 03:57:56PM -0800, Ihor Solodrai wrote:
> Update usdt tests to also check for correct behavior of
> bpf_usdt_arg_size().
>
> Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
Reviewed-by: Jiri Olsa <jolsa@kernel.org>
jirka
> ---
> tools/testing/selftests/bpf/prog_tests/usdt.c | 11 ++++++++++-
> tools/testing/selftests/bpf/progs/test_usdt.c | 14 ++++++++++++++
> 2 files changed, 24 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/usdt.c b/tools/testing/selftests/bpf/prog_tests/usdt.c
> index 56ed1eb9b527..495d66414b57 100644
> --- a/tools/testing/selftests/bpf/prog_tests/usdt.c
> +++ b/tools/testing/selftests/bpf/prog_tests/usdt.c
> @@ -45,7 +45,7 @@ static void subtest_basic_usdt(void)
> LIBBPF_OPTS(bpf_usdt_opts, opts);
> struct test_usdt *skel;
> struct test_usdt__bss *bss;
> - int err;
> + int err, i;
>
> skel = test_usdt__open_and_load();
> if (!ASSERT_OK_PTR(skel, "skel_open"))
> @@ -75,6 +75,7 @@ static void subtest_basic_usdt(void)
> ASSERT_EQ(bss->usdt0_cookie, 0xcafedeadbeeffeed, "usdt0_cookie");
> ASSERT_EQ(bss->usdt0_arg_cnt, 0, "usdt0_arg_cnt");
> ASSERT_EQ(bss->usdt0_arg_ret, -ENOENT, "usdt0_arg_ret");
> + ASSERT_EQ(bss->usdt0_arg_size, -ENOENT, "usdt0_arg_size");
>
> /* auto-attached usdt3 gets default zero cookie value */
> ASSERT_EQ(bss->usdt3_cookie, 0, "usdt3_cookie");
> @@ -86,6 +87,9 @@ static void subtest_basic_usdt(void)
> ASSERT_EQ(bss->usdt3_args[0], 1, "usdt3_arg1");
> ASSERT_EQ(bss->usdt3_args[1], 42, "usdt3_arg2");
> ASSERT_EQ(bss->usdt3_args[2], (uintptr_t)&bla, "usdt3_arg3");
> + ASSERT_EQ(bss->usdt3_arg_sizes[0], 4, "usdt3_arg1_size");
> + ASSERT_EQ(bss->usdt3_arg_sizes[1], 8, "usdt3_arg2_size");
> + ASSERT_EQ(bss->usdt3_arg_sizes[2], 8, "usdt3_arg3_size");
>
> /* auto-attached usdt12 gets default zero cookie value */
> ASSERT_EQ(bss->usdt12_cookie, 0, "usdt12_cookie");
> @@ -104,6 +108,11 @@ static void subtest_basic_usdt(void)
> ASSERT_EQ(bss->usdt12_args[10], nums[idx], "usdt12_arg11");
> ASSERT_EQ(bss->usdt12_args[11], t1.y, "usdt12_arg12");
>
> + int usdt12_expected_arg_sizes[12] = { 4, 4, 8, 8, 4, 8, 8, 8, 4, 2, 2, 1 };
> +
> + for (i = 0; i < 12; i++)
> + ASSERT_EQ(bss->usdt12_arg_sizes[i], usdt12_expected_arg_sizes[i], "usdt12_arg_size");
> +
> /* trigger_func() is marked __always_inline, so USDT invocations will be
> * inlined in two different places, meaning that each USDT will have
> * at least 2 different places to be attached to. This verifies that
> diff --git a/tools/testing/selftests/bpf/progs/test_usdt.c b/tools/testing/selftests/bpf/progs/test_usdt.c
> index 505aab9a5234..096488f47fbc 100644
> --- a/tools/testing/selftests/bpf/progs/test_usdt.c
> +++ b/tools/testing/selftests/bpf/progs/test_usdt.c
> @@ -11,6 +11,7 @@ int usdt0_called;
> u64 usdt0_cookie;
> int usdt0_arg_cnt;
> int usdt0_arg_ret;
> +int usdt0_arg_size;
>
> SEC("usdt")
> int usdt0(struct pt_regs *ctx)
> @@ -26,6 +27,7 @@ int usdt0(struct pt_regs *ctx)
> usdt0_arg_cnt = bpf_usdt_arg_cnt(ctx);
> /* should return -ENOENT for any arg_num */
> usdt0_arg_ret = bpf_usdt_arg(ctx, bpf_get_prandom_u32(), &tmp);
> + usdt0_arg_size = bpf_usdt_arg_size(ctx, bpf_get_prandom_u32());
> return 0;
> }
>
> @@ -34,6 +36,7 @@ u64 usdt3_cookie;
> int usdt3_arg_cnt;
> int usdt3_arg_rets[3];
> u64 usdt3_args[3];
> +int usdt3_arg_sizes[3];
>
> SEC("usdt//proc/self/exe:test:usdt3")
> int usdt3(struct pt_regs *ctx)
> @@ -50,12 +53,15 @@ int usdt3(struct pt_regs *ctx)
>
> usdt3_arg_rets[0] = bpf_usdt_arg(ctx, 0, &tmp);
> usdt3_args[0] = (int)tmp;
> + usdt3_arg_sizes[0] = bpf_usdt_arg_size(ctx, 0);
>
> usdt3_arg_rets[1] = bpf_usdt_arg(ctx, 1, &tmp);
> usdt3_args[1] = (long)tmp;
> + usdt3_arg_sizes[1] = bpf_usdt_arg_size(ctx, 1);
>
> usdt3_arg_rets[2] = bpf_usdt_arg(ctx, 2, &tmp);
> usdt3_args[2] = (uintptr_t)tmp;
> + usdt3_arg_sizes[2] = bpf_usdt_arg_size(ctx, 2);
>
> return 0;
> }
> @@ -64,12 +70,15 @@ int usdt12_called;
> u64 usdt12_cookie;
> int usdt12_arg_cnt;
> u64 usdt12_args[12];
> +int usdt12_arg_sizes[12];
>
> SEC("usdt//proc/self/exe:test:usdt12")
> int BPF_USDT(usdt12, int a1, int a2, long a3, long a4, unsigned a5,
> long a6, __u64 a7, uintptr_t a8, int a9, short a10,
> short a11, signed char a12)
> {
> + int i;
> +
> if (my_pid != (bpf_get_current_pid_tgid() >> 32))
> return 0;
>
> @@ -90,6 +99,11 @@ int BPF_USDT(usdt12, int a1, int a2, long a3, long a4, unsigned a5,
> usdt12_args[9] = a10;
> usdt12_args[10] = a11;
> usdt12_args[11] = a12;
> +
> + bpf_for(i, 0, 12) {
> + usdt12_arg_sizes[i] = bpf_usdt_arg_size(ctx, i);
> + }
> +
> return 0;
> }
>
> --
> 2.48.1
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH bpf-next v2 1/2] libbpf: implement bpf_usdt_arg_size BPF function
2025-02-24 23:57 [PATCH bpf-next v2 1/2] libbpf: implement bpf_usdt_arg_size BPF function Ihor Solodrai
2025-02-24 23:57 ` [PATCH bpf-next v2 2/2] selftests/bpf: test bpf_usdt_arg_size() function Ihor Solodrai
2025-02-26 14:34 ` [PATCH bpf-next v2 1/2] libbpf: implement bpf_usdt_arg_size BPF function Jiri Olsa
@ 2025-02-26 18:20 ` patchwork-bot+netdevbpf
2 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-02-26 18:20 UTC (permalink / raw)
To: Ihor Solodrai; +Cc: bpf, andrii, ast, daniel, eddyz87, mykolal, kernel-team
Hello:
This series was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:
On Mon, 24 Feb 2025 15:57:55 -0800 you wrote:
> 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.
>
> [...]
Here is the summary with links:
- [bpf-next,v2,1/2] libbpf: implement bpf_usdt_arg_size BPF function
https://git.kernel.org/bpf/bpf-next/c/b62dff14402a
- [bpf-next,v2,2/2] selftests/bpf: test bpf_usdt_arg_size() function
https://git.kernel.org/bpf/bpf-next/c/0ba0ef012eba
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-02-26 18:19 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-24 23:57 [PATCH bpf-next v2 1/2] libbpf: implement bpf_usdt_arg_size BPF function Ihor Solodrai
2025-02-24 23:57 ` [PATCH bpf-next v2 2/2] selftests/bpf: test bpf_usdt_arg_size() function Ihor Solodrai
2025-02-26 14:36 ` Jiri Olsa
2025-02-26 14:34 ` [PATCH bpf-next v2 1/2] libbpf: implement bpf_usdt_arg_size BPF function Jiri Olsa
2025-02-26 18:20 ` patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox