hc
2024-05-11 04dd17822334871b23ea2862f7798fb0e0007777
kernel/mm/memory.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/mm/memory.c
34 *
....@@ -64,11 +65,15 @@
6465 #include <linux/gfp.h>
6566 #include <linux/migrate.h>
6667 #include <linux/string.h>
67
-#include <linux/dma-debug.h>
6868 #include <linux/debugfs.h>
6969 #include <linux/userfaultfd_k.h>
7070 #include <linux/dax.h>
7171 #include <linux/oom.h>
72
+#include <linux/numa.h>
73
+#include <linux/perf_event.h>
74
+#include <linux/ptrace.h>
75
+#include <linux/vmalloc.h>
76
+#include <trace/hooks/mm.h>
7277
7378 #include <trace/events/kmem.h>
7479
....@@ -78,9 +83,13 @@
7883 #include <linux/uaccess.h>
7984 #include <asm/tlb.h>
8085 #include <asm/tlbflush.h>
81
-#include <asm/pgtable.h>
8286
87
+#include "pgalloc-track.h"
8388 #include "internal.h"
89
+#include <trace/hooks/mm.h>
90
+
91
+#define CREATE_TRACE_POINTS
92
+#include <trace/events/pagefault.h>
8493
8594 #if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST)
8695 #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
....@@ -127,6 +136,18 @@
127136 * will be hit on old pte.
128137 */
129138 return true;
139
+}
140
+#endif
141
+
142
+#ifndef arch_wants_old_prefaulted_pte
143
+static inline bool arch_wants_old_prefaulted_pte(void)
144
+{
145
+ /*
146
+ * Transitioning a PTE from 'old' to 'young' can be expensive on
147
+ * some architectures, even if it's performed in hardware. By
148
+ * default, "false" means prefaulted entries will be 'young'.
149
+ */
150
+ return false;
130151 }
131152 #endif
132153
....@@ -217,263 +238,6 @@
217238
218239 #endif /* SPLIT_RSS_COUNTING */
219240
220
-#ifdef HAVE_GENERIC_MMU_GATHER
221
-
222
-static bool tlb_next_batch(struct mmu_gather *tlb)
223
-{
224
- struct mmu_gather_batch *batch;
225
-
226
- batch = tlb->active;
227
- if (batch->next) {
228
- tlb->active = batch->next;
229
- return true;
230
- }
231
-
232
- if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
233
- return false;
234
-
235
- batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
236
- if (!batch)
237
- return false;
238
-
239
- tlb->batch_count++;
240
- batch->next = NULL;
241
- batch->nr = 0;
242
- batch->max = MAX_GATHER_BATCH;
243
-
244
- tlb->active->next = batch;
245
- tlb->active = batch;
246
-
247
- return true;
248
-}
249
-
250
-void arch_tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
251
- unsigned long start, unsigned long end)
252
-{
253
- tlb->mm = mm;
254
-
255
- /* Is it from 0 to ~0? */
256
- tlb->fullmm = !(start | (end+1));
257
- tlb->need_flush_all = 0;
258
- tlb->local.next = NULL;
259
- tlb->local.nr = 0;
260
- tlb->local.max = ARRAY_SIZE(tlb->__pages);
261
- tlb->active = &tlb->local;
262
- tlb->batch_count = 0;
263
-
264
-#ifdef CONFIG_HAVE_RCU_TABLE_FREE
265
- tlb->batch = NULL;
266
-#endif
267
- tlb->page_size = 0;
268
-
269
- __tlb_reset_range(tlb);
270
-}
271
-
272
-static void tlb_flush_mmu_free(struct mmu_gather *tlb)
273
-{
274
- struct mmu_gather_batch *batch;
275
-
276
-#ifdef CONFIG_HAVE_RCU_TABLE_FREE
277
- tlb_table_flush(tlb);
278
-#endif
279
- for (batch = &tlb->local; batch && batch->nr; batch = batch->next) {
280
- free_pages_and_swap_cache(batch->pages, batch->nr);
281
- batch->nr = 0;
282
- }
283
- tlb->active = &tlb->local;
284
-}
285
-
286
-void tlb_flush_mmu(struct mmu_gather *tlb)
287
-{
288
- tlb_flush_mmu_tlbonly(tlb);
289
- tlb_flush_mmu_free(tlb);
290
-}
291
-
292
-/* tlb_finish_mmu
293
- * Called at the end of the shootdown operation to free up any resources
294
- * that were required.
295
- */
296
-void arch_tlb_finish_mmu(struct mmu_gather *tlb,
297
- unsigned long start, unsigned long end, bool force)
298
-{
299
- struct mmu_gather_batch *batch, *next;
300
-
301
- if (force)
302
- __tlb_adjust_range(tlb, start, end - start);
303
-
304
- tlb_flush_mmu(tlb);
305
-
306
- /* keep the page table cache within bounds */
307
- check_pgt_cache();
308
-
309
- for (batch = tlb->local.next; batch; batch = next) {
310
- next = batch->next;
311
- free_pages((unsigned long)batch, 0);
312
- }
313
- tlb->local.next = NULL;
314
-}
315
-
316
-/* __tlb_remove_page
317
- * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
318
- * handling the additional races in SMP caused by other CPUs caching valid
319
- * mappings in their TLBs. Returns the number of free page slots left.
320
- * When out of page slots we must call tlb_flush_mmu().
321
- *returns true if the caller should flush.
322
- */
323
-bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size)
324
-{
325
- struct mmu_gather_batch *batch;
326
-
327
- VM_BUG_ON(!tlb->end);
328
- VM_WARN_ON(tlb->page_size != page_size);
329
-
330
- batch = tlb->active;
331
- /*
332
- * Add the page and check if we are full. If so
333
- * force a flush.
334
- */
335
- batch->pages[batch->nr++] = page;
336
- if (batch->nr == batch->max) {
337
- if (!tlb_next_batch(tlb))
338
- return true;
339
- batch = tlb->active;
340
- }
341
- VM_BUG_ON_PAGE(batch->nr > batch->max, page);
342
-
343
- return false;
344
-}
345
-
346
-void tlb_flush_pmd_range(struct mmu_gather *tlb, unsigned long address,
347
- unsigned long size)
348
-{
349
- if (tlb->page_size != 0 && tlb->page_size != PMD_SIZE)
350
- tlb_flush_mmu(tlb);
351
-
352
- tlb->page_size = PMD_SIZE;
353
- tlb->start = min(tlb->start, address);
354
- tlb->end = max(tlb->end, address + size);
355
-}
356
-#endif /* HAVE_GENERIC_MMU_GATHER */
357
-
358
-#ifdef CONFIG_HAVE_RCU_TABLE_FREE
359
-
360
-/*
361
- * See the comment near struct mmu_table_batch.
362
- */
363
-
364
-/*
365
- * If we want tlb_remove_table() to imply TLB invalidates.
366
- */
367
-static inline void tlb_table_invalidate(struct mmu_gather *tlb)
368
-{
369
-#ifdef CONFIG_HAVE_RCU_TABLE_INVALIDATE
370
- /*
371
- * Invalidate page-table caches used by hardware walkers. Then we still
372
- * need to RCU-sched wait while freeing the pages because software
373
- * walkers can still be in-flight.
374
- */
375
- tlb_flush_mmu_tlbonly(tlb);
376
-#endif
377
-}
378
-
379
-static void tlb_remove_table_smp_sync(void *arg)
380
-{
381
- /* Simply deliver the interrupt */
382
-}
383
-
384
-static void tlb_remove_table_one(void *table)
385
-{
386
- /*
387
- * This isn't an RCU grace period and hence the page-tables cannot be
388
- * assumed to be actually RCU-freed.
389
- *
390
- * It is however sufficient for software page-table walkers that rely on
391
- * IRQ disabling. See the comment near struct mmu_table_batch.
392
- */
393
- smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
394
- __tlb_remove_table(table);
395
-}
396
-
397
-static void tlb_remove_table_rcu(struct rcu_head *head)
398
-{
399
- struct mmu_table_batch *batch;
400
- int i;
401
-
402
- batch = container_of(head, struct mmu_table_batch, rcu);
403
-
404
- for (i = 0; i < batch->nr; i++)
405
- __tlb_remove_table(batch->tables[i]);
406
-
407
- free_page((unsigned long)batch);
408
-}
409
-
410
-void tlb_table_flush(struct mmu_gather *tlb)
411
-{
412
- struct mmu_table_batch **batch = &tlb->batch;
413
-
414
- if (*batch) {
415
- tlb_table_invalidate(tlb);
416
- call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
417
- *batch = NULL;
418
- }
419
-}
420
-
421
-void tlb_remove_table(struct mmu_gather *tlb, void *table)
422
-{
423
- struct mmu_table_batch **batch = &tlb->batch;
424
-
425
- if (*batch == NULL) {
426
- *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
427
- if (*batch == NULL) {
428
- tlb_table_invalidate(tlb);
429
- tlb_remove_table_one(table);
430
- return;
431
- }
432
- (*batch)->nr = 0;
433
- }
434
-
435
- (*batch)->tables[(*batch)->nr++] = table;
436
- if ((*batch)->nr == MAX_TABLE_BATCH)
437
- tlb_table_flush(tlb);
438
-}
439
-
440
-#endif /* CONFIG_HAVE_RCU_TABLE_FREE */
441
-
442
-/**
443
- * tlb_gather_mmu - initialize an mmu_gather structure for page-table tear-down
444
- * @tlb: the mmu_gather structure to initialize
445
- * @mm: the mm_struct of the target address space
446
- * @start: start of the region that will be removed from the page-table
447
- * @end: end of the region that will be removed from the page-table
448
- *
449
- * Called to initialize an (on-stack) mmu_gather structure for page-table
450
- * tear-down from @mm. The @start and @end are set to 0 and -1
451
- * respectively when @mm is without users and we're going to destroy
452
- * the full address space (exit/execve).
453
- */
454
-void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
455
- unsigned long start, unsigned long end)
456
-{
457
- arch_tlb_gather_mmu(tlb, mm, start, end);
458
- inc_tlb_flush_pending(tlb->mm);
459
-}
460
-
461
-void tlb_finish_mmu(struct mmu_gather *tlb,
462
- unsigned long start, unsigned long end)
463
-{
464
- /*
465
- * If there are parallel threads are doing PTE changes on same range
466
- * under non-exclusive lock(e.g., mmap_sem read-side) but defer TLB
467
- * flush by batching, a thread has stable TLB entry can fail to flush
468
- * the TLB by observing pte_none|!pte_dirty, for example so flush TLB
469
- * forcefully if we detect parallel PTE batching threads.
470
- */
471
- bool force = mm_tlb_flush_nested(tlb->mm);
472
-
473
- arch_tlb_finish_mmu(tlb, start, end, force);
474
- dec_tlb_flush_pending(tlb->mm);
475
-}
476
-
477241 /*
478242 * Note: this doesn't free the actual pages themselves. That
479243 * has been handled earlier when unmapping all the memory regions.
....@@ -482,6 +246,16 @@
482246 unsigned long addr)
483247 {
484248 pgtable_t token = pmd_pgtable(*pmd);
249
+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
250
+ /*
251
+ * Ensure page table destruction is blocked if __pte_map_lock managed
252
+ * to take this lock. Without this barrier tlb_remove_table_rcu can
253
+ * destroy ptl after __pte_map_lock locked it and during unlock would
254
+ * cause a use-after-free.
255
+ */
256
+ spinlock_t *ptl = pmd_lock(tlb->mm, pmd);
257
+ spin_unlock(ptl);
258
+#endif
485259 pmd_clear(pmd);
486260 pte_free_tlb(tlb, token, addr);
487261 mm_dec_nr_ptes(tlb->mm);
....@@ -643,7 +417,7 @@
643417 * We add page table cache pages with PAGE_SIZE,
644418 * (see pte_free_tlb()), flush the tlb if we need
645419 */
646
- tlb_remove_check_page_size_change(tlb, PAGE_SIZE);
420
+ tlb_change_page_size(tlb, PAGE_SIZE);
647421 pgd = pgd_offset(tlb->mm, addr);
648422 do {
649423 next = pgd_addr_end(addr, end);
....@@ -664,7 +438,9 @@
664438 * Hide vma from rmap and truncate_pagecache before freeing
665439 * pgtables
666440 */
441
+ vm_write_begin(vma);
667442 unlink_anon_vmas(vma);
443
+ vm_write_end(vma);
668444 unlink_file_vma(vma);
669445
670446 if (is_vm_hugetlb_page(vma)) {
....@@ -678,7 +454,9 @@
678454 && !is_vm_hugetlb_page(next)) {
679455 vma = next;
680456 next = vma->vm_next;
457
+ vm_write_begin(vma);
681458 unlink_anon_vmas(vma);
459
+ vm_write_end(vma);
682460 unlink_file_vma(vma);
683461 }
684462 free_pgd_range(tlb, addr, vma->vm_end,
....@@ -688,10 +466,10 @@
688466 }
689467 }
690468
691
-int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
469
+int __pte_alloc(struct mm_struct *mm, pmd_t *pmd)
692470 {
693471 spinlock_t *ptl;
694
- pgtable_t new = pte_alloc_one(mm, address);
472
+ pgtable_t new = pte_alloc_one(mm);
695473 if (!new)
696474 return -ENOMEM;
697475
....@@ -706,7 +484,7 @@
706484 * of a chain of data-dependent loads, meaning most CPUs (alpha
707485 * being the notable exception) will already guarantee loads are
708486 * seen in-order. See the alpha page table accessors for the
709
- * smp_read_barrier_depends() barriers in page table walking code.
487
+ * smp_rmb() barriers in page table walking code.
710488 */
711489 smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
712490
....@@ -722,9 +500,9 @@
722500 return 0;
723501 }
724502
725
-int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
503
+int __pte_alloc_kernel(pmd_t *pmd)
726504 {
727
- pte_t *new = pte_alloc_one_kernel(&init_mm, address);
505
+ pte_t *new = pte_alloc_one_kernel(&init_mm);
728506 if (!new)
729507 return -ENOMEM;
730508
....@@ -804,9 +582,9 @@
804582 (long long)pte_val(pte), (long long)pmd_val(*pmd));
805583 if (page)
806584 dump_page(page, "bad pte");
807
- pr_alert("addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n",
808
- (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
809
- pr_alert("file:%pD fault:%pf mmap:%pf readpage:%pf\n",
585
+ pr_alert("addr:%px vm_flags:%08lx anon_vma:%px mapping:%px index:%lx\n",
586
+ (void *)addr, READ_ONCE(vma->vm_flags), vma->anon_vma, mapping, index);
587
+ pr_alert("file:%pD fault:%ps mmap:%ps readpage:%ps\n",
810588 vma->vm_file,
811589 vma->vm_ops ? vma->vm_ops->fault : NULL,
812590 vma->vm_file ? vma->vm_file->f_op->mmap : NULL,
....@@ -816,7 +594,8 @@
816594 }
817595
818596 /*
819
- * vm_normal_page -- This function gets the "struct page" associated with a pte.
597
+ * __vm_normal_page -- This function gets the "struct page" associated with
598
+ * a pte.
820599 *
821600 * "Special" mappings do not wish to be associated with a "struct page" (either
822601 * it doesn't exist, or it exists but they don't want to touch it). In this
....@@ -858,7 +637,7 @@
858637 *
859638 */
860639 struct page *_vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
861
- pte_t pte, bool with_public_device)
640
+ pte_t pte, unsigned long vma_flags)
862641 {
863642 unsigned long pfn = pte_pfn(pte);
864643
....@@ -867,33 +646,10 @@
867646 goto check_pfn;
868647 if (vma->vm_ops && vma->vm_ops->find_special_page)
869648 return vma->vm_ops->find_special_page(vma, addr);
870
- if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
649
+ if (vma_flags & (VM_PFNMAP | VM_MIXEDMAP))
871650 return NULL;
872651 if (is_zero_pfn(pfn))
873652 return NULL;
874
-
875
- /*
876
- * Device public pages are special pages (they are ZONE_DEVICE
877
- * pages but different from persistent memory). They behave
878
- * allmost like normal pages. The difference is that they are
879
- * not on the lru and thus should never be involve with any-
880
- * thing that involve lru manipulation (mlock, numa balancing,
881
- * ...).
882
- *
883
- * This is why we still want to return NULL for such page from
884
- * vm_normal_page() so that we do not have to special case all
885
- * call site of vm_normal_page().
886
- */
887
- if (likely(pfn <= highest_memmap_pfn)) {
888
- struct page *page = pfn_to_page(pfn);
889
-
890
- if (is_device_public_page(page)) {
891
- if (with_public_device)
892
- return page;
893
- return NULL;
894
- }
895
- }
896
-
897653 if (pte_devmap(pte))
898654 return NULL;
899655
....@@ -902,9 +658,13 @@
902658 }
903659
904660 /* !CONFIG_ARCH_HAS_PTE_SPECIAL case follows: */
661
+ /*
662
+ * This part should never get called when CONFIG_SPECULATIVE_PAGE_FAULT
663
+ * is set. This is mainly because we can't rely on vm_start.
664
+ */
905665
906
- if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
907
- if (vma->vm_flags & VM_MIXEDMAP) {
666
+ if (unlikely(vma_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
667
+ if (vma_flags & VM_MIXEDMAP) {
908668 if (!pfn_valid(pfn))
909669 return NULL;
910670 goto out;
....@@ -913,7 +673,7 @@
913673 off = (addr - vma->vm_start) >> PAGE_SHIFT;
914674 if (pfn == vma->vm_pgoff + off)
915675 return NULL;
916
- if (!is_cow_mapping(vma->vm_flags))
676
+ if (!is_cow_mapping(vma_flags))
917677 return NULL;
918678 }
919679 }
....@@ -963,7 +723,7 @@
963723
964724 if (pmd_devmap(pmd))
965725 return NULL;
966
- if (is_zero_pfn(pfn))
726
+ if (is_huge_zero_pmd(pmd))
967727 return NULL;
968728 if (unlikely(pfn > highest_memmap_pfn))
969729 return NULL;
....@@ -983,80 +743,197 @@
983743 * covered by this vma.
984744 */
985745
986
-static inline unsigned long
987
-copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
988
- pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
989
- unsigned long addr, int *rss)
746
+static unsigned long
747
+copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
748
+ pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *dst_vma,
749
+ struct vm_area_struct *src_vma, unsigned long addr, int *rss)
990750 {
991
- unsigned long vm_flags = vma->vm_flags;
751
+ unsigned long vm_flags = dst_vma->vm_flags;
752
+ pte_t pte = *src_pte;
753
+ struct page *page;
754
+ swp_entry_t entry = pte_to_swp_entry(pte);
755
+
756
+ if (likely(!non_swap_entry(entry))) {
757
+ if (swap_duplicate(entry) < 0)
758
+ return entry.val;
759
+
760
+ /* make sure dst_mm is on swapoff's mmlist. */
761
+ if (unlikely(list_empty(&dst_mm->mmlist))) {
762
+ spin_lock(&mmlist_lock);
763
+ if (list_empty(&dst_mm->mmlist))
764
+ list_add(&dst_mm->mmlist,
765
+ &src_mm->mmlist);
766
+ spin_unlock(&mmlist_lock);
767
+ }
768
+ rss[MM_SWAPENTS]++;
769
+ } else if (is_migration_entry(entry)) {
770
+ page = migration_entry_to_page(entry);
771
+
772
+ rss[mm_counter(page)]++;
773
+
774
+ if (is_write_migration_entry(entry) &&
775
+ is_cow_mapping(vm_flags)) {
776
+ /*
777
+ * COW mappings require pages in both
778
+ * parent and child to be set to read.
779
+ */
780
+ make_migration_entry_read(&entry);
781
+ pte = swp_entry_to_pte(entry);
782
+ if (pte_swp_soft_dirty(*src_pte))
783
+ pte = pte_swp_mksoft_dirty(pte);
784
+ if (pte_swp_uffd_wp(*src_pte))
785
+ pte = pte_swp_mkuffd_wp(pte);
786
+ set_pte_at(src_mm, addr, src_pte, pte);
787
+ }
788
+ } else if (is_device_private_entry(entry)) {
789
+ page = device_private_entry_to_page(entry);
790
+
791
+ /*
792
+ * Update rss count even for unaddressable pages, as
793
+ * they should treated just like normal pages in this
794
+ * respect.
795
+ *
796
+ * We will likely want to have some new rss counters
797
+ * for unaddressable pages, at some point. But for now
798
+ * keep things as they are.
799
+ */
800
+ get_page(page);
801
+ rss[mm_counter(page)]++;
802
+ page_dup_rmap(page, false);
803
+
804
+ /*
805
+ * We do not preserve soft-dirty information, because so
806
+ * far, checkpoint/restore is the only feature that
807
+ * requires that. And checkpoint/restore does not work
808
+ * when a device driver is involved (you cannot easily
809
+ * save and restore device driver state).
810
+ */
811
+ if (is_write_device_private_entry(entry) &&
812
+ is_cow_mapping(vm_flags)) {
813
+ make_device_private_entry_read(&entry);
814
+ pte = swp_entry_to_pte(entry);
815
+ if (pte_swp_uffd_wp(*src_pte))
816
+ pte = pte_swp_mkuffd_wp(pte);
817
+ set_pte_at(src_mm, addr, src_pte, pte);
818
+ }
819
+ }
820
+ if (!userfaultfd_wp(dst_vma))
821
+ pte = pte_swp_clear_uffd_wp(pte);
822
+ set_pte_at(dst_mm, addr, dst_pte, pte);
823
+ return 0;
824
+}
825
+
826
+/*
827
+ * Copy a present and normal page if necessary.
828
+ *
829
+ * NOTE! The usual case is that this doesn't need to do
830
+ * anything, and can just return a positive value. That
831
+ * will let the caller know that it can just increase
832
+ * the page refcount and re-use the pte the traditional
833
+ * way.
834
+ *
835
+ * But _if_ we need to copy it because it needs to be
836
+ * pinned in the parent (and the child should get its own
837
+ * copy rather than just a reference to the same page),
838
+ * we'll do that here and return zero to let the caller
839
+ * know we're done.
840
+ *
841
+ * And if we need a pre-allocated page but don't yet have
842
+ * one, return a negative error to let the preallocation
843
+ * code know so that it can do so outside the page table
844
+ * lock.
845
+ */
846
+static inline int
847
+copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
848
+ pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss,
849
+ struct page **prealloc, pte_t pte, struct page *page)
850
+{
851
+ struct mm_struct *src_mm = src_vma->vm_mm;
852
+ struct page *new_page;
853
+
854
+ if (!is_cow_mapping(src_vma->vm_flags))
855
+ return 1;
856
+
857
+ /*
858
+ * What we want to do is to check whether this page may
859
+ * have been pinned by the parent process. If so,
860
+ * instead of wrprotect the pte on both sides, we copy
861
+ * the page immediately so that we'll always guarantee
862
+ * the pinned page won't be randomly replaced in the
863
+ * future.
864
+ *
865
+ * The page pinning checks are just "has this mm ever
866
+ * seen pinning", along with the (inexact) check of
867
+ * the page count. That might give false positives for
868
+ * for pinning, but it will work correctly.
869
+ */
870
+ if (likely(!atomic_read(&src_mm->has_pinned)))
871
+ return 1;
872
+ if (likely(!page_maybe_dma_pinned(page)))
873
+ return 1;
874
+
875
+ /*
876
+ * The vma->anon_vma of the child process may be NULL
877
+ * because the entire vma does not contain anonymous pages.
878
+ * A BUG will occur when the copy_present_page() passes
879
+ * a copy of a non-anonymous page of that vma to the
880
+ * page_add_new_anon_rmap() to set up new anonymous rmap.
881
+ * Return 1 if the page is not an anonymous page.
882
+ */
883
+ if (!PageAnon(page))
884
+ return 1;
885
+
886
+ new_page = *prealloc;
887
+ if (!new_page)
888
+ return -EAGAIN;
889
+
890
+ /*
891
+ * We have a prealloc page, all good! Take it
892
+ * over and copy the page & arm it.
893
+ */
894
+ *prealloc = NULL;
895
+ copy_user_highpage(new_page, page, addr, src_vma);
896
+ __SetPageUptodate(new_page);
897
+ page_add_new_anon_rmap(new_page, dst_vma, addr, false);
898
+ lru_cache_add_inactive_or_unevictable(new_page, dst_vma);
899
+ rss[mm_counter(new_page)]++;
900
+
901
+ /* All done, just insert the new page copy in the child */
902
+ pte = mk_pte(new_page, dst_vma->vm_page_prot);
903
+ pte = maybe_mkwrite(pte_mkdirty(pte), dst_vma->vm_flags);
904
+ if (userfaultfd_pte_wp(dst_vma, *src_pte))
905
+ /* Uffd-wp needs to be delivered to dest pte as well */
906
+ pte = pte_wrprotect(pte_mkuffd_wp(pte));
907
+ set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte);
908
+ return 0;
909
+}
910
+
911
+/*
912
+ * Copy one pte. Returns 0 if succeeded, or -EAGAIN if one preallocated page
913
+ * is required to copy this pte.
914
+ */
915
+static inline int
916
+copy_present_pte(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
917
+ pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss,
918
+ struct page **prealloc)
919
+{
920
+ struct mm_struct *src_mm = src_vma->vm_mm;
921
+ unsigned long vm_flags = src_vma->vm_flags;
992922 pte_t pte = *src_pte;
993923 struct page *page;
994924
995
- /* pte contains position in swap or file, so copy. */
996
- if (unlikely(!pte_present(pte))) {
997
- swp_entry_t entry = pte_to_swp_entry(pte);
925
+ page = vm_normal_page(src_vma, addr, pte);
926
+ if (page) {
927
+ int retval;
998928
999
- if (likely(!non_swap_entry(entry))) {
1000
- if (swap_duplicate(entry) < 0)
1001
- return entry.val;
929
+ retval = copy_present_page(dst_vma, src_vma, dst_pte, src_pte,
930
+ addr, rss, prealloc, pte, page);
931
+ if (retval <= 0)
932
+ return retval;
1002933
1003
- /* make sure dst_mm is on swapoff's mmlist. */
1004
- if (unlikely(list_empty(&dst_mm->mmlist))) {
1005
- spin_lock(&mmlist_lock);
1006
- if (list_empty(&dst_mm->mmlist))
1007
- list_add(&dst_mm->mmlist,
1008
- &src_mm->mmlist);
1009
- spin_unlock(&mmlist_lock);
1010
- }
1011
- rss[MM_SWAPENTS]++;
1012
- } else if (is_migration_entry(entry)) {
1013
- page = migration_entry_to_page(entry);
1014
-
1015
- rss[mm_counter(page)]++;
1016
-
1017
- if (is_write_migration_entry(entry) &&
1018
- is_cow_mapping(vm_flags)) {
1019
- /*
1020
- * COW mappings require pages in both
1021
- * parent and child to be set to read.
1022
- */
1023
- make_migration_entry_read(&entry);
1024
- pte = swp_entry_to_pte(entry);
1025
- if (pte_swp_soft_dirty(*src_pte))
1026
- pte = pte_swp_mksoft_dirty(pte);
1027
- set_pte_at(src_mm, addr, src_pte, pte);
1028
- }
1029
- } else if (is_device_private_entry(entry)) {
1030
- page = device_private_entry_to_page(entry);
1031
-
1032
- /*
1033
- * Update rss count even for unaddressable pages, as
1034
- * they should treated just like normal pages in this
1035
- * respect.
1036
- *
1037
- * We will likely want to have some new rss counters
1038
- * for unaddressable pages, at some point. But for now
1039
- * keep things as they are.
1040
- */
1041
- get_page(page);
1042
- rss[mm_counter(page)]++;
1043
- page_dup_rmap(page, false);
1044
-
1045
- /*
1046
- * We do not preserve soft-dirty information, because so
1047
- * far, checkpoint/restore is the only feature that
1048
- * requires that. And checkpoint/restore does not work
1049
- * when a device driver is involved (you cannot easily
1050
- * save and restore device driver state).
1051
- */
1052
- if (is_write_device_private_entry(entry) &&
1053
- is_cow_mapping(vm_flags)) {
1054
- make_device_private_entry_read(&entry);
1055
- pte = swp_entry_to_pte(entry);
1056
- set_pte_at(src_mm, addr, src_pte, pte);
1057
- }
1058
- }
1059
- goto out_set_pte;
934
+ get_page(page);
935
+ page_dup_rmap(page, false);
936
+ rss[mm_counter(page)]++;
1060937 }
1061938
1062939 /*
....@@ -1076,48 +953,56 @@
1076953 pte = pte_mkclean(pte);
1077954 pte = pte_mkold(pte);
1078955
1079
- page = vm_normal_page(vma, addr, pte);
1080
- if (page) {
1081
- get_page(page);
1082
- page_dup_rmap(page, false);
1083
- rss[mm_counter(page)]++;
1084
- } else if (pte_devmap(pte)) {
1085
- page = pte_page(pte);
956
+ if (!userfaultfd_wp(dst_vma))
957
+ pte = pte_clear_uffd_wp(pte);
1086958
1087
- /*
1088
- * Cache coherent device memory behave like regular page and
1089
- * not like persistent memory page. For more informations see
1090
- * MEMORY_DEVICE_CACHE_COHERENT in memory_hotplug.h
1091
- */
1092
- if (is_device_public_page(page)) {
1093
- get_page(page);
1094
- page_dup_rmap(page, false);
1095
- rss[mm_counter(page)]++;
1096
- }
1097
- }
1098
-
1099
-out_set_pte:
1100
- set_pte_at(dst_mm, addr, dst_pte, pte);
959
+ set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte);
1101960 return 0;
1102961 }
1103962
1104
-static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1105
- pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
1106
- unsigned long addr, unsigned long end)
963
+static inline struct page *
964
+page_copy_prealloc(struct mm_struct *src_mm, struct vm_area_struct *vma,
965
+ unsigned long addr)
1107966 {
967
+ struct page *new_page;
968
+
969
+ new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, addr);
970
+ if (!new_page)
971
+ return NULL;
972
+
973
+ if (mem_cgroup_charge(new_page, src_mm, GFP_KERNEL)) {
974
+ put_page(new_page);
975
+ return NULL;
976
+ }
977
+ cgroup_throttle_swaprate(new_page, GFP_KERNEL);
978
+
979
+ return new_page;
980
+}
981
+
982
+static int
983
+copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
984
+ pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
985
+ unsigned long end)
986
+{
987
+ struct mm_struct *dst_mm = dst_vma->vm_mm;
988
+ struct mm_struct *src_mm = src_vma->vm_mm;
1108989 pte_t *orig_src_pte, *orig_dst_pte;
1109990 pte_t *src_pte, *dst_pte;
1110991 spinlock_t *src_ptl, *dst_ptl;
1111
- int progress = 0;
992
+ int progress, ret = 0;
1112993 int rss[NR_MM_COUNTERS];
1113994 swp_entry_t entry = (swp_entry_t){0};
995
+ struct page *prealloc = NULL;
1114996
1115997 again:
998
+ progress = 0;
1116999 init_rss_vec(rss);
11171000
11181001 dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
1119
- if (!dst_pte)
1120
- return -ENOMEM;
1002
+ if (!dst_pte) {
1003
+ ret = -ENOMEM;
1004
+ goto out;
1005
+ }
11211006 src_pte = pte_offset_map(src_pmd, addr);
11221007 src_ptl = pte_lockptr(src_mm, src_pmd);
11231008 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
....@@ -1140,10 +1025,35 @@
11401025 progress++;
11411026 continue;
11421027 }
1143
- entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
1144
- vma, addr, rss);
1145
- if (entry.val)
1028
+ if (unlikely(!pte_present(*src_pte))) {
1029
+ entry.val = copy_nonpresent_pte(dst_mm, src_mm,
1030
+ dst_pte, src_pte,
1031
+ dst_vma, src_vma,
1032
+ addr, rss);
1033
+ if (entry.val)
1034
+ break;
1035
+ progress += 8;
1036
+ continue;
1037
+ }
1038
+ /* copy_present_pte() will clear `*prealloc' if consumed */
1039
+ ret = copy_present_pte(dst_vma, src_vma, dst_pte, src_pte,
1040
+ addr, rss, &prealloc);
1041
+ /*
1042
+ * If we need a pre-allocated page for this pte, drop the
1043
+ * locks, allocate, and try again.
1044
+ */
1045
+ if (unlikely(ret == -EAGAIN))
11461046 break;
1047
+ if (unlikely(prealloc)) {
1048
+ /*
1049
+ * pre-alloc page cannot be reused by next time so as
1050
+ * to strictly follow mempolicy (e.g., alloc_page_vma()
1051
+ * will allocate page according to address). This
1052
+ * could only happen if one pinned pte changed.
1053
+ */
1054
+ put_page(prealloc);
1055
+ prealloc = NULL;
1056
+ }
11471057 progress += 8;
11481058 } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
11491059
....@@ -1155,19 +1065,34 @@
11551065 cond_resched();
11561066
11571067 if (entry.val) {
1158
- if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
1068
+ if (add_swap_count_continuation(entry, GFP_KERNEL) < 0) {
1069
+ ret = -ENOMEM;
1070
+ goto out;
1071
+ }
1072
+ entry.val = 0;
1073
+ } else if (ret) {
1074
+ WARN_ON_ONCE(ret != -EAGAIN);
1075
+ prealloc = page_copy_prealloc(src_mm, src_vma, addr);
1076
+ if (!prealloc)
11591077 return -ENOMEM;
1160
- progress = 0;
1078
+ /* We've captured and resolved the error. Reset, try again. */
1079
+ ret = 0;
11611080 }
11621081 if (addr != end)
11631082 goto again;
1164
- return 0;
1083
+out:
1084
+ if (unlikely(prealloc))
1085
+ put_page(prealloc);
1086
+ return ret;
11651087 }
11661088
1167
-static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1168
- pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
1169
- unsigned long addr, unsigned long end)
1089
+static inline int
1090
+copy_pmd_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
1091
+ pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
1092
+ unsigned long end)
11701093 {
1094
+ struct mm_struct *dst_mm = dst_vma->vm_mm;
1095
+ struct mm_struct *src_mm = src_vma->vm_mm;
11711096 pmd_t *src_pmd, *dst_pmd;
11721097 unsigned long next;
11731098
....@@ -1180,9 +1105,9 @@
11801105 if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd)
11811106 || pmd_devmap(*src_pmd)) {
11821107 int err;
1183
- VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, vma);
1184
- err = copy_huge_pmd(dst_mm, src_mm,
1185
- dst_pmd, src_pmd, addr, vma);
1108
+ VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, src_vma);
1109
+ err = copy_huge_pmd(dst_mm, src_mm, dst_pmd, src_pmd,
1110
+ addr, dst_vma, src_vma);
11861111 if (err == -ENOMEM)
11871112 return -ENOMEM;
11881113 if (!err)
....@@ -1191,17 +1116,20 @@
11911116 }
11921117 if (pmd_none_or_clear_bad(src_pmd))
11931118 continue;
1194
- if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
1195
- vma, addr, next))
1119
+ if (copy_pte_range(dst_vma, src_vma, dst_pmd, src_pmd,
1120
+ addr, next))
11961121 return -ENOMEM;
11971122 } while (dst_pmd++, src_pmd++, addr = next, addr != end);
11981123 return 0;
11991124 }
12001125
1201
-static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1202
- p4d_t *dst_p4d, p4d_t *src_p4d, struct vm_area_struct *vma,
1203
- unsigned long addr, unsigned long end)
1126
+static inline int
1127
+copy_pud_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
1128
+ p4d_t *dst_p4d, p4d_t *src_p4d, unsigned long addr,
1129
+ unsigned long end)
12041130 {
1131
+ struct mm_struct *dst_mm = dst_vma->vm_mm;
1132
+ struct mm_struct *src_mm = src_vma->vm_mm;
12051133 pud_t *src_pud, *dst_pud;
12061134 unsigned long next;
12071135
....@@ -1214,9 +1142,9 @@
12141142 if (pud_trans_huge(*src_pud) || pud_devmap(*src_pud)) {
12151143 int err;
12161144
1217
- VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, vma);
1145
+ VM_BUG_ON_VMA(next-addr != HPAGE_PUD_SIZE, src_vma);
12181146 err = copy_huge_pud(dst_mm, src_mm,
1219
- dst_pud, src_pud, addr, vma);
1147
+ dst_pud, src_pud, addr, src_vma);
12201148 if (err == -ENOMEM)
12211149 return -ENOMEM;
12221150 if (!err)
....@@ -1225,17 +1153,19 @@
12251153 }
12261154 if (pud_none_or_clear_bad(src_pud))
12271155 continue;
1228
- if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
1229
- vma, addr, next))
1156
+ if (copy_pmd_range(dst_vma, src_vma, dst_pud, src_pud,
1157
+ addr, next))
12301158 return -ENOMEM;
12311159 } while (dst_pud++, src_pud++, addr = next, addr != end);
12321160 return 0;
12331161 }
12341162
1235
-static inline int copy_p4d_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1236
- pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
1237
- unsigned long addr, unsigned long end)
1163
+static inline int
1164
+copy_p4d_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
1165
+ pgd_t *dst_pgd, pgd_t *src_pgd, unsigned long addr,
1166
+ unsigned long end)
12381167 {
1168
+ struct mm_struct *dst_mm = dst_vma->vm_mm;
12391169 p4d_t *src_p4d, *dst_p4d;
12401170 unsigned long next;
12411171
....@@ -1247,22 +1177,23 @@
12471177 next = p4d_addr_end(addr, end);
12481178 if (p4d_none_or_clear_bad(src_p4d))
12491179 continue;
1250
- if (copy_pud_range(dst_mm, src_mm, dst_p4d, src_p4d,
1251
- vma, addr, next))
1180
+ if (copy_pud_range(dst_vma, src_vma, dst_p4d, src_p4d,
1181
+ addr, next))
12521182 return -ENOMEM;
12531183 } while (dst_p4d++, src_p4d++, addr = next, addr != end);
12541184 return 0;
12551185 }
12561186
1257
-int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1258
- struct vm_area_struct *vma)
1187
+int
1188
+copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)
12591189 {
12601190 pgd_t *src_pgd, *dst_pgd;
12611191 unsigned long next;
1262
- unsigned long addr = vma->vm_start;
1263
- unsigned long end = vma->vm_end;
1264
- unsigned long mmun_start; /* For mmu_notifiers */
1265
- unsigned long mmun_end; /* For mmu_notifiers */
1192
+ unsigned long addr = src_vma->vm_start;
1193
+ unsigned long end = src_vma->vm_end;
1194
+ struct mm_struct *dst_mm = dst_vma->vm_mm;
1195
+ struct mm_struct *src_mm = src_vma->vm_mm;
1196
+ struct mmu_notifier_range range;
12661197 bool is_cow;
12671198 int ret;
12681199
....@@ -1272,19 +1203,19 @@
12721203 * readonly mappings. The tradeoff is that copy_page_range is more
12731204 * efficient than faulting.
12741205 */
1275
- if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) &&
1276
- !vma->anon_vma)
1206
+ if (!(src_vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) &&
1207
+ !src_vma->anon_vma)
12771208 return 0;
12781209
1279
- if (is_vm_hugetlb_page(vma))
1280
- return copy_hugetlb_page_range(dst_mm, src_mm, vma);
1210
+ if (is_vm_hugetlb_page(src_vma))
1211
+ return copy_hugetlb_page_range(dst_mm, src_mm, src_vma);
12811212
1282
- if (unlikely(vma->vm_flags & VM_PFNMAP)) {
1213
+ if (unlikely(src_vma->vm_flags & VM_PFNMAP)) {
12831214 /*
12841215 * We do not free on error cases below as remove_vma
12851216 * gets called on error from higher level routine
12861217 */
1287
- ret = track_pfn_copy(vma);
1218
+ ret = track_pfn_copy(src_vma);
12881219 if (ret)
12891220 return ret;
12901221 }
....@@ -1295,12 +1226,22 @@
12951226 * parent mm. And a permission downgrade will only happen if
12961227 * is_cow_mapping() returns true.
12971228 */
1298
- is_cow = is_cow_mapping(vma->vm_flags);
1299
- mmun_start = addr;
1300
- mmun_end = end;
1301
- if (is_cow)
1302
- mmu_notifier_invalidate_range_start(src_mm, mmun_start,
1303
- mmun_end);
1229
+ is_cow = is_cow_mapping(src_vma->vm_flags);
1230
+
1231
+ if (is_cow) {
1232
+ mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE,
1233
+ 0, src_vma, src_mm, addr, end);
1234
+ mmu_notifier_invalidate_range_start(&range);
1235
+ /*
1236
+ * Disabling preemption is not needed for the write side, as
1237
+ * the read side doesn't spin, but goes to the mmap_lock.
1238
+ *
1239
+ * Use the raw variant of the seqcount_t write API to avoid
1240
+ * lockdep complaining about preemptibility.
1241
+ */
1242
+ mmap_assert_write_locked(src_mm);
1243
+ raw_write_seqcount_begin(&src_mm->write_protect_seq);
1244
+ }
13041245
13051246 ret = 0;
13061247 dst_pgd = pgd_offset(dst_mm, addr);
....@@ -1309,16 +1250,29 @@
13091250 next = pgd_addr_end(addr, end);
13101251 if (pgd_none_or_clear_bad(src_pgd))
13111252 continue;
1312
- if (unlikely(copy_p4d_range(dst_mm, src_mm, dst_pgd, src_pgd,
1313
- vma, addr, next))) {
1253
+ if (unlikely(copy_p4d_range(dst_vma, src_vma, dst_pgd, src_pgd,
1254
+ addr, next))) {
13141255 ret = -ENOMEM;
13151256 break;
13161257 }
13171258 } while (dst_pgd++, src_pgd++, addr = next, addr != end);
13181259
1319
- if (is_cow)
1320
- mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end);
1260
+ if (is_cow) {
1261
+ raw_write_seqcount_end(&src_mm->write_protect_seq);
1262
+ mmu_notifier_invalidate_range_end(&range);
1263
+ }
13211264 return ret;
1265
+}
1266
+
1267
+/* Whether we should zap all COWed (private) pages too */
1268
+static inline bool should_zap_cows(struct zap_details *details)
1269
+{
1270
+ /* By default, zap all pages */
1271
+ if (!details)
1272
+ return true;
1273
+
1274
+ /* Or, we zap COWed pages only if the caller wants to */
1275
+ return !details->check_mapping;
13221276 }
13231277
13241278 static unsigned long zap_pte_range(struct mmu_gather *tlb,
....@@ -1334,7 +1288,7 @@
13341288 pte_t *pte;
13351289 swp_entry_t entry;
13361290
1337
- tlb_remove_check_page_size_change(tlb, PAGE_SIZE);
1291
+ tlb_change_page_size(tlb, PAGE_SIZE);
13381292 again:
13391293 init_rss_vec(rss);
13401294 start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
....@@ -1346,10 +1300,13 @@
13461300 if (pte_none(ptent))
13471301 continue;
13481302
1303
+ if (need_resched())
1304
+ break;
1305
+
13491306 if (pte_present(ptent)) {
13501307 struct page *page;
13511308
1352
- page = _vm_normal_page(vma, addr, ptent, true);
1309
+ page = vm_normal_page(vma, addr, ptent);
13531310 if (unlikely(details) && page) {
13541311 /*
13551312 * unmap_shared_mapping_pages() wants to
....@@ -1379,7 +1336,8 @@
13791336 page_remove_rmap(page, false);
13801337 if (unlikely(page_mapcount(page) < 0))
13811338 print_bad_pte(vma, addr, ptent, page);
1382
- if (unlikely(__tlb_remove_page(tlb, page))) {
1339
+ if (unlikely(__tlb_remove_page(tlb, page)) ||
1340
+ lru_cache_disabled()) {
13831341 force_flush = 1;
13841342 addr += PAGE_SIZE;
13851343 break;
....@@ -1388,7 +1346,7 @@
13881346 }
13891347
13901348 entry = pte_to_swp_entry(ptent);
1391
- if (non_swap_entry(entry) && is_device_private_entry(entry)) {
1349
+ if (is_device_private_entry(entry)) {
13921350 struct page *page = device_private_entry_to_page(entry);
13931351
13941352 if (unlikely(details && details->check_mapping)) {
....@@ -1409,17 +1367,18 @@
14091367 continue;
14101368 }
14111369
1412
- /* If details->check_mapping, we leave swap entries. */
1413
- if (unlikely(details))
1414
- continue;
1415
-
1416
- entry = pte_to_swp_entry(ptent);
1417
- if (!non_swap_entry(entry))
1370
+ if (!non_swap_entry(entry)) {
1371
+ /* Genuine swap entry, hence a private anon page */
1372
+ if (!should_zap_cows(details))
1373
+ continue;
14181374 rss[MM_SWAPENTS]--;
1419
- else if (is_migration_entry(entry)) {
1375
+ } else if (is_migration_entry(entry)) {
14201376 struct page *page;
14211377
14221378 page = migration_entry_to_page(entry);
1379
+ if (details && details->check_mapping &&
1380
+ details->check_mapping != page_rmapping(page))
1381
+ continue;
14231382 rss[mm_counter(page)]--;
14241383 }
14251384 if (unlikely(!free_swap_and_cache(entry)))
....@@ -1443,9 +1402,12 @@
14431402 */
14441403 if (force_flush) {
14451404 force_flush = 0;
1446
- tlb_flush_mmu_free(tlb);
1447
- if (addr != end)
1448
- goto again;
1405
+ tlb_flush_mmu(tlb);
1406
+ }
1407
+
1408
+ if (addr != end) {
1409
+ cond_resched();
1410
+ goto again;
14491411 }
14501412
14511413 return addr;
....@@ -1484,7 +1446,7 @@
14841446 * Here there can be other concurrent MADV_DONTNEED or
14851447 * trans huge page faults running, and if the pmd is
14861448 * none or trans huge it can change under us. This is
1487
- * because MADV_DONTNEED holds the mmap_sem in read
1449
+ * because MADV_DONTNEED holds the mmap_lock in read
14881450 * mode.
14891451 */
14901452 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
....@@ -1510,7 +1472,7 @@
15101472 next = pud_addr_end(addr, end);
15111473 if (pud_trans_huge(*pud) || pud_devmap(*pud)) {
15121474 if (next - addr != HPAGE_PUD_SIZE) {
1513
- VM_BUG_ON_VMA(!rwsem_is_locked(&tlb->mm->mmap_sem), vma);
1475
+ mmap_assert_locked(tlb->mm);
15141476 split_huge_pud(vma, pud, addr);
15151477 } else if (zap_huge_pud(tlb, vma, pud, addr))
15161478 goto next;
....@@ -1631,12 +1593,14 @@
16311593 struct vm_area_struct *vma, unsigned long start_addr,
16321594 unsigned long end_addr)
16331595 {
1634
- struct mm_struct *mm = vma->vm_mm;
1596
+ struct mmu_notifier_range range;
16351597
1636
- mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
1598
+ mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, vma->vm_mm,
1599
+ start_addr, end_addr);
1600
+ mmu_notifier_invalidate_range_start(&range);
16371601 for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
16381602 unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
1639
- mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
1603
+ mmu_notifier_invalidate_range_end(&range);
16401604 }
16411605
16421606 /**
....@@ -1650,18 +1614,19 @@
16501614 void zap_page_range(struct vm_area_struct *vma, unsigned long start,
16511615 unsigned long size)
16521616 {
1653
- struct mm_struct *mm = vma->vm_mm;
1617
+ struct mmu_notifier_range range;
16541618 struct mmu_gather tlb;
1655
- unsigned long end = start + size;
16561619
16571620 lru_add_drain();
1658
- tlb_gather_mmu(&tlb, mm, start, end);
1659
- update_hiwater_rss(mm);
1660
- mmu_notifier_invalidate_range_start(mm, start, end);
1661
- for ( ; vma && vma->vm_start < end; vma = vma->vm_next)
1662
- unmap_single_vma(&tlb, vma, start, end, NULL);
1663
- mmu_notifier_invalidate_range_end(mm, start, end);
1664
- tlb_finish_mmu(&tlb, start, end);
1621
+ mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
1622
+ start, start + size);
1623
+ tlb_gather_mmu(&tlb, vma->vm_mm, start, range.end);
1624
+ update_hiwater_rss(vma->vm_mm);
1625
+ mmu_notifier_invalidate_range_start(&range);
1626
+ for ( ; vma && vma->vm_start < range.end; vma = vma->vm_next)
1627
+ unmap_single_vma(&tlb, vma, start, range.end, NULL);
1628
+ mmu_notifier_invalidate_range_end(&range);
1629
+ tlb_finish_mmu(&tlb, start, range.end);
16651630 }
16661631
16671632 /**
....@@ -1676,17 +1641,18 @@
16761641 static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
16771642 unsigned long size, struct zap_details *details)
16781643 {
1679
- struct mm_struct *mm = vma->vm_mm;
1644
+ struct mmu_notifier_range range;
16801645 struct mmu_gather tlb;
1681
- unsigned long end = address + size;
16821646
16831647 lru_add_drain();
1684
- tlb_gather_mmu(&tlb, mm, address, end);
1685
- update_hiwater_rss(mm);
1686
- mmu_notifier_invalidate_range_start(mm, address, end);
1687
- unmap_single_vma(&tlb, vma, address, end, details);
1688
- mmu_notifier_invalidate_range_end(mm, address, end);
1689
- tlb_finish_mmu(&tlb, address, end);
1648
+ mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
1649
+ address, address + size);
1650
+ tlb_gather_mmu(&tlb, vma->vm_mm, address, range.end);
1651
+ update_hiwater_rss(vma->vm_mm);
1652
+ mmu_notifier_invalidate_range_start(&range);
1653
+ unmap_single_vma(&tlb, vma, address, range.end, details);
1654
+ mmu_notifier_invalidate_range_end(&range);
1655
+ tlb_finish_mmu(&tlb, address, range.end);
16901656 }
16911657
16921658 /**
....@@ -1711,8 +1677,7 @@
17111677 }
17121678 EXPORT_SYMBOL_GPL(zap_vma_ptes);
17131679
1714
-pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
1715
- spinlock_t **ptl)
1680
+static pmd_t *walk_to_pmd(struct mm_struct *mm, unsigned long addr)
17161681 {
17171682 pgd_t *pgd;
17181683 p4d_t *p4d;
....@@ -1731,7 +1696,38 @@
17311696 return NULL;
17321697
17331698 VM_BUG_ON(pmd_trans_huge(*pmd));
1699
+ return pmd;
1700
+}
1701
+
1702
+pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
1703
+ spinlock_t **ptl)
1704
+{
1705
+ pmd_t *pmd = walk_to_pmd(mm, addr);
1706
+
1707
+ if (!pmd)
1708
+ return NULL;
17341709 return pte_alloc_map_lock(mm, pmd, addr, ptl);
1710
+}
1711
+
1712
+static int validate_page_before_insert(struct page *page)
1713
+{
1714
+ if (PageAnon(page) || PageSlab(page) || page_has_type(page))
1715
+ return -EINVAL;
1716
+ flush_dcache_page(page);
1717
+ return 0;
1718
+}
1719
+
1720
+static int insert_page_into_pte_locked(struct mm_struct *mm, pte_t *pte,
1721
+ unsigned long addr, struct page *page, pgprot_t prot)
1722
+{
1723
+ if (!pte_none(*pte))
1724
+ return -EBUSY;
1725
+ /* Ok, finally just insert the thing.. */
1726
+ get_page(page);
1727
+ inc_mm_counter_fast(mm, mm_counter_file(page));
1728
+ page_add_file_rmap(page, false);
1729
+ set_pte_at(mm, addr, pte, mk_pte(page, prot));
1730
+ return 0;
17351731 }
17361732
17371733 /*
....@@ -1749,32 +1745,135 @@
17491745 pte_t *pte;
17501746 spinlock_t *ptl;
17511747
1752
- retval = -EINVAL;
1753
- if (PageAnon(page))
1748
+ retval = validate_page_before_insert(page);
1749
+ if (retval)
17541750 goto out;
17551751 retval = -ENOMEM;
1756
- flush_dcache_page(page);
17571752 pte = get_locked_pte(mm, addr, &ptl);
17581753 if (!pte)
17591754 goto out;
1760
- retval = -EBUSY;
1761
- if (!pte_none(*pte))
1762
- goto out_unlock;
1763
-
1764
- /* Ok, finally just insert the thing.. */
1765
- get_page(page);
1766
- inc_mm_counter_fast(mm, mm_counter_file(page));
1767
- page_add_file_rmap(page, false);
1768
- set_pte_at(mm, addr, pte, mk_pte(page, prot));
1769
-
1770
- retval = 0;
1771
- pte_unmap_unlock(pte, ptl);
1772
- return retval;
1773
-out_unlock:
1755
+ retval = insert_page_into_pte_locked(mm, pte, addr, page, prot);
17741756 pte_unmap_unlock(pte, ptl);
17751757 out:
17761758 return retval;
17771759 }
1760
+
1761
+#ifdef pte_index
1762
+static int insert_page_in_batch_locked(struct mm_struct *mm, pte_t *pte,
1763
+ unsigned long addr, struct page *page, pgprot_t prot)
1764
+{
1765
+ int err;
1766
+
1767
+ if (!page_count(page))
1768
+ return -EINVAL;
1769
+ err = validate_page_before_insert(page);
1770
+ if (err)
1771
+ return err;
1772
+ return insert_page_into_pte_locked(mm, pte, addr, page, prot);
1773
+}
1774
+
1775
+/* insert_pages() amortizes the cost of spinlock operations
1776
+ * when inserting pages in a loop. Arch *must* define pte_index.
1777
+ */
1778
+static int insert_pages(struct vm_area_struct *vma, unsigned long addr,
1779
+ struct page **pages, unsigned long *num, pgprot_t prot)
1780
+{
1781
+ pmd_t *pmd = NULL;
1782
+ pte_t *start_pte, *pte;
1783
+ spinlock_t *pte_lock;
1784
+ struct mm_struct *const mm = vma->vm_mm;
1785
+ unsigned long curr_page_idx = 0;
1786
+ unsigned long remaining_pages_total = *num;
1787
+ unsigned long pages_to_write_in_pmd;
1788
+ int ret;
1789
+more:
1790
+ ret = -EFAULT;
1791
+ pmd = walk_to_pmd(mm, addr);
1792
+ if (!pmd)
1793
+ goto out;
1794
+
1795
+ pages_to_write_in_pmd = min_t(unsigned long,
1796
+ remaining_pages_total, PTRS_PER_PTE - pte_index(addr));
1797
+
1798
+ /* Allocate the PTE if necessary; takes PMD lock once only. */
1799
+ ret = -ENOMEM;
1800
+ if (pte_alloc(mm, pmd))
1801
+ goto out;
1802
+
1803
+ while (pages_to_write_in_pmd) {
1804
+ int pte_idx = 0;
1805
+ const int batch_size = min_t(int, pages_to_write_in_pmd, 8);
1806
+
1807
+ start_pte = pte_offset_map_lock(mm, pmd, addr, &pte_lock);
1808
+ for (pte = start_pte; pte_idx < batch_size; ++pte, ++pte_idx) {
1809
+ int err = insert_page_in_batch_locked(mm, pte,
1810
+ addr, pages[curr_page_idx], prot);
1811
+ if (unlikely(err)) {
1812
+ pte_unmap_unlock(start_pte, pte_lock);
1813
+ ret = err;
1814
+ remaining_pages_total -= pte_idx;
1815
+ goto out;
1816
+ }
1817
+ addr += PAGE_SIZE;
1818
+ ++curr_page_idx;
1819
+ }
1820
+ pte_unmap_unlock(start_pte, pte_lock);
1821
+ pages_to_write_in_pmd -= batch_size;
1822
+ remaining_pages_total -= batch_size;
1823
+ }
1824
+ if (remaining_pages_total)
1825
+ goto more;
1826
+ ret = 0;
1827
+out:
1828
+ *num = remaining_pages_total;
1829
+ return ret;
1830
+}
1831
+#endif /* ifdef pte_index */
1832
+
1833
+/**
1834
+ * vm_insert_pages - insert multiple pages into user vma, batching the pmd lock.
1835
+ * @vma: user vma to map to
1836
+ * @addr: target start user address of these pages
1837
+ * @pages: source kernel pages
1838
+ * @num: in: number of pages to map. out: number of pages that were *not*
1839
+ * mapped. (0 means all pages were successfully mapped).
1840
+ *
1841
+ * Preferred over vm_insert_page() when inserting multiple pages.
1842
+ *
1843
+ * In case of error, we may have mapped a subset of the provided
1844
+ * pages. It is the caller's responsibility to account for this case.
1845
+ *
1846
+ * The same restrictions apply as in vm_insert_page().
1847
+ */
1848
+int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr,
1849
+ struct page **pages, unsigned long *num)
1850
+{
1851
+#ifdef pte_index
1852
+ const unsigned long end_addr = addr + (*num * PAGE_SIZE) - 1;
1853
+
1854
+ if (addr < vma->vm_start || end_addr >= vma->vm_end)
1855
+ return -EFAULT;
1856
+ if (!(vma->vm_flags & VM_MIXEDMAP)) {
1857
+ BUG_ON(mmap_read_trylock(vma->vm_mm));
1858
+ BUG_ON(vma->vm_flags & VM_PFNMAP);
1859
+ vma->vm_flags |= VM_MIXEDMAP;
1860
+ }
1861
+ /* Defer page refcount checking till we're about to map that page. */
1862
+ return insert_pages(vma, addr, pages, num, vma->vm_page_prot);
1863
+#else
1864
+ unsigned long idx = 0, pgcount = *num;
1865
+ int err = -EINVAL;
1866
+
1867
+ for (; idx < pgcount; ++idx) {
1868
+ err = vm_insert_page(vma, addr + (PAGE_SIZE * idx), pages[idx]);
1869
+ if (err)
1870
+ break;
1871
+ }
1872
+ *num = pgcount - idx;
1873
+ return err;
1874
+#endif /* ifdef pte_index */
1875
+}
1876
+EXPORT_SYMBOL(vm_insert_pages);
17781877
17791878 /**
17801879 * vm_insert_page - insert single page into user vma
....@@ -1799,9 +1898,11 @@
17991898 * The page does not need to be reserved.
18001899 *
18011900 * Usually this function is called from f_op->mmap() handler
1802
- * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
1901
+ * under mm->mmap_lock write-lock, so it can change vma->vm_flags.
18031902 * Caller must set VM_MIXEDMAP on vma if it wants to call this
18041903 * function from other places, for example from page-fault handler.
1904
+ *
1905
+ * Return: %0 on success, negative error code otherwise.
18051906 */
18061907 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
18071908 struct page *page)
....@@ -1811,7 +1912,7 @@
18111912 if (!page_count(page))
18121913 return -EINVAL;
18131914 if (!(vma->vm_flags & VM_MIXEDMAP)) {
1814
- BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem));
1915
+ BUG_ON(mmap_read_trylock(vma->vm_mm));
18151916 BUG_ON(vma->vm_flags & VM_PFNMAP);
18161917 vma->vm_flags |= VM_MIXEDMAP;
18171918 }
....@@ -1819,19 +1920,97 @@
18191920 }
18201921 EXPORT_SYMBOL(vm_insert_page);
18211922
1822
-static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
1923
+/*
1924
+ * __vm_map_pages - maps range of kernel pages into user vma
1925
+ * @vma: user vma to map to
1926
+ * @pages: pointer to array of source kernel pages
1927
+ * @num: number of pages in page array
1928
+ * @offset: user's requested vm_pgoff
1929
+ *
1930
+ * This allows drivers to map range of kernel pages into a user vma.
1931
+ *
1932
+ * Return: 0 on success and error code otherwise.
1933
+ */
1934
+static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages,
1935
+ unsigned long num, unsigned long offset)
1936
+{
1937
+ unsigned long count = vma_pages(vma);
1938
+ unsigned long uaddr = vma->vm_start;
1939
+ int ret, i;
1940
+
1941
+ /* Fail if the user requested offset is beyond the end of the object */
1942
+ if (offset >= num)
1943
+ return -ENXIO;
1944
+
1945
+ /* Fail if the user requested size exceeds available object size */
1946
+ if (count > num - offset)
1947
+ return -ENXIO;
1948
+
1949
+ for (i = 0; i < count; i++) {
1950
+ ret = vm_insert_page(vma, uaddr, pages[offset + i]);
1951
+ if (ret < 0)
1952
+ return ret;
1953
+ uaddr += PAGE_SIZE;
1954
+ }
1955
+
1956
+ return 0;
1957
+}
1958
+
1959
+/**
1960
+ * vm_map_pages - maps range of kernel pages starts with non zero offset
1961
+ * @vma: user vma to map to
1962
+ * @pages: pointer to array of source kernel pages
1963
+ * @num: number of pages in page array
1964
+ *
1965
+ * Maps an object consisting of @num pages, catering for the user's
1966
+ * requested vm_pgoff
1967
+ *
1968
+ * If we fail to insert any page into the vma, the function will return
1969
+ * immediately leaving any previously inserted pages present. Callers
1970
+ * from the mmap handler may immediately return the error as their caller
1971
+ * will destroy the vma, removing any successfully inserted pages. Other
1972
+ * callers should make their own arrangements for calling unmap_region().
1973
+ *
1974
+ * Context: Process context. Called by mmap handlers.
1975
+ * Return: 0 on success and error code otherwise.
1976
+ */
1977
+int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
1978
+ unsigned long num)
1979
+{
1980
+ return __vm_map_pages(vma, pages, num, vma->vm_pgoff);
1981
+}
1982
+EXPORT_SYMBOL(vm_map_pages);
1983
+
1984
+/**
1985
+ * vm_map_pages_zero - map range of kernel pages starts with zero offset
1986
+ * @vma: user vma to map to
1987
+ * @pages: pointer to array of source kernel pages
1988
+ * @num: number of pages in page array
1989
+ *
1990
+ * Similar to vm_map_pages(), except that it explicitly sets the offset
1991
+ * to 0. This function is intended for the drivers that did not consider
1992
+ * vm_pgoff.
1993
+ *
1994
+ * Context: Process context. Called by mmap handlers.
1995
+ * Return: 0 on success and error code otherwise.
1996
+ */
1997
+int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
1998
+ unsigned long num)
1999
+{
2000
+ return __vm_map_pages(vma, pages, num, 0);
2001
+}
2002
+EXPORT_SYMBOL(vm_map_pages_zero);
2003
+
2004
+static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr,
18232005 pfn_t pfn, pgprot_t prot, bool mkwrite)
18242006 {
18252007 struct mm_struct *mm = vma->vm_mm;
1826
- int retval;
18272008 pte_t *pte, entry;
18282009 spinlock_t *ptl;
18292010
1830
- retval = -ENOMEM;
18312011 pte = get_locked_pte(mm, addr, &ptl);
18322012 if (!pte)
1833
- goto out;
1834
- retval = -EBUSY;
2013
+ return VM_FAULT_OOM;
18352014 if (!pte_none(*pte)) {
18362015 if (mkwrite) {
18372016 /*
....@@ -1849,7 +2028,8 @@
18492028 goto out_unlock;
18502029 }
18512030 entry = pte_mkyoung(*pte);
1852
- entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2031
+ entry = maybe_mkwrite(pte_mkdirty(entry),
2032
+ vma->vm_flags);
18532033 if (ptep_set_access_flags(vma, addr, pte, entry, 1))
18542034 update_mmu_cache(vma, addr, pte);
18552035 }
....@@ -1864,62 +2044,41 @@
18642044
18652045 if (mkwrite) {
18662046 entry = pte_mkyoung(entry);
1867
- entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2047
+ entry = maybe_mkwrite(pte_mkdirty(entry), vma->vm_flags);
18682048 }
18692049
18702050 set_pte_at(mm, addr, pte, entry);
18712051 update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
18722052
1873
- retval = 0;
18742053 out_unlock:
18752054 pte_unmap_unlock(pte, ptl);
1876
-out:
1877
- return retval;
2055
+ return VM_FAULT_NOPAGE;
18782056 }
18792057
18802058 /**
1881
- * vm_insert_pfn - insert single pfn into user vma
1882
- * @vma: user vma to map to
1883
- * @addr: target user address of this page
1884
- * @pfn: source kernel pfn
1885
- *
1886
- * Similar to vm_insert_page, this allows drivers to insert individual pages
1887
- * they've allocated into a user vma. Same comments apply.
1888
- *
1889
- * This function should only be called from a vm_ops->fault handler, and
1890
- * in that case the handler should return NULL.
1891
- *
1892
- * vma cannot be a COW mapping.
1893
- *
1894
- * As this is called only for pages that do not currently exist, we
1895
- * do not need to flush old virtual caches or the TLB.
1896
- */
1897
-int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
1898
- unsigned long pfn)
1899
-{
1900
- return vm_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot);
1901
-}
1902
-EXPORT_SYMBOL(vm_insert_pfn);
1903
-
1904
-/**
1905
- * vm_insert_pfn_prot - insert single pfn into user vma with specified pgprot
2059
+ * vmf_insert_pfn_prot - insert single pfn into user vma with specified pgprot
19062060 * @vma: user vma to map to
19072061 * @addr: target user address of this page
19082062 * @pfn: source kernel pfn
19092063 * @pgprot: pgprot flags for the inserted page
19102064 *
1911
- * This is exactly like vm_insert_pfn, except that it allows drivers to
2065
+ * This is exactly like vmf_insert_pfn(), except that it allows drivers
19122066 * to override pgprot on a per-page basis.
19132067 *
19142068 * This only makes sense for IO mappings, and it makes no sense for
1915
- * cow mappings. In general, using multiple vmas is preferable;
1916
- * vm_insert_pfn_prot should only be used if using multiple VMAs is
2069
+ * COW mappings. In general, using multiple vmas is preferable;
2070
+ * vmf_insert_pfn_prot should only be used if using multiple VMAs is
19172071 * impractical.
2072
+ *
2073
+ * See vmf_insert_mixed_prot() for a discussion of the implication of using
2074
+ * a value of @pgprot different from that of @vma->vm_page_prot.
2075
+ *
2076
+ * Context: Process context. May allocate using %GFP_KERNEL.
2077
+ * Return: vm_fault_t value.
19182078 */
1919
-int vm_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
2079
+vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
19202080 unsigned long pfn, pgprot_t pgprot)
19212081 {
1922
- int ret;
19232082 /*
19242083 * Technically, architectures with pte_special can avoid all these
19252084 * restrictions (same for remap_pfn_range). However we would like
....@@ -1933,19 +2092,44 @@
19332092 BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
19342093
19352094 if (addr < vma->vm_start || addr >= vma->vm_end)
1936
- return -EFAULT;
2095
+ return VM_FAULT_SIGBUS;
19372096
19382097 if (!pfn_modify_allowed(pfn, pgprot))
1939
- return -EACCES;
2098
+ return VM_FAULT_SIGBUS;
19402099
19412100 track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV));
19422101
1943
- ret = insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot,
2102
+ return insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot,
19442103 false);
1945
-
1946
- return ret;
19472104 }
1948
-EXPORT_SYMBOL(vm_insert_pfn_prot);
2105
+EXPORT_SYMBOL(vmf_insert_pfn_prot);
2106
+
2107
+/**
2108
+ * vmf_insert_pfn - insert single pfn into user vma
2109
+ * @vma: user vma to map to
2110
+ * @addr: target user address of this page
2111
+ * @pfn: source kernel pfn
2112
+ *
2113
+ * Similar to vm_insert_page, this allows drivers to insert individual pages
2114
+ * they've allocated into a user vma. Same comments apply.
2115
+ *
2116
+ * This function should only be called from a vm_ops->fault handler, and
2117
+ * in that case the handler should return the result of this function.
2118
+ *
2119
+ * vma cannot be a COW mapping.
2120
+ *
2121
+ * As this is called only for pages that do not currently exist, we
2122
+ * do not need to flush old virtual caches or the TLB.
2123
+ *
2124
+ * Context: Process context. May allocate using %GFP_KERNEL.
2125
+ * Return: vm_fault_t value.
2126
+ */
2127
+vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
2128
+ unsigned long pfn)
2129
+{
2130
+ return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot);
2131
+}
2132
+EXPORT_SYMBOL(vmf_insert_pfn);
19492133
19502134 static bool vm_mixed_ok(struct vm_area_struct *vma, pfn_t pfn)
19512135 {
....@@ -1961,20 +2145,21 @@
19612145 return false;
19622146 }
19632147
1964
-static int __vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
1965
- pfn_t pfn, bool mkwrite)
2148
+static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma,
2149
+ unsigned long addr, pfn_t pfn, pgprot_t pgprot,
2150
+ bool mkwrite)
19662151 {
1967
- pgprot_t pgprot = vma->vm_page_prot;
2152
+ int err;
19682153
19692154 BUG_ON(!vm_mixed_ok(vma, pfn));
19702155
19712156 if (addr < vma->vm_start || addr >= vma->vm_end)
1972
- return -EFAULT;
2157
+ return VM_FAULT_SIGBUS;
19732158
19742159 track_pfn_insert(vma, &pgprot, pfn);
19752160
19762161 if (!pfn_modify_allowed(pfn_t_to_pfn(pfn), pgprot))
1977
- return -EACCES;
2162
+ return VM_FAULT_SIGBUS;
19782163
19792164 /*
19802165 * If we don't have pte special, then we have to use the pfn_valid()
....@@ -1993,36 +2178,68 @@
19932178 * result in pfn_t_has_page() == false.
19942179 */
19952180 page = pfn_to_page(pfn_t_to_pfn(pfn));
1996
- return insert_page(vma, addr, page, pgprot);
2181
+ err = insert_page(vma, addr, page, pgprot);
2182
+ } else {
2183
+ return insert_pfn(vma, addr, pfn, pgprot, mkwrite);
19972184 }
1998
- return insert_pfn(vma, addr, pfn, pgprot, mkwrite);
2185
+
2186
+ if (err == -ENOMEM)
2187
+ return VM_FAULT_OOM;
2188
+ if (err < 0 && err != -EBUSY)
2189
+ return VM_FAULT_SIGBUS;
2190
+
2191
+ return VM_FAULT_NOPAGE;
19992192 }
20002193
2001
-int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
2002
- pfn_t pfn)
2194
+/**
2195
+ * vmf_insert_mixed_prot - insert single pfn into user vma with specified pgprot
2196
+ * @vma: user vma to map to
2197
+ * @addr: target user address of this page
2198
+ * @pfn: source kernel pfn
2199
+ * @pgprot: pgprot flags for the inserted page
2200
+ *
2201
+ * This is exactly like vmf_insert_mixed(), except that it allows drivers
2202
+ * to override pgprot on a per-page basis.
2203
+ *
2204
+ * Typically this function should be used by drivers to set caching- and
2205
+ * encryption bits different than those of @vma->vm_page_prot, because
2206
+ * the caching- or encryption mode may not be known at mmap() time.
2207
+ * This is ok as long as @vma->vm_page_prot is not used by the core vm
2208
+ * to set caching and encryption bits for those vmas (except for COW pages).
2209
+ * This is ensured by core vm only modifying these page table entries using
2210
+ * functions that don't touch caching- or encryption bits, using pte_modify()
2211
+ * if needed. (See for example mprotect()).
2212
+ * Also when new page-table entries are created, this is only done using the
2213
+ * fault() callback, and never using the value of vma->vm_page_prot,
2214
+ * except for page-table entries that point to anonymous pages as the result
2215
+ * of COW.
2216
+ *
2217
+ * Context: Process context. May allocate using %GFP_KERNEL.
2218
+ * Return: vm_fault_t value.
2219
+ */
2220
+vm_fault_t vmf_insert_mixed_prot(struct vm_area_struct *vma, unsigned long addr,
2221
+ pfn_t pfn, pgprot_t pgprot)
20032222 {
2004
- return __vm_insert_mixed(vma, addr, pfn, false);
2005
-
2223
+ return __vm_insert_mixed(vma, addr, pfn, pgprot, false);
20062224 }
2007
-EXPORT_SYMBOL(vm_insert_mixed);
2225
+EXPORT_SYMBOL(vmf_insert_mixed_prot);
2226
+
2227
+vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
2228
+ pfn_t pfn)
2229
+{
2230
+ return __vm_insert_mixed(vma, addr, pfn, vma->vm_page_prot, false);
2231
+}
2232
+EXPORT_SYMBOL(vmf_insert_mixed);
20082233
20092234 /*
20102235 * If the insertion of PTE failed because someone else already added a
20112236 * different entry in the mean time, we treat that as success as we assume
20122237 * the same entry was actually inserted.
20132238 */
2014
-
20152239 vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma,
20162240 unsigned long addr, pfn_t pfn)
20172241 {
2018
- int err;
2019
-
2020
- err = __vm_insert_mixed(vma, addr, pfn, true);
2021
- if (err == -ENOMEM)
2022
- return VM_FAULT_OOM;
2023
- if (err < 0 && err != -EBUSY)
2024
- return VM_FAULT_SIGBUS;
2025
- return VM_FAULT_NOPAGE;
2242
+ return __vm_insert_mixed(vma, addr, pfn, vma->vm_page_prot, true);
20262243 }
20272244 EXPORT_SYMBOL(vmf_insert_mixed_mkwrite);
20282245
....@@ -2127,12 +2344,14 @@
21272344 /**
21282345 * remap_pfn_range - remap kernel memory to userspace
21292346 * @vma: user vma to map to
2130
- * @addr: target user address to start at
2131
- * @pfn: physical address of kernel memory
2132
- * @size: size of map area
2347
+ * @addr: target page aligned user address to start at
2348
+ * @pfn: page frame number of kernel physical memory address
2349
+ * @size: size of mapping area
21332350 * @prot: page protection flags for this mapping
21342351 *
2135
- * Note: this is only safe if the mm semaphore is held when called.
2352
+ * Note: this is only safe if the mm semaphore is held when called.
2353
+ *
2354
+ * Return: %0 on success, negative error code otherwise.
21362355 */
21372356 int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
21382357 unsigned long pfn, unsigned long size, pgprot_t prot)
....@@ -2143,6 +2362,9 @@
21432362 struct mm_struct *mm = vma->vm_mm;
21442363 unsigned long remap_pfn = pfn;
21452364 int err;
2365
+
2366
+ if (WARN_ON_ONCE(!PAGE_ALIGNED(addr)))
2367
+ return -EINVAL;
21462368
21472369 /*
21482370 * Physically remapped pages are special. Tell the
....@@ -2196,7 +2418,7 @@
21962418 /**
21972419 * vm_iomap_memory - remap memory to userspace
21982420 * @vma: user vma to map to
2199
- * @start: start of area
2421
+ * @start: start of the physical memory to be mapped
22002422 * @len: size of area
22012423 *
22022424 * This is a simplified io_remap_pfn_range() for common driver use. The
....@@ -2205,6 +2427,8 @@
22052427 *
22062428 * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
22072429 * whatever write-combining details or similar.
2430
+ *
2431
+ * Return: %0 on success, negative error code otherwise.
22082432 */
22092433 int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
22102434 {
....@@ -2242,30 +2466,39 @@
22422466
22432467 static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
22442468 unsigned long addr, unsigned long end,
2245
- pte_fn_t fn, void *data)
2469
+ pte_fn_t fn, void *data, bool create,
2470
+ pgtbl_mod_mask *mask)
22462471 {
22472472 pte_t *pte;
2248
- int err;
2249
- pgtable_t token;
2250
- spinlock_t *uninitialized_var(ptl);
2473
+ int err = 0;
2474
+ spinlock_t *ptl;
22512475
2252
- pte = (mm == &init_mm) ?
2253
- pte_alloc_kernel(pmd, addr) :
2254
- pte_alloc_map_lock(mm, pmd, addr, &ptl);
2255
- if (!pte)
2256
- return -ENOMEM;
2476
+ if (create) {
2477
+ pte = (mm == &init_mm) ?
2478
+ pte_alloc_kernel_track(pmd, addr, mask) :
2479
+ pte_alloc_map_lock(mm, pmd, addr, &ptl);
2480
+ if (!pte)
2481
+ return -ENOMEM;
2482
+ } else {
2483
+ pte = (mm == &init_mm) ?
2484
+ pte_offset_kernel(pmd, addr) :
2485
+ pte_offset_map_lock(mm, pmd, addr, &ptl);
2486
+ }
22572487
22582488 BUG_ON(pmd_huge(*pmd));
22592489
22602490 arch_enter_lazy_mmu_mode();
22612491
2262
- token = pmd_pgtable(*pmd);
2263
-
2264
- do {
2265
- err = fn(pte++, token, addr, data);
2266
- if (err)
2267
- break;
2268
- } while (addr += PAGE_SIZE, addr != end);
2492
+ if (fn) {
2493
+ do {
2494
+ if (create || !pte_none(*pte)) {
2495
+ err = fn(pte++, addr, data);
2496
+ if (err)
2497
+ break;
2498
+ }
2499
+ } while (addr += PAGE_SIZE, addr != end);
2500
+ }
2501
+ *mask |= PGTBL_PTE_MODIFIED;
22692502
22702503 arch_leave_lazy_mmu_mode();
22712504
....@@ -2276,63 +2509,116 @@
22762509
22772510 static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
22782511 unsigned long addr, unsigned long end,
2279
- pte_fn_t fn, void *data)
2512
+ pte_fn_t fn, void *data, bool create,
2513
+ pgtbl_mod_mask *mask)
22802514 {
22812515 pmd_t *pmd;
22822516 unsigned long next;
2283
- int err;
2517
+ int err = 0;
22842518
22852519 BUG_ON(pud_huge(*pud));
22862520
2287
- pmd = pmd_alloc(mm, pud, addr);
2288
- if (!pmd)
2289
- return -ENOMEM;
2521
+ if (create) {
2522
+ pmd = pmd_alloc_track(mm, pud, addr, mask);
2523
+ if (!pmd)
2524
+ return -ENOMEM;
2525
+ } else {
2526
+ pmd = pmd_offset(pud, addr);
2527
+ }
22902528 do {
22912529 next = pmd_addr_end(addr, end);
2292
- err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
2293
- if (err)
2294
- break;
2530
+ if (create || !pmd_none_or_clear_bad(pmd)) {
2531
+ err = apply_to_pte_range(mm, pmd, addr, next, fn, data,
2532
+ create, mask);
2533
+ if (err)
2534
+ break;
2535
+ }
22952536 } while (pmd++, addr = next, addr != end);
22962537 return err;
22972538 }
22982539
22992540 static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d,
23002541 unsigned long addr, unsigned long end,
2301
- pte_fn_t fn, void *data)
2542
+ pte_fn_t fn, void *data, bool create,
2543
+ pgtbl_mod_mask *mask)
23022544 {
23032545 pud_t *pud;
23042546 unsigned long next;
2305
- int err;
2547
+ int err = 0;
23062548
2307
- pud = pud_alloc(mm, p4d, addr);
2308
- if (!pud)
2309
- return -ENOMEM;
2549
+ if (create) {
2550
+ pud = pud_alloc_track(mm, p4d, addr, mask);
2551
+ if (!pud)
2552
+ return -ENOMEM;
2553
+ } else {
2554
+ pud = pud_offset(p4d, addr);
2555
+ }
23102556 do {
23112557 next = pud_addr_end(addr, end);
2312
- err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
2313
- if (err)
2314
- break;
2558
+ if (create || !pud_none_or_clear_bad(pud)) {
2559
+ err = apply_to_pmd_range(mm, pud, addr, next, fn, data,
2560
+ create, mask);
2561
+ if (err)
2562
+ break;
2563
+ }
23152564 } while (pud++, addr = next, addr != end);
23162565 return err;
23172566 }
23182567
23192568 static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd,
23202569 unsigned long addr, unsigned long end,
2321
- pte_fn_t fn, void *data)
2570
+ pte_fn_t fn, void *data, bool create,
2571
+ pgtbl_mod_mask *mask)
23222572 {
23232573 p4d_t *p4d;
23242574 unsigned long next;
2325
- int err;
2575
+ int err = 0;
23262576
2327
- p4d = p4d_alloc(mm, pgd, addr);
2328
- if (!p4d)
2329
- return -ENOMEM;
2577
+ if (create) {
2578
+ p4d = p4d_alloc_track(mm, pgd, addr, mask);
2579
+ if (!p4d)
2580
+ return -ENOMEM;
2581
+ } else {
2582
+ p4d = p4d_offset(pgd, addr);
2583
+ }
23302584 do {
23312585 next = p4d_addr_end(addr, end);
2332
- err = apply_to_pud_range(mm, p4d, addr, next, fn, data);
2586
+ if (create || !p4d_none_or_clear_bad(p4d)) {
2587
+ err = apply_to_pud_range(mm, p4d, addr, next, fn, data,
2588
+ create, mask);
2589
+ if (err)
2590
+ break;
2591
+ }
2592
+ } while (p4d++, addr = next, addr != end);
2593
+ return err;
2594
+}
2595
+
2596
+static int __apply_to_page_range(struct mm_struct *mm, unsigned long addr,
2597
+ unsigned long size, pte_fn_t fn,
2598
+ void *data, bool create)
2599
+{
2600
+ pgd_t *pgd;
2601
+ unsigned long start = addr, next;
2602
+ unsigned long end = addr + size;
2603
+ pgtbl_mod_mask mask = 0;
2604
+ int err = 0;
2605
+
2606
+ if (WARN_ON(addr >= end))
2607
+ return -EINVAL;
2608
+
2609
+ pgd = pgd_offset(mm, addr);
2610
+ do {
2611
+ next = pgd_addr_end(addr, end);
2612
+ if (!create && pgd_none_or_clear_bad(pgd))
2613
+ continue;
2614
+ err = apply_to_p4d_range(mm, pgd, addr, next, fn, data, create, &mask);
23332615 if (err)
23342616 break;
2335
- } while (p4d++, addr = next, addr != end);
2617
+ } while (pgd++, addr = next, addr != end);
2618
+
2619
+ if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
2620
+ arch_sync_kernel_mappings(start, start + size);
2621
+
23362622 return err;
23372623 }
23382624
....@@ -2343,25 +2629,242 @@
23432629 int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
23442630 unsigned long size, pte_fn_t fn, void *data)
23452631 {
2346
- pgd_t *pgd;
2347
- unsigned long next;
2348
- unsigned long end = addr + size;
2349
- int err;
2350
-
2351
- if (WARN_ON(addr >= end))
2352
- return -EINVAL;
2353
-
2354
- pgd = pgd_offset(mm, addr);
2355
- do {
2356
- next = pgd_addr_end(addr, end);
2357
- err = apply_to_p4d_range(mm, pgd, addr, next, fn, data);
2358
- if (err)
2359
- break;
2360
- } while (pgd++, addr = next, addr != end);
2361
-
2362
- return err;
2632
+ return __apply_to_page_range(mm, addr, size, fn, data, true);
23632633 }
23642634 EXPORT_SYMBOL_GPL(apply_to_page_range);
2635
+
2636
+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
2637
+static bool pte_spinlock(struct vm_fault *vmf)
2638
+{
2639
+ bool ret = false;
2640
+ pmd_t pmdval;
2641
+
2642
+ /* Check if vma is still valid */
2643
+ if (!(vmf->flags & FAULT_FLAG_SPECULATIVE)) {
2644
+ vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
2645
+ spin_lock(vmf->ptl);
2646
+ return true;
2647
+ }
2648
+
2649
+ local_irq_disable();
2650
+ if (vma_has_changed(vmf)) {
2651
+ trace_spf_vma_changed(_RET_IP_, vmf->vma, vmf->address);
2652
+ goto out;
2653
+ }
2654
+
2655
+ /*
2656
+ * We check if the pmd value is still the same to ensure that there
2657
+ * is not a huge collapse operation in progress in our back.
2658
+ * It also ensures that pmd was not cleared by pmd_clear in
2659
+ * free_pte_range and ptl is still valid.
2660
+ */
2661
+ pmdval = READ_ONCE(*vmf->pmd);
2662
+ if (!pmd_same(pmdval, vmf->orig_pmd)) {
2663
+ trace_spf_pmd_changed(_RET_IP_, vmf->vma, vmf->address);
2664
+ goto out;
2665
+ }
2666
+
2667
+ vmf->ptl = pte_lockptr(vmf->vma->vm_mm, &pmdval);
2668
+ if (unlikely(!spin_trylock(vmf->ptl))) {
2669
+ trace_spf_pte_lock(_RET_IP_, vmf->vma, vmf->address);
2670
+ goto out;
2671
+ }
2672
+
2673
+ /*
2674
+ * The check below will fail if pte_spinlock passed its ptl barrier
2675
+ * before we took the ptl lock.
2676
+ */
2677
+ if (vma_has_changed(vmf)) {
2678
+ spin_unlock(vmf->ptl);
2679
+ trace_spf_vma_changed(_RET_IP_, vmf->vma, vmf->address);
2680
+ goto out;
2681
+ }
2682
+
2683
+ ret = true;
2684
+out:
2685
+ local_irq_enable();
2686
+ return ret;
2687
+}
2688
+
2689
+static bool __pte_map_lock_speculative(struct vm_fault *vmf, unsigned long addr)
2690
+{
2691
+ bool ret = false;
2692
+ pte_t *pte;
2693
+ spinlock_t *ptl;
2694
+ pmd_t pmdval;
2695
+
2696
+ /*
2697
+ * The first vma_has_changed() guarantees the page-tables are still
2698
+ * valid, having IRQs disabled ensures they stay around, hence the
2699
+ * second vma_has_changed() to make sure they are still valid once
2700
+ * we've got the lock. After that a concurrent zap_pte_range() will
2701
+ * block on the PTL and thus we're safe.
2702
+ */
2703
+ local_irq_disable();
2704
+ if (vma_has_changed(vmf)) {
2705
+ trace_spf_vma_changed(_RET_IP_, vmf->vma, addr);
2706
+ goto out;
2707
+ }
2708
+
2709
+ /*
2710
+ * We check if the pmd value is still the same to ensure that there
2711
+ * is not a huge collapse operation in progress in our back.
2712
+ */
2713
+ pmdval = READ_ONCE(*vmf->pmd);
2714
+ if (!pmd_same(pmdval, vmf->orig_pmd)) {
2715
+ trace_spf_pmd_changed(_RET_IP_, vmf->vma, addr);
2716
+ goto out;
2717
+ }
2718
+
2719
+ /*
2720
+ * Same as pte_offset_map_lock() except that we call
2721
+ * spin_trylock() in place of spin_lock() to avoid race with
2722
+ * unmap path which may have the lock and wait for this CPU
2723
+ * to invalidate TLB but this CPU has irq disabled.
2724
+ * Since we are in a speculative patch, accept it could fail
2725
+ */
2726
+ ptl = pte_lockptr(vmf->vma->vm_mm, &pmdval);
2727
+ pte = pte_offset_map(&pmdval, addr);
2728
+ if (unlikely(!spin_trylock(ptl))) {
2729
+ pte_unmap(pte);
2730
+ trace_spf_pte_lock(_RET_IP_, vmf->vma, addr);
2731
+ goto out;
2732
+ }
2733
+
2734
+ /*
2735
+ * The check below will fail if __pte_map_lock_speculative passed its ptl
2736
+ * barrier before we took the ptl lock.
2737
+ */
2738
+ if (vma_has_changed(vmf)) {
2739
+ pte_unmap_unlock(pte, ptl);
2740
+ trace_spf_vma_changed(_RET_IP_, vmf->vma, addr);
2741
+ goto out;
2742
+ }
2743
+
2744
+ vmf->pte = pte;
2745
+ vmf->ptl = ptl;
2746
+ ret = true;
2747
+out:
2748
+ local_irq_enable();
2749
+ return ret;
2750
+}
2751
+
2752
+static bool pte_map_lock(struct vm_fault *vmf)
2753
+{
2754
+ if (!(vmf->flags & FAULT_FLAG_SPECULATIVE)) {
2755
+ vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd,
2756
+ vmf->address, &vmf->ptl);
2757
+ return true;
2758
+ }
2759
+
2760
+ return __pte_map_lock_speculative(vmf, vmf->address);
2761
+}
2762
+
2763
+bool pte_map_lock_addr(struct vm_fault *vmf, unsigned long addr)
2764
+{
2765
+ if (!(vmf->flags & FAULT_FLAG_SPECULATIVE)) {
2766
+ vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd,
2767
+ addr, &vmf->ptl);
2768
+ return true;
2769
+ }
2770
+
2771
+ return __pte_map_lock_speculative(vmf, addr);
2772
+}
2773
+
2774
+static bool __read_mostly allow_file_spec_access;
2775
+static int __init allow_file_spec_access_setup(char *str)
2776
+{
2777
+ allow_file_spec_access = true;
2778
+ return 1;
2779
+}
2780
+__setup("allow_file_spec_access", allow_file_spec_access_setup);
2781
+
2782
+static bool vmf_allows_speculation(struct vm_fault *vmf)
2783
+{
2784
+ if (vma_is_anonymous(vmf->vma)) {
2785
+ /*
2786
+ * __anon_vma_prepare() requires the mmap_sem to be held
2787
+ * because vm_next and vm_prev must be safe. This can't be
2788
+ * guaranteed in the speculative path.
2789
+ */
2790
+ if (!vmf->vma->anon_vma) {
2791
+ trace_spf_vma_notsup(_RET_IP_, vmf->vma, vmf->address);
2792
+ return false;
2793
+ }
2794
+ return true;
2795
+ }
2796
+
2797
+ if (!allow_file_spec_access) {
2798
+ /*
2799
+ * Can't call vm_ops service has we don't know what they would
2800
+ * do with the VMA.
2801
+ * This include huge page from hugetlbfs.
2802
+ */
2803
+ trace_spf_vma_notsup(_RET_IP_, vmf->vma, vmf->address);
2804
+ return false;
2805
+ }
2806
+
2807
+ if (!(vmf->vma->vm_flags & VM_SHARED) &&
2808
+ (vmf->flags & FAULT_FLAG_WRITE) &&
2809
+ !vmf->vma->anon_vma) {
2810
+ /*
2811
+ * non-anonymous private COW without anon_vma.
2812
+ * See above.
2813
+ */
2814
+ trace_spf_vma_notsup(_RET_IP_, vmf->vma, vmf->address);
2815
+ return false;
2816
+ }
2817
+
2818
+ if (vmf->vma->vm_ops->allow_speculation &&
2819
+ vmf->vma->vm_ops->allow_speculation()) {
2820
+ return true;
2821
+ }
2822
+
2823
+ trace_spf_vma_notsup(_RET_IP_, vmf->vma, vmf->address);
2824
+ return false;
2825
+}
2826
+
2827
+#else
2828
+static inline bool pte_spinlock(struct vm_fault *vmf)
2829
+{
2830
+ vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
2831
+ spin_lock(vmf->ptl);
2832
+ return true;
2833
+}
2834
+
2835
+static inline bool pte_map_lock(struct vm_fault *vmf)
2836
+{
2837
+ vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd,
2838
+ vmf->address, &vmf->ptl);
2839
+ return true;
2840
+}
2841
+
2842
+inline bool pte_map_lock_addr(struct vm_fault *vmf, unsigned long addr)
2843
+{
2844
+ vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd,
2845
+ addr, &vmf->ptl);
2846
+ return true;
2847
+}
2848
+
2849
+static inline bool vmf_allows_speculation(struct vm_fault *vmf)
2850
+{
2851
+ return false;
2852
+}
2853
+#endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
2854
+
2855
+/*
2856
+ * Scan a region of virtual memory, calling a provided function on
2857
+ * each leaf page table where it exists.
2858
+ *
2859
+ * Unlike apply_to_page_range, this does _not_ fill in page tables
2860
+ * where they are absent.
2861
+ */
2862
+int apply_to_existing_page_range(struct mm_struct *mm, unsigned long addr,
2863
+ unsigned long size, pte_fn_t fn, void *data)
2864
+{
2865
+ return __apply_to_page_range(mm, addr, size, fn, data, false);
2866
+}
2867
+EXPORT_SYMBOL_GPL(apply_to_existing_page_range);
23652868
23662869 /*
23672870 * handle_pte_fault chooses page fault handler according to an entry which was
....@@ -2370,21 +2873,29 @@
23702873 * parts, do_swap_page must check under lock before unmapping the pte and
23712874 * proceeding (but do_wp_page is only called after already making such a check;
23722875 * and do_anonymous_page can safely check later on).
2876
+ *
2877
+ * pte_unmap_same() returns:
2878
+ * 0 if the PTE are the same
2879
+ * VM_FAULT_PTNOTSAME if the PTE are different
2880
+ * VM_FAULT_RETRY if the VMA has changed in our back during
2881
+ * a speculative page fault handling.
23732882 */
2374
-static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
2375
- pte_t *page_table, pte_t orig_pte)
2883
+static inline int pte_unmap_same(struct vm_fault *vmf)
23762884 {
2377
- int same = 1;
2885
+ int ret = 0;
2886
+
23782887 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
23792888 if (sizeof(pte_t) > sizeof(unsigned long)) {
2380
- spinlock_t *ptl = pte_lockptr(mm, pmd);
2381
- spin_lock(ptl);
2382
- same = pte_same(*page_table, orig_pte);
2383
- spin_unlock(ptl);
2889
+ if (pte_spinlock(vmf)) {
2890
+ if (!pte_same(*vmf->pte, vmf->orig_pte))
2891
+ ret = VM_FAULT_PTNOTSAME;
2892
+ spin_unlock(vmf->ptl);
2893
+ } else
2894
+ ret = VM_FAULT_RETRY;
23842895 }
23852896 #endif
2386
- pte_unmap(page_table);
2387
- return same;
2897
+ pte_unmap(vmf->pte);
2898
+ return ret;
23882899 }
23892900
23902901 static inline bool cow_user_page(struct page *dst, struct page *src,
....@@ -2397,8 +2908,6 @@
23972908 struct vm_area_struct *vma = vmf->vma;
23982909 struct mm_struct *mm = vma->vm_mm;
23992910 unsigned long addr = vmf->address;
2400
-
2401
- debug_dma_assert_idle(src);
24022911
24032912 if (likely(src)) {
24042913 copy_user_highpage(dst, src, addr, vma);
....@@ -2426,10 +2935,9 @@
24262935 if (!likely(pte_same(*vmf->pte, vmf->orig_pte))) {
24272936 /*
24282937 * Other thread has already handled the fault
2429
- * and we don't need to do anything. If it's
2430
- * not the case, the fault will be triggered
2431
- * again on the same address.
2938
+ * and update local tlb only
24322939 */
2940
+ update_mmu_tlb(vma, addr, vmf->pte);
24332941 ret = false;
24342942 goto pte_unlock;
24352943 }
....@@ -2453,13 +2961,14 @@
24532961 vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl);
24542962 locked = true;
24552963 if (!likely(pte_same(*vmf->pte, vmf->orig_pte))) {
2456
- /* The PTE changed under us. Retry page fault. */
2964
+ /* The PTE changed under us, update local tlb */
2965
+ update_mmu_tlb(vma, addr, vmf->pte);
24572966 ret = false;
24582967 goto pte_unlock;
24592968 }
24602969
24612970 /*
2462
- * The same page can be mapped back since last copy attampt.
2971
+ * The same page can be mapped back since last copy attempt.
24632972 * Try to copy again under PTL.
24642973 */
24652974 if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE)) {
....@@ -2538,10 +3047,11 @@
25383047 *
25393048 * The function expects the page to be locked and unlocks it.
25403049 */
2541
-static void fault_dirty_shared_page(struct vm_area_struct *vma,
2542
- struct page *page)
3050
+static vm_fault_t fault_dirty_shared_page(struct vm_fault *vmf)
25433051 {
3052
+ struct vm_area_struct *vma = vmf->vma;
25443053 struct address_space *mapping;
3054
+ struct page *page = vmf->page;
25453055 bool dirtied;
25463056 bool page_mkwrite = vma->vm_ops && vma->vm_ops->page_mkwrite;
25473057
....@@ -2556,16 +3066,30 @@
25563066 mapping = page_rmapping(page);
25573067 unlock_page(page);
25583068
2559
- if ((dirtied || page_mkwrite) && mapping) {
2560
- /*
2561
- * Some device drivers do not set page.mapping
2562
- * but still dirty their pages
2563
- */
2564
- balance_dirty_pages_ratelimited(mapping);
2565
- }
2566
-
25673069 if (!page_mkwrite)
25683070 file_update_time(vma->vm_file);
3071
+
3072
+ /*
3073
+ * Throttle page dirtying rate down to writeback speed.
3074
+ *
3075
+ * mapping may be NULL here because some device drivers do not
3076
+ * set page.mapping but still dirty their pages
3077
+ *
3078
+ * Drop the mmap_lock before waiting on IO, if we can. The file
3079
+ * is pinning the mapping, as per above.
3080
+ */
3081
+ if ((dirtied || page_mkwrite) && mapping) {
3082
+ struct file *fpin;
3083
+
3084
+ fpin = maybe_unlock_mmap_for_io(vmf, NULL);
3085
+ balance_dirty_pages_ratelimited(mapping);
3086
+ if (fpin) {
3087
+ fput(fpin);
3088
+ return VM_FAULT_RETRY;
3089
+ }
3090
+ }
3091
+
3092
+ return 0;
25693093 }
25703094
25713095 /*
....@@ -2592,16 +3116,17 @@
25923116
25933117 flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
25943118 entry = pte_mkyoung(vmf->orig_pte);
2595
- entry = maybe_mkwrite(pte_mkdirty(entry), vma);
3119
+ entry = maybe_mkwrite(pte_mkdirty(entry), vmf->vma_flags);
25963120 if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1))
25973121 update_mmu_cache(vma, vmf->address, vmf->pte);
25983122 pte_unmap_unlock(vmf->pte, vmf->ptl);
3123
+ count_vm_event(PGREUSE);
25993124 }
26003125
26013126 /*
26023127 * Handle the case of a page which we actually need to copy to a new page.
26033128 *
2604
- * Called with mmap_sem locked and the old page referenced, but
3129
+ * Called with mmap_lock locked and the old page referenced, but
26053130 * without the ptl held.
26063131 *
26073132 * High level logic flow:
....@@ -2622,23 +3147,22 @@
26223147 struct page *new_page = NULL;
26233148 pte_t entry;
26243149 int page_copied = 0;
2625
- const unsigned long mmun_start = vmf->address & PAGE_MASK;
2626
- const unsigned long mmun_end = mmun_start + PAGE_SIZE;
2627
- struct mem_cgroup *memcg;
3150
+ struct mmu_notifier_range range;
3151
+ vm_fault_t ret = VM_FAULT_OOM;
26283152
26293153 if (unlikely(anon_vma_prepare(vma)))
2630
- goto oom;
3154
+ goto out;
26313155
26323156 if (is_zero_pfn(pte_pfn(vmf->orig_pte))) {
26333157 new_page = alloc_zeroed_user_highpage_movable(vma,
26343158 vmf->address);
26353159 if (!new_page)
2636
- goto oom;
3160
+ goto out;
26373161 } else {
26383162 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
26393163 vmf->address);
26403164 if (!new_page)
2641
- goto oom;
3165
+ goto out;
26423166
26433167 if (!cow_user_page(new_page, old_page, vmf)) {
26443168 /*
....@@ -2652,19 +3176,27 @@
26523176 put_page(old_page);
26533177 return 0;
26543178 }
3179
+ trace_android_vh_cow_user_page(vmf, new_page);
26553180 }
26563181
2657
- if (mem_cgroup_try_charge_delay(new_page, mm, GFP_KERNEL, &memcg, false))
2658
- goto oom_free_new;
3182
+ if (mem_cgroup_charge(new_page, mm, GFP_KERNEL))
3183
+ goto out_free_new;
3184
+ cgroup_throttle_swaprate(new_page, GFP_KERNEL);
26593185
26603186 __SetPageUptodate(new_page);
26613187
2662
- mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
3188
+ mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
3189
+ vmf->address & PAGE_MASK,
3190
+ (vmf->address & PAGE_MASK) + PAGE_SIZE);
3191
+ mmu_notifier_invalidate_range_start(&range);
26633192
26643193 /*
26653194 * Re-check the pte - we dropped the lock
26663195 */
2667
- vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl);
3196
+ if (!pte_map_lock(vmf)) {
3197
+ ret = VM_FAULT_RETRY;
3198
+ goto out_invalidate_end;
3199
+ }
26683200 if (likely(pte_same(*vmf->pte, vmf->orig_pte))) {
26693201 if (old_page) {
26703202 if (!PageAnon(old_page)) {
....@@ -2676,8 +3208,9 @@
26763208 inc_mm_counter_fast(mm, MM_ANONPAGES);
26773209 }
26783210 flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
2679
- entry = mk_pte(new_page, vma->vm_page_prot);
2680
- entry = maybe_mkwrite(pte_mkdirty(entry), vma);
3211
+ entry = mk_pte(new_page, vmf->vma_page_prot);
3212
+ entry = pte_sw_mkyoung(entry);
3213
+ entry = maybe_mkwrite(pte_mkdirty(entry), vmf->vma_flags);
26813214 /*
26823215 * Clear the pte entry and flush it first, before updating the
26833216 * pte with the new entry. This will avoid a race condition
....@@ -2685,9 +3218,8 @@
26853218 * thread doing COW.
26863219 */
26873220 ptep_clear_flush_notify(vma, vmf->address, vmf->pte);
2688
- page_add_new_anon_rmap(new_page, vma, vmf->address, false);
2689
- mem_cgroup_commit_charge(new_page, memcg, false, false);
2690
- lru_cache_add_active_or_unevictable(new_page, vma);
3221
+ __page_add_new_anon_rmap(new_page, vma, vmf->address, false);
3222
+ __lru_cache_add_inactive_or_unevictable(new_page, vmf->vma_flags);
26913223 /*
26923224 * We call the notify macro here because, when using secondary
26933225 * mmu page tables (such as kvm shadow page tables), we want the
....@@ -2725,7 +3257,7 @@
27253257 new_page = old_page;
27263258 page_copied = 1;
27273259 } else {
2728
- mem_cgroup_cancel_charge(new_page, memcg, false);
3260
+ update_mmu_tlb(vma, vmf->address, vmf->pte);
27293261 }
27303262
27313263 if (new_page)
....@@ -2736,13 +3268,13 @@
27363268 * No need to double call mmu_notifier->invalidate_range() callback as
27373269 * the above ptep_clear_flush_notify() did already call it.
27383270 */
2739
- mmu_notifier_invalidate_range_only_end(mm, mmun_start, mmun_end);
3271
+ mmu_notifier_invalidate_range_only_end(&range);
27403272 if (old_page) {
27413273 /*
27423274 * Don't let another task, with possibly unlocked vma,
27433275 * keep the mlocked page.
27443276 */
2745
- if (page_copied && (vma->vm_flags & VM_LOCKED)) {
3277
+ if (page_copied && (vmf->vma_flags & VM_LOCKED)) {
27463278 lock_page(old_page); /* LRU manipulation */
27473279 if (PageMlocked(old_page))
27483280 munlock_vma_page(old_page);
....@@ -2751,12 +3283,14 @@
27513283 put_page(old_page);
27523284 }
27533285 return page_copied ? VM_FAULT_WRITE : 0;
2754
-oom_free_new:
3286
+out_invalidate_end:
3287
+ mmu_notifier_invalidate_range_only_end(&range);
3288
+out_free_new:
27553289 put_page(new_page);
2756
-oom:
3290
+out:
27573291 if (old_page)
27583292 put_page(old_page);
2759
- return VM_FAULT_OOM;
3293
+ return ret;
27603294 }
27613295
27623296 /**
....@@ -2767,23 +3301,25 @@
27673301 *
27683302 * This function handles all that is needed to finish a write page fault in a
27693303 * shared mapping due to PTE being read-only once the mapped page is prepared.
2770
- * It handles locking of PTE and modifying it. The function returns
2771
- * VM_FAULT_WRITE on success, 0 when PTE got changed before we acquired PTE
2772
- * lock.
3304
+ * It handles locking of PTE and modifying it.
27733305 *
27743306 * The function expects the page to be locked or other protection against
27753307 * concurrent faults / writeback (such as DAX radix tree locks).
3308
+ *
3309
+ * Return: %VM_FAULT_WRITE on success, %0 when PTE got changed before
3310
+ * we acquired PTE lock.
27763311 */
27773312 vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf)
27783313 {
2779
- WARN_ON_ONCE(!(vmf->vma->vm_flags & VM_SHARED));
2780
- vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, vmf->address,
2781
- &vmf->ptl);
3314
+ WARN_ON_ONCE(!(vmf->vma_flags & VM_SHARED));
3315
+ if (!pte_map_lock(vmf))
3316
+ return VM_FAULT_RETRY;
27823317 /*
27833318 * We might have raced with another page fault while we released the
27843319 * pte_offset_map_lock.
27853320 */
27863321 if (!pte_same(*vmf->pte, vmf->orig_pte)) {
3322
+ update_mmu_tlb(vmf->vma, vmf->address, vmf->pte);
27873323 pte_unmap_unlock(vmf->pte, vmf->ptl);
27883324 return VM_FAULT_NOPAGE;
27893325 }
....@@ -2817,6 +3353,7 @@
28173353 __releases(vmf->ptl)
28183354 {
28193355 struct vm_area_struct *vma = vmf->vma;
3356
+ vm_fault_t ret = VM_FAULT_WRITE;
28203357
28213358 get_page(vmf->page);
28223359
....@@ -2840,10 +3377,10 @@
28403377 wp_page_reuse(vmf);
28413378 lock_page(vmf->page);
28423379 }
2843
- fault_dirty_shared_page(vma, vmf->page);
3380
+ ret |= fault_dirty_shared_page(vmf);
28443381 put_page(vmf->page);
28453382
2846
- return VM_FAULT_WRITE;
3383
+ return ret;
28473384 }
28483385
28493386 /*
....@@ -2860,16 +3397,32 @@
28603397 * change only once the write actually happens. This avoids a few races,
28613398 * and potentially makes it more efficient.
28623399 *
2863
- * We enter with non-exclusive mmap_sem (to exclude vma changes,
3400
+ * We enter with non-exclusive mmap_lock (to exclude vma changes,
28643401 * but allow concurrent faults), with pte both mapped and locked.
2865
- * We return with mmap_sem still held, but pte unmapped and unlocked.
3402
+ * We return with mmap_lock still held, but pte unmapped and unlocked.
28663403 */
28673404 static vm_fault_t do_wp_page(struct vm_fault *vmf)
28683405 __releases(vmf->ptl)
28693406 {
28703407 struct vm_area_struct *vma = vmf->vma;
28713408
2872
- vmf->page = vm_normal_page(vma, vmf->address, vmf->orig_pte);
3409
+ if (userfaultfd_pte_wp(vma, *vmf->pte)) {
3410
+ pte_unmap_unlock(vmf->pte, vmf->ptl);
3411
+ if (vmf->flags & FAULT_FLAG_SPECULATIVE)
3412
+ return VM_FAULT_RETRY;
3413
+ return handle_userfault(vmf, VM_UFFD_WP);
3414
+ }
3415
+
3416
+ /*
3417
+ * Userfaultfd write-protect can defer flushes. Ensure the TLB
3418
+ * is flushed in this case before copying.
3419
+ */
3420
+ if (unlikely(userfaultfd_wp(vmf->vma) &&
3421
+ mm_tlb_flush_pending(vmf->vma->vm_mm)))
3422
+ flush_tlb_page(vmf->vma, vmf->address);
3423
+
3424
+ vmf->page = _vm_normal_page(vma, vmf->address, vmf->orig_pte,
3425
+ vmf->vma_flags);
28733426 if (!vmf->page) {
28743427 /*
28753428 * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
....@@ -2878,7 +3431,7 @@
28783431 * We should not cow pages in a shared writeable mapping.
28793432 * Just mark the pages writable and/or call ops->pfn_mkwrite.
28803433 */
2881
- if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
3434
+ if ((vmf->vma_flags & (VM_WRITE|VM_SHARED)) ==
28823435 (VM_WRITE|VM_SHARED))
28833436 return wp_pfn_shared(vmf);
28843437
....@@ -2890,43 +3443,31 @@
28903443 * Take out anonymous pages first, anonymous shared vmas are
28913444 * not dirty accountable.
28923445 */
2893
- if (PageAnon(vmf->page) && !PageKsm(vmf->page)) {
2894
- int total_map_swapcount;
2895
- if (!trylock_page(vmf->page)) {
2896
- get_page(vmf->page);
2897
- pte_unmap_unlock(vmf->pte, vmf->ptl);
2898
- lock_page(vmf->page);
2899
- vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
2900
- vmf->address, &vmf->ptl);
2901
- if (!pte_same(*vmf->pte, vmf->orig_pte)) {
2902
- unlock_page(vmf->page);
2903
- pte_unmap_unlock(vmf->pte, vmf->ptl);
2904
- put_page(vmf->page);
2905
- return 0;
2906
- }
2907
- put_page(vmf->page);
3446
+ if (PageAnon(vmf->page)) {
3447
+ struct page *page = vmf->page;
3448
+
3449
+ /* PageKsm() doesn't necessarily raise the page refcount */
3450
+ if (PageKsm(page) || page_count(page) != 1)
3451
+ goto copy;
3452
+ if (!trylock_page(page))
3453
+ goto copy;
3454
+ if (PageKsm(page) || page_mapcount(page) != 1 || page_count(page) != 1) {
3455
+ unlock_page(page);
3456
+ goto copy;
29083457 }
2909
- if (reuse_swap_page(vmf->page, &total_map_swapcount)) {
2910
- if (total_map_swapcount == 1) {
2911
- /*
2912
- * The page is all ours. Move it to
2913
- * our anon_vma so the rmap code will
2914
- * not search our parent or siblings.
2915
- * Protected against the rmap code by
2916
- * the page lock.
2917
- */
2918
- page_move_anon_rmap(vmf->page, vma);
2919
- }
2920
- unlock_page(vmf->page);
2921
- wp_page_reuse(vmf);
2922
- return VM_FAULT_WRITE;
2923
- }
2924
- unlock_page(vmf->page);
2925
- } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
3458
+ /*
3459
+ * Ok, we've got the only map reference, and the only
3460
+ * page count reference, and the page is locked,
3461
+ * it's dark out, and we're wearing sunglasses. Hit it.
3462
+ */
3463
+ unlock_page(page);
3464
+ wp_page_reuse(vmf);
3465
+ return VM_FAULT_WRITE;
3466
+ } else if (unlikely((vmf->vma_flags & (VM_WRITE|VM_SHARED)) ==
29263467 (VM_WRITE|VM_SHARED))) {
29273468 return wp_page_shared(vmf);
29283469 }
2929
-
3470
+copy:
29303471 /*
29313472 * Ok, we need to copy. Oh, well..
29323473 */
....@@ -2989,7 +3530,7 @@
29893530
29903531 details.check_mapping = mapping;
29913532 details.first_index = page->index;
2992
- details.last_index = page->index + hpage_nr_pages(page) - 1;
3533
+ details.last_index = page->index + thp_nr_pages(page) - 1;
29933534 details.single_page = page;
29943535
29953536 i_mmap_lock_write(mapping);
....@@ -3063,26 +3604,40 @@
30633604 EXPORT_SYMBOL(unmap_mapping_range);
30643605
30653606 /*
3066
- * We enter with non-exclusive mmap_sem (to exclude vma changes,
3607
+ * We enter with non-exclusive mmap_lock (to exclude vma changes,
30673608 * but allow concurrent faults), and pte mapped but not yet locked.
30683609 * We return with pte unmapped and unlocked.
30693610 *
3070
- * We return with the mmap_sem locked or unlocked in the same cases
3611
+ * We return with the mmap_lock locked or unlocked in the same cases
30713612 * as does filemap_fault().
30723613 */
30733614 vm_fault_t do_swap_page(struct vm_fault *vmf)
30743615 {
30753616 struct vm_area_struct *vma = vmf->vma;
30763617 struct page *page = NULL, *swapcache;
3077
- struct mem_cgroup *memcg;
30783618 swp_entry_t entry;
30793619 pte_t pte;
30803620 int locked;
30813621 int exclusive = 0;
3082
- vm_fault_t ret = 0;
3622
+ vm_fault_t ret;
3623
+ void *shadow = NULL;
30833624
3084
- if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte))
3625
+ if (vmf->flags & FAULT_FLAG_SPECULATIVE) {
3626
+ pte_unmap(vmf->pte);
3627
+ return VM_FAULT_RETRY;
3628
+ }
3629
+
3630
+ ret = pte_unmap_same(vmf);
3631
+ if (ret) {
3632
+ /*
3633
+ * If pte != orig_pte, this means another thread did the
3634
+ * swap operation in our back.
3635
+ * So nothing else to do.
3636
+ */
3637
+ if (ret == VM_FAULT_PTNOTSAME)
3638
+ ret = 0;
30853639 goto out;
3640
+ }
30863641
30873642 entry = pte_to_swp_entry(vmf->orig_pte);
30883643 if (unlikely(non_swap_entry(entry))) {
....@@ -3090,13 +3645,8 @@
30903645 migration_entry_wait(vma->vm_mm, vmf->pmd,
30913646 vmf->address);
30923647 } else if (is_device_private_entry(entry)) {
3093
- /*
3094
- * For un-addressable device memory we call the pgmap
3095
- * fault handler callback. The callback must migrate
3096
- * the page back to some CPU accessible page.
3097
- */
3098
- ret = device_private_entry_fault(vma, vmf->address, entry,
3099
- vmf->flags, vmf->pmd);
3648
+ vmf->page = device_private_entry_to_page(entry);
3649
+ ret = vmf->page->pgmap->ops->migrate_to_ram(vmf);
31003650 } else if (is_hwpoison_entry(entry)) {
31013651 ret = VM_FAULT_HWPOISON;
31023652 } else {
....@@ -3113,19 +3663,51 @@
31133663
31143664 if (!page) {
31153665 struct swap_info_struct *si = swp_swap_info(entry);
3666
+ bool skip_swapcache = false;
31163667
3117
- if (si->flags & SWP_SYNCHRONOUS_IO &&
3118
- __swap_count(si, entry) == 1) {
3668
+ trace_android_vh_skip_swapcache(entry, &skip_swapcache);
3669
+ if ((data_race(si->flags & SWP_SYNCHRONOUS_IO) || skip_swapcache) &&
3670
+ __swap_count(entry) == 1) {
31193671 /* skip swapcache */
3120
- page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
3121
- vmf->address);
3672
+ gfp_t flags = GFP_HIGHUSER_MOVABLE;
3673
+
3674
+ trace_android_rvh_set_skip_swapcache_flags(&flags);
3675
+ page = alloc_page_vma(flags, vma, vmf->address);
31223676 if (page) {
3677
+ int err;
3678
+
31233679 __SetPageLocked(page);
31243680 __SetPageSwapBacked(page);
31253681 set_page_private(page, entry.val);
3126
- lru_cache_add_anon(page);
3682
+
3683
+ /* Tell memcg to use swap ownership records */
3684
+ SetPageSwapCache(page);
3685
+ err = mem_cgroup_charge(page, vma->vm_mm,
3686
+ GFP_KERNEL);
3687
+ ClearPageSwapCache(page);
3688
+ if (err) {
3689
+ ret = VM_FAULT_OOM;
3690
+ goto out_page;
3691
+ }
3692
+
3693
+ shadow = get_shadow_from_swap_cache(entry);
3694
+ if (shadow)
3695
+ workingset_refault(page, shadow);
3696
+
3697
+ lru_cache_add(page);
31273698 swap_readpage(page, true);
31283699 }
3700
+ } else if (vmf->flags & FAULT_FLAG_SPECULATIVE) {
3701
+ /*
3702
+ * Don't try readahead during a speculative page fault
3703
+ * as the VMA's boundaries may change in our back.
3704
+ * If the page is not in the swap cache and synchronous
3705
+ * read is disabled, fall back to the regular page fault
3706
+ * mechanism.
3707
+ */
3708
+ delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
3709
+ ret = VM_FAULT_RETRY;
3710
+ goto out;
31293711 } else {
31303712 page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
31313713 vmf);
....@@ -3134,11 +3716,16 @@
31343716
31353717 if (!page) {
31363718 /*
3137
- * Back out if somebody else faulted in this pte
3138
- * while we released the pte lock.
3719
+ * Back out if the VMA has changed in our back during
3720
+ * a speculative page fault or if somebody else
3721
+ * faulted in this pte while we released the pte lock.
31393722 */
3140
- vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
3141
- vmf->address, &vmf->ptl);
3723
+ if (!pte_map_lock(vmf)) {
3724
+ delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
3725
+ ret = VM_FAULT_RETRY;
3726
+ goto out;
3727
+ }
3728
+
31423729 if (likely(pte_same(*vmf->pte, vmf->orig_pte)))
31433730 ret = VM_FAULT_OOM;
31443731 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
....@@ -3184,17 +3771,16 @@
31843771 goto out_page;
31853772 }
31863773
3187
- if (mem_cgroup_try_charge_delay(page, vma->vm_mm, GFP_KERNEL,
3188
- &memcg, false)) {
3189
- ret = VM_FAULT_OOM;
3190
- goto out_page;
3191
- }
3774
+ cgroup_throttle_swaprate(page, GFP_KERNEL);
31923775
31933776 /*
3194
- * Back out if somebody else already faulted in this pte.
3777
+ * Back out if the VMA has changed in our back during a speculative
3778
+ * page fault or if somebody else already faulted in this pte.
31953779 */
3196
- vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3197
- &vmf->ptl);
3780
+ if (!pte_map_lock(vmf)) {
3781
+ ret = VM_FAULT_RETRY;
3782
+ goto out_page;
3783
+ }
31983784 if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte)))
31993785 goto out_nomap;
32003786
....@@ -3215,9 +3801,9 @@
32153801
32163802 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
32173803 dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS);
3218
- pte = mk_pte(page, vma->vm_page_prot);
3804
+ pte = mk_pte(page, vmf->vma_page_prot);
32193805 if ((vmf->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) {
3220
- pte = maybe_mkwrite(pte_mkdirty(pte), vma);
3806
+ pte = maybe_mkwrite(pte_mkdirty(pte), vmf->vma_flags);
32213807 vmf->flags &= ~FAULT_FLAG_WRITE;
32223808 ret |= VM_FAULT_WRITE;
32233809 exclusive = RMAP_EXCLUSIVE;
....@@ -3225,24 +3811,26 @@
32253811 flush_icache_page(vma, page);
32263812 if (pte_swp_soft_dirty(vmf->orig_pte))
32273813 pte = pte_mksoft_dirty(pte);
3814
+ if (pte_swp_uffd_wp(vmf->orig_pte)) {
3815
+ pte = pte_mkuffd_wp(pte);
3816
+ pte = pte_wrprotect(pte);
3817
+ }
32283818 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte);
32293819 arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte);
32303820 vmf->orig_pte = pte;
32313821
32323822 /* ksm created a completely new copy */
32333823 if (unlikely(page != swapcache && swapcache)) {
3234
- page_add_new_anon_rmap(page, vma, vmf->address, false);
3235
- mem_cgroup_commit_charge(page, memcg, false, false);
3236
- lru_cache_add_active_or_unevictable(page, vma);
3824
+ __page_add_new_anon_rmap(page, vma, vmf->address, false);
3825
+ __lru_cache_add_inactive_or_unevictable(page, vmf->vma_flags);
32373826 } else {
32383827 do_page_add_anon_rmap(page, vma, vmf->address, exclusive);
3239
- mem_cgroup_commit_charge(page, memcg, true, false);
3240
- activate_page(page);
32413828 }
32423829
3830
+ trace_android_vh_swapin_add_anon_rmap(vmf, page);
32433831 swap_free(entry);
32443832 if (mem_cgroup_swap_full(page) ||
3245
- (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
3833
+ (vmf->vma_flags & VM_LOCKED) || PageMlocked(page))
32463834 try_to_free_swap(page);
32473835 unlock_page(page);
32483836 if (page != swapcache && swapcache) {
....@@ -3272,7 +3860,6 @@
32723860 out:
32733861 return ret;
32743862 out_nomap:
3275
- mem_cgroup_cancel_charge(page, memcg, false);
32763863 pte_unmap_unlock(vmf->pte, vmf->ptl);
32773864 out_page:
32783865 unlock_page(page);
....@@ -3286,51 +3873,65 @@
32863873 }
32873874
32883875 /*
3289
- * We enter with non-exclusive mmap_sem (to exclude vma changes,
3876
+ * We enter with non-exclusive mmap_lock (to exclude vma changes,
32903877 * but allow concurrent faults), and pte mapped but not yet locked.
3291
- * We return with mmap_sem still held, but pte unmapped and unlocked.
3878
+ * We return with mmap_lock still held, but pte unmapped and unlocked.
32923879 */
32933880 static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
32943881 {
32953882 struct vm_area_struct *vma = vmf->vma;
3296
- struct mem_cgroup *memcg;
32973883 struct page *page;
32983884 vm_fault_t ret = 0;
32993885 pte_t entry;
33003886
33013887 /* File mapping without ->vm_ops ? */
3302
- if (vma->vm_flags & VM_SHARED)
3888
+ if (vmf->vma_flags & VM_SHARED)
33033889 return VM_FAULT_SIGBUS;
3890
+
3891
+ /* Do not check unstable pmd, if it's changed will retry later */
3892
+ if (vmf->flags & FAULT_FLAG_SPECULATIVE)
3893
+ goto skip_pmd_checks;
33043894
33053895 /*
33063896 * Use pte_alloc() instead of pte_alloc_map(). We can't run
33073897 * pte_offset_map() on pmds where a huge pmd might be created
33083898 * from a different thread.
33093899 *
3310
- * pte_alloc_map() is safe to use under down_write(mmap_sem) or when
3900
+ * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when
33113901 * parallel threads are excluded by other means.
33123902 *
3313
- * Here we only have down_read(mmap_sem).
3903
+ * Here we only have mmap_read_lock(mm).
33143904 */
3315
- if (pte_alloc(vma->vm_mm, vmf->pmd, vmf->address))
3905
+ if (pte_alloc(vma->vm_mm, vmf->pmd))
33163906 return VM_FAULT_OOM;
33173907
3318
- /* See the comment in pte_alloc_one_map() */
3908
+ /* See comment in handle_pte_fault() */
33193909 if (unlikely(pmd_trans_unstable(vmf->pmd)))
33203910 return 0;
33213911
3912
+skip_pmd_checks:
33223913 /* Use the zero-page for reads */
33233914 if (!(vmf->flags & FAULT_FLAG_WRITE) &&
33243915 !mm_forbids_zeropage(vma->vm_mm)) {
33253916 entry = pte_mkspecial(pfn_pte(my_zero_pfn(vmf->address),
3326
- vma->vm_page_prot));
3327
- vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
3328
- vmf->address, &vmf->ptl);
3329
- if (!pte_none(*vmf->pte))
3917
+ vmf->vma_page_prot));
3918
+ if (!pte_map_lock(vmf))
3919
+ return VM_FAULT_RETRY;
3920
+ if (!pte_none(*vmf->pte)) {
3921
+ update_mmu_tlb(vma, vmf->address, vmf->pte);
33303922 goto unlock;
3923
+ }
33313924 ret = check_stable_address_space(vma->vm_mm);
33323925 if (ret)
33333926 goto unlock;
3927
+ /*
3928
+ * Don't call the userfaultfd during the speculative path.
3929
+ * We already checked for the VMA to not be managed through
3930
+ * userfaultfd, but it may be set in our back once we have lock
3931
+ * the pte. In such a case we can ignore it this time.
3932
+ */
3933
+ if (vmf->flags & FAULT_FLAG_SPECULATIVE)
3934
+ goto setpte;
33343935 /* Deliver the page fault to userland, check inside PT lock */
33353936 if (userfaultfd_missing(vma)) {
33363937 pte_unmap_unlock(vmf->pte, vmf->ptl);
....@@ -3346,42 +3947,47 @@
33463947 if (!page)
33473948 goto oom;
33483949
3349
- if (mem_cgroup_try_charge_delay(page, vma->vm_mm, GFP_KERNEL, &memcg,
3350
- false))
3950
+ if (mem_cgroup_charge(page, vma->vm_mm, GFP_KERNEL))
33513951 goto oom_free_page;
3952
+ cgroup_throttle_swaprate(page, GFP_KERNEL);
33523953
33533954 /*
33543955 * The memory barrier inside __SetPageUptodate makes sure that
3355
- * preceeding stores to the page contents become visible before
3956
+ * preceding stores to the page contents become visible before
33563957 * the set_pte_at() write.
33573958 */
33583959 __SetPageUptodate(page);
33593960
3360
- entry = mk_pte(page, vma->vm_page_prot);
3361
- if (vma->vm_flags & VM_WRITE)
3961
+ entry = mk_pte(page, vmf->vma_page_prot);
3962
+ entry = pte_sw_mkyoung(entry);
3963
+ if (vmf->vma_flags & VM_WRITE)
33623964 entry = pte_mkwrite(pte_mkdirty(entry));
33633965
3364
- vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3365
- &vmf->ptl);
3366
- if (!pte_none(*vmf->pte))
3966
+ if (!pte_map_lock(vmf)) {
3967
+ ret = VM_FAULT_RETRY;
33673968 goto release;
3969
+ }
3970
+
3971
+ if (!pte_none(*vmf->pte)) {
3972
+ update_mmu_cache(vma, vmf->address, vmf->pte);
3973
+ goto unlock_and_release;
3974
+ }
33683975
33693976 ret = check_stable_address_space(vma->vm_mm);
33703977 if (ret)
3371
- goto release;
3978
+ goto unlock_and_release;
33723979
33733980 /* Deliver the page fault to userland, check inside PT lock */
3374
- if (userfaultfd_missing(vma)) {
3981
+ if (!(vmf->flags & FAULT_FLAG_SPECULATIVE) &&
3982
+ userfaultfd_missing(vma)) {
33753983 pte_unmap_unlock(vmf->pte, vmf->ptl);
3376
- mem_cgroup_cancel_charge(page, memcg, false);
33773984 put_page(page);
33783985 return handle_userfault(vmf, VM_UFFD_MISSING);
33793986 }
33803987
33813988 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
3382
- page_add_new_anon_rmap(page, vma, vmf->address, false);
3383
- mem_cgroup_commit_charge(page, memcg, false, false);
3384
- lru_cache_add_active_or_unevictable(page, vma);
3989
+ __page_add_new_anon_rmap(page, vma, vmf->address, false);
3990
+ __lru_cache_add_inactive_or_unevictable(page, vmf->vma_flags);
33853991 setpte:
33863992 set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);
33873993
....@@ -3390,10 +3996,11 @@
33903996 unlock:
33913997 pte_unmap_unlock(vmf->pte, vmf->ptl);
33923998 return ret;
3999
+unlock_and_release:
4000
+ pte_unmap_unlock(vmf->pte, vmf->ptl);
33934001 release:
3394
- mem_cgroup_cancel_charge(page, memcg, false);
33954002 put_page(page);
3396
- goto unlock;
4003
+ return ret;
33974004 oom_free_page:
33984005 put_page(page);
33994006 oom:
....@@ -3401,7 +4008,7 @@
34014008 }
34024009
34034010 /*
3404
- * The mmap_sem must have been held on entry, and may have been
4011
+ * The mmap_lock must have been held on entry, and may have been
34054012 * released depending on flags and vma->vm_ops->fault() return value.
34064013 * See filemap_fault() and __lock_page_retry().
34074014 */
....@@ -3409,6 +4016,10 @@
34094016 {
34104017 struct vm_area_struct *vma = vmf->vma;
34114018 vm_fault_t ret;
4019
+
4020
+ /* Do not check unstable pmd, if it's changed will retry later */
4021
+ if (vmf->flags & FAULT_FLAG_SPECULATIVE)
4022
+ goto skip_pmd_checks;
34124023
34134024 /*
34144025 * Preallocate pte before we take page_lock because this might lead to
....@@ -3418,7 +4029,7 @@
34184029 * unlock_page(A)
34194030 * lock_page(B)
34204031 * lock_page(B)
3421
- * pte_alloc_pne
4032
+ * pte_alloc_one
34224033 * shrink_page_list
34234034 * wait_on_page_writeback(A)
34244035 * SetPageWriteback(B)
....@@ -3426,24 +4037,33 @@
34264037 * # flush A, B to clear the writeback
34274038 */
34284039 if (pmd_none(*vmf->pmd) && !vmf->prealloc_pte) {
3429
- vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm,
3430
- vmf->address);
4040
+ vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
34314041 if (!vmf->prealloc_pte)
34324042 return VM_FAULT_OOM;
34334043 smp_wmb(); /* See comment in __pte_alloc() */
34344044 }
34354045
4046
+skip_pmd_checks:
34364047 ret = vma->vm_ops->fault(vmf);
34374048 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY |
34384049 VM_FAULT_DONE_COW)))
34394050 return ret;
34404051
34414052 if (unlikely(PageHWPoison(vmf->page))) {
3442
- if (ret & VM_FAULT_LOCKED)
3443
- unlock_page(vmf->page);
3444
- put_page(vmf->page);
4053
+ struct page *page = vmf->page;
4054
+ vm_fault_t poisonret = VM_FAULT_HWPOISON;
4055
+ if (ret & VM_FAULT_LOCKED) {
4056
+ if (page_mapped(page))
4057
+ unmap_mapping_pages(page_mapping(page),
4058
+ page->index, 1, false);
4059
+ /* Retry if a clean page was removed from the cache. */
4060
+ if (invalidate_inode_page(page))
4061
+ poisonret = VM_FAULT_NOPAGE;
4062
+ unlock_page(page);
4063
+ }
4064
+ put_page(page);
34454065 vmf->page = NULL;
3446
- return VM_FAULT_HWPOISON;
4066
+ return poisonret;
34474067 }
34484068
34494069 if (unlikely(!(ret & VM_FAULT_LOCKED)))
....@@ -3454,80 +4074,7 @@
34544074 return ret;
34554075 }
34564076
3457
-/*
3458
- * The ordering of these checks is important for pmds with _PAGE_DEVMAP set.
3459
- * If we check pmd_trans_unstable() first we will trip the bad_pmd() check
3460
- * inside of pmd_none_or_trans_huge_or_clear_bad(). This will end up correctly
3461
- * returning 1 but not before it spams dmesg with the pmd_clear_bad() output.
3462
- */
3463
-static int pmd_devmap_trans_unstable(pmd_t *pmd)
3464
-{
3465
- return pmd_devmap(*pmd) || pmd_trans_unstable(pmd);
3466
-}
3467
-
3468
-static vm_fault_t pte_alloc_one_map(struct vm_fault *vmf)
3469
-{
3470
- struct vm_area_struct *vma = vmf->vma;
3471
-
3472
- if (!pmd_none(*vmf->pmd))
3473
- goto map_pte;
3474
- if (vmf->prealloc_pte) {
3475
- vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
3476
- if (unlikely(!pmd_none(*vmf->pmd))) {
3477
- spin_unlock(vmf->ptl);
3478
- goto map_pte;
3479
- }
3480
-
3481
- mm_inc_nr_ptes(vma->vm_mm);
3482
- pmd_populate(vma->vm_mm, vmf->pmd, vmf->prealloc_pte);
3483
- spin_unlock(vmf->ptl);
3484
- vmf->prealloc_pte = NULL;
3485
- } else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd, vmf->address))) {
3486
- return VM_FAULT_OOM;
3487
- }
3488
-map_pte:
3489
- /*
3490
- * If a huge pmd materialized under us just retry later. Use
3491
- * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead of
3492
- * pmd_trans_huge() to ensure the pmd didn't become pmd_trans_huge
3493
- * under us and then back to pmd_none, as a result of MADV_DONTNEED
3494
- * running immediately after a huge pmd fault in a different thread of
3495
- * this mm, in turn leading to a misleading pmd_trans_huge() retval.
3496
- * All we have to ensure is that it is a regular pmd that we can walk
3497
- * with pte_offset_map() and we can do that through an atomic read in
3498
- * C, which is what pmd_trans_unstable() provides.
3499
- */
3500
- if (pmd_devmap_trans_unstable(vmf->pmd))
3501
- return VM_FAULT_NOPAGE;
3502
-
3503
- /*
3504
- * At this point we know that our vmf->pmd points to a page of ptes
3505
- * and it cannot become pmd_none(), pmd_devmap() or pmd_trans_huge()
3506
- * for the duration of the fault. If a racing MADV_DONTNEED runs and
3507
- * we zap the ptes pointed to by our vmf->pmd, the vmf->ptl will still
3508
- * be valid and we will re-check to make sure the vmf->pte isn't
3509
- * pte_none() under vmf->ptl protection when we return to
3510
- * alloc_set_pte().
3511
- */
3512
- vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
3513
- &vmf->ptl);
3514
- return 0;
3515
-}
3516
-
3517
-#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3518
-
3519
-#define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1)
3520
-static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
3521
- unsigned long haddr)
3522
-{
3523
- if (((vma->vm_start >> PAGE_SHIFT) & HPAGE_CACHE_INDEX_MASK) !=
3524
- (vma->vm_pgoff & HPAGE_CACHE_INDEX_MASK))
3525
- return false;
3526
- if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
3527
- return false;
3528
- return true;
3529
-}
3530
-
4077
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
35314078 static void deposit_prealloc_pte(struct vm_fault *vmf)
35324079 {
35334080 struct vm_area_struct *vma = vmf->vma;
....@@ -3541,27 +4088,28 @@
35414088 vmf->prealloc_pte = NULL;
35424089 }
35434090
3544
-static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
4091
+vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
35454092 {
35464093 struct vm_area_struct *vma = vmf->vma;
35474094 bool write = vmf->flags & FAULT_FLAG_WRITE;
35484095 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
35494096 pmd_t entry;
35504097 int i;
3551
- vm_fault_t ret;
4098
+ vm_fault_t ret = VM_FAULT_FALLBACK;
35524099
35534100 if (!transhuge_vma_suitable(vma, haddr))
3554
- return VM_FAULT_FALLBACK;
4101
+ return ret;
35554102
3556
- ret = VM_FAULT_FALLBACK;
35574103 page = compound_head(page);
4104
+ if (compound_order(page) != HPAGE_PMD_ORDER)
4105
+ return ret;
35584106
35594107 /*
35604108 * Archs like ppc64 need additonal space to store information
35614109 * related to pte entry. Use the preallocated table for that.
35624110 */
35634111 if (arch_needs_pgtable_deposit() && !vmf->prealloc_pte) {
3564
- vmf->prealloc_pte = pte_alloc_one(vma->vm_mm, vmf->address);
4112
+ vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
35654113 if (!vmf->prealloc_pte)
35664114 return VM_FAULT_OOM;
35674115 smp_wmb(); /* See comment in __pte_alloc() */
....@@ -3574,7 +4122,7 @@
35744122 for (i = 0; i < HPAGE_PMD_NR; i++)
35754123 flush_icache_page(vma, page + i);
35764124
3577
- entry = mk_huge_pmd(page, vma->vm_page_prot);
4125
+ entry = mk_huge_pmd(page, vmf->vma_page_prot);
35784126 if (write)
35794127 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
35804128
....@@ -3598,77 +4146,40 @@
35984146 return ret;
35994147 }
36004148 #else
3601
-static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
4149
+vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
36024150 {
3603
- BUILD_BUG();
3604
- return 0;
4151
+ return VM_FAULT_FALLBACK;
36054152 }
36064153 #endif
36074154
3608
-/**
3609
- * alloc_set_pte - setup new PTE entry for given page and add reverse page
3610
- * mapping. If needed, the fucntion allocates page table or use pre-allocated.
3611
- *
3612
- * @vmf: fault environment
3613
- * @memcg: memcg to charge page (only for private mappings)
3614
- * @page: page to map
3615
- *
3616
- * Caller must take care of unlocking vmf->ptl, if vmf->pte is non-NULL on
3617
- * return.
3618
- *
3619
- * Target users are page handler itself and implementations of
3620
- * vm_ops->map_pages.
3621
- */
3622
-vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg,
3623
- struct page *page)
4155
+void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr)
36244156 {
36254157 struct vm_area_struct *vma = vmf->vma;
36264158 bool write = vmf->flags & FAULT_FLAG_WRITE;
4159
+ bool prefault = vmf->address != addr;
36274160 pte_t entry;
3628
- vm_fault_t ret;
3629
-
3630
- if (pmd_none(*vmf->pmd) && PageTransCompound(page) &&
3631
- IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
3632
- /* THP on COW? */
3633
- VM_BUG_ON_PAGE(memcg, page);
3634
-
3635
- ret = do_set_pmd(vmf, page);
3636
- if (ret != VM_FAULT_FALLBACK)
3637
- return ret;
3638
- }
3639
-
3640
- if (!vmf->pte) {
3641
- ret = pte_alloc_one_map(vmf);
3642
- if (ret)
3643
- return ret;
3644
- }
3645
-
3646
- /* Re-check under ptl */
3647
- if (unlikely(!pte_none(*vmf->pte)))
3648
- return VM_FAULT_NOPAGE;
36494161
36504162 flush_icache_page(vma, page);
3651
- entry = mk_pte(page, vma->vm_page_prot);
4163
+ entry = mk_pte(page, vmf->vma_page_prot);
4164
+
4165
+ if (prefault && arch_wants_old_prefaulted_pte())
4166
+ entry = pte_mkold(entry);
4167
+ else
4168
+ entry = pte_sw_mkyoung(entry);
4169
+
36524170 if (write)
3653
- entry = maybe_mkwrite(pte_mkdirty(entry), vma);
4171
+ entry = maybe_mkwrite(pte_mkdirty(entry), vmf->vma_flags);
36544172 /* copy-on-write page */
3655
- if (write && !(vma->vm_flags & VM_SHARED)) {
4173
+ if (write && !(vmf->vma_flags & VM_SHARED)) {
36564174 inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
3657
- page_add_new_anon_rmap(page, vma, vmf->address, false);
3658
- mem_cgroup_commit_charge(page, memcg, false, false);
3659
- lru_cache_add_active_or_unevictable(page, vma);
4175
+ __page_add_new_anon_rmap(page, vma, addr, false);
4176
+ __lru_cache_add_inactive_or_unevictable(page, vmf->vma_flags);
36604177 } else {
36614178 inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
36624179 page_add_file_rmap(page, false);
36634180 }
3664
- set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);
3665
-
3666
- /* no need to invalidate: a not-present page won't be cached */
3667
- update_mmu_cache(vma, vmf->address, vmf->pte);
3668
-
3669
- return 0;
4181
+ set_pte_at(vma->vm_mm, addr, vmf->pte, entry);
36704182 }
3671
-
36724183
36734184 /**
36744185 * finish_fault - finish page fault once we have prepared the page to fault
....@@ -3678,20 +4189,22 @@
36784189 * This function handles all that is needed to finish a page fault once the
36794190 * page to fault in is prepared. It handles locking of PTEs, inserts PTE for
36804191 * given page, adds reverse page mapping, handles memcg charges and LRU
3681
- * addition. The function returns 0 on success, VM_FAULT_ code in case of
3682
- * error.
4192
+ * addition.
36834193 *
36844194 * The function expects the page to be locked and on success it consumes a
36854195 * reference of a page being mapped (for the PTE which maps it).
4196
+ *
4197
+ * Return: %0 on success, %VM_FAULT_ code in case of error.
36864198 */
36874199 vm_fault_t finish_fault(struct vm_fault *vmf)
36884200 {
4201
+ struct vm_area_struct *vma = vmf->vma;
36894202 struct page *page;
3690
- vm_fault_t ret = 0;
4203
+ vm_fault_t ret;
36914204
36924205 /* Did we COW the page? */
36934206 if ((vmf->flags & FAULT_FLAG_WRITE) &&
3694
- !(vmf->vma->vm_flags & VM_SHARED))
4207
+ !(vmf->vma_flags & VM_SHARED))
36954208 page = vmf->cow_page;
36964209 else
36974210 page = vmf->page;
....@@ -3700,12 +4213,56 @@
37004213 * check even for read faults because we might have lost our CoWed
37014214 * page
37024215 */
3703
- if (!(vmf->vma->vm_flags & VM_SHARED))
3704
- ret = check_stable_address_space(vmf->vma->vm_mm);
3705
- if (!ret)
3706
- ret = alloc_set_pte(vmf, vmf->memcg, page);
3707
- if (vmf->pte)
3708
- pte_unmap_unlock(vmf->pte, vmf->ptl);
4216
+ if (!(vma->vm_flags & VM_SHARED)) {
4217
+ ret = check_stable_address_space(vma->vm_mm);
4218
+ if (ret)
4219
+ return ret;
4220
+ }
4221
+
4222
+ /* Do not check unstable pmd, if it's changed will retry later */
4223
+ if (vmf->flags & FAULT_FLAG_SPECULATIVE)
4224
+ goto skip_pmd_checks;
4225
+
4226
+ if (pmd_none(*vmf->pmd)) {
4227
+ if (PageTransCompound(page)) {
4228
+ ret = do_set_pmd(vmf, page);
4229
+ if (ret != VM_FAULT_FALLBACK)
4230
+ return ret;
4231
+ }
4232
+
4233
+ if (vmf->prealloc_pte) {
4234
+ vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
4235
+ if (likely(pmd_none(*vmf->pmd))) {
4236
+ mm_inc_nr_ptes(vma->vm_mm);
4237
+ pmd_populate(vma->vm_mm, vmf->pmd, vmf->prealloc_pte);
4238
+ vmf->prealloc_pte = NULL;
4239
+ }
4240
+ spin_unlock(vmf->ptl);
4241
+ } else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd))) {
4242
+ return VM_FAULT_OOM;
4243
+ }
4244
+ }
4245
+
4246
+ /*
4247
+ * See comment in handle_pte_fault() for how this scenario happens, we
4248
+ * need to return NOPAGE so that we drop this page.
4249
+ */
4250
+ if (pmd_devmap_trans_unstable(vmf->pmd))
4251
+ return VM_FAULT_NOPAGE;
4252
+
4253
+skip_pmd_checks:
4254
+ if (!pte_map_lock(vmf))
4255
+ return VM_FAULT_RETRY;
4256
+
4257
+ ret = 0;
4258
+ /* Re-check under ptl */
4259
+ if (likely(pte_none(*vmf->pte)))
4260
+ do_set_pte(vmf, page, vmf->address);
4261
+ else
4262
+ ret = VM_FAULT_NOPAGE;
4263
+
4264
+ update_mmu_tlb(vma, vmf->address, vmf->pte);
4265
+ pte_unmap_unlock(vmf->pte, vmf->ptl);
37094266 return ret;
37104267 }
37114268
....@@ -3738,12 +4295,8 @@
37384295
37394296 static int __init fault_around_debugfs(void)
37404297 {
3741
- void *ret;
3742
-
3743
- ret = debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL,
3744
- &fault_around_bytes_fops);
3745
- if (!ret)
3746
- pr_warn("Failed to create fault_around_bytes in debugfs");
4298
+ debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL,
4299
+ &fault_around_bytes_fops);
37474300 return 0;
37484301 }
37494302 late_initcall(fault_around_debugfs);
....@@ -3779,13 +4332,12 @@
37794332 pgoff_t start_pgoff = vmf->pgoff;
37804333 pgoff_t end_pgoff;
37814334 int off;
3782
- vm_fault_t ret = 0;
37834335
37844336 nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT;
37854337 mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
37864338
3787
- vmf->address = max(address & mask, vmf->vma->vm_start);
3788
- off = ((address - vmf->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
4339
+ address = max(address & mask, vmf->vma->vm_start);
4340
+ off = ((vmf->address - address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
37894341 start_pgoff -= off;
37904342
37914343 /*
....@@ -3793,40 +4345,20 @@
37934345 * the vma or nr_pages from start_pgoff, depending what is nearest.
37944346 */
37954347 end_pgoff = start_pgoff -
3796
- ((vmf->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
4348
+ ((address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
37974349 PTRS_PER_PTE - 1;
37984350 end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1,
37994351 start_pgoff + nr_pages - 1);
38004352
3801
- if (pmd_none(*vmf->pmd)) {
3802
- vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm,
3803
- vmf->address);
4353
+ if (!(vmf->flags & FAULT_FLAG_SPECULATIVE) &&
4354
+ pmd_none(*vmf->pmd)) {
4355
+ vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm);
38044356 if (!vmf->prealloc_pte)
3805
- goto out;
4357
+ return VM_FAULT_OOM;
38064358 smp_wmb(); /* See comment in __pte_alloc() */
38074359 }
38084360
3809
- vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff);
3810
-
3811
- /* Huge page is mapped? Page fault is solved */
3812
- if (pmd_trans_huge(*vmf->pmd)) {
3813
- ret = VM_FAULT_NOPAGE;
3814
- goto out;
3815
- }
3816
-
3817
- /* ->map_pages() haven't done anything useful. Cold page cache? */
3818
- if (!vmf->pte)
3819
- goto out;
3820
-
3821
- /* check if the page fault is solved */
3822
- vmf->pte -= (vmf->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT);
3823
- if (!pte_none(*vmf->pte))
3824
- ret = VM_FAULT_NOPAGE;
3825
- pte_unmap_unlock(vmf->pte, vmf->ptl);
3826
-out:
3827
- vmf->address = address;
3828
- vmf->pte = NULL;
3829
- return ret;
4361
+ return vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff);
38304362 }
38314363
38324364 static vm_fault_t do_read_fault(struct vm_fault *vmf)
....@@ -3840,9 +4372,11 @@
38404372 * something).
38414373 */
38424374 if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) {
3843
- ret = do_fault_around(vmf);
3844
- if (ret)
3845
- return ret;
4375
+ if (likely(!userfaultfd_minor(vmf->vma))) {
4376
+ ret = do_fault_around(vmf);
4377
+ if (ret)
4378
+ return ret;
4379
+ }
38464380 }
38474381
38484382 ret = __do_fault(vmf);
....@@ -3868,11 +4402,11 @@
38684402 if (!vmf->cow_page)
38694403 return VM_FAULT_OOM;
38704404
3871
- if (mem_cgroup_try_charge_delay(vmf->cow_page, vma->vm_mm, GFP_KERNEL,
3872
- &vmf->memcg, false)) {
4405
+ if (mem_cgroup_charge(vmf->cow_page, vma->vm_mm, GFP_KERNEL)) {
38734406 put_page(vmf->cow_page);
38744407 return VM_FAULT_OOM;
38754408 }
4409
+ cgroup_throttle_swaprate(vmf->cow_page, GFP_KERNEL);
38764410
38774411 ret = __do_fault(vmf);
38784412 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
....@@ -3890,7 +4424,6 @@
38904424 goto uncharge_out;
38914425 return ret;
38924426 uncharge_out:
3893
- mem_cgroup_cancel_charge(vmf->cow_page, vmf->memcg, false);
38944427 put_page(vmf->cow_page);
38954428 return ret;
38964429 }
....@@ -3926,16 +4459,16 @@
39264459 return ret;
39274460 }
39284461
3929
- fault_dirty_shared_page(vma, vmf->page);
4462
+ ret |= fault_dirty_shared_page(vmf);
39304463 return ret;
39314464 }
39324465
39334466 /*
3934
- * We enter with non-exclusive mmap_sem (to exclude vma changes,
4467
+ * We enter with non-exclusive mmap_lock (to exclude vma changes,
39354468 * but allow concurrent faults).
3936
- * The mmap_sem may have been released depending on flags and our
4469
+ * The mmap_lock may have been released depending on flags and our
39374470 * return value. See filemap_fault() and __lock_page_or_retry().
3938
- * If mmap_sem is released, vma may become invalid (for example
4471
+ * If mmap_lock is released, vma may become invalid (for example
39394472 * by other thread calling munmap()).
39404473 */
39414474 static vm_fault_t do_fault(struct vm_fault *vmf)
....@@ -3975,7 +4508,7 @@
39754508 }
39764509 } else if (!(vmf->flags & FAULT_FLAG_WRITE))
39774510 ret = do_read_fault(vmf);
3978
- else if (!(vma->vm_flags & VM_SHARED))
4511
+ else if (!(vmf->vma_flags & VM_SHARED))
39794512 ret = do_cow_fault(vmf);
39804513 else
39814514 ret = do_shared_fault(vmf);
....@@ -4007,11 +4540,11 @@
40074540 {
40084541 struct vm_area_struct *vma = vmf->vma;
40094542 struct page *page = NULL;
4010
- int page_nid = -1;
4543
+ int page_nid = NUMA_NO_NODE;
40114544 int last_cpupid;
40124545 int target_nid;
40134546 bool migrated = false;
4014
- pte_t pte;
4547
+ pte_t pte, old_pte;
40154548 bool was_writable = pte_savedwrite(vmf->orig_pte);
40164549 int flags = 0;
40174550
....@@ -4020,8 +4553,8 @@
40204553 * validation through pte_unmap_same(). It's of NUMA type but
40214554 * the pfn may be screwed if the read is non atomic.
40224555 */
4023
- vmf->ptl = pte_lockptr(vma->vm_mm, vmf->pmd);
4024
- spin_lock(vmf->ptl);
4556
+ if (!pte_spinlock(vmf))
4557
+ return VM_FAULT_RETRY;
40254558 if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) {
40264559 pte_unmap_unlock(vmf->pte, vmf->ptl);
40274560 goto out;
....@@ -4031,15 +4564,15 @@
40314564 * Make it present again, Depending on how arch implementes non
40324565 * accessible ptes, some can allow access by kernel mode.
40334566 */
4034
- pte = ptep_modify_prot_start(vma->vm_mm, vmf->address, vmf->pte);
4035
- pte = pte_modify(pte, vma->vm_page_prot);
4567
+ old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte);
4568
+ pte = pte_modify(old_pte, vmf->vma_page_prot);
40364569 pte = pte_mkyoung(pte);
40374570 if (was_writable)
40384571 pte = pte_mkwrite(pte);
4039
- ptep_modify_prot_commit(vma->vm_mm, vmf->address, vmf->pte, pte);
4572
+ ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte);
40404573 update_mmu_cache(vma, vmf->address, vmf->pte);
40414574
4042
- page = vm_normal_page(vma, vmf->address, pte);
4575
+ page = _vm_normal_page(vma, vmf->address, pte, vmf->vma_flags);
40434576 if (!page) {
40444577 pte_unmap_unlock(vmf->pte, vmf->ptl);
40454578 return 0;
....@@ -4066,7 +4599,7 @@
40664599 * Flag if the page is shared between multiple address spaces. This
40674600 * is later used when determining whether to group tasks together
40684601 */
4069
- if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED))
4602
+ if (page_mapcount(page) > 1 && (vmf->vma_flags & VM_SHARED))
40704603 flags |= TNF_SHARED;
40714604
40724605 last_cpupid = page_cpupid_last(page);
....@@ -4074,13 +4607,13 @@
40744607 target_nid = numa_migrate_prep(page, vma, vmf->address, page_nid,
40754608 &flags);
40764609 pte_unmap_unlock(vmf->pte, vmf->ptl);
4077
- if (target_nid == -1) {
4610
+ if (target_nid == NUMA_NO_NODE) {
40784611 put_page(page);
40794612 goto out;
40804613 }
40814614
40824615 /* Migrate to the requested node */
4083
- migrated = migrate_misplaced_page(page, vma, target_nid);
4616
+ migrated = migrate_misplaced_page(page, vmf, target_nid);
40844617 if (migrated) {
40854618 page_nid = target_nid;
40864619 flags |= TNF_MIGRATED;
....@@ -4088,7 +4621,7 @@
40884621 flags |= TNF_MIGRATE_FAIL;
40894622
40904623 out:
4091
- if (page_nid != -1)
4624
+ if (page_nid != NUMA_NO_NODE)
40924625 task_numa_fault(last_cpupid, page_nid, 1, flags);
40934626 return 0;
40944627 }
....@@ -4105,26 +4638,28 @@
41054638 /* `inline' is required to avoid gcc 4.1.2 build error */
41064639 static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd)
41074640 {
4108
- if (vma_is_anonymous(vmf->vma))
4641
+ if (vma_is_anonymous(vmf->vma)) {
4642
+ if (userfaultfd_huge_pmd_wp(vmf->vma, orig_pmd))
4643
+ return handle_userfault(vmf, VM_UFFD_WP);
41094644 return do_huge_pmd_wp_page(vmf, orig_pmd);
4110
- if (vmf->vma->vm_ops->huge_fault)
4111
- return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);
4645
+ }
4646
+ if (vmf->vma->vm_ops->huge_fault) {
4647
+ vm_fault_t ret = vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PMD);
41124648
4113
- /* COW handled on pte level: split pmd */
4114
- VM_BUG_ON_VMA(vmf->vma->vm_flags & VM_SHARED, vmf->vma);
4649
+ if (!(ret & VM_FAULT_FALLBACK))
4650
+ return ret;
4651
+ }
4652
+
4653
+ /* COW or write-notify handled on pte level: split pmd. */
41154654 __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL);
41164655
41174656 return VM_FAULT_FALLBACK;
41184657 }
41194658
4120
-static inline bool vma_is_accessible(struct vm_area_struct *vma)
4121
-{
4122
- return vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE);
4123
-}
4124
-
41254659 static vm_fault_t create_huge_pud(struct vm_fault *vmf)
41264660 {
4127
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
4661
+#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \
4662
+ defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
41284663 /* No support for anonymous transparent PUD pages yet */
41294664 if (vma_is_anonymous(vmf->vma))
41304665 return VM_FAULT_FALLBACK;
....@@ -4136,13 +4671,21 @@
41364671
41374672 static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud)
41384673 {
4139
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
4674
+#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \
4675
+ defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
41404676 /* No support for anonymous transparent PUD pages yet */
41414677 if (vma_is_anonymous(vmf->vma))
4142
- return VM_FAULT_FALLBACK;
4143
- if (vmf->vma->vm_ops->huge_fault)
4144
- return vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD);
4145
-#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
4678
+ goto split;
4679
+ if (vmf->vma->vm_ops->huge_fault) {
4680
+ vm_fault_t ret = vmf->vma->vm_ops->huge_fault(vmf, PE_SIZE_PUD);
4681
+
4682
+ if (!(ret & VM_FAULT_FALLBACK))
4683
+ return ret;
4684
+ }
4685
+split:
4686
+ /* COW or write-notify not handled on PUD level: split pud.*/
4687
+ __split_huge_pud(vmf->vma, vmf->pud, vmf->address);
4688
+#endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
41464689 return VM_FAULT_FALLBACK;
41474690 }
41484691
....@@ -4155,15 +4698,20 @@
41554698 * with external mmu caches can use to update those (ie the Sparc or
41564699 * PowerPC hashed page tables that act as extended TLBs).
41574700 *
4158
- * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow
4701
+ * We enter with non-exclusive mmap_lock (to exclude vma changes, but allow
41594702 * concurrent faults).
41604703 *
4161
- * The mmap_sem may have been released depending on flags and our return value.
4704
+ * The mmap_lock may have been released depending on flags and our return value.
41624705 * See filemap_fault() and __lock_page_or_retry().
41634706 */
41644707 static vm_fault_t handle_pte_fault(struct vm_fault *vmf)
41654708 {
41664709 pte_t entry;
4710
+ vm_fault_t ret = 0;
4711
+
4712
+ /* Do not check unstable pmd, if it's changed will retry later */
4713
+ if (vmf->flags & FAULT_FLAG_SPECULATIVE)
4714
+ goto skip_pmd_checks;
41674715
41684716 if (unlikely(pmd_none(*vmf->pmd))) {
41694717 /*
....@@ -4174,14 +4722,28 @@
41744722 */
41754723 vmf->pte = NULL;
41764724 } else {
4177
- /* See comment in pte_alloc_one_map() */
4725
+ /*
4726
+ * If a huge pmd materialized under us just retry later. Use
4727
+ * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead
4728
+ * of pmd_trans_huge() to ensure the pmd didn't become
4729
+ * pmd_trans_huge under us and then back to pmd_none, as a
4730
+ * result of MADV_DONTNEED running immediately after a huge pmd
4731
+ * fault in a different thread of this mm, in turn leading to a
4732
+ * misleading pmd_trans_huge() retval. All we have to ensure is
4733
+ * that it is a regular pmd that we can walk with
4734
+ * pte_offset_map() and we can do that through an atomic read
4735
+ * in C, which is what pmd_trans_unstable() provides.
4736
+ */
41784737 if (pmd_devmap_trans_unstable(vmf->pmd))
41794738 return 0;
41804739 /*
41814740 * A regular pmd is established and it can't morph into a huge
41824741 * pmd from under us anymore at this point because we hold the
4183
- * mmap_sem read mode and khugepaged takes it in write mode.
4742
+ * mmap_lock read mode and khugepaged takes it in write mode.
41844743 * So now it's safe to run pte_offset_map().
4744
+ * This is not applicable to the speculative page fault handler
4745
+ * but in that case, the pte is fetched earlier in
4746
+ * handle_speculative_fault().
41854747 */
41864748 vmf->pte = pte_offset_map(vmf->pmd, vmf->address);
41874749 vmf->orig_pte = *vmf->pte;
....@@ -4201,9 +4763,13 @@
42014763 }
42024764 }
42034765
4766
+skip_pmd_checks:
42044767 if (!vmf->pte) {
42054768 if (vma_is_anonymous(vmf->vma))
42064769 return do_anonymous_page(vmf);
4770
+ else if ((vmf->flags & FAULT_FLAG_SPECULATIVE) &&
4771
+ !vmf_allows_speculation(vmf))
4772
+ return VM_FAULT_RETRY;
42074773 else
42084774 return do_fault(vmf);
42094775 }
....@@ -4214,14 +4780,27 @@
42144780 if (pte_protnone(vmf->orig_pte) && vma_is_accessible(vmf->vma))
42154781 return do_numa_page(vmf);
42164782
4217
- vmf->ptl = pte_lockptr(vmf->vma->vm_mm, vmf->pmd);
4218
- spin_lock(vmf->ptl);
4783
+ if (!pte_spinlock(vmf))
4784
+ return VM_FAULT_RETRY;
42194785 entry = vmf->orig_pte;
4220
- if (unlikely(!pte_same(*vmf->pte, entry)))
4786
+ if (unlikely(!pte_same(*vmf->pte, entry))) {
4787
+ update_mmu_tlb(vmf->vma, vmf->address, vmf->pte);
42214788 goto unlock;
4789
+ }
42224790 if (vmf->flags & FAULT_FLAG_WRITE) {
4223
- if (!pte_write(entry))
4224
- return do_wp_page(vmf);
4791
+ if (!pte_write(entry)) {
4792
+ if (!(vmf->flags & FAULT_FLAG_SPECULATIVE))
4793
+ return do_wp_page(vmf);
4794
+
4795
+ if (!mmu_notifier_trylock(vmf->vma->vm_mm)) {
4796
+ ret = VM_FAULT_RETRY;
4797
+ goto unlock;
4798
+ }
4799
+
4800
+ ret = do_wp_page(vmf);
4801
+ mmu_notifier_unlock(vmf->vma->vm_mm);
4802
+ return ret;
4803
+ }
42254804 entry = pte_mkdirty(entry);
42264805 }
42274806 entry = pte_mkyoung(entry);
....@@ -4229,6 +4808,11 @@
42294808 vmf->flags & FAULT_FLAG_WRITE)) {
42304809 update_mmu_cache(vmf->vma, vmf->address, vmf->pte);
42314810 } else {
4811
+ /* Skip spurious TLB flush for retried page fault */
4812
+ if (vmf->flags & FAULT_FLAG_TRIED)
4813
+ goto unlock;
4814
+ if (vmf->flags & FAULT_FLAG_SPECULATIVE)
4815
+ ret = VM_FAULT_RETRY;
42324816 /*
42334817 * This is needed only for protection faults but the arch code
42344818 * is not yet telling us if this is a protection fault or not.
....@@ -4238,15 +4822,17 @@
42384822 if (vmf->flags & FAULT_FLAG_WRITE)
42394823 flush_tlb_fix_spurious_fault(vmf->vma, vmf->address);
42404824 }
4825
+ trace_android_rvh_handle_pte_fault_end(vmf, highest_memmap_pfn);
4826
+ trace_android_vh_handle_pte_fault_end(vmf, highest_memmap_pfn);
42414827 unlock:
42424828 pte_unmap_unlock(vmf->pte, vmf->ptl);
4243
- return 0;
4829
+ return ret;
42444830 }
42454831
42464832 /*
42474833 * By the time we get here, we already hold the mm semaphore
42484834 *
4249
- * The mmap_sem may have been released depending on flags and our
4835
+ * The mmap_lock may have been released depending on flags and our
42504836 * return value. See filemap_fault() and __lock_page_or_retry().
42514837 */
42524838 static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
....@@ -4258,6 +4844,8 @@
42584844 .flags = flags,
42594845 .pgoff = linear_page_index(vma, address),
42604846 .gfp_mask = __get_fault_gfp_mask(vma),
4847
+ .vma_flags = vma->vm_flags,
4848
+ .vma_page_prot = vma->vm_page_prot,
42614849 };
42624850 unsigned int dirty = flags & FAULT_FLAG_WRITE;
42634851 struct mm_struct *mm = vma->vm_mm;
....@@ -4273,6 +4861,7 @@
42734861 vmf.pud = pud_alloc(mm, p4d, address);
42744862 if (!vmf.pud)
42754863 return VM_FAULT_OOM;
4864
+retry_pud:
42764865 if (pud_none(*vmf.pud) && __transparent_hugepage_enabled(vma)) {
42774866 ret = create_huge_pud(&vmf);
42784867 if (!(ret & VM_FAULT_FALLBACK))
....@@ -4299,6 +4888,14 @@
42994888 vmf.pmd = pmd_alloc(mm, vmf.pud, address);
43004889 if (!vmf.pmd)
43014890 return VM_FAULT_OOM;
4891
+
4892
+ /* Huge pud page fault raced with pmd_alloc? */
4893
+ if (pud_trans_unstable(vmf.pud))
4894
+ goto retry_pud;
4895
+
4896
+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
4897
+ vmf.sequence = raw_read_seqcount(&vma->vm_sequence);
4898
+#endif
43024899 if (pmd_none(*vmf.pmd) && __transparent_hugepage_enabled(vma)) {
43034900 ret = create_huge_pmd(&vmf);
43044901 if (!(ret & VM_FAULT_FALLBACK))
....@@ -4332,14 +4929,342 @@
43324929 return handle_pte_fault(&vmf);
43334930 }
43344931
4932
+/**
4933
+ * mm_account_fault - Do page fault accountings
4934
+ *
4935
+ * @regs: the pt_regs struct pointer. When set to NULL, will skip accounting
4936
+ * of perf event counters, but we'll still do the per-task accounting to
4937
+ * the task who triggered this page fault.
4938
+ * @address: the faulted address.
4939
+ * @flags: the fault flags.
4940
+ * @ret: the fault retcode.
4941
+ *
4942
+ * This will take care of most of the page fault accountings. Meanwhile, it
4943
+ * will also include the PERF_COUNT_SW_PAGE_FAULTS_[MAJ|MIN] perf counter
4944
+ * updates. However note that the handling of PERF_COUNT_SW_PAGE_FAULTS should
4945
+ * still be in per-arch page fault handlers at the entry of page fault.
4946
+ */
4947
+static inline void mm_account_fault(struct pt_regs *regs,
4948
+ unsigned long address, unsigned int flags,
4949
+ vm_fault_t ret)
4950
+{
4951
+ bool major;
4952
+
4953
+ /*
4954
+ * We don't do accounting for some specific faults:
4955
+ *
4956
+ * - Unsuccessful faults (e.g. when the address wasn't valid). That
4957
+ * includes arch_vma_access_permitted() failing before reaching here.
4958
+ * So this is not a "this many hardware page faults" counter. We
4959
+ * should use the hw profiling for that.
4960
+ *
4961
+ * - Incomplete faults (VM_FAULT_RETRY). They will only be counted
4962
+ * once they're completed.
4963
+ */
4964
+ if (ret & (VM_FAULT_ERROR | VM_FAULT_RETRY))
4965
+ return;
4966
+
4967
+ /*
4968
+ * We define the fault as a major fault when the final successful fault
4969
+ * is VM_FAULT_MAJOR, or if it retried (which implies that we couldn't
4970
+ * handle it immediately previously).
4971
+ */
4972
+ major = (ret & VM_FAULT_MAJOR) || (flags & FAULT_FLAG_TRIED);
4973
+
4974
+ if (major)
4975
+ current->maj_flt++;
4976
+ else
4977
+ current->min_flt++;
4978
+
4979
+ /*
4980
+ * If the fault is done for GUP, regs will be NULL. We only do the
4981
+ * accounting for the per thread fault counters who triggered the
4982
+ * fault, and we skip the perf event updates.
4983
+ */
4984
+ if (!regs)
4985
+ return;
4986
+
4987
+ if (major)
4988
+ perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
4989
+ else
4990
+ perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
4991
+}
4992
+#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
4993
+
4994
+#ifndef CONFIG_ARCH_HAS_PTE_SPECIAL
4995
+/* This is required by vm_normal_page() */
4996
+#error "Speculative page fault handler requires CONFIG_ARCH_HAS_PTE_SPECIAL"
4997
+#endif
4998
+/*
4999
+ * vm_normal_page() adds some processing which should be done while
5000
+ * hodling the mmap_sem.
5001
+ */
5002
+
5003
+/*
5004
+ * Tries to handle the page fault in a speculative way, without grabbing the
5005
+ * mmap_sem.
5006
+ * When VM_FAULT_RETRY is returned, the vma pointer is valid and this vma must
5007
+ * be checked later when the mmap_sem has been grabbed by calling
5008
+ * can_reuse_spf_vma().
5009
+ * This is needed as the returned vma is kept in memory until the call to
5010
+ * can_reuse_spf_vma() is made.
5011
+ */
5012
+static vm_fault_t ___handle_speculative_fault(struct mm_struct *mm,
5013
+ unsigned long address, unsigned int flags,
5014
+ struct vm_area_struct *vma)
5015
+{
5016
+ struct vm_fault vmf = {
5017
+ .address = address,
5018
+ .pgoff = linear_page_index(vma, address),
5019
+ .vma = vma,
5020
+ .gfp_mask = __get_fault_gfp_mask(vma),
5021
+ .flags = flags,
5022
+ };
5023
+#ifdef CONFIG_NUMA
5024
+ struct mempolicy *pol;
5025
+#endif
5026
+ pgd_t *pgd, pgdval;
5027
+ p4d_t *p4d, p4dval;
5028
+ pud_t pudval;
5029
+ int seq;
5030
+ vm_fault_t ret;
5031
+
5032
+ /* Clear flags that may lead to release the mmap_sem to retry */
5033
+ flags &= ~(FAULT_FLAG_ALLOW_RETRY|FAULT_FLAG_KILLABLE);
5034
+ flags |= FAULT_FLAG_SPECULATIVE;
5035
+
5036
+ /* rmb <-> seqlock,vma_rb_erase() */
5037
+ seq = raw_read_seqcount(&vmf.vma->vm_sequence);
5038
+ if (seq & 1) {
5039
+ trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
5040
+ return VM_FAULT_RETRY;
5041
+ }
5042
+
5043
+ if (!vmf_allows_speculation(&vmf))
5044
+ return VM_FAULT_RETRY;
5045
+
5046
+ vmf.vma_flags = READ_ONCE(vmf.vma->vm_flags);
5047
+ vmf.vma_page_prot = READ_ONCE(vmf.vma->vm_page_prot);
5048
+
5049
+#ifdef CONFIG_USERFAULTFD
5050
+ /* Can't call userland page fault handler in the speculative path */
5051
+ if (unlikely(vmf.vma_flags & __VM_UFFD_FLAGS)) {
5052
+ trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
5053
+ return VM_FAULT_RETRY;
5054
+ }
5055
+#endif
5056
+
5057
+ if (vmf.vma_flags & VM_GROWSDOWN || vmf.vma_flags & VM_GROWSUP) {
5058
+ /*
5059
+ * This could be detected by the check address against VMA's
5060
+ * boundaries but we want to trace it as not supported instead
5061
+ * of changed.
5062
+ */
5063
+ trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
5064
+ return VM_FAULT_RETRY;
5065
+ }
5066
+
5067
+ if (address < READ_ONCE(vmf.vma->vm_start)
5068
+ || READ_ONCE(vmf.vma->vm_end) <= address) {
5069
+ trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
5070
+ return VM_FAULT_RETRY;
5071
+ }
5072
+
5073
+ if (!arch_vma_access_permitted(vmf.vma, flags & FAULT_FLAG_WRITE,
5074
+ flags & FAULT_FLAG_INSTRUCTION,
5075
+ flags & FAULT_FLAG_REMOTE))
5076
+ goto out_segv;
5077
+
5078
+ /* This is one is required to check that the VMA has write access set */
5079
+ if (flags & FAULT_FLAG_WRITE) {
5080
+ if (unlikely(!(vmf.vma_flags & VM_WRITE)))
5081
+ goto out_segv;
5082
+ } else if (unlikely(!(vmf.vma_flags & (VM_READ|VM_EXEC|VM_WRITE))))
5083
+ goto out_segv;
5084
+
5085
+#ifdef CONFIG_NUMA
5086
+ /*
5087
+ * MPOL_INTERLEAVE implies additional checks in
5088
+ * mpol_misplaced() which are not compatible with the
5089
+ *speculative page fault processing.
5090
+ */
5091
+ pol = __get_vma_policy(vmf.vma, address);
5092
+ if (!pol)
5093
+ pol = get_task_policy(current);
5094
+ if (pol && pol->mode == MPOL_INTERLEAVE) {
5095
+ trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
5096
+ return VM_FAULT_RETRY;
5097
+ }
5098
+#endif
5099
+
5100
+ /*
5101
+ * Do a speculative lookup of the PTE entry.
5102
+ */
5103
+ local_irq_disable();
5104
+ pgd = pgd_offset(mm, address);
5105
+ pgdval = READ_ONCE(*pgd);
5106
+ if (pgd_none(pgdval) || unlikely(pgd_bad(pgdval)))
5107
+ goto out_walk;
5108
+
5109
+ p4d = p4d_offset(pgd, address);
5110
+ if (pgd_val(READ_ONCE(*pgd)) != pgd_val(pgdval))
5111
+ goto out_walk;
5112
+ p4dval = READ_ONCE(*p4d);
5113
+ if (p4d_none(p4dval) || unlikely(p4d_bad(p4dval)))
5114
+ goto out_walk;
5115
+
5116
+ vmf.pud = pud_offset(p4d, address);
5117
+ if (p4d_val(READ_ONCE(*p4d)) != p4d_val(p4dval))
5118
+ goto out_walk;
5119
+ pudval = READ_ONCE(*vmf.pud);
5120
+ if (pud_none(pudval) || unlikely(pud_bad(pudval)))
5121
+ goto out_walk;
5122
+
5123
+ /* Huge pages at PUD level are not supported. */
5124
+ if (unlikely(pud_trans_huge(pudval)))
5125
+ goto out_walk;
5126
+
5127
+ vmf.pmd = pmd_offset(vmf.pud, address);
5128
+ if (pud_val(READ_ONCE(*vmf.pud)) != pud_val(pudval))
5129
+ goto out_walk;
5130
+ vmf.orig_pmd = READ_ONCE(*vmf.pmd);
5131
+ /*
5132
+ * pmd_none could mean that a hugepage collapse is in progress
5133
+ * in our back as collapse_huge_page() mark it before
5134
+ * invalidating the pte (which is done once the IPI is catched
5135
+ * by all CPU and we have interrupt disabled).
5136
+ * For this reason we cannot handle THP in a speculative way since we
5137
+ * can't safely indentify an in progress collapse operation done in our
5138
+ * back on that PMD.
5139
+ * Regarding the order of the following checks, see comment in
5140
+ * pmd_devmap_trans_unstable()
5141
+ */
5142
+ if (unlikely(pmd_devmap(vmf.orig_pmd) ||
5143
+ pmd_none(vmf.orig_pmd) || pmd_trans_huge(vmf.orig_pmd) ||
5144
+ is_swap_pmd(vmf.orig_pmd)))
5145
+ goto out_walk;
5146
+
5147
+ /*
5148
+ * The above does not allocate/instantiate page-tables because doing so
5149
+ * would lead to the possibility of instantiating page-tables after
5150
+ * free_pgtables() -- and consequently leaking them.
5151
+ *
5152
+ * The result is that we take at least one !speculative fault per PMD
5153
+ * in order to instantiate it.
5154
+ */
5155
+
5156
+ vmf.pte = pte_offset_map(vmf.pmd, address);
5157
+ if (pmd_val(READ_ONCE(*vmf.pmd)) != pmd_val(vmf.orig_pmd)) {
5158
+ pte_unmap(vmf.pte);
5159
+ vmf.pte = NULL;
5160
+ goto out_walk;
5161
+ }
5162
+ vmf.orig_pte = READ_ONCE(*vmf.pte);
5163
+ barrier(); /* See comment in handle_pte_fault() */
5164
+ if (pte_none(vmf.orig_pte)) {
5165
+ pte_unmap(vmf.pte);
5166
+ vmf.pte = NULL;
5167
+ }
5168
+
5169
+ vmf.sequence = seq;
5170
+ vmf.flags = flags;
5171
+
5172
+ local_irq_enable();
5173
+
5174
+ /*
5175
+ * We need to re-validate the VMA after checking the bounds, otherwise
5176
+ * we might have a false positive on the bounds.
5177
+ */
5178
+ if (read_seqcount_retry(&vmf.vma->vm_sequence, seq)) {
5179
+ trace_spf_vma_changed(_RET_IP_, vmf.vma, address);
5180
+ return VM_FAULT_RETRY;
5181
+ }
5182
+
5183
+ mem_cgroup_enter_user_fault();
5184
+ ret = handle_pte_fault(&vmf);
5185
+ mem_cgroup_exit_user_fault();
5186
+
5187
+ if (ret != VM_FAULT_RETRY) {
5188
+ if (vma_is_anonymous(vmf.vma))
5189
+ count_vm_event(SPECULATIVE_PGFAULT_ANON);
5190
+ else
5191
+ count_vm_event(SPECULATIVE_PGFAULT_FILE);
5192
+ }
5193
+
5194
+ /*
5195
+ * The task may have entered a memcg OOM situation but
5196
+ * if the allocation error was handled gracefully (no
5197
+ * VM_FAULT_OOM), there is no need to kill anything.
5198
+ * Just clean up the OOM state peacefully.
5199
+ */
5200
+ if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
5201
+ mem_cgroup_oom_synchronize(false);
5202
+ return ret;
5203
+
5204
+out_walk:
5205
+ trace_spf_vma_notsup(_RET_IP_, vmf.vma, address);
5206
+ local_irq_enable();
5207
+ return VM_FAULT_RETRY;
5208
+
5209
+out_segv:
5210
+ trace_spf_vma_access(_RET_IP_, vmf.vma, address);
5211
+ return VM_FAULT_SIGSEGV;
5212
+}
5213
+
5214
+vm_fault_t __handle_speculative_fault(struct mm_struct *mm,
5215
+ unsigned long address, unsigned int flags,
5216
+ struct vm_area_struct **vma,
5217
+ struct pt_regs *regs)
5218
+{
5219
+ vm_fault_t ret;
5220
+
5221
+ check_sync_rss_stat(current);
5222
+
5223
+ *vma = get_vma(mm, address);
5224
+ if (!*vma)
5225
+ return VM_FAULT_RETRY;
5226
+
5227
+ ret = ___handle_speculative_fault(mm, address, flags, *vma);
5228
+
5229
+ /*
5230
+ * If there is no need to retry, don't return the vma to the caller.
5231
+ */
5232
+ if (ret != VM_FAULT_RETRY) {
5233
+ put_vma(*vma);
5234
+ *vma = NULL;
5235
+ mm_account_fault(regs, address, flags, ret);
5236
+ }
5237
+
5238
+ return ret;
5239
+}
5240
+
5241
+/*
5242
+ * This is used to know if the vma fetch in the speculative page fault handler
5243
+ * is still valid when trying the regular fault path while holding the
5244
+ * mmap_sem.
5245
+ * The call to put_vma(vma) must be made after checking the vma's fields, as
5246
+ * the vma may be freed by put_vma(). In such a case it is expected that false
5247
+ * is returned.
5248
+ */
5249
+bool can_reuse_spf_vma(struct vm_area_struct *vma, unsigned long address)
5250
+{
5251
+ bool ret;
5252
+
5253
+ ret = !RB_EMPTY_NODE(&vma->vm_rb) &&
5254
+ vma->vm_start <= address && address < vma->vm_end;
5255
+ put_vma(vma);
5256
+ return ret;
5257
+}
5258
+#endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
5259
+
43355260 /*
43365261 * By the time we get here, we already hold the mm semaphore
43375262 *
4338
- * The mmap_sem may have been released depending on flags and our
5263
+ * The mmap_lock may have been released depending on flags and our
43395264 * return value. See filemap_fault() and __lock_page_or_retry().
43405265 */
43415266 vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
4342
- unsigned int flags)
5267
+ unsigned int flags, struct pt_regs *regs)
43435268 {
43445269 vm_fault_t ret;
43455270
....@@ -4379,6 +5304,8 @@
43795304 if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
43805305 mem_cgroup_oom_synchronize(false);
43815306 }
5307
+
5308
+ mm_account_fault(regs, address, flags, ret);
43825309
43835310 return ret;
43845311 }
....@@ -4421,19 +5348,11 @@
44215348 smp_wmb(); /* See comment in __pte_alloc */
44225349
44235350 spin_lock(&mm->page_table_lock);
4424
-#ifndef __ARCH_HAS_5LEVEL_HACK
44255351 if (!p4d_present(*p4d)) {
44265352 mm_inc_nr_puds(mm);
44275353 p4d_populate(mm, p4d, new);
44285354 } else /* Another has populated it */
44295355 pud_free(mm, new);
4430
-#else
4431
- if (!pgd_present(*p4d)) {
4432
- mm_inc_nr_puds(mm);
4433
- pgd_populate(mm, p4d, new);
4434
- } else /* Another has populated it */
4435
- pud_free(mm, new);
4436
-#endif /* __ARCH_HAS_5LEVEL_HACK */
44375356 spin_unlock(&mm->page_table_lock);
44385357 return 0;
44395358 }
....@@ -4454,27 +5373,19 @@
44545373 smp_wmb(); /* See comment in __pte_alloc */
44555374
44565375 ptl = pud_lock(mm, pud);
4457
-#ifndef __ARCH_HAS_4LEVEL_HACK
44585376 if (!pud_present(*pud)) {
44595377 mm_inc_nr_pmds(mm);
44605378 pud_populate(mm, pud, new);
44615379 } else /* Another has populated it */
44625380 pmd_free(mm, new);
4463
-#else
4464
- if (!pgd_present(*pud)) {
4465
- mm_inc_nr_pmds(mm);
4466
- pgd_populate(mm, pud, new);
4467
- } else /* Another has populated it */
4468
- pmd_free(mm, new);
4469
-#endif /* __ARCH_HAS_4LEVEL_HACK */
44705381 spin_unlock(ptl);
44715382 return 0;
44725383 }
44735384 #endif /* __PAGETABLE_PMD_FOLDED */
44745385
4475
-static int __follow_pte_pmd(struct mm_struct *mm, unsigned long address,
4476
- unsigned long *start, unsigned long *end,
4477
- pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp)
5386
+int follow_invalidate_pte(struct mm_struct *mm, unsigned long address,
5387
+ struct mmu_notifier_range *range, pte_t **ptepp,
5388
+ pmd_t **pmdpp, spinlock_t **ptlp)
44785389 {
44795390 pgd_t *pgd;
44805391 p4d_t *p4d;
....@@ -4501,10 +5412,11 @@
45015412 if (!pmdpp)
45025413 goto out;
45035414
4504
- if (start && end) {
4505
- *start = address & PMD_MASK;
4506
- *end = *start + PMD_SIZE;
4507
- mmu_notifier_invalidate_range_start(mm, *start, *end);
5415
+ if (range) {
5416
+ mmu_notifier_range_init(range, MMU_NOTIFY_CLEAR, 0,
5417
+ NULL, mm, address & PMD_MASK,
5418
+ (address & PMD_MASK) + PMD_SIZE);
5419
+ mmu_notifier_invalidate_range_start(range);
45085420 }
45095421 *ptlp = pmd_lock(mm, pmd);
45105422 if (pmd_huge(*pmd)) {
....@@ -4512,17 +5424,18 @@
45125424 return 0;
45135425 }
45145426 spin_unlock(*ptlp);
4515
- if (start && end)
4516
- mmu_notifier_invalidate_range_end(mm, *start, *end);
5427
+ if (range)
5428
+ mmu_notifier_invalidate_range_end(range);
45175429 }
45185430
45195431 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
45205432 goto out;
45215433
4522
- if (start && end) {
4523
- *start = address & PAGE_MASK;
4524
- *end = *start + PAGE_SIZE;
4525
- mmu_notifier_invalidate_range_start(mm, *start, *end);
5434
+ if (range) {
5435
+ mmu_notifier_range_init(range, MMU_NOTIFY_CLEAR, 0, NULL, mm,
5436
+ address & PAGE_MASK,
5437
+ (address & PAGE_MASK) + PAGE_SIZE);
5438
+ mmu_notifier_invalidate_range_start(range);
45265439 }
45275440 ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
45285441 if (!pte_present(*ptep))
....@@ -4531,37 +5444,39 @@
45315444 return 0;
45325445 unlock:
45335446 pte_unmap_unlock(ptep, *ptlp);
4534
- if (start && end)
4535
- mmu_notifier_invalidate_range_end(mm, *start, *end);
5447
+ if (range)
5448
+ mmu_notifier_invalidate_range_end(range);
45365449 out:
45375450 return -EINVAL;
45385451 }
45395452
4540
-static inline int follow_pte(struct mm_struct *mm, unsigned long address,
4541
- pte_t **ptepp, spinlock_t **ptlp)
5453
+/**
5454
+ * follow_pte - look up PTE at a user virtual address
5455
+ * @mm: the mm_struct of the target address space
5456
+ * @address: user virtual address
5457
+ * @ptepp: location to store found PTE
5458
+ * @ptlp: location to store the lock for the PTE
5459
+ *
5460
+ * On a successful return, the pointer to the PTE is stored in @ptepp;
5461
+ * the corresponding lock is taken and its location is stored in @ptlp.
5462
+ * The contents of the PTE are only stable until @ptlp is released;
5463
+ * any further use, if any, must be protected against invalidation
5464
+ * with MMU notifiers.
5465
+ *
5466
+ * Only IO mappings and raw PFN mappings are allowed. The mmap semaphore
5467
+ * should be taken for read.
5468
+ *
5469
+ * KVM uses this function. While it is arguably less bad than ``follow_pfn``,
5470
+ * it is not a good general-purpose API.
5471
+ *
5472
+ * Return: zero on success, -ve otherwise.
5473
+ */
5474
+int follow_pte(struct mm_struct *mm, unsigned long address,
5475
+ pte_t **ptepp, spinlock_t **ptlp)
45425476 {
4543
- int res;
4544
-
4545
- /* (void) is needed to make gcc happy */
4546
- (void) __cond_lock(*ptlp,
4547
- !(res = __follow_pte_pmd(mm, address, NULL, NULL,
4548
- ptepp, NULL, ptlp)));
4549
- return res;
5477
+ return follow_invalidate_pte(mm, address, NULL, ptepp, NULL, ptlp);
45505478 }
4551
-
4552
-int follow_pte_pmd(struct mm_struct *mm, unsigned long address,
4553
- unsigned long *start, unsigned long *end,
4554
- pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp)
4555
-{
4556
- int res;
4557
-
4558
- /* (void) is needed to make gcc happy */
4559
- (void) __cond_lock(*ptlp,
4560
- !(res = __follow_pte_pmd(mm, address, start, end,
4561
- ptepp, pmdpp, ptlp)));
4562
- return res;
4563
-}
4564
-EXPORT_SYMBOL(follow_pte_pmd);
5479
+EXPORT_SYMBOL_GPL(follow_pte);
45655480
45665481 /**
45675482 * follow_pfn - look up PFN at a user virtual address
....@@ -4571,7 +5486,10 @@
45715486 *
45725487 * Only IO mappings and raw PFN mappings are allowed.
45735488 *
4574
- * Returns zero and the pfn at @pfn on success, -ve otherwise.
5489
+ * This function does not allow the caller to read the permissions
5490
+ * of the PTE. Do not use it.
5491
+ *
5492
+ * Return: zero and the pfn at @pfn on success, -ve otherwise.
45755493 */
45765494 int follow_pfn(struct vm_area_struct *vma, unsigned long address,
45775495 unsigned long *pfn)
....@@ -4658,7 +5576,7 @@
46585576 void *old_buf = buf;
46595577 int write = gup_flags & FOLL_WRITE;
46605578
4661
- if (down_read_killable(&mm->mmap_sem))
5579
+ if (mmap_read_lock_killable(mm))
46625580 return 0;
46635581
46645582 /* ignore errors, just check how much was successfully transferred */
....@@ -4667,7 +5585,7 @@
46675585 void *maddr;
46685586 struct page *page = NULL;
46695587
4670
- ret = get_user_pages_remote(tsk, mm, addr, 1,
5588
+ ret = get_user_pages_remote(mm, addr, 1,
46715589 gup_flags, &page, &vma, NULL);
46725590 if (ret <= 0) {
46735591 #ifndef CONFIG_HAVE_IOREMAP_PROT
....@@ -4703,13 +5621,13 @@
47035621 buf, maddr + offset, bytes);
47045622 }
47055623 kunmap(page);
4706
- put_page(page);
5624
+ put_user_page(page);
47075625 }
47085626 len -= bytes;
47095627 buf += bytes;
47105628 addr += bytes;
47115629 }
4712
- up_read(&mm->mmap_sem);
5630
+ mmap_read_unlock(mm);
47135631
47145632 return buf - old_buf;
47155633 }
....@@ -4723,6 +5641,8 @@
47235641 * @gup_flags: flags modifying lookup behaviour
47245642 *
47255643 * The caller must hold a reference on @mm.
5644
+ *
5645
+ * Return: number of bytes copied from source to destination.
47265646 */
47275647 int access_remote_vm(struct mm_struct *mm, unsigned long addr,
47285648 void *buf, int len, unsigned int gup_flags)
....@@ -4764,7 +5684,7 @@
47645684 /*
47655685 * we might be running from an atomic context so we cannot sleep
47665686 */
4767
- if (!down_read_trylock(&mm->mmap_sem))
5687
+ if (!mmap_read_trylock(mm))
47685688 return;
47695689
47705690 vma = find_vma(mm, ip);
....@@ -4783,7 +5703,7 @@
47835703 free_page((unsigned long)buf);
47845704 }
47855705 }
4786
- up_read(&mm->mmap_sem);
5706
+ mmap_read_unlock(mm);
47875707 }
47885708
47895709 #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
....@@ -4791,7 +5711,7 @@
47915711 {
47925712 /*
47935713 * Some code (nfs/sunrpc) uses socket ops on kernel memory while
4794
- * holding the mmap_sem, this is safe because kernel memory doesn't
5714
+ * holding the mmap_lock, this is safe because kernel memory doesn't
47955715 * get paged out, therefore we'll never actually fault, and the
47965716 * below annotations will generate false positives.
47975717 */
....@@ -4802,7 +5722,7 @@
48025722 __might_sleep(file, line, 0);
48035723 #if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
48045724 if (current->mm)
4805
- might_lock_read(&current->mm->mmap_sem);
5725
+ might_lock_read(&current->mm->mmap_lock);
48065726 #endif
48075727 }
48085728 EXPORT_SYMBOL(__might_fault);
....@@ -4979,6 +5899,8 @@
49795899 if (rc)
49805900 break;
49815901
5902
+ flush_dcache_page(subpage);
5903
+
49825904 cond_resched();
49835905 }
49845906 return ret_val;