Lynx framebuffers multidomain implementation.
[linux/elbrus.git] / mm / memory.c
blobbc773ef9255f0f6efe73175986c2b95cec8ebb69
1 /*
2 * linux/mm/memory.c
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 */
7 /*
8 * demand-loading started 01.12.91 - seems it is high on the list of
9 * things wanted, and it should be easy to implement. - Linus
13 * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
14 * pages started 02.12.91, seems to work. - Linus.
16 * Tested sharing by executing about 30 /bin/sh: under the old kernel it
17 * would have taken more than the 6M I have free, but it worked well as
18 * far as I could see.
20 * Also corrected some "invalidate()"s - I wasn't doing enough of them.
24 * Real VM (paging to/from disk) started 18.12.91. Much more work and
25 * thought has to go into this. Oh, well..
26 * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why.
27 * Found it. Everything seems to work now.
28 * 20.12.91 - Ok, making the swap-device changeable like the root.
32 * 05.04.94 - Multi-page memory management added for v1.1.
33 * Idea by Alex Bligh (alex@cconcepts.co.uk)
35 * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG
36 * (Gerhard.Wichert@pdb.siemens.de)
38 * Aug/Sep 2004 Changed to four level page tables (Andi Kleen)
41 #include <linux/kernel_stat.h>
42 #include <linux/mm.h>
43 #include <linux/hugetlb.h>
44 #include <linux/mman.h>
45 #include <linux/swap.h>
46 #include <linux/highmem.h>
47 #include <linux/pagemap.h>
48 #include <linux/ksm.h>
49 #include <linux/rmap.h>
50 #include <linux/export.h>
51 #include <linux/delayacct.h>
52 #include <linux/delay.h>
53 #include <linux/init.h>
54 #include <linux/writeback.h>
55 #include <linux/memcontrol.h>
56 #include <linux/mmu_notifier.h>
57 #include <linux/kallsyms.h>
58 #include <linux/swapops.h>
59 #include <linux/elf.h>
60 #include <linux/gfp.h>
61 #include <linux/migrate.h>
62 #include <linux/string.h>
63 #include <linux/dma-debug.h>
65 #include <asm/io.h>
66 #include <asm/pgalloc.h>
67 #include <asm/uaccess.h>
68 #include <asm/tlb.h>
69 #include <asm/tlbflush.h>
70 #include <asm/pgtable.h>
72 #include "internal.h"
74 #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
75 #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
76 #endif
78 #ifndef CONFIG_NEED_MULTIPLE_NODES
79 /* use the per-pgdat data instead for discontigmem - mbligh */
80 unsigned long max_mapnr;
81 struct page *mem_map;
83 EXPORT_SYMBOL(max_mapnr);
84 EXPORT_SYMBOL(mem_map);
85 #endif
88 * A number of key systems in x86 including ioremap() rely on the assumption
89 * that high_memory defines the upper bound on direct map memory, then end
90 * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
91 * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
92 * and ZONE_HIGHMEM.
94 void * high_memory;
96 EXPORT_SYMBOL(high_memory);
99 * Randomize the address space (stacks, mmaps, brk, etc.).
101 * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
102 * as ancient (libc5 based) binaries can segfault. )
104 int randomize_va_space __read_mostly =
105 #ifdef CONFIG_COMPAT_BRK
107 #else
109 #endif
111 static int __init disable_randmaps(char *s)
113 randomize_va_space = 0;
114 return 1;
116 __setup("norandmaps", disable_randmaps);
118 unsigned long zero_pfn __read_mostly;
119 unsigned long highest_memmap_pfn __read_mostly;
122 * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
124 static int __init init_zero_pfn(void)
126 zero_pfn = page_to_pfn(ZERO_PAGE(0));
127 return 0;
129 core_initcall(init_zero_pfn);
132 #if defined(SPLIT_RSS_COUNTING)
134 void sync_mm_rss(struct mm_struct *mm)
136 int i;
138 for (i = 0; i < NR_MM_COUNTERS; i++) {
139 if (current->rss_stat.count[i]) {
140 add_mm_counter(mm, i, current->rss_stat.count[i]);
141 current->rss_stat.count[i] = 0;
144 current->rss_stat.events = 0;
147 static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
149 struct task_struct *task = current;
151 if (likely(task->mm == mm))
152 task->rss_stat.count[member] += val;
153 else
154 add_mm_counter(mm, member, val);
156 #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
157 #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
159 /* sync counter once per 64 page faults */
160 #define TASK_RSS_EVENTS_THRESH (64)
161 static void check_sync_rss_stat(struct task_struct *task)
163 if (unlikely(task != current))
164 return;
165 if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
166 sync_mm_rss(task->mm);
168 #else /* SPLIT_RSS_COUNTING */
170 #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
171 #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
173 static void check_sync_rss_stat(struct task_struct *task)
177 #endif /* SPLIT_RSS_COUNTING */
179 #ifdef HAVE_GENERIC_MMU_GATHER
181 static int tlb_next_batch(struct mmu_gather *tlb)
183 struct mmu_gather_batch *batch;
185 batch = tlb->active;
186 if (batch->next) {
187 tlb->active = batch->next;
188 return 1;
191 if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
192 return 0;
194 batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
195 if (!batch)
196 return 0;
198 tlb->batch_count++;
199 batch->next = NULL;
200 batch->nr = 0;
201 batch->max = MAX_GATHER_BATCH;
203 tlb->active->next = batch;
204 tlb->active = batch;
206 return 1;
209 /* tlb_gather_mmu
210 * Called to initialize an (on-stack) mmu_gather structure for page-table
211 * tear-down from @mm. The @fullmm argument is used when @mm is without
212 * users and we're going to destroy the full address space (exit/execve).
214 void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
216 tlb->mm = mm;
218 /* Is it from 0 to ~0? */
219 tlb->fullmm = !(start | (end+1));
220 tlb->need_flush_all = 0;
221 tlb->start = start;
222 tlb->end = end;
223 tlb->need_flush = 0;
224 tlb->local.next = NULL;
225 tlb->local.nr = 0;
226 tlb->local.max = ARRAY_SIZE(tlb->__pages);
227 tlb->active = &tlb->local;
228 tlb->batch_count = 0;
230 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
231 tlb->batch = NULL;
232 #endif
235 void tlb_flush_mmu(struct mmu_gather *tlb)
237 struct mmu_gather_batch *batch;
239 if (!tlb->need_flush)
240 return;
241 tlb->need_flush = 0;
242 tlb_flush(tlb);
243 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
244 tlb_table_flush(tlb);
245 #endif
247 for (batch = &tlb->local; batch; batch = batch->next) {
248 free_pages_and_swap_cache(batch->pages, batch->nr);
249 batch->nr = 0;
251 tlb->active = &tlb->local;
254 /* tlb_finish_mmu
255 * Called at the end of the shootdown operation to free up any resources
256 * that were required.
258 void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
260 struct mmu_gather_batch *batch, *next;
262 tlb_flush_mmu(tlb);
264 /* keep the page table cache within bounds */
265 check_pgt_cache();
267 for (batch = tlb->local.next; batch; batch = next) {
268 next = batch->next;
269 free_pages((unsigned long)batch, 0);
271 tlb->local.next = NULL;
274 /* __tlb_remove_page
275 * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
276 * handling the additional races in SMP caused by other CPUs caching valid
277 * mappings in their TLBs. Returns the number of free page slots left.
278 * When out of page slots we must call tlb_flush_mmu().
280 int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
282 struct mmu_gather_batch *batch;
284 VM_BUG_ON(!tlb->need_flush);
286 batch = tlb->active;
287 batch->pages[batch->nr++] = page;
288 if (batch->nr == batch->max) {
289 if (!tlb_next_batch(tlb))
290 return 0;
291 batch = tlb->active;
293 VM_BUG_ON_PAGE(batch->nr > batch->max, page);
295 return batch->max - batch->nr;
298 #endif /* HAVE_GENERIC_MMU_GATHER */
300 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
303 * See the comment near struct mmu_table_batch.
306 static void tlb_remove_table_smp_sync(void *arg)
308 /* Simply deliver the interrupt */
311 static void tlb_remove_table_one(void *table)
314 * This isn't an RCU grace period and hence the page-tables cannot be
315 * assumed to be actually RCU-freed.
317 * It is however sufficient for software page-table walkers that rely on
318 * IRQ disabling. See the comment near struct mmu_table_batch.
320 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
321 __tlb_remove_table(table);
324 static void tlb_remove_table_rcu(struct rcu_head *head)
326 struct mmu_table_batch *batch;
327 int i;
329 batch = container_of(head, struct mmu_table_batch, rcu);
331 for (i = 0; i < batch->nr; i++)
332 __tlb_remove_table(batch->tables[i]);
334 free_page((unsigned long)batch);
337 void tlb_table_flush(struct mmu_gather *tlb)
339 struct mmu_table_batch **batch = &tlb->batch;
341 if (*batch) {
342 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
343 *batch = NULL;
347 void tlb_remove_table(struct mmu_gather *tlb, void *table)
349 struct mmu_table_batch **batch = &tlb->batch;
351 tlb->need_flush = 1;
354 * When there's less then two users of this mm there cannot be a
355 * concurrent page-table walk.
357 if (atomic_read(&tlb->mm->mm_users) < 2) {
358 __tlb_remove_table(table);
359 return;
362 if (*batch == NULL) {
363 *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
364 if (*batch == NULL) {
365 tlb_remove_table_one(table);
366 return;
368 (*batch)->nr = 0;
370 (*batch)->tables[(*batch)->nr++] = table;
371 if ((*batch)->nr == MAX_TABLE_BATCH)
372 tlb_table_flush(tlb);
375 #endif /* CONFIG_HAVE_RCU_TABLE_FREE */
378 * Note: this doesn't free the actual pages themselves. That
379 * has been handled earlier when unmapping all the memory regions.
381 static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
382 unsigned long addr)
384 pgtable_t token = pmd_pgtable(*pmd);
385 pmd_clear(pmd);
386 pte_free_tlb(tlb, token, addr);
387 atomic_long_dec(&tlb->mm->nr_ptes);
390 static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
391 unsigned long addr, unsigned long end,
392 unsigned long floor, unsigned long ceiling)
394 pmd_t *pmd;
395 unsigned long next;
396 unsigned long start;
398 start = addr;
399 pmd = pmd_offset(pud, addr);
400 do {
401 next = pmd_addr_end(addr, end);
402 if (pmd_none_or_clear_bad(pmd))
403 continue;
404 free_pte_range(tlb, pmd, addr);
405 } while (pmd++, addr = next, addr != end);
407 start &= PUD_MASK;
408 if (start < floor)
409 return;
410 if (ceiling) {
411 ceiling &= PUD_MASK;
412 if (!ceiling)
413 return;
415 if (end - 1 > ceiling - 1)
416 return;
418 pmd = pmd_offset(pud, start);
419 pud_clear(pud);
420 pmd_free_tlb(tlb, pmd, start);
423 static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
424 unsigned long addr, unsigned long end,
425 unsigned long floor, unsigned long ceiling)
427 pud_t *pud;
428 unsigned long next;
429 unsigned long start;
431 start = addr;
432 pud = pud_offset(pgd, addr);
433 do {
434 next = pud_addr_end(addr, end);
435 if (pud_none_or_clear_bad(pud))
436 continue;
437 free_pmd_range(tlb, pud, addr, next, floor, ceiling);
438 } while (pud++, addr = next, addr != end);
440 start &= PGDIR_MASK;
441 if (start < floor)
442 return;
443 if (ceiling) {
444 ceiling &= PGDIR_MASK;
445 if (!ceiling)
446 return;
448 if (end - 1 > ceiling - 1)
449 return;
451 pud = pud_offset(pgd, start);
452 pgd_clear(pgd);
453 pud_free_tlb(tlb, pud, start);
457 * This function frees user-level page tables of a process.
459 void free_pgd_range(struct mmu_gather *tlb,
460 unsigned long addr, unsigned long end,
461 unsigned long floor, unsigned long ceiling)
463 pgd_t *pgd;
464 unsigned long next;
467 * The next few lines have given us lots of grief...
469 * Why are we testing PMD* at this top level? Because often
470 * there will be no work to do at all, and we'd prefer not to
471 * go all the way down to the bottom just to discover that.
473 * Why all these "- 1"s? Because 0 represents both the bottom
474 * of the address space and the top of it (using -1 for the
475 * top wouldn't help much: the masks would do the wrong thing).
476 * The rule is that addr 0 and floor 0 refer to the bottom of
477 * the address space, but end 0 and ceiling 0 refer to the top
478 * Comparisons need to use "end - 1" and "ceiling - 1" (though
479 * that end 0 case should be mythical).
481 * Wherever addr is brought up or ceiling brought down, we must
482 * be careful to reject "the opposite 0" before it confuses the
483 * subsequent tests. But what about where end is brought down
484 * by PMD_SIZE below? no, end can't go down to 0 there.
486 * Whereas we round start (addr) and ceiling down, by different
487 * masks at different levels, in order to test whether a table
488 * now has no other vmas using it, so can be freed, we don't
489 * bother to round floor or end up - the tests don't need that.
492 addr &= PMD_MASK;
493 if (addr < floor) {
494 addr += PMD_SIZE;
495 if (!addr)
496 return;
498 if (ceiling) {
499 ceiling &= PMD_MASK;
500 if (!ceiling)
501 return;
503 if (end - 1 > ceiling - 1)
504 end -= PMD_SIZE;
505 if (addr > end - 1)
506 return;
508 pgd = pgd_offset(tlb->mm, addr);
509 do {
510 next = pgd_addr_end(addr, end);
511 if (pgd_none_or_clear_bad(pgd))
512 continue;
513 free_pud_range(tlb, pgd, addr, next, floor, ceiling);
514 } while (pgd++, addr = next, addr != end);
517 void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
518 unsigned long floor, unsigned long ceiling)
520 while (vma) {
521 struct vm_area_struct *next = vma->vm_next;
522 unsigned long addr = vma->vm_start;
525 * Hide vma from rmap and truncate_pagecache before freeing
526 * pgtables
528 unlink_anon_vmas(vma);
529 unlink_file_vma(vma);
531 if (is_vm_hugetlb_page(vma)) {
532 hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
533 floor, next? next->vm_start: ceiling);
534 } else {
536 * Optimization: gather nearby vmas into one call down
538 while (next && next->vm_start <= vma->vm_end + PMD_SIZE
539 && !is_vm_hugetlb_page(next)) {
540 vma = next;
541 next = vma->vm_next;
542 unlink_anon_vmas(vma);
543 unlink_file_vma(vma);
545 free_pgd_range(tlb, addr, vma->vm_end,
546 floor, next? next->vm_start: ceiling);
548 vma = next;
552 int __pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
553 pmd_t *pmd, unsigned long address)
555 spinlock_t *ptl;
556 pgtable_t new = pte_alloc_one(mm, address);
557 int wait_split_huge_page;
558 if (!new)
559 return -ENOMEM;
562 * Ensure all pte setup (eg. pte page lock and page clearing) are
563 * visible before the pte is made visible to other CPUs by being
564 * put into page tables.
566 * The other side of the story is the pointer chasing in the page
567 * table walking code (when walking the page table without locking;
568 * ie. most of the time). Fortunately, these data accesses consist
569 * of a chain of data-dependent loads, meaning most CPUs (alpha
570 * being the notable exception) will already guarantee loads are
571 * seen in-order. See the alpha page table accessors for the
572 * smp_read_barrier_depends() barriers in page table walking code.
574 smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
576 ptl = pmd_lock(mm, pmd);
577 wait_split_huge_page = 0;
578 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
579 atomic_long_inc(&mm->nr_ptes);
580 pmd_populate(mm, pmd, new);
581 new = NULL;
582 } else if (unlikely(pmd_trans_splitting(*pmd)))
583 wait_split_huge_page = 1;
584 spin_unlock(ptl);
585 if (new)
586 pte_free(mm, new);
587 if (wait_split_huge_page)
588 wait_split_huge_page(vma->anon_vma, pmd);
589 return 0;
592 int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
594 pte_t *new = pte_alloc_one_kernel(&init_mm, address);
595 if (!new)
596 return -ENOMEM;
598 smp_wmb(); /* See comment in __pte_alloc */
600 spin_lock(&init_mm.page_table_lock);
601 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
602 pmd_populate_kernel(&init_mm, pmd, new);
603 new = NULL;
604 } else
605 VM_BUG_ON(pmd_trans_splitting(*pmd));
606 spin_unlock(&init_mm.page_table_lock);
607 if (new)
608 pte_free_kernel(&init_mm, new);
609 return 0;
612 static inline void init_rss_vec(int *rss)
614 memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
617 static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
619 int i;
621 if (current->mm == mm)
622 sync_mm_rss(mm);
623 for (i = 0; i < NR_MM_COUNTERS; i++)
624 if (rss[i])
625 add_mm_counter(mm, i, rss[i]);
629 * This function is called to print an error when a bad pte
630 * is found. For example, we might have a PFN-mapped pte in
631 * a region that doesn't allow it.
633 * The calling function must still handle the error.
635 static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
636 pte_t pte, struct page *page)
638 pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
639 pud_t *pud = pud_offset(pgd, addr);
640 pmd_t *pmd = pmd_offset(pud, addr);
641 struct address_space *mapping;
642 pgoff_t index;
643 static unsigned long resume;
644 static unsigned long nr_shown;
645 static unsigned long nr_unshown;
648 * Allow a burst of 60 reports, then keep quiet for that minute;
649 * or allow a steady drip of one report per second.
651 if (nr_shown == 60) {
652 if (time_before(jiffies, resume)) {
653 nr_unshown++;
654 return;
656 if (nr_unshown) {
657 printk(KERN_ALERT
658 "BUG: Bad page map: %lu messages suppressed\n",
659 nr_unshown);
660 nr_unshown = 0;
662 nr_shown = 0;
664 if (nr_shown++ == 0)
665 resume = jiffies + 60 * HZ;
667 mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
668 index = linear_page_index(vma, addr);
670 printk(KERN_ALERT
671 "BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n",
672 current->comm,
673 (long long)pte_val(pte), (long long)pmd_val(*pmd));
674 if (page)
675 dump_page(page, "bad pte");
676 printk(KERN_ALERT
677 "addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n",
678 (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
680 * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y
682 if (vma->vm_ops)
683 printk(KERN_ALERT "vma->vm_ops->fault: %pSR\n",
684 vma->vm_ops->fault);
685 if (vma->vm_file)
686 printk(KERN_ALERT "vma->vm_file->f_op->mmap: %pSR\n",
687 vma->vm_file->f_op->mmap);
688 dump_stack();
689 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
692 static inline bool is_cow_mapping(vm_flags_t flags)
694 return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
698 * vm_normal_page -- This function gets the "struct page" associated with a pte.
700 * "Special" mappings do not wish to be associated with a "struct page" (either
701 * it doesn't exist, or it exists but they don't want to touch it). In this
702 * case, NULL is returned here. "Normal" mappings do have a struct page.
704 * There are 2 broad cases. Firstly, an architecture may define a pte_special()
705 * pte bit, in which case this function is trivial. Secondly, an architecture
706 * may not have a spare pte bit, which requires a more complicated scheme,
707 * described below.
709 * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
710 * special mapping (even if there are underlying and valid "struct pages").
711 * COWed pages of a VM_PFNMAP are always normal.
713 * The way we recognize COWed pages within VM_PFNMAP mappings is through the
714 * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
715 * set, and the vm_pgoff will point to the first PFN mapped: thus every special
716 * mapping will always honor the rule
718 * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
720 * And for normal mappings this is false.
722 * This restricts such mappings to be a linear translation from virtual address
723 * to pfn. To get around this restriction, we allow arbitrary mappings so long
724 * as the vma is not a COW mapping; in that case, we know that all ptes are
725 * special (because none can have been COWed).
728 * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
730 * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
731 * page" backing, however the difference is that _all_ pages with a struct
732 * page (that is, those where pfn_valid is true) are refcounted and considered
733 * normal pages by the VM. The disadvantage is that pages are refcounted
734 * (which can be slower and simply not an option for some PFNMAP users). The
735 * advantage is that we don't have to follow the strict linearity rule of
736 * PFNMAP mappings in order to support COWable mappings.
739 #ifdef __HAVE_ARCH_PTE_SPECIAL
740 # define HAVE_PTE_SPECIAL 1
741 #else
742 # define HAVE_PTE_SPECIAL 0
743 #endif
744 struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
745 pte_t pte)
747 unsigned long pfn = pte_pfn(pte);
749 if (HAVE_PTE_SPECIAL) {
750 if (likely(!pte_special(pte)))
751 goto check_pfn;
752 if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
753 return NULL;
754 if (!is_zero_pfn(pfn))
755 print_bad_pte(vma, addr, pte, NULL);
756 return NULL;
759 /* !HAVE_PTE_SPECIAL case follows: */
761 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
762 if (vma->vm_flags & VM_MIXEDMAP) {
763 if (!pfn_valid(pfn))
764 return NULL;
765 goto out;
766 } else {
767 unsigned long off;
768 off = (addr - vma->vm_start) >> PAGE_SHIFT;
769 if (pfn == vma->vm_pgoff + off)
770 return NULL;
771 if (!is_cow_mapping(vma->vm_flags))
772 return NULL;
776 if (is_zero_pfn(pfn))
777 return NULL;
778 check_pfn:
779 if (unlikely(pfn > highest_memmap_pfn)) {
780 print_bad_pte(vma, addr, pte, NULL);
781 return NULL;
785 * NOTE! We still have PageReserved() pages in the page tables.
786 * eg. VDSO mappings can cause them to exist.
788 out:
789 return pfn_to_page(pfn);
793 * copy one vm_area from one task to the other. Assumes the page tables
794 * already present in the new task to be cleared in the whole range
795 * covered by this vma.
798 static inline unsigned long
799 copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
800 pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
801 unsigned long addr, int *rss)
803 unsigned long vm_flags = vma->vm_flags;
804 pte_t pte = *src_pte;
805 struct page *page;
807 /* pte contains position in swap or file, so copy. */
808 if (unlikely(!pte_present(pte))) {
809 if (!pte_file(pte)) {
810 swp_entry_t entry = pte_to_swp_entry(pte);
812 if (likely(!non_swap_entry(entry))) {
813 if (swap_duplicate(entry) < 0)
814 return entry.val;
816 /* make sure dst_mm is on swapoff's mmlist. */
817 if (unlikely(list_empty(&dst_mm->mmlist))) {
818 spin_lock(&mmlist_lock);
819 if (list_empty(&dst_mm->mmlist))
820 list_add(&dst_mm->mmlist,
821 &src_mm->mmlist);
822 spin_unlock(&mmlist_lock);
824 rss[MM_SWAPENTS]++;
825 } else if (is_migration_entry(entry)) {
826 page = migration_entry_to_page(entry);
828 if (PageAnon(page))
829 rss[MM_ANONPAGES]++;
830 else
831 rss[MM_FILEPAGES]++;
833 if (is_write_migration_entry(entry) &&
834 is_cow_mapping(vm_flags)) {
836 * COW mappings require pages in both
837 * parent and child to be set to read.
839 make_migration_entry_read(&entry);
840 pte = swp_entry_to_pte(entry);
841 if (pte_swp_soft_dirty(*src_pte))
842 pte = pte_swp_mksoft_dirty(pte);
843 set_pte_at(src_mm, addr, src_pte, pte);
847 goto out_set_pte;
851 * If it's a COW mapping, write protect it both
852 * in the parent and the child
854 if (is_cow_mapping(vm_flags)) {
855 ptep_set_wrprotect(src_mm, addr, src_pte);
856 pte = pte_wrprotect(pte);
860 * If it's a shared mapping, mark it clean in
861 * the child
863 if (vm_flags & VM_SHARED)
864 pte = pte_mkclean(pte);
865 pte = pte_mkold(pte);
867 page = vm_normal_page(vma, addr, pte);
868 if (page) {
869 get_page(page);
870 page_dup_rmap(page);
871 if (PageAnon(page))
872 rss[MM_ANONPAGES]++;
873 else
874 rss[MM_FILEPAGES]++;
877 out_set_pte:
878 set_pte_at(dst_mm, addr, dst_pte, pte);
879 return 0;
882 static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
883 pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
884 unsigned long addr, unsigned long end)
886 pte_t *orig_src_pte, *orig_dst_pte;
887 pte_t *src_pte, *dst_pte;
888 #ifdef CONFIG_MCST
889 spinlock_t *src_ptl, *uninitialized_var(dst_ptl);
890 #else
891 spinlock_t *src_ptl, *dst_ptl;
892 #endif
893 int progress = 0;
894 int rss[NR_MM_COUNTERS];
895 swp_entry_t entry = (swp_entry_t){0};
897 again:
898 init_rss_vec(rss);
900 dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
901 if (!dst_pte)
902 return -ENOMEM;
903 src_pte = pte_offset_map(src_pmd, addr);
904 src_ptl = pte_lockptr(src_mm, src_pmd);
905 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
906 orig_src_pte = src_pte;
907 orig_dst_pte = dst_pte;
908 arch_enter_lazy_mmu_mode();
910 do {
912 * We are holding two locks at this point - either of them
913 * could generate latencies in another task on another CPU.
915 if (progress >= 32) {
916 progress = 0;
917 if (need_resched() ||
918 spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
919 break;
921 if (pte_none(*src_pte)) {
922 progress++;
923 continue;
925 entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
926 vma, addr, rss);
927 if (entry.val)
928 break;
929 progress += 8;
930 } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
932 arch_leave_lazy_mmu_mode();
933 spin_unlock(src_ptl);
934 pte_unmap(orig_src_pte);
935 add_mm_rss_vec(dst_mm, rss);
936 pte_unmap_unlock(orig_dst_pte, dst_ptl);
937 cond_resched();
939 if (entry.val) {
940 if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
941 return -ENOMEM;
942 progress = 0;
944 if (addr != end)
945 goto again;
946 return 0;
949 static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
950 pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
951 unsigned long addr, unsigned long end)
953 pmd_t *src_pmd, *dst_pmd;
954 unsigned long next;
955 #if defined(CONFIG_E2K) && defined(CONFIG_SECONDARY_SPACE_SUPPORT)
956 unsigned long sec = ((pud_secondary(*src_pud) && !IS_UPT_E3S) ?
957 TBL_SEC_BIT : 0);
959 dst_pmd = pmd_alloc(dst_mm, dst_pud, addr | sec);
960 #else
961 dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
962 #endif
963 if (!dst_pmd)
964 return -ENOMEM;
965 src_pmd = pmd_offset(src_pud, addr);
966 do {
967 next = pmd_addr_end(addr, end);
968 if (pmd_trans_huge(*src_pmd)) {
969 int err;
970 VM_BUG_ON(next-addr != HPAGE_PMD_SIZE);
971 err = copy_huge_pmd(dst_mm, src_mm,
972 dst_pmd, src_pmd, addr, vma);
973 if (err == -ENOMEM)
974 return -ENOMEM;
975 if (!err)
976 continue;
977 /* fall through */
979 if (pmd_none_or_clear_bad(src_pmd))
980 continue;
981 if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
982 vma, addr, next))
983 return -ENOMEM;
984 } while (dst_pmd++, src_pmd++, addr = next, addr != end);
985 return 0;
988 static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
989 pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
990 unsigned long addr, unsigned long end)
992 pud_t *src_pud, *dst_pud;
993 unsigned long next;
995 dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
996 if (!dst_pud)
997 return -ENOMEM;
998 src_pud = pud_offset(src_pgd, addr);
999 do {
1000 next = pud_addr_end(addr, end);
1001 if (pud_none_or_clear_bad(src_pud))
1002 continue;
1003 if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
1004 vma, addr, next))
1005 return -ENOMEM;
1006 } while (dst_pud++, src_pud++, addr = next, addr != end);
1007 return 0;
1010 int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1011 struct vm_area_struct *vma)
1013 pgd_t *src_pgd, *dst_pgd;
1014 unsigned long next;
1015 unsigned long addr = vma->vm_start;
1016 unsigned long end = vma->vm_end;
1017 unsigned long mmun_start; /* For mmu_notifiers */
1018 unsigned long mmun_end; /* For mmu_notifiers */
1019 bool is_cow;
1020 int ret;
1023 * Don't copy ptes where a page fault will fill them correctly.
1024 * Fork becomes much lighter when there are big shared or private
1025 * readonly mappings. The tradeoff is that copy_page_range is more
1026 * efficient than faulting.
1028 if (!(vma->vm_flags & (VM_HUGETLB | VM_NONLINEAR |
1029 VM_PFNMAP | VM_MIXEDMAP))) {
1030 if (!vma->anon_vma)
1031 return 0;
1034 if (is_vm_hugetlb_page(vma))
1035 return copy_hugetlb_page_range(dst_mm, src_mm, vma);
1037 if (unlikely(vma->vm_flags & VM_PFNMAP)) {
1039 * We do not free on error cases below as remove_vma
1040 * gets called on error from higher level routine
1042 ret = track_pfn_copy(vma);
1043 if (ret)
1044 return ret;
1048 * We need to invalidate the secondary MMU mappings only when
1049 * there could be a permission downgrade on the ptes of the
1050 * parent mm. And a permission downgrade will only happen if
1051 * is_cow_mapping() returns true.
1053 is_cow = is_cow_mapping(vma->vm_flags);
1054 mmun_start = addr;
1055 mmun_end = end;
1056 if (is_cow)
1057 mmu_notifier_invalidate_range_start(src_mm, mmun_start,
1058 mmun_end);
1060 ret = 0;
1061 dst_pgd = pgd_offset(dst_mm, addr);
1062 src_pgd = pgd_offset(src_mm, addr);
1063 do {
1064 next = pgd_addr_end(addr, end);
1065 if (pgd_none_or_clear_bad(src_pgd))
1066 continue;
1067 if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
1068 vma, addr, next))) {
1069 ret = -ENOMEM;
1070 break;
1072 } while (dst_pgd++, src_pgd++, addr = next, addr != end);
1074 if (is_cow)
1075 mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end);
1076 return ret;
1079 static unsigned long zap_pte_range(struct mmu_gather *tlb,
1080 struct vm_area_struct *vma, pmd_t *pmd,
1081 unsigned long addr, unsigned long end,
1082 struct zap_details *details)
1084 struct mm_struct *mm = tlb->mm;
1085 int force_flush = 0;
1086 int rss[NR_MM_COUNTERS];
1087 spinlock_t *ptl;
1088 pte_t *start_pte;
1089 pte_t *pte;
1091 again:
1092 init_rss_vec(rss);
1093 start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
1094 pte = start_pte;
1095 arch_enter_lazy_mmu_mode();
1096 do {
1097 pte_t ptent = *pte;
1098 if (pte_none(ptent)) {
1099 #if defined(CONFIG_E2K) && defined(CONFIG_MAKE_ALL_PAGES_VALID)
1100 if (pte_valid(ptent)) {
1101 if (!test_ts_flag(TS_KEEP_PAGES_VALID))
1102 pte_clear_not_present_full(mm, addr,
1103 pte, tlb->fullmm);
1105 #endif
1106 continue;
1109 if (pte_present(ptent)) {
1110 struct page *page;
1112 page = vm_normal_page(vma, addr, ptent);
1113 if (unlikely(details) && page) {
1115 * unmap_shared_mapping_pages() wants to
1116 * invalidate cache without truncating:
1117 * unmap shared but keep private pages.
1119 if (details->check_mapping &&
1120 details->check_mapping != page->mapping)
1121 continue;
1123 * Each page->index must be checked when
1124 * invalidating or truncating nonlinear.
1126 if (details->nonlinear_vma &&
1127 (page->index < details->first_index ||
1128 page->index > details->last_index))
1129 continue;
1131 #if defined(CONFIG_E2K) && defined(CONFIG_MAKE_ALL_PAGES_VALID)
1132 if (test_ts_flag(TS_KEEP_PAGES_VALID))
1133 ptent = ptep_get_and_clear_as_valid(mm, addr,
1134 pte);
1135 else
1136 #endif
1137 ptent = ptep_get_and_clear_full(mm, addr, pte,
1138 tlb->fullmm);
1139 tlb_remove_tlb_entry(tlb, pte, addr);
1140 if (unlikely(!page))
1141 continue;
1142 if (unlikely(details) && details->nonlinear_vma
1143 && linear_page_index(details->nonlinear_vma,
1144 addr) != page->index) {
1145 pte_t ptfile = pgoff_to_pte(page->index);
1146 if (pte_soft_dirty(ptent))
1147 ptfile = pte_file_mksoft_dirty(ptfile);
1148 set_pte_at(mm, addr, pte, ptfile);
1150 if (PageAnon(page))
1151 rss[MM_ANONPAGES]--;
1152 else {
1153 if (pte_dirty(ptent))
1154 set_page_dirty(page);
1155 if (pte_young(ptent) &&
1156 likely(!(vma->vm_flags & VM_SEQ_READ)))
1157 mark_page_accessed(page);
1158 rss[MM_FILEPAGES]--;
1160 page_remove_rmap(page);
1161 if (unlikely(page_mapcount(page) < 0))
1162 print_bad_pte(vma, addr, ptent, page);
1163 force_flush = !__tlb_remove_page(tlb, page);
1164 if (force_flush)
1165 break;
1166 continue;
1169 * If details->check_mapping, we leave swap entries;
1170 * if details->nonlinear_vma, we leave file entries.
1172 if (unlikely(details))
1173 continue;
1174 if (pte_file(ptent)) {
1175 if (unlikely(!(vma->vm_flags & VM_NONLINEAR)))
1176 print_bad_pte(vma, addr, ptent, NULL);
1177 } else {
1178 swp_entry_t entry = pte_to_swp_entry(ptent);
1180 if (!non_swap_entry(entry))
1181 rss[MM_SWAPENTS]--;
1182 else if (is_migration_entry(entry)) {
1183 struct page *page;
1185 page = migration_entry_to_page(entry);
1187 if (PageAnon(page))
1188 rss[MM_ANONPAGES]--;
1189 else
1190 rss[MM_FILEPAGES]--;
1192 if (unlikely(!free_swap_and_cache(entry)))
1193 print_bad_pte(vma, addr, ptent, NULL);
1195 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
1196 } while (pte++, addr += PAGE_SIZE, addr != end);
1198 add_mm_rss_vec(mm, rss);
1199 arch_leave_lazy_mmu_mode();
1200 pte_unmap_unlock(start_pte, ptl);
1203 * mmu_gather ran out of room to batch pages, we break out of
1204 * the PTE lock to avoid doing the potential expensive TLB invalidate
1205 * and page-free while holding it.
1207 if (force_flush) {
1208 unsigned long old_end;
1210 force_flush = 0;
1213 * Flush the TLB just for the previous segment,
1214 * then update the range to be the remaining
1215 * TLB range.
1217 old_end = tlb->end;
1218 tlb->end = addr;
1220 tlb_flush_mmu(tlb);
1222 tlb->start = addr;
1223 tlb->end = old_end;
1225 if (addr != end)
1226 goto again;
1229 return addr;
1232 static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
1233 struct vm_area_struct *vma, pud_t *pud,
1234 unsigned long addr, unsigned long end,
1235 struct zap_details *details)
1237 pmd_t *pmd;
1238 unsigned long next;
1240 pmd = pmd_offset(pud, addr);
1241 do {
1242 next = pmd_addr_end(addr, end);
1243 if (pmd_trans_huge(*pmd)) {
1244 if (next - addr != HPAGE_PMD_SIZE) {
1245 #ifdef CONFIG_DEBUG_VM
1246 if (!rwsem_is_locked(&tlb->mm->mmap_sem)) {
1247 pr_err("%s: mmap_sem is unlocked! addr=0x%lx end=0x%lx vma->vm_start=0x%lx vma->vm_end=0x%lx\n",
1248 __func__, addr, end,
1249 vma->vm_start,
1250 vma->vm_end);
1251 BUG();
1253 #endif
1254 split_huge_page_pmd(vma, addr, pmd);
1255 } else if (zap_huge_pmd(tlb, vma, pmd, addr))
1256 goto next;
1257 /* fall through */
1260 * Here there can be other concurrent MADV_DONTNEED or
1261 * trans huge page faults running, and if the pmd is
1262 * none or trans huge it can change under us. This is
1263 * because MADV_DONTNEED holds the mmap_sem in read
1264 * mode.
1266 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
1267 goto next;
1268 next = zap_pte_range(tlb, vma, pmd, addr, next, details);
1269 next:
1270 cond_resched();
1271 } while (pmd++, addr = next, addr != end);
1273 return addr;
1276 static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
1277 struct vm_area_struct *vma, pgd_t *pgd,
1278 unsigned long addr, unsigned long end,
1279 struct zap_details *details)
1281 pud_t *pud;
1282 unsigned long next;
1284 pud = pud_offset(pgd, addr);
1285 do {
1286 next = pud_addr_end(addr, end);
1287 if (pud_none_or_clear_bad(pud))
1288 continue;
1289 next = zap_pmd_range(tlb, vma, pud, addr, next, details);
1290 } while (pud++, addr = next, addr != end);
1292 return addr;
1295 static void unmap_page_range(struct mmu_gather *tlb,
1296 struct vm_area_struct *vma,
1297 unsigned long addr, unsigned long end,
1298 struct zap_details *details)
1300 pgd_t *pgd;
1301 unsigned long next;
1303 if (details && !details->check_mapping && !details->nonlinear_vma)
1304 details = NULL;
1306 BUG_ON(addr >= end);
1307 mem_cgroup_uncharge_start();
1308 tlb_start_vma(tlb, vma);
1309 pgd = pgd_offset(vma->vm_mm, addr);
1310 do {
1311 next = pgd_addr_end(addr, end);
1312 if (pgd_none_or_clear_bad(pgd))
1313 continue;
1314 next = zap_pud_range(tlb, vma, pgd, addr, next, details);
1315 } while (pgd++, addr = next, addr != end);
1316 tlb_end_vma(tlb, vma);
1317 mem_cgroup_uncharge_end();
1321 static void unmap_single_vma(struct mmu_gather *tlb,
1322 struct vm_area_struct *vma, unsigned long start_addr,
1323 unsigned long end_addr,
1324 struct zap_details *details)
1326 unsigned long start = max(vma->vm_start, start_addr);
1327 unsigned long end;
1329 if (start >= vma->vm_end)
1330 return;
1331 end = min(vma->vm_end, end_addr);
1332 if (end <= vma->vm_start)
1333 return;
1335 if (vma->vm_file)
1336 uprobe_munmap(vma, start, end);
1338 if (unlikely(vma->vm_flags & VM_PFNMAP))
1339 untrack_pfn(vma, 0, 0);
1341 if (start != end) {
1342 if (unlikely(is_vm_hugetlb_page(vma))) {
1344 * It is undesirable to test vma->vm_file as it
1345 * should be non-null for valid hugetlb area.
1346 * However, vm_file will be NULL in the error
1347 * cleanup path of do_mmap_pgoff. When
1348 * hugetlbfs ->mmap method fails,
1349 * do_mmap_pgoff() nullifies vma->vm_file
1350 * before calling this function to clean up.
1351 * Since no pte has actually been setup, it is
1352 * safe to do nothing in this case.
1354 if (vma->vm_file) {
1355 mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
1356 __unmap_hugepage_range_final(tlb, vma, start, end, NULL);
1357 mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
1359 } else
1360 unmap_page_range(tlb, vma, start, end, details);
1365 * unmap_vmas - unmap a range of memory covered by a list of vma's
1366 * @tlb: address of the caller's struct mmu_gather
1367 * @vma: the starting vma
1368 * @start_addr: virtual address at which to start unmapping
1369 * @end_addr: virtual address at which to end unmapping
1371 * Unmap all pages in the vma list.
1373 * Only addresses between `start' and `end' will be unmapped.
1375 * The VMA list must be sorted in ascending virtual address order.
1377 * unmap_vmas() assumes that the caller will flush the whole unmapped address
1378 * range after unmap_vmas() returns. So the only responsibility here is to
1379 * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
1380 * drops the lock and schedules.
1382 void unmap_vmas(struct mmu_gather *tlb,
1383 struct vm_area_struct *vma, unsigned long start_addr,
1384 unsigned long end_addr)
1386 struct mm_struct *mm = vma->vm_mm;
1388 mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
1389 for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
1390 unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
1391 mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
1395 * zap_page_range - remove user pages in a given range
1396 * @vma: vm_area_struct holding the applicable pages
1397 * @start: starting address of pages to zap
1398 * @size: number of bytes to zap
1399 * @details: details of nonlinear truncation or shared cache invalidation
1401 * Caller must protect the VMA list
1403 void zap_page_range(struct vm_area_struct *vma, unsigned long start,
1404 unsigned long size, struct zap_details *details)
1406 struct mm_struct *mm = vma->vm_mm;
1407 struct mmu_gather tlb;
1408 unsigned long end = start + size;
1410 lru_add_drain();
1411 tlb_gather_mmu(&tlb, mm, start, end);
1412 update_hiwater_rss(mm);
1413 mmu_notifier_invalidate_range_start(mm, start, end);
1414 for ( ; vma && vma->vm_start < end; vma = vma->vm_next)
1415 unmap_single_vma(&tlb, vma, start, end, details);
1416 mmu_notifier_invalidate_range_end(mm, start, end);
1417 tlb_finish_mmu(&tlb, start, end);
1421 * zap_page_range_single - remove user pages in a given range
1422 * @vma: vm_area_struct holding the applicable pages
1423 * @address: starting address of pages to zap
1424 * @size: number of bytes to zap
1425 * @details: details of nonlinear truncation or shared cache invalidation
1427 * The range must fit into one VMA.
1429 static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
1430 unsigned long size, struct zap_details *details)
1432 struct mm_struct *mm = vma->vm_mm;
1433 struct mmu_gather tlb;
1434 unsigned long end = address + size;
1436 lru_add_drain();
1437 tlb_gather_mmu(&tlb, mm, address, end);
1438 update_hiwater_rss(mm);
1439 mmu_notifier_invalidate_range_start(mm, address, end);
1440 unmap_single_vma(&tlb, vma, address, end, details);
1441 mmu_notifier_invalidate_range_end(mm, address, end);
1442 tlb_finish_mmu(&tlb, address, end);
1446 * zap_vma_ptes - remove ptes mapping the vma
1447 * @vma: vm_area_struct holding ptes to be zapped
1448 * @address: starting address of pages to zap
1449 * @size: number of bytes to zap
1451 * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1453 * The entire address range must be fully contained within the vma.
1455 * Returns 0 if successful.
1457 int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
1458 unsigned long size)
1460 if (address < vma->vm_start || address + size > vma->vm_end ||
1461 !(vma->vm_flags & VM_PFNMAP))
1462 return -1;
1463 zap_page_range_single(vma, address, size, NULL);
1464 return 0;
1466 EXPORT_SYMBOL_GPL(zap_vma_ptes);
1469 * follow_page_mask - look up a page descriptor from a user-virtual address
1470 * @vma: vm_area_struct mapping @address
1471 * @address: virtual address to look up
1472 * @flags: flags modifying lookup behaviour
1473 * @page_mask: on output, *page_mask is set according to the size of the page
1475 * @flags can have FOLL_ flags set, defined in <linux/mm.h>
1477 * Returns the mapped (struct page *), %NULL if no mapping exists, or
1478 * an error pointer if there is a mapping to something not represented
1479 * by a page descriptor (see also vm_normal_page()).
1481 struct page *follow_page_mask(struct vm_area_struct *vma,
1482 unsigned long address, unsigned int flags,
1483 unsigned int *page_mask)
1485 pgd_t *pgd;
1486 pud_t *pud;
1487 pmd_t *pmd;
1488 pte_t *ptep, pte;
1489 spinlock_t *ptl;
1490 struct page *page;
1491 struct mm_struct *mm = vma->vm_mm;
1493 *page_mask = 0;
1495 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
1496 if (!IS_ERR(page)) {
1497 BUG_ON(flags & FOLL_GET);
1498 goto out;
1501 page = NULL;
1502 pgd = pgd_offset(mm, address);
1503 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
1504 goto no_page_table;
1506 pud = pud_offset(pgd, address);
1507 if (pud_none(*pud))
1508 goto no_page_table;
1509 if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
1510 if (flags & FOLL_GET)
1511 goto out;
1512 page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE);
1513 goto out;
1515 if (unlikely(pud_bad(*pud)))
1516 goto no_page_table;
1518 pmd = pmd_offset(pud, address);
1519 if (pmd_none(*pmd))
1520 goto no_page_table;
1521 if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
1522 page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
1523 if (flags & FOLL_GET) {
1525 * Refcount on tail pages are not well-defined and
1526 * shouldn't be taken. The caller should handle a NULL
1527 * return when trying to follow tail pages.
1529 if (PageHead(page))
1530 get_page(page);
1531 else {
1532 page = NULL;
1533 goto out;
1536 goto out;
1538 if ((flags & FOLL_NUMA) && pmd_numa(*pmd))
1539 goto no_page_table;
1540 if (pmd_trans_huge(*pmd)) {
1541 if (flags & FOLL_SPLIT) {
1542 split_huge_page_pmd(vma, address, pmd);
1543 goto split_fallthrough;
1545 ptl = pmd_lock(mm, pmd);
1546 if (likely(pmd_trans_huge(*pmd))) {
1547 if (unlikely(pmd_trans_splitting(*pmd))) {
1548 spin_unlock(ptl);
1549 wait_split_huge_page(vma->anon_vma, pmd);
1550 } else {
1551 page = follow_trans_huge_pmd(vma, address,
1552 pmd, flags);
1553 spin_unlock(ptl);
1554 *page_mask = HPAGE_PMD_NR - 1;
1555 goto out;
1557 } else
1558 spin_unlock(ptl);
1559 /* fall through */
1561 split_fallthrough:
1562 if (unlikely(pmd_bad(*pmd)))
1563 goto no_page_table;
1565 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
1567 pte = *ptep;
1568 if (!pte_present(pte)) {
1569 swp_entry_t entry;
1571 * KSM's break_ksm() relies upon recognizing a ksm page
1572 * even while it is being migrated, so for that case we
1573 * need migration_entry_wait().
1575 if (likely(!(flags & FOLL_MIGRATION)))
1576 goto no_page;
1577 if (pte_none(pte) || pte_file(pte))
1578 goto no_page;
1579 entry = pte_to_swp_entry(pte);
1580 if (!is_migration_entry(entry))
1581 goto no_page;
1582 pte_unmap_unlock(ptep, ptl);
1583 migration_entry_wait(mm, pmd, address);
1584 goto split_fallthrough;
1586 if ((flags & FOLL_NUMA) && pte_numa(pte))
1587 goto no_page;
1588 if ((flags & FOLL_WRITE) && !pte_write(pte))
1589 goto unlock;
1591 page = vm_normal_page(vma, address, pte);
1592 if (unlikely(!page)) {
1593 if ((flags & FOLL_DUMP) ||
1594 !is_zero_pfn(pte_pfn(pte)))
1595 goto bad_page;
1596 page = pte_page(pte);
1599 if (flags & FOLL_GET)
1600 get_page_foll(page);
1601 if (flags & FOLL_TOUCH) {
1602 if ((flags & FOLL_WRITE) &&
1603 !pte_dirty(pte) && !PageDirty(page))
1604 set_page_dirty(page);
1606 * pte_mkyoung() would be more correct here, but atomic care
1607 * is needed to avoid losing the dirty bit: it is easier to use
1608 * mark_page_accessed().
1610 mark_page_accessed(page);
1612 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
1614 * The preliminary mapping check is mainly to avoid the
1615 * pointless overhead of lock_page on the ZERO_PAGE
1616 * which might bounce very badly if there is contention.
1618 * If the page is already locked, we don't need to
1619 * handle it now - vmscan will handle it later if and
1620 * when it attempts to reclaim the page.
1622 if (page->mapping && trylock_page(page)) {
1623 lru_add_drain(); /* push cached pages to LRU */
1625 * Because we lock page here, and migration is
1626 * blocked by the pte's page reference, and we
1627 * know the page is still mapped, we don't even
1628 * need to check for file-cache page truncation.
1630 mlock_vma_page(page);
1631 unlock_page(page);
1634 unlock:
1635 pte_unmap_unlock(ptep, ptl);
1636 out:
1637 return page;
1639 bad_page:
1640 pte_unmap_unlock(ptep, ptl);
1641 return ERR_PTR(-EFAULT);
1643 no_page:
1644 pte_unmap_unlock(ptep, ptl);
1645 if (!pte_none(pte))
1646 return page;
1648 no_page_table:
1650 * When core dumping an enormous anonymous area that nobody
1651 * has touched so far, we don't want to allocate unnecessary pages or
1652 * page tables. Return error instead of NULL to skip handle_mm_fault,
1653 * then get_dump_page() will return NULL to leave a hole in the dump.
1654 * But we can only make this optimization where a hole would surely
1655 * be zero-filled if handle_mm_fault() actually did handle it.
1657 if ((flags & FOLL_DUMP) &&
1658 (!vma->vm_ops || !vma->vm_ops->fault))
1659 return ERR_PTR(-EFAULT);
1660 return page;
1663 static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long addr)
1665 return stack_guard_page_start(vma, addr) ||
1666 stack_guard_page_end(vma, addr+PAGE_SIZE);
1670 * __get_user_pages() - pin user pages in memory
1671 * @tsk: task_struct of target task
1672 * @mm: mm_struct of target mm
1673 * @start: starting user address
1674 * @nr_pages: number of pages from start to pin
1675 * @gup_flags: flags modifying pin behaviour
1676 * @pages: array that receives pointers to the pages pinned.
1677 * Should be at least nr_pages long. Or NULL, if caller
1678 * only intends to ensure the pages are faulted in.
1679 * @vmas: array of pointers to vmas corresponding to each page.
1680 * Or NULL if the caller does not require them.
1681 * @nonblocking: whether waiting for disk IO or mmap_sem contention
1683 * Returns number of pages pinned. This may be fewer than the number
1684 * requested. If nr_pages is 0 or negative, returns 0. If no pages
1685 * were pinned, returns -errno. Each page returned must be released
1686 * with a put_page() call when it is finished with. vmas will only
1687 * remain valid while mmap_sem is held.
1689 * Must be called with mmap_sem held for read or write.
1691 * __get_user_pages walks a process's page tables and takes a reference to
1692 * each struct page that each user address corresponds to at a given
1693 * instant. That is, it takes the page that would be accessed if a user
1694 * thread accesses the given user virtual address at that instant.
1696 * This does not guarantee that the page exists in the user mappings when
1697 * __get_user_pages returns, and there may even be a completely different
1698 * page there in some cases (eg. if mmapped pagecache has been invalidated
1699 * and subsequently re faulted). However it does guarantee that the page
1700 * won't be freed completely. And mostly callers simply care that the page
1701 * contains data that was valid *at some point in time*. Typically, an IO
1702 * or similar operation cannot guarantee anything stronger anyway because
1703 * locks can't be held over the syscall boundary.
1705 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
1706 * the page is written to, set_page_dirty (or set_page_dirty_lock, as
1707 * appropriate) must be called after the page is finished with, and
1708 * before put_page is called.
1710 * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
1711 * or mmap_sem contention, and if waiting is needed to pin all pages,
1712 * *@nonblocking will be set to 0.
1714 * In most cases, get_user_pages or get_user_pages_fast should be used
1715 * instead of __get_user_pages. __get_user_pages should be used only if
1716 * you need some special @gup_flags.
1718 long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
1719 unsigned long start, unsigned long nr_pages,
1720 unsigned int gup_flags, struct page **pages,
1721 struct vm_area_struct **vmas, int *nonblocking)
1723 long i;
1724 unsigned long vm_flags;
1725 unsigned int page_mask;
1727 if (!nr_pages)
1728 return 0;
1730 VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
1733 * Require read or write permissions.
1734 * If FOLL_FORCE is set, we only require the "MAY" flags.
1736 vm_flags = (gup_flags & FOLL_WRITE) ?
1737 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
1738 vm_flags &= (gup_flags & FOLL_FORCE) ?
1739 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1742 * If FOLL_FORCE and FOLL_NUMA are both set, handle_mm_fault
1743 * would be called on PROT_NONE ranges. We must never invoke
1744 * handle_mm_fault on PROT_NONE ranges or the NUMA hinting
1745 * page faults would unprotect the PROT_NONE ranges if
1746 * _PAGE_NUMA and _PAGE_PROTNONE are sharing the same pte/pmd
1747 * bitflag. So to avoid that, don't set FOLL_NUMA if
1748 * FOLL_FORCE is set.
1750 if (!(gup_flags & FOLL_FORCE))
1751 gup_flags |= FOLL_NUMA;
1753 i = 0;
1755 do {
1756 struct vm_area_struct *vma;
1758 vma = find_extend_vma(mm, start);
1759 if (!vma && in_gate_area(mm, start)) {
1760 unsigned long pg = start & PAGE_MASK;
1761 pgd_t *pgd;
1762 pud_t *pud;
1763 pmd_t *pmd;
1764 pte_t *pte;
1766 /* user gate pages are read-only */
1767 if (gup_flags & FOLL_WRITE)
1768 return i ? : -EFAULT;
1769 if (pg > TASK_SIZE)
1770 pgd = pgd_offset_k(pg);
1771 else
1772 pgd = pgd_offset_gate(mm, pg);
1773 BUG_ON(pgd_none(*pgd));
1774 pud = pud_offset(pgd, pg);
1775 BUG_ON(pud_none(*pud));
1776 pmd = pmd_offset(pud, pg);
1777 if (pmd_none(*pmd))
1778 return i ? : -EFAULT;
1779 VM_BUG_ON(pmd_trans_huge(*pmd));
1780 pte = pte_offset_map(pmd, pg);
1781 if (pte_none(*pte)) {
1782 pte_unmap(pte);
1783 return i ? : -EFAULT;
1785 vma = get_gate_vma(mm);
1786 if (pages) {
1787 struct page *page;
1789 page = vm_normal_page(vma, start, *pte);
1790 if (!page) {
1791 if (!(gup_flags & FOLL_DUMP) &&
1792 is_zero_pfn(pte_pfn(*pte)))
1793 page = pte_page(*pte);
1794 else {
1795 pte_unmap(pte);
1796 return i ? : -EFAULT;
1799 pages[i] = page;
1800 get_page(page);
1802 pte_unmap(pte);
1803 page_mask = 0;
1804 goto next_page;
1807 if (!vma ||
1808 (vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1809 !(vm_flags & vma->vm_flags))
1810 return i ? : -EFAULT;
1812 if (is_vm_hugetlb_page(vma)) {
1813 i = follow_hugetlb_page(mm, vma, pages, vmas,
1814 &start, &nr_pages, i, gup_flags);
1815 continue;
1818 do {
1819 struct page *page;
1820 unsigned int foll_flags = gup_flags;
1821 unsigned int page_increm;
1824 * If we have a pending SIGKILL, don't keep faulting
1825 * pages and potentially allocating memory.
1827 if (unlikely(fatal_signal_pending(current)))
1828 return i ? i : -ERESTARTSYS;
1830 cond_resched();
1831 while (!(page = follow_page_mask(vma, start,
1832 foll_flags, &page_mask))) {
1833 int ret;
1834 unsigned int fault_flags = 0;
1836 /* For mlock, just skip the stack guard page. */
1837 if (foll_flags & FOLL_MLOCK) {
1838 if (stack_guard_page(vma, start))
1839 goto next_page;
1841 if (foll_flags & FOLL_WRITE)
1842 fault_flags |= FAULT_FLAG_WRITE;
1843 if (nonblocking)
1844 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
1845 if (foll_flags & FOLL_NOWAIT)
1846 fault_flags |= (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT);
1848 ret = handle_mm_fault(mm, vma, start,
1849 fault_flags);
1851 if (ret & VM_FAULT_ERROR) {
1852 if (ret & VM_FAULT_OOM)
1853 return i ? i : -ENOMEM;
1854 if (ret & (VM_FAULT_HWPOISON |
1855 VM_FAULT_HWPOISON_LARGE)) {
1856 if (i)
1857 return i;
1858 else if (gup_flags & FOLL_HWPOISON)
1859 return -EHWPOISON;
1860 else
1861 return -EFAULT;
1863 if (ret & (VM_FAULT_SIGBUS |
1864 VM_FAULT_SIGSEGV))
1865 return i ? i : -EFAULT;
1866 BUG();
1869 if (tsk) {
1870 if (ret & VM_FAULT_MAJOR)
1871 tsk->maj_flt++;
1872 else
1873 tsk->min_flt++;
1876 if (ret & VM_FAULT_RETRY) {
1877 if (nonblocking)
1878 *nonblocking = 0;
1879 return i;
1883 * The VM_FAULT_WRITE bit tells us that
1884 * do_wp_page has broken COW when necessary,
1885 * even if maybe_mkwrite decided not to set
1886 * pte_write. We can thus safely do subsequent
1887 * page lookups as if they were reads. But only
1888 * do so when looping for pte_write is futile:
1889 * in some cases userspace may also be wanting
1890 * to write to the gotten user page, which a
1891 * read fault here might prevent (a readonly
1892 * page might get reCOWed by userspace write).
1894 if ((ret & VM_FAULT_WRITE) &&
1895 !(vma->vm_flags & VM_WRITE))
1896 foll_flags &= ~FOLL_WRITE;
1898 cond_resched();
1900 if (IS_ERR(page))
1901 return i ? i : PTR_ERR(page);
1902 if (pages) {
1903 pages[i] = page;
1905 flush_anon_page(vma, page, start);
1906 flush_dcache_page(page);
1907 page_mask = 0;
1909 next_page:
1910 if (vmas) {
1911 vmas[i] = vma;
1912 page_mask = 0;
1914 page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
1915 if (page_increm > nr_pages)
1916 page_increm = nr_pages;
1917 i += page_increm;
1918 start += page_increm * PAGE_SIZE;
1919 nr_pages -= page_increm;
1920 } while (nr_pages && start < vma->vm_end);
1921 } while (nr_pages);
1922 return i;
1924 EXPORT_SYMBOL(__get_user_pages);
1927 * fixup_user_fault() - manually resolve a user page fault
1928 * @tsk: the task_struct to use for page fault accounting, or
1929 * NULL if faults are not to be recorded.
1930 * @mm: mm_struct of target mm
1931 * @address: user address
1932 * @fault_flags:flags to pass down to handle_mm_fault()
1934 * This is meant to be called in the specific scenario where for locking reasons
1935 * we try to access user memory in atomic context (within a pagefault_disable()
1936 * section), this returns -EFAULT, and we want to resolve the user fault before
1937 * trying again.
1939 * Typically this is meant to be used by the futex code.
1941 * The main difference with get_user_pages() is that this function will
1942 * unconditionally call handle_mm_fault() which will in turn perform all the
1943 * necessary SW fixup of the dirty and young bits in the PTE, while
1944 * handle_mm_fault() only guarantees to update these in the struct page.
1946 * This is important for some architectures where those bits also gate the
1947 * access permission to the page because they are maintained in software. On
1948 * such architectures, gup() will not be enough to make a subsequent access
1949 * succeed.
1951 * This should be called with the mm_sem held for read.
1953 int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
1954 unsigned long address, unsigned int fault_flags)
1956 struct vm_area_struct *vma;
1957 vm_flags_t vm_flags;
1958 int ret;
1960 vma = find_extend_vma(mm, address);
1961 if (!vma || address < vma->vm_start)
1962 return -EFAULT;
1964 vm_flags = (fault_flags & FAULT_FLAG_WRITE) ? VM_WRITE : VM_READ;
1965 if (!(vm_flags & vma->vm_flags))
1966 return -EFAULT;
1968 ret = handle_mm_fault(mm, vma, address, fault_flags);
1969 if (ret & VM_FAULT_ERROR) {
1970 if (ret & VM_FAULT_OOM)
1971 return -ENOMEM;
1972 if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
1973 return -EHWPOISON;
1974 if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
1975 return -EFAULT;
1976 BUG();
1978 if (tsk) {
1979 if (ret & VM_FAULT_MAJOR)
1980 tsk->maj_flt++;
1981 else
1982 tsk->min_flt++;
1984 return 0;
1988 * get_user_pages() - pin user pages in memory
1989 * @tsk: the task_struct to use for page fault accounting, or
1990 * NULL if faults are not to be recorded.
1991 * @mm: mm_struct of target mm
1992 * @start: starting user address
1993 * @nr_pages: number of pages from start to pin
1994 * @write: whether pages will be written to by the caller
1995 * @force: whether to force write access even if user mapping is
1996 * readonly. This will result in the page being COWed even
1997 * in MAP_SHARED mappings. You do not want this.
1998 * @pages: array that receives pointers to the pages pinned.
1999 * Should be at least nr_pages long. Or NULL, if caller
2000 * only intends to ensure the pages are faulted in.
2001 * @vmas: array of pointers to vmas corresponding to each page.
2002 * Or NULL if the caller does not require them.
2004 * Returns number of pages pinned. This may be fewer than the number
2005 * requested. If nr_pages is 0 or negative, returns 0. If no pages
2006 * were pinned, returns -errno. Each page returned must be released
2007 * with a put_page() call when it is finished with. vmas will only
2008 * remain valid while mmap_sem is held.
2010 * Must be called with mmap_sem held for read or write.
2012 * get_user_pages walks a process's page tables and takes a reference to
2013 * each struct page that each user address corresponds to at a given
2014 * instant. That is, it takes the page that would be accessed if a user
2015 * thread accesses the given user virtual address at that instant.
2017 * This does not guarantee that the page exists in the user mappings when
2018 * get_user_pages returns, and there may even be a completely different
2019 * page there in some cases (eg. if mmapped pagecache has been invalidated
2020 * and subsequently re faulted). However it does guarantee that the page
2021 * won't be freed completely. And mostly callers simply care that the page
2022 * contains data that was valid *at some point in time*. Typically, an IO
2023 * or similar operation cannot guarantee anything stronger anyway because
2024 * locks can't be held over the syscall boundary.
2026 * If write=0, the page must not be written to. If the page is written to,
2027 * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
2028 * after the page is finished with, and before put_page is called.
2030 * get_user_pages is typically used for fewer-copy IO operations, to get a
2031 * handle on the memory by some means other than accesses via the user virtual
2032 * addresses. The pages may be submitted for DMA to devices or accessed via
2033 * their kernel linear mapping (via the kmap APIs). Care should be taken to
2034 * use the correct cache flushing APIs.
2036 * See also get_user_pages_fast, for performance critical applications.
2038 long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
2039 unsigned long start, unsigned long nr_pages, int write,
2040 int force, struct page **pages, struct vm_area_struct **vmas)
2042 int flags = FOLL_TOUCH;
2044 if (pages)
2045 flags |= FOLL_GET;
2046 if (write)
2047 flags |= FOLL_WRITE;
2048 if (force)
2049 flags |= FOLL_FORCE;
2051 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
2052 NULL);
2054 EXPORT_SYMBOL(get_user_pages);
2057 * get_dump_page() - pin user page in memory while writing it to core dump
2058 * @addr: user address
2060 * Returns struct page pointer of user page pinned for dump,
2061 * to be freed afterwards by page_cache_release() or put_page().
2063 * Returns NULL on any kind of failure - a hole must then be inserted into
2064 * the corefile, to preserve alignment with its headers; and also returns
2065 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
2066 * allowing a hole to be left in the corefile to save diskspace.
2068 * Called without mmap_sem, but after all other threads have been killed.
2070 #ifdef CONFIG_ELF_CORE
2071 struct page *get_dump_page(unsigned long addr)
2073 struct vm_area_struct *vma;
2074 struct page *page;
2076 if (__get_user_pages(current, current->mm, addr, 1,
2077 FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
2078 NULL) < 1)
2079 return NULL;
2080 flush_cache_page(vma, addr, page_to_pfn(page));
2081 return page;
2083 #endif /* CONFIG_ELF_CORE */
2085 pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
2086 spinlock_t **ptl)
2088 pgd_t * pgd = pgd_offset(mm, addr);
2089 pud_t * pud = pud_alloc(mm, pgd, addr);
2090 if (pud) {
2091 pmd_t * pmd = pmd_alloc(mm, pud, addr);
2092 if (pmd) {
2093 VM_BUG_ON(pmd_trans_huge(*pmd));
2094 return pte_alloc_map_lock(mm, pmd, addr, ptl);
2097 return NULL;
2101 * This is the old fallback for page remapping.
2103 * For historical reasons, it only allows reserved pages. Only
2104 * old drivers should use this, and they needed to mark their
2105 * pages reserved for the old functions anyway.
2107 static int insert_page(struct vm_area_struct *vma, unsigned long addr,
2108 struct page *page, pgprot_t prot)
2110 struct mm_struct *mm = vma->vm_mm;
2111 int retval;
2112 pte_t *pte;
2113 spinlock_t *ptl;
2115 retval = -EINVAL;
2116 if (PageAnon(page))
2117 goto out;
2118 retval = -ENOMEM;
2119 flush_dcache_page(page);
2120 pte = get_locked_pte(mm, addr, &ptl);
2121 if (!pte)
2122 goto out;
2123 retval = -EBUSY;
2124 if (!pte_none(*pte))
2125 goto out_unlock;
2127 /* Ok, finally just insert the thing.. */
2128 get_page(page);
2129 inc_mm_counter_fast(mm, MM_FILEPAGES);
2130 page_add_file_rmap(page);
2131 set_pte_at(mm, addr, pte, mk_pte(page, prot));
2133 retval = 0;
2134 pte_unmap_unlock(pte, ptl);
2135 return retval;
2136 out_unlock:
2137 pte_unmap_unlock(pte, ptl);
2138 out:
2139 return retval;
2143 * vm_insert_page - insert single page into user vma
2144 * @vma: user vma to map to
2145 * @addr: target user address of this page
2146 * @page: source kernel page
2148 * This allows drivers to insert individual pages they've allocated
2149 * into a user vma.
2151 * The page has to be a nice clean _individual_ kernel allocation.
2152 * If you allocate a compound page, you need to have marked it as
2153 * such (__GFP_COMP), or manually just split the page up yourself
2154 * (see split_page()).
2156 * NOTE! Traditionally this was done with "remap_pfn_range()" which
2157 * took an arbitrary page protection parameter. This doesn't allow
2158 * that. Your vma protection will have to be set up correctly, which
2159 * means that if you want a shared writable mapping, you'd better
2160 * ask for a shared writable mapping!
2162 * The page does not need to be reserved.
2164 * Usually this function is called from f_op->mmap() handler
2165 * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
2166 * Caller must set VM_MIXEDMAP on vma if it wants to call this
2167 * function from other places, for example from page-fault handler.
2169 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
2170 struct page *page)
2172 if (addr < vma->vm_start || addr >= vma->vm_end)
2173 return -EFAULT;
2174 if (!page_count(page))
2175 return -EINVAL;
2176 if (!(vma->vm_flags & VM_MIXEDMAP)) {
2177 BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem));
2178 BUG_ON(vma->vm_flags & VM_PFNMAP);
2179 vma->vm_flags |= VM_MIXEDMAP;
2181 return insert_page(vma, addr, page, vma->vm_page_prot);
2183 EXPORT_SYMBOL(vm_insert_page);
2185 static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
2186 unsigned long pfn, pgprot_t prot)
2188 struct mm_struct *mm = vma->vm_mm;
2189 int retval;
2190 pte_t *pte, entry;
2191 spinlock_t *ptl;
2193 retval = -ENOMEM;
2194 pte = get_locked_pte(mm, addr, &ptl);
2195 if (!pte)
2196 goto out;
2197 retval = -EBUSY;
2198 if (!pte_none(*pte))
2199 goto out_unlock;
2201 /* Ok, finally just insert the thing.. */
2202 entry = pte_mkspecial(pfn_pte(pfn, prot));
2203 set_pte_at(mm, addr, pte, entry);
2204 update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
2206 retval = 0;
2207 out_unlock:
2208 pte_unmap_unlock(pte, ptl);
2209 out:
2210 return retval;
2214 * vm_insert_pfn - insert single pfn into user vma
2215 * @vma: user vma to map to
2216 * @addr: target user address of this page
2217 * @pfn: source kernel pfn
2219 * Similar to vm_insert_page, this allows drivers to insert individual pages
2220 * they've allocated into a user vma. Same comments apply.
2222 * This function should only be called from a vm_ops->fault handler, and
2223 * in that case the handler should return NULL.
2225 * vma cannot be a COW mapping.
2227 * As this is called only for pages that do not currently exist, we
2228 * do not need to flush old virtual caches or the TLB.
2230 int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
2231 unsigned long pfn)
2233 int ret;
2234 pgprot_t pgprot = vma->vm_page_prot;
2236 * Technically, architectures with pte_special can avoid all these
2237 * restrictions (same for remap_pfn_range). However we would like
2238 * consistency in testing and feature parity among all, so we should
2239 * try to keep these invariants in place for everybody.
2241 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
2242 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
2243 (VM_PFNMAP|VM_MIXEDMAP));
2244 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
2245 BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
2247 if (addr < vma->vm_start || addr >= vma->vm_end)
2248 return -EFAULT;
2249 if (track_pfn_insert(vma, &pgprot, pfn))
2250 return -EINVAL;
2252 ret = insert_pfn(vma, addr, pfn, pgprot);
2254 return ret;
2256 EXPORT_SYMBOL(vm_insert_pfn);
2258 int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
2259 unsigned long pfn)
2261 BUG_ON(!(vma->vm_flags & VM_MIXEDMAP));
2263 if (addr < vma->vm_start || addr >= vma->vm_end)
2264 return -EFAULT;
2267 * If we don't have pte special, then we have to use the pfn_valid()
2268 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
2269 * refcount the page if pfn_valid is true (hence insert_page rather
2270 * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP
2271 * without pte special, it would there be refcounted as a normal page.
2273 if (!HAVE_PTE_SPECIAL && pfn_valid(pfn)) {
2274 struct page *page;
2276 page = pfn_to_page(pfn);
2277 return insert_page(vma, addr, page, vma->vm_page_prot);
2279 return insert_pfn(vma, addr, pfn, vma->vm_page_prot);
2281 EXPORT_SYMBOL(vm_insert_mixed);
2284 * maps a range of physical memory into the requested pages. the old
2285 * mappings are removed. any references to nonexistent pages results
2286 * in null mappings (currently treated as "copy-on-access")
2288 static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
2289 unsigned long addr, unsigned long end,
2290 unsigned long pfn, pgprot_t prot)
2292 pte_t *pte;
2293 #ifdef CONFIG_MCST
2294 spinlock_t *uninitialized_var(ptl);
2295 #else
2296 spinlock_t *ptl;
2297 #endif
2299 pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
2300 if (!pte)
2301 return -ENOMEM;
2302 arch_enter_lazy_mmu_mode();
2303 do {
2304 BUG_ON(!pte_none(*pte));
2305 set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
2306 pfn++;
2307 } while (pte++, addr += PAGE_SIZE, addr != end);
2308 arch_leave_lazy_mmu_mode();
2309 pte_unmap_unlock(pte - 1, ptl);
2310 return 0;
2313 static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
2314 unsigned long addr, unsigned long end,
2315 unsigned long pfn, pgprot_t prot)
2317 pmd_t *pmd;
2318 unsigned long next;
2320 pfn -= addr >> PAGE_SHIFT;
2321 pmd = pmd_alloc(mm, pud, addr);
2322 if (!pmd)
2323 return -ENOMEM;
2324 VM_BUG_ON(pmd_trans_huge(*pmd));
2325 do {
2326 next = pmd_addr_end(addr, end);
2327 if (remap_pte_range(mm, pmd, addr, next,
2328 pfn + (addr >> PAGE_SHIFT), prot))
2329 return -ENOMEM;
2330 } while (pmd++, addr = next, addr != end);
2331 return 0;
2334 static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
2335 unsigned long addr, unsigned long end,
2336 unsigned long pfn, pgprot_t prot)
2338 pud_t *pud;
2339 unsigned long next;
2341 pfn -= addr >> PAGE_SHIFT;
2342 pud = pud_alloc(mm, pgd, addr);
2343 if (!pud)
2344 return -ENOMEM;
2345 do {
2346 next = pud_addr_end(addr, end);
2347 if (remap_pmd_range(mm, pud, addr, next,
2348 pfn + (addr >> PAGE_SHIFT), prot))
2349 return -ENOMEM;
2350 } while (pud++, addr = next, addr != end);
2351 return 0;
2355 * remap_pfn_range - remap kernel memory to userspace
2356 * @vma: user vma to map to
2357 * @addr: target user address to start at
2358 * @pfn: physical address of kernel memory
2359 * @size: size of map area
2360 * @prot: page protection flags for this mapping
2362 * Note: this is only safe if the mm semaphore is held when called.
2364 int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
2365 unsigned long pfn, unsigned long size, pgprot_t prot)
2367 pgd_t *pgd;
2368 unsigned long next;
2369 unsigned long end = addr + PAGE_ALIGN(size);
2370 struct mm_struct *mm = vma->vm_mm;
2371 int err;
2374 * Physically remapped pages are special. Tell the
2375 * rest of the world about it:
2376 * VM_IO tells people not to look at these pages
2377 * (accesses can have side effects).
2378 * VM_PFNMAP tells the core MM that the base pages are just
2379 * raw PFN mappings, and do not have a "struct page" associated
2380 * with them.
2381 * VM_DONTEXPAND
2382 * Disable vma merging and expanding with mremap().
2383 * VM_DONTDUMP
2384 * Omit vma from core dump, even when VM_IO turned off.
2386 * There's a horrible special case to handle copy-on-write
2387 * behaviour that some programs depend on. We mark the "original"
2388 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
2389 * See vm_normal_page() for details.
2391 if (is_cow_mapping(vma->vm_flags)) {
2392 if (addr != vma->vm_start || end != vma->vm_end)
2393 return -EINVAL;
2394 vma->vm_pgoff = pfn;
2397 err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size));
2398 if (err)
2399 return -EINVAL;
2401 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
2403 BUG_ON(addr >= end);
2404 pfn -= addr >> PAGE_SHIFT;
2405 pgd = pgd_offset(mm, addr);
2406 flush_cache_range(vma, addr, end);
2407 do {
2408 next = pgd_addr_end(addr, end);
2409 err = remap_pud_range(mm, pgd, addr, next,
2410 pfn + (addr >> PAGE_SHIFT), prot);
2411 if (err)
2412 break;
2413 } while (pgd++, addr = next, addr != end);
2415 if (err)
2416 untrack_pfn(vma, pfn, PAGE_ALIGN(size));
2418 return err;
2420 EXPORT_SYMBOL(remap_pfn_range);
2423 * vm_iomap_memory - remap memory to userspace
2424 * @vma: user vma to map to
2425 * @start: start of area
2426 * @len: size of area
2428 * This is a simplified io_remap_pfn_range() for common driver use. The
2429 * driver just needs to give us the physical memory range to be mapped,
2430 * we'll figure out the rest from the vma information.
2432 * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
2433 * whatever write-combining details or similar.
2435 int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
2437 unsigned long vm_len, pfn, pages;
2439 /* Check that the physical memory area passed in looks valid */
2440 if (start + len < start)
2441 return -EINVAL;
2443 * You *really* shouldn't map things that aren't page-aligned,
2444 * but we've historically allowed it because IO memory might
2445 * just have smaller alignment.
2447 len += start & ~PAGE_MASK;
2448 pfn = start >> PAGE_SHIFT;
2449 pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
2450 if (pfn + pages < pfn)
2451 return -EINVAL;
2453 /* We start the mapping 'vm_pgoff' pages into the area */
2454 if (vma->vm_pgoff > pages)
2455 return -EINVAL;
2456 pfn += vma->vm_pgoff;
2457 pages -= vma->vm_pgoff;
2459 /* Can we fit all of the mapping? */
2460 vm_len = vma->vm_end - vma->vm_start;
2461 if (vm_len >> PAGE_SHIFT > pages)
2462 return -EINVAL;
2464 /* Ok, let it rip */
2465 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
2467 EXPORT_SYMBOL(vm_iomap_memory);
2469 static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
2470 unsigned long addr, unsigned long end,
2471 pte_fn_t fn, void *data)
2473 pte_t *pte;
2474 int err;
2475 pgtable_t token;
2476 spinlock_t *uninitialized_var(ptl);
2478 pte = (mm == &init_mm) ?
2479 pte_alloc_kernel(pmd, addr) :
2480 pte_alloc_map_lock(mm, pmd, addr, &ptl);
2481 if (!pte)
2482 return -ENOMEM;
2484 BUG_ON(pmd_huge(*pmd));
2486 arch_enter_lazy_mmu_mode();
2488 token = pmd_pgtable(*pmd);
2490 do {
2491 err = fn(pte++, token, addr, data);
2492 if (err)
2493 break;
2494 } while (addr += PAGE_SIZE, addr != end);
2496 arch_leave_lazy_mmu_mode();
2498 if (mm != &init_mm)
2499 pte_unmap_unlock(pte-1, ptl);
2500 return err;
2503 static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
2504 unsigned long addr, unsigned long end,
2505 pte_fn_t fn, void *data)
2507 pmd_t *pmd;
2508 unsigned long next;
2509 int err;
2511 BUG_ON(pud_huge(*pud));
2513 pmd = pmd_alloc(mm, pud, addr);
2514 if (!pmd)
2515 return -ENOMEM;
2516 do {
2517 next = pmd_addr_end(addr, end);
2518 err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
2519 if (err)
2520 break;
2521 } while (pmd++, addr = next, addr != end);
2522 return err;
2525 static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
2526 unsigned long addr, unsigned long end,
2527 pte_fn_t fn, void *data)
2529 pud_t *pud;
2530 unsigned long next;
2531 int err;
2533 pud = pud_alloc(mm, pgd, addr);
2534 if (!pud)
2535 return -ENOMEM;
2536 do {
2537 next = pud_addr_end(addr, end);
2538 err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
2539 if (err)
2540 break;
2541 } while (pud++, addr = next, addr != end);
2542 return err;
2546 * Scan a region of virtual memory, filling in page tables as necessary
2547 * and calling a provided function on each leaf page table.
2549 int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2550 unsigned long size, pte_fn_t fn, void *data)
2552 pgd_t *pgd;
2553 unsigned long next;
2554 unsigned long end = addr + size;
2555 int err;
2557 BUG_ON(addr >= end);
2558 pgd = pgd_offset(mm, addr);
2559 do {
2560 next = pgd_addr_end(addr, end);
2561 err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
2562 if (err)
2563 break;
2564 } while (pgd++, addr = next, addr != end);
2566 return err;
2568 EXPORT_SYMBOL_GPL(apply_to_page_range);
2571 * handle_pte_fault chooses page fault handler according to an entry
2572 * which was read non-atomically. Before making any commitment, on
2573 * those architectures or configurations (e.g. i386 with PAE) which
2574 * might give a mix of unmatched parts, do_swap_page and do_nonlinear_fault
2575 * must check under lock before unmapping the pte and proceeding
2576 * (but do_wp_page is only called after already making such a check;
2577 * and do_anonymous_page can safely check later on).
2579 static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
2580 pte_t *page_table, pte_t orig_pte)
2582 int same = 1;
2583 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
2584 if (sizeof(pte_t) > sizeof(unsigned long)) {
2585 spinlock_t *ptl = pte_lockptr(mm, pmd);
2586 spin_lock(ptl);
2587 same = pte_same(*page_table, orig_pte);
2588 spin_unlock(ptl);
2590 #endif
2591 pte_unmap(page_table);
2592 return same;
2595 static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
2597 debug_dma_assert_idle(src);
2600 * If the source page was a PFN mapping, we don't have
2601 * a "struct page" for it. We do a best-effort copy by
2602 * just copying from the original user address. If that
2603 * fails, we just zero-fill it. Live with it.
2605 if (unlikely(!src)) {
2606 void *kaddr = kmap_atomic(dst);
2607 void __user *uaddr = (void __user *)(va & PAGE_MASK);
2610 * This really shouldn't fail, because the page is there
2611 * in the page tables. But it might just be unreadable,
2612 * in which case we just give up and fill the result with
2613 * zeroes.
2615 if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
2616 clear_page(kaddr);
2617 kunmap_atomic(kaddr);
2618 flush_dcache_page(dst);
2619 } else
2620 copy_user_highpage(dst, src, va, vma);
2624 * This routine handles present pages, when users try to write
2625 * to a shared page. It is done by copying the page to a new address
2626 * and decrementing the shared-page counter for the old page.
2628 * Note that this routine assumes that the protection checks have been
2629 * done by the caller (the low-level page fault routine in most cases).
2630 * Thus we can safely just mark it writable once we've done any necessary
2631 * COW.
2633 * We also mark the page dirty at this point even though the page will
2634 * change only once the write actually happens. This avoids a few races,
2635 * and potentially makes it more efficient.
2637 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2638 * but allow concurrent faults), with pte both mapped and locked.
2639 * We return with mmap_sem still held, but pte unmapped and unlocked.
2641 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
2642 unsigned long address, pte_t *page_table, pmd_t *pmd,
2643 spinlock_t *ptl, pte_t orig_pte)
2644 __releases(ptl)
2646 struct page *old_page, *new_page = NULL;
2647 pte_t entry;
2648 int ret = 0;
2649 int page_mkwrite = 0;
2650 struct page *dirty_page = NULL;
2651 unsigned long mmun_start = 0; /* For mmu_notifiers */
2652 unsigned long mmun_end = 0; /* For mmu_notifiers */
2654 old_page = vm_normal_page(vma, address, orig_pte);
2655 if (!old_page) {
2657 * VM_MIXEDMAP !pfn_valid() case
2659 * We should not cow pages in a shared writeable mapping.
2660 * Just mark the pages writable as we can't do any dirty
2661 * accounting on raw pfn maps.
2663 if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2664 (VM_WRITE|VM_SHARED))
2665 goto reuse;
2666 goto gotten;
2670 * Take out anonymous pages first, anonymous shared vmas are
2671 * not dirty accountable.
2673 if (PageAnon(old_page) && !PageKsm(old_page)) {
2674 if (!trylock_page(old_page)) {
2675 page_cache_get(old_page);
2676 pte_unmap_unlock(page_table, ptl);
2677 lock_page(old_page);
2678 page_table = pte_offset_map_lock(mm, pmd, address,
2679 &ptl);
2680 if (!pte_same(*page_table, orig_pte)) {
2681 unlock_page(old_page);
2682 goto unlock;
2684 page_cache_release(old_page);
2686 if (reuse_swap_page(old_page)) {
2688 * The page is all ours. Move it to our anon_vma so
2689 * the rmap code will not search our parent or siblings.
2690 * Protected against the rmap code by the page lock.
2692 page_move_anon_rmap(old_page, vma, address);
2693 unlock_page(old_page);
2694 goto reuse;
2696 unlock_page(old_page);
2697 } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2698 (VM_WRITE|VM_SHARED))) {
2700 * Only catch write-faults on shared writable pages,
2701 * read-only shared pages can get COWed by
2702 * get_user_pages(.write=1, .force=1).
2704 if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
2705 struct vm_fault vmf;
2706 int tmp;
2708 vmf.virtual_address = (void __user *)(address &
2709 PAGE_MASK);
2710 vmf.pgoff = old_page->index;
2711 vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
2712 vmf.page = old_page;
2715 * Notify the address space that the page is about to
2716 * become writable so that it can prohibit this or wait
2717 * for the page to get into an appropriate state.
2719 * We do this without the lock held, so that it can
2720 * sleep if it needs to.
2722 page_cache_get(old_page);
2723 pte_unmap_unlock(page_table, ptl);
2725 tmp = vma->vm_ops->page_mkwrite(vma, &vmf);
2726 if (unlikely(tmp &
2727 (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
2728 ret = tmp;
2729 goto unwritable_page;
2731 if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
2732 lock_page(old_page);
2733 if (!old_page->mapping) {
2734 ret = 0; /* retry the fault */
2735 unlock_page(old_page);
2736 goto unwritable_page;
2738 } else
2739 VM_BUG_ON_PAGE(!PageLocked(old_page), old_page);
2742 * Since we dropped the lock we need to revalidate
2743 * the PTE as someone else may have changed it. If
2744 * they did, we just return, as we can count on the
2745 * MMU to tell us if they didn't also make it writable.
2747 page_table = pte_offset_map_lock(mm, pmd, address,
2748 &ptl);
2749 if (!pte_same(*page_table, orig_pte)) {
2750 unlock_page(old_page);
2751 goto unlock;
2754 page_mkwrite = 1;
2756 dirty_page = old_page;
2757 get_page(dirty_page);
2759 reuse:
2761 * Clear the pages cpupid information as the existing
2762 * information potentially belongs to a now completely
2763 * unrelated process.
2765 if (old_page)
2766 page_cpupid_xchg_last(old_page, (1 << LAST_CPUPID_SHIFT) - 1);
2768 flush_cache_page(vma, address, pte_pfn(orig_pte));
2769 entry = pte_mkyoung(orig_pte);
2770 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2771 if (ptep_set_access_flags(vma, address, page_table, entry,1))
2772 update_mmu_cache(vma, address, page_table);
2773 pte_unmap_unlock(page_table, ptl);
2774 ret |= VM_FAULT_WRITE;
2776 if (!dirty_page)
2777 return ret;
2780 * Yes, Virginia, this is actually required to prevent a race
2781 * with clear_page_dirty_for_io() from clearing the page dirty
2782 * bit after it clear all dirty ptes, but before a racing
2783 * do_wp_page installs a dirty pte.
2785 * __do_fault is protected similarly.
2787 if (!page_mkwrite) {
2788 wait_on_page_locked(dirty_page);
2789 set_page_dirty_balance(dirty_page, page_mkwrite);
2790 /* file_update_time outside page_lock */
2791 if (vma->vm_file)
2792 file_update_time(vma->vm_file);
2794 put_page(dirty_page);
2795 if (page_mkwrite) {
2796 struct address_space *mapping = dirty_page->mapping;
2798 set_page_dirty(dirty_page);
2799 unlock_page(dirty_page);
2800 page_cache_release(dirty_page);
2801 if (mapping) {
2803 * Some device drivers do not set page.mapping
2804 * but still dirty their pages
2806 balance_dirty_pages_ratelimited(mapping);
2810 return ret;
2814 * Ok, we need to copy. Oh, well..
2816 page_cache_get(old_page);
2817 gotten:
2818 pte_unmap_unlock(page_table, ptl);
2820 if (unlikely(anon_vma_prepare(vma)))
2821 goto oom;
2823 if (is_zero_pfn(pte_pfn(orig_pte))) {
2824 new_page = alloc_zeroed_user_highpage_movable(vma, address);
2825 if (!new_page)
2826 goto oom;
2827 } else {
2828 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
2829 if (!new_page)
2830 goto oom;
2831 cow_user_page(new_page, old_page, address, vma);
2833 __SetPageUptodate(new_page);
2835 if (mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))
2836 goto oom_free_new;
2838 mmun_start = address & PAGE_MASK;
2839 mmun_end = mmun_start + PAGE_SIZE;
2840 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
2843 * Re-check the pte - we dropped the lock
2845 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2846 if (likely(pte_same(*page_table, orig_pte))) {
2847 if (old_page) {
2848 if (!PageAnon(old_page)) {
2849 dec_mm_counter_fast(mm, MM_FILEPAGES);
2850 inc_mm_counter_fast(mm, MM_ANONPAGES);
2852 } else
2853 inc_mm_counter_fast(mm, MM_ANONPAGES);
2854 flush_cache_page(vma, address, pte_pfn(orig_pte));
2855 entry = mk_pte(new_page, vma->vm_page_prot);
2856 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2858 * Clear the pte entry and flush it first, before updating the
2859 * pte with the new entry. This will avoid a race condition
2860 * seen in the presence of one thread doing SMC and another
2861 * thread doing COW.
2863 #if defined(CONFIG_E2K) && defined(CONFIG_MAKE_ALL_PAGES_VALID)
2864 ptep_clear_flush_as_valid(vma, address, page_table);
2865 #else
2866 ptep_clear_flush(vma, address, page_table);
2867 #endif
2868 page_add_new_anon_rmap(new_page, vma, address);
2870 * We call the notify macro here because, when using secondary
2871 * mmu page tables (such as kvm shadow page tables), we want the
2872 * new page to be mapped directly into the secondary page table.
2874 set_pte_at_notify(mm, address, page_table, entry);
2875 update_mmu_cache(vma, address, page_table);
2876 if (old_page) {
2878 * Only after switching the pte to the new page may
2879 * we remove the mapcount here. Otherwise another
2880 * process may come and find the rmap count decremented
2881 * before the pte is switched to the new page, and
2882 * "reuse" the old page writing into it while our pte
2883 * here still points into it and can be read by other
2884 * threads.
2886 * The critical issue is to order this
2887 * page_remove_rmap with the ptp_clear_flush above.
2888 * Those stores are ordered by (if nothing else,)
2889 * the barrier present in the atomic_add_negative
2890 * in page_remove_rmap.
2892 * Then the TLB flush in ptep_clear_flush ensures that
2893 * no process can access the old page before the
2894 * decremented mapcount is visible. And the old page
2895 * cannot be reused until after the decremented
2896 * mapcount is visible. So transitively, TLBs to
2897 * old page will be flushed before it can be reused.
2899 page_remove_rmap(old_page);
2902 /* Free the old page.. */
2903 new_page = old_page;
2904 ret |= VM_FAULT_WRITE;
2905 } else
2906 mem_cgroup_uncharge_page(new_page);
2908 if (new_page)
2909 page_cache_release(new_page);
2910 unlock:
2911 pte_unmap_unlock(page_table, ptl);
2912 if (mmun_end > mmun_start)
2913 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2914 if (old_page) {
2916 * Don't let another task, with possibly unlocked vma,
2917 * keep the mlocked page.
2919 if ((ret & VM_FAULT_WRITE) && (vma->vm_flags & VM_LOCKED)) {
2920 lock_page(old_page); /* LRU manipulation */
2921 munlock_vma_page(old_page);
2922 unlock_page(old_page);
2924 page_cache_release(old_page);
2926 return ret;
2927 oom_free_new:
2928 page_cache_release(new_page);
2929 oom:
2930 if (old_page)
2931 page_cache_release(old_page);
2932 return VM_FAULT_OOM;
2934 unwritable_page:
2935 page_cache_release(old_page);
2936 return ret;
2939 static void unmap_mapping_range_vma(struct vm_area_struct *vma,
2940 unsigned long start_addr, unsigned long end_addr,
2941 struct zap_details *details)
2943 #if defined(CONFIG_E2K) && defined(CONFIG_MAKE_ALL_PAGES_VALID)
2945 * vma is not destroyed here, but zap_page_range will clear
2946 * vma ptes, so keep valid bit to handle pagefaults.
2948 set_ts_flag(TS_KEEP_PAGES_VALID);
2949 #endif
2950 zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
2951 #if defined(CONFIG_E2K) && defined(CONFIG_MAKE_ALL_PAGES_VALID)
2952 clear_ts_flag(TS_KEEP_PAGES_VALID);
2953 #endif
2956 static inline void unmap_mapping_range_tree(struct rb_root *root,
2957 struct zap_details *details)
2959 struct vm_area_struct *vma;
2960 pgoff_t vba, vea, zba, zea;
2962 vma_interval_tree_foreach(vma, root,
2963 details->first_index, details->last_index) {
2965 vba = vma->vm_pgoff;
2966 vea = vba + vma_pages(vma) - 1;
2967 /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
2968 zba = details->first_index;
2969 if (zba < vba)
2970 zba = vba;
2971 zea = details->last_index;
2972 if (zea > vea)
2973 zea = vea;
2975 unmap_mapping_range_vma(vma,
2976 ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
2977 ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
2978 details);
2982 static inline void unmap_mapping_range_list(struct list_head *head,
2983 struct zap_details *details)
2985 struct vm_area_struct *vma;
2988 * In nonlinear VMAs there is no correspondence between virtual address
2989 * offset and file offset. So we must perform an exhaustive search
2990 * across *all* the pages in each nonlinear VMA, not just the pages
2991 * whose virtual address lies outside the file truncation point.
2993 list_for_each_entry(vma, head, shared.nonlinear) {
2994 details->nonlinear_vma = vma;
2995 unmap_mapping_range_vma(vma, vma->vm_start, vma->vm_end, details);
3000 * unmap_mapping_range - unmap the portion of all mmaps in the specified address_space corresponding to the specified page range in the underlying file.
3001 * @mapping: the address space containing mmaps to be unmapped.
3002 * @holebegin: byte in first page to unmap, relative to the start of
3003 * the underlying file. This will be rounded down to a PAGE_SIZE
3004 * boundary. Note that this is different from truncate_pagecache(), which
3005 * must keep the partial page. In contrast, we must get rid of
3006 * partial pages.
3007 * @holelen: size of prospective hole in bytes. This will be rounded
3008 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
3009 * end of the file.
3010 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
3011 * but 0 when invalidating pagecache, don't throw away private data.
3013 void unmap_mapping_range(struct address_space *mapping,
3014 loff_t const holebegin, loff_t const holelen, int even_cows)
3016 struct zap_details details;
3017 pgoff_t hba = holebegin >> PAGE_SHIFT;
3018 pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3020 /* Check for overflow. */
3021 if (sizeof(holelen) > sizeof(hlen)) {
3022 long long holeend =
3023 (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3024 if (holeend & ~(long long)ULONG_MAX)
3025 hlen = ULONG_MAX - hba + 1;
3028 details.check_mapping = even_cows? NULL: mapping;
3029 details.nonlinear_vma = NULL;
3030 details.first_index = hba;
3031 details.last_index = hba + hlen - 1;
3032 if (details.last_index < details.first_index)
3033 details.last_index = ULONG_MAX;
3036 mutex_lock(&mapping->i_mmap_mutex);
3037 if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap)))
3038 unmap_mapping_range_tree(&mapping->i_mmap, &details);
3039 if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
3040 unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
3041 mutex_unlock(&mapping->i_mmap_mutex);
3043 EXPORT_SYMBOL(unmap_mapping_range);
3046 * We enter with non-exclusive mmap_sem (to exclude vma changes,
3047 * but allow concurrent faults), and pte mapped but not yet locked.
3048 * We return with mmap_sem still held, but pte unmapped and unlocked.
3050 static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
3051 unsigned long address, pte_t *page_table, pmd_t *pmd,
3052 unsigned int flags, pte_t orig_pte)
3054 spinlock_t *ptl;
3055 struct page *page, *swapcache;
3056 swp_entry_t entry;
3057 pte_t pte;
3058 int locked;
3059 struct mem_cgroup *ptr;
3060 int exclusive = 0;
3061 int ret = 0;
3063 if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
3064 goto out;
3066 entry = pte_to_swp_entry(orig_pte);
3067 if (unlikely(non_swap_entry(entry))) {
3068 if (is_migration_entry(entry)) {
3069 migration_entry_wait(mm, pmd, address);
3070 } else if (is_hwpoison_entry(entry)) {
3071 ret = VM_FAULT_HWPOISON;
3072 } else {
3073 print_bad_pte(vma, address, orig_pte, NULL);
3074 ret = VM_FAULT_SIGBUS;
3076 goto out;
3078 delayacct_set_flag(DELAYACCT_PF_SWAPIN);
3079 page = lookup_swap_cache(entry);
3080 if (!page) {
3081 page = swapin_readahead(entry,
3082 GFP_HIGHUSER_MOVABLE, vma, address);
3083 if (!page) {
3085 * Back out if somebody else faulted in this pte
3086 * while we released the pte lock.
3088 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
3089 if (likely(pte_same(*page_table, orig_pte)))
3090 ret = VM_FAULT_OOM;
3091 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
3092 goto unlock;
3095 /* Had to read the page from swap area: Major fault */
3096 ret = VM_FAULT_MAJOR;
3097 count_vm_event(PGMAJFAULT);
3098 mem_cgroup_count_vm_event(mm, PGMAJFAULT);
3099 } else if (PageHWPoison(page)) {
3101 * hwpoisoned dirty swapcache pages are kept for killing
3102 * owner processes (which may be unknown at hwpoison time)
3104 ret = VM_FAULT_HWPOISON;
3105 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
3106 swapcache = page;
3107 goto out_release;
3110 swapcache = page;
3111 locked = lock_page_or_retry(page, mm, flags);
3113 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
3114 if (!locked) {
3115 ret |= VM_FAULT_RETRY;
3116 goto out_release;
3120 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
3121 * release the swapcache from under us. The page pin, and pte_same
3122 * test below, are not enough to exclude that. Even if it is still
3123 * swapcache, we need to check that the page's swap has not changed.
3125 if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val))
3126 goto out_page;
3128 page = ksm_might_need_to_copy(page, vma, address);
3129 if (unlikely(!page)) {
3130 ret = VM_FAULT_OOM;
3131 page = swapcache;
3132 goto out_page;
3135 if (mem_cgroup_try_charge_swapin(mm, page, GFP_KERNEL, &ptr)) {
3136 ret = VM_FAULT_OOM;
3137 goto out_page;
3141 * Back out if somebody else already faulted in this pte.
3143 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
3144 if (unlikely(!pte_same(*page_table, orig_pte)))
3145 goto out_nomap;
3147 if (unlikely(!PageUptodate(page))) {
3148 ret = VM_FAULT_SIGBUS;
3149 goto out_nomap;
3153 * The page isn't present yet, go ahead with the fault.
3155 * Be careful about the sequence of operations here.
3156 * To get its accounting right, reuse_swap_page() must be called
3157 * while the page is counted on swap but not yet in mapcount i.e.
3158 * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
3159 * must be called after the swap_free(), or it will never succeed.
3160 * Because delete_from_swap_page() may be called by reuse_swap_page(),
3161 * mem_cgroup_commit_charge_swapin() may not be able to find swp_entry
3162 * in page->private. In this case, a record in swap_cgroup is silently
3163 * discarded at swap_free().
3166 inc_mm_counter_fast(mm, MM_ANONPAGES);
3167 dec_mm_counter_fast(mm, MM_SWAPENTS);
3168 pte = mk_pte(page, vma->vm_page_prot);
3169 if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) {
3170 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
3171 flags &= ~FAULT_FLAG_WRITE;
3172 ret |= VM_FAULT_WRITE;
3173 exclusive = 1;
3175 flush_icache_page(vma, page);
3176 if (pte_swp_soft_dirty(orig_pte))
3177 pte = pte_mksoft_dirty(pte);
3178 set_pte_at(mm, address, page_table, pte);
3179 if (page == swapcache)
3180 do_page_add_anon_rmap(page, vma, address, exclusive);
3181 else /* ksm created a completely new copy */
3182 page_add_new_anon_rmap(page, vma, address);
3183 /* It's better to call commit-charge after rmap is established */
3184 mem_cgroup_commit_charge_swapin(page, ptr);
3186 swap_free(entry);
3187 if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
3188 try_to_free_swap(page);
3189 unlock_page(page);
3190 if (page != swapcache) {
3192 * Hold the lock to avoid the swap entry to be reused
3193 * until we take the PT lock for the pte_same() check
3194 * (to avoid false positives from pte_same). For
3195 * further safety release the lock after the swap_free
3196 * so that the swap count won't change under a
3197 * parallel locked swapcache.
3199 unlock_page(swapcache);
3200 page_cache_release(swapcache);
3203 if (flags & FAULT_FLAG_WRITE) {
3204 ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte);
3205 if (ret & VM_FAULT_ERROR)
3206 ret &= VM_FAULT_ERROR;
3207 goto out;
3210 /* No need to invalidate - it was non-present before */
3211 update_mmu_cache(vma, address, page_table);
3212 unlock:
3213 pte_unmap_unlock(page_table, ptl);
3214 out:
3215 return ret;
3216 out_nomap:
3217 mem_cgroup_cancel_charge_swapin(ptr);
3218 pte_unmap_unlock(page_table, ptl);
3219 out_page:
3220 unlock_page(page);
3221 out_release:
3222 page_cache_release(page);
3223 if (page != swapcache) {
3224 unlock_page(swapcache);
3225 page_cache_release(swapcache);
3227 return ret;
3231 * This is like a special single-page "expand_{down|up}wards()",
3232 * except we must first make sure that 'address{-|+}PAGE_SIZE'
3233 * doesn't hit another vma.
3235 static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address)
3237 address &= PAGE_MASK;
3238 if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) {
3239 struct vm_area_struct *prev = vma->vm_prev;
3242 * Is there a mapping abutting this one below?
3244 * That's only ok if it's the same stack mapping
3245 * that has gotten split..
3247 if (prev && prev->vm_end == address)
3248 return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM;
3250 return expand_downwards(vma, address - PAGE_SIZE);
3252 if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) {
3253 struct vm_area_struct *next = vma->vm_next;
3255 /* As VM_GROWSDOWN but s/below/above/ */
3256 if (next && next->vm_start == address + PAGE_SIZE)
3257 return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM;
3259 return expand_upwards(vma, address + PAGE_SIZE);
3261 return 0;
3265 * We enter with non-exclusive mmap_sem (to exclude vma changes,
3266 * but allow concurrent faults), and pte mapped but not yet locked.
3267 * We return with mmap_sem still held, but pte unmapped and unlocked.
3269 static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
3270 unsigned long address, pte_t *page_table, pmd_t *pmd,
3271 unsigned int flags)
3273 struct page *page;
3274 spinlock_t *ptl;
3275 pte_t entry;
3277 pte_unmap(page_table);
3279 /* Check if we need to add a guard page to the stack */
3280 if (check_stack_guard_page(vma, address) < 0)
3281 return VM_FAULT_SIGSEGV;
3283 /* Use the zero-page for reads */
3284 if (!(flags & FAULT_FLAG_WRITE)) {
3285 entry = pte_mkspecial(pfn_pte(my_zero_pfn(address),
3286 vma->vm_page_prot));
3287 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
3288 if (!pte_none(*page_table))
3289 goto unlock;
3290 goto setpte;
3293 /* Allocate our own private page. */
3294 if (unlikely(anon_vma_prepare(vma)))
3295 goto oom;
3296 page = alloc_zeroed_user_highpage_movable(vma, address);
3297 if (!page)
3298 goto oom;
3300 * The memory barrier inside __SetPageUptodate makes sure that
3301 * preceeding stores to the page contents become visible before
3302 * the set_pte_at() write.
3304 __SetPageUptodate(page);
3306 if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))
3307 goto oom_free_page;
3309 entry = mk_pte(page, vma->vm_page_prot);
3310 if (vma->vm_flags & VM_WRITE)
3311 entry = pte_mkwrite(pte_mkdirty(entry));
3313 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
3314 if (!pte_none(*page_table))
3315 goto release;
3317 inc_mm_counter_fast(mm, MM_ANONPAGES);
3318 page_add_new_anon_rmap(page, vma, address);
3319 setpte:
3320 set_pte_at(mm, address, page_table, entry);
3322 /* No need to invalidate - it was non-present before */
3323 update_mmu_cache(vma, address, page_table);
3324 unlock:
3325 pte_unmap_unlock(page_table, ptl);
3326 return 0;
3327 release:
3328 mem_cgroup_uncharge_page(page);
3329 page_cache_release(page);
3330 goto unlock;
3331 oom_free_page:
3332 page_cache_release(page);
3333 oom:
3334 return VM_FAULT_OOM;
3337 #if defined(CONFIG_E2K) && defined(CONFIG_SECONDARY_SPACE_SUPPORT)
3338 struct sec_space_page {
3339 unsigned long address;
3340 struct vm_area_struct *vma;
3343 static struct page *new_sec_space_page(struct page *p,
3344 unsigned long private, int **result)
3346 struct page *page;
3347 struct sec_space_page *params = (struct sec_space_page *) private;
3349 *result = NULL;
3351 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE,
3352 params->vma, params->address);
3354 return page;
3356 #endif
3359 * __do_fault() tries to create a new page mapping. It aggressively
3360 * tries to share with existing pages, but makes a separate copy if
3361 * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid
3362 * the next page fault.
3364 * As this is called only for pages that do not currently exist, we
3365 * do not need to flush old virtual caches or the TLB.
3367 * We enter with non-exclusive mmap_sem (to exclude vma changes,
3368 * but allow concurrent faults), and pte neither mapped nor locked.
3369 * We return with mmap_sem still held, but pte unmapped and unlocked.
3371 static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3372 unsigned long address, pmd_t *pmd,
3373 pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
3375 pte_t *page_table;
3376 spinlock_t *ptl;
3377 struct page *page;
3378 struct page *cow_page;
3379 pte_t entry;
3380 int anon = 0;
3381 struct page *dirty_page = NULL;
3382 struct vm_fault vmf;
3383 int ret;
3384 int page_mkwrite = 0;
3387 * If we do COW later, allocate page befor taking lock_page()
3388 * on the file cache page. This will reduce lock holding time.
3390 if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
3392 if (unlikely(anon_vma_prepare(vma)))
3393 return VM_FAULT_OOM;
3395 cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
3396 if (!cow_page)
3397 return VM_FAULT_OOM;
3399 if (mem_cgroup_newpage_charge(cow_page, mm, GFP_KERNEL)) {
3400 page_cache_release(cow_page);
3401 return VM_FAULT_OOM;
3403 } else
3404 cow_page = NULL;
3406 #if defined(CONFIG_E2K) && defined(CONFIG_SECONDARY_SPACE_SUPPORT)
3407 retry:
3408 #endif
3409 vmf.virtual_address = (void __user *)(address & PAGE_MASK);
3410 vmf.pgoff = pgoff;
3411 vmf.flags = flags;
3412 vmf.page = NULL;
3414 ret = vma->vm_ops->fault(vma, &vmf);
3415 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
3416 VM_FAULT_RETRY)))
3417 goto uncharge_out;
3419 if (unlikely(PageHWPoison(vmf.page))) {
3420 if (ret & VM_FAULT_LOCKED)
3421 unlock_page(vmf.page);
3422 ret = VM_FAULT_HWPOISON;
3423 page_cache_release(vmf.page);
3424 goto uncharge_out;
3428 * For consistency in subsequent calls, make the faulted page always
3429 * locked.
3431 if (unlikely(!(ret & VM_FAULT_LOCKED)))
3432 lock_page(vmf.page);
3433 else
3434 VM_BUG_ON_PAGE(!PageLocked(vmf.page), vmf.page);
3436 #if defined(CONFIG_E2K) && defined(CONFIG_SECONDARY_SPACE_SUPPORT)
3437 if (TASK_IS_BINCO(current) && !IS_UPT_E3S && ADDR_IN_SS(address) &&
3438 !PAGE_IN_SS(vmf.page)) {
3439 struct sec_space_page params = { address, vma };
3440 LIST_HEAD(pagelist);
3441 int err;
3443 unlock_page(vmf.page);
3445 migrate_prep();
3447 err = isolate_lru_page(vmf.page);
3449 * Either remove the duplicate refcount from
3450 * isolate_lru_page() or drop the page ref if it was
3451 * not isolated.
3453 put_page(vmf.page);
3455 if (err) {
3456 pr_info_ratelimited("Error %d on LRU isolating secondary space page\n",
3457 err);
3458 msleep(10);
3459 goto retry;
3462 list_add_tail(&vmf.page->lru, &pagelist);
3463 err = migrate_pages(&pagelist, new_sec_space_page, NULL,
3464 (unsigned long) &params,
3465 MIGRATE_SYNC, MR_SYSCALL);
3466 if (err) {
3467 putback_movable_pages(&pagelist);
3468 pr_notice("Error %d on migrating secondary space page, mapcount %d, count %d, LRU %d\n",
3469 err, page_mapcount(vmf.page),
3470 page_count(vmf.page), PageLRU(vmf.page));
3472 ret = (err == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SS;
3474 goto uncharge_out;
3477 msleep(10);
3479 goto retry;
3481 #endif
3484 * Should we do an early C-O-W break?
3486 page = vmf.page;
3487 if (flags & FAULT_FLAG_WRITE) {
3488 if (!(vma->vm_flags & VM_SHARED)) {
3489 page = cow_page;
3490 anon = 1;
3491 copy_user_highpage(page, vmf.page, address, vma);
3492 __SetPageUptodate(page);
3493 } else {
3495 * If the page will be shareable, see if the backing
3496 * address space wants to know that the page is about
3497 * to become writable
3499 if (vma->vm_ops->page_mkwrite) {
3500 int tmp;
3502 unlock_page(page);
3503 vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
3504 tmp = vma->vm_ops->page_mkwrite(vma, &vmf);
3505 if (unlikely(tmp &
3506 (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
3507 ret = tmp;
3508 goto unwritable_page;
3510 if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
3511 lock_page(page);
3512 if (!page->mapping) {
3513 ret = 0; /* retry the fault */
3514 unlock_page(page);
3515 goto unwritable_page;
3517 } else
3518 VM_BUG_ON_PAGE(!PageLocked(page), page);
3519 page_mkwrite = 1;
3525 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
3528 * This silly early PAGE_DIRTY setting removes a race
3529 * due to the bad i386 page protection. But it's valid
3530 * for other architectures too.
3532 * Note that if FAULT_FLAG_WRITE is set, we either now have
3533 * an exclusive copy of the page, or this is a shared mapping,
3534 * so we can make it writable and dirty to avoid having to
3535 * handle that later.
3537 /* Only go through if we didn't race with anybody else... */
3538 if (likely(pte_same(*page_table, orig_pte))) {
3539 flush_icache_page(vma, page);
3540 entry = mk_pte(page, vma->vm_page_prot);
3541 if (flags & FAULT_FLAG_WRITE)
3542 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
3543 else if (pte_file(orig_pte) && pte_file_soft_dirty(orig_pte))
3544 pte_mksoft_dirty(entry);
3545 if (anon) {
3546 inc_mm_counter_fast(mm, MM_ANONPAGES);
3547 page_add_new_anon_rmap(page, vma, address);
3548 } else {
3549 inc_mm_counter_fast(mm, MM_FILEPAGES);
3550 page_add_file_rmap(page);
3551 if (flags & FAULT_FLAG_WRITE) {
3552 dirty_page = page;
3553 get_page(dirty_page);
3556 set_pte_at(mm, address, page_table, entry);
3558 /* no need to invalidate: a not-present page won't be cached */
3559 update_mmu_cache(vma, address, page_table);
3560 } else {
3561 if (cow_page)
3562 mem_cgroup_uncharge_page(cow_page);
3563 if (anon)
3564 page_cache_release(page);
3565 else
3566 anon = 1; /* no anon but release faulted_page */
3569 pte_unmap_unlock(page_table, ptl);
3571 if (dirty_page) {
3572 struct address_space *mapping = page->mapping;
3573 int dirtied = 0;
3575 if (set_page_dirty(dirty_page))
3576 dirtied = 1;
3577 unlock_page(dirty_page);
3578 put_page(dirty_page);
3579 if ((dirtied || page_mkwrite) && mapping) {
3581 * Some device drivers do not set page.mapping but still
3582 * dirty their pages
3584 balance_dirty_pages_ratelimited(mapping);
3587 /* file_update_time outside page_lock */
3588 if (vma->vm_file && !page_mkwrite)
3589 file_update_time(vma->vm_file);
3590 } else {
3591 unlock_page(vmf.page);
3592 if (anon)
3593 page_cache_release(vmf.page);
3596 return ret;
3598 unwritable_page:
3599 page_cache_release(page);
3600 return ret;
3601 uncharge_out:
3602 /* fs's fault handler get error */
3603 if (cow_page) {
3604 mem_cgroup_uncharge_page(cow_page);
3605 page_cache_release(cow_page);
3607 return ret;
3610 static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3611 unsigned long address, pte_t *page_table, pmd_t *pmd,
3612 unsigned int flags, pte_t orig_pte)
3614 pgoff_t pgoff = (((address & PAGE_MASK)
3615 - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
3617 pte_unmap(page_table);
3618 return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
3622 * Fault of a previously existing named mapping. Repopulate the pte
3623 * from the encoded file_pte if possible. This enables swappable
3624 * nonlinear vmas.
3626 * We enter with non-exclusive mmap_sem (to exclude vma changes,
3627 * but allow concurrent faults), and pte mapped but not yet locked.
3628 * We return with mmap_sem still held, but pte unmapped and unlocked.
3630 static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3631 unsigned long address, pte_t *page_table, pmd_t *pmd,
3632 unsigned int flags, pte_t orig_pte)
3634 pgoff_t pgoff;
3636 flags |= FAULT_FLAG_NONLINEAR;
3638 if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
3639 return 0;
3641 if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) {
3643 * Page table corrupted: show pte and kill process.
3645 print_bad_pte(vma, address, orig_pte, NULL);
3646 return VM_FAULT_SIGBUS;
3649 pgoff = pte_to_pgoff(orig_pte);
3650 return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
3653 int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
3654 unsigned long addr, int page_nid,
3655 int *flags)
3657 get_page(page);
3659 count_vm_numa_event(NUMA_HINT_FAULTS);
3660 if (page_nid == numa_node_id()) {
3661 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
3662 *flags |= TNF_FAULT_LOCAL;
3665 return mpol_misplaced(page, vma, addr);
3668 int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
3669 unsigned long addr, pte_t pte, pte_t *ptep, pmd_t *pmd)
3671 struct page *page = NULL;
3672 spinlock_t *ptl;
3673 int page_nid = -1;
3674 int last_cpupid;
3675 int target_nid;
3676 bool migrated = false;
3677 int flags = 0;
3680 * The "pte" at this point cannot be used safely without
3681 * validation through pte_unmap_same(). It's of NUMA type but
3682 * the pfn may be screwed if the read is non atomic.
3684 * ptep_modify_prot_start is not called as this is clearing
3685 * the _PAGE_NUMA bit and it is not really expected that there
3686 * would be concurrent hardware modifications to the PTE.
3688 ptl = pte_lockptr(mm, pmd);
3689 spin_lock(ptl);
3690 if (unlikely(!pte_same(*ptep, pte))) {
3691 pte_unmap_unlock(ptep, ptl);
3692 goto out;
3695 pte = pte_mknonnuma(pte);
3696 set_pte_at(mm, addr, ptep, pte);
3697 update_mmu_cache(vma, addr, ptep);
3699 page = vm_normal_page(vma, addr, pte);
3700 if (!page) {
3701 pte_unmap_unlock(ptep, ptl);
3702 return 0;
3704 BUG_ON(is_zero_pfn(page_to_pfn(page)));
3707 * Avoid grouping on DSO/COW pages in specific and RO pages
3708 * in general, RO pages shouldn't hurt as much anyway since
3709 * they can be in shared cache state.
3711 if (!pte_write(pte))
3712 flags |= TNF_NO_GROUP;
3715 * Flag if the page is shared between multiple address spaces. This
3716 * is later used when determining whether to group tasks together
3718 if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED))
3719 flags |= TNF_SHARED;
3721 last_cpupid = page_cpupid_last(page);
3722 page_nid = page_to_nid(page);
3723 target_nid = numa_migrate_prep(page, vma, addr, page_nid, &flags);
3724 pte_unmap_unlock(ptep, ptl);
3725 if (target_nid == -1) {
3726 put_page(page);
3727 goto out;
3730 /* Migrate to the requested node */
3731 migrated = migrate_misplaced_page(page, vma, target_nid);
3732 if (migrated) {
3733 page_nid = target_nid;
3734 flags |= TNF_MIGRATED;
3737 out:
3738 if (page_nid != -1)
3739 task_numa_fault(last_cpupid, page_nid, 1, flags);
3740 return 0;
3744 * These routines also need to handle stuff like marking pages dirty
3745 * and/or accessed for architectures that don't do it in hardware (most
3746 * RISC architectures). The early dirtying is also good on the i386.
3748 * There is also a hook called "update_mmu_cache()" that architectures
3749 * with external mmu caches can use to update those (ie the Sparc or
3750 * PowerPC hashed page tables that act as extended TLBs).
3752 * We enter with non-exclusive mmap_sem (to exclude vma changes,
3753 * but allow concurrent faults), and pte mapped but not yet locked.
3754 * We return with mmap_sem still held, but pte unmapped and unlocked.
3756 static int handle_pte_fault(struct mm_struct *mm,
3757 struct vm_area_struct *vma, unsigned long address,
3758 pte_t *pte, pmd_t *pmd, unsigned int flags)
3760 pte_t entry;
3761 spinlock_t *ptl;
3763 entry = ACCESS_ONCE(*pte);
3764 if (!pte_present(entry)) {
3765 if (pte_none(entry)) {
3766 if (vma->vm_ops) {
3767 if (likely(vma->vm_ops->fault))
3768 return do_linear_fault(mm, vma, address,
3769 pte, pmd, flags, entry);
3771 return do_anonymous_page(mm, vma, address,
3772 pte, pmd, flags);
3774 if (pte_file(entry))
3775 return do_nonlinear_fault(mm, vma, address,
3776 pte, pmd, flags, entry);
3777 return do_swap_page(mm, vma, address,
3778 pte, pmd, flags, entry);
3781 if (pte_numa(entry))
3782 return do_numa_page(mm, vma, address, entry, pte, pmd);
3784 ptl = pte_lockptr(mm, pmd);
3785 spin_lock(ptl);
3786 if (unlikely(!pte_same(*pte, entry)))
3787 goto unlock;
3788 if (flags & FAULT_FLAG_WRITE) {
3789 if (!pte_write(entry))
3790 return do_wp_page(mm, vma, address,
3791 pte, pmd, ptl, entry);
3792 entry = pte_mkdirty(entry);
3794 entry = pte_mkyoung(entry);
3795 if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) {
3796 update_mmu_cache(vma, address, pte);
3797 } else {
3799 * This is needed only for protection faults but the arch code
3800 * is not yet telling us if this is a protection fault or not.
3801 * This still avoids useless tlb flushes for .text page faults
3802 * with threads.
3804 if (flags & FAULT_FLAG_WRITE)
3805 flush_tlb_fix_spurious_fault(vma, address);
3807 unlock:
3808 pte_unmap_unlock(pte, ptl);
3809 return 0;
3812 #ifdef CONFIG_PREEMPT_RT_FULL
3813 void pagefault_disable(void)
3815 migrate_disable();
3816 current->pagefault_disabled++;
3818 * make sure to have issued the store before a pagefault
3819 * can hit.
3821 barrier();
3823 EXPORT_SYMBOL(pagefault_disable);
3825 void pagefault_enable(void)
3828 * make sure to issue those last loads/stores before enabling
3829 * the pagefault handler again.
3831 barrier();
3832 current->pagefault_disabled--;
3833 migrate_enable();
3835 EXPORT_SYMBOL(pagefault_enable);
3836 #endif
3839 * By the time we get here, we already hold the mm semaphore
3841 static int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3842 unsigned long address, unsigned int flags)
3844 pgd_t *pgd;
3845 pud_t *pud;
3846 pmd_t *pmd;
3847 pte_t *pte;
3849 #ifdef CONFIG_MCST_RT
3850 if (mm->extra_vm_flags & VM_MLOCK_DONE) {
3851 /* Attempt to allocate page when VM_MLOCK_DONE set */
3852 /* for gracefully exit() */
3853 mm->extra_vm_flags &= ~VM_MLOCK_DONE;
3854 pr_err("Attempt to allocate page when VM_MLOCK_DONE"
3855 "(after mlockall())\n");
3856 return VM_FAULT_SIGBUS;
3858 #endif /* CONFIG_MCST_RT */
3860 if (unlikely(is_vm_hugetlb_page(vma)))
3861 return hugetlb_fault(mm, vma, address, flags);
3863 pgd = pgd_offset(mm, address);
3864 pud = pud_alloc(mm, pgd, address);
3865 if (!pud)
3866 return VM_FAULT_OOM;
3867 pmd = pmd_alloc(mm, pud, address);
3868 if (!pmd)
3869 return VM_FAULT_OOM;
3870 if (pmd_none(*pmd) && transparent_hugepage_enabled(vma)) {
3871 int ret = VM_FAULT_FALLBACK;
3872 if (!vma->vm_ops)
3873 ret = do_huge_pmd_anonymous_page(mm, vma, address,
3874 pmd, flags);
3875 if (!(ret & VM_FAULT_FALLBACK))
3876 return ret;
3877 } else {
3878 pmd_t orig_pmd = *pmd;
3879 int ret;
3881 barrier();
3882 if (pmd_trans_huge(orig_pmd)) {
3883 unsigned int dirty = flags & FAULT_FLAG_WRITE;
3886 * If the pmd is splitting, return and retry the
3887 * the fault. Alternative: wait until the split
3888 * is done, and goto retry.
3890 if (pmd_trans_splitting(orig_pmd))
3891 return 0;
3893 if (pmd_numa(orig_pmd))
3894 return do_huge_pmd_numa_page(mm, vma, address,
3895 orig_pmd, pmd);
3897 if (dirty && !pmd_write(orig_pmd)) {
3898 ret = do_huge_pmd_wp_page(mm, vma, address, pmd,
3899 orig_pmd);
3900 if (!(ret & VM_FAULT_FALLBACK))
3901 return ret;
3902 } else {
3903 huge_pmd_set_accessed(mm, vma, address, pmd,
3904 orig_pmd, dirty);
3905 return 0;
3911 * Use __pte_alloc instead of pte_alloc_map, because we can't
3912 * run pte_offset_map on the pmd, if an huge pmd could
3913 * materialize from under us from a different thread.
3915 if (unlikely(pmd_none(*pmd)) &&
3916 unlikely(__pte_alloc(mm, vma, pmd, address)))
3917 return VM_FAULT_OOM;
3918 /* if an huge pmd materialized from under us just retry later */
3919 if (unlikely(pmd_trans_huge(*pmd)))
3920 return 0;
3922 * A regular pmd is established and it can't morph into a huge pmd
3923 * from under us anymore at this point because we hold the mmap_sem
3924 * read mode and khugepaged takes it in write mode. So now it's
3925 * safe to run pte_offset_map().
3927 pte = pte_offset_map(pmd, address);
3929 return handle_pte_fault(mm, vma, address, pte, pmd, flags);
3932 int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3933 unsigned long address, unsigned int flags)
3935 int ret;
3937 __set_current_state(TASK_RUNNING);
3939 count_vm_event(PGFAULT);
3940 mem_cgroup_count_vm_event(mm, PGFAULT);
3942 /* do counter updates before entering really critical section. */
3943 check_sync_rss_stat(current);
3946 * Enable the memcg OOM handling for faults triggered in user
3947 * space. Kernel faults are handled more gracefully.
3949 if (flags & FAULT_FLAG_USER)
3950 mem_cgroup_oom_enable();
3952 ret = __handle_mm_fault(mm, vma, address, flags);
3954 if (flags & FAULT_FLAG_USER) {
3955 mem_cgroup_oom_disable();
3957 * The task may have entered a memcg OOM situation but
3958 * if the allocation error was handled gracefully (no
3959 * VM_FAULT_OOM), there is no need to kill anything.
3960 * Just clean up the OOM state peacefully.
3962 if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
3963 mem_cgroup_oom_synchronize(false);
3966 return ret;
3969 #ifndef __PAGETABLE_PUD_FOLDED
3971 * Allocate page upper directory.
3972 * We've already handled the fast-path in-line.
3974 int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
3976 pud_t *new = pud_alloc_one(mm, address);
3977 if (!new)
3978 return -ENOMEM;
3980 smp_wmb(); /* See comment in __pte_alloc */
3982 spin_lock(&mm->page_table_lock);
3983 if (pgd_present(*pgd)) /* Another has populated it */
3984 pud_free(mm, new);
3985 else
3986 pgd_populate(mm, pgd, new);
3987 spin_unlock(&mm->page_table_lock);
3988 return 0;
3990 #endif /* __PAGETABLE_PUD_FOLDED */
3992 #ifndef __PAGETABLE_PMD_FOLDED
3994 * Allocate page middle directory.
3995 * We've already handled the fast-path in-line.
3997 int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
3999 pmd_t *new = pmd_alloc_one(mm, address);
4000 if (!new)
4001 return -ENOMEM;
4003 smp_wmb(); /* See comment in __pte_alloc */
4005 spin_lock(&mm->page_table_lock);
4006 #ifndef __ARCH_HAS_4LEVEL_HACK
4007 if (pud_present(*pud)) /* Another has populated it */
4008 pmd_free(mm, new);
4009 else
4010 pud_populate(mm, pud, new);
4011 #else
4012 if (pgd_present(*pud)) /* Another has populated it */
4013 pmd_free(mm, new);
4014 else
4015 pgd_populate(mm, pud, new);
4016 #endif /* __ARCH_HAS_4LEVEL_HACK */
4017 spin_unlock(&mm->page_table_lock);
4018 return 0;
4020 #endif /* __PAGETABLE_PMD_FOLDED */
4022 #if !defined(__HAVE_ARCH_GATE_AREA)
4024 #if defined(AT_SYSINFO_EHDR)
4025 static struct vm_area_struct gate_vma;
4027 static int __init gate_vma_init(void)
4029 gate_vma.vm_mm = NULL;
4030 gate_vma.vm_start = FIXADDR_USER_START;
4031 gate_vma.vm_end = FIXADDR_USER_END;
4032 gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
4033 gate_vma.vm_page_prot = __P101;
4035 return 0;
4037 __initcall(gate_vma_init);
4038 #endif
4040 struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
4042 #ifdef AT_SYSINFO_EHDR
4043 return &gate_vma;
4044 #else
4045 return NULL;
4046 #endif
4049 int in_gate_area_no_mm(unsigned long addr)
4051 #ifdef AT_SYSINFO_EHDR
4052 if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
4053 return 1;
4054 #endif
4055 return 0;
4058 #endif /* __HAVE_ARCH_GATE_AREA */
4060 static int __follow_pte(struct mm_struct *mm, unsigned long address,
4061 pte_t **ptepp, spinlock_t **ptlp)
4063 pgd_t *pgd;
4064 pud_t *pud;
4065 pmd_t *pmd;
4066 pte_t *ptep;
4068 pgd = pgd_offset(mm, address);
4069 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
4070 goto out;
4072 pud = pud_offset(pgd, address);
4073 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
4074 goto out;
4076 pmd = pmd_offset(pud, address);
4077 VM_BUG_ON(pmd_trans_huge(*pmd));
4078 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
4079 goto out;
4081 /* We cannot handle huge page PFN maps. Luckily they don't exist. */
4082 if (pmd_huge(*pmd))
4083 goto out;
4085 ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
4086 if (!ptep)
4087 goto out;
4088 if (!pte_present(*ptep))
4089 goto unlock;
4090 *ptepp = ptep;
4091 return 0;
4092 unlock:
4093 pte_unmap_unlock(ptep, *ptlp);
4094 out:
4095 return -EINVAL;
4098 static inline int follow_pte(struct mm_struct *mm, unsigned long address,
4099 pte_t **ptepp, spinlock_t **ptlp)
4101 int res;
4103 /* (void) is needed to make gcc happy */
4104 (void) __cond_lock(*ptlp,
4105 !(res = __follow_pte(mm, address, ptepp, ptlp)));
4106 return res;
4110 * follow_pfn - look up PFN at a user virtual address
4111 * @vma: memory mapping
4112 * @address: user virtual address
4113 * @pfn: location to store found PFN
4115 * Only IO mappings and raw PFN mappings are allowed.
4117 * Returns zero and the pfn at @pfn on success, -ve otherwise.
4119 int follow_pfn(struct vm_area_struct *vma, unsigned long address,
4120 unsigned long *pfn)
4122 int ret = -EINVAL;
4123 spinlock_t *ptl;
4124 pte_t *ptep;
4126 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
4127 return ret;
4129 ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
4130 if (ret)
4131 return ret;
4132 *pfn = pte_pfn(*ptep);
4133 pte_unmap_unlock(ptep, ptl);
4134 return 0;
4136 EXPORT_SYMBOL(follow_pfn);
4138 #ifdef CONFIG_HAVE_IOREMAP_PROT
4139 int follow_phys(struct vm_area_struct *vma,
4140 unsigned long address, unsigned int flags,
4141 unsigned long *prot, resource_size_t *phys)
4143 int ret = -EINVAL;
4144 pte_t *ptep, pte;
4145 spinlock_t *ptl;
4147 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
4148 goto out;
4150 if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
4151 goto out;
4152 pte = *ptep;
4154 if ((flags & FOLL_WRITE) && !pte_write(pte))
4155 goto unlock;
4157 *prot = pgprot_val(pte_pgprot(pte));
4158 *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
4160 ret = 0;
4161 unlock:
4162 pte_unmap_unlock(ptep, ptl);
4163 out:
4164 return ret;
4167 int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
4168 void *buf, int len, int write)
4170 resource_size_t phys_addr;
4171 unsigned long prot = 0;
4172 void __iomem *maddr;
4173 int offset = addr & (PAGE_SIZE-1);
4175 if (follow_phys(vma, addr, write, &prot, &phys_addr))
4176 return -EINVAL;
4178 maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot);
4179 if (write)
4180 memcpy_toio(maddr + offset, buf, len);
4181 else
4182 memcpy_fromio(buf, maddr + offset, len);
4183 iounmap(maddr);
4185 return len;
4187 EXPORT_SYMBOL_GPL(generic_access_phys);
4188 #endif
4191 * Access another process' address space as given in mm. If non-NULL, use the
4192 * given task for page fault accounting.
4194 static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
4195 unsigned long addr, void *buf, int len, int write)
4197 struct vm_area_struct *vma;
4198 void *old_buf = buf;
4200 down_read(&mm->mmap_sem);
4201 /* ignore errors, just check how much was successfully transferred */
4202 while (len) {
4203 int bytes, ret, offset;
4204 void *maddr;
4205 struct page *page = NULL;
4207 ret = get_user_pages(tsk, mm, addr, 1,
4208 write, 1, &page, &vma);
4209 if (ret <= 0) {
4211 * Check if this is a VM_IO | VM_PFNMAP VMA, which
4212 * we can access using slightly different code.
4214 #ifdef CONFIG_HAVE_IOREMAP_PROT
4215 vma = find_vma(mm, addr);
4216 if (!vma || vma->vm_start > addr)
4217 break;
4218 if (vma->vm_ops && vma->vm_ops->access)
4219 ret = vma->vm_ops->access(vma, addr, buf,
4220 len, write);
4221 if (ret <= 0)
4222 #endif
4223 break;
4224 bytes = ret;
4225 } else {
4226 bytes = len;
4227 offset = addr & (PAGE_SIZE-1);
4228 if (bytes > PAGE_SIZE-offset)
4229 bytes = PAGE_SIZE-offset;
4231 maddr = kmap(page);
4232 if (write) {
4233 copy_to_user_page(vma, page, addr,
4234 maddr + offset, buf, bytes);
4235 set_page_dirty_lock(page);
4236 } else {
4237 copy_from_user_page(vma, page, addr,
4238 buf, maddr + offset, bytes);
4240 kunmap(page);
4241 page_cache_release(page);
4243 len -= bytes;
4244 buf += bytes;
4245 addr += bytes;
4247 up_read(&mm->mmap_sem);
4249 return buf - old_buf;
4253 * access_remote_vm - access another process' address space
4254 * @mm: the mm_struct of the target address space
4255 * @addr: start address to access
4256 * @buf: source or destination buffer
4257 * @len: number of bytes to transfer
4258 * @write: whether the access is a write
4260 * The caller must hold a reference on @mm.
4262 int access_remote_vm(struct mm_struct *mm, unsigned long addr,
4263 void *buf, int len, int write)
4265 return __access_remote_vm(NULL, mm, addr, buf, len, write);
4269 * Access another process' address space.
4270 * Source/target buffer must be kernel space,
4271 * Do not walk the page table directly, use get_user_pages
4273 int access_process_vm(struct task_struct *tsk, unsigned long addr,
4274 void *buf, int len, int write)
4276 struct mm_struct *mm;
4277 int ret;
4279 mm = get_task_mm(tsk);
4280 if (!mm)
4281 return 0;
4283 ret = __access_remote_vm(tsk, mm, addr, buf, len, write);
4284 mmput(mm);
4286 return ret;
4290 * Print the name of a VMA.
4292 void print_vma_addr(char *prefix, unsigned long ip)
4294 struct mm_struct *mm = current->mm;
4295 struct vm_area_struct *vma;
4298 * Do not print if we are in atomic
4299 * contexts (in exception stacks, etc.):
4301 if (preempt_count())
4302 return;
4304 down_read(&mm->mmap_sem);
4305 vma = find_vma(mm, ip);
4306 if (vma && vma->vm_file) {
4307 struct file *f = vma->vm_file;
4308 char *buf = (char *)__get_free_page(GFP_KERNEL);
4309 if (buf) {
4310 char *p;
4312 p = d_path(&f->f_path, buf, PAGE_SIZE);
4313 if (IS_ERR(p))
4314 p = "?";
4315 printk("%s%s[%lx+%lx]", prefix, kbasename(p),
4316 vma->vm_start,
4317 vma->vm_end - vma->vm_start);
4318 free_page((unsigned long)buf);
4321 up_read(&mm->mmap_sem);
4324 #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
4325 void might_fault(void)
4328 * Some code (nfs/sunrpc) uses socket ops on kernel memory while
4329 * holding the mmap_sem, this is safe because kernel memory doesn't
4330 * get paged out, therefore we'll never actually fault, and the
4331 * below annotations will generate false positives.
4333 if (segment_eq(get_fs(), KERNEL_DS))
4334 return;
4337 * it would be nicer only to annotate paths which are not under
4338 * pagefault_disable, however that requires a larger audit and
4339 * providing helpers like get_user_atomic.
4341 if (in_atomic())
4342 return;
4344 __might_sleep(__FILE__, __LINE__, 0);
4346 if (current->mm)
4347 might_lock_read(&current->mm->mmap_sem);
4349 EXPORT_SYMBOL(might_fault);
4350 #endif
4352 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
4353 static void clear_gigantic_page(struct page *page,
4354 unsigned long addr,
4355 unsigned int pages_per_huge_page)
4357 int i;
4358 struct page *p = page;
4360 might_sleep();
4361 for (i = 0; i < pages_per_huge_page;
4362 i++, p = mem_map_next(p, page, i)) {
4363 cond_resched();
4364 clear_user_highpage(p, addr + i * PAGE_SIZE);
4367 void clear_huge_page(struct page *page,
4368 unsigned long addr, unsigned int pages_per_huge_page)
4370 int i;
4372 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
4373 clear_gigantic_page(page, addr, pages_per_huge_page);
4374 return;
4377 might_sleep();
4378 for (i = 0; i < pages_per_huge_page; i++) {
4379 cond_resched();
4380 clear_user_highpage(page + i, addr + i * PAGE_SIZE);
4384 static void copy_user_gigantic_page(struct page *dst, struct page *src,
4385 unsigned long addr,
4386 struct vm_area_struct *vma,
4387 unsigned int pages_per_huge_page)
4389 int i;
4390 struct page *dst_base = dst;
4391 struct page *src_base = src;
4393 for (i = 0; i < pages_per_huge_page; ) {
4394 cond_resched();
4395 copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
4397 i++;
4398 dst = mem_map_next(dst, dst_base, i);
4399 src = mem_map_next(src, src_base, i);
4403 void copy_user_huge_page(struct page *dst, struct page *src,
4404 unsigned long addr, struct vm_area_struct *vma,
4405 unsigned int pages_per_huge_page)
4407 int i;
4409 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
4410 copy_user_gigantic_page(dst, src, addr, vma,
4411 pages_per_huge_page);
4412 return;
4415 might_sleep();
4416 for (i = 0; i < pages_per_huge_page; i++) {
4417 cond_resched();
4418 copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
4421 #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
4423 #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS
4425 static struct kmem_cache *page_ptl_cachep;
4427 void __init ptlock_cache_init(void)
4429 page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0,
4430 SLAB_PANIC, NULL);
4433 bool ptlock_alloc(struct page *page)
4435 spinlock_t *ptl;
4437 ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL);
4438 if (!ptl)
4439 return false;
4440 page->ptl = ptl;
4441 return true;
4444 void ptlock_free(struct page *page)
4446 kmem_cache_free(page_ptl_cachep, page->ptl);
4448 #endif