blob: d219df50389fdd8edcef6c27da1a9841f694a2d4 [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/swapops.h>
63#include <linux/elf.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090064#include <linux/gfp.h>
Mel Gorman4daae3b2012-11-02 11:33:45 +000065#include <linux/migrate.h>
Andy Shevchenko2fbc57c2012-12-17 16:01:23 -080066#include <linux/string.h>
Dan Williams0abdd7a2014-01-21 15:48:12 -080067#include <linux/dma-debug.h>
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -070068#include <linux/debugfs.h>
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -070069#include <linux/userfaultfd_k.h>
Jan Karabc2466e2016-05-12 18:29:19 +020070#include <linux/dax.h>
Michal Hocko6b31d592017-08-18 15:16:15 -070071#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Joel Fernandes (Google)4f1a0442019-10-01 13:28:17 -040073#include <trace/events/kmem.h>
74
Alexey Dobriyan6952b612009-09-18 23:55:55 +040075#include <asm/io.h>
Dave Hansen33a709b2016-02-12 13:02:19 -080076#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <asm/pgalloc.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080078#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <asm/tlb.h>
80#include <asm/tlbflush.h>
81#include <asm/pgtable.h>
82
Jan Beulich42b77722008-07-23 21:27:10 -070083#include "internal.h"
84
Arnd Bergmannaf27d942018-02-16 16:25:53 +010085#if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST)
Peter Zijlstra90572892013-10-07 11:29:20 +010086#warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
Peter Zijlstra75980e92013-02-22 16:34:32 -080087#endif
88
Andy Whitcroftd41dee32005-06-23 00:07:54 -070089#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* use the per-pgdat data instead for discontigmem - mbligh */
91unsigned long max_mapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092EXPORT_SYMBOL(max_mapnr);
Tobin C Harding166f61b2017-02-24 14:59:01 -080093
94struct page *mem_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095EXPORT_SYMBOL(mem_map);
96#endif
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
99 * A number of key systems in x86 including ioremap() rely on the assumption
100 * that high_memory defines the upper bound on direct map memory, then end
101 * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
102 * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
103 * and ZONE_HIGHMEM.
104 */
Tobin C Harding166f61b2017-02-24 14:59:01 -0800105void *high_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106EXPORT_SYMBOL(high_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Ingo Molnar32a93232008-02-06 22:39:44 +0100108/*
109 * Randomize the address space (stacks, mmaps, brk, etc.).
110 *
111 * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
112 * as ancient (libc5 based) binaries can segfault. )
113 */
114int randomize_va_space __read_mostly =
115#ifdef CONFIG_COMPAT_BRK
116 1;
117#else
118 2;
119#endif
Andi Kleena62eaf12006-02-16 23:41:58 +0100120
121static int __init disable_randmaps(char *s)
122{
123 randomize_va_space = 0;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800124 return 1;
Andi Kleena62eaf12006-02-16 23:41:58 +0100125}
126__setup("norandmaps", disable_randmaps);
127
Hugh Dickins62eede62009-09-21 17:03:34 -0700128unsigned long zero_pfn __read_mostly;
Ard Biesheuvel0b700682014-09-12 22:17:23 +0200129EXPORT_SYMBOL(zero_pfn);
130
Tobin C Harding166f61b2017-02-24 14:59:01 -0800131unsigned long highest_memmap_pfn __read_mostly;
132
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -0700133/*
134 * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
135 */
136static int __init init_zero_pfn(void)
137{
138 zero_pfn = page_to_pfn(ZERO_PAGE(0));
139 return 0;
140}
141core_initcall(init_zero_pfn);
Andi Kleena62eaf12006-02-16 23:41:58 +0100142
Joel Fernandesb3ddf582019-12-10 10:45:34 -0500143/*
144 * Only trace rss_stat when there is a 512kb cross over.
145 * Smaller changes may be lost unless every small change is
146 * crossing into or returning to a 512kb boundary.
147 */
148#define TRACE_MM_COUNTER_THRESHOLD 128
149
150void mm_trace_rss_stat(struct mm_struct *mm, int member, long count,
151 long value)
Joel Fernandes (Google)4f1a0442019-10-01 13:28:17 -0400152{
Joel Fernandesb3ddf582019-12-10 10:45:34 -0500153 long thresh_mask = ~(TRACE_MM_COUNTER_THRESHOLD - 1);
154
155 /* Threshold roll-over, trace it */
156 if ((count & thresh_mask) != ((count - value) & thresh_mask))
157 trace_rss_stat(mm, member, count);
Joel Fernandes (Google)4f1a0442019-10-01 13:28:17 -0400158}
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800159
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800160#if defined(SPLIT_RSS_COUNTING)
161
David Rientjesea48cf72012-03-21 16:34:13 -0700162void sync_mm_rss(struct mm_struct *mm)
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800163{
164 int i;
165
166 for (i = 0; i < NR_MM_COUNTERS; i++) {
David Rientjes05af2e12012-03-21 16:34:13 -0700167 if (current->rss_stat.count[i]) {
168 add_mm_counter(mm, i, current->rss_stat.count[i]);
169 current->rss_stat.count[i] = 0;
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800170 }
171 }
David Rientjes05af2e12012-03-21 16:34:13 -0700172 current->rss_stat.events = 0;
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800173}
174
175static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
176{
177 struct task_struct *task = current;
178
179 if (likely(task->mm == mm))
180 task->rss_stat.count[member] += val;
181 else
182 add_mm_counter(mm, member, val);
183}
184#define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
185#define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
186
187/* sync counter once per 64 page faults */
188#define TASK_RSS_EVENTS_THRESH (64)
189static void check_sync_rss_stat(struct task_struct *task)
190{
191 if (unlikely(task != current))
192 return;
193 if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
David Rientjesea48cf72012-03-21 16:34:13 -0700194 sync_mm_rss(task->mm);
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800195}
Peter Zijlstra9547d012011-05-24 17:12:14 -0700196#else /* SPLIT_RSS_COUNTING */
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800197
198#define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
199#define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
200
201static void check_sync_rss_stat(struct task_struct *task)
202{
203}
204
Peter Zijlstra9547d012011-05-24 17:12:14 -0700205#endif /* SPLIT_RSS_COUNTING */
206
207#ifdef HAVE_GENERIC_MMU_GATHER
208
Nicholas Krauseca1d6c72015-09-04 15:48:22 -0700209static bool tlb_next_batch(struct mmu_gather *tlb)
Peter Zijlstra9547d012011-05-24 17:12:14 -0700210{
211 struct mmu_gather_batch *batch;
212
213 batch = tlb->active;
214 if (batch->next) {
215 tlb->active = batch->next;
Nicholas Krauseca1d6c72015-09-04 15:48:22 -0700216 return true;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700217 }
218
Michal Hocko53a59fc2013-01-04 15:35:12 -0800219 if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
Nicholas Krauseca1d6c72015-09-04 15:48:22 -0700220 return false;
Michal Hocko53a59fc2013-01-04 15:35:12 -0800221
Peter Zijlstra9547d012011-05-24 17:12:14 -0700222 batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
223 if (!batch)
Nicholas Krauseca1d6c72015-09-04 15:48:22 -0700224 return false;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700225
Michal Hocko53a59fc2013-01-04 15:35:12 -0800226 tlb->batch_count++;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700227 batch->next = NULL;
228 batch->nr = 0;
229 batch->max = MAX_GATHER_BATCH;
230
231 tlb->active->next = batch;
232 tlb->active = batch;
233
Nicholas Krauseca1d6c72015-09-04 15:48:22 -0700234 return true;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700235}
236
Minchan Kim56236a52017-08-10 15:24:05 -0700237void arch_tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
238 unsigned long start, unsigned long end)
Peter Zijlstra9547d012011-05-24 17:12:14 -0700239{
240 tlb->mm = mm;
241
Linus Torvalds2b047252013-08-15 11:42:25 -0700242 /* Is it from 0 to ~0? */
243 tlb->fullmm = !(start | (end+1));
Dave Hansen1de14c32013-04-12 16:23:54 -0700244 tlb->need_flush_all = 0;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700245 tlb->local.next = NULL;
246 tlb->local.nr = 0;
247 tlb->local.max = ARRAY_SIZE(tlb->__pages);
248 tlb->active = &tlb->local;
Michal Hocko53a59fc2013-01-04 15:35:12 -0800249 tlb->batch_count = 0;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700250
251#ifdef CONFIG_HAVE_RCU_TABLE_FREE
252 tlb->batch = NULL;
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800253#endif
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -0700254 tlb->page_size = 0;
Will Deaconfb7332a2014-10-29 10:03:09 +0000255
256 __tlb_reset_range(tlb);
Peter Zijlstra9547d012011-05-24 17:12:14 -0700257}
258
Linus Torvalds1cf35d42014-04-25 16:05:40 -0700259static void tlb_flush_mmu_free(struct mmu_gather *tlb)
260{
261 struct mmu_gather_batch *batch;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700262
Nicholas Piggindb7ddef2018-08-23 18:47:08 +1000263#ifdef CONFIG_HAVE_RCU_TABLE_FREE
264 tlb_table_flush(tlb);
265#endif
Will Deacon721c21c2015-01-12 19:10:55 +0000266 for (batch = &tlb->local; batch && batch->nr; batch = batch->next) {
Peter Zijlstra9547d012011-05-24 17:12:14 -0700267 free_pages_and_swap_cache(batch->pages, batch->nr);
268 batch->nr = 0;
269 }
270 tlb->active = &tlb->local;
271}
272
Linus Torvalds1cf35d42014-04-25 16:05:40 -0700273void tlb_flush_mmu(struct mmu_gather *tlb)
274{
Linus Torvalds1cf35d42014-04-25 16:05:40 -0700275 tlb_flush_mmu_tlbonly(tlb);
276 tlb_flush_mmu_free(tlb);
277}
278
Peter Zijlstra9547d012011-05-24 17:12:14 -0700279/* tlb_finish_mmu
280 * Called at the end of the shootdown operation to free up any resources
281 * that were required.
282 */
Minchan Kim56236a52017-08-10 15:24:05 -0700283void arch_tlb_finish_mmu(struct mmu_gather *tlb,
Minchan Kim99baac22017-08-10 15:24:12 -0700284 unsigned long start, unsigned long end, bool force)
Peter Zijlstra9547d012011-05-24 17:12:14 -0700285{
286 struct mmu_gather_batch *batch, *next;
287
Minchan Kim99baac22017-08-10 15:24:12 -0700288 if (force)
289 __tlb_adjust_range(tlb, start, end - start);
290
Peter Zijlstra9547d012011-05-24 17:12:14 -0700291 tlb_flush_mmu(tlb);
292
293 /* keep the page table cache within bounds */
294 check_pgt_cache();
295
296 for (batch = tlb->local.next; batch; batch = next) {
297 next = batch->next;
298 free_pages((unsigned long)batch, 0);
299 }
300 tlb->local.next = NULL;
301}
302
303/* __tlb_remove_page
304 * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
305 * handling the additional races in SMP caused by other CPUs caching valid
306 * mappings in their TLBs. Returns the number of free page slots left.
307 * When out of page slots we must call tlb_flush_mmu().
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -0700308 *returns true if the caller should flush.
Peter Zijlstra9547d012011-05-24 17:12:14 -0700309 */
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -0700310bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size)
Peter Zijlstra9547d012011-05-24 17:12:14 -0700311{
312 struct mmu_gather_batch *batch;
313
Will Deaconfb7332a2014-10-29 10:03:09 +0000314 VM_BUG_ON(!tlb->end);
Aneesh Kumar K.V692a68c2016-12-12 16:42:43 -0800315 VM_WARN_ON(tlb->page_size != page_size);
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -0700316
Peter Zijlstra9547d012011-05-24 17:12:14 -0700317 batch = tlb->active;
Aneesh Kumar K.V692a68c2016-12-12 16:42:43 -0800318 /*
319 * Add the page and check if we are full. If so
320 * force a flush.
321 */
322 batch->pages[batch->nr++] = page;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700323 if (batch->nr == batch->max) {
324 if (!tlb_next_batch(tlb))
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -0700325 return true;
Shaohua Li0b43c3a2011-07-08 15:39:41 -0700326 batch = tlb->active;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700327 }
Sasha Levin309381fea2014-01-23 15:52:54 -0800328 VM_BUG_ON_PAGE(batch->nr > batch->max, page);
Peter Zijlstra9547d012011-05-24 17:12:14 -0700329
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -0700330 return false;
Peter Zijlstra9547d012011-05-24 17:12:14 -0700331}
332
333#endif /* HAVE_GENERIC_MMU_GATHER */
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800334
Peter Zijlstra26723912011-05-24 17:12:00 -0700335#ifdef CONFIG_HAVE_RCU_TABLE_FREE
336
Peter Zijlstra52a288c2018-08-22 17:30:13 +0200337/*
338 * See the comment near struct mmu_table_batch.
339 */
340
Peter Zijlstrad86564a2018-08-22 17:30:15 +0200341/*
342 * If we want tlb_remove_table() to imply TLB invalidates.
343 */
344static inline void tlb_table_invalidate(struct mmu_gather *tlb)
345{
346#ifdef CONFIG_HAVE_RCU_TABLE_INVALIDATE
347 /*
348 * Invalidate page-table caches used by hardware walkers. Then we still
349 * need to RCU-sched wait while freeing the pages because software
350 * walkers can still be in-flight.
351 */
352 tlb_flush_mmu_tlbonly(tlb);
353#endif
354}
355
Peter Zijlstra26723912011-05-24 17:12:00 -0700356static void tlb_remove_table_smp_sync(void *arg)
357{
Peter Zijlstra52a288c2018-08-22 17:30:13 +0200358 /* Simply deliver the interrupt */
Peter Zijlstra26723912011-05-24 17:12:00 -0700359}
360
Peter Zijlstra52a288c2018-08-22 17:30:13 +0200361static void tlb_remove_table_one(void *table)
Peter Zijlstra26723912011-05-24 17:12:00 -0700362{
363 /*
364 * This isn't an RCU grace period and hence the page-tables cannot be
365 * assumed to be actually RCU-freed.
366 *
367 * It is however sufficient for software page-table walkers that rely on
368 * IRQ disabling. See the comment near struct mmu_table_batch.
369 */
Peter Zijlstra52a288c2018-08-22 17:30:13 +0200370 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
Peter Zijlstra26723912011-05-24 17:12:00 -0700371 __tlb_remove_table(table);
372}
373
374static void tlb_remove_table_rcu(struct rcu_head *head)
375{
376 struct mmu_table_batch *batch;
377 int i;
378
379 batch = container_of(head, struct mmu_table_batch, rcu);
380
381 for (i = 0; i < batch->nr; i++)
382 __tlb_remove_table(batch->tables[i]);
383
384 free_page((unsigned long)batch);
385}
386
387void tlb_table_flush(struct mmu_gather *tlb)
388{
389 struct mmu_table_batch **batch = &tlb->batch;
390
391 if (*batch) {
Peter Zijlstrad86564a2018-08-22 17:30:15 +0200392 tlb_table_invalidate(tlb);
Peter Zijlstra26723912011-05-24 17:12:00 -0700393 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
394 *batch = NULL;
395 }
396}
397
398void tlb_remove_table(struct mmu_gather *tlb, void *table)
399{
400 struct mmu_table_batch **batch = &tlb->batch;
401
Peter Zijlstra26723912011-05-24 17:12:00 -0700402 if (*batch == NULL) {
403 *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
404 if (*batch == NULL) {
Peter Zijlstrad86564a2018-08-22 17:30:15 +0200405 tlb_table_invalidate(tlb);
Peter Zijlstra52a288c2018-08-22 17:30:13 +0200406 tlb_remove_table_one(table);
Peter Zijlstra26723912011-05-24 17:12:00 -0700407 return;
408 }
409 (*batch)->nr = 0;
410 }
Peter Zijlstrad86564a2018-08-22 17:30:15 +0200411
Peter Zijlstra26723912011-05-24 17:12:00 -0700412 (*batch)->tables[(*batch)->nr++] = table;
413 if ((*batch)->nr == MAX_TABLE_BATCH)
414 tlb_table_flush(tlb);
415}
416
Peter Zijlstra9547d012011-05-24 17:12:14 -0700417#endif /* CONFIG_HAVE_RCU_TABLE_FREE */
Peter Zijlstra26723912011-05-24 17:12:00 -0700418
Mike Rapoportef549e12018-01-31 16:17:17 -0800419/**
420 * tlb_gather_mmu - initialize an mmu_gather structure for page-table tear-down
421 * @tlb: the mmu_gather structure to initialize
422 * @mm: the mm_struct of the target address space
423 * @start: start of the region that will be removed from the page-table
424 * @end: end of the region that will be removed from the page-table
425 *
426 * Called to initialize an (on-stack) mmu_gather structure for page-table
427 * tear-down from @mm. The @start and @end are set to 0 and -1
428 * respectively when @mm is without users and we're going to destroy
429 * the full address space (exit/execve).
Minchan Kim56236a52017-08-10 15:24:05 -0700430 */
431void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
432 unsigned long start, unsigned long end)
433{
434 arch_tlb_gather_mmu(tlb, mm, start, end);
Minchan Kim99baac22017-08-10 15:24:12 -0700435 inc_tlb_flush_pending(tlb->mm);
Minchan Kim56236a52017-08-10 15:24:05 -0700436}
437
438void tlb_finish_mmu(struct mmu_gather *tlb,
439 unsigned long start, unsigned long end)
440{
Minchan Kim99baac22017-08-10 15:24:12 -0700441 /*
442 * If there are parallel threads are doing PTE changes on same range
443 * under non-exclusive lock(e.g., mmap_sem read-side) but defer TLB
444 * flush by batching, a thread has stable TLB entry can fail to flush
445 * the TLB by observing pte_none|!pte_dirty, for example so flush TLB
446 * forcefully if we detect parallel PTE batching threads.
447 */
448 bool force = mm_tlb_flush_nested(tlb->mm);
449
450 arch_tlb_finish_mmu(tlb, start, end, force);
451 dec_tlb_flush_pending(tlb->mm);
Minchan Kim56236a52017-08-10 15:24:05 -0700452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * Note: this doesn't free the actual pages themselves. That
456 * has been handled earlier when unmapping all the memory regions.
457 */
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000458static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
459 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800461 pgtable_t token = pmd_pgtable(*pmd);
Hugh Dickinse0da3822005-04-19 13:29:15 -0700462 pmd_clear(pmd);
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000463 pte_free_tlb(tlb, token, addr);
Kirill A. Shutemovc4812902017-11-15 17:35:37 -0800464 mm_dec_nr_ptes(tlb->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Hugh Dickinse0da3822005-04-19 13:29:15 -0700467static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
468 unsigned long addr, unsigned long end,
469 unsigned long floor, unsigned long ceiling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 pmd_t *pmd;
472 unsigned long next;
Hugh Dickinse0da3822005-04-19 13:29:15 -0700473 unsigned long start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Hugh Dickinse0da3822005-04-19 13:29:15 -0700475 start = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 pmd = pmd_offset(pud, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 do {
478 next = pmd_addr_end(addr, end);
479 if (pmd_none_or_clear_bad(pmd))
480 continue;
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000481 free_pte_range(tlb, pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 } while (pmd++, addr = next, addr != end);
483
Hugh Dickinse0da3822005-04-19 13:29:15 -0700484 start &= PUD_MASK;
485 if (start < floor)
486 return;
487 if (ceiling) {
488 ceiling &= PUD_MASK;
489 if (!ceiling)
490 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
Hugh Dickinse0da3822005-04-19 13:29:15 -0700492 if (end - 1 > ceiling - 1)
493 return;
494
495 pmd = pmd_offset(pud, start);
496 pud_clear(pud);
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000497 pmd_free_tlb(tlb, pmd, start);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800498 mm_dec_nr_pmds(tlb->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300501static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d,
Hugh Dickinse0da3822005-04-19 13:29:15 -0700502 unsigned long addr, unsigned long end,
503 unsigned long floor, unsigned long ceiling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 pud_t *pud;
506 unsigned long next;
Hugh Dickinse0da3822005-04-19 13:29:15 -0700507 unsigned long start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Hugh Dickinse0da3822005-04-19 13:29:15 -0700509 start = addr;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300510 pud = pud_offset(p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 do {
512 next = pud_addr_end(addr, end);
513 if (pud_none_or_clear_bad(pud))
514 continue;
Hugh Dickinse0da3822005-04-19 13:29:15 -0700515 free_pmd_range(tlb, pud, addr, next, floor, ceiling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 } while (pud++, addr = next, addr != end);
517
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300518 start &= P4D_MASK;
519 if (start < floor)
520 return;
521 if (ceiling) {
522 ceiling &= P4D_MASK;
523 if (!ceiling)
524 return;
525 }
526 if (end - 1 > ceiling - 1)
527 return;
528
529 pud = pud_offset(p4d, start);
530 p4d_clear(p4d);
531 pud_free_tlb(tlb, pud, start);
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -0800532 mm_dec_nr_puds(tlb->mm);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300533}
534
535static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd,
536 unsigned long addr, unsigned long end,
537 unsigned long floor, unsigned long ceiling)
538{
539 p4d_t *p4d;
540 unsigned long next;
541 unsigned long start;
542
543 start = addr;
544 p4d = p4d_offset(pgd, addr);
545 do {
546 next = p4d_addr_end(addr, end);
547 if (p4d_none_or_clear_bad(p4d))
548 continue;
549 free_pud_range(tlb, p4d, addr, next, floor, ceiling);
550 } while (p4d++, addr = next, addr != end);
551
Hugh Dickinse0da3822005-04-19 13:29:15 -0700552 start &= PGDIR_MASK;
553 if (start < floor)
554 return;
555 if (ceiling) {
556 ceiling &= PGDIR_MASK;
557 if (!ceiling)
558 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
Hugh Dickinse0da3822005-04-19 13:29:15 -0700560 if (end - 1 > ceiling - 1)
561 return;
562
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300563 p4d = p4d_offset(pgd, start);
Hugh Dickinse0da3822005-04-19 13:29:15 -0700564 pgd_clear(pgd);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300565 p4d_free_tlb(tlb, p4d, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568/*
Hugh Dickinse0da3822005-04-19 13:29:15 -0700569 * This function frees user-level page tables of a process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 */
Jan Beulich42b77722008-07-23 21:27:10 -0700571void free_pgd_range(struct mmu_gather *tlb,
Hugh Dickinse0da3822005-04-19 13:29:15 -0700572 unsigned long addr, unsigned long end,
573 unsigned long floor, unsigned long ceiling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 pgd_t *pgd;
576 unsigned long next;
577
Hugh Dickinse0da3822005-04-19 13:29:15 -0700578 /*
579 * The next few lines have given us lots of grief...
580 *
581 * Why are we testing PMD* at this top level? Because often
582 * there will be no work to do at all, and we'd prefer not to
583 * go all the way down to the bottom just to discover that.
584 *
585 * Why all these "- 1"s? Because 0 represents both the bottom
586 * of the address space and the top of it (using -1 for the
587 * top wouldn't help much: the masks would do the wrong thing).
588 * The rule is that addr 0 and floor 0 refer to the bottom of
589 * the address space, but end 0 and ceiling 0 refer to the top
590 * Comparisons need to use "end - 1" and "ceiling - 1" (though
591 * that end 0 case should be mythical).
592 *
593 * Wherever addr is brought up or ceiling brought down, we must
594 * be careful to reject "the opposite 0" before it confuses the
595 * subsequent tests. But what about where end is brought down
596 * by PMD_SIZE below? no, end can't go down to 0 there.
597 *
598 * Whereas we round start (addr) and ceiling down, by different
599 * masks at different levels, in order to test whether a table
600 * now has no other vmas using it, so can be freed, we don't
601 * bother to round floor or end up - the tests don't need that.
602 */
603
604 addr &= PMD_MASK;
605 if (addr < floor) {
606 addr += PMD_SIZE;
607 if (!addr)
608 return;
609 }
610 if (ceiling) {
611 ceiling &= PMD_MASK;
612 if (!ceiling)
613 return;
614 }
615 if (end - 1 > ceiling - 1)
616 end -= PMD_SIZE;
617 if (addr > end - 1)
618 return;
Aneesh Kumar K.V07e32662016-12-12 16:42:40 -0800619 /*
620 * We add page table cache pages with PAGE_SIZE,
621 * (see pte_free_tlb()), flush the tlb if we need
622 */
623 tlb_remove_check_page_size_change(tlb, PAGE_SIZE);
Jan Beulich42b77722008-07-23 21:27:10 -0700624 pgd = pgd_offset(tlb->mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 do {
626 next = pgd_addr_end(addr, end);
627 if (pgd_none_or_clear_bad(pgd))
628 continue;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300629 free_p4d_range(tlb, pgd, addr, next, floor, ceiling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 } while (pgd++, addr = next, addr != end);
Hugh Dickinse0da3822005-04-19 13:29:15 -0700631}
632
Jan Beulich42b77722008-07-23 21:27:10 -0700633void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700634 unsigned long floor, unsigned long ceiling)
Hugh Dickinse0da3822005-04-19 13:29:15 -0700635{
636 while (vma) {
637 struct vm_area_struct *next = vma->vm_next;
638 unsigned long addr = vma->vm_start;
639
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700640 /*
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +1000641 * Hide vma from rmap and truncate_pagecache before freeing
642 * pgtables
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700643 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800644 unlink_anon_vmas(vma);
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700645 unlink_file_vma(vma);
646
David Gibson9da61ae2006-03-22 00:08:57 -0800647 if (is_vm_hugetlb_page(vma)) {
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700648 hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
Tobin C Harding166f61b2017-02-24 14:59:01 -0800649 floor, next ? next->vm_start : ceiling);
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700650 } else {
651 /*
652 * Optimization: gather nearby vmas into one call down
653 */
654 while (next && next->vm_start <= vma->vm_end + PMD_SIZE
David Gibson48669202006-03-22 00:08:58 -0800655 && !is_vm_hugetlb_page(next)) {
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700656 vma = next;
657 next = vma->vm_next;
Rik van Riel5beb4932010-03-05 13:42:07 -0800658 unlink_anon_vmas(vma);
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700659 unlink_file_vma(vma);
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700660 }
661 free_pgd_range(tlb, addr, vma->vm_end,
Tobin C Harding166f61b2017-02-24 14:59:01 -0800662 floor, next ? next->vm_start : ceiling);
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700663 }
Hugh Dickinse0da3822005-04-19 13:29:15 -0700664 vma = next;
665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Kirill A. Shutemov3ed3a4f2016-03-17 14:19:11 -0700668int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800670 spinlock_t *ptl;
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800671 pgtable_t new = pte_alloc_one(mm, address);
Hugh Dickins1bb36302005-10-29 18:16:22 -0700672 if (!new)
673 return -ENOMEM;
674
Nick Piggin362a61a2008-05-14 06:37:36 +0200675 /*
676 * Ensure all pte setup (eg. pte page lock and page clearing) are
677 * visible before the pte is made visible to other CPUs by being
678 * put into page tables.
679 *
680 * The other side of the story is the pointer chasing in the page
681 * table walking code (when walking the page table without locking;
682 * ie. most of the time). Fortunately, these data accesses consist
683 * of a chain of data-dependent loads, meaning most CPUs (alpha
684 * being the notable exception) will already guarantee loads are
685 * seen in-order. See the alpha page table accessors for the
686 * smp_read_barrier_depends() barriers in page table walking code.
687 */
688 smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
689
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800690 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli8ac1f832011-01-13 15:46:43 -0800691 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
Kirill A. Shutemovc4812902017-11-15 17:35:37 -0800692 mm_inc_nr_ptes(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 pmd_populate(mm, pmd, new);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800694 new = NULL;
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -0800695 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800696 spin_unlock(ptl);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800697 if (new)
698 pte_free(mm, new);
Hugh Dickins1bb36302005-10-29 18:16:22 -0700699 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Hugh Dickins1bb36302005-10-29 18:16:22 -0700702int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Hugh Dickins1bb36302005-10-29 18:16:22 -0700704 pte_t *new = pte_alloc_one_kernel(&init_mm, address);
705 if (!new)
706 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Nick Piggin362a61a2008-05-14 06:37:36 +0200708 smp_wmb(); /* See comment in __pte_alloc */
709
Hugh Dickins1bb36302005-10-29 18:16:22 -0700710 spin_lock(&init_mm.page_table_lock);
Andrea Arcangeli8ac1f832011-01-13 15:46:43 -0800711 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
Hugh Dickins1bb36302005-10-29 18:16:22 -0700712 pmd_populate_kernel(&init_mm, pmd, new);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800713 new = NULL;
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -0800714 }
Hugh Dickins1bb36302005-10-29 18:16:22 -0700715 spin_unlock(&init_mm.page_table_lock);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800716 if (new)
717 pte_free_kernel(&init_mm, new);
Hugh Dickins1bb36302005-10-29 18:16:22 -0700718 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800721static inline void init_rss_vec(int *rss)
Hugh Dickinsae859762005-10-29 18:16:05 -0700722{
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800723 memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
724}
725
726static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
727{
728 int i;
729
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800730 if (current->mm == mm)
David Rientjes05af2e12012-03-21 16:34:13 -0700731 sync_mm_rss(mm);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800732 for (i = 0; i < NR_MM_COUNTERS; i++)
733 if (rss[i])
734 add_mm_counter(mm, i, rss[i]);
Hugh Dickinsae859762005-10-29 18:16:05 -0700735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737/*
Linus Torvalds6aab3412005-11-28 14:34:23 -0800738 * This function is called to print an error when a bad pte
739 * is found. For example, we might have a PFN-mapped pte in
740 * a region that doesn't allow it.
Nick Pigginb5810032005-10-29 18:16:12 -0700741 *
742 * The calling function must still handle the error.
743 */
Hugh Dickins3dc14742009-01-06 14:40:08 -0800744static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
745 pte_t pte, struct page *page)
Nick Pigginb5810032005-10-29 18:16:12 -0700746{
Hugh Dickins3dc14742009-01-06 14:40:08 -0800747 pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300748 p4d_t *p4d = p4d_offset(pgd, addr);
749 pud_t *pud = pud_offset(p4d, addr);
Hugh Dickins3dc14742009-01-06 14:40:08 -0800750 pmd_t *pmd = pmd_offset(pud, addr);
751 struct address_space *mapping;
752 pgoff_t index;
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800753 static unsigned long resume;
754 static unsigned long nr_shown;
755 static unsigned long nr_unshown;
756
757 /*
758 * Allow a burst of 60 reports, then keep quiet for that minute;
759 * or allow a steady drip of one report per second.
760 */
761 if (nr_shown == 60) {
762 if (time_before(jiffies, resume)) {
763 nr_unshown++;
764 return;
765 }
766 if (nr_unshown) {
Joe Perches11705322016-03-17 14:19:50 -0700767 pr_alert("BUG: Bad page map: %lu messages suppressed\n",
768 nr_unshown);
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800769 nr_unshown = 0;
770 }
771 nr_shown = 0;
772 }
773 if (nr_shown++ == 0)
774 resume = jiffies + 60 * HZ;
Hugh Dickins3dc14742009-01-06 14:40:08 -0800775
776 mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
777 index = linear_page_index(vma, addr);
778
Joe Perches11705322016-03-17 14:19:50 -0700779 pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n",
780 current->comm,
781 (long long)pte_val(pte), (long long)pmd_val(*pmd));
Wu Fengguang718a3822010-03-10 15:20:43 -0800782 if (page)
Dave Hansenf0b791a2014-01-23 15:52:49 -0800783 dump_page(page, "bad pte");
Joe Perches11705322016-03-17 14:19:50 -0700784 pr_alert("addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n",
785 (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
Konstantin Khlebnikov26825822015-04-15 16:15:08 -0700786 pr_alert("file:%pD fault:%pf mmap:%pf readpage:%pf\n",
787 vma->vm_file,
788 vma->vm_ops ? vma->vm_ops->fault : NULL,
789 vma->vm_file ? vma->vm_file->f_op->mmap : NULL,
790 mapping ? mapping->a_ops->readpage : NULL);
Nick Pigginb5810032005-10-29 18:16:12 -0700791 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +1030792 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Nick Pigginb5810032005-10-29 18:16:12 -0700793}
794
795/*
Nick Piggin7e675132008-04-28 02:13:00 -0700796 * vm_normal_page -- This function gets the "struct page" associated with a pte.
Linus Torvalds6aab3412005-11-28 14:34:23 -0800797 *
Nick Piggin7e675132008-04-28 02:13:00 -0700798 * "Special" mappings do not wish to be associated with a "struct page" (either
799 * it doesn't exist, or it exists but they don't want to touch it). In this
800 * case, NULL is returned here. "Normal" mappings do have a struct page.
Jared Hulbertb379d792008-04-28 02:12:58 -0700801 *
Nick Piggin7e675132008-04-28 02:13:00 -0700802 * There are 2 broad cases. Firstly, an architecture may define a pte_special()
803 * pte bit, in which case this function is trivial. Secondly, an architecture
804 * may not have a spare pte bit, which requires a more complicated scheme,
805 * described below.
806 *
807 * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
808 * special mapping (even if there are underlying and valid "struct pages").
809 * COWed pages of a VM_PFNMAP are always normal.
Linus Torvalds6aab3412005-11-28 14:34:23 -0800810 *
Jared Hulbertb379d792008-04-28 02:12:58 -0700811 * The way we recognize COWed pages within VM_PFNMAP mappings is through the
812 * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
Nick Piggin7e675132008-04-28 02:13:00 -0700813 * set, and the vm_pgoff will point to the first PFN mapped: thus every special
814 * mapping will always honor the rule
Linus Torvalds6aab3412005-11-28 14:34:23 -0800815 *
816 * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
817 *
Nick Piggin7e675132008-04-28 02:13:00 -0700818 * And for normal mappings this is false.
Jared Hulbertb379d792008-04-28 02:12:58 -0700819 *
Nick Piggin7e675132008-04-28 02:13:00 -0700820 * This restricts such mappings to be a linear translation from virtual address
821 * to pfn. To get around this restriction, we allow arbitrary mappings so long
822 * as the vma is not a COW mapping; in that case, we know that all ptes are
823 * special (because none can have been COWed).
Jared Hulbertb379d792008-04-28 02:12:58 -0700824 *
825 *
Nick Piggin7e675132008-04-28 02:13:00 -0700826 * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
827 *
Jared Hulbertb379d792008-04-28 02:12:58 -0700828 * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
829 * page" backing, however the difference is that _all_ pages with a struct
830 * page (that is, those where pfn_valid is true) are refcounted and considered
831 * normal pages by the VM. The disadvantage is that pages are refcounted
832 * (which can be slower and simply not an option for some PFNMAP users). The
833 * advantage is that we don't have to follow the strict linearity rule of
834 * PFNMAP mappings in order to support COWable mappings.
835 *
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800836 */
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700837struct page *_vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
838 pte_t pte, bool with_public_device)
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800839{
Hugh Dickins22b31ee2009-01-06 14:40:09 -0800840 unsigned long pfn = pte_pfn(pte);
Nick Piggin7e675132008-04-28 02:13:00 -0700841
Laurent Dufour00b3a332018-06-07 17:06:12 -0700842 if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) {
Hugh Dickinsb38af472014-08-29 15:18:44 -0700843 if (likely(!pte_special(pte)))
Hugh Dickins22b31ee2009-01-06 14:40:09 -0800844 goto check_pfn;
David Vrabel667a0a02014-12-18 14:48:15 +0000845 if (vma->vm_ops && vma->vm_ops->find_special_page)
846 return vma->vm_ops->find_special_page(vma, addr);
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -0700847 if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
848 return NULL;
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700849 if (is_zero_pfn(pfn))
850 return NULL;
851
852 /*
853 * Device public pages are special pages (they are ZONE_DEVICE
854 * pages but different from persistent memory). They behave
855 * allmost like normal pages. The difference is that they are
856 * not on the lru and thus should never be involve with any-
857 * thing that involve lru manipulation (mlock, numa balancing,
858 * ...).
859 *
860 * This is why we still want to return NULL for such page from
861 * vm_normal_page() so that we do not have to special case all
862 * call site of vm_normal_page().
863 */
Reza Arbab7d790d22017-10-03 16:15:35 -0700864 if (likely(pfn <= highest_memmap_pfn)) {
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700865 struct page *page = pfn_to_page(pfn);
866
867 if (is_device_public_page(page)) {
868 if (with_public_device)
869 return page;
870 return NULL;
871 }
872 }
Dave Jiange1fb4a02018-08-17 15:43:40 -0700873
874 if (pte_devmap(pte))
875 return NULL;
876
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700877 print_bad_pte(vma, addr, pte, NULL);
Nick Piggin7e675132008-04-28 02:13:00 -0700878 return NULL;
879 }
880
Laurent Dufour00b3a332018-06-07 17:06:12 -0700881 /* !CONFIG_ARCH_HAS_PTE_SPECIAL case follows: */
Nick Piggin7e675132008-04-28 02:13:00 -0700882
Jared Hulbertb379d792008-04-28 02:12:58 -0700883 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
884 if (vma->vm_flags & VM_MIXEDMAP) {
885 if (!pfn_valid(pfn))
886 return NULL;
887 goto out;
888 } else {
Nick Piggin7e675132008-04-28 02:13:00 -0700889 unsigned long off;
890 off = (addr - vma->vm_start) >> PAGE_SHIFT;
Jared Hulbertb379d792008-04-28 02:12:58 -0700891 if (pfn == vma->vm_pgoff + off)
892 return NULL;
893 if (!is_cow_mapping(vma->vm_flags))
894 return NULL;
895 }
Linus Torvalds6aab3412005-11-28 14:34:23 -0800896 }
897
Hugh Dickinsb38af472014-08-29 15:18:44 -0700898 if (is_zero_pfn(pfn))
899 return NULL;
Laurent Dufour00b3a332018-06-07 17:06:12 -0700900
Hugh Dickins22b31ee2009-01-06 14:40:09 -0800901check_pfn:
902 if (unlikely(pfn > highest_memmap_pfn)) {
903 print_bad_pte(vma, addr, pte, NULL);
904 return NULL;
905 }
Linus Torvalds6aab3412005-11-28 14:34:23 -0800906
907 /*
Nick Piggin7e675132008-04-28 02:13:00 -0700908 * NOTE! We still have PageReserved() pages in the page tables.
Nick Piggin7e675132008-04-28 02:13:00 -0700909 * eg. VDSO mappings can cause them to exist.
Linus Torvalds6aab3412005-11-28 14:34:23 -0800910 */
Jared Hulbertb379d792008-04-28 02:12:58 -0700911out:
Linus Torvalds6aab3412005-11-28 14:34:23 -0800912 return pfn_to_page(pfn);
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800913}
914
Gerald Schaefer28093f92016-04-28 16:18:35 -0700915#ifdef CONFIG_TRANSPARENT_HUGEPAGE
916struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
917 pmd_t pmd)
918{
919 unsigned long pfn = pmd_pfn(pmd);
920
921 /*
922 * There is no pmd_special() but there may be special pmds, e.g.
923 * in a direct-access (dax) mapping, so let's just replicate the
Laurent Dufour00b3a332018-06-07 17:06:12 -0700924 * !CONFIG_ARCH_HAS_PTE_SPECIAL case from vm_normal_page() here.
Gerald Schaefer28093f92016-04-28 16:18:35 -0700925 */
926 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
927 if (vma->vm_flags & VM_MIXEDMAP) {
928 if (!pfn_valid(pfn))
929 return NULL;
930 goto out;
931 } else {
932 unsigned long off;
933 off = (addr - vma->vm_start) >> PAGE_SHIFT;
934 if (pfn == vma->vm_pgoff + off)
935 return NULL;
936 if (!is_cow_mapping(vma->vm_flags))
937 return NULL;
938 }
939 }
940
Dave Jiange1fb4a02018-08-17 15:43:40 -0700941 if (pmd_devmap(pmd))
942 return NULL;
Gerald Schaefer28093f92016-04-28 16:18:35 -0700943 if (is_zero_pfn(pfn))
944 return NULL;
945 if (unlikely(pfn > highest_memmap_pfn))
946 return NULL;
947
948 /*
949 * NOTE! We still have PageReserved() pages in the page tables.
950 * eg. VDSO mappings can cause them to exist.
951 */
952out:
953 return pfn_to_page(pfn);
954}
955#endif
956
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800957/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * copy one vm_area from one task to the other. Assumes the page tables
959 * already present in the new task to be cleared in the whole range
960 * covered by this vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 */
962
Hugh Dickins570a335b2009-12-14 17:58:46 -0800963static inline unsigned long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
Nick Pigginb5810032005-10-29 18:16:12 -0700965 pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
Hugh Dickins8c103762005-10-29 18:16:13 -0700966 unsigned long addr, int *rss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Nick Pigginb5810032005-10-29 18:16:12 -0700968 unsigned long vm_flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 pte_t pte = *src_pte;
970 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 /* pte contains position in swap or file, so copy. */
973 if (unlikely(!pte_present(pte))) {
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800974 swp_entry_t entry = pte_to_swp_entry(pte);
Christoph Lameter06972122006-06-23 02:03:35 -0700975
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800976 if (likely(!non_swap_entry(entry))) {
977 if (swap_duplicate(entry) < 0)
978 return entry.val;
Hugh Dickins570a335b2009-12-14 17:58:46 -0800979
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800980 /* make sure dst_mm is on swapoff's mmlist. */
981 if (unlikely(list_empty(&dst_mm->mmlist))) {
982 spin_lock(&mmlist_lock);
983 if (list_empty(&dst_mm->mmlist))
984 list_add(&dst_mm->mmlist,
985 &src_mm->mmlist);
986 spin_unlock(&mmlist_lock);
987 }
988 rss[MM_SWAPENTS]++;
989 } else if (is_migration_entry(entry)) {
990 page = migration_entry_to_page(entry);
Konstantin Khlebnikov9f9f1ac2012-01-20 14:34:24 -0800991
Jerome Marchandeca56ff2016-01-14 15:19:26 -0800992 rss[mm_counter(page)]++;
Konstantin Khlebnikov9f9f1ac2012-01-20 14:34:24 -0800993
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800994 if (is_write_migration_entry(entry) &&
995 is_cow_mapping(vm_flags)) {
996 /*
997 * COW mappings require pages in both
998 * parent and child to be set to read.
999 */
1000 make_migration_entry_read(&entry);
1001 pte = swp_entry_to_pte(entry);
1002 if (pte_swp_soft_dirty(*src_pte))
1003 pte = pte_swp_mksoft_dirty(pte);
1004 set_pte_at(src_mm, addr, src_pte, pte);
Christoph Lameter06972122006-06-23 02:03:35 -07001005 }
Jérôme Glisse5042db42017-09-08 16:11:43 -07001006 } else if (is_device_private_entry(entry)) {
1007 page = device_private_entry_to_page(entry);
1008
1009 /*
1010 * Update rss count even for unaddressable pages, as
1011 * they should treated just like normal pages in this
1012 * respect.
1013 *
1014 * We will likely want to have some new rss counters
1015 * for unaddressable pages, at some point. But for now
1016 * keep things as they are.
1017 */
1018 get_page(page);
1019 rss[mm_counter(page)]++;
1020 page_dup_rmap(page, false);
1021
1022 /*
1023 * We do not preserve soft-dirty information, because so
1024 * far, checkpoint/restore is the only feature that
1025 * requires that. And checkpoint/restore does not work
1026 * when a device driver is involved (you cannot easily
1027 * save and restore device driver state).
1028 */
1029 if (is_write_device_private_entry(entry) &&
1030 is_cow_mapping(vm_flags)) {
1031 make_device_private_entry_read(&entry);
1032 pte = swp_entry_to_pte(entry);
1033 set_pte_at(src_mm, addr, src_pte, pte);
1034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
Hugh Dickinsae859762005-10-29 18:16:05 -07001036 goto out_set_pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 /*
1040 * If it's a COW mapping, write protect it both
1041 * in the parent and the child
1042 */
Linus Torvalds1b2de5d2018-07-09 13:19:49 -07001043 if (is_cow_mapping(vm_flags) && pte_write(pte)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 ptep_set_wrprotect(src_mm, addr, src_pte);
Zachary Amsden3dc90792006-09-30 23:29:30 -07001045 pte = pte_wrprotect(pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
1047
1048 /*
1049 * If it's a shared mapping, mark it clean in
1050 * the child
1051 */
1052 if (vm_flags & VM_SHARED)
1053 pte = pte_mkclean(pte);
1054 pte = pte_mkold(pte);
Linus Torvalds6aab3412005-11-28 14:34:23 -08001055
1056 page = vm_normal_page(vma, addr, pte);
1057 if (page) {
1058 get_page(page);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001059 page_dup_rmap(page, false);
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001060 rss[mm_counter(page)]++;
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001061 } else if (pte_devmap(pte)) {
1062 page = pte_page(pte);
1063
1064 /*
1065 * Cache coherent device memory behave like regular page and
1066 * not like persistent memory page. For more informations see
1067 * MEMORY_DEVICE_CACHE_COHERENT in memory_hotplug.h
1068 */
1069 if (is_device_public_page(page)) {
1070 get_page(page);
1071 page_dup_rmap(page, false);
1072 rss[mm_counter(page)]++;
1073 }
Linus Torvalds6aab3412005-11-28 14:34:23 -08001074 }
Hugh Dickinsae859762005-10-29 18:16:05 -07001075
1076out_set_pte:
1077 set_pte_at(dst_mm, addr, dst_pte, pte);
Hugh Dickins570a335b2009-12-14 17:58:46 -08001078 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
Jerome Marchand21bda262014-08-06 16:06:56 -07001081static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001082 pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
1083 unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Daisuke Nishimurac36987e2009-10-26 16:50:23 -07001085 pte_t *orig_src_pte, *orig_dst_pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 pte_t *src_pte, *dst_pte;
Hugh Dickinsc74df322005-10-29 18:16:23 -07001087 spinlock_t *src_ptl, *dst_ptl;
Hugh Dickinse040f212005-10-29 18:15:53 -07001088 int progress = 0;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001089 int rss[NR_MM_COUNTERS];
Hugh Dickins570a335b2009-12-14 17:58:46 -08001090 swp_entry_t entry = (swp_entry_t){0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092again:
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001093 init_rss_vec(rss);
1094
Hugh Dickinsc74df322005-10-29 18:16:23 -07001095 dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (!dst_pte)
1097 return -ENOMEM;
Peter Zijlstraece0e2b2010-10-26 14:21:52 -07001098 src_pte = pte_offset_map(src_pmd, addr);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07001099 src_ptl = pte_lockptr(src_mm, src_pmd);
Ingo Molnarf20dc5f2006-07-03 00:25:08 -07001100 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Daisuke Nishimurac36987e2009-10-26 16:50:23 -07001101 orig_src_pte = src_pte;
1102 orig_dst_pte = dst_pte;
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001103 arch_enter_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 do {
1106 /*
1107 * We are holding two locks at this point - either of them
1108 * could generate latencies in another task on another CPU.
1109 */
Hugh Dickinse040f212005-10-29 18:15:53 -07001110 if (progress >= 32) {
1111 progress = 0;
1112 if (need_resched() ||
Nick Piggin95c354f2008-01-30 13:31:20 +01001113 spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
Hugh Dickinse040f212005-10-29 18:15:53 -07001114 break;
1115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (pte_none(*src_pte)) {
1117 progress++;
1118 continue;
1119 }
Hugh Dickins570a335b2009-12-14 17:58:46 -08001120 entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
1121 vma, addr, rss);
1122 if (entry.val)
1123 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 progress += 8;
1125 } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001127 arch_leave_lazy_mmu_mode();
Hugh Dickinsc74df322005-10-29 18:16:23 -07001128 spin_unlock(src_ptl);
Peter Zijlstraece0e2b2010-10-26 14:21:52 -07001129 pte_unmap(orig_src_pte);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001130 add_mm_rss_vec(dst_mm, rss);
Daisuke Nishimurac36987e2009-10-26 16:50:23 -07001131 pte_unmap_unlock(orig_dst_pte, dst_ptl);
Hugh Dickinsc74df322005-10-29 18:16:23 -07001132 cond_resched();
Hugh Dickins570a335b2009-12-14 17:58:46 -08001133
1134 if (entry.val) {
1135 if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
1136 return -ENOMEM;
1137 progress = 0;
1138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (addr != end)
1140 goto again;
1141 return 0;
1142}
1143
1144static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1145 pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
1146 unsigned long addr, unsigned long end)
1147{
1148 pmd_t *src_pmd, *dst_pmd;
1149 unsigned long next;
1150
1151 dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
1152 if (!dst_pmd)
1153 return -ENOMEM;
1154 src_pmd = pmd_offset(src_pud, addr);
1155 do {
1156 next = pmd_addr_end(addr, end);
Zi Yan84c3fc42017-09-08 16:11:01 -07001157 if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd)
1158 || pmd_devmap(*src_pmd)) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001159 int err;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001160 VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001161 err = copy_huge_pmd(dst_mm, src_mm,
1162 dst_pmd, src_pmd, addr, vma);
1163 if (err == -ENOMEM)
1164 return -ENOMEM;
1165 if (!err)
1166 continue;
1167 /* fall through */
1168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (pmd_none_or_clear_bad(src_pmd))
1170 continue;
1171 if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
1172 vma, addr, next))
1173 return -ENOMEM;
1174 } while (dst_pmd++, src_pmd++, addr = next, addr != end);
1175 return 0;
1176}
1177
1178static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001179 p4d_t *dst_p4d, p4d_t *src_p4d, struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 unsigned long addr, unsigned long end)
1181{
1182 pud_t *src_pud, *dst_pud;
1183 unsigned long next;
1184
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001185 dst_pud = pud_alloc(dst_mm, dst_p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (!dst_pud)
1187 return -ENOMEM;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001188 src_pud = pud_offset(src_p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 do {
1190 next = pud_addr_end(addr, end);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001191 if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) {
1192 int err;
1193
1194 VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, vma);
1195 err = copy_huge_pud(dst_mm, src_mm,
1196 dst_pud, src_pud, addr, vma);
1197 if (err == -ENOMEM)
1198 return -ENOMEM;
1199 if (!err)
1200 continue;
1201 /* fall through */
1202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (pud_none_or_clear_bad(src_pud))
1204 continue;
1205 if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
1206 vma, addr, next))
1207 return -ENOMEM;
1208 } while (dst_pud++, src_pud++, addr = next, addr != end);
1209 return 0;
1210}
1211
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001212static inline int copy_p4d_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1213 pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
1214 unsigned long addr, unsigned long end)
1215{
1216 p4d_t *src_p4d, *dst_p4d;
1217 unsigned long next;
1218
1219 dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr);
1220 if (!dst_p4d)
1221 return -ENOMEM;
1222 src_p4d = p4d_offset(src_pgd, addr);
1223 do {
1224 next = p4d_addr_end(addr, end);
1225 if (p4d_none_or_clear_bad(src_p4d))
1226 continue;
1227 if (copy_pud_range(dst_mm, src_mm, dst_p4d, src_p4d,
1228 vma, addr, next))
1229 return -ENOMEM;
1230 } while (dst_p4d++, src_p4d++, addr = next, addr != end);
1231 return 0;
1232}
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1235 struct vm_area_struct *vma)
1236{
1237 pgd_t *src_pgd, *dst_pgd;
1238 unsigned long next;
1239 unsigned long addr = vma->vm_start;
1240 unsigned long end = vma->vm_end;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001241 unsigned long mmun_start; /* For mmu_notifiers */
1242 unsigned long mmun_end; /* For mmu_notifiers */
1243 bool is_cow;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001244 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Nick Piggind9928952005-08-28 16:49:11 +10001246 /*
1247 * Don't copy ptes where a page fault will fill them correctly.
1248 * Fork becomes much lighter when there are big shared or private
1249 * readonly mappings. The tradeoff is that copy_page_range is more
1250 * efficient than faulting.
1251 */
Kirill A. Shutemov0661a332015-02-10 14:10:04 -08001252 if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) &&
1253 !vma->anon_vma)
1254 return 0;
Nick Piggind9928952005-08-28 16:49:11 +10001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (is_vm_hugetlb_page(vma))
1257 return copy_hugetlb_page_range(dst_mm, src_mm, vma);
1258
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001259 if (unlikely(vma->vm_flags & VM_PFNMAP)) {
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001260 /*
1261 * We do not free on error cases below as remove_vma
1262 * gets called on error from higher level routine
1263 */
Suresh Siddha5180da42012-10-08 16:28:29 -07001264 ret = track_pfn_copy(vma);
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001265 if (ret)
1266 return ret;
1267 }
1268
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001269 /*
1270 * We need to invalidate the secondary MMU mappings only when
1271 * there could be a permission downgrade on the ptes of the
1272 * parent mm. And a permission downgrade will only happen if
1273 * is_cow_mapping() returns true.
1274 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001275 is_cow = is_cow_mapping(vma->vm_flags);
1276 mmun_start = addr;
1277 mmun_end = end;
1278 if (is_cow)
1279 mmu_notifier_invalidate_range_start(src_mm, mmun_start,
1280 mmun_end);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001281
1282 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 dst_pgd = pgd_offset(dst_mm, addr);
1284 src_pgd = pgd_offset(src_mm, addr);
1285 do {
1286 next = pgd_addr_end(addr, end);
1287 if (pgd_none_or_clear_bad(src_pgd))
1288 continue;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001289 if (unlikely(copy_p4d_range(dst_mm, src_mm, dst_pgd, src_pgd,
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001290 vma, addr, next))) {
1291 ret = -ENOMEM;
1292 break;
1293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 } while (dst_pgd++, src_pgd++, addr = next, addr != end);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001295
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001296 if (is_cow)
1297 mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001298 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
Robin Holt51c6f662005-11-13 16:06:42 -08001301static unsigned long zap_pte_range(struct mmu_gather *tlb,
Nick Pigginb5810032005-10-29 18:16:12 -07001302 struct vm_area_struct *vma, pmd_t *pmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 unsigned long addr, unsigned long end,
Peter Zijlstra97a89412011-05-24 17:12:04 -07001304 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Nick Pigginb5810032005-10-29 18:16:12 -07001306 struct mm_struct *mm = tlb->mm;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001307 int force_flush = 0;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001308 int rss[NR_MM_COUNTERS];
Peter Zijlstra97a89412011-05-24 17:12:04 -07001309 spinlock_t *ptl;
Steven Rostedt5f1a1902011-06-15 15:08:23 -07001310 pte_t *start_pte;
Peter Zijlstra97a89412011-05-24 17:12:04 -07001311 pte_t *pte;
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001312 swp_entry_t entry;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001313
Aneesh Kumar K.V07e32662016-12-12 16:42:40 -08001314 tlb_remove_check_page_size_change(tlb, PAGE_SIZE);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001315again:
Peter Zijlstrae3032972011-05-24 17:12:01 -07001316 init_rss_vec(rss);
Steven Rostedt5f1a1902011-06-15 15:08:23 -07001317 start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
1318 pte = start_pte;
Mel Gorman3ea27712017-08-02 13:31:52 -07001319 flush_tlb_batched_pending(mm);
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001320 arch_enter_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 do {
1322 pte_t ptent = *pte;
Tobin C Harding166f61b2017-02-24 14:59:01 -08001323 if (pte_none(ptent))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 continue;
Hugh Dickins6f5e6b92006-03-16 23:04:09 -08001325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (pte_present(ptent)) {
Hugh Dickinsee498ed2005-11-21 21:32:18 -08001327 struct page *page;
Robin Holt51c6f662005-11-13 16:06:42 -08001328
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001329 page = _vm_normal_page(vma, addr, ptent, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (unlikely(details) && page) {
1331 /*
1332 * unmap_shared_mapping_pages() wants to
1333 * invalidate cache without truncating:
1334 * unmap shared but keep private pages.
1335 */
1336 if (details->check_mapping &&
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001337 details->check_mapping != page_rmapping(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
Nick Pigginb5810032005-10-29 18:16:12 -07001340 ptent = ptep_get_and_clear_full(mm, addr, pte,
Zachary Amsdena6003882005-09-03 15:55:04 -07001341 tlb->fullmm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 tlb_remove_tlb_entry(tlb, pte, addr);
1343 if (unlikely(!page))
1344 continue;
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001345
1346 if (!PageAnon(page)) {
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001347 if (pte_dirty(ptent)) {
1348 force_flush = 1;
Hugh Dickins6237bcd2005-10-29 18:15:54 -07001349 set_page_dirty(page);
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001350 }
Johannes Weiner4917e5d2009-01-06 14:39:17 -08001351 if (pte_young(ptent) &&
Joe Perches64363aa2013-07-08 16:00:18 -07001352 likely(!(vma->vm_flags & VM_SEQ_READ)))
Nick Pigginbf3f3bc2009-01-06 14:38:55 -08001353 mark_page_accessed(page);
Hugh Dickins6237bcd2005-10-29 18:15:54 -07001354 }
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001355 rss[mm_counter(page)]--;
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001356 page_remove_rmap(page, false);
Hugh Dickins3dc14742009-01-06 14:40:08 -08001357 if (unlikely(page_mapcount(page) < 0))
1358 print_bad_pte(vma, addr, ptent, page);
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -07001359 if (unlikely(__tlb_remove_page(tlb, page))) {
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001360 force_flush = 1;
Will Deaconce9ec372014-10-28 13:16:28 -07001361 addr += PAGE_SIZE;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001362 break;
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 continue;
1365 }
Jérôme Glisse5042db42017-09-08 16:11:43 -07001366
1367 entry = pte_to_swp_entry(ptent);
1368 if (non_swap_entry(entry) && is_device_private_entry(entry)) {
1369 struct page *page = device_private_entry_to_page(entry);
1370
1371 if (unlikely(details && details->check_mapping)) {
1372 /*
1373 * unmap_shared_mapping_pages() wants to
1374 * invalidate cache without truncating:
1375 * unmap shared but keep private pages.
1376 */
1377 if (details->check_mapping !=
1378 page_rmapping(page))
1379 continue;
1380 }
1381
1382 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
1383 rss[mm_counter(page)]--;
1384 page_remove_rmap(page, false);
1385 put_page(page);
1386 continue;
1387 }
1388
Kirill A. Shutemov3e8715f2017-02-22 15:46:34 -08001389 /* If details->check_mapping, we leave swap entries. */
1390 if (unlikely(details))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 continue;
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -08001392
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001393 entry = pte_to_swp_entry(ptent);
1394 if (!non_swap_entry(entry))
1395 rss[MM_SWAPENTS]--;
1396 else if (is_migration_entry(entry)) {
1397 struct page *page;
Konstantin Khlebnikov9f9f1ac2012-01-20 14:34:24 -08001398
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001399 page = migration_entry_to_page(entry);
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001400 rss[mm_counter(page)]--;
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -08001401 }
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001402 if (unlikely(!free_swap_and_cache(entry)))
1403 print_bad_pte(vma, addr, ptent, NULL);
Zachary Amsden9888a1c2006-09-30 23:29:31 -07001404 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
Peter Zijlstra97a89412011-05-24 17:12:04 -07001405 } while (pte++, addr += PAGE_SIZE, addr != end);
Hugh Dickinsae859762005-10-29 18:16:05 -07001406
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001407 add_mm_rss_vec(mm, rss);
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001408 arch_leave_lazy_mmu_mode();
Robin Holt51c6f662005-11-13 16:06:42 -08001409
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001410 /* Do the actual TLB flush before dropping ptl */
Will Deaconfb7332a2014-10-29 10:03:09 +00001411 if (force_flush)
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001412 tlb_flush_mmu_tlbonly(tlb);
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001413 pte_unmap_unlock(start_pte, ptl);
1414
1415 /*
1416 * If we forced a TLB flush (either due to running out of
1417 * batch buffers or because we needed to flush dirty TLB
1418 * entries before releasing the ptl), free the batched
1419 * memory too. Restart if we didn't do everything.
1420 */
1421 if (force_flush) {
1422 force_flush = 0;
1423 tlb_flush_mmu_free(tlb);
Linus Torvalds2b047252013-08-15 11:42:25 -07001424 if (addr != end)
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001425 goto again;
1426 }
1427
Robin Holt51c6f662005-11-13 16:06:42 -08001428 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Robin Holt51c6f662005-11-13 16:06:42 -08001431static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
Nick Pigginb5810032005-10-29 18:16:12 -07001432 struct vm_area_struct *vma, pud_t *pud,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 unsigned long addr, unsigned long end,
Peter Zijlstra97a89412011-05-24 17:12:04 -07001434 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 pmd_t *pmd;
1437 unsigned long next;
1438
1439 pmd = pmd_offset(pud, addr);
1440 do {
1441 next = pmd_addr_end(addr, end);
Zi Yan84c3fc42017-09-08 16:11:01 -07001442 if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
Hugh Dickins53406ed2018-08-01 11:31:52 -07001443 if (next - addr != HPAGE_PMD_SIZE)
David Rientjesfd607752016-12-12 16:42:20 -08001444 __split_huge_pmd(vma, pmd, addr, false, NULL);
Hugh Dickins53406ed2018-08-01 11:31:52 -07001445 else if (zap_huge_pmd(tlb, vma, pmd, addr))
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001446 goto next;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001447 /* fall through */
1448 }
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001449 /*
1450 * Here there can be other concurrent MADV_DONTNEED or
1451 * trans huge page faults running, and if the pmd is
1452 * none or trans huge it can change under us. This is
1453 * because MADV_DONTNEED holds the mmap_sem in read
1454 * mode.
1455 */
1456 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
1457 goto next;
Peter Zijlstra97a89412011-05-24 17:12:04 -07001458 next = zap_pte_range(tlb, vma, pmd, addr, next, details);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001459next:
Peter Zijlstra97a89412011-05-24 17:12:04 -07001460 cond_resched();
1461 } while (pmd++, addr = next, addr != end);
Robin Holt51c6f662005-11-13 16:06:42 -08001462
1463 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Robin Holt51c6f662005-11-13 16:06:42 -08001466static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001467 struct vm_area_struct *vma, p4d_t *p4d,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 unsigned long addr, unsigned long end,
Peter Zijlstra97a89412011-05-24 17:12:04 -07001469 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
1471 pud_t *pud;
1472 unsigned long next;
1473
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001474 pud = pud_offset(p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 do {
1476 next = pud_addr_end(addr, end);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001477 if (pud_trans_huge(*pud) || pud_devmap(*pud)) {
1478 if (next - addr != HPAGE_PUD_SIZE) {
1479 VM_BUG_ON_VMA(!rwsem_is_locked(&tlb->mm->mmap_sem), vma);
1480 split_huge_pud(vma, pud, addr);
1481 } else if (zap_huge_pud(tlb, vma, pud, addr))
1482 goto next;
1483 /* fall through */
1484 }
Peter Zijlstra97a89412011-05-24 17:12:04 -07001485 if (pud_none_or_clear_bad(pud))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 continue;
Peter Zijlstra97a89412011-05-24 17:12:04 -07001487 next = zap_pmd_range(tlb, vma, pud, addr, next, details);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001488next:
1489 cond_resched();
Peter Zijlstra97a89412011-05-24 17:12:04 -07001490 } while (pud++, addr = next, addr != end);
Robin Holt51c6f662005-11-13 16:06:42 -08001491
1492 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001495static inline unsigned long zap_p4d_range(struct mmu_gather *tlb,
1496 struct vm_area_struct *vma, pgd_t *pgd,
1497 unsigned long addr, unsigned long end,
1498 struct zap_details *details)
1499{
1500 p4d_t *p4d;
1501 unsigned long next;
1502
1503 p4d = p4d_offset(pgd, addr);
1504 do {
1505 next = p4d_addr_end(addr, end);
1506 if (p4d_none_or_clear_bad(p4d))
1507 continue;
1508 next = zap_pud_range(tlb, vma, p4d, addr, next, details);
1509 } while (p4d++, addr = next, addr != end);
1510
1511 return addr;
1512}
1513
Michal Hockoaac45362016-03-25 14:20:24 -07001514void unmap_page_range(struct mmu_gather *tlb,
Al Viro038c7aa2012-03-05 13:25:09 -05001515 struct vm_area_struct *vma,
1516 unsigned long addr, unsigned long end,
1517 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
1519 pgd_t *pgd;
1520 unsigned long next;
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 BUG_ON(addr >= end);
1523 tlb_start_vma(tlb, vma);
1524 pgd = pgd_offset(vma->vm_mm, addr);
1525 do {
1526 next = pgd_addr_end(addr, end);
Peter Zijlstra97a89412011-05-24 17:12:04 -07001527 if (pgd_none_or_clear_bad(pgd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 continue;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001529 next = zap_p4d_range(tlb, vma, pgd, addr, next, details);
Peter Zijlstra97a89412011-05-24 17:12:04 -07001530 } while (pgd++, addr = next, addr != end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 tlb_end_vma(tlb, vma);
1532}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Al Virof5cc4ee2012-03-05 14:14:20 -05001534
1535static void unmap_single_vma(struct mmu_gather *tlb,
1536 struct vm_area_struct *vma, unsigned long start_addr,
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001537 unsigned long end_addr,
Al Virof5cc4ee2012-03-05 14:14:20 -05001538 struct zap_details *details)
1539{
1540 unsigned long start = max(vma->vm_start, start_addr);
1541 unsigned long end;
1542
1543 if (start >= vma->vm_end)
1544 return;
1545 end = min(vma->vm_end, end_addr);
1546 if (end <= vma->vm_start)
1547 return;
1548
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301549 if (vma->vm_file)
1550 uprobe_munmap(vma, start, end);
1551
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001552 if (unlikely(vma->vm_flags & VM_PFNMAP))
Suresh Siddha5180da42012-10-08 16:28:29 -07001553 untrack_pfn(vma, 0, 0);
Al Virof5cc4ee2012-03-05 14:14:20 -05001554
1555 if (start != end) {
1556 if (unlikely(is_vm_hugetlb_page(vma))) {
1557 /*
1558 * It is undesirable to test vma->vm_file as it
1559 * should be non-null for valid hugetlb area.
1560 * However, vm_file will be NULL in the error
Davidlohr Bueso7aa6b4a2014-04-07 15:37:01 -07001561 * cleanup path of mmap_region. When
Al Virof5cc4ee2012-03-05 14:14:20 -05001562 * hugetlbfs ->mmap method fails,
Davidlohr Bueso7aa6b4a2014-04-07 15:37:01 -07001563 * mmap_region() nullifies vma->vm_file
Al Virof5cc4ee2012-03-05 14:14:20 -05001564 * before calling this function to clean up.
1565 * Since no pte has actually been setup, it is
1566 * safe to do nothing in this case.
1567 */
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07001568 if (vma->vm_file) {
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08001569 i_mmap_lock_write(vma->vm_file->f_mapping);
Mel Gormand8333522012-07-31 16:46:20 -07001570 __unmap_hugepage_range_final(tlb, vma, start, end, NULL);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08001571 i_mmap_unlock_write(vma->vm_file->f_mapping);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07001572 }
Al Virof5cc4ee2012-03-05 14:14:20 -05001573 } else
1574 unmap_page_range(tlb, vma, start, end, details);
1575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578/**
1579 * unmap_vmas - unmap a range of memory covered by a list of vma's
Randy Dunlap0164f692011-06-15 15:08:09 -07001580 * @tlb: address of the caller's struct mmu_gather
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 * @vma: the starting vma
1582 * @start_addr: virtual address at which to start unmapping
1583 * @end_addr: virtual address at which to end unmapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 *
Hugh Dickins508034a2005-10-29 18:16:30 -07001585 * Unmap all pages in the vma list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 * Only addresses between `start' and `end' will be unmapped.
1588 *
1589 * The VMA list must be sorted in ascending virtual address order.
1590 *
1591 * unmap_vmas() assumes that the caller will flush the whole unmapped address
1592 * range after unmap_vmas() returns. So the only responsibility here is to
1593 * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
1594 * drops the lock and schedules.
1595 */
Al Viro6e8bb012012-03-05 13:41:15 -05001596void unmap_vmas(struct mmu_gather *tlb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 struct vm_area_struct *vma, unsigned long start_addr,
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001598 unsigned long end_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001600 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001602 mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
Al Virof5cc4ee2012-03-05 14:14:20 -05001603 for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001604 unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001605 mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
1608/**
1609 * zap_page_range - remove user pages in a given range
1610 * @vma: vm_area_struct holding the applicable pages
Randy Dunlapeb4546b2012-06-20 12:53:02 -07001611 * @start: starting address of pages to zap
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 * @size: number of bytes to zap
Al Virof5cc4ee2012-03-05 14:14:20 -05001613 *
1614 * Caller must protect the VMA list
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 */
Linus Torvalds7e027b12012-05-06 13:43:15 -07001616void zap_page_range(struct vm_area_struct *vma, unsigned long start,
Kirill A. Shutemovecf13852017-02-22 15:46:37 -08001617 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 struct mm_struct *mm = vma->vm_mm;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001620 struct mmu_gather tlb;
Linus Torvalds7e027b12012-05-06 13:43:15 -07001621 unsigned long end = start + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 lru_add_drain();
Linus Torvalds2b047252013-08-15 11:42:25 -07001624 tlb_gather_mmu(&tlb, mm, start, end);
Hugh Dickins365e9c872005-10-29 18:16:18 -07001625 update_hiwater_rss(mm);
Linus Torvalds7e027b12012-05-06 13:43:15 -07001626 mmu_notifier_invalidate_range_start(mm, start, end);
Rik van Riel50c150f2018-08-17 15:48:53 -07001627 for ( ; vma && vma->vm_start < end; vma = vma->vm_next)
Kirill A. Shutemovecf13852017-02-22 15:46:37 -08001628 unmap_single_vma(&tlb, vma, start, end, NULL);
Linus Torvalds7e027b12012-05-06 13:43:15 -07001629 mmu_notifier_invalidate_range_end(mm, start, end);
1630 tlb_finish_mmu(&tlb, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
Jack Steinerc627f9c2008-07-29 22:33:53 -07001633/**
Al Virof5cc4ee2012-03-05 14:14:20 -05001634 * zap_page_range_single - remove user pages in a given range
1635 * @vma: vm_area_struct holding the applicable pages
1636 * @address: starting address of pages to zap
1637 * @size: number of bytes to zap
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001638 * @details: details of shared cache invalidation
Al Virof5cc4ee2012-03-05 14:14:20 -05001639 *
1640 * The range must fit into one VMA.
1641 */
1642static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
1643 unsigned long size, struct zap_details *details)
1644{
1645 struct mm_struct *mm = vma->vm_mm;
1646 struct mmu_gather tlb;
1647 unsigned long end = address + size;
Al Virof5cc4ee2012-03-05 14:14:20 -05001648
1649 lru_add_drain();
Linus Torvalds2b047252013-08-15 11:42:25 -07001650 tlb_gather_mmu(&tlb, mm, address, end);
Al Virof5cc4ee2012-03-05 14:14:20 -05001651 update_hiwater_rss(mm);
1652 mmu_notifier_invalidate_range_start(mm, address, end);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001653 unmap_single_vma(&tlb, vma, address, end, details);
Al Virof5cc4ee2012-03-05 14:14:20 -05001654 mmu_notifier_invalidate_range_end(mm, address, end);
1655 tlb_finish_mmu(&tlb, address, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
Jack Steinerc627f9c2008-07-29 22:33:53 -07001658/**
1659 * zap_vma_ptes - remove ptes mapping the vma
1660 * @vma: vm_area_struct holding ptes to be zapped
1661 * @address: starting address of pages to zap
1662 * @size: number of bytes to zap
1663 *
1664 * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1665 *
1666 * The entire address range must be fully contained within the vma.
1667 *
Jack Steinerc627f9c2008-07-29 22:33:53 -07001668 */
Leon Romanovsky27d036e2018-05-29 15:14:07 +03001669void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
Jack Steinerc627f9c2008-07-29 22:33:53 -07001670 unsigned long size)
1671{
1672 if (address < vma->vm_start || address + size > vma->vm_end ||
1673 !(vma->vm_flags & VM_PFNMAP))
Leon Romanovsky27d036e2018-05-29 15:14:07 +03001674 return;
1675
Al Virof5cc4ee2012-03-05 14:14:20 -05001676 zap_page_range_single(vma, address, size, NULL);
Jack Steinerc627f9c2008-07-29 22:33:53 -07001677}
1678EXPORT_SYMBOL_GPL(zap_vma_ptes);
1679
Namhyung Kim25ca1d62010-10-26 14:21:59 -07001680pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
Harvey Harrison920c7a52008-02-04 22:29:26 -08001681 spinlock_t **ptl)
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -08001682{
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001683 pgd_t *pgd;
1684 p4d_t *p4d;
1685 pud_t *pud;
1686 pmd_t *pmd;
1687
1688 pgd = pgd_offset(mm, addr);
1689 p4d = p4d_alloc(mm, pgd, addr);
1690 if (!p4d)
1691 return NULL;
1692 pud = pud_alloc(mm, p4d, addr);
1693 if (!pud)
1694 return NULL;
1695 pmd = pmd_alloc(mm, pud, addr);
1696 if (!pmd)
1697 return NULL;
1698
1699 VM_BUG_ON(pmd_trans_huge(*pmd));
1700 return pte_alloc_map_lock(mm, pmd, addr, ptl);
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -08001701}
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703/*
Linus Torvalds238f58d2005-11-29 13:01:56 -08001704 * This is the old fallback for page remapping.
1705 *
1706 * For historical reasons, it only allows reserved pages. Only
1707 * old drivers should use this, and they needed to mark their
1708 * pages reserved for the old functions anyway.
1709 */
Nick Piggin423bad602008-04-28 02:13:01 -07001710static int insert_page(struct vm_area_struct *vma, unsigned long addr,
1711 struct page *page, pgprot_t prot)
Linus Torvalds238f58d2005-11-29 13:01:56 -08001712{
Nick Piggin423bad602008-04-28 02:13:01 -07001713 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds238f58d2005-11-29 13:01:56 -08001714 int retval;
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -08001715 pte_t *pte;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001716 spinlock_t *ptl;
1717
Linus Torvalds238f58d2005-11-29 13:01:56 -08001718 retval = -EINVAL;
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001719 if (PageAnon(page))
KAMEZAWA Hiroyuki5b4e6552008-10-18 20:28:10 -07001720 goto out;
Linus Torvalds238f58d2005-11-29 13:01:56 -08001721 retval = -ENOMEM;
1722 flush_dcache_page(page);
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -08001723 pte = get_locked_pte(mm, addr, &ptl);
Linus Torvalds238f58d2005-11-29 13:01:56 -08001724 if (!pte)
KAMEZAWA Hiroyuki5b4e6552008-10-18 20:28:10 -07001725 goto out;
Linus Torvalds238f58d2005-11-29 13:01:56 -08001726 retval = -EBUSY;
1727 if (!pte_none(*pte))
1728 goto out_unlock;
1729
1730 /* Ok, finally just insert the thing.. */
1731 get_page(page);
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001732 inc_mm_counter_fast(mm, mm_counter_file(page));
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07001733 page_add_file_rmap(page, false);
Linus Torvalds238f58d2005-11-29 13:01:56 -08001734 set_pte_at(mm, addr, pte, mk_pte(page, prot));
1735
1736 retval = 0;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001737 pte_unmap_unlock(pte, ptl);
1738 return retval;
Linus Torvalds238f58d2005-11-29 13:01:56 -08001739out_unlock:
1740 pte_unmap_unlock(pte, ptl);
1741out:
1742 return retval;
1743}
1744
Rolf Eike Beerbfa5bf62006-09-25 23:31:22 -07001745/**
1746 * vm_insert_page - insert single page into user vma
1747 * @vma: user vma to map to
1748 * @addr: target user address of this page
1749 * @page: source kernel page
1750 *
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001751 * This allows drivers to insert individual pages they've allocated
1752 * into a user vma.
1753 *
1754 * The page has to be a nice clean _individual_ kernel allocation.
1755 * If you allocate a compound page, you need to have marked it as
1756 * such (__GFP_COMP), or manually just split the page up yourself
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001757 * (see split_page()).
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001758 *
1759 * NOTE! Traditionally this was done with "remap_pfn_range()" which
1760 * took an arbitrary page protection parameter. This doesn't allow
1761 * that. Your vma protection will have to be set up correctly, which
1762 * means that if you want a shared writable mapping, you'd better
1763 * ask for a shared writable mapping!
1764 *
1765 * The page does not need to be reserved.
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001766 *
1767 * Usually this function is called from f_op->mmap() handler
1768 * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
1769 * Caller must set VM_MIXEDMAP on vma if it wants to call this
1770 * function from other places, for example from page-fault handler.
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001771 */
Nick Piggin423bad602008-04-28 02:13:01 -07001772int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
1773 struct page *page)
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001774{
1775 if (addr < vma->vm_start || addr >= vma->vm_end)
1776 return -EFAULT;
1777 if (!page_count(page))
1778 return -EINVAL;
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001779 if (!(vma->vm_flags & VM_MIXEDMAP)) {
1780 BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem));
1781 BUG_ON(vma->vm_flags & VM_PFNMAP);
1782 vma->vm_flags |= VM_MIXEDMAP;
1783 }
Nick Piggin423bad602008-04-28 02:13:01 -07001784 return insert_page(vma, addr, page, vma->vm_page_prot);
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001785}
Linus Torvaldse3c33742005-12-03 20:48:11 -08001786EXPORT_SYMBOL(vm_insert_page);
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001787
Nick Piggin423bad602008-04-28 02:13:01 -07001788static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
Ross Zwislerb2770da62017-09-06 16:18:35 -07001789 pfn_t pfn, pgprot_t prot, bool mkwrite)
Nick Piggin423bad602008-04-28 02:13:01 -07001790{
1791 struct mm_struct *mm = vma->vm_mm;
1792 int retval;
1793 pte_t *pte, entry;
1794 spinlock_t *ptl;
1795
1796 retval = -ENOMEM;
1797 pte = get_locked_pte(mm, addr, &ptl);
1798 if (!pte)
1799 goto out;
1800 retval = -EBUSY;
Ross Zwislerb2770da62017-09-06 16:18:35 -07001801 if (!pte_none(*pte)) {
1802 if (mkwrite) {
1803 /*
1804 * For read faults on private mappings the PFN passed
1805 * in may not match the PFN we have mapped if the
1806 * mapped PFN is a writeable COW page. In the mkwrite
1807 * case we are creating a writable PTE for a shared
Jan Kara423497a2018-10-30 15:10:47 -07001808 * mapping and we expect the PFNs to match. If they
1809 * don't match, we are likely racing with block
1810 * allocation and mapping invalidation so just skip the
1811 * update.
Ross Zwislerb2770da62017-09-06 16:18:35 -07001812 */
Jan Kara423497a2018-10-30 15:10:47 -07001813 if (pte_pfn(*pte) != pfn_t_to_pfn(pfn)) {
1814 WARN_ON_ONCE(!is_zero_pfn(pte_pfn(*pte)));
Ross Zwislerb2770da62017-09-06 16:18:35 -07001815 goto out_unlock;
Jan Kara423497a2018-10-30 15:10:47 -07001816 }
Jan Kara68321992019-03-28 20:43:19 -07001817 entry = pte_mkyoung(*pte);
1818 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1819 if (ptep_set_access_flags(vma, addr, pte, entry, 1))
1820 update_mmu_cache(vma, addr, pte);
1821 }
1822 goto out_unlock;
Ross Zwislerb2770da62017-09-06 16:18:35 -07001823 }
Nick Piggin423bad602008-04-28 02:13:01 -07001824
1825 /* Ok, finally just insert the thing.. */
Dan Williams01c8f1c2016-01-15 16:56:40 -08001826 if (pfn_t_devmap(pfn))
1827 entry = pte_mkdevmap(pfn_t_pte(pfn, prot));
1828 else
1829 entry = pte_mkspecial(pfn_t_pte(pfn, prot));
Ross Zwislerb2770da62017-09-06 16:18:35 -07001830
Ross Zwislerb2770da62017-09-06 16:18:35 -07001831 if (mkwrite) {
1832 entry = pte_mkyoung(entry);
1833 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1834 }
1835
Nick Piggin423bad602008-04-28 02:13:01 -07001836 set_pte_at(mm, addr, pte, entry);
Russell King4b3073e2009-12-18 16:40:18 +00001837 update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
Nick Piggin423bad602008-04-28 02:13:01 -07001838
1839 retval = 0;
1840out_unlock:
1841 pte_unmap_unlock(pte, ptl);
1842out:
1843 return retval;
1844}
1845
Nick Piggine0dc0d82007-02-12 00:51:36 -08001846/**
1847 * vm_insert_pfn - insert single pfn into user vma
1848 * @vma: user vma to map to
1849 * @addr: target user address of this page
1850 * @pfn: source kernel pfn
1851 *
Robert P. J. Dayc462f172012-10-08 16:33:43 -07001852 * Similar to vm_insert_page, this allows drivers to insert individual pages
Nick Piggine0dc0d82007-02-12 00:51:36 -08001853 * they've allocated into a user vma. Same comments apply.
1854 *
1855 * This function should only be called from a vm_ops->fault handler, and
1856 * in that case the handler should return NULL.
Nick Piggin0d71d10a2008-07-23 21:27:05 -07001857 *
1858 * vma cannot be a COW mapping.
1859 *
1860 * As this is called only for pages that do not currently exist, we
1861 * do not need to flush old virtual caches or the TLB.
Nick Piggine0dc0d82007-02-12 00:51:36 -08001862 */
1863int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
Nick Piggin423bad602008-04-28 02:13:01 -07001864 unsigned long pfn)
Nick Piggine0dc0d82007-02-12 00:51:36 -08001865{
Andy Lutomirski1745cbc2015-12-29 20:12:20 -08001866 return vm_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot);
1867}
1868EXPORT_SYMBOL(vm_insert_pfn);
1869
1870/**
1871 * vm_insert_pfn_prot - insert single pfn into user vma with specified pgprot
1872 * @vma: user vma to map to
1873 * @addr: target user address of this page
1874 * @pfn: source kernel pfn
1875 * @pgprot: pgprot flags for the inserted page
1876 *
1877 * This is exactly like vm_insert_pfn, except that it allows drivers to
1878 * to override pgprot on a per-page basis.
1879 *
1880 * This only makes sense for IO mappings, and it makes no sense for
1881 * cow mappings. In general, using multiple vmas is preferable;
1882 * vm_insert_pfn_prot should only be used if using multiple VMAs is
1883 * impractical.
1884 */
1885int vm_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
1886 unsigned long pfn, pgprot_t pgprot)
1887{
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001888 int ret;
Nick Piggin7e675132008-04-28 02:13:00 -07001889 /*
1890 * Technically, architectures with pte_special can avoid all these
1891 * restrictions (same for remap_pfn_range). However we would like
1892 * consistency in testing and feature parity among all, so we should
1893 * try to keep these invariants in place for everybody.
1894 */
Jared Hulbertb379d792008-04-28 02:12:58 -07001895 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
1896 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
1897 (VM_PFNMAP|VM_MIXEDMAP));
1898 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
1899 BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
Nick Piggine0dc0d82007-02-12 00:51:36 -08001900
Nick Piggin423bad602008-04-28 02:13:01 -07001901 if (addr < vma->vm_start || addr >= vma->vm_end)
1902 return -EFAULT;
Borislav Petkov308a0472016-10-26 19:43:43 +02001903
Andi Kleen42e40892018-06-13 15:48:27 -07001904 if (!pfn_modify_allowed(pfn, pgprot))
1905 return -EACCES;
1906
Borislav Petkov308a0472016-10-26 19:43:43 +02001907 track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV));
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001908
Ross Zwislerb2770da62017-09-06 16:18:35 -07001909 ret = insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot,
1910 false);
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001911
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001912 return ret;
Nick Piggine0dc0d82007-02-12 00:51:36 -08001913}
Andy Lutomirski1745cbc2015-12-29 20:12:20 -08001914EXPORT_SYMBOL(vm_insert_pfn_prot);
Nick Piggine0dc0d82007-02-12 00:51:36 -08001915
Dan Williams785a3fa2017-10-23 07:20:00 -07001916static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn)
1917{
1918 /* these checks mirror the abort conditions in vm_normal_page */
1919 if (vma->vm_flags & VM_MIXEDMAP)
1920 return true;
1921 if (pfn_t_devmap(pfn))
1922 return true;
1923 if (pfn_t_special(pfn))
1924 return true;
1925 if (is_zero_pfn(pfn_t_to_pfn(pfn)))
1926 return true;
1927 return false;
1928}
1929
Ross Zwislerb2770da62017-09-06 16:18:35 -07001930static int __vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
1931 pfn_t pfn, bool mkwrite)
Nick Piggin423bad602008-04-28 02:13:01 -07001932{
Dan Williams87744ab2016-10-07 17:00:18 -07001933 pgprot_t pgprot = vma->vm_page_prot;
1934
Dan Williams785a3fa2017-10-23 07:20:00 -07001935 BUG_ON(!vm_mixed_ok(vma, pfn));
Nick Piggin423bad602008-04-28 02:13:01 -07001936
1937 if (addr < vma->vm_start || addr >= vma->vm_end)
1938 return -EFAULT;
Borislav Petkov308a0472016-10-26 19:43:43 +02001939
1940 track_pfn_insert(vma, &pgprot, pfn);
Nick Piggin423bad602008-04-28 02:13:01 -07001941
Andi Kleen42e40892018-06-13 15:48:27 -07001942 if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot))
1943 return -EACCES;
1944
Nick Piggin423bad602008-04-28 02:13:01 -07001945 /*
1946 * If we don't have pte special, then we have to use the pfn_valid()
1947 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
1948 * refcount the page if pfn_valid is true (hence insert_page rather
Hugh Dickins62eede62009-09-21 17:03:34 -07001949 * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP
1950 * without pte special, it would there be refcounted as a normal page.
Nick Piggin423bad602008-04-28 02:13:01 -07001951 */
Laurent Dufour00b3a332018-06-07 17:06:12 -07001952 if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) &&
1953 !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) {
Nick Piggin423bad602008-04-28 02:13:01 -07001954 struct page *page;
1955
Dan Williams03fc2da2016-01-26 09:48:05 -08001956 /*
1957 * At this point we are committed to insert_page()
1958 * regardless of whether the caller specified flags that
1959 * result in pfn_t_has_page() == false.
1960 */
1961 page = pfn_to_page(pfn_t_to_pfn(pfn));
Dan Williams87744ab2016-10-07 17:00:18 -07001962 return insert_page(vma, addr, page, pgprot);
Nick Piggin423bad602008-04-28 02:13:01 -07001963 }
Ross Zwislerb2770da62017-09-06 16:18:35 -07001964 return insert_pfn(vma, addr, pfn, pgprot, mkwrite);
1965}
1966
1967int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
1968 pfn_t pfn)
1969{
1970 return __vm_insert_mixed(vma, addr, pfn, false);
1971
Nick Piggin423bad602008-04-28 02:13:01 -07001972}
1973EXPORT_SYMBOL(vm_insert_mixed);
1974
Souptick Joarderab77dab2018-06-07 17:04:29 -07001975/*
1976 * If the insertion of PTE failed because someone else already added a
1977 * different entry in the mean time, we treat that as success as we assume
1978 * the same entry was actually inserted.
1979 */
1980
1981vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma,
1982 unsigned long addr, pfn_t pfn)
Ross Zwislerb2770da62017-09-06 16:18:35 -07001983{
Souptick Joarderab77dab2018-06-07 17:04:29 -07001984 int err;
1985
1986 err = __vm_insert_mixed(vma, addr, pfn, true);
1987 if (err == -ENOMEM)
1988 return VM_FAULT_OOM;
1989 if (err < 0 && err != -EBUSY)
1990 return VM_FAULT_SIGBUS;
1991 return VM_FAULT_NOPAGE;
Ross Zwislerb2770da62017-09-06 16:18:35 -07001992}
Souptick Joarderab77dab2018-06-07 17:04:29 -07001993EXPORT_SYMBOL(vmf_insert_mixed_mkwrite);
Ross Zwislerb2770da62017-09-06 16:18:35 -07001994
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001995/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 * maps a range of physical memory into the requested pages. the old
1997 * mappings are removed. any references to nonexistent pages results
1998 * in null mappings (currently treated as "copy-on-access")
1999 */
2000static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
2001 unsigned long addr, unsigned long end,
2002 unsigned long pfn, pgprot_t prot)
2003{
2004 pte_t *pte;
Hugh Dickinsc74df322005-10-29 18:16:23 -07002005 spinlock_t *ptl;
Andi Kleen42e40892018-06-13 15:48:27 -07002006 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Hugh Dickinsc74df322005-10-29 18:16:23 -07002008 pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 if (!pte)
2010 return -ENOMEM;
Zachary Amsden6606c3e2006-09-30 23:29:33 -07002011 arch_enter_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 do {
2013 BUG_ON(!pte_none(*pte));
Andi Kleen42e40892018-06-13 15:48:27 -07002014 if (!pfn_modify_allowed(pfn, prot)) {
2015 err = -EACCES;
2016 break;
2017 }
Nick Piggin7e675132008-04-28 02:13:00 -07002018 set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 pfn++;
2020 } while (pte++, addr += PAGE_SIZE, addr != end);
Zachary Amsden6606c3e2006-09-30 23:29:33 -07002021 arch_leave_lazy_mmu_mode();
Hugh Dickinsc74df322005-10-29 18:16:23 -07002022 pte_unmap_unlock(pte - 1, ptl);
Andi Kleen42e40892018-06-13 15:48:27 -07002023 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
2026static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
2027 unsigned long addr, unsigned long end,
2028 unsigned long pfn, pgprot_t prot)
2029{
2030 pmd_t *pmd;
2031 unsigned long next;
Andi Kleen42e40892018-06-13 15:48:27 -07002032 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 pfn -= addr >> PAGE_SHIFT;
2035 pmd = pmd_alloc(mm, pud, addr);
2036 if (!pmd)
2037 return -ENOMEM;
Andrea Arcangelif66055ab2011-01-13 15:46:54 -08002038 VM_BUG_ON(pmd_trans_huge(*pmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 do {
2040 next = pmd_addr_end(addr, end);
Andi Kleen42e40892018-06-13 15:48:27 -07002041 err = remap_pte_range(mm, pmd, addr, next,
2042 pfn + (addr >> PAGE_SHIFT), prot);
2043 if (err)
2044 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 } while (pmd++, addr = next, addr != end);
2046 return 0;
2047}
2048
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002049static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 unsigned long addr, unsigned long end,
2051 unsigned long pfn, pgprot_t prot)
2052{
2053 pud_t *pud;
2054 unsigned long next;
Andi Kleen42e40892018-06-13 15:48:27 -07002055 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 pfn -= addr >> PAGE_SHIFT;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002058 pud = pud_alloc(mm, p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (!pud)
2060 return -ENOMEM;
2061 do {
2062 next = pud_addr_end(addr, end);
Andi Kleen42e40892018-06-13 15:48:27 -07002063 err = remap_pmd_range(mm, pud, addr, next,
2064 pfn + (addr >> PAGE_SHIFT), prot);
2065 if (err)
2066 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 } while (pud++, addr = next, addr != end);
2068 return 0;
2069}
2070
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002071static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd,
2072 unsigned long addr, unsigned long end,
2073 unsigned long pfn, pgprot_t prot)
2074{
2075 p4d_t *p4d;
2076 unsigned long next;
Andi Kleen42e40892018-06-13 15:48:27 -07002077 int err;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002078
2079 pfn -= addr >> PAGE_SHIFT;
2080 p4d = p4d_alloc(mm, pgd, addr);
2081 if (!p4d)
2082 return -ENOMEM;
2083 do {
2084 next = p4d_addr_end(addr, end);
Andi Kleen42e40892018-06-13 15:48:27 -07002085 err = remap_pud_range(mm, p4d, addr, next,
2086 pfn + (addr >> PAGE_SHIFT), prot);
2087 if (err)
2088 return err;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002089 } while (p4d++, addr = next, addr != end);
2090 return 0;
2091}
2092
Rolf Eike Beerbfa5bf62006-09-25 23:31:22 -07002093/**
2094 * remap_pfn_range - remap kernel memory to userspace
2095 * @vma: user vma to map to
2096 * @addr: target user address to start at
2097 * @pfn: physical address of kernel memory
2098 * @size: size of map area
2099 * @prot: page protection flags for this mapping
2100 *
2101 * Note: this is only safe if the mm semaphore is held when called.
2102 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
2104 unsigned long pfn, unsigned long size, pgprot_t prot)
2105{
2106 pgd_t *pgd;
2107 unsigned long next;
Hugh Dickins2d15cab2005-06-25 14:54:33 -07002108 unsigned long end = addr + PAGE_ALIGN(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 struct mm_struct *mm = vma->vm_mm;
Yongji Xied5957d22016-05-20 16:57:41 -07002110 unsigned long remap_pfn = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 int err;
2112
2113 /*
2114 * Physically remapped pages are special. Tell the
2115 * rest of the world about it:
2116 * VM_IO tells people not to look at these pages
2117 * (accesses can have side effects).
Linus Torvalds6aab3412005-11-28 14:34:23 -08002118 * VM_PFNMAP tells the core MM that the base pages are just
2119 * raw PFN mappings, and do not have a "struct page" associated
2120 * with them.
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07002121 * VM_DONTEXPAND
2122 * Disable vma merging and expanding with mremap().
2123 * VM_DONTDUMP
2124 * Omit vma from core dump, even when VM_IO turned off.
Linus Torvaldsfb155c12005-12-11 19:46:02 -08002125 *
2126 * There's a horrible special case to handle copy-on-write
2127 * behaviour that some programs depend on. We mark the "original"
2128 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07002129 * See vm_normal_page() for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 */
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07002131 if (is_cow_mapping(vma->vm_flags)) {
2132 if (addr != vma->vm_start || end != vma->vm_end)
2133 return -EINVAL;
Linus Torvaldsfb155c12005-12-11 19:46:02 -08002134 vma->vm_pgoff = pfn;
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07002135 }
2136
Yongji Xied5957d22016-05-20 16:57:41 -07002137 err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size));
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07002138 if (err)
venkatesh.pallipadi@intel.com3c8bb732008-12-18 11:41:27 -08002139 return -EINVAL;
Linus Torvaldsfb155c12005-12-11 19:46:02 -08002140
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07002141 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 BUG_ON(addr >= end);
2144 pfn -= addr >> PAGE_SHIFT;
2145 pgd = pgd_offset(mm, addr);
2146 flush_cache_range(vma, addr, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 do {
2148 next = pgd_addr_end(addr, end);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002149 err = remap_p4d_range(mm, pgd, addr, next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 pfn + (addr >> PAGE_SHIFT), prot);
2151 if (err)
2152 break;
2153 } while (pgd++, addr = next, addr != end);
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08002154
2155 if (err)
Yongji Xied5957d22016-05-20 16:57:41 -07002156 untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size));
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08002157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 return err;
2159}
2160EXPORT_SYMBOL(remap_pfn_range);
2161
Linus Torvaldsb4cbb192013-04-16 13:45:37 -07002162/**
2163 * vm_iomap_memory - remap memory to userspace
2164 * @vma: user vma to map to
2165 * @start: start of area
2166 * @len: size of area
2167 *
2168 * This is a simplified io_remap_pfn_range() for common driver use. The
2169 * driver just needs to give us the physical memory range to be mapped,
2170 * we'll figure out the rest from the vma information.
2171 *
2172 * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
2173 * whatever write-combining details or similar.
2174 */
2175int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
2176{
2177 unsigned long vm_len, pfn, pages;
2178
2179 /* Check that the physical memory area passed in looks valid */
2180 if (start + len < start)
2181 return -EINVAL;
2182 /*
2183 * You *really* shouldn't map things that aren't page-aligned,
2184 * but we've historically allowed it because IO memory might
2185 * just have smaller alignment.
2186 */
2187 len += start & ~PAGE_MASK;
2188 pfn = start >> PAGE_SHIFT;
2189 pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
2190 if (pfn + pages < pfn)
2191 return -EINVAL;
2192
2193 /* We start the mapping 'vm_pgoff' pages into the area */
2194 if (vma->vm_pgoff > pages)
2195 return -EINVAL;
2196 pfn += vma->vm_pgoff;
2197 pages -= vma->vm_pgoff;
2198
2199 /* Can we fit all of the mapping? */
2200 vm_len = vma->vm_end - vma->vm_start;
2201 if (vm_len >> PAGE_SHIFT > pages)
2202 return -EINVAL;
2203
2204 /* Ok, let it rip */
2205 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
2206}
2207EXPORT_SYMBOL(vm_iomap_memory);
2208
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002209static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
2210 unsigned long addr, unsigned long end,
2211 pte_fn_t fn, void *data)
2212{
2213 pte_t *pte;
2214 int err;
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08002215 pgtable_t token;
Borislav Petkov94909912007-05-06 14:49:17 -07002216 spinlock_t *uninitialized_var(ptl);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002217
2218 pte = (mm == &init_mm) ?
2219 pte_alloc_kernel(pmd, addr) :
2220 pte_alloc_map_lock(mm, pmd, addr, &ptl);
2221 if (!pte)
2222 return -ENOMEM;
2223
2224 BUG_ON(pmd_huge(*pmd));
2225
Jeremy Fitzhardinge38e0edb2009-01-06 14:39:21 -08002226 arch_enter_lazy_mmu_mode();
2227
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08002228 token = pmd_pgtable(*pmd);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002229
2230 do {
Daisuke Nishimurac36987e2009-10-26 16:50:23 -07002231 err = fn(pte++, token, addr, data);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002232 if (err)
2233 break;
Daisuke Nishimurac36987e2009-10-26 16:50:23 -07002234 } while (addr += PAGE_SIZE, addr != end);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002235
Jeremy Fitzhardinge38e0edb2009-01-06 14:39:21 -08002236 arch_leave_lazy_mmu_mode();
2237
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002238 if (mm != &init_mm)
2239 pte_unmap_unlock(pte-1, ptl);
2240 return err;
2241}
2242
2243static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
2244 unsigned long addr, unsigned long end,
2245 pte_fn_t fn, void *data)
2246{
2247 pmd_t *pmd;
2248 unsigned long next;
2249 int err;
2250
Andi Kleenceb86872008-07-23 21:27:50 -07002251 BUG_ON(pud_huge(*pud));
2252
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002253 pmd = pmd_alloc(mm, pud, addr);
2254 if (!pmd)
2255 return -ENOMEM;
2256 do {
2257 next = pmd_addr_end(addr, end);
2258 err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
2259 if (err)
2260 break;
2261 } while (pmd++, addr = next, addr != end);
2262 return err;
2263}
2264
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002265static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d,
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002266 unsigned long addr, unsigned long end,
2267 pte_fn_t fn, void *data)
2268{
2269 pud_t *pud;
2270 unsigned long next;
2271 int err;
2272
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002273 pud = pud_alloc(mm, p4d, addr);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002274 if (!pud)
2275 return -ENOMEM;
2276 do {
2277 next = pud_addr_end(addr, end);
2278 err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
2279 if (err)
2280 break;
2281 } while (pud++, addr = next, addr != end);
2282 return err;
2283}
2284
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002285static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd,
2286 unsigned long addr, unsigned long end,
2287 pte_fn_t fn, void *data)
2288{
2289 p4d_t *p4d;
2290 unsigned long next;
2291 int err;
2292
2293 p4d = p4d_alloc(mm, pgd, addr);
2294 if (!p4d)
2295 return -ENOMEM;
2296 do {
2297 next = p4d_addr_end(addr, end);
2298 err = apply_to_pud_range(mm, p4d, addr, next, fn, data);
2299 if (err)
2300 break;
2301 } while (p4d++, addr = next, addr != end);
2302 return err;
2303}
2304
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002305/*
2306 * Scan a region of virtual memory, filling in page tables as necessary
2307 * and calling a provided function on each leaf page table.
2308 */
2309int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2310 unsigned long size, pte_fn_t fn, void *data)
2311{
2312 pgd_t *pgd;
2313 unsigned long next;
Jeremy Fitzhardinge57250a52010-08-09 17:19:52 -07002314 unsigned long end = addr + size;
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002315 int err;
2316
Mika Penttilä9cb65bc2016-03-15 14:56:45 -07002317 if (WARN_ON(addr >= end))
2318 return -EINVAL;
2319
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002320 pgd = pgd_offset(mm, addr);
2321 do {
2322 next = pgd_addr_end(addr, end);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002323 err = apply_to_p4d_range(mm, pgd, addr, next, fn, data);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002324 if (err)
2325 break;
2326 } while (pgd++, addr = next, addr != end);
Jeremy Fitzhardinge57250a52010-08-09 17:19:52 -07002327
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002328 return err;
2329}
2330EXPORT_SYMBOL_GPL(apply_to_page_range);
2331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332/*
Kirill A. Shutemov9b4bdd22015-02-10 14:09:51 -08002333 * handle_pte_fault chooses page fault handler according to an entry which was
2334 * read non-atomically. Before making any commitment, on those architectures
2335 * or configurations (e.g. i386 with PAE) which might give a mix of unmatched
2336 * parts, do_swap_page must check under lock before unmapping the pte and
2337 * proceeding (but do_wp_page is only called after already making such a check;
Ryota Ozakia335b2e2011-02-10 13:56:28 +09002338 * and do_anonymous_page can safely check later on).
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002339 */
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07002340static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002341 pte_t *page_table, pte_t orig_pte)
2342{
2343 int same = 1;
2344#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
2345 if (sizeof(pte_t) > sizeof(unsigned long)) {
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07002346 spinlock_t *ptl = pte_lockptr(mm, pmd);
2347 spin_lock(ptl);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002348 same = pte_same(*page_table, orig_pte);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07002349 spin_unlock(ptl);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002350 }
2351#endif
2352 pte_unmap(page_table);
2353 return same;
2354}
2355
Atsushi Nemoto9de455b2006-12-12 17:14:55 +00002356static 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 -08002357{
Dan Williams0abdd7a2014-01-21 15:48:12 -08002358 debug_dma_assert_idle(src);
2359
Linus Torvalds6aab3412005-11-28 14:34:23 -08002360 /*
2361 * If the source page was a PFN mapping, we don't have
2362 * a "struct page" for it. We do a best-effort copy by
2363 * just copying from the original user address. If that
2364 * fails, we just zero-fill it. Live with it.
2365 */
2366 if (unlikely(!src)) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08002367 void *kaddr = kmap_atomic(dst);
Linus Torvalds5d2a2dbbc2005-11-29 14:07:55 -08002368 void __user *uaddr = (void __user *)(va & PAGE_MASK);
2369
2370 /*
2371 * This really shouldn't fail, because the page is there
2372 * in the page tables. But it might just be unreadable,
2373 * in which case we just give up and fill the result with
2374 * zeroes.
2375 */
2376 if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
Jan Beulich3ecb01d2010-10-26 14:22:27 -07002377 clear_page(kaddr);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002378 kunmap_atomic(kaddr);
Dmitriy Monakhovc4ec7b02006-10-19 23:29:08 -07002379 flush_dcache_page(dst);
Nick Piggin0ed361d2008-02-04 22:29:34 -08002380 } else
2381 copy_user_highpage(dst, src, va, vma);
Linus Torvalds6aab3412005-11-28 14:34:23 -08002382}
2383
Michal Hockoc20cd452016-01-14 15:20:12 -08002384static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma)
2385{
2386 struct file *vm_file = vma->vm_file;
2387
2388 if (vm_file)
2389 return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO;
2390
2391 /*
2392 * Special mappings (e.g. VDSO) do not have any file so fake
2393 * a default GFP_KERNEL for them.
2394 */
2395 return GFP_KERNEL;
2396}
2397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398/*
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002399 * Notify the address space that the page is about to become writable so that
2400 * it can prohibit this or wait for the page to get into an appropriate state.
2401 *
2402 * We do this without the lock held, so that it can sleep if it needs to.
2403 */
Souptick Joarder2b740302018-08-23 17:01:36 -07002404static vm_fault_t do_page_mkwrite(struct vm_fault *vmf)
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002405{
Souptick Joarder2b740302018-08-23 17:01:36 -07002406 vm_fault_t ret;
Jan Kara38b8cb72016-12-14 15:07:30 -08002407 struct page *page = vmf->page;
2408 unsigned int old_flags = vmf->flags;
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002409
Jan Kara38b8cb72016-12-14 15:07:30 -08002410 vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002411
Darrick J. Wong8cfd90e2019-08-20 07:55:16 -07002412 if (vmf->vma->vm_file &&
2413 IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host))
2414 return VM_FAULT_SIGBUS;
2415
Dave Jiang11bac802017-02-24 14:56:41 -08002416 ret = vmf->vma->vm_ops->page_mkwrite(vmf);
Jan Kara38b8cb72016-12-14 15:07:30 -08002417 /* Restore original flags so that caller is not surprised */
2418 vmf->flags = old_flags;
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002419 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
2420 return ret;
2421 if (unlikely(!(ret & VM_FAULT_LOCKED))) {
2422 lock_page(page);
2423 if (!page->mapping) {
2424 unlock_page(page);
2425 return 0; /* retry */
2426 }
2427 ret |= VM_FAULT_LOCKED;
2428 } else
2429 VM_BUG_ON_PAGE(!PageLocked(page), page);
2430 return ret;
2431}
2432
2433/*
Jan Kara97ba0c22016-12-14 15:07:27 -08002434 * Handle dirtying of a page in shared file mapping on a write fault.
2435 *
2436 * The function expects the page to be locked and unlocks it.
2437 */
2438static void fault_dirty_shared_page(struct vm_area_struct *vma,
2439 struct page *page)
2440{
2441 struct address_space *mapping;
2442 bool dirtied;
2443 bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite;
2444
2445 dirtied = set_page_dirty(page);
2446 VM_BUG_ON_PAGE(PageAnon(page), page);
2447 /*
2448 * Take a local copy of the address_space - page.mapping may be zeroed
2449 * by truncate after unlock_page(). The address_space itself remains
2450 * pinned by vma->vm_file's reference. We rely on unlock_page()'s
2451 * release semantics to prevent the compiler from undoing this copying.
2452 */
2453 mapping = page_rmapping(page);
2454 unlock_page(page);
2455
2456 if ((dirtied || page_mkwrite) && mapping) {
2457 /*
2458 * Some device drivers do not set page.mapping
2459 * but still dirty their pages
2460 */
2461 balance_dirty_pages_ratelimited(mapping);
2462 }
2463
2464 if (!page_mkwrite)
2465 file_update_time(vma->vm_file);
2466}
2467
2468/*
Shachar Raindel4e047f82015-04-14 15:46:25 -07002469 * Handle write page faults for pages that can be reused in the current vma
2470 *
2471 * This can happen either due to the mapping being with the VM_SHARED flag,
2472 * or due to us being the last reference standing to the page. In either
2473 * case, all we need to do here is to mark the page as writable and update
2474 * any related book-keeping.
2475 */
Jan Kara997dd982016-12-14 15:07:36 -08002476static inline void wp_page_reuse(struct vm_fault *vmf)
Jan Kara82b0f8c2016-12-14 15:06:58 -08002477 __releases(vmf->ptl)
Shachar Raindel4e047f82015-04-14 15:46:25 -07002478{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002479 struct vm_area_struct *vma = vmf->vma;
Jan Karaa41b70d2016-12-14 15:07:33 -08002480 struct page *page = vmf->page;
Shachar Raindel4e047f82015-04-14 15:46:25 -07002481 pte_t entry;
2482 /*
2483 * Clear the pages cpupid information as the existing
2484 * information potentially belongs to a now completely
2485 * unrelated process.
2486 */
2487 if (page)
2488 page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1);
2489
Jan Kara29943022016-12-14 15:07:16 -08002490 flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
2491 entry = pte_mkyoung(vmf->orig_pte);
Shachar Raindel4e047f82015-04-14 15:46:25 -07002492 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Jan Kara82b0f8c2016-12-14 15:06:58 -08002493 if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1))
2494 update_mmu_cache(vma, vmf->address, vmf->pte);
2495 pte_unmap_unlock(vmf->pte, vmf->ptl);
Shachar Raindel4e047f82015-04-14 15:46:25 -07002496}
2497
2498/*
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002499 * Handle the case of a page which we actually need to copy to a new page.
2500 *
2501 * Called with mmap_sem locked and the old page referenced, but
2502 * without the ptl held.
2503 *
2504 * High level logic flow:
2505 *
2506 * - Allocate a page, copy the content of the old page to the new one.
2507 * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc.
2508 * - Take the PTL. If the pte changed, bail out and release the allocated page
2509 * - If the pte is still the way we remember it, update the page table and all
2510 * relevant references. This includes dropping the reference the page-table
2511 * held to the old page, as well as updating the rmap.
2512 * - In any case, unlock the PTL and drop the reference we took to the old page.
2513 */
Souptick Joarder2b740302018-08-23 17:01:36 -07002514static vm_fault_t wp_page_copy(struct vm_fault *vmf)
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002515{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002516 struct vm_area_struct *vma = vmf->vma;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002517 struct mm_struct *mm = vma->vm_mm;
Jan Karaa41b70d2016-12-14 15:07:33 -08002518 struct page *old_page = vmf->page;
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002519 struct page *new_page = NULL;
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002520 pte_t entry;
2521 int page_copied = 0;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002522 const unsigned long mmun_start = vmf->address & PAGE_MASK;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002523 const unsigned long mmun_end = mmun_start + PAGE_SIZE;
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002524 struct mem_cgroup *memcg;
2525
2526 if (unlikely(anon_vma_prepare(vma)))
2527 goto oom;
2528
Jan Kara29943022016-12-14 15:07:16 -08002529 if (is_zero_pfn(pte_pfn(vmf->orig_pte))) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08002530 new_page = alloc_zeroed_user_highpage_movable(vma,
2531 vmf->address);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002532 if (!new_page)
2533 goto oom;
2534 } else {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002535 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
Jan Kara82b0f8c2016-12-14 15:06:58 -08002536 vmf->address);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002537 if (!new_page)
2538 goto oom;
Jan Kara82b0f8c2016-12-14 15:06:58 -08002539 cow_user_page(new_page, old_page, vmf->address, vma);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002540 }
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002541
Tejun Heo2cf85582018-07-03 11:14:56 -04002542 if (mem_cgroup_try_charge_delay(new_page, mm, GFP_KERNEL, &memcg, false))
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002543 goto oom_free_new;
2544
Mel Gormaneb3c24f2015-06-24 16:57:27 -07002545 __SetPageUptodate(new_page);
2546
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002547 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
2548
2549 /*
2550 * Re-check the pte - we dropped the lock
2551 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002552 vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08002553 if (likely(pte_same(*vmf->pte, vmf->orig_pte))) {
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002554 if (old_page) {
2555 if (!PageAnon(old_page)) {
Jerome Marchandeca56ff2016-01-14 15:19:26 -08002556 dec_mm_counter_fast(mm,
2557 mm_counter_file(old_page));
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002558 inc_mm_counter_fast(mm, MM_ANONPAGES);
2559 }
2560 } else {
2561 inc_mm_counter_fast(mm, MM_ANONPAGES);
2562 }
Jan Kara29943022016-12-14 15:07:16 -08002563 flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002564 entry = mk_pte(new_page, vma->vm_page_prot);
2565 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2566 /*
2567 * Clear the pte entry and flush it first, before updating the
2568 * pte with the new entry. This will avoid a race condition
2569 * seen in the presence of one thread doing SMC and another
2570 * thread doing COW.
2571 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002572 ptep_clear_flush_notify(vma, vmf->address, vmf->pte);
2573 page_add_new_anon_rmap(new_page, vma, vmf->address, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002574 mem_cgroup_commit_charge(new_page, memcg, false, false);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002575 lru_cache_add_active_or_unevictable(new_page, vma);
2576 /*
2577 * We call the notify macro here because, when using secondary
2578 * mmu page tables (such as kvm shadow page tables), we want the
2579 * new page to be mapped directly into the secondary page table.
2580 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002581 set_pte_at_notify(mm, vmf->address, vmf->pte, entry);
2582 update_mmu_cache(vma, vmf->address, vmf->pte);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002583 if (old_page) {
2584 /*
2585 * Only after switching the pte to the new page may
2586 * we remove the mapcount here. Otherwise another
2587 * process may come and find the rmap count decremented
2588 * before the pte is switched to the new page, and
2589 * "reuse" the old page writing into it while our pte
2590 * here still points into it and can be read by other
2591 * threads.
2592 *
2593 * The critical issue is to order this
2594 * page_remove_rmap with the ptp_clear_flush above.
2595 * Those stores are ordered by (if nothing else,)
2596 * the barrier present in the atomic_add_negative
2597 * in page_remove_rmap.
2598 *
2599 * Then the TLB flush in ptep_clear_flush ensures that
2600 * no process can access the old page before the
2601 * decremented mapcount is visible. And the old page
2602 * cannot be reused until after the decremented
2603 * mapcount is visible. So transitively, TLBs to
2604 * old page will be flushed before it can be reused.
2605 */
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002606 page_remove_rmap(old_page, false);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002607 }
2608
2609 /* Free the old page.. */
2610 new_page = old_page;
2611 page_copied = 1;
2612 } else {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002613 mem_cgroup_cancel_charge(new_page, memcg, false);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002614 }
2615
2616 if (new_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002617 put_page(new_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002618
Jan Kara82b0f8c2016-12-14 15:06:58 -08002619 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jérôme Glisse4645b9f2017-11-15 17:34:11 -08002620 /*
2621 * No need to double call mmu_notifier->invalidate_range() callback as
2622 * the above ptep_clear_flush_notify() did already call it.
2623 */
2624 mmu_notifier_invalidate_range_only_end(mm, mmun_start, mmun_end);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002625 if (old_page) {
2626 /*
2627 * Don't let another task, with possibly unlocked vma,
2628 * keep the mlocked page.
2629 */
2630 if (page_copied && (vma->vm_flags & VM_LOCKED)) {
2631 lock_page(old_page); /* LRU manipulation */
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002632 if (PageMlocked(old_page))
2633 munlock_vma_page(old_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002634 unlock_page(old_page);
2635 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002636 put_page(old_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002637 }
2638 return page_copied ? VM_FAULT_WRITE : 0;
2639oom_free_new:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002640 put_page(new_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002641oom:
2642 if (old_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002643 put_page(old_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002644 return VM_FAULT_OOM;
2645}
2646
Jan Kara66a61972016-12-14 15:07:39 -08002647/**
2648 * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE
2649 * writeable once the page is prepared
2650 *
2651 * @vmf: structure describing the fault
2652 *
2653 * This function handles all that is needed to finish a write page fault in a
2654 * shared mapping due to PTE being read-only once the mapped page is prepared.
2655 * It handles locking of PTE and modifying it. The function returns
2656 * VM_FAULT_WRITE on success, 0 when PTE got changed before we acquired PTE
2657 * lock.
2658 *
2659 * The function expects the page to be locked or other protection against
2660 * concurrent faults / writeback (such as DAX radix tree locks).
2661 */
Souptick Joarder2b740302018-08-23 17:01:36 -07002662vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf)
Jan Kara66a61972016-12-14 15:07:39 -08002663{
2664 WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED));
2665 vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address,
2666 &vmf->ptl);
2667 /*
2668 * We might have raced with another page fault while we released the
2669 * pte_offset_map_lock.
2670 */
2671 if (!pte_same(*vmf->pte, vmf->orig_pte)) {
2672 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karaa19e2552016-12-14 15:07:42 -08002673 return VM_FAULT_NOPAGE;
Jan Kara66a61972016-12-14 15:07:39 -08002674 }
2675 wp_page_reuse(vmf);
Jan Karaa19e2552016-12-14 15:07:42 -08002676 return 0;
Jan Kara66a61972016-12-14 15:07:39 -08002677}
2678
Boaz Harroshdd906182015-04-15 16:15:11 -07002679/*
2680 * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED
2681 * mapping
2682 */
Souptick Joarder2b740302018-08-23 17:01:36 -07002683static vm_fault_t wp_pfn_shared(struct vm_fault *vmf)
Boaz Harroshdd906182015-04-15 16:15:11 -07002684{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002685 struct vm_area_struct *vma = vmf->vma;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002686
Boaz Harroshdd906182015-04-15 16:15:11 -07002687 if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) {
Souptick Joarder2b740302018-08-23 17:01:36 -07002688 vm_fault_t ret;
Boaz Harroshdd906182015-04-15 16:15:11 -07002689
Jan Kara82b0f8c2016-12-14 15:06:58 -08002690 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karafe822212016-12-14 15:07:13 -08002691 vmf->flags |= FAULT_FLAG_MKWRITE;
Dave Jiang11bac802017-02-24 14:56:41 -08002692 ret = vma->vm_ops->pfn_mkwrite(vmf);
Jan Kara2f89dc12016-12-14 15:07:50 -08002693 if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))
Boaz Harroshdd906182015-04-15 16:15:11 -07002694 return ret;
Jan Kara66a61972016-12-14 15:07:39 -08002695 return finish_mkwrite_fault(vmf);
Boaz Harroshdd906182015-04-15 16:15:11 -07002696 }
Jan Kara997dd982016-12-14 15:07:36 -08002697 wp_page_reuse(vmf);
2698 return VM_FAULT_WRITE;
Boaz Harroshdd906182015-04-15 16:15:11 -07002699}
2700
Souptick Joarder2b740302018-08-23 17:01:36 -07002701static vm_fault_t wp_page_shared(struct vm_fault *vmf)
Jan Kara82b0f8c2016-12-14 15:06:58 -08002702 __releases(vmf->ptl)
Shachar Raindel93e478d2015-04-14 15:46:35 -07002703{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002704 struct vm_area_struct *vma = vmf->vma;
Shachar Raindel93e478d2015-04-14 15:46:35 -07002705
Jan Karaa41b70d2016-12-14 15:07:33 -08002706 get_page(vmf->page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07002707
Shachar Raindel93e478d2015-04-14 15:46:35 -07002708 if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
Souptick Joarder2b740302018-08-23 17:01:36 -07002709 vm_fault_t tmp;
Shachar Raindel93e478d2015-04-14 15:46:35 -07002710
Jan Kara82b0f8c2016-12-14 15:06:58 -08002711 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Kara38b8cb72016-12-14 15:07:30 -08002712 tmp = do_page_mkwrite(vmf);
Shachar Raindel93e478d2015-04-14 15:46:35 -07002713 if (unlikely(!tmp || (tmp &
2714 (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
Jan Karaa41b70d2016-12-14 15:07:33 -08002715 put_page(vmf->page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07002716 return tmp;
2717 }
Jan Kara66a61972016-12-14 15:07:39 -08002718 tmp = finish_mkwrite_fault(vmf);
Jan Karaa19e2552016-12-14 15:07:42 -08002719 if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
Jan Karaa41b70d2016-12-14 15:07:33 -08002720 unlock_page(vmf->page);
Jan Karaa41b70d2016-12-14 15:07:33 -08002721 put_page(vmf->page);
Jan Kara66a61972016-12-14 15:07:39 -08002722 return tmp;
Shachar Raindel93e478d2015-04-14 15:46:35 -07002723 }
Jan Kara66a61972016-12-14 15:07:39 -08002724 } else {
2725 wp_page_reuse(vmf);
Jan Kara997dd982016-12-14 15:07:36 -08002726 lock_page(vmf->page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07002727 }
Jan Kara997dd982016-12-14 15:07:36 -08002728 fault_dirty_shared_page(vma, vmf->page);
2729 put_page(vmf->page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07002730
Jan Kara997dd982016-12-14 15:07:36 -08002731 return VM_FAULT_WRITE;
Shachar Raindel93e478d2015-04-14 15:46:35 -07002732}
2733
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002734/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 * This routine handles present pages, when users try to write
2736 * to a shared page. It is done by copying the page to a new address
2737 * and decrementing the shared-page counter for the old page.
2738 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 * Note that this routine assumes that the protection checks have been
2740 * done by the caller (the low-level page fault routine in most cases).
2741 * Thus we can safely just mark it writable once we've done any necessary
2742 * COW.
2743 *
2744 * We also mark the page dirty at this point even though the page will
2745 * change only once the write actually happens. This avoids a few races,
2746 * and potentially makes it more efficient.
2747 *
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002748 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2749 * but allow concurrent faults), with pte both mapped and locked.
2750 * We return with mmap_sem still held, but pte unmapped and unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 */
Souptick Joarder2b740302018-08-23 17:01:36 -07002752static vm_fault_t do_wp_page(struct vm_fault *vmf)
Jan Kara82b0f8c2016-12-14 15:06:58 -08002753 __releases(vmf->ptl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002755 struct vm_area_struct *vma = vmf->vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Jan Karaa41b70d2016-12-14 15:07:33 -08002757 vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte);
2758 if (!vmf->page) {
Peter Zijlstra251b97f2008-07-04 09:59:24 -07002759 /*
Peter Feiner64e45502014-10-13 15:55:46 -07002760 * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
2761 * VM_PFNMAP VMA.
Peter Zijlstra251b97f2008-07-04 09:59:24 -07002762 *
2763 * We should not cow pages in a shared writeable mapping.
Boaz Harroshdd906182015-04-15 16:15:11 -07002764 * Just mark the pages writable and/or call ops->pfn_mkwrite.
Peter Zijlstra251b97f2008-07-04 09:59:24 -07002765 */
2766 if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2767 (VM_WRITE|VM_SHARED))
Jan Kara29943022016-12-14 15:07:16 -08002768 return wp_pfn_shared(vmf);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002769
Jan Kara82b0f8c2016-12-14 15:06:58 -08002770 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karaa41b70d2016-12-14 15:07:33 -08002771 return wp_page_copy(vmf);
Peter Zijlstra251b97f2008-07-04 09:59:24 -07002772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Peter Zijlstrad08b3852006-09-25 23:30:57 -07002774 /*
Peter Zijlstraee6a6452006-09-25 23:31:00 -07002775 * Take out anonymous pages first, anonymous shared vmas are
2776 * not dirty accountable.
Peter Zijlstrad08b3852006-09-25 23:30:57 -07002777 */
Jan Karaa41b70d2016-12-14 15:07:33 -08002778 if (PageAnon(vmf->page) && !PageKsm(vmf->page)) {
Huang Yingba3c4ce2017-09-06 16:22:19 -07002779 int total_map_swapcount;
Jan Karaa41b70d2016-12-14 15:07:33 -08002780 if (!trylock_page(vmf->page)) {
2781 get_page(vmf->page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08002782 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karaa41b70d2016-12-14 15:07:33 -08002783 lock_page(vmf->page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08002784 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
2785 vmf->address, &vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08002786 if (!pte_same(*vmf->pte, vmf->orig_pte)) {
Jan Karaa41b70d2016-12-14 15:07:33 -08002787 unlock_page(vmf->page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08002788 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karaa41b70d2016-12-14 15:07:33 -08002789 put_page(vmf->page);
Shachar Raindel28766802015-04-14 15:46:29 -07002790 return 0;
Hugh Dickinsab967d82009-01-06 14:39:33 -08002791 }
Jan Karaa41b70d2016-12-14 15:07:33 -08002792 put_page(vmf->page);
Peter Zijlstraee6a6452006-09-25 23:31:00 -07002793 }
Huang Yingba3c4ce2017-09-06 16:22:19 -07002794 if (reuse_swap_page(vmf->page, &total_map_swapcount)) {
2795 if (total_map_swapcount == 1) {
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002796 /*
2797 * The page is all ours. Move it to
2798 * our anon_vma so the rmap code will
2799 * not search our parent or siblings.
2800 * Protected against the rmap code by
2801 * the page lock.
2802 */
Jan Karaa41b70d2016-12-14 15:07:33 -08002803 page_move_anon_rmap(vmf->page, vma);
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002804 }
Jan Karaa41b70d2016-12-14 15:07:33 -08002805 unlock_page(vmf->page);
Jan Kara997dd982016-12-14 15:07:36 -08002806 wp_page_reuse(vmf);
2807 return VM_FAULT_WRITE;
Michel Lespinasseb009c022011-01-13 15:46:07 -08002808 }
Jan Karaa41b70d2016-12-14 15:07:33 -08002809 unlock_page(vmf->page);
Peter Zijlstraee6a6452006-09-25 23:31:00 -07002810 } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
Peter Zijlstrad08b3852006-09-25 23:30:57 -07002811 (VM_WRITE|VM_SHARED))) {
Jan Karaa41b70d2016-12-14 15:07:33 -08002812 return wp_page_shared(vmf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
2815 /*
2816 * Ok, we need to copy. Oh, well..
2817 */
Jan Karaa41b70d2016-12-14 15:07:33 -08002818 get_page(vmf->page);
Shachar Raindel28766802015-04-14 15:46:29 -07002819
Jan Kara82b0f8c2016-12-14 15:06:58 -08002820 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karaa41b70d2016-12-14 15:07:33 -08002821 return wp_page_copy(vmf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822}
2823
Peter Zijlstra97a89412011-05-24 17:12:04 -07002824static void unmap_mapping_range_vma(struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 unsigned long start_addr, unsigned long end_addr,
2826 struct zap_details *details)
2827{
Al Virof5cc4ee2012-03-05 14:14:20 -05002828 zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829}
2830
Davidlohr Buesof808c132017-09-08 16:15:08 -07002831static inline void unmap_mapping_range_tree(struct rb_root_cached *root,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 struct zap_details *details)
2833{
2834 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 pgoff_t vba, vea, zba, zea;
2836
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002837 vma_interval_tree_foreach(vma, root,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 details->first_index, details->last_index) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
2840 vba = vma->vm_pgoff;
Libind6e93212013-07-03 15:01:26 -07002841 vea = vba + vma_pages(vma) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 zba = details->first_index;
2843 if (zba < vba)
2844 zba = vba;
2845 zea = details->last_index;
2846 if (zea > vea)
2847 zea = vea;
2848
Peter Zijlstra97a89412011-05-24 17:12:04 -07002849 unmap_mapping_range_vma(vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
2851 ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
Peter Zijlstra97a89412011-05-24 17:12:04 -07002852 details);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 }
2854}
2855
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856/**
Matthew Wilcox977fbdc2018-01-31 16:17:36 -08002857 * unmap_mapping_pages() - Unmap pages from processes.
2858 * @mapping: The address space containing pages to be unmapped.
2859 * @start: Index of first page to be unmapped.
2860 * @nr: Number of pages to be unmapped. 0 to unmap to end of file.
2861 * @even_cows: Whether to unmap even private COWed pages.
2862 *
2863 * Unmap the pages in this address space from any userspace process which
2864 * has them mmaped. Generally, you want to remove COWed pages as well when
2865 * a file is being truncated, but not when invalidating pages from the page
2866 * cache.
2867 */
2868void unmap_mapping_pages(struct address_space *mapping, pgoff_t start,
2869 pgoff_t nr, bool even_cows)
2870{
2871 struct zap_details details = { };
2872
2873 details.check_mapping = even_cows ? NULL : mapping;
2874 details.first_index = start;
2875 details.last_index = start + nr - 1;
2876 if (details.last_index < details.first_index)
2877 details.last_index = ULONG_MAX;
2878
2879 i_mmap_lock_write(mapping);
2880 if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)))
2881 unmap_mapping_range_tree(&mapping->i_mmap, &details);
2882 i_mmap_unlock_write(mapping);
2883}
2884
2885/**
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08002886 * unmap_mapping_range - unmap the portion of all mmaps in the specified
Matthew Wilcox977fbdc2018-01-31 16:17:36 -08002887 * address_space corresponding to the specified byte range in the underlying
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08002888 * file.
2889 *
Martin Waitz3d410882005-06-23 22:05:21 -07002890 * @mapping: the address space containing mmaps to be unmapped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 * @holebegin: byte in first page to unmap, relative to the start of
2892 * the underlying file. This will be rounded down to a PAGE_SIZE
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +10002893 * boundary. Note that this is different from truncate_pagecache(), which
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 * must keep the partial page. In contrast, we must get rid of
2895 * partial pages.
2896 * @holelen: size of prospective hole in bytes. This will be rounded
2897 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
2898 * end of the file.
2899 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
2900 * but 0 when invalidating pagecache, don't throw away private data.
2901 */
2902void unmap_mapping_range(struct address_space *mapping,
2903 loff_t const holebegin, loff_t const holelen, int even_cows)
2904{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 pgoff_t hba = holebegin >> PAGE_SHIFT;
2906 pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
2907
2908 /* Check for overflow. */
2909 if (sizeof(holelen) > sizeof(hlen)) {
2910 long long holeend =
2911 (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
2912 if (holeend & ~(long long)ULONG_MAX)
2913 hlen = ULONG_MAX - hba + 1;
2914 }
2915
Matthew Wilcox977fbdc2018-01-31 16:17:36 -08002916 unmap_mapping_pages(mapping, hba, hlen, even_cows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917}
2918EXPORT_SYMBOL(unmap_mapping_range);
2919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920/*
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002921 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2922 * but allow concurrent faults), and pte mapped but not yet locked.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002923 * We return with pte unmapped and unlocked.
2924 *
2925 * We return with the mmap_sem locked or unlocked in the same cases
2926 * as does filemap_fault().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 */
Souptick Joarder2b740302018-08-23 17:01:36 -07002928vm_fault_t do_swap_page(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002930 struct vm_area_struct *vma = vmf->vma;
Minchan Kimeaf649eb2018-04-05 16:23:39 -07002931 struct page *page = NULL, *swapcache;
Johannes Weiner00501b52014-08-08 14:19:20 -07002932 struct mem_cgroup *memcg;
Hugh Dickins65500d22005-10-29 18:15:59 -07002933 swp_entry_t entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 pte_t pte;
Michel Lespinassed065bd82010-10-26 14:21:57 -07002935 int locked;
Rik van Rielad8c2ee2010-08-09 17:19:48 -07002936 int exclusive = 0;
Souptick Joarder2b740302018-08-23 17:01:36 -07002937 vm_fault_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Minchan Kimeaf649eb2018-04-05 16:23:39 -07002939 if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte))
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002940 goto out;
Hugh Dickins65500d22005-10-29 18:15:59 -07002941
Jan Kara29943022016-12-14 15:07:16 -08002942 entry = pte_to_swp_entry(vmf->orig_pte);
Andi Kleend1737fd2009-09-16 11:50:06 +02002943 if (unlikely(non_swap_entry(entry))) {
2944 if (is_migration_entry(entry)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08002945 migration_entry_wait(vma->vm_mm, vmf->pmd,
2946 vmf->address);
Jérôme Glisse5042db42017-09-08 16:11:43 -07002947 } else if (is_device_private_entry(entry)) {
2948 /*
2949 * For un-addressable device memory we call the pgmap
2950 * fault handler callback. The callback must migrate
2951 * the page back to some CPU accessible page.
2952 */
2953 ret = device_private_entry_fault(vma, vmf->address, entry,
2954 vmf->flags, vmf->pmd);
Andi Kleend1737fd2009-09-16 11:50:06 +02002955 } else if (is_hwpoison_entry(entry)) {
2956 ret = VM_FAULT_HWPOISON;
2957 } else {
Jan Kara29943022016-12-14 15:07:16 -08002958 print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL);
Hugh Dickinsd99be1a2009-12-14 17:59:04 -08002959 ret = VM_FAULT_SIGBUS;
Andi Kleend1737fd2009-09-16 11:50:06 +02002960 }
Christoph Lameter06972122006-06-23 02:03:35 -07002961 goto out;
2962 }
Minchan Kim0bcac062017-11-15 17:33:07 -08002963
2964
Shailabh Nagar0ff92242006-07-14 00:24:37 -07002965 delayacct_set_flag(DELAYACCT_PF_SWAPIN);
Minchan Kimeaf649eb2018-04-05 16:23:39 -07002966 page = lookup_swap_cache(entry, vma, vmf->address);
2967 swapcache = page;
Minchan Kimf8020772018-01-18 16:33:50 -08002968
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 if (!page) {
Minchan Kim0bcac062017-11-15 17:33:07 -08002970 struct swap_info_struct *si = swp_swap_info(entry);
2971
Minchan Kimaa8d22a2017-11-15 17:33:11 -08002972 if (si->flags & SWP_SYNCHRONOUS_IO &&
2973 __swap_count(si, entry) == 1) {
Minchan Kim0bcac062017-11-15 17:33:07 -08002974 /* skip swapcache */
Minchan Kime9e9b7e2018-04-05 16:23:42 -07002975 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
2976 vmf->address);
Minchan Kim0bcac062017-11-15 17:33:07 -08002977 if (page) {
2978 __SetPageLocked(page);
2979 __SetPageSwapBacked(page);
2980 set_page_private(page, entry.val);
2981 lru_cache_add_anon(page);
2982 swap_readpage(page, true);
2983 }
Minchan Kimaa8d22a2017-11-15 17:33:11 -08002984 } else {
Minchan Kime9e9b7e2018-04-05 16:23:42 -07002985 page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
2986 vmf);
Minchan Kimaa8d22a2017-11-15 17:33:11 -08002987 swapcache = page;
Minchan Kim0bcac062017-11-15 17:33:07 -08002988 }
2989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 if (!page) {
2991 /*
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002992 * Back out if somebody else faulted in this pte
2993 * while we released the pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002995 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
2996 vmf->address, &vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08002997 if (likely(pte_same(*vmf->pte, vmf->orig_pte)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 ret = VM_FAULT_OOM;
Shailabh Nagar0ff92242006-07-14 00:24:37 -07002999 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
Hugh Dickins65500d22005-10-29 18:15:59 -07003000 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 }
3002
3003 /* Had to read the page from swap area: Major fault */
3004 ret = VM_FAULT_MAJOR;
Christoph Lameterf8891e52006-06-30 01:55:45 -07003005 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07003006 count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
Andi Kleend1737fd2009-09-16 11:50:06 +02003007 } else if (PageHWPoison(page)) {
Wu Fengguang71f72522009-12-16 12:19:58 +01003008 /*
3009 * hwpoisoned dirty swapcache pages are kept for killing
3010 * owner processes (which may be unknown at hwpoison time)
3011 */
Andi Kleend1737fd2009-09-16 11:50:06 +02003012 ret = VM_FAULT_HWPOISON;
3013 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
Andi Kleen4779cb32009-10-14 01:51:41 +02003014 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 }
3016
Jan Kara82b0f8c2016-12-14 15:06:58 -08003017 locked = lock_page_or_retry(page, vma->vm_mm, vmf->flags);
Rik van Riele709ffd2012-05-29 15:06:18 -07003018
Balbir Singh20a10222007-11-14 17:00:33 -08003019 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
Michel Lespinassed065bd82010-10-26 14:21:57 -07003020 if (!locked) {
3021 ret |= VM_FAULT_RETRY;
3022 goto out_release;
3023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003025 /*
Hugh Dickins31c4a3d2010-09-19 19:40:22 -07003026 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
3027 * release the swapcache from under us. The page pin, and pte_same
3028 * test below, are not enough to exclude that. Even if it is still
3029 * swapcache, we need to check that the page's swap has not changed.
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003030 */
Minchan Kim0bcac062017-11-15 17:33:07 -08003031 if (unlikely((!PageSwapCache(page) ||
3032 page_private(page) != entry.val)) && swapcache)
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003033 goto out_page;
3034
Jan Kara82b0f8c2016-12-14 15:06:58 -08003035 page = ksm_might_need_to_copy(page, vma, vmf->address);
Hugh Dickinscbf86cf2013-02-22 16:35:08 -08003036 if (unlikely(!page)) {
3037 ret = VM_FAULT_OOM;
3038 page = swapcache;
Hugh Dickinscbf86cf2013-02-22 16:35:08 -08003039 goto out_page;
Hugh Dickins5ad64682009-12-14 17:59:24 -08003040 }
3041
Tejun Heo2cf85582018-07-03 11:14:56 -04003042 if (mem_cgroup_try_charge_delay(page, vma->vm_mm, GFP_KERNEL,
3043 &memcg, false)) {
KAMEZAWA Hiroyuki073e5872008-10-18 20:28:08 -07003044 ret = VM_FAULT_OOM;
Johannes Weinerbc43f752009-04-30 15:08:08 -07003045 goto out_page;
KAMEZAWA Hiroyuki073e5872008-10-18 20:28:08 -07003046 }
3047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 /*
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003049 * Back out if somebody else already faulted in this pte.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003051 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3052 &vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08003053 if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte)))
Kirill Korotaevb8107482005-05-16 21:53:50 -07003054 goto out_nomap;
Kirill Korotaevb8107482005-05-16 21:53:50 -07003055
3056 if (unlikely(!PageUptodate(page))) {
3057 ret = VM_FAULT_SIGBUS;
3058 goto out_nomap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 }
3060
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003061 /*
3062 * The page isn't present yet, go ahead with the fault.
3063 *
3064 * Be careful about the sequence of operations here.
3065 * To get its accounting right, reuse_swap_page() must be called
3066 * while the page is counted on swap but not yet in mapcount i.e.
3067 * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
3068 * must be called after the swap_free(), or it will never succeed.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003069 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003071 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
3072 dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 pte = mk_pte(page, vma->vm_page_prot);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003074 if ((vmf->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003076 vmf->flags &= ~FAULT_FLAG_WRITE;
Andrea Arcangeli9a5b4892010-08-09 17:19:49 -07003077 ret |= VM_FAULT_WRITE;
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08003078 exclusive = RMAP_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 flush_icache_page(vma, page);
Jan Kara29943022016-12-14 15:07:16 -08003081 if (pte_swp_soft_dirty(vmf->orig_pte))
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07003082 pte = pte_mksoft_dirty(pte);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003083 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte);
Khalid Azizca827d52018-02-21 10:15:44 -07003084 arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte);
Jan Kara29943022016-12-14 15:07:16 -08003085 vmf->orig_pte = pte;
Minchan Kim0bcac062017-11-15 17:33:07 -08003086
3087 /* ksm created a completely new copy */
3088 if (unlikely(page != swapcache && swapcache)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003089 page_add_new_anon_rmap(page, vma, vmf->address, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08003090 mem_cgroup_commit_charge(page, memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07003091 lru_cache_add_active_or_unevictable(page, vma);
Minchan Kim0bcac062017-11-15 17:33:07 -08003092 } else {
3093 do_page_add_anon_rmap(page, vma, vmf->address, exclusive);
3094 mem_cgroup_commit_charge(page, memcg, true, false);
3095 activate_page(page);
Johannes Weiner00501b52014-08-08 14:19:20 -07003096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07003098 swap_free(entry);
Vladimir Davydov5ccc5ab2016-01-20 15:03:10 -08003099 if (mem_cgroup_swap_full(page) ||
3100 (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -08003101 try_to_free_swap(page);
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07003102 unlock_page(page);
Minchan Kim0bcac062017-11-15 17:33:07 -08003103 if (page != swapcache && swapcache) {
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003104 /*
3105 * Hold the lock to avoid the swap entry to be reused
3106 * until we take the PT lock for the pte_same() check
3107 * (to avoid false positives from pte_same). For
3108 * further safety release the lock after the swap_free
3109 * so that the swap count won't change under a
3110 * parallel locked swapcache.
3111 */
3112 unlock_page(swapcache);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003113 put_page(swapcache);
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003114 }
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07003115
Jan Kara82b0f8c2016-12-14 15:06:58 -08003116 if (vmf->flags & FAULT_FLAG_WRITE) {
Jan Kara29943022016-12-14 15:07:16 -08003117 ret |= do_wp_page(vmf);
Hugh Dickins61469f12008-03-04 14:29:04 -08003118 if (ret & VM_FAULT_ERROR)
3119 ret &= VM_FAULT_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 goto out;
3121 }
3122
3123 /* No need to invalidate - it was non-present before */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003124 update_mmu_cache(vma, vmf->address, vmf->pte);
Hugh Dickins65500d22005-10-29 18:15:59 -07003125unlock:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003126 pte_unmap_unlock(vmf->pte, vmf->ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127out:
3128 return ret;
Kirill Korotaevb8107482005-05-16 21:53:50 -07003129out_nomap:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08003130 mem_cgroup_cancel_charge(page, memcg, false);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003131 pte_unmap_unlock(vmf->pte, vmf->ptl);
Johannes Weinerbc43f752009-04-30 15:08:08 -07003132out_page:
Kirill Korotaevb8107482005-05-16 21:53:50 -07003133 unlock_page(page);
Andi Kleen4779cb32009-10-14 01:51:41 +02003134out_release:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003135 put_page(page);
Minchan Kim0bcac062017-11-15 17:33:07 -08003136 if (page != swapcache && swapcache) {
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003137 unlock_page(swapcache);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003138 put_page(swapcache);
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003139 }
Hugh Dickins65500d22005-10-29 18:15:59 -07003140 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141}
3142
3143/*
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003144 * We enter with non-exclusive mmap_sem (to exclude vma changes,
3145 * but allow concurrent faults), and pte mapped but not yet locked.
3146 * We return with mmap_sem still held, but pte unmapped and unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003148static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003150 struct vm_area_struct *vma = vmf->vma;
Johannes Weiner00501b52014-08-08 14:19:20 -07003151 struct mem_cgroup *memcg;
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003152 struct page *page;
Souptick Joarder2b740302018-08-23 17:01:36 -07003153 vm_fault_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 pte_t entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Kirill A. Shutemov6b7339f2015-07-06 23:18:37 +03003156 /* File mapping without ->vm_ops ? */
3157 if (vma->vm_flags & VM_SHARED)
3158 return VM_FAULT_SIGBUS;
3159
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003160 /*
3161 * Use pte_alloc() instead of pte_alloc_map(). We can't run
3162 * pte_offset_map() on pmds where a huge pmd might be created
3163 * from a different thread.
3164 *
3165 * pte_alloc_map() is safe to use under down_write(mmap_sem) or when
3166 * parallel threads are excluded by other means.
3167 *
3168 * Here we only have down_read(mmap_sem).
3169 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003170 if (pte_alloc(vma->vm_mm, vmf->pmd, vmf->address))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003171 return VM_FAULT_OOM;
3172
3173 /* See the comment in pte_alloc_one_map() */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003174 if (unlikely(pmd_trans_unstable(vmf->pmd)))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003175 return 0;
3176
Linus Torvalds11ac5522010-08-14 11:44:56 -07003177 /* Use the zero-page for reads */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003178 if (!(vmf->flags & FAULT_FLAG_WRITE) &&
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003179 !mm_forbids_zeropage(vma->vm_mm)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003180 entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address),
Hugh Dickins62eede62009-09-21 17:03:34 -07003181 vma->vm_page_prot));
Jan Kara82b0f8c2016-12-14 15:06:58 -08003182 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
3183 vmf->address, &vmf->ptl);
3184 if (!pte_none(*vmf->pte))
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -07003185 goto unlock;
Michal Hocko6b31d592017-08-18 15:16:15 -07003186 ret = check_stable_address_space(vma->vm_mm);
3187 if (ret)
3188 goto unlock;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07003189 /* Deliver the page fault to userland, check inside PT lock */
3190 if (userfaultfd_missing(vma)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003191 pte_unmap_unlock(vmf->pte, vmf->ptl);
3192 return handle_userfault(vmf, VM_UFFD_MISSING);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07003193 }
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -07003194 goto setpte;
3195 }
3196
Nick Piggin557ed1f2007-10-16 01:24:40 -07003197 /* Allocate our own private page. */
Nick Piggin557ed1f2007-10-16 01:24:40 -07003198 if (unlikely(anon_vma_prepare(vma)))
3199 goto oom;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003200 page = alloc_zeroed_user_highpage_movable(vma, vmf->address);
Nick Piggin557ed1f2007-10-16 01:24:40 -07003201 if (!page)
3202 goto oom;
Mel Gormaneb3c24f2015-06-24 16:57:27 -07003203
Tejun Heo2cf85582018-07-03 11:14:56 -04003204 if (mem_cgroup_try_charge_delay(page, vma->vm_mm, GFP_KERNEL, &memcg,
3205 false))
Mel Gormaneb3c24f2015-06-24 16:57:27 -07003206 goto oom_free_page;
3207
Minchan Kim52f37622013-04-29 15:08:15 -07003208 /*
3209 * The memory barrier inside __SetPageUptodate makes sure that
3210 * preceeding stores to the page contents become visible before
3211 * the set_pte_at() write.
3212 */
Nick Piggin0ed361d2008-02-04 22:29:34 -08003213 __SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Nick Piggin557ed1f2007-10-16 01:24:40 -07003215 entry = mk_pte(page, vma->vm_page_prot);
Hugh Dickins1ac0cb52009-09-21 17:03:29 -07003216 if (vma->vm_flags & VM_WRITE)
3217 entry = pte_mkwrite(pte_mkdirty(entry));
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003218
Jan Kara82b0f8c2016-12-14 15:06:58 -08003219 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3220 &vmf->ptl);
3221 if (!pte_none(*vmf->pte))
Nick Piggin557ed1f2007-10-16 01:24:40 -07003222 goto release;
Hugh Dickins9ba69292009-09-21 17:02:20 -07003223
Michal Hocko6b31d592017-08-18 15:16:15 -07003224 ret = check_stable_address_space(vma->vm_mm);
3225 if (ret)
3226 goto release;
3227
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07003228 /* Deliver the page fault to userland, check inside PT lock */
3229 if (userfaultfd_missing(vma)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003230 pte_unmap_unlock(vmf->pte, vmf->ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08003231 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003232 put_page(page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003233 return handle_userfault(vmf, VM_UFFD_MISSING);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07003234 }
3235
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003236 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003237 page_add_new_anon_rmap(page, vma, vmf->address, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08003238 mem_cgroup_commit_charge(page, memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07003239 lru_cache_add_active_or_unevictable(page, vma);
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -07003240setpte:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003241 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
3243 /* No need to invalidate - it was non-present before */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003244 update_mmu_cache(vma, vmf->address, vmf->pte);
Hugh Dickins65500d22005-10-29 18:15:59 -07003245unlock:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003246 pte_unmap_unlock(vmf->pte, vmf->ptl);
Michal Hocko6b31d592017-08-18 15:16:15 -07003247 return ret;
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003248release:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08003249 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003250 put_page(page);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003251 goto unlock;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003252oom_free_page:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003253 put_page(page);
Hugh Dickins65500d22005-10-29 18:15:59 -07003254oom:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 return VM_FAULT_OOM;
3256}
3257
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003258/*
3259 * The mmap_sem must have been held on entry, and may have been
3260 * released depending on flags and vma->vm_ops->fault() return value.
3261 * See filemap_fault() and __lock_page_retry().
3262 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003263static vm_fault_t __do_fault(struct vm_fault *vmf)
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003264{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003265 struct vm_area_struct *vma = vmf->vma;
Souptick Joarder2b740302018-08-23 17:01:36 -07003266 vm_fault_t ret;
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003267
Michal Hocko97b02b62019-01-08 15:23:07 -08003268 /*
3269 * Preallocate pte before we take page_lock because this might lead to
3270 * deadlocks for memcg reclaim which waits for pages under writeback:
3271 * lock_page(A)
3272 * SetPageWriteback(A)
3273 * unlock_page(A)
3274 * lock_page(B)
3275 * lock_page(B)
3276 * pte_alloc_pne
3277 * shrink_page_list
3278 * wait_on_page_writeback(A)
3279 * SetPageWriteback(B)
3280 * unlock_page(B)
3281 * # flush A, B to clear the writeback
3282 */
3283 if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) {
3284 vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm,
3285 vmf->address);
3286 if (!vmf->prealloc_pte)
3287 return VM_FAULT_OOM;
3288 smp_wmb(); /* See comment in __pte_alloc() */
3289 }
3290
Dave Jiang11bac802017-02-24 14:56:41 -08003291 ret = vma->vm_ops->fault(vmf);
Jan Kara39170482016-12-14 15:07:18 -08003292 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY |
Jan Karab1aa8122016-12-14 15:07:24 -08003293 VM_FAULT_DONE_COW)))
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003294 return ret;
3295
Jan Kara667240e2016-12-14 15:07:07 -08003296 if (unlikely(PageHWPoison(vmf->page))) {
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003297 if (ret & VM_FAULT_LOCKED)
Jan Kara667240e2016-12-14 15:07:07 -08003298 unlock_page(vmf->page);
3299 put_page(vmf->page);
Jan Kara936ca802016-12-14 15:07:10 -08003300 vmf->page = NULL;
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003301 return VM_FAULT_HWPOISON;
3302 }
3303
3304 if (unlikely(!(ret & VM_FAULT_LOCKED)))
Jan Kara667240e2016-12-14 15:07:07 -08003305 lock_page(vmf->page);
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003306 else
Jan Kara667240e2016-12-14 15:07:07 -08003307 VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page);
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003308
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003309 return ret;
3310}
3311
Ross Zwislerd0f0931d2017-06-02 14:46:34 -07003312/*
3313 * The ordering of these checks is important for pmds with _PAGE_DEVMAP set.
3314 * If we check pmd_trans_unstable() first we will trip the bad_pmd() check
3315 * inside of pmd_none_or_trans_huge_or_clear_bad(). This will end up correctly
3316 * returning 1 but not before it spams dmesg with the pmd_clear_bad() output.
3317 */
3318static int pmd_devmap_trans_unstable(pmd_t *pmd)
3319{
3320 return pmd_devmap(*pmd) || pmd_trans_unstable(pmd);
3321}
3322
Souptick Joarder2b740302018-08-23 17:01:36 -07003323static vm_fault_t pte_alloc_one_map(struct vm_fault *vmf)
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003324{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003325 struct vm_area_struct *vma = vmf->vma;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003326
Jan Kara82b0f8c2016-12-14 15:06:58 -08003327 if (!pmd_none(*vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003328 goto map_pte;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003329 if (vmf->prealloc_pte) {
3330 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
3331 if (unlikely(!pmd_none(*vmf->pmd))) {
3332 spin_unlock(vmf->ptl);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003333 goto map_pte;
3334 }
3335
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08003336 mm_inc_nr_ptes(vma->vm_mm);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003337 pmd_populate(vma->vm_mm, vmf->pmd, vmf->prealloc_pte);
3338 spin_unlock(vmf->ptl);
Tobin C Harding7f2b6ce2017-02-24 14:58:59 -08003339 vmf->prealloc_pte = NULL;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003340 } else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd, vmf->address))) {
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003341 return VM_FAULT_OOM;
3342 }
3343map_pte:
3344 /*
3345 * If a huge pmd materialized under us just retry later. Use
Ross Zwislerd0f0931d2017-06-02 14:46:34 -07003346 * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead of
3347 * pmd_trans_huge() to ensure the pmd didn't become pmd_trans_huge
3348 * under us and then back to pmd_none, as a result of MADV_DONTNEED
3349 * running immediately after a huge pmd fault in a different thread of
3350 * this mm, in turn leading to a misleading pmd_trans_huge() retval.
3351 * All we have to ensure is that it is a regular pmd that we can walk
3352 * with pte_offset_map() and we can do that through an atomic read in
3353 * C, which is what pmd_trans_unstable() provides.
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003354 */
Ross Zwislerd0f0931d2017-06-02 14:46:34 -07003355 if (pmd_devmap_trans_unstable(vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003356 return VM_FAULT_NOPAGE;
3357
Ross Zwislerd0f0931d2017-06-02 14:46:34 -07003358 /*
3359 * At this point we know that our vmf->pmd points to a page of ptes
3360 * and it cannot become pmd_none(), pmd_devmap() or pmd_trans_huge()
3361 * for the duration of the fault. If a racing MADV_DONTNEED runs and
3362 * we zap the ptes pointed to by our vmf->pmd, the vmf->ptl will still
3363 * be valid and we will re-check to make sure the vmf->pte isn't
3364 * pte_none() under vmf->ptl protection when we return to
3365 * alloc_set_pte().
3366 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003367 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3368 &vmf->ptl);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003369 return 0;
3370}
3371
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003372#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003373
3374#define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1)
3375static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
3376 unsigned long haddr)
3377{
3378 if (((vma->vm_start >> PAGE_SHIFT) & HPAGE_CACHE_INDEX_MASK) !=
3379 (vma->vm_pgoff & HPAGE_CACHE_INDEX_MASK))
3380 return false;
3381 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
3382 return false;
3383 return true;
3384}
3385
Jan Kara82b0f8c2016-12-14 15:06:58 -08003386static void deposit_prealloc_pte(struct vm_fault *vmf)
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003387{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003388 struct vm_area_struct *vma = vmf->vma;
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003389
Jan Kara82b0f8c2016-12-14 15:06:58 -08003390 pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte);
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003391 /*
3392 * We are going to consume the prealloc table,
3393 * count that as nr_ptes.
3394 */
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08003395 mm_inc_nr_ptes(vma->vm_mm);
Tobin C Harding7f2b6ce2017-02-24 14:58:59 -08003396 vmf->prealloc_pte = NULL;
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003397}
3398
Souptick Joarder2b740302018-08-23 17:01:36 -07003399static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003400{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003401 struct vm_area_struct *vma = vmf->vma;
3402 bool write = vmf->flags & FAULT_FLAG_WRITE;
3403 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003404 pmd_t entry;
Souptick Joarder2b740302018-08-23 17:01:36 -07003405 int i;
3406 vm_fault_t ret;
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003407
3408 if (!transhuge_vma_suitable(vma, haddr))
3409 return VM_FAULT_FALLBACK;
3410
3411 ret = VM_FAULT_FALLBACK;
3412 page = compound_head(page);
3413
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003414 /*
3415 * Archs like ppc64 need additonal space to store information
3416 * related to pte entry. Use the preallocated table for that.
3417 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003418 if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) {
3419 vmf->prealloc_pte = pte_alloc_one(vma->vm_mm, vmf->address);
3420 if (!vmf->prealloc_pte)
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003421 return VM_FAULT_OOM;
3422 smp_wmb(); /* See comment in __pte_alloc() */
3423 }
3424
Jan Kara82b0f8c2016-12-14 15:06:58 -08003425 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
3426 if (unlikely(!pmd_none(*vmf->pmd)))
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003427 goto out;
3428
3429 for (i = 0; i < HPAGE_PMD_NR; i++)
3430 flush_icache_page(vma, page + i);
3431
3432 entry = mk_huge_pmd(page, vma->vm_page_prot);
3433 if (write)
Linus Torvaldsf55e1012017-11-29 09:01:01 -08003434 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003435
Yang Shifadae292018-08-17 15:44:55 -07003436 add_mm_counter(vma->vm_mm, mm_counter_file(page), HPAGE_PMD_NR);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003437 page_add_file_rmap(page, true);
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003438 /*
3439 * deposit and withdraw with pmd lock held
3440 */
3441 if (arch_needs_pgtable_deposit())
Jan Kara82b0f8c2016-12-14 15:06:58 -08003442 deposit_prealloc_pte(vmf);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003443
Jan Kara82b0f8c2016-12-14 15:06:58 -08003444 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003445
Jan Kara82b0f8c2016-12-14 15:06:58 -08003446 update_mmu_cache_pmd(vma, haddr, vmf->pmd);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003447
3448 /* fault is handled */
3449 ret = 0;
Kirill A. Shutemov95ecedc2016-07-26 15:25:31 -07003450 count_vm_event(THP_FILE_MAPPED);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003451out:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003452 spin_unlock(vmf->ptl);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003453 return ret;
3454}
3455#else
Souptick Joarder2b740302018-08-23 17:01:36 -07003456static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003457{
3458 BUILD_BUG();
3459 return 0;
3460}
3461#endif
3462
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003463/**
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003464 * alloc_set_pte - setup new PTE entry for given page and add reverse page
3465 * mapping. If needed, the fucntion allocates page table or use pre-allocated.
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003466 *
Jan Kara82b0f8c2016-12-14 15:06:58 -08003467 * @vmf: fault environment
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003468 * @memcg: memcg to charge page (only for private mappings)
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003469 * @page: page to map
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003470 *
Jan Kara82b0f8c2016-12-14 15:06:58 -08003471 * Caller must take care of unlocking vmf->ptl, if vmf->pte is non-NULL on
3472 * return.
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003473 *
3474 * Target users are page handler itself and implementations of
3475 * vm_ops->map_pages.
3476 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003477vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg,
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003478 struct page *page)
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003479{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003480 struct vm_area_struct *vma = vmf->vma;
3481 bool write = vmf->flags & FAULT_FLAG_WRITE;
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003482 pte_t entry;
Souptick Joarder2b740302018-08-23 17:01:36 -07003483 vm_fault_t ret;
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003484
Jan Kara82b0f8c2016-12-14 15:06:58 -08003485 if (pmd_none(*vmf->pmd) && PageTransCompound(page) &&
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003486 IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003487 /* THP on COW? */
3488 VM_BUG_ON_PAGE(memcg, page);
3489
Jan Kara82b0f8c2016-12-14 15:06:58 -08003490 ret = do_set_pmd(vmf, page);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003491 if (ret != VM_FAULT_FALLBACK)
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003492 return ret;
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003493 }
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003494
Jan Kara82b0f8c2016-12-14 15:06:58 -08003495 if (!vmf->pte) {
3496 ret = pte_alloc_one_map(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003497 if (ret)
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003498 return ret;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003499 }
3500
3501 /* Re-check under ptl */
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003502 if (unlikely(!pte_none(*vmf->pte)))
3503 return VM_FAULT_NOPAGE;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003504
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003505 flush_icache_page(vma, page);
3506 entry = mk_pte(page, vma->vm_page_prot);
3507 if (write)
3508 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003509 /* copy-on-write page */
3510 if (write && !(vma->vm_flags & VM_SHARED)) {
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003511 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003512 page_add_new_anon_rmap(page, vma, vmf->address, false);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003513 mem_cgroup_commit_charge(page, memcg, false, false);
3514 lru_cache_add_active_or_unevictable(page, vma);
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003515 } else {
Jerome Marchandeca56ff2016-01-14 15:19:26 -08003516 inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07003517 page_add_file_rmap(page, false);
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003518 }
Jan Kara82b0f8c2016-12-14 15:06:58 -08003519 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003520
3521 /* no need to invalidate: a not-present page won't be cached */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003522 update_mmu_cache(vma, vmf->address, vmf->pte);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003523
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003524 return 0;
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003525}
3526
Jan Kara9118c0c2016-12-14 15:07:21 -08003527
3528/**
3529 * finish_fault - finish page fault once we have prepared the page to fault
3530 *
3531 * @vmf: structure describing the fault
3532 *
3533 * This function handles all that is needed to finish a page fault once the
3534 * page to fault in is prepared. It handles locking of PTEs, inserts PTE for
3535 * given page, adds reverse page mapping, handles memcg charges and LRU
3536 * addition. The function returns 0 on success, VM_FAULT_ code in case of
3537 * error.
3538 *
3539 * The function expects the page to be locked and on success it consumes a
3540 * reference of a page being mapped (for the PTE which maps it).
3541 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003542vm_fault_t finish_fault(struct vm_fault *vmf)
Jan Kara9118c0c2016-12-14 15:07:21 -08003543{
3544 struct page *page;
Souptick Joarder2b740302018-08-23 17:01:36 -07003545 vm_fault_t ret = 0;
Jan Kara9118c0c2016-12-14 15:07:21 -08003546
3547 /* Did we COW the page? */
3548 if ((vmf->flags & FAULT_FLAG_WRITE) &&
3549 !(vmf->vma->vm_flags & VM_SHARED))
3550 page = vmf->cow_page;
3551 else
3552 page = vmf->page;
Michal Hocko6b31d592017-08-18 15:16:15 -07003553
3554 /*
3555 * check even for read faults because we might have lost our CoWed
3556 * page
3557 */
3558 if (!(vmf->vma->vm_flags & VM_SHARED))
3559 ret = check_stable_address_space(vmf->vma->vm_mm);
3560 if (!ret)
3561 ret = alloc_set_pte(vmf, vmf->memcg, page);
Jan Kara9118c0c2016-12-14 15:07:21 -08003562 if (vmf->pte)
3563 pte_unmap_unlock(vmf->pte, vmf->ptl);
3564 return ret;
3565}
3566
Kirill A. Shutemov3a910532014-08-06 16:08:07 -07003567static unsigned long fault_around_bytes __read_mostly =
3568 rounddown_pow_of_two(65536);
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003569
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003570#ifdef CONFIG_DEBUG_FS
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003571static int fault_around_bytes_get(void *data, u64 *val)
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003572{
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003573 *val = fault_around_bytes;
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003574 return 0;
3575}
3576
Andrey Ryabininb4903d62014-07-30 16:08:35 -07003577/*
William Kucharskida391d62018-01-31 16:21:11 -08003578 * fault_around_bytes must be rounded down to the nearest page order as it's
3579 * what do_fault_around() expects to see.
Andrey Ryabininb4903d62014-07-30 16:08:35 -07003580 */
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003581static int fault_around_bytes_set(void *data, u64 val)
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003582{
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003583 if (val / PAGE_SIZE > PTRS_PER_PTE)
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003584 return -EINVAL;
Andrey Ryabininb4903d62014-07-30 16:08:35 -07003585 if (val > PAGE_SIZE)
3586 fault_around_bytes = rounddown_pow_of_two(val);
3587 else
3588 fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003589 return 0;
3590}
Yevgen Pronenko0a1345f2017-07-10 15:47:17 -07003591DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops,
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003592 fault_around_bytes_get, fault_around_bytes_set, "%llu\n");
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003593
3594static int __init fault_around_debugfs(void)
3595{
3596 void *ret;
3597
Yevgen Pronenko0a1345f2017-07-10 15:47:17 -07003598 ret = debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL,
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003599 &fault_around_bytes_fops);
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003600 if (!ret)
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003601 pr_warn("Failed to create fault_around_bytes in debugfs");
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003602 return 0;
3603}
3604late_initcall(fault_around_debugfs);
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003605#endif
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003606
Kirill A. Shutemov1fdb4122014-06-04 16:10:55 -07003607/*
3608 * do_fault_around() tries to map few pages around the fault address. The hope
3609 * is that the pages will be needed soon and this will lower the number of
3610 * faults to handle.
3611 *
3612 * It uses vm_ops->map_pages() to map the pages, which skips the page if it's
3613 * not ready to be mapped: not up-to-date, locked, etc.
3614 *
3615 * This function is called with the page table lock taken. In the split ptlock
3616 * case the page table lock only protects only those entries which belong to
3617 * the page table corresponding to the fault address.
3618 *
3619 * This function doesn't cross the VMA boundaries, in order to call map_pages()
3620 * only once.
3621 *
William Kucharskida391d62018-01-31 16:21:11 -08003622 * fault_around_bytes defines how many bytes we'll try to map.
3623 * do_fault_around() expects it to be set to a power of two less than or equal
3624 * to PTRS_PER_PTE.
Kirill A. Shutemov1fdb4122014-06-04 16:10:55 -07003625 *
William Kucharskida391d62018-01-31 16:21:11 -08003626 * The virtual address of the area that we map is naturally aligned to
3627 * fault_around_bytes rounded down to the machine page size
3628 * (and therefore to page order). This way it's easier to guarantee
3629 * that we don't cross page table boundaries.
Kirill A. Shutemov1fdb4122014-06-04 16:10:55 -07003630 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003631static vm_fault_t do_fault_around(struct vm_fault *vmf)
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003632{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003633 unsigned long address = vmf->address, nr_pages, mask;
Jan Kara0721ec82016-12-14 15:07:04 -08003634 pgoff_t start_pgoff = vmf->pgoff;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003635 pgoff_t end_pgoff;
Souptick Joarder2b740302018-08-23 17:01:36 -07003636 int off;
3637 vm_fault_t ret = 0;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003638
Jason Low4db0c3c2015-04-15 16:14:08 -07003639 nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT;
Kirill A. Shutemovaecd6f42014-08-06 16:08:05 -07003640 mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
3641
Jan Kara82b0f8c2016-12-14 15:06:58 -08003642 vmf->address = max(address & mask, vmf->vma->vm_start);
3643 off = ((address - vmf->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003644 start_pgoff -= off;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003645
3646 /*
William Kucharskida391d62018-01-31 16:21:11 -08003647 * end_pgoff is either the end of the page table, the end of
3648 * the vma or nr_pages from start_pgoff, depending what is nearest.
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003649 */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003650 end_pgoff = start_pgoff -
Jan Kara82b0f8c2016-12-14 15:06:58 -08003651 ((vmf->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003652 PTRS_PER_PTE - 1;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003653 end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003654 start_pgoff + nr_pages - 1);
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003655
Jan Kara82b0f8c2016-12-14 15:06:58 -08003656 if (pmd_none(*vmf->pmd)) {
3657 vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm,
3658 vmf->address);
3659 if (!vmf->prealloc_pte)
Vegard Nossumc5f88bd2016-08-02 14:02:22 -07003660 goto out;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003661 smp_wmb(); /* See comment in __pte_alloc() */
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003662 }
3663
Jan Kara82b0f8c2016-12-14 15:06:58 -08003664 vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003665
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003666 /* Huge page is mapped? Page fault is solved */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003667 if (pmd_trans_huge(*vmf->pmd)) {
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003668 ret = VM_FAULT_NOPAGE;
3669 goto out;
3670 }
3671
3672 /* ->map_pages() haven't done anything useful. Cold page cache? */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003673 if (!vmf->pte)
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003674 goto out;
3675
3676 /* check if the page fault is solved */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003677 vmf->pte -= (vmf->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT);
3678 if (!pte_none(*vmf->pte))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003679 ret = VM_FAULT_NOPAGE;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003680 pte_unmap_unlock(vmf->pte, vmf->ptl);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003681out:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003682 vmf->address = address;
3683 vmf->pte = NULL;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003684 return ret;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003685}
3686
Souptick Joarder2b740302018-08-23 17:01:36 -07003687static vm_fault_t do_read_fault(struct vm_fault *vmf)
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003688{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003689 struct vm_area_struct *vma = vmf->vma;
Souptick Joarder2b740302018-08-23 17:01:36 -07003690 vm_fault_t ret = 0;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003691
3692 /*
3693 * Let's call ->map_pages() first and use ->fault() as fallback
3694 * if page by the offset is not ready to be mapped (cold cache or
3695 * something).
3696 */
Kirill A. Shutemov9b4bdd22015-02-10 14:09:51 -08003697 if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) {
Jan Kara0721ec82016-12-14 15:07:04 -08003698 ret = do_fault_around(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003699 if (ret)
3700 return ret;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003701 }
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003702
Jan Kara936ca802016-12-14 15:07:10 -08003703 ret = __do_fault(vmf);
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003704 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3705 return ret;
3706
Jan Kara9118c0c2016-12-14 15:07:21 -08003707 ret |= finish_fault(vmf);
Jan Kara936ca802016-12-14 15:07:10 -08003708 unlock_page(vmf->page);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003709 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
Jan Kara936ca802016-12-14 15:07:10 -08003710 put_page(vmf->page);
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003711 return ret;
3712}
3713
Souptick Joarder2b740302018-08-23 17:01:36 -07003714static vm_fault_t do_cow_fault(struct vm_fault *vmf)
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003715{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003716 struct vm_area_struct *vma = vmf->vma;
Souptick Joarder2b740302018-08-23 17:01:36 -07003717 vm_fault_t ret;
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003718
3719 if (unlikely(anon_vma_prepare(vma)))
3720 return VM_FAULT_OOM;
3721
Jan Kara936ca802016-12-14 15:07:10 -08003722 vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address);
3723 if (!vmf->cow_page)
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003724 return VM_FAULT_OOM;
3725
Tejun Heo2cf85582018-07-03 11:14:56 -04003726 if (mem_cgroup_try_charge_delay(vmf->cow_page, vma->vm_mm, GFP_KERNEL,
Jan Kara39170482016-12-14 15:07:18 -08003727 &vmf->memcg, false)) {
Jan Kara936ca802016-12-14 15:07:10 -08003728 put_page(vmf->cow_page);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003729 return VM_FAULT_OOM;
3730 }
3731
Jan Kara936ca802016-12-14 15:07:10 -08003732 ret = __do_fault(vmf);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003733 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3734 goto uncharge_out;
Jan Kara39170482016-12-14 15:07:18 -08003735 if (ret & VM_FAULT_DONE_COW)
3736 return ret;
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003737
Jan Karab1aa8122016-12-14 15:07:24 -08003738 copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma);
Jan Kara936ca802016-12-14 15:07:10 -08003739 __SetPageUptodate(vmf->cow_page);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003740
Jan Kara9118c0c2016-12-14 15:07:21 -08003741 ret |= finish_fault(vmf);
Jan Karab1aa8122016-12-14 15:07:24 -08003742 unlock_page(vmf->page);
3743 put_page(vmf->page);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003744 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3745 goto uncharge_out;
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003746 return ret;
3747uncharge_out:
Jan Kara39170482016-12-14 15:07:18 -08003748 mem_cgroup_cancel_charge(vmf->cow_page, vmf->memcg, false);
Jan Kara936ca802016-12-14 15:07:10 -08003749 put_page(vmf->cow_page);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003750 return ret;
3751}
3752
Souptick Joarder2b740302018-08-23 17:01:36 -07003753static vm_fault_t do_shared_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003755 struct vm_area_struct *vma = vmf->vma;
Souptick Joarder2b740302018-08-23 17:01:36 -07003756 vm_fault_t ret, tmp;
KAMEZAWA Hiroyuki1d65f862011-07-25 17:12:27 -07003757
Jan Kara936ca802016-12-14 15:07:10 -08003758 ret = __do_fault(vmf);
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003759 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003760 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
3762 /*
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003763 * Check if the backing address space wants to know that the page is
3764 * about to become writable
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 */
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07003766 if (vma->vm_ops->page_mkwrite) {
Jan Kara936ca802016-12-14 15:07:10 -08003767 unlock_page(vmf->page);
Jan Kara38b8cb72016-12-14 15:07:30 -08003768 tmp = do_page_mkwrite(vmf);
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07003769 if (unlikely(!tmp ||
3770 (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
Jan Kara936ca802016-12-14 15:07:10 -08003771 put_page(vmf->page);
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07003772 return tmp;
3773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 }
3775
Jan Kara9118c0c2016-12-14 15:07:21 -08003776 ret |= finish_fault(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003777 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
3778 VM_FAULT_RETRY))) {
Jan Kara936ca802016-12-14 15:07:10 -08003779 unlock_page(vmf->page);
3780 put_page(vmf->page);
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003781 return ret;
Peter Zijlstrad08b3852006-09-25 23:30:57 -07003782 }
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003783
Jan Kara97ba0c22016-12-14 15:07:27 -08003784 fault_dirty_shared_page(vma, vmf->page);
KAMEZAWA Hiroyuki1d65f862011-07-25 17:12:27 -07003785 return ret;
Nick Piggin54cb8822007-07-19 01:46:59 -07003786}
Nick Piggind00806b2007-07-19 01:46:57 -07003787
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003788/*
3789 * We enter with non-exclusive mmap_sem (to exclude vma changes,
3790 * but allow concurrent faults).
3791 * The mmap_sem may have been released depending on flags and our
3792 * return value. See filemap_fault() and __lock_page_or_retry().
Jan Stancek09417dd2019-03-05 15:50:08 -08003793 * If mmap_sem is released, vma may become invalid (for example
3794 * by other thread calling munmap()).
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003795 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003796static vm_fault_t do_fault(struct vm_fault *vmf)
Nick Piggin54cb8822007-07-19 01:46:59 -07003797{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003798 struct vm_area_struct *vma = vmf->vma;
Jan Stancek09417dd2019-03-05 15:50:08 -08003799 struct mm_struct *vm_mm = vma->vm_mm;
Souptick Joarder2b740302018-08-23 17:01:36 -07003800 vm_fault_t ret;
Nick Piggin54cb8822007-07-19 01:46:59 -07003801
Aneesh Kumar K.V59996092018-10-26 15:09:01 -07003802 /*
3803 * The VMA was not fully populated on mmap() or missing VM_DONTEXPAND
3804 */
3805 if (!vma->vm_ops->fault) {
3806 /*
3807 * If we find a migration pmd entry or a none pmd entry, which
3808 * should never happen, return SIGBUS
3809 */
3810 if (unlikely(!pmd_present(*vmf->pmd)))
3811 ret = VM_FAULT_SIGBUS;
3812 else {
3813 vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm,
3814 vmf->pmd,
3815 vmf->address,
3816 &vmf->ptl);
3817 /*
3818 * Make sure this is not a temporary clearing of pte
3819 * by holding ptl and checking again. A R/M/W update
3820 * of pte involves: take ptl, clearing the pte so that
3821 * we don't have concurrent modification by hardware
3822 * followed by an update.
3823 */
3824 if (unlikely(pte_none(*vmf->pte)))
3825 ret = VM_FAULT_SIGBUS;
3826 else
3827 ret = VM_FAULT_NOPAGE;
3828
3829 pte_unmap_unlock(vmf->pte, vmf->ptl);
3830 }
3831 } else if (!(vmf->flags & FAULT_FLAG_WRITE))
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003832 ret = do_read_fault(vmf);
3833 else if (!(vma->vm_flags & VM_SHARED))
3834 ret = do_cow_fault(vmf);
3835 else
3836 ret = do_shared_fault(vmf);
3837
3838 /* preallocated pagetable is unused: free it */
3839 if (vmf->prealloc_pte) {
Jan Stancek09417dd2019-03-05 15:50:08 -08003840 pte_free(vm_mm, vmf->prealloc_pte);
Tobin C Harding7f2b6ce2017-02-24 14:58:59 -08003841 vmf->prealloc_pte = NULL;
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003842 }
3843 return ret;
Nick Piggin54cb8822007-07-19 01:46:59 -07003844}
3845
Rashika Kheriab19a9932014-04-03 14:48:02 -07003846static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
Rik van Riel04bb2f92013-10-07 11:29:36 +01003847 unsigned long addr, int page_nid,
3848 int *flags)
Mel Gorman9532fec2012-11-15 01:24:32 +00003849{
3850 get_page(page);
3851
3852 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01003853 if (page_nid == numa_node_id()) {
Mel Gorman9532fec2012-11-15 01:24:32 +00003854 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01003855 *flags |= TNF_FAULT_LOCAL;
3856 }
Mel Gorman9532fec2012-11-15 01:24:32 +00003857
3858 return mpol_misplaced(page, vma, addr);
3859}
3860
Souptick Joarder2b740302018-08-23 17:01:36 -07003861static vm_fault_t do_numa_page(struct vm_fault *vmf)
Mel Gormand10e63f2012-10-25 14:16:31 +02003862{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003863 struct vm_area_struct *vma = vmf->vma;
Mel Gorman4daae3b2012-11-02 11:33:45 +00003864 struct page *page = NULL;
Mel Gorman8191acb2013-10-07 11:28:45 +01003865 int page_nid = -1;
Peter Zijlstra90572892013-10-07 11:29:20 +01003866 int last_cpupid;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02003867 int target_nid;
Mel Gormanb8593bf2012-11-21 01:18:23 +00003868 bool migrated = false;
Aneesh Kumar K.Vcee216a2017-02-24 14:59:13 -08003869 pte_t pte;
Aneesh Kumar K.V288bc542017-02-24 14:59:16 -08003870 bool was_writable = pte_savedwrite(vmf->orig_pte);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003871 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02003872
3873 /*
Tobin C Harding166f61b2017-02-24 14:59:01 -08003874 * The "pte" at this point cannot be used safely without
3875 * validation through pte_unmap_same(). It's of NUMA type but
3876 * the pfn may be screwed if the read is non atomic.
Tobin C Harding166f61b2017-02-24 14:59:01 -08003877 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003878 vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd);
3879 spin_lock(vmf->ptl);
Aneesh Kumar K.Vcee216a2017-02-24 14:59:13 -08003880 if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003881 pte_unmap_unlock(vmf->pte, vmf->ptl);
Mel Gorman4daae3b2012-11-02 11:33:45 +00003882 goto out;
3883 }
3884
Aneesh Kumar K.Vcee216a2017-02-24 14:59:13 -08003885 /*
3886 * Make it present again, Depending on how arch implementes non
3887 * accessible ptes, some can allow access by kernel mode.
3888 */
3889 pte = ptep_modify_prot_start(vma->vm_mm, vmf->address, vmf->pte);
Mel Gorman4d942462015-02-12 14:58:28 -08003890 pte = pte_modify(pte, vma->vm_page_prot);
3891 pte = pte_mkyoung(pte);
Mel Gormanb191f9b2015-03-25 15:55:40 -07003892 if (was_writable)
3893 pte = pte_mkwrite(pte);
Aneesh Kumar K.Vcee216a2017-02-24 14:59:13 -08003894 ptep_modify_prot_commit(vma->vm_mm, vmf->address, vmf->pte, pte);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003895 update_mmu_cache(vma, vmf->address, vmf->pte);
Mel Gormand10e63f2012-10-25 14:16:31 +02003896
Jan Kara82b0f8c2016-12-14 15:06:58 -08003897 page = vm_normal_page(vma, vmf->address, pte);
Mel Gormand10e63f2012-10-25 14:16:31 +02003898 if (!page) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003899 pte_unmap_unlock(vmf->pte, vmf->ptl);
Mel Gormand10e63f2012-10-25 14:16:31 +02003900 return 0;
3901 }
3902
Kirill A. Shutemove81c4802016-01-15 16:53:49 -08003903 /* TODO: handle PTE-mapped THP */
3904 if (PageCompound(page)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003905 pte_unmap_unlock(vmf->pte, vmf->ptl);
Kirill A. Shutemove81c4802016-01-15 16:53:49 -08003906 return 0;
3907 }
3908
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003909 /*
Mel Gormanbea66fb2015-03-25 15:55:37 -07003910 * Avoid grouping on RO pages in general. RO pages shouldn't hurt as
3911 * much anyway since they can be in shared cache state. This misses
3912 * the case where a mapping is writable but the process never writes
3913 * to it but pte_write gets cleared during protection updates and
3914 * pte_dirty has unpredictable behaviour between PTE scan updates,
3915 * background writeback, dirty balancing and application behaviour.
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003916 */
Rik van Rield59dc7b2016-09-08 21:30:53 -04003917 if (!pte_write(pte))
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003918 flags |= TNF_NO_GROUP;
3919
Rik van Rieldabe1d92013-10-07 11:29:34 +01003920 /*
3921 * Flag if the page is shared between multiple address spaces. This
3922 * is later used when determining whether to group tasks together
3923 */
3924 if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED))
3925 flags |= TNF_SHARED;
3926
Peter Zijlstra90572892013-10-07 11:29:20 +01003927 last_cpupid = page_cpupid_last(page);
Mel Gorman8191acb2013-10-07 11:28:45 +01003928 page_nid = page_to_nid(page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003929 target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003930 &flags);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003931 pte_unmap_unlock(vmf->pte, vmf->ptl);
Mel Gorman4daae3b2012-11-02 11:33:45 +00003932 if (target_nid == -1) {
Mel Gorman4daae3b2012-11-02 11:33:45 +00003933 put_page(page);
3934 goto out;
3935 }
3936
3937 /* Migrate to the requested node */
Mel Gorman1bc115d2013-10-07 11:29:05 +01003938 migrated = migrate_misplaced_page(page, vma, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003939 if (migrated) {
Mel Gorman8191acb2013-10-07 11:28:45 +01003940 page_nid = target_nid;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003941 flags |= TNF_MIGRATED;
Mel Gorman074c2382015-03-25 15:55:42 -07003942 } else
3943 flags |= TNF_MIGRATE_FAIL;
Mel Gorman4daae3b2012-11-02 11:33:45 +00003944
3945out:
Mel Gorman8191acb2013-10-07 11:28:45 +01003946 if (page_nid != -1)
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003947 task_numa_fault(last_cpupid, page_nid, 1, flags);
Mel Gormand10e63f2012-10-25 14:16:31 +02003948 return 0;
3949}
3950
Souptick Joarder2b740302018-08-23 17:01:36 -07003951static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf)
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07003952{
Dave Jiangf4200392017-02-22 15:40:06 -08003953 if (vma_is_anonymous(vmf->vma))
Jan Kara82b0f8c2016-12-14 15:06:58 -08003954 return do_huge_pmd_anonymous_page(vmf);
Dave Jianga2d58162017-02-24 14:56:59 -08003955 if (vmf->vma->vm_ops->huge_fault)
Dave Jiangc791ace2017-02-24 14:57:08 -08003956 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07003957 return VM_FAULT_FALLBACK;
3958}
3959
Geert Uytterhoeven183f24a2017-12-14 15:32:52 -08003960/* `inline' is required to avoid gcc 4.1.2 build error */
Souptick Joarder2b740302018-08-23 17:01:36 -07003961static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd)
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07003962{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003963 if (vma_is_anonymous(vmf->vma))
3964 return do_huge_pmd_wp_page(vmf, orig_pmd);
Dave Jianga2d58162017-02-24 14:56:59 -08003965 if (vmf->vma->vm_ops->huge_fault)
Dave Jiangc791ace2017-02-24 14:57:08 -08003966 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);
Kirill A. Shutemovaf9e4d52016-07-26 15:25:40 -07003967
3968 /* COW handled on pte level: split pmd */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003969 VM_BUG_ON_VMA(vmf->vma->vm_flags & VM_SHARED, vmf->vma);
3970 __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL);
Kirill A. Shutemovaf9e4d52016-07-26 15:25:40 -07003971
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07003972 return VM_FAULT_FALLBACK;
3973}
3974
Lorenzo Stoakes38e08852016-09-11 23:54:25 +01003975static inline bool vma_is_accessible(struct vm_area_struct *vma)
3976{
3977 return vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE);
3978}
3979
Souptick Joarder2b740302018-08-23 17:01:36 -07003980static vm_fault_t create_huge_pud(struct vm_fault *vmf)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08003981{
3982#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3983 /* No support for anonymous transparent PUD pages yet */
3984 if (vma_is_anonymous(vmf->vma))
3985 return VM_FAULT_FALLBACK;
3986 if (vmf->vma->vm_ops->huge_fault)
Dave Jiangc791ace2017-02-24 14:57:08 -08003987 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08003988#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
3989 return VM_FAULT_FALLBACK;
3990}
3991
Souptick Joarder2b740302018-08-23 17:01:36 -07003992static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08003993{
3994#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3995 /* No support for anonymous transparent PUD pages yet */
3996 if (vma_is_anonymous(vmf->vma))
3997 return VM_FAULT_FALLBACK;
3998 if (vmf->vma->vm_ops->huge_fault)
Dave Jiangc791ace2017-02-24 14:57:08 -08003999 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004000#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
4001 return VM_FAULT_FALLBACK;
4002}
4003
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004/*
4005 * These routines also need to handle stuff like marking pages dirty
4006 * and/or accessed for architectures that don't do it in hardware (most
4007 * RISC architectures). The early dirtying is also good on the i386.
4008 *
4009 * There is also a hook called "update_mmu_cache()" that architectures
4010 * with external mmu caches can use to update those (ie the Sparc or
4011 * PowerPC hashed page tables that act as extended TLBs).
4012 *
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004013 * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow
4014 * concurrent faults).
Paul Cassella9a95f3c2014-08-06 16:07:24 -07004015 *
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004016 * The mmap_sem may have been released depending on flags and our return value.
4017 * See filemap_fault() and __lock_page_or_retry().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 */
Souptick Joarder2b740302018-08-23 17:01:36 -07004019static vm_fault_t handle_pte_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020{
4021 pte_t entry;
4022
Jan Kara82b0f8c2016-12-14 15:06:58 -08004023 if (unlikely(pmd_none(*vmf->pmd))) {
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004024 /*
4025 * Leave __pte_alloc() until later: because vm_ops->fault may
4026 * want to allocate huge page, and if we expose page table
4027 * for an instant, it will be difficult to retract from
4028 * concurrent faults and from rmap lookups.
4029 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08004030 vmf->pte = NULL;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004031 } else {
4032 /* See comment in pte_alloc_one_map() */
Ross Zwislerd0f0931d2017-06-02 14:46:34 -07004033 if (pmd_devmap_trans_unstable(vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004034 return 0;
4035 /*
4036 * A regular pmd is established and it can't morph into a huge
4037 * pmd from under us anymore at this point because we hold the
4038 * mmap_sem read mode and khugepaged takes it in write mode.
4039 * So now it's safe to run pte_offset_map().
4040 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08004041 vmf->pte = pte_offset_map(vmf->pmd, vmf->address);
Jan Kara29943022016-12-14 15:07:16 -08004042 vmf->orig_pte = *vmf->pte;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004043
4044 /*
4045 * some architectures can have larger ptes than wordsize,
4046 * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and
Paul E. McKenneyb03a0fe2017-10-23 14:07:25 -07004047 * CONFIG_32BIT=y, so READ_ONCE cannot guarantee atomic
4048 * accesses. The code below just needs a consistent view
4049 * for the ifs and we later double check anyway with the
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004050 * ptl lock held. So here a barrier will do.
4051 */
4052 barrier();
Jan Kara29943022016-12-14 15:07:16 -08004053 if (pte_none(vmf->orig_pte)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08004054 pte_unmap(vmf->pte);
4055 vmf->pte = NULL;
Hugh Dickins65500d22005-10-29 18:15:59 -07004056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 }
4058
Jan Kara82b0f8c2016-12-14 15:06:58 -08004059 if (!vmf->pte) {
4060 if (vma_is_anonymous(vmf->vma))
4061 return do_anonymous_page(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004062 else
Jan Kara82b0f8c2016-12-14 15:06:58 -08004063 return do_fault(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004064 }
4065
Jan Kara29943022016-12-14 15:07:16 -08004066 if (!pte_present(vmf->orig_pte))
4067 return do_swap_page(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004068
Jan Kara29943022016-12-14 15:07:16 -08004069 if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma))
4070 return do_numa_page(vmf);
Mel Gormand10e63f2012-10-25 14:16:31 +02004071
Jan Kara82b0f8c2016-12-14 15:06:58 -08004072 vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
4073 spin_lock(vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08004074 entry = vmf->orig_pte;
Jan Kara82b0f8c2016-12-14 15:06:58 -08004075 if (unlikely(!pte_same(*vmf->pte, entry)))
Hugh Dickins8f4e2102005-10-29 18:16:26 -07004076 goto unlock;
Jan Kara82b0f8c2016-12-14 15:06:58 -08004077 if (vmf->flags & FAULT_FLAG_WRITE) {
Linus Torvaldsf6f37322017-12-15 18:53:22 -08004078 if (!pte_write(entry))
Jan Kara29943022016-12-14 15:07:16 -08004079 return do_wp_page(vmf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 entry = pte_mkdirty(entry);
4081 }
4082 entry = pte_mkyoung(entry);
Jan Kara82b0f8c2016-12-14 15:06:58 -08004083 if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry,
4084 vmf->flags & FAULT_FLAG_WRITE)) {
4085 update_mmu_cache(vmf->vma, vmf->address, vmf->pte);
Andrea Arcangeli1a44e142005-10-29 18:16:48 -07004086 } else {
4087 /*
4088 * This is needed only for protection faults but the arch code
4089 * is not yet telling us if this is a protection fault or not.
4090 * This still avoids useless tlb flushes for .text page faults
4091 * with threads.
4092 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08004093 if (vmf->flags & FAULT_FLAG_WRITE)
4094 flush_tlb_fix_spurious_fault(vmf->vma, vmf->address);
Andrea Arcangeli1a44e142005-10-29 18:16:48 -07004095 }
Hugh Dickins8f4e2102005-10-29 18:16:26 -07004096unlock:
Jan Kara82b0f8c2016-12-14 15:06:58 -08004097 pte_unmap_unlock(vmf->pte, vmf->ptl);
Nick Piggin83c54072007-07-19 01:47:05 -07004098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099}
4100
4101/*
4102 * By the time we get here, we already hold the mm semaphore
Paul Cassella9a95f3c2014-08-06 16:07:24 -07004103 *
4104 * The mmap_sem may have been released depending on flags and our
4105 * return value. See filemap_fault() and __lock_page_or_retry().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 */
Souptick Joarder2b740302018-08-23 17:01:36 -07004107static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
4108 unsigned long address, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109{
Jan Kara82b0f8c2016-12-14 15:06:58 -08004110 struct vm_fault vmf = {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07004111 .vma = vma,
Jan Kara1a29d852016-12-14 15:07:01 -08004112 .address = address & PAGE_MASK,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07004113 .flags = flags,
Jan Kara0721ec82016-12-14 15:07:04 -08004114 .pgoff = linear_page_index(vma, address),
Jan Kara667240e2016-12-14 15:07:07 -08004115 .gfp_mask = __get_fault_gfp_mask(vma),
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07004116 };
Anshuman Khandualfde26be2017-09-08 16:12:45 -07004117 unsigned int dirty = flags & FAULT_FLAG_WRITE;
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -07004118 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004120 p4d_t *p4d;
Souptick Joarder2b740302018-08-23 17:01:36 -07004121 vm_fault_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 pgd = pgd_offset(mm, address);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004124 p4d = p4d_alloc(mm, pgd, address);
4125 if (!p4d)
4126 return VM_FAULT_OOM;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004127
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004128 vmf.pud = pud_alloc(mm, p4d, address);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004129 if (!vmf.pud)
Hugh Dickinsc74df322005-10-29 18:16:23 -07004130 return VM_FAULT_OOM;
Michal Hockoc76adee2018-12-28 00:38:21 -08004131 if (pud_none(*vmf.pud) && __transparent_hugepage_enabled(vma)) {
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004132 ret = create_huge_pud(&vmf);
4133 if (!(ret & VM_FAULT_FALLBACK))
4134 return ret;
4135 } else {
4136 pud_t orig_pud = *vmf.pud;
4137
4138 barrier();
4139 if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) {
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004140
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004141 /* NUMA case for anonymous PUDs would go here */
4142
Linus Torvaldsf6f37322017-12-15 18:53:22 -08004143 if (dirty && !pud_write(orig_pud)) {
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004144 ret = wp_huge_pud(&vmf, orig_pud);
4145 if (!(ret & VM_FAULT_FALLBACK))
4146 return ret;
4147 } else {
4148 huge_pud_set_accessed(&vmf, orig_pud);
4149 return 0;
4150 }
4151 }
4152 }
4153
4154 vmf.pmd = pmd_alloc(mm, vmf.pud, address);
Jan Kara82b0f8c2016-12-14 15:06:58 -08004155 if (!vmf.pmd)
Hugh Dickinsc74df322005-10-29 18:16:23 -07004156 return VM_FAULT_OOM;
Michal Hockoc76adee2018-12-28 00:38:21 -08004157 if (pmd_none(*vmf.pmd) && __transparent_hugepage_enabled(vma)) {
Dave Jianga2d58162017-02-24 14:56:59 -08004158 ret = create_huge_pmd(&vmf);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -07004159 if (!(ret & VM_FAULT_FALLBACK))
4160 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08004161 } else {
Jan Kara82b0f8c2016-12-14 15:06:58 -08004162 pmd_t orig_pmd = *vmf.pmd;
David Rientjes1f1d06c2012-05-29 15:06:23 -07004163
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08004164 barrier();
Zi Yan84c3fc42017-09-08 16:11:01 -07004165 if (unlikely(is_swap_pmd(orig_pmd))) {
4166 VM_BUG_ON(thp_migration_supported() &&
4167 !is_pmd_migration_entry(orig_pmd));
4168 if (is_pmd_migration_entry(orig_pmd))
4169 pmd_migration_entry_wait(mm, vmf.pmd);
4170 return 0;
4171 }
Dan Williams5c7fb562016-01-15 16:56:52 -08004172 if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) {
Lorenzo Stoakes38e08852016-09-11 23:54:25 +01004173 if (pmd_protnone(orig_pmd) && vma_is_accessible(vma))
Jan Kara82b0f8c2016-12-14 15:06:58 -08004174 return do_huge_pmd_numa_page(&vmf, orig_pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02004175
Linus Torvaldsf6f37322017-12-15 18:53:22 -08004176 if (dirty && !pmd_write(orig_pmd)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08004177 ret = wp_huge_pmd(&vmf, orig_pmd);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08004178 if (!(ret & VM_FAULT_FALLBACK))
4179 return ret;
Will Deacona1dd4502012-12-11 16:01:27 -08004180 } else {
Jan Kara82b0f8c2016-12-14 15:06:58 -08004181 huge_pmd_set_accessed(&vmf, orig_pmd);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08004182 return 0;
David Rientjes1f1d06c2012-05-29 15:06:23 -07004183 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08004184 }
4185 }
4186
Jan Kara82b0f8c2016-12-14 15:06:58 -08004187 return handle_pte_fault(&vmf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188}
4189
Paul Cassella9a95f3c2014-08-06 16:07:24 -07004190/*
4191 * By the time we get here, we already hold the mm semaphore
4192 *
4193 * The mmap_sem may have been released depending on flags and our
4194 * return value. See filemap_fault() and __lock_page_or_retry().
4195 */
Souptick Joarder2b740302018-08-23 17:01:36 -07004196vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -07004197 unsigned int flags)
Johannes Weiner519e5242013-09-12 15:13:42 -07004198{
Souptick Joarder2b740302018-08-23 17:01:36 -07004199 vm_fault_t ret;
Johannes Weiner519e5242013-09-12 15:13:42 -07004200
4201 __set_current_state(TASK_RUNNING);
4202
4203 count_vm_event(PGFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07004204 count_memcg_event_mm(vma->vm_mm, PGFAULT);
Johannes Weiner519e5242013-09-12 15:13:42 -07004205
4206 /* do counter updates before entering really critical section. */
4207 check_sync_rss_stat(current);
4208
Laurent Dufourde0c7992017-09-08 16:13:12 -07004209 if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
4210 flags & FAULT_FLAG_INSTRUCTION,
4211 flags & FAULT_FLAG_REMOTE))
4212 return VM_FAULT_SIGSEGV;
4213
Johannes Weiner519e5242013-09-12 15:13:42 -07004214 /*
4215 * Enable the memcg OOM handling for faults triggered in user
4216 * space. Kernel faults are handled more gracefully.
4217 */
4218 if (flags & FAULT_FLAG_USER)
Michal Hocko29ef6802018-08-17 15:47:11 -07004219 mem_cgroup_enter_user_fault();
Johannes Weiner519e5242013-09-12 15:13:42 -07004220
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07004221 if (unlikely(is_vm_hugetlb_page(vma)))
4222 ret = hugetlb_fault(vma->vm_mm, vma, address, flags);
4223 else
4224 ret = __handle_mm_fault(vma, address, flags);
Johannes Weiner519e5242013-09-12 15:13:42 -07004225
Johannes Weiner49426422013-10-16 13:46:59 -07004226 if (flags & FAULT_FLAG_USER) {
Michal Hocko29ef6802018-08-17 15:47:11 -07004227 mem_cgroup_exit_user_fault();
Tobin C Harding166f61b2017-02-24 14:59:01 -08004228 /*
4229 * The task may have entered a memcg OOM situation but
4230 * if the allocation error was handled gracefully (no
4231 * VM_FAULT_OOM), there is no need to kill anything.
4232 * Just clean up the OOM state peacefully.
4233 */
4234 if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
4235 mem_cgroup_oom_synchronize(false);
Johannes Weiner49426422013-10-16 13:46:59 -07004236 }
Johannes Weiner3812c8c2013-09-12 15:13:44 -07004237
Johannes Weiner519e5242013-09-12 15:13:42 -07004238 return ret;
4239}
Jesse Barnese1d6d012014-12-12 16:55:27 -08004240EXPORT_SYMBOL_GPL(handle_mm_fault);
Johannes Weiner519e5242013-09-12 15:13:42 -07004241
Kirill A. Shutemov90eceff2017-03-09 17:24:08 +03004242#ifndef __PAGETABLE_P4D_FOLDED
4243/*
4244 * Allocate p4d page table.
4245 * We've already handled the fast-path in-line.
4246 */
4247int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
4248{
4249 p4d_t *new = p4d_alloc_one(mm, address);
4250 if (!new)
4251 return -ENOMEM;
4252
4253 smp_wmb(); /* See comment in __pte_alloc */
4254
4255 spin_lock(&mm->page_table_lock);
4256 if (pgd_present(*pgd)) /* Another has populated it */
4257 p4d_free(mm, new);
4258 else
4259 pgd_populate(mm, pgd, new);
4260 spin_unlock(&mm->page_table_lock);
4261 return 0;
4262}
4263#endif /* __PAGETABLE_P4D_FOLDED */
4264
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265#ifndef __PAGETABLE_PUD_FOLDED
4266/*
4267 * Allocate page upper directory.
Hugh Dickins872fec12005-10-29 18:16:21 -07004268 * We've already handled the fast-path in-line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 */
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004270int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
Hugh Dickinsc74df322005-10-29 18:16:23 -07004272 pud_t *new = pud_alloc_one(mm, address);
4273 if (!new)
Hugh Dickins1bb36302005-10-29 18:16:22 -07004274 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
Nick Piggin362a61a2008-05-14 06:37:36 +02004276 smp_wmb(); /* See comment in __pte_alloc */
4277
Hugh Dickins872fec12005-10-29 18:16:21 -07004278 spin_lock(&mm->page_table_lock);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004279#ifndef __ARCH_HAS_5LEVEL_HACK
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -08004280 if (!p4d_present(*p4d)) {
4281 mm_inc_nr_puds(mm);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004282 p4d_populate(mm, p4d, new);
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -08004283 } else /* Another has populated it */
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004284 pud_free(mm, new);
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -08004285#else
4286 if (!pgd_present(*p4d)) {
4287 mm_inc_nr_puds(mm);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004288 pgd_populate(mm, p4d, new);
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -08004289 } else /* Another has populated it */
4290 pud_free(mm, new);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004291#endif /* __ARCH_HAS_5LEVEL_HACK */
Hugh Dickinsc74df322005-10-29 18:16:23 -07004292 spin_unlock(&mm->page_table_lock);
Hugh Dickins1bb36302005-10-29 18:16:22 -07004293 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294}
4295#endif /* __PAGETABLE_PUD_FOLDED */
4296
4297#ifndef __PAGETABLE_PMD_FOLDED
4298/*
4299 * Allocate page middle directory.
Hugh Dickins872fec12005-10-29 18:16:21 -07004300 * We've already handled the fast-path in-line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 */
Hugh Dickins1bb36302005-10-29 18:16:22 -07004302int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303{
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004304 spinlock_t *ptl;
Hugh Dickinsc74df322005-10-29 18:16:23 -07004305 pmd_t *new = pmd_alloc_one(mm, address);
4306 if (!new)
Hugh Dickins1bb36302005-10-29 18:16:22 -07004307 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
Nick Piggin362a61a2008-05-14 06:37:36 +02004309 smp_wmb(); /* See comment in __pte_alloc */
4310
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004311 ptl = pud_lock(mm, pud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312#ifndef __ARCH_HAS_4LEVEL_HACK
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004313 if (!pud_present(*pud)) {
4314 mm_inc_nr_pmds(mm);
Hugh Dickins1bb36302005-10-29 18:16:22 -07004315 pud_populate(mm, pud, new);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004316 } else /* Another has populated it */
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -08004317 pmd_free(mm, new);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004318#else
4319 if (!pgd_present(*pud)) {
4320 mm_inc_nr_pmds(mm);
Hugh Dickins1bb36302005-10-29 18:16:22 -07004321 pgd_populate(mm, pud, new);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004322 } else /* Another has populated it */
4323 pmd_free(mm, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324#endif /* __ARCH_HAS_4LEVEL_HACK */
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004325 spin_unlock(ptl);
Hugh Dickins1bb36302005-10-29 18:16:22 -07004326 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327}
4328#endif /* __PAGETABLE_PMD_FOLDED */
4329
Ross Zwisler09796392017-01-10 16:57:21 -08004330static int __follow_pte_pmd(struct mm_struct *mm, unsigned long address,
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004331 unsigned long *start, unsigned long *end,
4332 pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp)
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004333{
4334 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004335 p4d_t *p4d;
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004336 pud_t *pud;
4337 pmd_t *pmd;
4338 pte_t *ptep;
4339
4340 pgd = pgd_offset(mm, address);
4341 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
4342 goto out;
4343
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004344 p4d = p4d_offset(pgd, address);
4345 if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d)))
4346 goto out;
4347
4348 pud = pud_offset(p4d, address);
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004349 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
4350 goto out;
4351
4352 pmd = pmd_offset(pud, address);
Andrea Arcangelif66055ab2011-01-13 15:46:54 -08004353 VM_BUG_ON(pmd_trans_huge(*pmd));
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004354
Ross Zwisler09796392017-01-10 16:57:21 -08004355 if (pmd_huge(*pmd)) {
4356 if (!pmdpp)
4357 goto out;
4358
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004359 if (start && end) {
4360 *start = address & PMD_MASK;
4361 *end = *start + PMD_SIZE;
4362 mmu_notifier_invalidate_range_start(mm, *start, *end);
4363 }
Ross Zwisler09796392017-01-10 16:57:21 -08004364 *ptlp = pmd_lock(mm, pmd);
4365 if (pmd_huge(*pmd)) {
4366 *pmdpp = pmd;
4367 return 0;
4368 }
4369 spin_unlock(*ptlp);
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004370 if (start && end)
4371 mmu_notifier_invalidate_range_end(mm, *start, *end);
Ross Zwisler09796392017-01-10 16:57:21 -08004372 }
4373
4374 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004375 goto out;
4376
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004377 if (start && end) {
4378 *start = address & PAGE_MASK;
4379 *end = *start + PAGE_SIZE;
4380 mmu_notifier_invalidate_range_start(mm, *start, *end);
4381 }
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004382 ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004383 if (!pte_present(*ptep))
4384 goto unlock;
4385 *ptepp = ptep;
4386 return 0;
4387unlock:
4388 pte_unmap_unlock(ptep, *ptlp);
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004389 if (start && end)
4390 mmu_notifier_invalidate_range_end(mm, *start, *end);
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004391out:
4392 return -EINVAL;
4393}
4394
Ross Zwislerf729c8c2017-01-10 16:57:24 -08004395static inline int follow_pte(struct mm_struct *mm, unsigned long address,
4396 pte_t **ptepp, spinlock_t **ptlp)
Namhyung Kim1b36ba82010-10-26 14:22:00 -07004397{
4398 int res;
4399
4400 /* (void) is needed to make gcc happy */
4401 (void) __cond_lock(*ptlp,
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004402 !(res = __follow_pte_pmd(mm, address, NULL, NULL,
4403 ptepp, NULL, ptlp)));
Namhyung Kim1b36ba82010-10-26 14:22:00 -07004404 return res;
4405}
4406
Ross Zwisler09796392017-01-10 16:57:21 -08004407int follow_pte_pmd(struct mm_struct *mm, unsigned long address,
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004408 unsigned long *start, unsigned long *end,
Ross Zwisler09796392017-01-10 16:57:21 -08004409 pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp)
4410{
4411 int res;
4412
4413 /* (void) is needed to make gcc happy */
4414 (void) __cond_lock(*ptlp,
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004415 !(res = __follow_pte_pmd(mm, address, start, end,
4416 ptepp, pmdpp, ptlp)));
Ross Zwisler09796392017-01-10 16:57:21 -08004417 return res;
4418}
4419EXPORT_SYMBOL(follow_pte_pmd);
4420
Johannes Weiner3b6748e2009-06-16 15:32:35 -07004421/**
4422 * follow_pfn - look up PFN at a user virtual address
4423 * @vma: memory mapping
4424 * @address: user virtual address
4425 * @pfn: location to store found PFN
4426 *
4427 * Only IO mappings and raw PFN mappings are allowed.
4428 *
4429 * Returns zero and the pfn at @pfn on success, -ve otherwise.
4430 */
4431int follow_pfn(struct vm_area_struct *vma, unsigned long address,
4432 unsigned long *pfn)
4433{
4434 int ret = -EINVAL;
4435 spinlock_t *ptl;
4436 pte_t *ptep;
4437
4438 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
4439 return ret;
4440
4441 ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
4442 if (ret)
4443 return ret;
4444 *pfn = pte_pfn(*ptep);
4445 pte_unmap_unlock(ptep, ptl);
4446 return 0;
4447}
4448EXPORT_SYMBOL(follow_pfn);
4449
Rik van Riel28b2ee22008-07-23 21:27:05 -07004450#ifdef CONFIG_HAVE_IOREMAP_PROT
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004451int follow_phys(struct vm_area_struct *vma,
4452 unsigned long address, unsigned int flags,
4453 unsigned long *prot, resource_size_t *phys)
Rik van Riel28b2ee22008-07-23 21:27:05 -07004454{
Johannes Weiner03668a42009-06-16 15:32:34 -07004455 int ret = -EINVAL;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004456 pte_t *ptep, pte;
4457 spinlock_t *ptl;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004458
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004459 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
4460 goto out;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004461
Johannes Weiner03668a42009-06-16 15:32:34 -07004462 if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004463 goto out;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004464 pte = *ptep;
Johannes Weiner03668a42009-06-16 15:32:34 -07004465
Linus Torvaldsf6f37322017-12-15 18:53:22 -08004466 if ((flags & FOLL_WRITE) && !pte_write(pte))
Rik van Riel28b2ee22008-07-23 21:27:05 -07004467 goto unlock;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004468
4469 *prot = pgprot_val(pte_pgprot(pte));
Johannes Weiner03668a42009-06-16 15:32:34 -07004470 *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004471
Johannes Weiner03668a42009-06-16 15:32:34 -07004472 ret = 0;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004473unlock:
4474 pte_unmap_unlock(ptep, ptl);
4475out:
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004476 return ret;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004477}
4478
4479int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
4480 void *buf, int len, int write)
4481{
4482 resource_size_t phys_addr;
4483 unsigned long prot = 0;
KOSAKI Motohiro2bc72732009-01-06 14:39:43 -08004484 void __iomem *maddr;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004485 int offset = addr & (PAGE_SIZE-1);
4486
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004487 if (follow_phys(vma, addr, write, &prot, &phys_addr))
Rik van Riel28b2ee22008-07-23 21:27:05 -07004488 return -EINVAL;
4489
Grazvydas Ignotas9cb12d72015-02-12 15:00:19 -08004490 maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot);
jie@chenjie6@huwei.com24eee1e2018-08-10 17:23:06 -07004491 if (!maddr)
4492 return -ENOMEM;
4493
Rik van Riel28b2ee22008-07-23 21:27:05 -07004494 if (write)
4495 memcpy_toio(maddr + offset, buf, len);
4496 else
4497 memcpy_fromio(buf, maddr + offset, len);
4498 iounmap(maddr);
4499
4500 return len;
4501}
Uwe Kleine-König5a736332013-08-07 13:02:52 +02004502EXPORT_SYMBOL_GPL(generic_access_phys);
Rik van Riel28b2ee22008-07-23 21:27:05 -07004503#endif
4504
David Howells0ec76a12006-09-27 01:50:15 -07004505/*
Stephen Wilson206cb632011-03-13 15:49:19 -04004506 * Access another process' address space as given in mm. If non-NULL, use the
4507 * given task for page fault accounting.
David Howells0ec76a12006-09-27 01:50:15 -07004508 */
Eric W. Biederman84d77d32016-11-22 12:06:50 -06004509int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
Lorenzo Stoakes442486e2016-10-13 01:20:18 +01004510 unsigned long addr, void *buf, int len, unsigned int gup_flags)
David Howells0ec76a12006-09-27 01:50:15 -07004511{
David Howells0ec76a12006-09-27 01:50:15 -07004512 struct vm_area_struct *vma;
David Howells0ec76a12006-09-27 01:50:15 -07004513 void *old_buf = buf;
Lorenzo Stoakes442486e2016-10-13 01:20:18 +01004514 int write = gup_flags & FOLL_WRITE;
David Howells0ec76a12006-09-27 01:50:15 -07004515
Konstantin Khlebnikovb0768722019-07-11 21:00:07 -07004516 if (down_read_killable(&mm->mmap_sem))
4517 return 0;
4518
Simon Arlott183ff222007-10-20 01:27:18 +02004519 /* ignore errors, just check how much was successfully transferred */
David Howells0ec76a12006-09-27 01:50:15 -07004520 while (len) {
4521 int bytes, ret, offset;
4522 void *maddr;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004523 struct page *page = NULL;
David Howells0ec76a12006-09-27 01:50:15 -07004524
Dave Hansen1e987792016-02-12 13:01:54 -08004525 ret = get_user_pages_remote(tsk, mm, addr, 1,
Lorenzo Stoakes5b56d492016-12-14 15:06:52 -08004526 gup_flags, &page, &vma, NULL);
Rik van Riel28b2ee22008-07-23 21:27:05 -07004527 if (ret <= 0) {
Rik van Rieldbffcd02014-08-06 16:08:12 -07004528#ifndef CONFIG_HAVE_IOREMAP_PROT
4529 break;
4530#else
Rik van Riel28b2ee22008-07-23 21:27:05 -07004531 /*
4532 * Check if this is a VM_IO | VM_PFNMAP VMA, which
4533 * we can access using slightly different code.
4534 */
Rik van Riel28b2ee22008-07-23 21:27:05 -07004535 vma = find_vma(mm, addr);
Michael Ellermanfe936df2011-04-14 15:22:10 -07004536 if (!vma || vma->vm_start > addr)
Rik van Riel28b2ee22008-07-23 21:27:05 -07004537 break;
4538 if (vma->vm_ops && vma->vm_ops->access)
4539 ret = vma->vm_ops->access(vma, addr, buf,
4540 len, write);
4541 if (ret <= 0)
Rik van Riel28b2ee22008-07-23 21:27:05 -07004542 break;
4543 bytes = ret;
Rik van Rieldbffcd02014-08-06 16:08:12 -07004544#endif
David Howells0ec76a12006-09-27 01:50:15 -07004545 } else {
Rik van Riel28b2ee22008-07-23 21:27:05 -07004546 bytes = len;
4547 offset = addr & (PAGE_SIZE-1);
4548 if (bytes > PAGE_SIZE-offset)
4549 bytes = PAGE_SIZE-offset;
4550
4551 maddr = kmap(page);
4552 if (write) {
4553 copy_to_user_page(vma, page, addr,
4554 maddr + offset, buf, bytes);
4555 set_page_dirty_lock(page);
4556 } else {
4557 copy_from_user_page(vma, page, addr,
4558 buf, maddr + offset, bytes);
4559 }
4560 kunmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004561 put_page(page);
David Howells0ec76a12006-09-27 01:50:15 -07004562 }
David Howells0ec76a12006-09-27 01:50:15 -07004563 len -= bytes;
4564 buf += bytes;
4565 addr += bytes;
4566 }
4567 up_read(&mm->mmap_sem);
David Howells0ec76a12006-09-27 01:50:15 -07004568
4569 return buf - old_buf;
4570}
Andi Kleen03252912008-01-30 13:33:18 +01004571
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04004572/**
Randy Dunlapae91dbf2011-03-26 13:27:01 -07004573 * access_remote_vm - access another process' address space
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04004574 * @mm: the mm_struct of the target address space
4575 * @addr: start address to access
4576 * @buf: source or destination buffer
4577 * @len: number of bytes to transfer
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01004578 * @gup_flags: flags modifying lookup behaviour
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04004579 *
4580 * The caller must hold a reference on @mm.
4581 */
4582int access_remote_vm(struct mm_struct *mm, unsigned long addr,
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01004583 void *buf, int len, unsigned int gup_flags)
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04004584{
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01004585 return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags);
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04004586}
4587
Andi Kleen03252912008-01-30 13:33:18 +01004588/*
Stephen Wilson206cb632011-03-13 15:49:19 -04004589 * Access another process' address space.
4590 * Source/target buffer must be kernel space,
4591 * Do not walk the page table directly, use get_user_pages
4592 */
4593int access_process_vm(struct task_struct *tsk, unsigned long addr,
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +01004594 void *buf, int len, unsigned int gup_flags)
Stephen Wilson206cb632011-03-13 15:49:19 -04004595{
4596 struct mm_struct *mm;
4597 int ret;
4598
4599 mm = get_task_mm(tsk);
4600 if (!mm)
4601 return 0;
4602
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +01004603 ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags);
Lorenzo Stoakes442486e2016-10-13 01:20:18 +01004604
Stephen Wilson206cb632011-03-13 15:49:19 -04004605 mmput(mm);
4606
4607 return ret;
4608}
Catalin Marinasfcd35852016-11-01 14:43:25 -07004609EXPORT_SYMBOL_GPL(access_process_vm);
Stephen Wilson206cb632011-03-13 15:49:19 -04004610
Andi Kleen03252912008-01-30 13:33:18 +01004611/*
4612 * Print the name of a VMA.
4613 */
4614void print_vma_addr(char *prefix, unsigned long ip)
4615{
4616 struct mm_struct *mm = current->mm;
4617 struct vm_area_struct *vma;
4618
Ingo Molnare8bff742008-02-13 20:21:06 +01004619 /*
Michal Hocko0a7f6822017-11-15 17:38:59 -08004620 * we might be running from an atomic context so we cannot sleep
Ingo Molnare8bff742008-02-13 20:21:06 +01004621 */
Michal Hocko0a7f6822017-11-15 17:38:59 -08004622 if (!down_read_trylock(&mm->mmap_sem))
Ingo Molnare8bff742008-02-13 20:21:06 +01004623 return;
4624
Andi Kleen03252912008-01-30 13:33:18 +01004625 vma = find_vma(mm, ip);
4626 if (vma && vma->vm_file) {
4627 struct file *f = vma->vm_file;
Michal Hocko0a7f6822017-11-15 17:38:59 -08004628 char *buf = (char *)__get_free_page(GFP_NOWAIT);
Andi Kleen03252912008-01-30 13:33:18 +01004629 if (buf) {
Andy Shevchenko2fbc57c2012-12-17 16:01:23 -08004630 char *p;
Andi Kleen03252912008-01-30 13:33:18 +01004631
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02004632 p = file_path(f, buf, PAGE_SIZE);
Andi Kleen03252912008-01-30 13:33:18 +01004633 if (IS_ERR(p))
4634 p = "?";
Andy Shevchenko2fbc57c2012-12-17 16:01:23 -08004635 printk("%s%s[%lx+%lx]", prefix, kbasename(p),
Andi Kleen03252912008-01-30 13:33:18 +01004636 vma->vm_start,
4637 vma->vm_end - vma->vm_start);
4638 free_page((unsigned long)buf);
4639 }
4640 }
Jeff Liu51a07e52012-07-31 16:43:18 -07004641 up_read(&mm->mmap_sem);
Andi Kleen03252912008-01-30 13:33:18 +01004642}
Nick Piggin3ee1afa2008-09-10 13:37:17 +02004643
Michael S. Tsirkin662bbcb2013-05-26 17:32:23 +03004644#if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
David Hildenbrand9ec23532015-05-11 17:52:07 +02004645void __might_fault(const char *file, int line)
Nick Piggin3ee1afa2008-09-10 13:37:17 +02004646{
Peter Zijlstra95156f02009-01-12 13:02:11 +01004647 /*
4648 * Some code (nfs/sunrpc) uses socket ops on kernel memory while
4649 * holding the mmap_sem, this is safe because kernel memory doesn't
4650 * get paged out, therefore we'll never actually fault, and the
4651 * below annotations will generate false positives.
4652 */
Al Virodb68ce12017-03-20 21:08:07 -04004653 if (uaccess_kernel())
Peter Zijlstra95156f02009-01-12 13:02:11 +01004654 return;
David Hildenbrand9ec23532015-05-11 17:52:07 +02004655 if (pagefault_disabled())
Michael S. Tsirkin662bbcb2013-05-26 17:32:23 +03004656 return;
David Hildenbrand9ec23532015-05-11 17:52:07 +02004657 __might_sleep(file, line, 0);
4658#if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
Michael S. Tsirkin662bbcb2013-05-26 17:32:23 +03004659 if (current->mm)
Nick Piggin3ee1afa2008-09-10 13:37:17 +02004660 might_lock_read(&current->mm->mmap_sem);
David Hildenbrand9ec23532015-05-11 17:52:07 +02004661#endif
Nick Piggin3ee1afa2008-09-10 13:37:17 +02004662}
David Hildenbrand9ec23532015-05-11 17:52:07 +02004663EXPORT_SYMBOL(__might_fault);
Nick Piggin3ee1afa2008-09-10 13:37:17 +02004664#endif
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004665
4666#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
Huang Yingc6ddfb62018-08-17 15:45:46 -07004667/*
4668 * Process all subpages of the specified huge page with the specified
4669 * operation. The target subpage will be processed last to keep its
4670 * cache lines hot.
4671 */
4672static inline void process_huge_page(
4673 unsigned long addr_hint, unsigned int pages_per_huge_page,
4674 void (*process_subpage)(unsigned long addr, int idx, void *arg),
4675 void *arg)
4676{
4677 int i, n, base, l;
4678 unsigned long addr = addr_hint &
4679 ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
4680
4681 /* Process target subpage last to keep its cache lines hot */
4682 might_sleep();
4683 n = (addr_hint - addr) / PAGE_SIZE;
4684 if (2 * n <= pages_per_huge_page) {
4685 /* If target subpage in first half of huge page */
4686 base = 0;
4687 l = n;
4688 /* Process subpages at the end of huge page */
4689 for (i = pages_per_huge_page - 1; i >= 2 * n; i--) {
4690 cond_resched();
4691 process_subpage(addr + i * PAGE_SIZE, i, arg);
4692 }
4693 } else {
4694 /* If target subpage in second half of huge page */
4695 base = pages_per_huge_page - 2 * (pages_per_huge_page - n);
4696 l = pages_per_huge_page - n;
4697 /* Process subpages at the begin of huge page */
4698 for (i = 0; i < base; i++) {
4699 cond_resched();
4700 process_subpage(addr + i * PAGE_SIZE, i, arg);
4701 }
4702 }
4703 /*
4704 * Process remaining subpages in left-right-left-right pattern
4705 * towards the target subpage
4706 */
4707 for (i = 0; i < l; i++) {
4708 int left_idx = base + i;
4709 int right_idx = base + 2 * l - 1 - i;
4710
4711 cond_resched();
4712 process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg);
4713 cond_resched();
4714 process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg);
4715 }
4716}
4717
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004718static void clear_gigantic_page(struct page *page,
4719 unsigned long addr,
4720 unsigned int pages_per_huge_page)
4721{
4722 int i;
4723 struct page *p = page;
4724
4725 might_sleep();
4726 for (i = 0; i < pages_per_huge_page;
4727 i++, p = mem_map_next(p, page, i)) {
4728 cond_resched();
4729 clear_user_highpage(p, addr + i * PAGE_SIZE);
4730 }
4731}
Huang Yingc6ddfb62018-08-17 15:45:46 -07004732
4733static void clear_subpage(unsigned long addr, int idx, void *arg)
4734{
4735 struct page *page = arg;
4736
4737 clear_user_highpage(page + idx, addr);
4738}
4739
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004740void clear_huge_page(struct page *page,
Huang Yingc79b57e2017-09-06 16:25:04 -07004741 unsigned long addr_hint, unsigned int pages_per_huge_page)
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004742{
Huang Yingc79b57e2017-09-06 16:25:04 -07004743 unsigned long addr = addr_hint &
4744 ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004745
4746 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
4747 clear_gigantic_page(page, addr, pages_per_huge_page);
4748 return;
4749 }
4750
Huang Yingc6ddfb62018-08-17 15:45:46 -07004751 process_huge_page(addr_hint, pages_per_huge_page, clear_subpage, page);
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004752}
4753
4754static void copy_user_gigantic_page(struct page *dst, struct page *src,
4755 unsigned long addr,
4756 struct vm_area_struct *vma,
4757 unsigned int pages_per_huge_page)
4758{
4759 int i;
4760 struct page *dst_base = dst;
4761 struct page *src_base = src;
4762
4763 for (i = 0; i < pages_per_huge_page; ) {
4764 cond_resched();
4765 copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
4766
4767 i++;
4768 dst = mem_map_next(dst, dst_base, i);
4769 src = mem_map_next(src, src_base, i);
4770 }
4771}
4772
Huang Yingc9f4cd72018-08-17 15:45:49 -07004773struct copy_subpage_arg {
4774 struct page *dst;
4775 struct page *src;
4776 struct vm_area_struct *vma;
4777};
4778
4779static void copy_subpage(unsigned long addr, int idx, void *arg)
4780{
4781 struct copy_subpage_arg *copy_arg = arg;
4782
4783 copy_user_highpage(copy_arg->dst + idx, copy_arg->src + idx,
4784 addr, copy_arg->vma);
4785}
4786
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004787void copy_user_huge_page(struct page *dst, struct page *src,
Huang Yingc9f4cd72018-08-17 15:45:49 -07004788 unsigned long addr_hint, struct vm_area_struct *vma,
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004789 unsigned int pages_per_huge_page)
4790{
Huang Yingc9f4cd72018-08-17 15:45:49 -07004791 unsigned long addr = addr_hint &
4792 ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
4793 struct copy_subpage_arg arg = {
4794 .dst = dst,
4795 .src = src,
4796 .vma = vma,
4797 };
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004798
4799 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
4800 copy_user_gigantic_page(dst, src, addr, vma,
4801 pages_per_huge_page);
4802 return;
4803 }
4804
Huang Yingc9f4cd72018-08-17 15:45:49 -07004805 process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg);
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004806}
Mike Kravetzfa4d75c2017-02-22 15:42:49 -08004807
4808long copy_huge_page_from_user(struct page *dst_page,
4809 const void __user *usr_src,
Mike Kravetz810a56b2017-02-22 15:42:58 -08004810 unsigned int pages_per_huge_page,
4811 bool allow_pagefault)
Mike Kravetzfa4d75c2017-02-22 15:42:49 -08004812{
4813 void *src = (void *)usr_src;
4814 void *page_kaddr;
4815 unsigned long i, rc = 0;
4816 unsigned long ret_val = pages_per_huge_page * PAGE_SIZE;
4817
4818 for (i = 0; i < pages_per_huge_page; i++) {
Mike Kravetz810a56b2017-02-22 15:42:58 -08004819 if (allow_pagefault)
4820 page_kaddr = kmap(dst_page + i);
4821 else
4822 page_kaddr = kmap_atomic(dst_page + i);
Mike Kravetzfa4d75c2017-02-22 15:42:49 -08004823 rc = copy_from_user(page_kaddr,
4824 (const void __user *)(src + i * PAGE_SIZE),
4825 PAGE_SIZE);
Mike Kravetz810a56b2017-02-22 15:42:58 -08004826 if (allow_pagefault)
4827 kunmap(dst_page + i);
4828 else
4829 kunmap_atomic(page_kaddr);
Mike Kravetzfa4d75c2017-02-22 15:42:49 -08004830
4831 ret_val -= (PAGE_SIZE - rc);
4832 if (rc)
4833 break;
4834
4835 cond_resched();
4836 }
4837 return ret_val;
4838}
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004839#endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08004840
Olof Johansson40b64ac2013-12-20 14:28:05 -08004841#if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08004842
4843static struct kmem_cache *page_ptl_cachep;
4844
4845void __init ptlock_cache_init(void)
4846{
4847 page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0,
4848 SLAB_PANIC, NULL);
4849}
4850
Peter Zijlstra539edb52013-11-14 14:31:52 -08004851bool ptlock_alloc(struct page *page)
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08004852{
4853 spinlock_t *ptl;
4854
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08004855 ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL);
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08004856 if (!ptl)
4857 return false;
Peter Zijlstra539edb52013-11-14 14:31:52 -08004858 page->ptl = ptl;
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08004859 return true;
4860}
4861
Peter Zijlstra539edb52013-11-14 14:31:52 -08004862void ptlock_free(struct page *page)
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08004863{
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08004864 kmem_cache_free(page_ptl_cachep, page->ptl);
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08004865}
4866#endif