blob: ece007d30a006f541f91c6997121899089d6fd28 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/mm/memory.c
4 *
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
6 */
7
8/*
9 * demand-loading started 01.12.91 - seems it is high on the list of
10 * things wanted, and it should be easy to implement. - Linus
11 */
12
13/*
14 * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
15 * pages started 02.12.91, seems to work. - Linus.
16 *
17 * Tested sharing by executing about 30 /bin/sh: under the old kernel it
18 * would have taken more than the 6M I have free, but it worked well as
19 * far as I could see.
20 *
21 * Also corrected some "invalidate()"s - I wasn't doing enough of them.
22 */
23
24/*
25 * Real VM (paging to/from disk) started 18.12.91. Much more work and
26 * thought has to go into this. Oh, well..
27 * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why.
28 * Found it. Everything seems to work now.
29 * 20.12.91 - Ok, making the swap-device changeable like the root.
30 */
31
32/*
33 * 05.04.94 - Multi-page memory management added for v1.1.
Tobin C Harding166f61b2017-02-24 14:59:01 -080034 * Idea by Alex Bligh (alex@cconcepts.co.uk)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 *
36 * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG
37 * (Gerhard.Wichert@pdb.siemens.de)
38 *
39 * Aug/Sep 2004 Changed to four level page tables (Andi Kleen)
40 */
41
42#include <linux/kernel_stat.h>
43#include <linux/mm.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010044#include <linux/sched/mm.h>
Ingo Molnarf7ccbae2017-02-08 18:51:30 +010045#include <linux/sched/coredump.h>
Ingo Molnar6a3827d2017-02-08 18:51:31 +010046#include <linux/sched/numa_balancing.h>
Ingo Molnar29930022017-02-08 18:51:36 +010047#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/hugetlb.h>
49#include <linux/mman.h>
50#include <linux/swap.h>
51#include <linux/highmem.h>
52#include <linux/pagemap.h>
Jérôme Glisse5042db42017-09-08 16:11:43 -070053#include <linux/memremap.h>
Hugh Dickins9a840892009-09-21 17:02:01 -070054#include <linux/ksm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/rmap.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040056#include <linux/export.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070057#include <linux/delayacct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/init.h>
Dan Williams01c8f1c2016-01-15 16:56:40 -080059#include <linux/pfn_t.h>
Peter Zijlstraedc79b22006-09-25 23:30:58 -070060#include <linux/writeback.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080061#include <linux/memcontrol.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070062#include <linux/mmu_notifier.h>
Hugh Dickins3dc14742009-01-06 14:40:08 -080063#include <linux/swapops.h>
64#include <linux/elf.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090065#include <linux/gfp.h>
Mel Gorman4daae3b2012-11-02 11:33:45 +000066#include <linux/migrate.h>
Andy Shevchenko2fbc57c2012-12-17 16:01:23 -080067#include <linux/string.h>
Dan Williams0abdd7a2014-01-21 15:48:12 -080068#include <linux/dma-debug.h>
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -070069#include <linux/debugfs.h>
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -070070#include <linux/userfaultfd_k.h>
Jan Karabc2466e2016-05-12 18:29:19 +020071#include <linux/dax.h>
Michal Hocko6b31d592017-08-18 15:16:15 -070072#include <linux/oom.h>
Anshuman Khandual98fa15f2019-03-05 15:42:58 -080073#include <linux/numa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Joel Fernandes (Google)b3d14112019-11-30 17:50:30 -080075#include <trace/events/kmem.h>
76
Alexey Dobriyan6952b612009-09-18 23:55:55 +040077#include <asm/io.h>
Dave Hansen33a709b2016-02-12 13:02:19 -080078#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <asm/pgalloc.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080080#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <asm/tlb.h>
82#include <asm/tlbflush.h>
83#include <asm/pgtable.h>
84
Jan Beulich42b77722008-07-23 21:27:10 -070085#include "internal.h"
86
Arnd Bergmannaf27d942018-02-16 16:25:53 +010087#if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST)
Peter Zijlstra90572892013-10-07 11:29:20 +010088#warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
Peter Zijlstra75980e92013-02-22 16:34:32 -080089#endif
90
Andy Whitcroftd41dee32005-06-23 00:07:54 -070091#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* use the per-pgdat data instead for discontigmem - mbligh */
93unsigned long max_mapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094EXPORT_SYMBOL(max_mapnr);
Tobin C Harding166f61b2017-02-24 14:59:01 -080095
96struct page *mem_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097EXPORT_SYMBOL(mem_map);
98#endif
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
101 * A number of key systems in x86 including ioremap() rely on the assumption
102 * that high_memory defines the upper bound on direct map memory, then end
103 * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
104 * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
105 * and ZONE_HIGHMEM.
106 */
Tobin C Harding166f61b2017-02-24 14:59:01 -0800107void *high_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108EXPORT_SYMBOL(high_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Ingo Molnar32a93232008-02-06 22:39:44 +0100110/*
111 * Randomize the address space (stacks, mmaps, brk, etc.).
112 *
113 * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
114 * as ancient (libc5 based) binaries can segfault. )
115 */
116int randomize_va_space __read_mostly =
117#ifdef CONFIG_COMPAT_BRK
118 1;
119#else
120 2;
121#endif
Andi Kleena62eaf12006-02-16 23:41:58 +0100122
Jia He83d116c2019-10-11 22:09:39 +0800123#ifndef arch_faults_on_old_pte
124static inline bool arch_faults_on_old_pte(void)
125{
126 /*
127 * Those arches which don't have hw access flag feature need to
128 * implement their own helper. By default, "true" means pagefault
129 * will be hit on old pte.
130 */
131 return true;
132}
133#endif
134
Andi Kleena62eaf12006-02-16 23:41:58 +0100135static int __init disable_randmaps(char *s)
136{
137 randomize_va_space = 0;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800138 return 1;
Andi Kleena62eaf12006-02-16 23:41:58 +0100139}
140__setup("norandmaps", disable_randmaps);
141
Hugh Dickins62eede62009-09-21 17:03:34 -0700142unsigned long zero_pfn __read_mostly;
Ard Biesheuvel0b700682014-09-12 22:17:23 +0200143EXPORT_SYMBOL(zero_pfn);
144
Tobin C Harding166f61b2017-02-24 14:59:01 -0800145unsigned long highest_memmap_pfn __read_mostly;
146
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -0700147/*
148 * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
149 */
150static int __init init_zero_pfn(void)
151{
152 zero_pfn = page_to_pfn(ZERO_PAGE(0));
153 return 0;
154}
155core_initcall(init_zero_pfn);
Andi Kleena62eaf12006-02-16 23:41:58 +0100156
Joel Fernandes (Google)e4dcad22019-11-30 17:50:33 -0800157void mm_trace_rss_stat(struct mm_struct *mm, int member, long count)
Joel Fernandes (Google)b3d14112019-11-30 17:50:30 -0800158{
Joel Fernandes (Google)e4dcad22019-11-30 17:50:33 -0800159 trace_rss_stat(mm, member, count);
Joel Fernandes (Google)b3d14112019-11-30 17:50:30 -0800160}
Greg Kroah-Hartmanbb0c8742019-12-17 19:12:36 +0100161EXPORT_SYMBOL_GPL(mm_trace_rss_stat);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800162
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800163#if defined(SPLIT_RSS_COUNTING)
164
David Rientjesea48cf72012-03-21 16:34:13 -0700165void sync_mm_rss(struct mm_struct *mm)
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800166{
167 int i;
168
169 for (i = 0; i < NR_MM_COUNTERS; i++) {
David Rientjes05af2e12012-03-21 16:34:13 -0700170 if (current->rss_stat.count[i]) {
171 add_mm_counter(mm, i, current->rss_stat.count[i]);
172 current->rss_stat.count[i] = 0;
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800173 }
174 }
David Rientjes05af2e12012-03-21 16:34:13 -0700175 current->rss_stat.events = 0;
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800176}
177
178static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
179{
180 struct task_struct *task = current;
181
182 if (likely(task->mm == mm))
183 task->rss_stat.count[member] += val;
184 else
185 add_mm_counter(mm, member, val);
186}
187#define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
188#define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
189
190/* sync counter once per 64 page faults */
191#define TASK_RSS_EVENTS_THRESH (64)
192static void check_sync_rss_stat(struct task_struct *task)
193{
194 if (unlikely(task != current))
195 return;
196 if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
David Rientjesea48cf72012-03-21 16:34:13 -0700197 sync_mm_rss(task->mm);
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800198}
Peter Zijlstra9547d012011-05-24 17:12:14 -0700199#else /* SPLIT_RSS_COUNTING */
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800200
201#define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
202#define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
203
204static void check_sync_rss_stat(struct task_struct *task)
205{
206}
207
Peter Zijlstra9547d012011-05-24 17:12:14 -0700208#endif /* SPLIT_RSS_COUNTING */
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * Note: this doesn't free the actual pages themselves. That
212 * has been handled earlier when unmapping all the memory regions.
213 */
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000214static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
215 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800217 pgtable_t token = pmd_pgtable(*pmd);
Hugh Dickinse0da3822005-04-19 13:29:15 -0700218 pmd_clear(pmd);
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000219 pte_free_tlb(tlb, token, addr);
Kirill A. Shutemovc4812902017-11-15 17:35:37 -0800220 mm_dec_nr_ptes(tlb->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Hugh Dickinse0da3822005-04-19 13:29:15 -0700223static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
224 unsigned long addr, unsigned long end,
225 unsigned long floor, unsigned long ceiling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 pmd_t *pmd;
228 unsigned long next;
Hugh Dickinse0da3822005-04-19 13:29:15 -0700229 unsigned long start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Hugh Dickinse0da3822005-04-19 13:29:15 -0700231 start = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 pmd = pmd_offset(pud, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 do {
234 next = pmd_addr_end(addr, end);
235 if (pmd_none_or_clear_bad(pmd))
236 continue;
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000237 free_pte_range(tlb, pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 } while (pmd++, addr = next, addr != end);
239
Hugh Dickinse0da3822005-04-19 13:29:15 -0700240 start &= PUD_MASK;
241 if (start < floor)
242 return;
243 if (ceiling) {
244 ceiling &= PUD_MASK;
245 if (!ceiling)
246 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
Hugh Dickinse0da3822005-04-19 13:29:15 -0700248 if (end - 1 > ceiling - 1)
249 return;
250
251 pmd = pmd_offset(pud, start);
252 pud_clear(pud);
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000253 pmd_free_tlb(tlb, pmd, start);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800254 mm_dec_nr_pmds(tlb->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300257static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d,
Hugh Dickinse0da3822005-04-19 13:29:15 -0700258 unsigned long addr, unsigned long end,
259 unsigned long floor, unsigned long ceiling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 pud_t *pud;
262 unsigned long next;
Hugh Dickinse0da3822005-04-19 13:29:15 -0700263 unsigned long start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Hugh Dickinse0da3822005-04-19 13:29:15 -0700265 start = addr;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300266 pud = pud_offset(p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 do {
268 next = pud_addr_end(addr, end);
269 if (pud_none_or_clear_bad(pud))
270 continue;
Hugh Dickinse0da3822005-04-19 13:29:15 -0700271 free_pmd_range(tlb, pud, addr, next, floor, ceiling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 } while (pud++, addr = next, addr != end);
273
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300274 start &= P4D_MASK;
275 if (start < floor)
276 return;
277 if (ceiling) {
278 ceiling &= P4D_MASK;
279 if (!ceiling)
280 return;
281 }
282 if (end - 1 > ceiling - 1)
283 return;
284
285 pud = pud_offset(p4d, start);
286 p4d_clear(p4d);
287 pud_free_tlb(tlb, pud, start);
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -0800288 mm_dec_nr_puds(tlb->mm);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300289}
290
291static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd,
292 unsigned long addr, unsigned long end,
293 unsigned long floor, unsigned long ceiling)
294{
295 p4d_t *p4d;
296 unsigned long next;
297 unsigned long start;
298
299 start = addr;
300 p4d = p4d_offset(pgd, addr);
301 do {
302 next = p4d_addr_end(addr, end);
303 if (p4d_none_or_clear_bad(p4d))
304 continue;
305 free_pud_range(tlb, p4d, addr, next, floor, ceiling);
306 } while (p4d++, addr = next, addr != end);
307
Hugh Dickinse0da3822005-04-19 13:29:15 -0700308 start &= PGDIR_MASK;
309 if (start < floor)
310 return;
311 if (ceiling) {
312 ceiling &= PGDIR_MASK;
313 if (!ceiling)
314 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
Hugh Dickinse0da3822005-04-19 13:29:15 -0700316 if (end - 1 > ceiling - 1)
317 return;
318
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300319 p4d = p4d_offset(pgd, start);
Hugh Dickinse0da3822005-04-19 13:29:15 -0700320 pgd_clear(pgd);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300321 p4d_free_tlb(tlb, p4d, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324/*
Hugh Dickinse0da3822005-04-19 13:29:15 -0700325 * This function frees user-level page tables of a process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
Jan Beulich42b77722008-07-23 21:27:10 -0700327void free_pgd_range(struct mmu_gather *tlb,
Hugh Dickinse0da3822005-04-19 13:29:15 -0700328 unsigned long addr, unsigned long end,
329 unsigned long floor, unsigned long ceiling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 pgd_t *pgd;
332 unsigned long next;
333
Hugh Dickinse0da3822005-04-19 13:29:15 -0700334 /*
335 * The next few lines have given us lots of grief...
336 *
337 * Why are we testing PMD* at this top level? Because often
338 * there will be no work to do at all, and we'd prefer not to
339 * go all the way down to the bottom just to discover that.
340 *
341 * Why all these "- 1"s? Because 0 represents both the bottom
342 * of the address space and the top of it (using -1 for the
343 * top wouldn't help much: the masks would do the wrong thing).
344 * The rule is that addr 0 and floor 0 refer to the bottom of
345 * the address space, but end 0 and ceiling 0 refer to the top
346 * Comparisons need to use "end - 1" and "ceiling - 1" (though
347 * that end 0 case should be mythical).
348 *
349 * Wherever addr is brought up or ceiling brought down, we must
350 * be careful to reject "the opposite 0" before it confuses the
351 * subsequent tests. But what about where end is brought down
352 * by PMD_SIZE below? no, end can't go down to 0 there.
353 *
354 * Whereas we round start (addr) and ceiling down, by different
355 * masks at different levels, in order to test whether a table
356 * now has no other vmas using it, so can be freed, we don't
357 * bother to round floor or end up - the tests don't need that.
358 */
359
360 addr &= PMD_MASK;
361 if (addr < floor) {
362 addr += PMD_SIZE;
363 if (!addr)
364 return;
365 }
366 if (ceiling) {
367 ceiling &= PMD_MASK;
368 if (!ceiling)
369 return;
370 }
371 if (end - 1 > ceiling - 1)
372 end -= PMD_SIZE;
373 if (addr > end - 1)
374 return;
Aneesh Kumar K.V07e32662016-12-12 16:42:40 -0800375 /*
376 * We add page table cache pages with PAGE_SIZE,
377 * (see pte_free_tlb()), flush the tlb if we need
378 */
Peter Zijlstraed6a7932018-08-31 14:46:08 +0200379 tlb_change_page_size(tlb, PAGE_SIZE);
Jan Beulich42b77722008-07-23 21:27:10 -0700380 pgd = pgd_offset(tlb->mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 do {
382 next = pgd_addr_end(addr, end);
383 if (pgd_none_or_clear_bad(pgd))
384 continue;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300385 free_p4d_range(tlb, pgd, addr, next, floor, ceiling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 } while (pgd++, addr = next, addr != end);
Hugh Dickinse0da3822005-04-19 13:29:15 -0700387}
388
Jan Beulich42b77722008-07-23 21:27:10 -0700389void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700390 unsigned long floor, unsigned long ceiling)
Hugh Dickinse0da3822005-04-19 13:29:15 -0700391{
392 while (vma) {
393 struct vm_area_struct *next = vma->vm_next;
394 unsigned long addr = vma->vm_start;
395
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700396 /*
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +1000397 * Hide vma from rmap and truncate_pagecache before freeing
398 * pgtables
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700399 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800400 unlink_anon_vmas(vma);
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700401 unlink_file_vma(vma);
402
David Gibson9da61ae2006-03-22 00:08:57 -0800403 if (is_vm_hugetlb_page(vma)) {
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700404 hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
Tobin C Harding166f61b2017-02-24 14:59:01 -0800405 floor, next ? next->vm_start : ceiling);
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700406 } else {
407 /*
408 * Optimization: gather nearby vmas into one call down
409 */
410 while (next && next->vm_start <= vma->vm_end + PMD_SIZE
David Gibson48669202006-03-22 00:08:58 -0800411 && !is_vm_hugetlb_page(next)) {
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700412 vma = next;
413 next = vma->vm_next;
Rik van Riel5beb4932010-03-05 13:42:07 -0800414 unlink_anon_vmas(vma);
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700415 unlink_file_vma(vma);
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700416 }
417 free_pgd_range(tlb, addr, vma->vm_end,
Tobin C Harding166f61b2017-02-24 14:59:01 -0800418 floor, next ? next->vm_start : ceiling);
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700419 }
Hugh Dickinse0da3822005-04-19 13:29:15 -0700420 vma = next;
421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -0800424int __pte_alloc(struct mm_struct *mm, pmd_t *pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800426 spinlock_t *ptl;
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -0800427 pgtable_t new = pte_alloc_one(mm);
Hugh Dickins1bb36302005-10-29 18:16:22 -0700428 if (!new)
429 return -ENOMEM;
430
Nick Piggin362a61a2008-05-14 06:37:36 +0200431 /*
432 * Ensure all pte setup (eg. pte page lock and page clearing) are
433 * visible before the pte is made visible to other CPUs by being
434 * put into page tables.
435 *
436 * The other side of the story is the pointer chasing in the page
437 * table walking code (when walking the page table without locking;
438 * ie. most of the time). Fortunately, these data accesses consist
439 * of a chain of data-dependent loads, meaning most CPUs (alpha
440 * being the notable exception) will already guarantee loads are
441 * seen in-order. See the alpha page table accessors for the
442 * smp_read_barrier_depends() barriers in page table walking code.
443 */
444 smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
445
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800446 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli8ac1f832011-01-13 15:46:43 -0800447 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
Kirill A. Shutemovc4812902017-11-15 17:35:37 -0800448 mm_inc_nr_ptes(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 pmd_populate(mm, pmd, new);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800450 new = NULL;
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -0800451 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800452 spin_unlock(ptl);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800453 if (new)
454 pte_free(mm, new);
Hugh Dickins1bb36302005-10-29 18:16:22 -0700455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -0800458int __pte_alloc_kernel(pmd_t *pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -0800460 pte_t *new = pte_alloc_one_kernel(&init_mm);
Hugh Dickins1bb36302005-10-29 18:16:22 -0700461 if (!new)
462 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Nick Piggin362a61a2008-05-14 06:37:36 +0200464 smp_wmb(); /* See comment in __pte_alloc */
465
Hugh Dickins1bb36302005-10-29 18:16:22 -0700466 spin_lock(&init_mm.page_table_lock);
Andrea Arcangeli8ac1f832011-01-13 15:46:43 -0800467 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
Hugh Dickins1bb36302005-10-29 18:16:22 -0700468 pmd_populate_kernel(&init_mm, pmd, new);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800469 new = NULL;
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -0800470 }
Hugh Dickins1bb36302005-10-29 18:16:22 -0700471 spin_unlock(&init_mm.page_table_lock);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800472 if (new)
473 pte_free_kernel(&init_mm, new);
Hugh Dickins1bb36302005-10-29 18:16:22 -0700474 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800477static inline void init_rss_vec(int *rss)
Hugh Dickinsae859762005-10-29 18:16:05 -0700478{
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800479 memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
480}
481
482static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
483{
484 int i;
485
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800486 if (current->mm == mm)
David Rientjes05af2e12012-03-21 16:34:13 -0700487 sync_mm_rss(mm);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800488 for (i = 0; i < NR_MM_COUNTERS; i++)
489 if (rss[i])
490 add_mm_counter(mm, i, rss[i]);
Hugh Dickinsae859762005-10-29 18:16:05 -0700491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/*
Linus Torvalds6aab3412005-11-28 14:34:23 -0800494 * This function is called to print an error when a bad pte
495 * is found. For example, we might have a PFN-mapped pte in
496 * a region that doesn't allow it.
Nick Pigginb5810032005-10-29 18:16:12 -0700497 *
498 * The calling function must still handle the error.
499 */
Hugh Dickins3dc14742009-01-06 14:40:08 -0800500static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
501 pte_t pte, struct page *page)
Nick Pigginb5810032005-10-29 18:16:12 -0700502{
Hugh Dickins3dc14742009-01-06 14:40:08 -0800503 pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300504 p4d_t *p4d = p4d_offset(pgd, addr);
505 pud_t *pud = pud_offset(p4d, addr);
Hugh Dickins3dc14742009-01-06 14:40:08 -0800506 pmd_t *pmd = pmd_offset(pud, addr);
507 struct address_space *mapping;
508 pgoff_t index;
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800509 static unsigned long resume;
510 static unsigned long nr_shown;
511 static unsigned long nr_unshown;
512
513 /*
514 * Allow a burst of 60 reports, then keep quiet for that minute;
515 * or allow a steady drip of one report per second.
516 */
517 if (nr_shown == 60) {
518 if (time_before(jiffies, resume)) {
519 nr_unshown++;
520 return;
521 }
522 if (nr_unshown) {
Joe Perches11705322016-03-17 14:19:50 -0700523 pr_alert("BUG: Bad page map: %lu messages suppressed\n",
524 nr_unshown);
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800525 nr_unshown = 0;
526 }
527 nr_shown = 0;
528 }
529 if (nr_shown++ == 0)
530 resume = jiffies + 60 * HZ;
Hugh Dickins3dc14742009-01-06 14:40:08 -0800531
532 mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
533 index = linear_page_index(vma, addr);
534
Joe Perches11705322016-03-17 14:19:50 -0700535 pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n",
536 current->comm,
537 (long long)pte_val(pte), (long long)pmd_val(*pmd));
Wu Fengguang718a3822010-03-10 15:20:43 -0800538 if (page)
Dave Hansenf0b791a2014-01-23 15:52:49 -0800539 dump_page(page, "bad pte");
Kefeng Wang6aa9b8b2019-09-23 15:35:34 -0700540 pr_alert("addr:%px vm_flags:%08lx anon_vma:%px mapping:%px index:%lx\n",
Joe Perches11705322016-03-17 14:19:50 -0700541 (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
Sakari Ailusd75f7732019-03-25 21:32:28 +0200542 pr_alert("file:%pD fault:%ps mmap:%ps readpage:%ps\n",
Konstantin Khlebnikov26825822015-04-15 16:15:08 -0700543 vma->vm_file,
544 vma->vm_ops ? vma->vm_ops->fault : NULL,
545 vma->vm_file ? vma->vm_file->f_op->mmap : NULL,
546 mapping ? mapping->a_ops->readpage : NULL);
Nick Pigginb5810032005-10-29 18:16:12 -0700547 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +1030548 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Nick Pigginb5810032005-10-29 18:16:12 -0700549}
550
551/*
Nick Piggin7e675132008-04-28 02:13:00 -0700552 * vm_normal_page -- This function gets the "struct page" associated with a pte.
Linus Torvalds6aab3412005-11-28 14:34:23 -0800553 *
Nick Piggin7e675132008-04-28 02:13:00 -0700554 * "Special" mappings do not wish to be associated with a "struct page" (either
555 * it doesn't exist, or it exists but they don't want to touch it). In this
556 * case, NULL is returned here. "Normal" mappings do have a struct page.
Jared Hulbertb379d792008-04-28 02:12:58 -0700557 *
Nick Piggin7e675132008-04-28 02:13:00 -0700558 * There are 2 broad cases. Firstly, an architecture may define a pte_special()
559 * pte bit, in which case this function is trivial. Secondly, an architecture
560 * may not have a spare pte bit, which requires a more complicated scheme,
561 * described below.
562 *
563 * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
564 * special mapping (even if there are underlying and valid "struct pages").
565 * COWed pages of a VM_PFNMAP are always normal.
Linus Torvalds6aab3412005-11-28 14:34:23 -0800566 *
Jared Hulbertb379d792008-04-28 02:12:58 -0700567 * The way we recognize COWed pages within VM_PFNMAP mappings is through the
568 * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
Nick Piggin7e675132008-04-28 02:13:00 -0700569 * set, and the vm_pgoff will point to the first PFN mapped: thus every special
570 * mapping will always honor the rule
Linus Torvalds6aab3412005-11-28 14:34:23 -0800571 *
572 * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
573 *
Nick Piggin7e675132008-04-28 02:13:00 -0700574 * And for normal mappings this is false.
Jared Hulbertb379d792008-04-28 02:12:58 -0700575 *
Nick Piggin7e675132008-04-28 02:13:00 -0700576 * This restricts such mappings to be a linear translation from virtual address
577 * to pfn. To get around this restriction, we allow arbitrary mappings so long
578 * as the vma is not a COW mapping; in that case, we know that all ptes are
579 * special (because none can have been COWed).
Jared Hulbertb379d792008-04-28 02:12:58 -0700580 *
581 *
Nick Piggin7e675132008-04-28 02:13:00 -0700582 * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
583 *
Jared Hulbertb379d792008-04-28 02:12:58 -0700584 * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
585 * page" backing, however the difference is that _all_ pages with a struct
586 * page (that is, those where pfn_valid is true) are refcounted and considered
587 * normal pages by the VM. The disadvantage is that pages are refcounted
588 * (which can be slower and simply not an option for some PFNMAP users). The
589 * advantage is that we don't have to follow the strict linearity rule of
590 * PFNMAP mappings in order to support COWable mappings.
591 *
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800592 */
Christoph Hellwig25b29952019-06-13 22:50:49 +0200593struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
594 pte_t pte)
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800595{
Hugh Dickins22b31ee2009-01-06 14:40:09 -0800596 unsigned long pfn = pte_pfn(pte);
Nick Piggin7e675132008-04-28 02:13:00 -0700597
Laurent Dufour00b3a332018-06-07 17:06:12 -0700598 if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) {
Hugh Dickinsb38af472014-08-29 15:18:44 -0700599 if (likely(!pte_special(pte)))
Hugh Dickins22b31ee2009-01-06 14:40:09 -0800600 goto check_pfn;
David Vrabel667a0a02014-12-18 14:48:15 +0000601 if (vma->vm_ops && vma->vm_ops->find_special_page)
602 return vma->vm_ops->find_special_page(vma, addr);
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -0700603 if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
604 return NULL;
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700605 if (is_zero_pfn(pfn))
606 return NULL;
Dave Jiange1fb4a02018-08-17 15:43:40 -0700607 if (pte_devmap(pte))
608 return NULL;
609
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700610 print_bad_pte(vma, addr, pte, NULL);
Nick Piggin7e675132008-04-28 02:13:00 -0700611 return NULL;
612 }
613
Laurent Dufour00b3a332018-06-07 17:06:12 -0700614 /* !CONFIG_ARCH_HAS_PTE_SPECIAL case follows: */
Nick Piggin7e675132008-04-28 02:13:00 -0700615
Jared Hulbertb379d792008-04-28 02:12:58 -0700616 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
617 if (vma->vm_flags & VM_MIXEDMAP) {
618 if (!pfn_valid(pfn))
619 return NULL;
620 goto out;
621 } else {
Nick Piggin7e675132008-04-28 02:13:00 -0700622 unsigned long off;
623 off = (addr - vma->vm_start) >> PAGE_SHIFT;
Jared Hulbertb379d792008-04-28 02:12:58 -0700624 if (pfn == vma->vm_pgoff + off)
625 return NULL;
626 if (!is_cow_mapping(vma->vm_flags))
627 return NULL;
628 }
Linus Torvalds6aab3412005-11-28 14:34:23 -0800629 }
630
Hugh Dickinsb38af472014-08-29 15:18:44 -0700631 if (is_zero_pfn(pfn))
632 return NULL;
Laurent Dufour00b3a332018-06-07 17:06:12 -0700633
Hugh Dickins22b31ee2009-01-06 14:40:09 -0800634check_pfn:
635 if (unlikely(pfn > highest_memmap_pfn)) {
636 print_bad_pte(vma, addr, pte, NULL);
637 return NULL;
638 }
Linus Torvalds6aab3412005-11-28 14:34:23 -0800639
640 /*
Nick Piggin7e675132008-04-28 02:13:00 -0700641 * NOTE! We still have PageReserved() pages in the page tables.
Nick Piggin7e675132008-04-28 02:13:00 -0700642 * eg. VDSO mappings can cause them to exist.
Linus Torvalds6aab3412005-11-28 14:34:23 -0800643 */
Jared Hulbertb379d792008-04-28 02:12:58 -0700644out:
Linus Torvalds6aab3412005-11-28 14:34:23 -0800645 return pfn_to_page(pfn);
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800646}
647
Gerald Schaefer28093f92016-04-28 16:18:35 -0700648#ifdef CONFIG_TRANSPARENT_HUGEPAGE
649struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
650 pmd_t pmd)
651{
652 unsigned long pfn = pmd_pfn(pmd);
653
654 /*
655 * There is no pmd_special() but there may be special pmds, e.g.
656 * in a direct-access (dax) mapping, so let's just replicate the
Laurent Dufour00b3a332018-06-07 17:06:12 -0700657 * !CONFIG_ARCH_HAS_PTE_SPECIAL case from vm_normal_page() here.
Gerald Schaefer28093f92016-04-28 16:18:35 -0700658 */
659 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
660 if (vma->vm_flags & VM_MIXEDMAP) {
661 if (!pfn_valid(pfn))
662 return NULL;
663 goto out;
664 } else {
665 unsigned long off;
666 off = (addr - vma->vm_start) >> PAGE_SHIFT;
667 if (pfn == vma->vm_pgoff + off)
668 return NULL;
669 if (!is_cow_mapping(vma->vm_flags))
670 return NULL;
671 }
672 }
673
Dave Jiange1fb4a02018-08-17 15:43:40 -0700674 if (pmd_devmap(pmd))
675 return NULL;
Yu Zhao3cde2872019-12-04 16:49:56 -0800676 if (is_huge_zero_pmd(pmd))
Gerald Schaefer28093f92016-04-28 16:18:35 -0700677 return NULL;
678 if (unlikely(pfn > highest_memmap_pfn))
679 return NULL;
680
681 /*
682 * NOTE! We still have PageReserved() pages in the page tables.
683 * eg. VDSO mappings can cause them to exist.
684 */
685out:
686 return pfn_to_page(pfn);
687}
688#endif
689
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800690/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 * copy one vm_area from one task to the other. Assumes the page tables
692 * already present in the new task to be cleared in the whole range
693 * covered by this vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 */
695
Hugh Dickins570a335b2009-12-14 17:58:46 -0800696static inline unsigned long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
Nick Pigginb5810032005-10-29 18:16:12 -0700698 pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
Hugh Dickins8c103762005-10-29 18:16:13 -0700699 unsigned long addr, int *rss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Nick Pigginb5810032005-10-29 18:16:12 -0700701 unsigned long vm_flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 pte_t pte = *src_pte;
703 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 /* pte contains position in swap or file, so copy. */
706 if (unlikely(!pte_present(pte))) {
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800707 swp_entry_t entry = pte_to_swp_entry(pte);
Christoph Lameter06972122006-06-23 02:03:35 -0700708
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800709 if (likely(!non_swap_entry(entry))) {
710 if (swap_duplicate(entry) < 0)
711 return entry.val;
Hugh Dickins570a335b2009-12-14 17:58:46 -0800712
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800713 /* make sure dst_mm is on swapoff's mmlist. */
714 if (unlikely(list_empty(&dst_mm->mmlist))) {
715 spin_lock(&mmlist_lock);
716 if (list_empty(&dst_mm->mmlist))
717 list_add(&dst_mm->mmlist,
718 &src_mm->mmlist);
719 spin_unlock(&mmlist_lock);
720 }
721 rss[MM_SWAPENTS]++;
722 } else if (is_migration_entry(entry)) {
723 page = migration_entry_to_page(entry);
Konstantin Khlebnikov9f9f1ac2012-01-20 14:34:24 -0800724
Jerome Marchandeca56ff2016-01-14 15:19:26 -0800725 rss[mm_counter(page)]++;
Konstantin Khlebnikov9f9f1ac2012-01-20 14:34:24 -0800726
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800727 if (is_write_migration_entry(entry) &&
728 is_cow_mapping(vm_flags)) {
729 /*
730 * COW mappings require pages in both
731 * parent and child to be set to read.
732 */
733 make_migration_entry_read(&entry);
734 pte = swp_entry_to_pte(entry);
735 if (pte_swp_soft_dirty(*src_pte))
736 pte = pte_swp_mksoft_dirty(pte);
737 set_pte_at(src_mm, addr, src_pte, pte);
Christoph Lameter06972122006-06-23 02:03:35 -0700738 }
Jérôme Glisse5042db42017-09-08 16:11:43 -0700739 } else if (is_device_private_entry(entry)) {
740 page = device_private_entry_to_page(entry);
741
742 /*
743 * Update rss count even for unaddressable pages, as
744 * they should treated just like normal pages in this
745 * respect.
746 *
747 * We will likely want to have some new rss counters
748 * for unaddressable pages, at some point. But for now
749 * keep things as they are.
750 */
751 get_page(page);
752 rss[mm_counter(page)]++;
753 page_dup_rmap(page, false);
754
755 /*
756 * We do not preserve soft-dirty information, because so
757 * far, checkpoint/restore is the only feature that
758 * requires that. And checkpoint/restore does not work
759 * when a device driver is involved (you cannot easily
760 * save and restore device driver state).
761 */
762 if (is_write_device_private_entry(entry) &&
763 is_cow_mapping(vm_flags)) {
764 make_device_private_entry_read(&entry);
765 pte = swp_entry_to_pte(entry);
766 set_pte_at(src_mm, addr, src_pte, pte);
767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
Hugh Dickinsae859762005-10-29 18:16:05 -0700769 goto out_set_pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /*
773 * If it's a COW mapping, write protect it both
774 * in the parent and the child
775 */
Linus Torvalds1b2de5d2018-07-09 13:19:49 -0700776 if (is_cow_mapping(vm_flags) && pte_write(pte)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 ptep_set_wrprotect(src_mm, addr, src_pte);
Zachary Amsden3dc90792006-09-30 23:29:30 -0700778 pte = pte_wrprotect(pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
781 /*
782 * If it's a shared mapping, mark it clean in
783 * the child
784 */
785 if (vm_flags & VM_SHARED)
786 pte = pte_mkclean(pte);
787 pte = pte_mkold(pte);
Linus Torvalds6aab3412005-11-28 14:34:23 -0800788
789 page = vm_normal_page(vma, addr, pte);
790 if (page) {
791 get_page(page);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -0800792 page_dup_rmap(page, false);
Jerome Marchandeca56ff2016-01-14 15:19:26 -0800793 rss[mm_counter(page)]++;
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700794 } else if (pte_devmap(pte)) {
795 page = pte_page(pte);
Linus Torvalds6aab3412005-11-28 14:34:23 -0800796 }
Hugh Dickinsae859762005-10-29 18:16:05 -0700797
798out_set_pte:
799 set_pte_at(dst_mm, addr, dst_pte, pte);
Hugh Dickins570a335b2009-12-14 17:58:46 -0800800 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
Jerome Marchand21bda262014-08-06 16:06:56 -0700803static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800804 pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
805 unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Daisuke Nishimurac36987e2009-10-26 16:50:23 -0700807 pte_t *orig_src_pte, *orig_dst_pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 pte_t *src_pte, *dst_pte;
Hugh Dickinsc74df322005-10-29 18:16:23 -0700809 spinlock_t *src_ptl, *dst_ptl;
Hugh Dickinse040f212005-10-29 18:15:53 -0700810 int progress = 0;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800811 int rss[NR_MM_COUNTERS];
Hugh Dickins570a335b2009-12-14 17:58:46 -0800812 swp_entry_t entry = (swp_entry_t){0};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814again:
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800815 init_rss_vec(rss);
816
Hugh Dickinsc74df322005-10-29 18:16:23 -0700817 dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (!dst_pte)
819 return -ENOMEM;
Peter Zijlstraece0e2b2010-10-26 14:21:52 -0700820 src_pte = pte_offset_map(src_pmd, addr);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700821 src_ptl = pte_lockptr(src_mm, src_pmd);
Ingo Molnarf20dc5f2006-07-03 00:25:08 -0700822 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Daisuke Nishimurac36987e2009-10-26 16:50:23 -0700823 orig_src_pte = src_pte;
824 orig_dst_pte = dst_pte;
Zachary Amsden6606c3e2006-09-30 23:29:33 -0700825 arch_enter_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 do {
828 /*
829 * We are holding two locks at this point - either of them
830 * could generate latencies in another task on another CPU.
831 */
Hugh Dickinse040f212005-10-29 18:15:53 -0700832 if (progress >= 32) {
833 progress = 0;
834 if (need_resched() ||
Nick Piggin95c354f2008-01-30 13:31:20 +0100835 spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
Hugh Dickinse040f212005-10-29 18:15:53 -0700836 break;
837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (pte_none(*src_pte)) {
839 progress++;
840 continue;
841 }
Hugh Dickins570a335b2009-12-14 17:58:46 -0800842 entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
843 vma, addr, rss);
844 if (entry.val)
845 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 progress += 8;
847 } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Zachary Amsden6606c3e2006-09-30 23:29:33 -0700849 arch_leave_lazy_mmu_mode();
Hugh Dickinsc74df322005-10-29 18:16:23 -0700850 spin_unlock(src_ptl);
Peter Zijlstraece0e2b2010-10-26 14:21:52 -0700851 pte_unmap(orig_src_pte);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800852 add_mm_rss_vec(dst_mm, rss);
Daisuke Nishimurac36987e2009-10-26 16:50:23 -0700853 pte_unmap_unlock(orig_dst_pte, dst_ptl);
Hugh Dickinsc74df322005-10-29 18:16:23 -0700854 cond_resched();
Hugh Dickins570a335b2009-12-14 17:58:46 -0800855
856 if (entry.val) {
857 if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
858 return -ENOMEM;
859 progress = 0;
860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (addr != end)
862 goto again;
863 return 0;
864}
865
866static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
867 pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
868 unsigned long addr, unsigned long end)
869{
870 pmd_t *src_pmd, *dst_pmd;
871 unsigned long next;
872
873 dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
874 if (!dst_pmd)
875 return -ENOMEM;
876 src_pmd = pmd_offset(src_pud, addr);
877 do {
878 next = pmd_addr_end(addr, end);
Zi Yan84c3fc42017-09-08 16:11:01 -0700879 if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd)
880 || pmd_devmap(*src_pmd)) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800881 int err;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800882 VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800883 err = copy_huge_pmd(dst_mm, src_mm,
884 dst_pmd, src_pmd, addr, vma);
885 if (err == -ENOMEM)
886 return -ENOMEM;
887 if (!err)
888 continue;
889 /* fall through */
890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (pmd_none_or_clear_bad(src_pmd))
892 continue;
893 if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
894 vma, addr, next))
895 return -ENOMEM;
896 } while (dst_pmd++, src_pmd++, addr = next, addr != end);
897 return 0;
898}
899
900static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300901 p4d_t *dst_p4d, p4d_t *src_p4d, struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 unsigned long addr, unsigned long end)
903{
904 pud_t *src_pud, *dst_pud;
905 unsigned long next;
906
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300907 dst_pud = pud_alloc(dst_mm, dst_p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (!dst_pud)
909 return -ENOMEM;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300910 src_pud = pud_offset(src_p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 do {
912 next = pud_addr_end(addr, end);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800913 if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) {
914 int err;
915
916 VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, vma);
917 err = copy_huge_pud(dst_mm, src_mm,
918 dst_pud, src_pud, addr, vma);
919 if (err == -ENOMEM)
920 return -ENOMEM;
921 if (!err)
922 continue;
923 /* fall through */
924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (pud_none_or_clear_bad(src_pud))
926 continue;
927 if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
928 vma, addr, next))
929 return -ENOMEM;
930 } while (dst_pud++, src_pud++, addr = next, addr != end);
931 return 0;
932}
933
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300934static inline int copy_p4d_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
935 pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
936 unsigned long addr, unsigned long end)
937{
938 p4d_t *src_p4d, *dst_p4d;
939 unsigned long next;
940
941 dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr);
942 if (!dst_p4d)
943 return -ENOMEM;
944 src_p4d = p4d_offset(src_pgd, addr);
945 do {
946 next = p4d_addr_end(addr, end);
947 if (p4d_none_or_clear_bad(src_p4d))
948 continue;
949 if (copy_pud_range(dst_mm, src_mm, dst_p4d, src_p4d,
950 vma, addr, next))
951 return -ENOMEM;
952 } while (dst_p4d++, src_p4d++, addr = next, addr != end);
953 return 0;
954}
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
957 struct vm_area_struct *vma)
958{
959 pgd_t *src_pgd, *dst_pgd;
960 unsigned long next;
961 unsigned long addr = vma->vm_start;
962 unsigned long end = vma->vm_end;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800963 struct mmu_notifier_range range;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -0700964 bool is_cow;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700965 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Nick Piggind9928952005-08-28 16:49:11 +1000967 /*
968 * Don't copy ptes where a page fault will fill them correctly.
969 * Fork becomes much lighter when there are big shared or private
970 * readonly mappings. The tradeoff is that copy_page_range is more
971 * efficient than faulting.
972 */
Kirill A. Shutemov0661a332015-02-10 14:10:04 -0800973 if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) &&
974 !vma->anon_vma)
975 return 0;
Nick Piggind9928952005-08-28 16:49:11 +1000976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (is_vm_hugetlb_page(vma))
978 return copy_hugetlb_page_range(dst_mm, src_mm, vma);
979
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -0700980 if (unlikely(vma->vm_flags & VM_PFNMAP)) {
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -0800981 /*
982 * We do not free on error cases below as remove_vma
983 * gets called on error from higher level routine
984 */
Suresh Siddha5180da42012-10-08 16:28:29 -0700985 ret = track_pfn_copy(vma);
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -0800986 if (ret)
987 return ret;
988 }
989
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700990 /*
991 * We need to invalidate the secondary MMU mappings only when
992 * there could be a permission downgrade on the ptes of the
993 * parent mm. And a permission downgrade will only happen if
994 * is_cow_mapping() returns true.
995 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -0700996 is_cow = is_cow_mapping(vma->vm_flags);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800997
998 if (is_cow) {
Jérôme Glisse7269f992019-05-13 17:20:53 -0700999 mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE,
1000 0, vma, src_mm, addr, end);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001001 mmu_notifier_invalidate_range_start(&range);
1002 }
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001003
1004 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 dst_pgd = pgd_offset(dst_mm, addr);
1006 src_pgd = pgd_offset(src_mm, addr);
1007 do {
1008 next = pgd_addr_end(addr, end);
1009 if (pgd_none_or_clear_bad(src_pgd))
1010 continue;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001011 if (unlikely(copy_p4d_range(dst_mm, src_mm, dst_pgd, src_pgd,
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001012 vma, addr, next))) {
1013 ret = -ENOMEM;
1014 break;
1015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 } while (dst_pgd++, src_pgd++, addr = next, addr != end);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001017
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001018 if (is_cow)
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001019 mmu_notifier_invalidate_range_end(&range);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001020 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
Robin Holt51c6f662005-11-13 16:06:42 -08001023static unsigned long zap_pte_range(struct mmu_gather *tlb,
Nick Pigginb5810032005-10-29 18:16:12 -07001024 struct vm_area_struct *vma, pmd_t *pmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 unsigned long addr, unsigned long end,
Peter Zijlstra97a89412011-05-24 17:12:04 -07001026 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Nick Pigginb5810032005-10-29 18:16:12 -07001028 struct mm_struct *mm = tlb->mm;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001029 int force_flush = 0;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001030 int rss[NR_MM_COUNTERS];
Peter Zijlstra97a89412011-05-24 17:12:04 -07001031 spinlock_t *ptl;
Steven Rostedt5f1a1902011-06-15 15:08:23 -07001032 pte_t *start_pte;
Peter Zijlstra97a89412011-05-24 17:12:04 -07001033 pte_t *pte;
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001034 swp_entry_t entry;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001035
Peter Zijlstraed6a7932018-08-31 14:46:08 +02001036 tlb_change_page_size(tlb, PAGE_SIZE);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001037again:
Peter Zijlstrae3032972011-05-24 17:12:01 -07001038 init_rss_vec(rss);
Steven Rostedt5f1a1902011-06-15 15:08:23 -07001039 start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
1040 pte = start_pte;
Mel Gorman3ea27712017-08-02 13:31:52 -07001041 flush_tlb_batched_pending(mm);
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001042 arch_enter_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 do {
1044 pte_t ptent = *pte;
Tobin C Harding166f61b2017-02-24 14:59:01 -08001045 if (pte_none(ptent))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 continue;
Hugh Dickins6f5e6b92006-03-16 23:04:09 -08001047
Minchan Kim7b167b62019-09-24 00:02:24 +00001048 if (need_resched())
1049 break;
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (pte_present(ptent)) {
Hugh Dickinsee498ed2005-11-21 21:32:18 -08001052 struct page *page;
Robin Holt51c6f662005-11-13 16:06:42 -08001053
Christoph Hellwig25b29952019-06-13 22:50:49 +02001054 page = vm_normal_page(vma, addr, ptent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (unlikely(details) && page) {
1056 /*
1057 * unmap_shared_mapping_pages() wants to
1058 * invalidate cache without truncating:
1059 * unmap shared but keep private pages.
1060 */
1061 if (details->check_mapping &&
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001062 details->check_mapping != page_rmapping(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
Nick Pigginb5810032005-10-29 18:16:12 -07001065 ptent = ptep_get_and_clear_full(mm, addr, pte,
Zachary Amsdena6003882005-09-03 15:55:04 -07001066 tlb->fullmm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 tlb_remove_tlb_entry(tlb, pte, addr);
1068 if (unlikely(!page))
1069 continue;
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001070
1071 if (!PageAnon(page)) {
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001072 if (pte_dirty(ptent)) {
1073 force_flush = 1;
Hugh Dickins6237bcd2005-10-29 18:15:54 -07001074 set_page_dirty(page);
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001075 }
Johannes Weiner4917e5d2009-01-06 14:39:17 -08001076 if (pte_young(ptent) &&
Joe Perches64363aa2013-07-08 16:00:18 -07001077 likely(!(vma->vm_flags & VM_SEQ_READ)))
Nick Pigginbf3f3bc2009-01-06 14:38:55 -08001078 mark_page_accessed(page);
Hugh Dickins6237bcd2005-10-29 18:15:54 -07001079 }
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001080 rss[mm_counter(page)]--;
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001081 page_remove_rmap(page, false);
Hugh Dickins3dc14742009-01-06 14:40:08 -08001082 if (unlikely(page_mapcount(page) < 0))
1083 print_bad_pte(vma, addr, ptent, page);
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -07001084 if (unlikely(__tlb_remove_page(tlb, page))) {
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001085 force_flush = 1;
Will Deaconce9ec372014-10-28 13:16:28 -07001086 addr += PAGE_SIZE;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001087 break;
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 continue;
1090 }
Jérôme Glisse5042db42017-09-08 16:11:43 -07001091
1092 entry = pte_to_swp_entry(ptent);
1093 if (non_swap_entry(entry) && is_device_private_entry(entry)) {
1094 struct page *page = device_private_entry_to_page(entry);
1095
1096 if (unlikely(details && details->check_mapping)) {
1097 /*
1098 * unmap_shared_mapping_pages() wants to
1099 * invalidate cache without truncating:
1100 * unmap shared but keep private pages.
1101 */
1102 if (details->check_mapping !=
1103 page_rmapping(page))
1104 continue;
1105 }
1106
1107 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
1108 rss[mm_counter(page)]--;
1109 page_remove_rmap(page, false);
1110 put_page(page);
1111 continue;
1112 }
1113
Kirill A. Shutemov3e8715f2017-02-22 15:46:34 -08001114 /* If details->check_mapping, we leave swap entries. */
1115 if (unlikely(details))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 continue;
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -08001117
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001118 if (!non_swap_entry(entry))
1119 rss[MM_SWAPENTS]--;
1120 else if (is_migration_entry(entry)) {
1121 struct page *page;
Konstantin Khlebnikov9f9f1ac2012-01-20 14:34:24 -08001122
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001123 page = migration_entry_to_page(entry);
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001124 rss[mm_counter(page)]--;
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -08001125 }
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001126 if (unlikely(!free_swap_and_cache(entry)))
1127 print_bad_pte(vma, addr, ptent, NULL);
Zachary Amsden9888a1c2006-09-30 23:29:31 -07001128 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
Peter Zijlstra97a89412011-05-24 17:12:04 -07001129 } while (pte++, addr += PAGE_SIZE, addr != end);
Hugh Dickinsae859762005-10-29 18:16:05 -07001130
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001131 add_mm_rss_vec(mm, rss);
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001132 arch_leave_lazy_mmu_mode();
Robin Holt51c6f662005-11-13 16:06:42 -08001133
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001134 /* Do the actual TLB flush before dropping ptl */
Will Deaconfb7332a2014-10-29 10:03:09 +00001135 if (force_flush)
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001136 tlb_flush_mmu_tlbonly(tlb);
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001137 pte_unmap_unlock(start_pte, ptl);
1138
1139 /*
1140 * If we forced a TLB flush (either due to running out of
1141 * batch buffers or because we needed to flush dirty TLB
1142 * entries before releasing the ptl), free the batched
1143 * memory too. Restart if we didn't do everything.
1144 */
1145 if (force_flush) {
1146 force_flush = 0;
Peter Zijlstrafa0aafb2018-09-20 10:54:04 +02001147 tlb_flush_mmu(tlb);
Minchan Kim7b167b62019-09-24 00:02:24 +00001148 }
1149
1150 if (addr != end) {
1151 cond_resched();
1152 goto again;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001153 }
1154
Robin Holt51c6f662005-11-13 16:06:42 -08001155 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
Robin Holt51c6f662005-11-13 16:06:42 -08001158static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
Nick Pigginb5810032005-10-29 18:16:12 -07001159 struct vm_area_struct *vma, pud_t *pud,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 unsigned long addr, unsigned long end,
Peter Zijlstra97a89412011-05-24 17:12:04 -07001161 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 pmd_t *pmd;
1164 unsigned long next;
1165
1166 pmd = pmd_offset(pud, addr);
1167 do {
1168 next = pmd_addr_end(addr, end);
Zi Yan84c3fc42017-09-08 16:11:01 -07001169 if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
Hugh Dickins53406ed2018-08-01 11:31:52 -07001170 if (next - addr != HPAGE_PMD_SIZE)
David Rientjesfd607752016-12-12 16:42:20 -08001171 __split_huge_pmd(vma, pmd, addr, false, NULL);
Hugh Dickins53406ed2018-08-01 11:31:52 -07001172 else if (zap_huge_pmd(tlb, vma, pmd, addr))
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001173 goto next;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001174 /* fall through */
1175 }
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001176 /*
1177 * Here there can be other concurrent MADV_DONTNEED or
1178 * trans huge page faults running, and if the pmd is
1179 * none or trans huge it can change under us. This is
1180 * because MADV_DONTNEED holds the mmap_sem in read
1181 * mode.
1182 */
1183 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
1184 goto next;
Peter Zijlstra97a89412011-05-24 17:12:04 -07001185 next = zap_pte_range(tlb, vma, pmd, addr, next, details);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001186next:
Peter Zijlstra97a89412011-05-24 17:12:04 -07001187 cond_resched();
1188 } while (pmd++, addr = next, addr != end);
Robin Holt51c6f662005-11-13 16:06:42 -08001189
1190 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
Robin Holt51c6f662005-11-13 16:06:42 -08001193static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001194 struct vm_area_struct *vma, p4d_t *p4d,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 unsigned long addr, unsigned long end,
Peter Zijlstra97a89412011-05-24 17:12:04 -07001196 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
1198 pud_t *pud;
1199 unsigned long next;
1200
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001201 pud = pud_offset(p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 do {
1203 next = pud_addr_end(addr, end);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001204 if (pud_trans_huge(*pud) || pud_devmap(*pud)) {
1205 if (next - addr != HPAGE_PUD_SIZE) {
1206 VM_BUG_ON_VMA(!rwsem_is_locked(&tlb->mm->mmap_sem), vma);
1207 split_huge_pud(vma, pud, addr);
1208 } else if (zap_huge_pud(tlb, vma, pud, addr))
1209 goto next;
1210 /* fall through */
1211 }
Peter Zijlstra97a89412011-05-24 17:12:04 -07001212 if (pud_none_or_clear_bad(pud))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 continue;
Peter Zijlstra97a89412011-05-24 17:12:04 -07001214 next = zap_pmd_range(tlb, vma, pud, addr, next, details);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001215next:
1216 cond_resched();
Peter Zijlstra97a89412011-05-24 17:12:04 -07001217 } while (pud++, addr = next, addr != end);
Robin Holt51c6f662005-11-13 16:06:42 -08001218
1219 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001222static inline unsigned long zap_p4d_range(struct mmu_gather *tlb,
1223 struct vm_area_struct *vma, pgd_t *pgd,
1224 unsigned long addr, unsigned long end,
1225 struct zap_details *details)
1226{
1227 p4d_t *p4d;
1228 unsigned long next;
1229
1230 p4d = p4d_offset(pgd, addr);
1231 do {
1232 next = p4d_addr_end(addr, end);
1233 if (p4d_none_or_clear_bad(p4d))
1234 continue;
1235 next = zap_pud_range(tlb, vma, p4d, addr, next, details);
1236 } while (p4d++, addr = next, addr != end);
1237
1238 return addr;
1239}
1240
Michal Hockoaac45362016-03-25 14:20:24 -07001241void unmap_page_range(struct mmu_gather *tlb,
Al Viro038c7aa2012-03-05 13:25:09 -05001242 struct vm_area_struct *vma,
1243 unsigned long addr, unsigned long end,
1244 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 pgd_t *pgd;
1247 unsigned long next;
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 BUG_ON(addr >= end);
1250 tlb_start_vma(tlb, vma);
1251 pgd = pgd_offset(vma->vm_mm, addr);
1252 do {
1253 next = pgd_addr_end(addr, end);
Peter Zijlstra97a89412011-05-24 17:12:04 -07001254 if (pgd_none_or_clear_bad(pgd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 continue;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001256 next = zap_p4d_range(tlb, vma, pgd, addr, next, details);
Peter Zijlstra97a89412011-05-24 17:12:04 -07001257 } while (pgd++, addr = next, addr != end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 tlb_end_vma(tlb, vma);
1259}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Al Virof5cc4ee2012-03-05 14:14:20 -05001261
1262static void unmap_single_vma(struct mmu_gather *tlb,
1263 struct vm_area_struct *vma, unsigned long start_addr,
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001264 unsigned long end_addr,
Al Virof5cc4ee2012-03-05 14:14:20 -05001265 struct zap_details *details)
1266{
1267 unsigned long start = max(vma->vm_start, start_addr);
1268 unsigned long end;
1269
1270 if (start >= vma->vm_end)
1271 return;
1272 end = min(vma->vm_end, end_addr);
1273 if (end <= vma->vm_start)
1274 return;
1275
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301276 if (vma->vm_file)
1277 uprobe_munmap(vma, start, end);
1278
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001279 if (unlikely(vma->vm_flags & VM_PFNMAP))
Suresh Siddha5180da42012-10-08 16:28:29 -07001280 untrack_pfn(vma, 0, 0);
Al Virof5cc4ee2012-03-05 14:14:20 -05001281
1282 if (start != end) {
1283 if (unlikely(is_vm_hugetlb_page(vma))) {
1284 /*
1285 * It is undesirable to test vma->vm_file as it
1286 * should be non-null for valid hugetlb area.
1287 * However, vm_file will be NULL in the error
Davidlohr Bueso7aa6b4a2014-04-07 15:37:01 -07001288 * cleanup path of mmap_region. When
Al Virof5cc4ee2012-03-05 14:14:20 -05001289 * hugetlbfs ->mmap method fails,
Davidlohr Bueso7aa6b4a2014-04-07 15:37:01 -07001290 * mmap_region() nullifies vma->vm_file
Al Virof5cc4ee2012-03-05 14:14:20 -05001291 * before calling this function to clean up.
1292 * Since no pte has actually been setup, it is
1293 * safe to do nothing in this case.
1294 */
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07001295 if (vma->vm_file) {
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08001296 i_mmap_lock_write(vma->vm_file->f_mapping);
Mel Gormand8333522012-07-31 16:46:20 -07001297 __unmap_hugepage_range_final(tlb, vma, start, end, NULL);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08001298 i_mmap_unlock_write(vma->vm_file->f_mapping);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07001299 }
Al Virof5cc4ee2012-03-05 14:14:20 -05001300 } else
1301 unmap_page_range(tlb, vma, start, end, details);
1302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305/**
1306 * unmap_vmas - unmap a range of memory covered by a list of vma's
Randy Dunlap0164f692011-06-15 15:08:09 -07001307 * @tlb: address of the caller's struct mmu_gather
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 * @vma: the starting vma
1309 * @start_addr: virtual address at which to start unmapping
1310 * @end_addr: virtual address at which to end unmapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 *
Hugh Dickins508034a2005-10-29 18:16:30 -07001312 * Unmap all pages in the vma list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 * Only addresses between `start' and `end' will be unmapped.
1315 *
1316 * The VMA list must be sorted in ascending virtual address order.
1317 *
1318 * unmap_vmas() assumes that the caller will flush the whole unmapped address
1319 * range after unmap_vmas() returns. So the only responsibility here is to
1320 * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
1321 * drops the lock and schedules.
1322 */
Al Viro6e8bb012012-03-05 13:41:15 -05001323void unmap_vmas(struct mmu_gather *tlb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 struct vm_area_struct *vma, unsigned long start_addr,
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001325 unsigned long end_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001327 struct mmu_notifier_range range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07001329 mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, vma->vm_mm,
1330 start_addr, end_addr);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001331 mmu_notifier_invalidate_range_start(&range);
Al Virof5cc4ee2012-03-05 14:14:20 -05001332 for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001333 unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001334 mmu_notifier_invalidate_range_end(&range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
1337/**
1338 * zap_page_range - remove user pages in a given range
1339 * @vma: vm_area_struct holding the applicable pages
Randy Dunlapeb4546b2012-06-20 12:53:02 -07001340 * @start: starting address of pages to zap
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 * @size: number of bytes to zap
Al Virof5cc4ee2012-03-05 14:14:20 -05001342 *
1343 * Caller must protect the VMA list
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 */
Linus Torvalds7e027b12012-05-06 13:43:15 -07001345void zap_page_range(struct vm_area_struct *vma, unsigned long start,
Kirill A. Shutemovecf13852017-02-22 15:46:37 -08001346 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001348 struct mmu_notifier_range range;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001349 struct mmu_gather tlb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 lru_add_drain();
Jérôme Glisse7269f992019-05-13 17:20:53 -07001352 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07001353 start, start + size);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001354 tlb_gather_mmu(&tlb, vma->vm_mm, start, range.end);
1355 update_hiwater_rss(vma->vm_mm);
1356 mmu_notifier_invalidate_range_start(&range);
1357 for ( ; vma && vma->vm_start < range.end; vma = vma->vm_next)
1358 unmap_single_vma(&tlb, vma, start, range.end, NULL);
1359 mmu_notifier_invalidate_range_end(&range);
1360 tlb_finish_mmu(&tlb, start, range.end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
Jack Steinerc627f9c2008-07-29 22:33:53 -07001363/**
Al Virof5cc4ee2012-03-05 14:14:20 -05001364 * zap_page_range_single - remove user pages in a given range
1365 * @vma: vm_area_struct holding the applicable pages
1366 * @address: starting address of pages to zap
1367 * @size: number of bytes to zap
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001368 * @details: details of shared cache invalidation
Al Virof5cc4ee2012-03-05 14:14:20 -05001369 *
1370 * The range must fit into one VMA.
1371 */
1372static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
1373 unsigned long size, struct zap_details *details)
1374{
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001375 struct mmu_notifier_range range;
Al Virof5cc4ee2012-03-05 14:14:20 -05001376 struct mmu_gather tlb;
Al Virof5cc4ee2012-03-05 14:14:20 -05001377
1378 lru_add_drain();
Jérôme Glisse7269f992019-05-13 17:20:53 -07001379 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07001380 address, address + size);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001381 tlb_gather_mmu(&tlb, vma->vm_mm, address, range.end);
1382 update_hiwater_rss(vma->vm_mm);
1383 mmu_notifier_invalidate_range_start(&range);
1384 unmap_single_vma(&tlb, vma, address, range.end, details);
1385 mmu_notifier_invalidate_range_end(&range);
1386 tlb_finish_mmu(&tlb, address, range.end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
Jack Steinerc627f9c2008-07-29 22:33:53 -07001389/**
1390 * zap_vma_ptes - remove ptes mapping the vma
1391 * @vma: vm_area_struct holding ptes to be zapped
1392 * @address: starting address of pages to zap
1393 * @size: number of bytes to zap
1394 *
1395 * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1396 *
1397 * The entire address range must be fully contained within the vma.
1398 *
Jack Steinerc627f9c2008-07-29 22:33:53 -07001399 */
Leon Romanovsky27d036e2018-05-29 15:14:07 +03001400void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
Jack Steinerc627f9c2008-07-29 22:33:53 -07001401 unsigned long size)
1402{
1403 if (address < vma->vm_start || address + size > vma->vm_end ||
1404 !(vma->vm_flags & VM_PFNMAP))
Leon Romanovsky27d036e2018-05-29 15:14:07 +03001405 return;
1406
Al Virof5cc4ee2012-03-05 14:14:20 -05001407 zap_page_range_single(vma, address, size, NULL);
Jack Steinerc627f9c2008-07-29 22:33:53 -07001408}
1409EXPORT_SYMBOL_GPL(zap_vma_ptes);
1410
Namhyung Kim25ca1d62010-10-26 14:21:59 -07001411pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
Harvey Harrison920c7a52008-02-04 22:29:26 -08001412 spinlock_t **ptl)
Linus Torvaldsc9cfcddf2005-11-29 14:03:14 -08001413{
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001414 pgd_t *pgd;
1415 p4d_t *p4d;
1416 pud_t *pud;
1417 pmd_t *pmd;
1418
1419 pgd = pgd_offset(mm, addr);
1420 p4d = p4d_alloc(mm, pgd, addr);
1421 if (!p4d)
1422 return NULL;
1423 pud = pud_alloc(mm, p4d, addr);
1424 if (!pud)
1425 return NULL;
1426 pmd = pmd_alloc(mm, pud, addr);
1427 if (!pmd)
1428 return NULL;
1429
1430 VM_BUG_ON(pmd_trans_huge(*pmd));
1431 return pte_alloc_map_lock(mm, pmd, addr, ptl);
Linus Torvaldsc9cfcddf2005-11-29 14:03:14 -08001432}
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434/*
Linus Torvalds238f58d2005-11-29 13:01:56 -08001435 * This is the old fallback for page remapping.
1436 *
1437 * For historical reasons, it only allows reserved pages. Only
1438 * old drivers should use this, and they needed to mark their
1439 * pages reserved for the old functions anyway.
1440 */
Nick Piggin423bad602008-04-28 02:13:01 -07001441static int insert_page(struct vm_area_struct *vma, unsigned long addr,
1442 struct page *page, pgprot_t prot)
Linus Torvalds238f58d2005-11-29 13:01:56 -08001443{
Nick Piggin423bad602008-04-28 02:13:01 -07001444 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds238f58d2005-11-29 13:01:56 -08001445 int retval;
Linus Torvaldsc9cfcddf2005-11-29 14:03:14 -08001446 pte_t *pte;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001447 spinlock_t *ptl;
1448
Linus Torvalds238f58d2005-11-29 13:01:56 -08001449 retval = -EINVAL;
Matthew Wilcox0ee930e2019-03-05 15:46:06 -08001450 if (PageAnon(page) || PageSlab(page) || page_has_type(page))
KAMEZAWA Hiroyuki5b4e6552008-10-18 20:28:10 -07001451 goto out;
Linus Torvalds238f58d2005-11-29 13:01:56 -08001452 retval = -ENOMEM;
1453 flush_dcache_page(page);
Linus Torvaldsc9cfcddf2005-11-29 14:03:14 -08001454 pte = get_locked_pte(mm, addr, &ptl);
Linus Torvalds238f58d2005-11-29 13:01:56 -08001455 if (!pte)
KAMEZAWA Hiroyuki5b4e6552008-10-18 20:28:10 -07001456 goto out;
Linus Torvalds238f58d2005-11-29 13:01:56 -08001457 retval = -EBUSY;
1458 if (!pte_none(*pte))
1459 goto out_unlock;
1460
1461 /* Ok, finally just insert the thing.. */
1462 get_page(page);
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001463 inc_mm_counter_fast(mm, mm_counter_file(page));
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07001464 page_add_file_rmap(page, false);
Linus Torvalds238f58d2005-11-29 13:01:56 -08001465 set_pte_at(mm, addr, pte, mk_pte(page, prot));
1466
1467 retval = 0;
1468out_unlock:
1469 pte_unmap_unlock(pte, ptl);
1470out:
1471 return retval;
1472}
1473
Rolf Eike Beerbfa5bf62006-09-25 23:31:22 -07001474/**
1475 * vm_insert_page - insert single page into user vma
1476 * @vma: user vma to map to
1477 * @addr: target user address of this page
1478 * @page: source kernel page
1479 *
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001480 * This allows drivers to insert individual pages they've allocated
1481 * into a user vma.
1482 *
1483 * The page has to be a nice clean _individual_ kernel allocation.
1484 * If you allocate a compound page, you need to have marked it as
1485 * such (__GFP_COMP), or manually just split the page up yourself
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001486 * (see split_page()).
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001487 *
1488 * NOTE! Traditionally this was done with "remap_pfn_range()" which
1489 * took an arbitrary page protection parameter. This doesn't allow
1490 * that. Your vma protection will have to be set up correctly, which
1491 * means that if you want a shared writable mapping, you'd better
1492 * ask for a shared writable mapping!
1493 *
1494 * The page does not need to be reserved.
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001495 *
1496 * Usually this function is called from f_op->mmap() handler
1497 * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
1498 * Caller must set VM_MIXEDMAP on vma if it wants to call this
1499 * function from other places, for example from page-fault handler.
Mike Rapoporta862f682019-03-05 15:48:42 -08001500 *
1501 * Return: %0 on success, negative error code otherwise.
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001502 */
Nick Piggin423bad602008-04-28 02:13:01 -07001503int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
1504 struct page *page)
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001505{
1506 if (addr < vma->vm_start || addr >= vma->vm_end)
1507 return -EFAULT;
1508 if (!page_count(page))
1509 return -EINVAL;
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001510 if (!(vma->vm_flags & VM_MIXEDMAP)) {
1511 BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem));
1512 BUG_ON(vma->vm_flags & VM_PFNMAP);
1513 vma->vm_flags |= VM_MIXEDMAP;
1514 }
Nick Piggin423bad602008-04-28 02:13:01 -07001515 return insert_page(vma, addr, page, vma->vm_page_prot);
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001516}
Linus Torvaldse3c33742005-12-03 20:48:11 -08001517EXPORT_SYMBOL(vm_insert_page);
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001518
Souptick Joardera667d742019-05-13 17:21:56 -07001519/*
1520 * __vm_map_pages - maps range of kernel pages into user vma
1521 * @vma: user vma to map to
1522 * @pages: pointer to array of source kernel pages
1523 * @num: number of pages in page array
1524 * @offset: user's requested vm_pgoff
1525 *
1526 * This allows drivers to map range of kernel pages into a user vma.
1527 *
1528 * Return: 0 on success and error code otherwise.
1529 */
1530static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages,
1531 unsigned long num, unsigned long offset)
1532{
1533 unsigned long count = vma_pages(vma);
1534 unsigned long uaddr = vma->vm_start;
1535 int ret, i;
1536
1537 /* Fail if the user requested offset is beyond the end of the object */
Miguel Ojeda96756fc2019-07-11 20:58:47 -07001538 if (offset >= num)
Souptick Joardera667d742019-05-13 17:21:56 -07001539 return -ENXIO;
1540
1541 /* Fail if the user requested size exceeds available object size */
1542 if (count > num - offset)
1543 return -ENXIO;
1544
1545 for (i = 0; i < count; i++) {
1546 ret = vm_insert_page(vma, uaddr, pages[offset + i]);
1547 if (ret < 0)
1548 return ret;
1549 uaddr += PAGE_SIZE;
1550 }
1551
1552 return 0;
1553}
1554
1555/**
1556 * vm_map_pages - maps range of kernel pages starts with non zero offset
1557 * @vma: user vma to map to
1558 * @pages: pointer to array of source kernel pages
1559 * @num: number of pages in page array
1560 *
1561 * Maps an object consisting of @num pages, catering for the user's
1562 * requested vm_pgoff
1563 *
1564 * If we fail to insert any page into the vma, the function will return
1565 * immediately leaving any previously inserted pages present. Callers
1566 * from the mmap handler may immediately return the error as their caller
1567 * will destroy the vma, removing any successfully inserted pages. Other
1568 * callers should make their own arrangements for calling unmap_region().
1569 *
1570 * Context: Process context. Called by mmap handlers.
1571 * Return: 0 on success and error code otherwise.
1572 */
1573int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
1574 unsigned long num)
1575{
1576 return __vm_map_pages(vma, pages, num, vma->vm_pgoff);
1577}
1578EXPORT_SYMBOL(vm_map_pages);
1579
1580/**
1581 * vm_map_pages_zero - map range of kernel pages starts with zero offset
1582 * @vma: user vma to map to
1583 * @pages: pointer to array of source kernel pages
1584 * @num: number of pages in page array
1585 *
1586 * Similar to vm_map_pages(), except that it explicitly sets the offset
1587 * to 0. This function is intended for the drivers that did not consider
1588 * vm_pgoff.
1589 *
1590 * Context: Process context. Called by mmap handlers.
1591 * Return: 0 on success and error code otherwise.
1592 */
1593int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
1594 unsigned long num)
1595{
1596 return __vm_map_pages(vma, pages, num, 0);
1597}
1598EXPORT_SYMBOL(vm_map_pages_zero);
1599
Matthew Wilcox9b5a8e02018-10-26 15:04:40 -07001600static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr,
Ross Zwislerb2770da62017-09-06 16:18:35 -07001601 pfn_t pfn, pgprot_t prot, bool mkwrite)
Nick Piggin423bad602008-04-28 02:13:01 -07001602{
1603 struct mm_struct *mm = vma->vm_mm;
Nick Piggin423bad602008-04-28 02:13:01 -07001604 pte_t *pte, entry;
1605 spinlock_t *ptl;
1606
Nick Piggin423bad602008-04-28 02:13:01 -07001607 pte = get_locked_pte(mm, addr, &ptl);
1608 if (!pte)
Matthew Wilcox9b5a8e02018-10-26 15:04:40 -07001609 return VM_FAULT_OOM;
Ross Zwislerb2770da62017-09-06 16:18:35 -07001610 if (!pte_none(*pte)) {
1611 if (mkwrite) {
1612 /*
1613 * For read faults on private mappings the PFN passed
1614 * in may not match the PFN we have mapped if the
1615 * mapped PFN is a writeable COW page. In the mkwrite
1616 * case we are creating a writable PTE for a shared
Jan Karaf2c57d92018-10-30 15:10:47 -07001617 * mapping and we expect the PFNs to match. If they
1618 * don't match, we are likely racing with block
1619 * allocation and mapping invalidation so just skip the
1620 * update.
Ross Zwislerb2770da62017-09-06 16:18:35 -07001621 */
Jan Karaf2c57d92018-10-30 15:10:47 -07001622 if (pte_pfn(*pte) != pfn_t_to_pfn(pfn)) {
1623 WARN_ON_ONCE(!is_zero_pfn(pte_pfn(*pte)));
Ross Zwislerb2770da62017-09-06 16:18:35 -07001624 goto out_unlock;
Jan Karaf2c57d92018-10-30 15:10:47 -07001625 }
Jan Karacae85cb2019-03-28 20:43:19 -07001626 entry = pte_mkyoung(*pte);
1627 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1628 if (ptep_set_access_flags(vma, addr, pte, entry, 1))
1629 update_mmu_cache(vma, addr, pte);
1630 }
1631 goto out_unlock;
Ross Zwislerb2770da62017-09-06 16:18:35 -07001632 }
Nick Piggin423bad602008-04-28 02:13:01 -07001633
1634 /* Ok, finally just insert the thing.. */
Dan Williams01c8f1c2016-01-15 16:56:40 -08001635 if (pfn_t_devmap(pfn))
1636 entry = pte_mkdevmap(pfn_t_pte(pfn, prot));
1637 else
1638 entry = pte_mkspecial(pfn_t_pte(pfn, prot));
Ross Zwislerb2770da62017-09-06 16:18:35 -07001639
Ross Zwislerb2770da62017-09-06 16:18:35 -07001640 if (mkwrite) {
1641 entry = pte_mkyoung(entry);
1642 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1643 }
1644
Nick Piggin423bad602008-04-28 02:13:01 -07001645 set_pte_at(mm, addr, pte, entry);
Russell King4b3073e2009-12-18 16:40:18 +00001646 update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
Nick Piggin423bad602008-04-28 02:13:01 -07001647
Nick Piggin423bad602008-04-28 02:13:01 -07001648out_unlock:
1649 pte_unmap_unlock(pte, ptl);
Matthew Wilcox9b5a8e02018-10-26 15:04:40 -07001650 return VM_FAULT_NOPAGE;
Nick Piggin423bad602008-04-28 02:13:01 -07001651}
1652
Matthew Wilcoxf5e6d1d2018-10-26 15:04:13 -07001653/**
1654 * vmf_insert_pfn_prot - insert single pfn into user vma with specified pgprot
1655 * @vma: user vma to map to
1656 * @addr: target user address of this page
1657 * @pfn: source kernel pfn
1658 * @pgprot: pgprot flags for the inserted page
1659 *
1660 * This is exactly like vmf_insert_pfn(), except that it allows drivers to
1661 * to override pgprot on a per-page basis.
1662 *
1663 * This only makes sense for IO mappings, and it makes no sense for
1664 * COW mappings. In general, using multiple vmas is preferable;
Matthew Wilcoxae2b01f2018-10-26 15:04:29 -07001665 * vmf_insert_pfn_prot should only be used if using multiple VMAs is
Matthew Wilcoxf5e6d1d2018-10-26 15:04:13 -07001666 * impractical.
1667 *
Matthew Wilcoxae2b01f2018-10-26 15:04:29 -07001668 * Context: Process context. May allocate using %GFP_KERNEL.
Matthew Wilcoxf5e6d1d2018-10-26 15:04:13 -07001669 * Return: vm_fault_t value.
1670 */
1671vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
1672 unsigned long pfn, pgprot_t pgprot)
1673{
Matthew Wilcox6d958542018-10-26 15:04:33 -07001674 /*
1675 * Technically, architectures with pte_special can avoid all these
1676 * restrictions (same for remap_pfn_range). However we would like
1677 * consistency in testing and feature parity among all, so we should
1678 * try to keep these invariants in place for everybody.
1679 */
1680 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
1681 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
1682 (VM_PFNMAP|VM_MIXEDMAP));
1683 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
1684 BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
1685
1686 if (addr < vma->vm_start || addr >= vma->vm_end)
1687 return VM_FAULT_SIGBUS;
1688
1689 if (!pfn_modify_allowed(pfn, pgprot))
1690 return VM_FAULT_SIGBUS;
1691
1692 track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV));
1693
Matthew Wilcox9b5a8e02018-10-26 15:04:40 -07001694 return insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot,
Matthew Wilcox6d958542018-10-26 15:04:33 -07001695 false);
Matthew Wilcoxf5e6d1d2018-10-26 15:04:13 -07001696}
1697EXPORT_SYMBOL(vmf_insert_pfn_prot);
Nick Piggine0dc0d82007-02-12 00:51:36 -08001698
Matthew Wilcoxae2b01f2018-10-26 15:04:29 -07001699/**
1700 * vmf_insert_pfn - insert single pfn into user vma
1701 * @vma: user vma to map to
1702 * @addr: target user address of this page
1703 * @pfn: source kernel pfn
1704 *
1705 * Similar to vm_insert_page, this allows drivers to insert individual pages
1706 * they've allocated into a user vma. Same comments apply.
1707 *
1708 * This function should only be called from a vm_ops->fault handler, and
1709 * in that case the handler should return the result of this function.
1710 *
1711 * vma cannot be a COW mapping.
1712 *
1713 * As this is called only for pages that do not currently exist, we
1714 * do not need to flush old virtual caches or the TLB.
1715 *
1716 * Context: Process context. May allocate using %GFP_KERNEL.
1717 * Return: vm_fault_t value.
1718 */
1719vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
1720 unsigned long pfn)
1721{
1722 return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot);
1723}
1724EXPORT_SYMBOL(vmf_insert_pfn);
1725
Dan Williams785a3fa2017-10-23 07:20:00 -07001726static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn)
1727{
1728 /* these checks mirror the abort conditions in vm_normal_page */
1729 if (vma->vm_flags & VM_MIXEDMAP)
1730 return true;
1731 if (pfn_t_devmap(pfn))
1732 return true;
1733 if (pfn_t_special(pfn))
1734 return true;
1735 if (is_zero_pfn(pfn_t_to_pfn(pfn)))
1736 return true;
1737 return false;
1738}
1739
Matthew Wilcox79f3aa52018-10-26 15:04:37 -07001740static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma,
1741 unsigned long addr, pfn_t pfn, bool mkwrite)
Nick Piggin423bad602008-04-28 02:13:01 -07001742{
Dan Williams87744ab2016-10-07 17:00:18 -07001743 pgprot_t pgprot = vma->vm_page_prot;
Matthew Wilcox79f3aa52018-10-26 15:04:37 -07001744 int err;
Dan Williams87744ab2016-10-07 17:00:18 -07001745
Dan Williams785a3fa2017-10-23 07:20:00 -07001746 BUG_ON(!vm_mixed_ok(vma, pfn));
Nick Piggin423bad602008-04-28 02:13:01 -07001747
1748 if (addr < vma->vm_start || addr >= vma->vm_end)
Matthew Wilcox79f3aa52018-10-26 15:04:37 -07001749 return VM_FAULT_SIGBUS;
Borislav Petkov308a0472016-10-26 19:43:43 +02001750
1751 track_pfn_insert(vma, &pgprot, pfn);
Nick Piggin423bad602008-04-28 02:13:01 -07001752
Andi Kleen42e40892018-06-13 15:48:27 -07001753 if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot))
Matthew Wilcox79f3aa52018-10-26 15:04:37 -07001754 return VM_FAULT_SIGBUS;
Andi Kleen42e40892018-06-13 15:48:27 -07001755
Nick Piggin423bad602008-04-28 02:13:01 -07001756 /*
1757 * If we don't have pte special, then we have to use the pfn_valid()
1758 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
1759 * refcount the page if pfn_valid is true (hence insert_page rather
Hugh Dickins62eede62009-09-21 17:03:34 -07001760 * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP
1761 * without pte special, it would there be refcounted as a normal page.
Nick Piggin423bad602008-04-28 02:13:01 -07001762 */
Laurent Dufour00b3a332018-06-07 17:06:12 -07001763 if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) &&
1764 !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) {
Nick Piggin423bad602008-04-28 02:13:01 -07001765 struct page *page;
1766
Dan Williams03fc2da2016-01-26 09:48:05 -08001767 /*
1768 * At this point we are committed to insert_page()
1769 * regardless of whether the caller specified flags that
1770 * result in pfn_t_has_page() == false.
1771 */
1772 page = pfn_to_page(pfn_t_to_pfn(pfn));
Matthew Wilcox79f3aa52018-10-26 15:04:37 -07001773 err = insert_page(vma, addr, page, pgprot);
1774 } else {
Matthew Wilcox9b5a8e02018-10-26 15:04:40 -07001775 return insert_pfn(vma, addr, pfn, pgprot, mkwrite);
Nick Piggin423bad602008-04-28 02:13:01 -07001776 }
Ross Zwislerb2770da62017-09-06 16:18:35 -07001777
Matthew Wilcox5d747632018-10-26 15:04:10 -07001778 if (err == -ENOMEM)
1779 return VM_FAULT_OOM;
1780 if (err < 0 && err != -EBUSY)
1781 return VM_FAULT_SIGBUS;
1782
1783 return VM_FAULT_NOPAGE;
Nick Piggin423bad602008-04-28 02:13:01 -07001784}
Matthew Wilcox79f3aa52018-10-26 15:04:37 -07001785
1786vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
1787 pfn_t pfn)
1788{
1789 return __vm_insert_mixed(vma, addr, pfn, false);
1790}
Matthew Wilcox5d747632018-10-26 15:04:10 -07001791EXPORT_SYMBOL(vmf_insert_mixed);
Nick Piggin423bad602008-04-28 02:13:01 -07001792
Souptick Joarderab77dab2018-06-07 17:04:29 -07001793/*
1794 * If the insertion of PTE failed because someone else already added a
1795 * different entry in the mean time, we treat that as success as we assume
1796 * the same entry was actually inserted.
1797 */
Souptick Joarderab77dab2018-06-07 17:04:29 -07001798vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma,
1799 unsigned long addr, pfn_t pfn)
Ross Zwislerb2770da62017-09-06 16:18:35 -07001800{
Matthew Wilcox79f3aa52018-10-26 15:04:37 -07001801 return __vm_insert_mixed(vma, addr, pfn, true);
Ross Zwislerb2770da62017-09-06 16:18:35 -07001802}
Souptick Joarderab77dab2018-06-07 17:04:29 -07001803EXPORT_SYMBOL(vmf_insert_mixed_mkwrite);
Ross Zwislerb2770da62017-09-06 16:18:35 -07001804
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001805/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 * maps a range of physical memory into the requested pages. the old
1807 * mappings are removed. any references to nonexistent pages results
1808 * in null mappings (currently treated as "copy-on-access")
1809 */
1810static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
1811 unsigned long addr, unsigned long end,
1812 unsigned long pfn, pgprot_t prot)
1813{
1814 pte_t *pte;
Hugh Dickinsc74df322005-10-29 18:16:23 -07001815 spinlock_t *ptl;
Andi Kleen42e40892018-06-13 15:48:27 -07001816 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Hugh Dickinsc74df322005-10-29 18:16:23 -07001818 pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (!pte)
1820 return -ENOMEM;
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001821 arch_enter_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 do {
1823 BUG_ON(!pte_none(*pte));
Andi Kleen42e40892018-06-13 15:48:27 -07001824 if (!pfn_modify_allowed(pfn, prot)) {
1825 err = -EACCES;
1826 break;
1827 }
Nick Piggin7e675132008-04-28 02:13:00 -07001828 set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 pfn++;
1830 } while (pte++, addr += PAGE_SIZE, addr != end);
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001831 arch_leave_lazy_mmu_mode();
Hugh Dickinsc74df322005-10-29 18:16:23 -07001832 pte_unmap_unlock(pte - 1, ptl);
Andi Kleen42e40892018-06-13 15:48:27 -07001833 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
1836static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
1837 unsigned long addr, unsigned long end,
1838 unsigned long pfn, pgprot_t prot)
1839{
1840 pmd_t *pmd;
1841 unsigned long next;
Andi Kleen42e40892018-06-13 15:48:27 -07001842 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 pfn -= addr >> PAGE_SHIFT;
1845 pmd = pmd_alloc(mm, pud, addr);
1846 if (!pmd)
1847 return -ENOMEM;
Andrea Arcangelif66055ab2011-01-13 15:46:54 -08001848 VM_BUG_ON(pmd_trans_huge(*pmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 do {
1850 next = pmd_addr_end(addr, end);
Andi Kleen42e40892018-06-13 15:48:27 -07001851 err = remap_pte_range(mm, pmd, addr, next,
1852 pfn + (addr >> PAGE_SHIFT), prot);
1853 if (err)
1854 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 } while (pmd++, addr = next, addr != end);
1856 return 0;
1857}
1858
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001859static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 unsigned long addr, unsigned long end,
1861 unsigned long pfn, pgprot_t prot)
1862{
1863 pud_t *pud;
1864 unsigned long next;
Andi Kleen42e40892018-06-13 15:48:27 -07001865 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 pfn -= addr >> PAGE_SHIFT;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001868 pud = pud_alloc(mm, p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (!pud)
1870 return -ENOMEM;
1871 do {
1872 next = pud_addr_end(addr, end);
Andi Kleen42e40892018-06-13 15:48:27 -07001873 err = remap_pmd_range(mm, pud, addr, next,
1874 pfn + (addr >> PAGE_SHIFT), prot);
1875 if (err)
1876 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 } while (pud++, addr = next, addr != end);
1878 return 0;
1879}
1880
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001881static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd,
1882 unsigned long addr, unsigned long end,
1883 unsigned long pfn, pgprot_t prot)
1884{
1885 p4d_t *p4d;
1886 unsigned long next;
Andi Kleen42e40892018-06-13 15:48:27 -07001887 int err;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001888
1889 pfn -= addr >> PAGE_SHIFT;
1890 p4d = p4d_alloc(mm, pgd, addr);
1891 if (!p4d)
1892 return -ENOMEM;
1893 do {
1894 next = p4d_addr_end(addr, end);
Andi Kleen42e40892018-06-13 15:48:27 -07001895 err = remap_pud_range(mm, p4d, addr, next,
1896 pfn + (addr >> PAGE_SHIFT), prot);
1897 if (err)
1898 return err;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001899 } while (p4d++, addr = next, addr != end);
1900 return 0;
1901}
1902
Rolf Eike Beerbfa5bf62006-09-25 23:31:22 -07001903/**
1904 * remap_pfn_range - remap kernel memory to userspace
1905 * @vma: user vma to map to
1906 * @addr: target user address to start at
1907 * @pfn: physical address of kernel memory
1908 * @size: size of map area
1909 * @prot: page protection flags for this mapping
1910 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001911 * Note: this is only safe if the mm semaphore is held when called.
1912 *
1913 * Return: %0 on success, negative error code otherwise.
Rolf Eike Beerbfa5bf62006-09-25 23:31:22 -07001914 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1916 unsigned long pfn, unsigned long size, pgprot_t prot)
1917{
1918 pgd_t *pgd;
1919 unsigned long next;
Hugh Dickins2d15cab2005-06-25 14:54:33 -07001920 unsigned long end = addr + PAGE_ALIGN(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 struct mm_struct *mm = vma->vm_mm;
Yongji Xied5957d22016-05-20 16:57:41 -07001922 unsigned long remap_pfn = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 int err;
1924
1925 /*
1926 * Physically remapped pages are special. Tell the
1927 * rest of the world about it:
1928 * VM_IO tells people not to look at these pages
1929 * (accesses can have side effects).
Linus Torvalds6aab3412005-11-28 14:34:23 -08001930 * VM_PFNMAP tells the core MM that the base pages are just
1931 * raw PFN mappings, and do not have a "struct page" associated
1932 * with them.
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001933 * VM_DONTEXPAND
1934 * Disable vma merging and expanding with mremap().
1935 * VM_DONTDUMP
1936 * Omit vma from core dump, even when VM_IO turned off.
Linus Torvaldsfb155c12005-12-11 19:46:02 -08001937 *
1938 * There's a horrible special case to handle copy-on-write
1939 * behaviour that some programs depend on. We mark the "original"
1940 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001941 * See vm_normal_page() for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 */
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001943 if (is_cow_mapping(vma->vm_flags)) {
1944 if (addr != vma->vm_start || end != vma->vm_end)
1945 return -EINVAL;
Linus Torvaldsfb155c12005-12-11 19:46:02 -08001946 vma->vm_pgoff = pfn;
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001947 }
1948
Yongji Xied5957d22016-05-20 16:57:41 -07001949 err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size));
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001950 if (err)
venkatesh.pallipadi@intel.com3c8bb732008-12-18 11:41:27 -08001951 return -EINVAL;
Linus Torvaldsfb155c12005-12-11 19:46:02 -08001952
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001953 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 BUG_ON(addr >= end);
1956 pfn -= addr >> PAGE_SHIFT;
1957 pgd = pgd_offset(mm, addr);
1958 flush_cache_range(vma, addr, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 do {
1960 next = pgd_addr_end(addr, end);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001961 err = remap_p4d_range(mm, pgd, addr, next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 pfn + (addr >> PAGE_SHIFT), prot);
1963 if (err)
1964 break;
1965 } while (pgd++, addr = next, addr != end);
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001966
1967 if (err)
Yongji Xied5957d22016-05-20 16:57:41 -07001968 untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size));
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 return err;
1971}
1972EXPORT_SYMBOL(remap_pfn_range);
1973
Linus Torvaldsb4cbb192013-04-16 13:45:37 -07001974/**
1975 * vm_iomap_memory - remap memory to userspace
1976 * @vma: user vma to map to
1977 * @start: start of area
1978 * @len: size of area
1979 *
1980 * This is a simplified io_remap_pfn_range() for common driver use. The
1981 * driver just needs to give us the physical memory range to be mapped,
1982 * we'll figure out the rest from the vma information.
1983 *
1984 * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
1985 * whatever write-combining details or similar.
Mike Rapoporta862f682019-03-05 15:48:42 -08001986 *
1987 * Return: %0 on success, negative error code otherwise.
Linus Torvaldsb4cbb192013-04-16 13:45:37 -07001988 */
1989int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1990{
1991 unsigned long vm_len, pfn, pages;
1992
1993 /* Check that the physical memory area passed in looks valid */
1994 if (start + len < start)
1995 return -EINVAL;
1996 /*
1997 * You *really* shouldn't map things that aren't page-aligned,
1998 * but we've historically allowed it because IO memory might
1999 * just have smaller alignment.
2000 */
2001 len += start & ~PAGE_MASK;
2002 pfn = start >> PAGE_SHIFT;
2003 pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
2004 if (pfn + pages < pfn)
2005 return -EINVAL;
2006
2007 /* We start the mapping 'vm_pgoff' pages into the area */
2008 if (vma->vm_pgoff > pages)
2009 return -EINVAL;
2010 pfn += vma->vm_pgoff;
2011 pages -= vma->vm_pgoff;
2012
2013 /* Can we fit all of the mapping? */
2014 vm_len = vma->vm_end - vma->vm_start;
2015 if (vm_len >> PAGE_SHIFT > pages)
2016 return -EINVAL;
2017
2018 /* Ok, let it rip */
2019 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
2020}
2021EXPORT_SYMBOL(vm_iomap_memory);
2022
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002023static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
2024 unsigned long addr, unsigned long end,
2025 pte_fn_t fn, void *data)
2026{
2027 pte_t *pte;
2028 int err;
Borislav Petkov94909912007-05-06 14:49:17 -07002029 spinlock_t *uninitialized_var(ptl);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002030
2031 pte = (mm == &init_mm) ?
2032 pte_alloc_kernel(pmd, addr) :
2033 pte_alloc_map_lock(mm, pmd, addr, &ptl);
2034 if (!pte)
2035 return -ENOMEM;
2036
2037 BUG_ON(pmd_huge(*pmd));
2038
Jeremy Fitzhardinge38e0edb2009-01-06 14:39:21 -08002039 arch_enter_lazy_mmu_mode();
2040
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002041 do {
Anshuman Khandual8b1e0f82019-07-11 20:58:43 -07002042 err = fn(pte++, addr, data);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002043 if (err)
2044 break;
Daisuke Nishimurac36987e2009-10-26 16:50:23 -07002045 } while (addr += PAGE_SIZE, addr != end);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002046
Jeremy Fitzhardinge38e0edb2009-01-06 14:39:21 -08002047 arch_leave_lazy_mmu_mode();
2048
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002049 if (mm != &init_mm)
2050 pte_unmap_unlock(pte-1, ptl);
2051 return err;
2052}
2053
2054static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
2055 unsigned long addr, unsigned long end,
2056 pte_fn_t fn, void *data)
2057{
2058 pmd_t *pmd;
2059 unsigned long next;
2060 int err;
2061
Andi Kleenceb86872008-07-23 21:27:50 -07002062 BUG_ON(pud_huge(*pud));
2063
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002064 pmd = pmd_alloc(mm, pud, addr);
2065 if (!pmd)
2066 return -ENOMEM;
2067 do {
2068 next = pmd_addr_end(addr, end);
2069 err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
2070 if (err)
2071 break;
2072 } while (pmd++, addr = next, addr != end);
2073 return err;
2074}
2075
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002076static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d,
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002077 unsigned long addr, unsigned long end,
2078 pte_fn_t fn, void *data)
2079{
2080 pud_t *pud;
2081 unsigned long next;
2082 int err;
2083
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002084 pud = pud_alloc(mm, p4d, addr);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002085 if (!pud)
2086 return -ENOMEM;
2087 do {
2088 next = pud_addr_end(addr, end);
2089 err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
2090 if (err)
2091 break;
2092 } while (pud++, addr = next, addr != end);
2093 return err;
2094}
2095
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002096static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd,
2097 unsigned long addr, unsigned long end,
2098 pte_fn_t fn, void *data)
2099{
2100 p4d_t *p4d;
2101 unsigned long next;
2102 int err;
2103
2104 p4d = p4d_alloc(mm, pgd, addr);
2105 if (!p4d)
2106 return -ENOMEM;
2107 do {
2108 next = p4d_addr_end(addr, end);
2109 err = apply_to_pud_range(mm, p4d, addr, next, fn, data);
2110 if (err)
2111 break;
2112 } while (p4d++, addr = next, addr != end);
2113 return err;
2114}
2115
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002116/*
2117 * Scan a region of virtual memory, filling in page tables as necessary
2118 * and calling a provided function on each leaf page table.
2119 */
2120int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2121 unsigned long size, pte_fn_t fn, void *data)
2122{
2123 pgd_t *pgd;
2124 unsigned long next;
Jeremy Fitzhardinge57250a52010-08-09 17:19:52 -07002125 unsigned long end = addr + size;
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002126 int err;
2127
Mika Penttilä9cb65bc32016-03-15 14:56:45 -07002128 if (WARN_ON(addr >= end))
2129 return -EINVAL;
2130
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002131 pgd = pgd_offset(mm, addr);
2132 do {
2133 next = pgd_addr_end(addr, end);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002134 err = apply_to_p4d_range(mm, pgd, addr, next, fn, data);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002135 if (err)
2136 break;
2137 } while (pgd++, addr = next, addr != end);
Jeremy Fitzhardinge57250a52010-08-09 17:19:52 -07002138
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002139 return err;
2140}
2141EXPORT_SYMBOL_GPL(apply_to_page_range);
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143/*
Kirill A. Shutemov9b4bdd22015-02-10 14:09:51 -08002144 * handle_pte_fault chooses page fault handler according to an entry which was
2145 * read non-atomically. Before making any commitment, on those architectures
2146 * or configurations (e.g. i386 with PAE) which might give a mix of unmatched
2147 * parts, do_swap_page must check under lock before unmapping the pte and
2148 * proceeding (but do_wp_page is only called after already making such a check;
Ryota Ozakia335b2e2011-02-10 13:56:28 +09002149 * and do_anonymous_page can safely check later on).
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002150 */
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07002151static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002152 pte_t *page_table, pte_t orig_pte)
2153{
2154 int same = 1;
2155#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
2156 if (sizeof(pte_t) > sizeof(unsigned long)) {
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07002157 spinlock_t *ptl = pte_lockptr(mm, pmd);
2158 spin_lock(ptl);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002159 same = pte_same(*page_table, orig_pte);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07002160 spin_unlock(ptl);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002161 }
2162#endif
2163 pte_unmap(page_table);
2164 return same;
2165}
2166
Jia He83d116c2019-10-11 22:09:39 +08002167static inline bool cow_user_page(struct page *dst, struct page *src,
2168 struct vm_fault *vmf)
Linus Torvalds6aab3412005-11-28 14:34:23 -08002169{
Jia He83d116c2019-10-11 22:09:39 +08002170 bool ret;
2171 void *kaddr;
2172 void __user *uaddr;
2173 bool force_mkyoung;
2174 struct vm_area_struct *vma = vmf->vma;
2175 struct mm_struct *mm = vma->vm_mm;
2176 unsigned long addr = vmf->address;
2177
Dan Williams0abdd7a2014-01-21 15:48:12 -08002178 debug_dma_assert_idle(src);
2179
Jia He83d116c2019-10-11 22:09:39 +08002180 if (likely(src)) {
2181 copy_user_highpage(dst, src, addr, vma);
2182 return true;
2183 }
2184
Linus Torvalds6aab3412005-11-28 14:34:23 -08002185 /*
2186 * If the source page was a PFN mapping, we don't have
2187 * a "struct page" for it. We do a best-effort copy by
2188 * just copying from the original user address. If that
2189 * fails, we just zero-fill it. Live with it.
2190 */
Jia He83d116c2019-10-11 22:09:39 +08002191 kaddr = kmap_atomic(dst);
2192 uaddr = (void __user *)(addr & PAGE_MASK);
Linus Torvalds5d2a2dbbc2005-11-29 14:07:55 -08002193
Jia He83d116c2019-10-11 22:09:39 +08002194 /*
2195 * On architectures with software "accessed" bits, we would
2196 * take a double page fault, so mark it accessed here.
2197 */
2198 force_mkyoung = arch_faults_on_old_pte() && !pte_young(vmf->orig_pte);
2199 if (force_mkyoung) {
2200 pte_t entry;
2201
2202 vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl);
2203 if (!likely(pte_same(*vmf->pte, vmf->orig_pte))) {
2204 /*
2205 * Other thread has already handled the fault
2206 * and we don't need to do anything. If it's
2207 * not the case, the fault will be triggered
2208 * again on the same address.
2209 */
2210 ret = false;
2211 goto pte_unlock;
2212 }
2213
2214 entry = pte_mkyoung(vmf->orig_pte);
2215 if (ptep_set_access_flags(vma, addr, vmf->pte, entry, 0))
2216 update_mmu_cache(vma, addr, vmf->pte);
2217 }
2218
2219 /*
2220 * This really shouldn't fail, because the page is there
2221 * in the page tables. But it might just be unreadable,
2222 * in which case we just give up and fill the result with
2223 * zeroes.
2224 */
2225 if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) {
Linus Torvalds5d2a2dbbc2005-11-29 14:07:55 -08002226 /*
Jia He83d116c2019-10-11 22:09:39 +08002227 * Give a warn in case there can be some obscure
2228 * use-case
Linus Torvalds5d2a2dbbc2005-11-29 14:07:55 -08002229 */
Jia He83d116c2019-10-11 22:09:39 +08002230 WARN_ON_ONCE(1);
2231 clear_page(kaddr);
2232 }
2233
2234 ret = true;
2235
2236pte_unlock:
2237 if (force_mkyoung)
2238 pte_unmap_unlock(vmf->pte, vmf->ptl);
2239 kunmap_atomic(kaddr);
2240 flush_dcache_page(dst);
2241
2242 return ret;
Linus Torvalds6aab3412005-11-28 14:34:23 -08002243}
2244
Michal Hockoc20cd452016-01-14 15:20:12 -08002245static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma)
2246{
2247 struct file *vm_file = vma->vm_file;
2248
2249 if (vm_file)
2250 return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO;
2251
2252 /*
2253 * Special mappings (e.g. VDSO) do not have any file so fake
2254 * a default GFP_KERNEL for them.
2255 */
2256 return GFP_KERNEL;
2257}
2258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259/*
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002260 * Notify the address space that the page is about to become writable so that
2261 * it can prohibit this or wait for the page to get into an appropriate state.
2262 *
2263 * We do this without the lock held, so that it can sleep if it needs to.
2264 */
Souptick Joarder2b740302018-08-23 17:01:36 -07002265static vm_fault_t do_page_mkwrite(struct vm_fault *vmf)
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002266{
Souptick Joarder2b740302018-08-23 17:01:36 -07002267 vm_fault_t ret;
Jan Kara38b8cb72016-12-14 15:07:30 -08002268 struct page *page = vmf->page;
2269 unsigned int old_flags = vmf->flags;
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002270
Jan Kara38b8cb72016-12-14 15:07:30 -08002271 vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002272
Darrick J. Wongdc617f22019-08-20 07:55:16 -07002273 if (vmf->vma->vm_file &&
2274 IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host))
2275 return VM_FAULT_SIGBUS;
2276
Dave Jiang11bac802017-02-24 14:56:41 -08002277 ret = vmf->vma->vm_ops->page_mkwrite(vmf);
Jan Kara38b8cb72016-12-14 15:07:30 -08002278 /* Restore original flags so that caller is not surprised */
2279 vmf->flags = old_flags;
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002280 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
2281 return ret;
2282 if (unlikely(!(ret & VM_FAULT_LOCKED))) {
2283 lock_page(page);
2284 if (!page->mapping) {
2285 unlock_page(page);
2286 return 0; /* retry */
2287 }
2288 ret |= VM_FAULT_LOCKED;
2289 } else
2290 VM_BUG_ON_PAGE(!PageLocked(page), page);
2291 return ret;
2292}
2293
2294/*
Jan Kara97ba0c22016-12-14 15:07:27 -08002295 * Handle dirtying of a page in shared file mapping on a write fault.
2296 *
2297 * The function expects the page to be locked and unlocks it.
2298 */
Johannes Weiner89b15332019-11-30 17:50:22 -08002299static vm_fault_t fault_dirty_shared_page(struct vm_fault *vmf)
Jan Kara97ba0c22016-12-14 15:07:27 -08002300{
Johannes Weiner89b15332019-11-30 17:50:22 -08002301 struct vm_area_struct *vma = vmf->vma;
Jan Kara97ba0c22016-12-14 15:07:27 -08002302 struct address_space *mapping;
Johannes Weiner89b15332019-11-30 17:50:22 -08002303 struct page *page = vmf->page;
Jan Kara97ba0c22016-12-14 15:07:27 -08002304 bool dirtied;
2305 bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite;
2306
2307 dirtied = set_page_dirty(page);
2308 VM_BUG_ON_PAGE(PageAnon(page), page);
2309 /*
2310 * Take a local copy of the address_space - page.mapping may be zeroed
2311 * by truncate after unlock_page(). The address_space itself remains
2312 * pinned by vma->vm_file's reference. We rely on unlock_page()'s
2313 * release semantics to prevent the compiler from undoing this copying.
2314 */
2315 mapping = page_rmapping(page);
2316 unlock_page(page);
2317
Jan Kara97ba0c22016-12-14 15:07:27 -08002318 if (!page_mkwrite)
2319 file_update_time(vma->vm_file);
Johannes Weiner89b15332019-11-30 17:50:22 -08002320
2321 /*
2322 * Throttle page dirtying rate down to writeback speed.
2323 *
2324 * mapping may be NULL here because some device drivers do not
2325 * set page.mapping but still dirty their pages
2326 *
2327 * Drop the mmap_sem before waiting on IO, if we can. The file
2328 * is pinning the mapping, as per above.
2329 */
2330 if ((dirtied || page_mkwrite) && mapping) {
2331 struct file *fpin;
2332
2333 fpin = maybe_unlock_mmap_for_io(vmf, NULL);
2334 balance_dirty_pages_ratelimited(mapping);
2335 if (fpin) {
2336 fput(fpin);
2337 return VM_FAULT_RETRY;
2338 }
2339 }
2340
2341 return 0;
Jan Kara97ba0c22016-12-14 15:07:27 -08002342}
2343
2344/*
Shachar Raindel4e047f82015-04-14 15:46:25 -07002345 * Handle write page faults for pages that can be reused in the current vma
2346 *
2347 * This can happen either due to the mapping being with the VM_SHARED flag,
2348 * or due to us being the last reference standing to the page. In either
2349 * case, all we need to do here is to mark the page as writable and update
2350 * any related book-keeping.
2351 */
Jan Kara997dd982016-12-14 15:07:36 -08002352static inline void wp_page_reuse(struct vm_fault *vmf)
Jan Kara82b0f8c2016-12-14 15:06:58 -08002353 __releases(vmf->ptl)
Shachar Raindel4e047f82015-04-14 15:46:25 -07002354{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002355 struct vm_area_struct *vma = vmf->vma;
Jan Karaa41b70d2016-12-14 15:07:33 -08002356 struct page *page = vmf->page;
Shachar Raindel4e047f82015-04-14 15:46:25 -07002357 pte_t entry;
2358 /*
2359 * Clear the pages cpupid information as the existing
2360 * information potentially belongs to a now completely
2361 * unrelated process.
2362 */
2363 if (page)
2364 page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1);
2365
Jan Kara29943022016-12-14 15:07:16 -08002366 flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
2367 entry = pte_mkyoung(vmf->orig_pte);
Shachar Raindel4e047f82015-04-14 15:46:25 -07002368 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Jan Kara82b0f8c2016-12-14 15:06:58 -08002369 if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1))
2370 update_mmu_cache(vma, vmf->address, vmf->pte);
2371 pte_unmap_unlock(vmf->pte, vmf->ptl);
Shachar Raindel4e047f82015-04-14 15:46:25 -07002372}
2373
2374/*
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002375 * Handle the case of a page which we actually need to copy to a new page.
2376 *
2377 * Called with mmap_sem locked and the old page referenced, but
2378 * without the ptl held.
2379 *
2380 * High level logic flow:
2381 *
2382 * - Allocate a page, copy the content of the old page to the new one.
2383 * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc.
2384 * - Take the PTL. If the pte changed, bail out and release the allocated page
2385 * - If the pte is still the way we remember it, update the page table and all
2386 * relevant references. This includes dropping the reference the page-table
2387 * held to the old page, as well as updating the rmap.
2388 * - In any case, unlock the PTL and drop the reference we took to the old page.
2389 */
Souptick Joarder2b740302018-08-23 17:01:36 -07002390static vm_fault_t wp_page_copy(struct vm_fault *vmf)
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002391{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002392 struct vm_area_struct *vma = vmf->vma;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002393 struct mm_struct *mm = vma->vm_mm;
Jan Karaa41b70d2016-12-14 15:07:33 -08002394 struct page *old_page = vmf->page;
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002395 struct page *new_page = NULL;
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002396 pte_t entry;
2397 int page_copied = 0;
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002398 struct mem_cgroup *memcg;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002399 struct mmu_notifier_range range;
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002400
2401 if (unlikely(anon_vma_prepare(vma)))
2402 goto oom;
2403
Jan Kara29943022016-12-14 15:07:16 -08002404 if (is_zero_pfn(pte_pfn(vmf->orig_pte))) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08002405 new_page = alloc_zeroed_user_highpage_movable(vma,
2406 vmf->address);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002407 if (!new_page)
2408 goto oom;
2409 } else {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002410 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
Jan Kara82b0f8c2016-12-14 15:06:58 -08002411 vmf->address);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002412 if (!new_page)
2413 goto oom;
Jia He83d116c2019-10-11 22:09:39 +08002414
2415 if (!cow_user_page(new_page, old_page, vmf)) {
2416 /*
2417 * COW failed, if the fault was solved by other,
2418 * it's fine. If not, userspace would re-fault on
2419 * the same address and we will handle the fault
2420 * from the second attempt.
2421 */
2422 put_page(new_page);
2423 if (old_page)
2424 put_page(old_page);
2425 return 0;
2426 }
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002427 }
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002428
Tejun Heo2cf85582018-07-03 11:14:56 -04002429 if (mem_cgroup_try_charge_delay(new_page, mm, GFP_KERNEL, &memcg, false))
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002430 goto oom_free_new;
2431
Mel Gormaneb3c24f2015-06-24 16:57:27 -07002432 __SetPageUptodate(new_page);
2433
Jérôme Glisse7269f992019-05-13 17:20:53 -07002434 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07002435 vmf->address & PAGE_MASK,
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002436 (vmf->address & PAGE_MASK) + PAGE_SIZE);
2437 mmu_notifier_invalidate_range_start(&range);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002438
2439 /*
2440 * Re-check the pte - we dropped the lock
2441 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002442 vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08002443 if (likely(pte_same(*vmf->pte, vmf->orig_pte))) {
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002444 if (old_page) {
2445 if (!PageAnon(old_page)) {
Jerome Marchandeca56ff2016-01-14 15:19:26 -08002446 dec_mm_counter_fast(mm,
2447 mm_counter_file(old_page));
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002448 inc_mm_counter_fast(mm, MM_ANONPAGES);
2449 }
2450 } else {
2451 inc_mm_counter_fast(mm, MM_ANONPAGES);
2452 }
Jan Kara29943022016-12-14 15:07:16 -08002453 flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002454 entry = mk_pte(new_page, vma->vm_page_prot);
2455 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2456 /*
2457 * Clear the pte entry and flush it first, before updating the
2458 * pte with the new entry. This will avoid a race condition
2459 * seen in the presence of one thread doing SMC and another
2460 * thread doing COW.
2461 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002462 ptep_clear_flush_notify(vma, vmf->address, vmf->pte);
2463 page_add_new_anon_rmap(new_page, vma, vmf->address, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002464 mem_cgroup_commit_charge(new_page, memcg, false, false);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002465 lru_cache_add_active_or_unevictable(new_page, vma);
2466 /*
2467 * We call the notify macro here because, when using secondary
2468 * mmu page tables (such as kvm shadow page tables), we want the
2469 * new page to be mapped directly into the secondary page table.
2470 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002471 set_pte_at_notify(mm, vmf->address, vmf->pte, entry);
2472 update_mmu_cache(vma, vmf->address, vmf->pte);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002473 if (old_page) {
2474 /*
2475 * Only after switching the pte to the new page may
2476 * we remove the mapcount here. Otherwise another
2477 * process may come and find the rmap count decremented
2478 * before the pte is switched to the new page, and
2479 * "reuse" the old page writing into it while our pte
2480 * here still points into it and can be read by other
2481 * threads.
2482 *
2483 * The critical issue is to order this
2484 * page_remove_rmap with the ptp_clear_flush above.
2485 * Those stores are ordered by (if nothing else,)
2486 * the barrier present in the atomic_add_negative
2487 * in page_remove_rmap.
2488 *
2489 * Then the TLB flush in ptep_clear_flush ensures that
2490 * no process can access the old page before the
2491 * decremented mapcount is visible. And the old page
2492 * cannot be reused until after the decremented
2493 * mapcount is visible. So transitively, TLBs to
2494 * old page will be flushed before it can be reused.
2495 */
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002496 page_remove_rmap(old_page, false);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002497 }
2498
2499 /* Free the old page.. */
2500 new_page = old_page;
2501 page_copied = 1;
2502 } else {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002503 mem_cgroup_cancel_charge(new_page, memcg, false);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002504 }
2505
2506 if (new_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002507 put_page(new_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002508
Jan Kara82b0f8c2016-12-14 15:06:58 -08002509 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jérôme Glisse4645b9f2017-11-15 17:34:11 -08002510 /*
2511 * No need to double call mmu_notifier->invalidate_range() callback as
2512 * the above ptep_clear_flush_notify() did already call it.
2513 */
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002514 mmu_notifier_invalidate_range_only_end(&range);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002515 if (old_page) {
2516 /*
2517 * Don't let another task, with possibly unlocked vma,
2518 * keep the mlocked page.
2519 */
2520 if (page_copied && (vma->vm_flags & VM_LOCKED)) {
2521 lock_page(old_page); /* LRU manipulation */
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002522 if (PageMlocked(old_page))
2523 munlock_vma_page(old_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002524 unlock_page(old_page);
2525 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002526 put_page(old_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002527 }
2528 return page_copied ? VM_FAULT_WRITE : 0;
2529oom_free_new:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002530 put_page(new_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002531oom:
2532 if (old_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002533 put_page(old_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002534 return VM_FAULT_OOM;
2535}
2536
Jan Kara66a61972016-12-14 15:07:39 -08002537/**
2538 * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE
2539 * writeable once the page is prepared
2540 *
2541 * @vmf: structure describing the fault
2542 *
2543 * This function handles all that is needed to finish a write page fault in a
2544 * shared mapping due to PTE being read-only once the mapped page is prepared.
Mike Rapoporta862f682019-03-05 15:48:42 -08002545 * It handles locking of PTE and modifying it.
Jan Kara66a61972016-12-14 15:07:39 -08002546 *
2547 * The function expects the page to be locked or other protection against
2548 * concurrent faults / writeback (such as DAX radix tree locks).
Mike Rapoporta862f682019-03-05 15:48:42 -08002549 *
2550 * Return: %VM_FAULT_WRITE on success, %0 when PTE got changed before
2551 * we acquired PTE lock.
Jan Kara66a61972016-12-14 15:07:39 -08002552 */
Souptick Joarder2b740302018-08-23 17:01:36 -07002553vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf)
Jan Kara66a61972016-12-14 15:07:39 -08002554{
2555 WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED));
2556 vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address,
2557 &vmf->ptl);
2558 /*
2559 * We might have raced with another page fault while we released the
2560 * pte_offset_map_lock.
2561 */
2562 if (!pte_same(*vmf->pte, vmf->orig_pte)) {
2563 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karaa19e2552016-12-14 15:07:42 -08002564 return VM_FAULT_NOPAGE;
Jan Kara66a61972016-12-14 15:07:39 -08002565 }
2566 wp_page_reuse(vmf);
Jan Karaa19e2552016-12-14 15:07:42 -08002567 return 0;
Jan Kara66a61972016-12-14 15:07:39 -08002568}
2569
Boaz Harroshdd906182015-04-15 16:15:11 -07002570/*
2571 * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED
2572 * mapping
2573 */
Souptick Joarder2b740302018-08-23 17:01:36 -07002574static vm_fault_t wp_pfn_shared(struct vm_fault *vmf)
Boaz Harroshdd906182015-04-15 16:15:11 -07002575{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002576 struct vm_area_struct *vma = vmf->vma;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002577
Boaz Harroshdd906182015-04-15 16:15:11 -07002578 if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) {
Souptick Joarder2b740302018-08-23 17:01:36 -07002579 vm_fault_t ret;
Boaz Harroshdd906182015-04-15 16:15:11 -07002580
Jan Kara82b0f8c2016-12-14 15:06:58 -08002581 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karafe822212016-12-14 15:07:13 -08002582 vmf->flags |= FAULT_FLAG_MKWRITE;
Dave Jiang11bac802017-02-24 14:56:41 -08002583 ret = vma->vm_ops->pfn_mkwrite(vmf);
Jan Kara2f89dc12016-12-14 15:07:50 -08002584 if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))
Boaz Harroshdd906182015-04-15 16:15:11 -07002585 return ret;
Jan Kara66a61972016-12-14 15:07:39 -08002586 return finish_mkwrite_fault(vmf);
Boaz Harroshdd906182015-04-15 16:15:11 -07002587 }
Jan Kara997dd982016-12-14 15:07:36 -08002588 wp_page_reuse(vmf);
2589 return VM_FAULT_WRITE;
Boaz Harroshdd906182015-04-15 16:15:11 -07002590}
2591
Souptick Joarder2b740302018-08-23 17:01:36 -07002592static vm_fault_t wp_page_shared(struct vm_fault *vmf)
Jan Kara82b0f8c2016-12-14 15:06:58 -08002593 __releases(vmf->ptl)
Shachar Raindel93e478d2015-04-14 15:46:35 -07002594{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002595 struct vm_area_struct *vma = vmf->vma;
Johannes Weiner89b15332019-11-30 17:50:22 -08002596 vm_fault_t ret = VM_FAULT_WRITE;
Shachar Raindel93e478d2015-04-14 15:46:35 -07002597
Jan Karaa41b70d2016-12-14 15:07:33 -08002598 get_page(vmf->page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07002599
Shachar Raindel93e478d2015-04-14 15:46:35 -07002600 if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
Souptick Joarder2b740302018-08-23 17:01:36 -07002601 vm_fault_t tmp;
Shachar Raindel93e478d2015-04-14 15:46:35 -07002602
Jan Kara82b0f8c2016-12-14 15:06:58 -08002603 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Kara38b8cb72016-12-14 15:07:30 -08002604 tmp = do_page_mkwrite(vmf);
Shachar Raindel93e478d2015-04-14 15:46:35 -07002605 if (unlikely(!tmp || (tmp &
2606 (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
Jan Karaa41b70d2016-12-14 15:07:33 -08002607 put_page(vmf->page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07002608 return tmp;
2609 }
Jan Kara66a61972016-12-14 15:07:39 -08002610 tmp = finish_mkwrite_fault(vmf);
Jan Karaa19e2552016-12-14 15:07:42 -08002611 if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
Jan Karaa41b70d2016-12-14 15:07:33 -08002612 unlock_page(vmf->page);
Jan Karaa41b70d2016-12-14 15:07:33 -08002613 put_page(vmf->page);
Jan Kara66a61972016-12-14 15:07:39 -08002614 return tmp;
Shachar Raindel93e478d2015-04-14 15:46:35 -07002615 }
Jan Kara66a61972016-12-14 15:07:39 -08002616 } else {
2617 wp_page_reuse(vmf);
Jan Kara997dd982016-12-14 15:07:36 -08002618 lock_page(vmf->page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07002619 }
Johannes Weiner89b15332019-11-30 17:50:22 -08002620 ret |= fault_dirty_shared_page(vmf);
Jan Kara997dd982016-12-14 15:07:36 -08002621 put_page(vmf->page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07002622
Johannes Weiner89b15332019-11-30 17:50:22 -08002623 return ret;
Shachar Raindel93e478d2015-04-14 15:46:35 -07002624}
2625
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002626/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 * This routine handles present pages, when users try to write
2628 * to a shared page. It is done by copying the page to a new address
2629 * and decrementing the shared-page counter for the old page.
2630 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 * Note that this routine assumes that the protection checks have been
2632 * done by the caller (the low-level page fault routine in most cases).
2633 * Thus we can safely just mark it writable once we've done any necessary
2634 * COW.
2635 *
2636 * We also mark the page dirty at this point even though the page will
2637 * change only once the write actually happens. This avoids a few races,
2638 * and potentially makes it more efficient.
2639 *
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002640 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2641 * but allow concurrent faults), with pte both mapped and locked.
2642 * We return with mmap_sem still held, but pte unmapped and unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 */
Souptick Joarder2b740302018-08-23 17:01:36 -07002644static vm_fault_t do_wp_page(struct vm_fault *vmf)
Jan Kara82b0f8c2016-12-14 15:06:58 -08002645 __releases(vmf->ptl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002647 struct vm_area_struct *vma = vmf->vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Jan Karaa41b70d2016-12-14 15:07:33 -08002649 vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte);
2650 if (!vmf->page) {
Peter Zijlstra251b97f2008-07-04 09:59:24 -07002651 /*
Peter Feiner64e455072014-10-13 15:55:46 -07002652 * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
2653 * VM_PFNMAP VMA.
Peter Zijlstra251b97f2008-07-04 09:59:24 -07002654 *
2655 * We should not cow pages in a shared writeable mapping.
Boaz Harroshdd906182015-04-15 16:15:11 -07002656 * Just mark the pages writable and/or call ops->pfn_mkwrite.
Peter Zijlstra251b97f2008-07-04 09:59:24 -07002657 */
2658 if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
2659 (VM_WRITE|VM_SHARED))
Jan Kara29943022016-12-14 15:07:16 -08002660 return wp_pfn_shared(vmf);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002661
Jan Kara82b0f8c2016-12-14 15:06:58 -08002662 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karaa41b70d2016-12-14 15:07:33 -08002663 return wp_page_copy(vmf);
Peter Zijlstra251b97f2008-07-04 09:59:24 -07002664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Peter Zijlstrad08b3852006-09-25 23:30:57 -07002666 /*
Peter Zijlstraee6a6452006-09-25 23:31:00 -07002667 * Take out anonymous pages first, anonymous shared vmas are
2668 * not dirty accountable.
Peter Zijlstrad08b3852006-09-25 23:30:57 -07002669 */
Kirill Tkhai52d1e602019-03-05 15:43:06 -08002670 if (PageAnon(vmf->page)) {
Huang Yingba3c4ce2017-09-06 16:22:19 -07002671 int total_map_swapcount;
Kirill Tkhai52d1e602019-03-05 15:43:06 -08002672 if (PageKsm(vmf->page) && (PageSwapCache(vmf->page) ||
2673 page_count(vmf->page) != 1))
2674 goto copy;
Jan Karaa41b70d2016-12-14 15:07:33 -08002675 if (!trylock_page(vmf->page)) {
2676 get_page(vmf->page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08002677 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karaa41b70d2016-12-14 15:07:33 -08002678 lock_page(vmf->page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08002679 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
2680 vmf->address, &vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08002681 if (!pte_same(*vmf->pte, vmf->orig_pte)) {
Jan Karaa41b70d2016-12-14 15:07:33 -08002682 unlock_page(vmf->page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08002683 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karaa41b70d2016-12-14 15:07:33 -08002684 put_page(vmf->page);
Shachar Raindel28766802015-04-14 15:46:29 -07002685 return 0;
Hugh Dickinsab967d82009-01-06 14:39:33 -08002686 }
Jan Karaa41b70d2016-12-14 15:07:33 -08002687 put_page(vmf->page);
Peter Zijlstraee6a6452006-09-25 23:31:00 -07002688 }
Kirill Tkhai52d1e602019-03-05 15:43:06 -08002689 if (PageKsm(vmf->page)) {
2690 bool reused = reuse_ksm_page(vmf->page, vmf->vma,
2691 vmf->address);
2692 unlock_page(vmf->page);
2693 if (!reused)
2694 goto copy;
2695 wp_page_reuse(vmf);
2696 return VM_FAULT_WRITE;
2697 }
Huang Yingba3c4ce2017-09-06 16:22:19 -07002698 if (reuse_swap_page(vmf->page, &total_map_swapcount)) {
2699 if (total_map_swapcount == 1) {
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002700 /*
2701 * The page is all ours. Move it to
2702 * our anon_vma so the rmap code will
2703 * not search our parent or siblings.
2704 * Protected against the rmap code by
2705 * the page lock.
2706 */
Jan Karaa41b70d2016-12-14 15:07:33 -08002707 page_move_anon_rmap(vmf->page, vma);
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002708 }
Jan Karaa41b70d2016-12-14 15:07:33 -08002709 unlock_page(vmf->page);
Jan Kara997dd982016-12-14 15:07:36 -08002710 wp_page_reuse(vmf);
2711 return VM_FAULT_WRITE;
Michel Lespinasseb009c022011-01-13 15:46:07 -08002712 }
Jan Karaa41b70d2016-12-14 15:07:33 -08002713 unlock_page(vmf->page);
Peter Zijlstraee6a6452006-09-25 23:31:00 -07002714 } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
Peter Zijlstrad08b3852006-09-25 23:30:57 -07002715 (VM_WRITE|VM_SHARED))) {
Jan Karaa41b70d2016-12-14 15:07:33 -08002716 return wp_page_shared(vmf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 }
Kirill Tkhai52d1e602019-03-05 15:43:06 -08002718copy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 /*
2720 * Ok, we need to copy. Oh, well..
2721 */
Jan Karaa41b70d2016-12-14 15:07:33 -08002722 get_page(vmf->page);
Shachar Raindel28766802015-04-14 15:46:29 -07002723
Jan Kara82b0f8c2016-12-14 15:06:58 -08002724 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karaa41b70d2016-12-14 15:07:33 -08002725 return wp_page_copy(vmf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726}
2727
Peter Zijlstra97a89412011-05-24 17:12:04 -07002728static void unmap_mapping_range_vma(struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 unsigned long start_addr, unsigned long end_addr,
2730 struct zap_details *details)
2731{
Al Virof5cc4ee2012-03-05 14:14:20 -05002732 zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733}
2734
Davidlohr Buesof808c132017-09-08 16:15:08 -07002735static inline void unmap_mapping_range_tree(struct rb_root_cached *root,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 struct zap_details *details)
2737{
2738 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 pgoff_t vba, vea, zba, zea;
2740
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002741 vma_interval_tree_foreach(vma, root,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 details->first_index, details->last_index) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
2744 vba = vma->vm_pgoff;
Libind6e93212013-07-03 15:01:26 -07002745 vea = vba + vma_pages(vma) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 zba = details->first_index;
2747 if (zba < vba)
2748 zba = vba;
2749 zea = details->last_index;
2750 if (zea > vea)
2751 zea = vea;
2752
Peter Zijlstra97a89412011-05-24 17:12:04 -07002753 unmap_mapping_range_vma(vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
2755 ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
Peter Zijlstra97a89412011-05-24 17:12:04 -07002756 details);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 }
2758}
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760/**
Matthew Wilcox977fbdc2018-01-31 16:17:36 -08002761 * unmap_mapping_pages() - Unmap pages from processes.
2762 * @mapping: The address space containing pages to be unmapped.
2763 * @start: Index of first page to be unmapped.
2764 * @nr: Number of pages to be unmapped. 0 to unmap to end of file.
2765 * @even_cows: Whether to unmap even private COWed pages.
2766 *
2767 * Unmap the pages in this address space from any userspace process which
2768 * has them mmaped. Generally, you want to remove COWed pages as well when
2769 * a file is being truncated, but not when invalidating pages from the page
2770 * cache.
2771 */
2772void unmap_mapping_pages(struct address_space *mapping, pgoff_t start,
2773 pgoff_t nr, bool even_cows)
2774{
2775 struct zap_details details = { };
2776
2777 details.check_mapping = even_cows ? NULL : mapping;
2778 details.first_index = start;
2779 details.last_index = start + nr - 1;
2780 if (details.last_index < details.first_index)
2781 details.last_index = ULONG_MAX;
2782
2783 i_mmap_lock_write(mapping);
2784 if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)))
2785 unmap_mapping_range_tree(&mapping->i_mmap, &details);
2786 i_mmap_unlock_write(mapping);
2787}
2788
2789/**
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08002790 * unmap_mapping_range - unmap the portion of all mmaps in the specified
Matthew Wilcox977fbdc2018-01-31 16:17:36 -08002791 * address_space corresponding to the specified byte range in the underlying
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08002792 * file.
2793 *
Martin Waitz3d410882005-06-23 22:05:21 -07002794 * @mapping: the address space containing mmaps to be unmapped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 * @holebegin: byte in first page to unmap, relative to the start of
2796 * the underlying file. This will be rounded down to a PAGE_SIZE
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +10002797 * boundary. Note that this is different from truncate_pagecache(), which
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 * must keep the partial page. In contrast, we must get rid of
2799 * partial pages.
2800 * @holelen: size of prospective hole in bytes. This will be rounded
2801 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
2802 * end of the file.
2803 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
2804 * but 0 when invalidating pagecache, don't throw away private data.
2805 */
2806void unmap_mapping_range(struct address_space *mapping,
2807 loff_t const holebegin, loff_t const holelen, int even_cows)
2808{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 pgoff_t hba = holebegin >> PAGE_SHIFT;
2810 pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
2811
2812 /* Check for overflow. */
2813 if (sizeof(holelen) > sizeof(hlen)) {
2814 long long holeend =
2815 (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
2816 if (holeend & ~(long long)ULONG_MAX)
2817 hlen = ULONG_MAX - hba + 1;
2818 }
2819
Matthew Wilcox977fbdc2018-01-31 16:17:36 -08002820 unmap_mapping_pages(mapping, hba, hlen, even_cows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821}
2822EXPORT_SYMBOL(unmap_mapping_range);
2823
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824/*
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002825 * We enter with non-exclusive mmap_sem (to exclude vma changes,
2826 * but allow concurrent faults), and pte mapped but not yet locked.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002827 * We return with pte unmapped and unlocked.
2828 *
2829 * We return with the mmap_sem locked or unlocked in the same cases
2830 * as does filemap_fault().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 */
Souptick Joarder2b740302018-08-23 17:01:36 -07002832vm_fault_t do_swap_page(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002834 struct vm_area_struct *vma = vmf->vma;
Minchan Kimeaf649eb2018-04-05 16:23:39 -07002835 struct page *page = NULL, *swapcache;
Johannes Weiner00501b52014-08-08 14:19:20 -07002836 struct mem_cgroup *memcg;
Hugh Dickins65500d22005-10-29 18:15:59 -07002837 swp_entry_t entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 pte_t pte;
Michel Lespinassed065bd82010-10-26 14:21:57 -07002839 int locked;
Rik van Rielad8c2ee2010-08-09 17:19:48 -07002840 int exclusive = 0;
Souptick Joarder2b740302018-08-23 17:01:36 -07002841 vm_fault_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Minchan Kimeaf649eb2018-04-05 16:23:39 -07002843 if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte))
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002844 goto out;
Hugh Dickins65500d22005-10-29 18:15:59 -07002845
Jan Kara29943022016-12-14 15:07:16 -08002846 entry = pte_to_swp_entry(vmf->orig_pte);
Andi Kleend1737fd2009-09-16 11:50:06 +02002847 if (unlikely(non_swap_entry(entry))) {
2848 if (is_migration_entry(entry)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08002849 migration_entry_wait(vma->vm_mm, vmf->pmd,
2850 vmf->address);
Jérôme Glisse5042db42017-09-08 16:11:43 -07002851 } else if (is_device_private_entry(entry)) {
Christoph Hellwig897e6362019-06-26 14:27:11 +02002852 vmf->page = device_private_entry_to_page(entry);
2853 ret = vmf->page->pgmap->ops->migrate_to_ram(vmf);
Andi Kleend1737fd2009-09-16 11:50:06 +02002854 } else if (is_hwpoison_entry(entry)) {
2855 ret = VM_FAULT_HWPOISON;
2856 } else {
Jan Kara29943022016-12-14 15:07:16 -08002857 print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL);
Hugh Dickinsd99be1a2009-12-14 17:59:04 -08002858 ret = VM_FAULT_SIGBUS;
Andi Kleend1737fd2009-09-16 11:50:06 +02002859 }
Christoph Lameter06972122006-06-23 02:03:35 -07002860 goto out;
2861 }
Minchan Kim0bcac062017-11-15 17:33:07 -08002862
2863
Shailabh Nagar0ff92242006-07-14 00:24:37 -07002864 delayacct_set_flag(DELAYACCT_PF_SWAPIN);
Minchan Kimeaf649eb2018-04-05 16:23:39 -07002865 page = lookup_swap_cache(entry, vma, vmf->address);
2866 swapcache = page;
Minchan Kimf8020772018-01-18 16:33:50 -08002867
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 if (!page) {
Minchan Kim0bcac062017-11-15 17:33:07 -08002869 struct swap_info_struct *si = swp_swap_info(entry);
2870
Minchan Kimaa8d22a2017-11-15 17:33:11 -08002871 if (si->flags & SWP_SYNCHRONOUS_IO &&
Huang Yingeb085572019-07-11 20:55:33 -07002872 __swap_count(entry) == 1) {
Minchan Kim0bcac062017-11-15 17:33:07 -08002873 /* skip swapcache */
Minchan Kime9e9b7e2018-04-05 16:23:42 -07002874 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
2875 vmf->address);
Minchan Kim0bcac062017-11-15 17:33:07 -08002876 if (page) {
2877 __SetPageLocked(page);
2878 __SetPageSwapBacked(page);
2879 set_page_private(page, entry.val);
2880 lru_cache_add_anon(page);
2881 swap_readpage(page, true);
2882 }
Minchan Kimaa8d22a2017-11-15 17:33:11 -08002883 } else {
Minchan Kime9e9b7e2018-04-05 16:23:42 -07002884 page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
2885 vmf);
Minchan Kimaa8d22a2017-11-15 17:33:11 -08002886 swapcache = page;
Minchan Kim0bcac062017-11-15 17:33:07 -08002887 }
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (!page) {
2890 /*
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002891 * Back out if somebody else faulted in this pte
2892 * while we released the pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002894 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
2895 vmf->address, &vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08002896 if (likely(pte_same(*vmf->pte, vmf->orig_pte)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 ret = VM_FAULT_OOM;
Shailabh Nagar0ff92242006-07-14 00:24:37 -07002898 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
Hugh Dickins65500d22005-10-29 18:15:59 -07002899 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 }
2901
2902 /* Had to read the page from swap area: Major fault */
2903 ret = VM_FAULT_MAJOR;
Christoph Lameterf8891e52006-06-30 01:55:45 -07002904 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07002905 count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
Andi Kleend1737fd2009-09-16 11:50:06 +02002906 } else if (PageHWPoison(page)) {
Wu Fengguang71f72522009-12-16 12:19:58 +01002907 /*
2908 * hwpoisoned dirty swapcache pages are kept for killing
2909 * owner processes (which may be unknown at hwpoison time)
2910 */
Andi Kleend1737fd2009-09-16 11:50:06 +02002911 ret = VM_FAULT_HWPOISON;
2912 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
Andi Kleen4779cb32009-10-14 01:51:41 +02002913 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 }
2915
Jan Kara82b0f8c2016-12-14 15:06:58 -08002916 locked = lock_page_or_retry(page, vma->vm_mm, vmf->flags);
Rik van Riele709ffd2012-05-29 15:06:18 -07002917
Balbir Singh20a10222007-11-14 17:00:33 -08002918 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
Michel Lespinassed065bd82010-10-26 14:21:57 -07002919 if (!locked) {
2920 ret |= VM_FAULT_RETRY;
2921 goto out_release;
2922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Andrea Arcangeli4969c112010-09-09 16:37:52 -07002924 /*
Hugh Dickins31c4a3d2010-09-19 19:40:22 -07002925 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
2926 * release the swapcache from under us. The page pin, and pte_same
2927 * test below, are not enough to exclude that. Even if it is still
2928 * swapcache, we need to check that the page's swap has not changed.
Andrea Arcangeli4969c112010-09-09 16:37:52 -07002929 */
Minchan Kim0bcac062017-11-15 17:33:07 -08002930 if (unlikely((!PageSwapCache(page) ||
2931 page_private(page) != entry.val)) && swapcache)
Andrea Arcangeli4969c112010-09-09 16:37:52 -07002932 goto out_page;
2933
Jan Kara82b0f8c2016-12-14 15:06:58 -08002934 page = ksm_might_need_to_copy(page, vma, vmf->address);
Hugh Dickinscbf86cf2013-02-22 16:35:08 -08002935 if (unlikely(!page)) {
2936 ret = VM_FAULT_OOM;
2937 page = swapcache;
Hugh Dickinscbf86cf2013-02-22 16:35:08 -08002938 goto out_page;
Hugh Dickins5ad64682009-12-14 17:59:24 -08002939 }
2940
Tejun Heo2cf85582018-07-03 11:14:56 -04002941 if (mem_cgroup_try_charge_delay(page, vma->vm_mm, GFP_KERNEL,
2942 &memcg, false)) {
KAMEZAWA Hiroyuki073e5872008-10-18 20:28:08 -07002943 ret = VM_FAULT_OOM;
Johannes Weinerbc43f752009-04-30 15:08:08 -07002944 goto out_page;
KAMEZAWA Hiroyuki073e5872008-10-18 20:28:08 -07002945 }
2946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 /*
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002948 * Back out if somebody else already faulted in this pte.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002950 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
2951 &vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08002952 if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte)))
Kirill Korotaevb8107482005-05-16 21:53:50 -07002953 goto out_nomap;
Kirill Korotaevb8107482005-05-16 21:53:50 -07002954
2955 if (unlikely(!PageUptodate(page))) {
2956 ret = VM_FAULT_SIGBUS;
2957 goto out_nomap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 }
2959
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002960 /*
2961 * The page isn't present yet, go ahead with the fault.
2962 *
2963 * Be careful about the sequence of operations here.
2964 * To get its accounting right, reuse_swap_page() must be called
2965 * while the page is counted on swap but not yet in mapcount i.e.
2966 * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
2967 * must be called after the swap_free(), or it will never succeed.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002968 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002970 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
2971 dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 pte = mk_pte(page, vma->vm_page_prot);
Jan Kara82b0f8c2016-12-14 15:06:58 -08002973 if ((vmf->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
Jan Kara82b0f8c2016-12-14 15:06:58 -08002975 vmf->flags &= ~FAULT_FLAG_WRITE;
Andrea Arcangeli9a5b4892010-08-09 17:19:49 -07002976 ret |= VM_FAULT_WRITE;
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002977 exclusive = RMAP_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 flush_icache_page(vma, page);
Jan Kara29943022016-12-14 15:07:16 -08002980 if (pte_swp_soft_dirty(vmf->orig_pte))
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07002981 pte = pte_mksoft_dirty(pte);
Jan Kara82b0f8c2016-12-14 15:06:58 -08002982 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte);
Khalid Azizca827d52018-02-21 10:15:44 -07002983 arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte);
Jan Kara29943022016-12-14 15:07:16 -08002984 vmf->orig_pte = pte;
Minchan Kim0bcac062017-11-15 17:33:07 -08002985
2986 /* ksm created a completely new copy */
2987 if (unlikely(page != swapcache && swapcache)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08002988 page_add_new_anon_rmap(page, vma, vmf->address, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002989 mem_cgroup_commit_charge(page, memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07002990 lru_cache_add_active_or_unevictable(page, vma);
Minchan Kim0bcac062017-11-15 17:33:07 -08002991 } else {
2992 do_page_add_anon_rmap(page, vma, vmf->address, exclusive);
2993 mem_cgroup_commit_charge(page, memcg, true, false);
2994 activate_page(page);
Johannes Weiner00501b52014-08-08 14:19:20 -07002995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07002997 swap_free(entry);
Vladimir Davydov5ccc5ab2016-01-20 15:03:10 -08002998 if (mem_cgroup_swap_full(page) ||
2999 (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -08003000 try_to_free_swap(page);
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07003001 unlock_page(page);
Minchan Kim0bcac062017-11-15 17:33:07 -08003002 if (page != swapcache && swapcache) {
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003003 /*
3004 * Hold the lock to avoid the swap entry to be reused
3005 * until we take the PT lock for the pte_same() check
3006 * (to avoid false positives from pte_same). For
3007 * further safety release the lock after the swap_free
3008 * so that the swap count won't change under a
3009 * parallel locked swapcache.
3010 */
3011 unlock_page(swapcache);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003012 put_page(swapcache);
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003013 }
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07003014
Jan Kara82b0f8c2016-12-14 15:06:58 -08003015 if (vmf->flags & FAULT_FLAG_WRITE) {
Jan Kara29943022016-12-14 15:07:16 -08003016 ret |= do_wp_page(vmf);
Hugh Dickins61469f12008-03-04 14:29:04 -08003017 if (ret & VM_FAULT_ERROR)
3018 ret &= VM_FAULT_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 goto out;
3020 }
3021
3022 /* No need to invalidate - it was non-present before */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003023 update_mmu_cache(vma, vmf->address, vmf->pte);
Hugh Dickins65500d22005-10-29 18:15:59 -07003024unlock:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003025 pte_unmap_unlock(vmf->pte, vmf->ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026out:
3027 return ret;
Kirill Korotaevb8107482005-05-16 21:53:50 -07003028out_nomap:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08003029 mem_cgroup_cancel_charge(page, memcg, false);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003030 pte_unmap_unlock(vmf->pte, vmf->ptl);
Johannes Weinerbc43f752009-04-30 15:08:08 -07003031out_page:
Kirill Korotaevb8107482005-05-16 21:53:50 -07003032 unlock_page(page);
Andi Kleen4779cb32009-10-14 01:51:41 +02003033out_release:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003034 put_page(page);
Minchan Kim0bcac062017-11-15 17:33:07 -08003035 if (page != swapcache && swapcache) {
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003036 unlock_page(swapcache);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003037 put_page(swapcache);
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003038 }
Hugh Dickins65500d22005-10-29 18:15:59 -07003039 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
3042/*
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003043 * We enter with non-exclusive mmap_sem (to exclude vma changes,
3044 * but allow concurrent faults), and pte mapped but not yet locked.
3045 * We return with mmap_sem still held, but pte unmapped and unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003047static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003049 struct vm_area_struct *vma = vmf->vma;
Johannes Weiner00501b52014-08-08 14:19:20 -07003050 struct mem_cgroup *memcg;
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003051 struct page *page;
Souptick Joarder2b740302018-08-23 17:01:36 -07003052 vm_fault_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 pte_t entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Kirill A. Shutemov6b7339f2015-07-06 23:18:37 +03003055 /* File mapping without ->vm_ops ? */
3056 if (vma->vm_flags & VM_SHARED)
3057 return VM_FAULT_SIGBUS;
3058
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003059 /*
3060 * Use pte_alloc() instead of pte_alloc_map(). We can't run
3061 * pte_offset_map() on pmds where a huge pmd might be created
3062 * from a different thread.
3063 *
3064 * pte_alloc_map() is safe to use under down_write(mmap_sem) or when
3065 * parallel threads are excluded by other means.
3066 *
3067 * Here we only have down_read(mmap_sem).
3068 */
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08003069 if (pte_alloc(vma->vm_mm, vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003070 return VM_FAULT_OOM;
3071
3072 /* See the comment in pte_alloc_one_map() */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003073 if (unlikely(pmd_trans_unstable(vmf->pmd)))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003074 return 0;
3075
Linus Torvalds11ac5522010-08-14 11:44:56 -07003076 /* Use the zero-page for reads */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003077 if (!(vmf->flags & FAULT_FLAG_WRITE) &&
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003078 !mm_forbids_zeropage(vma->vm_mm)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003079 entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address),
Hugh Dickins62eede62009-09-21 17:03:34 -07003080 vma->vm_page_prot));
Jan Kara82b0f8c2016-12-14 15:06:58 -08003081 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
3082 vmf->address, &vmf->ptl);
3083 if (!pte_none(*vmf->pte))
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -07003084 goto unlock;
Michal Hocko6b31d592017-08-18 15:16:15 -07003085 ret = check_stable_address_space(vma->vm_mm);
3086 if (ret)
3087 goto unlock;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07003088 /* Deliver the page fault to userland, check inside PT lock */
3089 if (userfaultfd_missing(vma)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003090 pte_unmap_unlock(vmf->pte, vmf->ptl);
3091 return handle_userfault(vmf, VM_UFFD_MISSING);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07003092 }
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -07003093 goto setpte;
3094 }
3095
Nick Piggin557ed1f2007-10-16 01:24:40 -07003096 /* Allocate our own private page. */
Nick Piggin557ed1f2007-10-16 01:24:40 -07003097 if (unlikely(anon_vma_prepare(vma)))
3098 goto oom;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003099 page = alloc_zeroed_user_highpage_movable(vma, vmf->address);
Nick Piggin557ed1f2007-10-16 01:24:40 -07003100 if (!page)
3101 goto oom;
Mel Gormaneb3c24f2015-06-24 16:57:27 -07003102
Tejun Heo2cf85582018-07-03 11:14:56 -04003103 if (mem_cgroup_try_charge_delay(page, vma->vm_mm, GFP_KERNEL, &memcg,
3104 false))
Mel Gormaneb3c24f2015-06-24 16:57:27 -07003105 goto oom_free_page;
3106
Minchan Kim52f37622013-04-29 15:08:15 -07003107 /*
3108 * The memory barrier inside __SetPageUptodate makes sure that
Wei Yangf4f53292019-11-30 17:58:17 -08003109 * preceding stores to the page contents become visible before
Minchan Kim52f37622013-04-29 15:08:15 -07003110 * the set_pte_at() write.
3111 */
Nick Piggin0ed361d2008-02-04 22:29:34 -08003112 __SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Nick Piggin557ed1f2007-10-16 01:24:40 -07003114 entry = mk_pte(page, vma->vm_page_prot);
Hugh Dickins1ac0cb52009-09-21 17:03:29 -07003115 if (vma->vm_flags & VM_WRITE)
3116 entry = pte_mkwrite(pte_mkdirty(entry));
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003117
Jan Kara82b0f8c2016-12-14 15:06:58 -08003118 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3119 &vmf->ptl);
3120 if (!pte_none(*vmf->pte))
Nick Piggin557ed1f2007-10-16 01:24:40 -07003121 goto release;
Hugh Dickins9ba69292009-09-21 17:02:20 -07003122
Michal Hocko6b31d592017-08-18 15:16:15 -07003123 ret = check_stable_address_space(vma->vm_mm);
3124 if (ret)
3125 goto release;
3126
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07003127 /* Deliver the page fault to userland, check inside PT lock */
3128 if (userfaultfd_missing(vma)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003129 pte_unmap_unlock(vmf->pte, vmf->ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08003130 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003131 put_page(page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003132 return handle_userfault(vmf, VM_UFFD_MISSING);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07003133 }
3134
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003135 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003136 page_add_new_anon_rmap(page, vma, vmf->address, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08003137 mem_cgroup_commit_charge(page, memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07003138 lru_cache_add_active_or_unevictable(page, vma);
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -07003139setpte:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003140 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
3142 /* No need to invalidate - it was non-present before */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003143 update_mmu_cache(vma, vmf->address, vmf->pte);
Hugh Dickins65500d22005-10-29 18:15:59 -07003144unlock:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003145 pte_unmap_unlock(vmf->pte, vmf->ptl);
Michal Hocko6b31d592017-08-18 15:16:15 -07003146 return ret;
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003147release:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08003148 mem_cgroup_cancel_charge(page, memcg, false);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003149 put_page(page);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003150 goto unlock;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003151oom_free_page:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003152 put_page(page);
Hugh Dickins65500d22005-10-29 18:15:59 -07003153oom:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 return VM_FAULT_OOM;
3155}
3156
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003157/*
3158 * The mmap_sem must have been held on entry, and may have been
3159 * released depending on flags and vma->vm_ops->fault() return value.
3160 * See filemap_fault() and __lock_page_retry().
3161 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003162static vm_fault_t __do_fault(struct vm_fault *vmf)
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003163{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003164 struct vm_area_struct *vma = vmf->vma;
Souptick Joarder2b740302018-08-23 17:01:36 -07003165 vm_fault_t ret;
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003166
Michal Hocko63f36552019-01-08 15:23:07 -08003167 /*
3168 * Preallocate pte before we take page_lock because this might lead to
3169 * deadlocks for memcg reclaim which waits for pages under writeback:
3170 * lock_page(A)
3171 * SetPageWriteback(A)
3172 * unlock_page(A)
3173 * lock_page(B)
3174 * lock_page(B)
3175 * pte_alloc_pne
3176 * shrink_page_list
3177 * wait_on_page_writeback(A)
3178 * SetPageWriteback(B)
3179 * unlock_page(B)
3180 * # flush A, B to clear the writeback
3181 */
3182 if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) {
3183 vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm);
3184 if (!vmf->prealloc_pte)
3185 return VM_FAULT_OOM;
3186 smp_wmb(); /* See comment in __pte_alloc() */
3187 }
3188
Dave Jiang11bac802017-02-24 14:56:41 -08003189 ret = vma->vm_ops->fault(vmf);
Jan Kara39170482016-12-14 15:07:18 -08003190 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY |
Jan Karab1aa8122016-12-14 15:07:24 -08003191 VM_FAULT_DONE_COW)))
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003192 return ret;
3193
Jan Kara667240e2016-12-14 15:07:07 -08003194 if (unlikely(PageHWPoison(vmf->page))) {
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003195 if (ret & VM_FAULT_LOCKED)
Jan Kara667240e2016-12-14 15:07:07 -08003196 unlock_page(vmf->page);
3197 put_page(vmf->page);
Jan Kara936ca802016-12-14 15:07:10 -08003198 vmf->page = NULL;
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003199 return VM_FAULT_HWPOISON;
3200 }
3201
3202 if (unlikely(!(ret & VM_FAULT_LOCKED)))
Jan Kara667240e2016-12-14 15:07:07 -08003203 lock_page(vmf->page);
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003204 else
Jan Kara667240e2016-12-14 15:07:07 -08003205 VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page);
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003206
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003207 return ret;
3208}
3209
Ross Zwislerd0f0931d2017-06-02 14:46:34 -07003210/*
3211 * The ordering of these checks is important for pmds with _PAGE_DEVMAP set.
3212 * If we check pmd_trans_unstable() first we will trip the bad_pmd() check
3213 * inside of pmd_none_or_trans_huge_or_clear_bad(). This will end up correctly
3214 * returning 1 but not before it spams dmesg with the pmd_clear_bad() output.
3215 */
3216static int pmd_devmap_trans_unstable(pmd_t *pmd)
3217{
3218 return pmd_devmap(*pmd) || pmd_trans_unstable(pmd);
3219}
3220
Souptick Joarder2b740302018-08-23 17:01:36 -07003221static vm_fault_t pte_alloc_one_map(struct vm_fault *vmf)
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003222{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003223 struct vm_area_struct *vma = vmf->vma;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003224
Jan Kara82b0f8c2016-12-14 15:06:58 -08003225 if (!pmd_none(*vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003226 goto map_pte;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003227 if (vmf->prealloc_pte) {
3228 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
3229 if (unlikely(!pmd_none(*vmf->pmd))) {
3230 spin_unlock(vmf->ptl);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003231 goto map_pte;
3232 }
3233
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08003234 mm_inc_nr_ptes(vma->vm_mm);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003235 pmd_populate(vma->vm_mm, vmf->pmd, vmf->prealloc_pte);
3236 spin_unlock(vmf->ptl);
Tobin C Harding7f2b6ce2017-02-24 14:58:59 -08003237 vmf->prealloc_pte = NULL;
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08003238 } else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd))) {
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003239 return VM_FAULT_OOM;
3240 }
3241map_pte:
3242 /*
3243 * If a huge pmd materialized under us just retry later. Use
Ross Zwislerd0f0931d2017-06-02 14:46:34 -07003244 * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead of
3245 * pmd_trans_huge() to ensure the pmd didn't become pmd_trans_huge
3246 * under us and then back to pmd_none, as a result of MADV_DONTNEED
3247 * running immediately after a huge pmd fault in a different thread of
3248 * this mm, in turn leading to a misleading pmd_trans_huge() retval.
3249 * All we have to ensure is that it is a regular pmd that we can walk
3250 * with pte_offset_map() and we can do that through an atomic read in
3251 * C, which is what pmd_trans_unstable() provides.
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003252 */
Ross Zwislerd0f0931d2017-06-02 14:46:34 -07003253 if (pmd_devmap_trans_unstable(vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003254 return VM_FAULT_NOPAGE;
3255
Ross Zwislerd0f0931d2017-06-02 14:46:34 -07003256 /*
3257 * At this point we know that our vmf->pmd points to a page of ptes
3258 * and it cannot become pmd_none(), pmd_devmap() or pmd_trans_huge()
3259 * for the duration of the fault. If a racing MADV_DONTNEED runs and
3260 * we zap the ptes pointed to by our vmf->pmd, the vmf->ptl will still
3261 * be valid and we will re-check to make sure the vmf->pte isn't
3262 * pte_none() under vmf->ptl protection when we return to
3263 * alloc_set_pte().
3264 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003265 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3266 &vmf->ptl);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003267 return 0;
3268}
3269
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003270#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Jan Kara82b0f8c2016-12-14 15:06:58 -08003271static void deposit_prealloc_pte(struct vm_fault *vmf)
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003272{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003273 struct vm_area_struct *vma = vmf->vma;
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003274
Jan Kara82b0f8c2016-12-14 15:06:58 -08003275 pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte);
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003276 /*
3277 * We are going to consume the prealloc table,
3278 * count that as nr_ptes.
3279 */
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08003280 mm_inc_nr_ptes(vma->vm_mm);
Tobin C Harding7f2b6ce2017-02-24 14:58:59 -08003281 vmf->prealloc_pte = NULL;
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003282}
3283
Souptick Joarder2b740302018-08-23 17:01:36 -07003284static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003285{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003286 struct vm_area_struct *vma = vmf->vma;
3287 bool write = vmf->flags & FAULT_FLAG_WRITE;
3288 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003289 pmd_t entry;
Souptick Joarder2b740302018-08-23 17:01:36 -07003290 int i;
3291 vm_fault_t ret;
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003292
3293 if (!transhuge_vma_suitable(vma, haddr))
3294 return VM_FAULT_FALLBACK;
3295
3296 ret = VM_FAULT_FALLBACK;
3297 page = compound_head(page);
3298
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003299 /*
3300 * Archs like ppc64 need additonal space to store information
3301 * related to pte entry. Use the preallocated table for that.
3302 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003303 if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) {
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08003304 vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003305 if (!vmf->prealloc_pte)
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003306 return VM_FAULT_OOM;
3307 smp_wmb(); /* See comment in __pte_alloc() */
3308 }
3309
Jan Kara82b0f8c2016-12-14 15:06:58 -08003310 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
3311 if (unlikely(!pmd_none(*vmf->pmd)))
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003312 goto out;
3313
3314 for (i = 0; i < HPAGE_PMD_NR; i++)
3315 flush_icache_page(vma, page + i);
3316
3317 entry = mk_huge_pmd(page, vma->vm_page_prot);
3318 if (write)
Linus Torvaldsf55e1012017-11-29 09:01:01 -08003319 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003320
Yang Shifadae292018-08-17 15:44:55 -07003321 add_mm_counter(vma->vm_mm, mm_counter_file(page), HPAGE_PMD_NR);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003322 page_add_file_rmap(page, true);
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003323 /*
3324 * deposit and withdraw with pmd lock held
3325 */
3326 if (arch_needs_pgtable_deposit())
Jan Kara82b0f8c2016-12-14 15:06:58 -08003327 deposit_prealloc_pte(vmf);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003328
Jan Kara82b0f8c2016-12-14 15:06:58 -08003329 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003330
Jan Kara82b0f8c2016-12-14 15:06:58 -08003331 update_mmu_cache_pmd(vma, haddr, vmf->pmd);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003332
3333 /* fault is handled */
3334 ret = 0;
Kirill A. Shutemov95ecedc2016-07-26 15:25:31 -07003335 count_vm_event(THP_FILE_MAPPED);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003336out:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003337 spin_unlock(vmf->ptl);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003338 return ret;
3339}
3340#else
Souptick Joarder2b740302018-08-23 17:01:36 -07003341static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003342{
3343 BUILD_BUG();
3344 return 0;
3345}
3346#endif
3347
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003348/**
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003349 * alloc_set_pte - setup new PTE entry for given page and add reverse page
3350 * mapping. If needed, the fucntion allocates page table or use pre-allocated.
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003351 *
Jan Kara82b0f8c2016-12-14 15:06:58 -08003352 * @vmf: fault environment
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003353 * @memcg: memcg to charge page (only for private mappings)
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003354 * @page: page to map
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003355 *
Jan Kara82b0f8c2016-12-14 15:06:58 -08003356 * Caller must take care of unlocking vmf->ptl, if vmf->pte is non-NULL on
3357 * return.
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003358 *
3359 * Target users are page handler itself and implementations of
3360 * vm_ops->map_pages.
Mike Rapoporta862f682019-03-05 15:48:42 -08003361 *
3362 * Return: %0 on success, %VM_FAULT_ code in case of error.
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003363 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003364vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg,
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003365 struct page *page)
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003366{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003367 struct vm_area_struct *vma = vmf->vma;
3368 bool write = vmf->flags & FAULT_FLAG_WRITE;
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003369 pte_t entry;
Souptick Joarder2b740302018-08-23 17:01:36 -07003370 vm_fault_t ret;
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003371
Jan Kara82b0f8c2016-12-14 15:06:58 -08003372 if (pmd_none(*vmf->pmd) && PageTransCompound(page) &&
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003373 IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003374 /* THP on COW? */
3375 VM_BUG_ON_PAGE(memcg, page);
3376
Jan Kara82b0f8c2016-12-14 15:06:58 -08003377 ret = do_set_pmd(vmf, page);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003378 if (ret != VM_FAULT_FALLBACK)
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003379 return ret;
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003380 }
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003381
Jan Kara82b0f8c2016-12-14 15:06:58 -08003382 if (!vmf->pte) {
3383 ret = pte_alloc_one_map(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003384 if (ret)
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003385 return ret;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003386 }
3387
3388 /* Re-check under ptl */
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003389 if (unlikely(!pte_none(*vmf->pte)))
3390 return VM_FAULT_NOPAGE;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003391
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003392 flush_icache_page(vma, page);
3393 entry = mk_pte(page, vma->vm_page_prot);
3394 if (write)
3395 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003396 /* copy-on-write page */
3397 if (write && !(vma->vm_flags & VM_SHARED)) {
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003398 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003399 page_add_new_anon_rmap(page, vma, vmf->address, false);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003400 mem_cgroup_commit_charge(page, memcg, false, false);
3401 lru_cache_add_active_or_unevictable(page, vma);
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003402 } else {
Jerome Marchandeca56ff2016-01-14 15:19:26 -08003403 inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07003404 page_add_file_rmap(page, false);
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003405 }
Jan Kara82b0f8c2016-12-14 15:06:58 -08003406 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003407
3408 /* no need to invalidate: a not-present page won't be cached */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003409 update_mmu_cache(vma, vmf->address, vmf->pte);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003410
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003411 return 0;
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003412}
3413
Jan Kara9118c0c2016-12-14 15:07:21 -08003414
3415/**
3416 * finish_fault - finish page fault once we have prepared the page to fault
3417 *
3418 * @vmf: structure describing the fault
3419 *
3420 * This function handles all that is needed to finish a page fault once the
3421 * page to fault in is prepared. It handles locking of PTEs, inserts PTE for
3422 * given page, adds reverse page mapping, handles memcg charges and LRU
Mike Rapoporta862f682019-03-05 15:48:42 -08003423 * addition.
Jan Kara9118c0c2016-12-14 15:07:21 -08003424 *
3425 * The function expects the page to be locked and on success it consumes a
3426 * reference of a page being mapped (for the PTE which maps it).
Mike Rapoporta862f682019-03-05 15:48:42 -08003427 *
3428 * Return: %0 on success, %VM_FAULT_ code in case of error.
Jan Kara9118c0c2016-12-14 15:07:21 -08003429 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003430vm_fault_t finish_fault(struct vm_fault *vmf)
Jan Kara9118c0c2016-12-14 15:07:21 -08003431{
3432 struct page *page;
Souptick Joarder2b740302018-08-23 17:01:36 -07003433 vm_fault_t ret = 0;
Jan Kara9118c0c2016-12-14 15:07:21 -08003434
3435 /* Did we COW the page? */
3436 if ((vmf->flags & FAULT_FLAG_WRITE) &&
3437 !(vmf->vma->vm_flags & VM_SHARED))
3438 page = vmf->cow_page;
3439 else
3440 page = vmf->page;
Michal Hocko6b31d592017-08-18 15:16:15 -07003441
3442 /*
3443 * check even for read faults because we might have lost our CoWed
3444 * page
3445 */
3446 if (!(vmf->vma->vm_flags & VM_SHARED))
3447 ret = check_stable_address_space(vmf->vma->vm_mm);
3448 if (!ret)
3449 ret = alloc_set_pte(vmf, vmf->memcg, page);
Jan Kara9118c0c2016-12-14 15:07:21 -08003450 if (vmf->pte)
3451 pte_unmap_unlock(vmf->pte, vmf->ptl);
3452 return ret;
3453}
3454
Kirill A. Shutemov3a910532014-08-06 16:08:07 -07003455static unsigned long fault_around_bytes __read_mostly =
3456 rounddown_pow_of_two(65536);
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003457
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003458#ifdef CONFIG_DEBUG_FS
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003459static int fault_around_bytes_get(void *data, u64 *val)
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003460{
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003461 *val = fault_around_bytes;
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003462 return 0;
3463}
3464
Andrey Ryabininb4903d62014-07-30 16:08:35 -07003465/*
William Kucharskida391d62018-01-31 16:21:11 -08003466 * fault_around_bytes must be rounded down to the nearest page order as it's
3467 * what do_fault_around() expects to see.
Andrey Ryabininb4903d62014-07-30 16:08:35 -07003468 */
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003469static int fault_around_bytes_set(void *data, u64 val)
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003470{
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003471 if (val / PAGE_SIZE > PTRS_PER_PTE)
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003472 return -EINVAL;
Andrey Ryabininb4903d62014-07-30 16:08:35 -07003473 if (val > PAGE_SIZE)
3474 fault_around_bytes = rounddown_pow_of_two(val);
3475 else
3476 fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003477 return 0;
3478}
Yevgen Pronenko0a1345f2017-07-10 15:47:17 -07003479DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops,
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003480 fault_around_bytes_get, fault_around_bytes_set, "%llu\n");
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003481
3482static int __init fault_around_debugfs(void)
3483{
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -08003484 debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL,
3485 &fault_around_bytes_fops);
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003486 return 0;
3487}
3488late_initcall(fault_around_debugfs);
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003489#endif
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003490
Kirill A. Shutemov1fdb4122014-06-04 16:10:55 -07003491/*
3492 * do_fault_around() tries to map few pages around the fault address. The hope
3493 * is that the pages will be needed soon and this will lower the number of
3494 * faults to handle.
3495 *
3496 * It uses vm_ops->map_pages() to map the pages, which skips the page if it's
3497 * not ready to be mapped: not up-to-date, locked, etc.
3498 *
3499 * This function is called with the page table lock taken. In the split ptlock
3500 * case the page table lock only protects only those entries which belong to
3501 * the page table corresponding to the fault address.
3502 *
3503 * This function doesn't cross the VMA boundaries, in order to call map_pages()
3504 * only once.
3505 *
William Kucharskida391d62018-01-31 16:21:11 -08003506 * fault_around_bytes defines how many bytes we'll try to map.
3507 * do_fault_around() expects it to be set to a power of two less than or equal
3508 * to PTRS_PER_PTE.
Kirill A. Shutemov1fdb4122014-06-04 16:10:55 -07003509 *
William Kucharskida391d62018-01-31 16:21:11 -08003510 * The virtual address of the area that we map is naturally aligned to
3511 * fault_around_bytes rounded down to the machine page size
3512 * (and therefore to page order). This way it's easier to guarantee
3513 * that we don't cross page table boundaries.
Kirill A. Shutemov1fdb4122014-06-04 16:10:55 -07003514 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003515static vm_fault_t do_fault_around(struct vm_fault *vmf)
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003516{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003517 unsigned long address = vmf->address, nr_pages, mask;
Jan Kara0721ec82016-12-14 15:07:04 -08003518 pgoff_t start_pgoff = vmf->pgoff;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003519 pgoff_t end_pgoff;
Souptick Joarder2b740302018-08-23 17:01:36 -07003520 int off;
3521 vm_fault_t ret = 0;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003522
Jason Low4db0c3c2015-04-15 16:14:08 -07003523 nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT;
Kirill A. Shutemovaecd6f42014-08-06 16:08:05 -07003524 mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
3525
Jan Kara82b0f8c2016-12-14 15:06:58 -08003526 vmf->address = max(address & mask, vmf->vma->vm_start);
3527 off = ((address - vmf->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003528 start_pgoff -= off;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003529
3530 /*
William Kucharskida391d62018-01-31 16:21:11 -08003531 * end_pgoff is either the end of the page table, the end of
3532 * the vma or nr_pages from start_pgoff, depending what is nearest.
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003533 */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003534 end_pgoff = start_pgoff -
Jan Kara82b0f8c2016-12-14 15:06:58 -08003535 ((vmf->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003536 PTRS_PER_PTE - 1;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003537 end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003538 start_pgoff + nr_pages - 1);
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003539
Jan Kara82b0f8c2016-12-14 15:06:58 -08003540 if (pmd_none(*vmf->pmd)) {
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08003541 vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003542 if (!vmf->prealloc_pte)
Vegard Nossumc5f88bd2016-08-02 14:02:22 -07003543 goto out;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003544 smp_wmb(); /* See comment in __pte_alloc() */
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003545 }
3546
Jan Kara82b0f8c2016-12-14 15:06:58 -08003547 vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003548
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003549 /* Huge page is mapped? Page fault is solved */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003550 if (pmd_trans_huge(*vmf->pmd)) {
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003551 ret = VM_FAULT_NOPAGE;
3552 goto out;
3553 }
3554
3555 /* ->map_pages() haven't done anything useful. Cold page cache? */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003556 if (!vmf->pte)
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003557 goto out;
3558
3559 /* check if the page fault is solved */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003560 vmf->pte -= (vmf->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT);
3561 if (!pte_none(*vmf->pte))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003562 ret = VM_FAULT_NOPAGE;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003563 pte_unmap_unlock(vmf->pte, vmf->ptl);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003564out:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003565 vmf->address = address;
3566 vmf->pte = NULL;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003567 return ret;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003568}
3569
Souptick Joarder2b740302018-08-23 17:01:36 -07003570static vm_fault_t do_read_fault(struct vm_fault *vmf)
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003571{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003572 struct vm_area_struct *vma = vmf->vma;
Souptick Joarder2b740302018-08-23 17:01:36 -07003573 vm_fault_t ret = 0;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003574
3575 /*
3576 * Let's call ->map_pages() first and use ->fault() as fallback
3577 * if page by the offset is not ready to be mapped (cold cache or
3578 * something).
3579 */
Kirill A. Shutemov9b4bdd22015-02-10 14:09:51 -08003580 if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) {
Jan Kara0721ec82016-12-14 15:07:04 -08003581 ret = do_fault_around(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003582 if (ret)
3583 return ret;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003584 }
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003585
Jan Kara936ca802016-12-14 15:07:10 -08003586 ret = __do_fault(vmf);
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003587 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3588 return ret;
3589
Jan Kara9118c0c2016-12-14 15:07:21 -08003590 ret |= finish_fault(vmf);
Jan Kara936ca802016-12-14 15:07:10 -08003591 unlock_page(vmf->page);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003592 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
Jan Kara936ca802016-12-14 15:07:10 -08003593 put_page(vmf->page);
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07003594 return ret;
3595}
3596
Souptick Joarder2b740302018-08-23 17:01:36 -07003597static vm_fault_t do_cow_fault(struct vm_fault *vmf)
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003598{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003599 struct vm_area_struct *vma = vmf->vma;
Souptick Joarder2b740302018-08-23 17:01:36 -07003600 vm_fault_t ret;
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003601
3602 if (unlikely(anon_vma_prepare(vma)))
3603 return VM_FAULT_OOM;
3604
Jan Kara936ca802016-12-14 15:07:10 -08003605 vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address);
3606 if (!vmf->cow_page)
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003607 return VM_FAULT_OOM;
3608
Tejun Heo2cf85582018-07-03 11:14:56 -04003609 if (mem_cgroup_try_charge_delay(vmf->cow_page, vma->vm_mm, GFP_KERNEL,
Jan Kara39170482016-12-14 15:07:18 -08003610 &vmf->memcg, false)) {
Jan Kara936ca802016-12-14 15:07:10 -08003611 put_page(vmf->cow_page);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003612 return VM_FAULT_OOM;
3613 }
3614
Jan Kara936ca802016-12-14 15:07:10 -08003615 ret = __do_fault(vmf);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003616 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3617 goto uncharge_out;
Jan Kara39170482016-12-14 15:07:18 -08003618 if (ret & VM_FAULT_DONE_COW)
3619 return ret;
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003620
Jan Karab1aa8122016-12-14 15:07:24 -08003621 copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma);
Jan Kara936ca802016-12-14 15:07:10 -08003622 __SetPageUptodate(vmf->cow_page);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003623
Jan Kara9118c0c2016-12-14 15:07:21 -08003624 ret |= finish_fault(vmf);
Jan Karab1aa8122016-12-14 15:07:24 -08003625 unlock_page(vmf->page);
3626 put_page(vmf->page);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003627 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
3628 goto uncharge_out;
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003629 return ret;
3630uncharge_out:
Jan Kara39170482016-12-14 15:07:18 -08003631 mem_cgroup_cancel_charge(vmf->cow_page, vmf->memcg, false);
Jan Kara936ca802016-12-14 15:07:10 -08003632 put_page(vmf->cow_page);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07003633 return ret;
3634}
3635
Souptick Joarder2b740302018-08-23 17:01:36 -07003636static vm_fault_t do_shared_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003638 struct vm_area_struct *vma = vmf->vma;
Souptick Joarder2b740302018-08-23 17:01:36 -07003639 vm_fault_t ret, tmp;
KAMEZAWA Hiroyuki1d65f862011-07-25 17:12:27 -07003640
Jan Kara936ca802016-12-14 15:07:10 -08003641 ret = __do_fault(vmf);
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003642 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003643 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
3645 /*
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003646 * Check if the backing address space wants to know that the page is
3647 * about to become writable
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 */
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07003649 if (vma->vm_ops->page_mkwrite) {
Jan Kara936ca802016-12-14 15:07:10 -08003650 unlock_page(vmf->page);
Jan Kara38b8cb72016-12-14 15:07:30 -08003651 tmp = do_page_mkwrite(vmf);
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07003652 if (unlikely(!tmp ||
3653 (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
Jan Kara936ca802016-12-14 15:07:10 -08003654 put_page(vmf->page);
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07003655 return tmp;
3656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 }
3658
Jan Kara9118c0c2016-12-14 15:07:21 -08003659 ret |= finish_fault(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003660 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
3661 VM_FAULT_RETRY))) {
Jan Kara936ca802016-12-14 15:07:10 -08003662 unlock_page(vmf->page);
3663 put_page(vmf->page);
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003664 return ret;
Peter Zijlstrad08b3852006-09-25 23:30:57 -07003665 }
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07003666
Johannes Weiner89b15332019-11-30 17:50:22 -08003667 ret |= fault_dirty_shared_page(vmf);
KAMEZAWA Hiroyuki1d65f862011-07-25 17:12:27 -07003668 return ret;
Nick Piggin54cb8822007-07-19 01:46:59 -07003669}
Nick Piggind00806b2007-07-19 01:46:57 -07003670
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003671/*
3672 * We enter with non-exclusive mmap_sem (to exclude vma changes,
3673 * but allow concurrent faults).
3674 * The mmap_sem may have been released depending on flags and our
3675 * return value. See filemap_fault() and __lock_page_or_retry().
Jan Stancekfc8efd2d2019-03-05 15:50:08 -08003676 * If mmap_sem is released, vma may become invalid (for example
3677 * by other thread calling munmap()).
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003678 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003679static vm_fault_t do_fault(struct vm_fault *vmf)
Nick Piggin54cb8822007-07-19 01:46:59 -07003680{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003681 struct vm_area_struct *vma = vmf->vma;
Jan Stancekfc8efd2d2019-03-05 15:50:08 -08003682 struct mm_struct *vm_mm = vma->vm_mm;
Souptick Joarder2b740302018-08-23 17:01:36 -07003683 vm_fault_t ret;
Nick Piggin54cb8822007-07-19 01:46:59 -07003684
Aneesh Kumar K.Vff09d7e2018-10-26 15:09:01 -07003685 /*
3686 * The VMA was not fully populated on mmap() or missing VM_DONTEXPAND
3687 */
3688 if (!vma->vm_ops->fault) {
3689 /*
3690 * If we find a migration pmd entry or a none pmd entry, which
3691 * should never happen, return SIGBUS
3692 */
3693 if (unlikely(!pmd_present(*vmf->pmd)))
3694 ret = VM_FAULT_SIGBUS;
3695 else {
3696 vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm,
3697 vmf->pmd,
3698 vmf->address,
3699 &vmf->ptl);
3700 /*
3701 * Make sure this is not a temporary clearing of pte
3702 * by holding ptl and checking again. A R/M/W update
3703 * of pte involves: take ptl, clearing the pte so that
3704 * we don't have concurrent modification by hardware
3705 * followed by an update.
3706 */
3707 if (unlikely(pte_none(*vmf->pte)))
3708 ret = VM_FAULT_SIGBUS;
3709 else
3710 ret = VM_FAULT_NOPAGE;
3711
3712 pte_unmap_unlock(vmf->pte, vmf->ptl);
3713 }
3714 } else if (!(vmf->flags & FAULT_FLAG_WRITE))
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003715 ret = do_read_fault(vmf);
3716 else if (!(vma->vm_flags & VM_SHARED))
3717 ret = do_cow_fault(vmf);
3718 else
3719 ret = do_shared_fault(vmf);
3720
3721 /* preallocated pagetable is unused: free it */
3722 if (vmf->prealloc_pte) {
Jan Stancekfc8efd2d2019-03-05 15:50:08 -08003723 pte_free(vm_mm, vmf->prealloc_pte);
Tobin C Harding7f2b6ce2017-02-24 14:58:59 -08003724 vmf->prealloc_pte = NULL;
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08003725 }
3726 return ret;
Nick Piggin54cb8822007-07-19 01:46:59 -07003727}
3728
Rashika Kheriab19a9932014-04-03 14:48:02 -07003729static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
Rik van Riel04bb2f92013-10-07 11:29:36 +01003730 unsigned long addr, int page_nid,
3731 int *flags)
Mel Gorman9532fec2012-11-15 01:24:32 +00003732{
3733 get_page(page);
3734
3735 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01003736 if (page_nid == numa_node_id()) {
Mel Gorman9532fec2012-11-15 01:24:32 +00003737 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01003738 *flags |= TNF_FAULT_LOCAL;
3739 }
Mel Gorman9532fec2012-11-15 01:24:32 +00003740
3741 return mpol_misplaced(page, vma, addr);
3742}
3743
Souptick Joarder2b740302018-08-23 17:01:36 -07003744static vm_fault_t do_numa_page(struct vm_fault *vmf)
Mel Gormand10e63f2012-10-25 14:16:31 +02003745{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003746 struct vm_area_struct *vma = vmf->vma;
Mel Gorman4daae3b2012-11-02 11:33:45 +00003747 struct page *page = NULL;
Anshuman Khandual98fa15f2019-03-05 15:42:58 -08003748 int page_nid = NUMA_NO_NODE;
Peter Zijlstra90572892013-10-07 11:29:20 +01003749 int last_cpupid;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02003750 int target_nid;
Mel Gormanb8593bf2012-11-21 01:18:23 +00003751 bool migrated = false;
Aneesh Kumar K.V04a86452019-03-05 15:46:29 -08003752 pte_t pte, old_pte;
Aneesh Kumar K.V288bc542017-02-24 14:59:16 -08003753 bool was_writable = pte_savedwrite(vmf->orig_pte);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003754 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02003755
3756 /*
Tobin C Harding166f61b2017-02-24 14:59:01 -08003757 * The "pte" at this point cannot be used safely without
3758 * validation through pte_unmap_same(). It's of NUMA type but
3759 * the pfn may be screwed if the read is non atomic.
Tobin C Harding166f61b2017-02-24 14:59:01 -08003760 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003761 vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd);
3762 spin_lock(vmf->ptl);
Aneesh Kumar K.Vcee216a2017-02-24 14:59:13 -08003763 if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003764 pte_unmap_unlock(vmf->pte, vmf->ptl);
Mel Gorman4daae3b2012-11-02 11:33:45 +00003765 goto out;
3766 }
3767
Aneesh Kumar K.Vcee216a2017-02-24 14:59:13 -08003768 /*
3769 * Make it present again, Depending on how arch implementes non
3770 * accessible ptes, some can allow access by kernel mode.
3771 */
Aneesh Kumar K.V04a86452019-03-05 15:46:29 -08003772 old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte);
3773 pte = pte_modify(old_pte, vma->vm_page_prot);
Mel Gorman4d942462015-02-12 14:58:28 -08003774 pte = pte_mkyoung(pte);
Mel Gormanb191f9b2015-03-25 15:55:40 -07003775 if (was_writable)
3776 pte = pte_mkwrite(pte);
Aneesh Kumar K.V04a86452019-03-05 15:46:29 -08003777 ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003778 update_mmu_cache(vma, vmf->address, vmf->pte);
Mel Gormand10e63f2012-10-25 14:16:31 +02003779
Jan Kara82b0f8c2016-12-14 15:06:58 -08003780 page = vm_normal_page(vma, vmf->address, pte);
Mel Gormand10e63f2012-10-25 14:16:31 +02003781 if (!page) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003782 pte_unmap_unlock(vmf->pte, vmf->ptl);
Mel Gormand10e63f2012-10-25 14:16:31 +02003783 return 0;
3784 }
3785
Kirill A. Shutemove81c4802016-01-15 16:53:49 -08003786 /* TODO: handle PTE-mapped THP */
3787 if (PageCompound(page)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003788 pte_unmap_unlock(vmf->pte, vmf->ptl);
Kirill A. Shutemove81c4802016-01-15 16:53:49 -08003789 return 0;
3790 }
3791
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003792 /*
Mel Gormanbea66fb2015-03-25 15:55:37 -07003793 * Avoid grouping on RO pages in general. RO pages shouldn't hurt as
3794 * much anyway since they can be in shared cache state. This misses
3795 * the case where a mapping is writable but the process never writes
3796 * to it but pte_write gets cleared during protection updates and
3797 * pte_dirty has unpredictable behaviour between PTE scan updates,
3798 * background writeback, dirty balancing and application behaviour.
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003799 */
Rik van Rield59dc7b2016-09-08 21:30:53 -04003800 if (!pte_write(pte))
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003801 flags |= TNF_NO_GROUP;
3802
Rik van Rieldabe1d92013-10-07 11:29:34 +01003803 /*
3804 * Flag if the page is shared between multiple address spaces. This
3805 * is later used when determining whether to group tasks together
3806 */
3807 if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED))
3808 flags |= TNF_SHARED;
3809
Peter Zijlstra90572892013-10-07 11:29:20 +01003810 last_cpupid = page_cpupid_last(page);
Mel Gorman8191acb2013-10-07 11:28:45 +01003811 page_nid = page_to_nid(page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003812 target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003813 &flags);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003814 pte_unmap_unlock(vmf->pte, vmf->ptl);
Anshuman Khandual98fa15f2019-03-05 15:42:58 -08003815 if (target_nid == NUMA_NO_NODE) {
Mel Gorman4daae3b2012-11-02 11:33:45 +00003816 put_page(page);
3817 goto out;
3818 }
3819
3820 /* Migrate to the requested node */
Mel Gorman1bc115d2013-10-07 11:29:05 +01003821 migrated = migrate_misplaced_page(page, vma, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003822 if (migrated) {
Mel Gorman8191acb2013-10-07 11:28:45 +01003823 page_nid = target_nid;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003824 flags |= TNF_MIGRATED;
Mel Gorman074c2382015-03-25 15:55:42 -07003825 } else
3826 flags |= TNF_MIGRATE_FAIL;
Mel Gorman4daae3b2012-11-02 11:33:45 +00003827
3828out:
Anshuman Khandual98fa15f2019-03-05 15:42:58 -08003829 if (page_nid != NUMA_NO_NODE)
Peter Zijlstra6688cc02013-10-07 11:29:24 +01003830 task_numa_fault(last_cpupid, page_nid, 1, flags);
Mel Gormand10e63f2012-10-25 14:16:31 +02003831 return 0;
3832}
3833
Souptick Joarder2b740302018-08-23 17:01:36 -07003834static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf)
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07003835{
Dave Jiangf4200392017-02-22 15:40:06 -08003836 if (vma_is_anonymous(vmf->vma))
Jan Kara82b0f8c2016-12-14 15:06:58 -08003837 return do_huge_pmd_anonymous_page(vmf);
Dave Jianga2d58162017-02-24 14:56:59 -08003838 if (vmf->vma->vm_ops->huge_fault)
Dave Jiangc791ace2017-02-24 14:57:08 -08003839 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07003840 return VM_FAULT_FALLBACK;
3841}
3842
Geert Uytterhoeven183f24aa2017-12-14 15:32:52 -08003843/* `inline' is required to avoid gcc 4.1.2 build error */
Souptick Joarder2b740302018-08-23 17:01:36 -07003844static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd)
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07003845{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003846 if (vma_is_anonymous(vmf->vma))
3847 return do_huge_pmd_wp_page(vmf, orig_pmd);
Dave Jianga2d58162017-02-24 14:56:59 -08003848 if (vmf->vma->vm_ops->huge_fault)
Dave Jiangc791ace2017-02-24 14:57:08 -08003849 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);
Kirill A. Shutemovaf9e4d52016-07-26 15:25:40 -07003850
3851 /* COW handled on pte level: split pmd */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003852 VM_BUG_ON_VMA(vmf->vma->vm_flags & VM_SHARED, vmf->vma);
3853 __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL);
Kirill A. Shutemovaf9e4d52016-07-26 15:25:40 -07003854
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07003855 return VM_FAULT_FALLBACK;
3856}
3857
Lorenzo Stoakes38e08852016-09-11 23:54:25 +01003858static inline bool vma_is_accessible(struct vm_area_struct *vma)
3859{
3860 return vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE);
3861}
3862
Souptick Joarder2b740302018-08-23 17:01:36 -07003863static vm_fault_t create_huge_pud(struct vm_fault *vmf)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08003864{
3865#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3866 /* No support for anonymous transparent PUD pages yet */
3867 if (vma_is_anonymous(vmf->vma))
3868 return VM_FAULT_FALLBACK;
3869 if (vmf->vma->vm_ops->huge_fault)
Dave Jiangc791ace2017-02-24 14:57:08 -08003870 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08003871#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
3872 return VM_FAULT_FALLBACK;
3873}
3874
Souptick Joarder2b740302018-08-23 17:01:36 -07003875static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08003876{
3877#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3878 /* No support for anonymous transparent PUD pages yet */
3879 if (vma_is_anonymous(vmf->vma))
3880 return VM_FAULT_FALLBACK;
3881 if (vmf->vma->vm_ops->huge_fault)
Dave Jiangc791ace2017-02-24 14:57:08 -08003882 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08003883#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
3884 return VM_FAULT_FALLBACK;
3885}
3886
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887/*
3888 * These routines also need to handle stuff like marking pages dirty
3889 * and/or accessed for architectures that don't do it in hardware (most
3890 * RISC architectures). The early dirtying is also good on the i386.
3891 *
3892 * There is also a hook called "update_mmu_cache()" that architectures
3893 * with external mmu caches can use to update those (ie the Sparc or
3894 * PowerPC hashed page tables that act as extended TLBs).
3895 *
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003896 * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow
3897 * concurrent faults).
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003898 *
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003899 * The mmap_sem may have been released depending on flags and our return value.
3900 * See filemap_fault() and __lock_page_or_retry().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003902static vm_fault_t handle_pte_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903{
3904 pte_t entry;
3905
Jan Kara82b0f8c2016-12-14 15:06:58 -08003906 if (unlikely(pmd_none(*vmf->pmd))) {
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003907 /*
3908 * Leave __pte_alloc() until later: because vm_ops->fault may
3909 * want to allocate huge page, and if we expose page table
3910 * for an instant, it will be difficult to retract from
3911 * concurrent faults and from rmap lookups.
3912 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003913 vmf->pte = NULL;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003914 } else {
3915 /* See comment in pte_alloc_one_map() */
Ross Zwislerd0f0931d2017-06-02 14:46:34 -07003916 if (pmd_devmap_trans_unstable(vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003917 return 0;
3918 /*
3919 * A regular pmd is established and it can't morph into a huge
3920 * pmd from under us anymore at this point because we hold the
3921 * mmap_sem read mode and khugepaged takes it in write mode.
3922 * So now it's safe to run pte_offset_map().
3923 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003924 vmf->pte = pte_offset_map(vmf->pmd, vmf->address);
Jan Kara29943022016-12-14 15:07:16 -08003925 vmf->orig_pte = *vmf->pte;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003926
3927 /*
3928 * some architectures can have larger ptes than wordsize,
3929 * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and
Paul E. McKenneyb03a0fe2017-10-23 14:07:25 -07003930 * CONFIG_32BIT=y, so READ_ONCE cannot guarantee atomic
3931 * accesses. The code below just needs a consistent view
3932 * for the ifs and we later double check anyway with the
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003933 * ptl lock held. So here a barrier will do.
3934 */
3935 barrier();
Jan Kara29943022016-12-14 15:07:16 -08003936 if (pte_none(vmf->orig_pte)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003937 pte_unmap(vmf->pte);
3938 vmf->pte = NULL;
Hugh Dickins65500d22005-10-29 18:15:59 -07003939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 }
3941
Jan Kara82b0f8c2016-12-14 15:06:58 -08003942 if (!vmf->pte) {
3943 if (vma_is_anonymous(vmf->vma))
3944 return do_anonymous_page(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003945 else
Jan Kara82b0f8c2016-12-14 15:06:58 -08003946 return do_fault(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003947 }
3948
Jan Kara29943022016-12-14 15:07:16 -08003949 if (!pte_present(vmf->orig_pte))
3950 return do_swap_page(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003951
Jan Kara29943022016-12-14 15:07:16 -08003952 if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma))
3953 return do_numa_page(vmf);
Mel Gormand10e63f2012-10-25 14:16:31 +02003954
Jan Kara82b0f8c2016-12-14 15:06:58 -08003955 vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
3956 spin_lock(vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08003957 entry = vmf->orig_pte;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003958 if (unlikely(!pte_same(*vmf->pte, entry)))
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003959 goto unlock;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003960 if (vmf->flags & FAULT_FLAG_WRITE) {
Linus Torvaldsf6f37322017-12-15 18:53:22 -08003961 if (!pte_write(entry))
Jan Kara29943022016-12-14 15:07:16 -08003962 return do_wp_page(vmf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 entry = pte_mkdirty(entry);
3964 }
3965 entry = pte_mkyoung(entry);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003966 if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry,
3967 vmf->flags & FAULT_FLAG_WRITE)) {
3968 update_mmu_cache(vmf->vma, vmf->address, vmf->pte);
Andrea Arcangeli1a44e142005-10-29 18:16:48 -07003969 } else {
3970 /*
3971 * This is needed only for protection faults but the arch code
3972 * is not yet telling us if this is a protection fault or not.
3973 * This still avoids useless tlb flushes for .text page faults
3974 * with threads.
3975 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003976 if (vmf->flags & FAULT_FLAG_WRITE)
3977 flush_tlb_fix_spurious_fault(vmf->vma, vmf->address);
Andrea Arcangeli1a44e142005-10-29 18:16:48 -07003978 }
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003979unlock:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003980 pte_unmap_unlock(vmf->pte, vmf->ptl);
Nick Piggin83c54072007-07-19 01:47:05 -07003981 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982}
3983
3984/*
3985 * By the time we get here, we already hold the mm semaphore
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003986 *
3987 * The mmap_sem may have been released depending on flags and our
3988 * return value. See filemap_fault() and __lock_page_or_retry().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003990static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
3991 unsigned long address, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003993 struct vm_fault vmf = {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003994 .vma = vma,
Jan Kara1a29d852016-12-14 15:07:01 -08003995 .address = address & PAGE_MASK,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003996 .flags = flags,
Jan Kara0721ec82016-12-14 15:07:04 -08003997 .pgoff = linear_page_index(vma, address),
Jan Kara667240e2016-12-14 15:07:07 -08003998 .gfp_mask = __get_fault_gfp_mask(vma),
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003999 };
Anshuman Khandualfde26be2017-09-08 16:12:45 -07004000 unsigned int dirty = flags & FAULT_FLAG_WRITE;
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -07004001 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004003 p4d_t *p4d;
Souptick Joarder2b740302018-08-23 17:01:36 -07004004 vm_fault_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 pgd = pgd_offset(mm, address);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004007 p4d = p4d_alloc(mm, pgd, address);
4008 if (!p4d)
4009 return VM_FAULT_OOM;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004010
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004011 vmf.pud = pud_alloc(mm, p4d, address);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004012 if (!vmf.pud)
Hugh Dickinsc74df322005-10-29 18:16:23 -07004013 return VM_FAULT_OOM;
Thomas Hellstrom625110b2019-11-30 17:51:32 -08004014retry_pud:
Michal Hocko7635d9c2018-12-28 00:38:21 -08004015 if (pud_none(*vmf.pud) && __transparent_hugepage_enabled(vma)) {
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004016 ret = create_huge_pud(&vmf);
4017 if (!(ret & VM_FAULT_FALLBACK))
4018 return ret;
4019 } else {
4020 pud_t orig_pud = *vmf.pud;
4021
4022 barrier();
4023 if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) {
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004024
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004025 /* NUMA case for anonymous PUDs would go here */
4026
Linus Torvaldsf6f37322017-12-15 18:53:22 -08004027 if (dirty && !pud_write(orig_pud)) {
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004028 ret = wp_huge_pud(&vmf, orig_pud);
4029 if (!(ret & VM_FAULT_FALLBACK))
4030 return ret;
4031 } else {
4032 huge_pud_set_accessed(&vmf, orig_pud);
4033 return 0;
4034 }
4035 }
4036 }
4037
4038 vmf.pmd = pmd_alloc(mm, vmf.pud, address);
Jan Kara82b0f8c2016-12-14 15:06:58 -08004039 if (!vmf.pmd)
Hugh Dickinsc74df322005-10-29 18:16:23 -07004040 return VM_FAULT_OOM;
Thomas Hellstrom625110b2019-11-30 17:51:32 -08004041
4042 /* Huge pud page fault raced with pmd_alloc? */
4043 if (pud_trans_unstable(vmf.pud))
4044 goto retry_pud;
4045
Michal Hocko7635d9c2018-12-28 00:38:21 -08004046 if (pmd_none(*vmf.pmd) && __transparent_hugepage_enabled(vma)) {
Dave Jianga2d58162017-02-24 14:56:59 -08004047 ret = create_huge_pmd(&vmf);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -07004048 if (!(ret & VM_FAULT_FALLBACK))
4049 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08004050 } else {
Jan Kara82b0f8c2016-12-14 15:06:58 -08004051 pmd_t orig_pmd = *vmf.pmd;
David Rientjes1f1d06c2012-05-29 15:06:23 -07004052
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08004053 barrier();
Zi Yan84c3fc42017-09-08 16:11:01 -07004054 if (unlikely(is_swap_pmd(orig_pmd))) {
4055 VM_BUG_ON(thp_migration_supported() &&
4056 !is_pmd_migration_entry(orig_pmd));
4057 if (is_pmd_migration_entry(orig_pmd))
4058 pmd_migration_entry_wait(mm, vmf.pmd);
4059 return 0;
4060 }
Dan Williams5c7fb562016-01-15 16:56:52 -08004061 if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) {
Lorenzo Stoakes38e08852016-09-11 23:54:25 +01004062 if (pmd_protnone(orig_pmd) && vma_is_accessible(vma))
Jan Kara82b0f8c2016-12-14 15:06:58 -08004063 return do_huge_pmd_numa_page(&vmf, orig_pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02004064
Linus Torvaldsf6f37322017-12-15 18:53:22 -08004065 if (dirty && !pmd_write(orig_pmd)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08004066 ret = wp_huge_pmd(&vmf, orig_pmd);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08004067 if (!(ret & VM_FAULT_FALLBACK))
4068 return ret;
Will Deacona1dd4502012-12-11 16:01:27 -08004069 } else {
Jan Kara82b0f8c2016-12-14 15:06:58 -08004070 huge_pmd_set_accessed(&vmf, orig_pmd);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08004071 return 0;
David Rientjes1f1d06c2012-05-29 15:06:23 -07004072 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08004073 }
4074 }
4075
Jan Kara82b0f8c2016-12-14 15:06:58 -08004076 return handle_pte_fault(&vmf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077}
4078
Paul Cassella9a95f3c2014-08-06 16:07:24 -07004079/*
4080 * By the time we get here, we already hold the mm semaphore
4081 *
4082 * The mmap_sem may have been released depending on flags and our
4083 * return value. See filemap_fault() and __lock_page_or_retry().
4084 */
Souptick Joarder2b740302018-08-23 17:01:36 -07004085vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -07004086 unsigned int flags)
Johannes Weiner519e5242013-09-12 15:13:42 -07004087{
Souptick Joarder2b740302018-08-23 17:01:36 -07004088 vm_fault_t ret;
Johannes Weiner519e5242013-09-12 15:13:42 -07004089
4090 __set_current_state(TASK_RUNNING);
4091
4092 count_vm_event(PGFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07004093 count_memcg_event_mm(vma->vm_mm, PGFAULT);
Johannes Weiner519e5242013-09-12 15:13:42 -07004094
4095 /* do counter updates before entering really critical section. */
4096 check_sync_rss_stat(current);
4097
Laurent Dufourde0c7992017-09-08 16:13:12 -07004098 if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
4099 flags & FAULT_FLAG_INSTRUCTION,
4100 flags & FAULT_FLAG_REMOTE))
4101 return VM_FAULT_SIGSEGV;
4102
Johannes Weiner519e5242013-09-12 15:13:42 -07004103 /*
4104 * Enable the memcg OOM handling for faults triggered in user
4105 * space. Kernel faults are handled more gracefully.
4106 */
4107 if (flags & FAULT_FLAG_USER)
Michal Hocko29ef6802018-08-17 15:47:11 -07004108 mem_cgroup_enter_user_fault();
Johannes Weiner519e5242013-09-12 15:13:42 -07004109
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07004110 if (unlikely(is_vm_hugetlb_page(vma)))
4111 ret = hugetlb_fault(vma->vm_mm, vma, address, flags);
4112 else
4113 ret = __handle_mm_fault(vma, address, flags);
Johannes Weiner519e5242013-09-12 15:13:42 -07004114
Johannes Weiner49426422013-10-16 13:46:59 -07004115 if (flags & FAULT_FLAG_USER) {
Michal Hocko29ef6802018-08-17 15:47:11 -07004116 mem_cgroup_exit_user_fault();
Tobin C Harding166f61b2017-02-24 14:59:01 -08004117 /*
4118 * The task may have entered a memcg OOM situation but
4119 * if the allocation error was handled gracefully (no
4120 * VM_FAULT_OOM), there is no need to kill anything.
4121 * Just clean up the OOM state peacefully.
4122 */
4123 if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
4124 mem_cgroup_oom_synchronize(false);
Johannes Weiner49426422013-10-16 13:46:59 -07004125 }
Johannes Weiner3812c8c2013-09-12 15:13:44 -07004126
Johannes Weiner519e5242013-09-12 15:13:42 -07004127 return ret;
4128}
Jesse Barnese1d6d012014-12-12 16:55:27 -08004129EXPORT_SYMBOL_GPL(handle_mm_fault);
Johannes Weiner519e5242013-09-12 15:13:42 -07004130
Kirill A. Shutemov90eceff2017-03-09 17:24:08 +03004131#ifndef __PAGETABLE_P4D_FOLDED
4132/*
4133 * Allocate p4d page table.
4134 * We've already handled the fast-path in-line.
4135 */
4136int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
4137{
4138 p4d_t *new = p4d_alloc_one(mm, address);
4139 if (!new)
4140 return -ENOMEM;
4141
4142 smp_wmb(); /* See comment in __pte_alloc */
4143
4144 spin_lock(&mm->page_table_lock);
4145 if (pgd_present(*pgd)) /* Another has populated it */
4146 p4d_free(mm, new);
4147 else
4148 pgd_populate(mm, pgd, new);
4149 spin_unlock(&mm->page_table_lock);
4150 return 0;
4151}
4152#endif /* __PAGETABLE_P4D_FOLDED */
4153
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154#ifndef __PAGETABLE_PUD_FOLDED
4155/*
4156 * Allocate page upper directory.
Hugh Dickins872fec12005-10-29 18:16:21 -07004157 * We've already handled the fast-path in-line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 */
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004159int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160{
Hugh Dickinsc74df322005-10-29 18:16:23 -07004161 pud_t *new = pud_alloc_one(mm, address);
4162 if (!new)
Hugh Dickins1bb36302005-10-29 18:16:22 -07004163 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
Nick Piggin362a61a2008-05-14 06:37:36 +02004165 smp_wmb(); /* See comment in __pte_alloc */
4166
Hugh Dickins872fec12005-10-29 18:16:21 -07004167 spin_lock(&mm->page_table_lock);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004168#ifndef __ARCH_HAS_5LEVEL_HACK
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -08004169 if (!p4d_present(*p4d)) {
4170 mm_inc_nr_puds(mm);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004171 p4d_populate(mm, p4d, new);
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -08004172 } else /* Another has populated it */
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004173 pud_free(mm, new);
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -08004174#else
4175 if (!pgd_present(*p4d)) {
4176 mm_inc_nr_puds(mm);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004177 pgd_populate(mm, p4d, new);
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -08004178 } else /* Another has populated it */
4179 pud_free(mm, new);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004180#endif /* __ARCH_HAS_5LEVEL_HACK */
Hugh Dickinsc74df322005-10-29 18:16:23 -07004181 spin_unlock(&mm->page_table_lock);
Hugh Dickins1bb36302005-10-29 18:16:22 -07004182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183}
4184#endif /* __PAGETABLE_PUD_FOLDED */
4185
4186#ifndef __PAGETABLE_PMD_FOLDED
4187/*
4188 * Allocate page middle directory.
Hugh Dickins872fec12005-10-29 18:16:21 -07004189 * We've already handled the fast-path in-line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 */
Hugh Dickins1bb36302005-10-29 18:16:22 -07004191int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192{
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004193 spinlock_t *ptl;
Hugh Dickinsc74df322005-10-29 18:16:23 -07004194 pmd_t *new = pmd_alloc_one(mm, address);
4195 if (!new)
Hugh Dickins1bb36302005-10-29 18:16:22 -07004196 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
Nick Piggin362a61a2008-05-14 06:37:36 +02004198 smp_wmb(); /* See comment in __pte_alloc */
4199
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004200 ptl = pud_lock(mm, pud);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004201 if (!pud_present(*pud)) {
4202 mm_inc_nr_pmds(mm);
Hugh Dickins1bb36302005-10-29 18:16:22 -07004203 pud_populate(mm, pud, new);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004204 } else /* Another has populated it */
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -08004205 pmd_free(mm, new);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004206 spin_unlock(ptl);
Hugh Dickins1bb36302005-10-29 18:16:22 -07004207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208}
4209#endif /* __PAGETABLE_PMD_FOLDED */
4210
Ross Zwisler09796392017-01-10 16:57:21 -08004211static int __follow_pte_pmd(struct mm_struct *mm, unsigned long address,
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004212 struct mmu_notifier_range *range,
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004213 pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp)
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004214{
4215 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004216 p4d_t *p4d;
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004217 pud_t *pud;
4218 pmd_t *pmd;
4219 pte_t *ptep;
4220
4221 pgd = pgd_offset(mm, address);
4222 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
4223 goto out;
4224
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004225 p4d = p4d_offset(pgd, address);
4226 if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d)))
4227 goto out;
4228
4229 pud = pud_offset(p4d, address);
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004230 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
4231 goto out;
4232
4233 pmd = pmd_offset(pud, address);
Andrea Arcangelif66055ab2011-01-13 15:46:54 -08004234 VM_BUG_ON(pmd_trans_huge(*pmd));
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004235
Ross Zwisler09796392017-01-10 16:57:21 -08004236 if (pmd_huge(*pmd)) {
4237 if (!pmdpp)
4238 goto out;
4239
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004240 if (range) {
Jérôme Glisse7269f992019-05-13 17:20:53 -07004241 mmu_notifier_range_init(range, MMU_NOTIFY_CLEAR, 0,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07004242 NULL, mm, address & PMD_MASK,
4243 (address & PMD_MASK) + PMD_SIZE);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004244 mmu_notifier_invalidate_range_start(range);
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004245 }
Ross Zwisler09796392017-01-10 16:57:21 -08004246 *ptlp = pmd_lock(mm, pmd);
4247 if (pmd_huge(*pmd)) {
4248 *pmdpp = pmd;
4249 return 0;
4250 }
4251 spin_unlock(*ptlp);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004252 if (range)
4253 mmu_notifier_invalidate_range_end(range);
Ross Zwisler09796392017-01-10 16:57:21 -08004254 }
4255
4256 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004257 goto out;
4258
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004259 if (range) {
Jérôme Glisse7269f992019-05-13 17:20:53 -07004260 mmu_notifier_range_init(range, MMU_NOTIFY_CLEAR, 0, NULL, mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07004261 address & PAGE_MASK,
4262 (address & PAGE_MASK) + PAGE_SIZE);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004263 mmu_notifier_invalidate_range_start(range);
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004264 }
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004265 ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004266 if (!pte_present(*ptep))
4267 goto unlock;
4268 *ptepp = ptep;
4269 return 0;
4270unlock:
4271 pte_unmap_unlock(ptep, *ptlp);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004272 if (range)
4273 mmu_notifier_invalidate_range_end(range);
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004274out:
4275 return -EINVAL;
4276}
4277
Ross Zwislerf729c8c2017-01-10 16:57:24 -08004278static inline int follow_pte(struct mm_struct *mm, unsigned long address,
4279 pte_t **ptepp, spinlock_t **ptlp)
Namhyung Kim1b36ba82010-10-26 14:22:00 -07004280{
4281 int res;
4282
4283 /* (void) is needed to make gcc happy */
4284 (void) __cond_lock(*ptlp,
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004285 !(res = __follow_pte_pmd(mm, address, NULL,
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004286 ptepp, NULL, ptlp)));
Namhyung Kim1b36ba82010-10-26 14:22:00 -07004287 return res;
4288}
4289
Ross Zwisler09796392017-01-10 16:57:21 -08004290int follow_pte_pmd(struct mm_struct *mm, unsigned long address,
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004291 struct mmu_notifier_range *range,
4292 pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp)
Ross Zwisler09796392017-01-10 16:57:21 -08004293{
4294 int res;
4295
4296 /* (void) is needed to make gcc happy */
4297 (void) __cond_lock(*ptlp,
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004298 !(res = __follow_pte_pmd(mm, address, range,
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004299 ptepp, pmdpp, ptlp)));
Ross Zwisler09796392017-01-10 16:57:21 -08004300 return res;
4301}
4302EXPORT_SYMBOL(follow_pte_pmd);
4303
Johannes Weiner3b6748e2009-06-16 15:32:35 -07004304/**
4305 * follow_pfn - look up PFN at a user virtual address
4306 * @vma: memory mapping
4307 * @address: user virtual address
4308 * @pfn: location to store found PFN
4309 *
4310 * Only IO mappings and raw PFN mappings are allowed.
4311 *
Mike Rapoporta862f682019-03-05 15:48:42 -08004312 * Return: zero and the pfn at @pfn on success, -ve otherwise.
Johannes Weiner3b6748e2009-06-16 15:32:35 -07004313 */
4314int follow_pfn(struct vm_area_struct *vma, unsigned long address,
4315 unsigned long *pfn)
4316{
4317 int ret = -EINVAL;
4318 spinlock_t *ptl;
4319 pte_t *ptep;
4320
4321 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
4322 return ret;
4323
4324 ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
4325 if (ret)
4326 return ret;
4327 *pfn = pte_pfn(*ptep);
4328 pte_unmap_unlock(ptep, ptl);
4329 return 0;
4330}
4331EXPORT_SYMBOL(follow_pfn);
4332
Rik van Riel28b2ee22008-07-23 21:27:05 -07004333#ifdef CONFIG_HAVE_IOREMAP_PROT
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004334int follow_phys(struct vm_area_struct *vma,
4335 unsigned long address, unsigned int flags,
4336 unsigned long *prot, resource_size_t *phys)
Rik van Riel28b2ee22008-07-23 21:27:05 -07004337{
Johannes Weiner03668a42009-06-16 15:32:34 -07004338 int ret = -EINVAL;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004339 pte_t *ptep, pte;
4340 spinlock_t *ptl;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004341
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004342 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
4343 goto out;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004344
Johannes Weiner03668a42009-06-16 15:32:34 -07004345 if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004346 goto out;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004347 pte = *ptep;
Johannes Weiner03668a42009-06-16 15:32:34 -07004348
Linus Torvaldsf6f37322017-12-15 18:53:22 -08004349 if ((flags & FOLL_WRITE) && !pte_write(pte))
Rik van Riel28b2ee22008-07-23 21:27:05 -07004350 goto unlock;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004351
4352 *prot = pgprot_val(pte_pgprot(pte));
Johannes Weiner03668a42009-06-16 15:32:34 -07004353 *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004354
Johannes Weiner03668a42009-06-16 15:32:34 -07004355 ret = 0;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004356unlock:
4357 pte_unmap_unlock(ptep, ptl);
4358out:
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004359 return ret;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004360}
4361
4362int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
4363 void *buf, int len, int write)
4364{
4365 resource_size_t phys_addr;
4366 unsigned long prot = 0;
KOSAKI Motohiro2bc72732009-01-06 14:39:43 -08004367 void __iomem *maddr;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004368 int offset = addr & (PAGE_SIZE-1);
4369
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004370 if (follow_phys(vma, addr, write, &prot, &phys_addr))
Rik van Riel28b2ee22008-07-23 21:27:05 -07004371 return -EINVAL;
4372
Grazvydas Ignotas9cb12d72015-02-12 15:00:19 -08004373 maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot);
jie@chenjie6@huwei.com24eee1e2018-08-10 17:23:06 -07004374 if (!maddr)
4375 return -ENOMEM;
4376
Rik van Riel28b2ee22008-07-23 21:27:05 -07004377 if (write)
4378 memcpy_toio(maddr + offset, buf, len);
4379 else
4380 memcpy_fromio(buf, maddr + offset, len);
4381 iounmap(maddr);
4382
4383 return len;
4384}
Uwe Kleine-König5a736332013-08-07 13:02:52 +02004385EXPORT_SYMBOL_GPL(generic_access_phys);
Rik van Riel28b2ee22008-07-23 21:27:05 -07004386#endif
4387
David Howells0ec76a12006-09-27 01:50:15 -07004388/*
Stephen Wilson206cb632011-03-13 15:49:19 -04004389 * Access another process' address space as given in mm. If non-NULL, use the
4390 * given task for page fault accounting.
David Howells0ec76a12006-09-27 01:50:15 -07004391 */
Eric W. Biederman84d77d32016-11-22 12:06:50 -06004392int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
Lorenzo Stoakes442486e2016-10-13 01:20:18 +01004393 unsigned long addr, void *buf, int len, unsigned int gup_flags)
David Howells0ec76a12006-09-27 01:50:15 -07004394{
David Howells0ec76a12006-09-27 01:50:15 -07004395 struct vm_area_struct *vma;
David Howells0ec76a12006-09-27 01:50:15 -07004396 void *old_buf = buf;
Lorenzo Stoakes442486e2016-10-13 01:20:18 +01004397 int write = gup_flags & FOLL_WRITE;
David Howells0ec76a12006-09-27 01:50:15 -07004398
Konstantin Khlebnikov1e426fe2019-07-11 21:00:07 -07004399 if (down_read_killable(&mm->mmap_sem))
4400 return 0;
4401
Simon Arlott183ff222007-10-20 01:27:18 +02004402 /* ignore errors, just check how much was successfully transferred */
David Howells0ec76a12006-09-27 01:50:15 -07004403 while (len) {
4404 int bytes, ret, offset;
4405 void *maddr;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004406 struct page *page = NULL;
David Howells0ec76a12006-09-27 01:50:15 -07004407
Dave Hansen1e987792016-02-12 13:01:54 -08004408 ret = get_user_pages_remote(tsk, mm, addr, 1,
Lorenzo Stoakes5b56d492016-12-14 15:06:52 -08004409 gup_flags, &page, &vma, NULL);
Rik van Riel28b2ee22008-07-23 21:27:05 -07004410 if (ret <= 0) {
Rik van Rieldbffcd02014-08-06 16:08:12 -07004411#ifndef CONFIG_HAVE_IOREMAP_PROT
4412 break;
4413#else
Rik van Riel28b2ee22008-07-23 21:27:05 -07004414 /*
4415 * Check if this is a VM_IO | VM_PFNMAP VMA, which
4416 * we can access using slightly different code.
4417 */
Rik van Riel28b2ee22008-07-23 21:27:05 -07004418 vma = find_vma(mm, addr);
Michael Ellermanfe936df2011-04-14 15:22:10 -07004419 if (!vma || vma->vm_start > addr)
Rik van Riel28b2ee22008-07-23 21:27:05 -07004420 break;
4421 if (vma->vm_ops && vma->vm_ops->access)
4422 ret = vma->vm_ops->access(vma, addr, buf,
4423 len, write);
4424 if (ret <= 0)
Rik van Riel28b2ee22008-07-23 21:27:05 -07004425 break;
4426 bytes = ret;
Rik van Rieldbffcd02014-08-06 16:08:12 -07004427#endif
David Howells0ec76a12006-09-27 01:50:15 -07004428 } else {
Rik van Riel28b2ee22008-07-23 21:27:05 -07004429 bytes = len;
4430 offset = addr & (PAGE_SIZE-1);
4431 if (bytes > PAGE_SIZE-offset)
4432 bytes = PAGE_SIZE-offset;
4433
4434 maddr = kmap(page);
4435 if (write) {
4436 copy_to_user_page(vma, page, addr,
4437 maddr + offset, buf, bytes);
4438 set_page_dirty_lock(page);
4439 } else {
4440 copy_from_user_page(vma, page, addr,
4441 buf, maddr + offset, bytes);
4442 }
4443 kunmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004444 put_page(page);
David Howells0ec76a12006-09-27 01:50:15 -07004445 }
David Howells0ec76a12006-09-27 01:50:15 -07004446 len -= bytes;
4447 buf += bytes;
4448 addr += bytes;
4449 }
4450 up_read(&mm->mmap_sem);
David Howells0ec76a12006-09-27 01:50:15 -07004451
4452 return buf - old_buf;
4453}
Andi Kleen03252912008-01-30 13:33:18 +01004454
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04004455/**
Randy Dunlapae91dbf2011-03-26 13:27:01 -07004456 * access_remote_vm - access another process' address space
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04004457 * @mm: the mm_struct of the target address space
4458 * @addr: start address to access
4459 * @buf: source or destination buffer
4460 * @len: number of bytes to transfer
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01004461 * @gup_flags: flags modifying lookup behaviour
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04004462 *
4463 * The caller must hold a reference on @mm.
Mike Rapoporta862f682019-03-05 15:48:42 -08004464 *
4465 * Return: number of bytes copied from source to destination.
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04004466 */
4467int access_remote_vm(struct mm_struct *mm, unsigned long addr,
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01004468 void *buf, int len, unsigned int gup_flags)
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04004469{
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01004470 return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags);
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04004471}
4472
Andi Kleen03252912008-01-30 13:33:18 +01004473/*
Stephen Wilson206cb632011-03-13 15:49:19 -04004474 * Access another process' address space.
4475 * Source/target buffer must be kernel space,
4476 * Do not walk the page table directly, use get_user_pages
4477 */
4478int access_process_vm(struct task_struct *tsk, unsigned long addr,
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +01004479 void *buf, int len, unsigned int gup_flags)
Stephen Wilson206cb632011-03-13 15:49:19 -04004480{
4481 struct mm_struct *mm;
4482 int ret;
4483
4484 mm = get_task_mm(tsk);
4485 if (!mm)
4486 return 0;
4487
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +01004488 ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags);
Lorenzo Stoakes442486e2016-10-13 01:20:18 +01004489
Stephen Wilson206cb632011-03-13 15:49:19 -04004490 mmput(mm);
4491
4492 return ret;
4493}
Catalin Marinasfcd35852016-11-01 14:43:25 -07004494EXPORT_SYMBOL_GPL(access_process_vm);
Stephen Wilson206cb632011-03-13 15:49:19 -04004495
Andi Kleen03252912008-01-30 13:33:18 +01004496/*
4497 * Print the name of a VMA.
4498 */
4499void print_vma_addr(char *prefix, unsigned long ip)
4500{
4501 struct mm_struct *mm = current->mm;
4502 struct vm_area_struct *vma;
4503
Ingo Molnare8bff742008-02-13 20:21:06 +01004504 /*
Michal Hocko0a7f6822017-11-15 17:38:59 -08004505 * we might be running from an atomic context so we cannot sleep
Ingo Molnare8bff742008-02-13 20:21:06 +01004506 */
Michal Hocko0a7f6822017-11-15 17:38:59 -08004507 if (!down_read_trylock(&mm->mmap_sem))
Ingo Molnare8bff742008-02-13 20:21:06 +01004508 return;
4509
Andi Kleen03252912008-01-30 13:33:18 +01004510 vma = find_vma(mm, ip);
4511 if (vma && vma->vm_file) {
4512 struct file *f = vma->vm_file;
Michal Hocko0a7f6822017-11-15 17:38:59 -08004513 char *buf = (char *)__get_free_page(GFP_NOWAIT);
Andi Kleen03252912008-01-30 13:33:18 +01004514 if (buf) {
Andy Shevchenko2fbc57c2012-12-17 16:01:23 -08004515 char *p;
Andi Kleen03252912008-01-30 13:33:18 +01004516
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02004517 p = file_path(f, buf, PAGE_SIZE);
Andi Kleen03252912008-01-30 13:33:18 +01004518 if (IS_ERR(p))
4519 p = "?";
Andy Shevchenko2fbc57c2012-12-17 16:01:23 -08004520 printk("%s%s[%lx+%lx]", prefix, kbasename(p),
Andi Kleen03252912008-01-30 13:33:18 +01004521 vma->vm_start,
4522 vma->vm_end - vma->vm_start);
4523 free_page((unsigned long)buf);
4524 }
4525 }
Jeff Liu51a07e52012-07-31 16:43:18 -07004526 up_read(&mm->mmap_sem);
Andi Kleen03252912008-01-30 13:33:18 +01004527}
Nick Piggin3ee1afa2008-09-10 13:37:17 +02004528
Michael S. Tsirkin662bbcb2013-05-26 17:32:23 +03004529#if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
David Hildenbrand9ec23532015-05-11 17:52:07 +02004530void __might_fault(const char *file, int line)
Nick Piggin3ee1afa2008-09-10 13:37:17 +02004531{
Peter Zijlstra95156f02009-01-12 13:02:11 +01004532 /*
4533 * Some code (nfs/sunrpc) uses socket ops on kernel memory while
4534 * holding the mmap_sem, this is safe because kernel memory doesn't
4535 * get paged out, therefore we'll never actually fault, and the
4536 * below annotations will generate false positives.
4537 */
Al Virodb68ce12017-03-20 21:08:07 -04004538 if (uaccess_kernel())
Peter Zijlstra95156f02009-01-12 13:02:11 +01004539 return;
David Hildenbrand9ec23532015-05-11 17:52:07 +02004540 if (pagefault_disabled())
Michael S. Tsirkin662bbcb2013-05-26 17:32:23 +03004541 return;
David Hildenbrand9ec23532015-05-11 17:52:07 +02004542 __might_sleep(file, line, 0);
4543#if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
Michael S. Tsirkin662bbcb2013-05-26 17:32:23 +03004544 if (current->mm)
Nick Piggin3ee1afa2008-09-10 13:37:17 +02004545 might_lock_read(&current->mm->mmap_sem);
David Hildenbrand9ec23532015-05-11 17:52:07 +02004546#endif
Nick Piggin3ee1afa2008-09-10 13:37:17 +02004547}
David Hildenbrand9ec23532015-05-11 17:52:07 +02004548EXPORT_SYMBOL(__might_fault);
Nick Piggin3ee1afa2008-09-10 13:37:17 +02004549#endif
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004550
4551#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
Huang Yingc6ddfb62018-08-17 15:45:46 -07004552/*
4553 * Process all subpages of the specified huge page with the specified
4554 * operation. The target subpage will be processed last to keep its
4555 * cache lines hot.
4556 */
4557static inline void process_huge_page(
4558 unsigned long addr_hint, unsigned int pages_per_huge_page,
4559 void (*process_subpage)(unsigned long addr, int idx, void *arg),
4560 void *arg)
4561{
4562 int i, n, base, l;
4563 unsigned long addr = addr_hint &
4564 ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
4565
4566 /* Process target subpage last to keep its cache lines hot */
4567 might_sleep();
4568 n = (addr_hint - addr) / PAGE_SIZE;
4569 if (2 * n <= pages_per_huge_page) {
4570 /* If target subpage in first half of huge page */
4571 base = 0;
4572 l = n;
4573 /* Process subpages at the end of huge page */
4574 for (i = pages_per_huge_page - 1; i >= 2 * n; i--) {
4575 cond_resched();
4576 process_subpage(addr + i * PAGE_SIZE, i, arg);
4577 }
4578 } else {
4579 /* If target subpage in second half of huge page */
4580 base = pages_per_huge_page - 2 * (pages_per_huge_page - n);
4581 l = pages_per_huge_page - n;
4582 /* Process subpages at the begin of huge page */
4583 for (i = 0; i < base; i++) {
4584 cond_resched();
4585 process_subpage(addr + i * PAGE_SIZE, i, arg);
4586 }
4587 }
4588 /*
4589 * Process remaining subpages in left-right-left-right pattern
4590 * towards the target subpage
4591 */
4592 for (i = 0; i < l; i++) {
4593 int left_idx = base + i;
4594 int right_idx = base + 2 * l - 1 - i;
4595
4596 cond_resched();
4597 process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg);
4598 cond_resched();
4599 process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg);
4600 }
4601}
4602
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004603static void clear_gigantic_page(struct page *page,
4604 unsigned long addr,
4605 unsigned int pages_per_huge_page)
4606{
4607 int i;
4608 struct page *p = page;
4609
4610 might_sleep();
4611 for (i = 0; i < pages_per_huge_page;
4612 i++, p = mem_map_next(p, page, i)) {
4613 cond_resched();
4614 clear_user_highpage(p, addr + i * PAGE_SIZE);
4615 }
4616}
Huang Yingc6ddfb62018-08-17 15:45:46 -07004617
4618static void clear_subpage(unsigned long addr, int idx, void *arg)
4619{
4620 struct page *page = arg;
4621
4622 clear_user_highpage(page + idx, addr);
4623}
4624
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004625void clear_huge_page(struct page *page,
Huang Yingc79b57e2017-09-06 16:25:04 -07004626 unsigned long addr_hint, unsigned int pages_per_huge_page)
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004627{
Huang Yingc79b57e2017-09-06 16:25:04 -07004628 unsigned long addr = addr_hint &
4629 ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004630
4631 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
4632 clear_gigantic_page(page, addr, pages_per_huge_page);
4633 return;
4634 }
4635
Huang Yingc6ddfb62018-08-17 15:45:46 -07004636 process_huge_page(addr_hint, pages_per_huge_page, clear_subpage, page);
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004637}
4638
4639static void copy_user_gigantic_page(struct page *dst, struct page *src,
4640 unsigned long addr,
4641 struct vm_area_struct *vma,
4642 unsigned int pages_per_huge_page)
4643{
4644 int i;
4645 struct page *dst_base = dst;
4646 struct page *src_base = src;
4647
4648 for (i = 0; i < pages_per_huge_page; ) {
4649 cond_resched();
4650 copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
4651
4652 i++;
4653 dst = mem_map_next(dst, dst_base, i);
4654 src = mem_map_next(src, src_base, i);
4655 }
4656}
4657
Huang Yingc9f4cd72018-08-17 15:45:49 -07004658struct copy_subpage_arg {
4659 struct page *dst;
4660 struct page *src;
4661 struct vm_area_struct *vma;
4662};
4663
4664static void copy_subpage(unsigned long addr, int idx, void *arg)
4665{
4666 struct copy_subpage_arg *copy_arg = arg;
4667
4668 copy_user_highpage(copy_arg->dst + idx, copy_arg->src + idx,
4669 addr, copy_arg->vma);
4670}
4671
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004672void copy_user_huge_page(struct page *dst, struct page *src,
Huang Yingc9f4cd72018-08-17 15:45:49 -07004673 unsigned long addr_hint, struct vm_area_struct *vma,
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004674 unsigned int pages_per_huge_page)
4675{
Huang Yingc9f4cd72018-08-17 15:45:49 -07004676 unsigned long addr = addr_hint &
4677 ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
4678 struct copy_subpage_arg arg = {
4679 .dst = dst,
4680 .src = src,
4681 .vma = vma,
4682 };
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004683
4684 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
4685 copy_user_gigantic_page(dst, src, addr, vma,
4686 pages_per_huge_page);
4687 return;
4688 }
4689
Huang Yingc9f4cd72018-08-17 15:45:49 -07004690 process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg);
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004691}
Mike Kravetzfa4d75c2017-02-22 15:42:49 -08004692
4693long copy_huge_page_from_user(struct page *dst_page,
4694 const void __user *usr_src,
Mike Kravetz810a56b2017-02-22 15:42:58 -08004695 unsigned int pages_per_huge_page,
4696 bool allow_pagefault)
Mike Kravetzfa4d75c2017-02-22 15:42:49 -08004697{
4698 void *src = (void *)usr_src;
4699 void *page_kaddr;
4700 unsigned long i, rc = 0;
4701 unsigned long ret_val = pages_per_huge_page * PAGE_SIZE;
4702
4703 for (i = 0; i < pages_per_huge_page; i++) {
Mike Kravetz810a56b2017-02-22 15:42:58 -08004704 if (allow_pagefault)
4705 page_kaddr = kmap(dst_page + i);
4706 else
4707 page_kaddr = kmap_atomic(dst_page + i);
Mike Kravetzfa4d75c2017-02-22 15:42:49 -08004708 rc = copy_from_user(page_kaddr,
4709 (const void __user *)(src + i * PAGE_SIZE),
4710 PAGE_SIZE);
Mike Kravetz810a56b2017-02-22 15:42:58 -08004711 if (allow_pagefault)
4712 kunmap(dst_page + i);
4713 else
4714 kunmap_atomic(page_kaddr);
Mike Kravetzfa4d75c2017-02-22 15:42:49 -08004715
4716 ret_val -= (PAGE_SIZE - rc);
4717 if (rc)
4718 break;
4719
4720 cond_resched();
4721 }
4722 return ret_val;
4723}
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08004724#endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08004725
Olof Johansson40b64ac2013-12-20 14:28:05 -08004726#if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08004727
4728static struct kmem_cache *page_ptl_cachep;
4729
4730void __init ptlock_cache_init(void)
4731{
4732 page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0,
4733 SLAB_PANIC, NULL);
4734}
4735
Peter Zijlstra539edb52013-11-14 14:31:52 -08004736bool ptlock_alloc(struct page *page)
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08004737{
4738 spinlock_t *ptl;
4739
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08004740 ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL);
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08004741 if (!ptl)
4742 return false;
Peter Zijlstra539edb52013-11-14 14:31:52 -08004743 page->ptl = ptl;
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08004744 return true;
4745}
4746
Peter Zijlstra539edb52013-11-14 14:31:52 -08004747void ptlock_free(struct page *page)
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08004748{
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08004749 kmem_cache_free(page_ptl_cachep, page->ptl);
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08004750}
4751#endif