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