From: Daniel Borkmann <daniel@iogearbox.net>
To: Eduard Zingerman <eddyz87@gmail.com>,
bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org,
kernel-team@fb.com, song@kernel.org, joannelkoong@gmail.com
Subject: Re: [PATCH bpf-next v7 3/5] bpf: Inline calls to bpf_loop when callback is known
Date: Fri, 17 Jun 2022 01:12:44 +0200 [thread overview]
Message-ID: <1ad45d14-e917-82f0-e4ab-121c2027c0d6@iogearbox.net> (raw)
In-Reply-To: <20220613205008.212724-4-eddyz87@gmail.com>
On 6/13/22 10:50 PM, Eduard Zingerman wrote:
[...]
> diff --git a/kernel/bpf/bpf_iter.c b/kernel/bpf/bpf_iter.c
> index d5d96ceca105..7e8fd49406f6 100644
> --- a/kernel/bpf/bpf_iter.c
> +++ b/kernel/bpf/bpf_iter.c
> @@ -723,9 +723,6 @@ const struct bpf_func_proto bpf_for_each_map_elem_proto = {
> .arg4_type = ARG_ANYTHING,
> };
>
> -/* maximum number of loops */
> -#define MAX_LOOPS BIT(23)
> -
> BPF_CALL_4(bpf_loop, u32, nr_loops, void *, callback_fn, void *, callback_ctx,
> u64, flags)
> {
> @@ -733,9 +730,13 @@ BPF_CALL_4(bpf_loop, u32, nr_loops, void *, callback_fn, void *, callback_ctx,
> u64 ret;
> u32 i;
>
> + /* Note: these safety checks are also verified when bpf_loop
> + * is inlined, be careful to modify this code in sync. See
> + * function verifier.c:inline_bpf_loop.
> + */
> if (flags)
> return -EINVAL;
> - if (nr_loops > MAX_LOOPS)
> + if (nr_loops > BPF_MAX_LOOPS)
> return -E2BIG;
>
> for (i = 0; i < nr_loops; i++) {
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 2d2872682278..db854c09b603 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -7103,6 +7103,38 @@ static int check_get_func_ip(struct bpf_verifier_env *env)
> return -ENOTSUPP;
> }
>
> +static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env)
> +{
> + return &env->insn_aux_data[env->insn_idx];
> +}
> +
> +static bool loop_flag_is_zero(struct bpf_verifier_env *env)
> +{
> + struct bpf_reg_state *regs = cur_regs(env);
> + struct bpf_reg_state *reg = ®s[BPF_REG_4];
> +
> + return register_is_const(reg) && reg->var_off.value == 0;
I think you might also need to add precision tracking for the flag check :
mark_chain_precision(env, BPF_REG_4)
See also cc52d9140aa92 ("bpf: Fix record_func_key to perform backtracking on r3").. not too
much of an issue at the moment, but once we extend flags.
> +}
> +
> +static void update_loop_inline_state(struct bpf_verifier_env *env, u32 subprogno)
> +{
> + struct bpf_loop_inline_state *state = &cur_aux(env)->loop_inline_state;
> +
> + if (!state->initialized) {
> + state->initialized = 1;
> + state->fit_for_inline = loop_flag_is_zero(env);
> + state->callback_subprogno = subprogno;
> + return;
> + }
> +
> + if (!state->fit_for_inline)
> + return;
> +
> + state->fit_for_inline =
> + loop_flag_is_zero(env) &&
> + state->callback_subprogno == subprogno;
> +}
> +
> static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
> int *insn_idx_p)
> {
> @@ -7255,6 +7287,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
> err = check_bpf_snprintf_call(env, regs);
> break;
> case BPF_FUNC_loop:
> + update_loop_inline_state(env, meta.subprogno);
> err = __check_func_call(env, insn, insn_idx_p, meta.subprogno,
> set_loop_callback_state);
> break;
next prev parent reply other threads:[~2022-06-16 23:12 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-13 20:50 [PATCH bpf-next v7 0/5] bpf_loop inlining Eduard Zingerman
2022-06-13 20:50 ` [PATCH bpf-next v7 1/5] selftests/bpf: specify expected instructions in test_verifier tests Eduard Zingerman
2022-06-13 20:50 ` [PATCH bpf-next v7 2/5] selftests/bpf: allow BTF specs and func infos " Eduard Zingerman
2022-06-13 20:50 ` [PATCH bpf-next v7 3/5] bpf: Inline calls to bpf_loop when callback is known Eduard Zingerman
2022-06-14 5:49 ` Song Liu
2022-06-16 23:12 ` Daniel Borkmann [this message]
2022-06-17 2:14 ` Alexei Starovoitov
2022-06-19 20:09 ` Eduard Zingerman
2022-06-19 21:10 ` Alexei Starovoitov
2022-06-19 22:01 ` Eduard Zingerman
2022-06-19 23:37 ` Alexei Starovoitov
2022-06-20 12:59 ` Eduard Zingerman
2022-06-13 20:50 ` [PATCH bpf-next v7 4/5] selftests/bpf: BPF test_verifier selftests for bpf_loop inlining Eduard Zingerman
2022-06-13 20:50 ` [PATCH bpf-next v7 5/5] selftests/bpf: BPF test_prog " Eduard Zingerman
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=1ad45d14-e917-82f0-e4ab-121c2027c0d6@iogearbox.net \
--to=daniel@iogearbox.net \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=eddyz87@gmail.com \
--cc=joannelkoong@gmail.com \
--cc=kernel-team@fb.com \
--cc=song@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox