From: Martin KaFai Lau <martin.lau@linux.dev>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: bpf <bpf@vger.kernel.org>, Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Eduard Zingerman <eddyz87@gmail.com>,
Yonghong Song <yonghong.song@linux.dev>,
Amery Hung <ameryhung@gmail.com>,
Kernel Team <kernel-team@meta.com>
Subject: Re: [PATCH v2 bpf-next 7/8] bpf: Allow pro/epilogue to call kfunc
Date: Wed, 21 Aug 2024 23:09:55 -0700 [thread overview]
Message-ID: <7a4aa80b-b5fe-4f9a-95a3-743d2a218927@linux.dev> (raw)
In-Reply-To: <CAADnVQK4LUVsKQYHdaw0x9-CryA0wQX6stkvhFnNoDh1tt0jhg@mail.gmail.com>
On 8/21/24 6:32 PM, Alexei Starovoitov wrote:
> On Wed, Aug 21, 2024 at 4:35 PM Martin KaFai Lau <martin.lau@linux.dev> wrote:
>>
>> From: Martin KaFai Lau <martin.lau@kernel.org>
>>
>> The existing prologue has been able to call bpf helper but not a kfunc.
>> This patch allows the prologue/epilogue to call the kfunc.
>>
>> The subsystem that implements the .gen_prologue and .gen_epilogue
>> can add the BPF_PSEUDO_KFUNC_CALL instruction with insn->imm
>> set to the btf func_id of the kfunc call. This part is the same
>> as the bpf prog loaded from the sys_bpf.
>
> I don't understand the value of this feature, since it seems
> pretty hard to use.
> The module (qdisc-bpf or else) would need to do something
> like patch 8/8:
> +BTF_ID_LIST(st_ops_epilogue_kfunc_list)
> +BTF_ID(func, bpf_kfunc_st_ops_inc10)
> +BTF_ID(func, bpf_kfunc_st_ops_inc100)
>
> just to be able to:
> BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0,
> st_ops_epilogue_kfunc_list[0]);
>
> So a bunch of extra work on the module side and
> a bunch of work in this patch to enable such a pattern,
> but what is the value?
>
> gen_epilogue() can call arbitrary kernel function.
> It doesn't have to be a helper.
> kfunc-s provide calling convention conversion from bpf to native,
> but the same thing is achieved by BPF_CALL_N macro.
> The module can use that macro without adding an actual bpf helper
> to uapi bpf.h.
> Then in gen_epilogue() the extra bpf insn can use:
> BPF_EMIT_CALL(module_provided_helper_that_is_not_helper)
> which will use
> BPF_CALL_IMM(x) ((void *)(x) - (void *)__bpf_call_base)
BPF_EMIT_CALL() was my earlier thought. I switched to the kfunc in this patch
because of the bpf_jit_supports_far_kfunc_call() support for the kernel module.
Using kfunc call will make supporting it the same.
I think the future bpf-qdisc can enforce built-in. bpf-tcp-cc has already been
built-in only also. I think the hid_bpf is built-in only also.
Another consideration is also holding the module refcnt when having an
attachable bpf prog calling a kernel func implemented in a kernel module. iiuc,
this is the reason why aux->kfunc_btf_tab holds a reference to the kernel
module. This should not be a problem to struct_ops though because the struct_ops
map is the one that is attachable instead of the struct_ops prog. The struct_ops
map has already held a refcnt of the module.
> to populate imm.
> And JITs will emit jump to that wrapper code provided by
> BPF_CALL_N.
>
> And no need for this extra complexity in the verifier and
> its consumers that have to figure out (module_fd, btf_id) for
> kfunc just to fit into kfunc pattern with btf_distill_func_proto().
>
> I guess one can argue that if such kfunc is already available
> to bpf prog then extra BPF_CALL_N wrapper for the same thing
> is a waste of kernel text, but this patch also adds quite a bit of
> kernel text. So the cost of BPF_CALL_N (which is a zero on x86)
> is acceptable.
next prev parent reply other threads:[~2024-08-22 6:10 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-21 23:34 [PATCH v2 bpf-next 0/8] bpf: Add gen_epilogue and allow kfunc call in pro/epilogue Martin KaFai Lau
2024-08-21 23:34 ` [PATCH v2 bpf-next 1/8] bpf: Add gen_epilogue to bpf_verifier_ops Martin KaFai Lau
2024-08-22 0:22 ` Alexei Starovoitov
2024-08-22 0:30 ` Eduard Zingerman
2024-08-22 0:34 ` Alexei Starovoitov
2024-08-22 0:38 ` Eduard Zingerman
2024-08-22 0:52 ` Martin KaFai Lau
2024-08-21 23:34 ` [PATCH v2 bpf-next 2/8] bpf: Export bpf_base_func_proto Martin KaFai Lau
2024-08-21 23:34 ` [PATCH v2 bpf-next 3/8] selftests/bpf: attach struct_ops maps before test prog runs Martin KaFai Lau
2024-08-21 23:34 ` [PATCH v2 bpf-next 4/8] selftests/bpf: Test gen_prologue and gen_epilogue Martin KaFai Lau
2024-08-21 23:34 ` [PATCH v2 bpf-next 5/8] selftests/bpf: Add tailcall epilogue test Martin KaFai Lau
2024-08-21 23:34 ` [PATCH v2 bpf-next 6/8] bpf: Add module parameter to gen_prologue and gen_epilogue Martin KaFai Lau
2024-08-21 23:34 ` [PATCH v2 bpf-next 7/8] bpf: Allow pro/epilogue to call kfunc Martin KaFai Lau
2024-08-22 1:32 ` Alexei Starovoitov
2024-08-22 6:09 ` Martin KaFai Lau [this message]
2024-08-22 13:47 ` Alexei Starovoitov
2024-08-22 17:38 ` Martin KaFai Lau
2024-08-22 17:58 ` Alexei Starovoitov
2024-08-21 23:34 ` [PATCH v2 bpf-next 8/8] selftests/bpf: Add kfunc call test in gen_prologue and gen_epilogue Martin KaFai Lau
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7a4aa80b-b5fe-4f9a-95a3-743d2a218927@linux.dev \
--to=martin.lau@linux.dev \
--cc=alexei.starovoitov@gmail.com \
--cc=ameryhung@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=kernel-team@meta.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox