diff options
author | Jérôme Glisse <jglisse@redhat.com> | 2013-08-02 15:17:24 -0400 |
---|---|---|
committer | Jérôme Glisse <jglisse@redhat.com> | 2016-04-07 13:23:23 -0400 |
commit | 6abff77f76d50c374057228baa0e01354d9bf1a1 (patch) | |
tree | d776cb91d9f79758431858ced7020301a28b9d0e | |
parent | 5692a25511e0da564d0f5a554ac9f39197b2358f (diff) |
mmu_notifier: add event information to address invalidation v9
The event information will be useful for new user of mmu_notifier API.
The event argument differentiate between a vma disappearing, a page
being write protected or simply a page being unmaped. This allow new
user to take different path for different event for instance on unmap
the resource used to track a vma are still valid and should stay around.
While if the event is saying that a vma is being destroy it means that any
resources used to track this vma can be free.
Changed since v1:
- renamed action into event (updated commit message too).
- simplified the event names and clarified their usage
also documenting what exceptation the listener can have in
respect to each event.
Changed since v2:
- Avoid crazy name.
- Do not move code that do not need to move.
Changed since v3:
- Separate huge page split from mlock/munlock and softdirty.
Changed since v4:
- Rebase (no other changes).
Changed since v5:
- Typo fix.
- Changed zap_page_range from MMU_MUNMAP to MMU_MIGRATE to reflect the
fact that the address range is still valid just the page backing it
are no longer.
Changed since v6:
- try_to_unmap_one() only invalidate when doing migration.
- Differentiate fork from other case.
Changed since v7:
- Renamed MMU_HUGE_PAGE_SPLIT to MMU_HUGE_PAGE_SPLIT.
- Renamed MMU_ISDIRTY to MMU_CLEAR_SOFT_DIRTY.
- Renamed MMU_WRITE_PROTECT to MMU_KSM_WRITE_PROTECT.
- English syntax fixes.
Changed since v8:
- Added freeze/unfreeze for new huge page splitting.
Signed-off-by: Jérôme Glisse <jglisse@redhat.com>
Reviewed-by: Rik van Riel <riel@redhat.com>
-rw-r--r-- | drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 6 | ||||
-rw-r--r-- | drivers/gpu/drm/i915/i915_gem_userptr.c | 3 | ||||
-rw-r--r-- | drivers/gpu/drm/radeon/radeon_mn.c | 3 | ||||
-rw-r--r-- | drivers/infiniband/core/umem_odp.c | 9 | ||||
-rw-r--r-- | drivers/iommu/amd_iommu_v2.c | 3 | ||||
-rw-r--r-- | drivers/iommu/intel-svm.c | 6 | ||||
-rw-r--r-- | drivers/misc/sgi-gru/grutlbpurge.c | 9 | ||||
-rw-r--r-- | drivers/xen/gntdev.c | 9 | ||||
-rw-r--r-- | fs/proc/task_mmu.c | 6 | ||||
-rw-r--r-- | include/linux/mmu_notifier.h | 137 | ||||
-rw-r--r-- | kernel/events/uprobes.c | 10 | ||||
-rw-r--r-- | mm/huge_memory.c | 27 | ||||
-rw-r--r-- | mm/hugetlb.c | 23 | ||||
-rw-r--r-- | mm/ksm.c | 18 | ||||
-rw-r--r-- | mm/madvise.c | 4 | ||||
-rw-r--r-- | mm/memory.c | 27 | ||||
-rw-r--r-- | mm/migrate.c | 9 | ||||
-rw-r--r-- | mm/mmu_notifier.c | 28 | ||||
-rw-r--r-- | mm/mprotect.c | 6 | ||||
-rw-r--r-- | mm/mremap.c | 6 | ||||
-rw-r--r-- | mm/rmap.c | 4 | ||||
-rw-r--r-- | virt/kvm/kvm_main.c | 12 |
22 files changed, 263 insertions, 102 deletions
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c index 9f4a45cd2aab..147015cc034a 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c @@ -157,7 +157,8 @@ static void amdgpu_mn_invalidate_node(struct amdgpu_mn_node *node, */ static void amdgpu_mn_invalidate_page(struct mmu_notifier *mn, struct mm_struct *mm, - unsigned long address) + unsigned long address, + enum mmu_event event) { struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn); struct interval_tree_node *it; @@ -189,7 +190,8 @@ static void amdgpu_mn_invalidate_page(struct mmu_notifier *mn, static void amdgpu_mn_invalidate_range_start(struct mmu_notifier *mn, struct mm_struct *mm, unsigned long start, - unsigned long end) + unsigned long end, + enum mmu_event event) { struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn); struct interval_tree_node *it; diff --git a/drivers/gpu/drm/i915/i915_gem_userptr.c b/drivers/gpu/drm/i915/i915_gem_userptr.c index 18ba8139e922..3bfc683bb0e2 100644 --- a/drivers/gpu/drm/i915/i915_gem_userptr.c +++ b/drivers/gpu/drm/i915/i915_gem_userptr.c @@ -112,7 +112,8 @@ static void del_object(struct i915_mmu_object *mo) static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn, struct mm_struct *mm, unsigned long start, - unsigned long end) + unsigned long end, + enum mmu_event event) { struct i915_mmu_notifier *mn = container_of(_mn, struct i915_mmu_notifier, mn); diff --git a/drivers/gpu/drm/radeon/radeon_mn.c b/drivers/gpu/drm/radeon/radeon_mn.c index eef006c48584..3a9615b96a88 100644 --- a/drivers/gpu/drm/radeon/radeon_mn.c +++ b/drivers/gpu/drm/radeon/radeon_mn.c @@ -121,7 +121,8 @@ static void radeon_mn_release(struct mmu_notifier *mn, static void radeon_mn_invalidate_range_start(struct mmu_notifier *mn, struct mm_struct *mm, unsigned long start, - unsigned long end) + unsigned long end, + enum mmu_event event) { struct radeon_mn *rmn = container_of(mn, struct radeon_mn, mn); struct interval_tree_node *it; diff --git a/drivers/infiniband/core/umem_odp.c b/drivers/infiniband/core/umem_odp.c index 75077a018675..aad511234d4b 100644 --- a/drivers/infiniband/core/umem_odp.c +++ b/drivers/infiniband/core/umem_odp.c @@ -165,7 +165,8 @@ static int invalidate_page_trampoline(struct ib_umem *item, u64 start, static void ib_umem_notifier_invalidate_page(struct mmu_notifier *mn, struct mm_struct *mm, - unsigned long address) + unsigned long address, + enum mmu_event event) { struct ib_ucontext *context = container_of(mn, struct ib_ucontext, mn); @@ -192,7 +193,8 @@ static int invalidate_range_start_trampoline(struct ib_umem *item, u64 start, static void ib_umem_notifier_invalidate_range_start(struct mmu_notifier *mn, struct mm_struct *mm, unsigned long start, - unsigned long end) + unsigned long end, + enum mmu_event event) { struct ib_ucontext *context = container_of(mn, struct ib_ucontext, mn); @@ -217,7 +219,8 @@ static int invalidate_range_end_trampoline(struct ib_umem *item, u64 start, static void ib_umem_notifier_invalidate_range_end(struct mmu_notifier *mn, struct mm_struct *mm, unsigned long start, - unsigned long end) + unsigned long end, + enum mmu_event event) { struct ib_ucontext *context = container_of(mn, struct ib_ucontext, mn); diff --git a/drivers/iommu/amd_iommu_v2.c b/drivers/iommu/amd_iommu_v2.c index 56999d2fac07..cba209e4d4e6 100644 --- a/drivers/iommu/amd_iommu_v2.c +++ b/drivers/iommu/amd_iommu_v2.c @@ -392,7 +392,8 @@ static int mn_clear_flush_young(struct mmu_notifier *mn, static void mn_invalidate_page(struct mmu_notifier *mn, struct mm_struct *mm, - unsigned long address) + unsigned long address, + enum mmu_event event) { __mn_flush_page(mn, address); } diff --git a/drivers/iommu/intel-svm.c b/drivers/iommu/intel-svm.c index d9939fa9b588..60decf3d14e3 100644 --- a/drivers/iommu/intel-svm.c +++ b/drivers/iommu/intel-svm.c @@ -209,7 +209,8 @@ static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address, } static void intel_change_pte(struct mmu_notifier *mn, struct mm_struct *mm, - unsigned long address, pte_t pte) + unsigned long address, pte_t pte, + enum mmu_event event) { struct intel_svm *svm = container_of(mn, struct intel_svm, notifier); @@ -217,7 +218,8 @@ static void intel_change_pte(struct mmu_notifier *mn, struct mm_struct *mm, } static void intel_invalidate_page(struct mmu_notifier *mn, struct mm_struct *mm, - unsigned long address) + unsigned long address, + enum mmu_event event) { struct intel_svm *svm = container_of(mn, struct intel_svm, notifier); diff --git a/drivers/misc/sgi-gru/grutlbpurge.c b/drivers/misc/sgi-gru/grutlbpurge.c index e936d43895d2..1c220ae60232 100644 --- a/drivers/misc/sgi-gru/grutlbpurge.c +++ b/drivers/misc/sgi-gru/grutlbpurge.c @@ -221,7 +221,8 @@ void gru_flush_all_tlb(struct gru_state *gru) */ static void gru_invalidate_range_start(struct mmu_notifier *mn, struct mm_struct *mm, - unsigned long start, unsigned long end) + unsigned long start, unsigned long end, + enum mmu_event event) { struct gru_mm_struct *gms = container_of(mn, struct gru_mm_struct, ms_notifier); @@ -235,7 +236,8 @@ static void gru_invalidate_range_start(struct mmu_notifier *mn, static void gru_invalidate_range_end(struct mmu_notifier *mn, struct mm_struct *mm, unsigned long start, - unsigned long end) + unsigned long end, + enum mmu_event event) { struct gru_mm_struct *gms = container_of(mn, struct gru_mm_struct, ms_notifier); @@ -248,7 +250,8 @@ static void gru_invalidate_range_end(struct mmu_notifier *mn, } static void gru_invalidate_page(struct mmu_notifier *mn, struct mm_struct *mm, - unsigned long address) + unsigned long address, + enum mmu_event event) { struct gru_mm_struct *gms = container_of(mn, struct gru_mm_struct, ms_notifier); diff --git a/drivers/xen/gntdev.c b/drivers/xen/gntdev.c index dc495383ad73..df7640c2f4af 100644 --- a/drivers/xen/gntdev.c +++ b/drivers/xen/gntdev.c @@ -467,7 +467,9 @@ static void unmap_if_in_range(struct grant_map *map, static void mn_invl_range_start(struct mmu_notifier *mn, struct mm_struct *mm, - unsigned long start, unsigned long end) + unsigned long start, + unsigned long end, + enum mmu_event event) { struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn); struct grant_map *map; @@ -484,9 +486,10 @@ static void mn_invl_range_start(struct mmu_notifier *mn, static void mn_invl_page(struct mmu_notifier *mn, struct mm_struct *mm, - unsigned long address) + unsigned long address, + enum mmu_event event) { - mn_invl_range_start(mn, mm, address, address + PAGE_SIZE); + mn_invl_range_start(mn, mm, address, address + PAGE_SIZE, event); } static void mn_release(struct mmu_notifier *mn, diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 229cb546bee0..3101b70dd273 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -1051,11 +1051,13 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf, downgrade_write(&mm->mmap_sem); break; } - mmu_notifier_invalidate_range_start(mm, 0, -1); + mmu_notifier_invalidate_range_start(mm, 0, -1, + MMU_CLEAR_SOFT_DIRTY); } walk_page_range(0, ~0UL, &clear_refs_walk); if (type == CLEAR_REFS_SOFT_DIRTY) - mmu_notifier_invalidate_range_end(mm, 0, -1); + mmu_notifier_invalidate_range_end(mm, 0, -1, + MMU_CLEAR_SOFT_DIRTY); flush_tlb_mm(mm); up_read(&mm->mmap_sem); out_mm: diff --git a/include/linux/mmu_notifier.h b/include/linux/mmu_notifier.h index a1a210d59961..906aad09022c 100644 --- a/include/linux/mmu_notifier.h +++ b/include/linux/mmu_notifier.h @@ -9,6 +9,72 @@ struct mmu_notifier; struct mmu_notifier_ops; +/* MMU Events report fine-grained information to the callback routine, allowing + * the event listener to make a more informed decision as to what action to + * take. The event types are: + * + * - MMU_FORK a process is forking. This will lead to vmas getting + * write-protected, in order to set up COW + * + * - MMU_HUGE_PAGE_SPLIT the pages don't move, nor does their content change, + * but the page table structure is updated (levels added or removed). + * + * - MMU_HUGE_FREEZE/MMU_HUGE_UNFREEZE huge page splitting is a multi-step, + * first pmd is freeze and then split before being unfreeze. + * + * - MMU_CLEAR_SOFT_DIRTY need to write protect so write properly update the + * soft dirty bit of page table entry. + * + * - MMU_MIGRATE: memory is migrating from one page to another, thus all write + * access must stop after invalidate_range_start callback returns. + * Furthermore, no read access should be allowed either, as a new page can + * be remapped with write access before the invalidate_range_end callback + * happens and thus any read access to old page might read stale data. There + * are several sources for this event, including: + * + * - A page moving to swap (various reasons, including page reclaim), + * - An mremap syscall, + * - migration for NUMA reasons, + * - balancing the memory pool, + * - write fault on COW page, + * - and more that are not listed here. + * + * - MMU_MPROT: memory access protection is changing. Refer to the vma to get + * the new access protection. All memory access are still valid until the + * invalidate_range_end callback. + * + * - MMU_MUNLOCK: unlock memory. Content of page table stays the same but + * page are unlocked. + * + * - MMU_MUNMAP: the range is being unmapped (outcome of a munmap syscall or + * process destruction). However, access is still allowed, up until the + * invalidate_range_free_pages callback. This also implies that secondary + * page table can be trimmed, because the address range is no longer valid. + * + * - MMU_WRITE_BACK: memory is being written back to disk, all write accesses + * must stop after invalidate_range_start callback returns. Read access are + * still allowed. + * + * - MMU_KSM_WRITE_PROTECT: memory is being write protected for KSM. + * + * If in doubt when adding a new notifier caller, please use MMU_MIGRATE, + * because it will always lead to reasonable behavior, but will not allow the + * listener a chance to optimize its events. + */ +enum mmu_event { + MMU_FORK = 0, + MMU_HUGE_PAGE_SPLIT, + MMU_HUGE_FREEZE, + MMU_HUGE_UNFREEZE, + MMU_CLEAR_SOFT_DIRTY, + MMU_MIGRATE, + MMU_MPROT, + MMU_MUNLOCK, + MMU_MUNMAP, + MMU_WRITE_BACK, + MMU_KSM_WRITE_PROTECT, +}; + #ifdef CONFIG_MMU_NOTIFIER /* @@ -92,7 +158,8 @@ struct mmu_notifier_ops { void (*change_pte)(struct mmu_notifier *mn, struct mm_struct *mm, unsigned long address, - pte_t pte); + pte_t pte, + enum mmu_event event); /* * Before this is invoked any secondary MMU is still ok to @@ -103,7 +170,8 @@ struct mmu_notifier_ops { */ void (*invalidate_page)(struct mmu_notifier *mn, struct mm_struct *mm, - unsigned long address); + unsigned long address, + enum mmu_event event); /* * invalidate_range_start() and invalidate_range_end() must be @@ -150,10 +218,14 @@ struct mmu_notifier_ops { */ void (*invalidate_range_start)(struct mmu_notifier *mn, struct mm_struct *mm, - unsigned long start, unsigned long end); + unsigned long start, + unsigned long end, + enum mmu_event event); void (*invalidate_range_end)(struct mmu_notifier *mn, struct mm_struct *mm, - unsigned long start, unsigned long end); + unsigned long start, + unsigned long end, + enum mmu_event event); /* * invalidate_range() is either called between @@ -219,13 +291,20 @@ extern int __mmu_notifier_clear_young(struct mm_struct *mm, extern int __mmu_notifier_test_young(struct mm_struct *mm, unsigned long address); extern void __mmu_notifier_change_pte(struct mm_struct *mm, - unsigned long address, pte_t pte); + unsigned long address, + pte_t pte, + enum mmu_event event); extern void __mmu_notifier_invalidate_page(struct mm_struct *mm, - unsigned long address); + unsigned long address, + enum mmu_event event); extern void __mmu_notifier_invalidate_range_start(struct mm_struct *mm, - unsigned long start, unsigned long end); + unsigned long start, + unsigned long end, + enum mmu_event event); extern void __mmu_notifier_invalidate_range_end(struct mm_struct *mm, - unsigned long start, unsigned long end); + unsigned long start, + unsigned long end, + enum mmu_event event); extern void __mmu_notifier_invalidate_range(struct mm_struct *mm, unsigned long start, unsigned long end); @@ -262,31 +341,38 @@ static inline int mmu_notifier_test_young(struct mm_struct *mm, } static inline void mmu_notifier_change_pte(struct mm_struct *mm, - unsigned long address, pte_t pte) + unsigned long address, + pte_t pte, + enum mmu_event event) { if (mm_has_notifiers(mm)) - __mmu_notifier_change_pte(mm, address, pte); + __mmu_notifier_change_pte(mm, address, pte, event); } static inline void mmu_notifier_invalidate_page(struct mm_struct *mm, - unsigned long address) + unsigned long address, + enum mmu_event event) { if (mm_has_notifiers(mm)) - __mmu_notifier_invalidate_page(mm, address); + __mmu_notifier_invalidate_page(mm, address, event); } static inline void mmu_notifier_invalidate_range_start(struct mm_struct *mm, - unsigned long start, unsigned long end) + unsigned long start, + unsigned long end, + enum mmu_event event) { if (mm_has_notifiers(mm)) - __mmu_notifier_invalidate_range_start(mm, start, end); + __mmu_notifier_invalidate_range_start(mm, start, end, event); } static inline void mmu_notifier_invalidate_range_end(struct mm_struct *mm, - unsigned long start, unsigned long end) + unsigned long start, + unsigned long end, + enum mmu_event event) { if (mm_has_notifiers(mm)) - __mmu_notifier_invalidate_range_end(mm, start, end); + __mmu_notifier_invalidate_range_end(mm, start, end, event); } static inline void mmu_notifier_invalidate_range(struct mm_struct *mm, @@ -403,13 +489,13 @@ static inline void mmu_notifier_mm_destroy(struct mm_struct *mm) * old page would remain mapped readonly in the secondary MMUs after the new * page is already writable by some CPU through the primary MMU. */ -#define set_pte_at_notify(__mm, __address, __ptep, __pte) \ +#define set_pte_at_notify(__mm, __address, __ptep, __pte, __event) \ ({ \ struct mm_struct *___mm = __mm; \ unsigned long ___address = __address; \ pte_t ___pte = __pte; \ \ - mmu_notifier_change_pte(___mm, ___address, ___pte); \ + mmu_notifier_change_pte(___mm, ___address, ___pte, __event); \ set_pte_at(___mm, ___address, __ptep, ___pte); \ }) @@ -437,22 +523,29 @@ static inline int mmu_notifier_test_young(struct mm_struct *mm, } static inline void mmu_notifier_change_pte(struct mm_struct *mm, - unsigned long address, pte_t pte) + unsigned long address, + pte_t pte, + enum mmu_event event) { } static inline void mmu_notifier_invalidate_page(struct mm_struct *mm, - unsigned long address) + unsigned long address, + enum mmu_event event) { } static inline void mmu_notifier_invalidate_range_start(struct mm_struct *mm, - unsigned long start, unsigned long end) + unsigned long start, + unsigned long end, + enum mmu_event event) { } static inline void mmu_notifier_invalidate_range_end(struct mm_struct *mm, - unsigned long start, unsigned long end) + unsigned long start, + unsigned long end, + enum mmu_event event) { } diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index 7edc95edfaee..fbe307205bcc 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -169,7 +169,8 @@ static int __replace_page(struct vm_area_struct *vma, unsigned long addr, /* For try_to_free_swap() and munlock_vma_page() below */ lock_page(page); - mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_start(mm, mmun_start, + mmun_end, MMU_MIGRATE); err = -EAGAIN; ptep = page_check_address(page, mm, addr, &ptl, 0); if (!ptep) @@ -187,7 +188,9 @@ static int __replace_page(struct vm_area_struct *vma, unsigned long addr, flush_cache_page(vma, addr, pte_pfn(*ptep)); ptep_clear_flush_notify(vma, addr, ptep); - set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot)); + set_pte_at_notify(mm, addr, ptep, + mk_pte(kpage, vma->vm_page_prot), + MMU_MIGRATE); page_remove_rmap(page, false); if (!page_mapped(page)) @@ -201,7 +204,8 @@ static int __replace_page(struct vm_area_struct *vma, unsigned long addr, err = 0; unlock: mem_cgroup_cancel_charge(kpage, memcg, false); - mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end, + MMU_MIGRATE); unlock_page(page); return err; } diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 86f9f8b82f8e..1eecc2dce473 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1218,7 +1218,8 @@ static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm, mmun_start = haddr; mmun_end = haddr + HPAGE_PMD_SIZE; - mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end, + MMU_MIGRATE); ptl = pmd_lock(mm, pmd); if (unlikely(!pmd_same(*pmd, orig_pmd))) @@ -1252,7 +1253,8 @@ static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm, page_remove_rmap(page, true); spin_unlock(ptl); - mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_end(mm, mmun_start, + mmun_end, MMU_MIGRATE); ret |= VM_FAULT_WRITE; put_page(page); @@ -1262,7 +1264,8 @@ out: out_free_pages: spin_unlock(ptl); - mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_end(mm, mmun_start, + mmun_end, MMU_MIGRATE); for (i = 0; i < HPAGE_PMD_NR; i++) { memcg = (void *)page_private(pages[i]); set_page_private(pages[i], 0); @@ -1367,7 +1370,8 @@ alloc: mmun_start = haddr; mmun_end = haddr + HPAGE_PMD_SIZE; - mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end, + MMU_MIGRATE); spin_lock(ptl); if (page) @@ -1399,7 +1403,8 @@ alloc: } spin_unlock(ptl); out_mn: - mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_end(mm, mmun_start, + mmun_end, MMU_MIGRATE); out: return ret; out_unlock: @@ -2430,7 +2435,8 @@ static void collapse_huge_page(struct mm_struct *mm, mmun_start = address; mmun_end = address + HPAGE_PMD_SIZE; - mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_start(mm, mmun_start, + mmun_end, MMU_MIGRATE); pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */ /* * After this gup_fast can't run anymore. This also removes @@ -2440,7 +2446,8 @@ static void collapse_huge_page(struct mm_struct *mm, */ _pmd = pmdp_collapse_flush(vma, address, pmd); spin_unlock(pmd_ptl); - mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_end(mm, mmun_start, + mmun_end, MMU_MIGRATE); spin_lock(pte_ptl); isolated = __collapse_huge_page_isolate(vma, address, pte); @@ -2994,7 +3001,8 @@ void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, struct mm_struct *mm = vma->vm_mm; unsigned long haddr = address & HPAGE_PMD_MASK; - mmu_notifier_invalidate_range_start(mm, haddr, haddr + HPAGE_PMD_SIZE); + mmu_notifier_invalidate_range_start(mm, haddr, haddr + HPAGE_PMD_SIZE, + MMU_HUGE_PAGE_SPLIT); ptl = pmd_lock(mm, pmd); if (pmd_trans_huge(*pmd)) { struct page *page = pmd_page(*pmd); @@ -3005,7 +3013,8 @@ void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, __split_huge_pmd_locked(vma, pmd, haddr, freeze); out: spin_unlock(ptl); - mmu_notifier_invalidate_range_end(mm, haddr, haddr + HPAGE_PMD_SIZE); + mmu_notifier_invalidate_range_end(mm, haddr, haddr + HPAGE_PMD_SIZE, + MMU_HUGE_PAGE_SPLIT); } void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address, diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 19d0d08b396f..c22f2c1c7da0 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3058,7 +3058,8 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, mmun_start = vma->vm_start; mmun_end = vma->vm_end; if (cow) - mmu_notifier_invalidate_range_start(src, mmun_start, mmun_end); + mmu_notifier_invalidate_range_start(src, mmun_start, + mmun_end, MMU_MIGRATE); for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) { spinlock_t *src_ptl, *dst_ptl; @@ -3113,7 +3114,8 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, } if (cow) - mmu_notifier_invalidate_range_end(src, mmun_start, mmun_end); + mmu_notifier_invalidate_range_end(src, mmun_start, + mmun_end, MMU_MIGRATE); return ret; } @@ -3139,7 +3141,8 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, BUG_ON(end & ~huge_page_mask(h)); tlb_start_vma(tlb, vma); - mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_start(mm, mmun_start, + mmun_end, MMU_MIGRATE); address = start; again: for (; address < end; address += sz) { @@ -3214,7 +3217,8 @@ unlock: if (address < end && !ref_page) goto again; } - mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_end(mm, mmun_start, + mmun_end, MMU_MIGRATE); tlb_end_vma(tlb, vma); } @@ -3401,8 +3405,8 @@ retry_avoidcopy: mmun_start = address & huge_page_mask(h); mmun_end = mmun_start + huge_page_size(h); - mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); - + mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end, + MMU_MIGRATE); /* * Retake the page table lock to check for racing updates * before the page tables are altered @@ -3423,7 +3427,8 @@ retry_avoidcopy: new_page = old_page; } spin_unlock(ptl); - mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end, + MMU_MIGRATE); out_release_all: put_page(new_page); out_release_old: @@ -3906,7 +3911,7 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, BUG_ON(address >= end); flush_cache_range(vma, address, end); - mmu_notifier_invalidate_range_start(mm, start, end); + mmu_notifier_invalidate_range_start(mm, start, end, MMU_MPROT); i_mmap_lock_write(vma->vm_file->f_mapping); for (; address < end; address += huge_page_size(h)) { spinlock_t *ptl; @@ -3956,7 +3961,7 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, flush_tlb_range(vma, start, end); mmu_notifier_invalidate_range(mm, start, end); i_mmap_unlock_write(vma->vm_file->f_mapping); - mmu_notifier_invalidate_range_end(mm, start, end); + mmu_notifier_invalidate_range_end(mm, start, end, MMU_MPROT); return pages << h->order; } @@ -863,7 +863,8 @@ static int write_protect_page(struct vm_area_struct *vma, struct page *page, mmun_start = addr; mmun_end = addr + PAGE_SIZE; - mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end, + MMU_KSM_WRITE_PROTECT); ptep = page_check_address(page, mm, addr, &ptl, 0); if (!ptep) @@ -895,7 +896,7 @@ static int write_protect_page(struct vm_area_struct *vma, struct page *page, if (pte_dirty(entry)) set_page_dirty(page); entry = pte_mkclean(pte_wrprotect(entry)); - set_pte_at_notify(mm, addr, ptep, entry); + set_pte_at_notify(mm, addr, ptep, entry, MMU_KSM_WRITE_PROTECT); } *orig_pte = *ptep; err = 0; @@ -903,7 +904,8 @@ static int write_protect_page(struct vm_area_struct *vma, struct page *page, out_unlock: pte_unmap_unlock(ptep, ptl); out_mn: - mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end, + MMU_KSM_WRITE_PROTECT); out: return err; } @@ -939,7 +941,8 @@ static int replace_page(struct vm_area_struct *vma, struct page *page, mmun_start = addr; mmun_end = addr + PAGE_SIZE; - mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end, + MMU_MIGRATE); ptep = pte_offset_map_lock(mm, pmd, addr, &ptl); if (!pte_same(*ptep, orig_pte)) { @@ -952,7 +955,9 @@ static int replace_page(struct vm_area_struct *vma, struct page *page, flush_cache_page(vma, addr, pte_pfn(*ptep)); ptep_clear_flush_notify(vma, addr, ptep); - set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot)); + set_pte_at_notify(mm, addr, ptep, + mk_pte(kpage, vma->vm_page_prot), + MMU_MIGRATE); page_remove_rmap(page, false); if (!page_mapped(page)) @@ -962,7 +967,8 @@ static int replace_page(struct vm_area_struct *vma, struct page *page, pte_unmap_unlock(ptep, ptl); err = 0; out_mn: - mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end, + MMU_MIGRATE); out: return err; } diff --git a/mm/madvise.c b/mm/madvise.c index 07427d3fcead..7b384db6b179 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -432,9 +432,9 @@ static int madvise_free_single_vma(struct vm_area_struct *vma, tlb_gather_mmu(&tlb, mm, start, end); update_hiwater_rss(mm); - mmu_notifier_invalidate_range_start(mm, start, end); + mmu_notifier_invalidate_range_start(mm, start, end, MMU_MUNMAP); madvise_free_page_range(&tlb, vma, start, end); - mmu_notifier_invalidate_range_end(mm, start, end); + mmu_notifier_invalidate_range_end(mm, start, end, MMU_MUNMAP); tlb_finish_mmu(&tlb, start, end); return 0; diff --git a/mm/memory.c b/mm/memory.c index 93897f23cc11..28d771745b3d 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1035,7 +1035,7 @@ int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, mmun_end = end; if (is_cow) mmu_notifier_invalidate_range_start(src_mm, mmun_start, - mmun_end); + mmun_end, MMU_FORK); ret = 0; dst_pgd = pgd_offset(dst_mm, addr); @@ -1052,7 +1052,8 @@ int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm, } while (dst_pgd++, src_pgd++, addr = next, addr != end); if (is_cow) - mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_end(src_mm, mmun_start, + mmun_end, MMU_FORK); return ret; } @@ -1320,10 +1321,12 @@ void unmap_vmas(struct mmu_gather *tlb, { struct mm_struct *mm = vma->vm_mm; - mmu_notifier_invalidate_range_start(mm, start_addr, end_addr); + mmu_notifier_invalidate_range_start(mm, start_addr, + end_addr, MMU_MUNMAP); for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) unmap_single_vma(tlb, vma, start_addr, end_addr, NULL); - mmu_notifier_invalidate_range_end(mm, start_addr, end_addr); + mmu_notifier_invalidate_range_end(mm, start_addr, + end_addr, MMU_MUNMAP); } /** @@ -1345,10 +1348,10 @@ void zap_page_range(struct vm_area_struct *vma, unsigned long start, lru_add_drain(); tlb_gather_mmu(&tlb, mm, start, end); update_hiwater_rss(mm); - mmu_notifier_invalidate_range_start(mm, start, end); + mmu_notifier_invalidate_range_start(mm, start, end, MMU_MIGRATE); for ( ; vma && vma->vm_start < end; vma = vma->vm_next) unmap_single_vma(&tlb, vma, start, end, details); - mmu_notifier_invalidate_range_end(mm, start, end); + mmu_notifier_invalidate_range_end(mm, start, end, MMU_MIGRATE); tlb_finish_mmu(&tlb, start, end); } @@ -1371,9 +1374,9 @@ static void zap_page_range_single(struct vm_area_struct *vma, unsigned long addr lru_add_drain(); tlb_gather_mmu(&tlb, mm, address, end); update_hiwater_rss(mm); - mmu_notifier_invalidate_range_start(mm, address, end); + mmu_notifier_invalidate_range_start(mm, address, end, MMU_MUNMAP); unmap_single_vma(&tlb, vma, address, end, details); - mmu_notifier_invalidate_range_end(mm, address, end); + mmu_notifier_invalidate_range_end(mm, address, end, MMU_MUNMAP); tlb_finish_mmu(&tlb, address, end); } @@ -2118,7 +2121,8 @@ static int wp_page_copy(struct mm_struct *mm, struct vm_area_struct *vma, __SetPageUptodate(new_page); - mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_start(mm, mmun_start, + mmun_end, MMU_MIGRATE); /* * Re-check the pte - we dropped the lock @@ -2152,7 +2156,7 @@ static int wp_page_copy(struct mm_struct *mm, struct vm_area_struct *vma, * mmu page tables (such as kvm shadow page tables), we want the * new page to be mapped directly into the secondary page table. */ - set_pte_at_notify(mm, address, page_table, entry); + set_pte_at_notify(mm, address, page_table, entry, MMU_MIGRATE); update_mmu_cache(vma, address, page_table); if (old_page) { /* @@ -2191,7 +2195,8 @@ static int wp_page_copy(struct mm_struct *mm, struct vm_area_struct *vma, put_page(new_page); pte_unmap_unlock(page_table, ptl); - mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_end(mm, mmun_start, + mmun_end, MMU_MIGRATE); if (old_page) { /* * Don't let another task, with possibly unlocked vma, diff --git a/mm/migrate.c b/mm/migrate.c index 6c822a7b27e0..f9d911e9d651 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1794,12 +1794,14 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm, WARN_ON(PageLRU(new_page)); /* Recheck the target PMD */ - mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_start(mm, mmun_start, + mmun_end, MMU_MIGRATE); ptl = pmd_lock(mm, pmd); if (unlikely(!pmd_same(*pmd, entry) || page_count(page) != 2)) { fail_putback: spin_unlock(ptl); - mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_end(mm, mmun_start, + mmun_end, MMU_MIGRATE); /* Reverse changes made by migrate_page_copy() */ if (TestClearPageActive(new_page)) @@ -1851,7 +1853,8 @@ fail_putback: set_page_owner_migrate_reason(new_page, MR_NUMA_MISPLACED); spin_unlock(ptl); - mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_end(mm, mmun_start, + mmun_end, MMU_MIGRATE); /* Take an "isolate" reference and put new page on the LRU. */ get_page(new_page); diff --git a/mm/mmu_notifier.c b/mm/mmu_notifier.c index f4259e496f83..7e34526a29d2 100644 --- a/mm/mmu_notifier.c +++ b/mm/mmu_notifier.c @@ -159,8 +159,10 @@ int __mmu_notifier_test_young(struct mm_struct *mm, return young; } -void __mmu_notifier_change_pte(struct mm_struct *mm, unsigned long address, - pte_t pte) +void __mmu_notifier_change_pte(struct mm_struct *mm, + unsigned long address, + pte_t pte, + enum mmu_event event) { struct mmu_notifier *mn; int id; @@ -168,13 +170,14 @@ void __mmu_notifier_change_pte(struct mm_struct *mm, unsigned long address, id = srcu_read_lock(&srcu); hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) { if (mn->ops->change_pte) - mn->ops->change_pte(mn, mm, address, pte); + mn->ops->change_pte(mn, mm, address, pte, event); } srcu_read_unlock(&srcu, id); } void __mmu_notifier_invalidate_page(struct mm_struct *mm, - unsigned long address) + unsigned long address, + enum mmu_event event) { struct mmu_notifier *mn; int id; @@ -182,13 +185,16 @@ void __mmu_notifier_invalidate_page(struct mm_struct *mm, id = srcu_read_lock(&srcu); hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) { if (mn->ops->invalidate_page) - mn->ops->invalidate_page(mn, mm, address); + mn->ops->invalidate_page(mn, mm, address, event); } srcu_read_unlock(&srcu, id); } void __mmu_notifier_invalidate_range_start(struct mm_struct *mm, - unsigned long start, unsigned long end) + unsigned long start, + unsigned long end, + enum mmu_event event) + { struct mmu_notifier *mn; int id; @@ -196,14 +202,17 @@ void __mmu_notifier_invalidate_range_start(struct mm_struct *mm, id = srcu_read_lock(&srcu); hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) { if (mn->ops->invalidate_range_start) - mn->ops->invalidate_range_start(mn, mm, start, end); + mn->ops->invalidate_range_start(mn, mm, start, + end, event); } srcu_read_unlock(&srcu, id); } EXPORT_SYMBOL_GPL(__mmu_notifier_invalidate_range_start); void __mmu_notifier_invalidate_range_end(struct mm_struct *mm, - unsigned long start, unsigned long end) + unsigned long start, + unsigned long end, + enum mmu_event event) { struct mmu_notifier *mn; int id; @@ -221,7 +230,8 @@ void __mmu_notifier_invalidate_range_end(struct mm_struct *mm, if (mn->ops->invalidate_range) mn->ops->invalidate_range(mn, mm, start, end); if (mn->ops->invalidate_range_end) - mn->ops->invalidate_range_end(mn, mm, start, end); + mn->ops->invalidate_range_end(mn, mm, start, + end, event); } srcu_read_unlock(&srcu, id); } diff --git a/mm/mprotect.c b/mm/mprotect.c index b650c5412f58..64ee26fb451d 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -157,7 +157,8 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma, /* invoke the mmu notifier if the pmd is populated */ if (!mni_start) { mni_start = addr; - mmu_notifier_invalidate_range_start(mm, mni_start, end); + mmu_notifier_invalidate_range_start(mm, mni_start, + end, MMU_MPROT); } if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) { @@ -187,7 +188,8 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma, } while (pmd++, addr = next, addr != end); if (mni_start) - mmu_notifier_invalidate_range_end(mm, mni_start, end); + mmu_notifier_invalidate_range_end(mm, mni_start, end, + MMU_MPROT); if (nr_huge_updates) count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates); diff --git a/mm/mremap.c b/mm/mremap.c index 3fa0a467df66..9544022ca67a 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -175,7 +175,8 @@ unsigned long move_page_tables(struct vm_area_struct *vma, mmun_start = old_addr; mmun_end = old_end; - mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, + mmun_end, MMU_MIGRATE); for (; old_addr < old_end; old_addr += extent, new_addr += extent) { cond_resched(); @@ -227,7 +228,8 @@ unsigned long move_page_tables(struct vm_area_struct *vma, if (likely(need_flush)) flush_tlb_range(vma, old_end-len, old_addr); - mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end); + mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, + mmun_end, MMU_MIGRATE); return len + old_addr - old_end; /* how much done */ } diff --git a/mm/rmap.c b/mm/rmap.c index 307b555024ef..da96c7bb28a0 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1040,7 +1040,7 @@ static int page_mkclean_one(struct page *page, struct vm_area_struct *vma, pte_unmap_unlock(pte, ptl); if (ret) { - mmu_notifier_invalidate_page(mm, address); + mmu_notifier_invalidate_page(mm, address, MMU_WRITE_BACK); (*cleaned)++; } out: @@ -1546,7 +1546,7 @@ discard: out_unmap: pte_unmap_unlock(pte, ptl); if (ret != SWAP_FAIL && ret != SWAP_MLOCK && !(flags & TTU_MUNLOCK)) - mmu_notifier_invalidate_page(mm, address); + mmu_notifier_invalidate_page(mm, address, MMU_MIGRATE); out: return ret; } diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 4fd482fb9260..62dd45e9b86f 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -275,7 +275,8 @@ static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn) static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn, struct mm_struct *mm, - unsigned long address) + unsigned long address, + enum mmu_event event) { struct kvm *kvm = mmu_notifier_to_kvm(mn); int need_tlb_flush, idx; @@ -317,7 +318,8 @@ static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn, static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn, struct mm_struct *mm, unsigned long address, - pte_t pte) + pte_t pte, + enum mmu_event event) { struct kvm *kvm = mmu_notifier_to_kvm(mn); int idx; @@ -333,7 +335,8 @@ static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn, static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, struct mm_struct *mm, unsigned long start, - unsigned long end) + unsigned long end, + enum mmu_event event) { struct kvm *kvm = mmu_notifier_to_kvm(mn); int need_tlb_flush = 0, idx; @@ -359,7 +362,8 @@ static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, struct mm_struct *mm, unsigned long start, - unsigned long end) + unsigned long end, + enum mmu_event event) { struct kvm *kvm = mmu_notifier_to_kvm(mn); |