From: Yonghong Song <yonghong.song@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>,
Kernel Team <kernel-team@fb.com>,
Martin KaFai Lau <martin.lau@kernel.org>
Subject: Re: [PATCH bpf-next v3 3/5] bpf: Mark each subprog with proper pstack states
Date: Mon, 30 Sep 2024 09:26:02 -0700 [thread overview]
Message-ID: <75341619-0f3d-4d36-bbbd-a3128bca34c3@linux.dev> (raw)
In-Reply-To: <CAADnVQ+BQ+hkpyyWKH+W-j4FbXmh1STycEEpeGfTxOnafSO8og@mail.gmail.com>
On 9/30/24 7:49 AM, Alexei Starovoitov wrote:
> On Thu, Sep 26, 2024 at 4:45 PM Yonghong Song <yonghong.song@linux.dev> wrote:
>> Three private stack states are used to direct jit action:
>> PSTACK_TREE_NO: do not use private stack
>> PSTACK_TREE_INTERNAL: adjust frame pointer address (similar to normal stack)
>> PSTACK_TREE_ROOT: set the frame pointer
>>
>> Note that for subtree root, even if the root bpf_prog stack size is 0,
>> PSTACK_TREE_INTERNAL is still used. This is for bpf exception handling.
>> More details can be found in subsequent jit support and selftest patches.
>>
>> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
>> ---
>> include/linux/bpf.h | 9 +++++++++
>> kernel/bpf/core.c | 19 +++++++++++++++++++
>> kernel/bpf/verifier.c | 30 ++++++++++++++++++++++++++++++
>> 3 files changed, 58 insertions(+)
>>
>> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
>> index 156b9516d9f6..8f02d11bd408 100644
>> --- a/include/linux/bpf.h
>> +++ b/include/linux/bpf.h
>> @@ -1550,6 +1550,12 @@ struct bpf_prog_aux {
>> };
>> };
>>
>> +enum bpf_pstack_state {
>> + PSTACK_TREE_NO,
>> + PSTACK_TREE_INTERNAL,
>> + PSTACK_TREE_ROOT,
>> +};
> The names could be improved and 'state' doesn't quite fit imo.
> How about:
> enum bpf_priv_stack_mode {
> NO_PRIV_STACK,
> PRIV_STACK_SUB_PROG,
> PRIV_STACK_MAIN_PROG,
> };
Since we agreed to use priv_stack instead of pstack. The above
names make sense. Will change.
>
>> +
>> struct bpf_prog {
>> u16 pages; /* Number of allocated pages */
>> u16 jited:1, /* Is our filter JIT'ed? */
>> @@ -1570,15 +1576,18 @@ struct bpf_prog {
>> pstack_eligible:1; /* Candidate for private stacks */
>> enum bpf_prog_type type; /* Type of BPF program */
>> enum bpf_attach_type expected_attach_type; /* For some prog types */
>> + enum bpf_pstack_state pstack:2; /* Private stack state */
>> u32 len; /* Number of filter blocks */
>> u32 jited_len; /* Size of jited insns in bytes */
>> u8 tag[BPF_TAG_SIZE];
>> + u16 subtree_stack_depth; /* Subtree stack depth if PSTACK_TREE_ROOT prog, 0 otherwise */
> All the extra vars can be in prog->aux.
> No need to put them in struct bpf_prog.
Will do.
>
>> struct bpf_prog_stats __percpu *stats;
>> int __percpu *active;
>> unsigned int (*bpf_func)(const void *ctx,
>> const struct bpf_insn *insn);
>> struct bpf_prog_aux *aux; /* Auxiliary fields */
>> struct sock_fprog_kern *orig_prog; /* Original BPF program */
>> + void __percpu *private_stack_ptr;
> same as this one. prog->aux should be fine.
Will do.
>
>> /* Instructions for interpreter */
>> union {
>> DECLARE_FLEX_ARRAY(struct sock_filter, insns);
>> diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
>> index 0727fff6de0e..d6eb052f6631 100644
>> --- a/kernel/bpf/core.c
>> +++ b/kernel/bpf/core.c
>> @@ -1239,6 +1239,7 @@ void __weak bpf_jit_free(struct bpf_prog *fp)
>> struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
>>
>> bpf_jit_binary_free(hdr);
>> + free_percpu(fp->private_stack_ptr);
>> WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
>> }
>>
>> @@ -2420,6 +2421,24 @@ struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
>> if (*err)
>> return fp;
>>
>> + if (fp->pstack_eligible) {
>> + if (!fp->aux->stack_depth) {
>> + fp->pstack = PSTACK_TREE_NO;
>> + } else {
>> + void __percpu *private_stack_ptr;
>> +
>> + fp->pstack = PSTACK_TREE_ROOT;
>> + private_stack_ptr =
>> + __alloc_percpu_gfp(fp->aux->stack_depth, 8, GFP_KERNEL);
>> + if (!private_stack_ptr) {
>> + *err = -ENOMEM;
>> + return fp;
>> + }
>> + fp->subtree_stack_depth = fp->aux->stack_depth;
>> + fp->private_stack_ptr = private_stack_ptr;
>> + }
>> + }
>> +
>> fp = bpf_int_jit_compile(fp);
>> bpf_prog_jit_attempt_done(fp);
>> if (!fp->jited && jit_needed) {
>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
>> index 69e17cb22037..9d093e2013ca 100644
>> --- a/kernel/bpf/verifier.c
>> +++ b/kernel/bpf/verifier.c
>> @@ -20060,6 +20060,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
>> {
>> struct bpf_prog *prog = env->prog, **func, *tmp;
>> int i, j, subprog_start, subprog_end = 0, len, subprog;
>> + int subtree_top_idx, subtree_stack_depth;
>> struct bpf_map *map_ptr;
>> struct bpf_insn *insn;
>> void *old_bpf_func;
>> @@ -20138,6 +20139,35 @@ static int jit_subprogs(struct bpf_verifier_env *env)
>> func[i]->is_func = 1;
>> func[i]->sleepable = prog->sleepable;
>> func[i]->aux->func_idx = i;
>> +
>> + subtree_top_idx = env->subprog_info[i].subtree_top_idx;
>> + if (env->subprog_info[subtree_top_idx].pstack_eligible) {
>> + if (subtree_top_idx == i)
>> + func[i]->subtree_stack_depth =
>> + env->subprog_info[i].subtree_stack_depth;
>> +
>> + subtree_stack_depth = func[i]->subtree_stack_depth;
>> + if (subtree_top_idx != i) {
>> + if (env->subprog_info[subtree_top_idx].subtree_stack_depth)
>> + func[i]->pstack = PSTACK_TREE_INTERNAL;
>> + else
>> + func[i]->pstack = PSTACK_TREE_NO;
>> + } else if (!subtree_stack_depth) {
>> + func[i]->pstack = PSTACK_TREE_INTERNAL;
>> + } else {
>> + void __percpu *private_stack_ptr;
>> +
>> + func[i]->pstack = PSTACK_TREE_ROOT;
>> + private_stack_ptr =
>> + __alloc_percpu_gfp(subtree_stack_depth, 8, GFP_KERNEL);
>> + if (!private_stack_ptr) {
>> + err = -ENOMEM;
>> + goto out_free;
>> + }
>> + func[i]->private_stack_ptr = private_stack_ptr;
>> + }
>> + }
>> +
>> /* Below members will be freed only at prog->aux */
>> func[i]->aux->btf = prog->aux->btf;
>> func[i]->aux->func_info = prog->aux->func_info;
>> --
>> 2.43.5
>>
next prev parent reply other threads:[~2024-09-30 16:26 UTC|newest]
Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-26 23:45 [PATCH bpf-next v3 0/5] bpf: Support private stack for bpf progs Yonghong Song
2024-09-26 23:45 ` [PATCH bpf-next v3 1/5] bpf: Allow each subprog having stack size of 512 bytes Yonghong Song
2024-09-26 23:45 ` [PATCH bpf-next v3 2/5] bpf: Collect stack depth information Yonghong Song
2024-09-30 14:42 ` Alexei Starovoitov
2024-09-30 16:23 ` Yonghong Song
2024-09-26 23:45 ` [PATCH bpf-next v3 3/5] bpf: Mark each subprog with proper pstack states Yonghong Song
2024-09-30 14:49 ` Alexei Starovoitov
2024-09-30 16:26 ` Yonghong Song [this message]
2024-09-26 23:45 ` [PATCH bpf-next v3 4/5] bpf, x86: Add jit support for private stack Yonghong Song
2024-09-27 4:58 ` Leon Hwang
2024-09-27 15:24 ` Yonghong Song
2024-09-29 8:31 ` kernel test robot
2024-09-30 16:29 ` Yonghong Song
2024-09-29 13:02 ` kernel test robot
2024-09-30 16:31 ` Yonghong Song
2024-09-29 13:34 ` kernel test robot
2024-09-30 15:03 ` Alexei Starovoitov
2024-09-30 16:33 ` Yonghong Song
2024-10-01 4:31 ` Kumar Kartikeya Dwivedi
2024-10-01 4:37 ` Kumar Kartikeya Dwivedi
2024-10-01 18:49 ` Alexei Starovoitov
2024-10-01 19:53 ` yet another approach Was: " Alexei Starovoitov
2024-10-01 20:50 ` Kumar Kartikeya Dwivedi
2024-10-01 21:28 ` Alexei Starovoitov
2024-10-02 0:22 ` Kumar Kartikeya Dwivedi
2024-10-02 1:26 ` Alexei Starovoitov
2024-10-02 2:16 ` Kumar Kartikeya Dwivedi
2024-10-02 6:28 ` Yonghong Song
2024-10-02 6:48 ` Yonghong Song
2024-10-03 6:17 ` Yonghong Song
2024-10-03 13:39 ` Kumar Kartikeya Dwivedi
2024-10-03 17:35 ` Alexei Starovoitov
2024-10-03 18:53 ` Yonghong Song
2024-10-03 20:44 ` Yonghong Song
2024-10-03 20:47 ` Kumar Kartikeya Dwivedi
2024-10-03 20:54 ` Yonghong Song
2024-10-03 22:32 ` Alexei Starovoitov
2024-10-04 5:22 ` Yonghong Song
2024-10-04 19:27 ` Yonghong Song
2024-10-04 19:52 ` Alexei Starovoitov
2024-10-05 2:03 ` Yonghong Song
2024-10-08 22:10 ` Alexei Starovoitov
2024-10-09 2:06 ` Alexei Starovoitov
2024-10-09 6:31 ` Yonghong Song
2024-10-09 14:56 ` Alexei Starovoitov
2024-10-09 15:56 ` Yonghong Song
2024-10-09 16:36 ` Kumar Kartikeya Dwivedi
2024-10-09 16:38 ` Kumar Kartikeya Dwivedi
2024-10-09 17:37 ` Kumar Kartikeya Dwivedi
2024-10-09 6:12 ` Yonghong Song
2024-09-26 23:45 ` [PATCH bpf-next v3 5/5] selftests/bpf: Add private stack tests Yonghong Song
2024-09-30 13:40 ` Jiri Olsa
2024-09-30 15:05 ` Alexei Starovoitov
2024-09-30 16:35 ` Yonghong Song
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=75341619-0f3d-4d36-bbbd-a3128bca34c3@linux.dev \
--to=yonghong.song@linux.dev \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@fb.com \
--cc=martin.lau@kernel.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.