blob: fb9f7737c1ff6f043da92339d74dec588c767d9e [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.
Tobin C Harding166f61b2017-02-24 14:59:01 -080033 * Idea by Alex Bligh (alex@cconcepts.co.uk)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 *
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>
Ingo Molnar6e84f312017-02-08 18:51:29 +010043#include <linux/sched/mm.h>
Ingo Molnarf7ccbae2017-02-08 18:51:30 +010044#include <linux/sched/coredump.h>
Ingo Molnar6a3827d2017-02-08 18:51:31 +010045#include <linux/sched/numa_balancing.h>
Ingo Molnar29930022017-02-08 18:51:36 +010046#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/hugetlb.h>
48#include <linux/mman.h>
49#include <linux/swap.h>
50#include <linux/highmem.h>
51#include <linux/pagemap.h>
Jérôme Glisse5042db42017-09-08 16:11:43 -070052#include <linux/memremap.h>
Hugh Dickins9a840892009-09-21 17:02:01 -070053#include <linux/ksm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/rmap.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040055#include <linux/export.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070056#include <linux/delayacct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/init.h>
Dan Williams01c8f1c2016-01-15 16:56:40 -080058#include <linux/pfn_t.h>
Peter Zijlstraedc79b22006-09-25 23:30:58 -070059#include <linux/writeback.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080060#include <linux/memcontrol.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070061#include <linux/mmu_notifier.h>
Hugh Dickins3dc14742009-01-06 14:40:08 -080062#include <linux/kallsyms.h>
63#include <linux/swapops.h>
64#include <linux/elf.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090065#include <linux/gfp.h>
Mel Gorman4daae3b2012-11-02 11:33:45 +000066#include <linux/migrate.h>
Andy Shevchenko2fbc57c2012-12-17 16:01:23 -080067#include <linux/string.h>
Dan Williams0abdd7a2014-01-21 15:48:12 -080068#include <linux/dma-debug.h>
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -070069#include <linux/debugfs.h>
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -070070#include <linux/userfaultfd_k.h>
Jan Karabc2466e2016-05-12 18:29:19 +020071#include <linux/dax.h>
Michal Hocko6b31d592017-08-18 15:16:15 -070072#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Alexey Dobriyan6952b612009-09-18 23:55:55 +040074#include <asm/io.h>
Dave Hansen33a709b2016-02-12 13:02:19 -080075#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <asm/pgalloc.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080077#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <asm/tlb.h>
79#include <asm/tlbflush.h>
80#include <asm/pgtable.h>
81
Jan Beulich42b77722008-07-23 21:27:10 -070082#include "internal.h"
83
Arnd Bergmann568c61f2018-02-16 16:25:53 +010084#if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST)
Peter Zijlstra90572892013-10-07 11:29:20 +010085#warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
Peter Zijlstra75980e92013-02-22 16:34:32 -080086#endif
87
Andy Whitcroftd41dee32005-06-23 00:07:54 -070088#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* use the per-pgdat data instead for discontigmem - mbligh */
90unsigned long max_mapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091EXPORT_SYMBOL(max_mapnr);
Tobin C Harding166f61b2017-02-24 14:59:01 -080092
93struct page *mem_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094EXPORT_SYMBOL(mem_map);
95#endif
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/*
98 * A number of key systems in x86 including ioremap() rely on the assumption
99 * that high_memory defines the upper bound on direct map memory, then end
100 * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
101 * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
102 * and ZONE_HIGHMEM.
103 */
Tobin C Harding166f61b2017-02-24 14:59:01 -0800104void *high_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105EXPORT_SYMBOL(high_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Ingo Molnar32a93232008-02-06 22:39:44 +0100107/*
108 * Randomize the address space (stacks, mmaps, brk, etc.).
109 *
110 * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
111 * as ancient (libc5 based) binaries can segfault. )
112 */
113int randomize_va_space __read_mostly =
114#ifdef CONFIG_COMPAT_BRK
115 1;
116#else
117 2;
118#endif
Andi Kleena62eaf12006-02-16 23:41:58 +0100119
120static int __init disable_randmaps(char *s)
121{
122 randomize_va_space = 0;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800123 return 1;
Andi Kleena62eaf12006-02-16 23:41:58 +0100124}
125__setup("norandmaps", disable_randmaps);
126
Hugh Dickins62eede62009-09-21 17:03:34 -0700127unsigned long zero_pfn __read_mostly;
Ard Biesheuvel0b700682014-09-12 22:17:23 +0200128EXPORT_SYMBOL(zero_pfn);
129
Tobin C Harding166f61b2017-02-24 14:59:01 -0800130unsigned long highest_memmap_pfn __read_mostly;
131
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -0700132/*
133 * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
134 */
135static int __init init_zero_pfn(void)
136{
137 zero_pfn = page_to_pfn(ZERO_PAGE(0));
138 return 0;
139}
140core_initcall(init_zero_pfn);
Andi Kleena62eaf12006-02-16 23:41:58 +0100141
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800142
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800143#if defined(SPLIT_RSS_COUNTING)
144
David Rientjesea48cf72012-03-21 16:34:13 -0700145void sync_mm_rss(struct mm_struct *mm)
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800146{
147 int i;
148
149 for (i = 0; i < NR_MM_COUNTERS; i++) {
David Rientjes05af2e12012-03-21 16:34:13 -0700150 if (current->rss_stat.count[i]) {
151 add_mm_counter(mm, i, current->rss_stat.count[i]);
152 current->rss_stat.count[i] = 0;
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800153 }
154 }
David Rientjes05af2e12012-03-21 16:34:13 -0700155 current->rss_stat.events = 0;
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800156}
157
158static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
159{
160 struct task_struct *task = current;
161
162 if (likely(task->mm == mm))
163 task->rss_stat.count[member] += val;
164 else
165 add_mm_counter(mm, member, val);
166}
167#define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
168#define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
169
170/* sync counter once per 64 page faults */
171#define TASK_RSS_EVENTS_THRESH (64)
172static void check_sync_rss_stat(struct task_struct *task)
173{
174 if (unlikely(task != current))
175 return;
176 if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
David Rientjesea48cf72012-03-21 16:34:13 -0700177 sync_mm_rss(task->mm);
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800178}
Peter Zijlstra9547d012011-05-24 17:12:14 -0700179#else /* SPLIT_RSS_COUNTING */
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800180
181#define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
182#define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
183
184static void check_sync_rss_stat(struct task_struct *task)
185{
186}
187
Peter Zijlstra9547d012011-05-24 17:12:14 -0700188#endif /* SPLIT_RSS_COUNTING */
189
190#ifdef HAVE_GENERIC_MMU_GATHER
191
Nicholas Krauseca1d6c72015-09-04 15:48:22 -0700192static bool tlb_next_batch(struct mmu_gather *tlb)
Peter Zijlstra9547d012011-05-24 17:12:14 -0700193{
194 struct mmu_gather_batch *batch;
195
196 batch = tlb->active;
197 if (batch->next) {
198 tlb->active = batch->next;
Nicholas Krauseca1d6c72015-09-04 15:48:22 -0700199 return true;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700200 }
201
Michal Hocko53a59fc2013-01-04 15:35:12 -0800202 if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
Nicholas Krauseca1d6c72015-09-04 15:48:22 -0700203 return false;
Michal Hocko53a59fc2013-01-04 15:35:12 -0800204
Peter Zijlstra9547d012011-05-24 17:12:14 -0700205 batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
206 if (!batch)
Nicholas Krauseca1d6c72015-09-04 15:48:22 -0700207 return false;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700208
Michal Hocko53a59fc2013-01-04 15:35:12 -0800209 tlb->batch_count++;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700210 batch->next = NULL;
211 batch->nr = 0;
212 batch->max = MAX_GATHER_BATCH;
213
214 tlb->active->next = batch;
215 tlb->active = batch;
216
Nicholas Krauseca1d6c72015-09-04 15:48:22 -0700217 return true;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700218}
219
Minchan Kim56236a52017-08-10 15:24:05 -0700220void arch_tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
221 unsigned long start, unsigned long end)
Peter Zijlstra9547d012011-05-24 17:12:14 -0700222{
223 tlb->mm = mm;
224
Linus Torvalds2b047252013-08-15 11:42:25 -0700225 /* Is it from 0 to ~0? */
226 tlb->fullmm = !(start | (end+1));
Dave Hansen1de14c32013-04-12 16:23:54 -0700227 tlb->need_flush_all = 0;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700228 tlb->local.next = NULL;
229 tlb->local.nr = 0;
230 tlb->local.max = ARRAY_SIZE(tlb->__pages);
231 tlb->active = &tlb->local;
Michal Hocko53a59fc2013-01-04 15:35:12 -0800232 tlb->batch_count = 0;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700233
234#ifdef CONFIG_HAVE_RCU_TABLE_FREE
235 tlb->batch = NULL;
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800236#endif
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -0700237 tlb->page_size = 0;
Will Deaconfb7332a2014-10-29 10:03:09 +0000238
239 __tlb_reset_range(tlb);
Peter Zijlstra9547d012011-05-24 17:12:14 -0700240}
241
Linus Torvalds1cf35d42014-04-25 16:05:40 -0700242static void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
Peter Zijlstra9547d012011-05-24 17:12:14 -0700243{
Will Deacon721c21c2015-01-12 19:10:55 +0000244 if (!tlb->end)
245 return;
246
Peter Zijlstra9547d012011-05-24 17:12:14 -0700247 tlb_flush(tlb);
Joerg Roedel34ee6452014-11-13 13:46:09 +1100248 mmu_notifier_invalidate_range(tlb->mm, tlb->start, tlb->end);
Will Deaconfb7332a2014-10-29 10:03:09 +0000249 __tlb_reset_range(tlb);
Linus Torvalds1cf35d42014-04-25 16:05:40 -0700250}
251
252static void tlb_flush_mmu_free(struct mmu_gather *tlb)
253{
254 struct mmu_gather_batch *batch;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700255
Nicholas Piggin3e099462018-08-23 18:47:08 +1000256#ifdef CONFIG_HAVE_RCU_TABLE_FREE
257 tlb_table_flush(tlb);
258#endif
Will Deacon721c21c2015-01-12 19:10:55 +0000259 for (batch = &tlb->local; batch && batch->nr; batch = batch->next) {
Peter Zijlstra9547d012011-05-24 17:12:14 -0700260 free_pages_and_swap_cache(batch->pages, batch->nr);
261 batch->nr = 0;
262 }
263 tlb->active = &tlb->local;
264}
265
Linus Torvalds1cf35d42014-04-25 16:05:40 -0700266void tlb_flush_mmu(struct mmu_gather *tlb)
267{
Linus Torvalds1cf35d42014-04-25 16:05:40 -0700268 tlb_flush_mmu_tlbonly(tlb);
269 tlb_flush_mmu_free(tlb);
270}
271
Peter Zijlstra9547d012011-05-24 17:12:14 -0700272/* tlb_finish_mmu
273 * Called at the end of the shootdown operation to free up any resources
274 * that were required.
275 */
Minchan Kim56236a52017-08-10 15:24:05 -0700276void arch_tlb_finish_mmu(struct mmu_gather *tlb,
Minchan Kim99baac22017-08-10 15:24:12 -0700277 unsigned long start, unsigned long end, bool force)
Peter Zijlstra9547d012011-05-24 17:12:14 -0700278{
279 struct mmu_gather_batch *batch, *next;
280
Minchan Kim99baac22017-08-10 15:24:12 -0700281 if (force)
282 __tlb_adjust_range(tlb, start, end - start);
283
Peter Zijlstra9547d012011-05-24 17:12:14 -0700284 tlb_flush_mmu(tlb);
285
286 /* keep the page table cache within bounds */
287 check_pgt_cache();
288
289 for (batch = tlb->local.next; batch; batch = next) {
290 next = batch->next;
291 free_pages((unsigned long)batch, 0);
292 }
293 tlb->local.next = NULL;
294}
295
296/* __tlb_remove_page
297 * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
298 * handling the additional races in SMP caused by other CPUs caching valid
299 * mappings in their TLBs. Returns the number of free page slots left.
300 * When out of page slots we must call tlb_flush_mmu().
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -0700301 *returns true if the caller should flush.
Peter Zijlstra9547d012011-05-24 17:12:14 -0700302 */
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -0700303bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size)
Peter Zijlstra9547d012011-05-24 17:12:14 -0700304{
305 struct mmu_gather_batch *batch;
306
Will Deaconfb7332a2014-10-29 10:03:09 +0000307 VM_BUG_ON(!tlb->end);
Aneesh Kumar K.V692a68c2016-12-12 16:42:43 -0800308 VM_WARN_ON(tlb->page_size != page_size);
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -0700309
Peter Zijlstra9547d012011-05-24 17:12:14 -0700310 batch = tlb->active;
Aneesh Kumar K.V692a68c2016-12-12 16:42:43 -0800311 /*
312 * Add the page and check if we are full. If so
313 * force a flush.
314 */
315 batch->pages[batch->nr++] = page;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700316 if (batch->nr == batch->max) {
317 if (!tlb_next_batch(tlb))
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -0700318 return true;
Shaohua Li0b43c3a2011-07-08 15:39:41 -0700319 batch = tlb->active;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700320 }
Sasha Levin309381fea2014-01-23 15:52:54 -0800321 VM_BUG_ON_PAGE(batch->nr > batch->max, page);
Peter Zijlstra9547d012011-05-24 17:12:14 -0700322
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -0700323 return false;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700324}
325
326#endif /* HAVE_GENERIC_MMU_GATHER */
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800327
Peter Zijlstra26723912011-05-24 17:12:00 -0700328#ifdef CONFIG_HAVE_RCU_TABLE_FREE
329
330/*
331 * See the comment near struct mmu_table_batch.
332 */
333
Peter Zijlstrae9afa7c2018-08-22 17:30:15 +0200334/*
335 * If we want tlb_remove_table() to imply TLB invalidates.
336 */
337static inline void tlb_table_invalidate(struct mmu_gather *tlb)
338{
339#ifdef CONFIG_HAVE_RCU_TABLE_INVALIDATE
340 /*
341 * Invalidate page-table caches used by hardware walkers. Then we still
342 * need to RCU-sched wait while freeing the pages because software
343 * walkers can still be in-flight.
344 */
345 tlb_flush_mmu_tlbonly(tlb);
346#endif
347}
348
Peter Zijlstra26723912011-05-24 17:12:00 -0700349static void tlb_remove_table_smp_sync(void *arg)
350{
351 /* Simply deliver the interrupt */
352}
353
354static void tlb_remove_table_one(void *table)
355{
356 /*
357 * This isn't an RCU grace period and hence the page-tables cannot be
358 * assumed to be actually RCU-freed.
359 *
360 * It is however sufficient for software page-table walkers that rely on
361 * IRQ disabling. See the comment near struct mmu_table_batch.
362 */
363 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
364 __tlb_remove_table(table);
365}
366
367static void tlb_remove_table_rcu(struct rcu_head *head)
368{
369 struct mmu_table_batch *batch;
370 int i;
371
372 batch = container_of(head, struct mmu_table_batch, rcu);
373
374 for (i = 0; i < batch->nr; i++)
375 __tlb_remove_table(batch->tables[i]);
376
377 free_page((unsigned long)batch);
378}
379
380void tlb_table_flush(struct mmu_gather *tlb)
381{
382 struct mmu_table_batch **batch = &tlb->batch;
383
384 if (*batch) {
Peter Zijlstrae9afa7c2018-08-22 17:30:15 +0200385 tlb_table_invalidate(tlb);
Peter Zijlstra26723912011-05-24 17:12:00 -0700386 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
387 *batch = NULL;
388 }
389}
390
391void tlb_remove_table(struct mmu_gather *tlb, void *table)
392{
393 struct mmu_table_batch **batch = &tlb->batch;
394
Peter Zijlstra26723912011-05-24 17:12:00 -0700395 if (*batch == NULL) {
396 *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
397 if (*batch == NULL) {
Peter Zijlstrae9afa7c2018-08-22 17:30:15 +0200398 tlb_table_invalidate(tlb);
Peter Zijlstra26723912011-05-24 17:12:00 -0700399 tlb_remove_table_one(table);
400 return;
401 }
402 (*batch)->nr = 0;
403 }
Peter Zijlstrae9afa7c2018-08-22 17:30:15 +0200404
Peter Zijlstra26723912011-05-24 17:12:00 -0700405 (*batch)->tables[(*batch)->nr++] = table;
406 if ((*batch)->nr == MAX_TABLE_BATCH)
407 tlb_table_flush(tlb);
408}
409
Peter Zijlstra9547d012011-05-24 17:12:14 -0700410#endif /* CONFIG_HAVE_RCU_TABLE_FREE */
Peter Zijlstra26723912011-05-24 17:12:00 -0700411
Minchan Kim56236a52017-08-10 15:24:05 -0700412/* tlb_gather_mmu
413 * Called to initialize an (on-stack) mmu_gather structure for page-table
414 * tear-down from @mm. The @fullmm argument is used when @mm is without
415 * users and we're going to destroy the full address space (exit/execve).
416 */
417void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
418 unsigned long start, unsigned long end)
419{
420 arch_tlb_gather_mmu(tlb, mm, start, end);
Minchan Kim99baac22017-08-10 15:24:12 -0700421 inc_tlb_flush_pending(tlb->mm);
Minchan Kim56236a52017-08-10 15:24:05 -0700422}
423
424void tlb_finish_mmu(struct mmu_gather *tlb,
425 unsigned long start, unsigned long end)
426{
Minchan Kim99baac22017-08-10 15:24:12 -0700427 /*
428 * If there are parallel threads are doing PTE changes on same range
429 * under non-exclusive lock(e.g., mmap_sem read-side) but defer TLB
430 * flush by batching, a thread has stable TLB entry can fail to flush
431 * the TLB by observing pte_none|!pte_dirty, for example so flush TLB
432 * forcefully if we detect parallel PTE batching threads.
433 */
434 bool force = mm_tlb_flush_nested(tlb->mm);
435
436 arch_tlb_finish_mmu(tlb, start, end, force);
437 dec_tlb_flush_pending(tlb->mm);
Minchan Kim56236a52017-08-10 15:24:05 -0700438}
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 * Note: this doesn't free the actual pages themselves. That
442 * has been handled earlier when unmapping all the memory regions.
443 */
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000444static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
445 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800447 pgtable_t token = pmd_pgtable(*pmd);
Hugh Dickinse0da3822005-04-19 13:29:15 -0700448 pmd_clear(pmd);
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000449 pte_free_tlb(tlb, token, addr);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800450 atomic_long_dec(&tlb->mm->nr_ptes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Hugh Dickinse0da3822005-04-19 13:29:15 -0700453static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
454 unsigned long addr, unsigned long end,
455 unsigned long floor, unsigned long ceiling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 pmd_t *pmd;
458 unsigned long next;
Hugh Dickinse0da3822005-04-19 13:29:15 -0700459 unsigned long start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Hugh Dickinse0da3822005-04-19 13:29:15 -0700461 start = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 pmd = pmd_offset(pud, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 do {
464 next = pmd_addr_end(addr, end);
465 if (pmd_none_or_clear_bad(pmd))
466 continue;
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000467 free_pte_range(tlb, pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 } while (pmd++, addr = next, addr != end);
469
Hugh Dickinse0da3822005-04-19 13:29:15 -0700470 start &= PUD_MASK;
471 if (start < floor)
472 return;
473 if (ceiling) {
474 ceiling &= PUD_MASK;
475 if (!ceiling)
476 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Hugh Dickinse0da3822005-04-19 13:29:15 -0700478 if (end - 1 > ceiling - 1)
479 return;
480
481 pmd = pmd_offset(pud, start);
482 pud_clear(pud);
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000483 pmd_free_tlb(tlb, pmd, start);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800484 mm_dec_nr_pmds(tlb->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300487static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d,
Hugh Dickinse0da3822005-04-19 13:29:15 -0700488 unsigned long addr, unsigned long end,
489 unsigned long floor, unsigned long ceiling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 pud_t *pud;
492 unsigned long next;
Hugh Dickinse0da3822005-04-19 13:29:15 -0700493 unsigned long start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Hugh Dickinse0da3822005-04-19 13:29:15 -0700495 start = addr;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300496 pud = pud_offset(p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 do {
498 next = pud_addr_end(addr, end);
499 if (pud_none_or_clear_bad(pud))
500 continue;
Hugh Dickinse0da3822005-04-19 13:29:15 -0700501 free_pmd_range(tlb, pud, addr, next, floor, ceiling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 } while (pud++, addr = next, addr != end);
503
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300504 start &= P4D_MASK;
505 if (start < floor)
506 return;
507 if (ceiling) {
508 ceiling &= P4D_MASK;
509 if (!ceiling)
510 return;
511 }
512 if (end - 1 > ceiling - 1)
513 return;
514
515 pud = pud_offset(p4d, start);
516 p4d_clear(p4d);
517 pud_free_tlb(tlb, pud, start);
518}
519
520static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd,
521 unsigned long addr, unsigned long end,
522 unsigned long floor, unsigned long ceiling)
523{
524 p4d_t *p4d;
525 unsigned long next;
526 unsigned long start;
527
528 start = addr;
529 p4d = p4d_offset(pgd, addr);
530 do {
531 next = p4d_addr_end(addr, end);
532 if (p4d_none_or_clear_bad(p4d))
533 continue;
534 free_pud_range(tlb, p4d, addr, next, floor, ceiling);
535 } while (p4d++, addr = next, addr != end);
536
Hugh Dickinse0da3822005-04-19 13:29:15 -0700537 start &= PGDIR_MASK;
538 if (start < floor)
539 return;
540 if (ceiling) {
541 ceiling &= PGDIR_MASK;
542 if (!ceiling)
543 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
Hugh Dickinse0da3822005-04-19 13:29:15 -0700545 if (end - 1 > ceiling - 1)
546 return;
547
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300548 p4d = p4d_offset(pgd, start);
Hugh Dickinse0da3822005-04-19 13:29:15 -0700549 pgd_clear(pgd);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300550 p4d_free_tlb(tlb, p4d, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553/*
Hugh Dickinse0da3822005-04-19 13:29:15 -0700554 * This function frees user-level page tables of a process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 */
Jan Beulich42b77722008-07-23 21:27:10 -0700556void free_pgd_range(struct mmu_gather *tlb,
Hugh Dickinse0da3822005-04-19 13:29:15 -0700557 unsigned long addr, unsigned long end,
558 unsigned long floor, unsigned long ceiling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 pgd_t *pgd;
561 unsigned long next;
562
Hugh Dickinse0da3822005-04-19 13:29:15 -0700563 /*
564 * The next few lines have given us lots of grief...
565 *
566 * Why are we testing PMD* at this top level? Because often
567 * there will be no work to do at all, and we'd prefer not to
568 * go all the way down to the bottom just to discover that.
569 *
570 * Why all these "- 1"s? Because 0 represents both the bottom
571 * of the address space and the top of it (using -1 for the
572 * top wouldn't help much: the masks would do the wrong thing).
573 * The rule is that addr 0 and floor 0 refer to the bottom of
574 * the address space, but end 0 and ceiling 0 refer to the top
575 * Comparisons need to use "end - 1" and "ceiling - 1" (though
576 * that end 0 case should be mythical).
577 *
578 * Wherever addr is brought up or ceiling brought down, we must
579 * be careful to reject "the opposite 0" before it confuses the
580 * subsequent tests. But what about where end is brought down
581 * by PMD_SIZE below? no, end can't go down to 0 there.
582 *
583 * Whereas we round start (addr) and ceiling down, by different
584 * masks at different levels, in order to test whether a table
585 * now has no other vmas using it, so can be freed, we don't
586 * bother to round floor or end up - the tests don't need that.
587 */
588
589 addr &= PMD_MASK;
590 if (addr < floor) {
591 addr += PMD_SIZE;
592 if (!addr)
593 return;
594 }
595 if (ceiling) {
596 ceiling &= PMD_MASK;
597 if (!ceiling)
598 return;
599 }
600 if (end - 1 > ceiling - 1)
601 end -= PMD_SIZE;
602 if (addr > end - 1)
603 return;
Aneesh Kumar K.V07e32662016-12-12 16:42:40 -0800604 /*
605 * We add page table cache pages with PAGE_SIZE,
606 * (see pte_free_tlb()), flush the tlb if we need
607 */
608 tlb_remove_check_page_size_change(tlb, PAGE_SIZE);
Jan Beulich42b77722008-07-23 21:27:10 -0700609 pgd = pgd_offset(tlb->mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 do {
611 next = pgd_addr_end(addr, end);
612 if (pgd_none_or_clear_bad(pgd))
613 continue;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300614 free_p4d_range(tlb, pgd, addr, next, floor, ceiling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 } while (pgd++, addr = next, addr != end);
Hugh Dickinse0da3822005-04-19 13:29:15 -0700616}
617
Jan Beulich42b77722008-07-23 21:27:10 -0700618void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700619 unsigned long floor, unsigned long ceiling)
Hugh Dickinse0da3822005-04-19 13:29:15 -0700620{
621 while (vma) {
622 struct vm_area_struct *next = vma->vm_next;
623 unsigned long addr = vma->vm_start;
624
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700625 /*
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +1000626 * Hide vma from rmap and truncate_pagecache before freeing
627 * pgtables
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700628 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800629 unlink_anon_vmas(vma);
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700630 unlink_file_vma(vma);
631
David Gibson9da61ae2006-03-22 00:08:57 -0800632 if (is_vm_hugetlb_page(vma)) {
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700633 hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
Tobin C Harding166f61b2017-02-24 14:59:01 -0800634 floor, next ? next->vm_start : ceiling);
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700635 } else {
636 /*
637 * Optimization: gather nearby vmas into one call down
638 */
639 while (next && next->vm_start <= vma->vm_end + PMD_SIZE
David Gibson48669202006-03-22 00:08:58 -0800640 && !is_vm_hugetlb_page(next)) {
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700641 vma = next;
642 next = vma->vm_next;
Rik van Riel5beb4932010-03-05 13:42:07 -0800643 unlink_anon_vmas(vma);
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700644 unlink_file_vma(vma);
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700645 }
646 free_pgd_range(tlb, addr, vma->vm_end,
Tobin C Harding166f61b2017-02-24 14:59:01 -0800647 floor, next ? next->vm_start : ceiling);
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700648 }
Hugh Dickinse0da3822005-04-19 13:29:15 -0700649 vma = next;
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Kirill A. Shutemov3ed3a4f2016-03-17 14:19:11 -0700653int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800655 spinlock_t *ptl;
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800656 pgtable_t new = pte_alloc_one(mm, address);
Hugh Dickins1bb36302005-10-29 18:16:22 -0700657 if (!new)
658 return -ENOMEM;
659
Nick Piggin362a61a2008-05-14 06:37:36 +0200660 /*
661 * Ensure all pte setup (eg. pte page lock and page clearing) are
662 * visible before the pte is made visible to other CPUs by being
663 * put into page tables.
664 *
665 * The other side of the story is the pointer chasing in the page
666 * table walking code (when walking the page table without locking;
667 * ie. most of the time). Fortunately, these data accesses consist
668 * of a chain of data-dependent loads, meaning most CPUs (alpha
669 * being the notable exception) will already guarantee loads are
670 * seen in-order. See the alpha page table accessors for the
671 * smp_read_barrier_depends() barriers in page table walking code.
672 */
673 smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
674
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800675 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli8ac1f832011-01-13 15:46:43 -0800676 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800677 atomic_long_inc(&mm->nr_ptes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 pmd_populate(mm, pmd, new);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800679 new = NULL;
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -0800680 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800681 spin_unlock(ptl);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800682 if (new)
683 pte_free(mm, new);
Hugh Dickins1bb36302005-10-29 18:16:22 -0700684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Hugh Dickins1bb36302005-10-29 18:16:22 -0700687int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Hugh Dickins1bb36302005-10-29 18:16:22 -0700689 pte_t *new = pte_alloc_one_kernel(&init_mm, address);
690 if (!new)
691 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Nick Piggin362a61a2008-05-14 06:37:36 +0200693 smp_wmb(); /* See comment in __pte_alloc */
694
Hugh Dickins1bb36302005-10-29 18:16:22 -0700695 spin_lock(&init_mm.page_table_lock);
Andrea Arcangeli8ac1f832011-01-13 15:46:43 -0800696 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
Hugh Dickins1bb36302005-10-29 18:16:22 -0700697 pmd_populate_kernel(&init_mm, pmd, new);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800698 new = NULL;
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -0800699 }
Hugh Dickins1bb36302005-10-29 18:16:22 -0700700 spin_unlock(&init_mm.page_table_lock);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800701 if (new)
702 pte_free_kernel(&init_mm, new);
Hugh Dickins1bb36302005-10-29 18:16:22 -0700703 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800706static inline void init_rss_vec(int *rss)
Hugh Dickinsae859762005-10-29 18:16:05 -0700707{
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800708 memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
709}
710
711static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
712{
713 int i;
714
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800715 if (current->mm == mm)
David Rientjes05af2e12012-03-21 16:34:13 -0700716 sync_mm_rss(mm);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800717 for (i = 0; i < NR_MM_COUNTERS; i++)
718 if (rss[i])
719 add_mm_counter(mm, i, rss[i]);
Hugh Dickinsae859762005-10-29 18:16:05 -0700720}
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722/*
Linus Torvalds6aab3412005-11-28 14:34:23 -0800723 * This function is called to print an error when a bad pte
724 * is found. For example, we might have a PFN-mapped pte in
725 * a region that doesn't allow it.
Nick Pigginb5810032005-10-29 18:16:12 -0700726 *
727 * The calling function must still handle the error.
728 */
Hugh Dickins3dc14742009-01-06 14:40:08 -0800729static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
730 pte_t pte, struct page *page)
Nick Pigginb5810032005-10-29 18:16:12 -0700731{
Hugh Dickins3dc14742009-01-06 14:40:08 -0800732 pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300733 p4d_t *p4d = p4d_offset(pgd, addr);
734 pud_t *pud = pud_offset(p4d, addr);
Hugh Dickins3dc14742009-01-06 14:40:08 -0800735 pmd_t *pmd = pmd_offset(pud, addr);
736 struct address_space *mapping;
737 pgoff_t index;
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800738 static unsigned long resume;
739 static unsigned long nr_shown;
740 static unsigned long nr_unshown;
741
742 /*
743 * Allow a burst of 60 reports, then keep quiet for that minute;
744 * or allow a steady drip of one report per second.
745 */
746 if (nr_shown == 60) {
747 if (time_before(jiffies, resume)) {
748 nr_unshown++;
749 return;
750 }
751 if (nr_unshown) {
Joe Perches11705322016-03-17 14:19:50 -0700752 pr_alert("BUG: Bad page map: %lu messages suppressed\n",
753 nr_unshown);
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800754 nr_unshown = 0;
755 }
756 nr_shown = 0;
757 }
758 if (nr_shown++ == 0)
759 resume = jiffies + 60 * HZ;
Hugh Dickins3dc14742009-01-06 14:40:08 -0800760
761 mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
762 index = linear_page_index(vma, addr);
763
Joe Perches11705322016-03-17 14:19:50 -0700764 pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n",
765 current->comm,
766 (long long)pte_val(pte), (long long)pmd_val(*pmd));
Wu Fengguang718a3822010-03-10 15:20:43 -0800767 if (page)
Dave Hansenf0b791a2014-01-23 15:52:49 -0800768 dump_page(page, "bad pte");
Joe Perches11705322016-03-17 14:19:50 -0700769 pr_alert("addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n",
770 (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
Hugh Dickins3dc14742009-01-06 14:40:08 -0800771 /*
772 * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y
773 */
Konstantin Khlebnikov26825822015-04-15 16:15:08 -0700774 pr_alert("file:%pD fault:%pf mmap:%pf readpage:%pf\n",
775 vma->vm_file,
776 vma->vm_ops ? vma->vm_ops->fault : NULL,
777 vma->vm_file ? vma->vm_file->f_op->mmap : NULL,
778 mapping ? mapping->a_ops->readpage : NULL);
Nick Pigginb5810032005-10-29 18:16:12 -0700779 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +1030780 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Nick Pigginb5810032005-10-29 18:16:12 -0700781}
782
783/*
Nick Piggin7e675132008-04-28 02:13:00 -0700784 * vm_normal_page -- This function gets the "struct page" associated with a pte.
Linus Torvalds6aab3412005-11-28 14:34:23 -0800785 *
Nick Piggin7e675132008-04-28 02:13:00 -0700786 * "Special" mappings do not wish to be associated with a "struct page" (either
787 * it doesn't exist, or it exists but they don't want to touch it). In this
788 * case, NULL is returned here. "Normal" mappings do have a struct page.
Jared Hulbertb379d792008-04-28 02:12:58 -0700789 *
Nick Piggin7e675132008-04-28 02:13:00 -0700790 * There are 2 broad cases. Firstly, an architecture may define a pte_special()
791 * pte bit, in which case this function is trivial. Secondly, an architecture
792 * may not have a spare pte bit, which requires a more complicated scheme,
793 * described below.
794 *
795 * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
796 * special mapping (even if there are underlying and valid "struct pages").
797 * COWed pages of a VM_PFNMAP are always normal.
Linus Torvalds6aab3412005-11-28 14:34:23 -0800798 *
Jared Hulbertb379d792008-04-28 02:12:58 -0700799 * The way we recognize COWed pages within VM_PFNMAP mappings is through the
800 * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
Nick Piggin7e675132008-04-28 02:13:00 -0700801 * set, and the vm_pgoff will point to the first PFN mapped: thus every special
802 * mapping will always honor the rule
Linus Torvalds6aab3412005-11-28 14:34:23 -0800803 *
804 * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
805 *
Nick Piggin7e675132008-04-28 02:13:00 -0700806 * And for normal mappings this is false.
Jared Hulbertb379d792008-04-28 02:12:58 -0700807 *
Nick Piggin7e675132008-04-28 02:13:00 -0700808 * This restricts such mappings to be a linear translation from virtual address
809 * to pfn. To get around this restriction, we allow arbitrary mappings so long
810 * as the vma is not a COW mapping; in that case, we know that all ptes are
811 * special (because none can have been COWed).
Jared Hulbertb379d792008-04-28 02:12:58 -0700812 *
813 *
Nick Piggin7e675132008-04-28 02:13:00 -0700814 * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
815 *
Jared Hulbertb379d792008-04-28 02:12:58 -0700816 * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
817 * page" backing, however the difference is that _all_ pages with a struct
818 * page (that is, those where pfn_valid is true) are refcounted and considered
819 * normal pages by the VM. The disadvantage is that pages are refcounted
820 * (which can be slower and simply not an option for some PFNMAP users). The
821 * advantage is that we don't have to follow the strict linearity rule of
822 * PFNMAP mappings in order to support COWable mappings.
823 *
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800824 */
Nick Piggin7e675132008-04-28 02:13:00 -0700825#ifdef __HAVE_ARCH_PTE_SPECIAL
826# define HAVE_PTE_SPECIAL 1
827#else
828# define HAVE_PTE_SPECIAL 0
829#endif
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700830struct page *_vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
831 pte_t pte, bool with_public_device)
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800832{
Hugh Dickins22b31ee2009-01-06 14:40:09 -0800833 unsigned long pfn = pte_pfn(pte);
Nick Piggin7e675132008-04-28 02:13:00 -0700834
835 if (HAVE_PTE_SPECIAL) {
Hugh Dickinsb38af472014-08-29 15:18:44 -0700836 if (likely(!pte_special(pte)))
Hugh Dickins22b31ee2009-01-06 14:40:09 -0800837 goto check_pfn;
David Vrabel667a0a02014-12-18 14:48:15 +0000838 if (vma->vm_ops && vma->vm_ops->find_special_page)
839 return vma->vm_ops->find_special_page(vma, addr);
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -0700840 if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
841 return NULL;
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700842 if (is_zero_pfn(pfn))
843 return NULL;
844
845 /*
846 * Device public pages are special pages (they are ZONE_DEVICE
847 * pages but different from persistent memory). They behave
848 * allmost like normal pages. The difference is that they are
849 * not on the lru and thus should never be involve with any-
850 * thing that involve lru manipulation (mlock, numa balancing,
851 * ...).
852 *
853 * This is why we still want to return NULL for such page from
854 * vm_normal_page() so that we do not have to special case all
855 * call site of vm_normal_page().
856 */
Reza Arbab7d790d22017-10-03 16:15:35 -0700857 if (likely(pfn <= highest_memmap_pfn)) {
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700858 struct page *page = pfn_to_page(pfn);
859
860 if (is_device_public_page(page)) {
861 if (with_public_device)
862 return page;
863 return NULL;
864 }
865 }
866 print_bad_pte(vma, addr, pte, NULL);
Nick Piggin7e675132008-04-28 02:13:00 -0700867 return NULL;
868 }
869
870 /* !HAVE_PTE_SPECIAL case follows: */
871
Jared Hulbertb379d792008-04-28 02:12:58 -0700872 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
873 if (vma->vm_flags & VM_MIXEDMAP) {
874 if (!pfn_valid(pfn))
875 return NULL;
876 goto out;
877 } else {
Nick Piggin7e675132008-04-28 02:13:00 -0700878 unsigned long off;
879 off = (addr - vma->vm_start) >> PAGE_SHIFT;
Jared Hulbertb379d792008-04-28 02:12:58 -0700880 if (pfn == vma->vm_pgoff + off)
881 return NULL;
882 if (!is_cow_mapping(vma->vm_flags))
883 return NULL;
884 }
Linus Torvalds6aab3412005-11-28 14:34:23 -0800885 }
886
Hugh Dickinsb38af472014-08-29 15:18:44 -0700887 if (is_zero_pfn(pfn))
888 return NULL;
Hugh Dickins22b31ee2009-01-06 14:40:09 -0800889check_pfn:
890 if (unlikely(pfn > highest_memmap_pfn)) {
891 print_bad_pte(vma, addr, pte, NULL);
892 return NULL;
893 }
Linus Torvalds6aab3412005-11-28 14:34:23 -0800894
895 /*
Nick Piggin7e675132008-04-28 02:13:00 -0700896 * NOTE! We still have PageReserved() pages in the page tables.
Nick Piggin7e675132008-04-28 02:13:00 -0700897 * eg. VDSO mappings can cause them to exist.
Linus Torvalds6aab3412005-11-28 14:34:23 -0800898 */
Jared Hulbertb379d792008-04-28 02:12:58 -0700899out:
Linus Torvalds6aab3412005-11-28 14:34:23 -0800900 return pfn_to_page(pfn);
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800901}
902
Gerald Schaefer28093f92016-04-28 16:18:35 -0700903#ifdef CONFIG_TRANSPARENT_HUGEPAGE
904struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
905 pmd_t pmd)
906{
907 unsigned long pfn = pmd_pfn(pmd);
908
909 /*
910 * There is no pmd_special() but there may be special pmds, e.g.
911 * in a direct-access (dax) mapping, so let's just replicate the
912 * !HAVE_PTE_SPECIAL case from vm_normal_page() here.
913 */
914 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
915 if (vma->vm_flags & VM_MIXEDMAP) {
916 if (!pfn_valid(pfn))
917 return NULL;
918 goto out;
919 } else {
920 unsigned long off;
921 off = (addr - vma->vm_start) >> PAGE_SHIFT;
922 if (pfn == vma->vm_pgoff + off)
923 return NULL;
924 if (!is_cow_mapping(vma->vm_flags))
925 return NULL;
926 }
927 }
928
929 if (is_zero_pfn(pfn))
930 return NULL;
931 if (unlikely(pfn > highest_memmap_pfn))
932 return NULL;
933
934 /*
935 * NOTE! We still have PageReserved() pages in the page tables.
936 * eg. VDSO mappings can cause them to exist.
937 */
938out:
939 return pfn_to_page(pfn);
940}
941#endif
942
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800943/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 * copy one vm_area from one task to the other. Assumes the page tables
945 * already present in the new task to be cleared in the whole range
946 * covered by this vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 */
948
Hugh Dickins570a335b2009-12-14 17:58:46 -0800949static inline unsigned long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
Nick Pigginb5810032005-10-29 18:16:12 -0700951 pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
Hugh Dickins8c103762005-10-29 18:16:13 -0700952 unsigned long addr, int *rss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Nick Pigginb5810032005-10-29 18:16:12 -0700954 unsigned long vm_flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 pte_t pte = *src_pte;
956 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 /* pte contains position in swap or file, so copy. */
959 if (unlikely(!pte_present(pte))) {
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800960 swp_entry_t entry = pte_to_swp_entry(pte);
Christoph Lameter06972122006-06-23 02:03:35 -0700961
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800962 if (likely(!non_swap_entry(entry))) {
963 if (swap_duplicate(entry) < 0)
964 return entry.val;
Hugh Dickins570a335b2009-12-14 17:58:46 -0800965
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800966 /* make sure dst_mm is on swapoff's mmlist. */
967 if (unlikely(list_empty(&dst_mm->mmlist))) {
968 spin_lock(&mmlist_lock);
969 if (list_empty(&dst_mm->mmlist))
970 list_add(&dst_mm->mmlist,
971 &src_mm->mmlist);
972 spin_unlock(&mmlist_lock);
973 }
974 rss[MM_SWAPENTS]++;
975 } else if (is_migration_entry(entry)) {
976 page = migration_entry_to_page(entry);
Konstantin Khlebnikov9f9f1ac2012-01-20 14:34:24 -0800977
Jerome Marchandeca56ff2016-01-14 15:19:26 -0800978 rss[mm_counter(page)]++;
Konstantin Khlebnikov9f9f1ac2012-01-20 14:34:24 -0800979
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800980 if (is_write_migration_entry(entry) &&
981 is_cow_mapping(vm_flags)) {
982 /*
983 * COW mappings require pages in both
984 * parent and child to be set to read.
985 */
986 make_migration_entry_read(&entry);
987 pte = swp_entry_to_pte(entry);
988 if (pte_swp_soft_dirty(*src_pte))
989 pte = pte_swp_mksoft_dirty(pte);
990 set_pte_at(src_mm, addr, src_pte, pte);
Christoph Lameter06972122006-06-23 02:03:35 -0700991 }
Jérôme Glisse5042db42017-09-08 16:11:43 -0700992 } else if (is_device_private_entry(entry)) {
993 page = device_private_entry_to_page(entry);
994
995 /*
996 * Update rss count even for unaddressable pages, as
997 * they should treated just like normal pages in this
998 * respect.
999 *
1000 * We will likely want to have some new rss counters
1001 * for unaddressable pages, at some point. But for now
1002 * keep things as they are.
1003 */
1004 get_page(page);
1005 rss[mm_counter(page)]++;
1006 page_dup_rmap(page, false);
1007
1008 /*
1009 * We do not preserve soft-dirty information, because so
1010 * far, checkpoint/restore is the only feature that
1011 * requires that. And checkpoint/restore does not work
1012 * when a device driver is involved (you cannot easily
1013 * save and restore device driver state).
1014 */
1015 if (is_write_device_private_entry(entry) &&
1016 is_cow_mapping(vm_flags)) {
1017 make_device_private_entry_read(&entry);
1018 pte = swp_entry_to_pte(entry);
1019 set_pte_at(src_mm, addr, src_pte, pte);
1020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
Hugh Dickinsae859762005-10-29 18:16:05 -07001022 goto out_set_pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /*
1026 * If it's a COW mapping, write protect it both
1027 * in the parent and the child
1028 */
Linus Torvalds67121172005-12-11 20:38:17 -08001029 if (is_cow_mapping(vm_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 ptep_set_wrprotect(src_mm, addr, src_pte);
Zachary Amsden3dc90792006-09-30 23:29:30 -07001031 pte = pte_wrprotect(pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033
1034 /*
1035 * If it's a shared mapping, mark it clean in
1036 * the child
1037 */
1038 if (vm_flags & VM_SHARED)
1039 pte = pte_mkclean(pte);
1040 pte = pte_mkold(pte);
Linus Torvalds6aab3412005-11-28 14:34:23 -08001041
1042 page = vm_normal_page(vma, addr, pte);
1043 if (page) {
1044 get_page(page);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001045 page_dup_rmap(page, false);
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001046 rss[mm_counter(page)]++;
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001047 } else if (pte_devmap(pte)) {
1048 page = pte_page(pte);
1049
1050 /*
1051 * Cache coherent device memory behave like regular page and
1052 * not like persistent memory page. For more informations see
1053 * MEMORY_DEVICE_CACHE_COHERENT in memory_hotplug.h
1054 */
1055 if (is_device_public_page(page)) {
1056 get_page(page);
1057 page_dup_rmap(page, false);
1058 rss[mm_counter(page)]++;
1059 }
Linus Torvalds6aab3412005-11-28 14:34:23 -08001060 }
Hugh Dickinsae859762005-10-29 18:16:05 -07001061
1062out_set_pte:
1063 set_pte_at(dst_mm, addr, dst_pte, pte);
Hugh Dickins570a335b2009-12-14 17:58:46 -08001064 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
Jerome Marchand21bda262014-08-06 16:06:56 -07001067static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001068 pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
1069 unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Daisuke Nishimurac36987e2009-10-26 16:50:23 -07001071 pte_t *orig_src_pte, *orig_dst_pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 pte_t *src_pte, *dst_pte;
Hugh Dickinsc74df322005-10-29 18:16:23 -07001073 spinlock_t *src_ptl, *dst_ptl;
Hugh Dickinse040f212005-10-29 18:15:53 -07001074 int progress = 0;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001075 int rss[NR_MM_COUNTERS];
Hugh Dickins570a335b2009-12-14 17:58:46 -08001076 swp_entry_t entry = (swp_entry_t){0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078again:
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001079 init_rss_vec(rss);
1080
Hugh Dickinsc74df322005-10-29 18:16:23 -07001081 dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (!dst_pte)
1083 return -ENOMEM;
Peter Zijlstraece0e2b2010-10-26 14:21:52 -07001084 src_pte = pte_offset_map(src_pmd, addr);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07001085 src_ptl = pte_lockptr(src_mm, src_pmd);
Ingo Molnarf20dc5f2006-07-03 00:25:08 -07001086 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Daisuke Nishimurac36987e2009-10-26 16:50:23 -07001087 orig_src_pte = src_pte;
1088 orig_dst_pte = dst_pte;
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001089 arch_enter_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 do {
1092 /*
1093 * We are holding two locks at this point - either of them
1094 * could generate latencies in another task on another CPU.
1095 */
Hugh Dickinse040f212005-10-29 18:15:53 -07001096 if (progress >= 32) {
1097 progress = 0;
1098 if (need_resched() ||
Nick Piggin95c354f2008-01-30 13:31:20 +01001099 spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
Hugh Dickinse040f212005-10-29 18:15:53 -07001100 break;
1101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (pte_none(*src_pte)) {
1103 progress++;
1104 continue;
1105 }
Hugh Dickins570a335b2009-12-14 17:58:46 -08001106 entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
1107 vma, addr, rss);
1108 if (entry.val)
1109 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 progress += 8;
1111 } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001113 arch_leave_lazy_mmu_mode();
Hugh Dickinsc74df322005-10-29 18:16:23 -07001114 spin_unlock(src_ptl);
Peter Zijlstraece0e2b2010-10-26 14:21:52 -07001115 pte_unmap(orig_src_pte);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001116 add_mm_rss_vec(dst_mm, rss);
Daisuke Nishimurac36987e2009-10-26 16:50:23 -07001117 pte_unmap_unlock(orig_dst_pte, dst_ptl);
Hugh Dickinsc74df322005-10-29 18:16:23 -07001118 cond_resched();
Hugh Dickins570a335b2009-12-14 17:58:46 -08001119
1120 if (entry.val) {
1121 if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
1122 return -ENOMEM;
1123 progress = 0;
1124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (addr != end)
1126 goto again;
1127 return 0;
1128}
1129
1130static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1131 pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
1132 unsigned long addr, unsigned long end)
1133{
1134 pmd_t *src_pmd, *dst_pmd;
1135 unsigned long next;
1136
1137 dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
1138 if (!dst_pmd)
1139 return -ENOMEM;
1140 src_pmd = pmd_offset(src_pud, addr);
1141 do {
1142 next = pmd_addr_end(addr, end);
Zi Yan84c3fc42017-09-08 16:11:01 -07001143 if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd)
1144 || pmd_devmap(*src_pmd)) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001145 int err;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001146 VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001147 err = copy_huge_pmd(dst_mm, src_mm,
1148 dst_pmd, src_pmd, addr, vma);
1149 if (err == -ENOMEM)
1150 return -ENOMEM;
1151 if (!err)
1152 continue;
1153 /* fall through */
1154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (pmd_none_or_clear_bad(src_pmd))
1156 continue;
1157 if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
1158 vma, addr, next))
1159 return -ENOMEM;
1160 } while (dst_pmd++, src_pmd++, addr = next, addr != end);
1161 return 0;
1162}
1163
1164static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001165 p4d_t *dst_p4d, p4d_t *src_p4d, struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 unsigned long addr, unsigned long end)
1167{
1168 pud_t *src_pud, *dst_pud;
1169 unsigned long next;
1170
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001171 dst_pud = pud_alloc(dst_mm, dst_p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (!dst_pud)
1173 return -ENOMEM;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001174 src_pud = pud_offset(src_p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 do {
1176 next = pud_addr_end(addr, end);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001177 if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) {
1178 int err;
1179
1180 VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, vma);
1181 err = copy_huge_pud(dst_mm, src_mm,
1182 dst_pud, src_pud, addr, vma);
1183 if (err == -ENOMEM)
1184 return -ENOMEM;
1185 if (!err)
1186 continue;
1187 /* fall through */
1188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (pud_none_or_clear_bad(src_pud))
1190 continue;
1191 if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
1192 vma, addr, next))
1193 return -ENOMEM;
1194 } while (dst_pud++, src_pud++, addr = next, addr != end);
1195 return 0;
1196}
1197
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001198static inline int copy_p4d_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1199 pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
1200 unsigned long addr, unsigned long end)
1201{
1202 p4d_t *src_p4d, *dst_p4d;
1203 unsigned long next;
1204
1205 dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr);
1206 if (!dst_p4d)
1207 return -ENOMEM;
1208 src_p4d = p4d_offset(src_pgd, addr);
1209 do {
1210 next = p4d_addr_end(addr, end);
1211 if (p4d_none_or_clear_bad(src_p4d))
1212 continue;
1213 if (copy_pud_range(dst_mm, src_mm, dst_p4d, src_p4d,
1214 vma, addr, next))
1215 return -ENOMEM;
1216 } while (dst_p4d++, src_p4d++, addr = next, addr != end);
1217 return 0;
1218}
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1221 struct vm_area_struct *vma)
1222{
1223 pgd_t *src_pgd, *dst_pgd;
1224 unsigned long next;
1225 unsigned long addr = vma->vm_start;
1226 unsigned long end = vma->vm_end;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001227 unsigned long mmun_start; /* For mmu_notifiers */
1228 unsigned long mmun_end; /* For mmu_notifiers */
1229 bool is_cow;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001230 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Nick Piggind9928952005-08-28 16:49:11 +10001232 /*
1233 * Don't copy ptes where a page fault will fill them correctly.
1234 * Fork becomes much lighter when there are big shared or private
1235 * readonly mappings. The tradeoff is that copy_page_range is more
1236 * efficient than faulting.
1237 */
Kirill A. Shutemov0661a332015-02-10 14:10:04 -08001238 if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) &&
1239 !vma->anon_vma)
1240 return 0;
Nick Piggind9928952005-08-28 16:49:11 +10001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (is_vm_hugetlb_page(vma))
1243 return copy_hugetlb_page_range(dst_mm, src_mm, vma);
1244
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001245 if (unlikely(vma->vm_flags & VM_PFNMAP)) {
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001246 /*
1247 * We do not free on error cases below as remove_vma
1248 * gets called on error from higher level routine
1249 */
Suresh Siddha5180da42012-10-08 16:28:29 -07001250 ret = track_pfn_copy(vma);
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001251 if (ret)
1252 return ret;
1253 }
1254
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001255 /*
1256 * We need to invalidate the secondary MMU mappings only when
1257 * there could be a permission downgrade on the ptes of the
1258 * parent mm. And a permission downgrade will only happen if
1259 * is_cow_mapping() returns true.
1260 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001261 is_cow = is_cow_mapping(vma->vm_flags);
1262 mmun_start = addr;
1263 mmun_end = end;
1264 if (is_cow)
1265 mmu_notifier_invalidate_range_start(src_mm, mmun_start,
1266 mmun_end);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001267
1268 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 dst_pgd = pgd_offset(dst_mm, addr);
1270 src_pgd = pgd_offset(src_mm, addr);
1271 do {
1272 next = pgd_addr_end(addr, end);
1273 if (pgd_none_or_clear_bad(src_pgd))
1274 continue;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001275 if (unlikely(copy_p4d_range(dst_mm, src_mm, dst_pgd, src_pgd,
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001276 vma, addr, next))) {
1277 ret = -ENOMEM;
1278 break;
1279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 } while (dst_pgd++, src_pgd++, addr = next, addr != end);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001281
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001282 if (is_cow)
1283 mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001284 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
Robin Holt51c6f662005-11-13 16:06:42 -08001287static unsigned long zap_pte_range(struct mmu_gather *tlb,
Nick Pigginb5810032005-10-29 18:16:12 -07001288 struct vm_area_struct *vma, pmd_t *pmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 unsigned long addr, unsigned long end,
Peter Zijlstra97a89412011-05-24 17:12:04 -07001290 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Nick Pigginb5810032005-10-29 18:16:12 -07001292 struct mm_struct *mm = tlb->mm;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001293 int force_flush = 0;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001294 int rss[NR_MM_COUNTERS];
Peter Zijlstra97a89412011-05-24 17:12:04 -07001295 spinlock_t *ptl;
Steven Rostedt5f1a1902011-06-15 15:08:23 -07001296 pte_t *start_pte;
Peter Zijlstra97a89412011-05-24 17:12:04 -07001297 pte_t *pte;
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001298 swp_entry_t entry;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001299
Aneesh Kumar K.V07e32662016-12-12 16:42:40 -08001300 tlb_remove_check_page_size_change(tlb, PAGE_SIZE);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001301again:
Peter Zijlstrae3032972011-05-24 17:12:01 -07001302 init_rss_vec(rss);
Steven Rostedt5f1a1902011-06-15 15:08:23 -07001303 start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
1304 pte = start_pte;
Mel Gorman3ea27712017-08-02 13:31:52 -07001305 flush_tlb_batched_pending(mm);
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001306 arch_enter_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 do {
1308 pte_t ptent = *pte;
Tobin C Harding166f61b2017-02-24 14:59:01 -08001309 if (pte_none(ptent))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 continue;
Hugh Dickins6f5e6b92006-03-16 23:04:09 -08001311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (pte_present(ptent)) {
Hugh Dickinsee498ed2005-11-21 21:32:18 -08001313 struct page *page;
Robin Holt51c6f662005-11-13 16:06:42 -08001314
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001315 page = _vm_normal_page(vma, addr, ptent, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (unlikely(details) && page) {
1317 /*
1318 * unmap_shared_mapping_pages() wants to
1319 * invalidate cache without truncating:
1320 * unmap shared but keep private pages.
1321 */
1322 if (details->check_mapping &&
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001323 details->check_mapping != page_rmapping(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
Nick Pigginb5810032005-10-29 18:16:12 -07001326 ptent = ptep_get_and_clear_full(mm, addr, pte,
Zachary Amsdena6003882005-09-03 15:55:04 -07001327 tlb->fullmm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 tlb_remove_tlb_entry(tlb, pte, addr);
1329 if (unlikely(!page))
1330 continue;
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001331
1332 if (!PageAnon(page)) {
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001333 if (pte_dirty(ptent)) {
1334 force_flush = 1;
Hugh Dickins6237bcd2005-10-29 18:15:54 -07001335 set_page_dirty(page);
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001336 }
Johannes Weiner4917e5d2009-01-06 14:39:17 -08001337 if (pte_young(ptent) &&
Joe Perches64363aa2013-07-08 16:00:18 -07001338 likely(!(vma->vm_flags & VM_SEQ_READ)))
Nick Pigginbf3f3bc2009-01-06 14:38:55 -08001339 mark_page_accessed(page);
Hugh Dickins6237bcd2005-10-29 18:15:54 -07001340 }
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001341 rss[mm_counter(page)]--;
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001342 page_remove_rmap(page, false);
Hugh Dickins3dc14742009-01-06 14:40:08 -08001343 if (unlikely(page_mapcount(page) < 0))
1344 print_bad_pte(vma, addr, ptent, page);
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -07001345 if (unlikely(__tlb_remove_page(tlb, page))) {
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001346 force_flush = 1;
Will Deaconce9ec372014-10-28 13:16:28 -07001347 addr += PAGE_SIZE;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001348 break;
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 continue;
1351 }
Jérôme Glisse5042db42017-09-08 16:11:43 -07001352
1353 entry = pte_to_swp_entry(ptent);
1354 if (non_swap_entry(entry) && is_device_private_entry(entry)) {
1355 struct page *page = device_private_entry_to_page(entry);
1356
1357 if (unlikely(details && details->check_mapping)) {
1358 /*
1359 * unmap_shared_mapping_pages() wants to
1360 * invalidate cache without truncating:
1361 * unmap shared but keep private pages.
1362 */
1363 if (details->check_mapping !=
1364 page_rmapping(page))
1365 continue;
1366 }
1367
1368 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
1369 rss[mm_counter(page)]--;
1370 page_remove_rmap(page, false);
1371 put_page(page);
1372 continue;
1373 }
1374
Kirill A. Shutemov3e8715f2017-02-22 15:46:34 -08001375 /* If details->check_mapping, we leave swap entries. */
1376 if (unlikely(details))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 continue;
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -08001378
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001379 entry = pte_to_swp_entry(ptent);
1380 if (!non_swap_entry(entry))
1381 rss[MM_SWAPENTS]--;
1382 else if (is_migration_entry(entry)) {
1383 struct page *page;
Konstantin Khlebnikov9f9f1ac2012-01-20 14:34:24 -08001384
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001385 page = migration_entry_to_page(entry);
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001386 rss[mm_counter(page)]--;
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -08001387 }
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001388 if (unlikely(!free_swap_and_cache(entry)))
1389 print_bad_pte(vma, addr, ptent, NULL);
Zachary Amsden9888a1c2006-09-30 23:29:31 -07001390 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
Peter Zijlstra97a89412011-05-24 17:12:04 -07001391 } while (pte++, addr += PAGE_SIZE, addr != end);
Hugh Dickinsae859762005-10-29 18:16:05 -07001392
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001393 add_mm_rss_vec(mm, rss);
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001394 arch_leave_lazy_mmu_mode();
Robin Holt51c6f662005-11-13 16:06:42 -08001395
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001396 /* Do the actual TLB flush before dropping ptl */
Will Deaconfb7332a2014-10-29 10:03:09 +00001397 if (force_flush)
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001398 tlb_flush_mmu_tlbonly(tlb);
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001399 pte_unmap_unlock(start_pte, ptl);
1400
1401 /*
1402 * If we forced a TLB flush (either due to running out of
1403 * batch buffers or because we needed to flush dirty TLB
1404 * entries before releasing the ptl), free the batched
1405 * memory too. Restart if we didn't do everything.
1406 */
1407 if (force_flush) {
1408 force_flush = 0;
1409 tlb_flush_mmu_free(tlb);
Linus Torvalds2b047252013-08-15 11:42:25 -07001410 if (addr != end)
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001411 goto again;
1412 }
1413
Robin Holt51c6f662005-11-13 16:06:42 -08001414 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Robin Holt51c6f662005-11-13 16:06:42 -08001417static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
Nick Pigginb5810032005-10-29 18:16:12 -07001418 struct vm_area_struct *vma, pud_t *pud,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 unsigned long addr, unsigned long end,
Peter Zijlstra97a89412011-05-24 17:12:04 -07001420 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
1422 pmd_t *pmd;
1423 unsigned long next;
1424
1425 pmd = pmd_offset(pud, addr);
1426 do {
1427 next = pmd_addr_end(addr, end);
Zi Yan84c3fc42017-09-08 16:11:01 -07001428 if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
Hugh Dickins249778d2018-08-01 11:31:52 -07001429 if (next - addr != HPAGE_PMD_SIZE)
David Rientjesfd607752016-12-12 16:42:20 -08001430 __split_huge_pmd(vma, pmd, addr, false, NULL);
Hugh Dickins249778d2018-08-01 11:31:52 -07001431 else if (zap_huge_pmd(tlb, vma, pmd, addr))
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001432 goto next;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001433 /* fall through */
1434 }
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001435 /*
1436 * Here there can be other concurrent MADV_DONTNEED or
1437 * trans huge page faults running, and if the pmd is
1438 * none or trans huge it can change under us. This is
1439 * because MADV_DONTNEED holds the mmap_sem in read
1440 * mode.
1441 */
1442 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
1443 goto next;
Peter Zijlstra97a89412011-05-24 17:12:04 -07001444 next = zap_pte_range(tlb, vma, pmd, addr, next, details);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001445next:
Peter Zijlstra97a89412011-05-24 17:12:04 -07001446 cond_resched();
1447 } while (pmd++, addr = next, addr != end);
Robin Holt51c6f662005-11-13 16:06:42 -08001448
1449 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Robin Holt51c6f662005-11-13 16:06:42 -08001452static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001453 struct vm_area_struct *vma, p4d_t *p4d,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 unsigned long addr, unsigned long end,
Peter Zijlstra97a89412011-05-24 17:12:04 -07001455 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 pud_t *pud;
1458 unsigned long next;
1459
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001460 pud = pud_offset(p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 do {
1462 next = pud_addr_end(addr, end);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001463 if (pud_trans_huge(*pud) || pud_devmap(*pud)) {
1464 if (next - addr != HPAGE_PUD_SIZE) {
1465 VM_BUG_ON_VMA(!rwsem_is_locked(&tlb->mm->mmap_sem), vma);
1466 split_huge_pud(vma, pud, addr);
1467 } else if (zap_huge_pud(tlb, vma, pud, addr))
1468 goto next;
1469 /* fall through */
1470 }
Peter Zijlstra97a89412011-05-24 17:12:04 -07001471 if (pud_none_or_clear_bad(pud))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 continue;
Peter Zijlstra97a89412011-05-24 17:12:04 -07001473 next = zap_pmd_range(tlb, vma, pud, addr, next, details);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001474next:
1475 cond_resched();
Peter Zijlstra97a89412011-05-24 17:12:04 -07001476 } while (pud++, addr = next, addr != end);
Robin Holt51c6f662005-11-13 16:06:42 -08001477
1478 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001481static inline unsigned long zap_p4d_range(struct mmu_gather *tlb,
1482 struct vm_area_struct *vma, pgd_t *pgd,
1483 unsigned long addr, unsigned long end,
1484 struct zap_details *details)
1485{
1486 p4d_t *p4d;
1487 unsigned long next;
1488
1489 p4d = p4d_offset(pgd, addr);
1490 do {
1491 next = p4d_addr_end(addr, end);
1492 if (p4d_none_or_clear_bad(p4d))
1493 continue;
1494 next = zap_pud_range(tlb, vma, p4d, addr, next, details);
1495 } while (p4d++, addr = next, addr != end);
1496
1497 return addr;
1498}
1499
Michal Hockoaac45362016-03-25 14:20:24 -07001500void unmap_page_range(struct mmu_gather *tlb,
Al Viro038c7aa2012-03-05 13:25:09 -05001501 struct vm_area_struct *vma,
1502 unsigned long addr, unsigned long end,
1503 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 pgd_t *pgd;
1506 unsigned long next;
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 BUG_ON(addr >= end);
1509 tlb_start_vma(tlb, vma);
1510 pgd = pgd_offset(vma->vm_mm, addr);
1511 do {
1512 next = pgd_addr_end(addr, end);
Peter Zijlstra97a89412011-05-24 17:12:04 -07001513 if (pgd_none_or_clear_bad(pgd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 continue;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001515 next = zap_p4d_range(tlb, vma, pgd, addr, next, details);
Peter Zijlstra97a89412011-05-24 17:12:04 -07001516 } while (pgd++, addr = next, addr != end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 tlb_end_vma(tlb, vma);
1518}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Al Virof5cc4ee2012-03-05 14:14:20 -05001520
1521static void unmap_single_vma(struct mmu_gather *tlb,
1522 struct vm_area_struct *vma, unsigned long start_addr,
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001523 unsigned long end_addr,
Al Virof5cc4ee2012-03-05 14:14:20 -05001524 struct zap_details *details)
1525{
1526 unsigned long start = max(vma->vm_start, start_addr);
1527 unsigned long end;
1528
1529 if (start >= vma->vm_end)
1530 return;
1531 end = min(vma->vm_end, end_addr);
1532 if (end <= vma->vm_start)
1533 return;
1534
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301535 if (vma->vm_file)
1536 uprobe_munmap(vma, start, end);
1537
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001538 if (unlikely(vma->vm_flags & VM_PFNMAP))
Suresh Siddha5180da42012-10-08 16:28:29 -07001539 untrack_pfn(vma, 0, 0);
Al Virof5cc4ee2012-03-05 14:14:20 -05001540
1541 if (start != end) {
1542 if (unlikely(is_vm_hugetlb_page(vma))) {
1543 /*
1544 * It is undesirable to test vma->vm_file as it
1545 * should be non-null for valid hugetlb area.
1546 * However, vm_file will be NULL in the error
Davidlohr Bueso7aa6b4a2014-04-07 15:37:01 -07001547 * cleanup path of mmap_region. When
Al Virof5cc4ee2012-03-05 14:14:20 -05001548 * hugetlbfs ->mmap method fails,
Davidlohr Bueso7aa6b4a2014-04-07 15:37:01 -07001549 * mmap_region() nullifies vma->vm_file
Al Virof5cc4ee2012-03-05 14:14:20 -05001550 * before calling this function to clean up.
1551 * Since no pte has actually been setup, it is
1552 * safe to do nothing in this case.
1553 */
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07001554 if (vma->vm_file) {
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08001555 i_mmap_lock_write(vma->vm_file->f_mapping);
Mel Gormand8333522012-07-31 16:46:20 -07001556 __unmap_hugepage_range_final(tlb, vma, start, end, NULL);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08001557 i_mmap_unlock_write(vma->vm_file->f_mapping);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07001558 }
Al Virof5cc4ee2012-03-05 14:14:20 -05001559 } else
1560 unmap_page_range(tlb, vma, start, end, details);
1561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564/**
1565 * unmap_vmas - unmap a range of memory covered by a list of vma's
Randy Dunlap0164f692011-06-15 15:08:09 -07001566 * @tlb: address of the caller's struct mmu_gather
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 * @vma: the starting vma
1568 * @start_addr: virtual address at which to start unmapping
1569 * @end_addr: virtual address at which to end unmapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 *
Hugh Dickins508034a2005-10-29 18:16:30 -07001571 * Unmap all pages in the vma list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 * Only addresses between `start' and `end' will be unmapped.
1574 *
1575 * The VMA list must be sorted in ascending virtual address order.
1576 *
1577 * unmap_vmas() assumes that the caller will flush the whole unmapped address
1578 * range after unmap_vmas() returns. So the only responsibility here is to
1579 * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
1580 * drops the lock and schedules.
1581 */
Al Viro6e8bb012012-03-05 13:41:15 -05001582void unmap_vmas(struct mmu_gather *tlb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 struct vm_area_struct *vma, unsigned long start_addr,
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001584 unsigned long end_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001586 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001588 mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
Al Virof5cc4ee2012-03-05 14:14:20 -05001589 for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001590 unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001591 mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593
1594/**
1595 * zap_page_range - remove user pages in a given range
1596 * @vma: vm_area_struct holding the applicable pages
Randy Dunlapeb4546b2012-06-20 12:53:02 -07001597 * @start: starting address of pages to zap
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 * @size: number of bytes to zap
Al Virof5cc4ee2012-03-05 14:14:20 -05001599 *
1600 * Caller must protect the VMA list
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 */
Linus Torvalds7e027b12012-05-06 13:43:15 -07001602void zap_page_range(struct vm_area_struct *vma, unsigned long start,
Kirill A. Shutemovecf13852017-02-22 15:46:37 -08001603 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
1605 struct mm_struct *mm = vma->vm_mm;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001606 struct mmu_gather tlb;
Linus Torvalds7e027b12012-05-06 13:43:15 -07001607 unsigned long end = start + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 lru_add_drain();
Linus Torvalds2b047252013-08-15 11:42:25 -07001610 tlb_gather_mmu(&tlb, mm, start, end);
Hugh Dickins365e9c872005-10-29 18:16:18 -07001611 update_hiwater_rss(mm);
Linus Torvalds7e027b12012-05-06 13:43:15 -07001612 mmu_notifier_invalidate_range_start(mm, start, end);
Mel Gorman46477062017-09-06 16:21:05 -07001613 for ( ; vma && vma->vm_start < end; vma = vma->vm_next) {
Kirill A. Shutemovecf13852017-02-22 15:46:37 -08001614 unmap_single_vma(&tlb, vma, start, end, NULL);
Mel Gorman46477062017-09-06 16:21:05 -07001615
1616 /*
1617 * zap_page_range does not specify whether mmap_sem should be
1618 * held for read or write. That allows parallel zap_page_range
1619 * operations to unmap a PTE and defer a flush meaning that
1620 * this call observes pte_none and fails to flush the TLB.
1621 * Rather than adding a complex API, ensure that no stale
1622 * TLB entries exist when this call returns.
1623 */
1624 flush_tlb_range(vma, start, end);
1625 }
1626
Linus Torvalds7e027b12012-05-06 13:43:15 -07001627 mmu_notifier_invalidate_range_end(mm, start, end);
1628 tlb_finish_mmu(&tlb, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
Jack Steinerc627f9c2008-07-29 22:33:53 -07001631/**
Al Virof5cc4ee2012-03-05 14:14:20 -05001632 * zap_page_range_single - remove user pages in a given range
1633 * @vma: vm_area_struct holding the applicable pages
1634 * @address: starting address of pages to zap
1635 * @size: number of bytes to zap
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001636 * @details: details of shared cache invalidation
Al Virof5cc4ee2012-03-05 14:14:20 -05001637 *
1638 * The range must fit into one VMA.
1639 */
1640static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
1641 unsigned long size, struct zap_details *details)
1642{
1643 struct mm_struct *mm = vma->vm_mm;
1644 struct mmu_gather tlb;
1645 unsigned long end = address + size;
Al Virof5cc4ee2012-03-05 14:14:20 -05001646
1647 lru_add_drain();
Linus Torvalds2b047252013-08-15 11:42:25 -07001648 tlb_gather_mmu(&tlb, mm, address, end);
Al Virof5cc4ee2012-03-05 14:14:20 -05001649 update_hiwater_rss(mm);
1650 mmu_notifier_invalidate_range_start(mm, address, end);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001651 unmap_single_vma(&tlb, vma, address, end, details);
Al Virof5cc4ee2012-03-05 14:14:20 -05001652 mmu_notifier_invalidate_range_end(mm, address, end);
1653 tlb_finish_mmu(&tlb, address, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
Jack Steinerc627f9c2008-07-29 22:33:53 -07001656/**
1657 * zap_vma_ptes - remove ptes mapping the vma
1658 * @vma: vm_area_struct holding ptes to be zapped
1659 * @address: starting address of pages to zap
1660 * @size: number of bytes to zap
1661 *
1662 * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1663 *
1664 * The entire address range must be fully contained within the vma.
1665 *
1666 * Returns 0 if successful.
1667 */
1668int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
1669 unsigned long size)
1670{
1671 if (address < vma->vm_start || address + size > vma->vm_end ||
1672 !(vma->vm_flags & VM_PFNMAP))
1673 return -1;
Al Virof5cc4ee2012-03-05 14:14:20 -05001674 zap_page_range_single(vma, address, size, NULL);
Jack Steinerc627f9c2008-07-29 22:33:53 -07001675 return 0;
1676}
1677EXPORT_SYMBOL_GPL(zap_vma_ptes);
1678
Namhyung Kim25ca1d62010-10-26 14:21:59 -07001679pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
Harvey Harrison920c7a52008-02-04 22:29:26 -08001680 spinlock_t **ptl)
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -08001681{
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001682 pgd_t *pgd;
1683 p4d_t *p4d;
1684 pud_t *pud;
1685 pmd_t *pmd;
1686
1687 pgd = pgd_offset(mm, addr);
1688 p4d = p4d_alloc(mm, pgd, addr);
1689 if (!p4d)
1690 return NULL;
1691 pud = pud_alloc(mm, p4d, addr);
1692 if (!pud)
1693 return NULL;
1694 pmd = pmd_alloc(mm, pud, addr);
1695 if (!pmd)
1696 return NULL;
1697
1698 VM_BUG_ON(pmd_trans_huge(*pmd));
1699 return pte_alloc_map_lock(mm, pmd, addr, ptl);
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -08001700}
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702/*
Linus Torvalds238f58d2005-11-29 13:01:56 -08001703 * This is the old fallback for page remapping.
1704 *
1705 * For historical reasons, it only allows reserved pages. Only
1706 * old drivers should use this, and they needed to mark their
1707 * pages reserved for the old functions anyway.
1708 */
Nick Piggin423bad62008-04-28 02:13:01 -07001709static int insert_page(struct vm_area_struct *vma, unsigned long addr,
1710 struct page *page, pgprot_t prot)
Linus Torvalds238f58d2005-11-29 13:01:56 -08001711{
Nick Piggin423bad62008-04-28 02:13:01 -07001712 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds238f58d2005-11-29 13:01:56 -08001713 int retval;
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -08001714 pte_t *pte;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001715 spinlock_t *ptl;
1716
Linus Torvalds238f58d2005-11-29 13:01:56 -08001717 retval = -EINVAL;
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001718 if (PageAnon(page))
KAMEZAWA Hiroyuki5b4e6552008-10-18 20:28:10 -07001719 goto out;
Linus Torvalds238f58d2005-11-29 13:01:56 -08001720 retval = -ENOMEM;
1721 flush_dcache_page(page);
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -08001722 pte = get_locked_pte(mm, addr, &ptl);
Linus Torvalds238f58d2005-11-29 13:01:56 -08001723 if (!pte)
KAMEZAWA Hiroyuki5b4e6552008-10-18 20:28:10 -07001724 goto out;
Linus Torvalds238f58d2005-11-29 13:01:56 -08001725 retval = -EBUSY;
1726 if (!pte_none(*pte))
1727 goto out_unlock;
1728
1729 /* Ok, finally just insert the thing.. */
1730 get_page(page);
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001731 inc_mm_counter_fast(mm, mm_counter_file(page));
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07001732 page_add_file_rmap(page, false);
Linus Torvalds238f58d2005-11-29 13:01:56 -08001733 set_pte_at(mm, addr, pte, mk_pte(page, prot));
1734
1735 retval = 0;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001736 pte_unmap_unlock(pte, ptl);
1737 return retval;
Linus Torvalds238f58d2005-11-29 13:01:56 -08001738out_unlock:
1739 pte_unmap_unlock(pte, ptl);
1740out:
1741 return retval;
1742}
1743
Rolf Eike Beerbfa5bf62006-09-25 23:31:22 -07001744/**
1745 * vm_insert_page - insert single page into user vma
1746 * @vma: user vma to map to
1747 * @addr: target user address of this page
1748 * @page: source kernel page
1749 *
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001750 * This allows drivers to insert individual pages they've allocated
1751 * into a user vma.
1752 *
1753 * The page has to be a nice clean _individual_ kernel allocation.
1754 * If you allocate a compound page, you need to have marked it as
1755 * such (__GFP_COMP), or manually just split the page up yourself
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001756 * (see split_page()).
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001757 *
1758 * NOTE! Traditionally this was done with "remap_pfn_range()" which
1759 * took an arbitrary page protection parameter. This doesn't allow
1760 * that. Your vma protection will have to be set up correctly, which
1761 * means that if you want a shared writable mapping, you'd better
1762 * ask for a shared writable mapping!
1763 *
1764 * The page does not need to be reserved.
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001765 *
1766 * Usually this function is called from f_op->mmap() handler
1767 * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
1768 * Caller must set VM_MIXEDMAP on vma if it wants to call this
1769 * function from other places, for example from page-fault handler.
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001770 */
Nick Piggin423bad62008-04-28 02:13:01 -07001771int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
1772 struct page *page)
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001773{
1774 if (addr < vma->vm_start || addr >= vma->vm_end)
1775 return -EFAULT;
1776 if (!page_count(page))
1777 return -EINVAL;
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001778 if (!(vma->vm_flags & VM_MIXEDMAP)) {
1779 BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem));
1780 BUG_ON(vma->vm_flags & VM_PFNMAP);
1781 vma->vm_flags |= VM_MIXEDMAP;
1782 }
Nick Piggin423bad62008-04-28 02:13:01 -07001783 return insert_page(vma, addr, page, vma->vm_page_prot);
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001784}
Linus Torvaldse3c33742005-12-03 20:48:11 -08001785EXPORT_SYMBOL(vm_insert_page);
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001786
Nick Piggin423bad62008-04-28 02:13:01 -07001787static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
Ross Zwislerb2770da62017-09-06 16:18:35 -07001788 pfn_t pfn, pgprot_t prot, bool mkwrite)
Nick Piggin423bad62008-04-28 02:13:01 -07001789{
1790 struct mm_struct *mm = vma->vm_mm;
1791 int retval;
1792 pte_t *pte, entry;
1793 spinlock_t *ptl;
1794
1795 retval = -ENOMEM;
1796 pte = get_locked_pte(mm, addr, &ptl);
1797 if (!pte)
1798 goto out;
1799 retval = -EBUSY;
Ross Zwislerb2770da62017-09-06 16:18:35 -07001800 if (!pte_none(*pte)) {
1801 if (mkwrite) {
1802 /*
1803 * For read faults on private mappings the PFN passed
1804 * in may not match the PFN we have mapped if the
1805 * mapped PFN is a writeable COW page. In the mkwrite
1806 * case we are creating a writable PTE for a shared
1807 * mapping and we expect the PFNs to match.
1808 */
1809 if (WARN_ON_ONCE(pte_pfn(*pte) != pfn_t_to_pfn(pfn)))
1810 goto out_unlock;
1811 entry = *pte;
1812 goto out_mkwrite;
1813 } else
1814 goto out_unlock;
1815 }
Nick Piggin423bad62008-04-28 02:13:01 -07001816
1817 /* Ok, finally just insert the thing.. */
Dan Williams01c8f1c2016-01-15 16:56:40 -08001818 if (pfn_t_devmap(pfn))
1819 entry = pte_mkdevmap(pfn_t_pte(pfn, prot));
1820 else
1821 entry = pte_mkspecial(pfn_t_pte(pfn, prot));
Ross Zwislerb2770da62017-09-06 16:18:35 -07001822
1823out_mkwrite:
1824 if (mkwrite) {
1825 entry = pte_mkyoung(entry);
1826 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1827 }
1828
Nick Piggin423bad62008-04-28 02:13:01 -07001829 set_pte_at(mm, addr, pte, entry);
Russell King4b3073e2009-12-18 16:40:18 +00001830 update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
Nick Piggin423bad62008-04-28 02:13:01 -07001831
1832 retval = 0;
1833out_unlock:
1834 pte_unmap_unlock(pte, ptl);
1835out:
1836 return retval;
1837}
1838
Nick Piggine0dc0d82007-02-12 00:51:36 -08001839/**
1840 * vm_insert_pfn - insert single pfn into user vma
1841 * @vma: user vma to map to
1842 * @addr: target user address of this page
1843 * @pfn: source kernel pfn
1844 *
Robert P. J. Dayc462f172012-10-08 16:33:43 -07001845 * Similar to vm_insert_page, this allows drivers to insert individual pages
Nick Piggine0dc0d82007-02-12 00:51:36 -08001846 * they've allocated into a user vma. Same comments apply.
1847 *
1848 * This function should only be called from a vm_ops->fault handler, and
1849 * in that case the handler should return NULL.
Nick Piggin0d71d10a2008-07-23 21:27:05 -07001850 *
1851 * vma cannot be a COW mapping.
1852 *
1853 * As this is called only for pages that do not currently exist, we
1854 * do not need to flush old virtual caches or the TLB.
Nick Piggine0dc0d82007-02-12 00:51:36 -08001855 */
1856int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
Nick Piggin423bad62008-04-28 02:13:01 -07001857 unsigned long pfn)
Nick Piggine0dc0d82007-02-12 00:51:36 -08001858{
Andy Lutomirski1745cbc2015-12-29 20:12:20 -08001859 return vm_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot);
1860}
1861EXPORT_SYMBOL(vm_insert_pfn);
1862
1863/**
1864 * vm_insert_pfn_prot - insert single pfn into user vma with specified pgprot
1865 * @vma: user vma to map to
1866 * @addr: target user address of this page
1867 * @pfn: source kernel pfn
1868 * @pgprot: pgprot flags for the inserted page
1869 *
1870 * This is exactly like vm_insert_pfn, except that it allows drivers to
1871 * to override pgprot on a per-page basis.
1872 *
1873 * This only makes sense for IO mappings, and it makes no sense for
1874 * cow mappings. In general, using multiple vmas is preferable;
1875 * vm_insert_pfn_prot should only be used if using multiple VMAs is
1876 * impractical.
1877 */
1878int vm_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
1879 unsigned long pfn, pgprot_t pgprot)
1880{
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001881 int ret;
Nick Piggin7e675132008-04-28 02:13:00 -07001882 /*
1883 * Technically, architectures with pte_special can avoid all these
1884 * restrictions (same for remap_pfn_range). However we would like
1885 * consistency in testing and feature parity among all, so we should
1886 * try to keep these invariants in place for everybody.
1887 */
Jared Hulbertb379d792008-04-28 02:12:58 -07001888 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
1889 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
1890 (VM_PFNMAP|VM_MIXEDMAP));
1891 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
1892 BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
Nick Piggine0dc0d82007-02-12 00:51:36 -08001893
Nick Piggin423bad62008-04-28 02:13:01 -07001894 if (addr < vma->vm_start || addr >= vma->vm_end)
1895 return -EFAULT;
Borislav Petkov308a0472016-10-26 19:43:43 +02001896
Andi Kleena4116332018-06-13 15:48:27 -07001897 if (!pfn_modify_allowed(pfn, pgprot))
1898 return -EACCES;
1899
Borislav Petkov308a0472016-10-26 19:43:43 +02001900 track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV));
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001901
Ross Zwislerb2770da62017-09-06 16:18:35 -07001902 ret = insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot,
1903 false);
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001904
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001905 return ret;
Nick Piggine0dc0d82007-02-12 00:51:36 -08001906}
Andy Lutomirski1745cbc2015-12-29 20:12:20 -08001907EXPORT_SYMBOL(vm_insert_pfn_prot);
Nick Piggine0dc0d82007-02-12 00:51:36 -08001908
Ross Zwislerb2770da62017-09-06 16:18:35 -07001909static int __vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
1910 pfn_t pfn, bool mkwrite)
Nick Piggin423bad62008-04-28 02:13:01 -07001911{
Dan Williams87744ab2016-10-07 17:00:18 -07001912 pgprot_t pgprot = vma->vm_page_prot;
1913
Nick Piggin423bad62008-04-28 02:13:01 -07001914 BUG_ON(!(vma->vm_flags & VM_MIXEDMAP));
1915
1916 if (addr < vma->vm_start || addr >= vma->vm_end)
1917 return -EFAULT;
Borislav Petkov308a0472016-10-26 19:43:43 +02001918
1919 track_pfn_insert(vma, &pgprot, pfn);
Nick Piggin423bad62008-04-28 02:13:01 -07001920
Andi Kleena4116332018-06-13 15:48:27 -07001921 if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot))
1922 return -EACCES;
1923
Nick Piggin423bad62008-04-28 02:13:01 -07001924 /*
1925 * If we don't have pte special, then we have to use the pfn_valid()
1926 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
1927 * refcount the page if pfn_valid is true (hence insert_page rather
Hugh Dickins62eede62009-09-21 17:03:34 -07001928 * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP
1929 * without pte special, it would there be refcounted as a normal page.
Nick Piggin423bad62008-04-28 02:13:01 -07001930 */
Dan Williams03fc2da2016-01-26 09:48:05 -08001931 if (!HAVE_PTE_SPECIAL && !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) {
Nick Piggin423bad62008-04-28 02:13:01 -07001932 struct page *page;
1933
Dan Williams03fc2da2016-01-26 09:48:05 -08001934 /*
1935 * At this point we are committed to insert_page()
1936 * regardless of whether the caller specified flags that
1937 * result in pfn_t_has_page() == false.
1938 */
1939 page = pfn_to_page(pfn_t_to_pfn(pfn));
Dan Williams87744ab2016-10-07 17:00:18 -07001940 return insert_page(vma, addr, page, pgprot);
Nick Piggin423bad62008-04-28 02:13:01 -07001941 }
Ross Zwislerb2770da62017-09-06 16:18:35 -07001942 return insert_pfn(vma, addr, pfn, pgprot, mkwrite);
1943}
1944
1945int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
1946 pfn_t pfn)
1947{
1948 return __vm_insert_mixed(vma, addr, pfn, false);
1949
Nick Piggin423bad62008-04-28 02:13:01 -07001950}
1951EXPORT_SYMBOL(vm_insert_mixed);
1952
Ross Zwislerb2770da62017-09-06 16:18:35 -07001953int vm_insert_mixed_mkwrite(struct vm_area_struct *vma, unsigned long addr,
1954 pfn_t pfn)
1955{
1956 return __vm_insert_mixed(vma, addr, pfn, true);
1957}
1958EXPORT_SYMBOL(vm_insert_mixed_mkwrite);
1959
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001960/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 * maps a range of physical memory into the requested pages. the old
1962 * mappings are removed. any references to nonexistent pages results
1963 * in null mappings (currently treated as "copy-on-access")
1964 */
1965static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
1966 unsigned long addr, unsigned long end,
1967 unsigned long pfn, pgprot_t prot)
1968{
1969 pte_t *pte;
Hugh Dickinsc74df322005-10-29 18:16:23 -07001970 spinlock_t *ptl;
Andi Kleena4116332018-06-13 15:48:27 -07001971 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Hugh Dickinsc74df322005-10-29 18:16:23 -07001973 pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (!pte)
1975 return -ENOMEM;
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001976 arch_enter_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 do {
1978 BUG_ON(!pte_none(*pte));
Andi Kleena4116332018-06-13 15:48:27 -07001979 if (!pfn_modify_allowed(pfn, prot)) {
1980 err = -EACCES;
1981 break;
1982 }
Nick Piggin7e675132008-04-28 02:13:00 -07001983 set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 pfn++;
1985 } while (pte++, addr += PAGE_SIZE, addr != end);
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001986 arch_leave_lazy_mmu_mode();
Hugh Dickinsc74df322005-10-29 18:16:23 -07001987 pte_unmap_unlock(pte - 1, ptl);
Andi Kleena4116332018-06-13 15:48:27 -07001988 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
1991static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
1992 unsigned long addr, unsigned long end,
1993 unsigned long pfn, pgprot_t prot)
1994{
1995 pmd_t *pmd;
1996 unsigned long next;
Andi Kleena4116332018-06-13 15:48:27 -07001997 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 pfn -= addr >> PAGE_SHIFT;
2000 pmd = pmd_alloc(mm, pud, addr);
2001 if (!pmd)
2002 return -ENOMEM;
Andrea Arcangelif66055ab2011-01-13 15:46:54 -08002003 VM_BUG_ON(pmd_trans_huge(*pmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 do {
2005 next = pmd_addr_end(addr, end);
Andi Kleena4116332018-06-13 15:48:27 -07002006 err = remap_pte_range(mm, pmd, addr, next,
2007 pfn + (addr >> PAGE_SHIFT), prot);
2008 if (err)
2009 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 } while (pmd++, addr = next, addr != end);
2011 return 0;
2012}
2013
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002014static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 unsigned long addr, unsigned long end,
2016 unsigned long pfn, pgprot_t prot)
2017{
2018 pud_t *pud;
2019 unsigned long next;
Andi Kleena4116332018-06-13 15:48:27 -07002020 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 pfn -= addr >> PAGE_SHIFT;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002023 pud = pud_alloc(mm, p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (!pud)
2025 return -ENOMEM;
2026 do {
2027 next = pud_addr_end(addr, end);
Andi Kleena4116332018-06-13 15:48:27 -07002028 err = remap_pmd_range(mm, pud, addr, next,
2029 pfn + (addr >> PAGE_SHIFT), prot);
2030 if (err)
2031 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 } while (pud++, addr = next, addr != end);
2033 return 0;
2034}
2035
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002036static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd,
2037 unsigned long addr, unsigned long end,
2038 unsigned long pfn, pgprot_t prot)
2039{
2040 p4d_t *p4d;
2041 unsigned long next;
Andi Kleena4116332018-06-13 15:48:27 -07002042 int err;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002043
2044 pfn -= addr >> PAGE_SHIFT;
2045 p4d = p4d_alloc(mm, pgd, addr);
2046 if (!p4d)
2047 return -ENOMEM;
2048 do {
2049 next = p4d_addr_end(addr, end);
Andi Kleena4116332018-06-13 15:48:27 -07002050 err = remap_pud_range(mm, p4d, addr, next,
2051 pfn + (addr >> PAGE_SHIFT), prot);
2052 if (err)
2053 return err;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002054 } while (p4d++, addr = next, addr != end);
2055 return 0;
2056}
2057
Rolf Eike Beerbfa5bf62006-09-25 23:31:22 -07002058/**
2059 * remap_pfn_range - remap kernel memory to userspace
2060 * @vma: user vma to map to
2061 * @addr: target user address to start at
2062 * @pfn: physical address of kernel memory
2063 * @size: size of map area
2064 * @prot: page protection flags for this mapping
2065 *
2066 * Note: this is only safe if the mm semaphore is held when called.
2067 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
2069 unsigned long pfn, unsigned long size, pgprot_t prot)
2070{
2071 pgd_t *pgd;
2072 unsigned long next;
Hugh Dickins2d15cab2005-06-25 14:54:33 -07002073 unsigned long end = addr + PAGE_ALIGN(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 struct mm_struct *mm = vma->vm_mm;
Yongji Xied5957d22016-05-20 16:57:41 -07002075 unsigned long remap_pfn = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 int err;
2077
2078 /*
2079 * Physically remapped pages are special. Tell the
2080 * rest of the world about it:
2081 * VM_IO tells people not to look at these pages
2082 * (accesses can have side effects).
Linus Torvalds6aab3412005-11-28 14:34:23 -08002083 * VM_PFNMAP tells the core MM that the base pages are just
2084 * raw PFN mappings, and do not have a "struct page" associated
2085 * with them.
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07002086 * VM_DONTEXPAND
2087 * Disable vma merging and expanding with mremap().
2088 * VM_DONTDUMP
2089 * Omit vma from core dump, even when VM_IO turned off.
Linus Torvaldsfb155c12005-12-11 19:46:02 -08002090 *
2091 * There's a horrible special case to handle copy-on-write
2092 * behaviour that some programs depend on. We mark the "original"
2093 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07002094 * See vm_normal_page() for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 */
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07002096 if (is_cow_mapping(vma->vm_flags)) {
2097 if (addr != vma->vm_start || end != vma->vm_end)
2098 return -EINVAL;
Linus Torvaldsfb155c12005-12-11 19:46:02 -08002099 vma->vm_pgoff = pfn;
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07002100 }
2101
Yongji Xied5957d22016-05-20 16:57:41 -07002102 err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size));
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07002103 if (err)
venkatesh.pallipadi@intel.com3c8bb732008-12-18 11:41:27 -08002104 return -EINVAL;
Linus Torvaldsfb155c12005-12-11 19:46:02 -08002105
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07002106 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 BUG_ON(addr >= end);
2109 pfn -= addr >> PAGE_SHIFT;
2110 pgd = pgd_offset(mm, addr);
2111 flush_cache_range(vma, addr, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 do {
2113 next = pgd_addr_end(addr, end);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002114 err = remap_p4d_range(mm, pgd, addr, next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 pfn + (addr >> PAGE_SHIFT), prot);
2116 if (err)
2117 break;
2118 } while (pgd++, addr = next, addr != end);
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08002119
2120 if (err)
Yongji Xied5957d22016-05-20 16:57:41 -07002121 untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size));
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08002122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return err;
2124}
2125EXPORT_SYMBOL(remap_pfn_range);
2126
Linus Torvaldsb4cbb192013-04-16 13:45:37 -07002127/**
2128 * vm_iomap_memory - remap memory to userspace
2129 * @vma: user vma to map to
2130 * @start: start of area
2131 * @len: size of area
2132 *
2133 * This is a simplified io_remap_pfn_range() for common driver use. The
2134 * driver just needs to give us the physical memory range to be mapped,
2135 * we'll figure out the rest from the vma information.
2136 *
2137 * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
2138 * whatever write-combining details or similar.
2139 */
2140int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
2141{
2142 unsigned long vm_len, pfn, pages;
2143
2144 /* Check that the physical memory area passed in looks valid */
2145 if (start + len < start)
2146 return -EINVAL;
2147 /*
2148 * You *really* shouldn't map things that aren't page-aligned,
2149 * but we've historically allowed it because IO memory might
2150 * just have smaller alignment.
2151 */
2152 len += start & ~PAGE_MASK;
2153 pfn = start >> PAGE_SHIFT;
2154 pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
2155 if (pfn + pages < pfn)
2156 return -EINVAL;
2157
2158 /* We start the mapping 'vm_pgoff' pages into the area */
2159 if (vma->vm_pgoff > pages)
2160 return -EINVAL;
2161 pfn += vma->vm_pgoff;
2162 pages -= vma->vm_pgoff;
2163
2164 /* Can we fit all of the mapping? */
2165 vm_len = vma->vm_end - vma->vm_start;
2166 if (vm_len >> PAGE_SHIFT > pages)
2167 return -EINVAL;
2168
2169 /* Ok, let it rip */
2170 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
2171}
2172EXPORT_SYMBOL(vm_iomap_memory);
2173
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002174static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
2175 unsigned long addr, unsigned long end,
2176 pte_fn_t fn, void *data)
2177{
2178 pte_t *pte;
2179 int err;
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08002180 pgtable_t token;
Borislav Petkov94909912007-05-06 14:49:17 -07002181 spinlock_t *uninitialized_var(ptl);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002182
2183 pte = (mm == &init_mm) ?
2184 pte_alloc_kernel(pmd, addr) :
2185 pte_alloc_map_lock(mm, pmd, addr, &ptl);
2186 if (!pte)
2187 return -ENOMEM;
2188
2189 BUG_ON(pmd_huge(*pmd));
2190
Jeremy Fitzhardinge38e0edb2009-01-06 14:39:21 -08002191 arch_enter_lazy_mmu_mode();
2192
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08002193 token = pmd_pgtable(*pmd);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002194
2195 do {
Daisuke Nishimurac36987e2009-10-26 16:50:23 -07002196 err = fn(pte++, token, addr, data);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002197 if (err)
2198 break;
Daisuke Nishimurac36987e2009-10-26 16:50:23 -07002199 } while (addr += PAGE_SIZE, addr != end);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002200
Jeremy Fitzhardinge38e0edb2009-01-06 14:39:21 -08002201 arch_leave_lazy_mmu_mode();
2202
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002203 if (mm != &init_mm)
2204 pte_unmap_unlock(pte-1, ptl);
2205 return err;
2206}
2207
2208static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
2209 unsigned long addr, unsigned long end,
2210 pte_fn_t fn, void *data)
2211{
2212 pmd_t *pmd;
2213 unsigned long next;
2214 int err;
2215
Andi Kleenceb86872008-07-23 21:27:50 -07002216 BUG_ON(pud_huge(*pud));
2217
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002218 pmd = pmd_alloc(mm, pud, addr);
2219 if (!pmd)
2220 return -ENOMEM;
2221 do {
2222 next = pmd_addr_end(addr, end);
2223 err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
2224 if (err)
2225 break;
2226 } while (pmd++, addr = next, addr != end);
2227 return err;
2228}
2229
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002230static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d,
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002231 unsigned long addr, unsigned long end,
2232 pte_fn_t fn, void *data)
2233{
2234 pud_t *pud;
2235 unsigned long next;
2236 int err;
2237
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002238 pud = pud_alloc(mm, p4d, addr);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002239 if (!pud)
2240 return -ENOMEM;
2241 do {
2242 next = pud_addr_end(addr, end);
2243 err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
2244 if (err)
2245 break;
2246 } while (pud++, addr = next, addr != end);
2247 return err;
2248}
2249
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002250static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd,
2251 unsigned long addr, unsigned long end,
2252 pte_fn_t fn, void *data)
2253{
2254 p4d_t *p4d;
2255 unsigned long next;
2256 int err;
2257
2258 p4d = p4d_alloc(mm, pgd, addr);
2259 if (!p4d)
2260 return -ENOMEM;
2261 do {
2262 next = p4d_addr_end(addr, end);
2263 err = apply_to_pud_range(mm, p4d, addr, next, fn, data);
2264 if (err)
2265 break;
2266 } while (p4d++, addr = next, addr != end);
2267 return err;
2268}
2269
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002270/*
2271 * Scan a region of virtual memory, filling in page tables as necessary
2272 * and calling a provided function on each leaf page table.
2273 */
2274int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2275 unsigned long size, pte_fn_t fn, void *data)
2276{
2277 pgd_t *pgd;
2278 unsigned long next;
Jeremy Fitzhardinge57250a52010-08-09 17:19:52 -07002279 unsigned long end = addr + size;
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002280 int err;
2281
Mika Penttilä9cb65bc2016-03-15 14:56:45 -07002282 if (WARN_ON(addr >= end))
2283 return -EINVAL;
2284
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002285 pgd = pgd_offset(mm, addr);
2286 do {
2287 next = pgd_addr_end(addr, end);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002288 err = apply_to_p4d_range(mm, pgd, addr, next, fn, data);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002289 if (err)
2290 break;
2291 } while (pgd++, addr = next, addr != end);
Jeremy Fitzhardinge57250a52010-08-09 17:19:52 -07002292
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002293 return err;
2294}
2295EXPORT_SYMBOL_GPL(apply_to_page_range);
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297/*
Kirill A. Shutemov9b4bdd22015-02-10 14:09:51 -08002298 * handle_pte_fault chooses page fault handler according to an entry which was
2299 * read non-atomically. Before making any commitment, on those architectures
2300 * or configurations (e.g. i386 with PAE) which might give a mix of unmatched
2301 * parts, do_swap_page must check under lock before unmapping the pte and
2302 * proceeding (but do_wp_page is only called after already making such a check;
Ryota Ozakia335b2e2011-02-10 13:56:28 +09002303 * and do_anonymous_page can safely check later on).
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002304 */
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07002305static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002306 pte_t *page_table, pte_t orig_pte)
2307{
2308 int same = 1;
2309#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
2310 if (sizeof(pte_t) > sizeof(unsigned long)) {
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07002311 spinlock_t *ptl = pte_lockptr(mm, pmd);
2312 spin_lock(ptl);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002313 same = pte_same(*page_table, orig_pte);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07002314 spin_unlock(ptl);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002315 }
2316#endif
2317 pte_unmap(page_table);
2318 return same;
2319}
2320
Atsushi Nemoto9de455b2006-12-12 17:14:55 +00002321static 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 -08002322{
Dan Williams0abdd7a2014-01-21 15:48:12 -08002323 debug_dma_assert_idle(src);
2324
Linus Torvalds6aab3412005-11-28 14:34:23 -08002325 /*
2326 * If the source page was a PFN mapping, we don't have
2327 * a "struct page" for it. We do a best-effort copy by
2328 * just copying from the original user address. If that
2329 * fails, we just zero-fill it. Live with it.
2330 */
2331 if (unlikely(!src)) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08002332 void *kaddr = kmap_atomic(dst);
Linus Torvalds5d2a2dbbc2005-11-29 14:07:55 -08002333 void __user *uaddr = (void __user *)(va & PAGE_MASK);
2334
2335 /*
2336 * This really shouldn't fail, because the page is there
2337 * in the page tables. But it might just be unreadable,
2338 * in which case we just give up and fill the result with
2339 * zeroes.
2340 */
2341 if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
Jan Beulich3ecb01d2010-10-26 14:22:27 -07002342 clear_page(kaddr);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002343 kunmap_atomic(kaddr);
Dmitriy Monakhovc4ec7b02006-10-19 23:29:08 -07002344 flush_dcache_page(dst);
Nick Piggin0ed361d2008-02-04 22:29:34 -08002345 } else
2346 copy_user_highpage(dst, src, va, vma);
Linus Torvalds6aab3412005-11-28 14:34:23 -08002347}
2348
Michal Hockoc20cd452016-01-14 15:20:12 -08002349static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma)
2350{
2351 struct file *vm_file = vma->vm_file;
2352
2353 if (vm_file)
2354 return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO;
2355
2356 /*
2357 * Special mappings (e.g. VDSO) do not have any file so fake
2358 * a default GFP_KERNEL for them.
2359 */
2360 return GFP_KERNEL;
2361}
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363/*
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002364 * Notify the address space that the page is about to become writable so that
2365 * it can prohibit this or wait for the page to get into an appropriate state.
2366 *
2367 * We do this without the lock held, so that it can sleep if it needs to.
2368 */
Jan Kara38b8cb72016-12-14 15:07:30 -08002369static int do_page_mkwrite(struct vm_fault *vmf)
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002370{
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002371 int ret;
Jan Kara38b8cb72016-12-14 15:07:30 -08002372 struct page *page = vmf->page;
2373 unsigned int old_flags = vmf->flags;
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002374
Jan Kara38b8cb72016-12-14 15:07:30 -08002375 vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002376
Dave Jiang11bac802017-02-24 14:56:41 -08002377 ret = vmf->vma->vm_ops->page_mkwrite(vmf);
Jan Kara38b8cb72016-12-14 15:07:30 -08002378 /* Restore original flags so that caller is not surprised */
2379 vmf->flags = old_flags;
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002380 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
2381 return ret;
2382 if (unlikely(!(ret & VM_FAULT_LOCKED))) {
2383 lock_page(page);
2384 if (!page->mapping) {
2385 unlock_page(page);
2386 return 0; /* retry */
2387 }
2388 ret |= VM_FAULT_LOCKED;
2389 } else
2390 VM_BUG_ON_PAGE(!PageLocked(page), page);
2391 return ret;
2392}
2393
2394/*
Jan Kara97ba0c22016-12-14 15:07:27 -08002395 * Handle dirtying of a page in shared file mapping on a write fault.
2396 *
2397 * The function expects the page to be locked and unlocks it.
2398 */
2399static void fault_dirty_shared_page(struct vm_area_struct *vma,
2400 struct page *page)
2401{
2402 struct address_space *mapping;
2403 bool dirtied;
2404 bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite;
2405
2406 dirtied = set_page_dirty(page);
2407 VM_BUG_ON_PAGE(PageAnon(page), page);
2408 /*
2409 * Take a local copy of the address_space - page.mapping may be zeroed
2410 * by truncate after unlock_page(). The address_space itself remains
2411 * pinned by vma->vm_file's reference. We rely on unlock_page()'s
2412 * release semantics to prevent the compiler from undoing this copying.
2413 */
2414 mapping = page_rmapping(page);
2415 unlock_page(page);
2416
2417 if ((dirtied || page_mkwrite) && mapping) {
2418 /*
2419 * Some device drivers do not set page.mapping
2420 * but still dirty their pages
2421 */
2422 balance_dirty_pages_ratelimited(mapping);
2423 }
2424
2425 if (!page_mkwrite)
2426 file_update_time(vma->vm_file);
2427}
2428
2429/*
Shachar Raindel4e047f82015-04-14 15:46:25 -07002430 * Handle write page faults for pages that can be reused in the current vma
2431 *
2432 * This can happen either due to the mapping being with the VM_SHARED flag,
2433 * or due to us being the last reference standing to the page. In either
2434 * case, all we need to do here is to mark the page as writable and update
2435 * any related book-keeping.
2436 */
Jan Kara997dd982016-12-14 15:07:36 -08002437static inline void wp_page_reuse(struct vm_fault *vmf)
Jan Kara82b0f8c2016-12-14 15:06:58 -08002438 __releases(vmf->ptl)
Shachar Raindel4e047f82015-04-14 15:46:25 -07002439{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002440 struct vm_area_struct *vma = vmf->vma;
Jan Karaa41b70d2016-12-14 15:07:33 -08002441 struct page *page = vmf->page;
Shachar Raindel4e047f82015-04-14 15:46:25 -07002442 pte_t entry;
2443 /*
2444 * Clear the pages cpupid information as the existing
2445 * information potentially belongs to a now completely
2446 * unrelated process.
2447 */
2448 if (page)
2449 page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1);
2450
Jan Kara29943022016-12-14 15:07:16 -08002451 flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
2452 entry = pte_mkyoung(vmf->orig_pte);
Shachar Raindel4e047f82015-04-14 15:46:25 -07002453 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Jan Kara82b0f8c2016-12-14 15:06:58 -08002454 if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1))
2455 update_mmu_cache(vma, vmf->address, vmf->pte);
2456 pte_unmap_unlock(vmf->pte, vmf->ptl);
Shachar Raindel4e047f82015-04-14 15:46:25 -07002457}
2458
2459/*
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002460 * Handle the case of a page which we actually need to copy to a new page.
2461 *
2462 * Called with mmap_sem locked and the old page referenced, but
2463 * without the ptl held.
2464 *
2465 * High level logic flow:
2466 *
2467 * - Allocate a page, copy the content of the old page to the new one.
2468 * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc.
2469 * - Take the PTL. If the pte changed, bail out and release the allocated page
2470 * - If the pte is still the way we remember it, update the page table and all
2471 * relevant references. This includes dropping the reference the page-table
2472 * held to the old page, as well as updating the rmap.
2473 * - In any case, unlock the PTL and drop the reference we took to the old page.
2474 */
Jan Karaa41b70d2016-12-14 15:07:33 -08002475static int wp_page_copy(struct vm_fault *vmf)
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002476{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002477 struct vm_area_struct *vma = vmf->vma;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002478 struct mm_struct *mm = vma->vm_mm;
Jan Karaa41b70d2016-12-14 15:07:33 -08002479 struct page *old_page = vmf->page;
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002480 struct page *new_page = NULL;
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002481 pte_t entry;
2482 int page_copied = 0;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002483 const unsigned long mmun_start = vmf->address & PAGE_MASK;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002484 const unsigned long mmun_end = mmun_start + PAGE_SIZE;
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002485 struct mem_cgroup *memcg;
2486
2487 if (unlikely(anon_vma_prepare(vma)))
2488 goto oom;
2489
Jan Kara29943022016-12-14 15:07:16 -08002490 if (is_zero_pfn(pte_pfn(vmf->orig_pte))) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08002491 new_page = alloc_zeroed_user_highpage_movable(vma,
2492 vmf->address);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002493 if (!new_page)
2494 goto oom;
2495 } else {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002496 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
Jan Kara82b0f8c2016-12-14 15:06:58 -08002497 vmf->address);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002498 if (!new_page)
2499 goto oom;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002500 cow_user_page(new_page, old_page, vmf->address, vma);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002501 }
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002502
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002503 if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg, false))
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002504 goto oom_free_new;
2505
Mel Gormaneb3c24f2015-06-24 16:57:27 -07002506 __SetPageUptodate(new_page);
2507
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002508 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
2509
2510 /*
2511 * Re-check the pte - we dropped the lock
2512 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002513 vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08002514 if (likely(pte_same(*vmf->pte, vmf->orig_pte))) {
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002515 if (old_page) {
2516 if (!PageAnon(old_page)) {
Jerome Marchandeca56ff2016-01-14 15:19:26 -08002517 dec_mm_counter_fast(mm,
2518 mm_counter_file(old_page));
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002519 inc_mm_counter_fast(mm, MM_ANONPAGES);
2520 }
2521 } else {
2522 inc_mm_counter_fast(mm, MM_ANONPAGES);
2523 }
Jan Kara29943022016-12-14 15:07:16 -08002524 flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002525 entry = mk_pte(new_page, vma->vm_page_prot);
2526 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2527 /*
2528 * Clear the pte entry and flush it first, before updating the
2529 * pte with the new entry. This will avoid a race condition
2530 * seen in the presence of one thread doing SMC and another
2531 * thread doing COW.
2532 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002533 ptep_clear_flush_notify(vma, vmf->address, vmf->pte);
2534 page_add_new_anon_rmap(new_page, vma, vmf->address, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002535 mem_cgroup_commit_charge(new_page, memcg, false, false);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002536 lru_cache_add_active_or_unevictable(new_page, vma);
2537 /*
2538 * We call the notify macro here because, when using secondary
2539 * mmu page tables (such as kvm shadow page tables), we want the
2540 * new page to be mapped directly into the secondary page table.
2541 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002542 set_pte_at_notify(mm, vmf->address, vmf->pte, entry);
2543 update_mmu_cache(vma, vmf->address, vmf->pte);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002544 if (old_page) {
2545 /*
2546 * Only after switching the pte to the new page may
2547 * we remove the mapcount here. Otherwise another
2548 * process may come and find the rmap count decremented
2549 * before the pte is switched to the new page, and
2550 * "reuse" the old page writing into it while our pte
2551 * here still points into it and can be read by other
2552 * threads.
2553 *
2554 * The critical issue is to order this
2555 * page_remove_rmap with the ptp_clear_flush above.
2556 * Those stores are ordered by (if nothing else,)
2557 * the barrier present in the atomic_add_negative
2558 * in page_remove_rmap.
2559 *
2560 * Then the TLB flush in ptep_clear_flush ensures that
2561 * no process can access the old page before the
2562 * decremented mapcount is visible. And the old page
2563 * cannot be reused until after the decremented
2564 * mapcount is visible. So transitively, TLBs to
2565 * old page will be flushed before it can be reused.
2566 */
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002567 page_remove_rmap(old_page, false);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002568 }
2569
2570 /* Free the old page.. */
2571 new_page = old_page;
2572 page_copied = 1;
2573 } else {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002574 mem_cgroup_cancel_charge(new_page, memcg, false);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002575 }
2576
2577 if (new_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002578 put_page(new_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002579
Jan Kara82b0f8c2016-12-14 15:06:58 -08002580 pte_unmap_unlock(vmf->pte, vmf->ptl);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002581 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2582 if (old_page) {
2583 /*
2584 * Don't let another task, with possibly unlocked vma,
2585 * keep the mlocked page.
2586 */
2587 if (page_copied && (vma->vm_flags & VM_LOCKED)) {
2588 lock_page(old_page); /* LRU manipulation */
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002589 if (PageMlocked(old_page))
2590 munlock_vma_page(old_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002591 unlock_page(old_page);
2592 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002593 put_page(old_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002594 }
2595 return page_copied ? VM_FAULT_WRITE : 0;
2596oom_free_new:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002597 put_page(new_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002598oom:
2599 if (old_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002600 put_page(old_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002601 return VM_FAULT_OOM;
2602}
2603
Jan Kara66a61972016-12-14 15:07:39 -08002604/**
2605 * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE
2606 * writeable once the page is prepared
2607 *
2608 * @vmf: structure describing the fault
2609 *
2610 * This function handles all that is needed to finish a write page fault in a
2611 * shared mapping due to PTE being read-only once the mapped page is prepared.
2612 * It handles locking of PTE and modifying it. The function returns
2613 * VM_FAULT_WRITE on success, 0 when PTE got changed before we acquired PTE
2614 * lock.
2615 *
2616 * The function expects the page to be locked or other protection against
2617 * concurrent faults / writeback (such as DAX radix tree locks).
2618 */
2619int finish_mkwrite_fault(struct vm_fault *vmf)
2620{
2621 WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED));
2622 vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address,
2623 &vmf->ptl);
2624 /*
2625 * We might have raced with another page fault while we released the
2626 * pte_offset_map_lock.
2627 */
2628 if (!pte_same(*vmf->pte, vmf->orig_pte)) {
2629 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karaa19e2552016-12-14 15:07:42 -08002630 return VM_FAULT_NOPAGE;
Jan Kara66a61972016-12-14 15:07:39 -08002631 }
2632 wp_page_reuse(vmf);
Jan Karaa19e2552016-12-14 15:07:42 -08002633 return 0;
Jan Kara66a61972016-12-14 15:07:39 -08002634}
2635
Boaz Harroshdd906182015-04-15 16:15:11 -07002636/*
2637 * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED
2638 * mapping
2639 */
Jan Kara29943022016-12-14 15:07:16 -08002640static int wp_pfn_shared(struct vm_fault *vmf)
Boaz Harroshdd906182015-04-15 16:15:11 -07002641{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002642 struct vm_area_struct *vma = vmf->vma;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002643
Boaz Harroshdd906182015-04-15 16:15:11 -07002644 if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) {
Boaz Harroshdd906182015-04-15 16:15:11 -07002645 int ret;
2646
Jan Kara82b0f8c2016-12-14 15:06:58 -08002647 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karafe822212016-12-14 15:07:13 -08002648 vmf->flags |= FAULT_FLAG_MKWRITE;
Dave Jiang11bac802017-02-24 14:56:41 -08002649 ret = vma->vm_ops->pfn_mkwrite(vmf);
Jan Kara2f89dc12016-12-14 15:07:50 -08002650 if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))
Boaz Harroshdd906182015-04-15 16:15:11 -07002651 return ret;
Jan Kara66a61972016-12-14 15:07:39 -08002652 return finish_mkwrite_fault(vmf);
Boaz Harroshdd906182015-04-15 16:15:11 -07002653 }
Jan Kara997dd982016-12-14 15:07:36 -08002654 wp_page_reuse(vmf);
2655 return VM_FAULT_WRITE;
Boaz Harroshdd906182015-04-15 16:15:11 -07002656}
2657
Jan Karaa41b70d2016-12-14 15:07:33 -08002658static int wp_page_shared(struct vm_fault *vmf)
Jan Kara82b0f8c2016-12-14 15:06:58 -08002659 __releases(vmf->ptl)
Shachar Raindel93e478d2015-04-14 15:46:35 -07002660{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002661 struct vm_area_struct *vma = vmf->vma;
Shachar Raindel93e478d2015-04-14 15:46:35 -07002662
Jan Karaa41b70d2016-12-14 15:07:33 -08002663 get_page(vmf->page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07002664
Shachar Raindel93e478d2015-04-14 15:46:35 -07002665 if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
2666 int tmp;
2667
Jan Kara82b0f8c2016-12-14 15:06:58 -08002668 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Kara38b8cb72016-12-14 15:07:30 -08002669 tmp = do_page_mkwrite(vmf);
Shachar Raindel93e478d2015-04-14 15:46:35 -07002670 if (unlikely(!tmp || (tmp &
2671 (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
Jan Karaa41b70d2016-12-14 15:07:33 -08002672 put_page(vmf->page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07002673 return tmp;
2674 }
Jan Kara66a61972016-12-14 15:07:39 -08002675 tmp = finish_mkwrite_fault(vmf);
Jan Karaa19e2552016-12-14 15:07:42 -08002676 if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
Jan Karaa41b70d2016-12-14 15:07:33 -08002677 unlock_page(vmf->page);
Jan Karaa41b70d2016-12-14 15:07:33 -08002678 put_page(vmf->page);
Jan Kara66a61972016-12-14 15:07:39 -08002679 return tmp;
Shachar Raindel93e478d2015-04-14 15:46:35 -07002680 }
Jan Kara66a61972016-12-14 15:07:39 -08002681 } else {
2682 wp_page_reuse(vmf);
Jan Kara997dd982016-12-14 15:07:36 -08002683 lock_page(vmf->page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07002684 }
Jan Kara997dd982016-12-14 15:07:36 -08002685 fault_dirty_shared_page(vma, vmf->page);
2686 put_page(vmf->page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07002687
Jan Kara997dd982016-12-14 15:07:36 -08002688 return VM_FAULT_WRITE;
Shachar Raindel93e478d2015-04-14 15:46:35 -07002689}
2690
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002691/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 * This routine handles present pages, when users try to write
2693 * to a shared page. It is done by copying the page to a new address
2694 * and decrementing the shared-page counter for the old page.
2695 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 * Note that this routine assumes that the protection checks have been
2697 * done by the caller (the low-level page fault routine in most cases).
2698 * Thus we can safely just mark it writable once we've done any necessary
2699 * COW.
2700 *
2701 * We also mark the page dirty at this point even though the page will
2702 * change only once the write actually happens. This avoids a few races,
2703 * and potentially makes it more efficient.
2704 *
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002705 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2706 * but allow concurrent faults), with pte both mapped and locked.
2707 * We return with mmap_sem still held, but pte unmapped and unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 */
Jan Kara29943022016-12-14 15:07:16 -08002709static int do_wp_page(struct vm_fault *vmf)
Jan Kara82b0f8c2016-12-14 15:06:58 -08002710 __releases(vmf->ptl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002712 struct vm_area_struct *vma = vmf->vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Jan Karaa41b70d2016-12-14 15:07:33 -08002714 vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte);
2715 if (!vmf->page) {
Peter Zijlstra251b97f2008-07-04 09:59:24 -07002716 /*
Peter Feiner64e45502014-10-13 15:55:46 -07002717 * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
2718 * VM_PFNMAP VMA.
Peter Zijlstra251b97f2008-07-04 09:59:24 -07002719 *
2720 * We should not cow pages in a shared writeable mapping.
Boaz Harroshdd906182015-04-15 16:15:11 -07002721 * Just mark the pages writable and/or call ops->pfn_mkwrite.
Peter Zijlstra251b97f2008-07-04 09:59:24 -07002722 */
2723 if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2724 (VM_WRITE|VM_SHARED))
Jan Kara29943022016-12-14 15:07:16 -08002725 return wp_pfn_shared(vmf);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002726
Jan Kara82b0f8c2016-12-14 15:06:58 -08002727 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karaa41b70d2016-12-14 15:07:33 -08002728 return wp_page_copy(vmf);
Peter Zijlstra251b97f2008-07-04 09:59:24 -07002729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Peter Zijlstrad08b3852006-09-25 23:30:57 -07002731 /*
Peter Zijlstraee6a6452006-09-25 23:31:00 -07002732 * Take out anonymous pages first, anonymous shared vmas are
2733 * not dirty accountable.
Peter Zijlstrad08b3852006-09-25 23:30:57 -07002734 */
Jan Karaa41b70d2016-12-14 15:07:33 -08002735 if (PageAnon(vmf->page) && !PageKsm(vmf->page)) {
Huang Yingba3c4ce2017-09-06 16:22:19 -07002736 int total_map_swapcount;
Jan Karaa41b70d2016-12-14 15:07:33 -08002737 if (!trylock_page(vmf->page)) {
2738 get_page(vmf->page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08002739 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karaa41b70d2016-12-14 15:07:33 -08002740 lock_page(vmf->page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08002741 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
2742 vmf->address, &vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08002743 if (!pte_same(*vmf->pte, vmf->orig_pte)) {
Jan Karaa41b70d2016-12-14 15:07:33 -08002744 unlock_page(vmf->page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08002745 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karaa41b70d2016-12-14 15:07:33 -08002746 put_page(vmf->page);
Shachar Raindel28766802015-04-14 15:46:29 -07002747 return 0;
Hugh Dickinsab967d82009-01-06 14:39:33 -08002748 }
Jan Karaa41b70d2016-12-14 15:07:33 -08002749 put_page(vmf->page);
Peter Zijlstraee6a6452006-09-25 23:31:00 -07002750 }
Huang Yingba3c4ce2017-09-06 16:22:19 -07002751 if (reuse_swap_page(vmf->page, &total_map_swapcount)) {
2752 if (total_map_swapcount == 1) {
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002753 /*
2754 * The page is all ours. Move it to
2755 * our anon_vma so the rmap code will
2756 * not search our parent or siblings.
2757 * Protected against the rmap code by
2758 * the page lock.
2759 */
Jan Karaa41b70d2016-12-14 15:07:33 -08002760 page_move_anon_rmap(vmf->page, vma);
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002761 }
Jan Karaa41b70d2016-12-14 15:07:33 -08002762 unlock_page(vmf->page);
Jan Kara997dd982016-12-14 15:07:36 -08002763 wp_page_reuse(vmf);
2764 return VM_FAULT_WRITE;
Michel Lespinasseb009c022011-01-13 15:46:07 -08002765 }
Jan Karaa41b70d2016-12-14 15:07:33 -08002766 unlock_page(vmf->page);
Peter Zijlstraee6a6452006-09-25 23:31:00 -07002767 } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
Peter Zijlstrad08b3852006-09-25 23:30:57 -07002768 (VM_WRITE|VM_SHARED))) {
Jan Karaa41b70d2016-12-14 15:07:33 -08002769 return wp_page_shared(vmf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
2772 /*
2773 * Ok, we need to copy. Oh, well..
2774 */
Jan Karaa41b70d2016-12-14 15:07:33 -08002775 get_page(vmf->page);
Shachar Raindel28766802015-04-14 15:46:29 -07002776
Jan Kara82b0f8c2016-12-14 15:06:58 -08002777 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karaa41b70d2016-12-14 15:07:33 -08002778 return wp_page_copy(vmf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779}
2780
Peter Zijlstra97a89412011-05-24 17:12:04 -07002781static void unmap_mapping_range_vma(struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 unsigned long start_addr, unsigned long end_addr,
2783 struct zap_details *details)
2784{
Al Virof5cc4ee2012-03-05 14:14:20 -05002785 zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786}
2787
Davidlohr Buesof808c132017-09-08 16:15:08 -07002788static inline void unmap_mapping_range_tree(struct rb_root_cached *root,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 struct zap_details *details)
2790{
2791 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 pgoff_t vba, vea, zba, zea;
2793
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002794 vma_interval_tree_foreach(vma, root,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 details->first_index, details->last_index) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
2797 vba = vma->vm_pgoff;
Libind6e93212013-07-03 15:01:26 -07002798 vea = vba + vma_pages(vma) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 zba = details->first_index;
2800 if (zba < vba)
2801 zba = vba;
2802 zea = details->last_index;
2803 if (zea > vea)
2804 zea = vea;
2805
Peter Zijlstra97a89412011-05-24 17:12:04 -07002806 unmap_mapping_range_vma(vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
2808 ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
Peter Zijlstra97a89412011-05-24 17:12:04 -07002809 details);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 }
2811}
2812
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813/**
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08002814 * unmap_mapping_range - unmap the portion of all mmaps in the specified
2815 * address_space corresponding to the specified page range in the underlying
2816 * file.
2817 *
Martin Waitz3d410882005-06-23 22:05:21 -07002818 * @mapping: the address space containing mmaps to be unmapped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 * @holebegin: byte in first page to unmap, relative to the start of
2820 * the underlying file. This will be rounded down to a PAGE_SIZE
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +10002821 * boundary. Note that this is different from truncate_pagecache(), which
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 * must keep the partial page. In contrast, we must get rid of
2823 * partial pages.
2824 * @holelen: size of prospective hole in bytes. This will be rounded
2825 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
2826 * end of the file.
2827 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
2828 * but 0 when invalidating pagecache, don't throw away private data.
2829 */
2830void unmap_mapping_range(struct address_space *mapping,
2831 loff_t const holebegin, loff_t const holelen, int even_cows)
2832{
Michal Hockoaac45362016-03-25 14:20:24 -07002833 struct zap_details details = { };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 pgoff_t hba = holebegin >> PAGE_SHIFT;
2835 pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
2836
2837 /* Check for overflow. */
2838 if (sizeof(holelen) > sizeof(hlen)) {
2839 long long holeend =
2840 (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
2841 if (holeend & ~(long long)ULONG_MAX)
2842 hlen = ULONG_MAX - hba + 1;
2843 }
2844
Tobin C Harding166f61b2017-02-24 14:59:01 -08002845 details.check_mapping = even_cows ? NULL : mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 details.first_index = hba;
2847 details.last_index = hba + hlen - 1;
2848 if (details.last_index < details.first_index)
2849 details.last_index = ULONG_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Kirill A. Shutemov46c043e2015-09-08 14:59:42 -07002851 i_mmap_lock_write(mapping);
Davidlohr Buesof808c132017-09-08 16:15:08 -07002852 if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 unmap_mapping_range_tree(&mapping->i_mmap, &details);
Kirill A. Shutemov46c043e2015-09-08 14:59:42 -07002854 i_mmap_unlock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855}
2856EXPORT_SYMBOL(unmap_mapping_range);
2857
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858/*
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002859 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2860 * but allow concurrent faults), and pte mapped but not yet locked.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002861 * We return with pte unmapped and unlocked.
2862 *
2863 * We return with the mmap_sem locked or unlocked in the same cases
2864 * as does filemap_fault().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 */
Jan Kara29943022016-12-14 15:07:16 -08002866int do_swap_page(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002868 struct vm_area_struct *vma = vmf->vma;
Huang Yingec560172017-09-06 16:24:36 -07002869 struct page *page = NULL, *swapcache;
Johannes Weiner00501b52014-08-08 14:19:20 -07002870 struct mem_cgroup *memcg;
Huang Yingec560172017-09-06 16:24:36 -07002871 struct vma_swap_readahead swap_ra;
Hugh Dickins65500d22005-10-29 18:15:59 -07002872 swp_entry_t entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 pte_t pte;
Michel Lespinassed065bd82010-10-26 14:21:57 -07002874 int locked;
Rik van Rielad8c2ee2010-08-09 17:19:48 -07002875 int exclusive = 0;
Nick Piggin83c54072007-07-19 01:47:05 -07002876 int ret = 0;
Huang Yingec560172017-09-06 16:24:36 -07002877 bool vma_readahead = swap_use_vma_readahead();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
Huang Yingec560172017-09-06 16:24:36 -07002879 if (vma_readahead)
2880 page = swap_readahead_detect(vmf, &swap_ra);
2881 if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte)) {
2882 if (page)
2883 put_page(page);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002884 goto out;
Huang Yingec560172017-09-06 16:24:36 -07002885 }
Hugh Dickins65500d22005-10-29 18:15:59 -07002886
Jan Kara29943022016-12-14 15:07:16 -08002887 entry = pte_to_swp_entry(vmf->orig_pte);
Andi Kleend1737fd2009-09-16 11:50:06 +02002888 if (unlikely(non_swap_entry(entry))) {
2889 if (is_migration_entry(entry)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08002890 migration_entry_wait(vma->vm_mm, vmf->pmd,
2891 vmf->address);
Jérôme Glisse5042db42017-09-08 16:11:43 -07002892 } else if (is_device_private_entry(entry)) {
2893 /*
2894 * For un-addressable device memory we call the pgmap
2895 * fault handler callback. The callback must migrate
2896 * the page back to some CPU accessible page.
2897 */
2898 ret = device_private_entry_fault(vma, vmf->address, entry,
2899 vmf->flags, vmf->pmd);
Andi Kleend1737fd2009-09-16 11:50:06 +02002900 } else if (is_hwpoison_entry(entry)) {
2901 ret = VM_FAULT_HWPOISON;
2902 } else {
Jan Kara29943022016-12-14 15:07:16 -08002903 print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL);
Hugh Dickinsd99be1a2009-12-14 17:59:04 -08002904 ret = VM_FAULT_SIGBUS;
Andi Kleend1737fd2009-09-16 11:50:06 +02002905 }
Christoph Lameter06972122006-06-23 02:03:35 -07002906 goto out;
2907 }
Shailabh Nagar0ff92242006-07-14 00:24:37 -07002908 delayacct_set_flag(DELAYACCT_PF_SWAPIN);
Huang Yingec560172017-09-06 16:24:36 -07002909 if (!page)
2910 page = lookup_swap_cache(entry, vma_readahead ? vma : NULL,
2911 vmf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 if (!page) {
Huang Yingec560172017-09-06 16:24:36 -07002913 if (vma_readahead)
2914 page = do_swap_page_readahead(entry,
2915 GFP_HIGHUSER_MOVABLE, vmf, &swap_ra);
2916 else
2917 page = swapin_readahead(entry,
2918 GFP_HIGHUSER_MOVABLE, vma, vmf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 if (!page) {
2920 /*
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002921 * Back out if somebody else faulted in this pte
2922 * while we released the pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002924 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
2925 vmf->address, &vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08002926 if (likely(pte_same(*vmf->pte, vmf->orig_pte)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 ret = VM_FAULT_OOM;
Shailabh Nagar0ff92242006-07-14 00:24:37 -07002928 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
Hugh Dickins65500d22005-10-29 18:15:59 -07002929 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 }
2931
2932 /* Had to read the page from swap area: Major fault */
2933 ret = VM_FAULT_MAJOR;
Christoph Lameterf8891e52006-06-30 01:55:45 -07002934 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07002935 count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
Andi Kleend1737fd2009-09-16 11:50:06 +02002936 } else if (PageHWPoison(page)) {
Wu Fengguang71f72522009-12-16 12:19:58 +01002937 /*
2938 * hwpoisoned dirty swapcache pages are kept for killing
2939 * owner processes (which may be unknown at hwpoison time)
2940 */
Andi Kleend1737fd2009-09-16 11:50:06 +02002941 ret = VM_FAULT_HWPOISON;
2942 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
Hugh Dickins56f31802013-02-22 16:36:10 -08002943 swapcache = page;
Andi Kleen4779cb32009-10-14 01:51:41 +02002944 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 }
2946
Hugh Dickins56f31802013-02-22 16:36:10 -08002947 swapcache = page;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002948 locked = lock_page_or_retry(page, vma->vm_mm, vmf->flags);
Rik van Riele709ffd2012-05-29 15:06:18 -07002949
Balbir Singh20a10222007-11-14 17:00:33 -08002950 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
Michel Lespinassed065bd82010-10-26 14:21:57 -07002951 if (!locked) {
2952 ret |= VM_FAULT_RETRY;
2953 goto out_release;
2954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Andrea Arcangeli4969c112010-09-09 16:37:52 -07002956 /*
Hugh Dickins31c4a3d2010-09-19 19:40:22 -07002957 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
2958 * release the swapcache from under us. The page pin, and pte_same
2959 * test below, are not enough to exclude that. Even if it is still
2960 * swapcache, we need to check that the page's swap has not changed.
Andrea Arcangeli4969c112010-09-09 16:37:52 -07002961 */
Hugh Dickins31c4a3d2010-09-19 19:40:22 -07002962 if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val))
Andrea Arcangeli4969c112010-09-09 16:37:52 -07002963 goto out_page;
2964
Jan Kara82b0f8c2016-12-14 15:06:58 -08002965 page = ksm_might_need_to_copy(page, vma, vmf->address);
Hugh Dickinscbf86cf2013-02-22 16:35:08 -08002966 if (unlikely(!page)) {
2967 ret = VM_FAULT_OOM;
2968 page = swapcache;
Hugh Dickinscbf86cf2013-02-22 16:35:08 -08002969 goto out_page;
Hugh Dickins5ad64682009-12-14 17:59:24 -08002970 }
2971
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002972 if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL,
2973 &memcg, false)) {
KAMEZAWA Hiroyuki073e5872008-10-18 20:28:08 -07002974 ret = VM_FAULT_OOM;
Johannes Weinerbc43f752009-04-30 15:08:08 -07002975 goto out_page;
KAMEZAWA Hiroyuki073e5872008-10-18 20:28:08 -07002976 }
2977
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 /*
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002979 * Back out if somebody else already faulted in this pte.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002981 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
2982 &vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08002983 if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte)))
Kirill Korotaevb8107482005-05-16 21:53:50 -07002984 goto out_nomap;
Kirill Korotaevb8107482005-05-16 21:53:50 -07002985
2986 if (unlikely(!PageUptodate(page))) {
2987 ret = VM_FAULT_SIGBUS;
2988 goto out_nomap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 }
2990
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002991 /*
2992 * The page isn't present yet, go ahead with the fault.
2993 *
2994 * Be careful about the sequence of operations here.
2995 * To get its accounting right, reuse_swap_page() must be called
2996 * while the page is counted on swap but not yet in mapcount i.e.
2997 * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
2998 * must be called after the swap_free(), or it will never succeed.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002999 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003001 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
3002 dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 pte = mk_pte(page, vma->vm_page_prot);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003004 if ((vmf->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003006 vmf->flags &= ~FAULT_FLAG_WRITE;
Andrea Arcangeli9a5b4892010-08-09 17:19:49 -07003007 ret |= VM_FAULT_WRITE;
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08003008 exclusive = RMAP_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 flush_icache_page(vma, page);
Jan Kara29943022016-12-14 15:07:16 -08003011 if (pte_swp_soft_dirty(vmf->orig_pte))
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07003012 pte = pte_mksoft_dirty(pte);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003013 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte);
Jan Kara29943022016-12-14 15:07:16 -08003014 vmf->orig_pte = pte;
Johannes Weiner00501b52014-08-08 14:19:20 -07003015 if (page == swapcache) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003016 do_page_add_anon_rmap(page, vma, vmf->address, exclusive);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08003017 mem_cgroup_commit_charge(page, memcg, true, false);
Minchan Kim1a8018f2016-08-02 14:02:25 -07003018 activate_page(page);
Johannes Weiner00501b52014-08-08 14:19:20 -07003019 } else { /* ksm created a completely new copy */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003020 page_add_new_anon_rmap(page, vma, vmf->address, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08003021 mem_cgroup_commit_charge(page, memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07003022 lru_cache_add_active_or_unevictable(page, vma);
3023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07003025 swap_free(entry);
Vladimir Davydov5ccc5ab2016-01-20 15:03:10 -08003026 if (mem_cgroup_swap_full(page) ||
3027 (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -08003028 try_to_free_swap(page);
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07003029 unlock_page(page);
Hugh Dickins56f31802013-02-22 16:36:10 -08003030 if (page != swapcache) {
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003031 /*
3032 * Hold the lock to avoid the swap entry to be reused
3033 * until we take the PT lock for the pte_same() check
3034 * (to avoid false positives from pte_same). For
3035 * further safety release the lock after the swap_free
3036 * so that the swap count won't change under a
3037 * parallel locked swapcache.
3038 */
3039 unlock_page(swapcache);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003040 put_page(swapcache);
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003041 }
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07003042
Jan Kara82b0f8c2016-12-14 15:06:58 -08003043 if (vmf->flags & FAULT_FLAG_WRITE) {
Jan Kara29943022016-12-14 15:07:16 -08003044 ret |= do_wp_page(vmf);
Hugh Dickins61469f12008-03-04 14:29:04 -08003045 if (ret & VM_FAULT_ERROR)
3046 ret &= VM_FAULT_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 goto out;
3048 }
3049
3050 /* No need to invalidate - it was non-present before */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003051 update_mmu_cache(vma, vmf->address, vmf->pte);
Hugh Dickins65500d22005-10-29 18:15:59 -07003052unlock:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003053 pte_unmap_unlock(vmf->pte, vmf->ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054out:
3055 return ret;
Kirill Korotaevb8107482005-05-16 21:53:50 -07003056out_nomap:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08003057 mem_cgroup_cancel_charge(page, memcg, false);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003058 pte_unmap_unlock(vmf->pte, vmf->ptl);
Johannes Weinerbc43f752009-04-30 15:08:08 -07003059out_page:
Kirill Korotaevb8107482005-05-16 21:53:50 -07003060 unlock_page(page);
Andi Kleen4779cb32009-10-14 01:51:41 +02003061out_release:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003062 put_page(page);
Hugh Dickins56f31802013-02-22 16:36:10 -08003063 if (page != swapcache) {
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003064 unlock_page(swapcache);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003065 put_page(swapcache);
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003066 }
Hugh Dickins65500d22005-10-29 18:15:59 -07003067 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
3069
3070/*
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003071 * We enter with non-exclusive mmap_sem (to exclude vma changes,
3072 * but allow concurrent faults), and pte mapped but not yet locked.
3073 * We return with mmap_sem still held, but pte unmapped and unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003075static int do_anonymous_page(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003077 struct vm_area_struct *vma = vmf->vma;
Johannes Weiner00501b52014-08-08 14:19:20 -07003078 struct mem_cgroup *memcg;
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003079 struct page *page;
Michal Hocko6b31d592017-08-18 15:16:15 -07003080 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 pte_t entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Kirill A. Shutemov6b7339f2015-07-06 23:18:37 +03003083 /* File mapping without ->vm_ops ? */
3084 if (vma->vm_flags & VM_SHARED)
3085 return VM_FAULT_SIGBUS;
3086
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003087 /*
3088 * Use pte_alloc() instead of pte_alloc_map(). We can't run
3089 * pte_offset_map() on pmds where a huge pmd might be created
3090 * from a different thread.
3091 *
3092 * pte_alloc_map() is safe to use under down_write(mmap_sem) or when
3093 * parallel threads are excluded by other means.
3094 *
3095 * Here we only have down_read(mmap_sem).
3096 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003097 if (pte_alloc(vma->vm_mm, vmf->pmd, vmf->address))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003098 return VM_FAULT_OOM;
3099
3100 /* See the comment in pte_alloc_one_map() */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003101 if (unlikely(pmd_trans_unstable(vmf->pmd)))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003102 return 0;
3103
Linus Torvalds11ac5522010-08-14 11:44:56 -07003104 /* Use the zero-page for reads */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003105 if (!(vmf->flags & FAULT_FLAG_WRITE) &&
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003106 !mm_forbids_zeropage(vma->vm_mm)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003107 entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address),
Hugh Dickins62eede62009-09-21 17:03:34 -07003108 vma->vm_page_prot));
Jan Kara82b0f8c2016-12-14 15:06:58 -08003109 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
3110 vmf->address, &vmf->ptl);
3111 if (!pte_none(*vmf->pte))
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -07003112 goto unlock;
Michal Hocko6b31d592017-08-18 15:16:15 -07003113 ret = check_stable_address_space(vma->vm_mm);
3114 if (ret)
3115 goto unlock;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07003116 /* Deliver the page fault to userland, check inside PT lock */
3117 if (userfaultfd_missing(vma)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003118 pte_unmap_unlock(vmf->pte, vmf->ptl);
3119 return handle_userfault(vmf, VM_UFFD_MISSING);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07003120 }
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -07003121 goto setpte;
3122 }
3123
Nick Piggin557ed1f2007-10-16 01:24:40 -07003124 /* Allocate our own private page. */
Nick Piggin557ed1f2007-10-16 01:24:40 -07003125 if (unlikely(anon_vma_prepare(vma)))
3126 goto oom;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003127 page = alloc_zeroed_user_highpage_movable(vma, vmf->address);
Nick Piggin557ed1f2007-10-16 01:24:40 -07003128 if (!page)
3129 goto oom;
Mel Gormaneb3c24f2015-06-24 16:57:27 -07003130
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003131 if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, &memcg, false))
Mel Gormaneb3c24f2015-06-24 16:57:27 -07003132 goto oom_free_page;
3133
Minchan Kim52f37622013-04-29 15:08:15 -07003134 /*
3135 * The memory barrier inside __SetPageUptodate makes sure that
3136 * preceeding stores to the page contents become visible before
3137 * the set_pte_at() write.
3138 */
Nick Piggin0ed361d2008-02-04 22:29:34 -08003139 __SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Nick Piggin557ed1f2007-10-16 01:24:40 -07003141 entry = mk_pte(page, vma->vm_page_prot);
Hugh Dickins1ac0cb52009-09-21 17:03:29 -07003142 if (vma->vm_flags & VM_WRITE)
3143 entry = pte_mkwrite(pte_mkdirty(entry));
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003144
Jan Kara82b0f8c2016-12-14 15:06:58 -08003145 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3146 &vmf->ptl);
3147 if (!pte_none(*vmf->pte))
Nick Piggin557ed1f2007-10-16 01:24:40 -07003148 goto release;
Hugh Dickins9ba69292009-09-21 17:02:20 -07003149
Michal Hocko6b31d592017-08-18 15:16:15 -07003150 ret = check_stable_address_space(vma->vm_mm);
3151 if (ret)
3152 goto release;
3153
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07003154 /* Deliver the page fault to userland, check inside PT lock */
3155 if (userfaultfd_missing(vma)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003156 pte_unmap_unlock(vmf->pte, vmf->ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08003157 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003158 put_page(page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003159 return handle_userfault(vmf, VM_UFFD_MISSING);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07003160 }
3161
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003162 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003163 page_add_new_anon_rmap(page, vma, vmf->address, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08003164 mem_cgroup_commit_charge(page, memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07003165 lru_cache_add_active_or_unevictable(page, vma);
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -07003166setpte:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003167 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
3169 /* No need to invalidate - it was non-present before */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003170 update_mmu_cache(vma, vmf->address, vmf->pte);
Hugh Dickins65500d22005-10-29 18:15:59 -07003171unlock:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003172 pte_unmap_unlock(vmf->pte, vmf->ptl);
Michal Hocko6b31d592017-08-18 15:16:15 -07003173 return ret;
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003174release:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08003175 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003176 put_page(page);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003177 goto unlock;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003178oom_free_page:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003179 put_page(page);
Hugh Dickins65500d22005-10-29 18:15:59 -07003180oom:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 return VM_FAULT_OOM;
3182}
3183
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003184/*
3185 * The mmap_sem must have been held on entry, and may have been
3186 * released depending on flags and vma->vm_ops->fault() return value.
3187 * See filemap_fault() and __lock_page_retry().
3188 */
Jan Kara936ca802016-12-14 15:07:10 -08003189static int __do_fault(struct vm_fault *vmf)
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003190{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003191 struct vm_area_struct *vma = vmf->vma;
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003192 int ret;
3193
Michal Hocko8c4da112019-01-08 15:23:07 -08003194 /*
3195 * Preallocate pte before we take page_lock because this might lead to
3196 * deadlocks for memcg reclaim which waits for pages under writeback:
3197 * lock_page(A)
3198 * SetPageWriteback(A)
3199 * unlock_page(A)
3200 * lock_page(B)
3201 * lock_page(B)
3202 * pte_alloc_pne
3203 * shrink_page_list
3204 * wait_on_page_writeback(A)
3205 * SetPageWriteback(B)
3206 * unlock_page(B)
3207 * # flush A, B to clear the writeback
3208 */
3209 if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) {
3210 vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm,
3211 vmf->address);
3212 if (!vmf->prealloc_pte)
3213 return VM_FAULT_OOM;
3214 smp_wmb(); /* See comment in __pte_alloc() */
3215 }
3216
Dave Jiang11bac802017-02-24 14:56:41 -08003217 ret = vma->vm_ops->fault(vmf);
Jan Kara39170482016-12-14 15:07:18 -08003218 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY |
Jan Karab1aa8122016-12-14 15:07:24 -08003219 VM_FAULT_DONE_COW)))
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003220 return ret;
3221
Jan Kara667240e2016-12-14 15:07:07 -08003222 if (unlikely(PageHWPoison(vmf->page))) {
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003223 if (ret & VM_FAULT_LOCKED)
Jan Kara667240e2016-12-14 15:07:07 -08003224 unlock_page(vmf->page);
3225 put_page(vmf->page);
Jan Kara936ca802016-12-14 15:07:10 -08003226 vmf->page = NULL;
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003227 return VM_FAULT_HWPOISON;
3228 }
3229
3230 if (unlikely(!(ret & VM_FAULT_LOCKED)))
Jan Kara667240e2016-12-14 15:07:07 -08003231 lock_page(vmf->page);
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003232 else
Jan Kara667240e2016-12-14 15:07:07 -08003233 VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page);
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003234
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003235 return ret;
3236}
3237
Ross Zwislerd0f0931d2017-06-02 14:46:34 -07003238/*
3239 * The ordering of these checks is important for pmds with _PAGE_DEVMAP set.
3240 * If we check pmd_trans_unstable() first we will trip the bad_pmd() check
3241 * inside of pmd_none_or_trans_huge_or_clear_bad(). This will end up correctly
3242 * returning 1 but not before it spams dmesg with the pmd_clear_bad() output.
3243 */
3244static int pmd_devmap_trans_unstable(pmd_t *pmd)
3245{
3246 return pmd_devmap(*pmd) || pmd_trans_unstable(pmd);
3247}
3248
Jan Kara82b0f8c2016-12-14 15:06:58 -08003249static int pte_alloc_one_map(struct vm_fault *vmf)
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003250{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003251 struct vm_area_struct *vma = vmf->vma;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003252
Jan Kara82b0f8c2016-12-14 15:06:58 -08003253 if (!pmd_none(*vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003254 goto map_pte;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003255 if (vmf->prealloc_pte) {
3256 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
3257 if (unlikely(!pmd_none(*vmf->pmd))) {
3258 spin_unlock(vmf->ptl);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003259 goto map_pte;
3260 }
3261
3262 atomic_long_inc(&vma->vm_mm->nr_ptes);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003263 pmd_populate(vma->vm_mm, vmf->pmd, vmf->prealloc_pte);
3264 spin_unlock(vmf->ptl);
Tobin C Harding7f2b6ce2017-02-24 14:58:59 -08003265 vmf->prealloc_pte = NULL;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003266 } else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd, vmf->address))) {
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003267 return VM_FAULT_OOM;
3268 }
3269map_pte:
3270 /*
3271 * If a huge pmd materialized under us just retry later. Use
Ross Zwislerd0f0931d2017-06-02 14:46:34 -07003272 * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead of
3273 * pmd_trans_huge() to ensure the pmd didn't become pmd_trans_huge
3274 * under us and then back to pmd_none, as a result of MADV_DONTNEED
3275 * running immediately after a huge pmd fault in a different thread of
3276 * this mm, in turn leading to a misleading pmd_trans_huge() retval.
3277 * All we have to ensure is that it is a regular pmd that we can walk
3278 * with pte_offset_map() and we can do that through an atomic read in
3279 * C, which is what pmd_trans_unstable() provides.
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003280 */
Ross Zwislerd0f0931d2017-06-02 14:46:34 -07003281 if (pmd_devmap_trans_unstable(vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003282 return VM_FAULT_NOPAGE;
3283
Ross Zwislerd0f0931d2017-06-02 14:46:34 -07003284 /*
3285 * At this point we know that our vmf->pmd points to a page of ptes
3286 * and it cannot become pmd_none(), pmd_devmap() or pmd_trans_huge()
3287 * for the duration of the fault. If a racing MADV_DONTNEED runs and
3288 * we zap the ptes pointed to by our vmf->pmd, the vmf->ptl will still
3289 * be valid and we will re-check to make sure the vmf->pte isn't
3290 * pte_none() under vmf->ptl protection when we return to
3291 * alloc_set_pte().
3292 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003293 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3294 &vmf->ptl);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003295 return 0;
3296}
3297
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003298#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003299
3300#define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1)
3301static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
3302 unsigned long haddr)
3303{
3304 if (((vma->vm_start >> PAGE_SHIFT) & HPAGE_CACHE_INDEX_MASK) !=
3305 (vma->vm_pgoff & HPAGE_CACHE_INDEX_MASK))
3306 return false;
3307 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
3308 return false;
3309 return true;
3310}
3311
Jan Kara82b0f8c2016-12-14 15:06:58 -08003312static void deposit_prealloc_pte(struct vm_fault *vmf)
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003313{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003314 struct vm_area_struct *vma = vmf->vma;
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003315
Jan Kara82b0f8c2016-12-14 15:06:58 -08003316 pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte);
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003317 /*
3318 * We are going to consume the prealloc table,
3319 * count that as nr_ptes.
3320 */
3321 atomic_long_inc(&vma->vm_mm->nr_ptes);
Tobin C Harding7f2b6ce2017-02-24 14:58:59 -08003322 vmf->prealloc_pte = NULL;
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003323}
3324
Jan Kara82b0f8c2016-12-14 15:06:58 -08003325static int do_set_pmd(struct vm_fault *vmf, struct page *page)
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003326{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003327 struct vm_area_struct *vma = vmf->vma;
3328 bool write = vmf->flags & FAULT_FLAG_WRITE;
3329 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003330 pmd_t entry;
3331 int i, ret;
3332
3333 if (!transhuge_vma_suitable(vma, haddr))
3334 return VM_FAULT_FALLBACK;
3335
3336 ret = VM_FAULT_FALLBACK;
3337 page = compound_head(page);
3338
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003339 /*
3340 * Archs like ppc64 need additonal space to store information
3341 * related to pte entry. Use the preallocated table for that.
3342 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003343 if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) {
3344 vmf->prealloc_pte = pte_alloc_one(vma->vm_mm, vmf->address);
3345 if (!vmf->prealloc_pte)
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003346 return VM_FAULT_OOM;
3347 smp_wmb(); /* See comment in __pte_alloc() */
3348 }
3349
Jan Kara82b0f8c2016-12-14 15:06:58 -08003350 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
3351 if (unlikely(!pmd_none(*vmf->pmd)))
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003352 goto out;
3353
3354 for (i = 0; i < HPAGE_PMD_NR; i++)
3355 flush_icache_page(vma, page + i);
3356
3357 entry = mk_huge_pmd(page, vma->vm_page_prot);
3358 if (write)
3359 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
3360
3361 add_mm_counter(vma->vm_mm, MM_FILEPAGES, HPAGE_PMD_NR);
3362 page_add_file_rmap(page, true);
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003363 /*
3364 * deposit and withdraw with pmd lock held
3365 */
3366 if (arch_needs_pgtable_deposit())
Jan Kara82b0f8c2016-12-14 15:06:58 -08003367 deposit_prealloc_pte(vmf);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003368
Jan Kara82b0f8c2016-12-14 15:06:58 -08003369 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003370
Jan Kara82b0f8c2016-12-14 15:06:58 -08003371 update_mmu_cache_pmd(vma, haddr, vmf->pmd);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003372
3373 /* fault is handled */
3374 ret = 0;
Kirill A. Shutemov95ecedc2016-07-26 15:25:31 -07003375 count_vm_event(THP_FILE_MAPPED);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003376out:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003377 spin_unlock(vmf->ptl);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003378 return ret;
3379}
3380#else
Jan Kara82b0f8c2016-12-14 15:06:58 -08003381static int do_set_pmd(struct vm_fault *vmf, struct page *page)
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003382{
3383 BUILD_BUG();
3384 return 0;
3385}
3386#endif
3387
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003388/**
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003389 * alloc_set_pte - setup new PTE entry for given page and add reverse page
3390 * mapping. If needed, the fucntion allocates page table or use pre-allocated.
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003391 *
Jan Kara82b0f8c2016-12-14 15:06:58 -08003392 * @vmf: fault environment
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003393 * @memcg: memcg to charge page (only for private mappings)
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003394 * @page: page to map
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003395 *
Jan Kara82b0f8c2016-12-14 15:06:58 -08003396 * Caller must take care of unlocking vmf->ptl, if vmf->pte is non-NULL on
3397 * return.
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003398 *
3399 * Target users are page handler itself and implementations of
3400 * vm_ops->map_pages.
3401 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003402int alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg,
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003403 struct page *page)
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003404{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003405 struct vm_area_struct *vma = vmf->vma;
3406 bool write = vmf->flags & FAULT_FLAG_WRITE;
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003407 pte_t entry;
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003408 int ret;
3409
Jan Kara82b0f8c2016-12-14 15:06:58 -08003410 if (pmd_none(*vmf->pmd) && PageTransCompound(page) &&
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003411 IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003412 /* THP on COW? */
3413 VM_BUG_ON_PAGE(memcg, page);
3414
Jan Kara82b0f8c2016-12-14 15:06:58 -08003415 ret = do_set_pmd(vmf, page);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003416 if (ret != VM_FAULT_FALLBACK)
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003417 return ret;
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003418 }
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003419
Jan Kara82b0f8c2016-12-14 15:06:58 -08003420 if (!vmf->pte) {
3421 ret = pte_alloc_one_map(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003422 if (ret)
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003423 return ret;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003424 }
3425
3426 /* Re-check under ptl */
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003427 if (unlikely(!pte_none(*vmf->pte)))
3428 return VM_FAULT_NOPAGE;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003429
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003430 flush_icache_page(vma, page);
3431 entry = mk_pte(page, vma->vm_page_prot);
3432 if (write)
3433 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003434 /* copy-on-write page */
3435 if (write && !(vma->vm_flags & VM_SHARED)) {
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003436 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003437 page_add_new_anon_rmap(page, vma, vmf->address, false);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003438 mem_cgroup_commit_charge(page, memcg, false, false);
3439 lru_cache_add_active_or_unevictable(page, vma);
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003440 } else {
Jerome Marchandeca56ff2016-01-14 15:19:26 -08003441 inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07003442 page_add_file_rmap(page, false);
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003443 }
Jan Kara82b0f8c2016-12-14 15:06:58 -08003444 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003445
3446 /* no need to invalidate: a not-present page won't be cached */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003447 update_mmu_cache(vma, vmf->address, vmf->pte);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003448
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003449 return 0;
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003450}
3451
Jan Kara9118c0c2016-12-14 15:07:21 -08003452
3453/**
3454 * finish_fault - finish page fault once we have prepared the page to fault
3455 *
3456 * @vmf: structure describing the fault
3457 *
3458 * This function handles all that is needed to finish a page fault once the
3459 * page to fault in is prepared. It handles locking of PTEs, inserts PTE for
3460 * given page, adds reverse page mapping, handles memcg charges and LRU
3461 * addition. The function returns 0 on success, VM_FAULT_ code in case of
3462 * error.
3463 *
3464 * The function expects the page to be locked and on success it consumes a
3465 * reference of a page being mapped (for the PTE which maps it).
3466 */
3467int finish_fault(struct vm_fault *vmf)
3468{
3469 struct page *page;
Michal Hocko6b31d592017-08-18 15:16:15 -07003470 int ret = 0;
Jan Kara9118c0c2016-12-14 15:07:21 -08003471
3472 /* Did we COW the page? */
3473 if ((vmf->flags & FAULT_FLAG_WRITE) &&
3474 !(vmf->vma->vm_flags & VM_SHARED))
3475 page = vmf->cow_page;
3476 else
3477 page = vmf->page;
Michal Hocko6b31d592017-08-18 15:16:15 -07003478
3479 /*
3480 * check even for read faults because we might have lost our CoWed
3481 * page
3482 */
3483 if (!(vmf->vma->vm_flags & VM_SHARED))
3484 ret = check_stable_address_space(vmf->vma->vm_mm);
3485 if (!ret)
3486 ret = alloc_set_pte(vmf, vmf->memcg, page);
Jan Kara9118c0c2016-12-14 15:07:21 -08003487 if (vmf->pte)
3488 pte_unmap_unlock(vmf->pte, vmf->ptl);
3489 return ret;
3490}
3491
Kirill A. Shutemov3a910532014-08-06 16:08:07 -07003492static unsigned long fault_around_bytes __read_mostly =
3493 rounddown_pow_of_two(65536);
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003494
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003495#ifdef CONFIG_DEBUG_FS
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003496static int fault_around_bytes_get(void *data, u64 *val)
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003497{
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003498 *val = fault_around_bytes;
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003499 return 0;
3500}
3501
Andrey Ryabininb4903d62014-07-30 16:08:35 -07003502/*
3503 * fault_around_pages() and fault_around_mask() expects fault_around_bytes
3504 * rounded down to nearest page order. It's what do_fault_around() expects to
3505 * see.
3506 */
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003507static int fault_around_bytes_set(void *data, u64 val)
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003508{
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003509 if (val / PAGE_SIZE > PTRS_PER_PTE)
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003510 return -EINVAL;
Andrey Ryabininb4903d62014-07-30 16:08:35 -07003511 if (val > PAGE_SIZE)
3512 fault_around_bytes = rounddown_pow_of_two(val);
3513 else
3514 fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003515 return 0;
3516}
Yevgen Pronenko0a1345f2017-07-10 15:47:17 -07003517DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops,
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003518 fault_around_bytes_get, fault_around_bytes_set, "%llu\n");
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003519
3520static int __init fault_around_debugfs(void)
3521{
3522 void *ret;
3523
Yevgen Pronenko0a1345f2017-07-10 15:47:17 -07003524 ret = debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL,
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003525 &fault_around_bytes_fops);
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003526 if (!ret)
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003527 pr_warn("Failed to create fault_around_bytes in debugfs");
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003528 return 0;
3529}
3530late_initcall(fault_around_debugfs);
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003531#endif
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003532
Kirill A. Shutemov1fdb4122014-06-04 16:10:55 -07003533/*
3534 * do_fault_around() tries to map few pages around the fault address. The hope
3535 * is that the pages will be needed soon and this will lower the number of
3536 * faults to handle.
3537 *
3538 * It uses vm_ops->map_pages() to map the pages, which skips the page if it's
3539 * not ready to be mapped: not up-to-date, locked, etc.
3540 *
3541 * This function is called with the page table lock taken. In the split ptlock
3542 * case the page table lock only protects only those entries which belong to
3543 * the page table corresponding to the fault address.
3544 *
3545 * This function doesn't cross the VMA boundaries, in order to call map_pages()
3546 * only once.
3547 *
3548 * fault_around_pages() defines how many pages we'll try to map.
3549 * do_fault_around() expects it to return a power of two less than or equal to
3550 * PTRS_PER_PTE.
3551 *
3552 * The virtual address of the area that we map is naturally aligned to the
3553 * fault_around_pages() value (and therefore to page order). This way it's
3554 * easier to guarantee that we don't cross page table boundaries.
3555 */
Jan Kara0721ec82016-12-14 15:07:04 -08003556static int do_fault_around(struct vm_fault *vmf)
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003557{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003558 unsigned long address = vmf->address, nr_pages, mask;
Jan Kara0721ec82016-12-14 15:07:04 -08003559 pgoff_t start_pgoff = vmf->pgoff;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003560 pgoff_t end_pgoff;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003561 int off, ret = 0;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003562
Jason Low4db0c3c2015-04-15 16:14:08 -07003563 nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT;
Kirill A. Shutemovaecd6f42014-08-06 16:08:05 -07003564 mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
3565
Jan Kara82b0f8c2016-12-14 15:06:58 -08003566 vmf->address = max(address & mask, vmf->vma->vm_start);
3567 off = ((address - vmf->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003568 start_pgoff -= off;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003569
3570 /*
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003571 * end_pgoff is either end of page table or end of vma
3572 * or fault_around_pages() from start_pgoff, depending what is nearest.
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003573 */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003574 end_pgoff = start_pgoff -
Jan Kara82b0f8c2016-12-14 15:06:58 -08003575 ((vmf->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003576 PTRS_PER_PTE - 1;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003577 end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003578 start_pgoff + nr_pages - 1);
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003579
Jan Kara82b0f8c2016-12-14 15:06:58 -08003580 if (pmd_none(*vmf->pmd)) {
3581 vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm,
3582 vmf->address);
3583 if (!vmf->prealloc_pte)
Vegard Nossumc5f88bd2016-08-02 14:02:22 -07003584 goto out;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003585 smp_wmb(); /* See comment in __pte_alloc() */
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003586 }
3587
Jan Kara82b0f8c2016-12-14 15:06:58 -08003588 vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003589
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003590 /* Huge page is mapped? Page fault is solved */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003591 if (pmd_trans_huge(*vmf->pmd)) {
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003592 ret = VM_FAULT_NOPAGE;
3593 goto out;
3594 }
3595
3596 /* ->map_pages() haven't done anything useful. Cold page cache? */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003597 if (!vmf->pte)
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003598 goto out;
3599
3600 /* check if the page fault is solved */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003601 vmf->pte -= (vmf->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT);
3602 if (!pte_none(*vmf->pte))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003603 ret = VM_FAULT_NOPAGE;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003604 pte_unmap_unlock(vmf->pte, vmf->ptl);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003605out:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003606 vmf->address = address;
3607 vmf->pte = NULL;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003608 return ret;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003609}
3610
Jan Kara0721ec82016-12-14 15:07:04 -08003611static int do_read_fault(struct vm_fault *vmf)
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003612{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003613 struct vm_area_struct *vma = vmf->vma;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003614 int ret = 0;
3615
3616 /*
3617 * Let's call ->map_pages() first and use ->fault() as fallback
3618 * if page by the offset is not ready to be mapped (cold cache or
3619 * something).
3620 */
Kirill A. Shutemov9b4bdd22015-02-10 14:09:51 -08003621 if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) {
Jan Kara0721ec82016-12-14 15:07:04 -08003622 ret = do_fault_around(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003623 if (ret)
3624 return ret;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003625 }
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003626
Jan Kara936ca802016-12-14 15:07:10 -08003627 ret = __do_fault(vmf);
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003628 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3629 return ret;
3630
Jan Kara9118c0c2016-12-14 15:07:21 -08003631 ret |= finish_fault(vmf);
Jan Kara936ca802016-12-14 15:07:10 -08003632 unlock_page(vmf->page);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003633 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
Jan Kara936ca802016-12-14 15:07:10 -08003634 put_page(vmf->page);
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003635 return ret;
3636}
3637
Jan Kara0721ec82016-12-14 15:07:04 -08003638static int do_cow_fault(struct vm_fault *vmf)
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003639{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003640 struct vm_area_struct *vma = vmf->vma;
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003641 int ret;
3642
3643 if (unlikely(anon_vma_prepare(vma)))
3644 return VM_FAULT_OOM;
3645
Jan Kara936ca802016-12-14 15:07:10 -08003646 vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address);
3647 if (!vmf->cow_page)
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003648 return VM_FAULT_OOM;
3649
Jan Kara936ca802016-12-14 15:07:10 -08003650 if (mem_cgroup_try_charge(vmf->cow_page, vma->vm_mm, GFP_KERNEL,
Jan Kara39170482016-12-14 15:07:18 -08003651 &vmf->memcg, false)) {
Jan Kara936ca802016-12-14 15:07:10 -08003652 put_page(vmf->cow_page);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003653 return VM_FAULT_OOM;
3654 }
3655
Jan Kara936ca802016-12-14 15:07:10 -08003656 ret = __do_fault(vmf);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003657 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3658 goto uncharge_out;
Jan Kara39170482016-12-14 15:07:18 -08003659 if (ret & VM_FAULT_DONE_COW)
3660 return ret;
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003661
Jan Karab1aa8122016-12-14 15:07:24 -08003662 copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma);
Jan Kara936ca802016-12-14 15:07:10 -08003663 __SetPageUptodate(vmf->cow_page);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003664
Jan Kara9118c0c2016-12-14 15:07:21 -08003665 ret |= finish_fault(vmf);
Jan Karab1aa8122016-12-14 15:07:24 -08003666 unlock_page(vmf->page);
3667 put_page(vmf->page);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003668 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3669 goto uncharge_out;
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003670 return ret;
3671uncharge_out:
Jan Kara39170482016-12-14 15:07:18 -08003672 mem_cgroup_cancel_charge(vmf->cow_page, vmf->memcg, false);
Jan Kara936ca802016-12-14 15:07:10 -08003673 put_page(vmf->cow_page);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003674 return ret;
3675}
3676
Jan Kara0721ec82016-12-14 15:07:04 -08003677static int do_shared_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003679 struct vm_area_struct *vma = vmf->vma;
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003680 int ret, tmp;
KAMEZAWA Hiroyuki1d65f862011-07-25 17:12:27 -07003681
Jan Kara936ca802016-12-14 15:07:10 -08003682 ret = __do_fault(vmf);
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003683 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003684 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
3686 /*
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003687 * Check if the backing address space wants to know that the page is
3688 * about to become writable
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 */
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07003690 if (vma->vm_ops->page_mkwrite) {
Jan Kara936ca802016-12-14 15:07:10 -08003691 unlock_page(vmf->page);
Jan Kara38b8cb72016-12-14 15:07:30 -08003692 tmp = do_page_mkwrite(vmf);
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07003693 if (unlikely(!tmp ||
3694 (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
Jan Kara936ca802016-12-14 15:07:10 -08003695 put_page(vmf->page);
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07003696 return tmp;
3697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 }
3699
Jan Kara9118c0c2016-12-14 15:07:21 -08003700 ret |= finish_fault(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003701 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
3702 VM_FAULT_RETRY))) {
Jan Kara936ca802016-12-14 15:07:10 -08003703 unlock_page(vmf->page);
3704 put_page(vmf->page);
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003705 return ret;
Peter Zijlstrad08b3852006-09-25 23:30:57 -07003706 }
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003707
Jan Kara97ba0c22016-12-14 15:07:27 -08003708 fault_dirty_shared_page(vma, vmf->page);
KAMEZAWA Hiroyuki1d65f862011-07-25 17:12:27 -07003709 return ret;
Nick Piggin54cb8822007-07-19 01:46:59 -07003710}
Nick Piggind00806b2007-07-19 01:46:57 -07003711
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003712/*
3713 * We enter with non-exclusive mmap_sem (to exclude vma changes,
3714 * but allow concurrent faults).
3715 * The mmap_sem may have been released depending on flags and our
3716 * return value. See filemap_fault() and __lock_page_or_retry().
3717 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003718static int do_fault(struct vm_fault *vmf)
Nick Piggin54cb8822007-07-19 01:46:59 -07003719{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003720 struct vm_area_struct *vma = vmf->vma;
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003721 int ret;
Nick Piggin54cb8822007-07-19 01:46:59 -07003722
Aneesh Kumar K.V6b43a992018-10-26 15:09:01 -07003723 /*
3724 * The VMA was not fully populated on mmap() or missing VM_DONTEXPAND
3725 */
3726 if (!vma->vm_ops->fault) {
3727 /*
3728 * If we find a migration pmd entry or a none pmd entry, which
3729 * should never happen, return SIGBUS
3730 */
3731 if (unlikely(!pmd_present(*vmf->pmd)))
3732 ret = VM_FAULT_SIGBUS;
3733 else {
3734 vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm,
3735 vmf->pmd,
3736 vmf->address,
3737 &vmf->ptl);
3738 /*
3739 * Make sure this is not a temporary clearing of pte
3740 * by holding ptl and checking again. A R/M/W update
3741 * of pte involves: take ptl, clearing the pte so that
3742 * we don't have concurrent modification by hardware
3743 * followed by an update.
3744 */
3745 if (unlikely(pte_none(*vmf->pte)))
3746 ret = VM_FAULT_SIGBUS;
3747 else
3748 ret = VM_FAULT_NOPAGE;
3749
3750 pte_unmap_unlock(vmf->pte, vmf->ptl);
3751 }
3752 } else if (!(vmf->flags & FAULT_FLAG_WRITE))
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003753 ret = do_read_fault(vmf);
3754 else if (!(vma->vm_flags & VM_SHARED))
3755 ret = do_cow_fault(vmf);
3756 else
3757 ret = do_shared_fault(vmf);
3758
3759 /* preallocated pagetable is unused: free it */
3760 if (vmf->prealloc_pte) {
3761 pte_free(vma->vm_mm, vmf->prealloc_pte);
Tobin C Harding7f2b6ce2017-02-24 14:58:59 -08003762 vmf->prealloc_pte = NULL;
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003763 }
3764 return ret;
Nick Piggin54cb8822007-07-19 01:46:59 -07003765}
3766
Rashika Kheriab19a9932014-04-03 14:48:02 -07003767static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
Rik van Riel04bb2f92013-10-07 11:29:36 +01003768 unsigned long addr, int page_nid,
3769 int *flags)
Mel Gorman9532fec2012-11-15 01:24:32 +00003770{
3771 get_page(page);
3772
3773 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01003774 if (page_nid == numa_node_id()) {
Mel Gorman9532fec2012-11-15 01:24:32 +00003775 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01003776 *flags |= TNF_FAULT_LOCAL;
3777 }
Mel Gorman9532fec2012-11-15 01:24:32 +00003778
3779 return mpol_misplaced(page, vma, addr);
3780}
3781
Jan Kara29943022016-12-14 15:07:16 -08003782static int do_numa_page(struct vm_fault *vmf)
Mel Gormand10e63f2012-10-25 14:16:31 +02003783{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003784 struct vm_area_struct *vma = vmf->vma;
Mel Gorman4daae3b2012-11-02 11:33:45 +00003785 struct page *page = NULL;
Mel Gorman8191acb2013-10-07 11:28:45 +01003786 int page_nid = -1;
Peter Zijlstra90572892013-10-07 11:29:20 +01003787 int last_cpupid;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02003788 int target_nid;
Mel Gormanb8593bf2012-11-21 01:18:23 +00003789 bool migrated = false;
Aneesh Kumar K.Vcee216a2017-02-24 14:59:13 -08003790 pte_t pte;
Aneesh Kumar K.V288bc542017-02-24 14:59:16 -08003791 bool was_writable = pte_savedwrite(vmf->orig_pte);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003792 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02003793
3794 /*
Tobin C Harding166f61b2017-02-24 14:59:01 -08003795 * The "pte" at this point cannot be used safely without
3796 * validation through pte_unmap_same(). It's of NUMA type but
3797 * the pfn may be screwed if the read is non atomic.
Tobin C Harding166f61b2017-02-24 14:59:01 -08003798 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003799 vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd);
3800 spin_lock(vmf->ptl);
Aneesh Kumar K.Vcee216a2017-02-24 14:59:13 -08003801 if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003802 pte_unmap_unlock(vmf->pte, vmf->ptl);
Mel Gorman4daae3b2012-11-02 11:33:45 +00003803 goto out;
3804 }
3805
Aneesh Kumar K.Vcee216a2017-02-24 14:59:13 -08003806 /*
3807 * Make it present again, Depending on how arch implementes non
3808 * accessible ptes, some can allow access by kernel mode.
3809 */
3810 pte = ptep_modify_prot_start(vma->vm_mm, vmf->address, vmf->pte);
Mel Gorman4d942462015-02-12 14:58:28 -08003811 pte = pte_modify(pte, vma->vm_page_prot);
3812 pte = pte_mkyoung(pte);
Mel Gormanb191f9b2015-03-25 15:55:40 -07003813 if (was_writable)
3814 pte = pte_mkwrite(pte);
Aneesh Kumar K.Vcee216a2017-02-24 14:59:13 -08003815 ptep_modify_prot_commit(vma->vm_mm, vmf->address, vmf->pte, pte);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003816 update_mmu_cache(vma, vmf->address, vmf->pte);
Mel Gormand10e63f2012-10-25 14:16:31 +02003817
Jan Kara82b0f8c2016-12-14 15:06:58 -08003818 page = vm_normal_page(vma, vmf->address, pte);
Mel Gormand10e63f2012-10-25 14:16:31 +02003819 if (!page) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003820 pte_unmap_unlock(vmf->pte, vmf->ptl);
Mel Gormand10e63f2012-10-25 14:16:31 +02003821 return 0;
3822 }
3823
Kirill A. Shutemove81c4802016-01-15 16:53:49 -08003824 /* TODO: handle PTE-mapped THP */
3825 if (PageCompound(page)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003826 pte_unmap_unlock(vmf->pte, vmf->ptl);
Kirill A. Shutemove81c4802016-01-15 16:53:49 -08003827 return 0;
3828 }
3829
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003830 /*
Mel Gormanbea66fb2015-03-25 15:55:37 -07003831 * Avoid grouping on RO pages in general. RO pages shouldn't hurt as
3832 * much anyway since they can be in shared cache state. This misses
3833 * the case where a mapping is writable but the process never writes
3834 * to it but pte_write gets cleared during protection updates and
3835 * pte_dirty has unpredictable behaviour between PTE scan updates,
3836 * background writeback, dirty balancing and application behaviour.
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003837 */
Rik van Rield59dc7b2016-09-08 21:30:53 -04003838 if (!pte_write(pte))
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003839 flags |= TNF_NO_GROUP;
3840
Rik van Rieldabe1d92013-10-07 11:29:34 +01003841 /*
3842 * Flag if the page is shared between multiple address spaces. This
3843 * is later used when determining whether to group tasks together
3844 */
3845 if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED))
3846 flags |= TNF_SHARED;
3847
Peter Zijlstra90572892013-10-07 11:29:20 +01003848 last_cpupid = page_cpupid_last(page);
Mel Gorman8191acb2013-10-07 11:28:45 +01003849 page_nid = page_to_nid(page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003850 target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003851 &flags);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003852 pte_unmap_unlock(vmf->pte, vmf->ptl);
Mel Gorman4daae3b2012-11-02 11:33:45 +00003853 if (target_nid == -1) {
Mel Gorman4daae3b2012-11-02 11:33:45 +00003854 put_page(page);
3855 goto out;
3856 }
3857
3858 /* Migrate to the requested node */
Mel Gorman1bc115d2013-10-07 11:29:05 +01003859 migrated = migrate_misplaced_page(page, vma, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003860 if (migrated) {
Mel Gorman8191acb2013-10-07 11:28:45 +01003861 page_nid = target_nid;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003862 flags |= TNF_MIGRATED;
Mel Gorman074c2382015-03-25 15:55:42 -07003863 } else
3864 flags |= TNF_MIGRATE_FAIL;
Mel Gorman4daae3b2012-11-02 11:33:45 +00003865
3866out:
Mel Gorman8191acb2013-10-07 11:28:45 +01003867 if (page_nid != -1)
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003868 task_numa_fault(last_cpupid, page_nid, 1, flags);
Mel Gormand10e63f2012-10-25 14:16:31 +02003869 return 0;
3870}
3871
Geert Uytterhoeven91a90142017-07-12 14:33:08 -07003872static inline int create_huge_pmd(struct vm_fault *vmf)
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07003873{
Dave Jiangf4200392017-02-22 15:40:06 -08003874 if (vma_is_anonymous(vmf->vma))
Jan Kara82b0f8c2016-12-14 15:06:58 -08003875 return do_huge_pmd_anonymous_page(vmf);
Dave Jianga2d58162017-02-24 14:56:59 -08003876 if (vmf->vma->vm_ops->huge_fault)
Dave Jiangc791ace2017-02-24 14:57:08 -08003877 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07003878 return VM_FAULT_FALLBACK;
3879}
3880
Jan Kara82b0f8c2016-12-14 15:06:58 -08003881static int wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd)
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07003882{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003883 if (vma_is_anonymous(vmf->vma))
3884 return do_huge_pmd_wp_page(vmf, orig_pmd);
Dave Jianga2d58162017-02-24 14:56:59 -08003885 if (vmf->vma->vm_ops->huge_fault)
Dave Jiangc791ace2017-02-24 14:57:08 -08003886 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);
Kirill A. Shutemovaf9e4d52016-07-26 15:25:40 -07003887
3888 /* COW handled on pte level: split pmd */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003889 VM_BUG_ON_VMA(vmf->vma->vm_flags & VM_SHARED, vmf->vma);
3890 __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL);
Kirill A. Shutemovaf9e4d52016-07-26 15:25:40 -07003891
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07003892 return VM_FAULT_FALLBACK;
3893}
3894
Lorenzo Stoakes38e08852016-09-11 23:54:25 +01003895static inline bool vma_is_accessible(struct vm_area_struct *vma)
3896{
3897 return vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE);
3898}
3899
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08003900static int create_huge_pud(struct vm_fault *vmf)
3901{
3902#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3903 /* No support for anonymous transparent PUD pages yet */
3904 if (vma_is_anonymous(vmf->vma))
3905 return VM_FAULT_FALLBACK;
3906 if (vmf->vma->vm_ops->huge_fault)
Dave Jiangc791ace2017-02-24 14:57:08 -08003907 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08003908#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
3909 return VM_FAULT_FALLBACK;
3910}
3911
3912static int wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud)
3913{
3914#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3915 /* No support for anonymous transparent PUD pages yet */
3916 if (vma_is_anonymous(vmf->vma))
3917 return VM_FAULT_FALLBACK;
3918 if (vmf->vma->vm_ops->huge_fault)
Dave Jiangc791ace2017-02-24 14:57:08 -08003919 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08003920#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
3921 return VM_FAULT_FALLBACK;
3922}
3923
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924/*
3925 * These routines also need to handle stuff like marking pages dirty
3926 * and/or accessed for architectures that don't do it in hardware (most
3927 * RISC architectures). The early dirtying is also good on the i386.
3928 *
3929 * There is also a hook called "update_mmu_cache()" that architectures
3930 * with external mmu caches can use to update those (ie the Sparc or
3931 * PowerPC hashed page tables that act as extended TLBs).
3932 *
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003933 * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow
3934 * concurrent faults).
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003935 *
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003936 * The mmap_sem may have been released depending on flags and our return value.
3937 * See filemap_fault() and __lock_page_or_retry().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003939static int handle_pte_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940{
3941 pte_t entry;
3942
Jan Kara82b0f8c2016-12-14 15:06:58 -08003943 if (unlikely(pmd_none(*vmf->pmd))) {
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003944 /*
3945 * Leave __pte_alloc() until later: because vm_ops->fault may
3946 * want to allocate huge page, and if we expose page table
3947 * for an instant, it will be difficult to retract from
3948 * concurrent faults and from rmap lookups.
3949 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003950 vmf->pte = NULL;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003951 } else {
3952 /* See comment in pte_alloc_one_map() */
Ross Zwislerd0f0931d2017-06-02 14:46:34 -07003953 if (pmd_devmap_trans_unstable(vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003954 return 0;
3955 /*
3956 * A regular pmd is established and it can't morph into a huge
3957 * pmd from under us anymore at this point because we hold the
3958 * mmap_sem read mode and khugepaged takes it in write mode.
3959 * So now it's safe to run pte_offset_map().
3960 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003961 vmf->pte = pte_offset_map(vmf->pmd, vmf->address);
Jan Kara29943022016-12-14 15:07:16 -08003962 vmf->orig_pte = *vmf->pte;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003963
3964 /*
3965 * some architectures can have larger ptes than wordsize,
3966 * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and
3967 * CONFIG_32BIT=y, so READ_ONCE or ACCESS_ONCE cannot guarantee
3968 * atomic accesses. The code below just needs a consistent
3969 * view for the ifs and we later double check anyway with the
3970 * ptl lock held. So here a barrier will do.
3971 */
3972 barrier();
Jan Kara29943022016-12-14 15:07:16 -08003973 if (pte_none(vmf->orig_pte)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003974 pte_unmap(vmf->pte);
3975 vmf->pte = NULL;
Hugh Dickins65500d22005-10-29 18:15:59 -07003976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 }
3978
Jan Kara82b0f8c2016-12-14 15:06:58 -08003979 if (!vmf->pte) {
3980 if (vma_is_anonymous(vmf->vma))
3981 return do_anonymous_page(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003982 else
Jan Kara82b0f8c2016-12-14 15:06:58 -08003983 return do_fault(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003984 }
3985
Jan Kara29943022016-12-14 15:07:16 -08003986 if (!pte_present(vmf->orig_pte))
3987 return do_swap_page(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003988
Jan Kara29943022016-12-14 15:07:16 -08003989 if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma))
3990 return do_numa_page(vmf);
Mel Gormand10e63f2012-10-25 14:16:31 +02003991
Jan Kara82b0f8c2016-12-14 15:06:58 -08003992 vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
3993 spin_lock(vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08003994 entry = vmf->orig_pte;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003995 if (unlikely(!pte_same(*vmf->pte, entry)))
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003996 goto unlock;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003997 if (vmf->flags & FAULT_FLAG_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 if (!pte_write(entry))
Jan Kara29943022016-12-14 15:07:16 -08003999 return do_wp_page(vmf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 entry = pte_mkdirty(entry);
4001 }
4002 entry = pte_mkyoung(entry);
Jan Kara82b0f8c2016-12-14 15:06:58 -08004003 if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry,
4004 vmf->flags & FAULT_FLAG_WRITE)) {
4005 update_mmu_cache(vmf->vma, vmf->address, vmf->pte);
Andrea Arcangeli1a44e142005-10-29 18:16:48 -07004006 } else {
4007 /*
4008 * This is needed only for protection faults but the arch code
4009 * is not yet telling us if this is a protection fault or not.
4010 * This still avoids useless tlb flushes for .text page faults
4011 * with threads.
4012 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08004013 if (vmf->flags & FAULT_FLAG_WRITE)
4014 flush_tlb_fix_spurious_fault(vmf->vma, vmf->address);
Andrea Arcangeli1a44e142005-10-29 18:16:48 -07004015 }
Hugh Dickins8f4e2102005-10-29 18:16:26 -07004016unlock:
Jan Kara82b0f8c2016-12-14 15:06:58 -08004017 pte_unmap_unlock(vmf->pte, vmf->ptl);
Nick Piggin83c54072007-07-19 01:47:05 -07004018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019}
4020
4021/*
4022 * By the time we get here, we already hold the mm semaphore
Paul Cassella9a95f3c2014-08-06 16:07:24 -07004023 *
4024 * The mmap_sem may have been released depending on flags and our
4025 * return value. See filemap_fault() and __lock_page_or_retry().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 */
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -07004027static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
4028 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029{
Jan Kara82b0f8c2016-12-14 15:06:58 -08004030 struct vm_fault vmf = {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07004031 .vma = vma,
Jan Kara1a29d852016-12-14 15:07:01 -08004032 .address = address & PAGE_MASK,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07004033 .flags = flags,
Jan Kara0721ec82016-12-14 15:07:04 -08004034 .pgoff = linear_page_index(vma, address),
Jan Kara667240e2016-12-14 15:07:07 -08004035 .gfp_mask = __get_fault_gfp_mask(vma),
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07004036 };
Anshuman Khandualfde26be2017-09-08 16:12:45 -07004037 unsigned int dirty = flags & FAULT_FLAG_WRITE;
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -07004038 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004040 p4d_t *p4d;
Dave Jianga2d58162017-02-24 14:56:59 -08004041 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 pgd = pgd_offset(mm, address);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004044 p4d = p4d_alloc(mm, pgd, address);
4045 if (!p4d)
4046 return VM_FAULT_OOM;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004047
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004048 vmf.pud = pud_alloc(mm, p4d, address);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004049 if (!vmf.pud)
Hugh Dickinsc74df322005-10-29 18:16:23 -07004050 return VM_FAULT_OOM;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004051 if (pud_none(*vmf.pud) && transparent_hugepage_enabled(vma)) {
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004052 ret = create_huge_pud(&vmf);
4053 if (!(ret & VM_FAULT_FALLBACK))
4054 return ret;
4055 } else {
4056 pud_t orig_pud = *vmf.pud;
4057
4058 barrier();
4059 if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) {
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004060
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004061 /* NUMA case for anonymous PUDs would go here */
4062
4063 if (dirty && !pud_write(orig_pud)) {
4064 ret = wp_huge_pud(&vmf, orig_pud);
4065 if (!(ret & VM_FAULT_FALLBACK))
4066 return ret;
4067 } else {
4068 huge_pud_set_accessed(&vmf, orig_pud);
4069 return 0;
4070 }
4071 }
4072 }
4073
4074 vmf.pmd = pmd_alloc(mm, vmf.pud, address);
Jan Kara82b0f8c2016-12-14 15:06:58 -08004075 if (!vmf.pmd)
Hugh Dickinsc74df322005-10-29 18:16:23 -07004076 return VM_FAULT_OOM;
Jan Kara82b0f8c2016-12-14 15:06:58 -08004077 if (pmd_none(*vmf.pmd) && transparent_hugepage_enabled(vma)) {
Dave Jianga2d58162017-02-24 14:56:59 -08004078 ret = create_huge_pmd(&vmf);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -07004079 if (!(ret & VM_FAULT_FALLBACK))
4080 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08004081 } else {
Jan Kara82b0f8c2016-12-14 15:06:58 -08004082 pmd_t orig_pmd = *vmf.pmd;
David Rientjes1f1d06c2012-05-29 15:06:23 -07004083
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08004084 barrier();
Zi Yan84c3fc42017-09-08 16:11:01 -07004085 if (unlikely(is_swap_pmd(orig_pmd))) {
4086 VM_BUG_ON(thp_migration_supported() &&
4087 !is_pmd_migration_entry(orig_pmd));
4088 if (is_pmd_migration_entry(orig_pmd))
4089 pmd_migration_entry_wait(mm, vmf.pmd);
4090 return 0;
4091 }
Dan Williams5c7fb562016-01-15 16:56:52 -08004092 if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) {
Lorenzo Stoakes38e08852016-09-11 23:54:25 +01004093 if (pmd_protnone(orig_pmd) && vma_is_accessible(vma))
Jan Kara82b0f8c2016-12-14 15:06:58 -08004094 return do_huge_pmd_numa_page(&vmf, orig_pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02004095
Anshuman Khandualfde26be2017-09-08 16:12:45 -07004096 if (dirty && !pmd_write(orig_pmd)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08004097 ret = wp_huge_pmd(&vmf, orig_pmd);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08004098 if (!(ret & VM_FAULT_FALLBACK))
4099 return ret;
Will Deacona1dd4502012-12-11 16:01:27 -08004100 } else {
Jan Kara82b0f8c2016-12-14 15:06:58 -08004101 huge_pmd_set_accessed(&vmf, orig_pmd);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08004102 return 0;
David Rientjes1f1d06c2012-05-29 15:06:23 -07004103 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08004104 }
4105 }
4106
Jan Kara82b0f8c2016-12-14 15:06:58 -08004107 return handle_pte_fault(&vmf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108}
4109
Paul Cassella9a95f3c2014-08-06 16:07:24 -07004110/*
4111 * By the time we get here, we already hold the mm semaphore
4112 *
4113 * The mmap_sem may have been released depending on flags and our
4114 * return value. See filemap_fault() and __lock_page_or_retry().
4115 */
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -07004116int handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
4117 unsigned int flags)
Johannes Weiner519e5242013-09-12 15:13:42 -07004118{
4119 int ret;
4120
4121 __set_current_state(TASK_RUNNING);
4122
4123 count_vm_event(PGFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07004124 count_memcg_event_mm(vma->vm_mm, PGFAULT);
Johannes Weiner519e5242013-09-12 15:13:42 -07004125
4126 /* do counter updates before entering really critical section. */
4127 check_sync_rss_stat(current);
4128
Laurent Dufourde0c7992017-09-08 16:13:12 -07004129 if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
4130 flags & FAULT_FLAG_INSTRUCTION,
4131 flags & FAULT_FLAG_REMOTE))
4132 return VM_FAULT_SIGSEGV;
4133
Johannes Weiner519e5242013-09-12 15:13:42 -07004134 /*
4135 * Enable the memcg OOM handling for faults triggered in user
4136 * space. Kernel faults are handled more gracefully.
4137 */
4138 if (flags & FAULT_FLAG_USER)
Johannes Weiner49426422013-10-16 13:46:59 -07004139 mem_cgroup_oom_enable();
Johannes Weiner519e5242013-09-12 15:13:42 -07004140
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07004141 if (unlikely(is_vm_hugetlb_page(vma)))
4142 ret = hugetlb_fault(vma->vm_mm, vma, address, flags);
4143 else
4144 ret = __handle_mm_fault(vma, address, flags);
Johannes Weiner519e5242013-09-12 15:13:42 -07004145
Johannes Weiner49426422013-10-16 13:46:59 -07004146 if (flags & FAULT_FLAG_USER) {
4147 mem_cgroup_oom_disable();
Tobin C Harding166f61b2017-02-24 14:59:01 -08004148 /*
4149 * The task may have entered a memcg OOM situation but
4150 * if the allocation error was handled gracefully (no
4151 * VM_FAULT_OOM), there is no need to kill anything.
4152 * Just clean up the OOM state peacefully.
4153 */
4154 if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
4155 mem_cgroup_oom_synchronize(false);
Johannes Weiner49426422013-10-16 13:46:59 -07004156 }
Johannes Weiner3812c8c2013-09-12 15:13:44 -07004157
Johannes Weiner519e5242013-09-12 15:13:42 -07004158 return ret;
4159}
Jesse Barnese1d6d012014-12-12 16:55:27 -08004160EXPORT_SYMBOL_GPL(handle_mm_fault);
Johannes Weiner519e5242013-09-12 15:13:42 -07004161
Kirill A. Shutemov90eceff2017-03-09 17:24:08 +03004162#ifndef __PAGETABLE_P4D_FOLDED
4163/*
4164 * Allocate p4d page table.
4165 * We've already handled the fast-path in-line.
4166 */
4167int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
4168{
4169 p4d_t *new = p4d_alloc_one(mm, address);
4170 if (!new)
4171 return -ENOMEM;
4172
4173 smp_wmb(); /* See comment in __pte_alloc */
4174
4175 spin_lock(&mm->page_table_lock);
4176 if (pgd_present(*pgd)) /* Another has populated it */
4177 p4d_free(mm, new);
4178 else
4179 pgd_populate(mm, pgd, new);
4180 spin_unlock(&mm->page_table_lock);
4181 return 0;
4182}
4183#endif /* __PAGETABLE_P4D_FOLDED */
4184
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185#ifndef __PAGETABLE_PUD_FOLDED
4186/*
4187 * Allocate page upper directory.
Hugh Dickins872fec12005-10-29 18:16:21 -07004188 * We've already handled the fast-path in-line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 */
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004190int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191{
Hugh Dickinsc74df322005-10-29 18:16:23 -07004192 pud_t *new = pud_alloc_one(mm, address);
4193 if (!new)
Hugh Dickins1bb36302005-10-29 18:16:22 -07004194 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
Nick Piggin362a61a2008-05-14 06:37:36 +02004196 smp_wmb(); /* See comment in __pte_alloc */
4197
Hugh Dickins872fec12005-10-29 18:16:21 -07004198 spin_lock(&mm->page_table_lock);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004199#ifndef __ARCH_HAS_5LEVEL_HACK
4200 if (p4d_present(*p4d)) /* Another has populated it */
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -08004201 pud_free(mm, new);
Hugh Dickins1bb36302005-10-29 18:16:22 -07004202 else
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004203 p4d_populate(mm, p4d, new);
4204#else
4205 if (pgd_present(*p4d)) /* Another has populated it */
4206 pud_free(mm, new);
4207 else
4208 pgd_populate(mm, p4d, new);
4209#endif /* __ARCH_HAS_5LEVEL_HACK */
Hugh Dickinsc74df322005-10-29 18:16:23 -07004210 spin_unlock(&mm->page_table_lock);
Hugh Dickins1bb36302005-10-29 18:16:22 -07004211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212}
4213#endif /* __PAGETABLE_PUD_FOLDED */
4214
4215#ifndef __PAGETABLE_PMD_FOLDED
4216/*
4217 * Allocate page middle directory.
Hugh Dickins872fec12005-10-29 18:16:21 -07004218 * We've already handled the fast-path in-line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 */
Hugh Dickins1bb36302005-10-29 18:16:22 -07004220int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221{
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004222 spinlock_t *ptl;
Hugh Dickinsc74df322005-10-29 18:16:23 -07004223 pmd_t *new = pmd_alloc_one(mm, address);
4224 if (!new)
Hugh Dickins1bb36302005-10-29 18:16:22 -07004225 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226
Nick Piggin362a61a2008-05-14 06:37:36 +02004227 smp_wmb(); /* See comment in __pte_alloc */
4228
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004229 ptl = pud_lock(mm, pud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230#ifndef __ARCH_HAS_4LEVEL_HACK
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004231 if (!pud_present(*pud)) {
4232 mm_inc_nr_pmds(mm);
Hugh Dickins1bb36302005-10-29 18:16:22 -07004233 pud_populate(mm, pud, new);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004234 } else /* Another has populated it */
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -08004235 pmd_free(mm, new);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004236#else
4237 if (!pgd_present(*pud)) {
4238 mm_inc_nr_pmds(mm);
Hugh Dickins1bb36302005-10-29 18:16:22 -07004239 pgd_populate(mm, pud, new);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004240 } else /* Another has populated it */
4241 pmd_free(mm, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242#endif /* __ARCH_HAS_4LEVEL_HACK */
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004243 spin_unlock(ptl);
Hugh Dickins1bb36302005-10-29 18:16:22 -07004244 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245}
4246#endif /* __PAGETABLE_PMD_FOLDED */
4247
Ross Zwisler09796392017-01-10 16:57:21 -08004248static int __follow_pte_pmd(struct mm_struct *mm, unsigned long address,
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004249 unsigned long *start, unsigned long *end,
4250 pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp)
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004251{
4252 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004253 p4d_t *p4d;
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004254 pud_t *pud;
4255 pmd_t *pmd;
4256 pte_t *ptep;
4257
4258 pgd = pgd_offset(mm, address);
4259 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
4260 goto out;
4261
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004262 p4d = p4d_offset(pgd, address);
4263 if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d)))
4264 goto out;
4265
4266 pud = pud_offset(p4d, address);
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004267 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
4268 goto out;
4269
4270 pmd = pmd_offset(pud, address);
Andrea Arcangelif66055ab2011-01-13 15:46:54 -08004271 VM_BUG_ON(pmd_trans_huge(*pmd));
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004272
Ross Zwisler09796392017-01-10 16:57:21 -08004273 if (pmd_huge(*pmd)) {
4274 if (!pmdpp)
4275 goto out;
4276
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004277 if (start && end) {
4278 *start = address & PMD_MASK;
4279 *end = *start + PMD_SIZE;
4280 mmu_notifier_invalidate_range_start(mm, *start, *end);
4281 }
Ross Zwisler09796392017-01-10 16:57:21 -08004282 *ptlp = pmd_lock(mm, pmd);
4283 if (pmd_huge(*pmd)) {
4284 *pmdpp = pmd;
4285 return 0;
4286 }
4287 spin_unlock(*ptlp);
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004288 if (start && end)
4289 mmu_notifier_invalidate_range_end(mm, *start, *end);
Ross Zwisler09796392017-01-10 16:57:21 -08004290 }
4291
4292 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004293 goto out;
4294
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004295 if (start && end) {
4296 *start = address & PAGE_MASK;
4297 *end = *start + PAGE_SIZE;
4298 mmu_notifier_invalidate_range_start(mm, *start, *end);
4299 }
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004300 ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004301 if (!pte_present(*ptep))
4302 goto unlock;
4303 *ptepp = ptep;
4304 return 0;
4305unlock:
4306 pte_unmap_unlock(ptep, *ptlp);
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004307 if (start && end)
4308 mmu_notifier_invalidate_range_end(mm, *start, *end);
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004309out:
4310 return -EINVAL;
4311}
4312
Ross Zwislerf729c8c2017-01-10 16:57:24 -08004313static inline int follow_pte(struct mm_struct *mm, unsigned long address,
4314 pte_t **ptepp, spinlock_t **ptlp)
Namhyung Kim1b36ba82010-10-26 14:22:00 -07004315{
4316 int res;
4317
4318 /* (void) is needed to make gcc happy */
4319 (void) __cond_lock(*ptlp,
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004320 !(res = __follow_pte_pmd(mm, address, NULL, NULL,
4321 ptepp, NULL, ptlp)));
Namhyung Kim1b36ba82010-10-26 14:22:00 -07004322 return res;
4323}
4324
Ross Zwisler09796392017-01-10 16:57:21 -08004325int follow_pte_pmd(struct mm_struct *mm, unsigned long address,
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004326 unsigned long *start, unsigned long *end,
Ross Zwisler09796392017-01-10 16:57:21 -08004327 pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp)
4328{
4329 int res;
4330
4331 /* (void) is needed to make gcc happy */
4332 (void) __cond_lock(*ptlp,
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004333 !(res = __follow_pte_pmd(mm, address, start, end,
4334 ptepp, pmdpp, ptlp)));
Ross Zwisler09796392017-01-10 16:57:21 -08004335 return res;
4336}
4337EXPORT_SYMBOL(follow_pte_pmd);
4338
Johannes Weiner3b6748e2009-06-16 15:32:35 -07004339/**
4340 * follow_pfn - look up PFN at a user virtual address
4341 * @vma: memory mapping
4342 * @address: user virtual address
4343 * @pfn: location to store found PFN
4344 *
4345 * Only IO mappings and raw PFN mappings are allowed.
4346 *
4347 * Returns zero and the pfn at @pfn on success, -ve otherwise.
4348 */
4349int follow_pfn(struct vm_area_struct *vma, unsigned long address,
4350 unsigned long *pfn)
4351{
4352 int ret = -EINVAL;
4353 spinlock_t *ptl;
4354 pte_t *ptep;
4355
4356 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
4357 return ret;
4358
4359 ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
4360 if (ret)
4361 return ret;
4362 *pfn = pte_pfn(*ptep);
4363 pte_unmap_unlock(ptep, ptl);
4364 return 0;
4365}
4366EXPORT_SYMBOL(follow_pfn);
4367
Rik van Riel28b2ee22008-07-23 21:27:05 -07004368#ifdef CONFIG_HAVE_IOREMAP_PROT
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004369int follow_phys(struct vm_area_struct *vma,
4370 unsigned long address, unsigned int flags,
4371 unsigned long *prot, resource_size_t *phys)
Rik van Riel28b2ee22008-07-23 21:27:05 -07004372{
Johannes Weiner03668a42009-06-16 15:32:34 -07004373 int ret = -EINVAL;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004374 pte_t *ptep, pte;
4375 spinlock_t *ptl;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004376
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004377 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
4378 goto out;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004379
Johannes Weiner03668a42009-06-16 15:32:34 -07004380 if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004381 goto out;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004382 pte = *ptep;
Johannes Weiner03668a42009-06-16 15:32:34 -07004383
Rik van Riel28b2ee22008-07-23 21:27:05 -07004384 if ((flags & FOLL_WRITE) && !pte_write(pte))
4385 goto unlock;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004386
4387 *prot = pgprot_val(pte_pgprot(pte));
Johannes Weiner03668a42009-06-16 15:32:34 -07004388 *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004389
Johannes Weiner03668a42009-06-16 15:32:34 -07004390 ret = 0;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004391unlock:
4392 pte_unmap_unlock(ptep, ptl);
4393out:
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004394 return ret;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004395}
4396
4397int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
4398 void *buf, int len, int write)
4399{
4400 resource_size_t phys_addr;
4401 unsigned long prot = 0;
KOSAKI Motohiro2bc72732009-01-06 14:39:43 -08004402 void __iomem *maddr;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004403 int offset = addr & (PAGE_SIZE-1);
4404
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004405 if (follow_phys(vma, addr, write, &prot, &phys_addr))
Rik van Riel28b2ee22008-07-23 21:27:05 -07004406 return -EINVAL;
4407
Grazvydas Ignotas9cb12d72015-02-12 15:00:19 -08004408 maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot);
jie@chenjie6@huwei.comcf7ab2a2018-08-10 17:23:06 -07004409 if (!maddr)
4410 return -ENOMEM;
4411
Rik van Riel28b2ee22008-07-23 21:27:05 -07004412 if (write)
4413 memcpy_toio(maddr + offset, buf, len);
4414 else
4415 memcpy_fromio(buf, maddr + offset, len);
4416 iounmap(maddr);
4417
4418 return len;
4419}
Uwe Kleine-König5a736332013-08-07 13:02:52 +02004420EXPORT_SYMBOL_GPL(generic_access_phys);
Rik van Riel28b2ee22008-07-23 21:27:05 -07004421#endif
4422
David Howells0ec76a12006-09-27 01:50:15 -07004423/*
Stephen Wilson206cb632011-03-13 15:49:19 -04004424 * Access another process' address space as given in mm. If non-NULL, use the
4425 * given task for page fault accounting.
David Howells0ec76a12006-09-27 01:50:15 -07004426 */
Eric W. Biederman84d77d32016-11-22 12:06:50 -06004427int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
Lorenzo Stoakes442486e2016-10-13 01:20:18 +01004428 unsigned long addr, void *buf, int len, unsigned int gup_flags)
David Howells0ec76a12006-09-27 01:50:15 -07004429{
David Howells0ec76a12006-09-27 01:50:15 -07004430 struct vm_area_struct *vma;
David Howells0ec76a12006-09-27 01:50:15 -07004431 void *old_buf = buf;
Lorenzo Stoakes442486e2016-10-13 01:20:18 +01004432 int write = gup_flags & FOLL_WRITE;
David Howells0ec76a12006-09-27 01:50:15 -07004433
David Howells0ec76a12006-09-27 01:50:15 -07004434 down_read(&mm->mmap_sem);
Simon Arlott183ff222007-10-20 01:27:18 +02004435 /* ignore errors, just check how much was successfully transferred */
David Howells0ec76a12006-09-27 01:50:15 -07004436 while (len) {
4437 int bytes, ret, offset;
4438 void *maddr;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004439 struct page *page = NULL;
David Howells0ec76a12006-09-27 01:50:15 -07004440
Dave Hansen1e987792016-02-12 13:01:54 -08004441 ret = get_user_pages_remote(tsk, mm, addr, 1,
Lorenzo Stoakes5b56d492016-12-14 15:06:52 -08004442 gup_flags, &page, &vma, NULL);
Rik van Riel28b2ee22008-07-23 21:27:05 -07004443 if (ret <= 0) {
Rik van Rieldbffcd02014-08-06 16:08:12 -07004444#ifndef CONFIG_HAVE_IOREMAP_PROT
4445 break;
4446#else
Rik van Riel28b2ee22008-07-23 21:27:05 -07004447 /*
4448 * Check if this is a VM_IO | VM_PFNMAP VMA, which
4449 * we can access using slightly different code.
4450 */
Rik van Riel28b2ee22008-07-23 21:27:05 -07004451 vma = find_vma(mm, addr);
Michael Ellermanfe936df2011-04-14 15:22:10 -07004452 if (!vma || vma->vm_start > addr)
Rik van Riel28b2ee22008-07-23 21:27:05 -07004453 break;
4454 if (vma->vm_ops && vma->vm_ops->access)
4455 ret = vma->vm_ops->access(vma, addr, buf,
4456 len, write);
4457 if (ret <= 0)
Rik van Riel28b2ee22008-07-23 21:27:05 -07004458 break;
4459 bytes = ret;
Rik van Rieldbffcd02014-08-06 16:08:12 -07004460#endif
David Howells0ec76a12006-09-27 01:50:15 -07004461 } else {
Rik van Riel28b2ee22008-07-23 21:27:05 -07004462 bytes = len;
4463 offset = addr & (PAGE_SIZE-1);
4464 if (bytes > PAGE_SIZE-offset)
4465 bytes = PAGE_SIZE-offset;
4466
4467 maddr = kmap(page);
4468 if (write) {
4469 copy_to_user_page(vma, page, addr,
4470 maddr + offset, buf, bytes);
4471 set_page_dirty_lock(page);
4472 } else {
4473 copy_from_user_page(vma, page, addr,
4474 buf, maddr + offset, bytes);
4475 }
4476 kunmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004477 put_page(page);
David Howells0ec76a12006-09-27 01:50:15 -07004478 }
David Howells0ec76a12006-09-27 01:50:15 -07004479 len -= bytes;
4480 buf += bytes;
4481 addr += bytes;
4482 }
4483 up_read(&mm->mmap_sem);
David Howells0ec76a12006-09-27 01:50:15 -07004484
4485 return buf - old_buf;
4486}
Andi Kleen03252912008-01-30 13:33:18 +01004487
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04004488/**
Randy Dunlapae91dbf2011-03-26 13:27:01 -07004489 * access_remote_vm - access another process' address space
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04004490 * @mm: the mm_struct of the target address space
4491 * @addr: start address to access
4492 * @buf: source or destination buffer
4493 * @len: number of bytes to transfer
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01004494 * @gup_flags: flags modifying lookup behaviour
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04004495 *
4496 * The caller must hold a reference on @mm.
4497 */
4498int access_remote_vm(struct mm_struct *mm, unsigned long addr,
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01004499 void *buf, int len, unsigned int gup_flags)
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04004500{
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01004501 return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags);
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04004502}
4503
Andi Kleen03252912008-01-30 13:33:18 +01004504/*
Stephen Wilson206cb632011-03-13 15:49:19 -04004505 * Access another process' address space.
4506 * Source/target buffer must be kernel space,
4507 * Do not walk the page table directly, use get_user_pages
4508 */
4509int access_process_vm(struct task_struct *tsk, unsigned long addr,
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +01004510 void *buf, int len, unsigned int gup_flags)
Stephen Wilson206cb632011-03-13 15:49:19 -04004511{
4512 struct mm_struct *mm;
4513 int ret;
4514
4515 mm = get_task_mm(tsk);
4516 if (!mm)
4517 return 0;
4518
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +01004519 ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags);
Lorenzo Stoakes442486e2016-10-13 01:20:18 +01004520
Stephen Wilson206cb632011-03-13 15:49:19 -04004521 mmput(mm);
4522
4523 return ret;
4524}
Catalin Marinasfcd35852016-11-01 14:43:25 -07004525EXPORT_SYMBOL_GPL(access_process_vm);
Stephen Wilson206cb632011-03-13 15:49:19 -04004526
Andi Kleen03252912008-01-30 13:33:18 +01004527/*
4528 * Print the name of a VMA.
4529 */
4530void print_vma_addr(char *prefix, unsigned long ip)
4531{
4532 struct mm_struct *mm = current->mm;
4533 struct vm_area_struct *vma;
4534
Ingo Molnare8bff742008-02-13 20:21:06 +01004535 /*
4536 * Do not print if we are in atomic
4537 * contexts (in exception stacks, etc.):
4538 */
4539 if (preempt_count())
4540 return;
4541
Andi Kleen03252912008-01-30 13:33:18 +01004542 down_read(&mm->mmap_sem);
4543 vma = find_vma(mm, ip);
4544 if (vma && vma->vm_file) {
4545 struct file *f = vma->vm_file;
4546 char *buf = (char *)__get_free_page(GFP_KERNEL);
4547 if (buf) {
Andy Shevchenko2fbc57c2012-12-17 16:01:23 -08004548 char *p;
Andi Kleen03252912008-01-30 13:33:18 +01004549
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02004550 p = file_path(f, buf, PAGE_SIZE);
Andi Kleen03252912008-01-30 13:33:18 +01004551 if (IS_ERR(p))
4552 p = "?";
Andy Shevchenko2fbc57c2012-12-17 16:01:23 -08004553 printk("%s%s[%lx+%lx]", prefix, kbasename(p),
Andi Kleen03252912008-01-30 13:33:18 +01004554 vma->vm_start,
4555 vma->vm_end - vma->vm_start);
4556 free_page((unsigned long)buf);
4557 }
4558 }
Jeff Liu51a07e52012-07-31 16:43:18 -07004559 up_read(&mm->mmap_sem);
Andi Kleen03252912008-01-30 13:33:18 +01004560}
Nick Piggin3ee1afa2008-09-10 13:37:17 +02004561
Michael S. Tsirkin662bbcb2013-05-26 17:32:23 +03004562#if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
David Hildenbrand9ec23532015-05-11 17:52:07 +02004563void __might_fault(const char *file, int line)
Nick Piggin3ee1afa2008-09-10 13:37:17 +02004564{
Peter Zijlstra95156f02009-01-12 13:02:11 +01004565 /*
4566 * Some code (nfs/sunrpc) uses socket ops on kernel memory while
4567 * holding the mmap_sem, this is safe because kernel memory doesn't
4568 * get paged out, therefore we'll never actually fault, and the
4569 * below annotations will generate false positives.
4570 */
Al Virodb68ce12017-03-20 21:08:07 -04004571 if (uaccess_kernel())
Peter Zijlstra95156f02009-01-12 13:02:11 +01004572 return;
David Hildenbrand9ec23532015-05-11 17:52:07 +02004573 if (pagefault_disabled())
Michael S. Tsirkin662bbcb2013-05-26 17:32:23 +03004574 return;
David Hildenbrand9ec23532015-05-11 17:52:07 +02004575 __might_sleep(file, line, 0);
4576#if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
Michael S. Tsirkin662bbcb2013-05-26 17:32:23 +03004577 if (current->mm)
Nick Piggin3ee1afa2008-09-10 13:37:17 +02004578 might_lock_read(&current->mm->mmap_sem);
David Hildenbrand9ec23532015-05-11 17:52:07 +02004579#endif
Nick Piggin3ee1afa2008-09-10 13:37:17 +02004580}
David Hildenbrand9ec23532015-05-11 17:52:07 +02004581EXPORT_SYMBOL(__might_fault);
Nick Piggin3ee1afa2008-09-10 13:37:17 +02004582#endif
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004583
4584#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
4585static void clear_gigantic_page(struct page *page,
4586 unsigned long addr,
4587 unsigned int pages_per_huge_page)
4588{
4589 int i;
4590 struct page *p = page;
4591
4592 might_sleep();
4593 for (i = 0; i < pages_per_huge_page;
4594 i++, p = mem_map_next(p, page, i)) {
4595 cond_resched();
4596 clear_user_highpage(p, addr + i * PAGE_SIZE);
4597 }
4598}
4599void clear_huge_page(struct page *page,
Huang Yingc79b57e2017-09-06 16:25:04 -07004600 unsigned long addr_hint, unsigned int pages_per_huge_page)
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004601{
Huang Yingc79b57e2017-09-06 16:25:04 -07004602 int i, n, base, l;
4603 unsigned long addr = addr_hint &
4604 ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004605
4606 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
4607 clear_gigantic_page(page, addr, pages_per_huge_page);
4608 return;
4609 }
4610
Huang Yingc79b57e2017-09-06 16:25:04 -07004611 /* Clear sub-page to access last to keep its cache lines hot */
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004612 might_sleep();
Huang Yingc79b57e2017-09-06 16:25:04 -07004613 n = (addr_hint - addr) / PAGE_SIZE;
4614 if (2 * n <= pages_per_huge_page) {
4615 /* If sub-page to access in first half of huge page */
4616 base = 0;
4617 l = n;
4618 /* Clear sub-pages at the end of huge page */
4619 for (i = pages_per_huge_page - 1; i >= 2 * n; i--) {
4620 cond_resched();
4621 clear_user_highpage(page + i, addr + i * PAGE_SIZE);
4622 }
4623 } else {
4624 /* If sub-page to access in second half of huge page */
4625 base = pages_per_huge_page - 2 * (pages_per_huge_page - n);
4626 l = pages_per_huge_page - n;
4627 /* Clear sub-pages at the begin of huge page */
4628 for (i = 0; i < base; i++) {
4629 cond_resched();
4630 clear_user_highpage(page + i, addr + i * PAGE_SIZE);
4631 }
4632 }
4633 /*
4634 * Clear remaining sub-pages in left-right-left-right pattern
4635 * towards the sub-page to access
4636 */
4637 for (i = 0; i < l; i++) {
4638 int left_idx = base + i;
4639 int right_idx = base + 2 * l - 1 - i;
4640
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004641 cond_resched();
Huang Yingc79b57e2017-09-06 16:25:04 -07004642 clear_user_highpage(page + left_idx,
4643 addr + left_idx * PAGE_SIZE);
4644 cond_resched();
4645 clear_user_highpage(page + right_idx,
4646 addr + right_idx * PAGE_SIZE);
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004647 }
4648}
4649
4650static void copy_user_gigantic_page(struct page *dst, struct page *src,
4651 unsigned long addr,
4652 struct vm_area_struct *vma,
4653 unsigned int pages_per_huge_page)
4654{
4655 int i;
4656 struct page *dst_base = dst;
4657 struct page *src_base = src;
4658
4659 for (i = 0; i < pages_per_huge_page; ) {
4660 cond_resched();
4661 copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
4662
4663 i++;
4664 dst = mem_map_next(dst, dst_base, i);
4665 src = mem_map_next(src, src_base, i);
4666 }
4667}
4668
4669void copy_user_huge_page(struct page *dst, struct page *src,
4670 unsigned long addr, struct vm_area_struct *vma,
4671 unsigned int pages_per_huge_page)
4672{
4673 int i;
4674
4675 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
4676 copy_user_gigantic_page(dst, src, addr, vma,
4677 pages_per_huge_page);
4678 return;
4679 }
4680
4681 might_sleep();
4682 for (i = 0; i < pages_per_huge_page; i++) {
4683 cond_resched();
4684 copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
4685 }
4686}
Mike Kravetzfa4d75c2017-02-22 15:42:49 -08004687
4688long copy_huge_page_from_user(struct page *dst_page,
4689 const void __user *usr_src,
Mike Kravetz810a56b2017-02-22 15:42:58 -08004690 unsigned int pages_per_huge_page,
4691 bool allow_pagefault)
Mike Kravetzfa4d75c2017-02-22 15:42:49 -08004692{
4693 void *src = (void *)usr_src;
4694 void *page_kaddr;
4695 unsigned long i, rc = 0;
4696 unsigned long ret_val = pages_per_huge_page * PAGE_SIZE;
4697
4698 for (i = 0; i < pages_per_huge_page; i++) {
Mike Kravetz810a56b2017-02-22 15:42:58 -08004699 if (allow_pagefault)
4700 page_kaddr = kmap(dst_page + i);
4701 else
4702 page_kaddr = kmap_atomic(dst_page + i);
Mike Kravetzfa4d75c2017-02-22 15:42:49 -08004703 rc = copy_from_user(page_kaddr,
4704 (const void __user *)(src + i * PAGE_SIZE),
4705 PAGE_SIZE);
Mike Kravetz810a56b2017-02-22 15:42:58 -08004706 if (allow_pagefault)
4707 kunmap(dst_page + i);
4708 else
4709 kunmap_atomic(page_kaddr);
Mike Kravetzfa4d75c2017-02-22 15:42:49 -08004710
4711 ret_val -= (PAGE_SIZE - rc);
4712 if (rc)
4713 break;
4714
4715 cond_resched();
4716 }
4717 return ret_val;
4718}
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004719#endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08004720
Olof Johansson40b64ac2013-12-20 14:28:05 -08004721#if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08004722
4723static struct kmem_cache *page_ptl_cachep;
4724
4725void __init ptlock_cache_init(void)
4726{
4727 page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0,
4728 SLAB_PANIC, NULL);
4729}
4730
Peter Zijlstra539edb52013-11-14 14:31:52 -08004731bool ptlock_alloc(struct page *page)
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08004732{
4733 spinlock_t *ptl;
4734
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08004735 ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL);
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08004736 if (!ptl)
4737 return false;
Peter Zijlstra539edb52013-11-14 14:31:52 -08004738 page->ptl = ptl;
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08004739 return true;
4740}
4741
Peter Zijlstra539edb52013-11-14 14:31:52 -08004742void ptlock_free(struct page *page)
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08004743{
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08004744 kmem_cache_free(page_ptl_cachep, page->ptl);
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08004745}
4746#endif