1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/export.h>
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>
25#include <linux/blkdev.h>
26#include <linux/backing-dev.h>
27#include <linux/mount.h>
28#include <linux/personality.h>
29#include <linux/security.h>
30#include <linux/syscalls.h>
31#include <linux/audit.h>
32#include <linux/sched/sysctl.h>
33
34#include <asm/uaccess.h>
35#include <asm/tlb.h>
36#include <asm/tlbflush.h>
37#include <asm/mmu_context.h>
38#include "internal.h"
39
40#if 0
41#define kenter(FMT, ...) \
42 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
43#define kleave(FMT, ...) \
44 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
45#define kdebug(FMT, ...) \
46 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
47#else
48#define kenter(FMT, ...) \
49 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
50#define kleave(FMT, ...) \
51 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
52#define kdebug(FMT, ...) \
53 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
54#endif
55
56void *high_memory;
57struct page *mem_map;
58unsigned long max_mapnr;
59unsigned long num_physpages;
60unsigned long highest_memmap_pfn;
61struct percpu_counter vm_committed_as;
62int sysctl_overcommit_memory = OVERCOMMIT_GUESS;
63int sysctl_overcommit_ratio = 50;
64unsigned long sysctl_overcommit_kbytes __read_mostly;
65int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
66int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
67unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17;
68unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13;
69int heap_stack_gap = 0;
70
71atomic_long_t mmap_pages_allocated;
72
73
74
75
76
77
78
79
80
81unsigned long vm_memory_committed(void)
82{
83 return percpu_counter_read_positive(&vm_committed_as);
84}
85
86EXPORT_SYMBOL_GPL(vm_memory_committed);
87
88EXPORT_SYMBOL(mem_map);
89EXPORT_SYMBOL(num_physpages);
90
91
92static struct kmem_cache *vm_region_jar;
93struct rb_root nommu_region_tree = RB_ROOT;
94DECLARE_RWSEM(nommu_region_sem);
95
96const struct vm_operations_struct generic_file_vm_ops = {
97};
98
99
100
101
102
103
104
105unsigned int kobjsize(const void *objp)
106{
107 struct page *page;
108
109
110
111
112
113 if (!objp || !virt_addr_valid(objp))
114 return 0;
115
116 page = virt_to_head_page(objp);
117
118
119
120
121
122 if (PageSlab(page))
123 return ksize(objp);
124
125
126
127
128
129
130
131 if (!PageCompound(page)) {
132 struct vm_area_struct *vma;
133
134 vma = find_vma(current->mm, (unsigned long)objp);
135 if (vma)
136 return vma->vm_end - vma->vm_start;
137 }
138
139
140
141
142
143 return PAGE_SIZE << compound_order(page);
144}
145
146long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
147 unsigned long start, unsigned long nr_pages,
148 unsigned int foll_flags, struct page **pages,
149 struct vm_area_struct **vmas, int *nonblocking)
150{
151 struct vm_area_struct *vma;
152 unsigned long vm_flags;
153 int i;
154
155
156
157
158 vm_flags = (foll_flags & FOLL_WRITE) ?
159 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
160 vm_flags &= (foll_flags & FOLL_FORCE) ?
161 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
162
163 for (i = 0; i < nr_pages; i++) {
164 vma = find_vma(mm, start);
165 if (!vma)
166 goto finish_or_fault;
167
168
169 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
170 !(vm_flags & vma->vm_flags))
171 goto finish_or_fault;
172
173 if (pages) {
174 pages[i] = virt_to_page(start);
175 if (pages[i])
176 page_cache_get(pages[i]);
177 }
178 if (vmas)
179 vmas[i] = vma;
180 start = (start + PAGE_SIZE) & PAGE_MASK;
181 }
182
183 return i;
184
185finish_or_fault:
186 return i ? : -EFAULT;
187}
188
189
190
191
192
193
194
195
196long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
197 unsigned long start, unsigned long nr_pages,
198 int write, int force, struct page **pages,
199 struct vm_area_struct **vmas)
200{
201 int flags = 0;
202
203 if (write)
204 flags |= FOLL_WRITE;
205 if (force)
206 flags |= FOLL_FORCE;
207
208 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
209 NULL);
210}
211EXPORT_SYMBOL(get_user_pages);
212
213long get_user_pages_locked(struct task_struct *tsk, struct mm_struct *mm,
214 unsigned long start, unsigned long nr_pages,
215 int write, int force, struct page **pages,
216 int *locked)
217{
218 return get_user_pages(tsk, mm, start, nr_pages, write, force,
219 pages, NULL);
220}
221EXPORT_SYMBOL(get_user_pages_locked);
222
223long __get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
224 unsigned long start, unsigned long nr_pages,
225 int write, int force, struct page **pages,
226 unsigned int gup_flags)
227{
228 long ret;
229 down_read(&mm->mmap_sem);
230 ret = get_user_pages(tsk, mm, start, nr_pages, write, force,
231 pages, NULL);
232 up_read(&mm->mmap_sem);
233 return ret;
234}
235EXPORT_SYMBOL(__get_user_pages_unlocked);
236
237long get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
238 unsigned long start, unsigned long nr_pages,
239 int write, int force, struct page **pages)
240{
241 return __get_user_pages_unlocked(tsk, mm, start, nr_pages, write,
242 force, pages, 0);
243}
244EXPORT_SYMBOL(get_user_pages_unlocked);
245
246
247
248
249
250
251
252
253
254
255
256int follow_pfn(struct vm_area_struct *vma, unsigned long address,
257 unsigned long *pfn)
258{
259 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
260 return -EINVAL;
261
262 *pfn = address >> PAGE_SHIFT;
263 return 0;
264}
265EXPORT_SYMBOL(follow_pfn);
266
267LIST_HEAD(vmap_area_list);
268
269void vfree(const void *addr)
270{
271 kfree(addr);
272}
273EXPORT_SYMBOL(vfree);
274
275void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
276{
277
278
279
280
281 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
282}
283EXPORT_SYMBOL(__vmalloc);
284
285void *__vmalloc_node_flags(unsigned long size, int node, gfp_t flags)
286{
287 return __vmalloc(size, flags, PAGE_KERNEL);
288}
289
290void *vmalloc_user(unsigned long size)
291{
292 void *ret;
293
294 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
295 PAGE_KERNEL);
296 if (ret) {
297 struct vm_area_struct *vma;
298
299 down_write(¤t->mm->mmap_sem);
300 vma = find_vma(current->mm, (unsigned long)ret);
301 if (vma)
302 vma->vm_flags |= VM_USERMAP;
303 up_write(¤t->mm->mmap_sem);
304 }
305
306 return ret;
307}
308EXPORT_SYMBOL(vmalloc_user);
309
310struct page *vmalloc_to_page(const void *addr)
311{
312 return virt_to_page(addr);
313}
314EXPORT_SYMBOL(vmalloc_to_page);
315
316unsigned long vmalloc_to_pfn(const void *addr)
317{
318 return page_to_pfn(virt_to_page(addr));
319}
320EXPORT_SYMBOL(vmalloc_to_pfn);
321
322long vread(char *buf, char *addr, unsigned long count)
323{
324 memcpy(buf, addr, count);
325 return count;
326}
327
328long vwrite(char *buf, char *addr, unsigned long count)
329{
330
331 if ((unsigned long) addr + count < count)
332 count = -(unsigned long) addr;
333
334 memcpy(addr, buf, count);
335 return(count);
336}
337
338
339
340
341
342
343
344
345
346
347
348
349void *vmalloc(unsigned long size)
350{
351 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
352}
353EXPORT_SYMBOL(vmalloc);
354
355
356
357
358
359
360
361
362
363
364
365
366
367void *vzalloc(unsigned long size)
368{
369 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
370 PAGE_KERNEL);
371}
372EXPORT_SYMBOL(vzalloc);
373
374
375
376
377
378
379
380
381
382
383
384
385void *vmalloc_node(unsigned long size, int node)
386{
387 return vmalloc(size);
388}
389EXPORT_SYMBOL(vmalloc_node);
390
391
392
393
394
395
396
397
398
399
400
401
402
403void *vzalloc_node(unsigned long size, int node)
404{
405 return vzalloc(size);
406}
407EXPORT_SYMBOL(vzalloc_node);
408
409#ifndef PAGE_KERNEL_EXEC
410# define PAGE_KERNEL_EXEC PAGE_KERNEL
411#endif
412
413
414
415
416
417
418
419
420
421
422
423
424
425void *vmalloc_exec(unsigned long size)
426{
427 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
428}
429
430
431
432
433
434
435
436
437void *vmalloc_32(unsigned long size)
438{
439 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
440}
441EXPORT_SYMBOL(vmalloc_32);
442
443
444
445
446
447
448
449
450
451
452
453void *vmalloc_32_user(unsigned long size)
454{
455
456
457
458
459 return vmalloc_user(size);
460}
461EXPORT_SYMBOL(vmalloc_32_user);
462
463void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
464{
465 BUG();
466 return NULL;
467}
468EXPORT_SYMBOL(vmap);
469
470void vunmap(const void *addr)
471{
472 BUG();
473}
474EXPORT_SYMBOL(vunmap);
475
476void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
477{
478 BUG();
479 return NULL;
480}
481EXPORT_SYMBOL(vm_map_ram);
482
483void vm_unmap_ram(const void *mem, unsigned int count)
484{
485 BUG();
486}
487EXPORT_SYMBOL(vm_unmap_ram);
488
489void vm_unmap_aliases(void)
490{
491}
492EXPORT_SYMBOL_GPL(vm_unmap_aliases);
493
494
495
496
497
498void __attribute__((weak)) vmalloc_sync_all(void)
499{
500}
501
502
503
504
505
506
507
508
509
510
511
512
513
514struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
515{
516 BUG();
517 return NULL;
518}
519EXPORT_SYMBOL_GPL(alloc_vm_area);
520
521void free_vm_area(struct vm_struct *area)
522{
523 BUG();
524}
525EXPORT_SYMBOL_GPL(free_vm_area);
526
527int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
528 struct page *page)
529{
530 return -EINVAL;
531}
532EXPORT_SYMBOL(vm_insert_page);
533
534
535
536
537
538
539
540
541SYSCALL_DEFINE1(brk, unsigned long, brk)
542{
543 struct mm_struct *mm = current->mm;
544
545 if (brk < mm->start_brk || brk > mm->context.end_brk)
546 return mm->brk;
547
548 if (mm->brk == brk)
549 return mm->brk;
550
551
552
553
554 if (brk <= mm->brk) {
555 mm->brk = brk;
556 return brk;
557 }
558
559
560
561
562 flush_icache_range(mm->brk, brk);
563 return mm->brk = brk;
564}
565
566
567
568
569void __init mmap_init(void)
570{
571 int ret;
572
573 ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
574 VM_BUG_ON(ret);
575 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
576}
577
578
579
580
581
582#ifdef CONFIG_DEBUG_NOMMU_REGIONS
583static noinline void validate_nommu_regions(void)
584{
585 struct vm_region *region, *last;
586 struct rb_node *p, *lastp;
587
588 lastp = rb_first(&nommu_region_tree);
589 if (!lastp)
590 return;
591
592 last = rb_entry(lastp, struct vm_region, vm_rb);
593 BUG_ON(unlikely(last->vm_end <= last->vm_start));
594 BUG_ON(unlikely(last->vm_top < last->vm_end));
595
596 while ((p = rb_next(lastp))) {
597 region = rb_entry(p, struct vm_region, vm_rb);
598 last = rb_entry(lastp, struct vm_region, vm_rb);
599
600 BUG_ON(unlikely(region->vm_end <= region->vm_start));
601 BUG_ON(unlikely(region->vm_top < region->vm_end));
602 BUG_ON(unlikely(region->vm_start < last->vm_top));
603
604 lastp = p;
605 }
606}
607#else
608static void validate_nommu_regions(void)
609{
610}
611#endif
612
613
614
615
616static void add_nommu_region(struct vm_region *region)
617{
618 struct vm_region *pregion;
619 struct rb_node **p, *parent;
620
621 validate_nommu_regions();
622
623 parent = NULL;
624 p = &nommu_region_tree.rb_node;
625 while (*p) {
626 parent = *p;
627 pregion = rb_entry(parent, struct vm_region, vm_rb);
628 if (region->vm_start < pregion->vm_start)
629 p = &(*p)->rb_left;
630 else if (region->vm_start > pregion->vm_start)
631 p = &(*p)->rb_right;
632 else if (pregion == region)
633 return;
634 else
635 BUG();
636 }
637
638 rb_link_node(®ion->vm_rb, parent, p);
639 rb_insert_color(®ion->vm_rb, &nommu_region_tree);
640
641 validate_nommu_regions();
642}
643
644
645
646
647static void delete_nommu_region(struct vm_region *region)
648{
649 BUG_ON(!nommu_region_tree.rb_node);
650
651 validate_nommu_regions();
652 rb_erase(®ion->vm_rb, &nommu_region_tree);
653 validate_nommu_regions();
654}
655
656
657
658
659static void free_page_series(unsigned long from, unsigned long to)
660{
661 for (; from < to; from += PAGE_SIZE) {
662 struct page *page = virt_to_page(from);
663
664 kdebug("- free %lx", from);
665 atomic_long_dec(&mmap_pages_allocated);
666 if (page_count(page) != 1)
667 kdebug("free page %p: refcount not one: %d",
668 page, page_count(page));
669 put_page(page);
670 }
671}
672
673
674
675
676
677
678
679static void __put_nommu_region(struct vm_region *region)
680 __releases(nommu_region_sem)
681{
682 kenter("%p{%d}", region, region->vm_usage);
683
684 BUG_ON(!nommu_region_tree.rb_node);
685
686 if (--region->vm_usage == 0) {
687 if (region->vm_top > region->vm_start)
688 delete_nommu_region(region);
689 up_write(&nommu_region_sem);
690
691 if (region->vm_file)
692 fput(region->vm_file);
693
694
695
696 if (region->vm_flags & VM_MAPPED_COPY) {
697 kdebug("free series");
698 free_page_series(region->vm_start, region->vm_top);
699 }
700 kmem_cache_free(vm_region_jar, region);
701 } else {
702 up_write(&nommu_region_sem);
703 }
704}
705
706
707
708
709static void put_nommu_region(struct vm_region *region)
710{
711 down_write(&nommu_region_sem);
712 __put_nommu_region(region);
713}
714
715
716
717
718static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
719{
720#ifdef CONFIG_MPU
721 struct mm_struct *mm = vma->vm_mm;
722 long start = vma->vm_start & PAGE_MASK;
723 while (start < vma->vm_end) {
724 protect_page(mm, start, flags);
725 start += PAGE_SIZE;
726 }
727 update_protections(mm);
728#endif
729}
730
731
732
733
734
735
736
737static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
738{
739 struct vm_area_struct *pvma, *prev;
740 struct address_space *mapping;
741 struct rb_node **p, *parent, *rb_prev;
742
743 kenter(",%p", vma);
744
745 BUG_ON(!vma->vm_region);
746
747 mm->map_count++;
748 vma->vm_mm = mm;
749
750 protect_vma(vma, vma->vm_flags);
751
752
753 if (vma->vm_file) {
754 mapping = vma->vm_file->f_mapping;
755
756 mutex_lock(&mapping->i_mmap_mutex);
757 flush_dcache_mmap_lock(mapping);
758 vma_interval_tree_insert(vma, &mapping->i_mmap);
759 flush_dcache_mmap_unlock(mapping);
760 mutex_unlock(&mapping->i_mmap_mutex);
761 }
762
763
764 parent = rb_prev = NULL;
765 p = &mm->mm_rb.rb_node;
766 while (*p) {
767 parent = *p;
768 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
769
770
771
772 if (vma->vm_start < pvma->vm_start)
773 p = &(*p)->rb_left;
774 else if (vma->vm_start > pvma->vm_start) {
775 rb_prev = parent;
776 p = &(*p)->rb_right;
777 } else if (vma->vm_end < pvma->vm_end)
778 p = &(*p)->rb_left;
779 else if (vma->vm_end > pvma->vm_end) {
780 rb_prev = parent;
781 p = &(*p)->rb_right;
782 } else if (vma < pvma)
783 p = &(*p)->rb_left;
784 else if (vma > pvma) {
785 rb_prev = parent;
786 p = &(*p)->rb_right;
787 } else
788 BUG();
789 }
790
791 rb_link_node(&vma->vm_rb, parent, p);
792 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
793
794
795 prev = NULL;
796 if (rb_prev)
797 prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
798
799 __vma_link_list(mm, vma, prev, parent);
800}
801
802
803
804
805static void delete_vma_from_mm(struct vm_area_struct *vma)
806{
807 struct address_space *mapping;
808 struct mm_struct *mm = vma->vm_mm;
809
810 kenter("%p", vma);
811
812 protect_vma(vma, 0);
813
814 mm->map_count--;
815 if (mm->mmap_cache == vma)
816 mm->mmap_cache = NULL;
817
818
819 if (vma->vm_file) {
820 mapping = vma->vm_file->f_mapping;
821
822 mutex_lock(&mapping->i_mmap_mutex);
823 flush_dcache_mmap_lock(mapping);
824 vma_interval_tree_remove(vma, &mapping->i_mmap);
825 flush_dcache_mmap_unlock(mapping);
826 mutex_unlock(&mapping->i_mmap_mutex);
827 }
828
829
830 rb_erase(&vma->vm_rb, &mm->mm_rb);
831
832 if (vma->vm_prev)
833 vma->vm_prev->vm_next = vma->vm_next;
834 else
835 mm->mmap = vma->vm_next;
836
837 if (vma->vm_next)
838 vma->vm_next->vm_prev = vma->vm_prev;
839}
840
841
842
843
844static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
845{
846 kenter("%p", vma);
847 if (vma->vm_ops && vma->vm_ops->close)
848 vma->vm_ops->close(vma);
849 if (vma->vm_file)
850 fput(vma->vm_file);
851 put_nommu_region(vma->vm_region);
852 kmem_cache_free(vm_area_cachep, vma);
853}
854
855
856
857
858
859struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
860{
861 struct vm_area_struct *vma;
862
863
864 vma = ACCESS_ONCE(mm->mmap_cache);
865 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
866 return vma;
867
868
869
870 for (vma = mm->mmap; vma; vma = vma->vm_next) {
871 if (vma->vm_start > addr)
872 return NULL;
873 if (vma->vm_end > addr) {
874 mm->mmap_cache = vma;
875 return vma;
876 }
877 }
878
879 return NULL;
880}
881EXPORT_SYMBOL(find_vma);
882
883
884
885
886
887struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
888{
889 return find_vma(mm, addr);
890}
891
892
893
894
895
896int expand_stack(struct vm_area_struct *vma, unsigned long address)
897{
898 return -ENOMEM;
899}
900
901
902
903
904
905static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
906 unsigned long addr,
907 unsigned long len)
908{
909 struct vm_area_struct *vma;
910 unsigned long end = addr + len;
911
912
913 vma = mm->mmap_cache;
914 if (vma && vma->vm_start == addr && vma->vm_end == end)
915 return vma;
916
917
918
919 for (vma = mm->mmap; vma; vma = vma->vm_next) {
920 if (vma->vm_start < addr)
921 continue;
922 if (vma->vm_start > addr)
923 return NULL;
924 if (vma->vm_end == end) {
925 mm->mmap_cache = vma;
926 return vma;
927 }
928 }
929
930 return NULL;
931}
932
933
934
935
936
937static int validate_mmap_request(struct file *file,
938 unsigned long addr,
939 unsigned long len,
940 unsigned long prot,
941 unsigned long flags,
942 unsigned long pgoff,
943 unsigned long *_capabilities)
944{
945 unsigned long capabilities, rlen;
946 int ret;
947
948
949 if (flags & MAP_FIXED) {
950 printk(KERN_DEBUG
951 "%d: Can't do fixed-address/overlay mmap of RAM\n",
952 current->pid);
953 return -EINVAL;
954 }
955
956 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
957 (flags & MAP_TYPE) != MAP_SHARED)
958 return -EINVAL;
959
960 if (!len)
961 return -EINVAL;
962
963
964 rlen = PAGE_ALIGN(len);
965 if (!rlen || rlen > TASK_SIZE)
966 return -ENOMEM;
967
968
969 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
970 return -EOVERFLOW;
971
972 if (file) {
973
974 struct address_space *mapping;
975
976
977 if (!file->f_op || !file->f_op->mmap)
978 return -ENODEV;
979
980
981
982
983
984 mapping = file->f_mapping;
985 if (!mapping)
986 mapping = file_inode(file)->i_mapping;
987
988 capabilities = 0;
989 if (mapping && mapping->backing_dev_info)
990 capabilities = mapping->backing_dev_info->capabilities;
991
992 if (!capabilities) {
993
994
995 switch (file_inode(file)->i_mode & S_IFMT) {
996 case S_IFREG:
997 case S_IFBLK:
998 capabilities = BDI_CAP_MAP_COPY;
999 break;
1000
1001 case S_IFCHR:
1002 capabilities =
1003 BDI_CAP_MAP_DIRECT |
1004 BDI_CAP_READ_MAP |
1005 BDI_CAP_WRITE_MAP;
1006 break;
1007
1008 default:
1009 return -EINVAL;
1010 }
1011 }
1012
1013
1014
1015 if (!file->f_op->get_unmapped_area)
1016 capabilities &= ~BDI_CAP_MAP_DIRECT;
1017 if (!file->f_op->read)
1018 capabilities &= ~BDI_CAP_MAP_COPY;
1019
1020
1021 if (!(file->f_mode & FMODE_READ))
1022 return -EACCES;
1023
1024 if (flags & MAP_SHARED) {
1025
1026 if ((prot & PROT_WRITE) &&
1027 !(file->f_mode & FMODE_WRITE))
1028 return -EACCES;
1029
1030 if (IS_APPEND(file_inode(file)) &&
1031 (file->f_mode & FMODE_WRITE))
1032 return -EACCES;
1033
1034 if (locks_verify_locked(file))
1035 return -EAGAIN;
1036
1037 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1038 return -ENODEV;
1039
1040
1041 capabilities &= ~BDI_CAP_MAP_COPY;
1042 }
1043 else {
1044
1045
1046 if (!(capabilities & BDI_CAP_MAP_COPY))
1047 return -ENODEV;
1048
1049
1050
1051 if (prot & PROT_WRITE)
1052 capabilities &= ~BDI_CAP_MAP_DIRECT;
1053 }
1054
1055 if (capabilities & BDI_CAP_MAP_DIRECT) {
1056 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
1057 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
1058 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
1059 ) {
1060 capabilities &= ~BDI_CAP_MAP_DIRECT;
1061 if (flags & MAP_SHARED) {
1062 printk(KERN_WARNING
1063 "MAP_SHARED not completely supported on !MMU\n");
1064 return -EINVAL;
1065 }
1066 }
1067 }
1068
1069
1070
1071 if (path_noexec(&file->f_path)) {
1072 if (prot & PROT_EXEC)
1073 return -EPERM;
1074 }
1075 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
1076
1077 if (current->personality & READ_IMPLIES_EXEC) {
1078 if (capabilities & BDI_CAP_EXEC_MAP)
1079 prot |= PROT_EXEC;
1080 }
1081 }
1082 else if ((prot & PROT_READ) &&
1083 (prot & PROT_EXEC) &&
1084 !(capabilities & BDI_CAP_EXEC_MAP)
1085 ) {
1086
1087 capabilities &= ~BDI_CAP_MAP_DIRECT;
1088 }
1089 }
1090 else {
1091
1092
1093
1094 capabilities = BDI_CAP_MAP_COPY;
1095
1096
1097 if ((prot & PROT_READ) &&
1098 (current->personality & READ_IMPLIES_EXEC))
1099 prot |= PROT_EXEC;
1100 }
1101
1102
1103 ret = security_mmap_addr(addr);
1104 if (ret < 0)
1105 return ret;
1106
1107
1108 *_capabilities = capabilities;
1109 return 0;
1110}
1111
1112
1113
1114
1115
1116static unsigned long determine_vm_flags(struct file *file,
1117 unsigned long prot,
1118 unsigned long flags,
1119 unsigned long capabilities)
1120{
1121 unsigned long vm_flags;
1122
1123 vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags);
1124
1125
1126 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1127
1128 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1129 if (file && !(prot & PROT_WRITE))
1130 vm_flags |= VM_MAYSHARE;
1131 } else {
1132
1133
1134
1135 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
1136 if (flags & MAP_SHARED)
1137 vm_flags |= VM_SHARED;
1138 }
1139
1140
1141
1142
1143
1144 if ((flags & MAP_PRIVATE) && current->ptrace)
1145 vm_flags &= ~VM_MAYSHARE;
1146
1147 return vm_flags;
1148}
1149
1150
1151
1152
1153
1154static int do_mmap_shared_file(struct vm_area_struct *vma)
1155{
1156 int ret;
1157
1158 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1159 if (ret == 0) {
1160 vma->vm_region->vm_top = vma->vm_region->vm_end;
1161 return 0;
1162 }
1163 if (ret != -ENOSYS)
1164 return ret;
1165
1166
1167
1168
1169 return -ENODEV;
1170}
1171
1172
1173
1174
1175static int do_mmap_private(struct vm_area_struct *vma,
1176 struct vm_region *region,
1177 unsigned long len,
1178 unsigned long capabilities)
1179{
1180 struct page *pages;
1181 unsigned long total, point, n;
1182 void *base;
1183 int ret, order;
1184
1185
1186
1187
1188
1189 if (capabilities & BDI_CAP_MAP_DIRECT) {
1190 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1191 if (ret == 0) {
1192
1193 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1194 vma->vm_region->vm_top = vma->vm_region->vm_end;
1195 return 0;
1196 }
1197 if (ret != -ENOSYS)
1198 return ret;
1199
1200
1201
1202
1203 }
1204
1205
1206
1207
1208
1209
1210 order = get_order(len);
1211 kdebug("alloc order %d for %lx", order, len);
1212
1213 pages = alloc_pages(GFP_KERNEL, order);
1214 if (!pages)
1215 goto enomem;
1216
1217 total = 1 << order;
1218 atomic_long_add(total, &mmap_pages_allocated);
1219
1220 point = len >> PAGE_SHIFT;
1221
1222
1223
1224 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1225 while (total > point) {
1226 order = ilog2(total - point);
1227 n = 1 << order;
1228 kdebug("shave %lu/%lu @%lu", n, total - point, total);
1229 atomic_long_sub(n, &mmap_pages_allocated);
1230 total -= n;
1231 set_page_refcounted(pages + total);
1232 __free_pages(pages + total, order);
1233 }
1234 }
1235
1236 for (point = 1; point < total; point++)
1237 set_page_refcounted(&pages[point]);
1238
1239 base = page_address(pages);
1240 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1241 region->vm_start = (unsigned long) base;
1242 region->vm_end = region->vm_start + len;
1243 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
1244
1245 vma->vm_start = region->vm_start;
1246 vma->vm_end = region->vm_start + len;
1247
1248 if (vma->vm_file) {
1249
1250 mm_segment_t old_fs;
1251 loff_t fpos;
1252
1253 fpos = vma->vm_pgoff;
1254 fpos <<= PAGE_SHIFT;
1255
1256 old_fs = get_fs();
1257 set_fs(KERNEL_DS);
1258 ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
1259 set_fs(old_fs);
1260
1261 if (ret < 0)
1262 goto error_free;
1263
1264
1265 if (ret < len)
1266 memset(base + ret, 0, len - ret);
1267
1268 }
1269
1270 return 0;
1271
1272error_free:
1273 free_page_series(region->vm_start, region->vm_top);
1274 region->vm_start = vma->vm_start = 0;
1275 region->vm_end = vma->vm_end = 0;
1276 region->vm_top = 0;
1277 return ret;
1278
1279enomem:
1280 printk("Allocation of length %lu from process %d (%s) failed\n",
1281 len, current->pid, current->comm);
1282 show_free_areas(0);
1283 return -ENOMEM;
1284}
1285
1286
1287
1288
1289unsigned long do_mmap(struct file *file,
1290 unsigned long addr,
1291 unsigned long len,
1292 unsigned long prot,
1293 unsigned long flags,
1294 vm_flags_t vm_flags,
1295 unsigned long pgoff,
1296 unsigned long *populate,
1297 struct list_head *uf_unused)
1298{
1299 struct vm_area_struct *vma;
1300 struct vm_region *region;
1301 struct rb_node *rb;
1302 unsigned long capabilities, result;
1303 int ret;
1304
1305 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1306
1307 *populate = 0;
1308
1309
1310
1311 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1312 &capabilities);
1313 if (ret < 0) {
1314 kleave(" = %d [val]", ret);
1315 return ret;
1316 }
1317
1318
1319 addr = 0;
1320 len = PAGE_ALIGN(len);
1321
1322
1323
1324 vm_flags |= determine_vm_flags(file, prot, flags, capabilities);
1325
1326
1327 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1328 if (!region)
1329 goto error_getting_region;
1330
1331 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1332 if (!vma)
1333 goto error_getting_vma;
1334
1335 region->vm_usage = 1;
1336 region->vm_flags = vm_flags;
1337 region->vm_pgoff = pgoff;
1338
1339 INIT_LIST_HEAD(&vma->anon_vma_chain);
1340 vma->vm_flags = vm_flags;
1341 vma->vm_pgoff = pgoff;
1342
1343 if (file) {
1344 region->vm_file = get_file(file);
1345 vma->vm_file = get_file(file);
1346 }
1347
1348 down_write(&nommu_region_sem);
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358 if (vm_flags & VM_MAYSHARE) {
1359 struct vm_region *pregion;
1360 unsigned long pglen, rpglen, pgend, rpgend, start;
1361
1362 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1363 pgend = pgoff + pglen;
1364
1365 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1366 pregion = rb_entry(rb, struct vm_region, vm_rb);
1367
1368 if (!(pregion->vm_flags & VM_MAYSHARE))
1369 continue;
1370
1371
1372 if (file_inode(pregion->vm_file) !=
1373 file_inode(file))
1374 continue;
1375
1376 if (pregion->vm_pgoff >= pgend)
1377 continue;
1378
1379 rpglen = pregion->vm_end - pregion->vm_start;
1380 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1381 rpgend = pregion->vm_pgoff + rpglen;
1382 if (pgoff >= rpgend)
1383 continue;
1384
1385
1386
1387 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1388 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1389
1390 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1391 goto sharing_violation;
1392 continue;
1393 }
1394
1395
1396 pregion->vm_usage++;
1397 vma->vm_region = pregion;
1398 start = pregion->vm_start;
1399 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1400 vma->vm_start = start;
1401 vma->vm_end = start + len;
1402
1403 if (pregion->vm_flags & VM_MAPPED_COPY) {
1404 kdebug("share copy");
1405 vma->vm_flags |= VM_MAPPED_COPY;
1406 } else {
1407 kdebug("share mmap");
1408 ret = do_mmap_shared_file(vma);
1409 if (ret < 0) {
1410 vma->vm_region = NULL;
1411 vma->vm_start = 0;
1412 vma->vm_end = 0;
1413 pregion->vm_usage--;
1414 pregion = NULL;
1415 goto error_just_free;
1416 }
1417 }
1418 fput(region->vm_file);
1419 kmem_cache_free(vm_region_jar, region);
1420 region = pregion;
1421 result = start;
1422 goto share;
1423 }
1424
1425
1426
1427
1428
1429 if (capabilities & BDI_CAP_MAP_DIRECT) {
1430 addr = file->f_op->get_unmapped_area(file, addr, len,
1431 pgoff, flags);
1432 if (IS_ERR_VALUE(addr)) {
1433 ret = addr;
1434 if (ret != -ENOSYS)
1435 goto error_just_free;
1436
1437
1438
1439
1440 ret = -ENODEV;
1441 if (!(capabilities & BDI_CAP_MAP_COPY))
1442 goto error_just_free;
1443
1444 capabilities &= ~BDI_CAP_MAP_DIRECT;
1445 } else {
1446 vma->vm_start = region->vm_start = addr;
1447 vma->vm_end = region->vm_end = addr + len;
1448 }
1449 }
1450 }
1451
1452 vma->vm_region = region;
1453
1454
1455
1456
1457 if (file && vma->vm_flags & VM_SHARED)
1458 ret = do_mmap_shared_file(vma);
1459 else
1460 ret = do_mmap_private(vma, region, len, capabilities);
1461 if (ret < 0)
1462 goto error_just_free;
1463 add_nommu_region(region);
1464
1465
1466 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1467 memset((void *)region->vm_start, 0,
1468 region->vm_end - region->vm_start);
1469
1470
1471 result = vma->vm_start;
1472
1473 current->mm->total_vm += len >> PAGE_SHIFT;
1474
1475share:
1476 add_vma_to_mm(current->mm, vma);
1477
1478
1479
1480 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1481 flush_icache_range(region->vm_start, region->vm_end);
1482 region->vm_icache_flushed = true;
1483 }
1484
1485 up_write(&nommu_region_sem);
1486
1487 kleave(" = %lx", result);
1488 return result;
1489
1490error_just_free:
1491 up_write(&nommu_region_sem);
1492error:
1493 if (region->vm_file)
1494 fput(region->vm_file);
1495 kmem_cache_free(vm_region_jar, region);
1496 if (vma->vm_file)
1497 fput(vma->vm_file);
1498 kmem_cache_free(vm_area_cachep, vma);
1499 kleave(" = %d", ret);
1500 return ret;
1501
1502sharing_violation:
1503 up_write(&nommu_region_sem);
1504 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1505 ret = -EINVAL;
1506 goto error;
1507
1508error_getting_vma:
1509 kmem_cache_free(vm_region_jar, region);
1510 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1511 " from process %d failed\n",
1512 len, current->pid);
1513 show_free_areas(0);
1514 return -ENOMEM;
1515
1516error_getting_region:
1517 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1518 " from process %d failed\n",
1519 len, current->pid);
1520 show_free_areas(0);
1521 return -ENOMEM;
1522}
1523
1524SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1525 unsigned long, prot, unsigned long, flags,
1526 unsigned long, fd, unsigned long, pgoff)
1527{
1528 struct file *file = NULL;
1529 unsigned long retval = -EBADF;
1530
1531 audit_mmap_fd(fd, flags);
1532 if (!(flags & MAP_ANONYMOUS)) {
1533 file = fget(fd);
1534 if (!file)
1535 goto out;
1536 }
1537
1538 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1539
1540 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1541
1542 if (file)
1543 fput(file);
1544out:
1545 return retval;
1546}
1547
1548#ifdef __ARCH_WANT_SYS_OLD_MMAP
1549struct mmap_arg_struct {
1550 unsigned long addr;
1551 unsigned long len;
1552 unsigned long prot;
1553 unsigned long flags;
1554 unsigned long fd;
1555 unsigned long offset;
1556};
1557
1558SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1559{
1560 struct mmap_arg_struct a;
1561
1562 if (copy_from_user(&a, arg, sizeof(a)))
1563 return -EFAULT;
1564 if (a.offset & ~PAGE_MASK)
1565 return -EINVAL;
1566
1567 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1568 a.offset >> PAGE_SHIFT);
1569}
1570#endif
1571
1572
1573
1574
1575
1576int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1577 unsigned long addr, int new_below)
1578{
1579 struct vm_area_struct *new;
1580 struct vm_region *region;
1581 unsigned long npages;
1582
1583 kenter("");
1584
1585
1586
1587 if (vma->vm_file)
1588 return -ENOMEM;
1589
1590 if (mm->map_count >= sysctl_max_map_count)
1591 return -ENOMEM;
1592
1593 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1594 if (!region)
1595 return -ENOMEM;
1596
1597 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1598 if (!new) {
1599 kmem_cache_free(vm_region_jar, region);
1600 return -ENOMEM;
1601 }
1602
1603
1604 *new = *vma;
1605 *region = *vma->vm_region;
1606 new->vm_region = region;
1607
1608 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1609
1610 if (new_below) {
1611 region->vm_top = region->vm_end = new->vm_end = addr;
1612 } else {
1613 region->vm_start = new->vm_start = addr;
1614 region->vm_pgoff = new->vm_pgoff += npages;
1615 }
1616
1617 if (new->vm_ops && new->vm_ops->open)
1618 new->vm_ops->open(new);
1619
1620 delete_vma_from_mm(vma);
1621 down_write(&nommu_region_sem);
1622 delete_nommu_region(vma->vm_region);
1623 if (new_below) {
1624 vma->vm_region->vm_start = vma->vm_start = addr;
1625 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1626 } else {
1627 vma->vm_region->vm_end = vma->vm_end = addr;
1628 vma->vm_region->vm_top = addr;
1629 }
1630 add_nommu_region(vma->vm_region);
1631 add_nommu_region(new->vm_region);
1632 up_write(&nommu_region_sem);
1633 add_vma_to_mm(mm, vma);
1634 add_vma_to_mm(mm, new);
1635 return 0;
1636}
1637
1638
1639
1640
1641
1642static int shrink_vma(struct mm_struct *mm,
1643 struct vm_area_struct *vma,
1644 unsigned long from, unsigned long to)
1645{
1646 struct vm_region *region;
1647
1648 kenter("");
1649
1650
1651
1652 delete_vma_from_mm(vma);
1653 if (from > vma->vm_start)
1654 vma->vm_end = from;
1655 else
1656 vma->vm_start = to;
1657 add_vma_to_mm(mm, vma);
1658
1659
1660 region = vma->vm_region;
1661 BUG_ON(region->vm_usage != 1);
1662
1663 down_write(&nommu_region_sem);
1664 delete_nommu_region(region);
1665 if (from > region->vm_start) {
1666 to = region->vm_top;
1667 region->vm_top = region->vm_end = from;
1668 } else {
1669 region->vm_start = to;
1670 }
1671 add_nommu_region(region);
1672 up_write(&nommu_region_sem);
1673
1674 free_page_series(from, to);
1675 return 0;
1676}
1677
1678
1679
1680
1681
1682
1683int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf)
1684{
1685 struct vm_area_struct *vma;
1686 unsigned long end;
1687 int ret;
1688
1689 kenter(",%lx,%zx", start, len);
1690
1691 len = PAGE_ALIGN(len);
1692 if (len == 0)
1693 return -EINVAL;
1694
1695 end = start + len;
1696
1697
1698 vma = find_vma(mm, start);
1699 if (!vma) {
1700 static int limit = 0;
1701 if (limit < 5) {
1702 printk(KERN_WARNING
1703 "munmap of memory not mmapped by process %d"
1704 " (%s): 0x%lx-0x%lx\n",
1705 current->pid, current->comm,
1706 start, start + len - 1);
1707 limit++;
1708 }
1709 return -EINVAL;
1710 }
1711
1712
1713 if (vma->vm_file) {
1714 do {
1715 if (start > vma->vm_start) {
1716 kleave(" = -EINVAL [miss]");
1717 return -EINVAL;
1718 }
1719 if (end == vma->vm_end)
1720 goto erase_whole_vma;
1721 vma = vma->vm_next;
1722 } while (vma);
1723 kleave(" = -EINVAL [split file]");
1724 return -EINVAL;
1725 } else {
1726
1727 if (start == vma->vm_start && end == vma->vm_end)
1728 goto erase_whole_vma;
1729 if (start < vma->vm_start || end > vma->vm_end) {
1730 kleave(" = -EINVAL [superset]");
1731 return -EINVAL;
1732 }
1733 if (start & ~PAGE_MASK) {
1734 kleave(" = -EINVAL [unaligned start]");
1735 return -EINVAL;
1736 }
1737 if (end != vma->vm_end && end & ~PAGE_MASK) {
1738 kleave(" = -EINVAL [unaligned split]");
1739 return -EINVAL;
1740 }
1741 if (start != vma->vm_start && end != vma->vm_end) {
1742 ret = split_vma(mm, vma, start, 1);
1743 if (ret < 0) {
1744 kleave(" = %d [split]", ret);
1745 return ret;
1746 }
1747 }
1748 return shrink_vma(mm, vma, start, end);
1749 }
1750
1751erase_whole_vma:
1752 delete_vma_from_mm(vma);
1753 delete_vma(mm, vma);
1754 kleave(" = 0");
1755 return 0;
1756}
1757EXPORT_SYMBOL(do_munmap);
1758
1759int vm_munmap(unsigned long addr, size_t len)
1760{
1761 struct mm_struct *mm = current->mm;
1762 int ret;
1763
1764 down_write(&mm->mmap_sem);
1765 ret = do_munmap(mm, addr, len, NULL);
1766 up_write(&mm->mmap_sem);
1767 return ret;
1768}
1769EXPORT_SYMBOL(vm_munmap);
1770
1771SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1772{
1773 return vm_munmap(addr, len);
1774}
1775
1776
1777
1778
1779void exit_mmap(struct mm_struct *mm)
1780{
1781 struct vm_area_struct *vma;
1782
1783 if (!mm)
1784 return;
1785
1786 kenter("");
1787
1788 mm->total_vm = 0;
1789
1790 while ((vma = mm->mmap)) {
1791 mm->mmap = vma->vm_next;
1792 delete_vma_from_mm(vma);
1793 delete_vma(mm, vma);
1794 cond_resched();
1795 }
1796
1797 kleave("");
1798}
1799
1800unsigned long vm_brk(unsigned long addr, unsigned long len)
1801{
1802 return -ENOMEM;
1803}
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815static unsigned long do_mremap(unsigned long addr,
1816 unsigned long old_len, unsigned long new_len,
1817 unsigned long flags, unsigned long new_addr)
1818{
1819 struct vm_area_struct *vma;
1820
1821
1822 old_len = PAGE_ALIGN(old_len);
1823 new_len = PAGE_ALIGN(new_len);
1824 if (old_len == 0 || new_len == 0)
1825 return (unsigned long) -EINVAL;
1826
1827 if (addr & ~PAGE_MASK)
1828 return -EINVAL;
1829
1830 if (flags & MREMAP_FIXED && new_addr != addr)
1831 return (unsigned long) -EINVAL;
1832
1833 vma = find_vma_exact(current->mm, addr, old_len);
1834 if (!vma)
1835 return (unsigned long) -EINVAL;
1836
1837 if (vma->vm_end != vma->vm_start + old_len)
1838 return (unsigned long) -EFAULT;
1839
1840 if (vma->vm_flags & VM_MAYSHARE)
1841 return (unsigned long) -EPERM;
1842
1843 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
1844 return (unsigned long) -ENOMEM;
1845
1846
1847 vma->vm_end = vma->vm_start + new_len;
1848 return vma->vm_start;
1849}
1850
1851SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1852 unsigned long, new_len, unsigned long, flags,
1853 unsigned long, new_addr)
1854{
1855 unsigned long ret;
1856
1857 down_write(¤t->mm->mmap_sem);
1858 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1859 up_write(¤t->mm->mmap_sem);
1860 return ret;
1861}
1862
1863struct page *follow_page_mask(struct vm_area_struct *vma,
1864 unsigned long address, unsigned int flags,
1865 unsigned int *page_mask)
1866{
1867 *page_mask = 0;
1868 return NULL;
1869}
1870
1871int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1872 unsigned long pfn, unsigned long size, pgprot_t prot)
1873{
1874 if (addr != (pfn << PAGE_SHIFT))
1875 return -EINVAL;
1876
1877 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1878 return 0;
1879}
1880EXPORT_SYMBOL(remap_pfn_range);
1881
1882int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1883{
1884 unsigned long pfn = start >> PAGE_SHIFT;
1885 unsigned long vm_len = vma->vm_end - vma->vm_start;
1886
1887 pfn += vma->vm_pgoff;
1888 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
1889}
1890EXPORT_SYMBOL(vm_iomap_memory);
1891
1892int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1893 unsigned long pgoff)
1894{
1895 unsigned int size = vma->vm_end - vma->vm_start;
1896
1897 if (!(vma->vm_flags & VM_USERMAP))
1898 return -EINVAL;
1899
1900 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1901 vma->vm_end = vma->vm_start + size;
1902
1903 return 0;
1904}
1905EXPORT_SYMBOL(remap_vmalloc_range);
1906
1907unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1908 unsigned long len, unsigned long pgoff, unsigned long flags)
1909{
1910 return -ENOMEM;
1911}
1912
1913void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
1914{
1915}
1916
1917void unmap_mapping_range(struct address_space *mapping,
1918 loff_t const holebegin, loff_t const holelen,
1919 int even_cows)
1920{
1921}
1922EXPORT_SYMBOL(unmap_mapping_range);
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
1941{
1942 unsigned long free, allowed, reserve;
1943
1944 vm_acct_memory(pages);
1945
1946
1947
1948
1949 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1950 return 0;
1951
1952 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1953 free = global_page_state(NR_FREE_PAGES);
1954 free += global_page_state(NR_FILE_PAGES);
1955
1956
1957
1958
1959
1960
1961
1962 free -= global_page_state(NR_SHMEM);
1963
1964 free += get_nr_swap_pages();
1965
1966
1967
1968
1969
1970
1971
1972 free += global_page_state(NR_SLAB_RECLAIMABLE);
1973
1974
1975
1976
1977 if (free <= totalreserve_pages)
1978 goto error;
1979 else
1980 free -= totalreserve_pages;
1981
1982
1983
1984
1985 if (!cap_sys_admin)
1986 free -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
1987
1988 if (free > pages)
1989 return 0;
1990
1991 goto error;
1992 }
1993
1994 allowed = vm_commit_limit();
1995
1996
1997
1998 if (!cap_sys_admin)
1999 allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
2000
2001
2002
2003
2004 if (mm) {
2005 reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
2006 allowed -= min(mm->total_vm / 32, reserve);
2007 }
2008
2009 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
2010 return 0;
2011
2012error:
2013 vm_unacct_memory(pages);
2014
2015 return -ENOMEM;
2016}
2017
2018int in_gate_area_no_mm(unsigned long addr)
2019{
2020 return 0;
2021}
2022
2023int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2024{
2025 BUG();
2026 return 0;
2027}
2028EXPORT_SYMBOL(filemap_fault);
2029
2030int generic_file_remap_pages(struct vm_area_struct *vma, unsigned long addr,
2031 unsigned long size, pgoff_t pgoff)
2032{
2033 BUG();
2034 return 0;
2035}
2036EXPORT_SYMBOL(generic_file_remap_pages);
2037
2038static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
2039 unsigned long addr, void *buf, int len, int write)
2040{
2041 struct vm_area_struct *vma;
2042
2043 down_read(&mm->mmap_sem);
2044
2045
2046 vma = find_vma(mm, addr);
2047 if (vma) {
2048
2049 if (addr + len >= vma->vm_end)
2050 len = vma->vm_end - addr;
2051
2052
2053 if (write && vma->vm_flags & VM_MAYWRITE)
2054 copy_to_user_page(vma, NULL, addr,
2055 (void *) addr, buf, len);
2056 else if (!write && vma->vm_flags & VM_MAYREAD)
2057 copy_from_user_page(vma, NULL, addr,
2058 buf, (void *) addr, len);
2059 else
2060 len = 0;
2061 } else {
2062 len = 0;
2063 }
2064
2065 up_read(&mm->mmap_sem);
2066
2067 return len;
2068}
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080int access_remote_vm(struct mm_struct *mm, unsigned long addr,
2081 void *buf, int len, int write)
2082{
2083 return __access_remote_vm(NULL, mm, addr, buf, len, write);
2084}
2085
2086
2087
2088
2089
2090int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
2091{
2092 struct mm_struct *mm;
2093
2094 if (addr + len < addr)
2095 return 0;
2096
2097 mm = get_task_mm(tsk);
2098 if (!mm)
2099 return 0;
2100
2101 len = __access_remote_vm(tsk, mm, addr, buf, len, write);
2102
2103 mmput(mm);
2104 return len;
2105}
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
2119 size_t newsize)
2120{
2121 struct vm_area_struct *vma;
2122 struct vm_region *region;
2123 pgoff_t low, high;
2124 size_t r_size, r_top;
2125
2126 low = newsize >> PAGE_SHIFT;
2127 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2128
2129 down_write(&nommu_region_sem);
2130 mutex_lock(&inode->i_mapping->i_mmap_mutex);
2131
2132
2133 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
2134
2135
2136 if (vma->vm_flags & VM_SHARED) {
2137 mutex_unlock(&inode->i_mapping->i_mmap_mutex);
2138 up_write(&nommu_region_sem);
2139 return -ETXTBSY;
2140 }
2141 }
2142
2143
2144
2145
2146
2147
2148
2149 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap,
2150 0, ULONG_MAX) {
2151 if (!(vma->vm_flags & VM_SHARED))
2152 continue;
2153
2154 region = vma->vm_region;
2155 r_size = region->vm_top - region->vm_start;
2156 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
2157
2158 if (r_top > newsize) {
2159 region->vm_top -= r_top - newsize;
2160 if (region->vm_end > region->vm_top)
2161 region->vm_end = region->vm_top;
2162 }
2163 }
2164
2165 mutex_unlock(&inode->i_mapping->i_mmap_mutex);
2166 up_write(&nommu_region_sem);
2167 return 0;
2168}
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180static int __meminit init_user_reserve(void)
2181{
2182 unsigned long free_kbytes;
2183
2184 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2185
2186 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
2187 return 0;
2188}
2189module_init(init_user_reserve)
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201static int __meminit init_admin_reserve(void)
2202{
2203 unsigned long free_kbytes;
2204
2205 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2206
2207 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
2208 return 0;
2209}
2210module_init(init_admin_reserve)
2211