Skip to content

Commit ffb9d5c

Browse files
lmbgregkh
authored andcommitted
bpf: verifier: Allocate idmap scratch in verifier env
commit c9e73e3 upstream. func_states_equal makes a very short lived allocation for idmap, probably because it's too large to fit on the stack. However the function is called quite often, leading to a lot of alloc / free churn. Replace the temporary allocation with dedicated scratch space in struct bpf_verifier_env. Signed-off-by: Lorenz Bauer <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Edward Cree <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent a11ca29 commit ffb9d5c

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

include/linux/bpf_verifier.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ struct bpf_idx_pair {
204204
u32 idx;
205205
};
206206

207+
struct bpf_id_pair {
208+
u32 old;
209+
u32 cur;
210+
};
211+
212+
/* Maximum number of register states that can exist at once */
213+
#define BPF_ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
207214
#define MAX_CALL_FRAMES 8
208215
struct bpf_verifier_state {
209216
/* call stack tracking */
@@ -401,6 +408,7 @@ struct bpf_verifier_env {
401408
const struct bpf_line_info *prev_linfo;
402409
struct bpf_verifier_log log;
403410
struct bpf_subprog_info subprog_info[BPF_MAX_SUBPROGS + 1];
411+
struct bpf_id_pair idmap_scratch[BPF_ID_MAP_SIZE];
404412
struct {
405413
int *insn_state;
406414
int *insn_stack;

kernel/bpf/verifier.c

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8962,13 +8962,6 @@ static bool range_within(struct bpf_reg_state *old,
89628962
old->s32_max_value >= cur->s32_max_value;
89638963
}
89648964

8965-
/* Maximum number of register states that can exist at once */
8966-
#define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
8967-
struct idpair {
8968-
u32 old;
8969-
u32 cur;
8970-
};
8971-
89728965
/* If in the old state two registers had the same id, then they need to have
89738966
* the same id in the new state as well. But that id could be different from
89748967
* the old state, so we need to track the mapping from old to new ids.
@@ -8979,11 +8972,11 @@ struct idpair {
89798972
* So we look through our idmap to see if this old id has been seen before. If
89808973
* so, we require the new id to match; otherwise, we add the id pair to the map.
89818974
*/
8982-
static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap)
8975+
static bool check_ids(u32 old_id, u32 cur_id, struct bpf_id_pair *idmap)
89838976
{
89848977
unsigned int i;
89858978

8986-
for (i = 0; i < ID_MAP_SIZE; i++) {
8979+
for (i = 0; i < BPF_ID_MAP_SIZE; i++) {
89878980
if (!idmap[i].old) {
89888981
/* Reached an empty slot; haven't seen this id before */
89898982
idmap[i].old = old_id;
@@ -9096,7 +9089,7 @@ static void clean_live_states(struct bpf_verifier_env *env, int insn,
90969089

90979090
/* Returns true if (rold safe implies rcur safe) */
90989091
static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
9099-
struct idpair *idmap)
9092+
struct bpf_id_pair *idmap)
91009093
{
91019094
bool equal;
91029095

@@ -9213,7 +9206,7 @@ static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
92139206

92149207
static bool stacksafe(struct bpf_func_state *old,
92159208
struct bpf_func_state *cur,
9216-
struct idpair *idmap)
9209+
struct bpf_id_pair *idmap)
92179210
{
92189211
int i, spi;
92199212

@@ -9310,32 +9303,23 @@ static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur)
93109303
* whereas register type in current state is meaningful, it means that
93119304
* the current state will reach 'bpf_exit' instruction safely
93129305
*/
9313-
static bool func_states_equal(struct bpf_func_state *old,
9306+
static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_state *old,
93149307
struct bpf_func_state *cur)
93159308
{
9316-
struct idpair *idmap;
9317-
bool ret = false;
93189309
int i;
93199310

9320-
idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL);
9321-
/* If we failed to allocate the idmap, just say it's not safe */
9322-
if (!idmap)
9323-
return false;
9324-
9325-
for (i = 0; i < MAX_BPF_REG; i++) {
9326-
if (!regsafe(&old->regs[i], &cur->regs[i], idmap))
9327-
goto out_free;
9328-
}
9311+
memset(env->idmap_scratch, 0, sizeof(env->idmap_scratch));
9312+
for (i = 0; i < MAX_BPF_REG; i++)
9313+
if (!regsafe(&old->regs[i], &cur->regs[i], env->idmap_scratch))
9314+
return false;
93299315

9330-
if (!stacksafe(old, cur, idmap))
9331-
goto out_free;
9316+
if (!stacksafe(old, cur, env->idmap_scratch))
9317+
return false;
93329318

93339319
if (!refsafe(old, cur))
9334-
goto out_free;
9335-
ret = true;
9336-
out_free:
9337-
kfree(idmap);
9338-
return ret;
9320+
return false;
9321+
9322+
return true;
93399323
}
93409324

93419325
static bool states_equal(struct bpf_verifier_env *env,
@@ -9362,7 +9346,7 @@ static bool states_equal(struct bpf_verifier_env *env,
93629346
for (i = 0; i <= old->curframe; i++) {
93639347
if (old->frame[i]->callsite != cur->frame[i]->callsite)
93649348
return false;
9365-
if (!func_states_equal(old->frame[i], cur->frame[i]))
9349+
if (!func_states_equal(env, old->frame[i], cur->frame[i]))
93669350
return false;
93679351
}
93689352
return true;

0 commit comments

Comments
 (0)