vaddr-test.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Data Access Monitor Unit Tests
  4. *
  5. * Copyright 2019 Amazon.com, Inc. or its affiliates. All rights reserved.
  6. *
  7. * Author: SeongJae Park <sjpark@amazon.de>
  8. */
  9. #ifdef CONFIG_DAMON_VADDR_KUNIT_TEST
  10. #ifndef _DAMON_VADDR_TEST_H
  11. #define _DAMON_VADDR_TEST_H
  12. #include <kunit/test.h>
  13. static void __link_vmas(struct vm_area_struct *vmas, ssize_t nr_vmas)
  14. {
  15. int i, j;
  16. unsigned long largest_gap, gap;
  17. if (!nr_vmas)
  18. return;
  19. for (i = 0; i < nr_vmas - 1; i++) {
  20. vmas[i].vm_next = &vmas[i + 1];
  21. vmas[i].vm_rb.rb_left = NULL;
  22. vmas[i].vm_rb.rb_right = &vmas[i + 1].vm_rb;
  23. largest_gap = 0;
  24. for (j = i; j < nr_vmas; j++) {
  25. if (j == 0)
  26. continue;
  27. gap = vmas[j].vm_start - vmas[j - 1].vm_end;
  28. if (gap > largest_gap)
  29. largest_gap = gap;
  30. }
  31. vmas[i].rb_subtree_gap = largest_gap;
  32. }
  33. vmas[i].vm_next = NULL;
  34. vmas[i].vm_rb.rb_right = NULL;
  35. vmas[i].rb_subtree_gap = 0;
  36. }
  37. /*
  38. * Test __damon_va_three_regions() function
  39. *
  40. * In case of virtual memory address spaces monitoring, DAMON converts the
  41. * complex and dynamic memory mappings of each target task to three
  42. * discontiguous regions which cover every mapped areas. However, the three
  43. * regions should not include the two biggest unmapped areas in the original
  44. * mapping, because the two biggest areas are normally the areas between 1)
  45. * heap and the mmap()-ed regions, and 2) the mmap()-ed regions and stack.
  46. * Because these two unmapped areas are very huge but obviously never accessed,
  47. * covering the region is just a waste.
  48. *
  49. * '__damon_va_three_regions() receives an address space of a process. It
  50. * first identifies the start of mappings, end of mappings, and the two biggest
  51. * unmapped areas. After that, based on the information, it constructs the
  52. * three regions and returns. For more detail, refer to the comment of
  53. * 'damon_init_regions_of()' function definition in 'mm/damon.c' file.
  54. *
  55. * For example, suppose virtual address ranges of 10-20, 20-25, 200-210,
  56. * 210-220, 300-305, and 307-330 (Other comments represent this mappings in
  57. * more short form: 10-20-25, 200-210-220, 300-305, 307-330) of a process are
  58. * mapped. To cover every mappings, the three regions should start with 10,
  59. * and end with 305. The process also has three unmapped areas, 25-200,
  60. * 220-300, and 305-307. Among those, 25-200 and 220-300 are the biggest two
  61. * unmapped areas, and thus it should be converted to three regions of 10-25,
  62. * 200-220, and 300-330.
  63. */
  64. static void damon_test_three_regions_in_vmas(struct kunit *test)
  65. {
  66. struct damon_addr_range regions[3] = {0,};
  67. /* 10-20-25, 200-210-220, 300-305, 307-330 */
  68. struct vm_area_struct vmas[] = {
  69. (struct vm_area_struct) {.vm_start = 10, .vm_end = 20},
  70. (struct vm_area_struct) {.vm_start = 20, .vm_end = 25},
  71. (struct vm_area_struct) {.vm_start = 200, .vm_end = 210},
  72. (struct vm_area_struct) {.vm_start = 210, .vm_end = 220},
  73. (struct vm_area_struct) {.vm_start = 300, .vm_end = 305},
  74. (struct vm_area_struct) {.vm_start = 307, .vm_end = 330},
  75. };
  76. __link_vmas(vmas, 6);
  77. __damon_va_three_regions(&vmas[0], regions);
  78. KUNIT_EXPECT_EQ(test, 10ul, regions[0].start);
  79. KUNIT_EXPECT_EQ(test, 25ul, regions[0].end);
  80. KUNIT_EXPECT_EQ(test, 200ul, regions[1].start);
  81. KUNIT_EXPECT_EQ(test, 220ul, regions[1].end);
  82. KUNIT_EXPECT_EQ(test, 300ul, regions[2].start);
  83. KUNIT_EXPECT_EQ(test, 330ul, regions[2].end);
  84. }
  85. static struct damon_region *__nth_region_of(struct damon_target *t, int idx)
  86. {
  87. struct damon_region *r;
  88. unsigned int i = 0;
  89. damon_for_each_region(r, t) {
  90. if (i++ == idx)
  91. return r;
  92. }
  93. return NULL;
  94. }
  95. /*
  96. * Test 'damon_va_apply_three_regions()'
  97. *
  98. * test kunit object
  99. * regions an array containing start/end addresses of current
  100. * monitoring target regions
  101. * nr_regions the number of the addresses in 'regions'
  102. * three_regions The three regions that need to be applied now
  103. * expected start/end addresses of monitoring target regions that
  104. * 'three_regions' are applied
  105. * nr_expected the number of addresses in 'expected'
  106. *
  107. * The memory mapping of the target processes changes dynamically. To follow
  108. * the change, DAMON periodically reads the mappings, simplifies it to the
  109. * three regions, and updates the monitoring target regions to fit in the three
  110. * regions. The update of current target regions is the role of
  111. * 'damon_va_apply_three_regions()'.
  112. *
  113. * This test passes the given target regions and the new three regions that
  114. * need to be applied to the function and check whether it updates the regions
  115. * as expected.
  116. */
  117. static void damon_do_test_apply_three_regions(struct kunit *test,
  118. unsigned long *regions, int nr_regions,
  119. struct damon_addr_range *three_regions,
  120. unsigned long *expected, int nr_expected)
  121. {
  122. struct damon_target *t;
  123. struct damon_region *r;
  124. int i;
  125. t = damon_new_target(42);
  126. for (i = 0; i < nr_regions / 2; i++) {
  127. r = damon_new_region(regions[i * 2], regions[i * 2 + 1]);
  128. damon_add_region(r, t);
  129. }
  130. damon_va_apply_three_regions(t, three_regions);
  131. for (i = 0; i < nr_expected / 2; i++) {
  132. r = __nth_region_of(t, i);
  133. KUNIT_EXPECT_EQ(test, r->ar.start, expected[i * 2]);
  134. KUNIT_EXPECT_EQ(test, r->ar.end, expected[i * 2 + 1]);
  135. }
  136. }
  137. /*
  138. * This function test most common case where the three big regions are only
  139. * slightly changed. Target regions should adjust their boundary (10-20-30,
  140. * 50-55, 70-80, 90-100) to fit with the new big regions or remove target
  141. * regions (57-79) that now out of the three regions.
  142. */
  143. static void damon_test_apply_three_regions1(struct kunit *test)
  144. {
  145. /* 10-20-30, 50-55-57-59, 70-80-90-100 */
  146. unsigned long regions[] = {10, 20, 20, 30, 50, 55, 55, 57, 57, 59,
  147. 70, 80, 80, 90, 90, 100};
  148. /* 5-27, 45-55, 73-104 */
  149. struct damon_addr_range new_three_regions[3] = {
  150. (struct damon_addr_range){.start = 5, .end = 27},
  151. (struct damon_addr_range){.start = 45, .end = 55},
  152. (struct damon_addr_range){.start = 73, .end = 104} };
  153. /* 5-20-27, 45-55, 73-80-90-104 */
  154. unsigned long expected[] = {5, 20, 20, 27, 45, 55,
  155. 73, 80, 80, 90, 90, 104};
  156. damon_do_test_apply_three_regions(test, regions, ARRAY_SIZE(regions),
  157. new_three_regions, expected, ARRAY_SIZE(expected));
  158. }
  159. /*
  160. * Test slightly bigger change. Similar to above, but the second big region
  161. * now require two target regions (50-55, 57-59) to be removed.
  162. */
  163. static void damon_test_apply_three_regions2(struct kunit *test)
  164. {
  165. /* 10-20-30, 50-55-57-59, 70-80-90-100 */
  166. unsigned long regions[] = {10, 20, 20, 30, 50, 55, 55, 57, 57, 59,
  167. 70, 80, 80, 90, 90, 100};
  168. /* 5-27, 56-57, 65-104 */
  169. struct damon_addr_range new_three_regions[3] = {
  170. (struct damon_addr_range){.start = 5, .end = 27},
  171. (struct damon_addr_range){.start = 56, .end = 57},
  172. (struct damon_addr_range){.start = 65, .end = 104} };
  173. /* 5-20-27, 56-57, 65-80-90-104 */
  174. unsigned long expected[] = {5, 20, 20, 27, 56, 57,
  175. 65, 80, 80, 90, 90, 104};
  176. damon_do_test_apply_three_regions(test, regions, ARRAY_SIZE(regions),
  177. new_three_regions, expected, ARRAY_SIZE(expected));
  178. }
  179. /*
  180. * Test a big change. The second big region has totally freed and mapped to
  181. * different area (50-59 -> 61-63). The target regions which were in the old
  182. * second big region (50-55-57-59) should be removed and new target region
  183. * covering the second big region (61-63) should be created.
  184. */
  185. static void damon_test_apply_three_regions3(struct kunit *test)
  186. {
  187. /* 10-20-30, 50-55-57-59, 70-80-90-100 */
  188. unsigned long regions[] = {10, 20, 20, 30, 50, 55, 55, 57, 57, 59,
  189. 70, 80, 80, 90, 90, 100};
  190. /* 5-27, 61-63, 65-104 */
  191. struct damon_addr_range new_three_regions[3] = {
  192. (struct damon_addr_range){.start = 5, .end = 27},
  193. (struct damon_addr_range){.start = 61, .end = 63},
  194. (struct damon_addr_range){.start = 65, .end = 104} };
  195. /* 5-20-27, 61-63, 65-80-90-104 */
  196. unsigned long expected[] = {5, 20, 20, 27, 61, 63,
  197. 65, 80, 80, 90, 90, 104};
  198. damon_do_test_apply_three_regions(test, regions, ARRAY_SIZE(regions),
  199. new_three_regions, expected, ARRAY_SIZE(expected));
  200. }
  201. /*
  202. * Test another big change. Both of the second and third big regions (50-59
  203. * and 70-100) has totally freed and mapped to different area (30-32 and
  204. * 65-68). The target regions which were in the old second and third big
  205. * regions should now be removed and new target regions covering the new second
  206. * and third big regions should be created.
  207. */
  208. static void damon_test_apply_three_regions4(struct kunit *test)
  209. {
  210. /* 10-20-30, 50-55-57-59, 70-80-90-100 */
  211. unsigned long regions[] = {10, 20, 20, 30, 50, 55, 55, 57, 57, 59,
  212. 70, 80, 80, 90, 90, 100};
  213. /* 5-7, 30-32, 65-68 */
  214. struct damon_addr_range new_three_regions[3] = {
  215. (struct damon_addr_range){.start = 5, .end = 7},
  216. (struct damon_addr_range){.start = 30, .end = 32},
  217. (struct damon_addr_range){.start = 65, .end = 68} };
  218. /* expect 5-7, 30-32, 65-68 */
  219. unsigned long expected[] = {5, 7, 30, 32, 65, 68};
  220. damon_do_test_apply_three_regions(test, regions, ARRAY_SIZE(regions),
  221. new_three_regions, expected, ARRAY_SIZE(expected));
  222. }
  223. static void damon_test_split_evenly_fail(struct kunit *test,
  224. unsigned long start, unsigned long end, unsigned int nr_pieces)
  225. {
  226. struct damon_target *t = damon_new_target(42);
  227. struct damon_region *r = damon_new_region(start, end);
  228. damon_add_region(r, t);
  229. KUNIT_EXPECT_EQ(test,
  230. damon_va_evenly_split_region(t, r, nr_pieces), -EINVAL);
  231. KUNIT_EXPECT_EQ(test, damon_nr_regions(t), 1u);
  232. damon_for_each_region(r, t) {
  233. KUNIT_EXPECT_EQ(test, r->ar.start, start);
  234. KUNIT_EXPECT_EQ(test, r->ar.end, end);
  235. }
  236. damon_free_target(t);
  237. }
  238. static void damon_test_split_evenly_succ(struct kunit *test,
  239. unsigned long start, unsigned long end, unsigned int nr_pieces)
  240. {
  241. struct damon_target *t = damon_new_target(42);
  242. struct damon_region *r = damon_new_region(start, end);
  243. unsigned long expected_width = (end - start) / nr_pieces;
  244. unsigned long i = 0;
  245. damon_add_region(r, t);
  246. KUNIT_EXPECT_EQ(test,
  247. damon_va_evenly_split_region(t, r, nr_pieces), 0);
  248. KUNIT_EXPECT_EQ(test, damon_nr_regions(t), nr_pieces);
  249. damon_for_each_region(r, t) {
  250. if (i == nr_pieces - 1)
  251. break;
  252. KUNIT_EXPECT_EQ(test,
  253. r->ar.start, start + i++ * expected_width);
  254. KUNIT_EXPECT_EQ(test, r->ar.end, start + i * expected_width);
  255. }
  256. KUNIT_EXPECT_EQ(test, r->ar.start, start + i * expected_width);
  257. KUNIT_EXPECT_EQ(test, r->ar.end, end);
  258. damon_free_target(t);
  259. }
  260. static void damon_test_split_evenly(struct kunit *test)
  261. {
  262. KUNIT_EXPECT_EQ(test, damon_va_evenly_split_region(NULL, NULL, 5),
  263. -EINVAL);
  264. damon_test_split_evenly_fail(test, 0, 100, 0);
  265. damon_test_split_evenly_succ(test, 0, 100, 10);
  266. damon_test_split_evenly_succ(test, 5, 59, 5);
  267. damon_test_split_evenly_fail(test, 5, 6, 2);
  268. }
  269. static struct kunit_case damon_test_cases[] = {
  270. KUNIT_CASE(damon_test_three_regions_in_vmas),
  271. KUNIT_CASE(damon_test_apply_three_regions1),
  272. KUNIT_CASE(damon_test_apply_three_regions2),
  273. KUNIT_CASE(damon_test_apply_three_regions3),
  274. KUNIT_CASE(damon_test_apply_three_regions4),
  275. KUNIT_CASE(damon_test_split_evenly),
  276. {},
  277. };
  278. static struct kunit_suite damon_test_suite = {
  279. .name = "damon-primitives",
  280. .test_cases = damon_test_cases,
  281. };
  282. kunit_test_suite(damon_test_suite);
  283. #endif /* _DAMON_VADDR_TEST_H */
  284. #endif /* CONFIG_DAMON_VADDR_KUNIT_TEST */