blob: 28adeabe851ff22dbea2da9d701208dadcc4925d [file] [log] [blame]
Ralf Baechle6f6c3c332011-05-19 09:21:33 +01001/*
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 */
Kevin Cernekee16650102011-06-18 11:28:48 -07009#include <linux/compiler.h>
Ralf Baechle6f6c3c332011-05-19 09:21:33 +010010#include <linux/errno.h>
11#include <linux/mm.h>
12#include <linux/mman.h>
Paul Gortmakerd9ba5772016-08-21 15:58:14 -040013#include <linux/export.h>
Jian Pengd0be89f2011-05-17 12:27:49 -070014#include <linux/personality.h>
Ralf Baechle6f6c3c332011-05-19 09:21:33 +010015#include <linux/random.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010016#include <linux/sched/signal.h>
Ingo Molnar01042602017-02-08 18:51:31 +010017#include <linux/sched/mm.h>
Ralf Baechle6f6c3c332011-05-19 09:21:33 +010018
19unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
Ralf Baechle6f6c3c332011-05-19 09:21:33 +010020EXPORT_SYMBOL(shm_align_mask);
21
Jian Pengd0be89f2011-05-17 12:27:49 -070022/* gap between mmap and stack */
23#define MIN_GAP (128*1024*1024UL)
Kevin Cernekee16650102011-06-18 11:28:48 -070024#define MAX_GAP ((TASK_SIZE)/6*5)
Jian Pengd0be89f2011-05-17 12:27:49 -070025
26static int mmap_is_legacy(void)
27{
28 if (current->personality & ADDR_COMPAT_LAYOUT)
29 return 1;
30
31 if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
32 return 1;
33
34 return sysctl_legacy_va_layout;
35}
36
37static unsigned long mmap_base(unsigned long rnd)
38{
39 unsigned long gap = rlimit(RLIMIT_STACK);
40
41 if (gap < MIN_GAP)
42 gap = MIN_GAP;
43 else if (gap > MAX_GAP)
44 gap = MAX_GAP;
45
46 return PAGE_ALIGN(TASK_SIZE - gap - rnd);
47}
48
Kevin Cernekee16650102011-06-18 11:28:48 -070049#define COLOUR_ALIGN(addr, pgoff) \
Ralf Baechle6f6c3c332011-05-19 09:21:33 +010050 ((((addr) + shm_align_mask) & ~shm_align_mask) + \
51 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
52
Jian Pengd0be89f2011-05-17 12:27:49 -070053enum mmap_allocation_direction {UP, DOWN};
54
Kevin Cernekee16650102011-06-18 11:28:48 -070055static unsigned long arch_get_unmapped_area_common(struct file *filp,
Jian Pengd0be89f2011-05-17 12:27:49 -070056 unsigned long addr0, unsigned long len, unsigned long pgoff,
57 unsigned long flags, enum mmap_allocation_direction dir)
Ralf Baechle6f6c3c332011-05-19 09:21:33 +010058{
Jian Pengd0be89f2011-05-17 12:27:49 -070059 struct mm_struct *mm = current->mm;
60 struct vm_area_struct *vma;
61 unsigned long addr = addr0;
Ralf Baechle6f6c3c332011-05-19 09:21:33 +010062 int do_color_align;
Michel Lespinasseb6661862012-12-11 16:02:06 -080063 struct vm_unmapped_area_info info;
Ralf Baechle6f6c3c332011-05-19 09:21:33 +010064
Jian Pengd0be89f2011-05-17 12:27:49 -070065 if (unlikely(len > TASK_SIZE))
Ralf Baechle6f6c3c332011-05-19 09:21:33 +010066 return -ENOMEM;
67
68 if (flags & MAP_FIXED) {
Jian Pengd0be89f2011-05-17 12:27:49 -070069 /* Even MAP_FIXED mappings must reside within TASK_SIZE */
Ralf Baechle6f6c3c332011-05-19 09:21:33 +010070 if (TASK_SIZE - len < addr)
71 return -EINVAL;
72
73 /*
74 * We do not accept a shared mapping if it would violate
75 * cache aliasing constraints.
76 */
77 if ((flags & MAP_SHARED) &&
78 ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
79 return -EINVAL;
80 return addr;
81 }
82
83 do_color_align = 0;
84 if (filp || (flags & MAP_SHARED))
85 do_color_align = 1;
Jian Pengd0be89f2011-05-17 12:27:49 -070086
87 /* requesting a specific address */
Ralf Baechle6f6c3c332011-05-19 09:21:33 +010088 if (addr) {
89 if (do_color_align)
90 addr = COLOUR_ALIGN(addr, pgoff);
91 else
92 addr = PAGE_ALIGN(addr);
Ralf Baechle6f6c3c332011-05-19 09:21:33 +010093
Jian Pengd0be89f2011-05-17 12:27:49 -070094 vma = find_vma(mm, addr);
95 if (TASK_SIZE - len >= addr &&
Hugh Dickins1be71072017-06-19 04:03:24 -070096 (!vma || addr + len <= vm_start_gap(vma)))
Ralf Baechle6f6c3c332011-05-19 09:21:33 +010097 return addr;
Ralf Baechle6f6c3c332011-05-19 09:21:33 +010098 }
Jian Pengd0be89f2011-05-17 12:27:49 -070099
Michel Lespinasseb6661862012-12-11 16:02:06 -0800100 info.length = len;
101 info.align_mask = do_color_align ? (PAGE_MASK & shm_align_mask) : 0;
102 info.align_offset = pgoff << PAGE_SHIFT;
Jian Pengd0be89f2011-05-17 12:27:49 -0700103
Michel Lespinasseb6661862012-12-11 16:02:06 -0800104 if (dir == DOWN) {
105 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
106 info.low_limit = PAGE_SIZE;
107 info.high_limit = mm->mmap_base;
108 addr = vm_unmapped_area(&info);
Jian Pengd0be89f2011-05-17 12:27:49 -0700109
Michel Lespinasseb6661862012-12-11 16:02:06 -0800110 if (!(addr & ~PAGE_MASK))
111 return addr;
Jian Pengd0be89f2011-05-17 12:27:49 -0700112
Jian Pengd0be89f2011-05-17 12:27:49 -0700113 /*
114 * A failed mmap() very likely causes application failure,
115 * so fall back to the bottom-up function here. This scenario
116 * can happen with large stack limits and large mmap()
117 * allocations.
118 */
Jian Pengd0be89f2011-05-17 12:27:49 -0700119 }
Michel Lespinasseb6661862012-12-11 16:02:06 -0800120
121 info.flags = 0;
122 info.low_limit = mm->mmap_base;
123 info.high_limit = TASK_SIZE;
124 return vm_unmapped_area(&info);
Jian Pengd0be89f2011-05-17 12:27:49 -0700125}
126
127unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr0,
128 unsigned long len, unsigned long pgoff, unsigned long flags)
129{
Kevin Cernekee16650102011-06-18 11:28:48 -0700130 return arch_get_unmapped_area_common(filp,
Jian Pengd0be89f2011-05-17 12:27:49 -0700131 addr0, len, pgoff, flags, UP);
132}
133
134/*
135 * There is no need to export this but sched.h declares the function as
136 * extern so making it static here results in an error.
137 */
138unsigned long arch_get_unmapped_area_topdown(struct file *filp,
139 unsigned long addr0, unsigned long len, unsigned long pgoff,
140 unsigned long flags)
141{
Kevin Cernekee16650102011-06-18 11:28:48 -0700142 return arch_get_unmapped_area_common(filp,
Jian Pengd0be89f2011-05-17 12:27:49 -0700143 addr0, len, pgoff, flags, DOWN);
Ralf Baechle6f6c3c332011-05-19 09:21:33 +0100144}
145
Kees Cook2b68f6c2015-04-14 15:48:00 -0700146unsigned long arch_mmap_rnd(void)
Kees Cook1f0569d2015-04-14 15:47:51 -0700147{
148 unsigned long rnd;
149
Matt Redfearn109c32f2016-11-24 17:32:45 +0000150#ifdef CONFIG_COMPAT
Kees Cook1f0569d2015-04-14 15:47:51 -0700151 if (TASK_IS_32BIT_ADDR)
Matt Redfearn109c32f2016-11-24 17:32:45 +0000152 rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1);
Kees Cook1f0569d2015-04-14 15:47:51 -0700153 else
Matt Redfearn109c32f2016-11-24 17:32:45 +0000154#endif /* CONFIG_COMPAT */
155 rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
Kees Cook1f0569d2015-04-14 15:47:51 -0700156
Matt Redfearn109c32f2016-11-24 17:32:45 +0000157 return rnd << PAGE_SHIFT;
Kees Cook1f0569d2015-04-14 15:47:51 -0700158}
159
Ralf Baechle6f6c3c332011-05-19 09:21:33 +0100160void arch_pick_mmap_layout(struct mm_struct *mm)
161{
162 unsigned long random_factor = 0UL;
163
Kees Cook1f0569d2015-04-14 15:47:51 -0700164 if (current->flags & PF_RANDOMIZE)
Kees Cook2b68f6c2015-04-14 15:48:00 -0700165 random_factor = arch_mmap_rnd();
Ralf Baechle6f6c3c332011-05-19 09:21:33 +0100166
Jian Pengd0be89f2011-05-17 12:27:49 -0700167 if (mmap_is_legacy()) {
168 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
169 mm->get_unmapped_area = arch_get_unmapped_area;
Jian Pengd0be89f2011-05-17 12:27:49 -0700170 } else {
171 mm->mmap_base = mmap_base(random_factor);
172 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
Jian Pengd0be89f2011-05-17 12:27:49 -0700173 }
Ralf Baechle6f6c3c332011-05-19 09:21:33 +0100174}
175
176static inline unsigned long brk_rnd(void)
177{
Daniel Cashman5ef11c32016-02-26 15:19:37 -0800178 unsigned long rnd = get_random_long();
Ralf Baechle6f6c3c332011-05-19 09:21:33 +0100179
180 rnd = rnd << PAGE_SHIFT;
181 /* 8MB for 32bit, 256MB for 64bit */
182 if (TASK_IS_32BIT_ADDR)
183 rnd = rnd & 0x7ffffful;
184 else
185 rnd = rnd & 0xffffffful;
186
187 return rnd;
188}
189
190unsigned long arch_randomize_brk(struct mm_struct *mm)
191{
192 unsigned long base = mm->brk;
193 unsigned long ret;
194
195 ret = PAGE_ALIGN(base + brk_rnd());
196
197 if (ret < mm->brk)
198 return mm->brk;
199
200 return ret;
201}
Steven Rostedt196897a2013-01-25 17:13:15 +0000202
203int __virt_addr_valid(const volatile void *kaddr)
204{
205 return pfn_valid(PFN_DOWN(virt_to_phys(kaddr)));
206}
207EXPORT_SYMBOL_GPL(__virt_addr_valid);