1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/percpu_counter.h>
8 #include <linux/atomic.h>
9 #include <uapi/linux/mman.h>
12 * Arrange for legacy / undefined architecture specific flags to be
13 * ignored by mmap handling code.
19 #define MAP_HUGE_2MB 0
22 #define MAP_HUGE_1GB 0
24 #ifndef MAP_UNINITIALIZED
25 #define MAP_UNINITIALIZED 0
32 * The historical set of flags that all mmap implementations implicitly
33 * support when a ->mmap_validate() op is not provided in file_operations.
35 * MAP_EXECUTABLE and MAP_DENYWRITE are completely ignored throughout the
38 #define LEGACY_MAP_MASK (MAP_SHARED \
56 extern int sysctl_overcommit_memory;
57 extern int sysctl_overcommit_ratio;
58 extern unsigned long sysctl_overcommit_kbytes;
59 extern struct percpu_counter vm_committed_as;
62 extern s32 vm_committed_as_batch;
63 extern void mm_compute_batch(int overcommit_policy);
65 #define vm_committed_as_batch 0
66 static inline void mm_compute_batch(int overcommit_policy)
71 unsigned long vm_memory_committed(void);
73 static inline void vm_acct_memory(long pages)
75 percpu_counter_add_batch(&vm_committed_as, pages, vm_committed_as_batch);
78 static inline void vm_unacct_memory(long pages)
80 vm_acct_memory(-pages);
84 * Allow architectures to handle additional protection and flag bits. The
85 * overriding macros must be defined in the arch-specific asm/mman.h file.
88 #ifndef arch_calc_vm_prot_bits
89 #define arch_calc_vm_prot_bits(prot, pkey) 0
92 #ifndef arch_calc_vm_flag_bits
93 #define arch_calc_vm_flag_bits(flags) 0
96 #ifndef arch_validate_prot
98 * This is called from mprotect(). PROT_GROWSDOWN and PROT_GROWSUP have
99 * already been masked out.
101 * Returns true if the prot flags are valid
103 static inline bool arch_validate_prot(unsigned long prot, unsigned long addr)
105 return (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM)) == 0;
107 #define arch_validate_prot arch_validate_prot
110 #ifndef arch_validate_flags
112 * This is called from mmap() and mprotect() with the updated vma->vm_flags.
114 * Returns true if the VM_* flags are valid.
116 static inline bool arch_validate_flags(unsigned long flags)
120 #define arch_validate_flags arch_validate_flags
124 * Optimisation macro. It is equivalent to:
125 * (x & bit1) ? bit2 : 0
126 * but this version is faster.
127 * ("bit1" and "bit2" must be single bits)
129 #define _calc_vm_trans(x, bit1, bit2) \
130 ((!(bit1) || !(bit2)) ? 0 : \
131 ((bit1) <= (bit2) ? ((x) & (bit1)) * ((bit2) / (bit1)) \
132 : ((x) & (bit1)) / ((bit1) / (bit2))))
135 * Combine the mmap "prot" argument into "vm_flags" used internally.
137 static inline unsigned long
138 calc_vm_prot_bits(unsigned long prot, unsigned long pkey)
140 return _calc_vm_trans(prot, PROT_READ, VM_READ ) |
141 _calc_vm_trans(prot, PROT_WRITE, VM_WRITE) |
142 _calc_vm_trans(prot, PROT_EXEC, VM_EXEC) |
143 arch_calc_vm_prot_bits(prot, pkey);
147 * Combine the mmap "flags" argument into "vm_flags" used internally.
149 static inline unsigned long
150 calc_vm_flag_bits(unsigned long flags)
152 return _calc_vm_trans(flags, MAP_GROWSDOWN, VM_GROWSDOWN ) |
153 _calc_vm_trans(flags, MAP_LOCKED, VM_LOCKED ) |
154 _calc_vm_trans(flags, MAP_SYNC, VM_SYNC ) |
155 arch_calc_vm_flag_bits(flags);
158 unsigned long vm_commit_limit(void);
161 * Denies creating a writable executable mapping or gaining executable permissions.
163 * This denies the following:
165 * a) mmap(PROT_WRITE | PROT_EXEC)
167 * b) mmap(PROT_WRITE)
168 * mprotect(PROT_EXEC)
170 * c) mmap(PROT_WRITE)
171 * mprotect(PROT_READ)
172 * mprotect(PROT_EXEC)
174 * But allows the following:
176 * d) mmap(PROT_READ | PROT_EXEC)
177 * mmap(PROT_READ | PROT_EXEC | PROT_BTI)
179 static inline bool map_deny_write_exec(struct vm_area_struct *vma, unsigned long vm_flags)
181 if (!test_bit(MMF_HAS_MDWE, ¤t->mm->flags))
184 if ((vm_flags & VM_EXEC) && (vm_flags & VM_WRITE))
187 if (!(vma->vm_flags & VM_EXEC) && (vm_flags & VM_EXEC))
193 #endif /* _LINUX_MMAN_H */