vaddr-test.h 11 KB

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