On Thu, Feb 25, 2021 at 1:35 AM Yonghong Song <yhs@xxxxxx> wrote: > > Factor out the function verbose_invalid_scalar() to verbose > print if a scalar is not in a tnum range. There is no > functionality change and the function will be used by > later patch which introduced bpf_for_each_map_elem(). > > Signed-off-by: Yonghong Song <yhs@xxxxxx> > --- Acked-by: Andrii Nakryiko <andrii@xxxxxxxxxx> > kernel/bpf/verifier.c | 31 ++++++++++++++++++++----------- > 1 file changed, 20 insertions(+), 11 deletions(-) > > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c > index 9cb182e91162..a657860ecba5 100644 > --- a/kernel/bpf/verifier.c > +++ b/kernel/bpf/verifier.c > @@ -390,6 +390,25 @@ __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, > env->prev_linfo = linfo; > } > > +static void verbose_invalid_scalar(struct bpf_verifier_env *env, > + struct bpf_reg_state *reg, > + struct tnum *range, const char *ctx, > + const char *reg_name) > +{ > + char tn_buf[48]; > + > + verbose(env, "At %s the register %s ", ctx, reg_name); > + if (!tnum_is_unknown(reg->var_off)) { > + tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); > + verbose(env, "has value %s", tn_buf); > + } else { > + verbose(env, "has unknown scalar value"); > + } > + tnum_strn(tn_buf, sizeof(tn_buf), *range); > + verbose(env, " should have been in %s\n", tn_buf); > +} > + > + nit: double empty line > static bool type_is_pkt_pointer(enum bpf_reg_type type) > { > return type == PTR_TO_PACKET || > @@ -8455,17 +8474,7 @@ static int check_return_code(struct bpf_verifier_env *env) > } > > if (!tnum_in(range, reg->var_off)) { > - char tn_buf[48]; > - > - verbose(env, "At program exit the register R0 "); > - if (!tnum_is_unknown(reg->var_off)) { > - tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); > - verbose(env, "has value %s", tn_buf); > - } else { > - verbose(env, "has unknown scalar value"); > - } > - tnum_strn(tn_buf, sizeof(tn_buf), range); > - verbose(env, " should have been in %s\n", tn_buf); > + verbose_invalid_scalar(env, reg, &range, "program exit", "R0"); > return -EINVAL; > } > > -- > 2.24.1 >