Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Harding | 166f61b | 2017-02-24 14:59:01 -0800 | [diff] [blame] | 34 | * Idea by Alex Bligh (alex@cconcepts.co.uk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | * |
| 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 Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 44 | #include <linux/sched/mm.h> |
Ingo Molnar | f7ccbae | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 45 | #include <linux/sched/coredump.h> |
Ingo Molnar | 6a3827d | 2017-02-08 18:51:31 +0100 | [diff] [blame] | 46 | #include <linux/sched/numa_balancing.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 47 | #include <linux/sched/task.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #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 Glisse | 5042db4 | 2017-09-08 16:11:43 -0700 | [diff] [blame] | 53 | #include <linux/memremap.h> |
Hugh Dickins | 9a84089 | 2009-09-21 17:02:01 -0700 | [diff] [blame] | 54 | #include <linux/ksm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <linux/rmap.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 56 | #include <linux/export.h> |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 57 | #include <linux/delayacct.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #include <linux/init.h> |
Dan Williams | 01c8f1c | 2016-01-15 16:56:40 -0800 | [diff] [blame] | 59 | #include <linux/pfn_t.h> |
Peter Zijlstra | edc79b2 | 2006-09-25 23:30:58 -0700 | [diff] [blame] | 60 | #include <linux/writeback.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 61 | #include <linux/memcontrol.h> |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 62 | #include <linux/mmu_notifier.h> |
Hugh Dickins | 3dc1474 | 2009-01-06 14:40:08 -0800 | [diff] [blame] | 63 | #include <linux/swapops.h> |
| 64 | #include <linux/elf.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 65 | #include <linux/gfp.h> |
Mel Gorman | 4daae3b | 2012-11-02 11:33:45 +0000 | [diff] [blame] | 66 | #include <linux/migrate.h> |
Andy Shevchenko | 2fbc57c | 2012-12-17 16:01:23 -0800 | [diff] [blame] | 67 | #include <linux/string.h> |
Kirill A. Shutemov | 1592eef | 2014-04-07 15:37:22 -0700 | [diff] [blame] | 68 | #include <linux/debugfs.h> |
Andrea Arcangeli | 6b251fc | 2015-09-04 15:46:20 -0700 | [diff] [blame] | 69 | #include <linux/userfaultfd_k.h> |
Jan Kara | bc2466e | 2016-05-12 18:29:19 +0200 | [diff] [blame] | 70 | #include <linux/dax.h> |
Michal Hocko | 6b31d59 | 2017-08-18 15:16:15 -0700 | [diff] [blame] | 71 | #include <linux/oom.h> |
Anshuman Khandual | 98fa15f | 2019-03-05 15:42:58 -0800 | [diff] [blame] | 72 | #include <linux/numa.h> |
Peter Xu | bce617e | 2020-08-11 18:37:44 -0700 | [diff] [blame] | 73 | #include <linux/perf_event.h> |
| 74 | #include <linux/ptrace.h> |
Joerg Roedel | e80d390 | 2020-09-04 16:35:43 -0700 | [diff] [blame] | 75 | #include <linux/vmalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Joel Fernandes (Google) | b3d1411 | 2019-11-30 17:50:30 -0800 | [diff] [blame] | 77 | #include <trace/events/kmem.h> |
| 78 | |
Alexey Dobriyan | 6952b61 | 2009-09-18 23:55:55 +0400 | [diff] [blame] | 79 | #include <asm/io.h> |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 80 | #include <asm/mmu_context.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | #include <asm/pgalloc.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 82 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | #include <asm/tlb.h> |
| 84 | #include <asm/tlbflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Joerg Roedel | e80d390 | 2020-09-04 16:35:43 -0700 | [diff] [blame] | 86 | #include "pgalloc-track.h" |
Jan Beulich | 42b7772 | 2008-07-23 21:27:10 -0700 | [diff] [blame] | 87 | #include "internal.h" |
| 88 | |
Arnd Bergmann | af27d94 | 2018-02-16 16:25:53 +0100 | [diff] [blame] | 89 | #if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST) |
Peter Zijlstra | 9057289 | 2013-10-07 11:29:20 +0100 | [diff] [blame] | 90 | #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid. |
Peter Zijlstra | 75980e9 | 2013-02-22 16:34:32 -0800 | [diff] [blame] | 91 | #endif |
| 92 | |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 93 | #ifndef CONFIG_NEED_MULTIPLE_NODES |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | /* use the per-pgdat data instead for discontigmem - mbligh */ |
| 95 | unsigned long max_mapnr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | EXPORT_SYMBOL(max_mapnr); |
Tobin C Harding | 166f61b | 2017-02-24 14:59:01 -0800 | [diff] [blame] | 97 | |
| 98 | struct page *mem_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | EXPORT_SYMBOL(mem_map); |
| 100 | #endif |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | /* |
| 103 | * A number of key systems in x86 including ioremap() rely on the assumption |
| 104 | * that high_memory defines the upper bound on direct map memory, then end |
| 105 | * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and |
| 106 | * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL |
| 107 | * and ZONE_HIGHMEM. |
| 108 | */ |
Tobin C Harding | 166f61b | 2017-02-24 14:59:01 -0800 | [diff] [blame] | 109 | void *high_memory; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | EXPORT_SYMBOL(high_memory); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Ingo Molnar | 32a9323 | 2008-02-06 22:39:44 +0100 | [diff] [blame] | 112 | /* |
| 113 | * Randomize the address space (stacks, mmaps, brk, etc.). |
| 114 | * |
| 115 | * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization, |
| 116 | * as ancient (libc5 based) binaries can segfault. ) |
| 117 | */ |
| 118 | int randomize_va_space __read_mostly = |
| 119 | #ifdef CONFIG_COMPAT_BRK |
| 120 | 1; |
| 121 | #else |
| 122 | 2; |
| 123 | #endif |
Andi Kleen | a62eaf1 | 2006-02-16 23:41:58 +0100 | [diff] [blame] | 124 | |
Jia He | 83d116c | 2019-10-11 22:09:39 +0800 | [diff] [blame] | 125 | #ifndef arch_faults_on_old_pte |
| 126 | static inline bool arch_faults_on_old_pte(void) |
| 127 | { |
| 128 | /* |
| 129 | * Those arches which don't have hw access flag feature need to |
| 130 | * implement their own helper. By default, "true" means pagefault |
| 131 | * will be hit on old pte. |
| 132 | */ |
| 133 | return true; |
| 134 | } |
| 135 | #endif |
| 136 | |
Will Deacon | 46bdb42 | 2020-11-24 18:48:26 +0000 | [diff] [blame] | 137 | #ifndef arch_wants_old_prefaulted_pte |
| 138 | static inline bool arch_wants_old_prefaulted_pte(void) |
| 139 | { |
| 140 | /* |
| 141 | * Transitioning a PTE from 'old' to 'young' can be expensive on |
| 142 | * some architectures, even if it's performed in hardware. By |
| 143 | * default, "false" means prefaulted entries will be 'young'. |
| 144 | */ |
| 145 | return false; |
| 146 | } |
| 147 | #endif |
| 148 | |
Andi Kleen | a62eaf1 | 2006-02-16 23:41:58 +0100 | [diff] [blame] | 149 | static int __init disable_randmaps(char *s) |
| 150 | { |
| 151 | randomize_va_space = 0; |
OGAWA Hirofumi | 9b41046 | 2006-03-31 02:30:33 -0800 | [diff] [blame] | 152 | return 1; |
Andi Kleen | a62eaf1 | 2006-02-16 23:41:58 +0100 | [diff] [blame] | 153 | } |
| 154 | __setup("norandmaps", disable_randmaps); |
| 155 | |
Hugh Dickins | 62eede6 | 2009-09-21 17:03:34 -0700 | [diff] [blame] | 156 | unsigned long zero_pfn __read_mostly; |
Ard Biesheuvel | 0b70068 | 2014-09-12 22:17:23 +0200 | [diff] [blame] | 157 | EXPORT_SYMBOL(zero_pfn); |
| 158 | |
Tobin C Harding | 166f61b | 2017-02-24 14:59:01 -0800 | [diff] [blame] | 159 | unsigned long highest_memmap_pfn __read_mostly; |
| 160 | |
Hugh Dickins | a13ea5b | 2009-09-21 17:03:30 -0700 | [diff] [blame] | 161 | /* |
| 162 | * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init() |
| 163 | */ |
| 164 | static int __init init_zero_pfn(void) |
| 165 | { |
| 166 | zero_pfn = page_to_pfn(ZERO_PAGE(0)); |
| 167 | return 0; |
| 168 | } |
Ilya Lipnitskiy | e720e7d | 2021-03-29 21:42:08 -0700 | [diff] [blame] | 169 | early_initcall(init_zero_pfn); |
Andi Kleen | a62eaf1 | 2006-02-16 23:41:58 +0100 | [diff] [blame] | 170 | |
Joel Fernandes (Google) | e4dcad2 | 2019-11-30 17:50:33 -0800 | [diff] [blame] | 171 | void mm_trace_rss_stat(struct mm_struct *mm, int member, long count) |
Joel Fernandes (Google) | b3d1411 | 2019-11-30 17:50:30 -0800 | [diff] [blame] | 172 | { |
Joel Fernandes (Google) | e4dcad2 | 2019-11-30 17:50:33 -0800 | [diff] [blame] | 173 | trace_rss_stat(mm, member, count); |
Joel Fernandes (Google) | b3d1411 | 2019-11-30 17:50:30 -0800 | [diff] [blame] | 174 | } |
KAMEZAWA Hiroyuki | d559db0 | 2010-03-05 13:41:39 -0800 | [diff] [blame] | 175 | |
KAMEZAWA Hiroyuki | 34e5523 | 2010-03-05 13:41:40 -0800 | [diff] [blame] | 176 | #if defined(SPLIT_RSS_COUNTING) |
| 177 | |
David Rientjes | ea48cf7 | 2012-03-21 16:34:13 -0700 | [diff] [blame] | 178 | void sync_mm_rss(struct mm_struct *mm) |
KAMEZAWA Hiroyuki | 34e5523 | 2010-03-05 13:41:40 -0800 | [diff] [blame] | 179 | { |
| 180 | int i; |
| 181 | |
| 182 | for (i = 0; i < NR_MM_COUNTERS; i++) { |
David Rientjes | 05af2e1 | 2012-03-21 16:34:13 -0700 | [diff] [blame] | 183 | if (current->rss_stat.count[i]) { |
| 184 | add_mm_counter(mm, i, current->rss_stat.count[i]); |
| 185 | current->rss_stat.count[i] = 0; |
KAMEZAWA Hiroyuki | 34e5523 | 2010-03-05 13:41:40 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
David Rientjes | 05af2e1 | 2012-03-21 16:34:13 -0700 | [diff] [blame] | 188 | current->rss_stat.events = 0; |
KAMEZAWA Hiroyuki | 34e5523 | 2010-03-05 13:41:40 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static void add_mm_counter_fast(struct mm_struct *mm, int member, int val) |
| 192 | { |
| 193 | struct task_struct *task = current; |
| 194 | |
| 195 | if (likely(task->mm == mm)) |
| 196 | task->rss_stat.count[member] += val; |
| 197 | else |
| 198 | add_mm_counter(mm, member, val); |
| 199 | } |
| 200 | #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1) |
| 201 | #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1) |
| 202 | |
| 203 | /* sync counter once per 64 page faults */ |
| 204 | #define TASK_RSS_EVENTS_THRESH (64) |
| 205 | static void check_sync_rss_stat(struct task_struct *task) |
| 206 | { |
| 207 | if (unlikely(task != current)) |
| 208 | return; |
| 209 | if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH)) |
David Rientjes | ea48cf7 | 2012-03-21 16:34:13 -0700 | [diff] [blame] | 210 | sync_mm_rss(task->mm); |
KAMEZAWA Hiroyuki | 34e5523 | 2010-03-05 13:41:40 -0800 | [diff] [blame] | 211 | } |
Peter Zijlstra | 9547d01 | 2011-05-24 17:12:14 -0700 | [diff] [blame] | 212 | #else /* SPLIT_RSS_COUNTING */ |
KAMEZAWA Hiroyuki | 34e5523 | 2010-03-05 13:41:40 -0800 | [diff] [blame] | 213 | |
| 214 | #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member) |
| 215 | #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member) |
| 216 | |
| 217 | static void check_sync_rss_stat(struct task_struct *task) |
| 218 | { |
| 219 | } |
| 220 | |
Peter Zijlstra | 9547d01 | 2011-05-24 17:12:14 -0700 | [diff] [blame] | 221 | #endif /* SPLIT_RSS_COUNTING */ |
| 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | * Note: this doesn't free the actual pages themselves. That |
| 225 | * has been handled earlier when unmapping all the memory regions. |
| 226 | */ |
Benjamin Herrenschmidt | 9e1b32c | 2009-07-22 15:44:28 +1000 | [diff] [blame] | 227 | static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, |
| 228 | unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | { |
Martin Schwidefsky | 2f569af | 2008-02-08 04:22:04 -0800 | [diff] [blame] | 230 | pgtable_t token = pmd_pgtable(*pmd); |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 231 | pmd_clear(pmd); |
Benjamin Herrenschmidt | 9e1b32c | 2009-07-22 15:44:28 +1000 | [diff] [blame] | 232 | pte_free_tlb(tlb, token, addr); |
Kirill A. Shutemov | c481290 | 2017-11-15 17:35:37 -0800 | [diff] [blame] | 233 | mm_dec_nr_ptes(tlb->mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 236 | static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, |
| 237 | unsigned long addr, unsigned long end, |
| 238 | unsigned long floor, unsigned long ceiling) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
| 240 | pmd_t *pmd; |
| 241 | unsigned long next; |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 242 | unsigned long start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 244 | start = addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | pmd = pmd_offset(pud, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | do { |
| 247 | next = pmd_addr_end(addr, end); |
| 248 | if (pmd_none_or_clear_bad(pmd)) |
| 249 | continue; |
Benjamin Herrenschmidt | 9e1b32c | 2009-07-22 15:44:28 +1000 | [diff] [blame] | 250 | free_pte_range(tlb, pmd, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } while (pmd++, addr = next, addr != end); |
| 252 | |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 253 | start &= PUD_MASK; |
| 254 | if (start < floor) |
| 255 | return; |
| 256 | if (ceiling) { |
| 257 | ceiling &= PUD_MASK; |
| 258 | if (!ceiling) |
| 259 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 261 | if (end - 1 > ceiling - 1) |
| 262 | return; |
| 263 | |
| 264 | pmd = pmd_offset(pud, start); |
| 265 | pud_clear(pud); |
Benjamin Herrenschmidt | 9e1b32c | 2009-07-22 15:44:28 +1000 | [diff] [blame] | 266 | pmd_free_tlb(tlb, pmd, start); |
Kirill A. Shutemov | dc6c9a3 | 2015-02-11 15:26:50 -0800 | [diff] [blame] | 267 | mm_dec_nr_pmds(tlb->mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 270 | static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d, |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 271 | unsigned long addr, unsigned long end, |
| 272 | unsigned long floor, unsigned long ceiling) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { |
| 274 | pud_t *pud; |
| 275 | unsigned long next; |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 276 | unsigned long start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 278 | start = addr; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 279 | pud = pud_offset(p4d, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | do { |
| 281 | next = pud_addr_end(addr, end); |
| 282 | if (pud_none_or_clear_bad(pud)) |
| 283 | continue; |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 284 | free_pmd_range(tlb, pud, addr, next, floor, ceiling); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } while (pud++, addr = next, addr != end); |
| 286 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 287 | start &= P4D_MASK; |
| 288 | if (start < floor) |
| 289 | return; |
| 290 | if (ceiling) { |
| 291 | ceiling &= P4D_MASK; |
| 292 | if (!ceiling) |
| 293 | return; |
| 294 | } |
| 295 | if (end - 1 > ceiling - 1) |
| 296 | return; |
| 297 | |
| 298 | pud = pud_offset(p4d, start); |
| 299 | p4d_clear(p4d); |
| 300 | pud_free_tlb(tlb, pud, start); |
Kirill A. Shutemov | b4e98d9 | 2017-11-15 17:35:33 -0800 | [diff] [blame] | 301 | mm_dec_nr_puds(tlb->mm); |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd, |
| 305 | unsigned long addr, unsigned long end, |
| 306 | unsigned long floor, unsigned long ceiling) |
| 307 | { |
| 308 | p4d_t *p4d; |
| 309 | unsigned long next; |
| 310 | unsigned long start; |
| 311 | |
| 312 | start = addr; |
| 313 | p4d = p4d_offset(pgd, addr); |
| 314 | do { |
| 315 | next = p4d_addr_end(addr, end); |
| 316 | if (p4d_none_or_clear_bad(p4d)) |
| 317 | continue; |
| 318 | free_pud_range(tlb, p4d, addr, next, floor, ceiling); |
| 319 | } while (p4d++, addr = next, addr != end); |
| 320 | |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 321 | start &= PGDIR_MASK; |
| 322 | if (start < floor) |
| 323 | return; |
| 324 | if (ceiling) { |
| 325 | ceiling &= PGDIR_MASK; |
| 326 | if (!ceiling) |
| 327 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 329 | if (end - 1 > ceiling - 1) |
| 330 | return; |
| 331 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 332 | p4d = p4d_offset(pgd, start); |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 333 | pgd_clear(pgd); |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 334 | p4d_free_tlb(tlb, p4d, start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /* |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 338 | * This function frees user-level page tables of a process. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | */ |
Jan Beulich | 42b7772 | 2008-07-23 21:27:10 -0700 | [diff] [blame] | 340 | void free_pgd_range(struct mmu_gather *tlb, |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 341 | unsigned long addr, unsigned long end, |
| 342 | unsigned long floor, unsigned long ceiling) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | { |
| 344 | pgd_t *pgd; |
| 345 | unsigned long next; |
| 346 | |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 347 | /* |
| 348 | * The next few lines have given us lots of grief... |
| 349 | * |
| 350 | * Why are we testing PMD* at this top level? Because often |
| 351 | * there will be no work to do at all, and we'd prefer not to |
| 352 | * go all the way down to the bottom just to discover that. |
| 353 | * |
| 354 | * Why all these "- 1"s? Because 0 represents both the bottom |
| 355 | * of the address space and the top of it (using -1 for the |
| 356 | * top wouldn't help much: the masks would do the wrong thing). |
| 357 | * The rule is that addr 0 and floor 0 refer to the bottom of |
| 358 | * the address space, but end 0 and ceiling 0 refer to the top |
| 359 | * Comparisons need to use "end - 1" and "ceiling - 1" (though |
| 360 | * that end 0 case should be mythical). |
| 361 | * |
| 362 | * Wherever addr is brought up or ceiling brought down, we must |
| 363 | * be careful to reject "the opposite 0" before it confuses the |
| 364 | * subsequent tests. But what about where end is brought down |
| 365 | * by PMD_SIZE below? no, end can't go down to 0 there. |
| 366 | * |
| 367 | * Whereas we round start (addr) and ceiling down, by different |
| 368 | * masks at different levels, in order to test whether a table |
| 369 | * now has no other vmas using it, so can be freed, we don't |
| 370 | * bother to round floor or end up - the tests don't need that. |
| 371 | */ |
| 372 | |
| 373 | addr &= PMD_MASK; |
| 374 | if (addr < floor) { |
| 375 | addr += PMD_SIZE; |
| 376 | if (!addr) |
| 377 | return; |
| 378 | } |
| 379 | if (ceiling) { |
| 380 | ceiling &= PMD_MASK; |
| 381 | if (!ceiling) |
| 382 | return; |
| 383 | } |
| 384 | if (end - 1 > ceiling - 1) |
| 385 | end -= PMD_SIZE; |
| 386 | if (addr > end - 1) |
| 387 | return; |
Aneesh Kumar K.V | 07e3266 | 2016-12-12 16:42:40 -0800 | [diff] [blame] | 388 | /* |
| 389 | * We add page table cache pages with PAGE_SIZE, |
| 390 | * (see pte_free_tlb()), flush the tlb if we need |
| 391 | */ |
Peter Zijlstra | ed6a793 | 2018-08-31 14:46:08 +0200 | [diff] [blame] | 392 | tlb_change_page_size(tlb, PAGE_SIZE); |
Jan Beulich | 42b7772 | 2008-07-23 21:27:10 -0700 | [diff] [blame] | 393 | pgd = pgd_offset(tlb->mm, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | do { |
| 395 | next = pgd_addr_end(addr, end); |
| 396 | if (pgd_none_or_clear_bad(pgd)) |
| 397 | continue; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 398 | free_p4d_range(tlb, pgd, addr, next, floor, ceiling); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | } while (pgd++, addr = next, addr != end); |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Jan Beulich | 42b7772 | 2008-07-23 21:27:10 -0700 | [diff] [blame] | 402 | void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, |
Hugh Dickins | 3bf5ee9 | 2005-04-19 13:29:16 -0700 | [diff] [blame] | 403 | unsigned long floor, unsigned long ceiling) |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 404 | { |
| 405 | while (vma) { |
| 406 | struct vm_area_struct *next = vma->vm_next; |
| 407 | unsigned long addr = vma->vm_start; |
| 408 | |
Hugh Dickins | 8f4f8c1 | 2005-10-29 18:16:29 -0700 | [diff] [blame] | 409 | /* |
npiggin@suse.de | 25d9e2d | 2009-08-21 02:35:05 +1000 | [diff] [blame] | 410 | * Hide vma from rmap and truncate_pagecache before freeing |
| 411 | * pgtables |
Hugh Dickins | 8f4f8c1 | 2005-10-29 18:16:29 -0700 | [diff] [blame] | 412 | */ |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 413 | unlink_anon_vmas(vma); |
Hugh Dickins | 8f4f8c1 | 2005-10-29 18:16:29 -0700 | [diff] [blame] | 414 | unlink_file_vma(vma); |
| 415 | |
David Gibson | 9da61ae | 2006-03-22 00:08:57 -0800 | [diff] [blame] | 416 | if (is_vm_hugetlb_page(vma)) { |
Hugh Dickins | 3bf5ee9 | 2005-04-19 13:29:16 -0700 | [diff] [blame] | 417 | hugetlb_free_pgd_range(tlb, addr, vma->vm_end, |
Tobin C Harding | 166f61b | 2017-02-24 14:59:01 -0800 | [diff] [blame] | 418 | floor, next ? next->vm_start : ceiling); |
Hugh Dickins | 3bf5ee9 | 2005-04-19 13:29:16 -0700 | [diff] [blame] | 419 | } else { |
| 420 | /* |
| 421 | * Optimization: gather nearby vmas into one call down |
| 422 | */ |
| 423 | while (next && next->vm_start <= vma->vm_end + PMD_SIZE |
David Gibson | 4866920 | 2006-03-22 00:08:58 -0800 | [diff] [blame] | 424 | && !is_vm_hugetlb_page(next)) { |
Hugh Dickins | 3bf5ee9 | 2005-04-19 13:29:16 -0700 | [diff] [blame] | 425 | vma = next; |
| 426 | next = vma->vm_next; |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 427 | unlink_anon_vmas(vma); |
Hugh Dickins | 8f4f8c1 | 2005-10-29 18:16:29 -0700 | [diff] [blame] | 428 | unlink_file_vma(vma); |
Hugh Dickins | 3bf5ee9 | 2005-04-19 13:29:16 -0700 | [diff] [blame] | 429 | } |
| 430 | free_pgd_range(tlb, addr, vma->vm_end, |
Tobin C Harding | 166f61b | 2017-02-24 14:59:01 -0800 | [diff] [blame] | 431 | floor, next ? next->vm_start : ceiling); |
Hugh Dickins | 3bf5ee9 | 2005-04-19 13:29:16 -0700 | [diff] [blame] | 432 | } |
Hugh Dickins | e0da382 | 2005-04-19 13:29:15 -0700 | [diff] [blame] | 433 | vma = next; |
| 434 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Joel Fernandes (Google) | 4cf5892 | 2019-01-03 15:28:34 -0800 | [diff] [blame] | 437 | int __pte_alloc(struct mm_struct *mm, pmd_t *pmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | { |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 439 | spinlock_t *ptl; |
Joel Fernandes (Google) | 4cf5892 | 2019-01-03 15:28:34 -0800 | [diff] [blame] | 440 | pgtable_t new = pte_alloc_one(mm); |
Hugh Dickins | 1bb3630 | 2005-10-29 18:16:22 -0700 | [diff] [blame] | 441 | if (!new) |
| 442 | return -ENOMEM; |
| 443 | |
Nick Piggin | 362a61a | 2008-05-14 06:37:36 +0200 | [diff] [blame] | 444 | /* |
| 445 | * Ensure all pte setup (eg. pte page lock and page clearing) are |
| 446 | * visible before the pte is made visible to other CPUs by being |
| 447 | * put into page tables. |
| 448 | * |
| 449 | * The other side of the story is the pointer chasing in the page |
| 450 | * table walking code (when walking the page table without locking; |
| 451 | * ie. most of the time). Fortunately, these data accesses consist |
| 452 | * of a chain of data-dependent loads, meaning most CPUs (alpha |
| 453 | * being the notable exception) will already guarantee loads are |
| 454 | * seen in-order. See the alpha page table accessors for the |
Will Deacon | bb7cdd3 | 2019-10-30 17:15:01 +0000 | [diff] [blame] | 455 | * smp_rmb() barriers in page table walking code. |
Nick Piggin | 362a61a | 2008-05-14 06:37:36 +0200 | [diff] [blame] | 456 | */ |
| 457 | smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ |
| 458 | |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 459 | ptl = pmd_lock(mm, pmd); |
Andrea Arcangeli | 8ac1f83 | 2011-01-13 15:46:43 -0800 | [diff] [blame] | 460 | if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ |
Kirill A. Shutemov | c481290 | 2017-11-15 17:35:37 -0800 | [diff] [blame] | 461 | mm_inc_nr_ptes(mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | pmd_populate(mm, pmd, new); |
Martin Schwidefsky | 2f569af | 2008-02-08 04:22:04 -0800 | [diff] [blame] | 463 | new = NULL; |
Kirill A. Shutemov | 4b471e8 | 2016-01-15 16:53:39 -0800 | [diff] [blame] | 464 | } |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 465 | spin_unlock(ptl); |
Martin Schwidefsky | 2f569af | 2008-02-08 04:22:04 -0800 | [diff] [blame] | 466 | if (new) |
| 467 | pte_free(mm, new); |
Hugh Dickins | 1bb3630 | 2005-10-29 18:16:22 -0700 | [diff] [blame] | 468 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Joel Fernandes (Google) | 4cf5892 | 2019-01-03 15:28:34 -0800 | [diff] [blame] | 471 | int __pte_alloc_kernel(pmd_t *pmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | { |
Joel Fernandes (Google) | 4cf5892 | 2019-01-03 15:28:34 -0800 | [diff] [blame] | 473 | pte_t *new = pte_alloc_one_kernel(&init_mm); |
Hugh Dickins | 1bb3630 | 2005-10-29 18:16:22 -0700 | [diff] [blame] | 474 | if (!new) |
| 475 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
Nick Piggin | 362a61a | 2008-05-14 06:37:36 +0200 | [diff] [blame] | 477 | smp_wmb(); /* See comment in __pte_alloc */ |
| 478 | |
Hugh Dickins | 1bb3630 | 2005-10-29 18:16:22 -0700 | [diff] [blame] | 479 | spin_lock(&init_mm.page_table_lock); |
Andrea Arcangeli | 8ac1f83 | 2011-01-13 15:46:43 -0800 | [diff] [blame] | 480 | if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ |
Hugh Dickins | 1bb3630 | 2005-10-29 18:16:22 -0700 | [diff] [blame] | 481 | pmd_populate_kernel(&init_mm, pmd, new); |
Martin Schwidefsky | 2f569af | 2008-02-08 04:22:04 -0800 | [diff] [blame] | 482 | new = NULL; |
Kirill A. Shutemov | 4b471e8 | 2016-01-15 16:53:39 -0800 | [diff] [blame] | 483 | } |
Hugh Dickins | 1bb3630 | 2005-10-29 18:16:22 -0700 | [diff] [blame] | 484 | spin_unlock(&init_mm.page_table_lock); |
Martin Schwidefsky | 2f569af | 2008-02-08 04:22:04 -0800 | [diff] [blame] | 485 | if (new) |
| 486 | pte_free_kernel(&init_mm, new); |
Hugh Dickins | 1bb3630 | 2005-10-29 18:16:22 -0700 | [diff] [blame] | 487 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | } |
| 489 | |
KAMEZAWA Hiroyuki | d559db0 | 2010-03-05 13:41:39 -0800 | [diff] [blame] | 490 | static inline void init_rss_vec(int *rss) |
Hugh Dickins | ae85976 | 2005-10-29 18:16:05 -0700 | [diff] [blame] | 491 | { |
KAMEZAWA Hiroyuki | d559db0 | 2010-03-05 13:41:39 -0800 | [diff] [blame] | 492 | memset(rss, 0, sizeof(int) * NR_MM_COUNTERS); |
| 493 | } |
| 494 | |
| 495 | static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss) |
| 496 | { |
| 497 | int i; |
| 498 | |
KAMEZAWA Hiroyuki | 34e5523 | 2010-03-05 13:41:40 -0800 | [diff] [blame] | 499 | if (current->mm == mm) |
David Rientjes | 05af2e1 | 2012-03-21 16:34:13 -0700 | [diff] [blame] | 500 | sync_mm_rss(mm); |
KAMEZAWA Hiroyuki | d559db0 | 2010-03-05 13:41:39 -0800 | [diff] [blame] | 501 | for (i = 0; i < NR_MM_COUNTERS; i++) |
| 502 | if (rss[i]) |
| 503 | add_mm_counter(mm, i, rss[i]); |
Hugh Dickins | ae85976 | 2005-10-29 18:16:05 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | /* |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 507 | * This function is called to print an error when a bad pte |
| 508 | * is found. For example, we might have a PFN-mapped pte in |
| 509 | * a region that doesn't allow it. |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 510 | * |
| 511 | * The calling function must still handle the error. |
| 512 | */ |
Hugh Dickins | 3dc1474 | 2009-01-06 14:40:08 -0800 | [diff] [blame] | 513 | static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, |
| 514 | pte_t pte, struct page *page) |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 515 | { |
Hugh Dickins | 3dc1474 | 2009-01-06 14:40:08 -0800 | [diff] [blame] | 516 | pgd_t *pgd = pgd_offset(vma->vm_mm, addr); |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 517 | p4d_t *p4d = p4d_offset(pgd, addr); |
| 518 | pud_t *pud = pud_offset(p4d, addr); |
Hugh Dickins | 3dc1474 | 2009-01-06 14:40:08 -0800 | [diff] [blame] | 519 | pmd_t *pmd = pmd_offset(pud, addr); |
| 520 | struct address_space *mapping; |
| 521 | pgoff_t index; |
Hugh Dickins | d936cf9 | 2009-01-06 14:40:12 -0800 | [diff] [blame] | 522 | static unsigned long resume; |
| 523 | static unsigned long nr_shown; |
| 524 | static unsigned long nr_unshown; |
| 525 | |
| 526 | /* |
| 527 | * Allow a burst of 60 reports, then keep quiet for that minute; |
| 528 | * or allow a steady drip of one report per second. |
| 529 | */ |
| 530 | if (nr_shown == 60) { |
| 531 | if (time_before(jiffies, resume)) { |
| 532 | nr_unshown++; |
| 533 | return; |
| 534 | } |
| 535 | if (nr_unshown) { |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 536 | pr_alert("BUG: Bad page map: %lu messages suppressed\n", |
| 537 | nr_unshown); |
Hugh Dickins | d936cf9 | 2009-01-06 14:40:12 -0800 | [diff] [blame] | 538 | nr_unshown = 0; |
| 539 | } |
| 540 | nr_shown = 0; |
| 541 | } |
| 542 | if (nr_shown++ == 0) |
| 543 | resume = jiffies + 60 * HZ; |
Hugh Dickins | 3dc1474 | 2009-01-06 14:40:08 -0800 | [diff] [blame] | 544 | |
| 545 | mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; |
| 546 | index = linear_page_index(vma, addr); |
| 547 | |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 548 | pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", |
| 549 | current->comm, |
| 550 | (long long)pte_val(pte), (long long)pmd_val(*pmd)); |
Wu Fengguang | 718a382 | 2010-03-10 15:20:43 -0800 | [diff] [blame] | 551 | if (page) |
Dave Hansen | f0b791a | 2014-01-23 15:52:49 -0800 | [diff] [blame] | 552 | dump_page(page, "bad pte"); |
Kefeng Wang | 6aa9b8b | 2019-09-23 15:35:34 -0700 | [diff] [blame] | 553 | pr_alert("addr:%px vm_flags:%08lx anon_vma:%px mapping:%px index:%lx\n", |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 554 | (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); |
Sakari Ailus | d75f773 | 2019-03-25 21:32:28 +0200 | [diff] [blame] | 555 | pr_alert("file:%pD fault:%ps mmap:%ps readpage:%ps\n", |
Konstantin Khlebnikov | 2682582 | 2015-04-15 16:15:08 -0700 | [diff] [blame] | 556 | vma->vm_file, |
| 557 | vma->vm_ops ? vma->vm_ops->fault : NULL, |
| 558 | vma->vm_file ? vma->vm_file->f_op->mmap : NULL, |
| 559 | mapping ? mapping->a_ops->readpage : NULL); |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 560 | dump_stack(); |
Rusty Russell | 373d4d0 | 2013-01-21 17:17:39 +1030 | [diff] [blame] | 561 | add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /* |
Nick Piggin | 7e67513 | 2008-04-28 02:13:00 -0700 | [diff] [blame] | 565 | * vm_normal_page -- This function gets the "struct page" associated with a pte. |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 566 | * |
Nick Piggin | 7e67513 | 2008-04-28 02:13:00 -0700 | [diff] [blame] | 567 | * "Special" mappings do not wish to be associated with a "struct page" (either |
| 568 | * it doesn't exist, or it exists but they don't want to touch it). In this |
| 569 | * case, NULL is returned here. "Normal" mappings do have a struct page. |
Jared Hulbert | b379d79 | 2008-04-28 02:12:58 -0700 | [diff] [blame] | 570 | * |
Nick Piggin | 7e67513 | 2008-04-28 02:13:00 -0700 | [diff] [blame] | 571 | * There are 2 broad cases. Firstly, an architecture may define a pte_special() |
| 572 | * pte bit, in which case this function is trivial. Secondly, an architecture |
| 573 | * may not have a spare pte bit, which requires a more complicated scheme, |
| 574 | * described below. |
| 575 | * |
| 576 | * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a |
| 577 | * special mapping (even if there are underlying and valid "struct pages"). |
| 578 | * COWed pages of a VM_PFNMAP are always normal. |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 579 | * |
Jared Hulbert | b379d79 | 2008-04-28 02:12:58 -0700 | [diff] [blame] | 580 | * The way we recognize COWed pages within VM_PFNMAP mappings is through the |
| 581 | * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit |
Nick Piggin | 7e67513 | 2008-04-28 02:13:00 -0700 | [diff] [blame] | 582 | * set, and the vm_pgoff will point to the first PFN mapped: thus every special |
| 583 | * mapping will always honor the rule |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 584 | * |
| 585 | * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) |
| 586 | * |
Nick Piggin | 7e67513 | 2008-04-28 02:13:00 -0700 | [diff] [blame] | 587 | * And for normal mappings this is false. |
Jared Hulbert | b379d79 | 2008-04-28 02:12:58 -0700 | [diff] [blame] | 588 | * |
Nick Piggin | 7e67513 | 2008-04-28 02:13:00 -0700 | [diff] [blame] | 589 | * This restricts such mappings to be a linear translation from virtual address |
| 590 | * to pfn. To get around this restriction, we allow arbitrary mappings so long |
| 591 | * as the vma is not a COW mapping; in that case, we know that all ptes are |
| 592 | * special (because none can have been COWed). |
Jared Hulbert | b379d79 | 2008-04-28 02:12:58 -0700 | [diff] [blame] | 593 | * |
| 594 | * |
Nick Piggin | 7e67513 | 2008-04-28 02:13:00 -0700 | [diff] [blame] | 595 | * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. |
| 596 | * |
Jared Hulbert | b379d79 | 2008-04-28 02:12:58 -0700 | [diff] [blame] | 597 | * VM_MIXEDMAP mappings can likewise contain memory with or without "struct |
| 598 | * page" backing, however the difference is that _all_ pages with a struct |
| 599 | * page (that is, those where pfn_valid is true) are refcounted and considered |
| 600 | * normal pages by the VM. The disadvantage is that pages are refcounted |
| 601 | * (which can be slower and simply not an option for some PFNMAP users). The |
| 602 | * advantage is that we don't have to follow the strict linearity rule of |
| 603 | * PFNMAP mappings in order to support COWable mappings. |
| 604 | * |
Hugh Dickins | ee498ed | 2005-11-21 21:32:18 -0800 | [diff] [blame] | 605 | */ |
Christoph Hellwig | 25b2995 | 2019-06-13 22:50:49 +0200 | [diff] [blame] | 606 | struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, |
| 607 | pte_t pte) |
Hugh Dickins | ee498ed | 2005-11-21 21:32:18 -0800 | [diff] [blame] | 608 | { |
Hugh Dickins | 22b31ee | 2009-01-06 14:40:09 -0800 | [diff] [blame] | 609 | unsigned long pfn = pte_pfn(pte); |
Nick Piggin | 7e67513 | 2008-04-28 02:13:00 -0700 | [diff] [blame] | 610 | |
Laurent Dufour | 00b3a33 | 2018-06-07 17:06:12 -0700 | [diff] [blame] | 611 | if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL)) { |
Hugh Dickins | b38af47 | 2014-08-29 15:18:44 -0700 | [diff] [blame] | 612 | if (likely(!pte_special(pte))) |
Hugh Dickins | 22b31ee | 2009-01-06 14:40:09 -0800 | [diff] [blame] | 613 | goto check_pfn; |
David Vrabel | 667a0a0 | 2014-12-18 14:48:15 +0000 | [diff] [blame] | 614 | if (vma->vm_ops && vma->vm_ops->find_special_page) |
| 615 | return vma->vm_ops->find_special_page(vma, addr); |
Hugh Dickins | a13ea5b | 2009-09-21 17:03:30 -0700 | [diff] [blame] | 616 | if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) |
| 617 | return NULL; |
Jérôme Glisse | df6ad69 | 2017-09-08 16:12:24 -0700 | [diff] [blame] | 618 | if (is_zero_pfn(pfn)) |
| 619 | return NULL; |
Dave Jiang | e1fb4a0 | 2018-08-17 15:43:40 -0700 | [diff] [blame] | 620 | if (pte_devmap(pte)) |
| 621 | return NULL; |
| 622 | |
Jérôme Glisse | df6ad69 | 2017-09-08 16:12:24 -0700 | [diff] [blame] | 623 | print_bad_pte(vma, addr, pte, NULL); |
Nick Piggin | 7e67513 | 2008-04-28 02:13:00 -0700 | [diff] [blame] | 624 | return NULL; |
| 625 | } |
| 626 | |
Laurent Dufour | 00b3a33 | 2018-06-07 17:06:12 -0700 | [diff] [blame] | 627 | /* !CONFIG_ARCH_HAS_PTE_SPECIAL case follows: */ |
Nick Piggin | 7e67513 | 2008-04-28 02:13:00 -0700 | [diff] [blame] | 628 | |
Jared Hulbert | b379d79 | 2008-04-28 02:12:58 -0700 | [diff] [blame] | 629 | if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { |
| 630 | if (vma->vm_flags & VM_MIXEDMAP) { |
| 631 | if (!pfn_valid(pfn)) |
| 632 | return NULL; |
| 633 | goto out; |
| 634 | } else { |
Nick Piggin | 7e67513 | 2008-04-28 02:13:00 -0700 | [diff] [blame] | 635 | unsigned long off; |
| 636 | off = (addr - vma->vm_start) >> PAGE_SHIFT; |
Jared Hulbert | b379d79 | 2008-04-28 02:12:58 -0700 | [diff] [blame] | 637 | if (pfn == vma->vm_pgoff + off) |
| 638 | return NULL; |
| 639 | if (!is_cow_mapping(vma->vm_flags)) |
| 640 | return NULL; |
| 641 | } |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Hugh Dickins | b38af47 | 2014-08-29 15:18:44 -0700 | [diff] [blame] | 644 | if (is_zero_pfn(pfn)) |
| 645 | return NULL; |
Laurent Dufour | 00b3a33 | 2018-06-07 17:06:12 -0700 | [diff] [blame] | 646 | |
Hugh Dickins | 22b31ee | 2009-01-06 14:40:09 -0800 | [diff] [blame] | 647 | check_pfn: |
| 648 | if (unlikely(pfn > highest_memmap_pfn)) { |
| 649 | print_bad_pte(vma, addr, pte, NULL); |
| 650 | return NULL; |
| 651 | } |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 652 | |
| 653 | /* |
Nick Piggin | 7e67513 | 2008-04-28 02:13:00 -0700 | [diff] [blame] | 654 | * NOTE! We still have PageReserved() pages in the page tables. |
Nick Piggin | 7e67513 | 2008-04-28 02:13:00 -0700 | [diff] [blame] | 655 | * eg. VDSO mappings can cause them to exist. |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 656 | */ |
Jared Hulbert | b379d79 | 2008-04-28 02:12:58 -0700 | [diff] [blame] | 657 | out: |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 658 | return pfn_to_page(pfn); |
Hugh Dickins | ee498ed | 2005-11-21 21:32:18 -0800 | [diff] [blame] | 659 | } |
| 660 | |
Gerald Schaefer | 28093f9 | 2016-04-28 16:18:35 -0700 | [diff] [blame] | 661 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 662 | struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr, |
| 663 | pmd_t pmd) |
| 664 | { |
| 665 | unsigned long pfn = pmd_pfn(pmd); |
| 666 | |
| 667 | /* |
| 668 | * There is no pmd_special() but there may be special pmds, e.g. |
| 669 | * in a direct-access (dax) mapping, so let's just replicate the |
Laurent Dufour | 00b3a33 | 2018-06-07 17:06:12 -0700 | [diff] [blame] | 670 | * !CONFIG_ARCH_HAS_PTE_SPECIAL case from vm_normal_page() here. |
Gerald Schaefer | 28093f9 | 2016-04-28 16:18:35 -0700 | [diff] [blame] | 671 | */ |
| 672 | if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { |
| 673 | if (vma->vm_flags & VM_MIXEDMAP) { |
| 674 | if (!pfn_valid(pfn)) |
| 675 | return NULL; |
| 676 | goto out; |
| 677 | } else { |
| 678 | unsigned long off; |
| 679 | off = (addr - vma->vm_start) >> PAGE_SHIFT; |
| 680 | if (pfn == vma->vm_pgoff + off) |
| 681 | return NULL; |
| 682 | if (!is_cow_mapping(vma->vm_flags)) |
| 683 | return NULL; |
| 684 | } |
| 685 | } |
| 686 | |
Dave Jiang | e1fb4a0 | 2018-08-17 15:43:40 -0700 | [diff] [blame] | 687 | if (pmd_devmap(pmd)) |
| 688 | return NULL; |
Yu Zhao | 3cde287 | 2019-12-04 16:49:56 -0800 | [diff] [blame] | 689 | if (is_huge_zero_pmd(pmd)) |
Gerald Schaefer | 28093f9 | 2016-04-28 16:18:35 -0700 | [diff] [blame] | 690 | return NULL; |
| 691 | if (unlikely(pfn > highest_memmap_pfn)) |
| 692 | return NULL; |
| 693 | |
| 694 | /* |
| 695 | * NOTE! We still have PageReserved() pages in the page tables. |
| 696 | * eg. VDSO mappings can cause them to exist. |
| 697 | */ |
| 698 | out: |
| 699 | return pfn_to_page(pfn); |
| 700 | } |
| 701 | #endif |
| 702 | |
Hugh Dickins | ee498ed | 2005-11-21 21:32:18 -0800 | [diff] [blame] | 703 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | * copy one vm_area from one task to the other. Assumes the page tables |
| 705 | * already present in the new task to be cleared in the whole range |
| 706 | * covered by this vma. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | */ |
| 708 | |
Linus Torvalds | df3a57d | 2020-09-23 09:56:59 -0700 | [diff] [blame] | 709 | static unsigned long |
| 710 | copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, |
| 711 | pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, |
| 712 | unsigned long addr, int *rss) |
| 713 | { |
| 714 | unsigned long vm_flags = vma->vm_flags; |
| 715 | pte_t pte = *src_pte; |
| 716 | struct page *page; |
| 717 | swp_entry_t entry = pte_to_swp_entry(pte); |
| 718 | |
| 719 | if (likely(!non_swap_entry(entry))) { |
| 720 | if (swap_duplicate(entry) < 0) |
| 721 | return entry.val; |
| 722 | |
| 723 | /* make sure dst_mm is on swapoff's mmlist. */ |
| 724 | if (unlikely(list_empty(&dst_mm->mmlist))) { |
| 725 | spin_lock(&mmlist_lock); |
| 726 | if (list_empty(&dst_mm->mmlist)) |
| 727 | list_add(&dst_mm->mmlist, |
| 728 | &src_mm->mmlist); |
| 729 | spin_unlock(&mmlist_lock); |
| 730 | } |
| 731 | rss[MM_SWAPENTS]++; |
| 732 | } else if (is_migration_entry(entry)) { |
| 733 | page = migration_entry_to_page(entry); |
| 734 | |
| 735 | rss[mm_counter(page)]++; |
| 736 | |
| 737 | if (is_write_migration_entry(entry) && |
| 738 | is_cow_mapping(vm_flags)) { |
| 739 | /* |
| 740 | * COW mappings require pages in both |
| 741 | * parent and child to be set to read. |
| 742 | */ |
| 743 | make_migration_entry_read(&entry); |
| 744 | pte = swp_entry_to_pte(entry); |
| 745 | if (pte_swp_soft_dirty(*src_pte)) |
| 746 | pte = pte_swp_mksoft_dirty(pte); |
| 747 | if (pte_swp_uffd_wp(*src_pte)) |
| 748 | pte = pte_swp_mkuffd_wp(pte); |
| 749 | set_pte_at(src_mm, addr, src_pte, pte); |
| 750 | } |
| 751 | } else if (is_device_private_entry(entry)) { |
| 752 | page = device_private_entry_to_page(entry); |
| 753 | |
| 754 | /* |
| 755 | * Update rss count even for unaddressable pages, as |
| 756 | * they should treated just like normal pages in this |
| 757 | * respect. |
| 758 | * |
| 759 | * We will likely want to have some new rss counters |
| 760 | * for unaddressable pages, at some point. But for now |
| 761 | * keep things as they are. |
| 762 | */ |
| 763 | get_page(page); |
| 764 | rss[mm_counter(page)]++; |
| 765 | page_dup_rmap(page, false); |
| 766 | |
| 767 | /* |
| 768 | * We do not preserve soft-dirty information, because so |
| 769 | * far, checkpoint/restore is the only feature that |
| 770 | * requires that. And checkpoint/restore does not work |
| 771 | * when a device driver is involved (you cannot easily |
| 772 | * save and restore device driver state). |
| 773 | */ |
| 774 | if (is_write_device_private_entry(entry) && |
| 775 | is_cow_mapping(vm_flags)) { |
| 776 | make_device_private_entry_read(&entry); |
| 777 | pte = swp_entry_to_pte(entry); |
| 778 | if (pte_swp_uffd_wp(*src_pte)) |
| 779 | pte = pte_swp_mkuffd_wp(pte); |
| 780 | set_pte_at(src_mm, addr, src_pte, pte); |
| 781 | } |
| 782 | } |
| 783 | set_pte_at(dst_mm, addr, dst_pte, pte); |
| 784 | return 0; |
| 785 | } |
| 786 | |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 787 | /* |
| 788 | * Copy a present and normal page if necessary. |
| 789 | * |
| 790 | * NOTE! The usual case is that this doesn't need to do |
| 791 | * anything, and can just return a positive value. That |
| 792 | * will let the caller know that it can just increase |
| 793 | * the page refcount and re-use the pte the traditional |
| 794 | * way. |
| 795 | * |
| 796 | * But _if_ we need to copy it because it needs to be |
| 797 | * pinned in the parent (and the child should get its own |
| 798 | * copy rather than just a reference to the same page), |
| 799 | * we'll do that here and return zero to let the caller |
| 800 | * know we're done. |
| 801 | * |
| 802 | * And if we need a pre-allocated page but don't yet have |
| 803 | * one, return a negative error to let the preallocation |
| 804 | * code know so that it can do so outside the page table |
| 805 | * lock. |
| 806 | */ |
| 807 | static inline int |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 808 | copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, |
| 809 | pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss, |
| 810 | struct page **prealloc, pte_t pte, struct page *page) |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 811 | { |
| 812 | struct page *new_page; |
| 813 | |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 814 | /* |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 815 | * What we want to do is to check whether this page may |
| 816 | * have been pinned by the parent process. If so, |
| 817 | * instead of wrprotect the pte on both sides, we copy |
| 818 | * the page immediately so that we'll always guarantee |
| 819 | * the pinned page won't be randomly replaced in the |
| 820 | * future. |
| 821 | * |
Linus Torvalds | f3c64ed | 2020-09-28 12:50:03 -0700 | [diff] [blame] | 822 | * The page pinning checks are just "has this mm ever |
| 823 | * seen pinning", along with the (inexact) check of |
| 824 | * the page count. That might give false positives for |
| 825 | * for pinning, but it will work correctly. |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 826 | */ |
Peter Xu | 97a7e47 | 2021-03-12 21:07:26 -0800 | [diff] [blame] | 827 | if (likely(!page_needs_cow_for_dma(src_vma, page))) |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 828 | return 1; |
| 829 | |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 830 | new_page = *prealloc; |
| 831 | if (!new_page) |
| 832 | return -EAGAIN; |
| 833 | |
| 834 | /* |
| 835 | * We have a prealloc page, all good! Take it |
| 836 | * over and copy the page & arm it. |
| 837 | */ |
| 838 | *prealloc = NULL; |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 839 | copy_user_highpage(new_page, page, addr, src_vma); |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 840 | __SetPageUptodate(new_page); |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 841 | page_add_new_anon_rmap(new_page, dst_vma, addr, false); |
| 842 | lru_cache_add_inactive_or_unevictable(new_page, dst_vma); |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 843 | rss[mm_counter(new_page)]++; |
| 844 | |
| 845 | /* All done, just insert the new page copy in the child */ |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 846 | pte = mk_pte(new_page, dst_vma->vm_page_prot); |
| 847 | pte = maybe_mkwrite(pte_mkdirty(pte), dst_vma); |
| 848 | set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte); |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | /* |
| 853 | * Copy one pte. Returns 0 if succeeded, or -EAGAIN if one preallocated page |
| 854 | * is required to copy this pte. |
| 855 | */ |
| 856 | static inline int |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 857 | copy_present_pte(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, |
| 858 | pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss, |
| 859 | struct page **prealloc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | { |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 861 | struct mm_struct *src_mm = src_vma->vm_mm; |
| 862 | unsigned long vm_flags = src_vma->vm_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | pte_t pte = *src_pte; |
| 864 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 866 | page = vm_normal_page(src_vma, addr, pte); |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 867 | if (page) { |
| 868 | int retval; |
| 869 | |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 870 | retval = copy_present_page(dst_vma, src_vma, dst_pte, src_pte, |
| 871 | addr, rss, prealloc, pte, page); |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 872 | if (retval <= 0) |
| 873 | return retval; |
| 874 | |
| 875 | get_page(page); |
| 876 | page_dup_rmap(page, false); |
| 877 | rss[mm_counter(page)]++; |
| 878 | } |
| 879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | /* |
| 881 | * If it's a COW mapping, write protect it both |
| 882 | * in the parent and the child |
| 883 | */ |
Linus Torvalds | 1b2de5d | 2018-07-09 13:19:49 -0700 | [diff] [blame] | 884 | if (is_cow_mapping(vm_flags) && pte_write(pte)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | ptep_set_wrprotect(src_mm, addr, src_pte); |
Zachary Amsden | 3dc9079 | 2006-09-30 23:29:30 -0700 | [diff] [blame] | 886 | pte = pte_wrprotect(pte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | /* |
| 890 | * If it's a shared mapping, mark it clean in |
| 891 | * the child |
| 892 | */ |
| 893 | if (vm_flags & VM_SHARED) |
| 894 | pte = pte_mkclean(pte); |
| 895 | pte = pte_mkold(pte); |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 896 | |
Peter Xu | b569a17 | 2020-04-06 20:05:53 -0700 | [diff] [blame] | 897 | /* |
| 898 | * Make sure the _PAGE_UFFD_WP bit is cleared if the new VMA |
| 899 | * does not have the VM_UFFD_WP, which means that the uffd |
| 900 | * fork event is not enabled. |
| 901 | */ |
| 902 | if (!(vm_flags & VM_UFFD_WP)) |
| 903 | pte = pte_clear_uffd_wp(pte); |
| 904 | |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 905 | set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte); |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 906 | return 0; |
| 907 | } |
| 908 | |
| 909 | static inline struct page * |
| 910 | page_copy_prealloc(struct mm_struct *src_mm, struct vm_area_struct *vma, |
| 911 | unsigned long addr) |
| 912 | { |
| 913 | struct page *new_page; |
| 914 | |
| 915 | new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, addr); |
| 916 | if (!new_page) |
| 917 | return NULL; |
| 918 | |
| 919 | if (mem_cgroup_charge(new_page, src_mm, GFP_KERNEL)) { |
| 920 | put_page(new_page); |
| 921 | return NULL; |
| 922 | } |
| 923 | cgroup_throttle_swaprate(new_page, GFP_KERNEL); |
| 924 | |
| 925 | return new_page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 928 | static int |
| 929 | copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, |
| 930 | pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr, |
| 931 | unsigned long end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | { |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 933 | struct mm_struct *dst_mm = dst_vma->vm_mm; |
| 934 | struct mm_struct *src_mm = src_vma->vm_mm; |
Daisuke Nishimura | c36987e | 2009-10-26 16:50:23 -0700 | [diff] [blame] | 935 | pte_t *orig_src_pte, *orig_dst_pte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | pte_t *src_pte, *dst_pte; |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 937 | spinlock_t *src_ptl, *dst_ptl; |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 938 | int progress, ret = 0; |
KAMEZAWA Hiroyuki | d559db0 | 2010-03-05 13:41:39 -0800 | [diff] [blame] | 939 | int rss[NR_MM_COUNTERS]; |
Hugh Dickins | 570a335b | 2009-12-14 17:58:46 -0800 | [diff] [blame] | 940 | swp_entry_t entry = (swp_entry_t){0}; |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 941 | struct page *prealloc = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
| 943 | again: |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 944 | progress = 0; |
KAMEZAWA Hiroyuki | d559db0 | 2010-03-05 13:41:39 -0800 | [diff] [blame] | 945 | init_rss_vec(rss); |
| 946 | |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 947 | dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 948 | if (!dst_pte) { |
| 949 | ret = -ENOMEM; |
| 950 | goto out; |
| 951 | } |
Peter Zijlstra | ece0e2b | 2010-10-26 14:21:52 -0700 | [diff] [blame] | 952 | src_pte = pte_offset_map(src_pmd, addr); |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 953 | src_ptl = pte_lockptr(src_mm, src_pmd); |
Ingo Molnar | f20dc5f | 2006-07-03 00:25:08 -0700 | [diff] [blame] | 954 | spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); |
Daisuke Nishimura | c36987e | 2009-10-26 16:50:23 -0700 | [diff] [blame] | 955 | orig_src_pte = src_pte; |
| 956 | orig_dst_pte = dst_pte; |
Zachary Amsden | 6606c3e | 2006-09-30 23:29:33 -0700 | [diff] [blame] | 957 | arch_enter_lazy_mmu_mode(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | do { |
| 960 | /* |
| 961 | * We are holding two locks at this point - either of them |
| 962 | * could generate latencies in another task on another CPU. |
| 963 | */ |
Hugh Dickins | e040f21 | 2005-10-29 18:15:53 -0700 | [diff] [blame] | 964 | if (progress >= 32) { |
| 965 | progress = 0; |
| 966 | if (need_resched() || |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 967 | spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) |
Hugh Dickins | e040f21 | 2005-10-29 18:15:53 -0700 | [diff] [blame] | 968 | break; |
| 969 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | if (pte_none(*src_pte)) { |
| 971 | progress++; |
| 972 | continue; |
| 973 | } |
Linus Torvalds | 79a1971 | 2020-09-23 10:04:16 -0700 | [diff] [blame] | 974 | if (unlikely(!pte_present(*src_pte))) { |
| 975 | entry.val = copy_nonpresent_pte(dst_mm, src_mm, |
| 976 | dst_pte, src_pte, |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 977 | src_vma, addr, rss); |
Linus Torvalds | 79a1971 | 2020-09-23 10:04:16 -0700 | [diff] [blame] | 978 | if (entry.val) |
| 979 | break; |
| 980 | progress += 8; |
| 981 | continue; |
| 982 | } |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 983 | /* copy_present_pte() will clear `*prealloc' if consumed */ |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 984 | ret = copy_present_pte(dst_vma, src_vma, dst_pte, src_pte, |
| 985 | addr, rss, &prealloc); |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 986 | /* |
| 987 | * If we need a pre-allocated page for this pte, drop the |
| 988 | * locks, allocate, and try again. |
| 989 | */ |
| 990 | if (unlikely(ret == -EAGAIN)) |
| 991 | break; |
| 992 | if (unlikely(prealloc)) { |
| 993 | /* |
| 994 | * pre-alloc page cannot be reused by next time so as |
| 995 | * to strictly follow mempolicy (e.g., alloc_page_vma() |
| 996 | * will allocate page according to address). This |
| 997 | * could only happen if one pinned pte changed. |
| 998 | */ |
| 999 | put_page(prealloc); |
| 1000 | prealloc = NULL; |
| 1001 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | progress += 8; |
| 1003 | } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
Zachary Amsden | 6606c3e | 2006-09-30 23:29:33 -0700 | [diff] [blame] | 1005 | arch_leave_lazy_mmu_mode(); |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 1006 | spin_unlock(src_ptl); |
Peter Zijlstra | ece0e2b | 2010-10-26 14:21:52 -0700 | [diff] [blame] | 1007 | pte_unmap(orig_src_pte); |
KAMEZAWA Hiroyuki | d559db0 | 2010-03-05 13:41:39 -0800 | [diff] [blame] | 1008 | add_mm_rss_vec(dst_mm, rss); |
Daisuke Nishimura | c36987e | 2009-10-26 16:50:23 -0700 | [diff] [blame] | 1009 | pte_unmap_unlock(orig_dst_pte, dst_ptl); |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 1010 | cond_resched(); |
Hugh Dickins | 570a335b | 2009-12-14 17:58:46 -0800 | [diff] [blame] | 1011 | |
| 1012 | if (entry.val) { |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 1013 | if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) { |
| 1014 | ret = -ENOMEM; |
| 1015 | goto out; |
| 1016 | } |
| 1017 | entry.val = 0; |
| 1018 | } else if (ret) { |
| 1019 | WARN_ON_ONCE(ret != -EAGAIN); |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1020 | prealloc = page_copy_prealloc(src_mm, src_vma, addr); |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 1021 | if (!prealloc) |
Hugh Dickins | 570a335b | 2009-12-14 17:58:46 -0800 | [diff] [blame] | 1022 | return -ENOMEM; |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 1023 | /* We've captured and resolved the error. Reset, try again. */ |
| 1024 | ret = 0; |
Hugh Dickins | 570a335b | 2009-12-14 17:58:46 -0800 | [diff] [blame] | 1025 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | if (addr != end) |
| 1027 | goto again; |
Peter Xu | 70e806e | 2020-09-25 18:25:59 -0400 | [diff] [blame] | 1028 | out: |
| 1029 | if (unlikely(prealloc)) |
| 1030 | put_page(prealloc); |
| 1031 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1034 | static inline int |
| 1035 | copy_pmd_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, |
| 1036 | pud_t *dst_pud, pud_t *src_pud, unsigned long addr, |
| 1037 | unsigned long end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | { |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1039 | struct mm_struct *dst_mm = dst_vma->vm_mm; |
| 1040 | struct mm_struct *src_mm = src_vma->vm_mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | pmd_t *src_pmd, *dst_pmd; |
| 1042 | unsigned long next; |
| 1043 | |
| 1044 | dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); |
| 1045 | if (!dst_pmd) |
| 1046 | return -ENOMEM; |
| 1047 | src_pmd = pmd_offset(src_pud, addr); |
| 1048 | do { |
| 1049 | next = pmd_addr_end(addr, end); |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 1050 | if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd) |
| 1051 | || pmd_devmap(*src_pmd)) { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1052 | int err; |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1053 | VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, src_vma); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1054 | err = copy_huge_pmd(dst_mm, src_mm, |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1055 | dst_pmd, src_pmd, addr, src_vma); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1056 | if (err == -ENOMEM) |
| 1057 | return -ENOMEM; |
| 1058 | if (!err) |
| 1059 | continue; |
| 1060 | /* fall through */ |
| 1061 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | if (pmd_none_or_clear_bad(src_pmd)) |
| 1063 | continue; |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1064 | if (copy_pte_range(dst_vma, src_vma, dst_pmd, src_pmd, |
| 1065 | addr, next)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | return -ENOMEM; |
| 1067 | } while (dst_pmd++, src_pmd++, addr = next, addr != end); |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1071 | static inline int |
| 1072 | copy_pud_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, |
| 1073 | p4d_t *dst_p4d, p4d_t *src_p4d, unsigned long addr, |
| 1074 | unsigned long end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | { |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1076 | struct mm_struct *dst_mm = dst_vma->vm_mm; |
| 1077 | struct mm_struct *src_mm = src_vma->vm_mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | pud_t *src_pud, *dst_pud; |
| 1079 | unsigned long next; |
| 1080 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 1081 | dst_pud = pud_alloc(dst_mm, dst_p4d, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | if (!dst_pud) |
| 1083 | return -ENOMEM; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 1084 | src_pud = pud_offset(src_p4d, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | do { |
| 1086 | next = pud_addr_end(addr, end); |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 1087 | if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) { |
| 1088 | int err; |
| 1089 | |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1090 | VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, src_vma); |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 1091 | err = copy_huge_pud(dst_mm, src_mm, |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1092 | dst_pud, src_pud, addr, src_vma); |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 1093 | if (err == -ENOMEM) |
| 1094 | return -ENOMEM; |
| 1095 | if (!err) |
| 1096 | continue; |
| 1097 | /* fall through */ |
| 1098 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | if (pud_none_or_clear_bad(src_pud)) |
| 1100 | continue; |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1101 | if (copy_pmd_range(dst_vma, src_vma, dst_pud, src_pud, |
| 1102 | addr, next)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | return -ENOMEM; |
| 1104 | } while (dst_pud++, src_pud++, addr = next, addr != end); |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1108 | static inline int |
| 1109 | copy_p4d_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, |
| 1110 | pgd_t *dst_pgd, pgd_t *src_pgd, unsigned long addr, |
| 1111 | unsigned long end) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 1112 | { |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1113 | struct mm_struct *dst_mm = dst_vma->vm_mm; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 1114 | p4d_t *src_p4d, *dst_p4d; |
| 1115 | unsigned long next; |
| 1116 | |
| 1117 | dst_p4d = p4d_alloc(dst_mm, dst_pgd, addr); |
| 1118 | if (!dst_p4d) |
| 1119 | return -ENOMEM; |
| 1120 | src_p4d = p4d_offset(src_pgd, addr); |
| 1121 | do { |
| 1122 | next = p4d_addr_end(addr, end); |
| 1123 | if (p4d_none_or_clear_bad(src_p4d)) |
| 1124 | continue; |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1125 | if (copy_pud_range(dst_vma, src_vma, dst_p4d, src_p4d, |
| 1126 | addr, next)) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 1127 | return -ENOMEM; |
| 1128 | } while (dst_p4d++, src_p4d++, addr = next, addr != end); |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1132 | int |
| 1133 | copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | { |
| 1135 | pgd_t *src_pgd, *dst_pgd; |
| 1136 | unsigned long next; |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1137 | unsigned long addr = src_vma->vm_start; |
| 1138 | unsigned long end = src_vma->vm_end; |
| 1139 | struct mm_struct *dst_mm = dst_vma->vm_mm; |
| 1140 | struct mm_struct *src_mm = src_vma->vm_mm; |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1141 | struct mmu_notifier_range range; |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1142 | bool is_cow; |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 1143 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | |
Nick Piggin | d992895 | 2005-08-28 16:49:11 +1000 | [diff] [blame] | 1145 | /* |
| 1146 | * Don't copy ptes where a page fault will fill them correctly. |
| 1147 | * Fork becomes much lighter when there are big shared or private |
| 1148 | * readonly mappings. The tradeoff is that copy_page_range is more |
| 1149 | * efficient than faulting. |
| 1150 | */ |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1151 | if (!(src_vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) && |
| 1152 | !src_vma->anon_vma) |
Kirill A. Shutemov | 0661a33 | 2015-02-10 14:10:04 -0800 | [diff] [blame] | 1153 | return 0; |
Nick Piggin | d992895 | 2005-08-28 16:49:11 +1000 | [diff] [blame] | 1154 | |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1155 | if (is_vm_hugetlb_page(src_vma)) |
| 1156 | return copy_hugetlb_page_range(dst_mm, src_mm, src_vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1158 | if (unlikely(src_vma->vm_flags & VM_PFNMAP)) { |
venkatesh.pallipadi@intel.com | 2ab6403 | 2008-12-18 11:41:29 -0800 | [diff] [blame] | 1159 | /* |
| 1160 | * We do not free on error cases below as remove_vma |
| 1161 | * gets called on error from higher level routine |
| 1162 | */ |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1163 | ret = track_pfn_copy(src_vma); |
venkatesh.pallipadi@intel.com | 2ab6403 | 2008-12-18 11:41:29 -0800 | [diff] [blame] | 1164 | if (ret) |
| 1165 | return ret; |
| 1166 | } |
| 1167 | |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 1168 | /* |
| 1169 | * We need to invalidate the secondary MMU mappings only when |
| 1170 | * there could be a permission downgrade on the ptes of the |
| 1171 | * parent mm. And a permission downgrade will only happen if |
| 1172 | * is_cow_mapping() returns true. |
| 1173 | */ |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1174 | is_cow = is_cow_mapping(src_vma->vm_flags); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1175 | |
| 1176 | if (is_cow) { |
Jérôme Glisse | 7269f99 | 2019-05-13 17:20:53 -0700 | [diff] [blame] | 1177 | mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE, |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1178 | 0, src_vma, src_mm, addr, end); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1179 | mmu_notifier_invalidate_range_start(&range); |
Jason Gunthorpe | 57efa1f | 2020-12-14 19:05:44 -0800 | [diff] [blame] | 1180 | /* |
| 1181 | * Disabling preemption is not needed for the write side, as |
| 1182 | * the read side doesn't spin, but goes to the mmap_lock. |
| 1183 | * |
| 1184 | * Use the raw variant of the seqcount_t write API to avoid |
| 1185 | * lockdep complaining about preemptibility. |
| 1186 | */ |
| 1187 | mmap_assert_write_locked(src_mm); |
| 1188 | raw_write_seqcount_begin(&src_mm->write_protect_seq); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1189 | } |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 1190 | |
| 1191 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | dst_pgd = pgd_offset(dst_mm, addr); |
| 1193 | src_pgd = pgd_offset(src_mm, addr); |
| 1194 | do { |
| 1195 | next = pgd_addr_end(addr, end); |
| 1196 | if (pgd_none_or_clear_bad(src_pgd)) |
| 1197 | continue; |
Peter Xu | c78f463 | 2020-10-13 16:54:21 -0700 | [diff] [blame] | 1198 | if (unlikely(copy_p4d_range(dst_vma, src_vma, dst_pgd, src_pgd, |
| 1199 | addr, next))) { |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 1200 | ret = -ENOMEM; |
| 1201 | break; |
| 1202 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | } while (dst_pgd++, src_pgd++, addr = next, addr != end); |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 1204 | |
Jason Gunthorpe | 57efa1f | 2020-12-14 19:05:44 -0800 | [diff] [blame] | 1205 | if (is_cow) { |
| 1206 | raw_write_seqcount_end(&src_mm->write_protect_seq); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1207 | mmu_notifier_invalidate_range_end(&range); |
Jason Gunthorpe | 57efa1f | 2020-12-14 19:05:44 -0800 | [diff] [blame] | 1208 | } |
Andrea Arcangeli | cddb8a5 | 2008-07-28 15:46:29 -0700 | [diff] [blame] | 1209 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
Robin Holt | 51c6f66 | 2005-11-13 16:06:42 -0800 | [diff] [blame] | 1212 | static unsigned long zap_pte_range(struct mmu_gather *tlb, |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 1213 | struct vm_area_struct *vma, pmd_t *pmd, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | unsigned long addr, unsigned long end, |
Peter Zijlstra | 97a8941 | 2011-05-24 17:12:04 -0700 | [diff] [blame] | 1215 | struct zap_details *details) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | { |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 1217 | struct mm_struct *mm = tlb->mm; |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame] | 1218 | int force_flush = 0; |
KAMEZAWA Hiroyuki | d559db0 | 2010-03-05 13:41:39 -0800 | [diff] [blame] | 1219 | int rss[NR_MM_COUNTERS]; |
Peter Zijlstra | 97a8941 | 2011-05-24 17:12:04 -0700 | [diff] [blame] | 1220 | spinlock_t *ptl; |
Steven Rostedt | 5f1a190 | 2011-06-15 15:08:23 -0700 | [diff] [blame] | 1221 | pte_t *start_pte; |
Peter Zijlstra | 97a8941 | 2011-05-24 17:12:04 -0700 | [diff] [blame] | 1222 | pte_t *pte; |
Kirill A. Shutemov | 8a5f14a | 2015-02-10 14:09:49 -0800 | [diff] [blame] | 1223 | swp_entry_t entry; |
KAMEZAWA Hiroyuki | d559db0 | 2010-03-05 13:41:39 -0800 | [diff] [blame] | 1224 | |
Peter Zijlstra | ed6a793 | 2018-08-31 14:46:08 +0200 | [diff] [blame] | 1225 | tlb_change_page_size(tlb, PAGE_SIZE); |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame] | 1226 | again: |
Peter Zijlstra | e303297 | 2011-05-24 17:12:01 -0700 | [diff] [blame] | 1227 | init_rss_vec(rss); |
Steven Rostedt | 5f1a190 | 2011-06-15 15:08:23 -0700 | [diff] [blame] | 1228 | start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl); |
| 1229 | pte = start_pte; |
Mel Gorman | 3ea2771 | 2017-08-02 13:31:52 -0700 | [diff] [blame] | 1230 | flush_tlb_batched_pending(mm); |
Zachary Amsden | 6606c3e | 2006-09-30 23:29:33 -0700 | [diff] [blame] | 1231 | arch_enter_lazy_mmu_mode(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | do { |
| 1233 | pte_t ptent = *pte; |
Tobin C Harding | 166f61b | 2017-02-24 14:59:01 -0800 | [diff] [blame] | 1234 | if (pte_none(ptent)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | continue; |
Hugh Dickins | 6f5e6b9 | 2006-03-16 23:04:09 -0800 | [diff] [blame] | 1236 | |
Minchan Kim | 7b167b6 | 2019-09-24 00:02:24 +0000 | [diff] [blame] | 1237 | if (need_resched()) |
| 1238 | break; |
| 1239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | if (pte_present(ptent)) { |
Hugh Dickins | ee498ed | 2005-11-21 21:32:18 -0800 | [diff] [blame] | 1241 | struct page *page; |
Robin Holt | 51c6f66 | 2005-11-13 16:06:42 -0800 | [diff] [blame] | 1242 | |
Christoph Hellwig | 25b2995 | 2019-06-13 22:50:49 +0200 | [diff] [blame] | 1243 | page = vm_normal_page(vma, addr, ptent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | if (unlikely(details) && page) { |
| 1245 | /* |
| 1246 | * unmap_shared_mapping_pages() wants to |
| 1247 | * invalidate cache without truncating: |
| 1248 | * unmap shared but keep private pages. |
| 1249 | */ |
| 1250 | if (details->check_mapping && |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1251 | details->check_mapping != page_rmapping(page)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | } |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 1254 | ptent = ptep_get_and_clear_full(mm, addr, pte, |
Zachary Amsden | a600388 | 2005-09-03 15:55:04 -0700 | [diff] [blame] | 1255 | tlb->fullmm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | tlb_remove_tlb_entry(tlb, pte, addr); |
| 1257 | if (unlikely(!page)) |
| 1258 | continue; |
Jerome Marchand | eca56ff | 2016-01-14 15:19:26 -0800 | [diff] [blame] | 1259 | |
| 1260 | if (!PageAnon(page)) { |
Linus Torvalds | 1cf35d4 | 2014-04-25 16:05:40 -0700 | [diff] [blame] | 1261 | if (pte_dirty(ptent)) { |
| 1262 | force_flush = 1; |
Hugh Dickins | 6237bcd | 2005-10-29 18:15:54 -0700 | [diff] [blame] | 1263 | set_page_dirty(page); |
Linus Torvalds | 1cf35d4 | 2014-04-25 16:05:40 -0700 | [diff] [blame] | 1264 | } |
Johannes Weiner | 4917e5d | 2009-01-06 14:39:17 -0800 | [diff] [blame] | 1265 | if (pte_young(ptent) && |
Joe Perches | 64363aa | 2013-07-08 16:00:18 -0700 | [diff] [blame] | 1266 | likely(!(vma->vm_flags & VM_SEQ_READ))) |
Nick Piggin | bf3f3bc | 2009-01-06 14:38:55 -0800 | [diff] [blame] | 1267 | mark_page_accessed(page); |
Hugh Dickins | 6237bcd | 2005-10-29 18:15:54 -0700 | [diff] [blame] | 1268 | } |
Jerome Marchand | eca56ff | 2016-01-14 15:19:26 -0800 | [diff] [blame] | 1269 | rss[mm_counter(page)]--; |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 1270 | page_remove_rmap(page, false); |
Hugh Dickins | 3dc1474 | 2009-01-06 14:40:08 -0800 | [diff] [blame] | 1271 | if (unlikely(page_mapcount(page) < 0)) |
| 1272 | print_bad_pte(vma, addr, ptent, page); |
Aneesh Kumar K.V | e9d55e1 | 2016-07-26 15:24:09 -0700 | [diff] [blame] | 1273 | if (unlikely(__tlb_remove_page(tlb, page))) { |
Linus Torvalds | 1cf35d4 | 2014-04-25 16:05:40 -0700 | [diff] [blame] | 1274 | force_flush = 1; |
Will Deacon | ce9ec37 | 2014-10-28 13:16:28 -0700 | [diff] [blame] | 1275 | addr += PAGE_SIZE; |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame] | 1276 | break; |
Linus Torvalds | 1cf35d4 | 2014-04-25 16:05:40 -0700 | [diff] [blame] | 1277 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | continue; |
| 1279 | } |
Jérôme Glisse | 5042db4 | 2017-09-08 16:11:43 -0700 | [diff] [blame] | 1280 | |
| 1281 | entry = pte_to_swp_entry(ptent); |
Ralph Campbell | 463b7a1 | 2020-08-06 23:22:21 -0700 | [diff] [blame] | 1282 | if (is_device_private_entry(entry)) { |
Jérôme Glisse | 5042db4 | 2017-09-08 16:11:43 -0700 | [diff] [blame] | 1283 | struct page *page = device_private_entry_to_page(entry); |
| 1284 | |
| 1285 | if (unlikely(details && details->check_mapping)) { |
| 1286 | /* |
| 1287 | * unmap_shared_mapping_pages() wants to |
| 1288 | * invalidate cache without truncating: |
| 1289 | * unmap shared but keep private pages. |
| 1290 | */ |
| 1291 | if (details->check_mapping != |
| 1292 | page_rmapping(page)) |
| 1293 | continue; |
| 1294 | } |
| 1295 | |
| 1296 | pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); |
| 1297 | rss[mm_counter(page)]--; |
| 1298 | page_remove_rmap(page, false); |
| 1299 | put_page(page); |
| 1300 | continue; |
| 1301 | } |
| 1302 | |
Kirill A. Shutemov | 3e8715f | 2017-02-22 15:46:34 -0800 | [diff] [blame] | 1303 | /* If details->check_mapping, we leave swap entries. */ |
| 1304 | if (unlikely(details)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | continue; |
KAMEZAWA Hiroyuki | b084d43 | 2010-03-05 13:41:42 -0800 | [diff] [blame] | 1306 | |
Kirill A. Shutemov | 8a5f14a | 2015-02-10 14:09:49 -0800 | [diff] [blame] | 1307 | if (!non_swap_entry(entry)) |
| 1308 | rss[MM_SWAPENTS]--; |
| 1309 | else if (is_migration_entry(entry)) { |
| 1310 | struct page *page; |
Konstantin Khlebnikov | 9f9f1ac | 2012-01-20 14:34:24 -0800 | [diff] [blame] | 1311 | |
Kirill A. Shutemov | 8a5f14a | 2015-02-10 14:09:49 -0800 | [diff] [blame] | 1312 | page = migration_entry_to_page(entry); |
Jerome Marchand | eca56ff | 2016-01-14 15:19:26 -0800 | [diff] [blame] | 1313 | rss[mm_counter(page)]--; |
KAMEZAWA Hiroyuki | b084d43 | 2010-03-05 13:41:42 -0800 | [diff] [blame] | 1314 | } |
Kirill A. Shutemov | 8a5f14a | 2015-02-10 14:09:49 -0800 | [diff] [blame] | 1315 | if (unlikely(!free_swap_and_cache(entry))) |
| 1316 | print_bad_pte(vma, addr, ptent, NULL); |
Zachary Amsden | 9888a1c | 2006-09-30 23:29:31 -0700 | [diff] [blame] | 1317 | pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); |
Peter Zijlstra | 97a8941 | 2011-05-24 17:12:04 -0700 | [diff] [blame] | 1318 | } while (pte++, addr += PAGE_SIZE, addr != end); |
Hugh Dickins | ae85976 | 2005-10-29 18:16:05 -0700 | [diff] [blame] | 1319 | |
KAMEZAWA Hiroyuki | d559db0 | 2010-03-05 13:41:39 -0800 | [diff] [blame] | 1320 | add_mm_rss_vec(mm, rss); |
Zachary Amsden | 6606c3e | 2006-09-30 23:29:33 -0700 | [diff] [blame] | 1321 | arch_leave_lazy_mmu_mode(); |
Robin Holt | 51c6f66 | 2005-11-13 16:06:42 -0800 | [diff] [blame] | 1322 | |
Linus Torvalds | 1cf35d4 | 2014-04-25 16:05:40 -0700 | [diff] [blame] | 1323 | /* Do the actual TLB flush before dropping ptl */ |
Will Deacon | fb7332a | 2014-10-29 10:03:09 +0000 | [diff] [blame] | 1324 | if (force_flush) |
Linus Torvalds | 1cf35d4 | 2014-04-25 16:05:40 -0700 | [diff] [blame] | 1325 | tlb_flush_mmu_tlbonly(tlb); |
Linus Torvalds | 1cf35d4 | 2014-04-25 16:05:40 -0700 | [diff] [blame] | 1326 | pte_unmap_unlock(start_pte, ptl); |
| 1327 | |
| 1328 | /* |
| 1329 | * If we forced a TLB flush (either due to running out of |
| 1330 | * batch buffers or because we needed to flush dirty TLB |
| 1331 | * entries before releasing the ptl), free the batched |
| 1332 | * memory too. Restart if we didn't do everything. |
| 1333 | */ |
| 1334 | if (force_flush) { |
| 1335 | force_flush = 0; |
Peter Zijlstra | fa0aafb | 2018-09-20 10:54:04 +0200 | [diff] [blame] | 1336 | tlb_flush_mmu(tlb); |
Minchan Kim | 7b167b6 | 2019-09-24 00:02:24 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | if (addr != end) { |
| 1340 | cond_resched(); |
| 1341 | goto again; |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame] | 1342 | } |
| 1343 | |
Robin Holt | 51c6f66 | 2005-11-13 16:06:42 -0800 | [diff] [blame] | 1344 | return addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
Robin Holt | 51c6f66 | 2005-11-13 16:06:42 -0800 | [diff] [blame] | 1347 | static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 1348 | struct vm_area_struct *vma, pud_t *pud, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | unsigned long addr, unsigned long end, |
Peter Zijlstra | 97a8941 | 2011-05-24 17:12:04 -0700 | [diff] [blame] | 1350 | struct zap_details *details) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | { |
| 1352 | pmd_t *pmd; |
| 1353 | unsigned long next; |
| 1354 | |
| 1355 | pmd = pmd_offset(pud, addr); |
| 1356 | do { |
| 1357 | next = pmd_addr_end(addr, end); |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 1358 | if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) { |
Hugh Dickins | 53406ed | 2018-08-01 11:31:52 -0700 | [diff] [blame] | 1359 | if (next - addr != HPAGE_PMD_SIZE) |
David Rientjes | fd60775 | 2016-12-12 16:42:20 -0800 | [diff] [blame] | 1360 | __split_huge_pmd(vma, pmd, addr, false, NULL); |
Hugh Dickins | 53406ed | 2018-08-01 11:31:52 -0700 | [diff] [blame] | 1361 | else if (zap_huge_pmd(tlb, vma, pmd, addr)) |
Andrea Arcangeli | 1a5a990 | 2012-03-21 16:33:42 -0700 | [diff] [blame] | 1362 | goto next; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1363 | /* fall through */ |
| 1364 | } |
Andrea Arcangeli | 1a5a990 | 2012-03-21 16:33:42 -0700 | [diff] [blame] | 1365 | /* |
| 1366 | * Here there can be other concurrent MADV_DONTNEED or |
| 1367 | * trans huge page faults running, and if the pmd is |
| 1368 | * none or trans huge it can change under us. This is |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 1369 | * because MADV_DONTNEED holds the mmap_lock in read |
Andrea Arcangeli | 1a5a990 | 2012-03-21 16:33:42 -0700 | [diff] [blame] | 1370 | * mode. |
| 1371 | */ |
| 1372 | if (pmd_none_or_trans_huge_or_clear_bad(pmd)) |
| 1373 | goto next; |
Peter Zijlstra | 97a8941 | 2011-05-24 17:12:04 -0700 | [diff] [blame] | 1374 | next = zap_pte_range(tlb, vma, pmd, addr, next, details); |
Andrea Arcangeli | 1a5a990 | 2012-03-21 16:33:42 -0700 | [diff] [blame] | 1375 | next: |
Peter Zijlstra | 97a8941 | 2011-05-24 17:12:04 -0700 | [diff] [blame] | 1376 | cond_resched(); |
| 1377 | } while (pmd++, addr = next, addr != end); |
Robin Holt | 51c6f66 | 2005-11-13 16:06:42 -0800 | [diff] [blame] | 1378 | |
| 1379 | return addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
Robin Holt | 51c6f66 | 2005-11-13 16:06:42 -0800 | [diff] [blame] | 1382 | static inline unsigned long zap_pud_range(struct mmu_gather *tlb, |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 1383 | struct vm_area_struct *vma, p4d_t *p4d, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | unsigned long addr, unsigned long end, |
Peter Zijlstra | 97a8941 | 2011-05-24 17:12:04 -0700 | [diff] [blame] | 1385 | struct zap_details *details) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | { |
| 1387 | pud_t *pud; |
| 1388 | unsigned long next; |
| 1389 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 1390 | pud = pud_offset(p4d, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | do { |
| 1392 | next = pud_addr_end(addr, end); |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 1393 | if (pud_trans_huge(*pud) || pud_devmap(*pud)) { |
| 1394 | if (next - addr != HPAGE_PUD_SIZE) { |
Michel Lespinasse | 42fc541 | 2020-06-08 21:33:44 -0700 | [diff] [blame] | 1395 | mmap_assert_locked(tlb->mm); |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 1396 | split_huge_pud(vma, pud, addr); |
| 1397 | } else if (zap_huge_pud(tlb, vma, pud, addr)) |
| 1398 | goto next; |
| 1399 | /* fall through */ |
| 1400 | } |
Peter Zijlstra | 97a8941 | 2011-05-24 17:12:04 -0700 | [diff] [blame] | 1401 | if (pud_none_or_clear_bad(pud)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | continue; |
Peter Zijlstra | 97a8941 | 2011-05-24 17:12:04 -0700 | [diff] [blame] | 1403 | next = zap_pmd_range(tlb, vma, pud, addr, next, details); |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 1404 | next: |
| 1405 | cond_resched(); |
Peter Zijlstra | 97a8941 | 2011-05-24 17:12:04 -0700 | [diff] [blame] | 1406 | } while (pud++, addr = next, addr != end); |
Robin Holt | 51c6f66 | 2005-11-13 16:06:42 -0800 | [diff] [blame] | 1407 | |
| 1408 | return addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | } |
| 1410 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 1411 | static inline unsigned long zap_p4d_range(struct mmu_gather *tlb, |
| 1412 | struct vm_area_struct *vma, pgd_t *pgd, |
| 1413 | unsigned long addr, unsigned long end, |
| 1414 | struct zap_details *details) |
| 1415 | { |
| 1416 | p4d_t *p4d; |
| 1417 | unsigned long next; |
| 1418 | |
| 1419 | p4d = p4d_offset(pgd, addr); |
| 1420 | do { |
| 1421 | next = p4d_addr_end(addr, end); |
| 1422 | if (p4d_none_or_clear_bad(p4d)) |
| 1423 | continue; |
| 1424 | next = zap_pud_range(tlb, vma, p4d, addr, next, details); |
| 1425 | } while (p4d++, addr = next, addr != end); |
| 1426 | |
| 1427 | return addr; |
| 1428 | } |
| 1429 | |
Michal Hocko | aac4536 | 2016-03-25 14:20:24 -0700 | [diff] [blame] | 1430 | void unmap_page_range(struct mmu_gather *tlb, |
Al Viro | 038c7aa | 2012-03-05 13:25:09 -0500 | [diff] [blame] | 1431 | struct vm_area_struct *vma, |
| 1432 | unsigned long addr, unsigned long end, |
| 1433 | struct zap_details *details) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | { |
| 1435 | pgd_t *pgd; |
| 1436 | unsigned long next; |
| 1437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | BUG_ON(addr >= end); |
| 1439 | tlb_start_vma(tlb, vma); |
| 1440 | pgd = pgd_offset(vma->vm_mm, addr); |
| 1441 | do { |
| 1442 | next = pgd_addr_end(addr, end); |
Peter Zijlstra | 97a8941 | 2011-05-24 17:12:04 -0700 | [diff] [blame] | 1443 | if (pgd_none_or_clear_bad(pgd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | continue; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 1445 | next = zap_p4d_range(tlb, vma, pgd, addr, next, details); |
Peter Zijlstra | 97a8941 | 2011-05-24 17:12:04 -0700 | [diff] [blame] | 1446 | } while (pgd++, addr = next, addr != end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | tlb_end_vma(tlb, vma); |
| 1448 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | |
Al Viro | f5cc4ee | 2012-03-05 14:14:20 -0500 | [diff] [blame] | 1450 | |
| 1451 | static void unmap_single_vma(struct mmu_gather *tlb, |
| 1452 | struct vm_area_struct *vma, unsigned long start_addr, |
Linus Torvalds | 4f74d2c | 2012-05-06 13:54:06 -0700 | [diff] [blame] | 1453 | unsigned long end_addr, |
Al Viro | f5cc4ee | 2012-03-05 14:14:20 -0500 | [diff] [blame] | 1454 | struct zap_details *details) |
| 1455 | { |
| 1456 | unsigned long start = max(vma->vm_start, start_addr); |
| 1457 | unsigned long end; |
| 1458 | |
| 1459 | if (start >= vma->vm_end) |
| 1460 | return; |
| 1461 | end = min(vma->vm_end, end_addr); |
| 1462 | if (end <= vma->vm_start) |
| 1463 | return; |
| 1464 | |
Srikar Dronamraju | cbc91f7 | 2012-04-11 16:05:27 +0530 | [diff] [blame] | 1465 | if (vma->vm_file) |
| 1466 | uprobe_munmap(vma, start, end); |
| 1467 | |
Konstantin Khlebnikov | b3b9c29 | 2012-10-08 16:28:34 -0700 | [diff] [blame] | 1468 | if (unlikely(vma->vm_flags & VM_PFNMAP)) |
Suresh Siddha | 5180da4 | 2012-10-08 16:28:29 -0700 | [diff] [blame] | 1469 | untrack_pfn(vma, 0, 0); |
Al Viro | f5cc4ee | 2012-03-05 14:14:20 -0500 | [diff] [blame] | 1470 | |
| 1471 | if (start != end) { |
| 1472 | if (unlikely(is_vm_hugetlb_page(vma))) { |
| 1473 | /* |
| 1474 | * It is undesirable to test vma->vm_file as it |
| 1475 | * should be non-null for valid hugetlb area. |
| 1476 | * However, vm_file will be NULL in the error |
Davidlohr Bueso | 7aa6b4a | 2014-04-07 15:37:01 -0700 | [diff] [blame] | 1477 | * cleanup path of mmap_region. When |
Al Viro | f5cc4ee | 2012-03-05 14:14:20 -0500 | [diff] [blame] | 1478 | * hugetlbfs ->mmap method fails, |
Davidlohr Bueso | 7aa6b4a | 2014-04-07 15:37:01 -0700 | [diff] [blame] | 1479 | * mmap_region() nullifies vma->vm_file |
Al Viro | f5cc4ee | 2012-03-05 14:14:20 -0500 | [diff] [blame] | 1480 | * before calling this function to clean up. |
| 1481 | * Since no pte has actually been setup, it is |
| 1482 | * safe to do nothing in this case. |
| 1483 | */ |
Aneesh Kumar K.V | 24669e5 | 2012-07-31 16:42:03 -0700 | [diff] [blame] | 1484 | if (vma->vm_file) { |
Davidlohr Bueso | 83cde9e | 2014-12-12 16:54:21 -0800 | [diff] [blame] | 1485 | i_mmap_lock_write(vma->vm_file->f_mapping); |
Mel Gorman | d833352 | 2012-07-31 16:46:20 -0700 | [diff] [blame] | 1486 | __unmap_hugepage_range_final(tlb, vma, start, end, NULL); |
Davidlohr Bueso | 83cde9e | 2014-12-12 16:54:21 -0800 | [diff] [blame] | 1487 | i_mmap_unlock_write(vma->vm_file->f_mapping); |
Aneesh Kumar K.V | 24669e5 | 2012-07-31 16:42:03 -0700 | [diff] [blame] | 1488 | } |
Al Viro | f5cc4ee | 2012-03-05 14:14:20 -0500 | [diff] [blame] | 1489 | } else |
| 1490 | unmap_page_range(tlb, vma, start, end, details); |
| 1491 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | /** |
| 1495 | * unmap_vmas - unmap a range of memory covered by a list of vma's |
Randy Dunlap | 0164f69 | 2011-06-15 15:08:09 -0700 | [diff] [blame] | 1496 | * @tlb: address of the caller's struct mmu_gather |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | * @vma: the starting vma |
| 1498 | * @start_addr: virtual address at which to start unmapping |
| 1499 | * @end_addr: virtual address at which to end unmapping |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | * |
Hugh Dickins | 508034a | 2005-10-29 18:16:30 -0700 | [diff] [blame] | 1501 | * Unmap all pages in the vma list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | * Only addresses between `start' and `end' will be unmapped. |
| 1504 | * |
| 1505 | * The VMA list must be sorted in ascending virtual address order. |
| 1506 | * |
| 1507 | * unmap_vmas() assumes that the caller will flush the whole unmapped address |
| 1508 | * range after unmap_vmas() returns. So the only responsibility here is to |
| 1509 | * ensure that any thus-far unmapped pages are flushed before unmap_vmas() |
| 1510 | * drops the lock and schedules. |
| 1511 | */ |
Al Viro | 6e8bb01 | 2012-03-05 13:41:15 -0500 | [diff] [blame] | 1512 | void unmap_vmas(struct mmu_gather *tlb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | struct vm_area_struct *vma, unsigned long start_addr, |
Linus Torvalds | 4f74d2c | 2012-05-06 13:54:06 -0700 | [diff] [blame] | 1514 | unsigned long end_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | { |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1516 | struct mmu_notifier_range range; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | |
Jérôme Glisse | 6f4f13e | 2019-05-13 17:20:49 -0700 | [diff] [blame] | 1518 | mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, vma->vm_mm, |
| 1519 | start_addr, end_addr); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1520 | mmu_notifier_invalidate_range_start(&range); |
Al Viro | f5cc4ee | 2012-03-05 14:14:20 -0500 | [diff] [blame] | 1521 | for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) |
Linus Torvalds | 4f74d2c | 2012-05-06 13:54:06 -0700 | [diff] [blame] | 1522 | unmap_single_vma(tlb, vma, start_addr, end_addr, NULL); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1523 | mmu_notifier_invalidate_range_end(&range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | /** |
| 1527 | * zap_page_range - remove user pages in a given range |
| 1528 | * @vma: vm_area_struct holding the applicable pages |
Randy Dunlap | eb4546b | 2012-06-20 12:53:02 -0700 | [diff] [blame] | 1529 | * @start: starting address of pages to zap |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | * @size: number of bytes to zap |
Al Viro | f5cc4ee | 2012-03-05 14:14:20 -0500 | [diff] [blame] | 1531 | * |
| 1532 | * Caller must protect the VMA list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | */ |
Linus Torvalds | 7e027b1 | 2012-05-06 13:43:15 -0700 | [diff] [blame] | 1534 | void zap_page_range(struct vm_area_struct *vma, unsigned long start, |
Kirill A. Shutemov | ecf1385 | 2017-02-22 15:46:37 -0800 | [diff] [blame] | 1535 | unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | { |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1537 | struct mmu_notifier_range range; |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame] | 1538 | struct mmu_gather tlb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | lru_add_drain(); |
Jérôme Glisse | 7269f99 | 2019-05-13 17:20:53 -0700 | [diff] [blame] | 1541 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm, |
Jérôme Glisse | 6f4f13e | 2019-05-13 17:20:49 -0700 | [diff] [blame] | 1542 | start, start + size); |
Will Deacon | a72afd8 | 2021-01-27 23:53:45 +0000 | [diff] [blame] | 1543 | tlb_gather_mmu(&tlb, vma->vm_mm); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1544 | update_hiwater_rss(vma->vm_mm); |
| 1545 | mmu_notifier_invalidate_range_start(&range); |
| 1546 | for ( ; vma && vma->vm_start < range.end; vma = vma->vm_next) |
| 1547 | unmap_single_vma(&tlb, vma, start, range.end, NULL); |
| 1548 | mmu_notifier_invalidate_range_end(&range); |
Will Deacon | ae8eba8 | 2021-01-27 23:53:43 +0000 | [diff] [blame] | 1549 | tlb_finish_mmu(&tlb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | } |
| 1551 | |
Jack Steiner | c627f9c | 2008-07-29 22:33:53 -0700 | [diff] [blame] | 1552 | /** |
Al Viro | f5cc4ee | 2012-03-05 14:14:20 -0500 | [diff] [blame] | 1553 | * zap_page_range_single - remove user pages in a given range |
| 1554 | * @vma: vm_area_struct holding the applicable pages |
| 1555 | * @address: starting address of pages to zap |
| 1556 | * @size: number of bytes to zap |
Kirill A. Shutemov | 8a5f14a | 2015-02-10 14:09:49 -0800 | [diff] [blame] | 1557 | * @details: details of shared cache invalidation |
Al Viro | f5cc4ee | 2012-03-05 14:14:20 -0500 | [diff] [blame] | 1558 | * |
| 1559 | * The range must fit into one VMA. |
| 1560 | */ |
| 1561 | static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, |
| 1562 | unsigned long size, struct zap_details *details) |
| 1563 | { |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1564 | struct mmu_notifier_range range; |
Al Viro | f5cc4ee | 2012-03-05 14:14:20 -0500 | [diff] [blame] | 1565 | struct mmu_gather tlb; |
Al Viro | f5cc4ee | 2012-03-05 14:14:20 -0500 | [diff] [blame] | 1566 | |
| 1567 | lru_add_drain(); |
Jérôme Glisse | 7269f99 | 2019-05-13 17:20:53 -0700 | [diff] [blame] | 1568 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm, |
Jérôme Glisse | 6f4f13e | 2019-05-13 17:20:49 -0700 | [diff] [blame] | 1569 | address, address + size); |
Will Deacon | a72afd8 | 2021-01-27 23:53:45 +0000 | [diff] [blame] | 1570 | tlb_gather_mmu(&tlb, vma->vm_mm); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1571 | update_hiwater_rss(vma->vm_mm); |
| 1572 | mmu_notifier_invalidate_range_start(&range); |
| 1573 | unmap_single_vma(&tlb, vma, address, range.end, details); |
| 1574 | mmu_notifier_invalidate_range_end(&range); |
Will Deacon | ae8eba8 | 2021-01-27 23:53:43 +0000 | [diff] [blame] | 1575 | tlb_finish_mmu(&tlb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | } |
| 1577 | |
Jack Steiner | c627f9c | 2008-07-29 22:33:53 -0700 | [diff] [blame] | 1578 | /** |
| 1579 | * zap_vma_ptes - remove ptes mapping the vma |
| 1580 | * @vma: vm_area_struct holding ptes to be zapped |
| 1581 | * @address: starting address of pages to zap |
| 1582 | * @size: number of bytes to zap |
| 1583 | * |
| 1584 | * This function only unmaps ptes assigned to VM_PFNMAP vmas. |
| 1585 | * |
| 1586 | * The entire address range must be fully contained within the vma. |
| 1587 | * |
Jack Steiner | c627f9c | 2008-07-29 22:33:53 -0700 | [diff] [blame] | 1588 | */ |
Leon Romanovsky | 27d036e | 2018-05-29 15:14:07 +0300 | [diff] [blame] | 1589 | void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, |
Jack Steiner | c627f9c | 2008-07-29 22:33:53 -0700 | [diff] [blame] | 1590 | unsigned long size) |
| 1591 | { |
| 1592 | if (address < vma->vm_start || address + size > vma->vm_end || |
| 1593 | !(vma->vm_flags & VM_PFNMAP)) |
Leon Romanovsky | 27d036e | 2018-05-29 15:14:07 +0300 | [diff] [blame] | 1594 | return; |
| 1595 | |
Al Viro | f5cc4ee | 2012-03-05 14:14:20 -0500 | [diff] [blame] | 1596 | zap_page_range_single(vma, address, size, NULL); |
Jack Steiner | c627f9c | 2008-07-29 22:33:53 -0700 | [diff] [blame] | 1597 | } |
| 1598 | EXPORT_SYMBOL_GPL(zap_vma_ptes); |
| 1599 | |
Arjun Roy | 8cd3984 | 2020-04-10 14:33:01 -0700 | [diff] [blame] | 1600 | static pmd_t *walk_to_pmd(struct mm_struct *mm, unsigned long addr) |
Linus Torvalds | c9cfcdd | 2005-11-29 14:03:14 -0800 | [diff] [blame] | 1601 | { |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 1602 | pgd_t *pgd; |
| 1603 | p4d_t *p4d; |
| 1604 | pud_t *pud; |
| 1605 | pmd_t *pmd; |
| 1606 | |
| 1607 | pgd = pgd_offset(mm, addr); |
| 1608 | p4d = p4d_alloc(mm, pgd, addr); |
| 1609 | if (!p4d) |
| 1610 | return NULL; |
| 1611 | pud = pud_alloc(mm, p4d, addr); |
| 1612 | if (!pud) |
| 1613 | return NULL; |
| 1614 | pmd = pmd_alloc(mm, pud, addr); |
| 1615 | if (!pmd) |
| 1616 | return NULL; |
| 1617 | |
| 1618 | VM_BUG_ON(pmd_trans_huge(*pmd)); |
Arjun Roy | 8cd3984 | 2020-04-10 14:33:01 -0700 | [diff] [blame] | 1619 | return pmd; |
| 1620 | } |
| 1621 | |
| 1622 | pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, |
| 1623 | spinlock_t **ptl) |
| 1624 | { |
| 1625 | pmd_t *pmd = walk_to_pmd(mm, addr); |
| 1626 | |
| 1627 | if (!pmd) |
| 1628 | return NULL; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 1629 | return pte_alloc_map_lock(mm, pmd, addr, ptl); |
Linus Torvalds | c9cfcdd | 2005-11-29 14:03:14 -0800 | [diff] [blame] | 1630 | } |
| 1631 | |
Arjun Roy | 8efd6f5b | 2020-04-10 14:32:51 -0700 | [diff] [blame] | 1632 | static int validate_page_before_insert(struct page *page) |
| 1633 | { |
| 1634 | if (PageAnon(page) || PageSlab(page) || page_has_type(page)) |
| 1635 | return -EINVAL; |
| 1636 | flush_dcache_page(page); |
| 1637 | return 0; |
| 1638 | } |
| 1639 | |
| 1640 | static int insert_page_into_pte_locked(struct mm_struct *mm, pte_t *pte, |
| 1641 | unsigned long addr, struct page *page, pgprot_t prot) |
| 1642 | { |
| 1643 | if (!pte_none(*pte)) |
| 1644 | return -EBUSY; |
| 1645 | /* Ok, finally just insert the thing.. */ |
| 1646 | get_page(page); |
| 1647 | inc_mm_counter_fast(mm, mm_counter_file(page)); |
| 1648 | page_add_file_rmap(page, false); |
| 1649 | set_pte_at(mm, addr, pte, mk_pte(page, prot)); |
| 1650 | return 0; |
| 1651 | } |
| 1652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | /* |
Linus Torvalds | 238f58d | 2005-11-29 13:01:56 -0800 | [diff] [blame] | 1654 | * This is the old fallback for page remapping. |
| 1655 | * |
| 1656 | * For historical reasons, it only allows reserved pages. Only |
| 1657 | * old drivers should use this, and they needed to mark their |
| 1658 | * pages reserved for the old functions anyway. |
| 1659 | */ |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 1660 | static int insert_page(struct vm_area_struct *vma, unsigned long addr, |
| 1661 | struct page *page, pgprot_t prot) |
Linus Torvalds | 238f58d | 2005-11-29 13:01:56 -0800 | [diff] [blame] | 1662 | { |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 1663 | struct mm_struct *mm = vma->vm_mm; |
Linus Torvalds | 238f58d | 2005-11-29 13:01:56 -0800 | [diff] [blame] | 1664 | int retval; |
Linus Torvalds | c9cfcdd | 2005-11-29 14:03:14 -0800 | [diff] [blame] | 1665 | pte_t *pte; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1666 | spinlock_t *ptl; |
| 1667 | |
Arjun Roy | 8efd6f5b | 2020-04-10 14:32:51 -0700 | [diff] [blame] | 1668 | retval = validate_page_before_insert(page); |
| 1669 | if (retval) |
KAMEZAWA Hiroyuki | 5b4e655 | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 1670 | goto out; |
Linus Torvalds | 238f58d | 2005-11-29 13:01:56 -0800 | [diff] [blame] | 1671 | retval = -ENOMEM; |
Linus Torvalds | c9cfcdd | 2005-11-29 14:03:14 -0800 | [diff] [blame] | 1672 | pte = get_locked_pte(mm, addr, &ptl); |
Linus Torvalds | 238f58d | 2005-11-29 13:01:56 -0800 | [diff] [blame] | 1673 | if (!pte) |
KAMEZAWA Hiroyuki | 5b4e655 | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 1674 | goto out; |
Arjun Roy | 8efd6f5b | 2020-04-10 14:32:51 -0700 | [diff] [blame] | 1675 | retval = insert_page_into_pte_locked(mm, pte, addr, page, prot); |
Linus Torvalds | 238f58d | 2005-11-29 13:01:56 -0800 | [diff] [blame] | 1676 | pte_unmap_unlock(pte, ptl); |
| 1677 | out: |
| 1678 | return retval; |
| 1679 | } |
| 1680 | |
Arjun Roy | 8cd3984 | 2020-04-10 14:33:01 -0700 | [diff] [blame] | 1681 | #ifdef pte_index |
Arjun Roy | 7f70c2a | 2020-06-25 20:30:01 -0700 | [diff] [blame] | 1682 | static int insert_page_in_batch_locked(struct mm_struct *mm, pte_t *pte, |
Arjun Roy | 8cd3984 | 2020-04-10 14:33:01 -0700 | [diff] [blame] | 1683 | unsigned long addr, struct page *page, pgprot_t prot) |
| 1684 | { |
| 1685 | int err; |
| 1686 | |
| 1687 | if (!page_count(page)) |
| 1688 | return -EINVAL; |
| 1689 | err = validate_page_before_insert(page); |
Arjun Roy | 7f70c2a | 2020-06-25 20:30:01 -0700 | [diff] [blame] | 1690 | if (err) |
| 1691 | return err; |
| 1692 | return insert_page_into_pte_locked(mm, pte, addr, page, prot); |
Arjun Roy | 8cd3984 | 2020-04-10 14:33:01 -0700 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | /* insert_pages() amortizes the cost of spinlock operations |
| 1696 | * when inserting pages in a loop. Arch *must* define pte_index. |
| 1697 | */ |
| 1698 | static int insert_pages(struct vm_area_struct *vma, unsigned long addr, |
| 1699 | struct page **pages, unsigned long *num, pgprot_t prot) |
| 1700 | { |
| 1701 | pmd_t *pmd = NULL; |
Arjun Roy | 7f70c2a | 2020-06-25 20:30:01 -0700 | [diff] [blame] | 1702 | pte_t *start_pte, *pte; |
| 1703 | spinlock_t *pte_lock; |
Arjun Roy | 8cd3984 | 2020-04-10 14:33:01 -0700 | [diff] [blame] | 1704 | struct mm_struct *const mm = vma->vm_mm; |
| 1705 | unsigned long curr_page_idx = 0; |
| 1706 | unsigned long remaining_pages_total = *num; |
| 1707 | unsigned long pages_to_write_in_pmd; |
| 1708 | int ret; |
| 1709 | more: |
| 1710 | ret = -EFAULT; |
| 1711 | pmd = walk_to_pmd(mm, addr); |
| 1712 | if (!pmd) |
| 1713 | goto out; |
| 1714 | |
| 1715 | pages_to_write_in_pmd = min_t(unsigned long, |
| 1716 | remaining_pages_total, PTRS_PER_PTE - pte_index(addr)); |
| 1717 | |
| 1718 | /* Allocate the PTE if necessary; takes PMD lock once only. */ |
| 1719 | ret = -ENOMEM; |
| 1720 | if (pte_alloc(mm, pmd)) |
| 1721 | goto out; |
Arjun Roy | 8cd3984 | 2020-04-10 14:33:01 -0700 | [diff] [blame] | 1722 | |
| 1723 | while (pages_to_write_in_pmd) { |
| 1724 | int pte_idx = 0; |
| 1725 | const int batch_size = min_t(int, pages_to_write_in_pmd, 8); |
| 1726 | |
Arjun Roy | 7f70c2a | 2020-06-25 20:30:01 -0700 | [diff] [blame] | 1727 | start_pte = pte_offset_map_lock(mm, pmd, addr, &pte_lock); |
| 1728 | for (pte = start_pte; pte_idx < batch_size; ++pte, ++pte_idx) { |
| 1729 | int err = insert_page_in_batch_locked(mm, pte, |
Arjun Roy | 8cd3984 | 2020-04-10 14:33:01 -0700 | [diff] [blame] | 1730 | addr, pages[curr_page_idx], prot); |
| 1731 | if (unlikely(err)) { |
Arjun Roy | 7f70c2a | 2020-06-25 20:30:01 -0700 | [diff] [blame] | 1732 | pte_unmap_unlock(start_pte, pte_lock); |
Arjun Roy | 8cd3984 | 2020-04-10 14:33:01 -0700 | [diff] [blame] | 1733 | ret = err; |
| 1734 | remaining_pages_total -= pte_idx; |
| 1735 | goto out; |
| 1736 | } |
| 1737 | addr += PAGE_SIZE; |
| 1738 | ++curr_page_idx; |
| 1739 | } |
Arjun Roy | 7f70c2a | 2020-06-25 20:30:01 -0700 | [diff] [blame] | 1740 | pte_unmap_unlock(start_pte, pte_lock); |
Arjun Roy | 8cd3984 | 2020-04-10 14:33:01 -0700 | [diff] [blame] | 1741 | pages_to_write_in_pmd -= batch_size; |
| 1742 | remaining_pages_total -= batch_size; |
| 1743 | } |
| 1744 | if (remaining_pages_total) |
| 1745 | goto more; |
| 1746 | ret = 0; |
| 1747 | out: |
| 1748 | *num = remaining_pages_total; |
| 1749 | return ret; |
| 1750 | } |
| 1751 | #endif /* ifdef pte_index */ |
| 1752 | |
| 1753 | /** |
| 1754 | * vm_insert_pages - insert multiple pages into user vma, batching the pmd lock. |
| 1755 | * @vma: user vma to map to |
| 1756 | * @addr: target start user address of these pages |
| 1757 | * @pages: source kernel pages |
| 1758 | * @num: in: number of pages to map. out: number of pages that were *not* |
| 1759 | * mapped. (0 means all pages were successfully mapped). |
| 1760 | * |
| 1761 | * Preferred over vm_insert_page() when inserting multiple pages. |
| 1762 | * |
| 1763 | * In case of error, we may have mapped a subset of the provided |
| 1764 | * pages. It is the caller's responsibility to account for this case. |
| 1765 | * |
| 1766 | * The same restrictions apply as in vm_insert_page(). |
| 1767 | */ |
| 1768 | int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr, |
| 1769 | struct page **pages, unsigned long *num) |
| 1770 | { |
| 1771 | #ifdef pte_index |
| 1772 | const unsigned long end_addr = addr + (*num * PAGE_SIZE) - 1; |
| 1773 | |
| 1774 | if (addr < vma->vm_start || end_addr >= vma->vm_end) |
| 1775 | return -EFAULT; |
| 1776 | if (!(vma->vm_flags & VM_MIXEDMAP)) { |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1777 | BUG_ON(mmap_read_trylock(vma->vm_mm)); |
Arjun Roy | 8cd3984 | 2020-04-10 14:33:01 -0700 | [diff] [blame] | 1778 | BUG_ON(vma->vm_flags & VM_PFNMAP); |
| 1779 | vma->vm_flags |= VM_MIXEDMAP; |
| 1780 | } |
| 1781 | /* Defer page refcount checking till we're about to map that page. */ |
| 1782 | return insert_pages(vma, addr, pages, num, vma->vm_page_prot); |
| 1783 | #else |
| 1784 | unsigned long idx = 0, pgcount = *num; |
Tom Rix | 45779b0 | 2020-07-23 21:15:18 -0700 | [diff] [blame] | 1785 | int err = -EINVAL; |
Arjun Roy | 8cd3984 | 2020-04-10 14:33:01 -0700 | [diff] [blame] | 1786 | |
| 1787 | for (; idx < pgcount; ++idx) { |
| 1788 | err = vm_insert_page(vma, addr + (PAGE_SIZE * idx), pages[idx]); |
| 1789 | if (err) |
| 1790 | break; |
| 1791 | } |
| 1792 | *num = pgcount - idx; |
| 1793 | return err; |
| 1794 | #endif /* ifdef pte_index */ |
| 1795 | } |
| 1796 | EXPORT_SYMBOL(vm_insert_pages); |
| 1797 | |
Rolf Eike Beer | bfa5bf6 | 2006-09-25 23:31:22 -0700 | [diff] [blame] | 1798 | /** |
| 1799 | * vm_insert_page - insert single page into user vma |
| 1800 | * @vma: user vma to map to |
| 1801 | * @addr: target user address of this page |
| 1802 | * @page: source kernel page |
| 1803 | * |
Linus Torvalds | a145dd4 | 2005-11-30 09:35:19 -0800 | [diff] [blame] | 1804 | * This allows drivers to insert individual pages they've allocated |
| 1805 | * into a user vma. |
| 1806 | * |
| 1807 | * The page has to be a nice clean _individual_ kernel allocation. |
| 1808 | * If you allocate a compound page, you need to have marked it as |
| 1809 | * such (__GFP_COMP), or manually just split the page up yourself |
Nick Piggin | 8dfcc9b | 2006-03-22 00:08:05 -0800 | [diff] [blame] | 1810 | * (see split_page()). |
Linus Torvalds | a145dd4 | 2005-11-30 09:35:19 -0800 | [diff] [blame] | 1811 | * |
| 1812 | * NOTE! Traditionally this was done with "remap_pfn_range()" which |
| 1813 | * took an arbitrary page protection parameter. This doesn't allow |
| 1814 | * that. Your vma protection will have to be set up correctly, which |
| 1815 | * means that if you want a shared writable mapping, you'd better |
| 1816 | * ask for a shared writable mapping! |
| 1817 | * |
| 1818 | * The page does not need to be reserved. |
Konstantin Khlebnikov | 4b6e1e3 | 2012-10-08 16:28:40 -0700 | [diff] [blame] | 1819 | * |
| 1820 | * Usually this function is called from f_op->mmap() handler |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 1821 | * under mm->mmap_lock write-lock, so it can change vma->vm_flags. |
Konstantin Khlebnikov | 4b6e1e3 | 2012-10-08 16:28:40 -0700 | [diff] [blame] | 1822 | * Caller must set VM_MIXEDMAP on vma if it wants to call this |
| 1823 | * function from other places, for example from page-fault handler. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 1824 | * |
| 1825 | * Return: %0 on success, negative error code otherwise. |
Linus Torvalds | a145dd4 | 2005-11-30 09:35:19 -0800 | [diff] [blame] | 1826 | */ |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 1827 | int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, |
| 1828 | struct page *page) |
Linus Torvalds | a145dd4 | 2005-11-30 09:35:19 -0800 | [diff] [blame] | 1829 | { |
| 1830 | if (addr < vma->vm_start || addr >= vma->vm_end) |
| 1831 | return -EFAULT; |
| 1832 | if (!page_count(page)) |
| 1833 | return -EINVAL; |
Konstantin Khlebnikov | 4b6e1e3 | 2012-10-08 16:28:40 -0700 | [diff] [blame] | 1834 | if (!(vma->vm_flags & VM_MIXEDMAP)) { |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1835 | BUG_ON(mmap_read_trylock(vma->vm_mm)); |
Konstantin Khlebnikov | 4b6e1e3 | 2012-10-08 16:28:40 -0700 | [diff] [blame] | 1836 | BUG_ON(vma->vm_flags & VM_PFNMAP); |
| 1837 | vma->vm_flags |= VM_MIXEDMAP; |
| 1838 | } |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 1839 | return insert_page(vma, addr, page, vma->vm_page_prot); |
Linus Torvalds | a145dd4 | 2005-11-30 09:35:19 -0800 | [diff] [blame] | 1840 | } |
Linus Torvalds | e3c3374 | 2005-12-03 20:48:11 -0800 | [diff] [blame] | 1841 | EXPORT_SYMBOL(vm_insert_page); |
Linus Torvalds | a145dd4 | 2005-11-30 09:35:19 -0800 | [diff] [blame] | 1842 | |
Souptick Joarder | a667d74 | 2019-05-13 17:21:56 -0700 | [diff] [blame] | 1843 | /* |
| 1844 | * __vm_map_pages - maps range of kernel pages into user vma |
| 1845 | * @vma: user vma to map to |
| 1846 | * @pages: pointer to array of source kernel pages |
| 1847 | * @num: number of pages in page array |
| 1848 | * @offset: user's requested vm_pgoff |
| 1849 | * |
| 1850 | * This allows drivers to map range of kernel pages into a user vma. |
| 1851 | * |
| 1852 | * Return: 0 on success and error code otherwise. |
| 1853 | */ |
| 1854 | static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages, |
| 1855 | unsigned long num, unsigned long offset) |
| 1856 | { |
| 1857 | unsigned long count = vma_pages(vma); |
| 1858 | unsigned long uaddr = vma->vm_start; |
| 1859 | int ret, i; |
| 1860 | |
| 1861 | /* Fail if the user requested offset is beyond the end of the object */ |
Miguel Ojeda | 96756fc | 2019-07-11 20:58:47 -0700 | [diff] [blame] | 1862 | if (offset >= num) |
Souptick Joarder | a667d74 | 2019-05-13 17:21:56 -0700 | [diff] [blame] | 1863 | return -ENXIO; |
| 1864 | |
| 1865 | /* Fail if the user requested size exceeds available object size */ |
| 1866 | if (count > num - offset) |
| 1867 | return -ENXIO; |
| 1868 | |
| 1869 | for (i = 0; i < count; i++) { |
| 1870 | ret = vm_insert_page(vma, uaddr, pages[offset + i]); |
| 1871 | if (ret < 0) |
| 1872 | return ret; |
| 1873 | uaddr += PAGE_SIZE; |
| 1874 | } |
| 1875 | |
| 1876 | return 0; |
| 1877 | } |
| 1878 | |
| 1879 | /** |
| 1880 | * vm_map_pages - maps range of kernel pages starts with non zero offset |
| 1881 | * @vma: user vma to map to |
| 1882 | * @pages: pointer to array of source kernel pages |
| 1883 | * @num: number of pages in page array |
| 1884 | * |
| 1885 | * Maps an object consisting of @num pages, catering for the user's |
| 1886 | * requested vm_pgoff |
| 1887 | * |
| 1888 | * If we fail to insert any page into the vma, the function will return |
| 1889 | * immediately leaving any previously inserted pages present. Callers |
| 1890 | * from the mmap handler may immediately return the error as their caller |
| 1891 | * will destroy the vma, removing any successfully inserted pages. Other |
| 1892 | * callers should make their own arrangements for calling unmap_region(). |
| 1893 | * |
| 1894 | * Context: Process context. Called by mmap handlers. |
| 1895 | * Return: 0 on success and error code otherwise. |
| 1896 | */ |
| 1897 | int vm_map_pages(struct vm_area_struct *vma, struct page **pages, |
| 1898 | unsigned long num) |
| 1899 | { |
| 1900 | return __vm_map_pages(vma, pages, num, vma->vm_pgoff); |
| 1901 | } |
| 1902 | EXPORT_SYMBOL(vm_map_pages); |
| 1903 | |
| 1904 | /** |
| 1905 | * vm_map_pages_zero - map range of kernel pages starts with zero offset |
| 1906 | * @vma: user vma to map to |
| 1907 | * @pages: pointer to array of source kernel pages |
| 1908 | * @num: number of pages in page array |
| 1909 | * |
| 1910 | * Similar to vm_map_pages(), except that it explicitly sets the offset |
| 1911 | * to 0. This function is intended for the drivers that did not consider |
| 1912 | * vm_pgoff. |
| 1913 | * |
| 1914 | * Context: Process context. Called by mmap handlers. |
| 1915 | * Return: 0 on success and error code otherwise. |
| 1916 | */ |
| 1917 | int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages, |
| 1918 | unsigned long num) |
| 1919 | { |
| 1920 | return __vm_map_pages(vma, pages, num, 0); |
| 1921 | } |
| 1922 | EXPORT_SYMBOL(vm_map_pages_zero); |
| 1923 | |
Matthew Wilcox | 9b5a8e0 | 2018-10-26 15:04:40 -0700 | [diff] [blame] | 1924 | static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr, |
Ross Zwisler | b2770da6 | 2017-09-06 16:18:35 -0700 | [diff] [blame] | 1925 | pfn_t pfn, pgprot_t prot, bool mkwrite) |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 1926 | { |
| 1927 | struct mm_struct *mm = vma->vm_mm; |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 1928 | pte_t *pte, entry; |
| 1929 | spinlock_t *ptl; |
| 1930 | |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 1931 | pte = get_locked_pte(mm, addr, &ptl); |
| 1932 | if (!pte) |
Matthew Wilcox | 9b5a8e0 | 2018-10-26 15:04:40 -0700 | [diff] [blame] | 1933 | return VM_FAULT_OOM; |
Ross Zwisler | b2770da6 | 2017-09-06 16:18:35 -0700 | [diff] [blame] | 1934 | if (!pte_none(*pte)) { |
| 1935 | if (mkwrite) { |
| 1936 | /* |
| 1937 | * For read faults on private mappings the PFN passed |
| 1938 | * in may not match the PFN we have mapped if the |
| 1939 | * mapped PFN is a writeable COW page. In the mkwrite |
| 1940 | * case we are creating a writable PTE for a shared |
Jan Kara | f2c57d9 | 2018-10-30 15:10:47 -0700 | [diff] [blame] | 1941 | * mapping and we expect the PFNs to match. If they |
| 1942 | * don't match, we are likely racing with block |
| 1943 | * allocation and mapping invalidation so just skip the |
| 1944 | * update. |
Ross Zwisler | b2770da6 | 2017-09-06 16:18:35 -0700 | [diff] [blame] | 1945 | */ |
Jan Kara | f2c57d9 | 2018-10-30 15:10:47 -0700 | [diff] [blame] | 1946 | if (pte_pfn(*pte) != pfn_t_to_pfn(pfn)) { |
| 1947 | WARN_ON_ONCE(!is_zero_pfn(pte_pfn(*pte))); |
Ross Zwisler | b2770da6 | 2017-09-06 16:18:35 -0700 | [diff] [blame] | 1948 | goto out_unlock; |
Jan Kara | f2c57d9 | 2018-10-30 15:10:47 -0700 | [diff] [blame] | 1949 | } |
Jan Kara | cae85cb | 2019-03-28 20:43:19 -0700 | [diff] [blame] | 1950 | entry = pte_mkyoung(*pte); |
| 1951 | entry = maybe_mkwrite(pte_mkdirty(entry), vma); |
| 1952 | if (ptep_set_access_flags(vma, addr, pte, entry, 1)) |
| 1953 | update_mmu_cache(vma, addr, pte); |
| 1954 | } |
| 1955 | goto out_unlock; |
Ross Zwisler | b2770da6 | 2017-09-06 16:18:35 -0700 | [diff] [blame] | 1956 | } |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 1957 | |
| 1958 | /* Ok, finally just insert the thing.. */ |
Dan Williams | 01c8f1c | 2016-01-15 16:56:40 -0800 | [diff] [blame] | 1959 | if (pfn_t_devmap(pfn)) |
| 1960 | entry = pte_mkdevmap(pfn_t_pte(pfn, prot)); |
| 1961 | else |
| 1962 | entry = pte_mkspecial(pfn_t_pte(pfn, prot)); |
Ross Zwisler | b2770da6 | 2017-09-06 16:18:35 -0700 | [diff] [blame] | 1963 | |
Ross Zwisler | b2770da6 | 2017-09-06 16:18:35 -0700 | [diff] [blame] | 1964 | if (mkwrite) { |
| 1965 | entry = pte_mkyoung(entry); |
| 1966 | entry = maybe_mkwrite(pte_mkdirty(entry), vma); |
| 1967 | } |
| 1968 | |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 1969 | set_pte_at(mm, addr, pte, entry); |
Russell King | 4b3073e | 2009-12-18 16:40:18 +0000 | [diff] [blame] | 1970 | update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 1971 | |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 1972 | out_unlock: |
| 1973 | pte_unmap_unlock(pte, ptl); |
Matthew Wilcox | 9b5a8e0 | 2018-10-26 15:04:40 -0700 | [diff] [blame] | 1974 | return VM_FAULT_NOPAGE; |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 1975 | } |
| 1976 | |
Matthew Wilcox | f5e6d1d | 2018-10-26 15:04:13 -0700 | [diff] [blame] | 1977 | /** |
| 1978 | * vmf_insert_pfn_prot - insert single pfn into user vma with specified pgprot |
| 1979 | * @vma: user vma to map to |
| 1980 | * @addr: target user address of this page |
| 1981 | * @pfn: source kernel pfn |
| 1982 | * @pgprot: pgprot flags for the inserted page |
| 1983 | * |
Randy Dunlap | a1a0aea | 2020-08-11 18:33:05 -0700 | [diff] [blame] | 1984 | * This is exactly like vmf_insert_pfn(), except that it allows drivers |
Matthew Wilcox | f5e6d1d | 2018-10-26 15:04:13 -0700 | [diff] [blame] | 1985 | * to override pgprot on a per-page basis. |
| 1986 | * |
| 1987 | * This only makes sense for IO mappings, and it makes no sense for |
| 1988 | * COW mappings. In general, using multiple vmas is preferable; |
Matthew Wilcox | ae2b01f | 2018-10-26 15:04:29 -0700 | [diff] [blame] | 1989 | * vmf_insert_pfn_prot should only be used if using multiple VMAs is |
Matthew Wilcox | f5e6d1d | 2018-10-26 15:04:13 -0700 | [diff] [blame] | 1990 | * impractical. |
| 1991 | * |
Thomas Hellstrom | 574c5b3d | 2019-11-22 09:25:12 +0100 | [diff] [blame] | 1992 | * See vmf_insert_mixed_prot() for a discussion of the implication of using |
| 1993 | * a value of @pgprot different from that of @vma->vm_page_prot. |
| 1994 | * |
Matthew Wilcox | ae2b01f | 2018-10-26 15:04:29 -0700 | [diff] [blame] | 1995 | * Context: Process context. May allocate using %GFP_KERNEL. |
Matthew Wilcox | f5e6d1d | 2018-10-26 15:04:13 -0700 | [diff] [blame] | 1996 | * Return: vm_fault_t value. |
| 1997 | */ |
| 1998 | vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, |
| 1999 | unsigned long pfn, pgprot_t pgprot) |
| 2000 | { |
Matthew Wilcox | 6d95854 | 2018-10-26 15:04:33 -0700 | [diff] [blame] | 2001 | /* |
| 2002 | * Technically, architectures with pte_special can avoid all these |
| 2003 | * restrictions (same for remap_pfn_range). However we would like |
| 2004 | * consistency in testing and feature parity among all, so we should |
| 2005 | * try to keep these invariants in place for everybody. |
| 2006 | */ |
| 2007 | BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); |
| 2008 | BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == |
| 2009 | (VM_PFNMAP|VM_MIXEDMAP)); |
| 2010 | BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); |
| 2011 | BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); |
| 2012 | |
| 2013 | if (addr < vma->vm_start || addr >= vma->vm_end) |
| 2014 | return VM_FAULT_SIGBUS; |
| 2015 | |
| 2016 | if (!pfn_modify_allowed(pfn, pgprot)) |
| 2017 | return VM_FAULT_SIGBUS; |
| 2018 | |
| 2019 | track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)); |
| 2020 | |
Matthew Wilcox | 9b5a8e0 | 2018-10-26 15:04:40 -0700 | [diff] [blame] | 2021 | return insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot, |
Matthew Wilcox | 6d95854 | 2018-10-26 15:04:33 -0700 | [diff] [blame] | 2022 | false); |
Matthew Wilcox | f5e6d1d | 2018-10-26 15:04:13 -0700 | [diff] [blame] | 2023 | } |
| 2024 | EXPORT_SYMBOL(vmf_insert_pfn_prot); |
Nick Piggin | e0dc0d8 | 2007-02-12 00:51:36 -0800 | [diff] [blame] | 2025 | |
Matthew Wilcox | ae2b01f | 2018-10-26 15:04:29 -0700 | [diff] [blame] | 2026 | /** |
| 2027 | * vmf_insert_pfn - insert single pfn into user vma |
| 2028 | * @vma: user vma to map to |
| 2029 | * @addr: target user address of this page |
| 2030 | * @pfn: source kernel pfn |
| 2031 | * |
| 2032 | * Similar to vm_insert_page, this allows drivers to insert individual pages |
| 2033 | * they've allocated into a user vma. Same comments apply. |
| 2034 | * |
| 2035 | * This function should only be called from a vm_ops->fault handler, and |
| 2036 | * in that case the handler should return the result of this function. |
| 2037 | * |
| 2038 | * vma cannot be a COW mapping. |
| 2039 | * |
| 2040 | * As this is called only for pages that do not currently exist, we |
| 2041 | * do not need to flush old virtual caches or the TLB. |
| 2042 | * |
| 2043 | * Context: Process context. May allocate using %GFP_KERNEL. |
| 2044 | * Return: vm_fault_t value. |
| 2045 | */ |
| 2046 | vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr, |
| 2047 | unsigned long pfn) |
| 2048 | { |
| 2049 | return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot); |
| 2050 | } |
| 2051 | EXPORT_SYMBOL(vmf_insert_pfn); |
| 2052 | |
Dan Williams | 785a3fa | 2017-10-23 07:20:00 -0700 | [diff] [blame] | 2053 | static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn) |
| 2054 | { |
| 2055 | /* these checks mirror the abort conditions in vm_normal_page */ |
| 2056 | if (vma->vm_flags & VM_MIXEDMAP) |
| 2057 | return true; |
| 2058 | if (pfn_t_devmap(pfn)) |
| 2059 | return true; |
| 2060 | if (pfn_t_special(pfn)) |
| 2061 | return true; |
| 2062 | if (is_zero_pfn(pfn_t_to_pfn(pfn))) |
| 2063 | return true; |
| 2064 | return false; |
| 2065 | } |
| 2066 | |
Matthew Wilcox | 79f3aa5 | 2018-10-26 15:04:37 -0700 | [diff] [blame] | 2067 | static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma, |
Thomas Hellstrom | 574c5b3d | 2019-11-22 09:25:12 +0100 | [diff] [blame] | 2068 | unsigned long addr, pfn_t pfn, pgprot_t pgprot, |
| 2069 | bool mkwrite) |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 2070 | { |
Matthew Wilcox | 79f3aa5 | 2018-10-26 15:04:37 -0700 | [diff] [blame] | 2071 | int err; |
Dan Williams | 87744ab | 2016-10-07 17:00:18 -0700 | [diff] [blame] | 2072 | |
Dan Williams | 785a3fa | 2017-10-23 07:20:00 -0700 | [diff] [blame] | 2073 | BUG_ON(!vm_mixed_ok(vma, pfn)); |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 2074 | |
| 2075 | if (addr < vma->vm_start || addr >= vma->vm_end) |
Matthew Wilcox | 79f3aa5 | 2018-10-26 15:04:37 -0700 | [diff] [blame] | 2076 | return VM_FAULT_SIGBUS; |
Borislav Petkov | 308a047 | 2016-10-26 19:43:43 +0200 | [diff] [blame] | 2077 | |
| 2078 | track_pfn_insert(vma, &pgprot, pfn); |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 2079 | |
Andi Kleen | 42e4089 | 2018-06-13 15:48:27 -0700 | [diff] [blame] | 2080 | if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot)) |
Matthew Wilcox | 79f3aa5 | 2018-10-26 15:04:37 -0700 | [diff] [blame] | 2081 | return VM_FAULT_SIGBUS; |
Andi Kleen | 42e4089 | 2018-06-13 15:48:27 -0700 | [diff] [blame] | 2082 | |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 2083 | /* |
| 2084 | * If we don't have pte special, then we have to use the pfn_valid() |
| 2085 | * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* |
| 2086 | * refcount the page if pfn_valid is true (hence insert_page rather |
Hugh Dickins | 62eede6 | 2009-09-21 17:03:34 -0700 | [diff] [blame] | 2087 | * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP |
| 2088 | * without pte special, it would there be refcounted as a normal page. |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 2089 | */ |
Laurent Dufour | 00b3a33 | 2018-06-07 17:06:12 -0700 | [diff] [blame] | 2090 | if (!IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && |
| 2091 | !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) { |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 2092 | struct page *page; |
| 2093 | |
Dan Williams | 03fc2da | 2016-01-26 09:48:05 -0800 | [diff] [blame] | 2094 | /* |
| 2095 | * At this point we are committed to insert_page() |
| 2096 | * regardless of whether the caller specified flags that |
| 2097 | * result in pfn_t_has_page() == false. |
| 2098 | */ |
| 2099 | page = pfn_to_page(pfn_t_to_pfn(pfn)); |
Matthew Wilcox | 79f3aa5 | 2018-10-26 15:04:37 -0700 | [diff] [blame] | 2100 | err = insert_page(vma, addr, page, pgprot); |
| 2101 | } else { |
Matthew Wilcox | 9b5a8e0 | 2018-10-26 15:04:40 -0700 | [diff] [blame] | 2102 | return insert_pfn(vma, addr, pfn, pgprot, mkwrite); |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 2103 | } |
Ross Zwisler | b2770da6 | 2017-09-06 16:18:35 -0700 | [diff] [blame] | 2104 | |
Matthew Wilcox | 5d74763 | 2018-10-26 15:04:10 -0700 | [diff] [blame] | 2105 | if (err == -ENOMEM) |
| 2106 | return VM_FAULT_OOM; |
| 2107 | if (err < 0 && err != -EBUSY) |
| 2108 | return VM_FAULT_SIGBUS; |
| 2109 | |
| 2110 | return VM_FAULT_NOPAGE; |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 2111 | } |
Matthew Wilcox | 79f3aa5 | 2018-10-26 15:04:37 -0700 | [diff] [blame] | 2112 | |
Thomas Hellstrom | 574c5b3d | 2019-11-22 09:25:12 +0100 | [diff] [blame] | 2113 | /** |
| 2114 | * vmf_insert_mixed_prot - insert single pfn into user vma with specified pgprot |
| 2115 | * @vma: user vma to map to |
| 2116 | * @addr: target user address of this page |
| 2117 | * @pfn: source kernel pfn |
| 2118 | * @pgprot: pgprot flags for the inserted page |
| 2119 | * |
Randy Dunlap | a1a0aea | 2020-08-11 18:33:05 -0700 | [diff] [blame] | 2120 | * This is exactly like vmf_insert_mixed(), except that it allows drivers |
Thomas Hellstrom | 574c5b3d | 2019-11-22 09:25:12 +0100 | [diff] [blame] | 2121 | * to override pgprot on a per-page basis. |
| 2122 | * |
| 2123 | * Typically this function should be used by drivers to set caching- and |
| 2124 | * encryption bits different than those of @vma->vm_page_prot, because |
| 2125 | * the caching- or encryption mode may not be known at mmap() time. |
| 2126 | * This is ok as long as @vma->vm_page_prot is not used by the core vm |
| 2127 | * to set caching and encryption bits for those vmas (except for COW pages). |
| 2128 | * This is ensured by core vm only modifying these page table entries using |
| 2129 | * functions that don't touch caching- or encryption bits, using pte_modify() |
| 2130 | * if needed. (See for example mprotect()). |
| 2131 | * Also when new page-table entries are created, this is only done using the |
| 2132 | * fault() callback, and never using the value of vma->vm_page_prot, |
| 2133 | * except for page-table entries that point to anonymous pages as the result |
| 2134 | * of COW. |
| 2135 | * |
| 2136 | * Context: Process context. May allocate using %GFP_KERNEL. |
| 2137 | * Return: vm_fault_t value. |
| 2138 | */ |
| 2139 | vm_fault_t vmf_insert_mixed_prot(struct vm_area_struct *vma, unsigned long addr, |
| 2140 | pfn_t pfn, pgprot_t pgprot) |
| 2141 | { |
| 2142 | return __vm_insert_mixed(vma, addr, pfn, pgprot, false); |
| 2143 | } |
Thomas Hellstrom | 5379e4d | 2019-11-22 09:34:35 +0100 | [diff] [blame] | 2144 | EXPORT_SYMBOL(vmf_insert_mixed_prot); |
Thomas Hellstrom | 574c5b3d | 2019-11-22 09:25:12 +0100 | [diff] [blame] | 2145 | |
Matthew Wilcox | 79f3aa5 | 2018-10-26 15:04:37 -0700 | [diff] [blame] | 2146 | vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr, |
| 2147 | pfn_t pfn) |
| 2148 | { |
Thomas Hellstrom | 574c5b3d | 2019-11-22 09:25:12 +0100 | [diff] [blame] | 2149 | return __vm_insert_mixed(vma, addr, pfn, vma->vm_page_prot, false); |
Matthew Wilcox | 79f3aa5 | 2018-10-26 15:04:37 -0700 | [diff] [blame] | 2150 | } |
Matthew Wilcox | 5d74763 | 2018-10-26 15:04:10 -0700 | [diff] [blame] | 2151 | EXPORT_SYMBOL(vmf_insert_mixed); |
Nick Piggin | 423bad60 | 2008-04-28 02:13:01 -0700 | [diff] [blame] | 2152 | |
Souptick Joarder | ab77dab | 2018-06-07 17:04:29 -0700 | [diff] [blame] | 2153 | /* |
| 2154 | * If the insertion of PTE failed because someone else already added a |
| 2155 | * different entry in the mean time, we treat that as success as we assume |
| 2156 | * the same entry was actually inserted. |
| 2157 | */ |
Souptick Joarder | ab77dab | 2018-06-07 17:04:29 -0700 | [diff] [blame] | 2158 | vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma, |
| 2159 | unsigned long addr, pfn_t pfn) |
Ross Zwisler | b2770da6 | 2017-09-06 16:18:35 -0700 | [diff] [blame] | 2160 | { |
Thomas Hellstrom | 574c5b3d | 2019-11-22 09:25:12 +0100 | [diff] [blame] | 2161 | return __vm_insert_mixed(vma, addr, pfn, vma->vm_page_prot, true); |
Ross Zwisler | b2770da6 | 2017-09-06 16:18:35 -0700 | [diff] [blame] | 2162 | } |
Souptick Joarder | ab77dab | 2018-06-07 17:04:29 -0700 | [diff] [blame] | 2163 | EXPORT_SYMBOL(vmf_insert_mixed_mkwrite); |
Ross Zwisler | b2770da6 | 2017-09-06 16:18:35 -0700 | [diff] [blame] | 2164 | |
Linus Torvalds | a145dd4 | 2005-11-30 09:35:19 -0800 | [diff] [blame] | 2165 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | * maps a range of physical memory into the requested pages. the old |
| 2167 | * mappings are removed. any references to nonexistent pages results |
| 2168 | * in null mappings (currently treated as "copy-on-access") |
| 2169 | */ |
| 2170 | static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, |
| 2171 | unsigned long addr, unsigned long end, |
| 2172 | unsigned long pfn, pgprot_t prot) |
| 2173 | { |
Miaohe Lin | 90a3e37 | 2021-02-24 12:04:33 -0800 | [diff] [blame] | 2174 | pte_t *pte, *mapped_pte; |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 2175 | spinlock_t *ptl; |
Andi Kleen | 42e4089 | 2018-06-13 15:48:27 -0700 | [diff] [blame] | 2176 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | |
Miaohe Lin | 90a3e37 | 2021-02-24 12:04:33 -0800 | [diff] [blame] | 2178 | mapped_pte = pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | if (!pte) |
| 2180 | return -ENOMEM; |
Zachary Amsden | 6606c3e | 2006-09-30 23:29:33 -0700 | [diff] [blame] | 2181 | arch_enter_lazy_mmu_mode(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | do { |
| 2183 | BUG_ON(!pte_none(*pte)); |
Andi Kleen | 42e4089 | 2018-06-13 15:48:27 -0700 | [diff] [blame] | 2184 | if (!pfn_modify_allowed(pfn, prot)) { |
| 2185 | err = -EACCES; |
| 2186 | break; |
| 2187 | } |
Nick Piggin | 7e67513 | 2008-04-28 02:13:00 -0700 | [diff] [blame] | 2188 | set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | pfn++; |
| 2190 | } while (pte++, addr += PAGE_SIZE, addr != end); |
Zachary Amsden | 6606c3e | 2006-09-30 23:29:33 -0700 | [diff] [blame] | 2191 | arch_leave_lazy_mmu_mode(); |
Miaohe Lin | 90a3e37 | 2021-02-24 12:04:33 -0800 | [diff] [blame] | 2192 | pte_unmap_unlock(mapped_pte, ptl); |
Andi Kleen | 42e4089 | 2018-06-13 15:48:27 -0700 | [diff] [blame] | 2193 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | } |
| 2195 | |
| 2196 | static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, |
| 2197 | unsigned long addr, unsigned long end, |
| 2198 | unsigned long pfn, pgprot_t prot) |
| 2199 | { |
| 2200 | pmd_t *pmd; |
| 2201 | unsigned long next; |
Andi Kleen | 42e4089 | 2018-06-13 15:48:27 -0700 | [diff] [blame] | 2202 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | |
| 2204 | pfn -= addr >> PAGE_SHIFT; |
| 2205 | pmd = pmd_alloc(mm, pud, addr); |
| 2206 | if (!pmd) |
| 2207 | return -ENOMEM; |
Andrea Arcangeli | f66055ab | 2011-01-13 15:46:54 -0800 | [diff] [blame] | 2208 | VM_BUG_ON(pmd_trans_huge(*pmd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | do { |
| 2210 | next = pmd_addr_end(addr, end); |
Andi Kleen | 42e4089 | 2018-06-13 15:48:27 -0700 | [diff] [blame] | 2211 | err = remap_pte_range(mm, pmd, addr, next, |
| 2212 | pfn + (addr >> PAGE_SHIFT), prot); |
| 2213 | if (err) |
| 2214 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | } while (pmd++, addr = next, addr != end); |
| 2216 | return 0; |
| 2217 | } |
| 2218 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2219 | static inline int remap_pud_range(struct mm_struct *mm, p4d_t *p4d, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | unsigned long addr, unsigned long end, |
| 2221 | unsigned long pfn, pgprot_t prot) |
| 2222 | { |
| 2223 | pud_t *pud; |
| 2224 | unsigned long next; |
Andi Kleen | 42e4089 | 2018-06-13 15:48:27 -0700 | [diff] [blame] | 2225 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | |
| 2227 | pfn -= addr >> PAGE_SHIFT; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2228 | pud = pud_alloc(mm, p4d, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | if (!pud) |
| 2230 | return -ENOMEM; |
| 2231 | do { |
| 2232 | next = pud_addr_end(addr, end); |
Andi Kleen | 42e4089 | 2018-06-13 15:48:27 -0700 | [diff] [blame] | 2233 | err = remap_pmd_range(mm, pud, addr, next, |
| 2234 | pfn + (addr >> PAGE_SHIFT), prot); |
| 2235 | if (err) |
| 2236 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | } while (pud++, addr = next, addr != end); |
| 2238 | return 0; |
| 2239 | } |
| 2240 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2241 | static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd, |
| 2242 | unsigned long addr, unsigned long end, |
| 2243 | unsigned long pfn, pgprot_t prot) |
| 2244 | { |
| 2245 | p4d_t *p4d; |
| 2246 | unsigned long next; |
Andi Kleen | 42e4089 | 2018-06-13 15:48:27 -0700 | [diff] [blame] | 2247 | int err; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2248 | |
| 2249 | pfn -= addr >> PAGE_SHIFT; |
| 2250 | p4d = p4d_alloc(mm, pgd, addr); |
| 2251 | if (!p4d) |
| 2252 | return -ENOMEM; |
| 2253 | do { |
| 2254 | next = p4d_addr_end(addr, end); |
Andi Kleen | 42e4089 | 2018-06-13 15:48:27 -0700 | [diff] [blame] | 2255 | err = remap_pud_range(mm, p4d, addr, next, |
| 2256 | pfn + (addr >> PAGE_SHIFT), prot); |
| 2257 | if (err) |
| 2258 | return err; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2259 | } while (p4d++, addr = next, addr != end); |
| 2260 | return 0; |
| 2261 | } |
| 2262 | |
Rolf Eike Beer | bfa5bf6 | 2006-09-25 23:31:22 -0700 | [diff] [blame] | 2263 | /** |
| 2264 | * remap_pfn_range - remap kernel memory to userspace |
| 2265 | * @vma: user vma to map to |
Alex Zhang | 0c4123e | 2020-08-06 23:22:24 -0700 | [diff] [blame] | 2266 | * @addr: target page aligned user address to start at |
WANG Wenhu | 86a7633 | 2020-04-01 21:09:03 -0700 | [diff] [blame] | 2267 | * @pfn: page frame number of kernel physical memory address |
chenqiwu | 552657b | 2020-04-06 20:08:33 -0700 | [diff] [blame] | 2268 | * @size: size of mapping area |
Rolf Eike Beer | bfa5bf6 | 2006-09-25 23:31:22 -0700 | [diff] [blame] | 2269 | * @prot: page protection flags for this mapping |
| 2270 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2271 | * Note: this is only safe if the mm semaphore is held when called. |
| 2272 | * |
| 2273 | * Return: %0 on success, negative error code otherwise. |
Rolf Eike Beer | bfa5bf6 | 2006-09-25 23:31:22 -0700 | [diff] [blame] | 2274 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, |
| 2276 | unsigned long pfn, unsigned long size, pgprot_t prot) |
| 2277 | { |
| 2278 | pgd_t *pgd; |
| 2279 | unsigned long next; |
Hugh Dickins | 2d15cab | 2005-06-25 14:54:33 -0700 | [diff] [blame] | 2280 | unsigned long end = addr + PAGE_ALIGN(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | struct mm_struct *mm = vma->vm_mm; |
Yongji Xie | d5957d2 | 2016-05-20 16:57:41 -0700 | [diff] [blame] | 2282 | unsigned long remap_pfn = pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | int err; |
| 2284 | |
Alex Zhang | 0c4123e | 2020-08-06 23:22:24 -0700 | [diff] [blame] | 2285 | if (WARN_ON_ONCE(!PAGE_ALIGNED(addr))) |
| 2286 | return -EINVAL; |
| 2287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | /* |
| 2289 | * Physically remapped pages are special. Tell the |
| 2290 | * rest of the world about it: |
| 2291 | * VM_IO tells people not to look at these pages |
| 2292 | * (accesses can have side effects). |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 2293 | * VM_PFNMAP tells the core MM that the base pages are just |
| 2294 | * raw PFN mappings, and do not have a "struct page" associated |
| 2295 | * with them. |
Konstantin Khlebnikov | 314e51b | 2012-10-08 16:29:02 -0700 | [diff] [blame] | 2296 | * VM_DONTEXPAND |
| 2297 | * Disable vma merging and expanding with mremap(). |
| 2298 | * VM_DONTDUMP |
| 2299 | * Omit vma from core dump, even when VM_IO turned off. |
Linus Torvalds | fb155c1 | 2005-12-11 19:46:02 -0800 | [diff] [blame] | 2300 | * |
| 2301 | * There's a horrible special case to handle copy-on-write |
| 2302 | * behaviour that some programs depend on. We mark the "original" |
| 2303 | * un-COW'ed pages by matching them up with "vma->vm_pgoff". |
Konstantin Khlebnikov | b3b9c29 | 2012-10-08 16:28:34 -0700 | [diff] [blame] | 2304 | * See vm_normal_page() for details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | */ |
Konstantin Khlebnikov | b3b9c29 | 2012-10-08 16:28:34 -0700 | [diff] [blame] | 2306 | if (is_cow_mapping(vma->vm_flags)) { |
| 2307 | if (addr != vma->vm_start || end != vma->vm_end) |
| 2308 | return -EINVAL; |
Linus Torvalds | fb155c1 | 2005-12-11 19:46:02 -0800 | [diff] [blame] | 2309 | vma->vm_pgoff = pfn; |
Konstantin Khlebnikov | b3b9c29 | 2012-10-08 16:28:34 -0700 | [diff] [blame] | 2310 | } |
| 2311 | |
Yongji Xie | d5957d2 | 2016-05-20 16:57:41 -0700 | [diff] [blame] | 2312 | err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size)); |
Konstantin Khlebnikov | b3b9c29 | 2012-10-08 16:28:34 -0700 | [diff] [blame] | 2313 | if (err) |
venkatesh.pallipadi@intel.com | 3c8bb73 | 2008-12-18 11:41:27 -0800 | [diff] [blame] | 2314 | return -EINVAL; |
Linus Torvalds | fb155c1 | 2005-12-11 19:46:02 -0800 | [diff] [blame] | 2315 | |
Konstantin Khlebnikov | 314e51b | 2012-10-08 16:29:02 -0700 | [diff] [blame] | 2316 | vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | |
| 2318 | BUG_ON(addr >= end); |
| 2319 | pfn -= addr >> PAGE_SHIFT; |
| 2320 | pgd = pgd_offset(mm, addr); |
| 2321 | flush_cache_range(vma, addr, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | do { |
| 2323 | next = pgd_addr_end(addr, end); |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2324 | err = remap_p4d_range(mm, pgd, addr, next, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | pfn + (addr >> PAGE_SHIFT), prot); |
| 2326 | if (err) |
| 2327 | break; |
| 2328 | } while (pgd++, addr = next, addr != end); |
venkatesh.pallipadi@intel.com | 2ab6403 | 2008-12-18 11:41:29 -0800 | [diff] [blame] | 2329 | |
| 2330 | if (err) |
Yongji Xie | d5957d2 | 2016-05-20 16:57:41 -0700 | [diff] [blame] | 2331 | untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size)); |
venkatesh.pallipadi@intel.com | 2ab6403 | 2008-12-18 11:41:29 -0800 | [diff] [blame] | 2332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | return err; |
| 2334 | } |
| 2335 | EXPORT_SYMBOL(remap_pfn_range); |
| 2336 | |
Linus Torvalds | b4cbb19 | 2013-04-16 13:45:37 -0700 | [diff] [blame] | 2337 | /** |
| 2338 | * vm_iomap_memory - remap memory to userspace |
| 2339 | * @vma: user vma to map to |
Wang Wenhu | abd69b9 | 2020-04-01 21:09:07 -0700 | [diff] [blame] | 2340 | * @start: start of the physical memory to be mapped |
Linus Torvalds | b4cbb19 | 2013-04-16 13:45:37 -0700 | [diff] [blame] | 2341 | * @len: size of area |
| 2342 | * |
| 2343 | * This is a simplified io_remap_pfn_range() for common driver use. The |
| 2344 | * driver just needs to give us the physical memory range to be mapped, |
| 2345 | * we'll figure out the rest from the vma information. |
| 2346 | * |
| 2347 | * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get |
| 2348 | * whatever write-combining details or similar. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2349 | * |
| 2350 | * Return: %0 on success, negative error code otherwise. |
Linus Torvalds | b4cbb19 | 2013-04-16 13:45:37 -0700 | [diff] [blame] | 2351 | */ |
| 2352 | int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) |
| 2353 | { |
| 2354 | unsigned long vm_len, pfn, pages; |
| 2355 | |
| 2356 | /* Check that the physical memory area passed in looks valid */ |
| 2357 | if (start + len < start) |
| 2358 | return -EINVAL; |
| 2359 | /* |
| 2360 | * You *really* shouldn't map things that aren't page-aligned, |
| 2361 | * but we've historically allowed it because IO memory might |
| 2362 | * just have smaller alignment. |
| 2363 | */ |
| 2364 | len += start & ~PAGE_MASK; |
| 2365 | pfn = start >> PAGE_SHIFT; |
| 2366 | pages = (len + ~PAGE_MASK) >> PAGE_SHIFT; |
| 2367 | if (pfn + pages < pfn) |
| 2368 | return -EINVAL; |
| 2369 | |
| 2370 | /* We start the mapping 'vm_pgoff' pages into the area */ |
| 2371 | if (vma->vm_pgoff > pages) |
| 2372 | return -EINVAL; |
| 2373 | pfn += vma->vm_pgoff; |
| 2374 | pages -= vma->vm_pgoff; |
| 2375 | |
| 2376 | /* Can we fit all of the mapping? */ |
| 2377 | vm_len = vma->vm_end - vma->vm_start; |
| 2378 | if (vm_len >> PAGE_SHIFT > pages) |
| 2379 | return -EINVAL; |
| 2380 | |
| 2381 | /* Ok, let it rip */ |
| 2382 | return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); |
| 2383 | } |
| 2384 | EXPORT_SYMBOL(vm_iomap_memory); |
| 2385 | |
Jeremy Fitzhardinge | aee16b3 | 2007-05-06 14:48:54 -0700 | [diff] [blame] | 2386 | static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, |
| 2387 | unsigned long addr, unsigned long end, |
Joerg Roedel | e80d390 | 2020-09-04 16:35:43 -0700 | [diff] [blame] | 2388 | pte_fn_t fn, void *data, bool create, |
| 2389 | pgtbl_mod_mask *mask) |
Jeremy Fitzhardinge | aee16b3 | 2007-05-06 14:48:54 -0700 | [diff] [blame] | 2390 | { |
Miaohe Lin | 8abb50c | 2021-02-24 12:04:42 -0800 | [diff] [blame] | 2391 | pte_t *pte, *mapped_pte; |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2392 | int err = 0; |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 2393 | spinlock_t *ptl; |
Jeremy Fitzhardinge | aee16b3 | 2007-05-06 14:48:54 -0700 | [diff] [blame] | 2394 | |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2395 | if (create) { |
Miaohe Lin | 8abb50c | 2021-02-24 12:04:42 -0800 | [diff] [blame] | 2396 | mapped_pte = pte = (mm == &init_mm) ? |
Joerg Roedel | e80d390 | 2020-09-04 16:35:43 -0700 | [diff] [blame] | 2397 | pte_alloc_kernel_track(pmd, addr, mask) : |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2398 | pte_alloc_map_lock(mm, pmd, addr, &ptl); |
| 2399 | if (!pte) |
| 2400 | return -ENOMEM; |
| 2401 | } else { |
Miaohe Lin | 8abb50c | 2021-02-24 12:04:42 -0800 | [diff] [blame] | 2402 | mapped_pte = pte = (mm == &init_mm) ? |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2403 | pte_offset_kernel(pmd, addr) : |
| 2404 | pte_offset_map_lock(mm, pmd, addr, &ptl); |
| 2405 | } |
Jeremy Fitzhardinge | aee16b3 | 2007-05-06 14:48:54 -0700 | [diff] [blame] | 2406 | |
| 2407 | BUG_ON(pmd_huge(*pmd)); |
| 2408 | |
Jeremy Fitzhardinge | 38e0edb | 2009-01-06 14:39:21 -0800 | [diff] [blame] | 2409 | arch_enter_lazy_mmu_mode(); |
| 2410 | |
Christoph Hellwig | eeb4a05 | 2020-10-17 16:15:14 -0700 | [diff] [blame] | 2411 | if (fn) { |
| 2412 | do { |
| 2413 | if (create || !pte_none(*pte)) { |
| 2414 | err = fn(pte++, addr, data); |
| 2415 | if (err) |
| 2416 | break; |
| 2417 | } |
| 2418 | } while (addr += PAGE_SIZE, addr != end); |
| 2419 | } |
Joerg Roedel | e80d390 | 2020-09-04 16:35:43 -0700 | [diff] [blame] | 2420 | *mask |= PGTBL_PTE_MODIFIED; |
Jeremy Fitzhardinge | aee16b3 | 2007-05-06 14:48:54 -0700 | [diff] [blame] | 2421 | |
Jeremy Fitzhardinge | 38e0edb | 2009-01-06 14:39:21 -0800 | [diff] [blame] | 2422 | arch_leave_lazy_mmu_mode(); |
| 2423 | |
Jeremy Fitzhardinge | aee16b3 | 2007-05-06 14:48:54 -0700 | [diff] [blame] | 2424 | if (mm != &init_mm) |
Miaohe Lin | 8abb50c | 2021-02-24 12:04:42 -0800 | [diff] [blame] | 2425 | pte_unmap_unlock(mapped_pte, ptl); |
Jeremy Fitzhardinge | aee16b3 | 2007-05-06 14:48:54 -0700 | [diff] [blame] | 2426 | return err; |
| 2427 | } |
| 2428 | |
| 2429 | static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, |
| 2430 | unsigned long addr, unsigned long end, |
Joerg Roedel | e80d390 | 2020-09-04 16:35:43 -0700 | [diff] [blame] | 2431 | pte_fn_t fn, void *data, bool create, |
| 2432 | pgtbl_mod_mask *mask) |
Jeremy Fitzhardinge | aee16b3 | 2007-05-06 14:48:54 -0700 | [diff] [blame] | 2433 | { |
| 2434 | pmd_t *pmd; |
| 2435 | unsigned long next; |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2436 | int err = 0; |
Jeremy Fitzhardinge | aee16b3 | 2007-05-06 14:48:54 -0700 | [diff] [blame] | 2437 | |
Andi Kleen | ceb8687 | 2008-07-23 21:27:50 -0700 | [diff] [blame] | 2438 | BUG_ON(pud_huge(*pud)); |
| 2439 | |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2440 | if (create) { |
Joerg Roedel | e80d390 | 2020-09-04 16:35:43 -0700 | [diff] [blame] | 2441 | pmd = pmd_alloc_track(mm, pud, addr, mask); |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2442 | if (!pmd) |
| 2443 | return -ENOMEM; |
| 2444 | } else { |
| 2445 | pmd = pmd_offset(pud, addr); |
| 2446 | } |
Jeremy Fitzhardinge | aee16b3 | 2007-05-06 14:48:54 -0700 | [diff] [blame] | 2447 | do { |
| 2448 | next = pmd_addr_end(addr, end); |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2449 | if (create || !pmd_none_or_clear_bad(pmd)) { |
| 2450 | err = apply_to_pte_range(mm, pmd, addr, next, fn, data, |
Joerg Roedel | e80d390 | 2020-09-04 16:35:43 -0700 | [diff] [blame] | 2451 | create, mask); |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2452 | if (err) |
| 2453 | break; |
| 2454 | } |
Jeremy Fitzhardinge | aee16b3 | 2007-05-06 14:48:54 -0700 | [diff] [blame] | 2455 | } while (pmd++, addr = next, addr != end); |
| 2456 | return err; |
| 2457 | } |
| 2458 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2459 | static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d, |
Jeremy Fitzhardinge | aee16b3 | 2007-05-06 14:48:54 -0700 | [diff] [blame] | 2460 | unsigned long addr, unsigned long end, |
Joerg Roedel | e80d390 | 2020-09-04 16:35:43 -0700 | [diff] [blame] | 2461 | pte_fn_t fn, void *data, bool create, |
| 2462 | pgtbl_mod_mask *mask) |
Jeremy Fitzhardinge | aee16b3 | 2007-05-06 14:48:54 -0700 | [diff] [blame] | 2463 | { |
| 2464 | pud_t *pud; |
| 2465 | unsigned long next; |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2466 | int err = 0; |
Jeremy Fitzhardinge | aee16b3 | 2007-05-06 14:48:54 -0700 | [diff] [blame] | 2467 | |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2468 | if (create) { |
Joerg Roedel | e80d390 | 2020-09-04 16:35:43 -0700 | [diff] [blame] | 2469 | pud = pud_alloc_track(mm, p4d, addr, mask); |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2470 | if (!pud) |
| 2471 | return -ENOMEM; |
| 2472 | } else { |
| 2473 | pud = pud_offset(p4d, addr); |
| 2474 | } |
Jeremy Fitzhardinge | aee16b3 | 2007-05-06 14:48:54 -0700 | [diff] [blame] | 2475 | do { |
| 2476 | next = pud_addr_end(addr, end); |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2477 | if (create || !pud_none_or_clear_bad(pud)) { |
| 2478 | err = apply_to_pmd_range(mm, pud, addr, next, fn, data, |
Joerg Roedel | e80d390 | 2020-09-04 16:35:43 -0700 | [diff] [blame] | 2479 | create, mask); |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2480 | if (err) |
| 2481 | break; |
| 2482 | } |
Jeremy Fitzhardinge | aee16b3 | 2007-05-06 14:48:54 -0700 | [diff] [blame] | 2483 | } while (pud++, addr = next, addr != end); |
| 2484 | return err; |
| 2485 | } |
| 2486 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2487 | static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd, |
| 2488 | unsigned long addr, unsigned long end, |
Joerg Roedel | e80d390 | 2020-09-04 16:35:43 -0700 | [diff] [blame] | 2489 | pte_fn_t fn, void *data, bool create, |
| 2490 | pgtbl_mod_mask *mask) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2491 | { |
| 2492 | p4d_t *p4d; |
| 2493 | unsigned long next; |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2494 | int err = 0; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2495 | |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2496 | if (create) { |
Joerg Roedel | e80d390 | 2020-09-04 16:35:43 -0700 | [diff] [blame] | 2497 | p4d = p4d_alloc_track(mm, pgd, addr, mask); |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2498 | if (!p4d) |
| 2499 | return -ENOMEM; |
| 2500 | } else { |
| 2501 | p4d = p4d_offset(pgd, addr); |
| 2502 | } |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2503 | do { |
| 2504 | next = p4d_addr_end(addr, end); |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2505 | if (create || !p4d_none_or_clear_bad(p4d)) { |
| 2506 | err = apply_to_pud_range(mm, p4d, addr, next, fn, data, |
Joerg Roedel | e80d390 | 2020-09-04 16:35:43 -0700 | [diff] [blame] | 2507 | create, mask); |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2508 | if (err) |
| 2509 | break; |
| 2510 | } |
| 2511 | } while (p4d++, addr = next, addr != end); |
| 2512 | return err; |
| 2513 | } |
| 2514 | |
| 2515 | static int __apply_to_page_range(struct mm_struct *mm, unsigned long addr, |
| 2516 | unsigned long size, pte_fn_t fn, |
| 2517 | void *data, bool create) |
| 2518 | { |
| 2519 | pgd_t *pgd; |
Joerg Roedel | e80d390 | 2020-09-04 16:35:43 -0700 | [diff] [blame] | 2520 | unsigned long start = addr, next; |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2521 | unsigned long end = addr + size; |
Joerg Roedel | e80d390 | 2020-09-04 16:35:43 -0700 | [diff] [blame] | 2522 | pgtbl_mod_mask mask = 0; |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2523 | int err = 0; |
| 2524 | |
| 2525 | if (WARN_ON(addr >= end)) |
| 2526 | return -EINVAL; |
| 2527 | |
| 2528 | pgd = pgd_offset(mm, addr); |
| 2529 | do { |
| 2530 | next = pgd_addr_end(addr, end); |
| 2531 | if (!create && pgd_none_or_clear_bad(pgd)) |
| 2532 | continue; |
Joerg Roedel | e80d390 | 2020-09-04 16:35:43 -0700 | [diff] [blame] | 2533 | err = apply_to_p4d_range(mm, pgd, addr, next, fn, data, create, &mask); |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2534 | if (err) |
| 2535 | break; |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2536 | } while (pgd++, addr = next, addr != end); |
| 2537 | |
Joerg Roedel | e80d390 | 2020-09-04 16:35:43 -0700 | [diff] [blame] | 2538 | if (mask & ARCH_PAGE_TABLE_SYNC_MASK) |
| 2539 | arch_sync_kernel_mappings(start, start + size); |
| 2540 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2541 | return err; |
| 2542 | } |
| 2543 | |
Jeremy Fitzhardinge | aee16b3 | 2007-05-06 14:48:54 -0700 | [diff] [blame] | 2544 | /* |
| 2545 | * Scan a region of virtual memory, filling in page tables as necessary |
| 2546 | * and calling a provided function on each leaf page table. |
| 2547 | */ |
| 2548 | int apply_to_page_range(struct mm_struct *mm, unsigned long addr, |
| 2549 | unsigned long size, pte_fn_t fn, void *data) |
| 2550 | { |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2551 | return __apply_to_page_range(mm, addr, size, fn, data, true); |
Jeremy Fitzhardinge | aee16b3 | 2007-05-06 14:48:54 -0700 | [diff] [blame] | 2552 | } |
| 2553 | EXPORT_SYMBOL_GPL(apply_to_page_range); |
| 2554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2555 | /* |
Daniel Axtens | be1db47 | 2019-12-17 20:51:41 -0800 | [diff] [blame] | 2556 | * Scan a region of virtual memory, calling a provided function on |
| 2557 | * each leaf page table where it exists. |
| 2558 | * |
| 2559 | * Unlike apply_to_page_range, this does _not_ fill in page tables |
| 2560 | * where they are absent. |
| 2561 | */ |
| 2562 | int apply_to_existing_page_range(struct mm_struct *mm, unsigned long addr, |
| 2563 | unsigned long size, pte_fn_t fn, void *data) |
| 2564 | { |
| 2565 | return __apply_to_page_range(mm, addr, size, fn, data, false); |
| 2566 | } |
| 2567 | EXPORT_SYMBOL_GPL(apply_to_existing_page_range); |
| 2568 | |
| 2569 | /* |
Kirill A. Shutemov | 9b4bdd2 | 2015-02-10 14:09:51 -0800 | [diff] [blame] | 2570 | * handle_pte_fault chooses page fault handler according to an entry which was |
| 2571 | * read non-atomically. Before making any commitment, on those architectures |
| 2572 | * or configurations (e.g. i386 with PAE) which might give a mix of unmatched |
| 2573 | * parts, do_swap_page must check under lock before unmapping the pte and |
| 2574 | * proceeding (but do_wp_page is only called after already making such a check; |
Ryota Ozaki | a335b2e | 2011-02-10 13:56:28 +0900 | [diff] [blame] | 2575 | * and do_anonymous_page can safely check later on). |
Hugh Dickins | 8f4e210 | 2005-10-29 18:16:26 -0700 | [diff] [blame] | 2576 | */ |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 2577 | static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, |
Hugh Dickins | 8f4e210 | 2005-10-29 18:16:26 -0700 | [diff] [blame] | 2578 | pte_t *page_table, pte_t orig_pte) |
| 2579 | { |
| 2580 | int same = 1; |
Thomas Gleixner | 923717c | 2019-10-15 21:18:12 +0200 | [diff] [blame] | 2581 | #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPTION) |
Hugh Dickins | 8f4e210 | 2005-10-29 18:16:26 -0700 | [diff] [blame] | 2582 | if (sizeof(pte_t) > sizeof(unsigned long)) { |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 2583 | spinlock_t *ptl = pte_lockptr(mm, pmd); |
| 2584 | spin_lock(ptl); |
Hugh Dickins | 8f4e210 | 2005-10-29 18:16:26 -0700 | [diff] [blame] | 2585 | same = pte_same(*page_table, orig_pte); |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 2586 | spin_unlock(ptl); |
Hugh Dickins | 8f4e210 | 2005-10-29 18:16:26 -0700 | [diff] [blame] | 2587 | } |
| 2588 | #endif |
| 2589 | pte_unmap(page_table); |
| 2590 | return same; |
| 2591 | } |
| 2592 | |
Jia He | 83d116c | 2019-10-11 22:09:39 +0800 | [diff] [blame] | 2593 | static inline bool cow_user_page(struct page *dst, struct page *src, |
| 2594 | struct vm_fault *vmf) |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 2595 | { |
Jia He | 83d116c | 2019-10-11 22:09:39 +0800 | [diff] [blame] | 2596 | bool ret; |
| 2597 | void *kaddr; |
| 2598 | void __user *uaddr; |
Kirill A. Shutemov | c3e5ea6 | 2020-03-05 22:28:32 -0800 | [diff] [blame] | 2599 | bool locked = false; |
Jia He | 83d116c | 2019-10-11 22:09:39 +0800 | [diff] [blame] | 2600 | struct vm_area_struct *vma = vmf->vma; |
| 2601 | struct mm_struct *mm = vma->vm_mm; |
| 2602 | unsigned long addr = vmf->address; |
| 2603 | |
Jia He | 83d116c | 2019-10-11 22:09:39 +0800 | [diff] [blame] | 2604 | if (likely(src)) { |
| 2605 | copy_user_highpage(dst, src, addr, vma); |
| 2606 | return true; |
| 2607 | } |
| 2608 | |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 2609 | /* |
| 2610 | * If the source page was a PFN mapping, we don't have |
| 2611 | * a "struct page" for it. We do a best-effort copy by |
| 2612 | * just copying from the original user address. If that |
| 2613 | * fails, we just zero-fill it. Live with it. |
| 2614 | */ |
Jia He | 83d116c | 2019-10-11 22:09:39 +0800 | [diff] [blame] | 2615 | kaddr = kmap_atomic(dst); |
| 2616 | uaddr = (void __user *)(addr & PAGE_MASK); |
Linus Torvalds | 5d2a2dbbc | 2005-11-29 14:07:55 -0800 | [diff] [blame] | 2617 | |
Jia He | 83d116c | 2019-10-11 22:09:39 +0800 | [diff] [blame] | 2618 | /* |
| 2619 | * On architectures with software "accessed" bits, we would |
| 2620 | * take a double page fault, so mark it accessed here. |
| 2621 | */ |
Kirill A. Shutemov | c3e5ea6 | 2020-03-05 22:28:32 -0800 | [diff] [blame] | 2622 | if (arch_faults_on_old_pte() && !pte_young(vmf->orig_pte)) { |
Jia He | 83d116c | 2019-10-11 22:09:39 +0800 | [diff] [blame] | 2623 | pte_t entry; |
| 2624 | |
| 2625 | vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl); |
Kirill A. Shutemov | c3e5ea6 | 2020-03-05 22:28:32 -0800 | [diff] [blame] | 2626 | locked = true; |
Jia He | 83d116c | 2019-10-11 22:09:39 +0800 | [diff] [blame] | 2627 | if (!likely(pte_same(*vmf->pte, vmf->orig_pte))) { |
| 2628 | /* |
| 2629 | * Other thread has already handled the fault |
Bibo Mao | 7df6769 | 2020-05-27 10:25:18 +0800 | [diff] [blame] | 2630 | * and update local tlb only |
Jia He | 83d116c | 2019-10-11 22:09:39 +0800 | [diff] [blame] | 2631 | */ |
Bibo Mao | 7df6769 | 2020-05-27 10:25:18 +0800 | [diff] [blame] | 2632 | update_mmu_tlb(vma, addr, vmf->pte); |
Jia He | 83d116c | 2019-10-11 22:09:39 +0800 | [diff] [blame] | 2633 | ret = false; |
| 2634 | goto pte_unlock; |
| 2635 | } |
| 2636 | |
| 2637 | entry = pte_mkyoung(vmf->orig_pte); |
| 2638 | if (ptep_set_access_flags(vma, addr, vmf->pte, entry, 0)) |
| 2639 | update_mmu_cache(vma, addr, vmf->pte); |
| 2640 | } |
| 2641 | |
| 2642 | /* |
| 2643 | * This really shouldn't fail, because the page is there |
| 2644 | * in the page tables. But it might just be unreadable, |
| 2645 | * in which case we just give up and fill the result with |
| 2646 | * zeroes. |
| 2647 | */ |
| 2648 | if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) { |
Kirill A. Shutemov | c3e5ea6 | 2020-03-05 22:28:32 -0800 | [diff] [blame] | 2649 | if (locked) |
| 2650 | goto warn; |
| 2651 | |
| 2652 | /* Re-validate under PTL if the page is still mapped */ |
| 2653 | vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl); |
| 2654 | locked = true; |
| 2655 | if (!likely(pte_same(*vmf->pte, vmf->orig_pte))) { |
Bibo Mao | 7df6769 | 2020-05-27 10:25:18 +0800 | [diff] [blame] | 2656 | /* The PTE changed under us, update local tlb */ |
| 2657 | update_mmu_tlb(vma, addr, vmf->pte); |
Kirill A. Shutemov | c3e5ea6 | 2020-03-05 22:28:32 -0800 | [diff] [blame] | 2658 | ret = false; |
| 2659 | goto pte_unlock; |
| 2660 | } |
| 2661 | |
Linus Torvalds | 5d2a2dbbc | 2005-11-29 14:07:55 -0800 | [diff] [blame] | 2662 | /* |
Ethon Paul | 985ba00 | 2020-06-04 16:49:43 -0700 | [diff] [blame] | 2663 | * The same page can be mapped back since last copy attempt. |
Kirill A. Shutemov | c3e5ea6 | 2020-03-05 22:28:32 -0800 | [diff] [blame] | 2664 | * Try to copy again under PTL. |
Linus Torvalds | 5d2a2dbbc | 2005-11-29 14:07:55 -0800 | [diff] [blame] | 2665 | */ |
Kirill A. Shutemov | c3e5ea6 | 2020-03-05 22:28:32 -0800 | [diff] [blame] | 2666 | if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) { |
| 2667 | /* |
| 2668 | * Give a warn in case there can be some obscure |
| 2669 | * use-case |
| 2670 | */ |
| 2671 | warn: |
| 2672 | WARN_ON_ONCE(1); |
| 2673 | clear_page(kaddr); |
| 2674 | } |
Jia He | 83d116c | 2019-10-11 22:09:39 +0800 | [diff] [blame] | 2675 | } |
| 2676 | |
| 2677 | ret = true; |
| 2678 | |
| 2679 | pte_unlock: |
Kirill A. Shutemov | c3e5ea6 | 2020-03-05 22:28:32 -0800 | [diff] [blame] | 2680 | if (locked) |
Jia He | 83d116c | 2019-10-11 22:09:39 +0800 | [diff] [blame] | 2681 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
| 2682 | kunmap_atomic(kaddr); |
| 2683 | flush_dcache_page(dst); |
| 2684 | |
| 2685 | return ret; |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 2686 | } |
| 2687 | |
Michal Hocko | c20cd45 | 2016-01-14 15:20:12 -0800 | [diff] [blame] | 2688 | static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma) |
| 2689 | { |
| 2690 | struct file *vm_file = vma->vm_file; |
| 2691 | |
| 2692 | if (vm_file) |
| 2693 | return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO; |
| 2694 | |
| 2695 | /* |
| 2696 | * Special mappings (e.g. VDSO) do not have any file so fake |
| 2697 | * a default GFP_KERNEL for them. |
| 2698 | */ |
| 2699 | return GFP_KERNEL; |
| 2700 | } |
| 2701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | /* |
Kirill A. Shutemov | fb09a46 | 2014-04-03 14:48:15 -0700 | [diff] [blame] | 2703 | * Notify the address space that the page is about to become writable so that |
| 2704 | * it can prohibit this or wait for the page to get into an appropriate state. |
| 2705 | * |
| 2706 | * We do this without the lock held, so that it can sleep if it needs to. |
| 2707 | */ |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 2708 | static vm_fault_t do_page_mkwrite(struct vm_fault *vmf) |
Kirill A. Shutemov | fb09a46 | 2014-04-03 14:48:15 -0700 | [diff] [blame] | 2709 | { |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 2710 | vm_fault_t ret; |
Jan Kara | 38b8cb7 | 2016-12-14 15:07:30 -0800 | [diff] [blame] | 2711 | struct page *page = vmf->page; |
| 2712 | unsigned int old_flags = vmf->flags; |
Kirill A. Shutemov | fb09a46 | 2014-04-03 14:48:15 -0700 | [diff] [blame] | 2713 | |
Jan Kara | 38b8cb7 | 2016-12-14 15:07:30 -0800 | [diff] [blame] | 2714 | vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; |
Kirill A. Shutemov | fb09a46 | 2014-04-03 14:48:15 -0700 | [diff] [blame] | 2715 | |
Darrick J. Wong | dc617f2 | 2019-08-20 07:55:16 -0700 | [diff] [blame] | 2716 | if (vmf->vma->vm_file && |
| 2717 | IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host)) |
| 2718 | return VM_FAULT_SIGBUS; |
| 2719 | |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 2720 | ret = vmf->vma->vm_ops->page_mkwrite(vmf); |
Jan Kara | 38b8cb7 | 2016-12-14 15:07:30 -0800 | [diff] [blame] | 2721 | /* Restore original flags so that caller is not surprised */ |
| 2722 | vmf->flags = old_flags; |
Kirill A. Shutemov | fb09a46 | 2014-04-03 14:48:15 -0700 | [diff] [blame] | 2723 | if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) |
| 2724 | return ret; |
| 2725 | if (unlikely(!(ret & VM_FAULT_LOCKED))) { |
| 2726 | lock_page(page); |
| 2727 | if (!page->mapping) { |
| 2728 | unlock_page(page); |
| 2729 | return 0; /* retry */ |
| 2730 | } |
| 2731 | ret |= VM_FAULT_LOCKED; |
| 2732 | } else |
| 2733 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 2734 | return ret; |
| 2735 | } |
| 2736 | |
| 2737 | /* |
Jan Kara | 97ba0c2 | 2016-12-14 15:07:27 -0800 | [diff] [blame] | 2738 | * Handle dirtying of a page in shared file mapping on a write fault. |
| 2739 | * |
| 2740 | * The function expects the page to be locked and unlocks it. |
| 2741 | */ |
Johannes Weiner | 89b1533 | 2019-11-30 17:50:22 -0800 | [diff] [blame] | 2742 | static vm_fault_t fault_dirty_shared_page(struct vm_fault *vmf) |
Jan Kara | 97ba0c2 | 2016-12-14 15:07:27 -0800 | [diff] [blame] | 2743 | { |
Johannes Weiner | 89b1533 | 2019-11-30 17:50:22 -0800 | [diff] [blame] | 2744 | struct vm_area_struct *vma = vmf->vma; |
Jan Kara | 97ba0c2 | 2016-12-14 15:07:27 -0800 | [diff] [blame] | 2745 | struct address_space *mapping; |
Johannes Weiner | 89b1533 | 2019-11-30 17:50:22 -0800 | [diff] [blame] | 2746 | struct page *page = vmf->page; |
Jan Kara | 97ba0c2 | 2016-12-14 15:07:27 -0800 | [diff] [blame] | 2747 | bool dirtied; |
| 2748 | bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite; |
| 2749 | |
| 2750 | dirtied = set_page_dirty(page); |
| 2751 | VM_BUG_ON_PAGE(PageAnon(page), page); |
| 2752 | /* |
| 2753 | * Take a local copy of the address_space - page.mapping may be zeroed |
| 2754 | * by truncate after unlock_page(). The address_space itself remains |
| 2755 | * pinned by vma->vm_file's reference. We rely on unlock_page()'s |
| 2756 | * release semantics to prevent the compiler from undoing this copying. |
| 2757 | */ |
| 2758 | mapping = page_rmapping(page); |
| 2759 | unlock_page(page); |
| 2760 | |
Jan Kara | 97ba0c2 | 2016-12-14 15:07:27 -0800 | [diff] [blame] | 2761 | if (!page_mkwrite) |
| 2762 | file_update_time(vma->vm_file); |
Johannes Weiner | 89b1533 | 2019-11-30 17:50:22 -0800 | [diff] [blame] | 2763 | |
| 2764 | /* |
| 2765 | * Throttle page dirtying rate down to writeback speed. |
| 2766 | * |
| 2767 | * mapping may be NULL here because some device drivers do not |
| 2768 | * set page.mapping but still dirty their pages |
| 2769 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2770 | * Drop the mmap_lock before waiting on IO, if we can. The file |
Johannes Weiner | 89b1533 | 2019-11-30 17:50:22 -0800 | [diff] [blame] | 2771 | * is pinning the mapping, as per above. |
| 2772 | */ |
| 2773 | if ((dirtied || page_mkwrite) && mapping) { |
| 2774 | struct file *fpin; |
| 2775 | |
| 2776 | fpin = maybe_unlock_mmap_for_io(vmf, NULL); |
| 2777 | balance_dirty_pages_ratelimited(mapping); |
| 2778 | if (fpin) { |
| 2779 | fput(fpin); |
| 2780 | return VM_FAULT_RETRY; |
| 2781 | } |
| 2782 | } |
| 2783 | |
| 2784 | return 0; |
Jan Kara | 97ba0c2 | 2016-12-14 15:07:27 -0800 | [diff] [blame] | 2785 | } |
| 2786 | |
| 2787 | /* |
Shachar Raindel | 4e047f8 | 2015-04-14 15:46:25 -0700 | [diff] [blame] | 2788 | * Handle write page faults for pages that can be reused in the current vma |
| 2789 | * |
| 2790 | * This can happen either due to the mapping being with the VM_SHARED flag, |
| 2791 | * or due to us being the last reference standing to the page. In either |
| 2792 | * case, all we need to do here is to mark the page as writable and update |
| 2793 | * any related book-keeping. |
| 2794 | */ |
Jan Kara | 997dd98 | 2016-12-14 15:07:36 -0800 | [diff] [blame] | 2795 | static inline void wp_page_reuse(struct vm_fault *vmf) |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2796 | __releases(vmf->ptl) |
Shachar Raindel | 4e047f8 | 2015-04-14 15:46:25 -0700 | [diff] [blame] | 2797 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2798 | struct vm_area_struct *vma = vmf->vma; |
Jan Kara | a41b70d | 2016-12-14 15:07:33 -0800 | [diff] [blame] | 2799 | struct page *page = vmf->page; |
Shachar Raindel | 4e047f8 | 2015-04-14 15:46:25 -0700 | [diff] [blame] | 2800 | pte_t entry; |
| 2801 | /* |
| 2802 | * Clear the pages cpupid information as the existing |
| 2803 | * information potentially belongs to a now completely |
| 2804 | * unrelated process. |
| 2805 | */ |
| 2806 | if (page) |
| 2807 | page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1); |
| 2808 | |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 2809 | flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); |
| 2810 | entry = pte_mkyoung(vmf->orig_pte); |
Shachar Raindel | 4e047f8 | 2015-04-14 15:46:25 -0700 | [diff] [blame] | 2811 | entry = maybe_mkwrite(pte_mkdirty(entry), vma); |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2812 | if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) |
| 2813 | update_mmu_cache(vma, vmf->address, vmf->pte); |
| 2814 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
Peter Xu | 798a6b8 | 2020-08-21 19:49:58 -0400 | [diff] [blame] | 2815 | count_vm_event(PGREUSE); |
Shachar Raindel | 4e047f8 | 2015-04-14 15:46:25 -0700 | [diff] [blame] | 2816 | } |
| 2817 | |
| 2818 | /* |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2819 | * Handle the case of a page which we actually need to copy to a new page. |
| 2820 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2821 | * Called with mmap_lock locked and the old page referenced, but |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2822 | * without the ptl held. |
| 2823 | * |
| 2824 | * High level logic flow: |
| 2825 | * |
| 2826 | * - Allocate a page, copy the content of the old page to the new one. |
| 2827 | * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc. |
| 2828 | * - Take the PTL. If the pte changed, bail out and release the allocated page |
| 2829 | * - If the pte is still the way we remember it, update the page table and all |
| 2830 | * relevant references. This includes dropping the reference the page-table |
| 2831 | * held to the old page, as well as updating the rmap. |
| 2832 | * - In any case, unlock the PTL and drop the reference we took to the old page. |
| 2833 | */ |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 2834 | static vm_fault_t wp_page_copy(struct vm_fault *vmf) |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2835 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2836 | struct vm_area_struct *vma = vmf->vma; |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 2837 | struct mm_struct *mm = vma->vm_mm; |
Jan Kara | a41b70d | 2016-12-14 15:07:33 -0800 | [diff] [blame] | 2838 | struct page *old_page = vmf->page; |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2839 | struct page *new_page = NULL; |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2840 | pte_t entry; |
| 2841 | int page_copied = 0; |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 2842 | struct mmu_notifier_range range; |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2843 | |
| 2844 | if (unlikely(anon_vma_prepare(vma))) |
| 2845 | goto oom; |
| 2846 | |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 2847 | if (is_zero_pfn(pte_pfn(vmf->orig_pte))) { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2848 | new_page = alloc_zeroed_user_highpage_movable(vma, |
| 2849 | vmf->address); |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2850 | if (!new_page) |
| 2851 | goto oom; |
| 2852 | } else { |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 2853 | new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2854 | vmf->address); |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2855 | if (!new_page) |
| 2856 | goto oom; |
Jia He | 83d116c | 2019-10-11 22:09:39 +0800 | [diff] [blame] | 2857 | |
| 2858 | if (!cow_user_page(new_page, old_page, vmf)) { |
| 2859 | /* |
| 2860 | * COW failed, if the fault was solved by other, |
| 2861 | * it's fine. If not, userspace would re-fault on |
| 2862 | * the same address and we will handle the fault |
| 2863 | * from the second attempt. |
| 2864 | */ |
| 2865 | put_page(new_page); |
| 2866 | if (old_page) |
| 2867 | put_page(old_page); |
| 2868 | return 0; |
| 2869 | } |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2870 | } |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2871 | |
Johannes Weiner | d9eb1ea | 2020-06-03 16:02:24 -0700 | [diff] [blame] | 2872 | if (mem_cgroup_charge(new_page, mm, GFP_KERNEL)) |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2873 | goto oom_free_new; |
Johannes Weiner | 9d82c69 | 2020-06-03 16:02:04 -0700 | [diff] [blame] | 2874 | cgroup_throttle_swaprate(new_page, GFP_KERNEL); |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2875 | |
Mel Gorman | eb3c24f | 2015-06-24 16:57:27 -0700 | [diff] [blame] | 2876 | __SetPageUptodate(new_page); |
| 2877 | |
Jérôme Glisse | 7269f99 | 2019-05-13 17:20:53 -0700 | [diff] [blame] | 2878 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, |
Jérôme Glisse | 6f4f13e | 2019-05-13 17:20:49 -0700 | [diff] [blame] | 2879 | vmf->address & PAGE_MASK, |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 2880 | (vmf->address & PAGE_MASK) + PAGE_SIZE); |
| 2881 | mmu_notifier_invalidate_range_start(&range); |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2882 | |
| 2883 | /* |
| 2884 | * Re-check the pte - we dropped the lock |
| 2885 | */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2886 | vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl); |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 2887 | if (likely(pte_same(*vmf->pte, vmf->orig_pte))) { |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2888 | if (old_page) { |
| 2889 | if (!PageAnon(old_page)) { |
Jerome Marchand | eca56ff | 2016-01-14 15:19:26 -0800 | [diff] [blame] | 2890 | dec_mm_counter_fast(mm, |
| 2891 | mm_counter_file(old_page)); |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2892 | inc_mm_counter_fast(mm, MM_ANONPAGES); |
| 2893 | } |
| 2894 | } else { |
| 2895 | inc_mm_counter_fast(mm, MM_ANONPAGES); |
| 2896 | } |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 2897 | flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2898 | entry = mk_pte(new_page, vma->vm_page_prot); |
| 2899 | entry = maybe_mkwrite(pte_mkdirty(entry), vma); |
Nicholas Piggin | 111fe71 | 2020-12-29 15:14:43 -0800 | [diff] [blame] | 2900 | |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2901 | /* |
| 2902 | * Clear the pte entry and flush it first, before updating the |
Nicholas Piggin | 111fe71 | 2020-12-29 15:14:43 -0800 | [diff] [blame] | 2903 | * pte with the new entry, to keep TLBs on different CPUs in |
| 2904 | * sync. This code used to set the new PTE then flush TLBs, but |
| 2905 | * that left a window where the new PTE could be loaded into |
| 2906 | * some TLBs while the old PTE remains in others. |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2907 | */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2908 | ptep_clear_flush_notify(vma, vmf->address, vmf->pte); |
| 2909 | page_add_new_anon_rmap(new_page, vma, vmf->address, false); |
Joonsoo Kim | b518154 | 2020-08-11 18:30:40 -0700 | [diff] [blame] | 2910 | lru_cache_add_inactive_or_unevictable(new_page, vma); |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2911 | /* |
| 2912 | * We call the notify macro here because, when using secondary |
| 2913 | * mmu page tables (such as kvm shadow page tables), we want the |
| 2914 | * new page to be mapped directly into the secondary page table. |
| 2915 | */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2916 | set_pte_at_notify(mm, vmf->address, vmf->pte, entry); |
| 2917 | update_mmu_cache(vma, vmf->address, vmf->pte); |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2918 | if (old_page) { |
| 2919 | /* |
| 2920 | * Only after switching the pte to the new page may |
| 2921 | * we remove the mapcount here. Otherwise another |
| 2922 | * process may come and find the rmap count decremented |
| 2923 | * before the pte is switched to the new page, and |
| 2924 | * "reuse" the old page writing into it while our pte |
| 2925 | * here still points into it and can be read by other |
| 2926 | * threads. |
| 2927 | * |
| 2928 | * The critical issue is to order this |
| 2929 | * page_remove_rmap with the ptp_clear_flush above. |
| 2930 | * Those stores are ordered by (if nothing else,) |
| 2931 | * the barrier present in the atomic_add_negative |
| 2932 | * in page_remove_rmap. |
| 2933 | * |
| 2934 | * Then the TLB flush in ptep_clear_flush ensures that |
| 2935 | * no process can access the old page before the |
| 2936 | * decremented mapcount is visible. And the old page |
| 2937 | * cannot be reused until after the decremented |
| 2938 | * mapcount is visible. So transitively, TLBs to |
| 2939 | * old page will be flushed before it can be reused. |
| 2940 | */ |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 2941 | page_remove_rmap(old_page, false); |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2942 | } |
| 2943 | |
| 2944 | /* Free the old page.. */ |
| 2945 | new_page = old_page; |
| 2946 | page_copied = 1; |
| 2947 | } else { |
Bibo Mao | 7df6769 | 2020-05-27 10:25:18 +0800 | [diff] [blame] | 2948 | update_mmu_tlb(vma, vmf->address, vmf->pte); |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2949 | } |
| 2950 | |
| 2951 | if (new_page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2952 | put_page(new_page); |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2953 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2954 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
Jérôme Glisse | 4645b9f | 2017-11-15 17:34:11 -0800 | [diff] [blame] | 2955 | /* |
| 2956 | * No need to double call mmu_notifier->invalidate_range() callback as |
| 2957 | * the above ptep_clear_flush_notify() did already call it. |
| 2958 | */ |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 2959 | mmu_notifier_invalidate_range_only_end(&range); |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2960 | if (old_page) { |
| 2961 | /* |
| 2962 | * Don't let another task, with possibly unlocked vma, |
| 2963 | * keep the mlocked page. |
| 2964 | */ |
| 2965 | if (page_copied && (vma->vm_flags & VM_LOCKED)) { |
| 2966 | lock_page(old_page); /* LRU manipulation */ |
Kirill A. Shutemov | e90309c | 2016-01-15 16:54:33 -0800 | [diff] [blame] | 2967 | if (PageMlocked(old_page)) |
| 2968 | munlock_vma_page(old_page); |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2969 | unlock_page(old_page); |
| 2970 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2971 | put_page(old_page); |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2972 | } |
| 2973 | return page_copied ? VM_FAULT_WRITE : 0; |
| 2974 | oom_free_new: |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2975 | put_page(new_page); |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2976 | oom: |
| 2977 | if (old_page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2978 | put_page(old_page); |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 2979 | return VM_FAULT_OOM; |
| 2980 | } |
| 2981 | |
Jan Kara | 66a6197 | 2016-12-14 15:07:39 -0800 | [diff] [blame] | 2982 | /** |
| 2983 | * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE |
| 2984 | * writeable once the page is prepared |
| 2985 | * |
| 2986 | * @vmf: structure describing the fault |
| 2987 | * |
| 2988 | * This function handles all that is needed to finish a write page fault in a |
| 2989 | * shared mapping due to PTE being read-only once the mapped page is prepared. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2990 | * It handles locking of PTE and modifying it. |
Jan Kara | 66a6197 | 2016-12-14 15:07:39 -0800 | [diff] [blame] | 2991 | * |
| 2992 | * The function expects the page to be locked or other protection against |
| 2993 | * concurrent faults / writeback (such as DAX radix tree locks). |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2994 | * |
| 2995 | * Return: %VM_FAULT_WRITE on success, %0 when PTE got changed before |
| 2996 | * we acquired PTE lock. |
Jan Kara | 66a6197 | 2016-12-14 15:07:39 -0800 | [diff] [blame] | 2997 | */ |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 2998 | vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf) |
Jan Kara | 66a6197 | 2016-12-14 15:07:39 -0800 | [diff] [blame] | 2999 | { |
| 3000 | WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED)); |
| 3001 | vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address, |
| 3002 | &vmf->ptl); |
| 3003 | /* |
| 3004 | * We might have raced with another page fault while we released the |
| 3005 | * pte_offset_map_lock. |
| 3006 | */ |
| 3007 | if (!pte_same(*vmf->pte, vmf->orig_pte)) { |
Bibo Mao | 7df6769 | 2020-05-27 10:25:18 +0800 | [diff] [blame] | 3008 | update_mmu_tlb(vmf->vma, vmf->address, vmf->pte); |
Jan Kara | 66a6197 | 2016-12-14 15:07:39 -0800 | [diff] [blame] | 3009 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
Jan Kara | a19e255 | 2016-12-14 15:07:42 -0800 | [diff] [blame] | 3010 | return VM_FAULT_NOPAGE; |
Jan Kara | 66a6197 | 2016-12-14 15:07:39 -0800 | [diff] [blame] | 3011 | } |
| 3012 | wp_page_reuse(vmf); |
Jan Kara | a19e255 | 2016-12-14 15:07:42 -0800 | [diff] [blame] | 3013 | return 0; |
Jan Kara | 66a6197 | 2016-12-14 15:07:39 -0800 | [diff] [blame] | 3014 | } |
| 3015 | |
Boaz Harrosh | dd90618 | 2015-04-15 16:15:11 -0700 | [diff] [blame] | 3016 | /* |
| 3017 | * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED |
| 3018 | * mapping |
| 3019 | */ |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3020 | static vm_fault_t wp_pfn_shared(struct vm_fault *vmf) |
Boaz Harrosh | dd90618 | 2015-04-15 16:15:11 -0700 | [diff] [blame] | 3021 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3022 | struct vm_area_struct *vma = vmf->vma; |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 3023 | |
Boaz Harrosh | dd90618 | 2015-04-15 16:15:11 -0700 | [diff] [blame] | 3024 | if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) { |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3025 | vm_fault_t ret; |
Boaz Harrosh | dd90618 | 2015-04-15 16:15:11 -0700 | [diff] [blame] | 3026 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3027 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
Jan Kara | fe82221 | 2016-12-14 15:07:13 -0800 | [diff] [blame] | 3028 | vmf->flags |= FAULT_FLAG_MKWRITE; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 3029 | ret = vma->vm_ops->pfn_mkwrite(vmf); |
Jan Kara | 2f89dc1 | 2016-12-14 15:07:50 -0800 | [diff] [blame] | 3030 | if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)) |
Boaz Harrosh | dd90618 | 2015-04-15 16:15:11 -0700 | [diff] [blame] | 3031 | return ret; |
Jan Kara | 66a6197 | 2016-12-14 15:07:39 -0800 | [diff] [blame] | 3032 | return finish_mkwrite_fault(vmf); |
Boaz Harrosh | dd90618 | 2015-04-15 16:15:11 -0700 | [diff] [blame] | 3033 | } |
Jan Kara | 997dd98 | 2016-12-14 15:07:36 -0800 | [diff] [blame] | 3034 | wp_page_reuse(vmf); |
| 3035 | return VM_FAULT_WRITE; |
Boaz Harrosh | dd90618 | 2015-04-15 16:15:11 -0700 | [diff] [blame] | 3036 | } |
| 3037 | |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3038 | static vm_fault_t wp_page_shared(struct vm_fault *vmf) |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3039 | __releases(vmf->ptl) |
Shachar Raindel | 93e478d | 2015-04-14 15:46:35 -0700 | [diff] [blame] | 3040 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3041 | struct vm_area_struct *vma = vmf->vma; |
Johannes Weiner | 89b1533 | 2019-11-30 17:50:22 -0800 | [diff] [blame] | 3042 | vm_fault_t ret = VM_FAULT_WRITE; |
Shachar Raindel | 93e478d | 2015-04-14 15:46:35 -0700 | [diff] [blame] | 3043 | |
Jan Kara | a41b70d | 2016-12-14 15:07:33 -0800 | [diff] [blame] | 3044 | get_page(vmf->page); |
Shachar Raindel | 93e478d | 2015-04-14 15:46:35 -0700 | [diff] [blame] | 3045 | |
Shachar Raindel | 93e478d | 2015-04-14 15:46:35 -0700 | [diff] [blame] | 3046 | if (vma->vm_ops && vma->vm_ops->page_mkwrite) { |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3047 | vm_fault_t tmp; |
Shachar Raindel | 93e478d | 2015-04-14 15:46:35 -0700 | [diff] [blame] | 3048 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3049 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
Jan Kara | 38b8cb7 | 2016-12-14 15:07:30 -0800 | [diff] [blame] | 3050 | tmp = do_page_mkwrite(vmf); |
Shachar Raindel | 93e478d | 2015-04-14 15:46:35 -0700 | [diff] [blame] | 3051 | if (unlikely(!tmp || (tmp & |
| 3052 | (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { |
Jan Kara | a41b70d | 2016-12-14 15:07:33 -0800 | [diff] [blame] | 3053 | put_page(vmf->page); |
Shachar Raindel | 93e478d | 2015-04-14 15:46:35 -0700 | [diff] [blame] | 3054 | return tmp; |
| 3055 | } |
Jan Kara | 66a6197 | 2016-12-14 15:07:39 -0800 | [diff] [blame] | 3056 | tmp = finish_mkwrite_fault(vmf); |
Jan Kara | a19e255 | 2016-12-14 15:07:42 -0800 | [diff] [blame] | 3057 | if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { |
Jan Kara | a41b70d | 2016-12-14 15:07:33 -0800 | [diff] [blame] | 3058 | unlock_page(vmf->page); |
Jan Kara | a41b70d | 2016-12-14 15:07:33 -0800 | [diff] [blame] | 3059 | put_page(vmf->page); |
Jan Kara | 66a6197 | 2016-12-14 15:07:39 -0800 | [diff] [blame] | 3060 | return tmp; |
Shachar Raindel | 93e478d | 2015-04-14 15:46:35 -0700 | [diff] [blame] | 3061 | } |
Jan Kara | 66a6197 | 2016-12-14 15:07:39 -0800 | [diff] [blame] | 3062 | } else { |
| 3063 | wp_page_reuse(vmf); |
Jan Kara | 997dd98 | 2016-12-14 15:07:36 -0800 | [diff] [blame] | 3064 | lock_page(vmf->page); |
Shachar Raindel | 93e478d | 2015-04-14 15:46:35 -0700 | [diff] [blame] | 3065 | } |
Johannes Weiner | 89b1533 | 2019-11-30 17:50:22 -0800 | [diff] [blame] | 3066 | ret |= fault_dirty_shared_page(vmf); |
Jan Kara | 997dd98 | 2016-12-14 15:07:36 -0800 | [diff] [blame] | 3067 | put_page(vmf->page); |
Shachar Raindel | 93e478d | 2015-04-14 15:46:35 -0700 | [diff] [blame] | 3068 | |
Johannes Weiner | 89b1533 | 2019-11-30 17:50:22 -0800 | [diff] [blame] | 3069 | return ret; |
Shachar Raindel | 93e478d | 2015-04-14 15:46:35 -0700 | [diff] [blame] | 3070 | } |
| 3071 | |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 3072 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | * This routine handles present pages, when users try to write |
| 3074 | * to a shared page. It is done by copying the page to a new address |
| 3075 | * and decrementing the shared-page counter for the old page. |
| 3076 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3077 | * Note that this routine assumes that the protection checks have been |
| 3078 | * done by the caller (the low-level page fault routine in most cases). |
| 3079 | * Thus we can safely just mark it writable once we've done any necessary |
| 3080 | * COW. |
| 3081 | * |
| 3082 | * We also mark the page dirty at this point even though the page will |
| 3083 | * change only once the write actually happens. This avoids a few races, |
| 3084 | * and potentially makes it more efficient. |
| 3085 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 3086 | * We enter with non-exclusive mmap_lock (to exclude vma changes, |
Hugh Dickins | 8f4e210 | 2005-10-29 18:16:26 -0700 | [diff] [blame] | 3087 | * but allow concurrent faults), with pte both mapped and locked. |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 3088 | * We return with mmap_lock still held, but pte unmapped and unlocked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | */ |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3090 | static vm_fault_t do_wp_page(struct vm_fault *vmf) |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3091 | __releases(vmf->ptl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3093 | struct vm_area_struct *vma = vmf->vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | |
Peter Xu | 292924b | 2020-04-06 20:05:49 -0700 | [diff] [blame] | 3095 | if (userfaultfd_pte_wp(vma, *vmf->pte)) { |
Andrea Arcangeli | 529b930 | 2020-04-06 20:05:29 -0700 | [diff] [blame] | 3096 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
| 3097 | return handle_userfault(vmf, VM_UFFD_WP); |
| 3098 | } |
| 3099 | |
Nadav Amit | 6ce6442 | 2021-03-12 21:08:17 -0800 | [diff] [blame] | 3100 | /* |
| 3101 | * Userfaultfd write-protect can defer flushes. Ensure the TLB |
| 3102 | * is flushed in this case before copying. |
| 3103 | */ |
| 3104 | if (unlikely(userfaultfd_wp(vmf->vma) && |
| 3105 | mm_tlb_flush_pending(vmf->vma->vm_mm))) |
| 3106 | flush_tlb_page(vmf->vma, vmf->address); |
| 3107 | |
Jan Kara | a41b70d | 2016-12-14 15:07:33 -0800 | [diff] [blame] | 3108 | vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte); |
| 3109 | if (!vmf->page) { |
Peter Zijlstra | 251b97f | 2008-07-04 09:59:24 -0700 | [diff] [blame] | 3110 | /* |
Peter Feiner | 64e45507 | 2014-10-13 15:55:46 -0700 | [diff] [blame] | 3111 | * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a |
| 3112 | * VM_PFNMAP VMA. |
Peter Zijlstra | 251b97f | 2008-07-04 09:59:24 -0700 | [diff] [blame] | 3113 | * |
| 3114 | * We should not cow pages in a shared writeable mapping. |
Boaz Harrosh | dd90618 | 2015-04-15 16:15:11 -0700 | [diff] [blame] | 3115 | * Just mark the pages writable and/or call ops->pfn_mkwrite. |
Peter Zijlstra | 251b97f | 2008-07-04 09:59:24 -0700 | [diff] [blame] | 3116 | */ |
| 3117 | if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) == |
| 3118 | (VM_WRITE|VM_SHARED)) |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 3119 | return wp_pfn_shared(vmf); |
Shachar Raindel | 2f38ab2 | 2015-04-14 15:46:32 -0700 | [diff] [blame] | 3120 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3121 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
Jan Kara | a41b70d | 2016-12-14 15:07:33 -0800 | [diff] [blame] | 3122 | return wp_page_copy(vmf); |
Peter Zijlstra | 251b97f | 2008-07-04 09:59:24 -0700 | [diff] [blame] | 3123 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3124 | |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 3125 | /* |
Peter Zijlstra | ee6a645 | 2006-09-25 23:31:00 -0700 | [diff] [blame] | 3126 | * Take out anonymous pages first, anonymous shared vmas are |
| 3127 | * not dirty accountable. |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 3128 | */ |
Kirill Tkhai | 52d1e60 | 2019-03-05 15:43:06 -0800 | [diff] [blame] | 3129 | if (PageAnon(vmf->page)) { |
Linus Torvalds | 09854ba | 2020-08-21 19:49:55 -0400 | [diff] [blame] | 3130 | struct page *page = vmf->page; |
| 3131 | |
| 3132 | /* PageKsm() doesn't necessarily raise the page refcount */ |
| 3133 | if (PageKsm(page) || page_count(page) != 1) |
Kirill Tkhai | 52d1e60 | 2019-03-05 15:43:06 -0800 | [diff] [blame] | 3134 | goto copy; |
Linus Torvalds | 09854ba | 2020-08-21 19:49:55 -0400 | [diff] [blame] | 3135 | if (!trylock_page(page)) |
| 3136 | goto copy; |
| 3137 | if (PageKsm(page) || page_mapcount(page) != 1 || page_count(page) != 1) { |
| 3138 | unlock_page(page); |
| 3139 | goto copy; |
Peter Zijlstra | ee6a645 | 2006-09-25 23:31:00 -0700 | [diff] [blame] | 3140 | } |
Linus Torvalds | 09854ba | 2020-08-21 19:49:55 -0400 | [diff] [blame] | 3141 | /* |
| 3142 | * Ok, we've got the only map reference, and the only |
| 3143 | * page count reference, and the page is locked, |
| 3144 | * it's dark out, and we're wearing sunglasses. Hit it. |
| 3145 | */ |
Linus Torvalds | 09854ba | 2020-08-21 19:49:55 -0400 | [diff] [blame] | 3146 | unlock_page(page); |
Linus Torvalds | be068f2 | 2020-09-24 08:41:32 -0700 | [diff] [blame] | 3147 | wp_page_reuse(vmf); |
Linus Torvalds | 09854ba | 2020-08-21 19:49:55 -0400 | [diff] [blame] | 3148 | return VM_FAULT_WRITE; |
Peter Zijlstra | ee6a645 | 2006-09-25 23:31:00 -0700 | [diff] [blame] | 3149 | } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 3150 | (VM_WRITE|VM_SHARED))) { |
Jan Kara | a41b70d | 2016-12-14 15:07:33 -0800 | [diff] [blame] | 3151 | return wp_page_shared(vmf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | } |
Kirill Tkhai | 52d1e60 | 2019-03-05 15:43:06 -0800 | [diff] [blame] | 3153 | copy: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | /* |
| 3155 | * Ok, we need to copy. Oh, well.. |
| 3156 | */ |
Jan Kara | a41b70d | 2016-12-14 15:07:33 -0800 | [diff] [blame] | 3157 | get_page(vmf->page); |
Shachar Raindel | 2876680 | 2015-04-14 15:46:29 -0700 | [diff] [blame] | 3158 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3159 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
Jan Kara | a41b70d | 2016-12-14 15:07:33 -0800 | [diff] [blame] | 3160 | return wp_page_copy(vmf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | } |
| 3162 | |
Peter Zijlstra | 97a8941 | 2011-05-24 17:12:04 -0700 | [diff] [blame] | 3163 | static void unmap_mapping_range_vma(struct vm_area_struct *vma, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | unsigned long start_addr, unsigned long end_addr, |
| 3165 | struct zap_details *details) |
| 3166 | { |
Al Viro | f5cc4ee | 2012-03-05 14:14:20 -0500 | [diff] [blame] | 3167 | zap_page_range_single(vma, start_addr, end_addr - start_addr, details); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3168 | } |
| 3169 | |
Davidlohr Bueso | f808c13 | 2017-09-08 16:15:08 -0700 | [diff] [blame] | 3170 | static inline void unmap_mapping_range_tree(struct rb_root_cached *root, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | struct zap_details *details) |
| 3172 | { |
| 3173 | struct vm_area_struct *vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | pgoff_t vba, vea, zba, zea; |
| 3175 | |
Michel Lespinasse | 6b2dbba | 2012-10-08 16:31:25 -0700 | [diff] [blame] | 3176 | vma_interval_tree_foreach(vma, root, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3177 | details->first_index, details->last_index) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | |
| 3179 | vba = vma->vm_pgoff; |
Libin | d6e9321 | 2013-07-03 15:01:26 -0700 | [diff] [blame] | 3180 | vea = vba + vma_pages(vma) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | zba = details->first_index; |
| 3182 | if (zba < vba) |
| 3183 | zba = vba; |
| 3184 | zea = details->last_index; |
| 3185 | if (zea > vea) |
| 3186 | zea = vea; |
| 3187 | |
Peter Zijlstra | 97a8941 | 2011-05-24 17:12:04 -0700 | [diff] [blame] | 3188 | unmap_mapping_range_vma(vma, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3189 | ((zba - vba) << PAGE_SHIFT) + vma->vm_start, |
| 3190 | ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, |
Peter Zijlstra | 97a8941 | 2011-05-24 17:12:04 -0700 | [diff] [blame] | 3191 | details); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3192 | } |
| 3193 | } |
| 3194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3195 | /** |
Matthew Wilcox | 977fbdc | 2018-01-31 16:17:36 -0800 | [diff] [blame] | 3196 | * unmap_mapping_pages() - Unmap pages from processes. |
| 3197 | * @mapping: The address space containing pages to be unmapped. |
| 3198 | * @start: Index of first page to be unmapped. |
| 3199 | * @nr: Number of pages to be unmapped. 0 to unmap to end of file. |
| 3200 | * @even_cows: Whether to unmap even private COWed pages. |
| 3201 | * |
| 3202 | * Unmap the pages in this address space from any userspace process which |
| 3203 | * has them mmaped. Generally, you want to remove COWed pages as well when |
| 3204 | * a file is being truncated, but not when invalidating pages from the page |
| 3205 | * cache. |
| 3206 | */ |
| 3207 | void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, |
| 3208 | pgoff_t nr, bool even_cows) |
| 3209 | { |
| 3210 | struct zap_details details = { }; |
| 3211 | |
| 3212 | details.check_mapping = even_cows ? NULL : mapping; |
| 3213 | details.first_index = start; |
| 3214 | details.last_index = start + nr - 1; |
| 3215 | if (details.last_index < details.first_index) |
| 3216 | details.last_index = ULONG_MAX; |
| 3217 | |
| 3218 | i_mmap_lock_write(mapping); |
| 3219 | if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))) |
| 3220 | unmap_mapping_range_tree(&mapping->i_mmap, &details); |
| 3221 | i_mmap_unlock_write(mapping); |
| 3222 | } |
| 3223 | |
| 3224 | /** |
Kirill A. Shutemov | 8a5f14a | 2015-02-10 14:09:49 -0800 | [diff] [blame] | 3225 | * unmap_mapping_range - unmap the portion of all mmaps in the specified |
Matthew Wilcox | 977fbdc | 2018-01-31 16:17:36 -0800 | [diff] [blame] | 3226 | * address_space corresponding to the specified byte range in the underlying |
Kirill A. Shutemov | 8a5f14a | 2015-02-10 14:09:49 -0800 | [diff] [blame] | 3227 | * file. |
| 3228 | * |
Martin Waitz | 3d41088 | 2005-06-23 22:05:21 -0700 | [diff] [blame] | 3229 | * @mapping: the address space containing mmaps to be unmapped. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3230 | * @holebegin: byte in first page to unmap, relative to the start of |
| 3231 | * the underlying file. This will be rounded down to a PAGE_SIZE |
npiggin@suse.de | 25d9e2d | 2009-08-21 02:35:05 +1000 | [diff] [blame] | 3232 | * boundary. Note that this is different from truncate_pagecache(), which |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3233 | * must keep the partial page. In contrast, we must get rid of |
| 3234 | * partial pages. |
| 3235 | * @holelen: size of prospective hole in bytes. This will be rounded |
| 3236 | * up to a PAGE_SIZE boundary. A holelen of zero truncates to the |
| 3237 | * end of the file. |
| 3238 | * @even_cows: 1 when truncating a file, unmap even private COWed pages; |
| 3239 | * but 0 when invalidating pagecache, don't throw away private data. |
| 3240 | */ |
| 3241 | void unmap_mapping_range(struct address_space *mapping, |
| 3242 | loff_t const holebegin, loff_t const holelen, int even_cows) |
| 3243 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | pgoff_t hba = holebegin >> PAGE_SHIFT; |
| 3245 | pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 3246 | |
| 3247 | /* Check for overflow. */ |
| 3248 | if (sizeof(holelen) > sizeof(hlen)) { |
| 3249 | long long holeend = |
| 3250 | (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 3251 | if (holeend & ~(long long)ULONG_MAX) |
| 3252 | hlen = ULONG_MAX - hba + 1; |
| 3253 | } |
| 3254 | |
Matthew Wilcox | 977fbdc | 2018-01-31 16:17:36 -0800 | [diff] [blame] | 3255 | unmap_mapping_pages(mapping, hba, hlen, even_cows); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3256 | } |
| 3257 | EXPORT_SYMBOL(unmap_mapping_range); |
| 3258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3259 | /* |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 3260 | * We enter with non-exclusive mmap_lock (to exclude vma changes, |
Hugh Dickins | 8f4e210 | 2005-10-29 18:16:26 -0700 | [diff] [blame] | 3261 | * but allow concurrent faults), and pte mapped but not yet locked. |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 3262 | * We return with pte unmapped and unlocked. |
| 3263 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 3264 | * We return with the mmap_lock locked or unlocked in the same cases |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 3265 | * as does filemap_fault(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3266 | */ |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3267 | vm_fault_t do_swap_page(struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3268 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3269 | struct vm_area_struct *vma = vmf->vma; |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 3270 | struct page *page = NULL, *swapcache; |
Hugh Dickins | 65500d2 | 2005-10-29 18:15:59 -0700 | [diff] [blame] | 3271 | swp_entry_t entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | pte_t pte; |
Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 3273 | int locked; |
Rik van Riel | ad8c2ee | 2010-08-09 17:19:48 -0700 | [diff] [blame] | 3274 | int exclusive = 0; |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3275 | vm_fault_t ret = 0; |
Joonsoo Kim | aae466b | 2020-08-11 18:30:50 -0700 | [diff] [blame] | 3276 | void *shadow = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3277 | |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 3278 | if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte)) |
Hugh Dickins | 8f4e210 | 2005-10-29 18:16:26 -0700 | [diff] [blame] | 3279 | goto out; |
Hugh Dickins | 65500d2 | 2005-10-29 18:15:59 -0700 | [diff] [blame] | 3280 | |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 3281 | entry = pte_to_swp_entry(vmf->orig_pte); |
Andi Kleen | d1737fd | 2009-09-16 11:50:06 +0200 | [diff] [blame] | 3282 | if (unlikely(non_swap_entry(entry))) { |
| 3283 | if (is_migration_entry(entry)) { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3284 | migration_entry_wait(vma->vm_mm, vmf->pmd, |
| 3285 | vmf->address); |
Jérôme Glisse | 5042db4 | 2017-09-08 16:11:43 -0700 | [diff] [blame] | 3286 | } else if (is_device_private_entry(entry)) { |
Christoph Hellwig | 897e636 | 2019-06-26 14:27:11 +0200 | [diff] [blame] | 3287 | vmf->page = device_private_entry_to_page(entry); |
| 3288 | ret = vmf->page->pgmap->ops->migrate_to_ram(vmf); |
Andi Kleen | d1737fd | 2009-09-16 11:50:06 +0200 | [diff] [blame] | 3289 | } else if (is_hwpoison_entry(entry)) { |
| 3290 | ret = VM_FAULT_HWPOISON; |
| 3291 | } else { |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 3292 | print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL); |
Hugh Dickins | d99be1a | 2009-12-14 17:59:04 -0800 | [diff] [blame] | 3293 | ret = VM_FAULT_SIGBUS; |
Andi Kleen | d1737fd | 2009-09-16 11:50:06 +0200 | [diff] [blame] | 3294 | } |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 3295 | goto out; |
| 3296 | } |
Minchan Kim | 0bcac06 | 2017-11-15 17:33:07 -0800 | [diff] [blame] | 3297 | |
| 3298 | |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 3299 | delayacct_set_flag(DELAYACCT_PF_SWAPIN); |
Minchan Kim | eaf649eb | 2018-04-05 16:23:39 -0700 | [diff] [blame] | 3300 | page = lookup_swap_cache(entry, vma, vmf->address); |
| 3301 | swapcache = page; |
Minchan Kim | f802077 | 2018-01-18 16:33:50 -0800 | [diff] [blame] | 3302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3303 | if (!page) { |
Minchan Kim | 0bcac06 | 2017-11-15 17:33:07 -0800 | [diff] [blame] | 3304 | struct swap_info_struct *si = swp_swap_info(entry); |
| 3305 | |
Qian Cai | a449bf5 | 2020-08-14 17:31:31 -0700 | [diff] [blame] | 3306 | if (data_race(si->flags & SWP_SYNCHRONOUS_IO) && |
| 3307 | __swap_count(entry) == 1) { |
Minchan Kim | 0bcac06 | 2017-11-15 17:33:07 -0800 | [diff] [blame] | 3308 | /* skip swapcache */ |
Minchan Kim | e9e9b7e | 2018-04-05 16:23:42 -0700 | [diff] [blame] | 3309 | page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, |
| 3310 | vmf->address); |
Minchan Kim | 0bcac06 | 2017-11-15 17:33:07 -0800 | [diff] [blame] | 3311 | if (page) { |
| 3312 | __SetPageLocked(page); |
| 3313 | __SetPageSwapBacked(page); |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 3314 | |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 3315 | if (mem_cgroup_swapin_charge_page(page, |
| 3316 | vma->vm_mm, GFP_KERNEL, entry)) { |
Michal Hocko | 545b1b0 | 2020-06-25 20:29:21 -0700 | [diff] [blame] | 3317 | ret = VM_FAULT_OOM; |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 3318 | goto out_page; |
Michal Hocko | 545b1b0 | 2020-06-25 20:29:21 -0700 | [diff] [blame] | 3319 | } |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 3320 | mem_cgroup_swapin_uncharge_swap(entry); |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 3321 | |
Joonsoo Kim | aae466b | 2020-08-11 18:30:50 -0700 | [diff] [blame] | 3322 | shadow = get_shadow_from_swap_cache(entry); |
| 3323 | if (shadow) |
| 3324 | workingset_refault(page, shadow); |
Joonsoo Kim | 0076f02 | 2020-06-25 20:30:37 -0700 | [diff] [blame] | 3325 | |
Johannes Weiner | 6058eae | 2020-06-03 16:02:40 -0700 | [diff] [blame] | 3326 | lru_cache_add(page); |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 3327 | |
| 3328 | /* To provide entry to swap_readpage() */ |
| 3329 | set_page_private(page, entry.val); |
Minchan Kim | 0bcac06 | 2017-11-15 17:33:07 -0800 | [diff] [blame] | 3330 | swap_readpage(page, true); |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 3331 | set_page_private(page, 0); |
Minchan Kim | 0bcac06 | 2017-11-15 17:33:07 -0800 | [diff] [blame] | 3332 | } |
Minchan Kim | aa8d22a | 2017-11-15 17:33:11 -0800 | [diff] [blame] | 3333 | } else { |
Minchan Kim | e9e9b7e | 2018-04-05 16:23:42 -0700 | [diff] [blame] | 3334 | page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, |
| 3335 | vmf); |
Minchan Kim | aa8d22a | 2017-11-15 17:33:11 -0800 | [diff] [blame] | 3336 | swapcache = page; |
Minchan Kim | 0bcac06 | 2017-11-15 17:33:07 -0800 | [diff] [blame] | 3337 | } |
| 3338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3339 | if (!page) { |
| 3340 | /* |
Hugh Dickins | 8f4e210 | 2005-10-29 18:16:26 -0700 | [diff] [blame] | 3341 | * Back out if somebody else faulted in this pte |
| 3342 | * while we released the pte lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3343 | */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3344 | vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, |
| 3345 | vmf->address, &vmf->ptl); |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 3346 | if (likely(pte_same(*vmf->pte, vmf->orig_pte))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3347 | ret = VM_FAULT_OOM; |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 3348 | delayacct_clear_flag(DELAYACCT_PF_SWAPIN); |
Hugh Dickins | 65500d2 | 2005-10-29 18:15:59 -0700 | [diff] [blame] | 3349 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | /* Had to read the page from swap area: Major fault */ |
| 3353 | ret = VM_FAULT_MAJOR; |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 3354 | count_vm_event(PGMAJFAULT); |
Roman Gushchin | 2262185 | 2017-07-06 15:40:25 -0700 | [diff] [blame] | 3355 | count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); |
Andi Kleen | d1737fd | 2009-09-16 11:50:06 +0200 | [diff] [blame] | 3356 | } else if (PageHWPoison(page)) { |
Wu Fengguang | 71f7252 | 2009-12-16 12:19:58 +0100 | [diff] [blame] | 3357 | /* |
| 3358 | * hwpoisoned dirty swapcache pages are kept for killing |
| 3359 | * owner processes (which may be unknown at hwpoison time) |
| 3360 | */ |
Andi Kleen | d1737fd | 2009-09-16 11:50:06 +0200 | [diff] [blame] | 3361 | ret = VM_FAULT_HWPOISON; |
| 3362 | delayacct_clear_flag(DELAYACCT_PF_SWAPIN); |
Andi Kleen | 4779cb3 | 2009-10-14 01:51:41 +0200 | [diff] [blame] | 3363 | goto out_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3364 | } |
| 3365 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3366 | locked = lock_page_or_retry(page, vma->vm_mm, vmf->flags); |
Rik van Riel | e709ffd | 2012-05-29 15:06:18 -0700 | [diff] [blame] | 3367 | |
Balbir Singh | 20a1022 | 2007-11-14 17:00:33 -0800 | [diff] [blame] | 3368 | delayacct_clear_flag(DELAYACCT_PF_SWAPIN); |
Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 3369 | if (!locked) { |
| 3370 | ret |= VM_FAULT_RETRY; |
| 3371 | goto out_release; |
| 3372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | |
Andrea Arcangeli | 4969c11 | 2010-09-09 16:37:52 -0700 | [diff] [blame] | 3374 | /* |
Hugh Dickins | 31c4a3d | 2010-09-19 19:40:22 -0700 | [diff] [blame] | 3375 | * Make sure try_to_free_swap or reuse_swap_page or swapoff did not |
| 3376 | * release the swapcache from under us. The page pin, and pte_same |
| 3377 | * test below, are not enough to exclude that. Even if it is still |
| 3378 | * swapcache, we need to check that the page's swap has not changed. |
Andrea Arcangeli | 4969c11 | 2010-09-09 16:37:52 -0700 | [diff] [blame] | 3379 | */ |
Minchan Kim | 0bcac06 | 2017-11-15 17:33:07 -0800 | [diff] [blame] | 3380 | if (unlikely((!PageSwapCache(page) || |
| 3381 | page_private(page) != entry.val)) && swapcache) |
Andrea Arcangeli | 4969c11 | 2010-09-09 16:37:52 -0700 | [diff] [blame] | 3382 | goto out_page; |
| 3383 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3384 | page = ksm_might_need_to_copy(page, vma, vmf->address); |
Hugh Dickins | cbf86cf | 2013-02-22 16:35:08 -0800 | [diff] [blame] | 3385 | if (unlikely(!page)) { |
| 3386 | ret = VM_FAULT_OOM; |
| 3387 | page = swapcache; |
Hugh Dickins | cbf86cf | 2013-02-22 16:35:08 -0800 | [diff] [blame] | 3388 | goto out_page; |
Hugh Dickins | 5ad6468 | 2009-12-14 17:59:24 -0800 | [diff] [blame] | 3389 | } |
| 3390 | |
Johannes Weiner | 9d82c69 | 2020-06-03 16:02:04 -0700 | [diff] [blame] | 3391 | cgroup_throttle_swaprate(page, GFP_KERNEL); |
KAMEZAWA Hiroyuki | 073e587 | 2008-10-18 20:28:08 -0700 | [diff] [blame] | 3392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | /* |
Hugh Dickins | 8f4e210 | 2005-10-29 18:16:26 -0700 | [diff] [blame] | 3394 | * Back out if somebody else already faulted in this pte. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3396 | vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, |
| 3397 | &vmf->ptl); |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 3398 | if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) |
Kirill Korotaev | b810748 | 2005-05-16 21:53:50 -0700 | [diff] [blame] | 3399 | goto out_nomap; |
Kirill Korotaev | b810748 | 2005-05-16 21:53:50 -0700 | [diff] [blame] | 3400 | |
| 3401 | if (unlikely(!PageUptodate(page))) { |
| 3402 | ret = VM_FAULT_SIGBUS; |
| 3403 | goto out_nomap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3404 | } |
| 3405 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3406 | /* |
| 3407 | * The page isn't present yet, go ahead with the fault. |
| 3408 | * |
| 3409 | * Be careful about the sequence of operations here. |
| 3410 | * To get its accounting right, reuse_swap_page() must be called |
| 3411 | * while the page is counted on swap but not yet in mapcount i.e. |
| 3412 | * before page_add_anon_rmap() and swap_free(); try_to_free_swap() |
| 3413 | * must be called after the swap_free(), or it will never succeed. |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3414 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 3416 | inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); |
| 3417 | dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3418 | pte = mk_pte(page, vma->vm_page_prot); |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3419 | if ((vmf->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3420 | pte = maybe_mkwrite(pte_mkdirty(pte), vma); |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3421 | vmf->flags &= ~FAULT_FLAG_WRITE; |
Andrea Arcangeli | 9a5b489 | 2010-08-09 17:19:49 -0700 | [diff] [blame] | 3422 | ret |= VM_FAULT_WRITE; |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 3423 | exclusive = RMAP_EXCLUSIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3424 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3425 | flush_icache_page(vma, page); |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 3426 | if (pte_swp_soft_dirty(vmf->orig_pte)) |
Cyrill Gorcunov | 179ef71 | 2013-08-13 16:00:49 -0700 | [diff] [blame] | 3427 | pte = pte_mksoft_dirty(pte); |
Peter Xu | f45ec5f | 2020-04-06 20:06:01 -0700 | [diff] [blame] | 3428 | if (pte_swp_uffd_wp(vmf->orig_pte)) { |
| 3429 | pte = pte_mkuffd_wp(pte); |
| 3430 | pte = pte_wrprotect(pte); |
| 3431 | } |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3432 | set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); |
Khalid Aziz | ca827d5 | 2018-02-21 10:15:44 -0700 | [diff] [blame] | 3433 | arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte); |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 3434 | vmf->orig_pte = pte; |
Minchan Kim | 0bcac06 | 2017-11-15 17:33:07 -0800 | [diff] [blame] | 3435 | |
| 3436 | /* ksm created a completely new copy */ |
| 3437 | if (unlikely(page != swapcache && swapcache)) { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3438 | page_add_new_anon_rmap(page, vma, vmf->address, false); |
Joonsoo Kim | b518154 | 2020-08-11 18:30:40 -0700 | [diff] [blame] | 3439 | lru_cache_add_inactive_or_unevictable(page, vma); |
Minchan Kim | 0bcac06 | 2017-11-15 17:33:07 -0800 | [diff] [blame] | 3440 | } else { |
| 3441 | do_page_add_anon_rmap(page, vma, vmf->address, exclusive); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 3442 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3443 | |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 3444 | swap_free(entry); |
Vladimir Davydov | 5ccc5ab | 2016-01-20 15:03:10 -0800 | [diff] [blame] | 3445 | if (mem_cgroup_swap_full(page) || |
| 3446 | (vma->vm_flags & VM_LOCKED) || PageMlocked(page)) |
Hugh Dickins | a2c43ee | 2009-01-06 14:39:36 -0800 | [diff] [blame] | 3447 | try_to_free_swap(page); |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 3448 | unlock_page(page); |
Minchan Kim | 0bcac06 | 2017-11-15 17:33:07 -0800 | [diff] [blame] | 3449 | if (page != swapcache && swapcache) { |
Andrea Arcangeli | 4969c11 | 2010-09-09 16:37:52 -0700 | [diff] [blame] | 3450 | /* |
| 3451 | * Hold the lock to avoid the swap entry to be reused |
| 3452 | * until we take the PT lock for the pte_same() check |
| 3453 | * (to avoid false positives from pte_same). For |
| 3454 | * further safety release the lock after the swap_free |
| 3455 | * so that the swap count won't change under a |
| 3456 | * parallel locked swapcache. |
| 3457 | */ |
| 3458 | unlock_page(swapcache); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3459 | put_page(swapcache); |
Andrea Arcangeli | 4969c11 | 2010-09-09 16:37:52 -0700 | [diff] [blame] | 3460 | } |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 3461 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3462 | if (vmf->flags & FAULT_FLAG_WRITE) { |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 3463 | ret |= do_wp_page(vmf); |
Hugh Dickins | 61469f1 | 2008-03-04 14:29:04 -0800 | [diff] [blame] | 3464 | if (ret & VM_FAULT_ERROR) |
| 3465 | ret &= VM_FAULT_ERROR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3466 | goto out; |
| 3467 | } |
| 3468 | |
| 3469 | /* No need to invalidate - it was non-present before */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3470 | update_mmu_cache(vma, vmf->address, vmf->pte); |
Hugh Dickins | 65500d2 | 2005-10-29 18:15:59 -0700 | [diff] [blame] | 3471 | unlock: |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3472 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3473 | out: |
| 3474 | return ret; |
Kirill Korotaev | b810748 | 2005-05-16 21:53:50 -0700 | [diff] [blame] | 3475 | out_nomap: |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3476 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
Johannes Weiner | bc43f75 | 2009-04-30 15:08:08 -0700 | [diff] [blame] | 3477 | out_page: |
Kirill Korotaev | b810748 | 2005-05-16 21:53:50 -0700 | [diff] [blame] | 3478 | unlock_page(page); |
Andi Kleen | 4779cb3 | 2009-10-14 01:51:41 +0200 | [diff] [blame] | 3479 | out_release: |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3480 | put_page(page); |
Minchan Kim | 0bcac06 | 2017-11-15 17:33:07 -0800 | [diff] [blame] | 3481 | if (page != swapcache && swapcache) { |
Andrea Arcangeli | 4969c11 | 2010-09-09 16:37:52 -0700 | [diff] [blame] | 3482 | unlock_page(swapcache); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3483 | put_page(swapcache); |
Andrea Arcangeli | 4969c11 | 2010-09-09 16:37:52 -0700 | [diff] [blame] | 3484 | } |
Hugh Dickins | 65500d2 | 2005-10-29 18:15:59 -0700 | [diff] [blame] | 3485 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | /* |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 3489 | * We enter with non-exclusive mmap_lock (to exclude vma changes, |
Hugh Dickins | 8f4e210 | 2005-10-29 18:16:26 -0700 | [diff] [blame] | 3490 | * but allow concurrent faults), and pte mapped but not yet locked. |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 3491 | * We return with mmap_lock still held, but pte unmapped and unlocked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3492 | */ |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3493 | static vm_fault_t do_anonymous_page(struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3494 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3495 | struct vm_area_struct *vma = vmf->vma; |
Hugh Dickins | 8f4e210 | 2005-10-29 18:16:26 -0700 | [diff] [blame] | 3496 | struct page *page; |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3497 | vm_fault_t ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3498 | pte_t entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3499 | |
Kirill A. Shutemov | 6b7339f | 2015-07-06 23:18:37 +0300 | [diff] [blame] | 3500 | /* File mapping without ->vm_ops ? */ |
| 3501 | if (vma->vm_flags & VM_SHARED) |
| 3502 | return VM_FAULT_SIGBUS; |
| 3503 | |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 3504 | /* |
| 3505 | * Use pte_alloc() instead of pte_alloc_map(). We can't run |
| 3506 | * pte_offset_map() on pmds where a huge pmd might be created |
| 3507 | * from a different thread. |
| 3508 | * |
Michel Lespinasse | 3e4e28c | 2020-06-08 21:33:51 -0700 | [diff] [blame] | 3509 | * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 3510 | * parallel threads are excluded by other means. |
| 3511 | * |
Michel Lespinasse | 3e4e28c | 2020-06-08 21:33:51 -0700 | [diff] [blame] | 3512 | * Here we only have mmap_read_lock(mm). |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 3513 | */ |
Joel Fernandes (Google) | 4cf5892 | 2019-01-03 15:28:34 -0800 | [diff] [blame] | 3514 | if (pte_alloc(vma->vm_mm, vmf->pmd)) |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 3515 | return VM_FAULT_OOM; |
| 3516 | |
Kirill A. Shutemov | f9ce0be | 2020-12-19 15:19:23 +0300 | [diff] [blame] | 3517 | /* See comment in handle_pte_fault() */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3518 | if (unlikely(pmd_trans_unstable(vmf->pmd))) |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 3519 | return 0; |
| 3520 | |
Linus Torvalds | 11ac552 | 2010-08-14 11:44:56 -0700 | [diff] [blame] | 3521 | /* Use the zero-page for reads */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3522 | if (!(vmf->flags & FAULT_FLAG_WRITE) && |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 3523 | !mm_forbids_zeropage(vma->vm_mm)) { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3524 | entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address), |
Hugh Dickins | 62eede6 | 2009-09-21 17:03:34 -0700 | [diff] [blame] | 3525 | vma->vm_page_prot)); |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3526 | vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, |
| 3527 | vmf->address, &vmf->ptl); |
Bibo Mao | 7df6769 | 2020-05-27 10:25:18 +0800 | [diff] [blame] | 3528 | if (!pte_none(*vmf->pte)) { |
| 3529 | update_mmu_tlb(vma, vmf->address, vmf->pte); |
Hugh Dickins | a13ea5b | 2009-09-21 17:03:30 -0700 | [diff] [blame] | 3530 | goto unlock; |
Bibo Mao | 7df6769 | 2020-05-27 10:25:18 +0800 | [diff] [blame] | 3531 | } |
Michal Hocko | 6b31d59 | 2017-08-18 15:16:15 -0700 | [diff] [blame] | 3532 | ret = check_stable_address_space(vma->vm_mm); |
| 3533 | if (ret) |
| 3534 | goto unlock; |
Andrea Arcangeli | 6b251fc | 2015-09-04 15:46:20 -0700 | [diff] [blame] | 3535 | /* Deliver the page fault to userland, check inside PT lock */ |
| 3536 | if (userfaultfd_missing(vma)) { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3537 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
| 3538 | return handle_userfault(vmf, VM_UFFD_MISSING); |
Andrea Arcangeli | 6b251fc | 2015-09-04 15:46:20 -0700 | [diff] [blame] | 3539 | } |
Hugh Dickins | a13ea5b | 2009-09-21 17:03:30 -0700 | [diff] [blame] | 3540 | goto setpte; |
| 3541 | } |
| 3542 | |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 3543 | /* Allocate our own private page. */ |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 3544 | if (unlikely(anon_vma_prepare(vma))) |
| 3545 | goto oom; |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3546 | page = alloc_zeroed_user_highpage_movable(vma, vmf->address); |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 3547 | if (!page) |
| 3548 | goto oom; |
Mel Gorman | eb3c24f | 2015-06-24 16:57:27 -0700 | [diff] [blame] | 3549 | |
Johannes Weiner | d9eb1ea | 2020-06-03 16:02:24 -0700 | [diff] [blame] | 3550 | if (mem_cgroup_charge(page, vma->vm_mm, GFP_KERNEL)) |
Mel Gorman | eb3c24f | 2015-06-24 16:57:27 -0700 | [diff] [blame] | 3551 | goto oom_free_page; |
Johannes Weiner | 9d82c69 | 2020-06-03 16:02:04 -0700 | [diff] [blame] | 3552 | cgroup_throttle_swaprate(page, GFP_KERNEL); |
Mel Gorman | eb3c24f | 2015-06-24 16:57:27 -0700 | [diff] [blame] | 3553 | |
Minchan Kim | 52f3762 | 2013-04-29 15:08:15 -0700 | [diff] [blame] | 3554 | /* |
| 3555 | * The memory barrier inside __SetPageUptodate makes sure that |
Wei Yang | f4f5329 | 2019-11-30 17:58:17 -0800 | [diff] [blame] | 3556 | * preceding stores to the page contents become visible before |
Minchan Kim | 52f3762 | 2013-04-29 15:08:15 -0700 | [diff] [blame] | 3557 | * the set_pte_at() write. |
| 3558 | */ |
Nick Piggin | 0ed361d | 2008-02-04 22:29:34 -0800 | [diff] [blame] | 3559 | __SetPageUptodate(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3560 | |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 3561 | entry = mk_pte(page, vma->vm_page_prot); |
Hugh Dickins | 1ac0cb5 | 2009-09-21 17:03:29 -0700 | [diff] [blame] | 3562 | if (vma->vm_flags & VM_WRITE) |
| 3563 | entry = pte_mkwrite(pte_mkdirty(entry)); |
Hugh Dickins | 8f4e210 | 2005-10-29 18:16:26 -0700 | [diff] [blame] | 3564 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3565 | vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, |
| 3566 | &vmf->ptl); |
Bibo Mao | 7df6769 | 2020-05-27 10:25:18 +0800 | [diff] [blame] | 3567 | if (!pte_none(*vmf->pte)) { |
| 3568 | update_mmu_cache(vma, vmf->address, vmf->pte); |
Nick Piggin | 557ed1f | 2007-10-16 01:24:40 -0700 | [diff] [blame] | 3569 | goto release; |
Bibo Mao | 7df6769 | 2020-05-27 10:25:18 +0800 | [diff] [blame] | 3570 | } |
Hugh Dickins | 9ba6929 | 2009-09-21 17:02:20 -0700 | [diff] [blame] | 3571 | |
Michal Hocko | 6b31d59 | 2017-08-18 15:16:15 -0700 | [diff] [blame] | 3572 | ret = check_stable_address_space(vma->vm_mm); |
| 3573 | if (ret) |
| 3574 | goto release; |
| 3575 | |
Andrea Arcangeli | 6b251fc | 2015-09-04 15:46:20 -0700 | [diff] [blame] | 3576 | /* Deliver the page fault to userland, check inside PT lock */ |
| 3577 | if (userfaultfd_missing(vma)) { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3578 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3579 | put_page(page); |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3580 | return handle_userfault(vmf, VM_UFFD_MISSING); |
Andrea Arcangeli | 6b251fc | 2015-09-04 15:46:20 -0700 | [diff] [blame] | 3581 | } |
| 3582 | |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 3583 | inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3584 | page_add_new_anon_rmap(page, vma, vmf->address, false); |
Joonsoo Kim | b518154 | 2020-08-11 18:30:40 -0700 | [diff] [blame] | 3585 | lru_cache_add_inactive_or_unevictable(page, vma); |
Hugh Dickins | a13ea5b | 2009-09-21 17:03:30 -0700 | [diff] [blame] | 3586 | setpte: |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3587 | set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3588 | |
| 3589 | /* No need to invalidate - it was non-present before */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3590 | update_mmu_cache(vma, vmf->address, vmf->pte); |
Hugh Dickins | 65500d2 | 2005-10-29 18:15:59 -0700 | [diff] [blame] | 3591 | unlock: |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3592 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
Michal Hocko | 6b31d59 | 2017-08-18 15:16:15 -0700 | [diff] [blame] | 3593 | return ret; |
Hugh Dickins | 8f4e210 | 2005-10-29 18:16:26 -0700 | [diff] [blame] | 3594 | release: |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3595 | put_page(page); |
Hugh Dickins | 8f4e210 | 2005-10-29 18:16:26 -0700 | [diff] [blame] | 3596 | goto unlock; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 3597 | oom_free_page: |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3598 | put_page(page); |
Hugh Dickins | 65500d2 | 2005-10-29 18:15:59 -0700 | [diff] [blame] | 3599 | oom: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3600 | return VM_FAULT_OOM; |
| 3601 | } |
| 3602 | |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 3603 | /* |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 3604 | * The mmap_lock must have been held on entry, and may have been |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 3605 | * released depending on flags and vma->vm_ops->fault() return value. |
| 3606 | * See filemap_fault() and __lock_page_retry(). |
| 3607 | */ |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3608 | static vm_fault_t __do_fault(struct vm_fault *vmf) |
Kirill A. Shutemov | 7eae74a | 2014-04-03 14:48:10 -0700 | [diff] [blame] | 3609 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3610 | struct vm_area_struct *vma = vmf->vma; |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3611 | vm_fault_t ret; |
Kirill A. Shutemov | 7eae74a | 2014-04-03 14:48:10 -0700 | [diff] [blame] | 3612 | |
Michal Hocko | 63f3655 | 2019-01-08 15:23:07 -0800 | [diff] [blame] | 3613 | /* |
| 3614 | * Preallocate pte before we take page_lock because this might lead to |
| 3615 | * deadlocks for memcg reclaim which waits for pages under writeback: |
| 3616 | * lock_page(A) |
| 3617 | * SetPageWriteback(A) |
| 3618 | * unlock_page(A) |
| 3619 | * lock_page(B) |
| 3620 | * lock_page(B) |
Yanfei Xu | d383807 | 2020-10-13 16:53:26 -0700 | [diff] [blame] | 3621 | * pte_alloc_one |
Michal Hocko | 63f3655 | 2019-01-08 15:23:07 -0800 | [diff] [blame] | 3622 | * shrink_page_list |
| 3623 | * wait_on_page_writeback(A) |
| 3624 | * SetPageWriteback(B) |
| 3625 | * unlock_page(B) |
| 3626 | * # flush A, B to clear the writeback |
| 3627 | */ |
| 3628 | if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) { |
Yanfei Xu | a7069ee | 2020-10-13 16:53:29 -0700 | [diff] [blame] | 3629 | vmf->prealloc_pte = pte_alloc_one(vma->vm_mm); |
Michal Hocko | 63f3655 | 2019-01-08 15:23:07 -0800 | [diff] [blame] | 3630 | if (!vmf->prealloc_pte) |
| 3631 | return VM_FAULT_OOM; |
| 3632 | smp_wmb(); /* See comment in __pte_alloc() */ |
| 3633 | } |
| 3634 | |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 3635 | ret = vma->vm_ops->fault(vmf); |
Jan Kara | 3917048 | 2016-12-14 15:07:18 -0800 | [diff] [blame] | 3636 | if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY | |
Jan Kara | b1aa812 | 2016-12-14 15:07:24 -0800 | [diff] [blame] | 3637 | VM_FAULT_DONE_COW))) |
Kirill A. Shutemov | 7eae74a | 2014-04-03 14:48:10 -0700 | [diff] [blame] | 3638 | return ret; |
| 3639 | |
Jan Kara | 667240e | 2016-12-14 15:07:07 -0800 | [diff] [blame] | 3640 | if (unlikely(PageHWPoison(vmf->page))) { |
Kirill A. Shutemov | 7eae74a | 2014-04-03 14:48:10 -0700 | [diff] [blame] | 3641 | if (ret & VM_FAULT_LOCKED) |
Jan Kara | 667240e | 2016-12-14 15:07:07 -0800 | [diff] [blame] | 3642 | unlock_page(vmf->page); |
| 3643 | put_page(vmf->page); |
Jan Kara | 936ca80 | 2016-12-14 15:07:10 -0800 | [diff] [blame] | 3644 | vmf->page = NULL; |
Kirill A. Shutemov | 7eae74a | 2014-04-03 14:48:10 -0700 | [diff] [blame] | 3645 | return VM_FAULT_HWPOISON; |
| 3646 | } |
| 3647 | |
| 3648 | if (unlikely(!(ret & VM_FAULT_LOCKED))) |
Jan Kara | 667240e | 2016-12-14 15:07:07 -0800 | [diff] [blame] | 3649 | lock_page(vmf->page); |
Kirill A. Shutemov | 7eae74a | 2014-04-03 14:48:10 -0700 | [diff] [blame] | 3650 | else |
Jan Kara | 667240e | 2016-12-14 15:07:07 -0800 | [diff] [blame] | 3651 | VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page); |
Kirill A. Shutemov | 7eae74a | 2014-04-03 14:48:10 -0700 | [diff] [blame] | 3652 | |
Kirill A. Shutemov | 7eae74a | 2014-04-03 14:48:10 -0700 | [diff] [blame] | 3653 | return ret; |
| 3654 | } |
| 3655 | |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 3656 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3657 | static void deposit_prealloc_pte(struct vm_fault *vmf) |
Aneesh Kumar K.V | 953c66c | 2016-12-12 16:44:32 -0800 | [diff] [blame] | 3658 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3659 | struct vm_area_struct *vma = vmf->vma; |
Aneesh Kumar K.V | 953c66c | 2016-12-12 16:44:32 -0800 | [diff] [blame] | 3660 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3661 | pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); |
Aneesh Kumar K.V | 953c66c | 2016-12-12 16:44:32 -0800 | [diff] [blame] | 3662 | /* |
| 3663 | * We are going to consume the prealloc table, |
| 3664 | * count that as nr_ptes. |
| 3665 | */ |
Kirill A. Shutemov | c481290 | 2017-11-15 17:35:37 -0800 | [diff] [blame] | 3666 | mm_inc_nr_ptes(vma->vm_mm); |
Tobin C Harding | 7f2b6ce | 2017-02-24 14:58:59 -0800 | [diff] [blame] | 3667 | vmf->prealloc_pte = NULL; |
Aneesh Kumar K.V | 953c66c | 2016-12-12 16:44:32 -0800 | [diff] [blame] | 3668 | } |
| 3669 | |
Kirill A. Shutemov | f9ce0be | 2020-12-19 15:19:23 +0300 | [diff] [blame] | 3670 | vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) |
Kirill A. Shutemov | 1010245 | 2016-07-26 15:25:29 -0700 | [diff] [blame] | 3671 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3672 | struct vm_area_struct *vma = vmf->vma; |
| 3673 | bool write = vmf->flags & FAULT_FLAG_WRITE; |
| 3674 | unsigned long haddr = vmf->address & HPAGE_PMD_MASK; |
Kirill A. Shutemov | 1010245 | 2016-07-26 15:25:29 -0700 | [diff] [blame] | 3675 | pmd_t entry; |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3676 | int i; |
Matthew Wilcox (Oracle) | d01ac3c | 2020-10-15 20:05:26 -0700 | [diff] [blame] | 3677 | vm_fault_t ret = VM_FAULT_FALLBACK; |
Kirill A. Shutemov | 1010245 | 2016-07-26 15:25:29 -0700 | [diff] [blame] | 3678 | |
| 3679 | if (!transhuge_vma_suitable(vma, haddr)) |
Matthew Wilcox (Oracle) | d01ac3c | 2020-10-15 20:05:26 -0700 | [diff] [blame] | 3680 | return ret; |
Kirill A. Shutemov | 1010245 | 2016-07-26 15:25:29 -0700 | [diff] [blame] | 3681 | |
Kirill A. Shutemov | 1010245 | 2016-07-26 15:25:29 -0700 | [diff] [blame] | 3682 | page = compound_head(page); |
Matthew Wilcox (Oracle) | d01ac3c | 2020-10-15 20:05:26 -0700 | [diff] [blame] | 3683 | if (compound_order(page) != HPAGE_PMD_ORDER) |
| 3684 | return ret; |
Kirill A. Shutemov | 1010245 | 2016-07-26 15:25:29 -0700 | [diff] [blame] | 3685 | |
Aneesh Kumar K.V | 953c66c | 2016-12-12 16:44:32 -0800 | [diff] [blame] | 3686 | /* |
| 3687 | * Archs like ppc64 need additonal space to store information |
| 3688 | * related to pte entry. Use the preallocated table for that. |
| 3689 | */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3690 | if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) { |
Joel Fernandes (Google) | 4cf5892 | 2019-01-03 15:28:34 -0800 | [diff] [blame] | 3691 | vmf->prealloc_pte = pte_alloc_one(vma->vm_mm); |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3692 | if (!vmf->prealloc_pte) |
Aneesh Kumar K.V | 953c66c | 2016-12-12 16:44:32 -0800 | [diff] [blame] | 3693 | return VM_FAULT_OOM; |
| 3694 | smp_wmb(); /* See comment in __pte_alloc() */ |
| 3695 | } |
| 3696 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3697 | vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); |
| 3698 | if (unlikely(!pmd_none(*vmf->pmd))) |
Kirill A. Shutemov | 1010245 | 2016-07-26 15:25:29 -0700 | [diff] [blame] | 3699 | goto out; |
| 3700 | |
| 3701 | for (i = 0; i < HPAGE_PMD_NR; i++) |
| 3702 | flush_icache_page(vma, page + i); |
| 3703 | |
| 3704 | entry = mk_huge_pmd(page, vma->vm_page_prot); |
| 3705 | if (write) |
Linus Torvalds | f55e101 | 2017-11-29 09:01:01 -0800 | [diff] [blame] | 3706 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); |
Kirill A. Shutemov | 1010245 | 2016-07-26 15:25:29 -0700 | [diff] [blame] | 3707 | |
Yang Shi | fadae29 | 2018-08-17 15:44:55 -0700 | [diff] [blame] | 3708 | add_mm_counter(vma->vm_mm, mm_counter_file(page), HPAGE_PMD_NR); |
Kirill A. Shutemov | 1010245 | 2016-07-26 15:25:29 -0700 | [diff] [blame] | 3709 | page_add_file_rmap(page, true); |
Aneesh Kumar K.V | 953c66c | 2016-12-12 16:44:32 -0800 | [diff] [blame] | 3710 | /* |
| 3711 | * deposit and withdraw with pmd lock held |
| 3712 | */ |
| 3713 | if (arch_needs_pgtable_deposit()) |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3714 | deposit_prealloc_pte(vmf); |
Kirill A. Shutemov | 1010245 | 2016-07-26 15:25:29 -0700 | [diff] [blame] | 3715 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3716 | set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry); |
Kirill A. Shutemov | 1010245 | 2016-07-26 15:25:29 -0700 | [diff] [blame] | 3717 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3718 | update_mmu_cache_pmd(vma, haddr, vmf->pmd); |
Kirill A. Shutemov | 1010245 | 2016-07-26 15:25:29 -0700 | [diff] [blame] | 3719 | |
| 3720 | /* fault is handled */ |
| 3721 | ret = 0; |
Kirill A. Shutemov | 95ecedc | 2016-07-26 15:25:31 -0700 | [diff] [blame] | 3722 | count_vm_event(THP_FILE_MAPPED); |
Kirill A. Shutemov | 1010245 | 2016-07-26 15:25:29 -0700 | [diff] [blame] | 3723 | out: |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3724 | spin_unlock(vmf->ptl); |
Kirill A. Shutemov | 1010245 | 2016-07-26 15:25:29 -0700 | [diff] [blame] | 3725 | return ret; |
| 3726 | } |
| 3727 | #else |
Kirill A. Shutemov | f9ce0be | 2020-12-19 15:19:23 +0300 | [diff] [blame] | 3728 | vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) |
Kirill A. Shutemov | 1010245 | 2016-07-26 15:25:29 -0700 | [diff] [blame] | 3729 | { |
Kirill A. Shutemov | f9ce0be | 2020-12-19 15:19:23 +0300 | [diff] [blame] | 3730 | return VM_FAULT_FALLBACK; |
Kirill A. Shutemov | 1010245 | 2016-07-26 15:25:29 -0700 | [diff] [blame] | 3731 | } |
| 3732 | #endif |
| 3733 | |
Will Deacon | 9d3af4b | 2021-01-14 15:24:19 +0000 | [diff] [blame] | 3734 | void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr) |
Kirill A. Shutemov | 3bb9779 | 2014-04-03 14:48:16 -0700 | [diff] [blame] | 3735 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3736 | struct vm_area_struct *vma = vmf->vma; |
| 3737 | bool write = vmf->flags & FAULT_FLAG_WRITE; |
Will Deacon | 9d3af4b | 2021-01-14 15:24:19 +0000 | [diff] [blame] | 3738 | bool prefault = vmf->address != addr; |
Kirill A. Shutemov | 3bb9779 | 2014-04-03 14:48:16 -0700 | [diff] [blame] | 3739 | pte_t entry; |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 3740 | |
Kirill A. Shutemov | 3bb9779 | 2014-04-03 14:48:16 -0700 | [diff] [blame] | 3741 | flush_icache_page(vma, page); |
| 3742 | entry = mk_pte(page, vma->vm_page_prot); |
Will Deacon | 46bdb42 | 2020-11-24 18:48:26 +0000 | [diff] [blame] | 3743 | |
| 3744 | if (prefault && arch_wants_old_prefaulted_pte()) |
| 3745 | entry = pte_mkold(entry); |
Will Deacon | 46bdb42 | 2020-11-24 18:48:26 +0000 | [diff] [blame] | 3746 | |
Kirill A. Shutemov | 3bb9779 | 2014-04-03 14:48:16 -0700 | [diff] [blame] | 3747 | if (write) |
| 3748 | entry = maybe_mkwrite(pte_mkdirty(entry), vma); |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 3749 | /* copy-on-write page */ |
| 3750 | if (write && !(vma->vm_flags & VM_SHARED)) { |
Kirill A. Shutemov | 3bb9779 | 2014-04-03 14:48:16 -0700 | [diff] [blame] | 3751 | inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); |
Will Deacon | 9d3af4b | 2021-01-14 15:24:19 +0000 | [diff] [blame] | 3752 | page_add_new_anon_rmap(page, vma, addr, false); |
Joonsoo Kim | b518154 | 2020-08-11 18:30:40 -0700 | [diff] [blame] | 3753 | lru_cache_add_inactive_or_unevictable(page, vma); |
Kirill A. Shutemov | 3bb9779 | 2014-04-03 14:48:16 -0700 | [diff] [blame] | 3754 | } else { |
Jerome Marchand | eca56ff | 2016-01-14 15:19:26 -0800 | [diff] [blame] | 3755 | inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page)); |
Kirill A. Shutemov | dd78fed | 2016-07-26 15:25:26 -0700 | [diff] [blame] | 3756 | page_add_file_rmap(page, false); |
Kirill A. Shutemov | 3bb9779 | 2014-04-03 14:48:16 -0700 | [diff] [blame] | 3757 | } |
Will Deacon | 9d3af4b | 2021-01-14 15:24:19 +0000 | [diff] [blame] | 3758 | set_pte_at(vma->vm_mm, addr, vmf->pte, entry); |
Kirill A. Shutemov | 3bb9779 | 2014-04-03 14:48:16 -0700 | [diff] [blame] | 3759 | } |
| 3760 | |
Jan Kara | 9118c0c | 2016-12-14 15:07:21 -0800 | [diff] [blame] | 3761 | /** |
| 3762 | * finish_fault - finish page fault once we have prepared the page to fault |
| 3763 | * |
| 3764 | * @vmf: structure describing the fault |
| 3765 | * |
| 3766 | * This function handles all that is needed to finish a page fault once the |
| 3767 | * page to fault in is prepared. It handles locking of PTEs, inserts PTE for |
| 3768 | * given page, adds reverse page mapping, handles memcg charges and LRU |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 3769 | * addition. |
Jan Kara | 9118c0c | 2016-12-14 15:07:21 -0800 | [diff] [blame] | 3770 | * |
| 3771 | * The function expects the page to be locked and on success it consumes a |
| 3772 | * reference of a page being mapped (for the PTE which maps it). |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 3773 | * |
| 3774 | * Return: %0 on success, %VM_FAULT_ code in case of error. |
Jan Kara | 9118c0c | 2016-12-14 15:07:21 -0800 | [diff] [blame] | 3775 | */ |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3776 | vm_fault_t finish_fault(struct vm_fault *vmf) |
Jan Kara | 9118c0c | 2016-12-14 15:07:21 -0800 | [diff] [blame] | 3777 | { |
Kirill A. Shutemov | f9ce0be | 2020-12-19 15:19:23 +0300 | [diff] [blame] | 3778 | struct vm_area_struct *vma = vmf->vma; |
Jan Kara | 9118c0c | 2016-12-14 15:07:21 -0800 | [diff] [blame] | 3779 | struct page *page; |
Kirill A. Shutemov | f9ce0be | 2020-12-19 15:19:23 +0300 | [diff] [blame] | 3780 | vm_fault_t ret; |
Jan Kara | 9118c0c | 2016-12-14 15:07:21 -0800 | [diff] [blame] | 3781 | |
| 3782 | /* Did we COW the page? */ |
Kirill A. Shutemov | f9ce0be | 2020-12-19 15:19:23 +0300 | [diff] [blame] | 3783 | if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) |
Jan Kara | 9118c0c | 2016-12-14 15:07:21 -0800 | [diff] [blame] | 3784 | page = vmf->cow_page; |
| 3785 | else |
| 3786 | page = vmf->page; |
Michal Hocko | 6b31d59 | 2017-08-18 15:16:15 -0700 | [diff] [blame] | 3787 | |
| 3788 | /* |
| 3789 | * check even for read faults because we might have lost our CoWed |
| 3790 | * page |
| 3791 | */ |
Kirill A. Shutemov | f9ce0be | 2020-12-19 15:19:23 +0300 | [diff] [blame] | 3792 | if (!(vma->vm_flags & VM_SHARED)) { |
| 3793 | ret = check_stable_address_space(vma->vm_mm); |
| 3794 | if (ret) |
| 3795 | return ret; |
| 3796 | } |
| 3797 | |
| 3798 | if (pmd_none(*vmf->pmd)) { |
| 3799 | if (PageTransCompound(page)) { |
| 3800 | ret = do_set_pmd(vmf, page); |
| 3801 | if (ret != VM_FAULT_FALLBACK) |
| 3802 | return ret; |
| 3803 | } |
| 3804 | |
| 3805 | if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd))) |
| 3806 | return VM_FAULT_OOM; |
| 3807 | } |
| 3808 | |
| 3809 | /* See comment in handle_pte_fault() */ |
| 3810 | if (pmd_devmap_trans_unstable(vmf->pmd)) |
| 3811 | return 0; |
| 3812 | |
| 3813 | vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, |
| 3814 | vmf->address, &vmf->ptl); |
| 3815 | ret = 0; |
| 3816 | /* Re-check under ptl */ |
| 3817 | if (likely(pte_none(*vmf->pte))) |
Will Deacon | 9d3af4b | 2021-01-14 15:24:19 +0000 | [diff] [blame] | 3818 | do_set_pte(vmf, page, vmf->address); |
Kirill A. Shutemov | f9ce0be | 2020-12-19 15:19:23 +0300 | [diff] [blame] | 3819 | else |
| 3820 | ret = VM_FAULT_NOPAGE; |
| 3821 | |
| 3822 | update_mmu_tlb(vma, vmf->address, vmf->pte); |
| 3823 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
Jan Kara | 9118c0c | 2016-12-14 15:07:21 -0800 | [diff] [blame] | 3824 | return ret; |
| 3825 | } |
| 3826 | |
Kirill A. Shutemov | 3a91053 | 2014-08-06 16:08:07 -0700 | [diff] [blame] | 3827 | static unsigned long fault_around_bytes __read_mostly = |
| 3828 | rounddown_pow_of_two(65536); |
Kirill A. Shutemov | a9b0f86 | 2014-06-04 16:10:54 -0700 | [diff] [blame] | 3829 | |
Kirill A. Shutemov | 1592eef | 2014-04-07 15:37:22 -0700 | [diff] [blame] | 3830 | #ifdef CONFIG_DEBUG_FS |
Kirill A. Shutemov | a9b0f86 | 2014-06-04 16:10:54 -0700 | [diff] [blame] | 3831 | static int fault_around_bytes_get(void *data, u64 *val) |
Kirill A. Shutemov | 1592eef | 2014-04-07 15:37:22 -0700 | [diff] [blame] | 3832 | { |
Kirill A. Shutemov | a9b0f86 | 2014-06-04 16:10:54 -0700 | [diff] [blame] | 3833 | *val = fault_around_bytes; |
Kirill A. Shutemov | 1592eef | 2014-04-07 15:37:22 -0700 | [diff] [blame] | 3834 | return 0; |
| 3835 | } |
| 3836 | |
Andrey Ryabinin | b4903d6 | 2014-07-30 16:08:35 -0700 | [diff] [blame] | 3837 | /* |
William Kucharski | da391d6 | 2018-01-31 16:21:11 -0800 | [diff] [blame] | 3838 | * fault_around_bytes must be rounded down to the nearest page order as it's |
| 3839 | * what do_fault_around() expects to see. |
Andrey Ryabinin | b4903d6 | 2014-07-30 16:08:35 -0700 | [diff] [blame] | 3840 | */ |
Kirill A. Shutemov | a9b0f86 | 2014-06-04 16:10:54 -0700 | [diff] [blame] | 3841 | static int fault_around_bytes_set(void *data, u64 val) |
Kirill A. Shutemov | 1592eef | 2014-04-07 15:37:22 -0700 | [diff] [blame] | 3842 | { |
Kirill A. Shutemov | a9b0f86 | 2014-06-04 16:10:54 -0700 | [diff] [blame] | 3843 | if (val / PAGE_SIZE > PTRS_PER_PTE) |
Kirill A. Shutemov | 1592eef | 2014-04-07 15:37:22 -0700 | [diff] [blame] | 3844 | return -EINVAL; |
Andrey Ryabinin | b4903d6 | 2014-07-30 16:08:35 -0700 | [diff] [blame] | 3845 | if (val > PAGE_SIZE) |
| 3846 | fault_around_bytes = rounddown_pow_of_two(val); |
| 3847 | else |
| 3848 | fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */ |
Kirill A. Shutemov | 1592eef | 2014-04-07 15:37:22 -0700 | [diff] [blame] | 3849 | return 0; |
| 3850 | } |
Yevgen Pronenko | 0a1345f | 2017-07-10 15:47:17 -0700 | [diff] [blame] | 3851 | DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops, |
Kirill A. Shutemov | a9b0f86 | 2014-06-04 16:10:54 -0700 | [diff] [blame] | 3852 | fault_around_bytes_get, fault_around_bytes_set, "%llu\n"); |
Kirill A. Shutemov | 1592eef | 2014-04-07 15:37:22 -0700 | [diff] [blame] | 3853 | |
| 3854 | static int __init fault_around_debugfs(void) |
| 3855 | { |
Greg Kroah-Hartman | d9f7979 | 2019-03-05 15:46:09 -0800 | [diff] [blame] | 3856 | debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL, |
| 3857 | &fault_around_bytes_fops); |
Kirill A. Shutemov | 1592eef | 2014-04-07 15:37:22 -0700 | [diff] [blame] | 3858 | return 0; |
| 3859 | } |
| 3860 | late_initcall(fault_around_debugfs); |
Kirill A. Shutemov | 1592eef | 2014-04-07 15:37:22 -0700 | [diff] [blame] | 3861 | #endif |
Kirill A. Shutemov | 8c6e50b | 2014-04-07 15:37:18 -0700 | [diff] [blame] | 3862 | |
Kirill A. Shutemov | 1fdb412 | 2014-06-04 16:10:55 -0700 | [diff] [blame] | 3863 | /* |
| 3864 | * do_fault_around() tries to map few pages around the fault address. The hope |
| 3865 | * is that the pages will be needed soon and this will lower the number of |
| 3866 | * faults to handle. |
| 3867 | * |
| 3868 | * It uses vm_ops->map_pages() to map the pages, which skips the page if it's |
| 3869 | * not ready to be mapped: not up-to-date, locked, etc. |
| 3870 | * |
| 3871 | * This function is called with the page table lock taken. In the split ptlock |
| 3872 | * case the page table lock only protects only those entries which belong to |
| 3873 | * the page table corresponding to the fault address. |
| 3874 | * |
| 3875 | * This function doesn't cross the VMA boundaries, in order to call map_pages() |
| 3876 | * only once. |
| 3877 | * |
William Kucharski | da391d6 | 2018-01-31 16:21:11 -0800 | [diff] [blame] | 3878 | * fault_around_bytes defines how many bytes we'll try to map. |
| 3879 | * do_fault_around() expects it to be set to a power of two less than or equal |
| 3880 | * to PTRS_PER_PTE. |
Kirill A. Shutemov | 1fdb412 | 2014-06-04 16:10:55 -0700 | [diff] [blame] | 3881 | * |
William Kucharski | da391d6 | 2018-01-31 16:21:11 -0800 | [diff] [blame] | 3882 | * The virtual address of the area that we map is naturally aligned to |
| 3883 | * fault_around_bytes rounded down to the machine page size |
| 3884 | * (and therefore to page order). This way it's easier to guarantee |
| 3885 | * that we don't cross page table boundaries. |
Kirill A. Shutemov | 1fdb412 | 2014-06-04 16:10:55 -0700 | [diff] [blame] | 3886 | */ |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3887 | static vm_fault_t do_fault_around(struct vm_fault *vmf) |
Kirill A. Shutemov | 8c6e50b | 2014-04-07 15:37:18 -0700 | [diff] [blame] | 3888 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3889 | unsigned long address = vmf->address, nr_pages, mask; |
Jan Kara | 0721ec8 | 2016-12-14 15:07:04 -0800 | [diff] [blame] | 3890 | pgoff_t start_pgoff = vmf->pgoff; |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 3891 | pgoff_t end_pgoff; |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3892 | int off; |
Kirill A. Shutemov | 8c6e50b | 2014-04-07 15:37:18 -0700 | [diff] [blame] | 3893 | |
Jason Low | 4db0c3c | 2015-04-15 16:14:08 -0700 | [diff] [blame] | 3894 | nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT; |
Kirill A. Shutemov | aecd6f4 | 2014-08-06 16:08:05 -0700 | [diff] [blame] | 3895 | mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK; |
| 3896 | |
Kirill A. Shutemov | f9ce0be | 2020-12-19 15:19:23 +0300 | [diff] [blame] | 3897 | address = max(address & mask, vmf->vma->vm_start); |
| 3898 | off = ((vmf->address - address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1); |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 3899 | start_pgoff -= off; |
Kirill A. Shutemov | 8c6e50b | 2014-04-07 15:37:18 -0700 | [diff] [blame] | 3900 | |
| 3901 | /* |
William Kucharski | da391d6 | 2018-01-31 16:21:11 -0800 | [diff] [blame] | 3902 | * end_pgoff is either the end of the page table, the end of |
| 3903 | * the vma or nr_pages from start_pgoff, depending what is nearest. |
Kirill A. Shutemov | 8c6e50b | 2014-04-07 15:37:18 -0700 | [diff] [blame] | 3904 | */ |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 3905 | end_pgoff = start_pgoff - |
Kirill A. Shutemov | f9ce0be | 2020-12-19 15:19:23 +0300 | [diff] [blame] | 3906 | ((address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) + |
Kirill A. Shutemov | 8c6e50b | 2014-04-07 15:37:18 -0700 | [diff] [blame] | 3907 | PTRS_PER_PTE - 1; |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3908 | end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1, |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 3909 | start_pgoff + nr_pages - 1); |
Kirill A. Shutemov | 8c6e50b | 2014-04-07 15:37:18 -0700 | [diff] [blame] | 3910 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3911 | if (pmd_none(*vmf->pmd)) { |
Joel Fernandes (Google) | 4cf5892 | 2019-01-03 15:28:34 -0800 | [diff] [blame] | 3912 | vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm); |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3913 | if (!vmf->prealloc_pte) |
Kirill A. Shutemov | f9ce0be | 2020-12-19 15:19:23 +0300 | [diff] [blame] | 3914 | return VM_FAULT_OOM; |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 3915 | smp_wmb(); /* See comment in __pte_alloc() */ |
Kirill A. Shutemov | 8c6e50b | 2014-04-07 15:37:18 -0700 | [diff] [blame] | 3916 | } |
| 3917 | |
Kirill A. Shutemov | f9ce0be | 2020-12-19 15:19:23 +0300 | [diff] [blame] | 3918 | return vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff); |
Kirill A. Shutemov | 8c6e50b | 2014-04-07 15:37:18 -0700 | [diff] [blame] | 3919 | } |
| 3920 | |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3921 | static vm_fault_t do_read_fault(struct vm_fault *vmf) |
Kirill A. Shutemov | e655fb2 | 2014-04-03 14:48:11 -0700 | [diff] [blame] | 3922 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3923 | struct vm_area_struct *vma = vmf->vma; |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3924 | vm_fault_t ret = 0; |
Kirill A. Shutemov | 8c6e50b | 2014-04-07 15:37:18 -0700 | [diff] [blame] | 3925 | |
| 3926 | /* |
| 3927 | * Let's call ->map_pages() first and use ->fault() as fallback |
| 3928 | * if page by the offset is not ready to be mapped (cold cache or |
| 3929 | * something). |
| 3930 | */ |
Kirill A. Shutemov | 9b4bdd2 | 2015-02-10 14:09:51 -0800 | [diff] [blame] | 3931 | if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) { |
Jan Kara | 0721ec8 | 2016-12-14 15:07:04 -0800 | [diff] [blame] | 3932 | ret = do_fault_around(vmf); |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 3933 | if (ret) |
| 3934 | return ret; |
Kirill A. Shutemov | 8c6e50b | 2014-04-07 15:37:18 -0700 | [diff] [blame] | 3935 | } |
Kirill A. Shutemov | e655fb2 | 2014-04-03 14:48:11 -0700 | [diff] [blame] | 3936 | |
Jan Kara | 936ca80 | 2016-12-14 15:07:10 -0800 | [diff] [blame] | 3937 | ret = __do_fault(vmf); |
Kirill A. Shutemov | e655fb2 | 2014-04-03 14:48:11 -0700 | [diff] [blame] | 3938 | if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) |
| 3939 | return ret; |
| 3940 | |
Jan Kara | 9118c0c | 2016-12-14 15:07:21 -0800 | [diff] [blame] | 3941 | ret |= finish_fault(vmf); |
Jan Kara | 936ca80 | 2016-12-14 15:07:10 -0800 | [diff] [blame] | 3942 | unlock_page(vmf->page); |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 3943 | if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) |
Jan Kara | 936ca80 | 2016-12-14 15:07:10 -0800 | [diff] [blame] | 3944 | put_page(vmf->page); |
Kirill A. Shutemov | e655fb2 | 2014-04-03 14:48:11 -0700 | [diff] [blame] | 3945 | return ret; |
| 3946 | } |
| 3947 | |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3948 | static vm_fault_t do_cow_fault(struct vm_fault *vmf) |
Kirill A. Shutemov | ec47c3b | 2014-04-03 14:48:12 -0700 | [diff] [blame] | 3949 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3950 | struct vm_area_struct *vma = vmf->vma; |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3951 | vm_fault_t ret; |
Kirill A. Shutemov | ec47c3b | 2014-04-03 14:48:12 -0700 | [diff] [blame] | 3952 | |
| 3953 | if (unlikely(anon_vma_prepare(vma))) |
| 3954 | return VM_FAULT_OOM; |
| 3955 | |
Jan Kara | 936ca80 | 2016-12-14 15:07:10 -0800 | [diff] [blame] | 3956 | vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address); |
| 3957 | if (!vmf->cow_page) |
Kirill A. Shutemov | ec47c3b | 2014-04-03 14:48:12 -0700 | [diff] [blame] | 3958 | return VM_FAULT_OOM; |
| 3959 | |
Johannes Weiner | d9eb1ea | 2020-06-03 16:02:24 -0700 | [diff] [blame] | 3960 | if (mem_cgroup_charge(vmf->cow_page, vma->vm_mm, GFP_KERNEL)) { |
Jan Kara | 936ca80 | 2016-12-14 15:07:10 -0800 | [diff] [blame] | 3961 | put_page(vmf->cow_page); |
Kirill A. Shutemov | ec47c3b | 2014-04-03 14:48:12 -0700 | [diff] [blame] | 3962 | return VM_FAULT_OOM; |
| 3963 | } |
Johannes Weiner | 9d82c69 | 2020-06-03 16:02:04 -0700 | [diff] [blame] | 3964 | cgroup_throttle_swaprate(vmf->cow_page, GFP_KERNEL); |
Kirill A. Shutemov | ec47c3b | 2014-04-03 14:48:12 -0700 | [diff] [blame] | 3965 | |
Jan Kara | 936ca80 | 2016-12-14 15:07:10 -0800 | [diff] [blame] | 3966 | ret = __do_fault(vmf); |
Kirill A. Shutemov | ec47c3b | 2014-04-03 14:48:12 -0700 | [diff] [blame] | 3967 | if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) |
| 3968 | goto uncharge_out; |
Jan Kara | 3917048 | 2016-12-14 15:07:18 -0800 | [diff] [blame] | 3969 | if (ret & VM_FAULT_DONE_COW) |
| 3970 | return ret; |
Kirill A. Shutemov | ec47c3b | 2014-04-03 14:48:12 -0700 | [diff] [blame] | 3971 | |
Jan Kara | b1aa812 | 2016-12-14 15:07:24 -0800 | [diff] [blame] | 3972 | copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma); |
Jan Kara | 936ca80 | 2016-12-14 15:07:10 -0800 | [diff] [blame] | 3973 | __SetPageUptodate(vmf->cow_page); |
Kirill A. Shutemov | ec47c3b | 2014-04-03 14:48:12 -0700 | [diff] [blame] | 3974 | |
Jan Kara | 9118c0c | 2016-12-14 15:07:21 -0800 | [diff] [blame] | 3975 | ret |= finish_fault(vmf); |
Jan Kara | b1aa812 | 2016-12-14 15:07:24 -0800 | [diff] [blame] | 3976 | unlock_page(vmf->page); |
| 3977 | put_page(vmf->page); |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 3978 | if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) |
| 3979 | goto uncharge_out; |
Kirill A. Shutemov | ec47c3b | 2014-04-03 14:48:12 -0700 | [diff] [blame] | 3980 | return ret; |
| 3981 | uncharge_out: |
Jan Kara | 936ca80 | 2016-12-14 15:07:10 -0800 | [diff] [blame] | 3982 | put_page(vmf->cow_page); |
Kirill A. Shutemov | ec47c3b | 2014-04-03 14:48:12 -0700 | [diff] [blame] | 3983 | return ret; |
| 3984 | } |
| 3985 | |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3986 | static vm_fault_t do_shared_fault(struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3987 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 3988 | struct vm_area_struct *vma = vmf->vma; |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 3989 | vm_fault_t ret, tmp; |
KAMEZAWA Hiroyuki | 1d65f86 | 2011-07-25 17:12:27 -0700 | [diff] [blame] | 3990 | |
Jan Kara | 936ca80 | 2016-12-14 15:07:10 -0800 | [diff] [blame] | 3991 | ret = __do_fault(vmf); |
Kirill A. Shutemov | 7eae74a | 2014-04-03 14:48:10 -0700 | [diff] [blame] | 3992 | if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) |
Kirill A. Shutemov | f0c6d4d | 2014-04-03 14:48:13 -0700 | [diff] [blame] | 3993 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3994 | |
| 3995 | /* |
Kirill A. Shutemov | f0c6d4d | 2014-04-03 14:48:13 -0700 | [diff] [blame] | 3996 | * Check if the backing address space wants to know that the page is |
| 3997 | * about to become writable |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3998 | */ |
Kirill A. Shutemov | fb09a46 | 2014-04-03 14:48:15 -0700 | [diff] [blame] | 3999 | if (vma->vm_ops->page_mkwrite) { |
Jan Kara | 936ca80 | 2016-12-14 15:07:10 -0800 | [diff] [blame] | 4000 | unlock_page(vmf->page); |
Jan Kara | 38b8cb7 | 2016-12-14 15:07:30 -0800 | [diff] [blame] | 4001 | tmp = do_page_mkwrite(vmf); |
Kirill A. Shutemov | fb09a46 | 2014-04-03 14:48:15 -0700 | [diff] [blame] | 4002 | if (unlikely(!tmp || |
| 4003 | (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { |
Jan Kara | 936ca80 | 2016-12-14 15:07:10 -0800 | [diff] [blame] | 4004 | put_page(vmf->page); |
Kirill A. Shutemov | fb09a46 | 2014-04-03 14:48:15 -0700 | [diff] [blame] | 4005 | return tmp; |
| 4006 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4007 | } |
| 4008 | |
Jan Kara | 9118c0c | 2016-12-14 15:07:21 -0800 | [diff] [blame] | 4009 | ret |= finish_fault(vmf); |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 4010 | if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | |
| 4011 | VM_FAULT_RETRY))) { |
Jan Kara | 936ca80 | 2016-12-14 15:07:10 -0800 | [diff] [blame] | 4012 | unlock_page(vmf->page); |
| 4013 | put_page(vmf->page); |
Kirill A. Shutemov | f0c6d4d | 2014-04-03 14:48:13 -0700 | [diff] [blame] | 4014 | return ret; |
Peter Zijlstra | d08b385 | 2006-09-25 23:30:57 -0700 | [diff] [blame] | 4015 | } |
Kirill A. Shutemov | f0c6d4d | 2014-04-03 14:48:13 -0700 | [diff] [blame] | 4016 | |
Johannes Weiner | 89b1533 | 2019-11-30 17:50:22 -0800 | [diff] [blame] | 4017 | ret |= fault_dirty_shared_page(vmf); |
KAMEZAWA Hiroyuki | 1d65f86 | 2011-07-25 17:12:27 -0700 | [diff] [blame] | 4018 | return ret; |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 4019 | } |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 4020 | |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 4021 | /* |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 4022 | * We enter with non-exclusive mmap_lock (to exclude vma changes, |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 4023 | * but allow concurrent faults). |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 4024 | * The mmap_lock may have been released depending on flags and our |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 4025 | * return value. See filemap_fault() and __lock_page_or_retry(). |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 4026 | * If mmap_lock is released, vma may become invalid (for example |
Jan Stancek | fc8efd2 | 2019-03-05 15:50:08 -0800 | [diff] [blame] | 4027 | * by other thread calling munmap()). |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 4028 | */ |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 4029 | static vm_fault_t do_fault(struct vm_fault *vmf) |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 4030 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4031 | struct vm_area_struct *vma = vmf->vma; |
Jan Stancek | fc8efd2 | 2019-03-05 15:50:08 -0800 | [diff] [blame] | 4032 | struct mm_struct *vm_mm = vma->vm_mm; |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 4033 | vm_fault_t ret; |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 4034 | |
Aneesh Kumar K.V | ff09d7e | 2018-10-26 15:09:01 -0700 | [diff] [blame] | 4035 | /* |
| 4036 | * The VMA was not fully populated on mmap() or missing VM_DONTEXPAND |
| 4037 | */ |
| 4038 | if (!vma->vm_ops->fault) { |
| 4039 | /* |
| 4040 | * If we find a migration pmd entry or a none pmd entry, which |
| 4041 | * should never happen, return SIGBUS |
| 4042 | */ |
| 4043 | if (unlikely(!pmd_present(*vmf->pmd))) |
| 4044 | ret = VM_FAULT_SIGBUS; |
| 4045 | else { |
| 4046 | vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, |
| 4047 | vmf->pmd, |
| 4048 | vmf->address, |
| 4049 | &vmf->ptl); |
| 4050 | /* |
| 4051 | * Make sure this is not a temporary clearing of pte |
| 4052 | * by holding ptl and checking again. A R/M/W update |
| 4053 | * of pte involves: take ptl, clearing the pte so that |
| 4054 | * we don't have concurrent modification by hardware |
| 4055 | * followed by an update. |
| 4056 | */ |
| 4057 | if (unlikely(pte_none(*vmf->pte))) |
| 4058 | ret = VM_FAULT_SIGBUS; |
| 4059 | else |
| 4060 | ret = VM_FAULT_NOPAGE; |
| 4061 | |
| 4062 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
| 4063 | } |
| 4064 | } else if (!(vmf->flags & FAULT_FLAG_WRITE)) |
Hugh Dickins | b0b9b3d | 2017-01-07 15:37:31 -0800 | [diff] [blame] | 4065 | ret = do_read_fault(vmf); |
| 4066 | else if (!(vma->vm_flags & VM_SHARED)) |
| 4067 | ret = do_cow_fault(vmf); |
| 4068 | else |
| 4069 | ret = do_shared_fault(vmf); |
| 4070 | |
| 4071 | /* preallocated pagetable is unused: free it */ |
| 4072 | if (vmf->prealloc_pte) { |
Jan Stancek | fc8efd2 | 2019-03-05 15:50:08 -0800 | [diff] [blame] | 4073 | pte_free(vm_mm, vmf->prealloc_pte); |
Tobin C Harding | 7f2b6ce | 2017-02-24 14:58:59 -0800 | [diff] [blame] | 4074 | vmf->prealloc_pte = NULL; |
Hugh Dickins | b0b9b3d | 2017-01-07 15:37:31 -0800 | [diff] [blame] | 4075 | } |
| 4076 | return ret; |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 4077 | } |
| 4078 | |
Rashika Kheria | b19a993 | 2014-04-03 14:48:02 -0700 | [diff] [blame] | 4079 | static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 4080 | unsigned long addr, int page_nid, |
| 4081 | int *flags) |
Mel Gorman | 9532fec | 2012-11-15 01:24:32 +0000 | [diff] [blame] | 4082 | { |
| 4083 | get_page(page); |
| 4084 | |
| 4085 | count_vm_numa_event(NUMA_HINT_FAULTS); |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 4086 | if (page_nid == numa_node_id()) { |
Mel Gorman | 9532fec | 2012-11-15 01:24:32 +0000 | [diff] [blame] | 4087 | count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 4088 | *flags |= TNF_FAULT_LOCAL; |
| 4089 | } |
Mel Gorman | 9532fec | 2012-11-15 01:24:32 +0000 | [diff] [blame] | 4090 | |
| 4091 | return mpol_misplaced(page, vma, addr); |
| 4092 | } |
| 4093 | |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 4094 | static vm_fault_t do_numa_page(struct vm_fault *vmf) |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 4095 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4096 | struct vm_area_struct *vma = vmf->vma; |
Mel Gorman | 4daae3b | 2012-11-02 11:33:45 +0000 | [diff] [blame] | 4097 | struct page *page = NULL; |
Anshuman Khandual | 98fa15f | 2019-03-05 15:42:58 -0800 | [diff] [blame] | 4098 | int page_nid = NUMA_NO_NODE; |
Peter Zijlstra | 9057289 | 2013-10-07 11:29:20 +0100 | [diff] [blame] | 4099 | int last_cpupid; |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 4100 | int target_nid; |
Aneesh Kumar K.V | 04a8645 | 2019-03-05 15:46:29 -0800 | [diff] [blame] | 4101 | pte_t pte, old_pte; |
Aneesh Kumar K.V | 288bc54 | 2017-02-24 14:59:16 -0800 | [diff] [blame] | 4102 | bool was_writable = pte_savedwrite(vmf->orig_pte); |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 4103 | int flags = 0; |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 4104 | |
| 4105 | /* |
Tobin C Harding | 166f61b | 2017-02-24 14:59:01 -0800 | [diff] [blame] | 4106 | * The "pte" at this point cannot be used safely without |
| 4107 | * validation through pte_unmap_same(). It's of NUMA type but |
| 4108 | * the pfn may be screwed if the read is non atomic. |
Tobin C Harding | 166f61b | 2017-02-24 14:59:01 -0800 | [diff] [blame] | 4109 | */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4110 | vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd); |
| 4111 | spin_lock(vmf->ptl); |
Aneesh Kumar K.V | cee216a | 2017-02-24 14:59:13 -0800 | [diff] [blame] | 4112 | if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4113 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
Mel Gorman | 4daae3b | 2012-11-02 11:33:45 +0000 | [diff] [blame] | 4114 | goto out; |
| 4115 | } |
| 4116 | |
Aneesh Kumar K.V | cee216a | 2017-02-24 14:59:13 -0800 | [diff] [blame] | 4117 | /* |
| 4118 | * Make it present again, Depending on how arch implementes non |
| 4119 | * accessible ptes, some can allow access by kernel mode. |
| 4120 | */ |
Aneesh Kumar K.V | 04a8645 | 2019-03-05 15:46:29 -0800 | [diff] [blame] | 4121 | old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte); |
| 4122 | pte = pte_modify(old_pte, vma->vm_page_prot); |
Mel Gorman | 4d94246 | 2015-02-12 14:58:28 -0800 | [diff] [blame] | 4123 | pte = pte_mkyoung(pte); |
Mel Gorman | b191f9b | 2015-03-25 15:55:40 -0700 | [diff] [blame] | 4124 | if (was_writable) |
| 4125 | pte = pte_mkwrite(pte); |
Aneesh Kumar K.V | 04a8645 | 2019-03-05 15:46:29 -0800 | [diff] [blame] | 4126 | ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte); |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4127 | update_mmu_cache(vma, vmf->address, vmf->pte); |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 4128 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4129 | page = vm_normal_page(vma, vmf->address, pte); |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 4130 | if (!page) { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4131 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 4132 | return 0; |
| 4133 | } |
| 4134 | |
Kirill A. Shutemov | e81c480 | 2016-01-15 16:53:49 -0800 | [diff] [blame] | 4135 | /* TODO: handle PTE-mapped THP */ |
| 4136 | if (PageCompound(page)) { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4137 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
Kirill A. Shutemov | e81c480 | 2016-01-15 16:53:49 -0800 | [diff] [blame] | 4138 | return 0; |
| 4139 | } |
| 4140 | |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 4141 | /* |
Mel Gorman | bea66fb | 2015-03-25 15:55:37 -0700 | [diff] [blame] | 4142 | * Avoid grouping on RO pages in general. RO pages shouldn't hurt as |
| 4143 | * much anyway since they can be in shared cache state. This misses |
| 4144 | * the case where a mapping is writable but the process never writes |
| 4145 | * to it but pte_write gets cleared during protection updates and |
| 4146 | * pte_dirty has unpredictable behaviour between PTE scan updates, |
| 4147 | * background writeback, dirty balancing and application behaviour. |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 4148 | */ |
Rik van Riel | d59dc7b | 2016-09-08 21:30:53 -0400 | [diff] [blame] | 4149 | if (!pte_write(pte)) |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 4150 | flags |= TNF_NO_GROUP; |
| 4151 | |
Rik van Riel | dabe1d9 | 2013-10-07 11:29:34 +0100 | [diff] [blame] | 4152 | /* |
| 4153 | * Flag if the page is shared between multiple address spaces. This |
| 4154 | * is later used when determining whether to group tasks together |
| 4155 | */ |
| 4156 | if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED)) |
| 4157 | flags |= TNF_SHARED; |
| 4158 | |
Peter Zijlstra | 9057289 | 2013-10-07 11:29:20 +0100 | [diff] [blame] | 4159 | last_cpupid = page_cpupid_last(page); |
Mel Gorman | 8191acb | 2013-10-07 11:28:45 +0100 | [diff] [blame] | 4160 | page_nid = page_to_nid(page); |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4161 | target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid, |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 4162 | &flags); |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4163 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
Anshuman Khandual | 98fa15f | 2019-03-05 15:42:58 -0800 | [diff] [blame] | 4164 | if (target_nid == NUMA_NO_NODE) { |
Mel Gorman | 4daae3b | 2012-11-02 11:33:45 +0000 | [diff] [blame] | 4165 | put_page(page); |
| 4166 | goto out; |
| 4167 | } |
| 4168 | |
| 4169 | /* Migrate to the requested node */ |
Wang Qing | bf90ac1 | 2021-04-29 22:57:07 -0700 | [diff] [blame^] | 4170 | if (migrate_misplaced_page(page, vma, target_nid)) { |
Mel Gorman | 8191acb | 2013-10-07 11:28:45 +0100 | [diff] [blame] | 4171 | page_nid = target_nid; |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 4172 | flags |= TNF_MIGRATED; |
Mel Gorman | 074c238 | 2015-03-25 15:55:42 -0700 | [diff] [blame] | 4173 | } else |
| 4174 | flags |= TNF_MIGRATE_FAIL; |
Mel Gorman | 4daae3b | 2012-11-02 11:33:45 +0000 | [diff] [blame] | 4175 | |
| 4176 | out: |
Anshuman Khandual | 98fa15f | 2019-03-05 15:42:58 -0800 | [diff] [blame] | 4177 | if (page_nid != NUMA_NO_NODE) |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 4178 | task_numa_fault(last_cpupid, page_nid, 1, flags); |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 4179 | return 0; |
| 4180 | } |
| 4181 | |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 4182 | static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf) |
Matthew Wilcox | b96375f | 2015-09-08 14:58:48 -0700 | [diff] [blame] | 4183 | { |
Dave Jiang | f420039 | 2017-02-22 15:40:06 -0800 | [diff] [blame] | 4184 | if (vma_is_anonymous(vmf->vma)) |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4185 | return do_huge_pmd_anonymous_page(vmf); |
Dave Jiang | a2d5816 | 2017-02-24 14:56:59 -0800 | [diff] [blame] | 4186 | if (vmf->vma->vm_ops->huge_fault) |
Dave Jiang | c791ace | 2017-02-24 14:57:08 -0800 | [diff] [blame] | 4187 | return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); |
Matthew Wilcox | b96375f | 2015-09-08 14:58:48 -0700 | [diff] [blame] | 4188 | return VM_FAULT_FALLBACK; |
| 4189 | } |
| 4190 | |
Geert Uytterhoeven | 183f24aa | 2017-12-14 15:32:52 -0800 | [diff] [blame] | 4191 | /* `inline' is required to avoid gcc 4.1.2 build error */ |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 4192 | static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd) |
Matthew Wilcox | b96375f | 2015-09-08 14:58:48 -0700 | [diff] [blame] | 4193 | { |
Andrea Arcangeli | 529b930 | 2020-04-06 20:05:29 -0700 | [diff] [blame] | 4194 | if (vma_is_anonymous(vmf->vma)) { |
Peter Xu | 292924b | 2020-04-06 20:05:49 -0700 | [diff] [blame] | 4195 | if (userfaultfd_huge_pmd_wp(vmf->vma, orig_pmd)) |
Andrea Arcangeli | 529b930 | 2020-04-06 20:05:29 -0700 | [diff] [blame] | 4196 | return handle_userfault(vmf, VM_UFFD_WP); |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4197 | return do_huge_pmd_wp_page(vmf, orig_pmd); |
Andrea Arcangeli | 529b930 | 2020-04-06 20:05:29 -0700 | [diff] [blame] | 4198 | } |
Thomas Hellstrom (VMware) | 327e9fd | 2020-03-24 18:47:47 +0100 | [diff] [blame] | 4199 | if (vmf->vma->vm_ops->huge_fault) { |
| 4200 | vm_fault_t ret = vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); |
Kirill A. Shutemov | af9e4d5 | 2016-07-26 15:25:40 -0700 | [diff] [blame] | 4201 | |
Thomas Hellstrom (VMware) | 327e9fd | 2020-03-24 18:47:47 +0100 | [diff] [blame] | 4202 | if (!(ret & VM_FAULT_FALLBACK)) |
| 4203 | return ret; |
| 4204 | } |
| 4205 | |
| 4206 | /* COW or write-notify handled on pte level: split pmd. */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4207 | __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL); |
Kirill A. Shutemov | af9e4d5 | 2016-07-26 15:25:40 -0700 | [diff] [blame] | 4208 | |
Matthew Wilcox | b96375f | 2015-09-08 14:58:48 -0700 | [diff] [blame] | 4209 | return VM_FAULT_FALLBACK; |
| 4210 | } |
| 4211 | |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 4212 | static vm_fault_t create_huge_pud(struct vm_fault *vmf) |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 4213 | { |
Thomas Hellstrom (VMware) | 327e9fd | 2020-03-24 18:47:47 +0100 | [diff] [blame] | 4214 | #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \ |
| 4215 | defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 4216 | /* No support for anonymous transparent PUD pages yet */ |
| 4217 | if (vma_is_anonymous(vmf->vma)) |
Thomas Hellstrom (VMware) | 327e9fd | 2020-03-24 18:47:47 +0100 | [diff] [blame] | 4218 | goto split; |
| 4219 | if (vmf->vma->vm_ops->huge_fault) { |
| 4220 | vm_fault_t ret = vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); |
| 4221 | |
| 4222 | if (!(ret & VM_FAULT_FALLBACK)) |
| 4223 | return ret; |
| 4224 | } |
| 4225 | split: |
| 4226 | /* COW or write-notify not handled on PUD level: split pud.*/ |
| 4227 | __split_huge_pud(vmf->vma, vmf->pud, vmf->address); |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 4228 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 4229 | return VM_FAULT_FALLBACK; |
| 4230 | } |
| 4231 | |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 4232 | static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud) |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 4233 | { |
| 4234 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 4235 | /* No support for anonymous transparent PUD pages yet */ |
| 4236 | if (vma_is_anonymous(vmf->vma)) |
| 4237 | return VM_FAULT_FALLBACK; |
| 4238 | if (vmf->vma->vm_ops->huge_fault) |
Dave Jiang | c791ace | 2017-02-24 14:57:08 -0800 | [diff] [blame] | 4239 | return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 4240 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 4241 | return VM_FAULT_FALLBACK; |
| 4242 | } |
| 4243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4244 | /* |
| 4245 | * These routines also need to handle stuff like marking pages dirty |
| 4246 | * and/or accessed for architectures that don't do it in hardware (most |
| 4247 | * RISC architectures). The early dirtying is also good on the i386. |
| 4248 | * |
| 4249 | * There is also a hook called "update_mmu_cache()" that architectures |
| 4250 | * with external mmu caches can use to update those (ie the Sparc or |
| 4251 | * PowerPC hashed page tables that act as extended TLBs). |
| 4252 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 4253 | * We enter with non-exclusive mmap_lock (to exclude vma changes, but allow |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 4254 | * concurrent faults). |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 4255 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 4256 | * The mmap_lock may have been released depending on flags and our return value. |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 4257 | * See filemap_fault() and __lock_page_or_retry(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4258 | */ |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 4259 | static vm_fault_t handle_pte_fault(struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | { |
| 4261 | pte_t entry; |
| 4262 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4263 | if (unlikely(pmd_none(*vmf->pmd))) { |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 4264 | /* |
| 4265 | * Leave __pte_alloc() until later: because vm_ops->fault may |
| 4266 | * want to allocate huge page, and if we expose page table |
| 4267 | * for an instant, it will be difficult to retract from |
| 4268 | * concurrent faults and from rmap lookups. |
| 4269 | */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4270 | vmf->pte = NULL; |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 4271 | } else { |
Kirill A. Shutemov | f9ce0be | 2020-12-19 15:19:23 +0300 | [diff] [blame] | 4272 | /* |
| 4273 | * If a huge pmd materialized under us just retry later. Use |
| 4274 | * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead |
| 4275 | * of pmd_trans_huge() to ensure the pmd didn't become |
| 4276 | * pmd_trans_huge under us and then back to pmd_none, as a |
| 4277 | * result of MADV_DONTNEED running immediately after a huge pmd |
| 4278 | * fault in a different thread of this mm, in turn leading to a |
| 4279 | * misleading pmd_trans_huge() retval. All we have to ensure is |
| 4280 | * that it is a regular pmd that we can walk with |
| 4281 | * pte_offset_map() and we can do that through an atomic read |
| 4282 | * in C, which is what pmd_trans_unstable() provides. |
| 4283 | */ |
Ross Zwisler | d0f0931d | 2017-06-02 14:46:34 -0700 | [diff] [blame] | 4284 | if (pmd_devmap_trans_unstable(vmf->pmd)) |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 4285 | return 0; |
| 4286 | /* |
| 4287 | * A regular pmd is established and it can't morph into a huge |
| 4288 | * pmd from under us anymore at this point because we hold the |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 4289 | * mmap_lock read mode and khugepaged takes it in write mode. |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 4290 | * So now it's safe to run pte_offset_map(). |
| 4291 | */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4292 | vmf->pte = pte_offset_map(vmf->pmd, vmf->address); |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 4293 | vmf->orig_pte = *vmf->pte; |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 4294 | |
| 4295 | /* |
| 4296 | * some architectures can have larger ptes than wordsize, |
| 4297 | * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and |
Paul E. McKenney | b03a0fe | 2017-10-23 14:07:25 -0700 | [diff] [blame] | 4298 | * CONFIG_32BIT=y, so READ_ONCE cannot guarantee atomic |
| 4299 | * accesses. The code below just needs a consistent view |
| 4300 | * for the ifs and we later double check anyway with the |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 4301 | * ptl lock held. So here a barrier will do. |
| 4302 | */ |
| 4303 | barrier(); |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 4304 | if (pte_none(vmf->orig_pte)) { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4305 | pte_unmap(vmf->pte); |
| 4306 | vmf->pte = NULL; |
Hugh Dickins | 65500d2 | 2005-10-29 18:15:59 -0700 | [diff] [blame] | 4307 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4308 | } |
| 4309 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4310 | if (!vmf->pte) { |
| 4311 | if (vma_is_anonymous(vmf->vma)) |
| 4312 | return do_anonymous_page(vmf); |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 4313 | else |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4314 | return do_fault(vmf); |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 4315 | } |
| 4316 | |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 4317 | if (!pte_present(vmf->orig_pte)) |
| 4318 | return do_swap_page(vmf); |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 4319 | |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 4320 | if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma)) |
| 4321 | return do_numa_page(vmf); |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 4322 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4323 | vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd); |
| 4324 | spin_lock(vmf->ptl); |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 4325 | entry = vmf->orig_pte; |
Bibo Mao | 7df6769 | 2020-05-27 10:25:18 +0800 | [diff] [blame] | 4326 | if (unlikely(!pte_same(*vmf->pte, entry))) { |
| 4327 | update_mmu_tlb(vmf->vma, vmf->address, vmf->pte); |
Hugh Dickins | 8f4e210 | 2005-10-29 18:16:26 -0700 | [diff] [blame] | 4328 | goto unlock; |
Bibo Mao | 7df6769 | 2020-05-27 10:25:18 +0800 | [diff] [blame] | 4329 | } |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4330 | if (vmf->flags & FAULT_FLAG_WRITE) { |
Linus Torvalds | f6f3732 | 2017-12-15 18:53:22 -0800 | [diff] [blame] | 4331 | if (!pte_write(entry)) |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 4332 | return do_wp_page(vmf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4333 | entry = pte_mkdirty(entry); |
| 4334 | } |
| 4335 | entry = pte_mkyoung(entry); |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4336 | if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry, |
| 4337 | vmf->flags & FAULT_FLAG_WRITE)) { |
| 4338 | update_mmu_cache(vmf->vma, vmf->address, vmf->pte); |
Andrea Arcangeli | 1a44e14 | 2005-10-29 18:16:48 -0700 | [diff] [blame] | 4339 | } else { |
Yang Shi | b7333b5 | 2020-08-14 21:30:41 -0700 | [diff] [blame] | 4340 | /* Skip spurious TLB flush for retried page fault */ |
| 4341 | if (vmf->flags & FAULT_FLAG_TRIED) |
| 4342 | goto unlock; |
Andrea Arcangeli | 1a44e14 | 2005-10-29 18:16:48 -0700 | [diff] [blame] | 4343 | /* |
| 4344 | * This is needed only for protection faults but the arch code |
| 4345 | * is not yet telling us if this is a protection fault or not. |
| 4346 | * This still avoids useless tlb flushes for .text page faults |
| 4347 | * with threads. |
| 4348 | */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4349 | if (vmf->flags & FAULT_FLAG_WRITE) |
| 4350 | flush_tlb_fix_spurious_fault(vmf->vma, vmf->address); |
Andrea Arcangeli | 1a44e14 | 2005-10-29 18:16:48 -0700 | [diff] [blame] | 4351 | } |
Hugh Dickins | 8f4e210 | 2005-10-29 18:16:26 -0700 | [diff] [blame] | 4352 | unlock: |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4353 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 4354 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4355 | } |
| 4356 | |
| 4357 | /* |
| 4358 | * By the time we get here, we already hold the mm semaphore |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 4359 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 4360 | * The mmap_lock may have been released depending on flags and our |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 4361 | * return value. See filemap_fault() and __lock_page_or_retry(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | */ |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 4363 | static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, |
| 4364 | unsigned long address, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4365 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4366 | struct vm_fault vmf = { |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 4367 | .vma = vma, |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 4368 | .address = address & PAGE_MASK, |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 4369 | .flags = flags, |
Jan Kara | 0721ec8 | 2016-12-14 15:07:04 -0800 | [diff] [blame] | 4370 | .pgoff = linear_page_index(vma, address), |
Jan Kara | 667240e | 2016-12-14 15:07:07 -0800 | [diff] [blame] | 4371 | .gfp_mask = __get_fault_gfp_mask(vma), |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 4372 | }; |
Anshuman Khandual | fde26be | 2017-09-08 16:12:45 -0700 | [diff] [blame] | 4373 | unsigned int dirty = flags & FAULT_FLAG_WRITE; |
Kirill A. Shutemov | dcddffd | 2016-07-26 15:25:18 -0700 | [diff] [blame] | 4374 | struct mm_struct *mm = vma->vm_mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4375 | pgd_t *pgd; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 4376 | p4d_t *p4d; |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 4377 | vm_fault_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | pgd = pgd_offset(mm, address); |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 4380 | p4d = p4d_alloc(mm, pgd, address); |
| 4381 | if (!p4d) |
| 4382 | return VM_FAULT_OOM; |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 4383 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 4384 | vmf.pud = pud_alloc(mm, p4d, address); |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 4385 | if (!vmf.pud) |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 4386 | return VM_FAULT_OOM; |
Thomas Hellstrom | 625110b | 2019-11-30 17:51:32 -0800 | [diff] [blame] | 4387 | retry_pud: |
Michal Hocko | 7635d9c | 2018-12-28 00:38:21 -0800 | [diff] [blame] | 4388 | if (pud_none(*vmf.pud) && __transparent_hugepage_enabled(vma)) { |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 4389 | ret = create_huge_pud(&vmf); |
| 4390 | if (!(ret & VM_FAULT_FALLBACK)) |
| 4391 | return ret; |
| 4392 | } else { |
| 4393 | pud_t orig_pud = *vmf.pud; |
| 4394 | |
| 4395 | barrier(); |
| 4396 | if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) { |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 4397 | |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 4398 | /* NUMA case for anonymous PUDs would go here */ |
| 4399 | |
Linus Torvalds | f6f3732 | 2017-12-15 18:53:22 -0800 | [diff] [blame] | 4400 | if (dirty && !pud_write(orig_pud)) { |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 4401 | ret = wp_huge_pud(&vmf, orig_pud); |
| 4402 | if (!(ret & VM_FAULT_FALLBACK)) |
| 4403 | return ret; |
| 4404 | } else { |
| 4405 | huge_pud_set_accessed(&vmf, orig_pud); |
| 4406 | return 0; |
| 4407 | } |
| 4408 | } |
| 4409 | } |
| 4410 | |
| 4411 | vmf.pmd = pmd_alloc(mm, vmf.pud, address); |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4412 | if (!vmf.pmd) |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 4413 | return VM_FAULT_OOM; |
Thomas Hellstrom | 625110b | 2019-11-30 17:51:32 -0800 | [diff] [blame] | 4414 | |
| 4415 | /* Huge pud page fault raced with pmd_alloc? */ |
| 4416 | if (pud_trans_unstable(vmf.pud)) |
| 4417 | goto retry_pud; |
| 4418 | |
Michal Hocko | 7635d9c | 2018-12-28 00:38:21 -0800 | [diff] [blame] | 4419 | if (pmd_none(*vmf.pmd) && __transparent_hugepage_enabled(vma)) { |
Dave Jiang | a2d5816 | 2017-02-24 14:56:59 -0800 | [diff] [blame] | 4420 | ret = create_huge_pmd(&vmf); |
Kirill A. Shutemov | c029255 | 2013-09-12 15:14:05 -0700 | [diff] [blame] | 4421 | if (!(ret & VM_FAULT_FALLBACK)) |
| 4422 | return ret; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 4423 | } else { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4424 | pmd_t orig_pmd = *vmf.pmd; |
David Rientjes | 1f1d06c | 2012-05-29 15:06:23 -0700 | [diff] [blame] | 4425 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 4426 | barrier(); |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 4427 | if (unlikely(is_swap_pmd(orig_pmd))) { |
| 4428 | VM_BUG_ON(thp_migration_supported() && |
| 4429 | !is_pmd_migration_entry(orig_pmd)); |
| 4430 | if (is_pmd_migration_entry(orig_pmd)) |
| 4431 | pmd_migration_entry_wait(mm, vmf.pmd); |
| 4432 | return 0; |
| 4433 | } |
Dan Williams | 5c7fb56 | 2016-01-15 16:56:52 -0800 | [diff] [blame] | 4434 | if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) { |
Lorenzo Stoakes | 38e0885 | 2016-09-11 23:54:25 +0100 | [diff] [blame] | 4435 | if (pmd_protnone(orig_pmd) && vma_is_accessible(vma)) |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4436 | return do_huge_pmd_numa_page(&vmf, orig_pmd); |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 4437 | |
Linus Torvalds | f6f3732 | 2017-12-15 18:53:22 -0800 | [diff] [blame] | 4438 | if (dirty && !pmd_write(orig_pmd)) { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4439 | ret = wp_huge_pmd(&vmf, orig_pmd); |
Kirill A. Shutemov | 9845cbb | 2014-02-25 15:01:42 -0800 | [diff] [blame] | 4440 | if (!(ret & VM_FAULT_FALLBACK)) |
| 4441 | return ret; |
Will Deacon | a1dd450 | 2012-12-11 16:01:27 -0800 | [diff] [blame] | 4442 | } else { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4443 | huge_pmd_set_accessed(&vmf, orig_pmd); |
Kirill A. Shutemov | 9845cbb | 2014-02-25 15:01:42 -0800 | [diff] [blame] | 4444 | return 0; |
David Rientjes | 1f1d06c | 2012-05-29 15:06:23 -0700 | [diff] [blame] | 4445 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 4446 | } |
| 4447 | } |
| 4448 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 4449 | return handle_pte_fault(&vmf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | } |
| 4451 | |
Peter Xu | bce617e | 2020-08-11 18:37:44 -0700 | [diff] [blame] | 4452 | /** |
| 4453 | * mm_account_fault - Do page fault accountings |
| 4454 | * |
| 4455 | * @regs: the pt_regs struct pointer. When set to NULL, will skip accounting |
| 4456 | * of perf event counters, but we'll still do the per-task accounting to |
| 4457 | * the task who triggered this page fault. |
| 4458 | * @address: the faulted address. |
| 4459 | * @flags: the fault flags. |
| 4460 | * @ret: the fault retcode. |
| 4461 | * |
| 4462 | * This will take care of most of the page fault accountings. Meanwhile, it |
| 4463 | * will also include the PERF_COUNT_SW_PAGE_FAULTS_[MAJ|MIN] perf counter |
| 4464 | * updates. However note that the handling of PERF_COUNT_SW_PAGE_FAULTS should |
| 4465 | * still be in per-arch page fault handlers at the entry of page fault. |
| 4466 | */ |
| 4467 | static inline void mm_account_fault(struct pt_regs *regs, |
| 4468 | unsigned long address, unsigned int flags, |
| 4469 | vm_fault_t ret) |
| 4470 | { |
| 4471 | bool major; |
| 4472 | |
| 4473 | /* |
| 4474 | * We don't do accounting for some specific faults: |
| 4475 | * |
| 4476 | * - Unsuccessful faults (e.g. when the address wasn't valid). That |
| 4477 | * includes arch_vma_access_permitted() failing before reaching here. |
| 4478 | * So this is not a "this many hardware page faults" counter. We |
| 4479 | * should use the hw profiling for that. |
| 4480 | * |
| 4481 | * - Incomplete faults (VM_FAULT_RETRY). They will only be counted |
| 4482 | * once they're completed. |
| 4483 | */ |
| 4484 | if (ret & (VM_FAULT_ERROR | VM_FAULT_RETRY)) |
| 4485 | return; |
| 4486 | |
| 4487 | /* |
| 4488 | * We define the fault as a major fault when the final successful fault |
| 4489 | * is VM_FAULT_MAJOR, or if it retried (which implies that we couldn't |
| 4490 | * handle it immediately previously). |
| 4491 | */ |
| 4492 | major = (ret & VM_FAULT_MAJOR) || (flags & FAULT_FLAG_TRIED); |
| 4493 | |
Peter Xu | a2beb5f | 2020-08-11 18:38:57 -0700 | [diff] [blame] | 4494 | if (major) |
| 4495 | current->maj_flt++; |
| 4496 | else |
| 4497 | current->min_flt++; |
| 4498 | |
Peter Xu | bce617e | 2020-08-11 18:37:44 -0700 | [diff] [blame] | 4499 | /* |
Peter Xu | a2beb5f | 2020-08-11 18:38:57 -0700 | [diff] [blame] | 4500 | * If the fault is done for GUP, regs will be NULL. We only do the |
| 4501 | * accounting for the per thread fault counters who triggered the |
| 4502 | * fault, and we skip the perf event updates. |
Peter Xu | bce617e | 2020-08-11 18:37:44 -0700 | [diff] [blame] | 4503 | */ |
| 4504 | if (!regs) |
| 4505 | return; |
| 4506 | |
Peter Xu | a2beb5f | 2020-08-11 18:38:57 -0700 | [diff] [blame] | 4507 | if (major) |
Peter Xu | bce617e | 2020-08-11 18:37:44 -0700 | [diff] [blame] | 4508 | perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address); |
Peter Xu | a2beb5f | 2020-08-11 18:38:57 -0700 | [diff] [blame] | 4509 | else |
Peter Xu | bce617e | 2020-08-11 18:37:44 -0700 | [diff] [blame] | 4510 | perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address); |
Peter Xu | bce617e | 2020-08-11 18:37:44 -0700 | [diff] [blame] | 4511 | } |
| 4512 | |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 4513 | /* |
| 4514 | * By the time we get here, we already hold the mm semaphore |
| 4515 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 4516 | * The mmap_lock may have been released depending on flags and our |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 4517 | * return value. See filemap_fault() and __lock_page_or_retry(). |
| 4518 | */ |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 4519 | vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, |
Peter Xu | bce617e | 2020-08-11 18:37:44 -0700 | [diff] [blame] | 4520 | unsigned int flags, struct pt_regs *regs) |
Johannes Weiner | 519e524 | 2013-09-12 15:13:42 -0700 | [diff] [blame] | 4521 | { |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 4522 | vm_fault_t ret; |
Johannes Weiner | 519e524 | 2013-09-12 15:13:42 -0700 | [diff] [blame] | 4523 | |
| 4524 | __set_current_state(TASK_RUNNING); |
| 4525 | |
| 4526 | count_vm_event(PGFAULT); |
Roman Gushchin | 2262185 | 2017-07-06 15:40:25 -0700 | [diff] [blame] | 4527 | count_memcg_event_mm(vma->vm_mm, PGFAULT); |
Johannes Weiner | 519e524 | 2013-09-12 15:13:42 -0700 | [diff] [blame] | 4528 | |
| 4529 | /* do counter updates before entering really critical section. */ |
| 4530 | check_sync_rss_stat(current); |
| 4531 | |
Laurent Dufour | de0c799 | 2017-09-08 16:13:12 -0700 | [diff] [blame] | 4532 | if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE, |
| 4533 | flags & FAULT_FLAG_INSTRUCTION, |
| 4534 | flags & FAULT_FLAG_REMOTE)) |
| 4535 | return VM_FAULT_SIGSEGV; |
| 4536 | |
Johannes Weiner | 519e524 | 2013-09-12 15:13:42 -0700 | [diff] [blame] | 4537 | /* |
| 4538 | * Enable the memcg OOM handling for faults triggered in user |
| 4539 | * space. Kernel faults are handled more gracefully. |
| 4540 | */ |
| 4541 | if (flags & FAULT_FLAG_USER) |
Michal Hocko | 29ef680 | 2018-08-17 15:47:11 -0700 | [diff] [blame] | 4542 | mem_cgroup_enter_user_fault(); |
Johannes Weiner | 519e524 | 2013-09-12 15:13:42 -0700 | [diff] [blame] | 4543 | |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 4544 | if (unlikely(is_vm_hugetlb_page(vma))) |
| 4545 | ret = hugetlb_fault(vma->vm_mm, vma, address, flags); |
| 4546 | else |
| 4547 | ret = __handle_mm_fault(vma, address, flags); |
Johannes Weiner | 519e524 | 2013-09-12 15:13:42 -0700 | [diff] [blame] | 4548 | |
Johannes Weiner | 4942642 | 2013-10-16 13:46:59 -0700 | [diff] [blame] | 4549 | if (flags & FAULT_FLAG_USER) { |
Michal Hocko | 29ef680 | 2018-08-17 15:47:11 -0700 | [diff] [blame] | 4550 | mem_cgroup_exit_user_fault(); |
Tobin C Harding | 166f61b | 2017-02-24 14:59:01 -0800 | [diff] [blame] | 4551 | /* |
| 4552 | * The task may have entered a memcg OOM situation but |
| 4553 | * if the allocation error was handled gracefully (no |
| 4554 | * VM_FAULT_OOM), there is no need to kill anything. |
| 4555 | * Just clean up the OOM state peacefully. |
| 4556 | */ |
| 4557 | if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM)) |
| 4558 | mem_cgroup_oom_synchronize(false); |
Johannes Weiner | 4942642 | 2013-10-16 13:46:59 -0700 | [diff] [blame] | 4559 | } |
Johannes Weiner | 3812c8c | 2013-09-12 15:13:44 -0700 | [diff] [blame] | 4560 | |
Peter Xu | bce617e | 2020-08-11 18:37:44 -0700 | [diff] [blame] | 4561 | mm_account_fault(regs, address, flags, ret); |
| 4562 | |
Johannes Weiner | 519e524 | 2013-09-12 15:13:42 -0700 | [diff] [blame] | 4563 | return ret; |
| 4564 | } |
Jesse Barnes | e1d6d01 | 2014-12-12 16:55:27 -0800 | [diff] [blame] | 4565 | EXPORT_SYMBOL_GPL(handle_mm_fault); |
Johannes Weiner | 519e524 | 2013-09-12 15:13:42 -0700 | [diff] [blame] | 4566 | |
Kirill A. Shutemov | 90eceff | 2017-03-09 17:24:08 +0300 | [diff] [blame] | 4567 | #ifndef __PAGETABLE_P4D_FOLDED |
| 4568 | /* |
| 4569 | * Allocate p4d page table. |
| 4570 | * We've already handled the fast-path in-line. |
| 4571 | */ |
| 4572 | int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) |
| 4573 | { |
| 4574 | p4d_t *new = p4d_alloc_one(mm, address); |
| 4575 | if (!new) |
| 4576 | return -ENOMEM; |
| 4577 | |
| 4578 | smp_wmb(); /* See comment in __pte_alloc */ |
| 4579 | |
| 4580 | spin_lock(&mm->page_table_lock); |
| 4581 | if (pgd_present(*pgd)) /* Another has populated it */ |
| 4582 | p4d_free(mm, new); |
| 4583 | else |
| 4584 | pgd_populate(mm, pgd, new); |
| 4585 | spin_unlock(&mm->page_table_lock); |
| 4586 | return 0; |
| 4587 | } |
| 4588 | #endif /* __PAGETABLE_P4D_FOLDED */ |
| 4589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | #ifndef __PAGETABLE_PUD_FOLDED |
| 4591 | /* |
| 4592 | * Allocate page upper directory. |
Hugh Dickins | 872fec1 | 2005-10-29 18:16:21 -0700 | [diff] [blame] | 4593 | * We've already handled the fast-path in-line. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4594 | */ |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 4595 | int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4596 | { |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 4597 | pud_t *new = pud_alloc_one(mm, address); |
| 4598 | if (!new) |
Hugh Dickins | 1bb3630 | 2005-10-29 18:16:22 -0700 | [diff] [blame] | 4599 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | |
Nick Piggin | 362a61a | 2008-05-14 06:37:36 +0200 | [diff] [blame] | 4601 | smp_wmb(); /* See comment in __pte_alloc */ |
| 4602 | |
Hugh Dickins | 872fec1 | 2005-10-29 18:16:21 -0700 | [diff] [blame] | 4603 | spin_lock(&mm->page_table_lock); |
Kirill A. Shutemov | b4e98d9 | 2017-11-15 17:35:33 -0800 | [diff] [blame] | 4604 | if (!p4d_present(*p4d)) { |
| 4605 | mm_inc_nr_puds(mm); |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 4606 | p4d_populate(mm, p4d, new); |
Kirill A. Shutemov | b4e98d9 | 2017-11-15 17:35:33 -0800 | [diff] [blame] | 4607 | } else /* Another has populated it */ |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 4608 | pud_free(mm, new); |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 4609 | spin_unlock(&mm->page_table_lock); |
Hugh Dickins | 1bb3630 | 2005-10-29 18:16:22 -0700 | [diff] [blame] | 4610 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | } |
| 4612 | #endif /* __PAGETABLE_PUD_FOLDED */ |
| 4613 | |
| 4614 | #ifndef __PAGETABLE_PMD_FOLDED |
| 4615 | /* |
| 4616 | * Allocate page middle directory. |
Hugh Dickins | 872fec1 | 2005-10-29 18:16:21 -0700 | [diff] [blame] | 4617 | * We've already handled the fast-path in-line. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | */ |
Hugh Dickins | 1bb3630 | 2005-10-29 18:16:22 -0700 | [diff] [blame] | 4619 | int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4620 | { |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 4621 | spinlock_t *ptl; |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 4622 | pmd_t *new = pmd_alloc_one(mm, address); |
| 4623 | if (!new) |
Hugh Dickins | 1bb3630 | 2005-10-29 18:16:22 -0700 | [diff] [blame] | 4624 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | |
Nick Piggin | 362a61a | 2008-05-14 06:37:36 +0200 | [diff] [blame] | 4626 | smp_wmb(); /* See comment in __pte_alloc */ |
| 4627 | |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 4628 | ptl = pud_lock(mm, pud); |
Kirill A. Shutemov | dc6c9a3 | 2015-02-11 15:26:50 -0800 | [diff] [blame] | 4629 | if (!pud_present(*pud)) { |
| 4630 | mm_inc_nr_pmds(mm); |
Hugh Dickins | 1bb3630 | 2005-10-29 18:16:22 -0700 | [diff] [blame] | 4631 | pud_populate(mm, pud, new); |
Kirill A. Shutemov | dc6c9a3 | 2015-02-11 15:26:50 -0800 | [diff] [blame] | 4632 | } else /* Another has populated it */ |
Benjamin Herrenschmidt | 5e54197 | 2008-02-04 22:29:14 -0800 | [diff] [blame] | 4633 | pmd_free(mm, new); |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 4634 | spin_unlock(ptl); |
Hugh Dickins | 1bb3630 | 2005-10-29 18:16:22 -0700 | [diff] [blame] | 4635 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | } |
| 4637 | #endif /* __PAGETABLE_PMD_FOLDED */ |
| 4638 | |
Paolo Bonzini | 9fd6dad | 2021-02-05 05:07:11 -0500 | [diff] [blame] | 4639 | int follow_invalidate_pte(struct mm_struct *mm, unsigned long address, |
| 4640 | struct mmu_notifier_range *range, pte_t **ptepp, |
| 4641 | pmd_t **pmdpp, spinlock_t **ptlp) |
Johannes Weiner | f8ad0f49 | 2009-06-16 15:32:33 -0700 | [diff] [blame] | 4642 | { |
| 4643 | pgd_t *pgd; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 4644 | p4d_t *p4d; |
Johannes Weiner | f8ad0f49 | 2009-06-16 15:32:33 -0700 | [diff] [blame] | 4645 | pud_t *pud; |
| 4646 | pmd_t *pmd; |
| 4647 | pte_t *ptep; |
| 4648 | |
| 4649 | pgd = pgd_offset(mm, address); |
| 4650 | if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) |
| 4651 | goto out; |
| 4652 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 4653 | p4d = p4d_offset(pgd, address); |
| 4654 | if (p4d_none(*p4d) || unlikely(p4d_bad(*p4d))) |
| 4655 | goto out; |
| 4656 | |
| 4657 | pud = pud_offset(p4d, address); |
Johannes Weiner | f8ad0f49 | 2009-06-16 15:32:33 -0700 | [diff] [blame] | 4658 | if (pud_none(*pud) || unlikely(pud_bad(*pud))) |
| 4659 | goto out; |
| 4660 | |
| 4661 | pmd = pmd_offset(pud, address); |
Andrea Arcangeli | f66055ab | 2011-01-13 15:46:54 -0800 | [diff] [blame] | 4662 | VM_BUG_ON(pmd_trans_huge(*pmd)); |
Johannes Weiner | f8ad0f49 | 2009-06-16 15:32:33 -0700 | [diff] [blame] | 4663 | |
Ross Zwisler | 0979639 | 2017-01-10 16:57:21 -0800 | [diff] [blame] | 4664 | if (pmd_huge(*pmd)) { |
| 4665 | if (!pmdpp) |
| 4666 | goto out; |
| 4667 | |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 4668 | if (range) { |
Jérôme Glisse | 7269f99 | 2019-05-13 17:20:53 -0700 | [diff] [blame] | 4669 | mmu_notifier_range_init(range, MMU_NOTIFY_CLEAR, 0, |
Jérôme Glisse | 6f4f13e | 2019-05-13 17:20:49 -0700 | [diff] [blame] | 4670 | NULL, mm, address & PMD_MASK, |
| 4671 | (address & PMD_MASK) + PMD_SIZE); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 4672 | mmu_notifier_invalidate_range_start(range); |
Jérôme Glisse | a4d1a88 | 2017-08-31 17:17:26 -0400 | [diff] [blame] | 4673 | } |
Ross Zwisler | 0979639 | 2017-01-10 16:57:21 -0800 | [diff] [blame] | 4674 | *ptlp = pmd_lock(mm, pmd); |
| 4675 | if (pmd_huge(*pmd)) { |
| 4676 | *pmdpp = pmd; |
| 4677 | return 0; |
| 4678 | } |
| 4679 | spin_unlock(*ptlp); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 4680 | if (range) |
| 4681 | mmu_notifier_invalidate_range_end(range); |
Ross Zwisler | 0979639 | 2017-01-10 16:57:21 -0800 | [diff] [blame] | 4682 | } |
| 4683 | |
| 4684 | if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) |
Johannes Weiner | f8ad0f49 | 2009-06-16 15:32:33 -0700 | [diff] [blame] | 4685 | goto out; |
| 4686 | |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 4687 | if (range) { |
Jérôme Glisse | 7269f99 | 2019-05-13 17:20:53 -0700 | [diff] [blame] | 4688 | mmu_notifier_range_init(range, MMU_NOTIFY_CLEAR, 0, NULL, mm, |
Jérôme Glisse | 6f4f13e | 2019-05-13 17:20:49 -0700 | [diff] [blame] | 4689 | address & PAGE_MASK, |
| 4690 | (address & PAGE_MASK) + PAGE_SIZE); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 4691 | mmu_notifier_invalidate_range_start(range); |
Jérôme Glisse | a4d1a88 | 2017-08-31 17:17:26 -0400 | [diff] [blame] | 4692 | } |
Johannes Weiner | f8ad0f49 | 2009-06-16 15:32:33 -0700 | [diff] [blame] | 4693 | ptep = pte_offset_map_lock(mm, pmd, address, ptlp); |
Johannes Weiner | f8ad0f49 | 2009-06-16 15:32:33 -0700 | [diff] [blame] | 4694 | if (!pte_present(*ptep)) |
| 4695 | goto unlock; |
| 4696 | *ptepp = ptep; |
| 4697 | return 0; |
| 4698 | unlock: |
| 4699 | pte_unmap_unlock(ptep, *ptlp); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 4700 | if (range) |
| 4701 | mmu_notifier_invalidate_range_end(range); |
Johannes Weiner | f8ad0f49 | 2009-06-16 15:32:33 -0700 | [diff] [blame] | 4702 | out: |
| 4703 | return -EINVAL; |
| 4704 | } |
| 4705 | |
Johannes Weiner | 3b6748e | 2009-06-16 15:32:35 -0700 | [diff] [blame] | 4706 | /** |
Paolo Bonzini | 9fd6dad | 2021-02-05 05:07:11 -0500 | [diff] [blame] | 4707 | * follow_pte - look up PTE at a user virtual address |
| 4708 | * @mm: the mm_struct of the target address space |
| 4709 | * @address: user virtual address |
| 4710 | * @ptepp: location to store found PTE |
| 4711 | * @ptlp: location to store the lock for the PTE |
| 4712 | * |
| 4713 | * On a successful return, the pointer to the PTE is stored in @ptepp; |
| 4714 | * the corresponding lock is taken and its location is stored in @ptlp. |
| 4715 | * The contents of the PTE are only stable until @ptlp is released; |
| 4716 | * any further use, if any, must be protected against invalidation |
| 4717 | * with MMU notifiers. |
| 4718 | * |
| 4719 | * Only IO mappings and raw PFN mappings are allowed. The mmap semaphore |
| 4720 | * should be taken for read. |
| 4721 | * |
| 4722 | * KVM uses this function. While it is arguably less bad than ``follow_pfn``, |
| 4723 | * it is not a good general-purpose API. |
| 4724 | * |
| 4725 | * Return: zero on success, -ve otherwise. |
| 4726 | */ |
| 4727 | int follow_pte(struct mm_struct *mm, unsigned long address, |
| 4728 | pte_t **ptepp, spinlock_t **ptlp) |
| 4729 | { |
| 4730 | return follow_invalidate_pte(mm, address, NULL, ptepp, NULL, ptlp); |
| 4731 | } |
| 4732 | EXPORT_SYMBOL_GPL(follow_pte); |
| 4733 | |
| 4734 | /** |
Johannes Weiner | 3b6748e | 2009-06-16 15:32:35 -0700 | [diff] [blame] | 4735 | * follow_pfn - look up PFN at a user virtual address |
| 4736 | * @vma: memory mapping |
| 4737 | * @address: user virtual address |
| 4738 | * @pfn: location to store found PFN |
| 4739 | * |
| 4740 | * Only IO mappings and raw PFN mappings are allowed. |
| 4741 | * |
Paolo Bonzini | 9fd6dad | 2021-02-05 05:07:11 -0500 | [diff] [blame] | 4742 | * This function does not allow the caller to read the permissions |
| 4743 | * of the PTE. Do not use it. |
| 4744 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 4745 | * Return: zero and the pfn at @pfn on success, -ve otherwise. |
Johannes Weiner | 3b6748e | 2009-06-16 15:32:35 -0700 | [diff] [blame] | 4746 | */ |
| 4747 | int follow_pfn(struct vm_area_struct *vma, unsigned long address, |
| 4748 | unsigned long *pfn) |
| 4749 | { |
| 4750 | int ret = -EINVAL; |
| 4751 | spinlock_t *ptl; |
| 4752 | pte_t *ptep; |
| 4753 | |
| 4754 | if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) |
| 4755 | return ret; |
| 4756 | |
Paolo Bonzini | 9fd6dad | 2021-02-05 05:07:11 -0500 | [diff] [blame] | 4757 | ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); |
Johannes Weiner | 3b6748e | 2009-06-16 15:32:35 -0700 | [diff] [blame] | 4758 | if (ret) |
| 4759 | return ret; |
| 4760 | *pfn = pte_pfn(*ptep); |
| 4761 | pte_unmap_unlock(ptep, ptl); |
| 4762 | return 0; |
| 4763 | } |
| 4764 | EXPORT_SYMBOL(follow_pfn); |
| 4765 | |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4766 | #ifdef CONFIG_HAVE_IOREMAP_PROT |
venkatesh.pallipadi@intel.com | d87fe66 | 2008-12-19 13:47:27 -0800 | [diff] [blame] | 4767 | int follow_phys(struct vm_area_struct *vma, |
| 4768 | unsigned long address, unsigned int flags, |
| 4769 | unsigned long *prot, resource_size_t *phys) |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4770 | { |
Johannes Weiner | 03668a4 | 2009-06-16 15:32:34 -0700 | [diff] [blame] | 4771 | int ret = -EINVAL; |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4772 | pte_t *ptep, pte; |
| 4773 | spinlock_t *ptl; |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4774 | |
venkatesh.pallipadi@intel.com | d87fe66 | 2008-12-19 13:47:27 -0800 | [diff] [blame] | 4775 | if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) |
| 4776 | goto out; |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4777 | |
Paolo Bonzini | 9fd6dad | 2021-02-05 05:07:11 -0500 | [diff] [blame] | 4778 | if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) |
venkatesh.pallipadi@intel.com | d87fe66 | 2008-12-19 13:47:27 -0800 | [diff] [blame] | 4779 | goto out; |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4780 | pte = *ptep; |
Johannes Weiner | 03668a4 | 2009-06-16 15:32:34 -0700 | [diff] [blame] | 4781 | |
Linus Torvalds | f6f3732 | 2017-12-15 18:53:22 -0800 | [diff] [blame] | 4782 | if ((flags & FOLL_WRITE) && !pte_write(pte)) |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4783 | goto unlock; |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4784 | |
| 4785 | *prot = pgprot_val(pte_pgprot(pte)); |
Johannes Weiner | 03668a4 | 2009-06-16 15:32:34 -0700 | [diff] [blame] | 4786 | *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4787 | |
Johannes Weiner | 03668a4 | 2009-06-16 15:32:34 -0700 | [diff] [blame] | 4788 | ret = 0; |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4789 | unlock: |
| 4790 | pte_unmap_unlock(ptep, ptl); |
| 4791 | out: |
venkatesh.pallipadi@intel.com | d87fe66 | 2008-12-19 13:47:27 -0800 | [diff] [blame] | 4792 | return ret; |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4793 | } |
| 4794 | |
Daniel Vetter | 96667f8 | 2020-11-27 17:41:21 +0100 | [diff] [blame] | 4795 | /** |
| 4796 | * generic_access_phys - generic implementation for iomem mmap access |
| 4797 | * @vma: the vma to access |
| 4798 | * @addr: userspace addres, not relative offset within @vma |
| 4799 | * @buf: buffer to read/write |
| 4800 | * @len: length of transfer |
| 4801 | * @write: set to FOLL_WRITE when writing, otherwise reading |
| 4802 | * |
| 4803 | * This is a generic implementation for &vm_operations_struct.access for an |
| 4804 | * iomem mapping. This callback is used by access_process_vm() when the @vma is |
| 4805 | * not page based. |
| 4806 | */ |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4807 | int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, |
| 4808 | void *buf, int len, int write) |
| 4809 | { |
| 4810 | resource_size_t phys_addr; |
| 4811 | unsigned long prot = 0; |
KOSAKI Motohiro | 2bc7273 | 2009-01-06 14:39:43 -0800 | [diff] [blame] | 4812 | void __iomem *maddr; |
Daniel Vetter | 96667f8 | 2020-11-27 17:41:21 +0100 | [diff] [blame] | 4813 | pte_t *ptep, pte; |
| 4814 | spinlock_t *ptl; |
| 4815 | int offset = offset_in_page(addr); |
| 4816 | int ret = -EINVAL; |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4817 | |
Daniel Vetter | 96667f8 | 2020-11-27 17:41:21 +0100 | [diff] [blame] | 4818 | if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) |
| 4819 | return -EINVAL; |
| 4820 | |
| 4821 | retry: |
Linus Torvalds | e913a8c | 2021-02-22 17:45:02 -0800 | [diff] [blame] | 4822 | if (follow_pte(vma->vm_mm, addr, &ptep, &ptl)) |
Daniel Vetter | 96667f8 | 2020-11-27 17:41:21 +0100 | [diff] [blame] | 4823 | return -EINVAL; |
| 4824 | pte = *ptep; |
| 4825 | pte_unmap_unlock(ptep, ptl); |
| 4826 | |
| 4827 | prot = pgprot_val(pte_pgprot(pte)); |
| 4828 | phys_addr = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; |
| 4829 | |
| 4830 | if ((write & FOLL_WRITE) && !pte_write(pte)) |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4831 | return -EINVAL; |
| 4832 | |
Grazvydas Ignotas | 9cb12d7 | 2015-02-12 15:00:19 -0800 | [diff] [blame] | 4833 | maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot); |
jie@chenjie6@huwei.com | 24eee1e | 2018-08-10 17:23:06 -0700 | [diff] [blame] | 4834 | if (!maddr) |
| 4835 | return -ENOMEM; |
| 4836 | |
Linus Torvalds | e913a8c | 2021-02-22 17:45:02 -0800 | [diff] [blame] | 4837 | if (follow_pte(vma->vm_mm, addr, &ptep, &ptl)) |
Daniel Vetter | 96667f8 | 2020-11-27 17:41:21 +0100 | [diff] [blame] | 4838 | goto out_unmap; |
| 4839 | |
| 4840 | if (!pte_same(pte, *ptep)) { |
| 4841 | pte_unmap_unlock(ptep, ptl); |
| 4842 | iounmap(maddr); |
| 4843 | |
| 4844 | goto retry; |
| 4845 | } |
| 4846 | |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4847 | if (write) |
| 4848 | memcpy_toio(maddr + offset, buf, len); |
| 4849 | else |
| 4850 | memcpy_fromio(buf, maddr + offset, len); |
Daniel Vetter | 96667f8 | 2020-11-27 17:41:21 +0100 | [diff] [blame] | 4851 | ret = len; |
| 4852 | pte_unmap_unlock(ptep, ptl); |
| 4853 | out_unmap: |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4854 | iounmap(maddr); |
| 4855 | |
Daniel Vetter | 96667f8 | 2020-11-27 17:41:21 +0100 | [diff] [blame] | 4856 | return ret; |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4857 | } |
Uwe Kleine-König | 5a73633 | 2013-08-07 13:02:52 +0200 | [diff] [blame] | 4858 | EXPORT_SYMBOL_GPL(generic_access_phys); |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4859 | #endif |
| 4860 | |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 4861 | /* |
John Hubbard | d3f5ffc | 2020-12-14 19:07:45 -0800 | [diff] [blame] | 4862 | * Access another process' address space as given in mm. |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 4863 | */ |
John Hubbard | d3f5ffc | 2020-12-14 19:07:45 -0800 | [diff] [blame] | 4864 | int __access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf, |
| 4865 | int len, unsigned int gup_flags) |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 4866 | { |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 4867 | struct vm_area_struct *vma; |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 4868 | void *old_buf = buf; |
Lorenzo Stoakes | 442486e | 2016-10-13 01:20:18 +0100 | [diff] [blame] | 4869 | int write = gup_flags & FOLL_WRITE; |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 4870 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 4871 | if (mmap_read_lock_killable(mm)) |
Konstantin Khlebnikov | 1e426fe | 2019-07-11 21:00:07 -0700 | [diff] [blame] | 4872 | return 0; |
| 4873 | |
Simon Arlott | 183ff22 | 2007-10-20 01:27:18 +0200 | [diff] [blame] | 4874 | /* ignore errors, just check how much was successfully transferred */ |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 4875 | while (len) { |
| 4876 | int bytes, ret, offset; |
| 4877 | void *maddr; |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4878 | struct page *page = NULL; |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 4879 | |
Peter Xu | 64019a2 | 2020-08-11 18:39:01 -0700 | [diff] [blame] | 4880 | ret = get_user_pages_remote(mm, addr, 1, |
Lorenzo Stoakes | 5b56d49 | 2016-12-14 15:06:52 -0800 | [diff] [blame] | 4881 | gup_flags, &page, &vma, NULL); |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4882 | if (ret <= 0) { |
Rik van Riel | dbffcd0 | 2014-08-06 16:08:12 -0700 | [diff] [blame] | 4883 | #ifndef CONFIG_HAVE_IOREMAP_PROT |
| 4884 | break; |
| 4885 | #else |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4886 | /* |
| 4887 | * Check if this is a VM_IO | VM_PFNMAP VMA, which |
| 4888 | * we can access using slightly different code. |
| 4889 | */ |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4890 | vma = find_vma(mm, addr); |
Michael Ellerman | fe936df | 2011-04-14 15:22:10 -0700 | [diff] [blame] | 4891 | if (!vma || vma->vm_start > addr) |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4892 | break; |
| 4893 | if (vma->vm_ops && vma->vm_ops->access) |
| 4894 | ret = vma->vm_ops->access(vma, addr, buf, |
| 4895 | len, write); |
| 4896 | if (ret <= 0) |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4897 | break; |
| 4898 | bytes = ret; |
Rik van Riel | dbffcd0 | 2014-08-06 16:08:12 -0700 | [diff] [blame] | 4899 | #endif |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 4900 | } else { |
Rik van Riel | 28b2ee2 | 2008-07-23 21:27:05 -0700 | [diff] [blame] | 4901 | bytes = len; |
| 4902 | offset = addr & (PAGE_SIZE-1); |
| 4903 | if (bytes > PAGE_SIZE-offset) |
| 4904 | bytes = PAGE_SIZE-offset; |
| 4905 | |
| 4906 | maddr = kmap(page); |
| 4907 | if (write) { |
| 4908 | copy_to_user_page(vma, page, addr, |
| 4909 | maddr + offset, buf, bytes); |
| 4910 | set_page_dirty_lock(page); |
| 4911 | } else { |
| 4912 | copy_from_user_page(vma, page, addr, |
| 4913 | buf, maddr + offset, bytes); |
| 4914 | } |
| 4915 | kunmap(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4916 | put_page(page); |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 4917 | } |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 4918 | len -= bytes; |
| 4919 | buf += bytes; |
| 4920 | addr += bytes; |
| 4921 | } |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 4922 | mmap_read_unlock(mm); |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 4923 | |
| 4924 | return buf - old_buf; |
| 4925 | } |
Andi Kleen | 0325291 | 2008-01-30 13:33:18 +0100 | [diff] [blame] | 4926 | |
Stephen Wilson | 5ddd36b | 2011-03-13 15:49:20 -0400 | [diff] [blame] | 4927 | /** |
Randy Dunlap | ae91dbf | 2011-03-26 13:27:01 -0700 | [diff] [blame] | 4928 | * access_remote_vm - access another process' address space |
Stephen Wilson | 5ddd36b | 2011-03-13 15:49:20 -0400 | [diff] [blame] | 4929 | * @mm: the mm_struct of the target address space |
| 4930 | * @addr: start address to access |
| 4931 | * @buf: source or destination buffer |
| 4932 | * @len: number of bytes to transfer |
Lorenzo Stoakes | 6347e8d | 2016-10-13 01:20:19 +0100 | [diff] [blame] | 4933 | * @gup_flags: flags modifying lookup behaviour |
Stephen Wilson | 5ddd36b | 2011-03-13 15:49:20 -0400 | [diff] [blame] | 4934 | * |
| 4935 | * The caller must hold a reference on @mm. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 4936 | * |
| 4937 | * Return: number of bytes copied from source to destination. |
Stephen Wilson | 5ddd36b | 2011-03-13 15:49:20 -0400 | [diff] [blame] | 4938 | */ |
| 4939 | int access_remote_vm(struct mm_struct *mm, unsigned long addr, |
Lorenzo Stoakes | 6347e8d | 2016-10-13 01:20:19 +0100 | [diff] [blame] | 4940 | void *buf, int len, unsigned int gup_flags) |
Stephen Wilson | 5ddd36b | 2011-03-13 15:49:20 -0400 | [diff] [blame] | 4941 | { |
John Hubbard | d3f5ffc | 2020-12-14 19:07:45 -0800 | [diff] [blame] | 4942 | return __access_remote_vm(mm, addr, buf, len, gup_flags); |
Stephen Wilson | 5ddd36b | 2011-03-13 15:49:20 -0400 | [diff] [blame] | 4943 | } |
| 4944 | |
Andi Kleen | 0325291 | 2008-01-30 13:33:18 +0100 | [diff] [blame] | 4945 | /* |
Stephen Wilson | 206cb63 | 2011-03-13 15:49:19 -0400 | [diff] [blame] | 4946 | * Access another process' address space. |
| 4947 | * Source/target buffer must be kernel space, |
| 4948 | * Do not walk the page table directly, use get_user_pages |
| 4949 | */ |
| 4950 | int access_process_vm(struct task_struct *tsk, unsigned long addr, |
Lorenzo Stoakes | f307ab6 | 2016-10-13 01:20:20 +0100 | [diff] [blame] | 4951 | void *buf, int len, unsigned int gup_flags) |
Stephen Wilson | 206cb63 | 2011-03-13 15:49:19 -0400 | [diff] [blame] | 4952 | { |
| 4953 | struct mm_struct *mm; |
| 4954 | int ret; |
| 4955 | |
| 4956 | mm = get_task_mm(tsk); |
| 4957 | if (!mm) |
| 4958 | return 0; |
| 4959 | |
John Hubbard | d3f5ffc | 2020-12-14 19:07:45 -0800 | [diff] [blame] | 4960 | ret = __access_remote_vm(mm, addr, buf, len, gup_flags); |
Lorenzo Stoakes | 442486e | 2016-10-13 01:20:18 +0100 | [diff] [blame] | 4961 | |
Stephen Wilson | 206cb63 | 2011-03-13 15:49:19 -0400 | [diff] [blame] | 4962 | mmput(mm); |
| 4963 | |
| 4964 | return ret; |
| 4965 | } |
Catalin Marinas | fcd3585 | 2016-11-01 14:43:25 -0700 | [diff] [blame] | 4966 | EXPORT_SYMBOL_GPL(access_process_vm); |
Stephen Wilson | 206cb63 | 2011-03-13 15:49:19 -0400 | [diff] [blame] | 4967 | |
Andi Kleen | 0325291 | 2008-01-30 13:33:18 +0100 | [diff] [blame] | 4968 | /* |
| 4969 | * Print the name of a VMA. |
| 4970 | */ |
| 4971 | void print_vma_addr(char *prefix, unsigned long ip) |
| 4972 | { |
| 4973 | struct mm_struct *mm = current->mm; |
| 4974 | struct vm_area_struct *vma; |
| 4975 | |
Ingo Molnar | e8bff74 | 2008-02-13 20:21:06 +0100 | [diff] [blame] | 4976 | /* |
Michal Hocko | 0a7f682 | 2017-11-15 17:38:59 -0800 | [diff] [blame] | 4977 | * we might be running from an atomic context so we cannot sleep |
Ingo Molnar | e8bff74 | 2008-02-13 20:21:06 +0100 | [diff] [blame] | 4978 | */ |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 4979 | if (!mmap_read_trylock(mm)) |
Ingo Molnar | e8bff74 | 2008-02-13 20:21:06 +0100 | [diff] [blame] | 4980 | return; |
| 4981 | |
Andi Kleen | 0325291 | 2008-01-30 13:33:18 +0100 | [diff] [blame] | 4982 | vma = find_vma(mm, ip); |
| 4983 | if (vma && vma->vm_file) { |
| 4984 | struct file *f = vma->vm_file; |
Michal Hocko | 0a7f682 | 2017-11-15 17:38:59 -0800 | [diff] [blame] | 4985 | char *buf = (char *)__get_free_page(GFP_NOWAIT); |
Andi Kleen | 0325291 | 2008-01-30 13:33:18 +0100 | [diff] [blame] | 4986 | if (buf) { |
Andy Shevchenko | 2fbc57c | 2012-12-17 16:01:23 -0800 | [diff] [blame] | 4987 | char *p; |
Andi Kleen | 0325291 | 2008-01-30 13:33:18 +0100 | [diff] [blame] | 4988 | |
Miklos Szeredi | 9bf39ab | 2015-06-19 10:29:13 +0200 | [diff] [blame] | 4989 | p = file_path(f, buf, PAGE_SIZE); |
Andi Kleen | 0325291 | 2008-01-30 13:33:18 +0100 | [diff] [blame] | 4990 | if (IS_ERR(p)) |
| 4991 | p = "?"; |
Andy Shevchenko | 2fbc57c | 2012-12-17 16:01:23 -0800 | [diff] [blame] | 4992 | printk("%s%s[%lx+%lx]", prefix, kbasename(p), |
Andi Kleen | 0325291 | 2008-01-30 13:33:18 +0100 | [diff] [blame] | 4993 | vma->vm_start, |
| 4994 | vma->vm_end - vma->vm_start); |
| 4995 | free_page((unsigned long)buf); |
| 4996 | } |
| 4997 | } |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 4998 | mmap_read_unlock(mm); |
Andi Kleen | 0325291 | 2008-01-30 13:33:18 +0100 | [diff] [blame] | 4999 | } |
Nick Piggin | 3ee1afa | 2008-09-10 13:37:17 +0200 | [diff] [blame] | 5000 | |
Michael S. Tsirkin | 662bbcb | 2013-05-26 17:32:23 +0300 | [diff] [blame] | 5001 | #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP) |
David Hildenbrand | 9ec2353 | 2015-05-11 17:52:07 +0200 | [diff] [blame] | 5002 | void __might_fault(const char *file, int line) |
Nick Piggin | 3ee1afa | 2008-09-10 13:37:17 +0200 | [diff] [blame] | 5003 | { |
Peter Zijlstra | 95156f0 | 2009-01-12 13:02:11 +0100 | [diff] [blame] | 5004 | /* |
| 5005 | * Some code (nfs/sunrpc) uses socket ops on kernel memory while |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 5006 | * holding the mmap_lock, this is safe because kernel memory doesn't |
Peter Zijlstra | 95156f0 | 2009-01-12 13:02:11 +0100 | [diff] [blame] | 5007 | * get paged out, therefore we'll never actually fault, and the |
| 5008 | * below annotations will generate false positives. |
| 5009 | */ |
Al Viro | db68ce1 | 2017-03-20 21:08:07 -0400 | [diff] [blame] | 5010 | if (uaccess_kernel()) |
Peter Zijlstra | 95156f0 | 2009-01-12 13:02:11 +0100 | [diff] [blame] | 5011 | return; |
David Hildenbrand | 9ec2353 | 2015-05-11 17:52:07 +0200 | [diff] [blame] | 5012 | if (pagefault_disabled()) |
Michael S. Tsirkin | 662bbcb | 2013-05-26 17:32:23 +0300 | [diff] [blame] | 5013 | return; |
David Hildenbrand | 9ec2353 | 2015-05-11 17:52:07 +0200 | [diff] [blame] | 5014 | __might_sleep(file, line, 0); |
| 5015 | #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) |
Michael S. Tsirkin | 662bbcb | 2013-05-26 17:32:23 +0300 | [diff] [blame] | 5016 | if (current->mm) |
Michel Lespinasse | da1c55f | 2020-06-08 21:33:47 -0700 | [diff] [blame] | 5017 | might_lock_read(¤t->mm->mmap_lock); |
David Hildenbrand | 9ec2353 | 2015-05-11 17:52:07 +0200 | [diff] [blame] | 5018 | #endif |
Nick Piggin | 3ee1afa | 2008-09-10 13:37:17 +0200 | [diff] [blame] | 5019 | } |
David Hildenbrand | 9ec2353 | 2015-05-11 17:52:07 +0200 | [diff] [blame] | 5020 | EXPORT_SYMBOL(__might_fault); |
Nick Piggin | 3ee1afa | 2008-09-10 13:37:17 +0200 | [diff] [blame] | 5021 | #endif |
Andrea Arcangeli | 47ad847 | 2011-01-13 15:46:47 -0800 | [diff] [blame] | 5022 | |
| 5023 | #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) |
Huang Ying | c6ddfb6 | 2018-08-17 15:45:46 -0700 | [diff] [blame] | 5024 | /* |
| 5025 | * Process all subpages of the specified huge page with the specified |
| 5026 | * operation. The target subpage will be processed last to keep its |
| 5027 | * cache lines hot. |
| 5028 | */ |
| 5029 | static inline void process_huge_page( |
| 5030 | unsigned long addr_hint, unsigned int pages_per_huge_page, |
| 5031 | void (*process_subpage)(unsigned long addr, int idx, void *arg), |
| 5032 | void *arg) |
| 5033 | { |
| 5034 | int i, n, base, l; |
| 5035 | unsigned long addr = addr_hint & |
| 5036 | ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); |
| 5037 | |
| 5038 | /* Process target subpage last to keep its cache lines hot */ |
| 5039 | might_sleep(); |
| 5040 | n = (addr_hint - addr) / PAGE_SIZE; |
| 5041 | if (2 * n <= pages_per_huge_page) { |
| 5042 | /* If target subpage in first half of huge page */ |
| 5043 | base = 0; |
| 5044 | l = n; |
| 5045 | /* Process subpages at the end of huge page */ |
| 5046 | for (i = pages_per_huge_page - 1; i >= 2 * n; i--) { |
| 5047 | cond_resched(); |
| 5048 | process_subpage(addr + i * PAGE_SIZE, i, arg); |
| 5049 | } |
| 5050 | } else { |
| 5051 | /* If target subpage in second half of huge page */ |
| 5052 | base = pages_per_huge_page - 2 * (pages_per_huge_page - n); |
| 5053 | l = pages_per_huge_page - n; |
| 5054 | /* Process subpages at the begin of huge page */ |
| 5055 | for (i = 0; i < base; i++) { |
| 5056 | cond_resched(); |
| 5057 | process_subpage(addr + i * PAGE_SIZE, i, arg); |
| 5058 | } |
| 5059 | } |
| 5060 | /* |
| 5061 | * Process remaining subpages in left-right-left-right pattern |
| 5062 | * towards the target subpage |
| 5063 | */ |
| 5064 | for (i = 0; i < l; i++) { |
| 5065 | int left_idx = base + i; |
| 5066 | int right_idx = base + 2 * l - 1 - i; |
| 5067 | |
| 5068 | cond_resched(); |
| 5069 | process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg); |
| 5070 | cond_resched(); |
| 5071 | process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg); |
| 5072 | } |
| 5073 | } |
| 5074 | |
Andrea Arcangeli | 47ad847 | 2011-01-13 15:46:47 -0800 | [diff] [blame] | 5075 | static void clear_gigantic_page(struct page *page, |
| 5076 | unsigned long addr, |
| 5077 | unsigned int pages_per_huge_page) |
| 5078 | { |
| 5079 | int i; |
| 5080 | struct page *p = page; |
| 5081 | |
| 5082 | might_sleep(); |
| 5083 | for (i = 0; i < pages_per_huge_page; |
| 5084 | i++, p = mem_map_next(p, page, i)) { |
| 5085 | cond_resched(); |
| 5086 | clear_user_highpage(p, addr + i * PAGE_SIZE); |
| 5087 | } |
| 5088 | } |
Huang Ying | c6ddfb6 | 2018-08-17 15:45:46 -0700 | [diff] [blame] | 5089 | |
| 5090 | static void clear_subpage(unsigned long addr, int idx, void *arg) |
| 5091 | { |
| 5092 | struct page *page = arg; |
| 5093 | |
| 5094 | clear_user_highpage(page + idx, addr); |
| 5095 | } |
| 5096 | |
Andrea Arcangeli | 47ad847 | 2011-01-13 15:46:47 -0800 | [diff] [blame] | 5097 | void clear_huge_page(struct page *page, |
Huang Ying | c79b57e | 2017-09-06 16:25:04 -0700 | [diff] [blame] | 5098 | unsigned long addr_hint, unsigned int pages_per_huge_page) |
Andrea Arcangeli | 47ad847 | 2011-01-13 15:46:47 -0800 | [diff] [blame] | 5099 | { |
Huang Ying | c79b57e | 2017-09-06 16:25:04 -0700 | [diff] [blame] | 5100 | unsigned long addr = addr_hint & |
| 5101 | ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); |
Andrea Arcangeli | 47ad847 | 2011-01-13 15:46:47 -0800 | [diff] [blame] | 5102 | |
| 5103 | if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { |
| 5104 | clear_gigantic_page(page, addr, pages_per_huge_page); |
| 5105 | return; |
| 5106 | } |
| 5107 | |
Huang Ying | c6ddfb6 | 2018-08-17 15:45:46 -0700 | [diff] [blame] | 5108 | process_huge_page(addr_hint, pages_per_huge_page, clear_subpage, page); |
Andrea Arcangeli | 47ad847 | 2011-01-13 15:46:47 -0800 | [diff] [blame] | 5109 | } |
| 5110 | |
| 5111 | static void copy_user_gigantic_page(struct page *dst, struct page *src, |
| 5112 | unsigned long addr, |
| 5113 | struct vm_area_struct *vma, |
| 5114 | unsigned int pages_per_huge_page) |
| 5115 | { |
| 5116 | int i; |
| 5117 | struct page *dst_base = dst; |
| 5118 | struct page *src_base = src; |
| 5119 | |
| 5120 | for (i = 0; i < pages_per_huge_page; ) { |
| 5121 | cond_resched(); |
| 5122 | copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma); |
| 5123 | |
| 5124 | i++; |
| 5125 | dst = mem_map_next(dst, dst_base, i); |
| 5126 | src = mem_map_next(src, src_base, i); |
| 5127 | } |
| 5128 | } |
| 5129 | |
Huang Ying | c9f4cd7 | 2018-08-17 15:45:49 -0700 | [diff] [blame] | 5130 | struct copy_subpage_arg { |
| 5131 | struct page *dst; |
| 5132 | struct page *src; |
| 5133 | struct vm_area_struct *vma; |
| 5134 | }; |
| 5135 | |
| 5136 | static void copy_subpage(unsigned long addr, int idx, void *arg) |
| 5137 | { |
| 5138 | struct copy_subpage_arg *copy_arg = arg; |
| 5139 | |
| 5140 | copy_user_highpage(copy_arg->dst + idx, copy_arg->src + idx, |
| 5141 | addr, copy_arg->vma); |
| 5142 | } |
| 5143 | |
Andrea Arcangeli | 47ad847 | 2011-01-13 15:46:47 -0800 | [diff] [blame] | 5144 | void copy_user_huge_page(struct page *dst, struct page *src, |
Huang Ying | c9f4cd7 | 2018-08-17 15:45:49 -0700 | [diff] [blame] | 5145 | unsigned long addr_hint, struct vm_area_struct *vma, |
Andrea Arcangeli | 47ad847 | 2011-01-13 15:46:47 -0800 | [diff] [blame] | 5146 | unsigned int pages_per_huge_page) |
| 5147 | { |
Huang Ying | c9f4cd7 | 2018-08-17 15:45:49 -0700 | [diff] [blame] | 5148 | unsigned long addr = addr_hint & |
| 5149 | ~(((unsigned long)pages_per_huge_page << PAGE_SHIFT) - 1); |
| 5150 | struct copy_subpage_arg arg = { |
| 5151 | .dst = dst, |
| 5152 | .src = src, |
| 5153 | .vma = vma, |
| 5154 | }; |
Andrea Arcangeli | 47ad847 | 2011-01-13 15:46:47 -0800 | [diff] [blame] | 5155 | |
| 5156 | if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { |
| 5157 | copy_user_gigantic_page(dst, src, addr, vma, |
| 5158 | pages_per_huge_page); |
| 5159 | return; |
| 5160 | } |
| 5161 | |
Huang Ying | c9f4cd7 | 2018-08-17 15:45:49 -0700 | [diff] [blame] | 5162 | process_huge_page(addr_hint, pages_per_huge_page, copy_subpage, &arg); |
Andrea Arcangeli | 47ad847 | 2011-01-13 15:46:47 -0800 | [diff] [blame] | 5163 | } |
Mike Kravetz | fa4d75c | 2017-02-22 15:42:49 -0800 | [diff] [blame] | 5164 | |
| 5165 | long copy_huge_page_from_user(struct page *dst_page, |
| 5166 | const void __user *usr_src, |
Mike Kravetz | 810a56b | 2017-02-22 15:42:58 -0800 | [diff] [blame] | 5167 | unsigned int pages_per_huge_page, |
| 5168 | bool allow_pagefault) |
Mike Kravetz | fa4d75c | 2017-02-22 15:42:49 -0800 | [diff] [blame] | 5169 | { |
| 5170 | void *src = (void *)usr_src; |
| 5171 | void *page_kaddr; |
| 5172 | unsigned long i, rc = 0; |
| 5173 | unsigned long ret_val = pages_per_huge_page * PAGE_SIZE; |
Mike Kravetz | 3272cfc2 | 2021-02-24 12:07:54 -0800 | [diff] [blame] | 5174 | struct page *subpage = dst_page; |
Mike Kravetz | fa4d75c | 2017-02-22 15:42:49 -0800 | [diff] [blame] | 5175 | |
Mike Kravetz | 3272cfc2 | 2021-02-24 12:07:54 -0800 | [diff] [blame] | 5176 | for (i = 0; i < pages_per_huge_page; |
| 5177 | i++, subpage = mem_map_next(subpage, dst_page, i)) { |
Mike Kravetz | 810a56b | 2017-02-22 15:42:58 -0800 | [diff] [blame] | 5178 | if (allow_pagefault) |
Mike Kravetz | 3272cfc2 | 2021-02-24 12:07:54 -0800 | [diff] [blame] | 5179 | page_kaddr = kmap(subpage); |
Mike Kravetz | 810a56b | 2017-02-22 15:42:58 -0800 | [diff] [blame] | 5180 | else |
Mike Kravetz | 3272cfc2 | 2021-02-24 12:07:54 -0800 | [diff] [blame] | 5181 | page_kaddr = kmap_atomic(subpage); |
Mike Kravetz | fa4d75c | 2017-02-22 15:42:49 -0800 | [diff] [blame] | 5182 | rc = copy_from_user(page_kaddr, |
| 5183 | (const void __user *)(src + i * PAGE_SIZE), |
| 5184 | PAGE_SIZE); |
Mike Kravetz | 810a56b | 2017-02-22 15:42:58 -0800 | [diff] [blame] | 5185 | if (allow_pagefault) |
Mike Kravetz | 3272cfc2 | 2021-02-24 12:07:54 -0800 | [diff] [blame] | 5186 | kunmap(subpage); |
Mike Kravetz | 810a56b | 2017-02-22 15:42:58 -0800 | [diff] [blame] | 5187 | else |
| 5188 | kunmap_atomic(page_kaddr); |
Mike Kravetz | fa4d75c | 2017-02-22 15:42:49 -0800 | [diff] [blame] | 5189 | |
| 5190 | ret_val -= (PAGE_SIZE - rc); |
| 5191 | if (rc) |
| 5192 | break; |
| 5193 | |
| 5194 | cond_resched(); |
| 5195 | } |
| 5196 | return ret_val; |
| 5197 | } |
Andrea Arcangeli | 47ad847 | 2011-01-13 15:46:47 -0800 | [diff] [blame] | 5198 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ |
Kirill A. Shutemov | 49076ec | 2013-11-14 14:31:51 -0800 | [diff] [blame] | 5199 | |
Olof Johansson | 40b64ac | 2013-12-20 14:28:05 -0800 | [diff] [blame] | 5200 | #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS |
Kirill A. Shutemov | b35f181 | 2014-01-21 15:49:07 -0800 | [diff] [blame] | 5201 | |
| 5202 | static struct kmem_cache *page_ptl_cachep; |
| 5203 | |
| 5204 | void __init ptlock_cache_init(void) |
| 5205 | { |
| 5206 | page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0, |
| 5207 | SLAB_PANIC, NULL); |
| 5208 | } |
| 5209 | |
Peter Zijlstra | 539edb5 | 2013-11-14 14:31:52 -0800 | [diff] [blame] | 5210 | bool ptlock_alloc(struct page *page) |
Kirill A. Shutemov | 49076ec | 2013-11-14 14:31:51 -0800 | [diff] [blame] | 5211 | { |
| 5212 | spinlock_t *ptl; |
| 5213 | |
Kirill A. Shutemov | b35f181 | 2014-01-21 15:49:07 -0800 | [diff] [blame] | 5214 | ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL); |
Kirill A. Shutemov | 49076ec | 2013-11-14 14:31:51 -0800 | [diff] [blame] | 5215 | if (!ptl) |
| 5216 | return false; |
Peter Zijlstra | 539edb5 | 2013-11-14 14:31:52 -0800 | [diff] [blame] | 5217 | page->ptl = ptl; |
Kirill A. Shutemov | 49076ec | 2013-11-14 14:31:51 -0800 | [diff] [blame] | 5218 | return true; |
| 5219 | } |
| 5220 | |
Peter Zijlstra | 539edb5 | 2013-11-14 14:31:52 -0800 | [diff] [blame] | 5221 | void ptlock_free(struct page *page) |
Kirill A. Shutemov | 49076ec | 2013-11-14 14:31:51 -0800 | [diff] [blame] | 5222 | { |
Kirill A. Shutemov | b35f181 | 2014-01-21 15:49:07 -0800 | [diff] [blame] | 5223 | kmem_cache_free(page_ptl_cachep, page->ptl); |
Kirill A. Shutemov | 49076ec | 2013-11-14 14:31:51 -0800 | [diff] [blame] | 5224 | } |
| 5225 | #endif |