powerpc/mm/radix: Use mm->task_size for boundary checking instead of addr_limit
[linux-2.6-block.git] / arch / powerpc / mm / mmap.c
CommitLineData
1da177e4 1/*
1da177e4
LT
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
25#include <linux/personality.h>
26#include <linux/mm.h>
9f14c42d 27#include <linux/random.h>
3f07c014 28#include <linux/sched/signal.h>
01042607 29#include <linux/sched/mm.h>
7f92bc56 30#include <linux/elf-randomize.h>
7a0eedee
AK
31#include <linux/security.h>
32#include <linux/mman.h>
1da177e4
LT
33
34/*
35 * Top of mmap area (just below the process stack).
36 *
002b0ec7
AB
37 * Leave at least a ~128 MB hole on 32bit applications.
38 *
39 * On 64bit applications we randomise the stack by 1GB so we need to
40 * space our mmap start address by a further 1GB, otherwise there is a
41 * chance the mmap area will end up closer to the stack than our ulimit
42 * requires.
1da177e4 43 */
002b0ec7
AB
44#define MIN_GAP32 (128*1024*1024)
45#define MIN_GAP64 ((128 + 1024)*1024*1024UL)
46#define MIN_GAP ((is_32bit_task()) ? MIN_GAP32 : MIN_GAP64)
1da177e4
LT
47#define MAX_GAP (TASK_SIZE/6*5)
48
13a2cb36
AB
49static inline int mmap_is_legacy(void)
50{
51 if (current->personality & ADDR_COMPAT_LAYOUT)
52 return 1;
53
4bf936b9 54 if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
13a2cb36
AB
55 return 1;
56
57 return sysctl_legacy_va_layout;
58}
59
2b68f6ca 60unsigned long arch_mmap_rnd(void)
9f14c42d 61{
ed632274
KC
62 unsigned long rnd;
63
64 /* 8MB for 32bit, 1GB for 64bit */
65 if (is_32bit_task())
5ef11c35 66 rnd = get_random_long() % (1<<(23-PAGE_SHIFT));
ed632274 67 else
5ef11c35 68 rnd = get_random_long() % (1UL<<(30-PAGE_SHIFT));
9f14c42d 69
fa8cbaaf 70 return rnd << PAGE_SHIFT;
9f14c42d
AB
71}
72
ed632274 73static inline unsigned long mmap_base(unsigned long rnd)
1da177e4 74{
4bf936b9 75 unsigned long gap = rlimit(RLIMIT_STACK);
1da177e4
LT
76
77 if (gap < MIN_GAP)
78 gap = MIN_GAP;
79 else if (gap > MAX_GAP)
80 gap = MAX_GAP;
81
f4ea6dcb 82 return PAGE_ALIGN(DEFAULT_MAP_WINDOW - gap - rnd);
1da177e4
LT
83}
84
7a0eedee
AK
85#ifdef CONFIG_PPC_RADIX_MMU
86/*
87 * Same function as generic code used only for radix, because we don't need to overload
88 * the generic one. But we will have to duplicate, because hash select
89 * HAVE_ARCH_UNMAPPED_AREA
90 */
91static unsigned long
92radix__arch_get_unmapped_area(struct file *filp, unsigned long addr,
93 unsigned long len, unsigned long pgoff,
94 unsigned long flags)
95{
96 struct mm_struct *mm = current->mm;
97 struct vm_area_struct *vma;
98 struct vm_unmapped_area_info info;
99
f4ea6dcb
AK
100 if (unlikely(addr > mm->context.addr_limit && addr < TASK_SIZE))
101 mm->context.addr_limit = TASK_SIZE;
102
be77e999 103 if (len > mm->task_size - mmap_min_addr)
7a0eedee
AK
104 return -ENOMEM;
105
106 if (flags & MAP_FIXED)
107 return addr;
108
109 if (addr) {
110 addr = PAGE_ALIGN(addr);
111 vma = find_vma(mm, addr);
be77e999 112 if (mm->task_size - len >= addr && addr >= mmap_min_addr &&
7a0eedee
AK
113 (!vma || addr + len <= vma->vm_start))
114 return addr;
115 }
116
117 info.flags = 0;
118 info.length = len;
119 info.low_limit = mm->mmap_base;
7a0eedee 120 info.align_mask = 0;
f4ea6dcb
AK
121
122 if (unlikely(addr > DEFAULT_MAP_WINDOW))
123 info.high_limit = mm->context.addr_limit;
124 else
125 info.high_limit = DEFAULT_MAP_WINDOW;
126
7a0eedee
AK
127 return vm_unmapped_area(&info);
128}
129
130static unsigned long
131radix__arch_get_unmapped_area_topdown(struct file *filp,
132 const unsigned long addr0,
133 const unsigned long len,
134 const unsigned long pgoff,
135 const unsigned long flags)
136{
137 struct vm_area_struct *vma;
138 struct mm_struct *mm = current->mm;
139 unsigned long addr = addr0;
140 struct vm_unmapped_area_info info;
141
f4ea6dcb
AK
142 if (unlikely(addr > mm->context.addr_limit && addr < TASK_SIZE))
143 mm->context.addr_limit = TASK_SIZE;
144
7a0eedee 145 /* requested length too big for entire address space */
be77e999 146 if (len > mm->task_size - mmap_min_addr)
7a0eedee
AK
147 return -ENOMEM;
148
149 if (flags & MAP_FIXED)
150 return addr;
151
152 /* requesting a specific address */
153 if (addr) {
154 addr = PAGE_ALIGN(addr);
155 vma = find_vma(mm, addr);
be77e999 156 if (mm->task_size - len >= addr && addr >= mmap_min_addr &&
7a0eedee
AK
157 (!vma || addr + len <= vma->vm_start))
158 return addr;
159 }
160
161 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
162 info.length = len;
163 info.low_limit = max(PAGE_SIZE, mmap_min_addr);
164 info.high_limit = mm->mmap_base;
165 info.align_mask = 0;
f4ea6dcb
AK
166
167 if (addr > DEFAULT_MAP_WINDOW)
168 info.high_limit += mm->context.addr_limit - DEFAULT_MAP_WINDOW;
169
7a0eedee 170 addr = vm_unmapped_area(&info);
f4ea6dcb
AK
171 if (!(addr & ~PAGE_MASK))
172 return addr;
173 VM_BUG_ON(addr != -ENOMEM);
7a0eedee
AK
174
175 /*
176 * A failed mmap() very likely causes application failure,
177 * so fall back to the bottom-up function here. This scenario
178 * can happen with large stack limits and large mmap()
179 * allocations.
180 */
f4ea6dcb 181 return radix__arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
7a0eedee
AK
182}
183
184static void radix__arch_pick_mmap_layout(struct mm_struct *mm,
185 unsigned long random_factor)
186{
187 if (mmap_is_legacy()) {
188 mm->mmap_base = TASK_UNMAPPED_BASE;
189 mm->get_unmapped_area = radix__arch_get_unmapped_area;
190 } else {
191 mm->mmap_base = mmap_base(random_factor);
192 mm->get_unmapped_area = radix__arch_get_unmapped_area_topdown;
193 }
194}
195#else
196/* dummy */
197extern void radix__arch_pick_mmap_layout(struct mm_struct *mm,
198 unsigned long random_factor);
199#endif
1da177e4
LT
200/*
201 * This function, called very early during the creation of a new
202 * process VM image, sets up which VM layout function to use:
203 */
204void arch_pick_mmap_layout(struct mm_struct *mm)
205{
ed632274
KC
206 unsigned long random_factor = 0UL;
207
208 if (current->flags & PF_RANDOMIZE)
2b68f6ca 209 random_factor = arch_mmap_rnd();
ed632274 210
7a0eedee
AK
211 if (radix_enabled())
212 return radix__arch_pick_mmap_layout(mm, random_factor);
1da177e4
LT
213 /*
214 * Fall back to the standard layout if the personality
215 * bit is set, or if the expected stack growth is unlimited:
216 */
217 if (mmap_is_legacy()) {
218 mm->mmap_base = TASK_UNMAPPED_BASE;
219 mm->get_unmapped_area = arch_get_unmapped_area;
1da177e4 220 } else {
ed632274 221 mm->mmap_base = mmap_base(random_factor);
1da177e4 222 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
1da177e4
LT
223 }
224}