mm: numa: do not trap faults on shared data section pages.
[linux-2.6-block.git] / mm / memory.c
CommitLineData
1da177e4
LT
1/*
2 * linux/mm/memory.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 */
6
7/*
8 * demand-loading started 01.12.91 - seems it is high on the list of
9 * things wanted, and it should be easy to implement. - Linus
10 */
11
12/*
13 * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
14 * pages started 02.12.91, seems to work. - Linus.
15 *
16 * Tested sharing by executing about 30 /bin/sh: under the old kernel it
17 * would have taken more than the 6M I have free, but it worked well as
18 * far as I could see.
19 *
20 * Also corrected some "invalidate()"s - I wasn't doing enough of them.
21 */
22
23/*
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.
29 */
30
31/*
32 * 05.04.94 - Multi-page memory management added for v1.1.
166f61b9 33 * Idea by Alex Bligh (alex@cconcepts.co.uk)
1da177e4
LT
34 *
35 * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG
36 * (Gerhard.Wichert@pdb.siemens.de)
37 *
38 * Aug/Sep 2004 Changed to four level page tables (Andi Kleen)
39 */
40
41#include <linux/kernel_stat.h>
42#include <linux/mm.h>
6e84f315 43#include <linux/sched/mm.h>
f7ccbae4 44#include <linux/sched/coredump.h>
6a3827d7 45#include <linux/sched/numa_balancing.h>
29930025 46#include <linux/sched/task.h>
1da177e4
LT
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>
5042db43 52#include <linux/memremap.h>
9a840895 53#include <linux/ksm.h>
1da177e4 54#include <linux/rmap.h>
b95f1b31 55#include <linux/export.h>
0ff92245 56#include <linux/delayacct.h>
1da177e4 57#include <linux/init.h>
01c8f1c4 58#include <linux/pfn_t.h>
edc79b2a 59#include <linux/writeback.h>
8a9f3ccd 60#include <linux/memcontrol.h>
cddb8a5c 61#include <linux/mmu_notifier.h>
3dc14741
HD
62#include <linux/kallsyms.h>
63#include <linux/swapops.h>
64#include <linux/elf.h>
5a0e3ad6 65#include <linux/gfp.h>
4daae3b4 66#include <linux/migrate.h>
2fbc57c5 67#include <linux/string.h>
0abdd7a8 68#include <linux/dma-debug.h>
1592eef0 69#include <linux/debugfs.h>
6b251fc9 70#include <linux/userfaultfd_k.h>
bc2466e4 71#include <linux/dax.h>
6b31d595 72#include <linux/oom.h>
1da177e4 73
6952b61d 74#include <asm/io.h>
33a709b2 75#include <asm/mmu_context.h>
1da177e4 76#include <asm/pgalloc.h>
7c0f6ba6 77#include <linux/uaccess.h>
1da177e4
LT
78#include <asm/tlb.h>
79#include <asm/tlbflush.h>
80#include <asm/pgtable.h>
81
42b77728
JB
82#include "internal.h"
83
90572890
PZ
84#ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
85#warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
75980e97
PZ
86#endif
87
d41dee36 88#ifndef CONFIG_NEED_MULTIPLE_NODES
1da177e4
LT
89/* use the per-pgdat data instead for discontigmem - mbligh */
90unsigned long max_mapnr;
1da177e4 91EXPORT_SYMBOL(max_mapnr);
166f61b9
TH
92
93struct page *mem_map;
1da177e4
LT
94EXPORT_SYMBOL(mem_map);
95#endif
96
1da177e4
LT
97/*
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
102 * and ZONE_HIGHMEM.
103 */
166f61b9 104void *high_memory;
1da177e4 105EXPORT_SYMBOL(high_memory);
1da177e4 106
32a93233
IM
107/*
108 * Randomize the address space (stacks, mmaps, brk, etc.).
109 *
110 * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
111 * as ancient (libc5 based) binaries can segfault. )
112 */
113int randomize_va_space __read_mostly =
114#ifdef CONFIG_COMPAT_BRK
115 1;
116#else
117 2;
118#endif
a62eaf15
AK
119
120static int __init disable_randmaps(char *s)
121{
122 randomize_va_space = 0;
9b41046c 123 return 1;
a62eaf15
AK
124}
125__setup("norandmaps", disable_randmaps);
126
62eede62 127unsigned long zero_pfn __read_mostly;
0b70068e
AB
128EXPORT_SYMBOL(zero_pfn);
129
166f61b9
TH
130unsigned long highest_memmap_pfn __read_mostly;
131
a13ea5b7
HD
132/*
133 * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
134 */
135static int __init init_zero_pfn(void)
136{
137 zero_pfn = page_to_pfn(ZERO_PAGE(0));
138 return 0;
139}
140core_initcall(init_zero_pfn);
a62eaf15 141
d559db08 142
34e55232
KH
143#if defined(SPLIT_RSS_COUNTING)
144
ea48cf78 145void sync_mm_rss(struct mm_struct *mm)
34e55232
KH
146{
147 int i;
148
149 for (i = 0; i < NR_MM_COUNTERS; i++) {
05af2e10
DR
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;
34e55232
KH
153 }
154 }
05af2e10 155 current->rss_stat.events = 0;
34e55232
KH
156}
157
158static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
159{
160 struct task_struct *task = current;
161
162 if (likely(task->mm == mm))
163 task->rss_stat.count[member] += val;
164 else
165 add_mm_counter(mm, member, val);
166}
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)
169
170/* sync counter once per 64 page faults */
171#define TASK_RSS_EVENTS_THRESH (64)
172static void check_sync_rss_stat(struct task_struct *task)
173{
174 if (unlikely(task != current))
175 return;
176 if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
ea48cf78 177 sync_mm_rss(task->mm);
34e55232 178}
9547d01b 179#else /* SPLIT_RSS_COUNTING */
34e55232
KH
180
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)
183
184static void check_sync_rss_stat(struct task_struct *task)
185{
186}
187
9547d01b
PZ
188#endif /* SPLIT_RSS_COUNTING */
189
190#ifdef HAVE_GENERIC_MMU_GATHER
191
ca1d6c7d 192static bool tlb_next_batch(struct mmu_gather *tlb)
9547d01b
PZ
193{
194 struct mmu_gather_batch *batch;
195
196 batch = tlb->active;
197 if (batch->next) {
198 tlb->active = batch->next;
ca1d6c7d 199 return true;
9547d01b
PZ
200 }
201
53a59fc6 202 if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
ca1d6c7d 203 return false;
53a59fc6 204
9547d01b
PZ
205 batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
206 if (!batch)
ca1d6c7d 207 return false;
9547d01b 208
53a59fc6 209 tlb->batch_count++;
9547d01b
PZ
210 batch->next = NULL;
211 batch->nr = 0;
212 batch->max = MAX_GATHER_BATCH;
213
214 tlb->active->next = batch;
215 tlb->active = batch;
216
ca1d6c7d 217 return true;
9547d01b
PZ
218}
219
56236a59
MK
220void arch_tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
221 unsigned long start, unsigned long end)
9547d01b
PZ
222{
223 tlb->mm = mm;
224
2b047252
LT
225 /* Is it from 0 to ~0? */
226 tlb->fullmm = !(start | (end+1));
1de14c3c 227 tlb->need_flush_all = 0;
9547d01b
PZ
228 tlb->local.next = NULL;
229 tlb->local.nr = 0;
230 tlb->local.max = ARRAY_SIZE(tlb->__pages);
231 tlb->active = &tlb->local;
53a59fc6 232 tlb->batch_count = 0;
9547d01b
PZ
233
234#ifdef CONFIG_HAVE_RCU_TABLE_FREE
235 tlb->batch = NULL;
236#endif
e77b0852 237 tlb->page_size = 0;
fb7332a9
WD
238
239 __tlb_reset_range(tlb);
9547d01b
PZ
240}
241
1cf35d47 242static void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
9547d01b 243{
721c21c1
WD
244 if (!tlb->end)
245 return;
246
9547d01b 247 tlb_flush(tlb);
34ee645e 248 mmu_notifier_invalidate_range(tlb->mm, tlb->start, tlb->end);
9547d01b
PZ
249#ifdef CONFIG_HAVE_RCU_TABLE_FREE
250 tlb_table_flush(tlb);
34e55232 251#endif
fb7332a9 252 __tlb_reset_range(tlb);
1cf35d47
LT
253}
254
255static void tlb_flush_mmu_free(struct mmu_gather *tlb)
256{
257 struct mmu_gather_batch *batch;
34e55232 258
721c21c1 259 for (batch = &tlb->local; batch && batch->nr; batch = batch->next) {
9547d01b
PZ
260 free_pages_and_swap_cache(batch->pages, batch->nr);
261 batch->nr = 0;
262 }
263 tlb->active = &tlb->local;
264}
265
1cf35d47
LT
266void tlb_flush_mmu(struct mmu_gather *tlb)
267{
1cf35d47
LT
268 tlb_flush_mmu_tlbonly(tlb);
269 tlb_flush_mmu_free(tlb);
270}
271
9547d01b
PZ
272/* tlb_finish_mmu
273 * Called at the end of the shootdown operation to free up any resources
274 * that were required.
275 */
56236a59 276void arch_tlb_finish_mmu(struct mmu_gather *tlb,
99baac21 277 unsigned long start, unsigned long end, bool force)
9547d01b
PZ
278{
279 struct mmu_gather_batch *batch, *next;
280
99baac21
MK
281 if (force)
282 __tlb_adjust_range(tlb, start, end - start);
283
9547d01b
PZ
284 tlb_flush_mmu(tlb);
285
286 /* keep the page table cache within bounds */
287 check_pgt_cache();
288
289 for (batch = tlb->local.next; batch; batch = next) {
290 next = batch->next;
291 free_pages((unsigned long)batch, 0);
292 }
293 tlb->local.next = NULL;
294}
295
296/* __tlb_remove_page
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().
e9d55e15 301 *returns true if the caller should flush.
9547d01b 302 */
e77b0852 303bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size)
9547d01b
PZ
304{
305 struct mmu_gather_batch *batch;
306
fb7332a9 307 VM_BUG_ON(!tlb->end);
692a68c1 308 VM_WARN_ON(tlb->page_size != page_size);
e77b0852 309
9547d01b 310 batch = tlb->active;
692a68c1
AK
311 /*
312 * Add the page and check if we are full. If so
313 * force a flush.
314 */
315 batch->pages[batch->nr++] = page;
9547d01b
PZ
316 if (batch->nr == batch->max) {
317 if (!tlb_next_batch(tlb))
e9d55e15 318 return true;
0b43c3aa 319 batch = tlb->active;
9547d01b 320 }
309381fe 321 VM_BUG_ON_PAGE(batch->nr > batch->max, page);
9547d01b 322
e9d55e15 323 return false;
9547d01b
PZ
324}
325
326#endif /* HAVE_GENERIC_MMU_GATHER */
327
26723911
PZ
328#ifdef CONFIG_HAVE_RCU_TABLE_FREE
329
330/*
331 * See the comment near struct mmu_table_batch.
332 */
333
334static void tlb_remove_table_smp_sync(void *arg)
335{
336 /* Simply deliver the interrupt */
337}
338
339static void tlb_remove_table_one(void *table)
340{
341 /*
342 * This isn't an RCU grace period and hence the page-tables cannot be
343 * assumed to be actually RCU-freed.
344 *
345 * It is however sufficient for software page-table walkers that rely on
346 * IRQ disabling. See the comment near struct mmu_table_batch.
347 */
348 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
349 __tlb_remove_table(table);
350}
351
352static void tlb_remove_table_rcu(struct rcu_head *head)
353{
354 struct mmu_table_batch *batch;
355 int i;
356
357 batch = container_of(head, struct mmu_table_batch, rcu);
358
359 for (i = 0; i < batch->nr; i++)
360 __tlb_remove_table(batch->tables[i]);
361
362 free_page((unsigned long)batch);
363}
364
365void tlb_table_flush(struct mmu_gather *tlb)
366{
367 struct mmu_table_batch **batch = &tlb->batch;
368
369 if (*batch) {
370 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
371 *batch = NULL;
372 }
373}
374
375void tlb_remove_table(struct mmu_gather *tlb, void *table)
376{
377 struct mmu_table_batch **batch = &tlb->batch;
378
26723911
PZ
379 /*
380 * When there's less then two users of this mm there cannot be a
381 * concurrent page-table walk.
382 */
383 if (atomic_read(&tlb->mm->mm_users) < 2) {
384 __tlb_remove_table(table);
385 return;
386 }
387
388 if (*batch == NULL) {
389 *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
390 if (*batch == NULL) {
391 tlb_remove_table_one(table);
392 return;
393 }
394 (*batch)->nr = 0;
395 }
396 (*batch)->tables[(*batch)->nr++] = table;
397 if ((*batch)->nr == MAX_TABLE_BATCH)
398 tlb_table_flush(tlb);
399}
400
9547d01b 401#endif /* CONFIG_HAVE_RCU_TABLE_FREE */
26723911 402
ef549e13
MR
403/**
404 * tlb_gather_mmu - initialize an mmu_gather structure for page-table tear-down
405 * @tlb: the mmu_gather structure to initialize
406 * @mm: the mm_struct of the target address space
407 * @start: start of the region that will be removed from the page-table
408 * @end: end of the region that will be removed from the page-table
409 *
410 * Called to initialize an (on-stack) mmu_gather structure for page-table
411 * tear-down from @mm. The @start and @end are set to 0 and -1
412 * respectively when @mm is without users and we're going to destroy
413 * the full address space (exit/execve).
56236a59
MK
414 */
415void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
416 unsigned long start, unsigned long end)
417{
418 arch_tlb_gather_mmu(tlb, mm, start, end);
99baac21 419 inc_tlb_flush_pending(tlb->mm);
56236a59
MK
420}
421
422void tlb_finish_mmu(struct mmu_gather *tlb,
423 unsigned long start, unsigned long end)
424{
99baac21
MK
425 /*
426 * If there are parallel threads are doing PTE changes on same range
427 * under non-exclusive lock(e.g., mmap_sem read-side) but defer TLB
428 * flush by batching, a thread has stable TLB entry can fail to flush
429 * the TLB by observing pte_none|!pte_dirty, for example so flush TLB
430 * forcefully if we detect parallel PTE batching threads.
431 */
432 bool force = mm_tlb_flush_nested(tlb->mm);
433
434 arch_tlb_finish_mmu(tlb, start, end, force);
435 dec_tlb_flush_pending(tlb->mm);
56236a59
MK
436}
437
1da177e4
LT
438/*
439 * Note: this doesn't free the actual pages themselves. That
440 * has been handled earlier when unmapping all the memory regions.
441 */
9e1b32ca
BH
442static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
443 unsigned long addr)
1da177e4 444{
2f569afd 445 pgtable_t token = pmd_pgtable(*pmd);
e0da382c 446 pmd_clear(pmd);
9e1b32ca 447 pte_free_tlb(tlb, token, addr);
c4812909 448 mm_dec_nr_ptes(tlb->mm);
1da177e4
LT
449}
450
e0da382c
HD
451static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
452 unsigned long addr, unsigned long end,
453 unsigned long floor, unsigned long ceiling)
1da177e4
LT
454{
455 pmd_t *pmd;
456 unsigned long next;
e0da382c 457 unsigned long start;
1da177e4 458
e0da382c 459 start = addr;
1da177e4 460 pmd = pmd_offset(pud, addr);
1da177e4
LT
461 do {
462 next = pmd_addr_end(addr, end);
463 if (pmd_none_or_clear_bad(pmd))
464 continue;
9e1b32ca 465 free_pte_range(tlb, pmd, addr);
1da177e4
LT
466 } while (pmd++, addr = next, addr != end);
467
e0da382c
HD
468 start &= PUD_MASK;
469 if (start < floor)
470 return;
471 if (ceiling) {
472 ceiling &= PUD_MASK;
473 if (!ceiling)
474 return;
1da177e4 475 }
e0da382c
HD
476 if (end - 1 > ceiling - 1)
477 return;
478
479 pmd = pmd_offset(pud, start);
480 pud_clear(pud);
9e1b32ca 481 pmd_free_tlb(tlb, pmd, start);
dc6c9a35 482 mm_dec_nr_pmds(tlb->mm);
1da177e4
LT
483}
484
c2febafc 485static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d,
e0da382c
HD
486 unsigned long addr, unsigned long end,
487 unsigned long floor, unsigned long ceiling)
1da177e4
LT
488{
489 pud_t *pud;
490 unsigned long next;
e0da382c 491 unsigned long start;
1da177e4 492
e0da382c 493 start = addr;
c2febafc 494 pud = pud_offset(p4d, addr);
1da177e4
LT
495 do {
496 next = pud_addr_end(addr, end);
497 if (pud_none_or_clear_bad(pud))
498 continue;
e0da382c 499 free_pmd_range(tlb, pud, addr, next, floor, ceiling);
1da177e4
LT
500 } while (pud++, addr = next, addr != end);
501
c2febafc
KS
502 start &= P4D_MASK;
503 if (start < floor)
504 return;
505 if (ceiling) {
506 ceiling &= P4D_MASK;
507 if (!ceiling)
508 return;
509 }
510 if (end - 1 > ceiling - 1)
511 return;
512
513 pud = pud_offset(p4d, start);
514 p4d_clear(p4d);
515 pud_free_tlb(tlb, pud, start);
b4e98d9a 516 mm_dec_nr_puds(tlb->mm);
c2febafc
KS
517}
518
519static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd,
520 unsigned long addr, unsigned long end,
521 unsigned long floor, unsigned long ceiling)
522{
523 p4d_t *p4d;
524 unsigned long next;
525 unsigned long start;
526
527 start = addr;
528 p4d = p4d_offset(pgd, addr);
529 do {
530 next = p4d_addr_end(addr, end);
531 if (p4d_none_or_clear_bad(p4d))
532 continue;
533 free_pud_range(tlb, p4d, addr, next, floor, ceiling);
534 } while (p4d++, addr = next, addr != end);
535
e0da382c
HD
536 start &= PGDIR_MASK;
537 if (start < floor)
538 return;
539 if (ceiling) {
540 ceiling &= PGDIR_MASK;
541 if (!ceiling)
542 return;
1da177e4 543 }
e0da382c
HD
544 if (end - 1 > ceiling - 1)
545 return;
546
c2febafc 547 p4d = p4d_offset(pgd, start);
e0da382c 548 pgd_clear(pgd);
c2febafc 549 p4d_free_tlb(tlb, p4d, start);
1da177e4
LT
550}
551
552/*
e0da382c 553 * This function frees user-level page tables of a process.
1da177e4 554 */
42b77728 555void free_pgd_range(struct mmu_gather *tlb,
e0da382c
HD
556 unsigned long addr, unsigned long end,
557 unsigned long floor, unsigned long ceiling)
1da177e4
LT
558{
559 pgd_t *pgd;
560 unsigned long next;
e0da382c
HD
561
562 /*
563 * The next few lines have given us lots of grief...
564 *
565 * Why are we testing PMD* at this top level? Because often
566 * there will be no work to do at all, and we'd prefer not to
567 * go all the way down to the bottom just to discover that.
568 *
569 * Why all these "- 1"s? Because 0 represents both the bottom
570 * of the address space and the top of it (using -1 for the
571 * top wouldn't help much: the masks would do the wrong thing).
572 * The rule is that addr 0 and floor 0 refer to the bottom of
573 * the address space, but end 0 and ceiling 0 refer to the top
574 * Comparisons need to use "end - 1" and "ceiling - 1" (though
575 * that end 0 case should be mythical).
576 *
577 * Wherever addr is brought up or ceiling brought down, we must
578 * be careful to reject "the opposite 0" before it confuses the
579 * subsequent tests. But what about where end is brought down
580 * by PMD_SIZE below? no, end can't go down to 0 there.
581 *
582 * Whereas we round start (addr) and ceiling down, by different
583 * masks at different levels, in order to test whether a table
584 * now has no other vmas using it, so can be freed, we don't
585 * bother to round floor or end up - the tests don't need that.
586 */
1da177e4 587
e0da382c
HD
588 addr &= PMD_MASK;
589 if (addr < floor) {
590 addr += PMD_SIZE;
591 if (!addr)
592 return;
593 }
594 if (ceiling) {
595 ceiling &= PMD_MASK;
596 if (!ceiling)
597 return;
598 }
599 if (end - 1 > ceiling - 1)
600 end -= PMD_SIZE;
601 if (addr > end - 1)
602 return;
07e32661
AK
603 /*
604 * We add page table cache pages with PAGE_SIZE,
605 * (see pte_free_tlb()), flush the tlb if we need
606 */
607 tlb_remove_check_page_size_change(tlb, PAGE_SIZE);
42b77728 608 pgd = pgd_offset(tlb->mm, addr);
1da177e4
LT
609 do {
610 next = pgd_addr_end(addr, end);
611 if (pgd_none_or_clear_bad(pgd))
612 continue;
c2febafc 613 free_p4d_range(tlb, pgd, addr, next, floor, ceiling);
1da177e4 614 } while (pgd++, addr = next, addr != end);
e0da382c
HD
615}
616
42b77728 617void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
3bf5ee95 618 unsigned long floor, unsigned long ceiling)
e0da382c
HD
619{
620 while (vma) {
621 struct vm_area_struct *next = vma->vm_next;
622 unsigned long addr = vma->vm_start;
623
8f4f8c16 624 /*
25d9e2d1 625 * Hide vma from rmap and truncate_pagecache before freeing
626 * pgtables
8f4f8c16 627 */
5beb4930 628 unlink_anon_vmas(vma);
8f4f8c16
HD
629 unlink_file_vma(vma);
630
9da61aef 631 if (is_vm_hugetlb_page(vma)) {
3bf5ee95 632 hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
166f61b9 633 floor, next ? next->vm_start : ceiling);
3bf5ee95
HD
634 } else {
635 /*
636 * Optimization: gather nearby vmas into one call down
637 */
638 while (next && next->vm_start <= vma->vm_end + PMD_SIZE
4866920b 639 && !is_vm_hugetlb_page(next)) {
3bf5ee95
HD
640 vma = next;
641 next = vma->vm_next;
5beb4930 642 unlink_anon_vmas(vma);
8f4f8c16 643 unlink_file_vma(vma);
3bf5ee95
HD
644 }
645 free_pgd_range(tlb, addr, vma->vm_end,
166f61b9 646 floor, next ? next->vm_start : ceiling);
3bf5ee95 647 }
e0da382c
HD
648 vma = next;
649 }
1da177e4
LT
650}
651
3ed3a4f0 652int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
1da177e4 653{
c4088ebd 654 spinlock_t *ptl;
2f569afd 655 pgtable_t new = pte_alloc_one(mm, address);
1bb3630e
HD
656 if (!new)
657 return -ENOMEM;
658
362a61ad
NP
659 /*
660 * Ensure all pte setup (eg. pte page lock and page clearing) are
661 * visible before the pte is made visible to other CPUs by being
662 * put into page tables.
663 *
664 * The other side of the story is the pointer chasing in the page
665 * table walking code (when walking the page table without locking;
666 * ie. most of the time). Fortunately, these data accesses consist
667 * of a chain of data-dependent loads, meaning most CPUs (alpha
668 * being the notable exception) will already guarantee loads are
669 * seen in-order. See the alpha page table accessors for the
670 * smp_read_barrier_depends() barriers in page table walking code.
671 */
672 smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
673
c4088ebd 674 ptl = pmd_lock(mm, pmd);
8ac1f832 675 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
c4812909 676 mm_inc_nr_ptes(mm);
1da177e4 677 pmd_populate(mm, pmd, new);
2f569afd 678 new = NULL;
4b471e88 679 }
c4088ebd 680 spin_unlock(ptl);
2f569afd
MS
681 if (new)
682 pte_free(mm, new);
1bb3630e 683 return 0;
1da177e4
LT
684}
685
1bb3630e 686int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
1da177e4 687{
1bb3630e
HD
688 pte_t *new = pte_alloc_one_kernel(&init_mm, address);
689 if (!new)
690 return -ENOMEM;
691
362a61ad
NP
692 smp_wmb(); /* See comment in __pte_alloc */
693
1bb3630e 694 spin_lock(&init_mm.page_table_lock);
8ac1f832 695 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
1bb3630e 696 pmd_populate_kernel(&init_mm, pmd, new);
2f569afd 697 new = NULL;
4b471e88 698 }
1bb3630e 699 spin_unlock(&init_mm.page_table_lock);
2f569afd
MS
700 if (new)
701 pte_free_kernel(&init_mm, new);
1bb3630e 702 return 0;
1da177e4
LT
703}
704
d559db08
KH
705static inline void init_rss_vec(int *rss)
706{
707 memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
708}
709
710static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
ae859762 711{
d559db08
KH
712 int i;
713
34e55232 714 if (current->mm == mm)
05af2e10 715 sync_mm_rss(mm);
d559db08
KH
716 for (i = 0; i < NR_MM_COUNTERS; i++)
717 if (rss[i])
718 add_mm_counter(mm, i, rss[i]);
ae859762
HD
719}
720
b5810039 721/*
6aab341e
LT
722 * This function is called to print an error when a bad pte
723 * is found. For example, we might have a PFN-mapped pte in
724 * a region that doesn't allow it.
b5810039
NP
725 *
726 * The calling function must still handle the error.
727 */
3dc14741
HD
728static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
729 pte_t pte, struct page *page)
b5810039 730{
3dc14741 731 pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
c2febafc
KS
732 p4d_t *p4d = p4d_offset(pgd, addr);
733 pud_t *pud = pud_offset(p4d, addr);
3dc14741
HD
734 pmd_t *pmd = pmd_offset(pud, addr);
735 struct address_space *mapping;
736 pgoff_t index;
d936cf9b
HD
737 static unsigned long resume;
738 static unsigned long nr_shown;
739 static unsigned long nr_unshown;
740
741 /*
742 * Allow a burst of 60 reports, then keep quiet for that minute;
743 * or allow a steady drip of one report per second.
744 */
745 if (nr_shown == 60) {
746 if (time_before(jiffies, resume)) {
747 nr_unshown++;
748 return;
749 }
750 if (nr_unshown) {
1170532b
JP
751 pr_alert("BUG: Bad page map: %lu messages suppressed\n",
752 nr_unshown);
d936cf9b
HD
753 nr_unshown = 0;
754 }
755 nr_shown = 0;
756 }
757 if (nr_shown++ == 0)
758 resume = jiffies + 60 * HZ;
3dc14741
HD
759
760 mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
761 index = linear_page_index(vma, addr);
762
1170532b
JP
763 pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n",
764 current->comm,
765 (long long)pte_val(pte), (long long)pmd_val(*pmd));
718a3821 766 if (page)
f0b791a3 767 dump_page(page, "bad pte");
1170532b
JP
768 pr_alert("addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n",
769 (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
3dc14741
HD
770 /*
771 * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y
772 */
2682582a
KK
773 pr_alert("file:%pD fault:%pf mmap:%pf readpage:%pf\n",
774 vma->vm_file,
775 vma->vm_ops ? vma->vm_ops->fault : NULL,
776 vma->vm_file ? vma->vm_file->f_op->mmap : NULL,
777 mapping ? mapping->a_ops->readpage : NULL);
b5810039 778 dump_stack();
373d4d09 779 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
b5810039
NP
780}
781
ee498ed7 782/*
7e675137 783 * vm_normal_page -- This function gets the "struct page" associated with a pte.
6aab341e 784 *
7e675137
NP
785 * "Special" mappings do not wish to be associated with a "struct page" (either
786 * it doesn't exist, or it exists but they don't want to touch it). In this
787 * case, NULL is returned here. "Normal" mappings do have a struct page.
b379d790 788 *
7e675137
NP
789 * There are 2 broad cases. Firstly, an architecture may define a pte_special()
790 * pte bit, in which case this function is trivial. Secondly, an architecture
791 * may not have a spare pte bit, which requires a more complicated scheme,
792 * described below.
793 *
794 * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
795 * special mapping (even if there are underlying and valid "struct pages").
796 * COWed pages of a VM_PFNMAP are always normal.
6aab341e 797 *
b379d790
JH
798 * The way we recognize COWed pages within VM_PFNMAP mappings is through the
799 * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
7e675137
NP
800 * set, and the vm_pgoff will point to the first PFN mapped: thus every special
801 * mapping will always honor the rule
6aab341e
LT
802 *
803 * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
804 *
7e675137
NP
805 * And for normal mappings this is false.
806 *
807 * This restricts such mappings to be a linear translation from virtual address
808 * to pfn. To get around this restriction, we allow arbitrary mappings so long
809 * as the vma is not a COW mapping; in that case, we know that all ptes are
810 * special (because none can have been COWed).
b379d790 811 *
b379d790 812 *
7e675137 813 * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
b379d790
JH
814 *
815 * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
816 * page" backing, however the difference is that _all_ pages with a struct
817 * page (that is, those where pfn_valid is true) are refcounted and considered
818 * normal pages by the VM. The disadvantage is that pages are refcounted
819 * (which can be slower and simply not an option for some PFNMAP users). The
820 * advantage is that we don't have to follow the strict linearity rule of
821 * PFNMAP mappings in order to support COWable mappings.
822 *
ee498ed7 823 */
7e675137
NP
824#ifdef __HAVE_ARCH_PTE_SPECIAL
825# define HAVE_PTE_SPECIAL 1
826#else
827# define HAVE_PTE_SPECIAL 0
828#endif
df6ad698
JG
829struct page *_vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
830 pte_t pte, bool with_public_device)
ee498ed7 831{
22b31eec 832 unsigned long pfn = pte_pfn(pte);
7e675137
NP
833
834 if (HAVE_PTE_SPECIAL) {
b38af472 835 if (likely(!pte_special(pte)))
22b31eec 836 goto check_pfn;
667a0a06
DV
837 if (vma->vm_ops && vma->vm_ops->find_special_page)
838 return vma->vm_ops->find_special_page(vma, addr);
a13ea5b7
HD
839 if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
840 return NULL;
df6ad698
JG
841 if (is_zero_pfn(pfn))
842 return NULL;
843
844 /*
845 * Device public pages are special pages (they are ZONE_DEVICE
846 * pages but different from persistent memory). They behave
847 * allmost like normal pages. The difference is that they are
848 * not on the lru and thus should never be involve with any-
849 * thing that involve lru manipulation (mlock, numa balancing,
850 * ...).
851 *
852 * This is why we still want to return NULL for such page from
853 * vm_normal_page() so that we do not have to special case all
854 * call site of vm_normal_page().
855 */
7d790d2d 856 if (likely(pfn <= highest_memmap_pfn)) {
df6ad698
JG
857 struct page *page = pfn_to_page(pfn);
858
859 if (is_device_public_page(page)) {
860 if (with_public_device)
861 return page;
862 return NULL;
863 }
864 }
865 print_bad_pte(vma, addr, pte, NULL);
7e675137
NP
866 return NULL;
867 }
868
869 /* !HAVE_PTE_SPECIAL case follows: */
870
b379d790
JH
871 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
872 if (vma->vm_flags & VM_MIXEDMAP) {
873 if (!pfn_valid(pfn))
874 return NULL;
875 goto out;
876 } else {
7e675137
NP
877 unsigned long off;
878 off = (addr - vma->vm_start) >> PAGE_SHIFT;
b379d790
JH
879 if (pfn == vma->vm_pgoff + off)
880 return NULL;
881 if (!is_cow_mapping(vma->vm_flags))
882 return NULL;
883 }
6aab341e
LT
884 }
885
b38af472
HD
886 if (is_zero_pfn(pfn))
887 return NULL;
22b31eec
HD
888check_pfn:
889 if (unlikely(pfn > highest_memmap_pfn)) {
890 print_bad_pte(vma, addr, pte, NULL);
891 return NULL;
892 }
6aab341e
LT
893
894 /*
7e675137 895 * NOTE! We still have PageReserved() pages in the page tables.
7e675137 896 * eg. VDSO mappings can cause them to exist.
6aab341e 897 */
b379d790 898out:
6aab341e 899 return pfn_to_page(pfn);
ee498ed7
HD
900}
901
28093f9f
GS
902#ifdef CONFIG_TRANSPARENT_HUGEPAGE
903struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
904 pmd_t pmd)
905{
906 unsigned long pfn = pmd_pfn(pmd);
907
908 /*
909 * There is no pmd_special() but there may be special pmds, e.g.
910 * in a direct-access (dax) mapping, so let's just replicate the
911 * !HAVE_PTE_SPECIAL case from vm_normal_page() here.
912 */
913 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
914 if (vma->vm_flags & VM_MIXEDMAP) {
915 if (!pfn_valid(pfn))
916 return NULL;
917 goto out;
918 } else {
919 unsigned long off;
920 off = (addr - vma->vm_start) >> PAGE_SHIFT;
921 if (pfn == vma->vm_pgoff + off)
922 return NULL;
923 if (!is_cow_mapping(vma->vm_flags))
924 return NULL;
925 }
926 }
927
928 if (is_zero_pfn(pfn))
929 return NULL;
930 if (unlikely(pfn > highest_memmap_pfn))
931 return NULL;
932
933 /*
934 * NOTE! We still have PageReserved() pages in the page tables.
935 * eg. VDSO mappings can cause them to exist.
936 */
937out:
938 return pfn_to_page(pfn);
939}
940#endif
941
1da177e4
LT
942/*
943 * copy one vm_area from one task to the other. Assumes the page tables
944 * already present in the new task to be cleared in the whole range
945 * covered by this vma.
1da177e4
LT
946 */
947
570a335b 948static inline unsigned long
1da177e4 949copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
b5810039 950 pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
8c103762 951 unsigned long addr, int *rss)
1da177e4 952{
b5810039 953 unsigned long vm_flags = vma->vm_flags;
1da177e4
LT
954 pte_t pte = *src_pte;
955 struct page *page;
1da177e4
LT
956
957 /* pte contains position in swap or file, so copy. */
958 if (unlikely(!pte_present(pte))) {
0661a336
KS
959 swp_entry_t entry = pte_to_swp_entry(pte);
960
961 if (likely(!non_swap_entry(entry))) {
962 if (swap_duplicate(entry) < 0)
963 return entry.val;
964
965 /* make sure dst_mm is on swapoff's mmlist. */
966 if (unlikely(list_empty(&dst_mm->mmlist))) {
967 spin_lock(&mmlist_lock);
968 if (list_empty(&dst_mm->mmlist))
969 list_add(&dst_mm->mmlist,
970 &src_mm->mmlist);
971 spin_unlock(&mmlist_lock);
972 }
973 rss[MM_SWAPENTS]++;
974 } else if (is_migration_entry(entry)) {
975 page = migration_entry_to_page(entry);
976
eca56ff9 977 rss[mm_counter(page)]++;
0661a336
KS
978
979 if (is_write_migration_entry(entry) &&
980 is_cow_mapping(vm_flags)) {
981 /*
982 * COW mappings require pages in both
983 * parent and child to be set to read.
984 */
985 make_migration_entry_read(&entry);
986 pte = swp_entry_to_pte(entry);
987 if (pte_swp_soft_dirty(*src_pte))
988 pte = pte_swp_mksoft_dirty(pte);
989 set_pte_at(src_mm, addr, src_pte, pte);
0697212a 990 }
5042db43
JG
991 } else if (is_device_private_entry(entry)) {
992 page = device_private_entry_to_page(entry);
993
994 /*
995 * Update rss count even for unaddressable pages, as
996 * they should treated just like normal pages in this
997 * respect.
998 *
999 * We will likely want to have some new rss counters
1000 * for unaddressable pages, at some point. But for now
1001 * keep things as they are.
1002 */
1003 get_page(page);
1004 rss[mm_counter(page)]++;
1005 page_dup_rmap(page, false);
1006
1007 /*
1008 * We do not preserve soft-dirty information, because so
1009 * far, checkpoint/restore is the only feature that
1010 * requires that. And checkpoint/restore does not work
1011 * when a device driver is involved (you cannot easily
1012 * save and restore device driver state).
1013 */
1014 if (is_write_device_private_entry(entry) &&
1015 is_cow_mapping(vm_flags)) {
1016 make_device_private_entry_read(&entry);
1017 pte = swp_entry_to_pte(entry);
1018 set_pte_at(src_mm, addr, src_pte, pte);
1019 }
1da177e4 1020 }
ae859762 1021 goto out_set_pte;
1da177e4
LT
1022 }
1023
1da177e4
LT
1024 /*
1025 * If it's a COW mapping, write protect it both
1026 * in the parent and the child
1027 */
67121172 1028 if (is_cow_mapping(vm_flags)) {
1da177e4 1029 ptep_set_wrprotect(src_mm, addr, src_pte);
3dc90795 1030 pte = pte_wrprotect(pte);
1da177e4
LT
1031 }
1032
1033 /*
1034 * If it's a shared mapping, mark it clean in
1035 * the child
1036 */
1037 if (vm_flags & VM_SHARED)
1038 pte = pte_mkclean(pte);
1039 pte = pte_mkold(pte);
6aab341e
LT
1040
1041 page = vm_normal_page(vma, addr, pte);
1042 if (page) {
1043 get_page(page);
53f9263b 1044 page_dup_rmap(page, false);
eca56ff9 1045 rss[mm_counter(page)]++;
df6ad698
JG
1046 } else if (pte_devmap(pte)) {
1047 page = pte_page(pte);
1048
1049 /*
1050 * Cache coherent device memory behave like regular page and
1051 * not like persistent memory page. For more informations see
1052 * MEMORY_DEVICE_CACHE_COHERENT in memory_hotplug.h
1053 */
1054 if (is_device_public_page(page)) {
1055 get_page(page);
1056 page_dup_rmap(page, false);
1057 rss[mm_counter(page)]++;
1058 }
6aab341e 1059 }
ae859762
HD
1060
1061out_set_pte:
1062 set_pte_at(dst_mm, addr, dst_pte, pte);
570a335b 1063 return 0;
1da177e4
LT
1064}
1065
21bda264 1066static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
71e3aac0
AA
1067 pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
1068 unsigned long addr, unsigned long end)
1da177e4 1069{
c36987e2 1070 pte_t *orig_src_pte, *orig_dst_pte;
1da177e4 1071 pte_t *src_pte, *dst_pte;
c74df32c 1072 spinlock_t *src_ptl, *dst_ptl;
e040f218 1073 int progress = 0;
d559db08 1074 int rss[NR_MM_COUNTERS];
570a335b 1075 swp_entry_t entry = (swp_entry_t){0};
1da177e4
LT
1076
1077again:
d559db08
KH
1078 init_rss_vec(rss);
1079
c74df32c 1080 dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
1da177e4
LT
1081 if (!dst_pte)
1082 return -ENOMEM;
ece0e2b6 1083 src_pte = pte_offset_map(src_pmd, addr);
4c21e2f2 1084 src_ptl = pte_lockptr(src_mm, src_pmd);
f20dc5f7 1085 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
c36987e2
DN
1086 orig_src_pte = src_pte;
1087 orig_dst_pte = dst_pte;
6606c3e0 1088 arch_enter_lazy_mmu_mode();
1da177e4 1089
1da177e4
LT
1090 do {
1091 /*
1092 * We are holding two locks at this point - either of them
1093 * could generate latencies in another task on another CPU.
1094 */
e040f218
HD
1095 if (progress >= 32) {
1096 progress = 0;
1097 if (need_resched() ||
95c354fe 1098 spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
e040f218
HD
1099 break;
1100 }
1da177e4
LT
1101 if (pte_none(*src_pte)) {
1102 progress++;
1103 continue;
1104 }
570a335b
HD
1105 entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
1106 vma, addr, rss);
1107 if (entry.val)
1108 break;
1da177e4
LT
1109 progress += 8;
1110 } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
1da177e4 1111
6606c3e0 1112 arch_leave_lazy_mmu_mode();
c74df32c 1113 spin_unlock(src_ptl);
ece0e2b6 1114 pte_unmap(orig_src_pte);
d559db08 1115 add_mm_rss_vec(dst_mm, rss);
c36987e2 1116 pte_unmap_unlock(orig_dst_pte, dst_ptl);
c74df32c 1117 cond_resched();
570a335b
HD
1118
1119 if (entry.val) {
1120 if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
1121 return -ENOMEM;
1122 progress = 0;
1123 }
1da177e4
LT
1124 if (addr != end)
1125 goto again;
1126 return 0;
1127}
1128
1129static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1130 pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
1131 unsigned long addr, unsigned long end)
1132{
1133 pmd_t *src_pmd, *dst_pmd;
1134 unsigned long next;
1135
1136 dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
1137 if (!dst_pmd)
1138 return -ENOMEM;
1139 src_pmd = pmd_offset(src_pud, addr);
1140 do {
1141 next = pmd_addr_end(addr, end);
84c3fc4e
ZY
1142 if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd)
1143 || pmd_devmap(*src_pmd)) {
71e3aac0 1144 int err;
a00cc7d9 1145 VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, vma);
71e3aac0
AA
1146 err = copy_huge_pmd(dst_mm, src_mm,
1147 dst_pmd, src_pmd, addr, vma);
1148 if (err == -ENOMEM)
1149 return -ENOMEM;
1150 if (!err)
1151 continue;
1152 /* fall through */
1153 }
1da177e4
LT
1154 if (pmd_none_or_clear_bad(src_pmd))
1155 continue;
1156 if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
1157 vma, addr, next))
1158 return -ENOMEM;
1159 } while (dst_pmd++, src_pmd++, addr = next, addr != end);
1160 return 0;
1161}
1162
1163static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
c2febafc 1164 p4d_t *dst_p4d, p4d_t *src_p4d, struct vm_area_struct *vma,
1da177e4
LT
1165 unsigned long addr, unsigned long end)
1166{
1167 pud_t *src_pud, *dst_pud;
1168 unsigned long next;
1169
c2febafc 1170 dst_pud = pud_alloc(dst_mm, dst_p4d, addr);
1da177e4
LT
1171 if (!dst_pud)
1172 return -ENOMEM;
c2febafc 1173 src_pud = pud_offset(src_p4d, addr);
1da177e4
LT
1174 do {
1175 next = pud_addr_end(addr, end);
a00cc7d9
MW
1176 if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) {
1177 int err;
1178
1179 VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, vma);
1180 err = copy_huge_pud(dst_mm, src_mm,
1181 dst_pud, src_pud, addr, vma);
1182 if (err == -ENOMEM)
1183 return -ENOMEM;
1184 if (!err)
1185 continue;
1186 /* fall through */
1187 }
1da177e4
LT
1188 if (pud_none_or_clear_bad(src_pud))
1189 continue;
1190 if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
1191 vma, addr, next))
1192 return -ENOMEM;
1193 } while (dst_pud++, src_pud++, addr = next, addr != end);
1194 return 0;
1195}
1196
c2febafc
KS
1197static inline int copy_p4d_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1198 pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
1199 unsigned long addr, unsigned long end)
1200{
1201 p4d_t *src_p4d, *dst_p4d;
1202 unsigned long next;
1203
1204 dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr);
1205 if (!dst_p4d)
1206 return -ENOMEM;
1207 src_p4d = p4d_offset(src_pgd, addr);
1208 do {
1209 next = p4d_addr_end(addr, end);
1210 if (p4d_none_or_clear_bad(src_p4d))
1211 continue;
1212 if (copy_pud_range(dst_mm, src_mm, dst_p4d, src_p4d,
1213 vma, addr, next))
1214 return -ENOMEM;
1215 } while (dst_p4d++, src_p4d++, addr = next, addr != end);
1216 return 0;
1217}
1218
1da177e4
LT
1219int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1220 struct vm_area_struct *vma)
1221{
1222 pgd_t *src_pgd, *dst_pgd;
1223 unsigned long next;
1224 unsigned long addr = vma->vm_start;
1225 unsigned long end = vma->vm_end;
2ec74c3e
SG
1226 unsigned long mmun_start; /* For mmu_notifiers */
1227 unsigned long mmun_end; /* For mmu_notifiers */
1228 bool is_cow;
cddb8a5c 1229 int ret;
1da177e4 1230
d992895b
NP
1231 /*
1232 * Don't copy ptes where a page fault will fill them correctly.
1233 * Fork becomes much lighter when there are big shared or private
1234 * readonly mappings. The tradeoff is that copy_page_range is more
1235 * efficient than faulting.
1236 */
0661a336
KS
1237 if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) &&
1238 !vma->anon_vma)
1239 return 0;
d992895b 1240
1da177e4
LT
1241 if (is_vm_hugetlb_page(vma))
1242 return copy_hugetlb_page_range(dst_mm, src_mm, vma);
1243
b3b9c293 1244 if (unlikely(vma->vm_flags & VM_PFNMAP)) {
2ab64037 1245 /*
1246 * We do not free on error cases below as remove_vma
1247 * gets called on error from higher level routine
1248 */
5180da41 1249 ret = track_pfn_copy(vma);
2ab64037 1250 if (ret)
1251 return ret;
1252 }
1253
cddb8a5c
AA
1254 /*
1255 * We need to invalidate the secondary MMU mappings only when
1256 * there could be a permission downgrade on the ptes of the
1257 * parent mm. And a permission downgrade will only happen if
1258 * is_cow_mapping() returns true.
1259 */
2ec74c3e
SG
1260 is_cow = is_cow_mapping(vma->vm_flags);
1261 mmun_start = addr;
1262 mmun_end = end;
1263 if (is_cow)
1264 mmu_notifier_invalidate_range_start(src_mm, mmun_start,
1265 mmun_end);
cddb8a5c
AA
1266
1267 ret = 0;
1da177e4
LT
1268 dst_pgd = pgd_offset(dst_mm, addr);
1269 src_pgd = pgd_offset(src_mm, addr);
1270 do {
1271 next = pgd_addr_end(addr, end);
1272 if (pgd_none_or_clear_bad(src_pgd))
1273 continue;
c2febafc 1274 if (unlikely(copy_p4d_range(dst_mm, src_mm, dst_pgd, src_pgd,
cddb8a5c
AA
1275 vma, addr, next))) {
1276 ret = -ENOMEM;
1277 break;
1278 }
1da177e4 1279 } while (dst_pgd++, src_pgd++, addr = next, addr != end);
cddb8a5c 1280
2ec74c3e
SG
1281 if (is_cow)
1282 mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end);
cddb8a5c 1283 return ret;
1da177e4
LT
1284}
1285
51c6f666 1286static unsigned long zap_pte_range(struct mmu_gather *tlb,
b5810039 1287 struct vm_area_struct *vma, pmd_t *pmd,
1da177e4 1288 unsigned long addr, unsigned long end,
97a89413 1289 struct zap_details *details)
1da177e4 1290{
b5810039 1291 struct mm_struct *mm = tlb->mm;
d16dfc55 1292 int force_flush = 0;
d559db08 1293 int rss[NR_MM_COUNTERS];
97a89413 1294 spinlock_t *ptl;
5f1a1907 1295 pte_t *start_pte;
97a89413 1296 pte_t *pte;
8a5f14a2 1297 swp_entry_t entry;
d559db08 1298
07e32661 1299 tlb_remove_check_page_size_change(tlb, PAGE_SIZE);
d16dfc55 1300again:
e303297e 1301 init_rss_vec(rss);
5f1a1907
SR
1302 start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
1303 pte = start_pte;
3ea27719 1304 flush_tlb_batched_pending(mm);
6606c3e0 1305 arch_enter_lazy_mmu_mode();
1da177e4
LT
1306 do {
1307 pte_t ptent = *pte;
166f61b9 1308 if (pte_none(ptent))
1da177e4 1309 continue;
6f5e6b9e 1310
1da177e4 1311 if (pte_present(ptent)) {
ee498ed7 1312 struct page *page;
51c6f666 1313
df6ad698 1314 page = _vm_normal_page(vma, addr, ptent, true);
1da177e4
LT
1315 if (unlikely(details) && page) {
1316 /*
1317 * unmap_shared_mapping_pages() wants to
1318 * invalidate cache without truncating:
1319 * unmap shared but keep private pages.
1320 */
1321 if (details->check_mapping &&
800d8c63 1322 details->check_mapping != page_rmapping(page))
1da177e4 1323 continue;
1da177e4 1324 }
b5810039 1325 ptent = ptep_get_and_clear_full(mm, addr, pte,
a600388d 1326 tlb->fullmm);
1da177e4
LT
1327 tlb_remove_tlb_entry(tlb, pte, addr);
1328 if (unlikely(!page))
1329 continue;
eca56ff9
JM
1330
1331 if (!PageAnon(page)) {
1cf35d47
LT
1332 if (pte_dirty(ptent)) {
1333 force_flush = 1;
6237bcd9 1334 set_page_dirty(page);
1cf35d47 1335 }
4917e5d0 1336 if (pte_young(ptent) &&
64363aad 1337 likely(!(vma->vm_flags & VM_SEQ_READ)))
bf3f3bc5 1338 mark_page_accessed(page);
6237bcd9 1339 }
eca56ff9 1340 rss[mm_counter(page)]--;
d281ee61 1341 page_remove_rmap(page, false);
3dc14741
HD
1342 if (unlikely(page_mapcount(page) < 0))
1343 print_bad_pte(vma, addr, ptent, page);
e9d55e15 1344 if (unlikely(__tlb_remove_page(tlb, page))) {
1cf35d47 1345 force_flush = 1;
ce9ec37b 1346 addr += PAGE_SIZE;
d16dfc55 1347 break;
1cf35d47 1348 }
1da177e4
LT
1349 continue;
1350 }
5042db43
JG
1351
1352 entry = pte_to_swp_entry(ptent);
1353 if (non_swap_entry(entry) && is_device_private_entry(entry)) {
1354 struct page *page = device_private_entry_to_page(entry);
1355
1356 if (unlikely(details && details->check_mapping)) {
1357 /*
1358 * unmap_shared_mapping_pages() wants to
1359 * invalidate cache without truncating:
1360 * unmap shared but keep private pages.
1361 */
1362 if (details->check_mapping !=
1363 page_rmapping(page))
1364 continue;
1365 }
1366
1367 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
1368 rss[mm_counter(page)]--;
1369 page_remove_rmap(page, false);
1370 put_page(page);
1371 continue;
1372 }
1373
3e8715fd
KS
1374 /* If details->check_mapping, we leave swap entries. */
1375 if (unlikely(details))
1da177e4 1376 continue;
b084d435 1377
8a5f14a2
KS
1378 entry = pte_to_swp_entry(ptent);
1379 if (!non_swap_entry(entry))
1380 rss[MM_SWAPENTS]--;
1381 else if (is_migration_entry(entry)) {
1382 struct page *page;
9f9f1acd 1383
8a5f14a2 1384 page = migration_entry_to_page(entry);
eca56ff9 1385 rss[mm_counter(page)]--;
b084d435 1386 }
8a5f14a2
KS
1387 if (unlikely(!free_swap_and_cache(entry)))
1388 print_bad_pte(vma, addr, ptent, NULL);
9888a1ca 1389 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
97a89413 1390 } while (pte++, addr += PAGE_SIZE, addr != end);
ae859762 1391
d559db08 1392 add_mm_rss_vec(mm, rss);
6606c3e0 1393 arch_leave_lazy_mmu_mode();
51c6f666 1394
1cf35d47 1395 /* Do the actual TLB flush before dropping ptl */
fb7332a9 1396 if (force_flush)
1cf35d47 1397 tlb_flush_mmu_tlbonly(tlb);
1cf35d47
LT
1398 pte_unmap_unlock(start_pte, ptl);
1399
1400 /*
1401 * If we forced a TLB flush (either due to running out of
1402 * batch buffers or because we needed to flush dirty TLB
1403 * entries before releasing the ptl), free the batched
1404 * memory too. Restart if we didn't do everything.
1405 */
1406 if (force_flush) {
1407 force_flush = 0;
1408 tlb_flush_mmu_free(tlb);
2b047252 1409 if (addr != end)
d16dfc55
PZ
1410 goto again;
1411 }
1412
51c6f666 1413 return addr;
1da177e4
LT
1414}
1415
51c6f666 1416static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
b5810039 1417 struct vm_area_struct *vma, pud_t *pud,
1da177e4 1418 unsigned long addr, unsigned long end,
97a89413 1419 struct zap_details *details)
1da177e4
LT
1420{
1421 pmd_t *pmd;
1422 unsigned long next;
1423
1424 pmd = pmd_offset(pud, addr);
1425 do {
1426 next = pmd_addr_end(addr, end);
84c3fc4e 1427 if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
1a5a9906 1428 if (next - addr != HPAGE_PMD_SIZE) {
68428398
HD
1429 VM_BUG_ON_VMA(vma_is_anonymous(vma) &&
1430 !rwsem_is_locked(&tlb->mm->mmap_sem), vma);
fd60775a 1431 __split_huge_pmd(vma, pmd, addr, false, NULL);
f21760b1 1432 } else if (zap_huge_pmd(tlb, vma, pmd, addr))
1a5a9906 1433 goto next;
71e3aac0
AA
1434 /* fall through */
1435 }
1a5a9906
AA
1436 /*
1437 * Here there can be other concurrent MADV_DONTNEED or
1438 * trans huge page faults running, and if the pmd is
1439 * none or trans huge it can change under us. This is
1440 * because MADV_DONTNEED holds the mmap_sem in read
1441 * mode.
1442 */
1443 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
1444 goto next;
97a89413 1445 next = zap_pte_range(tlb, vma, pmd, addr, next, details);
1a5a9906 1446next:
97a89413
PZ
1447 cond_resched();
1448 } while (pmd++, addr = next, addr != end);
51c6f666
RH
1449
1450 return addr;
1da177e4
LT
1451}
1452
51c6f666 1453static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
c2febafc 1454 struct vm_area_struct *vma, p4d_t *p4d,
1da177e4 1455 unsigned long addr, unsigned long end,
97a89413 1456 struct zap_details *details)
1da177e4
LT
1457{
1458 pud_t *pud;
1459 unsigned long next;
1460
c2febafc 1461 pud = pud_offset(p4d, addr);
1da177e4
LT
1462 do {
1463 next = pud_addr_end(addr, end);
a00cc7d9
MW
1464 if (pud_trans_huge(*pud) || pud_devmap(*pud)) {
1465 if (next - addr != HPAGE_PUD_SIZE) {
1466 VM_BUG_ON_VMA(!rwsem_is_locked(&tlb->mm->mmap_sem), vma);
1467 split_huge_pud(vma, pud, addr);
1468 } else if (zap_huge_pud(tlb, vma, pud, addr))
1469 goto next;
1470 /* fall through */
1471 }
97a89413 1472 if (pud_none_or_clear_bad(pud))
1da177e4 1473 continue;
97a89413 1474 next = zap_pmd_range(tlb, vma, pud, addr, next, details);
a00cc7d9
MW
1475next:
1476 cond_resched();
97a89413 1477 } while (pud++, addr = next, addr != end);
51c6f666
RH
1478
1479 return addr;
1da177e4
LT
1480}
1481
c2febafc
KS
1482static inline unsigned long zap_p4d_range(struct mmu_gather *tlb,
1483 struct vm_area_struct *vma, pgd_t *pgd,
1484 unsigned long addr, unsigned long end,
1485 struct zap_details *details)
1486{
1487 p4d_t *p4d;
1488 unsigned long next;
1489
1490 p4d = p4d_offset(pgd, addr);
1491 do {
1492 next = p4d_addr_end(addr, end);
1493 if (p4d_none_or_clear_bad(p4d))
1494 continue;
1495 next = zap_pud_range(tlb, vma, p4d, addr, next, details);
1496 } while (p4d++, addr = next, addr != end);
1497
1498 return addr;
1499}
1500
aac45363 1501void unmap_page_range(struct mmu_gather *tlb,
038c7aa1
AV
1502 struct vm_area_struct *vma,
1503 unsigned long addr, unsigned long end,
1504 struct zap_details *details)
1da177e4
LT
1505{
1506 pgd_t *pgd;
1507 unsigned long next;
1508
1da177e4
LT
1509 BUG_ON(addr >= end);
1510 tlb_start_vma(tlb, vma);
1511 pgd = pgd_offset(vma->vm_mm, addr);
1512 do {
1513 next = pgd_addr_end(addr, end);
97a89413 1514 if (pgd_none_or_clear_bad(pgd))
1da177e4 1515 continue;
c2febafc 1516 next = zap_p4d_range(tlb, vma, pgd, addr, next, details);
97a89413 1517 } while (pgd++, addr = next, addr != end);
1da177e4
LT
1518 tlb_end_vma(tlb, vma);
1519}
51c6f666 1520
f5cc4eef
AV
1521
1522static void unmap_single_vma(struct mmu_gather *tlb,
1523 struct vm_area_struct *vma, unsigned long start_addr,
4f74d2c8 1524 unsigned long end_addr,
f5cc4eef
AV
1525 struct zap_details *details)
1526{
1527 unsigned long start = max(vma->vm_start, start_addr);
1528 unsigned long end;
1529
1530 if (start >= vma->vm_end)
1531 return;
1532 end = min(vma->vm_end, end_addr);
1533 if (end <= vma->vm_start)
1534 return;
1535
cbc91f71
SD
1536 if (vma->vm_file)
1537 uprobe_munmap(vma, start, end);
1538
b3b9c293 1539 if (unlikely(vma->vm_flags & VM_PFNMAP))
5180da41 1540 untrack_pfn(vma, 0, 0);
f5cc4eef
AV
1541
1542 if (start != end) {
1543 if (unlikely(is_vm_hugetlb_page(vma))) {
1544 /*
1545 * It is undesirable to test vma->vm_file as it
1546 * should be non-null for valid hugetlb area.
1547 * However, vm_file will be NULL in the error
7aa6b4ad 1548 * cleanup path of mmap_region. When
f5cc4eef 1549 * hugetlbfs ->mmap method fails,
7aa6b4ad 1550 * mmap_region() nullifies vma->vm_file
f5cc4eef
AV
1551 * before calling this function to clean up.
1552 * Since no pte has actually been setup, it is
1553 * safe to do nothing in this case.
1554 */
24669e58 1555 if (vma->vm_file) {
83cde9e8 1556 i_mmap_lock_write(vma->vm_file->f_mapping);
d833352a 1557 __unmap_hugepage_range_final(tlb, vma, start, end, NULL);
83cde9e8 1558 i_mmap_unlock_write(vma->vm_file->f_mapping);
24669e58 1559 }
f5cc4eef
AV
1560 } else
1561 unmap_page_range(tlb, vma, start, end, details);
1562 }
1da177e4
LT
1563}
1564
1da177e4
LT
1565/**
1566 * unmap_vmas - unmap a range of memory covered by a list of vma's
0164f69d 1567 * @tlb: address of the caller's struct mmu_gather
1da177e4
LT
1568 * @vma: the starting vma
1569 * @start_addr: virtual address at which to start unmapping
1570 * @end_addr: virtual address at which to end unmapping
1da177e4 1571 *
508034a3 1572 * Unmap all pages in the vma list.
1da177e4 1573 *
1da177e4
LT
1574 * Only addresses between `start' and `end' will be unmapped.
1575 *
1576 * The VMA list must be sorted in ascending virtual address order.
1577 *
1578 * unmap_vmas() assumes that the caller will flush the whole unmapped address
1579 * range after unmap_vmas() returns. So the only responsibility here is to
1580 * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
1581 * drops the lock and schedules.
1582 */
6e8bb019 1583void unmap_vmas(struct mmu_gather *tlb,
1da177e4 1584 struct vm_area_struct *vma, unsigned long start_addr,
4f74d2c8 1585 unsigned long end_addr)
1da177e4 1586{
cddb8a5c 1587 struct mm_struct *mm = vma->vm_mm;
1da177e4 1588
cddb8a5c 1589 mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
f5cc4eef 1590 for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
4f74d2c8 1591 unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
cddb8a5c 1592 mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
1da177e4
LT
1593}
1594
1595/**
1596 * zap_page_range - remove user pages in a given range
1597 * @vma: vm_area_struct holding the applicable pages
eb4546bb 1598 * @start: starting address of pages to zap
1da177e4 1599 * @size: number of bytes to zap
f5cc4eef
AV
1600 *
1601 * Caller must protect the VMA list
1da177e4 1602 */
7e027b14 1603void zap_page_range(struct vm_area_struct *vma, unsigned long start,
ecf1385d 1604 unsigned long size)
1da177e4
LT
1605{
1606 struct mm_struct *mm = vma->vm_mm;
d16dfc55 1607 struct mmu_gather tlb;
7e027b14 1608 unsigned long end = start + size;
1da177e4 1609
1da177e4 1610 lru_add_drain();
2b047252 1611 tlb_gather_mmu(&tlb, mm, start, end);
365e9c87 1612 update_hiwater_rss(mm);
7e027b14 1613 mmu_notifier_invalidate_range_start(mm, start, end);
4647706e 1614 for ( ; vma && vma->vm_start < end; vma = vma->vm_next) {
ecf1385d 1615 unmap_single_vma(&tlb, vma, start, end, NULL);
4647706e
MG
1616
1617 /*
1618 * zap_page_range does not specify whether mmap_sem should be
1619 * held for read or write. That allows parallel zap_page_range
1620 * operations to unmap a PTE and defer a flush meaning that
1621 * this call observes pte_none and fails to flush the TLB.
1622 * Rather than adding a complex API, ensure that no stale
1623 * TLB entries exist when this call returns.
1624 */
1625 flush_tlb_range(vma, start, end);
1626 }
1627
7e027b14
LT
1628 mmu_notifier_invalidate_range_end(mm, start, end);
1629 tlb_finish_mmu(&tlb, start, end);
1da177e4
LT
1630}
1631
f5cc4eef
AV
1632/**
1633 * zap_page_range_single - remove user pages in a given range
1634 * @vma: vm_area_struct holding the applicable pages
1635 * @address: starting address of pages to zap
1636 * @size: number of bytes to zap
8a5f14a2 1637 * @details: details of shared cache invalidation
f5cc4eef
AV
1638 *
1639 * The range must fit into one VMA.
1da177e4 1640 */
f5cc4eef 1641static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
1da177e4
LT
1642 unsigned long size, struct zap_details *details)
1643{
1644 struct mm_struct *mm = vma->vm_mm;
d16dfc55 1645 struct mmu_gather tlb;
1da177e4 1646 unsigned long end = address + size;
1da177e4 1647
1da177e4 1648 lru_add_drain();
2b047252 1649 tlb_gather_mmu(&tlb, mm, address, end);
365e9c87 1650 update_hiwater_rss(mm);
f5cc4eef 1651 mmu_notifier_invalidate_range_start(mm, address, end);
4f74d2c8 1652 unmap_single_vma(&tlb, vma, address, end, details);
f5cc4eef 1653 mmu_notifier_invalidate_range_end(mm, address, end);
d16dfc55 1654 tlb_finish_mmu(&tlb, address, end);
1da177e4
LT
1655}
1656
c627f9cc
JS
1657/**
1658 * zap_vma_ptes - remove ptes mapping the vma
1659 * @vma: vm_area_struct holding ptes to be zapped
1660 * @address: starting address of pages to zap
1661 * @size: number of bytes to zap
1662 *
1663 * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1664 *
1665 * The entire address range must be fully contained within the vma.
1666 *
1667 * Returns 0 if successful.
1668 */
1669int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
1670 unsigned long size)
1671{
1672 if (address < vma->vm_start || address + size > vma->vm_end ||
1673 !(vma->vm_flags & VM_PFNMAP))
1674 return -1;
f5cc4eef 1675 zap_page_range_single(vma, address, size, NULL);
c627f9cc
JS
1676 return 0;
1677}
1678EXPORT_SYMBOL_GPL(zap_vma_ptes);
1679
25ca1d6c 1680pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
920c7a5d 1681 spinlock_t **ptl)
c9cfcddf 1682{
c2febafc
KS
1683 pgd_t *pgd;
1684 p4d_t *p4d;
1685 pud_t *pud;
1686 pmd_t *pmd;
1687
1688 pgd = pgd_offset(mm, addr);
1689 p4d = p4d_alloc(mm, pgd, addr);
1690 if (!p4d)
1691 return NULL;
1692 pud = pud_alloc(mm, p4d, addr);
1693 if (!pud)
1694 return NULL;
1695 pmd = pmd_alloc(mm, pud, addr);
1696 if (!pmd)
1697 return NULL;
1698
1699 VM_BUG_ON(pmd_trans_huge(*pmd));
1700 return pte_alloc_map_lock(mm, pmd, addr, ptl);
c9cfcddf
LT
1701}
1702
238f58d8
LT
1703/*
1704 * This is the old fallback for page remapping.
1705 *
1706 * For historical reasons, it only allows reserved pages. Only
1707 * old drivers should use this, and they needed to mark their
1708 * pages reserved for the old functions anyway.
1709 */
423bad60
NP
1710static int insert_page(struct vm_area_struct *vma, unsigned long addr,
1711 struct page *page, pgprot_t prot)
238f58d8 1712{
423bad60 1713 struct mm_struct *mm = vma->vm_mm;
238f58d8 1714 int retval;
c9cfcddf 1715 pte_t *pte;
8a9f3ccd
BS
1716 spinlock_t *ptl;
1717
238f58d8 1718 retval = -EINVAL;
a145dd41 1719 if (PageAnon(page))
5b4e655e 1720 goto out;
238f58d8
LT
1721 retval = -ENOMEM;
1722 flush_dcache_page(page);
c9cfcddf 1723 pte = get_locked_pte(mm, addr, &ptl);
238f58d8 1724 if (!pte)
5b4e655e 1725 goto out;
238f58d8
LT
1726 retval = -EBUSY;
1727 if (!pte_none(*pte))
1728 goto out_unlock;
1729
1730 /* Ok, finally just insert the thing.. */
1731 get_page(page);
eca56ff9 1732 inc_mm_counter_fast(mm, mm_counter_file(page));
dd78fedd 1733 page_add_file_rmap(page, false);
238f58d8
LT
1734 set_pte_at(mm, addr, pte, mk_pte(page, prot));
1735
1736 retval = 0;
8a9f3ccd
BS
1737 pte_unmap_unlock(pte, ptl);
1738 return retval;
238f58d8
LT
1739out_unlock:
1740 pte_unmap_unlock(pte, ptl);
1741out:
1742 return retval;
1743}
1744
bfa5bf6d
REB
1745/**
1746 * vm_insert_page - insert single page into user vma
1747 * @vma: user vma to map to
1748 * @addr: target user address of this page
1749 * @page: source kernel page
1750 *
a145dd41
LT
1751 * This allows drivers to insert individual pages they've allocated
1752 * into a user vma.
1753 *
1754 * The page has to be a nice clean _individual_ kernel allocation.
1755 * If you allocate a compound page, you need to have marked it as
1756 * such (__GFP_COMP), or manually just split the page up yourself
8dfcc9ba 1757 * (see split_page()).
a145dd41
LT
1758 *
1759 * NOTE! Traditionally this was done with "remap_pfn_range()" which
1760 * took an arbitrary page protection parameter. This doesn't allow
1761 * that. Your vma protection will have to be set up correctly, which
1762 * means that if you want a shared writable mapping, you'd better
1763 * ask for a shared writable mapping!
1764 *
1765 * The page does not need to be reserved.
4b6e1e37
KK
1766 *
1767 * Usually this function is called from f_op->mmap() handler
1768 * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
1769 * Caller must set VM_MIXEDMAP on vma if it wants to call this
1770 * function from other places, for example from page-fault handler.
a145dd41 1771 */
423bad60
NP
1772int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
1773 struct page *page)
a145dd41
LT
1774{
1775 if (addr < vma->vm_start || addr >= vma->vm_end)
1776 return -EFAULT;
1777 if (!page_count(page))
1778 return -EINVAL;
4b6e1e37
KK
1779 if (!(vma->vm_flags & VM_MIXEDMAP)) {
1780 BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem));
1781 BUG_ON(vma->vm_flags & VM_PFNMAP);
1782 vma->vm_flags |= VM_MIXEDMAP;
1783 }
423bad60 1784 return insert_page(vma, addr, page, vma->vm_page_prot);
a145dd41 1785}
e3c3374f 1786EXPORT_SYMBOL(vm_insert_page);
a145dd41 1787
423bad60 1788static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
b2770da6 1789 pfn_t pfn, pgprot_t prot, bool mkwrite)
423bad60
NP
1790{
1791 struct mm_struct *mm = vma->vm_mm;
1792 int retval;
1793 pte_t *pte, entry;
1794 spinlock_t *ptl;
1795
1796 retval = -ENOMEM;
1797 pte = get_locked_pte(mm, addr, &ptl);
1798 if (!pte)
1799 goto out;
1800 retval = -EBUSY;
b2770da6
RZ
1801 if (!pte_none(*pte)) {
1802 if (mkwrite) {
1803 /*
1804 * For read faults on private mappings the PFN passed
1805 * in may not match the PFN we have mapped if the
1806 * mapped PFN is a writeable COW page. In the mkwrite
1807 * case we are creating a writable PTE for a shared
1808 * mapping and we expect the PFNs to match.
1809 */
1810 if (WARN_ON_ONCE(pte_pfn(*pte) != pfn_t_to_pfn(pfn)))
1811 goto out_unlock;
1812 entry = *pte;
1813 goto out_mkwrite;
1814 } else
1815 goto out_unlock;
1816 }
423bad60
NP
1817
1818 /* Ok, finally just insert the thing.. */
01c8f1c4
DW
1819 if (pfn_t_devmap(pfn))
1820 entry = pte_mkdevmap(pfn_t_pte(pfn, prot));
1821 else
1822 entry = pte_mkspecial(pfn_t_pte(pfn, prot));
b2770da6
RZ
1823
1824out_mkwrite:
1825 if (mkwrite) {
1826 entry = pte_mkyoung(entry);
1827 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1828 }
1829
423bad60 1830 set_pte_at(mm, addr, pte, entry);
4b3073e1 1831 update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
423bad60
NP
1832
1833 retval = 0;
1834out_unlock:
1835 pte_unmap_unlock(pte, ptl);
1836out:
1837 return retval;
1838}
1839
e0dc0d8f
NP
1840/**
1841 * vm_insert_pfn - insert single pfn into user vma
1842 * @vma: user vma to map to
1843 * @addr: target user address of this page
1844 * @pfn: source kernel pfn
1845 *
c462f179 1846 * Similar to vm_insert_page, this allows drivers to insert individual pages
e0dc0d8f
NP
1847 * they've allocated into a user vma. Same comments apply.
1848 *
1849 * This function should only be called from a vm_ops->fault handler, and
1850 * in that case the handler should return NULL.
0d71d10a
NP
1851 *
1852 * vma cannot be a COW mapping.
1853 *
1854 * As this is called only for pages that do not currently exist, we
1855 * do not need to flush old virtual caches or the TLB.
e0dc0d8f
NP
1856 */
1857int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
423bad60 1858 unsigned long pfn)
1745cbc5
AL
1859{
1860 return vm_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot);
1861}
1862EXPORT_SYMBOL(vm_insert_pfn);
1863
1864/**
1865 * vm_insert_pfn_prot - insert single pfn into user vma with specified pgprot
1866 * @vma: user vma to map to
1867 * @addr: target user address of this page
1868 * @pfn: source kernel pfn
1869 * @pgprot: pgprot flags for the inserted page
1870 *
1871 * This is exactly like vm_insert_pfn, except that it allows drivers to
1872 * to override pgprot on a per-page basis.
1873 *
1874 * This only makes sense for IO mappings, and it makes no sense for
1875 * cow mappings. In general, using multiple vmas is preferable;
1876 * vm_insert_pfn_prot should only be used if using multiple VMAs is
1877 * impractical.
1878 */
1879int vm_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
1880 unsigned long pfn, pgprot_t pgprot)
e0dc0d8f 1881{
2ab64037 1882 int ret;
7e675137
NP
1883 /*
1884 * Technically, architectures with pte_special can avoid all these
1885 * restrictions (same for remap_pfn_range). However we would like
1886 * consistency in testing and feature parity among all, so we should
1887 * try to keep these invariants in place for everybody.
1888 */
b379d790
JH
1889 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
1890 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
1891 (VM_PFNMAP|VM_MIXEDMAP));
1892 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
1893 BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
e0dc0d8f 1894
423bad60
NP
1895 if (addr < vma->vm_start || addr >= vma->vm_end)
1896 return -EFAULT;
308a047c
BP
1897
1898 track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV));
2ab64037 1899
b2770da6
RZ
1900 ret = insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot,
1901 false);
2ab64037 1902
2ab64037 1903 return ret;
423bad60 1904}
1745cbc5 1905EXPORT_SYMBOL(vm_insert_pfn_prot);
e0dc0d8f 1906
b2770da6
RZ
1907static int __vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
1908 pfn_t pfn, bool mkwrite)
423bad60 1909{
87744ab3
DW
1910 pgprot_t pgprot = vma->vm_page_prot;
1911
423bad60 1912 BUG_ON(!(vma->vm_flags & VM_MIXEDMAP));
e0dc0d8f 1913
423bad60
NP
1914 if (addr < vma->vm_start || addr >= vma->vm_end)
1915 return -EFAULT;
308a047c
BP
1916
1917 track_pfn_insert(vma, &pgprot, pfn);
e0dc0d8f 1918
423bad60
NP
1919 /*
1920 * If we don't have pte special, then we have to use the pfn_valid()
1921 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
1922 * refcount the page if pfn_valid is true (hence insert_page rather
62eede62
HD
1923 * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP
1924 * without pte special, it would there be refcounted as a normal page.
423bad60 1925 */
03fc2da6 1926 if (!HAVE_PTE_SPECIAL && !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) {
423bad60
NP
1927 struct page *page;
1928
03fc2da6
DW
1929 /*
1930 * At this point we are committed to insert_page()
1931 * regardless of whether the caller specified flags that
1932 * result in pfn_t_has_page() == false.
1933 */
1934 page = pfn_to_page(pfn_t_to_pfn(pfn));
87744ab3 1935 return insert_page(vma, addr, page, pgprot);
423bad60 1936 }
b2770da6
RZ
1937 return insert_pfn(vma, addr, pfn, pgprot, mkwrite);
1938}
1939
1940int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
1941 pfn_t pfn)
1942{
1943 return __vm_insert_mixed(vma, addr, pfn, false);
1944
e0dc0d8f 1945}
423bad60 1946EXPORT_SYMBOL(vm_insert_mixed);
e0dc0d8f 1947
b2770da6
RZ
1948int vm_insert_mixed_mkwrite(struct vm_area_struct *vma, unsigned long addr,
1949 pfn_t pfn)
1950{
1951 return __vm_insert_mixed(vma, addr, pfn, true);
1952}
1953EXPORT_SYMBOL(vm_insert_mixed_mkwrite);
1954
1da177e4
LT
1955/*
1956 * maps a range of physical memory into the requested pages. the old
1957 * mappings are removed. any references to nonexistent pages results
1958 * in null mappings (currently treated as "copy-on-access")
1959 */
1960static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
1961 unsigned long addr, unsigned long end,
1962 unsigned long pfn, pgprot_t prot)
1963{
1964 pte_t *pte;
c74df32c 1965 spinlock_t *ptl;
1da177e4 1966
c74df32c 1967 pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
1da177e4
LT
1968 if (!pte)
1969 return -ENOMEM;
6606c3e0 1970 arch_enter_lazy_mmu_mode();
1da177e4
LT
1971 do {
1972 BUG_ON(!pte_none(*pte));
7e675137 1973 set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
1da177e4
LT
1974 pfn++;
1975 } while (pte++, addr += PAGE_SIZE, addr != end);
6606c3e0 1976 arch_leave_lazy_mmu_mode();
c74df32c 1977 pte_unmap_unlock(pte - 1, ptl);
1da177e4
LT
1978 return 0;
1979}
1980
1981static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
1982 unsigned long addr, unsigned long end,
1983 unsigned long pfn, pgprot_t prot)
1984{
1985 pmd_t *pmd;
1986 unsigned long next;
1987
1988 pfn -= addr >> PAGE_SHIFT;
1989 pmd = pmd_alloc(mm, pud, addr);
1990 if (!pmd)
1991 return -ENOMEM;
f66055ab 1992 VM_BUG_ON(pmd_trans_huge(*pmd));
1da177e4
LT
1993 do {
1994 next = pmd_addr_end(addr, end);
1995 if (remap_pte_range(mm, pmd, addr, next,
1996 pfn + (addr >> PAGE_SHIFT), prot))
1997 return -ENOMEM;
1998 } while (pmd++, addr = next, addr != end);
1999 return 0;
2000}
2001
c2febafc 2002static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d,
1da177e4
LT
2003 unsigned long addr, unsigned long end,
2004 unsigned long pfn, pgprot_t prot)
2005{
2006 pud_t *pud;
2007 unsigned long next;
2008
2009 pfn -= addr >> PAGE_SHIFT;
c2febafc 2010 pud = pud_alloc(mm, p4d, addr);
1da177e4
LT
2011 if (!pud)
2012 return -ENOMEM;
2013 do {
2014 next = pud_addr_end(addr, end);
2015 if (remap_pmd_range(mm, pud, addr, next,
2016 pfn + (addr >> PAGE_SHIFT), prot))
2017 return -ENOMEM;
2018 } while (pud++, addr = next, addr != end);
2019 return 0;
2020}
2021
c2febafc
KS
2022static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd,
2023 unsigned long addr, unsigned long end,
2024 unsigned long pfn, pgprot_t prot)
2025{
2026 p4d_t *p4d;
2027 unsigned long next;
2028
2029 pfn -= addr >> PAGE_SHIFT;
2030 p4d = p4d_alloc(mm, pgd, addr);
2031 if (!p4d)
2032 return -ENOMEM;
2033 do {
2034 next = p4d_addr_end(addr, end);
2035 if (remap_pud_range(mm, p4d, addr, next,
2036 pfn + (addr >> PAGE_SHIFT), prot))
2037 return -ENOMEM;
2038 } while (p4d++, addr = next, addr != end);
2039 return 0;
2040}
2041
bfa5bf6d
REB
2042/**
2043 * remap_pfn_range - remap kernel memory to userspace
2044 * @vma: user vma to map to
2045 * @addr: target user address to start at
2046 * @pfn: physical address of kernel memory
2047 * @size: size of map area
2048 * @prot: page protection flags for this mapping
2049 *
2050 * Note: this is only safe if the mm semaphore is held when called.
2051 */
1da177e4
LT
2052int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
2053 unsigned long pfn, unsigned long size, pgprot_t prot)
2054{
2055 pgd_t *pgd;
2056 unsigned long next;
2d15cab8 2057 unsigned long end = addr + PAGE_ALIGN(size);
1da177e4 2058 struct mm_struct *mm = vma->vm_mm;
d5957d2f 2059 unsigned long remap_pfn = pfn;
1da177e4
LT
2060 int err;
2061
2062 /*
2063 * Physically remapped pages are special. Tell the
2064 * rest of the world about it:
2065 * VM_IO tells people not to look at these pages
2066 * (accesses can have side effects).
6aab341e
LT
2067 * VM_PFNMAP tells the core MM that the base pages are just
2068 * raw PFN mappings, and do not have a "struct page" associated
2069 * with them.
314e51b9
KK
2070 * VM_DONTEXPAND
2071 * Disable vma merging and expanding with mremap().
2072 * VM_DONTDUMP
2073 * Omit vma from core dump, even when VM_IO turned off.
fb155c16
LT
2074 *
2075 * There's a horrible special case to handle copy-on-write
2076 * behaviour that some programs depend on. We mark the "original"
2077 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
b3b9c293 2078 * See vm_normal_page() for details.
1da177e4 2079 */
b3b9c293
KK
2080 if (is_cow_mapping(vma->vm_flags)) {
2081 if (addr != vma->vm_start || end != vma->vm_end)
2082 return -EINVAL;
fb155c16 2083 vma->vm_pgoff = pfn;
b3b9c293
KK
2084 }
2085
d5957d2f 2086 err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size));
b3b9c293 2087 if (err)
3c8bb73a 2088 return -EINVAL;
fb155c16 2089
314e51b9 2090 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1da177e4
LT
2091
2092 BUG_ON(addr >= end);
2093 pfn -= addr >> PAGE_SHIFT;
2094 pgd = pgd_offset(mm, addr);
2095 flush_cache_range(vma, addr, end);
1da177e4
LT
2096 do {
2097 next = pgd_addr_end(addr, end);
c2febafc 2098 err = remap_p4d_range(mm, pgd, addr, next,
1da177e4
LT
2099 pfn + (addr >> PAGE_SHIFT), prot);
2100 if (err)
2101 break;
2102 } while (pgd++, addr = next, addr != end);
2ab64037 2103
2104 if (err)
d5957d2f 2105 untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size));
2ab64037 2106
1da177e4
LT
2107 return err;
2108}
2109EXPORT_SYMBOL(remap_pfn_range);
2110
b4cbb197
LT
2111/**
2112 * vm_iomap_memory - remap memory to userspace
2113 * @vma: user vma to map to
2114 * @start: start of area
2115 * @len: size of area
2116 *
2117 * This is a simplified io_remap_pfn_range() for common driver use. The
2118 * driver just needs to give us the physical memory range to be mapped,
2119 * we'll figure out the rest from the vma information.
2120 *
2121 * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
2122 * whatever write-combining details or similar.
2123 */
2124int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
2125{
2126 unsigned long vm_len, pfn, pages;
2127
2128 /* Check that the physical memory area passed in looks valid */
2129 if (start + len < start)
2130 return -EINVAL;
2131 /*
2132 * You *really* shouldn't map things that aren't page-aligned,
2133 * but we've historically allowed it because IO memory might
2134 * just have smaller alignment.
2135 */
2136 len += start & ~PAGE_MASK;
2137 pfn = start >> PAGE_SHIFT;
2138 pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
2139 if (pfn + pages < pfn)
2140 return -EINVAL;
2141
2142 /* We start the mapping 'vm_pgoff' pages into the area */
2143 if (vma->vm_pgoff > pages)
2144 return -EINVAL;
2145 pfn += vma->vm_pgoff;
2146 pages -= vma->vm_pgoff;
2147
2148 /* Can we fit all of the mapping? */
2149 vm_len = vma->vm_end - vma->vm_start;
2150 if (vm_len >> PAGE_SHIFT > pages)
2151 return -EINVAL;
2152
2153 /* Ok, let it rip */
2154 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
2155}
2156EXPORT_SYMBOL(vm_iomap_memory);
2157
aee16b3c
JF
2158static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
2159 unsigned long addr, unsigned long end,
2160 pte_fn_t fn, void *data)
2161{
2162 pte_t *pte;
2163 int err;
2f569afd 2164 pgtable_t token;
94909914 2165 spinlock_t *uninitialized_var(ptl);
aee16b3c
JF
2166
2167 pte = (mm == &init_mm) ?
2168 pte_alloc_kernel(pmd, addr) :
2169 pte_alloc_map_lock(mm, pmd, addr, &ptl);
2170 if (!pte)
2171 return -ENOMEM;
2172
2173 BUG_ON(pmd_huge(*pmd));
2174
38e0edb1
JF
2175 arch_enter_lazy_mmu_mode();
2176
2f569afd 2177 token = pmd_pgtable(*pmd);
aee16b3c
JF
2178
2179 do {
c36987e2 2180 err = fn(pte++, token, addr, data);
aee16b3c
JF
2181 if (err)
2182 break;
c36987e2 2183 } while (addr += PAGE_SIZE, addr != end);
aee16b3c 2184
38e0edb1
JF
2185 arch_leave_lazy_mmu_mode();
2186
aee16b3c
JF
2187 if (mm != &init_mm)
2188 pte_unmap_unlock(pte-1, ptl);
2189 return err;
2190}
2191
2192static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
2193 unsigned long addr, unsigned long end,
2194 pte_fn_t fn, void *data)
2195{
2196 pmd_t *pmd;
2197 unsigned long next;
2198 int err;
2199
ceb86879
AK
2200 BUG_ON(pud_huge(*pud));
2201
aee16b3c
JF
2202 pmd = pmd_alloc(mm, pud, addr);
2203 if (!pmd)
2204 return -ENOMEM;
2205 do {
2206 next = pmd_addr_end(addr, end);
2207 err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
2208 if (err)
2209 break;
2210 } while (pmd++, addr = next, addr != end);
2211 return err;
2212}
2213
c2febafc 2214static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d,
aee16b3c
JF
2215 unsigned long addr, unsigned long end,
2216 pte_fn_t fn, void *data)
2217{
2218 pud_t *pud;
2219 unsigned long next;
2220 int err;
2221
c2febafc 2222 pud = pud_alloc(mm, p4d, addr);
aee16b3c
JF
2223 if (!pud)
2224 return -ENOMEM;
2225 do {
2226 next = pud_addr_end(addr, end);
2227 err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
2228 if (err)
2229 break;
2230 } while (pud++, addr = next, addr != end);
2231 return err;
2232}
2233
c2febafc
KS
2234static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd,
2235 unsigned long addr, unsigned long end,
2236 pte_fn_t fn, void *data)
2237{
2238 p4d_t *p4d;
2239 unsigned long next;
2240 int err;
2241
2242 p4d = p4d_alloc(mm, pgd, addr);
2243 if (!p4d)
2244 return -ENOMEM;
2245 do {
2246 next = p4d_addr_end(addr, end);
2247 err = apply_to_pud_range(mm, p4d, addr, next, fn, data);
2248 if (err)
2249 break;
2250 } while (p4d++, addr = next, addr != end);
2251 return err;
2252}
2253
aee16b3c
JF
2254/*
2255 * Scan a region of virtual memory, filling in page tables as necessary
2256 * and calling a provided function on each leaf page table.
2257 */
2258int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2259 unsigned long size, pte_fn_t fn, void *data)
2260{
2261 pgd_t *pgd;
2262 unsigned long next;
57250a5b 2263 unsigned long end = addr + size;
aee16b3c
JF
2264 int err;
2265
9cb65bc3
MP
2266 if (WARN_ON(addr >= end))
2267 return -EINVAL;
2268
aee16b3c
JF
2269 pgd = pgd_offset(mm, addr);
2270 do {
2271 next = pgd_addr_end(addr, end);
c2febafc 2272 err = apply_to_p4d_range(mm, pgd, addr, next, fn, data);
aee16b3c
JF
2273 if (err)
2274 break;
2275 } while (pgd++, addr = next, addr != end);
57250a5b 2276
aee16b3c
JF
2277 return err;
2278}
2279EXPORT_SYMBOL_GPL(apply_to_page_range);
2280
8f4e2101 2281/*
9b4bdd2f
KS
2282 * handle_pte_fault chooses page fault handler according to an entry which was
2283 * read non-atomically. Before making any commitment, on those architectures
2284 * or configurations (e.g. i386 with PAE) which might give a mix of unmatched
2285 * parts, do_swap_page must check under lock before unmapping the pte and
2286 * proceeding (but do_wp_page is only called after already making such a check;
a335b2e1 2287 * and do_anonymous_page can safely check later on).
8f4e2101 2288 */
4c21e2f2 2289static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
8f4e2101
HD
2290 pte_t *page_table, pte_t orig_pte)
2291{
2292 int same = 1;
2293#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
2294 if (sizeof(pte_t) > sizeof(unsigned long)) {
4c21e2f2
HD
2295 spinlock_t *ptl = pte_lockptr(mm, pmd);
2296 spin_lock(ptl);
8f4e2101 2297 same = pte_same(*page_table, orig_pte);
4c21e2f2 2298 spin_unlock(ptl);
8f4e2101
HD
2299 }
2300#endif
2301 pte_unmap(page_table);
2302 return same;
2303}
2304
9de455b2 2305static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
6aab341e 2306{
0abdd7a8
DW
2307 debug_dma_assert_idle(src);
2308
6aab341e
LT
2309 /*
2310 * If the source page was a PFN mapping, we don't have
2311 * a "struct page" for it. We do a best-effort copy by
2312 * just copying from the original user address. If that
2313 * fails, we just zero-fill it. Live with it.
2314 */
2315 if (unlikely(!src)) {
9b04c5fe 2316 void *kaddr = kmap_atomic(dst);
5d2a2dbb
LT
2317 void __user *uaddr = (void __user *)(va & PAGE_MASK);
2318
2319 /*
2320 * This really shouldn't fail, because the page is there
2321 * in the page tables. But it might just be unreadable,
2322 * in which case we just give up and fill the result with
2323 * zeroes.
2324 */
2325 if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
3ecb01df 2326 clear_page(kaddr);
9b04c5fe 2327 kunmap_atomic(kaddr);
c4ec7b0d 2328 flush_dcache_page(dst);
0ed361de
NP
2329 } else
2330 copy_user_highpage(dst, src, va, vma);
6aab341e
LT
2331}
2332
c20cd45e
MH
2333static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma)
2334{
2335 struct file *vm_file = vma->vm_file;
2336
2337 if (vm_file)
2338 return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO;
2339
2340 /*
2341 * Special mappings (e.g. VDSO) do not have any file so fake
2342 * a default GFP_KERNEL for them.
2343 */
2344 return GFP_KERNEL;
2345}
2346
fb09a464
KS
2347/*
2348 * Notify the address space that the page is about to become writable so that
2349 * it can prohibit this or wait for the page to get into an appropriate state.
2350 *
2351 * We do this without the lock held, so that it can sleep if it needs to.
2352 */
38b8cb7f 2353static int do_page_mkwrite(struct vm_fault *vmf)
fb09a464 2354{
fb09a464 2355 int ret;
38b8cb7f
JK
2356 struct page *page = vmf->page;
2357 unsigned int old_flags = vmf->flags;
fb09a464 2358
38b8cb7f 2359 vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
fb09a464 2360
11bac800 2361 ret = vmf->vma->vm_ops->page_mkwrite(vmf);
38b8cb7f
JK
2362 /* Restore original flags so that caller is not surprised */
2363 vmf->flags = old_flags;
fb09a464
KS
2364 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
2365 return ret;
2366 if (unlikely(!(ret & VM_FAULT_LOCKED))) {
2367 lock_page(page);
2368 if (!page->mapping) {
2369 unlock_page(page);
2370 return 0; /* retry */
2371 }
2372 ret |= VM_FAULT_LOCKED;
2373 } else
2374 VM_BUG_ON_PAGE(!PageLocked(page), page);
2375 return ret;
2376}
2377
97ba0c2b
JK
2378/*
2379 * Handle dirtying of a page in shared file mapping on a write fault.
2380 *
2381 * The function expects the page to be locked and unlocks it.
2382 */
2383static void fault_dirty_shared_page(struct vm_area_struct *vma,
2384 struct page *page)
2385{
2386 struct address_space *mapping;
2387 bool dirtied;
2388 bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite;
2389
2390 dirtied = set_page_dirty(page);
2391 VM_BUG_ON_PAGE(PageAnon(page), page);
2392 /*
2393 * Take a local copy of the address_space - page.mapping may be zeroed
2394 * by truncate after unlock_page(). The address_space itself remains
2395 * pinned by vma->vm_file's reference. We rely on unlock_page()'s
2396 * release semantics to prevent the compiler from undoing this copying.
2397 */
2398 mapping = page_rmapping(page);
2399 unlock_page(page);
2400
2401 if ((dirtied || page_mkwrite) && mapping) {
2402 /*
2403 * Some device drivers do not set page.mapping
2404 * but still dirty their pages
2405 */
2406 balance_dirty_pages_ratelimited(mapping);
2407 }
2408
2409 if (!page_mkwrite)
2410 file_update_time(vma->vm_file);
2411}
2412
4e047f89
SR
2413/*
2414 * Handle write page faults for pages that can be reused in the current vma
2415 *
2416 * This can happen either due to the mapping being with the VM_SHARED flag,
2417 * or due to us being the last reference standing to the page. In either
2418 * case, all we need to do here is to mark the page as writable and update
2419 * any related book-keeping.
2420 */
997dd98d 2421static inline void wp_page_reuse(struct vm_fault *vmf)
82b0f8c3 2422 __releases(vmf->ptl)
4e047f89 2423{
82b0f8c3 2424 struct vm_area_struct *vma = vmf->vma;
a41b70d6 2425 struct page *page = vmf->page;
4e047f89
SR
2426 pte_t entry;
2427 /*
2428 * Clear the pages cpupid information as the existing
2429 * information potentially belongs to a now completely
2430 * unrelated process.
2431 */
2432 if (page)
2433 page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1);
2434
2994302b
JK
2435 flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
2436 entry = pte_mkyoung(vmf->orig_pte);
4e047f89 2437 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
82b0f8c3
JK
2438 if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1))
2439 update_mmu_cache(vma, vmf->address, vmf->pte);
2440 pte_unmap_unlock(vmf->pte, vmf->ptl);
4e047f89
SR
2441}
2442
2f38ab2c
SR
2443/*
2444 * Handle the case of a page which we actually need to copy to a new page.
2445 *
2446 * Called with mmap_sem locked and the old page referenced, but
2447 * without the ptl held.
2448 *
2449 * High level logic flow:
2450 *
2451 * - Allocate a page, copy the content of the old page to the new one.
2452 * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc.
2453 * - Take the PTL. If the pte changed, bail out and release the allocated page
2454 * - If the pte is still the way we remember it, update the page table and all
2455 * relevant references. This includes dropping the reference the page-table
2456 * held to the old page, as well as updating the rmap.
2457 * - In any case, unlock the PTL and drop the reference we took to the old page.
2458 */
a41b70d6 2459static int wp_page_copy(struct vm_fault *vmf)
2f38ab2c 2460{
82b0f8c3 2461 struct vm_area_struct *vma = vmf->vma;
bae473a4 2462 struct mm_struct *mm = vma->vm_mm;
a41b70d6 2463 struct page *old_page = vmf->page;
2f38ab2c 2464 struct page *new_page = NULL;
2f38ab2c
SR
2465 pte_t entry;
2466 int page_copied = 0;
82b0f8c3 2467 const unsigned long mmun_start = vmf->address & PAGE_MASK;
bae473a4 2468 const unsigned long mmun_end = mmun_start + PAGE_SIZE;
2f38ab2c
SR
2469 struct mem_cgroup *memcg;
2470
2471 if (unlikely(anon_vma_prepare(vma)))
2472 goto oom;
2473
2994302b 2474 if (is_zero_pfn(pte_pfn(vmf->orig_pte))) {
82b0f8c3
JK
2475 new_page = alloc_zeroed_user_highpage_movable(vma,
2476 vmf->address);
2f38ab2c
SR
2477 if (!new_page)
2478 goto oom;
2479 } else {
bae473a4 2480 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
82b0f8c3 2481 vmf->address);
2f38ab2c
SR
2482 if (!new_page)
2483 goto oom;
82b0f8c3 2484 cow_user_page(new_page, old_page, vmf->address, vma);
2f38ab2c 2485 }
2f38ab2c 2486
f627c2f5 2487 if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg, false))
2f38ab2c
SR
2488 goto oom_free_new;
2489
eb3c24f3
MG
2490 __SetPageUptodate(new_page);
2491
2f38ab2c
SR
2492 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
2493
2494 /*
2495 * Re-check the pte - we dropped the lock
2496 */
82b0f8c3 2497 vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl);
2994302b 2498 if (likely(pte_same(*vmf->pte, vmf->orig_pte))) {
2f38ab2c
SR
2499 if (old_page) {
2500 if (!PageAnon(old_page)) {
eca56ff9
JM
2501 dec_mm_counter_fast(mm,
2502 mm_counter_file(old_page));
2f38ab2c
SR
2503 inc_mm_counter_fast(mm, MM_ANONPAGES);
2504 }
2505 } else {
2506 inc_mm_counter_fast(mm, MM_ANONPAGES);
2507 }
2994302b 2508 flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
2f38ab2c
SR
2509 entry = mk_pte(new_page, vma->vm_page_prot);
2510 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2511 /*
2512 * Clear the pte entry and flush it first, before updating the
2513 * pte with the new entry. This will avoid a race condition
2514 * seen in the presence of one thread doing SMC and another
2515 * thread doing COW.
2516 */
82b0f8c3
JK
2517 ptep_clear_flush_notify(vma, vmf->address, vmf->pte);
2518 page_add_new_anon_rmap(new_page, vma, vmf->address, false);
f627c2f5 2519 mem_cgroup_commit_charge(new_page, memcg, false, false);
2f38ab2c
SR
2520 lru_cache_add_active_or_unevictable(new_page, vma);
2521 /*
2522 * We call the notify macro here because, when using secondary
2523 * mmu page tables (such as kvm shadow page tables), we want the
2524 * new page to be mapped directly into the secondary page table.
2525 */
82b0f8c3
JK
2526 set_pte_at_notify(mm, vmf->address, vmf->pte, entry);
2527 update_mmu_cache(vma, vmf->address, vmf->pte);
2f38ab2c
SR
2528 if (old_page) {
2529 /*
2530 * Only after switching the pte to the new page may
2531 * we remove the mapcount here. Otherwise another
2532 * process may come and find the rmap count decremented
2533 * before the pte is switched to the new page, and
2534 * "reuse" the old page writing into it while our pte
2535 * here still points into it and can be read by other
2536 * threads.
2537 *
2538 * The critical issue is to order this
2539 * page_remove_rmap with the ptp_clear_flush above.
2540 * Those stores are ordered by (if nothing else,)
2541 * the barrier present in the atomic_add_negative
2542 * in page_remove_rmap.
2543 *
2544 * Then the TLB flush in ptep_clear_flush ensures that
2545 * no process can access the old page before the
2546 * decremented mapcount is visible. And the old page
2547 * cannot be reused until after the decremented
2548 * mapcount is visible. So transitively, TLBs to
2549 * old page will be flushed before it can be reused.
2550 */
d281ee61 2551 page_remove_rmap(old_page, false);
2f38ab2c
SR
2552 }
2553
2554 /* Free the old page.. */
2555 new_page = old_page;
2556 page_copied = 1;
2557 } else {
f627c2f5 2558 mem_cgroup_cancel_charge(new_page, memcg, false);
2f38ab2c
SR
2559 }
2560
2561 if (new_page)
09cbfeaf 2562 put_page(new_page);
2f38ab2c 2563
82b0f8c3 2564 pte_unmap_unlock(vmf->pte, vmf->ptl);
4645b9fe
JG
2565 /*
2566 * No need to double call mmu_notifier->invalidate_range() callback as
2567 * the above ptep_clear_flush_notify() did already call it.
2568 */
2569 mmu_notifier_invalidate_range_only_end(mm, mmun_start, mmun_end);
2f38ab2c
SR
2570 if (old_page) {
2571 /*
2572 * Don't let another task, with possibly unlocked vma,
2573 * keep the mlocked page.
2574 */
2575 if (page_copied && (vma->vm_flags & VM_LOCKED)) {
2576 lock_page(old_page); /* LRU manipulation */
e90309c9
KS
2577 if (PageMlocked(old_page))
2578 munlock_vma_page(old_page);
2f38ab2c
SR
2579 unlock_page(old_page);
2580 }
09cbfeaf 2581 put_page(old_page);
2f38ab2c
SR
2582 }
2583 return page_copied ? VM_FAULT_WRITE : 0;
2584oom_free_new:
09cbfeaf 2585 put_page(new_page);
2f38ab2c
SR
2586oom:
2587 if (old_page)
09cbfeaf 2588 put_page(old_page);
2f38ab2c
SR
2589 return VM_FAULT_OOM;
2590}
2591
66a6197c
JK
2592/**
2593 * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE
2594 * writeable once the page is prepared
2595 *
2596 * @vmf: structure describing the fault
2597 *
2598 * This function handles all that is needed to finish a write page fault in a
2599 * shared mapping due to PTE being read-only once the mapped page is prepared.
2600 * It handles locking of PTE and modifying it. The function returns
2601 * VM_FAULT_WRITE on success, 0 when PTE got changed before we acquired PTE
2602 * lock.
2603 *
2604 * The function expects the page to be locked or other protection against
2605 * concurrent faults / writeback (such as DAX radix tree locks).
2606 */
2607int finish_mkwrite_fault(struct vm_fault *vmf)
2608{
2609 WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED));
2610 vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address,
2611 &vmf->ptl);
2612 /*
2613 * We might have raced with another page fault while we released the
2614 * pte_offset_map_lock.
2615 */
2616 if (!pte_same(*vmf->pte, vmf->orig_pte)) {
2617 pte_unmap_unlock(vmf->pte, vmf->ptl);
a19e2553 2618 return VM_FAULT_NOPAGE;
66a6197c
JK
2619 }
2620 wp_page_reuse(vmf);
a19e2553 2621 return 0;
66a6197c
JK
2622}
2623
dd906184
BH
2624/*
2625 * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED
2626 * mapping
2627 */
2994302b 2628static int wp_pfn_shared(struct vm_fault *vmf)
dd906184 2629{
82b0f8c3 2630 struct vm_area_struct *vma = vmf->vma;
bae473a4 2631
dd906184 2632 if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) {
dd906184
BH
2633 int ret;
2634
82b0f8c3 2635 pte_unmap_unlock(vmf->pte, vmf->ptl);
fe82221f 2636 vmf->flags |= FAULT_FLAG_MKWRITE;
11bac800 2637 ret = vma->vm_ops->pfn_mkwrite(vmf);
2f89dc12 2638 if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))
dd906184 2639 return ret;
66a6197c 2640 return finish_mkwrite_fault(vmf);
dd906184 2641 }
997dd98d
JK
2642 wp_page_reuse(vmf);
2643 return VM_FAULT_WRITE;
dd906184
BH
2644}
2645
a41b70d6 2646static int wp_page_shared(struct vm_fault *vmf)
82b0f8c3 2647 __releases(vmf->ptl)
93e478d4 2648{
82b0f8c3 2649 struct vm_area_struct *vma = vmf->vma;
93e478d4 2650
a41b70d6 2651 get_page(vmf->page);
93e478d4 2652
93e478d4
SR
2653 if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
2654 int tmp;
2655
82b0f8c3 2656 pte_unmap_unlock(vmf->pte, vmf->ptl);
38b8cb7f 2657 tmp = do_page_mkwrite(vmf);
93e478d4
SR
2658 if (unlikely(!tmp || (tmp &
2659 (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
a41b70d6 2660 put_page(vmf->page);
93e478d4
SR
2661 return tmp;
2662 }
66a6197c 2663 tmp = finish_mkwrite_fault(vmf);
a19e2553 2664 if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
a41b70d6 2665 unlock_page(vmf->page);
a41b70d6 2666 put_page(vmf->page);
66a6197c 2667 return tmp;
93e478d4 2668 }
66a6197c
JK
2669 } else {
2670 wp_page_reuse(vmf);
997dd98d 2671 lock_page(vmf->page);
93e478d4 2672 }
997dd98d
JK
2673 fault_dirty_shared_page(vma, vmf->page);
2674 put_page(vmf->page);
93e478d4 2675
997dd98d 2676 return VM_FAULT_WRITE;
93e478d4
SR
2677}
2678
1da177e4
LT
2679/*
2680 * This routine handles present pages, when users try to write
2681 * to a shared page. It is done by copying the page to a new address
2682 * and decrementing the shared-page counter for the old page.
2683 *
1da177e4
LT
2684 * Note that this routine assumes that the protection checks have been
2685 * done by the caller (the low-level page fault routine in most cases).
2686 * Thus we can safely just mark it writable once we've done any necessary
2687 * COW.
2688 *
2689 * We also mark the page dirty at this point even though the page will
2690 * change only once the write actually happens. This avoids a few races,
2691 * and potentially makes it more efficient.
2692 *
8f4e2101
HD
2693 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2694 * but allow concurrent faults), with pte both mapped and locked.
2695 * We return with mmap_sem still held, but pte unmapped and unlocked.
1da177e4 2696 */
2994302b 2697static int do_wp_page(struct vm_fault *vmf)
82b0f8c3 2698 __releases(vmf->ptl)
1da177e4 2699{
82b0f8c3 2700 struct vm_area_struct *vma = vmf->vma;
1da177e4 2701
a41b70d6
JK
2702 vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte);
2703 if (!vmf->page) {
251b97f5 2704 /*
64e45507
PF
2705 * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
2706 * VM_PFNMAP VMA.
251b97f5
PZ
2707 *
2708 * We should not cow pages in a shared writeable mapping.
dd906184 2709 * Just mark the pages writable and/or call ops->pfn_mkwrite.
251b97f5
PZ
2710 */
2711 if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2712 (VM_WRITE|VM_SHARED))
2994302b 2713 return wp_pfn_shared(vmf);
2f38ab2c 2714
82b0f8c3 2715 pte_unmap_unlock(vmf->pte, vmf->ptl);
a41b70d6 2716 return wp_page_copy(vmf);
251b97f5 2717 }
1da177e4 2718
d08b3851 2719 /*
ee6a6457
PZ
2720 * Take out anonymous pages first, anonymous shared vmas are
2721 * not dirty accountable.
d08b3851 2722 */
a41b70d6 2723 if (PageAnon(vmf->page) && !PageKsm(vmf->page)) {
ba3c4ce6 2724 int total_map_swapcount;
a41b70d6
JK
2725 if (!trylock_page(vmf->page)) {
2726 get_page(vmf->page);
82b0f8c3 2727 pte_unmap_unlock(vmf->pte, vmf->ptl);
a41b70d6 2728 lock_page(vmf->page);
82b0f8c3
JK
2729 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
2730 vmf->address, &vmf->ptl);
2994302b 2731 if (!pte_same(*vmf->pte, vmf->orig_pte)) {
a41b70d6 2732 unlock_page(vmf->page);
82b0f8c3 2733 pte_unmap_unlock(vmf->pte, vmf->ptl);
a41b70d6 2734 put_page(vmf->page);
28766805 2735 return 0;
ab967d86 2736 }
a41b70d6 2737 put_page(vmf->page);
ee6a6457 2738 }
ba3c4ce6
HY
2739 if (reuse_swap_page(vmf->page, &total_map_swapcount)) {
2740 if (total_map_swapcount == 1) {
6d0a07ed
AA
2741 /*
2742 * The page is all ours. Move it to
2743 * our anon_vma so the rmap code will
2744 * not search our parent or siblings.
2745 * Protected against the rmap code by
2746 * the page lock.
2747 */
a41b70d6 2748 page_move_anon_rmap(vmf->page, vma);
6d0a07ed 2749 }
a41b70d6 2750 unlock_page(vmf->page);
997dd98d
JK
2751 wp_page_reuse(vmf);
2752 return VM_FAULT_WRITE;
b009c024 2753 }
a41b70d6 2754 unlock_page(vmf->page);
ee6a6457 2755 } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
d08b3851 2756 (VM_WRITE|VM_SHARED))) {
a41b70d6 2757 return wp_page_shared(vmf);
1da177e4 2758 }
1da177e4
LT
2759
2760 /*
2761 * Ok, we need to copy. Oh, well..
2762 */
a41b70d6 2763 get_page(vmf->page);
28766805 2764
82b0f8c3 2765 pte_unmap_unlock(vmf->pte, vmf->ptl);
a41b70d6 2766 return wp_page_copy(vmf);
1da177e4
LT
2767}
2768
97a89413 2769static void unmap_mapping_range_vma(struct vm_area_struct *vma,
1da177e4
LT
2770 unsigned long start_addr, unsigned long end_addr,
2771 struct zap_details *details)
2772{
f5cc4eef 2773 zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
1da177e4
LT
2774}
2775
f808c13f 2776static inline void unmap_mapping_range_tree(struct rb_root_cached *root,
1da177e4
LT
2777 struct zap_details *details)
2778{
2779 struct vm_area_struct *vma;
1da177e4
LT
2780 pgoff_t vba, vea, zba, zea;
2781
6b2dbba8 2782 vma_interval_tree_foreach(vma, root,
1da177e4 2783 details->first_index, details->last_index) {
1da177e4
LT
2784
2785 vba = vma->vm_pgoff;
d6e93217 2786 vea = vba + vma_pages(vma) - 1;
1da177e4
LT
2787 zba = details->first_index;
2788 if (zba < vba)
2789 zba = vba;
2790 zea = details->last_index;
2791 if (zea > vea)
2792 zea = vea;
2793
97a89413 2794 unmap_mapping_range_vma(vma,
1da177e4
LT
2795 ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
2796 ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
97a89413 2797 details);
1da177e4
LT
2798 }
2799}
2800
977fbdcd
MW
2801/**
2802 * unmap_mapping_pages() - Unmap pages from processes.
2803 * @mapping: The address space containing pages to be unmapped.
2804 * @start: Index of first page to be unmapped.
2805 * @nr: Number of pages to be unmapped. 0 to unmap to end of file.
2806 * @even_cows: Whether to unmap even private COWed pages.
2807 *
2808 * Unmap the pages in this address space from any userspace process which
2809 * has them mmaped. Generally, you want to remove COWed pages as well when
2810 * a file is being truncated, but not when invalidating pages from the page
2811 * cache.
2812 */
2813void unmap_mapping_pages(struct address_space *mapping, pgoff_t start,
2814 pgoff_t nr, bool even_cows)
2815{
2816 struct zap_details details = { };
2817
2818 details.check_mapping = even_cows ? NULL : mapping;
2819 details.first_index = start;
2820 details.last_index = start + nr - 1;
2821 if (details.last_index < details.first_index)
2822 details.last_index = ULONG_MAX;
2823
2824 i_mmap_lock_write(mapping);
2825 if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)))
2826 unmap_mapping_range_tree(&mapping->i_mmap, &details);
2827 i_mmap_unlock_write(mapping);
2828}
2829
1da177e4 2830/**
8a5f14a2 2831 * unmap_mapping_range - unmap the portion of all mmaps in the specified
977fbdcd 2832 * address_space corresponding to the specified byte range in the underlying
8a5f14a2
KS
2833 * file.
2834 *
3d41088f 2835 * @mapping: the address space containing mmaps to be unmapped.
1da177e4
LT
2836 * @holebegin: byte in first page to unmap, relative to the start of
2837 * the underlying file. This will be rounded down to a PAGE_SIZE
25d9e2d1 2838 * boundary. Note that this is different from truncate_pagecache(), which
1da177e4
LT
2839 * must keep the partial page. In contrast, we must get rid of
2840 * partial pages.
2841 * @holelen: size of prospective hole in bytes. This will be rounded
2842 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
2843 * end of the file.
2844 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
2845 * but 0 when invalidating pagecache, don't throw away private data.
2846 */
2847void unmap_mapping_range(struct address_space *mapping,
2848 loff_t const holebegin, loff_t const holelen, int even_cows)
2849{
1da177e4
LT
2850 pgoff_t hba = holebegin >> PAGE_SHIFT;
2851 pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
2852
2853 /* Check for overflow. */
2854 if (sizeof(holelen) > sizeof(hlen)) {
2855 long long holeend =
2856 (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
2857 if (holeend & ~(long long)ULONG_MAX)
2858 hlen = ULONG_MAX - hba + 1;
2859 }
2860
977fbdcd 2861 unmap_mapping_pages(mapping, hba, hlen, even_cows);
1da177e4
LT
2862}
2863EXPORT_SYMBOL(unmap_mapping_range);
2864
1da177e4 2865/*
8f4e2101
HD
2866 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2867 * but allow concurrent faults), and pte mapped but not yet locked.
9a95f3cf
PC
2868 * We return with pte unmapped and unlocked.
2869 *
2870 * We return with the mmap_sem locked or unlocked in the same cases
2871 * as does filemap_fault().
1da177e4 2872 */
2994302b 2873int do_swap_page(struct vm_fault *vmf)
1da177e4 2874{
82b0f8c3 2875 struct vm_area_struct *vma = vmf->vma;
0bcac06f 2876 struct page *page = NULL, *swapcache = NULL;
00501b53 2877 struct mem_cgroup *memcg;
ec560175 2878 struct vma_swap_readahead swap_ra;
65500d23 2879 swp_entry_t entry;
1da177e4 2880 pte_t pte;
d065bd81 2881 int locked;
ad8c2ee8 2882 int exclusive = 0;
83c54070 2883 int ret = 0;
ec560175 2884 bool vma_readahead = swap_use_vma_readahead();
1da177e4 2885
f8020772 2886 if (vma_readahead) {
ec560175 2887 page = swap_readahead_detect(vmf, &swap_ra);
f8020772
MK
2888 swapcache = page;
2889 }
2890
ec560175
HY
2891 if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte)) {
2892 if (page)
2893 put_page(page);
8f4e2101 2894 goto out;
ec560175 2895 }
65500d23 2896
2994302b 2897 entry = pte_to_swp_entry(vmf->orig_pte);
d1737fdb
AK
2898 if (unlikely(non_swap_entry(entry))) {
2899 if (is_migration_entry(entry)) {
82b0f8c3
JK
2900 migration_entry_wait(vma->vm_mm, vmf->pmd,
2901 vmf->address);
5042db43
JG
2902 } else if (is_device_private_entry(entry)) {
2903 /*
2904 * For un-addressable device memory we call the pgmap
2905 * fault handler callback. The callback must migrate
2906 * the page back to some CPU accessible page.
2907 */
2908 ret = device_private_entry_fault(vma, vmf->address, entry,
2909 vmf->flags, vmf->pmd);
d1737fdb
AK
2910 } else if (is_hwpoison_entry(entry)) {
2911 ret = VM_FAULT_HWPOISON;
2912 } else {
2994302b 2913 print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL);
d99be1a8 2914 ret = VM_FAULT_SIGBUS;
d1737fdb 2915 }
0697212a
CL
2916 goto out;
2917 }
0bcac06f
MK
2918
2919
0ff92245 2920 delayacct_set_flag(DELAYACCT_PF_SWAPIN);
f8020772 2921 if (!page) {
ec560175
HY
2922 page = lookup_swap_cache(entry, vma_readahead ? vma : NULL,
2923 vmf->address);
f8020772
MK
2924 swapcache = page;
2925 }
2926
1da177e4 2927 if (!page) {
0bcac06f
MK
2928 struct swap_info_struct *si = swp_swap_info(entry);
2929
aa8d22a1
MK
2930 if (si->flags & SWP_SYNCHRONOUS_IO &&
2931 __swap_count(si, entry) == 1) {
0bcac06f
MK
2932 /* skip swapcache */
2933 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address);
2934 if (page) {
2935 __SetPageLocked(page);
2936 __SetPageSwapBacked(page);
2937 set_page_private(page, entry.val);
2938 lru_cache_add_anon(page);
2939 swap_readpage(page, true);
2940 }
aa8d22a1
MK
2941 } else {
2942 if (vma_readahead)
2943 page = do_swap_page_readahead(entry,
2944 GFP_HIGHUSER_MOVABLE, vmf, &swap_ra);
2945 else
2946 page = swapin_readahead(entry,
2947 GFP_HIGHUSER_MOVABLE, vma, vmf->address);
2948 swapcache = page;
0bcac06f
MK
2949 }
2950
1da177e4
LT
2951 if (!page) {
2952 /*
8f4e2101
HD
2953 * Back out if somebody else faulted in this pte
2954 * while we released the pte lock.
1da177e4 2955 */
82b0f8c3
JK
2956 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
2957 vmf->address, &vmf->ptl);
2994302b 2958 if (likely(pte_same(*vmf->pte, vmf->orig_pte)))
1da177e4 2959 ret = VM_FAULT_OOM;
0ff92245 2960 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
65500d23 2961 goto unlock;
1da177e4
LT
2962 }
2963
2964 /* Had to read the page from swap area: Major fault */
2965 ret = VM_FAULT_MAJOR;
f8891e5e 2966 count_vm_event(PGMAJFAULT);
2262185c 2967 count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
d1737fdb 2968 } else if (PageHWPoison(page)) {
71f72525
WF
2969 /*
2970 * hwpoisoned dirty swapcache pages are kept for killing
2971 * owner processes (which may be unknown at hwpoison time)
2972 */
d1737fdb
AK
2973 ret = VM_FAULT_HWPOISON;
2974 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
56f31801 2975 swapcache = page;
4779cb31 2976 goto out_release;
1da177e4
LT
2977 }
2978
82b0f8c3 2979 locked = lock_page_or_retry(page, vma->vm_mm, vmf->flags);
e709ffd6 2980
073e587e 2981 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
d065bd81
ML
2982 if (!locked) {
2983 ret |= VM_FAULT_RETRY;
2984 goto out_release;
2985 }
073e587e 2986
4969c119 2987 /*
31c4a3d3
HD
2988 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
2989 * release the swapcache from under us. The page pin, and pte_same
2990 * test below, are not enough to exclude that. Even if it is still
2991 * swapcache, we need to check that the page's swap has not changed.
4969c119 2992 */
0bcac06f
MK
2993 if (unlikely((!PageSwapCache(page) ||
2994 page_private(page) != entry.val)) && swapcache)
4969c119
AA
2995 goto out_page;
2996
82b0f8c3 2997 page = ksm_might_need_to_copy(page, vma, vmf->address);
cbf86cfe
HD
2998 if (unlikely(!page)) {
2999 ret = VM_FAULT_OOM;
3000 page = swapcache;
cbf86cfe 3001 goto out_page;
5ad64688
HD
3002 }
3003
bae473a4
KS
3004 if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL,
3005 &memcg, false)) {
8a9f3ccd 3006 ret = VM_FAULT_OOM;
bc43f75c 3007 goto out_page;
8a9f3ccd
BS
3008 }
3009
1da177e4 3010 /*
8f4e2101 3011 * Back out if somebody else already faulted in this pte.
1da177e4 3012 */
82b0f8c3
JK
3013 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3014 &vmf->ptl);
2994302b 3015 if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte)))
b8107480 3016 goto out_nomap;
b8107480
KK
3017
3018 if (unlikely(!PageUptodate(page))) {
3019 ret = VM_FAULT_SIGBUS;
3020 goto out_nomap;
1da177e4
LT
3021 }
3022
8c7c6e34
KH
3023 /*
3024 * The page isn't present yet, go ahead with the fault.
3025 *
3026 * Be careful about the sequence of operations here.
3027 * To get its accounting right, reuse_swap_page() must be called
3028 * while the page is counted on swap but not yet in mapcount i.e.
3029 * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
3030 * must be called after the swap_free(), or it will never succeed.
8c7c6e34 3031 */
1da177e4 3032
bae473a4
KS
3033 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
3034 dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS);
1da177e4 3035 pte = mk_pte(page, vma->vm_page_prot);
82b0f8c3 3036 if ((vmf->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) {
1da177e4 3037 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
82b0f8c3 3038 vmf->flags &= ~FAULT_FLAG_WRITE;
9a5b489b 3039 ret |= VM_FAULT_WRITE;
d281ee61 3040 exclusive = RMAP_EXCLUSIVE;
1da177e4 3041 }
1da177e4 3042 flush_icache_page(vma, page);
2994302b 3043 if (pte_swp_soft_dirty(vmf->orig_pte))
179ef71c 3044 pte = pte_mksoft_dirty(pte);
82b0f8c3 3045 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte);
2994302b 3046 vmf->orig_pte = pte;
0bcac06f
MK
3047
3048 /* ksm created a completely new copy */
3049 if (unlikely(page != swapcache && swapcache)) {
82b0f8c3 3050 page_add_new_anon_rmap(page, vma, vmf->address, false);
f627c2f5 3051 mem_cgroup_commit_charge(page, memcg, false, false);
00501b53 3052 lru_cache_add_active_or_unevictable(page, vma);
0bcac06f
MK
3053 } else {
3054 do_page_add_anon_rmap(page, vma, vmf->address, exclusive);
3055 mem_cgroup_commit_charge(page, memcg, true, false);
3056 activate_page(page);
00501b53 3057 }
1da177e4 3058
c475a8ab 3059 swap_free(entry);
5ccc5aba
VD
3060 if (mem_cgroup_swap_full(page) ||
3061 (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
a2c43eed 3062 try_to_free_swap(page);
c475a8ab 3063 unlock_page(page);
0bcac06f 3064 if (page != swapcache && swapcache) {
4969c119
AA
3065 /*
3066 * Hold the lock to avoid the swap entry to be reused
3067 * until we take the PT lock for the pte_same() check
3068 * (to avoid false positives from pte_same). For
3069 * further safety release the lock after the swap_free
3070 * so that the swap count won't change under a
3071 * parallel locked swapcache.
3072 */
3073 unlock_page(swapcache);
09cbfeaf 3074 put_page(swapcache);
4969c119 3075 }
c475a8ab 3076
82b0f8c3 3077 if (vmf->flags & FAULT_FLAG_WRITE) {
2994302b 3078 ret |= do_wp_page(vmf);
61469f1d
HD
3079 if (ret & VM_FAULT_ERROR)
3080 ret &= VM_FAULT_ERROR;
1da177e4
LT
3081 goto out;
3082 }
3083
3084 /* No need to invalidate - it was non-present before */
82b0f8c3 3085 update_mmu_cache(vma, vmf->address, vmf->pte);
65500d23 3086unlock:
82b0f8c3 3087 pte_unmap_unlock(vmf->pte, vmf->ptl);
1da177e4
LT
3088out:
3089 return ret;
b8107480 3090out_nomap:
f627c2f5 3091 mem_cgroup_cancel_charge(page, memcg, false);
82b0f8c3 3092 pte_unmap_unlock(vmf->pte, vmf->ptl);
bc43f75c 3093out_page:
b8107480 3094 unlock_page(page);
4779cb31 3095out_release:
09cbfeaf 3096 put_page(page);
0bcac06f 3097 if (page != swapcache && swapcache) {
4969c119 3098 unlock_page(swapcache);
09cbfeaf 3099 put_page(swapcache);
4969c119 3100 }
65500d23 3101 return ret;
1da177e4
LT
3102}
3103
3104/*
8f4e2101
HD
3105 * We enter with non-exclusive mmap_sem (to exclude vma changes,
3106 * but allow concurrent faults), and pte mapped but not yet locked.
3107 * We return with mmap_sem still held, but pte unmapped and unlocked.
1da177e4 3108 */
82b0f8c3 3109static int do_anonymous_page(struct vm_fault *vmf)
1da177e4 3110{
82b0f8c3 3111 struct vm_area_struct *vma = vmf->vma;
00501b53 3112 struct mem_cgroup *memcg;
8f4e2101 3113 struct page *page;
6b31d595 3114 int ret = 0;
1da177e4 3115 pte_t entry;
1da177e4 3116
6b7339f4
KS
3117 /* File mapping without ->vm_ops ? */
3118 if (vma->vm_flags & VM_SHARED)
3119 return VM_FAULT_SIGBUS;
3120
7267ec00
KS
3121 /*
3122 * Use pte_alloc() instead of pte_alloc_map(). We can't run
3123 * pte_offset_map() on pmds where a huge pmd might be created
3124 * from a different thread.
3125 *
3126 * pte_alloc_map() is safe to use under down_write(mmap_sem) or when
3127 * parallel threads are excluded by other means.
3128 *
3129 * Here we only have down_read(mmap_sem).
3130 */
82b0f8c3 3131 if (pte_alloc(vma->vm_mm, vmf->pmd, vmf->address))
7267ec00
KS
3132 return VM_FAULT_OOM;
3133
3134 /* See the comment in pte_alloc_one_map() */
82b0f8c3 3135 if (unlikely(pmd_trans_unstable(vmf->pmd)))
7267ec00
KS
3136 return 0;
3137
11ac5524 3138 /* Use the zero-page for reads */
82b0f8c3 3139 if (!(vmf->flags & FAULT_FLAG_WRITE) &&
bae473a4 3140 !mm_forbids_zeropage(vma->vm_mm)) {
82b0f8c3 3141 entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address),
62eede62 3142 vma->vm_page_prot));
82b0f8c3
JK
3143 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
3144 vmf->address, &vmf->ptl);
3145 if (!pte_none(*vmf->pte))
a13ea5b7 3146 goto unlock;
6b31d595
MH
3147 ret = check_stable_address_space(vma->vm_mm);
3148 if (ret)
3149 goto unlock;
6b251fc9
AA
3150 /* Deliver the page fault to userland, check inside PT lock */
3151 if (userfaultfd_missing(vma)) {
82b0f8c3
JK
3152 pte_unmap_unlock(vmf->pte, vmf->ptl);
3153 return handle_userfault(vmf, VM_UFFD_MISSING);
6b251fc9 3154 }
a13ea5b7
HD
3155 goto setpte;
3156 }
3157
557ed1fa 3158 /* Allocate our own private page. */
557ed1fa
NP
3159 if (unlikely(anon_vma_prepare(vma)))
3160 goto oom;
82b0f8c3 3161 page = alloc_zeroed_user_highpage_movable(vma, vmf->address);
557ed1fa
NP
3162 if (!page)
3163 goto oom;
eb3c24f3 3164
bae473a4 3165 if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, &memcg, false))
eb3c24f3
MG
3166 goto oom_free_page;
3167
52f37629
MK
3168 /*
3169 * The memory barrier inside __SetPageUptodate makes sure that
3170 * preceeding stores to the page contents become visible before
3171 * the set_pte_at() write.
3172 */
0ed361de 3173 __SetPageUptodate(page);
8f4e2101 3174
557ed1fa 3175 entry = mk_pte(page, vma->vm_page_prot);
1ac0cb5d
HD
3176 if (vma->vm_flags & VM_WRITE)
3177 entry = pte_mkwrite(pte_mkdirty(entry));
1da177e4 3178
82b0f8c3
JK
3179 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3180 &vmf->ptl);
3181 if (!pte_none(*vmf->pte))
557ed1fa 3182 goto release;
9ba69294 3183
6b31d595
MH
3184 ret = check_stable_address_space(vma->vm_mm);
3185 if (ret)
3186 goto release;
3187
6b251fc9
AA
3188 /* Deliver the page fault to userland, check inside PT lock */
3189 if (userfaultfd_missing(vma)) {
82b0f8c3 3190 pte_unmap_unlock(vmf->pte, vmf->ptl);
f627c2f5 3191 mem_cgroup_cancel_charge(page, memcg, false);
09cbfeaf 3192 put_page(page);
82b0f8c3 3193 return handle_userfault(vmf, VM_UFFD_MISSING);
6b251fc9
AA
3194 }
3195
bae473a4 3196 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
82b0f8c3 3197 page_add_new_anon_rmap(page, vma, vmf->address, false);
f627c2f5 3198 mem_cgroup_commit_charge(page, memcg, false, false);
00501b53 3199 lru_cache_add_active_or_unevictable(page, vma);
a13ea5b7 3200setpte:
82b0f8c3 3201 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);
1da177e4
LT
3202
3203 /* No need to invalidate - it was non-present before */
82b0f8c3 3204 update_mmu_cache(vma, vmf->address, vmf->pte);
65500d23 3205unlock:
82b0f8c3 3206 pte_unmap_unlock(vmf->pte, vmf->ptl);
6b31d595 3207 return ret;
8f4e2101 3208release:
f627c2f5 3209 mem_cgroup_cancel_charge(page, memcg, false);
09cbfeaf 3210 put_page(page);
8f4e2101 3211 goto unlock;
8a9f3ccd 3212oom_free_page:
09cbfeaf 3213 put_page(page);
65500d23 3214oom:
1da177e4
LT
3215 return VM_FAULT_OOM;
3216}
3217
9a95f3cf
PC
3218/*
3219 * The mmap_sem must have been held on entry, and may have been
3220 * released depending on flags and vma->vm_ops->fault() return value.
3221 * See filemap_fault() and __lock_page_retry().
3222 */
936ca80d 3223static int __do_fault(struct vm_fault *vmf)
7eae74af 3224{
82b0f8c3 3225 struct vm_area_struct *vma = vmf->vma;
7eae74af
KS
3226 int ret;
3227
11bac800 3228 ret = vma->vm_ops->fault(vmf);
3917048d 3229 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY |
b1aa812b 3230 VM_FAULT_DONE_COW)))
bc2466e4 3231 return ret;
7eae74af 3232
667240e0 3233 if (unlikely(PageHWPoison(vmf->page))) {
7eae74af 3234 if (ret & VM_FAULT_LOCKED)
667240e0
JK
3235 unlock_page(vmf->page);
3236 put_page(vmf->page);
936ca80d 3237 vmf->page = NULL;
7eae74af
KS
3238 return VM_FAULT_HWPOISON;
3239 }
3240
3241 if (unlikely(!(ret & VM_FAULT_LOCKED)))
667240e0 3242 lock_page(vmf->page);
7eae74af 3243 else
667240e0 3244 VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page);
7eae74af 3245
7eae74af
KS
3246 return ret;
3247}
3248
d0f0931d
RZ
3249/*
3250 * The ordering of these checks is important for pmds with _PAGE_DEVMAP set.
3251 * If we check pmd_trans_unstable() first we will trip the bad_pmd() check
3252 * inside of pmd_none_or_trans_huge_or_clear_bad(). This will end up correctly
3253 * returning 1 but not before it spams dmesg with the pmd_clear_bad() output.
3254 */
3255static int pmd_devmap_trans_unstable(pmd_t *pmd)
3256{
3257 return pmd_devmap(*pmd) || pmd_trans_unstable(pmd);
3258}
3259
82b0f8c3 3260static int pte_alloc_one_map(struct vm_fault *vmf)
7267ec00 3261{
82b0f8c3 3262 struct vm_area_struct *vma = vmf->vma;
7267ec00 3263
82b0f8c3 3264 if (!pmd_none(*vmf->pmd))
7267ec00 3265 goto map_pte;
82b0f8c3
JK
3266 if (vmf->prealloc_pte) {
3267 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
3268 if (unlikely(!pmd_none(*vmf->pmd))) {
3269 spin_unlock(vmf->ptl);
7267ec00
KS
3270 goto map_pte;
3271 }
3272
c4812909 3273 mm_inc_nr_ptes(vma->vm_mm);
82b0f8c3
JK
3274 pmd_populate(vma->vm_mm, vmf->pmd, vmf->prealloc_pte);
3275 spin_unlock(vmf->ptl);
7f2b6ce8 3276 vmf->prealloc_pte = NULL;
82b0f8c3 3277 } else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd, vmf->address))) {
7267ec00
KS
3278 return VM_FAULT_OOM;
3279 }
3280map_pte:
3281 /*
3282 * If a huge pmd materialized under us just retry later. Use
d0f0931d
RZ
3283 * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead of
3284 * pmd_trans_huge() to ensure the pmd didn't become pmd_trans_huge
3285 * under us and then back to pmd_none, as a result of MADV_DONTNEED
3286 * running immediately after a huge pmd fault in a different thread of
3287 * this mm, in turn leading to a misleading pmd_trans_huge() retval.
3288 * All we have to ensure is that it is a regular pmd that we can walk
3289 * with pte_offset_map() and we can do that through an atomic read in
3290 * C, which is what pmd_trans_unstable() provides.
7267ec00 3291 */
d0f0931d 3292 if (pmd_devmap_trans_unstable(vmf->pmd))
7267ec00
KS
3293 return VM_FAULT_NOPAGE;
3294
d0f0931d
RZ
3295 /*
3296 * At this point we know that our vmf->pmd points to a page of ptes
3297 * and it cannot become pmd_none(), pmd_devmap() or pmd_trans_huge()
3298 * for the duration of the fault. If a racing MADV_DONTNEED runs and
3299 * we zap the ptes pointed to by our vmf->pmd, the vmf->ptl will still
3300 * be valid and we will re-check to make sure the vmf->pte isn't
3301 * pte_none() under vmf->ptl protection when we return to
3302 * alloc_set_pte().
3303 */
82b0f8c3
JK
3304 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3305 &vmf->ptl);
7267ec00
KS
3306 return 0;
3307}
3308
e496cf3d 3309#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
10102459
KS
3310
3311#define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1)
3312static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
3313 unsigned long haddr)
3314{
3315 if (((vma->vm_start >> PAGE_SHIFT) & HPAGE_CACHE_INDEX_MASK) !=
3316 (vma->vm_pgoff & HPAGE_CACHE_INDEX_MASK))
3317 return false;
3318 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
3319 return false;
3320 return true;
3321}
3322
82b0f8c3 3323static void deposit_prealloc_pte(struct vm_fault *vmf)
953c66c2 3324{
82b0f8c3 3325 struct vm_area_struct *vma = vmf->vma;
953c66c2 3326
82b0f8c3 3327 pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte);
953c66c2
AK
3328 /*
3329 * We are going to consume the prealloc table,
3330 * count that as nr_ptes.
3331 */
c4812909 3332 mm_inc_nr_ptes(vma->vm_mm);
7f2b6ce8 3333 vmf->prealloc_pte = NULL;
953c66c2
AK
3334}
3335
82b0f8c3 3336static int do_set_pmd(struct vm_fault *vmf, struct page *page)
10102459 3337{
82b0f8c3
JK
3338 struct vm_area_struct *vma = vmf->vma;
3339 bool write = vmf->flags & FAULT_FLAG_WRITE;
3340 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
10102459
KS
3341 pmd_t entry;
3342 int i, ret;
3343
3344 if (!transhuge_vma_suitable(vma, haddr))
3345 return VM_FAULT_FALLBACK;
3346
3347 ret = VM_FAULT_FALLBACK;
3348 page = compound_head(page);
3349
953c66c2
AK
3350 /*
3351 * Archs like ppc64 need additonal space to store information
3352 * related to pte entry. Use the preallocated table for that.
3353 */
82b0f8c3
JK
3354 if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) {
3355 vmf->prealloc_pte = pte_alloc_one(vma->vm_mm, vmf->address);
3356 if (!vmf->prealloc_pte)
953c66c2
AK
3357 return VM_FAULT_OOM;
3358 smp_wmb(); /* See comment in __pte_alloc() */
3359 }
3360
82b0f8c3
JK
3361 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
3362 if (unlikely(!pmd_none(*vmf->pmd)))
10102459
KS
3363 goto out;
3364
3365 for (i = 0; i < HPAGE_PMD_NR; i++)
3366 flush_icache_page(vma, page + i);
3367
3368 entry = mk_huge_pmd(page, vma->vm_page_prot);
3369 if (write)
f55e1014 3370 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
10102459
KS
3371
3372 add_mm_counter(vma->vm_mm, MM_FILEPAGES, HPAGE_PMD_NR);
3373 page_add_file_rmap(page, true);
953c66c2
AK
3374 /*
3375 * deposit and withdraw with pmd lock held
3376 */
3377 if (arch_needs_pgtable_deposit())
82b0f8c3 3378 deposit_prealloc_pte(vmf);
10102459 3379
82b0f8c3 3380 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry);
10102459 3381
82b0f8c3 3382 update_mmu_cache_pmd(vma, haddr, vmf->pmd);
10102459
KS
3383
3384 /* fault is handled */
3385 ret = 0;
95ecedcd 3386 count_vm_event(THP_FILE_MAPPED);
10102459 3387out:
82b0f8c3 3388 spin_unlock(vmf->ptl);
10102459
KS
3389 return ret;
3390}
3391#else
82b0f8c3 3392static int do_set_pmd(struct vm_fault *vmf, struct page *page)
10102459
KS
3393{
3394 BUILD_BUG();
3395 return 0;
3396}
3397#endif
3398
8c6e50b0 3399/**
7267ec00
KS
3400 * alloc_set_pte - setup new PTE entry for given page and add reverse page
3401 * mapping. If needed, the fucntion allocates page table or use pre-allocated.
8c6e50b0 3402 *
82b0f8c3 3403 * @vmf: fault environment
7267ec00 3404 * @memcg: memcg to charge page (only for private mappings)
8c6e50b0 3405 * @page: page to map
8c6e50b0 3406 *
82b0f8c3
JK
3407 * Caller must take care of unlocking vmf->ptl, if vmf->pte is non-NULL on
3408 * return.
8c6e50b0
KS
3409 *
3410 * Target users are page handler itself and implementations of
3411 * vm_ops->map_pages.
3412 */
82b0f8c3 3413int alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg,
7267ec00 3414 struct page *page)
3bb97794 3415{
82b0f8c3
JK
3416 struct vm_area_struct *vma = vmf->vma;
3417 bool write = vmf->flags & FAULT_FLAG_WRITE;
3bb97794 3418 pte_t entry;
10102459
KS
3419 int ret;
3420
82b0f8c3 3421 if (pmd_none(*vmf->pmd) && PageTransCompound(page) &&
e496cf3d 3422 IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
10102459
KS
3423 /* THP on COW? */
3424 VM_BUG_ON_PAGE(memcg, page);
3425
82b0f8c3 3426 ret = do_set_pmd(vmf, page);
10102459 3427 if (ret != VM_FAULT_FALLBACK)
b0b9b3df 3428 return ret;
10102459 3429 }
3bb97794 3430
82b0f8c3
JK
3431 if (!vmf->pte) {
3432 ret = pte_alloc_one_map(vmf);
7267ec00 3433 if (ret)
b0b9b3df 3434 return ret;
7267ec00
KS
3435 }
3436
3437 /* Re-check under ptl */
b0b9b3df
HD
3438 if (unlikely(!pte_none(*vmf->pte)))
3439 return VM_FAULT_NOPAGE;
7267ec00 3440
3bb97794
KS
3441 flush_icache_page(vma, page);
3442 entry = mk_pte(page, vma->vm_page_prot);
3443 if (write)
3444 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
bae473a4
KS
3445 /* copy-on-write page */
3446 if (write && !(vma->vm_flags & VM_SHARED)) {
3bb97794 3447 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
82b0f8c3 3448 page_add_new_anon_rmap(page, vma, vmf->address, false);
7267ec00
KS
3449 mem_cgroup_commit_charge(page, memcg, false, false);
3450 lru_cache_add_active_or_unevictable(page, vma);
3bb97794 3451 } else {
eca56ff9 3452 inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
dd78fedd 3453 page_add_file_rmap(page, false);
3bb97794 3454 }
82b0f8c3 3455 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);
3bb97794
KS
3456
3457 /* no need to invalidate: a not-present page won't be cached */
82b0f8c3 3458 update_mmu_cache(vma, vmf->address, vmf->pte);
7267ec00 3459
b0b9b3df 3460 return 0;
3bb97794
KS
3461}
3462
9118c0cb
JK
3463
3464/**
3465 * finish_fault - finish page fault once we have prepared the page to fault
3466 *
3467 * @vmf: structure describing the fault
3468 *
3469 * This function handles all that is needed to finish a page fault once the
3470 * page to fault in is prepared. It handles locking of PTEs, inserts PTE for
3471 * given page, adds reverse page mapping, handles memcg charges and LRU
3472 * addition. The function returns 0 on success, VM_FAULT_ code in case of
3473 * error.
3474 *
3475 * The function expects the page to be locked and on success it consumes a
3476 * reference of a page being mapped (for the PTE which maps it).
3477 */
3478int finish_fault(struct vm_fault *vmf)
3479{
3480 struct page *page;
6b31d595 3481 int ret = 0;
9118c0cb
JK
3482
3483 /* Did we COW the page? */
3484 if ((vmf->flags & FAULT_FLAG_WRITE) &&
3485 !(vmf->vma->vm_flags & VM_SHARED))
3486 page = vmf->cow_page;
3487 else
3488 page = vmf->page;
6b31d595
MH
3489
3490 /*
3491 * check even for read faults because we might have lost our CoWed
3492 * page
3493 */
3494 if (!(vmf->vma->vm_flags & VM_SHARED))
3495 ret = check_stable_address_space(vmf->vma->vm_mm);
3496 if (!ret)
3497 ret = alloc_set_pte(vmf, vmf->memcg, page);
9118c0cb
JK
3498 if (vmf->pte)
3499 pte_unmap_unlock(vmf->pte, vmf->ptl);
3500 return ret;
3501}
3502
3a91053a
KS
3503static unsigned long fault_around_bytes __read_mostly =
3504 rounddown_pow_of_two(65536);
a9b0f861 3505
a9b0f861
KS
3506#ifdef CONFIG_DEBUG_FS
3507static int fault_around_bytes_get(void *data, u64 *val)
1592eef0 3508{
a9b0f861 3509 *val = fault_around_bytes;
1592eef0
KS
3510 return 0;
3511}
3512
b4903d6e
AR
3513/*
3514 * fault_around_pages() and fault_around_mask() expects fault_around_bytes
3515 * rounded down to nearest page order. It's what do_fault_around() expects to
3516 * see.
3517 */
a9b0f861 3518static int fault_around_bytes_set(void *data, u64 val)
1592eef0 3519{
a9b0f861 3520 if (val / PAGE_SIZE > PTRS_PER_PTE)
1592eef0 3521 return -EINVAL;
b4903d6e
AR
3522 if (val > PAGE_SIZE)
3523 fault_around_bytes = rounddown_pow_of_two(val);
3524 else
3525 fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */
1592eef0
KS
3526 return 0;
3527}
0a1345f8 3528DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops,
a9b0f861 3529 fault_around_bytes_get, fault_around_bytes_set, "%llu\n");
1592eef0
KS
3530
3531static int __init fault_around_debugfs(void)
3532{
3533 void *ret;
3534
0a1345f8 3535 ret = debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL,
a9b0f861 3536 &fault_around_bytes_fops);
1592eef0 3537 if (!ret)
a9b0f861 3538 pr_warn("Failed to create fault_around_bytes in debugfs");
1592eef0
KS
3539 return 0;
3540}
3541late_initcall(fault_around_debugfs);
1592eef0 3542#endif
8c6e50b0 3543
1fdb412b
KS
3544/*
3545 * do_fault_around() tries to map few pages around the fault address. The hope
3546 * is that the pages will be needed soon and this will lower the number of
3547 * faults to handle.
3548 *
3549 * It uses vm_ops->map_pages() to map the pages, which skips the page if it's
3550 * not ready to be mapped: not up-to-date, locked, etc.
3551 *
3552 * This function is called with the page table lock taken. In the split ptlock
3553 * case the page table lock only protects only those entries which belong to
3554 * the page table corresponding to the fault address.
3555 *
3556 * This function doesn't cross the VMA boundaries, in order to call map_pages()
3557 * only once.
3558 *
3559 * fault_around_pages() defines how many pages we'll try to map.
3560 * do_fault_around() expects it to return a power of two less than or equal to
3561 * PTRS_PER_PTE.
3562 *
3563 * The virtual address of the area that we map is naturally aligned to the
3564 * fault_around_pages() value (and therefore to page order). This way it's
3565 * easier to guarantee that we don't cross page table boundaries.
3566 */
0721ec8b 3567static int do_fault_around(struct vm_fault *vmf)
8c6e50b0 3568{
82b0f8c3 3569 unsigned long address = vmf->address, nr_pages, mask;
0721ec8b 3570 pgoff_t start_pgoff = vmf->pgoff;
bae473a4 3571 pgoff_t end_pgoff;
7267ec00 3572 int off, ret = 0;
8c6e50b0 3573
4db0c3c2 3574 nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT;
aecd6f44
KS
3575 mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
3576
82b0f8c3
JK
3577 vmf->address = max(address & mask, vmf->vma->vm_start);
3578 off = ((address - vmf->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
bae473a4 3579 start_pgoff -= off;
8c6e50b0
KS
3580
3581 /*
bae473a4
KS
3582 * end_pgoff is either end of page table or end of vma
3583 * or fault_around_pages() from start_pgoff, depending what is nearest.
8c6e50b0 3584 */
bae473a4 3585 end_pgoff = start_pgoff -
82b0f8c3 3586 ((vmf->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
8c6e50b0 3587 PTRS_PER_PTE - 1;
82b0f8c3 3588 end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1,
bae473a4 3589 start_pgoff + nr_pages - 1);
8c6e50b0 3590
82b0f8c3
JK
3591 if (pmd_none(*vmf->pmd)) {
3592 vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm,
3593 vmf->address);
3594 if (!vmf->prealloc_pte)
c5f88bd2 3595 goto out;
7267ec00 3596 smp_wmb(); /* See comment in __pte_alloc() */
8c6e50b0
KS
3597 }
3598
82b0f8c3 3599 vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff);
7267ec00 3600
7267ec00 3601 /* Huge page is mapped? Page fault is solved */
82b0f8c3 3602 if (pmd_trans_huge(*vmf->pmd)) {
7267ec00
KS
3603 ret = VM_FAULT_NOPAGE;
3604 goto out;
3605 }
3606
3607 /* ->map_pages() haven't done anything useful. Cold page cache? */
82b0f8c3 3608 if (!vmf->pte)
7267ec00
KS
3609 goto out;
3610
3611 /* check if the page fault is solved */
82b0f8c3
JK
3612 vmf->pte -= (vmf->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT);
3613 if (!pte_none(*vmf->pte))
7267ec00 3614 ret = VM_FAULT_NOPAGE;
82b0f8c3 3615 pte_unmap_unlock(vmf->pte, vmf->ptl);
bae473a4 3616out:
82b0f8c3
JK
3617 vmf->address = address;
3618 vmf->pte = NULL;
7267ec00 3619 return ret;
8c6e50b0
KS
3620}
3621
0721ec8b 3622static int do_read_fault(struct vm_fault *vmf)
e655fb29 3623{
82b0f8c3 3624 struct vm_area_struct *vma = vmf->vma;
8c6e50b0
KS
3625 int ret = 0;
3626
3627 /*
3628 * Let's call ->map_pages() first and use ->fault() as fallback
3629 * if page by the offset is not ready to be mapped (cold cache or
3630 * something).
3631 */
9b4bdd2f 3632 if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) {
0721ec8b 3633 ret = do_fault_around(vmf);
7267ec00
KS
3634 if (ret)
3635 return ret;
8c6e50b0 3636 }
e655fb29 3637
936ca80d 3638 ret = __do_fault(vmf);
e655fb29
KS
3639 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3640 return ret;
3641
9118c0cb 3642 ret |= finish_fault(vmf);
936ca80d 3643 unlock_page(vmf->page);
7267ec00 3644 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
936ca80d 3645 put_page(vmf->page);
e655fb29
KS
3646 return ret;
3647}
3648
0721ec8b 3649static int do_cow_fault(struct vm_fault *vmf)
ec47c3b9 3650{
82b0f8c3 3651 struct vm_area_struct *vma = vmf->vma;
ec47c3b9
KS
3652 int ret;
3653
3654 if (unlikely(anon_vma_prepare(vma)))
3655 return VM_FAULT_OOM;
3656
936ca80d
JK
3657 vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address);
3658 if (!vmf->cow_page)
ec47c3b9
KS
3659 return VM_FAULT_OOM;
3660
936ca80d 3661 if (mem_cgroup_try_charge(vmf->cow_page, vma->vm_mm, GFP_KERNEL,
3917048d 3662 &vmf->memcg, false)) {
936ca80d 3663 put_page(vmf->cow_page);
ec47c3b9
KS
3664 return VM_FAULT_OOM;
3665 }
3666
936ca80d 3667 ret = __do_fault(vmf);
ec47c3b9
KS
3668 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3669 goto uncharge_out;
3917048d
JK
3670 if (ret & VM_FAULT_DONE_COW)
3671 return ret;
ec47c3b9 3672
b1aa812b 3673 copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma);
936ca80d 3674 __SetPageUptodate(vmf->cow_page);
ec47c3b9 3675
9118c0cb 3676 ret |= finish_fault(vmf);
b1aa812b
JK
3677 unlock_page(vmf->page);
3678 put_page(vmf->page);
7267ec00
KS
3679 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3680 goto uncharge_out;
ec47c3b9
KS
3681 return ret;
3682uncharge_out:
3917048d 3683 mem_cgroup_cancel_charge(vmf->cow_page, vmf->memcg, false);
936ca80d 3684 put_page(vmf->cow_page);
ec47c3b9
KS
3685 return ret;
3686}
3687
0721ec8b 3688static int do_shared_fault(struct vm_fault *vmf)
1da177e4 3689{
82b0f8c3 3690 struct vm_area_struct *vma = vmf->vma;
f0c6d4d2 3691 int ret, tmp;
1d65f86d 3692
936ca80d 3693 ret = __do_fault(vmf);
7eae74af 3694 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
f0c6d4d2 3695 return ret;
1da177e4
LT
3696
3697 /*
f0c6d4d2
KS
3698 * Check if the backing address space wants to know that the page is
3699 * about to become writable
1da177e4 3700 */
fb09a464 3701 if (vma->vm_ops->page_mkwrite) {
936ca80d 3702 unlock_page(vmf->page);
38b8cb7f 3703 tmp = do_page_mkwrite(vmf);
fb09a464
KS
3704 if (unlikely(!tmp ||
3705 (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
936ca80d 3706 put_page(vmf->page);
fb09a464 3707 return tmp;
4294621f 3708 }
fb09a464
KS
3709 }
3710
9118c0cb 3711 ret |= finish_fault(vmf);
7267ec00
KS
3712 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
3713 VM_FAULT_RETRY))) {
936ca80d
JK
3714 unlock_page(vmf->page);
3715 put_page(vmf->page);
f0c6d4d2 3716 return ret;
1da177e4 3717 }
b827e496 3718
97ba0c2b 3719 fault_dirty_shared_page(vma, vmf->page);
1d65f86d 3720 return ret;
54cb8821 3721}
d00806b1 3722
9a95f3cf
PC
3723/*
3724 * We enter with non-exclusive mmap_sem (to exclude vma changes,
3725 * but allow concurrent faults).
3726 * The mmap_sem may have been released depending on flags and our
3727 * return value. See filemap_fault() and __lock_page_or_retry().
3728 */
82b0f8c3 3729static int do_fault(struct vm_fault *vmf)
54cb8821 3730{
82b0f8c3 3731 struct vm_area_struct *vma = vmf->vma;
b0b9b3df 3732 int ret;
54cb8821 3733
6b7339f4
KS
3734 /* The VMA was not fully populated on mmap() or missing VM_DONTEXPAND */
3735 if (!vma->vm_ops->fault)
b0b9b3df
HD
3736 ret = VM_FAULT_SIGBUS;
3737 else if (!(vmf->flags & FAULT_FLAG_WRITE))
3738 ret = do_read_fault(vmf);
3739 else if (!(vma->vm_flags & VM_SHARED))
3740 ret = do_cow_fault(vmf);
3741 else
3742 ret = do_shared_fault(vmf);
3743
3744 /* preallocated pagetable is unused: free it */
3745 if (vmf->prealloc_pte) {
3746 pte_free(vma->vm_mm, vmf->prealloc_pte);
7f2b6ce8 3747 vmf->prealloc_pte = NULL;
b0b9b3df
HD
3748 }
3749 return ret;
54cb8821
NP
3750}
3751
b19a9939 3752static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
04bb2f94
RR
3753 unsigned long addr, int page_nid,
3754 int *flags)
9532fec1
MG
3755{
3756 get_page(page);
3757
3758 count_vm_numa_event(NUMA_HINT_FAULTS);
04bb2f94 3759 if (page_nid == numa_node_id()) {
9532fec1 3760 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
04bb2f94
RR
3761 *flags |= TNF_FAULT_LOCAL;
3762 }
9532fec1
MG
3763
3764 return mpol_misplaced(page, vma, addr);
3765}
3766
2994302b 3767static int do_numa_page(struct vm_fault *vmf)
d10e63f2 3768{
82b0f8c3 3769 struct vm_area_struct *vma = vmf->vma;
4daae3b4 3770 struct page *page = NULL;
8191acbd 3771 int page_nid = -1;
90572890 3772 int last_cpupid;
cbee9f88 3773 int target_nid;
b8593bfd 3774 bool migrated = false;
cee216a6 3775 pte_t pte;
288bc549 3776 bool was_writable = pte_savedwrite(vmf->orig_pte);
6688cc05 3777 int flags = 0;
d10e63f2
MG
3778
3779 /*
166f61b9
TH
3780 * The "pte" at this point cannot be used safely without
3781 * validation through pte_unmap_same(). It's of NUMA type but
3782 * the pfn may be screwed if the read is non atomic.
166f61b9 3783 */
82b0f8c3
JK
3784 vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd);
3785 spin_lock(vmf->ptl);
cee216a6 3786 if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) {
82b0f8c3 3787 pte_unmap_unlock(vmf->pte, vmf->ptl);
4daae3b4
MG
3788 goto out;
3789 }
3790
cee216a6
AK
3791 /*
3792 * Make it present again, Depending on how arch implementes non
3793 * accessible ptes, some can allow access by kernel mode.
3794 */
3795 pte = ptep_modify_prot_start(vma->vm_mm, vmf->address, vmf->pte);
4d942466
MG
3796 pte = pte_modify(pte, vma->vm_page_prot);
3797 pte = pte_mkyoung(pte);
b191f9b1
MG
3798 if (was_writable)
3799 pte = pte_mkwrite(pte);
cee216a6 3800 ptep_modify_prot_commit(vma->vm_mm, vmf->address, vmf->pte, pte);
82b0f8c3 3801 update_mmu_cache(vma, vmf->address, vmf->pte);
d10e63f2 3802
82b0f8c3 3803 page = vm_normal_page(vma, vmf->address, pte);
d10e63f2 3804 if (!page) {
82b0f8c3 3805 pte_unmap_unlock(vmf->pte, vmf->ptl);
d10e63f2
MG
3806 return 0;
3807 }
3808
e81c4802
KS
3809 /* TODO: handle PTE-mapped THP */
3810 if (PageCompound(page)) {
82b0f8c3 3811 pte_unmap_unlock(vmf->pte, vmf->ptl);
e81c4802
KS
3812 return 0;
3813 }
3814
6688cc05 3815 /*
bea66fbd
MG
3816 * Avoid grouping on RO pages in general. RO pages shouldn't hurt as
3817 * much anyway since they can be in shared cache state. This misses
3818 * the case where a mapping is writable but the process never writes
3819 * to it but pte_write gets cleared during protection updates and
3820 * pte_dirty has unpredictable behaviour between PTE scan updates,
3821 * background writeback, dirty balancing and application behaviour.
6688cc05 3822 */
d59dc7bc 3823 if (!pte_write(pte))
6688cc05
PZ
3824 flags |= TNF_NO_GROUP;
3825
dabe1d99
RR
3826 /*
3827 * Flag if the page is shared between multiple address spaces. This
3828 * is later used when determining whether to group tasks together
3829 */
3830 if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED))
3831 flags |= TNF_SHARED;
3832
90572890 3833 last_cpupid = page_cpupid_last(page);
8191acbd 3834 page_nid = page_to_nid(page);
82b0f8c3 3835 target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid,
bae473a4 3836 &flags);
82b0f8c3 3837 pte_unmap_unlock(vmf->pte, vmf->ptl);
4daae3b4 3838 if (target_nid == -1) {
4daae3b4
MG
3839 put_page(page);
3840 goto out;
3841 }
3842
3843 /* Migrate to the requested node */
1bc115d8 3844 migrated = migrate_misplaced_page(page, vma, target_nid);
6688cc05 3845 if (migrated) {
8191acbd 3846 page_nid = target_nid;
6688cc05 3847 flags |= TNF_MIGRATED;
074c2381
MG
3848 } else
3849 flags |= TNF_MIGRATE_FAIL;
4daae3b4
MG
3850
3851out:
8191acbd 3852 if (page_nid != -1)
6688cc05 3853 task_numa_fault(last_cpupid, page_nid, 1, flags);
d10e63f2
MG
3854 return 0;
3855}
3856
91a90140 3857static inline int create_huge_pmd(struct vm_fault *vmf)
b96375f7 3858{
f4200391 3859 if (vma_is_anonymous(vmf->vma))
82b0f8c3 3860 return do_huge_pmd_anonymous_page(vmf);
a2d58167 3861 if (vmf->vma->vm_ops->huge_fault)
c791ace1 3862 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);
b96375f7
MW
3863 return VM_FAULT_FALLBACK;
3864}
3865
183f24aa
GU
3866/* `inline' is required to avoid gcc 4.1.2 build error */
3867static inline int wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd)
b96375f7 3868{
82b0f8c3
JK
3869 if (vma_is_anonymous(vmf->vma))
3870 return do_huge_pmd_wp_page(vmf, orig_pmd);
a2d58167 3871 if (vmf->vma->vm_ops->huge_fault)
c791ace1 3872 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);
af9e4d5f
KS
3873
3874 /* COW handled on pte level: split pmd */
82b0f8c3
JK
3875 VM_BUG_ON_VMA(vmf->vma->vm_flags & VM_SHARED, vmf->vma);
3876 __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL);
af9e4d5f 3877
b96375f7
MW
3878 return VM_FAULT_FALLBACK;
3879}
3880
38e08854
LS
3881static inline bool vma_is_accessible(struct vm_area_struct *vma)
3882{
3883 return vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE);
3884}
3885
a00cc7d9
MW
3886static int create_huge_pud(struct vm_fault *vmf)
3887{
3888#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3889 /* No support for anonymous transparent PUD pages yet */
3890 if (vma_is_anonymous(vmf->vma))
3891 return VM_FAULT_FALLBACK;
3892 if (vmf->vma->vm_ops->huge_fault)
c791ace1 3893 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD);
a00cc7d9
MW
3894#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
3895 return VM_FAULT_FALLBACK;
3896}
3897
3898static int wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud)
3899{
3900#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3901 /* No support for anonymous transparent PUD pages yet */
3902 if (vma_is_anonymous(vmf->vma))
3903 return VM_FAULT_FALLBACK;
3904 if (vmf->vma->vm_ops->huge_fault)
c791ace1 3905 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD);
a00cc7d9
MW
3906#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
3907 return VM_FAULT_FALLBACK;
3908}
3909
1da177e4
LT
3910/*
3911 * These routines also need to handle stuff like marking pages dirty
3912 * and/or accessed for architectures that don't do it in hardware (most
3913 * RISC architectures). The early dirtying is also good on the i386.
3914 *
3915 * There is also a hook called "update_mmu_cache()" that architectures
3916 * with external mmu caches can use to update those (ie the Sparc or
3917 * PowerPC hashed page tables that act as extended TLBs).
3918 *
7267ec00
KS
3919 * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow
3920 * concurrent faults).
9a95f3cf 3921 *
7267ec00
KS
3922 * The mmap_sem may have been released depending on flags and our return value.
3923 * See filemap_fault() and __lock_page_or_retry().
1da177e4 3924 */
82b0f8c3 3925static int handle_pte_fault(struct vm_fault *vmf)
1da177e4
LT
3926{
3927 pte_t entry;
3928
82b0f8c3 3929 if (unlikely(pmd_none(*vmf->pmd))) {
7267ec00
KS
3930 /*
3931 * Leave __pte_alloc() until later: because vm_ops->fault may
3932 * want to allocate huge page, and if we expose page table
3933 * for an instant, it will be difficult to retract from
3934 * concurrent faults and from rmap lookups.
3935 */
82b0f8c3 3936 vmf->pte = NULL;
7267ec00
KS
3937 } else {
3938 /* See comment in pte_alloc_one_map() */
d0f0931d 3939 if (pmd_devmap_trans_unstable(vmf->pmd))
7267ec00
KS
3940 return 0;
3941 /*
3942 * A regular pmd is established and it can't morph into a huge
3943 * pmd from under us anymore at this point because we hold the
3944 * mmap_sem read mode and khugepaged takes it in write mode.
3945 * So now it's safe to run pte_offset_map().
3946 */
82b0f8c3 3947 vmf->pte = pte_offset_map(vmf->pmd, vmf->address);
2994302b 3948 vmf->orig_pte = *vmf->pte;
7267ec00
KS
3949
3950 /*
3951 * some architectures can have larger ptes than wordsize,
3952 * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and
b03a0fe0
PM
3953 * CONFIG_32BIT=y, so READ_ONCE cannot guarantee atomic
3954 * accesses. The code below just needs a consistent view
3955 * for the ifs and we later double check anyway with the
7267ec00
KS
3956 * ptl lock held. So here a barrier will do.
3957 */
3958 barrier();
2994302b 3959 if (pte_none(vmf->orig_pte)) {
82b0f8c3
JK
3960 pte_unmap(vmf->pte);
3961 vmf->pte = NULL;
65500d23 3962 }
1da177e4
LT
3963 }
3964
82b0f8c3
JK
3965 if (!vmf->pte) {
3966 if (vma_is_anonymous(vmf->vma))
3967 return do_anonymous_page(vmf);
7267ec00 3968 else
82b0f8c3 3969 return do_fault(vmf);
7267ec00
KS
3970 }
3971
2994302b
JK
3972 if (!pte_present(vmf->orig_pte))
3973 return do_swap_page(vmf);
7267ec00 3974
2994302b
JK
3975 if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma))
3976 return do_numa_page(vmf);
d10e63f2 3977
82b0f8c3
JK
3978 vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
3979 spin_lock(vmf->ptl);
2994302b 3980 entry = vmf->orig_pte;
82b0f8c3 3981 if (unlikely(!pte_same(*vmf->pte, entry)))
8f4e2101 3982 goto unlock;
82b0f8c3 3983 if (vmf->flags & FAULT_FLAG_WRITE) {
f6f37321 3984 if (!pte_write(entry))
2994302b 3985 return do_wp_page(vmf);
1da177e4
LT
3986 entry = pte_mkdirty(entry);
3987 }
3988 entry = pte_mkyoung(entry);
82b0f8c3
JK
3989 if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry,
3990 vmf->flags & FAULT_FLAG_WRITE)) {
3991 update_mmu_cache(vmf->vma, vmf->address, vmf->pte);
1a44e149
AA
3992 } else {
3993 /*
3994 * This is needed only for protection faults but the arch code
3995 * is not yet telling us if this is a protection fault or not.
3996 * This still avoids useless tlb flushes for .text page faults
3997 * with threads.
3998 */
82b0f8c3
JK
3999 if (vmf->flags & FAULT_FLAG_WRITE)
4000 flush_tlb_fix_spurious_fault(vmf->vma, vmf->address);
1a44e149 4001 }
8f4e2101 4002unlock:
82b0f8c3 4003 pte_unmap_unlock(vmf->pte, vmf->ptl);
83c54070 4004 return 0;
1da177e4
LT
4005}
4006
4007/*
4008 * By the time we get here, we already hold the mm semaphore
9a95f3cf
PC
4009 *
4010 * The mmap_sem may have been released depending on flags and our
4011 * return value. See filemap_fault() and __lock_page_or_retry().
1da177e4 4012 */
dcddffd4
KS
4013static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
4014 unsigned int flags)
1da177e4 4015{
82b0f8c3 4016 struct vm_fault vmf = {
bae473a4 4017 .vma = vma,
1a29d85e 4018 .address = address & PAGE_MASK,
bae473a4 4019 .flags = flags,
0721ec8b 4020 .pgoff = linear_page_index(vma, address),
667240e0 4021 .gfp_mask = __get_fault_gfp_mask(vma),
bae473a4 4022 };
fde26bed 4023 unsigned int dirty = flags & FAULT_FLAG_WRITE;
dcddffd4 4024 struct mm_struct *mm = vma->vm_mm;
1da177e4 4025 pgd_t *pgd;
c2febafc 4026 p4d_t *p4d;
a2d58167 4027 int ret;
1da177e4 4028
1da177e4 4029 pgd = pgd_offset(mm, address);
c2febafc
KS
4030 p4d = p4d_alloc(mm, pgd, address);
4031 if (!p4d)
4032 return VM_FAULT_OOM;
a00cc7d9 4033
c2febafc 4034 vmf.pud = pud_alloc(mm, p4d, address);
a00cc7d9 4035 if (!vmf.pud)
c74df32c 4036 return VM_FAULT_OOM;
a00cc7d9 4037 if (pud_none(*vmf.pud) && transparent_hugepage_enabled(vma)) {
a00cc7d9
MW
4038 ret = create_huge_pud(&vmf);
4039 if (!(ret & VM_FAULT_FALLBACK))
4040 return ret;
4041 } else {
4042 pud_t orig_pud = *vmf.pud;
4043
4044 barrier();
4045 if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) {
a00cc7d9 4046
a00cc7d9
MW
4047 /* NUMA case for anonymous PUDs would go here */
4048
f6f37321 4049 if (dirty && !pud_write(orig_pud)) {
a00cc7d9
MW
4050 ret = wp_huge_pud(&vmf, orig_pud);
4051 if (!(ret & VM_FAULT_FALLBACK))
4052 return ret;
4053 } else {
4054 huge_pud_set_accessed(&vmf, orig_pud);
4055 return 0;
4056 }
4057 }
4058 }
4059
4060 vmf.pmd = pmd_alloc(mm, vmf.pud, address);
82b0f8c3 4061 if (!vmf.pmd)
c74df32c 4062 return VM_FAULT_OOM;
82b0f8c3 4063 if (pmd_none(*vmf.pmd) && transparent_hugepage_enabled(vma)) {
a2d58167 4064 ret = create_huge_pmd(&vmf);
c0292554
KS
4065 if (!(ret & VM_FAULT_FALLBACK))
4066 return ret;
71e3aac0 4067 } else {
82b0f8c3 4068 pmd_t orig_pmd = *vmf.pmd;
1f1d06c3 4069
71e3aac0 4070 barrier();
84c3fc4e
ZY
4071 if (unlikely(is_swap_pmd(orig_pmd))) {
4072 VM_BUG_ON(thp_migration_supported() &&
4073 !is_pmd_migration_entry(orig_pmd));
4074 if (is_pmd_migration_entry(orig_pmd))
4075 pmd_migration_entry_wait(mm, vmf.pmd);
4076 return 0;
4077 }
5c7fb56e 4078 if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) {
38e08854 4079 if (pmd_protnone(orig_pmd) && vma_is_accessible(vma))
82b0f8c3 4080 return do_huge_pmd_numa_page(&vmf, orig_pmd);
d10e63f2 4081
f6f37321 4082 if (dirty && !pmd_write(orig_pmd)) {
82b0f8c3 4083 ret = wp_huge_pmd(&vmf, orig_pmd);
9845cbbd
KS
4084 if (!(ret & VM_FAULT_FALLBACK))
4085 return ret;
a1dd450b 4086 } else {
82b0f8c3 4087 huge_pmd_set_accessed(&vmf, orig_pmd);
9845cbbd 4088 return 0;
1f1d06c3 4089 }
71e3aac0
AA
4090 }
4091 }
4092
82b0f8c3 4093 return handle_pte_fault(&vmf);
1da177e4
LT
4094}
4095
9a95f3cf
PC
4096/*
4097 * By the time we get here, we already hold the mm semaphore
4098 *
4099 * The mmap_sem may have been released depending on flags and our
4100 * return value. See filemap_fault() and __lock_page_or_retry().
4101 */
dcddffd4
KS
4102int handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
4103 unsigned int flags)
519e5247
JW
4104{
4105 int ret;
4106
4107 __set_current_state(TASK_RUNNING);
4108
4109 count_vm_event(PGFAULT);
2262185c 4110 count_memcg_event_mm(vma->vm_mm, PGFAULT);
519e5247
JW
4111
4112 /* do counter updates before entering really critical section. */
4113 check_sync_rss_stat(current);
4114
de0c799b
LD
4115 if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
4116 flags & FAULT_FLAG_INSTRUCTION,
4117 flags & FAULT_FLAG_REMOTE))
4118 return VM_FAULT_SIGSEGV;
4119
519e5247
JW
4120 /*
4121 * Enable the memcg OOM handling for faults triggered in user
4122 * space. Kernel faults are handled more gracefully.
4123 */
4124 if (flags & FAULT_FLAG_USER)
49426420 4125 mem_cgroup_oom_enable();
519e5247 4126
bae473a4
KS
4127 if (unlikely(is_vm_hugetlb_page(vma)))
4128 ret = hugetlb_fault(vma->vm_mm, vma, address, flags);
4129 else
4130 ret = __handle_mm_fault(vma, address, flags);
519e5247 4131
49426420
JW
4132 if (flags & FAULT_FLAG_USER) {
4133 mem_cgroup_oom_disable();
166f61b9
TH
4134 /*
4135 * The task may have entered a memcg OOM situation but
4136 * if the allocation error was handled gracefully (no
4137 * VM_FAULT_OOM), there is no need to kill anything.
4138 * Just clean up the OOM state peacefully.
4139 */
4140 if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
4141 mem_cgroup_oom_synchronize(false);
49426420 4142 }
3812c8c8 4143
519e5247
JW
4144 return ret;
4145}
e1d6d01a 4146EXPORT_SYMBOL_GPL(handle_mm_fault);
519e5247 4147
90eceff1
KS
4148#ifndef __PAGETABLE_P4D_FOLDED
4149/*
4150 * Allocate p4d page table.
4151 * We've already handled the fast-path in-line.
4152 */
4153int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
4154{
4155 p4d_t *new = p4d_alloc_one(mm, address);
4156 if (!new)
4157 return -ENOMEM;
4158
4159 smp_wmb(); /* See comment in __pte_alloc */
4160
4161 spin_lock(&mm->page_table_lock);
4162 if (pgd_present(*pgd)) /* Another has populated it */
4163 p4d_free(mm, new);
4164 else
4165 pgd_populate(mm, pgd, new);
4166 spin_unlock(&mm->page_table_lock);
4167 return 0;
4168}
4169#endif /* __PAGETABLE_P4D_FOLDED */
4170
1da177e4
LT
4171#ifndef __PAGETABLE_PUD_FOLDED
4172/*
4173 * Allocate page upper directory.
872fec16 4174 * We've already handled the fast-path in-line.
1da177e4 4175 */
c2febafc 4176int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address)
1da177e4 4177{
c74df32c
HD
4178 pud_t *new = pud_alloc_one(mm, address);
4179 if (!new)
1bb3630e 4180 return -ENOMEM;
1da177e4 4181
362a61ad
NP
4182 smp_wmb(); /* See comment in __pte_alloc */
4183
872fec16 4184 spin_lock(&mm->page_table_lock);
c2febafc 4185#ifndef __ARCH_HAS_5LEVEL_HACK
b4e98d9a
KS
4186 if (!p4d_present(*p4d)) {
4187 mm_inc_nr_puds(mm);
c2febafc 4188 p4d_populate(mm, p4d, new);
b4e98d9a 4189 } else /* Another has populated it */
5e541973 4190 pud_free(mm, new);
b4e98d9a
KS
4191#else
4192 if (!pgd_present(*p4d)) {
4193 mm_inc_nr_puds(mm);
c2febafc 4194 pgd_populate(mm, p4d, new);
b4e98d9a
KS
4195 } else /* Another has populated it */
4196 pud_free(mm, new);
c2febafc 4197#endif /* __ARCH_HAS_5LEVEL_HACK */
c74df32c 4198 spin_unlock(&mm->page_table_lock);
1bb3630e 4199 return 0;
1da177e4
LT
4200}
4201#endif /* __PAGETABLE_PUD_FOLDED */
4202
4203#ifndef __PAGETABLE_PMD_FOLDED
4204/*
4205 * Allocate page middle directory.
872fec16 4206 * We've already handled the fast-path in-line.
1da177e4 4207 */
1bb3630e 4208int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
1da177e4 4209{
a00cc7d9 4210 spinlock_t *ptl;
c74df32c
HD
4211 pmd_t *new = pmd_alloc_one(mm, address);
4212 if (!new)
1bb3630e 4213 return -ENOMEM;
1da177e4 4214
362a61ad
NP
4215 smp_wmb(); /* See comment in __pte_alloc */
4216
a00cc7d9 4217 ptl = pud_lock(mm, pud);
1da177e4 4218#ifndef __ARCH_HAS_4LEVEL_HACK
dc6c9a35
KS
4219 if (!pud_present(*pud)) {
4220 mm_inc_nr_pmds(mm);
1bb3630e 4221 pud_populate(mm, pud, new);
dc6c9a35 4222 } else /* Another has populated it */
5e541973 4223 pmd_free(mm, new);
dc6c9a35
KS
4224#else
4225 if (!pgd_present(*pud)) {
4226 mm_inc_nr_pmds(mm);
1bb3630e 4227 pgd_populate(mm, pud, new);
dc6c9a35
KS
4228 } else /* Another has populated it */
4229 pmd_free(mm, new);
1da177e4 4230#endif /* __ARCH_HAS_4LEVEL_HACK */
a00cc7d9 4231 spin_unlock(ptl);
1bb3630e 4232 return 0;
e0f39591 4233}
1da177e4
LT
4234#endif /* __PAGETABLE_PMD_FOLDED */
4235
09796395 4236static int __follow_pte_pmd(struct mm_struct *mm, unsigned long address,
a4d1a885
JG
4237 unsigned long *start, unsigned long *end,
4238 pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp)
f8ad0f49
JW
4239{
4240 pgd_t *pgd;
c2febafc 4241 p4d_t *p4d;
f8ad0f49
JW
4242 pud_t *pud;
4243 pmd_t *pmd;
4244 pte_t *ptep;
4245
4246 pgd = pgd_offset(mm, address);
4247 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
4248 goto out;
4249
c2febafc
KS
4250 p4d = p4d_offset(pgd, address);
4251 if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d)))
4252 goto out;
4253
4254 pud = pud_offset(p4d, address);
f8ad0f49
JW
4255 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
4256 goto out;
4257
4258 pmd = pmd_offset(pud, address);
f66055ab 4259 VM_BUG_ON(pmd_trans_huge(*pmd));
f8ad0f49 4260
09796395
RZ
4261 if (pmd_huge(*pmd)) {
4262 if (!pmdpp)
4263 goto out;
4264
a4d1a885
JG
4265 if (start && end) {
4266 *start = address & PMD_MASK;
4267 *end = *start + PMD_SIZE;
4268 mmu_notifier_invalidate_range_start(mm, *start, *end);
4269 }
09796395
RZ
4270 *ptlp = pmd_lock(mm, pmd);
4271 if (pmd_huge(*pmd)) {
4272 *pmdpp = pmd;
4273 return 0;
4274 }
4275 spin_unlock(*ptlp);
a4d1a885
JG
4276 if (start && end)
4277 mmu_notifier_invalidate_range_end(mm, *start, *end);
09796395
RZ
4278 }
4279
4280 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
f8ad0f49
JW
4281 goto out;
4282
a4d1a885
JG
4283 if (start && end) {
4284 *start = address & PAGE_MASK;
4285 *end = *start + PAGE_SIZE;
4286 mmu_notifier_invalidate_range_start(mm, *start, *end);
4287 }
f8ad0f49 4288 ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
f8ad0f49
JW
4289 if (!pte_present(*ptep))
4290 goto unlock;
4291 *ptepp = ptep;
4292 return 0;
4293unlock:
4294 pte_unmap_unlock(ptep, *ptlp);
a4d1a885
JG
4295 if (start && end)
4296 mmu_notifier_invalidate_range_end(mm, *start, *end);
f8ad0f49
JW
4297out:
4298 return -EINVAL;
4299}
4300
f729c8c9
RZ
4301static inline int follow_pte(struct mm_struct *mm, unsigned long address,
4302 pte_t **ptepp, spinlock_t **ptlp)
1b36ba81
NK
4303{
4304 int res;
4305
4306 /* (void) is needed to make gcc happy */
4307 (void) __cond_lock(*ptlp,
a4d1a885
JG
4308 !(res = __follow_pte_pmd(mm, address, NULL, NULL,
4309 ptepp, NULL, ptlp)));
09796395
RZ
4310 return res;
4311}
4312
4313int follow_pte_pmd(struct mm_struct *mm, unsigned long address,
a4d1a885 4314 unsigned long *start, unsigned long *end,
09796395
RZ
4315 pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp)
4316{
4317 int res;
4318
4319 /* (void) is needed to make gcc happy */
4320 (void) __cond_lock(*ptlp,
a4d1a885
JG
4321 !(res = __follow_pte_pmd(mm, address, start, end,
4322 ptepp, pmdpp, ptlp)));
1b36ba81
NK
4323 return res;
4324}
09796395 4325EXPORT_SYMBOL(follow_pte_pmd);
1b36ba81 4326
3b6748e2
JW
4327/**
4328 * follow_pfn - look up PFN at a user virtual address
4329 * @vma: memory mapping
4330 * @address: user virtual address
4331 * @pfn: location to store found PFN
4332 *
4333 * Only IO mappings and raw PFN mappings are allowed.
4334 *
4335 * Returns zero and the pfn at @pfn on success, -ve otherwise.
4336 */
4337int follow_pfn(struct vm_area_struct *vma, unsigned long address,
4338 unsigned long *pfn)
4339{
4340 int ret = -EINVAL;
4341 spinlock_t *ptl;
4342 pte_t *ptep;
4343
4344 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
4345 return ret;
4346
4347 ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
4348 if (ret)
4349 return ret;
4350 *pfn = pte_pfn(*ptep);
4351 pte_unmap_unlock(ptep, ptl);
4352 return 0;
4353}
4354EXPORT_SYMBOL(follow_pfn);
4355
28b2ee20 4356#ifdef CONFIG_HAVE_IOREMAP_PROT
d87fe660 4357int follow_phys(struct vm_area_struct *vma,
4358 unsigned long address, unsigned int flags,
4359 unsigned long *prot, resource_size_t *phys)
28b2ee20 4360{
03668a4d 4361 int ret = -EINVAL;
28b2ee20
RR
4362 pte_t *ptep, pte;
4363 spinlock_t *ptl;
28b2ee20 4364
d87fe660 4365 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
4366 goto out;
28b2ee20 4367
03668a4d 4368 if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
d87fe660 4369 goto out;
28b2ee20 4370 pte = *ptep;
03668a4d 4371
f6f37321 4372 if ((flags & FOLL_WRITE) && !pte_write(pte))
28b2ee20 4373 goto unlock;
28b2ee20
RR
4374
4375 *prot = pgprot_val(pte_pgprot(pte));
03668a4d 4376 *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
28b2ee20 4377
03668a4d 4378 ret = 0;
28b2ee20
RR
4379unlock:
4380 pte_unmap_unlock(ptep, ptl);
4381out:
d87fe660 4382 return ret;
28b2ee20
RR
4383}
4384
4385int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
4386 void *buf, int len, int write)
4387{
4388 resource_size_t phys_addr;
4389 unsigned long prot = 0;
2bc7273b 4390 void __iomem *maddr;
28b2ee20
RR
4391 int offset = addr & (PAGE_SIZE-1);
4392
d87fe660 4393 if (follow_phys(vma, addr, write, &prot, &phys_addr))
28b2ee20
RR
4394 return -EINVAL;
4395
9cb12d7b 4396 maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot);
28b2ee20
RR
4397 if (write)
4398 memcpy_toio(maddr + offset, buf, len);
4399 else
4400 memcpy_fromio(buf, maddr + offset, len);
4401 iounmap(maddr);
4402
4403 return len;
4404}
5a73633e 4405EXPORT_SYMBOL_GPL(generic_access_phys);
28b2ee20
RR
4406#endif
4407
0ec76a11 4408/*
206cb636
SW
4409 * Access another process' address space as given in mm. If non-NULL, use the
4410 * given task for page fault accounting.
0ec76a11 4411 */
84d77d3f 4412int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
442486ec 4413 unsigned long addr, void *buf, int len, unsigned int gup_flags)
0ec76a11 4414{
0ec76a11 4415 struct vm_area_struct *vma;
0ec76a11 4416 void *old_buf = buf;
442486ec 4417 int write = gup_flags & FOLL_WRITE;
0ec76a11 4418
0ec76a11 4419 down_read(&mm->mmap_sem);
183ff22b 4420 /* ignore errors, just check how much was successfully transferred */
0ec76a11
DH
4421 while (len) {
4422 int bytes, ret, offset;
4423 void *maddr;
28b2ee20 4424 struct page *page = NULL;
0ec76a11 4425
1e987790 4426 ret = get_user_pages_remote(tsk, mm, addr, 1,
5b56d49f 4427 gup_flags, &page, &vma, NULL);
28b2ee20 4428 if (ret <= 0) {
dbffcd03
RR
4429#ifndef CONFIG_HAVE_IOREMAP_PROT
4430 break;
4431#else
28b2ee20
RR
4432 /*
4433 * Check if this is a VM_IO | VM_PFNMAP VMA, which
4434 * we can access using slightly different code.
4435 */
28b2ee20 4436 vma = find_vma(mm, addr);
fe936dfc 4437 if (!vma || vma->vm_start > addr)
28b2ee20
RR
4438 break;
4439 if (vma->vm_ops && vma->vm_ops->access)
4440 ret = vma->vm_ops->access(vma, addr, buf,
4441 len, write);
4442 if (ret <= 0)
28b2ee20
RR
4443 break;
4444 bytes = ret;
dbffcd03 4445#endif
0ec76a11 4446 } else {
28b2ee20
RR
4447 bytes = len;
4448 offset = addr & (PAGE_SIZE-1);
4449 if (bytes > PAGE_SIZE-offset)
4450 bytes = PAGE_SIZE-offset;
4451
4452 maddr = kmap(page);
4453 if (write) {
4454 copy_to_user_page(vma, page, addr,
4455 maddr + offset, buf, bytes);
4456 set_page_dirty_lock(page);
4457 } else {
4458 copy_from_user_page(vma, page, addr,
4459 buf, maddr + offset, bytes);
4460 }
4461 kunmap(page);
09cbfeaf 4462 put_page(page);
0ec76a11 4463 }
0ec76a11
DH
4464 len -= bytes;
4465 buf += bytes;
4466 addr += bytes;
4467 }
4468 up_read(&mm->mmap_sem);
0ec76a11
DH
4469
4470 return buf - old_buf;
4471}
03252919 4472
5ddd36b9 4473/**
ae91dbfc 4474 * access_remote_vm - access another process' address space
5ddd36b9
SW
4475 * @mm: the mm_struct of the target address space
4476 * @addr: start address to access
4477 * @buf: source or destination buffer
4478 * @len: number of bytes to transfer
6347e8d5 4479 * @gup_flags: flags modifying lookup behaviour
5ddd36b9
SW
4480 *
4481 * The caller must hold a reference on @mm.
4482 */
4483int access_remote_vm(struct mm_struct *mm, unsigned long addr,
6347e8d5 4484 void *buf, int len, unsigned int gup_flags)
5ddd36b9 4485{
6347e8d5 4486 return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags);
5ddd36b9
SW
4487}
4488
206cb636
SW
4489/*
4490 * Access another process' address space.
4491 * Source/target buffer must be kernel space,
4492 * Do not walk the page table directly, use get_user_pages
4493 */
4494int access_process_vm(struct task_struct *tsk, unsigned long addr,
f307ab6d 4495 void *buf, int len, unsigned int gup_flags)
206cb636
SW
4496{
4497 struct mm_struct *mm;
4498 int ret;
4499
4500 mm = get_task_mm(tsk);
4501 if (!mm)
4502 return 0;
4503
f307ab6d 4504 ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags);
442486ec 4505
206cb636
SW
4506 mmput(mm);
4507
4508 return ret;
4509}
fcd35857 4510EXPORT_SYMBOL_GPL(access_process_vm);
206cb636 4511
03252919
AK
4512/*
4513 * Print the name of a VMA.
4514 */
4515void print_vma_addr(char *prefix, unsigned long ip)
4516{
4517 struct mm_struct *mm = current->mm;
4518 struct vm_area_struct *vma;
4519
e8bff74a 4520 /*
0a7f682d 4521 * we might be running from an atomic context so we cannot sleep
e8bff74a 4522 */
0a7f682d 4523 if (!down_read_trylock(&mm->mmap_sem))
e8bff74a
IM
4524 return;
4525
03252919
AK
4526 vma = find_vma(mm, ip);
4527 if (vma && vma->vm_file) {
4528 struct file *f = vma->vm_file;
0a7f682d 4529 char *buf = (char *)__get_free_page(GFP_NOWAIT);
03252919 4530 if (buf) {
2fbc57c5 4531 char *p;
03252919 4532
9bf39ab2 4533 p = file_path(f, buf, PAGE_SIZE);
03252919
AK
4534 if (IS_ERR(p))
4535 p = "?";
2fbc57c5 4536 printk("%s%s[%lx+%lx]", prefix, kbasename(p),
03252919
AK
4537 vma->vm_start,
4538 vma->vm_end - vma->vm_start);
4539 free_page((unsigned long)buf);
4540 }
4541 }
51a07e50 4542 up_read(&mm->mmap_sem);
03252919 4543}
3ee1afa3 4544
662bbcb2 4545#if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
9ec23531 4546void __might_fault(const char *file, int line)
3ee1afa3 4547{
95156f00
PZ
4548 /*
4549 * Some code (nfs/sunrpc) uses socket ops on kernel memory while
4550 * holding the mmap_sem, this is safe because kernel memory doesn't
4551 * get paged out, therefore we'll never actually fault, and the
4552 * below annotations will generate false positives.
4553 */
db68ce10 4554 if (uaccess_kernel())
95156f00 4555 return;
9ec23531 4556 if (pagefault_disabled())
662bbcb2 4557 return;
9ec23531
DH
4558 __might_sleep(file, line, 0);
4559#if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
662bbcb2 4560 if (current->mm)
3ee1afa3 4561 might_lock_read(&current->mm->mmap_sem);
9ec23531 4562#endif
3ee1afa3 4563}
9ec23531 4564EXPORT_SYMBOL(__might_fault);
3ee1afa3 4565#endif
47ad8475
AA
4566
4567#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
4568static void clear_gigantic_page(struct page *page,
4569 unsigned long addr,
4570 unsigned int pages_per_huge_page)
4571{
4572 int i;
4573 struct page *p = page;
4574
4575 might_sleep();
4576 for (i = 0; i < pages_per_huge_page;
4577 i++, p = mem_map_next(p, page, i)) {
4578 cond_resched();
4579 clear_user_highpage(p, addr + i * PAGE_SIZE);
4580 }
4581}
4582void clear_huge_page(struct page *page,
c79b57e4 4583 unsigned long addr_hint, unsigned int pages_per_huge_page)
47ad8475 4584{
c79b57e4
HY
4585 int i, n, base, l;
4586 unsigned long addr = addr_hint &
4587 ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
47ad8475
AA
4588
4589 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
4590 clear_gigantic_page(page, addr, pages_per_huge_page);
4591 return;
4592 }
4593
c79b57e4 4594 /* Clear sub-page to access last to keep its cache lines hot */
47ad8475 4595 might_sleep();
c79b57e4
HY
4596 n = (addr_hint - addr) / PAGE_SIZE;
4597 if (2 * n <= pages_per_huge_page) {
4598 /* If sub-page to access in first half of huge page */
4599 base = 0;
4600 l = n;
4601 /* Clear sub-pages at the end of huge page */
4602 for (i = pages_per_huge_page - 1; i >= 2 * n; i--) {
4603 cond_resched();
4604 clear_user_highpage(page + i, addr + i * PAGE_SIZE);
4605 }
4606 } else {
4607 /* If sub-page to access in second half of huge page */
4608 base = pages_per_huge_page - 2 * (pages_per_huge_page - n);
4609 l = pages_per_huge_page - n;
4610 /* Clear sub-pages at the begin of huge page */
4611 for (i = 0; i < base; i++) {
4612 cond_resched();
4613 clear_user_highpage(page + i, addr + i * PAGE_SIZE);
4614 }
4615 }
4616 /*
4617 * Clear remaining sub-pages in left-right-left-right pattern
4618 * towards the sub-page to access
4619 */
4620 for (i = 0; i < l; i++) {
4621 int left_idx = base + i;
4622 int right_idx = base + 2 * l - 1 - i;
4623
4624 cond_resched();
4625 clear_user_highpage(page + left_idx,
4626 addr + left_idx * PAGE_SIZE);
47ad8475 4627 cond_resched();
c79b57e4
HY
4628 clear_user_highpage(page + right_idx,
4629 addr + right_idx * PAGE_SIZE);
47ad8475
AA
4630 }
4631}
4632
4633static void copy_user_gigantic_page(struct page *dst, struct page *src,
4634 unsigned long addr,
4635 struct vm_area_struct *vma,
4636 unsigned int pages_per_huge_page)
4637{
4638 int i;
4639 struct page *dst_base = dst;
4640 struct page *src_base = src;
4641
4642 for (i = 0; i < pages_per_huge_page; ) {
4643 cond_resched();
4644 copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
4645
4646 i++;
4647 dst = mem_map_next(dst, dst_base, i);
4648 src = mem_map_next(src, src_base, i);
4649 }
4650}
4651
4652void copy_user_huge_page(struct page *dst, struct page *src,
4653 unsigned long addr, struct vm_area_struct *vma,
4654 unsigned int pages_per_huge_page)
4655{
4656 int i;
4657
4658 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
4659 copy_user_gigantic_page(dst, src, addr, vma,
4660 pages_per_huge_page);
4661 return;
4662 }
4663
4664 might_sleep();
4665 for (i = 0; i < pages_per_huge_page; i++) {
4666 cond_resched();
4667 copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
4668 }
4669}
fa4d75c1
MK
4670
4671long copy_huge_page_from_user(struct page *dst_page,
4672 const void __user *usr_src,
810a56b9
MK
4673 unsigned int pages_per_huge_page,
4674 bool allow_pagefault)
fa4d75c1
MK
4675{
4676 void *src = (void *)usr_src;
4677 void *page_kaddr;
4678 unsigned long i, rc = 0;
4679 unsigned long ret_val = pages_per_huge_page * PAGE_SIZE;
4680
4681 for (i = 0; i < pages_per_huge_page; i++) {
810a56b9
MK
4682 if (allow_pagefault)
4683 page_kaddr = kmap(dst_page + i);
4684 else
4685 page_kaddr = kmap_atomic(dst_page + i);
fa4d75c1
MK
4686 rc = copy_from_user(page_kaddr,
4687 (const void __user *)(src + i * PAGE_SIZE),
4688 PAGE_SIZE);
810a56b9
MK
4689 if (allow_pagefault)
4690 kunmap(dst_page + i);
4691 else
4692 kunmap_atomic(page_kaddr);
fa4d75c1
MK
4693
4694 ret_val -= (PAGE_SIZE - rc);
4695 if (rc)
4696 break;
4697
4698 cond_resched();
4699 }
4700 return ret_val;
4701}
47ad8475 4702#endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
49076ec2 4703
40b64acd 4704#if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS
b35f1819
KS
4705
4706static struct kmem_cache *page_ptl_cachep;
4707
4708void __init ptlock_cache_init(void)
4709{
4710 page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0,
4711 SLAB_PANIC, NULL);
4712}
4713
539edb58 4714bool ptlock_alloc(struct page *page)
49076ec2
KS
4715{
4716 spinlock_t *ptl;
4717
b35f1819 4718 ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL);
49076ec2
KS
4719 if (!ptl)
4720 return false;
539edb58 4721 page->ptl = ptl;
49076ec2
KS
4722 return true;
4723}
4724
539edb58 4725void ptlock_free(struct page *page)
49076ec2 4726{
b35f1819 4727 kmem_cache_free(page_ptl_cachep, page->ptl);
49076ec2
KS
4728}
4729#endif