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