HMM (Heterogeneous Memory Management) v7
Subsystems (24)
So after PTO and before end of year frenzy here is an updated HMM patchset. While not reusing Linus page table design, i use something that is, in my view at least, close to it. Also i avoid pretending that this will be useful to other and move it to hmm specific code. There is a longer justification on why implementing a new page table code instead of using radix or other existing kernel structure as part of commit message.
Everything else is pretty much the same, ie this patchset is just the ground foundation on to which we want to build our features set. Main feature being migrating memory to device memory. The very first version of this patchset already show cased proof of concept of much of the features.
Below is previous patchset cover letter pretty much unchanged as background and motivation for it did not.
What it is ?
In a nutshell HMM is a subsystem that provide an easy to use api to mirror a process address on a device with minimal hardware requirement (mainly device page fault and read only page mapping). This does not rely on ATS and PASID PCIE extensions. It intends to supersede those extensions by allowing to move system memory to device memory in a transparent fashion for core kernel mm code (ie cpu page fault on page residing in device memory will trigger migration back to system memory).
Why doing this ?
We want to be able to mirror a process address space so that compute api such as OpenCL or other similar api can start using the exact same address space on the GPU as on the CPU. This will greatly simplify usages of those api. Moreover we believe that we will see more and more specialize unit functions that will want to mirror process address using their own mmu.
The migration side is simply because GPU memory bandwidth is far beyond than system memory bandwith and there is no sign that this gap is closing (quite the opposite).
Current status and future features :
None of this core code change in any major way core kernel mm code. This is simple ground work with no impact on existing code path. Features that will be implemented on top of this are :
1 - Tansparently handle page mapping on behalf of device driver (DMA). 2 - Improve DMA api to better match new usage pattern of HMM. 3 - Migration of anonymous memory to device memory.
4 - Locking memory to remote memory (CPU access trigger SIGBUS). 5 - Access exclusion btw CPU and device for atomic operations. 6 - Migration of file backed memory to device memory.
How future features will be implemented :
1 - Simply use existing DMA api to map page on behalf of a device. 2 - Introduce new DMA api to match new semantic of HMM. It is no longer page
we map but address range and managing which page is effectively backing an address should be easy to update. I gave a presentation about that during this LPC.
3 - Requires change to cpu page fault code path to handle migration back to
system memory on cpu access. An implementation of this was already sent as part of v1. This will be low impact and only add a new special swap type handling to existing fault code.
4 - Require a new syscall as i can not see which current syscall would be
appropriate for this. My first feeling was to use mbind as it has the right semantic (binding a range of address to a device) but mbind is too numa centric.
Second one was madvise, but semantic does not match, madvise does allow kernel to ignore them while we do want to block cpu access for as long as the range is bind to a device.
So i do not think any of existing syscall can be extended with new flags but maybe i am wrong.
5 - Allowing to map a page as read only on the CPU while a device perform
some atomic operation on it (this is mainly to work around system bus that do not support atomic memory access and sadly there is a large base of hardware without that feature).
Easiest implementation would be using some page flags but there is none left. So it must be some flags in vma to know if there is a need to query HMM for write protection.
6 - This is the trickiest one to implement and while i showed a proof of
concept with v1, i am still have a lot of conflictual feeling about how to achieve this.
As usual comments are more then welcome. Thanks in advance to anyone that take a look at this code.
Previous patchset posting :
v1 http://lwn.net/Articles/597289/
v2 https://lkml.org/lkml/2014/6/12/559 (cover letter did not make it to ml) v3 https://lkml.org/lkml/2014/6/13/633
v4 https://lkml.org/lkml/2014/8/29/423
v5 https://lkml.org/lkml/2014/11/3/759
v6 http://lwn.net/Articles/619737/
Cheers,
Jérôme
To: "Andrew Morton" <[email protected]>,
Cc: <[email protected]>,
Cc: linux-mm <[email protected]>,
Cc: <[email protected]>,
Cc: "Linus Torvalds" <[email protected]>,
Cc: "Mel Gorman" <[email protected]>,
Cc: "H. Peter Anvin" <[email protected]>,
Cc: "Peter Zijlstra" <[email protected]>,
Cc: "Linda Wang" <[email protected]>,
Cc: "Kevin E Martin" <[email protected]>,
Cc: "Jerome Glisse" <[email protected]>,
Cc: "Andrea Arcangeli" <[email protected]>,
Cc: "Johannes Weiner" <[email protected]>,
Cc: "Larry Woodman" <[email protected]>,
Cc: "Rik van Riel" <[email protected]>,
Cc: "Dave Airlie" <[email protected]>,
Cc: "Jeff Law" <[email protected]>,
Cc: "Brendan Conoboy" <[email protected]>,
Cc: "Joe Donohue" <[email protected]>,
Cc: "Duncan Poole" <[email protected]>,
Cc: "Sherry Cheung" <[email protected]>,
Cc: "Subhash Gutti" <[email protected]>,
Cc: "John Hubbard" <[email protected]>,
Cc: "Mark Hairgrove" <[email protected]>,
Cc: "Lucien Dunning" <[email protected]>,
Cc: "Cameron Buschardt" <[email protected]>,
Cc: "Arvind Gopalakrishnan" <[email protected]>,
Cc: "Haggai Eran" <[email protected]>,
Cc: "Or Gerlitz" <[email protected]>,
Cc: "Sagi Grimberg" <[email protected]>
Cc: "Shachar Raindel" <[email protected]>,
Cc: "Liran Liss" <[email protected]>,
Cc: "Roland Dreier" <[email protected]>,
Cc: "Sander, Ben" <[email protected]>,
Cc: "Stoner, Greg" <[email protected]>,
Cc: "Bridgman, John" <[email protected]>,
Cc: "Mantor, Michael" <[email protected]>,
Cc: "Blinzer, Paul" <[email protected]>,
Cc: "Morichetti, Laurent" <[email protected]>,
Cc: "Deucher, Alexander" <[email protected]>,
Cc: "Gabbay, Oded" <[email protected]>,
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/7 mmu_notifier: add event information to address invalidation v6
From: JA(C)rA'me Glisse <[email protected]>
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 hugue 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.
--- drivers/gpu/drm/i915/i915_gem_userptr.c | 3 +- drivers/gpu/drm/radeon/radeon_mn.c | 3 +- drivers/infiniband/core/umem_odp.c | 9 ++- drivers/iommu/amd_iommu_v2.c | 3 +- drivers/misc/sgi-gru/grutlbpurge.c | 9 ++- drivers/xen/gntdev.c | 9 ++- fs/proc/task_mmu.c | 6 +- include/linux/mmu_notifier.h | 131 ++++++++++++++++++++++++++------ kernel/events/uprobes.c | 10 ++- mm/filemap_xip.c | 2 +- mm/huge_memory.c | 39 ++++++---- mm/hugetlb.c | 23 +++--- mm/ksm.c | 18 +++-- mm/madvise.c | 4 +- mm/memory.c | 27 ++++--- mm/migrate.c | 9 ++- mm/mmu_notifier.c | 28 ++++--- mm/mprotect.c | 6 +- mm/mremap.c | 6 +- mm/rmap.c | 24 ++++-- virt/kvm/kvm_main.c | 12 ++- 21 files changed, 274 insertions(+), 107 deletions(-)
drivers/gpu/drm/i915/i915_gem_userptr.c +2 -1
@@ -129,7 +129,8 @@ restart: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);struct interval_tree_node *it =NULL ;
drivers/gpu/drm/radeon/radeon_mn.c +2 -1
@@ -109,7 +109,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;
drivers/infiniband/core/umem_odp.c +6 -3
@@ -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);
drivers/iommu/amd_iommu_v2.c +2 -1
@@ -402,7 +402,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); }
drivers/misc/sgi-gru/grutlbpurge.c +6 -3
@@ -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);
drivers/xen/gntdev.c +6 -3
@@ -428,7 +428,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;
@@ -445,9 +447,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,
fs/proc/task_mmu.c +4 -2
@@ -875,7 +875,8 @@ 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_ISDIRTY ); }for (vma = mm->mmap; vma; vma = vma->vm_next) { cp.vma = vma;
@@ -900,7 +901,8 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf, &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_ISDIRTY );flush_tlb_mm (mm);up_read (&mm->mmap_sem);mmput (mm);
include/linux/mmu_notifier.h +109 -22
218 lines not on this page · raw message
kernel/events/uprobes.c +7 -3
@@ -176,7 +176,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)
@@ -194,7 +195,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);if (!page_mapped (page))
@@ -208,7 +211,8 @@ static int __replace_page(struct vm_area_struct *vma, unsigned long addr, err =0 ; unlock:mem_cgroup_cancel_charge (kpage, memcg); -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; }
mm/filemap_xip.c +1 -1
@@ -193,7 +193,7 @@ retry:BUG_ON (pte_dirty (pteval));pte_unmap_unlock (pte, ptl);/* must invalidate_page _before_ freeing the page */ -mmu_notifier_invalidate_page (mm, address); +mmu_notifier_invalidate_page (mm, address,MMU_MIGRATE );page_cache_release (page); } }
mm/huge_memory.c +26 -13
@@ -1028,7 +1028,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)))
@@ -1062,7 +1063,8 @@ static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,page_remove_rmap (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 ); ret |=VM_FAULT_WRITE ;put_page (page);
@@ -1072,7 +1074,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 );
@@ -1164,7 +1167,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)
@@ -1196,7 +1200,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:
@@ -1667,7 +1672,8 @@ static int __split_huge_page_splitting(struct page *page,const unsigned long mmun_start = address;const unsigned long 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_HSPLIT ); pmd =page_check_address_pmd (page, mm, address,PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG , &ptl);if (pmd) {
@@ -1683,7 +1689,8 @@ static int __split_huge_page_splitting(struct page *page, ret =1 ;spin_unlock (ptl); } -mmu_notifier_invalidate_range_end (mm, mmun_start, mmun_end); +mmu_notifier_invalidate_range_end (mm, mmun_start, + mmun_end,MMU_HSPLIT );return ret; }
@@ -2504,7 +2511,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
@@ -2514,7 +2522,8 @@ static void collapse_huge_page(struct mm_struct *mm,*/ _pmd =pmdp_clear_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);
@@ -2905,24 +2914,28 @@ void __split_huge_page_pmd(struct vm_area_struct *vma, unsigned long address, mmun_start = haddr; mmun_end = haddr +HPAGE_PMD_SIZE ; again: -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_trans_huge (*pmd))) {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 );return ; }if (is_huge_zero_pmd (*pmd)) {__split_huge_zero_page_pmd (vma, haddr, pmd);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 );return ; } page =pmd_page (*pmd);VM_BUG_ON_PAGE (!page_count (page), page);get_page (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 );split_huge_page (page);
mm/hugetlb.c +14 -9
@@ -2560,7 +2560,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;
@@ -2614,7 +2615,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; }
@@ -2640,7 +2642,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) {
@@ -2713,7 +2716,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); }
@@ -2891,8 +2895,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
@@ -2913,7 +2917,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:page_cache_release (new_page); out_release_old:
@@ -3351,7 +3356,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;
@@ -3382,7 +3387,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; }
mm/ksm.c +12 -6
@@ -872,7 +872,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_WRITE_PROTECT ); ptep =page_check_address (page, mm, addr, &ptl,0 );if (!ptep)
@@ -904,7 +905,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_WRITE_PROTECT ); } *orig_pte = *ptep; err =0 ;
@@ -912,7 +913,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_WRITE_PROTECT ); out:return err; }
@@ -948,7 +950,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)) {
@@ -961,7 +964,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);if (!page_mapped (page))
@@ -971,7 +976,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; }
mm/madvise.c +2 -2
@@ -376,9 +376,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 ;
mm/memory.c +16 -11
@@ -1050,7 +1050,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_MIGRATE ); ret =0 ; dst_pgd =pgd_offset (dst_mm, addr);
@@ -1067,7 +1067,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_MIGRATE );return ret; }
@@ -1360,10 +1361,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 ); }/**
@@ -1385,10 +1388,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); }
@@ -1411,9 +1414,9 @@ static void zap_page_range_single(struct vm_area_struct *vma, unsigned long addrlru_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); }
@@ -2198,7 +2201,8 @@ gotten: mmun_start = address &PAGE_MASK ; mmun_end = mmun_start +PAGE_SIZE ; -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
@@ -2230,7 +2234,7 @@ gotten:* 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) {/*
@@ -2269,7 +2273,8 @@ gotten: unlock:pte_unmap_unlock (page_table, ptl);if (mmun_end > mmun_start) -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,
mm/migrate.c +6 -3
@@ -1801,12 +1801,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))
@@ -1860,7 +1862,8 @@ fail_putback:page_remove_rmap (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 );/* Take an "isolate" reference and put new page on the LRU. */ get_page (new_page);
mm/mmu_notifier.c +19 -9
@@ -142,8 +142,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;
@@ -151,13 +153,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;
@@ -165,13 +168,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;
@@ -179,14 +185,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;
@@ -204,7 +213,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); }
mm/mprotect.c +4 -2
@@ -152,7 +152,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)) {
@@ -180,7 +181,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);
mm/mremap.c +4 -2
@@ -177,7 +177,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 ();
@@ -229,7 +230,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 */ }
mm/rmap.c +18 -6
@@ -869,7 +869,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:
@@ -1171,8 +1171,12 @@ static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,spinlock_t *ptl;int ret =SWAP_AGAIN ;enum ttu_flags flags = (enum ttu_flags )arg; +enum mmu_event event =MMU_MIGRATE ;int dirty =0 ; +if (flags &TTU_MUNLOCK ) + event =MMU_MUNLOCK ; + pte =page_check_address (page, mm, address, &ptl,0 );if (!pte) goto out;
@@ -1292,7 +1296,7 @@ discard: out_unmap:pte_unmap_unlock (pte, ptl);if (ret !=SWAP_FAIL && !(flags &TTU_MUNLOCK )) -mmu_notifier_invalidate_page (mm, address); +mmu_notifier_invalidate_page (mm, address, event); out:return ret;
@@ -1346,7 +1350,9 @@ out_mlock:#define CLUSTER_MASK (~(CLUSTER_SIZE - 1))static int try_to_unmap_cluster (unsigned long cursor,unsigned int *mapcount, -struct vm_area_struct *vma,struct page *check_page) +struct vm_area_struct *vma, +struct page *check_page, +enum ttu_flags flags) {struct mm_struct *mm = vma->vm_mm;pmd_t *pmd;
@@ -1360,6 +1366,10 @@ static int try_to_unmap_cluster(unsigned long cursor, unsigned int *mapcount,unsigned long end;int ret =SWAP_AGAIN ;int locked_vma =0 ; +enum mmu_event event =MMU_MIGRATE ; + +if (flags &TTU_MUNLOCK ) + event =MMU_MUNLOCK ; address = (vma->vm_start + cursor) &CLUSTER_MASK ; end = address +CLUSTER_SIZE ;
@@ -1374,7 +1384,7 @@ static int try_to_unmap_cluster(unsigned long cursor, unsigned int *mapcount, mmun_start = address; mmun_end = end; -mmu_notifier_invalidate_range_start (mm, mmun_start, mmun_end); +mmu_notifier_invalidate_range_start (mm, mmun_start, mmun_end, event);/* * If we can acquire the mmap_sem for read, and vma is VM_LOCKED,
@@ -1443,7 +1453,7 @@ static int try_to_unmap_cluster(unsigned long cursor, unsigned int *mapcount, (*mapcount)--; }pte_unmap_unlock (pte -1 , ptl); -mmu_notifier_invalidate_range_end (mm, mmun_start, mmun_end); +mmu_notifier_invalidate_range_end (mm, mmun_start, mmun_end, event);if (locked_vma)up_read (&vma->vm_mm->mmap_sem);return ret;
@@ -1499,7 +1509,9 @@ static int try_to_unmap_nonlinear(struct page *page,while (cursor < max_nl_cursor && cursor < vma->vm_end - vma->vm_start) {if (try_to_unmap_cluster (cursor, &mapcount, - vma, page) ==SWAP_MLOCK ) + vma, page, + (enum ttu_flags )arg) + ==SWAP_MLOCK ) ret =SWAP_MLOCK ; cursor +=CLUSTER_SIZE ; vma->vm_private_data = (void *) cursor;
virt/kvm/kvm_main.c +8 -4
@@ -254,7 +254,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;
@@ -296,7 +297,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;
@@ -312,7 +314,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;
@@ -338,7 +341,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);
Signature · 7 lines
1.9.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 2/7 mmu_notifier: keep track of active invalidation ranges v2
In reply to [email protected]From: JA(C)rA'me Glisse <[email protected]>
The mmu_notifier_invalidate_range_start() and mmu_notifier_invalidate_range_end() can be considered as forming an "atomic" section for the cpu page table update point of view. Between this two function the cpu page table content is unreliable for the address range being invalidated.
Current user such as kvm need to know when they can trust the content of the cpu page table. This becomes even more important to new users of the mmu_notifier api (such as HMM or ODP).
This patch use a structure define at all call site to invalidate_range_start() that is added to a list for the duration of the invalidation. It adds two new helpers to allow querying if a range is being invalidated or to wait for a range to become valid.
For proper synchronization, user must block new range invalidation from inside there invalidate_range_start() callback, before calling the helper functions. Otherwise there is no garanty that a new range invalidation will not be added after the call to the helper function to query for existing range.
Changed since v1:
- Fix a possible deadlock in mmu_notifier_range_wait_valid()
--- drivers/gpu/drm/i915/i915_gem_userptr.c | 9 ++-- drivers/gpu/drm/radeon/radeon_mn.c | 14 ++---- drivers/infiniband/core/umem_odp.c | 16 +++--- drivers/misc/sgi-gru/grutlbpurge.c | 15 +++--- drivers/xen/gntdev.c | 15 +++--- fs/proc/task_mmu.c | 12 +++-- include/linux/mmu_notifier.h | 60 +++++++++++++---------- kernel/events/uprobes.c | 13 +++-- mm/huge_memory.c | 78 +++++++++++++---------------- mm/hugetlb.c | 55 +++++++++++---------- mm/ksm.c | 28 +++++------ mm/madvise.c | 8 ++- mm/memory.c | 78 ++++++++++++++++------------- mm/migrate.c | 36 +++++++------- mm/mmu_notifier.c | 87 ++++++++++++++++++++++++++++----- mm/mprotect.c | 18 ++++--- mm/mremap.c | 14 +++--- mm/rmap.c | 15 +++--- virt/kvm/kvm_main.c | 10 ++-- 19 files changed, 322 insertions(+), 259 deletions(-)
drivers/gpu/drm/i915/i915_gem_userptr.c +4 -5
@@ -128,16 +128,15 @@ restart:static void i915_gem_userptr_mn_invalidate_range_start (struct mmu_notifier *_mn,struct mm_struct *mm, -unsigned long start, -unsigned long end, -enum mmu_event event) +const struct mmu_notifier_range *range) { struct i915_mmu_notifier *mn = container_of(_mn, struct i915_mmu_notifier, mn);struct interval_tree_node *it =NULL ; -unsigned long next = start; +unsigned long next = range->start;unsigned long serial =0 ; +/* interval ranges are inclusive, but invalidate range is exclusive */ +unsigned long end = range->end -1 , start = range->start; - end--;/* interval ranges are inclusive, but invalidate range is exclusive */ while (next < end) {struct drm_i915_gem_object *obj =NULL ;
drivers/gpu/drm/radeon/radeon_mn.c +5 -9
@@ -100,34 +100,30 @@ static void radeon_mn_release(struct mmu_notifier *mn,* * @mn: our notifier * @mn: the mm this callback is about -* @start: start of updated range -* @end: end of updated range +* @range: Address range information. * * We block for all BOs between start and end to be idle and * unmap them by move them into system domain again. */ static void radeon_mn_invalidate_range_start (struct mmu_notifier *mn,struct mm_struct *mm, -unsigned long start, -unsigned long end, -enum mmu_event event) +const struct mmu_notifier_range *range) { struct radeon_mn *rmn = container_of(mn, struct radeon_mn, mn);struct interval_tree_node *it; -/* notification is exclusive, but interval is inclusive */ - end -=1 ; +unsigned long end = range->end -1 ;mutex_lock (&rmn->lock); - it =interval_tree_iter_first (&rmn->objects, start, end); + it =interval_tree_iter_first (&rmn->objects, range->start, end);while (it) {struct radeon_bo *bo;struct fence *fence;int r; bo = container_of(it, struct radeon_bo, mn_it); - it =interval_tree_iter_next (it, start, end); + it =interval_tree_iter_next (it, range->start, end); r =radeon_bo_reserve (bo, true);if (r) {
drivers/infiniband/core/umem_odp.c +6 -10
@@ -192,9 +192,7 @@ 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, -enum mmu_event event) +const struct mmu_notifier_range *range) { struct ib_ucontext *context = container_of(mn, struct ib_ucontext, mn);
@@ -203,8 +201,8 @@ static void ib_umem_notifier_invalidate_range_start(struct mmu_notifier *mn,ib_ucontext_notifier_start_account (context);down_read (&context->umem_rwsem); -rbt_ib_umem_for_each_in_range (&context->umem_tree, start, - end, +rbt_ib_umem_for_each_in_range (&context->umem_tree, range->start, + range->end, invalidate_range_start_trampoline,NULL );up_read (&context->umem_rwsem); }
@@ -218,9 +216,7 @@ 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, -enum mmu_event event) +const struct mmu_notifier_range *range) { struct ib_ucontext *context = container_of(mn, struct ib_ucontext, mn);
@@ -228,8 +224,8 @@ static void ib_umem_notifier_invalidate_range_end(struct mmu_notifier *mn,return ;down_read (&context->umem_rwsem); -rbt_ib_umem_for_each_in_range (&context->umem_tree, start, - end, +rbt_ib_umem_for_each_in_range (&context->umem_tree, range->start, + range->end, invalidate_range_end_trampoline,NULL );up_read (&context->umem_rwsem);ib_ucontext_notifier_end_account (context);
drivers/misc/sgi-gru/grutlbpurge.c +7 -8
@@ -221,8 +221,7 @@ 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, -enum mmu_event event) +const struct mmu_notifier_range *range) { struct gru_mm_struct *gms = container_of(mn, struct gru_mm_struct, ms_notifier);
@@ -230,14 +229,13 @@ static void gru_invalidate_range_start(struct mmu_notifier *mn,STAT (mmu_invalidate_range);atomic_inc (&gms->ms_range_active);gru_dbg (grudev,"gms %p, start 0x%lx, end 0x%lx, act %d\n" , gms, - start, end,atomic_read (&gms->ms_range_active)); -gru_flush_tlb_range (gms, start, end - start); + range->start, range->end,atomic_read (&gms->ms_range_active)); +gru_flush_tlb_range (gms, range->start, range->end - range->start); }static void gru_invalidate_range_end (struct mmu_notifier *mn, -struct mm_struct *mm,unsigned long start, -unsigned long end, -enum mmu_event event) +struct mm_struct *mm, +const struct mmu_notifier_range *range) { struct gru_mm_struct *gms = container_of(mn, struct gru_mm_struct, ms_notifier);
@@ -246,7 +244,8 @@ static void gru_invalidate_range_end(struct mmu_notifier *mn, (void )atomic_dec_and_test (&gms->ms_range_active);wake_up_all (&gms->ms_wait_queue); -gru_dbg (grudev,"gms %p, start 0x%lx, end 0x%lx\n" , gms, start, end); +gru_dbg (grudev,"gms %p, start 0x%lx, end 0x%lx\n" , gms, + range->start, range->end); } static void gru_invalidate_page(struct mmu_notifier *mn, struct mm_struct *mm,
drivers/xen/gntdev.c +9 -6
@@ -428,19 +428,17 @@ 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, -enum mmu_event event) +const struct mmu_notifier_range *range) { struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);struct grant_map *map;spin_lock (&priv->lock);list_for_each_entry (map, &priv->maps, next) { -unmap_if_in_range (map, start, end); +unmap_if_in_range (map, range->start, range->end); }list_for_each_entry (map, &priv->freeable_maps, next) { -unmap_if_in_range (map, start, end); +unmap_if_in_range (map, range->start, range->end); }spin_unlock (&priv->lock); }
@@ -450,7 +448,12 @@ static void mn_invl_page(struct mmu_notifier *mn,unsigned long address, enum mmu_event event) { - mn_invl_range_start(mn, mm, address, address + PAGE_SIZE, event); + struct mmu_notifier_range range; + + range.start = address; + range.end = address +PAGE_SIZE ; + range.event = event; +mn_invl_range_start (mn, mm, &range); } static void mn_release(struct mmu_notifier *mn,
fs/proc/task_mmu.c +8 -4
@@ -861,6 +861,12 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf, .mm = mm, .private = &cp, }; +struct mmu_notifier_range range = { + .start =0 , + .end =-1UL , + .event =MMU_ISDIRTY , + }; +down_read (&mm->mmap_sem);if (type ==CLEAR_REFS_SOFT_DIRTY ) {for (vma = mm->mmap; vma; vma = vma->vm_next) {
@@ -875,8 +881,7 @@ 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_ISDIRTY ); +mmu_notifier_invalidate_range_start (mm, &range); }for (vma = mm->mmap; vma; vma = vma->vm_next) { cp.vma = vma;
@@ -901,8 +906,7 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf, &clear_refs_walk); }if (type ==CLEAR_REFS_SOFT_DIRTY ) -mmu_notifier_invalidate_range_end (mm,0 , --1 ,MMU_ISDIRTY ); +mmu_notifier_invalidate_range_end (mm, &range);flush_tlb_mm (mm);up_read (&mm->mmap_sem);mmput (mm);
include/linux/mmu_notifier.h +34 -26
112 lines not on this page · raw message
kernel/events/uprobes.c +6 -7
@@ -164,9 +164,7 @@ static int __replace_page(struct vm_area_struct *vma, unsigned long addr,spinlock_t *ptl;pte_t *ptep;int err; -/* For mmu_notifiers */ -const unsigned long mmun_start = addr; -const unsigned long mmun_end = addr +PAGE_SIZE ; +struct mmu_notifier_range range;struct mem_cgroup *memcg; err =mem_cgroup_try_charge (kpage, vma->vm_mm,GFP_KERNEL , &memcg);
@@ -176,8 +174,10 @@ 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_MIGRATE ); + range.start = addr; + range.end = addr +PAGE_SIZE ; + range.event =MMU_MIGRATE ; +mmu_notifier_invalidate_range_start (mm, &range); err = -EAGAIN ; ptep =page_check_address (page, mm, addr, &ptl,0 );if (!ptep)
@@ -211,8 +211,7 @@ static int __replace_page(struct vm_area_struct *vma, unsigned long addr, err =0 ; unlock:mem_cgroup_cancel_charge (kpage, memcg); -mmu_notifier_invalidate_range_end (mm, mmun_start, - mmun_end,MMU_MIGRATE ); +mmu_notifier_invalidate_range_end (mm, &range);unlock_page (page);return err; }
mm/huge_memory.c +33 -45
175 lines not on this page · raw message
mm/hugetlb.c +28 -27
136 lines not on this page · raw message
mm/ksm.c +12 -16
@@ -855,14 +855,13 @@ static inline int pages_identical(struct page *page1, struct page *page2)static int write_protect_page (struct vm_area_struct *vma,struct page *page,pte_t *orig_pte) { +struct mmu_notifier_range range;struct mm_struct *mm = vma->vm_mm;unsigned long addr;pte_t *ptep;spinlock_t *ptl;int swapped;int err = -EFAULT ; -unsigned long mmun_start;/* For mmu_notifiers */ -unsigned long mmun_end;/* For mmu_notifiers */ addr =page_address_in_vma (page, vma);if (addr == -EFAULT )
@@ -870,10 +869,10 @@ static int write_protect_page(struct vm_area_struct *vma, struct page *page,BUG_ON (PageTransCompound (page)); - mmun_start = addr; - mmun_end = addr +PAGE_SIZE ; -mmu_notifier_invalidate_range_start (mm, mmun_start, mmun_end, -MMU_WRITE_PROTECT ); + range.start = addr; + range.end = addr +PAGE_SIZE ; + range.event =MMU_WRITE_PROTECT ; +mmu_notifier_invalidate_range_start (mm, &range); ptep =page_check_address (page, mm, addr, &ptl,0 );if (!ptep)
@@ -913,8 +912,7 @@ 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_WRITE_PROTECT ); +mmu_notifier_invalidate_range_end (mm, &range); out:return err; }
@@ -937,8 +935,7 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,spinlock_t *ptl;unsigned long addr;int err = -EFAULT ; -unsigned long mmun_start;/* For mmu_notifiers */ -unsigned long mmun_end;/* For mmu_notifiers */ +struct mmu_notifier_range range; addr =page_address_in_vma (page, vma);if (addr == -EFAULT )
@@ -948,10 +945,10 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,if (!pmd) goto out; - mmun_start = addr; - mmun_end = addr +PAGE_SIZE ; -mmu_notifier_invalidate_range_start (mm, mmun_start, mmun_end, -MMU_MIGRATE ); + range.start = addr; + range.end = addr +PAGE_SIZE ; + range.event =MMU_MIGRATE ; +mmu_notifier_invalidate_range_start (mm, &range); ptep =pte_offset_map_lock (mm, pmd, addr, &ptl);if (!pte_same (*ptep, orig_pte)) {
@@ -976,8 +973,7 @@ 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_MIGRATE ); +mmu_notifier_invalidate_range_end (mm, &range); out:return err; }
mm/madvise.c +6 -2
@@ -357,6 +357,7 @@ static int madvise_free_single_vma(struct vm_area_struct *vma,unsigned long start, end;struct mm_struct *mm = vma->vm_mm;struct mmu_gather tlb; +struct mmu_notifier_range range;if (vma->vm_flags & (VM_LOCKED |VM_HUGETLB |VM_PFNMAP ))return -EINVAL ;
@@ -376,9 +377,12 @@ 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_MUNMAP ); + range.start = start; + range.end = end; + range.event =MMU_MUNMAP ; +mmu_notifier_invalidate_range_start (mm, &range);madvise_free_page_range (&tlb, vma, start, end); -mmu_notifier_invalidate_range_end (mm, start, end,MMU_MUNMAP ); +mmu_notifier_invalidate_range_end (mm, &range);tlb_finish_mmu (&tlb, start, end);return 0 ;
mm/memory.c +44 -34
144 lines not on this page · raw message
mm/migrate.c +18 -18
@@ -1763,10 +1763,13 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm,int isolated =0 ;struct page *new_page =NULL ;int page_lru =page_is_file_cache (page); -unsigned long mmun_start = address &HPAGE_PMD_MASK ; -unsigned long mmun_end = mmun_start +HPAGE_PMD_SIZE ; +struct mmu_notifier_range range;pmd_t orig_entry; + range.start = address &HPAGE_PMD_MASK ; + range.end = range.start +HPAGE_PMD_SIZE ; + range.event =MMU_MIGRATE ; +/* * Rate-limit the amount of data that is being migrated to a node. * Optimal placement is no good if the memory bus is saturated and
@@ -1788,7 +1791,7 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm, }if (mm_tlb_flush_pending (mm)) -flush_tlb_range (vma, mmun_start, mmun_end); +flush_tlb_range (vma, range.start, range.end);/* Prepare a page as a migration target */ __set_page_locked (new_page);
@@ -1801,14 +1804,12 @@ 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_MIGRATE ); +mmu_notifier_invalidate_range_start (mm, &range); 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_MIGRATE ); +mmu_notifier_invalidate_range_end (mm, &range);/* Reverse changes made by migrate_page_copy() */ if (TestClearPageActive (new_page))
@@ -1841,17 +1842,17 @@ fail_putback:* The SetPageUptodate on the new page and page_add_new_anon_rmap * guarantee the copy is visible before the pagetable update. */ -flush_cache_range (vma, mmun_start, mmun_end); -page_add_anon_rmap (new_page, vma, mmun_start); -pmdp_clear_flush_notify (vma, mmun_start, pmd); -set_pmd_at (mm, mmun_start, pmd, entry); -flush_tlb_range (vma, mmun_start, mmun_end); +flush_cache_range (vma, range.start, range.end); +page_add_anon_rmap (new_page, vma, range.start); +pmdp_clear_flush_notify (vma, range.start, pmd); +set_pmd_at (mm, range.start, pmd, entry); +flush_tlb_range (vma, range.start, range.end);update_mmu_cache_pmd (vma, address, &entry);if (page_count (page) !=2 ) { -set_pmd_at (mm, mmun_start, pmd, orig_entry); -flush_tlb_range (vma, mmun_start, mmun_end); -mmu_notifier_invalidate_range (mm, mmun_start, mmun_end); +set_pmd_at (mm, range.start, pmd, orig_entry); +flush_tlb_range (vma, range.start, range.end); +mmu_notifier_invalidate_range (mm, range.start, range.end);update_mmu_cache_pmd (vma, address, &entry);page_remove_rmap (new_page); goto fail_putback;
@@ -1862,8 +1863,7 @@ fail_putback:page_remove_rmap (page);spin_unlock (ptl); -mmu_notifier_invalidate_range_end (mm, mmun_start, - mmun_end,MMU_MIGRATE ); +mmu_notifier_invalidate_range_end (mm, &range);/* Take an "isolate" reference and put new page on the LRU. */ get_page (new_page);
@@ -1888,7 +1888,7 @@ out_dropref: ptl =pmd_lock (mm, pmd);if (pmd_same (*pmd, entry)) { entry =pmd_mknonnuma (entry); -set_pmd_at (mm, mmun_start, pmd, entry); +set_pmd_at (mm, range.start, pmd, entry);update_mmu_cache_pmd (vma, address, &entry); }spin_unlock (ptl);
mm/mmu_notifier.c +76 -11
130 lines not on this page · raw message
mm/mprotect.c +10 -8
@@ -139,7 +139,9 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,unsigned long next;unsigned long pages =0 ;unsigned long nr_huge_updates =0 ; -unsigned long mni_start =0 ; +struct mmu_notifier_range range = { + .start =0 , + }; pmd =pmd_offset (pud, addr); do {
@@ -150,10 +152,11 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,continue ;/* 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_MPROT ); +if (!range.start) { + range.start = addr; + range.end = end; + range.event =MMU_MPROT ; +mmu_notifier_invalidate_range_start (mm, &range); }if (pmd_trans_huge (*pmd)) {
@@ -180,9 +183,8 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma, pages += this_pages; }while (pmd++, addr = next, addr != end); -if (mni_start) -mmu_notifier_invalidate_range_end (mm, mni_start, end, -MMU_MPROT ); +if (range.start) +mmu_notifier_invalidate_range_end (mm, &range);if (nr_huge_updates)count_vm_numa_events (NUMA_HUGE_PTE_UPDATES , nr_huge_updates);
mm/mremap.c +6 -8
@@ -167,18 +167,17 @@ unsigned long move_page_tables(struct vm_area_struct *vma, bool need_rmap_locks) {unsigned long extent, next, old_end; +struct mmu_notifier_range range;pmd_t *old_pmd, *new_pmd;bool need_flush = false; -unsigned long mmun_start;/* For mmu_notifiers */ -unsigned long mmun_end;/* For mmu_notifiers */ old_end = old_addr + len;flush_cache_range (vma, old_addr, old_end); - mmun_start = old_addr; - mmun_end = old_end; -mmu_notifier_invalidate_range_start (vma->vm_mm, mmun_start, - mmun_end,MMU_MIGRATE ); + range.start = old_addr; + range.end = old_end; + range.event =MMU_MIGRATE ; +mmu_notifier_invalidate_range_start (vma->vm_mm, &range);for (; old_addr < old_end; old_addr += extent, new_addr += extent) {cond_resched ();
@@ -230,8 +229,7 @@ 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_MIGRATE ); +mmu_notifier_invalidate_range_end (vma->vm_mm, &range);return len + old_addr - old_end;/* how much done */ }
mm/rmap.c +7 -8
@@ -1361,15 +1361,14 @@ static int try_to_unmap_cluster(unsigned long cursor, unsigned int *mapcount,spinlock_t *ptl;struct page *page;unsigned long address; -unsigned long mmun_start;/* For mmu_notifiers */ -unsigned long mmun_end;/* For mmu_notifiers */ +struct mmu_notifier_range range;unsigned long end;int ret =SWAP_AGAIN ;int locked_vma =0 ; -enum mmu_event event =MMU_MIGRATE ; + range.event =MMU_MIGRATE ;if (flags &TTU_MUNLOCK ) - event =MMU_MUNLOCK ; + range.event =MMU_MUNLOCK ; address = (vma->vm_start + cursor) &CLUSTER_MASK ; end = address +CLUSTER_SIZE ;
@@ -1382,9 +1381,9 @@ static int try_to_unmap_cluster(unsigned long cursor, unsigned int *mapcount,if (!pmd)return ret; - mmun_start = address; - mmun_end = end; -mmu_notifier_invalidate_range_start (mm, mmun_start, mmun_end, event); + range.start = address; + range.end = end; +mmu_notifier_invalidate_range_start (mm, &range);/* * If we can acquire the mmap_sem for read, and vma is VM_LOCKED,
@@ -1453,7 +1452,7 @@ static int try_to_unmap_cluster(unsigned long cursor, unsigned int *mapcount, (*mapcount)--; }pte_unmap_unlock (pte -1 , ptl); -mmu_notifier_invalidate_range_end (mm, mmun_start, mmun_end, event); +mmu_notifier_invalidate_range_end (mm, &range);if (locked_vma)up_read (&vma->vm_mm->mmap_sem);return ret;
virt/kvm/kvm_main.c +3 -7
@@ -313,9 +313,7 @@ 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, -enum mmu_event event) +const struct mmu_notifier_range *range) {struct kvm *kvm =mmu_notifier_to_kvm (mn);int need_tlb_flush =0 , idx;
@@ -328,7 +326,7 @@ static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,* count is also read inside the mmu_lock critical section. */ kvm->mmu_notifier_count++; - need_tlb_flush =kvm_unmap_hva_range (kvm, start, end); + need_tlb_flush =kvm_unmap_hva_range (kvm, range->start, range->end); need_tlb_flush |= kvm->tlbs_dirty;/* we've to flush the tlb before the pages can be freed */ if (need_tlb_flush)
@@ -340,9 +338,7 @@ 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, -enum mmu_event event) +const struct mmu_notifier_range *range) {struct kvm *kvm =mmu_notifier_to_kvm (mn);
Signature · 7 lines
1.9.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 2/7] mmu_notifier: keep track of active invalidation ranges v2
include/linux/mmu_notifier.h static inline void mmu_notifier_invalidate_page(struct mm_struct *mm, · line 350
Quoted 11 lines · On 22/12/2014 18:48, [email protected] wrote:
static inline void mmu_notifier_invalidate_range_start(struct
mm_struct*mm,include/linux/mmu_notifier.h +6
-3-unsigned long start, - unsigned long end, - enum mmu_event event) + struct mmu_notifier_range *range) { +/* +* Initialize list no matter what in case a mmu_notifier register after +* a range_start but before matching range_end. +*/ +INIT_LIST_HEAD (&range->list);
I don't see how can an mmu_notifier register after a range_start but before a matching range_end. The mmu_notifier registration locks all mm locks, and that should prevent any invalidation from running, right?
Quoted 4 lines
include/linux/mmu_notifier.h +1
-1if (mm_has_notifiers (mm)) -__mmu_notifier_invalidate_range_start (mm, start, end, event); +__mmu_notifier_invalidate_range_start (mm, range); }
...
Quoted 18 lines
void __mmu_notifier_invalidate_range_start(struct
mm_struct*mm,mm/mmu_notifier.c +3
-5-unsigned long start, - unsigned long end, - enum mmu_event event) + struct mmu_notifier_range *range) {struct mmu_notifier *mn;@@ -185,21 +183,36 @@ 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, event); + mn->ops->invalidate_range_start (mn, mm, range); }srcu_read_unlock (&srcu, id); +
mm/mmu_notifier.c void __mmu_notifier_invalidate_range_start(struct mm_struct *mm, · line 199
Quoted 10 lines
mm/mmu_notifier.c +8
-0+/* +* This must happen after the callback so that subsystem can block on +* new invalidation range to synchronize itself. +*/ +spin_lock (&mm->mmu_notifier_mm->lock); +list_add_tail (&range->list, &mm->mmu_notifier_mm->ranges); + mm->mmu_notifier_mm->nranges++; +spin_unlock (&mm->mmu_notifier_mm->lock); }EXPORT_SYMBOL_GPL (__mmu_notifier_invalidate_range_start);
Don't you have a race here because you add the range struct after the callback?
-------------------------------------------------------------------------
Thread A | Thread B
-------------------------------------------------------------------------
call mmu notifier callback |
clear SPTE |
| device page fault
| mmu_notifier_range_is_valid returns true
| install new SPTE
add event struct to list |
mm clears/modifies the PTE |-------------------------------------------------------------------------
So we are left with different entries in the host page table and the secondary page table.
I would think you'd want the event struct to be added to the list before the callback is run.
Best regards,
Haggai
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 2/7] mmu_notifier: keep track of active invalidation ranges v2
Quoted 16 lines · Haggai Eran
include/linux/mmu_notifier.h static inline void mmu_notifier_invalidate_page(struct mm_struct *mm, · line 350
Quoted 11 lines · On 22/12/2014 18:48, [email protected] wrote:
static inline void mmu_notifier_invalidate_range_start(struct
mm_struct*mm,include/linux/mmu_notifier.h +6
-3-unsigned long start, - unsigned long end, - enum mmu_event event) + struct mmu_notifier_range *range) { +/* +* Initialize list no matter what in case a mmu_notifier register after +* a range_start but before matching range_end. +*/ +INIT_LIST_HEAD (&range->list);I don't see how can an
mmu_notifierregister after arange_startbut before a matchingrange_end. Themmu_notifierregistration locks all mm locks, and that should prevent any invalidation from running, right?
File invalidation (like truncation) can lead to this case.
Quoted 50 lines
Quoted 5 lines
include/linux/mmu_notifier.h +1
-1if (mm_has_notifiers (mm)) -__mmu_notifier_invalidate_range_start (mm, start, end, event); +__mmu_notifier_invalidate_range_start (mm, range); }...
Quoted 18 lines
void __mmu_notifier_invalidate_range_start(struct
mm_struct*mm,mm/mmu_notifier.c +3
-5-unsigned long start, - unsigned long end, - enum mmu_event event) + struct mmu_notifier_range *range) {struct mmu_notifier *mn;@@ -185,21 +183,36 @@ 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, event); + mn->ops->invalidate_range_start (mn, mm, range); }srcu_read_unlock (&srcu, id); +mm/mmu_notifier.c void __mmu_notifier_invalidate_range_start(struct mm_struct *mm, · line 199
Quoted 10 lines
mm/mmu_notifier.c +8
-0+/* +* This must happen after the callback so that subsystem can block on +* new invalidation range to synchronize itself. +*/ +spin_lock (&mm->mmu_notifier_mm->lock); +list_add_tail (&range->list, &mm->mmu_notifier_mm->ranges); + mm->mmu_notifier_mm->nranges++; +spin_unlock (&mm->mmu_notifier_mm->lock); }EXPORT_SYMBOL_GPL (__mmu_notifier_invalidate_range_start);Don't you have a race here because you add the range struct after the callback?
-------------------------------------------------------------------------
Thread A | Thread B
-------------------------------------------------------------------------call mmu notifier callback | clear SPTE | | device page fault | mmu_notifier_range_is_valid returns true | install new SPTE add event struct to list | mm clears/modifies the PTE |
Quoted 8 lines
-------------------------------------------------------------------------
So we are left with different entries in the host page table and the secondary page table.
I would think you'd want the event struct to be added to the list before the callback is run.
Yes you right, but the comment i left trigger memory that i did that on purpose a one point probably with a different synch mecanism inside hmm. I will try to medidate a bit see if i can bring back memory why i did it that way in respect to previous design.
In all case i will respin with that order modified. Can i add you review by after doing so ?
Cheers,
Jerome
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 3/7 HMM: introduce heterogeneous memory management.
In reply to [email protected]From: JA(C)rA'me Glisse <[email protected]>
This patch only introduce core HMM functions for registering a new mirror and stopping a mirror as well as registering and unregistering a device.
The lifecycle of HMM object is handled differently then one of mmu_notifier because unlike mmu_notifier there can be concurrent call from both mm code to HMM code and/or from device driver code to HMM code. Moreover lifetime of HMM can be uncorrelated from the lifetime of the process that is being mirror.
--- MAINTAINERS | 7 + include/linux/hmm.h | 129 ++++++++++++++++ include/linux/mm.h | 11 ++ include/linux/mm_types.h | 14 ++ kernel/fork.c | 2 + mm/Kconfig | 15 ++ mm/Makefile | 1 + mm/hmm.c | 374 +++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 553 insertions(+) create mode 100644 include/linux/hmm.h create mode 100644 mm/hmm.c
MAINTAINERS +7 -0
@@ -4533,6 +4533,13 @@ F: include/uapi/linux/if_hippi.h F: net/802/hippi.c F: drivers/net/hippi/ +HMM - Heterogeneous Memory Management +M: JA(C)rA'me Glisse <[email protected]> +L: [email protected] +S: Maintained +F: mm/hmm.c +F: include/linux/hmm.h + HOST AP DRIVER M: Jouni Malinen <[email protected]> L: [email protected] (subscribers-only)
include/linux/hmm.h new file +129 -0
129 lines not on this page · raw message
include/linux/mm.h +11 -0
@@ -2208,5 +2208,16 @@ void __init setup_nr_node_ids(void);static inline void setup_nr_node_ids (void ) {}#endif +#ifdef CONFIG_HMM +static inline void hmm_mm_init (struct mm_struct *mm) +{ + mm->hmm =NULL ; +} +#else /* !CONFIG_HMM */ +static inline void hmm_mm_init (struct mm_struct *mm) +{ +} +#endif /* !CONFIG_HMM */ +#endif /* __KERNEL__ */ #endif /* _LINUX_MM_H */
include/linux/mm_types.h +14 -0
@@ -15,6 +15,10 @@#include <asm/page.h> #include <asm/mmu.h> +#ifdef CONFIG_HMM +struct hmm ; +#endif +#ifndef AT_VECTOR_SIZE_ARCH #define AT_VECTOR_SIZE_ARCH 0#endif
@@ -426,6 +430,16 @@ struct mm_struct {#ifdef CONFIG_MMU_NOTIFIER struct mmu_notifier_mm *mmu_notifier_mm;#endif +#ifdef CONFIG_HMM +/* +* hmm always register an mmu_notifier we rely on mmu notifier to keep +* refcount on mm struct as well as forbiding registering hmm on a +* dying mm +* +* This field is set with mmap_sem old in write mode. +*/ +struct hmm *hmm; +#endif #if defined(CONFIG_TRANSPARENT_HUGEPAGE ) && !USE_SPLIT_PMD_PTLOCKS pgtable_t pmd_huge_pte;/* protected by page_table_lock */ #endif
kernel/fork.c +2 -0
@@ -27,6 +27,7 @@#include <linux/binfmts.h> #include <linux/mman.h> #include <linux/mmu_notifier.h> +#include <linux/hmm.h> #include <linux/fs.h> #include <linux/mm.h> #include <linux/vmacache.h>
@@ -568,6 +569,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p)mm_init_aio (mm);mm_init_owner (mm, p);mmu_notifier_mm_init (mm); +hmm_mm_init (mm);clear_tlb_flush_pending (mm);#if defined(CONFIG_TRANSPARENT_HUGEPAGE ) && !USE_SPLIT_PMD_PTLOCKS mm->pmd_huge_pte =NULL ;
mm/Kconfig +15 -0
@@ -618,3 +618,18 @@ config MAX_STACK_SIZE_MB changed to a smaller value in which case that is used. A sane initial value is 80 MB. + +if STAGING +config HMM + bool "Enable heterogeneous memory management (HMM)" + depends on MMU + select MMU_NOTIFIER + select GENERIC_PAGE_TABLE + default n + help + Heterogeneous memory management provide infrastructure for a device + to mirror a process address space into an hardware mmu or into any + things supporting pagefault like event. + + If unsure, say N to disable hmm. +endif # STAGING
mm/Makefile +1 -0
@@ -73,3 +73,4 @@ obj-$(CONFIG_GENERIC_EARLY_IOREMAP) += early_ioremap.o obj-$(CONFIG_CMA) += cma.o obj-$(CONFIG_MEMORY_BALLOON) += balloon_compaction.o obj-$(CONFIG_PAGE_EXTENSION) += page_ext.o +obj-$(CONFIG_HMM) += hmm.o
mm/hmm.c new file +374 -0
374 lines not on this page · raw message
Signature · 7 lines
1.9.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 3/7] HMM: introduce heterogeneous memory management.
Hi,
Quoted 9 lines · On 22/12/2014 18:48, [email protected] wrote:
+/*
hmm_device_register()- register a device with HMM.
+ *
+ * @device: Thehmm_devicestruct.
+ * Returns: 0 on success or-EINVALotherwise.
+ *
+ *
+ * Call when device driver want to register itself with HMM. Device driver can
+ * only register once. It will return a reference on the device thus to release
+ * a device the driver must unreference the device.
I see that the code doesn't actually have a reference count on the hmm_device, but just registers and unregisters it through the hmm_device_register/hmm_device_unregister functions. Perhaps you should update the comment here to tell that.
Quoted 5 lines
+ */
mm/hmm.c new file +4
-0+int hmm_device_register (struct hmm_device *device) +{ +/* sanity check */ +BUG_ON (!device);
mm/hmm.c line 346
Quoted 10 lines
mm/hmm.c new file +10
-0+BUG_ON (!device->name); +BUG_ON (!device->ops); +BUG_ON (!device->ops->release); + +mutex_init (&device->mutex); +INIT_LIST_HEAD (&device->mirrors); + +return 0 ; +} +EXPORT_SYMBOL (hmm_device_register);
Regards,
Haggai
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 4/7 HMM: add HMM page table.
In reply to [email protected]From: JA(C)rA'me Glisse <[email protected]>
Heterogeneous memory management main purpose is to mirror a process address. To do so it must maintain a secondary page table that is use by the device driver to program the device or build a device specific page table.
Radix tree can not be use to create this secondary page table because HMM needs more flags than RADIX_TREE_MAX_TAGS (while this can be increase we believe HMM will require so much flags that cost will becomes prohibitive to others users of radix tree).
Moreover radix tree is built around long but for HMM we need to store dma address and on some platform sizeof(dma_addr_t) > sizeof(long). Thus radix tree is unsuitable to fulfill HMM requirement hence why we introduce this code which allows to create page table that can grow and shrink dynamicly.
The design is very clause to CPU page table as it reuse some of the feature such as spinlock embedded in struct page.
--- MAINTAINERS | 2 + include/linux/hmm_pt.h | 261 ++++++++++++++++++++++++++++++ mm/Makefile | 2 +- mm/hmm_pt.c | 425 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 689 insertions(+), 1 deletion(-) create mode 100644 include/linux/hmm_pt.h create mode 100644 mm/hmm_pt.c
MAINTAINERS +2 -0
@@ -4539,6 +4539,8 @@ L: [email protected] S: Maintained F: mm/hmm.c F: include/linux/hmm.h +F: mm/hmm_pt.c +F: include/linux/hmm_pt.h HOST AP DRIVER M: Jouni Malinen <[email protected]>
include/linux/hmm_pt.h new file +261 -0
261 lines not on this page · raw message
mm/Makefile +1 -1
@@ -73,4 +73,4 @@ obj-$(CONFIG_GENERIC_EARLY_IOREMAP) += early_ioremap.o obj-$(CONFIG_CMA) += cma.o obj-$(CONFIG_MEMORY_BALLOON) += balloon_compaction.o obj-$(CONFIG_PAGE_EXTENSION) += page_ext.o -obj-$(CONFIG_HMM) += hmm.o +obj-$(CONFIG_HMM) += hmm.o hmm_pt.o
mm/hmm_pt.c new file +425 -0
425 lines not on this page · raw message
Signature · 7 lines
1.9.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 5/7 HMM: add per mirror page table.
In reply to [email protected]From: JA(C)rA'me Glisse <[email protected]>
This patch add the per mirror page table. It also propagate CPU page table update to this per mirror page table using mmu_notifier callback. All update are contextualized with an HMM event structure that convey all information needed by device driver to take proper actions (update its own mmu to reflect changes and schedule proper flushing).
Core HMM is responsible for updating the per mirror page table once the device driver is done with its update. Most importantly HMM will properly propagate HMM page table dirty bit to underlying page.
--- include/linux/hmm.h | 136 +++++++++++++++++++++++++++ mm/hmm.c | 263 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 399 insertions(+)
include/linux/hmm.h +136 -0
@@ -46,12 +46,65 @@#include <linux/mmu_notifier.h> #include <linux/workqueue.h> #include <linux/mman.h> +#include <linux/hmm_pt.h> struct hmm_device ;struct hmm_mirror ; +struct hmm_fence ;struct hmm ; +/* hmm_fence - Device driver fence allowing to batch update and delay wait. +* +* @mirror: The HMM mirror this fence is associated with. +* @list: List of fence. +* +* Each time HMM callback into a device driver for update the device driver can +* return fence which core HMM will wait on. This allow HMM to batch update to +* several different device driver and then wait for each of them to complete. +* +* The hmm_fence structure is intended to be embedded inside a device driver +* specific fence structure. +*/ +struct hmm_fence { +struct hmm_mirror *mirror; +struct list_head list; +}; + + +/* +* hmm_event - each event is described by a type associated with a struct. +*/ +enum hmm_etype { +HMM_NONE =0 , +HMM_ISDIRTY , +HMM_MIGRATE , +HMM_MUNMAP , +HMM_DEVICE_RFAULT , +HMM_DEVICE_WFAULT , +HMM_WRITE_PROTECT , +}; + +/* struct hmm_event - memory event information. +* +* @list: So HMM can keep track of all active events. +* @start: First address (inclusive). +* @end: Last address (exclusive). +* @fences: List of device fences associated with this event. +* @pte_mask: HMM pte update mask (bit(s) that are still valid). +* @etype: Event type (munmap, migrate, truncate, ...). +* @backoff: Only meaningful for device page fault. +*/ +struct hmm_event { +struct list_head list; +unsigned long start; +unsigned long end; +struct list_head fences; +dma_addr_t pte_mask; +enum hmm_etype etype; +bool backoff; +}; +/* hmm_device - Each device must register one and only one hmm_device. *
@@ -72,6 +125,87 @@ struct hmm_device_ops {* from the mirror page table. */ void (*release)(struct hmm_mirror *mirror); + +/* fence_wait() - to wait on device driver fence. +* +* @fence: The device driver fence struct. +* Returns: 0 on success,-EIO on error, -EAGAIN to wait again. +* +* Called when hmm want to wait for all operations associated with a +* fence to complete (including device cache flush if the event mandate +* it). +* +* Device driver must free fence and associated resources if it returns +* something else thant -EAGAIN. On -EAGAIN the fence must not be free +* as hmm will call back again. +* +* Return error if scheduled operation failed or if need to wait again. +* -EIO Some input/output error with the device. +* -EAGAIN The fence not yet signaled, hmm reschedule waiting thread. +* +* All other return value trigger warning and are transformed to -EIO. +*/ +int (*fence_wait)(struct hmm_fence *fence); + +/* fence_ref() - take a reference fence structure. +* +* @fence: Fence structure hmm is referencing. +*/ +void (*fence_ref)(struct hmm_fence *fence); + +/* fence_unref() - drop a reference fence structure. +* +* @fence: Fence structure hmm is dereferencing. +*/ +void (*fence_unref)(struct hmm_fence *fence); + +/* update() - update device mmu following an event. +* +* @mirror: The mirror that link process address space with the device. +* @event: The event that triggered the update. +* Returns: Valid fence ptr or NULL on success otherwise ERR_PTR. +* +* Called to update device page table for a range of address. +* The event type provide the nature of the update : +* - Range is no longer valid (munmap). +* - Range protection changes (mprotect, COW, ...). +* - Range is unmapped (swap, reclaim, page migration, ...). +* - Device page fault. +* - ... +* +* Thought most device driver only need to use pte_mask as it reflects +* change that will happen to the HMM page table ie : +* new_pte = old_pte & event->pte_mask; +* +* Device driver must not update the HMM mirror page table. Core HMM +* will update HMM page table after the update is done (ie if a fence +* is returned after ->fence_wait() report fence is done). +* +* Any event that block further write to the memory must also trigger a +* device cache flush and everything has to be flush to local memory by +* the time the wait callback return (if this callback returned a fence +* otherwise everything must be flush by the time the callback return). +* +* Device must properly set the dirty bit using hmm_pte_mk_dirty helper +* on each HMM page table entry. +* +* The driver should return a fence pointer or NULL on success. Device +* driver should return fence and delay wait for the operation to the +* fence wait callback. Returning a fence allow hmm to batch update to +* several devices and delay wait on those once they all have scheduled +* the update. +* +* Device driver must not fail lightly, any failure result in device +* process being kill. +* +* Return fence or NULL on success, error value otherwise : +* -ENOMEM Not enough memory for performing the operation. +* -EIO Some input/output error with the device. +* +* All other return value trigger warning and are transformed to -EIO. +*/ +struct hmm_fence *(*update)(struct hmm_mirror *mirror, +const struct hmm_event *event); };/* struct hmm_device - per device HMM structure
@@ -108,6 +242,7 @@ int hmm_device_unregister(struct hmm_device *device);* @hmm: The hmm struct this hmm_mirror is associated to. * @dlist: List of all hmm_mirror for same device. * @mlist: List of all hmm_mirror for same process. +* @pt: Mirror page table. * * Each device that want to mirror an address space must register one of this * struct for each of the address space it wants to mirror. Same device can
@@ -119,6 +254,7 @@ struct hmm_mirror {struct hmm *hmm;struct list_head dlist;struct hlist_node mlist; +struct hmm_pt pt; };int hmm_mirror_register (struct hmm_mirror *mirror,struct hmm_device *device);
mm/hmm.c +263 -0
319 lines not on this page · raw message
Signature · 7 lines
1.9.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 6/7 HMM: add device page fault support.
In reply to [email protected]From: JA(C)rA'me Glisse <[email protected]>
This patch add helper for device page fault. Device page fault helper will fill the mirror page table using the CPU page table all this synchronized with any update to CPU page table.
--- include/linux/hmm.h | 1 + mm/hmm.c | 384 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 385 insertions(+)
include/linux/hmm.h +1 -0
@@ -259,6 +259,7 @@ struct hmm_mirror {int hmm_mirror_register (struct hmm_mirror *mirror,struct hmm_device *device);void hmm_mirror_unregister (struct hmm_mirror *mirror); +int hmm_mirror_fault (struct hmm_mirror *mirror,struct hmm_event *event);#endif /* CONFIG_HMM */
mm/hmm.c +384 -0
438 lines not on this page · raw message
Signature · 7 lines
1.9.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>