New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpf: Support struct argument for trampoline base progs #3479
Conversation
Master branch: 10b62d6 |
Master branch: 54c9397 |
805e946
to
39b4967
Compare
Master branch: 4961d07 |
39b4967
to
0ed8ae8
Compare
Master branch: cea5588 |
0ed8ae8
to
30d7c6e
Compare
Master branch: 8308bf2 |
30d7c6e
to
04812b1
Compare
Add arg_flags for additional information about arguments in btf_func_model. Currently, only whether an argument is a structure or not is recorded. Such information will be used in arch specific function arch_prepare_bpf_trampoline() to prepare argument access properly in trampoline. Signed-off-by: Yonghong Song <yhs@fb.com>
Rename argument 'stack_size' to 'regs_off' in save_regs() and restore_regs() since in reality 'stack_size' represents a particular stack offset and 'regs_off' is the one in the caller argument. Leter, another stack offset will be added to these two functions for saving and restoring struct argument values. Signed-off-by: Yonghong Song <yhs@fb.com>
In C, struct value can be passed as a function argument. For small structs, struct value may be passed in one or more registers. For trampoline based bpf programs, This would cause complication since one-to-one mapping between function argument and arch argument register is not valid any more. To support struct value argument and make bpf programs easy to write, the bpf program function parameter is changed from struct type to a pointer to struct type. The following is a simplified example. In one of later selftests, we have a bpf_testmod function: struct bpf_testmod_struct_arg_2 { long a; long b; }; noinline int bpf_testmod_test_struct_arg_2(int a, struct bpf_testmod_struct_arg_2 b, int c) { bpf_testmod_test_struct_arg_result = a + b.a + b.b + c; return bpf_testmod_test_struct_arg_result; } When a bpf program is attached to the bpf_testmod function bpf_testmod_test_struct_arg_2(), the bpf program may look like SEC("fentry/bpf_testmod_test_struct_arg_2") int BPF_PROG(test_struct_arg_3, int a, struct bpf_testmod_struct_arg_2 *b, int c) { t2_a = a; t2_b_a = b->a; t2_b_b = b->b; t2_c = c; return 0; } Basically struct value becomes a pointer to the struct. The trampoline stack will be increased to store the stack values and the pointer to these values will be saved in the stack slot corresponding to that argument. For x86_64, the struct size is limited up to 16 bytes so the struct can fit in one or two registers. The struct size of more than 16 bytes is not supported now as our current use case is for sockptr_t in the argument. We could handle such large struct's in the future if we have concrete use cases. The main changes are in save_regs() and restore_regs(). The following illustrated the trampoline asm codes for save_regs() and restore_regs(). save_regs(): /* first argument */ mov DWORD PTR [rbp-0x18],edi /* second argument: struct, save actual values and put the pointer to the slot */ lea rax,[rbp-0x40] mov QWORD PTR [rbp-0x10],rax mov QWORD PTR [rbp-0x40],rsi mov QWORD PTR [rbp-0x38],rdx /* third argument */ mov DWORD PTR [rbp-0x8],esi restore_regs(): mov edi,DWORD PTR [rbp-0x18] mov rsi,QWORD PTR [rbp-0x40] mov rdx,QWORD PTR [rbp-0x38] mov esi,DWORD PTR [rbp-0x8] Signed-off-by: Yonghong Song <yhs@fb.com>
ARM64 does not support struct argument for trampoline based bpf programs yet. Signed-off-by: Yonghong Song <yhs@fb.com>
Add struct argument support in btf_ctx_access() and btf_distill_func_proto(). The arch-specific code will handle whether and how such struct arguments are supported. Signed-off-by: Yonghong Song <yhs@fb.com>
Master branch: 8308bf2 |
Add various struct argument tests with fentry/fexit programs. Signed-off-by: Yonghong Song <yhs@fb.com>
04812b1
to
cf5c12c
Compare
At least one diff in series https://patchwork.kernel.org/project/netdevbpf/list/?series=667059 expired. Closing PR. |
Pull request for series with
subject: bpf: Support struct argument for trampoline base progs
version: 2
url: https://patchwork.kernel.org/project/netdevbpf/list/?series=667059