Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _LINUX_MMAN_H |
| 2 | #define _LINUX_MMAN_H |
| 3 | |
David Woodhouse | 9cdcb56 | 2006-04-25 14:18:07 +0100 | [diff] [blame] | 4 | #include <linux/mm.h> |
KOSAKI Motohiro | 00a62ce | 2009-04-30 15:08:51 -0700 | [diff] [blame] | 5 | #include <linux/percpu_counter.h> |
David Woodhouse | 9cdcb56 | 2006-04-25 14:18:07 +0100 | [diff] [blame] | 6 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 7 | #include <linux/atomic.h> |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 8 | #include <uapi/linux/mman.h> |
David Woodhouse | 9cdcb56 | 2006-04-25 14:18:07 +0100 | [diff] [blame] | 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | extern int sysctl_overcommit_memory; |
| 11 | extern int sysctl_overcommit_ratio; |
KOSAKI Motohiro | 00a62ce | 2009-04-30 15:08:51 -0700 | [diff] [blame] | 12 | extern struct percpu_counter vm_committed_as; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
K. Y. Srinivasan | 997071b | 2012-11-15 14:34:42 -0800 | [diff] [blame] | 14 | unsigned long vm_memory_committed(void); |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | static inline void vm_acct_memory(long pages) |
| 17 | { |
KOSAKI Motohiro | 00a62ce | 2009-04-30 15:08:51 -0700 | [diff] [blame] | 18 | percpu_counter_add(&vm_committed_as, pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | static inline void vm_unacct_memory(long pages) |
| 22 | { |
| 23 | vm_acct_memory(-pages); |
| 24 | } |
| 25 | |
| 26 | /* |
Dave Kleikamp | b845f31 | 2008-07-08 00:28:51 +1000 | [diff] [blame] | 27 | * Allow architectures to handle additional protection bits |
| 28 | */ |
| 29 | |
| 30 | #ifndef arch_calc_vm_prot_bits |
| 31 | #define arch_calc_vm_prot_bits(prot) 0 |
| 32 | #endif |
| 33 | |
| 34 | #ifndef arch_vm_get_page_prot |
| 35 | #define arch_vm_get_page_prot(vm_flags) __pgprot(0) |
| 36 | #endif |
| 37 | |
| 38 | #ifndef arch_validate_prot |
| 39 | /* |
| 40 | * This is called from mprotect(). PROT_GROWSDOWN and PROT_GROWSUP have |
| 41 | * already been masked out. |
| 42 | * |
| 43 | * Returns true if the prot flags are valid |
| 44 | */ |
| 45 | static inline int arch_validate_prot(unsigned long prot) |
| 46 | { |
| 47 | return (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM)) == 0; |
| 48 | } |
| 49 | #define arch_validate_prot arch_validate_prot |
| 50 | #endif |
| 51 | |
| 52 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | * Optimisation macro. It is equivalent to: |
| 54 | * (x & bit1) ? bit2 : 0 |
| 55 | * but this version is faster. |
| 56 | * ("bit1" and "bit2" must be single bits) |
| 57 | */ |
| 58 | #define _calc_vm_trans(x, bit1, bit2) \ |
| 59 | ((bit1) <= (bit2) ? ((x) & (bit1)) * ((bit2) / (bit1)) \ |
| 60 | : ((x) & (bit1)) / ((bit1) / (bit2))) |
| 61 | |
| 62 | /* |
| 63 | * Combine the mmap "prot" argument into "vm_flags" used internally. |
| 64 | */ |
| 65 | static inline unsigned long |
| 66 | calc_vm_prot_bits(unsigned long prot) |
| 67 | { |
| 68 | return _calc_vm_trans(prot, PROT_READ, VM_READ ) | |
| 69 | _calc_vm_trans(prot, PROT_WRITE, VM_WRITE) | |
Dave Kleikamp | b845f31 | 2008-07-08 00:28:51 +1000 | [diff] [blame] | 70 | _calc_vm_trans(prot, PROT_EXEC, VM_EXEC) | |
| 71 | arch_calc_vm_prot_bits(prot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Combine the mmap "flags" argument into "vm_flags" used internally. |
| 76 | */ |
| 77 | static inline unsigned long |
| 78 | calc_vm_flag_bits(unsigned long flags) |
| 79 | { |
| 80 | return _calc_vm_trans(flags, MAP_GROWSDOWN, VM_GROWSDOWN ) | |
| 81 | _calc_vm_trans(flags, MAP_DENYWRITE, VM_DENYWRITE ) | |
Michel Lespinasse | 1869305 | 2013-02-22 16:32:46 -0800 | [diff] [blame] | 82 | ((flags & MAP_LOCKED) ? (VM_LOCKED | VM_POPULATE) : 0) | |
| 83 | (((flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE) ? |
| 84 | VM_POPULATE : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | #endif /* _LINUX_MMAN_H */ |