]>
Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * linux/mm/nommu.c | |
4 | * | |
5 | * Replacement code for mm functions to support CPU's that don't | |
6 | * have any form of memory management unit (thus no virtual memory). | |
7 | * | |
8 | * See Documentation/nommu-mmap.txt | |
9 | * | |
8feae131 | 10 | * Copyright (c) 2004-2008 David Howells <[email protected]> |
1da177e4 LT |
11 | * Copyright (c) 2000-2003 David McCullough <[email protected]> |
12 | * Copyright (c) 2000-2001 D Jeff Dionne <[email protected]> | |
13 | * Copyright (c) 2002 Greg Ungerer <[email protected]> | |
29c185e5 | 14 | * Copyright (c) 2007-2010 Paul Mundt <[email protected]> |
1da177e4 LT |
15 | */ |
16 | ||
b1de0d13 MH |
17 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
18 | ||
b95f1b31 | 19 | #include <linux/export.h> |
1da177e4 | 20 | #include <linux/mm.h> |
6e84f315 | 21 | #include <linux/sched/mm.h> |
615d6e87 | 22 | #include <linux/vmacache.h> |
1da177e4 LT |
23 | #include <linux/mman.h> |
24 | #include <linux/swap.h> | |
25 | #include <linux/file.h> | |
26 | #include <linux/highmem.h> | |
27 | #include <linux/pagemap.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/vmalloc.h> | |
1da177e4 LT |
30 | #include <linux/blkdev.h> |
31 | #include <linux/backing-dev.h> | |
3b32123d | 32 | #include <linux/compiler.h> |
1da177e4 LT |
33 | #include <linux/mount.h> |
34 | #include <linux/personality.h> | |
35 | #include <linux/security.h> | |
36 | #include <linux/syscalls.h> | |
120a795d | 37 | #include <linux/audit.h> |
b1de0d13 | 38 | #include <linux/printk.h> |
1da177e4 | 39 | |
7c0f6ba6 | 40 | #include <linux/uaccess.h> |
1da177e4 LT |
41 | #include <asm/tlb.h> |
42 | #include <asm/tlbflush.h> | |
eb8cdec4 | 43 | #include <asm/mmu_context.h> |
8feae131 DH |
44 | #include "internal.h" |
45 | ||
1da177e4 | 46 | void *high_memory; |
944b6874 | 47 | EXPORT_SYMBOL(high_memory); |
1da177e4 LT |
48 | struct page *mem_map; |
49 | unsigned long max_mapnr; | |
5b8bf307 | 50 | EXPORT_SYMBOL(max_mapnr); |
4266c97a | 51 | unsigned long highest_memmap_pfn; |
fc4d5c29 | 52 | int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS; |
1da177e4 LT |
53 | int heap_stack_gap = 0; |
54 | ||
33e5d769 | 55 | atomic_long_t mmap_pages_allocated; |
8feae131 | 56 | |
1da177e4 | 57 | EXPORT_SYMBOL(mem_map); |
1da177e4 | 58 | |
8feae131 DH |
59 | /* list of mapped, potentially shareable regions */ |
60 | static struct kmem_cache *vm_region_jar; | |
61 | struct rb_root nommu_region_tree = RB_ROOT; | |
62 | DECLARE_RWSEM(nommu_region_sem); | |
1da177e4 | 63 | |
f0f37e2f | 64 | const struct vm_operations_struct generic_file_vm_ops = { |
1da177e4 LT |
65 | }; |
66 | ||
1da177e4 LT |
67 | /* |
68 | * Return the total memory allocated for this pointer, not | |
69 | * just what the caller asked for. | |
70 | * | |
71 | * Doesn't have to be accurate, i.e. may have races. | |
72 | */ | |
73 | unsigned int kobjsize(const void *objp) | |
74 | { | |
75 | struct page *page; | |
76 | ||
4016a139 MH |
77 | /* |
78 | * If the object we have should not have ksize performed on it, | |
79 | * return size of 0 | |
80 | */ | |
5a1603be | 81 | if (!objp || !virt_addr_valid(objp)) |
6cfd53fc PM |
82 | return 0; |
83 | ||
84 | page = virt_to_head_page(objp); | |
6cfd53fc PM |
85 | |
86 | /* | |
87 | * If the allocator sets PageSlab, we know the pointer came from | |
88 | * kmalloc(). | |
89 | */ | |
1da177e4 LT |
90 | if (PageSlab(page)) |
91 | return ksize(objp); | |
92 | ||
ab2e83ea PM |
93 | /* |
94 | * If it's not a compound page, see if we have a matching VMA | |
95 | * region. This test is intentionally done in reverse order, | |
96 | * so if there's no VMA, we still fall through and hand back | |
97 | * PAGE_SIZE for 0-order pages. | |
98 | */ | |
99 | if (!PageCompound(page)) { | |
100 | struct vm_area_struct *vma; | |
101 | ||
102 | vma = find_vma(current->mm, (unsigned long)objp); | |
103 | if (vma) | |
104 | return vma->vm_end - vma->vm_start; | |
105 | } | |
106 | ||
6cfd53fc PM |
107 | /* |
108 | * The ksize() function is only guaranteed to work for pointers | |
5a1603be | 109 | * returned by kmalloc(). So handle arbitrary pointers here. |
6cfd53fc | 110 | */ |
5a1603be | 111 | return PAGE_SIZE << compound_order(page); |
1da177e4 LT |
112 | } |
113 | ||
0d731759 | 114 | static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, |
28a35716 ML |
115 | unsigned long start, unsigned long nr_pages, |
116 | unsigned int foll_flags, struct page **pages, | |
117 | struct vm_area_struct **vmas, int *nonblocking) | |
1da177e4 | 118 | { |
910e46da | 119 | struct vm_area_struct *vma; |
7b4d5b8b DH |
120 | unsigned long vm_flags; |
121 | int i; | |
122 | ||
123 | /* calculate required read or write permissions. | |
58fa879e | 124 | * If FOLL_FORCE is set, we only require the "MAY" flags. |
7b4d5b8b | 125 | */ |
58fa879e HD |
126 | vm_flags = (foll_flags & FOLL_WRITE) ? |
127 | (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); | |
128 | vm_flags &= (foll_flags & FOLL_FORCE) ? | |
129 | (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); | |
1da177e4 | 130 | |
9d73777e | 131 | for (i = 0; i < nr_pages; i++) { |
7561e8ca | 132 | vma = find_vma(mm, start); |
7b4d5b8b DH |
133 | if (!vma) |
134 | goto finish_or_fault; | |
135 | ||
136 | /* protect what we can, including chardevs */ | |
1c3aff1c HD |
137 | if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) || |
138 | !(vm_flags & vma->vm_flags)) | |
7b4d5b8b | 139 | goto finish_or_fault; |
910e46da | 140 | |
1da177e4 LT |
141 | if (pages) { |
142 | pages[i] = virt_to_page(start); | |
143 | if (pages[i]) | |
09cbfeaf | 144 | get_page(pages[i]); |
1da177e4 LT |
145 | } |
146 | if (vmas) | |
910e46da | 147 | vmas[i] = vma; |
e1ee65d8 | 148 | start = (start + PAGE_SIZE) & PAGE_MASK; |
1da177e4 | 149 | } |
7b4d5b8b DH |
150 | |
151 | return i; | |
152 | ||
153 | finish_or_fault: | |
154 | return i ? : -EFAULT; | |
1da177e4 | 155 | } |
b291f000 | 156 | |
b291f000 NP |
157 | /* |
158 | * get a list of pages in an address range belonging to the specified process | |
159 | * and indicate the VMA that covers each page | |
160 | * - this is potentially dodgy as we may end incrementing the page count of a | |
161 | * slab page or a secondary page from a compound page | |
162 | * - don't permit access to VMAs that don't support it, such as I/O mappings | |
163 | */ | |
c12d2da5 | 164 | long get_user_pages(unsigned long start, unsigned long nr_pages, |
768ae309 | 165 | unsigned int gup_flags, struct page **pages, |
28a35716 | 166 | struct vm_area_struct **vmas) |
b291f000 | 167 | { |
768ae309 LS |
168 | return __get_user_pages(current, current->mm, start, nr_pages, |
169 | gup_flags, pages, vmas, NULL); | |
b291f000 | 170 | } |
c12d2da5 | 171 | EXPORT_SYMBOL(get_user_pages); |
66aa2b4b | 172 | |
c12d2da5 | 173 | long get_user_pages_locked(unsigned long start, unsigned long nr_pages, |
3b913179 | 174 | unsigned int gup_flags, struct page **pages, |
cde70140 | 175 | int *locked) |
f0818f47 | 176 | { |
768ae309 | 177 | return get_user_pages(start, nr_pages, gup_flags, pages, NULL); |
f0818f47 | 178 | } |
c12d2da5 | 179 | EXPORT_SYMBOL(get_user_pages_locked); |
f0818f47 | 180 | |
8b7457ef LS |
181 | static long __get_user_pages_unlocked(struct task_struct *tsk, |
182 | struct mm_struct *mm, unsigned long start, | |
183 | unsigned long nr_pages, struct page **pages, | |
184 | unsigned int gup_flags) | |
f0818f47 AA |
185 | { |
186 | long ret; | |
187 | down_read(&mm->mmap_sem); | |
cde70140 DH |
188 | ret = __get_user_pages(tsk, mm, start, nr_pages, gup_flags, pages, |
189 | NULL, NULL); | |
f0818f47 AA |
190 | up_read(&mm->mmap_sem); |
191 | return ret; | |
192 | } | |
0fd71a56 | 193 | |
c12d2da5 | 194 | long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages, |
c164154f | 195 | struct page **pages, unsigned int gup_flags) |
0fd71a56 | 196 | { |
cde70140 | 197 | return __get_user_pages_unlocked(current, current->mm, start, nr_pages, |
c164154f | 198 | pages, gup_flags); |
0fd71a56 | 199 | } |
c12d2da5 | 200 | EXPORT_SYMBOL(get_user_pages_unlocked); |
f0818f47 | 201 | |
dfc2f91a PM |
202 | /** |
203 | * follow_pfn - look up PFN at a user virtual address | |
204 | * @vma: memory mapping | |
205 | * @address: user virtual address | |
206 | * @pfn: location to store found PFN | |
207 | * | |
208 | * Only IO mappings and raw PFN mappings are allowed. | |
209 | * | |
210 | * Returns zero and the pfn at @pfn on success, -ve otherwise. | |
211 | */ | |
212 | int follow_pfn(struct vm_area_struct *vma, unsigned long address, | |
213 | unsigned long *pfn) | |
214 | { | |
215 | if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) | |
216 | return -EINVAL; | |
217 | ||
218 | *pfn = address >> PAGE_SHIFT; | |
219 | return 0; | |
220 | } | |
221 | EXPORT_SYMBOL(follow_pfn); | |
222 | ||
f1c4069e | 223 | LIST_HEAD(vmap_area_list); |
1da177e4 | 224 | |
b3bdda02 | 225 | void vfree(const void *addr) |
1da177e4 LT |
226 | { |
227 | kfree(addr); | |
228 | } | |
b5073173 | 229 | EXPORT_SYMBOL(vfree); |
1da177e4 | 230 | |
dd0fc66f | 231 | void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot) |
1da177e4 LT |
232 | { |
233 | /* | |
8518609d RD |
234 | * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc() |
235 | * returns only a logical address. | |
1da177e4 | 236 | */ |
84097518 | 237 | return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM); |
1da177e4 | 238 | } |
b5073173 | 239 | EXPORT_SYMBOL(__vmalloc); |
1da177e4 | 240 | |
a7c3e901 MH |
241 | void *__vmalloc_node_flags(unsigned long size, int node, gfp_t flags) |
242 | { | |
243 | return __vmalloc(size, flags, PAGE_KERNEL); | |
244 | } | |
245 | ||
f905bc44 PM |
246 | void *vmalloc_user(unsigned long size) |
247 | { | |
248 | void *ret; | |
249 | ||
19809c2d | 250 | ret = __vmalloc(size, GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL); |
f905bc44 PM |
251 | if (ret) { |
252 | struct vm_area_struct *vma; | |
253 | ||
254 | down_write(¤t->mm->mmap_sem); | |
255 | vma = find_vma(current->mm, (unsigned long)ret); | |
256 | if (vma) | |
257 | vma->vm_flags |= VM_USERMAP; | |
258 | up_write(¤t->mm->mmap_sem); | |
259 | } | |
260 | ||
261 | return ret; | |
262 | } | |
263 | EXPORT_SYMBOL(vmalloc_user); | |
264 | ||
b3bdda02 | 265 | struct page *vmalloc_to_page(const void *addr) |
1da177e4 LT |
266 | { |
267 | return virt_to_page(addr); | |
268 | } | |
b5073173 | 269 | EXPORT_SYMBOL(vmalloc_to_page); |
1da177e4 | 270 | |
b3bdda02 | 271 | unsigned long vmalloc_to_pfn(const void *addr) |
1da177e4 LT |
272 | { |
273 | return page_to_pfn(virt_to_page(addr)); | |
274 | } | |
b5073173 | 275 | EXPORT_SYMBOL(vmalloc_to_pfn); |
1da177e4 LT |
276 | |
277 | long vread(char *buf, char *addr, unsigned long count) | |
278 | { | |
9bde916b CG |
279 | /* Don't allow overflow */ |
280 | if ((unsigned long) buf + count < count) | |
281 | count = -(unsigned long) buf; | |
282 | ||
1da177e4 LT |
283 | memcpy(buf, addr, count); |
284 | return count; | |
285 | } | |
286 | ||
287 | long vwrite(char *buf, char *addr, unsigned long count) | |
288 | { | |
289 | /* Don't allow overflow */ | |
290 | if ((unsigned long) addr + count < count) | |
291 | count = -(unsigned long) addr; | |
292 | ||
293 | memcpy(addr, buf, count); | |
ac714904 | 294 | return count; |
1da177e4 LT |
295 | } |
296 | ||
297 | /* | |
e1c05067 | 298 | * vmalloc - allocate virtually contiguous memory |
1da177e4 LT |
299 | * |
300 | * @size: allocation size | |
301 | * | |
302 | * Allocate enough pages to cover @size from the page level | |
e1c05067 | 303 | * allocator and map them into contiguous kernel virtual space. |
1da177e4 | 304 | * |
c1c8897f | 305 | * For tight control over page level allocator and protection flags |
1da177e4 LT |
306 | * use __vmalloc() instead. |
307 | */ | |
308 | void *vmalloc(unsigned long size) | |
309 | { | |
310 | return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL); | |
311 | } | |
f6138882 AM |
312 | EXPORT_SYMBOL(vmalloc); |
313 | ||
e1ca7788 | 314 | /* |
e1c05067 | 315 | * vzalloc - allocate virtually contiguous memory with zero fill |
e1ca7788 DY |
316 | * |
317 | * @size: allocation size | |
318 | * | |
319 | * Allocate enough pages to cover @size from the page level | |
e1c05067 | 320 | * allocator and map them into contiguous kernel virtual space. |
e1ca7788 DY |
321 | * The memory allocated is set to zero. |
322 | * | |
323 | * For tight control over page level allocator and protection flags | |
324 | * use __vmalloc() instead. | |
325 | */ | |
326 | void *vzalloc(unsigned long size) | |
327 | { | |
328 | return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, | |
329 | PAGE_KERNEL); | |
330 | } | |
331 | EXPORT_SYMBOL(vzalloc); | |
332 | ||
333 | /** | |
334 | * vmalloc_node - allocate memory on a specific node | |
335 | * @size: allocation size | |
336 | * @node: numa node | |
337 | * | |
338 | * Allocate enough pages to cover @size from the page level | |
339 | * allocator and map them into contiguous kernel virtual space. | |
340 | * | |
341 | * For tight control over page level allocator and protection flags | |
342 | * use __vmalloc() instead. | |
343 | */ | |
f6138882 AM |
344 | void *vmalloc_node(unsigned long size, int node) |
345 | { | |
346 | return vmalloc(size); | |
347 | } | |
9a14f653 | 348 | EXPORT_SYMBOL(vmalloc_node); |
e1ca7788 DY |
349 | |
350 | /** | |
351 | * vzalloc_node - allocate memory on a specific node with zero fill | |
352 | * @size: allocation size | |
353 | * @node: numa node | |
354 | * | |
355 | * Allocate enough pages to cover @size from the page level | |
356 | * allocator and map them into contiguous kernel virtual space. | |
357 | * The memory allocated is set to zero. | |
358 | * | |
359 | * For tight control over page level allocator and protection flags | |
360 | * use __vmalloc() instead. | |
361 | */ | |
362 | void *vzalloc_node(unsigned long size, int node) | |
363 | { | |
364 | return vzalloc(size); | |
365 | } | |
366 | EXPORT_SYMBOL(vzalloc_node); | |
1da177e4 | 367 | |
1af446ed PM |
368 | /** |
369 | * vmalloc_exec - allocate virtually contiguous, executable memory | |
370 | * @size: allocation size | |
371 | * | |
372 | * Kernel-internal function to allocate enough pages to cover @size | |
373 | * the page level allocator and map them into contiguous and | |
374 | * executable kernel virtual space. | |
375 | * | |
376 | * For tight control over page level allocator and protection flags | |
377 | * use __vmalloc() instead. | |
378 | */ | |
379 | ||
380 | void *vmalloc_exec(unsigned long size) | |
381 | { | |
382 | return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC); | |
383 | } | |
384 | ||
b5073173 PM |
385 | /** |
386 | * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) | |
1da177e4 LT |
387 | * @size: allocation size |
388 | * | |
389 | * Allocate enough 32bit PA addressable pages to cover @size from the | |
e1c05067 | 390 | * page level allocator and map them into contiguous kernel virtual space. |
1da177e4 LT |
391 | */ |
392 | void *vmalloc_32(unsigned long size) | |
393 | { | |
394 | return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL); | |
395 | } | |
b5073173 PM |
396 | EXPORT_SYMBOL(vmalloc_32); |
397 | ||
398 | /** | |
399 | * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory | |
400 | * @size: allocation size | |
401 | * | |
402 | * The resulting memory area is 32bit addressable and zeroed so it can be | |
403 | * mapped to userspace without leaking data. | |
f905bc44 PM |
404 | * |
405 | * VM_USERMAP is set on the corresponding VMA so that subsequent calls to | |
406 | * remap_vmalloc_range() are permissible. | |
b5073173 PM |
407 | */ |
408 | void *vmalloc_32_user(unsigned long size) | |
409 | { | |
f905bc44 PM |
410 | /* |
411 | * We'll have to sort out the ZONE_DMA bits for 64-bit, | |
412 | * but for now this can simply use vmalloc_user() directly. | |
413 | */ | |
414 | return vmalloc_user(size); | |
b5073173 PM |
415 | } |
416 | EXPORT_SYMBOL(vmalloc_32_user); | |
1da177e4 LT |
417 | |
418 | void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot) | |
419 | { | |
420 | BUG(); | |
421 | return NULL; | |
422 | } | |
b5073173 | 423 | EXPORT_SYMBOL(vmap); |
1da177e4 | 424 | |
b3bdda02 | 425 | void vunmap(const void *addr) |
1da177e4 LT |
426 | { |
427 | BUG(); | |
428 | } | |
b5073173 | 429 | EXPORT_SYMBOL(vunmap); |
1da177e4 | 430 | |
eb6434d9 PM |
431 | void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot) |
432 | { | |
433 | BUG(); | |
434 | return NULL; | |
435 | } | |
436 | EXPORT_SYMBOL(vm_map_ram); | |
437 | ||
438 | void vm_unmap_ram(const void *mem, unsigned int count) | |
439 | { | |
440 | BUG(); | |
441 | } | |
442 | EXPORT_SYMBOL(vm_unmap_ram); | |
443 | ||
444 | void vm_unmap_aliases(void) | |
445 | { | |
446 | } | |
447 | EXPORT_SYMBOL_GPL(vm_unmap_aliases); | |
448 | ||
1eeb66a1 CH |
449 | /* |
450 | * Implement a stub for vmalloc_sync_all() if the architecture chose not to | |
451 | * have one. | |
452 | */ | |
3b32123d | 453 | void __weak vmalloc_sync_all(void) |
1eeb66a1 CH |
454 | { |
455 | } | |
456 | ||
cd12909c | 457 | struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes) |
29c185e5 PM |
458 | { |
459 | BUG(); | |
460 | return NULL; | |
461 | } | |
462 | EXPORT_SYMBOL_GPL(alloc_vm_area); | |
463 | ||
464 | void free_vm_area(struct vm_struct *area) | |
465 | { | |
466 | BUG(); | |
467 | } | |
468 | EXPORT_SYMBOL_GPL(free_vm_area); | |
469 | ||
b5073173 PM |
470 | int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, |
471 | struct page *page) | |
472 | { | |
473 | return -EINVAL; | |
474 | } | |
475 | EXPORT_SYMBOL(vm_insert_page); | |
476 | ||
a667d745 SJ |
477 | int vm_map_pages(struct vm_area_struct *vma, struct page **pages, |
478 | unsigned long num) | |
479 | { | |
480 | return -EINVAL; | |
481 | } | |
482 | EXPORT_SYMBOL(vm_map_pages); | |
483 | ||
484 | int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages, | |
485 | unsigned long num) | |
486 | { | |
487 | return -EINVAL; | |
488 | } | |
489 | EXPORT_SYMBOL(vm_map_pages_zero); | |
490 | ||
1da177e4 LT |
491 | /* |
492 | * sys_brk() for the most part doesn't need the global kernel | |
493 | * lock, except when an application is doing something nasty | |
494 | * like trying to un-brk an area that has already been mapped | |
495 | * to a regular file. in this case, the unmapping will need | |
496 | * to invoke file system routines that need the global lock. | |
497 | */ | |
6a6160a7 | 498 | SYSCALL_DEFINE1(brk, unsigned long, brk) |
1da177e4 LT |
499 | { |
500 | struct mm_struct *mm = current->mm; | |
501 | ||
502 | if (brk < mm->start_brk || brk > mm->context.end_brk) | |
503 | return mm->brk; | |
504 | ||
505 | if (mm->brk == brk) | |
506 | return mm->brk; | |
507 | ||
508 | /* | |
509 | * Always allow shrinking brk | |
510 | */ | |
511 | if (brk <= mm->brk) { | |
512 | mm->brk = brk; | |
513 | return brk; | |
514 | } | |
515 | ||
516 | /* | |
517 | * Ok, looks good - let it rip. | |
518 | */ | |
cfe79c00 | 519 | flush_icache_range(mm->brk, brk); |
1da177e4 LT |
520 | return mm->brk = brk; |
521 | } | |
522 | ||
8feae131 | 523 | /* |
3edf41d8 | 524 | * initialise the percpu counter for VM and region record slabs |
8feae131 DH |
525 | */ |
526 | void __init mmap_init(void) | |
1da177e4 | 527 | { |
00a62ce9 KM |
528 | int ret; |
529 | ||
908c7f19 | 530 | ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL); |
00a62ce9 | 531 | VM_BUG_ON(ret); |
5d097056 | 532 | vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT); |
1da177e4 | 533 | } |
1da177e4 | 534 | |
3034097a | 535 | /* |
8feae131 DH |
536 | * validate the region tree |
537 | * - the caller must hold the region lock | |
3034097a | 538 | */ |
8feae131 DH |
539 | #ifdef CONFIG_DEBUG_NOMMU_REGIONS |
540 | static noinline void validate_nommu_regions(void) | |
3034097a | 541 | { |
8feae131 DH |
542 | struct vm_region *region, *last; |
543 | struct rb_node *p, *lastp; | |
3034097a | 544 | |
8feae131 DH |
545 | lastp = rb_first(&nommu_region_tree); |
546 | if (!lastp) | |
547 | return; | |
548 | ||
549 | last = rb_entry(lastp, struct vm_region, vm_rb); | |
c9427bc0 GT |
550 | BUG_ON(last->vm_end <= last->vm_start); |
551 | BUG_ON(last->vm_top < last->vm_end); | |
8feae131 DH |
552 | |
553 | while ((p = rb_next(lastp))) { | |
554 | region = rb_entry(p, struct vm_region, vm_rb); | |
555 | last = rb_entry(lastp, struct vm_region, vm_rb); | |
556 | ||
c9427bc0 GT |
557 | BUG_ON(region->vm_end <= region->vm_start); |
558 | BUG_ON(region->vm_top < region->vm_end); | |
559 | BUG_ON(region->vm_start < last->vm_top); | |
3034097a | 560 | |
8feae131 DH |
561 | lastp = p; |
562 | } | |
3034097a | 563 | } |
8feae131 | 564 | #else |
33e5d769 DH |
565 | static void validate_nommu_regions(void) |
566 | { | |
567 | } | |
8feae131 | 568 | #endif |
3034097a DH |
569 | |
570 | /* | |
8feae131 | 571 | * add a region into the global tree |
3034097a | 572 | */ |
8feae131 | 573 | static void add_nommu_region(struct vm_region *region) |
3034097a | 574 | { |
8feae131 DH |
575 | struct vm_region *pregion; |
576 | struct rb_node **p, *parent; | |
3034097a | 577 | |
8feae131 DH |
578 | validate_nommu_regions(); |
579 | ||
8feae131 DH |
580 | parent = NULL; |
581 | p = &nommu_region_tree.rb_node; | |
582 | while (*p) { | |
583 | parent = *p; | |
584 | pregion = rb_entry(parent, struct vm_region, vm_rb); | |
585 | if (region->vm_start < pregion->vm_start) | |
586 | p = &(*p)->rb_left; | |
587 | else if (region->vm_start > pregion->vm_start) | |
588 | p = &(*p)->rb_right; | |
589 | else if (pregion == region) | |
590 | return; | |
591 | else | |
592 | BUG(); | |
3034097a DH |
593 | } |
594 | ||
8feae131 DH |
595 | rb_link_node(®ion->vm_rb, parent, p); |
596 | rb_insert_color(®ion->vm_rb, &nommu_region_tree); | |
3034097a | 597 | |
8feae131 | 598 | validate_nommu_regions(); |
3034097a | 599 | } |
3034097a | 600 | |
930e652a | 601 | /* |
8feae131 | 602 | * delete a region from the global tree |
930e652a | 603 | */ |
8feae131 | 604 | static void delete_nommu_region(struct vm_region *region) |
930e652a | 605 | { |
8feae131 | 606 | BUG_ON(!nommu_region_tree.rb_node); |
930e652a | 607 | |
8feae131 DH |
608 | validate_nommu_regions(); |
609 | rb_erase(®ion->vm_rb, &nommu_region_tree); | |
610 | validate_nommu_regions(); | |
57c8f63e GU |
611 | } |
612 | ||
6fa5f80b | 613 | /* |
8feae131 | 614 | * free a contiguous series of pages |
6fa5f80b | 615 | */ |
8feae131 | 616 | static void free_page_series(unsigned long from, unsigned long to) |
6fa5f80b | 617 | { |
8feae131 DH |
618 | for (; from < to; from += PAGE_SIZE) { |
619 | struct page *page = virt_to_page(from); | |
620 | ||
33e5d769 | 621 | atomic_long_dec(&mmap_pages_allocated); |
8feae131 | 622 | put_page(page); |
6fa5f80b | 623 | } |
6fa5f80b DH |
624 | } |
625 | ||
3034097a | 626 | /* |
8feae131 | 627 | * release a reference to a region |
33e5d769 | 628 | * - the caller must hold the region semaphore for writing, which this releases |
dd8632a1 | 629 | * - the region may not have been added to the tree yet, in which case vm_top |
8feae131 | 630 | * will equal vm_start |
3034097a | 631 | */ |
8feae131 DH |
632 | static void __put_nommu_region(struct vm_region *region) |
633 | __releases(nommu_region_sem) | |
1da177e4 | 634 | { |
8feae131 | 635 | BUG_ON(!nommu_region_tree.rb_node); |
1da177e4 | 636 | |
1e2ae599 | 637 | if (--region->vm_usage == 0) { |
dd8632a1 | 638 | if (region->vm_top > region->vm_start) |
8feae131 DH |
639 | delete_nommu_region(region); |
640 | up_write(&nommu_region_sem); | |
641 | ||
642 | if (region->vm_file) | |
643 | fput(region->vm_file); | |
644 | ||
645 | /* IO memory and memory shared directly out of the pagecache | |
646 | * from ramfs/tmpfs mustn't be released here */ | |
22cc877b | 647 | if (region->vm_flags & VM_MAPPED_COPY) |
dd8632a1 | 648 | free_page_series(region->vm_start, region->vm_top); |
8feae131 DH |
649 | kmem_cache_free(vm_region_jar, region); |
650 | } else { | |
651 | up_write(&nommu_region_sem); | |
1da177e4 | 652 | } |
8feae131 | 653 | } |
1da177e4 | 654 | |
8feae131 DH |
655 | /* |
656 | * release a reference to a region | |
657 | */ | |
658 | static void put_nommu_region(struct vm_region *region) | |
659 | { | |
660 | down_write(&nommu_region_sem); | |
661 | __put_nommu_region(region); | |
1da177e4 LT |
662 | } |
663 | ||
3034097a | 664 | /* |
8feae131 DH |
665 | * add a VMA into a process's mm_struct in the appropriate place in the list |
666 | * and tree and add to the address space's page tree also if not an anonymous | |
667 | * page | |
668 | * - should be called with mm->mmap_sem held writelocked | |
3034097a | 669 | */ |
8feae131 | 670 | static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma) |
1da177e4 | 671 | { |
6038def0 | 672 | struct vm_area_struct *pvma, *prev; |
1da177e4 | 673 | struct address_space *mapping; |
6038def0 | 674 | struct rb_node **p, *parent, *rb_prev; |
8feae131 | 675 | |
8feae131 DH |
676 | BUG_ON(!vma->vm_region); |
677 | ||
678 | mm->map_count++; | |
679 | vma->vm_mm = mm; | |
1da177e4 LT |
680 | |
681 | /* add the VMA to the mapping */ | |
682 | if (vma->vm_file) { | |
683 | mapping = vma->vm_file->f_mapping; | |
684 | ||
83cde9e8 | 685 | i_mmap_lock_write(mapping); |
1da177e4 | 686 | flush_dcache_mmap_lock(mapping); |
6b2dbba8 | 687 | vma_interval_tree_insert(vma, &mapping->i_mmap); |
1da177e4 | 688 | flush_dcache_mmap_unlock(mapping); |
83cde9e8 | 689 | i_mmap_unlock_write(mapping); |
1da177e4 LT |
690 | } |
691 | ||
8feae131 | 692 | /* add the VMA to the tree */ |
6038def0 | 693 | parent = rb_prev = NULL; |
8feae131 | 694 | p = &mm->mm_rb.rb_node; |
1da177e4 LT |
695 | while (*p) { |
696 | parent = *p; | |
697 | pvma = rb_entry(parent, struct vm_area_struct, vm_rb); | |
698 | ||
8feae131 DH |
699 | /* sort by: start addr, end addr, VMA struct addr in that order |
700 | * (the latter is necessary as we may get identical VMAs) */ | |
701 | if (vma->vm_start < pvma->vm_start) | |
1da177e4 | 702 | p = &(*p)->rb_left; |
6038def0 NK |
703 | else if (vma->vm_start > pvma->vm_start) { |
704 | rb_prev = parent; | |
1da177e4 | 705 | p = &(*p)->rb_right; |
6038def0 | 706 | } else if (vma->vm_end < pvma->vm_end) |
8feae131 | 707 | p = &(*p)->rb_left; |
6038def0 NK |
708 | else if (vma->vm_end > pvma->vm_end) { |
709 | rb_prev = parent; | |
8feae131 | 710 | p = &(*p)->rb_right; |
6038def0 | 711 | } else if (vma < pvma) |
8feae131 | 712 | p = &(*p)->rb_left; |
6038def0 NK |
713 | else if (vma > pvma) { |
714 | rb_prev = parent; | |
8feae131 | 715 | p = &(*p)->rb_right; |
6038def0 | 716 | } else |
8feae131 | 717 | BUG(); |
1da177e4 LT |
718 | } |
719 | ||
720 | rb_link_node(&vma->vm_rb, parent, p); | |
8feae131 DH |
721 | rb_insert_color(&vma->vm_rb, &mm->mm_rb); |
722 | ||
723 | /* add VMA to the VMA list also */ | |
6038def0 NK |
724 | prev = NULL; |
725 | if (rb_prev) | |
726 | prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb); | |
8feae131 | 727 | |
6038def0 | 728 | __vma_link_list(mm, vma, prev, parent); |
1da177e4 LT |
729 | } |
730 | ||
3034097a | 731 | /* |
8feae131 | 732 | * delete a VMA from its owning mm_struct and address space |
3034097a | 733 | */ |
8feae131 | 734 | static void delete_vma_from_mm(struct vm_area_struct *vma) |
1da177e4 | 735 | { |
615d6e87 | 736 | int i; |
1da177e4 | 737 | struct address_space *mapping; |
8feae131 | 738 | struct mm_struct *mm = vma->vm_mm; |
615d6e87 | 739 | struct task_struct *curr = current; |
8feae131 | 740 | |
8feae131 | 741 | mm->map_count--; |
615d6e87 DB |
742 | for (i = 0; i < VMACACHE_SIZE; i++) { |
743 | /* if the vma is cached, invalidate the entire cache */ | |
314ff785 | 744 | if (curr->vmacache.vmas[i] == vma) { |
e020d5bd | 745 | vmacache_invalidate(mm); |
615d6e87 DB |
746 | break; |
747 | } | |
748 | } | |
1da177e4 LT |
749 | |
750 | /* remove the VMA from the mapping */ | |
751 | if (vma->vm_file) { | |
752 | mapping = vma->vm_file->f_mapping; | |
753 | ||
83cde9e8 | 754 | i_mmap_lock_write(mapping); |
1da177e4 | 755 | flush_dcache_mmap_lock(mapping); |
6b2dbba8 | 756 | vma_interval_tree_remove(vma, &mapping->i_mmap); |
1da177e4 | 757 | flush_dcache_mmap_unlock(mapping); |
83cde9e8 | 758 | i_mmap_unlock_write(mapping); |
1da177e4 LT |
759 | } |
760 | ||
8feae131 DH |
761 | /* remove from the MM's tree and list */ |
762 | rb_erase(&vma->vm_rb, &mm->mm_rb); | |
b951bf2c NK |
763 | |
764 | if (vma->vm_prev) | |
765 | vma->vm_prev->vm_next = vma->vm_next; | |
766 | else | |
767 | mm->mmap = vma->vm_next; | |
768 | ||
769 | if (vma->vm_next) | |
770 | vma->vm_next->vm_prev = vma->vm_prev; | |
8feae131 DH |
771 | } |
772 | ||
773 | /* | |
774 | * destroy a VMA record | |
775 | */ | |
776 | static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma) | |
777 | { | |
8feae131 DH |
778 | if (vma->vm_ops && vma->vm_ops->close) |
779 | vma->vm_ops->close(vma); | |
e9714acf | 780 | if (vma->vm_file) |
8feae131 | 781 | fput(vma->vm_file); |
8feae131 | 782 | put_nommu_region(vma->vm_region); |
3928d4f5 | 783 | vm_area_free(vma); |
8feae131 DH |
784 | } |
785 | ||
786 | /* | |
787 | * look up the first VMA in which addr resides, NULL if none | |
788 | * - should be called with mm->mmap_sem at least held readlocked | |
789 | */ | |
790 | struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) | |
791 | { | |
792 | struct vm_area_struct *vma; | |
8feae131 DH |
793 | |
794 | /* check the cache first */ | |
615d6e87 DB |
795 | vma = vmacache_find(mm, addr); |
796 | if (likely(vma)) | |
8feae131 DH |
797 | return vma; |
798 | ||
e922c4c5 | 799 | /* trawl the list (there may be multiple mappings in which addr |
8feae131 | 800 | * resides) */ |
e922c4c5 | 801 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
8feae131 DH |
802 | if (vma->vm_start > addr) |
803 | return NULL; | |
804 | if (vma->vm_end > addr) { | |
615d6e87 | 805 | vmacache_update(addr, vma); |
8feae131 DH |
806 | return vma; |
807 | } | |
808 | } | |
809 | ||
810 | return NULL; | |
811 | } | |
812 | EXPORT_SYMBOL(find_vma); | |
813 | ||
814 | /* | |
815 | * find a VMA | |
816 | * - we don't extend stack VMAs under NOMMU conditions | |
817 | */ | |
818 | struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr) | |
819 | { | |
7561e8ca | 820 | return find_vma(mm, addr); |
8feae131 DH |
821 | } |
822 | ||
823 | /* | |
824 | * expand a stack to a given address | |
825 | * - not supported under NOMMU conditions | |
826 | */ | |
827 | int expand_stack(struct vm_area_struct *vma, unsigned long address) | |
828 | { | |
829 | return -ENOMEM; | |
830 | } | |
831 | ||
832 | /* | |
833 | * look up the first VMA exactly that exactly matches addr | |
834 | * - should be called with mm->mmap_sem at least held readlocked | |
835 | */ | |
836 | static struct vm_area_struct *find_vma_exact(struct mm_struct *mm, | |
837 | unsigned long addr, | |
838 | unsigned long len) | |
839 | { | |
840 | struct vm_area_struct *vma; | |
8feae131 DH |
841 | unsigned long end = addr + len; |
842 | ||
843 | /* check the cache first */ | |
615d6e87 DB |
844 | vma = vmacache_find_exact(mm, addr, end); |
845 | if (vma) | |
8feae131 DH |
846 | return vma; |
847 | ||
e922c4c5 | 848 | /* trawl the list (there may be multiple mappings in which addr |
8feae131 | 849 | * resides) */ |
e922c4c5 | 850 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
8feae131 DH |
851 | if (vma->vm_start < addr) |
852 | continue; | |
853 | if (vma->vm_start > addr) | |
854 | return NULL; | |
855 | if (vma->vm_end == end) { | |
615d6e87 | 856 | vmacache_update(addr, vma); |
8feae131 DH |
857 | return vma; |
858 | } | |
859 | } | |
860 | ||
861 | return NULL; | |
1da177e4 LT |
862 | } |
863 | ||
864 | /* | |
865 | * determine whether a mapping should be permitted and, if so, what sort of | |
866 | * mapping we're capable of supporting | |
867 | */ | |
868 | static int validate_mmap_request(struct file *file, | |
869 | unsigned long addr, | |
870 | unsigned long len, | |
871 | unsigned long prot, | |
872 | unsigned long flags, | |
873 | unsigned long pgoff, | |
874 | unsigned long *_capabilities) | |
875 | { | |
8feae131 | 876 | unsigned long capabilities, rlen; |
1da177e4 LT |
877 | int ret; |
878 | ||
879 | /* do the simple checks first */ | |
22cc877b | 880 | if (flags & MAP_FIXED) |
1da177e4 | 881 | return -EINVAL; |
1da177e4 LT |
882 | |
883 | if ((flags & MAP_TYPE) != MAP_PRIVATE && | |
884 | (flags & MAP_TYPE) != MAP_SHARED) | |
885 | return -EINVAL; | |
886 | ||
f81cff0d | 887 | if (!len) |
1da177e4 LT |
888 | return -EINVAL; |
889 | ||
f81cff0d | 890 | /* Careful about overflows.. */ |
8feae131 DH |
891 | rlen = PAGE_ALIGN(len); |
892 | if (!rlen || rlen > TASK_SIZE) | |
f81cff0d MF |
893 | return -ENOMEM; |
894 | ||
1da177e4 | 895 | /* offset overflow? */ |
8feae131 | 896 | if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff) |
f81cff0d | 897 | return -EOVERFLOW; |
1da177e4 LT |
898 | |
899 | if (file) { | |
1da177e4 | 900 | /* files must support mmap */ |
72c2d531 | 901 | if (!file->f_op->mmap) |
1da177e4 LT |
902 | return -ENODEV; |
903 | ||
904 | /* work out if what we've got could possibly be shared | |
905 | * - we support chardevs that provide their own "memory" | |
906 | * - we support files/blockdevs that are memory backed | |
907 | */ | |
b4caecd4 CH |
908 | if (file->f_op->mmap_capabilities) { |
909 | capabilities = file->f_op->mmap_capabilities(file); | |
910 | } else { | |
1da177e4 LT |
911 | /* no explicit capabilities set, so assume some |
912 | * defaults */ | |
496ad9aa | 913 | switch (file_inode(file)->i_mode & S_IFMT) { |
1da177e4 LT |
914 | case S_IFREG: |
915 | case S_IFBLK: | |
b4caecd4 | 916 | capabilities = NOMMU_MAP_COPY; |
1da177e4 LT |
917 | break; |
918 | ||
919 | case S_IFCHR: | |
920 | capabilities = | |
b4caecd4 CH |
921 | NOMMU_MAP_DIRECT | |
922 | NOMMU_MAP_READ | | |
923 | NOMMU_MAP_WRITE; | |
1da177e4 LT |
924 | break; |
925 | ||
926 | default: | |
927 | return -EINVAL; | |
928 | } | |
929 | } | |
930 | ||
931 | /* eliminate any capabilities that we can't support on this | |
932 | * device */ | |
933 | if (!file->f_op->get_unmapped_area) | |
b4caecd4 | 934 | capabilities &= ~NOMMU_MAP_DIRECT; |
6e242a1c | 935 | if (!(file->f_mode & FMODE_CAN_READ)) |
b4caecd4 | 936 | capabilities &= ~NOMMU_MAP_COPY; |
1da177e4 | 937 | |
28d7a6ae GY |
938 | /* The file shall have been opened with read permission. */ |
939 | if (!(file->f_mode & FMODE_READ)) | |
940 | return -EACCES; | |
941 | ||
1da177e4 LT |
942 | if (flags & MAP_SHARED) { |
943 | /* do checks for writing, appending and locking */ | |
944 | if ((prot & PROT_WRITE) && | |
945 | !(file->f_mode & FMODE_WRITE)) | |
946 | return -EACCES; | |
947 | ||
496ad9aa | 948 | if (IS_APPEND(file_inode(file)) && |
1da177e4 LT |
949 | (file->f_mode & FMODE_WRITE)) |
950 | return -EACCES; | |
951 | ||
d7a06983 | 952 | if (locks_verify_locked(file)) |
1da177e4 LT |
953 | return -EAGAIN; |
954 | ||
b4caecd4 | 955 | if (!(capabilities & NOMMU_MAP_DIRECT)) |
1da177e4 LT |
956 | return -ENODEV; |
957 | ||
1da177e4 | 958 | /* we mustn't privatise shared mappings */ |
b4caecd4 | 959 | capabilities &= ~NOMMU_MAP_COPY; |
ac714904 | 960 | } else { |
1da177e4 LT |
961 | /* we're going to read the file into private memory we |
962 | * allocate */ | |
b4caecd4 | 963 | if (!(capabilities & NOMMU_MAP_COPY)) |
1da177e4 LT |
964 | return -ENODEV; |
965 | ||
966 | /* we don't permit a private writable mapping to be | |
967 | * shared with the backing device */ | |
968 | if (prot & PROT_WRITE) | |
b4caecd4 | 969 | capabilities &= ~NOMMU_MAP_DIRECT; |
1da177e4 LT |
970 | } |
971 | ||
b4caecd4 CH |
972 | if (capabilities & NOMMU_MAP_DIRECT) { |
973 | if (((prot & PROT_READ) && !(capabilities & NOMMU_MAP_READ)) || | |
974 | ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) || | |
975 | ((prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC)) | |
3c7b2045 | 976 | ) { |
b4caecd4 | 977 | capabilities &= ~NOMMU_MAP_DIRECT; |
3c7b2045 | 978 | if (flags & MAP_SHARED) { |
22cc877b | 979 | pr_warn("MAP_SHARED not completely supported on !MMU\n"); |
3c7b2045 BS |
980 | return -EINVAL; |
981 | } | |
982 | } | |
983 | } | |
984 | ||
1da177e4 LT |
985 | /* handle executable mappings and implied executable |
986 | * mappings */ | |
90f8572b | 987 | if (path_noexec(&file->f_path)) { |
1da177e4 LT |
988 | if (prot & PROT_EXEC) |
989 | return -EPERM; | |
ac714904 | 990 | } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) { |
1da177e4 LT |
991 | /* handle implication of PROT_EXEC by PROT_READ */ |
992 | if (current->personality & READ_IMPLIES_EXEC) { | |
b4caecd4 | 993 | if (capabilities & NOMMU_MAP_EXEC) |
1da177e4 LT |
994 | prot |= PROT_EXEC; |
995 | } | |
ac714904 | 996 | } else if ((prot & PROT_READ) && |
1da177e4 | 997 | (prot & PROT_EXEC) && |
b4caecd4 | 998 | !(capabilities & NOMMU_MAP_EXEC) |
1da177e4 LT |
999 | ) { |
1000 | /* backing file is not executable, try to copy */ | |
b4caecd4 | 1001 | capabilities &= ~NOMMU_MAP_DIRECT; |
1da177e4 | 1002 | } |
ac714904 | 1003 | } else { |
1da177e4 LT |
1004 | /* anonymous mappings are always memory backed and can be |
1005 | * privately mapped | |
1006 | */ | |
b4caecd4 | 1007 | capabilities = NOMMU_MAP_COPY; |
1da177e4 LT |
1008 | |
1009 | /* handle PROT_EXEC implication by PROT_READ */ | |
1010 | if ((prot & PROT_READ) && | |
1011 | (current->personality & READ_IMPLIES_EXEC)) | |
1012 | prot |= PROT_EXEC; | |
1013 | } | |
1014 | ||
1015 | /* allow the security API to have its say */ | |
e5467859 | 1016 | ret = security_mmap_addr(addr); |
1da177e4 LT |
1017 | if (ret < 0) |
1018 | return ret; | |
1019 | ||
1020 | /* looks okay */ | |
1021 | *_capabilities = capabilities; | |
1022 | return 0; | |
1023 | } | |
1024 | ||
1025 | /* | |
1026 | * we've determined that we can make the mapping, now translate what we | |
1027 | * now know into VMA flags | |
1028 | */ | |
1029 | static unsigned long determine_vm_flags(struct file *file, | |
1030 | unsigned long prot, | |
1031 | unsigned long flags, | |
1032 | unsigned long capabilities) | |
1033 | { | |
1034 | unsigned long vm_flags; | |
1035 | ||
e6bfb709 | 1036 | vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags); |
1da177e4 LT |
1037 | /* vm_flags |= mm->def_flags; */ |
1038 | ||
b4caecd4 | 1039 | if (!(capabilities & NOMMU_MAP_DIRECT)) { |
1da177e4 | 1040 | /* attempt to share read-only copies of mapped file chunks */ |
3c7b2045 | 1041 | vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; |
1da177e4 LT |
1042 | if (file && !(prot & PROT_WRITE)) |
1043 | vm_flags |= VM_MAYSHARE; | |
3c7b2045 | 1044 | } else { |
1da177e4 LT |
1045 | /* overlay a shareable mapping on the backing device or inode |
1046 | * if possible - used for chardevs, ramfs/tmpfs/shmfs and | |
1047 | * romfs/cramfs */ | |
b4caecd4 | 1048 | vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS); |
1da177e4 | 1049 | if (flags & MAP_SHARED) |
3c7b2045 | 1050 | vm_flags |= VM_SHARED; |
1da177e4 LT |
1051 | } |
1052 | ||
1053 | /* refuse to let anyone share private mappings with this process if | |
1054 | * it's being traced - otherwise breakpoints set in it may interfere | |
1055 | * with another untraced process | |
1056 | */ | |
a288eecc | 1057 | if ((flags & MAP_PRIVATE) && current->ptrace) |
1da177e4 LT |
1058 | vm_flags &= ~VM_MAYSHARE; |
1059 | ||
1060 | return vm_flags; | |
1061 | } | |
1062 | ||
1063 | /* | |
8feae131 DH |
1064 | * set up a shared mapping on a file (the driver or filesystem provides and |
1065 | * pins the storage) | |
1da177e4 | 1066 | */ |
8feae131 | 1067 | static int do_mmap_shared_file(struct vm_area_struct *vma) |
1da177e4 LT |
1068 | { |
1069 | int ret; | |
1070 | ||
f74ac015 | 1071 | ret = call_mmap(vma->vm_file, vma); |
dd8632a1 PM |
1072 | if (ret == 0) { |
1073 | vma->vm_region->vm_top = vma->vm_region->vm_end; | |
645d83c5 | 1074 | return 0; |
dd8632a1 | 1075 | } |
1da177e4 LT |
1076 | if (ret != -ENOSYS) |
1077 | return ret; | |
1078 | ||
3fa30460 DH |
1079 | /* getting -ENOSYS indicates that direct mmap isn't possible (as |
1080 | * opposed to tried but failed) so we can only give a suitable error as | |
1081 | * it's not possible to make a private copy if MAP_SHARED was given */ | |
1da177e4 LT |
1082 | return -ENODEV; |
1083 | } | |
1084 | ||
1085 | /* | |
1086 | * set up a private mapping or an anonymous shared mapping | |
1087 | */ | |
8feae131 DH |
1088 | static int do_mmap_private(struct vm_area_struct *vma, |
1089 | struct vm_region *region, | |
645d83c5 DH |
1090 | unsigned long len, |
1091 | unsigned long capabilities) | |
1da177e4 | 1092 | { |
dbc8358c | 1093 | unsigned long total, point; |
1da177e4 | 1094 | void *base; |
8feae131 | 1095 | int ret, order; |
1da177e4 LT |
1096 | |
1097 | /* invoke the file's mapping function so that it can keep track of | |
1098 | * shared mappings on devices or memory | |
1099 | * - VM_MAYSHARE will be set if it may attempt to share | |
1100 | */ | |
b4caecd4 | 1101 | if (capabilities & NOMMU_MAP_DIRECT) { |
f74ac015 | 1102 | ret = call_mmap(vma->vm_file, vma); |
dd8632a1 | 1103 | if (ret == 0) { |
1da177e4 | 1104 | /* shouldn't return success if we're not sharing */ |
dd8632a1 PM |
1105 | BUG_ON(!(vma->vm_flags & VM_MAYSHARE)); |
1106 | vma->vm_region->vm_top = vma->vm_region->vm_end; | |
645d83c5 | 1107 | return 0; |
1da177e4 | 1108 | } |
dd8632a1 PM |
1109 | if (ret != -ENOSYS) |
1110 | return ret; | |
1da177e4 LT |
1111 | |
1112 | /* getting an ENOSYS error indicates that direct mmap isn't | |
1113 | * possible (as opposed to tried but failed) so we'll try to | |
1114 | * make a private copy of the data and map that instead */ | |
1115 | } | |
1116 | ||
8feae131 | 1117 | |
1da177e4 LT |
1118 | /* allocate some memory to hold the mapping |
1119 | * - note that this may not return a page-aligned address if the object | |
1120 | * we're allocating is smaller than a page | |
1121 | */ | |
f67d9b15 | 1122 | order = get_order(len); |
8feae131 | 1123 | total = 1 << order; |
f67d9b15 | 1124 | point = len >> PAGE_SHIFT; |
dd8632a1 | 1125 | |
dbc8358c | 1126 | /* we don't want to allocate a power-of-2 sized page set */ |
22cc877b | 1127 | if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) |
dbc8358c | 1128 | total = point; |
8feae131 | 1129 | |
da616534 | 1130 | base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL); |
dbc8358c JK |
1131 | if (!base) |
1132 | goto enomem; | |
1133 | ||
1134 | atomic_long_add(total, &mmap_pages_allocated); | |
1da177e4 | 1135 | |
8feae131 DH |
1136 | region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY; |
1137 | region->vm_start = (unsigned long) base; | |
f67d9b15 | 1138 | region->vm_end = region->vm_start + len; |
dd8632a1 | 1139 | region->vm_top = region->vm_start + (total << PAGE_SHIFT); |
8feae131 DH |
1140 | |
1141 | vma->vm_start = region->vm_start; | |
1142 | vma->vm_end = region->vm_start + len; | |
1da177e4 LT |
1143 | |
1144 | if (vma->vm_file) { | |
1145 | /* read the contents of a file into the copy */ | |
1da177e4 LT |
1146 | loff_t fpos; |
1147 | ||
1148 | fpos = vma->vm_pgoff; | |
1149 | fpos <<= PAGE_SHIFT; | |
1150 | ||
b4bf802a | 1151 | ret = kernel_read(vma->vm_file, base, len, &fpos); |
1da177e4 LT |
1152 | if (ret < 0) |
1153 | goto error_free; | |
1154 | ||
1155 | /* clear the last little bit */ | |
f67d9b15 BL |
1156 | if (ret < len) |
1157 | memset(base + ret, 0, len - ret); | |
1da177e4 | 1158 | |
bfd40eaf KS |
1159 | } else { |
1160 | vma_set_anonymous(vma); | |
1da177e4 LT |
1161 | } |
1162 | ||
1163 | return 0; | |
1164 | ||
1165 | error_free: | |
7223bb4a | 1166 | free_page_series(region->vm_start, region->vm_top); |
8feae131 DH |
1167 | region->vm_start = vma->vm_start = 0; |
1168 | region->vm_end = vma->vm_end = 0; | |
dd8632a1 | 1169 | region->vm_top = 0; |
1da177e4 LT |
1170 | return ret; |
1171 | ||
1172 | enomem: | |
b1de0d13 | 1173 | pr_err("Allocation of length %lu from process %d (%s) failed\n", |
05ae6fa3 | 1174 | len, current->pid, current->comm); |
9af744d7 | 1175 | show_free_areas(0, NULL); |
1da177e4 LT |
1176 | return -ENOMEM; |
1177 | } | |
1178 | ||
1179 | /* | |
1180 | * handle mapping creation for uClinux | |
1181 | */ | |
1fcfd8db ON |
1182 | unsigned long do_mmap(struct file *file, |
1183 | unsigned long addr, | |
1184 | unsigned long len, | |
1185 | unsigned long prot, | |
1186 | unsigned long flags, | |
1187 | vm_flags_t vm_flags, | |
1188 | unsigned long pgoff, | |
897ab3e0 MR |
1189 | unsigned long *populate, |
1190 | struct list_head *uf) | |
1da177e4 | 1191 | { |
8feae131 DH |
1192 | struct vm_area_struct *vma; |
1193 | struct vm_region *region; | |
1da177e4 | 1194 | struct rb_node *rb; |
1fcfd8db | 1195 | unsigned long capabilities, result; |
1da177e4 LT |
1196 | int ret; |
1197 | ||
41badc15 | 1198 | *populate = 0; |
bebeb3d6 | 1199 | |
1da177e4 LT |
1200 | /* decide whether we should attempt the mapping, and if so what sort of |
1201 | * mapping */ | |
1202 | ret = validate_mmap_request(file, addr, len, prot, flags, pgoff, | |
1203 | &capabilities); | |
22cc877b | 1204 | if (ret < 0) |
1da177e4 LT |
1205 | return ret; |
1206 | ||
06aab5a3 DH |
1207 | /* we ignore the address hint */ |
1208 | addr = 0; | |
f67d9b15 | 1209 | len = PAGE_ALIGN(len); |
06aab5a3 | 1210 | |
1da177e4 LT |
1211 | /* we've determined that we can make the mapping, now translate what we |
1212 | * now know into VMA flags */ | |
1fcfd8db | 1213 | vm_flags |= determine_vm_flags(file, prot, flags, capabilities); |
1da177e4 | 1214 | |
8feae131 DH |
1215 | /* we're going to need to record the mapping */ |
1216 | region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL); | |
1217 | if (!region) | |
1218 | goto error_getting_region; | |
1219 | ||
490fc053 | 1220 | vma = vm_area_alloc(current->mm); |
8feae131 DH |
1221 | if (!vma) |
1222 | goto error_getting_vma; | |
1da177e4 | 1223 | |
1e2ae599 | 1224 | region->vm_usage = 1; |
8feae131 DH |
1225 | region->vm_flags = vm_flags; |
1226 | region->vm_pgoff = pgoff; | |
1227 | ||
8feae131 DH |
1228 | vma->vm_flags = vm_flags; |
1229 | vma->vm_pgoff = pgoff; | |
1da177e4 | 1230 | |
8feae131 | 1231 | if (file) { |
cb0942b8 AV |
1232 | region->vm_file = get_file(file); |
1233 | vma->vm_file = get_file(file); | |
8feae131 DH |
1234 | } |
1235 | ||
1236 | down_write(&nommu_region_sem); | |
1237 | ||
1238 | /* if we want to share, we need to check for regions created by other | |
1da177e4 | 1239 | * mmap() calls that overlap with our proposed mapping |
8feae131 | 1240 | * - we can only share with a superset match on most regular files |
1da177e4 LT |
1241 | * - shared mappings on character devices and memory backed files are |
1242 | * permitted to overlap inexactly as far as we are concerned for in | |
1243 | * these cases, sharing is handled in the driver or filesystem rather | |
1244 | * than here | |
1245 | */ | |
1246 | if (vm_flags & VM_MAYSHARE) { | |
8feae131 DH |
1247 | struct vm_region *pregion; |
1248 | unsigned long pglen, rpglen, pgend, rpgend, start; | |
1da177e4 | 1249 | |
8feae131 DH |
1250 | pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
1251 | pgend = pgoff + pglen; | |
165b2392 | 1252 | |
8feae131 DH |
1253 | for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) { |
1254 | pregion = rb_entry(rb, struct vm_region, vm_rb); | |
1da177e4 | 1255 | |
8feae131 | 1256 | if (!(pregion->vm_flags & VM_MAYSHARE)) |
1da177e4 LT |
1257 | continue; |
1258 | ||
1259 | /* search for overlapping mappings on the same file */ | |
496ad9aa AV |
1260 | if (file_inode(pregion->vm_file) != |
1261 | file_inode(file)) | |
1da177e4 LT |
1262 | continue; |
1263 | ||
8feae131 | 1264 | if (pregion->vm_pgoff >= pgend) |
1da177e4 LT |
1265 | continue; |
1266 | ||
8feae131 DH |
1267 | rpglen = pregion->vm_end - pregion->vm_start; |
1268 | rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
1269 | rpgend = pregion->vm_pgoff + rpglen; | |
1270 | if (pgoff >= rpgend) | |
1da177e4 LT |
1271 | continue; |
1272 | ||
8feae131 DH |
1273 | /* handle inexactly overlapping matches between |
1274 | * mappings */ | |
1275 | if ((pregion->vm_pgoff != pgoff || rpglen != pglen) && | |
1276 | !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) { | |
1277 | /* new mapping is not a subset of the region */ | |
b4caecd4 | 1278 | if (!(capabilities & NOMMU_MAP_DIRECT)) |
1da177e4 LT |
1279 | goto sharing_violation; |
1280 | continue; | |
1281 | } | |
1282 | ||
8feae131 | 1283 | /* we've found a region we can share */ |
1e2ae599 | 1284 | pregion->vm_usage++; |
8feae131 DH |
1285 | vma->vm_region = pregion; |
1286 | start = pregion->vm_start; | |
1287 | start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT; | |
1288 | vma->vm_start = start; | |
1289 | vma->vm_end = start + len; | |
1290 | ||
22cc877b | 1291 | if (pregion->vm_flags & VM_MAPPED_COPY) |
8feae131 | 1292 | vma->vm_flags |= VM_MAPPED_COPY; |
22cc877b | 1293 | else { |
8feae131 DH |
1294 | ret = do_mmap_shared_file(vma); |
1295 | if (ret < 0) { | |
1296 | vma->vm_region = NULL; | |
1297 | vma->vm_start = 0; | |
1298 | vma->vm_end = 0; | |
1e2ae599 | 1299 | pregion->vm_usage--; |
8feae131 DH |
1300 | pregion = NULL; |
1301 | goto error_just_free; | |
1302 | } | |
1303 | } | |
1304 | fput(region->vm_file); | |
1305 | kmem_cache_free(vm_region_jar, region); | |
1306 | region = pregion; | |
1307 | result = start; | |
1308 | goto share; | |
1da177e4 LT |
1309 | } |
1310 | ||
1da177e4 LT |
1311 | /* obtain the address at which to make a shared mapping |
1312 | * - this is the hook for quasi-memory character devices to | |
1313 | * tell us the location of a shared mapping | |
1314 | */ | |
b4caecd4 | 1315 | if (capabilities & NOMMU_MAP_DIRECT) { |
1da177e4 LT |
1316 | addr = file->f_op->get_unmapped_area(file, addr, len, |
1317 | pgoff, flags); | |
bb005a59 | 1318 | if (IS_ERR_VALUE(addr)) { |
1da177e4 | 1319 | ret = addr; |
bb005a59 | 1320 | if (ret != -ENOSYS) |
8feae131 | 1321 | goto error_just_free; |
1da177e4 LT |
1322 | |
1323 | /* the driver refused to tell us where to site | |
1324 | * the mapping so we'll have to attempt to copy | |
1325 | * it */ | |
bb005a59 | 1326 | ret = -ENODEV; |
b4caecd4 | 1327 | if (!(capabilities & NOMMU_MAP_COPY)) |
8feae131 | 1328 | goto error_just_free; |
1da177e4 | 1329 | |
b4caecd4 | 1330 | capabilities &= ~NOMMU_MAP_DIRECT; |
8feae131 DH |
1331 | } else { |
1332 | vma->vm_start = region->vm_start = addr; | |
1333 | vma->vm_end = region->vm_end = addr + len; | |
1da177e4 LT |
1334 | } |
1335 | } | |
1336 | } | |
1337 | ||
8feae131 | 1338 | vma->vm_region = region; |
1da177e4 | 1339 | |
645d83c5 | 1340 | /* set up the mapping |
b4caecd4 | 1341 | * - the region is filled in if NOMMU_MAP_DIRECT is still set |
645d83c5 | 1342 | */ |
1da177e4 | 1343 | if (file && vma->vm_flags & VM_SHARED) |
8feae131 | 1344 | ret = do_mmap_shared_file(vma); |
1da177e4 | 1345 | else |
645d83c5 | 1346 | ret = do_mmap_private(vma, region, len, capabilities); |
1da177e4 | 1347 | if (ret < 0) |
645d83c5 DH |
1348 | goto error_just_free; |
1349 | add_nommu_region(region); | |
8feae131 | 1350 | |
ea637639 JZ |
1351 | /* clear anonymous mappings that don't ask for uninitialized data */ |
1352 | if (!vma->vm_file && !(flags & MAP_UNINITIALIZED)) | |
1353 | memset((void *)region->vm_start, 0, | |
1354 | region->vm_end - region->vm_start); | |
1355 | ||
1da177e4 | 1356 | /* okay... we have a mapping; now we have to register it */ |
8feae131 | 1357 | result = vma->vm_start; |
1da177e4 | 1358 | |
1da177e4 LT |
1359 | current->mm->total_vm += len >> PAGE_SHIFT; |
1360 | ||
8feae131 DH |
1361 | share: |
1362 | add_vma_to_mm(current->mm, vma); | |
1da177e4 | 1363 | |
cfe79c00 MF |
1364 | /* we flush the region from the icache only when the first executable |
1365 | * mapping of it is made */ | |
1366 | if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) { | |
1367 | flush_icache_range(region->vm_start, region->vm_end); | |
1368 | region->vm_icache_flushed = true; | |
1369 | } | |
1da177e4 | 1370 | |
cfe79c00 | 1371 | up_write(&nommu_region_sem); |
1da177e4 | 1372 | |
8feae131 | 1373 | return result; |
1da177e4 | 1374 | |
8feae131 DH |
1375 | error_just_free: |
1376 | up_write(&nommu_region_sem); | |
1377 | error: | |
89a86402 DH |
1378 | if (region->vm_file) |
1379 | fput(region->vm_file); | |
8feae131 | 1380 | kmem_cache_free(vm_region_jar, region); |
89a86402 DH |
1381 | if (vma->vm_file) |
1382 | fput(vma->vm_file); | |
3928d4f5 | 1383 | vm_area_free(vma); |
8feae131 DH |
1384 | return ret; |
1385 | ||
1386 | sharing_violation: | |
1387 | up_write(&nommu_region_sem); | |
22cc877b | 1388 | pr_warn("Attempt to share mismatched mappings\n"); |
8feae131 DH |
1389 | ret = -EINVAL; |
1390 | goto error; | |
1da177e4 | 1391 | |
8feae131 DH |
1392 | error_getting_vma: |
1393 | kmem_cache_free(vm_region_jar, region); | |
22cc877b LR |
1394 | pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n", |
1395 | len, current->pid); | |
9af744d7 | 1396 | show_free_areas(0, NULL); |
1da177e4 LT |
1397 | return -ENOMEM; |
1398 | ||
8feae131 | 1399 | error_getting_region: |
22cc877b LR |
1400 | pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n", |
1401 | len, current->pid); | |
9af744d7 | 1402 | show_free_areas(0, NULL); |
1da177e4 LT |
1403 | return -ENOMEM; |
1404 | } | |
6be5ceb0 | 1405 | |
a90f590a DB |
1406 | unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len, |
1407 | unsigned long prot, unsigned long flags, | |
1408 | unsigned long fd, unsigned long pgoff) | |
66f0dc48 HD |
1409 | { |
1410 | struct file *file = NULL; | |
1411 | unsigned long retval = -EBADF; | |
1412 | ||
120a795d | 1413 | audit_mmap_fd(fd, flags); |
66f0dc48 HD |
1414 | if (!(flags & MAP_ANONYMOUS)) { |
1415 | file = fget(fd); | |
1416 | if (!file) | |
1417 | goto out; | |
1418 | } | |
1419 | ||
1420 | flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE); | |
1421 | ||
ad1ed293 | 1422 | retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); |
66f0dc48 HD |
1423 | |
1424 | if (file) | |
1425 | fput(file); | |
1426 | out: | |
1427 | return retval; | |
1428 | } | |
1429 | ||
a90f590a DB |
1430 | SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, |
1431 | unsigned long, prot, unsigned long, flags, | |
1432 | unsigned long, fd, unsigned long, pgoff) | |
1433 | { | |
1434 | return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff); | |
1435 | } | |
1436 | ||
a4679373 CH |
1437 | #ifdef __ARCH_WANT_SYS_OLD_MMAP |
1438 | struct mmap_arg_struct { | |
1439 | unsigned long addr; | |
1440 | unsigned long len; | |
1441 | unsigned long prot; | |
1442 | unsigned long flags; | |
1443 | unsigned long fd; | |
1444 | unsigned long offset; | |
1445 | }; | |
1446 | ||
1447 | SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) | |
1448 | { | |
1449 | struct mmap_arg_struct a; | |
1450 | ||
1451 | if (copy_from_user(&a, arg, sizeof(a))) | |
1452 | return -EFAULT; | |
1824cb75 | 1453 | if (offset_in_page(a.offset)) |
a4679373 CH |
1454 | return -EINVAL; |
1455 | ||
a90f590a DB |
1456 | return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, |
1457 | a.offset >> PAGE_SHIFT); | |
a4679373 CH |
1458 | } |
1459 | #endif /* __ARCH_WANT_SYS_OLD_MMAP */ | |
1460 | ||
1da177e4 | 1461 | /* |
8feae131 DH |
1462 | * split a vma into two pieces at address 'addr', a new vma is allocated either |
1463 | * for the first part or the tail. | |
1da177e4 | 1464 | */ |
8feae131 DH |
1465 | int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, |
1466 | unsigned long addr, int new_below) | |
1da177e4 | 1467 | { |
8feae131 DH |
1468 | struct vm_area_struct *new; |
1469 | struct vm_region *region; | |
1470 | unsigned long npages; | |
1da177e4 | 1471 | |
779c1023 DH |
1472 | /* we're only permitted to split anonymous regions (these should have |
1473 | * only a single usage on the region) */ | |
1474 | if (vma->vm_file) | |
8feae131 | 1475 | return -ENOMEM; |
1da177e4 | 1476 | |
8feae131 DH |
1477 | if (mm->map_count >= sysctl_max_map_count) |
1478 | return -ENOMEM; | |
1da177e4 | 1479 | |
8feae131 DH |
1480 | region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL); |
1481 | if (!region) | |
1482 | return -ENOMEM; | |
1da177e4 | 1483 | |
3928d4f5 | 1484 | new = vm_area_dup(vma); |
8feae131 DH |
1485 | if (!new) { |
1486 | kmem_cache_free(vm_region_jar, region); | |
1487 | return -ENOMEM; | |
1488 | } | |
1489 | ||
1490 | /* most fields are the same, copy all, and then fixup */ | |
8feae131 DH |
1491 | *region = *vma->vm_region; |
1492 | new->vm_region = region; | |
1493 | ||
1494 | npages = (addr - vma->vm_start) >> PAGE_SHIFT; | |
1495 | ||
1496 | if (new_below) { | |
dd8632a1 | 1497 | region->vm_top = region->vm_end = new->vm_end = addr; |
8feae131 DH |
1498 | } else { |
1499 | region->vm_start = new->vm_start = addr; | |
1500 | region->vm_pgoff = new->vm_pgoff += npages; | |
1da177e4 | 1501 | } |
8feae131 DH |
1502 | |
1503 | if (new->vm_ops && new->vm_ops->open) | |
1504 | new->vm_ops->open(new); | |
1505 | ||
1506 | delete_vma_from_mm(vma); | |
1507 | down_write(&nommu_region_sem); | |
1508 | delete_nommu_region(vma->vm_region); | |
1509 | if (new_below) { | |
1510 | vma->vm_region->vm_start = vma->vm_start = addr; | |
1511 | vma->vm_region->vm_pgoff = vma->vm_pgoff += npages; | |
1512 | } else { | |
1513 | vma->vm_region->vm_end = vma->vm_end = addr; | |
dd8632a1 | 1514 | vma->vm_region->vm_top = addr; |
8feae131 DH |
1515 | } |
1516 | add_nommu_region(vma->vm_region); | |
1517 | add_nommu_region(new->vm_region); | |
1518 | up_write(&nommu_region_sem); | |
1519 | add_vma_to_mm(mm, vma); | |
1520 | add_vma_to_mm(mm, new); | |
1521 | return 0; | |
1da177e4 LT |
1522 | } |
1523 | ||
3034097a | 1524 | /* |
8feae131 DH |
1525 | * shrink a VMA by removing the specified chunk from either the beginning or |
1526 | * the end | |
3034097a | 1527 | */ |
8feae131 DH |
1528 | static int shrink_vma(struct mm_struct *mm, |
1529 | struct vm_area_struct *vma, | |
1530 | unsigned long from, unsigned long to) | |
1da177e4 | 1531 | { |
8feae131 | 1532 | struct vm_region *region; |
1da177e4 | 1533 | |
8feae131 DH |
1534 | /* adjust the VMA's pointers, which may reposition it in the MM's tree |
1535 | * and list */ | |
1536 | delete_vma_from_mm(vma); | |
1537 | if (from > vma->vm_start) | |
1538 | vma->vm_end = from; | |
1539 | else | |
1540 | vma->vm_start = to; | |
1541 | add_vma_to_mm(mm, vma); | |
1da177e4 | 1542 | |
8feae131 DH |
1543 | /* cut the backing region down to size */ |
1544 | region = vma->vm_region; | |
1e2ae599 | 1545 | BUG_ON(region->vm_usage != 1); |
8feae131 DH |
1546 | |
1547 | down_write(&nommu_region_sem); | |
1548 | delete_nommu_region(region); | |
dd8632a1 PM |
1549 | if (from > region->vm_start) { |
1550 | to = region->vm_top; | |
1551 | region->vm_top = region->vm_end = from; | |
1552 | } else { | |
8feae131 | 1553 | region->vm_start = to; |
dd8632a1 | 1554 | } |
8feae131 DH |
1555 | add_nommu_region(region); |
1556 | up_write(&nommu_region_sem); | |
1557 | ||
1558 | free_page_series(from, to); | |
1559 | return 0; | |
1560 | } | |
1da177e4 | 1561 | |
8feae131 DH |
1562 | /* |
1563 | * release a mapping | |
1564 | * - under NOMMU conditions the chunk to be unmapped must be backed by a single | |
1565 | * VMA, though it need not cover the whole VMA | |
1566 | */ | |
897ab3e0 | 1567 | int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf) |
8feae131 DH |
1568 | { |
1569 | struct vm_area_struct *vma; | |
f67d9b15 | 1570 | unsigned long end; |
8feae131 | 1571 | int ret; |
1da177e4 | 1572 | |
f67d9b15 | 1573 | len = PAGE_ALIGN(len); |
8feae131 DH |
1574 | if (len == 0) |
1575 | return -EINVAL; | |
365e9c87 | 1576 | |
f67d9b15 BL |
1577 | end = start + len; |
1578 | ||
8feae131 DH |
1579 | /* find the first potentially overlapping VMA */ |
1580 | vma = find_vma(mm, start); | |
1581 | if (!vma) { | |
ac714904 | 1582 | static int limit; |
33e5d769 | 1583 | if (limit < 5) { |
22cc877b LR |
1584 | pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n", |
1585 | current->pid, current->comm, | |
1586 | start, start + len - 1); | |
33e5d769 DH |
1587 | limit++; |
1588 | } | |
8feae131 DH |
1589 | return -EINVAL; |
1590 | } | |
1da177e4 | 1591 | |
8feae131 DH |
1592 | /* we're allowed to split an anonymous VMA but not a file-backed one */ |
1593 | if (vma->vm_file) { | |
1594 | do { | |
22cc877b | 1595 | if (start > vma->vm_start) |
8feae131 | 1596 | return -EINVAL; |
8feae131 DH |
1597 | if (end == vma->vm_end) |
1598 | goto erase_whole_vma; | |
d75a310c NK |
1599 | vma = vma->vm_next; |
1600 | } while (vma); | |
8feae131 DH |
1601 | return -EINVAL; |
1602 | } else { | |
1603 | /* the chunk must be a subset of the VMA found */ | |
1604 | if (start == vma->vm_start && end == vma->vm_end) | |
1605 | goto erase_whole_vma; | |
22cc877b | 1606 | if (start < vma->vm_start || end > vma->vm_end) |
8feae131 | 1607 | return -EINVAL; |
1824cb75 | 1608 | if (offset_in_page(start)) |
8feae131 | 1609 | return -EINVAL; |
1824cb75 | 1610 | if (end != vma->vm_end && offset_in_page(end)) |
8feae131 | 1611 | return -EINVAL; |
8feae131 DH |
1612 | if (start != vma->vm_start && end != vma->vm_end) { |
1613 | ret = split_vma(mm, vma, start, 1); | |
22cc877b | 1614 | if (ret < 0) |
8feae131 | 1615 | return ret; |
8feae131 DH |
1616 | } |
1617 | return shrink_vma(mm, vma, start, end); | |
1618 | } | |
1da177e4 | 1619 | |
8feae131 DH |
1620 | erase_whole_vma: |
1621 | delete_vma_from_mm(vma); | |
1622 | delete_vma(mm, vma); | |
1da177e4 LT |
1623 | return 0; |
1624 | } | |
b5073173 | 1625 | EXPORT_SYMBOL(do_munmap); |
1da177e4 | 1626 | |
bfce281c | 1627 | int vm_munmap(unsigned long addr, size_t len) |
3034097a | 1628 | { |
bfce281c | 1629 | struct mm_struct *mm = current->mm; |
3034097a | 1630 | int ret; |
3034097a DH |
1631 | |
1632 | down_write(&mm->mmap_sem); | |
897ab3e0 | 1633 | ret = do_munmap(mm, addr, len, NULL); |
3034097a DH |
1634 | up_write(&mm->mmap_sem); |
1635 | return ret; | |
1636 | } | |
a46ef99d LT |
1637 | EXPORT_SYMBOL(vm_munmap); |
1638 | ||
1639 | SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) | |
1640 | { | |
bfce281c | 1641 | return vm_munmap(addr, len); |
a46ef99d | 1642 | } |
3034097a DH |
1643 | |
1644 | /* | |
8feae131 | 1645 | * release all the mappings made in a process's VM space |
3034097a | 1646 | */ |
8feae131 | 1647 | void exit_mmap(struct mm_struct *mm) |
1da177e4 | 1648 | { |
8feae131 | 1649 | struct vm_area_struct *vma; |
1da177e4 | 1650 | |
8feae131 DH |
1651 | if (!mm) |
1652 | return; | |
1da177e4 | 1653 | |
8feae131 | 1654 | mm->total_vm = 0; |
1da177e4 | 1655 | |
8feae131 DH |
1656 | while ((vma = mm->mmap)) { |
1657 | mm->mmap = vma->vm_next; | |
1658 | delete_vma_from_mm(vma); | |
1659 | delete_vma(mm, vma); | |
04c34961 | 1660 | cond_resched(); |
1da177e4 LT |
1661 | } |
1662 | } | |
1663 | ||
5d22fc25 | 1664 | int vm_brk(unsigned long addr, unsigned long len) |
1da177e4 LT |
1665 | { |
1666 | return -ENOMEM; | |
1667 | } | |
1668 | ||
1669 | /* | |
6fa5f80b DH |
1670 | * expand (or shrink) an existing mapping, potentially moving it at the same |
1671 | * time (controlled by the MREMAP_MAYMOVE flag and available VM space) | |
1da177e4 | 1672 | * |
6fa5f80b | 1673 | * under NOMMU conditions, we only permit changing a mapping's size, and only |
8feae131 DH |
1674 | * as long as it stays within the region allocated by do_mmap_private() and the |
1675 | * block is not shareable | |
1da177e4 | 1676 | * |
6fa5f80b | 1677 | * MREMAP_FIXED is not supported under NOMMU conditions |
1da177e4 | 1678 | */ |
4b377bab | 1679 | static unsigned long do_mremap(unsigned long addr, |
1da177e4 LT |
1680 | unsigned long old_len, unsigned long new_len, |
1681 | unsigned long flags, unsigned long new_addr) | |
1682 | { | |
6fa5f80b | 1683 | struct vm_area_struct *vma; |
1da177e4 LT |
1684 | |
1685 | /* insanity checks first */ | |
f67d9b15 BL |
1686 | old_len = PAGE_ALIGN(old_len); |
1687 | new_len = PAGE_ALIGN(new_len); | |
8feae131 | 1688 | if (old_len == 0 || new_len == 0) |
1da177e4 LT |
1689 | return (unsigned long) -EINVAL; |
1690 | ||
1824cb75 | 1691 | if (offset_in_page(addr)) |
8feae131 DH |
1692 | return -EINVAL; |
1693 | ||
1da177e4 LT |
1694 | if (flags & MREMAP_FIXED && new_addr != addr) |
1695 | return (unsigned long) -EINVAL; | |
1696 | ||
8feae131 | 1697 | vma = find_vma_exact(current->mm, addr, old_len); |
6fa5f80b DH |
1698 | if (!vma) |
1699 | return (unsigned long) -EINVAL; | |
1da177e4 | 1700 | |
6fa5f80b | 1701 | if (vma->vm_end != vma->vm_start + old_len) |
1da177e4 LT |
1702 | return (unsigned long) -EFAULT; |
1703 | ||
6fa5f80b | 1704 | if (vma->vm_flags & VM_MAYSHARE) |
1da177e4 LT |
1705 | return (unsigned long) -EPERM; |
1706 | ||
8feae131 | 1707 | if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start) |
1da177e4 LT |
1708 | return (unsigned long) -ENOMEM; |
1709 | ||
1710 | /* all checks complete - do it */ | |
6fa5f80b | 1711 | vma->vm_end = vma->vm_start + new_len; |
6fa5f80b DH |
1712 | return vma->vm_start; |
1713 | } | |
1714 | ||
6a6160a7 HC |
1715 | SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, |
1716 | unsigned long, new_len, unsigned long, flags, | |
1717 | unsigned long, new_addr) | |
6fa5f80b DH |
1718 | { |
1719 | unsigned long ret; | |
1720 | ||
1721 | down_write(¤t->mm->mmap_sem); | |
1722 | ret = do_mremap(addr, old_len, new_len, flags, new_addr); | |
1723 | up_write(¤t->mm->mmap_sem); | |
1724 | return ret; | |
1da177e4 LT |
1725 | } |
1726 | ||
df06b37f KB |
1727 | struct page *follow_page(struct vm_area_struct *vma, unsigned long address, |
1728 | unsigned int foll_flags) | |
1da177e4 LT |
1729 | { |
1730 | return NULL; | |
1731 | } | |
1732 | ||
8f3b1327 BL |
1733 | int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, |
1734 | unsigned long pfn, unsigned long size, pgprot_t prot) | |
1da177e4 | 1735 | { |
8f3b1327 BL |
1736 | if (addr != (pfn << PAGE_SHIFT)) |
1737 | return -EINVAL; | |
1738 | ||
314e51b9 | 1739 | vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; |
66aa2b4b | 1740 | return 0; |
1da177e4 | 1741 | } |
22c4af40 | 1742 | EXPORT_SYMBOL(remap_pfn_range); |
1da177e4 | 1743 | |
3c0b9de6 LT |
1744 | int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) |
1745 | { | |
1746 | unsigned long pfn = start >> PAGE_SHIFT; | |
1747 | unsigned long vm_len = vma->vm_end - vma->vm_start; | |
1748 | ||
1749 | pfn += vma->vm_pgoff; | |
1750 | return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); | |
1751 | } | |
1752 | EXPORT_SYMBOL(vm_iomap_memory); | |
1753 | ||
f905bc44 PM |
1754 | int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, |
1755 | unsigned long pgoff) | |
1756 | { | |
1757 | unsigned int size = vma->vm_end - vma->vm_start; | |
1758 | ||
1759 | if (!(vma->vm_flags & VM_USERMAP)) | |
1760 | return -EINVAL; | |
1761 | ||
1762 | vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT)); | |
1763 | vma->vm_end = vma->vm_start + size; | |
1764 | ||
1765 | return 0; | |
1766 | } | |
1767 | EXPORT_SYMBOL(remap_vmalloc_range); | |
1768 | ||
1da177e4 LT |
1769 | unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr, |
1770 | unsigned long len, unsigned long pgoff, unsigned long flags) | |
1771 | { | |
1772 | return -ENOMEM; | |
1773 | } | |
1774 | ||
2bcd6454 | 1775 | vm_fault_t filemap_fault(struct vm_fault *vmf) |
b0e15190 DH |
1776 | { |
1777 | BUG(); | |
d0217ac0 | 1778 | return 0; |
b0e15190 | 1779 | } |
b5073173 | 1780 | EXPORT_SYMBOL(filemap_fault); |
0ec76a11 | 1781 | |
82b0f8c3 | 1782 | void filemap_map_pages(struct vm_fault *vmf, |
bae473a4 | 1783 | pgoff_t start_pgoff, pgoff_t end_pgoff) |
f1820361 KS |
1784 | { |
1785 | BUG(); | |
1786 | } | |
1787 | EXPORT_SYMBOL(filemap_map_pages); | |
1788 | ||
84d77d3f | 1789 | int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, |
442486ec | 1790 | unsigned long addr, void *buf, int len, unsigned int gup_flags) |
0ec76a11 | 1791 | { |
0ec76a11 | 1792 | struct vm_area_struct *vma; |
442486ec | 1793 | int write = gup_flags & FOLL_WRITE; |
0ec76a11 DH |
1794 | |
1795 | down_read(&mm->mmap_sem); | |
1796 | ||
1797 | /* the access must start within one of the target process's mappings */ | |
0159b141 DH |
1798 | vma = find_vma(mm, addr); |
1799 | if (vma) { | |
0ec76a11 DH |
1800 | /* don't overrun this mapping */ |
1801 | if (addr + len >= vma->vm_end) | |
1802 | len = vma->vm_end - addr; | |
1803 | ||
1804 | /* only read or write mappings where it is permitted */ | |
d00c7b99 | 1805 | if (write && vma->vm_flags & VM_MAYWRITE) |
7959722b JZ |
1806 | copy_to_user_page(vma, NULL, addr, |
1807 | (void *) addr, buf, len); | |
d00c7b99 | 1808 | else if (!write && vma->vm_flags & VM_MAYREAD) |
7959722b JZ |
1809 | copy_from_user_page(vma, NULL, addr, |
1810 | buf, (void *) addr, len); | |
0ec76a11 DH |
1811 | else |
1812 | len = 0; | |
1813 | } else { | |
1814 | len = 0; | |
1815 | } | |
1816 | ||
1817 | up_read(&mm->mmap_sem); | |
f55f199b MF |
1818 | |
1819 | return len; | |
1820 | } | |
1821 | ||
1822 | /** | |
b7701a5f | 1823 | * access_remote_vm - access another process' address space |
f55f199b MF |
1824 | * @mm: the mm_struct of the target address space |
1825 | * @addr: start address to access | |
1826 | * @buf: source or destination buffer | |
1827 | * @len: number of bytes to transfer | |
6347e8d5 | 1828 | * @gup_flags: flags modifying lookup behaviour |
f55f199b MF |
1829 | * |
1830 | * The caller must hold a reference on @mm. | |
1831 | */ | |
1832 | int access_remote_vm(struct mm_struct *mm, unsigned long addr, | |
6347e8d5 | 1833 | void *buf, int len, unsigned int gup_flags) |
f55f199b | 1834 | { |
6347e8d5 | 1835 | return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags); |
f55f199b MF |
1836 | } |
1837 | ||
1838 | /* | |
1839 | * Access another process' address space. | |
1840 | * - source/target buffer must be kernel space | |
1841 | */ | |
f307ab6d LS |
1842 | int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, |
1843 | unsigned int gup_flags) | |
f55f199b MF |
1844 | { |
1845 | struct mm_struct *mm; | |
1846 | ||
1847 | if (addr + len < addr) | |
1848 | return 0; | |
1849 | ||
1850 | mm = get_task_mm(tsk); | |
1851 | if (!mm) | |
1852 | return 0; | |
1853 | ||
f307ab6d | 1854 | len = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags); |
f55f199b | 1855 | |
0ec76a11 DH |
1856 | mmput(mm); |
1857 | return len; | |
1858 | } | |
fcd35857 | 1859 | EXPORT_SYMBOL_GPL(access_process_vm); |
7e660872 DH |
1860 | |
1861 | /** | |
1862 | * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode | |
1863 | * @inode: The inode to check | |
1864 | * @size: The current filesize of the inode | |
1865 | * @newsize: The proposed filesize of the inode | |
1866 | * | |
1867 | * Check the shared mappings on an inode on behalf of a shrinking truncate to | |
1868 | * make sure that that any outstanding VMAs aren't broken and then shrink the | |
1869 | * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't | |
1870 | * automatically grant mappings that are too large. | |
1871 | */ | |
1872 | int nommu_shrink_inode_mappings(struct inode *inode, size_t size, | |
1873 | size_t newsize) | |
1874 | { | |
1875 | struct vm_area_struct *vma; | |
7e660872 DH |
1876 | struct vm_region *region; |
1877 | pgoff_t low, high; | |
1878 | size_t r_size, r_top; | |
1879 | ||
1880 | low = newsize >> PAGE_SHIFT; | |
1881 | high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
1882 | ||
1883 | down_write(&nommu_region_sem); | |
1acf2e04 | 1884 | i_mmap_lock_read(inode->i_mapping); |
7e660872 DH |
1885 | |
1886 | /* search for VMAs that fall within the dead zone */ | |
6b2dbba8 | 1887 | vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) { |
7e660872 DH |
1888 | /* found one - only interested if it's shared out of the page |
1889 | * cache */ | |
1890 | if (vma->vm_flags & VM_SHARED) { | |
1acf2e04 | 1891 | i_mmap_unlock_read(inode->i_mapping); |
7e660872 DH |
1892 | up_write(&nommu_region_sem); |
1893 | return -ETXTBSY; /* not quite true, but near enough */ | |
1894 | } | |
1895 | } | |
1896 | ||
1897 | /* reduce any regions that overlap the dead zone - if in existence, | |
1898 | * these will be pointed to by VMAs that don't overlap the dead zone | |
1899 | * | |
1900 | * we don't check for any regions that start beyond the EOF as there | |
1901 | * shouldn't be any | |
1902 | */ | |
1acf2e04 | 1903 | vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) { |
7e660872 DH |
1904 | if (!(vma->vm_flags & VM_SHARED)) |
1905 | continue; | |
1906 | ||
1907 | region = vma->vm_region; | |
1908 | r_size = region->vm_top - region->vm_start; | |
1909 | r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size; | |
1910 | ||
1911 | if (r_top > newsize) { | |
1912 | region->vm_top -= r_top - newsize; | |
1913 | if (region->vm_end > region->vm_top) | |
1914 | region->vm_end = region->vm_top; | |
1915 | } | |
1916 | } | |
1917 | ||
1acf2e04 | 1918 | i_mmap_unlock_read(inode->i_mapping); |
7e660872 DH |
1919 | up_write(&nommu_region_sem); |
1920 | return 0; | |
1921 | } | |
c9b1d098 AS |
1922 | |
1923 | /* | |
1924 | * Initialise sysctl_user_reserve_kbytes. | |
1925 | * | |
1926 | * This is intended to prevent a user from starting a single memory hogging | |
1927 | * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER | |
1928 | * mode. | |
1929 | * | |
1930 | * The default value is min(3% of free memory, 128MB) | |
1931 | * 128MB is enough to recover with sshd/login, bash, and top/kill. | |
1932 | */ | |
1933 | static int __meminit init_user_reserve(void) | |
1934 | { | |
1935 | unsigned long free_kbytes; | |
1936 | ||
c41f012a | 1937 | free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); |
c9b1d098 AS |
1938 | |
1939 | sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); | |
1940 | return 0; | |
1941 | } | |
a4bc6fc7 | 1942 | subsys_initcall(init_user_reserve); |
4eeab4f5 AS |
1943 | |
1944 | /* | |
1945 | * Initialise sysctl_admin_reserve_kbytes. | |
1946 | * | |
1947 | * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin | |
1948 | * to log in and kill a memory hogging process. | |
1949 | * | |
1950 | * Systems with more than 256MB will reserve 8MB, enough to recover | |
1951 | * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will | |
1952 | * only reserve 3% of free pages by default. | |
1953 | */ | |
1954 | static int __meminit init_admin_reserve(void) | |
1955 | { | |
1956 | unsigned long free_kbytes; | |
1957 | ||
c41f012a | 1958 | free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); |
4eeab4f5 AS |
1959 | |
1960 | sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); | |
1961 | return 0; | |
1962 | } | |
a4bc6fc7 | 1963 | subsys_initcall(init_admin_reserve); |