mman.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #ifndef _LINUX_MMAN_H
  2. #define _LINUX_MMAN_H
  3. #include <linux/mm.h>
  4. #include <linux/percpu_counter.h>
  5. #include <linux/atomic.h>
  6. #include <uapi/linux/mman.h>
  7. /*
  8. * Arrange for legacy / undefined architecture specific flags to be
  9. * ignored by mmap handling code.
  10. */
  11. #ifndef MAP_32BIT
  12. #define MAP_32BIT 0
  13. #endif
  14. #ifndef MAP_HUGE_2MB
  15. #define MAP_HUGE_2MB 0
  16. #endif
  17. #ifndef MAP_HUGE_1GB
  18. #define MAP_HUGE_1GB 0
  19. #endif
  20. #ifndef MAP_UNINITIALIZED
  21. #define MAP_UNINITIALIZED 0
  22. #endif
  23. #ifndef MAP_SYNC
  24. #define MAP_SYNC 0
  25. #endif
  26. /*
  27. * The historical set of flags that all mmap implementations implicitly
  28. * support when a ->mmap_validate() op is not provided in file_operations.
  29. */
  30. #define LEGACY_MAP_MASK (MAP_SHARED \
  31. | MAP_PRIVATE \
  32. | MAP_FIXED \
  33. | MAP_ANONYMOUS \
  34. | MAP_DENYWRITE \
  35. | MAP_EXECUTABLE \
  36. | MAP_UNINITIALIZED \
  37. | MAP_GROWSDOWN \
  38. | MAP_LOCKED \
  39. | MAP_NORESERVE \
  40. | MAP_POPULATE \
  41. | MAP_NONBLOCK \
  42. | MAP_STACK \
  43. | MAP_HUGETLB \
  44. | MAP_32BIT \
  45. | MAP_HUGE_2MB \
  46. | MAP_HUGE_1GB)
  47. extern int sysctl_overcommit_memory;
  48. extern int sysctl_overcommit_ratio;
  49. extern unsigned long sysctl_overcommit_kbytes;
  50. extern struct percpu_counter vm_committed_as;
  51. #ifdef CONFIG_SMP
  52. extern s32 vm_committed_as_batch;
  53. #else
  54. #define vm_committed_as_batch 0
  55. #endif
  56. unsigned long vm_memory_committed(void);
  57. static inline void vm_acct_memory(long pages)
  58. {
  59. percpu_counter_add_batch(&vm_committed_as, pages, vm_committed_as_batch);
  60. }
  61. static inline void vm_unacct_memory(long pages)
  62. {
  63. vm_acct_memory(-pages);
  64. }
  65. /*
  66. * Allow architectures to handle additional protection bits
  67. */
  68. #ifndef arch_calc_vm_prot_bits
  69. #define arch_calc_vm_prot_bits(prot, pkey) 0
  70. #endif
  71. #ifndef arch_vm_get_page_prot
  72. #define arch_vm_get_page_prot(vm_flags) __pgprot(0)
  73. #endif
  74. #ifndef arch_validate_prot
  75. /*
  76. * This is called from mprotect(). PROT_GROWSDOWN and PROT_GROWSUP have
  77. * already been masked out.
  78. *
  79. * Returns true if the prot flags are valid
  80. */
  81. static inline bool arch_validate_prot(unsigned long prot)
  82. {
  83. return (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM)) == 0;
  84. }
  85. #define arch_validate_prot arch_validate_prot
  86. #endif
  87. /*
  88. * Optimisation macro. It is equivalent to:
  89. * (x & bit1) ? bit2 : 0
  90. * but this version is faster.
  91. * ("bit1" and "bit2" must be single bits)
  92. */
  93. #define _calc_vm_trans(x, bit1, bit2) \
  94. ((!(bit1) || !(bit2)) ? 0 : \
  95. ((bit1) <= (bit2) ? ((x) & (bit1)) * ((bit2) / (bit1)) \
  96. : ((x) & (bit1)) / ((bit1) / (bit2))))
  97. /*
  98. * Combine the mmap "prot" argument into "vm_flags" used internally.
  99. */
  100. static inline unsigned long
  101. calc_vm_prot_bits(unsigned long prot, unsigned long pkey)
  102. {
  103. return _calc_vm_trans(prot, PROT_READ, VM_READ ) |
  104. _calc_vm_trans(prot, PROT_WRITE, VM_WRITE) |
  105. _calc_vm_trans(prot, PROT_EXEC, VM_EXEC) |
  106. arch_calc_vm_prot_bits(prot, pkey);
  107. }
  108. /*
  109. * Combine the mmap "flags" argument into "vm_flags" used internally.
  110. */
  111. static inline unsigned long
  112. calc_vm_flag_bits(unsigned long flags)
  113. {
  114. return _calc_vm_trans(flags, MAP_GROWSDOWN, VM_GROWSDOWN ) |
  115. _calc_vm_trans(flags, MAP_DENYWRITE, VM_DENYWRITE ) |
  116. _calc_vm_trans(flags, MAP_LOCKED, VM_LOCKED ) |
  117. _calc_vm_trans(flags, MAP_SYNC, VM_SYNC );
  118. }
  119. unsigned long vm_commit_limit(void);
  120. #endif /* _LINUX_MMAN_H */