mmap.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * flexible mmap layout support
  3. *
  4. * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
  5. * All Rights Reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. *
  22. * Started by Ingo Molnar <mingo@elte.hu>
  23. */
  24. #include <linux/personality.h>
  25. #include <linux/mm.h>
  26. #include <linux/mman.h>
  27. #include <linux/module.h>
  28. #include <linux/random.h>
  29. #include <linux/compat.h>
  30. #include <linux/security.h>
  31. #include <asm/pgalloc.h>
  32. static unsigned long stack_maxrandom_size(void)
  33. {
  34. if (!(current->flags & PF_RANDOMIZE))
  35. return 0;
  36. if (current->personality & ADDR_NO_RANDOMIZE)
  37. return 0;
  38. return STACK_RND_MASK << PAGE_SHIFT;
  39. }
  40. /*
  41. * Top of mmap area (just below the process stack).
  42. *
  43. * Leave at least a ~32 MB hole.
  44. */
  45. #define MIN_GAP (32*1024*1024)
  46. #define MAX_GAP (STACK_TOP/6*5)
  47. static inline int mmap_is_legacy(void)
  48. {
  49. if (current->personality & ADDR_COMPAT_LAYOUT)
  50. return 1;
  51. if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
  52. return 1;
  53. return sysctl_legacy_va_layout;
  54. }
  55. unsigned long arch_mmap_rnd(void)
  56. {
  57. return (get_random_int() & MMAP_RND_MASK) << PAGE_SHIFT;
  58. }
  59. static unsigned long mmap_base_legacy(unsigned long rnd)
  60. {
  61. return TASK_UNMAPPED_BASE + rnd;
  62. }
  63. static inline unsigned long mmap_base(unsigned long rnd)
  64. {
  65. unsigned long gap = rlimit(RLIMIT_STACK);
  66. if (gap < MIN_GAP)
  67. gap = MIN_GAP;
  68. else if (gap > MAX_GAP)
  69. gap = MAX_GAP;
  70. gap &= PAGE_MASK;
  71. return STACK_TOP - stack_maxrandom_size() - rnd - gap;
  72. }
  73. unsigned long
  74. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  75. unsigned long len, unsigned long pgoff, unsigned long flags)
  76. {
  77. struct mm_struct *mm = current->mm;
  78. struct vm_area_struct *vma;
  79. struct vm_unmapped_area_info info;
  80. if (len > TASK_SIZE - mmap_min_addr)
  81. return -ENOMEM;
  82. if (flags & MAP_FIXED)
  83. return addr;
  84. if (addr) {
  85. addr = PAGE_ALIGN(addr);
  86. vma = find_vma(mm, addr);
  87. if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
  88. (!vma || addr + len <= vm_start_gap(vma)))
  89. return addr;
  90. }
  91. info.flags = 0;
  92. info.length = len;
  93. info.low_limit = mm->mmap_base;
  94. info.high_limit = TASK_SIZE;
  95. if (filp || (flags & MAP_SHARED))
  96. info.align_mask = MMAP_ALIGN_MASK << PAGE_SHIFT;
  97. else
  98. info.align_mask = 0;
  99. info.align_offset = pgoff << PAGE_SHIFT;
  100. return vm_unmapped_area(&info);
  101. }
  102. unsigned long
  103. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  104. const unsigned long len, const unsigned long pgoff,
  105. const unsigned long flags)
  106. {
  107. struct vm_area_struct *vma;
  108. struct mm_struct *mm = current->mm;
  109. unsigned long addr = addr0;
  110. struct vm_unmapped_area_info info;
  111. /* requested length too big for entire address space */
  112. if (len > TASK_SIZE - mmap_min_addr)
  113. return -ENOMEM;
  114. if (flags & MAP_FIXED)
  115. return addr;
  116. /* requesting a specific address */
  117. if (addr) {
  118. addr = PAGE_ALIGN(addr);
  119. vma = find_vma(mm, addr);
  120. if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
  121. (!vma || addr + len <= vm_start_gap(vma)))
  122. return addr;
  123. }
  124. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  125. info.length = len;
  126. info.low_limit = max(PAGE_SIZE, mmap_min_addr);
  127. info.high_limit = mm->mmap_base;
  128. if (filp || (flags & MAP_SHARED))
  129. info.align_mask = MMAP_ALIGN_MASK << PAGE_SHIFT;
  130. else
  131. info.align_mask = 0;
  132. info.align_offset = pgoff << PAGE_SHIFT;
  133. addr = vm_unmapped_area(&info);
  134. /*
  135. * A failed mmap() very likely causes application failure,
  136. * so fall back to the bottom-up function here. This scenario
  137. * can happen with large stack limits and large mmap()
  138. * allocations.
  139. */
  140. if (addr & ~PAGE_MASK) {
  141. VM_BUG_ON(addr != -ENOMEM);
  142. info.flags = 0;
  143. info.low_limit = TASK_UNMAPPED_BASE;
  144. info.high_limit = TASK_SIZE;
  145. addr = vm_unmapped_area(&info);
  146. }
  147. return addr;
  148. }
  149. int s390_mmap_check(unsigned long addr, unsigned long len, unsigned long flags)
  150. {
  151. if (is_compat_task() || (TASK_SIZE >= (1UL << 53)))
  152. return 0;
  153. if (!(flags & MAP_FIXED))
  154. addr = 0;
  155. if ((addr + len) >= TASK_SIZE)
  156. return crst_table_upgrade(current->mm);
  157. return 0;
  158. }
  159. static unsigned long
  160. s390_get_unmapped_area(struct file *filp, unsigned long addr,
  161. unsigned long len, unsigned long pgoff, unsigned long flags)
  162. {
  163. struct mm_struct *mm = current->mm;
  164. unsigned long area;
  165. int rc;
  166. area = arch_get_unmapped_area(filp, addr, len, pgoff, flags);
  167. if (!(area & ~PAGE_MASK))
  168. return area;
  169. if (area == -ENOMEM && !is_compat_task() && TASK_SIZE < (1UL << 53)) {
  170. /* Upgrade the page table to 4 levels and retry. */
  171. rc = crst_table_upgrade(mm);
  172. if (rc)
  173. return (unsigned long) rc;
  174. area = arch_get_unmapped_area(filp, addr, len, pgoff, flags);
  175. }
  176. return area;
  177. }
  178. static unsigned long
  179. s390_get_unmapped_area_topdown(struct file *filp, const unsigned long addr,
  180. const unsigned long len, const unsigned long pgoff,
  181. const unsigned long flags)
  182. {
  183. struct mm_struct *mm = current->mm;
  184. unsigned long area;
  185. int rc;
  186. area = arch_get_unmapped_area_topdown(filp, addr, len, pgoff, flags);
  187. if (!(area & ~PAGE_MASK))
  188. return area;
  189. if (area == -ENOMEM && !is_compat_task() && TASK_SIZE < (1UL << 53)) {
  190. /* Upgrade the page table to 4 levels and retry. */
  191. rc = crst_table_upgrade(mm);
  192. if (rc)
  193. return (unsigned long) rc;
  194. area = arch_get_unmapped_area_topdown(filp, addr, len,
  195. pgoff, flags);
  196. }
  197. return area;
  198. }
  199. /*
  200. * This function, called very early during the creation of a new
  201. * process VM image, sets up which VM layout function to use:
  202. */
  203. void arch_pick_mmap_layout(struct mm_struct *mm)
  204. {
  205. unsigned long random_factor = 0UL;
  206. if (current->flags & PF_RANDOMIZE)
  207. random_factor = arch_mmap_rnd();
  208. /*
  209. * Fall back to the standard layout if the personality
  210. * bit is set, or if the expected stack growth is unlimited:
  211. */
  212. if (mmap_is_legacy()) {
  213. mm->mmap_base = mmap_base_legacy(random_factor);
  214. mm->get_unmapped_area = s390_get_unmapped_area;
  215. } else {
  216. mm->mmap_base = mmap_base(random_factor);
  217. mm->get_unmapped_area = s390_get_unmapped_area_topdown;
  218. }
  219. }