]> git.baikalelectronics.ru Git - kernel.git/commit
bpf: Fix excessive memory allocation in stack_map_alloc()
authorYuntao Wang <ytcoode@gmail.com>
Thu, 7 Apr 2022 13:04:23 +0000 (21:04 +0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Fri, 8 Apr 2022 22:28:21 +0000 (00:28 +0200)
commit1df09678569b3d73c3d8305248a6cbde4a5afcb2
treeeae4d0e258db5d566fafb7aed8e6cee5bfa27b57
parent07b39ebdd461755f67472b794526b010a9d09de5
bpf: Fix excessive memory allocation in stack_map_alloc()

The 'n_buckets * (value_size + sizeof(struct stack_map_bucket))' part of the
allocated memory for 'smap' is never used after the memlock accounting was
removed, thus get rid of it.

[ Note, Daniel:

Commit c318b041ffc0 ("bpf: rework memlock-based memory accounting for maps")
moved `cost += n_buckets * (value_size + sizeof(struct stack_map_bucket))`
up and therefore before the bpf_map_area_alloc() allocation, sigh. In a later
step commit e650ab9e53b9 ("bpf: move memory size checks to bpf_map_charge_init()"),
and the overflow checks of `cost >= U32_MAX - PAGE_SIZE` moved into
bpf_map_charge_init(). And then 57db40e54227 ("bpf: Eliminate rlimit-based
memory accounting for stackmap maps") finally removed the bpf_map_charge_init().
Anyway, the original code did the allocation same way as /after/ this fix. ]

Fixes: c318b041ffc0 ("bpf: rework memlock-based memory accounting for maps")
Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20220407130423.798386-1-ytcoode@gmail.com
kernel/bpf/stackmap.c