vaddr-kunit.h 11 KB

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