[PATCH v1] mm/huge_memory: check pmd_special() only after pmd_present()

David Hildenbrand posted 1 patch 2 months ago
mm/huge_memory.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH v1] mm/huge_memory: check pmd_special() only after pmd_present()
Posted by David Hildenbrand 2 months ago
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: syzbot+bf2c35fa302ebe3c7471@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/lkml/66f15c8d.050a0220.c23dd.000f.GAE@google.com/
Fixes: bc02afbd4d73 ("mm/fork: accept huge pfnmap entries")
Cc: Peter Xu <peterx@redhat.com>
Signed-off-by: David Hildenbrand <david@redhat.com>
---

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

---
 mm/huge_memory.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 0580ac9e47b9..e55efcad1e6c 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -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);
-- 
2.46.1
Re: [PATCH v1] mm/huge_memory: check pmd_special() only after pmd_present()
Posted by David Hildenbrand 1 month, 4 weeks ago
On 26.09.24 17:42, David Hildenbrand wrote:
> 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: syzbot+bf2c35fa302ebe3c7471@syzkaller.appspotmail.com
> Closes: https://lore.kernel.org/lkml/66f15c8d.050a0220.c23dd.000f.GAE@google.com/
> Fixes: bc02afbd4d73 ("mm/fork: accept huge pfnmap entries")
> Cc: Peter Xu <peterx@redhat.com>
> Signed-off-by: David Hildenbrand <david@redhat.com>
> ---
> 
> 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)

-- 
Cheers,

David / dhildenb
Re: [PATCH v1] mm/huge_memory: check pmd_special() only after pmd_present()
Posted by Peter Xu 2 months ago
On Thu, Sep 26, 2024 at 05:42:34PM +0200, David Hildenbrand wrote:
> 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: syzbot+bf2c35fa302ebe3c7471@syzkaller.appspotmail.com
> Closes: https://lore.kernel.org/lkml/66f15c8d.050a0220.c23dd.000f.GAE@google.com/
> Fixes: bc02afbd4d73 ("mm/fork: accept huge pfnmap entries")
> Cc: Peter Xu <peterx@redhat.com>
> Signed-off-by: David Hildenbrand <david@redhat.com>

Reviewed-by: Peter Xu <peterx@redhat.com>

-- 
Peter Xu