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