From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796100989; b=5jyfgTqVrl6gB6fhZxcviD6uKaG+atZAly9Zt8dlUDlUk5ZiP8BPyUapwhikqGY5s2Dx NdOoTAsDFcEKiAwObQrhshnZcFGSb/mn3c78Q6/IpH5rAAL7mBE4PZWXu6Rcym8bU5U6r DXFm/JiSyGQ3lyhkEpMru/j6X7mvwxVpuDis938hXNIgkkPnTY1UxoiCS1hdXNyOjH/06 VHBFJQpbupWZxImT30YJPp0A6p/2C1LRuimmjV0xbAhEDSmStiEP+y2UguY12ZqWdWeze RMy+qkwAwquXjljI1wM2HX0VUZQqGWTRkYI3bNAgpiF5pX9l46SrvuthJZZUWqxVLBA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100989; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received: X-Received:Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=l9s4u/w158L5rE/R90VfTQRsX/eto/UX5fDHEHQ431A=; b=fkfQMx5NOfXc7NtLPHC/p1eYH9jqn2XTKKlA0iHdSLsQMDJqQoEM+oR0Ken1jL2d6YVb /af+jAsp5z65ghmrgNBwtt8sKYwYebry4ZCvp2JNK3PwtO0G2LG06DAq0g3NPNR9Ui6M7 /5fKP3ILOTDNm/pVz48evQBtrgj/7AQzzCBZo/DHEdxdYMOTYVCXONIqM2CfcvEUBbxDn i5gnok1kn1WrxOvASRnRtYcH/2SGxC324VfepVTK8TJ8LZt8IAC4zi/SB4AvqqaDCX5TS KYHC3laBikz+4jrdD4bZJlYFAoEB2Y4BUegHBL5VJpMi0sL87GxiO3A4W1vRgWiLtAw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=VsWZIoTE; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=jzOfkyX1; arc=none smtp.remote-ip=170.10.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 052AB606A325 for ; Tue, 1 Dec 2026 04:56:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 052AB606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=VsWZIoTE; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=jzOfkyX1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796100989; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=l9s4u/w158L5rE/R90VfTQRsX/eto/UX5fDHEHQ431A=; b=VsWZIoTE7jnq2bHomkZzLF9ALikLWxRLrECDH6FTrjALMbr4uT7yA3H+BC81CzY9cFDEiY Cx6B6GUl7uh5bBNb2BOIJ0E+cOoR4RWDtMace6n9FqMxYT8DBlqaNdAFI565E9OM1JPKoc MTWiFWmvgXqyfjvj5QFwAvnlqKECDt8= Received: from mail-lf1-f71.google.com (mail-lf1-f71.google.com [209.85.167.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-688-SUnEqXTGMbS0cvDvAzz_VQ-1; Tue, 1 Dec 2026 04:56:26 +0000 X-MC-Unique: SUnEqXTGMbS0cvDvAzz_VQ-1 X-Mimecast-MFC-AGG-ID: SUnEqXTGMbS0cvDvAzz_VQ_1796100985 Received: by mail-lf1-f71.google.com with SMTP id 2adb3069b0e04-5c35382e6b4so850606e87.2 for ; Mon, 30 Nov 2026 20:56:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1796100984; x=1796705784; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=l9s4u/w158L5rE/R90VfTQRsX/eto/UX5fDHEHQ431A=; b=jzOfkyX19jLETNFJTy7SePUhAYPEUVY5yl09AV/hk1xLOFzWZIwDMZ3uOqURqaHrpq uCFJ4xzqO76mj2JfWE6kLqujAm0/aZFXk6zfTkBQBauLuBZBbw8PVeqww5CE/ndsk7iV tqay/DDdd8fzcq2ZSbImsLzvDMIGQcImNcYi1+m4dxxcMGEqfhzv4+jM1DPZHfpDAow4 2k7L0A0JnzYb5xlu25kRoez90oYCDORUcLAZwC/p32KzcgTPJEEcslJ4axb/pV0RQsLA 7n4FuO8vcJ2/qx9ZMPMERJqWeQmGJSI0UF22t5o/wsiLHtu5DtG9DSxa31GUDGQP3kDU zXsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100984; x=1796705784; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=l9s4u/w158L5rE/R90VfTQRsX/eto/UX5fDHEHQ431A=; b=qvw/iGUj+L5+FV+StVU6kRY+60mdAce2GZC9MDwNE5C4uIwlAM1YPnAyoFS46MfbeH zeWuVf79oOzFCfnT/wHz3x8NiSCtt/Sgdgy6r/9J6IOmz0Uo05AXAK1ihwZCAuEY/oqE C0m5QnqGSv7o9JhqFZutJWALN46j+7KsxuP/oqGP/f6sUUFYkJE49ErMjqKlaQ2sXJW8 vdDoO/zj0j9WNxvRq+bLBixqlZnpu4ceFjxdyUbVST1WFvyL+CWkZMW2sRgpYeqUrRhF Ke7z33xYEk7L37183400aAO7chCX5CUBQPoP60RcYAi952F8hwLshxpbaaJIU0qot2wG B8hQ== X-Forwarded-Encrypted: i=1; AL+hErFEWUhSPD96cL4A6/f2lEsxGiSo2bNh3GnRnt68shWNApmZFjJ/FTn0U56d5oZ3W90wCYp/gJD3OK2C13E=@vger.kernel.org X-Gm-Message-State: APuU4Z+g9ibfeFmAwUQ6d37yvFD3cntNVHKTb0oc9G1gB/++CXZmfnIX OzDr7K22bYgaOIYRNHd738eSfk2hJ5CXEBf+jzDlbj6uSAPF//02j4wb2QB1q4jAVsEaqiak8S/ qXjR5fRNvPgEZuOnH39eiXSwuZRgO2wqMYjo+FYJbO+osrqr8xW624bcQIDv1obbW X-Gm-Gg: AZFdTZfagglzMyedXQzzcyUSBGvaPsvrO37z8rnJg3y/+RqyLdH8uiwecT2Z1+kj/fH fFbVayJxLk3GXvW+ziwa0vsOvQwmmv7NXueQxBuP4OGo0PY5xUvgHc3Ecjr+kRP+dkNVFFfRaFq 92uNFT5SjN/oH+zxN9757eAO0JkdiGwAP0PcXXmzOcg5lqsFEyqBkcGx5UPGleeI9tcIlQ+TiP9 coyrGD1k+ZimN7/Jf7nMdrkJJ6EZ1It/IiC0HxaE5i/oTEP2lDaJoKGiEuduw/YG438wc08VHyQ 2iNSydxVfVHZlexSpm1Ka894hxNyc8c2qfJObPCDNuICUNULefxQRpv25LnJsQ/BgAhjTgmkxu1 I43zSatELVD7IrJM6JSCv+FJylu+A7RLwD0BYfqZv5gHt0vVLKlSYgMOvZnSWfcBwyiZCfA== X-Received: by 2002:a05:6512:3d27:b0:5bf:c2fa:3261 with SMTP id 2adb3069b0e04-5c1bc8507bbmr10663494e87.62.1796100984421; Mon, 30 Nov 2026 20:56:24 -0800 (PST) X-Received: by 2002:a05:6512:3d27:b0:5bf:c2fa:3261 with SMTP id 2adb3069b0e04-5c1bc8507bbmr10663478e87.62.1796100983889; Mon, 30 Nov 2026 20:56:23 -0800 (PST) Received: from fedora (89-27-86-246.bb.dnainternet.fi. [89.27.86.246]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5c1bd0563casm8668638e87.67.2026.11.30.20.56.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:56:20 -0800 (PST) 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, =?UTF-8?q?Mika=20Penttil=C3=A4?= , David Hildenbrand , Jason Gunthorpe , Leon Romanovsky , Alistair Popple , Balbir Singh , Zi Yan , Matthew Brost , Andrew Morton , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Michal Hocko , Lyude Paul , Danilo Krummrich , Jonathan Corbet , Shuah Khan , Madhavan Srinivasan Subject: [PATCH v18 11/14] Documentation/mm/hmm: document migration through hmm_range_fault() Date: Tue, 1 Dec 2026 06:55:28 +0200 Message-ID: <20261201045531.3622535-12-mpenttil@redhat.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261201045531.3622535-1-mpenttil@redhat.com> References: <20261201045531.3622535-1-mpenttil@redhat.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Mika Penttilä 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ä --- 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