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