4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
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
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>
43 #include <linux/sched/mm.h>
44 #include <linux/sched/coredump.h>
45 #include <linux/sched/numa_balancing.h>
46 #include <linux/sched/task.h>
47 #include <linux/hugetlb.h>
48 #include <linux/mman.h>
49 #include <linux/swap.h>
50 #include <linux/highmem.h>
51 #include <linux/pagemap.h>
52 #include <linux/memremap.h>
53 #include <linux/ksm.h>
54 #include <linux/rmap.h>
55 #include <linux/export.h>
56 #include <linux/delayacct.h>
57 #include <linux/init.h>
58 #include <linux/pfn_t.h>
59 #include <linux/writeback.h>
60 #include <linux/memcontrol.h>
61 #include <linux/mmu_notifier.h>
62 #include <linux/kallsyms.h>
63 #include <linux/swapops.h>
64 #include <linux/elf.h>
65 #include <linux/gfp.h>
66 #include <linux/migrate.h>
67 #include <linux/string.h>
68 #include <linux/dma-debug.h>
69 #include <linux/debugfs.h>
70 #include <linux/userfaultfd_k.h>
71 #include <linux/dax.h>
72 #include <linux/oom.h>
75 #include <asm/mmu_context.h>
76 #include <asm/pgalloc.h>
77 #include <linux/uaccess.h>
79 #include <asm/tlbflush.h>
80 #include <asm/pgtable.h>
84 #if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST)
85 #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
88 #ifndef CONFIG_NEED_MULTIPLE_NODES
89 /* use the per-pgdat data instead for discontigmem - mbligh */
90 unsigned long max_mapnr
;
91 EXPORT_SYMBOL(max_mapnr
);
94 EXPORT_SYMBOL(mem_map
);
98 * A number of key systems in x86 including ioremap() rely on the assumption
99 * that high_memory defines the upper bound on direct map memory, then end
100 * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
101 * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
105 EXPORT_SYMBOL(high_memory
);
108 * Randomize the address space (stacks, mmaps, brk, etc.).
110 * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
111 * as ancient (libc5 based) binaries can segfault. )
113 int randomize_va_space __read_mostly
=
114 #ifdef CONFIG_COMPAT_BRK
120 static int __init
disable_randmaps(char *s
)
122 randomize_va_space
= 0;
125 __setup("norandmaps", disable_randmaps
);
127 unsigned long zero_pfn __read_mostly
;
128 EXPORT_SYMBOL(zero_pfn
);
130 unsigned long highest_memmap_pfn __read_mostly
;
133 * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
135 static int __init
init_zero_pfn(void)
137 zero_pfn
= page_to_pfn(ZERO_PAGE(0));
140 core_initcall(init_zero_pfn
);
143 #if defined(SPLIT_RSS_COUNTING)
145 void sync_mm_rss(struct mm_struct
*mm
)
149 for (i
= 0; i
< NR_MM_COUNTERS
; i
++) {
150 if (current
->rss_stat
.count
[i
]) {
151 add_mm_counter(mm
, i
, current
->rss_stat
.count
[i
]);
152 current
->rss_stat
.count
[i
] = 0;
155 current
->rss_stat
.events
= 0;
158 static void add_mm_counter_fast(struct mm_struct
*mm
, int member
, int val
)
160 struct task_struct
*task
= current
;
162 if (likely(task
->mm
== mm
))
163 task
->rss_stat
.count
[member
] += val
;
165 add_mm_counter(mm
, member
, val
);
167 #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
168 #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
170 /* sync counter once per 64 page faults */
171 #define TASK_RSS_EVENTS_THRESH (64)
172 static void check_sync_rss_stat(struct task_struct
*task
)
174 if (unlikely(task
!= current
))
176 if (unlikely(task
->rss_stat
.events
++ > TASK_RSS_EVENTS_THRESH
))
177 sync_mm_rss(task
->mm
);
179 #else /* SPLIT_RSS_COUNTING */
181 #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
182 #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
184 static void check_sync_rss_stat(struct task_struct
*task
)
188 #endif /* SPLIT_RSS_COUNTING */
190 #ifdef HAVE_GENERIC_MMU_GATHER
192 static bool tlb_next_batch(struct mmu_gather
*tlb
)
194 struct mmu_gather_batch
*batch
;
198 tlb
->active
= batch
->next
;
202 if (tlb
->batch_count
== MAX_GATHER_BATCH_COUNT
)
205 batch
= (void *)__get_free_pages(GFP_NOWAIT
| __GFP_NOWARN
, 0);
212 batch
->max
= MAX_GATHER_BATCH
;
214 tlb
->active
->next
= batch
;
220 void arch_tlb_gather_mmu(struct mmu_gather
*tlb
, struct mm_struct
*mm
,
221 unsigned long start
, unsigned long end
)
225 /* Is it from 0 to ~0? */
226 tlb
->fullmm
= !(start
| (end
+1));
227 tlb
->need_flush_all
= 0;
228 tlb
->local
.next
= NULL
;
230 tlb
->local
.max
= ARRAY_SIZE(tlb
->__pages
);
231 tlb
->active
= &tlb
->local
;
232 tlb
->batch_count
= 0;
234 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
239 __tlb_reset_range(tlb
);
242 static void tlb_flush_mmu_tlbonly(struct mmu_gather
*tlb
)
248 mmu_notifier_invalidate_range(tlb
->mm
, tlb
->start
, tlb
->end
);
249 __tlb_reset_range(tlb
);
252 static void tlb_flush_mmu_free(struct mmu_gather
*tlb
)
254 struct mmu_gather_batch
*batch
;
256 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
257 tlb_table_flush(tlb
);
259 for (batch
= &tlb
->local
; batch
&& batch
->nr
; batch
= batch
->next
) {
260 free_pages_and_swap_cache(batch
->pages
, batch
->nr
);
263 tlb
->active
= &tlb
->local
;
266 void tlb_flush_mmu(struct mmu_gather
*tlb
)
268 tlb_flush_mmu_tlbonly(tlb
);
269 tlb_flush_mmu_free(tlb
);
273 * Called at the end of the shootdown operation to free up any resources
274 * that were required.
276 void arch_tlb_finish_mmu(struct mmu_gather
*tlb
,
277 unsigned long start
, unsigned long end
, bool force
)
279 struct mmu_gather_batch
*batch
, *next
;
282 __tlb_adjust_range(tlb
, start
, end
- start
);
286 /* keep the page table cache within bounds */
289 for (batch
= tlb
->local
.next
; batch
; batch
= next
) {
291 free_pages((unsigned long)batch
, 0);
293 tlb
->local
.next
= NULL
;
297 * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
298 * handling the additional races in SMP caused by other CPUs caching valid
299 * mappings in their TLBs. Returns the number of free page slots left.
300 * When out of page slots we must call tlb_flush_mmu().
301 *returns true if the caller should flush.
303 bool __tlb_remove_page_size(struct mmu_gather
*tlb
, struct page
*page
, int page_size
)
305 struct mmu_gather_batch
*batch
;
307 VM_BUG_ON(!tlb
->end
);
308 VM_WARN_ON(tlb
->page_size
!= page_size
);
312 * Add the page and check if we are full. If so
315 batch
->pages
[batch
->nr
++] = page
;
316 if (batch
->nr
== batch
->max
) {
317 if (!tlb_next_batch(tlb
))
321 VM_BUG_ON_PAGE(batch
->nr
> batch
->max
, page
);
326 #endif /* HAVE_GENERIC_MMU_GATHER */
328 #ifdef CONFIG_HAVE_RCU_TABLE_FREE
331 * See the comment near struct mmu_table_batch.
334 static void tlb_remove_table_smp_sync(void *arg
)
336 /* Simply deliver the interrupt */
339 static void tlb_remove_table_one(void *table
)
342 * This isn't an RCU grace period and hence the page-tables cannot be
343 * assumed to be actually RCU-freed.
345 * It is however sufficient for software page-table walkers that rely on
346 * IRQ disabling. See the comment near struct mmu_table_batch.
348 smp_call_function(tlb_remove_table_smp_sync
, NULL
, 1);
349 __tlb_remove_table(table
);
352 static void tlb_remove_table_rcu(struct rcu_head
*head
)
354 struct mmu_table_batch
*batch
;
357 batch
= container_of(head
, struct mmu_table_batch
, rcu
);
359 for (i
= 0; i
< batch
->nr
; i
++)
360 __tlb_remove_table(batch
->tables
[i
]);
362 free_page((unsigned long)batch
);
365 void tlb_table_flush(struct mmu_gather
*tlb
)
367 struct mmu_table_batch
**batch
= &tlb
->batch
;
370 call_rcu_sched(&(*batch
)->rcu
, tlb_remove_table_rcu
);
375 void tlb_remove_table(struct mmu_gather
*tlb
, void *table
)
377 struct mmu_table_batch
**batch
= &tlb
->batch
;
379 if (*batch
== NULL
) {
380 *batch
= (struct mmu_table_batch
*)__get_free_page(GFP_NOWAIT
| __GFP_NOWARN
);
381 if (*batch
== NULL
) {
382 tlb_remove_table_one(table
);
387 (*batch
)->tables
[(*batch
)->nr
++] = table
;
388 if ((*batch
)->nr
== MAX_TABLE_BATCH
)
389 tlb_table_flush(tlb
);
392 #endif /* CONFIG_HAVE_RCU_TABLE_FREE */
395 * Called to initialize an (on-stack) mmu_gather structure for page-table
396 * tear-down from @mm. The @fullmm argument is used when @mm is without
397 * users and we're going to destroy the full address space (exit/execve).
399 void tlb_gather_mmu(struct mmu_gather
*tlb
, struct mm_struct
*mm
,
400 unsigned long start
, unsigned long end
)
402 arch_tlb_gather_mmu(tlb
, mm
, start
, end
);
403 inc_tlb_flush_pending(tlb
->mm
);
406 void tlb_finish_mmu(struct mmu_gather
*tlb
,
407 unsigned long start
, unsigned long end
)
410 * If there are parallel threads are doing PTE changes on same range
411 * under non-exclusive lock(e.g., mmap_sem read-side) but defer TLB
412 * flush by batching, a thread has stable TLB entry can fail to flush
413 * the TLB by observing pte_none|!pte_dirty, for example so flush TLB
414 * forcefully if we detect parallel PTE batching threads.
416 bool force
= mm_tlb_flush_nested(tlb
->mm
);
418 arch_tlb_finish_mmu(tlb
, start
, end
, force
);
419 dec_tlb_flush_pending(tlb
->mm
);
423 * Note: this doesn't free the actual pages themselves. That
424 * has been handled earlier when unmapping all the memory regions.
426 static void free_pte_range(struct mmu_gather
*tlb
, pmd_t
*pmd
,
429 pgtable_t token
= pmd_pgtable(*pmd
);
431 pte_free_tlb(tlb
, token
, addr
);
432 mm_dec_nr_ptes(tlb
->mm
);
435 static inline void free_pmd_range(struct mmu_gather
*tlb
, pud_t
*pud
,
436 unsigned long addr
, unsigned long end
,
437 unsigned long floor
, unsigned long ceiling
)
444 pmd
= pmd_offset(pud
, addr
);
446 next
= pmd_addr_end(addr
, end
);
447 if (pmd_none_or_clear_bad(pmd
))
449 free_pte_range(tlb
, pmd
, addr
);
450 } while (pmd
++, addr
= next
, addr
!= end
);
460 if (end
- 1 > ceiling
- 1)
463 pmd
= pmd_offset(pud
, start
);
465 pmd_free_tlb(tlb
, pmd
, start
);
466 mm_dec_nr_pmds(tlb
->mm
);
469 static inline void free_pud_range(struct mmu_gather
*tlb
, p4d_t
*p4d
,
470 unsigned long addr
, unsigned long end
,
471 unsigned long floor
, unsigned long ceiling
)
478 pud
= pud_offset(p4d
, addr
);
480 next
= pud_addr_end(addr
, end
);
481 if (pud_none_or_clear_bad(pud
))
483 free_pmd_range(tlb
, pud
, addr
, next
, floor
, ceiling
);
484 } while (pud
++, addr
= next
, addr
!= end
);
494 if (end
- 1 > ceiling
- 1)
497 pud
= pud_offset(p4d
, start
);
499 pud_free_tlb(tlb
, pud
, start
);
500 mm_dec_nr_puds(tlb
->mm
);
503 static inline void free_p4d_range(struct mmu_gather
*tlb
, pgd_t
*pgd
,
504 unsigned long addr
, unsigned long end
,
505 unsigned long floor
, unsigned long ceiling
)
512 p4d
= p4d_offset(pgd
, addr
);
514 next
= p4d_addr_end(addr
, end
);
515 if (p4d_none_or_clear_bad(p4d
))
517 free_pud_range(tlb
, p4d
, addr
, next
, floor
, ceiling
);
518 } while (p4d
++, addr
= next
, addr
!= end
);
524 ceiling
&= PGDIR_MASK
;
528 if (end
- 1 > ceiling
- 1)
531 p4d
= p4d_offset(pgd
, start
);
533 p4d_free_tlb(tlb
, p4d
, start
);
537 * This function frees user-level page tables of a process.
539 void free_pgd_range(struct mmu_gather
*tlb
,
540 unsigned long addr
, unsigned long end
,
541 unsigned long floor
, unsigned long ceiling
)
547 * The next few lines have given us lots of grief...
549 * Why are we testing PMD* at this top level? Because often
550 * there will be no work to do at all, and we'd prefer not to
551 * go all the way down to the bottom just to discover that.
553 * Why all these "- 1"s? Because 0 represents both the bottom
554 * of the address space and the top of it (using -1 for the
555 * top wouldn't help much: the masks would do the wrong thing).
556 * The rule is that addr 0 and floor 0 refer to the bottom of
557 * the address space, but end 0 and ceiling 0 refer to the top
558 * Comparisons need to use "end - 1" and "ceiling - 1" (though
559 * that end 0 case should be mythical).
561 * Wherever addr is brought up or ceiling brought down, we must
562 * be careful to reject "the opposite 0" before it confuses the
563 * subsequent tests. But what about where end is brought down
564 * by PMD_SIZE below? no, end can't go down to 0 there.
566 * Whereas we round start (addr) and ceiling down, by different
567 * masks at different levels, in order to test whether a table
568 * now has no other vmas using it, so can be freed, we don't
569 * bother to round floor or end up - the tests don't need that.
583 if (end
- 1 > ceiling
- 1)
588 * We add page table cache pages with PAGE_SIZE,
589 * (see pte_free_tlb()), flush the tlb if we need
591 tlb_remove_check_page_size_change(tlb
, PAGE_SIZE
);
592 pgd
= pgd_offset(tlb
->mm
, addr
);
594 next
= pgd_addr_end(addr
, end
);
595 if (pgd_none_or_clear_bad(pgd
))
597 free_p4d_range(tlb
, pgd
, addr
, next
, floor
, ceiling
);
598 } while (pgd
++, addr
= next
, addr
!= end
);
601 void free_pgtables(struct mmu_gather
*tlb
, struct vm_area_struct
*vma
,
602 unsigned long floor
, unsigned long ceiling
)
605 struct vm_area_struct
*next
= vma
->vm_next
;
606 unsigned long addr
= vma
->vm_start
;
609 * Hide vma from rmap and truncate_pagecache before freeing
612 unlink_anon_vmas(vma
);
613 unlink_file_vma(vma
);
615 if (is_vm_hugetlb_page(vma
)) {
616 hugetlb_free_pgd_range(tlb
, addr
, vma
->vm_end
,
617 floor
, next
? next
->vm_start
: ceiling
);
620 * Optimization: gather nearby vmas into one call down
622 while (next
&& next
->vm_start
<= vma
->vm_end
+ PMD_SIZE
623 && !is_vm_hugetlb_page(next
)) {
626 unlink_anon_vmas(vma
);
627 unlink_file_vma(vma
);
629 free_pgd_range(tlb
, addr
, vma
->vm_end
,
630 floor
, next
? next
->vm_start
: ceiling
);
636 int __pte_alloc(struct mm_struct
*mm
, pmd_t
*pmd
, unsigned long address
)
639 pgtable_t
new = pte_alloc_one(mm
, address
);
644 * Ensure all pte setup (eg. pte page lock and page clearing) are
645 * visible before the pte is made visible to other CPUs by being
646 * put into page tables.
648 * The other side of the story is the pointer chasing in the page
649 * table walking code (when walking the page table without locking;
650 * ie. most of the time). Fortunately, these data accesses consist
651 * of a chain of data-dependent loads, meaning most CPUs (alpha
652 * being the notable exception) will already guarantee loads are
653 * seen in-order. See the alpha page table accessors for the
654 * smp_read_barrier_depends() barriers in page table walking code.
656 smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
658 ptl
= pmd_lock(mm
, pmd
);
659 if (likely(pmd_none(*pmd
))) { /* Has another populated it ? */
661 pmd_populate(mm
, pmd
, new);
670 int __pte_alloc_kernel(pmd_t
*pmd
, unsigned long address
)
672 pte_t
*new = pte_alloc_one_kernel(&init_mm
, address
);
676 smp_wmb(); /* See comment in __pte_alloc */
678 spin_lock(&init_mm
.page_table_lock
);
679 if (likely(pmd_none(*pmd
))) { /* Has another populated it ? */
680 pmd_populate_kernel(&init_mm
, pmd
, new);
683 spin_unlock(&init_mm
.page_table_lock
);
685 pte_free_kernel(&init_mm
, new);
689 static inline void init_rss_vec(int *rss
)
691 memset(rss
, 0, sizeof(int) * NR_MM_COUNTERS
);
694 static inline void add_mm_rss_vec(struct mm_struct
*mm
, int *rss
)
698 if (current
->mm
== mm
)
700 for (i
= 0; i
< NR_MM_COUNTERS
; i
++)
702 add_mm_counter(mm
, i
, rss
[i
]);
706 * This function is called to print an error when a bad pte
707 * is found. For example, we might have a PFN-mapped pte in
708 * a region that doesn't allow it.
710 * The calling function must still handle the error.
712 static void print_bad_pte(struct vm_area_struct
*vma
, unsigned long addr
,
713 pte_t pte
, struct page
*page
)
715 pgd_t
*pgd
= pgd_offset(vma
->vm_mm
, addr
);
716 p4d_t
*p4d
= p4d_offset(pgd
, addr
);
717 pud_t
*pud
= pud_offset(p4d
, addr
);
718 pmd_t
*pmd
= pmd_offset(pud
, addr
);
719 struct address_space
*mapping
;
721 static unsigned long resume
;
722 static unsigned long nr_shown
;
723 static unsigned long nr_unshown
;
726 * Allow a burst of 60 reports, then keep quiet for that minute;
727 * or allow a steady drip of one report per second.
729 if (nr_shown
== 60) {
730 if (time_before(jiffies
, resume
)) {
735 pr_alert("BUG: Bad page map: %lu messages suppressed\n",
742 resume
= jiffies
+ 60 * HZ
;
744 mapping
= vma
->vm_file
? vma
->vm_file
->f_mapping
: NULL
;
745 index
= linear_page_index(vma
, addr
);
747 pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n",
749 (long long)pte_val(pte
), (long long)pmd_val(*pmd
));
751 dump_page(page
, "bad pte");
752 pr_alert("addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n",
753 (void *)addr
, vma
->vm_flags
, vma
->anon_vma
, mapping
, index
);
755 * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y
757 pr_alert("file:%pD fault:%pf mmap:%pf readpage:%pf\n",
759 vma
->vm_ops
? vma
->vm_ops
->fault
: NULL
,
760 vma
->vm_file
? vma
->vm_file
->f_op
->mmap
: NULL
,
761 mapping
? mapping
->a_ops
->readpage
: NULL
);
763 add_taint(TAINT_BAD_PAGE
, LOCKDEP_NOW_UNRELIABLE
);
767 * vm_normal_page -- This function gets the "struct page" associated with a pte.
769 * "Special" mappings do not wish to be associated with a "struct page" (either
770 * it doesn't exist, or it exists but they don't want to touch it). In this
771 * case, NULL is returned here. "Normal" mappings do have a struct page.
773 * There are 2 broad cases. Firstly, an architecture may define a pte_special()
774 * pte bit, in which case this function is trivial. Secondly, an architecture
775 * may not have a spare pte bit, which requires a more complicated scheme,
778 * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
779 * special mapping (even if there are underlying and valid "struct pages").
780 * COWed pages of a VM_PFNMAP are always normal.
782 * The way we recognize COWed pages within VM_PFNMAP mappings is through the
783 * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
784 * set, and the vm_pgoff will point to the first PFN mapped: thus every special
785 * mapping will always honor the rule
787 * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
789 * And for normal mappings this is false.
791 * This restricts such mappings to be a linear translation from virtual address
792 * to pfn. To get around this restriction, we allow arbitrary mappings so long
793 * as the vma is not a COW mapping; in that case, we know that all ptes are
794 * special (because none can have been COWed).
797 * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
799 * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
800 * page" backing, however the difference is that _all_ pages with a struct
801 * page (that is, those where pfn_valid is true) are refcounted and considered
802 * normal pages by the VM. The disadvantage is that pages are refcounted
803 * (which can be slower and simply not an option for some PFNMAP users). The
804 * advantage is that we don't have to follow the strict linearity rule of
805 * PFNMAP mappings in order to support COWable mappings.
808 #ifdef __HAVE_ARCH_PTE_SPECIAL
809 # define HAVE_PTE_SPECIAL 1
811 # define HAVE_PTE_SPECIAL 0
813 struct page
*_vm_normal_page(struct vm_area_struct
*vma
, unsigned long addr
,
814 pte_t pte
, bool with_public_device
)
816 unsigned long pfn
= pte_pfn(pte
);
818 if (HAVE_PTE_SPECIAL
) {
819 if (likely(!pte_special(pte
)))
821 if (vma
->vm_ops
&& vma
->vm_ops
->find_special_page
)
822 return vma
->vm_ops
->find_special_page(vma
, addr
);
823 if (vma
->vm_flags
& (VM_PFNMAP
| VM_MIXEDMAP
))
825 if (is_zero_pfn(pfn
))
829 * Device public pages are special pages (they are ZONE_DEVICE
830 * pages but different from persistent memory). They behave
831 * allmost like normal pages. The difference is that they are
832 * not on the lru and thus should never be involve with any-
833 * thing that involve lru manipulation (mlock, numa balancing,
836 * This is why we still want to return NULL for such page from
837 * vm_normal_page() so that we do not have to special case all
838 * call site of vm_normal_page().
840 if (likely(pfn
<= highest_memmap_pfn
)) {
841 struct page
*page
= pfn_to_page(pfn
);
843 if (is_device_public_page(page
)) {
844 if (with_public_device
)
849 print_bad_pte(vma
, addr
, pte
, NULL
);
853 /* !HAVE_PTE_SPECIAL case follows: */
855 if (unlikely(vma
->vm_flags
& (VM_PFNMAP
|VM_MIXEDMAP
))) {
856 if (vma
->vm_flags
& VM_MIXEDMAP
) {
862 off
= (addr
- vma
->vm_start
) >> PAGE_SHIFT
;
863 if (pfn
== vma
->vm_pgoff
+ off
)
865 if (!is_cow_mapping(vma
->vm_flags
))
870 if (is_zero_pfn(pfn
))
873 if (unlikely(pfn
> highest_memmap_pfn
)) {
874 print_bad_pte(vma
, addr
, pte
, NULL
);
879 * NOTE! We still have PageReserved() pages in the page tables.
880 * eg. VDSO mappings can cause them to exist.
883 return pfn_to_page(pfn
);
886 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
887 struct page
*vm_normal_page_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
890 unsigned long pfn
= pmd_pfn(pmd
);
893 * There is no pmd_special() but there may be special pmds, e.g.
894 * in a direct-access (dax) mapping, so let's just replicate the
895 * !HAVE_PTE_SPECIAL case from vm_normal_page() here.
897 if (unlikely(vma
->vm_flags
& (VM_PFNMAP
|VM_MIXEDMAP
))) {
898 if (vma
->vm_flags
& VM_MIXEDMAP
) {
904 off
= (addr
- vma
->vm_start
) >> PAGE_SHIFT
;
905 if (pfn
== vma
->vm_pgoff
+ off
)
907 if (!is_cow_mapping(vma
->vm_flags
))
912 if (is_zero_pfn(pfn
))
914 if (unlikely(pfn
> highest_memmap_pfn
))
918 * NOTE! We still have PageReserved() pages in the page tables.
919 * eg. VDSO mappings can cause them to exist.
922 return pfn_to_page(pfn
);
927 * copy one vm_area from one task to the other. Assumes the page tables
928 * already present in the new task to be cleared in the whole range
929 * covered by this vma.
932 static inline unsigned long
933 copy_one_pte(struct mm_struct
*dst_mm
, struct mm_struct
*src_mm
,
934 pte_t
*dst_pte
, pte_t
*src_pte
, struct vm_area_struct
*vma
,
935 unsigned long addr
, int *rss
)
937 unsigned long vm_flags
= vma
->vm_flags
;
938 pte_t pte
= *src_pte
;
941 /* pte contains position in swap or file, so copy. */
942 if (unlikely(!pte_present(pte
))) {
943 swp_entry_t entry
= pte_to_swp_entry(pte
);
945 if (likely(!non_swap_entry(entry
))) {
946 if (swap_duplicate(entry
) < 0)
949 /* make sure dst_mm is on swapoff's mmlist. */
950 if (unlikely(list_empty(&dst_mm
->mmlist
))) {
951 spin_lock(&mmlist_lock
);
952 if (list_empty(&dst_mm
->mmlist
))
953 list_add(&dst_mm
->mmlist
,
955 spin_unlock(&mmlist_lock
);
958 } else if (is_migration_entry(entry
)) {
959 page
= migration_entry_to_page(entry
);
961 rss
[mm_counter(page
)]++;
963 if (is_write_migration_entry(entry
) &&
964 is_cow_mapping(vm_flags
)) {
966 * COW mappings require pages in both
967 * parent and child to be set to read.
969 make_migration_entry_read(&entry
);
970 pte
= swp_entry_to_pte(entry
);
971 if (pte_swp_soft_dirty(*src_pte
))
972 pte
= pte_swp_mksoft_dirty(pte
);
973 set_pte_at(src_mm
, addr
, src_pte
, pte
);
975 } else if (is_device_private_entry(entry
)) {
976 page
= device_private_entry_to_page(entry
);
979 * Update rss count even for unaddressable pages, as
980 * they should treated just like normal pages in this
983 * We will likely want to have some new rss counters
984 * for unaddressable pages, at some point. But for now
985 * keep things as they are.
988 rss
[mm_counter(page
)]++;
989 page_dup_rmap(page
, false);
992 * We do not preserve soft-dirty information, because so
993 * far, checkpoint/restore is the only feature that
994 * requires that. And checkpoint/restore does not work
995 * when a device driver is involved (you cannot easily
996 * save and restore device driver state).
998 if (is_write_device_private_entry(entry
) &&
999 is_cow_mapping(vm_flags
)) {
1000 make_device_private_entry_read(&entry
);
1001 pte
= swp_entry_to_pte(entry
);
1002 set_pte_at(src_mm
, addr
, src_pte
, pte
);
1009 * If it's a COW mapping, write protect it both
1010 * in the parent and the child
1012 if (is_cow_mapping(vm_flags
)) {
1013 ptep_set_wrprotect(src_mm
, addr
, src_pte
);
1014 pte
= pte_wrprotect(pte
);
1018 * If it's a shared mapping, mark it clean in
1021 if (vm_flags
& VM_SHARED
)
1022 pte
= pte_mkclean(pte
);
1023 pte
= pte_mkold(pte
);
1025 page
= vm_normal_page(vma
, addr
, pte
);
1028 page_dup_rmap(page
, false);
1029 rss
[mm_counter(page
)]++;
1030 } else if (pte_devmap(pte
)) {
1031 page
= pte_page(pte
);
1034 * Cache coherent device memory behave like regular page and
1035 * not like persistent memory page. For more informations see
1036 * MEMORY_DEVICE_CACHE_COHERENT in memory_hotplug.h
1038 if (is_device_public_page(page
)) {
1040 page_dup_rmap(page
, false);
1041 rss
[mm_counter(page
)]++;
1046 set_pte_at(dst_mm
, addr
, dst_pte
, pte
);
1050 static int copy_pte_range(struct mm_struct
*dst_mm
, struct mm_struct
*src_mm
,
1051 pmd_t
*dst_pmd
, pmd_t
*src_pmd
, struct vm_area_struct
*vma
,
1052 unsigned long addr
, unsigned long end
)
1054 pte_t
*orig_src_pte
, *orig_dst_pte
;
1055 pte_t
*src_pte
, *dst_pte
;
1056 spinlock_t
*src_ptl
, *dst_ptl
;
1058 int rss
[NR_MM_COUNTERS
];
1059 swp_entry_t entry
= (swp_entry_t
){0};
1064 dst_pte
= pte_alloc_map_lock(dst_mm
, dst_pmd
, addr
, &dst_ptl
);
1067 src_pte
= pte_offset_map(src_pmd
, addr
);
1068 src_ptl
= pte_lockptr(src_mm
, src_pmd
);
1069 spin_lock_nested(src_ptl
, SINGLE_DEPTH_NESTING
);
1070 orig_src_pte
= src_pte
;
1071 orig_dst_pte
= dst_pte
;
1072 arch_enter_lazy_mmu_mode();
1076 * We are holding two locks at this point - either of them
1077 * could generate latencies in another task on another CPU.
1079 if (progress
>= 32) {
1081 if (need_resched() ||
1082 spin_needbreak(src_ptl
) || spin_needbreak(dst_ptl
))
1085 if (pte_none(*src_pte
)) {
1089 entry
.val
= copy_one_pte(dst_mm
, src_mm
, dst_pte
, src_pte
,
1094 } while (dst_pte
++, src_pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
1096 arch_leave_lazy_mmu_mode();
1097 spin_unlock(src_ptl
);
1098 pte_unmap(orig_src_pte
);
1099 add_mm_rss_vec(dst_mm
, rss
);
1100 pte_unmap_unlock(orig_dst_pte
, dst_ptl
);
1104 if (add_swap_count_continuation(entry
, GFP_KERNEL
) < 0)
1113 static inline int copy_pmd_range(struct mm_struct
*dst_mm
, struct mm_struct
*src_mm
,
1114 pud_t
*dst_pud
, pud_t
*src_pud
, struct vm_area_struct
*vma
,
1115 unsigned long addr
, unsigned long end
)
1117 pmd_t
*src_pmd
, *dst_pmd
;
1120 dst_pmd
= pmd_alloc(dst_mm
, dst_pud
, addr
);
1123 src_pmd
= pmd_offset(src_pud
, addr
);
1125 next
= pmd_addr_end(addr
, end
);
1126 if (is_swap_pmd(*src_pmd
) || pmd_trans_huge(*src_pmd
)
1127 || pmd_devmap(*src_pmd
)) {
1129 VM_BUG_ON_VMA(next
-addr
!= HPAGE_PMD_SIZE
, vma
);
1130 err
= copy_huge_pmd(dst_mm
, src_mm
,
1131 dst_pmd
, src_pmd
, addr
, vma
);
1138 if (pmd_none_or_clear_bad(src_pmd
))
1140 if (copy_pte_range(dst_mm
, src_mm
, dst_pmd
, src_pmd
,
1143 } while (dst_pmd
++, src_pmd
++, addr
= next
, addr
!= end
);
1147 static inline int copy_pud_range(struct mm_struct
*dst_mm
, struct mm_struct
*src_mm
,
1148 p4d_t
*dst_p4d
, p4d_t
*src_p4d
, struct vm_area_struct
*vma
,
1149 unsigned long addr
, unsigned long end
)
1151 pud_t
*src_pud
, *dst_pud
;
1154 dst_pud
= pud_alloc(dst_mm
, dst_p4d
, addr
);
1157 src_pud
= pud_offset(src_p4d
, addr
);
1159 next
= pud_addr_end(addr
, end
);
1160 if (pud_trans_huge(*src_pud
) || pud_devmap(*src_pud
)) {
1163 VM_BUG_ON_VMA(next
-addr
!= HPAGE_PUD_SIZE
, vma
);
1164 err
= copy_huge_pud(dst_mm
, src_mm
,
1165 dst_pud
, src_pud
, addr
, vma
);
1172 if (pud_none_or_clear_bad(src_pud
))
1174 if (copy_pmd_range(dst_mm
, src_mm
, dst_pud
, src_pud
,
1177 } while (dst_pud
++, src_pud
++, addr
= next
, addr
!= end
);
1181 static inline int copy_p4d_range(struct mm_struct
*dst_mm
, struct mm_struct
*src_mm
,
1182 pgd_t
*dst_pgd
, pgd_t
*src_pgd
, struct vm_area_struct
*vma
,
1183 unsigned long addr
, unsigned long end
)
1185 p4d_t
*src_p4d
, *dst_p4d
;
1188 dst_p4d
= p4d_alloc(dst_mm
, dst_pgd
, addr
);
1191 src_p4d
= p4d_offset(src_pgd
, addr
);
1193 next
= p4d_addr_end(addr
, end
);
1194 if (p4d_none_or_clear_bad(src_p4d
))
1196 if (copy_pud_range(dst_mm
, src_mm
, dst_p4d
, src_p4d
,
1199 } while (dst_p4d
++, src_p4d
++, addr
= next
, addr
!= end
);
1203 int copy_page_range(struct mm_struct
*dst_mm
, struct mm_struct
*src_mm
,
1204 struct vm_area_struct
*vma
)
1206 pgd_t
*src_pgd
, *dst_pgd
;
1208 unsigned long addr
= vma
->vm_start
;
1209 unsigned long end
= vma
->vm_end
;
1210 unsigned long mmun_start
; /* For mmu_notifiers */
1211 unsigned long mmun_end
; /* For mmu_notifiers */
1216 * Don't copy ptes where a page fault will fill them correctly.
1217 * Fork becomes much lighter when there are big shared or private
1218 * readonly mappings. The tradeoff is that copy_page_range is more
1219 * efficient than faulting.
1221 if (!(vma
->vm_flags
& (VM_HUGETLB
| VM_PFNMAP
| VM_MIXEDMAP
)) &&
1225 if (is_vm_hugetlb_page(vma
))
1226 return copy_hugetlb_page_range(dst_mm
, src_mm
, vma
);
1228 if (unlikely(vma
->vm_flags
& VM_PFNMAP
)) {
1230 * We do not free on error cases below as remove_vma
1231 * gets called on error from higher level routine
1233 ret
= track_pfn_copy(vma
);
1239 * We need to invalidate the secondary MMU mappings only when
1240 * there could be a permission downgrade on the ptes of the
1241 * parent mm. And a permission downgrade will only happen if
1242 * is_cow_mapping() returns true.
1244 is_cow
= is_cow_mapping(vma
->vm_flags
);
1248 mmu_notifier_invalidate_range_start(src_mm
, mmun_start
,
1252 dst_pgd
= pgd_offset(dst_mm
, addr
);
1253 src_pgd
= pgd_offset(src_mm
, addr
);
1255 next
= pgd_addr_end(addr
, end
);
1256 if (pgd_none_or_clear_bad(src_pgd
))
1258 if (unlikely(copy_p4d_range(dst_mm
, src_mm
, dst_pgd
, src_pgd
,
1259 vma
, addr
, next
))) {
1263 } while (dst_pgd
++, src_pgd
++, addr
= next
, addr
!= end
);
1266 mmu_notifier_invalidate_range_end(src_mm
, mmun_start
, mmun_end
);
1270 static unsigned long zap_pte_range(struct mmu_gather
*tlb
,
1271 struct vm_area_struct
*vma
, pmd_t
*pmd
,
1272 unsigned long addr
, unsigned long end
,
1273 struct zap_details
*details
)
1275 struct mm_struct
*mm
= tlb
->mm
;
1276 int force_flush
= 0;
1277 int rss
[NR_MM_COUNTERS
];
1283 tlb_remove_check_page_size_change(tlb
, PAGE_SIZE
);
1286 start_pte
= pte_offset_map_lock(mm
, pmd
, addr
, &ptl
);
1288 flush_tlb_batched_pending(mm
);
1289 arch_enter_lazy_mmu_mode();
1292 if (pte_none(ptent
))
1295 if (pte_present(ptent
)) {
1298 page
= _vm_normal_page(vma
, addr
, ptent
, true);
1299 if (unlikely(details
) && page
) {
1301 * unmap_shared_mapping_pages() wants to
1302 * invalidate cache without truncating:
1303 * unmap shared but keep private pages.
1305 if (details
->check_mapping
&&
1306 details
->check_mapping
!= page_rmapping(page
))
1309 ptent
= ptep_get_and_clear_full(mm
, addr
, pte
,
1311 tlb_remove_tlb_entry(tlb
, pte
, addr
);
1312 if (unlikely(!page
))
1315 if (!PageAnon(page
)) {
1316 if (pte_dirty(ptent
)) {
1318 set_page_dirty(page
);
1320 if (pte_young(ptent
) &&
1321 likely(!(vma
->vm_flags
& VM_SEQ_READ
)))
1322 mark_page_accessed(page
);
1324 rss
[mm_counter(page
)]--;
1325 page_remove_rmap(page
, false);
1326 if (unlikely(page_mapcount(page
) < 0))
1327 print_bad_pte(vma
, addr
, ptent
, page
);
1328 if (unlikely(__tlb_remove_page(tlb
, page
))) {
1336 entry
= pte_to_swp_entry(ptent
);
1337 if (non_swap_entry(entry
) && is_device_private_entry(entry
)) {
1338 struct page
*page
= device_private_entry_to_page(entry
);
1340 if (unlikely(details
&& details
->check_mapping
)) {
1342 * unmap_shared_mapping_pages() wants to
1343 * invalidate cache without truncating:
1344 * unmap shared but keep private pages.
1346 if (details
->check_mapping
!=
1347 page_rmapping(page
))
1351 pte_clear_not_present_full(mm
, addr
, pte
, tlb
->fullmm
);
1352 rss
[mm_counter(page
)]--;
1353 page_remove_rmap(page
, false);
1358 /* If details->check_mapping, we leave swap entries. */
1359 if (unlikely(details
))
1362 entry
= pte_to_swp_entry(ptent
);
1363 if (!non_swap_entry(entry
))
1365 else if (is_migration_entry(entry
)) {
1368 page
= migration_entry_to_page(entry
);
1369 rss
[mm_counter(page
)]--;
1371 if (unlikely(!free_swap_and_cache(entry
)))
1372 print_bad_pte(vma
, addr
, ptent
, NULL
);
1373 pte_clear_not_present_full(mm
, addr
, pte
, tlb
->fullmm
);
1374 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
1376 add_mm_rss_vec(mm
, rss
);
1377 arch_leave_lazy_mmu_mode();
1379 /* Do the actual TLB flush before dropping ptl */
1381 tlb_flush_mmu_tlbonly(tlb
);
1382 pte_unmap_unlock(start_pte
, ptl
);
1385 * If we forced a TLB flush (either due to running out of
1386 * batch buffers or because we needed to flush dirty TLB
1387 * entries before releasing the ptl), free the batched
1388 * memory too. Restart if we didn't do everything.
1392 tlb_flush_mmu_free(tlb
);
1400 static inline unsigned long zap_pmd_range(struct mmu_gather
*tlb
,
1401 struct vm_area_struct
*vma
, pud_t
*pud
,
1402 unsigned long addr
, unsigned long end
,
1403 struct zap_details
*details
)
1408 pmd
= pmd_offset(pud
, addr
);
1410 next
= pmd_addr_end(addr
, end
);
1411 if (is_swap_pmd(*pmd
) || pmd_trans_huge(*pmd
) || pmd_devmap(*pmd
)) {
1412 if (next
- addr
!= HPAGE_PMD_SIZE
) {
1413 VM_BUG_ON_VMA(vma_is_anonymous(vma
) &&
1414 !rwsem_is_locked(&tlb
->mm
->mmap_sem
), vma
);
1415 __split_huge_pmd(vma
, pmd
, addr
, false, NULL
);
1416 } else if (zap_huge_pmd(tlb
, vma
, pmd
, addr
))
1421 * Here there can be other concurrent MADV_DONTNEED or
1422 * trans huge page faults running, and if the pmd is
1423 * none or trans huge it can change under us. This is
1424 * because MADV_DONTNEED holds the mmap_sem in read
1427 if (pmd_none_or_trans_huge_or_clear_bad(pmd
))
1429 next
= zap_pte_range(tlb
, vma
, pmd
, addr
, next
, details
);
1432 } while (pmd
++, addr
= next
, addr
!= end
);
1437 static inline unsigned long zap_pud_range(struct mmu_gather
*tlb
,
1438 struct vm_area_struct
*vma
, p4d_t
*p4d
,
1439 unsigned long addr
, unsigned long end
,
1440 struct zap_details
*details
)
1445 pud
= pud_offset(p4d
, addr
);
1447 next
= pud_addr_end(addr
, end
);
1448 if (pud_trans_huge(*pud
) || pud_devmap(*pud
)) {
1449 if (next
- addr
!= HPAGE_PUD_SIZE
) {
1450 VM_BUG_ON_VMA(!rwsem_is_locked(&tlb
->mm
->mmap_sem
), vma
);
1451 split_huge_pud(vma
, pud
, addr
);
1452 } else if (zap_huge_pud(tlb
, vma
, pud
, addr
))
1456 if (pud_none_or_clear_bad(pud
))
1458 next
= zap_pmd_range(tlb
, vma
, pud
, addr
, next
, details
);
1461 } while (pud
++, addr
= next
, addr
!= end
);
1466 static inline unsigned long zap_p4d_range(struct mmu_gather
*tlb
,
1467 struct vm_area_struct
*vma
, pgd_t
*pgd
,
1468 unsigned long addr
, unsigned long end
,
1469 struct zap_details
*details
)
1474 p4d
= p4d_offset(pgd
, addr
);
1476 next
= p4d_addr_end(addr
, end
);
1477 if (p4d_none_or_clear_bad(p4d
))
1479 next
= zap_pud_range(tlb
, vma
, p4d
, addr
, next
, details
);
1480 } while (p4d
++, addr
= next
, addr
!= end
);
1485 void unmap_page_range(struct mmu_gather
*tlb
,
1486 struct vm_area_struct
*vma
,
1487 unsigned long addr
, unsigned long end
,
1488 struct zap_details
*details
)
1493 BUG_ON(addr
>= end
);
1494 tlb_start_vma(tlb
, vma
);
1495 pgd
= pgd_offset(vma
->vm_mm
, addr
);
1497 next
= pgd_addr_end(addr
, end
);
1498 if (pgd_none_or_clear_bad(pgd
))
1500 next
= zap_p4d_range(tlb
, vma
, pgd
, addr
, next
, details
);
1501 } while (pgd
++, addr
= next
, addr
!= end
);
1502 tlb_end_vma(tlb
, vma
);
1506 static void unmap_single_vma(struct mmu_gather
*tlb
,
1507 struct vm_area_struct
*vma
, unsigned long start_addr
,
1508 unsigned long end_addr
,
1509 struct zap_details
*details
)
1511 unsigned long start
= max(vma
->vm_start
, start_addr
);
1514 if (start
>= vma
->vm_end
)
1516 end
= min(vma
->vm_end
, end_addr
);
1517 if (end
<= vma
->vm_start
)
1521 uprobe_munmap(vma
, start
, end
);
1523 if (unlikely(vma
->vm_flags
& VM_PFNMAP
))
1524 untrack_pfn(vma
, 0, 0);
1527 if (unlikely(is_vm_hugetlb_page(vma
))) {
1529 * It is undesirable to test vma->vm_file as it
1530 * should be non-null for valid hugetlb area.
1531 * However, vm_file will be NULL in the error
1532 * cleanup path of mmap_region. When
1533 * hugetlbfs ->mmap method fails,
1534 * mmap_region() nullifies vma->vm_file
1535 * before calling this function to clean up.
1536 * Since no pte has actually been setup, it is
1537 * safe to do nothing in this case.
1540 i_mmap_lock_write(vma
->vm_file
->f_mapping
);
1541 __unmap_hugepage_range_final(tlb
, vma
, start
, end
, NULL
);
1542 i_mmap_unlock_write(vma
->vm_file
->f_mapping
);
1545 unmap_page_range(tlb
, vma
, start
, end
, details
);
1550 * unmap_vmas - unmap a range of memory covered by a list of vma's
1551 * @tlb: address of the caller's struct mmu_gather
1552 * @vma: the starting vma
1553 * @start_addr: virtual address at which to start unmapping
1554 * @end_addr: virtual address at which to end unmapping
1556 * Unmap all pages in the vma list.
1558 * Only addresses between `start' and `end' will be unmapped.
1560 * The VMA list must be sorted in ascending virtual address order.
1562 * unmap_vmas() assumes that the caller will flush the whole unmapped address
1563 * range after unmap_vmas() returns. So the only responsibility here is to
1564 * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
1565 * drops the lock and schedules.
1567 void unmap_vmas(struct mmu_gather
*tlb
,
1568 struct vm_area_struct
*vma
, unsigned long start_addr
,
1569 unsigned long end_addr
)
1571 struct mm_struct
*mm
= vma
->vm_mm
;
1573 mmu_notifier_invalidate_range_start(mm
, start_addr
, end_addr
);
1574 for ( ; vma
&& vma
->vm_start
< end_addr
; vma
= vma
->vm_next
)
1575 unmap_single_vma(tlb
, vma
, start_addr
, end_addr
, NULL
);
1576 mmu_notifier_invalidate_range_end(mm
, start_addr
, end_addr
);
1580 * zap_page_range - remove user pages in a given range
1581 * @vma: vm_area_struct holding the applicable pages
1582 * @start: starting address of pages to zap
1583 * @size: number of bytes to zap
1585 * Caller must protect the VMA list
1587 void zap_page_range(struct vm_area_struct
*vma
, unsigned long start
,
1590 struct mm_struct
*mm
= vma
->vm_mm
;
1591 struct mmu_gather tlb
;
1592 unsigned long end
= start
+ size
;
1595 tlb_gather_mmu(&tlb
, mm
, start
, end
);
1596 update_hiwater_rss(mm
);
1597 mmu_notifier_invalidate_range_start(mm
, start
, end
);
1598 for ( ; vma
&& vma
->vm_start
< end
; vma
= vma
->vm_next
) {
1599 unmap_single_vma(&tlb
, vma
, start
, end
, NULL
);
1602 * zap_page_range does not specify whether mmap_sem should be
1603 * held for read or write. That allows parallel zap_page_range
1604 * operations to unmap a PTE and defer a flush meaning that
1605 * this call observes pte_none and fails to flush the TLB.
1606 * Rather than adding a complex API, ensure that no stale
1607 * TLB entries exist when this call returns.
1609 flush_tlb_range(vma
, start
, end
);
1612 mmu_notifier_invalidate_range_end(mm
, start
, end
);
1613 tlb_finish_mmu(&tlb
, start
, end
);
1617 * zap_page_range_single - remove user pages in a given range
1618 * @vma: vm_area_struct holding the applicable pages
1619 * @address: starting address of pages to zap
1620 * @size: number of bytes to zap
1621 * @details: details of shared cache invalidation
1623 * The range must fit into one VMA.
1625 static void zap_page_range_single(struct vm_area_struct
*vma
, unsigned long address
,
1626 unsigned long size
, struct zap_details
*details
)
1628 struct mm_struct
*mm
= vma
->vm_mm
;
1629 struct mmu_gather tlb
;
1630 unsigned long end
= address
+ size
;
1633 tlb_gather_mmu(&tlb
, mm
, address
, end
);
1634 update_hiwater_rss(mm
);
1635 mmu_notifier_invalidate_range_start(mm
, address
, end
);
1636 unmap_single_vma(&tlb
, vma
, address
, end
, details
);
1637 mmu_notifier_invalidate_range_end(mm
, address
, end
);
1638 tlb_finish_mmu(&tlb
, address
, end
);
1642 * zap_vma_ptes - remove ptes mapping the vma
1643 * @vma: vm_area_struct holding ptes to be zapped
1644 * @address: starting address of pages to zap
1645 * @size: number of bytes to zap
1647 * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1649 * The entire address range must be fully contained within the vma.
1651 * Returns 0 if successful.
1653 int zap_vma_ptes(struct vm_area_struct
*vma
, unsigned long address
,
1656 if (address
< vma
->vm_start
|| address
+ size
> vma
->vm_end
||
1657 !(vma
->vm_flags
& VM_PFNMAP
))
1659 zap_page_range_single(vma
, address
, size
, NULL
);
1662 EXPORT_SYMBOL_GPL(zap_vma_ptes
);
1664 pte_t
*__get_locked_pte(struct mm_struct
*mm
, unsigned long addr
,
1672 pgd
= pgd_offset(mm
, addr
);
1673 p4d
= p4d_alloc(mm
, pgd
, addr
);
1676 pud
= pud_alloc(mm
, p4d
, addr
);
1679 pmd
= pmd_alloc(mm
, pud
, addr
);
1683 VM_BUG_ON(pmd_trans_huge(*pmd
));
1684 return pte_alloc_map_lock(mm
, pmd
, addr
, ptl
);
1688 * This is the old fallback for page remapping.
1690 * For historical reasons, it only allows reserved pages. Only
1691 * old drivers should use this, and they needed to mark their
1692 * pages reserved for the old functions anyway.
1694 static int insert_page(struct vm_area_struct
*vma
, unsigned long addr
,
1695 struct page
*page
, pgprot_t prot
)
1697 struct mm_struct
*mm
= vma
->vm_mm
;
1706 flush_dcache_page(page
);
1707 pte
= get_locked_pte(mm
, addr
, &ptl
);
1711 if (!pte_none(*pte
))
1714 /* Ok, finally just insert the thing.. */
1716 inc_mm_counter_fast(mm
, mm_counter_file(page
));
1717 page_add_file_rmap(page
, false);
1718 set_pte_at(mm
, addr
, pte
, mk_pte(page
, prot
));
1721 pte_unmap_unlock(pte
, ptl
);
1724 pte_unmap_unlock(pte
, ptl
);
1730 * vm_insert_page - insert single page into user vma
1731 * @vma: user vma to map to
1732 * @addr: target user address of this page
1733 * @page: source kernel page
1735 * This allows drivers to insert individual pages they've allocated
1738 * The page has to be a nice clean _individual_ kernel allocation.
1739 * If you allocate a compound page, you need to have marked it as
1740 * such (__GFP_COMP), or manually just split the page up yourself
1741 * (see split_page()).
1743 * NOTE! Traditionally this was done with "remap_pfn_range()" which
1744 * took an arbitrary page protection parameter. This doesn't allow
1745 * that. Your vma protection will have to be set up correctly, which
1746 * means that if you want a shared writable mapping, you'd better
1747 * ask for a shared writable mapping!
1749 * The page does not need to be reserved.
1751 * Usually this function is called from f_op->mmap() handler
1752 * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
1753 * Caller must set VM_MIXEDMAP on vma if it wants to call this
1754 * function from other places, for example from page-fault handler.
1756 int vm_insert_page(struct vm_area_struct
*vma
, unsigned long addr
,
1759 if (addr
< vma
->vm_start
|| addr
>= vma
->vm_end
)
1761 if (!page_count(page
))
1763 if (!(vma
->vm_flags
& VM_MIXEDMAP
)) {
1764 BUG_ON(down_read_trylock(&vma
->vm_mm
->mmap_sem
));
1765 BUG_ON(vma
->vm_flags
& VM_PFNMAP
);
1766 vma
->vm_flags
|= VM_MIXEDMAP
;
1768 return insert_page(vma
, addr
, page
, vma
->vm_page_prot
);
1770 EXPORT_SYMBOL(vm_insert_page
);
1772 static int insert_pfn(struct vm_area_struct
*vma
, unsigned long addr
,
1773 pfn_t pfn
, pgprot_t prot
, bool mkwrite
)
1775 struct mm_struct
*mm
= vma
->vm_mm
;
1781 pte
= get_locked_pte(mm
, addr
, &ptl
);
1785 if (!pte_none(*pte
)) {
1788 * For read faults on private mappings the PFN passed
1789 * in may not match the PFN we have mapped if the
1790 * mapped PFN is a writeable COW page. In the mkwrite
1791 * case we are creating a writable PTE for a shared
1792 * mapping and we expect the PFNs to match.
1794 if (WARN_ON_ONCE(pte_pfn(*pte
) != pfn_t_to_pfn(pfn
)))
1802 /* Ok, finally just insert the thing.. */
1803 if (pfn_t_devmap(pfn
))
1804 entry
= pte_mkdevmap(pfn_t_pte(pfn
, prot
));
1806 entry
= pte_mkspecial(pfn_t_pte(pfn
, prot
));
1810 entry
= pte_mkyoung(entry
);
1811 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
1814 set_pte_at(mm
, addr
, pte
, entry
);
1815 update_mmu_cache(vma
, addr
, pte
); /* XXX: why not for insert_page? */
1819 pte_unmap_unlock(pte
, ptl
);
1825 * vm_insert_pfn - insert single pfn into user vma
1826 * @vma: user vma to map to
1827 * @addr: target user address of this page
1828 * @pfn: source kernel pfn
1830 * Similar to vm_insert_page, this allows drivers to insert individual pages
1831 * they've allocated into a user vma. Same comments apply.
1833 * This function should only be called from a vm_ops->fault handler, and
1834 * in that case the handler should return NULL.
1836 * vma cannot be a COW mapping.
1838 * As this is called only for pages that do not currently exist, we
1839 * do not need to flush old virtual caches or the TLB.
1841 int vm_insert_pfn(struct vm_area_struct
*vma
, unsigned long addr
,
1844 return vm_insert_pfn_prot(vma
, addr
, pfn
, vma
->vm_page_prot
);
1846 EXPORT_SYMBOL(vm_insert_pfn
);
1849 * vm_insert_pfn_prot - insert single pfn into user vma with specified pgprot
1850 * @vma: user vma to map to
1851 * @addr: target user address of this page
1852 * @pfn: source kernel pfn
1853 * @pgprot: pgprot flags for the inserted page
1855 * This is exactly like vm_insert_pfn, except that it allows drivers to
1856 * to override pgprot on a per-page basis.
1858 * This only makes sense for IO mappings, and it makes no sense for
1859 * cow mappings. In general, using multiple vmas is preferable;
1860 * vm_insert_pfn_prot should only be used if using multiple VMAs is
1863 int vm_insert_pfn_prot(struct vm_area_struct
*vma
, unsigned long addr
,
1864 unsigned long pfn
, pgprot_t pgprot
)
1868 * Technically, architectures with pte_special can avoid all these
1869 * restrictions (same for remap_pfn_range). However we would like
1870 * consistency in testing and feature parity among all, so we should
1871 * try to keep these invariants in place for everybody.
1873 BUG_ON(!(vma
->vm_flags
& (VM_PFNMAP
|VM_MIXEDMAP
)));
1874 BUG_ON((vma
->vm_flags
& (VM_PFNMAP
|VM_MIXEDMAP
)) ==
1875 (VM_PFNMAP
|VM_MIXEDMAP
));
1876 BUG_ON((vma
->vm_flags
& VM_PFNMAP
) && is_cow_mapping(vma
->vm_flags
));
1877 BUG_ON((vma
->vm_flags
& VM_MIXEDMAP
) && pfn_valid(pfn
));
1879 if (addr
< vma
->vm_start
|| addr
>= vma
->vm_end
)
1882 if (!pfn_modify_allowed(pfn
, pgprot
))
1885 track_pfn_insert(vma
, &pgprot
, __pfn_to_pfn_t(pfn
, PFN_DEV
));
1887 ret
= insert_pfn(vma
, addr
, __pfn_to_pfn_t(pfn
, PFN_DEV
), pgprot
,
1892 EXPORT_SYMBOL(vm_insert_pfn_prot
);
1894 static bool vm_mixed_ok(struct vm_area_struct
*vma
, pfn_t pfn
)
1896 /* these checks mirror the abort conditions in vm_normal_page */
1897 if (vma
->vm_flags
& VM_MIXEDMAP
)
1899 if (pfn_t_devmap(pfn
))
1901 if (pfn_t_special(pfn
))
1903 if (is_zero_pfn(pfn_t_to_pfn(pfn
)))
1908 static int __vm_insert_mixed(struct vm_area_struct
*vma
, unsigned long addr
,
1909 pfn_t pfn
, bool mkwrite
)
1911 pgprot_t pgprot
= vma
->vm_page_prot
;
1913 BUG_ON(!vm_mixed_ok(vma
, pfn
));
1915 if (addr
< vma
->vm_start
|| addr
>= vma
->vm_end
)
1918 track_pfn_insert(vma
, &pgprot
, pfn
);
1920 if (!pfn_modify_allowed(pfn_t_to_pfn(pfn
), pgprot
))
1924 * If we don't have pte special, then we have to use the pfn_valid()
1925 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
1926 * refcount the page if pfn_valid is true (hence insert_page rather
1927 * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP
1928 * without pte special, it would there be refcounted as a normal page.
1930 if (!HAVE_PTE_SPECIAL
&& !pfn_t_devmap(pfn
) && pfn_t_valid(pfn
)) {
1934 * At this point we are committed to insert_page()
1935 * regardless of whether the caller specified flags that
1936 * result in pfn_t_has_page() == false.
1938 page
= pfn_to_page(pfn_t_to_pfn(pfn
));
1939 return insert_page(vma
, addr
, page
, pgprot
);
1941 return insert_pfn(vma
, addr
, pfn
, pgprot
, mkwrite
);
1944 int vm_insert_mixed(struct vm_area_struct
*vma
, unsigned long addr
,
1947 return __vm_insert_mixed(vma
, addr
, pfn
, false);
1950 EXPORT_SYMBOL(vm_insert_mixed
);
1952 int vm_insert_mixed_mkwrite(struct vm_area_struct
*vma
, unsigned long addr
,
1955 return __vm_insert_mixed(vma
, addr
, pfn
, true);
1957 EXPORT_SYMBOL(vm_insert_mixed_mkwrite
);
1960 * maps a range of physical memory into the requested pages. the old
1961 * mappings are removed. any references to nonexistent pages results
1962 * in null mappings (currently treated as "copy-on-access")
1964 static int remap_pte_range(struct mm_struct
*mm
, pmd_t
*pmd
,
1965 unsigned long addr
, unsigned long end
,
1966 unsigned long pfn
, pgprot_t prot
)
1972 pte
= pte_alloc_map_lock(mm
, pmd
, addr
, &ptl
);
1975 arch_enter_lazy_mmu_mode();
1977 BUG_ON(!pte_none(*pte
));
1978 if (!pfn_modify_allowed(pfn
, prot
)) {
1982 set_pte_at(mm
, addr
, pte
, pte_mkspecial(pfn_pte(pfn
, prot
)));
1984 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
1985 arch_leave_lazy_mmu_mode();
1986 pte_unmap_unlock(pte
- 1, ptl
);
1990 static inline int remap_pmd_range(struct mm_struct
*mm
, pud_t
*pud
,
1991 unsigned long addr
, unsigned long end
,
1992 unsigned long pfn
, pgprot_t prot
)
1998 pfn
-= addr
>> PAGE_SHIFT
;
1999 pmd
= pmd_alloc(mm
, pud
, addr
);
2002 VM_BUG_ON(pmd_trans_huge(*pmd
));
2004 next
= pmd_addr_end(addr
, end
);
2005 err
= remap_pte_range(mm
, pmd
, addr
, next
,
2006 pfn
+ (addr
>> PAGE_SHIFT
), prot
);
2009 } while (pmd
++, addr
= next
, addr
!= end
);
2013 static inline int remap_pud_range(struct mm_struct
*mm
, p4d_t
*p4d
,
2014 unsigned long addr
, unsigned long end
,
2015 unsigned long pfn
, pgprot_t prot
)
2021 pfn
-= addr
>> PAGE_SHIFT
;
2022 pud
= pud_alloc(mm
, p4d
, addr
);
2026 next
= pud_addr_end(addr
, end
);
2027 err
= remap_pmd_range(mm
, pud
, addr
, next
,
2028 pfn
+ (addr
>> PAGE_SHIFT
), prot
);
2031 } while (pud
++, addr
= next
, addr
!= end
);
2035 static inline int remap_p4d_range(struct mm_struct
*mm
, pgd_t
*pgd
,
2036 unsigned long addr
, unsigned long end
,
2037 unsigned long pfn
, pgprot_t prot
)
2043 pfn
-= addr
>> PAGE_SHIFT
;
2044 p4d
= p4d_alloc(mm
, pgd
, addr
);
2048 next
= p4d_addr_end(addr
, end
);
2049 err
= remap_pud_range(mm
, p4d
, addr
, next
,
2050 pfn
+ (addr
>> PAGE_SHIFT
), prot
);
2053 } while (p4d
++, addr
= next
, addr
!= end
);
2058 * remap_pfn_range - remap kernel memory to userspace
2059 * @vma: user vma to map to
2060 * @addr: target user address to start at
2061 * @pfn: physical address of kernel memory
2062 * @size: size of map area
2063 * @prot: page protection flags for this mapping
2065 * Note: this is only safe if the mm semaphore is held when called.
2067 int remap_pfn_range(struct vm_area_struct
*vma
, unsigned long addr
,
2068 unsigned long pfn
, unsigned long size
, pgprot_t prot
)
2072 unsigned long end
= addr
+ PAGE_ALIGN(size
);
2073 struct mm_struct
*mm
= vma
->vm_mm
;
2074 unsigned long remap_pfn
= pfn
;
2078 * Physically remapped pages are special. Tell the
2079 * rest of the world about it:
2080 * VM_IO tells people not to look at these pages
2081 * (accesses can have side effects).
2082 * VM_PFNMAP tells the core MM that the base pages are just
2083 * raw PFN mappings, and do not have a "struct page" associated
2086 * Disable vma merging and expanding with mremap().
2088 * Omit vma from core dump, even when VM_IO turned off.
2090 * There's a horrible special case to handle copy-on-write
2091 * behaviour that some programs depend on. We mark the "original"
2092 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
2093 * See vm_normal_page() for details.
2095 if (is_cow_mapping(vma
->vm_flags
)) {
2096 if (addr
!= vma
->vm_start
|| end
!= vma
->vm_end
)
2098 vma
->vm_pgoff
= pfn
;
2101 err
= track_pfn_remap(vma
, &prot
, remap_pfn
, addr
, PAGE_ALIGN(size
));
2105 vma
->vm_flags
|= VM_IO
| VM_PFNMAP
| VM_DONTEXPAND
| VM_DONTDUMP
;
2107 BUG_ON(addr
>= end
);
2108 pfn
-= addr
>> PAGE_SHIFT
;
2109 pgd
= pgd_offset(mm
, addr
);
2110 flush_cache_range(vma
, addr
, end
);
2112 next
= pgd_addr_end(addr
, end
);
2113 err
= remap_p4d_range(mm
, pgd
, addr
, next
,
2114 pfn
+ (addr
>> PAGE_SHIFT
), prot
);
2117 } while (pgd
++, addr
= next
, addr
!= end
);
2120 untrack_pfn(vma
, remap_pfn
, PAGE_ALIGN(size
));
2124 EXPORT_SYMBOL(remap_pfn_range
);
2127 * vm_iomap_memory - remap memory to userspace
2128 * @vma: user vma to map to
2129 * @start: start of area
2130 * @len: size of area
2132 * This is a simplified io_remap_pfn_range() for common driver use. The
2133 * driver just needs to give us the physical memory range to be mapped,
2134 * we'll figure out the rest from the vma information.
2136 * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
2137 * whatever write-combining details or similar.
2139 int vm_iomap_memory(struct vm_area_struct
*vma
, phys_addr_t start
, unsigned long len
)
2141 unsigned long vm_len
, pfn
, pages
;
2143 /* Check that the physical memory area passed in looks valid */
2144 if (start
+ len
< start
)
2147 * You *really* shouldn't map things that aren't page-aligned,
2148 * but we've historically allowed it because IO memory might
2149 * just have smaller alignment.
2151 len
+= start
& ~PAGE_MASK
;
2152 pfn
= start
>> PAGE_SHIFT
;
2153 pages
= (len
+ ~PAGE_MASK
) >> PAGE_SHIFT
;
2154 if (pfn
+ pages
< pfn
)
2157 /* We start the mapping 'vm_pgoff' pages into the area */
2158 if (vma
->vm_pgoff
> pages
)
2160 pfn
+= vma
->vm_pgoff
;
2161 pages
-= vma
->vm_pgoff
;
2163 /* Can we fit all of the mapping? */
2164 vm_len
= vma
->vm_end
- vma
->vm_start
;
2165 if (vm_len
>> PAGE_SHIFT
> pages
)
2168 /* Ok, let it rip */
2169 return io_remap_pfn_range(vma
, vma
->vm_start
, pfn
, vm_len
, vma
->vm_page_prot
);
2171 EXPORT_SYMBOL(vm_iomap_memory
);
2173 static int apply_to_pte_range(struct mm_struct
*mm
, pmd_t
*pmd
,
2174 unsigned long addr
, unsigned long end
,
2175 pte_fn_t fn
, void *data
)
2180 spinlock_t
*uninitialized_var(ptl
);
2182 pte
= (mm
== &init_mm
) ?
2183 pte_alloc_kernel(pmd
, addr
) :
2184 pte_alloc_map_lock(mm
, pmd
, addr
, &ptl
);
2188 BUG_ON(pmd_huge(*pmd
));
2190 arch_enter_lazy_mmu_mode();
2192 token
= pmd_pgtable(*pmd
);
2195 err
= fn(pte
++, token
, addr
, data
);
2198 } while (addr
+= PAGE_SIZE
, addr
!= end
);
2200 arch_leave_lazy_mmu_mode();
2203 pte_unmap_unlock(pte
-1, ptl
);
2207 static int apply_to_pmd_range(struct mm_struct
*mm
, pud_t
*pud
,
2208 unsigned long addr
, unsigned long end
,
2209 pte_fn_t fn
, void *data
)
2215 BUG_ON(pud_huge(*pud
));
2217 pmd
= pmd_alloc(mm
, pud
, addr
);
2221 next
= pmd_addr_end(addr
, end
);
2222 err
= apply_to_pte_range(mm
, pmd
, addr
, next
, fn
, data
);
2225 } while (pmd
++, addr
= next
, addr
!= end
);
2229 static int apply_to_pud_range(struct mm_struct
*mm
, p4d_t
*p4d
,
2230 unsigned long addr
, unsigned long end
,
2231 pte_fn_t fn
, void *data
)
2237 pud
= pud_alloc(mm
, p4d
, addr
);
2241 next
= pud_addr_end(addr
, end
);
2242 err
= apply_to_pmd_range(mm
, pud
, addr
, next
, fn
, data
);
2245 } while (pud
++, addr
= next
, addr
!= end
);
2249 static int apply_to_p4d_range(struct mm_struct
*mm
, pgd_t
*pgd
,
2250 unsigned long addr
, unsigned long end
,
2251 pte_fn_t fn
, void *data
)
2257 p4d
= p4d_alloc(mm
, pgd
, addr
);
2261 next
= p4d_addr_end(addr
, end
);
2262 err
= apply_to_pud_range(mm
, p4d
, addr
, next
, fn
, data
);
2265 } while (p4d
++, addr
= next
, addr
!= end
);
2270 * Scan a region of virtual memory, filling in page tables as necessary
2271 * and calling a provided function on each leaf page table.
2273 int apply_to_page_range(struct mm_struct
*mm
, unsigned long addr
,
2274 unsigned long size
, pte_fn_t fn
, void *data
)
2278 unsigned long end
= addr
+ size
;
2281 if (WARN_ON(addr
>= end
))
2284 pgd
= pgd_offset(mm
, addr
);
2286 next
= pgd_addr_end(addr
, end
);
2287 err
= apply_to_p4d_range(mm
, pgd
, addr
, next
, fn
, data
);
2290 } while (pgd
++, addr
= next
, addr
!= end
);
2294 EXPORT_SYMBOL_GPL(apply_to_page_range
);
2297 * handle_pte_fault chooses page fault handler according to an entry which was
2298 * read non-atomically. Before making any commitment, on those architectures
2299 * or configurations (e.g. i386 with PAE) which might give a mix of unmatched
2300 * parts, do_swap_page must check under lock before unmapping the pte and
2301 * proceeding (but do_wp_page is only called after already making such a check;
2302 * and do_anonymous_page can safely check later on).
2304 static inline int pte_unmap_same(struct mm_struct
*mm
, pmd_t
*pmd
,
2305 pte_t
*page_table
, pte_t orig_pte
)
2308 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
2309 if (sizeof(pte_t
) > sizeof(unsigned long)) {
2310 spinlock_t
*ptl
= pte_lockptr(mm
, pmd
);
2312 same
= pte_same(*page_table
, orig_pte
);
2316 pte_unmap(page_table
);
2320 static inline void cow_user_page(struct page
*dst
, struct page
*src
, unsigned long va
, struct vm_area_struct
*vma
)
2322 debug_dma_assert_idle(src
);
2325 * If the source page was a PFN mapping, we don't have
2326 * a "struct page" for it. We do a best-effort copy by
2327 * just copying from the original user address. If that
2328 * fails, we just zero-fill it. Live with it.
2330 if (unlikely(!src
)) {
2331 void *kaddr
= kmap_atomic(dst
);
2332 void __user
*uaddr
= (void __user
*)(va
& PAGE_MASK
);
2335 * This really shouldn't fail, because the page is there
2336 * in the page tables. But it might just be unreadable,
2337 * in which case we just give up and fill the result with
2340 if (__copy_from_user_inatomic(kaddr
, uaddr
, PAGE_SIZE
))
2342 kunmap_atomic(kaddr
);
2343 flush_dcache_page(dst
);
2345 copy_user_highpage(dst
, src
, va
, vma
);
2348 static gfp_t
__get_fault_gfp_mask(struct vm_area_struct
*vma
)
2350 struct file
*vm_file
= vma
->vm_file
;
2353 return mapping_gfp_mask(vm_file
->f_mapping
) | __GFP_FS
| __GFP_IO
;
2356 * Special mappings (e.g. VDSO) do not have any file so fake
2357 * a default GFP_KERNEL for them.
2363 * Notify the address space that the page is about to become writable so that
2364 * it can prohibit this or wait for the page to get into an appropriate state.
2366 * We do this without the lock held, so that it can sleep if it needs to.
2368 static int do_page_mkwrite(struct vm_fault
*vmf
)
2371 struct page
*page
= vmf
->page
;
2372 unsigned int old_flags
= vmf
->flags
;
2374 vmf
->flags
= FAULT_FLAG_WRITE
|FAULT_FLAG_MKWRITE
;
2376 ret
= vmf
->vma
->vm_ops
->page_mkwrite(vmf
);
2377 /* Restore original flags so that caller is not surprised */
2378 vmf
->flags
= old_flags
;
2379 if (unlikely(ret
& (VM_FAULT_ERROR
| VM_FAULT_NOPAGE
)))
2381 if (unlikely(!(ret
& VM_FAULT_LOCKED
))) {
2383 if (!page
->mapping
) {
2385 return 0; /* retry */
2387 ret
|= VM_FAULT_LOCKED
;
2389 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
2394 * Handle dirtying of a page in shared file mapping on a write fault.
2396 * The function expects the page to be locked and unlocks it.
2398 static void fault_dirty_shared_page(struct vm_area_struct
*vma
,
2401 struct address_space
*mapping
;
2403 bool page_mkwrite
= vma
->vm_ops
&& vma
->vm_ops
->page_mkwrite
;
2405 dirtied
= set_page_dirty(page
);
2406 VM_BUG_ON_PAGE(PageAnon(page
), page
);
2408 * Take a local copy of the address_space - page.mapping may be zeroed
2409 * by truncate after unlock_page(). The address_space itself remains
2410 * pinned by vma->vm_file's reference. We rely on unlock_page()'s
2411 * release semantics to prevent the compiler from undoing this copying.
2413 mapping
= page_rmapping(page
);
2416 if ((dirtied
|| page_mkwrite
) && mapping
) {
2418 * Some device drivers do not set page.mapping
2419 * but still dirty their pages
2421 balance_dirty_pages_ratelimited(mapping
);
2425 file_update_time(vma
->vm_file
);
2429 * Handle write page faults for pages that can be reused in the current vma
2431 * This can happen either due to the mapping being with the VM_SHARED flag,
2432 * or due to us being the last reference standing to the page. In either
2433 * case, all we need to do here is to mark the page as writable and update
2434 * any related book-keeping.
2436 static inline void wp_page_reuse(struct vm_fault
*vmf
)
2437 __releases(vmf
->ptl
)
2439 struct vm_area_struct
*vma
= vmf
->vma
;
2440 struct page
*page
= vmf
->page
;
2443 * Clear the pages cpupid information as the existing
2444 * information potentially belongs to a now completely
2445 * unrelated process.
2448 page_cpupid_xchg_last(page
, (1 << LAST_CPUPID_SHIFT
) - 1);
2450 flush_cache_page(vma
, vmf
->address
, pte_pfn(vmf
->orig_pte
));
2451 entry
= pte_mkyoung(vmf
->orig_pte
);
2452 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
2453 if (ptep_set_access_flags(vma
, vmf
->address
, vmf
->pte
, entry
, 1))
2454 update_mmu_cache(vma
, vmf
->address
, vmf
->pte
);
2455 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
2459 * Handle the case of a page which we actually need to copy to a new page.
2461 * Called with mmap_sem locked and the old page referenced, but
2462 * without the ptl held.
2464 * High level logic flow:
2466 * - Allocate a page, copy the content of the old page to the new one.
2467 * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc.
2468 * - Take the PTL. If the pte changed, bail out and release the allocated page
2469 * - If the pte is still the way we remember it, update the page table and all
2470 * relevant references. This includes dropping the reference the page-table
2471 * held to the old page, as well as updating the rmap.
2472 * - In any case, unlock the PTL and drop the reference we took to the old page.
2474 static int wp_page_copy(struct vm_fault
*vmf
)
2476 struct vm_area_struct
*vma
= vmf
->vma
;
2477 struct mm_struct
*mm
= vma
->vm_mm
;
2478 struct page
*old_page
= vmf
->page
;
2479 struct page
*new_page
= NULL
;
2481 int page_copied
= 0;
2482 const unsigned long mmun_start
= vmf
->address
& PAGE_MASK
;
2483 const unsigned long mmun_end
= mmun_start
+ PAGE_SIZE
;
2484 struct mem_cgroup
*memcg
;
2486 if (unlikely(anon_vma_prepare(vma
)))
2489 if (is_zero_pfn(pte_pfn(vmf
->orig_pte
))) {
2490 new_page
= alloc_zeroed_user_highpage_movable(vma
,
2495 new_page
= alloc_page_vma(GFP_HIGHUSER_MOVABLE
, vma
,
2499 cow_user_page(new_page
, old_page
, vmf
->address
, vma
);
2502 if (mem_cgroup_try_charge(new_page
, mm
, GFP_KERNEL
, &memcg
, false))
2505 __SetPageUptodate(new_page
);
2507 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
2510 * Re-check the pte - we dropped the lock
2512 vmf
->pte
= pte_offset_map_lock(mm
, vmf
->pmd
, vmf
->address
, &vmf
->ptl
);
2513 if (likely(pte_same(*vmf
->pte
, vmf
->orig_pte
))) {
2515 if (!PageAnon(old_page
)) {
2516 dec_mm_counter_fast(mm
,
2517 mm_counter_file(old_page
));
2518 inc_mm_counter_fast(mm
, MM_ANONPAGES
);
2521 inc_mm_counter_fast(mm
, MM_ANONPAGES
);
2523 flush_cache_page(vma
, vmf
->address
, pte_pfn(vmf
->orig_pte
));
2524 entry
= mk_pte(new_page
, vma
->vm_page_prot
);
2525 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
2527 * Clear the pte entry and flush it first, before updating the
2528 * pte with the new entry. This will avoid a race condition
2529 * seen in the presence of one thread doing SMC and another
2532 ptep_clear_flush_notify(vma
, vmf
->address
, vmf
->pte
);
2533 page_add_new_anon_rmap(new_page
, vma
, vmf
->address
, false);
2534 mem_cgroup_commit_charge(new_page
, memcg
, false, false);
2535 lru_cache_add_active_or_unevictable(new_page
, vma
);
2537 * We call the notify macro here because, when using secondary
2538 * mmu page tables (such as kvm shadow page tables), we want the
2539 * new page to be mapped directly into the secondary page table.
2541 set_pte_at_notify(mm
, vmf
->address
, vmf
->pte
, entry
);
2542 update_mmu_cache(vma
, vmf
->address
, vmf
->pte
);
2545 * Only after switching the pte to the new page may
2546 * we remove the mapcount here. Otherwise another
2547 * process may come and find the rmap count decremented
2548 * before the pte is switched to the new page, and
2549 * "reuse" the old page writing into it while our pte
2550 * here still points into it and can be read by other
2553 * The critical issue is to order this
2554 * page_remove_rmap with the ptp_clear_flush above.
2555 * Those stores are ordered by (if nothing else,)
2556 * the barrier present in the atomic_add_negative
2557 * in page_remove_rmap.
2559 * Then the TLB flush in ptep_clear_flush ensures that
2560 * no process can access the old page before the
2561 * decremented mapcount is visible. And the old page
2562 * cannot be reused until after the decremented
2563 * mapcount is visible. So transitively, TLBs to
2564 * old page will be flushed before it can be reused.
2566 page_remove_rmap(old_page
, false);
2569 /* Free the old page.. */
2570 new_page
= old_page
;
2573 mem_cgroup_cancel_charge(new_page
, memcg
, false);
2579 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
2581 * No need to double call mmu_notifier->invalidate_range() callback as
2582 * the above ptep_clear_flush_notify() did already call it.
2584 mmu_notifier_invalidate_range_only_end(mm
, mmun_start
, mmun_end
);
2587 * Don't let another task, with possibly unlocked vma,
2588 * keep the mlocked page.
2590 if (page_copied
&& (vma
->vm_flags
& VM_LOCKED
)) {
2591 lock_page(old_page
); /* LRU manipulation */
2592 if (PageMlocked(old_page
))
2593 munlock_vma_page(old_page
);
2594 unlock_page(old_page
);
2598 return page_copied
? VM_FAULT_WRITE
: 0;
2604 return VM_FAULT_OOM
;
2608 * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE
2609 * writeable once the page is prepared
2611 * @vmf: structure describing the fault
2613 * This function handles all that is needed to finish a write page fault in a
2614 * shared mapping due to PTE being read-only once the mapped page is prepared.
2615 * It handles locking of PTE and modifying it. The function returns
2616 * VM_FAULT_WRITE on success, 0 when PTE got changed before we acquired PTE
2619 * The function expects the page to be locked or other protection against
2620 * concurrent faults / writeback (such as DAX radix tree locks).
2622 int finish_mkwrite_fault(struct vm_fault
*vmf
)
2624 WARN_ON_ONCE(!(vmf
->vma
->vm_flags
& VM_SHARED
));
2625 vmf
->pte
= pte_offset_map_lock(vmf
->vma
->vm_mm
, vmf
->pmd
, vmf
->address
,
2628 * We might have raced with another page fault while we released the
2629 * pte_offset_map_lock.
2631 if (!pte_same(*vmf
->pte
, vmf
->orig_pte
)) {
2632 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
2633 return VM_FAULT_NOPAGE
;
2640 * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED
2643 static int wp_pfn_shared(struct vm_fault
*vmf
)
2645 struct vm_area_struct
*vma
= vmf
->vma
;
2647 if (vma
->vm_ops
&& vma
->vm_ops
->pfn_mkwrite
) {
2650 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
2651 vmf
->flags
|= FAULT_FLAG_MKWRITE
;
2652 ret
= vma
->vm_ops
->pfn_mkwrite(vmf
);
2653 if (ret
& (VM_FAULT_ERROR
| VM_FAULT_NOPAGE
))
2655 return finish_mkwrite_fault(vmf
);
2658 return VM_FAULT_WRITE
;
2661 static int wp_page_shared(struct vm_fault
*vmf
)
2662 __releases(vmf
->ptl
)
2664 struct vm_area_struct
*vma
= vmf
->vma
;
2666 get_page(vmf
->page
);
2668 if (vma
->vm_ops
&& vma
->vm_ops
->page_mkwrite
) {
2671 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
2672 tmp
= do_page_mkwrite(vmf
);
2673 if (unlikely(!tmp
|| (tmp
&
2674 (VM_FAULT_ERROR
| VM_FAULT_NOPAGE
)))) {
2675 put_page(vmf
->page
);
2678 tmp
= finish_mkwrite_fault(vmf
);
2679 if (unlikely(tmp
& (VM_FAULT_ERROR
| VM_FAULT_NOPAGE
))) {
2680 unlock_page(vmf
->page
);
2681 put_page(vmf
->page
);
2686 lock_page(vmf
->page
);
2688 fault_dirty_shared_page(vma
, vmf
->page
);
2689 put_page(vmf
->page
);
2691 return VM_FAULT_WRITE
;
2695 * This routine handles present pages, when users try to write
2696 * to a shared page. It is done by copying the page to a new address
2697 * and decrementing the shared-page counter for the old page.
2699 * Note that this routine assumes that the protection checks have been
2700 * done by the caller (the low-level page fault routine in most cases).
2701 * Thus we can safely just mark it writable once we've done any necessary
2704 * We also mark the page dirty at this point even though the page will
2705 * change only once the write actually happens. This avoids a few races,
2706 * and potentially makes it more efficient.
2708 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2709 * but allow concurrent faults), with pte both mapped and locked.
2710 * We return with mmap_sem still held, but pte unmapped and unlocked.
2712 static int do_wp_page(struct vm_fault
*vmf
)
2713 __releases(vmf
->ptl
)
2715 struct vm_area_struct
*vma
= vmf
->vma
;
2717 vmf
->page
= vm_normal_page(vma
, vmf
->address
, vmf
->orig_pte
);
2720 * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
2723 * We should not cow pages in a shared writeable mapping.
2724 * Just mark the pages writable and/or call ops->pfn_mkwrite.
2726 if ((vma
->vm_flags
& (VM_WRITE
|VM_SHARED
)) ==
2727 (VM_WRITE
|VM_SHARED
))
2728 return wp_pfn_shared(vmf
);
2730 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
2731 return wp_page_copy(vmf
);
2735 * Take out anonymous pages first, anonymous shared vmas are
2736 * not dirty accountable.
2738 if (PageAnon(vmf
->page
) && !PageKsm(vmf
->page
)) {
2739 int total_map_swapcount
;
2740 if (!trylock_page(vmf
->page
)) {
2741 get_page(vmf
->page
);
2742 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
2743 lock_page(vmf
->page
);
2744 vmf
->pte
= pte_offset_map_lock(vma
->vm_mm
, vmf
->pmd
,
2745 vmf
->address
, &vmf
->ptl
);
2746 if (!pte_same(*vmf
->pte
, vmf
->orig_pte
)) {
2747 unlock_page(vmf
->page
);
2748 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
2749 put_page(vmf
->page
);
2752 put_page(vmf
->page
);
2754 if (reuse_swap_page(vmf
->page
, &total_map_swapcount
)) {
2755 if (total_map_swapcount
== 1) {
2757 * The page is all ours. Move it to
2758 * our anon_vma so the rmap code will
2759 * not search our parent or siblings.
2760 * Protected against the rmap code by
2763 page_move_anon_rmap(vmf
->page
, vma
);
2765 unlock_page(vmf
->page
);
2767 return VM_FAULT_WRITE
;
2769 unlock_page(vmf
->page
);
2770 } else if (unlikely((vma
->vm_flags
& (VM_WRITE
|VM_SHARED
)) ==
2771 (VM_WRITE
|VM_SHARED
))) {
2772 return wp_page_shared(vmf
);
2776 * Ok, we need to copy. Oh, well..
2778 get_page(vmf
->page
);
2780 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
2781 return wp_page_copy(vmf
);
2784 static void unmap_mapping_range_vma(struct vm_area_struct
*vma
,
2785 unsigned long start_addr
, unsigned long end_addr
,
2786 struct zap_details
*details
)
2788 zap_page_range_single(vma
, start_addr
, end_addr
- start_addr
, details
);
2791 static inline void unmap_mapping_range_tree(struct rb_root_cached
*root
,
2792 struct zap_details
*details
)
2794 struct vm_area_struct
*vma
;
2795 pgoff_t vba
, vea
, zba
, zea
;
2797 vma_interval_tree_foreach(vma
, root
,
2798 details
->first_index
, details
->last_index
) {
2800 vba
= vma
->vm_pgoff
;
2801 vea
= vba
+ vma_pages(vma
) - 1;
2802 zba
= details
->first_index
;
2805 zea
= details
->last_index
;
2809 unmap_mapping_range_vma(vma
,
2810 ((zba
- vba
) << PAGE_SHIFT
) + vma
->vm_start
,
2811 ((zea
- vba
+ 1) << PAGE_SHIFT
) + vma
->vm_start
,
2817 * unmap_mapping_range - unmap the portion of all mmaps in the specified
2818 * address_space corresponding to the specified page range in the underlying
2821 * @mapping: the address space containing mmaps to be unmapped.
2822 * @holebegin: byte in first page to unmap, relative to the start of
2823 * the underlying file. This will be rounded down to a PAGE_SIZE
2824 * boundary. Note that this is different from truncate_pagecache(), which
2825 * must keep the partial page. In contrast, we must get rid of
2827 * @holelen: size of prospective hole in bytes. This will be rounded
2828 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
2830 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
2831 * but 0 when invalidating pagecache, don't throw away private data.
2833 void unmap_mapping_range(struct address_space
*mapping
,
2834 loff_t
const holebegin
, loff_t
const holelen
, int even_cows
)
2836 struct zap_details details
= { };
2837 pgoff_t hba
= holebegin
>> PAGE_SHIFT
;
2838 pgoff_t hlen
= (holelen
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
2840 /* Check for overflow. */
2841 if (sizeof(holelen
) > sizeof(hlen
)) {
2843 (holebegin
+ holelen
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
2844 if (holeend
& ~(long long)ULONG_MAX
)
2845 hlen
= ULONG_MAX
- hba
+ 1;
2848 details
.check_mapping
= even_cows
? NULL
: mapping
;
2849 details
.first_index
= hba
;
2850 details
.last_index
= hba
+ hlen
- 1;
2851 if (details
.last_index
< details
.first_index
)
2852 details
.last_index
= ULONG_MAX
;
2854 i_mmap_lock_write(mapping
);
2855 if (unlikely(!RB_EMPTY_ROOT(&mapping
->i_mmap
.rb_root
)))
2856 unmap_mapping_range_tree(&mapping
->i_mmap
, &details
);
2857 i_mmap_unlock_write(mapping
);
2859 EXPORT_SYMBOL(unmap_mapping_range
);
2862 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2863 * but allow concurrent faults), and pte mapped but not yet locked.
2864 * We return with pte unmapped and unlocked.
2866 * We return with the mmap_sem locked or unlocked in the same cases
2867 * as does filemap_fault().
2869 int do_swap_page(struct vm_fault
*vmf
)
2871 struct vm_area_struct
*vma
= vmf
->vma
;
2872 struct page
*page
= NULL
, *swapcache
= NULL
;
2873 struct mem_cgroup
*memcg
;
2874 struct vma_swap_readahead swap_ra
;
2880 bool vma_readahead
= swap_use_vma_readahead();
2882 if (vma_readahead
) {
2883 page
= swap_readahead_detect(vmf
, &swap_ra
);
2887 if (!pte_unmap_same(vma
->vm_mm
, vmf
->pmd
, vmf
->pte
, vmf
->orig_pte
)) {
2893 entry
= pte_to_swp_entry(vmf
->orig_pte
);
2894 if (unlikely(non_swap_entry(entry
))) {
2895 if (is_migration_entry(entry
)) {
2896 migration_entry_wait(vma
->vm_mm
, vmf
->pmd
,
2898 } else if (is_device_private_entry(entry
)) {
2900 * For un-addressable device memory we call the pgmap
2901 * fault handler callback. The callback must migrate
2902 * the page back to some CPU accessible page.
2904 ret
= device_private_entry_fault(vma
, vmf
->address
, entry
,
2905 vmf
->flags
, vmf
->pmd
);
2906 } else if (is_hwpoison_entry(entry
)) {
2907 ret
= VM_FAULT_HWPOISON
;
2909 print_bad_pte(vma
, vmf
->address
, vmf
->orig_pte
, NULL
);
2910 ret
= VM_FAULT_SIGBUS
;
2916 delayacct_set_flag(DELAYACCT_PF_SWAPIN
);
2918 page
= lookup_swap_cache(entry
, vma_readahead
? vma
: NULL
,
2924 struct swap_info_struct
*si
= swp_swap_info(entry
);
2926 if (si
->flags
& SWP_SYNCHRONOUS_IO
&&
2927 __swap_count(si
, entry
) == 1) {
2928 /* skip swapcache */
2929 page
= alloc_page_vma(GFP_HIGHUSER_MOVABLE
, vma
, vmf
->address
);
2931 __SetPageLocked(page
);
2932 __SetPageSwapBacked(page
);
2933 set_page_private(page
, entry
.val
);
2934 lru_cache_add_anon(page
);
2935 swap_readpage(page
, true);
2939 page
= do_swap_page_readahead(entry
,
2940 GFP_HIGHUSER_MOVABLE
, vmf
, &swap_ra
);
2942 page
= swapin_readahead(entry
,
2943 GFP_HIGHUSER_MOVABLE
, vma
, vmf
->address
);
2949 * Back out if somebody else faulted in this pte
2950 * while we released the pte lock.
2952 vmf
->pte
= pte_offset_map_lock(vma
->vm_mm
, vmf
->pmd
,
2953 vmf
->address
, &vmf
->ptl
);
2954 if (likely(pte_same(*vmf
->pte
, vmf
->orig_pte
)))
2956 delayacct_clear_flag(DELAYACCT_PF_SWAPIN
);
2960 /* Had to read the page from swap area: Major fault */
2961 ret
= VM_FAULT_MAJOR
;
2962 count_vm_event(PGMAJFAULT
);
2963 count_memcg_event_mm(vma
->vm_mm
, PGMAJFAULT
);
2964 } else if (PageHWPoison(page
)) {
2966 * hwpoisoned dirty swapcache pages are kept for killing
2967 * owner processes (which may be unknown at hwpoison time)
2969 ret
= VM_FAULT_HWPOISON
;
2970 delayacct_clear_flag(DELAYACCT_PF_SWAPIN
);
2975 locked
= lock_page_or_retry(page
, vma
->vm_mm
, vmf
->flags
);
2977 delayacct_clear_flag(DELAYACCT_PF_SWAPIN
);
2979 ret
|= VM_FAULT_RETRY
;
2984 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
2985 * release the swapcache from under us. The page pin, and pte_same
2986 * test below, are not enough to exclude that. Even if it is still
2987 * swapcache, we need to check that the page's swap has not changed.
2989 if (unlikely((!PageSwapCache(page
) ||
2990 page_private(page
) != entry
.val
)) && swapcache
)
2993 page
= ksm_might_need_to_copy(page
, vma
, vmf
->address
);
2994 if (unlikely(!page
)) {
3000 if (mem_cgroup_try_charge(page
, vma
->vm_mm
, GFP_KERNEL
,
3007 * Back out if somebody else already faulted in this pte.
3009 vmf
->pte
= pte_offset_map_lock(vma
->vm_mm
, vmf
->pmd
, vmf
->address
,
3011 if (unlikely(!pte_same(*vmf
->pte
, vmf
->orig_pte
)))
3014 if (unlikely(!PageUptodate(page
))) {
3015 ret
= VM_FAULT_SIGBUS
;
3020 * The page isn't present yet, go ahead with the fault.
3022 * Be careful about the sequence of operations here.
3023 * To get its accounting right, reuse_swap_page() must be called
3024 * while the page is counted on swap but not yet in mapcount i.e.
3025 * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
3026 * must be called after the swap_free(), or it will never succeed.
3029 inc_mm_counter_fast(vma
->vm_mm
, MM_ANONPAGES
);
3030 dec_mm_counter_fast(vma
->vm_mm
, MM_SWAPENTS
);
3031 pte
= mk_pte(page
, vma
->vm_page_prot
);
3032 if ((vmf
->flags
& FAULT_FLAG_WRITE
) && reuse_swap_page(page
, NULL
)) {
3033 pte
= maybe_mkwrite(pte_mkdirty(pte
), vma
);
3034 vmf
->flags
&= ~FAULT_FLAG_WRITE
;
3035 ret
|= VM_FAULT_WRITE
;
3036 exclusive
= RMAP_EXCLUSIVE
;
3038 flush_icache_page(vma
, page
);
3039 if (pte_swp_soft_dirty(vmf
->orig_pte
))
3040 pte
= pte_mksoft_dirty(pte
);
3041 set_pte_at(vma
->vm_mm
, vmf
->address
, vmf
->pte
, pte
);
3042 vmf
->orig_pte
= pte
;
3044 /* ksm created a completely new copy */
3045 if (unlikely(page
!= swapcache
&& swapcache
)) {
3046 page_add_new_anon_rmap(page
, vma
, vmf
->address
, false);
3047 mem_cgroup_commit_charge(page
, memcg
, false, false);
3048 lru_cache_add_active_or_unevictable(page
, vma
);
3050 do_page_add_anon_rmap(page
, vma
, vmf
->address
, exclusive
);
3051 mem_cgroup_commit_charge(page
, memcg
, true, false);
3052 activate_page(page
);
3056 if (mem_cgroup_swap_full(page
) ||
3057 (vma
->vm_flags
& VM_LOCKED
) || PageMlocked(page
))
3058 try_to_free_swap(page
);
3060 if (page
!= swapcache
&& swapcache
) {
3062 * Hold the lock to avoid the swap entry to be reused
3063 * until we take the PT lock for the pte_same() check
3064 * (to avoid false positives from pte_same). For
3065 * further safety release the lock after the swap_free
3066 * so that the swap count won't change under a
3067 * parallel locked swapcache.
3069 unlock_page(swapcache
);
3070 put_page(swapcache
);
3073 if (vmf
->flags
& FAULT_FLAG_WRITE
) {
3074 ret
|= do_wp_page(vmf
);
3075 if (ret
& VM_FAULT_ERROR
)
3076 ret
&= VM_FAULT_ERROR
;
3080 /* No need to invalidate - it was non-present before */
3081 update_mmu_cache(vma
, vmf
->address
, vmf
->pte
);
3083 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
3087 mem_cgroup_cancel_charge(page
, memcg
, false);
3088 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
3093 if (page
!= swapcache
&& swapcache
) {
3094 unlock_page(swapcache
);
3095 put_page(swapcache
);
3101 * We enter with non-exclusive mmap_sem (to exclude vma changes,
3102 * but allow concurrent faults), and pte mapped but not yet locked.
3103 * We return with mmap_sem still held, but pte unmapped and unlocked.
3105 static int do_anonymous_page(struct vm_fault
*vmf
)
3107 struct vm_area_struct
*vma
= vmf
->vma
;
3108 struct mem_cgroup
*memcg
;
3113 /* File mapping without ->vm_ops ? */
3114 if (vma
->vm_flags
& VM_SHARED
)
3115 return VM_FAULT_SIGBUS
;
3118 * Use pte_alloc() instead of pte_alloc_map(). We can't run
3119 * pte_offset_map() on pmds where a huge pmd might be created
3120 * from a different thread.
3122 * pte_alloc_map() is safe to use under down_write(mmap_sem) or when
3123 * parallel threads are excluded by other means.
3125 * Here we only have down_read(mmap_sem).
3127 if (pte_alloc(vma
->vm_mm
, vmf
->pmd
, vmf
->address
))
3128 return VM_FAULT_OOM
;
3130 /* See the comment in pte_alloc_one_map() */
3131 if (unlikely(pmd_trans_unstable(vmf
->pmd
)))
3134 /* Use the zero-page for reads */
3135 if (!(vmf
->flags
& FAULT_FLAG_WRITE
) &&
3136 !mm_forbids_zeropage(vma
->vm_mm
)) {
3137 entry
= pte_mkspecial(pfn_pte(my_zero_pfn(vmf
->address
),
3138 vma
->vm_page_prot
));
3139 vmf
->pte
= pte_offset_map_lock(vma
->vm_mm
, vmf
->pmd
,
3140 vmf
->address
, &vmf
->ptl
);
3141 if (!pte_none(*vmf
->pte
))
3143 ret
= check_stable_address_space(vma
->vm_mm
);
3146 /* Deliver the page fault to userland, check inside PT lock */
3147 if (userfaultfd_missing(vma
)) {
3148 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
3149 return handle_userfault(vmf
, VM_UFFD_MISSING
);
3154 /* Allocate our own private page. */
3155 if (unlikely(anon_vma_prepare(vma
)))
3157 page
= alloc_zeroed_user_highpage_movable(vma
, vmf
->address
);
3161 if (mem_cgroup_try_charge(page
, vma
->vm_mm
, GFP_KERNEL
, &memcg
, false))
3165 * The memory barrier inside __SetPageUptodate makes sure that
3166 * preceeding stores to the page contents become visible before
3167 * the set_pte_at() write.
3169 __SetPageUptodate(page
);
3171 entry
= mk_pte(page
, vma
->vm_page_prot
);
3172 if (vma
->vm_flags
& VM_WRITE
)
3173 entry
= pte_mkwrite(pte_mkdirty(entry
));
3175 vmf
->pte
= pte_offset_map_lock(vma
->vm_mm
, vmf
->pmd
, vmf
->address
,
3177 if (!pte_none(*vmf
->pte
))
3180 ret
= check_stable_address_space(vma
->vm_mm
);
3184 /* Deliver the page fault to userland, check inside PT lock */
3185 if (userfaultfd_missing(vma
)) {
3186 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
3187 mem_cgroup_cancel_charge(page
, memcg
, false);
3189 return handle_userfault(vmf
, VM_UFFD_MISSING
);
3192 inc_mm_counter_fast(vma
->vm_mm
, MM_ANONPAGES
);
3193 page_add_new_anon_rmap(page
, vma
, vmf
->address
, false);
3194 mem_cgroup_commit_charge(page
, memcg
, false, false);
3195 lru_cache_add_active_or_unevictable(page
, vma
);
3197 set_pte_at(vma
->vm_mm
, vmf
->address
, vmf
->pte
, entry
);
3199 /* No need to invalidate - it was non-present before */
3200 update_mmu_cache(vma
, vmf
->address
, vmf
->pte
);
3202 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
3205 mem_cgroup_cancel_charge(page
, memcg
, false);
3211 return VM_FAULT_OOM
;
3215 * The mmap_sem must have been held on entry, and may have been
3216 * released depending on flags and vma->vm_ops->fault() return value.
3217 * See filemap_fault() and __lock_page_retry().
3219 static int __do_fault(struct vm_fault
*vmf
)
3221 struct vm_area_struct
*vma
= vmf
->vma
;
3224 ret
= vma
->vm_ops
->fault(vmf
);
3225 if (unlikely(ret
& (VM_FAULT_ERROR
| VM_FAULT_NOPAGE
| VM_FAULT_RETRY
|
3226 VM_FAULT_DONE_COW
)))
3229 if (unlikely(PageHWPoison(vmf
->page
))) {
3230 if (ret
& VM_FAULT_LOCKED
)
3231 unlock_page(vmf
->page
);
3232 put_page(vmf
->page
);
3234 return VM_FAULT_HWPOISON
;
3237 if (unlikely(!(ret
& VM_FAULT_LOCKED
)))
3238 lock_page(vmf
->page
);
3240 VM_BUG_ON_PAGE(!PageLocked(vmf
->page
), vmf
->page
);
3246 * The ordering of these checks is important for pmds with _PAGE_DEVMAP set.
3247 * If we check pmd_trans_unstable() first we will trip the bad_pmd() check
3248 * inside of pmd_none_or_trans_huge_or_clear_bad(). This will end up correctly
3249 * returning 1 but not before it spams dmesg with the pmd_clear_bad() output.
3251 static int pmd_devmap_trans_unstable(pmd_t
*pmd
)
3253 return pmd_devmap(*pmd
) || pmd_trans_unstable(pmd
);
3256 static int pte_alloc_one_map(struct vm_fault
*vmf
)
3258 struct vm_area_struct
*vma
= vmf
->vma
;
3260 if (!pmd_none(*vmf
->pmd
))
3262 if (vmf
->prealloc_pte
) {
3263 vmf
->ptl
= pmd_lock(vma
->vm_mm
, vmf
->pmd
);
3264 if (unlikely(!pmd_none(*vmf
->pmd
))) {
3265 spin_unlock(vmf
->ptl
);
3269 mm_inc_nr_ptes(vma
->vm_mm
);
3270 pmd_populate(vma
->vm_mm
, vmf
->pmd
, vmf
->prealloc_pte
);
3271 spin_unlock(vmf
->ptl
);
3272 vmf
->prealloc_pte
= NULL
;
3273 } else if (unlikely(pte_alloc(vma
->vm_mm
, vmf
->pmd
, vmf
->address
))) {
3274 return VM_FAULT_OOM
;
3278 * If a huge pmd materialized under us just retry later. Use
3279 * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead of
3280 * pmd_trans_huge() to ensure the pmd didn't become pmd_trans_huge
3281 * under us and then back to pmd_none, as a result of MADV_DONTNEED
3282 * running immediately after a huge pmd fault in a different thread of
3283 * this mm, in turn leading to a misleading pmd_trans_huge() retval.
3284 * All we have to ensure is that it is a regular pmd that we can walk
3285 * with pte_offset_map() and we can do that through an atomic read in
3286 * C, which is what pmd_trans_unstable() provides.
3288 if (pmd_devmap_trans_unstable(vmf
->pmd
))
3289 return VM_FAULT_NOPAGE
;
3292 * At this point we know that our vmf->pmd points to a page of ptes
3293 * and it cannot become pmd_none(), pmd_devmap() or pmd_trans_huge()
3294 * for the duration of the fault. If a racing MADV_DONTNEED runs and
3295 * we zap the ptes pointed to by our vmf->pmd, the vmf->ptl will still
3296 * be valid and we will re-check to make sure the vmf->pte isn't
3297 * pte_none() under vmf->ptl protection when we return to
3300 vmf
->pte
= pte_offset_map_lock(vma
->vm_mm
, vmf
->pmd
, vmf
->address
,
3305 #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3307 #define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1)
3308 static inline bool transhuge_vma_suitable(struct vm_area_struct
*vma
,
3309 unsigned long haddr
)
3311 if (((vma
->vm_start
>> PAGE_SHIFT
) & HPAGE_CACHE_INDEX_MASK
) !=
3312 (vma
->vm_pgoff
& HPAGE_CACHE_INDEX_MASK
))
3314 if (haddr
< vma
->vm_start
|| haddr
+ HPAGE_PMD_SIZE
> vma
->vm_end
)
3319 static void deposit_prealloc_pte(struct vm_fault
*vmf
)
3321 struct vm_area_struct
*vma
= vmf
->vma
;
3323 pgtable_trans_huge_deposit(vma
->vm_mm
, vmf
->pmd
, vmf
->prealloc_pte
);
3325 * We are going to consume the prealloc table,
3326 * count that as nr_ptes.
3328 mm_inc_nr_ptes(vma
->vm_mm
);
3329 vmf
->prealloc_pte
= NULL
;
3332 static int do_set_pmd(struct vm_fault
*vmf
, struct page
*page
)
3334 struct vm_area_struct
*vma
= vmf
->vma
;
3335 bool write
= vmf
->flags
& FAULT_FLAG_WRITE
;
3336 unsigned long haddr
= vmf
->address
& HPAGE_PMD_MASK
;
3340 if (!transhuge_vma_suitable(vma
, haddr
))
3341 return VM_FAULT_FALLBACK
;
3343 ret
= VM_FAULT_FALLBACK
;
3344 page
= compound_head(page
);
3347 * Archs like ppc64 need additonal space to store information
3348 * related to pte entry. Use the preallocated table for that.
3350 if (arch_needs_pgtable_deposit() && !vmf
->prealloc_pte
) {
3351 vmf
->prealloc_pte
= pte_alloc_one(vma
->vm_mm
, vmf
->address
);
3352 if (!vmf
->prealloc_pte
)
3353 return VM_FAULT_OOM
;
3354 smp_wmb(); /* See comment in __pte_alloc() */
3357 vmf
->ptl
= pmd_lock(vma
->vm_mm
, vmf
->pmd
);
3358 if (unlikely(!pmd_none(*vmf
->pmd
)))
3361 for (i
= 0; i
< HPAGE_PMD_NR
; i
++)
3362 flush_icache_page(vma
, page
+ i
);
3364 entry
= mk_huge_pmd(page
, vma
->vm_page_prot
);
3366 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
3368 add_mm_counter(vma
->vm_mm
, MM_FILEPAGES
, HPAGE_PMD_NR
);
3369 page_add_file_rmap(page
, true);
3371 * deposit and withdraw with pmd lock held
3373 if (arch_needs_pgtable_deposit())
3374 deposit_prealloc_pte(vmf
);
3376 set_pmd_at(vma
->vm_mm
, haddr
, vmf
->pmd
, entry
);
3378 update_mmu_cache_pmd(vma
, haddr
, vmf
->pmd
);
3380 /* fault is handled */
3382 count_vm_event(THP_FILE_MAPPED
);
3384 spin_unlock(vmf
->ptl
);
3388 static int do_set_pmd(struct vm_fault
*vmf
, struct page
*page
)
3396 * alloc_set_pte - setup new PTE entry for given page and add reverse page
3397 * mapping. If needed, the fucntion allocates page table or use pre-allocated.
3399 * @vmf: fault environment
3400 * @memcg: memcg to charge page (only for private mappings)
3401 * @page: page to map
3403 * Caller must take care of unlocking vmf->ptl, if vmf->pte is non-NULL on
3406 * Target users are page handler itself and implementations of
3407 * vm_ops->map_pages.
3409 int alloc_set_pte(struct vm_fault
*vmf
, struct mem_cgroup
*memcg
,
3412 struct vm_area_struct
*vma
= vmf
->vma
;
3413 bool write
= vmf
->flags
& FAULT_FLAG_WRITE
;
3417 if (pmd_none(*vmf
->pmd
) && PageTransCompound(page
) &&
3418 IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE
)) {
3420 VM_BUG_ON_PAGE(memcg
, page
);
3422 ret
= do_set_pmd(vmf
, page
);
3423 if (ret
!= VM_FAULT_FALLBACK
)
3428 ret
= pte_alloc_one_map(vmf
);
3433 /* Re-check under ptl */
3434 if (unlikely(!pte_none(*vmf
->pte
)))
3435 return VM_FAULT_NOPAGE
;
3437 flush_icache_page(vma
, page
);
3438 entry
= mk_pte(page
, vma
->vm_page_prot
);
3440 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
3441 /* copy-on-write page */
3442 if (write
&& !(vma
->vm_flags
& VM_SHARED
)) {
3443 inc_mm_counter_fast(vma
->vm_mm
, MM_ANONPAGES
);
3444 page_add_new_anon_rmap(page
, vma
, vmf
->address
, false);
3445 mem_cgroup_commit_charge(page
, memcg
, false, false);
3446 lru_cache_add_active_or_unevictable(page
, vma
);
3448 inc_mm_counter_fast(vma
->vm_mm
, mm_counter_file(page
));
3449 page_add_file_rmap(page
, false);
3451 set_pte_at(vma
->vm_mm
, vmf
->address
, vmf
->pte
, entry
);
3453 /* no need to invalidate: a not-present page won't be cached */
3454 update_mmu_cache(vma
, vmf
->address
, vmf
->pte
);
3461 * finish_fault - finish page fault once we have prepared the page to fault
3463 * @vmf: structure describing the fault
3465 * This function handles all that is needed to finish a page fault once the
3466 * page to fault in is prepared. It handles locking of PTEs, inserts PTE for
3467 * given page, adds reverse page mapping, handles memcg charges and LRU
3468 * addition. The function returns 0 on success, VM_FAULT_ code in case of
3471 * The function expects the page to be locked and on success it consumes a
3472 * reference of a page being mapped (for the PTE which maps it).
3474 int finish_fault(struct vm_fault
*vmf
)
3479 /* Did we COW the page? */
3480 if ((vmf
->flags
& FAULT_FLAG_WRITE
) &&
3481 !(vmf
->vma
->vm_flags
& VM_SHARED
))
3482 page
= vmf
->cow_page
;
3487 * check even for read faults because we might have lost our CoWed
3490 if (!(vmf
->vma
->vm_flags
& VM_SHARED
))
3491 ret
= check_stable_address_space(vmf
->vma
->vm_mm
);
3493 ret
= alloc_set_pte(vmf
, vmf
->memcg
, page
);
3495 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
3499 static unsigned long fault_around_bytes __read_mostly
=
3500 rounddown_pow_of_two(65536);
3502 #ifdef CONFIG_DEBUG_FS
3503 static int fault_around_bytes_get(void *data
, u64
*val
)
3505 *val
= fault_around_bytes
;
3510 * fault_around_pages() and fault_around_mask() expects fault_around_bytes
3511 * rounded down to nearest page order. It's what do_fault_around() expects to
3514 static int fault_around_bytes_set(void *data
, u64 val
)
3516 if (val
/ PAGE_SIZE
> PTRS_PER_PTE
)
3518 if (val
> PAGE_SIZE
)
3519 fault_around_bytes
= rounddown_pow_of_two(val
);
3521 fault_around_bytes
= PAGE_SIZE
; /* rounddown_pow_of_two(0) is undefined */
3524 DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops
,
3525 fault_around_bytes_get
, fault_around_bytes_set
, "%llu\n");
3527 static int __init
fault_around_debugfs(void)
3531 ret
= debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL
, NULL
,
3532 &fault_around_bytes_fops
);
3534 pr_warn("Failed to create fault_around_bytes in debugfs");
3537 late_initcall(fault_around_debugfs
);
3541 * do_fault_around() tries to map few pages around the fault address. The hope
3542 * is that the pages will be needed soon and this will lower the number of
3545 * It uses vm_ops->map_pages() to map the pages, which skips the page if it's
3546 * not ready to be mapped: not up-to-date, locked, etc.
3548 * This function is called with the page table lock taken. In the split ptlock
3549 * case the page table lock only protects only those entries which belong to
3550 * the page table corresponding to the fault address.
3552 * This function doesn't cross the VMA boundaries, in order to call map_pages()
3555 * fault_around_pages() defines how many pages we'll try to map.
3556 * do_fault_around() expects it to return a power of two less than or equal to
3559 * The virtual address of the area that we map is naturally aligned to the
3560 * fault_around_pages() value (and therefore to page order). This way it's
3561 * easier to guarantee that we don't cross page table boundaries.
3563 static int do_fault_around(struct vm_fault
*vmf
)
3565 unsigned long address
= vmf
->address
, nr_pages
, mask
;
3566 pgoff_t start_pgoff
= vmf
->pgoff
;
3570 nr_pages
= READ_ONCE(fault_around_bytes
) >> PAGE_SHIFT
;
3571 mask
= ~(nr_pages
* PAGE_SIZE
- 1) & PAGE_MASK
;
3573 vmf
->address
= max(address
& mask
, vmf
->vma
->vm_start
);
3574 off
= ((address
- vmf
->address
) >> PAGE_SHIFT
) & (PTRS_PER_PTE
- 1);
3578 * end_pgoff is either end of page table or end of vma
3579 * or fault_around_pages() from start_pgoff, depending what is nearest.
3581 end_pgoff
= start_pgoff
-
3582 ((vmf
->address
>> PAGE_SHIFT
) & (PTRS_PER_PTE
- 1)) +
3584 end_pgoff
= min3(end_pgoff
, vma_pages(vmf
->vma
) + vmf
->vma
->vm_pgoff
- 1,
3585 start_pgoff
+ nr_pages
- 1);
3587 if (pmd_none(*vmf
->pmd
)) {
3588 vmf
->prealloc_pte
= pte_alloc_one(vmf
->vma
->vm_mm
,
3590 if (!vmf
->prealloc_pte
)
3592 smp_wmb(); /* See comment in __pte_alloc() */
3595 vmf
->vma
->vm_ops
->map_pages(vmf
, start_pgoff
, end_pgoff
);
3597 /* Huge page is mapped? Page fault is solved */
3598 if (pmd_trans_huge(*vmf
->pmd
)) {
3599 ret
= VM_FAULT_NOPAGE
;
3603 /* ->map_pages() haven't done anything useful. Cold page cache? */
3607 /* check if the page fault is solved */
3608 vmf
->pte
-= (vmf
->address
>> PAGE_SHIFT
) - (address
>> PAGE_SHIFT
);
3609 if (!pte_none(*vmf
->pte
))
3610 ret
= VM_FAULT_NOPAGE
;
3611 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
3613 vmf
->address
= address
;
3618 static int do_read_fault(struct vm_fault
*vmf
)
3620 struct vm_area_struct
*vma
= vmf
->vma
;
3624 * Let's call ->map_pages() first and use ->fault() as fallback
3625 * if page by the offset is not ready to be mapped (cold cache or
3628 if (vma
->vm_ops
->map_pages
&& fault_around_bytes
>> PAGE_SHIFT
> 1) {
3629 ret
= do_fault_around(vmf
);
3634 ret
= __do_fault(vmf
);
3635 if (unlikely(ret
& (VM_FAULT_ERROR
| VM_FAULT_NOPAGE
| VM_FAULT_RETRY
)))
3638 ret
|= finish_fault(vmf
);
3639 unlock_page(vmf
->page
);
3640 if (unlikely(ret
& (VM_FAULT_ERROR
| VM_FAULT_NOPAGE
| VM_FAULT_RETRY
)))
3641 put_page(vmf
->page
);
3645 static int do_cow_fault(struct vm_fault
*vmf
)
3647 struct vm_area_struct
*vma
= vmf
->vma
;
3650 if (unlikely(anon_vma_prepare(vma
)))
3651 return VM_FAULT_OOM
;
3653 vmf
->cow_page
= alloc_page_vma(GFP_HIGHUSER_MOVABLE
, vma
, vmf
->address
);
3655 return VM_FAULT_OOM
;
3657 if (mem_cgroup_try_charge(vmf
->cow_page
, vma
->vm_mm
, GFP_KERNEL
,
3658 &vmf
->memcg
, false)) {
3659 put_page(vmf
->cow_page
);
3660 return VM_FAULT_OOM
;
3663 ret
= __do_fault(vmf
);
3664 if (unlikely(ret
& (VM_FAULT_ERROR
| VM_FAULT_NOPAGE
| VM_FAULT_RETRY
)))
3666 if (ret
& VM_FAULT_DONE_COW
)
3669 copy_user_highpage(vmf
->cow_page
, vmf
->page
, vmf
->address
, vma
);
3670 __SetPageUptodate(vmf
->cow_page
);
3672 ret
|= finish_fault(vmf
);
3673 unlock_page(vmf
->page
);
3674 put_page(vmf
->page
);
3675 if (unlikely(ret
& (VM_FAULT_ERROR
| VM_FAULT_NOPAGE
| VM_FAULT_RETRY
)))
3679 mem_cgroup_cancel_charge(vmf
->cow_page
, vmf
->memcg
, false);
3680 put_page(vmf
->cow_page
);
3684 static int do_shared_fault(struct vm_fault
*vmf
)
3686 struct vm_area_struct
*vma
= vmf
->vma
;
3689 ret
= __do_fault(vmf
);
3690 if (unlikely(ret
& (VM_FAULT_ERROR
| VM_FAULT_NOPAGE
| VM_FAULT_RETRY
)))
3694 * Check if the backing address space wants to know that the page is
3695 * about to become writable
3697 if (vma
->vm_ops
->page_mkwrite
) {
3698 unlock_page(vmf
->page
);
3699 tmp
= do_page_mkwrite(vmf
);
3700 if (unlikely(!tmp
||
3701 (tmp
& (VM_FAULT_ERROR
| VM_FAULT_NOPAGE
)))) {
3702 put_page(vmf
->page
);
3707 ret
|= finish_fault(vmf
);
3708 if (unlikely(ret
& (VM_FAULT_ERROR
| VM_FAULT_NOPAGE
|
3710 unlock_page(vmf
->page
);
3711 put_page(vmf
->page
);
3715 fault_dirty_shared_page(vma
, vmf
->page
);
3720 * We enter with non-exclusive mmap_sem (to exclude vma changes,
3721 * but allow concurrent faults).
3722 * The mmap_sem may have been released depending on flags and our
3723 * return value. See filemap_fault() and __lock_page_or_retry().
3725 static int do_fault(struct vm_fault
*vmf
)
3727 struct vm_area_struct
*vma
= vmf
->vma
;
3730 /* The VMA was not fully populated on mmap() or missing VM_DONTEXPAND */
3731 if (!vma
->vm_ops
->fault
)
3732 ret
= VM_FAULT_SIGBUS
;
3733 else if (!(vmf
->flags
& FAULT_FLAG_WRITE
))
3734 ret
= do_read_fault(vmf
);
3735 else if (!(vma
->vm_flags
& VM_SHARED
))
3736 ret
= do_cow_fault(vmf
);
3738 ret
= do_shared_fault(vmf
);
3740 /* preallocated pagetable is unused: free it */
3741 if (vmf
->prealloc_pte
) {
3742 pte_free(vma
->vm_mm
, vmf
->prealloc_pte
);
3743 vmf
->prealloc_pte
= NULL
;
3748 static int numa_migrate_prep(struct page
*page
, struct vm_area_struct
*vma
,
3749 unsigned long addr
, int page_nid
,
3754 count_vm_numa_event(NUMA_HINT_FAULTS
);
3755 if (page_nid
== numa_node_id()) {
3756 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL
);
3757 *flags
|= TNF_FAULT_LOCAL
;
3760 return mpol_misplaced(page
, vma
, addr
);
3763 static int do_numa_page(struct vm_fault
*vmf
)
3765 struct vm_area_struct
*vma
= vmf
->vma
;
3766 struct page
*page
= NULL
;
3770 bool migrated
= false;
3772 bool was_writable
= pte_savedwrite(vmf
->orig_pte
);
3776 * The "pte" at this point cannot be used safely without
3777 * validation through pte_unmap_same(). It's of NUMA type but
3778 * the pfn may be screwed if the read is non atomic.
3780 vmf
->ptl
= pte_lockptr(vma
->vm_mm
, vmf
->pmd
);
3781 spin_lock(vmf
->ptl
);
3782 if (unlikely(!pte_same(*vmf
->pte
, vmf
->orig_pte
))) {
3783 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
3788 * Make it present again, Depending on how arch implementes non
3789 * accessible ptes, some can allow access by kernel mode.
3791 pte
= ptep_modify_prot_start(vma
->vm_mm
, vmf
->address
, vmf
->pte
);
3792 pte
= pte_modify(pte
, vma
->vm_page_prot
);
3793 pte
= pte_mkyoung(pte
);
3795 pte
= pte_mkwrite(pte
);
3796 ptep_modify_prot_commit(vma
->vm_mm
, vmf
->address
, vmf
->pte
, pte
);
3797 update_mmu_cache(vma
, vmf
->address
, vmf
->pte
);
3799 page
= vm_normal_page(vma
, vmf
->address
, pte
);
3801 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
3805 /* TODO: handle PTE-mapped THP */
3806 if (PageCompound(page
)) {
3807 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
3812 * Avoid grouping on RO pages in general. RO pages shouldn't hurt as
3813 * much anyway since they can be in shared cache state. This misses
3814 * the case where a mapping is writable but the process never writes
3815 * to it but pte_write gets cleared during protection updates and
3816 * pte_dirty has unpredictable behaviour between PTE scan updates,
3817 * background writeback, dirty balancing and application behaviour.
3819 if (!pte_write(pte
))
3820 flags
|= TNF_NO_GROUP
;
3823 * Flag if the page is shared between multiple address spaces. This
3824 * is later used when determining whether to group tasks together
3826 if (page_mapcount(page
) > 1 && (vma
->vm_flags
& VM_SHARED
))
3827 flags
|= TNF_SHARED
;
3829 last_cpupid
= page_cpupid_last(page
);
3830 page_nid
= page_to_nid(page
);
3831 target_nid
= numa_migrate_prep(page
, vma
, vmf
->address
, page_nid
,
3833 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
3834 if (target_nid
== -1) {
3839 /* Migrate to the requested node */
3840 migrated
= migrate_misplaced_page(page
, vma
, target_nid
);
3842 page_nid
= target_nid
;
3843 flags
|= TNF_MIGRATED
;
3845 flags
|= TNF_MIGRATE_FAIL
;
3849 task_numa_fault(last_cpupid
, page_nid
, 1, flags
);
3853 static inline int create_huge_pmd(struct vm_fault
*vmf
)
3855 if (vma_is_anonymous(vmf
->vma
))
3856 return do_huge_pmd_anonymous_page(vmf
);
3857 if (vmf
->vma
->vm_ops
->huge_fault
)
3858 return vmf
->vma
->vm_ops
->huge_fault(vmf
, PE_SIZE_PMD
);
3859 return VM_FAULT_FALLBACK
;
3862 /* `inline' is required to avoid gcc 4.1.2 build error */
3863 static inline int wp_huge_pmd(struct vm_fault
*vmf
, pmd_t orig_pmd
)
3865 if (vma_is_anonymous(vmf
->vma
))
3866 return do_huge_pmd_wp_page(vmf
, orig_pmd
);
3867 if (vmf
->vma
->vm_ops
->huge_fault
)
3868 return vmf
->vma
->vm_ops
->huge_fault(vmf
, PE_SIZE_PMD
);
3870 /* COW handled on pte level: split pmd */
3871 VM_BUG_ON_VMA(vmf
->vma
->vm_flags
& VM_SHARED
, vmf
->vma
);
3872 __split_huge_pmd(vmf
->vma
, vmf
->pmd
, vmf
->address
, false, NULL
);
3874 return VM_FAULT_FALLBACK
;
3877 static inline bool vma_is_accessible(struct vm_area_struct
*vma
)
3879 return vma
->vm_flags
& (VM_READ
| VM_EXEC
| VM_WRITE
);
3882 static int create_huge_pud(struct vm_fault
*vmf
)
3884 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
3885 /* No support for anonymous transparent PUD pages yet */
3886 if (vma_is_anonymous(vmf
->vma
))
3887 return VM_FAULT_FALLBACK
;
3888 if (vmf
->vma
->vm_ops
->huge_fault
)
3889 return vmf
->vma
->vm_ops
->huge_fault(vmf
, PE_SIZE_PUD
);
3890 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
3891 return VM_FAULT_FALLBACK
;
3894 static int wp_huge_pud(struct vm_fault
*vmf
, pud_t orig_pud
)
3896 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
3897 /* No support for anonymous transparent PUD pages yet */
3898 if (vma_is_anonymous(vmf
->vma
))
3899 return VM_FAULT_FALLBACK
;
3900 if (vmf
->vma
->vm_ops
->huge_fault
)
3901 return vmf
->vma
->vm_ops
->huge_fault(vmf
, PE_SIZE_PUD
);
3902 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
3903 return VM_FAULT_FALLBACK
;
3907 * These routines also need to handle stuff like marking pages dirty
3908 * and/or accessed for architectures that don't do it in hardware (most
3909 * RISC architectures). The early dirtying is also good on the i386.
3911 * There is also a hook called "update_mmu_cache()" that architectures
3912 * with external mmu caches can use to update those (ie the Sparc or
3913 * PowerPC hashed page tables that act as extended TLBs).
3915 * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow
3916 * concurrent faults).
3918 * The mmap_sem may have been released depending on flags and our return value.
3919 * See filemap_fault() and __lock_page_or_retry().
3921 static int handle_pte_fault(struct vm_fault
*vmf
)
3925 if (unlikely(pmd_none(*vmf
->pmd
))) {
3927 * Leave __pte_alloc() until later: because vm_ops->fault may
3928 * want to allocate huge page, and if we expose page table
3929 * for an instant, it will be difficult to retract from
3930 * concurrent faults and from rmap lookups.
3934 /* See comment in pte_alloc_one_map() */
3935 if (pmd_devmap_trans_unstable(vmf
->pmd
))
3938 * A regular pmd is established and it can't morph into a huge
3939 * pmd from under us anymore at this point because we hold the
3940 * mmap_sem read mode and khugepaged takes it in write mode.
3941 * So now it's safe to run pte_offset_map().
3943 vmf
->pte
= pte_offset_map(vmf
->pmd
, vmf
->address
);
3944 vmf
->orig_pte
= *vmf
->pte
;
3947 * some architectures can have larger ptes than wordsize,
3948 * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and
3949 * CONFIG_32BIT=y, so READ_ONCE cannot guarantee atomic
3950 * accesses. The code below just needs a consistent view
3951 * for the ifs and we later double check anyway with the
3952 * ptl lock held. So here a barrier will do.
3955 if (pte_none(vmf
->orig_pte
)) {
3956 pte_unmap(vmf
->pte
);
3962 if (vma_is_anonymous(vmf
->vma
))
3963 return do_anonymous_page(vmf
);
3965 return do_fault(vmf
);
3968 if (!pte_present(vmf
->orig_pte
))
3969 return do_swap_page(vmf
);
3971 if (pte_protnone(vmf
->orig_pte
) && vma_is_accessible(vmf
->vma
))
3972 return do_numa_page(vmf
);
3974 vmf
->ptl
= pte_lockptr(vmf
->vma
->vm_mm
, vmf
->pmd
);
3975 spin_lock(vmf
->ptl
);
3976 entry
= vmf
->orig_pte
;
3977 if (unlikely(!pte_same(*vmf
->pte
, entry
)))
3979 if (vmf
->flags
& FAULT_FLAG_WRITE
) {
3980 if (!pte_write(entry
))
3981 return do_wp_page(vmf
);
3982 entry
= pte_mkdirty(entry
);
3984 entry
= pte_mkyoung(entry
);
3985 if (ptep_set_access_flags(vmf
->vma
, vmf
->address
, vmf
->pte
, entry
,
3986 vmf
->flags
& FAULT_FLAG_WRITE
)) {
3987 update_mmu_cache(vmf
->vma
, vmf
->address
, vmf
->pte
);
3990 * This is needed only for protection faults but the arch code
3991 * is not yet telling us if this is a protection fault or not.
3992 * This still avoids useless tlb flushes for .text page faults
3995 if (vmf
->flags
& FAULT_FLAG_WRITE
)
3996 flush_tlb_fix_spurious_fault(vmf
->vma
, vmf
->address
);
3999 pte_unmap_unlock(vmf
->pte
, vmf
->ptl
);
4004 * By the time we get here, we already hold the mm semaphore
4006 * The mmap_sem may have been released depending on flags and our
4007 * return value. See filemap_fault() and __lock_page_or_retry().
4009 static int __handle_mm_fault(struct vm_area_struct
*vma
, unsigned long address
,
4012 struct vm_fault vmf
= {
4014 .address
= address
& PAGE_MASK
,
4016 .pgoff
= linear_page_index(vma
, address
),
4017 .gfp_mask
= __get_fault_gfp_mask(vma
),
4019 unsigned int dirty
= flags
& FAULT_FLAG_WRITE
;
4020 struct mm_struct
*mm
= vma
->vm_mm
;
4025 pgd
= pgd_offset(mm
, address
);
4026 p4d
= p4d_alloc(mm
, pgd
, address
);
4028 return VM_FAULT_OOM
;
4030 vmf
.pud
= pud_alloc(mm
, p4d
, address
);
4032 return VM_FAULT_OOM
;
4033 if (pud_none(*vmf
.pud
) && transparent_hugepage_enabled(vma
)) {
4034 ret
= create_huge_pud(&vmf
);
4035 if (!(ret
& VM_FAULT_FALLBACK
))
4038 pud_t orig_pud
= *vmf
.pud
;
4041 if (pud_trans_huge(orig_pud
) || pud_devmap(orig_pud
)) {
4043 /* NUMA case for anonymous PUDs would go here */
4045 if (dirty
&& !pud_write(orig_pud
)) {
4046 ret
= wp_huge_pud(&vmf
, orig_pud
);
4047 if (!(ret
& VM_FAULT_FALLBACK
))
4050 huge_pud_set_accessed(&vmf
, orig_pud
);
4056 vmf
.pmd
= pmd_alloc(mm
, vmf
.pud
, address
);
4058 return VM_FAULT_OOM
;
4059 if (pmd_none(*vmf
.pmd
) && transparent_hugepage_enabled(vma
)) {
4060 ret
= create_huge_pmd(&vmf
);
4061 if (!(ret
& VM_FAULT_FALLBACK
))
4064 pmd_t orig_pmd
= *vmf
.pmd
;
4067 if (unlikely(is_swap_pmd(orig_pmd
))) {
4068 VM_BUG_ON(thp_migration_supported() &&
4069 !is_pmd_migration_entry(orig_pmd
));
4070 if (is_pmd_migration_entry(orig_pmd
))
4071 pmd_migration_entry_wait(mm
, vmf
.pmd
);
4074 if (pmd_trans_huge(orig_pmd
) || pmd_devmap(orig_pmd
)) {
4075 if (pmd_protnone(orig_pmd
) && vma_is_accessible(vma
))
4076 return do_huge_pmd_numa_page(&vmf
, orig_pmd
);
4078 if (dirty
&& !pmd_write(orig_pmd
)) {
4079 ret
= wp_huge_pmd(&vmf
, orig_pmd
);
4080 if (!(ret
& VM_FAULT_FALLBACK
))
4083 huge_pmd_set_accessed(&vmf
, orig_pmd
);
4089 return handle_pte_fault(&vmf
);
4093 * By the time we get here, we already hold the mm semaphore
4095 * The mmap_sem may have been released depending on flags and our
4096 * return value. See filemap_fault() and __lock_page_or_retry().
4098 int handle_mm_fault(struct vm_area_struct
*vma
, unsigned long address
,
4103 __set_current_state(TASK_RUNNING
);
4105 count_vm_event(PGFAULT
);
4106 count_memcg_event_mm(vma
->vm_mm
, PGFAULT
);
4108 /* do counter updates before entering really critical section. */
4109 check_sync_rss_stat(current
);
4111 if (!arch_vma_access_permitted(vma
, flags
& FAULT_FLAG_WRITE
,
4112 flags
& FAULT_FLAG_INSTRUCTION
,
4113 flags
& FAULT_FLAG_REMOTE
))
4114 return VM_FAULT_SIGSEGV
;
4117 * Enable the memcg OOM handling for faults triggered in user
4118 * space. Kernel faults are handled more gracefully.
4120 if (flags
& FAULT_FLAG_USER
)
4121 mem_cgroup_oom_enable();
4123 if (unlikely(is_vm_hugetlb_page(vma
)))
4124 ret
= hugetlb_fault(vma
->vm_mm
, vma
, address
, flags
);
4126 ret
= __handle_mm_fault(vma
, address
, flags
);
4128 if (flags
& FAULT_FLAG_USER
) {
4129 mem_cgroup_oom_disable();
4131 * The task may have entered a memcg OOM situation but
4132 * if the allocation error was handled gracefully (no
4133 * VM_FAULT_OOM), there is no need to kill anything.
4134 * Just clean up the OOM state peacefully.
4136 if (task_in_memcg_oom(current
) && !(ret
& VM_FAULT_OOM
))
4137 mem_cgroup_oom_synchronize(false);
4142 EXPORT_SYMBOL_GPL(handle_mm_fault
);
4144 #ifndef __PAGETABLE_P4D_FOLDED
4146 * Allocate p4d page table.
4147 * We've already handled the fast-path in-line.
4149 int __p4d_alloc(struct mm_struct
*mm
, pgd_t
*pgd
, unsigned long address
)
4151 p4d_t
*new = p4d_alloc_one(mm
, address
);
4155 smp_wmb(); /* See comment in __pte_alloc */
4157 spin_lock(&mm
->page_table_lock
);
4158 if (pgd_present(*pgd
)) /* Another has populated it */
4161 pgd_populate(mm
, pgd
, new);
4162 spin_unlock(&mm
->page_table_lock
);
4165 #endif /* __PAGETABLE_P4D_FOLDED */
4167 #ifndef __PAGETABLE_PUD_FOLDED
4169 * Allocate page upper directory.
4170 * We've already handled the fast-path in-line.
4172 int __pud_alloc(struct mm_struct
*mm
, p4d_t
*p4d
, unsigned long address
)
4174 pud_t
*new = pud_alloc_one(mm
, address
);
4178 smp_wmb(); /* See comment in __pte_alloc */
4180 spin_lock(&mm
->page_table_lock
);
4181 #ifndef __ARCH_HAS_5LEVEL_HACK
4182 if (!p4d_present(*p4d
)) {
4184 p4d_populate(mm
, p4d
, new);
4185 } else /* Another has populated it */
4188 if (!pgd_present(*p4d
)) {
4190 pgd_populate(mm
, p4d
, new);
4191 } else /* Another has populated it */
4193 #endif /* __ARCH_HAS_5LEVEL_HACK */
4194 spin_unlock(&mm
->page_table_lock
);
4197 #endif /* __PAGETABLE_PUD_FOLDED */
4199 #ifndef __PAGETABLE_PMD_FOLDED
4201 * Allocate page middle directory.
4202 * We've already handled the fast-path in-line.
4204 int __pmd_alloc(struct mm_struct
*mm
, pud_t
*pud
, unsigned long address
)
4207 pmd_t
*new = pmd_alloc_one(mm
, address
);
4211 smp_wmb(); /* See comment in __pte_alloc */
4213 ptl
= pud_lock(mm
, pud
);
4214 #ifndef __ARCH_HAS_4LEVEL_HACK
4215 if (!pud_present(*pud
)) {
4217 pud_populate(mm
, pud
, new);
4218 } else /* Another has populated it */
4221 if (!pgd_present(*pud
)) {
4223 pgd_populate(mm
, pud
, new);
4224 } else /* Another has populated it */
4226 #endif /* __ARCH_HAS_4LEVEL_HACK */
4230 #endif /* __PAGETABLE_PMD_FOLDED */
4232 static int __follow_pte_pmd(struct mm_struct
*mm
, unsigned long address
,
4233 unsigned long *start
, unsigned long *end
,
4234 pte_t
**ptepp
, pmd_t
**pmdpp
, spinlock_t
**ptlp
)
4242 pgd
= pgd_offset(mm
, address
);
4243 if (pgd_none(*pgd
) || unlikely(pgd_bad(*pgd
)))
4246 p4d
= p4d_offset(pgd
, address
);
4247 if (p4d_none(*p4d
) || unlikely(p4d_bad(*p4d
)))
4250 pud
= pud_offset(p4d
, address
);
4251 if (pud_none(*pud
) || unlikely(pud_bad(*pud
)))
4254 pmd
= pmd_offset(pud
, address
);
4255 VM_BUG_ON(pmd_trans_huge(*pmd
));
4257 if (pmd_huge(*pmd
)) {
4262 *start
= address
& PMD_MASK
;
4263 *end
= *start
+ PMD_SIZE
;
4264 mmu_notifier_invalidate_range_start(mm
, *start
, *end
);
4266 *ptlp
= pmd_lock(mm
, pmd
);
4267 if (pmd_huge(*pmd
)) {
4273 mmu_notifier_invalidate_range_end(mm
, *start
, *end
);
4276 if (pmd_none(*pmd
) || unlikely(pmd_bad(*pmd
)))
4280 *start
= address
& PAGE_MASK
;
4281 *end
= *start
+ PAGE_SIZE
;
4282 mmu_notifier_invalidate_range_start(mm
, *start
, *end
);
4284 ptep
= pte_offset_map_lock(mm
, pmd
, address
, ptlp
);
4285 if (!pte_present(*ptep
))
4290 pte_unmap_unlock(ptep
, *ptlp
);
4292 mmu_notifier_invalidate_range_end(mm
, *start
, *end
);
4297 static inline int follow_pte(struct mm_struct
*mm
, unsigned long address
,
4298 pte_t
**ptepp
, spinlock_t
**ptlp
)
4302 /* (void) is needed to make gcc happy */
4303 (void) __cond_lock(*ptlp
,
4304 !(res
= __follow_pte_pmd(mm
, address
, NULL
, NULL
,
4305 ptepp
, NULL
, ptlp
)));
4309 int follow_pte_pmd(struct mm_struct
*mm
, unsigned long address
,
4310 unsigned long *start
, unsigned long *end
,
4311 pte_t
**ptepp
, pmd_t
**pmdpp
, spinlock_t
**ptlp
)
4315 /* (void) is needed to make gcc happy */
4316 (void) __cond_lock(*ptlp
,
4317 !(res
= __follow_pte_pmd(mm
, address
, start
, end
,
4318 ptepp
, pmdpp
, ptlp
)));
4321 EXPORT_SYMBOL(follow_pte_pmd
);
4324 * follow_pfn - look up PFN at a user virtual address
4325 * @vma: memory mapping
4326 * @address: user virtual address
4327 * @pfn: location to store found PFN
4329 * Only IO mappings and raw PFN mappings are allowed.
4331 * Returns zero and the pfn at @pfn on success, -ve otherwise.
4333 int follow_pfn(struct vm_area_struct
*vma
, unsigned long address
,
4340 if (!(vma
->vm_flags
& (VM_IO
| VM_PFNMAP
)))
4343 ret
= follow_pte(vma
->vm_mm
, address
, &ptep
, &ptl
);
4346 *pfn
= pte_pfn(*ptep
);
4347 pte_unmap_unlock(ptep
, ptl
);
4350 EXPORT_SYMBOL(follow_pfn
);
4352 #ifdef CONFIG_HAVE_IOREMAP_PROT
4353 int follow_phys(struct vm_area_struct
*vma
,
4354 unsigned long address
, unsigned int flags
,
4355 unsigned long *prot
, resource_size_t
*phys
)
4361 if (!(vma
->vm_flags
& (VM_IO
| VM_PFNMAP
)))
4364 if (follow_pte(vma
->vm_mm
, address
, &ptep
, &ptl
))
4368 if ((flags
& FOLL_WRITE
) && !pte_write(pte
))
4371 *prot
= pgprot_val(pte_pgprot(pte
));
4372 *phys
= (resource_size_t
)pte_pfn(pte
) << PAGE_SHIFT
;
4376 pte_unmap_unlock(ptep
, ptl
);
4381 int generic_access_phys(struct vm_area_struct
*vma
, unsigned long addr
,
4382 void *buf
, int len
, int write
)
4384 resource_size_t phys_addr
;
4385 unsigned long prot
= 0;
4386 void __iomem
*maddr
;
4387 int offset
= addr
& (PAGE_SIZE
-1);
4389 if (follow_phys(vma
, addr
, write
, &prot
, &phys_addr
))
4392 maddr
= ioremap_prot(phys_addr
, PAGE_ALIGN(len
+ offset
), prot
);
4394 memcpy_toio(maddr
+ offset
, buf
, len
);
4396 memcpy_fromio(buf
, maddr
+ offset
, len
);
4401 EXPORT_SYMBOL_GPL(generic_access_phys
);
4405 * Access another process' address space as given in mm. If non-NULL, use the
4406 * given task for page fault accounting.
4408 int __access_remote_vm(struct task_struct
*tsk
, struct mm_struct
*mm
,
4409 unsigned long addr
, void *buf
, int len
, unsigned int gup_flags
)
4411 struct vm_area_struct
*vma
;
4412 void *old_buf
= buf
;
4413 int write
= gup_flags
& FOLL_WRITE
;
4415 down_read(&mm
->mmap_sem
);
4416 /* ignore errors, just check how much was successfully transferred */
4418 int bytes
, ret
, offset
;
4420 struct page
*page
= NULL
;
4422 ret
= get_user_pages_remote(tsk
, mm
, addr
, 1,
4423 gup_flags
, &page
, &vma
, NULL
);
4425 #ifndef CONFIG_HAVE_IOREMAP_PROT
4429 * Check if this is a VM_IO | VM_PFNMAP VMA, which
4430 * we can access using slightly different code.
4432 vma
= find_vma(mm
, addr
);
4433 if (!vma
|| vma
->vm_start
> addr
)
4435 if (vma
->vm_ops
&& vma
->vm_ops
->access
)
4436 ret
= vma
->vm_ops
->access(vma
, addr
, buf
,
4444 offset
= addr
& (PAGE_SIZE
-1);
4445 if (bytes
> PAGE_SIZE
-offset
)
4446 bytes
= PAGE_SIZE
-offset
;
4450 copy_to_user_page(vma
, page
, addr
,
4451 maddr
+ offset
, buf
, bytes
);
4452 set_page_dirty_lock(page
);
4454 copy_from_user_page(vma
, page
, addr
,
4455 buf
, maddr
+ offset
, bytes
);
4464 up_read(&mm
->mmap_sem
);
4466 return buf
- old_buf
;
4470 * access_remote_vm - access another process' address space
4471 * @mm: the mm_struct of the target address space
4472 * @addr: start address to access
4473 * @buf: source or destination buffer
4474 * @len: number of bytes to transfer
4475 * @gup_flags: flags modifying lookup behaviour
4477 * The caller must hold a reference on @mm.
4479 int access_remote_vm(struct mm_struct
*mm
, unsigned long addr
,
4480 void *buf
, int len
, unsigned int gup_flags
)
4482 return __access_remote_vm(NULL
, mm
, addr
, buf
, len
, gup_flags
);
4486 * Access another process' address space.
4487 * Source/target buffer must be kernel space,
4488 * Do not walk the page table directly, use get_user_pages
4490 int access_process_vm(struct task_struct
*tsk
, unsigned long addr
,
4491 void *buf
, int len
, unsigned int gup_flags
)
4493 struct mm_struct
*mm
;
4496 mm
= get_task_mm(tsk
);
4500 ret
= __access_remote_vm(tsk
, mm
, addr
, buf
, len
, gup_flags
);
4506 EXPORT_SYMBOL_GPL(access_process_vm
);
4509 * Print the name of a VMA.
4511 void print_vma_addr(char *prefix
, unsigned long ip
)
4513 struct mm_struct
*mm
= current
->mm
;
4514 struct vm_area_struct
*vma
;
4517 * we might be running from an atomic context so we cannot sleep
4519 if (!down_read_trylock(&mm
->mmap_sem
))
4522 vma
= find_vma(mm
, ip
);
4523 if (vma
&& vma
->vm_file
) {
4524 struct file
*f
= vma
->vm_file
;
4525 char *buf
= (char *)__get_free_page(GFP_NOWAIT
);
4529 p
= file_path(f
, buf
, PAGE_SIZE
);
4532 printk("%s%s[%lx+%lx]", prefix
, kbasename(p
),
4534 vma
->vm_end
- vma
->vm_start
);
4535 free_page((unsigned long)buf
);
4538 up_read(&mm
->mmap_sem
);
4541 #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
4542 void __might_fault(const char *file
, int line
)
4545 * Some code (nfs/sunrpc) uses socket ops on kernel memory while
4546 * holding the mmap_sem, this is safe because kernel memory doesn't
4547 * get paged out, therefore we'll never actually fault, and the
4548 * below annotations will generate false positives.
4550 if (uaccess_kernel())
4552 if (pagefault_disabled())
4554 __might_sleep(file
, line
, 0);
4555 #if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
4557 might_lock_read(¤t
->mm
->mmap_sem
);
4560 EXPORT_SYMBOL(__might_fault
);
4563 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
4564 static void clear_gigantic_page(struct page
*page
,
4566 unsigned int pages_per_huge_page
)
4569 struct page
*p
= page
;
4572 for (i
= 0; i
< pages_per_huge_page
;
4573 i
++, p
= mem_map_next(p
, page
, i
)) {
4575 clear_user_highpage(p
, addr
+ i
* PAGE_SIZE
);
4578 void clear_huge_page(struct page
*page
,
4579 unsigned long addr_hint
, unsigned int pages_per_huge_page
)
4582 unsigned long addr
= addr_hint
&
4583 ~(((unsigned long)pages_per_huge_page
<< PAGE_SHIFT
) - 1);
4585 if (unlikely(pages_per_huge_page
> MAX_ORDER_NR_PAGES
)) {
4586 clear_gigantic_page(page
, addr
, pages_per_huge_page
);
4590 /* Clear sub-page to access last to keep its cache lines hot */
4592 n
= (addr_hint
- addr
) / PAGE_SIZE
;
4593 if (2 * n
<= pages_per_huge_page
) {
4594 /* If sub-page to access in first half of huge page */
4597 /* Clear sub-pages at the end of huge page */
4598 for (i
= pages_per_huge_page
- 1; i
>= 2 * n
; i
--) {
4600 clear_user_highpage(page
+ i
, addr
+ i
* PAGE_SIZE
);
4603 /* If sub-page to access in second half of huge page */
4604 base
= pages_per_huge_page
- 2 * (pages_per_huge_page
- n
);
4605 l
= pages_per_huge_page
- n
;
4606 /* Clear sub-pages at the begin of huge page */
4607 for (i
= 0; i
< base
; i
++) {
4609 clear_user_highpage(page
+ i
, addr
+ i
* PAGE_SIZE
);
4613 * Clear remaining sub-pages in left-right-left-right pattern
4614 * towards the sub-page to access
4616 for (i
= 0; i
< l
; i
++) {
4617 int left_idx
= base
+ i
;
4618 int right_idx
= base
+ 2 * l
- 1 - i
;
4621 clear_user_highpage(page
+ left_idx
,
4622 addr
+ left_idx
* PAGE_SIZE
);
4624 clear_user_highpage(page
+ right_idx
,
4625 addr
+ right_idx
* PAGE_SIZE
);
4629 static void copy_user_gigantic_page(struct page
*dst
, struct page
*src
,
4631 struct vm_area_struct
*vma
,
4632 unsigned int pages_per_huge_page
)
4635 struct page
*dst_base
= dst
;
4636 struct page
*src_base
= src
;
4638 for (i
= 0; i
< pages_per_huge_page
; ) {
4640 copy_user_highpage(dst
, src
, addr
+ i
*PAGE_SIZE
, vma
);
4643 dst
= mem_map_next(dst
, dst_base
, i
);
4644 src
= mem_map_next(src
, src_base
, i
);
4648 void copy_user_huge_page(struct page
*dst
, struct page
*src
,
4649 unsigned long addr
, struct vm_area_struct
*vma
,
4650 unsigned int pages_per_huge_page
)
4654 if (unlikely(pages_per_huge_page
> MAX_ORDER_NR_PAGES
)) {
4655 copy_user_gigantic_page(dst
, src
, addr
, vma
,
4656 pages_per_huge_page
);
4661 for (i
= 0; i
< pages_per_huge_page
; i
++) {
4663 copy_user_highpage(dst
+ i
, src
+ i
, addr
+ i
*PAGE_SIZE
, vma
);
4667 long copy_huge_page_from_user(struct page
*dst_page
,
4668 const void __user
*usr_src
,
4669 unsigned int pages_per_huge_page
,
4670 bool allow_pagefault
)
4672 void *src
= (void *)usr_src
;
4674 unsigned long i
, rc
= 0;
4675 unsigned long ret_val
= pages_per_huge_page
* PAGE_SIZE
;
4677 for (i
= 0; i
< pages_per_huge_page
; i
++) {
4678 if (allow_pagefault
)
4679 page_kaddr
= kmap(dst_page
+ i
);
4681 page_kaddr
= kmap_atomic(dst_page
+ i
);
4682 rc
= copy_from_user(page_kaddr
,
4683 (const void __user
*)(src
+ i
* PAGE_SIZE
),
4685 if (allow_pagefault
)
4686 kunmap(dst_page
+ i
);
4688 kunmap_atomic(page_kaddr
);
4690 ret_val
-= (PAGE_SIZE
- rc
);
4698 #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
4700 #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS
4702 static struct kmem_cache
*page_ptl_cachep
;
4704 void __init
ptlock_cache_init(void)
4706 page_ptl_cachep
= kmem_cache_create("page->ptl", sizeof(spinlock_t
), 0,
4710 bool ptlock_alloc(struct page
*page
)
4714 ptl
= kmem_cache_alloc(page_ptl_cachep
, GFP_KERNEL
);
4721 void ptlock_free(struct page
*page
)
4723 kmem_cache_free(page_ptl_cachep
, page
->ptl
);