Skip to content

drm/amdkfd: Enable device private memory THP support in kfd svm driver

Xiaogang.Chen amd-gfx 5 versions – New

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v5 Latest3 patches 0 repliesNew
  2. v43 patches 0 repliesSuperseded
  3. v33 patches 3 repliesSuperseded
Earlier versions · 2
VersionPostedPatchesRepliesStatus
v24 patches6 repliesSuperseded
v14 patches0 repliesSuperseded

Changes in v5 from the cover letter

- Rebase code on tip of amd-staging-drm-next branch which is based on kernel 7.2.0

Patches in v5 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3drm/amdkfd: Add awareness of THP of device and system RAM in kfd svm driverdrivers/gpu/drm/amd/amdkfd/kfd_migrate.c drivers/gpu/drm/amd/amdkfd/kfd_svm.c drivers/gpu/drm/amd/amdkfd/kfd_svm.h+152 -170 replies
2/3drm/amdkfd: Do not migrate partial THP memory at CPU/GPU page fault handlerdrivers/gpu/drm/amd/amdkfd/kfd_migrate.c drivers/gpu/drm/amd/amdkfd/kfd_svm.c drivers/gpu/drm/amd/amdkfd/kfd_svm.h+135 -40 replies
3/3drm/amdkfd: Enable HMM device-private THP migration in KFD SVM driverdrivers/gpu/drm/amd/amdkfd/kfd_migrate.c+530 -520 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v5 yet.

Read the v5 thread

Cover letter [PATCH v5 0/3] 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