mman.h 3.9 KB

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