return 0;
 }
 
-static bool is_dynptr_reg_valid_uninit(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
+static bool is_dynptr_reg_valid_uninit(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
+                                      int spi)
 {
-       int spi;
-
        if (reg->type == CONST_PTR_TO_DYNPTR)
                return false;
 
-       spi = dynptr_get_spi(env, reg);
        /* For -ERANGE (i.e. spi not falling into allocated stack slots), we
         * will do check_mem_access to check and update stack bounds later, so
         * return true for that case.
        return true;
 }
 
-static bool is_dynptr_reg_valid_init(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
+static bool is_dynptr_reg_valid_init(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
+                                    int spi)
 {
        struct bpf_func_state *state = func(env, reg);
-       int spi, i;
+       int i;
 
        /* This already represents first slot of initialized bpf_dynptr */
        if (reg->type == CONST_PTR_TO_DYNPTR)
                return true;
 
-       spi = dynptr_get_spi(env, reg);
        if (spi < 0)
                return false;
        if (!state->stack[spi].spilled_ptr.dynptr.first_slot)
                        enum bpf_arg_type arg_type, struct bpf_call_arg_meta *meta)
 {
        struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno];
+       int spi = 0;
 
        /* MEM_UNINIT and MEM_RDONLY are exclusive, when applied to an
         * ARG_PTR_TO_DYNPTR (or ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_*):
         * and its alignment for PTR_TO_STACK.
         */
        if (reg->type == PTR_TO_STACK) {
-               int err = dynptr_get_spi(env, reg);
-
-               if (err < 0 && err != -ERANGE)
-                       return err;
+               spi = dynptr_get_spi(env, reg);
+               if (spi < 0 && spi != -ERANGE)
+                       return spi;
        }
 
        /*  MEM_UNINIT - Points to memory that is an appropriate candidate for
         *               to.
         */
        if (arg_type & MEM_UNINIT) {
-               if (!is_dynptr_reg_valid_uninit(env, reg)) {
+               if (!is_dynptr_reg_valid_uninit(env, reg, spi)) {
                        verbose(env, "Dynptr has to be an uninitialized dynptr\n");
                        return -EINVAL;
                }
                        return -EINVAL;
                }
 
-               if (!is_dynptr_reg_valid_init(env, reg)) {
+               if (!is_dynptr_reg_valid_init(env, reg, spi)) {
                        verbose(env,
                                "Expected an initialized dynptr as arg #%d\n",
                                regno);