xref: /linux/tools/testing/vma/shared.c (revision 7135a05838e85845e1bbf6caa6596a7e3b791a40)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #include "shared.h"
4 
5 
6 bool fail_prealloc;
7 unsigned long mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
8 unsigned long dac_mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
9 unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT;
10 
11 const struct vm_operations_struct vma_dummy_vm_ops;
12 struct anon_vma dummy_anon_vma;
13 struct task_struct __current;
14 
15 static int mmap_zero_prepare(struct vm_area_desc *desc)
16 {
17 	return 0;
18 }
19 
20 const struct file_operations zero_fops = {
21 	.mmap_prepare = mmap_zero_prepare,
22 };
23 
24 struct vm_area_struct *alloc_vma(struct mm_struct *mm,
25 		unsigned long start, unsigned long end,
26 		pgoff_t pgoff, vma_flags_t vma_flags)
27 {
28 	struct vm_area_struct *vma = vm_area_alloc(mm);
29 
30 	if (vma == NULL)
31 		return NULL;
32 
33 	vma->vm_start = start;
34 	vma->vm_end = end;
35 	vma_set_pgoff(vma, pgoff);
36 	vma_set_anon_pgoff(vma, start >> PAGE_SHIFT);
37 	vma->flags = vma_flags;
38 	vma_assert_detached(vma);
39 
40 	return vma;
41 }
42 
43 void detach_free_vma(struct vm_area_struct *vma)
44 {
45 	vma_mark_detached(vma);
46 	vm_area_free(vma);
47 }
48 
49 struct vm_area_struct *alloc_and_link_vma(struct mm_struct *mm,
50 		unsigned long start, unsigned long end,
51 		pgoff_t pgoff, vma_flags_t vma_flags)
52 {
53 	struct vm_area_struct *vma = alloc_vma(mm, start, end, pgoff, vma_flags);
54 
55 	if (vma == NULL)
56 		return NULL;
57 
58 	if (attach_vma(mm, vma)) {
59 		detach_free_vma(vma);
60 		return NULL;
61 	}
62 
63 	/*
64 	 * Reset this counter which we use to track whether writes have
65 	 * begun. Linking to the tree will have caused this to be incremented,
66 	 * which means we will get a false positive otherwise.
67 	 */
68 	vma->vm_lock_seq = 0;
69 
70 	return vma;
71 }
72 
73 void reset_dummy_anon_vma(void)
74 {
75 	dummy_anon_vma.was_cloned = false;
76 	dummy_anon_vma.was_unlinked = false;
77 }
78 
79 int cleanup_mm(struct mm_struct *mm, struct vma_iterator *vmi)
80 {
81 	struct vm_area_struct *vma;
82 	int count = 0;
83 
84 	fail_prealloc = false;
85 	reset_dummy_anon_vma();
86 
87 	vma_iter_set(vmi, 0);
88 	for_each_vma(*vmi, vma) {
89 		detach_free_vma(vma);
90 		count++;
91 	}
92 
93 	mtree_destroy(&mm->mm_mt);
94 	mm->map_count = 0;
95 	return count;
96 }
97 
98 bool vma_write_started(struct vm_area_struct *vma)
99 {
100 	unsigned int seq = vma->vm_lock_seq;
101 
102 	/* We reset after each check. */
103 	vma->vm_lock_seq = 0;
104 
105 	/* The vma_start_write() stub simply increments this value. */
106 	return seq > 0;
107 }
108 
109 void __vma_set_dummy_anon_vma(struct vm_area_struct *vma,
110 		struct anon_vma_chain *avc, struct anon_vma *anon_vma)
111 {
112 	vma->anon_vma = anon_vma;
113 	INIT_LIST_HEAD(&vma->anon_vma_chain);
114 	list_add(&avc->same_vma, &vma->anon_vma_chain);
115 	avc->anon_vma = vma->anon_vma;
116 }
117 
118 void vma_set_dummy_anon_vma(struct vm_area_struct *vma,
119 		struct anon_vma_chain *avc)
120 {
121 	__vma_set_dummy_anon_vma(vma, avc, &dummy_anon_vma);
122 }
123 
124 struct task_struct *get_current(void)
125 {
126 	return &__current;
127 }
128 
129 unsigned long rlimit(unsigned int limit)
130 {
131 	return (unsigned long)-1;
132 }
133