Re: Can't reproduce kfunc example in kfuncs documentation, kernel v6.0

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> I'm trying to reproduce the example in `Documentation/bpf/kfuncs.rst`
> in kernel 6.0
> My end goal is to be able to call a kfunc from a kprobe, so the
> documentation seemed like a good start.
> I've created a file with almost the same content as the documentation
> (below) and put it in
> net/bpf and added it to the Makefile, with the added __diag directives
> that are in
> net/bpf/test_run.c around the kfuncs.
>
>  __diag_push();
>  __diag_ignore_all("-Wmissing-prototypes",
>       "Global functions as their definitions will be in vmlinux BTF");
>   u64 bpf_get_task_pid(void) {
>     return 1;
>   }
>    u64 bpf_put_pid(void) {
>     return 2;
>   }
>   __diag_pop();
>
>   BTF_SET8_START(bpf_task_set)
>   BTF_ID_FLAGS(func, bpf_get_task_pid)
>   BTF_ID_FLAGS(func, bpf_put_pid)
>   BTF_SET8_END(bpf_task_set)
>
>   static const struct btf_kfunc_id_set bpf_task_kfunc_set = {
>       .owner = THIS_MODULE,
>       .set   = &bpf_task_set,
>   };
>
>   static int bpftest_init_subsystem(void)
>   {
>     pr_warn(">>>>>>>>>>>>>>> bpftest_init_subsystem registered");
>     //I want BPF_PROG_TYPE_KPROBE, but I'm testing also with
> BPF_PROG_TYPE_TRACEPOINT
>     return register_btf_kfunc_id_set(BPF_PROG_TYPE_KPROBE, &bpf_task_kfunc_set);
>   }
>   late_initcall(bpftest_init_subsystem);
>
>
> I can see that this is being registered, but after that I see many
> (16, all the same) messages like the one below.
> These messages are gone if I don't compile the file I created above.
> Is this file breaking something in bpf?
>
> [    5.845543] failed to validate module [cryptd] BTF: -22
> [    5.861117] BPF: [129150] STRUCT
> [    5.862980] BPF: size=96 vlen=1
> [    5.864710] BPF:
> [    5.865941] BPF: Invalid name
> [    5.867221] BPF:
>
>
> Ignoring these errors, I've tried both KPROBE and TRACEPOINT prog
> types in `register_btf_kfunc_id_set`.
> I can't find what a program with "tracing" is, so I changed it to
> BPF_PROG_TYPE_TRACEPOINT and used
> an example from the kernel: samples/bpf/syscall_tp_kern.c
> As for testing with KPROBE, I'm using the kprobe.bpf.c do_unlinkat
> example in libbpf/libbpf-bootstrap.
> It seems like the kfunc is not being found in the set, or the set is
> not registered correctly,
> since running the bpf program with any of the two types prints out:
>
>   libbpf: prog 'trace_enter_open': BPF program load failed: Permission denied
>   ...
>   calling kernel function bpf_get_task_pid is not allowed
>
> Both the bpf programs are simple, their bodies have:
>     __u64 a = bpf_get_task_pid();
> The function is getting resolved since I see
> libbpf: extern (func ksym) 'bpf_get_task_pid': resolved to kernel
>
> How can I correctly register a set and make the kernel allow me to call a kfunc?
> Thank you.

Turns out KPROBE or TRACEPOINT don't have hooks, so the id is never found.
After creating one for each in kernel/bpf/btf.c it works.

I still don't know why the `failed to validate module [cryptd] BTF:
-22` error is happening,
but I'll ignore it for now.



[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux