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