* [v3 PATCH bpf-next 1/2] bpf: Check percpu map value size first
2024-09-10 14:41 [v3 PATCH bpf-next 0/2] bpf: Add percpu map value size check Tao Chen
@ 2024-09-10 14:41 ` Tao Chen
2024-09-10 14:41 ` [v3 PATCH bpf-next 2/2] bpf/selftests: Check errno when percpu map value size exceeds Tao Chen
2024-09-11 20:30 ` [v3 PATCH bpf-next 0/2] bpf: Add percpu map value size check patchwork-bot+netdevbpf
2 siblings, 0 replies; 4+ messages in thread
From: Tao Chen @ 2024-09-10 14:41 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Song Liu, Hou Tao, Yonghong Song,
John Fastabend, KP Singh, Stanislav Fomichev
Cc: Tao Chen, bpf, linux-kernel, Jinke Han, Jiri Olsa
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".
Signed-off-by: Tao Chen <chen.dylane@gmail.com>
Signed-off-by: Jinke Han <jinkehan@didiglobal.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
---
kernel/bpf/arraymap.c | 3 +++
kernel/bpf/hashtab.c | 3 +++
2 files changed, 6 insertions(+)
diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c
index a43e62e2a8bb..79660e3fca4c 100644
--- a/kernel/bpf/arraymap.c
+++ b/kernel/bpf/arraymap.c
@@ -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;
}
diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
index 45c7195b65ba..b14b87463ee0 100644
--- a/kernel/bpf/hashtab.c
+++ b/kernel/bpf/hashtab.c
@@ -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;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [v3 PATCH bpf-next 2/2] bpf/selftests: Check errno when percpu map value size exceeds
2024-09-10 14:41 [v3 PATCH bpf-next 0/2] bpf: Add percpu map value size check Tao Chen
2024-09-10 14:41 ` [v3 PATCH bpf-next 1/2] bpf: Check percpu map value size first Tao Chen
@ 2024-09-10 14:41 ` Tao Chen
2024-09-11 20:30 ` [v3 PATCH bpf-next 0/2] bpf: Add percpu map value size check patchwork-bot+netdevbpf
2 siblings, 0 replies; 4+ messages in thread
From: Tao Chen @ 2024-09-10 14:41 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Song Liu, Hou Tao, Yonghong Song,
John Fastabend, KP Singh, Stanislav Fomichev
Cc: Tao Chen, bpf, linux-kernel, Jinke Han
This 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
Signed-off-by: Tao Chen <chen.dylane@gmail.com>
Signed-off-by: Jinke Han <jinkehan@didiglobal.com>
---
.../selftests/bpf/map_tests/map_percpu_stats.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/tools/testing/selftests/bpf/map_tests/map_percpu_stats.c b/tools/testing/selftests/bpf/map_tests/map_percpu_stats.c
index 2ea36408816b..1c7c04288eff 100644
--- a/tools/testing/selftests/bpf/map_tests/map_percpu_stats.c
+++ b/tools/testing/selftests/bpf/map_tests/map_percpu_stats.c
@@ -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 32768
static 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();
}
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [v3 PATCH bpf-next 0/2] bpf: Add percpu map value size check
2024-09-10 14:41 [v3 PATCH bpf-next 0/2] bpf: Add percpu map value size check Tao Chen
2024-09-10 14:41 ` [v3 PATCH bpf-next 1/2] bpf: Check percpu map value size first Tao Chen
2024-09-10 14:41 ` [v3 PATCH bpf-next 2/2] bpf/selftests: Check errno when percpu map value size exceeds Tao Chen
@ 2024-09-11 20:30 ` patchwork-bot+netdevbpf
2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-09-11 20:30 UTC (permalink / raw)
To: Tao Chen
Cc: ast, daniel, andrii, eddyz87, song, houtao1, yonghong.song,
john.fastabend, kpsingh, sdf, bpf, linux-kernel
Hello:
This series was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:
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:
> - 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
>
> [...]
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!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 4+ messages in thread