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