xref: /linux/tools/testing/vma/include/dup.h (revision eeccf287a2a517954b57cf9d733b3cf5d47afa34)
1a1f0dacaSLorenzo Stoakes /* SPDX-License-Identifier: GPL-2.0+ */
2a1f0dacaSLorenzo Stoakes 
3a1f0dacaSLorenzo Stoakes #pragma once
4a1f0dacaSLorenzo Stoakes 
5a1f0dacaSLorenzo Stoakes /* Forward declarations to avoid header cycle. */
6a1f0dacaSLorenzo Stoakes struct vm_area_struct;
7a1f0dacaSLorenzo Stoakes static inline void vma_start_write(struct vm_area_struct *vma);
8a1f0dacaSLorenzo Stoakes 
9a1f0dacaSLorenzo Stoakes extern const struct vm_operations_struct vma_dummy_vm_ops;
10a1f0dacaSLorenzo Stoakes extern unsigned long stack_guard_gap;
11a1f0dacaSLorenzo Stoakes extern const struct vm_operations_struct vma_dummy_vm_ops;
12a1f0dacaSLorenzo Stoakes extern unsigned long rlimit(unsigned int limit);
13a1f0dacaSLorenzo Stoakes struct task_struct *get_current(void);
14a1f0dacaSLorenzo Stoakes 
15a1f0dacaSLorenzo Stoakes #define MMF_HAS_MDWE	28
16a1f0dacaSLorenzo Stoakes #define current get_current()
17a1f0dacaSLorenzo Stoakes 
18a1f0dacaSLorenzo Stoakes /*
19a1f0dacaSLorenzo Stoakes  * Define the task command name length as enum, then it can be visible to
20a1f0dacaSLorenzo Stoakes  * BPF programs.
21a1f0dacaSLorenzo Stoakes  */
22a1f0dacaSLorenzo Stoakes enum {
23a1f0dacaSLorenzo Stoakes 	TASK_COMM_LEN = 16,
24a1f0dacaSLorenzo Stoakes };
25a1f0dacaSLorenzo Stoakes 
26a1f0dacaSLorenzo Stoakes /* PARTIALLY implemented types. */
27a1f0dacaSLorenzo Stoakes struct mm_struct {
28a1f0dacaSLorenzo Stoakes 	struct maple_tree mm_mt;
29a1f0dacaSLorenzo Stoakes 	int map_count;			/* number of VMAs */
30a1f0dacaSLorenzo Stoakes 	unsigned long total_vm;	   /* Total pages mapped */
31a1f0dacaSLorenzo Stoakes 	unsigned long locked_vm;   /* Pages that have PG_mlocked set */
32a1f0dacaSLorenzo Stoakes 	unsigned long data_vm;	   /* VM_WRITE & ~VM_SHARED & ~VM_STACK */
33a1f0dacaSLorenzo Stoakes 	unsigned long exec_vm;	   /* VM_EXEC & ~VM_WRITE & ~VM_STACK */
34a1f0dacaSLorenzo Stoakes 	unsigned long stack_vm;	   /* VM_STACK */
35a1f0dacaSLorenzo Stoakes 
36a1f0dacaSLorenzo Stoakes 	unsigned long def_flags;
37a1f0dacaSLorenzo Stoakes 
38a1f0dacaSLorenzo Stoakes 	mm_flags_t flags; /* Must use mm_flags_* helpers to access */
39a1f0dacaSLorenzo Stoakes };
40a1f0dacaSLorenzo Stoakes struct address_space {
41a1f0dacaSLorenzo Stoakes 	struct rb_root_cached	i_mmap;
42a1f0dacaSLorenzo Stoakes 	unsigned long		flags;
43a1f0dacaSLorenzo Stoakes 	atomic_t		i_mmap_writable;
44a1f0dacaSLorenzo Stoakes };
45a1f0dacaSLorenzo Stoakes struct file_operations {
46a1f0dacaSLorenzo Stoakes 	int (*mmap)(struct file *, struct vm_area_struct *);
47a1f0dacaSLorenzo Stoakes 	int (*mmap_prepare)(struct vm_area_desc *);
48a1f0dacaSLorenzo Stoakes };
49a1f0dacaSLorenzo Stoakes struct file {
50a1f0dacaSLorenzo Stoakes 	struct address_space	*f_mapping;
51a1f0dacaSLorenzo Stoakes 	const struct file_operations	*f_op;
52a1f0dacaSLorenzo Stoakes };
53a1f0dacaSLorenzo Stoakes struct anon_vma_chain {
54a1f0dacaSLorenzo Stoakes 	struct anon_vma *anon_vma;
55a1f0dacaSLorenzo Stoakes 	struct list_head same_vma;
56a1f0dacaSLorenzo Stoakes };
57a1f0dacaSLorenzo Stoakes struct task_struct {
58a1f0dacaSLorenzo Stoakes 	char comm[TASK_COMM_LEN];
59a1f0dacaSLorenzo Stoakes 	pid_t pid;
60a1f0dacaSLorenzo Stoakes 	struct mm_struct *mm;
61a1f0dacaSLorenzo Stoakes 
62a1f0dacaSLorenzo Stoakes 	/* Used for emulating ABI behavior of previous Linux versions: */
63a1f0dacaSLorenzo Stoakes 	unsigned int			personality;
64a1f0dacaSLorenzo Stoakes };
65a1f0dacaSLorenzo Stoakes 
66a1f0dacaSLorenzo Stoakes struct kref {
67a1f0dacaSLorenzo Stoakes 	refcount_t refcount;
68a1f0dacaSLorenzo Stoakes };
69a1f0dacaSLorenzo Stoakes 
70a1f0dacaSLorenzo Stoakes struct anon_vma_name {
71a1f0dacaSLorenzo Stoakes 	struct kref kref;
72a1f0dacaSLorenzo Stoakes 	/* The name needs to be at the end because it is dynamically sized. */
73a1f0dacaSLorenzo Stoakes 	char name[];
74a1f0dacaSLorenzo Stoakes };
75a1f0dacaSLorenzo Stoakes 
76a1f0dacaSLorenzo Stoakes /*
77a1f0dacaSLorenzo Stoakes  * Contains declarations that are DUPLICATED from kernel source in order to
78a1f0dacaSLorenzo Stoakes  * faciliate userland VMA testing.
79a1f0dacaSLorenzo Stoakes  *
80a1f0dacaSLorenzo Stoakes  * These must be kept in sync with kernel source.
81a1f0dacaSLorenzo Stoakes  */
82a1f0dacaSLorenzo Stoakes 
83a1f0dacaSLorenzo Stoakes #define VMA_LOCK_OFFSET	0x40000000
84a1f0dacaSLorenzo Stoakes 
85a1f0dacaSLorenzo Stoakes typedef struct { unsigned long v; } freeptr_t;
86a1f0dacaSLorenzo Stoakes 
87a1f0dacaSLorenzo Stoakes #define VM_NONE		0x00000000
88a1f0dacaSLorenzo Stoakes 
89a1f0dacaSLorenzo Stoakes typedef int __bitwise vma_flag_t;
90a1f0dacaSLorenzo Stoakes 
91a1f0dacaSLorenzo Stoakes #define ACCESS_PRIVATE(p, member) ((p)->member)
92a1f0dacaSLorenzo Stoakes 
93a1f0dacaSLorenzo Stoakes #define DECLARE_VMA_BIT(name, bitnum) \
94a1f0dacaSLorenzo Stoakes 	VMA_ ## name ## _BIT = ((__force vma_flag_t)bitnum)
95a1f0dacaSLorenzo Stoakes #define DECLARE_VMA_BIT_ALIAS(name, aliased) \
96a1f0dacaSLorenzo Stoakes 	VMA_ ## name ## _BIT = VMA_ ## aliased ## _BIT
97a1f0dacaSLorenzo Stoakes enum {
98a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(READ, 0),
99a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(WRITE, 1),
100a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(EXEC, 2),
101a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(SHARED, 3),
102a1f0dacaSLorenzo Stoakes 	/* mprotect() hardcodes VM_MAYREAD >> 4 == VM_READ, and so for r/w/x bits. */
103a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(MAYREAD, 4),	/* limits for mprotect() etc. */
104a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(MAYWRITE, 5),
105a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(MAYEXEC, 6),
106a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(MAYSHARE, 7),
107a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(GROWSDOWN, 8),	/* general info on the segment */
108a1f0dacaSLorenzo Stoakes #ifdef CONFIG_MMU
109a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(UFFD_MISSING, 9),/* missing pages tracking */
110a1f0dacaSLorenzo Stoakes #else
111a1f0dacaSLorenzo Stoakes 	/* nommu: R/O MAP_PRIVATE mapping that might overlay a file mapping */
112a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(MAYOVERLAY, 9),
113a1f0dacaSLorenzo Stoakes #endif /* CONFIG_MMU */
114a1f0dacaSLorenzo Stoakes 	/* Page-ranges managed without "struct page", just pure PFN */
115a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(PFNMAP, 10),
116a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(MAYBE_GUARD, 11),
117a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(UFFD_WP, 12),	/* wrprotect pages tracking */
118a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(LOCKED, 13),
119a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(IO, 14),	/* Memory mapped I/O or similar */
120a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(SEQ_READ, 15),	/* App will access data sequentially */
121a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(RAND_READ, 16),	/* App will not benefit from clustered reads */
122a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(DONTCOPY, 17),	/* Do not copy this vma on fork */
123a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(DONTEXPAND, 18),/* Cannot expand with mremap() */
124a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(LOCKONFAULT, 19),/* Lock pages covered when faulted in */
125a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(ACCOUNT, 20),	/* Is a VM accounted object */
126a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(NORESERVE, 21),	/* should the VM suppress accounting */
127a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(HUGETLB, 22),	/* Huge TLB Page VM */
128a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(SYNC, 23),	/* Synchronous page faults */
129a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(ARCH_1, 24),	/* Architecture-specific flag */
130a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(WIPEONFORK, 25),/* Wipe VMA contents in child. */
131a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(DONTDUMP, 26),	/* Do not include in the core dump */
132a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(SOFTDIRTY, 27),	/* NOT soft dirty clean area */
133a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(MIXEDMAP, 28),	/* Can contain struct page and pure PFN pages */
134a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(HUGEPAGE, 29),	/* MADV_HUGEPAGE marked this vma */
135a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(NOHUGEPAGE, 30),/* MADV_NOHUGEPAGE marked this vma */
136a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(MERGEABLE, 31),	/* KSM may merge identical pages */
137a1f0dacaSLorenzo Stoakes 	/* These bits are reused, we define specific uses below. */
138a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(HIGH_ARCH_0, 32),
139a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(HIGH_ARCH_1, 33),
140a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(HIGH_ARCH_2, 34),
141a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(HIGH_ARCH_3, 35),
142a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(HIGH_ARCH_4, 36),
143a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(HIGH_ARCH_5, 37),
144a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(HIGH_ARCH_6, 38),
145a1f0dacaSLorenzo Stoakes 	/*
146a1f0dacaSLorenzo Stoakes 	 * This flag is used to connect VFIO to arch specific KVM code. It
147a1f0dacaSLorenzo Stoakes 	 * indicates that the memory under this VMA is safe for use with any
148a1f0dacaSLorenzo Stoakes 	 * non-cachable memory type inside KVM. Some VFIO devices, on some
149a1f0dacaSLorenzo Stoakes 	 * platforms, are thought to be unsafe and can cause machine crashes
150a1f0dacaSLorenzo Stoakes 	 * if KVM does not lock down the memory type.
151a1f0dacaSLorenzo Stoakes 	 */
152a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(ALLOW_ANY_UNCACHED, 39),
153a1f0dacaSLorenzo Stoakes #ifdef CONFIG_PPC32
154a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(DROPPABLE, ARCH_1),
155a1f0dacaSLorenzo Stoakes #else
156a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(DROPPABLE, 40),
157a1f0dacaSLorenzo Stoakes #endif
158a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(UFFD_MINOR, 41),
159a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT(SEALED, 42),
160a1f0dacaSLorenzo Stoakes 	/* Flags that reuse flags above. */
161a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(PKEY_BIT0, HIGH_ARCH_0),
162a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(PKEY_BIT1, HIGH_ARCH_1),
163a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(PKEY_BIT2, HIGH_ARCH_2),
164a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(PKEY_BIT3, HIGH_ARCH_3),
165a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(PKEY_BIT4, HIGH_ARCH_4),
166a1f0dacaSLorenzo Stoakes #if defined(CONFIG_X86_USER_SHADOW_STACK)
167a1f0dacaSLorenzo Stoakes 	/*
168a1f0dacaSLorenzo Stoakes 	 * VM_SHADOW_STACK should not be set with VM_SHARED because of lack of
169a1f0dacaSLorenzo Stoakes 	 * support core mm.
170a1f0dacaSLorenzo Stoakes 	 *
171a1f0dacaSLorenzo Stoakes 	 * These VMAs will get a single end guard page. This helps userspace
172a1f0dacaSLorenzo Stoakes 	 * protect itself from attacks. A single page is enough for current
173a1f0dacaSLorenzo Stoakes 	 * shadow stack archs (x86). See the comments near alloc_shstk() in
174a1f0dacaSLorenzo Stoakes 	 * arch/x86/kernel/shstk.c for more details on the guard size.
175a1f0dacaSLorenzo Stoakes 	 */
176a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(SHADOW_STACK, HIGH_ARCH_5),
177a1f0dacaSLorenzo Stoakes #elif defined(CONFIG_ARM64_GCS)
178a1f0dacaSLorenzo Stoakes 	/*
179a1f0dacaSLorenzo Stoakes 	 * arm64's Guarded Control Stack implements similar functionality and
180a1f0dacaSLorenzo Stoakes 	 * has similar constraints to shadow stacks.
181a1f0dacaSLorenzo Stoakes 	 */
182a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(SHADOW_STACK, HIGH_ARCH_6),
183a1f0dacaSLorenzo Stoakes #endif
184a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(SAO, ARCH_1),		/* Strong Access Ordering (powerpc) */
185a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(GROWSUP, ARCH_1),		/* parisc */
186a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(SPARC_ADI, ARCH_1),	/* sparc64 */
187a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(ARM64_BTI, ARCH_1),	/* arm64 */
188a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(ARCH_CLEAR, ARCH_1),	/* sparc64, arm64 */
189a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(MAPPED_COPY, ARCH_1),	/* !CONFIG_MMU */
190a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(MTE, HIGH_ARCH_4),	/* arm64 */
191a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(MTE_ALLOWED, HIGH_ARCH_5),/* arm64 */
192a1f0dacaSLorenzo Stoakes #ifdef CONFIG_STACK_GROWSUP
193a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(STACK, GROWSUP),
194a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(STACK_EARLY, GROWSDOWN),
195a1f0dacaSLorenzo Stoakes #else
196a1f0dacaSLorenzo Stoakes 	DECLARE_VMA_BIT_ALIAS(STACK, GROWSDOWN),
197a1f0dacaSLorenzo Stoakes #endif
198a1f0dacaSLorenzo Stoakes };
199a1f0dacaSLorenzo Stoakes 
200a1f0dacaSLorenzo Stoakes #define INIT_VM_FLAG(name) BIT((__force int) VMA_ ## name ## _BIT)
201a1f0dacaSLorenzo Stoakes #define VM_READ		INIT_VM_FLAG(READ)
202a1f0dacaSLorenzo Stoakes #define VM_WRITE	INIT_VM_FLAG(WRITE)
203a1f0dacaSLorenzo Stoakes #define VM_EXEC		INIT_VM_FLAG(EXEC)
204a1f0dacaSLorenzo Stoakes #define VM_SHARED	INIT_VM_FLAG(SHARED)
205a1f0dacaSLorenzo Stoakes #define VM_MAYREAD	INIT_VM_FLAG(MAYREAD)
206a1f0dacaSLorenzo Stoakes #define VM_MAYWRITE	INIT_VM_FLAG(MAYWRITE)
207a1f0dacaSLorenzo Stoakes #define VM_MAYEXEC	INIT_VM_FLAG(MAYEXEC)
208a1f0dacaSLorenzo Stoakes #define VM_MAYSHARE	INIT_VM_FLAG(MAYSHARE)
209a1f0dacaSLorenzo Stoakes #define VM_GROWSDOWN	INIT_VM_FLAG(GROWSDOWN)
210a1f0dacaSLorenzo Stoakes #ifdef CONFIG_MMU
211a1f0dacaSLorenzo Stoakes #define VM_UFFD_MISSING	INIT_VM_FLAG(UFFD_MISSING)
212a1f0dacaSLorenzo Stoakes #else
213a1f0dacaSLorenzo Stoakes #define VM_UFFD_MISSING	VM_NONE
214a1f0dacaSLorenzo Stoakes #define VM_MAYOVERLAY	INIT_VM_FLAG(MAYOVERLAY)
215a1f0dacaSLorenzo Stoakes #endif
216a1f0dacaSLorenzo Stoakes #define VM_PFNMAP	INIT_VM_FLAG(PFNMAP)
217a1f0dacaSLorenzo Stoakes #define VM_MAYBE_GUARD	INIT_VM_FLAG(MAYBE_GUARD)
218a1f0dacaSLorenzo Stoakes #define VM_UFFD_WP	INIT_VM_FLAG(UFFD_WP)
219a1f0dacaSLorenzo Stoakes #define VM_LOCKED	INIT_VM_FLAG(LOCKED)
220a1f0dacaSLorenzo Stoakes #define VM_IO		INIT_VM_FLAG(IO)
221a1f0dacaSLorenzo Stoakes #define VM_SEQ_READ	INIT_VM_FLAG(SEQ_READ)
222a1f0dacaSLorenzo Stoakes #define VM_RAND_READ	INIT_VM_FLAG(RAND_READ)
223a1f0dacaSLorenzo Stoakes #define VM_DONTCOPY	INIT_VM_FLAG(DONTCOPY)
224a1f0dacaSLorenzo Stoakes #define VM_DONTEXPAND	INIT_VM_FLAG(DONTEXPAND)
225a1f0dacaSLorenzo Stoakes #define VM_LOCKONFAULT	INIT_VM_FLAG(LOCKONFAULT)
226a1f0dacaSLorenzo Stoakes #define VM_ACCOUNT	INIT_VM_FLAG(ACCOUNT)
227a1f0dacaSLorenzo Stoakes #define VM_NORESERVE	INIT_VM_FLAG(NORESERVE)
228a1f0dacaSLorenzo Stoakes #define VM_HUGETLB	INIT_VM_FLAG(HUGETLB)
229a1f0dacaSLorenzo Stoakes #define VM_SYNC		INIT_VM_FLAG(SYNC)
230a1f0dacaSLorenzo Stoakes #define VM_ARCH_1	INIT_VM_FLAG(ARCH_1)
231a1f0dacaSLorenzo Stoakes #define VM_WIPEONFORK	INIT_VM_FLAG(WIPEONFORK)
232a1f0dacaSLorenzo Stoakes #define VM_DONTDUMP	INIT_VM_FLAG(DONTDUMP)
233a1f0dacaSLorenzo Stoakes #ifdef CONFIG_MEM_SOFT_DIRTY
234a1f0dacaSLorenzo Stoakes #define VM_SOFTDIRTY	INIT_VM_FLAG(SOFTDIRTY)
235a1f0dacaSLorenzo Stoakes #else
236a1f0dacaSLorenzo Stoakes #define VM_SOFTDIRTY	VM_NONE
237a1f0dacaSLorenzo Stoakes #endif
238a1f0dacaSLorenzo Stoakes #define VM_MIXEDMAP	INIT_VM_FLAG(MIXEDMAP)
239a1f0dacaSLorenzo Stoakes #define VM_HUGEPAGE	INIT_VM_FLAG(HUGEPAGE)
240a1f0dacaSLorenzo Stoakes #define VM_NOHUGEPAGE	INIT_VM_FLAG(NOHUGEPAGE)
241a1f0dacaSLorenzo Stoakes #define VM_MERGEABLE	INIT_VM_FLAG(MERGEABLE)
242a1f0dacaSLorenzo Stoakes #define VM_STACK	INIT_VM_FLAG(STACK)
243a1f0dacaSLorenzo Stoakes #ifdef CONFIG_STACK_GROWS_UP
244a1f0dacaSLorenzo Stoakes #define VM_STACK_EARLY	INIT_VM_FLAG(STACK_EARLY)
245a1f0dacaSLorenzo Stoakes #else
246a1f0dacaSLorenzo Stoakes #define VM_STACK_EARLY	VM_NONE
247a1f0dacaSLorenzo Stoakes #endif
248a1f0dacaSLorenzo Stoakes #ifdef CONFIG_ARCH_HAS_PKEYS
249a1f0dacaSLorenzo Stoakes #define VM_PKEY_SHIFT ((__force int)VMA_HIGH_ARCH_0_BIT)
250a1f0dacaSLorenzo Stoakes /* Despite the naming, these are FLAGS not bits. */
251a1f0dacaSLorenzo Stoakes #define VM_PKEY_BIT0 INIT_VM_FLAG(PKEY_BIT0)
252a1f0dacaSLorenzo Stoakes #define VM_PKEY_BIT1 INIT_VM_FLAG(PKEY_BIT1)
253a1f0dacaSLorenzo Stoakes #define VM_PKEY_BIT2 INIT_VM_FLAG(PKEY_BIT2)
254a1f0dacaSLorenzo Stoakes #if CONFIG_ARCH_PKEY_BITS > 3
255a1f0dacaSLorenzo Stoakes #define VM_PKEY_BIT3 INIT_VM_FLAG(PKEY_BIT3)
256a1f0dacaSLorenzo Stoakes #else
257a1f0dacaSLorenzo Stoakes #define VM_PKEY_BIT3  VM_NONE
258a1f0dacaSLorenzo Stoakes #endif /* CONFIG_ARCH_PKEY_BITS > 3 */
259a1f0dacaSLorenzo Stoakes #if CONFIG_ARCH_PKEY_BITS > 4
260a1f0dacaSLorenzo Stoakes #define VM_PKEY_BIT4 INIT_VM_FLAG(PKEY_BIT4)
261a1f0dacaSLorenzo Stoakes #else
262a1f0dacaSLorenzo Stoakes #define VM_PKEY_BIT4  VM_NONE
263a1f0dacaSLorenzo Stoakes #endif /* CONFIG_ARCH_PKEY_BITS > 4 */
264a1f0dacaSLorenzo Stoakes #endif /* CONFIG_ARCH_HAS_PKEYS */
265a1f0dacaSLorenzo Stoakes #if defined(CONFIG_X86_USER_SHADOW_STACK) || defined(CONFIG_ARM64_GCS)
266a1f0dacaSLorenzo Stoakes #define VM_SHADOW_STACK	INIT_VM_FLAG(SHADOW_STACK)
267a1f0dacaSLorenzo Stoakes #else
268a1f0dacaSLorenzo Stoakes #define VM_SHADOW_STACK	VM_NONE
269a1f0dacaSLorenzo Stoakes #endif
270a1f0dacaSLorenzo Stoakes #if defined(CONFIG_PPC64)
271a1f0dacaSLorenzo Stoakes #define VM_SAO		INIT_VM_FLAG(SAO)
272a1f0dacaSLorenzo Stoakes #elif defined(CONFIG_PARISC)
273a1f0dacaSLorenzo Stoakes #define VM_GROWSUP	INIT_VM_FLAG(GROWSUP)
274a1f0dacaSLorenzo Stoakes #elif defined(CONFIG_SPARC64)
275a1f0dacaSLorenzo Stoakes #define VM_SPARC_ADI	INIT_VM_FLAG(SPARC_ADI)
276a1f0dacaSLorenzo Stoakes #define VM_ARCH_CLEAR	INIT_VM_FLAG(ARCH_CLEAR)
277a1f0dacaSLorenzo Stoakes #elif defined(CONFIG_ARM64)
278a1f0dacaSLorenzo Stoakes #define VM_ARM64_BTI	INIT_VM_FLAG(ARM64_BTI)
279a1f0dacaSLorenzo Stoakes #define VM_ARCH_CLEAR	INIT_VM_FLAG(ARCH_CLEAR)
280a1f0dacaSLorenzo Stoakes #elif !defined(CONFIG_MMU)
281a1f0dacaSLorenzo Stoakes #define VM_MAPPED_COPY	INIT_VM_FLAG(MAPPED_COPY)
282a1f0dacaSLorenzo Stoakes #endif
283a1f0dacaSLorenzo Stoakes #ifndef VM_GROWSUP
284a1f0dacaSLorenzo Stoakes #define VM_GROWSUP	VM_NONE
285a1f0dacaSLorenzo Stoakes #endif
286a1f0dacaSLorenzo Stoakes #ifdef CONFIG_ARM64_MTE
287a1f0dacaSLorenzo Stoakes #define VM_MTE		INIT_VM_FLAG(MTE)
288a1f0dacaSLorenzo Stoakes #define VM_MTE_ALLOWED	INIT_VM_FLAG(MTE_ALLOWED)
289a1f0dacaSLorenzo Stoakes #else
290a1f0dacaSLorenzo Stoakes #define VM_MTE		VM_NONE
291a1f0dacaSLorenzo Stoakes #define VM_MTE_ALLOWED	VM_NONE
292a1f0dacaSLorenzo Stoakes #endif
293a1f0dacaSLorenzo Stoakes #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR
294a1f0dacaSLorenzo Stoakes #define VM_UFFD_MINOR	INIT_VM_FLAG(UFFD_MINOR)
295a1f0dacaSLorenzo Stoakes #else
296a1f0dacaSLorenzo Stoakes #define VM_UFFD_MINOR	VM_NONE
297a1f0dacaSLorenzo Stoakes #endif
298a1f0dacaSLorenzo Stoakes #ifdef CONFIG_64BIT
299a1f0dacaSLorenzo Stoakes #define VM_ALLOW_ANY_UNCACHED	INIT_VM_FLAG(ALLOW_ANY_UNCACHED)
300a1f0dacaSLorenzo Stoakes #define VM_SEALED		INIT_VM_FLAG(SEALED)
301a1f0dacaSLorenzo Stoakes #else
302a1f0dacaSLorenzo Stoakes #define VM_ALLOW_ANY_UNCACHED	VM_NONE
303a1f0dacaSLorenzo Stoakes #define VM_SEALED		VM_NONE
304a1f0dacaSLorenzo Stoakes #endif
305a1f0dacaSLorenzo Stoakes #if defined(CONFIG_64BIT) || defined(CONFIG_PPC32)
306a1f0dacaSLorenzo Stoakes #define VM_DROPPABLE		INIT_VM_FLAG(DROPPABLE)
307a1f0dacaSLorenzo Stoakes #else
308a1f0dacaSLorenzo Stoakes #define VM_DROPPABLE		VM_NONE
309a1f0dacaSLorenzo Stoakes #endif
310a1f0dacaSLorenzo Stoakes 
311a1f0dacaSLorenzo Stoakes /* Bits set in the VMA until the stack is in its final location */
312a1f0dacaSLorenzo Stoakes #define VM_STACK_INCOMPLETE_SETUP (VM_RAND_READ | VM_SEQ_READ | VM_STACK_EARLY)
313a1f0dacaSLorenzo Stoakes 
314a1f0dacaSLorenzo Stoakes #define TASK_EXEC ((current->personality & READ_IMPLIES_EXEC) ? VM_EXEC : 0)
315a1f0dacaSLorenzo Stoakes 
316a1f0dacaSLorenzo Stoakes /* Common data flag combinations */
317a1f0dacaSLorenzo Stoakes #define VM_DATA_FLAGS_TSK_EXEC	(VM_READ | VM_WRITE | TASK_EXEC | \
318a1f0dacaSLorenzo Stoakes 				 VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
319a1f0dacaSLorenzo Stoakes #define VM_DATA_FLAGS_NON_EXEC	(VM_READ | VM_WRITE | VM_MAYREAD | \
320a1f0dacaSLorenzo Stoakes 				 VM_MAYWRITE | VM_MAYEXEC)
321a1f0dacaSLorenzo Stoakes #define VM_DATA_FLAGS_EXEC	(VM_READ | VM_WRITE | VM_EXEC | \
322a1f0dacaSLorenzo Stoakes 				 VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
323a1f0dacaSLorenzo Stoakes 
324a1f0dacaSLorenzo Stoakes #ifndef VM_DATA_DEFAULT_FLAGS		/* arch can override this */
325a1f0dacaSLorenzo Stoakes #define VM_DATA_DEFAULT_FLAGS  VM_DATA_FLAGS_EXEC
326a1f0dacaSLorenzo Stoakes #endif
327a1f0dacaSLorenzo Stoakes 
328a1f0dacaSLorenzo Stoakes #ifndef VM_STACK_DEFAULT_FLAGS		/* arch can override this */
329a1f0dacaSLorenzo Stoakes #define VM_STACK_DEFAULT_FLAGS VM_DATA_DEFAULT_FLAGS
330a1f0dacaSLorenzo Stoakes #endif
331a1f0dacaSLorenzo Stoakes 
332a1f0dacaSLorenzo Stoakes #define VM_STARTGAP_FLAGS (VM_GROWSDOWN | VM_SHADOW_STACK)
333a1f0dacaSLorenzo Stoakes 
334a1f0dacaSLorenzo Stoakes #define VM_STACK_FLAGS	(VM_STACK | VM_STACK_DEFAULT_FLAGS | VM_ACCOUNT)
335a1f0dacaSLorenzo Stoakes 
336a1f0dacaSLorenzo Stoakes /* VMA basic access permission flags */
337a1f0dacaSLorenzo Stoakes #define VM_ACCESS_FLAGS (VM_READ | VM_WRITE | VM_EXEC)
338a1f0dacaSLorenzo Stoakes 
339a1f0dacaSLorenzo Stoakes /*
340a1f0dacaSLorenzo Stoakes  * Special vmas that are non-mergable, non-mlock()able.
341a1f0dacaSLorenzo Stoakes  */
342a1f0dacaSLorenzo Stoakes #define VM_SPECIAL (VM_IO | VM_DONTEXPAND | VM_PFNMAP | VM_MIXEDMAP)
343a1f0dacaSLorenzo Stoakes 
344a1f0dacaSLorenzo Stoakes #define DEFAULT_MAP_WINDOW	((1UL << 47) - PAGE_SIZE)
345a1f0dacaSLorenzo Stoakes #define TASK_SIZE_LOW		DEFAULT_MAP_WINDOW
346a1f0dacaSLorenzo Stoakes #define TASK_SIZE_MAX		DEFAULT_MAP_WINDOW
347a1f0dacaSLorenzo Stoakes #define STACK_TOP		TASK_SIZE_LOW
348a1f0dacaSLorenzo Stoakes #define STACK_TOP_MAX		TASK_SIZE_MAX
349a1f0dacaSLorenzo Stoakes 
350a1f0dacaSLorenzo Stoakes /* This mask represents all the VMA flag bits used by mlock */
351a1f0dacaSLorenzo Stoakes #define VM_LOCKED_MASK	(VM_LOCKED | VM_LOCKONFAULT)
352a1f0dacaSLorenzo Stoakes 
353a1f0dacaSLorenzo Stoakes #define TASK_EXEC ((current->personality & READ_IMPLIES_EXEC) ? VM_EXEC : 0)
354a1f0dacaSLorenzo Stoakes 
355a1f0dacaSLorenzo Stoakes #define VM_DATA_FLAGS_TSK_EXEC	(VM_READ | VM_WRITE | TASK_EXEC | \
356a1f0dacaSLorenzo Stoakes 				 VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
357a1f0dacaSLorenzo Stoakes 
358a1f0dacaSLorenzo Stoakes #define RLIMIT_STACK		3	/* max stack size */
359a1f0dacaSLorenzo Stoakes #define RLIMIT_MEMLOCK		8	/* max locked-in-memory address space */
360a1f0dacaSLorenzo Stoakes 
361a1f0dacaSLorenzo Stoakes #define CAP_IPC_LOCK         14
362a1f0dacaSLorenzo Stoakes 
363a1f0dacaSLorenzo Stoakes #define VM_STICKY (VM_SOFTDIRTY | VM_MAYBE_GUARD)
364a1f0dacaSLorenzo Stoakes 
365a1f0dacaSLorenzo Stoakes #define VM_IGNORE_MERGE VM_STICKY
366a1f0dacaSLorenzo Stoakes 
367a1f0dacaSLorenzo Stoakes #define VM_COPY_ON_FORK (VM_PFNMAP | VM_MIXEDMAP | VM_UFFD_WP | VM_MAYBE_GUARD)
368a1f0dacaSLorenzo Stoakes 
369a1f0dacaSLorenzo Stoakes #define pgprot_val(x)		((x).pgprot)
370a1f0dacaSLorenzo Stoakes #define __pgprot(x)		((pgprot_t) { (x) } )
371a1f0dacaSLorenzo Stoakes 
372a1f0dacaSLorenzo Stoakes #define for_each_vma(__vmi, __vma)					\
373a1f0dacaSLorenzo Stoakes 	while (((__vma) = vma_next(&(__vmi))) != NULL)
374a1f0dacaSLorenzo Stoakes 
375a1f0dacaSLorenzo Stoakes /* The MM code likes to work with exclusive end addresses */
376a1f0dacaSLorenzo Stoakes #define for_each_vma_range(__vmi, __vma, __end)				\
377a1f0dacaSLorenzo Stoakes 	while (((__vma) = vma_find(&(__vmi), (__end))) != NULL)
378a1f0dacaSLorenzo Stoakes 
379a1f0dacaSLorenzo Stoakes #define offset_in_page(p)	((unsigned long)(p) & ~PAGE_MASK)
380a1f0dacaSLorenzo Stoakes 
381a1f0dacaSLorenzo Stoakes #define PHYS_PFN(x)	((unsigned long)((x) >> PAGE_SHIFT))
382a1f0dacaSLorenzo Stoakes 
383a1f0dacaSLorenzo Stoakes #define test_and_set_bit(nr, addr) __test_and_set_bit(nr, addr)
384a1f0dacaSLorenzo Stoakes #define test_and_clear_bit(nr, addr) __test_and_clear_bit(nr, addr)
385a1f0dacaSLorenzo Stoakes 
386a1f0dacaSLorenzo Stoakes #define AS_MM_ALL_LOCKS 2
387a1f0dacaSLorenzo Stoakes 
388a1f0dacaSLorenzo Stoakes #define swap(a, b) \
389a1f0dacaSLorenzo Stoakes 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
390a1f0dacaSLorenzo Stoakes 
391a1f0dacaSLorenzo Stoakes /*
392a1f0dacaSLorenzo Stoakes  * Flags for bug emulation.
393a1f0dacaSLorenzo Stoakes  *
394a1f0dacaSLorenzo Stoakes  * These occupy the top three bytes.
395a1f0dacaSLorenzo Stoakes  */
396a1f0dacaSLorenzo Stoakes enum {
397a1f0dacaSLorenzo Stoakes 	READ_IMPLIES_EXEC =	0x0400000,
398a1f0dacaSLorenzo Stoakes };
399a1f0dacaSLorenzo Stoakes 
400a1f0dacaSLorenzo Stoakes struct vma_iterator {
401a1f0dacaSLorenzo Stoakes 	struct ma_state mas;
402a1f0dacaSLorenzo Stoakes };
403a1f0dacaSLorenzo Stoakes 
404a1f0dacaSLorenzo Stoakes #define VMA_ITERATOR(name, __mm, __addr)				\
405a1f0dacaSLorenzo Stoakes 	struct vma_iterator name = {					\
406a1f0dacaSLorenzo Stoakes 		.mas = {						\
407a1f0dacaSLorenzo Stoakes 			.tree = &(__mm)->mm_mt,				\
408a1f0dacaSLorenzo Stoakes 			.index = __addr,				\
409a1f0dacaSLorenzo Stoakes 			.node = NULL,					\
410a1f0dacaSLorenzo Stoakes 			.status = ma_start,				\
411a1f0dacaSLorenzo Stoakes 		},							\
412a1f0dacaSLorenzo Stoakes 	}
413a1f0dacaSLorenzo Stoakes 
414a1f0dacaSLorenzo Stoakes #define DEFINE_MUTEX(mutexname) \
415a1f0dacaSLorenzo Stoakes 	struct mutex mutexname = {}
416a1f0dacaSLorenzo Stoakes 
417a1f0dacaSLorenzo Stoakes #define DECLARE_BITMAP(name, bits) \
418a1f0dacaSLorenzo Stoakes 	unsigned long name[BITS_TO_LONGS(bits)]
419a1f0dacaSLorenzo Stoakes 
420a1f0dacaSLorenzo Stoakes #define EMPTY_VMA_FLAGS ((vma_flags_t){ })
421a1f0dacaSLorenzo Stoakes 
422a1f0dacaSLorenzo Stoakes /* What action should be taken after an .mmap_prepare call is complete? */
423a1f0dacaSLorenzo Stoakes enum mmap_action_type {
424a1f0dacaSLorenzo Stoakes 	MMAP_NOTHING,		/* Mapping is complete, no further action. */
425a1f0dacaSLorenzo Stoakes 	MMAP_REMAP_PFN,		/* Remap PFN range. */
426a1f0dacaSLorenzo Stoakes 	MMAP_IO_REMAP_PFN,	/* I/O remap PFN range. */
427a1f0dacaSLorenzo Stoakes };
428a1f0dacaSLorenzo Stoakes 
429a1f0dacaSLorenzo Stoakes /*
430a1f0dacaSLorenzo Stoakes  * Describes an action an mmap_prepare hook can instruct to be taken to complete
431a1f0dacaSLorenzo Stoakes  * the mapping of a VMA. Specified in vm_area_desc.
432a1f0dacaSLorenzo Stoakes  */
433a1f0dacaSLorenzo Stoakes struct mmap_action {
434a1f0dacaSLorenzo Stoakes 	union {
435a1f0dacaSLorenzo Stoakes 		/* Remap range. */
436a1f0dacaSLorenzo Stoakes 		struct {
437a1f0dacaSLorenzo Stoakes 			unsigned long start;
438a1f0dacaSLorenzo Stoakes 			unsigned long start_pfn;
439a1f0dacaSLorenzo Stoakes 			unsigned long size;
440a1f0dacaSLorenzo Stoakes 			pgprot_t pgprot;
441a1f0dacaSLorenzo Stoakes 		} remap;
442a1f0dacaSLorenzo Stoakes 	};
443a1f0dacaSLorenzo Stoakes 	enum mmap_action_type type;
444a1f0dacaSLorenzo Stoakes 
445a1f0dacaSLorenzo Stoakes 	/*
446a1f0dacaSLorenzo Stoakes 	 * If specified, this hook is invoked after the selected action has been
447a1f0dacaSLorenzo Stoakes 	 * successfully completed. Note that the VMA write lock still held.
448a1f0dacaSLorenzo Stoakes 	 *
449a1f0dacaSLorenzo Stoakes 	 * The absolute minimum ought to be done here.
450a1f0dacaSLorenzo Stoakes 	 *
451a1f0dacaSLorenzo Stoakes 	 * Returns 0 on success, or an error code.
452a1f0dacaSLorenzo Stoakes 	 */
453a1f0dacaSLorenzo Stoakes 	int (*success_hook)(const struct vm_area_struct *vma);
454a1f0dacaSLorenzo Stoakes 
455a1f0dacaSLorenzo Stoakes 	/*
456a1f0dacaSLorenzo Stoakes 	 * If specified, this hook is invoked when an error occurred when
457a1f0dacaSLorenzo Stoakes 	 * attempting the selection action.
458a1f0dacaSLorenzo Stoakes 	 *
459a1f0dacaSLorenzo Stoakes 	 * The hook can return an error code in order to filter the error, but
460a1f0dacaSLorenzo Stoakes 	 * it is not valid to clear the error here.
461a1f0dacaSLorenzo Stoakes 	 */
462a1f0dacaSLorenzo Stoakes 	int (*error_hook)(int err);
463a1f0dacaSLorenzo Stoakes 
464a1f0dacaSLorenzo Stoakes 	/*
465a1f0dacaSLorenzo Stoakes 	 * This should be set in rare instances where the operation required
466a1f0dacaSLorenzo Stoakes 	 * that the rmap should not be able to access the VMA until
467a1f0dacaSLorenzo Stoakes 	 * completely set up.
468a1f0dacaSLorenzo Stoakes 	 */
469a1f0dacaSLorenzo Stoakes 	bool hide_from_rmap_until_complete :1;
470a1f0dacaSLorenzo Stoakes };
471a1f0dacaSLorenzo Stoakes 
472a1f0dacaSLorenzo Stoakes /* Operations which modify VMAs. */
473a1f0dacaSLorenzo Stoakes enum vma_operation {
474a1f0dacaSLorenzo Stoakes 	VMA_OP_SPLIT,
475a1f0dacaSLorenzo Stoakes 	VMA_OP_MERGE_UNFAULTED,
476a1f0dacaSLorenzo Stoakes 	VMA_OP_REMAP,
477a1f0dacaSLorenzo Stoakes 	VMA_OP_FORK,
478a1f0dacaSLorenzo Stoakes };
479a1f0dacaSLorenzo Stoakes 
480a1f0dacaSLorenzo Stoakes /*
481a1f0dacaSLorenzo Stoakes  * Describes a VMA that is about to be mmap()'ed. Drivers may choose to
482a1f0dacaSLorenzo Stoakes  * manipulate mutable fields which will cause those fields to be updated in the
483a1f0dacaSLorenzo Stoakes  * resultant VMA.
484a1f0dacaSLorenzo Stoakes  *
485a1f0dacaSLorenzo Stoakes  * Helper functions are not required for manipulating any field.
486a1f0dacaSLorenzo Stoakes  */
487a1f0dacaSLorenzo Stoakes struct vm_area_desc {
488a1f0dacaSLorenzo Stoakes 	/* Immutable state. */
489a1f0dacaSLorenzo Stoakes 	const struct mm_struct *const mm;
490a1f0dacaSLorenzo Stoakes 	struct file *const file; /* May vary from vm_file in stacked callers. */
491a1f0dacaSLorenzo Stoakes 	unsigned long start;
492a1f0dacaSLorenzo Stoakes 	unsigned long end;
493a1f0dacaSLorenzo Stoakes 
494a1f0dacaSLorenzo Stoakes 	/* Mutable fields. Populated with initial state. */
495a1f0dacaSLorenzo Stoakes 	pgoff_t pgoff;
496a1f0dacaSLorenzo Stoakes 	struct file *vm_file;
497a1f0dacaSLorenzo Stoakes 	union {
498a1f0dacaSLorenzo Stoakes 		vm_flags_t vm_flags;
499a1f0dacaSLorenzo Stoakes 		vma_flags_t vma_flags;
500a1f0dacaSLorenzo Stoakes 	};
501a1f0dacaSLorenzo Stoakes 	pgprot_t page_prot;
502a1f0dacaSLorenzo Stoakes 
503a1f0dacaSLorenzo Stoakes 	/* Write-only fields. */
504a1f0dacaSLorenzo Stoakes 	const struct vm_operations_struct *vm_ops;
505a1f0dacaSLorenzo Stoakes 	void *private_data;
506a1f0dacaSLorenzo Stoakes 
507a1f0dacaSLorenzo Stoakes 	/* Take further action? */
508a1f0dacaSLorenzo Stoakes 	struct mmap_action action;
509a1f0dacaSLorenzo Stoakes };
510a1f0dacaSLorenzo Stoakes 
511a1f0dacaSLorenzo Stoakes struct vm_area_struct {
512a1f0dacaSLorenzo Stoakes 	/* The first cache line has the info for VMA tree walking. */
513a1f0dacaSLorenzo Stoakes 
514a1f0dacaSLorenzo Stoakes 	union {
515a1f0dacaSLorenzo Stoakes 		struct {
516a1f0dacaSLorenzo Stoakes 			/* VMA covers [vm_start; vm_end) addresses within mm */
517a1f0dacaSLorenzo Stoakes 			unsigned long vm_start;
518a1f0dacaSLorenzo Stoakes 			unsigned long vm_end;
519a1f0dacaSLorenzo Stoakes 		};
520a1f0dacaSLorenzo Stoakes 		freeptr_t vm_freeptr; /* Pointer used by SLAB_TYPESAFE_BY_RCU */
521a1f0dacaSLorenzo Stoakes 	};
522a1f0dacaSLorenzo Stoakes 
523a1f0dacaSLorenzo Stoakes 	struct mm_struct *vm_mm;	/* The address space we belong to. */
524a1f0dacaSLorenzo Stoakes 	pgprot_t vm_page_prot;          /* Access permissions of this VMA. */
525a1f0dacaSLorenzo Stoakes 
526a1f0dacaSLorenzo Stoakes 	/*
527a1f0dacaSLorenzo Stoakes 	 * Flags, see mm.h.
528a1f0dacaSLorenzo Stoakes 	 * To modify use vm_flags_{init|reset|set|clear|mod} functions.
529a1f0dacaSLorenzo Stoakes 	 */
530a1f0dacaSLorenzo Stoakes 	union {
531a1f0dacaSLorenzo Stoakes 		const vm_flags_t vm_flags;
532a1f0dacaSLorenzo Stoakes 		vma_flags_t flags;
533a1f0dacaSLorenzo Stoakes 	};
534a1f0dacaSLorenzo Stoakes 
535a1f0dacaSLorenzo Stoakes #ifdef CONFIG_PER_VMA_LOCK
536a1f0dacaSLorenzo Stoakes 	/*
537a1f0dacaSLorenzo Stoakes 	 * Can only be written (using WRITE_ONCE()) while holding both:
538a1f0dacaSLorenzo Stoakes 	 *  - mmap_lock (in write mode)
539a1f0dacaSLorenzo Stoakes 	 *  - vm_refcnt bit at VMA_LOCK_OFFSET is set
540a1f0dacaSLorenzo Stoakes 	 * Can be read reliably while holding one of:
541a1f0dacaSLorenzo Stoakes 	 *  - mmap_lock (in read or write mode)
542a1f0dacaSLorenzo Stoakes 	 *  - vm_refcnt bit at VMA_LOCK_OFFSET is set or vm_refcnt > 1
543a1f0dacaSLorenzo Stoakes 	 * Can be read unreliably (using READ_ONCE()) for pessimistic bailout
544a1f0dacaSLorenzo Stoakes 	 * while holding nothing (except RCU to keep the VMA struct allocated).
545a1f0dacaSLorenzo Stoakes 	 *
546a1f0dacaSLorenzo Stoakes 	 * This sequence counter is explicitly allowed to overflow; sequence
547a1f0dacaSLorenzo Stoakes 	 * counter reuse can only lead to occasional unnecessary use of the
548a1f0dacaSLorenzo Stoakes 	 * slowpath.
549a1f0dacaSLorenzo Stoakes 	 */
550a1f0dacaSLorenzo Stoakes 	unsigned int vm_lock_seq;
551a1f0dacaSLorenzo Stoakes #endif
552a1f0dacaSLorenzo Stoakes 
553a1f0dacaSLorenzo Stoakes 	/*
554a1f0dacaSLorenzo Stoakes 	 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
555a1f0dacaSLorenzo Stoakes 	 * list, after a COW of one of the file pages.	A MAP_SHARED vma
556a1f0dacaSLorenzo Stoakes 	 * can only be in the i_mmap tree.  An anonymous MAP_PRIVATE, stack
557a1f0dacaSLorenzo Stoakes 	 * or brk vma (with NULL file) can only be in an anon_vma list.
558a1f0dacaSLorenzo Stoakes 	 */
559a1f0dacaSLorenzo Stoakes 	struct list_head anon_vma_chain; /* Serialized by mmap_lock &
560a1f0dacaSLorenzo Stoakes 					  * page_table_lock */
561a1f0dacaSLorenzo Stoakes 	struct anon_vma *anon_vma;	/* Serialized by page_table_lock */
562a1f0dacaSLorenzo Stoakes 
563a1f0dacaSLorenzo Stoakes 	/* Function pointers to deal with this struct. */
564a1f0dacaSLorenzo Stoakes 	const struct vm_operations_struct *vm_ops;
565a1f0dacaSLorenzo Stoakes 
566a1f0dacaSLorenzo Stoakes 	/* Information about our backing store: */
567a1f0dacaSLorenzo Stoakes 	unsigned long vm_pgoff;		/* Offset (within vm_file) in PAGE_SIZE
568a1f0dacaSLorenzo Stoakes 					   units */
569a1f0dacaSLorenzo Stoakes 	struct file * vm_file;		/* File we map to (can be NULL). */
570a1f0dacaSLorenzo Stoakes 	void * vm_private_data;		/* was vm_pte (shared mem) */
571a1f0dacaSLorenzo Stoakes 
572a1f0dacaSLorenzo Stoakes #ifdef CONFIG_SWAP
573a1f0dacaSLorenzo Stoakes 	atomic_long_t swap_readahead_info;
574a1f0dacaSLorenzo Stoakes #endif
575a1f0dacaSLorenzo Stoakes #ifndef CONFIG_MMU
576a1f0dacaSLorenzo Stoakes 	struct vm_region *vm_region;	/* NOMMU mapping region */
577a1f0dacaSLorenzo Stoakes #endif
578a1f0dacaSLorenzo Stoakes #ifdef CONFIG_NUMA
579a1f0dacaSLorenzo Stoakes 	struct mempolicy *vm_policy;	/* NUMA policy for the VMA */
580a1f0dacaSLorenzo Stoakes #endif
581a1f0dacaSLorenzo Stoakes #ifdef CONFIG_NUMA_BALANCING
582a1f0dacaSLorenzo Stoakes 	struct vma_numab_state *numab_state;	/* NUMA Balancing state */
583a1f0dacaSLorenzo Stoakes #endif
584a1f0dacaSLorenzo Stoakes #ifdef CONFIG_PER_VMA_LOCK
585a1f0dacaSLorenzo Stoakes 	/* Unstable RCU readers are allowed to read this. */
586a1f0dacaSLorenzo Stoakes 	refcount_t vm_refcnt;
587a1f0dacaSLorenzo Stoakes #endif
588a1f0dacaSLorenzo Stoakes 	/*
589a1f0dacaSLorenzo Stoakes 	 * For areas with an address space and backing store,
590a1f0dacaSLorenzo Stoakes 	 * linkage into the address_space->i_mmap interval tree.
591a1f0dacaSLorenzo Stoakes 	 *
592a1f0dacaSLorenzo Stoakes 	 */
593a1f0dacaSLorenzo Stoakes 	struct {
594a1f0dacaSLorenzo Stoakes 		struct rb_node rb;
595a1f0dacaSLorenzo Stoakes 		unsigned long rb_subtree_last;
596a1f0dacaSLorenzo Stoakes 	} shared;
597a1f0dacaSLorenzo Stoakes #ifdef CONFIG_ANON_VMA_NAME
598a1f0dacaSLorenzo Stoakes 	/*
599a1f0dacaSLorenzo Stoakes 	 * For private and shared anonymous mappings, a pointer to a null
600a1f0dacaSLorenzo Stoakes 	 * terminated string containing the name given to the vma, or NULL if
601a1f0dacaSLorenzo Stoakes 	 * unnamed. Serialized by mmap_lock. Use anon_vma_name to access.
602a1f0dacaSLorenzo Stoakes 	 */
603a1f0dacaSLorenzo Stoakes 	struct anon_vma_name *anon_name;
604a1f0dacaSLorenzo Stoakes #endif
605a1f0dacaSLorenzo Stoakes 	struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
606a1f0dacaSLorenzo Stoakes } __randomize_layout;
607a1f0dacaSLorenzo Stoakes 
608a1f0dacaSLorenzo Stoakes struct vm_operations_struct {
609a1f0dacaSLorenzo Stoakes 	void (*open)(struct vm_area_struct * area);
610a1f0dacaSLorenzo Stoakes 	/**
611a1f0dacaSLorenzo Stoakes 	 * @close: Called when the VMA is being removed from the MM.
612a1f0dacaSLorenzo Stoakes 	 * Context: User context.  May sleep.  Caller holds mmap_lock.
613a1f0dacaSLorenzo Stoakes 	 */
614a1f0dacaSLorenzo Stoakes 	void (*close)(struct vm_area_struct * area);
615a1f0dacaSLorenzo Stoakes 	/* Called any time before splitting to check if it's allowed */
616a1f0dacaSLorenzo Stoakes 	int (*may_split)(struct vm_area_struct *area, unsigned long addr);
617a1f0dacaSLorenzo Stoakes 	int (*mremap)(struct vm_area_struct *area);
618a1f0dacaSLorenzo Stoakes 	/*
619a1f0dacaSLorenzo Stoakes 	 * Called by mprotect() to make driver-specific permission
620a1f0dacaSLorenzo Stoakes 	 * checks before mprotect() is finalised.   The VMA must not
621a1f0dacaSLorenzo Stoakes 	 * be modified.  Returns 0 if mprotect() can proceed.
622a1f0dacaSLorenzo Stoakes 	 */
623a1f0dacaSLorenzo Stoakes 	int (*mprotect)(struct vm_area_struct *vma, unsigned long start,
624a1f0dacaSLorenzo Stoakes 			unsigned long end, unsigned long newflags);
625a1f0dacaSLorenzo Stoakes 	vm_fault_t (*fault)(struct vm_fault *vmf);
626a1f0dacaSLorenzo Stoakes 	vm_fault_t (*huge_fault)(struct vm_fault *vmf, unsigned int order);
627a1f0dacaSLorenzo Stoakes 	vm_fault_t (*map_pages)(struct vm_fault *vmf,
628a1f0dacaSLorenzo Stoakes 			pgoff_t start_pgoff, pgoff_t end_pgoff);
629a1f0dacaSLorenzo Stoakes 	unsigned long (*pagesize)(struct vm_area_struct * area);
630a1f0dacaSLorenzo Stoakes 
631a1f0dacaSLorenzo Stoakes 	/* notification that a previously read-only page is about to become
632a1f0dacaSLorenzo Stoakes 	 * writable, if an error is returned it will cause a SIGBUS */
633a1f0dacaSLorenzo Stoakes 	vm_fault_t (*page_mkwrite)(struct vm_fault *vmf);
634a1f0dacaSLorenzo Stoakes 
635a1f0dacaSLorenzo Stoakes 	/* same as page_mkwrite when using VM_PFNMAP|VM_MIXEDMAP */
636a1f0dacaSLorenzo Stoakes 	vm_fault_t (*pfn_mkwrite)(struct vm_fault *vmf);
637a1f0dacaSLorenzo Stoakes 
638a1f0dacaSLorenzo Stoakes 	/* called by access_process_vm when get_user_pages() fails, typically
639a1f0dacaSLorenzo Stoakes 	 * for use by special VMAs. See also generic_access_phys() for a generic
640a1f0dacaSLorenzo Stoakes 	 * implementation useful for any iomem mapping.
641a1f0dacaSLorenzo Stoakes 	 */
642a1f0dacaSLorenzo Stoakes 	int (*access)(struct vm_area_struct *vma, unsigned long addr,
643a1f0dacaSLorenzo Stoakes 		      void *buf, int len, int write);
644a1f0dacaSLorenzo Stoakes 
645a1f0dacaSLorenzo Stoakes 	/* Called by the /proc/PID/maps code to ask the vma whether it
646a1f0dacaSLorenzo Stoakes 	 * has a special name.  Returning non-NULL will also cause this
647a1f0dacaSLorenzo Stoakes 	 * vma to be dumped unconditionally. */
648a1f0dacaSLorenzo Stoakes 	const char *(*name)(struct vm_area_struct *vma);
649a1f0dacaSLorenzo Stoakes 
650a1f0dacaSLorenzo Stoakes #ifdef CONFIG_NUMA
651a1f0dacaSLorenzo Stoakes 	/*
652a1f0dacaSLorenzo Stoakes 	 * set_policy() op must add a reference to any non-NULL @new mempolicy
653a1f0dacaSLorenzo Stoakes 	 * to hold the policy upon return.  Caller should pass NULL @new to
654a1f0dacaSLorenzo Stoakes 	 * remove a policy and fall back to surrounding context--i.e. do not
655a1f0dacaSLorenzo Stoakes 	 * install a MPOL_DEFAULT policy, nor the task or system default
656a1f0dacaSLorenzo Stoakes 	 * mempolicy.
657a1f0dacaSLorenzo Stoakes 	 */
658a1f0dacaSLorenzo Stoakes 	int (*set_policy)(struct vm_area_struct *vma, struct mempolicy *new);
659a1f0dacaSLorenzo Stoakes 
660a1f0dacaSLorenzo Stoakes 	/*
661a1f0dacaSLorenzo Stoakes 	 * get_policy() op must add reference [mpol_get()] to any policy at
662a1f0dacaSLorenzo Stoakes 	 * (vma,addr) marked as MPOL_SHARED.  The shared policy infrastructure
663a1f0dacaSLorenzo Stoakes 	 * in mm/mempolicy.c will do this automatically.
664a1f0dacaSLorenzo Stoakes 	 * get_policy() must NOT add a ref if the policy at (vma,addr) is not
665a1f0dacaSLorenzo Stoakes 	 * marked as MPOL_SHARED. vma policies are protected by the mmap_lock.
666a1f0dacaSLorenzo Stoakes 	 * If no [shared/vma] mempolicy exists at the addr, get_policy() op
667a1f0dacaSLorenzo Stoakes 	 * must return NULL--i.e., do not "fallback" to task or system default
668a1f0dacaSLorenzo Stoakes 	 * policy.
669a1f0dacaSLorenzo Stoakes 	 */
670a1f0dacaSLorenzo Stoakes 	struct mempolicy *(*get_policy)(struct vm_area_struct *vma,
671a1f0dacaSLorenzo Stoakes 					unsigned long addr, pgoff_t *ilx);
672a1f0dacaSLorenzo Stoakes #endif
673a1f0dacaSLorenzo Stoakes #ifdef CONFIG_FIND_NORMAL_PAGE
674a1f0dacaSLorenzo Stoakes 	/*
675a1f0dacaSLorenzo Stoakes 	 * Called by vm_normal_page() for special PTEs in @vma at @addr. This
676a1f0dacaSLorenzo Stoakes 	 * allows for returning a "normal" page from vm_normal_page() even
677a1f0dacaSLorenzo Stoakes 	 * though the PTE indicates that the "struct page" either does not exist
678a1f0dacaSLorenzo Stoakes 	 * or should not be touched: "special".
679a1f0dacaSLorenzo Stoakes 	 *
680a1f0dacaSLorenzo Stoakes 	 * Do not add new users: this really only works when a "normal" page
681a1f0dacaSLorenzo Stoakes 	 * was mapped, but then the PTE got changed to something weird (+
682a1f0dacaSLorenzo Stoakes 	 * marked special) that would not make pte_pfn() identify the originally
683a1f0dacaSLorenzo Stoakes 	 * inserted page.
684a1f0dacaSLorenzo Stoakes 	 */
685a1f0dacaSLorenzo Stoakes 	struct page *(*find_normal_page)(struct vm_area_struct *vma,
686a1f0dacaSLorenzo Stoakes 					 unsigned long addr);
687a1f0dacaSLorenzo Stoakes #endif /* CONFIG_FIND_NORMAL_PAGE */
688a1f0dacaSLorenzo Stoakes };
689a1f0dacaSLorenzo Stoakes 
690a1f0dacaSLorenzo Stoakes struct vm_unmapped_area_info {
691a1f0dacaSLorenzo Stoakes #define VM_UNMAPPED_AREA_TOPDOWN 1
692a1f0dacaSLorenzo Stoakes 	unsigned long flags;
693a1f0dacaSLorenzo Stoakes 	unsigned long length;
694a1f0dacaSLorenzo Stoakes 	unsigned long low_limit;
695a1f0dacaSLorenzo Stoakes 	unsigned long high_limit;
696a1f0dacaSLorenzo Stoakes 	unsigned long align_mask;
697a1f0dacaSLorenzo Stoakes 	unsigned long align_offset;
698a1f0dacaSLorenzo Stoakes 	unsigned long start_gap;
699a1f0dacaSLorenzo Stoakes };
700a1f0dacaSLorenzo Stoakes 
701a1f0dacaSLorenzo Stoakes struct pagetable_move_control {
702a1f0dacaSLorenzo Stoakes 	struct vm_area_struct *old; /* Source VMA. */
703a1f0dacaSLorenzo Stoakes 	struct vm_area_struct *new; /* Destination VMA. */
704a1f0dacaSLorenzo Stoakes 	unsigned long old_addr; /* Address from which the move begins. */
705a1f0dacaSLorenzo Stoakes 	unsigned long old_end; /* Exclusive address at which old range ends. */
706a1f0dacaSLorenzo Stoakes 	unsigned long new_addr; /* Address to move page tables to. */
707a1f0dacaSLorenzo Stoakes 	unsigned long len_in; /* Bytes to remap specified by user. */
708a1f0dacaSLorenzo Stoakes 
709a1f0dacaSLorenzo Stoakes 	bool need_rmap_locks; /* Do rmap locks need to be taken? */
710a1f0dacaSLorenzo Stoakes 	bool for_stack; /* Is this an early temp stack being moved? */
711a1f0dacaSLorenzo Stoakes };
712a1f0dacaSLorenzo Stoakes 
713a1f0dacaSLorenzo Stoakes #define PAGETABLE_MOVE(name, old_, new_, old_addr_, new_addr_, len_)	\
714a1f0dacaSLorenzo Stoakes 	struct pagetable_move_control name = {				\
715a1f0dacaSLorenzo Stoakes 		.old = old_,						\
716a1f0dacaSLorenzo Stoakes 		.new = new_,						\
717a1f0dacaSLorenzo Stoakes 		.old_addr = old_addr_,					\
718a1f0dacaSLorenzo Stoakes 		.old_end = (old_addr_) + (len_),			\
719a1f0dacaSLorenzo Stoakes 		.new_addr = new_addr_,					\
720a1f0dacaSLorenzo Stoakes 		.len_in = len_,						\
721a1f0dacaSLorenzo Stoakes 	}
722a1f0dacaSLorenzo Stoakes 
vma_iter_invalidate(struct vma_iterator * vmi)723a1f0dacaSLorenzo Stoakes static inline void vma_iter_invalidate(struct vma_iterator *vmi)
724a1f0dacaSLorenzo Stoakes {
725a1f0dacaSLorenzo Stoakes 	mas_pause(&vmi->mas);
726a1f0dacaSLorenzo Stoakes }
727a1f0dacaSLorenzo Stoakes 
pgprot_modify(pgprot_t oldprot,pgprot_t newprot)728a1f0dacaSLorenzo Stoakes static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
729a1f0dacaSLorenzo Stoakes {
730a1f0dacaSLorenzo Stoakes 	return __pgprot(pgprot_val(oldprot) | pgprot_val(newprot));
731a1f0dacaSLorenzo Stoakes }
732a1f0dacaSLorenzo Stoakes 
vm_get_page_prot(vm_flags_t vm_flags)733a1f0dacaSLorenzo Stoakes static inline pgprot_t vm_get_page_prot(vm_flags_t vm_flags)
734a1f0dacaSLorenzo Stoakes {
735a1f0dacaSLorenzo Stoakes 	return __pgprot(vm_flags);
736a1f0dacaSLorenzo Stoakes }
737a1f0dacaSLorenzo Stoakes 
mm_flags_test(int flag,const struct mm_struct * mm)738a1f0dacaSLorenzo Stoakes static inline bool mm_flags_test(int flag, const struct mm_struct *mm)
739a1f0dacaSLorenzo Stoakes {
740a1f0dacaSLorenzo Stoakes 	return test_bit(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags));
741a1f0dacaSLorenzo Stoakes }
742a1f0dacaSLorenzo Stoakes 
743a1f0dacaSLorenzo Stoakes /*
744a1f0dacaSLorenzo Stoakes  * Copy value to the first system word of VMA flags, non-atomically.
745a1f0dacaSLorenzo Stoakes  *
746a1f0dacaSLorenzo Stoakes  * IMPORTANT: This does not overwrite bytes past the first system word. The
747a1f0dacaSLorenzo Stoakes  * caller must account for this.
748a1f0dacaSLorenzo Stoakes  */
vma_flags_overwrite_word(vma_flags_t * flags,unsigned long value)749a1f0dacaSLorenzo Stoakes static inline void vma_flags_overwrite_word(vma_flags_t *flags, unsigned long value)
750a1f0dacaSLorenzo Stoakes {
751a1f0dacaSLorenzo Stoakes 	*ACCESS_PRIVATE(flags, __vma_flags) = value;
752a1f0dacaSLorenzo Stoakes }
753a1f0dacaSLorenzo Stoakes 
754a1f0dacaSLorenzo Stoakes /*
755a1f0dacaSLorenzo Stoakes  * Copy value to the first system word of VMA flags ONCE, non-atomically.
756a1f0dacaSLorenzo Stoakes  *
757a1f0dacaSLorenzo Stoakes  * IMPORTANT: This does not overwrite bytes past the first system word. The
758a1f0dacaSLorenzo Stoakes  * caller must account for this.
759a1f0dacaSLorenzo Stoakes  */
vma_flags_overwrite_word_once(vma_flags_t * flags,unsigned long value)760a1f0dacaSLorenzo Stoakes static inline void vma_flags_overwrite_word_once(vma_flags_t *flags, unsigned long value)
761a1f0dacaSLorenzo Stoakes {
762a1f0dacaSLorenzo Stoakes 	unsigned long *bitmap = ACCESS_PRIVATE(flags, __vma_flags);
763a1f0dacaSLorenzo Stoakes 
764a1f0dacaSLorenzo Stoakes 	WRITE_ONCE(*bitmap, value);
765a1f0dacaSLorenzo Stoakes }
766a1f0dacaSLorenzo Stoakes 
767a1f0dacaSLorenzo Stoakes /* Update the first system word of VMA flags setting bits, non-atomically. */
vma_flags_set_word(vma_flags_t * flags,unsigned long value)768a1f0dacaSLorenzo Stoakes static inline void vma_flags_set_word(vma_flags_t *flags, unsigned long value)
769a1f0dacaSLorenzo Stoakes {
770a1f0dacaSLorenzo Stoakes 	unsigned long *bitmap = ACCESS_PRIVATE(flags, __vma_flags);
771a1f0dacaSLorenzo Stoakes 
772a1f0dacaSLorenzo Stoakes 	*bitmap |= value;
773a1f0dacaSLorenzo Stoakes }
774a1f0dacaSLorenzo Stoakes 
775a1f0dacaSLorenzo Stoakes /* Update the first system word of VMA flags clearing bits, non-atomically. */
vma_flags_clear_word(vma_flags_t * flags,unsigned long value)776a1f0dacaSLorenzo Stoakes static inline void vma_flags_clear_word(vma_flags_t *flags, unsigned long value)
777a1f0dacaSLorenzo Stoakes {
778a1f0dacaSLorenzo Stoakes 	unsigned long *bitmap = ACCESS_PRIVATE(flags, __vma_flags);
779a1f0dacaSLorenzo Stoakes 
780a1f0dacaSLorenzo Stoakes 	*bitmap &= ~value;
781a1f0dacaSLorenzo Stoakes }
782a1f0dacaSLorenzo Stoakes 
vma_flags_clear_all(vma_flags_t * flags)783a1f0dacaSLorenzo Stoakes static inline void vma_flags_clear_all(vma_flags_t *flags)
784a1f0dacaSLorenzo Stoakes {
785a1f0dacaSLorenzo Stoakes 	bitmap_zero(ACCESS_PRIVATE(flags, __vma_flags), NUM_VMA_FLAG_BITS);
786a1f0dacaSLorenzo Stoakes }
787a1f0dacaSLorenzo Stoakes 
vma_flag_set(vma_flags_t * flags,vma_flag_t bit)788a1f0dacaSLorenzo Stoakes static inline void vma_flag_set(vma_flags_t *flags, vma_flag_t bit)
789a1f0dacaSLorenzo Stoakes {
790a1f0dacaSLorenzo Stoakes 	unsigned long *bitmap = ACCESS_PRIVATE(flags, __vma_flags);
791a1f0dacaSLorenzo Stoakes 
792a1f0dacaSLorenzo Stoakes 	__set_bit((__force int)bit, bitmap);
793a1f0dacaSLorenzo Stoakes }
794a1f0dacaSLorenzo Stoakes 
795a1f0dacaSLorenzo Stoakes /* Use when VMA is not part of the VMA tree and needs no locking */
vm_flags_init(struct vm_area_struct * vma,vm_flags_t flags)796a1f0dacaSLorenzo Stoakes static inline void vm_flags_init(struct vm_area_struct *vma,
797a1f0dacaSLorenzo Stoakes 				 vm_flags_t flags)
798a1f0dacaSLorenzo Stoakes {
799a1f0dacaSLorenzo Stoakes 	vma_flags_clear_all(&vma->flags);
800a1f0dacaSLorenzo Stoakes 	vma_flags_overwrite_word(&vma->flags, flags);
801a1f0dacaSLorenzo Stoakes }
802a1f0dacaSLorenzo Stoakes 
803a1f0dacaSLorenzo Stoakes /*
804a1f0dacaSLorenzo Stoakes  * Use when VMA is part of the VMA tree and modifications need coordination
805a1f0dacaSLorenzo Stoakes  * Note: vm_flags_reset and vm_flags_reset_once do not lock the vma and
806a1f0dacaSLorenzo Stoakes  * it should be locked explicitly beforehand.
807a1f0dacaSLorenzo Stoakes  */
vm_flags_reset(struct vm_area_struct * vma,vm_flags_t flags)808a1f0dacaSLorenzo Stoakes static inline void vm_flags_reset(struct vm_area_struct *vma,
809a1f0dacaSLorenzo Stoakes 				  vm_flags_t flags)
810a1f0dacaSLorenzo Stoakes {
811a1f0dacaSLorenzo Stoakes 	vma_assert_write_locked(vma);
812a1f0dacaSLorenzo Stoakes 	vm_flags_init(vma, flags);
813a1f0dacaSLorenzo Stoakes }
814a1f0dacaSLorenzo Stoakes 
vm_flags_reset_once(struct vm_area_struct * vma,vm_flags_t flags)815a1f0dacaSLorenzo Stoakes static inline void vm_flags_reset_once(struct vm_area_struct *vma,
816a1f0dacaSLorenzo Stoakes 				       vm_flags_t flags)
817a1f0dacaSLorenzo Stoakes {
818a1f0dacaSLorenzo Stoakes 	vma_assert_write_locked(vma);
819a1f0dacaSLorenzo Stoakes 	/*
820a1f0dacaSLorenzo Stoakes 	 * The user should only be interested in avoiding reordering of
821a1f0dacaSLorenzo Stoakes 	 * assignment to the first word.
822a1f0dacaSLorenzo Stoakes 	 */
823a1f0dacaSLorenzo Stoakes 	vma_flags_clear_all(&vma->flags);
824a1f0dacaSLorenzo Stoakes 	vma_flags_overwrite_word_once(&vma->flags, flags);
825a1f0dacaSLorenzo Stoakes }
826a1f0dacaSLorenzo Stoakes 
vm_flags_set(struct vm_area_struct * vma,vm_flags_t flags)827a1f0dacaSLorenzo Stoakes static inline void vm_flags_set(struct vm_area_struct *vma,
828a1f0dacaSLorenzo Stoakes 				vm_flags_t flags)
829a1f0dacaSLorenzo Stoakes {
830a1f0dacaSLorenzo Stoakes 	vma_start_write(vma);
831a1f0dacaSLorenzo Stoakes 	vma_flags_set_word(&vma->flags, flags);
832a1f0dacaSLorenzo Stoakes }
833a1f0dacaSLorenzo Stoakes 
vm_flags_clear(struct vm_area_struct * vma,vm_flags_t flags)834a1f0dacaSLorenzo Stoakes static inline void vm_flags_clear(struct vm_area_struct *vma,
835a1f0dacaSLorenzo Stoakes 				  vm_flags_t flags)
836a1f0dacaSLorenzo Stoakes {
837a1f0dacaSLorenzo Stoakes 	vma_start_write(vma);
838a1f0dacaSLorenzo Stoakes 	vma_flags_clear_word(&vma->flags, flags);
839a1f0dacaSLorenzo Stoakes }
840a1f0dacaSLorenzo Stoakes 
841*f615cc92SLorenzo Stoakes static inline vma_flags_t __mk_vma_flags(size_t count, const vma_flag_t *bits);
842a1f0dacaSLorenzo Stoakes 
843a1f0dacaSLorenzo Stoakes #define mk_vma_flags(...) __mk_vma_flags(COUNT_ARGS(__VA_ARGS__), \
844a1f0dacaSLorenzo Stoakes 					 (const vma_flag_t []){__VA_ARGS__})
845a1f0dacaSLorenzo Stoakes 
vma_flags_test_mask(const vma_flags_t * flags,vma_flags_t to_test)846a1f0dacaSLorenzo Stoakes static __always_inline bool vma_flags_test_mask(const vma_flags_t *flags,
847a1f0dacaSLorenzo Stoakes 		vma_flags_t to_test)
848a1f0dacaSLorenzo Stoakes {
849a1f0dacaSLorenzo Stoakes 	const unsigned long *bitmap = flags->__vma_flags;
850a1f0dacaSLorenzo Stoakes 	const unsigned long *bitmap_to_test = to_test.__vma_flags;
851a1f0dacaSLorenzo Stoakes 
852a1f0dacaSLorenzo Stoakes 	return bitmap_intersects(bitmap_to_test, bitmap, NUM_VMA_FLAG_BITS);
853a1f0dacaSLorenzo Stoakes }
854a1f0dacaSLorenzo Stoakes 
855a1f0dacaSLorenzo Stoakes #define vma_flags_test(flags, ...) \
856a1f0dacaSLorenzo Stoakes 	vma_flags_test_mask(flags, mk_vma_flags(__VA_ARGS__))
857a1f0dacaSLorenzo Stoakes 
vma_flags_test_all_mask(const vma_flags_t * flags,vma_flags_t to_test)858a1f0dacaSLorenzo Stoakes static __always_inline bool vma_flags_test_all_mask(const vma_flags_t *flags,
859a1f0dacaSLorenzo Stoakes 		vma_flags_t to_test)
860a1f0dacaSLorenzo Stoakes {
861a1f0dacaSLorenzo Stoakes 	const unsigned long *bitmap = flags->__vma_flags;
862a1f0dacaSLorenzo Stoakes 	const unsigned long *bitmap_to_test = to_test.__vma_flags;
863a1f0dacaSLorenzo Stoakes 
864a1f0dacaSLorenzo Stoakes 	return bitmap_subset(bitmap_to_test, bitmap, NUM_VMA_FLAG_BITS);
865a1f0dacaSLorenzo Stoakes }
866a1f0dacaSLorenzo Stoakes 
867a1f0dacaSLorenzo Stoakes #define vma_flags_test_all(flags, ...) \
868a1f0dacaSLorenzo Stoakes 	vma_flags_test_all_mask(flags, mk_vma_flags(__VA_ARGS__))
869a1f0dacaSLorenzo Stoakes 
vma_flags_set_mask(vma_flags_t * flags,vma_flags_t to_set)870a1f0dacaSLorenzo Stoakes static __always_inline void vma_flags_set_mask(vma_flags_t *flags, vma_flags_t to_set)
871a1f0dacaSLorenzo Stoakes {
872a1f0dacaSLorenzo Stoakes 	unsigned long *bitmap = flags->__vma_flags;
873a1f0dacaSLorenzo Stoakes 	const unsigned long *bitmap_to_set = to_set.__vma_flags;
874a1f0dacaSLorenzo Stoakes 
875a1f0dacaSLorenzo Stoakes 	bitmap_or(bitmap, bitmap, bitmap_to_set, NUM_VMA_FLAG_BITS);
876a1f0dacaSLorenzo Stoakes }
877a1f0dacaSLorenzo Stoakes 
878a1f0dacaSLorenzo Stoakes #define vma_flags_set(flags, ...) \
879a1f0dacaSLorenzo Stoakes 	vma_flags_set_mask(flags, mk_vma_flags(__VA_ARGS__))
880a1f0dacaSLorenzo Stoakes 
vma_flags_clear_mask(vma_flags_t * flags,vma_flags_t to_clear)881a1f0dacaSLorenzo Stoakes static __always_inline void vma_flags_clear_mask(vma_flags_t *flags, vma_flags_t to_clear)
882a1f0dacaSLorenzo Stoakes {
883a1f0dacaSLorenzo Stoakes 	unsigned long *bitmap = flags->__vma_flags;
884a1f0dacaSLorenzo Stoakes 	const unsigned long *bitmap_to_clear = to_clear.__vma_flags;
885a1f0dacaSLorenzo Stoakes 
886a1f0dacaSLorenzo Stoakes 	bitmap_andnot(bitmap, bitmap, bitmap_to_clear, NUM_VMA_FLAG_BITS);
887a1f0dacaSLorenzo Stoakes }
888a1f0dacaSLorenzo Stoakes 
889a1f0dacaSLorenzo Stoakes #define vma_flags_clear(flags, ...) \
890a1f0dacaSLorenzo Stoakes 	vma_flags_clear_mask(flags, mk_vma_flags(__VA_ARGS__))
891a1f0dacaSLorenzo Stoakes 
vma_test_all_flags_mask(const struct vm_area_struct * vma,vma_flags_t flags)892a1f0dacaSLorenzo Stoakes static inline bool vma_test_all_flags_mask(const struct vm_area_struct *vma,
893a1f0dacaSLorenzo Stoakes 					   vma_flags_t flags)
894a1f0dacaSLorenzo Stoakes {
895a1f0dacaSLorenzo Stoakes 	return vma_flags_test_all_mask(&vma->flags, flags);
896a1f0dacaSLorenzo Stoakes }
897a1f0dacaSLorenzo Stoakes 
898a1f0dacaSLorenzo Stoakes #define vma_test_all_flags(vma, ...) \
899a1f0dacaSLorenzo Stoakes 	vma_test_all_flags_mask(vma, mk_vma_flags(__VA_ARGS__))
900a1f0dacaSLorenzo Stoakes 
is_shared_maywrite_vm_flags(vm_flags_t vm_flags)901a1f0dacaSLorenzo Stoakes static inline bool is_shared_maywrite_vm_flags(vm_flags_t vm_flags)
902a1f0dacaSLorenzo Stoakes {
903a1f0dacaSLorenzo Stoakes 	return (vm_flags & (VM_SHARED | VM_MAYWRITE)) ==
904a1f0dacaSLorenzo Stoakes 		(VM_SHARED | VM_MAYWRITE);
905a1f0dacaSLorenzo Stoakes }
906a1f0dacaSLorenzo Stoakes 
vma_set_flags_mask(struct vm_area_struct * vma,vma_flags_t flags)907a1f0dacaSLorenzo Stoakes static inline void vma_set_flags_mask(struct vm_area_struct *vma,
908a1f0dacaSLorenzo Stoakes 				      vma_flags_t flags)
909a1f0dacaSLorenzo Stoakes {
910a1f0dacaSLorenzo Stoakes 	vma_flags_set_mask(&vma->flags, flags);
911a1f0dacaSLorenzo Stoakes }
912a1f0dacaSLorenzo Stoakes 
913a1f0dacaSLorenzo Stoakes #define vma_set_flags(vma, ...) \
914a1f0dacaSLorenzo Stoakes 	vma_set_flags_mask(vma, mk_vma_flags(__VA_ARGS__))
915a1f0dacaSLorenzo Stoakes 
vma_desc_test_flags_mask(const struct vm_area_desc * desc,vma_flags_t flags)916a1f0dacaSLorenzo Stoakes static inline bool vma_desc_test_flags_mask(const struct vm_area_desc *desc,
917a1f0dacaSLorenzo Stoakes 					    vma_flags_t flags)
918a1f0dacaSLorenzo Stoakes {
919a1f0dacaSLorenzo Stoakes 	return vma_flags_test_mask(&desc->vma_flags, flags);
920a1f0dacaSLorenzo Stoakes }
921a1f0dacaSLorenzo Stoakes 
922a1f0dacaSLorenzo Stoakes #define vma_desc_test_flags(desc, ...) \
923a1f0dacaSLorenzo Stoakes 	vma_desc_test_flags_mask(desc, mk_vma_flags(__VA_ARGS__))
924a1f0dacaSLorenzo Stoakes 
vma_desc_set_flags_mask(struct vm_area_desc * desc,vma_flags_t flags)925a1f0dacaSLorenzo Stoakes static inline void vma_desc_set_flags_mask(struct vm_area_desc *desc,
926a1f0dacaSLorenzo Stoakes 					   vma_flags_t flags)
927a1f0dacaSLorenzo Stoakes {
928a1f0dacaSLorenzo Stoakes 	vma_flags_set_mask(&desc->vma_flags, flags);
929a1f0dacaSLorenzo Stoakes }
930a1f0dacaSLorenzo Stoakes 
931a1f0dacaSLorenzo Stoakes #define vma_desc_set_flags(desc, ...) \
932a1f0dacaSLorenzo Stoakes 	vma_desc_set_flags_mask(desc, mk_vma_flags(__VA_ARGS__))
933a1f0dacaSLorenzo Stoakes 
vma_desc_clear_flags_mask(struct vm_area_desc * desc,vma_flags_t flags)934a1f0dacaSLorenzo Stoakes static inline void vma_desc_clear_flags_mask(struct vm_area_desc *desc,
935a1f0dacaSLorenzo Stoakes 					     vma_flags_t flags)
936a1f0dacaSLorenzo Stoakes {
937a1f0dacaSLorenzo Stoakes 	vma_flags_clear_mask(&desc->vma_flags, flags);
938a1f0dacaSLorenzo Stoakes }
939a1f0dacaSLorenzo Stoakes 
940a1f0dacaSLorenzo Stoakes #define vma_desc_clear_flags(desc, ...) \
941a1f0dacaSLorenzo Stoakes 	vma_desc_clear_flags_mask(desc, mk_vma_flags(__VA_ARGS__))
942a1f0dacaSLorenzo Stoakes 
is_shared_maywrite(const vma_flags_t * flags)943a1f0dacaSLorenzo Stoakes static inline bool is_shared_maywrite(const vma_flags_t *flags)
944a1f0dacaSLorenzo Stoakes {
945a1f0dacaSLorenzo Stoakes 	return vma_flags_test_all(flags, VMA_SHARED_BIT, VMA_MAYWRITE_BIT);
946a1f0dacaSLorenzo Stoakes }
947a1f0dacaSLorenzo Stoakes 
vma_is_shared_maywrite(struct vm_area_struct * vma)948a1f0dacaSLorenzo Stoakes static inline bool vma_is_shared_maywrite(struct vm_area_struct *vma)
949a1f0dacaSLorenzo Stoakes {
950a1f0dacaSLorenzo Stoakes 	return is_shared_maywrite(&vma->flags);
951a1f0dacaSLorenzo Stoakes }
952a1f0dacaSLorenzo Stoakes 
vma_next(struct vma_iterator * vmi)953a1f0dacaSLorenzo Stoakes static inline struct vm_area_struct *vma_next(struct vma_iterator *vmi)
954a1f0dacaSLorenzo Stoakes {
955a1f0dacaSLorenzo Stoakes 	/*
956a1f0dacaSLorenzo Stoakes 	 * Uses mas_find() to get the first VMA when the iterator starts.
957a1f0dacaSLorenzo Stoakes 	 * Calling mas_next() could skip the first entry.
958a1f0dacaSLorenzo Stoakes 	 */
959a1f0dacaSLorenzo Stoakes 	return mas_find(&vmi->mas, ULONG_MAX);
960a1f0dacaSLorenzo Stoakes }
961a1f0dacaSLorenzo Stoakes 
962a1f0dacaSLorenzo Stoakes /*
963a1f0dacaSLorenzo Stoakes  * WARNING: to avoid racing with vma_mark_attached()/vma_mark_detached(), these
964a1f0dacaSLorenzo Stoakes  * assertions should be made either under mmap_write_lock or when the object
965a1f0dacaSLorenzo Stoakes  * has been isolated under mmap_write_lock, ensuring no competing writers.
966a1f0dacaSLorenzo Stoakes  */
vma_assert_attached(struct vm_area_struct * vma)967a1f0dacaSLorenzo Stoakes static inline void vma_assert_attached(struct vm_area_struct *vma)
968a1f0dacaSLorenzo Stoakes {
969a1f0dacaSLorenzo Stoakes 	WARN_ON_ONCE(!refcount_read(&vma->vm_refcnt));
970a1f0dacaSLorenzo Stoakes }
971a1f0dacaSLorenzo Stoakes 
vma_assert_detached(struct vm_area_struct * vma)972a1f0dacaSLorenzo Stoakes static inline void vma_assert_detached(struct vm_area_struct *vma)
973a1f0dacaSLorenzo Stoakes {
974a1f0dacaSLorenzo Stoakes 	WARN_ON_ONCE(refcount_read(&vma->vm_refcnt));
975a1f0dacaSLorenzo Stoakes }
976a1f0dacaSLorenzo Stoakes 
977a1f0dacaSLorenzo Stoakes static inline void vma_assert_write_locked(struct vm_area_struct *);
vma_mark_attached(struct vm_area_struct * vma)978a1f0dacaSLorenzo Stoakes static inline void vma_mark_attached(struct vm_area_struct *vma)
979a1f0dacaSLorenzo Stoakes {
980a1f0dacaSLorenzo Stoakes 	vma_assert_write_locked(vma);
981a1f0dacaSLorenzo Stoakes 	vma_assert_detached(vma);
982a1f0dacaSLorenzo Stoakes 	refcount_set_release(&vma->vm_refcnt, 1);
983a1f0dacaSLorenzo Stoakes }
984a1f0dacaSLorenzo Stoakes 
vma_mark_detached(struct vm_area_struct * vma)985a1f0dacaSLorenzo Stoakes static inline void vma_mark_detached(struct vm_area_struct *vma)
986a1f0dacaSLorenzo Stoakes {
987a1f0dacaSLorenzo Stoakes 	vma_assert_write_locked(vma);
988a1f0dacaSLorenzo Stoakes 	vma_assert_attached(vma);
989a1f0dacaSLorenzo Stoakes 	/* We are the only writer, so no need to use vma_refcount_put(). */
990a1f0dacaSLorenzo Stoakes 	if (unlikely(!refcount_dec_and_test(&vma->vm_refcnt))) {
991a1f0dacaSLorenzo Stoakes 		/*
992a1f0dacaSLorenzo Stoakes 		 * Reader must have temporarily raised vm_refcnt but it will
993a1f0dacaSLorenzo Stoakes 		 * drop it without using the vma since vma is write-locked.
994a1f0dacaSLorenzo Stoakes 		 */
995a1f0dacaSLorenzo Stoakes 	}
996a1f0dacaSLorenzo Stoakes }
997a1f0dacaSLorenzo Stoakes 
vma_init(struct vm_area_struct * vma,struct mm_struct * mm)998a1f0dacaSLorenzo Stoakes static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm)
999a1f0dacaSLorenzo Stoakes {
1000a1f0dacaSLorenzo Stoakes 	memset(vma, 0, sizeof(*vma));
1001a1f0dacaSLorenzo Stoakes 	vma->vm_mm = mm;
1002a1f0dacaSLorenzo Stoakes 	vma->vm_ops = &vma_dummy_vm_ops;
1003a1f0dacaSLorenzo Stoakes 	INIT_LIST_HEAD(&vma->anon_vma_chain);
1004a1f0dacaSLorenzo Stoakes 	vma->vm_lock_seq = UINT_MAX;
1005a1f0dacaSLorenzo Stoakes }
1006a1f0dacaSLorenzo Stoakes 
1007a1f0dacaSLorenzo Stoakes /*
1008a1f0dacaSLorenzo Stoakes  * These are defined in vma.h, but sadly vm_stat_account() is referenced by
1009a1f0dacaSLorenzo Stoakes  * kernel/fork.c, so we have to these broadly available there, and temporarily
1010a1f0dacaSLorenzo Stoakes  * define them here to resolve the dependency cycle.
1011a1f0dacaSLorenzo Stoakes  */
1012a1f0dacaSLorenzo Stoakes #define is_exec_mapping(flags) \
1013a1f0dacaSLorenzo Stoakes 	((flags & (VM_EXEC | VM_WRITE | VM_STACK)) == VM_EXEC)
1014a1f0dacaSLorenzo Stoakes 
1015a1f0dacaSLorenzo Stoakes #define is_stack_mapping(flags) \
1016a1f0dacaSLorenzo Stoakes 	(((flags & VM_STACK) == VM_STACK) || (flags & VM_SHADOW_STACK))
1017a1f0dacaSLorenzo Stoakes 
1018a1f0dacaSLorenzo Stoakes #define is_data_mapping(flags) \
1019a1f0dacaSLorenzo Stoakes 	((flags & (VM_WRITE | VM_SHARED | VM_STACK)) == VM_WRITE)
1020a1f0dacaSLorenzo Stoakes 
vm_stat_account(struct mm_struct * mm,vm_flags_t flags,long npages)1021a1f0dacaSLorenzo Stoakes static inline void vm_stat_account(struct mm_struct *mm, vm_flags_t flags,
1022a1f0dacaSLorenzo Stoakes 				   long npages)
1023a1f0dacaSLorenzo Stoakes {
1024a1f0dacaSLorenzo Stoakes 	WRITE_ONCE(mm->total_vm, READ_ONCE(mm->total_vm)+npages);
1025a1f0dacaSLorenzo Stoakes 
1026a1f0dacaSLorenzo Stoakes 	if (is_exec_mapping(flags))
1027a1f0dacaSLorenzo Stoakes 		mm->exec_vm += npages;
1028a1f0dacaSLorenzo Stoakes 	else if (is_stack_mapping(flags))
1029a1f0dacaSLorenzo Stoakes 		mm->stack_vm += npages;
1030a1f0dacaSLorenzo Stoakes 	else if (is_data_mapping(flags))
1031a1f0dacaSLorenzo Stoakes 		mm->data_vm += npages;
1032a1f0dacaSLorenzo Stoakes }
1033a1f0dacaSLorenzo Stoakes 
1034a1f0dacaSLorenzo Stoakes #undef is_exec_mapping
1035a1f0dacaSLorenzo Stoakes #undef is_stack_mapping
1036a1f0dacaSLorenzo Stoakes #undef is_data_mapping
1037a1f0dacaSLorenzo Stoakes 
vm_unacct_memory(long pages)1038a1f0dacaSLorenzo Stoakes static inline void vm_unacct_memory(long pages)
1039a1f0dacaSLorenzo Stoakes {
1040a1f0dacaSLorenzo Stoakes 	vm_acct_memory(-pages);
1041a1f0dacaSLorenzo Stoakes }
1042a1f0dacaSLorenzo Stoakes 
mapping_allow_writable(struct address_space * mapping)1043a1f0dacaSLorenzo Stoakes static inline void mapping_allow_writable(struct address_space *mapping)
1044a1f0dacaSLorenzo Stoakes {
1045a1f0dacaSLorenzo Stoakes 	atomic_inc(&mapping->i_mmap_writable);
1046a1f0dacaSLorenzo Stoakes }
1047a1f0dacaSLorenzo Stoakes 
1048a1f0dacaSLorenzo Stoakes static inline
vma_find(struct vma_iterator * vmi,unsigned long max)1049a1f0dacaSLorenzo Stoakes struct vm_area_struct *vma_find(struct vma_iterator *vmi, unsigned long max)
1050a1f0dacaSLorenzo Stoakes {
1051a1f0dacaSLorenzo Stoakes 	return mas_find(&vmi->mas, max - 1);
1052a1f0dacaSLorenzo Stoakes }
1053a1f0dacaSLorenzo Stoakes 
vma_iter_clear_gfp(struct vma_iterator * vmi,unsigned long start,unsigned long end,gfp_t gfp)1054a1f0dacaSLorenzo Stoakes static inline int vma_iter_clear_gfp(struct vma_iterator *vmi,
1055a1f0dacaSLorenzo Stoakes 			unsigned long start, unsigned long end, gfp_t gfp)
1056a1f0dacaSLorenzo Stoakes {
1057a1f0dacaSLorenzo Stoakes 	__mas_set_range(&vmi->mas, start, end - 1);
1058a1f0dacaSLorenzo Stoakes 	mas_store_gfp(&vmi->mas, NULL, gfp);
1059a1f0dacaSLorenzo Stoakes 	if (unlikely(mas_is_err(&vmi->mas)))
1060a1f0dacaSLorenzo Stoakes 		return -ENOMEM;
1061a1f0dacaSLorenzo Stoakes 
1062a1f0dacaSLorenzo Stoakes 	return 0;
1063a1f0dacaSLorenzo Stoakes }
1064a1f0dacaSLorenzo Stoakes 
vma_set_anonymous(struct vm_area_struct * vma)1065a1f0dacaSLorenzo Stoakes static inline void vma_set_anonymous(struct vm_area_struct *vma)
1066a1f0dacaSLorenzo Stoakes {
1067a1f0dacaSLorenzo Stoakes 	vma->vm_ops = NULL;
1068a1f0dacaSLorenzo Stoakes }
1069a1f0dacaSLorenzo Stoakes 
1070a1f0dacaSLorenzo Stoakes /* Declared in vma.h. */
1071a1f0dacaSLorenzo Stoakes static inline void set_vma_from_desc(struct vm_area_struct *vma,
1072a1f0dacaSLorenzo Stoakes 		struct vm_area_desc *desc);
1073a1f0dacaSLorenzo Stoakes 
__compat_vma_mmap(const struct file_operations * f_op,struct file * file,struct vm_area_struct * vma)1074a1f0dacaSLorenzo Stoakes static inline int __compat_vma_mmap(const struct file_operations *f_op,
1075a1f0dacaSLorenzo Stoakes 		struct file *file, struct vm_area_struct *vma)
1076a1f0dacaSLorenzo Stoakes {
1077a1f0dacaSLorenzo Stoakes 	struct vm_area_desc desc = {
1078a1f0dacaSLorenzo Stoakes 		.mm = vma->vm_mm,
1079a1f0dacaSLorenzo Stoakes 		.file = file,
1080a1f0dacaSLorenzo Stoakes 		.start = vma->vm_start,
1081a1f0dacaSLorenzo Stoakes 		.end = vma->vm_end,
1082a1f0dacaSLorenzo Stoakes 
1083a1f0dacaSLorenzo Stoakes 		.pgoff = vma->vm_pgoff,
1084a1f0dacaSLorenzo Stoakes 		.vm_file = vma->vm_file,
1085a1f0dacaSLorenzo Stoakes 		.vm_flags = vma->vm_flags,
1086a1f0dacaSLorenzo Stoakes 		.page_prot = vma->vm_page_prot,
1087a1f0dacaSLorenzo Stoakes 
1088a1f0dacaSLorenzo Stoakes 		.action.type = MMAP_NOTHING, /* Default */
1089a1f0dacaSLorenzo Stoakes 	};
1090a1f0dacaSLorenzo Stoakes 	int err;
1091a1f0dacaSLorenzo Stoakes 
1092a1f0dacaSLorenzo Stoakes 	err = f_op->mmap_prepare(&desc);
1093a1f0dacaSLorenzo Stoakes 	if (err)
1094a1f0dacaSLorenzo Stoakes 		return err;
1095a1f0dacaSLorenzo Stoakes 
1096a1f0dacaSLorenzo Stoakes 	mmap_action_prepare(&desc.action, &desc);
1097a1f0dacaSLorenzo Stoakes 	set_vma_from_desc(vma, &desc);
1098a1f0dacaSLorenzo Stoakes 	return mmap_action_complete(&desc.action, vma);
1099a1f0dacaSLorenzo Stoakes }
1100a1f0dacaSLorenzo Stoakes 
compat_vma_mmap(struct file * file,struct vm_area_struct * vma)1101a1f0dacaSLorenzo Stoakes static inline int compat_vma_mmap(struct file *file,
1102a1f0dacaSLorenzo Stoakes 		struct vm_area_struct *vma)
1103a1f0dacaSLorenzo Stoakes {
1104a1f0dacaSLorenzo Stoakes 	return __compat_vma_mmap(file->f_op, file, vma);
1105a1f0dacaSLorenzo Stoakes }
1106a1f0dacaSLorenzo Stoakes 
1107a1f0dacaSLorenzo Stoakes 
vma_iter_init(struct vma_iterator * vmi,struct mm_struct * mm,unsigned long addr)1108a1f0dacaSLorenzo Stoakes static inline void vma_iter_init(struct vma_iterator *vmi,
1109a1f0dacaSLorenzo Stoakes 		struct mm_struct *mm, unsigned long addr)
1110a1f0dacaSLorenzo Stoakes {
1111a1f0dacaSLorenzo Stoakes 	mas_init(&vmi->mas, &mm->mm_mt, addr);
1112a1f0dacaSLorenzo Stoakes }
1113a1f0dacaSLorenzo Stoakes 
vma_pages(struct vm_area_struct * vma)1114a1f0dacaSLorenzo Stoakes static inline unsigned long vma_pages(struct vm_area_struct *vma)
1115a1f0dacaSLorenzo Stoakes {
1116a1f0dacaSLorenzo Stoakes 	return (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
1117a1f0dacaSLorenzo Stoakes }
1118a1f0dacaSLorenzo Stoakes 
1119a1f0dacaSLorenzo Stoakes static inline void mmap_assert_locked(struct mm_struct *);
find_vma_intersection(struct mm_struct * mm,unsigned long start_addr,unsigned long end_addr)1120a1f0dacaSLorenzo Stoakes static inline struct vm_area_struct *find_vma_intersection(struct mm_struct *mm,
1121a1f0dacaSLorenzo Stoakes 						unsigned long start_addr,
1122a1f0dacaSLorenzo Stoakes 						unsigned long end_addr)
1123a1f0dacaSLorenzo Stoakes {
1124a1f0dacaSLorenzo Stoakes 	unsigned long index = start_addr;
1125a1f0dacaSLorenzo Stoakes 
1126a1f0dacaSLorenzo Stoakes 	mmap_assert_locked(mm);
1127a1f0dacaSLorenzo Stoakes 	return mt_find(&mm->mm_mt, &index, end_addr - 1);
1128a1f0dacaSLorenzo Stoakes }
1129a1f0dacaSLorenzo Stoakes 
1130a1f0dacaSLorenzo Stoakes static inline
vma_lookup(struct mm_struct * mm,unsigned long addr)1131a1f0dacaSLorenzo Stoakes struct vm_area_struct *vma_lookup(struct mm_struct *mm, unsigned long addr)
1132a1f0dacaSLorenzo Stoakes {
1133a1f0dacaSLorenzo Stoakes 	return mtree_load(&mm->mm_mt, addr);
1134a1f0dacaSLorenzo Stoakes }
1135a1f0dacaSLorenzo Stoakes 
vma_prev(struct vma_iterator * vmi)1136a1f0dacaSLorenzo Stoakes static inline struct vm_area_struct *vma_prev(struct vma_iterator *vmi)
1137a1f0dacaSLorenzo Stoakes {
1138a1f0dacaSLorenzo Stoakes 	return mas_prev(&vmi->mas, 0);
1139a1f0dacaSLorenzo Stoakes }
1140a1f0dacaSLorenzo Stoakes 
vma_iter_set(struct vma_iterator * vmi,unsigned long addr)1141a1f0dacaSLorenzo Stoakes static inline void vma_iter_set(struct vma_iterator *vmi, unsigned long addr)
1142a1f0dacaSLorenzo Stoakes {
1143a1f0dacaSLorenzo Stoakes 	mas_set(&vmi->mas, addr);
1144a1f0dacaSLorenzo Stoakes }
1145a1f0dacaSLorenzo Stoakes 
vma_is_anonymous(struct vm_area_struct * vma)1146a1f0dacaSLorenzo Stoakes static inline bool vma_is_anonymous(struct vm_area_struct *vma)
1147a1f0dacaSLorenzo Stoakes {
1148a1f0dacaSLorenzo Stoakes 	return !vma->vm_ops;
1149a1f0dacaSLorenzo Stoakes }
1150a1f0dacaSLorenzo Stoakes 
1151a1f0dacaSLorenzo Stoakes /* Defined in vma.h, so temporarily define here to avoid circular dependency. */
1152a1f0dacaSLorenzo Stoakes #define vma_iter_load(vmi) \
1153a1f0dacaSLorenzo Stoakes 	mas_walk(&(vmi)->mas)
1154a1f0dacaSLorenzo Stoakes 
1155a1f0dacaSLorenzo Stoakes static inline struct vm_area_struct *
find_vma_prev(struct mm_struct * mm,unsigned long addr,struct vm_area_struct ** pprev)1156a1f0dacaSLorenzo Stoakes find_vma_prev(struct mm_struct *mm, unsigned long addr,
1157a1f0dacaSLorenzo Stoakes 			struct vm_area_struct **pprev)
1158a1f0dacaSLorenzo Stoakes {
1159a1f0dacaSLorenzo Stoakes 	struct vm_area_struct *vma;
1160a1f0dacaSLorenzo Stoakes 	VMA_ITERATOR(vmi, mm, addr);
1161a1f0dacaSLorenzo Stoakes 
1162a1f0dacaSLorenzo Stoakes 	vma = vma_iter_load(&vmi);
1163a1f0dacaSLorenzo Stoakes 	*pprev = vma_prev(&vmi);
1164a1f0dacaSLorenzo Stoakes 	if (!vma)
1165a1f0dacaSLorenzo Stoakes 		vma = vma_next(&vmi);
1166a1f0dacaSLorenzo Stoakes 	return vma;
1167a1f0dacaSLorenzo Stoakes }
1168a1f0dacaSLorenzo Stoakes 
1169a1f0dacaSLorenzo Stoakes #undef vma_iter_load
1170a1f0dacaSLorenzo Stoakes 
vma_iter_free(struct vma_iterator * vmi)1171a1f0dacaSLorenzo Stoakes static inline void vma_iter_free(struct vma_iterator *vmi)
1172a1f0dacaSLorenzo Stoakes {
1173a1f0dacaSLorenzo Stoakes 	mas_destroy(&vmi->mas);
1174a1f0dacaSLorenzo Stoakes }
1175a1f0dacaSLorenzo Stoakes 
1176a1f0dacaSLorenzo Stoakes static inline
vma_iter_next_range(struct vma_iterator * vmi)1177a1f0dacaSLorenzo Stoakes struct vm_area_struct *vma_iter_next_range(struct vma_iterator *vmi)
1178a1f0dacaSLorenzo Stoakes {
1179a1f0dacaSLorenzo Stoakes 	return mas_next_range(&vmi->mas, ULONG_MAX);
1180a1f0dacaSLorenzo Stoakes }
1181a1f0dacaSLorenzo Stoakes 
1182a1f0dacaSLorenzo Stoakes bool vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot);
1183a1f0dacaSLorenzo Stoakes 
1184a1f0dacaSLorenzo Stoakes /* Update vma->vm_page_prot to reflect vma->vm_flags. */
vma_set_page_prot(struct vm_area_struct * vma)1185a1f0dacaSLorenzo Stoakes static inline void vma_set_page_prot(struct vm_area_struct *vma)
1186a1f0dacaSLorenzo Stoakes {
1187a1f0dacaSLorenzo Stoakes 	vm_flags_t vm_flags = vma->vm_flags;
1188a1f0dacaSLorenzo Stoakes 	pgprot_t vm_page_prot;
1189a1f0dacaSLorenzo Stoakes 
1190a1f0dacaSLorenzo Stoakes 	/* testing: we inline vm_pgprot_modify() to avoid clash with vma.h. */
1191a1f0dacaSLorenzo Stoakes 	vm_page_prot = pgprot_modify(vma->vm_page_prot, vm_get_page_prot(vm_flags));
1192a1f0dacaSLorenzo Stoakes 
1193a1f0dacaSLorenzo Stoakes 	if (vma_wants_writenotify(vma, vm_page_prot)) {
1194a1f0dacaSLorenzo Stoakes 		vm_flags &= ~VM_SHARED;
1195a1f0dacaSLorenzo Stoakes 		/* testing: we inline vm_pgprot_modify() to avoid clash with vma.h. */
1196a1f0dacaSLorenzo Stoakes 		vm_page_prot = pgprot_modify(vm_page_prot, vm_get_page_prot(vm_flags));
1197a1f0dacaSLorenzo Stoakes 	}
1198a1f0dacaSLorenzo Stoakes 	/* remove_protection_ptes reads vma->vm_page_prot without mmap_lock */
1199a1f0dacaSLorenzo Stoakes 	WRITE_ONCE(vma->vm_page_prot, vm_page_prot);
1200a1f0dacaSLorenzo Stoakes }
1201a1f0dacaSLorenzo Stoakes 
stack_guard_start_gap(struct vm_area_struct * vma)1202a1f0dacaSLorenzo Stoakes static inline unsigned long stack_guard_start_gap(struct vm_area_struct *vma)
1203a1f0dacaSLorenzo Stoakes {
1204a1f0dacaSLorenzo Stoakes 	if (vma->vm_flags & VM_GROWSDOWN)
1205a1f0dacaSLorenzo Stoakes 		return stack_guard_gap;
1206a1f0dacaSLorenzo Stoakes 
1207a1f0dacaSLorenzo Stoakes 	/* See reasoning around the VM_SHADOW_STACK definition */
1208a1f0dacaSLorenzo Stoakes 	if (vma->vm_flags & VM_SHADOW_STACK)
1209a1f0dacaSLorenzo Stoakes 		return PAGE_SIZE;
1210a1f0dacaSLorenzo Stoakes 
1211a1f0dacaSLorenzo Stoakes 	return 0;
1212a1f0dacaSLorenzo Stoakes }
1213a1f0dacaSLorenzo Stoakes 
vm_start_gap(struct vm_area_struct * vma)1214a1f0dacaSLorenzo Stoakes static inline unsigned long vm_start_gap(struct vm_area_struct *vma)
1215a1f0dacaSLorenzo Stoakes {
1216a1f0dacaSLorenzo Stoakes 	unsigned long gap = stack_guard_start_gap(vma);
1217a1f0dacaSLorenzo Stoakes 	unsigned long vm_start = vma->vm_start;
1218a1f0dacaSLorenzo Stoakes 
1219a1f0dacaSLorenzo Stoakes 	vm_start -= gap;
1220a1f0dacaSLorenzo Stoakes 	if (vm_start > vma->vm_start)
1221a1f0dacaSLorenzo Stoakes 		vm_start = 0;
1222a1f0dacaSLorenzo Stoakes 	return vm_start;
1223a1f0dacaSLorenzo Stoakes }
1224a1f0dacaSLorenzo Stoakes 
vm_end_gap(struct vm_area_struct * vma)1225a1f0dacaSLorenzo Stoakes static inline unsigned long vm_end_gap(struct vm_area_struct *vma)
1226a1f0dacaSLorenzo Stoakes {
1227a1f0dacaSLorenzo Stoakes 	unsigned long vm_end = vma->vm_end;
1228a1f0dacaSLorenzo Stoakes 
1229a1f0dacaSLorenzo Stoakes 	if (vma->vm_flags & VM_GROWSUP) {
1230a1f0dacaSLorenzo Stoakes 		vm_end += stack_guard_gap;
1231a1f0dacaSLorenzo Stoakes 		if (vm_end < vma->vm_end)
1232a1f0dacaSLorenzo Stoakes 			vm_end = -PAGE_SIZE;
1233a1f0dacaSLorenzo Stoakes 	}
1234a1f0dacaSLorenzo Stoakes 	return vm_end;
1235a1f0dacaSLorenzo Stoakes }
1236a1f0dacaSLorenzo Stoakes 
vma_is_accessible(struct vm_area_struct * vma)1237a1f0dacaSLorenzo Stoakes static inline bool vma_is_accessible(struct vm_area_struct *vma)
1238a1f0dacaSLorenzo Stoakes {
1239a1f0dacaSLorenzo Stoakes 	return vma->vm_flags & VM_ACCESS_FLAGS;
1240a1f0dacaSLorenzo Stoakes }
1241a1f0dacaSLorenzo Stoakes 
mlock_future_ok(const struct mm_struct * mm,vm_flags_t vm_flags,unsigned long bytes)1242a1f0dacaSLorenzo Stoakes static inline bool mlock_future_ok(const struct mm_struct *mm,
1243a1f0dacaSLorenzo Stoakes 		vm_flags_t vm_flags, unsigned long bytes)
1244a1f0dacaSLorenzo Stoakes {
1245a1f0dacaSLorenzo Stoakes 	unsigned long locked_pages, limit_pages;
1246a1f0dacaSLorenzo Stoakes 
1247a1f0dacaSLorenzo Stoakes 	if (!(vm_flags & VM_LOCKED) || capable(CAP_IPC_LOCK))
1248a1f0dacaSLorenzo Stoakes 		return true;
1249a1f0dacaSLorenzo Stoakes 
1250a1f0dacaSLorenzo Stoakes 	locked_pages = bytes >> PAGE_SHIFT;
1251a1f0dacaSLorenzo Stoakes 	locked_pages += mm->locked_vm;
1252a1f0dacaSLorenzo Stoakes 
1253a1f0dacaSLorenzo Stoakes 	limit_pages = rlimit(RLIMIT_MEMLOCK);
1254a1f0dacaSLorenzo Stoakes 	limit_pages >>= PAGE_SHIFT;
1255a1f0dacaSLorenzo Stoakes 
1256a1f0dacaSLorenzo Stoakes 	return locked_pages <= limit_pages;
1257a1f0dacaSLorenzo Stoakes }
1258a1f0dacaSLorenzo Stoakes 
map_deny_write_exec(unsigned long old,unsigned long new)1259a1f0dacaSLorenzo Stoakes static inline bool map_deny_write_exec(unsigned long old, unsigned long new)
1260a1f0dacaSLorenzo Stoakes {
1261a1f0dacaSLorenzo Stoakes 	/* If MDWE is disabled, we have nothing to deny. */
1262a1f0dacaSLorenzo Stoakes 	if (mm_flags_test(MMF_HAS_MDWE, current->mm))
1263a1f0dacaSLorenzo Stoakes 		return false;
1264a1f0dacaSLorenzo Stoakes 
1265a1f0dacaSLorenzo Stoakes 	/* If the new VMA is not executable, we have nothing to deny. */
1266a1f0dacaSLorenzo Stoakes 	if (!(new & VM_EXEC))
1267a1f0dacaSLorenzo Stoakes 		return false;
1268a1f0dacaSLorenzo Stoakes 
1269a1f0dacaSLorenzo Stoakes 	/* Under MDWE we do not accept newly writably executable VMAs... */
1270a1f0dacaSLorenzo Stoakes 	if (new & VM_WRITE)
1271a1f0dacaSLorenzo Stoakes 		return true;
1272a1f0dacaSLorenzo Stoakes 
1273a1f0dacaSLorenzo Stoakes 	/* ...nor previously non-executable VMAs becoming executable. */
1274a1f0dacaSLorenzo Stoakes 	if (!(old & VM_EXEC))
1275a1f0dacaSLorenzo Stoakes 		return true;
1276a1f0dacaSLorenzo Stoakes 
1277a1f0dacaSLorenzo Stoakes 	return false;
1278a1f0dacaSLorenzo Stoakes }
1279a1f0dacaSLorenzo Stoakes 
mapping_map_writable(struct address_space * mapping)1280a1f0dacaSLorenzo Stoakes static inline int mapping_map_writable(struct address_space *mapping)
1281a1f0dacaSLorenzo Stoakes {
1282a1f0dacaSLorenzo Stoakes 	return atomic_inc_unless_negative(&mapping->i_mmap_writable) ?
1283a1f0dacaSLorenzo Stoakes 		0 : -EPERM;
1284a1f0dacaSLorenzo Stoakes }
1285a1f0dacaSLorenzo Stoakes 
1286a1f0dacaSLorenzo Stoakes /* Did the driver provide valid mmap hook configuration? */
can_mmap_file(struct file * file)1287a1f0dacaSLorenzo Stoakes static inline bool can_mmap_file(struct file *file)
1288a1f0dacaSLorenzo Stoakes {
1289a1f0dacaSLorenzo Stoakes 	bool has_mmap = file->f_op->mmap;
1290a1f0dacaSLorenzo Stoakes 	bool has_mmap_prepare = file->f_op->mmap_prepare;
1291a1f0dacaSLorenzo Stoakes 
1292a1f0dacaSLorenzo Stoakes 	/* Hooks are mutually exclusive. */
1293a1f0dacaSLorenzo Stoakes 	if (WARN_ON_ONCE(has_mmap && has_mmap_prepare))
1294a1f0dacaSLorenzo Stoakes 		return false;
1295a1f0dacaSLorenzo Stoakes 	if (!has_mmap && !has_mmap_prepare)
1296a1f0dacaSLorenzo Stoakes 		return false;
1297a1f0dacaSLorenzo Stoakes 
1298a1f0dacaSLorenzo Stoakes 	return true;
1299a1f0dacaSLorenzo Stoakes }
1300a1f0dacaSLorenzo Stoakes 
vfs_mmap(struct file * file,struct vm_area_struct * vma)1301a1f0dacaSLorenzo Stoakes static inline int vfs_mmap(struct file *file, struct vm_area_struct *vma)
1302a1f0dacaSLorenzo Stoakes {
1303a1f0dacaSLorenzo Stoakes 	if (file->f_op->mmap_prepare)
1304a1f0dacaSLorenzo Stoakes 		return compat_vma_mmap(file, vma);
1305a1f0dacaSLorenzo Stoakes 
1306a1f0dacaSLorenzo Stoakes 	return file->f_op->mmap(file, vma);
1307a1f0dacaSLorenzo Stoakes }
1308a1f0dacaSLorenzo Stoakes 
vfs_mmap_prepare(struct file * file,struct vm_area_desc * desc)1309a1f0dacaSLorenzo Stoakes static inline int vfs_mmap_prepare(struct file *file, struct vm_area_desc *desc)
1310a1f0dacaSLorenzo Stoakes {
1311a1f0dacaSLorenzo Stoakes 	return file->f_op->mmap_prepare(desc);
1312a1f0dacaSLorenzo Stoakes }
1313a1f0dacaSLorenzo Stoakes 
vma_set_file(struct vm_area_struct * vma,struct file * file)1314a1f0dacaSLorenzo Stoakes static inline void vma_set_file(struct vm_area_struct *vma, struct file *file)
1315a1f0dacaSLorenzo Stoakes {
1316a1f0dacaSLorenzo Stoakes 	/* Changing an anonymous vma with this is illegal */
1317a1f0dacaSLorenzo Stoakes 	get_file(file);
1318a1f0dacaSLorenzo Stoakes 	swap(vma->vm_file, file);
1319a1f0dacaSLorenzo Stoakes 	fput(file);
1320a1f0dacaSLorenzo Stoakes }
1321