Skip to contentSkip to conversation

bpf: save extended inner map info for percpu array maps as well

PATCH Applied Andrii Nakryiko 4 participants 6 messages May 15, 2024

Subsystems (5)
lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

PATCH 1/2 bpf: save extended inner map info for percpu array maps as well

ARRAY_OF_MAPS and HASH_OF_MAPS map types have special logic to save a few extra fields required for correct operations of ARRAY maps, when they are used as inner maps. PERCPU_ARRAY maps have similar requirements as they now support generating inline element lookup logic. So make sure that both classes of maps are handled correctly.

Fixes db69718b8efa ("bpf: inline bpf_map_lookup_elem() for PERCPU_ARRAY maps")
---
 kernel/bpf/map_in_map.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/map_in_map.c +2 -2
index 8ef269e66ba5..b4f18c85d7bc 100644
@@ -32,7 +32,7 @@ struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
 
 	inner_map_meta_size = sizeof(*inner_map_meta);
 	/* In some cases verifier needs to access beyond just base map. */
-	if (inner_map->ops == &array_map_ops)
+	if (inner_map->ops == &array_map_ops || inner_map->ops == &percpu_array_map_ops)
 		inner_map_meta_size = sizeof(struct bpf_array);
 
 	inner_map_meta = kzalloc(inner_map_meta_size, GFP_USER);
@@ -68,7 +68,7 @@ struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
 
 	/* Misc members not needed in bpf_map_meta_equal() check. */
 	inner_map_meta->ops = inner_map->ops;
-	if (inner_map->ops == &array_map_ops) {
+	if (inner_map->ops == &array_map_ops || inner_map->ops == &percpu_array_map_ops) {
 		struct bpf_array *inner_array_meta =
 			container_of(inner_map_meta, struct bpf_array, map);
 		struct bpf_array *inner_array = container_of(inner_map, struct bpf_array, map);
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH 2/2 selftests/bpf: add more variations of map-in-map situations

Add test cases validating usage of PERCPU_ARRAY and PERCPU_HASH maps as inner maps.

---
 tools/testing/selftests/bpf/progs/map_kptr.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/testing/selftests/bpf/progs/map_kptr.c +10 -0
index da30f0d59364..ab0ce1d01a4a 100644
@@ -110,10 +110,14 @@ DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_ARRAY_OF_MAPS, array_map, array_of_array_maps);
 DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_ARRAY_OF_MAPS, hash_map, array_of_hash_maps);
 DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_ARRAY_OF_MAPS, hash_malloc_map, array_of_hash_malloc_maps);
 DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_ARRAY_OF_MAPS, lru_hash_map, array_of_lru_hash_maps);
+DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_ARRAY_OF_MAPS, pcpu_array_map, array_of_pcpu_array_maps);
+DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_ARRAY_OF_MAPS, pcpu_hash_map, array_of_pcpu_hash_maps);
 DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_HASH_OF_MAPS, array_map, hash_of_array_maps);
 DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_HASH_OF_MAPS, hash_map, hash_of_hash_maps);
 DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_HASH_OF_MAPS, hash_malloc_map, hash_of_hash_malloc_maps);
 DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_HASH_OF_MAPS, lru_hash_map, hash_of_lru_hash_maps);
+DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_HASH_OF_MAPS, pcpu_array_map, hash_of_pcpu_array_maps);
+DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_HASH_OF_MAPS, pcpu_hash_map, hash_of_pcpu_hash_maps);
 
 #define WRITE_ONCE(x, val) ((*(volatile typeof(x) *) &(x)) = (val))
 
@@ -204,6 +208,8 @@ int test_map_kptr(struct __sk_buff *ctx)
 	TEST(hash_map);
 	TEST(hash_malloc_map);
 	TEST(lru_hash_map);
+	TEST(pcpu_array_map);
+	TEST(pcpu_hash_map);
 
 #undef TEST
 	return 0;
@@ -281,10 +287,14 @@ int test_map_in_map_kptr(struct __sk_buff *ctx)
 	TEST(array_of_hash_maps);
 	TEST(array_of_hash_malloc_maps);
 	TEST(array_of_lru_hash_maps);
+	TEST(array_of_pcpu_array_maps);
+	TEST(array_of_pcpu_hash_maps);
 	TEST(hash_of_array_maps);
 	TEST(hash_of_hash_maps);
 	TEST(hash_of_hash_malloc_maps);
 	TEST(hash_of_lru_hash_maps);
+	TEST(hash_of_pcpu_array_maps);
+	TEST(hash_of_pcpu_hash_maps);
 
 #undef TEST
 	return 0;
Signature · 1 line
2.43.0
maintainer Acked-by on v1 1/2
#3 lore raw
In reply to Andrii Nakryiko
Quoted 11 lines · Andrii Nakryiko

ARRAY_OF_MAPS and HASH_OF_MAPS map types have special logic to save a few extra fields required for correct operations of ARRAY maps, when they are used as inner maps. PERCPU_ARRAY maps have similar requirements as they now support generating inline element lookup logic. So make sure that both classes of maps are handled correctly.

Fixes db69718b8efa ("bpf: inline bpf_map_lookup_elem() for PERCPU_ARRAY maps")

---

#4 lore raw
In reply to Andrii Nakryiko
Quoted 19 lines · Andrii Nakryiko

ARRAY_OF_MAPS and HASH_OF_MAPS map types have special logic to save a few extra fields required for correct operations of ARRAY maps, when they are used as inner maps. PERCPU_ARRAY maps have similar requirements as they now support generating inline element lookup logic. So make sure that both classes of maps are handled correctly.

Fixes db69718b8efa ("bpf: inline bpf_map_lookup_elem() for PERCPU_ARRAY maps")
---
 kernel/bpf/map_in_map.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/map_in_map.c +0 -0
index 8ef269e66ba5..b4f18c85d7bc 100644

kernel/bpf/map_in_map.c struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd) · line 36

Quoted 6 lines
kernel/bpf/map_in_map.c +1 -1
@@ -32,7 +32,7 @@ struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
 
        inner_map_meta_size = sizeof(*inner_map_meta);
        /* In some cases verifier needs to access beyond just base map. */
-       if (inner_map->ops == &array_map_ops)
+       if (inner_map->ops == &array_map_ops || inner_map->ops == &percpu_array_map_ops)
                inner_map_meta_size = sizeof(struct bpf_array);

Applied the fix to stop the bleeding,
but we need to fix this fragility long term.

#5 lore raw
In reply to Andrii Nakryiko
This series was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:

Quoted 11 lines · On Tue, 14 May 2024 23:24:39 -0700 you wrote:

ARRAY_OF_MAPS and HASH_OF_MAPS map types have special logic to save a few extra fields required for correct operations of ARRAY maps, when they are used as inner maps. PERCPU_ARRAY maps have similar requirements as they now support generating inline element lookup logic. So make sure that both classes of maps are handled correctly.

Fixes db69718b8efa ("bpf: inline bpf_map_lookup_elem() for PERCPU_ARRAY maps")

[...]

Here is the summary with links:
- [net-next,1/2] bpf: save extended inner map info for percpu array maps as well

    https://git.kernel.org/bpf/bpf/c/9ee982290831
  - [net-next,2/2] selftests/bpf: add more variations of map-in-map situations
    https://git.kernel.org/bpf/bpf/c/2322113ac9d0

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html