Documentation/mm/hmm.rst | 39 + include/linux/hmm.h | 51 +- include/linux/migrate.h | 58 +- lib/test_hmm.c | 132 +++- lib/test_hmm_uapi.h | 21 +- mm/Kconfig | 1 + mm/hmm.c | 977 +++++++++++++++++++++++-- mm/migrate_device.c | 617 +++------------- tools/testing/selftests/mm/hmm-tests.c | 54 ++ 9 files changed, 1341 insertions(+), 609 deletions(-)
From: Mika Penttilä <mpenttil@redhat.com> 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. For the migrate_vma_setup() call paths, new flags, MIGRATE_VMA_FAULT, and MIGRATE_VMA_WRITE are added to tell to add fault handling to migrate. An 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/20260130111050.53670-1-jniethe@nvidia.com/ plus a small patch to adjust with no problems. Changes since v13: - rebased on v7.3-rc - fix compile error in !CONFIG_MMU_NOTIFIER kernel configs - added patch 12/12 for Documentation/mm/hmm updates - document the semantics of new flags for migrate_vma_setup() - fix error handling after migrate_vma_split_folio() failure - comment and style fixes Link to v13: https://lore.kernel.org/linux-mm/20260804042631.2175585-1-mpenttil@redhat.com/ Cc: David Hildenbrand <david@kernel.org> Cc: Jason Gunthorpe <jgg@nvidia.com> Cc: Leon Romanovsky <leonro@nvidia.com> Cc: Alistair Popple <apopple@nvidia.com> Cc: Balbir Singh <balbirs@nvidia.com> Cc: Zi Yan <ziy@nvidia.com> Cc: Matthew Brost <matthew.brost@intel.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Lorenzo Stoakes <ljs@kernel.org> Cc: "Liam R. Howlett" <Liam.Howlett@oracle.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Mike Rapoport <rppt@kernel.org> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Michal Hocko <mhocko@suse.com> Mika Penttilä (12): 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 Documentation/mm/hmm: document migration through hmm_range_fault() Documentation/mm/hmm.rst | 39 + include/linux/hmm.h | 51 +- include/linux/migrate.h | 58 +- lib/test_hmm.c | 132 +++- lib/test_hmm_uapi.h | 21 +- mm/Kconfig | 1 + mm/hmm.c | 977 +++++++++++++++++++++++-- mm/migrate_device.c | 617 +++------------- tools/testing/selftests/mm/hmm-tests.c | 54 ++ 9 files changed, 1341 insertions(+), 609 deletions(-) drm-tip base-commit: dfe5a8188de9aaddd4e46b0f2410d0bccd5c1c04 -- 2.55.0
On Tue, 22 Sep 2026 08:34:09 +0300 mpenttil@redhat.com wrote: > From: Mika Penttilä <mpenttil@redhat.com> > > 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. Sounds sensible. > 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. But no performance benefits are demonstrated?
On 9/23/26 05:27, Andrew Morton wrote: > On Tue, 22 Sep 2026 08:34:09 +0300 mpenttil@redhat.com wrote: > >> From: Mika Penttilä <mpenttil@redhat.com> >> >> 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. > Sounds sensible. > >> 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. > But no performance benefits are demonstrated? There are no performance regressions for current tests. Real benefits come if want to do migrate on fault. For migrate on fault today missing pages are collected as not-present and the caller has to fault them and re-run migrate_vma_setup(); folding HMM_PFN_REQ_FAULT into the collecting walk removes that extra fault+retry round-trip, dropping two page table walks. Page table walks are not cheap. Not to mention simplified implementation for driver. Also, the vma looked up as part of the walk is readily available for migration, eliminating the need for explicit vma lookup - one more performance benefit. Net effect two saved page table walks and one vma lookup. This series also addresses the vanished/reborn page table while collecting problem which can crash current implementation. --Mika
On Wed, 23 Sep 2026 08:29:34 +0300 Mika Penttilä <mpenttil@redhat.com> wrote: > > >> 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. > > Sounds sensible. > > > >> 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. > > But no performance benefits are demonstrated? > > There are no performance regressions for current tests. > Real benefits come if want to do migrate on fault. > For migrate on fault today missing pages are collected as not-present and > the caller has to fault them and re-run migrate_vma_setup(); folding > HMM_PFN_REQ_FAULT into the collecting walk removes that extra > fault+retry round-trip, dropping two page table walks. Page table walks > are not cheap. Not to mention simplified implementation for driver. > Also, the vma looked up as part of the walk is readily available for > migration, eliminating the need for explicit vma lookup - one more > performance benefit. Net effect two saved page table walks and > one vma lookup. It certainly sounds that this series will result in performance improvements, but have those improvements been quantified? > This series also addresses the vanished/reborn page table while > collecting problem which can crash current implementation. Oh. I didn't get that message from the v14 changelogs, and crashes in the current implementation are not what we want. In fact, addressing those is more important than speeding things up. Under what circumstances to these crashes occur? Can/should we we fix those in a minimal -stable backportable fashion before moving on to speedups?
On 9/24/26 00:19, Andrew Morton wrote: > On Wed, 23 Sep 2026 08:29:34 +0300 Mika Penttilä <mpenttil@redhat.com> wrote: > >>>> 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. >>> Sounds sensible. >>> >>>> 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. >>> But no performance benefits are demonstrated? >> There are no performance regressions for current tests. >> Real benefits come if want to do migrate on fault. >> For migrate on fault today missing pages are collected as not-present and >> the caller has to fault them and re-run migrate_vma_setup(); folding >> HMM_PFN_REQ_FAULT into the collecting walk removes that extra >> fault+retry round-trip, dropping two page table walks. Page table walks >> are not cheap. Not to mention simplified implementation for driver. >> Also, the vma looked up as part of the walk is readily available for >> migration, eliminating the need for explicit vma lookup - one more >> performance benefit. Net effect two saved page table walks and >> one vma lookup. > It certainly sounds that this series will result in performance > improvements, but have those improvements been quantified? Yes I think Alistair mentioned those walks showed clearly in perf traces in his experiments, and that served as a motivation for this series > >> This series also addresses the vanished/reborn page table while >> collecting problem which can crash current implementation. > Oh. I didn't get that message from the v14 changelogs, and crashes in > the current implementation are not what we want. In fact, addressing > those is more important than speeding things up. > > Under what circumstances to these crashes occur? Can/should we we fix > those in a minimal -stable backportable fashion before moving on to speedups? It is about races between concurrent MADV_DONTNEED and collecting, where the collecting arrays overflow if page table is cleared, recycled and reinstantiated as large page. I am not sure if there is minimal fix to all the corner cases, or would it be more like the approach in this series. --Mika
On Wed, Sep 23, 2026 at 02:19:36PM -0700, Andrew Morton wrote: > On Wed, 23 Sep 2026 08:29:34 +0300 Mika Penttilä <mpenttil@redhat.com> wrote: > > > > > >> 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. > > > Sounds sensible. > > > > > >> 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. > > > But no performance benefits are demonstrated? > > > > There are no performance regressions for current tests. > > Real benefits come if want to do migrate on fault. > > For migrate on fault today missing pages are collected as not-present and > > the caller has to fault them and re-run migrate_vma_setup(); folding > > HMM_PFN_REQ_FAULT into the collecting walk removes that extra > > fault+retry round-trip, dropping two page table walks. Page table walks > > are not cheap. Not to mention simplified implementation for driver. > > Also, the vma looked up as part of the walk is readily available for > > migration, eliminating the need for explicit vma lookup - one more > > performance benefit. Net effect two saved page table walks and > > one vma lookup. > > It certainly sounds that this series will result in performance > improvements, but have those improvements been quantified? > > > This series also addresses the vanished/reborn page table while > > collecting problem which can crash current implementation. > > Oh. I didn't get that message from the v14 changelogs, and crashes in > the current implementation are not what we want. In fact, addressing > those is more important than speeding things up. Yeah, what is this about? The series still has lots of sashiko remarks, are they legit? Jason
On 9/24/26 02:24, Jason Gunthorpe wrote: > On Wed, Sep 23, 2026 at 02:19:36PM -0700, Andrew Morton wrote: >> On Wed, 23 Sep 2026 08:29:34 +0300 Mika Penttilä <mpenttil@redhat.com> wrote: >> >>>>> 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. >>>> Sounds sensible. >>>> >>>>> 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. >>>> But no performance benefits are demonstrated? >>> There are no performance regressions for current tests. >>> Real benefits come if want to do migrate on fault. >>> For migrate on fault today missing pages are collected as not-present and >>> the caller has to fault them and re-run migrate_vma_setup(); folding >>> HMM_PFN_REQ_FAULT into the collecting walk removes that extra >>> fault+retry round-trip, dropping two page table walks. Page table walks >>> are not cheap. Not to mention simplified implementation for driver. >>> Also, the vma looked up as part of the walk is readily available for >>> migration, eliminating the need for explicit vma lookup - one more >>> performance benefit. Net effect two saved page table walks and >>> one vma lookup. >> It certainly sounds that this series will result in performance >> improvements, but have those improvements been quantified? >> >>> This series also addresses the vanished/reborn page table while >>> collecting problem which can crash current implementation. >> Oh. I didn't get that message from the v14 changelogs, and crashes in >> the current implementation are not what we want. In fact, addressing >> those is more important than speeding things up. > Yeah, what is this about? > > The series still has lots of sashiko remarks, are they legit? Some of them are legit some not. This was actually the first time sashiko managed to apply the series. I will address all the remarks in v15 soon. > > Jason --Mika >
© 2016 - 2026 Red Hat, Inc.