xref: /linux/tools/testing/vma/shared.h (revision 6aacab308a5dfd222b2d23662bbae60c11007cfb)
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 IS_SET(_val, _flags) ((_val & _flags) == _flags)
39 
40 extern bool fail_prealloc;
41 
42 /* Override vma_iter_prealloc() so we can choose to fail it. */
43 #define vma_iter_prealloc(vmi, vma)					\
44 	(fail_prealloc ? -ENOMEM : mas_preallocate(&(vmi)->mas, (vma), GFP_KERNEL))
45 
46 #define CONFIG_DEFAULT_MMAP_MIN_ADDR 65536
47 
48 extern unsigned long mmap_min_addr;
49 extern unsigned long dac_mmap_min_addr;
50 extern unsigned long stack_guard_gap;
51 
52 extern const struct vm_operations_struct vma_dummy_vm_ops;
53 extern struct anon_vma dummy_anon_vma;
54 extern struct task_struct __current;
55 
56 /*
57  * Helper function which provides a wrapper around a merge existing VMA
58  * operation.
59  *
60  * Declared in main.c as uses static VMA function.
61  */
62 struct vm_area_struct *merge_existing(struct vma_merge_struct *vmg);
63 
64 /*
65  * Helper function to allocate a VMA and link it to the tree.
66  *
67  * Declared in main.c as uses static VMA function.
68  */
69 int attach_vma(struct mm_struct *mm, struct vm_area_struct *vma);
70 
71 /* Helper function providing a dummy vm_ops->close() method.*/
72 static inline void dummy_close(struct vm_area_struct *)
73 {
74 }
75 
76 /* Helper function to simply allocate a VMA. */
77 struct vm_area_struct *alloc_vma(struct mm_struct *mm,
78 		unsigned long start, unsigned long end,
79 		pgoff_t pgoff, vm_flags_t vm_flags);
80 
81 /* Helper function to detach and free a VMA. */
82 void detach_free_vma(struct vm_area_struct *vma);
83 
84 /* Helper function to allocate a VMA and link it to the tree. */
85 struct vm_area_struct *alloc_and_link_vma(struct mm_struct *mm,
86 		unsigned long start, unsigned long end,
87 		pgoff_t pgoff, vm_flags_t vm_flags);
88 
89 /*
90  * Helper function to reset the dummy anon_vma to indicate it has not been
91  * duplicated.
92  */
93 void reset_dummy_anon_vma(void);
94 
95 /*
96  * Helper function to remove all VMAs and destroy the maple tree associated with
97  * a virtual address space. Returns a count of VMAs in the tree.
98  */
99 int cleanup_mm(struct mm_struct *mm, struct vma_iterator *vmi);
100 
101 /* Helper function to determine if VMA has had vma_start_write() performed. */
102 bool vma_write_started(struct vm_area_struct *vma);
103 
104 void __vma_set_dummy_anon_vma(struct vm_area_struct *vma,
105 		struct anon_vma_chain *avc, struct anon_vma *anon_vma);
106 
107 /* Provide a simple dummy VMA/anon_vma dummy setup for testing. */
108 void vma_set_dummy_anon_vma(struct vm_area_struct *vma,
109 			    struct anon_vma_chain *avc);
110 
111 /* Helper function to specify a VMA's range. */
112 void vma_set_range(struct vm_area_struct *vma,
113 		   unsigned long start, unsigned long end,
114 		   pgoff_t pgoff);
115