^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include "tests.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include "map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include "maps.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "dso.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) struct map_def {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) u64 end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static int check_maps(struct map_def *merged, unsigned int size, struct maps *maps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct map *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) unsigned int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) maps__for_each_entry(maps, map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) if (i > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) TEST_ASSERT_VAL("less maps expected", (map && i < size) || (!map && i == size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) TEST_ASSERT_VAL("wrong map start", map->start == merged[i].start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) TEST_ASSERT_VAL("wrong map end", map->end == merged[i].end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) TEST_ASSERT_VAL("wrong map name", !strcmp(map->dso->name, merged[i].name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) TEST_ASSERT_VAL("wrong map refcnt", refcount_read(&map->refcnt) == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return TEST_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) int test__maps__merge_in(struct test *t __maybe_unused, int subtest __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct maps maps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct map_def bpf_progs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) { "bpf_prog_1", 200, 300 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) { "bpf_prog_2", 500, 600 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) { "bpf_prog_3", 800, 900 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct map_def merged12[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) { "kcore1", 100, 200 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) { "bpf_prog_1", 200, 300 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) { "kcore1", 300, 500 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) { "bpf_prog_2", 500, 600 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) { "kcore1", 600, 800 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) { "bpf_prog_3", 800, 900 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) { "kcore1", 900, 1000 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct map_def merged3[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) { "kcore1", 100, 200 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) { "bpf_prog_1", 200, 300 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) { "kcore1", 300, 500 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) { "bpf_prog_2", 500, 600 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) { "kcore1", 600, 800 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) { "bpf_prog_3", 800, 900 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) { "kcore1", 900, 1000 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) { "kcore3", 1000, 1100 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct map *map_kcore1, *map_kcore2, *map_kcore3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) maps__init(&maps, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) for (i = 0; i < ARRAY_SIZE(bpf_progs); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct map *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) map = dso__new_map(bpf_progs[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) TEST_ASSERT_VAL("failed to create map", map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) map->start = bpf_progs[i].start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) map->end = bpf_progs[i].end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) maps__insert(&maps, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) map__put(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) map_kcore1 = dso__new_map("kcore1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) TEST_ASSERT_VAL("failed to create map", map_kcore1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) map_kcore2 = dso__new_map("kcore2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) TEST_ASSERT_VAL("failed to create map", map_kcore2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) map_kcore3 = dso__new_map("kcore3");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) TEST_ASSERT_VAL("failed to create map", map_kcore3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* kcore1 map overlaps over all bpf maps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) map_kcore1->start = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) map_kcore1->end = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* kcore2 map hides behind bpf_prog_2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) map_kcore2->start = 550;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) map_kcore2->end = 570;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* kcore3 map hides behind bpf_prog_3, kcore1 and adds new map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) map_kcore3->start = 880;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) map_kcore3->end = 1100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) ret = maps__merge_in(&maps, map_kcore1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) TEST_ASSERT_VAL("failed to merge map", !ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ret = check_maps(merged12, ARRAY_SIZE(merged12), &maps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) TEST_ASSERT_VAL("merge check failed", !ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) ret = maps__merge_in(&maps, map_kcore2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) TEST_ASSERT_VAL("failed to merge map", !ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) ret = check_maps(merged12, ARRAY_SIZE(merged12), &maps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) TEST_ASSERT_VAL("merge check failed", !ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) ret = maps__merge_in(&maps, map_kcore3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) TEST_ASSERT_VAL("failed to merge map", !ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ret = check_maps(merged3, ARRAY_SIZE(merged3), &maps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) TEST_ASSERT_VAL("merge check failed", !ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) maps__exit(&maps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return TEST_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }