mmap.c 6.6 KB

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