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