blob: 48c4576df89812675e7715cddb3cec70e0102b66 [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>
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -070068#include <linux/debugfs.h>
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -070069#include <linux/userfaultfd_k.h>
Jan Karabc2466e2016-05-12 18:29:19 +020070#include <linux/dax.h>
Michal Hocko6b31d592017-08-18 15:16:15 -070071#include <linux/oom.h>
Anshuman Khandual98fa15f2019-03-05 15:42:58 -080072#include <linux/numa.h>
Peter Xubce617e2020-08-11 18:37:44 -070073#include <linux/perf_event.h>
74#include <linux/ptrace.h>
Joerg Roedele80d3902020-09-04 16:35:43 -070075#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Joel Fernandes (Google)b3d14112019-11-30 17:50:30 -080077#include <trace/events/kmem.h>
78
Alexey Dobriyan6952b612009-09-18 23:55:55 +040079#include <asm/io.h>
Dave Hansen33a709b2016-02-12 13:02:19 -080080#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <asm/pgalloc.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080082#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <asm/tlb.h>
84#include <asm/tlbflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Joerg Roedele80d3902020-09-04 16:35:43 -070086#include "pgalloc-track.h"
Jan Beulich42b77722008-07-23 21:27:10 -070087#include "internal.h"
88
Arnd Bergmannaf27d942018-02-16 16:25:53 +010089#if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST)
Peter Zijlstra90572892013-10-07 11:29:20 +010090#warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
Peter Zijlstra75980e92013-02-22 16:34:32 -080091#endif
92
Mike Rapoporta9ee6cf2021-06-28 19:43:01 -070093#ifndef CONFIG_NUMA
Linus Torvalds1da177e2005-04-16 15:20:36 -070094unsigned long max_mapnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095EXPORT_SYMBOL(max_mapnr);
Tobin C Harding166f61b2017-02-24 14:59:01 -080096
97struct page *mem_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098EXPORT_SYMBOL(mem_map);
99#endif
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/*
102 * A number of key systems in x86 including ioremap() rely on the assumption
103 * that high_memory defines the upper bound on direct map memory, then end
104 * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
105 * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
106 * and ZONE_HIGHMEM.
107 */
Tobin C Harding166f61b2017-02-24 14:59:01 -0800108void *high_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109EXPORT_SYMBOL(high_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Ingo Molnar32a93232008-02-06 22:39:44 +0100111/*
112 * Randomize the address space (stacks, mmaps, brk, etc.).
113 *
114 * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
115 * as ancient (libc5 based) binaries can segfault. )
116 */
117int randomize_va_space __read_mostly =
118#ifdef CONFIG_COMPAT_BRK
119 1;
120#else
121 2;
122#endif
Andi Kleena62eaf12006-02-16 23:41:58 +0100123
Jia He83d116c2019-10-11 22:09:39 +0800124#ifndef arch_faults_on_old_pte
125static inline bool arch_faults_on_old_pte(void)
126{
127 /*
128 * Those arches which don't have hw access flag feature need to
129 * implement their own helper. By default, "true" means pagefault
130 * will be hit on old pte.
131 */
132 return true;
133}
134#endif
135
Will Deacon46bdb422020-11-24 18:48:26 +0000136#ifndef arch_wants_old_prefaulted_pte
137static inline bool arch_wants_old_prefaulted_pte(void)
138{
139 /*
140 * Transitioning a PTE from 'old' to 'young' can be expensive on
141 * some architectures, even if it's performed in hardware. By
142 * default, "false" means prefaulted entries will be 'young'.
143 */
144 return false;
145}
146#endif
147
Andi Kleena62eaf12006-02-16 23:41:58 +0100148static int __init disable_randmaps(char *s)
149{
150 randomize_va_space = 0;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800151 return 1;
Andi Kleena62eaf12006-02-16 23:41:58 +0100152}
153__setup("norandmaps", disable_randmaps);
154
Hugh Dickins62eede62009-09-21 17:03:34 -0700155unsigned long zero_pfn __read_mostly;
Ard Biesheuvel0b700682014-09-12 22:17:23 +0200156EXPORT_SYMBOL(zero_pfn);
157
Tobin C Harding166f61b2017-02-24 14:59:01 -0800158unsigned long highest_memmap_pfn __read_mostly;
159
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -0700160/*
161 * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
162 */
163static int __init init_zero_pfn(void)
164{
165 zero_pfn = page_to_pfn(ZERO_PAGE(0));
166 return 0;
167}
Ilya Lipnitskiye720e7d2021-03-29 21:42:08 -0700168early_initcall(init_zero_pfn);
Andi Kleena62eaf12006-02-16 23:41:58 +0100169
Joel Fernandes (Google)e4dcad22019-11-30 17:50:33 -0800170void mm_trace_rss_stat(struct mm_struct *mm, int member, long count)
Joel Fernandes (Google)b3d14112019-11-30 17:50:30 -0800171{
Joel Fernandes (Google)e4dcad22019-11-30 17:50:33 -0800172 trace_rss_stat(mm, member, count);
Joel Fernandes (Google)b3d14112019-11-30 17:50:30 -0800173}
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800174
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800175#if defined(SPLIT_RSS_COUNTING)
176
David Rientjesea48cf72012-03-21 16:34:13 -0700177void sync_mm_rss(struct mm_struct *mm)
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800178{
179 int i;
180
181 for (i = 0; i < NR_MM_COUNTERS; i++) {
David Rientjes05af2e12012-03-21 16:34:13 -0700182 if (current->rss_stat.count[i]) {
183 add_mm_counter(mm, i, current->rss_stat.count[i]);
184 current->rss_stat.count[i] = 0;
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800185 }
186 }
David Rientjes05af2e12012-03-21 16:34:13 -0700187 current->rss_stat.events = 0;
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800188}
189
190static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
191{
192 struct task_struct *task = current;
193
194 if (likely(task->mm == mm))
195 task->rss_stat.count[member] += val;
196 else
197 add_mm_counter(mm, member, val);
198}
199#define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
200#define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
201
202/* sync counter once per 64 page faults */
203#define TASK_RSS_EVENTS_THRESH (64)
204static void check_sync_rss_stat(struct task_struct *task)
205{
206 if (unlikely(task != current))
207 return;
208 if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
David Rientjesea48cf72012-03-21 16:34:13 -0700209 sync_mm_rss(task->mm);
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800210}
Peter Zijlstra9547d012011-05-24 17:12:14 -0700211#else /* SPLIT_RSS_COUNTING */
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800212
213#define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
214#define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
215
216static void check_sync_rss_stat(struct task_struct *task)
217{
218}
219
Peter Zijlstra9547d012011-05-24 17:12:14 -0700220#endif /* SPLIT_RSS_COUNTING */
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * Note: this doesn't free the actual pages themselves. That
224 * has been handled earlier when unmapping all the memory regions.
225 */
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000226static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
227 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800229 pgtable_t token = pmd_pgtable(*pmd);
Hugh Dickinse0da3822005-04-19 13:29:15 -0700230 pmd_clear(pmd);
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000231 pte_free_tlb(tlb, token, addr);
Kirill A. Shutemovc4812902017-11-15 17:35:37 -0800232 mm_dec_nr_ptes(tlb->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Hugh Dickinse0da3822005-04-19 13:29:15 -0700235static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
236 unsigned long addr, unsigned long end,
237 unsigned long floor, unsigned long ceiling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 pmd_t *pmd;
240 unsigned long next;
Hugh Dickinse0da3822005-04-19 13:29:15 -0700241 unsigned long start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Hugh Dickinse0da3822005-04-19 13:29:15 -0700243 start = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 pmd = pmd_offset(pud, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 do {
246 next = pmd_addr_end(addr, end);
247 if (pmd_none_or_clear_bad(pmd))
248 continue;
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000249 free_pte_range(tlb, pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 } while (pmd++, addr = next, addr != end);
251
Hugh Dickinse0da3822005-04-19 13:29:15 -0700252 start &= PUD_MASK;
253 if (start < floor)
254 return;
255 if (ceiling) {
256 ceiling &= PUD_MASK;
257 if (!ceiling)
258 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
Hugh Dickinse0da3822005-04-19 13:29:15 -0700260 if (end - 1 > ceiling - 1)
261 return;
262
263 pmd = pmd_offset(pud, start);
264 pud_clear(pud);
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000265 pmd_free_tlb(tlb, pmd, start);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800266 mm_dec_nr_pmds(tlb->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300269static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d,
Hugh Dickinse0da3822005-04-19 13:29:15 -0700270 unsigned long addr, unsigned long end,
271 unsigned long floor, unsigned long ceiling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 pud_t *pud;
274 unsigned long next;
Hugh Dickinse0da3822005-04-19 13:29:15 -0700275 unsigned long start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Hugh Dickinse0da3822005-04-19 13:29:15 -0700277 start = addr;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300278 pud = pud_offset(p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 do {
280 next = pud_addr_end(addr, end);
281 if (pud_none_or_clear_bad(pud))
282 continue;
Hugh Dickinse0da3822005-04-19 13:29:15 -0700283 free_pmd_range(tlb, pud, addr, next, floor, ceiling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 } while (pud++, addr = next, addr != end);
285
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300286 start &= P4D_MASK;
287 if (start < floor)
288 return;
289 if (ceiling) {
290 ceiling &= P4D_MASK;
291 if (!ceiling)
292 return;
293 }
294 if (end - 1 > ceiling - 1)
295 return;
296
297 pud = pud_offset(p4d, start);
298 p4d_clear(p4d);
299 pud_free_tlb(tlb, pud, start);
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -0800300 mm_dec_nr_puds(tlb->mm);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300301}
302
303static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd,
304 unsigned long addr, unsigned long end,
305 unsigned long floor, unsigned long ceiling)
306{
307 p4d_t *p4d;
308 unsigned long next;
309 unsigned long start;
310
311 start = addr;
312 p4d = p4d_offset(pgd, addr);
313 do {
314 next = p4d_addr_end(addr, end);
315 if (p4d_none_or_clear_bad(p4d))
316 continue;
317 free_pud_range(tlb, p4d, addr, next, floor, ceiling);
318 } while (p4d++, addr = next, addr != end);
319
Hugh Dickinse0da3822005-04-19 13:29:15 -0700320 start &= PGDIR_MASK;
321 if (start < floor)
322 return;
323 if (ceiling) {
324 ceiling &= PGDIR_MASK;
325 if (!ceiling)
326 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
Hugh Dickinse0da3822005-04-19 13:29:15 -0700328 if (end - 1 > ceiling - 1)
329 return;
330
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300331 p4d = p4d_offset(pgd, start);
Hugh Dickinse0da3822005-04-19 13:29:15 -0700332 pgd_clear(pgd);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300333 p4d_free_tlb(tlb, p4d, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
336/*
Hugh Dickinse0da3822005-04-19 13:29:15 -0700337 * This function frees user-level page tables of a process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
Jan Beulich42b77722008-07-23 21:27:10 -0700339void free_pgd_range(struct mmu_gather *tlb,
Hugh Dickinse0da3822005-04-19 13:29:15 -0700340 unsigned long addr, unsigned long end,
341 unsigned long floor, unsigned long ceiling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 pgd_t *pgd;
344 unsigned long next;
345
Hugh Dickinse0da3822005-04-19 13:29:15 -0700346 /*
347 * The next few lines have given us lots of grief...
348 *
349 * Why are we testing PMD* at this top level? Because often
350 * there will be no work to do at all, and we'd prefer not to
351 * go all the way down to the bottom just to discover that.
352 *
353 * Why all these "- 1"s? Because 0 represents both the bottom
354 * of the address space and the top of it (using -1 for the
355 * top wouldn't help much: the masks would do the wrong thing).
356 * The rule is that addr 0 and floor 0 refer to the bottom of
357 * the address space, but end 0 and ceiling 0 refer to the top
358 * Comparisons need to use "end - 1" and "ceiling - 1" (though
359 * that end 0 case should be mythical).
360 *
361 * Wherever addr is brought up or ceiling brought down, we must
362 * be careful to reject "the opposite 0" before it confuses the
363 * subsequent tests. But what about where end is brought down
364 * by PMD_SIZE below? no, end can't go down to 0 there.
365 *
366 * Whereas we round start (addr) and ceiling down, by different
367 * masks at different levels, in order to test whether a table
368 * now has no other vmas using it, so can be freed, we don't
369 * bother to round floor or end up - the tests don't need that.
370 */
371
372 addr &= PMD_MASK;
373 if (addr < floor) {
374 addr += PMD_SIZE;
375 if (!addr)
376 return;
377 }
378 if (ceiling) {
379 ceiling &= PMD_MASK;
380 if (!ceiling)
381 return;
382 }
383 if (end - 1 > ceiling - 1)
384 end -= PMD_SIZE;
385 if (addr > end - 1)
386 return;
Aneesh Kumar K.V07e32662016-12-12 16:42:40 -0800387 /*
388 * We add page table cache pages with PAGE_SIZE,
389 * (see pte_free_tlb()), flush the tlb if we need
390 */
Peter Zijlstraed6a7932018-08-31 14:46:08 +0200391 tlb_change_page_size(tlb, PAGE_SIZE);
Jan Beulich42b77722008-07-23 21:27:10 -0700392 pgd = pgd_offset(tlb->mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 do {
394 next = pgd_addr_end(addr, end);
395 if (pgd_none_or_clear_bad(pgd))
396 continue;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300397 free_p4d_range(tlb, pgd, addr, next, floor, ceiling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 } while (pgd++, addr = next, addr != end);
Hugh Dickinse0da3822005-04-19 13:29:15 -0700399}
400
Jan Beulich42b77722008-07-23 21:27:10 -0700401void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700402 unsigned long floor, unsigned long ceiling)
Hugh Dickinse0da3822005-04-19 13:29:15 -0700403{
404 while (vma) {
405 struct vm_area_struct *next = vma->vm_next;
406 unsigned long addr = vma->vm_start;
407
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700408 /*
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +1000409 * Hide vma from rmap and truncate_pagecache before freeing
410 * pgtables
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700411 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800412 unlink_anon_vmas(vma);
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700413 unlink_file_vma(vma);
414
David Gibson9da61ae2006-03-22 00:08:57 -0800415 if (is_vm_hugetlb_page(vma)) {
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700416 hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
Tobin C Harding166f61b2017-02-24 14:59:01 -0800417 floor, next ? next->vm_start : ceiling);
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700418 } else {
419 /*
420 * Optimization: gather nearby vmas into one call down
421 */
422 while (next && next->vm_start <= vma->vm_end + PMD_SIZE
David Gibson48669202006-03-22 00:08:58 -0800423 && !is_vm_hugetlb_page(next)) {
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700424 vma = next;
425 next = vma->vm_next;
Rik van Riel5beb4932010-03-05 13:42:07 -0800426 unlink_anon_vmas(vma);
Hugh Dickins8f4f8c12005-10-29 18:16:29 -0700427 unlink_file_vma(vma);
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700428 }
429 free_pgd_range(tlb, addr, vma->vm_end,
Tobin C Harding166f61b2017-02-24 14:59:01 -0800430 floor, next ? next->vm_start : ceiling);
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700431 }
Hugh Dickinse0da3822005-04-19 13:29:15 -0700432 vma = next;
433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -0800436int __pte_alloc(struct mm_struct *mm, pmd_t *pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800438 spinlock_t *ptl;
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -0800439 pgtable_t new = pte_alloc_one(mm);
Hugh Dickins1bb36302005-10-29 18:16:22 -0700440 if (!new)
441 return -ENOMEM;
442
Nick Piggin362a61a2008-05-14 06:37:36 +0200443 /*
444 * Ensure all pte setup (eg. pte page lock and page clearing) are
445 * visible before the pte is made visible to other CPUs by being
446 * put into page tables.
447 *
448 * The other side of the story is the pointer chasing in the page
449 * table walking code (when walking the page table without locking;
450 * ie. most of the time). Fortunately, these data accesses consist
451 * of a chain of data-dependent loads, meaning most CPUs (alpha
452 * being the notable exception) will already guarantee loads are
453 * seen in-order. See the alpha page table accessors for the
Will Deaconbb7cdd32019-10-30 17:15:01 +0000454 * smp_rmb() barriers in page table walking code.
Nick Piggin362a61a2008-05-14 06:37:36 +0200455 */
456 smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
457
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800458 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli8ac1f832011-01-13 15:46:43 -0800459 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
Kirill A. Shutemovc4812902017-11-15 17:35:37 -0800460 mm_inc_nr_ptes(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 pmd_populate(mm, pmd, new);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800462 new = NULL;
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -0800463 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800464 spin_unlock(ptl);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800465 if (new)
466 pte_free(mm, new);
Hugh Dickins1bb36302005-10-29 18:16:22 -0700467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -0800470int __pte_alloc_kernel(pmd_t *pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -0800472 pte_t *new = pte_alloc_one_kernel(&init_mm);
Hugh Dickins1bb36302005-10-29 18:16:22 -0700473 if (!new)
474 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Nick Piggin362a61a2008-05-14 06:37:36 +0200476 smp_wmb(); /* See comment in __pte_alloc */
477
Hugh Dickins1bb36302005-10-29 18:16:22 -0700478 spin_lock(&init_mm.page_table_lock);
Andrea Arcangeli8ac1f832011-01-13 15:46:43 -0800479 if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
Hugh Dickins1bb36302005-10-29 18:16:22 -0700480 pmd_populate_kernel(&init_mm, pmd, new);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800481 new = NULL;
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -0800482 }
Hugh Dickins1bb36302005-10-29 18:16:22 -0700483 spin_unlock(&init_mm.page_table_lock);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800484 if (new)
485 pte_free_kernel(&init_mm, new);
Hugh Dickins1bb36302005-10-29 18:16:22 -0700486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800489static inline void init_rss_vec(int *rss)
Hugh Dickinsae859762005-10-29 18:16:05 -0700490{
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800491 memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
492}
493
494static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
495{
496 int i;
497
KAMEZAWA Hiroyuki34e55232010-03-05 13:41:40 -0800498 if (current->mm == mm)
David Rientjes05af2e12012-03-21 16:34:13 -0700499 sync_mm_rss(mm);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800500 for (i = 0; i < NR_MM_COUNTERS; i++)
501 if (rss[i])
502 add_mm_counter(mm, i, rss[i]);
Hugh Dickinsae859762005-10-29 18:16:05 -0700503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505/*
Linus Torvalds6aab3412005-11-28 14:34:23 -0800506 * This function is called to print an error when a bad pte
507 * is found. For example, we might have a PFN-mapped pte in
508 * a region that doesn't allow it.
Nick Pigginb5810032005-10-29 18:16:12 -0700509 *
510 * The calling function must still handle the error.
511 */
Hugh Dickins3dc14742009-01-06 14:40:08 -0800512static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
513 pte_t pte, struct page *page)
Nick Pigginb5810032005-10-29 18:16:12 -0700514{
Hugh Dickins3dc14742009-01-06 14:40:08 -0800515 pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300516 p4d_t *p4d = p4d_offset(pgd, addr);
517 pud_t *pud = pud_offset(p4d, addr);
Hugh Dickins3dc14742009-01-06 14:40:08 -0800518 pmd_t *pmd = pmd_offset(pud, addr);
519 struct address_space *mapping;
520 pgoff_t index;
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800521 static unsigned long resume;
522 static unsigned long nr_shown;
523 static unsigned long nr_unshown;
524
525 /*
526 * Allow a burst of 60 reports, then keep quiet for that minute;
527 * or allow a steady drip of one report per second.
528 */
529 if (nr_shown == 60) {
530 if (time_before(jiffies, resume)) {
531 nr_unshown++;
532 return;
533 }
534 if (nr_unshown) {
Joe Perches11705322016-03-17 14:19:50 -0700535 pr_alert("BUG: Bad page map: %lu messages suppressed\n",
536 nr_unshown);
Hugh Dickinsd936cf92009-01-06 14:40:12 -0800537 nr_unshown = 0;
538 }
539 nr_shown = 0;
540 }
541 if (nr_shown++ == 0)
542 resume = jiffies + 60 * HZ;
Hugh Dickins3dc14742009-01-06 14:40:08 -0800543
544 mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
545 index = linear_page_index(vma, addr);
546
Joe Perches11705322016-03-17 14:19:50 -0700547 pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n",
548 current->comm,
549 (long long)pte_val(pte), (long long)pmd_val(*pmd));
Wu Fengguang718a3822010-03-10 15:20:43 -0800550 if (page)
Dave Hansenf0b791a2014-01-23 15:52:49 -0800551 dump_page(page, "bad pte");
Kefeng Wang6aa9b8b2019-09-23 15:35:34 -0700552 pr_alert("addr:%px vm_flags:%08lx anon_vma:%px mapping:%px index:%lx\n",
Joe Perches11705322016-03-17 14:19:50 -0700553 (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
Sakari Ailusd75f7732019-03-25 21:32:28 +0200554 pr_alert("file:%pD fault:%ps mmap:%ps readpage:%ps\n",
Konstantin Khlebnikov26825822015-04-15 16:15:08 -0700555 vma->vm_file,
556 vma->vm_ops ? vma->vm_ops->fault : NULL,
557 vma->vm_file ? vma->vm_file->f_op->mmap : NULL,
558 mapping ? mapping->a_ops->readpage : NULL);
Nick Pigginb5810032005-10-29 18:16:12 -0700559 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +1030560 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Nick Pigginb5810032005-10-29 18:16:12 -0700561}
562
563/*
Nick Piggin7e675132008-04-28 02:13:00 -0700564 * vm_normal_page -- This function gets the "struct page" associated with a pte.
Linus Torvalds6aab3412005-11-28 14:34:23 -0800565 *
Nick Piggin7e675132008-04-28 02:13:00 -0700566 * "Special" mappings do not wish to be associated with a "struct page" (either
567 * it doesn't exist, or it exists but they don't want to touch it). In this
568 * case, NULL is returned here. "Normal" mappings do have a struct page.
Jared Hulbertb379d792008-04-28 02:12:58 -0700569 *
Nick Piggin7e675132008-04-28 02:13:00 -0700570 * There are 2 broad cases. Firstly, an architecture may define a pte_special()
571 * pte bit, in which case this function is trivial. Secondly, an architecture
572 * may not have a spare pte bit, which requires a more complicated scheme,
573 * described below.
574 *
575 * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
576 * special mapping (even if there are underlying and valid "struct pages").
577 * COWed pages of a VM_PFNMAP are always normal.
Linus Torvalds6aab3412005-11-28 14:34:23 -0800578 *
Jared Hulbertb379d792008-04-28 02:12:58 -0700579 * The way we recognize COWed pages within VM_PFNMAP mappings is through the
580 * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
Nick Piggin7e675132008-04-28 02:13:00 -0700581 * set, and the vm_pgoff will point to the first PFN mapped: thus every special
582 * mapping will always honor the rule
Linus Torvalds6aab3412005-11-28 14:34:23 -0800583 *
584 * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
585 *
Nick Piggin7e675132008-04-28 02:13:00 -0700586 * And for normal mappings this is false.
Jared Hulbertb379d792008-04-28 02:12:58 -0700587 *
Nick Piggin7e675132008-04-28 02:13:00 -0700588 * This restricts such mappings to be a linear translation from virtual address
589 * to pfn. To get around this restriction, we allow arbitrary mappings so long
590 * as the vma is not a COW mapping; in that case, we know that all ptes are
591 * special (because none can have been COWed).
Jared Hulbertb379d792008-04-28 02:12:58 -0700592 *
593 *
Nick Piggin7e675132008-04-28 02:13:00 -0700594 * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
595 *
Jared Hulbertb379d792008-04-28 02:12:58 -0700596 * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
597 * page" backing, however the difference is that _all_ pages with a struct
598 * page (that is, those where pfn_valid is true) are refcounted and considered
599 * normal pages by the VM. The disadvantage is that pages are refcounted
600 * (which can be slower and simply not an option for some PFNMAP users). The
601 * advantage is that we don't have to follow the strict linearity rule of
602 * PFNMAP mappings in order to support COWable mappings.
603 *
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800604 */
Christoph Hellwig25b29952019-06-13 22:50:49 +0200605struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
606 pte_t pte)
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800607{
Hugh Dickins22b31ee2009-01-06 14:40:09 -0800608 unsigned long pfn = pte_pfn(pte);
Nick Piggin7e675132008-04-28 02:13:00 -0700609
Laurent Dufour00b3a332018-06-07 17:06:12 -0700610 if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) {
Hugh Dickinsb38af472014-08-29 15:18:44 -0700611 if (likely(!pte_special(pte)))
Hugh Dickins22b31ee2009-01-06 14:40:09 -0800612 goto check_pfn;
David Vrabel667a0a02014-12-18 14:48:15 +0000613 if (vma->vm_ops && vma->vm_ops->find_special_page)
614 return vma->vm_ops->find_special_page(vma, addr);
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -0700615 if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
616 return NULL;
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700617 if (is_zero_pfn(pfn))
618 return NULL;
Dave Jiange1fb4a02018-08-17 15:43:40 -0700619 if (pte_devmap(pte))
620 return NULL;
621
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700622 print_bad_pte(vma, addr, pte, NULL);
Nick Piggin7e675132008-04-28 02:13:00 -0700623 return NULL;
624 }
625
Laurent Dufour00b3a332018-06-07 17:06:12 -0700626 /* !CONFIG_ARCH_HAS_PTE_SPECIAL case follows: */
Nick Piggin7e675132008-04-28 02:13:00 -0700627
Jared Hulbertb379d792008-04-28 02:12:58 -0700628 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
629 if (vma->vm_flags & VM_MIXEDMAP) {
630 if (!pfn_valid(pfn))
631 return NULL;
632 goto out;
633 } else {
Nick Piggin7e675132008-04-28 02:13:00 -0700634 unsigned long off;
635 off = (addr - vma->vm_start) >> PAGE_SHIFT;
Jared Hulbertb379d792008-04-28 02:12:58 -0700636 if (pfn == vma->vm_pgoff + off)
637 return NULL;
638 if (!is_cow_mapping(vma->vm_flags))
639 return NULL;
640 }
Linus Torvalds6aab3412005-11-28 14:34:23 -0800641 }
642
Hugh Dickinsb38af472014-08-29 15:18:44 -0700643 if (is_zero_pfn(pfn))
644 return NULL;
Laurent Dufour00b3a332018-06-07 17:06:12 -0700645
Hugh Dickins22b31ee2009-01-06 14:40:09 -0800646check_pfn:
647 if (unlikely(pfn > highest_memmap_pfn)) {
648 print_bad_pte(vma, addr, pte, NULL);
649 return NULL;
650 }
Linus Torvalds6aab3412005-11-28 14:34:23 -0800651
652 /*
Nick Piggin7e675132008-04-28 02:13:00 -0700653 * NOTE! We still have PageReserved() pages in the page tables.
Nick Piggin7e675132008-04-28 02:13:00 -0700654 * eg. VDSO mappings can cause them to exist.
Linus Torvalds6aab3412005-11-28 14:34:23 -0800655 */
Jared Hulbertb379d792008-04-28 02:12:58 -0700656out:
Linus Torvalds6aab3412005-11-28 14:34:23 -0800657 return pfn_to_page(pfn);
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800658}
659
Gerald Schaefer28093f92016-04-28 16:18:35 -0700660#ifdef CONFIG_TRANSPARENT_HUGEPAGE
661struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
662 pmd_t pmd)
663{
664 unsigned long pfn = pmd_pfn(pmd);
665
666 /*
667 * There is no pmd_special() but there may be special pmds, e.g.
668 * in a direct-access (dax) mapping, so let's just replicate the
Laurent Dufour00b3a332018-06-07 17:06:12 -0700669 * !CONFIG_ARCH_HAS_PTE_SPECIAL case from vm_normal_page() here.
Gerald Schaefer28093f92016-04-28 16:18:35 -0700670 */
671 if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
672 if (vma->vm_flags & VM_MIXEDMAP) {
673 if (!pfn_valid(pfn))
674 return NULL;
675 goto out;
676 } else {
677 unsigned long off;
678 off = (addr - vma->vm_start) >> PAGE_SHIFT;
679 if (pfn == vma->vm_pgoff + off)
680 return NULL;
681 if (!is_cow_mapping(vma->vm_flags))
682 return NULL;
683 }
684 }
685
Dave Jiange1fb4a02018-08-17 15:43:40 -0700686 if (pmd_devmap(pmd))
687 return NULL;
Yu Zhao3cde2872019-12-04 16:49:56 -0800688 if (is_huge_zero_pmd(pmd))
Gerald Schaefer28093f92016-04-28 16:18:35 -0700689 return NULL;
690 if (unlikely(pfn > highest_memmap_pfn))
691 return NULL;
692
693 /*
694 * NOTE! We still have PageReserved() pages in the page tables.
695 * eg. VDSO mappings can cause them to exist.
696 */
697out:
698 return pfn_to_page(pfn);
699}
700#endif
701
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800702/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 * copy one vm_area from one task to the other. Assumes the page tables
704 * already present in the new task to be cleared in the whole range
705 * covered by this vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 */
707
Linus Torvaldsdf3a57d2020-09-23 09:56:59 -0700708static unsigned long
709copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
710 pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
711 unsigned long addr, int *rss)
712{
713 unsigned long vm_flags = vma->vm_flags;
714 pte_t pte = *src_pte;
715 struct page *page;
716 swp_entry_t entry = pte_to_swp_entry(pte);
717
718 if (likely(!non_swap_entry(entry))) {
719 if (swap_duplicate(entry) < 0)
720 return entry.val;
721
722 /* make sure dst_mm is on swapoff's mmlist. */
723 if (unlikely(list_empty(&dst_mm->mmlist))) {
724 spin_lock(&mmlist_lock);
725 if (list_empty(&dst_mm->mmlist))
726 list_add(&dst_mm->mmlist,
727 &src_mm->mmlist);
728 spin_unlock(&mmlist_lock);
729 }
730 rss[MM_SWAPENTS]++;
731 } else if (is_migration_entry(entry)) {
732 page = migration_entry_to_page(entry);
733
734 rss[mm_counter(page)]++;
735
736 if (is_write_migration_entry(entry) &&
737 is_cow_mapping(vm_flags)) {
738 /*
739 * COW mappings require pages in both
740 * parent and child to be set to read.
741 */
742 make_migration_entry_read(&entry);
743 pte = swp_entry_to_pte(entry);
744 if (pte_swp_soft_dirty(*src_pte))
745 pte = pte_swp_mksoft_dirty(pte);
746 if (pte_swp_uffd_wp(*src_pte))
747 pte = pte_swp_mkuffd_wp(pte);
748 set_pte_at(src_mm, addr, src_pte, pte);
749 }
750 } else if (is_device_private_entry(entry)) {
751 page = device_private_entry_to_page(entry);
752
753 /*
754 * Update rss count even for unaddressable pages, as
755 * they should treated just like normal pages in this
756 * respect.
757 *
758 * We will likely want to have some new rss counters
759 * for unaddressable pages, at some point. But for now
760 * keep things as they are.
761 */
762 get_page(page);
763 rss[mm_counter(page)]++;
764 page_dup_rmap(page, false);
765
766 /*
767 * We do not preserve soft-dirty information, because so
768 * far, checkpoint/restore is the only feature that
769 * requires that. And checkpoint/restore does not work
770 * when a device driver is involved (you cannot easily
771 * save and restore device driver state).
772 */
773 if (is_write_device_private_entry(entry) &&
774 is_cow_mapping(vm_flags)) {
775 make_device_private_entry_read(&entry);
776 pte = swp_entry_to_pte(entry);
777 if (pte_swp_uffd_wp(*src_pte))
778 pte = pte_swp_mkuffd_wp(pte);
779 set_pte_at(src_mm, addr, src_pte, pte);
780 }
781 }
782 set_pte_at(dst_mm, addr, dst_pte, pte);
783 return 0;
784}
785
Peter Xu70e806e2020-09-25 18:25:59 -0400786/*
787 * Copy a present and normal page if necessary.
788 *
789 * NOTE! The usual case is that this doesn't need to do
790 * anything, and can just return a positive value. That
791 * will let the caller know that it can just increase
792 * the page refcount and re-use the pte the traditional
793 * way.
794 *
795 * But _if_ we need to copy it because it needs to be
796 * pinned in the parent (and the child should get its own
797 * copy rather than just a reference to the same page),
798 * we'll do that here and return zero to let the caller
799 * know we're done.
800 *
801 * And if we need a pre-allocated page but don't yet have
802 * one, return a negative error to let the preallocation
803 * code know so that it can do so outside the page table
804 * lock.
805 */
806static inline int
Peter Xuc78f4632020-10-13 16:54:21 -0700807copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
808 pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss,
809 struct page **prealloc, pte_t pte, struct page *page)
Peter Xu70e806e2020-09-25 18:25:59 -0400810{
811 struct page *new_page;
812
Peter Xu70e806e2020-09-25 18:25:59 -0400813 /*
Peter Xu70e806e2020-09-25 18:25:59 -0400814 * What we want to do is to check whether this page may
815 * have been pinned by the parent process. If so,
816 * instead of wrprotect the pte on both sides, we copy
817 * the page immediately so that we'll always guarantee
818 * the pinned page won't be randomly replaced in the
819 * future.
820 *
Linus Torvaldsf3c64ed2020-09-28 12:50:03 -0700821 * The page pinning checks are just "has this mm ever
822 * seen pinning", along with the (inexact) check of
823 * the page count. That might give false positives for
824 * for pinning, but it will work correctly.
Peter Xu70e806e2020-09-25 18:25:59 -0400825 */
Peter Xu97a7e472021-03-12 21:07:26 -0800826 if (likely(!page_needs_cow_for_dma(src_vma, page)))
Peter Xu70e806e2020-09-25 18:25:59 -0400827 return 1;
828
Peter Xu70e806e2020-09-25 18:25:59 -0400829 new_page = *prealloc;
830 if (!new_page)
831 return -EAGAIN;
832
833 /*
834 * We have a prealloc page, all good! Take it
835 * over and copy the page & arm it.
836 */
837 *prealloc = NULL;
Peter Xuc78f4632020-10-13 16:54:21 -0700838 copy_user_highpage(new_page, page, addr, src_vma);
Peter Xu70e806e2020-09-25 18:25:59 -0400839 __SetPageUptodate(new_page);
Peter Xuc78f4632020-10-13 16:54:21 -0700840 page_add_new_anon_rmap(new_page, dst_vma, addr, false);
841 lru_cache_add_inactive_or_unevictable(new_page, dst_vma);
Peter Xu70e806e2020-09-25 18:25:59 -0400842 rss[mm_counter(new_page)]++;
843
844 /* All done, just insert the new page copy in the child */
Peter Xuc78f4632020-10-13 16:54:21 -0700845 pte = mk_pte(new_page, dst_vma->vm_page_prot);
846 pte = maybe_mkwrite(pte_mkdirty(pte), dst_vma);
847 set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte);
Peter Xu70e806e2020-09-25 18:25:59 -0400848 return 0;
849}
850
851/*
852 * Copy one pte. Returns 0 if succeeded, or -EAGAIN if one preallocated page
853 * is required to copy this pte.
854 */
855static inline int
Peter Xuc78f4632020-10-13 16:54:21 -0700856copy_present_pte(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
857 pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss,
858 struct page **prealloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Peter Xuc78f4632020-10-13 16:54:21 -0700860 struct mm_struct *src_mm = src_vma->vm_mm;
861 unsigned long vm_flags = src_vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 pte_t pte = *src_pte;
863 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Peter Xuc78f4632020-10-13 16:54:21 -0700865 page = vm_normal_page(src_vma, addr, pte);
Peter Xu70e806e2020-09-25 18:25:59 -0400866 if (page) {
867 int retval;
868
Peter Xuc78f4632020-10-13 16:54:21 -0700869 retval = copy_present_page(dst_vma, src_vma, dst_pte, src_pte,
870 addr, rss, prealloc, pte, page);
Peter Xu70e806e2020-09-25 18:25:59 -0400871 if (retval <= 0)
872 return retval;
873
874 get_page(page);
875 page_dup_rmap(page, false);
876 rss[mm_counter(page)]++;
877 }
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /*
880 * If it's a COW mapping, write protect it both
881 * in the parent and the child
882 */
Linus Torvalds1b2de5d2018-07-09 13:19:49 -0700883 if (is_cow_mapping(vm_flags) && pte_write(pte)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 ptep_set_wrprotect(src_mm, addr, src_pte);
Zachary Amsden3dc90792006-09-30 23:29:30 -0700885 pte = pte_wrprotect(pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887
888 /*
889 * If it's a shared mapping, mark it clean in
890 * the child
891 */
892 if (vm_flags & VM_SHARED)
893 pte = pte_mkclean(pte);
894 pte = pte_mkold(pte);
Linus Torvalds6aab3412005-11-28 14:34:23 -0800895
Peter Xub569a172020-04-06 20:05:53 -0700896 /*
897 * Make sure the _PAGE_UFFD_WP bit is cleared if the new VMA
898 * does not have the VM_UFFD_WP, which means that the uffd
899 * fork event is not enabled.
900 */
901 if (!(vm_flags & VM_UFFD_WP))
902 pte = pte_clear_uffd_wp(pte);
903
Peter Xuc78f4632020-10-13 16:54:21 -0700904 set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte);
Peter Xu70e806e2020-09-25 18:25:59 -0400905 return 0;
906}
907
908static inline struct page *
909page_copy_prealloc(struct mm_struct *src_mm, struct vm_area_struct *vma,
910 unsigned long addr)
911{
912 struct page *new_page;
913
914 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, addr);
915 if (!new_page)
916 return NULL;
917
918 if (mem_cgroup_charge(new_page, src_mm, GFP_KERNEL)) {
919 put_page(new_page);
920 return NULL;
921 }
922 cgroup_throttle_swaprate(new_page, GFP_KERNEL);
923
924 return new_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
Peter Xuc78f4632020-10-13 16:54:21 -0700927static int
928copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
929 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
930 unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Peter Xuc78f4632020-10-13 16:54:21 -0700932 struct mm_struct *dst_mm = dst_vma->vm_mm;
933 struct mm_struct *src_mm = src_vma->vm_mm;
Daisuke Nishimurac36987e2009-10-26 16:50:23 -0700934 pte_t *orig_src_pte, *orig_dst_pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 pte_t *src_pte, *dst_pte;
Hugh Dickinsc74df322005-10-29 18:16:23 -0700936 spinlock_t *src_ptl, *dst_ptl;
Peter Xu70e806e2020-09-25 18:25:59 -0400937 int progress, ret = 0;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800938 int rss[NR_MM_COUNTERS];
Hugh Dickins570a335b2009-12-14 17:58:46 -0800939 swp_entry_t entry = (swp_entry_t){0};
Peter Xu70e806e2020-09-25 18:25:59 -0400940 struct page *prealloc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942again:
Peter Xu70e806e2020-09-25 18:25:59 -0400943 progress = 0;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800944 init_rss_vec(rss);
945
Hugh Dickinsc74df322005-10-29 18:16:23 -0700946 dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
Peter Xu70e806e2020-09-25 18:25:59 -0400947 if (!dst_pte) {
948 ret = -ENOMEM;
949 goto out;
950 }
Peter Zijlstraece0e2b2010-10-26 14:21:52 -0700951 src_pte = pte_offset_map(src_pmd, addr);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700952 src_ptl = pte_lockptr(src_mm, src_pmd);
Ingo Molnarf20dc5f2006-07-03 00:25:08 -0700953 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Daisuke Nishimurac36987e2009-10-26 16:50:23 -0700954 orig_src_pte = src_pte;
955 orig_dst_pte = dst_pte;
Zachary Amsden6606c3e2006-09-30 23:29:33 -0700956 arch_enter_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 do {
959 /*
960 * We are holding two locks at this point - either of them
961 * could generate latencies in another task on another CPU.
962 */
Hugh Dickinse040f212005-10-29 18:15:53 -0700963 if (progress >= 32) {
964 progress = 0;
965 if (need_resched() ||
Nick Piggin95c354f2008-01-30 13:31:20 +0100966 spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
Hugh Dickinse040f212005-10-29 18:15:53 -0700967 break;
968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (pte_none(*src_pte)) {
970 progress++;
971 continue;
972 }
Linus Torvalds79a19712020-09-23 10:04:16 -0700973 if (unlikely(!pte_present(*src_pte))) {
974 entry.val = copy_nonpresent_pte(dst_mm, src_mm,
975 dst_pte, src_pte,
Peter Xuc78f4632020-10-13 16:54:21 -0700976 src_vma, addr, rss);
Linus Torvalds79a19712020-09-23 10:04:16 -0700977 if (entry.val)
978 break;
979 progress += 8;
980 continue;
981 }
Peter Xu70e806e2020-09-25 18:25:59 -0400982 /* copy_present_pte() will clear `*prealloc' if consumed */
Peter Xuc78f4632020-10-13 16:54:21 -0700983 ret = copy_present_pte(dst_vma, src_vma, dst_pte, src_pte,
984 addr, rss, &prealloc);
Peter Xu70e806e2020-09-25 18:25:59 -0400985 /*
986 * If we need a pre-allocated page for this pte, drop the
987 * locks, allocate, and try again.
988 */
989 if (unlikely(ret == -EAGAIN))
990 break;
991 if (unlikely(prealloc)) {
992 /*
993 * pre-alloc page cannot be reused by next time so as
994 * to strictly follow mempolicy (e.g., alloc_page_vma()
995 * will allocate page according to address). This
996 * could only happen if one pinned pte changed.
997 */
998 put_page(prealloc);
999 prealloc = NULL;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 progress += 8;
1002 } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001004 arch_leave_lazy_mmu_mode();
Hugh Dickinsc74df322005-10-29 18:16:23 -07001005 spin_unlock(src_ptl);
Peter Zijlstraece0e2b2010-10-26 14:21:52 -07001006 pte_unmap(orig_src_pte);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001007 add_mm_rss_vec(dst_mm, rss);
Daisuke Nishimurac36987e2009-10-26 16:50:23 -07001008 pte_unmap_unlock(orig_dst_pte, dst_ptl);
Hugh Dickinsc74df322005-10-29 18:16:23 -07001009 cond_resched();
Hugh Dickins570a335b2009-12-14 17:58:46 -08001010
1011 if (entry.val) {
Peter Xu70e806e2020-09-25 18:25:59 -04001012 if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) {
1013 ret = -ENOMEM;
1014 goto out;
1015 }
1016 entry.val = 0;
1017 } else if (ret) {
1018 WARN_ON_ONCE(ret != -EAGAIN);
Peter Xuc78f4632020-10-13 16:54:21 -07001019 prealloc = page_copy_prealloc(src_mm, src_vma, addr);
Peter Xu70e806e2020-09-25 18:25:59 -04001020 if (!prealloc)
Hugh Dickins570a335b2009-12-14 17:58:46 -08001021 return -ENOMEM;
Peter Xu70e806e2020-09-25 18:25:59 -04001022 /* We've captured and resolved the error. Reset, try again. */
1023 ret = 0;
Hugh Dickins570a335b2009-12-14 17:58:46 -08001024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (addr != end)
1026 goto again;
Peter Xu70e806e2020-09-25 18:25:59 -04001027out:
1028 if (unlikely(prealloc))
1029 put_page(prealloc);
1030 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
Peter Xuc78f4632020-10-13 16:54:21 -07001033static inline int
1034copy_pmd_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
1035 pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
1036 unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Peter Xuc78f4632020-10-13 16:54:21 -07001038 struct mm_struct *dst_mm = dst_vma->vm_mm;
1039 struct mm_struct *src_mm = src_vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 pmd_t *src_pmd, *dst_pmd;
1041 unsigned long next;
1042
1043 dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
1044 if (!dst_pmd)
1045 return -ENOMEM;
1046 src_pmd = pmd_offset(src_pud, addr);
1047 do {
1048 next = pmd_addr_end(addr, end);
Zi Yan84c3fc42017-09-08 16:11:01 -07001049 if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd)
1050 || pmd_devmap(*src_pmd)) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001051 int err;
Peter Xuc78f4632020-10-13 16:54:21 -07001052 VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, src_vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001053 err = copy_huge_pmd(dst_mm, src_mm,
Peter Xuc78f4632020-10-13 16:54:21 -07001054 dst_pmd, src_pmd, addr, src_vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001055 if (err == -ENOMEM)
1056 return -ENOMEM;
1057 if (!err)
1058 continue;
1059 /* fall through */
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (pmd_none_or_clear_bad(src_pmd))
1062 continue;
Peter Xuc78f4632020-10-13 16:54:21 -07001063 if (copy_pte_range(dst_vma, src_vma, dst_pmd, src_pmd,
1064 addr, next))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return -ENOMEM;
1066 } while (dst_pmd++, src_pmd++, addr = next, addr != end);
1067 return 0;
1068}
1069
Peter Xuc78f4632020-10-13 16:54:21 -07001070static inline int
1071copy_pud_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
1072 p4d_t *dst_p4d, p4d_t *src_p4d, unsigned long addr,
1073 unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Peter Xuc78f4632020-10-13 16:54:21 -07001075 struct mm_struct *dst_mm = dst_vma->vm_mm;
1076 struct mm_struct *src_mm = src_vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 pud_t *src_pud, *dst_pud;
1078 unsigned long next;
1079
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001080 dst_pud = pud_alloc(dst_mm, dst_p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (!dst_pud)
1082 return -ENOMEM;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001083 src_pud = pud_offset(src_p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 do {
1085 next = pud_addr_end(addr, end);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001086 if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) {
1087 int err;
1088
Peter Xuc78f4632020-10-13 16:54:21 -07001089 VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, src_vma);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001090 err = copy_huge_pud(dst_mm, src_mm,
Peter Xuc78f4632020-10-13 16:54:21 -07001091 dst_pud, src_pud, addr, src_vma);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001092 if (err == -ENOMEM)
1093 return -ENOMEM;
1094 if (!err)
1095 continue;
1096 /* fall through */
1097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (pud_none_or_clear_bad(src_pud))
1099 continue;
Peter Xuc78f4632020-10-13 16:54:21 -07001100 if (copy_pmd_range(dst_vma, src_vma, dst_pud, src_pud,
1101 addr, next))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return -ENOMEM;
1103 } while (dst_pud++, src_pud++, addr = next, addr != end);
1104 return 0;
1105}
1106
Peter Xuc78f4632020-10-13 16:54:21 -07001107static inline int
1108copy_p4d_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
1109 pgd_t *dst_pgd, pgd_t *src_pgd, unsigned long addr,
1110 unsigned long end)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001111{
Peter Xuc78f4632020-10-13 16:54:21 -07001112 struct mm_struct *dst_mm = dst_vma->vm_mm;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001113 p4d_t *src_p4d, *dst_p4d;
1114 unsigned long next;
1115
1116 dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr);
1117 if (!dst_p4d)
1118 return -ENOMEM;
1119 src_p4d = p4d_offset(src_pgd, addr);
1120 do {
1121 next = p4d_addr_end(addr, end);
1122 if (p4d_none_or_clear_bad(src_p4d))
1123 continue;
Peter Xuc78f4632020-10-13 16:54:21 -07001124 if (copy_pud_range(dst_vma, src_vma, dst_p4d, src_p4d,
1125 addr, next))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001126 return -ENOMEM;
1127 } while (dst_p4d++, src_p4d++, addr = next, addr != end);
1128 return 0;
1129}
1130
Peter Xuc78f4632020-10-13 16:54:21 -07001131int
1132copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
1134 pgd_t *src_pgd, *dst_pgd;
1135 unsigned long next;
Peter Xuc78f4632020-10-13 16:54:21 -07001136 unsigned long addr = src_vma->vm_start;
1137 unsigned long end = src_vma->vm_end;
1138 struct mm_struct *dst_mm = dst_vma->vm_mm;
1139 struct mm_struct *src_mm = src_vma->vm_mm;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001140 struct mmu_notifier_range range;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001141 bool is_cow;
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001142 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Nick Piggind9928952005-08-28 16:49:11 +10001144 /*
1145 * Don't copy ptes where a page fault will fill them correctly.
1146 * Fork becomes much lighter when there are big shared or private
1147 * readonly mappings. The tradeoff is that copy_page_range is more
1148 * efficient than faulting.
1149 */
Peter Xuc78f4632020-10-13 16:54:21 -07001150 if (!(src_vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) &&
1151 !src_vma->anon_vma)
Kirill A. Shutemov0661a332015-02-10 14:10:04 -08001152 return 0;
Nick Piggind9928952005-08-28 16:49:11 +10001153
Peter Xuc78f4632020-10-13 16:54:21 -07001154 if (is_vm_hugetlb_page(src_vma))
1155 return copy_hugetlb_page_range(dst_mm, src_mm, src_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Peter Xuc78f4632020-10-13 16:54:21 -07001157 if (unlikely(src_vma->vm_flags & VM_PFNMAP)) {
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001158 /*
1159 * We do not free on error cases below as remove_vma
1160 * gets called on error from higher level routine
1161 */
Peter Xuc78f4632020-10-13 16:54:21 -07001162 ret = track_pfn_copy(src_vma);
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08001163 if (ret)
1164 return ret;
1165 }
1166
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001167 /*
1168 * We need to invalidate the secondary MMU mappings only when
1169 * there could be a permission downgrade on the ptes of the
1170 * parent mm. And a permission downgrade will only happen if
1171 * is_cow_mapping() returns true.
1172 */
Peter Xuc78f4632020-10-13 16:54:21 -07001173 is_cow = is_cow_mapping(src_vma->vm_flags);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001174
1175 if (is_cow) {
Jérôme Glisse7269f992019-05-13 17:20:53 -07001176 mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE,
Peter Xuc78f4632020-10-13 16:54:21 -07001177 0, src_vma, src_mm, addr, end);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001178 mmu_notifier_invalidate_range_start(&range);
Jason Gunthorpe57efa1f2020-12-14 19:05:44 -08001179 /*
1180 * Disabling preemption is not needed for the write side, as
1181 * the read side doesn't spin, but goes to the mmap_lock.
1182 *
1183 * Use the raw variant of the seqcount_t write API to avoid
1184 * lockdep complaining about preemptibility.
1185 */
1186 mmap_assert_write_locked(src_mm);
1187 raw_write_seqcount_begin(&src_mm->write_protect_seq);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001188 }
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001189
1190 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 dst_pgd = pgd_offset(dst_mm, addr);
1192 src_pgd = pgd_offset(src_mm, addr);
1193 do {
1194 next = pgd_addr_end(addr, end);
1195 if (pgd_none_or_clear_bad(src_pgd))
1196 continue;
Peter Xuc78f4632020-10-13 16:54:21 -07001197 if (unlikely(copy_p4d_range(dst_vma, src_vma, dst_pgd, src_pgd,
1198 addr, next))) {
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001199 ret = -ENOMEM;
1200 break;
1201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 } while (dst_pgd++, src_pgd++, addr = next, addr != end);
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001203
Jason Gunthorpe57efa1f2020-12-14 19:05:44 -08001204 if (is_cow) {
1205 raw_write_seqcount_end(&src_mm->write_protect_seq);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001206 mmu_notifier_invalidate_range_end(&range);
Jason Gunthorpe57efa1f2020-12-14 19:05:44 -08001207 }
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07001208 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Robin Holt51c6f662005-11-13 16:06:42 -08001211static unsigned long zap_pte_range(struct mmu_gather *tlb,
Nick Pigginb5810032005-10-29 18:16:12 -07001212 struct vm_area_struct *vma, pmd_t *pmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 unsigned long addr, unsigned long end,
Peter Zijlstra97a89412011-05-24 17:12:04 -07001214 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Nick Pigginb5810032005-10-29 18:16:12 -07001216 struct mm_struct *mm = tlb->mm;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001217 int force_flush = 0;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001218 int rss[NR_MM_COUNTERS];
Peter Zijlstra97a89412011-05-24 17:12:04 -07001219 spinlock_t *ptl;
Steven Rostedt5f1a1902011-06-15 15:08:23 -07001220 pte_t *start_pte;
Peter Zijlstra97a89412011-05-24 17:12:04 -07001221 pte_t *pte;
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001222 swp_entry_t entry;
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001223
Peter Zijlstraed6a7932018-08-31 14:46:08 +02001224 tlb_change_page_size(tlb, PAGE_SIZE);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001225again:
Peter Zijlstrae3032972011-05-24 17:12:01 -07001226 init_rss_vec(rss);
Steven Rostedt5f1a1902011-06-15 15:08:23 -07001227 start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
1228 pte = start_pte;
Mel Gorman3ea27712017-08-02 13:31:52 -07001229 flush_tlb_batched_pending(mm);
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001230 arch_enter_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 do {
1232 pte_t ptent = *pte;
Tobin C Harding166f61b2017-02-24 14:59:01 -08001233 if (pte_none(ptent))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 continue;
Hugh Dickins6f5e6b92006-03-16 23:04:09 -08001235
Minchan Kim7b167b62019-09-24 00:02:24 +00001236 if (need_resched())
1237 break;
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (pte_present(ptent)) {
Hugh Dickinsee498ed2005-11-21 21:32:18 -08001240 struct page *page;
Robin Holt51c6f662005-11-13 16:06:42 -08001241
Christoph Hellwig25b29952019-06-13 22:50:49 +02001242 page = vm_normal_page(vma, addr, ptent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (unlikely(details) && page) {
1244 /*
1245 * unmap_shared_mapping_pages() wants to
1246 * invalidate cache without truncating:
1247 * unmap shared but keep private pages.
1248 */
1249 if (details->check_mapping &&
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001250 details->check_mapping != page_rmapping(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
Nick Pigginb5810032005-10-29 18:16:12 -07001253 ptent = ptep_get_and_clear_full(mm, addr, pte,
Zachary Amsdena6003882005-09-03 15:55:04 -07001254 tlb->fullmm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 tlb_remove_tlb_entry(tlb, pte, addr);
1256 if (unlikely(!page))
1257 continue;
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001258
1259 if (!PageAnon(page)) {
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001260 if (pte_dirty(ptent)) {
1261 force_flush = 1;
Hugh Dickins6237bcd2005-10-29 18:15:54 -07001262 set_page_dirty(page);
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001263 }
Johannes Weiner4917e5d2009-01-06 14:39:17 -08001264 if (pte_young(ptent) &&
Joe Perches64363aa2013-07-08 16:00:18 -07001265 likely(!(vma->vm_flags & VM_SEQ_READ)))
Nick Pigginbf3f3bc2009-01-06 14:38:55 -08001266 mark_page_accessed(page);
Hugh Dickins6237bcd2005-10-29 18:15:54 -07001267 }
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001268 rss[mm_counter(page)]--;
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001269 page_remove_rmap(page, false);
Hugh Dickins3dc14742009-01-06 14:40:08 -08001270 if (unlikely(page_mapcount(page) < 0))
1271 print_bad_pte(vma, addr, ptent, page);
Aneesh Kumar K.Ve9d55e12016-07-26 15:24:09 -07001272 if (unlikely(__tlb_remove_page(tlb, page))) {
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001273 force_flush = 1;
Will Deaconce9ec372014-10-28 13:16:28 -07001274 addr += PAGE_SIZE;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001275 break;
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 continue;
1278 }
Jérôme Glisse5042db42017-09-08 16:11:43 -07001279
1280 entry = pte_to_swp_entry(ptent);
Ralph Campbell463b7a12020-08-06 23:22:21 -07001281 if (is_device_private_entry(entry)) {
Jérôme Glisse5042db42017-09-08 16:11:43 -07001282 struct page *page = device_private_entry_to_page(entry);
1283
1284 if (unlikely(details && details->check_mapping)) {
1285 /*
1286 * unmap_shared_mapping_pages() wants to
1287 * invalidate cache without truncating:
1288 * unmap shared but keep private pages.
1289 */
1290 if (details->check_mapping !=
1291 page_rmapping(page))
1292 continue;
1293 }
1294
1295 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
1296 rss[mm_counter(page)]--;
1297 page_remove_rmap(page, false);
1298 put_page(page);
1299 continue;
1300 }
1301
Kirill A. Shutemov3e8715f2017-02-22 15:46:34 -08001302 /* If details->check_mapping, we leave swap entries. */
1303 if (unlikely(details))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 continue;
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -08001305
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001306 if (!non_swap_entry(entry))
1307 rss[MM_SWAPENTS]--;
1308 else if (is_migration_entry(entry)) {
1309 struct page *page;
Konstantin Khlebnikov9f9f1ac2012-01-20 14:34:24 -08001310
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001311 page = migration_entry_to_page(entry);
Jerome Marchandeca56ff2016-01-14 15:19:26 -08001312 rss[mm_counter(page)]--;
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -08001313 }
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001314 if (unlikely(!free_swap_and_cache(entry)))
1315 print_bad_pte(vma, addr, ptent, NULL);
Zachary Amsden9888a1c2006-09-30 23:29:31 -07001316 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
Peter Zijlstra97a89412011-05-24 17:12:04 -07001317 } while (pte++, addr += PAGE_SIZE, addr != end);
Hugh Dickinsae859762005-10-29 18:16:05 -07001318
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001319 add_mm_rss_vec(mm, rss);
Zachary Amsden6606c3e2006-09-30 23:29:33 -07001320 arch_leave_lazy_mmu_mode();
Robin Holt51c6f662005-11-13 16:06:42 -08001321
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001322 /* Do the actual TLB flush before dropping ptl */
Will Deaconfb7332a2014-10-29 10:03:09 +00001323 if (force_flush)
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001324 tlb_flush_mmu_tlbonly(tlb);
Linus Torvalds1cf35d42014-04-25 16:05:40 -07001325 pte_unmap_unlock(start_pte, ptl);
1326
1327 /*
1328 * If we forced a TLB flush (either due to running out of
1329 * batch buffers or because we needed to flush dirty TLB
1330 * entries before releasing the ptl), free the batched
1331 * memory too. Restart if we didn't do everything.
1332 */
1333 if (force_flush) {
1334 force_flush = 0;
Peter Zijlstrafa0aafb2018-09-20 10:54:04 +02001335 tlb_flush_mmu(tlb);
Minchan Kim7b167b62019-09-24 00:02:24 +00001336 }
1337
1338 if (addr != end) {
1339 cond_resched();
1340 goto again;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001341 }
1342
Robin Holt51c6f662005-11-13 16:06:42 -08001343 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Robin Holt51c6f662005-11-13 16:06:42 -08001346static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
Nick Pigginb5810032005-10-29 18:16:12 -07001347 struct vm_area_struct *vma, pud_t *pud,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 unsigned long addr, unsigned long end,
Peter Zijlstra97a89412011-05-24 17:12:04 -07001349 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 pmd_t *pmd;
1352 unsigned long next;
1353
1354 pmd = pmd_offset(pud, addr);
1355 do {
1356 next = pmd_addr_end(addr, end);
Zi Yan84c3fc42017-09-08 16:11:01 -07001357 if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
Hugh Dickins53406ed2018-08-01 11:31:52 -07001358 if (next - addr != HPAGE_PMD_SIZE)
David Rientjesfd607752016-12-12 16:42:20 -08001359 __split_huge_pmd(vma, pmd, addr, false, NULL);
Hugh Dickins53406ed2018-08-01 11:31:52 -07001360 else if (zap_huge_pmd(tlb, vma, pmd, addr))
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001361 goto next;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001362 /* fall through */
Hugh Dickins22061a12021-06-15 18:24:03 -07001363 } else if (details && details->single_page &&
1364 PageTransCompound(details->single_page) &&
1365 next - addr == HPAGE_PMD_SIZE && pmd_none(*pmd)) {
1366 spinlock_t *ptl = pmd_lock(tlb->mm, pmd);
1367 /*
1368 * Take and drop THP pmd lock so that we cannot return
1369 * prematurely, while zap_huge_pmd() has cleared *pmd,
1370 * but not yet decremented compound_mapcount().
1371 */
1372 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001373 }
Hugh Dickins22061a12021-06-15 18:24:03 -07001374
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001375 /*
1376 * Here there can be other concurrent MADV_DONTNEED or
1377 * trans huge page faults running, and if the pmd is
1378 * none or trans huge it can change under us. This is
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001379 * because MADV_DONTNEED holds the mmap_lock in read
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001380 * mode.
1381 */
1382 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
1383 goto next;
Peter Zijlstra97a89412011-05-24 17:12:04 -07001384 next = zap_pte_range(tlb, vma, pmd, addr, next, details);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001385next:
Peter Zijlstra97a89412011-05-24 17:12:04 -07001386 cond_resched();
1387 } while (pmd++, addr = next, addr != end);
Robin Holt51c6f662005-11-13 16:06:42 -08001388
1389 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
Robin Holt51c6f662005-11-13 16:06:42 -08001392static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001393 struct vm_area_struct *vma, p4d_t *p4d,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 unsigned long addr, unsigned long end,
Peter Zijlstra97a89412011-05-24 17:12:04 -07001395 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
1397 pud_t *pud;
1398 unsigned long next;
1399
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001400 pud = pud_offset(p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 do {
1402 next = pud_addr_end(addr, end);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001403 if (pud_trans_huge(*pud) || pud_devmap(*pud)) {
1404 if (next - addr != HPAGE_PUD_SIZE) {
Michel Lespinasse42fc5412020-06-08 21:33:44 -07001405 mmap_assert_locked(tlb->mm);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001406 split_huge_pud(vma, pud, addr);
1407 } else if (zap_huge_pud(tlb, vma, pud, addr))
1408 goto next;
1409 /* fall through */
1410 }
Peter Zijlstra97a89412011-05-24 17:12:04 -07001411 if (pud_none_or_clear_bad(pud))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 continue;
Peter Zijlstra97a89412011-05-24 17:12:04 -07001413 next = zap_pmd_range(tlb, vma, pud, addr, next, details);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001414next:
1415 cond_resched();
Peter Zijlstra97a89412011-05-24 17:12:04 -07001416 } while (pud++, addr = next, addr != end);
Robin Holt51c6f662005-11-13 16:06:42 -08001417
1418 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
1420
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001421static inline unsigned long zap_p4d_range(struct mmu_gather *tlb,
1422 struct vm_area_struct *vma, pgd_t *pgd,
1423 unsigned long addr, unsigned long end,
1424 struct zap_details *details)
1425{
1426 p4d_t *p4d;
1427 unsigned long next;
1428
1429 p4d = p4d_offset(pgd, addr);
1430 do {
1431 next = p4d_addr_end(addr, end);
1432 if (p4d_none_or_clear_bad(p4d))
1433 continue;
1434 next = zap_pud_range(tlb, vma, p4d, addr, next, details);
1435 } while (p4d++, addr = next, addr != end);
1436
1437 return addr;
1438}
1439
Michal Hockoaac45362016-03-25 14:20:24 -07001440void unmap_page_range(struct mmu_gather *tlb,
Al Viro038c7aa2012-03-05 13:25:09 -05001441 struct vm_area_struct *vma,
1442 unsigned long addr, unsigned long end,
1443 struct zap_details *details)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 pgd_t *pgd;
1446 unsigned long next;
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 BUG_ON(addr >= end);
1449 tlb_start_vma(tlb, vma);
1450 pgd = pgd_offset(vma->vm_mm, addr);
1451 do {
1452 next = pgd_addr_end(addr, end);
Peter Zijlstra97a89412011-05-24 17:12:04 -07001453 if (pgd_none_or_clear_bad(pgd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 continue;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001455 next = zap_p4d_range(tlb, vma, pgd, addr, next, details);
Peter Zijlstra97a89412011-05-24 17:12:04 -07001456 } while (pgd++, addr = next, addr != end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 tlb_end_vma(tlb, vma);
1458}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Al Virof5cc4ee2012-03-05 14:14:20 -05001460
1461static void unmap_single_vma(struct mmu_gather *tlb,
1462 struct vm_area_struct *vma, unsigned long start_addr,
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001463 unsigned long end_addr,
Al Virof5cc4ee2012-03-05 14:14:20 -05001464 struct zap_details *details)
1465{
1466 unsigned long start = max(vma->vm_start, start_addr);
1467 unsigned long end;
1468
1469 if (start >= vma->vm_end)
1470 return;
1471 end = min(vma->vm_end, end_addr);
1472 if (end <= vma->vm_start)
1473 return;
1474
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301475 if (vma->vm_file)
1476 uprobe_munmap(vma, start, end);
1477
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001478 if (unlikely(vma->vm_flags & VM_PFNMAP))
Suresh Siddha5180da42012-10-08 16:28:29 -07001479 untrack_pfn(vma, 0, 0);
Al Virof5cc4ee2012-03-05 14:14:20 -05001480
1481 if (start != end) {
1482 if (unlikely(is_vm_hugetlb_page(vma))) {
1483 /*
1484 * It is undesirable to test vma->vm_file as it
1485 * should be non-null for valid hugetlb area.
1486 * However, vm_file will be NULL in the error
Davidlohr Bueso7aa6b4a2014-04-07 15:37:01 -07001487 * cleanup path of mmap_region. When
Al Virof5cc4ee2012-03-05 14:14:20 -05001488 * hugetlbfs ->mmap method fails,
Davidlohr Bueso7aa6b4a2014-04-07 15:37:01 -07001489 * mmap_region() nullifies vma->vm_file
Al Virof5cc4ee2012-03-05 14:14:20 -05001490 * before calling this function to clean up.
1491 * Since no pte has actually been setup, it is
1492 * safe to do nothing in this case.
1493 */
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07001494 if (vma->vm_file) {
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08001495 i_mmap_lock_write(vma->vm_file->f_mapping);
Mel Gormand8333522012-07-31 16:46:20 -07001496 __unmap_hugepage_range_final(tlb, vma, start, end, NULL);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08001497 i_mmap_unlock_write(vma->vm_file->f_mapping);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07001498 }
Al Virof5cc4ee2012-03-05 14:14:20 -05001499 } else
1500 unmap_page_range(tlb, vma, start, end, details);
1501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504/**
1505 * unmap_vmas - unmap a range of memory covered by a list of vma's
Randy Dunlap0164f692011-06-15 15:08:09 -07001506 * @tlb: address of the caller's struct mmu_gather
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 * @vma: the starting vma
1508 * @start_addr: virtual address at which to start unmapping
1509 * @end_addr: virtual address at which to end unmapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 *
Hugh Dickins508034a2005-10-29 18:16:30 -07001511 * Unmap all pages in the vma list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 * Only addresses between `start' and `end' will be unmapped.
1514 *
1515 * The VMA list must be sorted in ascending virtual address order.
1516 *
1517 * unmap_vmas() assumes that the caller will flush the whole unmapped address
1518 * range after unmap_vmas() returns. So the only responsibility here is to
1519 * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
1520 * drops the lock and schedules.
1521 */
Al Viro6e8bb012012-03-05 13:41:15 -05001522void unmap_vmas(struct mmu_gather *tlb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 struct vm_area_struct *vma, unsigned long start_addr,
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001524 unsigned long end_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001526 struct mmu_notifier_range range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07001528 mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, vma->vm_mm,
1529 start_addr, end_addr);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001530 mmu_notifier_invalidate_range_start(&range);
Al Virof5cc4ee2012-03-05 14:14:20 -05001531 for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07001532 unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001533 mmu_notifier_invalidate_range_end(&range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
1536/**
1537 * zap_page_range - remove user pages in a given range
1538 * @vma: vm_area_struct holding the applicable pages
Randy Dunlapeb4546b2012-06-20 12:53:02 -07001539 * @start: starting address of pages to zap
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 * @size: number of bytes to zap
Al Virof5cc4ee2012-03-05 14:14:20 -05001541 *
1542 * Caller must protect the VMA list
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 */
Linus Torvalds7e027b12012-05-06 13:43:15 -07001544void zap_page_range(struct vm_area_struct *vma, unsigned long start,
Kirill A. Shutemovecf13852017-02-22 15:46:37 -08001545 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001547 struct mmu_notifier_range range;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07001548 struct mmu_gather tlb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 lru_add_drain();
Jérôme Glisse7269f992019-05-13 17:20:53 -07001551 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07001552 start, start + size);
Will Deacona72afd82021-01-27 23:53:45 +00001553 tlb_gather_mmu(&tlb, vma->vm_mm);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001554 update_hiwater_rss(vma->vm_mm);
1555 mmu_notifier_invalidate_range_start(&range);
1556 for ( ; vma && vma->vm_start < range.end; vma = vma->vm_next)
1557 unmap_single_vma(&tlb, vma, start, range.end, NULL);
1558 mmu_notifier_invalidate_range_end(&range);
Will Deaconae8eba82021-01-27 23:53:43 +00001559 tlb_finish_mmu(&tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
Jack Steinerc627f9c2008-07-29 22:33:53 -07001562/**
Al Virof5cc4ee2012-03-05 14:14:20 -05001563 * zap_page_range_single - remove user pages in a given range
1564 * @vma: vm_area_struct holding the applicable pages
1565 * @address: starting address of pages to zap
1566 * @size: number of bytes to zap
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08001567 * @details: details of shared cache invalidation
Al Virof5cc4ee2012-03-05 14:14:20 -05001568 *
1569 * The range must fit into one VMA.
1570 */
1571static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
1572 unsigned long size, struct zap_details *details)
1573{
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001574 struct mmu_notifier_range range;
Al Virof5cc4ee2012-03-05 14:14:20 -05001575 struct mmu_gather tlb;
Al Virof5cc4ee2012-03-05 14:14:20 -05001576
1577 lru_add_drain();
Jérôme Glisse7269f992019-05-13 17:20:53 -07001578 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07001579 address, address + size);
Will Deacona72afd82021-01-27 23:53:45 +00001580 tlb_gather_mmu(&tlb, vma->vm_mm);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001581 update_hiwater_rss(vma->vm_mm);
1582 mmu_notifier_invalidate_range_start(&range);
1583 unmap_single_vma(&tlb, vma, address, range.end, details);
1584 mmu_notifier_invalidate_range_end(&range);
Will Deaconae8eba82021-01-27 23:53:43 +00001585 tlb_finish_mmu(&tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586}
1587
Jack Steinerc627f9c2008-07-29 22:33:53 -07001588/**
1589 * zap_vma_ptes - remove ptes mapping the vma
1590 * @vma: vm_area_struct holding ptes to be zapped
1591 * @address: starting address of pages to zap
1592 * @size: number of bytes to zap
1593 *
1594 * This function only unmaps ptes assigned to VM_PFNMAP vmas.
1595 *
1596 * The entire address range must be fully contained within the vma.
1597 *
Jack Steinerc627f9c2008-07-29 22:33:53 -07001598 */
Leon Romanovsky27d036e2018-05-29 15:14:07 +03001599void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
Jack Steinerc627f9c2008-07-29 22:33:53 -07001600 unsigned long size)
1601{
1602 if (address < vma->vm_start || address + size > vma->vm_end ||
1603 !(vma->vm_flags & VM_PFNMAP))
Leon Romanovsky27d036e2018-05-29 15:14:07 +03001604 return;
1605
Al Virof5cc4ee2012-03-05 14:14:20 -05001606 zap_page_range_single(vma, address, size, NULL);
Jack Steinerc627f9c2008-07-29 22:33:53 -07001607}
1608EXPORT_SYMBOL_GPL(zap_vma_ptes);
1609
Arjun Roy8cd39842020-04-10 14:33:01 -07001610static pmd_t *walk_to_pmd(struct mm_struct *mm, unsigned long addr)
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -08001611{
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001612 pgd_t *pgd;
1613 p4d_t *p4d;
1614 pud_t *pud;
1615 pmd_t *pmd;
1616
1617 pgd = pgd_offset(mm, addr);
1618 p4d = p4d_alloc(mm, pgd, addr);
1619 if (!p4d)
1620 return NULL;
1621 pud = pud_alloc(mm, p4d, addr);
1622 if (!pud)
1623 return NULL;
1624 pmd = pmd_alloc(mm, pud, addr);
1625 if (!pmd)
1626 return NULL;
1627
1628 VM_BUG_ON(pmd_trans_huge(*pmd));
Arjun Roy8cd39842020-04-10 14:33:01 -07001629 return pmd;
1630}
1631
1632pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
1633 spinlock_t **ptl)
1634{
1635 pmd_t *pmd = walk_to_pmd(mm, addr);
1636
1637 if (!pmd)
1638 return NULL;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001639 return pte_alloc_map_lock(mm, pmd, addr, ptl);
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -08001640}
1641
Arjun Roy8efd6f5b2020-04-10 14:32:51 -07001642static int validate_page_before_insert(struct page *page)
1643{
1644 if (PageAnon(page) || PageSlab(page) || page_has_type(page))
1645 return -EINVAL;
1646 flush_dcache_page(page);
1647 return 0;
1648}
1649
1650static int insert_page_into_pte_locked(struct mm_struct *mm, pte_t *pte,
1651 unsigned long addr, struct page *page, pgprot_t prot)
1652{
1653 if (!pte_none(*pte))
1654 return -EBUSY;
1655 /* Ok, finally just insert the thing.. */
1656 get_page(page);
1657 inc_mm_counter_fast(mm, mm_counter_file(page));
1658 page_add_file_rmap(page, false);
1659 set_pte_at(mm, addr, pte, mk_pte(page, prot));
1660 return 0;
1661}
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663/*
Linus Torvalds238f58d2005-11-29 13:01:56 -08001664 * This is the old fallback for page remapping.
1665 *
1666 * For historical reasons, it only allows reserved pages. Only
1667 * old drivers should use this, and they needed to mark their
1668 * pages reserved for the old functions anyway.
1669 */
Nick Piggin423bad602008-04-28 02:13:01 -07001670static int insert_page(struct vm_area_struct *vma, unsigned long addr,
1671 struct page *page, pgprot_t prot)
Linus Torvalds238f58d2005-11-29 13:01:56 -08001672{
Nick Piggin423bad602008-04-28 02:13:01 -07001673 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds238f58d2005-11-29 13:01:56 -08001674 int retval;
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -08001675 pte_t *pte;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001676 spinlock_t *ptl;
1677
Arjun Roy8efd6f5b2020-04-10 14:32:51 -07001678 retval = validate_page_before_insert(page);
1679 if (retval)
KAMEZAWA Hiroyuki5b4e6552008-10-18 20:28:10 -07001680 goto out;
Linus Torvalds238f58d2005-11-29 13:01:56 -08001681 retval = -ENOMEM;
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -08001682 pte = get_locked_pte(mm, addr, &ptl);
Linus Torvalds238f58d2005-11-29 13:01:56 -08001683 if (!pte)
KAMEZAWA Hiroyuki5b4e6552008-10-18 20:28:10 -07001684 goto out;
Arjun Roy8efd6f5b2020-04-10 14:32:51 -07001685 retval = insert_page_into_pte_locked(mm, pte, addr, page, prot);
Linus Torvalds238f58d2005-11-29 13:01:56 -08001686 pte_unmap_unlock(pte, ptl);
1687out:
1688 return retval;
1689}
1690
Arjun Roy8cd39842020-04-10 14:33:01 -07001691#ifdef pte_index
Arjun Roy7f70c2a2020-06-25 20:30:01 -07001692static int insert_page_in_batch_locked(struct mm_struct *mm, pte_t *pte,
Arjun Roy8cd39842020-04-10 14:33:01 -07001693 unsigned long addr, struct page *page, pgprot_t prot)
1694{
1695 int err;
1696
1697 if (!page_count(page))
1698 return -EINVAL;
1699 err = validate_page_before_insert(page);
Arjun Roy7f70c2a2020-06-25 20:30:01 -07001700 if (err)
1701 return err;
1702 return insert_page_into_pte_locked(mm, pte, addr, page, prot);
Arjun Roy8cd39842020-04-10 14:33:01 -07001703}
1704
1705/* insert_pages() amortizes the cost of spinlock operations
1706 * when inserting pages in a loop. Arch *must* define pte_index.
1707 */
1708static int insert_pages(struct vm_area_struct *vma, unsigned long addr,
1709 struct page **pages, unsigned long *num, pgprot_t prot)
1710{
1711 pmd_t *pmd = NULL;
Arjun Roy7f70c2a2020-06-25 20:30:01 -07001712 pte_t *start_pte, *pte;
1713 spinlock_t *pte_lock;
Arjun Roy8cd39842020-04-10 14:33:01 -07001714 struct mm_struct *const mm = vma->vm_mm;
1715 unsigned long curr_page_idx = 0;
1716 unsigned long remaining_pages_total = *num;
1717 unsigned long pages_to_write_in_pmd;
1718 int ret;
1719more:
1720 ret = -EFAULT;
1721 pmd = walk_to_pmd(mm, addr);
1722 if (!pmd)
1723 goto out;
1724
1725 pages_to_write_in_pmd = min_t(unsigned long,
1726 remaining_pages_total, PTRS_PER_PTE - pte_index(addr));
1727
1728 /* Allocate the PTE if necessary; takes PMD lock once only. */
1729 ret = -ENOMEM;
1730 if (pte_alloc(mm, pmd))
1731 goto out;
Arjun Roy8cd39842020-04-10 14:33:01 -07001732
1733 while (pages_to_write_in_pmd) {
1734 int pte_idx = 0;
1735 const int batch_size = min_t(int, pages_to_write_in_pmd, 8);
1736
Arjun Roy7f70c2a2020-06-25 20:30:01 -07001737 start_pte = pte_offset_map_lock(mm, pmd, addr, &pte_lock);
1738 for (pte = start_pte; pte_idx < batch_size; ++pte, ++pte_idx) {
1739 int err = insert_page_in_batch_locked(mm, pte,
Arjun Roy8cd39842020-04-10 14:33:01 -07001740 addr, pages[curr_page_idx], prot);
1741 if (unlikely(err)) {
Arjun Roy7f70c2a2020-06-25 20:30:01 -07001742 pte_unmap_unlock(start_pte, pte_lock);
Arjun Roy8cd39842020-04-10 14:33:01 -07001743 ret = err;
1744 remaining_pages_total -= pte_idx;
1745 goto out;
1746 }
1747 addr += PAGE_SIZE;
1748 ++curr_page_idx;
1749 }
Arjun Roy7f70c2a2020-06-25 20:30:01 -07001750 pte_unmap_unlock(start_pte, pte_lock);
Arjun Roy8cd39842020-04-10 14:33:01 -07001751 pages_to_write_in_pmd -= batch_size;
1752 remaining_pages_total -= batch_size;
1753 }
1754 if (remaining_pages_total)
1755 goto more;
1756 ret = 0;
1757out:
1758 *num = remaining_pages_total;
1759 return ret;
1760}
1761#endif /* ifdef pte_index */
1762
1763/**
1764 * vm_insert_pages - insert multiple pages into user vma, batching the pmd lock.
1765 * @vma: user vma to map to
1766 * @addr: target start user address of these pages
1767 * @pages: source kernel pages
1768 * @num: in: number of pages to map. out: number of pages that were *not*
1769 * mapped. (0 means all pages were successfully mapped).
1770 *
1771 * Preferred over vm_insert_page() when inserting multiple pages.
1772 *
1773 * In case of error, we may have mapped a subset of the provided
1774 * pages. It is the caller's responsibility to account for this case.
1775 *
1776 * The same restrictions apply as in vm_insert_page().
1777 */
1778int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr,
1779 struct page **pages, unsigned long *num)
1780{
1781#ifdef pte_index
1782 const unsigned long end_addr = addr + (*num * PAGE_SIZE) - 1;
1783
1784 if (addr < vma->vm_start || end_addr >= vma->vm_end)
1785 return -EFAULT;
1786 if (!(vma->vm_flags & VM_MIXEDMAP)) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001787 BUG_ON(mmap_read_trylock(vma->vm_mm));
Arjun Roy8cd39842020-04-10 14:33:01 -07001788 BUG_ON(vma->vm_flags & VM_PFNMAP);
1789 vma->vm_flags |= VM_MIXEDMAP;
1790 }
1791 /* Defer page refcount checking till we're about to map that page. */
1792 return insert_pages(vma, addr, pages, num, vma->vm_page_prot);
1793#else
1794 unsigned long idx = 0, pgcount = *num;
Tom Rix45779b02020-07-23 21:15:18 -07001795 int err = -EINVAL;
Arjun Roy8cd39842020-04-10 14:33:01 -07001796
1797 for (; idx < pgcount; ++idx) {
1798 err = vm_insert_page(vma, addr + (PAGE_SIZE * idx), pages[idx]);
1799 if (err)
1800 break;
1801 }
1802 *num = pgcount - idx;
1803 return err;
1804#endif /* ifdef pte_index */
1805}
1806EXPORT_SYMBOL(vm_insert_pages);
1807
Rolf Eike Beerbfa5bf62006-09-25 23:31:22 -07001808/**
1809 * vm_insert_page - insert single page into user vma
1810 * @vma: user vma to map to
1811 * @addr: target user address of this page
1812 * @page: source kernel page
1813 *
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001814 * This allows drivers to insert individual pages they've allocated
1815 * into a user vma.
1816 *
1817 * The page has to be a nice clean _individual_ kernel allocation.
1818 * If you allocate a compound page, you need to have marked it as
1819 * such (__GFP_COMP), or manually just split the page up yourself
Nick Piggin8dfcc9b2006-03-22 00:08:05 -08001820 * (see split_page()).
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001821 *
1822 * NOTE! Traditionally this was done with "remap_pfn_range()" which
1823 * took an arbitrary page protection parameter. This doesn't allow
1824 * that. Your vma protection will have to be set up correctly, which
1825 * means that if you want a shared writable mapping, you'd better
1826 * ask for a shared writable mapping!
1827 *
1828 * The page does not need to be reserved.
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001829 *
1830 * Usually this function is called from f_op->mmap() handler
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001831 * under mm->mmap_lock write-lock, so it can change vma->vm_flags.
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001832 * Caller must set VM_MIXEDMAP on vma if it wants to call this
1833 * function from other places, for example from page-fault handler.
Mike Rapoporta862f682019-03-05 15:48:42 -08001834 *
1835 * Return: %0 on success, negative error code otherwise.
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001836 */
Nick Piggin423bad602008-04-28 02:13:01 -07001837int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
1838 struct page *page)
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001839{
1840 if (addr < vma->vm_start || addr >= vma->vm_end)
1841 return -EFAULT;
1842 if (!page_count(page))
1843 return -EINVAL;
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001844 if (!(vma->vm_flags & VM_MIXEDMAP)) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001845 BUG_ON(mmap_read_trylock(vma->vm_mm));
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001846 BUG_ON(vma->vm_flags & VM_PFNMAP);
1847 vma->vm_flags |= VM_MIXEDMAP;
1848 }
Nick Piggin423bad602008-04-28 02:13:01 -07001849 return insert_page(vma, addr, page, vma->vm_page_prot);
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001850}
Linus Torvaldse3c33742005-12-03 20:48:11 -08001851EXPORT_SYMBOL(vm_insert_page);
Linus Torvaldsa145dd42005-11-30 09:35:19 -08001852
Souptick Joardera667d742019-05-13 17:21:56 -07001853/*
1854 * __vm_map_pages - maps range of kernel pages into user vma
1855 * @vma: user vma to map to
1856 * @pages: pointer to array of source kernel pages
1857 * @num: number of pages in page array
1858 * @offset: user's requested vm_pgoff
1859 *
1860 * This allows drivers to map range of kernel pages into a user vma.
1861 *
1862 * Return: 0 on success and error code otherwise.
1863 */
1864static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages,
1865 unsigned long num, unsigned long offset)
1866{
1867 unsigned long count = vma_pages(vma);
1868 unsigned long uaddr = vma->vm_start;
1869 int ret, i;
1870
1871 /* Fail if the user requested offset is beyond the end of the object */
Miguel Ojeda96756fc2019-07-11 20:58:47 -07001872 if (offset >= num)
Souptick Joardera667d742019-05-13 17:21:56 -07001873 return -ENXIO;
1874
1875 /* Fail if the user requested size exceeds available object size */
1876 if (count > num - offset)
1877 return -ENXIO;
1878
1879 for (i = 0; i < count; i++) {
1880 ret = vm_insert_page(vma, uaddr, pages[offset + i]);
1881 if (ret < 0)
1882 return ret;
1883 uaddr += PAGE_SIZE;
1884 }
1885
1886 return 0;
1887}
1888
1889/**
1890 * vm_map_pages - maps range of kernel pages starts with non zero offset
1891 * @vma: user vma to map to
1892 * @pages: pointer to array of source kernel pages
1893 * @num: number of pages in page array
1894 *
1895 * Maps an object consisting of @num pages, catering for the user's
1896 * requested vm_pgoff
1897 *
1898 * If we fail to insert any page into the vma, the function will return
1899 * immediately leaving any previously inserted pages present. Callers
1900 * from the mmap handler may immediately return the error as their caller
1901 * will destroy the vma, removing any successfully inserted pages. Other
1902 * callers should make their own arrangements for calling unmap_region().
1903 *
1904 * Context: Process context. Called by mmap handlers.
1905 * Return: 0 on success and error code otherwise.
1906 */
1907int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
1908 unsigned long num)
1909{
1910 return __vm_map_pages(vma, pages, num, vma->vm_pgoff);
1911}
1912EXPORT_SYMBOL(vm_map_pages);
1913
1914/**
1915 * vm_map_pages_zero - map range of kernel pages starts with zero offset
1916 * @vma: user vma to map to
1917 * @pages: pointer to array of source kernel pages
1918 * @num: number of pages in page array
1919 *
1920 * Similar to vm_map_pages(), except that it explicitly sets the offset
1921 * to 0. This function is intended for the drivers that did not consider
1922 * vm_pgoff.
1923 *
1924 * Context: Process context. Called by mmap handlers.
1925 * Return: 0 on success and error code otherwise.
1926 */
1927int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
1928 unsigned long num)
1929{
1930 return __vm_map_pages(vma, pages, num, 0);
1931}
1932EXPORT_SYMBOL(vm_map_pages_zero);
1933
Matthew Wilcox9b5a8e02018-10-26 15:04:40 -07001934static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr,
Ross Zwislerb2770da62017-09-06 16:18:35 -07001935 pfn_t pfn, pgprot_t prot, bool mkwrite)
Nick Piggin423bad602008-04-28 02:13:01 -07001936{
1937 struct mm_struct *mm = vma->vm_mm;
Nick Piggin423bad602008-04-28 02:13:01 -07001938 pte_t *pte, entry;
1939 spinlock_t *ptl;
1940
Nick Piggin423bad602008-04-28 02:13:01 -07001941 pte = get_locked_pte(mm, addr, &ptl);
1942 if (!pte)
Matthew Wilcox9b5a8e02018-10-26 15:04:40 -07001943 return VM_FAULT_OOM;
Ross Zwislerb2770da62017-09-06 16:18:35 -07001944 if (!pte_none(*pte)) {
1945 if (mkwrite) {
1946 /*
1947 * For read faults on private mappings the PFN passed
1948 * in may not match the PFN we have mapped if the
1949 * mapped PFN is a writeable COW page. In the mkwrite
1950 * case we are creating a writable PTE for a shared
Jan Karaf2c57d92018-10-30 15:10:47 -07001951 * mapping and we expect the PFNs to match. If they
1952 * don't match, we are likely racing with block
1953 * allocation and mapping invalidation so just skip the
1954 * update.
Ross Zwislerb2770da62017-09-06 16:18:35 -07001955 */
Jan Karaf2c57d92018-10-30 15:10:47 -07001956 if (pte_pfn(*pte) != pfn_t_to_pfn(pfn)) {
1957 WARN_ON_ONCE(!is_zero_pfn(pte_pfn(*pte)));
Ross Zwislerb2770da62017-09-06 16:18:35 -07001958 goto out_unlock;
Jan Karaf2c57d92018-10-30 15:10:47 -07001959 }
Jan Karacae85cb2019-03-28 20:43:19 -07001960 entry = pte_mkyoung(*pte);
1961 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1962 if (ptep_set_access_flags(vma, addr, pte, entry, 1))
1963 update_mmu_cache(vma, addr, pte);
1964 }
1965 goto out_unlock;
Ross Zwislerb2770da62017-09-06 16:18:35 -07001966 }
Nick Piggin423bad602008-04-28 02:13:01 -07001967
1968 /* Ok, finally just insert the thing.. */
Dan Williams01c8f1c2016-01-15 16:56:40 -08001969 if (pfn_t_devmap(pfn))
1970 entry = pte_mkdevmap(pfn_t_pte(pfn, prot));
1971 else
1972 entry = pte_mkspecial(pfn_t_pte(pfn, prot));
Ross Zwislerb2770da62017-09-06 16:18:35 -07001973
Ross Zwislerb2770da62017-09-06 16:18:35 -07001974 if (mkwrite) {
1975 entry = pte_mkyoung(entry);
1976 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1977 }
1978
Nick Piggin423bad602008-04-28 02:13:01 -07001979 set_pte_at(mm, addr, pte, entry);
Russell King4b3073e2009-12-18 16:40:18 +00001980 update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
Nick Piggin423bad602008-04-28 02:13:01 -07001981
Nick Piggin423bad602008-04-28 02:13:01 -07001982out_unlock:
1983 pte_unmap_unlock(pte, ptl);
Matthew Wilcox9b5a8e02018-10-26 15:04:40 -07001984 return VM_FAULT_NOPAGE;
Nick Piggin423bad602008-04-28 02:13:01 -07001985}
1986
Matthew Wilcoxf5e6d1d2018-10-26 15:04:13 -07001987/**
1988 * vmf_insert_pfn_prot - insert single pfn into user vma with specified pgprot
1989 * @vma: user vma to map to
1990 * @addr: target user address of this page
1991 * @pfn: source kernel pfn
1992 * @pgprot: pgprot flags for the inserted page
1993 *
Randy Dunlapa1a0aea2020-08-11 18:33:05 -07001994 * This is exactly like vmf_insert_pfn(), except that it allows drivers
Matthew Wilcoxf5e6d1d2018-10-26 15:04:13 -07001995 * to override pgprot on a per-page basis.
1996 *
1997 * This only makes sense for IO mappings, and it makes no sense for
1998 * COW mappings. In general, using multiple vmas is preferable;
Matthew Wilcoxae2b01f2018-10-26 15:04:29 -07001999 * vmf_insert_pfn_prot should only be used if using multiple VMAs is
Matthew Wilcoxf5e6d1d2018-10-26 15:04:13 -07002000 * impractical.
2001 *
Thomas Hellstrom574c5b3d2019-11-22 09:25:12 +01002002 * See vmf_insert_mixed_prot() for a discussion of the implication of using
2003 * a value of @pgprot different from that of @vma->vm_page_prot.
2004 *
Matthew Wilcoxae2b01f2018-10-26 15:04:29 -07002005 * Context: Process context. May allocate using %GFP_KERNEL.
Matthew Wilcoxf5e6d1d2018-10-26 15:04:13 -07002006 * Return: vm_fault_t value.
2007 */
2008vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
2009 unsigned long pfn, pgprot_t pgprot)
2010{
Matthew Wilcox6d958542018-10-26 15:04:33 -07002011 /*
2012 * Technically, architectures with pte_special can avoid all these
2013 * restrictions (same for remap_pfn_range). However we would like
2014 * consistency in testing and feature parity among all, so we should
2015 * try to keep these invariants in place for everybody.
2016 */
2017 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
2018 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
2019 (VM_PFNMAP|VM_MIXEDMAP));
2020 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
2021 BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
2022
2023 if (addr < vma->vm_start || addr >= vma->vm_end)
2024 return VM_FAULT_SIGBUS;
2025
2026 if (!pfn_modify_allowed(pfn, pgprot))
2027 return VM_FAULT_SIGBUS;
2028
2029 track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV));
2030
Matthew Wilcox9b5a8e02018-10-26 15:04:40 -07002031 return insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot,
Matthew Wilcox6d958542018-10-26 15:04:33 -07002032 false);
Matthew Wilcoxf5e6d1d2018-10-26 15:04:13 -07002033}
2034EXPORT_SYMBOL(vmf_insert_pfn_prot);
Nick Piggine0dc0d82007-02-12 00:51:36 -08002035
Matthew Wilcoxae2b01f2018-10-26 15:04:29 -07002036/**
2037 * vmf_insert_pfn - insert single pfn into user vma
2038 * @vma: user vma to map to
2039 * @addr: target user address of this page
2040 * @pfn: source kernel pfn
2041 *
2042 * Similar to vm_insert_page, this allows drivers to insert individual pages
2043 * they've allocated into a user vma. Same comments apply.
2044 *
2045 * This function should only be called from a vm_ops->fault handler, and
2046 * in that case the handler should return the result of this function.
2047 *
2048 * vma cannot be a COW mapping.
2049 *
2050 * As this is called only for pages that do not currently exist, we
2051 * do not need to flush old virtual caches or the TLB.
2052 *
2053 * Context: Process context. May allocate using %GFP_KERNEL.
2054 * Return: vm_fault_t value.
2055 */
2056vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
2057 unsigned long pfn)
2058{
2059 return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot);
2060}
2061EXPORT_SYMBOL(vmf_insert_pfn);
2062
Dan Williams785a3fa2017-10-23 07:20:00 -07002063static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn)
2064{
2065 /* these checks mirror the abort conditions in vm_normal_page */
2066 if (vma->vm_flags & VM_MIXEDMAP)
2067 return true;
2068 if (pfn_t_devmap(pfn))
2069 return true;
2070 if (pfn_t_special(pfn))
2071 return true;
2072 if (is_zero_pfn(pfn_t_to_pfn(pfn)))
2073 return true;
2074 return false;
2075}
2076
Matthew Wilcox79f3aa52018-10-26 15:04:37 -07002077static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma,
Thomas Hellstrom574c5b3d2019-11-22 09:25:12 +01002078 unsigned long addr, pfn_t pfn, pgprot_t pgprot,
2079 bool mkwrite)
Nick Piggin423bad602008-04-28 02:13:01 -07002080{
Matthew Wilcox79f3aa52018-10-26 15:04:37 -07002081 int err;
Dan Williams87744ab2016-10-07 17:00:18 -07002082
Dan Williams785a3fa2017-10-23 07:20:00 -07002083 BUG_ON(!vm_mixed_ok(vma, pfn));
Nick Piggin423bad602008-04-28 02:13:01 -07002084
2085 if (addr < vma->vm_start || addr >= vma->vm_end)
Matthew Wilcox79f3aa52018-10-26 15:04:37 -07002086 return VM_FAULT_SIGBUS;
Borislav Petkov308a0472016-10-26 19:43:43 +02002087
2088 track_pfn_insert(vma, &pgprot, pfn);
Nick Piggin423bad602008-04-28 02:13:01 -07002089
Andi Kleen42e40892018-06-13 15:48:27 -07002090 if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot))
Matthew Wilcox79f3aa52018-10-26 15:04:37 -07002091 return VM_FAULT_SIGBUS;
Andi Kleen42e40892018-06-13 15:48:27 -07002092
Nick Piggin423bad602008-04-28 02:13:01 -07002093 /*
2094 * If we don't have pte special, then we have to use the pfn_valid()
2095 * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
2096 * refcount the page if pfn_valid is true (hence insert_page rather
Hugh Dickins62eede62009-09-21 17:03:34 -07002097 * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP
2098 * without pte special, it would there be refcounted as a normal page.
Nick Piggin423bad602008-04-28 02:13:01 -07002099 */
Laurent Dufour00b3a332018-06-07 17:06:12 -07002100 if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) &&
2101 !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) {
Nick Piggin423bad602008-04-28 02:13:01 -07002102 struct page *page;
2103
Dan Williams03fc2da2016-01-26 09:48:05 -08002104 /*
2105 * At this point we are committed to insert_page()
2106 * regardless of whether the caller specified flags that
2107 * result in pfn_t_has_page() == false.
2108 */
2109 page = pfn_to_page(pfn_t_to_pfn(pfn));
Matthew Wilcox79f3aa52018-10-26 15:04:37 -07002110 err = insert_page(vma, addr, page, pgprot);
2111 } else {
Matthew Wilcox9b5a8e02018-10-26 15:04:40 -07002112 return insert_pfn(vma, addr, pfn, pgprot, mkwrite);
Nick Piggin423bad602008-04-28 02:13:01 -07002113 }
Ross Zwislerb2770da62017-09-06 16:18:35 -07002114
Matthew Wilcox5d747632018-10-26 15:04:10 -07002115 if (err == -ENOMEM)
2116 return VM_FAULT_OOM;
2117 if (err < 0 && err != -EBUSY)
2118 return VM_FAULT_SIGBUS;
2119
2120 return VM_FAULT_NOPAGE;
Nick Piggin423bad602008-04-28 02:13:01 -07002121}
Matthew Wilcox79f3aa52018-10-26 15:04:37 -07002122
Thomas Hellstrom574c5b3d2019-11-22 09:25:12 +01002123/**
2124 * vmf_insert_mixed_prot - insert single pfn into user vma with specified pgprot
2125 * @vma: user vma to map to
2126 * @addr: target user address of this page
2127 * @pfn: source kernel pfn
2128 * @pgprot: pgprot flags for the inserted page
2129 *
Randy Dunlapa1a0aea2020-08-11 18:33:05 -07002130 * This is exactly like vmf_insert_mixed(), except that it allows drivers
Thomas Hellstrom574c5b3d2019-11-22 09:25:12 +01002131 * to override pgprot on a per-page basis.
2132 *
2133 * Typically this function should be used by drivers to set caching- and
2134 * encryption bits different than those of @vma->vm_page_prot, because
2135 * the caching- or encryption mode may not be known at mmap() time.
2136 * This is ok as long as @vma->vm_page_prot is not used by the core vm
2137 * to set caching and encryption bits for those vmas (except for COW pages).
2138 * This is ensured by core vm only modifying these page table entries using
2139 * functions that don't touch caching- or encryption bits, using pte_modify()
2140 * if needed. (See for example mprotect()).
2141 * Also when new page-table entries are created, this is only done using the
2142 * fault() callback, and never using the value of vma->vm_page_prot,
2143 * except for page-table entries that point to anonymous pages as the result
2144 * of COW.
2145 *
2146 * Context: Process context. May allocate using %GFP_KERNEL.
2147 * Return: vm_fault_t value.
2148 */
2149vm_fault_t vmf_insert_mixed_prot(struct vm_area_struct *vma, unsigned long addr,
2150 pfn_t pfn, pgprot_t pgprot)
2151{
2152 return __vm_insert_mixed(vma, addr, pfn, pgprot, false);
2153}
Thomas Hellstrom5379e4d2019-11-22 09:34:35 +01002154EXPORT_SYMBOL(vmf_insert_mixed_prot);
Thomas Hellstrom574c5b3d2019-11-22 09:25:12 +01002155
Matthew Wilcox79f3aa52018-10-26 15:04:37 -07002156vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
2157 pfn_t pfn)
2158{
Thomas Hellstrom574c5b3d2019-11-22 09:25:12 +01002159 return __vm_insert_mixed(vma, addr, pfn, vma->vm_page_prot, false);
Matthew Wilcox79f3aa52018-10-26 15:04:37 -07002160}
Matthew Wilcox5d747632018-10-26 15:04:10 -07002161EXPORT_SYMBOL(vmf_insert_mixed);
Nick Piggin423bad602008-04-28 02:13:01 -07002162
Souptick Joarderab77dab2018-06-07 17:04:29 -07002163/*
2164 * If the insertion of PTE failed because someone else already added a
2165 * different entry in the mean time, we treat that as success as we assume
2166 * the same entry was actually inserted.
2167 */
Souptick Joarderab77dab2018-06-07 17:04:29 -07002168vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma,
2169 unsigned long addr, pfn_t pfn)
Ross Zwislerb2770da62017-09-06 16:18:35 -07002170{
Thomas Hellstrom574c5b3d2019-11-22 09:25:12 +01002171 return __vm_insert_mixed(vma, addr, pfn, vma->vm_page_prot, true);
Ross Zwislerb2770da62017-09-06 16:18:35 -07002172}
Souptick Joarderab77dab2018-06-07 17:04:29 -07002173EXPORT_SYMBOL(vmf_insert_mixed_mkwrite);
Ross Zwislerb2770da62017-09-06 16:18:35 -07002174
Linus Torvaldsa145dd42005-11-30 09:35:19 -08002175/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 * maps a range of physical memory into the requested pages. the old
2177 * mappings are removed. any references to nonexistent pages results
2178 * in null mappings (currently treated as "copy-on-access")
2179 */
2180static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
2181 unsigned long addr, unsigned long end,
2182 unsigned long pfn, pgprot_t prot)
2183{
Miaohe Lin90a3e372021-02-24 12:04:33 -08002184 pte_t *pte, *mapped_pte;
Hugh Dickinsc74df322005-10-29 18:16:23 -07002185 spinlock_t *ptl;
Andi Kleen42e40892018-06-13 15:48:27 -07002186 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Miaohe Lin90a3e372021-02-24 12:04:33 -08002188 mapped_pte = pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (!pte)
2190 return -ENOMEM;
Zachary Amsden6606c3e2006-09-30 23:29:33 -07002191 arch_enter_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 do {
2193 BUG_ON(!pte_none(*pte));
Andi Kleen42e40892018-06-13 15:48:27 -07002194 if (!pfn_modify_allowed(pfn, prot)) {
2195 err = -EACCES;
2196 break;
2197 }
Nick Piggin7e675132008-04-28 02:13:00 -07002198 set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 pfn++;
2200 } while (pte++, addr += PAGE_SIZE, addr != end);
Zachary Amsden6606c3e2006-09-30 23:29:33 -07002201 arch_leave_lazy_mmu_mode();
Miaohe Lin90a3e372021-02-24 12:04:33 -08002202 pte_unmap_unlock(mapped_pte, ptl);
Andi Kleen42e40892018-06-13 15:48:27 -07002203 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204}
2205
2206static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
2207 unsigned long addr, unsigned long end,
2208 unsigned long pfn, pgprot_t prot)
2209{
2210 pmd_t *pmd;
2211 unsigned long next;
Andi Kleen42e40892018-06-13 15:48:27 -07002212 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 pfn -= addr >> PAGE_SHIFT;
2215 pmd = pmd_alloc(mm, pud, addr);
2216 if (!pmd)
2217 return -ENOMEM;
Andrea Arcangelif66055ab2011-01-13 15:46:54 -08002218 VM_BUG_ON(pmd_trans_huge(*pmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 do {
2220 next = pmd_addr_end(addr, end);
Andi Kleen42e40892018-06-13 15:48:27 -07002221 err = remap_pte_range(mm, pmd, addr, next,
2222 pfn + (addr >> PAGE_SHIFT), prot);
2223 if (err)
2224 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 } while (pmd++, addr = next, addr != end);
2226 return 0;
2227}
2228
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002229static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 unsigned long addr, unsigned long end,
2231 unsigned long pfn, pgprot_t prot)
2232{
2233 pud_t *pud;
2234 unsigned long next;
Andi Kleen42e40892018-06-13 15:48:27 -07002235 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 pfn -= addr >> PAGE_SHIFT;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002238 pud = pud_alloc(mm, p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (!pud)
2240 return -ENOMEM;
2241 do {
2242 next = pud_addr_end(addr, end);
Andi Kleen42e40892018-06-13 15:48:27 -07002243 err = remap_pmd_range(mm, pud, addr, next,
2244 pfn + (addr >> PAGE_SHIFT), prot);
2245 if (err)
2246 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 } while (pud++, addr = next, addr != end);
2248 return 0;
2249}
2250
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002251static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd,
2252 unsigned long addr, unsigned long end,
2253 unsigned long pfn, pgprot_t prot)
2254{
2255 p4d_t *p4d;
2256 unsigned long next;
Andi Kleen42e40892018-06-13 15:48:27 -07002257 int err;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002258
2259 pfn -= addr >> PAGE_SHIFT;
2260 p4d = p4d_alloc(mm, pgd, addr);
2261 if (!p4d)
2262 return -ENOMEM;
2263 do {
2264 next = p4d_addr_end(addr, end);
Andi Kleen42e40892018-06-13 15:48:27 -07002265 err = remap_pud_range(mm, p4d, addr, next,
2266 pfn + (addr >> PAGE_SHIFT), prot);
2267 if (err)
2268 return err;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002269 } while (p4d++, addr = next, addr != end);
2270 return 0;
2271}
2272
Christoph Hellwig74ffa5a2021-04-29 22:57:29 -07002273/*
2274 * Variant of remap_pfn_range that does not call track_pfn_remap. The caller
2275 * must have pre-validated the caching bits of the pgprot_t.
Rolf Eike Beerbfa5bf62006-09-25 23:31:22 -07002276 */
Christoph Hellwig74ffa5a2021-04-29 22:57:29 -07002277int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,
2278 unsigned long pfn, unsigned long size, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
2280 pgd_t *pgd;
2281 unsigned long next;
Hugh Dickins2d15cab2005-06-25 14:54:33 -07002282 unsigned long end = addr + PAGE_ALIGN(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 struct mm_struct *mm = vma->vm_mm;
2284 int err;
2285
Alex Zhang0c4123e2020-08-06 23:22:24 -07002286 if (WARN_ON_ONCE(!PAGE_ALIGNED(addr)))
2287 return -EINVAL;
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 /*
2290 * Physically remapped pages are special. Tell the
2291 * rest of the world about it:
2292 * VM_IO tells people not to look at these pages
2293 * (accesses can have side effects).
Linus Torvalds6aab3412005-11-28 14:34:23 -08002294 * VM_PFNMAP tells the core MM that the base pages are just
2295 * raw PFN mappings, and do not have a "struct page" associated
2296 * with them.
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07002297 * VM_DONTEXPAND
2298 * Disable vma merging and expanding with mremap().
2299 * VM_DONTDUMP
2300 * Omit vma from core dump, even when VM_IO turned off.
Linus Torvaldsfb155c12005-12-11 19:46:02 -08002301 *
2302 * There's a horrible special case to handle copy-on-write
2303 * behaviour that some programs depend on. We mark the "original"
2304 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07002305 * See vm_normal_page() for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 */
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07002307 if (is_cow_mapping(vma->vm_flags)) {
2308 if (addr != vma->vm_start || end != vma->vm_end)
2309 return -EINVAL;
Linus Torvaldsfb155c12005-12-11 19:46:02 -08002310 vma->vm_pgoff = pfn;
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07002311 }
2312
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07002313 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 BUG_ON(addr >= end);
2316 pfn -= addr >> PAGE_SHIFT;
2317 pgd = pgd_offset(mm, addr);
2318 flush_cache_range(vma, addr, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 do {
2320 next = pgd_addr_end(addr, end);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002321 err = remap_p4d_range(mm, pgd, addr, next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 pfn + (addr >> PAGE_SHIFT), prot);
2323 if (err)
Christoph Hellwig74ffa5a2021-04-29 22:57:29 -07002324 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 } while (pgd++, addr = next, addr != end);
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08002326
Christoph Hellwig74ffa5a2021-04-29 22:57:29 -07002327 return 0;
2328}
venkatesh.pallipadi@intel.com2ab64032008-12-18 11:41:29 -08002329
Christoph Hellwig74ffa5a2021-04-29 22:57:29 -07002330/**
2331 * remap_pfn_range - remap kernel memory to userspace
2332 * @vma: user vma to map to
2333 * @addr: target page aligned user address to start at
2334 * @pfn: page frame number of kernel physical memory address
2335 * @size: size of mapping area
2336 * @prot: page protection flags for this mapping
2337 *
2338 * Note: this is only safe if the mm semaphore is held when called.
2339 *
2340 * Return: %0 on success, negative error code otherwise.
2341 */
2342int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
2343 unsigned long pfn, unsigned long size, pgprot_t prot)
2344{
2345 int err;
2346
2347 err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size));
2348 if (err)
2349 return -EINVAL;
2350
2351 err = remap_pfn_range_notrack(vma, addr, pfn, size, prot);
2352 if (err)
2353 untrack_pfn(vma, pfn, PAGE_ALIGN(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return err;
2355}
2356EXPORT_SYMBOL(remap_pfn_range);
2357
Linus Torvaldsb4cbb192013-04-16 13:45:37 -07002358/**
2359 * vm_iomap_memory - remap memory to userspace
2360 * @vma: user vma to map to
Wang Wenhuabd69b92020-04-01 21:09:07 -07002361 * @start: start of the physical memory to be mapped
Linus Torvaldsb4cbb192013-04-16 13:45:37 -07002362 * @len: size of area
2363 *
2364 * This is a simplified io_remap_pfn_range() for common driver use. The
2365 * driver just needs to give us the physical memory range to be mapped,
2366 * we'll figure out the rest from the vma information.
2367 *
2368 * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
2369 * whatever write-combining details or similar.
Mike Rapoporta862f682019-03-05 15:48:42 -08002370 *
2371 * Return: %0 on success, negative error code otherwise.
Linus Torvaldsb4cbb192013-04-16 13:45:37 -07002372 */
2373int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
2374{
2375 unsigned long vm_len, pfn, pages;
2376
2377 /* Check that the physical memory area passed in looks valid */
2378 if (start + len < start)
2379 return -EINVAL;
2380 /*
2381 * You *really* shouldn't map things that aren't page-aligned,
2382 * but we've historically allowed it because IO memory might
2383 * just have smaller alignment.
2384 */
2385 len += start & ~PAGE_MASK;
2386 pfn = start >> PAGE_SHIFT;
2387 pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
2388 if (pfn + pages < pfn)
2389 return -EINVAL;
2390
2391 /* We start the mapping 'vm_pgoff' pages into the area */
2392 if (vma->vm_pgoff > pages)
2393 return -EINVAL;
2394 pfn += vma->vm_pgoff;
2395 pages -= vma->vm_pgoff;
2396
2397 /* Can we fit all of the mapping? */
2398 vm_len = vma->vm_end - vma->vm_start;
2399 if (vm_len >> PAGE_SHIFT > pages)
2400 return -EINVAL;
2401
2402 /* Ok, let it rip */
2403 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
2404}
2405EXPORT_SYMBOL(vm_iomap_memory);
2406
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002407static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
2408 unsigned long addr, unsigned long end,
Joerg Roedele80d3902020-09-04 16:35:43 -07002409 pte_fn_t fn, void *data, bool create,
2410 pgtbl_mod_mask *mask)
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002411{
Miaohe Lin8abb50c2021-02-24 12:04:42 -08002412 pte_t *pte, *mapped_pte;
Daniel Axtensbe1db472019-12-17 20:51:41 -08002413 int err = 0;
Kees Cook3f649ab2020-06-03 13:09:38 -07002414 spinlock_t *ptl;
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002415
Daniel Axtensbe1db472019-12-17 20:51:41 -08002416 if (create) {
Miaohe Lin8abb50c2021-02-24 12:04:42 -08002417 mapped_pte = pte = (mm == &init_mm) ?
Joerg Roedele80d3902020-09-04 16:35:43 -07002418 pte_alloc_kernel_track(pmd, addr, mask) :
Daniel Axtensbe1db472019-12-17 20:51:41 -08002419 pte_alloc_map_lock(mm, pmd, addr, &ptl);
2420 if (!pte)
2421 return -ENOMEM;
2422 } else {
Miaohe Lin8abb50c2021-02-24 12:04:42 -08002423 mapped_pte = pte = (mm == &init_mm) ?
Daniel Axtensbe1db472019-12-17 20:51:41 -08002424 pte_offset_kernel(pmd, addr) :
2425 pte_offset_map_lock(mm, pmd, addr, &ptl);
2426 }
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002427
2428 BUG_ON(pmd_huge(*pmd));
2429
Jeremy Fitzhardinge38e0edb2009-01-06 14:39:21 -08002430 arch_enter_lazy_mmu_mode();
2431
Christoph Hellwigeeb4a052020-10-17 16:15:14 -07002432 if (fn) {
2433 do {
2434 if (create || !pte_none(*pte)) {
2435 err = fn(pte++, addr, data);
2436 if (err)
2437 break;
2438 }
2439 } while (addr += PAGE_SIZE, addr != end);
2440 }
Joerg Roedele80d3902020-09-04 16:35:43 -07002441 *mask |= PGTBL_PTE_MODIFIED;
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002442
Jeremy Fitzhardinge38e0edb2009-01-06 14:39:21 -08002443 arch_leave_lazy_mmu_mode();
2444
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002445 if (mm != &init_mm)
Miaohe Lin8abb50c2021-02-24 12:04:42 -08002446 pte_unmap_unlock(mapped_pte, ptl);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002447 return err;
2448}
2449
2450static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
2451 unsigned long addr, unsigned long end,
Joerg Roedele80d3902020-09-04 16:35:43 -07002452 pte_fn_t fn, void *data, bool create,
2453 pgtbl_mod_mask *mask)
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002454{
2455 pmd_t *pmd;
2456 unsigned long next;
Daniel Axtensbe1db472019-12-17 20:51:41 -08002457 int err = 0;
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002458
Andi Kleenceb86872008-07-23 21:27:50 -07002459 BUG_ON(pud_huge(*pud));
2460
Daniel Axtensbe1db472019-12-17 20:51:41 -08002461 if (create) {
Joerg Roedele80d3902020-09-04 16:35:43 -07002462 pmd = pmd_alloc_track(mm, pud, addr, mask);
Daniel Axtensbe1db472019-12-17 20:51:41 -08002463 if (!pmd)
2464 return -ENOMEM;
2465 } else {
2466 pmd = pmd_offset(pud, addr);
2467 }
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002468 do {
2469 next = pmd_addr_end(addr, end);
Nicholas Piggin0c95cba2021-04-29 22:58:16 -07002470 if (pmd_none(*pmd) && !create)
2471 continue;
2472 if (WARN_ON_ONCE(pmd_leaf(*pmd)))
2473 return -EINVAL;
2474 if (!pmd_none(*pmd) && WARN_ON_ONCE(pmd_bad(*pmd))) {
2475 if (!create)
2476 continue;
2477 pmd_clear_bad(pmd);
Daniel Axtensbe1db472019-12-17 20:51:41 -08002478 }
Nicholas Piggin0c95cba2021-04-29 22:58:16 -07002479 err = apply_to_pte_range(mm, pmd, addr, next,
2480 fn, data, create, mask);
2481 if (err)
2482 break;
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002483 } while (pmd++, addr = next, addr != end);
Nicholas Piggin0c95cba2021-04-29 22:58:16 -07002484
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002485 return err;
2486}
2487
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002488static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d,
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002489 unsigned long addr, unsigned long end,
Joerg Roedele80d3902020-09-04 16:35:43 -07002490 pte_fn_t fn, void *data, bool create,
2491 pgtbl_mod_mask *mask)
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002492{
2493 pud_t *pud;
2494 unsigned long next;
Daniel Axtensbe1db472019-12-17 20:51:41 -08002495 int err = 0;
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002496
Daniel Axtensbe1db472019-12-17 20:51:41 -08002497 if (create) {
Joerg Roedele80d3902020-09-04 16:35:43 -07002498 pud = pud_alloc_track(mm, p4d, addr, mask);
Daniel Axtensbe1db472019-12-17 20:51:41 -08002499 if (!pud)
2500 return -ENOMEM;
2501 } else {
2502 pud = pud_offset(p4d, addr);
2503 }
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002504 do {
2505 next = pud_addr_end(addr, end);
Nicholas Piggin0c95cba2021-04-29 22:58:16 -07002506 if (pud_none(*pud) && !create)
2507 continue;
2508 if (WARN_ON_ONCE(pud_leaf(*pud)))
2509 return -EINVAL;
2510 if (!pud_none(*pud) && WARN_ON_ONCE(pud_bad(*pud))) {
2511 if (!create)
2512 continue;
2513 pud_clear_bad(pud);
Daniel Axtensbe1db472019-12-17 20:51:41 -08002514 }
Nicholas Piggin0c95cba2021-04-29 22:58:16 -07002515 err = apply_to_pmd_range(mm, pud, addr, next,
2516 fn, data, create, mask);
2517 if (err)
2518 break;
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002519 } while (pud++, addr = next, addr != end);
Nicholas Piggin0c95cba2021-04-29 22:58:16 -07002520
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002521 return err;
2522}
2523
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002524static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd,
2525 unsigned long addr, unsigned long end,
Joerg Roedele80d3902020-09-04 16:35:43 -07002526 pte_fn_t fn, void *data, bool create,
2527 pgtbl_mod_mask *mask)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002528{
2529 p4d_t *p4d;
2530 unsigned long next;
Daniel Axtensbe1db472019-12-17 20:51:41 -08002531 int err = 0;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002532
Daniel Axtensbe1db472019-12-17 20:51:41 -08002533 if (create) {
Joerg Roedele80d3902020-09-04 16:35:43 -07002534 p4d = p4d_alloc_track(mm, pgd, addr, mask);
Daniel Axtensbe1db472019-12-17 20:51:41 -08002535 if (!p4d)
2536 return -ENOMEM;
2537 } else {
2538 p4d = p4d_offset(pgd, addr);
2539 }
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002540 do {
2541 next = p4d_addr_end(addr, end);
Nicholas Piggin0c95cba2021-04-29 22:58:16 -07002542 if (p4d_none(*p4d) && !create)
2543 continue;
2544 if (WARN_ON_ONCE(p4d_leaf(*p4d)))
2545 return -EINVAL;
2546 if (!p4d_none(*p4d) && WARN_ON_ONCE(p4d_bad(*p4d))) {
2547 if (!create)
2548 continue;
2549 p4d_clear_bad(p4d);
Daniel Axtensbe1db472019-12-17 20:51:41 -08002550 }
Nicholas Piggin0c95cba2021-04-29 22:58:16 -07002551 err = apply_to_pud_range(mm, p4d, addr, next,
2552 fn, data, create, mask);
2553 if (err)
2554 break;
Daniel Axtensbe1db472019-12-17 20:51:41 -08002555 } while (p4d++, addr = next, addr != end);
Nicholas Piggin0c95cba2021-04-29 22:58:16 -07002556
Daniel Axtensbe1db472019-12-17 20:51:41 -08002557 return err;
2558}
2559
2560static int __apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2561 unsigned long size, pte_fn_t fn,
2562 void *data, bool create)
2563{
2564 pgd_t *pgd;
Joerg Roedele80d3902020-09-04 16:35:43 -07002565 unsigned long start = addr, next;
Daniel Axtensbe1db472019-12-17 20:51:41 -08002566 unsigned long end = addr + size;
Joerg Roedele80d3902020-09-04 16:35:43 -07002567 pgtbl_mod_mask mask = 0;
Daniel Axtensbe1db472019-12-17 20:51:41 -08002568 int err = 0;
2569
2570 if (WARN_ON(addr >= end))
2571 return -EINVAL;
2572
2573 pgd = pgd_offset(mm, addr);
2574 do {
2575 next = pgd_addr_end(addr, end);
Nicholas Piggin0c95cba2021-04-29 22:58:16 -07002576 if (pgd_none(*pgd) && !create)
Daniel Axtensbe1db472019-12-17 20:51:41 -08002577 continue;
Nicholas Piggin0c95cba2021-04-29 22:58:16 -07002578 if (WARN_ON_ONCE(pgd_leaf(*pgd)))
2579 return -EINVAL;
2580 if (!pgd_none(*pgd) && WARN_ON_ONCE(pgd_bad(*pgd))) {
2581 if (!create)
2582 continue;
2583 pgd_clear_bad(pgd);
2584 }
2585 err = apply_to_p4d_range(mm, pgd, addr, next,
2586 fn, data, create, &mask);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002587 if (err)
2588 break;
Daniel Axtensbe1db472019-12-17 20:51:41 -08002589 } while (pgd++, addr = next, addr != end);
2590
Joerg Roedele80d3902020-09-04 16:35:43 -07002591 if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
2592 arch_sync_kernel_mappings(start, start + size);
2593
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002594 return err;
2595}
2596
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002597/*
2598 * Scan a region of virtual memory, filling in page tables as necessary
2599 * and calling a provided function on each leaf page table.
2600 */
2601int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2602 unsigned long size, pte_fn_t fn, void *data)
2603{
Daniel Axtensbe1db472019-12-17 20:51:41 -08002604 return __apply_to_page_range(mm, addr, size, fn, data, true);
Jeremy Fitzhardingeaee16b32007-05-06 14:48:54 -07002605}
2606EXPORT_SYMBOL_GPL(apply_to_page_range);
2607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608/*
Daniel Axtensbe1db472019-12-17 20:51:41 -08002609 * Scan a region of virtual memory, calling a provided function on
2610 * each leaf page table where it exists.
2611 *
2612 * Unlike apply_to_page_range, this does _not_ fill in page tables
2613 * where they are absent.
2614 */
2615int apply_to_existing_page_range(struct mm_struct *mm, unsigned long addr,
2616 unsigned long size, pte_fn_t fn, void *data)
2617{
2618 return __apply_to_page_range(mm, addr, size, fn, data, false);
2619}
2620EXPORT_SYMBOL_GPL(apply_to_existing_page_range);
2621
2622/*
Kirill A. Shutemov9b4bdd22015-02-10 14:09:51 -08002623 * handle_pte_fault chooses page fault handler according to an entry which was
2624 * read non-atomically. Before making any commitment, on those architectures
2625 * or configurations (e.g. i386 with PAE) which might give a mix of unmatched
2626 * parts, do_swap_page must check under lock before unmapping the pte and
2627 * proceeding (but do_wp_page is only called after already making such a check;
Ryota Ozakia335b2e2011-02-10 13:56:28 +09002628 * and do_anonymous_page can safely check later on).
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002629 */
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07002630static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002631 pte_t *page_table, pte_t orig_pte)
2632{
2633 int same = 1;
Thomas Gleixner923717c2019-10-15 21:18:12 +02002634#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPTION)
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002635 if (sizeof(pte_t) > sizeof(unsigned long)) {
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07002636 spinlock_t *ptl = pte_lockptr(mm, pmd);
2637 spin_lock(ptl);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002638 same = pte_same(*page_table, orig_pte);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07002639 spin_unlock(ptl);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07002640 }
2641#endif
2642 pte_unmap(page_table);
2643 return same;
2644}
2645
Jia He83d116c2019-10-11 22:09:39 +08002646static inline bool cow_user_page(struct page *dst, struct page *src,
2647 struct vm_fault *vmf)
Linus Torvalds6aab3412005-11-28 14:34:23 -08002648{
Jia He83d116c2019-10-11 22:09:39 +08002649 bool ret;
2650 void *kaddr;
2651 void __user *uaddr;
Kirill A. Shutemovc3e5ea62020-03-05 22:28:32 -08002652 bool locked = false;
Jia He83d116c2019-10-11 22:09:39 +08002653 struct vm_area_struct *vma = vmf->vma;
2654 struct mm_struct *mm = vma->vm_mm;
2655 unsigned long addr = vmf->address;
2656
Jia He83d116c2019-10-11 22:09:39 +08002657 if (likely(src)) {
2658 copy_user_highpage(dst, src, addr, vma);
2659 return true;
2660 }
2661
Linus Torvalds6aab3412005-11-28 14:34:23 -08002662 /*
2663 * If the source page was a PFN mapping, we don't have
2664 * a "struct page" for it. We do a best-effort copy by
2665 * just copying from the original user address. If that
2666 * fails, we just zero-fill it. Live with it.
2667 */
Jia He83d116c2019-10-11 22:09:39 +08002668 kaddr = kmap_atomic(dst);
2669 uaddr = (void __user *)(addr & PAGE_MASK);
Linus Torvalds5d2a2dbbc2005-11-29 14:07:55 -08002670
Jia He83d116c2019-10-11 22:09:39 +08002671 /*
2672 * On architectures with software "accessed" bits, we would
2673 * take a double page fault, so mark it accessed here.
2674 */
Kirill A. Shutemovc3e5ea62020-03-05 22:28:32 -08002675 if (arch_faults_on_old_pte() && !pte_young(vmf->orig_pte)) {
Jia He83d116c2019-10-11 22:09:39 +08002676 pte_t entry;
2677
2678 vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl);
Kirill A. Shutemovc3e5ea62020-03-05 22:28:32 -08002679 locked = true;
Jia He83d116c2019-10-11 22:09:39 +08002680 if (!likely(pte_same(*vmf->pte, vmf->orig_pte))) {
2681 /*
2682 * Other thread has already handled the fault
Bibo Mao7df67692020-05-27 10:25:18 +08002683 * and update local tlb only
Jia He83d116c2019-10-11 22:09:39 +08002684 */
Bibo Mao7df67692020-05-27 10:25:18 +08002685 update_mmu_tlb(vma, addr, vmf->pte);
Jia He83d116c2019-10-11 22:09:39 +08002686 ret = false;
2687 goto pte_unlock;
2688 }
2689
2690 entry = pte_mkyoung(vmf->orig_pte);
2691 if (ptep_set_access_flags(vma, addr, vmf->pte, entry, 0))
2692 update_mmu_cache(vma, addr, vmf->pte);
2693 }
2694
2695 /*
2696 * This really shouldn't fail, because the page is there
2697 * in the page tables. But it might just be unreadable,
2698 * in which case we just give up and fill the result with
2699 * zeroes.
2700 */
2701 if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) {
Kirill A. Shutemovc3e5ea62020-03-05 22:28:32 -08002702 if (locked)
2703 goto warn;
2704
2705 /* Re-validate under PTL if the page is still mapped */
2706 vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl);
2707 locked = true;
2708 if (!likely(pte_same(*vmf->pte, vmf->orig_pte))) {
Bibo Mao7df67692020-05-27 10:25:18 +08002709 /* The PTE changed under us, update local tlb */
2710 update_mmu_tlb(vma, addr, vmf->pte);
Kirill A. Shutemovc3e5ea62020-03-05 22:28:32 -08002711 ret = false;
2712 goto pte_unlock;
2713 }
2714
Linus Torvalds5d2a2dbbc2005-11-29 14:07:55 -08002715 /*
Ethon Paul985ba002020-06-04 16:49:43 -07002716 * The same page can be mapped back since last copy attempt.
Kirill A. Shutemovc3e5ea62020-03-05 22:28:32 -08002717 * Try to copy again under PTL.
Linus Torvalds5d2a2dbbc2005-11-29 14:07:55 -08002718 */
Kirill A. Shutemovc3e5ea62020-03-05 22:28:32 -08002719 if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) {
2720 /*
2721 * Give a warn in case there can be some obscure
2722 * use-case
2723 */
2724warn:
2725 WARN_ON_ONCE(1);
2726 clear_page(kaddr);
2727 }
Jia He83d116c2019-10-11 22:09:39 +08002728 }
2729
2730 ret = true;
2731
2732pte_unlock:
Kirill A. Shutemovc3e5ea62020-03-05 22:28:32 -08002733 if (locked)
Jia He83d116c2019-10-11 22:09:39 +08002734 pte_unmap_unlock(vmf->pte, vmf->ptl);
2735 kunmap_atomic(kaddr);
2736 flush_dcache_page(dst);
2737
2738 return ret;
Linus Torvalds6aab3412005-11-28 14:34:23 -08002739}
2740
Michal Hockoc20cd452016-01-14 15:20:12 -08002741static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma)
2742{
2743 struct file *vm_file = vma->vm_file;
2744
2745 if (vm_file)
2746 return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO;
2747
2748 /*
2749 * Special mappings (e.g. VDSO) do not have any file so fake
2750 * a default GFP_KERNEL for them.
2751 */
2752 return GFP_KERNEL;
2753}
2754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755/*
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002756 * Notify the address space that the page is about to become writable so that
2757 * it can prohibit this or wait for the page to get into an appropriate state.
2758 *
2759 * We do this without the lock held, so that it can sleep if it needs to.
2760 */
Souptick Joarder2b740302018-08-23 17:01:36 -07002761static vm_fault_t do_page_mkwrite(struct vm_fault *vmf)
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002762{
Souptick Joarder2b740302018-08-23 17:01:36 -07002763 vm_fault_t ret;
Jan Kara38b8cb72016-12-14 15:07:30 -08002764 struct page *page = vmf->page;
2765 unsigned int old_flags = vmf->flags;
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002766
Jan Kara38b8cb72016-12-14 15:07:30 -08002767 vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002768
Darrick J. Wongdc617f22019-08-20 07:55:16 -07002769 if (vmf->vma->vm_file &&
2770 IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host))
2771 return VM_FAULT_SIGBUS;
2772
Dave Jiang11bac802017-02-24 14:56:41 -08002773 ret = vmf->vma->vm_ops->page_mkwrite(vmf);
Jan Kara38b8cb72016-12-14 15:07:30 -08002774 /* Restore original flags so that caller is not surprised */
2775 vmf->flags = old_flags;
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07002776 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
2777 return ret;
2778 if (unlikely(!(ret & VM_FAULT_LOCKED))) {
2779 lock_page(page);
2780 if (!page->mapping) {
2781 unlock_page(page);
2782 return 0; /* retry */
2783 }
2784 ret |= VM_FAULT_LOCKED;
2785 } else
2786 VM_BUG_ON_PAGE(!PageLocked(page), page);
2787 return ret;
2788}
2789
2790/*
Jan Kara97ba0c22016-12-14 15:07:27 -08002791 * Handle dirtying of a page in shared file mapping on a write fault.
2792 *
2793 * The function expects the page to be locked and unlocks it.
2794 */
Johannes Weiner89b15332019-11-30 17:50:22 -08002795static vm_fault_t fault_dirty_shared_page(struct vm_fault *vmf)
Jan Kara97ba0c22016-12-14 15:07:27 -08002796{
Johannes Weiner89b15332019-11-30 17:50:22 -08002797 struct vm_area_struct *vma = vmf->vma;
Jan Kara97ba0c22016-12-14 15:07:27 -08002798 struct address_space *mapping;
Johannes Weiner89b15332019-11-30 17:50:22 -08002799 struct page *page = vmf->page;
Jan Kara97ba0c22016-12-14 15:07:27 -08002800 bool dirtied;
2801 bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite;
2802
2803 dirtied = set_page_dirty(page);
2804 VM_BUG_ON_PAGE(PageAnon(page), page);
2805 /*
2806 * Take a local copy of the address_space - page.mapping may be zeroed
2807 * by truncate after unlock_page(). The address_space itself remains
2808 * pinned by vma->vm_file's reference. We rely on unlock_page()'s
2809 * release semantics to prevent the compiler from undoing this copying.
2810 */
2811 mapping = page_rmapping(page);
2812 unlock_page(page);
2813
Jan Kara97ba0c22016-12-14 15:07:27 -08002814 if (!page_mkwrite)
2815 file_update_time(vma->vm_file);
Johannes Weiner89b15332019-11-30 17:50:22 -08002816
2817 /*
2818 * Throttle page dirtying rate down to writeback speed.
2819 *
2820 * mapping may be NULL here because some device drivers do not
2821 * set page.mapping but still dirty their pages
2822 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002823 * Drop the mmap_lock before waiting on IO, if we can. The file
Johannes Weiner89b15332019-11-30 17:50:22 -08002824 * is pinning the mapping, as per above.
2825 */
2826 if ((dirtied || page_mkwrite) && mapping) {
2827 struct file *fpin;
2828
2829 fpin = maybe_unlock_mmap_for_io(vmf, NULL);
2830 balance_dirty_pages_ratelimited(mapping);
2831 if (fpin) {
2832 fput(fpin);
2833 return VM_FAULT_RETRY;
2834 }
2835 }
2836
2837 return 0;
Jan Kara97ba0c22016-12-14 15:07:27 -08002838}
2839
2840/*
Shachar Raindel4e047f82015-04-14 15:46:25 -07002841 * Handle write page faults for pages that can be reused in the current vma
2842 *
2843 * This can happen either due to the mapping being with the VM_SHARED flag,
2844 * or due to us being the last reference standing to the page. In either
2845 * case, all we need to do here is to mark the page as writable and update
2846 * any related book-keeping.
2847 */
Jan Kara997dd982016-12-14 15:07:36 -08002848static inline void wp_page_reuse(struct vm_fault *vmf)
Jan Kara82b0f8c2016-12-14 15:06:58 -08002849 __releases(vmf->ptl)
Shachar Raindel4e047f82015-04-14 15:46:25 -07002850{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002851 struct vm_area_struct *vma = vmf->vma;
Jan Karaa41b70d2016-12-14 15:07:33 -08002852 struct page *page = vmf->page;
Shachar Raindel4e047f82015-04-14 15:46:25 -07002853 pte_t entry;
2854 /*
2855 * Clear the pages cpupid information as the existing
2856 * information potentially belongs to a now completely
2857 * unrelated process.
2858 */
2859 if (page)
2860 page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1);
2861
Jan Kara29943022016-12-14 15:07:16 -08002862 flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
2863 entry = pte_mkyoung(vmf->orig_pte);
Shachar Raindel4e047f82015-04-14 15:46:25 -07002864 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Jan Kara82b0f8c2016-12-14 15:06:58 -08002865 if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1))
2866 update_mmu_cache(vma, vmf->address, vmf->pte);
2867 pte_unmap_unlock(vmf->pte, vmf->ptl);
Peter Xu798a6b82020-08-21 19:49:58 -04002868 count_vm_event(PGREUSE);
Shachar Raindel4e047f82015-04-14 15:46:25 -07002869}
2870
2871/*
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002872 * Handle the case of a page which we actually need to copy to a new page.
2873 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002874 * Called with mmap_lock locked and the old page referenced, but
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002875 * without the ptl held.
2876 *
2877 * High level logic flow:
2878 *
2879 * - Allocate a page, copy the content of the old page to the new one.
2880 * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc.
2881 * - Take the PTL. If the pte changed, bail out and release the allocated page
2882 * - If the pte is still the way we remember it, update the page table and all
2883 * relevant references. This includes dropping the reference the page-table
2884 * held to the old page, as well as updating the rmap.
2885 * - In any case, unlock the PTL and drop the reference we took to the old page.
2886 */
Souptick Joarder2b740302018-08-23 17:01:36 -07002887static vm_fault_t wp_page_copy(struct vm_fault *vmf)
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002888{
Jan Kara82b0f8c2016-12-14 15:06:58 -08002889 struct vm_area_struct *vma = vmf->vma;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002890 struct mm_struct *mm = vma->vm_mm;
Jan Karaa41b70d2016-12-14 15:07:33 -08002891 struct page *old_page = vmf->page;
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002892 struct page *new_page = NULL;
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002893 pte_t entry;
2894 int page_copied = 0;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002895 struct mmu_notifier_range range;
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002896
2897 if (unlikely(anon_vma_prepare(vma)))
2898 goto oom;
2899
Jan Kara29943022016-12-14 15:07:16 -08002900 if (is_zero_pfn(pte_pfn(vmf->orig_pte))) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08002901 new_page = alloc_zeroed_user_highpage_movable(vma,
2902 vmf->address);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002903 if (!new_page)
2904 goto oom;
2905 } else {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002906 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
Jan Kara82b0f8c2016-12-14 15:06:58 -08002907 vmf->address);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002908 if (!new_page)
2909 goto oom;
Jia He83d116c2019-10-11 22:09:39 +08002910
2911 if (!cow_user_page(new_page, old_page, vmf)) {
2912 /*
2913 * COW failed, if the fault was solved by other,
2914 * it's fine. If not, userspace would re-fault on
2915 * the same address and we will handle the fault
2916 * from the second attempt.
2917 */
2918 put_page(new_page);
2919 if (old_page)
2920 put_page(old_page);
2921 return 0;
2922 }
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002923 }
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002924
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -07002925 if (mem_cgroup_charge(new_page, mm, GFP_KERNEL))
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002926 goto oom_free_new;
Johannes Weiner9d82c692020-06-03 16:02:04 -07002927 cgroup_throttle_swaprate(new_page, GFP_KERNEL);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002928
Mel Gormaneb3c24f2015-06-24 16:57:27 -07002929 __SetPageUptodate(new_page);
2930
Jérôme Glisse7269f992019-05-13 17:20:53 -07002931 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07002932 vmf->address & PAGE_MASK,
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002933 (vmf->address & PAGE_MASK) + PAGE_SIZE);
2934 mmu_notifier_invalidate_range_start(&range);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002935
2936 /*
2937 * Re-check the pte - we dropped the lock
2938 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002939 vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08002940 if (likely(pte_same(*vmf->pte, vmf->orig_pte))) {
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002941 if (old_page) {
2942 if (!PageAnon(old_page)) {
Jerome Marchandeca56ff2016-01-14 15:19:26 -08002943 dec_mm_counter_fast(mm,
2944 mm_counter_file(old_page));
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002945 inc_mm_counter_fast(mm, MM_ANONPAGES);
2946 }
2947 } else {
2948 inc_mm_counter_fast(mm, MM_ANONPAGES);
2949 }
Jan Kara29943022016-12-14 15:07:16 -08002950 flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002951 entry = mk_pte(new_page, vma->vm_page_prot);
Thomas Bogendoerfer50c25ee2021-06-04 20:01:08 -07002952 entry = pte_sw_mkyoung(entry);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002953 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Nicholas Piggin111fe712020-12-29 15:14:43 -08002954
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002955 /*
2956 * Clear the pte entry and flush it first, before updating the
Nicholas Piggin111fe712020-12-29 15:14:43 -08002957 * pte with the new entry, to keep TLBs on different CPUs in
2958 * sync. This code used to set the new PTE then flush TLBs, but
2959 * that left a window where the new PTE could be loaded into
2960 * some TLBs while the old PTE remains in others.
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002961 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002962 ptep_clear_flush_notify(vma, vmf->address, vmf->pte);
2963 page_add_new_anon_rmap(new_page, vma, vmf->address, false);
Joonsoo Kimb5181542020-08-11 18:30:40 -07002964 lru_cache_add_inactive_or_unevictable(new_page, vma);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002965 /*
2966 * We call the notify macro here because, when using secondary
2967 * mmu page tables (such as kvm shadow page tables), we want the
2968 * new page to be mapped directly into the secondary page table.
2969 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08002970 set_pte_at_notify(mm, vmf->address, vmf->pte, entry);
2971 update_mmu_cache(vma, vmf->address, vmf->pte);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002972 if (old_page) {
2973 /*
2974 * Only after switching the pte to the new page may
2975 * we remove the mapcount here. Otherwise another
2976 * process may come and find the rmap count decremented
2977 * before the pte is switched to the new page, and
2978 * "reuse" the old page writing into it while our pte
2979 * here still points into it and can be read by other
2980 * threads.
2981 *
2982 * The critical issue is to order this
2983 * page_remove_rmap with the ptp_clear_flush above.
2984 * Those stores are ordered by (if nothing else,)
2985 * the barrier present in the atomic_add_negative
2986 * in page_remove_rmap.
2987 *
2988 * Then the TLB flush in ptep_clear_flush ensures that
2989 * no process can access the old page before the
2990 * decremented mapcount is visible. And the old page
2991 * cannot be reused until after the decremented
2992 * mapcount is visible. So transitively, TLBs to
2993 * old page will be flushed before it can be reused.
2994 */
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002995 page_remove_rmap(old_page, false);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07002996 }
2997
2998 /* Free the old page.. */
2999 new_page = old_page;
3000 page_copied = 1;
3001 } else {
Bibo Mao7df67692020-05-27 10:25:18 +08003002 update_mmu_tlb(vma, vmf->address, vmf->pte);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07003003 }
3004
3005 if (new_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003006 put_page(new_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07003007
Jan Kara82b0f8c2016-12-14 15:06:58 -08003008 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jérôme Glisse4645b9f2017-11-15 17:34:11 -08003009 /*
3010 * No need to double call mmu_notifier->invalidate_range() callback as
3011 * the above ptep_clear_flush_notify() did already call it.
3012 */
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08003013 mmu_notifier_invalidate_range_only_end(&range);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07003014 if (old_page) {
3015 /*
3016 * Don't let another task, with possibly unlocked vma,
3017 * keep the mlocked page.
3018 */
3019 if (page_copied && (vma->vm_flags & VM_LOCKED)) {
3020 lock_page(old_page); /* LRU manipulation */
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08003021 if (PageMlocked(old_page))
3022 munlock_vma_page(old_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07003023 unlock_page(old_page);
3024 }
Huang Yingf4c4a3f2021-06-28 19:37:12 -07003025 if (page_copied)
3026 free_swap_cache(old_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003027 put_page(old_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07003028 }
3029 return page_copied ? VM_FAULT_WRITE : 0;
3030oom_free_new:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003031 put_page(new_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07003032oom:
3033 if (old_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003034 put_page(old_page);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07003035 return VM_FAULT_OOM;
3036}
3037
Jan Kara66a61972016-12-14 15:07:39 -08003038/**
3039 * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE
3040 * writeable once the page is prepared
3041 *
3042 * @vmf: structure describing the fault
3043 *
3044 * This function handles all that is needed to finish a write page fault in a
3045 * shared mapping due to PTE being read-only once the mapped page is prepared.
Mike Rapoporta862f682019-03-05 15:48:42 -08003046 * It handles locking of PTE and modifying it.
Jan Kara66a61972016-12-14 15:07:39 -08003047 *
3048 * The function expects the page to be locked or other protection against
3049 * concurrent faults / writeback (such as DAX radix tree locks).
Mike Rapoporta862f682019-03-05 15:48:42 -08003050 *
Liu Xiang2797e792021-06-28 19:38:47 -07003051 * Return: %0 on success, %VM_FAULT_NOPAGE when PTE got changed before
Mike Rapoporta862f682019-03-05 15:48:42 -08003052 * we acquired PTE lock.
Jan Kara66a61972016-12-14 15:07:39 -08003053 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003054vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf)
Jan Kara66a61972016-12-14 15:07:39 -08003055{
3056 WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED));
3057 vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address,
3058 &vmf->ptl);
3059 /*
3060 * We might have raced with another page fault while we released the
3061 * pte_offset_map_lock.
3062 */
3063 if (!pte_same(*vmf->pte, vmf->orig_pte)) {
Bibo Mao7df67692020-05-27 10:25:18 +08003064 update_mmu_tlb(vmf->vma, vmf->address, vmf->pte);
Jan Kara66a61972016-12-14 15:07:39 -08003065 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karaa19e2552016-12-14 15:07:42 -08003066 return VM_FAULT_NOPAGE;
Jan Kara66a61972016-12-14 15:07:39 -08003067 }
3068 wp_page_reuse(vmf);
Jan Karaa19e2552016-12-14 15:07:42 -08003069 return 0;
Jan Kara66a61972016-12-14 15:07:39 -08003070}
3071
Boaz Harroshdd906182015-04-15 16:15:11 -07003072/*
3073 * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED
3074 * mapping
3075 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003076static vm_fault_t wp_pfn_shared(struct vm_fault *vmf)
Boaz Harroshdd906182015-04-15 16:15:11 -07003077{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003078 struct vm_area_struct *vma = vmf->vma;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003079
Boaz Harroshdd906182015-04-15 16:15:11 -07003080 if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) {
Souptick Joarder2b740302018-08-23 17:01:36 -07003081 vm_fault_t ret;
Boaz Harroshdd906182015-04-15 16:15:11 -07003082
Jan Kara82b0f8c2016-12-14 15:06:58 -08003083 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karafe822212016-12-14 15:07:13 -08003084 vmf->flags |= FAULT_FLAG_MKWRITE;
Dave Jiang11bac802017-02-24 14:56:41 -08003085 ret = vma->vm_ops->pfn_mkwrite(vmf);
Jan Kara2f89dc12016-12-14 15:07:50 -08003086 if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))
Boaz Harroshdd906182015-04-15 16:15:11 -07003087 return ret;
Jan Kara66a61972016-12-14 15:07:39 -08003088 return finish_mkwrite_fault(vmf);
Boaz Harroshdd906182015-04-15 16:15:11 -07003089 }
Jan Kara997dd982016-12-14 15:07:36 -08003090 wp_page_reuse(vmf);
3091 return VM_FAULT_WRITE;
Boaz Harroshdd906182015-04-15 16:15:11 -07003092}
3093
Souptick Joarder2b740302018-08-23 17:01:36 -07003094static vm_fault_t wp_page_shared(struct vm_fault *vmf)
Jan Kara82b0f8c2016-12-14 15:06:58 -08003095 __releases(vmf->ptl)
Shachar Raindel93e478d2015-04-14 15:46:35 -07003096{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003097 struct vm_area_struct *vma = vmf->vma;
Johannes Weiner89b15332019-11-30 17:50:22 -08003098 vm_fault_t ret = VM_FAULT_WRITE;
Shachar Raindel93e478d2015-04-14 15:46:35 -07003099
Jan Karaa41b70d2016-12-14 15:07:33 -08003100 get_page(vmf->page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07003101
Shachar Raindel93e478d2015-04-14 15:46:35 -07003102 if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
Souptick Joarder2b740302018-08-23 17:01:36 -07003103 vm_fault_t tmp;
Shachar Raindel93e478d2015-04-14 15:46:35 -07003104
Jan Kara82b0f8c2016-12-14 15:06:58 -08003105 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Kara38b8cb72016-12-14 15:07:30 -08003106 tmp = do_page_mkwrite(vmf);
Shachar Raindel93e478d2015-04-14 15:46:35 -07003107 if (unlikely(!tmp || (tmp &
3108 (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
Jan Karaa41b70d2016-12-14 15:07:33 -08003109 put_page(vmf->page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07003110 return tmp;
3111 }
Jan Kara66a61972016-12-14 15:07:39 -08003112 tmp = finish_mkwrite_fault(vmf);
Jan Karaa19e2552016-12-14 15:07:42 -08003113 if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
Jan Karaa41b70d2016-12-14 15:07:33 -08003114 unlock_page(vmf->page);
Jan Karaa41b70d2016-12-14 15:07:33 -08003115 put_page(vmf->page);
Jan Kara66a61972016-12-14 15:07:39 -08003116 return tmp;
Shachar Raindel93e478d2015-04-14 15:46:35 -07003117 }
Jan Kara66a61972016-12-14 15:07:39 -08003118 } else {
3119 wp_page_reuse(vmf);
Jan Kara997dd982016-12-14 15:07:36 -08003120 lock_page(vmf->page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07003121 }
Johannes Weiner89b15332019-11-30 17:50:22 -08003122 ret |= fault_dirty_shared_page(vmf);
Jan Kara997dd982016-12-14 15:07:36 -08003123 put_page(vmf->page);
Shachar Raindel93e478d2015-04-14 15:46:35 -07003124
Johannes Weiner89b15332019-11-30 17:50:22 -08003125 return ret;
Shachar Raindel93e478d2015-04-14 15:46:35 -07003126}
3127
Shachar Raindel2f38ab22015-04-14 15:46:32 -07003128/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 * This routine handles present pages, when users try to write
3130 * to a shared page. It is done by copying the page to a new address
3131 * and decrementing the shared-page counter for the old page.
3132 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 * Note that this routine assumes that the protection checks have been
3134 * done by the caller (the low-level page fault routine in most cases).
3135 * Thus we can safely just mark it writable once we've done any necessary
3136 * COW.
3137 *
3138 * We also mark the page dirty at this point even though the page will
3139 * change only once the write actually happens. This avoids a few races,
3140 * and potentially makes it more efficient.
3141 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003142 * We enter with non-exclusive mmap_lock (to exclude vma changes,
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003143 * but allow concurrent faults), with pte both mapped and locked.
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003144 * We return with mmap_lock still held, but pte unmapped and unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003146static vm_fault_t do_wp_page(struct vm_fault *vmf)
Jan Kara82b0f8c2016-12-14 15:06:58 -08003147 __releases(vmf->ptl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003149 struct vm_area_struct *vma = vmf->vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Peter Xu292924b2020-04-06 20:05:49 -07003151 if (userfaultfd_pte_wp(vma, *vmf->pte)) {
Andrea Arcangeli529b9302020-04-06 20:05:29 -07003152 pte_unmap_unlock(vmf->pte, vmf->ptl);
3153 return handle_userfault(vmf, VM_UFFD_WP);
3154 }
3155
Nadav Amit6ce64422021-03-12 21:08:17 -08003156 /*
3157 * Userfaultfd write-protect can defer flushes. Ensure the TLB
3158 * is flushed in this case before copying.
3159 */
3160 if (unlikely(userfaultfd_wp(vmf->vma) &&
3161 mm_tlb_flush_pending(vmf->vma->vm_mm)))
3162 flush_tlb_page(vmf->vma, vmf->address);
3163
Jan Karaa41b70d2016-12-14 15:07:33 -08003164 vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte);
3165 if (!vmf->page) {
Peter Zijlstra251b97f2008-07-04 09:59:24 -07003166 /*
Peter Feiner64e455072014-10-13 15:55:46 -07003167 * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
3168 * VM_PFNMAP VMA.
Peter Zijlstra251b97f2008-07-04 09:59:24 -07003169 *
3170 * We should not cow pages in a shared writeable mapping.
Boaz Harroshdd906182015-04-15 16:15:11 -07003171 * Just mark the pages writable and/or call ops->pfn_mkwrite.
Peter Zijlstra251b97f2008-07-04 09:59:24 -07003172 */
3173 if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
3174 (VM_WRITE|VM_SHARED))
Jan Kara29943022016-12-14 15:07:16 -08003175 return wp_pfn_shared(vmf);
Shachar Raindel2f38ab22015-04-14 15:46:32 -07003176
Jan Kara82b0f8c2016-12-14 15:06:58 -08003177 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karaa41b70d2016-12-14 15:07:33 -08003178 return wp_page_copy(vmf);
Peter Zijlstra251b97f2008-07-04 09:59:24 -07003179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Peter Zijlstrad08b3852006-09-25 23:30:57 -07003181 /*
Peter Zijlstraee6a6452006-09-25 23:31:00 -07003182 * Take out anonymous pages first, anonymous shared vmas are
3183 * not dirty accountable.
Peter Zijlstrad08b3852006-09-25 23:30:57 -07003184 */
Kirill Tkhai52d1e602019-03-05 15:43:06 -08003185 if (PageAnon(vmf->page)) {
Linus Torvalds09854ba2020-08-21 19:49:55 -04003186 struct page *page = vmf->page;
3187
3188 /* PageKsm() doesn't necessarily raise the page refcount */
3189 if (PageKsm(page) || page_count(page) != 1)
Kirill Tkhai52d1e602019-03-05 15:43:06 -08003190 goto copy;
Linus Torvalds09854ba2020-08-21 19:49:55 -04003191 if (!trylock_page(page))
3192 goto copy;
3193 if (PageKsm(page) || page_mapcount(page) != 1 || page_count(page) != 1) {
3194 unlock_page(page);
3195 goto copy;
Peter Zijlstraee6a6452006-09-25 23:31:00 -07003196 }
Linus Torvalds09854ba2020-08-21 19:49:55 -04003197 /*
3198 * Ok, we've got the only map reference, and the only
3199 * page count reference, and the page is locked,
3200 * it's dark out, and we're wearing sunglasses. Hit it.
3201 */
Linus Torvalds09854ba2020-08-21 19:49:55 -04003202 unlock_page(page);
Linus Torvaldsbe068f22020-09-24 08:41:32 -07003203 wp_page_reuse(vmf);
Linus Torvalds09854ba2020-08-21 19:49:55 -04003204 return VM_FAULT_WRITE;
Peter Zijlstraee6a6452006-09-25 23:31:00 -07003205 } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
Peter Zijlstrad08b3852006-09-25 23:30:57 -07003206 (VM_WRITE|VM_SHARED))) {
Jan Karaa41b70d2016-12-14 15:07:33 -08003207 return wp_page_shared(vmf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 }
Kirill Tkhai52d1e602019-03-05 15:43:06 -08003209copy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 /*
3211 * Ok, we need to copy. Oh, well..
3212 */
Jan Karaa41b70d2016-12-14 15:07:33 -08003213 get_page(vmf->page);
Shachar Raindel28766802015-04-14 15:46:29 -07003214
Jan Kara82b0f8c2016-12-14 15:06:58 -08003215 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Karaa41b70d2016-12-14 15:07:33 -08003216 return wp_page_copy(vmf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217}
3218
Peter Zijlstra97a89412011-05-24 17:12:04 -07003219static void unmap_mapping_range_vma(struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 unsigned long start_addr, unsigned long end_addr,
3221 struct zap_details *details)
3222{
Al Virof5cc4ee2012-03-05 14:14:20 -05003223 zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224}
3225
Davidlohr Buesof808c132017-09-08 16:15:08 -07003226static inline void unmap_mapping_range_tree(struct rb_root_cached *root,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 struct zap_details *details)
3228{
3229 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 pgoff_t vba, vea, zba, zea;
3231
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07003232 vma_interval_tree_foreach(vma, root,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 details->first_index, details->last_index) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
3235 vba = vma->vm_pgoff;
Libind6e93212013-07-03 15:01:26 -07003236 vea = vba + vma_pages(vma) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 zba = details->first_index;
3238 if (zba < vba)
3239 zba = vba;
3240 zea = details->last_index;
3241 if (zea > vea)
3242 zea = vea;
3243
Peter Zijlstra97a89412011-05-24 17:12:04 -07003244 unmap_mapping_range_vma(vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
3246 ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
Peter Zijlstra97a89412011-05-24 17:12:04 -07003247 details);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 }
3249}
3250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251/**
Hugh Dickins22061a12021-06-15 18:24:03 -07003252 * unmap_mapping_page() - Unmap single page from processes.
3253 * @page: The locked page to be unmapped.
3254 *
3255 * Unmap this page from any userspace process which still has it mmaped.
3256 * Typically, for efficiency, the range of nearby pages has already been
3257 * unmapped by unmap_mapping_pages() or unmap_mapping_range(). But once
3258 * truncation or invalidation holds the lock on a page, it may find that
3259 * the page has been remapped again: and then uses unmap_mapping_page()
3260 * to unmap it finally.
3261 */
3262void unmap_mapping_page(struct page *page)
3263{
3264 struct address_space *mapping = page->mapping;
3265 struct zap_details details = { };
3266
3267 VM_BUG_ON(!PageLocked(page));
3268 VM_BUG_ON(PageTail(page));
3269
3270 details.check_mapping = mapping;
3271 details.first_index = page->index;
3272 details.last_index = page->index + thp_nr_pages(page) - 1;
3273 details.single_page = page;
3274
3275 i_mmap_lock_write(mapping);
3276 if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)))
3277 unmap_mapping_range_tree(&mapping->i_mmap, &details);
3278 i_mmap_unlock_write(mapping);
3279}
3280
3281/**
Matthew Wilcox977fbdc2018-01-31 16:17:36 -08003282 * unmap_mapping_pages() - Unmap pages from processes.
3283 * @mapping: The address space containing pages to be unmapped.
3284 * @start: Index of first page to be unmapped.
3285 * @nr: Number of pages to be unmapped. 0 to unmap to end of file.
3286 * @even_cows: Whether to unmap even private COWed pages.
3287 *
3288 * Unmap the pages in this address space from any userspace process which
3289 * has them mmaped. Generally, you want to remove COWed pages as well when
3290 * a file is being truncated, but not when invalidating pages from the page
3291 * cache.
3292 */
3293void unmap_mapping_pages(struct address_space *mapping, pgoff_t start,
3294 pgoff_t nr, bool even_cows)
3295{
3296 struct zap_details details = { };
3297
3298 details.check_mapping = even_cows ? NULL : mapping;
3299 details.first_index = start;
3300 details.last_index = start + nr - 1;
3301 if (details.last_index < details.first_index)
3302 details.last_index = ULONG_MAX;
3303
3304 i_mmap_lock_write(mapping);
3305 if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)))
3306 unmap_mapping_range_tree(&mapping->i_mmap, &details);
3307 i_mmap_unlock_write(mapping);
3308}
3309
3310/**
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08003311 * unmap_mapping_range - unmap the portion of all mmaps in the specified
Matthew Wilcox977fbdc2018-01-31 16:17:36 -08003312 * address_space corresponding to the specified byte range in the underlying
Kirill A. Shutemov8a5f14a2015-02-10 14:09:49 -08003313 * file.
3314 *
Martin Waitz3d410882005-06-23 22:05:21 -07003315 * @mapping: the address space containing mmaps to be unmapped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 * @holebegin: byte in first page to unmap, relative to the start of
3317 * the underlying file. This will be rounded down to a PAGE_SIZE
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +10003318 * boundary. Note that this is different from truncate_pagecache(), which
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 * must keep the partial page. In contrast, we must get rid of
3320 * partial pages.
3321 * @holelen: size of prospective hole in bytes. This will be rounded
3322 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
3323 * end of the file.
3324 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
3325 * but 0 when invalidating pagecache, don't throw away private data.
3326 */
3327void unmap_mapping_range(struct address_space *mapping,
3328 loff_t const holebegin, loff_t const holelen, int even_cows)
3329{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 pgoff_t hba = holebegin >> PAGE_SHIFT;
3331 pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3332
3333 /* Check for overflow. */
3334 if (sizeof(holelen) > sizeof(hlen)) {
3335 long long holeend =
3336 (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3337 if (holeend & ~(long long)ULONG_MAX)
3338 hlen = ULONG_MAX - hba + 1;
3339 }
3340
Matthew Wilcox977fbdc2018-01-31 16:17:36 -08003341 unmap_mapping_pages(mapping, hba, hlen, even_cows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342}
3343EXPORT_SYMBOL(unmap_mapping_range);
3344
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345/*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003346 * We enter with non-exclusive mmap_lock (to exclude vma changes,
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003347 * but allow concurrent faults), and pte mapped but not yet locked.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003348 * We return with pte unmapped and unlocked.
3349 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003350 * We return with the mmap_lock locked or unlocked in the same cases
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003351 * as does filemap_fault().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003353vm_fault_t do_swap_page(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003355 struct vm_area_struct *vma = vmf->vma;
Minchan Kimeaf649eb2018-04-05 16:23:39 -07003356 struct page *page = NULL, *swapcache;
Miaohe Lin2799e772021-06-28 19:36:50 -07003357 struct swap_info_struct *si = NULL;
Hugh Dickins65500d22005-10-29 18:15:59 -07003358 swp_entry_t entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 pte_t pte;
Michel Lespinassed065bd82010-10-26 14:21:57 -07003360 int locked;
Rik van Rielad8c2ee2010-08-09 17:19:48 -07003361 int exclusive = 0;
Souptick Joarder2b740302018-08-23 17:01:36 -07003362 vm_fault_t ret = 0;
Joonsoo Kimaae466b2020-08-11 18:30:50 -07003363 void *shadow = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
Minchan Kimeaf649eb2018-04-05 16:23:39 -07003365 if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte))
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003366 goto out;
Hugh Dickins65500d22005-10-29 18:15:59 -07003367
Jan Kara29943022016-12-14 15:07:16 -08003368 entry = pte_to_swp_entry(vmf->orig_pte);
Andi Kleend1737fd2009-09-16 11:50:06 +02003369 if (unlikely(non_swap_entry(entry))) {
3370 if (is_migration_entry(entry)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003371 migration_entry_wait(vma->vm_mm, vmf->pmd,
3372 vmf->address);
Jérôme Glisse5042db42017-09-08 16:11:43 -07003373 } else if (is_device_private_entry(entry)) {
Christoph Hellwig897e6362019-06-26 14:27:11 +02003374 vmf->page = device_private_entry_to_page(entry);
3375 ret = vmf->page->pgmap->ops->migrate_to_ram(vmf);
Andi Kleend1737fd2009-09-16 11:50:06 +02003376 } else if (is_hwpoison_entry(entry)) {
3377 ret = VM_FAULT_HWPOISON;
3378 } else {
Jan Kara29943022016-12-14 15:07:16 -08003379 print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL);
Hugh Dickinsd99be1a2009-12-14 17:59:04 -08003380 ret = VM_FAULT_SIGBUS;
Andi Kleend1737fd2009-09-16 11:50:06 +02003381 }
Christoph Lameter06972122006-06-23 02:03:35 -07003382 goto out;
3383 }
Minchan Kim0bcac062017-11-15 17:33:07 -08003384
Miaohe Lin2799e772021-06-28 19:36:50 -07003385 /* Prevent swapoff from happening to us. */
3386 si = get_swap_device(entry);
3387 if (unlikely(!si))
3388 goto out;
Minchan Kim0bcac062017-11-15 17:33:07 -08003389
Yafang Shao3d1c7fd2021-05-06 18:05:00 -07003390 delayacct_set_flag(current, DELAYACCT_PF_SWAPIN);
Minchan Kimeaf649eb2018-04-05 16:23:39 -07003391 page = lookup_swap_cache(entry, vma, vmf->address);
3392 swapcache = page;
Minchan Kimf8020772018-01-18 16:33:50 -08003393
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 if (!page) {
Qian Caia449bf52020-08-14 17:31:31 -07003395 if (data_race(si->flags & SWP_SYNCHRONOUS_IO) &&
3396 __swap_count(entry) == 1) {
Minchan Kim0bcac062017-11-15 17:33:07 -08003397 /* skip swapcache */
Minchan Kime9e9b7e2018-04-05 16:23:42 -07003398 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
3399 vmf->address);
Minchan Kim0bcac062017-11-15 17:33:07 -08003400 if (page) {
3401 __SetPageLocked(page);
3402 __SetPageSwapBacked(page);
Johannes Weiner4c6355b2020-06-03 16:02:17 -07003403
Shakeel Butt0add0c72021-04-29 22:56:36 -07003404 if (mem_cgroup_swapin_charge_page(page,
3405 vma->vm_mm, GFP_KERNEL, entry)) {
Michal Hocko545b1b02020-06-25 20:29:21 -07003406 ret = VM_FAULT_OOM;
Johannes Weiner4c6355b2020-06-03 16:02:17 -07003407 goto out_page;
Michal Hocko545b1b02020-06-25 20:29:21 -07003408 }
Shakeel Butt0add0c72021-04-29 22:56:36 -07003409 mem_cgroup_swapin_uncharge_swap(entry);
Johannes Weiner4c6355b2020-06-03 16:02:17 -07003410
Joonsoo Kimaae466b2020-08-11 18:30:50 -07003411 shadow = get_shadow_from_swap_cache(entry);
3412 if (shadow)
3413 workingset_refault(page, shadow);
Joonsoo Kim0076f022020-06-25 20:30:37 -07003414
Johannes Weiner6058eae2020-06-03 16:02:40 -07003415 lru_cache_add(page);
Shakeel Butt0add0c72021-04-29 22:56:36 -07003416
3417 /* To provide entry to swap_readpage() */
3418 set_page_private(page, entry.val);
Minchan Kim0bcac062017-11-15 17:33:07 -08003419 swap_readpage(page, true);
Shakeel Butt0add0c72021-04-29 22:56:36 -07003420 set_page_private(page, 0);
Minchan Kim0bcac062017-11-15 17:33:07 -08003421 }
Minchan Kimaa8d22a2017-11-15 17:33:11 -08003422 } else {
Minchan Kime9e9b7e2018-04-05 16:23:42 -07003423 page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
3424 vmf);
Minchan Kimaa8d22a2017-11-15 17:33:11 -08003425 swapcache = page;
Minchan Kim0bcac062017-11-15 17:33:07 -08003426 }
3427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 if (!page) {
3429 /*
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003430 * Back out if somebody else faulted in this pte
3431 * while we released the pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003433 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
3434 vmf->address, &vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08003435 if (likely(pte_same(*vmf->pte, vmf->orig_pte)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 ret = VM_FAULT_OOM;
Yafang Shao3d1c7fd2021-05-06 18:05:00 -07003437 delayacct_clear_flag(current, DELAYACCT_PF_SWAPIN);
Hugh Dickins65500d22005-10-29 18:15:59 -07003438 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 }
3440
3441 /* Had to read the page from swap area: Major fault */
3442 ret = VM_FAULT_MAJOR;
Christoph Lameterf8891e52006-06-30 01:55:45 -07003443 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07003444 count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
Andi Kleend1737fd2009-09-16 11:50:06 +02003445 } else if (PageHWPoison(page)) {
Wu Fengguang71f72522009-12-16 12:19:58 +01003446 /*
3447 * hwpoisoned dirty swapcache pages are kept for killing
3448 * owner processes (which may be unknown at hwpoison time)
3449 */
Andi Kleend1737fd2009-09-16 11:50:06 +02003450 ret = VM_FAULT_HWPOISON;
Yafang Shao3d1c7fd2021-05-06 18:05:00 -07003451 delayacct_clear_flag(current, DELAYACCT_PF_SWAPIN);
Andi Kleen4779cb32009-10-14 01:51:41 +02003452 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 }
3454
Jan Kara82b0f8c2016-12-14 15:06:58 -08003455 locked = lock_page_or_retry(page, vma->vm_mm, vmf->flags);
Rik van Riele709ffd2012-05-29 15:06:18 -07003456
Yafang Shao3d1c7fd2021-05-06 18:05:00 -07003457 delayacct_clear_flag(current, DELAYACCT_PF_SWAPIN);
Michel Lespinassed065bd82010-10-26 14:21:57 -07003458 if (!locked) {
3459 ret |= VM_FAULT_RETRY;
3460 goto out_release;
3461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003463 /*
Hugh Dickins31c4a3d2010-09-19 19:40:22 -07003464 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
3465 * release the swapcache from under us. The page pin, and pte_same
3466 * test below, are not enough to exclude that. Even if it is still
3467 * swapcache, we need to check that the page's swap has not changed.
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003468 */
Minchan Kim0bcac062017-11-15 17:33:07 -08003469 if (unlikely((!PageSwapCache(page) ||
3470 page_private(page) != entry.val)) && swapcache)
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003471 goto out_page;
3472
Jan Kara82b0f8c2016-12-14 15:06:58 -08003473 page = ksm_might_need_to_copy(page, vma, vmf->address);
Hugh Dickinscbf86cf2013-02-22 16:35:08 -08003474 if (unlikely(!page)) {
3475 ret = VM_FAULT_OOM;
3476 page = swapcache;
Hugh Dickinscbf86cf2013-02-22 16:35:08 -08003477 goto out_page;
Hugh Dickins5ad64682009-12-14 17:59:24 -08003478 }
3479
Johannes Weiner9d82c692020-06-03 16:02:04 -07003480 cgroup_throttle_swaprate(page, GFP_KERNEL);
KAMEZAWA Hiroyuki073e5872008-10-18 20:28:08 -07003481
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 /*
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003483 * Back out if somebody else already faulted in this pte.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003485 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3486 &vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08003487 if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte)))
Kirill Korotaevb8107482005-05-16 21:53:50 -07003488 goto out_nomap;
Kirill Korotaevb8107482005-05-16 21:53:50 -07003489
3490 if (unlikely(!PageUptodate(page))) {
3491 ret = VM_FAULT_SIGBUS;
3492 goto out_nomap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 }
3494
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003495 /*
3496 * The page isn't present yet, go ahead with the fault.
3497 *
3498 * Be careful about the sequence of operations here.
3499 * To get its accounting right, reuse_swap_page() must be called
3500 * while the page is counted on swap but not yet in mapcount i.e.
3501 * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
3502 * must be called after the swap_free(), or it will never succeed.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003503 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003505 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
3506 dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 pte = mk_pte(page, vma->vm_page_prot);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003508 if ((vmf->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003510 vmf->flags &= ~FAULT_FLAG_WRITE;
Andrea Arcangeli9a5b4892010-08-09 17:19:49 -07003511 ret |= VM_FAULT_WRITE;
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08003512 exclusive = RMAP_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 flush_icache_page(vma, page);
Jan Kara29943022016-12-14 15:07:16 -08003515 if (pte_swp_soft_dirty(vmf->orig_pte))
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07003516 pte = pte_mksoft_dirty(pte);
Peter Xuf45ec5f2020-04-06 20:06:01 -07003517 if (pte_swp_uffd_wp(vmf->orig_pte)) {
3518 pte = pte_mkuffd_wp(pte);
3519 pte = pte_wrprotect(pte);
3520 }
Jan Kara82b0f8c2016-12-14 15:06:58 -08003521 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte);
Khalid Azizca827d52018-02-21 10:15:44 -07003522 arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte);
Jan Kara29943022016-12-14 15:07:16 -08003523 vmf->orig_pte = pte;
Minchan Kim0bcac062017-11-15 17:33:07 -08003524
3525 /* ksm created a completely new copy */
3526 if (unlikely(page != swapcache && swapcache)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003527 page_add_new_anon_rmap(page, vma, vmf->address, false);
Joonsoo Kimb5181542020-08-11 18:30:40 -07003528 lru_cache_add_inactive_or_unevictable(page, vma);
Minchan Kim0bcac062017-11-15 17:33:07 -08003529 } else {
3530 do_page_add_anon_rmap(page, vma, vmf->address, exclusive);
Johannes Weiner00501b52014-08-08 14:19:20 -07003531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07003533 swap_free(entry);
Vladimir Davydov5ccc5ab2016-01-20 15:03:10 -08003534 if (mem_cgroup_swap_full(page) ||
3535 (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -08003536 try_to_free_swap(page);
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07003537 unlock_page(page);
Minchan Kim0bcac062017-11-15 17:33:07 -08003538 if (page != swapcache && swapcache) {
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003539 /*
3540 * Hold the lock to avoid the swap entry to be reused
3541 * until we take the PT lock for the pte_same() check
3542 * (to avoid false positives from pte_same). For
3543 * further safety release the lock after the swap_free
3544 * so that the swap count won't change under a
3545 * parallel locked swapcache.
3546 */
3547 unlock_page(swapcache);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003548 put_page(swapcache);
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003549 }
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07003550
Jan Kara82b0f8c2016-12-14 15:06:58 -08003551 if (vmf->flags & FAULT_FLAG_WRITE) {
Jan Kara29943022016-12-14 15:07:16 -08003552 ret |= do_wp_page(vmf);
Hugh Dickins61469f12008-03-04 14:29:04 -08003553 if (ret & VM_FAULT_ERROR)
3554 ret &= VM_FAULT_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 goto out;
3556 }
3557
3558 /* No need to invalidate - it was non-present before */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003559 update_mmu_cache(vma, vmf->address, vmf->pte);
Hugh Dickins65500d22005-10-29 18:15:59 -07003560unlock:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003561 pte_unmap_unlock(vmf->pte, vmf->ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562out:
Miaohe Lin2799e772021-06-28 19:36:50 -07003563 if (si)
3564 put_swap_device(si);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 return ret;
Kirill Korotaevb8107482005-05-16 21:53:50 -07003566out_nomap:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003567 pte_unmap_unlock(vmf->pte, vmf->ptl);
Johannes Weinerbc43f752009-04-30 15:08:08 -07003568out_page:
Kirill Korotaevb8107482005-05-16 21:53:50 -07003569 unlock_page(page);
Andi Kleen4779cb32009-10-14 01:51:41 +02003570out_release:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003571 put_page(page);
Minchan Kim0bcac062017-11-15 17:33:07 -08003572 if (page != swapcache && swapcache) {
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003573 unlock_page(swapcache);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003574 put_page(swapcache);
Andrea Arcangeli4969c112010-09-09 16:37:52 -07003575 }
Miaohe Lin2799e772021-06-28 19:36:50 -07003576 if (si)
3577 put_swap_device(si);
Hugh Dickins65500d22005-10-29 18:15:59 -07003578 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579}
3580
3581/*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003582 * We enter with non-exclusive mmap_lock (to exclude vma changes,
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003583 * but allow concurrent faults), and pte mapped but not yet locked.
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003584 * We return with mmap_lock still held, but pte unmapped and unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003586static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003588 struct vm_area_struct *vma = vmf->vma;
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003589 struct page *page;
Souptick Joarder2b740302018-08-23 17:01:36 -07003590 vm_fault_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 pte_t entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592
Kirill A. Shutemov6b7339f2015-07-06 23:18:37 +03003593 /* File mapping without ->vm_ops ? */
3594 if (vma->vm_flags & VM_SHARED)
3595 return VM_FAULT_SIGBUS;
3596
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003597 /*
3598 * Use pte_alloc() instead of pte_alloc_map(). We can't run
3599 * pte_offset_map() on pmds where a huge pmd might be created
3600 * from a different thread.
3601 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07003602 * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003603 * parallel threads are excluded by other means.
3604 *
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07003605 * Here we only have mmap_read_lock(mm).
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003606 */
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08003607 if (pte_alloc(vma->vm_mm, vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003608 return VM_FAULT_OOM;
3609
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003610 /* See comment in handle_pte_fault() */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003611 if (unlikely(pmd_trans_unstable(vmf->pmd)))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003612 return 0;
3613
Linus Torvalds11ac5522010-08-14 11:44:56 -07003614 /* Use the zero-page for reads */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003615 if (!(vmf->flags & FAULT_FLAG_WRITE) &&
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003616 !mm_forbids_zeropage(vma->vm_mm)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003617 entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address),
Hugh Dickins62eede62009-09-21 17:03:34 -07003618 vma->vm_page_prot));
Jan Kara82b0f8c2016-12-14 15:06:58 -08003619 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
3620 vmf->address, &vmf->ptl);
Bibo Mao7df67692020-05-27 10:25:18 +08003621 if (!pte_none(*vmf->pte)) {
3622 update_mmu_tlb(vma, vmf->address, vmf->pte);
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -07003623 goto unlock;
Bibo Mao7df67692020-05-27 10:25:18 +08003624 }
Michal Hocko6b31d592017-08-18 15:16:15 -07003625 ret = check_stable_address_space(vma->vm_mm);
3626 if (ret)
3627 goto unlock;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07003628 /* Deliver the page fault to userland, check inside PT lock */
3629 if (userfaultfd_missing(vma)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003630 pte_unmap_unlock(vmf->pte, vmf->ptl);
3631 return handle_userfault(vmf, VM_UFFD_MISSING);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07003632 }
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -07003633 goto setpte;
3634 }
3635
Nick Piggin557ed1f2007-10-16 01:24:40 -07003636 /* Allocate our own private page. */
Nick Piggin557ed1f2007-10-16 01:24:40 -07003637 if (unlikely(anon_vma_prepare(vma)))
3638 goto oom;
Jan Kara82b0f8c2016-12-14 15:06:58 -08003639 page = alloc_zeroed_user_highpage_movable(vma, vmf->address);
Nick Piggin557ed1f2007-10-16 01:24:40 -07003640 if (!page)
3641 goto oom;
Mel Gormaneb3c24f2015-06-24 16:57:27 -07003642
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -07003643 if (mem_cgroup_charge(page, vma->vm_mm, GFP_KERNEL))
Mel Gormaneb3c24f2015-06-24 16:57:27 -07003644 goto oom_free_page;
Johannes Weiner9d82c692020-06-03 16:02:04 -07003645 cgroup_throttle_swaprate(page, GFP_KERNEL);
Mel Gormaneb3c24f2015-06-24 16:57:27 -07003646
Minchan Kim52f37622013-04-29 15:08:15 -07003647 /*
3648 * The memory barrier inside __SetPageUptodate makes sure that
Wei Yangf4f53292019-11-30 17:58:17 -08003649 * preceding stores to the page contents become visible before
Minchan Kim52f37622013-04-29 15:08:15 -07003650 * the set_pte_at() write.
3651 */
Nick Piggin0ed361d2008-02-04 22:29:34 -08003652 __SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Nick Piggin557ed1f2007-10-16 01:24:40 -07003654 entry = mk_pte(page, vma->vm_page_prot);
Thomas Bogendoerfer50c25ee2021-06-04 20:01:08 -07003655 entry = pte_sw_mkyoung(entry);
Hugh Dickins1ac0cb52009-09-21 17:03:29 -07003656 if (vma->vm_flags & VM_WRITE)
3657 entry = pte_mkwrite(pte_mkdirty(entry));
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003658
Jan Kara82b0f8c2016-12-14 15:06:58 -08003659 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3660 &vmf->ptl);
Bibo Mao7df67692020-05-27 10:25:18 +08003661 if (!pte_none(*vmf->pte)) {
3662 update_mmu_cache(vma, vmf->address, vmf->pte);
Nick Piggin557ed1f2007-10-16 01:24:40 -07003663 goto release;
Bibo Mao7df67692020-05-27 10:25:18 +08003664 }
Hugh Dickins9ba69292009-09-21 17:02:20 -07003665
Michal Hocko6b31d592017-08-18 15:16:15 -07003666 ret = check_stable_address_space(vma->vm_mm);
3667 if (ret)
3668 goto release;
3669
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07003670 /* Deliver the page fault to userland, check inside PT lock */
3671 if (userfaultfd_missing(vma)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08003672 pte_unmap_unlock(vmf->pte, vmf->ptl);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003673 put_page(page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003674 return handle_userfault(vmf, VM_UFFD_MISSING);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07003675 }
3676
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003677 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003678 page_add_new_anon_rmap(page, vma, vmf->address, false);
Joonsoo Kimb5181542020-08-11 18:30:40 -07003679 lru_cache_add_inactive_or_unevictable(page, vma);
Hugh Dickinsa13ea5b2009-09-21 17:03:30 -07003680setpte:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003681 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
3683 /* No need to invalidate - it was non-present before */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003684 update_mmu_cache(vma, vmf->address, vmf->pte);
Hugh Dickins65500d22005-10-29 18:15:59 -07003685unlock:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003686 pte_unmap_unlock(vmf->pte, vmf->ptl);
Michal Hocko6b31d592017-08-18 15:16:15 -07003687 return ret;
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003688release:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003689 put_page(page);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07003690 goto unlock;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003691oom_free_page:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003692 put_page(page);
Hugh Dickins65500d22005-10-29 18:15:59 -07003693oom:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 return VM_FAULT_OOM;
3695}
3696
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003697/*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003698 * The mmap_lock must have been held on entry, and may have been
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003699 * released depending on flags and vma->vm_ops->fault() return value.
3700 * See filemap_fault() and __lock_page_retry().
3701 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003702static vm_fault_t __do_fault(struct vm_fault *vmf)
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003703{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003704 struct vm_area_struct *vma = vmf->vma;
Souptick Joarder2b740302018-08-23 17:01:36 -07003705 vm_fault_t ret;
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003706
Michal Hocko63f36552019-01-08 15:23:07 -08003707 /*
3708 * Preallocate pte before we take page_lock because this might lead to
3709 * deadlocks for memcg reclaim which waits for pages under writeback:
3710 * lock_page(A)
3711 * SetPageWriteback(A)
3712 * unlock_page(A)
3713 * lock_page(B)
3714 * lock_page(B)
Yanfei Xud3838072020-10-13 16:53:26 -07003715 * pte_alloc_one
Michal Hocko63f36552019-01-08 15:23:07 -08003716 * shrink_page_list
3717 * wait_on_page_writeback(A)
3718 * SetPageWriteback(B)
3719 * unlock_page(B)
3720 * # flush A, B to clear the writeback
3721 */
3722 if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) {
Yanfei Xua7069ee2020-10-13 16:53:29 -07003723 vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
Michal Hocko63f36552019-01-08 15:23:07 -08003724 if (!vmf->prealloc_pte)
3725 return VM_FAULT_OOM;
3726 smp_wmb(); /* See comment in __pte_alloc() */
3727 }
3728
Dave Jiang11bac802017-02-24 14:56:41 -08003729 ret = vma->vm_ops->fault(vmf);
Jan Kara39170482016-12-14 15:07:18 -08003730 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY |
Jan Karab1aa8122016-12-14 15:07:24 -08003731 VM_FAULT_DONE_COW)))
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003732 return ret;
3733
Jan Kara667240e2016-12-14 15:07:07 -08003734 if (unlikely(PageHWPoison(vmf->page))) {
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003735 if (ret & VM_FAULT_LOCKED)
Jan Kara667240e2016-12-14 15:07:07 -08003736 unlock_page(vmf->page);
3737 put_page(vmf->page);
Jan Kara936ca802016-12-14 15:07:10 -08003738 vmf->page = NULL;
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003739 return VM_FAULT_HWPOISON;
3740 }
3741
3742 if (unlikely(!(ret & VM_FAULT_LOCKED)))
Jan Kara667240e2016-12-14 15:07:07 -08003743 lock_page(vmf->page);
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003744 else
Jan Kara667240e2016-12-14 15:07:07 -08003745 VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page);
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003746
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07003747 return ret;
3748}
3749
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003750#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Jan Kara82b0f8c2016-12-14 15:06:58 -08003751static void deposit_prealloc_pte(struct vm_fault *vmf)
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003752{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003753 struct vm_area_struct *vma = vmf->vma;
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003754
Jan Kara82b0f8c2016-12-14 15:06:58 -08003755 pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte);
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003756 /*
3757 * We are going to consume the prealloc table,
3758 * count that as nr_ptes.
3759 */
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08003760 mm_inc_nr_ptes(vma->vm_mm);
Tobin C Harding7f2b6ce2017-02-24 14:58:59 -08003761 vmf->prealloc_pte = NULL;
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003762}
3763
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003764vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003765{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003766 struct vm_area_struct *vma = vmf->vma;
3767 bool write = vmf->flags & FAULT_FLAG_WRITE;
3768 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003769 pmd_t entry;
Souptick Joarder2b740302018-08-23 17:01:36 -07003770 int i;
Matthew Wilcox (Oracle)d01ac3c2020-10-15 20:05:26 -07003771 vm_fault_t ret = VM_FAULT_FALLBACK;
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003772
3773 if (!transhuge_vma_suitable(vma, haddr))
Matthew Wilcox (Oracle)d01ac3c2020-10-15 20:05:26 -07003774 return ret;
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003775
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003776 page = compound_head(page);
Matthew Wilcox (Oracle)d01ac3c2020-10-15 20:05:26 -07003777 if (compound_order(page) != HPAGE_PMD_ORDER)
3778 return ret;
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003779
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003780 /*
Ingo Molnarf0953a12021-05-06 18:06:47 -07003781 * Archs like ppc64 need additional space to store information
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003782 * related to pte entry. Use the preallocated table for that.
3783 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08003784 if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) {
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08003785 vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
Jan Kara82b0f8c2016-12-14 15:06:58 -08003786 if (!vmf->prealloc_pte)
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003787 return VM_FAULT_OOM;
3788 smp_wmb(); /* See comment in __pte_alloc() */
3789 }
3790
Jan Kara82b0f8c2016-12-14 15:06:58 -08003791 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
3792 if (unlikely(!pmd_none(*vmf->pmd)))
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003793 goto out;
3794
3795 for (i = 0; i < HPAGE_PMD_NR; i++)
3796 flush_icache_page(vma, page + i);
3797
3798 entry = mk_huge_pmd(page, vma->vm_page_prot);
3799 if (write)
Linus Torvaldsf55e1012017-11-29 09:01:01 -08003800 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003801
Yang Shifadae292018-08-17 15:44:55 -07003802 add_mm_counter(vma->vm_mm, mm_counter_file(page), HPAGE_PMD_NR);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003803 page_add_file_rmap(page, true);
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08003804 /*
3805 * deposit and withdraw with pmd lock held
3806 */
3807 if (arch_needs_pgtable_deposit())
Jan Kara82b0f8c2016-12-14 15:06:58 -08003808 deposit_prealloc_pte(vmf);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003809
Jan Kara82b0f8c2016-12-14 15:06:58 -08003810 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003811
Jan Kara82b0f8c2016-12-14 15:06:58 -08003812 update_mmu_cache_pmd(vma, haddr, vmf->pmd);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003813
3814 /* fault is handled */
3815 ret = 0;
Kirill A. Shutemov95ecedc2016-07-26 15:25:31 -07003816 count_vm_event(THP_FILE_MAPPED);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003817out:
Jan Kara82b0f8c2016-12-14 15:06:58 -08003818 spin_unlock(vmf->ptl);
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003819 return ret;
3820}
3821#else
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003822vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003823{
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003824 return VM_FAULT_FALLBACK;
Kirill A. Shutemov10102452016-07-26 15:25:29 -07003825}
3826#endif
3827
Will Deacon9d3af4b2021-01-14 15:24:19 +00003828void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr)
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003829{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003830 struct vm_area_struct *vma = vmf->vma;
3831 bool write = vmf->flags & FAULT_FLAG_WRITE;
Will Deacon9d3af4b2021-01-14 15:24:19 +00003832 bool prefault = vmf->address != addr;
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003833 pte_t entry;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003834
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003835 flush_icache_page(vma, page);
3836 entry = mk_pte(page, vma->vm_page_prot);
Will Deacon46bdb422020-11-24 18:48:26 +00003837
3838 if (prefault && arch_wants_old_prefaulted_pte())
3839 entry = pte_mkold(entry);
Thomas Bogendoerfer50c25ee2021-06-04 20:01:08 -07003840 else
3841 entry = pte_sw_mkyoung(entry);
Will Deacon46bdb422020-11-24 18:48:26 +00003842
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003843 if (write)
3844 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003845 /* copy-on-write page */
3846 if (write && !(vma->vm_flags & VM_SHARED)) {
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003847 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
Will Deacon9d3af4b2021-01-14 15:24:19 +00003848 page_add_new_anon_rmap(page, vma, addr, false);
Joonsoo Kimb5181542020-08-11 18:30:40 -07003849 lru_cache_add_inactive_or_unevictable(page, vma);
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003850 } else {
Jerome Marchandeca56ff2016-01-14 15:19:26 -08003851 inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07003852 page_add_file_rmap(page, false);
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003853 }
Will Deacon9d3af4b2021-01-14 15:24:19 +00003854 set_pte_at(vma->vm_mm, addr, vmf->pte, entry);
Kirill A. Shutemov3bb97792014-04-03 14:48:16 -07003855}
3856
Jan Kara9118c0c2016-12-14 15:07:21 -08003857/**
3858 * finish_fault - finish page fault once we have prepared the page to fault
3859 *
3860 * @vmf: structure describing the fault
3861 *
3862 * This function handles all that is needed to finish a page fault once the
3863 * page to fault in is prepared. It handles locking of PTEs, inserts PTE for
3864 * given page, adds reverse page mapping, handles memcg charges and LRU
Mike Rapoporta862f682019-03-05 15:48:42 -08003865 * addition.
Jan Kara9118c0c2016-12-14 15:07:21 -08003866 *
3867 * The function expects the page to be locked and on success it consumes a
3868 * reference of a page being mapped (for the PTE which maps it).
Mike Rapoporta862f682019-03-05 15:48:42 -08003869 *
3870 * Return: %0 on success, %VM_FAULT_ code in case of error.
Jan Kara9118c0c2016-12-14 15:07:21 -08003871 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003872vm_fault_t finish_fault(struct vm_fault *vmf)
Jan Kara9118c0c2016-12-14 15:07:21 -08003873{
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003874 struct vm_area_struct *vma = vmf->vma;
Jan Kara9118c0c2016-12-14 15:07:21 -08003875 struct page *page;
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003876 vm_fault_t ret;
Jan Kara9118c0c2016-12-14 15:07:21 -08003877
3878 /* Did we COW the page? */
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003879 if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED))
Jan Kara9118c0c2016-12-14 15:07:21 -08003880 page = vmf->cow_page;
3881 else
3882 page = vmf->page;
Michal Hocko6b31d592017-08-18 15:16:15 -07003883
3884 /*
3885 * check even for read faults because we might have lost our CoWed
3886 * page
3887 */
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003888 if (!(vma->vm_flags & VM_SHARED)) {
3889 ret = check_stable_address_space(vma->vm_mm);
3890 if (ret)
3891 return ret;
3892 }
3893
3894 if (pmd_none(*vmf->pmd)) {
3895 if (PageTransCompound(page)) {
3896 ret = do_set_pmd(vmf, page);
3897 if (ret != VM_FAULT_FALLBACK)
3898 return ret;
3899 }
3900
3901 if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd)))
3902 return VM_FAULT_OOM;
3903 }
3904
3905 /* See comment in handle_pte_fault() */
3906 if (pmd_devmap_trans_unstable(vmf->pmd))
3907 return 0;
3908
3909 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
3910 vmf->address, &vmf->ptl);
3911 ret = 0;
3912 /* Re-check under ptl */
3913 if (likely(pte_none(*vmf->pte)))
Will Deacon9d3af4b2021-01-14 15:24:19 +00003914 do_set_pte(vmf, page, vmf->address);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003915 else
3916 ret = VM_FAULT_NOPAGE;
3917
3918 update_mmu_tlb(vma, vmf->address, vmf->pte);
3919 pte_unmap_unlock(vmf->pte, vmf->ptl);
Jan Kara9118c0c2016-12-14 15:07:21 -08003920 return ret;
3921}
3922
Kirill A. Shutemov3a910532014-08-06 16:08:07 -07003923static unsigned long fault_around_bytes __read_mostly =
3924 rounddown_pow_of_two(65536);
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003925
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003926#ifdef CONFIG_DEBUG_FS
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003927static int fault_around_bytes_get(void *data, u64 *val)
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003928{
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003929 *val = fault_around_bytes;
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003930 return 0;
3931}
3932
Andrey Ryabininb4903d62014-07-30 16:08:35 -07003933/*
William Kucharskida391d62018-01-31 16:21:11 -08003934 * fault_around_bytes must be rounded down to the nearest page order as it's
3935 * what do_fault_around() expects to see.
Andrey Ryabininb4903d62014-07-30 16:08:35 -07003936 */
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003937static int fault_around_bytes_set(void *data, u64 val)
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003938{
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003939 if (val / PAGE_SIZE > PTRS_PER_PTE)
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003940 return -EINVAL;
Andrey Ryabininb4903d62014-07-30 16:08:35 -07003941 if (val > PAGE_SIZE)
3942 fault_around_bytes = rounddown_pow_of_two(val);
3943 else
3944 fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003945 return 0;
3946}
Yevgen Pronenko0a1345f2017-07-10 15:47:17 -07003947DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops,
Kirill A. Shutemova9b0f862014-06-04 16:10:54 -07003948 fault_around_bytes_get, fault_around_bytes_set, "%llu\n");
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003949
3950static int __init fault_around_debugfs(void)
3951{
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -08003952 debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL,
3953 &fault_around_bytes_fops);
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003954 return 0;
3955}
3956late_initcall(fault_around_debugfs);
Kirill A. Shutemov1592eef2014-04-07 15:37:22 -07003957#endif
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003958
Kirill A. Shutemov1fdb4122014-06-04 16:10:55 -07003959/*
3960 * do_fault_around() tries to map few pages around the fault address. The hope
3961 * is that the pages will be needed soon and this will lower the number of
3962 * faults to handle.
3963 *
3964 * It uses vm_ops->map_pages() to map the pages, which skips the page if it's
3965 * not ready to be mapped: not up-to-date, locked, etc.
3966 *
3967 * This function is called with the page table lock taken. In the split ptlock
3968 * case the page table lock only protects only those entries which belong to
3969 * the page table corresponding to the fault address.
3970 *
3971 * This function doesn't cross the VMA boundaries, in order to call map_pages()
3972 * only once.
3973 *
William Kucharskida391d62018-01-31 16:21:11 -08003974 * fault_around_bytes defines how many bytes we'll try to map.
3975 * do_fault_around() expects it to be set to a power of two less than or equal
3976 * to PTRS_PER_PTE.
Kirill A. Shutemov1fdb4122014-06-04 16:10:55 -07003977 *
William Kucharskida391d62018-01-31 16:21:11 -08003978 * The virtual address of the area that we map is naturally aligned to
3979 * fault_around_bytes rounded down to the machine page size
3980 * (and therefore to page order). This way it's easier to guarantee
3981 * that we don't cross page table boundaries.
Kirill A. Shutemov1fdb4122014-06-04 16:10:55 -07003982 */
Souptick Joarder2b740302018-08-23 17:01:36 -07003983static vm_fault_t do_fault_around(struct vm_fault *vmf)
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003984{
Jan Kara82b0f8c2016-12-14 15:06:58 -08003985 unsigned long address = vmf->address, nr_pages, mask;
Jan Kara0721ec82016-12-14 15:07:04 -08003986 pgoff_t start_pgoff = vmf->pgoff;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003987 pgoff_t end_pgoff;
Souptick Joarder2b740302018-08-23 17:01:36 -07003988 int off;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003989
Jason Low4db0c3c2015-04-15 16:14:08 -07003990 nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT;
Kirill A. Shutemovaecd6f42014-08-06 16:08:05 -07003991 mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
3992
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003993 address = max(address & mask, vmf->vma->vm_start);
3994 off = ((vmf->address - address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003995 start_pgoff -= off;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07003996
3997 /*
William Kucharskida391d62018-01-31 16:21:11 -08003998 * end_pgoff is either the end of the page table, the end of
3999 * the vma or nr_pages from start_pgoff, depending what is nearest.
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07004000 */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07004001 end_pgoff = start_pgoff -
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03004002 ((address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07004003 PTRS_PER_PTE - 1;
Jan Kara82b0f8c2016-12-14 15:06:58 -08004004 end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07004005 start_pgoff + nr_pages - 1);
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07004006
Jan Kara82b0f8c2016-12-14 15:06:58 -08004007 if (pmd_none(*vmf->pmd)) {
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08004008 vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm);
Jan Kara82b0f8c2016-12-14 15:06:58 -08004009 if (!vmf->prealloc_pte)
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03004010 return VM_FAULT_OOM;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004011 smp_wmb(); /* See comment in __pte_alloc() */
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07004012 }
4013
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03004014 return vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff);
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07004015}
4016
Souptick Joarder2b740302018-08-23 17:01:36 -07004017static vm_fault_t do_read_fault(struct vm_fault *vmf)
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07004018{
Jan Kara82b0f8c2016-12-14 15:06:58 -08004019 struct vm_area_struct *vma = vmf->vma;
Souptick Joarder2b740302018-08-23 17:01:36 -07004020 vm_fault_t ret = 0;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07004021
4022 /*
4023 * Let's call ->map_pages() first and use ->fault() as fallback
4024 * if page by the offset is not ready to be mapped (cold cache or
4025 * something).
4026 */
Kirill A. Shutemov9b4bdd22015-02-10 14:09:51 -08004027 if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) {
Jan Kara0721ec82016-12-14 15:07:04 -08004028 ret = do_fault_around(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004029 if (ret)
4030 return ret;
Kirill A. Shutemov8c6e50b2014-04-07 15:37:18 -07004031 }
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07004032
Jan Kara936ca802016-12-14 15:07:10 -08004033 ret = __do_fault(vmf);
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07004034 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
4035 return ret;
4036
Jan Kara9118c0c2016-12-14 15:07:21 -08004037 ret |= finish_fault(vmf);
Jan Kara936ca802016-12-14 15:07:10 -08004038 unlock_page(vmf->page);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004039 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
Jan Kara936ca802016-12-14 15:07:10 -08004040 put_page(vmf->page);
Kirill A. Shutemove655fb22014-04-03 14:48:11 -07004041 return ret;
4042}
4043
Souptick Joarder2b740302018-08-23 17:01:36 -07004044static vm_fault_t do_cow_fault(struct vm_fault *vmf)
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07004045{
Jan Kara82b0f8c2016-12-14 15:06:58 -08004046 struct vm_area_struct *vma = vmf->vma;
Souptick Joarder2b740302018-08-23 17:01:36 -07004047 vm_fault_t ret;
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07004048
4049 if (unlikely(anon_vma_prepare(vma)))
4050 return VM_FAULT_OOM;
4051
Jan Kara936ca802016-12-14 15:07:10 -08004052 vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address);
4053 if (!vmf->cow_page)
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07004054 return VM_FAULT_OOM;
4055
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -07004056 if (mem_cgroup_charge(vmf->cow_page, vma->vm_mm, GFP_KERNEL)) {
Jan Kara936ca802016-12-14 15:07:10 -08004057 put_page(vmf->cow_page);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07004058 return VM_FAULT_OOM;
4059 }
Johannes Weiner9d82c692020-06-03 16:02:04 -07004060 cgroup_throttle_swaprate(vmf->cow_page, GFP_KERNEL);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07004061
Jan Kara936ca802016-12-14 15:07:10 -08004062 ret = __do_fault(vmf);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07004063 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
4064 goto uncharge_out;
Jan Kara39170482016-12-14 15:07:18 -08004065 if (ret & VM_FAULT_DONE_COW)
4066 return ret;
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07004067
Jan Karab1aa8122016-12-14 15:07:24 -08004068 copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma);
Jan Kara936ca802016-12-14 15:07:10 -08004069 __SetPageUptodate(vmf->cow_page);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07004070
Jan Kara9118c0c2016-12-14 15:07:21 -08004071 ret |= finish_fault(vmf);
Jan Karab1aa8122016-12-14 15:07:24 -08004072 unlock_page(vmf->page);
4073 put_page(vmf->page);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004074 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
4075 goto uncharge_out;
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07004076 return ret;
4077uncharge_out:
Jan Kara936ca802016-12-14 15:07:10 -08004078 put_page(vmf->cow_page);
Kirill A. Shutemovec47c3b2014-04-03 14:48:12 -07004079 return ret;
4080}
4081
Souptick Joarder2b740302018-08-23 17:01:36 -07004082static vm_fault_t do_shared_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083{
Jan Kara82b0f8c2016-12-14 15:06:58 -08004084 struct vm_area_struct *vma = vmf->vma;
Souptick Joarder2b740302018-08-23 17:01:36 -07004085 vm_fault_t ret, tmp;
KAMEZAWA Hiroyuki1d65f862011-07-25 17:12:27 -07004086
Jan Kara936ca802016-12-14 15:07:10 -08004087 ret = __do_fault(vmf);
Kirill A. Shutemov7eae74a2014-04-03 14:48:10 -07004088 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07004089 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090
4091 /*
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07004092 * Check if the backing address space wants to know that the page is
4093 * about to become writable
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 */
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07004095 if (vma->vm_ops->page_mkwrite) {
Jan Kara936ca802016-12-14 15:07:10 -08004096 unlock_page(vmf->page);
Jan Kara38b8cb72016-12-14 15:07:30 -08004097 tmp = do_page_mkwrite(vmf);
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07004098 if (unlikely(!tmp ||
4099 (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
Jan Kara936ca802016-12-14 15:07:10 -08004100 put_page(vmf->page);
Kirill A. Shutemovfb09a462014-04-03 14:48:15 -07004101 return tmp;
4102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 }
4104
Jan Kara9118c0c2016-12-14 15:07:21 -08004105 ret |= finish_fault(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004106 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
4107 VM_FAULT_RETRY))) {
Jan Kara936ca802016-12-14 15:07:10 -08004108 unlock_page(vmf->page);
4109 put_page(vmf->page);
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07004110 return ret;
Peter Zijlstrad08b3852006-09-25 23:30:57 -07004111 }
Kirill A. Shutemovf0c6d4d2014-04-03 14:48:13 -07004112
Johannes Weiner89b15332019-11-30 17:50:22 -08004113 ret |= fault_dirty_shared_page(vmf);
KAMEZAWA Hiroyuki1d65f862011-07-25 17:12:27 -07004114 return ret;
Nick Piggin54cb8822007-07-19 01:46:59 -07004115}
Nick Piggind00806b2007-07-19 01:46:57 -07004116
Paul Cassella9a95f3c2014-08-06 16:07:24 -07004117/*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07004118 * We enter with non-exclusive mmap_lock (to exclude vma changes,
Paul Cassella9a95f3c2014-08-06 16:07:24 -07004119 * but allow concurrent faults).
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07004120 * The mmap_lock may have been released depending on flags and our
Paul Cassella9a95f3c2014-08-06 16:07:24 -07004121 * return value. See filemap_fault() and __lock_page_or_retry().
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07004122 * If mmap_lock is released, vma may become invalid (for example
Jan Stancekfc8efd22019-03-05 15:50:08 -08004123 * by other thread calling munmap()).
Paul Cassella9a95f3c2014-08-06 16:07:24 -07004124 */
Souptick Joarder2b740302018-08-23 17:01:36 -07004125static vm_fault_t do_fault(struct vm_fault *vmf)
Nick Piggin54cb8822007-07-19 01:46:59 -07004126{
Jan Kara82b0f8c2016-12-14 15:06:58 -08004127 struct vm_area_struct *vma = vmf->vma;
Jan Stancekfc8efd22019-03-05 15:50:08 -08004128 struct mm_struct *vm_mm = vma->vm_mm;
Souptick Joarder2b740302018-08-23 17:01:36 -07004129 vm_fault_t ret;
Nick Piggin54cb8822007-07-19 01:46:59 -07004130
Aneesh Kumar K.Vff09d7e2018-10-26 15:09:01 -07004131 /*
4132 * The VMA was not fully populated on mmap() or missing VM_DONTEXPAND
4133 */
4134 if (!vma->vm_ops->fault) {
4135 /*
4136 * If we find a migration pmd entry or a none pmd entry, which
4137 * should never happen, return SIGBUS
4138 */
4139 if (unlikely(!pmd_present(*vmf->pmd)))
4140 ret = VM_FAULT_SIGBUS;
4141 else {
4142 vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm,
4143 vmf->pmd,
4144 vmf->address,
4145 &vmf->ptl);
4146 /*
4147 * Make sure this is not a temporary clearing of pte
4148 * by holding ptl and checking again. A R/M/W update
4149 * of pte involves: take ptl, clearing the pte so that
4150 * we don't have concurrent modification by hardware
4151 * followed by an update.
4152 */
4153 if (unlikely(pte_none(*vmf->pte)))
4154 ret = VM_FAULT_SIGBUS;
4155 else
4156 ret = VM_FAULT_NOPAGE;
4157
4158 pte_unmap_unlock(vmf->pte, vmf->ptl);
4159 }
4160 } else if (!(vmf->flags & FAULT_FLAG_WRITE))
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08004161 ret = do_read_fault(vmf);
4162 else if (!(vma->vm_flags & VM_SHARED))
4163 ret = do_cow_fault(vmf);
4164 else
4165 ret = do_shared_fault(vmf);
4166
4167 /* preallocated pagetable is unused: free it */
4168 if (vmf->prealloc_pte) {
Jan Stancekfc8efd22019-03-05 15:50:08 -08004169 pte_free(vm_mm, vmf->prealloc_pte);
Tobin C Harding7f2b6ce2017-02-24 14:58:59 -08004170 vmf->prealloc_pte = NULL;
Hugh Dickinsb0b9b3d2017-01-07 15:37:31 -08004171 }
4172 return ret;
Nick Piggin54cb8822007-07-19 01:46:59 -07004173}
4174
Rashika Kheriab19a9932014-04-03 14:48:02 -07004175static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
Rik van Riel04bb2f92013-10-07 11:29:36 +01004176 unsigned long addr, int page_nid,
4177 int *flags)
Mel Gorman9532fec2012-11-15 01:24:32 +00004178{
4179 get_page(page);
4180
4181 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01004182 if (page_nid == numa_node_id()) {
Mel Gorman9532fec2012-11-15 01:24:32 +00004183 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01004184 *flags |= TNF_FAULT_LOCAL;
4185 }
Mel Gorman9532fec2012-11-15 01:24:32 +00004186
4187 return mpol_misplaced(page, vma, addr);
4188}
4189
Souptick Joarder2b740302018-08-23 17:01:36 -07004190static vm_fault_t do_numa_page(struct vm_fault *vmf)
Mel Gormand10e63f2012-10-25 14:16:31 +02004191{
Jan Kara82b0f8c2016-12-14 15:06:58 -08004192 struct vm_area_struct *vma = vmf->vma;
Mel Gorman4daae3b2012-11-02 11:33:45 +00004193 struct page *page = NULL;
Anshuman Khandual98fa15f2019-03-05 15:42:58 -08004194 int page_nid = NUMA_NO_NODE;
Peter Zijlstra90572892013-10-07 11:29:20 +01004195 int last_cpupid;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02004196 int target_nid;
Aneesh Kumar K.V04a86452019-03-05 15:46:29 -08004197 pte_t pte, old_pte;
Aneesh Kumar K.V288bc542017-02-24 14:59:16 -08004198 bool was_writable = pte_savedwrite(vmf->orig_pte);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01004199 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02004200
4201 /*
Tobin C Harding166f61b2017-02-24 14:59:01 -08004202 * The "pte" at this point cannot be used safely without
4203 * validation through pte_unmap_same(). It's of NUMA type but
4204 * the pfn may be screwed if the read is non atomic.
Tobin C Harding166f61b2017-02-24 14:59:01 -08004205 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08004206 vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd);
4207 spin_lock(vmf->ptl);
Aneesh Kumar K.Vcee216a2017-02-24 14:59:13 -08004208 if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08004209 pte_unmap_unlock(vmf->pte, vmf->ptl);
Mel Gorman4daae3b2012-11-02 11:33:45 +00004210 goto out;
4211 }
4212
Huang Yingb99a3422021-04-29 22:57:41 -07004213 /* Get the normal PTE */
4214 old_pte = ptep_get(vmf->pte);
Aneesh Kumar K.V04a86452019-03-05 15:46:29 -08004215 pte = pte_modify(old_pte, vma->vm_page_prot);
Mel Gormand10e63f2012-10-25 14:16:31 +02004216
Jan Kara82b0f8c2016-12-14 15:06:58 -08004217 page = vm_normal_page(vma, vmf->address, pte);
Huang Yingb99a3422021-04-29 22:57:41 -07004218 if (!page)
4219 goto out_map;
Mel Gormand10e63f2012-10-25 14:16:31 +02004220
Kirill A. Shutemove81c4802016-01-15 16:53:49 -08004221 /* TODO: handle PTE-mapped THP */
Huang Yingb99a3422021-04-29 22:57:41 -07004222 if (PageCompound(page))
4223 goto out_map;
Kirill A. Shutemove81c4802016-01-15 16:53:49 -08004224
Peter Zijlstra6688cc02013-10-07 11:29:24 +01004225 /*
Mel Gormanbea66fb2015-03-25 15:55:37 -07004226 * Avoid grouping on RO pages in general. RO pages shouldn't hurt as
4227 * much anyway since they can be in shared cache state. This misses
4228 * the case where a mapping is writable but the process never writes
4229 * to it but pte_write gets cleared during protection updates and
4230 * pte_dirty has unpredictable behaviour between PTE scan updates,
4231 * background writeback, dirty balancing and application behaviour.
Peter Zijlstra6688cc02013-10-07 11:29:24 +01004232 */
Huang Yingb99a3422021-04-29 22:57:41 -07004233 if (!was_writable)
Peter Zijlstra6688cc02013-10-07 11:29:24 +01004234 flags |= TNF_NO_GROUP;
4235
Rik van Rieldabe1d92013-10-07 11:29:34 +01004236 /*
4237 * Flag if the page is shared between multiple address spaces. This
4238 * is later used when determining whether to group tasks together
4239 */
4240 if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED))
4241 flags |= TNF_SHARED;
4242
Peter Zijlstra90572892013-10-07 11:29:20 +01004243 last_cpupid = page_cpupid_last(page);
Mel Gorman8191acb2013-10-07 11:28:45 +01004244 page_nid = page_to_nid(page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08004245 target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07004246 &flags);
Anshuman Khandual98fa15f2019-03-05 15:42:58 -08004247 if (target_nid == NUMA_NO_NODE) {
Mel Gorman4daae3b2012-11-02 11:33:45 +00004248 put_page(page);
Huang Yingb99a3422021-04-29 22:57:41 -07004249 goto out_map;
Mel Gorman4daae3b2012-11-02 11:33:45 +00004250 }
Huang Yingb99a3422021-04-29 22:57:41 -07004251 pte_unmap_unlock(vmf->pte, vmf->ptl);
Mel Gorman4daae3b2012-11-02 11:33:45 +00004252
4253 /* Migrate to the requested node */
Wang Qingbf90ac12021-04-29 22:57:07 -07004254 if (migrate_misplaced_page(page, vma, target_nid)) {
Mel Gorman8191acb2013-10-07 11:28:45 +01004255 page_nid = target_nid;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01004256 flags |= TNF_MIGRATED;
Huang Yingb99a3422021-04-29 22:57:41 -07004257 } else {
Mel Gorman074c2382015-03-25 15:55:42 -07004258 flags |= TNF_MIGRATE_FAIL;
Huang Yingb99a3422021-04-29 22:57:41 -07004259 vmf->pte = pte_offset_map(vmf->pmd, vmf->address);
4260 spin_lock(vmf->ptl);
4261 if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) {
4262 pte_unmap_unlock(vmf->pte, vmf->ptl);
4263 goto out;
4264 }
4265 goto out_map;
4266 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00004267
4268out:
Anshuman Khandual98fa15f2019-03-05 15:42:58 -08004269 if (page_nid != NUMA_NO_NODE)
Peter Zijlstra6688cc02013-10-07 11:29:24 +01004270 task_numa_fault(last_cpupid, page_nid, 1, flags);
Mel Gormand10e63f2012-10-25 14:16:31 +02004271 return 0;
Huang Yingb99a3422021-04-29 22:57:41 -07004272out_map:
4273 /*
4274 * Make it present again, depending on how arch implements
4275 * non-accessible ptes, some can allow access by kernel mode.
4276 */
4277 old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte);
4278 pte = pte_modify(old_pte, vma->vm_page_prot);
4279 pte = pte_mkyoung(pte);
4280 if (was_writable)
4281 pte = pte_mkwrite(pte);
4282 ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte);
4283 update_mmu_cache(vma, vmf->address, vmf->pte);
4284 pte_unmap_unlock(vmf->pte, vmf->ptl);
4285 goto out;
Mel Gormand10e63f2012-10-25 14:16:31 +02004286}
4287
Souptick Joarder2b740302018-08-23 17:01:36 -07004288static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf)
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07004289{
Dave Jiangf4200392017-02-22 15:40:06 -08004290 if (vma_is_anonymous(vmf->vma))
Jan Kara82b0f8c2016-12-14 15:06:58 -08004291 return do_huge_pmd_anonymous_page(vmf);
Dave Jianga2d58162017-02-24 14:56:59 -08004292 if (vmf->vma->vm_ops->huge_fault)
Dave Jiangc791ace2017-02-24 14:57:08 -08004293 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07004294 return VM_FAULT_FALLBACK;
4295}
4296
Geert Uytterhoeven183f24aa2017-12-14 15:32:52 -08004297/* `inline' is required to avoid gcc 4.1.2 build error */
Souptick Joarder2b740302018-08-23 17:01:36 -07004298static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd)
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07004299{
Andrea Arcangeli529b9302020-04-06 20:05:29 -07004300 if (vma_is_anonymous(vmf->vma)) {
Peter Xu292924b2020-04-06 20:05:49 -07004301 if (userfaultfd_huge_pmd_wp(vmf->vma, orig_pmd))
Andrea Arcangeli529b9302020-04-06 20:05:29 -07004302 return handle_userfault(vmf, VM_UFFD_WP);
Jan Kara82b0f8c2016-12-14 15:06:58 -08004303 return do_huge_pmd_wp_page(vmf, orig_pmd);
Andrea Arcangeli529b9302020-04-06 20:05:29 -07004304 }
Thomas Hellstrom (VMware)327e9fd2020-03-24 18:47:47 +01004305 if (vmf->vma->vm_ops->huge_fault) {
4306 vm_fault_t ret = vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);
Kirill A. Shutemovaf9e4d52016-07-26 15:25:40 -07004307
Thomas Hellstrom (VMware)327e9fd2020-03-24 18:47:47 +01004308 if (!(ret & VM_FAULT_FALLBACK))
4309 return ret;
4310 }
4311
4312 /* COW or write-notify handled on pte level: split pmd. */
Jan Kara82b0f8c2016-12-14 15:06:58 -08004313 __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL);
Kirill A. Shutemovaf9e4d52016-07-26 15:25:40 -07004314
Matthew Wilcoxb96375f2015-09-08 14:58:48 -07004315 return VM_FAULT_FALLBACK;
4316}
4317
Souptick Joarder2b740302018-08-23 17:01:36 -07004318static vm_fault_t create_huge_pud(struct vm_fault *vmf)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004319{
Thomas Hellstrom (VMware)327e9fd2020-03-24 18:47:47 +01004320#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \
4321 defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004322 /* No support for anonymous transparent PUD pages yet */
4323 if (vma_is_anonymous(vmf->vma))
Thomas Hellstrom (VMware)327e9fd2020-03-24 18:47:47 +01004324 goto split;
4325 if (vmf->vma->vm_ops->huge_fault) {
4326 vm_fault_t ret = vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD);
4327
4328 if (!(ret & VM_FAULT_FALLBACK))
4329 return ret;
4330 }
4331split:
4332 /* COW or write-notify not handled on PUD level: split pud.*/
4333 __split_huge_pud(vmf->vma, vmf->pud, vmf->address);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004334#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
4335 return VM_FAULT_FALLBACK;
4336}
4337
Souptick Joarder2b740302018-08-23 17:01:36 -07004338static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004339{
4340#ifdef CONFIG_TRANSPARENT_HUGEPAGE
4341 /* No support for anonymous transparent PUD pages yet */
4342 if (vma_is_anonymous(vmf->vma))
4343 return VM_FAULT_FALLBACK;
4344 if (vmf->vma->vm_ops->huge_fault)
Dave Jiangc791ace2017-02-24 14:57:08 -08004345 return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004346#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
4347 return VM_FAULT_FALLBACK;
4348}
4349
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350/*
4351 * These routines also need to handle stuff like marking pages dirty
4352 * and/or accessed for architectures that don't do it in hardware (most
4353 * RISC architectures). The early dirtying is also good on the i386.
4354 *
4355 * There is also a hook called "update_mmu_cache()" that architectures
4356 * with external mmu caches can use to update those (ie the Sparc or
4357 * PowerPC hashed page tables that act as extended TLBs).
4358 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07004359 * We enter with non-exclusive mmap_lock (to exclude vma changes, but allow
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004360 * concurrent faults).
Paul Cassella9a95f3c2014-08-06 16:07:24 -07004361 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07004362 * The mmap_lock may have been released depending on flags and our return value.
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004363 * See filemap_fault() and __lock_page_or_retry().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 */
Souptick Joarder2b740302018-08-23 17:01:36 -07004365static vm_fault_t handle_pte_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366{
4367 pte_t entry;
4368
Jan Kara82b0f8c2016-12-14 15:06:58 -08004369 if (unlikely(pmd_none(*vmf->pmd))) {
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004370 /*
4371 * Leave __pte_alloc() until later: because vm_ops->fault may
4372 * want to allocate huge page, and if we expose page table
4373 * for an instant, it will be difficult to retract from
4374 * concurrent faults and from rmap lookups.
4375 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08004376 vmf->pte = NULL;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004377 } else {
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03004378 /*
4379 * If a huge pmd materialized under us just retry later. Use
4380 * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead
4381 * of pmd_trans_huge() to ensure the pmd didn't become
4382 * pmd_trans_huge under us and then back to pmd_none, as a
4383 * result of MADV_DONTNEED running immediately after a huge pmd
4384 * fault in a different thread of this mm, in turn leading to a
4385 * misleading pmd_trans_huge() retval. All we have to ensure is
4386 * that it is a regular pmd that we can walk with
4387 * pte_offset_map() and we can do that through an atomic read
4388 * in C, which is what pmd_trans_unstable() provides.
4389 */
Ross Zwislerd0f0931d2017-06-02 14:46:34 -07004390 if (pmd_devmap_trans_unstable(vmf->pmd))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004391 return 0;
4392 /*
4393 * A regular pmd is established and it can't morph into a huge
4394 * pmd from under us anymore at this point because we hold the
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07004395 * mmap_lock read mode and khugepaged takes it in write mode.
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004396 * So now it's safe to run pte_offset_map().
4397 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08004398 vmf->pte = pte_offset_map(vmf->pmd, vmf->address);
Jan Kara29943022016-12-14 15:07:16 -08004399 vmf->orig_pte = *vmf->pte;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004400
4401 /*
4402 * some architectures can have larger ptes than wordsize,
4403 * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and
Paul E. McKenneyb03a0fe2017-10-23 14:07:25 -07004404 * CONFIG_32BIT=y, so READ_ONCE cannot guarantee atomic
4405 * accesses. The code below just needs a consistent view
4406 * for the ifs and we later double check anyway with the
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004407 * ptl lock held. So here a barrier will do.
4408 */
4409 barrier();
Jan Kara29943022016-12-14 15:07:16 -08004410 if (pte_none(vmf->orig_pte)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08004411 pte_unmap(vmf->pte);
4412 vmf->pte = NULL;
Hugh Dickins65500d22005-10-29 18:15:59 -07004413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 }
4415
Jan Kara82b0f8c2016-12-14 15:06:58 -08004416 if (!vmf->pte) {
4417 if (vma_is_anonymous(vmf->vma))
4418 return do_anonymous_page(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004419 else
Jan Kara82b0f8c2016-12-14 15:06:58 -08004420 return do_fault(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004421 }
4422
Jan Kara29943022016-12-14 15:07:16 -08004423 if (!pte_present(vmf->orig_pte))
4424 return do_swap_page(vmf);
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07004425
Jan Kara29943022016-12-14 15:07:16 -08004426 if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma))
4427 return do_numa_page(vmf);
Mel Gormand10e63f2012-10-25 14:16:31 +02004428
Jan Kara82b0f8c2016-12-14 15:06:58 -08004429 vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
4430 spin_lock(vmf->ptl);
Jan Kara29943022016-12-14 15:07:16 -08004431 entry = vmf->orig_pte;
Bibo Mao7df67692020-05-27 10:25:18 +08004432 if (unlikely(!pte_same(*vmf->pte, entry))) {
4433 update_mmu_tlb(vmf->vma, vmf->address, vmf->pte);
Hugh Dickins8f4e2102005-10-29 18:16:26 -07004434 goto unlock;
Bibo Mao7df67692020-05-27 10:25:18 +08004435 }
Jan Kara82b0f8c2016-12-14 15:06:58 -08004436 if (vmf->flags & FAULT_FLAG_WRITE) {
Linus Torvaldsf6f37322017-12-15 18:53:22 -08004437 if (!pte_write(entry))
Jan Kara29943022016-12-14 15:07:16 -08004438 return do_wp_page(vmf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 entry = pte_mkdirty(entry);
4440 }
4441 entry = pte_mkyoung(entry);
Jan Kara82b0f8c2016-12-14 15:06:58 -08004442 if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry,
4443 vmf->flags & FAULT_FLAG_WRITE)) {
4444 update_mmu_cache(vmf->vma, vmf->address, vmf->pte);
Andrea Arcangeli1a44e142005-10-29 18:16:48 -07004445 } else {
Yang Shib7333b52020-08-14 21:30:41 -07004446 /* Skip spurious TLB flush for retried page fault */
4447 if (vmf->flags & FAULT_FLAG_TRIED)
4448 goto unlock;
Andrea Arcangeli1a44e142005-10-29 18:16:48 -07004449 /*
4450 * This is needed only for protection faults but the arch code
4451 * is not yet telling us if this is a protection fault or not.
4452 * This still avoids useless tlb flushes for .text page faults
4453 * with threads.
4454 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08004455 if (vmf->flags & FAULT_FLAG_WRITE)
4456 flush_tlb_fix_spurious_fault(vmf->vma, vmf->address);
Andrea Arcangeli1a44e142005-10-29 18:16:48 -07004457 }
Hugh Dickins8f4e2102005-10-29 18:16:26 -07004458unlock:
Jan Kara82b0f8c2016-12-14 15:06:58 -08004459 pte_unmap_unlock(vmf->pte, vmf->ptl);
Nick Piggin83c54072007-07-19 01:47:05 -07004460 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461}
4462
4463/*
4464 * By the time we get here, we already hold the mm semaphore
Paul Cassella9a95f3c2014-08-06 16:07:24 -07004465 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07004466 * The mmap_lock may have been released depending on flags and our
Paul Cassella9a95f3c2014-08-06 16:07:24 -07004467 * return value. See filemap_fault() and __lock_page_or_retry().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 */
Souptick Joarder2b740302018-08-23 17:01:36 -07004469static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
4470 unsigned long address, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471{
Jan Kara82b0f8c2016-12-14 15:06:58 -08004472 struct vm_fault vmf = {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07004473 .vma = vma,
Jan Kara1a29d852016-12-14 15:07:01 -08004474 .address = address & PAGE_MASK,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07004475 .flags = flags,
Jan Kara0721ec82016-12-14 15:07:04 -08004476 .pgoff = linear_page_index(vma, address),
Jan Kara667240e2016-12-14 15:07:07 -08004477 .gfp_mask = __get_fault_gfp_mask(vma),
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07004478 };
Anshuman Khandualfde26be2017-09-08 16:12:45 -07004479 unsigned int dirty = flags & FAULT_FLAG_WRITE;
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -07004480 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004482 p4d_t *p4d;
Souptick Joarder2b740302018-08-23 17:01:36 -07004483 vm_fault_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 pgd = pgd_offset(mm, address);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004486 p4d = p4d_alloc(mm, pgd, address);
4487 if (!p4d)
4488 return VM_FAULT_OOM;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004489
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004490 vmf.pud = pud_alloc(mm, p4d, address);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004491 if (!vmf.pud)
Hugh Dickinsc74df322005-10-29 18:16:23 -07004492 return VM_FAULT_OOM;
Thomas Hellstrom625110b2019-11-30 17:51:32 -08004493retry_pud:
Michal Hocko7635d9c2018-12-28 00:38:21 -08004494 if (pud_none(*vmf.pud) && __transparent_hugepage_enabled(vma)) {
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004495 ret = create_huge_pud(&vmf);
4496 if (!(ret & VM_FAULT_FALLBACK))
4497 return ret;
4498 } else {
4499 pud_t orig_pud = *vmf.pud;
4500
4501 barrier();
4502 if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) {
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004503
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004504 /* NUMA case for anonymous PUDs would go here */
4505
Linus Torvaldsf6f37322017-12-15 18:53:22 -08004506 if (dirty && !pud_write(orig_pud)) {
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004507 ret = wp_huge_pud(&vmf, orig_pud);
4508 if (!(ret & VM_FAULT_FALLBACK))
4509 return ret;
4510 } else {
4511 huge_pud_set_accessed(&vmf, orig_pud);
4512 return 0;
4513 }
4514 }
4515 }
4516
4517 vmf.pmd = pmd_alloc(mm, vmf.pud, address);
Jan Kara82b0f8c2016-12-14 15:06:58 -08004518 if (!vmf.pmd)
Hugh Dickinsc74df322005-10-29 18:16:23 -07004519 return VM_FAULT_OOM;
Thomas Hellstrom625110b2019-11-30 17:51:32 -08004520
4521 /* Huge pud page fault raced with pmd_alloc? */
4522 if (pud_trans_unstable(vmf.pud))
4523 goto retry_pud;
4524
Michal Hocko7635d9c2018-12-28 00:38:21 -08004525 if (pmd_none(*vmf.pmd) && __transparent_hugepage_enabled(vma)) {
Dave Jianga2d58162017-02-24 14:56:59 -08004526 ret = create_huge_pmd(&vmf);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -07004527 if (!(ret & VM_FAULT_FALLBACK))
4528 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08004529 } else {
Jan Kara82b0f8c2016-12-14 15:06:58 -08004530 pmd_t orig_pmd = *vmf.pmd;
David Rientjes1f1d06c2012-05-29 15:06:23 -07004531
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08004532 barrier();
Zi Yan84c3fc42017-09-08 16:11:01 -07004533 if (unlikely(is_swap_pmd(orig_pmd))) {
4534 VM_BUG_ON(thp_migration_supported() &&
4535 !is_pmd_migration_entry(orig_pmd));
4536 if (is_pmd_migration_entry(orig_pmd))
4537 pmd_migration_entry_wait(mm, vmf.pmd);
4538 return 0;
4539 }
Dan Williams5c7fb562016-01-15 16:56:52 -08004540 if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) {
Lorenzo Stoakes38e08852016-09-11 23:54:25 +01004541 if (pmd_protnone(orig_pmd) && vma_is_accessible(vma))
Jan Kara82b0f8c2016-12-14 15:06:58 -08004542 return do_huge_pmd_numa_page(&vmf, orig_pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02004543
Linus Torvaldsf6f37322017-12-15 18:53:22 -08004544 if (dirty && !pmd_write(orig_pmd)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08004545 ret = wp_huge_pmd(&vmf, orig_pmd);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08004546 if (!(ret & VM_FAULT_FALLBACK))
4547 return ret;
Will Deacona1dd4502012-12-11 16:01:27 -08004548 } else {
Jan Kara82b0f8c2016-12-14 15:06:58 -08004549 huge_pmd_set_accessed(&vmf, orig_pmd);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08004550 return 0;
David Rientjes1f1d06c2012-05-29 15:06:23 -07004551 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08004552 }
4553 }
4554
Jan Kara82b0f8c2016-12-14 15:06:58 -08004555 return handle_pte_fault(&vmf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556}
4557
Peter Xubce617e2020-08-11 18:37:44 -07004558/**
Ingo Molnarf0953a12021-05-06 18:06:47 -07004559 * mm_account_fault - Do page fault accounting
Peter Xubce617e2020-08-11 18:37:44 -07004560 *
4561 * @regs: the pt_regs struct pointer. When set to NULL, will skip accounting
4562 * of perf event counters, but we'll still do the per-task accounting to
4563 * the task who triggered this page fault.
4564 * @address: the faulted address.
4565 * @flags: the fault flags.
4566 * @ret: the fault retcode.
4567 *
Ingo Molnarf0953a12021-05-06 18:06:47 -07004568 * This will take care of most of the page fault accounting. Meanwhile, it
Peter Xubce617e2020-08-11 18:37:44 -07004569 * will also include the PERF_COUNT_SW_PAGE_FAULTS_[MAJ|MIN] perf counter
Ingo Molnarf0953a12021-05-06 18:06:47 -07004570 * updates. However, note that the handling of PERF_COUNT_SW_PAGE_FAULTS should
Peter Xubce617e2020-08-11 18:37:44 -07004571 * still be in per-arch page fault handlers at the entry of page fault.
4572 */
4573static inline void mm_account_fault(struct pt_regs *regs,
4574 unsigned long address, unsigned int flags,
4575 vm_fault_t ret)
4576{
4577 bool major;
4578
4579 /*
4580 * We don't do accounting for some specific faults:
4581 *
4582 * - Unsuccessful faults (e.g. when the address wasn't valid). That
4583 * includes arch_vma_access_permitted() failing before reaching here.
4584 * So this is not a "this many hardware page faults" counter. We
4585 * should use the hw profiling for that.
4586 *
4587 * - Incomplete faults (VM_FAULT_RETRY). They will only be counted
4588 * once they're completed.
4589 */
4590 if (ret & (VM_FAULT_ERROR | VM_FAULT_RETRY))
4591 return;
4592
4593 /*
4594 * We define the fault as a major fault when the final successful fault
4595 * is VM_FAULT_MAJOR, or if it retried (which implies that we couldn't
4596 * handle it immediately previously).
4597 */
4598 major = (ret & VM_FAULT_MAJOR) || (flags & FAULT_FLAG_TRIED);
4599
Peter Xua2beb5f2020-08-11 18:38:57 -07004600 if (major)
4601 current->maj_flt++;
4602 else
4603 current->min_flt++;
4604
Peter Xubce617e2020-08-11 18:37:44 -07004605 /*
Peter Xua2beb5f2020-08-11 18:38:57 -07004606 * If the fault is done for GUP, regs will be NULL. We only do the
4607 * accounting for the per thread fault counters who triggered the
4608 * fault, and we skip the perf event updates.
Peter Xubce617e2020-08-11 18:37:44 -07004609 */
4610 if (!regs)
4611 return;
4612
Peter Xua2beb5f2020-08-11 18:38:57 -07004613 if (major)
Peter Xubce617e2020-08-11 18:37:44 -07004614 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
Peter Xua2beb5f2020-08-11 18:38:57 -07004615 else
Peter Xubce617e2020-08-11 18:37:44 -07004616 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
Peter Xubce617e2020-08-11 18:37:44 -07004617}
4618
Paul Cassella9a95f3c2014-08-06 16:07:24 -07004619/*
4620 * By the time we get here, we already hold the mm semaphore
4621 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07004622 * The mmap_lock may have been released depending on flags and our
Paul Cassella9a95f3c2014-08-06 16:07:24 -07004623 * return value. See filemap_fault() and __lock_page_or_retry().
4624 */
Souptick Joarder2b740302018-08-23 17:01:36 -07004625vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
Peter Xubce617e2020-08-11 18:37:44 -07004626 unsigned int flags, struct pt_regs *regs)
Johannes Weiner519e5242013-09-12 15:13:42 -07004627{
Souptick Joarder2b740302018-08-23 17:01:36 -07004628 vm_fault_t ret;
Johannes Weiner519e5242013-09-12 15:13:42 -07004629
4630 __set_current_state(TASK_RUNNING);
4631
4632 count_vm_event(PGFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07004633 count_memcg_event_mm(vma->vm_mm, PGFAULT);
Johannes Weiner519e5242013-09-12 15:13:42 -07004634
4635 /* do counter updates before entering really critical section. */
4636 check_sync_rss_stat(current);
4637
Laurent Dufourde0c7992017-09-08 16:13:12 -07004638 if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
4639 flags & FAULT_FLAG_INSTRUCTION,
4640 flags & FAULT_FLAG_REMOTE))
4641 return VM_FAULT_SIGSEGV;
4642
Johannes Weiner519e5242013-09-12 15:13:42 -07004643 /*
4644 * Enable the memcg OOM handling for faults triggered in user
4645 * space. Kernel faults are handled more gracefully.
4646 */
4647 if (flags & FAULT_FLAG_USER)
Michal Hocko29ef6802018-08-17 15:47:11 -07004648 mem_cgroup_enter_user_fault();
Johannes Weiner519e5242013-09-12 15:13:42 -07004649
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07004650 if (unlikely(is_vm_hugetlb_page(vma)))
4651 ret = hugetlb_fault(vma->vm_mm, vma, address, flags);
4652 else
4653 ret = __handle_mm_fault(vma, address, flags);
Johannes Weiner519e5242013-09-12 15:13:42 -07004654
Johannes Weiner49426422013-10-16 13:46:59 -07004655 if (flags & FAULT_FLAG_USER) {
Michal Hocko29ef6802018-08-17 15:47:11 -07004656 mem_cgroup_exit_user_fault();
Tobin C Harding166f61b2017-02-24 14:59:01 -08004657 /*
4658 * The task may have entered a memcg OOM situation but
4659 * if the allocation error was handled gracefully (no
4660 * VM_FAULT_OOM), there is no need to kill anything.
4661 * Just clean up the OOM state peacefully.
4662 */
4663 if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
4664 mem_cgroup_oom_synchronize(false);
Johannes Weiner49426422013-10-16 13:46:59 -07004665 }
Johannes Weiner3812c8c2013-09-12 15:13:44 -07004666
Peter Xubce617e2020-08-11 18:37:44 -07004667 mm_account_fault(regs, address, flags, ret);
4668
Johannes Weiner519e5242013-09-12 15:13:42 -07004669 return ret;
4670}
Jesse Barnese1d6d012014-12-12 16:55:27 -08004671EXPORT_SYMBOL_GPL(handle_mm_fault);
Johannes Weiner519e5242013-09-12 15:13:42 -07004672
Kirill A. Shutemov90eceff2017-03-09 17:24:08 +03004673#ifndef __PAGETABLE_P4D_FOLDED
4674/*
4675 * Allocate p4d page table.
4676 * We've already handled the fast-path in-line.
4677 */
4678int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
4679{
4680 p4d_t *new = p4d_alloc_one(mm, address);
4681 if (!new)
4682 return -ENOMEM;
4683
4684 smp_wmb(); /* See comment in __pte_alloc */
4685
4686 spin_lock(&mm->page_table_lock);
4687 if (pgd_present(*pgd)) /* Another has populated it */
4688 p4d_free(mm, new);
4689 else
4690 pgd_populate(mm, pgd, new);
4691 spin_unlock(&mm->page_table_lock);
4692 return 0;
4693}
4694#endif /* __PAGETABLE_P4D_FOLDED */
4695
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696#ifndef __PAGETABLE_PUD_FOLDED
4697/*
4698 * Allocate page upper directory.
Hugh Dickins872fec12005-10-29 18:16:21 -07004699 * We've already handled the fast-path in-line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 */
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004701int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702{
Hugh Dickinsc74df322005-10-29 18:16:23 -07004703 pud_t *new = pud_alloc_one(mm, address);
4704 if (!new)
Hugh Dickins1bb36302005-10-29 18:16:22 -07004705 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
Nick Piggin362a61a2008-05-14 06:37:36 +02004707 smp_wmb(); /* See comment in __pte_alloc */
4708
Hugh Dickins872fec12005-10-29 18:16:21 -07004709 spin_lock(&mm->page_table_lock);
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -08004710 if (!p4d_present(*p4d)) {
4711 mm_inc_nr_puds(mm);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004712 p4d_populate(mm, p4d, new);
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -08004713 } else /* Another has populated it */
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004714 pud_free(mm, new);
Hugh Dickinsc74df322005-10-29 18:16:23 -07004715 spin_unlock(&mm->page_table_lock);
Hugh Dickins1bb36302005-10-29 18:16:22 -07004716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717}
4718#endif /* __PAGETABLE_PUD_FOLDED */
4719
4720#ifndef __PAGETABLE_PMD_FOLDED
4721/*
4722 * Allocate page middle directory.
Hugh Dickins872fec12005-10-29 18:16:21 -07004723 * We've already handled the fast-path in-line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 */
Hugh Dickins1bb36302005-10-29 18:16:22 -07004725int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726{
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004727 spinlock_t *ptl;
Hugh Dickinsc74df322005-10-29 18:16:23 -07004728 pmd_t *new = pmd_alloc_one(mm, address);
4729 if (!new)
Hugh Dickins1bb36302005-10-29 18:16:22 -07004730 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
Nick Piggin362a61a2008-05-14 06:37:36 +02004732 smp_wmb(); /* See comment in __pte_alloc */
4733
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004734 ptl = pud_lock(mm, pud);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004735 if (!pud_present(*pud)) {
4736 mm_inc_nr_pmds(mm);
Hugh Dickins1bb36302005-10-29 18:16:22 -07004737 pud_populate(mm, pud, new);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08004738 } else /* Another has populated it */
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -08004739 pmd_free(mm, new);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08004740 spin_unlock(ptl);
Hugh Dickins1bb36302005-10-29 18:16:22 -07004741 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742}
4743#endif /* __PAGETABLE_PMD_FOLDED */
4744
Paolo Bonzini9fd6dad2021-02-05 05:07:11 -05004745int follow_invalidate_pte(struct mm_struct *mm, unsigned long address,
4746 struct mmu_notifier_range *range, pte_t **ptepp,
4747 pmd_t **pmdpp, spinlock_t **ptlp)
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004748{
4749 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004750 p4d_t *p4d;
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004751 pud_t *pud;
4752 pmd_t *pmd;
4753 pte_t *ptep;
4754
4755 pgd = pgd_offset(mm, address);
4756 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
4757 goto out;
4758
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03004759 p4d = p4d_offset(pgd, address);
4760 if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d)))
4761 goto out;
4762
4763 pud = pud_offset(p4d, address);
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004764 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
4765 goto out;
4766
4767 pmd = pmd_offset(pud, address);
Andrea Arcangelif66055ab2011-01-13 15:46:54 -08004768 VM_BUG_ON(pmd_trans_huge(*pmd));
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004769
Ross Zwisler09796392017-01-10 16:57:21 -08004770 if (pmd_huge(*pmd)) {
4771 if (!pmdpp)
4772 goto out;
4773
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004774 if (range) {
Jérôme Glisse7269f992019-05-13 17:20:53 -07004775 mmu_notifier_range_init(range, MMU_NOTIFY_CLEAR, 0,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07004776 NULL, mm, address & PMD_MASK,
4777 (address & PMD_MASK) + PMD_SIZE);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004778 mmu_notifier_invalidate_range_start(range);
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004779 }
Ross Zwisler09796392017-01-10 16:57:21 -08004780 *ptlp = pmd_lock(mm, pmd);
4781 if (pmd_huge(*pmd)) {
4782 *pmdpp = pmd;
4783 return 0;
4784 }
4785 spin_unlock(*ptlp);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004786 if (range)
4787 mmu_notifier_invalidate_range_end(range);
Ross Zwisler09796392017-01-10 16:57:21 -08004788 }
4789
4790 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004791 goto out;
4792
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004793 if (range) {
Jérôme Glisse7269f992019-05-13 17:20:53 -07004794 mmu_notifier_range_init(range, MMU_NOTIFY_CLEAR, 0, NULL, mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07004795 address & PAGE_MASK,
4796 (address & PAGE_MASK) + PAGE_SIZE);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004797 mmu_notifier_invalidate_range_start(range);
Jérôme Glissea4d1a882017-08-31 17:17:26 -04004798 }
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004799 ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004800 if (!pte_present(*ptep))
4801 goto unlock;
4802 *ptepp = ptep;
4803 return 0;
4804unlock:
4805 pte_unmap_unlock(ptep, *ptlp);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08004806 if (range)
4807 mmu_notifier_invalidate_range_end(range);
Johannes Weinerf8ad0f492009-06-16 15:32:33 -07004808out:
4809 return -EINVAL;
4810}
4811
Johannes Weiner3b6748e2009-06-16 15:32:35 -07004812/**
Paolo Bonzini9fd6dad2021-02-05 05:07:11 -05004813 * follow_pte - look up PTE at a user virtual address
4814 * @mm: the mm_struct of the target address space
4815 * @address: user virtual address
4816 * @ptepp: location to store found PTE
4817 * @ptlp: location to store the lock for the PTE
4818 *
4819 * On a successful return, the pointer to the PTE is stored in @ptepp;
4820 * the corresponding lock is taken and its location is stored in @ptlp.
4821 * The contents of the PTE are only stable until @ptlp is released;
4822 * any further use, if any, must be protected against invalidation
4823 * with MMU notifiers.
4824 *
4825 * Only IO mappings and raw PFN mappings are allowed. The mmap semaphore
4826 * should be taken for read.
4827 *
4828 * KVM uses this function. While it is arguably less bad than ``follow_pfn``,
4829 * it is not a good general-purpose API.
4830 *
4831 * Return: zero on success, -ve otherwise.
4832 */
4833int follow_pte(struct mm_struct *mm, unsigned long address,
4834 pte_t **ptepp, spinlock_t **ptlp)
4835{
4836 return follow_invalidate_pte(mm, address, NULL, ptepp, NULL, ptlp);
4837}
4838EXPORT_SYMBOL_GPL(follow_pte);
4839
4840/**
Johannes Weiner3b6748e2009-06-16 15:32:35 -07004841 * follow_pfn - look up PFN at a user virtual address
4842 * @vma: memory mapping
4843 * @address: user virtual address
4844 * @pfn: location to store found PFN
4845 *
4846 * Only IO mappings and raw PFN mappings are allowed.
4847 *
Paolo Bonzini9fd6dad2021-02-05 05:07:11 -05004848 * This function does not allow the caller to read the permissions
4849 * of the PTE. Do not use it.
4850 *
Mike Rapoporta862f682019-03-05 15:48:42 -08004851 * Return: zero and the pfn at @pfn on success, -ve otherwise.
Johannes Weiner3b6748e2009-06-16 15:32:35 -07004852 */
4853int follow_pfn(struct vm_area_struct *vma, unsigned long address,
4854 unsigned long *pfn)
4855{
4856 int ret = -EINVAL;
4857 spinlock_t *ptl;
4858 pte_t *ptep;
4859
4860 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
4861 return ret;
4862
Paolo Bonzini9fd6dad2021-02-05 05:07:11 -05004863 ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
Johannes Weiner3b6748e2009-06-16 15:32:35 -07004864 if (ret)
4865 return ret;
4866 *pfn = pte_pfn(*ptep);
4867 pte_unmap_unlock(ptep, ptl);
4868 return 0;
4869}
4870EXPORT_SYMBOL(follow_pfn);
4871
Rik van Riel28b2ee22008-07-23 21:27:05 -07004872#ifdef CONFIG_HAVE_IOREMAP_PROT
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004873int follow_phys(struct vm_area_struct *vma,
4874 unsigned long address, unsigned int flags,
4875 unsigned long *prot, resource_size_t *phys)
Rik van Riel28b2ee22008-07-23 21:27:05 -07004876{
Johannes Weiner03668a42009-06-16 15:32:34 -07004877 int ret = -EINVAL;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004878 pte_t *ptep, pte;
4879 spinlock_t *ptl;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004880
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004881 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
4882 goto out;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004883
Paolo Bonzini9fd6dad2021-02-05 05:07:11 -05004884 if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004885 goto out;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004886 pte = *ptep;
Johannes Weiner03668a42009-06-16 15:32:34 -07004887
Linus Torvaldsf6f37322017-12-15 18:53:22 -08004888 if ((flags & FOLL_WRITE) && !pte_write(pte))
Rik van Riel28b2ee22008-07-23 21:27:05 -07004889 goto unlock;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004890
4891 *prot = pgprot_val(pte_pgprot(pte));
Johannes Weiner03668a42009-06-16 15:32:34 -07004892 *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004893
Johannes Weiner03668a42009-06-16 15:32:34 -07004894 ret = 0;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004895unlock:
4896 pte_unmap_unlock(ptep, ptl);
4897out:
venkatesh.pallipadi@intel.comd87fe662008-12-19 13:47:27 -08004898 return ret;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004899}
4900
Daniel Vetter96667f82020-11-27 17:41:21 +01004901/**
4902 * generic_access_phys - generic implementation for iomem mmap access
4903 * @vma: the vma to access
Ingo Molnarf0953a12021-05-06 18:06:47 -07004904 * @addr: userspace address, not relative offset within @vma
Daniel Vetter96667f82020-11-27 17:41:21 +01004905 * @buf: buffer to read/write
4906 * @len: length of transfer
4907 * @write: set to FOLL_WRITE when writing, otherwise reading
4908 *
4909 * This is a generic implementation for &vm_operations_struct.access for an
4910 * iomem mapping. This callback is used by access_process_vm() when the @vma is
4911 * not page based.
4912 */
Rik van Riel28b2ee22008-07-23 21:27:05 -07004913int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
4914 void *buf, int len, int write)
4915{
4916 resource_size_t phys_addr;
4917 unsigned long prot = 0;
KOSAKI Motohiro2bc72732009-01-06 14:39:43 -08004918 void __iomem *maddr;
Daniel Vetter96667f82020-11-27 17:41:21 +01004919 pte_t *ptep, pte;
4920 spinlock_t *ptl;
4921 int offset = offset_in_page(addr);
4922 int ret = -EINVAL;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004923
Daniel Vetter96667f82020-11-27 17:41:21 +01004924 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
4925 return -EINVAL;
4926
4927retry:
Linus Torvaldse913a8c2021-02-22 17:45:02 -08004928 if (follow_pte(vma->vm_mm, addr, &ptep, &ptl))
Daniel Vetter96667f82020-11-27 17:41:21 +01004929 return -EINVAL;
4930 pte = *ptep;
4931 pte_unmap_unlock(ptep, ptl);
4932
4933 prot = pgprot_val(pte_pgprot(pte));
4934 phys_addr = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
4935
4936 if ((write & FOLL_WRITE) && !pte_write(pte))
Rik van Riel28b2ee22008-07-23 21:27:05 -07004937 return -EINVAL;
4938
Grazvydas Ignotas9cb12d72015-02-12 15:00:19 -08004939 maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot);
jie@chenjie6@huwei.com24eee1e2018-08-10 17:23:06 -07004940 if (!maddr)
4941 return -ENOMEM;
4942
Linus Torvaldse913a8c2021-02-22 17:45:02 -08004943 if (follow_pte(vma->vm_mm, addr, &ptep, &ptl))
Daniel Vetter96667f82020-11-27 17:41:21 +01004944 goto out_unmap;
4945
4946 if (!pte_same(pte, *ptep)) {
4947 pte_unmap_unlock(ptep, ptl);
4948 iounmap(maddr);
4949
4950 goto retry;
4951 }
4952
Rik van Riel28b2ee22008-07-23 21:27:05 -07004953 if (write)
4954 memcpy_toio(maddr + offset, buf, len);
4955 else
4956 memcpy_fromio(buf, maddr + offset, len);
Daniel Vetter96667f82020-11-27 17:41:21 +01004957 ret = len;
4958 pte_unmap_unlock(ptep, ptl);
4959out_unmap:
Rik van Riel28b2ee22008-07-23 21:27:05 -07004960 iounmap(maddr);
4961
Daniel Vetter96667f82020-11-27 17:41:21 +01004962 return ret;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004963}
Uwe Kleine-König5a736332013-08-07 13:02:52 +02004964EXPORT_SYMBOL_GPL(generic_access_phys);
Rik van Riel28b2ee22008-07-23 21:27:05 -07004965#endif
4966
David Howells0ec76a12006-09-27 01:50:15 -07004967/*
John Hubbardd3f5ffc2020-12-14 19:07:45 -08004968 * Access another process' address space as given in mm.
David Howells0ec76a12006-09-27 01:50:15 -07004969 */
John Hubbardd3f5ffc2020-12-14 19:07:45 -08004970int __access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf,
4971 int len, unsigned int gup_flags)
David Howells0ec76a12006-09-27 01:50:15 -07004972{
David Howells0ec76a12006-09-27 01:50:15 -07004973 struct vm_area_struct *vma;
David Howells0ec76a12006-09-27 01:50:15 -07004974 void *old_buf = buf;
Lorenzo Stoakes442486e2016-10-13 01:20:18 +01004975 int write = gup_flags & FOLL_WRITE;
David Howells0ec76a12006-09-27 01:50:15 -07004976
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07004977 if (mmap_read_lock_killable(mm))
Konstantin Khlebnikov1e426fe2019-07-11 21:00:07 -07004978 return 0;
4979
Simon Arlott183ff222007-10-20 01:27:18 +02004980 /* ignore errors, just check how much was successfully transferred */
David Howells0ec76a12006-09-27 01:50:15 -07004981 while (len) {
4982 int bytes, ret, offset;
4983 void *maddr;
Rik van Riel28b2ee22008-07-23 21:27:05 -07004984 struct page *page = NULL;
David Howells0ec76a12006-09-27 01:50:15 -07004985
Peter Xu64019a22020-08-11 18:39:01 -07004986 ret = get_user_pages_remote(mm, addr, 1,
Lorenzo Stoakes5b56d492016-12-14 15:06:52 -08004987 gup_flags, &page, &vma, NULL);
Rik van Riel28b2ee22008-07-23 21:27:05 -07004988 if (ret <= 0) {
Rik van Rieldbffcd02014-08-06 16:08:12 -07004989#ifndef CONFIG_HAVE_IOREMAP_PROT
4990 break;
4991#else
Rik van Riel28b2ee22008-07-23 21:27:05 -07004992 /*
4993 * Check if this is a VM_IO | VM_PFNMAP VMA, which
4994 * we can access using slightly different code.
4995 */
Liam Howlett3e418f92021-06-28 19:39:50 -07004996 vma = vma_lookup(mm, addr);
4997 if (!vma)
Rik van Riel28b2ee22008-07-23 21:27:05 -07004998 break;
4999 if (vma->vm_ops && vma->vm_ops->access)
5000 ret = vma->vm_ops->access(vma, addr, buf,
5001 len, write);
5002 if (ret <= 0)
Rik van Riel28b2ee22008-07-23 21:27:05 -07005003 break;
5004 bytes = ret;
Rik van Rieldbffcd02014-08-06 16:08:12 -07005005#endif
David Howells0ec76a12006-09-27 01:50:15 -07005006 } else {
Rik van Riel28b2ee22008-07-23 21:27:05 -07005007 bytes = len;
5008 offset = addr & (PAGE_SIZE-1);
5009 if (bytes > PAGE_SIZE-offset)
5010 bytes = PAGE_SIZE-offset;
5011
5012 maddr = kmap(page);
5013 if (write) {
5014 copy_to_user_page(vma, page, addr,
5015 maddr + offset, buf, bytes);
5016 set_page_dirty_lock(page);
5017 } else {
5018 copy_from_user_page(vma, page, addr,
5019 buf, maddr + offset, bytes);
5020 }
5021 kunmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005022 put_page(page);
David Howells0ec76a12006-09-27 01:50:15 -07005023 }
David Howells0ec76a12006-09-27 01:50:15 -07005024 len -= bytes;
5025 buf += bytes;
5026 addr += bytes;
5027 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07005028 mmap_read_unlock(mm);
David Howells0ec76a12006-09-27 01:50:15 -07005029
5030 return buf - old_buf;
5031}
Andi Kleen03252912008-01-30 13:33:18 +01005032
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04005033/**
Randy Dunlapae91dbf2011-03-26 13:27:01 -07005034 * access_remote_vm - access another process' address space
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04005035 * @mm: the mm_struct of the target address space
5036 * @addr: start address to access
5037 * @buf: source or destination buffer
5038 * @len: number of bytes to transfer
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01005039 * @gup_flags: flags modifying lookup behaviour
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04005040 *
5041 * The caller must hold a reference on @mm.
Mike Rapoporta862f682019-03-05 15:48:42 -08005042 *
5043 * Return: number of bytes copied from source to destination.
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04005044 */
5045int access_remote_vm(struct mm_struct *mm, unsigned long addr,
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01005046 void *buf, int len, unsigned int gup_flags)
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04005047{
John Hubbardd3f5ffc2020-12-14 19:07:45 -08005048 return __access_remote_vm(mm, addr, buf, len, gup_flags);
Stephen Wilson5ddd36b2011-03-13 15:49:20 -04005049}
5050
Andi Kleen03252912008-01-30 13:33:18 +01005051/*
Stephen Wilson206cb632011-03-13 15:49:19 -04005052 * Access another process' address space.
5053 * Source/target buffer must be kernel space,
5054 * Do not walk the page table directly, use get_user_pages
5055 */
5056int access_process_vm(struct task_struct *tsk, unsigned long addr,
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +01005057 void *buf, int len, unsigned int gup_flags)
Stephen Wilson206cb632011-03-13 15:49:19 -04005058{
5059 struct mm_struct *mm;
5060 int ret;
5061
5062 mm = get_task_mm(tsk);
5063 if (!mm)
5064 return 0;
5065
John Hubbardd3f5ffc2020-12-14 19:07:45 -08005066 ret = __access_remote_vm(mm, addr, buf, len, gup_flags);
Lorenzo Stoakes442486e2016-10-13 01:20:18 +01005067
Stephen Wilson206cb632011-03-13 15:49:19 -04005068 mmput(mm);
5069
5070 return ret;
5071}
Catalin Marinasfcd35852016-11-01 14:43:25 -07005072EXPORT_SYMBOL_GPL(access_process_vm);
Stephen Wilson206cb632011-03-13 15:49:19 -04005073
Andi Kleen03252912008-01-30 13:33:18 +01005074/*
5075 * Print the name of a VMA.
5076 */
5077void print_vma_addr(char *prefix, unsigned long ip)
5078{
5079 struct mm_struct *mm = current->mm;
5080 struct vm_area_struct *vma;
5081
Ingo Molnare8bff742008-02-13 20:21:06 +01005082 /*
Michal Hocko0a7f6822017-11-15 17:38:59 -08005083 * we might be running from an atomic context so we cannot sleep
Ingo Molnare8bff742008-02-13 20:21:06 +01005084 */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07005085 if (!mmap_read_trylock(mm))
Ingo Molnare8bff742008-02-13 20:21:06 +01005086 return;
5087
Andi Kleen03252912008-01-30 13:33:18 +01005088 vma = find_vma(mm, ip);
5089 if (vma && vma->vm_file) {
5090 struct file *f = vma->vm_file;
Michal Hocko0a7f6822017-11-15 17:38:59 -08005091 char *buf = (char *)__get_free_page(GFP_NOWAIT);
Andi Kleen03252912008-01-30 13:33:18 +01005092 if (buf) {
Andy Shevchenko2fbc57c2012-12-17 16:01:23 -08005093 char *p;
Andi Kleen03252912008-01-30 13:33:18 +01005094
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02005095 p = file_path(f, buf, PAGE_SIZE);
Andi Kleen03252912008-01-30 13:33:18 +01005096 if (IS_ERR(p))
5097 p = "?";
Andy Shevchenko2fbc57c2012-12-17 16:01:23 -08005098 printk("%s%s[%lx+%lx]", prefix, kbasename(p),
Andi Kleen03252912008-01-30 13:33:18 +01005099 vma->vm_start,
5100 vma->vm_end - vma->vm_start);
5101 free_page((unsigned long)buf);
5102 }
5103 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07005104 mmap_read_unlock(mm);
Andi Kleen03252912008-01-30 13:33:18 +01005105}
Nick Piggin3ee1afa2008-09-10 13:37:17 +02005106
Michael S. Tsirkin662bbcb2013-05-26 17:32:23 +03005107#if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
David Hildenbrand9ec23532015-05-11 17:52:07 +02005108void __might_fault(const char *file, int line)
Nick Piggin3ee1afa2008-09-10 13:37:17 +02005109{
Peter Zijlstra95156f02009-01-12 13:02:11 +01005110 /*
5111 * Some code (nfs/sunrpc) uses socket ops on kernel memory while
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07005112 * holding the mmap_lock, this is safe because kernel memory doesn't
Peter Zijlstra95156f02009-01-12 13:02:11 +01005113 * get paged out, therefore we'll never actually fault, and the
5114 * below annotations will generate false positives.
5115 */
Al Virodb68ce12017-03-20 21:08:07 -04005116 if (uaccess_kernel())
Peter Zijlstra95156f02009-01-12 13:02:11 +01005117 return;
David Hildenbrand9ec23532015-05-11 17:52:07 +02005118 if (pagefault_disabled())
Michael S. Tsirkin662bbcb2013-05-26 17:32:23 +03005119 return;
David Hildenbrand9ec23532015-05-11 17:52:07 +02005120 __might_sleep(file, line, 0);
5121#if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
Michael S. Tsirkin662bbcb2013-05-26 17:32:23 +03005122 if (current->mm)
Michel Lespinasseda1c55f2020-06-08 21:33:47 -07005123 might_lock_read(&current->mm->mmap_lock);
David Hildenbrand9ec23532015-05-11 17:52:07 +02005124#endif
Nick Piggin3ee1afa2008-09-10 13:37:17 +02005125}
David Hildenbrand9ec23532015-05-11 17:52:07 +02005126EXPORT_SYMBOL(__might_fault);
Nick Piggin3ee1afa2008-09-10 13:37:17 +02005127#endif
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08005128
5129#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
Huang Yingc6ddfb62018-08-17 15:45:46 -07005130/*
5131 * Process all subpages of the specified huge page with the specified
5132 * operation. The target subpage will be processed last to keep its
5133 * cache lines hot.
5134 */
5135static inline void process_huge_page(
5136 unsigned long addr_hint, unsigned int pages_per_huge_page,
5137 void (*process_subpage)(unsigned long addr, int idx, void *arg),
5138 void *arg)
5139{
5140 int i, n, base, l;
5141 unsigned long addr = addr_hint &
5142 ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
5143
5144 /* Process target subpage last to keep its cache lines hot */
5145 might_sleep();
5146 n = (addr_hint - addr) / PAGE_SIZE;
5147 if (2 * n <= pages_per_huge_page) {
5148 /* If target subpage in first half of huge page */
5149 base = 0;
5150 l = n;
5151 /* Process subpages at the end of huge page */
5152 for (i = pages_per_huge_page - 1; i >= 2 * n; i--) {
5153 cond_resched();
5154 process_subpage(addr + i * PAGE_SIZE, i, arg);
5155 }
5156 } else {
5157 /* If target subpage in second half of huge page */
5158 base = pages_per_huge_page - 2 * (pages_per_huge_page - n);
5159 l = pages_per_huge_page - n;
5160 /* Process subpages at the begin of huge page */
5161 for (i = 0; i < base; i++) {
5162 cond_resched();
5163 process_subpage(addr + i * PAGE_SIZE, i, arg);
5164 }
5165 }
5166 /*
5167 * Process remaining subpages in left-right-left-right pattern
5168 * towards the target subpage
5169 */
5170 for (i = 0; i < l; i++) {
5171 int left_idx = base + i;
5172 int right_idx = base + 2 * l - 1 - i;
5173
5174 cond_resched();
5175 process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg);
5176 cond_resched();
5177 process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg);
5178 }
5179}
5180
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08005181static void clear_gigantic_page(struct page *page,
5182 unsigned long addr,
5183 unsigned int pages_per_huge_page)
5184{
5185 int i;
5186 struct page *p = page;
5187
5188 might_sleep();
5189 for (i = 0; i < pages_per_huge_page;
5190 i++, p = mem_map_next(p, page, i)) {
5191 cond_resched();
5192 clear_user_highpage(p, addr + i * PAGE_SIZE);
5193 }
5194}
Huang Yingc6ddfb62018-08-17 15:45:46 -07005195
5196static void clear_subpage(unsigned long addr, int idx, void *arg)
5197{
5198 struct page *page = arg;
5199
5200 clear_user_highpage(page + idx, addr);
5201}
5202
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08005203void clear_huge_page(struct page *page,
Huang Yingc79b57e2017-09-06 16:25:04 -07005204 unsigned long addr_hint, unsigned int pages_per_huge_page)
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08005205{
Huang Yingc79b57e2017-09-06 16:25:04 -07005206 unsigned long addr = addr_hint &
5207 ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08005208
5209 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
5210 clear_gigantic_page(page, addr, pages_per_huge_page);
5211 return;
5212 }
5213
Huang Yingc6ddfb62018-08-17 15:45:46 -07005214 process_huge_page(addr_hint, pages_per_huge_page, clear_subpage, page);
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08005215}
5216
5217static void copy_user_gigantic_page(struct page *dst, struct page *src,
5218 unsigned long addr,
5219 struct vm_area_struct *vma,
5220 unsigned int pages_per_huge_page)
5221{
5222 int i;
5223 struct page *dst_base = dst;
5224 struct page *src_base = src;
5225
5226 for (i = 0; i < pages_per_huge_page; ) {
5227 cond_resched();
5228 copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
5229
5230 i++;
5231 dst = mem_map_next(dst, dst_base, i);
5232 src = mem_map_next(src, src_base, i);
5233 }
5234}
5235
Huang Yingc9f4cd72018-08-17 15:45:49 -07005236struct copy_subpage_arg {
5237 struct page *dst;
5238 struct page *src;
5239 struct vm_area_struct *vma;
5240};
5241
5242static void copy_subpage(unsigned long addr, int idx, void *arg)
5243{
5244 struct copy_subpage_arg *copy_arg = arg;
5245
5246 copy_user_highpage(copy_arg->dst + idx, copy_arg->src + idx,
5247 addr, copy_arg->vma);
5248}
5249
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08005250void copy_user_huge_page(struct page *dst, struct page *src,
Huang Yingc9f4cd72018-08-17 15:45:49 -07005251 unsigned long addr_hint, struct vm_area_struct *vma,
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08005252 unsigned int pages_per_huge_page)
5253{
Huang Yingc9f4cd72018-08-17 15:45:49 -07005254 unsigned long addr = addr_hint &
5255 ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1);
5256 struct copy_subpage_arg arg = {
5257 .dst = dst,
5258 .src = src,
5259 .vma = vma,
5260 };
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08005261
5262 if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
5263 copy_user_gigantic_page(dst, src, addr, vma,
5264 pages_per_huge_page);
5265 return;
5266 }
5267
Huang Yingc9f4cd72018-08-17 15:45:49 -07005268 process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg);
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08005269}
Mike Kravetzfa4d75c2017-02-22 15:42:49 -08005270
5271long copy_huge_page_from_user(struct page *dst_page,
5272 const void __user *usr_src,
Mike Kravetz810a56b2017-02-22 15:42:58 -08005273 unsigned int pages_per_huge_page,
5274 bool allow_pagefault)
Mike Kravetzfa4d75c2017-02-22 15:42:49 -08005275{
5276 void *src = (void *)usr_src;
5277 void *page_kaddr;
5278 unsigned long i, rc = 0;
5279 unsigned long ret_val = pages_per_huge_page * PAGE_SIZE;
Mike Kravetz3272cfc22021-02-24 12:07:54 -08005280 struct page *subpage = dst_page;
Mike Kravetzfa4d75c2017-02-22 15:42:49 -08005281
Mike Kravetz3272cfc22021-02-24 12:07:54 -08005282 for (i = 0; i < pages_per_huge_page;
5283 i++, subpage = mem_map_next(subpage, dst_page, i)) {
Mike Kravetz810a56b2017-02-22 15:42:58 -08005284 if (allow_pagefault)
Mike Kravetz3272cfc22021-02-24 12:07:54 -08005285 page_kaddr = kmap(subpage);
Mike Kravetz810a56b2017-02-22 15:42:58 -08005286 else
Mike Kravetz3272cfc22021-02-24 12:07:54 -08005287 page_kaddr = kmap_atomic(subpage);
Mike Kravetzfa4d75c2017-02-22 15:42:49 -08005288 rc = copy_from_user(page_kaddr,
5289 (const void __user *)(src + i * PAGE_SIZE),
5290 PAGE_SIZE);
Mike Kravetz810a56b2017-02-22 15:42:58 -08005291 if (allow_pagefault)
Mike Kravetz3272cfc22021-02-24 12:07:54 -08005292 kunmap(subpage);
Mike Kravetz810a56b2017-02-22 15:42:58 -08005293 else
5294 kunmap_atomic(page_kaddr);
Mike Kravetzfa4d75c2017-02-22 15:42:49 -08005295
5296 ret_val -= (PAGE_SIZE - rc);
5297 if (rc)
5298 break;
5299
5300 cond_resched();
5301 }
5302 return ret_val;
5303}
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08005304#endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08005305
Olof Johansson40b64ac2013-12-20 14:28:05 -08005306#if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08005307
5308static struct kmem_cache *page_ptl_cachep;
5309
5310void __init ptlock_cache_init(void)
5311{
5312 page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0,
5313 SLAB_PANIC, NULL);
5314}
5315
Peter Zijlstra539edb52013-11-14 14:31:52 -08005316bool ptlock_alloc(struct page *page)
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08005317{
5318 spinlock_t *ptl;
5319
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08005320 ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL);
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08005321 if (!ptl)
5322 return false;
Peter Zijlstra539edb52013-11-14 14:31:52 -08005323 page->ptl = ptl;
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08005324 return true;
5325}
5326
Peter Zijlstra539edb52013-11-14 14:31:52 -08005327void ptlock_free(struct page *page)
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08005328{
Kirill A. Shutemovb35f1812014-01-21 15:49:07 -08005329 kmem_cache_free(page_ptl_cachep, page->ptl);
Kirill A. Shutemov49076ec2013-11-14 14:31:51 -08005330}
5331#endif