Merge tag 'ktest-v4.11-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux-2.6-block.git] / arch / powerpc / mm / mmap.c
1 /*
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>
27 #include <linux/random.h>
28 #include <linux/sched/signal.h>
29 #include <linux/sched/mm.h>
30 #include <linux/elf-randomize.h>
31 #include <linux/security.h>
32 #include <linux/mman.h>
33
34 /*
35  * Top of mmap area (just below the process stack).
36  *
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.
43  */
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)
47 #define MAX_GAP (TASK_SIZE/6*5)
48
49 static inline int mmap_is_legacy(void)
50 {
51         if (current->personality & ADDR_COMPAT_LAYOUT)
52                 return 1;
53
54         if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
55                 return 1;
56
57         return sysctl_legacy_va_layout;
58 }
59
60 unsigned long arch_mmap_rnd(void)
61 {
62         unsigned long rnd;
63
64         /* 8MB for 32bit, 1GB for 64bit */
65         if (is_32bit_task())
66                 rnd = get_random_long() % (1<<(23-PAGE_SHIFT));
67         else
68                 rnd = get_random_long() % (1UL<<(30-PAGE_SHIFT));
69
70         return rnd << PAGE_SHIFT;
71 }
72
73 static inline unsigned long mmap_base(unsigned long rnd)
74 {
75         unsigned long gap = rlimit(RLIMIT_STACK);
76
77         if (gap < MIN_GAP)
78                 gap = MIN_GAP;
79         else if (gap > MAX_GAP)
80                 gap = MAX_GAP;
81
82         return PAGE_ALIGN(TASK_SIZE - gap - rnd);
83 }
84
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  */
91 static unsigned long
92 radix__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
100         if (len > TASK_SIZE - mmap_min_addr)
101                 return -ENOMEM;
102
103         if (flags & MAP_FIXED)
104                 return addr;
105
106         if (addr) {
107                 addr = PAGE_ALIGN(addr);
108                 vma = find_vma(mm, addr);
109                 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
110                     (!vma || addr + len <= vma->vm_start))
111                         return addr;
112         }
113
114         info.flags = 0;
115         info.length = len;
116         info.low_limit = mm->mmap_base;
117         info.high_limit = TASK_SIZE;
118         info.align_mask = 0;
119         return vm_unmapped_area(&info);
120 }
121
122 static unsigned long
123 radix__arch_get_unmapped_area_topdown(struct file *filp,
124                                      const unsigned long addr0,
125                                      const unsigned long len,
126                                      const unsigned long pgoff,
127                                      const unsigned long flags)
128 {
129         struct vm_area_struct *vma;
130         struct mm_struct *mm = current->mm;
131         unsigned long addr = addr0;
132         struct vm_unmapped_area_info info;
133
134         /* requested length too big for entire address space */
135         if (len > TASK_SIZE - mmap_min_addr)
136                 return -ENOMEM;
137
138         if (flags & MAP_FIXED)
139                 return addr;
140
141         /* requesting a specific address */
142         if (addr) {
143                 addr = PAGE_ALIGN(addr);
144                 vma = find_vma(mm, addr);
145                 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
146                                 (!vma || addr + len <= vma->vm_start))
147                         return addr;
148         }
149
150         info.flags = VM_UNMAPPED_AREA_TOPDOWN;
151         info.length = len;
152         info.low_limit = max(PAGE_SIZE, mmap_min_addr);
153         info.high_limit = mm->mmap_base;
154         info.align_mask = 0;
155         addr = vm_unmapped_area(&info);
156
157         /*
158          * A failed mmap() very likely causes application failure,
159          * so fall back to the bottom-up function here. This scenario
160          * can happen with large stack limits and large mmap()
161          * allocations.
162          */
163         if (addr & ~PAGE_MASK) {
164                 VM_BUG_ON(addr != -ENOMEM);
165                 info.flags = 0;
166                 info.low_limit = TASK_UNMAPPED_BASE;
167                 info.high_limit = TASK_SIZE;
168                 addr = vm_unmapped_area(&info);
169         }
170
171         return addr;
172 }
173
174 static void radix__arch_pick_mmap_layout(struct mm_struct *mm,
175                                         unsigned long random_factor)
176 {
177         if (mmap_is_legacy()) {
178                 mm->mmap_base = TASK_UNMAPPED_BASE;
179                 mm->get_unmapped_area = radix__arch_get_unmapped_area;
180         } else {
181                 mm->mmap_base = mmap_base(random_factor);
182                 mm->get_unmapped_area = radix__arch_get_unmapped_area_topdown;
183         }
184 }
185 #else
186 /* dummy */
187 extern void radix__arch_pick_mmap_layout(struct mm_struct *mm,
188                                         unsigned long random_factor);
189 #endif
190 /*
191  * This function, called very early during the creation of a new
192  * process VM image, sets up which VM layout function to use:
193  */
194 void arch_pick_mmap_layout(struct mm_struct *mm)
195 {
196         unsigned long random_factor = 0UL;
197
198         if (current->flags & PF_RANDOMIZE)
199                 random_factor = arch_mmap_rnd();
200
201         if (radix_enabled())
202                 return radix__arch_pick_mmap_layout(mm, random_factor);
203         /*
204          * Fall back to the standard layout if the personality
205          * bit is set, or if the expected stack growth is unlimited:
206          */
207         if (mmap_is_legacy()) {
208                 mm->mmap_base = TASK_UNMAPPED_BASE;
209                 mm->get_unmapped_area = arch_get_unmapped_area;
210         } else {
211                 mm->mmap_base = mmap_base(random_factor);
212                 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
213         }
214 }