xref: /openbmc/linux/mm/damon/vaddr-test.h (revision b9221f71)
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 
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  */
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 
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  */
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_ctx *ctx = damon_new_ctx();
139 	struct damon_target *t;
140 	struct damon_region *r;
141 	int i;
142 
143 	t = damon_new_target(42);
144 	for (i = 0; i < nr_regions / 2; i++) {
145 		r = damon_new_region(regions[i * 2], regions[i * 2 + 1]);
146 		damon_add_region(r, t);
147 	}
148 	damon_add_target(ctx, t);
149 
150 	damon_va_apply_three_regions(t, three_regions);
151 
152 	for (i = 0; i < nr_expected / 2; i++) {
153 		r = __nth_region_of(t, i);
154 		KUNIT_EXPECT_EQ(test, r->ar.start, expected[i * 2]);
155 		KUNIT_EXPECT_EQ(test, r->ar.end, expected[i * 2 + 1]);
156 	}
157 
158 	damon_destroy_ctx(ctx);
159 }
160 
161 /*
162  * This function test most common case where the three big regions are only
163  * slightly changed.  Target regions should adjust their boundary (10-20-30,
164  * 50-55, 70-80, 90-100) to fit with the new big regions or remove target
165  * regions (57-79) that now out of the three regions.
166  */
167 static void damon_test_apply_three_regions1(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, 45-55, 73-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 = 45, .end = 55},
176 		(struct damon_addr_range){.start = 73, .end = 104} };
177 	/* 5-20-27, 45-55, 73-80-90-104 */
178 	unsigned long expected[] = {5, 20, 20, 27, 45, 55,
179 				73, 80, 80, 90, 90, 104};
180 
181 	damon_do_test_apply_three_regions(test, regions, ARRAY_SIZE(regions),
182 			new_three_regions, expected, ARRAY_SIZE(expected));
183 }
184 
185 /*
186  * Test slightly bigger change.  Similar to above, but the second big region
187  * now require two target regions (50-55, 57-59) to be removed.
188  */
189 static void damon_test_apply_three_regions2(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, 56-57, 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 = 56, .end = 57},
198 		(struct damon_addr_range){.start = 65, .end = 104} };
199 	/* 5-20-27, 56-57, 65-80-90-104 */
200 	unsigned long expected[] = {5, 20, 20, 27, 56, 57,
201 				65, 80, 80, 90, 90, 104};
202 
203 	damon_do_test_apply_three_regions(test, regions, ARRAY_SIZE(regions),
204 			new_three_regions, expected, ARRAY_SIZE(expected));
205 }
206 
207 /*
208  * Test a big change.  The second big region has totally freed and mapped to
209  * different area (50-59 -> 61-63).  The target regions which were in the old
210  * second big region (50-55-57-59) should be removed and new target region
211  * covering the second big region (61-63) should be created.
212  */
213 static void damon_test_apply_three_regions3(struct kunit *test)
214 {
215 	/* 10-20-30, 50-55-57-59, 70-80-90-100 */
216 	unsigned long regions[] = {10, 20, 20, 30, 50, 55, 55, 57, 57, 59,
217 				70, 80, 80, 90, 90, 100};
218 	/* 5-27, 61-63, 65-104 */
219 	struct damon_addr_range new_three_regions[3] = {
220 		(struct damon_addr_range){.start = 5, .end = 27},
221 		(struct damon_addr_range){.start = 61, .end = 63},
222 		(struct damon_addr_range){.start = 65, .end = 104} };
223 	/* 5-20-27, 61-63, 65-80-90-104 */
224 	unsigned long expected[] = {5, 20, 20, 27, 61, 63,
225 				65, 80, 80, 90, 90, 104};
226 
227 	damon_do_test_apply_three_regions(test, regions, ARRAY_SIZE(regions),
228 			new_three_regions, expected, ARRAY_SIZE(expected));
229 }
230 
231 /*
232  * Test another big change.  Both of the second and third big regions (50-59
233  * and 70-100) has totally freed and mapped to different area (30-32 and
234  * 65-68).  The target regions which were in the old second and third big
235  * regions should now be removed and new target regions covering the new second
236  * and third big regions should be crated.
237  */
238 static void damon_test_apply_three_regions4(struct kunit *test)
239 {
240 	/* 10-20-30, 50-55-57-59, 70-80-90-100 */
241 	unsigned long regions[] = {10, 20, 20, 30, 50, 55, 55, 57, 57, 59,
242 				70, 80, 80, 90, 90, 100};
243 	/* 5-7, 30-32, 65-68 */
244 	struct damon_addr_range new_three_regions[3] = {
245 		(struct damon_addr_range){.start = 5, .end = 7},
246 		(struct damon_addr_range){.start = 30, .end = 32},
247 		(struct damon_addr_range){.start = 65, .end = 68} };
248 	/* expect 5-7, 30-32, 65-68 */
249 	unsigned long expected[] = {5, 7, 30, 32, 65, 68};
250 
251 	damon_do_test_apply_three_regions(test, regions, ARRAY_SIZE(regions),
252 			new_three_regions, expected, ARRAY_SIZE(expected));
253 }
254 
255 static void damon_test_split_evenly(struct kunit *test)
256 {
257 	struct damon_ctx *c = damon_new_ctx();
258 	struct damon_target *t;
259 	struct damon_region *r;
260 	unsigned long i;
261 
262 	KUNIT_EXPECT_EQ(test, damon_va_evenly_split_region(NULL, NULL, 5),
263 			-EINVAL);
264 
265 	t = damon_new_target(42);
266 	r = damon_new_region(0, 100);
267 	KUNIT_EXPECT_EQ(test, damon_va_evenly_split_region(t, r, 0), -EINVAL);
268 
269 	damon_add_region(r, t);
270 	KUNIT_EXPECT_EQ(test, damon_va_evenly_split_region(t, r, 10), 0);
271 	KUNIT_EXPECT_EQ(test, damon_nr_regions(t), 10u);
272 
273 	i = 0;
274 	damon_for_each_region(r, t) {
275 		KUNIT_EXPECT_EQ(test, r->ar.start, i++ * 10);
276 		KUNIT_EXPECT_EQ(test, r->ar.end, i * 10);
277 	}
278 	damon_free_target(t);
279 
280 	t = damon_new_target(42);
281 	r = damon_new_region(5, 59);
282 	damon_add_region(r, t);
283 	KUNIT_EXPECT_EQ(test, damon_va_evenly_split_region(t, r, 5), 0);
284 	KUNIT_EXPECT_EQ(test, damon_nr_regions(t), 5u);
285 
286 	i = 0;
287 	damon_for_each_region(r, t) {
288 		if (i == 4)
289 			break;
290 		KUNIT_EXPECT_EQ(test, r->ar.start, 5 + 10 * i++);
291 		KUNIT_EXPECT_EQ(test, r->ar.end, 5 + 10 * i);
292 	}
293 	KUNIT_EXPECT_EQ(test, r->ar.start, 5 + 10 * i);
294 	KUNIT_EXPECT_EQ(test, r->ar.end, 59ul);
295 	damon_free_target(t);
296 
297 	t = damon_new_target(42);
298 	r = damon_new_region(5, 6);
299 	damon_add_region(r, t);
300 	KUNIT_EXPECT_EQ(test, damon_va_evenly_split_region(t, r, 2), -EINVAL);
301 	KUNIT_EXPECT_EQ(test, damon_nr_regions(t), 1u);
302 
303 	damon_for_each_region(r, t) {
304 		KUNIT_EXPECT_EQ(test, r->ar.start, 5ul);
305 		KUNIT_EXPECT_EQ(test, r->ar.end, 6ul);
306 	}
307 	damon_free_target(t);
308 	damon_destroy_ctx(c);
309 }
310 
311 static struct kunit_case damon_test_cases[] = {
312 	KUNIT_CASE(damon_test_three_regions_in_vmas),
313 	KUNIT_CASE(damon_test_apply_three_regions1),
314 	KUNIT_CASE(damon_test_apply_three_regions2),
315 	KUNIT_CASE(damon_test_apply_three_regions3),
316 	KUNIT_CASE(damon_test_apply_three_regions4),
317 	KUNIT_CASE(damon_test_split_evenly),
318 	{},
319 };
320 
321 static struct kunit_suite damon_test_suite = {
322 	.name = "damon-primitives",
323 	.test_cases = damon_test_cases,
324 };
325 kunit_test_suite(damon_test_suite);
326 
327 #endif /* _DAMON_VADDR_TEST_H */
328 
329 #endif	/* CONFIG_DAMON_VADDR_KUNIT_TEST */
330