diff mbox series

[bpf-next] bpf: Update max_entries for array maps

Message ID 20221025092843.81572-1-dev@der-flo.net (mailing list archive)
State Rejected
Delegated to: BPF
Headers show
Series [bpf-next] bpf: Update max_entries for array maps | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 5 this patch: 5
netdev/cc_maintainers success CCed 12 of 12 maintainers
netdev/build_clang success Errors and warnings before: 5 this patch: 5
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 5 this patch: 5
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 7 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-4 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-5 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-1 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-6 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on x86_64 with llvm-16

Commit Message

Florian Lehner Oct. 25, 2022, 9:28 a.m. UTC
To improve memory handling and alignment max_entries is rounded up
before using its value to allocate memory.
This can lead to a situation where more memory is allocated than usable
if max_entries is no adjusted accordingly. So this change updates
max_entries in order to make the allocated memory available.

Signed-off-by: Florian Lehner <dev@der-flo.net>
---
 kernel/bpf/arraymap.c | 1 +
 1 file changed, 1 insertion(+)

Comments

Hou Tao Oct. 26, 2022, 3:35 a.m. UTC | #1
Hi,

On 10/25/2022 5:28 PM, Florian Lehner wrote:
> To improve memory handling and alignment max_entries is rounded up
> before using its value to allocate memory.
> This can lead to a situation where more memory is allocated than usable
> if max_entries is no adjusted accordingly. So this change updates
> max_entries in order to make the allocated memory available.
>
> Signed-off-by: Florian Lehner <dev@der-flo.net>
> ---
>  kernel/bpf/arraymap.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c
> index 832b2659e96e..9411fa255ccc 100644
> --- a/kernel/bpf/arraymap.c
> +++ b/kernel/bpf/arraymap.c
> @@ -145,6 +145,7 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)
>  	/* copy mandatory map attributes */
>  	bpf_map_init_from_attr(&array->map, attr);
>  	array->elem_size = elem_size;
> +	array->map.max_entries = max_entries;
>  
>  	if (percpu && bpf_array_alloc_percpu(array)) {
>  		bpf_map_area_free(array);
The override of max_entries is unnecessary and is also wrong.
bpf_array_alloc_percpu() will use array->map.max_entries to allocate per-cpu
value, and if using the rounded-up max_entries, there will be memory waste
because the extra allocated per-cpu values should not be accessible to bpf
program or user-space program.
diff mbox series

Patch

diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c
index 832b2659e96e..9411fa255ccc 100644
--- a/kernel/bpf/arraymap.c
+++ b/kernel/bpf/arraymap.c
@@ -145,6 +145,7 @@  static struct bpf_map *array_map_alloc(union bpf_attr *attr)
 	/* copy mandatory map attributes */
 	bpf_map_init_from_attr(&array->map, attr);
 	array->elem_size = elem_size;
+	array->map.max_entries = max_entries;
 
 	if (percpu && bpf_array_alloc_percpu(array)) {
 		bpf_map_area_free(array);