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