if (state->refs[i].id)
                                verbose(env, ",%d", state->refs[i].id);
        }
+       if (state->in_callback_fn)
+               verbose(env, " cb");
+       if (state->in_async_callback_fn)
+               verbose(env, " async_cb");
        verbose(env, "\n");
 }
 
        init_reg_state(env, state);
 }
 
+/* Similar to push_stack(), but for async callbacks */
+static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env,
+                                               int insn_idx, int prev_insn_idx,
+                                               int subprog)
+{
+       struct bpf_verifier_stack_elem *elem;
+       struct bpf_func_state *frame;
+
+       elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
+       if (!elem)
+               goto err;
+
+       elem->insn_idx = insn_idx;
+       elem->prev_insn_idx = prev_insn_idx;
+       elem->next = env->head;
+       elem->log_pos = env->log.len_used;
+       env->head = elem;
+       env->stack_size++;
+       if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) {
+               verbose(env,
+                       "The sequence of %d jumps is too complex for async cb.\n",
+                       env->stack_size);
+               goto err;
+       }
+       /* Unlike push_stack() do not copy_verifier_state().
+        * The caller state doesn't matter.
+        * This is async callback. It starts in a fresh stack.
+        * Initialize it similar to do_check_common().
+        */
+       elem->st.branches = 1;
+       frame = kzalloc(sizeof(*frame), GFP_KERNEL);
+       if (!frame)
+               goto err;
+       init_func_state(env, frame,
+                       BPF_MAIN_FUNC /* callsite */,
+                       0 /* frameno within this callchain */,
+                       subprog /* subprog number within this prog */);
+       elem->st.frame[0] = frame;
+       return &elem->st;
+err:
+       free_verifier_state(env->cur_state, true);
+       env->cur_state = NULL;
+       /* pop all elements and return */
+       while (!pop_stack(env, NULL, NULL, false));
+       return NULL;
+}
+
+
 enum reg_arg_type {
        SRC_OP,         /* register is used as source operand */
        DST_OP,         /* register is used as destination operand */
                }
        }
 
+       if (insn->code == (BPF_JMP | BPF_CALL) &&
+           insn->imm == BPF_FUNC_timer_set_callback) {
+               struct bpf_verifier_state *async_cb;
+
+               /* there is no real recursion here. timer callbacks are async */
+               async_cb = push_async_cb(env, env->subprog_info[subprog].start,
+                                        *insn_idx, subprog);
+               if (!async_cb)
+                       return -EFAULT;
+               callee = async_cb->frame[0];
+               callee->async_entry_cnt = caller->async_entry_cnt + 1;
+
+               /* Convert bpf_timer_set_callback() args into timer callback args */
+               err = set_callee_state_cb(env, caller, callee, *insn_idx);
+               if (err)
+                       return err;
+
+               clear_caller_saved_regs(env, caller->regs);
+               mark_reg_unknown(env, caller->regs, BPF_REG_0);
+               caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG;
+               /* continue with next insn after call */
+               return 0;
+       }
+
        callee = kzalloc(sizeof(*callee), GFP_KERNEL);
        if (!callee)
                return -ENOMEM;
        /* unused */
        __mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
        __mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
+       callee->in_async_callback_fn = true;
        return 0;
 }
 
        struct tnum range = tnum_range(0, 1);
        enum bpf_prog_type prog_type = resolve_prog_type(env->prog);
        int err;
-       const bool is_subprog = env->cur_state->frame[0]->subprogno;
+       struct bpf_func_state *frame = env->cur_state->frame[0];
+       const bool is_subprog = frame->subprogno;
 
        /* LSM and struct_ops func-ptr's return type could be "void" */
        if (!is_subprog &&
        }
 
        reg = cur_regs(env) + BPF_REG_0;
+
+       if (frame->in_async_callback_fn) {
+               /* enforce return zero from async callbacks like timer */
+               if (reg->type != SCALAR_VALUE) {
+                       verbose(env, "In async callback the register R0 is not a known value (%s)\n",
+                               reg_type_str[reg->type]);
+                       return -EINVAL;
+               }
+
+               if (!tnum_in(tnum_const(0), reg->var_off)) {
+                       verbose_invalid_scalar(env, reg, &range, "async callback", "R0");
+                       return -EINVAL;
+               }
+               return 0;
+       }
+
        if (is_subprog) {
                if (reg->type != SCALAR_VALUE) {
                        verbose(env, "At subprogram exit the register R0 is not a scalar value (%s)\n",
                return DONE_EXPLORING;
 
        case BPF_CALL:
+               if (insns[t].imm == BPF_FUNC_timer_set_callback)
+                       /* Mark this call insn to trigger is_state_visited() check
+                        * before call itself is processed by __check_func_call().
+                        * Otherwise new async state will be pushed for further
+                        * exploration.
+                        */
+                       init_explored_state(env, t);
                return visit_func_call_insn(t, insn_cnt, insns, env,
                                            insns[t].src_reg == BPF_PSEUDO_CALL);
 
                states_cnt++;
                if (sl->state.insn_idx != insn_idx)
                        goto next;
+
                if (sl->state.branches) {
-                       if (states_maybe_looping(&sl->state, cur) &&
-                           states_equal(env, &sl->state, cur)) {
+                       struct bpf_func_state *frame = sl->state.frame[sl->state.curframe];
+
+                       if (frame->in_async_callback_fn &&
+                           frame->async_entry_cnt != cur->frame[cur->curframe]->async_entry_cnt) {
+                               /* Different async_entry_cnt means that the verifier is
+                                * processing another entry into async callback.
+                                * Seeing the same state is not an indication of infinite
+                                * loop or infinite recursion.
+                                * But finding the same state doesn't mean that it's safe
+                                * to stop processing the current state. The previous state
+                                * hasn't yet reached bpf_exit, since state.branches > 0.
+                                * Checking in_async_callback_fn alone is not enough either.
+                                * Since the verifier still needs to catch infinite loops
+                                * inside async callbacks.
+                                */
+                       } else if (states_maybe_looping(&sl->state, cur) &&
+                                  states_equal(env, &sl->state, cur)) {
                                verbose_linfo(env, insn_idx, "; ");
                                verbose(env, "infinite loop detected at insn %d\n", insn_idx);
                                return -EINVAL;