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