maps.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <inttypes.h>
  3. #include <linux/compiler.h>
  4. #include <linux/kernel.h>
  5. #include "tests.h"
  6. #include "map.h"
  7. #include "maps.h"
  8. #include "dso.h"
  9. #include "debug.h"
  10. struct map_def {
  11. const char *name;
  12. u64 start;
  13. u64 end;
  14. };
  15. struct check_maps_cb_args {
  16. struct map_def *merged;
  17. unsigned int i;
  18. };
  19. static int check_maps_cb(struct map *map, void *data)
  20. {
  21. struct check_maps_cb_args *args = data;
  22. struct map_def *merged = &args->merged[args->i];
  23. if (map__start(map) != merged->start ||
  24. map__end(map) != merged->end ||
  25. strcmp(dso__name(map__dso(map)), merged->name) ||
  26. refcount_read(map__refcnt(map)) != 1) {
  27. return 1;
  28. }
  29. args->i++;
  30. return 0;
  31. }
  32. static int failed_cb(struct map *map, void *data __maybe_unused)
  33. {
  34. pr_debug("\tstart: %" PRIu64 " end: %" PRIu64 " name: '%s' refcnt: %d\n",
  35. map__start(map),
  36. map__end(map),
  37. dso__name(map__dso(map)),
  38. refcount_read(map__refcnt(map)));
  39. return 0;
  40. }
  41. static int check_maps(struct map_def *merged, unsigned int size, struct maps *maps)
  42. {
  43. bool failed = false;
  44. if (maps__nr_maps(maps) != size) {
  45. pr_debug("Expected %d maps, got %d", size, maps__nr_maps(maps));
  46. failed = true;
  47. } else {
  48. struct check_maps_cb_args args = {
  49. .merged = merged,
  50. .i = 0,
  51. };
  52. failed = maps__for_each_map(maps, check_maps_cb, &args);
  53. }
  54. if (failed) {
  55. pr_debug("Expected:\n");
  56. for (unsigned int i = 0; i < size; i++) {
  57. pr_debug("\tstart: %" PRIu64 " end: %" PRIu64 " name: '%s' refcnt: 1\n",
  58. merged[i].start, merged[i].end, merged[i].name);
  59. }
  60. pr_debug("Got:\n");
  61. maps__for_each_map(maps, failed_cb, NULL);
  62. }
  63. return failed ? TEST_FAIL : TEST_OK;
  64. }
  65. static int test__maps__merge_in(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
  66. {
  67. unsigned int i;
  68. struct map_def bpf_progs[] = {
  69. { "bpf_prog_1", 200, 300 },
  70. { "bpf_prog_2", 500, 600 },
  71. { "bpf_prog_3", 800, 900 },
  72. };
  73. struct map_def merged12[] = {
  74. { "kcore1", 100, 200 },
  75. { "bpf_prog_1", 200, 300 },
  76. { "kcore1", 300, 500 },
  77. { "bpf_prog_2", 500, 600 },
  78. { "kcore1", 600, 800 },
  79. { "bpf_prog_3", 800, 900 },
  80. { "kcore1", 900, 1000 },
  81. };
  82. struct map_def merged3[] = {
  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 },
  90. { "kcore3", 1000, 1100 },
  91. };
  92. struct map *map_kcore1, *map_kcore2, *map_kcore3;
  93. int ret;
  94. struct maps *maps = maps__new(NULL);
  95. TEST_ASSERT_VAL("failed to create maps", maps);
  96. for (i = 0; i < ARRAY_SIZE(bpf_progs); i++) {
  97. struct map *map;
  98. map = dso__new_map(bpf_progs[i].name);
  99. TEST_ASSERT_VAL("failed to create map", map);
  100. map__set_start(map, bpf_progs[i].start);
  101. map__set_end(map, bpf_progs[i].end);
  102. TEST_ASSERT_VAL("failed to insert map", maps__insert(maps, map) == 0);
  103. map__put(map);
  104. }
  105. map_kcore1 = dso__new_map("kcore1");
  106. TEST_ASSERT_VAL("failed to create map", map_kcore1);
  107. map_kcore2 = dso__new_map("kcore2");
  108. TEST_ASSERT_VAL("failed to create map", map_kcore2);
  109. map_kcore3 = dso__new_map("kcore3");
  110. TEST_ASSERT_VAL("failed to create map", map_kcore3);
  111. /* kcore1 map overlaps over all bpf maps */
  112. map__set_start(map_kcore1, 100);
  113. map__set_end(map_kcore1, 1000);
  114. /* kcore2 map hides behind bpf_prog_2 */
  115. map__set_start(map_kcore2, 550);
  116. map__set_end(map_kcore2, 570);
  117. /* kcore3 map hides behind bpf_prog_3, kcore1 and adds new map */
  118. map__set_start(map_kcore3, 880);
  119. map__set_end(map_kcore3, 1100);
  120. ret = maps__merge_in(maps, map_kcore1);
  121. TEST_ASSERT_VAL("failed to merge map", !ret);
  122. ret = check_maps(merged12, ARRAY_SIZE(merged12), maps);
  123. TEST_ASSERT_VAL("merge check failed", !ret);
  124. ret = maps__merge_in(maps, map_kcore2);
  125. TEST_ASSERT_VAL("failed to merge map", !ret);
  126. ret = check_maps(merged12, ARRAY_SIZE(merged12), maps);
  127. TEST_ASSERT_VAL("merge check failed", !ret);
  128. ret = maps__merge_in(maps, map_kcore3);
  129. TEST_ASSERT_VAL("failed to merge map", !ret);
  130. ret = check_maps(merged3, ARRAY_SIZE(merged3), maps);
  131. TEST_ASSERT_VAL("merge check failed", !ret);
  132. maps__zput(maps);
  133. map__zput(map_kcore1);
  134. map__zput(map_kcore2);
  135. map__zput(map_kcore3);
  136. return TEST_OK;
  137. }
  138. static int test__maps__fixup_overlap_and_insert(struct test_suite *t __maybe_unused,
  139. int subtest __maybe_unused)
  140. {
  141. struct map_def initial_maps[] = {
  142. { "target_map", 1000, 2000 },
  143. { "next_map", 3000, 4000 },
  144. };
  145. struct map_def insert_split = { "split_map", 1400, 1600 };
  146. struct map_def expected_after_split[] = {
  147. { "target_map", 1000, 1400 },
  148. { "split_map", 1400, 1600 },
  149. { "target_map", 1600, 2000 },
  150. { "next_map", 3000, 4000 },
  151. };
  152. struct map_def insert_eclipse = { "eclipse_map", 2500, 4500 };
  153. struct map_def expected_final[] = {
  154. { "target_map", 1000, 1400 },
  155. { "split_map", 1400, 1600 },
  156. { "target_map", 1600, 2000 },
  157. { "eclipse_map", 2500, 4500 },
  158. /* "next_map" (3000-4000) is removed */
  159. };
  160. struct map *map_split, *map_eclipse;
  161. int ret;
  162. unsigned int i;
  163. struct maps *maps = maps__new(NULL);
  164. TEST_ASSERT_VAL("failed to create maps", maps);
  165. for (i = 0; i < ARRAY_SIZE(initial_maps); i++) {
  166. struct map *map = dso__new_map(initial_maps[i].name);
  167. TEST_ASSERT_VAL("failed to create map", map);
  168. map__set_start(map, initial_maps[i].start);
  169. map__set_end(map, initial_maps[i].end);
  170. TEST_ASSERT_VAL("failed to insert map", maps__insert(maps, map) == 0);
  171. map__put(map);
  172. }
  173. // Check splitting.
  174. map_split = dso__new_map(insert_split.name);
  175. TEST_ASSERT_VAL("failed to create split map", map_split);
  176. map__set_start(map_split, insert_split.start);
  177. map__set_end(map_split, insert_split.end);
  178. ret = maps__fixup_overlap_and_insert(maps, map_split);
  179. TEST_ASSERT_VAL("failed to fixup and insert split map", !ret);
  180. map__zput(map_split);
  181. ret = check_maps(expected_after_split, ARRAY_SIZE(expected_after_split), maps);
  182. TEST_ASSERT_VAL("split check failed", !ret);
  183. // Check cover 1 map with another.
  184. map_eclipse = dso__new_map(insert_eclipse.name);
  185. TEST_ASSERT_VAL("failed to create eclipse map", map_eclipse);
  186. map__set_start(map_eclipse, insert_eclipse.start);
  187. map__set_end(map_eclipse, insert_eclipse.end);
  188. ret = maps__fixup_overlap_and_insert(maps, map_eclipse);
  189. TEST_ASSERT_VAL("failed to fixup and insert eclipse map", !ret);
  190. map__zput(map_eclipse);
  191. ret = check_maps(expected_final, ARRAY_SIZE(expected_final), maps);
  192. TEST_ASSERT_VAL("eclipse check failed", !ret);
  193. maps__zput(maps);
  194. return TEST_OK;
  195. }
  196. static struct test_case tests__maps[] = {
  197. TEST_CASE("Test merge_in interface", maps__merge_in),
  198. TEST_CASE("Test fix up overlap interface", maps__fixup_overlap_and_insert),
  199. { .name = NULL, }
  200. };
  201. struct test_suite suite__maps = {
  202. .desc = "Maps - per process mmaps abstraction",
  203. .test_cases = tests__maps,
  204. };