vaddr-kunit.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. static 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. if (!t)
  124. kunit_skip(test, "target alloc fail");
  125. for (i = 0; i < nr_regions / 2; i++) {
  126. r = damon_new_region(regions[i * 2], regions[i * 2 + 1]);
  127. if (!r) {
  128. damon_destroy_target(t, NULL);
  129. kunit_skip(test, "region alloc fail");
  130. }
  131. damon_add_region(r, t);
  132. }
  133. damon_set_regions(t, three_regions, 3, DAMON_MIN_REGION_SZ);
  134. for (i = 0; i < nr_expected / 2; i++) {
  135. r = __nth_region_of(t, i);
  136. KUNIT_EXPECT_EQ(test, r->ar.start, expected[i * 2]);
  137. KUNIT_EXPECT_EQ(test, r->ar.end, expected[i * 2 + 1]);
  138. }
  139. damon_destroy_target(t, NULL);
  140. }
  141. /*
  142. * This function test most common case where the three big regions are only
  143. * slightly changed. Target regions should adjust their boundary (10-20-30,
  144. * 50-55, 70-80, 90-100) to fit with the new big regions or remove target
  145. * regions (57-79) that now out of the three regions.
  146. */
  147. static void damon_test_apply_three_regions1(struct kunit *test)
  148. {
  149. /* 10-20-30, 50-55-57-59, 70-80-90-100 */
  150. unsigned long regions[] = {10, 20, 20, 30, 50, 55, 55, 57, 57, 59,
  151. 70, 80, 80, 90, 90, 100};
  152. /* 5-27, 45-55, 73-104 */
  153. struct damon_addr_range new_three_regions[3] = {
  154. (struct damon_addr_range){.start = 5, .end = 27},
  155. (struct damon_addr_range){.start = 45, .end = 55},
  156. (struct damon_addr_range){.start = 73, .end = 104} };
  157. /* 5-20-27, 45-55, 73-80-90-104 */
  158. unsigned long expected[] = {5, 20, 20, 27, 45, 55,
  159. 73, 80, 80, 90, 90, 104};
  160. damon_do_test_apply_three_regions(test, regions, ARRAY_SIZE(regions),
  161. new_three_regions, expected, ARRAY_SIZE(expected));
  162. }
  163. /*
  164. * Test slightly bigger change. Similar to above, but the second big region
  165. * now require two target regions (50-55, 57-59) to be removed.
  166. */
  167. static void damon_test_apply_three_regions2(struct kunit *test)
  168. {
  169. /* 10-20-30, 50-55-57-59, 70-80-90-100 */
  170. unsigned long regions[] = {10, 20, 20, 30, 50, 55, 55, 57, 57, 59,
  171. 70, 80, 80, 90, 90, 100};
  172. /* 5-27, 56-57, 65-104 */
  173. struct damon_addr_range new_three_regions[3] = {
  174. (struct damon_addr_range){.start = 5, .end = 27},
  175. (struct damon_addr_range){.start = 56, .end = 57},
  176. (struct damon_addr_range){.start = 65, .end = 104} };
  177. /* 5-20-27, 56-57, 65-80-90-104 */
  178. unsigned long expected[] = {5, 20, 20, 27, 56, 57,
  179. 65, 80, 80, 90, 90, 104};
  180. damon_do_test_apply_three_regions(test, regions, ARRAY_SIZE(regions),
  181. new_three_regions, expected, ARRAY_SIZE(expected));
  182. }
  183. /*
  184. * Test a big change. The second big region has totally freed and mapped to
  185. * different area (50-59 -> 61-63). The target regions which were in the old
  186. * second big region (50-55-57-59) should be removed and new target region
  187. * covering the second big region (61-63) should be created.
  188. */
  189. static void damon_test_apply_three_regions3(struct kunit *test)
  190. {
  191. /* 10-20-30, 50-55-57-59, 70-80-90-100 */
  192. unsigned long regions[] = {10, 20, 20, 30, 50, 55, 55, 57, 57, 59,
  193. 70, 80, 80, 90, 90, 100};
  194. /* 5-27, 61-63, 65-104 */
  195. struct damon_addr_range new_three_regions[3] = {
  196. (struct damon_addr_range){.start = 5, .end = 27},
  197. (struct damon_addr_range){.start = 61, .end = 63},
  198. (struct damon_addr_range){.start = 65, .end = 104} };
  199. /* 5-20-27, 61-63, 65-80-90-104 */
  200. unsigned long expected[] = {5, 20, 20, 27, 61, 63,
  201. 65, 80, 80, 90, 90, 104};
  202. damon_do_test_apply_three_regions(test, regions, ARRAY_SIZE(regions),
  203. new_three_regions, expected, ARRAY_SIZE(expected));
  204. }
  205. /*
  206. * Test another big change. Both of the second and third big regions (50-59
  207. * and 70-100) has totally freed and mapped to different area (30-32 and
  208. * 65-68). The target regions which were in the old second and third big
  209. * regions should now be removed and new target regions covering the new second
  210. * and third big regions should be created.
  211. */
  212. static void damon_test_apply_three_regions4(struct kunit *test)
  213. {
  214. /* 10-20-30, 50-55-57-59, 70-80-90-100 */
  215. unsigned long regions[] = {10, 20, 20, 30, 50, 55, 55, 57, 57, 59,
  216. 70, 80, 80, 90, 90, 100};
  217. /* 5-7, 30-32, 65-68 */
  218. struct damon_addr_range new_three_regions[3] = {
  219. (struct damon_addr_range){.start = 5, .end = 7},
  220. (struct damon_addr_range){.start = 30, .end = 32},
  221. (struct damon_addr_range){.start = 65, .end = 68} };
  222. /* expect 5-7, 30-32, 65-68 */
  223. unsigned long expected[] = {5, 7, 30, 32, 65, 68};
  224. damon_do_test_apply_three_regions(test, regions, ARRAY_SIZE(regions),
  225. new_three_regions, expected, ARRAY_SIZE(expected));
  226. }
  227. static void damon_test_split_evenly_fail(struct kunit *test,
  228. unsigned long start, unsigned long end, unsigned int nr_pieces)
  229. {
  230. struct damon_target *t = damon_new_target();
  231. struct damon_region *r;
  232. if (!t)
  233. kunit_skip(test, "target alloc fail");
  234. r = damon_new_region(start, end);
  235. if (!r) {
  236. damon_free_target(t);
  237. kunit_skip(test, "region alloc fail");
  238. }
  239. damon_add_region(r, t);
  240. KUNIT_EXPECT_EQ(test,
  241. damon_va_evenly_split_region(t, r, nr_pieces), -EINVAL);
  242. KUNIT_EXPECT_EQ(test, damon_nr_regions(t), 1u);
  243. damon_for_each_region(r, t) {
  244. KUNIT_EXPECT_EQ(test, r->ar.start, start);
  245. KUNIT_EXPECT_EQ(test, r->ar.end, end);
  246. }
  247. damon_free_target(t);
  248. }
  249. static void damon_test_split_evenly_succ(struct kunit *test,
  250. unsigned long start, unsigned long end, unsigned int nr_pieces)
  251. {
  252. struct damon_target *t = damon_new_target();
  253. struct damon_region *r;
  254. unsigned long expected_width = (end - start) / nr_pieces;
  255. unsigned long i = 0;
  256. if (!t)
  257. kunit_skip(test, "target alloc fail");
  258. r = damon_new_region(start, end);
  259. if (!r) {
  260. damon_free_target(t);
  261. kunit_skip(test, "region alloc fail");
  262. }
  263. damon_add_region(r, t);
  264. KUNIT_EXPECT_EQ(test,
  265. damon_va_evenly_split_region(t, r, nr_pieces), 0);
  266. KUNIT_EXPECT_EQ(test, damon_nr_regions(t), nr_pieces);
  267. damon_for_each_region(r, t) {
  268. if (i == nr_pieces - 1) {
  269. KUNIT_EXPECT_EQ(test,
  270. r->ar.start, start + i * expected_width);
  271. KUNIT_EXPECT_EQ(test, r->ar.end, end);
  272. break;
  273. }
  274. KUNIT_EXPECT_EQ(test,
  275. r->ar.start, start + i++ * expected_width);
  276. KUNIT_EXPECT_EQ(test, r->ar.end, start + i * expected_width);
  277. }
  278. damon_free_target(t);
  279. }
  280. static void damon_test_split_evenly(struct kunit *test)
  281. {
  282. KUNIT_EXPECT_EQ(test, damon_va_evenly_split_region(NULL, NULL, 5),
  283. -EINVAL);
  284. damon_test_split_evenly_fail(test, 0, 100, 0);
  285. damon_test_split_evenly_succ(test, 0, 100, 10);
  286. damon_test_split_evenly_succ(test, 5, 59, 5);
  287. damon_test_split_evenly_succ(test, 4, 6, 1);
  288. damon_test_split_evenly_succ(test, 0, 3, 2);
  289. damon_test_split_evenly_fail(test, 5, 6, 2);
  290. }
  291. static struct kunit_case damon_test_cases[] = {
  292. KUNIT_CASE(damon_test_three_regions_in_vmas),
  293. KUNIT_CASE(damon_test_apply_three_regions1),
  294. KUNIT_CASE(damon_test_apply_three_regions2),
  295. KUNIT_CASE(damon_test_apply_three_regions3),
  296. KUNIT_CASE(damon_test_apply_three_regions4),
  297. KUNIT_CASE(damon_test_split_evenly),
  298. {},
  299. };
  300. static struct kunit_suite damon_test_suite = {
  301. .name = "damon-operations",
  302. .test_cases = damon_test_cases,
  303. };
  304. kunit_test_suite(damon_test_suite);
  305. #endif /* _DAMON_VADDR_TEST_H */
  306. #endif /* CONFIG_DAMON_VADDR_KUNIT_TEST */