mm/hmm/nouveau: add THP migration to migrate_vma_*
Subsystems (14)
v2 cover mm/hmm/nouveau: add THP migration to migrate_vma_*
This series adds support for transparent huge page migration to migrate_vma_*() and adds nouveau SVM and HMM selftests as consumers. An earlier version was posted previously [1]. This version now supports splitting a THP midway in the migration process which led to a number of changes.
The patches apply cleanly to the current linux-mm tree. Since there are a couple of patches in linux-mm from Dan Williams that modify lib/test_hmm.c and drivers/gpu/drm/nouveau/nouveau_dmem.c, it might be easiest if Andrew could take these through the linux-mm tree assuming that's OK with other maintainers like Ben Skeggs.
Ralph Campbell (7):
mm/thp: fix __split_huge_pmd_locked() for migration PMD mm/migrate: move migrate_vma_collect_skip()
mm: support THP migration to device private memory mm/thp: add prep_transhuge_device_private_page() mm/thp: add THP allocation helper
mm/hmm/test: add self tests for THP migration
nouveau: support THP migration to private memory
drivers/gpu/drm/nouveau/nouveau_dmem.c | 289 +++++++++++----- drivers/gpu/drm/nouveau/nouveau_svm.c | 11 +- drivers/gpu/drm/nouveau/nouveau_svm.h | 3 +- include/linux/gfp.h | 10 + include/linux/huge_mm.h | 12 + include/linux/memremap.h | 9 + include/linux/migrate.h | 2 + lib/test_hmm.c | 439 +++++++++++++++++++++---- lib/test_hmm_uapi.h | 3 + mm/huge_memory.c | 177 +++++++--- mm/memory.c | 10 +- mm/migrate.c | 429 +++++++++++++++++++----- mm/rmap.c | 2 +- tools/testing/selftests/vm/hmm-tests.c | 404 +++++++++++++++++++++++ 14 files changed, 1519 insertions(+), 281 deletions(-)
Signature · 1 line
2.20.1
PATCH v2 1/7 mm/thp: fix __split_huge_pmd_locked() for migration PMD
A migrating transparent huge page has to already be unmapped. Otherwise, the page could be modified while it is being copied to a new page and data could be lost. The function __split_huge_pmd() checks for a PMD migration entry before calling __split_huge_pmd_locked() leading one to think that __split_huge_pmd_locked() can handle splitting a migrating PMD. However, the code always increments the page->_mapcount and adjusts the memory control group accounting assuming the page is mapped. Also, if the PMD entry is a migration PMD entry, the call to is_huge_zero_pmd(*pmd) is incorrect because it calls pmd_pfn(pmd) instead of migration_entry_to_pfn(pmd_to_swp_entry(pmd)).
Fix these problems by checking for a PMD migration entry.
--- mm/huge_memory.c | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-)
mm/huge_memory.c +23 -19
@@ -2023,7 +2023,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,put_page (page);add_mm_counter (mm,mm_counter_file (page), -HPAGE_PMD_NR );return ; - }else if (is_huge_zero_pmd (*pmd)) { + }else if (pmd_trans_huge (*pmd) &&is_huge_zero_pmd (*pmd)) {/* * FIXME: Do we want to invalidate secondary mmu by calling * mmu_notifier_invalidate_range() see comments below inside
@@ -2117,30 +2117,34 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, pte =pte_offset_map (&_pmd, addr);BUG_ON (!pte_none (*pte));set_pte_at (mm, addr, pte, entry); -atomic_inc (&page[i]._mapcount); -pte_unmap (pte); - } - -/* -* Set PG_double_map before dropping compound_mapcount to avoid -* false-negative page_mapped(). -*/ -if (compound_mapcount (page) >1 && !TestSetPageDoubleMap (page)) { -for (i =0 ; i <HPAGE_PMD_NR ; i++) +if (!pmd_migration)atomic_inc (&page[i]._mapcount); +pte_unmap (pte); } -lock_page_memcg (page); -if (atomic_add_negative (-1 ,compound_mapcount_ptr (page))) { -/* Last compound_mapcount is gone. */ -__dec_lruvec_page_state (page,NR_ANON_THPS ); -if (TestClearPageDoubleMap (page)) { -/* No need in mapcount reference anymore */ +if (!pmd_migration) { +/* +* Set PG_double_map before dropping compound_mapcount to avoid +* false-negative page_mapped(). +*/ +if (compound_mapcount (page) >1 && + !TestSetPageDoubleMap (page)) {for (i =0 ; i <HPAGE_PMD_NR ; i++) -atomic_dec (&page[i]._mapcount); +atomic_inc (&page[i]._mapcount); + } + +lock_page_memcg (page); +if (atomic_add_negative (-1 ,compound_mapcount_ptr (page))) { +/* Last compound_mapcount is gone. */ +__dec_lruvec_page_state (page,NR_ANON_THPS ); +if (TestClearPageDoubleMap (page)) { +/* No need in mapcount reference anymore */ +for (i =0 ; i <HPAGE_PMD_NR ; i++) +atomic_dec (&page[i]._mapcount); + } } +unlock_page_memcg (page); } -unlock_page_memcg (page);smp_wmb ();/* make pte visible before pmd */ pmd_populate (mm, pmd, pgtable);
Signature · 1 line
2.20.1
PATCH v2 2/7 mm/migrate: move migrate_vma_collect_skip()
In reply to Ralph CampbellMove the definition of migrate_vma_collect_skip() to make it callable by migrate_vma_collect_hole(). This helps make the next patch easier to read.
--- mm/migrate.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-)
mm/migrate.c +15 -15
@@ -2254,6 +2254,21 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm,#endif /* CONFIG_NUMA */ #ifdef CONFIG_DEVICE_PRIVATE +static int migrate_vma_collect_skip (unsigned long start, +unsigned long end, +struct mm_walk *walk) +{ +struct migrate_vma *migrate = walk->private; +unsigned long addr; + +for (addr = start; addr < end; addr +=PAGE_SIZE ) { + migrate->dst[migrate->npages] =0 ; + migrate->src[migrate->npages++] =0 ; + } + +return 0 ; +} + static int migrate_vma_collect_hole(unsigned long start, unsigned long end, __always_unused int depth,
@@ -2282,21 +2297,6 @@ static int migrate_vma_collect_hole(unsigned long start,return 0 ; } -static int migrate_vma_collect_skip (unsigned long start, -unsigned long end, -struct mm_walk *walk) -{ -struct migrate_vma *migrate = walk->private; -unsigned long addr; - -for (addr = start; addr < end; addr +=PAGE_SIZE ) { - migrate->dst[migrate->npages] =0 ; - migrate->src[migrate->npages++] =0 ; - } - -return 0 ; -} - static int migrate_vma_collect_pmd(pmd_t *pmdp, unsigned long start, unsigned long end,
Signature · 1 line
2.20.1
PATCH v2 3/7 mm: support THP migration to device private memory
In reply to Ralph CampbellSupport transparent huge page migration to ZONE_DEVICE private memory. A new selection flag (MIGRATE_VMA_SELECT_COMPOUND) is added to request THP migration. Otherwise, THPs are split when filling in the source PFN array. A new flag (MIGRATE_PFN_COMPOUND) is added to the source PFN array to indicate a huge page can be migrated. If the device driver can allocate a huge page, it sets the MIGRATE_PFN_COMPOUND flag in the destination PFN array. migrate_vma_pages() will fallback to PAGE_SIZE pages if MIGRATE_PFN_COMPOUND is not set in both source and destination arrays.
--- include/linux/huge_mm.h | 7 + include/linux/memremap.h | 9 + include/linux/migrate.h | 2 + mm/huge_memory.c | 113 ++++++++--- mm/memory.c | 10 +- mm/migrate.c | 413 ++++++++++++++++++++++++++++++++------- mm/rmap.c | 2 +- 7 files changed, 459 insertions(+), 97 deletions(-)
include/linux/huge_mm.h +7 -0
@@ -192,6 +192,8 @@ bool is_transparent_hugepage(struct page *page);bool can_split_huge_page (struct page *page,int *pextra_pins);int split_huge_page_to_list (struct page *page,struct list_head *list); +int split_migrating_huge_page (struct vm_area_struct *vma,pmd_t *pmd, +unsigned long address,struct page *page);static inline int split_huge_page (struct page *page) {return split_huge_page_to_list (page,NULL );
@@ -454,6 +456,11 @@ static inline bool is_huge_zero_page(struct page *page)return false; } +static inline bool is_huge_zero_pmd (pmd_t pmd) +{ +return false; +} +static inline bool is_huge_zero_pud (pud_t pud) {return false;
include/linux/memremap.h +9 -0
@@ -88,6 +88,15 @@ struct dev_pagemap_ops {* the page back to a CPU accessible page. */ vm_fault_t (*migrate_to_ram)(struct vm_fault *vmf); + +/* +* Used for private (un-addressable) device memory only. +* This is called when a compound device private page is split. +* The driver uses this callback to set tail_page->pgmap and +* tail_page->zone_device_data appropriately based on the head +* page. +*/ +void (*page_split)(struct page *head,struct page *tail_page); };#define PGMAP_ALTMAP_VALID (1 << 0)
include/linux/migrate.h +2 -0
@@ -144,6 +144,7 @@ static inline int migrate_misplaced_transhuge_page(struct mm_struct *mm,#define MIGRATE_PFN_MIGRATE (1UL << 1)#define MIGRATE_PFN_LOCKED (1UL << 2)#define MIGRATE_PFN_WRITE (1UL << 3) +#define MIGRATE_PFN_COMPOUND (1UL << 4)#define MIGRATE_PFN_SHIFT 6static inline struct page *migrate_pfn_to_page (unsigned long mpfn)
@@ -161,6 +162,7 @@ static inline unsigned long migrate_pfn(unsigned long pfn)enum migrate_vma_direction {MIGRATE_VMA_SELECT_SYSTEM =1 <<0 ,MIGRATE_VMA_SELECT_DEVICE_PRIVATE =1 <<1 , +MIGRATE_VMA_SELECT_COMPOUND =1 <<2 , };struct migrate_vma {
mm/huge_memory.c +91 -22
244 lines not on this page · raw message
mm/memory.c +8 -2
@@ -4346,9 +4346,15 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,barrier ();if (unlikely (is_swap_pmd (orig_pmd))) { +swp_entry_t entry =pmd_to_swp_entry (orig_pmd); + +if (is_device_private_entry (entry)) { + vmf.page =device_private_entry_to_page (entry); +return vmf.page->pgmap->ops->migrate_to_ram (&vmf); + }VM_BUG_ON (thp_migration_supported () && - !is_pmd_migration_entry (orig_pmd)); -if (is_pmd_migration_entry (orig_pmd)) + !is_migration_entry (entry)); +if (is_migration_entry (entry))pmd_migration_entry_wait (mm, vmf.pmd);return 0 ; }
mm/migrate.c +341 -72
525 lines not on this page · raw message
mm/rmap.c +1 -1
@@ -1497,7 +1497,7 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma, }if (IS_ENABLED (CONFIG_MIGRATION ) && - (flags &TTU_MIGRATION ) && + (flags & (TTU_MIGRATION |TTU_SPLIT_FREEZE )) &&is_zone_device_page (page)) {swp_entry_t entry;pte_t swp_pte;
Signature · 1 line
2.20.1
PATCH v2 4/7 mm/thp: add prep_transhuge_device_private_page()
In reply to Ralph CampbellAdd a helper function to allow device drivers to create device private transparent huge pages. This is intended to help support device private THP migrations.
--- include/linux/huge_mm.h | 5 +++++ mm/huge_memory.c | 8 ++++++++ 2 files changed, 13 insertions(+)
include/linux/huge_mm.h +5 -0
@@ -187,6 +187,7 @@ extern unsigned long thp_get_unmapped_area(struct file *filp, unsigned long flags);extern void prep_transhuge_page (struct page *page); +extern void prep_transhuge_device_private_page (struct page *page);extern void free_transhuge_page (struct page *page);bool is_transparent_hugepage (struct page *page);
@@ -382,6 +383,10 @@ static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,static inline void prep_transhuge_page (struct page *page) {} +static inline void prep_transhuge_device_private_page (struct page *page) +{ +} +static inline bool is_transparent_hugepage (struct page *page) {return false;
mm/huge_memory.c +8 -0
@@ -498,6 +498,14 @@ void prep_transhuge_page(struct page *page)set_compound_page_dtor (page,TRANSHUGE_PAGE_DTOR ); } +void prep_transhuge_device_private_page (struct page *page) +{ +prep_compound_page (page,HPAGE_PMD_ORDER ); +prep_transhuge_page (page); +percpu_ref_put_many (page->pgmap->ref,HPAGE_PMD_NR -1 ); +} +EXPORT_SYMBOL_GPL (prep_transhuge_device_private_page); +bool is_transparent_hugepage (struct page *page) {if (!PageCompound (page))
Signature · 1 line
2.20.1
PATCH v2 5/7 mm/thp: add THP allocation helper
In reply to Ralph CampbellTransparent huge page allocation policy is controlled by several sysfs variables. Rather than expose these to each device driver that needs to allocate THPs, provide a helper function.
--- include/linux/gfp.h | 10 ++++++++++ mm/huge_memory.c | 14 ++++++++++++++ 2 files changed, 24 insertions(+)
include/linux/gfp.h +10 -0
@@ -562,6 +562,16 @@ extern struct page *alloc_pages_vma(gfp_t gfp_mask, int order, alloc_pages_vma(gfp_mask, 0, vma, addr, numa_node_id(), false)#define alloc_page_vma_node (gfp_mask, vma, addr, node) \ alloc_pages_vma(gfp_mask, 0, vma, addr, node, false) +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +extern struct page *alloc_transhugepage (struct vm_area_struct *vma, +unsigned long addr); +#else +static inline struct page *alloc_transhugepage (struct vm_area_struct *vma, +unsigned long addr) +{ +return NULL ; +} +#endif extern unsigned long __get_free_pages (gfp_t gfp_mask,unsigned int order);extern unsigned long get_zeroed_page (gfp_t gfp_mask);
mm/huge_memory.c +14 -0
@@ -764,6 +764,20 @@ vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)return __do_huge_pmd_anonymous_page (vmf, page, gfp); } +struct page *alloc_transhugepage (struct vm_area_struct *vma, +unsigned long haddr) +{ +gfp_t gfp; +struct page *page; + + gfp =alloc_hugepage_direct_gfpmask (vma); + page =alloc_hugepage_vma (gfp, vma, haddr,HPAGE_PMD_ORDER ); +if (page) +prep_transhuge_page (page); +return page; +} +EXPORT_SYMBOL_GPL (alloc_transhugepage); +static void insert_pfn_pmd (struct vm_area_struct *vma,unsigned long addr,pmd_t *pmd,pfn_t pfn,pgprot_t prot,bool write,pgtable_t pgtable)
Signature · 1 line
2.20.1
PATCH v2 6/7 mm/hmm/test: add self tests for THP migration
In reply to Ralph CampbellAdd some basic stand alone self tests for migrating system memory to device private memory and back.
--- lib/test_hmm.c | 439 +++++++++++++++++++++---- lib/test_hmm_uapi.h | 3 + tools/testing/selftests/vm/hmm-tests.c | 404 +++++++++++++++++++++++ 3 files changed, 777 insertions(+), 69 deletions(-)
lib/test_hmm.c +370 -69
@@ -67,6 +67,7 @@ struct dmirror {struct xarray pt;struct mmu_interval_notifier notifier;struct mutex mutex; +__u64 flags; };/*
@@ -92,6 +93,7 @@ struct dmirror_device {unsigned long calloc;unsigned long cfree;struct page *free_pages; +struct page *free_huge_pages;spinlock_t lock;/* protects the above */ };
@@ -451,6 +453,7 @@ static int dmirror_write(struct dmirror *dmirror, struct hmm_dmirror_cmd *cmd) }static bool dmirror_allocate_chunk (struct dmirror_device *mdevice, +bool is_huge,struct page **ppage) {struct dmirror_chunk *devmem;
@@ -504,28 +507,51 @@ static bool dmirror_allocate_chunk(struct dmirror_device *mdevice,mutex_unlock (&mdevice->devmem_lock); -pr_info ("added new %u MB chunk (total %u chunks, %u MB) PFNs [0x%lx 0x%lx)\n" , +pr_info ("dev %u added %u MB (total %u chunks, %u MB) PFNs [0x%lx 0x%lx)\n" , +MINOR (mdevice->cdevice.dev),DEVMEM_CHUNK_SIZE / (1024 *1024 ), mdevice->devmem_count, mdevice->devmem_count * (DEVMEM_CHUNK_SIZE / (1024 *1024 )), pfn_first, pfn_last);spin_lock (&mdevice->lock); -for (pfn = pfn_first; pfn < pfn_last; pfn++) { +for (pfn = pfn_first; pfn < pfn_last; ) {struct page *page =pfn_to_page (pfn); +if (is_huge && (pfn & (HPAGE_PMD_NR -1 )) ==0 && + pfn +HPAGE_PMD_NR <= pfn_last) { +prep_transhuge_device_private_page (page); + page->zone_device_data = mdevice->free_huge_pages; + mdevice->free_huge_pages = page; + pfn +=HPAGE_PMD_NR ; +continue ; + } page->zone_device_data = mdevice->free_pages; mdevice->free_pages = page; + pfn++; }if (ppage) { - *ppage = mdevice->free_pages; - mdevice->free_pages = (*ppage)->zone_device_data; - mdevice->calloc++; +if (is_huge) { +if (!mdevice->free_huge_pages) + goto err_unlock; + *ppage = mdevice->free_huge_pages; + mdevice->free_huge_pages = (*ppage)->zone_device_data; + mdevice->calloc +=compound_nr (*ppage); + }else if (mdevice->free_pages) { + *ppage = mdevice->free_pages; + mdevice->free_pages = (*ppage)->zone_device_data; + mdevice->calloc++; + }else + goto err_unlock; }spin_unlock (&mdevice->lock);return true; +err_unlock: +spin_unlock (&mdevice->lock); +return false; + err_free:kfree (devmem); err_release:
@@ -535,7 +561,8 @@ static bool dmirror_allocate_chunk(struct dmirror_device *mdevice,return false; } -static struct page *dmirror_devmem_alloc_page (struct dmirror_device *mdevice) +static struct page *dmirror_devmem_alloc_page (struct dmirror_device *mdevice, +bool is_huge) {struct page *dpage =NULL ;struct page *rpage;
@@ -550,17 +577,40 @@ static struct page *dmirror_devmem_alloc_page(struct dmirror_device *mdevice)spin_lock (&mdevice->lock); -if (mdevice->free_pages) { +if (is_huge && mdevice->free_huge_pages) { + dpage = mdevice->free_huge_pages; + mdevice->free_huge_pages = dpage->zone_device_data; + mdevice->calloc +=compound_nr (dpage); +spin_unlock (&mdevice->lock); + }else if (!is_huge && mdevice->free_pages) { dpage = mdevice->free_pages; mdevice->free_pages = dpage->zone_device_data; mdevice->calloc++;spin_unlock (&mdevice->lock); }else {spin_unlock (&mdevice->lock); -if (!dmirror_allocate_chunk (mdevice, &dpage)) +if (!dmirror_allocate_chunk (mdevice, is_huge, &dpage)) goto error; } +if (is_huge) { +unsigned int nr_pages =compound_nr (dpage); +unsigned int i; +struct page **tpage; + + tpage =kmap (rpage); +for (i =0 ; i < nr_pages; i++, tpage++) { + *tpage =alloc_page (GFP_HIGHUSER ); +if (!*tpage) { +while (i--) +__free_page (*--tpage); +kunmap (rpage); + goto error; + } + } +kunmap (rpage); + } + dpage->zone_device_data = rpage;get_page (dpage);lock_page (dpage);
@@ -571,22 +621,26 @@ static struct page *dmirror_devmem_alloc_page(struct dmirror_device *mdevice)return NULL ; } -static void dmirror_migrate_alloc_and_copy (struct migrate_vma *args, -struct dmirror *dmirror) +static int dmirror_migrate_alloc_and_copy (struct migrate_vma *args, +struct dmirror *dmirror) {struct dmirror_device *mdevice = dmirror->mdevice;const unsigned long *src = args->src;unsigned long *dst = args->dst; -unsigned long addr; +unsigned long end_pfn = args->end >>PAGE_SHIFT ; +unsigned long pfn; -for (addr = args->start; addr < args->end; addr +=PAGE_SIZE , - src++, dst++) { +for (pfn = args->start >>PAGE_SHIFT ; pfn < end_pfn; ) {struct page *spage;struct page *dpage;struct page *rpage; +bool is_huge; +unsigned long write; +struct page **tpage; +unsigned long endp;if (!(*src &MIGRATE_PFN_MIGRATE )) -continue ; + goto next;/* * Note that spage might be NULL which is OK since it is an
@@ -594,15 +648,39 @@ static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args,*/ spage =migrate_pfn_to_page (*src); - dpage =dmirror_devmem_alloc_page (mdevice); -if (!dpage) +/* This flag is only set if a whole huge page is migrated. */ + is_huge = *src &MIGRATE_PFN_COMPOUND ; + write = (*src &MIGRATE_PFN_WRITE ) ?MIGRATE_PFN_WRITE :0 ; + +if (dmirror->flags &HMM_DMIRROR_FLAG_FAIL_ALLOC ) { + dmirror->flags &= ~HMM_DMIRROR_FLAG_FAIL_ALLOC ; + dpage =NULL ; + }else + dpage =dmirror_devmem_alloc_page (mdevice, is_huge); +if (!dpage) { +if (!is_huge) +return -ENOMEM ; +/* Try falling back to PAGE_SIZE pages. */ + endp = pfn +512 ;// XXX +while (pfn < endp) { + dpage =dmirror_devmem_alloc_page (mdevice, + false); +if (!dpage) +return -ENOMEM ; + rpage = dpage->zone_device_data; + rpage->zone_device_data = dmirror; + *dst =migrate_pfn (page_to_pfn (dpage)) | +MIGRATE_PFN_LOCKED | write; +if (spage) +copy_highpage (rpage, spage++); +else +clear_highpage (rpage); + pfn++; + src++; + dst++; + }continue ; - - rpage = dpage->zone_device_data; -if (spage) -copy_highpage (rpage, spage); -else -clear_highpage (rpage); + }/* * Normally, a device would use the page->zone_device_data to
@@ -610,14 +688,40 @@ static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args,* the simulated device memory and that page holds the pointer * to the mirror. */ + rpage = dpage->zone_device_data; rpage->zone_device_data = dmirror; - *dst =migrate_pfn (page_to_pfn (dpage)) | -MIGRATE_PFN_LOCKED ; -if ((*src &MIGRATE_PFN_WRITE ) || - (!spage && args->vma->vm_flags &VM_WRITE )) - *dst |=MIGRATE_PFN_WRITE ; + *dst =migrate_pfn (page_to_pfn (dpage)) |MIGRATE_PFN_LOCKED | + write; + +if (is_huge) { + endp = pfn +compound_nr (dpage); + *dst |=MIGRATE_PFN_COMPOUND ; + tpage =kmap (rpage); +while (pfn < endp) { +if (spage) +copy_highpage (*tpage, spage++); +else +clear_highpage (*tpage); + tpage++; + pfn++; + src++; + dst++; + } +kunmap (rpage); +continue ; + } + +if (spage) +copy_highpage (rpage, spage); +else +clear_highpage (rpage); +next: + pfn++; + src++; + dst++; } +return 0 ; } static int dmirror_migrate_finalize_and_map(struct migrate_vma *args,
@@ -628,38 +732,75 @@ static int dmirror_migrate_finalize_and_map(struct migrate_vma *args,const unsigned long *src = args->src;const unsigned long *dst = args->dst;unsigned long pfn; +int ret =0 ;/* Map the migrated pages into the device's page tables. */ mutex_lock (&dmirror->mutex); -for (pfn = start >>PAGE_SHIFT ; pfn < (end >>PAGE_SHIFT ); pfn++, - src++, dst++) { +for (pfn = start >>PAGE_SHIFT ; pfn < (end >>PAGE_SHIFT ); ) { +unsigned long mpfn;struct page *dpage; +struct page *rpage;void *entry;if (!(*src &MIGRATE_PFN_MIGRATE )) -continue ; + goto next; - dpage =migrate_pfn_to_page (*dst); + mpfn = *dst; + dpage =migrate_pfn_to_page (mpfn);if (!dpage) -continue ; + goto next;/* * Store the page that holds the data so the page table * doesn't have to deal with ZONE_DEVICE private pages. */ - entry = dpage->zone_device_data; -if (*dst &MIGRATE_PFN_WRITE ) + rpage = dpage->zone_device_data; +if (mpfn &MIGRATE_PFN_COMPOUND ) { +struct page **tpage; +unsigned long end_pfn = pfn +compound_nr (dpage); + + ret =0 ; + tpage =kmap (rpage); +while (pfn < end_pfn) { + entry = *tpage; +if (mpfn &MIGRATE_PFN_WRITE ) + entry =xa_tag_pointer (entry, +DPT_XA_TAG_WRITE ); + entry =xa_store (&dmirror->pt, pfn, entry, +GFP_KERNEL ); +if (xa_is_err (entry)) { + ret =xa_err (entry); +break ; + } + tpage++; + pfn++; + src++; + dst++; + } +kunmap (rpage); +if (ret) + goto err; +continue ; + } + + entry = rpage; +if (mpfn &MIGRATE_PFN_WRITE ) entry =xa_tag_pointer (entry,DPT_XA_TAG_WRITE ); entry =xa_store (&dmirror->pt, pfn, entry,GFP_ATOMIC );if (xa_is_err (entry)) { -mutex_unlock (&dmirror->mutex); -return xa_err (entry); + ret =xa_err (entry); + goto err; } +next: + pfn++; + src++; + dst++; } +err:mutex_unlock (&dmirror->mutex); -return 0 ; +return ret; } static int dmirror_migrate(struct dmirror *dmirror,
@@ -669,8 +810,8 @@ static int dmirror_migrate(struct dmirror *dmirror,unsigned long size = cmd->npages <<PAGE_SHIFT ;struct mm_struct *mm = dmirror->notifier.mm;struct vm_area_struct *vma; -unsigned long src_pfns[64 ]; -unsigned long dst_pfns[64 ]; +unsigned long *src_pfns; +unsigned long *dst_pfns;struct dmirror_bounce bounce;struct migrate_vma args;unsigned long next;
@@ -685,6 +826,17 @@ static int dmirror_migrate(struct dmirror *dmirror,if (!mmget_not_zero (mm))return -EINVAL ; + src_pfns =kmalloc_array (PTRS_PER_PTE ,sizeof (*src_pfns),GFP_KERNEL ); +if (!src_pfns) { + ret = -ENOMEM ; + goto out_put; + } + dst_pfns =kmalloc_array (PTRS_PER_PTE ,sizeof (*dst_pfns),GFP_KERNEL ); +if (!dst_pfns) { + ret = -ENOMEM ; + goto out_free_src; + } +mmap_read_lock (mm);for (addr = start; addr < end; addr = next) { vma =find_vma (mm, addr);
@@ -693,7 +845,7 @@ static int dmirror_migrate(struct dmirror *dmirror, ret = -EINVAL ; goto out; } - next =min (end, addr + (ARRAY_SIZE (src_pfns) <<PAGE_SHIFT )); + next =min (end, addr + (PTRS_PER_PTE <<PAGE_SHIFT ));if (next > vma->vm_end) next = vma->vm_end;
@@ -703,17 +855,24 @@ static int dmirror_migrate(struct dmirror *dmirror, args.start = addr; args.end = next; args.pgmap_owner = dmirror->mdevice; - args.flags =MIGRATE_VMA_SELECT_SYSTEM ; + args.flags =MIGRATE_VMA_SELECT_SYSTEM | +MIGRATE_VMA_SELECT_COMPOUND ; ret =migrate_vma_setup (&args);if (ret) goto out; -dmirror_migrate_alloc_and_copy (&args, dmirror); -migrate_vma_pages (&args); -dmirror_migrate_finalize_and_map (&args, dmirror); + ret =dmirror_migrate_alloc_and_copy (&args, dmirror); +if (!ret) { +migrate_vma_pages (&args); +dmirror_migrate_finalize_and_map (&args, dmirror); + }migrate_vma_finalize (&args); +if (ret) + goto out; }mmap_read_unlock (mm); +kfree (dst_pfns); +kfree (src_pfns);mmput (mm);/* Return the migrated data for verification. */
@@ -734,6 +893,10 @@ static int dmirror_migrate(struct dmirror *dmirror, out:mmap_read_unlock (mm); +kfree (dst_pfns); +out_free_src: +kfree (src_pfns); +out_put:mmput (mm);return ret; }
@@ -954,6 +1117,11 @@ static long dmirror_fops_unlocked_ioctl(struct file *filp, ret =dmirror_snapshot (dmirror, &cmd);break ; +case HMM_DMIRROR_FLAGS : + dmirror->flags = cmd.npages; + ret =0 ; +break ; +default :return -EINVAL ; }
@@ -977,22 +1145,72 @@ static const struct file_operations dmirror_fops = {static void dmirror_devmem_free (struct page *page) {struct page *rpage = page->zone_device_data; +unsigned int order =compound_order (page); +unsigned int nr_pages =1U << order;struct dmirror_device *mdevice; -if (rpage) +VM_BUG_ON_PAGE (PageTail (page), page); + +if (rpage) { +if (order) { +unsigned int i; +struct page **tpage; +void *kaddr; + + kaddr =kmap_atomic (rpage); + tpage = kaddr; +for (i =0 ; i < nr_pages; i++, tpage++) +__free_page (*tpage); +kunmap_atomic (kaddr); + }__free_page (rpage); + } mdevice =dmirror_page_to_device (page);spin_lock (&mdevice->lock); - mdevice->cfree++; - page->zone_device_data = mdevice->free_pages; - mdevice->free_pages = page; +if (order) { + page->zone_device_data = mdevice->free_huge_pages; + mdevice->free_huge_pages = page; + }else { + page->zone_device_data = mdevice->free_pages; + mdevice->free_pages = page; + } + mdevice->cfree += nr_pages;spin_unlock (&mdevice->lock); } +static void dmirror_devmem_split (struct page *head,struct page *page) +{ +struct page *rpage = head->zone_device_data; +unsigned long i; +struct page **tpage; +void *kaddr; + + page->pgmap = head->pgmap; + i = page - head; +if (i ==1 ) +percpu_ref_get_many (page->pgmap->ref,HPAGE_PMD_NR -1 ); + +if (!rpage) { + page->zone_device_data =NULL ; +return ; + } + + kaddr =kmap_atomic (rpage); + tpage = kaddr; + page->zone_device_data = tpage[i]; +if (i ==1 ) { + head->zone_device_data = tpage[0 ]; +kunmap_atomic (kaddr); +__free_page (rpage); + }else +kunmap_atomic (kaddr); +} +static vm_fault_t dmirror_devmem_fault_alloc_and_copy (struct migrate_vma *args, -struct dmirror *dmirror) +struct dmirror *dmirror, +unsigned long fault_addr) {const unsigned long *src = args->src;unsigned long *dst = args->dst;
@@ -1000,25 +1218,71 @@ static vm_fault_t dmirror_devmem_fault_alloc_and_copy(struct migrate_vma *args,unsigned long end = args->end;unsigned long addr; -for (addr = start; addr < end; addr +=PAGE_SIZE , - src++, dst++) { -struct page *dpage, *spage; +for (addr = start; addr < end; ) { +struct page *spage, *dpage; +unsigned int order =0 ; +unsigned int nr_pages =1 ; +struct page **tpage; +unsigned int i; spage =migrate_pfn_to_page (*src);if (!spage || !(*src &MIGRATE_PFN_MIGRATE )) -continue ; + goto next; + order =compound_order (spage); + nr_pages =1U << order; +/* The source page is the ZONE_DEVICE private page. */ spage = spage->zone_device_data; - dpage =alloc_page_vma (GFP_HIGHUSER_MOVABLE , args->vma, addr); -if (!dpage) -continue ; +if (dmirror->flags &HMM_DMIRROR_FLAG_FAIL_ALLOC ) { + dmirror->flags &= ~HMM_DMIRROR_FLAG_FAIL_ALLOC ; + dpage =NULL ; + }else if (order) + dpage =alloc_transhugepage (args->vma, addr); +else + dpage =alloc_pages_vma (GFP_HIGHUSER_MOVABLE ,0 , + args->vma, addr, +numa_node_id (), false); +if (!dpage) { +if (!order) +return VM_FAULT_OOM ; +/* Try falling back to PAGE_SIZE pages. */ + dpage =alloc_pages_vma (GFP_HIGHUSER_MOVABLE ,0 , + args->vma, addr, +numa_node_id (), false); +if (!dpage) +return VM_FAULT_OOM ; +lock_page (dpage); +xa_erase (&dmirror->pt, fault_addr >>PAGE_SHIFT ); + i = (fault_addr - start) >>PAGE_SHIFT ; + dst[i] =migrate_pfn (page_to_pfn (dpage)) |MIGRATE_PFN_LOCKED ; +if (*src &MIGRATE_PFN_WRITE ) + dst[i] |=MIGRATE_PFN_WRITE ; + tpage =kmap (spage); +copy_highpage (dpage, tpage[i]); +kunmap (spage); + goto next; + }lock_page (dpage);xa_erase (&dmirror->pt, addr >>PAGE_SHIFT ); -copy_highpage (dpage, spage); *dst =migrate_pfn (page_to_pfn (dpage)) |MIGRATE_PFN_LOCKED ;if (*src &MIGRATE_PFN_WRITE ) *dst |=MIGRATE_PFN_WRITE ; +if (order) { + *dst |=MIGRATE_PFN_COMPOUND ; + tpage =kmap (spage); +for (i =0 ; i < nr_pages; i++) { +copy_highpage (dpage, *tpage); + tpage++; + dpage++; + } +kunmap (spage); + }else +copy_highpage (dpage, spage); +next: + addr +=PAGE_SIZE << order; + src += nr_pages; + dst += nr_pages; }return 0 ; }
@@ -1028,33 +1292,55 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf)struct migrate_vma args;unsigned long src_pfns;unsigned long dst_pfns; +struct page *page;struct page *rpage; +unsigned int order;struct dmirror *dmirror;vm_fault_t ret; + page =compound_head (vmf->page); + order =compound_order (page); +/* * Normally, a device would use the page->zone_device_data to point to * the mirror but here we use it to hold the page for the simulated * device memory and that page holds the pointer to the mirror. */ - rpage = vmf->page->zone_device_data; + rpage = page->zone_device_data; dmirror = rpage->zone_device_data; -/* FIXME demonstrate how we can adjust migrate range */ +if (order) { + args.start = vmf->address & (PAGE_MASK << order); + args.end = args.start + (PAGE_SIZE << order); + args.src =kcalloc (PTRS_PER_PTE ,sizeof (*args.src), +GFP_KERNEL ); +if (!args.src) +return VM_FAULT_OOM ; + args.dst =kcalloc (PTRS_PER_PTE ,sizeof (*args.dst), +GFP_KERNEL ); +if (!args.dst) { + ret =VM_FAULT_OOM ; + goto error_src; + } + }else { + args.start = vmf->address; + args.end = args.start +PAGE_SIZE ; + args.src = &src_pfns; + args.dst = &dst_pfns; + } args.vma = vmf->vma; - args.start = vmf->address; - args.end = args.start +PAGE_SIZE ; - args.src = &src_pfns; - args.dst = &dst_pfns; args.pgmap_owner = dmirror->mdevice; - args.flags =MIGRATE_VMA_SELECT_DEVICE_PRIVATE ; + args.flags =MIGRATE_VMA_SELECT_DEVICE_PRIVATE | +MIGRATE_VMA_SELECT_COMPOUND ; -if (migrate_vma_setup (&args)) -return VM_FAULT_SIGBUS ; +if (migrate_vma_setup (&args)) { + ret =VM_FAULT_SIGBUS ; + goto error_dst; + } - ret =dmirror_devmem_fault_alloc_and_copy (&args, dmirror); + ret =dmirror_devmem_fault_alloc_and_copy (&args, dmirror, vmf->address);if (ret) -return ret; + goto error_fin;migrate_vma_pages (&args);/* * No device finalize step is needed since
@@ -1062,12 +1348,27 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf)* invalidated the device page table. */ migrate_vma_finalize (&args); +if (order) { +kfree (args.dst); +kfree (args.src); + }return 0 ; + +error_fin: +migrate_vma_finalize (&args); +error_dst: +if (args.dst != &dst_pfns) +kfree (args.dst); +error_src: +if (args.src != &src_pfns) +kfree (args.src); +return ret; }static const struct dev_pagemap_ops dmirror_devmem_ops = { .page_free = dmirror_devmem_free, .migrate_to_ram = dmirror_devmem_fault, + .page_split = dmirror_devmem_split, };static int dmirror_device_init (struct dmirror_device *mdevice,int id)
@@ -1086,7 +1387,7 @@ static int dmirror_device_init(struct dmirror_device *mdevice, int id)return ret;/* Build a list of free ZONE_DEVICE private struct pages */ -dmirror_allocate_chunk (mdevice,NULL ); +dmirror_allocate_chunk (mdevice, false,NULL );return 0 ; }
lib/test_hmm_uapi.h +3 -0
@@ -33,6 +33,9 @@ struct hmm_dmirror_cmd {#define HMM_DMIRROR_WRITE _IOWR('H', 0x01, struct hmm_dmirror_cmd)#define HMM_DMIRROR_MIGRATE _IOWR('H', 0x02, struct hmm_dmirror_cmd)#define HMM_DMIRROR_SNAPSHOT _IOWR('H', 0x03, struct hmm_dmirror_cmd) +#define HMM_DMIRROR_FLAGS _IOWR('H', 0x04, struct hmm_dmirror_cmd) + +#define HMM_DMIRROR_FLAG_FAIL_ALLOC (1ULL << 0)/* * Values returned in hmm_dmirror_cmd.ptr for HMM_DMIRROR_SNAPSHOT.
tools/testing/selftests/vm/hmm-tests.c +404 -0
408 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v2 7/7 nouveau: support THP migration to private memory
In reply to Ralph CampbellAdd support for migrating transparent huge pages to and from device private memory.
--- drivers/gpu/drm/nouveau/nouveau_dmem.c | 289 ++++++++++++++++++------- drivers/gpu/drm/nouveau/nouveau_svm.c | 11 +- drivers/gpu/drm/nouveau/nouveau_svm.h | 3 +- 3 files changed, 215 insertions(+), 88 deletions(-)
drivers/gpu/drm/nouveau/nouveau_dmem.c +205 -84
@@ -82,6 +82,7 @@ struct nouveau_dmem {struct list_head chunks;struct mutex mutex;struct page *free_pages; +struct page *free_huge_pages;spinlock_t lock; };
@@ -112,8 +113,13 @@ static void nouveau_dmem_page_free(struct page *page)struct nouveau_dmem *dmem = chunk->drm->dmem;spin_lock (&dmem->lock); - page->zone_device_data = dmem->free_pages; - dmem->free_pages = page; +if (PageHead (page)) { + page->zone_device_data = dmem->free_huge_pages; + dmem->free_huge_pages = page; + }else { + page->zone_device_data = dmem->free_pages; + dmem->free_pages = page; + }WARN_ON (!chunk->callocated); chunk->callocated--;
@@ -139,51 +145,100 @@ static void nouveau_dmem_fence_done(struct nouveau_fence **fence)static vm_fault_t nouveau_dmem_fault_copy_one (struct nouveau_drm *drm,struct vm_fault *vmf,struct migrate_vma *args, -dma_addr_t *dma_addr) +struct page *spage,bool is_huge,dma_addr_t *dma_addr) { +struct nouveau_svmm *svmm = spage->zone_device_data;struct device *dev = drm->dev->dev; -struct page *dpage, *spage; -struct nouveau_svmm *svmm; - - spage =migrate_pfn_to_page (args->src[0 ]); -if (!spage || !(args->src[0 ] &MIGRATE_PFN_MIGRATE )) -return 0 ; +struct page *dpage; +unsigned int i; - dpage =alloc_page_vma (GFP_HIGHUSER , vmf->vma, vmf->address); +if (is_huge) + dpage =alloc_transhugepage (vmf->vma, args->start); +else + dpage =alloc_page_vma (GFP_HIGHUSER , vmf->vma, vmf->address);if (!dpage) -return VM_FAULT_SIGBUS ; -lock_page (dpage); +return VM_FAULT_OOM ; +WARN_ON_ONCE (compound_order (spage) !=compound_order (dpage)); - *dma_addr =dma_map_page (dev, dpage,0 ,PAGE_SIZE ,DMA_BIDIRECTIONAL ); + *dma_addr =dma_map_page (dev, dpage,0 ,page_size (dpage), +DMA_BIDIRECTIONAL );if (dma_mapping_error (dev, *dma_addr)) goto error_free_page; - svmm = spage->zone_device_data; +lock_page (dpage); + i = (vmf->address - args->start) >>PAGE_SHIFT ; + spage += i;mutex_lock (&svmm->mutex);nouveau_svmm_invalidate (svmm, args->start, args->end); -if (drm->dmem->migrate.copy_func (drm,1 ,NOUVEAU_APER_HOST , *dma_addr, -NOUVEAU_APER_VRAM ,nouveau_dmem_page_addr (spage))) +if (drm->dmem->migrate.copy_func (drm,compound_nr (dpage), +NOUVEAU_APER_HOST , *dma_addr,NOUVEAU_APER_VRAM , +nouveau_dmem_page_addr (spage))) goto error_dma_unmap;mutex_unlock (&svmm->mutex); - args->dst[0 ] =migrate_pfn (page_to_pfn (dpage)) |MIGRATE_PFN_LOCKED ; + args->dst[i] =migrate_pfn (page_to_pfn (dpage)) |MIGRATE_PFN_LOCKED ; +if (is_huge) + args->dst[i] |=MIGRATE_PFN_COMPOUND ;return 0 ; error_dma_unmap:mutex_unlock (&svmm->mutex); -dma_unmap_page (dev, *dma_addr,PAGE_SIZE ,DMA_BIDIRECTIONAL ); +unlock_page (dpage); +dma_unmap_page (dev, *dma_addr,page_size (dpage),DMA_BIDIRECTIONAL ); error_free_page:__free_page (dpage);return VM_FAULT_SIGBUS ; } +static vm_fault_t nouveau_dmem_fault_chunk (struct nouveau_drm *drm, +struct vm_fault *vmf,struct migrate_vma *args) +{ +struct device *dev = drm->dev->dev; +struct nouveau_fence *fence; +struct page *spage; +unsigned long src = args->src[0 ]; +bool is_huge = (src & (MIGRATE_PFN_MIGRATE |MIGRATE_PFN_COMPOUND )) == + (MIGRATE_PFN_MIGRATE |MIGRATE_PFN_COMPOUND ); +unsigned long dma_page_size; +dma_addr_t dma_addr; +vm_fault_t ret =0 ; + + spage =migrate_pfn_to_page (src); +if (!spage) { + ret =VM_FAULT_SIGBUS ; + goto out; + } +if (is_huge) { + dma_page_size =PMD_SIZE ; + ret =nouveau_dmem_fault_copy_one (drm, vmf, args, spage, true, + &dma_addr); +if (!ret) + goto fence; +/* +* If we couldn't allocate a huge page, fallback to migrating +* a single page. +*/ + } + dma_page_size =PAGE_SIZE ; + ret =nouveau_dmem_fault_copy_one (drm, vmf, args, spage, false, + &dma_addr); +if (ret) + goto out; +fence: +nouveau_fence_new (drm->dmem->migrate.chan, false, &fence); +migrate_vma_pages (args); +nouveau_dmem_fence_done (&fence); +dma_unmap_page (dev, dma_addr, dma_page_size,DMA_BIDIRECTIONAL ); +out: +migrate_vma_finalize (args); +return ret; +} +static vm_fault_t nouveau_dmem_migrate_to_ram (struct vm_fault *vmf) {struct nouveau_drm *drm =page_to_drm (vmf->page); -struct nouveau_dmem *dmem = drm->dmem; -struct nouveau_fence *fence;unsigned long src =0 , dst =0 ; -dma_addr_t dma_addr =0 ; +struct page *page;vm_fault_t ret;struct migrate_vma args = { .vma = vmf->vma,
@@ -192,39 +247,64 @@ static vm_fault_t nouveau_dmem_migrate_to_ram(struct vm_fault *vmf) .src = &src, .dst = &dst, .pgmap_owner = drm->dev, - .flags = MIGRATE_VMA_SELECT_DEVICE_PRIVATE, + .flags = MIGRATE_VMA_SELECT_DEVICE_PRIVATE | + MIGRATE_VMA_SELECT_COMPOUND, }; +/* +* If the page was migrated to the GPU as a huge page, try to +* migrate it back the same way. +*/ + page =compound_head (vmf->page); +if (PageHead (page)) { +unsigned int order =compound_order (page); +unsigned int nr_pages =1U << order; + + args.start &=PAGE_MASK << order; + args.end = args.start + (PAGE_SIZE << order); + args.src =kmalloc_array (nr_pages,sizeof (*args.src), +GFP_KERNEL ); +if (!args.src) +return VM_FAULT_OOM ; + args.dst =kmalloc_array (nr_pages,sizeof (*args.dst), +GFP_KERNEL ); +if (!args.dst) { + ret =VM_FAULT_OOM ; + goto error_src; + } + } +/* * FIXME what we really want is to find some heuristic to migrate more * than just one page on CPU fault. When such fault happens it is very * likely that more surrounding page will CPU fault too. */ -if (migrate_vma_setup (&args) <0 ) -return VM_FAULT_SIGBUS ; -if (!args.cpages) -return 0 ; - - ret =nouveau_dmem_fault_copy_one (drm, vmf, &args, &dma_addr); -if (ret || dst ==0 ) - goto done; - -nouveau_fence_new (dmem->migrate.chan, false, &fence); -migrate_vma_pages (&args); -nouveau_dmem_fence_done (&fence); -dma_unmap_page (drm->dev->dev, dma_addr,PAGE_SIZE ,DMA_BIDIRECTIONAL ); -done: -migrate_vma_finalize (&args); +if (migrate_vma_setup (&args)) + ret =VM_FAULT_SIGBUS ; +else + ret =nouveau_dmem_fault_chunk (drm, vmf, &args); +if (args.dst != &dst) +kfree (args.dst); +error_src: +if (args.src != &src) +kfree (args.src);return ret; } +static void nouveau_page_split (struct page *head,struct page *page) +{ + page->pgmap = head->pgmap; + page->zone_device_data = head->zone_device_data; +} +static const struct dev_pagemap_ops nouveau_dmem_pagemap_ops = { .page_free = nouveau_dmem_page_free, .migrate_to_ram = nouveau_dmem_migrate_to_ram, + .page_split = nouveau_page_split, }; -static int -nouveau_dmem_chunk_alloc (struct nouveau_drm *drm,struct page **ppage) +static int nouveau_dmem_chunk_alloc (struct nouveau_drm *drm,bool is_huge, +struct page **ppage) {struct nouveau_dmem_chunk *chunk;struct resource *res;
@@ -278,16 +358,20 @@ nouveau_dmem_chunk_alloc(struct nouveau_drm *drm, struct page **ppage) pfn_first = chunk->pagemap.range.start >>PAGE_SHIFT ; page =pfn_to_page (pfn_first);spin_lock (&drm->dmem->lock); -for (i =0 ; i <DMEM_CHUNK_NPAGES -1 ; ++i, ++page) { - page->zone_device_data = drm->dmem->free_pages; - drm->dmem->free_pages = page; - } +if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE ) && is_huge) +prep_transhuge_device_private_page (page); +else +for (i =0 ; i <DMEM_CHUNK_NPAGES -1 ; ++i, ++page) { + page->zone_device_data = drm->dmem->free_pages; + drm->dmem->free_pages = page; + } *ppage = page; chunk->callocated++;spin_unlock (&drm->dmem->lock); -NV_INFO (drm,"DMEM: registered %ldMB of device memory\n" , -DMEM_CHUNK_SIZE >>20 ); +NV_INFO (drm,"DMEM: registered %ldMB of %sdevice memory %lx %lx\n" , +DMEM_CHUNK_SIZE >>20 , is_huge ?"huge " :"" , pfn_first, +nouveau_dmem_page_addr (page));return 0 ;
@@ -304,14 +388,20 @@ nouveau_dmem_chunk_alloc(struct nouveau_drm *drm, struct page **ppage) }static struct page * -nouveau_dmem_page_alloc_locked (struct nouveau_drm *drm) +nouveau_dmem_page_alloc_locked (struct nouveau_drm *drm,bool is_huge) {struct nouveau_dmem_chunk *chunk;struct page *page =NULL ;int ret;spin_lock (&drm->dmem->lock); -if (drm->dmem->free_pages) { +if (is_huge && drm->dmem->free_huge_pages) { + page = drm->dmem->free_huge_pages; + drm->dmem->free_huge_pages = page->zone_device_data; + chunk =nouveau_page_to_chunk (page); + chunk->callocated++; +spin_unlock (&drm->dmem->lock); + }else if (!is_huge && drm->dmem->free_pages) { page = drm->dmem->free_pages; drm->dmem->free_pages = page->zone_device_data; chunk =nouveau_page_to_chunk (page);
@@ -319,7 +409,7 @@ nouveau_dmem_page_alloc_locked(struct nouveau_drm *drm)spin_unlock (&drm->dmem->lock); }else {spin_unlock (&drm->dmem->lock); - ret =nouveau_dmem_chunk_alloc (drm, &page); + ret =nouveau_dmem_chunk_alloc (drm, is_huge, &page);if (ret)return NULL ; }
@@ -567,31 +657,22 @@ nouveau_dmem_init(struct nouveau_drm *drm)static unsigned long nouveau_dmem_migrate_copy_one (struct nouveau_drm *drm,struct nouveau_svmm *svmm,unsigned long src, -dma_addr_t *dma_addr,u64 *pfn) +struct page *spage,bool is_huge,dma_addr_t dma_addr,u64 *pfn) { -struct device *dev = drm->dev->dev; -struct page *dpage, *spage; +struct page *dpage;unsigned long paddr; +unsigned long dst; - spage =migrate_pfn_to_page (src); -if (!(src &MIGRATE_PFN_MIGRATE )) - goto out; - - dpage =nouveau_dmem_page_alloc_locked (drm); + dpage =nouveau_dmem_page_alloc_locked (drm, is_huge);if (!dpage) goto out; paddr =nouveau_dmem_page_addr (dpage);if (spage) { - *dma_addr =dma_map_page (dev, spage,0 ,page_size (spage), -DMA_BIDIRECTIONAL ); -if (dma_mapping_error (dev, *dma_addr)) +if (drm->dmem->migrate.copy_func (drm,compound_nr (dpage), +NOUVEAU_APER_VRAM , paddr,NOUVEAU_APER_HOST , dma_addr)) goto out_free_page; -if (drm->dmem->migrate.copy_func (drm,1 , -NOUVEAU_APER_VRAM , paddr,NOUVEAU_APER_HOST , *dma_addr)) - goto out_dma_unmap; }else { - *dma_addr =DMA_MAPPING_ERROR ;if (drm->dmem->migrate.clear_func (drm,page_size (dpage),NOUVEAU_APER_VRAM , paddr)) goto out_free_page;
@@ -602,10 +683,11 @@ static unsigned long nouveau_dmem_migrate_copy_one(struct nouveau_drm *drm, ((paddr >>PAGE_SHIFT ) <<NVIF_VMM_PFNMAP_V0_ADDR_SHIFT );if (src &MIGRATE_PFN_WRITE ) *pfn |=NVIF_VMM_PFNMAP_V0_W ; -return migrate_pfn (page_to_pfn (dpage)) |MIGRATE_PFN_LOCKED ; + dst =migrate_pfn (page_to_pfn (dpage)) |MIGRATE_PFN_LOCKED ; +if (PageHead (dpage)) + dst |=MIGRATE_PFN_COMPOUND ; +return dst; -out_dma_unmap: -dma_unmap_page (dev, *dma_addr,PAGE_SIZE ,DMA_BIDIRECTIONAL ); out_free_page:nouveau_dmem_page_free_locked (drm, dpage); out:
@@ -617,26 +699,64 @@ static void nouveau_dmem_migrate_chunk(struct nouveau_drm *drm, struct nouveau_svmm *svmm, struct migrate_vma *args, dma_addr_t *dma_addrs, u64 *pfns) { + struct device *dev = drm->dev->dev;struct nouveau_fence *fence;unsigned long addr = args->start, nr_dma =0 , i; +unsigned int page_shift =PAGE_SHIFT ; +struct page *spage; +unsigned long src = args->src[0 ]; +bool is_huge = (src & (MIGRATE_PFN_MIGRATE |MIGRATE_PFN_COMPOUND )) == + (MIGRATE_PFN_MIGRATE |MIGRATE_PFN_COMPOUND ); +unsigned long dma_page_size = is_huge ?PMD_SIZE :PAGE_SIZE ; + +if (is_huge) { + spage =migrate_pfn_to_page (src); +if (spage) { + dma_addrs[nr_dma] =dma_map_page (dev, spage,0 , +page_size (spage), +DMA_BIDIRECTIONAL ); +if (dma_mapping_error (dev, dma_addrs[nr_dma])) + goto out; + nr_dma++; + } + args->dst[0 ] =nouveau_dmem_migrate_copy_one (drm, svmm, src, + spage, true, *dma_addrs, pfns); +if (args->dst[0 ] &MIGRATE_PFN_COMPOUND ) { + page_shift =PMD_SHIFT ; + i =1 ; + goto fence; + } + } -for (i =0 ; addr < args->end; i++) { - args->dst[i] =nouveau_dmem_migrate_copy_one (drm, svmm, - args->src[i], dma_addrs + nr_dma, pfns + i); -if (!dma_mapping_error (drm->dev->dev, dma_addrs[nr_dma])) +for (i =0 ; addr < args->end; i++, addr +=PAGE_SIZE ) { + src = args->src[i]; +if (!(src &MIGRATE_PFN_MIGRATE )) +continue ; + spage =migrate_pfn_to_page (src); +if (spage && !is_huge) { + dma_addrs[i] =dma_map_page (dev, spage,0 , +page_size (spage), +DMA_BIDIRECTIONAL ); +if (dma_mapping_error (dev, dma_addrs[i])) +break ; nr_dma++; - addr +=PAGE_SIZE ; + }else if (spage && is_huge && i !=0 ) + dma_addrs[i] = dma_addrs[i -1 ] +PAGE_SIZE ; + args->dst[i] =nouveau_dmem_migrate_copy_one (drm, svmm, src, + spage, false, dma_addrs[i], pfns + i); } +fence:nouveau_fence_new (drm->dmem->migrate.chan, false, &fence);migrate_vma_pages (args);nouveau_dmem_fence_done (&fence); -nouveau_pfns_map (svmm, args->vma->vm_mm, args->start, pfns, i); +nouveau_pfns_map (svmm, args->vma->vm_mm, args->start, pfns, i, + page_shift); -while (nr_dma--) { -dma_unmap_page (drm->dev->dev, dma_addrs[nr_dma],PAGE_SIZE , -DMA_BIDIRECTIONAL ); - } +while (nr_dma) +dma_unmap_page (drm->dev->dev, dma_addrs[--nr_dma], + dma_page_size,DMA_BIDIRECTIONAL ); +out:migrate_vma_finalize (args); }
@@ -648,25 +768,25 @@ nouveau_dmem_migrate_vma(struct nouveau_drm *drm, unsigned long end) {unsigned long npages = (end - start) >>PAGE_SHIFT ; -unsigned long max =min (SG_MAX_SINGLE_ALLOC , npages); +unsigned long max =min (1UL << (PMD_SHIFT -PAGE_SHIFT ), npages);dma_addr_t *dma_addrs;struct migrate_vma args = { .vma = vma, .start = start, .pgmap_owner = drm->dev, - .flags =MIGRATE_VMA_SELECT_SYSTEM , + .flags =MIGRATE_VMA_SELECT_SYSTEM | +MIGRATE_VMA_SELECT_COMPOUND , }; -unsigned long i;u64 *pfns;int ret = -ENOMEM ;if (drm->dmem ==NULL )return -ENODEV ; - args.src =kcalloc (max,sizeof (*args.src),GFP_KERNEL ); + args.src =kmalloc_array (max,sizeof (*args.src),GFP_KERNEL );if (!args.src) goto out; - args.dst =kcalloc (max,sizeof (*args.dst),GFP_KERNEL ); + args.dst =kmalloc_array (max,sizeof (*args.dst),GFP_KERNEL );if (!args.dst) goto out_free_src;
@@ -678,8 +798,10 @@ nouveau_dmem_migrate_vma(struct nouveau_drm *drm,if (!pfns) goto out_free_dma; -for (i =0 ; i < npages; i += max) { - args.end = start + (max <<PAGE_SHIFT ); +for (; args.start < end; args.start = args.end) { + args.end =min (end,ALIGN (args.start,PMD_SIZE )); +if (args.start == args.end) + args.end =min (end, args.start +PMD_SIZE ); ret =migrate_vma_setup (&args);if (ret) goto out_free_pfns;
@@ -687,7 +809,6 @@ nouveau_dmem_migrate_vma(struct nouveau_drm *drm,if (args.cpages)nouveau_dmem_migrate_chunk (drm, svmm, &args, dma_addrs, pfns); - args.start = args.end; } ret =0 ;
drivers/gpu/drm/nouveau/nouveau_svm.c +8 -3
@@ -681,7 +681,6 @@ nouveau_svm_fault(struct nvif_notify *notify)nouveau_svm_fault_cancel_fault (svm, buffer->fault[fi]);continue ; } -SVMM_DBG (svmm,"addr %016llx" , buffer->fault[fi]->addr);/* We try and group handling of faults within a small * window into a single update.
@@ -733,6 +732,10 @@ nouveau_svm_fault(struct nvif_notify *notify) }mmput (mm); +SVMM_DBG (svmm,"addr %llx %s %c" , buffer->fault[fi]->addr, + args.phys[0 ] &NVIF_VMM_PFNMAP_V0_VRAM ? +"vram" :"sysmem" , + args.i.p.size >PAGE_SIZE ?'H' :'N' ); limit = args.i.p.addr + args.i.p.size;for (fn = fi; ++fn < buffer->fault_nr; ) {/* It's okay to skip over duplicate addresses from the
@@ -804,13 +807,15 @@ nouveau_pfns_free(u64 *pfns)void nouveau_pfns_map (struct nouveau_svmm *svmm,struct mm_struct *mm, -unsigned long addr,u64 *pfns,unsigned long npages) +unsigned long addr,u64 *pfns,unsigned long npages, +unsigned int page_shift) {struct nouveau_pfnmap_args *args =nouveau_pfns_to_args (pfns);int ret; args->p.addr = addr; - args->p.size = npages <<PAGE_SHIFT ; + args->p.page = page_shift; + args->p.size = npages << args->p.page;mutex_lock (&svmm->mutex);
drivers/gpu/drm/nouveau/nouveau_svm.h +2 -1
@@ -33,7 +33,8 @@ void nouveau_svmm_invalidate(struct nouveau_svmm *svmm, u64 start, u64 limit);u64 *nouveau_pfns_alloc (unsigned long npages);void nouveau_pfns_free (u64 *pfns);void nouveau_pfns_map (struct nouveau_svmm *svmm,struct mm_struct *mm, -unsigned long addr,u64 *pfns,unsigned long npages); +unsigned long addr,u64 *pfns,unsigned long npages, +unsigned int page_shift);#else /* IS_ENABLED(CONFIG_DRM_NOUVEAU_SVM) */ static inline void nouveau_svm_init (struct nouveau_drm *drm) {}static inline void nouveau_svm_fini (struct nouveau_drm *drm) {}
Signature · 1 line
2.20.1
Re: [PATCH v2 1/7] mm/thp: fix __split_huge_pmd_locked() for migration PMD
In reply to Ralph CampbellQuoted 12 lines · Ralph Campbell
A migrating transparent huge page has to already be unmapped. Otherwise, the page could be modified while it is being copied to a new page and data could be lost. The function
__split_huge_pmd()checks for a PMD migration entry before calling__split_huge_pmd_locked()leading one to think that__split_huge_pmd_locked()can handle splitting a migrating PMD. However, the code always increments thepage->_mapcountand adjusts the memory control group accounting assuming the page is mapped. Also, if the PMD entry is a migration PMD entry, the call tois_huge_zero_pmd(*pmd)is incorrect because it callspmd_pfn(pmd)instead of migration_entry_to_pfn(pmd_to_swp_entry(pmd)).
Fix these problems by checking for a PMD migration entry.
Thanks for catching this. The fix looks good to me. Reviewed-by: Yang Shi <[email protected]>
I think this fix can go separately with the series.
Quoted 76 lines
Signed-off-by Ralph Campbell <[email protected]>--- mm/huge_memory.c | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-)mm/huge_memory.c +23
-20@@ -2023,7 +2023,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,put_page (page);add_mm_counter (mm,mm_counter_file (page), -HPAGE_PMD_NR );return ; - }else if (is_huge_zero_pmd (*pmd)) { + }else if (pmd_trans_huge (*pmd) &&is_huge_zero_pmd (*pmd)) {/* * FIXME: Do we want to invalidate secondary mmu by calling * mmu_notifier_invalidate_range() see comments below inside @@ -2117,30 +2117,34 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, pte =pte_offset_map (&_pmd, addr);BUG_ON (!pte_none (*pte));set_pte_at (mm, addr, pte, entry); -atomic_inc (&page[i]._mapcount); -pte_unmap (pte); - } - -/* -* Set PG_double_map before dropping compound_mapcount to avoid -* false-negative page_mapped(). -*/ -if (compound_mapcount (page) >1 && !TestSetPageDoubleMap (page)) { -for (i =0 ; i <HPAGE_PMD_NR ; i++) +if (!pmd_migration)atomic_inc (&page[i]._mapcount); +pte_unmap (pte); } -lock_page_memcg (page); -if (atomic_add_negative (-1 ,compound_mapcount_ptr (page))) { -/* Last compound_mapcount is gone. */ -__dec_lruvec_page_state (page,NR_ANON_THPS ); -if (TestClearPageDoubleMap (page)) { -/* No need in mapcount reference anymore */ +if (!pmd_migration) { +/* +* Set PG_double_map before dropping compound_mapcount to avoid +* false-negative page_mapped(). +*/ +if (compound_mapcount (page) >1 && + !TestSetPageDoubleMap (page)) {for (i =0 ; i <HPAGE_PMD_NR ; i++) -atomic_dec (&page[i]._mapcount); +atomic_inc (&page[i]._mapcount); + } + +lock_page_memcg (page); +if (atomic_add_negative (-1 ,compound_mapcount_ptr (page))) { +/* Last compound_mapcount is gone. */ +__dec_lruvec_page_state (page,NR_ANON_THPS ); +if (TestClearPageDoubleMap (page)) { +/* No need in mapcount reference anymore */ +for (i =0 ; i <HPAGE_PMD_NR ; i++) +atomic_dec (&page[i]._mapcount); + } } +unlock_page_memcg (page); } -unlock_page_memcg (page);smp_wmb ();/* make pte visible before pmd */ pmd_populate (mm, pmd, pgtable); --2.20.1
Zi Yan
reviewerRe: [PATCH v2 1/7] mm/thp: fix __split_huge_pmd_locked() for migration PMD
In reply to Ralph CampbellQuoted 12 lines · Ralph Campbell
A migrating transparent huge page has to already be unmapped. Otherwise, the page could be modified while it is being copied to a new page and data could be lost. The function
__split_huge_pmd()checks for a PMD migration entry before calling__split_huge_pmd_locked()leading one to think that__split_huge_pmd_locked()can handle splitting a migrating PMD. However, the code always increments thepage->_mapcountand adjusts the memory control group accounting assuming the page is mapped. Also, if the PMD entry is a migration PMD entry, the call tois_huge_zero_pmd(*pmd)is incorrect because it callspmd_pfn(pmd)instead of migration_entry_to_pfn(pmd_to_swp_entry(pmd)).
Fix these problems by checking for a PMD migration entry.
Quoted 1 line
Signed-off-by Ralph Campbell <[email protected]>
Thanks for the fix. You can add Reviewed-by: Zi Yan <[email protected]>
I think you also want to add the Fixes tag and cc stable.
Fixes 84c3fc4e9c56 (“mm: thp: check pmd migration entry in common path”)
cc: [email protected] # 4.14+
Quoted 72 lines
--- mm/huge_memory.c | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-)mm/huge_memory.c +23
-19@@ -2023,7 +2023,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,put_page (page);add_mm_counter (mm,mm_counter_file (page), -HPAGE_PMD_NR );return ; - }else if (is_huge_zero_pmd (*pmd)) { + }else if (pmd_trans_huge (*pmd) &&is_huge_zero_pmd (*pmd)) {/* * FIXME: Do we want to invalidate secondary mmu by calling * mmu_notifier_invalidate_range() see comments below inside @@ -2117,30 +2117,34 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, pte =pte_offset_map (&_pmd, addr);BUG_ON (!pte_none (*pte));set_pte_at (mm, addr, pte, entry); -atomic_inc (&page[i]._mapcount); -pte_unmap (pte); - } - -/* -* Set PG_double_map before dropping compound_mapcount to avoid -* false-negative page_mapped(). -*/ -if (compound_mapcount (page) >1 && !TestSetPageDoubleMap (page)) { -for (i =0 ; i <HPAGE_PMD_NR ; i++) +if (!pmd_migration)atomic_inc (&page[i]._mapcount); +pte_unmap (pte); } -lock_page_memcg (page); -if (atomic_add_negative (-1 ,compound_mapcount_ptr (page))) { -/* Last compound_mapcount is gone. */ -__dec_lruvec_page_state (page,NR_ANON_THPS ); -if (TestClearPageDoubleMap (page)) { -/* No need in mapcount reference anymore */ +if (!pmd_migration) { +/* +* Set PG_double_map before dropping compound_mapcount to avoid +* false-negative page_mapped(). +*/ +if (compound_mapcount (page) >1 && + !TestSetPageDoubleMap (page)) {for (i =0 ; i <HPAGE_PMD_NR ; i++) -atomic_dec (&page[i]._mapcount); +atomic_inc (&page[i]._mapcount); + } + +lock_page_memcg (page); +if (atomic_add_negative (-1 ,compound_mapcount_ptr (page))) { +/* Last compound_mapcount is gone. */ +__dec_lruvec_page_state (page,NR_ANON_THPS ); +if (TestClearPageDoubleMap (page)) { +/* No need in mapcount reference anymore */ +for (i =0 ; i <HPAGE_PMD_NR ; i++) +atomic_dec (&page[i]._mapcount); + } } +unlock_page_memcg (page); } -unlock_page_memcg (page);smp_wmb ();/* make pte visible before pmd */ pmd_populate (mm, pmd, pgtable);Signature · 1 line
2.20.1
—
Best Regards,
Yan Zi
Re: [PATCH v2 1/7] mm/thp: fix __split_huge_pmd_locked() for migration PMD
Quoted 14 lines · Zi Yan
Quoted 12 lines · Ralph Campbell
A migrating transparent huge page has to already be unmapped. Otherwise, the page could be modified while it is being copied to a new page and data could be lost. The function
__split_huge_pmd()checks for a PMD migration entry before calling__split_huge_pmd_locked()leading one to think that__split_huge_pmd_locked()can handle splitting a migrating PMD. However, the code always increments thepage->_mapcountand adjusts the memory control group accounting assuming the page is mapped. Also, if the PMD entry is a migration PMD entry, the call tois_huge_zero_pmd(*pmd)is incorrect because it callspmd_pfn(pmd)instead of migration_entry_to_pfn(pmd_to_swp_entry(pmd)).
Fix these problems by checking for a PMD migration entry.
Quoted 8 lines
Quoted 1 line
Signed-off-by Ralph Campbell <[email protected]>Thanks for the fix. You can add Reviewed-by: Zi Yan <[email protected]>
I think you also want to add the Fixes tag and cc stable.
Fixes
84c3fc4e9c56(“mm: thp: check pmd migration entry in common path”)
cc: [email protected] # 4.14+
Thanks, I'll add these.
Re: [PATCH v2 1/7] mm/thp: fix __split_huge_pmd_locked() for migration PMD
In reply to Ralph CampbellQuoted 12 lines · Ralph Campbell
A migrating transparent huge page has to already be unmapped. Otherwise, the page could be modified while it is being copied to a new page and data could be lost. The function
__split_huge_pmd()checks for a PMD migration entry before calling__split_huge_pmd_locked()leading one to think that__split_huge_pmd_locked()can handle splitting a migrating PMD. However, the code always increments thepage->_mapcountand adjusts the memory control group accounting assuming the page is mapped. Also, if the PMD entry is a migration PMD entry, the call tois_huge_zero_pmd(*pmd)is incorrect because it callspmd_pfn(pmd)instead of migration_entry_to_pfn(pmd_to_swp_entry(pmd)).
Fix these problems by checking for a PMD migration entry.
Quoted 1 line
Signed-off-by Ralph Campbell <[email protected]>
Hm. Could you remind me what codepath splits migration PMD? Maybe it should wait until migration is complete? We could avoid a lot of complexity this way.
Signature · 1 line
Kirill A. Shutemov