Skip to contentSkip to conversation

mm/huge_memory: check pmd_special() only after pmd_present()

PATCH Applied David Hildenbrand 2 participants 3 messages Sep 26, 2024 – Sep 30, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/huge_memory: check pmd_special() only after pmd_present()

We should only check for pmd_special() after we made sure that we have a present PMD. For example, if we have a migration PMD, pmd_special() might indicate that we have a special PMD although we really don't.

This fixes confusing migration entries as PFN mappings, and not doing what we are supposed to do in the "is_swap_pmd()" case further down in the function -- including messing up COW, page table handling and accounting.

Reported-by [email protected]
Fixes bc02afbd4d73 ("mm/fork: accept huge pfnmap entries")
Cc Peter Xu <[email protected]>

---

I yet have to do more testing, but sending this out already.

---
 mm/huge_memory.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/huge_memory.c +1 -1
index 0580ac9e47b9..e55efcad1e6c 100644
@@ -1586,7 +1586,7 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
 	int ret = -ENOMEM;
 
 	pmd = pmdp_get_lockless(src_pmd);
-	if (unlikely(pmd_special(pmd))) {
+	if (unlikely(pmd_present(pmd) && pmd_special(pmd))) {
 		dst_ptl = pmd_lock(dst_mm, dst_pmd);
 		src_ptl = pmd_lockptr(src_mm, src_pmd);
 		spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Signature · 1 line
2.46.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · David Hildenbrand

We should only check for pmd_special() after we made sure that we have a present PMD. For example, if we have a migration PMD, pmd_special() might indicate that we have a special PMD although we really don't.

This fixes confusing migration entries as PFN mappings, and not doing what we are supposed to do in the "is_swap_pmd()" case further down in the function -- including messing up COW, page table handling and accounting.

Quoted 5 lines
Reported-by [email protected]
Fixes bc02afbd4d73 ("mm/fork: accept huge pfnmap entries")
Cc Peter Xu <[email protected]>
Signature · 1 line
Peter Xu
#2 lore raw
In reply to David Hildenbrand
Quoted 10 lines · David Hildenbrand

We should only check for pmd_special() after we made sure that we have a present PMD. For example, if we have a migration PMD, pmd_special() might indicate that we have a special PMD although we really don't.

This fixes confusing migration entries as PFN mappings, and not doing what we are supposed to do in the "is_swap_pmd()" case further down in the function -- including messing up COW, page table handling and accounting.

Quoted 8 lines
Reported-by [email protected]
Fixes bc02afbd4d73 ("mm/fork: accept huge pfnmap entries")
Cc Peter Xu <[email protected]>

---

I yet have to do more testing, but sending this out already.

Testing looks good. Andrew please queue this, it's a rather unpleasent behavior if we fork() with a PMD migration entry that should be fixed upstream soonish :)

(no idea how I could have missed CCing Andrew once more)

Signature · 3 lines
Cheers,

David / dhildenb