HMM updates, improvements and fixes v2
PATCH 2/6 mm/rmap: map_pte() was not handling private ZONE_DEVICE page properly v3
In reply to [email protected]From: Ralph Campbell <[email protected]>
Private ZONE_DEVICE pages use a special pte entry and thus are not present. Properly handle this case in map_pte(), it is already handled in check_pte(), the map_pte() part was lost in some rebase most probably.
Without this patch the slow migration path can not migrate back to any private ZONE_DEVICE memory to regular memory. This was found after stress testing migration back to system memory. This ultimatly can lead to the CPU constantly page fault looping on the special swap entry.
Changes since v2:
- add comments explaining what is going on
Changes since v1:
- properly lock pte directory in map_pte()
--- mm/page_vma_mapped.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-)
mm/page_vma_mapped.c +23 -1
@@ -21,7 +21,29 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw)if (!is_swap_pte (*pvmw->pte))return false; }else { -if (!pte_present (*pvmw->pte)) +/* +* We get here when we are trying to unmap a private +* device page from the process address space. Such +* page is not CPU accessible and thus is mapped as +* a special swap entry, nonetheless it still does +* count as a valid regular mapping for the page (and +* is accounted as such in page maps count). +* +* So handle this special case as if it was a normal +* page mapping ie lock CPU page table and returns +* true. +* +* For more details on device private memory see HMM +* (include/linux/hmm.h or mm/hmm.c). +*/ +if (is_swap_pte (*pvmw->pte)) { +swp_entry_t entry; + +/* Handle un-addressable ZONE_DEVICE memory */ + entry =pte_to_swp_entry (*pvmw->pte); +if (!is_device_private_entry (entry)) +return false; + }else if (!pte_present (*pvmw->pte))return false; } }
Signature · 1 line
2.17.2
PATCH 3/6 mm/hmm: fix race between hmm_mirror_unregister() and mmu_notifier callback
In reply to [email protected]From: Ralph Campbell <[email protected]>
In hmm_mirror_unregister(), mm->hmm is set to NULL and then mmu_notifier_unregister_no_release() is called. That creates a small window where mmu_notifier can call mmu_notifier_ops with mm->hmm equal to NULL. Fix this by first unregistering mmu notifier callbacks and then setting mm->hmm to NULL.
Similarly in hmm_register(), set mm->hmm before registering mmu_notifier callbacks so callback functions always see mm->hmm set.
--- mm/hmm.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-)
mm/hmm.c +21 -15
@@ -91,16 +91,6 @@ static struct hmm *hmm_register(struct mm_struct *mm)spin_lock_init (&hmm->lock); hmm->mm = mm; -/* -* We should only get here if hold the mmap_sem in write mode ie on -* registration of first mirror through hmm_mirror_register() -*/ - hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops; -if (__mmu_notifier_register (&hmm->mmu_notifier, mm)) { -kfree (hmm); -return NULL ; - } -spin_lock (&mm->page_table_lock);if (!mm->hmm) mm->hmm = hmm;
@@ -108,12 +98,27 @@ static struct hmm *hmm_register(struct mm_struct *mm) cleanup = true;spin_unlock (&mm->page_table_lock); -if (cleanup) { -mmu_notifier_unregister (&hmm->mmu_notifier, mm); -kfree (hmm); - } +if (cleanup) + goto error; + +/* +* We should only get here if hold the mmap_sem in write mode ie on +* registration of first mirror through hmm_mirror_register() +*/ + hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops; +if (__mmu_notifier_register (&hmm->mmu_notifier, mm)) + goto error_mm;return mm->hmm; + +error_mm: +spin_lock (&mm->page_table_lock); +if (mm->hmm == hmm) + mm->hmm =NULL ; +spin_unlock (&mm->page_table_lock); +error: +kfree (hmm); +return NULL ; }void hmm_mm_destroy (struct mm_struct *mm)
@@ -278,12 +283,13 @@ void hmm_mirror_unregister(struct hmm_mirror *mirror)if (!should_unregister || mm ==NULL )return ; +mmu_notifier_unregister_no_release (&hmm->mmu_notifier, mm); +spin_lock (&mm->page_table_lock);if (mm->hmm == hmm) mm->hmm =NULL ;spin_unlock (&mm->page_table_lock); -mmu_notifier_unregister_no_release (&hmm->mmu_notifier, mm);kfree (hmm); }EXPORT_SYMBOL (hmm_mirror_unregister);
Signature · 1 line
2.17.2
cover HMM updates, improvements and fixes v2
From: JA(C)rA'me Glisse <[email protected]>
[Andrew this is for 4.20, stable fixes as cc to stable]
Few fixes that only affect HMM users. Improve the synchronization call back so that we match was other mmu_notifier listener do and add proper support to the new blockable flags in the process.
For curious folks here are branches to leverage HMM in various existing device drivers:
https://cgit.freedesktop.org/~glisse/linux/log/?h=hmm-nouveau-v01
https://cgit.freedesktop.org/~glisse/linux/log/?h=hmm-radeon-v00
https://cgit.freedesktop.org/~glisse/linux/log/?h=hmm-intel-v00
More to come (amd gpu, Mellanox, ...)
I expect more of the preparatory work for nouveau will be merge in 4.20 (like we have been doing since 4.16) and i will wait until this patchset is upstream before pushing the patches that actualy make use of HMM (to avoid complex tree inter-dependency).
JA(C)rA'me Glisse (4):
mm/hmm: fix utf8 ...
mm/hmm: properly handle migration pmd v3
mm/hmm: use a structure for update callback parameters v2 mm/hmm: invalidate device page table at start of invalidation
Ralph Campbell (2):
mm/rmap: map_pte() was not handling private ZONE_DEVICE page properly
v3
mm/hmm: fix race between hmm_mirror_unregister() and mmu_notifier
callback
include/linux/hmm.h | 33 +++++++---- mm/hmm.c | 134 +++++++++++++++++++++++++++++-------------- mm/page_vma_mapped.c | 24 +++++++- 3 files changed, 137 insertions(+), 54 deletions(-)
Signature · 1 line
2.17.2
PATCH 1/6 mm/hmm: fix utf8 ...
From: JA(C)rA'me Glisse <[email protected]>
Somehow utf=8 must have been broken.
--- include/linux/hmm.h | 2 +- mm/hmm.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
include/linux/hmm.h +1 -1
@@ -11,7 +11,7 @@* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * -* Authors: JA?A(C)rA?A'me Glisse <[email protected]> +* Authors: JA(C)rA'me Glisse <[email protected]> */ /* * Heterogeneous Memory Management (HMM)
mm/hmm.c +1 -1
@@ -11,7 +11,7 @@* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * -* Authors: JA?A(C)rA?A'me Glisse <[email protected]> +* Authors: JA(C)rA'me Glisse <[email protected]> */ /* * Refer to include/linux/hmm.h for information about heterogeneous memory
Signature · 1 line
2.17.2
PATCH 4/6 mm/hmm: properly handle migration pmd v3
In reply to [email protected]From: JA(C)rA'me Glisse <[email protected]>
Before this patch migration pmd entry (!pmd_present()) would have been treated as a bad entry (pmd_bad() returns true on migration pmd entry). The outcome was that device driver would believe that the range covered by the pmd was bad and would either SIGBUS or simply kill all the device's threads (each device driver decide how to react when the device tries to access poisonnous or invalid range of memory).
This patch explicitly handle the case of migration pmd entry which are non present pmd entry and either wait for the migration to finish or report empty range (when device is just trying to pre- fill a range of virtual address and thus do not want to wait or trigger page fault).
Changed since v1:
- use is_pmd_migration_entry() instead of open coding the equivalent.
Changed since v2:
- protect is_pmd_migration_entry() with thp_migration_supported()
--- mm/hmm.c | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-)
mm/hmm.c +34 -6
@@ -577,22 +577,42 @@ static int hmm_vma_walk_pmd(pmd_t *pmdp, {struct hmm_vma_walk *hmm_vma_walk = walk->private;struct hmm_range *range = hmm_vma_walk->range; +struct vm_area_struct *vma = walk->vma;uint64_t *pfns = range->pfns;unsigned long addr = start, i;pte_t *ptep; +pmd_t pmd; - i = (addr - range->start) >>PAGE_SHIFT ; again: -if (pmd_none (*pmdp)) + pmd =READ_ONCE (*pmdp); +if (pmd_none (pmd))return hmm_vma_walk_hole (start, end, walk); -if (pmd_huge (*pmdp) && (range->vma->vm_flags &VM_HUGETLB )) +if (pmd_huge (pmd) && (range->vma->vm_flags &VM_HUGETLB ))return hmm_pfns_bad (start, end, walk); -if (pmd_devmap (*pmdp) ||pmd_trans_huge (*pmdp)) { -pmd_t pmd; +if (thp_migration_supported () &&is_pmd_migration_entry (pmd)) { +bool fault, write_fault; +unsigned long npages; +uint64_t *pfns; + + i = (addr - range->start) >>PAGE_SHIFT ; + npages = (end - addr) >>PAGE_SHIFT ; + pfns = &range->pfns[i]; + +hmm_range_need_fault (hmm_vma_walk, pfns, npages, +0 , &fault, &write_fault); +if (fault || write_fault) { + hmm_vma_walk->last = addr; +pmd_migration_entry_wait (vma->vm_mm, pmdp); +return -EAGAIN ; + } +return 0 ; + }else if (!pmd_present (pmd)) +return hmm_pfns_bad (start, end, walk); +if (pmd_devmap (pmd) ||pmd_trans_huge (pmd)) {/* * No need to take pmd_lock here, even if some other threads * is splitting the huge pmd we will get that event through
@@ -607,13 +627,21 @@ static int hmm_vma_walk_pmd(pmd_t *pmdp,if (!pmd_devmap (pmd) && !pmd_trans_huge (pmd)) goto again; + i = (addr - range->start) >>PAGE_SHIFT ;return hmm_vma_handle_pmd (walk, addr, end, &pfns[i], pmd); } -if (pmd_bad (*pmdp)) +/* +* We have handled all the valid case above ie either none, migration, +* huge or transparent huge. At this point either it is a valid pmd +* entry pointing to pte directory or it is a bad pmd that will not +* recover. +*/ +if (pmd_bad (pmd))return hmm_pfns_bad (start, end, walk); ptep =pte_offset_map (pmdp, addr); + i = (addr - range->start) >>PAGE_SHIFT ;for (; addr < end; addr +=PAGE_SIZE , ptep++, i++) {int r;
Signature · 1 line
2.17.2
PATCH 5/6 mm/hmm: use a structure for update callback parameters v2
In reply to [email protected]From: JA(C)rA'me Glisse <[email protected]>
Use a structure to gather all the parameters for the update callback. This make it easier when adding new parameters by avoiding having to update all callback function signature.
The hmm_update structure is always associated with a mmu_notifier callbacks so we are not planing on grouping multiple updates together. Nor do we care about page size for the range as range will over fully cover the page being invalidated (this is a mmu_notifier property).
Changed since v1:
- support for blockable mmu_notifier flags
- improved commit log
--- include/linux/hmm.h | 31 ++++++++++++++++++++++--------- mm/hmm.c | 33 ++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 20 deletions(-)
include/linux/hmm.h +22 -9
@@ -274,13 +274,28 @@ static inline uint64_t hmm_pfn_from_pfn(const struct hmm_range *range,struct hmm_mirror ;/* -* enum hmm_update_type - type of update +* enum hmm_update_event - type of update * @HMM_UPDATE_INVALIDATE: invalidate range (no indication as to why) */ -enum hmm_update_type { +enum hmm_update_event {HMM_UPDATE_INVALIDATE , }; +/* +* struct hmm_update - HMM update informations for callback +* +* @start: virtual start address of the range to update +* @end: virtual end address of the range to update +* @event: event triggering the update (what is happening) +* @blockable: can the callback block/sleep ? +*/ +struct hmm_update { +unsigned long start; +unsigned long end; +enum hmm_update_event event; +bool blockable; +}; +/* * struct hmm_mirror_ops - HMM mirror device operations callback *
@@ -300,9 +315,9 @@ struct hmm_mirror_ops {/* sync_cpu_device_pagetables() - synchronize page tables * * @mirror: pointer to struct hmm_mirror -* @update_type: type of update that occurred to the CPU page table -* @start: virtual start address of the range to update -* @end: virtual end address of the range to update +* @update: update informations (see struct hmm_update) +* Returns: -EAGAIN if update.blockable false and callback need to +* block, 0 otherwise. * * This callback ultimately originates from mmu_notifiers when the CPU * page table is updated. The device driver must update its page table
@@ -313,10 +328,8 @@ struct hmm_mirror_ops {* page tables are completely updated (TLBs flushed, etc); this is a * synchronous call. */ -void (*sync_cpu_device_pagetables)(struct hmm_mirror *mirror, -enum hmm_update_type update_type, -unsigned long start, -unsigned long end); +int (*sync_cpu_device_pagetables)(struct hmm_mirror *mirror, +const struct hmm_update *update); };/*
mm/hmm.c +22 -11
@@ -126,10 +126,8 @@ void hmm_mm_destroy(struct mm_struct *mm)kfree (mm->hmm); } -static void hmm_invalidate_range (struct hmm *hmm, -enum hmm_update_type action, -unsigned long start, -unsigned long end) +static int hmm_invalidate_range (struct hmm *hmm, +const struct hmm_update *update) {struct hmm_mirror *mirror;struct hmm_range *range;
@@ -138,22 +136,30 @@ static void hmm_invalidate_range(struct hmm *hmm,list_for_each_entry (range, &hmm->ranges, list) {unsigned long addr, idx, npages; -if (end < range->start || start >= range->end) +if (update->end < range->start || update->start >= range->end)continue ; range->valid = false; - addr =max (start, range->start); + addr =max (update->start, range->start); idx = (addr - range->start) >>PAGE_SHIFT ; - npages = (min (range->end, end) - addr) >>PAGE_SHIFT ; + npages = (min (range->end, update->end) - addr) >>PAGE_SHIFT ;memset (&range->pfns[idx],0 ,sizeof (*range->pfns) * npages); }spin_unlock (&hmm->lock);down_read (&hmm->mirrors_sem); -list_for_each_entry (mirror, &hmm->mirrors, list) - mirror->ops->sync_cpu_device_pagetables (mirror, action, - start, end); +list_for_each_entry (mirror, &hmm->mirrors, list) { +int ret; + + ret = mirror->ops->sync_cpu_device_pagetables (mirror, update); +if (!update->blockable && ret == -EAGAIN ) { +up_read (&hmm->mirrors_sem); +return -EAGAIN ; + } + }up_read (&hmm->mirrors_sem); + +return 0 ; }static void hmm_release (struct mmu_notifier *mn,struct mm_struct *mm)
@@ -202,11 +208,16 @@ static void hmm_invalidate_range_end(struct mmu_notifier *mn, unsigned long start, unsigned long end) { +struct hmm_update update;struct hmm *hmm = mm->hmm;VM_BUG_ON (!hmm); -hmm_invalidate_range (mm->hmm,HMM_UPDATE_INVALIDATE , start, end); + update.start = start; + update.end = end; + update.event =HMM_UPDATE_INVALIDATE ; + update.blockable = true; +hmm_invalidate_range (hmm, &update); }static const struct mmu_notifier_ops hmm_mmu_notifier_ops = {
Signature · 1 line
2.17.2
PATCH 6/6 mm/hmm: invalidate device page table at start of invalidation
In reply to [email protected]From: JA(C)rA'me Glisse <[email protected]>
Invalidate device page table at start of invalidation and invalidate in progress CPU page table snapshooting at both start and end of any invalidation.
This is helpful when device need to dirty page because the device page table report the page as dirty. Dirtying page must happen in the start mmu notifier callback and not in the end one.
--- mm/hmm.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-)
mm/hmm.c +15 -12
@@ -43,7 +43,6 @@ static const struct mmu_notifier_ops hmm_mmu_notifier_ops;* * @mm: mm struct this HMM struct is bound to * @lock: lock protecting ranges list -* @sequence: we track updates to the CPU page table with a sequence number * @ranges: list of range being snapshotted * @mirrors: list of mirrors for this mm * @mmu_notifier: mmu notifier to track updates to CPU page table
@@ -52,7 +51,6 @@ static const struct mmu_notifier_ops hmm_mmu_notifier_ops;struct hmm {struct mm_struct *mm;spinlock_t lock; -atomic_t sequence;struct list_head ranges;struct list_head mirrors;struct mmu_notifier mmu_notifier;
@@ -85,7 +83,6 @@ static struct hmm *hmm_register(struct mm_struct *mm)return NULL ;INIT_LIST_HEAD (&hmm->mirrors);init_rwsem (&hmm->mirrors_sem); -atomic_set (&hmm->sequence,0 ); hmm->mmu_notifier.ops =NULL ;INIT_LIST_HEAD (&hmm->ranges);spin_lock_init (&hmm->lock);
@@ -126,7 +123,7 @@ void hmm_mm_destroy(struct mm_struct *mm)kfree (mm->hmm); } -static int hmm_invalidate_range (struct hmm *hmm, +static int hmm_invalidate_range (struct hmm *hmm,bool device,const struct hmm_update *update) {struct hmm_mirror *mirror;
@@ -147,6 +144,9 @@ static int hmm_invalidate_range(struct hmm *hmm, }spin_unlock (&hmm->lock); +if (!device) +return 0 ; +down_read (&hmm->mirrors_sem);list_for_each_entry (mirror, &hmm->mirrors, list) {int ret;
@@ -189,18 +189,21 @@ static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm) }static int hmm_invalidate_range_start (struct mmu_notifier *mn, -struct mm_struct *mm, -unsigned long start, -unsigned long end, -bool blockable) +struct mm_struct *mm, +unsigned long start, +unsigned long end, +bool blockable) { +struct hmm_update update;struct hmm *hmm = mm->hmm;VM_BUG_ON (!hmm); -atomic_inc (&hmm->sequence); - -return 0 ; + update.start = start; + update.end = end; + update.event =HMM_UPDATE_INVALIDATE ; + update.blockable = blockable; +return hmm_invalidate_range (hmm, true, &update); } static void hmm_invalidate_range_end(struct mmu_notifier *mn,
@@ -217,7 +220,7 @@ static void hmm_invalidate_range_end(struct mmu_notifier *mn, update.end = end; update.event =HMM_UPDATE_INVALIDATE ; update.blockable = true; -hmm_invalidate_range (hmm, &update); +hmm_invalidate_range (hmm, false, &update); }static const struct mmu_notifier_ops hmm_mmu_notifier_ops = {
Signature · 1 line
2.17.2
Andrew Morton
maintainerRe: [PATCH 3/6] mm/hmm: fix race between hmm_mirror_unregister() and mmu_notifier callback
In reply to [email protected]Quoted 11 lines · On Fri, 19 Oct 2018 12:04:39 -0400 [email protected] wrote:
From: Ralph Campbell <[email protected]>
In
hmm_mirror_unregister(),mm->hmmis set to NULL and thenmmu_notifier_unregister_no_release()is called. That creates a small window wheremmu_notifiercan callmmu_notifier_opswithmm->hmmequal to NULL. Fix this by first unregistering mmu notifier callbacks and then settingmm->hmmto NULL.Similarly in
hmm_register(), setmm->hmmbefore registeringmmu_notifiercallbacks so callback functions always seemm->hmmset.
Quoted 6 lines
Signed-off-by Ralph Campbell <[email protected]>Reviewed-by John Hubbard <[email protected]>Reviewed-by Jérôme Glisse <[email protected]>Reviewed-by Balbir Singh <[email protected]>Cc Andrew Morton <[email protected]>
I added your Signed-off-by: to this one. It's required since you were on the patch delivery path.