mm/hugetlb: avoid recursive i_mmap_rwsem in PMD sharing
Subsystems (3)
PATCH 1/1 mm/hugetlb: avoid recursive i_mmap_rwsem in PMD sharing
huge_pmd_share() always takes i_mmap_rwsem for read before looking for a shareable PMD page table.
That is unsafe for callers that already hold the same mapping lock for write. In particular, move_hugetlb_page_tables() takes i_mmap_rwsem for write to prevent truncation races, then calls huge_pte_alloc() for the destination address. If the destination PUD is empty and PMD sharing is possible, huge_pte_alloc() can call huge_pmd_share(), which then tries to take the same rwsem for read and can deadlock on itself.
PMD sharing is only an optimization. If the read side of i_mmap_rwsem cannot be acquired immediately, fall back to allocating a private PMD table instead of blocking in the sharing path.
--- mm/hugetlb.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
mm/hugetlb.c +12 -2
@@ -6995,7 +6995,15 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,pte_t *spte =NULL ;pte_t *pte; -i_mmap_lock_read (mapping); +/* +* Some callers already hold i_mmap_rwsem for write, for example +* move_hugetlb_page_tables(). PMD sharing is only an optimization, so +* fall back to a private PMD table instead of blocking on the same +* rwsem in read mode. +*/ +if (!i_mmap_trylock_read (mapping)) + goto alloc; + mapping_rmap_tree_foreach(svma, mapping, idx, idx) {if (svma == vma)continue ;
@@ -7024,8 +7032,10 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, }spin_unlock (&mm->page_table_lock); out: - pte = (pte_t *)pmd_alloc (mm, pud, addr);i_mmap_unlock_read (mapping); + +alloc: + pte = (pte_t *)pmd_alloc (mm, pud, addr);return pte; }
Signature · 1 line
2.20.1
Quoted 26 lines · Li Zhe
huge_pmd_share()always takesi_mmap_rwsemfor read before looking for a shareable PMD page table.That is unsafe for callers that already hold the same mapping lock for write. In particular,
move_hugetlb_page_tables()takesi_mmap_rwsemfor write to prevent truncation races, then callshuge_pte_alloc()for the destination address. If the destination PUD is empty and PMD sharing is possible,huge_pte_alloc()can callhuge_pmd_share(), which then tries to take the same rwsem for read and can deadlock on itself.PMD sharing is only an optimization. If the read side of
i_mmap_rwsemcannot be acquired immediately, fall back to allocating a private PMD table instead of blocking in the sharing path.Signed-off-by Li Zhe <[email protected]>--- mm/hugetlb.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)mm/hugetlb.c +0
-0@@ -6995,7 +6995,15 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,pte_t *spte =NULL ;pte_t *pte;
mm/hugetlb.c pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, · line 7005
Quoted 10 lines
mm/hugetlb.c +8
-1-i_mmap_lock_read (mapping); +/* +* Some callers already hold i_mmap_rwsem for write, for example +* move_hugetlb_page_tables(). PMD sharing is only an optimization, so +* fall back to a private PMD table instead of blocking on the same +* rwsem in read mode. +*/ +if (!i_mmap_trylock_read (mapping)) + goto alloc;
try deadlock is not a fallback here. both callers already hold the write lock, and that is enough for the walk, so sharing is never attempted on these paths.
hugetlb_change_protection() does thr same thing on the uffd-wp path and is not in the changelog.
and no Fixes tag either. does this want Cc: stable?
Quoted 16 lines
mm/hugetlb.c +3
-2mapping_rmap_tree_foreach(svma, mapping, idx, idx) {if (svma == vma)continue ;@@ -7024,8 +7032,10 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, }spin_unlock (&mm->page_table_lock); out: - pte = (pte_t *)pmd_alloc (mm, pud, addr);i_mmap_unlock_read (mapping); + +alloc: + pte = (pte_t *)pmd_alloc (mm, pud, addr);return pte; } --
Quoted 2 lines
2.20.1
--
cheers,
jose a. p-a
Quoted 41 lines · Jose A. Perez de Azpillaga
Quoted 26 lines · Li Zhe
huge_pmd_share()always takesi_mmap_rwsemfor read before looking for a shareable PMD page table.That is unsafe for callers that already hold the same mapping lock for write. In particular,
move_hugetlb_page_tables()takesi_mmap_rwsemfor write to prevent truncation races, then callshuge_pte_alloc()for the destination address. If the destination PUD is empty and PMD sharing is possible,huge_pte_alloc()can callhuge_pmd_share(), which then tries to take the same rwsem for read and can deadlock on itself.PMD sharing is only an optimization. If the read side of
i_mmap_rwsemcannot be acquired immediately, fall back to allocating a private PMD table instead of blocking in the sharing path.Signed-off-by Li Zhe <[email protected]>--- mm/hugetlb.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)mm/hugetlb.c +0
-0@@ -6995,7 +6995,15 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,pte_t *spte =NULL ;pte_t *pte;mm/hugetlb.c pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, · line 7005
Quoted 10 lines
mm/hugetlb.c +8
-1-i_mmap_lock_read (mapping); +/* +* Some callers already hold i_mmap_rwsem for write, for example +* move_hugetlb_page_tables(). PMD sharing is only an optimization, so +* fall back to a private PMD table instead of blocking on the same +* rwsem in read mode. +*/ +if (!i_mmap_trylock_read (mapping)) + goto alloc;try deadlock is not a fallback here. both callers already hold the write lock, and that is enough for the walk, so sharing is never attempted on these paths.
Quoted 4 lines
hugetlb_change_protection()does thr same thing on the uffd-wp path and is not in the changelog.and no Fixes tag either. does this want Cc: stable?
Thanks for the review.
I agree that holding the write lock should be enough for the walk, and that avoiding PMD sharing on these paths is not the right direction.
David mentioned that Oscar was planning to send a proper fix for this issue, so I will wait for that patch instead of moving forward with this trylock fallback approach.
Thanks,
Zhe
Quoted 22 lines
Quoted 17 lines
mm/hugetlb.c +3
-2mapping_rmap_tree_foreach(svma, mapping, idx, idx) {if (svma == vma)continue ;@@ -7024,8 +7032,10 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, }spin_unlock (&mm->page_table_lock); out: - pte = (pte_t *)pmd_alloc (mm, pud, addr);i_mmap_unlock_read (mapping); + +alloc: + pte = (pte_t *)pmd_alloc (mm, pud, addr);return pte; } --Quoted 2 lines
2.20.1
--
cheers,
jose a. p-a
David Hildenbrand (Arm)
reviewerQuoted 48 lines · Li Zhe
Quoted 41 lines · Jose A. Perez de Azpillaga
Quoted 26 lines · Li Zhe
huge_pmd_share()always takesi_mmap_rwsemfor read before looking for a shareable PMD page table.That is unsafe for callers that already hold the same mapping lock for write. In particular,
move_hugetlb_page_tables()takesi_mmap_rwsemfor write to prevent truncation races, then callshuge_pte_alloc()for the destination address. If the destination PUD is empty and PMD sharing is possible,huge_pte_alloc()can callhuge_pmd_share(), which then tries to take the same rwsem for read and can deadlock on itself.PMD sharing is only an optimization. If the read side of
i_mmap_rwsemcannot be acquired immediately, fall back to allocating a private PMD table instead of blocking in the sharing path.Signed-off-by Li Zhe <[email protected]>--- mm/hugetlb.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)mm/hugetlb.c +0
-0@@ -6995,7 +6995,15 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,pte_t *spte =NULL ;pte_t *pte;mm/hugetlb.c pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, · line 7005
Quoted 10 lines
mm/hugetlb.c +8
-1-i_mmap_lock_read (mapping); +/* +* Some callers already hold i_mmap_rwsem for write, for example +* move_hugetlb_page_tables(). PMD sharing is only an optimization, so +* fall back to a private PMD table instead of blocking on the same +* rwsem in read mode. +*/ +if (!i_mmap_trylock_read (mapping)) + goto alloc;try deadlock is not a fallback here. both callers already hold the write lock, and that is enough for the walk, so sharing is never attempted on these paths.
Quoted 4 lines
hugetlb_change_protection()does thr same thing on the uffd-wp path and is not in the changelog.and no Fixes tag either. does this want Cc: stable?
Quoted 8 lines
Thanks for the review.
I agree that holding the write lock should be enough for the walk, and that avoiding PMD sharing on these paths is not the right direction.
David mentioned that Oscar was planning to send a proper fix for this issue, so I will wait for that patch instead of moving forward with this trylock fallback approach.
@Lorenzo, if Oscar is too busy, I guess we can paste the overall idea for the fix here as well (publicly)?
Signature · 3 lines
Cheers, David
Quoted 5 lines · David Hildenbrand (Arm)
Quoted 3 lines
David mentioned that Oscar was planning to send a proper fix for this issue, so I will wait for that patch instead of moving forward with this trylock fallback approach.
@Lorenzo, if Oscar is too busy, I guess we can paste the overall idea for the fix here as well (publicly)?
Yeah I don't see why not! I don't think there's anything that needs to be private here.
--
Cheers, Lorenzo
David Hildenbrand (Arm)
reviewerQuoted 9 lines · Lorenzo Stoakes (ARM)
Quoted 5 lines · David Hildenbrand (Arm)
Quoted 3 lines
David mentioned that Oscar was planning to send a proper fix for this issue, so I will wait for that patch instead of moving forward with this trylock fallback approach.
@Lorenzo, if Oscar is too busy, I guess we can paste the overall idea for the fix here as well (publicly)?
Yeah I don't see why not! I don't think there's anything that needs to be private here.
Li, see below, can you work with the below?
----8<----
From 9e1c0676ea6c9cbcabf91cd8badf81a9d88fd3ef Mon Sep 17 00:00:00 2001
From: "Lorenzo Stoakes (ARM)" <[email protected]>
Date: Wed, 29 Jul 2026 18:33:08 +0100
Subject: [PATCH] fix
--- mm/hugetlb.c | 103 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 79 insertions(+), 24 deletions(-)
mm/hugetlb.c +79 -24
@@ -5077,6 +5077,79 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,spin_unlock (dst_ptl); } +/* Returns last address processed */ +static unsigned long prealloc_or_move_page_tables (struct vm_area_struct *vma, +struct vm_area_struct *new_vma,unsigned long old_addr, +unsigned long new_addr,unsigned long len,unsigned long sz, +unsigned long last_addr_mask,struct mmu_gather *tlb, +bool is_prealloc) +{ +unsigned long old_end = old_addr + len; +struct hstate *h =hstate_vma (vma); +struct mm_struct *mm = vma->vm_mm; +pte_t *src_pte, *dst_pte; + +hugetlb_vma_assert_locked (vma); + +for (; old_addr < old_end; old_addr += sz, new_addr += sz) { + src_pte =hugetlb_walk (vma, old_addr, sz); +if (!src_pte) { + old_addr |= last_addr_mask; + new_addr |= last_addr_mask; +continue ; + } +if (huge_pte_none (huge_ptep_get (mm, old_addr, src_pte))) +continue ; + +if (is_prealloc) { +if (!huge_pte_alloc (mm, new_vma, new_addr, sz)) +break ; +continue ; + } + +if (huge_pmd_unshare (tlb, vma, old_addr, src_pte)) { + old_addr |= last_addr_mask; + new_addr |= last_addr_mask; +continue ; + } + + dst_pte =hugetlb_walk (new_vma, new_addr, sz); +if (!dst_pte) +break ; + +move_huge_pte (vma, old_addr, new_addr, src_pte, dst_pte, sz); +tlb_remove_huge_tlb_entry (h, tlb, src_pte, old_addr); + } + +return old_addr; +} + +/* Returns true if preallocation succeeded across range, otherwise false. */ +static bool prealloc_hugetlb_page_tables (struct vm_area_struct *vma, +struct vm_area_struct *new_vma,unsigned long old_addr, +unsigned long new_addr,unsigned long len,unsigned long sz, +unsigned long last_addr_mask) +{ +unsigned long old_end = old_addr + len; +unsigned long addr_end; + + addr_end =prealloc_or_move_page_tables (vma, new_vma, old_addr, new_addr, + len, sz, last_addr_mask,NULL ,/*is_prealloc=*/ true); + +return addr_end >= old_end; +} + +/* Returns last processed address. */ +static unsigned long __move_hugetlb_page_tables (struct vm_area_struct *vma, +struct vm_area_struct *new_vma,unsigned long old_addr, +unsigned long new_addr,unsigned long len,unsigned long sz, +unsigned long last_addr_mask,struct mmu_gather *tlb) +{ +i_mmap_assert_write_locked (vma->vm_file->f_mapping); +return prealloc_or_move_page_tables (vma, new_vma, old_addr, new_addr, + len, sz, last_addr_mask, tlb,/*is_prealloc=*/ false); +} + int move_hugetlb_page_tables(struct vm_area_struct *vma, struct vm_area_struct *new_vma, unsigned long old_addr, unsigned long new_addr,
@@ -5088,9 +5161,9 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,struct mm_struct *mm = vma->vm_mm;unsigned long old_end = old_addr + len;unsigned long last_addr_mask; -pte_t *src_pte, *dst_pte;struct mmu_notifier_range range;struct mmu_gather tlb; +bool preallocated;mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , mm, old_addr, old_end);
@@ -5106,30 +5179,12 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma, last_addr_mask =hugetlb_mask_last_page (h);/* Prevent race with file truncation */ hugetlb_vma_lock_write (vma); + preallocated =prealloc_hugetlb_page_tables (vma, new_vma, old_addr, + new_addr, len, sz, last_addr_mask);i_mmap_lock_write (mapping); -for (; old_addr < old_end; old_addr += sz, new_addr += sz) { - src_pte =hugetlb_walk (vma, old_addr, sz); -if (!src_pte) { - old_addr |= last_addr_mask; - new_addr |= last_addr_mask; -continue ; - } -if (huge_pte_none (huge_ptep_get (mm, old_addr, src_pte))) -continue ; - -if (huge_pmd_unshare (&tlb, vma, old_addr, src_pte)) { - old_addr |= last_addr_mask; - new_addr |= last_addr_mask; -continue ; - } - - dst_pte =huge_pte_alloc (mm, new_vma, new_addr, sz); -if (!dst_pte) -break ; - -move_huge_pte (vma, old_addr, new_addr, src_pte, dst_pte, sz); -tlb_remove_huge_tlb_entry (h, &tlb, src_pte, old_addr); - } +if (preallocated) + old_addr =__move_hugetlb_page_tables (vma, new_vma, old_addr, + new_addr, len, sz, last_addr_mask, &tlb);tlb_flush_mmu_tlbonly (&tlb);huge_pmd_unshare_flush (&tlb, vma);
--
2.55.0
Signature · 3 lines
Cheers, David
Quoted 10 lines · David Hildenbrand (Arm)
Quoted 8 lines · Lorenzo Stoakes (ARM)
Quoted 5 lines · David Hildenbrand (Arm)
Quoted 3 lines
David mentioned that Oscar was planning to send a proper fix for this issue, so I will wait for that patch instead of moving forward with this trylock fallback approach.
@Lorenzo, if Oscar is too busy, I guess we can paste the overall idea for the fix here as well (publicly)?
Yeah I don't see why not! I don't think there's anything that needs to be private here.
Li, see below, can you work with the below?
Thanks for the patch. I tested it with my original mremap reproducer, and I can no longer reproduce the hung task.
I also checked the hugetlb_change_protection() / uffd-wp path Jose mentioned. That path can reach hugetlb_change_protection() and huge_pte_alloc(), but it does not enter huge_pmd_share(), because VM_UFFD_WP makes want_pmd_share() return false via uffd_disable_huge_pmd_share().
My understanding of your patch is that we preallocate the destination hugetlb page tables before taking the mapping's i_mmap_rwsem in write mode. Then, while holding i_mmap_rwsem, we only walk the preallocated destination PTEs and move the source huge PTEs over. This avoids calling huge_pte_alloc(), and therefore huge_pmd_share(), while already holding i_mmap_rwsem for write.
Does that match your intended direction? If so, I will go through the details and make sure there are no issues, then send a v2 based on this approach.
Thanks,
Zhe
Quoted 145 lines
----8<----
From9e1c0676ea6c9cbcabf91cd8badf81a9d88fd3efMon Sep 17 00:00:00 2001
From: "Lorenzo Stoakes (ARM)" <[email protected]>
Date: Wed, 29 Jul 2026 18:33:08 +0100
Subject: [PATCH] fix--- mm/hugetlb.c | 103 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 79 insertions(+), 24 deletions(-)mm/hugetlb.c +79
-25@@ -5077,6 +5077,79 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,spin_unlock (dst_ptl); } +/* Returns last address processed */ +static unsigned long prealloc_or_move_page_tables (struct vm_area_struct *vma, +struct vm_area_struct *new_vma,unsigned long old_addr, +unsigned long new_addr,unsigned long len,unsigned long sz, +unsigned long last_addr_mask,struct mmu_gather *tlb, +bool is_prealloc) +{ +unsigned long old_end = old_addr + len; +struct hstate *h =hstate_vma (vma); +struct mm_struct *mm = vma->vm_mm; +pte_t *src_pte, *dst_pte; + +hugetlb_vma_assert_locked (vma); + +for (; old_addr < old_end; old_addr += sz, new_addr += sz) { + src_pte =hugetlb_walk (vma, old_addr, sz); +if (!src_pte) { + old_addr |= last_addr_mask; + new_addr |= last_addr_mask; +continue ; + } +if (huge_pte_none (huge_ptep_get (mm, old_addr, src_pte))) +continue ; + +if (is_prealloc) { +if (!huge_pte_alloc (mm, new_vma, new_addr, sz)) +break ; +continue ; + } + +if (huge_pmd_unshare (tlb, vma, old_addr, src_pte)) { + old_addr |= last_addr_mask; + new_addr |= last_addr_mask; +continue ; + } + + dst_pte =hugetlb_walk (new_vma, new_addr, sz); +if (!dst_pte) +break ; + +move_huge_pte (vma, old_addr, new_addr, src_pte, dst_pte, sz); +tlb_remove_huge_tlb_entry (h, tlb, src_pte, old_addr); + } + +return old_addr; +} + +/* Returns true if preallocation succeeded across range, otherwise false. */ +static bool prealloc_hugetlb_page_tables (struct vm_area_struct *vma, +struct vm_area_struct *new_vma,unsigned long old_addr, +unsigned long new_addr,unsigned long len,unsigned long sz, +unsigned long last_addr_mask) +{ +unsigned long old_end = old_addr + len; +unsigned long addr_end; + + addr_end =prealloc_or_move_page_tables (vma, new_vma, old_addr, new_addr, + len, sz, last_addr_mask,NULL ,/*is_prealloc=*/ true); + +return addr_end >= old_end; +} + +/* Returns last processed address. */ +static unsigned long __move_hugetlb_page_tables (struct vm_area_struct *vma, +struct vm_area_struct *new_vma,unsigned long old_addr, +unsigned long new_addr,unsigned long len,unsigned long sz, +unsigned long last_addr_mask,struct mmu_gather *tlb) +{ +i_mmap_assert_write_locked (vma->vm_file->f_mapping); +return prealloc_or_move_page_tables (vma, new_vma, old_addr, new_addr, + len, sz, last_addr_mask, tlb,/*is_prealloc=*/ false); +} + int move_hugetlb_page_tables(struct vm_area_struct *vma, struct vm_area_struct *new_vma, unsigned long old_addr, unsigned long new_addr,@@ -5088,9 +5161,9 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,struct mm_struct *mm = vma->vm_mm;unsigned long old_end = old_addr + len;unsigned long last_addr_mask; -pte_t *src_pte, *dst_pte;struct mmu_notifier_range range;struct mmu_gather tlb; +bool preallocated;mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , mm, old_addr, old_end);@@ -5106,30 +5179,12 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma, last_addr_mask =hugetlb_mask_last_page (h);/* Prevent race with file truncation */ hugetlb_vma_lock_write (vma); + preallocated =prealloc_hugetlb_page_tables (vma, new_vma, old_addr, + new_addr, len, sz, last_addr_mask);i_mmap_lock_write (mapping); -for (; old_addr < old_end; old_addr += sz, new_addr += sz) { - src_pte =hugetlb_walk (vma, old_addr, sz); -if (!src_pte) { - old_addr |= last_addr_mask; - new_addr |= last_addr_mask; -continue ; - } -if (huge_pte_none (huge_ptep_get (mm, old_addr, src_pte))) -continue ; - -if (huge_pmd_unshare (&tlb, vma, old_addr, src_pte)) { - old_addr |= last_addr_mask; - new_addr |= last_addr_mask; -continue ; - } - - dst_pte =huge_pte_alloc (mm, new_vma, new_addr, sz); -if (!dst_pte) -break ; - -move_huge_pte (vma, old_addr, new_addr, src_pte, dst_pte, sz); -tlb_remove_huge_tlb_entry (h, &tlb, src_pte, old_addr); - } +if (preallocated) + old_addr =__move_hugetlb_page_tables (vma, new_vma, old_addr, + new_addr, len, sz, last_addr_mask, &tlb);tlb_flush_mmu_tlbonly (&tlb);huge_pmd_unshare_flush (&tlb, vma); --2.55.0
David Hildenbrand (Arm)
reviewerQuoted 23 lines · Li Zhe
Quoted 4 lines · David Hildenbrand (Arm)
Quoted 2 lines · Lorenzo Stoakes (ARM)
Yeah I don't see why not! I don't think there's anything that needs to be private here.
Li, see below, can you work with the below?
Thanks for the patch. I tested it with my original mremap reproducer, and I can no longer reproduce the hung task.
I also checked the
hugetlb_change_protection()/ uffd-wp path Jose mentioned. That path can reachhugetlb_change_protection()andhuge_pte_alloc(), but it does not enterhuge_pmd_share(), becauseVM_UFFD_WPmakeswant_pmd_share()return false viauffd_disable_huge_pmd_share().My understanding of your patch is that we preallocate the destination hugetlb page tables before taking the mapping's
i_mmap_rwsemin write mode. Then, while holdingi_mmap_rwsem, we only walk the preallocated destination PTEs and move the source huge PTEs over. This avoids callinghuge_pte_alloc(), and thereforehuge_pmd_share(), while already holdingi_mmap_rwsemfor write.
Quoted 3 lines
Does that match your intended direction? If so, I will go through the details and make sure there are no issues, then send a v2 based on this approach.
Yes, please. make sure to keep Lorenzo's SOB/authorship. If you perform modifications (patch/commit message) add your co-developed-by + SOB when sending it out.
Signature · 3 lines
Cheers, David
Quoted 154 lines · David Hildenbrand (Arm)
Quoted 8 lines · Lorenzo Stoakes (ARM)
Quoted 5 lines · David Hildenbrand (Arm)
Quoted 3 lines
David mentioned that Oscar was planning to send a proper fix for this issue, so I will wait for that patch instead of moving forward with this trylock fallback approach.
@Lorenzo, if Oscar is too busy, I guess we can paste the overall idea for the fix here as well (publicly)?
Yeah I don't see why not! I don't think there's anything that needs to be private here.
Li, see below, can you work with the below?
----8<----
From9e1c0676ea6c9cbcabf91cd8badf81a9d88fd3efMon Sep 17 00:00:00 2001
From: "Lorenzo Stoakes (ARM)" <[email protected]>
Date: Wed, 29 Jul 2026 18:33:08 +0100
Subject: [PATCH] fix--- mm/hugetlb.c | 103 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 79 insertions(+), 24 deletions(-)mm/hugetlb.c +79
-25@@ -5077,6 +5077,79 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,spin_unlock (dst_ptl); } +/* Returns last address processed */ +static unsigned long prealloc_or_move_page_tables (struct vm_area_struct *vma, +struct vm_area_struct *new_vma,unsigned long old_addr, +unsigned long new_addr,unsigned long len,unsigned long sz, +unsigned long last_addr_mask,struct mmu_gather *tlb, +bool is_prealloc) +{ +unsigned long old_end = old_addr + len; +struct hstate *h =hstate_vma (vma); +struct mm_struct *mm = vma->vm_mm; +pte_t *src_pte, *dst_pte; + +hugetlb_vma_assert_locked (vma); + +for (; old_addr < old_end; old_addr += sz, new_addr += sz) { + src_pte =hugetlb_walk (vma, old_addr, sz); +if (!src_pte) { + old_addr |= last_addr_mask; + new_addr |= last_addr_mask; +continue ; + } +if (huge_pte_none (huge_ptep_get (mm, old_addr, src_pte))) +continue ; + +if (is_prealloc) { +if (!huge_pte_alloc (mm, new_vma, new_addr, sz)) +break ; +continue ; + } + +if (huge_pmd_unshare (tlb, vma, old_addr, src_pte)) { + old_addr |= last_addr_mask; + new_addr |= last_addr_mask; +continue ; + } + + dst_pte =hugetlb_walk (new_vma, new_addr, sz); +if (!dst_pte) +break ; + +move_huge_pte (vma, old_addr, new_addr, src_pte, dst_pte, sz); +tlb_remove_huge_tlb_entry (h, tlb, src_pte, old_addr); + } + +return old_addr; +} + +/* Returns true if preallocation succeeded across range, otherwise false. */ +static bool prealloc_hugetlb_page_tables (struct vm_area_struct *vma, +struct vm_area_struct *new_vma,unsigned long old_addr, +unsigned long new_addr,unsigned long len,unsigned long sz, +unsigned long last_addr_mask) +{ +unsigned long old_end = old_addr + len; +unsigned long addr_end; + + addr_end =prealloc_or_move_page_tables (vma, new_vma, old_addr, new_addr, + len, sz, last_addr_mask,NULL ,/*is_prealloc=*/ true); + +return addr_end >= old_end; +} + +/* Returns last processed address. */ +static unsigned long __move_hugetlb_page_tables (struct vm_area_struct *vma, +struct vm_area_struct *new_vma,unsigned long old_addr, +unsigned long new_addr,unsigned long len,unsigned long sz, +unsigned long last_addr_mask,struct mmu_gather *tlb) +{ +i_mmap_assert_write_locked (vma->vm_file->f_mapping); +return prealloc_or_move_page_tables (vma, new_vma, old_addr, new_addr, + len, sz, last_addr_mask, tlb,/*is_prealloc=*/ false); +} + int move_hugetlb_page_tables(struct vm_area_struct *vma, struct vm_area_struct *new_vma, unsigned long old_addr, unsigned long new_addr,@@ -5088,9 +5161,9 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,struct mm_struct *mm = vma->vm_mm;unsigned long old_end = old_addr + len;unsigned long last_addr_mask; -pte_t *src_pte, *dst_pte;struct mmu_notifier_range range;struct mmu_gather tlb; +bool preallocated;mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , mm, old_addr, old_end);@@ -5106,30 +5179,12 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma, last_addr_mask =hugetlb_mask_last_page (h);/* Prevent race with file truncation */ hugetlb_vma_lock_write (vma); + preallocated =prealloc_hugetlb_page_tables (vma, new_vma, old_addr, + new_addr, len, sz, last_addr_mask);i_mmap_lock_write (mapping); -for (; old_addr < old_end; old_addr += sz, new_addr += sz) { - src_pte =hugetlb_walk (vma, old_addr, sz); -if (!src_pte) { - old_addr |= last_addr_mask; - new_addr |= last_addr_mask; -continue ; - } -if (huge_pte_none (huge_ptep_get (mm, old_addr, src_pte))) -continue ; - -if (huge_pmd_unshare (&tlb, vma, old_addr, src_pte)) { - old_addr |= last_addr_mask; - new_addr |= last_addr_mask; -continue ; - } - - dst_pte =huge_pte_alloc (mm, new_vma, new_addr, sz); -if (!dst_pte) -break ; - -move_huge_pte (vma, old_addr, new_addr, src_pte, dst_pte, sz); -tlb_remove_huge_tlb_entry (h, &tlb, src_pte, old_addr); - } +if (preallocated) + old_addr =__move_hugetlb_page_tables (vma, new_vma, old_addr, + new_addr, len, sz, last_addr_mask, &tlb);tlb_flush_mmu_tlbonly (&tlb);huge_pmd_unshare_flush (&tlb, vma); --
Quoted 1 line
2.55.0
Hi David,
I thought more about the case where the destination page table was preallocated as a shared PMD.
I do not think the current move_huge_pte() handles that case very well. The reason is that it assumes the destination PTE is not part of a shared PMD page table: it clears the source PTE and then installs that PTE into the destination. That works when the destination PMD page table is not shared, because the source removal and destination install have a net-zero effect on rmap and hugetlb accounting.
If the destination PTE belongs to a shared PMD, that assumption does not hold. In that case, I think we should leave the shared destination page table untouched and treat the operation as a source-side unmap. Otherwise move_huge_pte() either writes through a shared page table or skips the source-side unmap accounting. That can make the rmap, hugetlb count and folio release handling inconsistent with the actual PTE update.
So I think the change should be roughly:
---
mm/hugetlb.c +0 -0
@@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long
old_addr,
- unsigned long new_addr, pte_t *src_pte, - pte_t *dst_pte, unsigned long sz) + unsigned long new_addr, pte_t *src_pte, + pte_t *dst_pte, struct mmu_gather *tlb, + unsigned long sz)
{
struct hstate *h = hstate_vma(vma);
+ bool dst_shared = hugetlb_pmd_shared(dst_pte);
struct mm_struct *mm = vma->vm_mm;
+ struct folio *folio = NULL;
pte_t pte;
pte = huge_ptep_get_and_clear(mm, old_addr, src_pte, sz);
+ tlb_remove_huge_tlb_entry(h, tlb, src_pte, old_addr); +
+ if (dst_shared) {
+ if (pte_present(pte)) {
+ folio = page_folio(pte_page(pte));
+ if (huge_pte_dirty(pte))
+ folio_mark_dirty(folio);
+ hugetlb_count_sub(pages_per_huge_page(h), mm); + hugetlb_remove_rmap(folio);
+ }
+ goto unlock;
+ }
/* Existing non-shared destination path remains unchanged. */ set_huge_pte_at(mm, new_addr, dst_pte, pte, sz);
+unlock:
...
+
+ if (folio)
+ tlb_remove_page_size(tlb, folio_page(folio, 0), + folio_size(folio));
}
---
Does this sound like the right direction to you? If so, I will fold this into the next version.
Thanks,
Zhe
David Hildenbrand (Arm)
reviewerQuoted 163 lines · Li Zhe
Quoted 148 lines · David Hildenbrand (Arm)
Quoted 2 lines · Lorenzo Stoakes (ARM)
Yeah I don't see why not! I don't think there's anything that needs to be private here.
Li, see below, can you work with the below?
----8<----
From9e1c0676ea6c9cbcabf91cd8badf81a9d88fd3efMon Sep 17 00:00:00 2001
From: "Lorenzo Stoakes (ARM)" <[email protected]>
Date: Wed, 29 Jul 2026 18:33:08 +0100
Subject: [PATCH] fix--- mm/hugetlb.c | 103 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 79 insertions(+), 24 deletions(-)mm/hugetlb.c +79
-25@@ -5077,6 +5077,79 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,spin_unlock (dst_ptl); } +/* Returns last address processed */ +static unsigned long prealloc_or_move_page_tables (struct vm_area_struct *vma, +struct vm_area_struct *new_vma,unsigned long old_addr, +unsigned long new_addr,unsigned long len,unsigned long sz, +unsigned long last_addr_mask,struct mmu_gather *tlb, +bool is_prealloc) +{ +unsigned long old_end = old_addr + len; +struct hstate *h =hstate_vma (vma); +struct mm_struct *mm = vma->vm_mm; +pte_t *src_pte, *dst_pte; + +hugetlb_vma_assert_locked (vma); + +for (; old_addr < old_end; old_addr += sz, new_addr += sz) { + src_pte =hugetlb_walk (vma, old_addr, sz); +if (!src_pte) { + old_addr |= last_addr_mask; + new_addr |= last_addr_mask; +continue ; + } +if (huge_pte_none (huge_ptep_get (mm, old_addr, src_pte))) +continue ; + +if (is_prealloc) { +if (!huge_pte_alloc (mm, new_vma, new_addr, sz)) +break ; +continue ; + } + +if (huge_pmd_unshare (tlb, vma, old_addr, src_pte)) { + old_addr |= last_addr_mask; + new_addr |= last_addr_mask; +continue ; + } + + dst_pte =hugetlb_walk (new_vma, new_addr, sz); +if (!dst_pte) +break ; + +move_huge_pte (vma, old_addr, new_addr, src_pte, dst_pte, sz); +tlb_remove_huge_tlb_entry (h, tlb, src_pte, old_addr); + } + +return old_addr; +} + +/* Returns true if preallocation succeeded across range, otherwise false. */ +static bool prealloc_hugetlb_page_tables (struct vm_area_struct *vma, +struct vm_area_struct *new_vma,unsigned long old_addr, +unsigned long new_addr,unsigned long len,unsigned long sz, +unsigned long last_addr_mask) +{ +unsigned long old_end = old_addr + len; +unsigned long addr_end; + + addr_end =prealloc_or_move_page_tables (vma, new_vma, old_addr, new_addr, + len, sz, last_addr_mask,NULL ,/*is_prealloc=*/ true); + +return addr_end >= old_end; +} + +/* Returns last processed address. */ +static unsigned long __move_hugetlb_page_tables (struct vm_area_struct *vma, +struct vm_area_struct *new_vma,unsigned long old_addr, +unsigned long new_addr,unsigned long len,unsigned long sz, +unsigned long last_addr_mask,struct mmu_gather *tlb) +{ +i_mmap_assert_write_locked (vma->vm_file->f_mapping); +return prealloc_or_move_page_tables (vma, new_vma, old_addr, new_addr, + len, sz, last_addr_mask, tlb,/*is_prealloc=*/ false); +} + int move_hugetlb_page_tables(struct vm_area_struct *vma, struct vm_area_struct *new_vma, unsigned long old_addr, unsigned long new_addr,@@ -5088,9 +5161,9 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,struct mm_struct *mm = vma->vm_mm;unsigned long old_end = old_addr + len;unsigned long last_addr_mask; -pte_t *src_pte, *dst_pte;struct mmu_notifier_range range;struct mmu_gather tlb; +bool preallocated;mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , mm, old_addr, old_end);@@ -5106,30 +5179,12 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma, last_addr_mask =hugetlb_mask_last_page (h);/* Prevent race with file truncation */ hugetlb_vma_lock_write (vma); + preallocated =prealloc_hugetlb_page_tables (vma, new_vma, old_addr, + new_addr, len, sz, last_addr_mask);i_mmap_lock_write (mapping); -for (; old_addr < old_end; old_addr += sz, new_addr += sz) { - src_pte =hugetlb_walk (vma, old_addr, sz); -if (!src_pte) { - old_addr |= last_addr_mask; - new_addr |= last_addr_mask; -continue ; - } -if (huge_pte_none (huge_ptep_get (mm, old_addr, src_pte))) -continue ; - -if (huge_pmd_unshare (&tlb, vma, old_addr, src_pte)) { - old_addr |= last_addr_mask; - new_addr |= last_addr_mask; -continue ; - } - - dst_pte =huge_pte_alloc (mm, new_vma, new_addr, sz); -if (!dst_pte) -break ; - -move_huge_pte (vma, old_addr, new_addr, src_pte, dst_pte, sz); -tlb_remove_huge_tlb_entry (h, &tlb, src_pte, old_addr); - } +if (preallocated) + old_addr =__move_hugetlb_page_tables (vma, new_vma, old_addr, + new_addr, len, sz, last_addr_mask, &tlb);tlb_flush_mmu_tlbonly (&tlb);huge_pmd_unshare_flush (&tlb, vma); --Quoted 1 line
2.55.0
Hi David,
I thought more about the case where the destination page table was preallocated as a shared PMD.
I do not think the current
move_huge_pte()handles that case very well. The reason is that it assumes the destination PTE is not part of a shared PMD page table: it clears the source PTE and then installs that PTE into the destination. That works when the destination PMD page table is not shared, because the source removal and destination install have a net-zero effect on rmap and hugetlb accounting.
Quoted 8 lines
If the destination PTE belongs to a shared PMD, that assumption does not hold. In that case, I think we should leave the shared destination page table untouched and treat the operation as a source-side unmap. Otherwise
move_huge_pte()either writes through a shared page table or skips the source-side unmap accounting. That can make the rmap, hugetlb count and folio release handling inconsistent with the actual PTE update.So I think the change should be roughly:
IIUC, what you are saying is:
A) If the source table is shared, unshare it.
b) If the destination table is shared, there is nothing to do.
?
Signature · 3 lines
Cheers, David
Quoted 170 lines · David Hildenbrand (Arm)
Quoted 169 lines · Li Zhe
Quoted 148 lines · David Hildenbrand (Arm)
Quoted 2 lines · Lorenzo Stoakes (ARM)
Yeah I don't see why not! I don't think there's anything that needs to be private here.
Li, see below, can you work with the below?
----8<----
From9e1c0676ea6c9cbcabf91cd8badf81a9d88fd3efMon Sep 17 00:00:00 2001
From: "Lorenzo Stoakes (ARM)" <[email protected]>
Date: Wed, 29 Jul 2026 18:33:08 +0100
Subject: [PATCH] fix--- mm/hugetlb.c | 103 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 79 insertions(+), 24 deletions(-)mm/hugetlb.c +79
-25@@ -5077,6 +5077,79 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,spin_unlock (dst_ptl); } +/* Returns last address processed */ +static unsigned long prealloc_or_move_page_tables (struct vm_area_struct *vma, +struct vm_area_struct *new_vma,unsigned long old_addr, +unsigned long new_addr,unsigned long len,unsigned long sz, +unsigned long last_addr_mask,struct mmu_gather *tlb, +bool is_prealloc) +{ +unsigned long old_end = old_addr + len; +struct hstate *h =hstate_vma (vma); +struct mm_struct *mm = vma->vm_mm; +pte_t *src_pte, *dst_pte; + +hugetlb_vma_assert_locked (vma); + +for (; old_addr < old_end; old_addr += sz, new_addr += sz) { + src_pte =hugetlb_walk (vma, old_addr, sz); +if (!src_pte) { + old_addr |= last_addr_mask; + new_addr |= last_addr_mask; +continue ; + } +if (huge_pte_none (huge_ptep_get (mm, old_addr, src_pte))) +continue ; + +if (is_prealloc) { +if (!huge_pte_alloc (mm, new_vma, new_addr, sz)) +break ; +continue ; + } + +if (huge_pmd_unshare (tlb, vma, old_addr, src_pte)) { + old_addr |= last_addr_mask; + new_addr |= last_addr_mask; +continue ; + } + + dst_pte =hugetlb_walk (new_vma, new_addr, sz); +if (!dst_pte) +break ; + +move_huge_pte (vma, old_addr, new_addr, src_pte, dst_pte, sz); +tlb_remove_huge_tlb_entry (h, tlb, src_pte, old_addr); + } + +return old_addr; +} + +/* Returns true if preallocation succeeded across range, otherwise false. */ +static bool prealloc_hugetlb_page_tables (struct vm_area_struct *vma, +struct vm_area_struct *new_vma,unsigned long old_addr, +unsigned long new_addr,unsigned long len,unsigned long sz, +unsigned long last_addr_mask) +{ +unsigned long old_end = old_addr + len; +unsigned long addr_end; + + addr_end =prealloc_or_move_page_tables (vma, new_vma, old_addr, new_addr, + len, sz, last_addr_mask,NULL ,/*is_prealloc=*/ true); + +return addr_end >= old_end; +} + +/* Returns last processed address. */ +static unsigned long __move_hugetlb_page_tables (struct vm_area_struct *vma, +struct vm_area_struct *new_vma,unsigned long old_addr, +unsigned long new_addr,unsigned long len,unsigned long sz, +unsigned long last_addr_mask,struct mmu_gather *tlb) +{ +i_mmap_assert_write_locked (vma->vm_file->f_mapping); +return prealloc_or_move_page_tables (vma, new_vma, old_addr, new_addr, + len, sz, last_addr_mask, tlb,/*is_prealloc=*/ false); +} + int move_hugetlb_page_tables(struct vm_area_struct *vma, struct vm_area_struct *new_vma, unsigned long old_addr, unsigned long new_addr,@@ -5088,9 +5161,9 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,struct mm_struct *mm = vma->vm_mm;unsigned long old_end = old_addr + len;unsigned long last_addr_mask; -pte_t *src_pte, *dst_pte;struct mmu_notifier_range range;struct mmu_gather tlb; +bool preallocated;mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , mm, old_addr, old_end);@@ -5106,30 +5179,12 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma, last_addr_mask =hugetlb_mask_last_page (h);/* Prevent race with file truncation */ hugetlb_vma_lock_write (vma); + preallocated =prealloc_hugetlb_page_tables (vma, new_vma, old_addr, + new_addr, len, sz, last_addr_mask);i_mmap_lock_write (mapping); -for (; old_addr < old_end; old_addr += sz, new_addr += sz) { - src_pte =hugetlb_walk (vma, old_addr, sz); -if (!src_pte) { - old_addr |= last_addr_mask; - new_addr |= last_addr_mask; -continue ; - } -if (huge_pte_none (huge_ptep_get (mm, old_addr, src_pte))) -continue ; - -if (huge_pmd_unshare (&tlb, vma, old_addr, src_pte)) { - old_addr |= last_addr_mask; - new_addr |= last_addr_mask; -continue ; - } - - dst_pte =huge_pte_alloc (mm, new_vma, new_addr, sz); -if (!dst_pte) -break ; - -move_huge_pte (vma, old_addr, new_addr, src_pte, dst_pte, sz); -tlb_remove_huge_tlb_entry (h, &tlb, src_pte, old_addr); - } +if (preallocated) + old_addr =__move_hugetlb_page_tables (vma, new_vma, old_addr, + new_addr, len, sz, last_addr_mask, &tlb);tlb_flush_mmu_tlbonly (&tlb);huge_pmd_unshare_flush (&tlb, vma); --Quoted 1 line
2.55.0
Hi David,
I thought more about the case where the destination page table was preallocated as a shared PMD.
I do not think the current
move_huge_pte()handles that case very well. The reason is that it assumes the destination PTE is not part of a shared PMD page table: it clears the source PTE and then installs that PTE into the destination. That works when the destination PMD page table is not shared, because the source removal and destination install have a net-zero effect on rmap and hugetlb accounting.If the destination PTE belongs to a shared PMD, that assumption does not hold. In that case, I think we should leave the shared destination page table untouched and treat the operation as a source-side unmap. Otherwise
move_huge_pte()either writes through a shared page table or skips the source-side unmap accounting. That can make the rmap, hugetlb count and folio release handling inconsistent with the actual PTE update.
Quoted 8 lines
Quoted 1 line
So I think the change should be roughly:
IIUC, what you are saying is:
A) If the source table is shared, unshare it.
b) If the destination table is shared, there is nothing to do.
?
Yes, mostly.
If the source PMD table is shared, huge_pmd_unshare() already drops that shared PMD table from the source side and we skip moving individual PTEs from that table.
If the source PTE is not in a shared PMD table, but the destination PTE is in a shared PMD table, then I think we should not install the source PTE into the destination page table. The destination mapping is already represented by the shared PMD page table.
However, move_huge_pte() still has to handle the source side as an
unmap: clear the source PTE, record the TLB removal, and update the source-side hugetlb accounting/rmap state. Otherwise the source mapping is removed from the page table without the matching source-side accounting. I was able to reproduce this as:
BUG: Bad page cache in process init page dumped because: still mapped when deleted
when keeping the preallocation approach but dropping the special handling for a destination PMD-shared page table.
So my current understanding is:
1. source PMD shared: unshare it and skip the PMD-sized range;
2. destination PMD shared: do not modify the destination PTE, but still clear and account the source PTE removal.
Thanks,
Zhe
David Hildenbrand (Arm)
reviewerQuoted 50 lines · Li Zhe
Quoted 20 lines · David Hildenbrand (Arm)
Quoted 19 lines · Li Zhe
Hi David,
I thought more about the case where the destination page table was preallocated as a shared PMD.
I do not think the current
move_huge_pte()handles that case very well. The reason is that it assumes the destination PTE is not part of a shared PMD page table: it clears the source PTE and then installs that PTE into the destination. That works when the destination PMD page table is not shared, because the source removal and destination install have a net-zero effect on rmap and hugetlb accounting.If the destination PTE belongs to a shared PMD, that assumption does not hold. In that case, I think we should leave the shared destination page table untouched and treat the operation as a source-side unmap. Otherwise
move_huge_pte()either writes through a shared page table or skips the source-side unmap accounting. That can make the rmap, hugetlb count and folio release handling inconsistent with the actual PTE update.Quoted 8 lines
Quoted 1 line
So I think the change should be roughly:
IIUC, what you are saying is:
A) If the source table is shared, unshare it.
b) If the destination table is shared, there is nothing to do.
?
Yes, mostly.
If the source PMD table is shared,
huge_pmd_unshare()already drops that shared PMD table from the source side and we skip moving individual PTEs from that table.If the source PTE is not in a shared PMD table, but the destination PTE is in a shared PMD table, then I think we should not install the source PTE into the destination page table. The destination mapping is already represented by the shared PMD page table.
However,
move_huge_pte()still has to handle the source side as an
unmap: clear the source PTE, record the TLB removal, and update the source-side hugetlb accounting/rmap state. Otherwise the source mapping is removed from the page table without the matching source-side accounting. I was able to reproduce this as:BUG: Bad page cache in process init page dumped because: still mapped when deleted
Quoted 8 lines
when keeping the preallocation approach but dropping the special handling for a destination PMD-shared page table.
So my current understanding is:
1. source PMD shared: unshare it and skip the PMD-sized range;
2. destination PMD shared: do not modify the destination PTE, but still clear and account the source PTE removal.
Yes, I think this makes a lot of sense. Given that this issue has been in the works for more than one month without a fix I tried to see how to do it cleaner than your version:
This is what I came up with quickly, using existing helpers by refactoring them.
Is this what you had in mind? I didn't actually test this yet. If it makes sense, could you test it and possibly clean it up further?
mm/hugetlb.c +34 -10
@@ -124,6 +124,9 @@ static int __huge_pmd_unshare(struct mmu_gather *tlb, bool check_locks);static void hugetlb_unshare_pmds (struct vm_area_struct *vma,unsigned long start,unsigned long end,bool take_locks); +static void hugetlb_unmap_range (struct mmu_gather *tlb, +struct vm_area_struct *vma,unsigned long start,unsigned long end, +struct folio *folio,zap_flags_t zap_flags);static struct resv_map *vma_resv_map (struct vm_area_struct *vma);static inline bool subpool_is_free (struct hugepage_subpool *spool)
@@ -5172,6 +5175,8 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,for (; old_addr < old_end; old_addr += sz, new_addr += sz) {const unsigned long offset_to_last_entry = (old_addr | last_addr_mask) - old_addr; +const unsigned long offset_to_last_dst_entry = + (new_addr | last_addr_mask) - new_addr; src_pte =hugetlb_walk (vma, old_addr, sz);if (!src_pte) {
@@ -5188,6 +5193,23 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,continue ; } +/* +* Don't try to install a shared PMD table and move stuff into +* them. Instead, just zap the source PMDs and share the PMD +* table later lazily. +*/ +if (sz ==PMD_SIZE &&want_pmd_share (new_vma, new_addr)) { +const unsigned long unmap_size =min (old_end - old_addr, + offset_to_last_dst_entry + sz); + +hugetlb_unmap_range (&tlb, vma, old_addr, + old_addr + unmap_size,NULL , +ZAP_FLAG_DROP_MARKER ); + old_addr += unmap_size - sz; + new_addr += unmap_size - sz; +continue ; + } + dst_pte =huge_pte_alloc (mm, new_vma, new_addr, sz);if (!dst_pte)break ;
@@ -5207,9 +5229,9 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,return len + old_addr - old_end; } -void __unmap_hugepage_range (struct mmu_gather *tlb,struct vm_area_struct *vma, -unsigned long start,unsigned long end, -struct folio *folio,zap_flags_t zap_flags) +static void hugetlb_unmap_range (struct mmu_gather *tlb, +struct vm_area_struct *vma,unsigned long start,unsigned long end, +struct folio *folio,zap_flags_t zap_flags) {struct mm_struct *mm = vma->vm_mm;const bool folio_provided = !!folio;
@@ -5227,13 +5249,6 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,BUG_ON (start & ~huge_page_mask (h));BUG_ON (end & ~huge_page_mask (h)); -/* -* This is a hugetlb vma, all the pte entries should point -* to huge page. -*/ -tlb_change_page_size (tlb, sz); -tlb_start_vma (tlb, vma); - last_addr_mask =hugetlb_mask_last_page (h); address = start;for (; address < end; address += sz) {
@@ -5362,6 +5377,15 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,if (folio_provided)break ; } +} + +void __unmap_hugepage_range (struct mmu_gather *tlb,struct vm_area_struct *vma, +unsigned long start,unsigned long end, +struct folio *folio,zap_flags_t zap_flags) +{ +tlb_change_page_size (tlb,huge_page_size (hstate_vma (vma))); +tlb_start_vma (tlb, vma); +hugetlb_unmap_range (tlb, vma, start, end, folio, zap_flags);tlb_end_vma (tlb, vma);huge_pmd_unshare_flush (tlb, vma);
Signature · 8 lines
2.43.0 -- Cheers, David
Quoted 147 lines · David Hildenbrand (Arm)
Quoted 49 lines · Li Zhe
Quoted 20 lines · David Hildenbrand (Arm)
Quoted 19 lines · Li Zhe
Hi David,
I thought more about the case where the destination page table was preallocated as a shared PMD.
I do not think the current
move_huge_pte()handles that case very well. The reason is that it assumes the destination PTE is not part of a shared PMD page table: it clears the source PTE and then installs that PTE into the destination. That works when the destination PMD page table is not shared, because the source removal and destination install have a net-zero effect on rmap and hugetlb accounting.If the destination PTE belongs to a shared PMD, that assumption does not hold. In that case, I think we should leave the shared destination page table untouched and treat the operation as a source-side unmap. Otherwise
move_huge_pte()either writes through a shared page table or skips the source-side unmap accounting. That can make the rmap, hugetlb count and folio release handling inconsistent with the actual PTE update.Quoted 8 lines
Quoted 1 line
So I think the change should be roughly:
IIUC, what you are saying is:
A) If the source table is shared, unshare it.
b) If the destination table is shared, there is nothing to do.
?
Yes, mostly.
If the source PMD table is shared,
huge_pmd_unshare()already drops that shared PMD table from the source side and we skip moving individual PTEs from that table.If the source PTE is not in a shared PMD table, but the destination PTE is in a shared PMD table, then I think we should not install the source PTE into the destination page table. The destination mapping is already represented by the shared PMD page table.
However,
move_huge_pte()still has to handle the source side as an
unmap: clear the source PTE, record the TLB removal, and update the source-side hugetlb accounting/rmap state. Otherwise the source mapping is removed from the page table without the matching source-side accounting. I was able to reproduce this as:BUG: Bad page cache in process init page dumped because: still mapped when deletedQuoted 8 lines
when keeping the preallocation approach but dropping the special handling for a destination PMD-shared page table.
So my current understanding is:
1. source PMD shared: unshare it and skip the PMD-sized range;
2. destination PMD shared: do not modify the destination PTE, but still clear and account the source PTE removal.Yes, I think this makes a lot of sense. Given that this issue has been in the works for more than one month without a fix I tried to see how to do it cleaner than your version:
This is what I came up with quickly, using existing helpers by refactoring them.
Is this what you had in mind? I didn't actually test this yet. If it makes sense, could you test it and possibly clean it up further?
mm/hugetlb.c +27
-10@@ -124,6 +124,9 @@ static int __huge_pmd_unshare(struct mmu_gather *tlb, bool check_locks);static void hugetlb_unshare_pmds (struct vm_area_struct *vma,unsigned long start,unsigned long end,bool take_locks); +static void hugetlb_unmap_range (struct mmu_gather *tlb, +struct vm_area_struct *vma,unsigned long start,unsigned long end, +struct folio *folio,zap_flags_t zap_flags);static struct resv_map *vma_resv_map (struct vm_area_struct *vma);static inline bool subpool_is_free (struct hugepage_subpool *spool)@@ -5172,6 +5175,8 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,for (; old_addr < old_end; old_addr += sz, new_addr += sz) {const unsigned long offset_to_last_entry = (old_addr | last_addr_mask) - old_addr; +const unsigned long offset_to_last_dst_entry = + (new_addr | last_addr_mask) - new_addr; src_pte =hugetlb_walk (vma, old_addr, sz);if (!src_pte) {@@ -5188,6 +5193,23 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,continue ; } +/* +* Don't try to install a shared PMD table and move stuff into +* them. Instead, just zap the source PMDs and share the PMD +* table later lazily. +*/ +if (sz ==PMD_SIZE &&want_pmd_share (new_vma, new_addr)) { +const unsigned long unmap_size =min (old_end - old_addr, + offset_to_last_dst_entry + sz); + +hugetlb_unmap_range (&tlb, vma, old_addr, + old_addr + unmap_size,NULL , +ZAP_FLAG_DROP_MARKER ); + old_addr += unmap_size - sz; + new_addr += unmap_size - sz; +continue ; + } + dst_pte =huge_pte_alloc (mm, new_vma, new_addr, sz);if (!dst_pte)break ;@@ -5207,9 +5229,9 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,return len + old_addr - old_end; } -void __unmap_hugepage_range (struct mmu_gather *tlb,struct vm_area_struct *vma, -unsigned long start,unsigned long end, -struct folio *folio,zap_flags_t zap_flags) +static void hugetlb_unmap_range (struct mmu_gather *tlb, +struct vm_area_struct *vma,unsigned long start,unsigned long end, +struct folio *folio,zap_flags_t zap_flags) {struct mm_struct *mm = vma->vm_mm;const bool folio_provided = !!folio;@@ -5227,13 +5249,6 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,BUG_ON (start & ~huge_page_mask (h));BUG_ON (end & ~huge_page_mask (h)); -/* -* This is a hugetlb vma, all the pte entries should point -* to huge page. -*/ -tlb_change_page_size (tlb, sz); -tlb_start_vma (tlb, vma); - last_addr_mask =hugetlb_mask_last_page (h); address = start;for (; address < end; address += sz) {@@ -5362,6 +5377,15 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,if (folio_provided)break ; } +} +
mm/hugetlb.c void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 5391
Quoted 10 lines
mm/hugetlb.c +7
-0+void __unmap_hugepage_range (struct mmu_gather *tlb,struct vm_area_struct *vma, +unsigned long start,unsigned long end, +struct folio *folio,zap_flags_t zap_flags) +{ +tlb_change_page_size (tlb,huge_page_size (hstate_vma (vma))); +tlb_start_vma (tlb, vma); +hugetlb_unmap_range (tlb, vma, start, end, folio, zap_flags);tlb_end_vma (tlb, vma);huge_pmd_unshare_flush (tlb, vma);
Hi David,
Your patch looks good to me.
Sashiko pointed out one corner case around UFFDIO_POISON. In the destination-PMD-sharing path, the patch zaps the source range and lets the destination fault path populate or share the PMD lazily. That works for normal hugetlb mappings, but if the source PTE contains PTE_MARKER_POISONED, the marker is not copied to the destination. The source zap uses ZAP_FLAG_DROP_MARKER, so the marker can be dropped, and a later fault at the new address would see an empty entry instead of the poisoned marker.
One possible way to avoid this is to make VM_UFFD_MISSING participate in the existing uffd_disable_huge_pmd_share() logic, because UFFDIO_POISON can install PTE_MARKER_POISONED and the poison state is stored in the PTE, similar to the existing UFFD-WP/RWP per-PTE state.
For example:
---
include/linux/userfaultfd_k.h +0 -0
@@ -183,8 +187,8 @@ static inline bool
is_mergeable_vm_userfaultfd_ctx(structvm_area_struct *vma,
static inline bool uffd_disable_huge_pmd_share(struct vm_area_struct *vma) {
return vma_test_any_mask(vma,
- mk_vma_flags_from_masks(VMA_UFFD_WP, VMA_UFFD_RWP,
- VMA_UFFD_MINOR));
+ mk_vma_flags_from_masks(VMA_UFFD_MISSING, VMA_UFFD_WP, + VMA_UFFD_RWP, VMA_UFFD_MINOR));
}
/*
---
However, Sashiko also pointed out that this may still be incomplete, because PTE_MARKER_POISONED can outlive VM_UFFD_MISSING.
I am not very familiar with this part of the userfaultfdsemantics.
Do you have any suggestions for this issue or for this direction?
Thanks,
Zhe
David Hildenbrand (Arm)
reviewerQuoted 133 lines · Li Zhe
Quoted 118 lines · David Hildenbrand (Arm)
Quoted 20 lines · Li Zhe
Yes, mostly.
If the source PMD table is shared,
huge_pmd_unshare()already drops that shared PMD table from the source side and we skip moving individual PTEs from that table.If the source PTE is not in a shared PMD table, but the destination PTE is in a shared PMD table, then I think we should not install the source PTE into the destination page table. The destination mapping is already represented by the shared PMD page table.
However,
move_huge_pte()still has to handle the source side as an
unmap: clear the source PTE, record the TLB removal, and update the source-side hugetlb accounting/rmap state. Otherwise the source mapping is removed from the page table without the matching source-side accounting. I was able to reproduce this as:BUG: Bad page cache in process init page dumped because: still mapped when deletedQuoted 8 lines
when keeping the preallocation approach but dropping the special handling for a destination PMD-shared page table.
So my current understanding is:
1. source PMD shared: unshare it and skip the PMD-sized range;
2. destination PMD shared: do not modify the destination PTE, but still clear and account the source PTE removal.Yes, I think this makes a lot of sense. Given that this issue has been in the works for more than one month without a fix I tried to see how to do it cleaner than your version:
This is what I came up with quickly, using existing helpers by refactoring them.
Is this what you had in mind? I didn't actually test this yet. If it makes sense, could you test it and possibly clean it up further?
mm/hugetlb.c +27
-10@@ -124,6 +124,9 @@ static int __huge_pmd_unshare(struct mmu_gather *tlb, bool check_locks);static void hugetlb_unshare_pmds (struct vm_area_struct *vma,unsigned long start,unsigned long end,bool take_locks); +static void hugetlb_unmap_range (struct mmu_gather *tlb, +struct vm_area_struct *vma,unsigned long start,unsigned long end, +struct folio *folio,zap_flags_t zap_flags);static struct resv_map *vma_resv_map (struct vm_area_struct *vma);static inline bool subpool_is_free (struct hugepage_subpool *spool)@@ -5172,6 +5175,8 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,for (; old_addr < old_end; old_addr += sz, new_addr += sz) {const unsigned long offset_to_last_entry = (old_addr | last_addr_mask) - old_addr; +const unsigned long offset_to_last_dst_entry = + (new_addr | last_addr_mask) - new_addr; src_pte =hugetlb_walk (vma, old_addr, sz);if (!src_pte) {@@ -5188,6 +5193,23 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,continue ; } +/* +* Don't try to install a shared PMD table and move stuff into +* them. Instead, just zap the source PMDs and share the PMD +* table later lazily. +*/ +if (sz ==PMD_SIZE &&want_pmd_share (new_vma, new_addr)) { +const unsigned long unmap_size =min (old_end - old_addr, + offset_to_last_dst_entry + sz); + +hugetlb_unmap_range (&tlb, vma, old_addr, + old_addr + unmap_size,NULL , +ZAP_FLAG_DROP_MARKER ); + old_addr += unmap_size - sz; + new_addr += unmap_size - sz; +continue ; + } + dst_pte =huge_pte_alloc (mm, new_vma, new_addr, sz);if (!dst_pte)break ;@@ -5207,9 +5229,9 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,return len + old_addr - old_end; } -void __unmap_hugepage_range (struct mmu_gather *tlb,struct vm_area_struct *vma, -unsigned long start,unsigned long end, -struct folio *folio,zap_flags_t zap_flags) +static void hugetlb_unmap_range (struct mmu_gather *tlb, +struct vm_area_struct *vma,unsigned long start,unsigned long end, +struct folio *folio,zap_flags_t zap_flags) {struct mm_struct *mm = vma->vm_mm;const bool folio_provided = !!folio;@@ -5227,13 +5249,6 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,BUG_ON (start & ~huge_page_mask (h));BUG_ON (end & ~huge_page_mask (h)); -/* -* This is a hugetlb vma, all the pte entries should point -* to huge page. -*/ -tlb_change_page_size (tlb, sz); -tlb_start_vma (tlb, vma); - last_addr_mask =hugetlb_mask_last_page (h); address = start;for (; address < end; address += sz) {@@ -5362,6 +5377,15 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,if (folio_provided)break ; } +} +mm/hugetlb.c void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 5391
Quoted 10 lines
mm/hugetlb.c +7
-0+void __unmap_hugepage_range (struct mmu_gather *tlb,struct vm_area_struct *vma, +unsigned long start,unsigned long end, +struct folio *folio,zap_flags_t zap_flags) +{ +tlb_change_page_size (tlb,huge_page_size (hstate_vma (vma))); +tlb_start_vma (tlb, vma); +hugetlb_unmap_range (tlb, vma, start, end, folio, zap_flags);tlb_end_vma (tlb, vma);huge_pmd_unshare_flush (tlb, vma);Hi David,
Quoted 7 lines
Your patch looks good to me.
Sashiko pointed out one corner case around UFFDIO_POISON. In the destination-PMD-sharing path, the patch zaps the source range and lets the destination fault path populate or share the PMD lazily. That works for normal hugetlb mappings, but if the source PTE contains
PTE_MARKER_POISONED, the marker is not copied to the destination.
Any UFFD activity must necessarily disable PMD sharing.
That's why want_pmd_share() checks uffd_disable_huge_pmd_share().
We cannot possibly move a UFFD marker into shared page table, that would be completely broken already.
Quoted 19 lines
The
source zap usesZAP_FLAG_DROP_MARKER, so the marker can be dropped, and a later fault at the new address would see an empty entry instead of the poisoned marker.One possible way to avoid this is to make
VM_UFFD_MISSINGparticipate in the existinguffd_disable_huge_pmd_share()logic, becauseUFFDIO_POISONcan installPTE_MARKER_POISONEDand the poison state is stored in the PTE, similar to the existing UFFD-WP/RWP per-PTE state.For example:
---
include/linux/userfaultfd_k.h +0
-0@@ -183,8 +187,8 @@ static inline boolis_mergeable_vm_userfaultfd_ctx(struct
Quoted 8 lines
vm_area_struct*vma,
static inline booluffd_disable_huge_pmd_share(struct vm_area_struct *vma){
return vma_test_any_mask(vma,
- mk_vma_flags_from_masks(VMA_UFFD_WP,VMA_UFFD_RWP,
-VMA_UFFD_MINOR));
+mk_vma_flags_from_masks(VMA_UFFD_MISSING, VMA_UFFD_WP, + VMA_UFFD_RWP, VMA_UFFD_MINOR));
I think we can currently install POISON markers into shared PMD tables. What a mess. Let me dig, that all needs a proper fix.
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
reviewerQuoted 144 lines · Li Zhe
Quoted 118 lines · David Hildenbrand (Arm)
Quoted 20 lines · Li Zhe
Yes, mostly.
If the source PMD table is shared,
huge_pmd_unshare()already drops that shared PMD table from the source side and we skip moving individual PTEs from that table.If the source PTE is not in a shared PMD table, but the destination PTE is in a shared PMD table, then I think we should not install the source PTE into the destination page table. The destination mapping is already represented by the shared PMD page table.
However,
move_huge_pte()still has to handle the source side as an
unmap: clear the source PTE, record the TLB removal, and update the source-side hugetlb accounting/rmap state. Otherwise the source mapping is removed from the page table without the matching source-side accounting. I was able to reproduce this as:BUG: Bad page cache in process init page dumped because: still mapped when deletedQuoted 8 lines
when keeping the preallocation approach but dropping the special handling for a destination PMD-shared page table.
So my current understanding is:
1. source PMD shared: unshare it and skip the PMD-sized range;
2. destination PMD shared: do not modify the destination PTE, but still clear and account the source PTE removal.Yes, I think this makes a lot of sense. Given that this issue has been in the works for more than one month without a fix I tried to see how to do it cleaner than your version:
This is what I came up with quickly, using existing helpers by refactoring them.
Is this what you had in mind? I didn't actually test this yet. If it makes sense, could you test it and possibly clean it up further?
mm/hugetlb.c +27
-10@@ -124,6 +124,9 @@ static int __huge_pmd_unshare(struct mmu_gather *tlb, bool check_locks);static void hugetlb_unshare_pmds (struct vm_area_struct *vma,unsigned long start,unsigned long end,bool take_locks); +static void hugetlb_unmap_range (struct mmu_gather *tlb, +struct vm_area_struct *vma,unsigned long start,unsigned long end, +struct folio *folio,zap_flags_t zap_flags);static struct resv_map *vma_resv_map (struct vm_area_struct *vma);static inline bool subpool_is_free (struct hugepage_subpool *spool)@@ -5172,6 +5175,8 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,for (; old_addr < old_end; old_addr += sz, new_addr += sz) {const unsigned long offset_to_last_entry = (old_addr | last_addr_mask) - old_addr; +const unsigned long offset_to_last_dst_entry = + (new_addr | last_addr_mask) - new_addr; src_pte =hugetlb_walk (vma, old_addr, sz);if (!src_pte) {@@ -5188,6 +5193,23 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,continue ; } +/* +* Don't try to install a shared PMD table and move stuff into +* them. Instead, just zap the source PMDs and share the PMD +* table later lazily. +*/ +if (sz ==PMD_SIZE &&want_pmd_share (new_vma, new_addr)) { +const unsigned long unmap_size =min (old_end - old_addr, + offset_to_last_dst_entry + sz); + +hugetlb_unmap_range (&tlb, vma, old_addr, + old_addr + unmap_size,NULL , +ZAP_FLAG_DROP_MARKER ); + old_addr += unmap_size - sz; + new_addr += unmap_size - sz; +continue ; + } + dst_pte =huge_pte_alloc (mm, new_vma, new_addr, sz);if (!dst_pte)break ;@@ -5207,9 +5229,9 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,return len + old_addr - old_end; } -void __unmap_hugepage_range (struct mmu_gather *tlb,struct vm_area_struct *vma, -unsigned long start,unsigned long end, -struct folio *folio,zap_flags_t zap_flags) +static void hugetlb_unmap_range (struct mmu_gather *tlb, +struct vm_area_struct *vma,unsigned long start,unsigned long end, +struct folio *folio,zap_flags_t zap_flags) {struct mm_struct *mm = vma->vm_mm;const bool folio_provided = !!folio;@@ -5227,13 +5249,6 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,BUG_ON (start & ~huge_page_mask (h));BUG_ON (end & ~huge_page_mask (h)); -/* -* This is a hugetlb vma, all the pte entries should point -* to huge page. -*/ -tlb_change_page_size (tlb, sz); -tlb_start_vma (tlb, vma); - last_addr_mask =hugetlb_mask_last_page (h); address = start;for (; address < end; address += sz) {@@ -5362,6 +5377,15 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,if (folio_provided)break ; } +} +mm/hugetlb.c void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 5391
Quoted 10 lines
mm/hugetlb.c +7
-0+void __unmap_hugepage_range (struct mmu_gather *tlb,struct vm_area_struct *vma, +unsigned long start,unsigned long end, +struct folio *folio,zap_flags_t zap_flags) +{ +tlb_change_page_size (tlb,huge_page_size (hstate_vma (vma))); +tlb_start_vma (tlb, vma); +hugetlb_unmap_range (tlb, vma, start, end, folio, zap_flags);tlb_end_vma (tlb, vma);huge_pmd_unshare_flush (tlb, vma);Hi David,
Your patch looks good to me.
Sashiko pointed out one corner case around UFFDIO_POISON. In the destination-PMD-sharing path, the patch zaps the source range and lets the destination fault path populate or share the PMD lazily. That works for normal hugetlb mappings, but if the source PTE contains
PTE_MARKER_POISONED, the marker is not copied to the destination. The source zap usesZAP_FLAG_DROP_MARKER, so the marker can be dropped, and a later fault at the new address would see an empty entry instead of the poisoned marker.
Quoted 4 lines
One possible way to avoid this is to make
VM_UFFD_MISSINGparticipate in the existinguffd_disable_huge_pmd_share()logic, becauseUFFDIO_POISONcan installPTE_MARKER_POISONEDand the poison state is stored in the PTE, similar to the existing UFFD-WP/RWP per-PTE state.
Okay, things escalated a bit and I feel like I ended up prototyping fixes for half the kernel ;)
So, we can as of today:
1) install POISON marker into shared PMD tables. Either using ordinary marker installation or using mremap(). Bad.
2) Lose POISON markers during fork(). Bad.
3) Lose POISON markers during retracting of pgtables. Bad.
I have prototype fixes for everything, but it depends on other fixes/infrastrcture that would make it all hard to backport.
So my thinking is that likely we should do what you suggest:
commit ed299abf3922678d2293072ee5d7bddc22b4e9a1 (HEAD)
Author: David Hildenbrand (Arm) <[email protected]> Date: Fri Oct 16 13:22:43 2026 +0200
mm/hugetlb: disable PMD sharing for UFFD missing mode
For UFFDIO_POISON to install a poison marker in a hugetlb mapping, it is sufficient to have the VMA registered for missing mode.
However, UFFD missing mode does currently not block PMD table sharing, so installing the marker will be visible to everyone using the shared PMD table, which is wrong as UFFDIO_POISON must stay local to the process.
To fix it, include VM_UFFD_MISSING in uffd_disable_huge_pmd_share(). Add a debug check on the marker installation path.
The existing registration path will then unshare PMD tables, and subsequent faults will not share them while the mapping remains registered.
The alternative would be to unshare PMD tables selectively when installing poison markers, which results in way more complexity. Or alternatively, simply failing the marker installation, which might break some use cases.
This effectively disables PMD sharing for missing-mode registrations that never use UFFDIO_POISON.
include/linux/userfaultfd_k.h +8 -4
@@ -168,7 +168,10 @@ static inline bool is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct *vma, }/* -* Never enable huge pmd sharing on some uffd registered vmas: +* Never enable huge pmd sharing on the following VMAs: +* +* - VM_UFFD_MISSING VMAs, because UFFDIO_POISON may install markers and we +* want PMD tables to be unshared when installing these markers already. * * - VM_UFFD_WP and VM_UFFD_RWP VMAs, because the write protect / access * tracking information is per pgtable entry.
@@ -181,9 +184,10 @@ static inline bool is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct *vma,*/ static inline bool uffd_disable_huge_pmd_share (struct vm_area_struct *vma) { -return vma_test_any_mask (vma, -mk_vma_flags_from_masks (VMA_UFFD_WP ,VMA_UFFD_RWP , -VMA_UFFD_MINOR )); +const vma_flags_t uffd_flags =mk_vma_flags_from_masks (VMA_UFFD_MISSING ,VMA_UFFD_WP , +VMA_UFFD_RWP ,VMA_UFFD_MINOR ); + +return vma_test_any_mask (vma, uffd_flags); }/*
mm/hugetlb.c +1 -0
@@ -6376,6 +6376,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,return -EEXIST ; } +VM_WARN_ON_ONCE (size ==PMD_SIZE &&hugetlb_pmd_shared (dst_pte)); _dst_pte =make_pte_marker (PTE_MARKER_POISONED );set_huge_pte_at (dst_mm, dst_addr, dst_pte, _dst_pte, size);
Followed by my fix here.
We will at least no longer be able to install poison markers into shared PMD tables or deadlock.
Yes, we might lose UFFD_POISON markers in some odd mremap() situations, but it's all very broken already, and likely we should just fix that later and not backport it.
It's unlikely that people depend on it.
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
reviewerQuoted 24 lines · Li Zhe
huge_pmd_share()always takesi_mmap_rwsemfor read before looking for a shareable PMD page table.That is unsafe for callers that already hold the same mapping lock for write. In particular,
move_hugetlb_page_tables()takesi_mmap_rwsemfor write to prevent truncation races, then callshuge_pte_alloc()for the destination address. If the destination PUD is empty and PMD sharing is possible,huge_pte_alloc()can callhuge_pmd_share(), which then tries to take the same rwsem for read and can deadlock on itself.PMD sharing is only an optimization. If the read side of
i_mmap_rwsemcannot be acquired immediately, fall back to allocating a private PMD table instead of blocking in the sharing path.Signed-off-by Li Zhe <[email protected]>--- mm/hugetlb.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)mm/hugetlb.c +0
-0
mm/hugetlb.c pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, · line 7003
Quoted 10 lines
mm/hugetlb.c +6
-1@@ -6995,7 +6995,15 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,pte_t *spte =NULL ;pte_t *pte; -i_mmap_lock_read (mapping); +/* +* Some callers already hold i_mmap_rwsem for write, for example +* move_hugetlb_page_tables(). PMD sharing is only an optimization, so +* fall back to a private PMD table instead of blocking on the same +* rwsem in read mode. +*/
"just an optimization" is not entirely true. There are workloads that depend on huge pmd sharing to work. So I don't think this is acceptable.
There was a private discussion on this issue recently (for some weird reason the deadlock was reported as a security issue), 2 months ago I think.
Oscar, I recall that you wanted to send a proper patch?
Signature · 3 lines
Cheers, David