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