mmap.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2011 Wind River Systems,
  7. * written by Ralf Baechle <ralf@linux-mips.org>
  8. */
  9. #include <linux/compiler.h>
  10. #include <linux/errno.h>
  11. #include <linux/mm.h>
  12. #include <linux/mman.h>
  13. #include <linux/module.h>
  14. #include <linux/personality.h>
  15. #include <linux/random.h>
  16. #include <linux/sched.h>
  17. unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
  18. EXPORT_SYMBOL(shm_align_mask);
  19. /* gap between mmap and stack */
  20. #define MIN_GAP (128*1024*1024UL)
  21. #define MAX_GAP ((TASK_SIZE)/6*5)
  22. static int mmap_is_legacy(void)
  23. {
  24. if (current->personality & ADDR_COMPAT_LAYOUT)
  25. return 1;
  26. if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
  27. return 1;
  28. return sysctl_legacy_va_layout;
  29. }
  30. static unsigned long mmap_base(unsigned long rnd)
  31. {
  32. unsigned long gap = rlimit(RLIMIT_STACK);
  33. if (gap < MIN_GAP)
  34. gap = MIN_GAP;
  35. else if (gap > MAX_GAP)
  36. gap = MAX_GAP;
  37. return PAGE_ALIGN(TASK_SIZE - gap - rnd);
  38. }
  39. static inline unsigned long COLOUR_ALIGN_DOWN(unsigned long addr,
  40. unsigned long pgoff)
  41. {
  42. unsigned long base = addr & ~shm_align_mask;
  43. unsigned long off = (pgoff << PAGE_SHIFT) & shm_align_mask;
  44. if (base + off <= addr)
  45. return base + off;
  46. return base - off;
  47. }
  48. #define COLOUR_ALIGN(addr, pgoff) \
  49. ((((addr) + shm_align_mask) & ~shm_align_mask) + \
  50. (((pgoff) << PAGE_SHIFT) & shm_align_mask))
  51. enum mmap_allocation_direction {UP, DOWN};
  52. static unsigned long arch_get_unmapped_area_common(struct file *filp,
  53. unsigned long addr0, unsigned long len, unsigned long pgoff,
  54. unsigned long flags, enum mmap_allocation_direction dir)
  55. {
  56. struct mm_struct *mm = current->mm;
  57. struct vm_area_struct *vma;
  58. unsigned long addr = addr0;
  59. int do_color_align;
  60. if (unlikely(len > TASK_SIZE))
  61. return -ENOMEM;
  62. if (flags & MAP_FIXED) {
  63. /* Even MAP_FIXED mappings must reside within TASK_SIZE */
  64. if (TASK_SIZE - len < addr)
  65. return -EINVAL;
  66. /*
  67. * We do not accept a shared mapping if it would violate
  68. * cache aliasing constraints.
  69. */
  70. if ((flags & MAP_SHARED) &&
  71. ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
  72. return -EINVAL;
  73. return addr;
  74. }
  75. do_color_align = 0;
  76. if (filp || (flags & MAP_SHARED))
  77. do_color_align = 1;
  78. /* requesting a specific address */
  79. if (addr) {
  80. if (do_color_align)
  81. addr = COLOUR_ALIGN(addr, pgoff);
  82. else
  83. addr = PAGE_ALIGN(addr);
  84. vma = find_vma(mm, addr);
  85. if (TASK_SIZE - len >= addr &&
  86. (!vma || addr + len <= vm_start_gap(vma)))
  87. return addr;
  88. }
  89. if (dir == UP) {
  90. addr = mm->mmap_base;
  91. if (do_color_align)
  92. addr = COLOUR_ALIGN(addr, pgoff);
  93. else
  94. addr = PAGE_ALIGN(addr);
  95. for (vma = find_vma(current->mm, addr); ; vma = vma->vm_next) {
  96. /* At this point: (!vma || addr < vma->vm_end). */
  97. if (TASK_SIZE - len < addr)
  98. return -ENOMEM;
  99. if (!vma || addr + len <= vma->vm_start)
  100. return addr;
  101. addr = vma->vm_end;
  102. if (do_color_align)
  103. addr = COLOUR_ALIGN(addr, pgoff);
  104. }
  105. } else {
  106. /* check if free_area_cache is useful for us */
  107. if (len <= mm->cached_hole_size) {
  108. mm->cached_hole_size = 0;
  109. mm->free_area_cache = mm->mmap_base;
  110. }
  111. /*
  112. * either no address requested, or the mapping can't fit into
  113. * the requested address hole
  114. */
  115. addr = mm->free_area_cache;
  116. if (do_color_align) {
  117. unsigned long base =
  118. COLOUR_ALIGN_DOWN(addr - len, pgoff);
  119. addr = base + len;
  120. }
  121. /* make sure it can fit in the remaining address space */
  122. if (likely(addr > len)) {
  123. vma = find_vma(mm, addr - len);
  124. if (!vma || addr <= vma->vm_start) {
  125. /* cache the address as a hint for next time */
  126. return mm->free_area_cache = addr - len;
  127. }
  128. }
  129. if (unlikely(mm->mmap_base < len))
  130. goto bottomup;
  131. addr = mm->mmap_base - len;
  132. if (do_color_align)
  133. addr = COLOUR_ALIGN_DOWN(addr, pgoff);
  134. do {
  135. /*
  136. * Lookup failure means no vma is above this address,
  137. * else if new region fits below vma->vm_start,
  138. * return with success:
  139. */
  140. vma = find_vma(mm, addr);
  141. if (likely(!vma || addr + len <= vma->vm_start)) {
  142. /* cache the address as a hint for next time */
  143. return mm->free_area_cache = addr;
  144. }
  145. /* remember the largest hole we saw so far */
  146. if (addr + mm->cached_hole_size < vma->vm_start)
  147. mm->cached_hole_size = vma->vm_start - addr;
  148. /* try just below the current vma->vm_start */
  149. addr = vma->vm_start - len;
  150. if (do_color_align)
  151. addr = COLOUR_ALIGN_DOWN(addr, pgoff);
  152. } while (likely(len < vma->vm_start));
  153. bottomup:
  154. /*
  155. * A failed mmap() very likely causes application failure,
  156. * so fall back to the bottom-up function here. This scenario
  157. * can happen with large stack limits and large mmap()
  158. * allocations.
  159. */
  160. mm->cached_hole_size = ~0UL;
  161. mm->free_area_cache = TASK_UNMAPPED_BASE;
  162. addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  163. /*
  164. * Restore the topdown base:
  165. */
  166. mm->free_area_cache = mm->mmap_base;
  167. mm->cached_hole_size = ~0UL;
  168. return addr;
  169. }
  170. }
  171. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr0,
  172. unsigned long len, unsigned long pgoff, unsigned long flags)
  173. {
  174. return arch_get_unmapped_area_common(filp,
  175. addr0, len, pgoff, flags, UP);
  176. }
  177. /*
  178. * There is no need to export this but sched.h declares the function as
  179. * extern so making it static here results in an error.
  180. */
  181. unsigned long arch_get_unmapped_area_topdown(struct file *filp,
  182. unsigned long addr0, unsigned long len, unsigned long pgoff,
  183. unsigned long flags)
  184. {
  185. return arch_get_unmapped_area_common(filp,
  186. addr0, len, pgoff, flags, DOWN);
  187. }
  188. void arch_pick_mmap_layout(struct mm_struct *mm)
  189. {
  190. unsigned long random_factor = 0UL;
  191. if (current->flags & PF_RANDOMIZE) {
  192. random_factor = get_random_long();
  193. random_factor = random_factor << PAGE_SHIFT;
  194. if (TASK_IS_32BIT_ADDR)
  195. random_factor &= 0xfffffful;
  196. else
  197. random_factor &= 0xffffffful;
  198. }
  199. if (mmap_is_legacy()) {
  200. mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
  201. mm->get_unmapped_area = arch_get_unmapped_area;
  202. mm->unmap_area = arch_unmap_area;
  203. } else {
  204. mm->mmap_base = mmap_base(random_factor);
  205. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  206. mm->unmap_area = arch_unmap_area_topdown;
  207. }
  208. }
  209. static inline unsigned long brk_rnd(void)
  210. {
  211. unsigned long rnd = get_random_long();
  212. rnd = rnd << PAGE_SHIFT;
  213. /* 8MB for 32bit, 256MB for 64bit */
  214. if (TASK_IS_32BIT_ADDR)
  215. rnd = rnd & 0x7ffffful;
  216. else
  217. rnd = rnd & 0xffffffful;
  218. return rnd;
  219. }
  220. unsigned long arch_randomize_brk(struct mm_struct *mm)
  221. {
  222. unsigned long base = mm->brk;
  223. unsigned long ret;
  224. ret = PAGE_ALIGN(base + brk_rnd());
  225. if (ret < mm->brk)
  226. return mm->brk;
  227. return ret;
  228. }