xref: /linux/tools/testing/vma/tests/vma.c (revision 6aacab308a5dfd222b2d23662bbae60c11007cfb)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 static bool test_copy_vma(void)
4 {
5 	vm_flags_t vm_flags = VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE;
6 	struct mm_struct mm = {};
7 	bool need_locks = false;
8 	VMA_ITERATOR(vmi, &mm, 0);
9 	struct vm_area_struct *vma, *vma_new, *vma_next;
10 
11 	/* Move backwards and do not merge. */
12 
13 	vma = alloc_and_link_vma(&mm, 0x3000, 0x5000, 3, vm_flags);
14 	vma_new = copy_vma(&vma, 0, 0x2000, 0, &need_locks);
15 	ASSERT_NE(vma_new, vma);
16 	ASSERT_EQ(vma_new->vm_start, 0);
17 	ASSERT_EQ(vma_new->vm_end, 0x2000);
18 	ASSERT_EQ(vma_new->vm_pgoff, 0);
19 	vma_assert_attached(vma_new);
20 
21 	cleanup_mm(&mm, &vmi);
22 
23 	/* Move a VMA into position next to another and merge the two. */
24 
25 	vma = alloc_and_link_vma(&mm, 0, 0x2000, 0, vm_flags);
26 	vma_next = alloc_and_link_vma(&mm, 0x6000, 0x8000, 6, vm_flags);
27 	vma_new = copy_vma(&vma, 0x4000, 0x2000, 4, &need_locks);
28 	vma_assert_attached(vma_new);
29 
30 	ASSERT_EQ(vma_new, vma_next);
31 
32 	cleanup_mm(&mm, &vmi);
33 	return true;
34 }
35 
36 static void run_vma_tests(int *num_tests, int *num_fail)
37 {
38 	TEST(copy_vma);
39 }
40