bpf: use kvmalloc in map_lookup_elem
Subsystems (4)
PATCH 1/1 bpf: use kvmalloc in map_lookup_elem
Use kvmalloc/kvfree for temporary value when looking up a map. kmalloc might not be sufficient for percpu maps where the value is big.
Can be reproduced with netcnt test on qemu with "-smp 255".
--- kernel/bpf/syscall.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/syscall.c +2 -2
@@ -1076,7 +1076,7 @@ static int map_lookup_elem(union bpf_attr *attr) value_size =bpf_map_value_size (map); err = -ENOMEM ; - value =kmalloc (value_size,GFP_USER | __GFP_NOWARN); + value =kvmalloc (value_size,GFP_USER | __GFP_NOWARN);if (!value) goto free_key;
@@ -1091,7 +1091,7 @@ static int map_lookup_elem(union bpf_attr *attr) err =0 ; free_value: -kfree (value); +kvfree (value); free_key:kfree (key); err_put:
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
Daniel Borkmann
maintainerQuoted 15 lines · Stanislav Fomichev
Use kvmalloc/kvfree for temporary value when looking up a map. kmalloc might not be sufficient for percpu maps where the value is big.
Can be reproduced with netcnt test on qemu with "-smp 255".
Signed-off-by Stanislav Fomichev <[email protected]>--- kernel/bpf/syscall.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/bpf/syscall.c +0
-0
kernel/bpf/syscall.c static int map_lookup_elem(union bpf_attr *attr) · line 1081
Quoted 7 lines
kernel/bpf/syscall.c +1
-1@@ -1076,7 +1076,7 @@ static int map_lookup_elem(union bpf_attr *attr) value_size =bpf_map_value_size (map); err = -ENOMEM ; - value =kmalloc (value_size,GFP_USER | __GFP_NOWARN); + value =kvmalloc (value_size,GFP_USER | __GFP_NOWARN);if (!value) goto free_key;
What about other cases like map_update_elem(), shouldn't they be adapted similarly?
Thanks,
Daniel
Quoted 19 lines · Daniel Borkmann
Quoted 17 lines · Stanislav Fomichev
Use kvmalloc/kvfree for temporary value when looking up a map. kmalloc might not be sufficient for percpu maps where the value is big.
Can be reproduced with netcnt test on qemu with "-smp 255".
Signed-off-by Stanislav Fomichev <[email protected]>--- kernel/bpf/syscall.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/bpf/syscall.c +0
-0@@ -1076,7 +1076,7 @@ static int map_lookup_elem(union bpf_attr *attr) value_size =bpf_map_value_size (map);
Quoted 8 lines
kernel/bpf/syscall.c static int map_lookup_elem(union bpf_attr *attr) · line 1081
Quoted 5 lines
kernel/bpf/syscall.c +1
-1err = -ENOMEM ; - value =kmalloc (value_size,GFP_USER | __GFP_NOWARN); + value =kvmalloc (value_size,GFP_USER | __GFP_NOWARN);if (!value) goto free_key;What about other cases like
map_update_elem(), shouldn't they be adapted similarly?
And in the same vein (with keys potentially being big as well), should we switch __bpf_copy_key() to use vmemdup_user() instead of memdup_user()?
Quoted 3 lines
Thanks,
Daniel
Quoted 30 lines · Andrii Nakryiko
Quoted 4 lines · Daniel Borkmann
Quoted 2 lines · Stanislav Fomichev
Use kvmalloc/kvfree for temporary value when looking up a map. kmalloc might not be sufficient for percpu maps where the value is
big.
Quoted 15 lines
Quoted 15 lines
Can be reproduced with netcnt test on qemu with "-smp 255".
Signed-off-by Stanislav Fomichev <[email protected]>--- kernel/bpf/syscall.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/bpf/syscall.c +0
-0@@ -1076,7 +1076,7 @@ static int map_lookup_elem(union bpf_attr *attr) value_size =bpf_map_value_size (map);Quoted 8 lines
kernel/bpf/syscall.c static int map_lookup_elem(union bpf_attr *attr) · line 1081
Quoted 5 lines
kernel/bpf/syscall.c +1
-1err = -ENOMEM ; - value =kmalloc (value_size,GFP_USER | __GFP_NOWARN); + value =kvmalloc (value_size,GFP_USER | __GFP_NOWARN);if (!value) goto free_key;What about other cases like
map_update_elem(), shouldn't they be adapted similarly?
Quoted 3 lines
And in the same vein (with keys potentially being big as well), should we switch
__bpf_copy_key()to usevmemdup_user()instead ofmemdup_user()?
Those are good questions :-)
I'm assuming that whatever is doing kmalloc on top of bpf_map_value_size() should definitely use kvmalloc since bpf_map_value_size() can blow up the value size. (will resend)
For __bpf_copy_key I'm less sure, but it might be a good idea as well. Let me try to look at bit more into it, but feels like there shouldn't be any downsides?