Skip to content
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 #3575

Closed
wants to merge 9 commits into from

Conversation

kernel-patches-bot
Copy link

Pull request for series with
subject: bpf: Support struct argument for trampoline base progs
version: 3
url: https://patchwork.kernel.org/project/netdevbpf/list/?series=671773

@kernel-patches-bot
Copy link
Author

Master branch: aa75622
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=671773
version: 3

@kernel-patches-bot
Copy link
Author

Master branch: bbcf0f5
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=671773
version: 3

@kernel-patches-bot
Copy link
Author

Master branch: 3721359
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=671773
version: 3

@kernel-patches-bot
Copy link
Author

Master branch: 2eb6804
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=671773
version: 3

@kernel-patches-bot
Copy link
Author

At least one diff in series https://patchwork.kernel.org/project/netdevbpf/list/?series=671773 expired. Closing PR.

@kernel-patches-bot
Copy link
Author

Master branch: 6f95de6
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=672864
version: 4

@kernel-patches-bot
Copy link
Author

Master branch: 6f95de6
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=672864
version: 4

@kernel-patches-bot
Copy link
Author

Master branch: 14e5ce7
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=672864
version: 4

Kernel Patches Daemon and others added 5 commits August 31, 2022 13:37
Allow struct argument in trampoline based programs where
the struct size should be <= 16 bytes. In such cases, the argument
will be put into up to 2 registers for bpf, x86_64 and arm64
architectures.

To support arch-specific trampoline manipulation,
add arg_flags for additional struct information about arguments
in btf_func_model. 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>
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.

The latest llvm16 added bpf support to pass by values
for struct up to 16 bytes ([1]). This is also true for
x86_64 architecture where two registers will hold
the struct value if the struct size is >8 and <= 16.
This may not be true if one of struct member is 'double'
type but in current linux source code we don't have
such instance yet, so we assume all >8 && <= 16 struct
holds two general purpose argument registers.

Also change on-stack nr_args value to the number
of registers holding the arguments. This will
permit bpf_get_func_arg() helper to get all
argument values.

 [1] https://reviews.llvm.org/D132144

Signed-off-by: Yonghong Song <yhs@fb.com>
Now instead of the number of arguments, the number of registers
holding argument values are stored in trampoline. Update
the description of bpf_get_func_arg[_cnt]() helpers. Previous
programs without struct arguments should continue to work
as usual.

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>
To support struct arguments in trampoline based programs,
existing BPF_PROG doesn't work any more since
the type size is needed to find whether a parameter
takes one or two registers. So this patch added a new
BPF_PROG2 macro to support such trampoline programs.

The idea is suggested by Andrii. For example, if the
to-be-traced function has signature like
  typedef struct {
       void *x;
       int t;
  } sockptr;
  int blah(sockptr x, char y);

In the new BPF_PROG2 macro, the argument can be
represented as
  __bpf_prog_call(
     ({ union {
          struct { __u64 x, y; } ___z;
          sockptr x;
        } ___tmp = { .___z = { ctx[0], ctx[1] }};
        ___tmp.x;
     }),
     ({ union {
          struct { __u8 x; } ___z;
          char y;
        } ___tmp = { .___z = { ctx[2] }};
        ___tmp.y;
     }));
In the above, the values stored on the stack are properly
assigned to the actual argument type value by using 'union'
magic. Note that the macro also works even if no arguments
are with struct types.

Note that new BPF_PROG2 works for both llvm16 and pre-llvm16
compilers where llvm16 supports bpf target passing value
with struct up to 16 byte size and pre-llvm16 will pass
by reference by storing values on the stack. With static functions
with struct argument as always inline, the compiler is able
to optimize and remove additional stack saving of struct values.

Signed-off-by: Yonghong Song <yhs@fb.com>
Add various struct argument tests with fentry/fexit programs.
Also add one test with a kernel func which does not have any
argument to test BPF_PROG2 macro in such situation.

Signed-off-by: Yonghong Song <yhs@fb.com>
@kernel-patches-bot
Copy link
Author

Master branch: 1970729
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=672864
version: 4

…arguments

Use BPF_PROG2 instead of BPF_PROG for programs in progs/timer.c
to test BPF_PROG2 for cases without struct arguments.

Signed-off-by: Yonghong Song <yhs@fb.com>
Add tracing_struct test in DENYLIST.s390x since s390x does not
support trampoline now.

Signed-off-by: Yonghong Song <yhs@fb.com>
@kernel-patches-bot
Copy link
Author

Master branch: c9ae8c9
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=672864
version: 4

Pull request is NOT updated. Failed to apply https://patchwork.kernel.org/project/netdevbpf/list/?series=672864
error message:

Cmd('git') failed due to: exit code(128)
  cmdline: git am -3
  stdout: 'Applying: bpf: Allow struct argument in trampoline based programs
Applying: bpf: x86: Support in-register struct arguments in trampoline programs
Applying: bpf: Update descriptions for helpers bpf_get_func_arg[_cnt]()
Applying: bpf: arm64: No support of struct argument in trampoline programs
Applying: libbpf: Add new BPF_PROG2 macro
Applying: selftests/bpf: Add struct argument tests with fentry/fexit programs.
Applying: selftests/bpf: Use BPF_PROG2 for some fentry programs without struct arguments
Applying: selftests/bpf: Add tracing_struct test in DENYLIST.s390x
Using index info to reconstruct a base tree...
M	tools/testing/selftests/bpf/DENYLIST.s390x
Falling back to patching base and 3-way merge...
Auto-merging tools/testing/selftests/bpf/DENYLIST.s390x
CONFLICT (content): Merge conflict in tools/testing/selftests/bpf/DENYLIST.s390x
Patch failed at 0008 selftests/bpf: Add tracing_struct test in DENYLIST.s390x
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".'
  stderr: 'error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch'

conflict:

diff --cc tools/testing/selftests/bpf/DENYLIST.s390x
index ba02b559ca68,aa18b6d24510..000000000000
--- a/tools/testing/selftests/bpf/DENYLIST.s390x
+++ b/tools/testing/selftests/bpf/DENYLIST.s390x
@@@ -68,4 -68,4 +68,8 @@@ unpriv_bpf_disable
  setget_sockopt                           # attach unexpected error: -524                                               (trampoline)
  cb_refs                                  # expected error message unexpected error: -524                               (trampoline)
  cgroup_hierarchical_stats                # JIT does not support calling kernel function                                (kfunc)
++<<<<<<< HEAD
 +htab_update                              # failed to attach: ERROR: strerror_r(-524)=22                                (trampoline)
++=======
+ tracing_struct                           # failed to auto-attach: -524                                                 (trampoline)
++>>>>>>> selftests/bpf: Add tracing_struct test in DENYLIST.s390x

@kernel-patches-bot
Copy link
Author

At least one diff in series https://patchwork.kernel.org/project/netdevbpf/list/?series=672864 irrelevant now. Closing PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants