xref: /linux/tools/testing/vma/shared.h (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #pragma once
4 
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 
9 #include "generated/bit-length.h"
10 #include "maple-shared.h"
11 #include "vma_internal.h"
12 #include "../../../mm/vma.h"
13 
14 /* Simple test runner. Assumes local num_[fail, tests] counters. */
15 #define TEST(name)							\
16 	do {								\
17 		(*num_tests)++;						\
18 		if (!test_##name()) {					\
19 			(*num_fail)++;					\
20 			fprintf(stderr, "Test " #name " FAILED\n");	\
21 		}							\
22 	} while (0)
23 
24 #define __ASSERT_TRUE(_expr, _fmt, ...)					   \
25 	do {								   \
26 		if (!(_expr)) {						   \
27 			fprintf(stderr,					   \
28 				"Assert FAILED at %s:%d:%s(): %s is FALSE" \
29 				_fmt ".\n",				   \
30 				__FILE__, __LINE__, __FUNCTION__, #_expr   \
31 				__VA_OPT__(,) __VA_ARGS__);		   \
32 			return false;					   \
33 		}							   \
34 	} while (0)
35 
36 #define __TO_SCALAR(x)	((unsigned long long)(uintptr_t)(x))
37 
38 #define ASSERT_TRUE(_expr) __ASSERT_TRUE(_expr, "")
39 #define ASSERT_FALSE(_expr) ASSERT_TRUE(!(_expr))
40 #define ASSERT_EQ(_val1, _val2) do {				 \
41 	__typeof__(_val1) __val1 = (_val1);			 \
42 	__typeof__(_val2) __val2 = (_val2);			 \
43 	__ASSERT_TRUE(__val1 == __val2, " (0x%llx != 0x%llx)",	 \
44 		      __TO_SCALAR(__val1), __TO_SCALAR(__val2)); \
45 	} while (0)
46 
47 #define ASSERT_NE(_val1, _val2) do {				 \
48 	__typeof__(_val1) __val1 = (_val1);			 \
49 	__typeof__(_val2) __val2 = (_val2);			 \
50 	__ASSERT_TRUE(__val1 != __val2, " (0x%llx == 0x%llx)",	 \
51 		      __TO_SCALAR(__val1), __TO_SCALAR(__val2)); \
52 	} while (0)
53 
54 #define ASSERT_FLAGS_SAME_MASK(_flags, _flags_other) \
55 	ASSERT_TRUE(vma_flags_same_mask((_flags), (_flags_other)))
56 
57 #define ASSERT_FLAGS_NOT_SAME_MASK(_flags, _flags_other) \
58 	ASSERT_FALSE(vma_flags_same_mask((_flags), (_flags_other)))
59 
60 #define ASSERT_FLAGS_SAME(_flags, ...) \
61 	ASSERT_TRUE(vma_flags_same(_flags, __VA_ARGS__))
62 
63 #define ASSERT_FLAGS_NOT_SAME(_flags, ...) \
64 	ASSERT_FALSE(vma_flags_same(_flags, __VA_ARGS__))
65 
66 #define ASSERT_FLAGS_EMPTY(_flags) \
67 	ASSERT_TRUE(vma_flags_empty(_flags))
68 
69 #define ASSERT_FLAGS_NONEMPTY(_flags) \
70 	ASSERT_FALSE(vma_flags_empty(_flags))
71 
72 extern bool fail_prealloc;
73 
74 /* Override vma_iter_prealloc() so we can choose to fail it. */
75 #define vma_iter_prealloc(vmi, vma)					\
76 	(fail_prealloc ? -ENOMEM : mas_preallocate(&(vmi)->mas, (vma), GFP_KERNEL))
77 
78 #define CONFIG_DEFAULT_MMAP_MIN_ADDR 65536
79 
80 extern unsigned long mmap_min_addr;
81 extern unsigned long dac_mmap_min_addr;
82 extern unsigned long stack_guard_gap;
83 
84 extern const struct vm_operations_struct vma_dummy_vm_ops;
85 extern struct anon_vma dummy_anon_vma;
86 extern struct task_struct __current;
87 
88 /*
89  * Helper function which provides a wrapper around a merge existing VMA
90  * operation.
91  *
92  * Declared in main.c as uses static VMA function.
93  */
94 struct vm_area_struct *merge_existing(struct vma_merge_struct *vmg);
95 
96 /*
97  * Helper function to allocate a VMA and link it to the tree.
98  *
99  * Declared in main.c as uses static VMA function.
100  */
101 int attach_vma(struct mm_struct *mm, struct vm_area_struct *vma);
102 
103 /* Helper function providing a dummy vm_ops->close() method.*/
104 static inline void dummy_close(struct vm_area_struct *)
105 {
106 }
107 
108 /* Helper function to simply allocate a VMA. */
109 struct vm_area_struct *alloc_vma(struct mm_struct *mm,
110 		unsigned long start, unsigned long end,
111 		pgoff_t pgoff, vma_flags_t vma_flags);
112 
113 /* Helper function to detach and free a VMA. */
114 void detach_free_vma(struct vm_area_struct *vma);
115 
116 /* Helper function to allocate a VMA and link it to the tree. */
117 struct vm_area_struct *alloc_and_link_vma(struct mm_struct *mm,
118 		unsigned long start, unsigned long end,
119 		pgoff_t pgoff, vma_flags_t vma_flags);
120 
121 /*
122  * Helper function to reset the dummy anon_vma to indicate it has not been
123  * duplicated.
124  */
125 void reset_dummy_anon_vma(void);
126 
127 /*
128  * Helper function to remove all VMAs and destroy the maple tree associated with
129  * a virtual address space. Returns a count of VMAs in the tree.
130  */
131 int cleanup_mm(struct mm_struct *mm, struct vma_iterator *vmi);
132 
133 /* Helper function to determine if VMA has had vma_start_write() performed. */
134 bool vma_write_started(struct vm_area_struct *vma);
135 
136 void __vma_set_dummy_anon_vma(struct vm_area_struct *vma,
137 		struct anon_vma_chain *avc, struct anon_vma *anon_vma);
138 
139 /* Provide a simple dummy VMA/anon_vma dummy setup for testing. */
140 void vma_set_dummy_anon_vma(struct vm_area_struct *vma,
141 			    struct anon_vma_chain *avc);
142