Skip to content

Commit

Permalink
bpf: Use kvmalloc for map values in syscall
Browse files Browse the repository at this point in the history
commit f0dce1d upstream

ANBZ:torvalds#198

[BackportNotes]
Modifications for bpf new interfaces(such as generic_map_update_batch,
generic_map_lookup_batch) is not applied, since they are not
implemented.

Use kvmalloc/kvfree for temporary value when manipulating a map via
syscall. kmalloc might not be sufficient for percpu maps where the value
is big (and further multiplied by hundreds of CPUs).

Can be reproduced with netcnt test on qemu with "-smp 255".

Signed-off-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Song Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20210818235216.1159202-1-sdf@google.com
Signed-off-by: Qiao Ma <mqaio@linux.alibaba.com>
Acked-by: Tony Lu <tonylu@linux.alibaba.com>
  • Loading branch information
fomichev authored and maqiao-mq committed Dec 24, 2021
1 parent 1532463 commit 77cb942
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions kernel/bpf/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ static int map_lookup_elem(union bpf_attr *attr)
value_size = map->value_size;

err = -ENOMEM;
value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
if (!value)
goto free_key;

Expand Down Expand Up @@ -847,7 +847,7 @@ static int map_lookup_elem(union bpf_attr *attr)
err = 0;

free_value:
kfree(value);
kvfree(value);
free_key:
kfree(key);
err_put:
Expand Down Expand Up @@ -906,7 +906,7 @@ static int map_update_elem(union bpf_attr *attr)
value_size = map->value_size;

err = -ENOMEM;
value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
if (!value)
goto free_key;

Expand Down Expand Up @@ -959,7 +959,7 @@ static int map_update_elem(union bpf_attr *attr)
maybe_wait_bpf_programs(map);
out:
free_value:
kfree(value);
kvfree(value);
free_key:
kfree(key);
err_put:
Expand Down

0 comments on commit 77cb942

Please sign in to comment.