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