xref: /linux/tools/testing/vma/shared.h (revision e2683c8868d03382da7e1ce8453b543a043066d1)
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)						\
25 	do {								\
26 		if (!(_expr)) {						\
27 			fprintf(stderr,					\
28 				"Assert FAILED at %s:%d:%s(): %s is FALSE.\n", \
29 				__FILE__, __LINE__, __FUNCTION__, #_expr); \
30 			return false;					\
31 		}							\
32 	} while (0)
33 
34 #define ASSERT_FALSE(_expr) ASSERT_TRUE(!(_expr))
35 #define ASSERT_EQ(_val1, _val2) ASSERT_TRUE((_val1) == (_val2))
36 #define ASSERT_NE(_val1, _val2) ASSERT_TRUE((_val1) != (_val2))
37 
38 #define ASSERT_FLAGS_SAME_MASK(_flags, _flags_other) \
39 	ASSERT_TRUE(vma_flags_same_mask((_flags), (_flags_other)))
40 
41 #define ASSERT_FLAGS_NOT_SAME_MASK(_flags, _flags_other) \
42 	ASSERT_FALSE(vma_flags_same_mask((_flags), (_flags_other)))
43 
44 #define ASSERT_FLAGS_SAME(_flags, ...) \
45 	ASSERT_TRUE(vma_flags_same(_flags, __VA_ARGS__))
46 
47 #define ASSERT_FLAGS_NOT_SAME(_flags, ...) \
48 	ASSERT_FALSE(vma_flags_same(_flags, __VA_ARGS__))
49 
50 #define ASSERT_FLAGS_EMPTY(_flags) \
51 	ASSERT_TRUE(vma_flags_empty(_flags))
52 
53 #define ASSERT_FLAGS_NONEMPTY(_flags) \
54 	ASSERT_FALSE(vma_flags_empty(_flags))
55 
56 #define IS_SET(_val, _flags) ((_val & _flags) == _flags)
57 
58 extern bool fail_prealloc;
59 
60 /* Override vma_iter_prealloc() so we can choose to fail it. */
61 #define vma_iter_prealloc(vmi, vma)					\
62 	(fail_prealloc ? -ENOMEM : mas_preallocate(&(vmi)->mas, (vma), GFP_KERNEL))
63 
64 #define CONFIG_DEFAULT_MMAP_MIN_ADDR 65536
65 
66 extern unsigned long mmap_min_addr;
67 extern unsigned long dac_mmap_min_addr;
68 extern unsigned long stack_guard_gap;
69 
70 extern const struct vm_operations_struct vma_dummy_vm_ops;
71 extern struct anon_vma dummy_anon_vma;
72 extern struct task_struct __current;
73 
74 /*
75  * Helper function which provides a wrapper around a merge existing VMA
76  * operation.
77  *
78  * Declared in main.c as uses static VMA function.
79  */
80 struct vm_area_struct *merge_existing(struct vma_merge_struct *vmg);
81 
82 /*
83  * Helper function to allocate a VMA and link it to the tree.
84  *
85  * Declared in main.c as uses static VMA function.
86  */
87 int attach_vma(struct mm_struct *mm, struct vm_area_struct *vma);
88 
89 /* Helper function providing a dummy vm_ops->close() method.*/
90 static inline void dummy_close(struct vm_area_struct *)
91 {
92 }
93 
94 /* Helper function to simply allocate a VMA. */
95 struct vm_area_struct *alloc_vma(struct mm_struct *mm,
96 		unsigned long start, unsigned long end,
97 		pgoff_t pgoff, vma_flags_t vma_flags);
98 
99 /* Helper function to detach and free a VMA. */
100 void detach_free_vma(struct vm_area_struct *vma);
101 
102 /* Helper function to allocate a VMA and link it to the tree. */
103 struct vm_area_struct *alloc_and_link_vma(struct mm_struct *mm,
104 		unsigned long start, unsigned long end,
105 		pgoff_t pgoff, vma_flags_t vma_flags);
106 
107 /*
108  * Helper function to reset the dummy anon_vma to indicate it has not been
109  * duplicated.
110  */
111 void reset_dummy_anon_vma(void);
112 
113 /*
114  * Helper function to remove all VMAs and destroy the maple tree associated with
115  * a virtual address space. Returns a count of VMAs in the tree.
116  */
117 int cleanup_mm(struct mm_struct *mm, struct vma_iterator *vmi);
118 
119 /* Helper function to determine if VMA has had vma_start_write() performed. */
120 bool vma_write_started(struct vm_area_struct *vma);
121 
122 void __vma_set_dummy_anon_vma(struct vm_area_struct *vma,
123 		struct anon_vma_chain *avc, struct anon_vma *anon_vma);
124 
125 /* Provide a simple dummy VMA/anon_vma dummy setup for testing. */
126 void vma_set_dummy_anon_vma(struct vm_area_struct *vma,
127 			    struct anon_vma_chain *avc);
128 
129 /* Helper function to specify a VMA's range. */
130 void vma_set_range(struct vm_area_struct *vma,
131 		   unsigned long start, unsigned long end,
132 		   pgoff_t pgoff);
133