maps.c 3.1 KB

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