1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/kernel.h>
6 #include "map_groups.h"
16 static int check_maps(struct map_def
*merged
, unsigned int size
, struct map_groups
*mg
)
21 map
= map_groups__first(mg
);
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);
29 map
= map_groups__next(map
);
31 TEST_ASSERT_VAL("less maps expected", (map
&& i
< size
) || (!map
&& i
== size
));
37 int test__map_groups__merge_in(struct test
*t __maybe_unused
, int subtest __maybe_unused
)
41 struct map_def bpf_progs
[] = {
42 { "bpf_prog_1", 200, 300 },
43 { "bpf_prog_2", 500, 600 },
44 { "bpf_prog_3", 800, 900 },
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 },
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 },
65 struct map
*map_kcore1
, *map_kcore2
, *map_kcore3
;
68 map_groups__init(&mg
, NULL
);
70 for (i
= 0; i
< ARRAY_SIZE(bpf_progs
); i
++) {
73 map
= dso__new_map(bpf_progs
[i
].name
);
74 TEST_ASSERT_VAL("failed to create map", map
);
76 map
->start
= bpf_progs
[i
].start
;
77 map
->end
= bpf_progs
[i
].end
;
78 map_groups__insert(&mg
, map
);
82 map_kcore1
= dso__new_map("kcore1");
83 TEST_ASSERT_VAL("failed to create map", map_kcore1
);
85 map_kcore2
= dso__new_map("kcore2");
86 TEST_ASSERT_VAL("failed to create map", map_kcore2
);
88 map_kcore3
= dso__new_map("kcore3");
89 TEST_ASSERT_VAL("failed to create map", map_kcore3
);
91 /* kcore1 map overlaps over all bpf maps */
92 map_kcore1
->start
= 100;
93 map_kcore1
->end
= 1000;
95 /* kcore2 map hides behind bpf_prog_2 */
96 map_kcore2
->start
= 550;
97 map_kcore2
->end
= 570;
99 /* kcore3 map hides behind bpf_prog_3, kcore1 and adds new map */
100 map_kcore3
->start
= 880;
101 map_kcore3
->end
= 1100;
103 ret
= map_groups__merge_in(&mg
, map_kcore1
);
104 TEST_ASSERT_VAL("failed to merge map", !ret
);
106 ret
= check_maps(merged12
, ARRAY_SIZE(merged12
), &mg
);
107 TEST_ASSERT_VAL("merge check failed", !ret
);
109 ret
= map_groups__merge_in(&mg
, map_kcore2
);
110 TEST_ASSERT_VAL("failed to merge map", !ret
);
112 ret
= check_maps(merged12
, ARRAY_SIZE(merged12
), &mg
);
113 TEST_ASSERT_VAL("merge check failed", !ret
);
115 ret
= map_groups__merge_in(&mg
, map_kcore3
);
116 TEST_ASSERT_VAL("failed to merge map", !ret
);
118 ret
= check_maps(merged3
, ARRAY_SIZE(merged3
), &mg
);
119 TEST_ASSERT_VAL("merge check failed", !ret
);