2 * flexible mmap layout support
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
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.
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.
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
22 * Started by Ingo Molnar <mingo@elte.hu>
25 #include <linux/personality.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>
35 * Top of mmap area (just below the process stack).
37 * Leave at least a ~128 MB hole.
39 #define MIN_GAP (128*1024*1024)
40 #define MAX_GAP (TASK_SIZE/6*5)
42 static inline int mmap_is_legacy(struct rlimit
*rlim_stack
)
44 if (current
->personality
& ADDR_COMPAT_LAYOUT
)
47 if (rlim_stack
->rlim_cur
== RLIM_INFINITY
)
50 return sysctl_legacy_va_layout
;
53 unsigned long arch_mmap_rnd(void)
55 unsigned long shift
, rnd
;
57 shift
= mmap_rnd_bits
;
60 shift
= mmap_rnd_compat_bits
;
62 rnd
= get_random_long() % (1ul << shift
);
64 return rnd
<< PAGE_SHIFT
;
67 static inline unsigned long stack_maxrandom_size(void)
69 if (!(current
->flags
& PF_RANDOMIZE
))
72 /* 8MB for 32bit, 1GB for 64bit */
79 static inline unsigned long mmap_base(unsigned long rnd
,
80 struct rlimit
*rlim_stack
)
82 unsigned long gap
= rlim_stack
->rlim_cur
;
83 unsigned long pad
= stack_maxrandom_size() + stack_guard_gap
;
85 /* Values close to RLIM_INFINITY can overflow. */
91 else if (gap
> MAX_GAP
)
94 return PAGE_ALIGN(DEFAULT_MAP_WINDOW
- gap
- rnd
);
97 #ifdef CONFIG_PPC_RADIX_MMU
99 * Same function as generic code used only for radix, because we don't need to overload
100 * the generic one. But we will have to duplicate, because hash select
101 * HAVE_ARCH_UNMAPPED_AREA
104 radix__arch_get_unmapped_area(struct file
*filp
, unsigned long addr
,
105 unsigned long len
, unsigned long pgoff
,
108 struct mm_struct
*mm
= current
->mm
;
109 struct vm_area_struct
*vma
;
110 int fixed
= (flags
& MAP_FIXED
);
111 unsigned long high_limit
;
112 struct vm_unmapped_area_info info
;
114 high_limit
= DEFAULT_MAP_WINDOW
;
115 if (addr
>= high_limit
|| (fixed
&& (addr
+ len
> high_limit
)))
116 high_limit
= TASK_SIZE
;
118 if (len
> high_limit
)
122 if (addr
> high_limit
- len
)
128 addr
= PAGE_ALIGN(addr
);
129 vma
= find_vma(mm
, addr
);
130 if (high_limit
- len
>= addr
&& addr
>= mmap_min_addr
&&
131 (!vma
|| addr
+ len
<= vm_start_gap(vma
)))
137 info
.low_limit
= mm
->mmap_base
;
138 info
.high_limit
= high_limit
;
141 return vm_unmapped_area(&info
);
145 radix__arch_get_unmapped_area_topdown(struct file
*filp
,
146 const unsigned long addr0
,
147 const unsigned long len
,
148 const unsigned long pgoff
,
149 const unsigned long flags
)
151 struct vm_area_struct
*vma
;
152 struct mm_struct
*mm
= current
->mm
;
153 unsigned long addr
= addr0
;
154 int fixed
= (flags
& MAP_FIXED
);
155 unsigned long high_limit
;
156 struct vm_unmapped_area_info info
;
158 high_limit
= DEFAULT_MAP_WINDOW
;
159 if (addr
>= high_limit
|| (fixed
&& (addr
+ len
> high_limit
)))
160 high_limit
= TASK_SIZE
;
162 if (len
> high_limit
)
166 if (addr
> high_limit
- len
)
172 addr
= PAGE_ALIGN(addr
);
173 vma
= find_vma(mm
, addr
);
174 if (high_limit
- len
>= addr
&& addr
>= mmap_min_addr
&&
175 (!vma
|| addr
+ len
<= vm_start_gap(vma
)))
179 info
.flags
= VM_UNMAPPED_AREA_TOPDOWN
;
181 info
.low_limit
= max(PAGE_SIZE
, mmap_min_addr
);
182 info
.high_limit
= mm
->mmap_base
+ (high_limit
- DEFAULT_MAP_WINDOW
);
185 addr
= vm_unmapped_area(&info
);
186 if (!(addr
& ~PAGE_MASK
))
188 VM_BUG_ON(addr
!= -ENOMEM
);
191 * A failed mmap() very likely causes application failure,
192 * so fall back to the bottom-up function here. This scenario
193 * can happen with large stack limits and large mmap()
196 return radix__arch_get_unmapped_area(filp
, addr0
, len
, pgoff
, flags
);
199 static void radix__arch_pick_mmap_layout(struct mm_struct
*mm
,
200 unsigned long random_factor
,
201 struct rlimit
*rlim_stack
)
203 if (mmap_is_legacy(rlim_stack
)) {
204 mm
->mmap_base
= TASK_UNMAPPED_BASE
;
205 mm
->get_unmapped_area
= radix__arch_get_unmapped_area
;
207 mm
->mmap_base
= mmap_base(random_factor
, rlim_stack
);
208 mm
->get_unmapped_area
= radix__arch_get_unmapped_area_topdown
;
213 extern void radix__arch_pick_mmap_layout(struct mm_struct
*mm
,
214 unsigned long random_factor
,
215 struct rlimit
*rlim_stack
);
218 * This function, called very early during the creation of a new
219 * process VM image, sets up which VM layout function to use:
221 void arch_pick_mmap_layout(struct mm_struct
*mm
, struct rlimit
*rlim_stack
)
223 unsigned long random_factor
= 0UL;
225 if (current
->flags
& PF_RANDOMIZE
)
226 random_factor
= arch_mmap_rnd();
229 return radix__arch_pick_mmap_layout(mm
, random_factor
,
232 * Fall back to the standard layout if the personality
233 * bit is set, or if the expected stack growth is unlimited:
235 if (mmap_is_legacy(rlim_stack
)) {
236 mm
->mmap_base
= TASK_UNMAPPED_BASE
;
237 mm
->get_unmapped_area
= arch_get_unmapped_area
;
239 mm
->mmap_base
= mmap_base(random_factor
, rlim_stack
);
240 mm
->get_unmapped_area
= arch_get_unmapped_area_topdown
;