Skip to content

Commit 27e88bc

Browse files
eddyz87Alexei Starovoitov
authored andcommitted
bpf: add find_containing_subprog() utility function
Add a utility function, looking for a subprogram containing a given instruction index, rewrite find_subprog() to use this function. Signed-off-by: Eduard Zingerman <eddyz87@gmail.com> Link: https://lore.kernel.org/r/20241210041100.1898468-2-eddyz87@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 978c448 commit 27e88bc

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

kernel/bpf/verifier.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2597,16 +2597,36 @@ static int cmp_subprogs(const void *a, const void *b)
25972597
((struct bpf_subprog_info *)b)->start;
25982598
}
25992599

2600+
/* Find subprogram that contains instruction at 'off' */
2601+
static struct bpf_subprog_info *find_containing_subprog(struct bpf_verifier_env *env, int off)
2602+
{
2603+
struct bpf_subprog_info *vals = env->subprog_info;
2604+
int l, r, m;
2605+
2606+
if (off >= env->prog->len || off < 0 || env->subprog_cnt == 0)
2607+
return NULL;
2608+
2609+
l = 0;
2610+
r = env->subprog_cnt - 1;
2611+
while (l < r) {
2612+
m = l + (r - l + 1) / 2;
2613+
if (vals[m].start <= off)
2614+
l = m;
2615+
else
2616+
r = m - 1;
2617+
}
2618+
return &vals[l];
2619+
}
2620+
2621+
/* Find subprogram that starts exactly at 'off' */
26002622
static int find_subprog(struct bpf_verifier_env *env, int off)
26012623
{
26022624
struct bpf_subprog_info *p;
26032625

2604-
p = bsearch(&off, env->subprog_info, env->subprog_cnt,
2605-
sizeof(env->subprog_info[0]), cmp_subprogs);
2606-
if (!p)
2626+
p = find_containing_subprog(env, off);
2627+
if (!p || p->start != off)
26072628
return -ENOENT;
26082629
return p - env->subprog_info;
2609-
26102630
}
26112631

26122632
static int add_subprog(struct bpf_verifier_env *env, int off)

0 commit comments

Comments
 (0)