mmap.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/export.h>
  14. #include <linux/personality.h>
  15. #include <linux/random.h>
  16. #include <linux/sched/signal.h>
  17. #include <linux/sched/mm.h>
  18. unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
  19. EXPORT_SYMBOL(shm_align_mask);
  20. /* gap between mmap and stack */
  21. #define MIN_GAP (128*1024*1024UL)
  22. #define MAX_GAP ((TASK_SIZE)/6*5)
  23. static int mmap_is_legacy(void)
  24. {
  25. if (current->personality & ADDR_COMPAT_LAYOUT)
  26. return 1;
  27. if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
  28. return 1;
  29. return sysctl_legacy_va_layout;
  30. }
  31. static unsigned long mmap_base(unsigned long rnd)
  32. {
  33. unsigned long gap = rlimit(RLIMIT_STACK);
  34. if (gap < MIN_GAP)
  35. gap = MIN_GAP;
  36. else if (gap > MAX_GAP)
  37. gap = MAX_GAP;
  38. return PAGE_ALIGN(TASK_SIZE - gap - rnd);
  39. }
  40. #define COLOUR_ALIGN(addr, pgoff) \
  41. ((((addr) + shm_align_mask) & ~shm_align_mask) + \
  42. (((pgoff) << PAGE_SHIFT) & shm_align_mask))
  43. enum mmap_allocation_direction {UP, DOWN};
  44. static unsigned long arch_get_unmapped_area_common(struct file *filp,
  45. unsigned long addr0, unsigned long len, unsigned long pgoff,
  46. unsigned long flags, enum mmap_allocation_direction dir)
  47. {
  48. struct mm_struct *mm = current->mm;
  49. struct vm_area_struct *vma;
  50. unsigned long addr = addr0;
  51. int do_color_align;
  52. struct vm_unmapped_area_info info;
  53. if (unlikely(len > TASK_SIZE))
  54. return -ENOMEM;
  55. if (flags & MAP_FIXED) {
  56. /* Even MAP_FIXED mappings must reside within TASK_SIZE */
  57. if (TASK_SIZE - len < addr)
  58. return -EINVAL;
  59. /*
  60. * We do not accept a shared mapping if it would violate
  61. * cache aliasing constraints.
  62. */
  63. if ((flags & MAP_SHARED) &&
  64. ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
  65. return -EINVAL;
  66. return addr;
  67. }
  68. do_color_align = 0;
  69. if (filp || (flags & MAP_SHARED))
  70. do_color_align = 1;
  71. /* requesting a specific address */
  72. if (addr) {
  73. if (do_color_align)
  74. addr = COLOUR_ALIGN(addr, pgoff);
  75. else
  76. addr = PAGE_ALIGN(addr);
  77. vma = find_vma(mm, addr);
  78. if (TASK_SIZE - len >= addr &&
  79. (!vma || addr + len <= vma->vm_start))
  80. return addr;
  81. }
  82. info.length = len;
  83. info.align_mask = do_color_align ? (PAGE_MASK & shm_align_mask) : 0;
  84. info.align_offset = pgoff << PAGE_SHIFT;
  85. if (dir == DOWN) {
  86. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  87. info.low_limit = PAGE_SIZE;
  88. info.high_limit = mm->mmap_base;
  89. addr = vm_unmapped_area(&info);
  90. if (!(addr & ~PAGE_MASK))
  91. return addr;
  92. /*
  93. * A failed mmap() very likely causes application failure,
  94. * so fall back to the bottom-up function here. This scenario
  95. * can happen with large stack limits and large mmap()
  96. * allocations.
  97. */
  98. }
  99. info.flags = 0;
  100. info.low_limit = mm->mmap_base;
  101. info.high_limit = TASK_SIZE;
  102. return vm_unmapped_area(&info);
  103. }
  104. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr0,
  105. unsigned long len, unsigned long pgoff, unsigned long flags)
  106. {
  107. return arch_get_unmapped_area_common(filp,
  108. addr0, len, pgoff, flags, UP);
  109. }
  110. /*
  111. * There is no need to export this but sched.h declares the function as
  112. * extern so making it static here results in an error.
  113. */
  114. unsigned long arch_get_unmapped_area_topdown(struct file *filp,
  115. unsigned long addr0, unsigned long len, unsigned long pgoff,
  116. unsigned long flags)
  117. {
  118. return arch_get_unmapped_area_common(filp,
  119. addr0, len, pgoff, flags, DOWN);
  120. }
  121. unsigned long arch_mmap_rnd(void)
  122. {
  123. unsigned long rnd;
  124. #ifdef CONFIG_COMPAT
  125. if (TASK_IS_32BIT_ADDR)
  126. rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1);
  127. else
  128. #endif /* CONFIG_COMPAT */
  129. rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
  130. return rnd << PAGE_SHIFT;
  131. }
  132. void arch_pick_mmap_layout(struct mm_struct *mm)
  133. {
  134. unsigned long random_factor = 0UL;
  135. if (current->flags & PF_RANDOMIZE)
  136. random_factor = arch_mmap_rnd();
  137. if (mmap_is_legacy()) {
  138. mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
  139. mm->get_unmapped_area = arch_get_unmapped_area;
  140. } else {
  141. mm->mmap_base = mmap_base(random_factor);
  142. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  143. }
  144. }
  145. static inline unsigned long brk_rnd(void)
  146. {
  147. unsigned long rnd = get_random_long();
  148. rnd = rnd << PAGE_SHIFT;
  149. /* 8MB for 32bit, 256MB for 64bit */
  150. if (TASK_IS_32BIT_ADDR)
  151. rnd = rnd & 0x7ffffful;
  152. else
  153. rnd = rnd & 0xffffffful;
  154. return rnd;
  155. }
  156. unsigned long arch_randomize_brk(struct mm_struct *mm)
  157. {
  158. unsigned long base = mm->brk;
  159. unsigned long ret;
  160. ret = PAGE_ALIGN(base + brk_rnd());
  161. if (ret < mm->brk)
  162. return mm->brk;
  163. return ret;
  164. }
  165. int __virt_addr_valid(const volatile void *kaddr)
  166. {
  167. return pfn_valid(PFN_DOWN(virt_to_phys(kaddr)));
  168. }
  169. EXPORT_SYMBOL_GPL(__virt_addr_valid);