xref: /linux/tools/testing/vma/include/custom.h (revision eeccf287a2a517954b57cf9d733b3cf5d47afa34)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 
3 #pragma once
4 
5 /*
6  * Contains declarations that exist in the kernel which have been CUSTOMISED for
7  * testing purposes to faciliate userland VMA testing.
8  */
9 
10 #ifdef CONFIG_MMU
11 extern unsigned long mmap_min_addr;
12 extern unsigned long dac_mmap_min_addr;
13 #else
14 #define mmap_min_addr		0UL
15 #define dac_mmap_min_addr	0UL
16 #endif
17 
18 #define VM_WARN_ON(_expr) (WARN_ON(_expr))
19 #define VM_WARN_ON_ONCE(_expr) (WARN_ON_ONCE(_expr))
20 #define VM_WARN_ON_VMG(_expr, _vmg) (WARN_ON(_expr))
21 #define VM_BUG_ON(_expr) (BUG_ON(_expr))
22 #define VM_BUG_ON_VMA(_expr, _vma) (BUG_ON(_expr))
23 
24 /* We hardcode this for now. */
25 #define sysctl_max_map_count 0x1000000UL
26 
27 #define TASK_SIZE ((1ul << 47)-PAGE_SIZE)
28 
29 /*
30  * The shared stubs do not implement this, it amounts to an fprintf(STDERR,...)
31  * either way :)
32  */
33 #define pr_warn_once pr_err
34 
35 #define pgtable_supports_soft_dirty() 1
36 
37 struct anon_vma {
38 	struct anon_vma *root;
39 	struct rb_root_cached rb_root;
40 
41 	/* Test fields. */
42 	bool was_cloned;
43 	bool was_unlinked;
44 };
45 
unlink_anon_vmas(struct vm_area_struct * vma)46 static inline void unlink_anon_vmas(struct vm_area_struct *vma)
47 {
48 	/* For testing purposes, indicate that the anon_vma was unlinked. */
49 	vma->anon_vma->was_unlinked = true;
50 }
51 
vma_start_write(struct vm_area_struct * vma)52 static inline void vma_start_write(struct vm_area_struct *vma)
53 {
54 	/* Used to indicate to tests that a write operation has begun. */
55 	vma->vm_lock_seq++;
56 }
57 
58 static inline __must_check
vma_start_write_killable(struct vm_area_struct * vma)59 int vma_start_write_killable(struct vm_area_struct *vma)
60 {
61 	/* Used to indicate to tests that a write operation has begun. */
62 	vma->vm_lock_seq++;
63 	return 0;
64 }
65 
anon_vma_clone(struct vm_area_struct * dst,struct vm_area_struct * src,enum vma_operation operation)66 static inline int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src,
67 				 enum vma_operation operation)
68 {
69 	/* For testing purposes. We indicate that an anon_vma has been cloned. */
70 	if (src->anon_vma != NULL) {
71 		dst->anon_vma = src->anon_vma;
72 		dst->anon_vma->was_cloned = true;
73 	}
74 
75 	return 0;
76 }
77 
__anon_vma_prepare(struct vm_area_struct * vma)78 static inline int __anon_vma_prepare(struct vm_area_struct *vma)
79 {
80 	struct anon_vma *anon_vma = calloc(1, sizeof(struct anon_vma));
81 
82 	if (!anon_vma)
83 		return -ENOMEM;
84 
85 	anon_vma->root = anon_vma;
86 	vma->anon_vma = anon_vma;
87 
88 	return 0;
89 }
90 
anon_vma_prepare(struct vm_area_struct * vma)91 static inline int anon_vma_prepare(struct vm_area_struct *vma)
92 {
93 	if (likely(vma->anon_vma))
94 		return 0;
95 
96 	return __anon_vma_prepare(vma);
97 }
98 
vma_lock_init(struct vm_area_struct * vma,bool reset_refcnt)99 static inline void vma_lock_init(struct vm_area_struct *vma, bool reset_refcnt)
100 {
101 	if (reset_refcnt)
102 		refcount_set(&vma->vm_refcnt, 0);
103 }
104 
__mk_vma_flags(size_t count,const vma_flag_t * bits)105 static inline vma_flags_t __mk_vma_flags(size_t count, const vma_flag_t *bits)
106 {
107 	vma_flags_t flags;
108 	int i;
109 
110 	/*
111 	 * For testing purposes: allow invalid bit specification so we can
112 	 * easily test.
113 	 */
114 	vma_flags_clear_all(&flags);
115 	for (i = 0; i < count; i++)
116 		if (bits[i] < NUM_VMA_FLAG_BITS)
117 			vma_flag_set(&flags, bits[i]);
118 	return flags;
119 }
120