bpf: verifier: Improve function state reallocation
authorLorenz Bauer <lmb@cloudflare.com>
Thu, 29 Apr 2021 13:46:54 +0000 (14:46 +0100)
committerAlexei Starovoitov <ast@kernel.org>
Mon, 10 May 2021 23:13:01 +0000 (16:13 -0700)
commitc69431aab67a912836e5831f03d99a819c14c9c3
treec7d1e311865974c2fe4555947ca51ca82ce7ebcc
parentb741596468b010af2846b75f5e75a842ce344a6e
bpf: verifier: Improve function state reallocation

Resizing and copying stack and reference tracking state currently
does a lot of kfree / kmalloc when the size of the tracked set changes.
The logic in copy_*_state and realloc_*_state is also hard to follow.

Refactor this into two core functions. copy_array copies from a source
into a destination. It avoids reallocation by taking the allocated
size of the destination into account via ksize(). The function is
essentially krealloc_array, with the difference that the contents of
dst are not preserved. realloc_array changes the size of an array and
zeroes newly allocated items. Contrary to krealloc both functions don't
free the destination if the size is zero. Instead we rely on free_func_state
to clean up.

realloc_stack_state is renamed to grow_stack_state to better convey
that it never shrinks the stack state.

Signed-off-by: Lorenz Bauer <lmb@cloudflare.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210429134656.122225-2-lmb@cloudflare.com
kernel/bpf/verifier.c