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=1796100976; b=wdV4sQ8LrEpPo0cSJWYROnuZ0SxHUwToyClV9++yDcUKM3EhhZQpiFU5PPpoebHtWtbn RGGsWrA0M2oIPPhNUjlDrWhctIRw7Pk+NYpRh0Ho0ecVRAOSP62dm+Z4xwP8YJ72bd+gb 21fFW+R6U+ONejYuLCN72tFL3l+ZTB0Z5fDFaQnpsdQJU+hI0QfudeEwfRmk9DAqN+GB4 FOGsHSltT7WVnzCQg9FdfFikiHWDOffaX2TCaT8etHBnOPGF/sDldrAGKG39oJGTEn7kt f3ABsCaiuSJJNcsDtiIBroxwc5h1UQ7ZnD8z51SrZPGfwYvQALOiWsXjc5rkEX5rMvg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100976; 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=laVt10sHTs21yBsMBCBmjRgixgW90dfnDOcOM0SYK5o=; b=O90vAAsISYBFGYB5c/84dNv5DSMon5HWfhMXUEwJI9+gxfOwdT8T0DEPwFpFr4L2MyVY 5Qt74POofhgE6Wmd7/zWfEvqqp3Qunizc7PealOgZEnh9ZM66aBbbsAPyMxRo3w9cimn0 bkx41PtkvP04gIjFMvMMgONTb9UaJ2kNiem4egFN8Ihc4MJ3o5hDAbtPer5yXWz/Y54vT JxEYCm0IVfGCnZQu7APqGTqWYTSfyj/bvPBWdigRbiNE1Uiqz8w3fSEdZPAZPM4uL+BNA JgJ/Z1n8m0egW9R5GBYt816l7ONRBKtOiuEhNkfOL1bjSlACUz4vrlORvsb16NSEqlg== 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=WP6SS1jI; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=Db+u718y; 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 42CC9606A327 for ; Tue, 1 Dec 2026 04:56:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 42CC9606A327 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=WP6SS1jI; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=Db+u718y DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796100975; 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=laVt10sHTs21yBsMBCBmjRgixgW90dfnDOcOM0SYK5o=; b=WP6SS1jIjAVdQLcgnV4p3GN98Z5M/w54A3wkCNLzHetcKj/EnJx+mnTVuFGpCE2Jwyfnri AM0XcHZngFtstRuqxCB2rNMs/jEwt2BmpI+EIqotsmZE5quKGrLlBSK0BMpPMb8EnbXmh5 vH+3BZPdjqXH3scXj3yG2VIdNIjskSU= Received: from mail-lf1-f70.google.com (mail-lf1-f70.google.com [209.85.167.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-204-Wpz2NyuHOvybPC_SH-2KZg-1; Tue, 1 Dec 2026 04:56:10 +0000 X-MC-Unique: Wpz2NyuHOvybPC_SH-2KZg-1 X-Mimecast-MFC-AGG-ID: Wpz2NyuHOvybPC_SH-2KZg_1796100969 Received: by mail-lf1-f70.google.com with SMTP id 2adb3069b0e04-5c319d442b6so3629762e87.3 for ; Mon, 30 Nov 2026 20:56:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1796100969; x=1796705769; 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=laVt10sHTs21yBsMBCBmjRgixgW90dfnDOcOM0SYK5o=; b=Db+u718ym1FEKESVfFsfbIENnmvyGV62kcH87wwbsqlR5JjPMzqY2gN7HQJlQvUl/c mAr4H58FMzM+AwMk5lRlf4Ftv7rTzue0SolHt762lHHhyp7Q92epdNmP2pX35XtHd8Xm +XPRg9g/ZgBdAjOgM6UtApU76Y9XZZOa+xdK1rjH3UZK862Fi3lBkhPXyKhf74/hvzca iT9/s/eS31aSPa60JHJ52ceP+dcfeEVHv2Q39rSRUP+ls9ZFX7DdGB7sWOX5Hti1ZJOB 2t7x/eq867HMASODy578unts4w2RscoZlFqQjCR2mqeUxNtsHnrNTR5t28/n0Sz3GmDr SfcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100969; x=1796705769; 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=laVt10sHTs21yBsMBCBmjRgixgW90dfnDOcOM0SYK5o=; b=BjO9hZQsc5bn1046xaMxh+KS972T/ZAfTDY5S1NuODgT3+MbjVAm2vT7RbDz3QfJHh +23GExoZHTy4WmOHoWKmQxw3vJFDCbRKqFga7IBEQBEOVF9pHsr6ZcBHTS/DqI6BTnmw zps2TFiVimplS3ex1hxhIE640m2bRFkmaTmkFpoSPsAGSC8SgPvo+wOmGaRR0+xwZzV3 Dc1v5NIIWXJ3wZMPvkibLOlJkFNvgn1oLWBOk4MGaWfM3M6KOvUBFpjdpIMB7KDRgEFK 2AuU79oUCGLF4LppFntP/T0eZlLG0izrgTDgbP8zkn3kgC79KJW+3QNnYOrm5Ig6Sbmf i2Jw== X-Forwarded-Encrypted: i=1; AL+hErFQoUIAnofJVQGvYWLN3OPkUp3HBtJ6168uqaDE3wBIdZd85qFJXWrbH/P0HidpQ5FC40Syzynzn1RvCuA=@vger.kernel.org X-Gm-Message-State: APuU4Z8y4XgIKPUxcfSQCis+Qmr9qlLuEVnWDb7p71wmZlb/laRMS2x1 SsPVUA8UcsX7NBIKnL9tliqjWwlhmXRS6uHAf/fARSmJc6mUs5wkBdTw+i6A+/FYLs3SKNbZ8f9 wW3FagX2jPJ4PvcX1qdOPUr9uv9TQisoehjsjTnsecd4kh0CWGAIbQ7dOpnM599DS X-Gm-Gg: AZFdTZeaSExm9jkV605BIl8VWmhikbQFFARy3WG1LVopMPmCbIJLkt0hmokbypYGDfg h6+W4DMKZkQliP21tYs2Oq/m1Mqbq9BDuUR0OndbvJ0dwj83PlA1F5ZlqR8jQxb02cVaxa3FCdy BRcVwN5DykdFVUm3X7IerrQDEOV0WbzBfMxADAGfXdZeCQYvAWvDbq+Tg2Y0kt+airOf4z1Afc+ 4PAW157NlbUnyixRgX8HfWiwY4S9C07Y9pda+G8LiYV4Z5fH8adrlRLXyuISuuoJtMcoqQLGXVc 5JKKXp47dql1ROrDn1lp08AQqgaHN6qjfcqHaIT35XTowBqBdTTFBlB0ROxz2xElR+7BjfxAeLY g9XLvu3sf4F2eUK0xLlv7bmy9xUZYSZnk465p/IWtyLUDzoTuxMwnwinwpdkIODYnckeFYA== X-Received: by 2002:a05:6512:1585:b0:5c0:4e3:a416 with SMTP id 2adb3069b0e04-5c1bc68009emr9798889e87.1.1796100968755; Mon, 30 Nov 2026 20:56:08 -0800 (PST) X-Received: by 2002:a05:6512:1585:b0:5c0:4e3:a416 with SMTP id 2adb3069b0e04-5c1bc68009emr9798864e87.1.1796100968253; Mon, 30 Nov 2026 20:56:08 -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.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:56:06 -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 06/14] mm/hmm: migrate collection in HMM pagewalk - pte level Date: Tue, 1 Dec 2026 06:55:23 +0200 Message-ID: <20261201045531.3622535-7-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ä Implement the needed hmm_vma_handle_migrate_prepare() function which is mostly carried over from migrate_device.c's migrate_vma_collect_pmd() function. Also implement hmm_migrate_split_folio(), for splitting pte mapped large folios. Compared with migrate_vma_split_folio() in migrate_device.c it takes the folio reference before dropping the page table lock, only trylocks the folio, and splits the fault folio at the fault page with split_huge_page(), so the caller's lock and reference stay on the folio holding the fault page. The old migrate_vma_collect*() code is removed once migration switches over to the HMM pagewalk, later in the series. As in the pending "mm/migrate_device: do not migrate a uffd-protected zero page", a zero page whose uffd state would be lost is not collected. As in Chris Mason's pending fix for migrate_vma_collect_pmd(), a KSM folio is not collected: KSM maps a KSM folio before rmap walks can find the mapping, so rmap_walk_ksm() may miss the pte when restoring the migration entry, leaving a stale one behind. With HMM pagewalk based migration, the idea is that hmm_vma_handle_*() are responsible for faulting, and the pfn collecting part. hmm_vma_handle_migrate_prepare*() do the migration decisions (with HMM_PFN_MIGRATE), possibly split folios, and insert migration ptes/pmds. Collected folios stay locked while hmm_range_fault() may still fault other pages of the range. This cannot deadlock: only anonymous folios are collected, so no page cache or invalidate_lock ordering is involved, and when the walk faults it collects only folios mapped by this process alone (PageAnonExclusive), besides the fault folio. A folio shared with another process (e.g. after fork) could be reached through that mapping by another faulting walk, which would sleep on our folio lock while we may sleep on its, so it is left as reported. A write fault breaks the sharing first, so only read faults leave shared folios in place; that is the price of faulting in the same walk. An exclusive folio is reachable only through our migration entry, and walks waiting on each other's migration entries can't form a cycle: a walk stops when it records a fault, so it holds only what it collected before the check that led to the fault, and a walk that finds another walk's migration entry leaves it without waiting, so every wait goes to a walk that checked later (see hmm_migrate_collectable()). Only the faulting pte is faulted, never our own entries, and when faulting a swapcache folio still referenced by other swap entries is not collected. The walk itself only trylocks folio locks, also before splitting a folio. Reclaim and direct compaction under the fault only trylock folio locks too. When the pte lock has to be dropped to split a folio, the pmd may change under us, so the walk re-reads it and continues at the current address, as migrate_vma_setup() has always done: a changed pmd is handled for the whole range, and entries collected before that address stay collected and are released by finalize if their migration ptes are gone. When locking or splitting a folio fails, the rest of the pmd is not walked further, also as before. HMM pagewalk based migration is enabled in later commit, for now hmm_select_migrate() returns MIGRATE_VMA_SELECT_NONE. 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ä --- mm/hmm.c | 380 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 377 insertions(+), 3 deletions(-) diff --git a/mm/hmm.c b/mm/hmm.c index 273d0e364fd7..359ed7c16f29 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -73,6 +73,19 @@ struct hmm_vma_walk { */ #define HMM_FAULT_UNLOCKED -ENOLCK +/* + * Internal results of hmm_vma_handle_migrate_prepare(), consumed by + * hmm_vma_walk_pmd() and never propagated. Both mean the pte lock was + * dropped: HMM_MIGRATE_REWALK after splitting a large folio, so the pmd + * range is walked again; HMM_MIGRATE_SKIP when a large folio can't be + * locked or split, so the rest of the pmd range is skipped (best effort). + */ +#define HMM_MIGRATE_REWALK 1 +#define HMM_MIGRATE_SKIP 2 + +#define HMM_ASSERT_PTE_LOCKED(hmm_vma_walk, locked) \ + WARN_ON_ONCE((hmm_vma_walk)->ptelocked != (locked)) + #define HMM_ASSERT_UNLOCKED(hmm_vma_walk) \ WARN_ON_ONCE((hmm_vma_walk)->ptelocked || \ (hmm_vma_walk)->pmdlocked) @@ -555,6 +568,75 @@ static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start, #endif /* CONFIG_ARCH_HAS_PMD_SOFTLEAVES */ #ifdef CONFIG_DEVICE_MIGRATION +/** + * __hmm_migrate_split_folio() - split a locked folio for migration + * @folio: the folio to split, locked and referenced by the caller + * @fault_page: fault page if any + * + * The fault folio is split at @fault_page, so the caller's lock and reference + * stay on the folio containing @fault_page. For any other folio our lock and + * reference are dropped, whether or not the split succeeds. + * + * Return: 0 on success, or the split_huge_page() / split_folio() error. + */ +static int __hmm_migrate_split_folio(struct folio *folio, + struct page *fault_page) +{ + int ret; + + if (fault_page && folio == page_folio(fault_page)) + return split_huge_page(fault_page); + + /* Found large under the page table lock and locked since: still large */ + ret = folio_test_large(folio) ? split_folio(folio) : 0; + folio_unlock(folio); + folio_put(folio); + return ret; +} + +/** + * hmm_migrate_split_folio() - drop the pte lock and split a THP folio + * @folio: the folio to split + * @fault_page: struct page associated with the fault if any + * @hmm_vma_walk: walk in progress + * @ptep: pte_t * for unmap and unlock ptl + * + * Always drops the pte lock associated with @ptep. + * + * Return: 0 on success, -EBUSY if the folio is locked by someone else, or the + * split_huge_page() / split_folio() error. + */ +static int hmm_migrate_split_folio(struct folio *folio, + struct page *fault_page, + struct hmm_vma_walk *hmm_vma_walk, + pte_t *ptep) +{ + struct folio *fault_folio = fault_page ? page_folio(fault_page) : NULL; + bool locked = true; + + /* + * Take the reference while still holding the PTE lock, so the folio + * cannot be freed once the lock is dropped. As at collection, only + * trylock: other collected folios may be held locked, so sleeping + * here could deadlock. A busy folio is just not migrated. The fault + * folio is already referenced and locked by do_swap_page(). + */ + if (folio != fault_folio) { + folio_get(folio); + locked = folio_trylock(folio); + } + + pte_unmap_unlock(ptep, hmm_vma_walk->ptl); + hmm_vma_walk->ptelocked = false; + + if (!locked) { + folio_put(folio); + return -EBUSY; + } + + return __hmm_migrate_split_folio(folio, fault_page); +} + /* * Due to dropping ptl locks for splitting for instance, would we * overwrite already collected pfns? This could happen when pmd @@ -603,6 +685,29 @@ static void hmm_vma_handle_migrate_prepare_rollback(const struct hmm_vma_walk *h } } +/* + * Collected folios stay locked while hmm_range_fault() faults other pages + * of the range. When faulting, collect only the fault folio and folios + * mapped by this process alone (PageAnonExclusive): a shared folio can be + * reached by another faulting walk, which would sleep on our lock while we + * sleep on its. A write fault breaks the sharing first, so only read faults + * on still shared pages (after fork, or KSM) leave them in place: a rare, + * deliberate trade-off. + * + * An exclusive folio is reachable only through our migration entry, and + * waits on those can't form a cycle: a walk stops when it records a fault + * and leaves other walks' entries alone, so every wait goes to a walk that + * checked later. + */ +static bool hmm_migrate_collectable(const struct hmm_range *range, + struct page *page, + struct folio *fault_folio) +{ + if (!(range->default_flags & HMM_PFN_REQ_FAULT)) + return true; + return page_folio(page) == fault_folio || PageAnonExclusive(page); +} + static int hmm_vma_handle_migrate_prepare_pmd(const struct mm_walk *walk, pmd_t *pmdp, unsigned long start, @@ -613,6 +718,10 @@ static int hmm_vma_handle_migrate_prepare_pmd(const struct mm_walk *walk, return 0; } +/* + * Install migration entries if migration requested, either from fault + * or migrate paths. + */ static int hmm_vma_handle_migrate_prepare(const struct mm_walk *walk, pmd_t *pmdp, pte_t *ptep, @@ -620,8 +729,240 @@ static int hmm_vma_handle_migrate_prepare(const struct mm_walk *walk, unsigned long *hmm_pfn, bool *unmapped) { - /* Collection is implemented by later patches */ + struct hmm_vma_walk *hmm_vma_walk = walk->private; + struct hmm_range *range = hmm_vma_walk->range; + struct migrate_vma *migrate = range->migrate; + struct mm_struct *mm = walk->vma->vm_mm; + struct folio *fault_folio = NULL; + enum migrate_vma_info minfo; + struct dev_pagemap *pgmap; + bool anon_exclusive; + struct folio *folio; + unsigned long pfn; + struct page *page; + softleaf_t entry; + pte_t pte, swp_pte; + bool writable = false; + + /* Do we want to migrate at all? */ + minfo = hmm_select_migrate(range); + if (!minfo) + return 0; + + HMM_ASSERT_PTE_LOCKED(hmm_vma_walk, true); + + fault_folio = migrate->fault_page ? + page_folio(migrate->fault_page) : NULL; + + pte = ptep_get(ptep); + + if (pte_none(pte)) { + if (vma_is_anonymous(walk->vma)) { + *hmm_pfn &= HMM_PFN_INOUT_FLAGS; + *hmm_pfn |= HMM_PFN_MIGRATE; + goto out; + } + } + + /* + * NUMA hinting (protnone) entries are not reported valid, but the + * page can still be migrated as is. + */ + if (!(hmm_pfn[0] & HMM_PFN_VALID) && + !(pte_present(pte) && pte_protnone(pte))) + goto out; + + 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 out; + + if (!(minfo & MIGRATE_VMA_SELECT_DEVICE_PRIVATE)) + goto out; + + page = softleaf_to_page(entry); + folio = page_folio(page); + if (folio->pgmap->owner != migrate->pgmap_owner) + goto out; + + pfn = page_to_pfn(page); + if (softleaf_is_device_private_write(entry)) + writable = true; + } else { + pfn = pte_pfn(pte); + if (is_zero_pfn(pfn) && + (minfo & MIGRATE_VMA_SELECT_SYSTEM)) { + /* Like pte_none(): only anonymous memory can be populated */ + if (!vma_is_anonymous(walk->vma)) + goto out; + /* Nothing carries the uffd state of the zero page over */ + if (userfaultfd_protected(walk->vma) && pte_uffd(pte)) + goto out; + *hmm_pfn &= HMM_PFN_INOUT_FLAGS; + *hmm_pfn |= HMM_PFN_MIGRATE; + goto out; + } + page = vm_normal_page(walk->vma, addr, pte); + if (page && !is_zone_device_page(page) && + !(minfo & MIGRATE_VMA_SELECT_SYSTEM)) { + goto out; + } else if (page && is_device_coherent_page(page)) { + pgmap = page_pgmap(page); + + if (!(minfo & + MIGRATE_VMA_SELECT_DEVICE_COHERENT) || + pgmap->owner != migrate->pgmap_owner) + goto out; + } + + folio = page ? page_folio(page) : NULL; + /* + * Only anonymous memory can be migrated to device memory. + * Check before splitting. + */ + if (!folio || !folio_test_anon(folio)) + goto out; + + writable = pte_write(pte); + } + + if (!hmm_migrate_collectable(range, page, fault_folio)) + goto out; + if (folio_test_large(folio)) { + int ret; + + ret = hmm_migrate_split_folio(folio, migrate->fault_page, + hmm_vma_walk, ptep); + if (ret) + goto out_skip; + return HMM_MIGRATE_REWALK; + } + + /* + * 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)) { + /* + * When faulting, a swapcache folio still referenced by other + * swap entries isn't collected: faulting one of those entries + * later in the walk, with no retry allowed, would block on the + * lock we hold. It couldn't be migrated anyway (see + * __migrate_device_pages()). Without faulting the swap cache is + * left alone here and freed only when the folio is migrated. + */ + if ((range->default_flags & HMM_PFN_REQ_FAULT) && + folio_test_swapcache(folio) && !folio_free_swap(folio)) { + if (fault_folio != folio) + folio_unlock(folio); + folio_put(folio); + goto out; + } + + /* + * rmap_walk_ksm() may not find this pte to restore the + * migration entry later. + */ + if (folio_test_ksm(folio)) { + if (fault_folio != folio) + folio_unlock(folio); + folio_put(folio); + goto out; + } + + anon_exclusive = folio_test_anon(folio) && + PageAnonExclusive(page); + + if (pte_present(pte)) + flush_cache_page(walk->vma, addr, pfn); + + if (anon_exclusive) { + pte = ptep_clear_flush(walk->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); + goto out; + } + } else { + pte = ptep_get_and_clear(mm, addr, ptep); + } + + if (pte_present(pte) && pte_dirty(pte)) + folio_mark_dirty(folio); + + /* Setup special migration page table entry */ + if (writable) + entry = make_writable_migration_entry(pfn); + else if (anon_exclusive) + entry = make_readable_exclusive_migration_entry(pfn); + else + entry = make_readable_migration_entry(pfn); + + 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); + folio_remove_rmap_pte(folio, page, walk->vma); + folio_put(folio); + *hmm_pfn |= HMM_PFN_VALID | HMM_PFN_MIGRATE; + if (writable) + *hmm_pfn |= HMM_PFN_WRITE; + if (pte_present(pte)) + *unmapped = true; + } else { + folio_put(folio); + } +out: return 0; +out_skip: + /* Lock or split failed: the caller skips the rest of the pmd (best effort) */ + return HMM_MIGRATE_SKIP; } #else @@ -880,6 +1221,7 @@ static int hmm_vma_walk_pmd(pmd_t *pmdp, } else { ptep = pte_offset_map(pmdp, addr); } + /* The pte table is gone: re-read the pmd and continue at addr */ if (!ptep) goto again; @@ -893,8 +1235,40 @@ static int hmm_vma_walk_pmd(pmd_t *pmdp, return r; } - /* Collection is added by later patches: a no-op until then */ - hmm_vma_handle_migrate_prepare(walk, pmdp, ptep, addr, hmm_pfns, &unmapped); + r = hmm_vma_handle_migrate_prepare(walk, pmdp, ptep, addr, hmm_pfns, &unmapped); + if (r == HMM_MIGRATE_REWALK) { + HMM_ASSERT_UNLOCKED(hmm_vma_walk); + if (unmapped) { + flush_tlb_range(walk->vma, start, addr); + unmapped = false; + } + /* + * The pte lock was dropped and the pmd may have changed: + * re-read it and continue at addr, as migrate_vma_setup() + * has always done. A changed pmd is handled for the whole + * range. Entries collected before addr are not revisited: + * if their migration ptes are gone meanwhile, finalize + * releases them (see + * hmm_vma_handle_migrate_prepare_rollback()). + */ + goto again; + } + if (r == HMM_MIGRATE_SKIP) { + /* + * Locking or splitting the folio failed, and the split + * helper already dropped the PTE lock. addr keeps its + * valid pfn. The rest of the pmd range is not walked + * further, as migrate_vma_setup() has always done: + * entries there keep what an earlier pass wrote, and + * entries already collected stay collected. When + * migrating, collection and faulting are best effort + * (see hmm_range_fault()). + */ + HMM_ASSERT_UNLOCKED(hmm_vma_walk); + if (unmapped) + flush_tlb_range(walk->vma, start, addr); + return 0; + } } if (unmapped) -- 2.55.0