mman.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #else
  55. #define vm_committed_as_batch 0
  56. #endif
  57. unsigned long vm_memory_committed(void);
  58. static inline void vm_acct_memory(long pages)
  59. {
  60. percpu_counter_add_batch(&vm_committed_as, pages, vm_committed_as_batch);
  61. }
  62. static inline void vm_unacct_memory(long pages)
  63. {
  64. vm_acct_memory(-pages);
  65. }
  66. /*
  67. * Allow architectures to handle additional protection bits
  68. */
  69. #ifndef arch_calc_vm_prot_bits
  70. #define arch_calc_vm_prot_bits(prot, pkey) 0
  71. #endif
  72. #ifndef arch_vm_get_page_prot
  73. #define arch_vm_get_page_prot(vm_flags) __pgprot(0)
  74. #endif
  75. #ifndef arch_validate_prot
  76. /*
  77. * This is called from mprotect(). PROT_GROWSDOWN and PROT_GROWSUP have
  78. * already been masked out.
  79. *
  80. * Returns true if the prot flags are valid
  81. */
  82. static inline bool arch_validate_prot(unsigned long prot, unsigned long addr)
  83. {
  84. return (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM)) == 0;
  85. }
  86. #define arch_validate_prot arch_validate_prot
  87. #endif
  88. /*
  89. * Optimisation macro. It is equivalent to:
  90. * (x & bit1) ? bit2 : 0
  91. * but this version is faster.
  92. * ("bit1" and "bit2" must be single bits)
  93. */
  94. #define _calc_vm_trans(x, bit1, bit2) \
  95. ((!(bit1) || !(bit2)) ? 0 : \
  96. ((bit1) <= (bit2) ? ((x) & (bit1)) * ((bit2) / (bit1)) \
  97. : ((x) & (bit1)) / ((bit1) / (bit2))))
  98. /*
  99. * Combine the mmap "prot" argument into "vm_flags" used internally.
  100. */
  101. static inline unsigned long
  102. calc_vm_prot_bits(unsigned long prot, unsigned long pkey)
  103. {
  104. return _calc_vm_trans(prot, PROT_READ, VM_READ ) |
  105. _calc_vm_trans(prot, PROT_WRITE, VM_WRITE) |
  106. _calc_vm_trans(prot, PROT_EXEC, VM_EXEC) |
  107. arch_calc_vm_prot_bits(prot, pkey);
  108. }
  109. /*
  110. * Combine the mmap "flags" argument into "vm_flags" used internally.
  111. */
  112. static inline unsigned long
  113. calc_vm_flag_bits(unsigned long flags)
  114. {
  115. return _calc_vm_trans(flags, MAP_GROWSDOWN, VM_GROWSDOWN ) |
  116. _calc_vm_trans(flags, MAP_DENYWRITE, VM_DENYWRITE ) |
  117. _calc_vm_trans(flags, MAP_LOCKED, VM_LOCKED ) |
  118. _calc_vm_trans(flags, MAP_SYNC, VM_SYNC );
  119. }
  120. unsigned long vm_commit_limit(void);
  121. #endif /* _LINUX_MMAN_H */