1 #ifndef _LINUX_MMAN_H
2 #define _LINUX_MMAN_H
3
4 #include <linux/mm.h>
5 #include <linux/percpu_counter.h>
6
7 #include <linux/atomic.h>
8 #include <uapi/linux/mman.h>
9
10 extern int sysctl_overcommit_memory;
11 extern int sysctl_overcommit_ratio;
12 extern unsigned long sysctl_overcommit_kbytes;
13 extern struct percpu_counter vm_committed_as;
14
15 #ifdef CONFIG_SMP
16 extern s32 vm_committed_as_batch;
17 #else
18 #define vm_committed_as_batch 0
19 #endif
20
21 unsigned long vm_memory_committed(void);
22
vm_acct_memory(long pages)23 static inline void vm_acct_memory(long pages)
24 {
25 __percpu_counter_add(&vm_committed_as, pages, vm_committed_as_batch);
26 }
27
vm_unacct_memory(long pages)28 static inline void vm_unacct_memory(long pages)
29 {
30 vm_acct_memory(-pages);
31 }
32
33 /*
34 * Allow architectures to handle additional protection bits
35 */
36
37 #ifndef arch_calc_vm_prot_bits
38 #define arch_calc_vm_prot_bits(prot) 0
39 #endif
40
41 #ifndef arch_vm_get_page_prot
42 #define arch_vm_get_page_prot(vm_flags) __pgprot(0)
43 #endif
44
45 #ifndef arch_validate_prot
46 /*
47 * This is called from mprotect(). PROT_GROWSDOWN and PROT_GROWSUP have
48 * already been masked out.
49 *
50 * Returns true if the prot flags are valid
51 */
arch_validate_prot(unsigned long prot)52 static inline int arch_validate_prot(unsigned long prot)
53 {
54 return (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM)) == 0;
55 }
56 #define arch_validate_prot arch_validate_prot
57 #endif
58
59 /*
60 * Optimisation macro. It is equivalent to:
61 * (x & bit1) ? bit2 : 0
62 * but this version is faster.
63 * ("bit1" and "bit2" must be single bits)
64 */
65 #define _calc_vm_trans(x, bit1, bit2) \
66 ((!(bit1) || !(bit2)) ? 0 : \
67 ((bit1) <= (bit2) ? ((x) & (bit1)) * ((bit2) / (bit1)) \
68 : ((x) & (bit1)) / ((bit1) / (bit2))))
69
70 /*
71 * Combine the mmap "prot" argument into "vm_flags" used internally.
72 */
73 static inline unsigned long
calc_vm_prot_bits(unsigned long prot)74 calc_vm_prot_bits(unsigned long prot)
75 {
76 return _calc_vm_trans(prot, PROT_READ, VM_READ ) |
77 _calc_vm_trans(prot, PROT_WRITE, VM_WRITE) |
78 _calc_vm_trans(prot, PROT_EXEC, VM_EXEC) |
79 arch_calc_vm_prot_bits(prot);
80 }
81
82 /*
83 * Combine the mmap "flags" argument into "vm_flags" used internally.
84 */
85 static inline unsigned long
calc_vm_flag_bits(unsigned long flags)86 calc_vm_flag_bits(unsigned long flags)
87 {
88 return _calc_vm_trans(flags, MAP_GROWSDOWN, VM_GROWSDOWN ) |
89 _calc_vm_trans(flags, MAP_DENYWRITE, VM_DENYWRITE ) |
90 _calc_vm_trans(flags, MAP_LOCKED, VM_LOCKED );
91 }
92
93 unsigned long vm_commit_limit(void);
94 #endif /* _LINUX_MMAN_H */
95