GNU Linux-libre 4.14.290-gnu1
[releases.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.
38  */
39 #define MIN_GAP (128*1024*1024)
40 #define MAX_GAP (TASK_SIZE/6*5)
41
42 static inline int mmap_is_legacy(void)
43 {
44         if (current->personality & ADDR_COMPAT_LAYOUT)
45                 return 1;
46
47         if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
48                 return 1;
49
50         return sysctl_legacy_va_layout;
51 }
52
53 unsigned long arch_mmap_rnd(void)
54 {
55         unsigned long shift, rnd;
56
57         shift = mmap_rnd_bits;
58 #ifdef CONFIG_COMPAT
59         if (is_32bit_task())
60                 shift = mmap_rnd_compat_bits;
61 #endif
62         rnd = get_random_long() % (1ul << shift);
63
64         return rnd << PAGE_SHIFT;
65 }
66
67 static inline unsigned long stack_maxrandom_size(void)
68 {
69         if (!(current->flags & PF_RANDOMIZE))
70                 return 0;
71
72         /* 8MB for 32bit, 1GB for 64bit */
73         if (is_32bit_task())
74                 return (1<<23);
75         else
76                 return (1<<30);
77 }
78
79 static inline unsigned long mmap_base(unsigned long rnd)
80 {
81         unsigned long gap = rlimit(RLIMIT_STACK);
82         unsigned long pad = stack_maxrandom_size() + stack_guard_gap;
83
84         /* Values close to RLIM_INFINITY can overflow. */
85         if (gap + pad > gap)
86                 gap += pad;
87
88         if (gap < MIN_GAP)
89                 gap = MIN_GAP;
90         else if (gap > MAX_GAP)
91                 gap = MAX_GAP;
92
93         return PAGE_ALIGN(DEFAULT_MAP_WINDOW - gap - rnd);
94 }
95
96 #ifdef CONFIG_PPC_RADIX_MMU
97 /*
98  * Same function as generic code used only for radix, because we don't need to overload
99  * the generic one. But we will have to duplicate, because hash select
100  * HAVE_ARCH_UNMAPPED_AREA
101  */
102 static unsigned long
103 radix__arch_get_unmapped_area(struct file *filp, unsigned long addr,
104                              unsigned long len, unsigned long pgoff,
105                              unsigned long flags)
106 {
107         struct mm_struct *mm = current->mm;
108         struct vm_area_struct *vma;
109         int fixed = (flags & MAP_FIXED);
110         unsigned long high_limit;
111         struct vm_unmapped_area_info info;
112
113         high_limit = DEFAULT_MAP_WINDOW;
114         if (addr >= high_limit || (fixed && (addr + len > high_limit)))
115                 high_limit = TASK_SIZE;
116
117         if (len > high_limit)
118                 return -ENOMEM;
119         if (fixed) {
120                 if (addr > high_limit - len)
121                         return -ENOMEM;
122         }
123
124         if (unlikely(addr > mm->context.addr_limit &&
125                      mm->context.addr_limit != TASK_SIZE))
126                 mm->context.addr_limit = TASK_SIZE;
127
128         if (fixed)
129                 return addr;
130
131         if (addr) {
132                 addr = PAGE_ALIGN(addr);
133                 vma = find_vma(mm, addr);
134                 if (high_limit - len >= addr && addr >= mmap_min_addr &&
135                     (!vma || addr + len <= vm_start_gap(vma)))
136                         return addr;
137         }
138
139         info.flags = 0;
140         info.length = len;
141         info.low_limit = mm->mmap_base;
142         info.high_limit = high_limit;
143         info.align_mask = 0;
144
145         return vm_unmapped_area(&info);
146 }
147
148 static unsigned long
149 radix__arch_get_unmapped_area_topdown(struct file *filp,
150                                      const unsigned long addr0,
151                                      const unsigned long len,
152                                      const unsigned long pgoff,
153                                      const unsigned long flags)
154 {
155         struct vm_area_struct *vma;
156         struct mm_struct *mm = current->mm;
157         unsigned long addr = addr0;
158         int fixed = (flags & MAP_FIXED);
159         unsigned long high_limit;
160         struct vm_unmapped_area_info info;
161
162         high_limit = DEFAULT_MAP_WINDOW;
163         if (addr >= high_limit || (fixed && (addr + len > high_limit)))
164                 high_limit = TASK_SIZE;
165
166         if (len > high_limit)
167                 return -ENOMEM;
168         if (fixed) {
169                 if (addr > high_limit - len)
170                         return -ENOMEM;
171         }
172
173         if (unlikely(addr > mm->context.addr_limit &&
174                      mm->context.addr_limit != TASK_SIZE))
175                 mm->context.addr_limit = TASK_SIZE;
176
177         if (fixed)
178                 return addr;
179
180         if (addr) {
181                 addr = PAGE_ALIGN(addr);
182                 vma = find_vma(mm, addr);
183                 if (high_limit - len >= addr && addr >= mmap_min_addr &&
184                     (!vma || addr + len <= vm_start_gap(vma)))
185                         return addr;
186         }
187
188         info.flags = VM_UNMAPPED_AREA_TOPDOWN;
189         info.length = len;
190         info.low_limit = max(PAGE_SIZE, mmap_min_addr);
191         info.high_limit = mm->mmap_base + (high_limit - DEFAULT_MAP_WINDOW);
192         info.align_mask = 0;
193
194         addr = vm_unmapped_area(&info);
195         if (!(addr & ~PAGE_MASK))
196                 return addr;
197         VM_BUG_ON(addr != -ENOMEM);
198
199         /*
200          * A failed mmap() very likely causes application failure,
201          * so fall back to the bottom-up function here. This scenario
202          * can happen with large stack limits and large mmap()
203          * allocations.
204          */
205         return radix__arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
206 }
207
208 static void radix__arch_pick_mmap_layout(struct mm_struct *mm,
209                                         unsigned long random_factor)
210 {
211         if (mmap_is_legacy()) {
212                 mm->mmap_base = TASK_UNMAPPED_BASE;
213                 mm->get_unmapped_area = radix__arch_get_unmapped_area;
214         } else {
215                 mm->mmap_base = mmap_base(random_factor);
216                 mm->get_unmapped_area = radix__arch_get_unmapped_area_topdown;
217         }
218 }
219 #else
220 /* dummy */
221 extern void radix__arch_pick_mmap_layout(struct mm_struct *mm,
222                                         unsigned long random_factor);
223 #endif
224 /*
225  * This function, called very early during the creation of a new
226  * process VM image, sets up which VM layout function to use:
227  */
228 void arch_pick_mmap_layout(struct mm_struct *mm)
229 {
230         unsigned long random_factor = 0UL;
231
232         if (current->flags & PF_RANDOMIZE)
233                 random_factor = arch_mmap_rnd();
234
235         if (radix_enabled())
236                 return radix__arch_pick_mmap_layout(mm, random_factor);
237         /*
238          * Fall back to the standard layout if the personality
239          * bit is set, or if the expected stack growth is unlimited:
240          */
241         if (mmap_is_legacy()) {
242                 mm->mmap_base = TASK_UNMAPPED_BASE;
243                 mm->get_unmapped_area = arch_get_unmapped_area;
244         } else {
245                 mm->mmap_base = mmap_base(random_factor);
246                 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
247         }
248 }