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