From: Balbir Singh <balbirs@nvidia.com>
To: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org, akpm@linux-foundation.org,
Balbir Singh <balbirs@nvidia.com>,
David Hildenbrand <david@kernel.org>,
Lorenzo Stoakes <lorenzo.stoakes@oracle.com>,
Zi Yan <ziy@nvidia.com>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
"Liam R. Howlett" <Liam.Howlett@oracle.com>,
Nico Pache <npache@redhat.com>,
Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
Barry Song <baohua@kernel.org>, Lance Yang <lance.yang@linux.dev>
Subject: [PATCH] mm/huge_memory: softleaf device private fixes in remove_migration_pmd()
Date: Wed, 12 Nov 2025 15:46:34 +1100 [thread overview]
Message-ID: <20251112044634.963360-2-balbirs@nvidia.com> (raw)
In-Reply-To: <20251112044634.963360-1-balbirs@nvidia.com>
commit a6ca2ba46390 ("mm: replace pmd_to_swp_entry() with softleaf_from_pmd()")
does not work with device private THP entries. softleaf_is_migration_young()
asserts that the entry be a migration entry, but in the current code, the
entry might already be replaced by a device private entry by the time the
check is made. The issue exists with commit
7385dbdbf841 ("mm/rmap: extend rmap and migration support device-private entries")
Fix this by processing the migration entries prior to conversion to
device private if the folio is device private.
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: David Hildenbrand <david@kernel.org>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Zi Yan <ziy@nvidia.com>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: "Liam R. Howlett" <Liam.Howlett@oracle.com>
Cc: Nico Pache <npache@redhat.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Dev Jain <dev.jain@arm.com>
Cc: Barry Song <baohua@kernel.org>
Cc: Lance Yang <lance.yang@linux.dev>
Signed-off-by: Balbir Singh <balbirs@nvidia.com>
---
mm/huge_memory.c | 25 ++++++++++++++++++-------
1 file changed, 18 insertions(+), 7 deletions(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 942bd8410c54..82b019205216 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -4939,6 +4939,7 @@ void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new)
unsigned long haddr = address & HPAGE_PMD_MASK;
pmd_t pmde;
softleaf_t entry;
+ bool old = false, dirty = false, migration_read_entry = false;
if (!(pvmw->pmd && !pvmw->pte))
return;
@@ -4947,6 +4948,19 @@ void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new)
folio_get(folio);
pmde = folio_mk_pmd(folio, READ_ONCE(vma->vm_page_prot));
+ if (!softleaf_is_migration_young(entry))
+ old = true;
+
+ /* NOTE: this may contain setting soft-dirty on some archs */
+ if (folio_test_dirty(folio) && softleaf_is_migration_dirty(entry))
+ dirty = true;
+
+ if (softleaf_is_migration_write(entry))
+ pmde = pmd_mkwrite(pmde, vma);
+
+ if (!softleaf_is_migration_read(entry))
+ migration_read_entry = true;
+
if (folio_is_device_private(folio)) {
if (pmd_write(pmde))
entry = make_writable_device_private_entry(
@@ -4959,20 +4973,17 @@ void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new)
if (pmd_swp_soft_dirty(*pvmw->pmd))
pmde = pmd_mksoft_dirty(pmde);
- if (softleaf_is_migration_write(entry))
- pmde = pmd_mkwrite(pmde, vma);
+ if (old)
+ pmde = pmd_mkold(pmde);
if (pmd_swp_uffd_wp(*pvmw->pmd))
pmde = pmd_mkuffd_wp(pmde);
- if (!softleaf_is_migration_young(entry))
- pmde = pmd_mkold(pmde);
- /* NOTE: this may contain setting soft-dirty on some archs */
- if (folio_test_dirty(folio) && softleaf_is_migration_dirty(entry))
+ if (dirty)
pmde = pmd_mkdirty(pmde);
if (folio_test_anon(folio)) {
rmap_t rmap_flags = RMAP_NONE;
- if (!softleaf_is_migration_read(entry))
+ if (migration_read_entry)
rmap_flags |= RMAP_EXCLUSIVE;
folio_add_anon_rmap_pmd(folio, new, vma, haddr, rmap_flags);
--
2.51.1
next prev parent reply other threads:[~2025-11-12 4:47 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-12 4:46 [PATCH] mm/huge_memory.c: introduce split_unmapped_folio_to_order Balbir Singh
2025-11-12 4:46 ` Balbir Singh [this message]
2025-11-12 11:37 ` [PATCH] mm/huge_memory: softleaf device private fixes in remove_migration_pmd() David Hildenbrand (Red Hat)
2025-11-13 5:03 ` Balbir Singh
2025-11-13 7:32 ` David Hildenbrand (Red Hat)
2025-11-12 13:43 ` Lorenzo Stoakes
2025-11-12 21:07 ` Balbir Singh
2025-11-12 23:55 ` Balbir Singh
2025-11-12 10:00 ` [PATCH] mm/huge_memory.c: introduce split_unmapped_folio_to_order David Hildenbrand (Red Hat)
2025-11-12 10:17 ` Balbir Singh
2025-11-12 11:34 ` David Hildenbrand (Red Hat)
2025-11-12 23:49 ` Balbir Singh
2025-11-13 21:39 ` Balbir Singh
2025-11-13 21:45 ` Zi Yan
2025-11-13 21:56 ` Balbir Singh
2025-11-14 0:23 ` Zi Yan
2025-11-18 20:17 ` David Hildenbrand (Red Hat)
2025-11-13 15:36 ` Francois Dugast
2025-11-13 16:02 ` Lorenzo Stoakes
2025-11-13 16:24 ` Zi Yan
2025-11-13 19:07 ` David Hildenbrand (Red Hat)
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=20251112044634.963360-2-balbirs@nvidia.com \
--to=balbirs@nvidia.com \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=lance.yang@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=npache@redhat.com \
--cc=ryan.roberts@arm.com \
--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