162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Data Access Monitor Unit Tests 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright 2019 Amazon.com, Inc. or its affiliates. All rights reserved. 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * Author: SeongJae Park <sjpark@amazon.de> 862306a36Sopenharmony_ci */ 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#ifdef CONFIG_DAMON_VADDR_KUNIT_TEST 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ci#ifndef _DAMON_VADDR_TEST_H 1362306a36Sopenharmony_ci#define _DAMON_VADDR_TEST_H 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#include <kunit/test.h> 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_cistatic int __link_vmas(struct maple_tree *mt, struct vm_area_struct *vmas, 1862306a36Sopenharmony_ci ssize_t nr_vmas) 1962306a36Sopenharmony_ci{ 2062306a36Sopenharmony_ci int i, ret = -ENOMEM; 2162306a36Sopenharmony_ci MA_STATE(mas, mt, 0, 0); 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci if (!nr_vmas) 2462306a36Sopenharmony_ci return 0; 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci mas_lock(&mas); 2762306a36Sopenharmony_ci for (i = 0; i < nr_vmas; i++) { 2862306a36Sopenharmony_ci mas_set_range(&mas, vmas[i].vm_start, vmas[i].vm_end - 1); 2962306a36Sopenharmony_ci if (mas_store_gfp(&mas, &vmas[i], GFP_KERNEL)) 3062306a36Sopenharmony_ci goto failed; 3162306a36Sopenharmony_ci } 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci ret = 0; 3462306a36Sopenharmony_cifailed: 3562306a36Sopenharmony_ci mas_unlock(&mas); 3662306a36Sopenharmony_ci return ret; 3762306a36Sopenharmony_ci} 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci/* 4062306a36Sopenharmony_ci * Test __damon_va_three_regions() function 4162306a36Sopenharmony_ci * 4262306a36Sopenharmony_ci * In case of virtual memory address spaces monitoring, DAMON converts the 4362306a36Sopenharmony_ci * complex and dynamic memory mappings of each target task to three 4462306a36Sopenharmony_ci * discontiguous regions which cover every mapped areas. However, the three 4562306a36Sopenharmony_ci * regions should not include the two biggest unmapped areas in the original 4662306a36Sopenharmony_ci * mapping, because the two biggest areas are normally the areas between 1) 4762306a36Sopenharmony_ci * heap and the mmap()-ed regions, and 2) the mmap()-ed regions and stack. 4862306a36Sopenharmony_ci * Because these two unmapped areas are very huge but obviously never accessed, 4962306a36Sopenharmony_ci * covering the region is just a waste. 5062306a36Sopenharmony_ci * 5162306a36Sopenharmony_ci * '__damon_va_three_regions() receives an address space of a process. It 5262306a36Sopenharmony_ci * first identifies the start of mappings, end of mappings, and the two biggest 5362306a36Sopenharmony_ci * unmapped areas. After that, based on the information, it constructs the 5462306a36Sopenharmony_ci * three regions and returns. For more detail, refer to the comment of 5562306a36Sopenharmony_ci * 'damon_init_regions_of()' function definition in 'mm/damon.c' file. 5662306a36Sopenharmony_ci * 5762306a36Sopenharmony_ci * For example, suppose virtual address ranges of 10-20, 20-25, 200-210, 5862306a36Sopenharmony_ci * 210-220, 300-305, and 307-330 (Other comments represent this mappings in 5962306a36Sopenharmony_ci * more short form: 10-20-25, 200-210-220, 300-305, 307-330) of a process are 6062306a36Sopenharmony_ci * mapped. To cover every mappings, the three regions should start with 10, 6162306a36Sopenharmony_ci * and end with 305. The process also has three unmapped areas, 25-200, 6262306a36Sopenharmony_ci * 220-300, and 305-307. Among those, 25-200 and 220-300 are the biggest two 6362306a36Sopenharmony_ci * unmapped areas, and thus it should be converted to three regions of 10-25, 6462306a36Sopenharmony_ci * 200-220, and 300-330. 6562306a36Sopenharmony_ci */ 6662306a36Sopenharmony_cistatic void damon_test_three_regions_in_vmas(struct kunit *test) 6762306a36Sopenharmony_ci{ 6862306a36Sopenharmony_ci static struct mm_struct mm; 6962306a36Sopenharmony_ci struct damon_addr_range regions[3] = {0,}; 7062306a36Sopenharmony_ci /* 10-20-25, 200-210-220, 300-305, 307-330 */ 7162306a36Sopenharmony_ci struct vm_area_struct vmas[] = { 7262306a36Sopenharmony_ci (struct vm_area_struct) {.vm_start = 10, .vm_end = 20}, 7362306a36Sopenharmony_ci (struct vm_area_struct) {.vm_start = 20, .vm_end = 25}, 7462306a36Sopenharmony_ci (struct vm_area_struct) {.vm_start = 200, .vm_end = 210}, 7562306a36Sopenharmony_ci (struct vm_area_struct) {.vm_start = 210, .vm_end = 220}, 7662306a36Sopenharmony_ci (struct vm_area_struct) {.vm_start = 300, .vm_end = 305}, 7762306a36Sopenharmony_ci (struct vm_area_struct) {.vm_start = 307, .vm_end = 330}, 7862306a36Sopenharmony_ci }; 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci mt_init_flags(&mm.mm_mt, MM_MT_FLAGS); 8162306a36Sopenharmony_ci if (__link_vmas(&mm.mm_mt, vmas, ARRAY_SIZE(vmas))) 8262306a36Sopenharmony_ci kunit_skip(test, "Failed to create VMA tree"); 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci __damon_va_three_regions(&mm, regions); 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, 10ul, regions[0].start); 8762306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, 25ul, regions[0].end); 8862306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, 200ul, regions[1].start); 8962306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, 220ul, regions[1].end); 9062306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, 300ul, regions[2].start); 9162306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, 330ul, regions[2].end); 9262306a36Sopenharmony_ci} 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_cistatic struct damon_region *__nth_region_of(struct damon_target *t, int idx) 9562306a36Sopenharmony_ci{ 9662306a36Sopenharmony_ci struct damon_region *r; 9762306a36Sopenharmony_ci unsigned int i = 0; 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci damon_for_each_region(r, t) { 10062306a36Sopenharmony_ci if (i++ == idx) 10162306a36Sopenharmony_ci return r; 10262306a36Sopenharmony_ci } 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci return NULL; 10562306a36Sopenharmony_ci} 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci/* 10862306a36Sopenharmony_ci * Test 'damon_set_regions()' 10962306a36Sopenharmony_ci * 11062306a36Sopenharmony_ci * test kunit object 11162306a36Sopenharmony_ci * regions an array containing start/end addresses of current 11262306a36Sopenharmony_ci * monitoring target regions 11362306a36Sopenharmony_ci * nr_regions the number of the addresses in 'regions' 11462306a36Sopenharmony_ci * three_regions The three regions that need to be applied now 11562306a36Sopenharmony_ci * expected start/end addresses of monitoring target regions that 11662306a36Sopenharmony_ci * 'three_regions' are applied 11762306a36Sopenharmony_ci * nr_expected the number of addresses in 'expected' 11862306a36Sopenharmony_ci * 11962306a36Sopenharmony_ci * The memory mapping of the target processes changes dynamically. To follow 12062306a36Sopenharmony_ci * the change, DAMON periodically reads the mappings, simplifies it to the 12162306a36Sopenharmony_ci * three regions, and updates the monitoring target regions to fit in the three 12262306a36Sopenharmony_ci * regions. The update of current target regions is the role of 12362306a36Sopenharmony_ci * 'damon_set_regions()'. 12462306a36Sopenharmony_ci * 12562306a36Sopenharmony_ci * This test passes the given target regions and the new three regions that 12662306a36Sopenharmony_ci * need to be applied to the function and check whether it updates the regions 12762306a36Sopenharmony_ci * as expected. 12862306a36Sopenharmony_ci */ 12962306a36Sopenharmony_cistatic void damon_do_test_apply_three_regions(struct kunit *test, 13062306a36Sopenharmony_ci unsigned long *regions, int nr_regions, 13162306a36Sopenharmony_ci struct damon_addr_range *three_regions, 13262306a36Sopenharmony_ci unsigned long *expected, int nr_expected) 13362306a36Sopenharmony_ci{ 13462306a36Sopenharmony_ci struct damon_target *t; 13562306a36Sopenharmony_ci struct damon_region *r; 13662306a36Sopenharmony_ci int i; 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ci t = damon_new_target(); 13962306a36Sopenharmony_ci for (i = 0; i < nr_regions / 2; i++) { 14062306a36Sopenharmony_ci r = damon_new_region(regions[i * 2], regions[i * 2 + 1]); 14162306a36Sopenharmony_ci damon_add_region(r, t); 14262306a36Sopenharmony_ci } 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci damon_set_regions(t, three_regions, 3); 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci for (i = 0; i < nr_expected / 2; i++) { 14762306a36Sopenharmony_ci r = __nth_region_of(t, i); 14862306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, r->ar.start, expected[i * 2]); 14962306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, r->ar.end, expected[i * 2 + 1]); 15062306a36Sopenharmony_ci } 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci damon_destroy_target(t); 15362306a36Sopenharmony_ci} 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci/* 15662306a36Sopenharmony_ci * This function test most common case where the three big regions are only 15762306a36Sopenharmony_ci * slightly changed. Target regions should adjust their boundary (10-20-30, 15862306a36Sopenharmony_ci * 50-55, 70-80, 90-100) to fit with the new big regions or remove target 15962306a36Sopenharmony_ci * regions (57-79) that now out of the three regions. 16062306a36Sopenharmony_ci */ 16162306a36Sopenharmony_cistatic void damon_test_apply_three_regions1(struct kunit *test) 16262306a36Sopenharmony_ci{ 16362306a36Sopenharmony_ci /* 10-20-30, 50-55-57-59, 70-80-90-100 */ 16462306a36Sopenharmony_ci unsigned long regions[] = {10, 20, 20, 30, 50, 55, 55, 57, 57, 59, 16562306a36Sopenharmony_ci 70, 80, 80, 90, 90, 100}; 16662306a36Sopenharmony_ci /* 5-27, 45-55, 73-104 */ 16762306a36Sopenharmony_ci struct damon_addr_range new_three_regions[3] = { 16862306a36Sopenharmony_ci (struct damon_addr_range){.start = 5, .end = 27}, 16962306a36Sopenharmony_ci (struct damon_addr_range){.start = 45, .end = 55}, 17062306a36Sopenharmony_ci (struct damon_addr_range){.start = 73, .end = 104} }; 17162306a36Sopenharmony_ci /* 5-20-27, 45-55, 73-80-90-104 */ 17262306a36Sopenharmony_ci unsigned long expected[] = {5, 20, 20, 27, 45, 55, 17362306a36Sopenharmony_ci 73, 80, 80, 90, 90, 104}; 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci damon_do_test_apply_three_regions(test, regions, ARRAY_SIZE(regions), 17662306a36Sopenharmony_ci new_three_regions, expected, ARRAY_SIZE(expected)); 17762306a36Sopenharmony_ci} 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_ci/* 18062306a36Sopenharmony_ci * Test slightly bigger change. Similar to above, but the second big region 18162306a36Sopenharmony_ci * now require two target regions (50-55, 57-59) to be removed. 18262306a36Sopenharmony_ci */ 18362306a36Sopenharmony_cistatic void damon_test_apply_three_regions2(struct kunit *test) 18462306a36Sopenharmony_ci{ 18562306a36Sopenharmony_ci /* 10-20-30, 50-55-57-59, 70-80-90-100 */ 18662306a36Sopenharmony_ci unsigned long regions[] = {10, 20, 20, 30, 50, 55, 55, 57, 57, 59, 18762306a36Sopenharmony_ci 70, 80, 80, 90, 90, 100}; 18862306a36Sopenharmony_ci /* 5-27, 56-57, 65-104 */ 18962306a36Sopenharmony_ci struct damon_addr_range new_three_regions[3] = { 19062306a36Sopenharmony_ci (struct damon_addr_range){.start = 5, .end = 27}, 19162306a36Sopenharmony_ci (struct damon_addr_range){.start = 56, .end = 57}, 19262306a36Sopenharmony_ci (struct damon_addr_range){.start = 65, .end = 104} }; 19362306a36Sopenharmony_ci /* 5-20-27, 56-57, 65-80-90-104 */ 19462306a36Sopenharmony_ci unsigned long expected[] = {5, 20, 20, 27, 56, 57, 19562306a36Sopenharmony_ci 65, 80, 80, 90, 90, 104}; 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci damon_do_test_apply_three_regions(test, regions, ARRAY_SIZE(regions), 19862306a36Sopenharmony_ci new_three_regions, expected, ARRAY_SIZE(expected)); 19962306a36Sopenharmony_ci} 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci/* 20262306a36Sopenharmony_ci * Test a big change. The second big region has totally freed and mapped to 20362306a36Sopenharmony_ci * different area (50-59 -> 61-63). The target regions which were in the old 20462306a36Sopenharmony_ci * second big region (50-55-57-59) should be removed and new target region 20562306a36Sopenharmony_ci * covering the second big region (61-63) should be created. 20662306a36Sopenharmony_ci */ 20762306a36Sopenharmony_cistatic void damon_test_apply_three_regions3(struct kunit *test) 20862306a36Sopenharmony_ci{ 20962306a36Sopenharmony_ci /* 10-20-30, 50-55-57-59, 70-80-90-100 */ 21062306a36Sopenharmony_ci unsigned long regions[] = {10, 20, 20, 30, 50, 55, 55, 57, 57, 59, 21162306a36Sopenharmony_ci 70, 80, 80, 90, 90, 100}; 21262306a36Sopenharmony_ci /* 5-27, 61-63, 65-104 */ 21362306a36Sopenharmony_ci struct damon_addr_range new_three_regions[3] = { 21462306a36Sopenharmony_ci (struct damon_addr_range){.start = 5, .end = 27}, 21562306a36Sopenharmony_ci (struct damon_addr_range){.start = 61, .end = 63}, 21662306a36Sopenharmony_ci (struct damon_addr_range){.start = 65, .end = 104} }; 21762306a36Sopenharmony_ci /* 5-20-27, 61-63, 65-80-90-104 */ 21862306a36Sopenharmony_ci unsigned long expected[] = {5, 20, 20, 27, 61, 63, 21962306a36Sopenharmony_ci 65, 80, 80, 90, 90, 104}; 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_ci damon_do_test_apply_three_regions(test, regions, ARRAY_SIZE(regions), 22262306a36Sopenharmony_ci new_three_regions, expected, ARRAY_SIZE(expected)); 22362306a36Sopenharmony_ci} 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci/* 22662306a36Sopenharmony_ci * Test another big change. Both of the second and third big regions (50-59 22762306a36Sopenharmony_ci * and 70-100) has totally freed and mapped to different area (30-32 and 22862306a36Sopenharmony_ci * 65-68). The target regions which were in the old second and third big 22962306a36Sopenharmony_ci * regions should now be removed and new target regions covering the new second 23062306a36Sopenharmony_ci * and third big regions should be created. 23162306a36Sopenharmony_ci */ 23262306a36Sopenharmony_cistatic void damon_test_apply_three_regions4(struct kunit *test) 23362306a36Sopenharmony_ci{ 23462306a36Sopenharmony_ci /* 10-20-30, 50-55-57-59, 70-80-90-100 */ 23562306a36Sopenharmony_ci unsigned long regions[] = {10, 20, 20, 30, 50, 55, 55, 57, 57, 59, 23662306a36Sopenharmony_ci 70, 80, 80, 90, 90, 100}; 23762306a36Sopenharmony_ci /* 5-7, 30-32, 65-68 */ 23862306a36Sopenharmony_ci struct damon_addr_range new_three_regions[3] = { 23962306a36Sopenharmony_ci (struct damon_addr_range){.start = 5, .end = 7}, 24062306a36Sopenharmony_ci (struct damon_addr_range){.start = 30, .end = 32}, 24162306a36Sopenharmony_ci (struct damon_addr_range){.start = 65, .end = 68} }; 24262306a36Sopenharmony_ci /* expect 5-7, 30-32, 65-68 */ 24362306a36Sopenharmony_ci unsigned long expected[] = {5, 7, 30, 32, 65, 68}; 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ci damon_do_test_apply_three_regions(test, regions, ARRAY_SIZE(regions), 24662306a36Sopenharmony_ci new_three_regions, expected, ARRAY_SIZE(expected)); 24762306a36Sopenharmony_ci} 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_cistatic void damon_test_split_evenly_fail(struct kunit *test, 25062306a36Sopenharmony_ci unsigned long start, unsigned long end, unsigned int nr_pieces) 25162306a36Sopenharmony_ci{ 25262306a36Sopenharmony_ci struct damon_target *t = damon_new_target(); 25362306a36Sopenharmony_ci struct damon_region *r = damon_new_region(start, end); 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_ci damon_add_region(r, t); 25662306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, 25762306a36Sopenharmony_ci damon_va_evenly_split_region(t, r, nr_pieces), -EINVAL); 25862306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, damon_nr_regions(t), 1u); 25962306a36Sopenharmony_ci 26062306a36Sopenharmony_ci damon_for_each_region(r, t) { 26162306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, r->ar.start, start); 26262306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, r->ar.end, end); 26362306a36Sopenharmony_ci } 26462306a36Sopenharmony_ci 26562306a36Sopenharmony_ci damon_free_target(t); 26662306a36Sopenharmony_ci} 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_cistatic void damon_test_split_evenly_succ(struct kunit *test, 26962306a36Sopenharmony_ci unsigned long start, unsigned long end, unsigned int nr_pieces) 27062306a36Sopenharmony_ci{ 27162306a36Sopenharmony_ci struct damon_target *t = damon_new_target(); 27262306a36Sopenharmony_ci struct damon_region *r = damon_new_region(start, end); 27362306a36Sopenharmony_ci unsigned long expected_width = (end - start) / nr_pieces; 27462306a36Sopenharmony_ci unsigned long i = 0; 27562306a36Sopenharmony_ci 27662306a36Sopenharmony_ci damon_add_region(r, t); 27762306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, 27862306a36Sopenharmony_ci damon_va_evenly_split_region(t, r, nr_pieces), 0); 27962306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, damon_nr_regions(t), nr_pieces); 28062306a36Sopenharmony_ci 28162306a36Sopenharmony_ci damon_for_each_region(r, t) { 28262306a36Sopenharmony_ci if (i == nr_pieces - 1) { 28362306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, 28462306a36Sopenharmony_ci r->ar.start, start + i * expected_width); 28562306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, r->ar.end, end); 28662306a36Sopenharmony_ci break; 28762306a36Sopenharmony_ci } 28862306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, 28962306a36Sopenharmony_ci r->ar.start, start + i++ * expected_width); 29062306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, r->ar.end, start + i * expected_width); 29162306a36Sopenharmony_ci } 29262306a36Sopenharmony_ci damon_free_target(t); 29362306a36Sopenharmony_ci} 29462306a36Sopenharmony_ci 29562306a36Sopenharmony_cistatic void damon_test_split_evenly(struct kunit *test) 29662306a36Sopenharmony_ci{ 29762306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, damon_va_evenly_split_region(NULL, NULL, 5), 29862306a36Sopenharmony_ci -EINVAL); 29962306a36Sopenharmony_ci 30062306a36Sopenharmony_ci damon_test_split_evenly_fail(test, 0, 100, 0); 30162306a36Sopenharmony_ci damon_test_split_evenly_succ(test, 0, 100, 10); 30262306a36Sopenharmony_ci damon_test_split_evenly_succ(test, 5, 59, 5); 30362306a36Sopenharmony_ci damon_test_split_evenly_fail(test, 5, 6, 2); 30462306a36Sopenharmony_ci} 30562306a36Sopenharmony_ci 30662306a36Sopenharmony_cistatic struct kunit_case damon_test_cases[] = { 30762306a36Sopenharmony_ci KUNIT_CASE(damon_test_three_regions_in_vmas), 30862306a36Sopenharmony_ci KUNIT_CASE(damon_test_apply_three_regions1), 30962306a36Sopenharmony_ci KUNIT_CASE(damon_test_apply_three_regions2), 31062306a36Sopenharmony_ci KUNIT_CASE(damon_test_apply_three_regions3), 31162306a36Sopenharmony_ci KUNIT_CASE(damon_test_apply_three_regions4), 31262306a36Sopenharmony_ci KUNIT_CASE(damon_test_split_evenly), 31362306a36Sopenharmony_ci {}, 31462306a36Sopenharmony_ci}; 31562306a36Sopenharmony_ci 31662306a36Sopenharmony_cistatic struct kunit_suite damon_test_suite = { 31762306a36Sopenharmony_ci .name = "damon-operations", 31862306a36Sopenharmony_ci .test_cases = damon_test_cases, 31962306a36Sopenharmony_ci}; 32062306a36Sopenharmony_cikunit_test_suite(damon_test_suite); 32162306a36Sopenharmony_ci 32262306a36Sopenharmony_ci#endif /* _DAMON_VADDR_TEST_H */ 32362306a36Sopenharmony_ci 32462306a36Sopenharmony_ci#endif /* CONFIG_DAMON_VADDR_KUNIT_TEST */ 325