blob: 7f7f8dae4b1deec32eb1079428f4b13b87dd25c7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_MMAN_H
2#define _LINUX_MMAN_H
3
David Woodhouse9cdcb562006-04-25 14:18:07 +01004#include <linux/mm.h>
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07005#include <linux/percpu_counter.h>
David Woodhouse9cdcb562006-04-25 14:18:07 +01006
Arun Sharma600634972011-07-26 16:09:06 -07007#include <linux/atomic.h>
David Howells607ca462012-10-13 10:46:48 +01008#include <uapi/linux/mman.h>
David Woodhouse9cdcb562006-04-25 14:18:07 +01009
Linus Torvalds1da177e2005-04-16 15:20:36 -070010extern int sysctl_overcommit_memory;
11extern int sysctl_overcommit_ratio;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -070012extern struct percpu_counter vm_committed_as;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Tim Chen917d9292013-07-03 15:02:44 -070014#ifdef CONFIG_SMP
15extern s32 vm_committed_as_batch;
16#else
17#define vm_committed_as_batch 0
18#endif
19
K. Y. Srinivasan997071b2012-11-15 14:34:42 -080020unsigned long vm_memory_committed(void);
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022static inline void vm_acct_memory(long pages)
23{
Tim Chen917d9292013-07-03 15:02:44 -070024 __percpu_counter_add(&vm_committed_as, pages, vm_committed_as_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025}
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27static inline void vm_unacct_memory(long pages)
28{
29 vm_acct_memory(-pages);
30}
31
32/*
Dave Kleikampb845f312008-07-08 00:28:51 +100033 * Allow architectures to handle additional protection bits
34 */
35
36#ifndef arch_calc_vm_prot_bits
37#define arch_calc_vm_prot_bits(prot) 0
38#endif
39
40#ifndef arch_vm_get_page_prot
41#define arch_vm_get_page_prot(vm_flags) __pgprot(0)
42#endif
43
44#ifndef arch_validate_prot
45/*
46 * This is called from mprotect(). PROT_GROWSDOWN and PROT_GROWSUP have
47 * already been masked out.
48 *
49 * Returns true if the prot flags are valid
50 */
51static inline int arch_validate_prot(unsigned long prot)
52{
53 return (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM)) == 0;
54}
55#define arch_validate_prot arch_validate_prot
56#endif
57
58/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * Optimisation macro. It is equivalent to:
60 * (x & bit1) ? bit2 : 0
61 * but this version is faster.
62 * ("bit1" and "bit2" must be single bits)
63 */
64#define _calc_vm_trans(x, bit1, bit2) \
65 ((bit1) <= (bit2) ? ((x) & (bit1)) * ((bit2) / (bit1)) \
66 : ((x) & (bit1)) / ((bit1) / (bit2)))
67
68/*
69 * Combine the mmap "prot" argument into "vm_flags" used internally.
70 */
71static inline unsigned long
72calc_vm_prot_bits(unsigned long prot)
73{
74 return _calc_vm_trans(prot, PROT_READ, VM_READ ) |
75 _calc_vm_trans(prot, PROT_WRITE, VM_WRITE) |
Dave Kleikampb845f312008-07-08 00:28:51 +100076 _calc_vm_trans(prot, PROT_EXEC, VM_EXEC) |
77 arch_calc_vm_prot_bits(prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80/*
81 * Combine the mmap "flags" argument into "vm_flags" used internally.
82 */
83static inline unsigned long
84calc_vm_flag_bits(unsigned long flags)
85{
86 return _calc_vm_trans(flags, MAP_GROWSDOWN, VM_GROWSDOWN ) |
87 _calc_vm_trans(flags, MAP_DENYWRITE, VM_DENYWRITE ) |
Michel Lespinasse09a9f1d2013-03-28 16:26:23 -070088 _calc_vm_trans(flags, MAP_LOCKED, VM_LOCKED );
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
Jerome Marchand00619bc2013-11-12 15:08:31 -080090
91unsigned long vm_commit_limit(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#endif /* _LINUX_MMAN_H */