1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/compiler.h>
4 #include <linux/kernel.h>
17 struct check_maps_cb_args
{
18 struct map_def
*merged
;
22 static int check_maps_cb(struct map
*map
, void *data
)
24 struct check_maps_cb_args
*args
= data
;
25 struct map_def
*merged
= &args
->merged
[args
->i
];
27 if (map__start(map
) != merged
->start
||
28 map__end(map
) != merged
->end
||
29 strcmp(dso__name(map__dso(map
)), merged
->name
) ||
30 refcount_read(map__refcnt(map
)) != 1) {
37 static int failed_cb(struct map
*map
, void *data __maybe_unused
)
39 pr_debug("\tstart: %" PRIu64
" end: %" PRIu64
" name: '%s' refcnt: %d\n",
42 dso__name(map__dso(map
)),
43 refcount_read(map__refcnt(map
)));
48 static int check_maps(struct map_def
*merged
, unsigned int size
, struct maps
*maps
)
52 if (maps__nr_maps(maps
) != size
) {
53 pr_debug("Expected %d maps, got %d", size
, maps__nr_maps(maps
));
56 struct check_maps_cb_args args
= {
60 failed
= maps__for_each_map(maps
, check_maps_cb
, &args
);
63 pr_debug("Expected:\n");
64 for (unsigned int i
= 0; i
< size
; i
++) {
65 pr_debug("\tstart: %" PRIu64
" end: %" PRIu64
" name: '%s' refcnt: 1\n",
66 merged
[i
].start
, merged
[i
].end
, merged
[i
].name
);
69 maps__for_each_map(maps
, failed_cb
, NULL
);
71 return failed
? TEST_FAIL
: TEST_OK
;
74 static int test__maps__merge_in(struct test_suite
*t __maybe_unused
, int subtest __maybe_unused
)
77 struct map_def bpf_progs
[] = {
78 { "bpf_prog_1", 200, 300 },
79 { "bpf_prog_2", 500, 600 },
80 { "bpf_prog_3", 800, 900 },
82 struct map_def merged12
[] = {
83 { "kcore1", 100, 200 },
84 { "bpf_prog_1", 200, 300 },
85 { "kcore1", 300, 500 },
86 { "bpf_prog_2", 500, 600 },
87 { "kcore1", 600, 800 },
88 { "bpf_prog_3", 800, 900 },
89 { "kcore1", 900, 1000 },
91 struct map_def merged3
[] = {
92 { "kcore1", 100, 200 },
93 { "bpf_prog_1", 200, 300 },
94 { "kcore1", 300, 500 },
95 { "bpf_prog_2", 500, 600 },
96 { "kcore1", 600, 800 },
97 { "bpf_prog_3", 800, 900 },
98 { "kcore1", 900, 1000 },
99 { "kcore3", 1000, 1100 },
101 struct map
*map_kcore1
, *map_kcore2
, *map_kcore3
;
103 struct maps
*maps
= maps__new(NULL
);
105 TEST_ASSERT_VAL("failed to create maps", maps
);
107 for (i
= 0; i
< ARRAY_SIZE(bpf_progs
); i
++) {
110 map
= dso__new_map(bpf_progs
[i
].name
);
111 TEST_ASSERT_VAL("failed to create map", map
);
113 map__set_start(map
, bpf_progs
[i
].start
);
114 map__set_end(map
, bpf_progs
[i
].end
);
115 TEST_ASSERT_VAL("failed to insert map", maps__insert(maps
, map
) == 0);
119 map_kcore1
= dso__new_map("kcore1");
120 TEST_ASSERT_VAL("failed to create map", map_kcore1
);
122 map_kcore2
= dso__new_map("kcore2");
123 TEST_ASSERT_VAL("failed to create map", map_kcore2
);
125 map_kcore3
= dso__new_map("kcore3");
126 TEST_ASSERT_VAL("failed to create map", map_kcore3
);
128 /* kcore1 map overlaps over all bpf maps */
129 map__set_start(map_kcore1
, 100);
130 map__set_end(map_kcore1
, 1000);
132 /* kcore2 map hides behind bpf_prog_2 */
133 map__set_start(map_kcore2
, 550);
134 map__set_end(map_kcore2
, 570);
136 /* kcore3 map hides behind bpf_prog_3, kcore1 and adds new map */
137 map__set_start(map_kcore3
, 880);
138 map__set_end(map_kcore3
, 1100);
140 ret
= maps__merge_in(maps
, map_kcore1
);
141 TEST_ASSERT_VAL("failed to merge map", !ret
);
143 ret
= check_maps(merged12
, ARRAY_SIZE(merged12
), maps
);
144 TEST_ASSERT_VAL("merge check failed", !ret
);
146 ret
= maps__merge_in(maps
, map_kcore2
);
147 TEST_ASSERT_VAL("failed to merge map", !ret
);
149 ret
= check_maps(merged12
, ARRAY_SIZE(merged12
), maps
);
150 TEST_ASSERT_VAL("merge check failed", !ret
);
152 ret
= maps__merge_in(maps
, map_kcore3
);
153 TEST_ASSERT_VAL("failed to merge map", !ret
);
155 ret
= check_maps(merged3
, ARRAY_SIZE(merged3
), maps
);
156 TEST_ASSERT_VAL("merge check failed", !ret
);
159 map__zput(map_kcore1
);
160 map__zput(map_kcore2
);
161 map__zput(map_kcore3
);
165 DEFINE_SUITE("maps__merge_in", maps__merge_in
);