bpf: Add percpu map value size check
Subsystems (3)
v3 cover bpf: Add percpu map value size check
Check percpu map value size first and add the test case in selftest.
Change list:
- v2 -> v3:
- use bpf_map_create API and mv test case in map_percpu_stats.c
- v1 -> v2:
- round up map value size with 8 bytes in patch 1
- add selftest case in patch 2
Tao Chen (2):
bpf: Check percpu map value size first bpf/selftests: Check errno when percpu map value size exceeds
kernel/bpf/arraymap.c | 3 +++ kernel/bpf/hashtab.c | 3 +++ .../selftests/bpf/map_tests/map_percpu_stats.c | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+)
Signature · 1 line
2.43.0
PATCH v3 1/2 bpf: Check percpu map value size first
Percpu map is often used, but the map value size limit often ignored, like issue: https://github.com/iovisor/bcc/issues/2519. Actually, percpu map value size is bound by PCPU_MIN_UNIT_SIZE, so we can check the value size whether it exceeds PCPU_MIN_UNIT_SIZE first, like percpu map of local_storage. Maybe the error message seems clearer compared with "cannot allocate memory".
--- kernel/bpf/arraymap.c | 3 +++ kernel/bpf/hashtab.c | 3 +++ 2 files changed, 6 insertions(+)
kernel/bpf/arraymap.c +3 -0
@@ -73,6 +73,9 @@ int array_map_alloc_check(union bpf_attr *attr)/* avoid overflow on round_up(map->value_size) */ if (attr->value_size >INT_MAX )return -E2BIG ; +/* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */ +if (percpu &&round_up (attr->value_size,8 ) >PCPU_MIN_UNIT_SIZE ) +return -E2BIG ;return 0 ; }
kernel/bpf/hashtab.c +3 -0
@@ -462,6 +462,9 @@ static int htab_map_alloc_check(union bpf_attr *attr)* kmalloc-able later in htab_map_update_elem() */ return -E2BIG ; +/* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */ +if (percpu &&round_up (attr->value_size,8 ) >PCPU_MIN_UNIT_SIZE ) +return -E2BIG ;return 0 ; }
Signature · 1 line
2.43.0
PATCH v3 2/2 bpf/selftests: Check errno when percpu map value size exceeds
In reply to Tao ChenThis test case checks the errno message when percpu map value size exceeds PCPU_MIN_UNIT_SIZE.
root@debian:~# ./test_maps ... test_map_percpu_stats_hash_of_maps:PASS test_map_percpu_stats_map_value_size:PASS test_sk_storage_map:PASS
--- .../selftests/bpf/map_tests/map_percpu_stats.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
tools/testing/selftests/bpf/map_tests/map_percpu_stats.c +18 -0
@@ -17,6 +17,7 @@#define MAX_ENTRIES_HASH_OF_MAPS 64#define N_THREADS 8#define MAX_MAP_KEY_SIZE 4 +#define PCPU_MIN_UNIT_SIZE 32768static void map_info (int map_fd,struct bpf_map_info *info) {
@@ -456,6 +457,22 @@ static void map_percpu_stats_hash_of_maps(void)printf ("test_%s:PASS\n" , __func__); } +static void map_percpu_stats_map_value_size (void ) +{ +int fd; +int value_sz =PCPU_MIN_UNIT_SIZE +1 ; +struct bpf_map_create_opts opts = { .sz =sizeof (opts) }; +enum bpf_map_type map_types[] = {BPF_MAP_TYPE_PERCPU_ARRAY , +BPF_MAP_TYPE_PERCPU_HASH , +BPF_MAP_TYPE_LRU_PERCPU_HASH }; +for (int i =0 ; i <ARRAY_SIZE (map_types); i++) { + fd =bpf_map_create (map_types[i],NULL ,sizeof (__u32), value_sz,1 , &opts); +CHECK (fd <0 && errno !=E2BIG ,"percpu map value size" , +"error: %s\n" ,strerror (errno)); + } +printf ("test_%s:PASS\n" , __func__); +} +void test_map_percpu_stats (void ) {map_percpu_stats_hash ();
@@ -467,4 +484,5 @@ void test_map_percpu_stats(void)map_percpu_stats_percpu_lru_hash ();map_percpu_stats_percpu_lru_hash_no_common ();map_percpu_stats_hash_of_maps (); +map_percpu_stats_map_value_size (); }
Signature · 1 line
2.43.0
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 10 lines · On Tue, 10 Sep 2024 22:41:09 +0800 you wrote:
Check percpu map value size first and add the test case in selftest.
Change list:
- v2 -> v3:
- usebpf_map_createAPI and mv test case inmap_percpu_stats.c
- v1 -> v2:
- round up map value size with 8 bytes in patch 1
- add selftest case in patch 2[...]
Here is the summary with links:
- [v3,bpf-next,1/2] bpf: Check percpu map value size first
https://git.kernel.org/bpf/bpf-next/c/1d244784be6b - [v3,bpf-next,2/2] bpf/selftests: Check errno when percpu map value size exceeds https://git.kernel.org/bpf/bpf-next/c/7eab3a58ac7b
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html