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