diff mbox series

[bpf-next,v2,1/3] bpf: new btf kfunc hooks for tracepoint and perf event

Message ID 20240826224814.289034-2-inwardvessel@gmail.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series allow cpumask kfuncs in tracepoint, kprobe, perf event | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next
netdev/apply fail Patch does not apply to bpf-next-0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-18 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc

Commit Message

JP Kobryn Aug. 26, 2024, 10:48 p.m. UTC
The additional hooks (and prog-to-hook mapping) for tracepoint and perf
event programs allow for registering kfuncs to be used within these
program types.

Signed-off-by: JP Kobryn <inwardvessel@gmail.com>
---
 kernel/bpf/btf.c | 6 ++++++
 1 file changed, 6 insertions(+)

Comments

Alexei Starovoitov Aug. 27, 2024, 9:01 p.m. UTC | #1
On Mon, Aug 26, 2024 at 3:48 PM JP Kobryn <inwardvessel@gmail.com> wrote:
>
> The additional hooks (and prog-to-hook mapping) for tracepoint and perf
> event programs allow for registering kfuncs to be used within these
> program types.
>
> Signed-off-by: JP Kobryn <inwardvessel@gmail.com>
> ---
>  kernel/bpf/btf.c | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 520f49f422fe..4816e309314e 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -210,6 +210,7 @@ enum btf_kfunc_hook {
>         BTF_KFUNC_HOOK_TC,
>         BTF_KFUNC_HOOK_STRUCT_OPS,
>         BTF_KFUNC_HOOK_TRACING,
> +       BTF_KFUNC_HOOK_TRACEPOINT,
>         BTF_KFUNC_HOOK_SYSCALL,
>         BTF_KFUNC_HOOK_FMODRET,
>         BTF_KFUNC_HOOK_CGROUP_SKB,
> @@ -219,6 +220,7 @@ enum btf_kfunc_hook {
>         BTF_KFUNC_HOOK_LWT,
>         BTF_KFUNC_HOOK_NETFILTER,
>         BTF_KFUNC_HOOK_KPROBE,
> +       BTF_KFUNC_HOOK_PERF_EVENT,
>         BTF_KFUNC_HOOK_MAX,
>  };
>
> @@ -8306,6 +8308,8 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)
>         case BPF_PROG_TYPE_TRACING:
>         case BPF_PROG_TYPE_LSM:
>                 return BTF_KFUNC_HOOK_TRACING;
> +       case BPF_PROG_TYPE_TRACEPOINT:
> +               return BTF_KFUNC_HOOK_TRACEPOINT;

why special case tp and perf_event and only limit them to cpumask?
The following would be equally safe, no?
         case BPF_PROG_TYPE_TRACING:
         case BPF_PROG_TYPE_LSM:
 +       case BPF_PROG_TYPE_TRACEPOINT:
 +       case BPF_PROG_TYPE_PERF_EVENT:
                return BTF_KFUNC_HOOK_TRACING;
?
Andrii Nakryiko Aug. 27, 2024, 10:42 p.m. UTC | #2
On Tue, Aug 27, 2024 at 2:01 PM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Mon, Aug 26, 2024 at 3:48 PM JP Kobryn <inwardvessel@gmail.com> wrote:
> >
> > The additional hooks (and prog-to-hook mapping) for tracepoint and perf
> > event programs allow for registering kfuncs to be used within these
> > program types.
> >
> > Signed-off-by: JP Kobryn <inwardvessel@gmail.com>
> > ---
> >  kernel/bpf/btf.c | 6 ++++++
> >  1 file changed, 6 insertions(+)
> >
> > diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> > index 520f49f422fe..4816e309314e 100644
> > --- a/kernel/bpf/btf.c
> > +++ b/kernel/bpf/btf.c
> > @@ -210,6 +210,7 @@ enum btf_kfunc_hook {
> >         BTF_KFUNC_HOOK_TC,
> >         BTF_KFUNC_HOOK_STRUCT_OPS,
> >         BTF_KFUNC_HOOK_TRACING,
> > +       BTF_KFUNC_HOOK_TRACEPOINT,
> >         BTF_KFUNC_HOOK_SYSCALL,
> >         BTF_KFUNC_HOOK_FMODRET,
> >         BTF_KFUNC_HOOK_CGROUP_SKB,
> > @@ -219,6 +220,7 @@ enum btf_kfunc_hook {
> >         BTF_KFUNC_HOOK_LWT,
> >         BTF_KFUNC_HOOK_NETFILTER,
> >         BTF_KFUNC_HOOK_KPROBE,
> > +       BTF_KFUNC_HOOK_PERF_EVENT,
> >         BTF_KFUNC_HOOK_MAX,
> >  };
> >
> > @@ -8306,6 +8308,8 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)
> >         case BPF_PROG_TYPE_TRACING:
> >         case BPF_PROG_TYPE_LSM:
> >                 return BTF_KFUNC_HOOK_TRACING;
> > +       case BPF_PROG_TYPE_TRACEPOINT:
> > +               return BTF_KFUNC_HOOK_TRACEPOINT;
>
> why special case tp and perf_event and only limit them to cpumask?
> The following would be equally safe, no?

Assuming we don't have kfuncs that accepts program context (like
bpf_get_stack(), if it was a kfunc) and that doesn't access
bpf_run_ctx (like bpf_get_func_ip()). We just need to be careful about
adding new special kfuncs like that going forward (not sure how to
best ensure we don't forget, though). Other than that I agree that
it's all "tracing".

>          case BPF_PROG_TYPE_TRACING:
>          case BPF_PROG_TYPE_LSM:
>  +       case BPF_PROG_TYPE_TRACEPOINT:
>  +       case BPF_PROG_TYPE_PERF_EVENT:
>                 return BTF_KFUNC_HOOK_TRACING;
> ?
JP Kobryn Aug. 28, 2024, 7:08 p.m. UTC | #3
On Tue, Aug 27, 2024 at 03:42:34PM -0700, Andrii Nakryiko wrote:
> On Tue, Aug 27, 2024 at 2:01 PM Alexei Starovoitov
> <alexei.starovoitov@gmail.com> wrote:
> >
> > On Mon, Aug 26, 2024 at 3:48 PM JP Kobryn <inwardvessel@gmail.com> wrote:
> > >
> > > The additional hooks (and prog-to-hook mapping) for tracepoint and perf
> > > event programs allow for registering kfuncs to be used within these
> > > program types.
> > >
> > > Signed-off-by: JP Kobryn <inwardvessel@gmail.com>
> > > ---
> > >  kernel/bpf/btf.c | 6 ++++++
> > >  1 file changed, 6 insertions(+)
> > >
> > > diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> > > index 520f49f422fe..4816e309314e 100644
> > > --- a/kernel/bpf/btf.c
> > > +++ b/kernel/bpf/btf.c
> > > @@ -210,6 +210,7 @@ enum btf_kfunc_hook {
> > >         BTF_KFUNC_HOOK_TC,
> > >         BTF_KFUNC_HOOK_STRUCT_OPS,
> > >         BTF_KFUNC_HOOK_TRACING,
> > > +       BTF_KFUNC_HOOK_TRACEPOINT,
> > >         BTF_KFUNC_HOOK_SYSCALL,
> > >         BTF_KFUNC_HOOK_FMODRET,
> > >         BTF_KFUNC_HOOK_CGROUP_SKB,
> > > @@ -219,6 +220,7 @@ enum btf_kfunc_hook {
> > >         BTF_KFUNC_HOOK_LWT,
> > >         BTF_KFUNC_HOOK_NETFILTER,
> > >         BTF_KFUNC_HOOK_KPROBE,
> > > +       BTF_KFUNC_HOOK_PERF_EVENT,
> > >         BTF_KFUNC_HOOK_MAX,
> > >  };
> > >
> > > @@ -8306,6 +8308,8 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)
> > >         case BPF_PROG_TYPE_TRACING:
> > >         case BPF_PROG_TYPE_LSM:
> > >                 return BTF_KFUNC_HOOK_TRACING;
> > > +       case BPF_PROG_TYPE_TRACEPOINT:
> > > +               return BTF_KFUNC_HOOK_TRACEPOINT;
> >
> > why special case tp and perf_event and only limit them to cpumask?
> > The following would be equally safe, no?
> 
> Assuming we don't have kfuncs that accepts program context (like
> bpf_get_stack(), if it was a kfunc) and that doesn't access
> bpf_run_ctx (like bpf_get_func_ip()). We just need to be careful about
> adding new special kfuncs like that going forward (not sure how to
> best ensure we don't forget, though). Other than that I agree that
> it's all "tracing".

What Alexei is suggesting works. I did something similar in v1[0] where I
associated BPF_PROG_TYPE_TRACEPOINT with BTF_KFUNC_HOOK_TRACING. But it
occurred to me that this circumvents the registration process during
initialization, so I want to make sure if this is or is not acceptable. See
below for my thoughts.
> 
> >          case BPF_PROG_TYPE_TRACING:
> >          case BPF_PROG_TYPE_LSM:
> >  +       case BPF_PROG_TYPE_TRACEPOINT:
> >  +       case BPF_PROG_TYPE_PERF_EVENT:
> >                 return BTF_KFUNC_HOOK_TRACING;
> > ?

With this change, anywhere we do
register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &some_kfunc_set),
BTF_KFUNC_HOOK_TRACING becomes allowed. So even if we never register the
extra program types like PROG_TYPE_PERF_EVENT, we still allow them as a
side effect since at runtime the program type mapping returns HOOK_TRACING.
Any program type associated with this hook will be allowed even though not
explicitly registered. My take on v2 was moving towards the element of
least surprise, and thought the explicit registration with the new hooks
made sense. I'm fine though, if we prefer this style above with the
implicit registration. Let me know and I can make a v3 if needed.

[0] https://lore.kernel.org/all/20240814235800.15253-3-inwardvessel@gmail.com/
Alexei Starovoitov Aug. 31, 2024, 2:14 a.m. UTC | #4
On Wed, Aug 28, 2024 at 12:08 PM JP Kobryn <inwardvessel@gmail.com> wrote:
>
> With this change, anywhere we do
> register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &some_kfunc_set),
> BTF_KFUNC_HOOK_TRACING becomes allowed. So even if we never register the
> extra program types like PROG_TYPE_PERF_EVENT, we still allow them as a
> side effect since at runtime the program type mapping returns HOOK_TRACING.
> Any program type associated with this hook will be allowed even though not
> explicitly registered.

Correct. kfuncs differentiate by type of their arguments.
prog_type -> helper was an old style when context was the only
thing we had. So we had to differentiate by prog_type.
kfuncs are currently register by prog_type too, but that will go away.
We're planning to do large refactoring in that area to satisfy
sched-ext requests. It's not the prog type that would allow or
disallow certain kfuncs but rather hook point plus custom flags.
diff mbox series

Patch

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 520f49f422fe..4816e309314e 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -210,6 +210,7 @@  enum btf_kfunc_hook {
 	BTF_KFUNC_HOOK_TC,
 	BTF_KFUNC_HOOK_STRUCT_OPS,
 	BTF_KFUNC_HOOK_TRACING,
+	BTF_KFUNC_HOOK_TRACEPOINT,
 	BTF_KFUNC_HOOK_SYSCALL,
 	BTF_KFUNC_HOOK_FMODRET,
 	BTF_KFUNC_HOOK_CGROUP_SKB,
@@ -219,6 +220,7 @@  enum btf_kfunc_hook {
 	BTF_KFUNC_HOOK_LWT,
 	BTF_KFUNC_HOOK_NETFILTER,
 	BTF_KFUNC_HOOK_KPROBE,
+	BTF_KFUNC_HOOK_PERF_EVENT,
 	BTF_KFUNC_HOOK_MAX,
 };
 
@@ -8306,6 +8308,8 @@  static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)
 	case BPF_PROG_TYPE_TRACING:
 	case BPF_PROG_TYPE_LSM:
 		return BTF_KFUNC_HOOK_TRACING;
+	case BPF_PROG_TYPE_TRACEPOINT:
+		return BTF_KFUNC_HOOK_TRACEPOINT;
 	case BPF_PROG_TYPE_SYSCALL:
 		return BTF_KFUNC_HOOK_SYSCALL;
 	case BPF_PROG_TYPE_CGROUP_SKB:
@@ -8326,6 +8330,8 @@  static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)
 		return BTF_KFUNC_HOOK_NETFILTER;
 	case BPF_PROG_TYPE_KPROBE:
 		return BTF_KFUNC_HOOK_KPROBE;
+	case BPF_PROG_TYPE_PERF_EVENT:
+		return BTF_KFUNC_HOOK_PERF_EVENT;
 	default:
 		return BTF_KFUNC_HOOK_MAX;
 	}