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