Merge branch 'bind_unbind' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / arch / mips / mm / mmap.c
CommitLineData
6f6c3c33
RB
1/*
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 */
16650107 9#include <linux/compiler.h>
6f6c3c33
RB
10#include <linux/errno.h>
11#include <linux/mm.h>
12#include <linux/mman.h>
d9ba5778 13#include <linux/export.h>
d0be89f6 14#include <linux/personality.h>
6f6c3c33 15#include <linux/random.h>
3f07c014 16#include <linux/sched/signal.h>
01042607 17#include <linux/sched/mm.h>
6f6c3c33
RB
18
19unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
6f6c3c33
RB
20EXPORT_SYMBOL(shm_align_mask);
21
d0be89f6
JP
22/* gap between mmap and stack */
23#define MIN_GAP (128*1024*1024UL)
16650107 24#define MAX_GAP ((TASK_SIZE)/6*5)
d0be89f6
JP
25
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
16650107 49#define COLOUR_ALIGN(addr, pgoff) \
6f6c3c33
RB
50 ((((addr) + shm_align_mask) & ~shm_align_mask) + \
51 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
52
d0be89f6
JP
53enum mmap_allocation_direction {UP, DOWN};
54
16650107 55static unsigned long arch_get_unmapped_area_common(struct file *filp,
d0be89f6
JP
56 unsigned long addr0, unsigned long len, unsigned long pgoff,
57 unsigned long flags, enum mmap_allocation_direction dir)
6f6c3c33 58{
d0be89f6
JP
59 struct mm_struct *mm = current->mm;
60 struct vm_area_struct *vma;
61 unsigned long addr = addr0;
6f6c3c33 62 int do_color_align;
b6661861 63 struct vm_unmapped_area_info info;
6f6c3c33 64
d0be89f6 65 if (unlikely(len > TASK_SIZE))
6f6c3c33
RB
66 return -ENOMEM;
67
68 if (flags & MAP_FIXED) {
d0be89f6 69 /* Even MAP_FIXED mappings must reside within TASK_SIZE */
6f6c3c33
RB
70 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;
d0be89f6
JP
86
87 /* requesting a specific address */
6f6c3c33
RB
88 if (addr) {
89 if (do_color_align)
90 addr = COLOUR_ALIGN(addr, pgoff);
91 else
92 addr = PAGE_ALIGN(addr);
d0be89f6
JP
93
94 vma = find_vma(mm, addr);
6f6c3c33 95 if (TASK_SIZE - len >= addr &&
1be7107f 96 (!vma || addr + len <= vm_start_gap(vma)))
6f6c3c33
RB
97 return addr;
98 }
6f6c3c33 99
b6661861
ML
100 info.length = len;
101 info.align_mask = do_color_align ? (PAGE_MASK & shm_align_mask) : 0;
102 info.align_offset = pgoff << PAGE_SHIFT;
d0be89f6 103
b6661861
ML
104 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);
109
110 if (!(addr & ~PAGE_MASK))
111 return addr;
d0be89f6 112
d0be89f6
JP
113 /*
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 */
6f6c3c33 119 }
b6661861
ML
120
121 info.flags = 0;
122 info.low_limit = mm->mmap_base;
123 info.high_limit = TASK_SIZE;
124 return vm_unmapped_area(&info);
6f6c3c33
RB
125}
126
d0be89f6
JP
127unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr0,
128 unsigned long len, unsigned long pgoff, unsigned long flags)
129{
16650107 130 return arch_get_unmapped_area_common(filp,
d0be89f6
JP
131 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{
16650107 142 return arch_get_unmapped_area_common(filp,
d0be89f6
JP
143 addr0, len, pgoff, flags, DOWN);
144}
145
2b68f6ca 146unsigned long arch_mmap_rnd(void)
1f0569df
KC
147{
148 unsigned long rnd;
149
109c32ff 150#ifdef CONFIG_COMPAT
1f0569df 151 if (TASK_IS_32BIT_ADDR)
109c32ff 152 rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1);
1f0569df 153 else
109c32ff
MR
154#endif /* CONFIG_COMPAT */
155 rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
1f0569df 156
109c32ff 157 return rnd << PAGE_SHIFT;
1f0569df
KC
158}
159
6f6c3c33
RB
160void arch_pick_mmap_layout(struct mm_struct *mm)
161{
162 unsigned long random_factor = 0UL;
163
1f0569df 164 if (current->flags & PF_RANDOMIZE)
2b68f6ca 165 random_factor = arch_mmap_rnd();
6f6c3c33 166
d0be89f6
JP
167 if (mmap_is_legacy()) {
168 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
169 mm->get_unmapped_area = arch_get_unmapped_area;
d0be89f6
JP
170 } else {
171 mm->mmap_base = mmap_base(random_factor);
172 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
d0be89f6 173 }
6f6c3c33
RB
174}
175
176static inline unsigned long brk_rnd(void)
177{
5ef11c35 178 unsigned long rnd = get_random_long();
6f6c3c33
RB
179
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}
196897a2
SR
202
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);