From: Alexei Starovoitov A loop can keep its counter in a stack slot. Scalar evolution calculates how many times such a loop runs only if no insn in the loop can write to the slot through a pointer. For a call of a helper, bpf_helper_stack_access_bytes() tells what the helper does through each argument. For an argument type that it does not list, it assumes a read and a write of any size. ARG_ANYTHING is such a type. strobemeta_nounroll1 calls bpf_probe_read_user_str() in a loop. r3 is the address to read from, and its type is ARG_ANYTHING. The liveness analysis cannot tell if r3 points to the stack. Thus it assumes that the call can write to all stack slots. One of them is the counter of the outer loop: loop header at 244, can't compute iterations count ARG_ANYTHING is a number that the prog selects freely. check_func_arg() does not examine it, and the main pass keeps what it knows about the stack over the call. Scalar evolution does not have to assume more. Clear may_write for ARG_ANYTHING. Only scalar evolution uses may_write. Keep may_read, because bpf_probe_read_kernel() can read the slot. loop header at 244, header_count is 13 The outer loops of strobemeta_nounroll1, strobemeta_nounroll2 and strobemeta_subprogs get a count. Each of them changes one more register that has no formula, thus the verifier still unrolls them. Signed-off-by: Alexei Starovoitov --- kernel/bpf/verifier.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index b480c5c242af..06e72aaf6769 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -15104,6 +15104,14 @@ bpf_helper_stack_access_bytes(struct bpf_verifier_env *env, struct bpf_insn *ins * analyzed separately. */ return (struct arg_access_info) {}; + case ARG_ANYTHING: + /* + * A number that the program is free to choose. bpf_probe_read_kernel() and + * the like may read what it points to. check_func_arg() does not look at + * the number: do_check() keeps what it knows about the stack over the call. + */ + info.may_write = false; + return info; default: return info; } -- 2.55.0