bpf: optimize bpf_map_update_elem() and fix 'when full' behavior

In prealloc mode the bpf_map_update_elem() is doing
new = pcpu_freelist_pop();
insert new;
delete old;
pcpu_freelist_push(old);
and only when map is full 'extra_elems' percpu spare elements are used.
Optimize this logic by always using spare elements when updating existing element.

In kmalloc mode the bpf_map_update_elem() is also using 'extra_elems'
when map is full and it can be misused, since it allows
max_entries+num_cpus elements to be present in the map.
Switch this logic back to kmalloc even when full and old element exists
like it was prior to commit 6c9059817432 ("bpf: pre-allocate hash map elements")

Add a test to check for over max_entries condition.

Fixes: 6c9059817432 ("bpf: pre-allocate hash map elements")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 files changed