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