Commit f892cac2 authored by Alexei Starovoitov's avatar Alexei Starovoitov

Merge branch 'bpf-x86-allow-function-arguments-up-to-12-for-tracing'

Menglong Dong says:

====================
bpf, x86: allow function arguments up to 12 for TRACING

From: Menglong Dong <imagedong@tencent.com>

For now, the BPF program of type BPF_PROG_TYPE_TRACING can only be used
on the kernel functions whose arguments count less than or equal to 6, if
not considering '> 8 bytes' struct argument. This is not friendly at all,
as too many functions have arguments count more than 6. According to the
current kernel version, below is a statistics of the function arguments
count:

argument count | function count
7              | 704
8              | 270
9              | 84
10             | 47
11             | 47
12             | 27
13             | 22
14             | 5
15             | 0
16             | 1

Therefore, let's enhance it by increasing the function arguments count
allowed in arch_prepare_bpf_trampoline(), for now, only x86_64.

In the 1st patch, we save/restore regs with BPF_DW size to make the code
in save_regs()/restore_regs() simpler.

In the 2nd patch, we make arch_prepare_bpf_trampoline() support to copy
function arguments in stack for x86 arch. Therefore, the maximum
arguments can be up to MAX_BPF_FUNC_ARGS for FENTRY, FEXIT and
MODIFY_RETURN. Meanwhile, we clean the potential garbage value when we
copy the arguments on-stack.

And the 3rd patch is for the testcases of the this series.

Changes since v9:
- fix the failed test cases of trampoline_count and get_func_args_test
  in the 3rd patch

Changes since v8:
- change the way to test fmod_ret in the 3rd patch

Changes since v7:
- split the testcases, and add fentry_many_args/fexit_many_args to
  DENYLIST.aarch64 in 3rd patch

Changes since v6:
- somit nits from commit message and comment in the 1st patch
- remove the inline in get_nr_regs() in the 1st patch
- rename some function and various in the 1st patch

Changes since v5:
- adjust the commit log of the 1st patch, avoiding confusing people that
  bugs exist in current code
- introduce get_nr_regs() to get the space that used to pass args on
  stack correct in the 2nd patch
- add testcases to tracing_struct.c instead of fentry_test.c and
  fexit_test.c

Changes since v4:
- consider the case of the struct in arguments can't be hold by regs
- add comment for some code
- add testcases for MODIFY_RETURN
- rebase to the latest

Changes since v3:
- try make the stack pointer 16-byte aligned. Not sure if I'm right :)
- introduce clean_garbage() to clean the grabage when argument count is 7
- use different data type in bpf_testmod_fentry_test{7,12}
- add testcase for grabage values in ctx

Changes since v2:
- keep MAX_BPF_FUNC_ARGS still
- clean garbage value in upper bytes in the 2nd patch
- move bpf_fentry_test{7,12} to bpf_testmod.c and rename them to
  bpf_testmod_fentry_test{7,12} meanwhile in the 3rd patch

Changes since v1:
- change the maximun function arguments to 14 from 12
- add testcases (Jiri Olsa)
- instead EMIT4 with EMIT3_off32 for "lea" to prevent overflow
====================

Link: https://lore.kernel.org/r/20230713040738.1789742-1-imagedong@tencent.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents 0a5550b1 5e9cf77d
This diff is collapsed.
......@@ -565,6 +565,13 @@ __bpf_kfunc int bpf_modify_return_test(int a, int *b)
return a + *b;
}
__bpf_kfunc int bpf_modify_return_test2(int a, int *b, short c, int d,
void *e, char f, int g)
{
*b += 1;
return a + *b + c + d + (long)e + f + g;
}
int noinline bpf_fentry_shadow_test(int a)
{
return a + 1;
......@@ -600,6 +607,7 @@ __diag_pop();
BTF_SET8_START(bpf_test_modify_return_ids)
BTF_ID_FLAGS(func, bpf_modify_return_test)
BTF_ID_FLAGS(func, bpf_modify_return_test2)
BTF_ID_FLAGS(func, bpf_fentry_test1, KF_SLEEPABLE)
BTF_SET8_END(bpf_test_modify_return_ids)
......@@ -667,7 +675,11 @@ int bpf_prog_test_run_tracing(struct bpf_prog *prog,
case BPF_MODIFY_RETURN:
ret = bpf_modify_return_test(1, &b);
if (b != 2)
side_effect = 1;
side_effect++;
b = 2;
ret += bpf_modify_return_test2(1, &b, 3, 4, (void *)5, 6, 7);
if (b != 2)
side_effect++;
break;
default:
goto out;
......
......@@ -10,3 +10,5 @@ kprobe_multi_test/link_api_addrs # link_fd unexpected link_fd: a
kprobe_multi_test/link_api_syms # link_fd unexpected link_fd: actual -95 < expected 0
kprobe_multi_test/skel_api # libbpf: failed to load BPF skeleton 'kprobe_multi': -3
module_attach # prog 'kprobe_multi': failed to auto-attach: -95
fentry_test/fentry_many_args # fentry_many_args:FAIL:fentry_many_args_attach unexpected error: -524
fexit_test/fexit_many_args # fexit_many_args:FAIL:fexit_many_args_attach unexpected error: -524
......@@ -34,6 +34,11 @@ struct bpf_testmod_struct_arg_3 {
int b[];
};
struct bpf_testmod_struct_arg_4 {
u64 a;
int b;
};
__diag_push();
__diag_ignore_all("-Wmissing-prototypes",
"Global functions as their definitions will be in bpf_testmod.ko BTF");
......@@ -75,6 +80,24 @@ bpf_testmod_test_struct_arg_6(struct bpf_testmod_struct_arg_3 *a) {
return bpf_testmod_test_struct_arg_result;
}
noinline int
bpf_testmod_test_struct_arg_7(u64 a, void *b, short c, int d, void *e,
struct bpf_testmod_struct_arg_4 f)
{
bpf_testmod_test_struct_arg_result = a + (long)b + c + d +
(long)e + f.a + f.b;
return bpf_testmod_test_struct_arg_result;
}
noinline int
bpf_testmod_test_struct_arg_8(u64 a, void *b, short c, int d, void *e,
struct bpf_testmod_struct_arg_4 f, int g)
{
bpf_testmod_test_struct_arg_result = a + (long)b + c + d +
(long)e + f.a + f.b + g;
return bpf_testmod_test_struct_arg_result;
}
__bpf_kfunc void
bpf_testmod_test_mod_kfunc(int i)
{
......@@ -191,6 +214,20 @@ noinline int bpf_testmod_fentry_test3(char a, int b, u64 c)
return a + b + c;
}
noinline int bpf_testmod_fentry_test7(u64 a, void *b, short c, int d,
void *e, char f, int g)
{
return a + (long)b + c + d + (long)e + f + g;
}
noinline int bpf_testmod_fentry_test11(u64 a, void *b, short c, int d,
void *e, char f, int g,
unsigned int h, long i, __u64 j,
unsigned long k)
{
return a + (long)b + c + d + (long)e + f + g + h + i + j + k;
}
int bpf_testmod_fentry_ok;
noinline ssize_t
......@@ -206,6 +243,7 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj,
struct bpf_testmod_struct_arg_1 struct_arg1 = {10};
struct bpf_testmod_struct_arg_2 struct_arg2 = {2, 3};
struct bpf_testmod_struct_arg_3 *struct_arg3;
struct bpf_testmod_struct_arg_4 struct_arg4 = {21, 22};
int i = 1;
while (bpf_testmod_return_ptr(i))
......@@ -216,6 +254,11 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj,
(void)bpf_testmod_test_struct_arg_3(1, 4, struct_arg2);
(void)bpf_testmod_test_struct_arg_4(struct_arg1, 1, 2, 3, struct_arg2);
(void)bpf_testmod_test_struct_arg_5();
(void)bpf_testmod_test_struct_arg_7(16, (void *)17, 18, 19,
(void *)20, struct_arg4);
(void)bpf_testmod_test_struct_arg_8(16, (void *)17, 18, 19,
(void *)20, struct_arg4, 23);
struct_arg3 = kmalloc((sizeof(struct bpf_testmod_struct_arg_3) +
sizeof(int)), GFP_KERNEL);
......@@ -243,7 +286,11 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj,
if (bpf_testmod_fentry_test1(1) != 2 ||
bpf_testmod_fentry_test2(2, 3) != 5 ||
bpf_testmod_fentry_test3(4, 5, 6) != 15)
bpf_testmod_fentry_test3(4, 5, 6) != 15 ||
bpf_testmod_fentry_test7(16, (void *)17, 18, 19, (void *)20,
21, 22) != 133 ||
bpf_testmod_fentry_test11(16, (void *)17, 18, 19, (void *)20,
21, 22, 23, 24, 25, 26) != 231)
goto out;
bpf_testmod_fentry_ok = 1;
......
......@@ -2,8 +2,9 @@
/* Copyright (c) 2019 Facebook */
#include <test_progs.h>
#include "fentry_test.lskel.h"
#include "fentry_many_args.skel.h"
static int fentry_test(struct fentry_test_lskel *fentry_skel)
static int fentry_test_common(struct fentry_test_lskel *fentry_skel)
{
int err, prog_fd, i;
int link_fd;
......@@ -37,7 +38,7 @@ static int fentry_test(struct fentry_test_lskel *fentry_skel)
return 0;
}
void test_fentry_test(void)
static void fentry_test(void)
{
struct fentry_test_lskel *fentry_skel = NULL;
int err;
......@@ -46,13 +47,47 @@ void test_fentry_test(void)
if (!ASSERT_OK_PTR(fentry_skel, "fentry_skel_load"))
goto cleanup;
err = fentry_test(fentry_skel);
err = fentry_test_common(fentry_skel);
if (!ASSERT_OK(err, "fentry_first_attach"))
goto cleanup;
err = fentry_test(fentry_skel);
err = fentry_test_common(fentry_skel);
ASSERT_OK(err, "fentry_second_attach");
cleanup:
fentry_test_lskel__destroy(fentry_skel);
}
static void fentry_many_args(void)
{
struct fentry_many_args *fentry_skel = NULL;
int err;
fentry_skel = fentry_many_args__open_and_load();
if (!ASSERT_OK_PTR(fentry_skel, "fentry_many_args_skel_load"))
goto cleanup;
err = fentry_many_args__attach(fentry_skel);
if (!ASSERT_OK(err, "fentry_many_args_attach"))
goto cleanup;
ASSERT_OK(trigger_module_test_read(1), "trigger_read");
ASSERT_EQ(fentry_skel->bss->test1_result, 1,
"fentry_many_args_result1");
ASSERT_EQ(fentry_skel->bss->test2_result, 1,
"fentry_many_args_result2");
ASSERT_EQ(fentry_skel->bss->test3_result, 1,
"fentry_many_args_result3");
cleanup:
fentry_many_args__destroy(fentry_skel);
}
void test_fentry_test(void)
{
if (test__start_subtest("fentry"))
fentry_test();
if (test__start_subtest("fentry_many_args"))
fentry_many_args();
}
......@@ -2,8 +2,9 @@
/* Copyright (c) 2019 Facebook */
#include <test_progs.h>
#include "fexit_test.lskel.h"
#include "fexit_many_args.skel.h"
static int fexit_test(struct fexit_test_lskel *fexit_skel)
static int fexit_test_common(struct fexit_test_lskel *fexit_skel)
{
int err, prog_fd, i;
int link_fd;
......@@ -37,7 +38,7 @@ static int fexit_test(struct fexit_test_lskel *fexit_skel)
return 0;
}
void test_fexit_test(void)
static void fexit_test(void)
{
struct fexit_test_lskel *fexit_skel = NULL;
int err;
......@@ -46,13 +47,47 @@ void test_fexit_test(void)
if (!ASSERT_OK_PTR(fexit_skel, "fexit_skel_load"))
goto cleanup;
err = fexit_test(fexit_skel);
err = fexit_test_common(fexit_skel);
if (!ASSERT_OK(err, "fexit_first_attach"))
goto cleanup;
err = fexit_test(fexit_skel);
err = fexit_test_common(fexit_skel);
ASSERT_OK(err, "fexit_second_attach");
cleanup:
fexit_test_lskel__destroy(fexit_skel);
}
static void fexit_many_args(void)
{
struct fexit_many_args *fexit_skel = NULL;
int err;
fexit_skel = fexit_many_args__open_and_load();
if (!ASSERT_OK_PTR(fexit_skel, "fexit_many_args_skel_load"))
goto cleanup;
err = fexit_many_args__attach(fexit_skel);
if (!ASSERT_OK(err, "fexit_many_args_attach"))
goto cleanup;
ASSERT_OK(trigger_module_test_read(1), "trigger_read");
ASSERT_EQ(fexit_skel->bss->test1_result, 1,
"fexit_many_args_result1");
ASSERT_EQ(fexit_skel->bss->test2_result, 1,
"fexit_many_args_result2");
ASSERT_EQ(fexit_skel->bss->test3_result, 1,
"fexit_many_args_result3");
cleanup:
fexit_many_args__destroy(fexit_skel);
}
void test_fexit_test(void)
{
if (test__start_subtest("fexit"))
fexit_test();
if (test__start_subtest("fexit_many_args"))
fexit_many_args();
}
......@@ -30,7 +30,9 @@ void test_get_func_args_test(void)
prog_fd = bpf_program__fd(skel->progs.fmod_ret_test);
err = bpf_prog_test_run_opts(prog_fd, &topts);
ASSERT_OK(err, "test_run");
ASSERT_EQ(topts.retval, 1234, "test_run");
ASSERT_EQ(topts.retval >> 16, 1, "test_run");
ASSERT_EQ(topts.retval & 0xffff, 1234 + 29, "test_run");
ASSERT_EQ(skel->bss->test1_result, 1, "test1_result");
ASSERT_EQ(skel->bss->test2_result, 1, "test2_result");
......
......@@ -41,6 +41,10 @@ static void run_test(__u32 input_retval, __u16 want_side_effect, __s16 want_ret)
ASSERT_EQ(skel->bss->fexit_result, 1, "modify_return fexit_result");
ASSERT_EQ(skel->bss->fmod_ret_result, 1, "modify_return fmod_ret_result");
ASSERT_EQ(skel->bss->fentry_result2, 1, "modify_return fentry_result2");
ASSERT_EQ(skel->bss->fexit_result2, 1, "modify_return fexit_result2");
ASSERT_EQ(skel->bss->fmod_ret_result2, 1, "modify_return fmod_ret_result2");
cleanup:
modify_return__destroy(skel);
}
......@@ -49,9 +53,9 @@ static void run_test(__u32 input_retval, __u16 want_side_effect, __s16 want_ret)
void serial_test_modify_return(void)
{
run_test(0 /* input_retval */,
1 /* want_side_effect */,
4 /* want_ret */);
2 /* want_side_effect */,
33 /* want_ret */);
run_test(-EINVAL /* input_retval */,
0 /* want_side_effect */,
-EINVAL /* want_ret */);
-EINVAL * 2 /* want_ret */);
}
......@@ -55,6 +55,25 @@ static void test_fentry(void)
ASSERT_EQ(skel->bss->t6, 1, "t6 ret");
ASSERT_EQ(skel->bss->t7_a, 16, "t7:a");
ASSERT_EQ(skel->bss->t7_b, 17, "t7:b");
ASSERT_EQ(skel->bss->t7_c, 18, "t7:c");
ASSERT_EQ(skel->bss->t7_d, 19, "t7:d");
ASSERT_EQ(skel->bss->t7_e, 20, "t7:e");
ASSERT_EQ(skel->bss->t7_f_a, 21, "t7:f.a");
ASSERT_EQ(skel->bss->t7_f_b, 22, "t7:f.b");
ASSERT_EQ(skel->bss->t7_ret, 133, "t7 ret");
ASSERT_EQ(skel->bss->t8_a, 16, "t8:a");
ASSERT_EQ(skel->bss->t8_b, 17, "t8:b");
ASSERT_EQ(skel->bss->t8_c, 18, "t8:c");
ASSERT_EQ(skel->bss->t8_d, 19, "t8:d");
ASSERT_EQ(skel->bss->t8_e, 20, "t8:e");
ASSERT_EQ(skel->bss->t8_f_a, 21, "t8:f.a");
ASSERT_EQ(skel->bss->t8_f_b, 22, "t8:f.b");
ASSERT_EQ(skel->bss->t8_g, 23, "t8:g");
ASSERT_EQ(skel->bss->t8_ret, 156, "t8 ret");
tracing_struct__detach(skel);
destroy_skel:
tracing_struct__destroy(skel);
......
......@@ -88,8 +88,8 @@ void serial_test_trampoline_count(void)
if (!ASSERT_OK(err, "bpf_prog_test_run_opts"))
goto cleanup;
ASSERT_EQ(opts.retval & 0xffff, 4, "bpf_modify_return_test.result");
ASSERT_EQ(opts.retval >> 16, 1, "bpf_modify_return_test.side_effect");
ASSERT_EQ(opts.retval & 0xffff, 33, "bpf_modify_return_test.result");
ASSERT_EQ(opts.retval >> 16, 2, "bpf_modify_return_test.side_effect");
cleanup:
for (; i >= 0; i--) {
......
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Tencent */
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
char _license[] SEC("license") = "GPL";
__u64 test1_result = 0;
SEC("fentry/bpf_testmod_fentry_test7")
int BPF_PROG(test1, __u64 a, void *b, short c, int d, void *e, char f,
int g)
{
test1_result = a == 16 && b == (void *)17 && c == 18 && d == 19 &&
e == (void *)20 && f == 21 && g == 22;
return 0;
}
__u64 test2_result = 0;
SEC("fentry/bpf_testmod_fentry_test11")
int BPF_PROG(test2, __u64 a, void *b, short c, int d, void *e, char f,
int g, unsigned int h, long i, __u64 j, unsigned long k)
{
test2_result = a == 16 && b == (void *)17 && c == 18 && d == 19 &&
e == (void *)20 && f == 21 && g == 22 && h == 23 &&
i == 24 && j == 25 && k == 26;
return 0;
}
__u64 test3_result = 0;
SEC("fentry/bpf_testmod_fentry_test11")
int BPF_PROG(test3, __u64 a, __u64 b, __u64 c, __u64 d, __u64 e, __u64 f,
__u64 g, __u64 h, __u64 i, __u64 j, __u64 k)
{
test3_result = a == 16 && b == 17 && c == 18 && d == 19 &&
e == 20 && f == 21 && g == 22 && h == 23 &&
i == 24 && j == 25 && k == 26;
return 0;
}
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Tencent */
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
char _license[] SEC("license") = "GPL";
__u64 test1_result = 0;
SEC("fexit/bpf_testmod_fentry_test7")
int BPF_PROG(test1, __u64 a, void *b, short c, int d, void *e, char f,
int g, int ret)
{
test1_result = a == 16 && b == (void *)17 && c == 18 && d == 19 &&
e == (void *)20 && f == 21 && g == 22 && ret == 133;
return 0;
}
__u64 test2_result = 0;
SEC("fexit/bpf_testmod_fentry_test11")
int BPF_PROG(test2, __u64 a, void *b, short c, int d, void *e, char f,
int g, unsigned int h, long i, __u64 j, unsigned long k,
int ret)
{
test2_result = a == 16 && b == (void *)17 && c == 18 && d == 19 &&
e == (void *)20 && f == 21 && g == 22 && h == 23 &&
i == 24 && j == 25 && k == 26 && ret == 231;
return 0;
}
__u64 test3_result = 0;
SEC("fexit/bpf_testmod_fentry_test11")
int BPF_PROG(test3, __u64 a, __u64 b, __u64 c, __u64 d, __u64 e, __u64 f,
__u64 g, __u64 h, __u64 i, __u64 j, __u64 k, __u64 ret)
{
test3_result = a == 16 && b == 17 && c == 18 && d == 19 &&
e == 20 && f == 21 && g == 22 && h == 23 &&
i == 24 && j == 25 && k == 26 && ret == 231;
return 0;
}
......@@ -47,3 +47,43 @@ int BPF_PROG(fexit_test, int a, __u64 b, int ret)
return 0;
}
static int sequence2;
__u64 fentry_result2 = 0;
SEC("fentry/bpf_modify_return_test2")
int BPF_PROG(fentry_test2, int a, int *b, short c, int d, void *e, char f,
int g)
{
sequence2++;
fentry_result2 = (sequence2 == 1);
return 0;
}
__u64 fmod_ret_result2 = 0;
SEC("fmod_ret/bpf_modify_return_test2")
int BPF_PROG(fmod_ret_test2, int a, int *b, short c, int d, void *e, char f,
int g, int ret)
{
sequence2++;
/* This is the first fmod_ret program, the ret passed should be 0 */
fmod_ret_result2 = (sequence2 == 2 && ret == 0);
return input_retval;
}
__u64 fexit_result2 = 0;
SEC("fexit/bpf_modify_return_test2")
int BPF_PROG(fexit_test2, int a, int *b, short c, int d, void *e, char f,
int g, int ret)
{
sequence2++;
/* If the input_reval is non-zero a successful modification should have
* occurred.
*/
if (input_retval)
fexit_result2 = (sequence2 == 3 && ret == input_retval);
else
fexit_result2 = (sequence2 == 3 && ret == 29);
return 0;
}
......@@ -18,6 +18,11 @@ struct bpf_testmod_struct_arg_3 {
int b[];
};
struct bpf_testmod_struct_arg_4 {
u64 a;
int b;
};
long t1_a_a, t1_a_b, t1_b, t1_c, t1_ret, t1_nregs;
__u64 t1_reg0, t1_reg1, t1_reg2, t1_reg3;
long t2_a, t2_b_a, t2_b_b, t2_c, t2_ret;
......@@ -25,6 +30,9 @@ long t3_a, t3_b, t3_c_a, t3_c_b, t3_ret;
long t4_a_a, t4_b, t4_c, t4_d, t4_e_a, t4_e_b, t4_ret;
long t5_ret;
int t6;
long t7_a, t7_b, t7_c, t7_d, t7_e, t7_f_a, t7_f_b, t7_ret;
long t8_a, t8_b, t8_c, t8_d, t8_e, t8_f_a, t8_f_b, t8_g, t8_ret;
SEC("fentry/bpf_testmod_test_struct_arg_1")
int BPF_PROG2(test_struct_arg_1, struct bpf_testmod_struct_arg_2, a, int, b, int, c)
......@@ -130,4 +138,50 @@ int BPF_PROG2(test_struct_arg_11, struct bpf_testmod_struct_arg_3 *, a)
return 0;
}
SEC("fentry/bpf_testmod_test_struct_arg_7")
int BPF_PROG2(test_struct_arg_12, __u64, a, void *, b, short, c, int, d,
void *, e, struct bpf_testmod_struct_arg_4, f)
{
t7_a = a;
t7_b = (long)b;
t7_c = c;
t7_d = d;
t7_e = (long)e;
t7_f_a = f.a;
t7_f_b = f.b;
return 0;
}
SEC("fexit/bpf_testmod_test_struct_arg_7")
int BPF_PROG2(test_struct_arg_13, __u64, a, void *, b, short, c, int, d,
void *, e, struct bpf_testmod_struct_arg_4, f, int, ret)
{
t7_ret = ret;
return 0;
}
SEC("fentry/bpf_testmod_test_struct_arg_8")
int BPF_PROG2(test_struct_arg_14, __u64, a, void *, b, short, c, int, d,
void *, e, struct bpf_testmod_struct_arg_4, f, int, g)
{
t8_a = a;
t8_b = (long)b;
t8_c = c;
t8_d = d;
t8_e = (long)e;
t8_f_a = f.a;
t8_f_b = f.b;
t8_g = g;
return 0;
}
SEC("fexit/bpf_testmod_test_struct_arg_8")
int BPF_PROG2(test_struct_arg_15, __u64, a, void *, b, short, c, int, d,
void *, e, struct bpf_testmod_struct_arg_4, f, int, g,
int, ret)
{
t8_ret = ret;
return 0;
}
char _license[] SEC("license") = "GPL";
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment