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