Commit 5762a20b authored by Daniel Borkmann's avatar Daniel Borkmann

Merge branch 'bpf-explored-states'

Alexei Starovoitov says:

====================
Convert explored_states array into hash table and use simple hash
to reduce verifier peak memory consumption for programs with bpf2bpf
calls. More details in patch 3.

v1->v2: fixed Jakub's small nit in patch 1
====================
Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parents 29c677c8 dc2a4ebc
...@@ -187,6 +187,7 @@ struct bpf_func_state { ...@@ -187,6 +187,7 @@ struct bpf_func_state {
struct bpf_verifier_state { struct bpf_verifier_state {
/* call stack tracking */ /* call stack tracking */
struct bpf_func_state *frame[MAX_CALL_FRAMES]; struct bpf_func_state *frame[MAX_CALL_FRAMES];
u32 insn_idx;
u32 curframe; u32 curframe;
u32 active_spin_lock; u32 active_spin_lock;
bool speculative; bool speculative;
...@@ -233,6 +234,7 @@ struct bpf_insn_aux_data { ...@@ -233,6 +234,7 @@ struct bpf_insn_aux_data {
int sanitize_stack_off; /* stack slot to be cleared */ int sanitize_stack_off; /* stack slot to be cleared */
bool seen; /* this insn was processed by the verifier */ bool seen; /* this insn was processed by the verifier */
u8 alu_state; /* used in combination with alu_limit */ u8 alu_state; /* used in combination with alu_limit */
bool prune_point;
unsigned int orig_idx; /* original instruction index */ unsigned int orig_idx; /* original instruction index */
}; };
......
...@@ -5436,7 +5436,25 @@ enum { ...@@ -5436,7 +5436,25 @@ enum {
BRANCH = 2, BRANCH = 2,
}; };
#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) static u32 state_htab_size(struct bpf_verifier_env *env)
{
return env->prog->len;
}
static struct bpf_verifier_state_list **explored_state(
struct bpf_verifier_env *env,
int idx)
{
struct bpf_verifier_state *cur = env->cur_state;
struct bpf_func_state *state = cur->frame[cur->curframe];
return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)];
}
static void init_explored_state(struct bpf_verifier_env *env, int idx)
{
env->insn_aux_data[idx].prune_point = true;
}
/* t, w, e - match pseudo-code above: /* t, w, e - match pseudo-code above:
* t - index of current instruction * t - index of current instruction
...@@ -5462,7 +5480,7 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env) ...@@ -5462,7 +5480,7 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
if (e == BRANCH) if (e == BRANCH)
/* mark branch target for state pruning */ /* mark branch target for state pruning */
env->explored_states[w] = STATE_LIST_MARK; init_explored_state(env, w);
if (insn_state[w] == 0) { if (insn_state[w] == 0) {
/* tree-edge */ /* tree-edge */
...@@ -5530,9 +5548,9 @@ static int check_cfg(struct bpf_verifier_env *env) ...@@ -5530,9 +5548,9 @@ static int check_cfg(struct bpf_verifier_env *env)
else if (ret < 0) else if (ret < 0)
goto err_free; goto err_free;
if (t + 1 < insn_cnt) if (t + 1 < insn_cnt)
env->explored_states[t + 1] = STATE_LIST_MARK; init_explored_state(env, t + 1);
if (insns[t].src_reg == BPF_PSEUDO_CALL) { if (insns[t].src_reg == BPF_PSEUDO_CALL) {
env->explored_states[t] = STATE_LIST_MARK; init_explored_state(env, t);
ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env); ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env);
if (ret == 1) if (ret == 1)
goto peek_stack; goto peek_stack;
...@@ -5555,10 +5573,10 @@ static int check_cfg(struct bpf_verifier_env *env) ...@@ -5555,10 +5573,10 @@ static int check_cfg(struct bpf_verifier_env *env)
* after every call and jump * after every call and jump
*/ */
if (t + 1 < insn_cnt) if (t + 1 < insn_cnt)
env->explored_states[t + 1] = STATE_LIST_MARK; init_explored_state(env, t + 1);
} else { } else {
/* conditional jump with two edges */ /* conditional jump with two edges */
env->explored_states[t] = STATE_LIST_MARK; init_explored_state(env, t);
ret = push_insn(t, t + 1, FALLTHROUGH, env); ret = push_insn(t, t + 1, FALLTHROUGH, env);
if (ret == 1) if (ret == 1)
goto peek_stack; goto peek_stack;
...@@ -6006,12 +6024,10 @@ static void clean_live_states(struct bpf_verifier_env *env, int insn, ...@@ -6006,12 +6024,10 @@ static void clean_live_states(struct bpf_verifier_env *env, int insn,
struct bpf_verifier_state_list *sl; struct bpf_verifier_state_list *sl;
int i; int i;
sl = env->explored_states[insn]; sl = *explored_state(env, insn);
if (!sl) while (sl) {
return; if (sl->state.insn_idx != insn ||
sl->state.curframe != cur->curframe)
while (sl != STATE_LIST_MARK) {
if (sl->state.curframe != cur->curframe)
goto next; goto next;
for (i = 0; i <= cur->curframe; i++) for (i = 0; i <= cur->curframe; i++)
if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) if (sl->state.frame[i]->callsite != cur->frame[i]->callsite)
...@@ -6365,18 +6381,21 @@ static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) ...@@ -6365,18 +6381,21 @@ static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
struct bpf_verifier_state *cur = env->cur_state, *new; struct bpf_verifier_state *cur = env->cur_state, *new;
int i, j, err, states_cnt = 0; int i, j, err, states_cnt = 0;
pprev = &env->explored_states[insn_idx]; if (!env->insn_aux_data[insn_idx].prune_point)
sl = *pprev;
if (!sl)
/* this 'insn_idx' instruction wasn't marked, so we will not /* this 'insn_idx' instruction wasn't marked, so we will not
* be doing state search here * be doing state search here
*/ */
return 0; return 0;
pprev = explored_state(env, insn_idx);
sl = *pprev;
clean_live_states(env, insn_idx, cur); clean_live_states(env, insn_idx, cur);
while (sl != STATE_LIST_MARK) { while (sl) {
states_cnt++;
if (sl->state.insn_idx != insn_idx)
goto next;
if (states_equal(env, &sl->state, cur)) { if (states_equal(env, &sl->state, cur)) {
sl->hit_cnt++; sl->hit_cnt++;
/* reached equivalent register/stack state, /* reached equivalent register/stack state,
...@@ -6394,7 +6413,6 @@ static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) ...@@ -6394,7 +6413,6 @@ static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
return err; return err;
return 1; return 1;
} }
states_cnt++;
sl->miss_cnt++; sl->miss_cnt++;
/* heuristic to determine whether this state is beneficial /* heuristic to determine whether this state is beneficial
* to keep checking from state equivalence point of view. * to keep checking from state equivalence point of view.
...@@ -6421,6 +6439,7 @@ static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) ...@@ -6421,6 +6439,7 @@ static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
sl = *pprev; sl = *pprev;
continue; continue;
} }
next:
pprev = &sl->next; pprev = &sl->next;
sl = *pprev; sl = *pprev;
} }
...@@ -6452,8 +6471,9 @@ static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) ...@@ -6452,8 +6471,9 @@ static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
kfree(new_sl); kfree(new_sl);
return err; return err;
} }
new_sl->next = env->explored_states[insn_idx]; new->insn_idx = insn_idx;
env->explored_states[insn_idx] = new_sl; new_sl->next = *explored_state(env, insn_idx);
*explored_state(env, insn_idx) = new_sl;
/* connect new state to parentage chain. Current frame needs all /* connect new state to parentage chain. Current frame needs all
* registers connected. Only r6 - r9 of the callers are alive (pushed * registers connected. Only r6 - r9 of the callers are alive (pushed
* to the stack implicitly by JITs) so in callers' frames connect just * to the stack implicitly by JITs) so in callers' frames connect just
...@@ -8131,16 +8151,15 @@ static void free_states(struct bpf_verifier_env *env) ...@@ -8131,16 +8151,15 @@ static void free_states(struct bpf_verifier_env *env)
if (!env->explored_states) if (!env->explored_states)
return; return;
for (i = 0; i < env->prog->len; i++) { for (i = 0; i < state_htab_size(env); i++) {
sl = env->explored_states[i]; sl = env->explored_states[i];
if (sl) while (sl) {
while (sl != STATE_LIST_MARK) { sln = sl->next;
sln = sl->next; free_verifier_state(&sl->state, false);
free_verifier_state(&sl->state, false); kfree(sl);
kfree(sl); sl = sln;
sl = sln; }
}
} }
kvfree(env->explored_states); kvfree(env->explored_states);
...@@ -8240,7 +8259,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, ...@@ -8240,7 +8259,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
goto skip_full_check; goto skip_full_check;
} }
env->explored_states = kvcalloc(env->prog->len, env->explored_states = kvcalloc(state_htab_size(env),
sizeof(struct bpf_verifier_state_list *), sizeof(struct bpf_verifier_state_list *),
GFP_USER); GFP_USER);
ret = -ENOMEM; ret = -ENOMEM;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment