drm/amdkfd: Enable device private memory THP support in kfd svm driver
Subsystems (3)
v5 cover drm/amdkfd: Enable device private memory THP support in kfd svm driver
From: Xiaogang Chen <[email protected]>
Add device private THP support to migration in kfd driver by using following work [1]. The implementation enables efficient migration of large folios between system memory and device-private memory.
A benchmark test shows the migration speed got improved more than 5 times comparing to current kfd/svm driver.
[1]https://lore.kernel.org/linux-mm/[email protected]/
v2:
- Reset is_large each 4K page iteration
- Unmap the THP DMA mapping once as DMA mapping, at its real size
- Leave migrate->dst[i] zero when the source is not migrating
- Count THP migrations as HPAGE_PMD_NR pages in prange->vram_pages
- Implement folio_split and hold one svm_bo ref per subpage
v3:
- Do not migrate partial THP memory at CPU/GPU page fault handler
- re-configure compound device folio after it is release from core HMM. So, it can be reused by ttm as 4K pages.
- Remove SDMA page entry FLAG(9) setting at GART page table since not see noticelbe perofmance change.
v4: fix multiples issues from Claude code review
- Fill every dst slot when a device THP falls back to 4K RAM pages, and handle an out-of-range compound entry
If no THP destination could be allocated, only the head slot got a 4K page. migrate_vma_pages() then splits the source and dereferences every tail dst slot without a NULL check. This happened on any failed THP allocation, and on every VMA with THP disabled. The new svm_migrate_compound_to_4k_ram()/svm_migrate_compound_to_4k_vram fills all 512 slots and copies them with one GART copy.
Out-of-range entry in both copy directions, a compound entry that runs past npages now triggers WARN_ON_ONCE() and fails the window.
- Build with THP disabled: HPAGE_PMD_* expand to BUILD_BUG() without CONFIG_TRANSPARENT_HUGEPAGE. Define SVM_THP_ENABLED and SVM_HPAGE_PMD_* to control the build.
- Don't dereference the folio in svm_range_cpu_huge_folio_order()
The PMD was read without locks, and the code then called folio_order() on its folio with no reference. MADV_DONTNEED, deferred split or migration could free or reuse that folio, which could make the migration window larger than intended.
Fix: a huge PMD always maps a PMD-order folio, so return SVM_HPAGE_PMD_ORDER without touching the folio.
- Get svm_bo from the folio head in svm_migrate_to_ram()(cpu page fault)
When a device THP's PMD has been split but the folio hasn't (partial munmap/mprotect/madvise, or a failed split_folio in migrate_vma_setup()), the CPU fault passes a tail page. Only the head page has zone_device_data set, so on a tail page svm_bo was garbage and svm_bo->mm could oops.
Read it from page_folio(vmf->page), which is safe because the caller holds the folio lock and a reference
- Respect THP policy and check NULL in svm_migrate_get_sys_page()
Problem: the result of vma_alloc_folio() went into folio_page() with no NULL check. The order-9 allocation used GFP_HIGHUSER, which means direct reclaim and compaction on every VRAM→RAM migration (fault, prefetch, eviction). It also ignored MADV_NOHUGEPAGE and PR_SET_THP_DISABLE.
Fix: check for NULL; skip THP when vma_thp_disabled() says so; allocate with GFP_TRANSHUGE_LIGHT, adding direct reclaim only for MADV_HUGEPAGE VMAs; Compile the THP path only with THP enabled.
- Reset device THP pages before dropping the svm_bo reference(race issue)
Problem: svm_migrate_folio_free() dropped the svm_bo reference first, then reset the 512 struct pages with svm_migrate_clear_compound_pages(). If that was the last reference, the VRAM could be freed and given to another range. A new zone_device_folio_init() on those pfns could then race with the reset, which could wipe the tails of a newly created THP.
Fix: reset the pages first, while the folio still holds the BO alive.
v5:
- Rebase code on tip of amd-staging-drm-next branch which is based on kernel 7.2.0
Xiaogang Chen (3):
drm/amdkfd: Add awareness of THP of device and system RAM in kfd svm
driver
drm/amdkfd: Do not migrate partial THP memory at CPU/GPU page fault
handler
drm/amdkfd: Enable HMM device-private THP migration in KFD SVM driver
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 642 +++++++++++++++++++++-- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 125 ++++- drivers/gpu/drm/amd/amdkfd/kfd_svm.h | 30 ++ 3 files changed, 732 insertions(+), 65 deletions(-)
Signature · 1 line
2.34.1
PATCH v5 1/3 drm/amdkfd: Add awareness of THP of device and system RAM in kfd svm driver
From: Xiaogang Chen <[email protected]>
Extend kfd/svm functions to allocate HPAGE_PMD_SIZE based device memory by buddy allocator, each drm_buddy_block is HPAGE_PMD_SIZE aligned; and to allocate THP system ram by vma_alloc_folio.
Introduce SVM_RANGE_DMA_THP flag that indicates dma map of THP. THP dma addresss will use this flag.
Add dev_pagemap_ops->folio_split callback that is called by folio_split when core MM splits device memory folio.
Reset compound folio of device memory after it got released from core HMM. So, TTM can reuse this VRAM as 4K pages on next range.
These are preparations for following support for (THP) migration of zone device-private memory, no function change.
--- drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 112 ++++++++++++++++++++--- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 39 ++++++-- drivers/gpu/drm/amd/amdkfd/kfd_svm.h | 18 ++++ 3 files changed, 152 insertions(+), 17 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +101 -11
@@ -218,17 +218,45 @@ svm_migrate_addr_to_pfn(struct amdgpu_device *adev, unsigned long addr)return (addr + adev->kfd.pgmap.range.start) >>PAGE_SHIFT ; } +/* +* TTM can reuse a 2MB VRAM block that was previously a device THP. +* Reset unused pages so the next 4K init does not see leftover head/tail. +*/ +static void svm_migrate_clear_compound_pages (struct page *page,unsigned int nr, +struct dev_pagemap *pgmap) +{ +unsigned int i; + +for (i =0 ; i < nr; i++, page++) { +struct folio *new_folio = (struct folio *)page; + +if (page_count (page)) +continue ; + + page->flags.f &= ~0xffUL ; +#ifdef NR_PAGES_IN_LARGE_FOLIO + ((struct folio *)(page -1 ))->_nr_pages =0 ; +#endif + new_folio->mapping =NULL ; + new_folio->pgmap = pgmap; + new_folio->share =0 ; + page->zone_device_data =NULL ; + } +} +static void -svm_migrate_get_vram_page (struct svm_range *prange,unsigned long pfn) +svm_migrate_get_vram_page (struct svm_range *prange,unsigned long pfn, +int order) { -struct page *page; +struct page *page =pfn_to_page (pfn); - page =pfn_to_page (pfn); -svm_range_bo_ref (prange->svm_bo); +/* Init this pfn, not page_folio() — that follows a leftover tail to the old head. */ +zone_device_folio_init ((struct folio *)page,page_pgmap (page), order); page->zone_device_data = prange->svm_bo; -zone_device_page_init (page,page_pgmap (page),0 ); +svm_range_bo_ref (prange->svm_bo); } +static void svm_migrate_put_vram_page (struct amdgpu_device *adev,unsigned long addr) {
@@ -249,11 +277,45 @@ svm_migrate_addr(struct amdgpu_device *adev, struct page *page) }static struct page * -svm_migrate_get_sys_page (struct vm_area_struct *vma,unsigned long addr) +svm_migrate_get_sys_page (struct vm_area_struct *vma,unsigned long addr, +unsigned long order) {struct page *page; - page =alloc_page_vma (GFP_HIGHUSER , vma, addr); +if (order) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +struct folio *folio; +gfp_t gfp; + +/* +* Honour MADV_NOHUGEPAGE and prctl(PR_SET_THP_DISABLE). The +* sysfs THP policy (thp_vma_allowable_orders()) and +* vma_thp_gfp_mask() are not exported to modules. +*/ +if (vma_thp_disabled (vma, vma->vm_flags, false)) +return NULL ; + +/* +* Used by every VRAM to RAM migration (CPU/GPU fault, +* prefetch, TTM eviction), and the caller falls back to 4K +* pages on failure. Fail fast like the default "madvise" +* defrag policy: no direct reclaim or compaction unless the +* VMA asked for huge pages. Eviction in particular runs under +* memory pressure, where compaction stalls are most costly. +*/ + gfp =GFP_TRANSHUGE_LIGHT ; +if (vma->vm_flags &VM_HUGEPAGE ) + gfp |= __GFP_DIRECT_RECLAIM; + + folio =vma_alloc_folio (gfp, order, vma, addr); + page = folio ?folio_page (folio,0 ) :NULL ; +#else +return NULL ; +#endif + }else { + page =alloc_page_vma (GFP_HIGHUSER , vma, addr); + } +if (page)lock_page (page);
@@ -267,8 +329,13 @@ static unsigned long svm_migrate_successful_pages(struct migrate_vma *migrate)for (i =0 ; i < migrate->npages; i++) {if (migrate->dst[i] &MIGRATE_PFN_VALID && - migrate->src[i] &MIGRATE_PFN_MIGRATE ) - mpages++; + migrate->src[i] &MIGRATE_PFN_MIGRATE ) { + +if (migrate->dst[i] &MIGRATE_PFN_COMPOUND ) + mpages +=SVM_HPAGE_PMD_NR ; +else + mpages++; + } }return mpages; }
@@ -312,7 +379,7 @@ svm_migrate_copy_to_vram(struct kfd_node *node, struct svm_range *prange, migrate->dst[i] =0 ; spage =NULL ; }else { -svm_migrate_get_vram_page (prange, migrate->dst[i]); +svm_migrate_get_vram_page (prange, migrate->dst[i],0 ); migrate->dst[i] =migrate_pfn (migrate->dst[i]); mpages++; }
@@ -626,10 +693,17 @@ svm_migrate_ram_to_vram(struct svm_range *prange, uint32_t best_loc,return r <0 ? r :0 ; } +/* folio can be compound folio or single page */ static void svm_migrate_folio_free (struct folio *folio) {struct page *page = &folio->page;struct svm_range_bo *svm_bo = page->zone_device_data; +struct dev_pagemap *pgmap = folio->pgmap; +unsigned int order =folio_order (folio); + +/* So TTM can reuse this VRAM as 4K pages on the next range. */ +if (order) +svm_migrate_clear_compound_pages (page,1U << order, pgmap);if (svm_bo) {pr_debug_ratelimited ("ref: %d\n" ,kref_read (&svm_bo->kref));
@@ -688,7 +762,7 @@ svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange, j =0 ; } - dpage =svm_migrate_get_sys_page (migrate->vma, addr); + dpage =svm_migrate_get_sys_page (migrate->vma, addr,0 );if (!dpage) {pr_debug ("failed get page svms 0x%p [0x%lx 0x%lx]\n" , prange->svms, prange->start, prange->last);
@@ -1091,9 +1165,25 @@ static vm_fault_t svm_migrate_to_ram(struct vm_fault *vmf)return r ?VM_FAULT_SIGBUS :0 ; } +static void svm_migrate_folio_split (struct folio *head,struct folio *tail) +{ +struct svm_range_bo *svm_bo; + +if (tail ==NULL ) +return ; + + tail->pgmap = head->pgmap; + tail->mapping = head->mapping; + + svm_bo =folio_zone_device_data (head); +folio_set_zone_device_data (tail, svm_bo); +svm_range_bo_ref (svm_bo); +} +static const struct dev_pagemap_ops svm_migrate_pgmap_ops = { .folio_free = svm_migrate_folio_free, .migrate_to_ram = svm_migrate_to_ram, + .folio_split = svm_migrate_folio_split, };/* Each VRAM page uses sizeof(struct page) on system memory */
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +33 -6
@@ -246,7 +246,16 @@ void svm_range_dma_unmap_dev(struct device *dev, dma_addr_t *dma_addr,if (!svm_is_valid_dma_mapping_addr (dev, dma_addr[i]))continue ;pr_debug_ratelimited ("unmap 0x%llx\n" , dma_addr[i] >>PAGE_SHIFT ); -dma_unmap_page (dev, dma_addr[i],PAGE_SIZE , dir); + +/* dma unmap of THP */ +if (dma_addr[i] &SVM_RANGE_DMA_THP ) { + dma_addr[i] &= ~SVM_RANGE_DMA_THP ; +dma_unmap_page (dev, dma_addr[i],PAGE_SIZE *SVM_HPAGE_PMD_NR , +DMA_BIDIRECTIONAL ); + }else { +dma_unmap_page (dev, dma_addr[i],PAGE_SIZE , dir); + } + dma_addr[i] =0 ; } }
@@ -579,10 +588,20 @@ svm_range_vram_node_new(struct kfd_node *node, struct svm_range *prange, }memset (&bp,0 ,sizeof (bp)); +/* +* Keep bp.size equal to the prange size so it matches the +* amdgpu_amdkfd_reserve_mem_limit() reservation made by the caller. +* A 2MB byte_align makes the VRAM manager allocate 2MB-aligned 2MB +* buddy blocks and trim only the last one to the requested size, so +* every full 2MB chunk can still back a device THP. +*/ bp.size = prange->npages *PAGE_SIZE ; +if (SVM_THP_ENABLED && prange->npages >=SVM_HPAGE_PMD_NR ) + bp.byte_align =SVM_HPAGE_PMD_SIZE ; +else + bp.byte_align =PAGE_SIZE ; bp.bo_ptr_size =sizeof (struct svm_range_bo ); bp.destroy = svm_range_bo_destroy; - bp.byte_align =PAGE_SIZE ; bp.domain =AMDGPU_GEM_DOMAIN_VRAM ; bp.flags =AMDGPU_GEM_CREATE_NO_CPU_ACCESS ; bp.flags |= clear ?AMDGPU_GEM_CREATE_VRAM_CLEARED :0 ;
@@ -594,9 +613,16 @@ svm_range_vram_node_new(struct kfd_node *node, struct svm_range *prange, r =amdgpu_bo_create (node->adev, &bp, &bo);if (r) { -pr_debug ("failed %d to create bo\n" , r); -mmput (mm); - goto create_bo_failed; +if (bp.byte_align ==SVM_HPAGE_PMD_SIZE ) { +/* fall back PAGE_SIZE granularity */ + bp.byte_align =PAGE_SIZE ; + r =amdgpu_bo_create (node->adev, &bp, &bo); + } +if (r) { +pr_debug ("failed %d to create bo\n" , r); +mmput (mm); + goto create_bo_failed; + } } svm_bo =to_svm_range_bo (bo);
@@ -957,6 +983,7 @@ svm_range_copy_array(void *psrc, size_t size, uint64_t num_elements, *vram_pages =0 ;for (i =0 ; i < num_elements; i++) {dma_addr_t *temp; + temp = (dma_addr_t *)dst + i; *temp = *((dma_addr_t *)src + i);if (*temp&SVM_RANGE_VRAM_DOMAIN )
@@ -1675,7 +1702,7 @@ static int svm_range_reserve_bos(struct svm_validate_context *ctx, bool intr)uint32_t gpuidx;int r; -drm_exec_init (&ctx->exec, intr ?DRM_EXEC_INTERRUPTIBLE_WAIT :0 ,0 ); +drm_exec_init (&ctx->exec, intr ?DRM_EXEC_INTERRUPTIBLE_WAIT :0 ,0 ); drm_exec_until_all_locked(&ctx->exec) {for_each_set_bit (gpuidx, ctx->bitmap,MAX_GPU_INSTANCE ) { pdd =kfd_process_device_from_gpuidx (ctx->process, gpuidx);
drivers/gpu/drm/amd/amdkfd/kfd_svm.h +18 -0
@@ -35,6 +35,24 @@#include "kfd_priv.h" #define SVM_RANGE_VRAM_DOMAIN (1UL << 0) +#define SVM_RANGE_DMA_THP (1UL << 1) + +/* +* Size of a PMD-mapped device or system THP. HPAGE_PMD_* expand to +* BUILD_BUG() without CONFIG_TRANSPARENT_HUGEPAGE, so the SVM code uses +* these instead. Without THP, SVM_THP_ENABLED is false, no compound +* migration is requested, and HMM never reports MIGRATE_PFN_COMPOUND, so +* the THP paths still compile but are never taken. +*/ +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +#define SVM_HPAGE_PMD_ORDER HPAGE_PMD_ORDER +#else +#define SVM_HPAGE_PMD_ORDER (PMD_SHIFT - PAGE_SHIFT) +#endif +#define SVM_HPAGE_PMD_NR (1UL << SVM_HPAGE_PMD_ORDER) +#define SVM_HPAGE_PMD_SIZE (PAGE_SIZE << SVM_HPAGE_PMD_ORDER) +#define SVM_THP_ENABLED IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) +#define SVM_ADEV_PGMAP_OWNER (adev)\ ((adev)->hive ? (void *)(adev)->hive : (void *)(adev))
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 1/3] drm/amdkfd: Add awareness of THP of device and system RAM in kfd svm driver
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] drm/amdkfd: race and tail refcount bug in svm_migrate_clear_compound_pages() - [Medium] drm/amdkfd…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] drm/amdkfd: race and tail refcount bug in svm_migrate_clear_compound_pages()
- [Medium] drm/amdkfd: svm_migrate_get_vram_page() calls page_pgmap() on stale tail page
- [Medium] drm/amdkfd: VRAM eviction thrashing and defeated fallback in svm_range_vram_node_new()
--
commit 19748270f4f63cf5e57c0c13c312409968fa180c
Author: Xiaogang Chen <[email protected]>
drm/amdkfd: Add awareness of THP of device and system RAM in kfd svm driver
This commit extends KFD SVM migration and buffer allocation helpers to support PMD-sized compound folios for device-private VRAM and system RAM.
Quoted 23 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +15
-0@@ -218,17 +218,45 @@ svm_migrate_addr_to_pfn(struct amdgpu_device *adev, unsigned long addr)return (addr + adev->kfd.pgmap.range.start) >>PAGE_SHIFT ; } +/* +* TTM can reuse a 2MB VRAM block that was previously a device THP. +* Reset unused pages so the next 4K init does not see leftover head/tail. +*/ +static void svm_migrate_clear_compound_pages (struct page *page,unsigned int nr, +struct dev_pagemap *pgmap) +{ +unsigned int i; + +for (i =0 ; i < nr; i++, page++) { +struct folio *new_folio = (struct folio *)page; + +if (page_count (page)) +continue ; +
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c svm_migrate_addr_to_pfn(struct amdgpu_device *adev, unsigned long addr) · line 245
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +10
-0+ page->flags.f &= ~0xffUL ; +#ifdef NR_PAGES_IN_LARGE_FOLIO + ((struct folio *)(page -1 ))->_nr_pages =0 ; +#endif + new_folio->mapping =NULL ; + new_folio->pgmap = pgmap; + new_folio->share =0 ; + page->zone_device_data =NULL ; + } +}
[Severity: High]
Can svm_migrate_clear_compound_pages() skip clearing tail pages or corrupt newly reallocated VRAM pages?
During a CPU fault on a device-private folio, do_huge_pmd_device_private() and do_swap_page() hold an extra folio reference across pgmap->ops->migrate_to_ram() and drop it via folio_put() after svm_migrate_to_ram() has already released prange->migrate_mutex. Because folio_put_testzero() decrements the folio refcount to 0 before calling free_zone_device_folio() -> svm_migrate_folio_free(), a concurrent migration reusing the same svm_bo can reallocate those VRAM pages while svm_migrate_clear_compound_pages() is running:
CPU0 CPU1do_huge_pmd_device_private()
folio_get(folio)
svm_migrate_to_ram()
...
mutex_unlock(&prange->migrate_mutex)
folio_put(folio)
folio_put_testzero(folio)
svm_migrate_ram_to_vram()
svm_migrate_copy_to_vram()
page_ref_count(page) == 0
svm_migrate_get_vram_page()
zone_device_folio_init()
free_zone_device_folio()
svm_migrate_folio_free()
svm_migrate_clear_compound_pages()
page_count(page) == 0
page->flags.f &= ~0xffUL
page->zone_device_data = NULL <-- clobbers reallocated folioEven without the TOCTOU race between page_count(page) and set_page_count(page, 1) inside zone_device_page_init(), page_count(page) calls folio_ref_count(page_folio(page)) before new_folio->pgmap = pgmap clears compound_head on tail pages. If page[0] has already been reallocated as a 4K page (setting page[0]._refcount = 1), page_folio() on each remaining tail page (i = 1 .. nr - 1) follows the leftover compound_head back to page[0], sees page_count() == 1, and skips clearing all 511 unused tail pages.
In addition, in svm_migrate_copy_to_vram(), page_ref_count() reads page->_refcount directly, which is 0 on tail pages even before the head folio's final reference is put. Reinitializing tail page 1 as a 4K page wipes folio_order() and folio->_nr_pages on the still-live compound folio before folio_put() runs, so free_zone_device_folio() leaks 512 pgmap->ref references and hangs memunmap_pages() on device teardown.
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c svm_migrate_addr_to_pfn(struct amdgpu_device *adev, unsigned long addr) · line 254
Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +5
-4static void -svm_migrate_get_vram_page (struct svm_range *prange,unsigned long pfn) +svm_migrate_get_vram_page (struct svm_range *prange,unsigned long pfn, +int order) { -struct page *page; +struct page *page =pfn_to_page (pfn); - page =pfn_to_page (pfn); -svm_range_bo_ref (prange->svm_bo); +/* Init this pfn, not page_folio() — that follows a leftover tail to the old head. */ +zone_device_folio_init ((struct folio *)page,page_pgmap (page), order);
[Severity: Medium]
Does page_pgmap(page) still call page_folio(page) on a leftover tail page here?
include/linux/mmzone.h:page_pgmap() {
VM_WARN_ON_ONCE_PAGE(!memdesc_is_zone_device(&page->flags), page);
return page_folio(page)->pgmap;
}Because page_pgmap(page) runs before zone_device_folio_init() clears any stale compound_head on page, it still follows a leftover tail pointer to the old head page. If that old head page has since become a tail page of another compound folio, its pgmap union slot holds an odd compound_head value, passing a corrupted pgmap pointer into zone_device_page_init() and percpu_ref_tryget_many(). Passing &prange->svm_bo->node->adev->kfd.pgmap directly avoids reading page_pgmap(page) before the page is initialized.
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0
-0
[ ... ]
Quoted 30 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +12
-4@@ -579,10 +588,20 @@ svm_range_vram_node_new(struct kfd_node *node, struct svm_range *prange, }memset (&bp,0 ,sizeof (bp)); +/* +* Keep bp.size equal to the prange size so it matches the +* amdgpu_amdkfd_reserve_mem_limit() reservation made by the caller. +* A 2MB byte_align makes the VRAM manager allocate 2MB-aligned 2MB +* buddy blocks and trim only the last one to the requested size, so +* every full 2MB chunk can still back a device THP. +*/ bp.size = prange->npages *PAGE_SIZE ; +if (SVM_THP_ENABLED && prange->npages >=SVM_HPAGE_PMD_NR ) + bp.byte_align =SVM_HPAGE_PMD_SIZE ; +else + bp.byte_align =PAGE_SIZE ; bp.bo_ptr_size =sizeof (struct svm_range_bo ); bp.destroy = svm_range_bo_destroy; - bp.byte_align =PAGE_SIZE ; bp.domain =AMDGPU_GEM_DOMAIN_VRAM ; bp.flags =AMDGPU_GEM_CREATE_NO_CPU_ACCESS ; bp.flags |= clear ?AMDGPU_GEM_CREATE_VRAM_CLEARED :0 ;@@ -594,9 +613,16 @@ svm_range_vram_node_new(struct kfd_node *node, struct svm_range *prange, r =amdgpu_bo_create (node->adev, &bp, &bo);if (r) { -pr_debug ("failed %d to create bo\n" , r); -mmput (mm); - goto create_bo_failed; +if (bp.byte_align ==SVM_HPAGE_PMD_SIZE ) {
drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_vram_node_new(struct kfd_node *node, struct svm_range *prange, · line 626
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +9
-0+/* fall back PAGE_SIZE granularity */ + bp.byte_align =PAGE_SIZE ; + r =amdgpu_bo_create (node->adev, &bp, &bo); + } +if (r) { +pr_debug ("failed %d to create bo\n" , r); +mmput (mm); + goto create_bo_failed; + } }
[Severity: Medium]
Does the first amdgpu_bo_create() call trigger TTM VRAM LRU eviction when 2MB blocks are fragmented, and is the PAGE_SIZE fallback a no-op when bp.size is a multiple of 2MB?
When bp.byte_align is SVM_HPAGE_PMD_SIZE, amdgpu_vram_mgr_new() sets min_block_size = 2MB and gpu_buddy_alloc_blocks() rounds the allocation up to a 2MB multiple before trimming the last block. If VRAM has enough 4K pages but no 2MB-contiguous blocks, ttm_bo_validate() does not return to the fallback immediately; its second pass calls ttm_bo_evict_alloc() and evicts other live VRAM BOs trying to coalesce 2MB blocks.
Furthermore, when bp.size is an exact multiple of 2MB, resetting bp.byte_align = PAGE_SIZE in the fallback still leaves min_block_size at 2MB (pages_per_block defaults to HPAGE_PMD_NR):
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c:amdgpu_vram_mgr_new() {
...
if (tbo->page_alignment)
min_block_size = (u64)tbo->page_alignment << PAGE_SHIFT;
else if ((size >= (u64)pages_per_block << PAGE_SHIFT) &&
!(size & (((u64)pages_per_block << PAGE_SHIFT) - 1)))
min_block_size = (u64)pages_per_block << PAGE_SHIFT;
else
min_block_size = PAGE_SIZE;
...
}Because tbo->page_alignment is 1 when bp.byte_align is PAGE_SIZE, wait - actually when tbo->page_alignment is 0 or when size is a 2MB multiple, the fallback does not relax the 2MB requirement or repeats the eviction scan.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v5 2/3 drm/amdkfd: Do not migrate partial THP memory at CPU/GPU page fault handler
In reply to Xiaogang.ChenFrom: Xiaogang Chen <[email protected]>
After support device THP the page fault(either CPU or GPU) address can point to THP(device or anonymous THP).
If the correspondent svm prange covers the whole compound folio migrate at folio order so HMM can collect MIGRATE_PFN_COMPOUND.
Do not migrate partial THP memory. If do HMM will split THP folio. If the compound folio has extra ref/pin the split will fail, then skip migration, leave without page restore, then cpu/gpu keep retrying.
If the prange was split and no longer covers the PMD, do not call split_folio(): it is not exported to modules. Walk only this prange and let migrate_vma_setup() split the PMD inside mm.
If the fault address has PTE mapping memory use prange->granularity to decide migration size.
--- drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 41 ++++++++++- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 86 +++++++++++++++++++++++- drivers/gpu/drm/amd/amdkfd/kfd_svm.h | 12 ++++ 3 files changed, 135 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +38 -3
@@ -1097,9 +1097,12 @@ static vm_fault_t svm_migrate_to_ram(struct vm_fault *vmf)struct svm_range *prange;struct kfd_process *p;struct mm_struct *mm; +struct folio *folio; +unsigned int order;int r =0 ; - svm_bo = vmf->page->zone_device_data; + folio =page_folio (vmf->page); + svm_bo =folio_zone_device_data (folio);if (!svm_bo) {pr_debug ("failed get device page at addr 0x%lx\n" , addr);return VM_FAULT_SIGBUS ;
@@ -1142,8 +1145,40 @@ static vm_fault_t svm_migrate_to_ram(struct vm_fault *vmf)if (!prange->actual_loc) goto out_unlock_prange; -/* Align migration range start and size to granularity size */ - size =1UL << prange->granularity; + order =folio_order (folio); + +/* +* A PMD device-private fault always passes the folio head in +* vmf->page. If this prange still covers the whole folio, migrate +* at folio order so HMM can collect MIGRATE_PFN_COMPOUND. +* +* Do not want migrate partial THP device memory. If do HMM will split +* THP folio. If the compound folio has extra ref/pin the split will fail, +* then skip migration, leave without page restore, then cpu keep retry. +* +* If the prange was split and no longer covers the PMD, do not call +* split_folio() here: it is not exported to modules. Walk only this +* prange and let migrate_vma_setup() split the PMD inside mm. +* +* If the fault address has PTE mapping device-private memory use +* prange->granularity to decide migration size. +*/ +if (order && !vmf->pte) { +unsigned long folio_start =ALIGN_DOWN (addr,1UL << order); +unsigned long folio_last = folio_start + (1UL << order) -1 ; + +if (prange->start > folio_start || prange->last < folio_last) { +pr_debug ("partial prange on device THP svms 0x%p prange [0x%lx 0x%lx] folio [0x%lx 0x%lx]\n" , + prange->svms, prange->start, prange->last, + folio_start, folio_last); + size =1UL << prange->granularity; + }else { + size =1UL <<max (prange->granularity, order); + } + }else { + size =1UL << prange->granularity; + } + start =max (ALIGN_DOWN (addr, size), prange->start); last =min (ALIGN (addr +1 , size) -1 , prange->last);
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +85 -1
105 lines not on this page · raw message
drivers/gpu/drm/amd/amdkfd/kfd_svm.h +12 -0
@@ -231,6 +231,13 @@ void svm_range_bo_unref_async(struct svm_range_bo *svm_bo);void svm_range_set_max_pages (struct amdgpu_device *adev);int svm_range_switch_xnack_reserve_mem (struct kfd_process *p,bool xnack_enabled); +/* check adev has device private zone memory */ +static inline bool svm_is_private_zone (struct amdgpu_device *adev) +{ +struct amdgpu_kfd_dev *kfddev = &adev->kfd; + +return (kfddev->pgmap.type ==MEMORY_DEVICE_PRIVATE ); +}#else
@@ -294,6 +301,11 @@ static inline void svm_range_set_max_pages(struct amdgpu_device *adev) { } +static inline bool svm_is_private_zone (struct amdgpu_device *adev) +{ +return false; +} +#define KFD_IS_SVM_API_SUPPORTED (dev) false#endif /* IS_ENABLED(CONFIG_HSA_AMD_SVM) */
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 2/3] drm/amdkfd: Do not migrate partial THP memory at CPU/GPU page fault handler
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/amdkfd: infinite CPU fault loop on split prange in svm_migrate_to_ram() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/amdkfd: infinite CPU fault loop on split prange in svm_migrate_to_ram()
--
commit b149598769982ad1feb63c501a92a881118fdebd
Author: Xiaogang Chen <[email protected]>
drm/amdkfd: Do not migrate partial THP memory at CPU/GPU page fault handler
This commit adjusts the migration range size on CPU and GPU page faults so that PMD-mapped THP folios fully covered by an svm_range are migrated at folio order, while split ranges fall back to prange->granularity.
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +0
-0
[ ... ]
Quoted 32 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +26
-2@@ -1142,8 +1145,40 @@ static vm_fault_t svm_migrate_to_ram(struct vm_fault *vmf)if (!prange->actual_loc) goto out_unlock_prange; -/* Align migration range start and size to granularity size */ - size =1UL << prange->granularity; + order =folio_order (folio); + +/* +* A PMD device-private fault always passes the folio head in +* vmf->page. If this prange still covers the whole folio, migrate +* at folio order so HMM can collect MIGRATE_PFN_COMPOUND. +* +* Do not want migrate partial THP device memory. If do HMM will split +* THP folio. If the compound folio has extra ref/pin the split will fail, +* then skip migration, leave without page restore, then cpu keep retry. +* +* If the prange was split and no longer covers the PMD, do not call +* split_folio() here: it is not exported to modules. Walk only this +* prange and let migrate_vma_setup() split the PMD inside mm. +* +* If the fault address has PTE mapping device-private memory use +* prange->granularity to decide migration size. +*/ +if (order && !vmf->pte) { +unsigned long folio_start =ALIGN_DOWN (addr,1UL << order); +unsigned long folio_last = folio_start + (1UL << order) -1 ; + +if (prange->start > folio_start || prange->last < folio_last) { +pr_debug ("partial prange on device THP svms 0x%p prange [0x%lx 0x%lx] folio [0x%lx 0x%lx]\n" , + prange->svms, prange->start, prange->last, + folio_start, folio_last);
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c static vm_fault_t svm_migrate_to_ram(struct vm_fault *vmf) · line 1183
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +8
-0+ size =1UL << prange->granularity; + }else { + size =1UL <<max (prange->granularity, order); + } + }else { + size =1UL << prange->granularity; + } + start =max (ALIGN_DOWN (addr, size), prange->start); last =min (ALIGN (addr +1 , size) -1 , prange->last);
[Severity: High]
Can a CPU fault on a PMD-mapped device-private folio enter an infinite fault loop when prange has been split to a sub-PMD range?
On a PMD device-private fault, do_huge_pmd_device_private() holds an extra reference on the fault folio across svm_migrate_to_ram(). When [start, last] is clamped to a split prange smaller than the PMD, migrate_vma_collect_huge_pmd() takes a second reference and falls back to split_folio():
mm/migrate_device.c:migrate_vma_collect_huge_pmd() {
...
folio_get(folio);
...
fallback:
spin_unlock(ptl);
if (!folio_test_large(folio))
goto done;
ret = split_folio(folio);
if (fault_folio != folio)
folio_unlock(folio);
folio_put(folio);
if (ret)
return migrate_vma_collect_skip(start, end, walk);
...
}Because two extra references are held on the folio, unmap_folio() inside split_folio() fails the reference count check with -EAGAIN before try_to_migrate() can split the PMD:
mm/huge_memory.c:unmap_folio() {
...
/* Racy check if we can split the page, before we split PMDs */
if (folio_expected_ref_count(folio) != folio_ref_count(folio) - 1)
return -EAGAIN;
...
}migrate_vma_collect_huge_pmd() then skips the PMD without splitting it or collecting any pages, and svm_migrate_to_ram() returns 0 with the device-private PMD entry still in place, causing the CPU to fault on the same instruction indefinitely.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v5 3/3 drm/amdkfd: Enable HMM device-private THP migration in KFD SVM driver
In reply to Xiaogang.ChenFrom: Xiaogang Chen <[email protected]>
HMM migrates a PMD-mapped THP as one compound folio when migrate_vma_setup() is passed MIGRATE_VMA_SELECT_COMPOUND.
Without that flag the collect path splits the source THP to 4K pages. If split_folio() fails because of extra refs or pins, those pages are skipped.
Set SELECT_COMPOUND for device-private ranges whose migrate window is at least HPAGE_PMD_NR pages. The RAM<->VRAM copy path follows MIGRATE_PFN_COMPOUND in src[] (HPAGE_PMD_ORDER device folio, THP DMA) and keeps the existing 4K path otherwise.
Coherent zone-device memory is unchanged.
--- drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 582 +++++++++++++++++++++-- 1 file changed, 530 insertions(+), 52 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +530 -52
@@ -340,6 +340,153 @@ static unsigned long svm_migrate_successful_pages(struct migrate_vma *migrate)return mpages; } +/* +* A reused svm_bo VRAM page may still be held by someone else, ex: +* (do_swap_page -> svm_migrate_to_ram). Migrating to VRAM re-initializes +* the page and resets its refcount, so the other's put_page() would +* free it mid-migration. Report whether any of the @nr VRAM pages starting +* at page offset @m of @cursor is still referenced. @cursor is a copy, so +* the range may cross VRAM blocks without moving the caller's cursor. +* +* Check the folio, not the page: a tail page of a live device THP always +* has a zero refcount while its folio is held. +*/ +static bool +svm_migrate_vram_busy (struct amdgpu_device *adev, +struct amdgpu_res_cursor cursor,u64 m,u64 nr) +{ +while (nr) { +u64 chunk = cursor.size >>PAGE_SHIFT ; +struct page *page; + +if (m >= chunk) { +if (cursor.remaining <= cursor.size) +return false; +amdgpu_res_next (&cursor, cursor.size); + m -= chunk; +continue ; + } + + page =pfn_to_page (svm_migrate_addr_to_pfn (adev, + cursor.start + (m <<PAGE_SHIFT ))); +if (folio_ref_count (page_folio (page))) +return true; + m++; + nr--; + } + +return false; +} + +/* Move the VRAM position (@cursor, page offset @m) forward by @nr pages. */ +static void +svm_migrate_cursor_skip (struct amdgpu_res_cursor *cursor,u64 *m,u64 nr) +{ + *m += nr; +while (*m >= (cursor->size >>PAGE_SHIFT ) && + cursor->remaining > cursor->size) { + *m -= cursor->size >>PAGE_SHIFT ; +amdgpu_res_next (cursor, cursor->size); + } +} + +/* +* Place one MIGRATE_PFN_COMPOUND source into 4K device pages. +* +* A device THP needs one PMD-aligned VRAM run. This folio does not have +* that run: its device pages continue into the next VRAM block. Publish +* one dst pfn per page so migrate_vma_pages() can split the source, and +* copy each contiguous VRAM run separately. SDMA copies a contiguous +* destination; a single 2MB copy cannot cross a block boundary. +* +* @m_in is the page offset in the current block. On return *@m_out is the +* offset of the next page, or the block size when the folio ends on a +* block boundary. +*/ +static int +svm_migrate_compound_to_4k_vram (struct amdgpu_device *adev,struct device *dev, +struct svm_range *prange, +struct migrate_vma *migrate, +struct dma_fence **mfence, +struct amdgpu_res_cursor *cursor, +dma_addr_t *src,u64 *dst,unsigned long idx, +u64 m_in,u64 *m_out) +{ +struct page *spage =migrate_pfn_to_page (migrate->src[idx]); +unsigned long done =0 ; +bool mapped = false; +u64 m = m_in; +int r =0 ; + +if (spage && !is_zone_device_page (spage)) { +unsigned long l; + + src[idx] =dma_map_page (dev, spage,0 ,PAGE_SIZE *SVM_HPAGE_PMD_NR , +DMA_BIDIRECTIONAL ); + r =dma_mapping_error (dev, src[idx]); +if (r) { +dev_err (dev,"%s: fail %d dma_map_page\n" , __func__, r); + src[idx] =0 ; +return r; + } + +for (l =1 ; l <SVM_HPAGE_PMD_NR ; l++) + src[idx + l] = src[idx] + l *PAGE_SIZE ; + mapped = true; + } + +while (done <SVM_HPAGE_PMD_NR ) { +unsigned long chunk_pages = cursor->size >>PAGE_SHIFT ; +unsigned long room = chunk_pages > m ? chunk_pages - m :0 ; + unsigned long step = min_t(unsigned long, SVM_HPAGE_PMD_NR - done, room); +unsigned long l; + +if (!step) { + r = -ENOSPC ; +break ; + } + +for (l =0 ; l < step; l++) { +u64 addr = cursor->start + ((m + l) <<PAGE_SHIFT ); +unsigned long pfn =svm_migrate_addr_to_pfn (adev, addr); + + dst[idx + done + l] = addr; +svm_migrate_get_vram_page (prange, pfn,0 ); + migrate->dst[idx + done + l] =migrate_pfn (pfn); + } + +if (mapped) { + r =svm_migrate_copy_memory_gart (adev, src + idx + done, + dst + idx + done, step, +FROM_RAM_TO_VRAM , mfence); +if (r) +break ; + } + + done += step; + m += step; +if (done <SVM_HPAGE_PMD_NR && m == chunk_pages) { +amdgpu_res_next (cursor, m <<PAGE_SHIFT ); + m =0 ; +if (!cursor->remaining) { + r = -ENOSPC ; +break ; + } + } + } + +if (mapped) { +unsigned long l; + + src[idx] |=SVM_RANGE_DMA_THP ; +for (l =1 ; l <SVM_HPAGE_PMD_NR ; l++) + src[idx + l] =0 ; + } + + *m_out = m; +return r; +} + static int svm_migrate_copy_to_vram(struct kfd_node *node, struct svm_range *prange, struct migrate_vma *migrate, struct dma_fence **mfence,
@@ -352,7 +499,7 @@ svm_migrate_copy_to_vram(struct kfd_node *node, struct svm_range *prange,u64 mpages =0 ;dma_addr_t *src;u64 *dst; -u64 i, j; +u64 i, j, k, l, m;int r =0 ; pr_debug("svms 0x%p [0x%lx 0x%lx 0x%llx]\n", prange->svms, prange->start,
@@ -363,69 +510,251 @@ svm_migrate_copy_to_vram(struct kfd_node *node, struct svm_range *prange,amdgpu_res_first (prange->ttm_res, ttm_res_offset, npages <<PAGE_SHIFT , &cursor); -for (i = j =0 ; (i < npages) && (mpages < migrate->cpages); i++) { -struct page *spage =migrate_pfn_to_page (migrate->src[i]); +for (i = j = m =0 ; (i < npages) && (mpages < migrate->cpages);) { +struct page *spage; +unsigned long cur_dst_pfn; +bool thp_dst = false; +bool busy = false; + + cur_dst_pfn =svm_migrate_addr_to_pfn (adev, + cursor.start + (m <<PAGE_SHIFT )); + +/* +* MIGRATE_PFN_COMPOUND is one slot for the whole folio. +* src[i+1 .. i+SVM_HPAGE_PMD_NR-1] stay zero and cpages counts +* the folio once. migrate_vma_pages() splits that folio when +* dst[i] is not compound and then reads every tail dst slot. +* Those slots must be real pages. A device THP is placed only +* at a PMD-aligned VRAM pfn. Any other compound source is +* migrated as 4K device pages, including when the folio +* crosses a VRAM block. +*/ +if (migrate->src[i] &MIGRATE_PFN_COMPOUND ) { +unsigned long room = (cursor.size >>PAGE_SHIFT ) - m; +bool fits = room >=SVM_HPAGE_PMD_NR ; -if (migrate->src[i] &MIGRATE_PFN_MIGRATE ) { - dst[i] = cursor.start + (j <<PAGE_SHIFT ); - migrate->dst[i] =svm_migrate_addr_to_pfn (adev, dst[i]);/* -* A reused svm_bo page may still be held by a CPU fault. +* HMM collects a compound entry only for a PMD fully +* inside [start, end), so the folio cannot run past +* npages. If it ever does, fail the whole window: the +* error path releases every published VRAM page, +* including the 4K pages still pending in j. +*/ +if (WARN_ON_ONCE (i +SVM_HPAGE_PMD_NR > npages)) { + r = -EINVAL ; + goto out_free_vram_pages; + } + +/* +* Every path below re-initializes all SVM_HPAGE_PMD_NR +* VRAM pages, and a split source needs a page in every +* dst slot, so single pages cannot be skipped. If any +* target page is still referenced (see +* svm_migrate_vram_busy()), leave the whole folio in +* system memory. Flush the pending run first: the next +* VRAM page is no longer contiguous with it. +*/ +if (svm_migrate_vram_busy (adev, cursor, m, +SVM_HPAGE_PMD_NR )) { +if (j) { + r =svm_migrate_copy_memory_gart ( + adev, src + i - j, dst + i - j, + j,FROM_RAM_TO_VRAM , mfence); +if (r) + goto out_free_vram_pages; + j =0 ; + } +svm_migrate_cursor_skip (&cursor, &m, +SVM_HPAGE_PMD_NR ); + i +=SVM_HPAGE_PMD_NR ; +continue ; + } + +if (!fits) { +u64 m_next; + +if (j) { + r =svm_migrate_copy_memory_gart ( + adev, src + i - j, dst + i - j, + j,FROM_RAM_TO_VRAM , mfence); +if (r) + goto out_free_vram_pages; + j =0 ; + } + + r =svm_migrate_compound_to_4k_vram (adev, dev, + prange, + migrate, + mfence, + &cursor, + src, dst, + i, m, + &m_next); +if (r) + goto out_free_vram_pages; + + mpages++; + i +=SVM_HPAGE_PMD_NR ; + m = m_next; +if (cursor.remaining && + m == (cursor.size >>PAGE_SHIFT )) { +amdgpu_res_next (&cursor, m <<PAGE_SHIFT ); + m =0 ; + } +continue ; + } + + k =SVM_HPAGE_PMD_NR ; + thp_dst =IS_ALIGNED (cur_dst_pfn,SVM_HPAGE_PMD_NR ); + }else { + k =1 ; + } + +if (thp_dst) { + dst[i] = cursor.start + (m <<PAGE_SHIFT ); +svm_migrate_get_vram_page (prange, cur_dst_pfn, +SVM_HPAGE_PMD_ORDER ); + + migrate->dst[i] =migrate_pfn (cur_dst_pfn) | +MIGRATE_PFN_COMPOUND ; + +for (l =1 ; l < k; l++) + migrate->dst[i + l] =0 ; + + mpages++; + }else if (k >1 ) { +for (l =0 ; l < k; l++) { +unsigned long pfn = cur_dst_pfn + l; + + dst[i + l] = cursor.start + ((m + l) <<PAGE_SHIFT ); +svm_migrate_get_vram_page (prange, pfn,0 ); + migrate->dst[i + l] =migrate_pfn (pfn); + } + mpages++; + }else if (migrate->src[i] &MIGRATE_PFN_MIGRATE ) { +/* +* A reused svm_bo page may still be held by someone else. * Reinitializing it would free it under this migration, * so leave this page in system memory. */ -if (page_ref_count (pfn_to_page (migrate->dst[i]))) { - dst[i] =0 ; - migrate->dst[i] =0 ; - spage =NULL ; - }else { -svm_migrate_get_vram_page (prange, migrate->dst[i],0 ); - migrate->dst[i] =migrate_pfn (migrate->dst[i]); + busy =svm_migrate_vram_busy (adev, cursor, m,1 ); +if (!busy) { + dst[i] = cursor.start + (m <<PAGE_SHIFT ); +svm_migrate_get_vram_page (prange, cur_dst_pfn,0 ); + migrate->dst[i] =migrate_pfn (cur_dst_pfn); mpages++; } } -if (spage && !is_zone_device_page (spage)) { - src[i] =dma_map_page (dev, spage,0 ,PAGE_SIZE , -DMA_BIDIRECTIONAL ); - r =dma_mapping_error (dev, src[i]); -if (r) { - src[i] =0 ; -dev_err (dev,"%s: fail %d dma_map_page\n" , - __func__, r); - goto out_free_vram_pages; + +if (k >1 ) { +if (j) { +/* migrate previous accumulated pages */ + r =svm_migrate_copy_memory_gart ( + adev, src + i - j, + dst + i - j, j, +FROM_RAM_TO_VRAM , + mfence); + +if (r) + goto out_free_vram_pages; + + j =0 ; + } + +/* for THP check if the first src page is valid +* if not valid skip following SVM_HPAGE_PMD_NR - 1 pages +*/ + spage =migrate_pfn_to_page (migrate->src[i]); +if (spage && !is_zone_device_page (spage)) { +/* dma_map continuous SVM_HPAGE_PMD_NR sys ram pages */ + src[i] =dma_map_page (dev, spage,0 ,PAGE_SIZE *SVM_HPAGE_PMD_NR , +DMA_BIDIRECTIONAL ); + + r =dma_mapping_error (dev, src[i]); +if (r) { +dev_err (dev,"%s: fail %d dma_map_page\n" , + __func__, r); + goto out_free_vram_pages; + } + +/* get dma address for following SVM_HPAGE_PMD_NR-1 pages +* since src pages are continuous their dma addresses +* are continuous too. +*/ +for (l =1 ; l < k; l++) + src[i + l] = src[i] + l*PAGE_SIZE ; + +/* migrate the SVM_HPAGE_PMD_NR pages above */ + r =svm_migrate_copy_memory_gart ( + adev, src + i, + dst + i,SVM_HPAGE_PMD_NR , +FROM_RAM_TO_VRAM , + mfence); + +/* mark head page dma mapping as THP, tail pages dma addr +* are set to 0 for following dma_unmap +*/ + src[i] |=SVM_RANGE_DMA_THP ; +for (l =1 ; l < k; l++) + src[i + l] =0 ; + +if (r) + goto out_free_vram_pages; + + j =0 ; } }else { -if (j) { +/* +* single normal page case; a busy VRAM page is not +* migrated, so treat it like a page with no source +*/ + spage = busy ?NULL :migrate_pfn_to_page (migrate->src[i]); +if (spage && !is_zone_device_page (spage)) { + src[i] =dma_map_page (dev, spage,0 ,PAGE_SIZE , +DMA_BIDIRECTIONAL ); + + r =dma_mapping_error (dev, src[i]); + +if (r) { +dev_err (dev,"%s: fail %d dma_map_page\n" , + __func__, r); + goto out_free_vram_pages; + } + j +=1 ; + + }else if (j) { r =svm_migrate_copy_memory_gart ( adev, src + i - j, dst + i - j, j,FROM_RAM_TO_VRAM , mfence); +if (r) goto out_free_vram_pages; -amdgpu_res_next (&cursor, (j +1 ) <<PAGE_SHIFT ); + j =0 ; - }else { -amdgpu_res_next (&cursor,PAGE_SIZE ); } -continue ; } -pr_debug_ratelimited ("dma mapping src to 0x%llx, pfn 0x%lx\n" , - src[i] >>PAGE_SHIFT ,page_to_pfn (spage)); +pr_debug_ratelimited ("dma mapping %lld pages, src to 0x%llx, pfn 0x%lx\n" , + k, src[i] >>PAGE_SHIFT , migrate->src[i] >>MIGRATE_PFN_SHIFT ); + i += k; + m += k; + +if (m >= (cursor.size >>PAGE_SHIFT )) { +if (j >0 ) { + r =svm_migrate_copy_memory_gart (adev, src + i - j, + dst + i - j, j, +FROM_RAM_TO_VRAM , + mfence); +if (r) + goto out_free_vram_pages; + } + +amdgpu_res_next (&cursor, m*PAGE_SIZE ); -/* accumulated j + 1 pages reach end of current drm_buddy_block */ -if (j +1 >= (cursor.size >>PAGE_SHIFT )) { - r =svm_migrate_copy_memory_gart (adev, src + i - j, - dst + i - j, j +1 , -FROM_RAM_TO_VRAM , - mfence); -if (r) - goto out_free_vram_pages; -amdgpu_res_next (&cursor, (j +1 ) *PAGE_SIZE ); j =0 ; - }else { - j++; + m =0 ; } }
@@ -436,12 +765,16 @@ svm_migrate_copy_to_vram(struct kfd_node *node, struct svm_range *prange, out_free_vram_pages:if (r) {pr_debug ("failed %d to copy memory to vram\n" , r); -for (i =0 ; i < npages && mpages; i++) { +/* +* A split compound source allocates one dst page per tail +* slot while mpages still counts the folio once. Release +* every page that was published. +*/ +for (i =0 ; i < npages; i++) {if (!dst[i])continue ;svm_migrate_put_vram_page (adev, dst[i]); migrate->dst[i] =0 ; - mpages--; } }
@@ -514,6 +847,7 @@ svm_migrate_vma_to_vram(struct kfd_node *node, struct svm_range *prange,struct kfd_process_device *pdd;struct dma_fence *mfence =NULL ;struct migrate_vma migrate = {0 }; +bool is_private_device = false;unsigned long cpages =0 ;unsigned long mpages =0 ;dma_addr_t *scratch;
@@ -526,11 +860,18 @@ svm_migrate_vma_to_vram(struct kfd_node *node, struct svm_range *prange,if (svm_range_window_resident (node, prange, start, end))return 0 ; + is_private_device =svm_is_private_zone (adev); +memset (&migrate,0 ,sizeof (migrate)); migrate.vma = vma; migrate.start = start; migrate.end = end; migrate.flags =MIGRATE_VMA_SELECT_SYSTEM ; + +if (SVM_THP_ENABLED && is_private_device && + ((end - start) >>PAGE_SHIFT ) >=SVM_HPAGE_PMD_NR ) + migrate.flags = migrate.flags |MIGRATE_VMA_SELECT_COMPOUND ; + migrate.pgmap_owner =SVM_ADEV_PGMAP_OWNER (adev); buf = kvcalloc(npages,
@@ -711,6 +1052,57 @@ static void svm_migrate_folio_free(struct folio *folio) } } +/* +* Migrate one compound device folio at @idx to 4K system pages, used when +* no THP destination can be allocated. +* +* When dst[idx] is not compound, migrate_vma_pages() splits the source +* folio and then migrates every one of its SVM_HPAGE_PMD_NR pages, reading +* each tail dst slot without a NULL check. So every slot must get a real +* page here, not just the head. +* +* The device folio is contiguous in VRAM, so one copy covers all pages. +* On error, pages already published in migrate->dst[] and their DMA +* mappings in @dst are released by the caller's error path. +*/ +static int +svm_migrate_compound_to_4k_ram (struct amdgpu_device *adev, +struct migrate_vma *migrate, +struct dma_fence **mfence,dma_addr_t *dst, +u64 *src,u64 idx,u64 addr) +{ +struct device *dev = adev->dev; +unsigned long l; +int r; + +for (l =0 ; l <SVM_HPAGE_PMD_NR ; l++) { +struct page *dpage; + + dpage =svm_migrate_get_sys_page (migrate->vma, + addr + l *PAGE_SIZE ,0 ); +if (!dpage) +return -ENOMEM ; + + dst[idx + l] =dma_map_page (dev, dpage,0 ,PAGE_SIZE , +DMA_BIDIRECTIONAL ); + r =dma_mapping_error (dev, dst[idx + l]); +if (r) { +dev_err (dev,"%s: fail %d dma_map_page\n" , __func__, r); + dst[idx + l] =0 ; +unlock_page (dpage); +put_page (dpage); +return r; + } + + migrate->dst[idx + l] =migrate_pfn (page_to_pfn (dpage)); + src[idx + l] = src[idx] + l *PAGE_SIZE ; + } + +return svm_migrate_copy_memory_gart (adev, dst + idx, src + idx, +SVM_HPAGE_PMD_NR ,FROM_VRAM_TO_RAM , + mfence); +} + static int svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange, struct migrate_vma *migrate, struct dma_fence **mfence,
@@ -724,6 +1116,7 @@ svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange,u64 i =0 , j;u64 addr;int r =0 ; +u64 l, k;pr_debug ("svms 0x%p [0x%lx 0x%lx]\n" , prange->svms, prange->start, prange->last);
@@ -732,8 +1125,7 @@ svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange, src = (u64 *)(scratch + npages); dst = scratch; - -for (i =0 , j =0 ; i < npages; i++, addr +=PAGE_SIZE ) { +for (i =0 , j =0 , k =1 ; i < npages; i += k, addr += k*PAGE_SIZE , k =1 ) {struct page *spage; spage =migrate_pfn_to_page (migrate->src[i]);
@@ -749,6 +1141,7 @@ svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange, goto out_oom; j =0 ; } +continue ; } src[i] =svm_migrate_addr (adev, spage);
@@ -762,7 +1155,58 @@ svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange, j =0 ; } - dpage =svm_migrate_get_sys_page (migrate->vma, addr,0 ); +if (migrate->src[i] &MIGRATE_PFN_COMPOUND ) { +/* +* Flush accumulated 4K pages before mapping the 2MB +* destination, so a flush failure never leaves a 2MB +* dst[i] mapping that is not yet marked SVM_RANGE_DMA_THP. +*/ +if (j) { + r =svm_migrate_copy_memory_gart (adev, dst + i - j, + src + i - j, j, +FROM_VRAM_TO_RAM , + mfence); +if (r) + goto out_oom; + j =0 ; + } + +/* +* HMM collects a compound entry only for a PMD fully +* inside [start, end), so the folio cannot run past +* npages. +*/ +if (WARN_ON_ONCE (i +SVM_HPAGE_PMD_NR > npages)) { + r = -EINVAL ; + goto out_oom; + } + + k =SVM_HPAGE_PMD_NR ; + dpage =NULL ; +if (IS_ALIGNED (page_to_pfn (spage),SVM_HPAGE_PMD_NR ) && +IS_ALIGNED (addr,SVM_HPAGE_PMD_SIZE )) + dpage =svm_migrate_get_sys_page (migrate->vma, + addr,SVM_HPAGE_PMD_ORDER ); + +/* +* No THP destination (allocation failed or THP is +* disabled for the VMA): migrate the whole folio as +* SVM_HPAGE_PMD_NR 4K pages so migrate_vma_pages() finds +* a page in every slot after it splits the source. +*/ +if (!dpage) { + r =svm_migrate_compound_to_4k_ram (adev, migrate, + mfence, dst, + src, i, addr); +if (r) + goto out_oom; +continue ; + } + }else { + k =1 ; + dpage =svm_migrate_get_sys_page (migrate->vma, addr,0 ); + } +if (!dpage) {pr_debug ("failed get page svms 0x%p [0x%lx 0x%lx]\n" , prange->svms, prange->start, prange->last);
@@ -770,7 +1214,7 @@ svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange, goto out_oom; } - dst[i] =dma_map_page (dev, dpage,0 ,PAGE_SIZE ,DMA_BIDIRECTIONAL ); + dst[i] =dma_map_page (dev, dpage,0 ,PAGE_SIZE *k,DMA_BIDIRECTIONAL ); r =dma_mapping_error (dev, dst[i]);if (r) {dev_err (adev->dev,"%s: fail %d dma_map_page\n" , __func__, r);
@@ -778,13 +1222,40 @@ svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange, goto out_oom; } -pr_debug_ratelimited ("dma mapping dst to 0x%llx, pfn 0x%lx\n" , - dst[i] >>PAGE_SHIFT ,page_to_pfn (dpage)); - migrate->dst[i] =migrate_pfn (page_to_pfn (dpage)); - dpage =NULL ; - j++; + +if (k >1 ) { + migrate->dst[i] |=MIGRATE_PFN_COMPOUND ; + +for (l =1 ; l < k; l++) { + + src[i + l] = src[i] + l*PAGE_SIZE ; + dst[i + l] = dst[i] + l*PAGE_SIZE ; + migrate->dst[i + l] =0 ; + } + +/* migrate the SVM_HPAGE_PMD_NR pages above +* the migration is for 2MB THP +*/ + r =svm_migrate_copy_memory_gart ( + adev, dst + i, + src + i,SVM_HPAGE_PMD_NR , +FROM_VRAM_TO_RAM , + mfence); + +/* mark head page dma mapping as THP, tail pages dma addr +* are set to 0 for following dma_unmap +*/ + dst[i] |=SVM_RANGE_DMA_THP ; +for (l =1 ; l < k; l++) + dst[i + l] =0 ; + +if (r) + goto out_oom; + + }else + j++; }if (j >0 )
@@ -849,6 +1320,7 @@ svm_migrate_vma_to_ram(struct kfd_node *node, struct svm_range *prange,unsigned long cpages =0 ;unsigned long mpages =0 ;struct amdgpu_device *adev = node->adev; +bool is_private_device = false;struct kfd_process_device *pdd;struct dma_fence *mfence =NULL ;struct migrate_vma migrate = {0 };
@@ -856,6 +1328,8 @@ svm_migrate_vma_to_ram(struct kfd_node *node, struct svm_range *prange,void *buf;int r = -ENOMEM ; + is_private_device =svm_is_private_zone (adev); +memset (&migrate,0 ,sizeof (migrate)); migrate.vma = vma; migrate.start = start;
@@ -866,6 +1340,10 @@ svm_migrate_vma_to_ram(struct kfd_node *node, struct svm_range *prange,else migrate.flags =MIGRATE_VMA_SELECT_DEVICE_PRIVATE ; +if (SVM_THP_ENABLED && is_private_device && + ((end - start) >>PAGE_SHIFT ) >=SVM_HPAGE_PMD_NR ) + migrate.flags = migrate.flags |MIGRATE_VMA_SELECT_COMPOUND ; + buf =kvcalloc (npages,2 *sizeof (*migrate.src) +sizeof (u64) +sizeof (dma_addr_t),GFP_KERNEL );
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 3/3] drm/amdkfd: Enable HMM device-private THP migration in KFD SVM driver
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] drm/amdkfd: broken out_oom cleanup and THP DMA unmap in svm_migrate_copy_to_ram() - [High]…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] drm/amdkfd: broken out_oom cleanup and THP DMA unmap in svm_migrate_copy_to_ram()
- [High] drm/amdkfd: svm_migrate_vram_busy() races with compound folio teardown
- [Medium] drm/amdkfd: uninitialized tail VRAM offsets in svm_migrate_copy_to_vram()
- [Medium] drm/amdkfd: CPU PMD fault 4K fallback in svm_migrate_copy_to_ram() fails folio split
--
commit 0bbf4e6ec6f1a6c7fb1fece90e6a6372e1d80249
Author: Xiaogang Chen <[email protected]>
drm/amdkfd: Enable HMM device-private THP migration in KFD SVM driver
This commit enables compound THP migration for device-private SVM ranges by setting MIGRATE_VMA_SELECT_COMPOUND when the migration window is at least SVM_HPAGE_PMD_NR pages and handling MIGRATE_PFN_COMPOUND entries in the RAM-to-VRAM and VRAM-to-RAM copy paths.
Quoted 35 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +27
-0@@ -340,6 +340,153 @@ static unsigned long svm_migrate_successful_pages(struct migrate_vma *migrate)return mpages; } +/* +* A reused svm_bo VRAM page may still be held by someone else, ex: +* (do_swap_page -> svm_migrate_to_ram). Migrating to VRAM re-initializes +* the page and resets its refcount, so the other's put_page() would +* free it mid-migration. Report whether any of the @nr VRAM pages starting +* at page offset @m of @cursor is still referenced. @cursor is a copy, so +* the range may cross VRAM blocks without moving the caller's cursor. +* +* Check the folio, not the page: a tail page of a live device THP always +* has a zero refcount while its folio is held. +*/ +static bool +svm_migrate_vram_busy (struct amdgpu_device *adev, +struct amdgpu_res_cursor cursor,u64 m,u64 nr) +{ +while (nr) { +u64 chunk = cursor.size >>PAGE_SHIFT ; +struct page *page; + +if (m >= chunk) { +if (cursor.remaining <= cursor.size) +return false; +amdgpu_res_next (&cursor, cursor.size); + m -= chunk; +continue ; + } +
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c static unsigned long svm_migrate_successful_pages(struct migrate_vma *migrate) · line 379
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +10
-0+ page =pfn_to_page (svm_migrate_addr_to_pfn (adev, + cursor.start + (m <<PAGE_SHIFT ))); +if (folio_ref_count (page_folio (page))) +return true; + m++; + nr--; + } + +return false; +}
[Severity: High]
Can svm_migrate_vram_busy() return false while a previous compound device folio on the same svm_bo is still being torn down in svm_migrate_folio_free()?
When a CPU fault finishes svm_migrate_to_ram() and drops its reference outside prange->migrate_mutex, folio_put_testzero() zeroes the folio refcount before svm_migrate_folio_free() clears the compound pages and drops the svm_bo reference:
CPU0 CPU1do_huge_pmd_device_private()
svm_migrate_to_ram()
...
mutex_unlock(&prange->migrate_mutex)
folio_put(folio)
folio_put_testzero(folio)
/* refcount is now 0 */
svm_migrate_to_vram()
/* reuses prange->svm_bo */
svm_migrate_copy_to_vram()
svm_migrate_vram_busy() -> false
svm_migrate_get_vram_page(..., order)
zone_device_folio_init()
free_zone_device_folio()
svm_migrate_folio_free()
svm_migrate_clear_compound_pages()
/* tail pages have page_count == 0 */
new_folio->pgmap = pgmap; <-- corrupts live compound tail pagesBecause svm_migrate_clear_compound_pages() only skips pages with a non-zero page_count(page), and all tail pages of the newly initialized compound folio have a page count of 0, the teardown loop wipes the new folio's compound order, _nr_pages, and tail page compound_head pointers.
[ ... ]
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +0
-0@@ -363,69 +510,251 @@ svm_migrate_copy_to_vram(struct kfd_node *node, struct svm_range *prange,amdgpu_res_first (prange->ttm_res, ttm_res_offset, npages <<PAGE_SHIFT , &cursor);
[ ... ]
Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +12
-0+if (thp_dst) { + dst[i] = cursor.start + (m <<PAGE_SHIFT ); +svm_migrate_get_vram_page (prange, cur_dst_pfn, +SVM_HPAGE_PMD_ORDER ); + + migrate->dst[i] =migrate_pfn (cur_dst_pfn) | +MIGRATE_PFN_COMPOUND ; + +for (l =1 ; l < k; l++) + migrate->dst[i + l] =0 ; + + mpages++;
[ ... ]
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c svm_migrate_copy_to_vram(struct kfd_node *node, struct svm_range *prange, · line 692
Quoted 13 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +13
-0+/* get dma address for following SVM_HPAGE_PMD_NR-1 pages +* since src pages are continuous their dma addresses +* are continuous too. +*/ +for (l =1 ; l < k; l++) + src[i + l] = src[i] + l*PAGE_SIZE ; + +/* migrate the SVM_HPAGE_PMD_NR pages above */ + r =svm_migrate_copy_memory_gart ( + adev, src + i, + dst + i,SVM_HPAGE_PMD_NR , +FROM_RAM_TO_VRAM , + mfence);
[Severity: Medium]
Does the thp_dst path leave dst[i + 1 .. i + k - 1] as 0 before calling svm_migrate_copy_memory_gart()?
svm_migrate_copy_memory_gart() splits transfers larger than GTT_MAX_PAGES (4 MiB) and advances vram += size on each chunk, dereferencing *vram for the destination VRAM offset. On architectures supported by CONFIG_HSA_AMD where HPAGE_PMD_SIZE exceeds 4 MiB (such as arm64, ppc64, or loongarch with larger base page sizes), every chunk after the first 4 MiB reads *vram == 0 and DMA copies the remainder of the THP to VRAM offset 0.
[ ... ]
Quoted 1 line
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +0
-0@@ -762,7 +1155,58 @@ svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange,
[ ... ]
Quoted 61 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +39
-6+/* +* No THP destination (allocation failed or THP is +* disabled for the VMA): migrate the whole folio as +* SVM_HPAGE_PMD_NR 4K pages so migrate_vma_pages() finds +* a page in every slot after it splits the source. +*/ +if (!dpage) { + r =svm_migrate_compound_to_4k_ram (adev, migrate, + mfence, dst, + src, i, addr); +if (r) + goto out_oom; +continue ; + } + }else { + k =1 ; + dpage =svm_migrate_get_sys_page (migrate->vma, addr,0 ); + } +if (!dpage) {pr_debug ("failed get page svms 0x%p [0x%lx 0x%lx]\n" , prange->svms, prange->start, prange->last);@@ -770,7 +1214,7 @@ svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange, goto out_oom; } - dst[i] =dma_map_page (dev, dpage,0 ,PAGE_SIZE ,DMA_BIDIRECTIONAL ); + dst[i] =dma_map_page (dev, dpage,0 ,PAGE_SIZE *k,DMA_BIDIRECTIONAL ); r =dma_mapping_error (dev, dst[i]);if (r) {dev_err (adev->dev,"%s: fail %d dma_map_page\n" , __func__, r);@@ -778,13 +1222,40 @@ svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange, goto out_oom; } -pr_debug_ratelimited ("dma mapping dst to 0x%llx, pfn 0x%lx\n" , - dst[i] >>PAGE_SHIFT ,page_to_pfn (dpage)); - migrate->dst[i] =migrate_pfn (page_to_pfn (dpage)); - dpage =NULL ; - j++; + +if (k >1 ) { + migrate->dst[i] |=MIGRATE_PFN_COMPOUND ; + +for (l =1 ; l < k; l++) { + + src[i + l] = src[i] + l*PAGE_SIZE ; + dst[i + l] = dst[i] + l*PAGE_SIZE ; + migrate->dst[i + l] =0 ; + } + +/* migrate the SVM_HPAGE_PMD_NR pages above +* the migration is for 2MB THP +*/ + r =svm_migrate_copy_memory_gart ( + adev, dst + i, + src + i,SVM_HPAGE_PMD_NR , +FROM_VRAM_TO_RAM , + mfence);
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange, · line 1255
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +10
-0+ +/* mark head page dma mapping as THP, tail pages dma addr +* are set to 0 for following dma_unmap +*/ + dst[i] |=SVM_RANGE_DMA_THP ; +for (l =1 ; l < k; l++) + dst[i + l] =0 ; + +if (r) + goto out_oom;
[Severity: High]
As reported by Philip Yang on v3 [1], does jumping to out_oom here or from the svm_migrate_compound_to_4k_ram() fallback leave migrate->dst[i .. i + k - 1] populated?
In both paths dpage is already NULL and i has not been advanced yet, so the while (i--) cleanup loop in out_oom only visits indices 0 .. i - 1:
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c:svm_migrate_copy_to_ram() {
...
/* release previous allocated sys pages and unmap dma address */
while (i--) {
if (dst[i]) {
dma_unmap_page(dev, dst[i], PAGE_SIZE,
DMA_BIDIRECTIONAL);
dst[i] = 0;
}
...
}Because svm_migrate_vma_to_ram() still calls migrate_vma_pages() and migrate_vma_finalize() after svm_migrate_copy_to_ram() fails, any pages left in migrate->dst[i .. i + SVM_HPAGE_PMD_NR - 1] are installed into the process page tables without valid copied contents. In addition, if an earlier iteration mapped a 2MB THP and set SVM_RANGE_DMA_THP on dst[idx], the out_oom loop passes the untagged address with SVM_RANGE_DMA_THP still set and size PAGE_SIZE (instead of PAGE_SIZE * SVM_HPAGE_PMD_NR) to dma_unmap_page().
[ ... ]
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c svm_migrate_vma_to_ram(struct kfd_node *node, struct svm_range *prange, · line 1345
Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +3
-0@@ -866,6 +1340,10 @@ svm_migrate_vma_to_ram(struct kfd_node *node, struct svm_range *prange,else migrate.flags =MIGRATE_VMA_SELECT_DEVICE_PRIVATE ; +if (SVM_THP_ENABLED && is_private_device && + ((end - start) >>PAGE_SHIFT ) >=SVM_HPAGE_PMD_NR ) + migrate.flags = migrate.flags |MIGRATE_VMA_SELECT_COMPOUND ;
[Severity: Medium]
When a CPU huge-PMD fault (do_huge_pmd_device_private() -> svm_migrate_to_ram()) sets migrate.fault_page and falls back to 4K system pages in svm_migrate_compound_to_4k_ram(), won't migrate_vma_pages() always fail to split the compound source folio?
do_huge_pmd_device_private() holds an extra reference on the faulting folio, and migrate_vma_collect_huge_pmd() takes another reference when collecting it with MIGRATE_PFN_COMPOUND. When migrate->dst[i] is populated with 4K pages instead of MIGRATE_PFN_COMPOUND, migrate_vma_split_unmapped_folio() calls folio_split_unmapped(), which requires folio_ref_count(folio) == 1 and fails with -EAGAIN, discarding all 512 allocated and SDMA-copied 4K pages.
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3