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