Re: [PATCH v3 bpf-next 08/13] bpf, x86: add support for indirect jumps

Anton Protopopov
2025-09-29T14:04:35Z

On 25/09/25 06:07PM, Anton Protopopov wrote:
> On 25/09/19 05:28PM, Eduard Zingerman wrote:
> > On Thu, 2025-09-18 at 09:38 +0000, Anton Protopopov wrote:
> [...]
> > > +static int check_indirect_jump(struct bpf_verifier_env *env, struct bpf_insn *insn)
> > > +{
> > > + struct bpf_verifier_state *other_branch;
> > > + struct bpf_reg_state *dst_reg;
> > > + struct bpf_map *map;
> > > + u32 min_index, max_index;
> > > + int err = 0;
> > > + u32 *xoff;
> > > + int n;
> > > + int i;
> > > +
> > > + dst_reg = reg_state(env, insn->dst_reg);
> > > + if (dst_reg->type != PTR_TO_INSN) {
> > > + verbose(env, "R%d has type %d, expected PTR_TO_INSN\n",
> > > + insn->dst_reg, dst_reg->type);
> > > + return -EINVAL;
> > > + }
> > > +
> > > + map = dst_reg->map_ptr;
> > > + if (verifier_bug_if(!map, env, "R%d has an empty map pointer", insn->dst_reg))
> > > + return -EFAULT;
> > > +
> > > + if (verifier_bug_if(map->map_type != BPF_MAP_TYPE_INSN_ARRAY, env,
> > > + "R%d has incorrect map type %d", insn->dst_reg, map->map_type))
> > > + return -EFAULT;
> > > +
> > > + err = indirect_jump_min_max_index(env, insn->dst_reg, map, &min_index, &max_index);
> > > + if (err)
> > > + return err;
> > > +
> > > + xoff = kvcalloc(max_index - min_index + 1, sizeof(u32), GFP_KERNEL_ACCOUNT);
> > > + if (!xoff)
> > > + return -ENOMEM;
> >
> > Let's keep a buffer for this allocation in `env` and realloc it when needed.
> > Would be good to avoid allocating memory each time this gotox is visited.
>
> Ok (to put it in bpf_subprog_info as suggested in your next letter).
> Though, probably it still needs to grow (= realloc).

On a second thought, this doesn't work in general case: hard to avoid
copying/sorting. Every other request wants to do sort(M[start, end]).
All three things are vaiables: M, start, end. For now I will just add
a buffer to avoid allocations.

> …