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 #define LEGACY_MAP_MASK (MAP_SHARED \
53 extern int sysctl_overcommit_memory;
54 extern int sysctl_overcommit_ratio;
55 extern unsigned long sysctl_overcommit_kbytes;
56 extern struct percpu_counter vm_committed_as;
59 extern s32 vm_committed_as_batch;
60 extern void mm_compute_batch(int overcommit_policy);
62 #define vm_committed_as_batch 0
63 static inline void mm_compute_batch(int overcommit_policy)
68 unsigned long vm_memory_committed(void);
70 static inline void vm_acct_memory(long pages)
72 percpu_counter_add_batch(&vm_committed_as, pages, vm_committed_as_batch);
75 static inline void vm_unacct_memory(long pages)
77 vm_acct_memory(-pages);
81 * Allow architectures to handle additional protection and flag bits. The
82 * overriding macros must be defined in the arch-specific asm/mman.h file.
85 #ifndef arch_calc_vm_prot_bits
86 #define arch_calc_vm_prot_bits(prot, pkey) 0
89 #ifndef arch_calc_vm_flag_bits
90 #define arch_calc_vm_flag_bits(flags) 0
93 #ifndef arch_vm_get_page_prot
94 #define arch_vm_get_page_prot(vm_flags) __pgprot(0)
97 #ifndef arch_validate_prot
99 * This is called from mprotect(). PROT_GROWSDOWN and PROT_GROWSUP have
100 * already been masked out.
102 * Returns true if the prot flags are valid
104 static inline bool arch_validate_prot(unsigned long prot, unsigned long addr)
106 return (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM)) == 0;
108 #define arch_validate_prot arch_validate_prot
112 * Optimisation macro. It is equivalent to:
113 * (x & bit1) ? bit2 : 0
114 * but this version is faster.
115 * ("bit1" and "bit2" must be single bits)
117 #define _calc_vm_trans(x, bit1, bit2) \
118 ((!(bit1) || !(bit2)) ? 0 : \
119 ((bit1) <= (bit2) ? ((x) & (bit1)) * ((bit2) / (bit1)) \
120 : ((x) & (bit1)) / ((bit1) / (bit2))))
123 * Combine the mmap "prot" argument into "vm_flags" used internally.
125 static inline unsigned long
126 calc_vm_prot_bits(unsigned long prot, unsigned long pkey)
128 return _calc_vm_trans(prot, PROT_READ, VM_READ ) |
129 _calc_vm_trans(prot, PROT_WRITE, VM_WRITE) |
130 _calc_vm_trans(prot, PROT_EXEC, VM_EXEC) |
131 arch_calc_vm_prot_bits(prot, pkey);
135 * Combine the mmap "flags" argument into "vm_flags" used internally.
137 static inline unsigned long
138 calc_vm_flag_bits(unsigned long flags)
140 return _calc_vm_trans(flags, MAP_GROWSDOWN, VM_GROWSDOWN ) |
141 _calc_vm_trans(flags, MAP_DENYWRITE, VM_DENYWRITE ) |
142 _calc_vm_trans(flags, MAP_LOCKED, VM_LOCKED ) |
143 _calc_vm_trans(flags, MAP_SYNC, VM_SYNC ) |
144 arch_calc_vm_flag_bits(flags);
147 unsigned long vm_commit_limit(void);
148 #endif /* _LINUX_MMAN_H */