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