xref: /linux/tools/testing/vma/vma_internal.h (revision d8a142058f39a32ae6c7cd5a786c656133c717fb)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * vma_internal.h
4  *
5  * Header providing userland wrappers and shims for the functionality provided
6  * by mm/vma_internal.h.
7  *
8  * We make the header guard the same as mm/vma_internal.h, so if this shim
9  * header is included, it precludes the inclusion of the kernel one.
10  */
11 
12 #ifndef __MM_VMA_INTERNAL_H
13 #define __MM_VMA_INTERNAL_H
14 
15 #define __private
16 #define __bitwise
17 #define __randomize_layout
18 
19 #define CONFIG_MMU
20 #define CONFIG_PER_VMA_LOCK
21 
22 #include <stdlib.h>
23 
24 #include <linux/list.h>
25 #include <linux/maple_tree.h>
26 #include <linux/mm.h>
27 #include <linux/rbtree.h>
28 #include <linux/rwsem.h>
29 
30 extern unsigned long stack_guard_gap;
31 #ifdef CONFIG_MMU
32 extern unsigned long mmap_min_addr;
33 extern unsigned long dac_mmap_min_addr;
34 #else
35 #define mmap_min_addr		0UL
36 #define dac_mmap_min_addr	0UL
37 #endif
38 
39 #define VM_WARN_ON(_expr) (WARN_ON(_expr))
40 #define VM_WARN_ON_ONCE(_expr) (WARN_ON_ONCE(_expr))
41 #define VM_BUG_ON(_expr) (BUG_ON(_expr))
42 #define VM_BUG_ON_VMA(_expr, _vma) (BUG_ON(_expr))
43 
44 #define VM_NONE		0x00000000
45 #define VM_READ		0x00000001
46 #define VM_WRITE	0x00000002
47 #define VM_EXEC		0x00000004
48 #define VM_SHARED	0x00000008
49 #define VM_MAYREAD	0x00000010
50 #define VM_MAYWRITE	0x00000020
51 #define VM_MAYEXEC	0x00000040
52 #define VM_GROWSDOWN	0x00000100
53 #define VM_PFNMAP	0x00000400
54 #define VM_LOCKED	0x00002000
55 #define VM_IO           0x00004000
56 #define VM_DONTEXPAND	0x00040000
57 #define VM_LOCKONFAULT	0x00080000
58 #define VM_ACCOUNT	0x00100000
59 #define VM_NORESERVE	0x00200000
60 #define VM_MIXEDMAP	0x10000000
61 #define VM_STACK	VM_GROWSDOWN
62 #define VM_SHADOW_STACK	VM_NONE
63 #define VM_SOFTDIRTY	0
64 #define VM_ARCH_1	0x01000000	/* Architecture-specific flag */
65 #define VM_GROWSUP	VM_NONE
66 
67 #define VM_ACCESS_FLAGS (VM_READ | VM_WRITE | VM_EXEC)
68 #define VM_SPECIAL (VM_IO | VM_DONTEXPAND | VM_PFNMAP | VM_MIXEDMAP)
69 
70 /* This mask represents all the VMA flag bits used by mlock */
71 #define VM_LOCKED_MASK	(VM_LOCKED | VM_LOCKONFAULT)
72 
73 #define TASK_EXEC ((current->personality & READ_IMPLIES_EXEC) ? VM_EXEC : 0)
74 
75 #define VM_DATA_FLAGS_TSK_EXEC	(VM_READ | VM_WRITE | TASK_EXEC | \
76 				 VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
77 
78 #define VM_DATA_DEFAULT_FLAGS	VM_DATA_FLAGS_TSK_EXEC
79 
80 #define VM_STARTGAP_FLAGS (VM_GROWSDOWN | VM_SHADOW_STACK)
81 
82 #define RLIMIT_STACK		3	/* max stack size */
83 #define RLIMIT_MEMLOCK		8	/* max locked-in-memory address space */
84 
85 #define CAP_IPC_LOCK         14
86 
87 #ifdef CONFIG_64BIT
88 /* VM is sealed, in vm_flags */
89 #define VM_SEALED	_BITUL(63)
90 #endif
91 
92 #define FIRST_USER_ADDRESS	0UL
93 #define USER_PGTABLES_CEILING	0UL
94 
95 #define vma_policy(vma) NULL
96 
97 #define down_write_nest_lock(sem, nest_lock)
98 
99 #define pgprot_val(x)		((x).pgprot)
100 #define __pgprot(x)		((pgprot_t) { (x) } )
101 
102 #define for_each_vma(__vmi, __vma)					\
103 	while (((__vma) = vma_next(&(__vmi))) != NULL)
104 
105 /* The MM code likes to work with exclusive end addresses */
106 #define for_each_vma_range(__vmi, __vma, __end)				\
107 	while (((__vma) = vma_find(&(__vmi), (__end))) != NULL)
108 
109 #define offset_in_page(p)	((unsigned long)(p) & ~PAGE_MASK)
110 
111 #define PHYS_PFN(x)	((unsigned long)((x) >> PAGE_SHIFT))
112 
113 #define test_and_set_bit(nr, addr) __test_and_set_bit(nr, addr)
114 #define test_and_clear_bit(nr, addr) __test_and_clear_bit(nr, addr)
115 
116 #define TASK_SIZE ((1ul << 47)-PAGE_SIZE)
117 
118 #define AS_MM_ALL_LOCKS 2
119 
120 /* We hardcode this for now. */
121 #define sysctl_max_map_count 0x1000000UL
122 
123 #define pgoff_t unsigned long
124 typedef unsigned long	pgprotval_t;
125 typedef struct pgprot { pgprotval_t pgprot; } pgprot_t;
126 typedef unsigned long vm_flags_t;
127 typedef __bitwise unsigned int vm_fault_t;
128 
129 /*
130  * The shared stubs do not implement this, it amounts to an fprintf(STDERR,...)
131  * either way :)
132  */
133 #define pr_warn_once pr_err
134 
135 typedef struct refcount_struct {
136 	atomic_t refs;
137 } refcount_t;
138 
139 struct kref {
140 	refcount_t refcount;
141 };
142 
143 /*
144  * Define the task command name length as enum, then it can be visible to
145  * BPF programs.
146  */
147 enum {
148 	TASK_COMM_LEN = 16,
149 };
150 
151 /*
152  * Flags for bug emulation.
153  *
154  * These occupy the top three bytes.
155  */
156 enum {
157 	READ_IMPLIES_EXEC =	0x0400000,
158 };
159 
160 struct task_struct {
161 	char comm[TASK_COMM_LEN];
162 	pid_t pid;
163 	struct mm_struct *mm;
164 
165 	/* Used for emulating ABI behavior of previous Linux versions: */
166 	unsigned int			personality;
167 };
168 
169 struct task_struct *get_current(void);
170 #define current get_current()
171 
172 struct anon_vma {
173 	struct anon_vma *root;
174 	struct rb_root_cached rb_root;
175 
176 	/* Test fields. */
177 	bool was_cloned;
178 	bool was_unlinked;
179 };
180 
181 struct anon_vma_chain {
182 	struct anon_vma *anon_vma;
183 	struct list_head same_vma;
184 };
185 
186 struct anon_vma_name {
187 	struct kref kref;
188 	/* The name needs to be at the end because it is dynamically sized. */
189 	char name[];
190 };
191 
192 struct vma_iterator {
193 	struct ma_state mas;
194 };
195 
196 #define VMA_ITERATOR(name, __mm, __addr)				\
197 	struct vma_iterator name = {					\
198 		.mas = {						\
199 			.tree = &(__mm)->mm_mt,				\
200 			.index = __addr,				\
201 			.node = NULL,					\
202 			.status = ma_start,				\
203 		},							\
204 	}
205 
206 struct address_space {
207 	struct rb_root_cached	i_mmap;
208 	unsigned long		flags;
209 	atomic_t		i_mmap_writable;
210 };
211 
212 struct vm_userfaultfd_ctx {};
213 struct mempolicy {};
214 struct mmu_gather {};
215 struct mutex {};
216 #define DEFINE_MUTEX(mutexname) \
217 	struct mutex mutexname = {}
218 
219 struct mm_struct {
220 	struct maple_tree mm_mt;
221 	int map_count;			/* number of VMAs */
222 	unsigned long total_vm;	   /* Total pages mapped */
223 	unsigned long locked_vm;   /* Pages that have PG_mlocked set */
224 	unsigned long data_vm;	   /* VM_WRITE & ~VM_SHARED & ~VM_STACK */
225 	unsigned long exec_vm;	   /* VM_EXEC & ~VM_WRITE & ~VM_STACK */
226 	unsigned long stack_vm;	   /* VM_STACK */
227 
228 	unsigned long def_flags;
229 };
230 
231 struct vma_lock {
232 	struct rw_semaphore lock;
233 };
234 
235 
236 struct file {
237 	struct address_space	*f_mapping;
238 };
239 
240 struct vm_area_struct {
241 	/* The first cache line has the info for VMA tree walking. */
242 
243 	union {
244 		struct {
245 			/* VMA covers [vm_start; vm_end) addresses within mm */
246 			unsigned long vm_start;
247 			unsigned long vm_end;
248 		};
249 #ifdef CONFIG_PER_VMA_LOCK
250 		struct rcu_head vm_rcu;	/* Used for deferred freeing. */
251 #endif
252 	};
253 
254 	struct mm_struct *vm_mm;	/* The address space we belong to. */
255 	pgprot_t vm_page_prot;          /* Access permissions of this VMA. */
256 
257 	/*
258 	 * Flags, see mm.h.
259 	 * To modify use vm_flags_{init|reset|set|clear|mod} functions.
260 	 */
261 	union {
262 		const vm_flags_t vm_flags;
263 		vm_flags_t __private __vm_flags;
264 	};
265 
266 #ifdef CONFIG_PER_VMA_LOCK
267 	/* Flag to indicate areas detached from the mm->mm_mt tree */
268 	bool detached;
269 
270 	/*
271 	 * Can only be written (using WRITE_ONCE()) while holding both:
272 	 *  - mmap_lock (in write mode)
273 	 *  - vm_lock->lock (in write mode)
274 	 * Can be read reliably while holding one of:
275 	 *  - mmap_lock (in read or write mode)
276 	 *  - vm_lock->lock (in read or write mode)
277 	 * Can be read unreliably (using READ_ONCE()) for pessimistic bailout
278 	 * while holding nothing (except RCU to keep the VMA struct allocated).
279 	 *
280 	 * This sequence counter is explicitly allowed to overflow; sequence
281 	 * counter reuse can only lead to occasional unnecessary use of the
282 	 * slowpath.
283 	 */
284 	unsigned int vm_lock_seq;
285 	struct vma_lock *vm_lock;
286 #endif
287 
288 	/*
289 	 * For areas with an address space and backing store,
290 	 * linkage into the address_space->i_mmap interval tree.
291 	 *
292 	 */
293 	struct {
294 		struct rb_node rb;
295 		unsigned long rb_subtree_last;
296 	} shared;
297 
298 	/*
299 	 * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
300 	 * list, after a COW of one of the file pages.	A MAP_SHARED vma
301 	 * can only be in the i_mmap tree.  An anonymous MAP_PRIVATE, stack
302 	 * or brk vma (with NULL file) can only be in an anon_vma list.
303 	 */
304 	struct list_head anon_vma_chain; /* Serialized by mmap_lock &
305 					  * page_table_lock */
306 	struct anon_vma *anon_vma;	/* Serialized by page_table_lock */
307 
308 	/* Function pointers to deal with this struct. */
309 	const struct vm_operations_struct *vm_ops;
310 
311 	/* Information about our backing store: */
312 	unsigned long vm_pgoff;		/* Offset (within vm_file) in PAGE_SIZE
313 					   units */
314 	struct file * vm_file;		/* File we map to (can be NULL). */
315 	void * vm_private_data;		/* was vm_pte (shared mem) */
316 
317 #ifdef CONFIG_ANON_VMA_NAME
318 	/*
319 	 * For private and shared anonymous mappings, a pointer to a null
320 	 * terminated string containing the name given to the vma, or NULL if
321 	 * unnamed. Serialized by mmap_lock. Use anon_vma_name to access.
322 	 */
323 	struct anon_vma_name *anon_name;
324 #endif
325 #ifdef CONFIG_SWAP
326 	atomic_long_t swap_readahead_info;
327 #endif
328 #ifndef CONFIG_MMU
329 	struct vm_region *vm_region;	/* NOMMU mapping region */
330 #endif
331 #ifdef CONFIG_NUMA
332 	struct mempolicy *vm_policy;	/* NUMA policy for the VMA */
333 #endif
334 #ifdef CONFIG_NUMA_BALANCING
335 	struct vma_numab_state *numab_state;	/* NUMA Balancing state */
336 #endif
337 	struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
338 } __randomize_layout;
339 
340 struct vm_fault {};
341 
342 struct vm_operations_struct {
343 	void (*open)(struct vm_area_struct * area);
344 	/**
345 	 * @close: Called when the VMA is being removed from the MM.
346 	 * Context: User context.  May sleep.  Caller holds mmap_lock.
347 	 */
348 	void (*close)(struct vm_area_struct * area);
349 	/* Called any time before splitting to check if it's allowed */
350 	int (*may_split)(struct vm_area_struct *area, unsigned long addr);
351 	int (*mremap)(struct vm_area_struct *area);
352 	/*
353 	 * Called by mprotect() to make driver-specific permission
354 	 * checks before mprotect() is finalised.   The VMA must not
355 	 * be modified.  Returns 0 if mprotect() can proceed.
356 	 */
357 	int (*mprotect)(struct vm_area_struct *vma, unsigned long start,
358 			unsigned long end, unsigned long newflags);
359 	vm_fault_t (*fault)(struct vm_fault *vmf);
360 	vm_fault_t (*huge_fault)(struct vm_fault *vmf, unsigned int order);
361 	vm_fault_t (*map_pages)(struct vm_fault *vmf,
362 			pgoff_t start_pgoff, pgoff_t end_pgoff);
363 	unsigned long (*pagesize)(struct vm_area_struct * area);
364 
365 	/* notification that a previously read-only page is about to become
366 	 * writable, if an error is returned it will cause a SIGBUS */
367 	vm_fault_t (*page_mkwrite)(struct vm_fault *vmf);
368 
369 	/* same as page_mkwrite when using VM_PFNMAP|VM_MIXEDMAP */
370 	vm_fault_t (*pfn_mkwrite)(struct vm_fault *vmf);
371 
372 	/* called by access_process_vm when get_user_pages() fails, typically
373 	 * for use by special VMAs. See also generic_access_phys() for a generic
374 	 * implementation useful for any iomem mapping.
375 	 */
376 	int (*access)(struct vm_area_struct *vma, unsigned long addr,
377 		      void *buf, int len, int write);
378 
379 	/* Called by the /proc/PID/maps code to ask the vma whether it
380 	 * has a special name.  Returning non-NULL will also cause this
381 	 * vma to be dumped unconditionally. */
382 	const char *(*name)(struct vm_area_struct *vma);
383 
384 #ifdef CONFIG_NUMA
385 	/*
386 	 * set_policy() op must add a reference to any non-NULL @new mempolicy
387 	 * to hold the policy upon return.  Caller should pass NULL @new to
388 	 * remove a policy and fall back to surrounding context--i.e. do not
389 	 * install a MPOL_DEFAULT policy, nor the task or system default
390 	 * mempolicy.
391 	 */
392 	int (*set_policy)(struct vm_area_struct *vma, struct mempolicy *new);
393 
394 	/*
395 	 * get_policy() op must add reference [mpol_get()] to any policy at
396 	 * (vma,addr) marked as MPOL_SHARED.  The shared policy infrastructure
397 	 * in mm/mempolicy.c will do this automatically.
398 	 * get_policy() must NOT add a ref if the policy at (vma,addr) is not
399 	 * marked as MPOL_SHARED. vma policies are protected by the mmap_lock.
400 	 * If no [shared/vma] mempolicy exists at the addr, get_policy() op
401 	 * must return NULL--i.e., do not "fallback" to task or system default
402 	 * policy.
403 	 */
404 	struct mempolicy *(*get_policy)(struct vm_area_struct *vma,
405 					unsigned long addr, pgoff_t *ilx);
406 #endif
407 	/*
408 	 * Called by vm_normal_page() for special PTEs to find the
409 	 * page for @addr.  This is useful if the default behavior
410 	 * (using pte_page()) would not find the correct page.
411 	 */
412 	struct page *(*find_special_page)(struct vm_area_struct *vma,
413 					  unsigned long addr);
414 };
415 
416 struct vm_unmapped_area_info {
417 #define VM_UNMAPPED_AREA_TOPDOWN 1
418 	unsigned long flags;
419 	unsigned long length;
420 	unsigned long low_limit;
421 	unsigned long high_limit;
422 	unsigned long align_mask;
423 	unsigned long align_offset;
424 	unsigned long start_gap;
425 };
426 
427 static inline void vma_iter_invalidate(struct vma_iterator *vmi)
428 {
429 	mas_pause(&vmi->mas);
430 }
431 
432 static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
433 {
434 	return __pgprot(pgprot_val(oldprot) | pgprot_val(newprot));
435 }
436 
437 static inline pgprot_t vm_get_page_prot(unsigned long vm_flags)
438 {
439 	return __pgprot(vm_flags);
440 }
441 
442 static inline bool is_shared_maywrite(vm_flags_t vm_flags)
443 {
444 	return (vm_flags & (VM_SHARED | VM_MAYWRITE)) ==
445 		(VM_SHARED | VM_MAYWRITE);
446 }
447 
448 static inline bool vma_is_shared_maywrite(struct vm_area_struct *vma)
449 {
450 	return is_shared_maywrite(vma->vm_flags);
451 }
452 
453 static inline struct vm_area_struct *vma_next(struct vma_iterator *vmi)
454 {
455 	/*
456 	 * Uses mas_find() to get the first VMA when the iterator starts.
457 	 * Calling mas_next() could skip the first entry.
458 	 */
459 	return mas_find(&vmi->mas, ULONG_MAX);
460 }
461 
462 static inline bool vma_lock_alloc(struct vm_area_struct *vma)
463 {
464 	vma->vm_lock = calloc(1, sizeof(struct vma_lock));
465 
466 	if (!vma->vm_lock)
467 		return false;
468 
469 	init_rwsem(&vma->vm_lock->lock);
470 	vma->vm_lock_seq = UINT_MAX;
471 
472 	return true;
473 }
474 
475 static inline void vma_assert_write_locked(struct vm_area_struct *);
476 static inline void vma_mark_detached(struct vm_area_struct *vma, bool detached)
477 {
478 	/* When detaching vma should be write-locked */
479 	if (detached)
480 		vma_assert_write_locked(vma);
481 	vma->detached = detached;
482 }
483 
484 extern const struct vm_operations_struct vma_dummy_vm_ops;
485 
486 extern unsigned long rlimit(unsigned int limit);
487 
488 static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm)
489 {
490 	memset(vma, 0, sizeof(*vma));
491 	vma->vm_mm = mm;
492 	vma->vm_ops = &vma_dummy_vm_ops;
493 	INIT_LIST_HEAD(&vma->anon_vma_chain);
494 	vma_mark_detached(vma, false);
495 }
496 
497 static inline struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)
498 {
499 	struct vm_area_struct *vma = calloc(1, sizeof(struct vm_area_struct));
500 
501 	if (!vma)
502 		return NULL;
503 
504 	vma_init(vma, mm);
505 	if (!vma_lock_alloc(vma)) {
506 		free(vma);
507 		return NULL;
508 	}
509 
510 	return vma;
511 }
512 
513 static inline struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig)
514 {
515 	struct vm_area_struct *new = calloc(1, sizeof(struct vm_area_struct));
516 
517 	if (!new)
518 		return NULL;
519 
520 	memcpy(new, orig, sizeof(*new));
521 	if (!vma_lock_alloc(new)) {
522 		free(new);
523 		return NULL;
524 	}
525 	INIT_LIST_HEAD(&new->anon_vma_chain);
526 
527 	return new;
528 }
529 
530 /*
531  * These are defined in vma.h, but sadly vm_stat_account() is referenced by
532  * kernel/fork.c, so we have to these broadly available there, and temporarily
533  * define them here to resolve the dependency cycle.
534  */
535 
536 #define is_exec_mapping(flags) \
537 	((flags & (VM_EXEC | VM_WRITE | VM_STACK)) == VM_EXEC)
538 
539 #define is_stack_mapping(flags) \
540 	(((flags & VM_STACK) == VM_STACK) || (flags & VM_SHADOW_STACK))
541 
542 #define is_data_mapping(flags) \
543 	((flags & (VM_WRITE | VM_SHARED | VM_STACK)) == VM_WRITE)
544 
545 static inline void vm_stat_account(struct mm_struct *mm, vm_flags_t flags,
546 				   long npages)
547 {
548 	WRITE_ONCE(mm->total_vm, READ_ONCE(mm->total_vm)+npages);
549 
550 	if (is_exec_mapping(flags))
551 		mm->exec_vm += npages;
552 	else if (is_stack_mapping(flags))
553 		mm->stack_vm += npages;
554 	else if (is_data_mapping(flags))
555 		mm->data_vm += npages;
556 }
557 
558 #undef is_exec_mapping
559 #undef is_stack_mapping
560 #undef is_data_mapping
561 
562 /* Currently stubbed but we may later wish to un-stub. */
563 static inline void vm_acct_memory(long pages);
564 static inline void vm_unacct_memory(long pages)
565 {
566 	vm_acct_memory(-pages);
567 }
568 
569 static inline void mapping_allow_writable(struct address_space *mapping)
570 {
571 	atomic_inc(&mapping->i_mmap_writable);
572 }
573 
574 static inline void vma_set_range(struct vm_area_struct *vma,
575 				 unsigned long start, unsigned long end,
576 				 pgoff_t pgoff)
577 {
578 	vma->vm_start = start;
579 	vma->vm_end = end;
580 	vma->vm_pgoff = pgoff;
581 }
582 
583 static inline
584 struct vm_area_struct *vma_find(struct vma_iterator *vmi, unsigned long max)
585 {
586 	return mas_find(&vmi->mas, max - 1);
587 }
588 
589 static inline int vma_iter_clear_gfp(struct vma_iterator *vmi,
590 			unsigned long start, unsigned long end, gfp_t gfp)
591 {
592 	__mas_set_range(&vmi->mas, start, end - 1);
593 	mas_store_gfp(&vmi->mas, NULL, gfp);
594 	if (unlikely(mas_is_err(&vmi->mas)))
595 		return -ENOMEM;
596 
597 	return 0;
598 }
599 
600 static inline void mmap_assert_locked(struct mm_struct *);
601 static inline struct vm_area_struct *find_vma_intersection(struct mm_struct *mm,
602 						unsigned long start_addr,
603 						unsigned long end_addr)
604 {
605 	unsigned long index = start_addr;
606 
607 	mmap_assert_locked(mm);
608 	return mt_find(&mm->mm_mt, &index, end_addr - 1);
609 }
610 
611 static inline
612 struct vm_area_struct *vma_lookup(struct mm_struct *mm, unsigned long addr)
613 {
614 	return mtree_load(&mm->mm_mt, addr);
615 }
616 
617 static inline struct vm_area_struct *vma_prev(struct vma_iterator *vmi)
618 {
619 	return mas_prev(&vmi->mas, 0);
620 }
621 
622 static inline void vma_iter_set(struct vma_iterator *vmi, unsigned long addr)
623 {
624 	mas_set(&vmi->mas, addr);
625 }
626 
627 static inline bool vma_is_anonymous(struct vm_area_struct *vma)
628 {
629 	return !vma->vm_ops;
630 }
631 
632 /* Defined in vma.h, so temporarily define here to avoid circular dependency. */
633 #define vma_iter_load(vmi) \
634 	mas_walk(&(vmi)->mas)
635 
636 static inline struct vm_area_struct *
637 find_vma_prev(struct mm_struct *mm, unsigned long addr,
638 			struct vm_area_struct **pprev)
639 {
640 	struct vm_area_struct *vma;
641 	VMA_ITERATOR(vmi, mm, addr);
642 
643 	vma = vma_iter_load(&vmi);
644 	*pprev = vma_prev(&vmi);
645 	if (!vma)
646 		vma = vma_next(&vmi);
647 	return vma;
648 }
649 
650 #undef vma_iter_load
651 
652 static inline void vma_iter_init(struct vma_iterator *vmi,
653 		struct mm_struct *mm, unsigned long addr)
654 {
655 	mas_init(&vmi->mas, &mm->mm_mt, addr);
656 }
657 
658 /* Stubbed functions. */
659 
660 static inline struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma)
661 {
662 	return NULL;
663 }
664 
665 static inline bool is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct *vma,
666 					struct vm_userfaultfd_ctx vm_ctx)
667 {
668 	return true;
669 }
670 
671 static inline bool anon_vma_name_eq(struct anon_vma_name *anon_name1,
672 				    struct anon_vma_name *anon_name2)
673 {
674 	return true;
675 }
676 
677 static inline void might_sleep(void)
678 {
679 }
680 
681 static inline unsigned long vma_pages(struct vm_area_struct *vma)
682 {
683 	return (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
684 }
685 
686 static inline void fput(struct file *)
687 {
688 }
689 
690 static inline void mpol_put(struct mempolicy *)
691 {
692 }
693 
694 static inline void vma_lock_free(struct vm_area_struct *vma)
695 {
696 	free(vma->vm_lock);
697 }
698 
699 static inline void __vm_area_free(struct vm_area_struct *vma)
700 {
701 	vma_lock_free(vma);
702 	free(vma);
703 }
704 
705 static inline void vm_area_free(struct vm_area_struct *vma)
706 {
707 	__vm_area_free(vma);
708 }
709 
710 static inline void lru_add_drain(void)
711 {
712 }
713 
714 static inline void tlb_gather_mmu(struct mmu_gather *, struct mm_struct *)
715 {
716 }
717 
718 static inline void update_hiwater_rss(struct mm_struct *)
719 {
720 }
721 
722 static inline void update_hiwater_vm(struct mm_struct *)
723 {
724 }
725 
726 static inline void unmap_vmas(struct mmu_gather *tlb, struct ma_state *mas,
727 		      struct vm_area_struct *vma, unsigned long start_addr,
728 		      unsigned long end_addr, unsigned long tree_end,
729 		      bool mm_wr_locked)
730 {
731 	(void)tlb;
732 	(void)mas;
733 	(void)vma;
734 	(void)start_addr;
735 	(void)end_addr;
736 	(void)tree_end;
737 	(void)mm_wr_locked;
738 }
739 
740 static inline void free_pgtables(struct mmu_gather *tlb, struct ma_state *mas,
741 		   struct vm_area_struct *vma, unsigned long floor,
742 		   unsigned long ceiling, bool mm_wr_locked)
743 {
744 	(void)tlb;
745 	(void)mas;
746 	(void)vma;
747 	(void)floor;
748 	(void)ceiling;
749 	(void)mm_wr_locked;
750 }
751 
752 static inline void mapping_unmap_writable(struct address_space *)
753 {
754 }
755 
756 static inline void flush_dcache_mmap_lock(struct address_space *)
757 {
758 }
759 
760 static inline void tlb_finish_mmu(struct mmu_gather *)
761 {
762 }
763 
764 static inline struct file *get_file(struct file *f)
765 {
766 	return f;
767 }
768 
769 static inline int vma_dup_policy(struct vm_area_struct *, struct vm_area_struct *)
770 {
771 	return 0;
772 }
773 
774 static inline int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src)
775 {
776 	/* For testing purposes. We indicate that an anon_vma has been cloned. */
777 	if (src->anon_vma != NULL) {
778 		dst->anon_vma = src->anon_vma;
779 		dst->anon_vma->was_cloned = true;
780 	}
781 
782 	return 0;
783 }
784 
785 static inline void vma_start_write(struct vm_area_struct *vma)
786 {
787 	/* Used to indicate to tests that a write operation has begun. */
788 	vma->vm_lock_seq++;
789 }
790 
791 static inline void vma_adjust_trans_huge(struct vm_area_struct *vma,
792 					 unsigned long start,
793 					 unsigned long end,
794 					 long adjust_next)
795 {
796 	(void)vma;
797 	(void)start;
798 	(void)end;
799 	(void)adjust_next;
800 }
801 
802 static inline void vma_iter_free(struct vma_iterator *vmi)
803 {
804 	mas_destroy(&vmi->mas);
805 }
806 
807 static inline
808 struct vm_area_struct *vma_iter_next_range(struct vma_iterator *vmi)
809 {
810 	return mas_next_range(&vmi->mas, ULONG_MAX);
811 }
812 
813 static inline void vm_acct_memory(long pages)
814 {
815 }
816 
817 static inline void vma_interval_tree_insert(struct vm_area_struct *,
818 					    struct rb_root_cached *)
819 {
820 }
821 
822 static inline void vma_interval_tree_remove(struct vm_area_struct *,
823 					    struct rb_root_cached *)
824 {
825 }
826 
827 static inline void flush_dcache_mmap_unlock(struct address_space *)
828 {
829 }
830 
831 static inline void anon_vma_interval_tree_insert(struct anon_vma_chain*,
832 						 struct rb_root_cached *)
833 {
834 }
835 
836 static inline void anon_vma_interval_tree_remove(struct anon_vma_chain*,
837 						 struct rb_root_cached *)
838 {
839 }
840 
841 static inline void uprobe_mmap(struct vm_area_struct *)
842 {
843 }
844 
845 static inline void uprobe_munmap(struct vm_area_struct *vma,
846 				 unsigned long start, unsigned long end)
847 {
848 	(void)vma;
849 	(void)start;
850 	(void)end;
851 }
852 
853 static inline void i_mmap_lock_write(struct address_space *)
854 {
855 }
856 
857 static inline void anon_vma_lock_write(struct anon_vma *)
858 {
859 }
860 
861 static inline void vma_assert_write_locked(struct vm_area_struct *)
862 {
863 }
864 
865 static inline void unlink_anon_vmas(struct vm_area_struct *vma)
866 {
867 	/* For testing purposes, indicate that the anon_vma was unlinked. */
868 	vma->anon_vma->was_unlinked = true;
869 }
870 
871 static inline void anon_vma_unlock_write(struct anon_vma *)
872 {
873 }
874 
875 static inline void i_mmap_unlock_write(struct address_space *)
876 {
877 }
878 
879 static inline void anon_vma_merge(struct vm_area_struct *,
880 				  struct vm_area_struct *)
881 {
882 }
883 
884 static inline int userfaultfd_unmap_prep(struct vm_area_struct *vma,
885 					 unsigned long start,
886 					 unsigned long end,
887 					 struct list_head *unmaps)
888 {
889 	(void)vma;
890 	(void)start;
891 	(void)end;
892 	(void)unmaps;
893 
894 	return 0;
895 }
896 
897 static inline void mmap_write_downgrade(struct mm_struct *)
898 {
899 }
900 
901 static inline void mmap_read_unlock(struct mm_struct *)
902 {
903 }
904 
905 static inline void mmap_write_unlock(struct mm_struct *)
906 {
907 }
908 
909 static inline int mmap_write_lock_killable(struct mm_struct *)
910 {
911 	return 0;
912 }
913 
914 static inline bool can_modify_mm(struct mm_struct *mm,
915 				 unsigned long start,
916 				 unsigned long end)
917 {
918 	(void)mm;
919 	(void)start;
920 	(void)end;
921 
922 	return true;
923 }
924 
925 static inline void arch_unmap(struct mm_struct *mm,
926 				 unsigned long start,
927 				 unsigned long end)
928 {
929 	(void)mm;
930 	(void)start;
931 	(void)end;
932 }
933 
934 static inline void mmap_assert_locked(struct mm_struct *)
935 {
936 }
937 
938 static inline bool mpol_equal(struct mempolicy *, struct mempolicy *)
939 {
940 	return true;
941 }
942 
943 static inline void khugepaged_enter_vma(struct vm_area_struct *vma,
944 			  unsigned long vm_flags)
945 {
946 	(void)vma;
947 	(void)vm_flags;
948 }
949 
950 static inline bool mapping_can_writeback(struct address_space *)
951 {
952 	return true;
953 }
954 
955 static inline bool is_vm_hugetlb_page(struct vm_area_struct *)
956 {
957 	return false;
958 }
959 
960 static inline bool vma_soft_dirty_enabled(struct vm_area_struct *)
961 {
962 	return false;
963 }
964 
965 static inline bool userfaultfd_wp(struct vm_area_struct *)
966 {
967 	return false;
968 }
969 
970 static inline void mmap_assert_write_locked(struct mm_struct *)
971 {
972 }
973 
974 static inline void mutex_lock(struct mutex *)
975 {
976 }
977 
978 static inline void mutex_unlock(struct mutex *)
979 {
980 }
981 
982 static inline bool mutex_is_locked(struct mutex *)
983 {
984 	return true;
985 }
986 
987 static inline bool signal_pending(void *)
988 {
989 	return false;
990 }
991 
992 static inline bool is_file_hugepages(struct file *)
993 {
994 	return false;
995 }
996 
997 static inline int security_vm_enough_memory_mm(struct mm_struct *, long)
998 {
999 	return 0;
1000 }
1001 
1002 static inline bool may_expand_vm(struct mm_struct *, vm_flags_t, unsigned long)
1003 {
1004 	return true;
1005 }
1006 
1007 static inline void vm_flags_init(struct vm_area_struct *vma,
1008 				 vm_flags_t flags)
1009 {
1010 	vma->__vm_flags = flags;
1011 }
1012 
1013 static inline void vm_flags_set(struct vm_area_struct *vma,
1014 				vm_flags_t flags)
1015 {
1016 	vma_start_write(vma);
1017 	vma->__vm_flags |= flags;
1018 }
1019 
1020 static inline void vm_flags_clear(struct vm_area_struct *vma,
1021 				  vm_flags_t flags)
1022 {
1023 	vma_start_write(vma);
1024 	vma->__vm_flags &= ~flags;
1025 }
1026 
1027 static inline int call_mmap(struct file *, struct vm_area_struct *)
1028 {
1029 	return 0;
1030 }
1031 
1032 static inline int shmem_zero_setup(struct vm_area_struct *)
1033 {
1034 	return 0;
1035 }
1036 
1037 static inline void vma_set_anonymous(struct vm_area_struct *vma)
1038 {
1039 	vma->vm_ops = NULL;
1040 }
1041 
1042 static inline void ksm_add_vma(struct vm_area_struct *)
1043 {
1044 }
1045 
1046 static inline void perf_event_mmap(struct vm_area_struct *)
1047 {
1048 }
1049 
1050 static inline bool vma_is_dax(struct vm_area_struct *)
1051 {
1052 	return false;
1053 }
1054 
1055 static inline struct vm_area_struct *get_gate_vma(struct mm_struct *)
1056 {
1057 	return NULL;
1058 }
1059 
1060 bool vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot);
1061 
1062 /* Update vma->vm_page_prot to reflect vma->vm_flags. */
1063 static inline void vma_set_page_prot(struct vm_area_struct *vma)
1064 {
1065 	unsigned long vm_flags = vma->vm_flags;
1066 	pgprot_t vm_page_prot;
1067 
1068 	/* testing: we inline vm_pgprot_modify() to avoid clash with vma.h. */
1069 	vm_page_prot = pgprot_modify(vma->vm_page_prot, vm_get_page_prot(vm_flags));
1070 
1071 	if (vma_wants_writenotify(vma, vm_page_prot)) {
1072 		vm_flags &= ~VM_SHARED;
1073 		/* testing: we inline vm_pgprot_modify() to avoid clash with vma.h. */
1074 		vm_page_prot = pgprot_modify(vm_page_prot, vm_get_page_prot(vm_flags));
1075 	}
1076 	/* remove_protection_ptes reads vma->vm_page_prot without mmap_lock */
1077 	WRITE_ONCE(vma->vm_page_prot, vm_page_prot);
1078 }
1079 
1080 static inline bool arch_validate_flags(unsigned long)
1081 {
1082 	return true;
1083 }
1084 
1085 static inline void vma_close(struct vm_area_struct *)
1086 {
1087 }
1088 
1089 static inline int mmap_file(struct file *, struct vm_area_struct *)
1090 {
1091 	return 0;
1092 }
1093 
1094 static inline unsigned long stack_guard_start_gap(struct vm_area_struct *vma)
1095 {
1096 	if (vma->vm_flags & VM_GROWSDOWN)
1097 		return stack_guard_gap;
1098 
1099 	/* See reasoning around the VM_SHADOW_STACK definition */
1100 	if (vma->vm_flags & VM_SHADOW_STACK)
1101 		return PAGE_SIZE;
1102 
1103 	return 0;
1104 }
1105 
1106 static inline unsigned long vm_start_gap(struct vm_area_struct *vma)
1107 {
1108 	unsigned long gap = stack_guard_start_gap(vma);
1109 	unsigned long vm_start = vma->vm_start;
1110 
1111 	vm_start -= gap;
1112 	if (vm_start > vma->vm_start)
1113 		vm_start = 0;
1114 	return vm_start;
1115 }
1116 
1117 static inline unsigned long vm_end_gap(struct vm_area_struct *vma)
1118 {
1119 	unsigned long vm_end = vma->vm_end;
1120 
1121 	if (vma->vm_flags & VM_GROWSUP) {
1122 		vm_end += stack_guard_gap;
1123 		if (vm_end < vma->vm_end)
1124 			vm_end = -PAGE_SIZE;
1125 	}
1126 	return vm_end;
1127 }
1128 
1129 static inline int is_hugepage_only_range(struct mm_struct *mm,
1130 					unsigned long addr, unsigned long len)
1131 {
1132 	return 0;
1133 }
1134 
1135 static inline bool vma_is_accessible(struct vm_area_struct *vma)
1136 {
1137 	return vma->vm_flags & VM_ACCESS_FLAGS;
1138 }
1139 
1140 static inline bool capable(int cap)
1141 {
1142 	return true;
1143 }
1144 
1145 static inline bool mlock_future_ok(struct mm_struct *mm, unsigned long flags,
1146 			unsigned long bytes)
1147 {
1148 	unsigned long locked_pages, limit_pages;
1149 
1150 	if (!(flags & VM_LOCKED) || capable(CAP_IPC_LOCK))
1151 		return true;
1152 
1153 	locked_pages = bytes >> PAGE_SHIFT;
1154 	locked_pages += mm->locked_vm;
1155 
1156 	limit_pages = rlimit(RLIMIT_MEMLOCK);
1157 	limit_pages >>= PAGE_SHIFT;
1158 
1159 	return locked_pages <= limit_pages;
1160 }
1161 
1162 static inline int __anon_vma_prepare(struct vm_area_struct *vma)
1163 {
1164 	struct anon_vma *anon_vma = calloc(1, sizeof(struct anon_vma));
1165 
1166 	if (!anon_vma)
1167 		return -ENOMEM;
1168 
1169 	anon_vma->root = anon_vma;
1170 	vma->anon_vma = anon_vma;
1171 
1172 	return 0;
1173 }
1174 
1175 static inline int anon_vma_prepare(struct vm_area_struct *vma)
1176 {
1177 	if (likely(vma->anon_vma))
1178 		return 0;
1179 
1180 	return __anon_vma_prepare(vma);
1181 }
1182 
1183 static inline void userfaultfd_unmap_complete(struct mm_struct *mm,
1184 					      struct list_head *uf)
1185 {
1186 }
1187 
1188 #endif	/* __MM_VMA_INTERNAL_H */
1189