Skip to content

Commit efcda22

Browse files
eddyz87Alexei Starovoitov
authored andcommitted
bpf: compute instructions postorder per subprogram
The next patch would require doing postorder traversal of individual subprograms. Facilitate this by moving env->cfg.insn_postorder computation from check_cfg() to a separate pass, as check_cfg() descends into called subprograms (and it needs to, because of merge_callee_effects() logic). env->cfg.insn_postorder is used only by compute_live_registers(), this function does not track cross subprogram dependencies, thus the change does not affect it's operation. Signed-off-by: Eduard Zingerman <eddyz87@gmail.com> Link: https://lore.kernel.org/r/20250918-callchain-sensitive-liveness-v3-5-c3cd27bacc60@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 3b20d3c commit efcda22

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed

include/linux/bpf_verifier.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ struct bpf_subprog_info {
665665
/* 'start' has to be the first field otherwise find_subprog() won't work */
666666
u32 start; /* insn idx of function entry point */
667667
u32 linfo_idx; /* The idx to the main_prog->aux->linfo */
668+
u32 postorder_start; /* The idx to the env->cfg.insn_postorder */
668669
u16 stack_depth; /* max. stack depth used by this function */
669670
u16 stack_extra;
670671
/* offsets in range [stack_depth .. fastcall_stack_off)
@@ -794,7 +795,10 @@ struct bpf_verifier_env {
794795
struct {
795796
int *insn_state;
796797
int *insn_stack;
797-
/* vector of instruction indexes sorted in post-order */
798+
/*
799+
* vector of instruction indexes sorted in post-order, grouped by subprogram,
800+
* see bpf_subprog_info->postorder_start.
801+
*/
798802
int *insn_postorder;
799803
int cur_stack;
800804
/* current position in the insn_postorder vector */

kernel/bpf/verifier.c

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17863,7 +17863,7 @@ static int visit_insn(int t, struct bpf_verifier_env *env)
1786317863
static int check_cfg(struct bpf_verifier_env *env)
1786417864
{
1786517865
int insn_cnt = env->prog->len;
17866-
int *insn_stack, *insn_state, *insn_postorder;
17866+
int *insn_stack, *insn_state;
1786717867
int ex_insn_beg, i, ret = 0;
1786817868

1786917869
insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL_ACCOUNT);
@@ -17876,14 +17876,6 @@ static int check_cfg(struct bpf_verifier_env *env)
1787617876
return -ENOMEM;
1787717877
}
1787817878

17879-
insn_postorder = env->cfg.insn_postorder =
17880-
kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL_ACCOUNT);
17881-
if (!insn_postorder) {
17882-
kvfree(insn_state);
17883-
kvfree(insn_stack);
17884-
return -ENOMEM;
17885-
}
17886-
1788717879
ex_insn_beg = env->exception_callback_subprog
1788817880
? env->subprog_info[env->exception_callback_subprog].start
1788917881
: 0;
@@ -17901,7 +17893,6 @@ static int check_cfg(struct bpf_verifier_env *env)
1790117893
case DONE_EXPLORING:
1790217894
insn_state[t] = EXPLORED;
1790317895
env->cfg.cur_stack--;
17904-
insn_postorder[env->cfg.cur_postorder++] = t;
1790517896
break;
1790617897
case KEEP_EXPLORING:
1790717898
break;
@@ -17955,6 +17946,56 @@ static int check_cfg(struct bpf_verifier_env *env)
1795517946
return ret;
1795617947
}
1795717948

17949+
/*
17950+
* For each subprogram 'i' fill array env->cfg.insn_subprogram sub-range
17951+
* [env->subprog_info[i].postorder_start, env->subprog_info[i+1].postorder_start)
17952+
* with indices of 'i' instructions in postorder.
17953+
*/
17954+
static int compute_postorder(struct bpf_verifier_env *env)
17955+
{
17956+
u32 cur_postorder, i, top, stack_sz, s, succ_cnt, succ[2];
17957+
int *stack = NULL, *postorder = NULL, *state = NULL;
17958+
17959+
postorder = kvcalloc(env->prog->len, sizeof(int), GFP_KERNEL_ACCOUNT);
17960+
state = kvcalloc(env->prog->len, sizeof(int), GFP_KERNEL_ACCOUNT);
17961+
stack = kvcalloc(env->prog->len, sizeof(int), GFP_KERNEL_ACCOUNT);
17962+
if (!postorder || !state || !stack) {
17963+
kvfree(postorder);
17964+
kvfree(state);
17965+
kvfree(stack);
17966+
return -ENOMEM;
17967+
}
17968+
cur_postorder = 0;
17969+
for (i = 0; i < env->subprog_cnt; i++) {
17970+
env->subprog_info[i].postorder_start = cur_postorder;
17971+
stack[0] = env->subprog_info[i].start;
17972+
stack_sz = 1;
17973+
do {
17974+
top = stack[stack_sz - 1];
17975+
state[top] |= DISCOVERED;
17976+
if (state[top] & EXPLORED) {
17977+
postorder[cur_postorder++] = top;
17978+
stack_sz--;
17979+
continue;
17980+
}
17981+
succ_cnt = bpf_insn_successors(env->prog, top, succ);
17982+
for (s = 0; s < succ_cnt; ++s) {
17983+
if (!state[succ[s]]) {
17984+
stack[stack_sz++] = succ[s];
17985+
state[succ[s]] |= DISCOVERED;
17986+
}
17987+
}
17988+
state[top] |= EXPLORED;
17989+
} while (stack_sz);
17990+
}
17991+
env->subprog_info[i].postorder_start = cur_postorder;
17992+
env->cfg.insn_postorder = postorder;
17993+
env->cfg.cur_postorder = cur_postorder;
17994+
kvfree(stack);
17995+
kvfree(state);
17996+
return 0;
17997+
}
17998+
1795817999
static int check_abnormal_return(struct bpf_verifier_env *env)
1795918000
{
1796018001
int i;
@@ -24422,9 +24463,6 @@ static int compute_live_registers(struct bpf_verifier_env *env)
2442224463

2442324464
out:
2442424465
kvfree(state);
24425-
kvfree(env->cfg.insn_postorder);
24426-
env->cfg.insn_postorder = NULL;
24427-
env->cfg.cur_postorder = 0;
2442824466
return err;
2442924467
}
2443024468

@@ -24727,6 +24765,10 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3
2472724765
if (ret < 0)
2472824766
goto skip_full_check;
2472924767

24768+
ret = compute_postorder(env);
24769+
if (ret < 0)
24770+
goto skip_full_check;
24771+
2473024772
ret = check_attach_btf_id(env);
2473124773
if (ret)
2473224774
goto skip_full_check;

0 commit comments

Comments
 (0)