From: mpenttil@redhat.com
To: linux-mm@kvack.org
Cc: dri-devel@lists.freedesktop.org, intel-xe@lists.freedesktop.org,
linux-kernel@vger.kernel.org,
"Mika Penttilä" <mpenttil@redhat.com>,
"David Hildenbrand" <david@kernel.org>,
"Jason Gunthorpe" <jgg@nvidia.com>,
"Leon Romanovsky" <leonro@nvidia.com>,
"Alistair Popple" <apopple@nvidia.com>,
"Balbir Singh" <balbirs@nvidia.com>, "Zi Yan" <ziy@nvidia.com>,
"Matthew Brost" <matthew.brost@intel.com>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Lorenzo Stoakes" <ljs@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Mike Rapoport" <rppt@kernel.org>,
"Michal Hocko" <mhocko@suse.com>, "Lyude Paul" <lyude@redhat.com>,
"Danilo Krummrich" <dakr@kernel.org>,
"Jonathan Corbet" <corbet@lwn.net>,
"Shuah Khan" <shuah@kernel.org>,
"Madhavan Srinivasan" <maddy@linux.ibm.com>
Subject: [PATCH v18 11/14] Documentation/mm/hmm: document migration through hmm_range_fault()
Date: Tue, 1 Dec 2026 06:55:28 +0200 [thread overview]
Message-ID: <20261201045531.3622535-12-mpenttil@redhat.com> (raw)
In-Reply-To: <20261201045531.3622535-1-mpenttil@redhat.com>
From: Mika Penttilä <mpenttil@redhat.com>
With this series migrate_vma_setup() collects through the
hmm_range_fault() page table walk, and drivers can collect directly
from hmm_range_fault() for migrate on fault. Update hmm.rst to match:
Describe the two new MIGRATE_VMA_FAULT / MIGRATE_VMA_WRITE flags of
migrate_vma_setup(), what is collected without them, and that a failed
fault aborts the whole range.
Correct the lock_page() wording: collection only trylocks the pages,
and a page that can't be locked is not migrated. Note that
migrate_vma_setup() now uses the hmm_range_fault() walk.
Add a section on driving the migration collection phase directly from
hmm_range_fault() via HMM_PFN_REQ_MIGRATE and migrate_hmm_range_setup():
the range and notifier requirements, how collected entries and holes are
marked, rollback, the mmu_interval_read_retry() duty, best effort
collection (shared pages aren't collected when faulting), the rule not
to hold locks across the call that the driver takes again before
finalizing, and the abort contract.
Signed-off-by: Mika Penttilä <mpenttil@redhat.com>
---
Documentation/mm/hmm.rst | 123 ++++++++++++++++++++++++++++++++++++++-
1 file changed, 121 insertions(+), 2 deletions(-)
diff --git a/Documentation/mm/hmm.rst b/Documentation/mm/hmm.rst
index fc1b8dc19825..1ba7086ca888 100644
--- a/Documentation/mm/hmm.rst
+++ b/Documentation/mm/hmm.rst
@@ -348,6 +348,33 @@ between device driver specific code and shared common code:
Currently only anonymous private VMA ranges can be migrated to or from
system memory and device private memory.
+ By default nothing is faulted in: present pages are collected, empty
+ entries of anonymous vmas are collected as holes to be filled with new
+ pages, and pages that are not present (swapped out, or another device's
+ private pages) are skipped. Two additional flags ask migrate_vma_setup()
+ to fault in such pages first:
+
+ * ``MIGRATE_VMA_FAULT`` faults in missing pages with read access.
+ * ``MIGRATE_VMA_WRITE`` faults in missing pages with write access
+ (implies faulting), breaking any sharing first, so the device gets an
+ exclusive writable copy.
+
+ When faulting, pages shared with other processes (e.g. after fork) are
+ only collected once a write fault has given this process its own copy:
+ with ``MIGRATE_VMA_FAULT`` alone they stay in place. Device private pages
+ owned by ``args->pgmap_owner`` are never faulted, so a write fault doesn't
+ unshare them either. The faults are those the CPU would take, so empty
+ anonymous entries get system pages that are then copied, instead of being
+ collected as holes.
+
+ With either flag, a fault that fails aborts the whole range: the partial
+ collection is undone, ``args->src`` is cleared, ``args->cpages`` and
+ ``args->npages`` are zero, and migrate_vma_setup() returns the error
+ (for example ``-EFAULT`` or ``-ENOMEM``). Pages that just can't be
+ collected don't abort the range. The ``MMU_NOTIFY_MIGRATE`` invalidation
+ described below is then ended around each fault and reopened over the
+ rest of the range.
+
One of the first steps migrate_vma_setup() does is to invalidate other
device's MMUs with the ``mmu_notifier_invalidate_range_start()`` and
``mmu_notifier_invalidate_range_end()`` calls around the page table
@@ -364,11 +391,14 @@ between device driver specific code and shared common code:
entry results in a valid "zero" PFN stored in the ``args->src`` array.
This lets the driver allocate device private memory and clear it instead
of copying a page of zeros. Valid PTE entries to system memory or
- device private struct pages will be locked with ``lock_page()``, isolated
+ device private struct pages will be locked (only trylocked: a page that
+ can't be locked is not migrated), isolated
from the LRU (if system memory since device private pages are not on
the LRU), unmapped from the process, and a special migration PTE is
inserted in place of the original PTE.
- migrate_vma_setup() also clears the ``args->dst`` array.
+ migrate_vma_setup() also clears the ``args->dst`` array. It does all of
+ this through the hmm_range_fault() page table walk, see "Migration
+ collection through hmm_range_fault()" below.
3. The device driver allocates destination pages and copies source pages to
destination pages.
@@ -427,6 +457,95 @@ between device driver specific code and shared common code:
The lock can now be released.
+Migration collection through hmm_range_fault()
+==============================================
+
+The collection phase of migration (steps 1 and 2 above) can also be driven by
+hmm_range_fault() directly, sharing its page table walk. This lets a driver
+fault in and collect a range for migration in one walk, which is useful for
+migrate on fault.
+
+To do so, the driver sets ``HMM_PFN_REQ_MIGRATE`` in ``range->default_flags``
+and points ``range->migrate`` at a ``struct migrate_vma`` it has filled in
+(``flags``, ``src``, ``dst``, ``pgmap_owner``; ``src``, ``dst`` and
+``range->hmm_pfns`` have one entry per page of the range). ``range->notifier``
+must be set and ``range->notifier_seq`` must come from
+mmu_interval_read_begin(); hmm_range_fault() fills in ``migrate->vma``,
+``start`` and ``end``. ``migrate->flags`` needs at least one
+``MIGRATE_VMA_SELECT_*`` bit. The range must lie within a single vma, otherwise
+``-ERANGE`` is returned (``-EFAULT`` if it starts in a hole); vmas
+migrate_vma_setup() doesn't handle (not mm backed, hugetlb, DAX, shadow stack)
+get ``-EINVAL``.
+
+The mmap_read_lock() has to be held for the whole migration, since the vma
+must stay stable. hmm_range_fault_unlocked_timeout() can't be used: it fails
+without collecting anything (don't call migrate_hmm_range_setup() after it).
+
+Usually ``HMM_PFN_REQ_FAULT`` (and ``HMM_PFN_REQ_WRITE``) is set as well, so
+missing pages are faulted in before being collected. ``MIGRATE_VMA_FAULT`` and
+``MIGRATE_VMA_WRITE`` in ``migrate->flags`` have no effect here: only
+migrate_vma_setup() reads them.
+
+Migration requests come only from ``range->default_flags``:
+``range->pfn_flags_mask`` must be 0 and ``range->dev_private_owner`` must
+equal ``migrate->pgmap_owner``. ``range->hmm_pfns`` is cleared at the start of
+hmm_range_fault(), so per-pfn input, including ``HMM_PFN_INOUT_FLAGS`` such as
+``HMM_PFN_DMA_MAPPED``, is not preserved across a migrating call.
+
+hmm_range_fault() collects the entries the same way migrate_vma_setup() does,
+taking a folio reference, locking it and installing a migration PTE. Collected
+pages are marked with ``HMM_PFN_VALID | HMM_PFN_MIGRATE`` in
+``range->hmm_pfns``. When a large folio is collected whole, only its first
+entry is marked, with ``HMM_PFN_COMPOUND`` added, and the following entries are
+0, as with ``MIGRATE_PFN_COMPOUND`` in ``migrate->src``. An entry with
+``HMM_PFN_MIGRATE`` but without ``HMM_PFN_VALID`` is a collected hole (empty or
+zero page) that the driver may fill with a new page. If the page tables change
+while locks are dropped, entries collected earlier are rolled back when the
+walk visits them again; entries it doesn't revisit stay collected and are
+released by migrate_vma_pages() and migrate_vma_finalize().
+
+The ``MMU_NOTIFY_MIGRATE`` invalidation covering the collected range is ended
+while hmm_range_fault() faults pages in. As with any hmm_range_fault() use, the
+driver must check mmu_interval_read_retry() under its own lock before mapping
+the destination pages on the device, and abort the migration if it reports a
+change.
+
+Collection is best effort when migrating: a folio that can't be locked is left
+as reported, and if a large folio can't be locked for splitting or can't be
+split, the rest of its pmd range is not walked further (not faulted or
+collected), as migrate_vma_setup() does, even with ``HMM_PFN_REQ_FAULT``.
+Entries under another migration are left 0, since waiting for them while
+holding collected folios could deadlock. For the same reason, when faulting,
+only pages mapped by this process alone are collected: a page shared with other
+processes (after fork, or KSM) is left as reported. A write fault gives this
+process its own copy first, so this only leaves shared pages in place for read
+faults. That is the price of faulting in the same walk. Drivers check
+``HMM_PFN_MIGRATE`` (and ``HMM_PFN_VALID``, see above) per entry.
+
+Faults by other tasks on collected pages wait until the migration is finalized.
+The driver must therefore not hold locks across a migrating hmm_range_fault()
+call that it takes again before migrate_vma_finalize().
+
+After hmm_range_fault() returns, the driver calls::
+
+ void migrate_hmm_range_setup(struct hmm_range *range);
+
+to translate ``range->hmm_pfns`` into ``migrate->src[]`` (``migrate->dst[]`` is
+zeroed), initialize ``migrate->cpages`` and ``migrate->npages``, and unmap the
+collected pages as migrate_vma_setup() does (migrate_vma_unmap()). From here on
+the ``range->migrate`` struct is ready for the rest of the flow (steps 3
+onwards): the driver either completes the migration or aborts it as below,
+and either way calls migrate_vma_pages() and migrate_vma_finalize().
+
+migrate_hmm_range_setup() must be called even when hmm_range_fault() fails,
+since it may have collected part of the range before failing. On any error,
+including ``-EBUSY``, the driver then aborts the partial migration: it skips
+step 3, so ``migrate->dst[]`` stays zeroed, and calls migrate_vma_pages() and
+migrate_vma_finalize(), which restore the original PTEs and release the
+collected folios. A migrating call does not resume a previous attempt, so
+the previous one, completed or aborted, must be finished before calling
+hmm_range_fault() again.
+
Exclusive access memory
=======================
--
2.55.0
next prev parent reply other threads:[~2026-12-01 4:56 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 4:55 [PATCH v18 00/14] migrate on fault for device pages mpenttil
2026-12-01 4:55 ` [PATCH v18 01/14] mm/Kconfig: changes for " mpenttil
2026-12-01 4:55 ` [PATCH v18 02/14] mm: add helper to convert HMM pfn to migrate pfn mpenttil
2026-12-01 4:55 ` [PATCH v18 03/14] mm/hmm: preparations for HMM to participate in migration mpenttil
2026-12-01 4:55 ` [PATCH v18 04/14] mm/hmm: do the plumbing " mpenttil
2026-12-01 4:55 ` [PATCH v18 05/14] mm/hmm: implement rollback for device page migration in HMM pagewalk mpenttil
2026-12-01 4:55 ` [PATCH v18 06/14] mm/hmm: migrate collection in HMM pagewalk - pte level mpenttil
2026-12-01 4:55 ` [PATCH v18 07/14] mm/hmm: migrate collection in HMM pagewalk - pmd level mpenttil
2026-12-01 4:55 ` [PATCH v18 08/14] mm/hmm: add lazy MMU mode support for migration in HMM pagewalk mpenttil
2026-12-01 4:55 ` [PATCH v18 09/14] mm: enable device page migration from " mpenttil
2026-12-01 4:55 ` [PATCH v18 10/14] lib/test_hmm: add a new testcase for the migrate on fault mpenttil
2026-12-01 4:55 ` mpenttil [this message]
2026-12-01 4:55 ` [PATCH v18 12/14] drm/nouveau/dmem: serialize the copy channel users mpenttil
2026-12-01 4:55 ` [PATCH v18 13/14] drm/nouveau/dmem: commit migrate_to_ram() only after the copy is confirmed mpenttil
2026-12-01 4:55 ` [PATCH v18 14/14] drm/nouveau/svm: add support for migrate on fault mpenttil
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201045531.3622535-12-mpenttil@redhat.com \
--to=mpenttil@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=apopple@nvidia.com \
--cc=balbirs@nvidia.com \
--cc=corbet@lwn.net \
--cc=dakr@kernel.org \
--cc=david@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=intel-xe@lists.freedesktop.org \
--cc=jgg@nvidia.com \
--cc=leonro@nvidia.com \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=lyude@redhat.com \
--cc=maddy@linux.ibm.com \
--cc=matthew.brost@intel.com \
--cc=mhocko@suse.com \
--cc=rppt@kernel.org \
--cc=shuah@kernel.org \
--cc=vbabka@kernel.org \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox