Skip to contentSkip to conversation

bpf: reduce verifier memory consumption

PATCH Superseded Alexei Starovoitov 2 participants 3 messages Oct 30, 2017 – Nov 1, 2017

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: reduce verifier memory consumption

the verifier got progressively smarter over time and size of its internal state grew as well. Time to reduce the memory consumption.

Before:
sizeof(struct bpf_verifier_state) = 6520
After:
sizeof(struct bpf_verifier_state) = 896

It's done by observing that majority of BPF programs use little to no stack whereas verifier kept all of 512 stack slots ready always. Instead dynamically reallocate struct verifier state when stack access is detected.
Besides memory savings such approach gives few % runtime perf improvement as well and small reduction in number of processed insns:
before after

bpf_lb-DLB_L3.o        2285   2043
bpf_lb-DLB_L4.o        3723   3570
bpf_lb-DUNKNOWN.o      1110   1109
bpf_lxc-DDROP_ALL.o   27954  27849
bpf_lxc-DUNKNOWN.o    38954  38724
bpf_netdev.o          16943  16131

bpf_overlay.o 7929 7733

---
 drivers/net/ethernet/netronome/nfp/bpf/verifier.c |   8 +-
 include/linux/bpf_verifier.h                      |  18 +-
 kernel/bpf/verifier.c                             | 390 ++++++++++++++--------
 3 files changed, 268 insertions(+), 148 deletions(-)
drivers/net/ethernet/netronome/nfp/bpf/verifier.c +4 -4
index 3d3dcac1c942..a8c7615546a9 100644
@@ -76,9 +76,9 @@ nfp_bpf_goto_meta(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta,
 
 static int
 nfp_bpf_check_exit(struct nfp_prog *nfp_prog,
-		   const struct bpf_verifier_env *env)
+		   struct bpf_verifier_env *env)
 {
-	const struct bpf_reg_state *reg0 = &env->cur_state.regs[0];
+	const struct bpf_reg_state *reg0 = cur_regs(env) + BPF_REG_0;
 	u64 imm;
 
 	if (nfp_prog->act == NN_ACT_XDP)
@@ -144,9 +144,9 @@ nfp_bpf_check_stack_access(struct nfp_prog *nfp_prog,
 
 static int
 nfp_bpf_check_ptr(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta,
-		  const struct bpf_verifier_env *env, u8 reg_no)
+		  struct bpf_verifier_env *env, u8 reg_no)
 {
-	const struct bpf_reg_state *reg = &env->cur_state.regs[reg_no];
+	const struct bpf_reg_state *reg = cur_regs(env) + reg_no;
 	int err;
 
 	if (reg->type != PTR_TO_CTX &&
include/linux/bpf_verifier.h +14 -4
index feeaea93d959..c1430701742c 100644
@@ -88,20 +88,25 @@ enum bpf_stack_slot_type {
 
 #define BPF_REG_SIZE 8	/* size of eBPF register in bytes */
 
+struct bpf_verifier_stack {
+	struct bpf_reg_state spilled_ptr;
+	u8 slot_type[BPF_REG_SIZE];
+};
+
 /* state of the program:
  * type of all registers and stack info
  */
 struct bpf_verifier_state {
 	struct bpf_reg_state regs[MAX_BPF_REG];
-	u8 stack_slot_type[MAX_BPF_STACK];
-	struct bpf_reg_state spilled_regs[MAX_BPF_STACK / BPF_REG_SIZE];
 	struct bpf_verifier_state *parent;
+	int allocated_stack;
+	struct bpf_verifier_stack stack[0];
 };
 
 /* linked list of verifier states used to prune search */
 struct bpf_verifier_state_list {
-	struct bpf_verifier_state state;
 	struct bpf_verifier_state_list *next;
+	struct bpf_verifier_state state;
 };
 
 struct bpf_insn_aux_data {
@@ -145,7 +150,7 @@ struct bpf_verifier_env {
 	struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */
 	int stack_size;			/* number of states to be processed */
 	bool strict_alignment;		/* perform strict pointer alignment checks */
-	struct bpf_verifier_state cur_state; /* current verifier state */
+	struct bpf_verifier_state *cur_state; /* current verifier state */
 	struct bpf_verifier_state_list **explored_states; /* search pruning optimization */
 	const struct bpf_ext_analyzer_ops *analyzer_ops; /* external analyzer ops */
 	void *analyzer_priv; /* pointer to external analyzer's private data */
@@ -159,6 +164,11 @@ struct bpf_verifier_env {
 	struct bpf_verifer_log log;
 };
 
+static inline struct bpf_reg_state *cur_regs(struct bpf_verifier_env *env)
+{
+	return env->cur_state->regs;
+}
+
 int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
 		 void *priv);
 
kernel/bpf/verifier.c +250 -140

807 lines not on this page · raw message

Signature · 1 line
2.9.5
#1 lore raw
Quoted 7 lines · Alexei Starovoitov

the verifier got progressively smarter over time and size of its internal state grew as well. Time to reduce the memory consumption.

Before:
sizeof(struct bpf_verifier_state) = 6520
After:
sizeof(struct bpf_verifier_state) = 896

Nice!

Quoted 6 lines

It's done by observing that majority of BPF programs use little to no stack whereas verifier kept all of 512 stack slots ready always. Instead dynamically reallocate struct verifier state when stack access is detected.
Besides memory savings such approach gives few % runtime perf improvement as well and small reduction in number of processed insns:

                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This worries me. Not that improving pruning performance is a bad thing,
but I don't see anything in this patch that should affect it, which suggests maybe a bug has been introduced that prunes too much?

kernel/bpf/verifier.c static void print_verifier_state(struct bpf_verifier_env *env, · line 294

Quoted 7 lines
kernel/bpf/verifier.c +7 -0
+static void copy_verifier_state(struct bpf_verifier_state *dst,
+				const struct bpf_verifier_state *src)
+{
+	int stack = dst->allocated_stack;
+
+	if (stack < src->allocated_stack) {
+		WARN_ON_ONCE(stack < src->allocated_stack);

Why not just

    if (WARN_ON_ONCE(stack < src->allocated_stack)) {

?

kernel/bpf/verifier.c static void print_verifier_state(struct bpf_verifier_env *env, · line 297

Quoted 3 lines
kernel/bpf/verifier.c +3 -0
+		/* internal bug, make state invalid to reject the program */
+		memset(dst, 0, sizeof(*dst));
+		return;

Any reason this function couldn't return int instead?

kernel/bpf/verifier.c static void print_verifier_state(struct bpf_verifier_env *env, · line 302

Quoted 5 lines
kernel/bpf/verifier.c +5 -0
+	}
+	memcpy(dst, src, offsetof(struct bpf_verifier_state,
+				  stack[src->allocated_stack / BPF_REG_SIZE]));
+	dst->allocated_stack = stack;
+}

Then again, I'm not entirely sure I like the idea of an array[0] at the
end of the struct anyway (which is what makes this copy_verifier_state() necessary). I'd be happier with a *array member pointing to a separate allocation; though that would still need separate copying in is_state_visited(). I also worry about a stale *parent somewhere pointing to the old state; I don't _think_ that can happen, but I'm not as sure of that as I'd like to be. Using a *array instead would mean that &state doesn't change when we grow it.
Also, why kzalloc() a new state (in realloc_verifier_state()) rather than
krealloc()ing the existing one and memset()ing the extra space? That way you wouldn't need to then copy across all the old data.

Quoted 6 lines

static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx) {

kernel/bpf/verifier.c +2 -1
-	struct bpf_verifier_stack_elem *elem;
+	struct bpf_verifier_state *cur = env->cur_state;
+	struct bpf_verifier_stack_elem *elem, *head = env->head;
 	int insn_idx;

kernel/bpf/verifier.c static void print_verifier_state(struct bpf_verifier_env *env, · line 341

Quoted 10 lines
kernel/bpf/verifier.c +4 -2
 
 	if (env->head == NULL)
 		return -1;
 
-	memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state));
-	insn_idx = env->head->insn_idx;
+	if (cur && cur->allocated_stack < head->st.allocated_stack) {
+		cur = realloc_verifier_state(cur, head->st.allocated_stack);
+		if (!cur)
+			return -ENOMEM;

I don't think this does what you want. Calling code in do_check() will
get insn_idx = -ENOMEM, see that it's < 0, and break out of the loop. Prog will then be accepted without further checking.

-Ed

#2 lore raw
Quoted 16 lines · Edward Cree
Quoted 7 lines · Alexei Starovoitov

the verifier got progressively smarter over time and size of its internal state grew as well. Time to reduce the memory consumption.

Before:
sizeof(struct bpf_verifier_state) = 6520
After:
sizeof(struct bpf_verifier_state) = 896

Nice!

Quoted 6 lines

It's done by observing that majority of BPF programs use little to no stack whereas verifier kept all of 512 stack slots ready always. Instead dynamically reallocate struct verifier state when stack access is detected.
Besides memory savings such approach gives few % runtime perf improvement as well and small reduction in number of processed insns:

Quoted 4 lines
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This worries me. Not that improving pruning performance is a bad thing,
but I don't see anything in this patch that should affect it, which suggests maybe a bug has been introduced that prunes too much?

that indeed turned out to be a missed case when poping a state with smaller stack than the current one.

Quoted 10 lines

kernel/bpf/verifier.c static void print_verifier_state(struct bpf_verifier_env *env, · line 294

Quoted 7 lines
kernel/bpf/verifier.c +7 -0
+static void copy_verifier_state(struct bpf_verifier_state *dst,
+				const struct bpf_verifier_state *src)
+{
+	int stack = dst->allocated_stack;
+
+	if (stack < src->allocated_stack) {
+		WARN_ON_ONCE(stack < src->allocated_stack);

Why not just

    if (WARN_ON_ONCE(stack < src->allocated_stack)) {

?

done.

Quoted 4 lines

kernel/bpf/verifier.c static void print_verifier_state(struct bpf_verifier_env *env, · line 297

Quoted 3 lines
kernel/bpf/verifier.c +3 -0
+		/* internal bug, make state invalid to reject the program */
+		memset(dst, 0, sizeof(*dst));
+		return;

Any reason this function couldn't return int instead?

I didn't do it, since it adds additional error checks to the code that should never be taken, but I guess it makes the verifier a tiny bit more robust in case of fatal errors, so done.

Quoted 10 lines

kernel/bpf/verifier.c static void print_verifier_state(struct bpf_verifier_env *env, · line 302

Quoted 5 lines
kernel/bpf/verifier.c +5 -0
+	}
+	memcpy(dst, src, offsetof(struct bpf_verifier_state,
+				  stack[src->allocated_stack / BPF_REG_SIZE]));
+	dst->allocated_stack = stack;
+}

Then again, I'm not entirely sure I like the idea of an array[0] at the
end of the struct anyway (which is what makes this copy_verifier_state() necessary). I'd be happier with a *array member pointing to a separate allocation;

I coded it up. It turned out to be a bit more complex than current version, but the argument about keeping state pointer the same is solid, so done.

Quoted 2 lines

though that would still need separate copying in
is_state_visited().

yes

Quoted 2 lines

I also worry about a stale *parent somewhere
pointing to the old state; I don't _think_ that can happen, but I'm not

'parent' can point to wrong thing since liveness was introduced, so this doesn't change it. With stack[0] and *stack approach, both need to copy parent. Specifically for that I had a comment next to realloc_verifier_state() and kept the comment in the new version.

Quoted 5 lines

as sure of that as I'd like to be. Using a *array instead would mean that &state doesn't change when we grow it.
Also, why kzalloc() a new state (in realloc_verifier_state()) rather than
krealloc()ing the existing one and memset()ing the extra space? That way you wouldn't need to then copy across all the old data.

because krealloc() unconditionally copies the data whereas we can make it smarter by not copying always.

Quoted 11 lines
Quoted 11 lines

static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx) {

kernel/bpf/verifier.c +2 -1
-	struct bpf_verifier_stack_elem *elem;
+	struct bpf_verifier_state *cur = env->cur_state;
+	struct bpf_verifier_stack_elem *elem, *head = env->head;
 	int insn_idx;
 
 	if (env->head == NULL)
 		return -1;
Quoted 9 lines

kernel/bpf/verifier.c static void print_verifier_state(struct bpf_verifier_env *env, · line 341

Quoted 6 lines
kernel/bpf/verifier.c +4 -2
-	memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state));
-	insn_idx = env->head->insn_idx;
+	if (cur && cur->allocated_stack < head->st.allocated_stack) {
+		cur = realloc_verifier_state(cur, head->st.allocated_stack);
+		if (!cur)
+			return -ENOMEM;

I don't think this does what you want. Calling code in do_check() will
get insn_idx = -ENOMEM, see that it's < 0, and break out of the loop. Prog will then be accepted without further checking.

yeah. it was on my todo list to fix before submitting, but then slipped my mind. Fixed now.

v2 is coming.