[PATCH v13 00/11] migrate on fault for device pages
[email protected] Tue, 4 Aug 2026 07:26:20 +0300
| Newsgroups | org.freedesktop.lists.intel-xe,org.freedesktop.lists.dri-devel,org.kernel.vger.linux-kernel,org.kvack.linux-mm |
|---|---|
| Message-ID | <[email protected]> |
From: Mika Penttilä <[email protected]> Currently, the way device page faulting and migration works is not optimal, if you want to do both fault handling and migration at once. Being able to migrate not present pages (or pages mapped with incorrect permissions, eg. COW) to the GPU requires doing either of the following sequences: 1. hmm_range_fault() - fault in non-present pages with correct permissions, etc. 2. migrate_vma_*() - migrate the pages Or: 1. migrate_vma_*() - migrate present pages 2. If non-present pages detected by migrate_vma_*(): a) call hmm_range_fault() to fault pages in b) call migrate_vma_*() again to migrate now present pages The problem with the first sequence is that you always have to do two page walks even when most of the time the pages are present or zero page mappings so the common case takes a performance hit. The second sequence is better for the common case, but far worse if pages aren't present because now you have to walk the page tables three times (once to find the page is not present, once so hmm_range_fault() can find a non-present page to fault in and once again to setup the migration). It is also tricky to code correctly. One page table walk could costs over 1000 cpu cycles on X86-64, which is a significant hit. We should be able to walk the page table once, faulting pages in as required and replacing them with migration entries if requested. Add a new flag to HMM APIs, HMM_PFN_REQ_MIGRATE, which tells to prepare for migration also during fault handling. Also, for the migrate_vma_setup() call paths, a flag, MIGRATE_VMA_FAULT, is added to tell to add fault handling to migrate. One extra benefit of migrating with hmm_range_fault() path is the migrate_vma.vma gets populated, so no need to retrieve that separataly. Tested in X86-64 VM with HMM test device, passing the selftests. For performance, the migrate throughput tests from the selftests show similar numbers (within error margin) as unmodified kernel. Tested also rebased on the "Remove device private pages from physical address space" series: https://lore.kernel.org/linux-mm/[email protected]/ plus a small patch to adjust with no problems. Changes since v12: - fixed Lorenzo's mail address - made hmm_pfns_fill() check for PMD size region for hole migration - added lazy mmu mode for migrate - check pte_present() before flush_cache_page() an folio_mark_dirty() - allowed the normal migration path to specify write access (fault) - split and refactored the series more. Also reduced ifdeffery in hmm.c with static inline functions - handled rollback for cases where the page table has been cleared and/or replaced with something else like PMD leaf Revisions: - RFC https://lore.kernel.org/linux-mm/[email protected]/ - v1: https://lore.kernel.org/all/[email protected]/ - v2: https://lore.kernel.org/all/[email protected]/ - v3: https://lore.kernel.org/all/[email protected]/ - v4: https://lore.kernel.org/all/[email protected]/ - v5: https://lore.kernel.org/linux-mm/[email protected]/ - v6: https://lore.kernel.org/linux-mm/[email protected]/ - v7: https://lore.kernel.org/linux-mm/[email protected]/ - v8: https://lore.kernel.org/linux-mm/[email protected]/ - v9: https://lore.kernel.org/linux-mm/[email protected]/ - v10: https://lore.kernel.org/linux-mm/[email protected]/ - v11: https://lore.kernel.org/linux-mm/[email protected]/ - v12: https://lore.kernel.org/linux-mm/[email protected]/ Cc: David Hildenbrand <[email protected]> Cc: Jason Gunthorpe <[email protected]> Cc: Leon Romanovsky <[email protected]> Cc: Alistair Popple <[email protected]> Cc: Balbir Singh <[email protected]> Cc: Zi Yan <[email protected]> Cc: Matthew Brost <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Lorenzo Stoakes <[email protected]> Cc: "Liam R. Howlett" <[email protected]> Cc: Vlastimil Babka <[email protected]> Cc: Mike Rapoport <[email protected]> Cc: Suren Baghdasaryan <[email protected]> Cc: Michal Hocko <[email protected]> Mika Penttilä (11): mm/Kconfig: changes for migrate on fault for device pages mm: add helper to convert HMM pfn to migrate pfn mm/hmm: preparations for HMM to participate in migration mm/hmm: do the plumbing for HMM to participate in migration mm/hmm: implement folio split for migrate needs in HMM pagewalk mm/hmm: migrate collection in HMM pagewalk - pte level mm/hmm: migrate collection in HMM pagewalk - pmd level mm/hmm: add lazy MMU mode support for migration in HMM pagewalk mm/hmm: implement rollback for device page migration in HMM pagewalk mm: enable device page migration from HMM pagewalk lib/test_hmm: add a new testcase for the migrate on fault include/linux/hmm.h | 41 +- include/linux/migrate.h | 54 +- lib/test_hmm.c | 132 +++- lib/test_hmm_uapi.h | 19 +- mm/Kconfig | 2 + mm/hmm.c | 950 +++++++++++++++++++++++-- mm/migrate_device.c | 588 +++------------ tools/testing/selftests/mm/hmm-tests.c | 54 ++ 8 files changed, 1260 insertions(+), 580 deletions(-) drm-tip base-commit: c2d24e2eda55d74743f0d99a98805bf0b596beef -- 2.55.0