bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: bpf <bpf@vger.kernel.org>, Alexei Starovoitov <ast@kernel.org>,
	Cong Wang <xiyou.wangcong@gmail.com>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Kernel Team <kernel-team@fb.com>
Subject: Re: [PATCH bpf-next v3 03/11] bpf: refactor check_func_call() to allow callback function
Date: Thu, 25 Feb 2021 16:08:00 -0800	[thread overview]
Message-ID: <097fc269-07d8-1610-970e-a72900dae71d@fb.com> (raw)
In-Reply-To: <CAEf4BzbdNTc4wqnhPPhfQeO0rARMHNocZ28xgR6cY1OVDAti1w@mail.gmail.com>



On 2/25/21 2:31 PM, Andrii Nakryiko wrote:
> On Thu, Feb 25, 2021 at 2:05 PM Andrii Nakryiko
> <andrii.nakryiko@gmail.com> wrote:
>>
>> On Thu, Feb 25, 2021 at 1:35 AM Yonghong Song <yhs@fb.com> wrote:
>>>
>>> Later proposed bpf_for_each_map_elem() helper has callback
>>> function as one of its arguments. This patch refactored
>>> check_func_call() to permit callback function which sets
>>> callee state. Different callback functions may have
>>> different callee states.
>>>
>>> There is no functionality change for this patch except
>>> it added a case to handle where subprog number is known
>>> and there is no need to do find_subprog(). This case
>>> is used later by implementing bpf_for_each_map() helper.
>>>
>>> Signed-off-by: Yonghong Song <yhs@fb.com>
>>> ---
>>>   kernel/bpf/verifier.c | 54 ++++++++++++++++++++++++++++++++-----------
>>>   1 file changed, 41 insertions(+), 13 deletions(-)
>>>
>>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
>>> index a657860ecba5..092d2c734dd8 100644
>>> --- a/kernel/bpf/verifier.c
>>> +++ b/kernel/bpf/verifier.c
>>> @@ -5250,13 +5250,19 @@ static void clear_caller_saved_regs(struct bpf_verifier_env *env,
>>>          }
>>>   }
>>>
>>> -static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
>>> -                          int *insn_idx)
>>> +typedef int (*set_callee_state_fn)(struct bpf_verifier_env *env,
>>> +                                  struct bpf_func_state *caller,
>>> +                                  struct bpf_func_state *callee,
>>> +                                  int insn_idx);
>>> +
>>> +static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
>>> +                            int *insn_idx, int subprog,
> 
> ok, patch #4 confused me because of this `int *insn_idx`. You don't
> seem to be ever updating it, so why pass it by pointer?... What did I
> miss?

We do have something later:

         /* and go analyze first insn of the callee */
         *insn_idx = target_insn;

which is the old code and probably did not show up in the diff.
The above statement changed insn_idx such that when done with
examining the func call, the control will jump (*insn_idx)++ instruction.

> 
>>> +                            set_callee_state_fn set_callee_st)
>>
>> nit: s/set_callee_st/set_callee_state_cb|set_calle_state_fn/
>>
>> _st is quite an unusual suffix
>>
>>>   {
>>>          struct bpf_verifier_state *state = env->cur_state;
>>>          struct bpf_func_info_aux *func_info_aux;
>>>          struct bpf_func_state *caller, *callee;
>>> -       int i, err, subprog, target_insn;
>>> +       int err, target_insn;
>>>          bool is_global = false;
>>>
>>>          if (state->curframe + 1 >= MAX_CALL_FRAMES) {
>>> @@ -5265,12 +5271,16 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
>>>                  return -E2BIG;
>>>          }
>>>
>>> -       target_insn = *insn_idx + insn->imm;
>>> -       subprog = find_subprog(env, target_insn + 1);
>>>          if (subprog < 0) {
>>> -               verbose(env, "verifier bug. No program starts at insn %d\n",
>>> -                       target_insn + 1);
>>> -               return -EFAULT;
>>> +               target_insn = *insn_idx + insn->imm;
>>> +               subprog = find_subprog(env, target_insn + 1);
>>> +               if (subprog < 0) {
>>> +                       verbose(env, "verifier bug. No program starts at insn %d\n",
>>> +                               target_insn + 1);
>>> +                       return -EFAULT;
>>> +               }
>>> +       } else {
>>> +               target_insn = env->subprog_info[subprog].start - 1;
>>>          }
>>>
>>>          caller = state->frame[state->curframe];
>>> @@ -5327,11 +5337,9 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
>>>          if (err)
>>>                  return err;
>>>
>>> -       /* copy r1 - r5 args that callee can access.  The copy includes parent
>>> -        * pointers, which connects us up to the liveness chain
>>> -        */
>>> -       for (i = BPF_REG_1; i <= BPF_REG_5; i++)
>>> -               callee->regs[i] = caller->regs[i];
>>> +       err = set_callee_st(env, caller, callee, *insn_idx);
>>> +       if (err)
>>> +               return err;
>>>
>>>          clear_caller_saved_regs(env, caller->regs);
>>>
>>> @@ -5350,6 +5358,26 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
>>>          return 0;
>>>   }
>>>
>>> +static int set_callee_state(struct bpf_verifier_env *env,
>>> +                           struct bpf_func_state *caller,
>>> +                           struct bpf_func_state *callee, int insn_idx)
>>> +{
>>> +       int i;
>>> +
>>> +       /* copy r1 - r5 args that callee can access.  The copy includes parent
>>> +        * pointers, which connects us up to the liveness chain
>>> +        */
>>> +       for (i = BPF_REG_1; i <= BPF_REG_5; i++)
>>> +               callee->regs[i] = caller->regs[i];
>>> +       return 0;
>>> +}
>>> +
>>> +static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
>>> +                          int *insn_idx)
>>> +{
>>> +       return __check_func_call(env, insn, insn_idx, -1, set_callee_state);
>>
>> I think it would be much cleaner to not have this -1 special case in
>> __check_func_call and instead search for the right subprog right here
>> in check_func_call(). Related question, is meta.subprogno (in patch
>> #4) expected to sometimes be < 0? If not, then I think
>> __check_func_call() definitely shouldn't support -1 case at all.
>>
>>
>>> +}
>>> +
>>>   static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
>>>   {
>>>          struct bpf_verifier_state *state = env->cur_state;
>>> --
>>> 2.24.1
>>>

  reply	other threads:[~2021-02-26  0:09 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-25  7:33 [PATCH bpf-next v3 00/11] bpf: add bpf_for_each_map_elem() helper Yonghong Song
2021-02-25  7:33 ` [PATCH bpf-next v3 01/11] bpf: factor out visit_func_call_insn() in check_cfg() Yonghong Song
2021-02-25 21:54   ` Andrii Nakryiko
2021-02-25 22:01     ` Alexei Starovoitov
2021-02-25  7:33 ` [PATCH bpf-next v3 02/11] bpf: factor out verbose_invalid_scalar() Yonghong Song
2021-02-25 21:56   ` Andrii Nakryiko
2021-02-25  7:33 ` [PATCH bpf-next v3 03/11] bpf: refactor check_func_call() to allow callback function Yonghong Song
2021-02-25 22:05   ` Andrii Nakryiko
2021-02-25 22:31     ` Andrii Nakryiko
2021-02-26  0:08       ` Yonghong Song [this message]
2021-02-26  1:18         ` Andrii Nakryiko
2021-02-26  0:05     ` Yonghong Song
2021-02-25  7:33 ` [PATCH bpf-next v3 04/11] bpf: add bpf_for_each_map_elem() helper Yonghong Song
2021-02-25 22:41   ` Andrii Nakryiko
2021-02-26  2:16     ` Yonghong Song
2021-02-26  3:22       ` Yonghong Song
2021-02-26  2:27   ` Cong Wang
2021-02-26  3:27     ` Yonghong Song
2021-02-25  7:33 ` [PATCH bpf-next v3 05/11] bpf: add hashtab support for " Yonghong Song
2021-02-25 22:44   ` Andrii Nakryiko
2021-02-25  7:33 ` [PATCH bpf-next v3 06/11] bpf: add arraymap " Yonghong Song
2021-02-25 22:48   ` Andrii Nakryiko
2021-02-25  7:33 ` [PATCH bpf-next v3 07/11] libbpf: move function is_ldimm64() earlier in libbpf.c Yonghong Song
2021-02-25  7:33 ` [PATCH bpf-next v3 08/11] libbpf: support subprog address relocation Yonghong Song
2021-02-25 23:04   ` Andrii Nakryiko
2021-02-25  7:33 ` [PATCH bpf-next v3 09/11] bpftool: print subprog address properly Yonghong Song
2021-02-25 23:04   ` Andrii Nakryiko
2021-02-25  7:33 ` [PATCH bpf-next v3 10/11] selftests/bpf: add hashmap test for bpf_for_each_map_elem() helper Yonghong Song
2021-02-25 23:25   ` Andrii Nakryiko
2021-02-26  3:24     ` Yonghong Song
2021-02-25  7:33 ` [PATCH bpf-next v3 11/11] selftests/bpf: add arraymap " Yonghong Song
2021-02-25 23:26   ` Andrii Nakryiko

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=097fc269-07d8-1610-970e-a72900dae71d@fb.com \
    --to=yhs@fb.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@fb.com \
    --cc=xiyou.wangcong@gmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).