1 /* SPDX-License-Identifier: GPL-2.0 */
7 #include <linux/percpu_counter.h>
9 #include <linux/atomic.h>
10 #include <uapi/linux/mman.h>
13 * Arrange for legacy / undefined architecture specific flags to be
14 * ignored by mmap handling code.
23 #define MAP_HUGE_2MB 0
26 #define MAP_HUGE_1GB 0
28 #ifndef MAP_UNINITIALIZED
29 #define MAP_UNINITIALIZED 0
36 * The historical set of flags that all mmap implementations implicitly
37 * support when a ->mmap_validate() op is not provided in file_operations.
39 * MAP_EXECUTABLE and MAP_DENYWRITE are completely ignored throughout the
42 #define LEGACY_MAP_MASK (MAP_SHARED \
61 extern int sysctl_overcommit_memory
;
62 extern int sysctl_overcommit_ratio
;
63 extern unsigned long sysctl_overcommit_kbytes
;
64 extern struct percpu_counter vm_committed_as
;
67 extern s32 vm_committed_as_batch
;
68 extern void mm_compute_batch(int overcommit_policy
);
70 #define vm_committed_as_batch 0
71 static inline void mm_compute_batch(int overcommit_policy
)
76 unsigned long vm_memory_committed(void);
78 static inline void vm_acct_memory(long pages
)
80 percpu_counter_add_batch(&vm_committed_as
, pages
, vm_committed_as_batch
);
83 static inline void vm_unacct_memory(long pages
)
85 vm_acct_memory(-pages
);
89 * Allow architectures to handle additional protection and flag bits. The
90 * overriding macros must be defined in the arch-specific asm/mman.h file.
93 #ifndef arch_calc_vm_prot_bits
94 #define arch_calc_vm_prot_bits(prot, pkey) 0
97 #ifndef arch_calc_vm_flag_bits
98 #define arch_calc_vm_flag_bits(file, flags) 0
101 #ifndef arch_validate_prot
103 * This is called from mprotect(). PROT_GROWSDOWN and PROT_GROWSUP have
104 * already been masked out.
106 * Returns true if the prot flags are valid
108 static inline bool arch_validate_prot(unsigned long prot
, unsigned long addr
)
110 return (prot
& ~(PROT_READ
| PROT_WRITE
| PROT_EXEC
| PROT_SEM
)) == 0;
112 #define arch_validate_prot arch_validate_prot
115 #ifndef arch_validate_flags
117 * This is called from mmap() and mprotect() with the updated vma->vm_flags.
119 * Returns true if the VM_* flags are valid.
121 static inline bool arch_validate_flags(unsigned long flags
)
125 #define arch_validate_flags arch_validate_flags
129 * Optimisation macro. It is equivalent to:
130 * (x & bit1) ? bit2 : 0
131 * but this version is faster.
132 * ("bit1" and "bit2" must be single bits)
134 #define _calc_vm_trans(x, bit1, bit2) \
135 ((!(bit1) || !(bit2)) ? 0 : \
136 ((bit1) <= (bit2) ? ((x) & (bit1)) * ((bit2) / (bit1)) \
137 : ((x) & (bit1)) / ((bit1) / (bit2))))
140 * Combine the mmap "prot" argument into "vm_flags" used internally.
142 static inline unsigned long
143 calc_vm_prot_bits(unsigned long prot
, unsigned long pkey
)
145 return _calc_vm_trans(prot
, PROT_READ
, VM_READ
) |
146 _calc_vm_trans(prot
, PROT_WRITE
, VM_WRITE
) |
147 _calc_vm_trans(prot
, PROT_EXEC
, VM_EXEC
) |
148 arch_calc_vm_prot_bits(prot
, pkey
);
152 * Combine the mmap "flags" argument into "vm_flags" used internally.
154 static inline unsigned long
155 calc_vm_flag_bits(struct file
*file
, unsigned long flags
)
157 return _calc_vm_trans(flags
, MAP_GROWSDOWN
, VM_GROWSDOWN
) |
158 _calc_vm_trans(flags
, MAP_LOCKED
, VM_LOCKED
) |
159 _calc_vm_trans(flags
, MAP_SYNC
, VM_SYNC
) |
160 _calc_vm_trans(flags
, MAP_STACK
, VM_NOHUGEPAGE
) |
161 arch_calc_vm_flag_bits(file
, flags
);
164 unsigned long vm_commit_limit(void);
166 #ifndef arch_memory_deny_write_exec_supported
167 static inline bool arch_memory_deny_write_exec_supported(void)
171 #define arch_memory_deny_write_exec_supported arch_memory_deny_write_exec_supported
175 * Denies creating a writable executable mapping or gaining executable permissions.
177 * This denies the following:
179 * a) mmap(PROT_WRITE | PROT_EXEC)
181 * b) mmap(PROT_WRITE)
182 * mprotect(PROT_EXEC)
184 * c) mmap(PROT_WRITE)
185 * mprotect(PROT_READ)
186 * mprotect(PROT_EXEC)
188 * But allows the following:
190 * d) mmap(PROT_READ | PROT_EXEC)
191 * mmap(PROT_READ | PROT_EXEC | PROT_BTI)
193 * This is only applicable if the user has set the Memory-Deny-Write-Execute
194 * (MDWE) protection mask for the current process.
196 * @old specifies the VMA flags the VMA originally possessed, and @new the ones
199 * Return: false if proposed change is OK, true if not ok and should be denied.
201 static inline bool map_deny_write_exec(unsigned long old
, unsigned long new)
203 /* If MDWE is disabled, we have nothing to deny. */
204 if (!test_bit(MMF_HAS_MDWE
, ¤t
->mm
->flags
))
207 /* If the new VMA is not executable, we have nothing to deny. */
208 if (!(new & VM_EXEC
))
211 /* Under MDWE we do not accept newly writably executable VMAs... */
215 /* ...nor previously non-executable VMAs becoming executable. */
216 if (!(old
& VM_EXEC
))
222 #endif /* _LINUX_MMAN_H */