From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7BA98606A32A for ; Tue, 1 Dec 2026 04:56:52 +0000 (UTC) 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=i6GBFG3S Received: by kanga.kvack.org (Postfix) id 85B456B05BF; Mon, 30 Nov 2026 23:56:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 80AF26B05C0; Mon, 30 Nov 2026 23:56:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6851B6B05C1; Mon, 30 Nov 2026 23:56:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C904C6B05BF for ; Mon, 30 Nov 2026 23:56:28 -0500 (EST) Received: from smtpin18.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 971BA40D56 for ; Tue, 1 Dec 2026 04:56:23 +0000 (UTC) X-FDA: 85494203046.18.CB10429 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf17.hostedemail.com (Postfix) with ESMTP id 2D24A4000A for ; Tue, 1 Dec 2026 04:56:20 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=i6GBFG3S; spf=pass (imf17.hostedemail.com: domain of mpenttil@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=mpenttil@redhat.com; dmarc=pass (policy=quarantine) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796100981; b=Kzda71+SI9/N+WLixzMNXGYifQ+GuRI2WEVe0wklkpr+jFh28Ph6SRFGObTMxOhvBaX4gC 2iQIIYsJYlFJ2Zstl/8+eGGNVUEHgPdLntlHVkq9UM4F8BILVBrXqA5iZ+55n/84WUidTd RphAIyt8RPfXSkIoMMUb3L5yXJERkEo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796100981; h=from:from:sender: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:dkim-signature; bh=CXaK3LPJxJts03sL4iOOwVfddGsBacsAISfbN/chsCU=; b=v1qZQN8ARaU1ItoXqZCw/pSXrdZeHutcR0J6+LJIvXidISgQJo8zW8QqfsIUS6VE9MgwXx WAXQohhrQBOaRY8hbX93JvU9Ar84DPeaxcrcC2wmtKQTREZ18Ce3jE/Cow5iMEdpPfv3S1 VIOjs6YWN3AFOUVe3Uu3JMnkpdJNSAU= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=i6GBFG3S; spf=pass (imf17.hostedemail.com: domain of mpenttil@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=mpenttil@redhat.com; dmarc=pass (policy=quarantine) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796100981; 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=CXaK3LPJxJts03sL4iOOwVfddGsBacsAISfbN/chsCU=; b=i6GBFG3SGwR9CgSAQ4WJpDmVjhibzznUR7b54JacRDtZsSJQYELm1NuNj7+NFlVjutf3Nj fdjPLNlCBsqnw2VzAY5ooO0svXVnwcz5poXQjerbGGxBSBZU+J4dknJiUJjVpgVMUpspCg tLo9tMg1KkHfvyqE/GWy2QojHCHJQhI= X-MC-Rep: binary re single Received: from mail-lj1-f199.google.com (mail-lj1-f199.google.com [209.85.208.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-652-aEatCggBPYaf4ymp0Krgyw-1; Tue, 1 Dec 2026 04:56:18 +0000 X-MC-Unique: aEatCggBPYaf4ymp0Krgyw-1 X-Mimecast-MFC-AGG-ID: aEatCggBPYaf4ymp0Krgyw_1796100977 Received: by mail-lj1-f199.google.com with SMTP id 38308e7fff4ca-3ab3dac2898so41566651fa.2 for ; Mon, 30 Nov 2026 20:56:18 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100977; x=1796705777; 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=CXaK3LPJxJts03sL4iOOwVfddGsBacsAISfbN/chsCU=; b=noup0+9LNOVvafQ+IwGgpkrkZx272N1t9jnnpf7Ua34iMnlQD3v6Op1O5Tnd87tc4x KE51uIb6XcTS/IbHM2fJb2TMS9DaAfH34RUPdTfAr9ug28j/slggtCk87IoO25OWhM7A UZLJMmTTmr7Iukd8Y456AM8A1y8d+AT2744QTMxtBTNE8rwjwRAFJIXQiFeLKEvdaDia eMiiVqoIPJWo7riZNjsJPO/nPsOdOk6cagxBoPlh+R0jJPpuHJcURI0uy7D7LmOx2xRZ 8Ty89ckdJrBdRDEIrKybPP88LKUNII0hQ8Fe/c8n6GpY6iM7bX2GNLM6muC6AHZtfJ9t 9H3w== X-Gm-Message-State: APuU4Z/ibou0dJG75eC57CAcnsnikaX4M82rJ2jInt0+6wPrwM70l3M4 uzUysas2D6vd8QhBWuO7cLG6M9rg3FEN2otQzDgZo4bMnVqoKLjnQAKSrkHiljVzNlyD7s2YqWr WqMrno+S90KMrohsPbpVYtO8zhaZYRUnEuctOGIt036GsZTaKurFlqHr67zZlkmYzn+GK8YUe1N QxbrD/j/NWqK8iVozD6ZjfoJWVt2mWMzdhGRy40g== X-Gm-Gg: AZFdTZdSCEvDlQL4sPQvTIrNB9bteJon6Y58MRWPythWlE85drqB8yU9QFZTbCgWFmW sKncNGf/LWV7xxd3QtbUUKfGR89j9+/ZxIqt+NWOHmHLlln5Z1uLKoeLy6EPMAnHsZfIPDHOauC EbqMQZ899sp0KhcGYNGw17tnqzhq+l0NEbLKgeVvDA59ThNHDUtfM5hB1y6Rg+dn5G1RB574cDv JVEZq58OU1a9ho1nuOhBfy4O3YY/XTWW/i+Q0rG9H4BuYwJ5guoDSH78Ncyma6iS+OknyfHcqAf RHdT7bSM3lKUirmEcuI+8SI7IkoDLrtY7aTEyU1juRJI3srrpAsX7DVoifHO+k/HnYVoKeP2Bhe hBtgTFtGyA0yC5dCstJl8qZ8lL9H9fCNEhRx+h8TseuIQ43yBZ5RrpqgDPCrg+Ok4W5u9EQ== X-Received: by 2002:a05:6512:a85:b0:5bf:b7d8:dc0c with SMTP id 2adb3069b0e04-5c1bc8ceda9mr10304048e87.56.1796100976542; Mon, 30 Nov 2026 20:56:16 -0800 (PST) X-Received: by 2002:a05:6512:a85:b0:5bf:b7d8:dc0c with SMTP id 2adb3069b0e04-5c1bc8ceda9mr10304019e87.56.1796100975901; Mon, 30 Nov 2026 20:56:15 -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.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:56:15 -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 09/14] mm: enable device page migration from HMM pagewalk Date: Tue, 1 Dec 2026 06:55:26 +0200 Message-ID: <20261201045531.3622535-10-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-Archived-By: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: QsiWJ6YtHtZkpdKO8MVk3KncJQm2ZzOHcOn8TlGjZiU_1796100977 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 2D24A4000A X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Stat-Signature: xjiirrygow7ip3dbu88rnkbmpb1gy9u6 X-HE-Tag: 1796100980-868592 X-HE-Meta: U2FsdGVkX1/e6PdKHTJqcYkfMeFSPNdS5o/cRig6VqZCZZ5YXqpBrFcHdb0d/ZnNp+GJQ9FyxhjvIra15Qg3CsbilYFYvSt76XybtXnl43/hxW3dAbSu6y7TPKK289TsNAr4PUFWxPMq2nUxz7PxOolFcXQFOlIeWCtmQhz4UHDt0lgle1Snx3U3XpoDYnU8D6hcDu5IFrSit9WvGal2ZQJMqDjNK1ydyKLUqfE8fTiW3rjEm5n/pQMlzteyN7SFkIpVrSnJwl6azX8tmSYqclkOn3/fdYWSFsjf8DP8VSbvd2KHE4PldhHgjjEpg6LOWEBdj7AKnZHiPkl2SAAvzE7sBFT589Obb0AoUAi/09/1RKCWRbU5qTFq/Avt/nOjm3CWpMhgqLzTN9H2JmWRnJtE5cG67/IFzOUQz6de+DVeWRy4/ut/C+j0IQNd/Odd6VUdKi7/WdsFUdIa481Lphb6vwWMVi4ujpPgd5wMN+DH7vfokfWp/ZqHwyZO3m23s3F4Wak8L1KL0Su7oBdxumZyQ+RmReslHRSgyyqqufJK5GX7dFswpZ+pjw5vqApXvmqWiY44JsWCehPf75V6/3NyL1POj01uNBLqL4uwhAv7OH9wMtRW7GEipcXf68PiAHiOvKekKVCAD2ZT3H5d1VqFPa4hUBw9VAN+tQffDxM/H8W2HnzkI/g7vZEjFJQ9+I8SMyoXiF61ppv9CqHbw2LHc/FS/AkPrDtHmOOQIWK+gfT5QiJW0jGN+HOBqMexBO/Ec3l9v8oXhC0rTJjdgEAnY8X1COvo82dtBlL8mYzP5jRyqdfG2BEn405/JvBixtQiztZH7rveW2n3Q5QRHN6ArtzwwJme+PL2n7Um6YpkCyQkgq+hh0erlPfz42mjHVTdjsmpos+jwRHb87TATx9HCA1rlxNRBV17sdTGSpBDfS2+SALPUQKmmIhlUIuDAYbe962yEYDzvscrfPp ulRSzRqp GYAXOeamokt1UILeQJbnyZTWEHqgB3Ksmg8uyZSRyxsP8zTkv8e9Cc5WmT8fDTlQt41PtFCmBoKGLKx+Jb8JiWw9fcnQLrv9DSt0yl5tDRhw/YawrbxPOWhVoJcPFCkZ28W1wM0dv3VvLU/NmqUzEopHpl7fS7gP9JJ3kcZB8Mc9Tpf+WOJ8CYavswHmBRAXK7YlWqvD1YAaJlSUGFNYYRktg8zekDJWXCPoY1dCtY4BwqX3mEJoUkExsD33r50iZppRERbzFqEQJSGfHBF2M8lXCAN8Do74YLvcuWEUwjRM7ir1KW10q4bX+hOLalIdN3CE1g2nLChl0So5THiDqQOFpr/Gem7eTqoYxEoY0DIsg2m9cy+oQC1eIOCeYbsZ1eWIImV0IvGvYi2Dooio8gBkN19Cah8LN57KxOsHGIyZ8fVwlKpIixF7Z6Yyw5NgaGTQxJla05T38QSeYpx8r84CY1+8OBu2NFuLShsaaysple8L2XvVoCcOGiDG1jH0DBieOlexoznu4qkkOKCtU5g1G3OWl/ThrHIeujJG56B91zAaOHG3uXkHB9AS5/7hjlT5XdgKIuG946zCPkVHGlJqFFpXMsvVR67SIo1pvlrw/8PWAFVP7hKoE9XudK954AkrmN4/wEl2tQuGjDv9jRpEiYFjMG9thCmbf4/c/QLN4caqMxpWoz3hzodHm+tQ6IPQs Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Mika Penttilä HMM pagewalk has now the machinery to do the first phase of device page migration, collecting the pfns and installing migration ptes. Enable migration in hmm_range_fault(), and change migrate_vma_setup() to use HMM pagewalk path. Two new flags, MIGRATE_VMA_FAULT and MIGRATE_VMA_WRITE, let migrate_vma_setup() callers fault in missing pages, for read or for write, in the same walk. Without them, pages that aren't present (swapped out, or another device's private pages) are skipped, and shared pages after fork are migrated as they are, for every process mapping them and read-only. MIGRATE_VMA_FAULT faults the missing pages in, and MIGRATE_VMA_WRITE also breaks the sharing first, so the device gets an exclusive writable copy, without a separate hmm_range_fault() or GUP pass. When faulting, shared pages are collected only after such a write fault: with MIGRATE_VMA_FAULT alone they stay in place, as the walk can't hold a folio another process can fault on while it sleeps in a fault itself (see the pte level collection commit). If a fault fails, the partial collection is undone and migrate_vma_setup() returns the error. Drivers that migrate on device fault use hmm_range_fault() with HMM_PFN_REQ_MIGRATE directly, as the test_hmm and nouveau changes later in this series do. Only anonymous folios are collected now. The old collector also took file folios, which could only move to system memory; no in-tree user migrates those. migrate_vma_setup() refuses shadow stack vmas up front, as in the pending "mm/migrate_device: refuse to migrate shadow stacks". It also refuses the fault flags together with a fault page: from migrate_to_ram() the fault page is locked by do_swap_page(), and nested faults must not start under it. Without the fault flags, migrate_vma_setup() still fails only on invalid arguments or an unsupported vma. nouveau's migrate_to_ram() leaks its src/dst arrays when migrate_vma_setup() fails or collects nothing; that is pre-existing (since c32287471077) and fixed by a later patch in this series. Also, the migrate_vma_collect*() based paths are now unused, so delete them. Cc: David Hildenbrand Cc: Jason Gunthorpe Cc: Leon Romanovsky Cc: Alistair Popple Cc: Balbir Singh Cc: Zi Yan Cc: Matthew Brost Suggested-by: Alistair Popple Signed-off-by: Mika Penttilä --- arch/powerpc/include/asm/book3s/64/pgtable.h | 2 +- drivers/gpu/drm/drm_pagemap.c | 2 +- include/linux/migrate.h | 10 +- include/linux/mmu_notifier.h | 3 +- mm/hmm.c | 42 +- mm/migrate_device.c | 593 +++---------------- 6 files changed, 129 insertions(+), 523 deletions(-) diff --git a/arch/powerpc/include/asm/book3s/64/pgtable.h b/arch/powerpc/include/asm/book3s/64/pgtable.h index dff8790a047d..c821d305aefc 100644 --- a/arch/powerpc/include/asm/book3s/64/pgtable.h +++ b/arch/powerpc/include/asm/book3s/64/pgtable.h @@ -1334,7 +1334,7 @@ static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, * entries. This can happen at 2 places: * - When the address space is being unmapped zap_huge_pmd(), and we * encounter non-present pmds. - * - migrate_vma_collect_huge_pmd() could calls this during migration + * - hmm_vma_handle_migrate_prepare_pmd() could call this during migration * of device-private pmd entries. */ if (!pmd_present(*pmdp)) { diff --git a/drivers/gpu/drm/drm_pagemap.c b/drivers/gpu/drm/drm_pagemap.c index bbef0c225b7e..1d1785f349cf 100644 --- a/drivers/gpu/drm/drm_pagemap.c +++ b/drivers/gpu/drm/drm_pagemap.c @@ -948,7 +948,7 @@ static int drm_pagemap_migrate_populate_ram_pfn(struct vm_area_struct *vas, * A large source folio is always collected whole, at its head * page, PMD aligned and flagged MIGRATE_PFN_COMPOUND: anything * else is split before it reaches us, either by - * migrate_vma_collect_pmd() or, for the eviction path, by + * the migration collection in mm/hmm.c or, for the eviction path, by * migrate_device_pfns(). Both the order-0 fallback below and * drm_pagemap_migrate_populate_src_pages() rely on that, as * they index the folio from @i. diff --git a/include/linux/migrate.h b/include/linux/migrate.h index 9513d1697232..b13600876083 100644 --- a/include/linux/migrate.h +++ b/include/linux/migrate.h @@ -188,6 +188,8 @@ enum migrate_vma_info { MIGRATE_VMA_SELECT_DEVICE_PRIVATE = 1 << 1, MIGRATE_VMA_SELECT_DEVICE_COHERENT = 1 << 2, MIGRATE_VMA_SELECT_COMPOUND = 1 << 3, + MIGRATE_VMA_FAULT = 1 << 4, + MIGRATE_VMA_WRITE = 1 << 5, }; struct migrate_vma { @@ -230,10 +232,14 @@ struct migrate_vma { * here next to struct migrate_vma, which they use, and the !CONFIG_MIGRATION * stubs above let mm/hmm.c build without #ifdefs. */ -/* Migration collection in hmm_range_fault() is enabled later in the series */ static inline enum migrate_vma_info hmm_select_migrate(struct hmm_range *range) { - return MIGRATE_VMA_SELECT_NONE; + enum migrate_vma_info minfo; + + minfo = (range->default_flags & HMM_PFN_REQ_MIGRATE) ? + range->migrate->flags : 0; + + return minfo; } static inline void *hmm_migrate_pgmap_owner(struct hmm_range *range) diff --git a/include/linux/mmu_notifier.h b/include/linux/mmu_notifier.h index 5db78e5761c5..1d5115df2bbc 100644 --- a/include/linux/mmu_notifier.h +++ b/include/linux/mmu_notifier.h @@ -39,7 +39,8 @@ struct mmu_interval_notifier; * @MMU_NOTIFY_RELEASE: used during mmu_interval_notifier invalidate to signal * that the mm refcount is zero and the range is no longer accessible. * - * @MMU_NOTIFY_MIGRATE: used during migrate_vma_collect() invalidate to signal + * @MMU_NOTIFY_MIGRATE: used during migration collection (migrate_vma_setup(), + * or hmm_range_fault() with HMM_PFN_REQ_MIGRATE) invalidate to signal * a device driver to possibly ignore the invalidation if the * owner field matches the driver's device private pgmap owner. * diff --git a/mm/hmm.c b/mm/hmm.c index d2f5909940d3..c97ab0e4271e 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -1804,8 +1804,46 @@ static int hmm_range_fault_locked(struct hmm_range *range, bool *locked) * * The mmap lock must be held by the caller and will remain held on return. * New users should prefer hmm_range_fault_unlocked_timeout() unless they - * specifically need to keep the mmap lock held across the call. This helper - * cannot support VMAs whose fault handlers need to drop the mmap lock. + * specifically need to keep the mmap lock held across the call like while + * migrating. This helper cannot support VMAs whose fault handlers need to + * drop the mmap lock. + * + * If want to do migration after faulting, call hmm_range_fault() with + * range.default_flags of HMM_PFN_REQ_MIGRATE, and optionally + * HMM_PFN_REQ_FAULT|HMM_PFN_REQ_WRITE, and initialize range->migrate field. + * range->migrate->vma will be populated during the call, + * and must be stable across the whole migrate process, which is + * why mmap_lock must be held around this call. + * + * When HMM_PFN_REQ_MIGRATE is set, migration collection may be partial on + * return and the caller takes responsibility for completing or aborting it. + * Collection is then best effort: 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), even with HMM_PFN_REQ_FAULT. Entries under another migration + * are not waited for, as that could deadlock: they are reported as 0 (not + * valid) and not collected. When faulting, a folio shared with other + * processes (after fork, or KSM) is not collected but left as reported (see + * hmm_migrate_collectable()); a write fault gives this process its own copy + * first. Callers check HMM_PFN_VALID and HMM_PFN_MIGRATE per entry. + * + * Other tasks' faults on collected pages wait until the migration is + * finalized, so the caller must not hold locks across the call that it + * takes again before migrate_vma_finalize(). + * + * On success, the caller calls migrate_hmm_range_setup() and then either + * completes the migration (prepare destination pages, migrate_vma_pages(), + * update device mappings as needed, migrate_vma_finalize()) or aborts it as + * below. Either way migrate_vma_finalize() must be called. + * + * On any error, including -EBUSY, the caller must call + * migrate_hmm_range_setup(), migrate_vma_pages() with no valid destination + * entries, and migrate_vma_finalize() to abort the partial migration before + * retrying. A migrating call clears range->hmm_pfns on entry and does not + * resume a previous attempt. + * + * The mmap read lock must remain held until the migration has either been + * completed or aborted. */ int hmm_range_fault(struct hmm_range *range) { diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 5db01a728fce..81fa96e92b94 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -18,507 +18,6 @@ #include #include "internal.h" -static int migrate_vma_collect_skip(unsigned long start, - unsigned long end, - struct mm_walk *walk) -{ - struct migrate_vma *migrate = walk->private; - unsigned long addr; - - for (addr = start; addr < end; addr += PAGE_SIZE) { - migrate->dst[migrate->npages] = 0; - migrate->src[migrate->npages++] = 0; - } - - return 0; -} - -static int migrate_vma_collect_hole(unsigned long start, - unsigned long end, - __always_unused int depth, - struct mm_walk *walk) -{ - struct migrate_vma *migrate = walk->private; - unsigned long addr; - - /* Only allow populating anonymous memory. */ - if (!vma_is_anonymous(walk->vma)) - return migrate_vma_collect_skip(start, end, walk); - - if (thp_migration_supported() && - (migrate->flags & MIGRATE_VMA_SELECT_COMPOUND) && - (IS_ALIGNED(start, HPAGE_PMD_SIZE) && - IS_ALIGNED(end, HPAGE_PMD_SIZE))) { - migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE | - MIGRATE_PFN_COMPOUND; - migrate->dst[migrate->npages] = 0; - migrate->npages++; - migrate->cpages++; - - /* - * Collect the remaining entries as holes, in case we - * need to split later - */ - return migrate_vma_collect_skip(start + PAGE_SIZE, end, walk); - } - - for (addr = start; addr < end; addr += PAGE_SIZE) { - migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE; - migrate->dst[migrate->npages] = 0; - migrate->npages++; - migrate->cpages++; - } - - return 0; -} - -/** - * migrate_vma_split_folio() - Helper function to split a THP folio - * @folio: the folio to split - * @fault_page: struct page associated with the fault if any - * - * If @folio is not the folio containing @fault_page, the caller must hold a - * reference on @folio. The helper consumes that reference. - * - * Returns 0 on success - */ -static int migrate_vma_split_folio(struct folio *folio, - struct page *fault_page) -{ - int ret; - struct folio *fault_folio = fault_page ? page_folio(fault_page) : NULL; - - /* - * The lock and the reference of the fault handler stay with the - * folio which holds the fault page after the split. - */ - if (folio == fault_folio) - return split_huge_page(fault_page); - - folio_lock(folio); - ret = split_folio(folio); - folio_unlock(folio); - folio_put(folio); - - return ret; -} - -/** migrate_vma_collect_huge_pmd - collect THP pages without splitting the - * folio for device private pages. - * @pmdp: pointer to pmd entry - * @start: start address of the range for migration - * @end: end address of the range for migration - * @walk: mm_walk callback structure - * @fault_folio: folio associated with the fault if any - * - * Collect the huge pmd entry at @pmdp for migration and set the - * MIGRATE_PFN_COMPOUND flag in the migrate src entry to indicate that - * migration will occur at HPAGE_PMD granularity - */ -static int migrate_vma_collect_huge_pmd(pmd_t *pmdp, unsigned long start, - unsigned long end, struct mm_walk *walk, - struct folio *fault_folio) -{ - struct mm_struct *mm = walk->mm; - struct folio *folio; - struct migrate_vma *migrate = walk->private; - spinlock_t *ptl; - int ret; - unsigned long write = 0; - - ptl = pmd_lock(mm, pmdp); - if (pmd_none(*pmdp)) { - spin_unlock(ptl); - return migrate_vma_collect_hole(start, end, -1, walk); - } - - if (pmd_trans_huge(*pmdp)) { - if (!(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) { - spin_unlock(ptl); - return migrate_vma_collect_skip(start, end, walk); - } - - folio = pmd_folio(*pmdp); - if (is_huge_zero_folio(folio)) { - spin_unlock(ptl); - return migrate_vma_collect_hole(start, end, -1, walk); - } - if (pmd_write(*pmdp)) - write = MIGRATE_PFN_WRITE; - } else if (!pmd_present(*pmdp)) { - const softleaf_t entry = softleaf_from_pmd(*pmdp); - - if (!softleaf_is_device_private(entry) || - !(migrate->flags & MIGRATE_VMA_SELECT_DEVICE_PRIVATE)) { - spin_unlock(ptl); - return migrate_vma_collect_skip(start, end, walk); - } - - folio = softleaf_to_folio(entry); - if (folio->pgmap->owner != migrate->pgmap_owner) { - spin_unlock(ptl); - return migrate_vma_collect_skip(start, end, walk); - } - - if (softleaf_is_device_private_write(entry)) - write = MIGRATE_PFN_WRITE; - } else { - spin_unlock(ptl); - return -EAGAIN; - } - - folio_get(folio); - if (folio != fault_folio && unlikely(!folio_trylock(folio))) { - spin_unlock(ptl); - folio_put(folio); - return migrate_vma_collect_skip(start, end, walk); - } - - if (thp_migration_supported() && - (migrate->flags & MIGRATE_VMA_SELECT_COMPOUND) && - (IS_ALIGNED(start, HPAGE_PMD_SIZE) && - IS_ALIGNED(end, HPAGE_PMD_SIZE))) { - - struct page_vma_mapped_walk pvmw = { - .ptl = ptl, - .address = start, - .pmd = pmdp, - .vma = walk->vma, - }; - - unsigned long pfn = page_to_pfn(folio_page(folio, 0)); - - migrate->src[migrate->npages] = migrate_pfn(pfn) | write - | MIGRATE_PFN_MIGRATE - | MIGRATE_PFN_COMPOUND; - migrate->dst[migrate->npages++] = 0; - migrate->cpages++; - ret = set_pmd_migration_entry(&pvmw, folio_page(folio, 0)); - if (ret) { - migrate->npages--; - migrate->cpages--; - migrate->src[migrate->npages] = 0; - migrate->dst[migrate->npages] = 0; - goto fallback; - } - migrate_vma_collect_skip(start + PAGE_SIZE, end, walk); - spin_unlock(ptl); - return 0; - } - -fallback: - spin_unlock(ptl); - if (!folio_test_large(folio)) - goto done; - ret = split_folio(folio); - if (fault_folio != folio) - folio_unlock(folio); - folio_put(folio); - if (ret) - return migrate_vma_collect_skip(start, end, walk); - if (pmd_none(pmdp_get_lockless(pmdp))) - return migrate_vma_collect_hole(start, end, -1, walk); - -done: - return -ENOENT; -} - -static int migrate_vma_collect_pmd(pmd_t *pmdp, - unsigned long start, - unsigned long end, - struct mm_walk *walk) -{ - struct migrate_vma *migrate = walk->private; - struct vm_area_struct *vma = walk->vma; - struct mm_struct *mm = vma->vm_mm; - unsigned long addr = start, unmapped = 0; - spinlock_t *ptl; - struct folio *fault_folio; - pte_t *ptep; - -again: - /* The fault page is in another folio once its folio has been split */ - fault_folio = migrate->fault_page ? - page_folio(migrate->fault_page) : NULL; - - /* [start, addr) is collected if we get here after splitting a folio */ - if (pmd_trans_huge(*pmdp) || !pmd_present(*pmdp)) { - int ret = migrate_vma_collect_huge_pmd(pmdp, addr, end, walk, fault_folio); - - if (ret == -EAGAIN) - goto again; - if (ret == 0) { - if (unmapped) - flush_tlb_range(walk->vma, start, end); - return 0; - } - } - - ptep = pte_offset_map_lock(mm, pmdp, start, &ptl); - if (!ptep) - goto again; - lazy_mmu_mode_enable(); - ptep += (addr - start) / PAGE_SIZE; - - for (; addr < end; addr += PAGE_SIZE, ptep++) { - struct dev_pagemap *pgmap; - unsigned long mpfn = 0, pfn; - struct folio *folio; - struct page *page; - softleaf_t entry; - pte_t pte; - - pte = ptep_get(ptep); - - if (pte_none(pte)) { - if (vma_is_anonymous(vma)) { - mpfn = MIGRATE_PFN_MIGRATE; - migrate->cpages++; - } - goto next; - } - - if (!pte_present(pte)) { - /* - * Only care about unaddressable device page special - * page table entry. Other special swap entries are not - * migratable, and we ignore regular swapped page. - */ - entry = softleaf_from_pte(pte); - if (!softleaf_is_device_private(entry)) - goto next; - - page = softleaf_to_page(entry); - pgmap = page_pgmap(page); - if (!(migrate->flags & - MIGRATE_VMA_SELECT_DEVICE_PRIVATE) || - pgmap->owner != migrate->pgmap_owner) - goto next; - - folio = page_folio(page); - if (folio_test_large(folio)) { - int ret; - - /* migrate_vma_split_folio() consumes this reference */ - if (folio != fault_folio) - folio_get(folio); - lazy_mmu_mode_disable(); - pte_unmap_unlock(ptep, ptl); - ret = migrate_vma_split_folio(folio, - migrate->fault_page); - - if (ret) { - if (unmapped) - flush_tlb_range(walk->vma, start, end); - - return migrate_vma_collect_skip(addr, end, walk); - } - - goto again; - } - - mpfn = migrate_pfn(page_to_pfn(page)) | - MIGRATE_PFN_MIGRATE; - if (softleaf_is_device_private_write(entry)) - mpfn |= MIGRATE_PFN_WRITE; - } else { - pfn = pte_pfn(pte); - if (is_zero_pfn(pfn) && - (migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) { - mpfn = MIGRATE_PFN_MIGRATE; - migrate->cpages++; - goto next; - } - page = vm_normal_page(migrate->vma, addr, pte); - if (page && !is_zone_device_page(page) && - !(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) { - goto next; - } else if (page && is_device_coherent_page(page)) { - pgmap = page_pgmap(page); - - if (!(migrate->flags & - MIGRATE_VMA_SELECT_DEVICE_COHERENT) || - pgmap->owner != migrate->pgmap_owner) - goto next; - } - folio = page ? page_folio(page) : NULL; - if (folio && folio_test_large(folio)) { - int ret; - - /* migrate_vma_split_folio() consumes this reference */ - if (folio != fault_folio) - folio_get(folio); - lazy_mmu_mode_disable(); - pte_unmap_unlock(ptep, ptl); - ret = migrate_vma_split_folio(folio, - migrate->fault_page); - - if (ret) { - if (unmapped) - flush_tlb_range(walk->vma, start, end); - - return migrate_vma_collect_skip(addr, end, walk); - } - - goto again; - } - mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE; - mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0; - } - - if (!page || !page->mapping) { - mpfn = 0; - goto next; - } - - /* - * By getting a reference on the folio we pin it and that blocks - * any kind of migration. Side effect is that it "freezes" the - * pte. - * - * We drop this reference after isolating the folio from the lru - * for non device folio (device folio are not on the lru and thus - * can't be dropped from it). - */ - folio = page_folio(page); - folio_get(folio); - - /* - * We rely on folio_trylock() to avoid deadlock between - * concurrent migrations where each is waiting on the others - * folio lock. If we can't immediately lock the folio we fail this - * migration as it is only best effort anyway. - * - * If we can lock the folio it's safe to set up a migration entry - * now. In the common case where the folio is mapped once in a - * single process setting up the migration entry now is an - * optimisation to avoid walking the rmap later with - * try_to_migrate(). - */ - if (fault_folio == folio || folio_trylock(folio)) { - bool anon_exclusive; - pte_t swp_pte; - - if (pte_present(pte)) - flush_cache_page(vma, addr, pte_pfn(pte)); - anon_exclusive = folio_test_anon(folio) && - PageAnonExclusive(page); - if (anon_exclusive) { - pte = ptep_clear_flush(vma, addr, ptep); - - if (folio_try_share_anon_rmap_pte(folio, page)) { - set_pte_at(mm, addr, ptep, pte); - if (fault_folio != folio) - folio_unlock(folio); - folio_put(folio); - mpfn = 0; - goto next; - } - } else { - pte = ptep_get_and_clear(mm, addr, ptep); - } - - migrate->cpages++; - - /* Set the dirty flag on the folio now the pte is gone. */ - if (pte_present(pte) && pte_dirty(pte)) - folio_mark_dirty(folio); - - /* Setup special migration page table entry */ - if (mpfn & MIGRATE_PFN_WRITE) - entry = make_writable_migration_entry( - page_to_pfn(page)); - else if (anon_exclusive) - entry = make_readable_exclusive_migration_entry( - page_to_pfn(page)); - else - entry = make_readable_migration_entry( - page_to_pfn(page)); - if (pte_present(pte)) { - if (pte_young(pte)) - entry = make_migration_entry_young(entry); - if (pte_dirty(pte)) - entry = make_migration_entry_dirty(entry); - } - swp_pte = swp_entry_to_pte(entry); - if (pte_present(pte)) { - if (pte_soft_dirty(pte)) - swp_pte = pte_swp_mksoft_dirty(swp_pte); - if (pte_uffd(pte)) - swp_pte = pte_swp_mkuffd(swp_pte); - } else { - if (pte_swp_soft_dirty(pte)) - swp_pte = pte_swp_mksoft_dirty(swp_pte); - if (pte_swp_uffd(pte)) - swp_pte = pte_swp_mkuffd(swp_pte); - } - set_pte_at(mm, addr, ptep, swp_pte); - - /* - * This is like regular unmap: we remove the rmap and - * drop the folio refcount. The folio won't be freed, as - * we took a reference just above. - */ - folio_remove_rmap_pte(folio, page, vma); - folio_put(folio); - - if (pte_present(pte)) - unmapped++; - } else { - folio_put(folio); - mpfn = 0; - } - -next: - migrate->dst[migrate->npages] = 0; - migrate->src[migrate->npages++] = mpfn; - } - - /* Only flush the TLB if we actually modified any entries */ - if (unmapped) - flush_tlb_range(walk->vma, start, end); - - lazy_mmu_mode_disable(); - pte_unmap_unlock(ptep - 1, ptl); - - return 0; -} - -static const struct mm_walk_ops migrate_vma_walk_ops = { - .pmd_entry = migrate_vma_collect_pmd, - .pte_hole = migrate_vma_collect_hole, - .walk_lock = PGWALK_RDLOCK, -}; - -/* - * migrate_vma_collect() - collect pages over a range of virtual addresses - * @migrate: migrate struct containing all migration information - * - * This will walk the CPU page table. For each virtual address backed by a - * valid page, it updates the src array and takes a reference on the page, in - * order to pin the page until we lock it and unmap it. - */ -static void migrate_vma_collect(struct migrate_vma *migrate) -{ - struct mmu_notifier_range range; - - /* - * Note that the pgmap_owner is passed to the mmu notifier callback so - * that the registered device driver can skip invalidating device - * private page mappings that won't be migrated. - */ - mmu_notifier_range_init_owner(&range, MMU_NOTIFY_MIGRATE, 0, - migrate->vma->vm_mm, migrate->start, migrate->end, - migrate->pgmap_owner); - mmu_notifier_invalidate_range_start(&range); - - walk_page_range_vma(migrate->vma, migrate->start, migrate->end, - &migrate_vma_walk_ops, migrate); - - mmu_notifier_invalidate_range_end(&range); - migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT); -} - /* * migrate_vma_check_page() - check if page is pinned or not * @page: struct page to check @@ -533,8 +32,8 @@ static bool migrate_vma_check_page(struct page *page, struct page *fault_page) /* * One extra ref because caller holds an extra reference, either from - * folio_isolate_lru() for a regular folio, or migrate_vma_collect() for - * a device folio. + * folio_isolate_lru() for a regular folio, or the migration collection + * in mm/hmm.c for a device folio. */ int extra = 1 + (page == fault_page); @@ -687,8 +186,12 @@ static void migrate_vma_unmap(struct migrate_vma *migrate) * migrate_vma_setup() - prepare to migrate a range of memory * @args: contains the vma, start, and pfns arrays for the migration * - * Returns: negative errno on failures, 0 when 0 or more pages were migrated - * without an error. + * Returns: -EINVAL on invalid arguments, -EFAULT for a VM_IO or VM_PFNMAP + * vma; with MIGRATE_VMA_FAULT or MIGRATE_VMA_WRITE, the hmm_range_fault() + * error (e.g. -EFAULT, -EPERM, -ENOMEM) if a fault fails, or -EINTR if a + * fatal signal is pending after a fault; 0 otherwise. A 0 return does not + * imply anything was collected; on return @args->cpages holds the number of + * pages prepared for migration (possibly 0). See the note on faulting below. * * Prepare to migrate a range of memory virtual address range by collecting all * the pages backing each virtual address in the range, saving them inside the @@ -698,6 +201,29 @@ static void migrate_vma_unmap(struct migrate_vma *migrate) * corresponding src array entry. Then restores any pages that are pinned, by * remapping and unlocking those pages. * + * By default migrate_vma_setup() is best-effort: virtual addresses that cannot + * be collected are left with an empty src[] entry and the rest of the range + * still proceeds. Only anonymous memory is collected. + * + * The optional flags MIGRATE_VMA_FAULT and MIGRATE_VMA_WRITE in @args->flags + * additionally fault in missing pages before collecting them, for read and for + * write respectively. If such a fault fails, the whole range is aborted: the + * partial collection is rolled back, @args->src is cleared, @args->cpages and + * @args->npages are reset to 0, and migrate_vma_setup() returns the error. + * Pages that just can't be collected (a folio that can't be locked or split, + * an entry under another migration) don't abort the range; their src[] + * entries stay empty, as without the flags. 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. + * + * The faults are those the CPU would take: empty entries of anonymous memory + * get system pages, which are then collected and copied, rather than being + * left empty for device memory as below. In a vma that isn't anonymous a fault + * can have side effects, such as dirtying page cache, while only anonymous + * folios are collected. The fault flags can't be combined with + * @args->fault_page. + * * The caller should then allocate destination memory and copy source memory to * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE * flag set). Once these are allocated and copied, the caller must update each @@ -712,14 +238,14 @@ static void migrate_vma_unmap(struct migrate_vma *migrate) * consequences for the userspace process, so it must be avoided if at all * possible. * - * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we - * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus - * allowing the caller to allocate device memory for those unbacked virtual - * addresses. For this the caller simply has to allocate device memory and - * properly set the destination entry like for regular migration. Note that - * this can still fail, and thus inside the device driver you must check if the - * migration was successful for those entries after calling migrate_vma_pages(), - * just like for regular migration. + * For empty entries inside CPU page table (pte_none() or pmd_none() is true), + * unless the fault flags fault them in, we do set MIGRATE_PFN_MIGRATE flag + * inside the corresponding source array thus allowing the caller to allocate + * device memory for those unbacked virtual addresses. For this the caller + * simply has to allocate device memory and properly set the destination entry + * like for regular migration. Note that this can still fail, and thus inside + * the device driver you must check if the migration was successful for those + * entries after calling migrate_vma_pages(), just like for regular migration. * * After that, the callers must call migrate_vma_pages() to go over each entry * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag @@ -751,14 +277,27 @@ int migrate_vma_setup(struct migrate_vma *args) { const struct vm_area_struct *vma = args->vma; long nr_pages = (args->end - args->start) >> PAGE_SHIFT; + struct hmm_range range = { + .notifier = NULL, + .hmm_pfns = args->src, + .dev_private_owner = args->pgmap_owner, + .migrate = args, + .default_flags = HMM_PFN_REQ_MIGRATE + }; + int ret; args->start &= PAGE_MASK; args->end &= PAGE_MASK; + range.start = args->start; + range.end = args->end; + if (!vma) return -EINVAL; /* The migrate_vma page walk has no hugetlb support. */ if (!vma_is_mm_backed(vma) || vma_is_hugetlb(vma) || vma_is_dax(vma)) return -EINVAL; + if (vma_test_single_mask(vma, VMA_SHADOW_STACK)) + return -EINVAL; if (nr_pages <= 0) return -EINVAL; if (args->start < vma->vm_start || args->start >= vma->vm_end) @@ -771,15 +310,37 @@ int migrate_vma_setup(struct migrate_vma *args) return -EINVAL; if (args->fault_page && !PageLocked(args->fault_page)) return -EINVAL; + /* + * From migrate_to_ram() the fault page is locked by do_swap_page(): + * don't start nested faults under it. + */ + if (args->fault_page && + (args->flags & (MIGRATE_VMA_FAULT | MIGRATE_VMA_WRITE))) + return -EINVAL; memset(args->src, 0, sizeof(*args->src) * nr_pages); args->cpages = 0; args->npages = 0; - migrate_vma_collect(args); + if (args->flags & MIGRATE_VMA_FAULT) + range.default_flags |= HMM_PFN_REQ_FAULT; - if (args->cpages) - migrate_vma_unmap(args); + if (args->flags & MIGRATE_VMA_WRITE) + range.default_flags |= HMM_PFN_REQ_FAULT | HMM_PFN_REQ_WRITE; + + ret = hmm_range_fault(&range); + + migrate_hmm_range_setup(&range); + + /* Remove migration PTEs */ + if (ret) { + migrate_vma_pages(args); + migrate_vma_finalize(args); + memset(args->src, 0, sizeof(*args->src) * nr_pages); + args->cpages = 0; + args->npages = 0; + return ret; + } /* * At this point pages are locked and unmapped, and thus they have -- 2.55.0