]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/mm/memory.c | |
3 | * | |
4 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds | |
5 | */ | |
6 | ||
7 | /* | |
8 | * demand-loading started 01.12.91 - seems it is high on the list of | |
9 | * things wanted, and it should be easy to implement. - Linus | |
10 | */ | |
11 | ||
12 | /* | |
13 | * Ok, demand-loading was easy, shared pages a little bit tricker. Shared | |
14 | * pages started 02.12.91, seems to work. - Linus. | |
15 | * | |
16 | * Tested sharing by executing about 30 /bin/sh: under the old kernel it | |
17 | * would have taken more than the 6M I have free, but it worked well as | |
18 | * far as I could see. | |
19 | * | |
20 | * Also corrected some "invalidate()"s - I wasn't doing enough of them. | |
21 | */ | |
22 | ||
23 | /* | |
24 | * Real VM (paging to/from disk) started 18.12.91. Much more work and | |
25 | * thought has to go into this. Oh, well.. | |
26 | * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why. | |
27 | * Found it. Everything seems to work now. | |
28 | * 20.12.91 - Ok, making the swap-device changeable like the root. | |
29 | */ | |
30 | ||
31 | /* | |
32 | * 05.04.94 - Multi-page memory management added for v1.1. | |
166f61b9 | 33 | * Idea by Alex Bligh (alex@cconcepts.co.uk) |
1da177e4 LT |
34 | * |
35 | * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG | |
36 | * (Gerhard.Wichert@pdb.siemens.de) | |
37 | * | |
38 | * Aug/Sep 2004 Changed to four level page tables (Andi Kleen) | |
39 | */ | |
40 | ||
41 | #include <linux/kernel_stat.h> | |
42 | #include <linux/mm.h> | |
6e84f315 | 43 | #include <linux/sched/mm.h> |
f7ccbae4 | 44 | #include <linux/sched/coredump.h> |
6a3827d7 | 45 | #include <linux/sched/numa_balancing.h> |
1da177e4 LT |
46 | #include <linux/hugetlb.h> |
47 | #include <linux/mman.h> | |
48 | #include <linux/swap.h> | |
49 | #include <linux/highmem.h> | |
50 | #include <linux/pagemap.h> | |
9a840895 | 51 | #include <linux/ksm.h> |
1da177e4 | 52 | #include <linux/rmap.h> |
b95f1b31 | 53 | #include <linux/export.h> |
0ff92245 | 54 | #include <linux/delayacct.h> |
1da177e4 | 55 | #include <linux/init.h> |
01c8f1c4 | 56 | #include <linux/pfn_t.h> |
edc79b2a | 57 | #include <linux/writeback.h> |
8a9f3ccd | 58 | #include <linux/memcontrol.h> |
cddb8a5c | 59 | #include <linux/mmu_notifier.h> |
3dc14741 HD |
60 | #include <linux/kallsyms.h> |
61 | #include <linux/swapops.h> | |
62 | #include <linux/elf.h> | |
5a0e3ad6 | 63 | #include <linux/gfp.h> |
4daae3b4 | 64 | #include <linux/migrate.h> |
2fbc57c5 | 65 | #include <linux/string.h> |
0abdd7a8 | 66 | #include <linux/dma-debug.h> |
1592eef0 | 67 | #include <linux/debugfs.h> |
6b251fc9 | 68 | #include <linux/userfaultfd_k.h> |
bc2466e4 | 69 | #include <linux/dax.h> |
1da177e4 | 70 | |
6952b61d | 71 | #include <asm/io.h> |
33a709b2 | 72 | #include <asm/mmu_context.h> |
1da177e4 | 73 | #include <asm/pgalloc.h> |
7c0f6ba6 | 74 | #include <linux/uaccess.h> |
1da177e4 LT |
75 | #include <asm/tlb.h> |
76 | #include <asm/tlbflush.h> | |
77 | #include <asm/pgtable.h> | |
78 | ||
42b77728 JB |
79 | #include "internal.h" |
80 | ||
90572890 PZ |
81 | #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS |
82 | #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid. | |
75980e97 PZ |
83 | #endif |
84 | ||
d41dee36 | 85 | #ifndef CONFIG_NEED_MULTIPLE_NODES |
1da177e4 LT |
86 | /* use the per-pgdat data instead for discontigmem - mbligh */ |
87 | unsigned long max_mapnr; | |
1da177e4 | 88 | EXPORT_SYMBOL(max_mapnr); |
166f61b9 TH |
89 | |
90 | struct page *mem_map; | |
1da177e4 LT |
91 | EXPORT_SYMBOL(mem_map); |
92 | #endif | |
93 | ||
1da177e4 LT |
94 | /* |
95 | * A number of key systems in x86 including ioremap() rely on the assumption | |
96 | * that high_memory defines the upper bound on direct map memory, then end | |
97 | * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and | |
98 | * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL | |
99 | * and ZONE_HIGHMEM. | |
100 | */ | |
166f61b9 | 101 | void *high_memory; |
1da177e4 | 102 | EXPORT_SYMBOL(high_memory); |
1da177e4 | 103 | |
32a93233 IM |
104 | /* |
105 | * Randomize the address space (stacks, mmaps, brk, etc.). | |
106 | * | |
107 | * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization, | |
108 | * as ancient (libc5 based) binaries can segfault. ) | |
109 | */ | |
110 | int randomize_va_space __read_mostly = | |
111 | #ifdef CONFIG_COMPAT_BRK | |
112 | 1; | |
113 | #else | |
114 | 2; | |
115 | #endif | |
a62eaf15 AK |
116 | |
117 | static int __init disable_randmaps(char *s) | |
118 | { | |
119 | randomize_va_space = 0; | |
9b41046c | 120 | return 1; |
a62eaf15 AK |
121 | } |
122 | __setup("norandmaps", disable_randmaps); | |
123 | ||
62eede62 | 124 | unsigned long zero_pfn __read_mostly; |
0b70068e AB |
125 | EXPORT_SYMBOL(zero_pfn); |
126 | ||
166f61b9 TH |
127 | unsigned long highest_memmap_pfn __read_mostly; |
128 | ||
a13ea5b7 HD |
129 | /* |
130 | * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init() | |
131 | */ | |
132 | static int __init init_zero_pfn(void) | |
133 | { | |
134 | zero_pfn = page_to_pfn(ZERO_PAGE(0)); | |
135 | return 0; | |
136 | } | |
137 | core_initcall(init_zero_pfn); | |
a62eaf15 | 138 | |
d559db08 | 139 | |
34e55232 KH |
140 | #if defined(SPLIT_RSS_COUNTING) |
141 | ||
ea48cf78 | 142 | void sync_mm_rss(struct mm_struct *mm) |
34e55232 KH |
143 | { |
144 | int i; | |
145 | ||
146 | for (i = 0; i < NR_MM_COUNTERS; i++) { | |
05af2e10 DR |
147 | if (current->rss_stat.count[i]) { |
148 | add_mm_counter(mm, i, current->rss_stat.count[i]); | |
149 | current->rss_stat.count[i] = 0; | |
34e55232 KH |
150 | } |
151 | } | |
05af2e10 | 152 | current->rss_stat.events = 0; |
34e55232 KH |
153 | } |
154 | ||
155 | static void add_mm_counter_fast(struct mm_struct *mm, int member, int val) | |
156 | { | |
157 | struct task_struct *task = current; | |
158 | ||
159 | if (likely(task->mm == mm)) | |
160 | task->rss_stat.count[member] += val; | |
161 | else | |
162 | add_mm_counter(mm, member, val); | |
163 | } | |
164 | #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1) | |
165 | #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1) | |
166 | ||
167 | /* sync counter once per 64 page faults */ | |
168 | #define TASK_RSS_EVENTS_THRESH (64) | |
169 | static void check_sync_rss_stat(struct task_struct *task) | |
170 | { | |
171 | if (unlikely(task != current)) | |
172 | return; | |
173 | if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH)) | |
ea48cf78 | 174 | sync_mm_rss(task->mm); |
34e55232 | 175 | } |
9547d01b | 176 | #else /* SPLIT_RSS_COUNTING */ |
34e55232 KH |
177 | |
178 | #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member) | |
179 | #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member) | |
180 | ||
181 | static void check_sync_rss_stat(struct task_struct *task) | |
182 | { | |
183 | } | |
184 | ||
9547d01b PZ |
185 | #endif /* SPLIT_RSS_COUNTING */ |
186 | ||
187 | #ifdef HAVE_GENERIC_MMU_GATHER | |
188 | ||
ca1d6c7d | 189 | static bool tlb_next_batch(struct mmu_gather *tlb) |
9547d01b PZ |
190 | { |
191 | struct mmu_gather_batch *batch; | |
192 | ||
193 | batch = tlb->active; | |
194 | if (batch->next) { | |
195 | tlb->active = batch->next; | |
ca1d6c7d | 196 | return true; |
9547d01b PZ |
197 | } |
198 | ||
53a59fc6 | 199 | if (tlb->batch_count == MAX_GATHER_BATCH_COUNT) |
ca1d6c7d | 200 | return false; |
53a59fc6 | 201 | |
9547d01b PZ |
202 | batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0); |
203 | if (!batch) | |
ca1d6c7d | 204 | return false; |
9547d01b | 205 | |
53a59fc6 | 206 | tlb->batch_count++; |
9547d01b PZ |
207 | batch->next = NULL; |
208 | batch->nr = 0; | |
209 | batch->max = MAX_GATHER_BATCH; | |
210 | ||
211 | tlb->active->next = batch; | |
212 | tlb->active = batch; | |
213 | ||
ca1d6c7d | 214 | return true; |
9547d01b PZ |
215 | } |
216 | ||
217 | /* tlb_gather_mmu | |
218 | * Called to initialize an (on-stack) mmu_gather structure for page-table | |
219 | * tear-down from @mm. The @fullmm argument is used when @mm is without | |
220 | * users and we're going to destroy the full address space (exit/execve). | |
221 | */ | |
2b047252 | 222 | void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end) |
9547d01b PZ |
223 | { |
224 | tlb->mm = mm; | |
225 | ||
2b047252 LT |
226 | /* Is it from 0 to ~0? */ |
227 | tlb->fullmm = !(start | (end+1)); | |
1de14c3c | 228 | tlb->need_flush_all = 0; |
9547d01b PZ |
229 | tlb->local.next = NULL; |
230 | tlb->local.nr = 0; | |
231 | tlb->local.max = ARRAY_SIZE(tlb->__pages); | |
232 | tlb->active = &tlb->local; | |
53a59fc6 | 233 | tlb->batch_count = 0; |
9547d01b PZ |
234 | |
235 | #ifdef CONFIG_HAVE_RCU_TABLE_FREE | |
236 | tlb->batch = NULL; | |
237 | #endif | |
e77b0852 | 238 | tlb->page_size = 0; |
fb7332a9 WD |
239 | |
240 | __tlb_reset_range(tlb); | |
9547d01b PZ |
241 | } |
242 | ||
1cf35d47 | 243 | static void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb) |
9547d01b | 244 | { |
721c21c1 WD |
245 | if (!tlb->end) |
246 | return; | |
247 | ||
9547d01b | 248 | tlb_flush(tlb); |
34ee645e | 249 | mmu_notifier_invalidate_range(tlb->mm, tlb->start, tlb->end); |
9547d01b PZ |
250 | #ifdef CONFIG_HAVE_RCU_TABLE_FREE |
251 | tlb_table_flush(tlb); | |
34e55232 | 252 | #endif |
fb7332a9 | 253 | __tlb_reset_range(tlb); |
1cf35d47 LT |
254 | } |
255 | ||
256 | static void tlb_flush_mmu_free(struct mmu_gather *tlb) | |
257 | { | |
258 | struct mmu_gather_batch *batch; | |
34e55232 | 259 | |
721c21c1 | 260 | for (batch = &tlb->local; batch && batch->nr; batch = batch->next) { |
9547d01b PZ |
261 | free_pages_and_swap_cache(batch->pages, batch->nr); |
262 | batch->nr = 0; | |
263 | } | |
264 | tlb->active = &tlb->local; | |
265 | } | |
266 | ||
1cf35d47 LT |
267 | void tlb_flush_mmu(struct mmu_gather *tlb) |
268 | { | |
1cf35d47 LT |
269 | tlb_flush_mmu_tlbonly(tlb); |
270 | tlb_flush_mmu_free(tlb); | |
271 | } | |
272 | ||
9547d01b PZ |
273 | /* tlb_finish_mmu |
274 | * Called at the end of the shootdown operation to free up any resources | |
275 | * that were required. | |
276 | */ | |
277 | void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end) | |
278 | { | |
279 | struct mmu_gather_batch *batch, *next; | |
280 | ||
281 | tlb_flush_mmu(tlb); | |
282 | ||
283 | /* keep the page table cache within bounds */ | |
284 | check_pgt_cache(); | |
285 | ||
286 | for (batch = tlb->local.next; batch; batch = next) { | |
287 | next = batch->next; | |
288 | free_pages((unsigned long)batch, 0); | |
289 | } | |
290 | tlb->local.next = NULL; | |
291 | } | |
292 | ||
293 | /* __tlb_remove_page | |
294 | * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while | |
295 | * handling the additional races in SMP caused by other CPUs caching valid | |
296 | * mappings in their TLBs. Returns the number of free page slots left. | |
297 | * When out of page slots we must call tlb_flush_mmu(). | |
e9d55e15 | 298 | *returns true if the caller should flush. |
9547d01b | 299 | */ |
e77b0852 | 300 | bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size) |
9547d01b PZ |
301 | { |
302 | struct mmu_gather_batch *batch; | |
303 | ||
fb7332a9 | 304 | VM_BUG_ON(!tlb->end); |
692a68c1 | 305 | VM_WARN_ON(tlb->page_size != page_size); |
e77b0852 | 306 | |
9547d01b | 307 | batch = tlb->active; |
692a68c1 AK |
308 | /* |
309 | * Add the page and check if we are full. If so | |
310 | * force a flush. | |
311 | */ | |
312 | batch->pages[batch->nr++] = page; | |
9547d01b PZ |
313 | if (batch->nr == batch->max) { |
314 | if (!tlb_next_batch(tlb)) | |
e9d55e15 | 315 | return true; |
0b43c3aa | 316 | batch = tlb->active; |
9547d01b | 317 | } |
309381fe | 318 | VM_BUG_ON_PAGE(batch->nr > batch->max, page); |
9547d01b | 319 | |
e9d55e15 | 320 | return false; |
9547d01b PZ |
321 | } |
322 | ||
323 | #endif /* HAVE_GENERIC_MMU_GATHER */ | |
324 | ||
26723911 PZ |
325 | #ifdef CONFIG_HAVE_RCU_TABLE_FREE |
326 | ||
327 | /* | |
328 | * See the comment near struct mmu_table_batch. | |
329 | */ | |
330 | ||
331 | static void tlb_remove_table_smp_sync(void *arg) | |
332 | { | |
333 | /* Simply deliver the interrupt */ | |
334 | } | |
335 | ||
336 | static void tlb_remove_table_one(void *table) | |
337 | { | |
338 | /* | |
339 | * This isn't an RCU grace period and hence the page-tables cannot be | |
340 | * assumed to be actually RCU-freed. | |
341 | * | |
342 | * It is however sufficient for software page-table walkers that rely on | |
343 | * IRQ disabling. See the comment near struct mmu_table_batch. | |
344 | */ | |
345 | smp_call_function(tlb_remove_table_smp_sync, NULL, 1); | |
346 | __tlb_remove_table(table); | |
347 | } | |
348 | ||
349 | static void tlb_remove_table_rcu(struct rcu_head *head) | |
350 | { | |
351 | struct mmu_table_batch *batch; | |
352 | int i; | |
353 | ||
354 | batch = container_of(head, struct mmu_table_batch, rcu); | |
355 | ||
356 | for (i = 0; i < batch->nr; i++) | |
357 | __tlb_remove_table(batch->tables[i]); | |
358 | ||
359 | free_page((unsigned long)batch); | |
360 | } | |
361 | ||
362 | void tlb_table_flush(struct mmu_gather *tlb) | |
363 | { | |
364 | struct mmu_table_batch **batch = &tlb->batch; | |
365 | ||
366 | if (*batch) { | |
367 | call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu); | |
368 | *batch = NULL; | |
369 | } | |
370 | } | |
371 | ||
372 | void tlb_remove_table(struct mmu_gather *tlb, void *table) | |
373 | { | |
374 | struct mmu_table_batch **batch = &tlb->batch; | |
375 | ||
26723911 PZ |
376 | /* |
377 | * When there's less then two users of this mm there cannot be a | |
378 | * concurrent page-table walk. | |
379 | */ | |
380 | if (atomic_read(&tlb->mm->mm_users) < 2) { | |
381 | __tlb_remove_table(table); | |
382 | return; | |
383 | } | |
384 | ||
385 | if (*batch == NULL) { | |
386 | *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN); | |
387 | if (*batch == NULL) { | |
388 | tlb_remove_table_one(table); | |
389 | return; | |
390 | } | |
391 | (*batch)->nr = 0; | |
392 | } | |
393 | (*batch)->tables[(*batch)->nr++] = table; | |
394 | if ((*batch)->nr == MAX_TABLE_BATCH) | |
395 | tlb_table_flush(tlb); | |
396 | } | |
397 | ||
9547d01b | 398 | #endif /* CONFIG_HAVE_RCU_TABLE_FREE */ |
26723911 | 399 | |
1da177e4 LT |
400 | /* |
401 | * Note: this doesn't free the actual pages themselves. That | |
402 | * has been handled earlier when unmapping all the memory regions. | |
403 | */ | |
9e1b32ca BH |
404 | static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, |
405 | unsigned long addr) | |
1da177e4 | 406 | { |
2f569afd | 407 | pgtable_t token = pmd_pgtable(*pmd); |
e0da382c | 408 | pmd_clear(pmd); |
9e1b32ca | 409 | pte_free_tlb(tlb, token, addr); |
e1f56c89 | 410 | atomic_long_dec(&tlb->mm->nr_ptes); |
1da177e4 LT |
411 | } |
412 | ||
e0da382c HD |
413 | static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud, |
414 | unsigned long addr, unsigned long end, | |
415 | unsigned long floor, unsigned long ceiling) | |
1da177e4 LT |
416 | { |
417 | pmd_t *pmd; | |
418 | unsigned long next; | |
e0da382c | 419 | unsigned long start; |
1da177e4 | 420 | |
e0da382c | 421 | start = addr; |
1da177e4 | 422 | pmd = pmd_offset(pud, addr); |
1da177e4 LT |
423 | do { |
424 | next = pmd_addr_end(addr, end); | |
425 | if (pmd_none_or_clear_bad(pmd)) | |
426 | continue; | |
9e1b32ca | 427 | free_pte_range(tlb, pmd, addr); |
1da177e4 LT |
428 | } while (pmd++, addr = next, addr != end); |
429 | ||
e0da382c HD |
430 | start &= PUD_MASK; |
431 | if (start < floor) | |
432 | return; | |
433 | if (ceiling) { | |
434 | ceiling &= PUD_MASK; | |
435 | if (!ceiling) | |
436 | return; | |
1da177e4 | 437 | } |
e0da382c HD |
438 | if (end - 1 > ceiling - 1) |
439 | return; | |
440 | ||
441 | pmd = pmd_offset(pud, start); | |
442 | pud_clear(pud); | |
9e1b32ca | 443 | pmd_free_tlb(tlb, pmd, start); |
dc6c9a35 | 444 | mm_dec_nr_pmds(tlb->mm); |
1da177e4 LT |
445 | } |
446 | ||
e0da382c HD |
447 | static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd, |
448 | unsigned long addr, unsigned long end, | |
449 | unsigned long floor, unsigned long ceiling) | |
1da177e4 LT |
450 | { |
451 | pud_t *pud; | |
452 | unsigned long next; | |
e0da382c | 453 | unsigned long start; |
1da177e4 | 454 | |
e0da382c | 455 | start = addr; |
1da177e4 | 456 | pud = pud_offset(pgd, addr); |
1da177e4 LT |
457 | do { |
458 | next = pud_addr_end(addr, end); | |
459 | if (pud_none_or_clear_bad(pud)) | |
460 | continue; | |
e0da382c | 461 | free_pmd_range(tlb, pud, addr, next, floor, ceiling); |
1da177e4 LT |
462 | } while (pud++, addr = next, addr != end); |
463 | ||
e0da382c HD |
464 | start &= PGDIR_MASK; |
465 | if (start < floor) | |
466 | return; | |
467 | if (ceiling) { | |
468 | ceiling &= PGDIR_MASK; | |
469 | if (!ceiling) | |
470 | return; | |
1da177e4 | 471 | } |
e0da382c HD |
472 | if (end - 1 > ceiling - 1) |
473 | return; | |
474 | ||
475 | pud = pud_offset(pgd, start); | |
476 | pgd_clear(pgd); | |
9e1b32ca | 477 | pud_free_tlb(tlb, pud, start); |
1da177e4 LT |
478 | } |
479 | ||
480 | /* | |
e0da382c | 481 | * This function frees user-level page tables of a process. |
1da177e4 | 482 | */ |
42b77728 | 483 | void free_pgd_range(struct mmu_gather *tlb, |
e0da382c HD |
484 | unsigned long addr, unsigned long end, |
485 | unsigned long floor, unsigned long ceiling) | |
1da177e4 LT |
486 | { |
487 | pgd_t *pgd; | |
488 | unsigned long next; | |
e0da382c HD |
489 | |
490 | /* | |
491 | * The next few lines have given us lots of grief... | |
492 | * | |
493 | * Why are we testing PMD* at this top level? Because often | |
494 | * there will be no work to do at all, and we'd prefer not to | |
495 | * go all the way down to the bottom just to discover that. | |
496 | * | |
497 | * Why all these "- 1"s? Because 0 represents both the bottom | |
498 | * of the address space and the top of it (using -1 for the | |
499 | * top wouldn't help much: the masks would do the wrong thing). | |
500 | * The rule is that addr 0 and floor 0 refer to the bottom of | |
501 | * the address space, but end 0 and ceiling 0 refer to the top | |
502 | * Comparisons need to use "end - 1" and "ceiling - 1" (though | |
503 | * that end 0 case should be mythical). | |
504 | * | |
505 | * Wherever addr is brought up or ceiling brought down, we must | |
506 | * be careful to reject "the opposite 0" before it confuses the | |
507 | * subsequent tests. But what about where end is brought down | |
508 | * by PMD_SIZE below? no, end can't go down to 0 there. | |
509 | * | |
510 | * Whereas we round start (addr) and ceiling down, by different | |
511 | * masks at different levels, in order to test whether a table | |
512 | * now has no other vmas using it, so can be freed, we don't | |
513 | * bother to round floor or end up - the tests don't need that. | |
514 | */ | |
1da177e4 | 515 | |
e0da382c HD |
516 | addr &= PMD_MASK; |
517 | if (addr < floor) { | |
518 | addr += PMD_SIZE; | |
519 | if (!addr) | |
520 | return; | |
521 | } | |
522 | if (ceiling) { | |
523 | ceiling &= PMD_MASK; | |
524 | if (!ceiling) | |
525 | return; | |
526 | } | |
527 | if (end - 1 > ceiling - 1) | |
528 | end -= PMD_SIZE; | |
529 | if (addr > end - 1) | |
530 | return; | |
07e32661 AK |
531 | /* |
532 | * We add page table cache pages with PAGE_SIZE, | |
533 | * (see pte_free_tlb()), flush the tlb if we need | |
534 | */ | |
535 | tlb_remove_check_page_size_change(tlb, PAGE_SIZE); | |
42b77728 | 536 | pgd = pgd_offset(tlb->mm, addr); |
1da177e4 LT |
537 | do { |
538 | next = pgd_addr_end(addr, end); | |
539 | if (pgd_none_or_clear_bad(pgd)) | |
540 | continue; | |
42b77728 | 541 | free_pud_range(tlb, pgd, addr, next, floor, ceiling); |
1da177e4 | 542 | } while (pgd++, addr = next, addr != end); |
e0da382c HD |
543 | } |
544 | ||
42b77728 | 545 | void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, |
3bf5ee95 | 546 | unsigned long floor, unsigned long ceiling) |
e0da382c HD |
547 | { |
548 | while (vma) { | |
549 | struct vm_area_struct *next = vma->vm_next; | |
550 | unsigned long addr = vma->vm_start; | |
551 | ||
8f4f8c16 | 552 | /* |
25d9e2d1 | 553 | * Hide vma from rmap and truncate_pagecache before freeing |
554 | * pgtables | |
8f4f8c16 | 555 | */ |
5beb4930 | 556 | unlink_anon_vmas(vma); |
8f4f8c16 HD |
557 | unlink_file_vma(vma); |
558 | ||
9da61aef | 559 | if (is_vm_hugetlb_page(vma)) { |
3bf5ee95 | 560 | hugetlb_free_pgd_range(tlb, addr, vma->vm_end, |
166f61b9 | 561 | floor, next ? next->vm_start : ceiling); |
3bf5ee95 HD |
562 | } else { |
563 | /* | |
564 | * Optimization: gather nearby vmas into one call down | |
565 | */ | |
566 | while (next && next->vm_start <= vma->vm_end + PMD_SIZE | |
4866920b | 567 | && !is_vm_hugetlb_page(next)) { |
3bf5ee95 HD |
568 | vma = next; |
569 | next = vma->vm_next; | |
5beb4930 | 570 | unlink_anon_vmas(vma); |
8f4f8c16 | 571 | unlink_file_vma(vma); |
3bf5ee95 HD |
572 | } |
573 | free_pgd_range(tlb, addr, vma->vm_end, | |
166f61b9 | 574 | floor, next ? next->vm_start : ceiling); |
3bf5ee95 | 575 | } |
e0da382c HD |
576 | vma = next; |
577 | } | |
1da177e4 LT |
578 | } |
579 | ||
3ed3a4f0 | 580 | int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address) |
1da177e4 | 581 | { |
c4088ebd | 582 | spinlock_t *ptl; |
2f569afd | 583 | pgtable_t new = pte_alloc_one(mm, address); |
1bb3630e HD |
584 | if (!new) |
585 | return -ENOMEM; | |
586 | ||
362a61ad NP |
587 | /* |
588 | * Ensure all pte setup (eg. pte page lock and page clearing) are | |
589 | * visible before the pte is made visible to other CPUs by being | |
590 | * put into page tables. | |
591 | * | |
592 | * The other side of the story is the pointer chasing in the page | |
593 | * table walking code (when walking the page table without locking; | |
594 | * ie. most of the time). Fortunately, these data accesses consist | |
595 | * of a chain of data-dependent loads, meaning most CPUs (alpha | |
596 | * being the notable exception) will already guarantee loads are | |
597 | * seen in-order. See the alpha page table accessors for the | |
598 | * smp_read_barrier_depends() barriers in page table walking code. | |
599 | */ | |
600 | smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ | |
601 | ||
c4088ebd | 602 | ptl = pmd_lock(mm, pmd); |
8ac1f832 | 603 | if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ |
e1f56c89 | 604 | atomic_long_inc(&mm->nr_ptes); |
1da177e4 | 605 | pmd_populate(mm, pmd, new); |
2f569afd | 606 | new = NULL; |
4b471e88 | 607 | } |
c4088ebd | 608 | spin_unlock(ptl); |
2f569afd MS |
609 | if (new) |
610 | pte_free(mm, new); | |
1bb3630e | 611 | return 0; |
1da177e4 LT |
612 | } |
613 | ||
1bb3630e | 614 | int __pte_alloc_kernel(pmd_t *pmd, unsigned long address) |
1da177e4 | 615 | { |
1bb3630e HD |
616 | pte_t *new = pte_alloc_one_kernel(&init_mm, address); |
617 | if (!new) | |
618 | return -ENOMEM; | |
619 | ||
362a61ad NP |
620 | smp_wmb(); /* See comment in __pte_alloc */ |
621 | ||
1bb3630e | 622 | spin_lock(&init_mm.page_table_lock); |
8ac1f832 | 623 | if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ |
1bb3630e | 624 | pmd_populate_kernel(&init_mm, pmd, new); |
2f569afd | 625 | new = NULL; |
4b471e88 | 626 | } |
1bb3630e | 627 | spin_unlock(&init_mm.page_table_lock); |
2f569afd MS |
628 | if (new) |
629 | pte_free_kernel(&init_mm, new); | |
1bb3630e | 630 | return 0; |
1da177e4 LT |
631 | } |
632 | ||
d559db08 KH |
633 | static inline void init_rss_vec(int *rss) |
634 | { | |
635 | memset(rss, 0, sizeof(int) * NR_MM_COUNTERS); | |
636 | } | |
637 | ||
638 | static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss) | |
ae859762 | 639 | { |
d559db08 KH |
640 | int i; |
641 | ||
34e55232 | 642 | if (current->mm == mm) |
05af2e10 | 643 | sync_mm_rss(mm); |
d559db08 KH |
644 | for (i = 0; i < NR_MM_COUNTERS; i++) |
645 | if (rss[i]) | |
646 | add_mm_counter(mm, i, rss[i]); | |
ae859762 HD |
647 | } |
648 | ||
b5810039 | 649 | /* |
6aab341e LT |
650 | * This function is called to print an error when a bad pte |
651 | * is found. For example, we might have a PFN-mapped pte in | |
652 | * a region that doesn't allow it. | |
b5810039 NP |
653 | * |
654 | * The calling function must still handle the error. | |
655 | */ | |
3dc14741 HD |
656 | static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, |
657 | pte_t pte, struct page *page) | |
b5810039 | 658 | { |
3dc14741 HD |
659 | pgd_t *pgd = pgd_offset(vma->vm_mm, addr); |
660 | pud_t *pud = pud_offset(pgd, addr); | |
661 | pmd_t *pmd = pmd_offset(pud, addr); | |
662 | struct address_space *mapping; | |
663 | pgoff_t index; | |
d936cf9b HD |
664 | static unsigned long resume; |
665 | static unsigned long nr_shown; | |
666 | static unsigned long nr_unshown; | |
667 | ||
668 | /* | |
669 | * Allow a burst of 60 reports, then keep quiet for that minute; | |
670 | * or allow a steady drip of one report per second. | |
671 | */ | |
672 | if (nr_shown == 60) { | |
673 | if (time_before(jiffies, resume)) { | |
674 | nr_unshown++; | |
675 | return; | |
676 | } | |
677 | if (nr_unshown) { | |
1170532b JP |
678 | pr_alert("BUG: Bad page map: %lu messages suppressed\n", |
679 | nr_unshown); | |
d936cf9b HD |
680 | nr_unshown = 0; |
681 | } | |
682 | nr_shown = 0; | |
683 | } | |
684 | if (nr_shown++ == 0) | |
685 | resume = jiffies + 60 * HZ; | |
3dc14741 HD |
686 | |
687 | mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL; | |
688 | index = linear_page_index(vma, addr); | |
689 | ||
1170532b JP |
690 | pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n", |
691 | current->comm, | |
692 | (long long)pte_val(pte), (long long)pmd_val(*pmd)); | |
718a3821 | 693 | if (page) |
f0b791a3 | 694 | dump_page(page, "bad pte"); |
1170532b JP |
695 | pr_alert("addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n", |
696 | (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index); | |
3dc14741 HD |
697 | /* |
698 | * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y | |
699 | */ | |
2682582a KK |
700 | pr_alert("file:%pD fault:%pf mmap:%pf readpage:%pf\n", |
701 | vma->vm_file, | |
702 | vma->vm_ops ? vma->vm_ops->fault : NULL, | |
703 | vma->vm_file ? vma->vm_file->f_op->mmap : NULL, | |
704 | mapping ? mapping->a_ops->readpage : NULL); | |
b5810039 | 705 | dump_stack(); |
373d4d09 | 706 | add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); |
b5810039 NP |
707 | } |
708 | ||
ee498ed7 | 709 | /* |
7e675137 | 710 | * vm_normal_page -- This function gets the "struct page" associated with a pte. |
6aab341e | 711 | * |
7e675137 NP |
712 | * "Special" mappings do not wish to be associated with a "struct page" (either |
713 | * it doesn't exist, or it exists but they don't want to touch it). In this | |
714 | * case, NULL is returned here. "Normal" mappings do have a struct page. | |
b379d790 | 715 | * |
7e675137 NP |
716 | * There are 2 broad cases. Firstly, an architecture may define a pte_special() |
717 | * pte bit, in which case this function is trivial. Secondly, an architecture | |
718 | * may not have a spare pte bit, which requires a more complicated scheme, | |
719 | * described below. | |
720 | * | |
721 | * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a | |
722 | * special mapping (even if there are underlying and valid "struct pages"). | |
723 | * COWed pages of a VM_PFNMAP are always normal. | |
6aab341e | 724 | * |
b379d790 JH |
725 | * The way we recognize COWed pages within VM_PFNMAP mappings is through the |
726 | * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit | |
7e675137 NP |
727 | * set, and the vm_pgoff will point to the first PFN mapped: thus every special |
728 | * mapping will always honor the rule | |
6aab341e LT |
729 | * |
730 | * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) | |
731 | * | |
7e675137 NP |
732 | * And for normal mappings this is false. |
733 | * | |
734 | * This restricts such mappings to be a linear translation from virtual address | |
735 | * to pfn. To get around this restriction, we allow arbitrary mappings so long | |
736 | * as the vma is not a COW mapping; in that case, we know that all ptes are | |
737 | * special (because none can have been COWed). | |
b379d790 | 738 | * |
b379d790 | 739 | * |
7e675137 | 740 | * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP. |
b379d790 JH |
741 | * |
742 | * VM_MIXEDMAP mappings can likewise contain memory with or without "struct | |
743 | * page" backing, however the difference is that _all_ pages with a struct | |
744 | * page (that is, those where pfn_valid is true) are refcounted and considered | |
745 | * normal pages by the VM. The disadvantage is that pages are refcounted | |
746 | * (which can be slower and simply not an option for some PFNMAP users). The | |
747 | * advantage is that we don't have to follow the strict linearity rule of | |
748 | * PFNMAP mappings in order to support COWable mappings. | |
749 | * | |
ee498ed7 | 750 | */ |
7e675137 NP |
751 | #ifdef __HAVE_ARCH_PTE_SPECIAL |
752 | # define HAVE_PTE_SPECIAL 1 | |
753 | #else | |
754 | # define HAVE_PTE_SPECIAL 0 | |
755 | #endif | |
756 | struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, | |
757 | pte_t pte) | |
ee498ed7 | 758 | { |
22b31eec | 759 | unsigned long pfn = pte_pfn(pte); |
7e675137 NP |
760 | |
761 | if (HAVE_PTE_SPECIAL) { | |
b38af472 | 762 | if (likely(!pte_special(pte))) |
22b31eec | 763 | goto check_pfn; |
667a0a06 DV |
764 | if (vma->vm_ops && vma->vm_ops->find_special_page) |
765 | return vma->vm_ops->find_special_page(vma, addr); | |
a13ea5b7 HD |
766 | if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) |
767 | return NULL; | |
62eede62 | 768 | if (!is_zero_pfn(pfn)) |
22b31eec | 769 | print_bad_pte(vma, addr, pte, NULL); |
7e675137 NP |
770 | return NULL; |
771 | } | |
772 | ||
773 | /* !HAVE_PTE_SPECIAL case follows: */ | |
774 | ||
b379d790 JH |
775 | if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { |
776 | if (vma->vm_flags & VM_MIXEDMAP) { | |
777 | if (!pfn_valid(pfn)) | |
778 | return NULL; | |
779 | goto out; | |
780 | } else { | |
7e675137 NP |
781 | unsigned long off; |
782 | off = (addr - vma->vm_start) >> PAGE_SHIFT; | |
b379d790 JH |
783 | if (pfn == vma->vm_pgoff + off) |
784 | return NULL; | |
785 | if (!is_cow_mapping(vma->vm_flags)) | |
786 | return NULL; | |
787 | } | |
6aab341e LT |
788 | } |
789 | ||
b38af472 HD |
790 | if (is_zero_pfn(pfn)) |
791 | return NULL; | |
22b31eec HD |
792 | check_pfn: |
793 | if (unlikely(pfn > highest_memmap_pfn)) { | |
794 | print_bad_pte(vma, addr, pte, NULL); | |
795 | return NULL; | |
796 | } | |
6aab341e LT |
797 | |
798 | /* | |
7e675137 | 799 | * NOTE! We still have PageReserved() pages in the page tables. |
7e675137 | 800 | * eg. VDSO mappings can cause them to exist. |
6aab341e | 801 | */ |
b379d790 | 802 | out: |
6aab341e | 803 | return pfn_to_page(pfn); |
ee498ed7 HD |
804 | } |
805 | ||
28093f9f GS |
806 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
807 | struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr, | |
808 | pmd_t pmd) | |
809 | { | |
810 | unsigned long pfn = pmd_pfn(pmd); | |
811 | ||
812 | /* | |
813 | * There is no pmd_special() but there may be special pmds, e.g. | |
814 | * in a direct-access (dax) mapping, so let's just replicate the | |
815 | * !HAVE_PTE_SPECIAL case from vm_normal_page() here. | |
816 | */ | |
817 | if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) { | |
818 | if (vma->vm_flags & VM_MIXEDMAP) { | |
819 | if (!pfn_valid(pfn)) | |
820 | return NULL; | |
821 | goto out; | |
822 | } else { | |
823 | unsigned long off; | |
824 | off = (addr - vma->vm_start) >> PAGE_SHIFT; | |
825 | if (pfn == vma->vm_pgoff + off) | |
826 | return NULL; | |
827 | if (!is_cow_mapping(vma->vm_flags)) | |
828 | return NULL; | |
829 | } | |
830 | } | |
831 | ||
832 | if (is_zero_pfn(pfn)) | |
833 | return NULL; | |
834 | if (unlikely(pfn > highest_memmap_pfn)) | |
835 | return NULL; | |
836 | ||
837 | /* | |
838 | * NOTE! We still have PageReserved() pages in the page tables. | |
839 | * eg. VDSO mappings can cause them to exist. | |
840 | */ | |
841 | out: | |
842 | return pfn_to_page(pfn); | |
843 | } | |
844 | #endif | |
845 | ||
1da177e4 LT |
846 | /* |
847 | * copy one vm_area from one task to the other. Assumes the page tables | |
848 | * already present in the new task to be cleared in the whole range | |
849 | * covered by this vma. | |
1da177e4 LT |
850 | */ |
851 | ||
570a335b | 852 | static inline unsigned long |
1da177e4 | 853 | copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, |
b5810039 | 854 | pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma, |
8c103762 | 855 | unsigned long addr, int *rss) |
1da177e4 | 856 | { |
b5810039 | 857 | unsigned long vm_flags = vma->vm_flags; |
1da177e4 LT |
858 | pte_t pte = *src_pte; |
859 | struct page *page; | |
1da177e4 LT |
860 | |
861 | /* pte contains position in swap or file, so copy. */ | |
862 | if (unlikely(!pte_present(pte))) { | |
0661a336 KS |
863 | swp_entry_t entry = pte_to_swp_entry(pte); |
864 | ||
865 | if (likely(!non_swap_entry(entry))) { | |
866 | if (swap_duplicate(entry) < 0) | |
867 | return entry.val; | |
868 | ||
869 | /* make sure dst_mm is on swapoff's mmlist. */ | |
870 | if (unlikely(list_empty(&dst_mm->mmlist))) { | |
871 | spin_lock(&mmlist_lock); | |
872 | if (list_empty(&dst_mm->mmlist)) | |
873 | list_add(&dst_mm->mmlist, | |
874 | &src_mm->mmlist); | |
875 | spin_unlock(&mmlist_lock); | |
876 | } | |
877 | rss[MM_SWAPENTS]++; | |
878 | } else if (is_migration_entry(entry)) { | |
879 | page = migration_entry_to_page(entry); | |
880 | ||
eca56ff9 | 881 | rss[mm_counter(page)]++; |
0661a336 KS |
882 | |
883 | if (is_write_migration_entry(entry) && | |
884 | is_cow_mapping(vm_flags)) { | |
885 | /* | |
886 | * COW mappings require pages in both | |
887 | * parent and child to be set to read. | |
888 | */ | |
889 | make_migration_entry_read(&entry); | |
890 | pte = swp_entry_to_pte(entry); | |
891 | if (pte_swp_soft_dirty(*src_pte)) | |
892 | pte = pte_swp_mksoft_dirty(pte); | |
893 | set_pte_at(src_mm, addr, src_pte, pte); | |
0697212a | 894 | } |
1da177e4 | 895 | } |
ae859762 | 896 | goto out_set_pte; |
1da177e4 LT |
897 | } |
898 | ||
1da177e4 LT |
899 | /* |
900 | * If it's a COW mapping, write protect it both | |
901 | * in the parent and the child | |
902 | */ | |
67121172 | 903 | if (is_cow_mapping(vm_flags)) { |
1da177e4 | 904 | ptep_set_wrprotect(src_mm, addr, src_pte); |
3dc90795 | 905 | pte = pte_wrprotect(pte); |
1da177e4 LT |
906 | } |
907 | ||
908 | /* | |
909 | * If it's a shared mapping, mark it clean in | |
910 | * the child | |
911 | */ | |
912 | if (vm_flags & VM_SHARED) | |
913 | pte = pte_mkclean(pte); | |
914 | pte = pte_mkold(pte); | |
6aab341e LT |
915 | |
916 | page = vm_normal_page(vma, addr, pte); | |
917 | if (page) { | |
918 | get_page(page); | |
53f9263b | 919 | page_dup_rmap(page, false); |
eca56ff9 | 920 | rss[mm_counter(page)]++; |
6aab341e | 921 | } |
ae859762 HD |
922 | |
923 | out_set_pte: | |
924 | set_pte_at(dst_mm, addr, dst_pte, pte); | |
570a335b | 925 | return 0; |
1da177e4 LT |
926 | } |
927 | ||
21bda264 | 928 | static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, |
71e3aac0 AA |
929 | pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma, |
930 | unsigned long addr, unsigned long end) | |
1da177e4 | 931 | { |
c36987e2 | 932 | pte_t *orig_src_pte, *orig_dst_pte; |
1da177e4 | 933 | pte_t *src_pte, *dst_pte; |
c74df32c | 934 | spinlock_t *src_ptl, *dst_ptl; |
e040f218 | 935 | int progress = 0; |
d559db08 | 936 | int rss[NR_MM_COUNTERS]; |
570a335b | 937 | swp_entry_t entry = (swp_entry_t){0}; |
1da177e4 LT |
938 | |
939 | again: | |
d559db08 KH |
940 | init_rss_vec(rss); |
941 | ||
c74df32c | 942 | dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl); |
1da177e4 LT |
943 | if (!dst_pte) |
944 | return -ENOMEM; | |
ece0e2b6 | 945 | src_pte = pte_offset_map(src_pmd, addr); |
4c21e2f2 | 946 | src_ptl = pte_lockptr(src_mm, src_pmd); |
f20dc5f7 | 947 | spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); |
c36987e2 DN |
948 | orig_src_pte = src_pte; |
949 | orig_dst_pte = dst_pte; | |
6606c3e0 | 950 | arch_enter_lazy_mmu_mode(); |
1da177e4 | 951 | |
1da177e4 LT |
952 | do { |
953 | /* | |
954 | * We are holding two locks at this point - either of them | |
955 | * could generate latencies in another task on another CPU. | |
956 | */ | |
e040f218 HD |
957 | if (progress >= 32) { |
958 | progress = 0; | |
959 | if (need_resched() || | |
95c354fe | 960 | spin_needbreak(src_ptl) || spin_needbreak(dst_ptl)) |
e040f218 HD |
961 | break; |
962 | } | |
1da177e4 LT |
963 | if (pte_none(*src_pte)) { |
964 | progress++; | |
965 | continue; | |
966 | } | |
570a335b HD |
967 | entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, |
968 | vma, addr, rss); | |
969 | if (entry.val) | |
970 | break; | |
1da177e4 LT |
971 | progress += 8; |
972 | } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); | |
1da177e4 | 973 | |
6606c3e0 | 974 | arch_leave_lazy_mmu_mode(); |
c74df32c | 975 | spin_unlock(src_ptl); |
ece0e2b6 | 976 | pte_unmap(orig_src_pte); |
d559db08 | 977 | add_mm_rss_vec(dst_mm, rss); |
c36987e2 | 978 | pte_unmap_unlock(orig_dst_pte, dst_ptl); |
c74df32c | 979 | cond_resched(); |
570a335b HD |
980 | |
981 | if (entry.val) { | |
982 | if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) | |
983 | return -ENOMEM; | |
984 | progress = 0; | |
985 | } | |
1da177e4 LT |
986 | if (addr != end) |
987 | goto again; | |
988 | return 0; | |
989 | } | |
990 | ||
991 | static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, | |
992 | pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma, | |
993 | unsigned long addr, unsigned long end) | |
994 | { | |
995 | pmd_t *src_pmd, *dst_pmd; | |
996 | unsigned long next; | |
997 | ||
998 | dst_pmd = pmd_alloc(dst_mm, dst_pud, addr); | |
999 | if (!dst_pmd) | |
1000 | return -ENOMEM; | |
1001 | src_pmd = pmd_offset(src_pud, addr); | |
1002 | do { | |
1003 | next = pmd_addr_end(addr, end); | |
5c7fb56e | 1004 | if (pmd_trans_huge(*src_pmd) || pmd_devmap(*src_pmd)) { |
71e3aac0 | 1005 | int err; |
a00cc7d9 | 1006 | VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, vma); |
71e3aac0 AA |
1007 | err = copy_huge_pmd(dst_mm, src_mm, |
1008 | dst_pmd, src_pmd, addr, vma); | |
1009 | if (err == -ENOMEM) | |
1010 | return -ENOMEM; | |
1011 | if (!err) | |
1012 | continue; | |
1013 | /* fall through */ | |
1014 | } | |
1da177e4 LT |
1015 | if (pmd_none_or_clear_bad(src_pmd)) |
1016 | continue; | |
1017 | if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd, | |
1018 | vma, addr, next)) | |
1019 | return -ENOMEM; | |
1020 | } while (dst_pmd++, src_pmd++, addr = next, addr != end); | |
1021 | return 0; | |
1022 | } | |
1023 | ||
1024 | static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, | |
1025 | pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma, | |
1026 | unsigned long addr, unsigned long end) | |
1027 | { | |
1028 | pud_t *src_pud, *dst_pud; | |
1029 | unsigned long next; | |
1030 | ||
1031 | dst_pud = pud_alloc(dst_mm, dst_pgd, addr); | |
1032 | if (!dst_pud) | |
1033 | return -ENOMEM; | |
1034 | src_pud = pud_offset(src_pgd, addr); | |
1035 | do { | |
1036 | next = pud_addr_end(addr, end); | |
a00cc7d9 MW |
1037 | if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) { |
1038 | int err; | |
1039 | ||
1040 | VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, vma); | |
1041 | err = copy_huge_pud(dst_mm, src_mm, | |
1042 | dst_pud, src_pud, addr, vma); | |
1043 | if (err == -ENOMEM) | |
1044 | return -ENOMEM; | |
1045 | if (!err) | |
1046 | continue; | |
1047 | /* fall through */ | |
1048 | } | |
1da177e4 LT |
1049 | if (pud_none_or_clear_bad(src_pud)) |
1050 | continue; | |
1051 | if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud, | |
1052 | vma, addr, next)) | |
1053 | return -ENOMEM; | |
1054 | } while (dst_pud++, src_pud++, addr = next, addr != end); | |
1055 | return 0; | |
1056 | } | |
1057 | ||
1058 | int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, | |
1059 | struct vm_area_struct *vma) | |
1060 | { | |
1061 | pgd_t *src_pgd, *dst_pgd; | |
1062 | unsigned long next; | |
1063 | unsigned long addr = vma->vm_start; | |
1064 | unsigned long end = vma->vm_end; | |
2ec74c3e SG |
1065 | unsigned long mmun_start; /* For mmu_notifiers */ |
1066 | unsigned long mmun_end; /* For mmu_notifiers */ | |
1067 | bool is_cow; | |
cddb8a5c | 1068 | int ret; |
1da177e4 | 1069 | |
d992895b NP |
1070 | /* |
1071 | * Don't copy ptes where a page fault will fill them correctly. | |
1072 | * Fork becomes much lighter when there are big shared or private | |
1073 | * readonly mappings. The tradeoff is that copy_page_range is more | |
1074 | * efficient than faulting. | |
1075 | */ | |
0661a336 KS |
1076 | if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) && |
1077 | !vma->anon_vma) | |
1078 | return 0; | |
d992895b | 1079 | |
1da177e4 LT |
1080 | if (is_vm_hugetlb_page(vma)) |
1081 | return copy_hugetlb_page_range(dst_mm, src_mm, vma); | |
1082 | ||
b3b9c293 | 1083 | if (unlikely(vma->vm_flags & VM_PFNMAP)) { |
2ab64037 | 1084 | /* |
1085 | * We do not free on error cases below as remove_vma | |
1086 | * gets called on error from higher level routine | |
1087 | */ | |
5180da41 | 1088 | ret = track_pfn_copy(vma); |
2ab64037 | 1089 | if (ret) |
1090 | return ret; | |
1091 | } | |
1092 | ||
cddb8a5c AA |
1093 | /* |
1094 | * We need to invalidate the secondary MMU mappings only when | |
1095 | * there could be a permission downgrade on the ptes of the | |
1096 | * parent mm. And a permission downgrade will only happen if | |
1097 | * is_cow_mapping() returns true. | |
1098 | */ | |
2ec74c3e SG |
1099 | is_cow = is_cow_mapping(vma->vm_flags); |
1100 | mmun_start = addr; | |
1101 | mmun_end = end; | |
1102 | if (is_cow) | |
1103 | mmu_notifier_invalidate_range_start(src_mm, mmun_start, | |
1104 | mmun_end); | |
cddb8a5c AA |
1105 | |
1106 | ret = 0; | |
1da177e4 LT |
1107 | dst_pgd = pgd_offset(dst_mm, addr); |
1108 | src_pgd = pgd_offset(src_mm, addr); | |
1109 | do { | |
1110 | next = pgd_addr_end(addr, end); | |
1111 | if (pgd_none_or_clear_bad(src_pgd)) | |
1112 | continue; | |
cddb8a5c AA |
1113 | if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd, |
1114 | vma, addr, next))) { | |
1115 | ret = -ENOMEM; | |
1116 | break; | |
1117 | } | |
1da177e4 | 1118 | } while (dst_pgd++, src_pgd++, addr = next, addr != end); |
cddb8a5c | 1119 | |
2ec74c3e SG |
1120 | if (is_cow) |
1121 | mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end); | |
cddb8a5c | 1122 | return ret; |
1da177e4 LT |
1123 | } |
1124 | ||
51c6f666 | 1125 | static unsigned long zap_pte_range(struct mmu_gather *tlb, |
b5810039 | 1126 | struct vm_area_struct *vma, pmd_t *pmd, |
1da177e4 | 1127 | unsigned long addr, unsigned long end, |
97a89413 | 1128 | struct zap_details *details) |
1da177e4 | 1129 | { |
b5810039 | 1130 | struct mm_struct *mm = tlb->mm; |
d16dfc55 | 1131 | int force_flush = 0; |
d559db08 | 1132 | int rss[NR_MM_COUNTERS]; |
97a89413 | 1133 | spinlock_t *ptl; |
5f1a1907 | 1134 | pte_t *start_pte; |
97a89413 | 1135 | pte_t *pte; |
8a5f14a2 | 1136 | swp_entry_t entry; |
d559db08 | 1137 | |
07e32661 | 1138 | tlb_remove_check_page_size_change(tlb, PAGE_SIZE); |
d16dfc55 | 1139 | again: |
e303297e | 1140 | init_rss_vec(rss); |
5f1a1907 SR |
1141 | start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl); |
1142 | pte = start_pte; | |
6606c3e0 | 1143 | arch_enter_lazy_mmu_mode(); |
1da177e4 LT |
1144 | do { |
1145 | pte_t ptent = *pte; | |
166f61b9 | 1146 | if (pte_none(ptent)) |
1da177e4 | 1147 | continue; |
6f5e6b9e | 1148 | |
1da177e4 | 1149 | if (pte_present(ptent)) { |
ee498ed7 | 1150 | struct page *page; |
51c6f666 | 1151 | |
6aab341e | 1152 | page = vm_normal_page(vma, addr, ptent); |
1da177e4 LT |
1153 | if (unlikely(details) && page) { |
1154 | /* | |
1155 | * unmap_shared_mapping_pages() wants to | |
1156 | * invalidate cache without truncating: | |
1157 | * unmap shared but keep private pages. | |
1158 | */ | |
1159 | if (details->check_mapping && | |
800d8c63 | 1160 | details->check_mapping != page_rmapping(page)) |
1da177e4 | 1161 | continue; |
1da177e4 | 1162 | } |
b5810039 | 1163 | ptent = ptep_get_and_clear_full(mm, addr, pte, |
a600388d | 1164 | tlb->fullmm); |
1da177e4 LT |
1165 | tlb_remove_tlb_entry(tlb, pte, addr); |
1166 | if (unlikely(!page)) | |
1167 | continue; | |
eca56ff9 JM |
1168 | |
1169 | if (!PageAnon(page)) { | |
1cf35d47 LT |
1170 | if (pte_dirty(ptent)) { |
1171 | force_flush = 1; | |
6237bcd9 | 1172 | set_page_dirty(page); |
1cf35d47 | 1173 | } |
4917e5d0 | 1174 | if (pte_young(ptent) && |
64363aad | 1175 | likely(!(vma->vm_flags & VM_SEQ_READ))) |
bf3f3bc5 | 1176 | mark_page_accessed(page); |
6237bcd9 | 1177 | } |
eca56ff9 | 1178 | rss[mm_counter(page)]--; |
d281ee61 | 1179 | page_remove_rmap(page, false); |
3dc14741 HD |
1180 | if (unlikely(page_mapcount(page) < 0)) |
1181 | print_bad_pte(vma, addr, ptent, page); | |
e9d55e15 | 1182 | if (unlikely(__tlb_remove_page(tlb, page))) { |
1cf35d47 | 1183 | force_flush = 1; |
ce9ec37b | 1184 | addr += PAGE_SIZE; |
d16dfc55 | 1185 | break; |
1cf35d47 | 1186 | } |
1da177e4 LT |
1187 | continue; |
1188 | } | |
3e8715fd KS |
1189 | /* If details->check_mapping, we leave swap entries. */ |
1190 | if (unlikely(details)) | |
1da177e4 | 1191 | continue; |
b084d435 | 1192 | |
8a5f14a2 KS |
1193 | entry = pte_to_swp_entry(ptent); |
1194 | if (!non_swap_entry(entry)) | |
1195 | rss[MM_SWAPENTS]--; | |
1196 | else if (is_migration_entry(entry)) { | |
1197 | struct page *page; | |
9f9f1acd | 1198 | |
8a5f14a2 | 1199 | page = migration_entry_to_page(entry); |
eca56ff9 | 1200 | rss[mm_counter(page)]--; |
b084d435 | 1201 | } |
8a5f14a2 KS |
1202 | if (unlikely(!free_swap_and_cache(entry))) |
1203 | print_bad_pte(vma, addr, ptent, NULL); | |
9888a1ca | 1204 | pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); |
97a89413 | 1205 | } while (pte++, addr += PAGE_SIZE, addr != end); |
ae859762 | 1206 | |
d559db08 | 1207 | add_mm_rss_vec(mm, rss); |
6606c3e0 | 1208 | arch_leave_lazy_mmu_mode(); |
51c6f666 | 1209 | |
1cf35d47 | 1210 | /* Do the actual TLB flush before dropping ptl */ |
fb7332a9 | 1211 | if (force_flush) |
1cf35d47 | 1212 | tlb_flush_mmu_tlbonly(tlb); |
1cf35d47 LT |
1213 | pte_unmap_unlock(start_pte, ptl); |
1214 | ||
1215 | /* | |
1216 | * If we forced a TLB flush (either due to running out of | |
1217 | * batch buffers or because we needed to flush dirty TLB | |
1218 | * entries before releasing the ptl), free the batched | |
1219 | * memory too. Restart if we didn't do everything. | |
1220 | */ | |
1221 | if (force_flush) { | |
1222 | force_flush = 0; | |
1223 | tlb_flush_mmu_free(tlb); | |
2b047252 | 1224 | if (addr != end) |
d16dfc55 PZ |
1225 | goto again; |
1226 | } | |
1227 | ||
51c6f666 | 1228 | return addr; |
1da177e4 LT |
1229 | } |
1230 | ||
51c6f666 | 1231 | static inline unsigned long zap_pmd_range(struct mmu_gather *tlb, |
b5810039 | 1232 | struct vm_area_struct *vma, pud_t *pud, |
1da177e4 | 1233 | unsigned long addr, unsigned long end, |
97a89413 | 1234 | struct zap_details *details) |
1da177e4 LT |
1235 | { |
1236 | pmd_t *pmd; | |
1237 | unsigned long next; | |
1238 | ||
1239 | pmd = pmd_offset(pud, addr); | |
1240 | do { | |
1241 | next = pmd_addr_end(addr, end); | |
5c7fb56e | 1242 | if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) { |
1a5a9906 | 1243 | if (next - addr != HPAGE_PMD_SIZE) { |
68428398 HD |
1244 | VM_BUG_ON_VMA(vma_is_anonymous(vma) && |
1245 | !rwsem_is_locked(&tlb->mm->mmap_sem), vma); | |
fd60775a | 1246 | __split_huge_pmd(vma, pmd, addr, false, NULL); |
f21760b1 | 1247 | } else if (zap_huge_pmd(tlb, vma, pmd, addr)) |
1a5a9906 | 1248 | goto next; |
71e3aac0 AA |
1249 | /* fall through */ |
1250 | } | |
1a5a9906 AA |
1251 | /* |
1252 | * Here there can be other concurrent MADV_DONTNEED or | |
1253 | * trans huge page faults running, and if the pmd is | |
1254 | * none or trans huge it can change under us. This is | |
1255 | * because MADV_DONTNEED holds the mmap_sem in read | |
1256 | * mode. | |
1257 | */ | |
1258 | if (pmd_none_or_trans_huge_or_clear_bad(pmd)) | |
1259 | goto next; | |
97a89413 | 1260 | next = zap_pte_range(tlb, vma, pmd, addr, next, details); |
1a5a9906 | 1261 | next: |
97a89413 PZ |
1262 | cond_resched(); |
1263 | } while (pmd++, addr = next, addr != end); | |
51c6f666 RH |
1264 | |
1265 | return addr; | |
1da177e4 LT |
1266 | } |
1267 | ||
51c6f666 | 1268 | static inline unsigned long zap_pud_range(struct mmu_gather *tlb, |
b5810039 | 1269 | struct vm_area_struct *vma, pgd_t *pgd, |
1da177e4 | 1270 | unsigned long addr, unsigned long end, |
97a89413 | 1271 | struct zap_details *details) |
1da177e4 LT |
1272 | { |
1273 | pud_t *pud; | |
1274 | unsigned long next; | |
1275 | ||
1276 | pud = pud_offset(pgd, addr); | |
1277 | do { | |
1278 | next = pud_addr_end(addr, end); | |
a00cc7d9 MW |
1279 | if (pud_trans_huge(*pud) || pud_devmap(*pud)) { |
1280 | if (next - addr != HPAGE_PUD_SIZE) { | |
1281 | VM_BUG_ON_VMA(!rwsem_is_locked(&tlb->mm->mmap_sem), vma); | |
1282 | split_huge_pud(vma, pud, addr); | |
1283 | } else if (zap_huge_pud(tlb, vma, pud, addr)) | |
1284 | goto next; | |
1285 | /* fall through */ | |
1286 | } | |
97a89413 | 1287 | if (pud_none_or_clear_bad(pud)) |
1da177e4 | 1288 | continue; |
97a89413 | 1289 | next = zap_pmd_range(tlb, vma, pud, addr, next, details); |
a00cc7d9 MW |
1290 | next: |
1291 | cond_resched(); | |
97a89413 | 1292 | } while (pud++, addr = next, addr != end); |
51c6f666 RH |
1293 | |
1294 | return addr; | |
1da177e4 LT |
1295 | } |
1296 | ||
aac45363 | 1297 | void unmap_page_range(struct mmu_gather *tlb, |
038c7aa1 AV |
1298 | struct vm_area_struct *vma, |
1299 | unsigned long addr, unsigned long end, | |
1300 | struct zap_details *details) | |
1da177e4 LT |
1301 | { |
1302 | pgd_t *pgd; | |
1303 | unsigned long next; | |
1304 | ||
1da177e4 LT |
1305 | BUG_ON(addr >= end); |
1306 | tlb_start_vma(tlb, vma); | |
1307 | pgd = pgd_offset(vma->vm_mm, addr); | |
1308 | do { | |
1309 | next = pgd_addr_end(addr, end); | |
97a89413 | 1310 | if (pgd_none_or_clear_bad(pgd)) |
1da177e4 | 1311 | continue; |
97a89413 PZ |
1312 | next = zap_pud_range(tlb, vma, pgd, addr, next, details); |
1313 | } while (pgd++, addr = next, addr != end); | |
1da177e4 LT |
1314 | tlb_end_vma(tlb, vma); |
1315 | } | |
51c6f666 | 1316 | |
f5cc4eef AV |
1317 | |
1318 | static void unmap_single_vma(struct mmu_gather *tlb, | |
1319 | struct vm_area_struct *vma, unsigned long start_addr, | |
4f74d2c8 | 1320 | unsigned long end_addr, |
f5cc4eef AV |
1321 | struct zap_details *details) |
1322 | { | |
1323 | unsigned long start = max(vma->vm_start, start_addr); | |
1324 | unsigned long end; | |
1325 | ||
1326 | if (start >= vma->vm_end) | |
1327 | return; | |
1328 | end = min(vma->vm_end, end_addr); | |
1329 | if (end <= vma->vm_start) | |
1330 | return; | |
1331 | ||
cbc91f71 SD |
1332 | if (vma->vm_file) |
1333 | uprobe_munmap(vma, start, end); | |
1334 | ||
b3b9c293 | 1335 | if (unlikely(vma->vm_flags & VM_PFNMAP)) |
5180da41 | 1336 | untrack_pfn(vma, 0, 0); |
f5cc4eef AV |
1337 | |
1338 | if (start != end) { | |
1339 | if (unlikely(is_vm_hugetlb_page(vma))) { | |
1340 | /* | |
1341 | * It is undesirable to test vma->vm_file as it | |
1342 | * should be non-null for valid hugetlb area. | |
1343 | * However, vm_file will be NULL in the error | |
7aa6b4ad | 1344 | * cleanup path of mmap_region. When |
f5cc4eef | 1345 | * hugetlbfs ->mmap method fails, |
7aa6b4ad | 1346 | * mmap_region() nullifies vma->vm_file |
f5cc4eef AV |
1347 | * before calling this function to clean up. |
1348 | * Since no pte has actually been setup, it is | |
1349 | * safe to do nothing in this case. | |
1350 | */ | |
24669e58 | 1351 | if (vma->vm_file) { |
83cde9e8 | 1352 | i_mmap_lock_write(vma->vm_file->f_mapping); |
d833352a | 1353 | __unmap_hugepage_range_final(tlb, vma, start, end, NULL); |
83cde9e8 | 1354 | i_mmap_unlock_write(vma->vm_file->f_mapping); |
24669e58 | 1355 | } |
f5cc4eef AV |
1356 | } else |
1357 | unmap_page_range(tlb, vma, start, end, details); | |
1358 | } | |
1da177e4 LT |
1359 | } |
1360 | ||
1da177e4 LT |
1361 | /** |
1362 | * unmap_vmas - unmap a range of memory covered by a list of vma's | |
0164f69d | 1363 | * @tlb: address of the caller's struct mmu_gather |
1da177e4 LT |
1364 | * @vma: the starting vma |
1365 | * @start_addr: virtual address at which to start unmapping | |
1366 | * @end_addr: virtual address at which to end unmapping | |
1da177e4 | 1367 | * |
508034a3 | 1368 | * Unmap all pages in the vma list. |
1da177e4 | 1369 | * |
1da177e4 LT |
1370 | * Only addresses between `start' and `end' will be unmapped. |
1371 | * | |
1372 | * The VMA list must be sorted in ascending virtual address order. | |
1373 | * | |
1374 | * unmap_vmas() assumes that the caller will flush the whole unmapped address | |
1375 | * range after unmap_vmas() returns. So the only responsibility here is to | |
1376 | * ensure that any thus-far unmapped pages are flushed before unmap_vmas() | |
1377 | * drops the lock and schedules. | |
1378 | */ | |
6e8bb019 | 1379 | void unmap_vmas(struct mmu_gather *tlb, |
1da177e4 | 1380 | struct vm_area_struct *vma, unsigned long start_addr, |
4f74d2c8 | 1381 | unsigned long end_addr) |
1da177e4 | 1382 | { |
cddb8a5c | 1383 | struct mm_struct *mm = vma->vm_mm; |
1da177e4 | 1384 | |
cddb8a5c | 1385 | mmu_notifier_invalidate_range_start(mm, start_addr, end_addr); |
f5cc4eef | 1386 | for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) |
4f74d2c8 | 1387 | unmap_single_vma(tlb, vma, start_addr, end_addr, NULL); |
cddb8a5c | 1388 | mmu_notifier_invalidate_range_end(mm, start_addr, end_addr); |
1da177e4 LT |
1389 | } |
1390 | ||
1391 | /** | |
1392 | * zap_page_range - remove user pages in a given range | |
1393 | * @vma: vm_area_struct holding the applicable pages | |
eb4546bb | 1394 | * @start: starting address of pages to zap |
1da177e4 | 1395 | * @size: number of bytes to zap |
f5cc4eef AV |
1396 | * |
1397 | * Caller must protect the VMA list | |
1da177e4 | 1398 | */ |
7e027b14 | 1399 | void zap_page_range(struct vm_area_struct *vma, unsigned long start, |
ecf1385d | 1400 | unsigned long size) |
1da177e4 LT |
1401 | { |
1402 | struct mm_struct *mm = vma->vm_mm; | |
d16dfc55 | 1403 | struct mmu_gather tlb; |
7e027b14 | 1404 | unsigned long end = start + size; |
1da177e4 | 1405 | |
1da177e4 | 1406 | lru_add_drain(); |
2b047252 | 1407 | tlb_gather_mmu(&tlb, mm, start, end); |
365e9c87 | 1408 | update_hiwater_rss(mm); |
7e027b14 LT |
1409 | mmu_notifier_invalidate_range_start(mm, start, end); |
1410 | for ( ; vma && vma->vm_start < end; vma = vma->vm_next) | |
ecf1385d | 1411 | unmap_single_vma(&tlb, vma, start, end, NULL); |
7e027b14 LT |
1412 | mmu_notifier_invalidate_range_end(mm, start, end); |
1413 | tlb_finish_mmu(&tlb, start, end); | |
1da177e4 LT |
1414 | } |
1415 | ||
f5cc4eef AV |
1416 | /** |
1417 | * zap_page_range_single - remove user pages in a given range | |
1418 | * @vma: vm_area_struct holding the applicable pages | |
1419 | * @address: starting address of pages to zap | |
1420 | * @size: number of bytes to zap | |
8a5f14a2 | 1421 | * @details: details of shared cache invalidation |
f5cc4eef AV |
1422 | * |
1423 | * The range must fit into one VMA. | |
1da177e4 | 1424 | */ |
f5cc4eef | 1425 | static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address, |
1da177e4 LT |
1426 | unsigned long size, struct zap_details *details) |
1427 | { | |
1428 | struct mm_struct *mm = vma->vm_mm; | |
d16dfc55 | 1429 | struct mmu_gather tlb; |
1da177e4 | 1430 | unsigned long end = address + size; |
1da177e4 | 1431 | |
1da177e4 | 1432 | lru_add_drain(); |
2b047252 | 1433 | tlb_gather_mmu(&tlb, mm, address, end); |
365e9c87 | 1434 | update_hiwater_rss(mm); |
f5cc4eef | 1435 | mmu_notifier_invalidate_range_start(mm, address, end); |
4f74d2c8 | 1436 | unmap_single_vma(&tlb, vma, address, end, details); |
f5cc4eef | 1437 | mmu_notifier_invalidate_range_end(mm, address, end); |
d16dfc55 | 1438 | tlb_finish_mmu(&tlb, address, end); |
1da177e4 LT |
1439 | } |
1440 | ||
c627f9cc JS |
1441 | /** |
1442 | * zap_vma_ptes - remove ptes mapping the vma | |
1443 | * @vma: vm_area_struct holding ptes to be zapped | |
1444 | * @address: starting address of pages to zap | |
1445 | * @size: number of bytes to zap | |
1446 | * | |
1447 | * This function only unmaps ptes assigned to VM_PFNMAP vmas. | |
1448 | * | |
1449 | * The entire address range must be fully contained within the vma. | |
1450 | * | |
1451 | * Returns 0 if successful. | |
1452 | */ | |
1453 | int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, | |
1454 | unsigned long size) | |
1455 | { | |
1456 | if (address < vma->vm_start || address + size > vma->vm_end || | |
1457 | !(vma->vm_flags & VM_PFNMAP)) | |
1458 | return -1; | |
f5cc4eef | 1459 | zap_page_range_single(vma, address, size, NULL); |
c627f9cc JS |
1460 | return 0; |
1461 | } | |
1462 | EXPORT_SYMBOL_GPL(zap_vma_ptes); | |
1463 | ||
25ca1d6c | 1464 | pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, |
920c7a5d | 1465 | spinlock_t **ptl) |
c9cfcddf | 1466 | { |
166f61b9 TH |
1467 | pgd_t *pgd = pgd_offset(mm, addr); |
1468 | pud_t *pud = pud_alloc(mm, pgd, addr); | |
c9cfcddf | 1469 | if (pud) { |
166f61b9 | 1470 | pmd_t *pmd = pmd_alloc(mm, pud, addr); |
f66055ab AA |
1471 | if (pmd) { |
1472 | VM_BUG_ON(pmd_trans_huge(*pmd)); | |
c9cfcddf | 1473 | return pte_alloc_map_lock(mm, pmd, addr, ptl); |
f66055ab | 1474 | } |
c9cfcddf LT |
1475 | } |
1476 | return NULL; | |
1477 | } | |
1478 | ||
238f58d8 LT |
1479 | /* |
1480 | * This is the old fallback for page remapping. | |
1481 | * | |
1482 | * For historical reasons, it only allows reserved pages. Only | |
1483 | * old drivers should use this, and they needed to mark their | |
1484 | * pages reserved for the old functions anyway. | |
1485 | */ | |
423bad60 NP |
1486 | static int insert_page(struct vm_area_struct *vma, unsigned long addr, |
1487 | struct page *page, pgprot_t prot) | |
238f58d8 | 1488 | { |
423bad60 | 1489 | struct mm_struct *mm = vma->vm_mm; |
238f58d8 | 1490 | int retval; |
c9cfcddf | 1491 | pte_t *pte; |
8a9f3ccd BS |
1492 | spinlock_t *ptl; |
1493 | ||
238f58d8 | 1494 | retval = -EINVAL; |
a145dd41 | 1495 | if (PageAnon(page)) |
5b4e655e | 1496 | goto out; |
238f58d8 LT |
1497 | retval = -ENOMEM; |
1498 | flush_dcache_page(page); | |
c9cfcddf | 1499 | pte = get_locked_pte(mm, addr, &ptl); |
238f58d8 | 1500 | if (!pte) |
5b4e655e | 1501 | goto out; |
238f58d8 LT |
1502 | retval = -EBUSY; |
1503 | if (!pte_none(*pte)) | |
1504 | goto out_unlock; | |
1505 | ||
1506 | /* Ok, finally just insert the thing.. */ | |
1507 | get_page(page); | |
eca56ff9 | 1508 | inc_mm_counter_fast(mm, mm_counter_file(page)); |
dd78fedd | 1509 | page_add_file_rmap(page, false); |
238f58d8 LT |
1510 | set_pte_at(mm, addr, pte, mk_pte(page, prot)); |
1511 | ||
1512 | retval = 0; | |
8a9f3ccd BS |
1513 | pte_unmap_unlock(pte, ptl); |
1514 | return retval; | |
238f58d8 LT |
1515 | out_unlock: |
1516 | pte_unmap_unlock(pte, ptl); | |
1517 | out: | |
1518 | return retval; | |
1519 | } | |
1520 | ||
bfa5bf6d REB |
1521 | /** |
1522 | * vm_insert_page - insert single page into user vma | |
1523 | * @vma: user vma to map to | |
1524 | * @addr: target user address of this page | |
1525 | * @page: source kernel page | |
1526 | * | |
a145dd41 LT |
1527 | * This allows drivers to insert individual pages they've allocated |
1528 | * into a user vma. | |
1529 | * | |
1530 | * The page has to be a nice clean _individual_ kernel allocation. | |
1531 | * If you allocate a compound page, you need to have marked it as | |
1532 | * such (__GFP_COMP), or manually just split the page up yourself | |
8dfcc9ba | 1533 | * (see split_page()). |
a145dd41 LT |
1534 | * |
1535 | * NOTE! Traditionally this was done with "remap_pfn_range()" which | |
1536 | * took an arbitrary page protection parameter. This doesn't allow | |
1537 | * that. Your vma protection will have to be set up correctly, which | |
1538 | * means that if you want a shared writable mapping, you'd better | |
1539 | * ask for a shared writable mapping! | |
1540 | * | |
1541 | * The page does not need to be reserved. | |
4b6e1e37 KK |
1542 | * |
1543 | * Usually this function is called from f_op->mmap() handler | |
1544 | * under mm->mmap_sem write-lock, so it can change vma->vm_flags. | |
1545 | * Caller must set VM_MIXEDMAP on vma if it wants to call this | |
1546 | * function from other places, for example from page-fault handler. | |
a145dd41 | 1547 | */ |
423bad60 NP |
1548 | int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, |
1549 | struct page *page) | |
a145dd41 LT |
1550 | { |
1551 | if (addr < vma->vm_start || addr >= vma->vm_end) | |
1552 | return -EFAULT; | |
1553 | if (!page_count(page)) | |
1554 | return -EINVAL; | |
4b6e1e37 KK |
1555 | if (!(vma->vm_flags & VM_MIXEDMAP)) { |
1556 | BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem)); | |
1557 | BUG_ON(vma->vm_flags & VM_PFNMAP); | |
1558 | vma->vm_flags |= VM_MIXEDMAP; | |
1559 | } | |
423bad60 | 1560 | return insert_page(vma, addr, page, vma->vm_page_prot); |
a145dd41 | 1561 | } |
e3c3374f | 1562 | EXPORT_SYMBOL(vm_insert_page); |
a145dd41 | 1563 | |
423bad60 | 1564 | static int insert_pfn(struct vm_area_struct *vma, unsigned long addr, |
01c8f1c4 | 1565 | pfn_t pfn, pgprot_t prot) |
423bad60 NP |
1566 | { |
1567 | struct mm_struct *mm = vma->vm_mm; | |
1568 | int retval; | |
1569 | pte_t *pte, entry; | |
1570 | spinlock_t *ptl; | |
1571 | ||
1572 | retval = -ENOMEM; | |
1573 | pte = get_locked_pte(mm, addr, &ptl); | |
1574 | if (!pte) | |
1575 | goto out; | |
1576 | retval = -EBUSY; | |
1577 | if (!pte_none(*pte)) | |
1578 | goto out_unlock; | |
1579 | ||
1580 | /* Ok, finally just insert the thing.. */ | |
01c8f1c4 DW |
1581 | if (pfn_t_devmap(pfn)) |
1582 | entry = pte_mkdevmap(pfn_t_pte(pfn, prot)); | |
1583 | else | |
1584 | entry = pte_mkspecial(pfn_t_pte(pfn, prot)); | |
423bad60 | 1585 | set_pte_at(mm, addr, pte, entry); |
4b3073e1 | 1586 | update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */ |
423bad60 NP |
1587 | |
1588 | retval = 0; | |
1589 | out_unlock: | |
1590 | pte_unmap_unlock(pte, ptl); | |
1591 | out: | |
1592 | return retval; | |
1593 | } | |
1594 | ||
e0dc0d8f NP |
1595 | /** |
1596 | * vm_insert_pfn - insert single pfn into user vma | |
1597 | * @vma: user vma to map to | |
1598 | * @addr: target user address of this page | |
1599 | * @pfn: source kernel pfn | |
1600 | * | |
c462f179 | 1601 | * Similar to vm_insert_page, this allows drivers to insert individual pages |
e0dc0d8f NP |
1602 | * they've allocated into a user vma. Same comments apply. |
1603 | * | |
1604 | * This function should only be called from a vm_ops->fault handler, and | |
1605 | * in that case the handler should return NULL. | |
0d71d10a NP |
1606 | * |
1607 | * vma cannot be a COW mapping. | |
1608 | * | |
1609 | * As this is called only for pages that do not currently exist, we | |
1610 | * do not need to flush old virtual caches or the TLB. | |
e0dc0d8f NP |
1611 | */ |
1612 | int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr, | |
423bad60 | 1613 | unsigned long pfn) |
1745cbc5 AL |
1614 | { |
1615 | return vm_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot); | |
1616 | } | |
1617 | EXPORT_SYMBOL(vm_insert_pfn); | |
1618 | ||
1619 | /** | |
1620 | * vm_insert_pfn_prot - insert single pfn into user vma with specified pgprot | |
1621 | * @vma: user vma to map to | |
1622 | * @addr: target user address of this page | |
1623 | * @pfn: source kernel pfn | |
1624 | * @pgprot: pgprot flags for the inserted page | |
1625 | * | |
1626 | * This is exactly like vm_insert_pfn, except that it allows drivers to | |
1627 | * to override pgprot on a per-page basis. | |
1628 | * | |
1629 | * This only makes sense for IO mappings, and it makes no sense for | |
1630 | * cow mappings. In general, using multiple vmas is preferable; | |
1631 | * vm_insert_pfn_prot should only be used if using multiple VMAs is | |
1632 | * impractical. | |
1633 | */ | |
1634 | int vm_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, | |
1635 | unsigned long pfn, pgprot_t pgprot) | |
e0dc0d8f | 1636 | { |
2ab64037 | 1637 | int ret; |
7e675137 NP |
1638 | /* |
1639 | * Technically, architectures with pte_special can avoid all these | |
1640 | * restrictions (same for remap_pfn_range). However we would like | |
1641 | * consistency in testing and feature parity among all, so we should | |
1642 | * try to keep these invariants in place for everybody. | |
1643 | */ | |
b379d790 JH |
1644 | BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); |
1645 | BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == | |
1646 | (VM_PFNMAP|VM_MIXEDMAP)); | |
1647 | BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); | |
1648 | BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn)); | |
e0dc0d8f | 1649 | |
423bad60 NP |
1650 | if (addr < vma->vm_start || addr >= vma->vm_end) |
1651 | return -EFAULT; | |
308a047c BP |
1652 | |
1653 | track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)); | |
2ab64037 | 1654 | |
01c8f1c4 | 1655 | ret = insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot); |
2ab64037 | 1656 | |
2ab64037 | 1657 | return ret; |
423bad60 | 1658 | } |
1745cbc5 | 1659 | EXPORT_SYMBOL(vm_insert_pfn_prot); |
e0dc0d8f | 1660 | |
423bad60 | 1661 | int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr, |
01c8f1c4 | 1662 | pfn_t pfn) |
423bad60 | 1663 | { |
87744ab3 DW |
1664 | pgprot_t pgprot = vma->vm_page_prot; |
1665 | ||
423bad60 | 1666 | BUG_ON(!(vma->vm_flags & VM_MIXEDMAP)); |
e0dc0d8f | 1667 | |
423bad60 NP |
1668 | if (addr < vma->vm_start || addr >= vma->vm_end) |
1669 | return -EFAULT; | |
308a047c BP |
1670 | |
1671 | track_pfn_insert(vma, &pgprot, pfn); | |
e0dc0d8f | 1672 | |
423bad60 NP |
1673 | /* |
1674 | * If we don't have pte special, then we have to use the pfn_valid() | |
1675 | * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must* | |
1676 | * refcount the page if pfn_valid is true (hence insert_page rather | |
62eede62 HD |
1677 | * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP |
1678 | * without pte special, it would there be refcounted as a normal page. | |
423bad60 | 1679 | */ |
03fc2da6 | 1680 | if (!HAVE_PTE_SPECIAL && !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) { |
423bad60 NP |
1681 | struct page *page; |
1682 | ||
03fc2da6 DW |
1683 | /* |
1684 | * At this point we are committed to insert_page() | |
1685 | * regardless of whether the caller specified flags that | |
1686 | * result in pfn_t_has_page() == false. | |
1687 | */ | |
1688 | page = pfn_to_page(pfn_t_to_pfn(pfn)); | |
87744ab3 | 1689 | return insert_page(vma, addr, page, pgprot); |
423bad60 | 1690 | } |
87744ab3 | 1691 | return insert_pfn(vma, addr, pfn, pgprot); |
e0dc0d8f | 1692 | } |
423bad60 | 1693 | EXPORT_SYMBOL(vm_insert_mixed); |
e0dc0d8f | 1694 | |
1da177e4 LT |
1695 | /* |
1696 | * maps a range of physical memory into the requested pages. the old | |
1697 | * mappings are removed. any references to nonexistent pages results | |
1698 | * in null mappings (currently treated as "copy-on-access") | |
1699 | */ | |
1700 | static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd, | |
1701 | unsigned long addr, unsigned long end, | |
1702 | unsigned long pfn, pgprot_t prot) | |
1703 | { | |
1704 | pte_t *pte; | |
c74df32c | 1705 | spinlock_t *ptl; |
1da177e4 | 1706 | |
c74df32c | 1707 | pte = pte_alloc_map_lock(mm, pmd, addr, &ptl); |
1da177e4 LT |
1708 | if (!pte) |
1709 | return -ENOMEM; | |
6606c3e0 | 1710 | arch_enter_lazy_mmu_mode(); |
1da177e4 LT |
1711 | do { |
1712 | BUG_ON(!pte_none(*pte)); | |
7e675137 | 1713 | set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot))); |
1da177e4 LT |
1714 | pfn++; |
1715 | } while (pte++, addr += PAGE_SIZE, addr != end); | |
6606c3e0 | 1716 | arch_leave_lazy_mmu_mode(); |
c74df32c | 1717 | pte_unmap_unlock(pte - 1, ptl); |
1da177e4 LT |
1718 | return 0; |
1719 | } | |
1720 | ||
1721 | static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud, | |
1722 | unsigned long addr, unsigned long end, | |
1723 | unsigned long pfn, pgprot_t prot) | |
1724 | { | |
1725 | pmd_t *pmd; | |
1726 | unsigned long next; | |
1727 | ||
1728 | pfn -= addr >> PAGE_SHIFT; | |
1729 | pmd = pmd_alloc(mm, pud, addr); | |
1730 | if (!pmd) | |
1731 | return -ENOMEM; | |
f66055ab | 1732 | VM_BUG_ON(pmd_trans_huge(*pmd)); |
1da177e4 LT |
1733 | do { |
1734 | next = pmd_addr_end(addr, end); | |
1735 | if (remap_pte_range(mm, pmd, addr, next, | |
1736 | pfn + (addr >> PAGE_SHIFT), prot)) | |
1737 | return -ENOMEM; | |
1738 | } while (pmd++, addr = next, addr != end); | |
1739 | return 0; | |
1740 | } | |
1741 | ||
1742 | static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd, | |
1743 | unsigned long addr, unsigned long end, | |
1744 | unsigned long pfn, pgprot_t prot) | |
1745 | { | |
1746 | pud_t *pud; | |
1747 | unsigned long next; | |
1748 | ||
1749 | pfn -= addr >> PAGE_SHIFT; | |
1750 | pud = pud_alloc(mm, pgd, addr); | |
1751 | if (!pud) | |
1752 | return -ENOMEM; | |
1753 | do { | |
1754 | next = pud_addr_end(addr, end); | |
1755 | if (remap_pmd_range(mm, pud, addr, next, | |
1756 | pfn + (addr >> PAGE_SHIFT), prot)) | |
1757 | return -ENOMEM; | |
1758 | } while (pud++, addr = next, addr != end); | |
1759 | return 0; | |
1760 | } | |
1761 | ||
bfa5bf6d REB |
1762 | /** |
1763 | * remap_pfn_range - remap kernel memory to userspace | |
1764 | * @vma: user vma to map to | |
1765 | * @addr: target user address to start at | |
1766 | * @pfn: physical address of kernel memory | |
1767 | * @size: size of map area | |
1768 | * @prot: page protection flags for this mapping | |
1769 | * | |
1770 | * Note: this is only safe if the mm semaphore is held when called. | |
1771 | */ | |
1da177e4 LT |
1772 | int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, |
1773 | unsigned long pfn, unsigned long size, pgprot_t prot) | |
1774 | { | |
1775 | pgd_t *pgd; | |
1776 | unsigned long next; | |
2d15cab8 | 1777 | unsigned long end = addr + PAGE_ALIGN(size); |
1da177e4 | 1778 | struct mm_struct *mm = vma->vm_mm; |
d5957d2f | 1779 | unsigned long remap_pfn = pfn; |
1da177e4 LT |
1780 | int err; |
1781 | ||
1782 | /* | |
1783 | * Physically remapped pages are special. Tell the | |
1784 | * rest of the world about it: | |
1785 | * VM_IO tells people not to look at these pages | |
1786 | * (accesses can have side effects). | |
6aab341e LT |
1787 | * VM_PFNMAP tells the core MM that the base pages are just |
1788 | * raw PFN mappings, and do not have a "struct page" associated | |
1789 | * with them. | |
314e51b9 KK |
1790 | * VM_DONTEXPAND |
1791 | * Disable vma merging and expanding with mremap(). | |
1792 | * VM_DONTDUMP | |
1793 | * Omit vma from core dump, even when VM_IO turned off. | |
fb155c16 LT |
1794 | * |
1795 | * There's a horrible special case to handle copy-on-write | |
1796 | * behaviour that some programs depend on. We mark the "original" | |
1797 | * un-COW'ed pages by matching them up with "vma->vm_pgoff". | |
b3b9c293 | 1798 | * See vm_normal_page() for details. |
1da177e4 | 1799 | */ |
b3b9c293 KK |
1800 | if (is_cow_mapping(vma->vm_flags)) { |
1801 | if (addr != vma->vm_start || end != vma->vm_end) | |
1802 | return -EINVAL; | |
fb155c16 | 1803 | vma->vm_pgoff = pfn; |
b3b9c293 KK |
1804 | } |
1805 | ||
d5957d2f | 1806 | err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size)); |
b3b9c293 | 1807 | if (err) |
3c8bb73a | 1808 | return -EINVAL; |
fb155c16 | 1809 | |
314e51b9 | 1810 | vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; |
1da177e4 LT |
1811 | |
1812 | BUG_ON(addr >= end); | |
1813 | pfn -= addr >> PAGE_SHIFT; | |
1814 | pgd = pgd_offset(mm, addr); | |
1815 | flush_cache_range(vma, addr, end); | |
1da177e4 LT |
1816 | do { |
1817 | next = pgd_addr_end(addr, end); | |
1818 | err = remap_pud_range(mm, pgd, addr, next, | |
1819 | pfn + (addr >> PAGE_SHIFT), prot); | |
1820 | if (err) | |
1821 | break; | |
1822 | } while (pgd++, addr = next, addr != end); | |
2ab64037 | 1823 | |
1824 | if (err) | |
d5957d2f | 1825 | untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size)); |
2ab64037 | 1826 | |
1da177e4 LT |
1827 | return err; |
1828 | } | |
1829 | EXPORT_SYMBOL(remap_pfn_range); | |
1830 | ||
b4cbb197 LT |
1831 | /** |
1832 | * vm_iomap_memory - remap memory to userspace | |
1833 | * @vma: user vma to map to | |
1834 | * @start: start of area | |
1835 | * @len: size of area | |
1836 | * | |
1837 | * This is a simplified io_remap_pfn_range() for common driver use. The | |
1838 | * driver just needs to give us the physical memory range to be mapped, | |
1839 | * we'll figure out the rest from the vma information. | |
1840 | * | |
1841 | * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get | |
1842 | * whatever write-combining details or similar. | |
1843 | */ | |
1844 | int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) | |
1845 | { | |
1846 | unsigned long vm_len, pfn, pages; | |
1847 | ||
1848 | /* Check that the physical memory area passed in looks valid */ | |
1849 | if (start + len < start) | |
1850 | return -EINVAL; | |
1851 | /* | |
1852 | * You *really* shouldn't map things that aren't page-aligned, | |
1853 | * but we've historically allowed it because IO memory might | |
1854 | * just have smaller alignment. | |
1855 | */ | |
1856 | len += start & ~PAGE_MASK; | |
1857 | pfn = start >> PAGE_SHIFT; | |
1858 | pages = (len + ~PAGE_MASK) >> PAGE_SHIFT; | |
1859 | if (pfn + pages < pfn) | |
1860 | return -EINVAL; | |
1861 | ||
1862 | /* We start the mapping 'vm_pgoff' pages into the area */ | |
1863 | if (vma->vm_pgoff > pages) | |
1864 | return -EINVAL; | |
1865 | pfn += vma->vm_pgoff; | |
1866 | pages -= vma->vm_pgoff; | |
1867 | ||
1868 | /* Can we fit all of the mapping? */ | |
1869 | vm_len = vma->vm_end - vma->vm_start; | |
1870 | if (vm_len >> PAGE_SHIFT > pages) | |
1871 | return -EINVAL; | |
1872 | ||
1873 | /* Ok, let it rip */ | |
1874 | return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); | |
1875 | } | |
1876 | EXPORT_SYMBOL(vm_iomap_memory); | |
1877 | ||
aee16b3c JF |
1878 | static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd, |
1879 | unsigned long addr, unsigned long end, | |
1880 | pte_fn_t fn, void *data) | |
1881 | { | |
1882 | pte_t *pte; | |
1883 | int err; | |
2f569afd | 1884 | pgtable_t token; |
94909914 | 1885 | spinlock_t *uninitialized_var(ptl); |
aee16b3c JF |
1886 | |
1887 | pte = (mm == &init_mm) ? | |
1888 | pte_alloc_kernel(pmd, addr) : | |
1889 | pte_alloc_map_lock(mm, pmd, addr, &ptl); | |
1890 | if (!pte) | |
1891 | return -ENOMEM; | |
1892 | ||
1893 | BUG_ON(pmd_huge(*pmd)); | |
1894 | ||
38e0edb1 JF |
1895 | arch_enter_lazy_mmu_mode(); |
1896 | ||
2f569afd | 1897 | token = pmd_pgtable(*pmd); |
aee16b3c JF |
1898 | |
1899 | do { | |
c36987e2 | 1900 | err = fn(pte++, token, addr, data); |
aee16b3c JF |
1901 | if (err) |
1902 | break; | |
c36987e2 | 1903 | } while (addr += PAGE_SIZE, addr != end); |
aee16b3c | 1904 | |
38e0edb1 JF |
1905 | arch_leave_lazy_mmu_mode(); |
1906 | ||
aee16b3c JF |
1907 | if (mm != &init_mm) |
1908 | pte_unmap_unlock(pte-1, ptl); | |
1909 | return err; | |
1910 | } | |
1911 | ||
1912 | static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, | |
1913 | unsigned long addr, unsigned long end, | |
1914 | pte_fn_t fn, void *data) | |
1915 | { | |
1916 | pmd_t *pmd; | |
1917 | unsigned long next; | |
1918 | int err; | |
1919 | ||
ceb86879 AK |
1920 | BUG_ON(pud_huge(*pud)); |
1921 | ||
aee16b3c JF |
1922 | pmd = pmd_alloc(mm, pud, addr); |
1923 | if (!pmd) | |
1924 | return -ENOMEM; | |
1925 | do { | |
1926 | next = pmd_addr_end(addr, end); | |
1927 | err = apply_to_pte_range(mm, pmd, addr, next, fn, data); | |
1928 | if (err) | |
1929 | break; | |
1930 | } while (pmd++, addr = next, addr != end); | |
1931 | return err; | |
1932 | } | |
1933 | ||
1934 | static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd, | |
1935 | unsigned long addr, unsigned long end, | |
1936 | pte_fn_t fn, void *data) | |
1937 | { | |
1938 | pud_t *pud; | |
1939 | unsigned long next; | |
1940 | int err; | |
1941 | ||
1942 | pud = pud_alloc(mm, pgd, addr); | |
1943 | if (!pud) | |
1944 | return -ENOMEM; | |
1945 | do { | |
1946 | next = pud_addr_end(addr, end); | |
1947 | err = apply_to_pmd_range(mm, pud, addr, next, fn, data); | |
1948 | if (err) | |
1949 | break; | |
1950 | } while (pud++, addr = next, addr != end); | |
1951 | return err; | |
1952 | } | |
1953 | ||
1954 | /* | |
1955 | * Scan a region of virtual memory, filling in page tables as necessary | |
1956 | * and calling a provided function on each leaf page table. | |
1957 | */ | |
1958 | int apply_to_page_range(struct mm_struct *mm, unsigned long addr, | |
1959 | unsigned long size, pte_fn_t fn, void *data) | |
1960 | { | |
1961 | pgd_t *pgd; | |
1962 | unsigned long next; | |
57250a5b | 1963 | unsigned long end = addr + size; |
aee16b3c JF |
1964 | int err; |
1965 | ||
9cb65bc3 MP |
1966 | if (WARN_ON(addr >= end)) |
1967 | return -EINVAL; | |
1968 | ||
aee16b3c JF |
1969 | pgd = pgd_offset(mm, addr); |
1970 | do { | |
1971 | next = pgd_addr_end(addr, end); | |
1972 | err = apply_to_pud_range(mm, pgd, addr, next, fn, data); | |
1973 | if (err) | |
1974 | break; | |
1975 | } while (pgd++, addr = next, addr != end); | |
57250a5b | 1976 | |
aee16b3c JF |
1977 | return err; |
1978 | } | |
1979 | EXPORT_SYMBOL_GPL(apply_to_page_range); | |
1980 | ||
8f4e2101 | 1981 | /* |
9b4bdd2f KS |
1982 | * handle_pte_fault chooses page fault handler according to an entry which was |
1983 | * read non-atomically. Before making any commitment, on those architectures | |
1984 | * or configurations (e.g. i386 with PAE) which might give a mix of unmatched | |
1985 | * parts, do_swap_page must check under lock before unmapping the pte and | |
1986 | * proceeding (but do_wp_page is only called after already making such a check; | |
a335b2e1 | 1987 | * and do_anonymous_page can safely check later on). |
8f4e2101 | 1988 | */ |
4c21e2f2 | 1989 | static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, |
8f4e2101 HD |
1990 | pte_t *page_table, pte_t orig_pte) |
1991 | { | |
1992 | int same = 1; | |
1993 | #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) | |
1994 | if (sizeof(pte_t) > sizeof(unsigned long)) { | |
4c21e2f2 HD |
1995 | spinlock_t *ptl = pte_lockptr(mm, pmd); |
1996 | spin_lock(ptl); | |
8f4e2101 | 1997 | same = pte_same(*page_table, orig_pte); |
4c21e2f2 | 1998 | spin_unlock(ptl); |
8f4e2101 HD |
1999 | } |
2000 | #endif | |
2001 | pte_unmap(page_table); | |
2002 | return same; | |
2003 | } | |
2004 | ||
9de455b2 | 2005 | static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma) |
6aab341e | 2006 | { |
0abdd7a8 DW |
2007 | debug_dma_assert_idle(src); |
2008 | ||
6aab341e LT |
2009 | /* |
2010 | * If the source page was a PFN mapping, we don't have | |
2011 | * a "struct page" for it. We do a best-effort copy by | |
2012 | * just copying from the original user address. If that | |
2013 | * fails, we just zero-fill it. Live with it. | |
2014 | */ | |
2015 | if (unlikely(!src)) { | |
9b04c5fe | 2016 | void *kaddr = kmap_atomic(dst); |
5d2a2dbb LT |
2017 | void __user *uaddr = (void __user *)(va & PAGE_MASK); |
2018 | ||
2019 | /* | |
2020 | * This really shouldn't fail, because the page is there | |
2021 | * in the page tables. But it might just be unreadable, | |
2022 | * in which case we just give up and fill the result with | |
2023 | * zeroes. | |
2024 | */ | |
2025 | if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) | |
3ecb01df | 2026 | clear_page(kaddr); |
9b04c5fe | 2027 | kunmap_atomic(kaddr); |
c4ec7b0d | 2028 | flush_dcache_page(dst); |
0ed361de NP |
2029 | } else |
2030 | copy_user_highpage(dst, src, va, vma); | |
6aab341e LT |
2031 | } |
2032 | ||
c20cd45e MH |
2033 | static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma) |
2034 | { | |
2035 | struct file *vm_file = vma->vm_file; | |
2036 | ||
2037 | if (vm_file) | |
2038 | return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO; | |
2039 | ||
2040 | /* | |
2041 | * Special mappings (e.g. VDSO) do not have any file so fake | |
2042 | * a default GFP_KERNEL for them. | |
2043 | */ | |
2044 | return GFP_KERNEL; | |
2045 | } | |
2046 | ||
fb09a464 KS |
2047 | /* |
2048 | * Notify the address space that the page is about to become writable so that | |
2049 | * it can prohibit this or wait for the page to get into an appropriate state. | |
2050 | * | |
2051 | * We do this without the lock held, so that it can sleep if it needs to. | |
2052 | */ | |
38b8cb7f | 2053 | static int do_page_mkwrite(struct vm_fault *vmf) |
fb09a464 | 2054 | { |
fb09a464 | 2055 | int ret; |
38b8cb7f JK |
2056 | struct page *page = vmf->page; |
2057 | unsigned int old_flags = vmf->flags; | |
fb09a464 | 2058 | |
38b8cb7f | 2059 | vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; |
fb09a464 | 2060 | |
11bac800 | 2061 | ret = vmf->vma->vm_ops->page_mkwrite(vmf); |
38b8cb7f JK |
2062 | /* Restore original flags so that caller is not surprised */ |
2063 | vmf->flags = old_flags; | |
fb09a464 KS |
2064 | if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) |
2065 | return ret; | |
2066 | if (unlikely(!(ret & VM_FAULT_LOCKED))) { | |
2067 | lock_page(page); | |
2068 | if (!page->mapping) { | |
2069 | unlock_page(page); | |
2070 | return 0; /* retry */ | |
2071 | } | |
2072 | ret |= VM_FAULT_LOCKED; | |
2073 | } else | |
2074 | VM_BUG_ON_PAGE(!PageLocked(page), page); | |
2075 | return ret; | |
2076 | } | |
2077 | ||
97ba0c2b JK |
2078 | /* |
2079 | * Handle dirtying of a page in shared file mapping on a write fault. | |
2080 | * | |
2081 | * The function expects the page to be locked and unlocks it. | |
2082 | */ | |
2083 | static void fault_dirty_shared_page(struct vm_area_struct *vma, | |
2084 | struct page *page) | |
2085 | { | |
2086 | struct address_space *mapping; | |
2087 | bool dirtied; | |
2088 | bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite; | |
2089 | ||
2090 | dirtied = set_page_dirty(page); | |
2091 | VM_BUG_ON_PAGE(PageAnon(page), page); | |
2092 | /* | |
2093 | * Take a local copy of the address_space - page.mapping may be zeroed | |
2094 | * by truncate after unlock_page(). The address_space itself remains | |
2095 | * pinned by vma->vm_file's reference. We rely on unlock_page()'s | |
2096 | * release semantics to prevent the compiler from undoing this copying. | |
2097 | */ | |
2098 | mapping = page_rmapping(page); | |
2099 | unlock_page(page); | |
2100 | ||
2101 | if ((dirtied || page_mkwrite) && mapping) { | |
2102 | /* | |
2103 | * Some device drivers do not set page.mapping | |
2104 | * but still dirty their pages | |
2105 | */ | |
2106 | balance_dirty_pages_ratelimited(mapping); | |
2107 | } | |
2108 | ||
2109 | if (!page_mkwrite) | |
2110 | file_update_time(vma->vm_file); | |
2111 | } | |
2112 | ||
4e047f89 SR |
2113 | /* |
2114 | * Handle write page faults for pages that can be reused in the current vma | |
2115 | * | |
2116 | * This can happen either due to the mapping being with the VM_SHARED flag, | |
2117 | * or due to us being the last reference standing to the page. In either | |
2118 | * case, all we need to do here is to mark the page as writable and update | |
2119 | * any related book-keeping. | |
2120 | */ | |
997dd98d | 2121 | static inline void wp_page_reuse(struct vm_fault *vmf) |
82b0f8c3 | 2122 | __releases(vmf->ptl) |
4e047f89 | 2123 | { |
82b0f8c3 | 2124 | struct vm_area_struct *vma = vmf->vma; |
a41b70d6 | 2125 | struct page *page = vmf->page; |
4e047f89 SR |
2126 | pte_t entry; |
2127 | /* | |
2128 | * Clear the pages cpupid information as the existing | |
2129 | * information potentially belongs to a now completely | |
2130 | * unrelated process. | |
2131 | */ | |
2132 | if (page) | |
2133 | page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1); | |
2134 | ||
2994302b JK |
2135 | flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); |
2136 | entry = pte_mkyoung(vmf->orig_pte); | |
4e047f89 | 2137 | entry = maybe_mkwrite(pte_mkdirty(entry), vma); |
82b0f8c3 JK |
2138 | if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) |
2139 | update_mmu_cache(vma, vmf->address, vmf->pte); | |
2140 | pte_unmap_unlock(vmf->pte, vmf->ptl); | |
4e047f89 SR |
2141 | } |
2142 | ||
2f38ab2c SR |
2143 | /* |
2144 | * Handle the case of a page which we actually need to copy to a new page. | |
2145 | * | |
2146 | * Called with mmap_sem locked and the old page referenced, but | |
2147 | * without the ptl held. | |
2148 | * | |
2149 | * High level logic flow: | |
2150 | * | |
2151 | * - Allocate a page, copy the content of the old page to the new one. | |
2152 | * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc. | |
2153 | * - Take the PTL. If the pte changed, bail out and release the allocated page | |
2154 | * - If the pte is still the way we remember it, update the page table and all | |
2155 | * relevant references. This includes dropping the reference the page-table | |
2156 | * held to the old page, as well as updating the rmap. | |
2157 | * - In any case, unlock the PTL and drop the reference we took to the old page. | |
2158 | */ | |
a41b70d6 | 2159 | static int wp_page_copy(struct vm_fault *vmf) |
2f38ab2c | 2160 | { |
82b0f8c3 | 2161 | struct vm_area_struct *vma = vmf->vma; |
bae473a4 | 2162 | struct mm_struct *mm = vma->vm_mm; |
a41b70d6 | 2163 | struct page *old_page = vmf->page; |
2f38ab2c | 2164 | struct page *new_page = NULL; |
2f38ab2c SR |
2165 | pte_t entry; |
2166 | int page_copied = 0; | |
82b0f8c3 | 2167 | const unsigned long mmun_start = vmf->address & PAGE_MASK; |
bae473a4 | 2168 | const unsigned long mmun_end = mmun_start + PAGE_SIZE; |
2f38ab2c SR |
2169 | struct mem_cgroup *memcg; |
2170 | ||
2171 | if (unlikely(anon_vma_prepare(vma))) | |
2172 | goto oom; | |
2173 | ||
2994302b | 2174 | if (is_zero_pfn(pte_pfn(vmf->orig_pte))) { |
82b0f8c3 JK |
2175 | new_page = alloc_zeroed_user_highpage_movable(vma, |
2176 | vmf->address); | |
2f38ab2c SR |
2177 | if (!new_page) |
2178 | goto oom; | |
2179 | } else { | |
bae473a4 | 2180 | new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, |
82b0f8c3 | 2181 | vmf->address); |
2f38ab2c SR |
2182 | if (!new_page) |
2183 | goto oom; | |
82b0f8c3 | 2184 | cow_user_page(new_page, old_page, vmf->address, vma); |
2f38ab2c | 2185 | } |
2f38ab2c | 2186 | |
f627c2f5 | 2187 | if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg, false)) |
2f38ab2c SR |
2188 | goto oom_free_new; |
2189 | ||
eb3c24f3 MG |
2190 | __SetPageUptodate(new_page); |
2191 | ||
2f38ab2c SR |
2192 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
2193 | ||
2194 | /* | |
2195 | * Re-check the pte - we dropped the lock | |
2196 | */ | |
82b0f8c3 | 2197 | vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl); |
2994302b | 2198 | if (likely(pte_same(*vmf->pte, vmf->orig_pte))) { |
2f38ab2c SR |
2199 | if (old_page) { |
2200 | if (!PageAnon(old_page)) { | |
eca56ff9 JM |
2201 | dec_mm_counter_fast(mm, |
2202 | mm_counter_file(old_page)); | |
2f38ab2c SR |
2203 | inc_mm_counter_fast(mm, MM_ANONPAGES); |
2204 | } | |
2205 | } else { | |
2206 | inc_mm_counter_fast(mm, MM_ANONPAGES); | |
2207 | } | |
2994302b | 2208 | flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); |
2f38ab2c SR |
2209 | entry = mk_pte(new_page, vma->vm_page_prot); |
2210 | entry = maybe_mkwrite(pte_mkdirty(entry), vma); | |
2211 | /* | |
2212 | * Clear the pte entry and flush it first, before updating the | |
2213 | * pte with the new entry. This will avoid a race condition | |
2214 | * seen in the presence of one thread doing SMC and another | |
2215 | * thread doing COW. | |
2216 | */ | |
82b0f8c3 JK |
2217 | ptep_clear_flush_notify(vma, vmf->address, vmf->pte); |
2218 | page_add_new_anon_rmap(new_page, vma, vmf->address, false); | |
f627c2f5 | 2219 | mem_cgroup_commit_charge(new_page, memcg, false, false); |
2f38ab2c SR |
2220 | lru_cache_add_active_or_unevictable(new_page, vma); |
2221 | /* | |
2222 | * We call the notify macro here because, when using secondary | |
2223 | * mmu page tables (such as kvm shadow page tables), we want the | |
2224 | * new page to be mapped directly into the secondary page table. | |
2225 | */ | |
82b0f8c3 JK |
2226 | set_pte_at_notify(mm, vmf->address, vmf->pte, entry); |
2227 | update_mmu_cache(vma, vmf->address, vmf->pte); | |
2f38ab2c SR |
2228 | if (old_page) { |
2229 | /* | |
2230 | * Only after switching the pte to the new page may | |
2231 | * we remove the mapcount here. Otherwise another | |
2232 | * process may come and find the rmap count decremented | |
2233 | * before the pte is switched to the new page, and | |
2234 | * "reuse" the old page writing into it while our pte | |
2235 | * here still points into it and can be read by other | |
2236 | * threads. | |
2237 | * | |
2238 | * The critical issue is to order this | |
2239 | * page_remove_rmap with the ptp_clear_flush above. | |
2240 | * Those stores are ordered by (if nothing else,) | |
2241 | * the barrier present in the atomic_add_negative | |
2242 | * in page_remove_rmap. | |
2243 | * | |
2244 | * Then the TLB flush in ptep_clear_flush ensures that | |
2245 | * no process can access the old page before the | |
2246 | * decremented mapcount is visible. And the old page | |
2247 | * cannot be reused until after the decremented | |
2248 | * mapcount is visible. So transitively, TLBs to | |
2249 | * old page will be flushed before it can be reused. | |
2250 | */ | |
d281ee61 | 2251 | page_remove_rmap(old_page, false); |
2f38ab2c SR |
2252 | } |
2253 | ||
2254 | /* Free the old page.. */ | |
2255 | new_page = old_page; | |
2256 | page_copied = 1; | |
2257 | } else { | |
f627c2f5 | 2258 | mem_cgroup_cancel_charge(new_page, memcg, false); |
2f38ab2c SR |
2259 | } |
2260 | ||
2261 | if (new_page) | |
09cbfeaf | 2262 | put_page(new_page); |
2f38ab2c | 2263 | |
82b0f8c3 | 2264 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
2f38ab2c SR |
2265 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
2266 | if (old_page) { | |
2267 | /* | |
2268 | * Don't let another task, with possibly unlocked vma, | |
2269 | * keep the mlocked page. | |
2270 | */ | |
2271 | if (page_copied && (vma->vm_flags & VM_LOCKED)) { | |
2272 | lock_page(old_page); /* LRU manipulation */ | |
e90309c9 KS |
2273 | if (PageMlocked(old_page)) |
2274 | munlock_vma_page(old_page); | |
2f38ab2c SR |
2275 | unlock_page(old_page); |
2276 | } | |
09cbfeaf | 2277 | put_page(old_page); |
2f38ab2c SR |
2278 | } |
2279 | return page_copied ? VM_FAULT_WRITE : 0; | |
2280 | oom_free_new: | |
09cbfeaf | 2281 | put_page(new_page); |
2f38ab2c SR |
2282 | oom: |
2283 | if (old_page) | |
09cbfeaf | 2284 | put_page(old_page); |
2f38ab2c SR |
2285 | return VM_FAULT_OOM; |
2286 | } | |
2287 | ||
66a6197c JK |
2288 | /** |
2289 | * finish_mkwrite_fault - finish page fault for a shared mapping, making PTE | |
2290 | * writeable once the page is prepared | |
2291 | * | |
2292 | * @vmf: structure describing the fault | |
2293 | * | |
2294 | * This function handles all that is needed to finish a write page fault in a | |
2295 | * shared mapping due to PTE being read-only once the mapped page is prepared. | |
2296 | * It handles locking of PTE and modifying it. The function returns | |
2297 | * VM_FAULT_WRITE on success, 0 when PTE got changed before we acquired PTE | |
2298 | * lock. | |
2299 | * | |
2300 | * The function expects the page to be locked or other protection against | |
2301 | * concurrent faults / writeback (such as DAX radix tree locks). | |
2302 | */ | |
2303 | int finish_mkwrite_fault(struct vm_fault *vmf) | |
2304 | { | |
2305 | WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED)); | |
2306 | vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address, | |
2307 | &vmf->ptl); | |
2308 | /* | |
2309 | * We might have raced with another page fault while we released the | |
2310 | * pte_offset_map_lock. | |
2311 | */ | |
2312 | if (!pte_same(*vmf->pte, vmf->orig_pte)) { | |
2313 | pte_unmap_unlock(vmf->pte, vmf->ptl); | |
a19e2553 | 2314 | return VM_FAULT_NOPAGE; |
66a6197c JK |
2315 | } |
2316 | wp_page_reuse(vmf); | |
a19e2553 | 2317 | return 0; |
66a6197c JK |
2318 | } |
2319 | ||
dd906184 BH |
2320 | /* |
2321 | * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED | |
2322 | * mapping | |
2323 | */ | |
2994302b | 2324 | static int wp_pfn_shared(struct vm_fault *vmf) |
dd906184 | 2325 | { |
82b0f8c3 | 2326 | struct vm_area_struct *vma = vmf->vma; |
bae473a4 | 2327 | |
dd906184 | 2328 | if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) { |
dd906184 BH |
2329 | int ret; |
2330 | ||
82b0f8c3 | 2331 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
fe82221f | 2332 | vmf->flags |= FAULT_FLAG_MKWRITE; |
11bac800 | 2333 | ret = vma->vm_ops->pfn_mkwrite(vmf); |
2f89dc12 | 2334 | if (ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)) |
dd906184 | 2335 | return ret; |
66a6197c | 2336 | return finish_mkwrite_fault(vmf); |
dd906184 | 2337 | } |
997dd98d JK |
2338 | wp_page_reuse(vmf); |
2339 | return VM_FAULT_WRITE; | |
dd906184 BH |
2340 | } |
2341 | ||
a41b70d6 | 2342 | static int wp_page_shared(struct vm_fault *vmf) |
82b0f8c3 | 2343 | __releases(vmf->ptl) |
93e478d4 | 2344 | { |
82b0f8c3 | 2345 | struct vm_area_struct *vma = vmf->vma; |
93e478d4 | 2346 | |
a41b70d6 | 2347 | get_page(vmf->page); |
93e478d4 | 2348 | |
93e478d4 SR |
2349 | if (vma->vm_ops && vma->vm_ops->page_mkwrite) { |
2350 | int tmp; | |
2351 | ||
82b0f8c3 | 2352 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
38b8cb7f | 2353 | tmp = do_page_mkwrite(vmf); |
93e478d4 SR |
2354 | if (unlikely(!tmp || (tmp & |
2355 | (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { | |
a41b70d6 | 2356 | put_page(vmf->page); |
93e478d4 SR |
2357 | return tmp; |
2358 | } | |
66a6197c | 2359 | tmp = finish_mkwrite_fault(vmf); |
a19e2553 | 2360 | if (unlikely(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) { |
a41b70d6 | 2361 | unlock_page(vmf->page); |
a41b70d6 | 2362 | put_page(vmf->page); |
66a6197c | 2363 | return tmp; |
93e478d4 | 2364 | } |
66a6197c JK |
2365 | } else { |
2366 | wp_page_reuse(vmf); | |
997dd98d | 2367 | lock_page(vmf->page); |
93e478d4 | 2368 | } |
997dd98d JK |
2369 | fault_dirty_shared_page(vma, vmf->page); |
2370 | put_page(vmf->page); | |
93e478d4 | 2371 | |
997dd98d | 2372 | return VM_FAULT_WRITE; |
93e478d4 SR |
2373 | } |
2374 | ||
1da177e4 LT |
2375 | /* |
2376 | * This routine handles present pages, when users try to write | |
2377 | * to a shared page. It is done by copying the page to a new address | |
2378 | * and decrementing the shared-page counter for the old page. | |
2379 | * | |
1da177e4 LT |
2380 | * Note that this routine assumes that the protection checks have been |
2381 | * done by the caller (the low-level page fault routine in most cases). | |
2382 | * Thus we can safely just mark it writable once we've done any necessary | |
2383 | * COW. | |
2384 | * | |
2385 | * We also mark the page dirty at this point even though the page will | |
2386 | * change only once the write actually happens. This avoids a few races, | |
2387 | * and potentially makes it more efficient. | |
2388 | * | |
8f4e2101 HD |
2389 | * We enter with non-exclusive mmap_sem (to exclude vma changes, |
2390 | * but allow concurrent faults), with pte both mapped and locked. | |
2391 | * We return with mmap_sem still held, but pte unmapped and unlocked. | |
1da177e4 | 2392 | */ |
2994302b | 2393 | static int do_wp_page(struct vm_fault *vmf) |
82b0f8c3 | 2394 | __releases(vmf->ptl) |
1da177e4 | 2395 | { |
82b0f8c3 | 2396 | struct vm_area_struct *vma = vmf->vma; |
1da177e4 | 2397 | |
a41b70d6 JK |
2398 | vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte); |
2399 | if (!vmf->page) { | |
251b97f5 | 2400 | /* |
64e45507 PF |
2401 | * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a |
2402 | * VM_PFNMAP VMA. | |
251b97f5 PZ |
2403 | * |
2404 | * We should not cow pages in a shared writeable mapping. | |
dd906184 | 2405 | * Just mark the pages writable and/or call ops->pfn_mkwrite. |
251b97f5 PZ |
2406 | */ |
2407 | if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) == | |
2408 | (VM_WRITE|VM_SHARED)) | |
2994302b | 2409 | return wp_pfn_shared(vmf); |
2f38ab2c | 2410 | |
82b0f8c3 | 2411 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
a41b70d6 | 2412 | return wp_page_copy(vmf); |
251b97f5 | 2413 | } |
1da177e4 | 2414 | |
d08b3851 | 2415 | /* |
ee6a6457 PZ |
2416 | * Take out anonymous pages first, anonymous shared vmas are |
2417 | * not dirty accountable. | |
d08b3851 | 2418 | */ |
a41b70d6 | 2419 | if (PageAnon(vmf->page) && !PageKsm(vmf->page)) { |
6d0a07ed | 2420 | int total_mapcount; |
a41b70d6 JK |
2421 | if (!trylock_page(vmf->page)) { |
2422 | get_page(vmf->page); | |
82b0f8c3 | 2423 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
a41b70d6 | 2424 | lock_page(vmf->page); |
82b0f8c3 JK |
2425 | vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, |
2426 | vmf->address, &vmf->ptl); | |
2994302b | 2427 | if (!pte_same(*vmf->pte, vmf->orig_pte)) { |
a41b70d6 | 2428 | unlock_page(vmf->page); |
82b0f8c3 | 2429 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
a41b70d6 | 2430 | put_page(vmf->page); |
28766805 | 2431 | return 0; |
ab967d86 | 2432 | } |
a41b70d6 | 2433 | put_page(vmf->page); |
ee6a6457 | 2434 | } |
a41b70d6 | 2435 | if (reuse_swap_page(vmf->page, &total_mapcount)) { |
6d0a07ed AA |
2436 | if (total_mapcount == 1) { |
2437 | /* | |
2438 | * The page is all ours. Move it to | |
2439 | * our anon_vma so the rmap code will | |
2440 | * not search our parent or siblings. | |
2441 | * Protected against the rmap code by | |
2442 | * the page lock. | |
2443 | */ | |
a41b70d6 | 2444 | page_move_anon_rmap(vmf->page, vma); |
6d0a07ed | 2445 | } |
a41b70d6 | 2446 | unlock_page(vmf->page); |
997dd98d JK |
2447 | wp_page_reuse(vmf); |
2448 | return VM_FAULT_WRITE; | |
b009c024 | 2449 | } |
a41b70d6 | 2450 | unlock_page(vmf->page); |
ee6a6457 | 2451 | } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) == |
d08b3851 | 2452 | (VM_WRITE|VM_SHARED))) { |
a41b70d6 | 2453 | return wp_page_shared(vmf); |
1da177e4 | 2454 | } |
1da177e4 LT |
2455 | |
2456 | /* | |
2457 | * Ok, we need to copy. Oh, well.. | |
2458 | */ | |
a41b70d6 | 2459 | get_page(vmf->page); |
28766805 | 2460 | |
82b0f8c3 | 2461 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
a41b70d6 | 2462 | return wp_page_copy(vmf); |
1da177e4 LT |
2463 | } |
2464 | ||
97a89413 | 2465 | static void unmap_mapping_range_vma(struct vm_area_struct *vma, |
1da177e4 LT |
2466 | unsigned long start_addr, unsigned long end_addr, |
2467 | struct zap_details *details) | |
2468 | { | |
f5cc4eef | 2469 | zap_page_range_single(vma, start_addr, end_addr - start_addr, details); |
1da177e4 LT |
2470 | } |
2471 | ||
6b2dbba8 | 2472 | static inline void unmap_mapping_range_tree(struct rb_root *root, |
1da177e4 LT |
2473 | struct zap_details *details) |
2474 | { | |
2475 | struct vm_area_struct *vma; | |
1da177e4 LT |
2476 | pgoff_t vba, vea, zba, zea; |
2477 | ||
6b2dbba8 | 2478 | vma_interval_tree_foreach(vma, root, |
1da177e4 | 2479 | details->first_index, details->last_index) { |
1da177e4 LT |
2480 | |
2481 | vba = vma->vm_pgoff; | |
d6e93217 | 2482 | vea = vba + vma_pages(vma) - 1; |
1da177e4 LT |
2483 | zba = details->first_index; |
2484 | if (zba < vba) | |
2485 | zba = vba; | |
2486 | zea = details->last_index; | |
2487 | if (zea > vea) | |
2488 | zea = vea; | |
2489 | ||
97a89413 | 2490 | unmap_mapping_range_vma(vma, |
1da177e4 LT |
2491 | ((zba - vba) << PAGE_SHIFT) + vma->vm_start, |
2492 | ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start, | |
97a89413 | 2493 | details); |
1da177e4 LT |
2494 | } |
2495 | } | |
2496 | ||
1da177e4 | 2497 | /** |
8a5f14a2 KS |
2498 | * unmap_mapping_range - unmap the portion of all mmaps in the specified |
2499 | * address_space corresponding to the specified page range in the underlying | |
2500 | * file. | |
2501 | * | |
3d41088f | 2502 | * @mapping: the address space containing mmaps to be unmapped. |
1da177e4 LT |
2503 | * @holebegin: byte in first page to unmap, relative to the start of |
2504 | * the underlying file. This will be rounded down to a PAGE_SIZE | |
25d9e2d1 | 2505 | * boundary. Note that this is different from truncate_pagecache(), which |
1da177e4 LT |
2506 | * must keep the partial page. In contrast, we must get rid of |
2507 | * partial pages. | |
2508 | * @holelen: size of prospective hole in bytes. This will be rounded | |
2509 | * up to a PAGE_SIZE boundary. A holelen of zero truncates to the | |
2510 | * end of the file. | |
2511 | * @even_cows: 1 when truncating a file, unmap even private COWed pages; | |
2512 | * but 0 when invalidating pagecache, don't throw away private data. | |
2513 | */ | |
2514 | void unmap_mapping_range(struct address_space *mapping, | |
2515 | loff_t const holebegin, loff_t const holelen, int even_cows) | |
2516 | { | |
aac45363 | 2517 | struct zap_details details = { }; |
1da177e4 LT |
2518 | pgoff_t hba = holebegin >> PAGE_SHIFT; |
2519 | pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
2520 | ||
2521 | /* Check for overflow. */ | |
2522 | if (sizeof(holelen) > sizeof(hlen)) { | |
2523 | long long holeend = | |
2524 | (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
2525 | if (holeend & ~(long long)ULONG_MAX) | |
2526 | hlen = ULONG_MAX - hba + 1; | |
2527 | } | |
2528 | ||
166f61b9 | 2529 | details.check_mapping = even_cows ? NULL : mapping; |
1da177e4 LT |
2530 | details.first_index = hba; |
2531 | details.last_index = hba + hlen - 1; | |
2532 | if (details.last_index < details.first_index) | |
2533 | details.last_index = ULONG_MAX; | |
1da177e4 | 2534 | |
46c043ed | 2535 | i_mmap_lock_write(mapping); |
6b2dbba8 | 2536 | if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap))) |
1da177e4 | 2537 | unmap_mapping_range_tree(&mapping->i_mmap, &details); |
46c043ed | 2538 | i_mmap_unlock_write(mapping); |
1da177e4 LT |
2539 | } |
2540 | EXPORT_SYMBOL(unmap_mapping_range); | |
2541 | ||
1da177e4 | 2542 | /* |
8f4e2101 HD |
2543 | * We enter with non-exclusive mmap_sem (to exclude vma changes, |
2544 | * but allow concurrent faults), and pte mapped but not yet locked. | |
9a95f3cf PC |
2545 | * We return with pte unmapped and unlocked. |
2546 | * | |
2547 | * We return with the mmap_sem locked or unlocked in the same cases | |
2548 | * as does filemap_fault(). | |
1da177e4 | 2549 | */ |
2994302b | 2550 | int do_swap_page(struct vm_fault *vmf) |
1da177e4 | 2551 | { |
82b0f8c3 | 2552 | struct vm_area_struct *vma = vmf->vma; |
56f31801 | 2553 | struct page *page, *swapcache; |
00501b53 | 2554 | struct mem_cgroup *memcg; |
65500d23 | 2555 | swp_entry_t entry; |
1da177e4 | 2556 | pte_t pte; |
d065bd81 | 2557 | int locked; |
ad8c2ee8 | 2558 | int exclusive = 0; |
83c54070 | 2559 | int ret = 0; |
1da177e4 | 2560 | |
2994302b | 2561 | if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte)) |
8f4e2101 | 2562 | goto out; |
65500d23 | 2563 | |
2994302b | 2564 | entry = pte_to_swp_entry(vmf->orig_pte); |
d1737fdb AK |
2565 | if (unlikely(non_swap_entry(entry))) { |
2566 | if (is_migration_entry(entry)) { | |
82b0f8c3 JK |
2567 | migration_entry_wait(vma->vm_mm, vmf->pmd, |
2568 | vmf->address); | |
d1737fdb AK |
2569 | } else if (is_hwpoison_entry(entry)) { |
2570 | ret = VM_FAULT_HWPOISON; | |
2571 | } else { | |
2994302b | 2572 | print_bad_pte(vma, vmf->address, vmf->orig_pte, NULL); |
d99be1a8 | 2573 | ret = VM_FAULT_SIGBUS; |
d1737fdb | 2574 | } |
0697212a CL |
2575 | goto out; |
2576 | } | |
0ff92245 | 2577 | delayacct_set_flag(DELAYACCT_PF_SWAPIN); |
1da177e4 LT |
2578 | page = lookup_swap_cache(entry); |
2579 | if (!page) { | |
82b0f8c3 JK |
2580 | page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, vma, |
2581 | vmf->address); | |
1da177e4 LT |
2582 | if (!page) { |
2583 | /* | |
8f4e2101 HD |
2584 | * Back out if somebody else faulted in this pte |
2585 | * while we released the pte lock. | |
1da177e4 | 2586 | */ |
82b0f8c3 JK |
2587 | vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, |
2588 | vmf->address, &vmf->ptl); | |
2994302b | 2589 | if (likely(pte_same(*vmf->pte, vmf->orig_pte))) |
1da177e4 | 2590 | ret = VM_FAULT_OOM; |
0ff92245 | 2591 | delayacct_clear_flag(DELAYACCT_PF_SWAPIN); |
65500d23 | 2592 | goto unlock; |
1da177e4 LT |
2593 | } |
2594 | ||
2595 | /* Had to read the page from swap area: Major fault */ | |
2596 | ret = VM_FAULT_MAJOR; | |
f8891e5e | 2597 | count_vm_event(PGMAJFAULT); |
bae473a4 | 2598 | mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); |
d1737fdb | 2599 | } else if (PageHWPoison(page)) { |
71f72525 WF |
2600 | /* |
2601 | * hwpoisoned dirty swapcache pages are kept for killing | |
2602 | * owner processes (which may be unknown at hwpoison time) | |
2603 | */ | |
d1737fdb AK |
2604 | ret = VM_FAULT_HWPOISON; |
2605 | delayacct_clear_flag(DELAYACCT_PF_SWAPIN); | |
56f31801 | 2606 | swapcache = page; |
4779cb31 | 2607 | goto out_release; |
1da177e4 LT |
2608 | } |
2609 | ||
56f31801 | 2610 | swapcache = page; |
82b0f8c3 | 2611 | locked = lock_page_or_retry(page, vma->vm_mm, vmf->flags); |
e709ffd6 | 2612 | |
073e587e | 2613 | delayacct_clear_flag(DELAYACCT_PF_SWAPIN); |
d065bd81 ML |
2614 | if (!locked) { |
2615 | ret |= VM_FAULT_RETRY; | |
2616 | goto out_release; | |
2617 | } | |
073e587e | 2618 | |
4969c119 | 2619 | /* |
31c4a3d3 HD |
2620 | * Make sure try_to_free_swap or reuse_swap_page or swapoff did not |
2621 | * release the swapcache from under us. The page pin, and pte_same | |
2622 | * test below, are not enough to exclude that. Even if it is still | |
2623 | * swapcache, we need to check that the page's swap has not changed. | |
4969c119 | 2624 | */ |
31c4a3d3 | 2625 | if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val)) |
4969c119 AA |
2626 | goto out_page; |
2627 | ||
82b0f8c3 | 2628 | page = ksm_might_need_to_copy(page, vma, vmf->address); |
cbf86cfe HD |
2629 | if (unlikely(!page)) { |
2630 | ret = VM_FAULT_OOM; | |
2631 | page = swapcache; | |
cbf86cfe | 2632 | goto out_page; |
5ad64688 HD |
2633 | } |
2634 | ||
bae473a4 KS |
2635 | if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, |
2636 | &memcg, false)) { | |
8a9f3ccd | 2637 | ret = VM_FAULT_OOM; |
bc43f75c | 2638 | goto out_page; |
8a9f3ccd BS |
2639 | } |
2640 | ||
1da177e4 | 2641 | /* |
8f4e2101 | 2642 | * Back out if somebody else already faulted in this pte. |
1da177e4 | 2643 | */ |
82b0f8c3 JK |
2644 | vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, |
2645 | &vmf->ptl); | |
2994302b | 2646 | if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) |
b8107480 | 2647 | goto out_nomap; |
b8107480 KK |
2648 | |
2649 | if (unlikely(!PageUptodate(page))) { | |
2650 | ret = VM_FAULT_SIGBUS; | |
2651 | goto out_nomap; | |
1da177e4 LT |
2652 | } |
2653 | ||
8c7c6e34 KH |
2654 | /* |
2655 | * The page isn't present yet, go ahead with the fault. | |
2656 | * | |
2657 | * Be careful about the sequence of operations here. | |
2658 | * To get its accounting right, reuse_swap_page() must be called | |
2659 | * while the page is counted on swap but not yet in mapcount i.e. | |
2660 | * before page_add_anon_rmap() and swap_free(); try_to_free_swap() | |
2661 | * must be called after the swap_free(), or it will never succeed. | |
8c7c6e34 | 2662 | */ |
1da177e4 | 2663 | |
bae473a4 KS |
2664 | inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); |
2665 | dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS); | |
1da177e4 | 2666 | pte = mk_pte(page, vma->vm_page_prot); |
82b0f8c3 | 2667 | if ((vmf->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) { |
1da177e4 | 2668 | pte = maybe_mkwrite(pte_mkdirty(pte), vma); |
82b0f8c3 | 2669 | vmf->flags &= ~FAULT_FLAG_WRITE; |
9a5b489b | 2670 | ret |= VM_FAULT_WRITE; |
d281ee61 | 2671 | exclusive = RMAP_EXCLUSIVE; |
1da177e4 | 2672 | } |
1da177e4 | 2673 | flush_icache_page(vma, page); |
2994302b | 2674 | if (pte_swp_soft_dirty(vmf->orig_pte)) |
179ef71c | 2675 | pte = pte_mksoft_dirty(pte); |
82b0f8c3 | 2676 | set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); |
2994302b | 2677 | vmf->orig_pte = pte; |
00501b53 | 2678 | if (page == swapcache) { |
82b0f8c3 | 2679 | do_page_add_anon_rmap(page, vma, vmf->address, exclusive); |
f627c2f5 | 2680 | mem_cgroup_commit_charge(page, memcg, true, false); |
1a8018fb | 2681 | activate_page(page); |
00501b53 | 2682 | } else { /* ksm created a completely new copy */ |
82b0f8c3 | 2683 | page_add_new_anon_rmap(page, vma, vmf->address, false); |
f627c2f5 | 2684 | mem_cgroup_commit_charge(page, memcg, false, false); |
00501b53 JW |
2685 | lru_cache_add_active_or_unevictable(page, vma); |
2686 | } | |
1da177e4 | 2687 | |
c475a8ab | 2688 | swap_free(entry); |
5ccc5aba VD |
2689 | if (mem_cgroup_swap_full(page) || |
2690 | (vma->vm_flags & VM_LOCKED) || PageMlocked(page)) | |
a2c43eed | 2691 | try_to_free_swap(page); |
c475a8ab | 2692 | unlock_page(page); |
56f31801 | 2693 | if (page != swapcache) { |
4969c119 AA |
2694 | /* |
2695 | * Hold the lock to avoid the swap entry to be reused | |
2696 | * until we take the PT lock for the pte_same() check | |
2697 | * (to avoid false positives from pte_same). For | |
2698 | * further safety release the lock after the swap_free | |
2699 | * so that the swap count won't change under a | |
2700 | * parallel locked swapcache. | |
2701 | */ | |
2702 | unlock_page(swapcache); | |
09cbfeaf | 2703 | put_page(swapcache); |
4969c119 | 2704 | } |
c475a8ab | 2705 | |
82b0f8c3 | 2706 | if (vmf->flags & FAULT_FLAG_WRITE) { |
2994302b | 2707 | ret |= do_wp_page(vmf); |
61469f1d HD |
2708 | if (ret & VM_FAULT_ERROR) |
2709 | ret &= VM_FAULT_ERROR; | |
1da177e4 LT |
2710 | goto out; |
2711 | } | |
2712 | ||
2713 | /* No need to invalidate - it was non-present before */ | |
82b0f8c3 | 2714 | update_mmu_cache(vma, vmf->address, vmf->pte); |
65500d23 | 2715 | unlock: |
82b0f8c3 | 2716 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
1da177e4 LT |
2717 | out: |
2718 | return ret; | |
b8107480 | 2719 | out_nomap: |
f627c2f5 | 2720 | mem_cgroup_cancel_charge(page, memcg, false); |
82b0f8c3 | 2721 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
bc43f75c | 2722 | out_page: |
b8107480 | 2723 | unlock_page(page); |
4779cb31 | 2724 | out_release: |
09cbfeaf | 2725 | put_page(page); |
56f31801 | 2726 | if (page != swapcache) { |
4969c119 | 2727 | unlock_page(swapcache); |
09cbfeaf | 2728 | put_page(swapcache); |
4969c119 | 2729 | } |
65500d23 | 2730 | return ret; |
1da177e4 LT |
2731 | } |
2732 | ||
320b2b8d | 2733 | /* |
8ca3eb08 TL |
2734 | * This is like a special single-page "expand_{down|up}wards()", |
2735 | * except we must first make sure that 'address{-|+}PAGE_SIZE' | |
320b2b8d | 2736 | * doesn't hit another vma. |
320b2b8d LT |
2737 | */ |
2738 | static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address) | |
2739 | { | |
2740 | address &= PAGE_MASK; | |
2741 | if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) { | |
0e8e50e2 LT |
2742 | struct vm_area_struct *prev = vma->vm_prev; |
2743 | ||
2744 | /* | |
2745 | * Is there a mapping abutting this one below? | |
2746 | * | |
2747 | * That's only ok if it's the same stack mapping | |
2748 | * that has gotten split.. | |
2749 | */ | |
2750 | if (prev && prev->vm_end == address) | |
2751 | return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM; | |
320b2b8d | 2752 | |
fee7e49d | 2753 | return expand_downwards(vma, address - PAGE_SIZE); |
320b2b8d | 2754 | } |
8ca3eb08 TL |
2755 | if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) { |
2756 | struct vm_area_struct *next = vma->vm_next; | |
2757 | ||
2758 | /* As VM_GROWSDOWN but s/below/above/ */ | |
2759 | if (next && next->vm_start == address + PAGE_SIZE) | |
2760 | return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM; | |
2761 | ||
fee7e49d | 2762 | return expand_upwards(vma, address + PAGE_SIZE); |
8ca3eb08 | 2763 | } |
320b2b8d LT |
2764 | return 0; |
2765 | } | |
2766 | ||
1da177e4 | 2767 | /* |
8f4e2101 HD |
2768 | * We enter with non-exclusive mmap_sem (to exclude vma changes, |
2769 | * but allow concurrent faults), and pte mapped but not yet locked. | |
2770 | * We return with mmap_sem still held, but pte unmapped and unlocked. | |
1da177e4 | 2771 | */ |
82b0f8c3 | 2772 | static int do_anonymous_page(struct vm_fault *vmf) |
1da177e4 | 2773 | { |
82b0f8c3 | 2774 | struct vm_area_struct *vma = vmf->vma; |
00501b53 | 2775 | struct mem_cgroup *memcg; |
8f4e2101 | 2776 | struct page *page; |
1da177e4 | 2777 | pte_t entry; |
1da177e4 | 2778 | |
6b7339f4 KS |
2779 | /* File mapping without ->vm_ops ? */ |
2780 | if (vma->vm_flags & VM_SHARED) | |
2781 | return VM_FAULT_SIGBUS; | |
2782 | ||
11ac5524 | 2783 | /* Check if we need to add a guard page to the stack */ |
82b0f8c3 | 2784 | if (check_stack_guard_page(vma, vmf->address) < 0) |
9c145c56 | 2785 | return VM_FAULT_SIGSEGV; |
320b2b8d | 2786 | |
7267ec00 KS |
2787 | /* |
2788 | * Use pte_alloc() instead of pte_alloc_map(). We can't run | |
2789 | * pte_offset_map() on pmds where a huge pmd might be created | |
2790 | * from a different thread. | |
2791 | * | |
2792 | * pte_alloc_map() is safe to use under down_write(mmap_sem) or when | |
2793 | * parallel threads are excluded by other means. | |
2794 | * | |
2795 | * Here we only have down_read(mmap_sem). | |
2796 | */ | |
82b0f8c3 | 2797 | if (pte_alloc(vma->vm_mm, vmf->pmd, vmf->address)) |
7267ec00 KS |
2798 | return VM_FAULT_OOM; |
2799 | ||
2800 | /* See the comment in pte_alloc_one_map() */ | |
82b0f8c3 | 2801 | if (unlikely(pmd_trans_unstable(vmf->pmd))) |
7267ec00 KS |
2802 | return 0; |
2803 | ||
11ac5524 | 2804 | /* Use the zero-page for reads */ |
82b0f8c3 | 2805 | if (!(vmf->flags & FAULT_FLAG_WRITE) && |
bae473a4 | 2806 | !mm_forbids_zeropage(vma->vm_mm)) { |
82b0f8c3 | 2807 | entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address), |
62eede62 | 2808 | vma->vm_page_prot)); |
82b0f8c3 JK |
2809 | vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, |
2810 | vmf->address, &vmf->ptl); | |
2811 | if (!pte_none(*vmf->pte)) | |
a13ea5b7 | 2812 | goto unlock; |
6b251fc9 AA |
2813 | /* Deliver the page fault to userland, check inside PT lock */ |
2814 | if (userfaultfd_missing(vma)) { | |
82b0f8c3 JK |
2815 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
2816 | return handle_userfault(vmf, VM_UFFD_MISSING); | |
6b251fc9 | 2817 | } |
a13ea5b7 HD |
2818 | goto setpte; |
2819 | } | |
2820 | ||
557ed1fa | 2821 | /* Allocate our own private page. */ |
557ed1fa NP |
2822 | if (unlikely(anon_vma_prepare(vma))) |
2823 | goto oom; | |
82b0f8c3 | 2824 | page = alloc_zeroed_user_highpage_movable(vma, vmf->address); |
557ed1fa NP |
2825 | if (!page) |
2826 | goto oom; | |
eb3c24f3 | 2827 | |
bae473a4 | 2828 | if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, &memcg, false)) |
eb3c24f3 MG |
2829 | goto oom_free_page; |
2830 | ||
52f37629 MK |
2831 | /* |
2832 | * The memory barrier inside __SetPageUptodate makes sure that | |
2833 | * preceeding stores to the page contents become visible before | |
2834 | * the set_pte_at() write. | |
2835 | */ | |
0ed361de | 2836 | __SetPageUptodate(page); |
8f4e2101 | 2837 | |
557ed1fa | 2838 | entry = mk_pte(page, vma->vm_page_prot); |
1ac0cb5d HD |
2839 | if (vma->vm_flags & VM_WRITE) |
2840 | entry = pte_mkwrite(pte_mkdirty(entry)); | |
1da177e4 | 2841 | |
82b0f8c3 JK |
2842 | vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, |
2843 | &vmf->ptl); | |
2844 | if (!pte_none(*vmf->pte)) | |
557ed1fa | 2845 | goto release; |
9ba69294 | 2846 | |
6b251fc9 AA |
2847 | /* Deliver the page fault to userland, check inside PT lock */ |
2848 | if (userfaultfd_missing(vma)) { | |
82b0f8c3 | 2849 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
f627c2f5 | 2850 | mem_cgroup_cancel_charge(page, memcg, false); |
09cbfeaf | 2851 | put_page(page); |
82b0f8c3 | 2852 | return handle_userfault(vmf, VM_UFFD_MISSING); |
6b251fc9 AA |
2853 | } |
2854 | ||
bae473a4 | 2855 | inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); |
82b0f8c3 | 2856 | page_add_new_anon_rmap(page, vma, vmf->address, false); |
f627c2f5 | 2857 | mem_cgroup_commit_charge(page, memcg, false, false); |
00501b53 | 2858 | lru_cache_add_active_or_unevictable(page, vma); |
a13ea5b7 | 2859 | setpte: |
82b0f8c3 | 2860 | set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); |
1da177e4 LT |
2861 | |
2862 | /* No need to invalidate - it was non-present before */ | |
82b0f8c3 | 2863 | update_mmu_cache(vma, vmf->address, vmf->pte); |
65500d23 | 2864 | unlock: |
82b0f8c3 | 2865 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
83c54070 | 2866 | return 0; |
8f4e2101 | 2867 | release: |
f627c2f5 | 2868 | mem_cgroup_cancel_charge(page, memcg, false); |
09cbfeaf | 2869 | put_page(page); |
8f4e2101 | 2870 | goto unlock; |
8a9f3ccd | 2871 | oom_free_page: |
09cbfeaf | 2872 | put_page(page); |
65500d23 | 2873 | oom: |
1da177e4 LT |
2874 | return VM_FAULT_OOM; |
2875 | } | |
2876 | ||
9a95f3cf PC |
2877 | /* |
2878 | * The mmap_sem must have been held on entry, and may have been | |
2879 | * released depending on flags and vma->vm_ops->fault() return value. | |
2880 | * See filemap_fault() and __lock_page_retry(). | |
2881 | */ | |
936ca80d | 2882 | static int __do_fault(struct vm_fault *vmf) |
7eae74af | 2883 | { |
82b0f8c3 | 2884 | struct vm_area_struct *vma = vmf->vma; |
7eae74af KS |
2885 | int ret; |
2886 | ||
11bac800 | 2887 | ret = vma->vm_ops->fault(vmf); |
3917048d | 2888 | if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY | |
b1aa812b | 2889 | VM_FAULT_DONE_COW))) |
bc2466e4 | 2890 | return ret; |
7eae74af | 2891 | |
667240e0 | 2892 | if (unlikely(PageHWPoison(vmf->page))) { |
7eae74af | 2893 | if (ret & VM_FAULT_LOCKED) |
667240e0 JK |
2894 | unlock_page(vmf->page); |
2895 | put_page(vmf->page); | |
936ca80d | 2896 | vmf->page = NULL; |
7eae74af KS |
2897 | return VM_FAULT_HWPOISON; |
2898 | } | |
2899 | ||
2900 | if (unlikely(!(ret & VM_FAULT_LOCKED))) | |
667240e0 | 2901 | lock_page(vmf->page); |
7eae74af | 2902 | else |
667240e0 | 2903 | VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page); |
7eae74af | 2904 | |
7eae74af KS |
2905 | return ret; |
2906 | } | |
2907 | ||
82b0f8c3 | 2908 | static int pte_alloc_one_map(struct vm_fault *vmf) |
7267ec00 | 2909 | { |
82b0f8c3 | 2910 | struct vm_area_struct *vma = vmf->vma; |
7267ec00 | 2911 | |
82b0f8c3 | 2912 | if (!pmd_none(*vmf->pmd)) |
7267ec00 | 2913 | goto map_pte; |
82b0f8c3 JK |
2914 | if (vmf->prealloc_pte) { |
2915 | vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); | |
2916 | if (unlikely(!pmd_none(*vmf->pmd))) { | |
2917 | spin_unlock(vmf->ptl); | |
7267ec00 KS |
2918 | goto map_pte; |
2919 | } | |
2920 | ||
2921 | atomic_long_inc(&vma->vm_mm->nr_ptes); | |
82b0f8c3 JK |
2922 | pmd_populate(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); |
2923 | spin_unlock(vmf->ptl); | |
7f2b6ce8 | 2924 | vmf->prealloc_pte = NULL; |
82b0f8c3 | 2925 | } else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd, vmf->address))) { |
7267ec00 KS |
2926 | return VM_FAULT_OOM; |
2927 | } | |
2928 | map_pte: | |
2929 | /* | |
2930 | * If a huge pmd materialized under us just retry later. Use | |
2931 | * pmd_trans_unstable() instead of pmd_trans_huge() to ensure the pmd | |
2932 | * didn't become pmd_trans_huge under us and then back to pmd_none, as | |
2933 | * a result of MADV_DONTNEED running immediately after a huge pmd fault | |
2934 | * in a different thread of this mm, in turn leading to a misleading | |
2935 | * pmd_trans_huge() retval. All we have to ensure is that it is a | |
2936 | * regular pmd that we can walk with pte_offset_map() and we can do that | |
2937 | * through an atomic read in C, which is what pmd_trans_unstable() | |
2938 | * provides. | |
2939 | */ | |
82b0f8c3 | 2940 | if (pmd_trans_unstable(vmf->pmd) || pmd_devmap(*vmf->pmd)) |
7267ec00 KS |
2941 | return VM_FAULT_NOPAGE; |
2942 | ||
82b0f8c3 JK |
2943 | vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, |
2944 | &vmf->ptl); | |
7267ec00 KS |
2945 | return 0; |
2946 | } | |
2947 | ||
e496cf3d | 2948 | #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE |
10102459 KS |
2949 | |
2950 | #define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1) | |
2951 | static inline bool transhuge_vma_suitable(struct vm_area_struct *vma, | |
2952 | unsigned long haddr) | |
2953 | { | |
2954 | if (((vma->vm_start >> PAGE_SHIFT) & HPAGE_CACHE_INDEX_MASK) != | |
2955 | (vma->vm_pgoff & HPAGE_CACHE_INDEX_MASK)) | |
2956 | return false; | |
2957 | if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end) | |
2958 | return false; | |
2959 | return true; | |
2960 | } | |
2961 | ||
82b0f8c3 | 2962 | static void deposit_prealloc_pte(struct vm_fault *vmf) |
953c66c2 | 2963 | { |
82b0f8c3 | 2964 | struct vm_area_struct *vma = vmf->vma; |
953c66c2 | 2965 | |
82b0f8c3 | 2966 | pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); |
953c66c2 AK |
2967 | /* |
2968 | * We are going to consume the prealloc table, | |
2969 | * count that as nr_ptes. | |
2970 | */ | |
2971 | atomic_long_inc(&vma->vm_mm->nr_ptes); | |
7f2b6ce8 | 2972 | vmf->prealloc_pte = NULL; |
953c66c2 AK |
2973 | } |
2974 | ||
82b0f8c3 | 2975 | static int do_set_pmd(struct vm_fault *vmf, struct page *page) |
10102459 | 2976 | { |
82b0f8c3 JK |
2977 | struct vm_area_struct *vma = vmf->vma; |
2978 | bool write = vmf->flags & FAULT_FLAG_WRITE; | |
2979 | unsigned long haddr = vmf->address & HPAGE_PMD_MASK; | |
10102459 KS |
2980 | pmd_t entry; |
2981 | int i, ret; | |
2982 | ||
2983 | if (!transhuge_vma_suitable(vma, haddr)) | |
2984 | return VM_FAULT_FALLBACK; | |
2985 | ||
2986 | ret = VM_FAULT_FALLBACK; | |
2987 | page = compound_head(page); | |
2988 | ||
953c66c2 AK |
2989 | /* |
2990 | * Archs like ppc64 need additonal space to store information | |
2991 | * related to pte entry. Use the preallocated table for that. | |
2992 | */ | |
82b0f8c3 JK |
2993 | if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) { |
2994 | vmf->prealloc_pte = pte_alloc_one(vma->vm_mm, vmf->address); | |
2995 | if (!vmf->prealloc_pte) | |
953c66c2 AK |
2996 | return VM_FAULT_OOM; |
2997 | smp_wmb(); /* See comment in __pte_alloc() */ | |
2998 | } | |
2999 | ||
82b0f8c3 JK |
3000 | vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); |
3001 | if (unlikely(!pmd_none(*vmf->pmd))) | |
10102459 KS |
3002 | goto out; |
3003 | ||
3004 | for (i = 0; i < HPAGE_PMD_NR; i++) | |
3005 | flush_icache_page(vma, page + i); | |
3006 | ||
3007 | entry = mk_huge_pmd(page, vma->vm_page_prot); | |
3008 | if (write) | |
3009 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); | |
3010 | ||
3011 | add_mm_counter(vma->vm_mm, MM_FILEPAGES, HPAGE_PMD_NR); | |
3012 | page_add_file_rmap(page, true); | |
953c66c2 AK |
3013 | /* |
3014 | * deposit and withdraw with pmd lock held | |
3015 | */ | |
3016 | if (arch_needs_pgtable_deposit()) | |
82b0f8c3 | 3017 | deposit_prealloc_pte(vmf); |
10102459 | 3018 | |
82b0f8c3 | 3019 | set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry); |
10102459 | 3020 | |
82b0f8c3 | 3021 | update_mmu_cache_pmd(vma, haddr, vmf->pmd); |
10102459 KS |
3022 | |
3023 | /* fault is handled */ | |
3024 | ret = 0; | |
95ecedcd | 3025 | count_vm_event(THP_FILE_MAPPED); |
10102459 | 3026 | out: |
82b0f8c3 | 3027 | spin_unlock(vmf->ptl); |
10102459 KS |
3028 | return ret; |
3029 | } | |
3030 | #else | |
82b0f8c3 | 3031 | static int do_set_pmd(struct vm_fault *vmf, struct page *page) |
10102459 KS |
3032 | { |
3033 | BUILD_BUG(); | |
3034 | return 0; | |
3035 | } | |
3036 | #endif | |
3037 | ||
8c6e50b0 | 3038 | /** |
7267ec00 KS |
3039 | * alloc_set_pte - setup new PTE entry for given page and add reverse page |
3040 | * mapping. If needed, the fucntion allocates page table or use pre-allocated. | |
8c6e50b0 | 3041 | * |
82b0f8c3 | 3042 | * @vmf: fault environment |
7267ec00 | 3043 | * @memcg: memcg to charge page (only for private mappings) |
8c6e50b0 | 3044 | * @page: page to map |
8c6e50b0 | 3045 | * |
82b0f8c3 JK |
3046 | * Caller must take care of unlocking vmf->ptl, if vmf->pte is non-NULL on |
3047 | * return. | |
8c6e50b0 KS |
3048 | * |
3049 | * Target users are page handler itself and implementations of | |
3050 | * vm_ops->map_pages. | |
3051 | */ | |
82b0f8c3 | 3052 | int alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg, |
7267ec00 | 3053 | struct page *page) |
3bb97794 | 3054 | { |
82b0f8c3 JK |
3055 | struct vm_area_struct *vma = vmf->vma; |
3056 | bool write = vmf->flags & FAULT_FLAG_WRITE; | |
3bb97794 | 3057 | pte_t entry; |
10102459 KS |
3058 | int ret; |
3059 | ||
82b0f8c3 | 3060 | if (pmd_none(*vmf->pmd) && PageTransCompound(page) && |
e496cf3d | 3061 | IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) { |
10102459 KS |
3062 | /* THP on COW? */ |
3063 | VM_BUG_ON_PAGE(memcg, page); | |
3064 | ||
82b0f8c3 | 3065 | ret = do_set_pmd(vmf, page); |
10102459 | 3066 | if (ret != VM_FAULT_FALLBACK) |
b0b9b3df | 3067 | return ret; |
10102459 | 3068 | } |
3bb97794 | 3069 | |
82b0f8c3 JK |
3070 | if (!vmf->pte) { |
3071 | ret = pte_alloc_one_map(vmf); | |
7267ec00 | 3072 | if (ret) |
b0b9b3df | 3073 | return ret; |
7267ec00 KS |
3074 | } |
3075 | ||
3076 | /* Re-check under ptl */ | |
b0b9b3df HD |
3077 | if (unlikely(!pte_none(*vmf->pte))) |
3078 | return VM_FAULT_NOPAGE; | |
7267ec00 | 3079 | |
3bb97794 KS |
3080 | flush_icache_page(vma, page); |
3081 | entry = mk_pte(page, vma->vm_page_prot); | |
3082 | if (write) | |
3083 | entry = maybe_mkwrite(pte_mkdirty(entry), vma); | |
bae473a4 KS |
3084 | /* copy-on-write page */ |
3085 | if (write && !(vma->vm_flags & VM_SHARED)) { | |
3bb97794 | 3086 | inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES); |
82b0f8c3 | 3087 | page_add_new_anon_rmap(page, vma, vmf->address, false); |
7267ec00 KS |
3088 | mem_cgroup_commit_charge(page, memcg, false, false); |
3089 | lru_cache_add_active_or_unevictable(page, vma); | |
3bb97794 | 3090 | } else { |
eca56ff9 | 3091 | inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page)); |
dd78fedd | 3092 | page_add_file_rmap(page, false); |
3bb97794 | 3093 | } |
82b0f8c3 | 3094 | set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); |
3bb97794 KS |
3095 | |
3096 | /* no need to invalidate: a not-present page won't be cached */ | |
82b0f8c3 | 3097 | update_mmu_cache(vma, vmf->address, vmf->pte); |
7267ec00 | 3098 | |
b0b9b3df | 3099 | return 0; |
3bb97794 KS |
3100 | } |
3101 | ||
9118c0cb JK |
3102 | |
3103 | /** | |
3104 | * finish_fault - finish page fault once we have prepared the page to fault | |
3105 | * | |
3106 | * @vmf: structure describing the fault | |
3107 | * | |
3108 | * This function handles all that is needed to finish a page fault once the | |
3109 | * page to fault in is prepared. It handles locking of PTEs, inserts PTE for | |
3110 | * given page, adds reverse page mapping, handles memcg charges and LRU | |
3111 | * addition. The function returns 0 on success, VM_FAULT_ code in case of | |
3112 | * error. | |
3113 | * | |
3114 | * The function expects the page to be locked and on success it consumes a | |
3115 | * reference of a page being mapped (for the PTE which maps it). | |
3116 | */ | |
3117 | int finish_fault(struct vm_fault *vmf) | |
3118 | { | |
3119 | struct page *page; | |
3120 | int ret; | |
3121 | ||
3122 | /* Did we COW the page? */ | |
3123 | if ((vmf->flags & FAULT_FLAG_WRITE) && | |
3124 | !(vmf->vma->vm_flags & VM_SHARED)) | |
3125 | page = vmf->cow_page; | |
3126 | else | |
3127 | page = vmf->page; | |
3128 | ret = alloc_set_pte(vmf, vmf->memcg, page); | |
3129 | if (vmf->pte) | |
3130 | pte_unmap_unlock(vmf->pte, vmf->ptl); | |
3131 | return ret; | |
3132 | } | |
3133 | ||
3a91053a KS |
3134 | static unsigned long fault_around_bytes __read_mostly = |
3135 | rounddown_pow_of_two(65536); | |
a9b0f861 | 3136 | |
a9b0f861 KS |
3137 | #ifdef CONFIG_DEBUG_FS |
3138 | static int fault_around_bytes_get(void *data, u64 *val) | |
1592eef0 | 3139 | { |
a9b0f861 | 3140 | *val = fault_around_bytes; |
1592eef0 KS |
3141 | return 0; |
3142 | } | |
3143 | ||
b4903d6e AR |
3144 | /* |
3145 | * fault_around_pages() and fault_around_mask() expects fault_around_bytes | |
3146 | * rounded down to nearest page order. It's what do_fault_around() expects to | |
3147 | * see. | |
3148 | */ | |
a9b0f861 | 3149 | static int fault_around_bytes_set(void *data, u64 val) |
1592eef0 | 3150 | { |
a9b0f861 | 3151 | if (val / PAGE_SIZE > PTRS_PER_PTE) |
1592eef0 | 3152 | return -EINVAL; |
b4903d6e AR |
3153 | if (val > PAGE_SIZE) |
3154 | fault_around_bytes = rounddown_pow_of_two(val); | |
3155 | else | |
3156 | fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */ | |
1592eef0 KS |
3157 | return 0; |
3158 | } | |
a9b0f861 KS |
3159 | DEFINE_SIMPLE_ATTRIBUTE(fault_around_bytes_fops, |
3160 | fault_around_bytes_get, fault_around_bytes_set, "%llu\n"); | |
1592eef0 KS |
3161 | |
3162 | static int __init fault_around_debugfs(void) | |
3163 | { | |
3164 | void *ret; | |
3165 | ||
a9b0f861 KS |
3166 | ret = debugfs_create_file("fault_around_bytes", 0644, NULL, NULL, |
3167 | &fault_around_bytes_fops); | |
1592eef0 | 3168 | if (!ret) |
a9b0f861 | 3169 | pr_warn("Failed to create fault_around_bytes in debugfs"); |
1592eef0 KS |
3170 | return 0; |
3171 | } | |
3172 | late_initcall(fault_around_debugfs); | |
1592eef0 | 3173 | #endif |
8c6e50b0 | 3174 | |
1fdb412b KS |
3175 | /* |
3176 | * do_fault_around() tries to map few pages around the fault address. The hope | |
3177 | * is that the pages will be needed soon and this will lower the number of | |
3178 | * faults to handle. | |
3179 | * | |
3180 | * It uses vm_ops->map_pages() to map the pages, which skips the page if it's | |
3181 | * not ready to be mapped: not up-to-date, locked, etc. | |
3182 | * | |
3183 | * This function is called with the page table lock taken. In the split ptlock | |
3184 | * case the page table lock only protects only those entries which belong to | |
3185 | * the page table corresponding to the fault address. | |
3186 | * | |
3187 | * This function doesn't cross the VMA boundaries, in order to call map_pages() | |
3188 | * only once. | |
3189 | * | |
3190 | * fault_around_pages() defines how many pages we'll try to map. | |
3191 | * do_fault_around() expects it to return a power of two less than or equal to | |
3192 | * PTRS_PER_PTE. | |
3193 | * | |
3194 | * The virtual address of the area that we map is naturally aligned to the | |
3195 | * fault_around_pages() value (and therefore to page order). This way it's | |
3196 | * easier to guarantee that we don't cross page table boundaries. | |
3197 | */ | |
0721ec8b | 3198 | static int do_fault_around(struct vm_fault *vmf) |
8c6e50b0 | 3199 | { |
82b0f8c3 | 3200 | unsigned long address = vmf->address, nr_pages, mask; |
0721ec8b | 3201 | pgoff_t start_pgoff = vmf->pgoff; |
bae473a4 | 3202 | pgoff_t end_pgoff; |
7267ec00 | 3203 | int off, ret = 0; |
8c6e50b0 | 3204 | |
4db0c3c2 | 3205 | nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT; |
aecd6f44 KS |
3206 | mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK; |
3207 | ||
82b0f8c3 JK |
3208 | vmf->address = max(address & mask, vmf->vma->vm_start); |
3209 | off = ((address - vmf->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1); | |
bae473a4 | 3210 | start_pgoff -= off; |
8c6e50b0 KS |
3211 | |
3212 | /* | |
bae473a4 KS |
3213 | * end_pgoff is either end of page table or end of vma |
3214 | * or fault_around_pages() from start_pgoff, depending what is nearest. | |
8c6e50b0 | 3215 | */ |
bae473a4 | 3216 | end_pgoff = start_pgoff - |
82b0f8c3 | 3217 | ((vmf->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) + |
8c6e50b0 | 3218 | PTRS_PER_PTE - 1; |
82b0f8c3 | 3219 | end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1, |
bae473a4 | 3220 | start_pgoff + nr_pages - 1); |
8c6e50b0 | 3221 | |
82b0f8c3 JK |
3222 | if (pmd_none(*vmf->pmd)) { |
3223 | vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm, | |
3224 | vmf->address); | |
3225 | if (!vmf->prealloc_pte) | |
c5f88bd2 | 3226 | goto out; |
7267ec00 | 3227 | smp_wmb(); /* See comment in __pte_alloc() */ |
8c6e50b0 KS |
3228 | } |
3229 | ||
82b0f8c3 | 3230 | vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff); |
7267ec00 | 3231 | |
7267ec00 | 3232 | /* Huge page is mapped? Page fault is solved */ |
82b0f8c3 | 3233 | if (pmd_trans_huge(*vmf->pmd)) { |
7267ec00 KS |
3234 | ret = VM_FAULT_NOPAGE; |
3235 | goto out; | |
3236 | } | |
3237 | ||
3238 | /* ->map_pages() haven't done anything useful. Cold page cache? */ | |
82b0f8c3 | 3239 | if (!vmf->pte) |
7267ec00 KS |
3240 | goto out; |
3241 | ||
3242 | /* check if the page fault is solved */ | |
82b0f8c3 JK |
3243 | vmf->pte -= (vmf->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT); |
3244 | if (!pte_none(*vmf->pte)) | |
7267ec00 | 3245 | ret = VM_FAULT_NOPAGE; |
82b0f8c3 | 3246 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
bae473a4 | 3247 | out: |
82b0f8c3 JK |
3248 | vmf->address = address; |
3249 | vmf->pte = NULL; | |
7267ec00 | 3250 | return ret; |
8c6e50b0 KS |
3251 | } |
3252 | ||
0721ec8b | 3253 | static int do_read_fault(struct vm_fault *vmf) |
e655fb29 | 3254 | { |
82b0f8c3 | 3255 | struct vm_area_struct *vma = vmf->vma; |
8c6e50b0 KS |
3256 | int ret = 0; |
3257 | ||
3258 | /* | |
3259 | * Let's call ->map_pages() first and use ->fault() as fallback | |
3260 | * if page by the offset is not ready to be mapped (cold cache or | |
3261 | * something). | |
3262 | */ | |
9b4bdd2f | 3263 | if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) { |
0721ec8b | 3264 | ret = do_fault_around(vmf); |
7267ec00 KS |
3265 | if (ret) |
3266 | return ret; | |
8c6e50b0 | 3267 | } |
e655fb29 | 3268 | |
936ca80d | 3269 | ret = __do_fault(vmf); |
e655fb29 KS |
3270 | if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) |
3271 | return ret; | |
3272 | ||
9118c0cb | 3273 | ret |= finish_fault(vmf); |
936ca80d | 3274 | unlock_page(vmf->page); |
7267ec00 | 3275 | if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) |
936ca80d | 3276 | put_page(vmf->page); |
e655fb29 KS |
3277 | return ret; |
3278 | } | |
3279 | ||
0721ec8b | 3280 | static int do_cow_fault(struct vm_fault *vmf) |
ec47c3b9 | 3281 | { |
82b0f8c3 | 3282 | struct vm_area_struct *vma = vmf->vma; |
ec47c3b9 KS |
3283 | int ret; |
3284 | ||
3285 | if (unlikely(anon_vma_prepare(vma))) | |
3286 | return VM_FAULT_OOM; | |
3287 | ||
936ca80d JK |
3288 | vmf->cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address); |
3289 | if (!vmf->cow_page) | |
ec47c3b9 KS |
3290 | return VM_FAULT_OOM; |
3291 | ||
936ca80d | 3292 | if (mem_cgroup_try_charge(vmf->cow_page, vma->vm_mm, GFP_KERNEL, |
3917048d | 3293 | &vmf->memcg, false)) { |
936ca80d | 3294 | put_page(vmf->cow_page); |
ec47c3b9 KS |
3295 | return VM_FAULT_OOM; |
3296 | } | |
3297 | ||
936ca80d | 3298 | ret = __do_fault(vmf); |
ec47c3b9 KS |
3299 | if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) |
3300 | goto uncharge_out; | |
3917048d JK |
3301 | if (ret & VM_FAULT_DONE_COW) |
3302 | return ret; | |
ec47c3b9 | 3303 | |
b1aa812b | 3304 | copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma); |
936ca80d | 3305 | __SetPageUptodate(vmf->cow_page); |
ec47c3b9 | 3306 | |
9118c0cb | 3307 | ret |= finish_fault(vmf); |
b1aa812b JK |
3308 | unlock_page(vmf->page); |
3309 | put_page(vmf->page); | |
7267ec00 KS |
3310 | if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) |
3311 | goto uncharge_out; | |
ec47c3b9 KS |
3312 | return ret; |
3313 | uncharge_out: | |
3917048d | 3314 | mem_cgroup_cancel_charge(vmf->cow_page, vmf->memcg, false); |
936ca80d | 3315 | put_page(vmf->cow_page); |
ec47c3b9 KS |
3316 | return ret; |
3317 | } | |
3318 | ||
0721ec8b | 3319 | static int do_shared_fault(struct vm_fault *vmf) |
1da177e4 | 3320 | { |
82b0f8c3 | 3321 | struct vm_area_struct *vma = vmf->vma; |
f0c6d4d2 | 3322 | int ret, tmp; |
1d65f86d | 3323 | |
936ca80d | 3324 | ret = __do_fault(vmf); |
7eae74af | 3325 | if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) |
f0c6d4d2 | 3326 | return ret; |
1da177e4 LT |
3327 | |
3328 | /* | |
f0c6d4d2 KS |
3329 | * Check if the backing address space wants to know that the page is |
3330 | * about to become writable | |
1da177e4 | 3331 | */ |
fb09a464 | 3332 | if (vma->vm_ops->page_mkwrite) { |
936ca80d | 3333 | unlock_page(vmf->page); |
38b8cb7f | 3334 | tmp = do_page_mkwrite(vmf); |
fb09a464 KS |
3335 | if (unlikely(!tmp || |
3336 | (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) { | |
936ca80d | 3337 | put_page(vmf->page); |
fb09a464 | 3338 | return tmp; |
4294621f | 3339 | } |
fb09a464 KS |
3340 | } |
3341 | ||
9118c0cb | 3342 | ret |= finish_fault(vmf); |
7267ec00 KS |
3343 | if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | |
3344 | VM_FAULT_RETRY))) { | |
936ca80d JK |
3345 | unlock_page(vmf->page); |
3346 | put_page(vmf->page); | |
f0c6d4d2 | 3347 | return ret; |
1da177e4 | 3348 | } |
b827e496 | 3349 | |
97ba0c2b | 3350 | fault_dirty_shared_page(vma, vmf->page); |
1d65f86d | 3351 | return ret; |
54cb8821 | 3352 | } |
d00806b1 | 3353 | |
9a95f3cf PC |
3354 | /* |
3355 | * We enter with non-exclusive mmap_sem (to exclude vma changes, | |
3356 | * but allow concurrent faults). | |
3357 | * The mmap_sem may have been released depending on flags and our | |
3358 | * return value. See filemap_fault() and __lock_page_or_retry(). | |
3359 | */ | |
82b0f8c3 | 3360 | static int do_fault(struct vm_fault *vmf) |
54cb8821 | 3361 | { |
82b0f8c3 | 3362 | struct vm_area_struct *vma = vmf->vma; |
b0b9b3df | 3363 | int ret; |
54cb8821 | 3364 | |
6b7339f4 KS |
3365 | /* The VMA was not fully populated on mmap() or missing VM_DONTEXPAND */ |
3366 | if (!vma->vm_ops->fault) | |
b0b9b3df HD |
3367 | ret = VM_FAULT_SIGBUS; |
3368 | else if (!(vmf->flags & FAULT_FLAG_WRITE)) | |
3369 | ret = do_read_fault(vmf); | |
3370 | else if (!(vma->vm_flags & VM_SHARED)) | |
3371 | ret = do_cow_fault(vmf); | |
3372 | else | |
3373 | ret = do_shared_fault(vmf); | |
3374 | ||
3375 | /* preallocated pagetable is unused: free it */ | |
3376 | if (vmf->prealloc_pte) { | |
3377 | pte_free(vma->vm_mm, vmf->prealloc_pte); | |
7f2b6ce8 | 3378 | vmf->prealloc_pte = NULL; |
b0b9b3df HD |
3379 | } |
3380 | return ret; | |
54cb8821 NP |
3381 | } |
3382 | ||
b19a9939 | 3383 | static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, |
04bb2f94 RR |
3384 | unsigned long addr, int page_nid, |
3385 | int *flags) | |
9532fec1 MG |
3386 | { |
3387 | get_page(page); | |
3388 | ||
3389 | count_vm_numa_event(NUMA_HINT_FAULTS); | |
04bb2f94 | 3390 | if (page_nid == numa_node_id()) { |
9532fec1 | 3391 | count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); |
04bb2f94 RR |
3392 | *flags |= TNF_FAULT_LOCAL; |
3393 | } | |
9532fec1 MG |
3394 | |
3395 | return mpol_misplaced(page, vma, addr); | |
3396 | } | |
3397 | ||
2994302b | 3398 | static int do_numa_page(struct vm_fault *vmf) |
d10e63f2 | 3399 | { |
82b0f8c3 | 3400 | struct vm_area_struct *vma = vmf->vma; |
4daae3b4 | 3401 | struct page *page = NULL; |
8191acbd | 3402 | int page_nid = -1; |
90572890 | 3403 | int last_cpupid; |
cbee9f88 | 3404 | int target_nid; |
b8593bfd | 3405 | bool migrated = false; |
cee216a6 | 3406 | pte_t pte; |
288bc549 | 3407 | bool was_writable = pte_savedwrite(vmf->orig_pte); |
6688cc05 | 3408 | int flags = 0; |
d10e63f2 MG |
3409 | |
3410 | /* | |
166f61b9 TH |
3411 | * The "pte" at this point cannot be used safely without |
3412 | * validation through pte_unmap_same(). It's of NUMA type but | |
3413 | * the pfn may be screwed if the read is non atomic. | |
166f61b9 | 3414 | */ |
82b0f8c3 JK |
3415 | vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd); |
3416 | spin_lock(vmf->ptl); | |
cee216a6 | 3417 | if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) { |
82b0f8c3 | 3418 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
4daae3b4 MG |
3419 | goto out; |
3420 | } | |
3421 | ||
cee216a6 AK |
3422 | /* |
3423 | * Make it present again, Depending on how arch implementes non | |
3424 | * accessible ptes, some can allow access by kernel mode. | |
3425 | */ | |
3426 | pte = ptep_modify_prot_start(vma->vm_mm, vmf->address, vmf->pte); | |
4d942466 MG |
3427 | pte = pte_modify(pte, vma->vm_page_prot); |
3428 | pte = pte_mkyoung(pte); | |
b191f9b1 MG |
3429 | if (was_writable) |
3430 | pte = pte_mkwrite(pte); | |
cee216a6 | 3431 | ptep_modify_prot_commit(vma->vm_mm, vmf->address, vmf->pte, pte); |
82b0f8c3 | 3432 | update_mmu_cache(vma, vmf->address, vmf->pte); |
d10e63f2 | 3433 | |
82b0f8c3 | 3434 | page = vm_normal_page(vma, vmf->address, pte); |
d10e63f2 | 3435 | if (!page) { |
82b0f8c3 | 3436 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
d10e63f2 MG |
3437 | return 0; |
3438 | } | |
3439 | ||
e81c4802 KS |
3440 | /* TODO: handle PTE-mapped THP */ |
3441 | if (PageCompound(page)) { | |
82b0f8c3 | 3442 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
e81c4802 KS |
3443 | return 0; |
3444 | } | |
3445 | ||
6688cc05 | 3446 | /* |
bea66fbd MG |
3447 | * Avoid grouping on RO pages in general. RO pages shouldn't hurt as |
3448 | * much anyway since they can be in shared cache state. This misses | |
3449 | * the case where a mapping is writable but the process never writes | |
3450 | * to it but pte_write gets cleared during protection updates and | |
3451 | * pte_dirty has unpredictable behaviour between PTE scan updates, | |
3452 | * background writeback, dirty balancing and application behaviour. | |
6688cc05 | 3453 | */ |
d59dc7bc | 3454 | if (!pte_write(pte)) |
6688cc05 PZ |
3455 | flags |= TNF_NO_GROUP; |
3456 | ||
dabe1d99 RR |
3457 | /* |
3458 | * Flag if the page is shared between multiple address spaces. This | |
3459 | * is later used when determining whether to group tasks together | |
3460 | */ | |
3461 | if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED)) | |
3462 | flags |= TNF_SHARED; | |
3463 | ||
90572890 | 3464 | last_cpupid = page_cpupid_last(page); |
8191acbd | 3465 | page_nid = page_to_nid(page); |
82b0f8c3 | 3466 | target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid, |
bae473a4 | 3467 | &flags); |
82b0f8c3 | 3468 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
4daae3b4 | 3469 | if (target_nid == -1) { |
4daae3b4 MG |
3470 | put_page(page); |
3471 | goto out; | |
3472 | } | |
3473 | ||
3474 | /* Migrate to the requested node */ | |
1bc115d8 | 3475 | migrated = migrate_misplaced_page(page, vma, target_nid); |
6688cc05 | 3476 | if (migrated) { |
8191acbd | 3477 | page_nid = target_nid; |
6688cc05 | 3478 | flags |= TNF_MIGRATED; |
074c2381 MG |
3479 | } else |
3480 | flags |= TNF_MIGRATE_FAIL; | |
4daae3b4 MG |
3481 | |
3482 | out: | |
8191acbd | 3483 | if (page_nid != -1) |
6688cc05 | 3484 | task_numa_fault(last_cpupid, page_nid, 1, flags); |
d10e63f2 MG |
3485 | return 0; |
3486 | } | |
3487 | ||
82b0f8c3 | 3488 | static int create_huge_pmd(struct vm_fault *vmf) |
b96375f7 | 3489 | { |
f4200391 | 3490 | if (vma_is_anonymous(vmf->vma)) |
82b0f8c3 | 3491 | return do_huge_pmd_anonymous_page(vmf); |
a2d58167 | 3492 | if (vmf->vma->vm_ops->huge_fault) |
c791ace1 | 3493 | return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); |
b96375f7 MW |
3494 | return VM_FAULT_FALLBACK; |
3495 | } | |
3496 | ||
82b0f8c3 | 3497 | static int wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd) |
b96375f7 | 3498 | { |
82b0f8c3 JK |
3499 | if (vma_is_anonymous(vmf->vma)) |
3500 | return do_huge_pmd_wp_page(vmf, orig_pmd); | |
a2d58167 | 3501 | if (vmf->vma->vm_ops->huge_fault) |
c791ace1 | 3502 | return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD); |
af9e4d5f KS |
3503 | |
3504 | /* COW handled on pte level: split pmd */ | |
82b0f8c3 JK |
3505 | VM_BUG_ON_VMA(vmf->vma->vm_flags & VM_SHARED, vmf->vma); |
3506 | __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL); | |
af9e4d5f | 3507 | |
b96375f7 MW |
3508 | return VM_FAULT_FALLBACK; |
3509 | } | |
3510 | ||
38e08854 LS |
3511 | static inline bool vma_is_accessible(struct vm_area_struct *vma) |
3512 | { | |
3513 | return vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE); | |
3514 | } | |
3515 | ||
a00cc7d9 MW |
3516 | static int create_huge_pud(struct vm_fault *vmf) |
3517 | { | |
3518 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE | |
3519 | /* No support for anonymous transparent PUD pages yet */ | |
3520 | if (vma_is_anonymous(vmf->vma)) | |
3521 | return VM_FAULT_FALLBACK; | |
3522 | if (vmf->vma->vm_ops->huge_fault) | |
c791ace1 | 3523 | return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); |
a00cc7d9 MW |
3524 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
3525 | return VM_FAULT_FALLBACK; | |
3526 | } | |
3527 | ||
3528 | static int wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud) | |
3529 | { | |
3530 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE | |
3531 | /* No support for anonymous transparent PUD pages yet */ | |
3532 | if (vma_is_anonymous(vmf->vma)) | |
3533 | return VM_FAULT_FALLBACK; | |
3534 | if (vmf->vma->vm_ops->huge_fault) | |
c791ace1 | 3535 | return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD); |
a00cc7d9 MW |
3536 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
3537 | return VM_FAULT_FALLBACK; | |
3538 | } | |
3539 | ||
1da177e4 LT |
3540 | /* |
3541 | * These routines also need to handle stuff like marking pages dirty | |
3542 | * and/or accessed for architectures that don't do it in hardware (most | |
3543 | * RISC architectures). The early dirtying is also good on the i386. | |
3544 | * | |
3545 | * There is also a hook called "update_mmu_cache()" that architectures | |
3546 | * with external mmu caches can use to update those (ie the Sparc or | |
3547 | * PowerPC hashed page tables that act as extended TLBs). | |
3548 | * | |
7267ec00 KS |
3549 | * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow |
3550 | * concurrent faults). | |
9a95f3cf | 3551 | * |
7267ec00 KS |
3552 | * The mmap_sem may have been released depending on flags and our return value. |
3553 | * See filemap_fault() and __lock_page_or_retry(). | |
1da177e4 | 3554 | */ |
82b0f8c3 | 3555 | static int handle_pte_fault(struct vm_fault *vmf) |
1da177e4 LT |
3556 | { |
3557 | pte_t entry; | |
3558 | ||
82b0f8c3 | 3559 | if (unlikely(pmd_none(*vmf->pmd))) { |
7267ec00 KS |
3560 | /* |
3561 | * Leave __pte_alloc() until later: because vm_ops->fault may | |
3562 | * want to allocate huge page, and if we expose page table | |
3563 | * for an instant, it will be difficult to retract from | |
3564 | * concurrent faults and from rmap lookups. | |
3565 | */ | |
82b0f8c3 | 3566 | vmf->pte = NULL; |
7267ec00 KS |
3567 | } else { |
3568 | /* See comment in pte_alloc_one_map() */ | |
82b0f8c3 | 3569 | if (pmd_trans_unstable(vmf->pmd) || pmd_devmap(*vmf->pmd)) |
7267ec00 KS |
3570 | return 0; |
3571 | /* | |
3572 | * A regular pmd is established and it can't morph into a huge | |
3573 | * pmd from under us anymore at this point because we hold the | |
3574 | * mmap_sem read mode and khugepaged takes it in write mode. | |
3575 | * So now it's safe to run pte_offset_map(). | |
3576 | */ | |
82b0f8c3 | 3577 | vmf->pte = pte_offset_map(vmf->pmd, vmf->address); |
2994302b | 3578 | vmf->orig_pte = *vmf->pte; |
7267ec00 KS |
3579 | |
3580 | /* | |
3581 | * some architectures can have larger ptes than wordsize, | |
3582 | * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and | |
3583 | * CONFIG_32BIT=y, so READ_ONCE or ACCESS_ONCE cannot guarantee | |
3584 | * atomic accesses. The code below just needs a consistent | |
3585 | * view for the ifs and we later double check anyway with the | |
3586 | * ptl lock held. So here a barrier will do. | |
3587 | */ | |
3588 | barrier(); | |
2994302b | 3589 | if (pte_none(vmf->orig_pte)) { |
82b0f8c3 JK |
3590 | pte_unmap(vmf->pte); |
3591 | vmf->pte = NULL; | |
65500d23 | 3592 | } |
1da177e4 LT |
3593 | } |
3594 | ||
82b0f8c3 JK |
3595 | if (!vmf->pte) { |
3596 | if (vma_is_anonymous(vmf->vma)) | |
3597 | return do_anonymous_page(vmf); | |
7267ec00 | 3598 | else |
82b0f8c3 | 3599 | return do_fault(vmf); |
7267ec00 KS |
3600 | } |
3601 | ||
2994302b JK |
3602 | if (!pte_present(vmf->orig_pte)) |
3603 | return do_swap_page(vmf); | |
7267ec00 | 3604 | |
2994302b JK |
3605 | if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma)) |
3606 | return do_numa_page(vmf); | |
d10e63f2 | 3607 | |
82b0f8c3 JK |
3608 | vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd); |
3609 | spin_lock(vmf->ptl); | |
2994302b | 3610 | entry = vmf->orig_pte; |
82b0f8c3 | 3611 | if (unlikely(!pte_same(*vmf->pte, entry))) |
8f4e2101 | 3612 | goto unlock; |
82b0f8c3 | 3613 | if (vmf->flags & FAULT_FLAG_WRITE) { |
1da177e4 | 3614 | if (!pte_write(entry)) |
2994302b | 3615 | return do_wp_page(vmf); |
1da177e4 LT |
3616 | entry = pte_mkdirty(entry); |
3617 | } | |
3618 | entry = pte_mkyoung(entry); | |
82b0f8c3 JK |
3619 | if (ptep_set_access_flags(vmf->vma, vmf->address, vmf->pte, entry, |
3620 | vmf->flags & FAULT_FLAG_WRITE)) { | |
3621 | update_mmu_cache(vmf->vma, vmf->address, vmf->pte); | |
1a44e149 AA |
3622 | } else { |
3623 | /* | |
3624 | * This is needed only for protection faults but the arch code | |
3625 | * is not yet telling us if this is a protection fault or not. | |
3626 | * This still avoids useless tlb flushes for .text page faults | |
3627 | * with threads. | |
3628 | */ | |
82b0f8c3 JK |
3629 | if (vmf->flags & FAULT_FLAG_WRITE) |
3630 | flush_tlb_fix_spurious_fault(vmf->vma, vmf->address); | |
1a44e149 | 3631 | } |
8f4e2101 | 3632 | unlock: |
82b0f8c3 | 3633 | pte_unmap_unlock(vmf->pte, vmf->ptl); |
83c54070 | 3634 | return 0; |
1da177e4 LT |
3635 | } |
3636 | ||
3637 | /* | |
3638 | * By the time we get here, we already hold the mm semaphore | |
9a95f3cf PC |
3639 | * |
3640 | * The mmap_sem may have been released depending on flags and our | |
3641 | * return value. See filemap_fault() and __lock_page_or_retry(). | |
1da177e4 | 3642 | */ |
dcddffd4 KS |
3643 | static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address, |
3644 | unsigned int flags) | |
1da177e4 | 3645 | { |
82b0f8c3 | 3646 | struct vm_fault vmf = { |
bae473a4 | 3647 | .vma = vma, |
1a29d85e | 3648 | .address = address & PAGE_MASK, |
bae473a4 | 3649 | .flags = flags, |
0721ec8b | 3650 | .pgoff = linear_page_index(vma, address), |
667240e0 | 3651 | .gfp_mask = __get_fault_gfp_mask(vma), |
bae473a4 | 3652 | }; |
dcddffd4 | 3653 | struct mm_struct *mm = vma->vm_mm; |
1da177e4 | 3654 | pgd_t *pgd; |
a2d58167 | 3655 | int ret; |
1da177e4 | 3656 | |
1da177e4 | 3657 | pgd = pgd_offset(mm, address); |
a00cc7d9 MW |
3658 | |
3659 | vmf.pud = pud_alloc(mm, pgd, address); | |
3660 | if (!vmf.pud) | |
c74df32c | 3661 | return VM_FAULT_OOM; |
a00cc7d9 | 3662 | if (pud_none(*vmf.pud) && transparent_hugepage_enabled(vma)) { |
a00cc7d9 MW |
3663 | ret = create_huge_pud(&vmf); |
3664 | if (!(ret & VM_FAULT_FALLBACK)) | |
3665 | return ret; | |
3666 | } else { | |
3667 | pud_t orig_pud = *vmf.pud; | |
3668 | ||
3669 | barrier(); | |
3670 | if (pud_trans_huge(orig_pud) || pud_devmap(orig_pud)) { | |
3671 | unsigned int dirty = flags & FAULT_FLAG_WRITE; | |
3672 | ||
a00cc7d9 MW |
3673 | /* NUMA case for anonymous PUDs would go here */ |
3674 | ||
3675 | if (dirty && !pud_write(orig_pud)) { | |
3676 | ret = wp_huge_pud(&vmf, orig_pud); | |
3677 | if (!(ret & VM_FAULT_FALLBACK)) | |
3678 | return ret; | |
3679 | } else { | |
3680 | huge_pud_set_accessed(&vmf, orig_pud); | |
3681 | return 0; | |
3682 | } | |
3683 | } | |
3684 | } | |
3685 | ||
3686 | vmf.pmd = pmd_alloc(mm, vmf.pud, address); | |
82b0f8c3 | 3687 | if (!vmf.pmd) |
c74df32c | 3688 | return VM_FAULT_OOM; |
82b0f8c3 | 3689 | if (pmd_none(*vmf.pmd) && transparent_hugepage_enabled(vma)) { |
a2d58167 | 3690 | ret = create_huge_pmd(&vmf); |
c0292554 KS |
3691 | if (!(ret & VM_FAULT_FALLBACK)) |
3692 | return ret; | |
71e3aac0 | 3693 | } else { |
82b0f8c3 | 3694 | pmd_t orig_pmd = *vmf.pmd; |
1f1d06c3 | 3695 | |
71e3aac0 | 3696 | barrier(); |
5c7fb56e | 3697 | if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) { |
38e08854 | 3698 | if (pmd_protnone(orig_pmd) && vma_is_accessible(vma)) |
82b0f8c3 | 3699 | return do_huge_pmd_numa_page(&vmf, orig_pmd); |
d10e63f2 | 3700 | |
82b0f8c3 | 3701 | if ((vmf.flags & FAULT_FLAG_WRITE) && |
bae473a4 | 3702 | !pmd_write(orig_pmd)) { |
82b0f8c3 | 3703 | ret = wp_huge_pmd(&vmf, orig_pmd); |
9845cbbd KS |
3704 | if (!(ret & VM_FAULT_FALLBACK)) |
3705 | return ret; | |
a1dd450b | 3706 | } else { |
82b0f8c3 | 3707 | huge_pmd_set_accessed(&vmf, orig_pmd); |
9845cbbd | 3708 | return 0; |
1f1d06c3 | 3709 | } |
71e3aac0 AA |
3710 | } |
3711 | } | |
3712 | ||
82b0f8c3 | 3713 | return handle_pte_fault(&vmf); |
1da177e4 LT |
3714 | } |
3715 | ||
9a95f3cf PC |
3716 | /* |
3717 | * By the time we get here, we already hold the mm semaphore | |
3718 | * | |
3719 | * The mmap_sem may have been released depending on flags and our | |
3720 | * return value. See filemap_fault() and __lock_page_or_retry(). | |
3721 | */ | |
dcddffd4 KS |
3722 | int handle_mm_fault(struct vm_area_struct *vma, unsigned long address, |
3723 | unsigned int flags) | |
519e5247 JW |
3724 | { |
3725 | int ret; | |
3726 | ||
3727 | __set_current_state(TASK_RUNNING); | |
3728 | ||
3729 | count_vm_event(PGFAULT); | |
dcddffd4 | 3730 | mem_cgroup_count_vm_event(vma->vm_mm, PGFAULT); |
519e5247 JW |
3731 | |
3732 | /* do counter updates before entering really critical section. */ | |
3733 | check_sync_rss_stat(current); | |
3734 | ||
3735 | /* | |
3736 | * Enable the memcg OOM handling for faults triggered in user | |
3737 | * space. Kernel faults are handled more gracefully. | |
3738 | */ | |
3739 | if (flags & FAULT_FLAG_USER) | |
49426420 | 3740 | mem_cgroup_oom_enable(); |
519e5247 | 3741 | |
bae473a4 KS |
3742 | if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE, |
3743 | flags & FAULT_FLAG_INSTRUCTION, | |
3744 | flags & FAULT_FLAG_REMOTE)) | |
3745 | return VM_FAULT_SIGSEGV; | |
3746 | ||
3747 | if (unlikely(is_vm_hugetlb_page(vma))) | |
3748 | ret = hugetlb_fault(vma->vm_mm, vma, address, flags); | |
3749 | else | |
3750 | ret = __handle_mm_fault(vma, address, flags); | |
519e5247 | 3751 | |
49426420 JW |
3752 | if (flags & FAULT_FLAG_USER) { |
3753 | mem_cgroup_oom_disable(); | |
166f61b9 TH |
3754 | /* |
3755 | * The task may have entered a memcg OOM situation but | |
3756 | * if the allocation error was handled gracefully (no | |
3757 | * VM_FAULT_OOM), there is no need to kill anything. | |
3758 | * Just clean up the OOM state peacefully. | |
3759 | */ | |
3760 | if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM)) | |
3761 | mem_cgroup_oom_synchronize(false); | |
49426420 | 3762 | } |
3812c8c8 | 3763 | |
3f70dc38 MH |
3764 | /* |
3765 | * This mm has been already reaped by the oom reaper and so the | |
3766 | * refault cannot be trusted in general. Anonymous refaults would | |
3767 | * lose data and give a zero page instead e.g. This is especially | |
3768 | * problem for use_mm() because regular tasks will just die and | |
3769 | * the corrupted data will not be visible anywhere while kthread | |
3770 | * will outlive the oom victim and potentially propagate the date | |
3771 | * further. | |
3772 | */ | |
3773 | if (unlikely((current->flags & PF_KTHREAD) && !(ret & VM_FAULT_ERROR) | |
3774 | && test_bit(MMF_UNSTABLE, &vma->vm_mm->flags))) | |
3775 | ret = VM_FAULT_SIGBUS; | |
3776 | ||
519e5247 JW |
3777 | return ret; |
3778 | } | |
e1d6d01a | 3779 | EXPORT_SYMBOL_GPL(handle_mm_fault); |
519e5247 | 3780 | |
1da177e4 LT |
3781 | #ifndef __PAGETABLE_PUD_FOLDED |
3782 | /* | |
3783 | * Allocate page upper directory. | |
872fec16 | 3784 | * We've already handled the fast-path in-line. |
1da177e4 | 3785 | */ |
1bb3630e | 3786 | int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address) |
1da177e4 | 3787 | { |
c74df32c HD |
3788 | pud_t *new = pud_alloc_one(mm, address); |
3789 | if (!new) | |
1bb3630e | 3790 | return -ENOMEM; |
1da177e4 | 3791 | |
362a61ad NP |
3792 | smp_wmb(); /* See comment in __pte_alloc */ |
3793 | ||
872fec16 | 3794 | spin_lock(&mm->page_table_lock); |
1bb3630e | 3795 | if (pgd_present(*pgd)) /* Another has populated it */ |
5e541973 | 3796 | pud_free(mm, new); |
1bb3630e HD |
3797 | else |
3798 | pgd_populate(mm, pgd, new); | |
c74df32c | 3799 | spin_unlock(&mm->page_table_lock); |
1bb3630e | 3800 | return 0; |
1da177e4 LT |
3801 | } |
3802 | #endif /* __PAGETABLE_PUD_FOLDED */ | |
3803 | ||
3804 | #ifndef __PAGETABLE_PMD_FOLDED | |
3805 | /* | |
3806 | * Allocate page middle directory. | |
872fec16 | 3807 | * We've already handled the fast-path in-line. |
1da177e4 | 3808 | */ |
1bb3630e | 3809 | int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address) |
1da177e4 | 3810 | { |
a00cc7d9 | 3811 | spinlock_t *ptl; |
c74df32c HD |
3812 | pmd_t *new = pmd_alloc_one(mm, address); |
3813 | if (!new) | |
1bb3630e | 3814 | return -ENOMEM; |
1da177e4 | 3815 | |
362a61ad NP |
3816 | smp_wmb(); /* See comment in __pte_alloc */ |
3817 | ||
a00cc7d9 | 3818 | ptl = pud_lock(mm, pud); |
1da177e4 | 3819 | #ifndef __ARCH_HAS_4LEVEL_HACK |
dc6c9a35 KS |
3820 | if (!pud_present(*pud)) { |
3821 | mm_inc_nr_pmds(mm); | |
1bb3630e | 3822 | pud_populate(mm, pud, new); |
dc6c9a35 | 3823 | } else /* Another has populated it */ |
5e541973 | 3824 | pmd_free(mm, new); |
dc6c9a35 KS |
3825 | #else |
3826 | if (!pgd_present(*pud)) { | |
3827 | mm_inc_nr_pmds(mm); | |
1bb3630e | 3828 | pgd_populate(mm, pud, new); |
dc6c9a35 KS |
3829 | } else /* Another has populated it */ |
3830 | pmd_free(mm, new); | |
1da177e4 | 3831 | #endif /* __ARCH_HAS_4LEVEL_HACK */ |
a00cc7d9 | 3832 | spin_unlock(ptl); |
1bb3630e | 3833 | return 0; |
e0f39591 | 3834 | } |
1da177e4 LT |
3835 | #endif /* __PAGETABLE_PMD_FOLDED */ |
3836 | ||
09796395 RZ |
3837 | static int __follow_pte_pmd(struct mm_struct *mm, unsigned long address, |
3838 | pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp) | |
f8ad0f49 JW |
3839 | { |
3840 | pgd_t *pgd; | |
3841 | pud_t *pud; | |
3842 | pmd_t *pmd; | |
3843 | pte_t *ptep; | |
3844 | ||
3845 | pgd = pgd_offset(mm, address); | |
3846 | if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) | |
3847 | goto out; | |
3848 | ||
3849 | pud = pud_offset(pgd, address); | |
3850 | if (pud_none(*pud) || unlikely(pud_bad(*pud))) | |
3851 | goto out; | |
3852 | ||
3853 | pmd = pmd_offset(pud, address); | |
f66055ab | 3854 | VM_BUG_ON(pmd_trans_huge(*pmd)); |
f8ad0f49 | 3855 | |
09796395 RZ |
3856 | if (pmd_huge(*pmd)) { |
3857 | if (!pmdpp) | |
3858 | goto out; | |
3859 | ||
3860 | *ptlp = pmd_lock(mm, pmd); | |
3861 | if (pmd_huge(*pmd)) { | |
3862 | *pmdpp = pmd; | |
3863 | return 0; | |
3864 | } | |
3865 | spin_unlock(*ptlp); | |
3866 | } | |
3867 | ||
3868 | if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) | |
f8ad0f49 JW |
3869 | goto out; |
3870 | ||
3871 | ptep = pte_offset_map_lock(mm, pmd, address, ptlp); | |
3872 | if (!ptep) | |
3873 | goto out; | |
3874 | if (!pte_present(*ptep)) | |
3875 | goto unlock; | |
3876 | *ptepp = ptep; | |
3877 | return 0; | |
3878 | unlock: | |
3879 | pte_unmap_unlock(ptep, *ptlp); | |
3880 | out: | |
3881 | return -EINVAL; | |
3882 | } | |
3883 | ||
f729c8c9 RZ |
3884 | static inline int follow_pte(struct mm_struct *mm, unsigned long address, |
3885 | pte_t **ptepp, spinlock_t **ptlp) | |
1b36ba81 NK |
3886 | { |
3887 | int res; | |
3888 | ||
3889 | /* (void) is needed to make gcc happy */ | |
3890 | (void) __cond_lock(*ptlp, | |
09796395 RZ |
3891 | !(res = __follow_pte_pmd(mm, address, ptepp, NULL, |
3892 | ptlp))); | |
3893 | return res; | |
3894 | } | |
3895 | ||
3896 | int follow_pte_pmd(struct mm_struct *mm, unsigned long address, | |
3897 | pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp) | |
3898 | { | |
3899 | int res; | |
3900 | ||
3901 | /* (void) is needed to make gcc happy */ | |
3902 | (void) __cond_lock(*ptlp, | |
3903 | !(res = __follow_pte_pmd(mm, address, ptepp, pmdpp, | |
3904 | ptlp))); | |
1b36ba81 NK |
3905 | return res; |
3906 | } | |
09796395 | 3907 | EXPORT_SYMBOL(follow_pte_pmd); |
1b36ba81 | 3908 | |
3b6748e2 JW |
3909 | /** |
3910 | * follow_pfn - look up PFN at a user virtual address | |
3911 | * @vma: memory mapping | |
3912 | * @address: user virtual address | |
3913 | * @pfn: location to store found PFN | |
3914 | * | |
3915 | * Only IO mappings and raw PFN mappings are allowed. | |
3916 | * | |
3917 | * Returns zero and the pfn at @pfn on success, -ve otherwise. | |
3918 | */ | |
3919 | int follow_pfn(struct vm_area_struct *vma, unsigned long address, | |
3920 | unsigned long *pfn) | |
3921 | { | |
3922 | int ret = -EINVAL; | |
3923 | spinlock_t *ptl; | |
3924 | pte_t *ptep; | |
3925 | ||
3926 | if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) | |
3927 | return ret; | |
3928 | ||
3929 | ret = follow_pte(vma->vm_mm, address, &ptep, &ptl); | |
3930 | if (ret) | |
3931 | return ret; | |
3932 | *pfn = pte_pfn(*ptep); | |
3933 | pte_unmap_unlock(ptep, ptl); | |
3934 | return 0; | |
3935 | } | |
3936 | EXPORT_SYMBOL(follow_pfn); | |
3937 | ||
28b2ee20 | 3938 | #ifdef CONFIG_HAVE_IOREMAP_PROT |
d87fe660 | 3939 | int follow_phys(struct vm_area_struct *vma, |
3940 | unsigned long address, unsigned int flags, | |
3941 | unsigned long *prot, resource_size_t *phys) | |
28b2ee20 | 3942 | { |
03668a4d | 3943 | int ret = -EINVAL; |
28b2ee20 RR |
3944 | pte_t *ptep, pte; |
3945 | spinlock_t *ptl; | |
28b2ee20 | 3946 | |
d87fe660 | 3947 | if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) |
3948 | goto out; | |
28b2ee20 | 3949 | |
03668a4d | 3950 | if (follow_pte(vma->vm_mm, address, &ptep, &ptl)) |
d87fe660 | 3951 | goto out; |
28b2ee20 | 3952 | pte = *ptep; |
03668a4d | 3953 | |
28b2ee20 RR |
3954 | if ((flags & FOLL_WRITE) && !pte_write(pte)) |
3955 | goto unlock; | |
28b2ee20 RR |
3956 | |
3957 | *prot = pgprot_val(pte_pgprot(pte)); | |
03668a4d | 3958 | *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT; |
28b2ee20 | 3959 | |
03668a4d | 3960 | ret = 0; |
28b2ee20 RR |
3961 | unlock: |
3962 | pte_unmap_unlock(ptep, ptl); | |
3963 | out: | |
d87fe660 | 3964 | return ret; |
28b2ee20 RR |
3965 | } |
3966 | ||
3967 | int generic_access_phys(struct vm_area_struct *vma, unsigned long addr, | |
3968 | void *buf, int len, int write) | |
3969 | { | |
3970 | resource_size_t phys_addr; | |
3971 | unsigned long prot = 0; | |
2bc7273b | 3972 | void __iomem *maddr; |
28b2ee20 RR |
3973 | int offset = addr & (PAGE_SIZE-1); |
3974 | ||
d87fe660 | 3975 | if (follow_phys(vma, addr, write, &prot, &phys_addr)) |
28b2ee20 RR |
3976 | return -EINVAL; |
3977 | ||
9cb12d7b | 3978 | maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot); |
28b2ee20 RR |
3979 | if (write) |
3980 | memcpy_toio(maddr + offset, buf, len); | |
3981 | else | |
3982 | memcpy_fromio(buf, maddr + offset, len); | |
3983 | iounmap(maddr); | |
3984 | ||
3985 | return len; | |
3986 | } | |
5a73633e | 3987 | EXPORT_SYMBOL_GPL(generic_access_phys); |
28b2ee20 RR |
3988 | #endif |
3989 | ||
0ec76a11 | 3990 | /* |
206cb636 SW |
3991 | * Access another process' address space as given in mm. If non-NULL, use the |
3992 | * given task for page fault accounting. | |
0ec76a11 | 3993 | */ |
84d77d3f | 3994 | int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, |
442486ec | 3995 | unsigned long addr, void *buf, int len, unsigned int gup_flags) |
0ec76a11 | 3996 | { |
0ec76a11 | 3997 | struct vm_area_struct *vma; |
0ec76a11 | 3998 | void *old_buf = buf; |
442486ec | 3999 | int write = gup_flags & FOLL_WRITE; |
0ec76a11 | 4000 | |
0ec76a11 | 4001 | down_read(&mm->mmap_sem); |
183ff22b | 4002 | /* ignore errors, just check how much was successfully transferred */ |
0ec76a11 DH |
4003 | while (len) { |
4004 | int bytes, ret, offset; | |
4005 | void *maddr; | |
28b2ee20 | 4006 | struct page *page = NULL; |
0ec76a11 | 4007 | |
1e987790 | 4008 | ret = get_user_pages_remote(tsk, mm, addr, 1, |
5b56d49f | 4009 | gup_flags, &page, &vma, NULL); |
28b2ee20 | 4010 | if (ret <= 0) { |
dbffcd03 RR |
4011 | #ifndef CONFIG_HAVE_IOREMAP_PROT |
4012 | break; | |
4013 | #else | |
28b2ee20 RR |
4014 | /* |
4015 | * Check if this is a VM_IO | VM_PFNMAP VMA, which | |
4016 | * we can access using slightly different code. | |
4017 | */ | |
28b2ee20 | 4018 | vma = find_vma(mm, addr); |
fe936dfc | 4019 | if (!vma || vma->vm_start > addr) |
28b2ee20 RR |
4020 | break; |
4021 | if (vma->vm_ops && vma->vm_ops->access) | |
4022 | ret = vma->vm_ops->access(vma, addr, buf, | |
4023 | len, write); | |
4024 | if (ret <= 0) | |
28b2ee20 RR |
4025 | break; |
4026 | bytes = ret; | |
dbffcd03 | 4027 | #endif |
0ec76a11 | 4028 | } else { |
28b2ee20 RR |
4029 | bytes = len; |
4030 | offset = addr & (PAGE_SIZE-1); | |
4031 | if (bytes > PAGE_SIZE-offset) | |
4032 | bytes = PAGE_SIZE-offset; | |
4033 | ||
4034 | maddr = kmap(page); | |
4035 | if (write) { | |
4036 | copy_to_user_page(vma, page, addr, | |
4037 | maddr + offset, buf, bytes); | |
4038 | set_page_dirty_lock(page); | |
4039 | } else { | |
4040 | copy_from_user_page(vma, page, addr, | |
4041 | buf, maddr + offset, bytes); | |
4042 | } | |
4043 | kunmap(page); | |
09cbfeaf | 4044 | put_page(page); |
0ec76a11 | 4045 | } |
0ec76a11 DH |
4046 | len -= bytes; |
4047 | buf += bytes; | |
4048 | addr += bytes; | |
4049 | } | |
4050 | up_read(&mm->mmap_sem); | |
0ec76a11 DH |
4051 | |
4052 | return buf - old_buf; | |
4053 | } | |
03252919 | 4054 | |
5ddd36b9 | 4055 | /** |
ae91dbfc | 4056 | * access_remote_vm - access another process' address space |
5ddd36b9 SW |
4057 | * @mm: the mm_struct of the target address space |
4058 | * @addr: start address to access | |
4059 | * @buf: source or destination buffer | |
4060 | * @len: number of bytes to transfer | |
6347e8d5 | 4061 | * @gup_flags: flags modifying lookup behaviour |
5ddd36b9 SW |
4062 | * |
4063 | * The caller must hold a reference on @mm. | |
4064 | */ | |
4065 | int access_remote_vm(struct mm_struct *mm, unsigned long addr, | |
6347e8d5 | 4066 | void *buf, int len, unsigned int gup_flags) |
5ddd36b9 | 4067 | { |
6347e8d5 | 4068 | return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags); |
5ddd36b9 SW |
4069 | } |
4070 | ||
206cb636 SW |
4071 | /* |
4072 | * Access another process' address space. | |
4073 | * Source/target buffer must be kernel space, | |
4074 | * Do not walk the page table directly, use get_user_pages | |
4075 | */ | |
4076 | int access_process_vm(struct task_struct *tsk, unsigned long addr, | |
f307ab6d | 4077 | void *buf, int len, unsigned int gup_flags) |
206cb636 SW |
4078 | { |
4079 | struct mm_struct *mm; | |
4080 | int ret; | |
4081 | ||
4082 | mm = get_task_mm(tsk); | |
4083 | if (!mm) | |
4084 | return 0; | |
4085 | ||
f307ab6d | 4086 | ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags); |
442486ec | 4087 | |
206cb636 SW |
4088 | mmput(mm); |
4089 | ||
4090 | return ret; | |
4091 | } | |
fcd35857 | 4092 | EXPORT_SYMBOL_GPL(access_process_vm); |
206cb636 | 4093 | |
03252919 AK |
4094 | /* |
4095 | * Print the name of a VMA. | |
4096 | */ | |
4097 | void print_vma_addr(char *prefix, unsigned long ip) | |
4098 | { | |
4099 | struct mm_struct *mm = current->mm; | |
4100 | struct vm_area_struct *vma; | |
4101 | ||
e8bff74a IM |
4102 | /* |
4103 | * Do not print if we are in atomic | |
4104 | * contexts (in exception stacks, etc.): | |
4105 | */ | |
4106 | if (preempt_count()) | |
4107 | return; | |
4108 | ||
03252919 AK |
4109 | down_read(&mm->mmap_sem); |
4110 | vma = find_vma(mm, ip); | |
4111 | if (vma && vma->vm_file) { | |
4112 | struct file *f = vma->vm_file; | |
4113 | char *buf = (char *)__get_free_page(GFP_KERNEL); | |
4114 | if (buf) { | |
2fbc57c5 | 4115 | char *p; |
03252919 | 4116 | |
9bf39ab2 | 4117 | p = file_path(f, buf, PAGE_SIZE); |
03252919 AK |
4118 | if (IS_ERR(p)) |
4119 | p = "?"; | |
2fbc57c5 | 4120 | printk("%s%s[%lx+%lx]", prefix, kbasename(p), |
03252919 AK |
4121 | vma->vm_start, |
4122 | vma->vm_end - vma->vm_start); | |
4123 | free_page((unsigned long)buf); | |
4124 | } | |
4125 | } | |
51a07e50 | 4126 | up_read(&mm->mmap_sem); |
03252919 | 4127 | } |
3ee1afa3 | 4128 | |
662bbcb2 | 4129 | #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP) |
9ec23531 | 4130 | void __might_fault(const char *file, int line) |
3ee1afa3 | 4131 | { |
95156f00 PZ |
4132 | /* |
4133 | * Some code (nfs/sunrpc) uses socket ops on kernel memory while | |
4134 | * holding the mmap_sem, this is safe because kernel memory doesn't | |
4135 | * get paged out, therefore we'll never actually fault, and the | |
4136 | * below annotations will generate false positives. | |
4137 | */ | |
4138 | if (segment_eq(get_fs(), KERNEL_DS)) | |
4139 | return; | |
9ec23531 | 4140 | if (pagefault_disabled()) |
662bbcb2 | 4141 | return; |
9ec23531 DH |
4142 | __might_sleep(file, line, 0); |
4143 | #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) | |
662bbcb2 | 4144 | if (current->mm) |
3ee1afa3 | 4145 | might_lock_read(¤t->mm->mmap_sem); |
9ec23531 | 4146 | #endif |
3ee1afa3 | 4147 | } |
9ec23531 | 4148 | EXPORT_SYMBOL(__might_fault); |
3ee1afa3 | 4149 | #endif |
47ad8475 AA |
4150 | |
4151 | #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS) | |
4152 | static void clear_gigantic_page(struct page *page, | |
4153 | unsigned long addr, | |
4154 | unsigned int pages_per_huge_page) | |
4155 | { | |
4156 | int i; | |
4157 | struct page *p = page; | |
4158 | ||
4159 | might_sleep(); | |
4160 | for (i = 0; i < pages_per_huge_page; | |
4161 | i++, p = mem_map_next(p, page, i)) { | |
4162 | cond_resched(); | |
4163 | clear_user_highpage(p, addr + i * PAGE_SIZE); | |
4164 | } | |
4165 | } | |
4166 | void clear_huge_page(struct page *page, | |
4167 | unsigned long addr, unsigned int pages_per_huge_page) | |
4168 | { | |
4169 | int i; | |
4170 | ||
4171 | if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { | |
4172 | clear_gigantic_page(page, addr, pages_per_huge_page); | |
4173 | return; | |
4174 | } | |
4175 | ||
4176 | might_sleep(); | |
4177 | for (i = 0; i < pages_per_huge_page; i++) { | |
4178 | cond_resched(); | |
4179 | clear_user_highpage(page + i, addr + i * PAGE_SIZE); | |
4180 | } | |
4181 | } | |
4182 | ||
4183 | static void copy_user_gigantic_page(struct page *dst, struct page *src, | |
4184 | unsigned long addr, | |
4185 | struct vm_area_struct *vma, | |
4186 | unsigned int pages_per_huge_page) | |
4187 | { | |
4188 | int i; | |
4189 | struct page *dst_base = dst; | |
4190 | struct page *src_base = src; | |
4191 | ||
4192 | for (i = 0; i < pages_per_huge_page; ) { | |
4193 | cond_resched(); | |
4194 | copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma); | |
4195 | ||
4196 | i++; | |
4197 | dst = mem_map_next(dst, dst_base, i); | |
4198 | src = mem_map_next(src, src_base, i); | |
4199 | } | |
4200 | } | |
4201 | ||
4202 | void copy_user_huge_page(struct page *dst, struct page *src, | |
4203 | unsigned long addr, struct vm_area_struct *vma, | |
4204 | unsigned int pages_per_huge_page) | |
4205 | { | |
4206 | int i; | |
4207 | ||
4208 | if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) { | |
4209 | copy_user_gigantic_page(dst, src, addr, vma, | |
4210 | pages_per_huge_page); | |
4211 | return; | |
4212 | } | |
4213 | ||
4214 | might_sleep(); | |
4215 | for (i = 0; i < pages_per_huge_page; i++) { | |
4216 | cond_resched(); | |
4217 | copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma); | |
4218 | } | |
4219 | } | |
fa4d75c1 MK |
4220 | |
4221 | long copy_huge_page_from_user(struct page *dst_page, | |
4222 | const void __user *usr_src, | |
810a56b9 MK |
4223 | unsigned int pages_per_huge_page, |
4224 | bool allow_pagefault) | |
fa4d75c1 MK |
4225 | { |
4226 | void *src = (void *)usr_src; | |
4227 | void *page_kaddr; | |
4228 | unsigned long i, rc = 0; | |
4229 | unsigned long ret_val = pages_per_huge_page * PAGE_SIZE; | |
4230 | ||
4231 | for (i = 0; i < pages_per_huge_page; i++) { | |
810a56b9 MK |
4232 | if (allow_pagefault) |
4233 | page_kaddr = kmap(dst_page + i); | |
4234 | else | |
4235 | page_kaddr = kmap_atomic(dst_page + i); | |
fa4d75c1 MK |
4236 | rc = copy_from_user(page_kaddr, |
4237 | (const void __user *)(src + i * PAGE_SIZE), | |
4238 | PAGE_SIZE); | |
810a56b9 MK |
4239 | if (allow_pagefault) |
4240 | kunmap(dst_page + i); | |
4241 | else | |
4242 | kunmap_atomic(page_kaddr); | |
fa4d75c1 MK |
4243 | |
4244 | ret_val -= (PAGE_SIZE - rc); | |
4245 | if (rc) | |
4246 | break; | |
4247 | ||
4248 | cond_resched(); | |
4249 | } | |
4250 | return ret_val; | |
4251 | } | |
47ad8475 | 4252 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ |
49076ec2 | 4253 | |
40b64acd | 4254 | #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS |
b35f1819 KS |
4255 | |
4256 | static struct kmem_cache *page_ptl_cachep; | |
4257 | ||
4258 | void __init ptlock_cache_init(void) | |
4259 | { | |
4260 | page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0, | |
4261 | SLAB_PANIC, NULL); | |
4262 | } | |
4263 | ||
539edb58 | 4264 | bool ptlock_alloc(struct page *page) |
49076ec2 KS |
4265 | { |
4266 | spinlock_t *ptl; | |
4267 | ||
b35f1819 | 4268 | ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL); |
49076ec2 KS |
4269 | if (!ptl) |
4270 | return false; | |
539edb58 | 4271 | page->ptl = ptl; |
49076ec2 KS |
4272 | return true; |
4273 | } | |
4274 | ||
539edb58 | 4275 | void ptlock_free(struct page *page) |
49076ec2 | 4276 | { |
b35f1819 | 4277 | kmem_cache_free(page_ptl_cachep, page->ptl); |
49076ec2 KS |
4278 | } |
4279 | #endif |