File Index
Files and directories
Sign in
FILE

vaddr-kunit.h

File preview with syntax highlighting.

linux-6.19.14/mm/damon/tests/vaddr-kunit.h C
  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 <sj@kernel.org>
  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
 17static int __link_vmas(struct maple_tree *mt, struct vm_area_struct *vmas,
 18			ssize_t nr_vmas)
 19{
 20	int i, ret = -ENOMEM;
 21	MA_STATE(mas, mt, 0, 0);
 22
 23	if (!nr_vmas)
 24		return 0;
 25
 26	mas_lock(&mas);
 27	for (i = 0; i < nr_vmas; i++) {
 28		mas_set_range(&mas, vmas[i].vm_start, vmas[i].vm_end - 1);
 29		if (mas_store_gfp(&mas, &vmas[i], GFP_KERNEL))
 30			goto failed;
 31	}
 32
 33	ret = 0;
 34failed:
 35	mas_unlock(&mas);
 36	return ret;
 37}
 38
 39/*
 40 * Test __damon_va_three_regions() function
 41 *
 42 * In case of virtual memory address spaces monitoring, DAMON converts the
 43 * complex and dynamic memory mappings of each target task to three
 44 * discontiguous regions which cover every mapped areas.  However, the three
 45 * regions should not include the two biggest unmapped areas in the original
 46 * mapping, because the two biggest areas are normally the areas between 1)
 47 * heap and the mmap()-ed regions, and 2) the mmap()-ed regions and stack.
 48 * Because these two unmapped areas are very huge but obviously never accessed,
 49 * covering the region is just a waste.
 50 *
 51 * '__damon_va_three_regions() receives an address space of a process.  It
 52 * first identifies the start of mappings, end of mappings, and the two biggest
 53 * unmapped areas.  After that, based on the information, it constructs the
 54 * three regions and returns.  For more detail, refer to the comment of
 55 * 'damon_init_regions_of()' function definition in 'mm/damon.c' file.
 56 *
 57 * For example, suppose virtual address ranges of 10-20, 20-25, 200-210,
 58 * 210-220, 300-305, and 307-330 (Other comments represent this mappings in
 59 * more short form: 10-20-25, 200-210-220, 300-305, 307-330) of a process are
 60 * mapped.  To cover every mappings, the three regions should start with 10,
 61 * and end with 305.  The process also has three unmapped areas, 25-200,
 62 * 220-300, and 305-307.  Among those, 25-200 and 220-300 are the biggest two
 63 * unmapped areas, and thus it should be converted to three regions of 10-25,
 64 * 200-220, and 300-330.
 65 */
 66static void damon_test_three_regions_in_vmas(struct kunit *test)
 67{
 68	static struct mm_struct mm;
 69	struct damon_addr_range regions[3] = {0};
 70	/* 10-20-25, 200-210-220, 300-305, 307-330 */
 71	static struct vm_area_struct vmas[] = {
 72		(struct vm_area_struct) {.vm_start = 10, .vm_end = 20},
 73		(struct vm_area_struct) {.vm_start = 20, .vm_end = 25},
 74		(struct vm_area_struct) {.vm_start = 200, .vm_end = 210},
 75		(struct vm_area_struct) {.vm_start = 210, .vm_end = 220},
 76		(struct vm_area_struct) {.vm_start = 300, .vm_end = 305},
 77		(struct vm_area_struct) {.vm_start = 307, .vm_end = 330},
 78	};
 79
 80	mt_init_flags(&mm.mm_mt, MT_FLAGS_ALLOC_RANGE | MT_FLAGS_USE_RCU);
 81	if (__link_vmas(&mm.mm_mt, vmas, ARRAY_SIZE(vmas)))
 82		kunit_skip(test, "Failed to create VMA tree");
 83
 84	__damon_va_three_regions(&mm, regions);
 85
 86	KUNIT_EXPECT_EQ(test, 10ul, regions[0].start);
 87	KUNIT_EXPECT_EQ(test, 25ul, regions[0].end);
 88	KUNIT_EXPECT_EQ(test, 200ul, regions[1].start);
 89	KUNIT_EXPECT_EQ(test, 220ul, regions[1].end);
 90	KUNIT_EXPECT_EQ(test, 300ul, regions[2].start);
 91	KUNIT_EXPECT_EQ(test, 330ul, regions[2].end);
 92}
 93
 94static struct damon_region *__nth_region_of(struct damon_target *t, int idx)
 95{
 96	struct damon_region *r;
 97	unsigned int i = 0;
 98
 99	damon_for_each_region(r, t) {
100		if (i++ == idx)
101			return r;
102	}
103
104	return NULL;
105}
106
107/*
108 * Test 'damon_set_regions()'
109 *
110 * test			kunit object
111 * regions		an array containing start/end addresses of current
112 *			monitoring target regions
113 * nr_regions		the number of the addresses in 'regions'
114 * three_regions	The three regions that need to be applied now
115 * expected		start/end addresses of monitoring target regions that
116 *			'three_regions' are applied
117 * nr_expected		the number of addresses in 'expected'
118 *
119 * The memory mapping of the target processes changes dynamically.  To follow
120 * the change, DAMON periodically reads the mappings, simplifies it to the
121 * three regions, and updates the monitoring target regions to fit in the three
122 * regions.  The update of current target regions is the role of
123 * 'damon_set_regions()'.
124 *
125 * This test passes the given target regions and the new three regions that
126 * need to be applied to the function and check whether it updates the regions
127 * as expected.
128 */
129static void damon_do_test_apply_three_regions(struct kunit *test,
130				unsigned long *regions, int nr_regions,
131				struct damon_addr_range *three_regions,
132				unsigned long *expected, int nr_expected)
133{
134	struct damon_target *t;
135	struct damon_region *r;
136	int i;
137
138	t = damon_new_target();
139	if (!t)
140		kunit_skip(test, "target alloc fail");
141	for (i = 0; i < nr_regions / 2; i++) {
142		r = damon_new_region(regions[i * 2], regions[i * 2 + 1]);
143		if (!r) {
144			damon_destroy_target(t, NULL);
145			kunit_skip(test, "region alloc fail");
146		}
147		damon_add_region(r, t);
148	}
149
150	damon_set_regions(t, three_regions, 3, DAMON_MIN_REGION_SZ);
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_target(t, NULL);
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 */
167static 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 */
189static 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 */
213static 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 created.
237 */
238static 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
255static void damon_test_split_evenly_fail(struct kunit *test,
256		unsigned long start, unsigned long end, unsigned int nr_pieces)
257{
258	struct damon_target *t = damon_new_target();
259	struct damon_region *r;
260
261	if (!t)
262		kunit_skip(test, "target alloc fail");
263
264	r = damon_new_region(start, end);
265	if (!r) {
266		damon_free_target(t);
267		kunit_skip(test, "region alloc fail");
268	}
269
270	damon_add_region(r, t);
271	KUNIT_EXPECT_EQ(test,
272			damon_va_evenly_split_region(t, r, nr_pieces), -EINVAL);
273	KUNIT_EXPECT_EQ(test, damon_nr_regions(t), 1u);
274
275	damon_for_each_region(r, t) {
276		KUNIT_EXPECT_EQ(test, r->ar.start, start);
277		KUNIT_EXPECT_EQ(test, r->ar.end, end);
278	}
279
280	damon_free_target(t);
281}
282
283static void damon_test_split_evenly_succ(struct kunit *test,
284	unsigned long start, unsigned long end, unsigned int nr_pieces)
285{
286	struct damon_target *t = damon_new_target();
287	struct damon_region *r;
288	unsigned long expected_width = (end - start) / nr_pieces;
289	unsigned long i = 0;
290
291	if (!t)
292		kunit_skip(test, "target alloc fail");
293	r = damon_new_region(start, end);
294	if (!r) {
295		damon_free_target(t);
296		kunit_skip(test, "region alloc fail");
297	}
298	damon_add_region(r, t);
299	KUNIT_EXPECT_EQ(test,
300			damon_va_evenly_split_region(t, r, nr_pieces), 0);
301	KUNIT_EXPECT_EQ(test, damon_nr_regions(t), nr_pieces);
302
303	damon_for_each_region(r, t) {
304		if (i == nr_pieces - 1) {
305			KUNIT_EXPECT_EQ(test,
306				r->ar.start, start + i * expected_width);
307			KUNIT_EXPECT_EQ(test, r->ar.end, end);
308			break;
309		}
310		KUNIT_EXPECT_EQ(test,
311				r->ar.start, start + i++ * expected_width);
312		KUNIT_EXPECT_EQ(test, r->ar.end, start + i * expected_width);
313	}
314	damon_free_target(t);
315}
316
317static void damon_test_split_evenly(struct kunit *test)
318{
319	KUNIT_EXPECT_EQ(test, damon_va_evenly_split_region(NULL, NULL, 5),
320			-EINVAL);
321
322	damon_test_split_evenly_fail(test, 0, 100, 0);
323	damon_test_split_evenly_succ(test, 0, 100, 10);
324	damon_test_split_evenly_succ(test, 5, 59, 5);
325	damon_test_split_evenly_succ(test, 4, 6, 1);
326	damon_test_split_evenly_succ(test, 0, 3, 2);
327	damon_test_split_evenly_fail(test, 5, 6, 2);
328}
329
330static struct kunit_case damon_test_cases[] = {
331	KUNIT_CASE(damon_test_three_regions_in_vmas),
332	KUNIT_CASE(damon_test_apply_three_regions1),
333	KUNIT_CASE(damon_test_apply_three_regions2),
334	KUNIT_CASE(damon_test_apply_three_regions3),
335	KUNIT_CASE(damon_test_apply_three_regions4),
336	KUNIT_CASE(damon_test_split_evenly),
337	{},
338};
339
340static struct kunit_suite damon_test_suite = {
341	.name = "damon-operations",
342	.test_cases = damon_test_cases,
343};
344kunit_test_suite(damon_test_suite);
345
346#endif /* _DAMON_VADDR_TEST_H */
347
348#endif	/* CONFIG_DAMON_VADDR_KUNIT_TEST */
~/ Read-only file preview.