blob: 12f31501c3231813086c64102911b4f7404bdbb0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Hugh Dickins9a840892009-09-21 17:02:01 -070048#include <linux/ksm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/rmap.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040050#include <linux/export.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070051#include <linux/delayacct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/init.h>
Dan Williams01c8f1c2016-01-15 16:56:40 -080053#include <linux/pfn_t.h>
Peter Zijlstraedc79b22006-09-25 23:30:58 -070054#include <linux/writeback.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080055#include <linux/memcontrol.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070056#include <linux/mmu_notifier.h>
Hugh Dickins3dc14742009-01-06 14:40:08 -080057#include <linux/kallsyms.h>
58#include <linux/swapops.h>
59#include <linux/elf.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090060#include <linux/gfp.h>
Mel Gorman4daae3b2012-11-02 11:33:45 +000061#include <linux/migrate.h>
Andy Shevchenko2fbc57c2012-12-17 16:01:23 -080062#include <linux/string.h>
Dan Williams0abdd7a2014-01-21 15:48:12 -080063#include <linux/dma-debug.h>
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -070064#include <linux/debugfs.h>
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -070065#include <linux/userfaultfd_k.h>
Jan Karabc2466e2016-05-12 18:29:19 +020066#include <linux/dax.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Alexey Dobriyan6952b612009-09-18 23:55:55 +040068#include <asm/io.h>
Dave Hansen33a709b2016-02-12 13:02:19 -080069#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <asm/pgalloc.h>
71#include <asm/uaccess.h>
72#include <asm/tlb.h>
73#include <asm/tlbflush.h>
74#include <asm/pgtable.h>
75
Jan Beulich42b77722008-07-23 21:27:10 -070076#include "internal.h"
77
Peter Zijlstra90572892013-10-07 11:29:20 +010078#ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
79#warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
Peter Zijlstra75980e92013-02-22 16:34:32 -080080#endif
81
Andy Whitcroftd41dee32005-06-23 00:07:54 -070082#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* use the per-pgdat data instead for discontigmem - mbligh */
84unsigned long max_mapnr;
85struct page *mem_map;
86
87EXPORT_SYMBOL(max_mapnr);
88EXPORT_SYMBOL(mem_map);
89#endif
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100EXPORT_SYMBOL(high_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Ingo Molnar32a93232008-02-06 22:39:44 +0100102/*
103 * Randomize the address space (stacks, mmaps, brk, etc.).
104 *
105 * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
106 * as ancient (libc5 based) binaries can segfault. )
107 */
108int randomize_va_space __read_mostly =
109#ifdef CONFIG_COMPAT_BRK
110 1;
111#else
112 2;
113#endif
Andi Kleena62eaf12006-02-16 23:41:58 +0100114
115static int __init disable_randmaps(char *s)
116{
117 randomize_va_space = 0;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800118 return 1;
Andi Kleena62eaf12006-02-16 23:41:58 +0100119}
120__setup("norandmaps", disable_randmaps);
121
Hugh Dickins62eede62009-09-21 17:03:34 -0700122unsigned long zero_pfn __read_mostly;
Hugh Dickins03f64622009-09-21 17:03:35 -0700123unsigned long highest_memmap_pfn __read_mostly;
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -0700124
Ard Biesheuvel0b700682014-09-12 22:17:23 +0200125EXPORT_SYMBOL(zero_pfn);
126
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -0700127/*
128 * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
129 */
130static int __init init_zero_pfn(void)
131{
132 zero_pfn = page_to_pfn(ZERO_PAGE(0));
133 return 0;
134}
135core_initcall(init_zero_pfn);
Andi Kleena62eaf12006-02-16 23:41:58 +0100136
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800137
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800138#if defined(SPLIT_RSS_COUNTING)
139
David Rientjesea48cf72012-03-21 16:34:13 -0700140void sync_mm_rss(struct mm_struct *mm)
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800141{
142 int i;
143
144 for (i = 0; i < NR_MM_COUNTERS; i++) {
David Rientjes05af2e12012-03-21 16:34:13 -0700145 if (current->rss_stat.count[i]) {
146 add_mm_counter(mm, i, current->rss_stat.count[i]);
147 current->rss_stat.count[i] = 0;
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800148 }
149 }
David Rientjes05af2e12012-03-21 16:34:13 -0700150 current->rss_stat.events = 0;
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800151}
152
153static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
154{
155 struct task_struct *task = current;
156
157 if (likely(task->mm == mm))
158 task->rss_stat.count[member] += val;
159 else
160 add_mm_counter(mm, member, val);
161}
162#define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
163#define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
164
165/* sync counter once per 64 page faults */
166#define TASK_RSS_EVENTS_THRESH (64)
167static void check_sync_rss_stat(struct task_struct *task)
168{
169 if (unlikely(task != current))
170 return;
171 if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
David Rientjesea48cf72012-03-21 16:34:13 -0700172 sync_mm_rss(task->mm);
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800173}
Peter Zijlstra9547d012011-05-24 17:12:14 -0700174#else /* SPLIT_RSS_COUNTING */
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800175
176#define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
177#define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
178
179static void check_sync_rss_stat(struct task_struct *task)
180{
181}
182
Peter Zijlstra9547d012011-05-24 17:12:14 -0700183#endif /* SPLIT_RSS_COUNTING */
184
185#ifdef HAVE_GENERIC_MMU_GATHER
186
Nicholas Krauseca1d6c72015-09-04 15:48:22 -0700187static bool tlb_next_batch(struct mmu_gather *tlb)
Peter Zijlstra9547d012011-05-24 17:12:14 -0700188{
189 struct mmu_gather_batch *batch;
190
191 batch = tlb->active;
192 if (batch->next) {
193 tlb->active = batch->next;
Nicholas Krauseca1d6c72015-09-04 15:48:22 -0700194 return true;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700195 }
196
Michal Hocko53a59fc2013-01-04 15:35:12 -0800197 if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
Nicholas Krauseca1d6c72015-09-04 15:48:22 -0700198 return false;
Michal Hocko53a59fc2013-01-04 15:35:12 -0800199
Peter Zijlstra9547d012011-05-24 17:12:14 -0700200 batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
201 if (!batch)
Nicholas Krauseca1d6c72015-09-04 15:48:22 -0700202 return false;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700203
Michal Hocko53a59fc2013-01-04 15:35:12 -0800204 tlb->batch_count++;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700205 batch->next = NULL;
206 batch->nr = 0;
207 batch->max = MAX_GATHER_BATCH;
208
209 tlb->active->next = batch;
210 tlb->active = batch;
211
Nicholas Krauseca1d6c72015-09-04 15:48:22 -0700212 return true;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700213}
214
215/* tlb_gather_mmu
216 * Called to initialize an (on-stack) mmu_gather structure for page-table
217 * tear-down from @mm. The @fullmm argument is used when @mm is without
218 * users and we're going to destroy the full address space (exit/execve).
219 */
Linus Torvalds2b047252013-08-15 11:42:25 -0700220void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
Peter Zijlstra9547d012011-05-24 17:12:14 -0700221{
222 tlb->mm = mm;
223
Linus Torvalds2b047252013-08-15 11:42:25 -0700224 /* Is it from 0 to ~0? */
225 tlb->fullmm = !(start | (end+1));
Dave Hansen1de14c32013-04-12 16:23:54 -0700226 tlb->need_flush_all = 0;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700227 tlb->local.next = NULL;
228 tlb->local.nr = 0;
229 tlb->local.max = ARRAY_SIZE(tlb->__pages);
230 tlb->active = &tlb->local;
Michal Hocko53a59fc2013-01-04 15:35:12 -0800231 tlb->batch_count = 0;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700232
233#ifdef CONFIG_HAVE_RCU_TABLE_FREE
234 tlb->batch = NULL;
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800235#endif
Will Deaconfb7332a2014-10-29 10:03:09 +0000236
237 __tlb_reset_range(tlb);
Peter Zijlstra9547d012011-05-24 17:12:14 -0700238}
239
Linus Torvalds1cf35d42014-04-25 16:05:40 -0700240static void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
Peter Zijlstra9547d012011-05-24 17:12:14 -0700241{
Will Deacon721c21c2015-01-12 19:10:55 +0000242 if (!tlb->end)
243 return;
244
Peter Zijlstra9547d012011-05-24 17:12:14 -0700245 tlb_flush(tlb);
Joerg Roedel34ee6452014-11-13 13:46:09 +1100246 mmu_notifier_invalidate_range(tlb->mm, tlb->start, tlb->end);
Peter Zijlstra9547d012011-05-24 17:12:14 -0700247#ifdef CONFIG_HAVE_RCU_TABLE_FREE
248 tlb_table_flush(tlb);
249#endif
Will Deaconfb7332a2014-10-29 10:03:09 +0000250 __tlb_reset_range(tlb);
Linus Torvalds1cf35d42014-04-25 16:05:40 -0700251}
252
253static void tlb_flush_mmu_free(struct mmu_gather *tlb)
254{
255 struct mmu_gather_batch *batch;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700256
Will Deacon721c21c2015-01-12 19:10:55 +0000257 for (batch = &tlb->local; batch && batch->nr; batch = batch->next) {
Peter Zijlstra9547d012011-05-24 17:12:14 -0700258 free_pages_and_swap_cache(batch->pages, batch->nr);
259 batch->nr = 0;
260 }
261 tlb->active = &tlb->local;
262}
263
Linus Torvalds1cf35d42014-04-25 16:05:40 -0700264void tlb_flush_mmu(struct mmu_gather *tlb)
265{
Linus Torvalds1cf35d42014-04-25 16:05:40 -0700266 tlb_flush_mmu_tlbonly(tlb);
267 tlb_flush_mmu_free(tlb);
268}
269
Peter Zijlstra9547d012011-05-24 17:12:14 -0700270/* tlb_finish_mmu
271 * Called at the end of the shootdown operation to free up any resources
272 * that were required.
273 */
274void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
275{
276 struct mmu_gather_batch *batch, *next;
277
278 tlb_flush_mmu(tlb);
279
280 /* keep the page table cache within bounds */
281 check_pgt_cache();
282
283 for (batch = tlb->local.next; batch; batch = next) {
284 next = batch->next;
285 free_pages((unsigned long)batch, 0);
286 }
287 tlb->local.next = NULL;
288}
289
290/* __tlb_remove_page
291 * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
292 * handling the additional races in SMP caused by other CPUs caching valid
293 * mappings in their TLBs. Returns the number of free page slots left.
294 * When out of page slots we must call tlb_flush_mmu().
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -0700295 *returns true if the caller should flush.
Peter Zijlstra9547d012011-05-24 17:12:14 -0700296 */
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -0700297bool __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
Peter Zijlstra9547d012011-05-24 17:12:14 -0700298{
299 struct mmu_gather_batch *batch;
300
Will Deaconfb7332a2014-10-29 10:03:09 +0000301 VM_BUG_ON(!tlb->end);
Peter Zijlstra9547d012011-05-24 17:12:14 -0700302
Peter Zijlstra9547d012011-05-24 17:12:14 -0700303 batch = tlb->active;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700304 if (batch->nr == batch->max) {
305 if (!tlb_next_batch(tlb))
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -0700306 return true;
Shaohua Li0b43c3aa2011-07-08 15:39:41 -0700307 batch = tlb->active;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700308 }
Sasha Levin309381fea2014-01-23 15:52:54 -0800309 VM_BUG_ON_PAGE(batch->nr > batch->max, page);
Peter Zijlstra9547d012011-05-24 17:12:14 -0700310
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -0700311 batch->pages[batch->nr++] = page;
312 return false;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700313}
314
315#endif /* HAVE_GENERIC_MMU_GATHER */
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800316
Peter Zijlstra26723912011-05-24 17:12:00 -0700317#ifdef CONFIG_HAVE_RCU_TABLE_FREE
318
319/*
320 * See the comment near struct mmu_table_batch.
321 */
322
323static void tlb_remove_table_smp_sync(void *arg)
324{
325 /* Simply deliver the interrupt */
326}
327
328static void tlb_remove_table_one(void *table)
329{
330 /*
331 * This isn't an RCU grace period and hence the page-tables cannot be
332 * assumed to be actually RCU-freed.
333 *
334 * It is however sufficient for software page-table walkers that rely on
335 * IRQ disabling. See the comment near struct mmu_table_batch.
336 */
337 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
338 __tlb_remove_table(table);
339}
340
341static void tlb_remove_table_rcu(struct rcu_head *head)
342{
343 struct mmu_table_batch *batch;
344 int i;
345
346 batch = container_of(head, struct mmu_table_batch, rcu);
347
348 for (i = 0; i < batch->nr; i++)
349 __tlb_remove_table(batch->tables[i]);
350
351 free_page((unsigned long)batch);
352}
353
354void tlb_table_flush(struct mmu_gather *tlb)
355{
356 struct mmu_table_batch **batch = &tlb->batch;
357
358 if (*batch) {
359 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
360 *batch = NULL;
361 }
362}
363
364void tlb_remove_table(struct mmu_gather *tlb, void *table)
365{
366 struct mmu_table_batch **batch = &tlb->batch;
367
Peter Zijlstra26723912011-05-24 17:12:00 -0700368 /*
369 * When there's less then two users of this mm there cannot be a
370 * concurrent page-table walk.
371 */
372 if (atomic_read(&tlb->mm->mm_users) < 2) {
373 __tlb_remove_table(table);
374 return;
375 }
376
377 if (*batch == NULL) {
378 *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
379 if (*batch == NULL) {
380 tlb_remove_table_one(table);
381 return;
382 }
383 (*batch)->nr = 0;
384 }
385 (*batch)->tables[(*batch)->nr++] = table;
386 if ((*batch)->nr == MAX_TABLE_BATCH)
387 tlb_table_flush(tlb);
388}
389
Peter Zijlstra9547d012011-05-24 17:12:14 -0700390#endif /* CONFIG_HAVE_RCU_TABLE_FREE */
Peter Zijlstra26723912011-05-24 17:12:00 -0700391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 * Note: this doesn't free the actual pages themselves. That
394 * has been handled earlier when unmapping all the memory regions.
395 */
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000396static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
397 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800399 pgtable_t token = pmd_pgtable(*pmd);
Hugh Dickinse0da3822005-04-19 13:29:15 -0700400 pmd_clear(pmd);
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000401 pte_free_tlb(tlb, token, addr);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800402 atomic_long_dec(&tlb->mm->nr_ptes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Hugh Dickinse0da3822005-04-19 13:29:15 -0700405static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
406 unsigned long addr, unsigned long end,
407 unsigned long floor, unsigned long ceiling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 pmd_t *pmd;
410 unsigned long next;
Hugh Dickinse0da3822005-04-19 13:29:15 -0700411 unsigned long start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Hugh Dickinse0da3822005-04-19 13:29:15 -0700413 start = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 pmd = pmd_offset(pud, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 do {
416 next = pmd_addr_end(addr, end);
417 if (pmd_none_or_clear_bad(pmd))
418 continue;
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000419 free_pte_range(tlb, pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 } while (pmd++, addr = next, addr != end);
421
Hugh Dickinse0da3822005-04-19 13:29:15 -0700422 start &= PUD_MASK;
423 if (start < floor)
424 return;
425 if (ceiling) {
426 ceiling &= PUD_MASK;
427 if (!ceiling)
428 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Hugh Dickinse0da3822005-04-19 13:29:15 -0700430 if (end - 1 > ceiling - 1)
431 return;
432
433 pmd = pmd_offset(pud, start);
434 pud_clear(pud);
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000435 pmd_free_tlb(tlb, pmd, start);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800436 mm_dec_nr_pmds(tlb->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Hugh Dickinse0da3822005-04-19 13:29:15 -0700439static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
440 unsigned long addr, unsigned long end,
441 unsigned long floor, unsigned long ceiling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 pud_t *pud;
444 unsigned long next;
Hugh Dickinse0da3822005-04-19 13:29:15 -0700445 unsigned long start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Hugh Dickinse0da3822005-04-19 13:29:15 -0700447 start = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 pud = pud_offset(pgd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 do {
450 next = pud_addr_end(addr, end);
451 if (pud_none_or_clear_bad(pud))
452 continue;
Hugh Dickinse0da3822005-04-19 13:29:15 -0700453 free_pmd_range(tlb, pud, addr, next, floor, ceiling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 } while (pud++, addr = next, addr != end);
455
Hugh Dickinse0da3822005-04-19 13:29:15 -0700456 start &= PGDIR_MASK;
457 if (start < floor)
458 return;
459 if (ceiling) {
460 ceiling &= PGDIR_MASK;
461 if (!ceiling)
462 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
Hugh Dickinse0da3822005-04-19 13:29:15 -0700464 if (end - 1 > ceiling - 1)
465 return;
466
467 pud = pud_offset(pgd, start);
468 pgd_clear(pgd);
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000469 pud_free_tlb(tlb, pud, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
472/*
Hugh Dickinse0da3822005-04-19 13:29:15 -0700473 * This function frees user-level page tables of a process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 */
Jan Beulich42b77722008-07-23 21:27:10 -0700475void free_pgd_range(struct mmu_gather *tlb,
Hugh Dickinse0da3822005-04-19 13:29:15 -0700476 unsigned long addr, unsigned long end,
477 unsigned long floor, unsigned long ceiling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 pgd_t *pgd;
480 unsigned long next;
481
Hugh Dickinse0da3822005-04-19 13:29:15 -0700482 /*
483 * The next few lines have given us lots of grief...
484 *
485 * Why are we testing PMD* at this top level? Because often
486 * there will be no work to do at all, and we'd prefer not to
487 * go all the way down to the bottom just to discover that.
488 *
489 * Why all these "- 1"s? Because 0 represents both the bottom
490 * of the address space and the top of it (using -1 for the
491 * top wouldn't help much: the masks would do the wrong thing).
492 * The rule is that addr 0 and floor 0 refer to the bottom of
493 * the address space, but end 0 and ceiling 0 refer to the top
494 * Comparisons need to use "end - 1" and "ceiling - 1" (though
495 * that end 0 case should be mythical).
496 *
497 * Wherever addr is brought up or ceiling brought down, we must
498 * be careful to reject "the opposite 0" before it confuses the
499 * subsequent tests. But what about where end is brought down
500 * by PMD_SIZE below? no, end can't go down to 0 there.
501 *
502 * Whereas we round start (addr) and ceiling down, by different
503 * masks at different levels, in order to test whether a table
504 * now has no other vmas using it, so can be freed, we don't
505 * bother to round floor or end up - the tests don't need that.
506 */
507
508 addr &= PMD_MASK;
509 if (addr < floor) {
510 addr += PMD_SIZE;
511 if (!addr)
512 return;
513 }
514 if (ceiling) {
515 ceiling &= PMD_MASK;
516 if (!ceiling)
517 return;
518 }
519 if (end - 1 > ceiling - 1)
520 end -= PMD_SIZE;
521 if (addr > end - 1)
522 return;
523
Jan Beulich42b77722008-07-23 21:27:10 -0700524 pgd = pgd_offset(tlb->mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 do {
526 next = pgd_addr_end(addr, end);
527 if (pgd_none_or_clear_bad(pgd))
528 continue;
Jan Beulich42b77722008-07-23 21:27:10 -0700529 free_pud_range(tlb, pgd, addr, next, floor, ceiling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 } while (pgd++, addr = next, addr != end);
Hugh Dickinse0da3822005-04-19 13:29:15 -0700531}
532
Jan Beulich42b77722008-07-23 21:27:10 -0700533void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700534 unsigned long floor, unsigned long ceiling)
Hugh Dickinse0da3822005-04-19 13:29:15 -0700535{
536 while (vma) {
537 struct vm_area_struct *next = vma->vm_next;
538 unsigned long addr = vma->vm_start;
539
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700540 /*
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +1000541 * Hide vma from rmap and truncate_pagecache before freeing
542 * pgtables
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700543 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800544 unlink_anon_vmas(vma);
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700545 unlink_file_vma(vma);
546
David Gibson9da61ae2006-03-22 00:08:57 -0800547 if (is_vm_hugetlb_page(vma)) {
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700548 hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
Hugh Dickinse0da3822005-04-19 13:29:15 -0700549 floor, next? next->vm_start: ceiling);
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700550 } else {
551 /*
552 * Optimization: gather nearby vmas into one call down
553 */
554 while (next && next->vm_start <= vma->vm_end + PMD_SIZE
David Gibson48669202006-03-22 00:08:58 -0800555 && !is_vm_hugetlb_page(next)) {
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700556 vma = next;
557 next = vma->vm_next;
Rik van Riel5beb4932010-03-05 13:42:07 -0800558 unlink_anon_vmas(vma);
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700559 unlink_file_vma(vma);
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700560 }
561 free_pgd_range(tlb, addr, vma->vm_end,
562 floor, next? next->vm_start: ceiling);
563 }
Hugh Dickinse0da3822005-04-19 13:29:15 -0700564 vma = next;
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Kirill A. Shutemov3ed3a4f2016-03-17 14:19:11 -0700568int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800570 spinlock_t *ptl;
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800571 pgtable_t new = pte_alloc_one(mm, address);
Hugh Dickins1bb36302005-10-29 18:16:22 -0700572 if (!new)
573 return -ENOMEM;
574
Nick Piggin362a61a2008-05-14 06:37:36 +0200575 /*
576 * Ensure all pte setup (eg. pte page lock and page clearing) are
577 * visible before the pte is made visible to other CPUs by being
578 * put into page tables.
579 *
580 * The other side of the story is the pointer chasing in the page
581 * table walking code (when walking the page table without locking;
582 * ie. most of the time). Fortunately, these data accesses consist
583 * of a chain of data-dependent loads, meaning most CPUs (alpha
584 * being the notable exception) will already guarantee loads are
585 * seen in-order. See the alpha page table accessors for the
586 * smp_read_barrier_depends() barriers in page table walking code.
587 */
588 smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
589
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800590 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli8ac1f832011-01-13 15:46:43 -0800591 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800592 atomic_long_inc(&mm->nr_ptes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 pmd_populate(mm, pmd, new);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800594 new = NULL;
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -0800595 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800596 spin_unlock(ptl);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800597 if (new)
598 pte_free(mm, new);
Hugh Dickins1bb36302005-10-29 18:16:22 -0700599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Hugh Dickins1bb36302005-10-29 18:16:22 -0700602int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Hugh Dickins1bb36302005-10-29 18:16:22 -0700604 pte_t *new = pte_alloc_one_kernel(&init_mm, address);
605 if (!new)
606 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Nick Piggin362a61a2008-05-14 06:37:36 +0200608 smp_wmb(); /* See comment in __pte_alloc */
609
Hugh Dickins1bb36302005-10-29 18:16:22 -0700610 spin_lock(&init_mm.page_table_lock);
Andrea Arcangeli8ac1f832011-01-13 15:46:43 -0800611 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
Hugh Dickins1bb36302005-10-29 18:16:22 -0700612 pmd_populate_kernel(&init_mm, pmd, new);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800613 new = NULL;
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -0800614 }
Hugh Dickins1bb36302005-10-29 18:16:22 -0700615 spin_unlock(&init_mm.page_table_lock);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800616 if (new)
617 pte_free_kernel(&init_mm, new);
Hugh Dickins1bb36302005-10-29 18:16:22 -0700618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800621static inline void init_rss_vec(int *rss)
Hugh Dickinsae859762005-10-29 18:16:05 -0700622{
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800623 memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
624}
625
626static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
627{
628 int i;
629
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800630 if (current->mm == mm)
David Rientjes05af2e12012-03-21 16:34:13 -0700631 sync_mm_rss(mm);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800632 for (i = 0; i < NR_MM_COUNTERS; i++)
633 if (rss[i])
634 add_mm_counter(mm, i, rss[i]);
Hugh Dickinsae859762005-10-29 18:16:05 -0700635}
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637/*
Linus Torvalds6aab3412005-11-28 14:34:23 -0800638 * This function is called to print an error when a bad pte
639 * is found. For example, we might have a PFN-mapped pte in
640 * a region that doesn't allow it.
Nick Pigginb5810032005-10-29 18:16:12 -0700641 *
642 * The calling function must still handle the error.
643 */
Hugh Dickins3dc14742009-01-06 14:40:08 -0800644static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
645 pte_t pte, struct page *page)
Nick Pigginb5810032005-10-29 18:16:12 -0700646{
Hugh Dickins3dc14742009-01-06 14:40:08 -0800647 pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
648 pud_t *pud = pud_offset(pgd, addr);
649 pmd_t *pmd = pmd_offset(pud, addr);
650 struct address_space *mapping;
651 pgoff_t index;
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800652 static unsigned long resume;
653 static unsigned long nr_shown;
654 static unsigned long nr_unshown;
655
656 /*
657 * Allow a burst of 60 reports, then keep quiet for that minute;
658 * or allow a steady drip of one report per second.
659 */
660 if (nr_shown == 60) {
661 if (time_before(jiffies, resume)) {
662 nr_unshown++;
663 return;
664 }
665 if (nr_unshown) {
Joe Perches11705322016-03-17 14:19:50 -0700666 pr_alert("BUG: Bad page map: %lu messages suppressed\n",
667 nr_unshown);
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800668 nr_unshown = 0;
669 }
670 nr_shown = 0;
671 }
672 if (nr_shown++ == 0)
673 resume = jiffies + 60 * HZ;
Hugh Dickins3dc14742009-01-06 14:40:08 -0800674
675 mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
676 index = linear_page_index(vma, addr);
677
Joe Perches11705322016-03-17 14:19:50 -0700678 pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n",
679 current->comm,
680 (long long)pte_val(pte), (long long)pmd_val(*pmd));
Wu Fengguang718a3822010-03-10 15:20:43 -0800681 if (page)
Dave Hansenf0b791a2014-01-23 15:52:49 -0800682 dump_page(page, "bad pte");
Joe Perches11705322016-03-17 14:19:50 -0700683 pr_alert("addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n",
684 (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
Hugh Dickins3dc14742009-01-06 14:40:08 -0800685 /*
686 * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y
687 */
Konstantin Khlebnikov26825822015-04-15 16:15:08 -0700688 pr_alert("file:%pD fault:%pf mmap:%pf readpage:%pf\n",
689 vma->vm_file,
690 vma->vm_ops ? vma->vm_ops->fault : NULL,
691 vma->vm_file ? vma->vm_file->f_op->mmap : NULL,
692 mapping ? mapping->a_ops->readpage : NULL);
Nick Pigginb5810032005-10-29 18:16:12 -0700693 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +1030694 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Nick Pigginb5810032005-10-29 18:16:12 -0700695}
696
697/*
Nick Piggin7e675132008-04-28 02:13:00 -0700698 * vm_normal_page -- This function gets the "struct page" associated with a pte.
Linus Torvalds6aab3412005-11-28 14:34:23 -0800699 *
Nick Piggin7e675132008-04-28 02:13:00 -0700700 * "Special" mappings do not wish to be associated with a "struct page" (either
701 * it doesn't exist, or it exists but they don't want to touch it). In this
702 * case, NULL is returned here. "Normal" mappings do have a struct page.
Jared Hulbertb379d792008-04-28 02:12:58 -0700703 *
Nick Piggin7e675132008-04-28 02:13:00 -0700704 * There are 2 broad cases. Firstly, an architecture may define a pte_special()
705 * pte bit, in which case this function is trivial. Secondly, an architecture
706 * may not have a spare pte bit, which requires a more complicated scheme,
707 * described below.
708 *
709 * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
710 * special mapping (even if there are underlying and valid "struct pages").
711 * COWed pages of a VM_PFNMAP are always normal.
Linus Torvalds6aab3412005-11-28 14:34:23 -0800712 *
Jared Hulbertb379d792008-04-28 02:12:58 -0700713 * The way we recognize COWed pages within VM_PFNMAP mappings is through the
714 * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
Nick Piggin7e675132008-04-28 02:13:00 -0700715 * set, and the vm_pgoff will point to the first PFN mapped: thus every special
716 * mapping will always honor the rule
Linus Torvalds6aab3412005-11-28 14:34:23 -0800717 *
718 * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
719 *
Nick Piggin7e675132008-04-28 02:13:00 -0700720 * And for normal mappings this is false.
Jared Hulbertb379d792008-04-28 02:12:58 -0700721 *
Nick Piggin7e675132008-04-28 02:13:00 -0700722 * This restricts such mappings to be a linear translation from virtual address
723 * to pfn. To get around this restriction, we allow arbitrary mappings so long
724 * as the vma is not a COW mapping; in that case, we know that all ptes are
725 * special (because none can have been COWed).
Jared Hulbertb379d792008-04-28 02:12:58 -0700726 *
727 *
Nick Piggin7e675132008-04-28 02:13:00 -0700728 * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
729 *
Jared Hulbertb379d792008-04-28 02:12:58 -0700730 * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
731 * page" backing, however the difference is that _all_ pages with a struct
732 * page (that is, those where pfn_valid is true) are refcounted and considered
733 * normal pages by the VM. The disadvantage is that pages are refcounted
734 * (which can be slower and simply not an option for some PFNMAP users). The
735 * advantage is that we don't have to follow the strict linearity rule of
736 * PFNMAP mappings in order to support COWable mappings.
737 *
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800738 */
Nick Piggin7e675132008-04-28 02:13:00 -0700739#ifdef __HAVE_ARCH_PTE_SPECIAL
740# define HAVE_PTE_SPECIAL 1
741#else
742# define HAVE_PTE_SPECIAL 0
743#endif
744struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
745 pte_t pte)
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800746{
Hugh Dickins22b31ee2009-01-06 14:40:09 -0800747 unsigned long pfn = pte_pfn(pte);
Nick Piggin7e675132008-04-28 02:13:00 -0700748
749 if (HAVE_PTE_SPECIAL) {
Hugh Dickinsb38af472014-08-29 15:18:44 -0700750 if (likely(!pte_special(pte)))
Hugh Dickins22b31ee2009-01-06 14:40:09 -0800751 goto check_pfn;
David Vrabel667a0a02014-12-18 14:48:15 +0000752 if (vma->vm_ops && vma->vm_ops->find_special_page)
753 return vma->vm_ops->find_special_page(vma, addr);
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -0700754 if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
755 return NULL;
Hugh Dickins62eede62009-09-21 17:03:34 -0700756 if (!is_zero_pfn(pfn))
Hugh Dickins22b31ee2009-01-06 14:40:09 -0800757 print_bad_pte(vma, addr, pte, NULL);
Nick Piggin7e675132008-04-28 02:13:00 -0700758 return NULL;
759 }
760
761 /* !HAVE_PTE_SPECIAL case follows: */
762
Jared Hulbertb379d792008-04-28 02:12:58 -0700763 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
764 if (vma->vm_flags & VM_MIXEDMAP) {
765 if (!pfn_valid(pfn))
766 return NULL;
767 goto out;
768 } else {
Nick Piggin7e675132008-04-28 02:13:00 -0700769 unsigned long off;
770 off = (addr - vma->vm_start) >> PAGE_SHIFT;
Jared Hulbertb379d792008-04-28 02:12:58 -0700771 if (pfn == vma->vm_pgoff + off)
772 return NULL;
773 if (!is_cow_mapping(vma->vm_flags))
774 return NULL;
775 }
Linus Torvalds6aab3412005-11-28 14:34:23 -0800776 }
777
Hugh Dickinsb38af472014-08-29 15:18:44 -0700778 if (is_zero_pfn(pfn))
779 return NULL;
Hugh Dickins22b31ee2009-01-06 14:40:09 -0800780check_pfn:
781 if (unlikely(pfn > highest_memmap_pfn)) {
782 print_bad_pte(vma, addr, pte, NULL);
783 return NULL;
784 }
Linus Torvalds6aab3412005-11-28 14:34:23 -0800785
786 /*
Nick Piggin7e675132008-04-28 02:13:00 -0700787 * NOTE! We still have PageReserved() pages in the page tables.
Nick Piggin7e675132008-04-28 02:13:00 -0700788 * eg. VDSO mappings can cause them to exist.
Linus Torvalds6aab3412005-11-28 14:34:23 -0800789 */
Jared Hulbertb379d792008-04-28 02:12:58 -0700790out:
Linus Torvalds6aab3412005-11-28 14:34:23 -0800791 return pfn_to_page(pfn);
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800792}
793
Gerald Schaefer28093f92016-04-28 16:18:35 -0700794#ifdef CONFIG_TRANSPARENT_HUGEPAGE
795struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
796 pmd_t pmd)
797{
798 unsigned long pfn = pmd_pfn(pmd);
799
800 /*
801 * There is no pmd_special() but there may be special pmds, e.g.
802 * in a direct-access (dax) mapping, so let's just replicate the
803 * !HAVE_PTE_SPECIAL case from vm_normal_page() here.
804 */
805 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
806 if (vma->vm_flags & VM_MIXEDMAP) {
807 if (!pfn_valid(pfn))
808 return NULL;
809 goto out;
810 } else {
811 unsigned long off;
812 off = (addr - vma->vm_start) >> PAGE_SHIFT;
813 if (pfn == vma->vm_pgoff + off)
814 return NULL;
815 if (!is_cow_mapping(vma->vm_flags))
816 return NULL;
817 }
818 }
819
820 if (is_zero_pfn(pfn))
821 return NULL;
822 if (unlikely(pfn > highest_memmap_pfn))
823 return NULL;
824
825 /*
826 * NOTE! We still have PageReserved() pages in the page tables.
827 * eg. VDSO mappings can cause them to exist.
828 */
829out:
830 return pfn_to_page(pfn);
831}
832#endif
833
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800834/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 * copy one vm_area from one task to the other. Assumes the page tables
836 * already present in the new task to be cleared in the whole range
837 * covered by this vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 */
839
Hugh Dickins570a335b2009-12-14 17:58:46 -0800840static inline unsigned long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
Nick Pigginb5810032005-10-29 18:16:12 -0700842 pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
Hugh Dickins8c103762005-10-29 18:16:13 -0700843 unsigned long addr, int *rss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Nick Pigginb5810032005-10-29 18:16:12 -0700845 unsigned long vm_flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 pte_t pte = *src_pte;
847 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /* pte contains position in swap or file, so copy. */
850 if (unlikely(!pte_present(pte))) {
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800851 swp_entry_t entry = pte_to_swp_entry(pte);
Christoph Lameter06972122006-06-23 02:03:35 -0700852
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800853 if (likely(!non_swap_entry(entry))) {
854 if (swap_duplicate(entry) < 0)
855 return entry.val;
Hugh Dickins570a335b2009-12-14 17:58:46 -0800856
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800857 /* make sure dst_mm is on swapoff's mmlist. */
858 if (unlikely(list_empty(&dst_mm->mmlist))) {
859 spin_lock(&mmlist_lock);
860 if (list_empty(&dst_mm->mmlist))
861 list_add(&dst_mm->mmlist,
862 &src_mm->mmlist);
863 spin_unlock(&mmlist_lock);
864 }
865 rss[MM_SWAPENTS]++;
866 } else if (is_migration_entry(entry)) {
867 page = migration_entry_to_page(entry);
Konstantin Khlebnikov9f9f1ac2012-01-20 14:34:24 -0800868
Jerome Marchandeca56ff2016-01-14 15:19:26 -0800869 rss[mm_counter(page)]++;
Konstantin Khlebnikov9f9f1ac2012-01-20 14:34:24 -0800870
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800871 if (is_write_migration_entry(entry) &&
872 is_cow_mapping(vm_flags)) {
873 /*
874 * COW mappings require pages in both
875 * parent and child to be set to read.
876 */
877 make_migration_entry_read(&entry);
878 pte = swp_entry_to_pte(entry);
879 if (pte_swp_soft_dirty(*src_pte))
880 pte = pte_swp_mksoft_dirty(pte);
881 set_pte_at(src_mm, addr, src_pte, pte);
Christoph Lameter06972122006-06-23 02:03:35 -0700882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
Hugh Dickinsae859762005-10-29 18:16:05 -0700884 goto out_set_pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /*
888 * If it's a COW mapping, write protect it both
889 * in the parent and the child
890 */
Linus Torvalds67121172005-12-11 20:38:17 -0800891 if (is_cow_mapping(vm_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 ptep_set_wrprotect(src_mm, addr, src_pte);
Zachary Amsden3dc90792006-09-30 23:29:30 -0700893 pte = pte_wrprotect(pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895
896 /*
897 * If it's a shared mapping, mark it clean in
898 * the child
899 */
900 if (vm_flags & VM_SHARED)
901 pte = pte_mkclean(pte);
902 pte = pte_mkold(pte);
Linus Torvalds6aab3412005-11-28 14:34:23 -0800903
904 page = vm_normal_page(vma, addr, pte);
905 if (page) {
906 get_page(page);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -0800907 page_dup_rmap(page, false);
Jerome Marchandeca56ff2016-01-14 15:19:26 -0800908 rss[mm_counter(page)]++;
Linus Torvalds6aab3412005-11-28 14:34:23 -0800909 }
Hugh Dickinsae859762005-10-29 18:16:05 -0700910
911out_set_pte:
912 set_pte_at(dst_mm, addr, dst_pte, pte);
Hugh Dickins570a335b2009-12-14 17:58:46 -0800913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Jerome Marchand21bda262014-08-06 16:06:56 -0700916static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800917 pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
918 unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Daisuke Nishimurac36987e2009-10-26 16:50:23 -0700920 pte_t *orig_src_pte, *orig_dst_pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 pte_t *src_pte, *dst_pte;
Hugh Dickinsc74df322005-10-29 18:16:23 -0700922 spinlock_t *src_ptl, *dst_ptl;
Hugh Dickinse040f212005-10-29 18:15:53 -0700923 int progress = 0;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800924 int rss[NR_MM_COUNTERS];
Hugh Dickins570a335b2009-12-14 17:58:46 -0800925 swp_entry_t entry = (swp_entry_t){0};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927again:
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800928 init_rss_vec(rss);
929
Hugh Dickinsc74df322005-10-29 18:16:23 -0700930 dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (!dst_pte)
932 return -ENOMEM;
Peter Zijlstraece0e2b2010-10-26 14:21:52 -0700933 src_pte = pte_offset_map(src_pmd, addr);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700934 src_ptl = pte_lockptr(src_mm, src_pmd);
Ingo Molnarf20dc5f2006-07-03 00:25:08 -0700935 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Daisuke Nishimurac36987e2009-10-26 16:50:23 -0700936 orig_src_pte = src_pte;
937 orig_dst_pte = dst_pte;
Zachary Amsden6606c3e2006-09-30 23:29:33 -0700938 arch_enter_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 do {
941 /*
942 * We are holding two locks at this point - either of them
943 * could generate latencies in another task on another CPU.
944 */
Hugh Dickinse040f212005-10-29 18:15:53 -0700945 if (progress >= 32) {
946 progress = 0;
947 if (need_resched() ||
Nick Piggin95c354f2008-01-30 13:31:20 +0100948 spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
Hugh Dickinse040f212005-10-29 18:15:53 -0700949 break;
950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (pte_none(*src_pte)) {
952 progress++;
953 continue;
954 }
Hugh Dickins570a335b2009-12-14 17:58:46 -0800955 entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
956 vma, addr, rss);
957 if (entry.val)
958 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 progress += 8;
960 } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Zachary Amsden6606c3e2006-09-30 23:29:33 -0700962 arch_leave_lazy_mmu_mode();
Hugh Dickinsc74df322005-10-29 18:16:23 -0700963 spin_unlock(src_ptl);
Peter Zijlstraece0e2b2010-10-26 14:21:52 -0700964 pte_unmap(orig_src_pte);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800965 add_mm_rss_vec(dst_mm, rss);
Daisuke Nishimurac36987e2009-10-26 16:50:23 -0700966 pte_unmap_unlock(orig_dst_pte, dst_ptl);
Hugh Dickinsc74df322005-10-29 18:16:23 -0700967 cond_resched();
Hugh Dickins570a335b2009-12-14 17:58:46 -0800968
969 if (entry.val) {
970 if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
971 return -ENOMEM;
972 progress = 0;
973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (addr != end)
975 goto again;
976 return 0;
977}
978
979static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
980 pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
981 unsigned long addr, unsigned long end)
982{
983 pmd_t *src_pmd, *dst_pmd;
984 unsigned long next;
985
986 dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
987 if (!dst_pmd)
988 return -ENOMEM;
989 src_pmd = pmd_offset(src_pud, addr);
990 do {
991 next = pmd_addr_end(addr, end);
Dan Williams5c7fb562016-01-15 16:56:52 -0800992 if (pmd_trans_huge(*src_pmd) || pmd_devmap(*src_pmd)) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800993 int err;
Andrea Arcangeli14d1a552011-01-13 15:47:15 -0800994 VM_BUG_ON(next-addr != HPAGE_PMD_SIZE);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800995 err = copy_huge_pmd(dst_mm, src_mm,
996 dst_pmd, src_pmd, addr, vma);
997 if (err == -ENOMEM)
998 return -ENOMEM;
999 if (!err)
1000 continue;
1001 /* fall through */
1002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (pmd_none_or_clear_bad(src_pmd))
1004 continue;
1005 if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
1006 vma, addr, next))
1007 return -ENOMEM;
1008 } while (dst_pmd++, src_pmd++, addr = next, addr != end);
1009 return 0;
1010}
1011
1012static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1013 pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
1014 unsigned long addr, unsigned long end)
1015{
1016 pud_t *src_pud, *dst_pud;
1017 unsigned long next;
1018
1019 dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
1020 if (!dst_pud)
1021 return -ENOMEM;
1022 src_pud = pud_offset(src_pgd, addr);
1023 do {
1024 next = pud_addr_end(addr, end);
1025 if (pud_none_or_clear_bad(src_pud))
1026 continue;
1027 if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
1028 vma, addr, next))
1029 return -ENOMEM;
1030 } while (dst_pud++, src_pud++, addr = next, addr != end);
1031 return 0;
1032}
1033
1034int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1035 struct vm_area_struct *vma)
1036{
1037 pgd_t *src_pgd, *dst_pgd;
1038 unsigned long next;
1039 unsigned long addr = vma->vm_start;
1040 unsigned long end = vma->vm_end;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001041 unsigned long mmun_start; /* For mmu_notifiers */
1042 unsigned long mmun_end; /* For mmu_notifiers */
1043 bool is_cow;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001044 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Nick Piggind9928952005-08-28 16:49:11 +10001046 /*
1047 * Don't copy ptes where a page fault will fill them correctly.
1048 * Fork becomes much lighter when there are big shared or private
1049 * readonly mappings. The tradeoff is that copy_page_range is more
1050 * efficient than faulting.
1051 */
Kirill A. Shutemov0661a332015-02-10 14:10:04 -08001052 if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) &&
1053 !vma->anon_vma)
1054 return 0;
Nick Piggind9928952005-08-28 16:49:11 +10001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (is_vm_hugetlb_page(vma))
1057 return copy_hugetlb_page_range(dst_mm, src_mm, vma);
1058
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001059 if (unlikely(vma->vm_flags & VM_PFNMAP)) {
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001060 /*
1061 * We do not free on error cases below as remove_vma
1062 * gets called on error from higher level routine
1063 */
Suresh Siddha5180da42012-10-08 16:28:29 -07001064 ret = track_pfn_copy(vma);
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001065 if (ret)
1066 return ret;
1067 }
1068
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001069 /*
1070 * We need to invalidate the secondary MMU mappings only when
1071 * there could be a permission downgrade on the ptes of the
1072 * parent mm. And a permission downgrade will only happen if
1073 * is_cow_mapping() returns true.
1074 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001075 is_cow = is_cow_mapping(vma->vm_flags);
1076 mmun_start = addr;
1077 mmun_end = end;
1078 if (is_cow)
1079 mmu_notifier_invalidate_range_start(src_mm, mmun_start,
1080 mmun_end);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001081
1082 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 dst_pgd = pgd_offset(dst_mm, addr);
1084 src_pgd = pgd_offset(src_mm, addr);
1085 do {
1086 next = pgd_addr_end(addr, end);
1087 if (pgd_none_or_clear_bad(src_pgd))
1088 continue;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001089 if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
1090 vma, addr, next))) {
1091 ret = -ENOMEM;
1092 break;
1093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 } while (dst_pgd++, src_pgd++, addr = next, addr != end);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001095
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001096 if (is_cow)
1097 mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001098 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
Robin Holt51c6f662005-11-13 16:06:42 -08001101static unsigned long zap_pte_range(struct mmu_gather *tlb,
Nick Pigginb5810032005-10-29 18:16:12 -07001102 struct vm_area_struct *vma, pmd_t *pmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 unsigned long addr, unsigned long end,
Peter Zijlstra97a89412011-05-24 17:12:04 -07001104 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
Nick Pigginb5810032005-10-29 18:16:12 -07001106 struct mm_struct *mm = tlb->mm;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001107 int force_flush = 0;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001108 int rss[NR_MM_COUNTERS];
Peter Zijlstra97a89412011-05-24 17:12:04 -07001109 spinlock_t *ptl;
Steven Rostedt5f1a1902011-06-15 15:08:23 -07001110 pte_t *start_pte;
Peter Zijlstra97a89412011-05-24 17:12:04 -07001111 pte_t *pte;
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001112 swp_entry_t entry;
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -07001113 struct page *pending_page = NULL;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001114
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001115again:
Peter Zijlstrae3032972011-05-24 17:12:01 -07001116 init_rss_vec(rss);
Steven Rostedt5f1a1902011-06-15 15:08:23 -07001117 start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
1118 pte = start_pte;
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001119 arch_enter_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 do {
1121 pte_t ptent = *pte;
Robin Holt51c6f662005-11-13 16:06:42 -08001122 if (pte_none(ptent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 continue;
Robin Holt51c6f662005-11-13 16:06:42 -08001124 }
Hugh Dickins6f5e6b92006-03-16 23:04:09 -08001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (pte_present(ptent)) {
Hugh Dickinsee498ed2005-11-21 21:32:18 -08001127 struct page *page;
Robin Holt51c6f662005-11-13 16:06:42 -08001128
Linus Torvalds6aab3412005-11-28 14:34:23 -08001129 page = vm_normal_page(vma, addr, ptent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (unlikely(details) && page) {
1131 /*
1132 * unmap_shared_mapping_pages() wants to
1133 * invalidate cache without truncating:
1134 * unmap shared but keep private pages.
1135 */
1136 if (details->check_mapping &&
1137 details->check_mapping != page->mapping)
1138 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
Nick Pigginb5810032005-10-29 18:16:12 -07001140 ptent = ptep_get_and_clear_full(mm, addr, pte,
Zachary Amsdena6003882005-09-03 15:55:04 -07001141 tlb->fullmm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 tlb_remove_tlb_entry(tlb, pte, addr);
1143 if (unlikely(!page))
1144 continue;
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001145
1146 if (!PageAnon(page)) {
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001147 if (pte_dirty(ptent)) {
Michal Hockoaac45362016-03-25 14:20:24 -07001148 /*
1149 * oom_reaper cannot tear down dirty
1150 * pages
1151 */
1152 if (unlikely(details && details->ignore_dirty))
1153 continue;
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001154 force_flush = 1;
Hugh Dickins6237bcd2005-10-29 18:15:54 -07001155 set_page_dirty(page);
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001156 }
Johannes Weiner4917e5d2009-01-06 14:39:17 -08001157 if (pte_young(ptent) &&
Joe Perches64363aa2013-07-08 16:00:18 -07001158 likely(!(vma->vm_flags & VM_SEQ_READ)))
Nick Pigginbf3f3bc2009-01-06 14:38:55 -08001159 mark_page_accessed(page);
Hugh Dickins6237bcd2005-10-29 18:15:54 -07001160 }
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001161 rss[mm_counter(page)]--;
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001162 page_remove_rmap(page, false);
Hugh Dickins3dc14742009-01-06 14:40:08 -08001163 if (unlikely(page_mapcount(page) < 0))
1164 print_bad_pte(vma, addr, ptent, page);
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -07001165 if (unlikely(__tlb_remove_page(tlb, page))) {
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001166 force_flush = 1;
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -07001167 pending_page = page;
Will Deaconce9ec372014-10-28 13:16:28 -07001168 addr += PAGE_SIZE;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001169 break;
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 continue;
1172 }
Michal Hockoaac45362016-03-25 14:20:24 -07001173 /* only check swap_entries if explicitly asked for in details */
1174 if (unlikely(details && !details->check_swap_entries))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 continue;
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -08001176
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001177 entry = pte_to_swp_entry(ptent);
1178 if (!non_swap_entry(entry))
1179 rss[MM_SWAPENTS]--;
1180 else if (is_migration_entry(entry)) {
1181 struct page *page;
Konstantin Khlebnikov9f9f1ac2012-01-20 14:34:24 -08001182
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001183 page = migration_entry_to_page(entry);
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001184 rss[mm_counter(page)]--;
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -08001185 }
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001186 if (unlikely(!free_swap_and_cache(entry)))
1187 print_bad_pte(vma, addr, ptent, NULL);
Zachary Amsden9888a1c2006-09-30 23:29:31 -07001188 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
Peter Zijlstra97a89412011-05-24 17:12:04 -07001189 } while (pte++, addr += PAGE_SIZE, addr != end);
Hugh Dickinsae859762005-10-29 18:16:05 -07001190
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001191 add_mm_rss_vec(mm, rss);
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001192 arch_leave_lazy_mmu_mode();
Robin Holt51c6f662005-11-13 16:06:42 -08001193
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001194 /* Do the actual TLB flush before dropping ptl */
Will Deaconfb7332a2014-10-29 10:03:09 +00001195 if (force_flush)
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001196 tlb_flush_mmu_tlbonly(tlb);
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001197 pte_unmap_unlock(start_pte, ptl);
1198
1199 /*
1200 * If we forced a TLB flush (either due to running out of
1201 * batch buffers or because we needed to flush dirty TLB
1202 * entries before releasing the ptl), free the batched
1203 * memory too. Restart if we didn't do everything.
1204 */
1205 if (force_flush) {
1206 force_flush = 0;
1207 tlb_flush_mmu_free(tlb);
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -07001208 if (pending_page) {
1209 /* remove the page with new size */
1210 __tlb_remove_pte_page(tlb, pending_page);
1211 pending_page = NULL;
1212 }
Linus Torvalds2b047252013-08-15 11:42:25 -07001213 if (addr != end)
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001214 goto again;
1215 }
1216
Robin Holt51c6f662005-11-13 16:06:42 -08001217 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Robin Holt51c6f662005-11-13 16:06:42 -08001220static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
Nick Pigginb5810032005-10-29 18:16:12 -07001221 struct vm_area_struct *vma, pud_t *pud,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 unsigned long addr, unsigned long end,
Peter Zijlstra97a89412011-05-24 17:12:04 -07001223 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 pmd_t *pmd;
1226 unsigned long next;
1227
1228 pmd = pmd_offset(pud, addr);
1229 do {
1230 next = pmd_addr_end(addr, end);
Dan Williams5c7fb562016-01-15 16:56:52 -08001231 if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001232 if (next - addr != HPAGE_PMD_SIZE) {
Hugh Dickins68428392016-05-05 16:22:09 -07001233 VM_BUG_ON_VMA(vma_is_anonymous(vma) &&
1234 !rwsem_is_locked(&tlb->mm->mmap_sem), vma);
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001235 split_huge_pmd(vma, pmd, addr);
Shaohua Lif21760b2012-01-12 17:19:16 -08001236 } else if (zap_huge_pmd(tlb, vma, pmd, addr))
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001237 goto next;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001238 /* fall through */
1239 }
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001240 /*
1241 * Here there can be other concurrent MADV_DONTNEED or
1242 * trans huge page faults running, and if the pmd is
1243 * none or trans huge it can change under us. This is
1244 * because MADV_DONTNEED holds the mmap_sem in read
1245 * mode.
1246 */
1247 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
1248 goto next;
Peter Zijlstra97a89412011-05-24 17:12:04 -07001249 next = zap_pte_range(tlb, vma, pmd, addr, next, details);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001250next:
Peter Zijlstra97a89412011-05-24 17:12:04 -07001251 cond_resched();
1252 } while (pmd++, addr = next, addr != end);
Robin Holt51c6f662005-11-13 16:06:42 -08001253
1254 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
Robin Holt51c6f662005-11-13 16:06:42 -08001257static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
Nick Pigginb5810032005-10-29 18:16:12 -07001258 struct vm_area_struct *vma, pgd_t *pgd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 unsigned long addr, unsigned long end,
Peter Zijlstra97a89412011-05-24 17:12:04 -07001260 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 pud_t *pud;
1263 unsigned long next;
1264
1265 pud = pud_offset(pgd, addr);
1266 do {
1267 next = pud_addr_end(addr, end);
Peter Zijlstra97a89412011-05-24 17:12:04 -07001268 if (pud_none_or_clear_bad(pud))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 continue;
Peter Zijlstra97a89412011-05-24 17:12:04 -07001270 next = zap_pmd_range(tlb, vma, pud, addr, next, details);
1271 } while (pud++, addr = next, addr != end);
Robin Holt51c6f662005-11-13 16:06:42 -08001272
1273 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
Michal Hockoaac45362016-03-25 14:20:24 -07001276void unmap_page_range(struct mmu_gather *tlb,
Al Viro038c7aa2012-03-05 13:25:09 -05001277 struct vm_area_struct *vma,
1278 unsigned long addr, unsigned long end,
1279 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 pgd_t *pgd;
1282 unsigned long next;
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 BUG_ON(addr >= end);
1285 tlb_start_vma(tlb, vma);
1286 pgd = pgd_offset(vma->vm_mm, addr);
1287 do {
1288 next = pgd_addr_end(addr, end);
Peter Zijlstra97a89412011-05-24 17:12:04 -07001289 if (pgd_none_or_clear_bad(pgd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 continue;
Peter Zijlstra97a89412011-05-24 17:12:04 -07001291 next = zap_pud_range(tlb, vma, pgd, addr, next, details);
1292 } while (pgd++, addr = next, addr != end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 tlb_end_vma(tlb, vma);
1294}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Al Virof5cc4ee2012-03-05 14:14:20 -05001296
1297static void unmap_single_vma(struct mmu_gather *tlb,
1298 struct vm_area_struct *vma, unsigned long start_addr,
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001299 unsigned long end_addr,
Al Virof5cc4ee2012-03-05 14:14:20 -05001300 struct zap_details *details)
1301{
1302 unsigned long start = max(vma->vm_start, start_addr);
1303 unsigned long end;
1304
1305 if (start >= vma->vm_end)
1306 return;
1307 end = min(vma->vm_end, end_addr);
1308 if (end <= vma->vm_start)
1309 return;
1310
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301311 if (vma->vm_file)
1312 uprobe_munmap(vma, start, end);
1313
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001314 if (unlikely(vma->vm_flags & VM_PFNMAP))
Suresh Siddha5180da42012-10-08 16:28:29 -07001315 untrack_pfn(vma, 0, 0);
Al Virof5cc4ee2012-03-05 14:14:20 -05001316
1317 if (start != end) {
1318 if (unlikely(is_vm_hugetlb_page(vma))) {
1319 /*
1320 * It is undesirable to test vma->vm_file as it
1321 * should be non-null for valid hugetlb area.
1322 * However, vm_file will be NULL in the error
Davidlohr Bueso7aa6b4a2014-04-07 15:37:01 -07001323 * cleanup path of mmap_region. When
Al Virof5cc4ee2012-03-05 14:14:20 -05001324 * hugetlbfs ->mmap method fails,
Davidlohr Bueso7aa6b4a2014-04-07 15:37:01 -07001325 * mmap_region() nullifies vma->vm_file
Al Virof5cc4ee2012-03-05 14:14:20 -05001326 * before calling this function to clean up.
1327 * Since no pte has actually been setup, it is
1328 * safe to do nothing in this case.
1329 */
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07001330 if (vma->vm_file) {
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08001331 i_mmap_lock_write(vma->vm_file->f_mapping);
Mel Gormand8333522012-07-31 16:46:20 -07001332 __unmap_hugepage_range_final(tlb, vma, start, end, NULL);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08001333 i_mmap_unlock_write(vma->vm_file->f_mapping);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07001334 }
Al Virof5cc4ee2012-03-05 14:14:20 -05001335 } else
1336 unmap_page_range(tlb, vma, start, end, details);
1337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340/**
1341 * unmap_vmas - unmap a range of memory covered by a list of vma's
Randy Dunlap0164f692011-06-15 15:08:09 -07001342 * @tlb: address of the caller's struct mmu_gather
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 * @vma: the starting vma
1344 * @start_addr: virtual address at which to start unmapping
1345 * @end_addr: virtual address at which to end unmapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 *
Hugh Dickins508034a2005-10-29 18:16:30 -07001347 * Unmap all pages in the vma list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 * Only addresses between `start' and `end' will be unmapped.
1350 *
1351 * The VMA list must be sorted in ascending virtual address order.
1352 *
1353 * unmap_vmas() assumes that the caller will flush the whole unmapped address
1354 * range after unmap_vmas() returns. So the only responsibility here is to
1355 * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
1356 * drops the lock and schedules.
1357 */
Al Viro6e8bb012012-03-05 13:41:15 -05001358void unmap_vmas(struct mmu_gather *tlb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 struct vm_area_struct *vma, unsigned long start_addr,
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001360 unsigned long end_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001362 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001364 mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
Al Virof5cc4ee2012-03-05 14:14:20 -05001365 for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001366 unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001367 mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
1370/**
1371 * zap_page_range - remove user pages in a given range
1372 * @vma: vm_area_struct holding the applicable pages
Randy Dunlapeb4546b2012-06-20 12:53:02 -07001373 * @start: starting address of pages to zap
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 * @size: number of bytes to zap
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001375 * @details: details of shared cache invalidation
Al Virof5cc4ee2012-03-05 14:14:20 -05001376 *
1377 * Caller must protect the VMA list
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 */
Linus Torvalds7e027b12012-05-06 13:43:15 -07001379void zap_page_range(struct vm_area_struct *vma, unsigned long start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 unsigned long size, struct zap_details *details)
1381{
1382 struct mm_struct *mm = vma->vm_mm;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001383 struct mmu_gather tlb;
Linus Torvalds7e027b12012-05-06 13:43:15 -07001384 unsigned long end = start + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 lru_add_drain();
Linus Torvalds2b047252013-08-15 11:42:25 -07001387 tlb_gather_mmu(&tlb, mm, start, end);
Hugh Dickins365e9c872005-10-29 18:16:18 -07001388 update_hiwater_rss(mm);
Linus Torvalds7e027b12012-05-06 13:43:15 -07001389 mmu_notifier_invalidate_range_start(mm, start, end);
1390 for ( ; vma && vma->vm_start < end; vma = vma->vm_next)
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001391 unmap_single_vma(&tlb, vma, start, end, details);
Linus Torvalds7e027b12012-05-06 13:43:15 -07001392 mmu_notifier_invalidate_range_end(mm, start, end);
1393 tlb_finish_mmu(&tlb, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
Jack Steinerc627f9c2008-07-29 22:33:53 -07001396/**
Al Virof5cc4ee2012-03-05 14:14:20 -05001397 * zap_page_range_single - remove user pages in a given range
1398 * @vma: vm_area_struct holding the applicable pages
1399 * @address: starting address of pages to zap
1400 * @size: number of bytes to zap
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001401 * @details: details of shared cache invalidation
Al Virof5cc4ee2012-03-05 14:14:20 -05001402 *
1403 * The range must fit into one VMA.
1404 */
1405static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
1406 unsigned long size, struct zap_details *details)
1407{
1408 struct mm_struct *mm = vma->vm_mm;
1409 struct mmu_gather tlb;
1410 unsigned long end = address + size;
Al Virof5cc4ee2012-03-05 14:14:20 -05001411
1412 lru_add_drain();
Linus Torvalds2b047252013-08-15 11:42:25 -07001413 tlb_gather_mmu(&tlb, mm, address, end);
Al Virof5cc4ee2012-03-05 14:14:20 -05001414 update_hiwater_rss(mm);
1415 mmu_notifier_invalidate_range_start(mm, address, end);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001416 unmap_single_vma(&tlb, vma, address, end, details);
Al Virof5cc4ee2012-03-05 14:14:20 -05001417 mmu_notifier_invalidate_range_end(mm, address, end);
1418 tlb_finish_mmu(&tlb, address, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
1420
Jack Steinerc627f9c2008-07-29 22:33:53 -07001421/**
1422 * zap_vma_ptes - remove ptes mapping the vma
1423 * @vma: vm_area_struct holding ptes to be zapped
1424 * @address: starting address of pages to zap
1425 * @size: number of bytes to zap
1426 *
1427 * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1428 *
1429 * The entire address range must be fully contained within the vma.
1430 *
1431 * Returns 0 if successful.
1432 */
1433int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
1434 unsigned long size)
1435{
1436 if (address < vma->vm_start || address + size > vma->vm_end ||
1437 !(vma->vm_flags & VM_PFNMAP))
1438 return -1;
Al Virof5cc4ee2012-03-05 14:14:20 -05001439 zap_page_range_single(vma, address, size, NULL);
Jack Steinerc627f9c2008-07-29 22:33:53 -07001440 return 0;
1441}
1442EXPORT_SYMBOL_GPL(zap_vma_ptes);
1443
Namhyung Kim25ca1d62010-10-26 14:21:59 -07001444pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
Harvey Harrison920c7a52008-02-04 22:29:26 -08001445 spinlock_t **ptl)
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -08001446{
1447 pgd_t * pgd = pgd_offset(mm, addr);
1448 pud_t * pud = pud_alloc(mm, pgd, addr);
1449 if (pud) {
Trond Myklebust49c91fb2005-11-29 19:27:22 -05001450 pmd_t * pmd = pmd_alloc(mm, pud, addr);
Andrea Arcangelif66055ab2011-01-13 15:46:54 -08001451 if (pmd) {
1452 VM_BUG_ON(pmd_trans_huge(*pmd));
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -08001453 return pte_alloc_map_lock(mm, pmd, addr, ptl);
Andrea Arcangelif66055ab2011-01-13 15:46:54 -08001454 }
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -08001455 }
1456 return NULL;
1457}
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459/*
Linus Torvalds238f58d2005-11-29 13:01:56 -08001460 * This is the old fallback for page remapping.
1461 *
1462 * For historical reasons, it only allows reserved pages. Only
1463 * old drivers should use this, and they needed to mark their
1464 * pages reserved for the old functions anyway.
1465 */
Nick Piggin423bad602008-04-28 02:13:01 -07001466static int insert_page(struct vm_area_struct *vma, unsigned long addr,
1467 struct page *page, pgprot_t prot)
Linus Torvalds238f58d2005-11-29 13:01:56 -08001468{
Nick Piggin423bad602008-04-28 02:13:01 -07001469 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds238f58d2005-11-29 13:01:56 -08001470 int retval;
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -08001471 pte_t *pte;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001472 spinlock_t *ptl;
1473
Linus Torvalds238f58d2005-11-29 13:01:56 -08001474 retval = -EINVAL;
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001475 if (PageAnon(page))
KAMEZAWA Hiroyuki5b4e6552008-10-18 20:28:10 -07001476 goto out;
Linus Torvalds238f58d2005-11-29 13:01:56 -08001477 retval = -ENOMEM;
1478 flush_dcache_page(page);
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -08001479 pte = get_locked_pte(mm, addr, &ptl);
Linus Torvalds238f58d2005-11-29 13:01:56 -08001480 if (!pte)
KAMEZAWA Hiroyuki5b4e6552008-10-18 20:28:10 -07001481 goto out;
Linus Torvalds238f58d2005-11-29 13:01:56 -08001482 retval = -EBUSY;
1483 if (!pte_none(*pte))
1484 goto out_unlock;
1485
1486 /* Ok, finally just insert the thing.. */
1487 get_page(page);
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001488 inc_mm_counter_fast(mm, mm_counter_file(page));
Linus Torvalds238f58d2005-11-29 13:01:56 -08001489 page_add_file_rmap(page);
1490 set_pte_at(mm, addr, pte, mk_pte(page, prot));
1491
1492 retval = 0;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001493 pte_unmap_unlock(pte, ptl);
1494 return retval;
Linus Torvalds238f58d2005-11-29 13:01:56 -08001495out_unlock:
1496 pte_unmap_unlock(pte, ptl);
1497out:
1498 return retval;
1499}
1500
Rolf Eike Beerbfa5bf62006-09-25 23:31:22 -07001501/**
1502 * vm_insert_page - insert single page into user vma
1503 * @vma: user vma to map to
1504 * @addr: target user address of this page
1505 * @page: source kernel page
1506 *
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001507 * This allows drivers to insert individual pages they've allocated
1508 * into a user vma.
1509 *
1510 * The page has to be a nice clean _individual_ kernel allocation.
1511 * If you allocate a compound page, you need to have marked it as
1512 * such (__GFP_COMP), or manually just split the page up yourself
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001513 * (see split_page()).
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001514 *
1515 * NOTE! Traditionally this was done with "remap_pfn_range()" which
1516 * took an arbitrary page protection parameter. This doesn't allow
1517 * that. Your vma protection will have to be set up correctly, which
1518 * means that if you want a shared writable mapping, you'd better
1519 * ask for a shared writable mapping!
1520 *
1521 * The page does not need to be reserved.
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001522 *
1523 * Usually this function is called from f_op->mmap() handler
1524 * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
1525 * Caller must set VM_MIXEDMAP on vma if it wants to call this
1526 * function from other places, for example from page-fault handler.
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001527 */
Nick Piggin423bad602008-04-28 02:13:01 -07001528int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
1529 struct page *page)
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001530{
1531 if (addr < vma->vm_start || addr >= vma->vm_end)
1532 return -EFAULT;
1533 if (!page_count(page))
1534 return -EINVAL;
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001535 if (!(vma->vm_flags & VM_MIXEDMAP)) {
1536 BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem));
1537 BUG_ON(vma->vm_flags & VM_PFNMAP);
1538 vma->vm_flags |= VM_MIXEDMAP;
1539 }
Nick Piggin423bad602008-04-28 02:13:01 -07001540 return insert_page(vma, addr, page, vma->vm_page_prot);
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001541}
Linus Torvaldse3c33742005-12-03 20:48:11 -08001542EXPORT_SYMBOL(vm_insert_page);
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001543
Nick Piggin423bad602008-04-28 02:13:01 -07001544static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
Dan Williams01c8f1c2016-01-15 16:56:40 -08001545 pfn_t pfn, pgprot_t prot)
Nick Piggin423bad602008-04-28 02:13:01 -07001546{
1547 struct mm_struct *mm = vma->vm_mm;
1548 int retval;
1549 pte_t *pte, entry;
1550 spinlock_t *ptl;
1551
1552 retval = -ENOMEM;
1553 pte = get_locked_pte(mm, addr, &ptl);
1554 if (!pte)
1555 goto out;
1556 retval = -EBUSY;
1557 if (!pte_none(*pte))
1558 goto out_unlock;
1559
1560 /* Ok, finally just insert the thing.. */
Dan Williams01c8f1c2016-01-15 16:56:40 -08001561 if (pfn_t_devmap(pfn))
1562 entry = pte_mkdevmap(pfn_t_pte(pfn, prot));
1563 else
1564 entry = pte_mkspecial(pfn_t_pte(pfn, prot));
Nick Piggin423bad602008-04-28 02:13:01 -07001565 set_pte_at(mm, addr, pte, entry);
Russell King4b3073e2009-12-18 16:40:18 +00001566 update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
Nick Piggin423bad602008-04-28 02:13:01 -07001567
1568 retval = 0;
1569out_unlock:
1570 pte_unmap_unlock(pte, ptl);
1571out:
1572 return retval;
1573}
1574
Nick Piggine0dc0d82007-02-12 00:51:36 -08001575/**
1576 * vm_insert_pfn - insert single pfn into user vma
1577 * @vma: user vma to map to
1578 * @addr: target user address of this page
1579 * @pfn: source kernel pfn
1580 *
Robert P. J. Dayc462f172012-10-08 16:33:43 -07001581 * Similar to vm_insert_page, this allows drivers to insert individual pages
Nick Piggine0dc0d82007-02-12 00:51:36 -08001582 * they've allocated into a user vma. Same comments apply.
1583 *
1584 * This function should only be called from a vm_ops->fault handler, and
1585 * in that case the handler should return NULL.
Nick Piggin0d71d10a2008-07-23 21:27:05 -07001586 *
1587 * vma cannot be a COW mapping.
1588 *
1589 * As this is called only for pages that do not currently exist, we
1590 * do not need to flush old virtual caches or the TLB.
Nick Piggine0dc0d82007-02-12 00:51:36 -08001591 */
1592int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
Nick Piggin423bad602008-04-28 02:13:01 -07001593 unsigned long pfn)
Nick Piggine0dc0d82007-02-12 00:51:36 -08001594{
Andy Lutomirski1745cbc2015-12-29 20:12:20 -08001595 return vm_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot);
1596}
1597EXPORT_SYMBOL(vm_insert_pfn);
1598
1599/**
1600 * vm_insert_pfn_prot - insert single pfn into user vma with specified pgprot
1601 * @vma: user vma to map to
1602 * @addr: target user address of this page
1603 * @pfn: source kernel pfn
1604 * @pgprot: pgprot flags for the inserted page
1605 *
1606 * This is exactly like vm_insert_pfn, except that it allows drivers to
1607 * to override pgprot on a per-page basis.
1608 *
1609 * This only makes sense for IO mappings, and it makes no sense for
1610 * cow mappings. In general, using multiple vmas is preferable;
1611 * vm_insert_pfn_prot should only be used if using multiple VMAs is
1612 * impractical.
1613 */
1614int vm_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
1615 unsigned long pfn, pgprot_t pgprot)
1616{
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001617 int ret;
Nick Piggin7e675132008-04-28 02:13:00 -07001618 /*
1619 * Technically, architectures with pte_special can avoid all these
1620 * restrictions (same for remap_pfn_range). However we would like
1621 * consistency in testing and feature parity among all, so we should
1622 * try to keep these invariants in place for everybody.
1623 */
Jared Hulbertb379d792008-04-28 02:12:58 -07001624 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
1625 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
1626 (VM_PFNMAP|VM_MIXEDMAP));
1627 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
1628 BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
Nick Piggine0dc0d82007-02-12 00:51:36 -08001629
Nick Piggin423bad602008-04-28 02:13:01 -07001630 if (addr < vma->vm_start || addr >= vma->vm_end)
1631 return -EFAULT;
Dan Williamsf25748e32016-01-15 16:56:43 -08001632 if (track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)))
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001633 return -EINVAL;
1634
Dan Williams01c8f1c2016-01-15 16:56:40 -08001635 ret = insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot);
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001636
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001637 return ret;
Nick Piggine0dc0d82007-02-12 00:51:36 -08001638}
Andy Lutomirski1745cbc2015-12-29 20:12:20 -08001639EXPORT_SYMBOL(vm_insert_pfn_prot);
Nick Piggine0dc0d82007-02-12 00:51:36 -08001640
Nick Piggin423bad602008-04-28 02:13:01 -07001641int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
Dan Williams01c8f1c2016-01-15 16:56:40 -08001642 pfn_t pfn)
Nick Piggin423bad602008-04-28 02:13:01 -07001643{
1644 BUG_ON(!(vma->vm_flags & VM_MIXEDMAP));
1645
1646 if (addr < vma->vm_start || addr >= vma->vm_end)
1647 return -EFAULT;
1648
1649 /*
1650 * If we don't have pte special, then we have to use the pfn_valid()
1651 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
1652 * refcount the page if pfn_valid is true (hence insert_page rather
Hugh Dickins62eede62009-09-21 17:03:34 -07001653 * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP
1654 * without pte special, it would there be refcounted as a normal page.
Nick Piggin423bad602008-04-28 02:13:01 -07001655 */
Dan Williams03fc2da2016-01-26 09:48:05 -08001656 if (!HAVE_PTE_SPECIAL && !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) {
Nick Piggin423bad602008-04-28 02:13:01 -07001657 struct page *page;
1658
Dan Williams03fc2da2016-01-26 09:48:05 -08001659 /*
1660 * At this point we are committed to insert_page()
1661 * regardless of whether the caller specified flags that
1662 * result in pfn_t_has_page() == false.
1663 */
1664 page = pfn_to_page(pfn_t_to_pfn(pfn));
Nick Piggin423bad602008-04-28 02:13:01 -07001665 return insert_page(vma, addr, page, vma->vm_page_prot);
1666 }
1667 return insert_pfn(vma, addr, pfn, vma->vm_page_prot);
1668}
1669EXPORT_SYMBOL(vm_insert_mixed);
1670
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001671/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 * maps a range of physical memory into the requested pages. the old
1673 * mappings are removed. any references to nonexistent pages results
1674 * in null mappings (currently treated as "copy-on-access")
1675 */
1676static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
1677 unsigned long addr, unsigned long end,
1678 unsigned long pfn, pgprot_t prot)
1679{
1680 pte_t *pte;
Hugh Dickinsc74df322005-10-29 18:16:23 -07001681 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Hugh Dickinsc74df322005-10-29 18:16:23 -07001683 pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 if (!pte)
1685 return -ENOMEM;
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001686 arch_enter_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 do {
1688 BUG_ON(!pte_none(*pte));
Nick Piggin7e675132008-04-28 02:13:00 -07001689 set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 pfn++;
1691 } while (pte++, addr += PAGE_SIZE, addr != end);
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001692 arch_leave_lazy_mmu_mode();
Hugh Dickinsc74df322005-10-29 18:16:23 -07001693 pte_unmap_unlock(pte - 1, ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return 0;
1695}
1696
1697static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
1698 unsigned long addr, unsigned long end,
1699 unsigned long pfn, pgprot_t prot)
1700{
1701 pmd_t *pmd;
1702 unsigned long next;
1703
1704 pfn -= addr >> PAGE_SHIFT;
1705 pmd = pmd_alloc(mm, pud, addr);
1706 if (!pmd)
1707 return -ENOMEM;
Andrea Arcangelif66055ab2011-01-13 15:46:54 -08001708 VM_BUG_ON(pmd_trans_huge(*pmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 do {
1710 next = pmd_addr_end(addr, end);
1711 if (remap_pte_range(mm, pmd, addr, next,
1712 pfn + (addr >> PAGE_SHIFT), prot))
1713 return -ENOMEM;
1714 } while (pmd++, addr = next, addr != end);
1715 return 0;
1716}
1717
1718static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
1719 unsigned long addr, unsigned long end,
1720 unsigned long pfn, pgprot_t prot)
1721{
1722 pud_t *pud;
1723 unsigned long next;
1724
1725 pfn -= addr >> PAGE_SHIFT;
1726 pud = pud_alloc(mm, pgd, addr);
1727 if (!pud)
1728 return -ENOMEM;
1729 do {
1730 next = pud_addr_end(addr, end);
1731 if (remap_pmd_range(mm, pud, addr, next,
1732 pfn + (addr >> PAGE_SHIFT), prot))
1733 return -ENOMEM;
1734 } while (pud++, addr = next, addr != end);
1735 return 0;
1736}
1737
Rolf Eike Beerbfa5bf62006-09-25 23:31:22 -07001738/**
1739 * remap_pfn_range - remap kernel memory to userspace
1740 * @vma: user vma to map to
1741 * @addr: target user address to start at
1742 * @pfn: physical address of kernel memory
1743 * @size: size of map area
1744 * @prot: page protection flags for this mapping
1745 *
1746 * Note: this is only safe if the mm semaphore is held when called.
1747 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1749 unsigned long pfn, unsigned long size, pgprot_t prot)
1750{
1751 pgd_t *pgd;
1752 unsigned long next;
Hugh Dickins2d15cab2005-06-25 14:54:33 -07001753 unsigned long end = addr + PAGE_ALIGN(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 struct mm_struct *mm = vma->vm_mm;
Yongji Xied5957d22016-05-20 16:57:41 -07001755 unsigned long remap_pfn = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 int err;
1757
1758 /*
1759 * Physically remapped pages are special. Tell the
1760 * rest of the world about it:
1761 * VM_IO tells people not to look at these pages
1762 * (accesses can have side effects).
Linus Torvalds6aab3412005-11-28 14:34:23 -08001763 * VM_PFNMAP tells the core MM that the base pages are just
1764 * raw PFN mappings, and do not have a "struct page" associated
1765 * with them.
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001766 * VM_DONTEXPAND
1767 * Disable vma merging and expanding with mremap().
1768 * VM_DONTDUMP
1769 * Omit vma from core dump, even when VM_IO turned off.
Linus Torvaldsfb155c12005-12-11 19:46:02 -08001770 *
1771 * There's a horrible special case to handle copy-on-write
1772 * behaviour that some programs depend on. We mark the "original"
1773 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001774 * See vm_normal_page() for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 */
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001776 if (is_cow_mapping(vma->vm_flags)) {
1777 if (addr != vma->vm_start || end != vma->vm_end)
1778 return -EINVAL;
Linus Torvaldsfb155c12005-12-11 19:46:02 -08001779 vma->vm_pgoff = pfn;
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001780 }
1781
Yongji Xied5957d22016-05-20 16:57:41 -07001782 err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size));
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001783 if (err)
venkatesh.pallipadi@intel.com3c8bb732008-12-18 11:41:27 -08001784 return -EINVAL;
Linus Torvaldsfb155c12005-12-11 19:46:02 -08001785
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001786 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 BUG_ON(addr >= end);
1789 pfn -= addr >> PAGE_SHIFT;
1790 pgd = pgd_offset(mm, addr);
1791 flush_cache_range(vma, addr, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 do {
1793 next = pgd_addr_end(addr, end);
1794 err = remap_pud_range(mm, pgd, addr, next,
1795 pfn + (addr >> PAGE_SHIFT), prot);
1796 if (err)
1797 break;
1798 } while (pgd++, addr = next, addr != end);
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001799
1800 if (err)
Yongji Xied5957d22016-05-20 16:57:41 -07001801 untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size));
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return err;
1804}
1805EXPORT_SYMBOL(remap_pfn_range);
1806
Linus Torvaldsb4cbb192013-04-16 13:45:37 -07001807/**
1808 * vm_iomap_memory - remap memory to userspace
1809 * @vma: user vma to map to
1810 * @start: start of area
1811 * @len: size of area
1812 *
1813 * This is a simplified io_remap_pfn_range() for common driver use. The
1814 * driver just needs to give us the physical memory range to be mapped,
1815 * we'll figure out the rest from the vma information.
1816 *
1817 * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
1818 * whatever write-combining details or similar.
1819 */
1820int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1821{
1822 unsigned long vm_len, pfn, pages;
1823
1824 /* Check that the physical memory area passed in looks valid */
1825 if (start + len < start)
1826 return -EINVAL;
1827 /*
1828 * You *really* shouldn't map things that aren't page-aligned,
1829 * but we've historically allowed it because IO memory might
1830 * just have smaller alignment.
1831 */
1832 len += start & ~PAGE_MASK;
1833 pfn = start >> PAGE_SHIFT;
1834 pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
1835 if (pfn + pages < pfn)
1836 return -EINVAL;
1837
1838 /* We start the mapping 'vm_pgoff' pages into the area */
1839 if (vma->vm_pgoff > pages)
1840 return -EINVAL;
1841 pfn += vma->vm_pgoff;
1842 pages -= vma->vm_pgoff;
1843
1844 /* Can we fit all of the mapping? */
1845 vm_len = vma->vm_end - vma->vm_start;
1846 if (vm_len >> PAGE_SHIFT > pages)
1847 return -EINVAL;
1848
1849 /* Ok, let it rip */
1850 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
1851}
1852EXPORT_SYMBOL(vm_iomap_memory);
1853
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07001854static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
1855 unsigned long addr, unsigned long end,
1856 pte_fn_t fn, void *data)
1857{
1858 pte_t *pte;
1859 int err;
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08001860 pgtable_t token;
Borislav Petkov94909912007-05-06 14:49:17 -07001861 spinlock_t *uninitialized_var(ptl);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07001862
1863 pte = (mm == &init_mm) ?
1864 pte_alloc_kernel(pmd, addr) :
1865 pte_alloc_map_lock(mm, pmd, addr, &ptl);
1866 if (!pte)
1867 return -ENOMEM;
1868
1869 BUG_ON(pmd_huge(*pmd));
1870
Jeremy Fitzhardinge38e0edb2009-01-06 14:39:21 -08001871 arch_enter_lazy_mmu_mode();
1872
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08001873 token = pmd_pgtable(*pmd);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07001874
1875 do {
Daisuke Nishimurac36987e2009-10-26 16:50:23 -07001876 err = fn(pte++, token, addr, data);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07001877 if (err)
1878 break;
Daisuke Nishimurac36987e2009-10-26 16:50:23 -07001879 } while (addr += PAGE_SIZE, addr != end);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07001880
Jeremy Fitzhardinge38e0edb2009-01-06 14:39:21 -08001881 arch_leave_lazy_mmu_mode();
1882
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07001883 if (mm != &init_mm)
1884 pte_unmap_unlock(pte-1, ptl);
1885 return err;
1886}
1887
1888static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
1889 unsigned long addr, unsigned long end,
1890 pte_fn_t fn, void *data)
1891{
1892 pmd_t *pmd;
1893 unsigned long next;
1894 int err;
1895
Andi Kleenceb86872008-07-23 21:27:50 -07001896 BUG_ON(pud_huge(*pud));
1897
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07001898 pmd = pmd_alloc(mm, pud, addr);
1899 if (!pmd)
1900 return -ENOMEM;
1901 do {
1902 next = pmd_addr_end(addr, end);
1903 err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
1904 if (err)
1905 break;
1906 } while (pmd++, addr = next, addr != end);
1907 return err;
1908}
1909
1910static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
1911 unsigned long addr, unsigned long end,
1912 pte_fn_t fn, void *data)
1913{
1914 pud_t *pud;
1915 unsigned long next;
1916 int err;
1917
1918 pud = pud_alloc(mm, pgd, addr);
1919 if (!pud)
1920 return -ENOMEM;
1921 do {
1922 next = pud_addr_end(addr, end);
1923 err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
1924 if (err)
1925 break;
1926 } while (pud++, addr = next, addr != end);
1927 return err;
1928}
1929
1930/*
1931 * Scan a region of virtual memory, filling in page tables as necessary
1932 * and calling a provided function on each leaf page table.
1933 */
1934int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
1935 unsigned long size, pte_fn_t fn, void *data)
1936{
1937 pgd_t *pgd;
1938 unsigned long next;
Jeremy Fitzhardinge57250a52010-08-09 17:19:52 -07001939 unsigned long end = addr + size;
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07001940 int err;
1941
Mika Penttilä9cb65bc2016-03-15 14:56:45 -07001942 if (WARN_ON(addr >= end))
1943 return -EINVAL;
1944
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07001945 pgd = pgd_offset(mm, addr);
1946 do {
1947 next = pgd_addr_end(addr, end);
1948 err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
1949 if (err)
1950 break;
1951 } while (pgd++, addr = next, addr != end);
Jeremy Fitzhardinge57250a52010-08-09 17:19:52 -07001952
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07001953 return err;
1954}
1955EXPORT_SYMBOL_GPL(apply_to_page_range);
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957/*
Kirill A. Shutemov9b4bdd22015-02-10 14:09:51 -08001958 * handle_pte_fault chooses page fault handler according to an entry which was
1959 * read non-atomically. Before making any commitment, on those architectures
1960 * or configurations (e.g. i386 with PAE) which might give a mix of unmatched
1961 * parts, do_swap_page must check under lock before unmapping the pte and
1962 * proceeding (but do_wp_page is only called after already making such a check;
Ryota Ozakia335b2e2011-02-10 13:56:28 +09001963 * and do_anonymous_page can safely check later on).
Hugh Dickins8f4e2102005-10-29 18:16:26 -07001964 */
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07001965static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
Hugh Dickins8f4e2102005-10-29 18:16:26 -07001966 pte_t *page_table, pte_t orig_pte)
1967{
1968 int same = 1;
1969#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
1970 if (sizeof(pte_t) > sizeof(unsigned long)) {
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07001971 spinlock_t *ptl = pte_lockptr(mm, pmd);
1972 spin_lock(ptl);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07001973 same = pte_same(*page_table, orig_pte);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07001974 spin_unlock(ptl);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07001975 }
1976#endif
1977 pte_unmap(page_table);
1978 return same;
1979}
1980
Atsushi Nemoto9de455b2006-12-12 17:14:55 +00001981static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
Linus Torvalds6aab3412005-11-28 14:34:23 -08001982{
Dan Williams0abdd7a2014-01-21 15:48:12 -08001983 debug_dma_assert_idle(src);
1984
Linus Torvalds6aab3412005-11-28 14:34:23 -08001985 /*
1986 * If the source page was a PFN mapping, we don't have
1987 * a "struct page" for it. We do a best-effort copy by
1988 * just copying from the original user address. If that
1989 * fails, we just zero-fill it. Live with it.
1990 */
1991 if (unlikely(!src)) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08001992 void *kaddr = kmap_atomic(dst);
Linus Torvalds5d2a2dbbc2005-11-29 14:07:55 -08001993 void __user *uaddr = (void __user *)(va & PAGE_MASK);
1994
1995 /*
1996 * This really shouldn't fail, because the page is there
1997 * in the page tables. But it might just be unreadable,
1998 * in which case we just give up and fill the result with
1999 * zeroes.
2000 */
2001 if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
Jan Beulich3ecb01d2010-10-26 14:22:27 -07002002 clear_page(kaddr);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002003 kunmap_atomic(kaddr);
Dmitriy Monakhovc4ec7b02006-10-19 23:29:08 -07002004 flush_dcache_page(dst);
Nick Piggin0ed361d2008-02-04 22:29:34 -08002005 } else
2006 copy_user_highpage(dst, src, va, vma);
Linus Torvalds6aab3412005-11-28 14:34:23 -08002007}
2008
Michal Hockoc20cd452016-01-14 15:20:12 -08002009static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma)
2010{
2011 struct file *vm_file = vma->vm_file;
2012
2013 if (vm_file)
2014 return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO;
2015
2016 /*
2017 * Special mappings (e.g. VDSO) do not have any file so fake
2018 * a default GFP_KERNEL for them.
2019 */
2020 return GFP_KERNEL;
2021}
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023/*
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002024 * Notify the address space that the page is about to become writable so that
2025 * it can prohibit this or wait for the page to get into an appropriate state.
2026 *
2027 * We do this without the lock held, so that it can sleep if it needs to.
2028 */
2029static int do_page_mkwrite(struct vm_area_struct *vma, struct page *page,
2030 unsigned long address)
2031{
2032 struct vm_fault vmf;
2033 int ret;
2034
2035 vmf.virtual_address = (void __user *)(address & PAGE_MASK);
2036 vmf.pgoff = page->index;
2037 vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
Michal Hockoc20cd452016-01-14 15:20:12 -08002038 vmf.gfp_mask = __get_fault_gfp_mask(vma);
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002039 vmf.page = page;
Matthew Wilcox2e4cdab2015-02-16 15:58:50 -08002040 vmf.cow_page = NULL;
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002041
2042 ret = vma->vm_ops->page_mkwrite(vma, &vmf);
2043 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
2044 return ret;
2045 if (unlikely(!(ret & VM_FAULT_LOCKED))) {
2046 lock_page(page);
2047 if (!page->mapping) {
2048 unlock_page(page);
2049 return 0; /* retry */
2050 }
2051 ret |= VM_FAULT_LOCKED;
2052 } else
2053 VM_BUG_ON_PAGE(!PageLocked(page), page);
2054 return ret;
2055}
2056
2057/*
Shachar Raindel4e047f82015-04-14 15:46:25 -07002058 * Handle write page faults for pages that can be reused in the current vma
2059 *
2060 * This can happen either due to the mapping being with the VM_SHARED flag,
2061 * or due to us being the last reference standing to the page. In either
2062 * case, all we need to do here is to mark the page as writable and update
2063 * any related book-keeping.
2064 */
2065static inline int wp_page_reuse(struct mm_struct *mm,
2066 struct vm_area_struct *vma, unsigned long address,
2067 pte_t *page_table, spinlock_t *ptl, pte_t orig_pte,
2068 struct page *page, int page_mkwrite,
2069 int dirty_shared)
2070 __releases(ptl)
2071{
2072 pte_t entry;
2073 /*
2074 * Clear the pages cpupid information as the existing
2075 * information potentially belongs to a now completely
2076 * unrelated process.
2077 */
2078 if (page)
2079 page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1);
2080
2081 flush_cache_page(vma, address, pte_pfn(orig_pte));
2082 entry = pte_mkyoung(orig_pte);
2083 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2084 if (ptep_set_access_flags(vma, address, page_table, entry, 1))
2085 update_mmu_cache(vma, address, page_table);
2086 pte_unmap_unlock(page_table, ptl);
2087
2088 if (dirty_shared) {
2089 struct address_space *mapping;
2090 int dirtied;
2091
2092 if (!page_mkwrite)
2093 lock_page(page);
2094
2095 dirtied = set_page_dirty(page);
2096 VM_BUG_ON_PAGE(PageAnon(page), page);
2097 mapping = page->mapping;
2098 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002099 put_page(page);
Shachar Raindel4e047f82015-04-14 15:46:25 -07002100
2101 if ((dirtied || page_mkwrite) && mapping) {
2102 /*
2103 * Some device drivers do not set page.mapping
2104 * but still dirty their pages
2105 */
2106 balance_dirty_pages_ratelimited(mapping);
2107 }
2108
2109 if (!page_mkwrite)
2110 file_update_time(vma->vm_file);
2111 }
2112
2113 return VM_FAULT_WRITE;
2114}
2115
2116/*
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002117 * Handle the case of a page which we actually need to copy to a new page.
2118 *
2119 * Called with mmap_sem locked and the old page referenced, but
2120 * without the ptl held.
2121 *
2122 * High level logic flow:
2123 *
2124 * - Allocate a page, copy the content of the old page to the new one.
2125 * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc.
2126 * - Take the PTL. If the pte changed, bail out and release the allocated page
2127 * - If the pte is still the way we remember it, update the page table and all
2128 * relevant references. This includes dropping the reference the page-table
2129 * held to the old page, as well as updating the rmap.
2130 * - In any case, unlock the PTL and drop the reference we took to the old page.
2131 */
2132static int wp_page_copy(struct mm_struct *mm, struct vm_area_struct *vma,
2133 unsigned long address, pte_t *page_table, pmd_t *pmd,
2134 pte_t orig_pte, struct page *old_page)
2135{
2136 struct page *new_page = NULL;
2137 spinlock_t *ptl = NULL;
2138 pte_t entry;
2139 int page_copied = 0;
2140 const unsigned long mmun_start = address & PAGE_MASK; /* For mmu_notifiers */
2141 const unsigned long mmun_end = mmun_start + PAGE_SIZE; /* For mmu_notifiers */
2142 struct mem_cgroup *memcg;
2143
2144 if (unlikely(anon_vma_prepare(vma)))
2145 goto oom;
2146
2147 if (is_zero_pfn(pte_pfn(orig_pte))) {
2148 new_page = alloc_zeroed_user_highpage_movable(vma, address);
2149 if (!new_page)
2150 goto oom;
2151 } else {
2152 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
2153 if (!new_page)
2154 goto oom;
2155 cow_user_page(new_page, old_page, address, vma);
2156 }
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002157
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002158 if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg, false))
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002159 goto oom_free_new;
2160
Mel Gormaneb3c24f2015-06-24 16:57:27 -07002161 __SetPageUptodate(new_page);
2162
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002163 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
2164
2165 /*
2166 * Re-check the pte - we dropped the lock
2167 */
2168 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2169 if (likely(pte_same(*page_table, orig_pte))) {
2170 if (old_page) {
2171 if (!PageAnon(old_page)) {
Jerome Marchandeca56ff2016-01-14 15:19:26 -08002172 dec_mm_counter_fast(mm,
2173 mm_counter_file(old_page));
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002174 inc_mm_counter_fast(mm, MM_ANONPAGES);
2175 }
2176 } else {
2177 inc_mm_counter_fast(mm, MM_ANONPAGES);
2178 }
2179 flush_cache_page(vma, address, pte_pfn(orig_pte));
2180 entry = mk_pte(new_page, vma->vm_page_prot);
2181 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2182 /*
2183 * Clear the pte entry and flush it first, before updating the
2184 * pte with the new entry. This will avoid a race condition
2185 * seen in the presence of one thread doing SMC and another
2186 * thread doing COW.
2187 */
2188 ptep_clear_flush_notify(vma, address, page_table);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002189 page_add_new_anon_rmap(new_page, vma, address, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002190 mem_cgroup_commit_charge(new_page, memcg, false, false);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002191 lru_cache_add_active_or_unevictable(new_page, vma);
2192 /*
2193 * We call the notify macro here because, when using secondary
2194 * mmu page tables (such as kvm shadow page tables), we want the
2195 * new page to be mapped directly into the secondary page table.
2196 */
2197 set_pte_at_notify(mm, address, page_table, entry);
2198 update_mmu_cache(vma, address, page_table);
2199 if (old_page) {
2200 /*
2201 * Only after switching the pte to the new page may
2202 * we remove the mapcount here. Otherwise another
2203 * process may come and find the rmap count decremented
2204 * before the pte is switched to the new page, and
2205 * "reuse" the old page writing into it while our pte
2206 * here still points into it and can be read by other
2207 * threads.
2208 *
2209 * The critical issue is to order this
2210 * page_remove_rmap with the ptp_clear_flush above.
2211 * Those stores are ordered by (if nothing else,)
2212 * the barrier present in the atomic_add_negative
2213 * in page_remove_rmap.
2214 *
2215 * Then the TLB flush in ptep_clear_flush ensures that
2216 * no process can access the old page before the
2217 * decremented mapcount is visible. And the old page
2218 * cannot be reused until after the decremented
2219 * mapcount is visible. So transitively, TLBs to
2220 * old page will be flushed before it can be reused.
2221 */
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002222 page_remove_rmap(old_page, false);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002223 }
2224
2225 /* Free the old page.. */
2226 new_page = old_page;
2227 page_copied = 1;
2228 } else {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002229 mem_cgroup_cancel_charge(new_page, memcg, false);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002230 }
2231
2232 if (new_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002233 put_page(new_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002234
2235 pte_unmap_unlock(page_table, ptl);
2236 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2237 if (old_page) {
2238 /*
2239 * Don't let another task, with possibly unlocked vma,
2240 * keep the mlocked page.
2241 */
2242 if (page_copied && (vma->vm_flags & VM_LOCKED)) {
2243 lock_page(old_page); /* LRU manipulation */
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002244 if (PageMlocked(old_page))
2245 munlock_vma_page(old_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002246 unlock_page(old_page);
2247 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002248 put_page(old_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002249 }
2250 return page_copied ? VM_FAULT_WRITE : 0;
2251oom_free_new:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002252 put_page(new_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002253oom:
2254 if (old_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002255 put_page(old_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002256 return VM_FAULT_OOM;
2257}
2258
Boaz Harroshdd906182015-04-15 16:15:11 -07002259/*
2260 * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED
2261 * mapping
2262 */
2263static int wp_pfn_shared(struct mm_struct *mm,
2264 struct vm_area_struct *vma, unsigned long address,
2265 pte_t *page_table, spinlock_t *ptl, pte_t orig_pte,
2266 pmd_t *pmd)
2267{
2268 if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) {
2269 struct vm_fault vmf = {
2270 .page = NULL,
2271 .pgoff = linear_page_index(vma, address),
2272 .virtual_address = (void __user *)(address & PAGE_MASK),
2273 .flags = FAULT_FLAG_WRITE | FAULT_FLAG_MKWRITE,
2274 };
2275 int ret;
2276
2277 pte_unmap_unlock(page_table, ptl);
2278 ret = vma->vm_ops->pfn_mkwrite(vma, &vmf);
2279 if (ret & VM_FAULT_ERROR)
2280 return ret;
2281 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
2282 /*
2283 * We might have raced with another page fault while we
2284 * released the pte_offset_map_lock.
2285 */
2286 if (!pte_same(*page_table, orig_pte)) {
2287 pte_unmap_unlock(page_table, ptl);
2288 return 0;
2289 }
2290 }
2291 return wp_page_reuse(mm, vma, address, page_table, ptl, orig_pte,
2292 NULL, 0, 0);
2293}
2294
Shachar Raindel93e478d2015-04-14 15:46:35 -07002295static int wp_page_shared(struct mm_struct *mm, struct vm_area_struct *vma,
2296 unsigned long address, pte_t *page_table,
2297 pmd_t *pmd, spinlock_t *ptl, pte_t orig_pte,
2298 struct page *old_page)
2299 __releases(ptl)
2300{
2301 int page_mkwrite = 0;
2302
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002303 get_page(old_page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07002304
Shachar Raindel93e478d2015-04-14 15:46:35 -07002305 if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
2306 int tmp;
2307
2308 pte_unmap_unlock(page_table, ptl);
2309 tmp = do_page_mkwrite(vma, old_page, address);
2310 if (unlikely(!tmp || (tmp &
2311 (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002312 put_page(old_page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07002313 return tmp;
2314 }
2315 /*
2316 * Since we dropped the lock we need to revalidate
2317 * the PTE as someone else may have changed it. If
2318 * they did, we just return, as we can count on the
2319 * MMU to tell us if they didn't also make it writable.
2320 */
2321 page_table = pte_offset_map_lock(mm, pmd, address,
2322 &ptl);
2323 if (!pte_same(*page_table, orig_pte)) {
2324 unlock_page(old_page);
2325 pte_unmap_unlock(page_table, ptl);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002326 put_page(old_page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07002327 return 0;
2328 }
2329 page_mkwrite = 1;
2330 }
2331
2332 return wp_page_reuse(mm, vma, address, page_table, ptl,
2333 orig_pte, old_page, page_mkwrite, 1);
2334}
2335
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002336/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 * This routine handles present pages, when users try to write
2338 * to a shared page. It is done by copying the page to a new address
2339 * and decrementing the shared-page counter for the old page.
2340 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 * Note that this routine assumes that the protection checks have been
2342 * done by the caller (the low-level page fault routine in most cases).
2343 * Thus we can safely just mark it writable once we've done any necessary
2344 * COW.
2345 *
2346 * We also mark the page dirty at this point even though the page will
2347 * change only once the write actually happens. This avoids a few races,
2348 * and potentially makes it more efficient.
2349 *
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002350 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2351 * but allow concurrent faults), with pte both mapped and locked.
2352 * We return with mmap_sem still held, but pte unmapped and unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 */
Hugh Dickins65500d22005-10-29 18:15:59 -07002354static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
2355 unsigned long address, pte_t *page_table, pmd_t *pmd,
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002356 spinlock_t *ptl, pte_t orig_pte)
Namhyung Kime6219ec2010-10-26 14:22:00 -07002357 __releases(ptl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002359 struct page *old_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Linus Torvalds6aab3412005-11-28 14:34:23 -08002361 old_page = vm_normal_page(vma, address, orig_pte);
Peter Zijlstra251b97f2008-07-04 09:59:24 -07002362 if (!old_page) {
2363 /*
Peter Feiner64e45502014-10-13 15:55:46 -07002364 * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
2365 * VM_PFNMAP VMA.
Peter Zijlstra251b97f2008-07-04 09:59:24 -07002366 *
2367 * We should not cow pages in a shared writeable mapping.
Boaz Harroshdd906182015-04-15 16:15:11 -07002368 * Just mark the pages writable and/or call ops->pfn_mkwrite.
Peter Zijlstra251b97f2008-07-04 09:59:24 -07002369 */
2370 if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2371 (VM_WRITE|VM_SHARED))
Boaz Harroshdd906182015-04-15 16:15:11 -07002372 return wp_pfn_shared(mm, vma, address, page_table, ptl,
2373 orig_pte, pmd);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002374
2375 pte_unmap_unlock(page_table, ptl);
2376 return wp_page_copy(mm, vma, address, page_table, pmd,
2377 orig_pte, old_page);
Peter Zijlstra251b97f2008-07-04 09:59:24 -07002378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Peter Zijlstrad08b3852006-09-25 23:30:57 -07002380 /*
Peter Zijlstraee6a6452006-09-25 23:31:00 -07002381 * Take out anonymous pages first, anonymous shared vmas are
2382 * not dirty accountable.
Peter Zijlstrad08b3852006-09-25 23:30:57 -07002383 */
Hugh Dickins9a840892009-09-21 17:02:01 -07002384 if (PageAnon(old_page) && !PageKsm(old_page)) {
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002385 int total_mapcount;
Hugh Dickinsab967d82009-01-06 14:39:33 -08002386 if (!trylock_page(old_page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002387 get_page(old_page);
Hugh Dickinsab967d82009-01-06 14:39:33 -08002388 pte_unmap_unlock(page_table, ptl);
2389 lock_page(old_page);
2390 page_table = pte_offset_map_lock(mm, pmd, address,
2391 &ptl);
2392 if (!pte_same(*page_table, orig_pte)) {
2393 unlock_page(old_page);
Shachar Raindel28766802015-04-14 15:46:29 -07002394 pte_unmap_unlock(page_table, ptl);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002395 put_page(old_page);
Shachar Raindel28766802015-04-14 15:46:29 -07002396 return 0;
Hugh Dickinsab967d82009-01-06 14:39:33 -08002397 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002398 put_page(old_page);
Peter Zijlstraee6a6452006-09-25 23:31:00 -07002399 }
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002400 if (reuse_swap_page(old_page, &total_mapcount)) {
2401 if (total_mapcount == 1) {
2402 /*
2403 * The page is all ours. Move it to
2404 * our anon_vma so the rmap code will
2405 * not search our parent or siblings.
2406 * Protected against the rmap code by
2407 * the page lock.
2408 */
Hugh Dickins5a499732016-07-14 12:07:38 -07002409 page_move_anon_rmap(old_page, vma);
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002410 }
Michel Lespinasseb009c022011-01-13 15:46:07 -08002411 unlock_page(old_page);
Shachar Raindel4e047f82015-04-14 15:46:25 -07002412 return wp_page_reuse(mm, vma, address, page_table, ptl,
2413 orig_pte, old_page, 0, 0);
Michel Lespinasseb009c022011-01-13 15:46:07 -08002414 }
Hugh Dickinsab967d82009-01-06 14:39:33 -08002415 unlock_page(old_page);
Peter Zijlstraee6a6452006-09-25 23:31:00 -07002416 } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
Peter Zijlstrad08b3852006-09-25 23:30:57 -07002417 (VM_WRITE|VM_SHARED))) {
Shachar Raindel93e478d2015-04-14 15:46:35 -07002418 return wp_page_shared(mm, vma, address, page_table, pmd,
2419 ptl, orig_pte, old_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 /*
2423 * Ok, we need to copy. Oh, well..
2424 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002425 get_page(old_page);
Shachar Raindel28766802015-04-14 15:46:29 -07002426
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002427 pte_unmap_unlock(page_table, ptl);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002428 return wp_page_copy(mm, vma, address, page_table, pmd,
2429 orig_pte, old_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430}
2431
Peter Zijlstra97a89412011-05-24 17:12:04 -07002432static void unmap_mapping_range_vma(struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 unsigned long start_addr, unsigned long end_addr,
2434 struct zap_details *details)
2435{
Al Virof5cc4ee2012-03-05 14:14:20 -05002436 zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437}
2438
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002439static inline void unmap_mapping_range_tree(struct rb_root *root,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 struct zap_details *details)
2441{
2442 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 pgoff_t vba, vea, zba, zea;
2444
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002445 vma_interval_tree_foreach(vma, root,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 details->first_index, details->last_index) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448 vba = vma->vm_pgoff;
Libind6e93212013-07-03 15:01:26 -07002449 vea = vba + vma_pages(vma) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 zba = details->first_index;
2451 if (zba < vba)
2452 zba = vba;
2453 zea = details->last_index;
2454 if (zea > vea)
2455 zea = vea;
2456
Peter Zijlstra97a89412011-05-24 17:12:04 -07002457 unmap_mapping_range_vma(vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
2459 ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
Peter Zijlstra97a89412011-05-24 17:12:04 -07002460 details);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
2462}
2463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464/**
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08002465 * unmap_mapping_range - unmap the portion of all mmaps in the specified
2466 * address_space corresponding to the specified page range in the underlying
2467 * file.
2468 *
Martin Waitz3d410882005-06-23 22:05:21 -07002469 * @mapping: the address space containing mmaps to be unmapped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 * @holebegin: byte in first page to unmap, relative to the start of
2471 * the underlying file. This will be rounded down to a PAGE_SIZE
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +10002472 * boundary. Note that this is different from truncate_pagecache(), which
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 * must keep the partial page. In contrast, we must get rid of
2474 * partial pages.
2475 * @holelen: size of prospective hole in bytes. This will be rounded
2476 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
2477 * end of the file.
2478 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
2479 * but 0 when invalidating pagecache, don't throw away private data.
2480 */
2481void unmap_mapping_range(struct address_space *mapping,
2482 loff_t const holebegin, loff_t const holelen, int even_cows)
2483{
Michal Hockoaac45362016-03-25 14:20:24 -07002484 struct zap_details details = { };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 pgoff_t hba = holebegin >> PAGE_SHIFT;
2486 pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
2487
2488 /* Check for overflow. */
2489 if (sizeof(holelen) > sizeof(hlen)) {
2490 long long holeend =
2491 (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
2492 if (holeend & ~(long long)ULONG_MAX)
2493 hlen = ULONG_MAX - hba + 1;
2494 }
2495
2496 details.check_mapping = even_cows? NULL: mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 details.first_index = hba;
2498 details.last_index = hba + hlen - 1;
2499 if (details.last_index < details.first_index)
2500 details.last_index = ULONG_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Kirill A. Shutemov46c043e2015-09-08 14:59:42 -07002502 i_mmap_lock_write(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002503 if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 unmap_mapping_range_tree(&mapping->i_mmap, &details);
Kirill A. Shutemov46c043e2015-09-08 14:59:42 -07002505 i_mmap_unlock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507EXPORT_SYMBOL(unmap_mapping_range);
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509/*
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002510 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2511 * but allow concurrent faults), and pte mapped but not yet locked.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002512 * We return with pte unmapped and unlocked.
2513 *
2514 * We return with the mmap_sem locked or unlocked in the same cases
2515 * as does filemap_fault().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 */
Hugh Dickins65500d22005-10-29 18:15:59 -07002517static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
2518 unsigned long address, pte_t *page_table, pmd_t *pmd,
Linus Torvalds30c9f3a2009-04-10 08:43:11 -07002519 unsigned int flags, pte_t orig_pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002521 spinlock_t *ptl;
Hugh Dickins56f31802013-02-22 16:36:10 -08002522 struct page *page, *swapcache;
Johannes Weiner00501b52014-08-08 14:19:20 -07002523 struct mem_cgroup *memcg;
Hugh Dickins65500d22005-10-29 18:15:59 -07002524 swp_entry_t entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 pte_t pte;
Michel Lespinassed065bd82010-10-26 14:21:57 -07002526 int locked;
Rik van Rielad8c2ee2010-08-09 17:19:48 -07002527 int exclusive = 0;
Nick Piggin83c54072007-07-19 01:47:05 -07002528 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07002530 if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002531 goto out;
Hugh Dickins65500d22005-10-29 18:15:59 -07002532
2533 entry = pte_to_swp_entry(orig_pte);
Andi Kleend1737fd2009-09-16 11:50:06 +02002534 if (unlikely(non_swap_entry(entry))) {
2535 if (is_migration_entry(entry)) {
2536 migration_entry_wait(mm, pmd, address);
2537 } else if (is_hwpoison_entry(entry)) {
2538 ret = VM_FAULT_HWPOISON;
2539 } else {
2540 print_bad_pte(vma, address, orig_pte, NULL);
Hugh Dickinsd99be1a2009-12-14 17:59:04 -08002541 ret = VM_FAULT_SIGBUS;
Andi Kleend1737fd2009-09-16 11:50:06 +02002542 }
Christoph Lameter06972122006-06-23 02:03:35 -07002543 goto out;
2544 }
Shailabh Nagar0ff92242006-07-14 00:24:37 -07002545 delayacct_set_flag(DELAYACCT_PF_SWAPIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 page = lookup_swap_cache(entry);
2547 if (!page) {
Hugh Dickins02098fe2008-02-04 22:28:42 -08002548 page = swapin_readahead(entry,
2549 GFP_HIGHUSER_MOVABLE, vma, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 if (!page) {
2551 /*
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002552 * Back out if somebody else faulted in this pte
2553 * while we released the pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 */
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002555 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if (likely(pte_same(*page_table, orig_pte)))
2557 ret = VM_FAULT_OOM;
Shailabh Nagar0ff92242006-07-14 00:24:37 -07002558 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
Hugh Dickins65500d22005-10-29 18:15:59 -07002559 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
2561
2562 /* Had to read the page from swap area: Major fault */
2563 ret = VM_FAULT_MAJOR;
Christoph Lameterf8891e52006-06-30 01:55:45 -07002564 count_vm_event(PGMAJFAULT);
Ying Han456f9982011-05-26 16:25:38 -07002565 mem_cgroup_count_vm_event(mm, PGMAJFAULT);
Andi Kleend1737fd2009-09-16 11:50:06 +02002566 } else if (PageHWPoison(page)) {
Wu Fengguang71f72522009-12-16 12:19:58 +01002567 /*
2568 * hwpoisoned dirty swapcache pages are kept for killing
2569 * owner processes (which may be unknown at hwpoison time)
2570 */
Andi Kleend1737fd2009-09-16 11:50:06 +02002571 ret = VM_FAULT_HWPOISON;
2572 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
Hugh Dickins56f31802013-02-22 16:36:10 -08002573 swapcache = page;
Andi Kleen4779cb32009-10-14 01:51:41 +02002574 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 }
2576
Hugh Dickins56f31802013-02-22 16:36:10 -08002577 swapcache = page;
Michel Lespinassed065bd82010-10-26 14:21:57 -07002578 locked = lock_page_or_retry(page, mm, flags);
Rik van Riele709ffd2012-05-29 15:06:18 -07002579
Balbir Singh20a10222007-11-14 17:00:33 -08002580 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
Michel Lespinassed065bd82010-10-26 14:21:57 -07002581 if (!locked) {
2582 ret |= VM_FAULT_RETRY;
2583 goto out_release;
2584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Andrea Arcangeli4969c112010-09-09 16:37:52 -07002586 /*
Hugh Dickins31c4a3d2010-09-19 19:40:22 -07002587 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
2588 * release the swapcache from under us. The page pin, and pte_same
2589 * test below, are not enough to exclude that. Even if it is still
2590 * swapcache, we need to check that the page's swap has not changed.
Andrea Arcangeli4969c112010-09-09 16:37:52 -07002591 */
Hugh Dickins31c4a3d2010-09-19 19:40:22 -07002592 if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val))
Andrea Arcangeli4969c112010-09-09 16:37:52 -07002593 goto out_page;
2594
Hugh Dickinscbf86cf2013-02-22 16:35:08 -08002595 page = ksm_might_need_to_copy(page, vma, address);
2596 if (unlikely(!page)) {
2597 ret = VM_FAULT_OOM;
2598 page = swapcache;
Hugh Dickinscbf86cf2013-02-22 16:35:08 -08002599 goto out_page;
Hugh Dickins5ad64682009-12-14 17:59:24 -08002600 }
2601
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002602 if (mem_cgroup_try_charge(page, mm, GFP_KERNEL, &memcg, false)) {
KAMEZAWA Hiroyuki073e5872008-10-18 20:28:08 -07002603 ret = VM_FAULT_OOM;
Johannes Weinerbc43f752009-04-30 15:08:08 -07002604 goto out_page;
KAMEZAWA Hiroyuki073e5872008-10-18 20:28:08 -07002605 }
2606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 /*
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002608 * Back out if somebody else already faulted in this pte.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 */
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002610 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
Hugh Dickins9e9bef02005-10-29 18:16:15 -07002611 if (unlikely(!pte_same(*page_table, orig_pte)))
Kirill Korotaevb8107482005-05-16 21:53:50 -07002612 goto out_nomap;
Kirill Korotaevb8107482005-05-16 21:53:50 -07002613
2614 if (unlikely(!PageUptodate(page))) {
2615 ret = VM_FAULT_SIGBUS;
2616 goto out_nomap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
2618
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002619 /*
2620 * The page isn't present yet, go ahead with the fault.
2621 *
2622 * Be careful about the sequence of operations here.
2623 * To get its accounting right, reuse_swap_page() must be called
2624 * while the page is counted on swap but not yet in mapcount i.e.
2625 * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
2626 * must be called after the swap_free(), or it will never succeed.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002627 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -08002629 inc_mm_counter_fast(mm, MM_ANONPAGES);
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -08002630 dec_mm_counter_fast(mm, MM_SWAPENTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 pte = mk_pte(page, vma->vm_page_prot);
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002632 if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
Linus Torvalds30c9f3a2009-04-10 08:43:11 -07002634 flags &= ~FAULT_FLAG_WRITE;
Andrea Arcangeli9a5b4892010-08-09 17:19:49 -07002635 ret |= VM_FAULT_WRITE;
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002636 exclusive = RMAP_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 flush_icache_page(vma, page);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07002639 if (pte_swp_soft_dirty(orig_pte))
2640 pte = pte_mksoft_dirty(pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 set_pte_at(mm, address, page_table, pte);
Johannes Weiner00501b52014-08-08 14:19:20 -07002642 if (page == swapcache) {
Johannes Weineraf347702013-02-22 16:32:20 -08002643 do_page_add_anon_rmap(page, vma, address, exclusive);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002644 mem_cgroup_commit_charge(page, memcg, true, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07002645 } else { /* ksm created a completely new copy */
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002646 page_add_new_anon_rmap(page, vma, address, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002647 mem_cgroup_commit_charge(page, memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07002648 lru_cache_add_active_or_unevictable(page, vma);
2649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07002651 swap_free(entry);
Vladimir Davydov5ccc5ab2016-01-20 15:03:10 -08002652 if (mem_cgroup_swap_full(page) ||
2653 (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -08002654 try_to_free_swap(page);
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07002655 unlock_page(page);
Hugh Dickins56f31802013-02-22 16:36:10 -08002656 if (page != swapcache) {
Andrea Arcangeli4969c112010-09-09 16:37:52 -07002657 /*
2658 * Hold the lock to avoid the swap entry to be reused
2659 * until we take the PT lock for the pte_same() check
2660 * (to avoid false positives from pte_same). For
2661 * further safety release the lock after the swap_free
2662 * so that the swap count won't change under a
2663 * parallel locked swapcache.
2664 */
2665 unlock_page(swapcache);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002666 put_page(swapcache);
Andrea Arcangeli4969c112010-09-09 16:37:52 -07002667 }
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07002668
Linus Torvalds30c9f3a2009-04-10 08:43:11 -07002669 if (flags & FAULT_FLAG_WRITE) {
Hugh Dickins61469f12008-03-04 14:29:04 -08002670 ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte);
2671 if (ret & VM_FAULT_ERROR)
2672 ret &= VM_FAULT_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 goto out;
2674 }
2675
2676 /* No need to invalidate - it was non-present before */
Russell King4b3073e2009-12-18 16:40:18 +00002677 update_mmu_cache(vma, address, page_table);
Hugh Dickins65500d22005-10-29 18:15:59 -07002678unlock:
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002679 pte_unmap_unlock(page_table, ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680out:
2681 return ret;
Kirill Korotaevb8107482005-05-16 21:53:50 -07002682out_nomap:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002683 mem_cgroup_cancel_charge(page, memcg, false);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002684 pte_unmap_unlock(page_table, ptl);
Johannes Weinerbc43f752009-04-30 15:08:08 -07002685out_page:
Kirill Korotaevb8107482005-05-16 21:53:50 -07002686 unlock_page(page);
Andi Kleen4779cb32009-10-14 01:51:41 +02002687out_release:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002688 put_page(page);
Hugh Dickins56f31802013-02-22 16:36:10 -08002689 if (page != swapcache) {
Andrea Arcangeli4969c112010-09-09 16:37:52 -07002690 unlock_page(swapcache);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002691 put_page(swapcache);
Andrea Arcangeli4969c112010-09-09 16:37:52 -07002692 }
Hugh Dickins65500d22005-10-29 18:15:59 -07002693 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694}
2695
2696/*
Luck, Tony8ca3eb082010-08-24 11:44:18 -07002697 * This is like a special single-page "expand_{down|up}wards()",
2698 * except we must first make sure that 'address{-|+}PAGE_SIZE'
Linus Torvalds320b2b82010-08-12 17:54:33 -07002699 * doesn't hit another vma.
Linus Torvalds320b2b82010-08-12 17:54:33 -07002700 */
2701static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address)
2702{
2703 address &= PAGE_MASK;
2704 if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) {
Linus Torvalds0e8e50e2010-08-20 16:49:40 -07002705 struct vm_area_struct *prev = vma->vm_prev;
Linus Torvalds320b2b82010-08-12 17:54:33 -07002706
Linus Torvalds0e8e50e2010-08-20 16:49:40 -07002707 /*
2708 * Is there a mapping abutting this one below?
2709 *
2710 * That's only ok if it's the same stack mapping
2711 * that has gotten split..
2712 */
2713 if (prev && prev->vm_end == address)
2714 return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM;
2715
Linus Torvaldsfee7e492015-01-06 13:00:05 -08002716 return expand_downwards(vma, address - PAGE_SIZE);
Linus Torvalds320b2b82010-08-12 17:54:33 -07002717 }
Luck, Tony8ca3eb082010-08-24 11:44:18 -07002718 if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) {
2719 struct vm_area_struct *next = vma->vm_next;
2720
2721 /* As VM_GROWSDOWN but s/below/above/ */
2722 if (next && next->vm_start == address + PAGE_SIZE)
2723 return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM;
2724
Linus Torvaldsfee7e492015-01-06 13:00:05 -08002725 return expand_upwards(vma, address + PAGE_SIZE);
Luck, Tony8ca3eb082010-08-24 11:44:18 -07002726 }
Linus Torvalds320b2b82010-08-12 17:54:33 -07002727 return 0;
2728}
2729
2730/*
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002731 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2732 * but allow concurrent faults), and pte mapped but not yet locked.
2733 * We return with mmap_sem still held, but pte unmapped and unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 */
Hugh Dickins65500d22005-10-29 18:15:59 -07002735static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
2736 unsigned long address, pte_t *page_table, pmd_t *pmd,
Linus Torvalds30c9f3a2009-04-10 08:43:11 -07002737 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
Johannes Weiner00501b52014-08-08 14:19:20 -07002739 struct mem_cgroup *memcg;
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002740 struct page *page;
2741 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 pte_t entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Linus Torvalds11ac5522010-08-14 11:44:56 -07002744 pte_unmap(page_table);
Linus Torvalds320b2b82010-08-12 17:54:33 -07002745
Kirill A. Shutemov6b7339f2015-07-06 23:18:37 +03002746 /* File mapping without ->vm_ops ? */
2747 if (vma->vm_flags & VM_SHARED)
2748 return VM_FAULT_SIGBUS;
2749
Linus Torvalds11ac5522010-08-14 11:44:56 -07002750 /* Check if we need to add a guard page to the stack */
2751 if (check_stack_guard_page(vma, address) < 0)
Linus Torvalds9c145c562015-01-29 11:15:17 -08002752 return VM_FAULT_SIGSEGV;
Linus Torvalds11ac5522010-08-14 11:44:56 -07002753
2754 /* Use the zero-page for reads */
Dominik Dingel593befa2014-10-23 12:07:44 +02002755 if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm)) {
Hugh Dickins62eede62009-09-21 17:03:34 -07002756 entry = pte_mkspecial(pfn_pte(my_zero_pfn(address),
2757 vma->vm_page_prot));
Linus Torvalds11ac5522010-08-14 11:44:56 -07002758 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -07002759 if (!pte_none(*page_table))
2760 goto unlock;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07002761 /* Deliver the page fault to userland, check inside PT lock */
2762 if (userfaultfd_missing(vma)) {
2763 pte_unmap_unlock(page_table, ptl);
2764 return handle_userfault(vma, address, flags,
2765 VM_UFFD_MISSING);
2766 }
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -07002767 goto setpte;
2768 }
2769
Nick Piggin557ed1f2007-10-16 01:24:40 -07002770 /* Allocate our own private page. */
Nick Piggin557ed1f2007-10-16 01:24:40 -07002771 if (unlikely(anon_vma_prepare(vma)))
2772 goto oom;
2773 page = alloc_zeroed_user_highpage_movable(vma, address);
2774 if (!page)
2775 goto oom;
Mel Gormaneb3c24f2015-06-24 16:57:27 -07002776
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002777 if (mem_cgroup_try_charge(page, mm, GFP_KERNEL, &memcg, false))
Mel Gormaneb3c24f2015-06-24 16:57:27 -07002778 goto oom_free_page;
2779
Minchan Kim52f37622013-04-29 15:08:15 -07002780 /*
2781 * The memory barrier inside __SetPageUptodate makes sure that
2782 * preceeding stores to the page contents become visible before
2783 * the set_pte_at() write.
2784 */
Nick Piggin0ed361d2008-02-04 22:29:34 -08002785 __SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Nick Piggin557ed1f2007-10-16 01:24:40 -07002787 entry = mk_pte(page, vma->vm_page_prot);
Hugh Dickins1ac0cb52009-09-21 17:03:29 -07002788 if (vma->vm_flags & VM_WRITE)
2789 entry = pte_mkwrite(pte_mkdirty(entry));
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002790
Nick Piggin557ed1f2007-10-16 01:24:40 -07002791 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
Andrea Arcangeli1c2fb7a2009-09-21 17:02:22 -07002792 if (!pte_none(*page_table))
Nick Piggin557ed1f2007-10-16 01:24:40 -07002793 goto release;
Hugh Dickins9ba69292009-09-21 17:02:20 -07002794
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07002795 /* Deliver the page fault to userland, check inside PT lock */
2796 if (userfaultfd_missing(vma)) {
2797 pte_unmap_unlock(page_table, ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002798 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002799 put_page(page);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07002800 return handle_userfault(vma, address, flags,
2801 VM_UFFD_MISSING);
2802 }
2803
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -08002804 inc_mm_counter_fast(mm, MM_ANONPAGES);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002805 page_add_new_anon_rmap(page, vma, address, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002806 mem_cgroup_commit_charge(page, memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07002807 lru_cache_add_active_or_unevictable(page, vma);
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -07002808setpte:
Hugh Dickins65500d22005-10-29 18:15:59 -07002809 set_pte_at(mm, address, page_table, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 /* No need to invalidate - it was non-present before */
Russell King4b3073e2009-12-18 16:40:18 +00002812 update_mmu_cache(vma, address, page_table);
Hugh Dickins65500d22005-10-29 18:15:59 -07002813unlock:
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002814 pte_unmap_unlock(page_table, ptl);
Nick Piggin83c54072007-07-19 01:47:05 -07002815 return 0;
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002816release:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002817 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002818 put_page(page);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002819 goto unlock;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002820oom_free_page:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002821 put_page(page);
Hugh Dickins65500d22005-10-29 18:15:59 -07002822oom:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 return VM_FAULT_OOM;
2824}
2825
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002826/*
2827 * The mmap_sem must have been held on entry, and may have been
2828 * released depending on flags and vma->vm_ops->fault() return value.
2829 * See filemap_fault() and __lock_page_retry().
2830 */
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07002831static int __do_fault(struct vm_area_struct *vma, unsigned long address,
Matthew Wilcox2e4cdab2015-02-16 15:58:50 -08002832 pgoff_t pgoff, unsigned int flags,
Jan Karabc2466e2016-05-12 18:29:19 +02002833 struct page *cow_page, struct page **page,
2834 void **entry)
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07002835{
2836 struct vm_fault vmf;
2837 int ret;
2838
2839 vmf.virtual_address = (void __user *)(address & PAGE_MASK);
2840 vmf.pgoff = pgoff;
2841 vmf.flags = flags;
2842 vmf.page = NULL;
Michal Hockoc20cd452016-01-14 15:20:12 -08002843 vmf.gfp_mask = __get_fault_gfp_mask(vma);
Matthew Wilcox2e4cdab2015-02-16 15:58:50 -08002844 vmf.cow_page = cow_page;
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07002845
2846 ret = vma->vm_ops->fault(vma, &vmf);
2847 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
2848 return ret;
Jan Karabc2466e2016-05-12 18:29:19 +02002849 if (ret & VM_FAULT_DAX_LOCKED) {
2850 *entry = vmf.entry;
2851 return ret;
2852 }
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07002853
2854 if (unlikely(PageHWPoison(vmf.page))) {
2855 if (ret & VM_FAULT_LOCKED)
2856 unlock_page(vmf.page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002857 put_page(vmf.page);
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07002858 return VM_FAULT_HWPOISON;
2859 }
2860
2861 if (unlikely(!(ret & VM_FAULT_LOCKED)))
2862 lock_page(vmf.page);
2863 else
2864 VM_BUG_ON_PAGE(!PageLocked(vmf.page), vmf.page);
2865
2866 *page = vmf.page;
2867 return ret;
2868}
2869
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07002870/**
2871 * do_set_pte - setup new PTE entry for given page and add reverse page mapping.
2872 *
2873 * @vma: virtual memory area
2874 * @address: user virtual address
2875 * @page: page to map
2876 * @pte: pointer to target page table entry
2877 * @write: true, if new entry is writable
2878 * @anon: true, if it's anonymous page
2879 *
2880 * Caller must hold page table lock relevant for @pte.
2881 *
2882 * Target users are page handler itself and implementations of
2883 * vm_ops->map_pages.
2884 */
2885void do_set_pte(struct vm_area_struct *vma, unsigned long address,
Kirill A. Shutemov315d09b2016-06-24 14:49:45 -07002886 struct page *page, pte_t *pte, bool write, bool anon)
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07002887{
2888 pte_t entry;
2889
2890 flush_icache_page(vma, page);
2891 entry = mk_pte(page, vma->vm_page_prot);
2892 if (write)
2893 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07002894 if (anon) {
2895 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002896 page_add_new_anon_rmap(page, vma, address, false);
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07002897 } else {
Jerome Marchandeca56ff2016-01-14 15:19:26 -08002898 inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07002899 page_add_file_rmap(page);
2900 }
2901 set_pte_at(vma->vm_mm, address, pte, entry);
2902
2903 /* no need to invalidate: a not-present page won't be cached */
2904 update_mmu_cache(vma, address, pte);
2905}
2906
Kirill A. Shutemov3a910532014-08-06 16:08:07 -07002907static unsigned long fault_around_bytes __read_mostly =
2908 rounddown_pow_of_two(65536);
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07002909
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07002910#ifdef CONFIG_DEBUG_FS
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07002911static int fault_around_bytes_get(void *data, u64 *val)
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07002912{
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07002913 *val = fault_around_bytes;
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07002914 return 0;
2915}
2916
Andrey Ryabininb4903d62014-07-30 16:08:35 -07002917/*
2918 * fault_around_pages() and fault_around_mask() expects fault_around_bytes
2919 * rounded down to nearest page order. It's what do_fault_around() expects to
2920 * see.
2921 */
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07002922static int fault_around_bytes_set(void *data, u64 val)
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07002923{
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07002924 if (val / PAGE_SIZE > PTRS_PER_PTE)
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07002925 return -EINVAL;
Andrey Ryabininb4903d62014-07-30 16:08:35 -07002926 if (val > PAGE_SIZE)
2927 fault_around_bytes = rounddown_pow_of_two(val);
2928 else
2929 fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07002930 return 0;
2931}
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07002932DEFINE_SIMPLE_ATTRIBUTE(fault_around_bytes_fops,
2933 fault_around_bytes_get, fault_around_bytes_set, "%llu\n");
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07002934
2935static int __init fault_around_debugfs(void)
2936{
2937 void *ret;
2938
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07002939 ret = debugfs_create_file("fault_around_bytes", 0644, NULL, NULL,
2940 &fault_around_bytes_fops);
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07002941 if (!ret)
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07002942 pr_warn("Failed to create fault_around_bytes in debugfs");
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07002943 return 0;
2944}
2945late_initcall(fault_around_debugfs);
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07002946#endif
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07002947
Kirill A. Shutemov1fdb4122014-06-04 16:10:55 -07002948/*
2949 * do_fault_around() tries to map few pages around the fault address. The hope
2950 * is that the pages will be needed soon and this will lower the number of
2951 * faults to handle.
2952 *
2953 * It uses vm_ops->map_pages() to map the pages, which skips the page if it's
2954 * not ready to be mapped: not up-to-date, locked, etc.
2955 *
2956 * This function is called with the page table lock taken. In the split ptlock
2957 * case the page table lock only protects only those entries which belong to
2958 * the page table corresponding to the fault address.
2959 *
2960 * This function doesn't cross the VMA boundaries, in order to call map_pages()
2961 * only once.
2962 *
2963 * fault_around_pages() defines how many pages we'll try to map.
2964 * do_fault_around() expects it to return a power of two less than or equal to
2965 * PTRS_PER_PTE.
2966 *
2967 * The virtual address of the area that we map is naturally aligned to the
2968 * fault_around_pages() value (and therefore to page order). This way it's
2969 * easier to guarantee that we don't cross page table boundaries.
2970 */
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07002971static void do_fault_around(struct vm_area_struct *vma, unsigned long address,
2972 pte_t *pte, pgoff_t pgoff, unsigned int flags)
2973{
Kirill A. Shutemovaecd6f42014-08-06 16:08:05 -07002974 unsigned long start_addr, nr_pages, mask;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07002975 pgoff_t max_pgoff;
2976 struct vm_fault vmf;
2977 int off;
2978
Jason Low4db0c3c2015-04-15 16:14:08 -07002979 nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT;
Kirill A. Shutemovaecd6f42014-08-06 16:08:05 -07002980 mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
2981
2982 start_addr = max(address & mask, vma->vm_start);
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07002983 off = ((address - start_addr) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
2984 pte -= off;
2985 pgoff -= off;
2986
2987 /*
2988 * max_pgoff is either end of page table or end of vma
Kirill A. Shutemov850e9c62014-06-04 16:10:45 -07002989 * or fault_around_pages() from pgoff, depending what is nearest.
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07002990 */
2991 max_pgoff = pgoff - ((start_addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
2992 PTRS_PER_PTE - 1;
2993 max_pgoff = min3(max_pgoff, vma_pages(vma) + vma->vm_pgoff - 1,
Kirill A. Shutemovaecd6f42014-08-06 16:08:05 -07002994 pgoff + nr_pages - 1);
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07002995
2996 /* Check if it makes any sense to call ->map_pages */
2997 while (!pte_none(*pte)) {
2998 if (++pgoff > max_pgoff)
2999 return;
3000 start_addr += PAGE_SIZE;
3001 if (start_addr >= vma->vm_end)
3002 return;
3003 pte++;
3004 }
3005
3006 vmf.virtual_address = (void __user *) start_addr;
3007 vmf.pte = pte;
3008 vmf.pgoff = pgoff;
3009 vmf.max_pgoff = max_pgoff;
3010 vmf.flags = flags;
Michal Hockoc20cd452016-01-14 15:20:12 -08003011 vmf.gfp_mask = __get_fault_gfp_mask(vma);
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003012 vma->vm_ops->map_pages(vma, &vmf);
3013}
3014
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003015static int do_read_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3016 unsigned long address, pmd_t *pmd,
3017 pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
3018{
3019 struct page *fault_page;
3020 spinlock_t *ptl;
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003021 pte_t *pte;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003022 int ret = 0;
3023
3024 /*
3025 * Let's call ->map_pages() first and use ->fault() as fallback
3026 * if page by the offset is not ready to be mapped (cold cache or
3027 * something).
3028 */
Kirill A. Shutemov9b4bdd22015-02-10 14:09:51 -08003029 if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) {
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003030 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
Kirill A. Shutemov315d09b2016-06-24 14:49:45 -07003031 do_fault_around(vma, address, pte, pgoff, flags);
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003032 if (!pte_same(*pte, orig_pte))
3033 goto unlock_out;
3034 pte_unmap_unlock(pte, ptl);
3035 }
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003036
Jan Karabc2466e2016-05-12 18:29:19 +02003037 ret = __do_fault(vma, address, pgoff, flags, NULL, &fault_page, NULL);
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003038 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3039 return ret;
3040
3041 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
3042 if (unlikely(!pte_same(*pte, orig_pte))) {
3043 pte_unmap_unlock(pte, ptl);
3044 unlock_page(fault_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003045 put_page(fault_page);
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003046 return ret;
3047 }
Kirill A. Shutemov315d09b2016-06-24 14:49:45 -07003048 do_set_pte(vma, address, fault_page, pte, false, false);
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003049 unlock_page(fault_page);
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003050unlock_out:
3051 pte_unmap_unlock(pte, ptl);
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003052 return ret;
3053}
3054
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003055static int do_cow_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3056 unsigned long address, pmd_t *pmd,
3057 pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
3058{
3059 struct page *fault_page, *new_page;
Jan Karabc2466e2016-05-12 18:29:19 +02003060 void *fault_entry;
Johannes Weiner00501b52014-08-08 14:19:20 -07003061 struct mem_cgroup *memcg;
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003062 spinlock_t *ptl;
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003063 pte_t *pte;
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003064 int ret;
3065
3066 if (unlikely(anon_vma_prepare(vma)))
3067 return VM_FAULT_OOM;
3068
3069 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
3070 if (!new_page)
3071 return VM_FAULT_OOM;
3072
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08003073 if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg, false)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003074 put_page(new_page);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003075 return VM_FAULT_OOM;
3076 }
3077
Jan Karabc2466e2016-05-12 18:29:19 +02003078 ret = __do_fault(vma, address, pgoff, flags, new_page, &fault_page,
3079 &fault_entry);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003080 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3081 goto uncharge_out;
3082
Jan Karabc2466e2016-05-12 18:29:19 +02003083 if (!(ret & VM_FAULT_DAX_LOCKED))
Matthew Wilcox2e4cdab2015-02-16 15:58:50 -08003084 copy_user_highpage(new_page, fault_page, address, vma);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003085 __SetPageUptodate(new_page);
3086
3087 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
3088 if (unlikely(!pte_same(*pte, orig_pte))) {
3089 pte_unmap_unlock(pte, ptl);
Jan Karabc2466e2016-05-12 18:29:19 +02003090 if (!(ret & VM_FAULT_DAX_LOCKED)) {
Matthew Wilcox2e4cdab2015-02-16 15:58:50 -08003091 unlock_page(fault_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003092 put_page(fault_page);
Matthew Wilcox2e4cdab2015-02-16 15:58:50 -08003093 } else {
Jan Karabc2466e2016-05-12 18:29:19 +02003094 dax_unlock_mapping_entry(vma->vm_file->f_mapping,
3095 pgoff);
Matthew Wilcox2e4cdab2015-02-16 15:58:50 -08003096 }
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003097 goto uncharge_out;
3098 }
Kirill A. Shutemov315d09b2016-06-24 14:49:45 -07003099 do_set_pte(vma, address, new_page, pte, true, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08003100 mem_cgroup_commit_charge(new_page, memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07003101 lru_cache_add_active_or_unevictable(new_page, vma);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003102 pte_unmap_unlock(pte, ptl);
Jan Karabc2466e2016-05-12 18:29:19 +02003103 if (!(ret & VM_FAULT_DAX_LOCKED)) {
Matthew Wilcox2e4cdab2015-02-16 15:58:50 -08003104 unlock_page(fault_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003105 put_page(fault_page);
Matthew Wilcox2e4cdab2015-02-16 15:58:50 -08003106 } else {
Jan Karabc2466e2016-05-12 18:29:19 +02003107 dax_unlock_mapping_entry(vma->vm_file->f_mapping, pgoff);
Matthew Wilcox2e4cdab2015-02-16 15:58:50 -08003108 }
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003109 return ret;
3110uncharge_out:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08003111 mem_cgroup_cancel_charge(new_page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003112 put_page(new_page);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003113 return ret;
3114}
3115
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003116static int do_shared_fault(struct mm_struct *mm, struct vm_area_struct *vma,
Hugh Dickins16abfa02007-10-04 16:56:06 +01003117 unsigned long address, pmd_t *pmd,
Nick Piggin54cb8822007-07-19 01:46:59 -07003118 pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003120 struct page *fault_page;
3121 struct address_space *mapping;
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003122 spinlock_t *ptl;
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003123 pte_t *pte;
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003124 int dirtied = 0;
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003125 int ret, tmp;
KAMEZAWA Hiroyuki1d65f862011-07-25 17:12:27 -07003126
Jan Karabc2466e2016-05-12 18:29:19 +02003127 ret = __do_fault(vma, address, pgoff, flags, NULL, &fault_page, NULL);
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003128 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003129 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
3131 /*
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003132 * Check if the backing address space wants to know that the page is
3133 * about to become writable
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 */
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07003135 if (vma->vm_ops->page_mkwrite) {
3136 unlock_page(fault_page);
3137 tmp = do_page_mkwrite(vma, fault_page, address);
3138 if (unlikely(!tmp ||
3139 (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003140 put_page(fault_page);
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07003141 return tmp;
3142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 }
3144
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003145 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
3146 if (unlikely(!pte_same(*pte, orig_pte))) {
3147 pte_unmap_unlock(pte, ptl);
3148 unlock_page(fault_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003149 put_page(fault_page);
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003150 return ret;
Peter Zijlstrad08b3852006-09-25 23:30:57 -07003151 }
Kirill A. Shutemov315d09b2016-06-24 14:49:45 -07003152 do_set_pte(vma, address, fault_page, pte, true, false);
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003153 pte_unmap_unlock(pte, ptl);
3154
3155 if (set_page_dirty(fault_page))
3156 dirtied = 1;
Andrew Mortond82fa872014-12-18 16:17:29 -08003157 /*
3158 * Take a local copy of the address_space - page.mapping may be zeroed
3159 * by truncate after unlock_page(). The address_space itself remains
3160 * pinned by vma->vm_file's reference. We rely on unlock_page()'s
3161 * release semantics to prevent the compiler from undoing this copying.
3162 */
Kirill A. Shutemov1c290f62016-01-15 16:52:07 -08003163 mapping = page_rmapping(fault_page);
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003164 unlock_page(fault_page);
3165 if ((dirtied || vma->vm_ops->page_mkwrite) && mapping) {
3166 /*
3167 * Some device drivers do not set page.mapping but still
3168 * dirty their pages
3169 */
3170 balance_dirty_pages_ratelimited(mapping);
KAMEZAWA Hiroyuki1d65f862011-07-25 17:12:27 -07003171 }
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003172
Johannes Weiner74ec6752015-02-10 14:11:28 -08003173 if (!vma->vm_ops->page_mkwrite)
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003174 file_update_time(vma->vm_file);
3175
KAMEZAWA Hiroyuki1d65f862011-07-25 17:12:27 -07003176 return ret;
Nick Piggin54cb8822007-07-19 01:46:59 -07003177}
Nick Piggind00806b2007-07-19 01:46:57 -07003178
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003179/*
3180 * We enter with non-exclusive mmap_sem (to exclude vma changes,
3181 * but allow concurrent faults).
3182 * The mmap_sem may have been released depending on flags and our
3183 * return value. See filemap_fault() and __lock_page_or_retry().
3184 */
Kirill A. Shutemov9b4bdd22015-02-10 14:09:51 -08003185static int do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
Nick Piggin54cb8822007-07-19 01:46:59 -07003186 unsigned long address, pte_t *page_table, pmd_t *pmd,
Linus Torvalds30c9f3a2009-04-10 08:43:11 -07003187 unsigned int flags, pte_t orig_pte)
Nick Piggin54cb8822007-07-19 01:46:59 -07003188{
Matthew Wilcox88193f72016-03-15 14:57:28 -07003189 pgoff_t pgoff = linear_page_index(vma, address);
Nick Piggin54cb8822007-07-19 01:46:59 -07003190
Hugh Dickins16abfa02007-10-04 16:56:06 +01003191 pte_unmap(page_table);
Kirill A. Shutemov6b7339f2015-07-06 23:18:37 +03003192 /* The VMA was not fully populated on mmap() or missing VM_DONTEXPAND */
3193 if (!vma->vm_ops->fault)
3194 return VM_FAULT_SIGBUS;
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003195 if (!(flags & FAULT_FLAG_WRITE))
3196 return do_read_fault(mm, vma, address, pmd, pgoff, flags,
3197 orig_pte);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003198 if (!(vma->vm_flags & VM_SHARED))
3199 return do_cow_fault(mm, vma, address, pmd, pgoff, flags,
3200 orig_pte);
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003201 return do_shared_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
Nick Piggin54cb8822007-07-19 01:46:59 -07003202}
3203
Rashika Kheriab19a9932014-04-03 14:48:02 -07003204static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
Rik van Riel04bb2f92013-10-07 11:29:36 +01003205 unsigned long addr, int page_nid,
3206 int *flags)
Mel Gorman9532fec2012-11-15 01:24:32 +00003207{
3208 get_page(page);
3209
3210 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01003211 if (page_nid == numa_node_id()) {
Mel Gorman9532fec2012-11-15 01:24:32 +00003212 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01003213 *flags |= TNF_FAULT_LOCAL;
3214 }
Mel Gorman9532fec2012-11-15 01:24:32 +00003215
3216 return mpol_misplaced(page, vma, addr);
3217}
3218
Rashika Kheriab19a9932014-04-03 14:48:02 -07003219static int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
Mel Gormand10e63f2012-10-25 14:16:31 +02003220 unsigned long addr, pte_t pte, pte_t *ptep, pmd_t *pmd)
3221{
Mel Gorman4daae3b2012-11-02 11:33:45 +00003222 struct page *page = NULL;
Mel Gormand10e63f2012-10-25 14:16:31 +02003223 spinlock_t *ptl;
Mel Gorman8191acb2013-10-07 11:28:45 +01003224 int page_nid = -1;
Peter Zijlstra90572892013-10-07 11:29:20 +01003225 int last_cpupid;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02003226 int target_nid;
Mel Gormanb8593bf2012-11-21 01:18:23 +00003227 bool migrated = false;
Mel Gormanb191f9b2015-03-25 15:55:40 -07003228 bool was_writable = pte_write(pte);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003229 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02003230
Mel Gormanc0e7cad2015-02-12 14:58:41 -08003231 /* A PROT_NONE fault should not end up here */
3232 BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
3233
Mel Gormand10e63f2012-10-25 14:16:31 +02003234 /*
3235 * The "pte" at this point cannot be used safely without
3236 * validation through pte_unmap_same(). It's of NUMA type but
3237 * the pfn may be screwed if the read is non atomic.
3238 *
Mel Gorman4d942462015-02-12 14:58:28 -08003239 * We can safely just do a "set_pte_at()", because the old
3240 * page table entry is not accessible, so there would be no
3241 * concurrent hardware modifications to the PTE.
Mel Gormand10e63f2012-10-25 14:16:31 +02003242 */
3243 ptl = pte_lockptr(mm, pmd);
3244 spin_lock(ptl);
Mel Gorman4daae3b2012-11-02 11:33:45 +00003245 if (unlikely(!pte_same(*ptep, pte))) {
3246 pte_unmap_unlock(ptep, ptl);
3247 goto out;
3248 }
3249
Mel Gorman4d942462015-02-12 14:58:28 -08003250 /* Make it present again */
3251 pte = pte_modify(pte, vma->vm_page_prot);
3252 pte = pte_mkyoung(pte);
Mel Gormanb191f9b2015-03-25 15:55:40 -07003253 if (was_writable)
3254 pte = pte_mkwrite(pte);
Mel Gormand10e63f2012-10-25 14:16:31 +02003255 set_pte_at(mm, addr, ptep, pte);
3256 update_mmu_cache(vma, addr, ptep);
3257
3258 page = vm_normal_page(vma, addr, pte);
3259 if (!page) {
3260 pte_unmap_unlock(ptep, ptl);
3261 return 0;
3262 }
3263
Kirill A. Shutemove81c4802016-01-15 16:53:49 -08003264 /* TODO: handle PTE-mapped THP */
3265 if (PageCompound(page)) {
3266 pte_unmap_unlock(ptep, ptl);
3267 return 0;
3268 }
3269
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003270 /*
Mel Gormanbea66fb2015-03-25 15:55:37 -07003271 * Avoid grouping on RO pages in general. RO pages shouldn't hurt as
3272 * much anyway since they can be in shared cache state. This misses
3273 * the case where a mapping is writable but the process never writes
3274 * to it but pte_write gets cleared during protection updates and
3275 * pte_dirty has unpredictable behaviour between PTE scan updates,
3276 * background writeback, dirty balancing and application behaviour.
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003277 */
Mel Gormanbea66fb2015-03-25 15:55:37 -07003278 if (!(vma->vm_flags & VM_WRITE))
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003279 flags |= TNF_NO_GROUP;
3280
Rik van Rieldabe1d92013-10-07 11:29:34 +01003281 /*
3282 * Flag if the page is shared between multiple address spaces. This
3283 * is later used when determining whether to group tasks together
3284 */
3285 if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED))
3286 flags |= TNF_SHARED;
3287
Peter Zijlstra90572892013-10-07 11:29:20 +01003288 last_cpupid = page_cpupid_last(page);
Mel Gorman8191acb2013-10-07 11:28:45 +01003289 page_nid = page_to_nid(page);
Rik van Riel04bb2f92013-10-07 11:29:36 +01003290 target_nid = numa_migrate_prep(page, vma, addr, page_nid, &flags);
Mel Gormand10e63f2012-10-25 14:16:31 +02003291 pte_unmap_unlock(ptep, ptl);
Mel Gorman4daae3b2012-11-02 11:33:45 +00003292 if (target_nid == -1) {
Mel Gorman4daae3b2012-11-02 11:33:45 +00003293 put_page(page);
3294 goto out;
3295 }
3296
3297 /* Migrate to the requested node */
Mel Gorman1bc115d2013-10-07 11:29:05 +01003298 migrated = migrate_misplaced_page(page, vma, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003299 if (migrated) {
Mel Gorman8191acb2013-10-07 11:28:45 +01003300 page_nid = target_nid;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003301 flags |= TNF_MIGRATED;
Mel Gorman074c2382015-03-25 15:55:42 -07003302 } else
3303 flags |= TNF_MIGRATE_FAIL;
Mel Gorman4daae3b2012-11-02 11:33:45 +00003304
3305out:
Mel Gorman8191acb2013-10-07 11:28:45 +01003306 if (page_nid != -1)
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003307 task_numa_fault(last_cpupid, page_nid, 1, flags);
Mel Gormand10e63f2012-10-25 14:16:31 +02003308 return 0;
3309}
3310
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07003311static int create_huge_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
3312 unsigned long address, pmd_t *pmd, unsigned int flags)
3313{
Kirill A. Shutemovfb6dd5f2015-09-09 15:39:35 -07003314 if (vma_is_anonymous(vma))
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07003315 return do_huge_pmd_anonymous_page(mm, vma, address, pmd, flags);
3316 if (vma->vm_ops->pmd_fault)
3317 return vma->vm_ops->pmd_fault(vma, address, pmd, flags);
3318 return VM_FAULT_FALLBACK;
3319}
3320
3321static int wp_huge_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
3322 unsigned long address, pmd_t *pmd, pmd_t orig_pmd,
3323 unsigned int flags)
3324{
Kirill A. Shutemovfb6dd5f2015-09-09 15:39:35 -07003325 if (vma_is_anonymous(vma))
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07003326 return do_huge_pmd_wp_page(mm, vma, address, pmd, orig_pmd);
3327 if (vma->vm_ops->pmd_fault)
3328 return vma->vm_ops->pmd_fault(vma, address, pmd, flags);
3329 return VM_FAULT_FALLBACK;
3330}
3331
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332/*
3333 * These routines also need to handle stuff like marking pages dirty
3334 * and/or accessed for architectures that don't do it in hardware (most
3335 * RISC architectures). The early dirtying is also good on the i386.
3336 *
3337 * There is also a hook called "update_mmu_cache()" that architectures
3338 * with external mmu caches can use to update those (ie the Sparc or
3339 * PowerPC hashed page tables that act as extended TLBs).
3340 *
Hugh Dickinsc74df322005-10-29 18:16:23 -07003341 * We enter with non-exclusive mmap_sem (to exclude vma changes,
3342 * but allow concurrent faults), and pte mapped but not yet locked.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003343 * We return with pte unmapped and unlocked.
3344 *
3345 * The mmap_sem may have been released depending on flags and our
3346 * return value. See filemap_fault() and __lock_page_or_retry().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 */
Kirill A. Shutemovc0292552013-09-12 15:14:05 -07003348static int handle_pte_fault(struct mm_struct *mm,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08003349 struct vm_area_struct *vma, unsigned long address,
3350 pte_t *pte, pmd_t *pmd, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
3352 pte_t entry;
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003353 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
Christian Borntraegere37c6982014-12-07 21:41:33 +01003355 /*
3356 * some architectures can have larger ptes than wordsize,
3357 * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and CONFIG_32BIT=y,
3358 * so READ_ONCE or ACCESS_ONCE cannot guarantee atomic accesses.
3359 * The code below just needs a consistent view for the ifs and
3360 * we later double check anyway with the ptl lock held. So here
3361 * a barrier will do.
3362 */
3363 entry = *pte;
3364 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 if (!pte_present(entry)) {
Hugh Dickins65500d22005-10-29 18:15:59 -07003366 if (pte_none(entry)) {
Oleg Nesterovb5330622015-09-08 14:58:28 -07003367 if (vma_is_anonymous(vma))
3368 return do_anonymous_page(mm, vma, address,
3369 pte, pmd, flags);
3370 else
Kirill A. Shutemov6b7339f2015-07-06 23:18:37 +03003371 return do_fault(mm, vma, address, pte, pmd,
3372 flags, entry);
Hugh Dickins65500d22005-10-29 18:15:59 -07003373 }
Hugh Dickins65500d22005-10-29 18:15:59 -07003374 return do_swap_page(mm, vma, address,
Linus Torvalds30c9f3a2009-04-10 08:43:11 -07003375 pte, pmd, flags, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 }
3377
Mel Gorman8a0516e2015-02-12 14:58:22 -08003378 if (pte_protnone(entry))
Mel Gormand10e63f2012-10-25 14:16:31 +02003379 return do_numa_page(mm, vma, address, entry, pte, pmd);
3380
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07003381 ptl = pte_lockptr(mm, pmd);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003382 spin_lock(ptl);
3383 if (unlikely(!pte_same(*pte, entry)))
3384 goto unlock;
Linus Torvalds30c9f3a2009-04-10 08:43:11 -07003385 if (flags & FAULT_FLAG_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 if (!pte_write(entry))
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003387 return do_wp_page(mm, vma, address,
3388 pte, pmd, ptl, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 entry = pte_mkdirty(entry);
3390 }
3391 entry = pte_mkyoung(entry);
Linus Torvalds30c9f3a2009-04-10 08:43:11 -07003392 if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) {
Russell King4b3073e2009-12-18 16:40:18 +00003393 update_mmu_cache(vma, address, pte);
Andrea Arcangeli1a44e142005-10-29 18:16:48 -07003394 } else {
3395 /*
3396 * This is needed only for protection faults but the arch code
3397 * is not yet telling us if this is a protection fault or not.
3398 * This still avoids useless tlb flushes for .text page faults
3399 * with threads.
3400 */
Linus Torvalds30c9f3a2009-04-10 08:43:11 -07003401 if (flags & FAULT_FLAG_WRITE)
Shaohua Li61c77322010-08-16 09:16:55 +08003402 flush_tlb_fix_spurious_fault(vma, address);
Andrea Arcangeli1a44e142005-10-29 18:16:48 -07003403 }
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003404unlock:
3405 pte_unmap_unlock(pte, ptl);
Nick Piggin83c54072007-07-19 01:47:05 -07003406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407}
3408
3409/*
3410 * By the time we get here, we already hold the mm semaphore
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003411 *
3412 * The mmap_sem may have been released depending on flags and our
3413 * return value. See filemap_fault() and __lock_page_or_retry().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 */
Johannes Weiner519e5242013-09-12 15:13:42 -07003415static int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3416 unsigned long address, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417{
3418 pgd_t *pgd;
3419 pud_t *pud;
3420 pmd_t *pmd;
3421 pte_t *pte;
3422
Dave Hansen1b2ee122016-02-12 13:02:21 -08003423 if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
Dave Hansend61172b2016-02-12 13:02:24 -08003424 flags & FAULT_FLAG_INSTRUCTION,
Dave Hansen1b2ee122016-02-12 13:02:21 -08003425 flags & FAULT_FLAG_REMOTE))
Dave Hansen33a709b2016-02-12 13:02:19 -08003426 return VM_FAULT_SIGSEGV;
3427
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01003428 if (unlikely(is_vm_hugetlb_page(vma)))
Linus Torvalds30c9f3a2009-04-10 08:43:11 -07003429 return hugetlb_fault(mm, vma, address, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 pgd = pgd_offset(mm, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 pud = pud_alloc(mm, pgd, address);
3433 if (!pud)
Hugh Dickinsc74df322005-10-29 18:16:23 -07003434 return VM_FAULT_OOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 pmd = pmd_alloc(mm, pud, address);
3436 if (!pmd)
Hugh Dickinsc74df322005-10-29 18:16:23 -07003437 return VM_FAULT_OOM;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08003438 if (pmd_none(*pmd) && transparent_hugepage_enabled(vma)) {
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07003439 int ret = create_huge_pmd(mm, vma, address, pmd, flags);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -07003440 if (!(ret & VM_FAULT_FALLBACK))
3441 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08003442 } else {
3443 pmd_t orig_pmd = *pmd;
David Rientjes1f1d06c2012-05-29 15:06:23 -07003444 int ret;
3445
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08003446 barrier();
Dan Williams5c7fb562016-01-15 16:56:52 -08003447 if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) {
Will Deacona1dd4502012-12-11 16:01:27 -08003448 unsigned int dirty = flags & FAULT_FLAG_WRITE;
3449
Mel Gorman8a0516e2015-02-12 14:58:22 -08003450 if (pmd_protnone(orig_pmd))
Mel Gorman4daae3b2012-11-02 11:33:45 +00003451 return do_huge_pmd_numa_page(mm, vma, address,
Mel Gormand10e63f2012-10-25 14:16:31 +02003452 orig_pmd, pmd);
3453
Linus Torvalds3d59eeb2012-12-16 14:33:25 -08003454 if (dirty && !pmd_write(orig_pmd)) {
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07003455 ret = wp_huge_pmd(mm, vma, address, pmd,
3456 orig_pmd, flags);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08003457 if (!(ret & VM_FAULT_FALLBACK))
3458 return ret;
Will Deacona1dd4502012-12-11 16:01:27 -08003459 } else {
3460 huge_pmd_set_accessed(mm, vma, address, pmd,
3461 orig_pmd, dirty);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08003462 return 0;
David Rientjes1f1d06c2012-05-29 15:06:23 -07003463 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08003464 }
3465 }
3466
3467 /*
Kirill A. Shutemov3ed3a4f2016-03-17 14:19:11 -07003468 * Use pte_alloc() instead of pte_alloc_map, because we can't
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08003469 * run pte_offset_map on the pmd, if an huge pmd could
3470 * materialize from under us from a different thread.
3471 */
Kirill A. Shutemov3ed3a4f2016-03-17 14:19:11 -07003472 if (unlikely(pte_alloc(mm, pmd, address)))
Hugh Dickinsc74df322005-10-29 18:16:23 -07003473 return VM_FAULT_OOM;
Andrea Arcangeliad33bb02016-02-26 15:19:28 -08003474 /*
3475 * If a huge pmd materialized under us just retry later. Use
3476 * pmd_trans_unstable() instead of pmd_trans_huge() to ensure the pmd
3477 * didn't become pmd_trans_huge under us and then back to pmd_none, as
3478 * a result of MADV_DONTNEED running immediately after a huge pmd fault
3479 * in a different thread of this mm, in turn leading to a misleading
3480 * pmd_trans_huge() retval. All we have to ensure is that it is a
3481 * regular pmd that we can walk with pte_offset_map() and we can do that
3482 * through an atomic read in C, which is what pmd_trans_unstable()
3483 * provides.
3484 */
3485 if (unlikely(pmd_trans_unstable(pmd) || pmd_devmap(*pmd)))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08003486 return 0;
3487 /*
3488 * A regular pmd is established and it can't morph into a huge pmd
3489 * from under us anymore at this point because we hold the mmap_sem
3490 * read mode and khugepaged takes it in write mode. So now it's
3491 * safe to run pte_offset_map().
3492 */
3493 pte = pte_offset_map(pmd, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
Linus Torvalds30c9f3a2009-04-10 08:43:11 -07003495 return handle_pte_fault(mm, vma, address, pte, pmd, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496}
3497
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003498/*
3499 * By the time we get here, we already hold the mm semaphore
3500 *
3501 * The mmap_sem may have been released depending on flags and our
3502 * return value. See filemap_fault() and __lock_page_or_retry().
3503 */
Johannes Weiner519e5242013-09-12 15:13:42 -07003504int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
3505 unsigned long address, unsigned int flags)
3506{
3507 int ret;
3508
3509 __set_current_state(TASK_RUNNING);
3510
3511 count_vm_event(PGFAULT);
3512 mem_cgroup_count_vm_event(mm, PGFAULT);
3513
3514 /* do counter updates before entering really critical section. */
3515 check_sync_rss_stat(current);
3516
3517 /*
3518 * Enable the memcg OOM handling for faults triggered in user
3519 * space. Kernel faults are handled more gracefully.
3520 */
3521 if (flags & FAULT_FLAG_USER)
Johannes Weiner49426422013-10-16 13:46:59 -07003522 mem_cgroup_oom_enable();
Johannes Weiner519e5242013-09-12 15:13:42 -07003523
3524 ret = __handle_mm_fault(mm, vma, address, flags);
3525
Johannes Weiner49426422013-10-16 13:46:59 -07003526 if (flags & FAULT_FLAG_USER) {
3527 mem_cgroup_oom_disable();
3528 /*
3529 * The task may have entered a memcg OOM situation but
3530 * if the allocation error was handled gracefully (no
3531 * VM_FAULT_OOM), there is no need to kill anything.
3532 * Just clean up the OOM state peacefully.
3533 */
3534 if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
3535 mem_cgroup_oom_synchronize(false);
3536 }
Johannes Weiner3812c8c2013-09-12 15:13:44 -07003537
Johannes Weiner519e5242013-09-12 15:13:42 -07003538 return ret;
3539}
Jesse Barnese1d6d012014-12-12 16:55:27 -08003540EXPORT_SYMBOL_GPL(handle_mm_fault);
Johannes Weiner519e5242013-09-12 15:13:42 -07003541
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542#ifndef __PAGETABLE_PUD_FOLDED
3543/*
3544 * Allocate page upper directory.
Hugh Dickins872fec12005-10-29 18:16:21 -07003545 * We've already handled the fast-path in-line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 */
Hugh Dickins1bb36302005-10-29 18:16:22 -07003547int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
Hugh Dickinsc74df322005-10-29 18:16:23 -07003549 pud_t *new = pud_alloc_one(mm, address);
3550 if (!new)
Hugh Dickins1bb36302005-10-29 18:16:22 -07003551 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
Nick Piggin362a61a2008-05-14 06:37:36 +02003553 smp_wmb(); /* See comment in __pte_alloc */
3554
Hugh Dickins872fec12005-10-29 18:16:21 -07003555 spin_lock(&mm->page_table_lock);
Hugh Dickins1bb36302005-10-29 18:16:22 -07003556 if (pgd_present(*pgd)) /* Another has populated it */
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -08003557 pud_free(mm, new);
Hugh Dickins1bb36302005-10-29 18:16:22 -07003558 else
3559 pgd_populate(mm, pgd, new);
Hugh Dickinsc74df322005-10-29 18:16:23 -07003560 spin_unlock(&mm->page_table_lock);
Hugh Dickins1bb36302005-10-29 18:16:22 -07003561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562}
3563#endif /* __PAGETABLE_PUD_FOLDED */
3564
3565#ifndef __PAGETABLE_PMD_FOLDED
3566/*
3567 * Allocate page middle directory.
Hugh Dickins872fec12005-10-29 18:16:21 -07003568 * We've already handled the fast-path in-line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 */
Hugh Dickins1bb36302005-10-29 18:16:22 -07003570int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571{
Hugh Dickinsc74df322005-10-29 18:16:23 -07003572 pmd_t *new = pmd_alloc_one(mm, address);
3573 if (!new)
Hugh Dickins1bb36302005-10-29 18:16:22 -07003574 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
Nick Piggin362a61a2008-05-14 06:37:36 +02003576 smp_wmb(); /* See comment in __pte_alloc */
3577
Hugh Dickins872fec12005-10-29 18:16:21 -07003578 spin_lock(&mm->page_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579#ifndef __ARCH_HAS_4LEVEL_HACK
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08003580 if (!pud_present(*pud)) {
3581 mm_inc_nr_pmds(mm);
Hugh Dickins1bb36302005-10-29 18:16:22 -07003582 pud_populate(mm, pud, new);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08003583 } else /* Another has populated it */
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -08003584 pmd_free(mm, new);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08003585#else
3586 if (!pgd_present(*pud)) {
3587 mm_inc_nr_pmds(mm);
Hugh Dickins1bb36302005-10-29 18:16:22 -07003588 pgd_populate(mm, pud, new);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08003589 } else /* Another has populated it */
3590 pmd_free(mm, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591#endif /* __ARCH_HAS_4LEVEL_HACK */
Hugh Dickinsc74df322005-10-29 18:16:23 -07003592 spin_unlock(&mm->page_table_lock);
Hugh Dickins1bb36302005-10-29 18:16:22 -07003593 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594}
3595#endif /* __PAGETABLE_PMD_FOLDED */
3596
Namhyung Kim1b36ba82010-10-26 14:22:00 -07003597static int __follow_pte(struct mm_struct *mm, unsigned long address,
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07003598 pte_t **ptepp, spinlock_t **ptlp)
3599{
3600 pgd_t *pgd;
3601 pud_t *pud;
3602 pmd_t *pmd;
3603 pte_t *ptep;
3604
3605 pgd = pgd_offset(mm, address);
3606 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
3607 goto out;
3608
3609 pud = pud_offset(pgd, address);
3610 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
3611 goto out;
3612
3613 pmd = pmd_offset(pud, address);
Andrea Arcangelif66055ab2011-01-13 15:46:54 -08003614 VM_BUG_ON(pmd_trans_huge(*pmd));
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07003615 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
3616 goto out;
3617
3618 /* We cannot handle huge page PFN maps. Luckily they don't exist. */
3619 if (pmd_huge(*pmd))
3620 goto out;
3621
3622 ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
3623 if (!ptep)
3624 goto out;
3625 if (!pte_present(*ptep))
3626 goto unlock;
3627 *ptepp = ptep;
3628 return 0;
3629unlock:
3630 pte_unmap_unlock(ptep, *ptlp);
3631out:
3632 return -EINVAL;
3633}
3634
Namhyung Kim1b36ba82010-10-26 14:22:00 -07003635static inline int follow_pte(struct mm_struct *mm, unsigned long address,
3636 pte_t **ptepp, spinlock_t **ptlp)
3637{
3638 int res;
3639
3640 /* (void) is needed to make gcc happy */
3641 (void) __cond_lock(*ptlp,
3642 !(res = __follow_pte(mm, address, ptepp, ptlp)));
3643 return res;
3644}
3645
Johannes Weiner3b6748e2009-06-16 15:32:35 -07003646/**
3647 * follow_pfn - look up PFN at a user virtual address
3648 * @vma: memory mapping
3649 * @address: user virtual address
3650 * @pfn: location to store found PFN
3651 *
3652 * Only IO mappings and raw PFN mappings are allowed.
3653 *
3654 * Returns zero and the pfn at @pfn on success, -ve otherwise.
3655 */
3656int follow_pfn(struct vm_area_struct *vma, unsigned long address,
3657 unsigned long *pfn)
3658{
3659 int ret = -EINVAL;
3660 spinlock_t *ptl;
3661 pte_t *ptep;
3662
3663 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
3664 return ret;
3665
3666 ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
3667 if (ret)
3668 return ret;
3669 *pfn = pte_pfn(*ptep);
3670 pte_unmap_unlock(ptep, ptl);
3671 return 0;
3672}
3673EXPORT_SYMBOL(follow_pfn);
3674
Rik van Riel28b2ee22008-07-23 21:27:05 -07003675#ifdef CONFIG_HAVE_IOREMAP_PROT
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08003676int follow_phys(struct vm_area_struct *vma,
3677 unsigned long address, unsigned int flags,
3678 unsigned long *prot, resource_size_t *phys)
Rik van Riel28b2ee22008-07-23 21:27:05 -07003679{
Johannes Weiner03668a42009-06-16 15:32:34 -07003680 int ret = -EINVAL;
Rik van Riel28b2ee22008-07-23 21:27:05 -07003681 pte_t *ptep, pte;
3682 spinlock_t *ptl;
Rik van Riel28b2ee22008-07-23 21:27:05 -07003683
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08003684 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
3685 goto out;
Rik van Riel28b2ee22008-07-23 21:27:05 -07003686
Johannes Weiner03668a42009-06-16 15:32:34 -07003687 if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08003688 goto out;
Rik van Riel28b2ee22008-07-23 21:27:05 -07003689 pte = *ptep;
Johannes Weiner03668a42009-06-16 15:32:34 -07003690
Rik van Riel28b2ee22008-07-23 21:27:05 -07003691 if ((flags & FOLL_WRITE) && !pte_write(pte))
3692 goto unlock;
Rik van Riel28b2ee22008-07-23 21:27:05 -07003693
3694 *prot = pgprot_val(pte_pgprot(pte));
Johannes Weiner03668a42009-06-16 15:32:34 -07003695 *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
Rik van Riel28b2ee22008-07-23 21:27:05 -07003696
Johannes Weiner03668a42009-06-16 15:32:34 -07003697 ret = 0;
Rik van Riel28b2ee22008-07-23 21:27:05 -07003698unlock:
3699 pte_unmap_unlock(ptep, ptl);
3700out:
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08003701 return ret;
Rik van Riel28b2ee22008-07-23 21:27:05 -07003702}
3703
3704int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
3705 void *buf, int len, int write)
3706{
3707 resource_size_t phys_addr;
3708 unsigned long prot = 0;
KOSAKI Motohiro2bc72732009-01-06 14:39:43 -08003709 void __iomem *maddr;
Rik van Riel28b2ee22008-07-23 21:27:05 -07003710 int offset = addr & (PAGE_SIZE-1);
3711
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08003712 if (follow_phys(vma, addr, write, &prot, &phys_addr))
Rik van Riel28b2ee22008-07-23 21:27:05 -07003713 return -EINVAL;
3714
Grazvydas Ignotas9cb12d72015-02-12 15:00:19 -08003715 maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot);
Rik van Riel28b2ee22008-07-23 21:27:05 -07003716 if (write)
3717 memcpy_toio(maddr + offset, buf, len);
3718 else
3719 memcpy_fromio(buf, maddr + offset, len);
3720 iounmap(maddr);
3721
3722 return len;
3723}
Uwe Kleine-König5a736332013-08-07 13:02:52 +02003724EXPORT_SYMBOL_GPL(generic_access_phys);
Rik van Riel28b2ee22008-07-23 21:27:05 -07003725#endif
3726
David Howells0ec76a12006-09-27 01:50:15 -07003727/*
Stephen Wilson206cb632011-03-13 15:49:19 -04003728 * Access another process' address space as given in mm. If non-NULL, use the
3729 * given task for page fault accounting.
David Howells0ec76a12006-09-27 01:50:15 -07003730 */
Stephen Wilson206cb632011-03-13 15:49:19 -04003731static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
3732 unsigned long addr, void *buf, int len, int write)
David Howells0ec76a12006-09-27 01:50:15 -07003733{
David Howells0ec76a12006-09-27 01:50:15 -07003734 struct vm_area_struct *vma;
David Howells0ec76a12006-09-27 01:50:15 -07003735 void *old_buf = buf;
3736
David Howells0ec76a12006-09-27 01:50:15 -07003737 down_read(&mm->mmap_sem);
Simon Arlott183ff222007-10-20 01:27:18 +02003738 /* ignore errors, just check how much was successfully transferred */
David Howells0ec76a12006-09-27 01:50:15 -07003739 while (len) {
3740 int bytes, ret, offset;
3741 void *maddr;
Rik van Riel28b2ee22008-07-23 21:27:05 -07003742 struct page *page = NULL;
David Howells0ec76a12006-09-27 01:50:15 -07003743
Dave Hansen1e987792016-02-12 13:01:54 -08003744 ret = get_user_pages_remote(tsk, mm, addr, 1,
David Howells0ec76a12006-09-27 01:50:15 -07003745 write, 1, &page, &vma);
Rik van Riel28b2ee22008-07-23 21:27:05 -07003746 if (ret <= 0) {
Rik van Rieldbffcd02014-08-06 16:08:12 -07003747#ifndef CONFIG_HAVE_IOREMAP_PROT
3748 break;
3749#else
Rik van Riel28b2ee22008-07-23 21:27:05 -07003750 /*
3751 * Check if this is a VM_IO | VM_PFNMAP VMA, which
3752 * we can access using slightly different code.
3753 */
Rik van Riel28b2ee22008-07-23 21:27:05 -07003754 vma = find_vma(mm, addr);
Michael Ellermanfe936df2011-04-14 15:22:10 -07003755 if (!vma || vma->vm_start > addr)
Rik van Riel28b2ee22008-07-23 21:27:05 -07003756 break;
3757 if (vma->vm_ops && vma->vm_ops->access)
3758 ret = vma->vm_ops->access(vma, addr, buf,
3759 len, write);
3760 if (ret <= 0)
Rik van Riel28b2ee22008-07-23 21:27:05 -07003761 break;
3762 bytes = ret;
Rik van Rieldbffcd02014-08-06 16:08:12 -07003763#endif
David Howells0ec76a12006-09-27 01:50:15 -07003764 } else {
Rik van Riel28b2ee22008-07-23 21:27:05 -07003765 bytes = len;
3766 offset = addr & (PAGE_SIZE-1);
3767 if (bytes > PAGE_SIZE-offset)
3768 bytes = PAGE_SIZE-offset;
3769
3770 maddr = kmap(page);
3771 if (write) {
3772 copy_to_user_page(vma, page, addr,
3773 maddr + offset, buf, bytes);
3774 set_page_dirty_lock(page);
3775 } else {
3776 copy_from_user_page(vma, page, addr,
3777 buf, maddr + offset, bytes);
3778 }
3779 kunmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003780 put_page(page);
David Howells0ec76a12006-09-27 01:50:15 -07003781 }
David Howells0ec76a12006-09-27 01:50:15 -07003782 len -= bytes;
3783 buf += bytes;
3784 addr += bytes;
3785 }
3786 up_read(&mm->mmap_sem);
David Howells0ec76a12006-09-27 01:50:15 -07003787
3788 return buf - old_buf;
3789}
Andi Kleen03252912008-01-30 13:33:18 +01003790
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04003791/**
Randy Dunlapae91dbf2011-03-26 13:27:01 -07003792 * access_remote_vm - access another process' address space
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04003793 * @mm: the mm_struct of the target address space
3794 * @addr: start address to access
3795 * @buf: source or destination buffer
3796 * @len: number of bytes to transfer
3797 * @write: whether the access is a write
3798 *
3799 * The caller must hold a reference on @mm.
3800 */
3801int access_remote_vm(struct mm_struct *mm, unsigned long addr,
3802 void *buf, int len, int write)
3803{
3804 return __access_remote_vm(NULL, mm, addr, buf, len, write);
3805}
3806
Andi Kleen03252912008-01-30 13:33:18 +01003807/*
Stephen Wilson206cb632011-03-13 15:49:19 -04003808 * Access another process' address space.
3809 * Source/target buffer must be kernel space,
3810 * Do not walk the page table directly, use get_user_pages
3811 */
3812int access_process_vm(struct task_struct *tsk, unsigned long addr,
3813 void *buf, int len, int write)
3814{
3815 struct mm_struct *mm;
3816 int ret;
3817
3818 mm = get_task_mm(tsk);
3819 if (!mm)
3820 return 0;
3821
3822 ret = __access_remote_vm(tsk, mm, addr, buf, len, write);
3823 mmput(mm);
3824
3825 return ret;
3826}
3827
Andi Kleen03252912008-01-30 13:33:18 +01003828/*
3829 * Print the name of a VMA.
3830 */
3831void print_vma_addr(char *prefix, unsigned long ip)
3832{
3833 struct mm_struct *mm = current->mm;
3834 struct vm_area_struct *vma;
3835
Ingo Molnare8bff742008-02-13 20:21:06 +01003836 /*
3837 * Do not print if we are in atomic
3838 * contexts (in exception stacks, etc.):
3839 */
3840 if (preempt_count())
3841 return;
3842
Andi Kleen03252912008-01-30 13:33:18 +01003843 down_read(&mm->mmap_sem);
3844 vma = find_vma(mm, ip);
3845 if (vma && vma->vm_file) {
3846 struct file *f = vma->vm_file;
3847 char *buf = (char *)__get_free_page(GFP_KERNEL);
3848 if (buf) {
Andy Shevchenko2fbc57c2012-12-17 16:01:23 -08003849 char *p;
Andi Kleen03252912008-01-30 13:33:18 +01003850
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02003851 p = file_path(f, buf, PAGE_SIZE);
Andi Kleen03252912008-01-30 13:33:18 +01003852 if (IS_ERR(p))
3853 p = "?";
Andy Shevchenko2fbc57c2012-12-17 16:01:23 -08003854 printk("%s%s[%lx+%lx]", prefix, kbasename(p),
Andi Kleen03252912008-01-30 13:33:18 +01003855 vma->vm_start,
3856 vma->vm_end - vma->vm_start);
3857 free_page((unsigned long)buf);
3858 }
3859 }
Jeff Liu51a07e52012-07-31 16:43:18 -07003860 up_read(&mm->mmap_sem);
Andi Kleen03252912008-01-30 13:33:18 +01003861}
Nick Piggin3ee1afa2008-09-10 13:37:17 +02003862
Michael S. Tsirkin662bbcb2013-05-26 17:32:23 +03003863#if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
David Hildenbrand9ec23532015-05-11 17:52:07 +02003864void __might_fault(const char *file, int line)
Nick Piggin3ee1afa2008-09-10 13:37:17 +02003865{
Peter Zijlstra95156f02009-01-12 13:02:11 +01003866 /*
3867 * Some code (nfs/sunrpc) uses socket ops on kernel memory while
3868 * holding the mmap_sem, this is safe because kernel memory doesn't
3869 * get paged out, therefore we'll never actually fault, and the
3870 * below annotations will generate false positives.
3871 */
3872 if (segment_eq(get_fs(), KERNEL_DS))
3873 return;
David Hildenbrand9ec23532015-05-11 17:52:07 +02003874 if (pagefault_disabled())
Michael S. Tsirkin662bbcb2013-05-26 17:32:23 +03003875 return;
David Hildenbrand9ec23532015-05-11 17:52:07 +02003876 __might_sleep(file, line, 0);
3877#if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
Michael S. Tsirkin662bbcb2013-05-26 17:32:23 +03003878 if (current->mm)
Nick Piggin3ee1afa2008-09-10 13:37:17 +02003879 might_lock_read(&current->mm->mmap_sem);
David Hildenbrand9ec23532015-05-11 17:52:07 +02003880#endif
Nick Piggin3ee1afa2008-09-10 13:37:17 +02003881}
David Hildenbrand9ec23532015-05-11 17:52:07 +02003882EXPORT_SYMBOL(__might_fault);
Nick Piggin3ee1afa2008-09-10 13:37:17 +02003883#endif
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08003884
3885#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
3886static void clear_gigantic_page(struct page *page,
3887 unsigned long addr,
3888 unsigned int pages_per_huge_page)
3889{
3890 int i;
3891 struct page *p = page;
3892
3893 might_sleep();
3894 for (i = 0; i < pages_per_huge_page;
3895 i++, p = mem_map_next(p, page, i)) {
3896 cond_resched();
3897 clear_user_highpage(p, addr + i * PAGE_SIZE);
3898 }
3899}
3900void clear_huge_page(struct page *page,
3901 unsigned long addr, unsigned int pages_per_huge_page)
3902{
3903 int i;
3904
3905 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
3906 clear_gigantic_page(page, addr, pages_per_huge_page);
3907 return;
3908 }
3909
3910 might_sleep();
3911 for (i = 0; i < pages_per_huge_page; i++) {
3912 cond_resched();
3913 clear_user_highpage(page + i, addr + i * PAGE_SIZE);
3914 }
3915}
3916
3917static void copy_user_gigantic_page(struct page *dst, struct page *src,
3918 unsigned long addr,
3919 struct vm_area_struct *vma,
3920 unsigned int pages_per_huge_page)
3921{
3922 int i;
3923 struct page *dst_base = dst;
3924 struct page *src_base = src;
3925
3926 for (i = 0; i < pages_per_huge_page; ) {
3927 cond_resched();
3928 copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
3929
3930 i++;
3931 dst = mem_map_next(dst, dst_base, i);
3932 src = mem_map_next(src, src_base, i);
3933 }
3934}
3935
3936void copy_user_huge_page(struct page *dst, struct page *src,
3937 unsigned long addr, struct vm_area_struct *vma,
3938 unsigned int pages_per_huge_page)
3939{
3940 int i;
3941
3942 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
3943 copy_user_gigantic_page(dst, src, addr, vma,
3944 pages_per_huge_page);
3945 return;
3946 }
3947
3948 might_sleep();
3949 for (i = 0; i < pages_per_huge_page; i++) {
3950 cond_resched();
3951 copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
3952 }
3953}
3954#endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08003955
Olof Johansson40b64ac2013-12-20 14:28:05 -08003956#if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08003957
3958static struct kmem_cache *page_ptl_cachep;
3959
3960void __init ptlock_cache_init(void)
3961{
3962 page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0,
3963 SLAB_PANIC, NULL);
3964}
3965
Peter Zijlstra539edb52013-11-14 14:31:52 -08003966bool ptlock_alloc(struct page *page)
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08003967{
3968 spinlock_t *ptl;
3969
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08003970 ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL);
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08003971 if (!ptl)
3972 return false;
Peter Zijlstra539edb52013-11-14 14:31:52 -08003973 page->ptl = ptl;
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08003974 return true;
3975}
3976
Peter Zijlstra539edb52013-11-14 14:31:52 -08003977void ptlock_free(struct page *page)
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08003978{
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08003979 kmem_cache_free(page_ptl_cachep, page->ptl);
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08003980}
3981#endif