mm/hugetlb: Fix uffd wr-protection for CoW optimization path
Subsystems (3)
PATCH 1/1 mm/hugetlb: Fix uffd wr-protection for CoW optimization path
This patch fixes an issue that a hugetlb uffd-wr-protected mapping can be writable even with uffd-wp bit set. It only happens with all these conditions met: (1) hugetlb memory (2) private mapping (3) original mapping was missing, then (4) being wr-protected (IOW, pte marker installed). Then write to the page to trigger.
Userfaultfd-wp trap for hugetlb was implemented in hugetlb_fault() before even reaching hugetlb_wp() to avoid taking more locks that userfault won't need. However there's one CoW optimization path for missing hugetlb page that can trigger hugetlb_wp() inside hugetlb_no_page(), that can bypass the userfaultfd-wp traps.
A few ways to resolve this:
(1) Skip the CoW optimization for hugetlb private mapping, considering that private mappings for hugetlb should be very rare, so it may not really be helpful to major workloads. The worst case is we only skip the optimization if userfaultfd_wp(vma)==true, because uffd-wp needs another fault anyway.
(2) Move the userfaultfd-wp handling for hugetlb from hugetlb_fault() into hugetlb_wp(). The major cons is there're a bunch of locks taken when calling hugetlb_wp(), and that will make the changeset unnecessarily complicated due to the lock operations.
(3) Carry over uffd-wp bit in hugetlb_wp(), so it'll need to fault again for uffd-wp privately mapped pages.
This patch chose option (3) which contains the minimum changeset (simplest for backport) and also make sure hugetlb_wp() itself will start to be always safe with uffd-wp ptes even if called elsewhere in the future.
This patch will be needed for v5.19+ hence copy stable.
--- mm/hugetlb.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
mm/hugetlb.c +5 -3
@@ -5478,7 +5478,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, struct folio *pagecache_folio, spinlock_t *ptl) { const bool unshare = flags & FAULT_FLAG_UNSHARE; -pte_t pte; +pte_t pte, newpte;struct hstate *h =hstate_vma (vma);struct page *old_page;struct folio *new_folio;
@@ -5622,8 +5622,10 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,mmu_notifier_invalidate_range (mm, range.start, range.end);page_remove_rmap (old_page, vma, true);hugepage_add_new_anon_rmap (new_folio, vma, haddr); -set_huge_pte_at (mm, haddr, ptep, -make_huge_pte (vma, &new_folio->page, !unshare)); + newpte =make_huge_pte (vma, &new_folio->page, !unshare); +if (huge_pte_uffd_wp (pte)) + newpte =huge_pte_mkuffd_wp (newpte); +set_huge_pte_at (mm, haddr, ptep, newpte);folio_set_hugetlb_migratable (new_folio);/* Make the old page be freed below */ new_folio =page_folio (old_page);
Signature · 1 line
2.39.1
Mike,
For some reason I forgot to copy you.. sorry. Here's the link:
https://lore.kernel.org/linux-mm/[email protected]
Quoted 71 lines · Peter Xu
This patch fixes an issue that a hugetlb uffd-wr-protected mapping can be writable even with uffd-wp bit set. It only happens with all these conditions met: (1) hugetlb memory (2) private mapping (3) original mapping was missing, then (4) being wr-protected (IOW, pte marker installed). Then write to the page to trigger.
Userfaultfd-wp trap for hugetlb was implemented in
hugetlb_fault()before even reachinghugetlb_wp()to avoid taking more locks that userfault won't need. However there's one CoW optimization path for missing hugetlb page that can triggerhugetlb_wp()insidehugetlb_no_page(), that can bypass the userfaultfd-wp traps.A few ways to resolve this:
(1) Skip the CoW optimization for hugetlb private mapping, considering that private mappings for hugetlb should be very rare, so it may not really be helpful to major workloads. The worst case is we only skip the optimization if userfaultfd_wp(vma)==true, because uffd-wp needs another fault anyway.
(2) Move the userfaultfd-wp handling for hugetlb from
hugetlb_fault()intohugetlb_wp(). The major cons is there're a bunch of locks taken when callinghugetlb_wp(), and that will make the changeset unnecessarily complicated due to the lock operations.(3) Carry over uffd-wp bit in
hugetlb_wp(), so it'll need to fault again for uffd-wp privately mapped pages.This patch chose option (3) which contains the minimum changeset (simplest for backport) and also make sure
hugetlb_wp()itself will start to be always safe with uffd-wp ptes even if called elsewhere in the future.This patch will be needed for v5.19+ hence copy stable.
Reported-by Muhammad Usama Anjum <[email protected]>Cc linux-stable <[email protected]>Fixes 166f3ecc0daf ("mm/hugetlb: hook page faults for uffd write protection")Signed-off-by Peter Xu <[email protected]>--- mm/hugetlb.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)mm/hugetlb.c +5
-3@@ -5478,7 +5478,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, struct folio *pagecache_folio, spinlock_t *ptl) { const bool unshare = flags & FAULT_FLAG_UNSHARE; -pte_t pte; +pte_t pte, newpte;struct hstate *h =hstate_vma (vma);struct page *old_page;struct folio *new_folio;@@ -5622,8 +5622,10 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,mmu_notifier_invalidate_range (mm, range.start, range.end);page_remove_rmap (old_page, vma, true);hugepage_add_new_anon_rmap (new_folio, vma, haddr); -set_huge_pte_at (mm, haddr, ptep, -make_huge_pte (vma, &new_folio->page, !unshare)); + newpte =make_huge_pte (vma, &new_folio->page, !unshare); +if (huge_pte_uffd_wp (pte)) + newpte =huge_pte_mkuffd_wp (newpte); +set_huge_pte_at (mm, haddr, ptep, newpte);folio_set_hugetlb_migratable (new_folio);/* Make the old page be freed below */ new_folio =page_folio (old_page);Signature · 1 line
2.39.1
Signature · 1 line
Peter Xu
Quoted 58 lines · Peter Xu
This patch fixes an issue that a hugetlb uffd-wr-protected mapping can be writable even with uffd-wp bit set. It only happens with all these conditions met: (1) hugetlb memory (2) private mapping (3) original mapping was missing, then (4) being wr-protected (IOW, pte marker installed). Then write to the page to trigger.
Userfaultfd-wp trap for hugetlb was implemented in
hugetlb_fault()before even reachinghugetlb_wp()to avoid taking more locks that userfault won't need. However there's one CoW optimization path for missing hugetlb page that can triggerhugetlb_wp()insidehugetlb_no_page(), that can bypass the userfaultfd-wp traps.A few ways to resolve this:
(1) Skip the CoW optimization for hugetlb private mapping, considering that private mappings for hugetlb should be very rare, so it may not really be helpful to major workloads. The worst case is we only skip the optimization if userfaultfd_wp(vma)==true, because uffd-wp needs another fault anyway.
(2) Move the userfaultfd-wp handling for hugetlb from
hugetlb_fault()intohugetlb_wp(). The major cons is there're a bunch of locks taken when callinghugetlb_wp(), and that will make the changeset unnecessarily complicated due to the lock operations.(3) Carry over uffd-wp bit in
hugetlb_wp(), so it'll need to fault again for uffd-wp privately mapped pages.This patch chose option (3) which contains the minimum changeset (simplest for backport) and also make sure
hugetlb_wp()itself will start to be always safe with uffd-wp ptes even if called elsewhere in the future.This patch will be needed for v5.19+ hence copy stable.
Reported-by Muhammad Usama Anjum <[email protected]>Cc linux-stable <[email protected]>Fixes 166f3ecc0daf ("mm/hugetlb: hook page faults for uffd write protection")Signed-off-by Peter Xu <[email protected]>--- mm/hugetlb.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)mm/hugetlb.c +1
-1@@ -5478,7 +5478,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, struct folio *pagecache_folio, spinlock_t *ptl) { const bool unshare = flags & FAULT_FLAG_UNSHARE; -pte_t pte; +pte_t pte, newpte;struct hstate *h =hstate_vma (vma);struct page *old_page;struct folio *new_folio;@@ -5622,8 +5622,10 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,mmu_notifier_invalidate_range (mm, range.start, range.end);page_remove_rmap (old_page, vma, true);
mm/hugetlb.c static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, · line 5631
Quoted 10 lines
mm/hugetlb.c +4
-2hugepage_add_new_anon_rmap (new_folio, vma, haddr); -set_huge_pte_at (mm, haddr, ptep, -make_huge_pte (vma, &new_folio->page, !unshare)); + newpte =make_huge_pte (vma, &new_folio->page, !unshare); +if (huge_pte_uffd_wp (pte)) + newpte =huge_pte_mkuffd_wp (newpte); +set_huge_pte_at (mm, haddr, ptep, newpte);folio_set_hugetlb_migratable (new_folio);/* Make the old page be freed below */ new_folio =page_folio (old_page);
Looks correct to me. Do we have a reproducer?
Signature · 3 lines
Thanks, David / dhildenb
Quoted 63 lines · David Hildenbrand
Quoted 62 lines · Peter Xu
This patch fixes an issue that a hugetlb uffd-wr-protected mapping can be writable even with uffd-wp bit set. It only happens with all these conditions met: (1) hugetlb memory (2) private mapping (3) original mapping was missing, then (4) being wr-protected (IOW, pte marker installed). Then write to the page to trigger.
Userfaultfd-wp trap for hugetlb was implemented in
hugetlb_fault()before even reachinghugetlb_wp()to avoid taking more locks that userfault won't need. However there's one CoW optimization path for missing hugetlb page that can triggerhugetlb_wp()insidehugetlb_no_page(), that can bypass the userfaultfd-wp traps.A few ways to resolve this:
(1) Skip the CoW optimization for hugetlb private mapping, considering that private mappings for hugetlb should be very rare, so it may not really be helpful to major workloads. The worst case is we only skip the optimization if userfaultfd_wp(vma)==true, because uffd-wp needs another fault anyway.
(2) Move the userfaultfd-wp handling for hugetlb from
hugetlb_fault()intohugetlb_wp(). The major cons is there're a bunch of locks taken when callinghugetlb_wp(), and that will make the changeset unnecessarily complicated due to the lock operations.(3) Carry over uffd-wp bit in
hugetlb_wp(), so it'll need to fault again for uffd-wp privately mapped pages.This patch chose option (3) which contains the minimum changeset (simplest for backport) and also make sure
hugetlb_wp()itself will start to be always safe with uffd-wp ptes even if called elsewhere in the future.This patch will be needed for v5.19+ hence copy stable.
Reported-by Muhammad Usama Anjum <[email protected]>Cc linux-stable <[email protected]>Fixes 166f3ecc0daf ("mm/hugetlb: hook page faults for uffd write protection")Signed-off-by Peter Xu <[email protected]>--- mm/hugetlb.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)mm/hugetlb.c +2
-3@@ -5478,7 +5478,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, struct folio *pagecache_folio, spinlock_t *ptl) { const bool unshare = flags & FAULT_FLAG_UNSHARE; -pte_t pte; +pte_t pte, newpte;struct hstate *h =hstate_vma (vma);struct page *old_page;struct folio *new_folio;@@ -5622,8 +5622,10 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,mmu_notifier_invalidate_range (mm, range.start, range.end);page_remove_rmap (old_page, vma, true);hugepage_add_new_anon_rmap (new_folio, vma, haddr); -set_huge_pte_at (mm, haddr, ptep, -make_huge_pte (vma, &new_folio->page, !unshare)); + newpte =make_huge_pte (vma, &new_folio->page, !unshare);
Quoted 8 lines
mm/hugetlb.c static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, · line 5631
Quoted 6 lines
mm/hugetlb.c +3
-0+if (huge_pte_uffd_wp (pte)) + newpte =huge_pte_mkuffd_wp (newpte); +set_huge_pte_at (mm, haddr, ptep, newpte);folio_set_hugetlb_migratable (new_folio);/* Make the old page be freed below */ new_folio =page_folio (old_page);Looks correct to me. Do we have a reproducer?
I used a reproducer for the async mode I wrote (patch 2 attached, need to change to VM_PRIVATE):
https://lore.kernel.org/all/ZBNr4nohj%2FTw4Zhw@x1n/
I don't think kernel kselftest can trigger it because we don't do strict checks yet with uffd-wp bits. I've already started looking into cleanup the test cases and I do plan to add new tests to cover this.
Meanwhile, let's also wait for an ack from Muhammad. Even though the async mode is not part of the code base, it'll be a good test for verifying every single uffd-wp bit being set or cleared as expected.
Quoted 1 line
Thanks,
Signature · 1 line
Peter Xu
Hi Peter,
Sorry for late reply.
Quoted 78 lines · Peter Xu
Quoted 63 lines · David Hildenbrand
Quoted 62 lines · Peter Xu
This patch fixes an issue that a hugetlb uffd-wr-protected mapping can be writable even with uffd-wp bit set. It only happens with all these conditions met: (1) hugetlb memory (2) private mapping (3) original mapping was missing, then (4) being wr-protected (IOW, pte marker installed). Then write to the page to trigger.
Userfaultfd-wp trap for hugetlb was implemented in
hugetlb_fault()before even reachinghugetlb_wp()to avoid taking more locks that userfault won't need. However there's one CoW optimization path for missing hugetlb page that can triggerhugetlb_wp()insidehugetlb_no_page(), that can bypass the userfaultfd-wp traps.A few ways to resolve this:
(1) Skip the CoW optimization for hugetlb private mapping, considering that private mappings for hugetlb should be very rare, so it may not really be helpful to major workloads. The worst case is we only skip the optimization if userfaultfd_wp(vma)==true, because uffd-wp needs another fault anyway.
(2) Move the userfaultfd-wp handling for hugetlb from
hugetlb_fault()intohugetlb_wp(). The major cons is there're a bunch of locks taken when callinghugetlb_wp(), and that will make the changeset unnecessarily complicated due to the lock operations.(3) Carry over uffd-wp bit in
hugetlb_wp(), so it'll need to fault again for uffd-wp privately mapped pages.This patch chose option (3) which contains the minimum changeset (simplest for backport) and also make sure
hugetlb_wp()itself will start to be always safe with uffd-wp ptes even if called elsewhere in the future.This patch will be needed for v5.19+ hence copy stable.
Reported-by Muhammad Usama Anjum <[email protected]>Cc linux-stable <[email protected]>Fixes 166f3ecc0daf ("mm/hugetlb: hook page faults for uffd write protection")Signed-off-by Peter Xu <[email protected]>--- mm/hugetlb.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)mm/hugetlb.c +2
-3@@ -5478,7 +5478,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, struct folio *pagecache_folio, spinlock_t *ptl) { const bool unshare = flags & FAULT_FLAG_UNSHARE; -pte_t pte; +pte_t pte, newpte;struct hstate *h =hstate_vma (vma);struct page *old_page;struct folio *new_folio;@@ -5622,8 +5622,10 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,mmu_notifier_invalidate_range (mm, range.start, range.end);page_remove_rmap (old_page, vma, true);hugepage_add_new_anon_rmap (new_folio, vma, haddr); -set_huge_pte_at (mm, haddr, ptep, -make_huge_pte (vma, &new_folio->page, !unshare)); + newpte =make_huge_pte (vma, &new_folio->page, !unshare);Quoted 8 lines
mm/hugetlb.c static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, · line 5631
Quoted 6 lines
mm/hugetlb.c +3
-0+if (huge_pte_uffd_wp (pte)) + newpte =huge_pte_mkuffd_wp (newpte); +set_huge_pte_at (mm, haddr, ptep, newpte);folio_set_hugetlb_migratable (new_folio);/* Make the old page be freed below */ new_folio =page_folio (old_page);Looks correct to me. Do we have a reproducer?
I used a reproducer for the async mode I wrote (patch 2 attached, need to change to
VM_PRIVATE):
Quoted 7 lines
I don't think kernel kselftest can trigger it because we don't do strict checks yet with uffd-wp bits. I've already started looking into cleanup the test cases and I do plan to add new tests to cover this.
Meanwhile, let's also wait for an ack from Muhammad. Even though the async mode is not part of the code base, it'll be a good test for verifying every single uffd-wp bit being set or cleared as expected.
I've tested by applying this patch. But the bug is still there. Just like Peter has mentioned, we are using our in progress patches related to pagemap_scan ioctl and userfaultd wp async patches to reproduce it.
To reproduce please build kernel and run pagemap_ioctl test in mm in hugetlb_mem_reproducer branch:
https://gitlab.collabora.com/usama.anjum/linux-mainline/-/tree/hugetlb_mem_reproducer
In case you have any question on how to reproduce, please let me know. I'll try to provide a cleaner alternative.
Quoted 5 lines
Quoted 2 lines
Thanks,
Signature · 2 lines
BR, Muhammad Usama Anjum
Quoted 94 lines · Muhammad Usama Anjum
Hi Peter,
Sorry for late reply.
Quoted 78 lines · Peter Xu
Quoted 63 lines · David Hildenbrand
Quoted 62 lines · Peter Xu
This patch fixes an issue that a hugetlb uffd-wr-protected mapping can be writable even with uffd-wp bit set. It only happens with all these conditions met: (1) hugetlb memory (2) private mapping (3) original mapping was missing, then (4) being wr-protected (IOW, pte marker installed). Then write to the page to trigger.
Userfaultfd-wp trap for hugetlb was implemented in
hugetlb_fault()before even reachinghugetlb_wp()to avoid taking more locks that userfault won't need. However there's one CoW optimization path for missing hugetlb page that can triggerhugetlb_wp()insidehugetlb_no_page(), that can bypass the userfaultfd-wp traps.A few ways to resolve this:
(1) Skip the CoW optimization for hugetlb private mapping, considering that private mappings for hugetlb should be very rare, so it may not really be helpful to major workloads. The worst case is we only skip the optimization if userfaultfd_wp(vma)==true, because uffd-wp needs another fault anyway.
(2) Move the userfaultfd-wp handling for hugetlb from
hugetlb_fault()intohugetlb_wp(). The major cons is there're a bunch of locks taken when callinghugetlb_wp(), and that will make the changeset unnecessarily complicated due to the lock operations.(3) Carry over uffd-wp bit in
hugetlb_wp(), so it'll need to fault again for uffd-wp privately mapped pages.This patch chose option (3) which contains the minimum changeset (simplest for backport) and also make sure
hugetlb_wp()itself will start to be always safe with uffd-wp ptes even if called elsewhere in the future.This patch will be needed for v5.19+ hence copy stable.
Reported-by Muhammad Usama Anjum <[email protected]>Cc linux-stable <[email protected]>Fixes 166f3ecc0daf ("mm/hugetlb: hook page faults for uffd write protection")Signed-off-by Peter Xu <[email protected]>--- mm/hugetlb.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)mm/hugetlb.c +2
-3@@ -5478,7 +5478,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, struct folio *pagecache_folio, spinlock_t *ptl) { const bool unshare = flags & FAULT_FLAG_UNSHARE; -pte_t pte; +pte_t pte, newpte;struct hstate *h =hstate_vma (vma);struct page *old_page;struct folio *new_folio;@@ -5622,8 +5622,10 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,mmu_notifier_invalidate_range (mm, range.start, range.end);page_remove_rmap (old_page, vma, true);hugepage_add_new_anon_rmap (new_folio, vma, haddr); -set_huge_pte_at (mm, haddr, ptep, -make_huge_pte (vma, &new_folio->page, !unshare)); + newpte =make_huge_pte (vma, &new_folio->page, !unshare);Quoted 8 lines
mm/hugetlb.c static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, · line 5631
Quoted 6 lines
mm/hugetlb.c +3
-0+if (huge_pte_uffd_wp (pte)) + newpte =huge_pte_mkuffd_wp (newpte); +set_huge_pte_at (mm, haddr, ptep, newpte);folio_set_hugetlb_migratable (new_folio);/* Make the old page be freed below */ new_folio =page_folio (old_page);Looks correct to me. Do we have a reproducer?
I used a reproducer for the async mode I wrote (patch 2 attached, need to change to
VM_PRIVATE):Quoted 7 lines
I don't think kernel kselftest can trigger it because we don't do strict checks yet with uffd-wp bits. I've already started looking into cleanup the test cases and I do plan to add new tests to cover this.
Meanwhile, let's also wait for an ack from Muhammad. Even though the async mode is not part of the code base, it'll be a good test for verifying every single uffd-wp bit being set or cleared as expected.
I've tested by applying this patch. But the bug is still there. Just like Peter has mentioned, we are using our in progress patches related to
pagemap_scanioctl and userfaultd wp async patches to reproduce it.
Quoted 6 lines
To reproduce please build kernel and run
pagemap_ioctltest in mm inhugetlb_mem_reproducerbranch:
https://gitlab.collabora.com/usama.anjum/linux-mainline/-/tree/hugetlb_mem_reproducerIn case you have any question on how to reproduce, please let me know. I'll try to provide a cleaner alternative.
Hmm, I think my current fix is incomplete if not wrong. The root cause should still be valid, however I overlooked another path:
if (page_mapcount(old_page) == 1 && PageAnon(old_page)) {
if (!PageAnonExclusive(old_page))
page_move_anon_rmap(old_page, vma);
if (likely(!unshare))
set_huge_ptep_writable(vma, haddr, ptep);
delayacct_wpcopy_end();
return 0;
}We should bail out early in this path, and it'll be even easier we always bail out hugetlb_wp() as long as uffd-wp is detected because userfault should always be handled before any decision to CoW.
v2 attached.. Please give it another shot.
Thanks,
Signature · 1 line
Peter Xu
From 4a294f9ec5d2ba94a6a7ecf03bd096ea35902f2f Mon Sep 17 00:00:00 2001
From: Peter Xu <[email protected]>
Date: Tue, 21 Mar 2023 14:58:42 -0400
Subject: [PATCH v2] mm/hugetlb: Fix uffd wr-protection for CoW optimization path
This patch fixes an issue that a hugetlb uffd-wr-protected mapping can be writable even with uffd-wp bit set. It only happens with hugetlb private mappings, when someone firstly wr-protects a missing pte (which will install a pte marker), followed by a write to the page. That will trigger a missing fault and an optimized CoW in the same fault stack.
Userfaultfd-wp trap for hugetlb was implemented in hugetlb_fault() before even reaching hugetlb_wp() to avoid taking more locks that userfault won't need. However there's one CoW optimization path for missing hugetlb page that can trigger hugetlb_wp() inside hugetlb_no_page(), that can bypass the userfaultfd-wp traps.
A few ways to resolve this:
(1) Skip the CoW optimization for hugetlb private mapping, considering that private mappings for hugetlb should be very rare, so it may not really be helpful to major workloads. The worst case is we only skip the optimization if userfaultfd_wp(vma)==true, because uffd-wp needs another fault anyway.
(2) Move the userfaultfd-wp handling for hugetlb from hugetlb_fault() into hugetlb_wp(). The major cons is there're a bunch of locks taken when calling hugetlb_wp(), and that will make the changeset unnecessarily complicated due to the lock operations.
(3) Skip hugetlb_wp() when uffd-wp bit is still set. It means it requires another hugetlb_fault() to resolve the uffd-wp bit first.
This patch chose option (3) which contains the minimum changeset (simplest for backport) and also make sure hugetlb_wp() itself will start to be always safe with uffd-wp ptes even if called elsewhere in the future.
This patch will be needed for v5.19+ hence copy stable.
--- mm/hugetlb.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
mm/hugetlb.c +12 -2
@@ -5478,7 +5478,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, struct folio *pagecache_folio, spinlock_t *ptl) { const bool unshare = flags & FAULT_FLAG_UNSHARE; -pte_t pte; +pte_t pte =huge_ptep_get (ptep);struct hstate *h =hstate_vma (vma);struct page *old_page;struct folio *new_folio;
@@ -5487,6 +5487,17 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,unsigned long haddr = address &huge_page_mask (h);struct mmu_notifier_range range; +/* +* Never handle CoW for uffd-wp protected pages. It should be only +* handled when the uffd-wp protection is removed. +* +* Note that only the CoW optimization path can trigger this and +* got skipped, because hugetlb_fault() will always resolve uffd-wp +* bit first. +*/ +if (huge_pte_uffd_wp (pte)) +return 0 ; +/* * hugetlb does not support FOLL_FORCE-style write faults that keep the * PTE mapped R/O such as maybe_mkwrite() would do.
@@ -5500,7 +5511,6 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,return 0 ; } - pte =huge_ptep_get (ptep); old_page =pte_page (pte);delayacct_wpcopy_start ();
Signature · 1 line
2.39.1
Muhammad Usama Anjum
Tested-byQuoted 115 lines · Peter Xu
Quoted 94 lines · Muhammad Usama Anjum
Hi Peter,
Sorry for late reply.
Quoted 78 lines · Peter Xu
Quoted 63 lines · David Hildenbrand
Quoted 62 lines · Peter Xu
This patch fixes an issue that a hugetlb uffd-wr-protected mapping can be writable even with uffd-wp bit set. It only happens with all these conditions met: (1) hugetlb memory (2) private mapping (3) original mapping was missing, then (4) being wr-protected (IOW, pte marker installed). Then write to the page to trigger.
Userfaultfd-wp trap for hugetlb was implemented in
hugetlb_fault()before even reachinghugetlb_wp()to avoid taking more locks that userfault won't need. However there's one CoW optimization path for missing hugetlb page that can triggerhugetlb_wp()insidehugetlb_no_page(), that can bypass the userfaultfd-wp traps.A few ways to resolve this:
(1) Skip the CoW optimization for hugetlb private mapping, considering that private mappings for hugetlb should be very rare, so it may not really be helpful to major workloads. The worst case is we only skip the optimization if userfaultfd_wp(vma)==true, because uffd-wp needs another fault anyway.
(2) Move the userfaultfd-wp handling for hugetlb from
hugetlb_fault()intohugetlb_wp(). The major cons is there're a bunch of locks taken when callinghugetlb_wp(), and that will make the changeset unnecessarily complicated due to the lock operations.(3) Carry over uffd-wp bit in
hugetlb_wp(), so it'll need to fault again for uffd-wp privately mapped pages.This patch chose option (3) which contains the minimum changeset (simplest for backport) and also make sure
hugetlb_wp()itself will start to be always safe with uffd-wp ptes even if called elsewhere in the future.This patch will be needed for v5.19+ hence copy stable.
Reported-by Muhammad Usama Anjum <[email protected]>Cc linux-stable <[email protected]>Fixes 166f3ecc0daf ("mm/hugetlb: hook page faults for uffd write protection")Signed-off-by Peter Xu <[email protected]>--- mm/hugetlb.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)mm/hugetlb.c +2
-3@@ -5478,7 +5478,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, struct folio *pagecache_folio, spinlock_t *ptl) { const bool unshare = flags & FAULT_FLAG_UNSHARE; -pte_t pte; +pte_t pte, newpte;struct hstate *h =hstate_vma (vma);struct page *old_page;struct folio *new_folio;@@ -5622,8 +5622,10 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,mmu_notifier_invalidate_range (mm, range.start, range.end);page_remove_rmap (old_page, vma, true);hugepage_add_new_anon_rmap (new_folio, vma, haddr); -set_huge_pte_at (mm, haddr, ptep, -make_huge_pte (vma, &new_folio->page, !unshare)); + newpte =make_huge_pte (vma, &new_folio->page, !unshare);Quoted 8 lines
mm/hugetlb.c static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, · line 5631
Quoted 6 lines
mm/hugetlb.c +3
-0+if (huge_pte_uffd_wp (pte)) + newpte =huge_pte_mkuffd_wp (newpte); +set_huge_pte_at (mm, haddr, ptep, newpte);folio_set_hugetlb_migratable (new_folio);/* Make the old page be freed below */ new_folio =page_folio (old_page);Looks correct to me. Do we have a reproducer?
I used a reproducer for the async mode I wrote (patch 2 attached, need to change to
VM_PRIVATE):Quoted 7 lines
I don't think kernel kselftest can trigger it because we don't do strict checks yet with uffd-wp bits. I've already started looking into cleanup the test cases and I do plan to add new tests to cover this.
Meanwhile, let's also wait for an ack from Muhammad. Even though the async mode is not part of the code base, it'll be a good test for verifying every single uffd-wp bit being set or cleared as expected.
I've tested by applying this patch. But the bug is still there. Just like Peter has mentioned, we are using our in progress patches related to
pagemap_scanioctl and userfaultd wp async patches to reproduce it.Quoted 6 lines
To reproduce please build kernel and run
pagemap_ioctltest in mm inhugetlb_mem_reproducerbranch:
https://gitlab.collabora.com/usama.anjum/linux-mainline/-/tree/hugetlb_mem_reproducerIn case you have any question on how to reproduce, please let me know. I'll try to provide a cleaner alternative.
Hmm, I think my current fix is incomplete if not wrong. The root cause should still be valid, however I overlooked another path:
if (page_mapcount(old_page) == 1 && PageAnon(old_page)) { if (!PageAnonExclusive(old_page)) page_move_anon_rmap(old_page, vma); if (likely(!unshare)) set_huge_ptep_writable(vma, haddr, ptep); delayacct_wpcopy_end(); return 0; }
Quoted 5 lines
We should bail out early in this path, and it'll be even easier we always bail out
hugetlb_wp()as long as uffd-wp is detected because userfault should always be handled before any decision to CoW.v2 attached.. Please give it another shot.
This attached v2 works. Please add:
Quoted 3 lines
Thanks,
Signature · 2 lines
BR, Muhammad Usama Anjum
Quoted 115 lines · Peter Xu
Quoted 94 lines · Muhammad Usama Anjum
Hi Peter,
Sorry for late reply.
Quoted 78 lines · Peter Xu
Quoted 63 lines · David Hildenbrand
Quoted 62 lines · Peter Xu
This patch fixes an issue that a hugetlb uffd-wr-protected mapping can be writable even with uffd-wp bit set. It only happens with all these conditions met: (1) hugetlb memory (2) private mapping (3) original mapping was missing, then (4) being wr-protected (IOW, pte marker installed). Then write to the page to trigger.
Userfaultfd-wp trap for hugetlb was implemented in
hugetlb_fault()before even reachinghugetlb_wp()to avoid taking more locks that userfault won't need. However there's one CoW optimization path for missing hugetlb page that can triggerhugetlb_wp()insidehugetlb_no_page(), that can bypass the userfaultfd-wp traps.A few ways to resolve this:
(1) Skip the CoW optimization for hugetlb private mapping, considering that private mappings for hugetlb should be very rare, so it may not really be helpful to major workloads. The worst case is we only skip the optimization if userfaultfd_wp(vma)==true, because uffd-wp needs another fault anyway.
(2) Move the userfaultfd-wp handling for hugetlb from
hugetlb_fault()intohugetlb_wp(). The major cons is there're a bunch of locks taken when callinghugetlb_wp(), and that will make the changeset unnecessarily complicated due to the lock operations.(3) Carry over uffd-wp bit in
hugetlb_wp(), so it'll need to fault again for uffd-wp privately mapped pages.This patch chose option (3) which contains the minimum changeset (simplest for backport) and also make sure
hugetlb_wp()itself will start to be always safe with uffd-wp ptes even if called elsewhere in the future.This patch will be needed for v5.19+ hence copy stable.
Reported-by Muhammad Usama Anjum <[email protected]>Cc linux-stable <[email protected]>Fixes 166f3ecc0daf ("mm/hugetlb: hook page faults for uffd write protection")Signed-off-by Peter Xu <[email protected]>--- mm/hugetlb.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)mm/hugetlb.c +2
-3@@ -5478,7 +5478,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, struct folio *pagecache_folio, spinlock_t *ptl) { const bool unshare = flags & FAULT_FLAG_UNSHARE; -pte_t pte; +pte_t pte, newpte;struct hstate *h =hstate_vma (vma);struct page *old_page;struct folio *new_folio;@@ -5622,8 +5622,10 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,mmu_notifier_invalidate_range (mm, range.start, range.end);page_remove_rmap (old_page, vma, true);hugepage_add_new_anon_rmap (new_folio, vma, haddr); -set_huge_pte_at (mm, haddr, ptep, -make_huge_pte (vma, &new_folio->page, !unshare)); + newpte =make_huge_pte (vma, &new_folio->page, !unshare);Quoted 8 lines
mm/hugetlb.c static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, · line 5631
Quoted 6 lines
mm/hugetlb.c +3
-0+if (huge_pte_uffd_wp (pte)) + newpte =huge_pte_mkuffd_wp (newpte); +set_huge_pte_at (mm, haddr, ptep, newpte);folio_set_hugetlb_migratable (new_folio);/* Make the old page be freed below */ new_folio =page_folio (old_page);Looks correct to me. Do we have a reproducer?
I used a reproducer for the async mode I wrote (patch 2 attached, need to change to
VM_PRIVATE):Quoted 7 lines
I don't think kernel kselftest can trigger it because we don't do strict checks yet with uffd-wp bits. I've already started looking into cleanup the test cases and I do plan to add new tests to cover this.
Meanwhile, let's also wait for an ack from Muhammad. Even though the async mode is not part of the code base, it'll be a good test for verifying every single uffd-wp bit being set or cleared as expected.
I've tested by applying this patch. But the bug is still there. Just like Peter has mentioned, we are using our in progress patches related to
pagemap_scanioctl and userfaultd wp async patches to reproduce it.Quoted 6 lines
To reproduce please build kernel and run
pagemap_ioctltest in mm inhugetlb_mem_reproducerbranch:
https://gitlab.collabora.com/usama.anjum/linux-mainline/-/tree/hugetlb_mem_reproducerIn case you have any question on how to reproduce, please let me know. I'll try to provide a cleaner alternative.
Hmm, I think my current fix is incomplete if not wrong. The root cause should still be valid, however I overlooked another path:
if (page_mapcount(old_page) == 1 && PageAnon(old_page)) { if (!PageAnonExclusive(old_page)) page_move_anon_rmap(old_page, vma); if (likely(!unshare)) set_huge_ptep_writable(vma, haddr, ptep); delayacct_wpcopy_end(); return 0; }
Quoted 5 lines
We should bail out early in this path, and it'll be even easier we always bail out
hugetlb_wp()as long as uffd-wp is detected because userfault should always be handled before any decision to CoW.v2 attached.. Please give it another shot.
Hmmm, I think you must only do that for !unshare (FAULT_FLAG_WRITE). Otherwise you'll never be able to resolve an unsharing request on a r/o mapped hugetlb page that has the uffd-wp set?
Or am I missing something?
Signature · 3 lines
Thanks, David / dhildenb
Quoted 120 lines · David Hildenbrand
Quoted 119 lines · Peter Xu
Quoted 94 lines · Muhammad Usama Anjum
Hi Peter,
Sorry for late reply.
Quoted 78 lines · Peter Xu
Quoted 63 lines · David Hildenbrand
Quoted 62 lines · Peter Xu
This patch fixes an issue that a hugetlb uffd-wr-protected mapping can be writable even with uffd-wp bit set. It only happens with all these conditions met: (1) hugetlb memory (2) private mapping (3) original mapping was missing, then (4) being wr-protected (IOW, pte marker installed). Then write to the page to trigger.
Userfaultfd-wp trap for hugetlb was implemented in
hugetlb_fault()before even reachinghugetlb_wp()to avoid taking more locks that userfault won't need. However there's one CoW optimization path for missing hugetlb page that can triggerhugetlb_wp()insidehugetlb_no_page(), that can bypass the userfaultfd-wp traps.A few ways to resolve this:
(1) Skip the CoW optimization for hugetlb private mapping, considering that private mappings for hugetlb should be very rare, so it may not really be helpful to major workloads. The worst case is we only skip the optimization if userfaultfd_wp(vma)==true, because uffd-wp needs another fault anyway.
(2) Move the userfaultfd-wp handling for hugetlb from
hugetlb_fault()intohugetlb_wp(). The major cons is there're a bunch of locks taken when callinghugetlb_wp(), and that will make the changeset unnecessarily complicated due to the lock operations.(3) Carry over uffd-wp bit in
hugetlb_wp(), so it'll need to fault again for uffd-wp privately mapped pages.This patch chose option (3) which contains the minimum changeset (simplest for backport) and also make sure
hugetlb_wp()itself will start to be always safe with uffd-wp ptes even if called elsewhere in the future.This patch will be needed for v5.19+ hence copy stable.
Reported-by Muhammad Usama Anjum <[email protected]>Cc linux-stable <[email protected]>Fixes 166f3ecc0daf ("mm/hugetlb: hook page faults for uffd write protection")Signed-off-by Peter Xu <[email protected]>--- mm/hugetlb.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)mm/hugetlb.c +2
-3@@ -5478,7 +5478,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, struct folio *pagecache_folio, spinlock_t *ptl) { const bool unshare = flags & FAULT_FLAG_UNSHARE; -pte_t pte; +pte_t pte, newpte;struct hstate *h =hstate_vma (vma);struct page *old_page;struct folio *new_folio;@@ -5622,8 +5622,10 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,mmu_notifier_invalidate_range (mm, range.start, range.end);page_remove_rmap (old_page, vma, true);hugepage_add_new_anon_rmap (new_folio, vma, haddr); -set_huge_pte_at (mm, haddr, ptep, -make_huge_pte (vma, &new_folio->page, !unshare)); + newpte =make_huge_pte (vma, &new_folio->page, !unshare);Quoted 8 lines
mm/hugetlb.c static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, · line 5631
Quoted 6 lines
mm/hugetlb.c +3
-0+if (huge_pte_uffd_wp (pte)) + newpte =huge_pte_mkuffd_wp (newpte); +set_huge_pte_at (mm, haddr, ptep, newpte);folio_set_hugetlb_migratable (new_folio);/* Make the old page be freed below */ new_folio =page_folio (old_page);Looks correct to me. Do we have a reproducer?
I used a reproducer for the async mode I wrote (patch 2 attached, need to change to
VM_PRIVATE):Quoted 7 lines
I don't think kernel kselftest can trigger it because we don't do strict checks yet with uffd-wp bits. I've already started looking into cleanup the test cases and I do plan to add new tests to cover this.
Meanwhile, let's also wait for an ack from Muhammad. Even though the async mode is not part of the code base, it'll be a good test for verifying every single uffd-wp bit being set or cleared as expected.
I've tested by applying this patch. But the bug is still there. Just like Peter has mentioned, we are using our in progress patches related to
pagemap_scanioctl and userfaultd wp async patches to reproduce it.Quoted 6 lines
To reproduce please build kernel and run
pagemap_ioctltest in mm inhugetlb_mem_reproducerbranch:
https://gitlab.collabora.com/usama.anjum/linux-mainline/-/tree/hugetlb_mem_reproducerIn case you have any question on how to reproduce, please let me know. I'll try to provide a cleaner alternative.
Hmm, I think my current fix is incomplete if not wrong. The root cause should still be valid, however I overlooked another path:
if (page_mapcount(old_page) == 1 && PageAnon(old_page)) { if (!PageAnonExclusive(old_page)) page_move_anon_rmap(old_page, vma); if (likely(!unshare)) set_huge_ptep_writable(vma, haddr, ptep); delayacct_wpcopy_end(); return 0; }We should bail out early in this path, and it'll be even easier we always bail out
hugetlb_wp()as long as uffd-wp is detected because userfault should always be handled before any decision to CoW.
Quoted 7 lines
Quoted 1 line
v2 attached.. Please give it another shot.
Hmmm, I think you must only do that for !unshare (
FAULT_FLAG_WRITE). Otherwise you'll never be able to resolve an unsharing request on a r/o mapped hugetlb page that has the uffd-wp set?Or am I missing something?
No, I think you're right. I'll fix that up when posting a v3. Thanks,
Signature · 1 line
Peter Xu
On 03/21/23 15:18, Peter Xu wrote:
Thanks Peter!
Quoted 4 lines
This patch fixes an issue that a hugetlb uffd-wr-protected mapping can be writable even with uffd-wp bit set. It only happens with all these conditions met: (1) hugetlb memory (2) private mapping (3) original mapping was missing, then (4) being wr-protected (IOW, pte marker installed). Then
^^^^
Nit, but is the word "then" intended to be there? Almost makes it sound as if wr-protected was a result of the previous 3 conditions being met.
Quoted 21 lines
write to the page to trigger.
Userfaultfd-wp trap for hugetlb was implemented in
hugetlb_fault()before even reachinghugetlb_wp()to avoid taking more locks that userfault won't need. However there's one CoW optimization path for missing hugetlb page that can triggerhugetlb_wp()insidehugetlb_no_page(), that can bypass the userfaultfd-wp traps.A few ways to resolve this:
(1) Skip the CoW optimization for hugetlb private mapping, considering that private mappings for hugetlb should be very rare, so it may not really be helpful to major workloads. The worst case is we only skip the optimization if userfaultfd_wp(vma)==true, because uffd-wp needs another fault anyway.
(2) Move the userfaultfd-wp handling for hugetlb from
hugetlb_fault()intohugetlb_wp(). The major cons is there're a bunch of locks taken when callinghugetlb_wp(), and that will make the changeset unnecessarily complicated due to the lock operations.
Quoted 6 lines
(3) Carry over uffd-wp bit in
hugetlb_wp(), so it'll need to fault again for uffd-wp privately mapped pages.This patch chose option (3) which contains the minimum changeset (simplest for backport) and also make sure
hugetlb_wp()itself will start to be always safe with uffd-wp ptes even if called elsewhere in the future.
I was going to suggest (1) as that would be the simplest. But, since (3) makes hugetlb_wp() safe for future callers, that is actually preferred.
Quoted 40 lines
This patch will be needed for v5.19+ hence copy stable.
Reported-by Muhammad Usama Anjum <[email protected]>Cc linux-stable <[email protected]>Fixes 166f3ecc0daf ("mm/hugetlb: hook page faults for uffd write protection")Signed-off-by Peter Xu <[email protected]>--- mm/hugetlb.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)mm/hugetlb.c +5
-3@@ -5478,7 +5478,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, struct folio *pagecache_folio, spinlock_t *ptl) { const bool unshare = flags & FAULT_FLAG_UNSHARE; -pte_t pte; +pte_t pte, newpte;struct hstate *h =hstate_vma (vma);struct page *old_page;struct folio *new_folio;@@ -5622,8 +5622,10 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,mmu_notifier_invalidate_range (mm, range.start, range.end);page_remove_rmap (old_page, vma, true);hugepage_add_new_anon_rmap (new_folio, vma, haddr); -set_huge_pte_at (mm, haddr, ptep, -make_huge_pte (vma, &new_folio->page, !unshare)); + newpte =make_huge_pte (vma, &new_folio->page, !unshare); +if (huge_pte_uffd_wp (pte)) + newpte =huge_pte_mkuffd_wp (newpte); +set_huge_pte_at (mm, haddr, ptep, newpte);folio_set_hugetlb_migratable (new_folio);/* Make the old page be freed below */ new_folio =page_folio (old_page);Signature · 1 line
2.39.1
Signature · 1 line
Mike Kravetz
Quoted 7 lines · Mike Kravetz
Quoted 4 lines
This patch fixes an issue that a hugetlb uffd-wr-protected mapping can be writable even with uffd-wp bit set. It only happens with all these conditions met: (1) hugetlb memory (2) private mapping (3) original mapping was missing, then (4) being wr-protected (IOW, pte marker installed). Then
^^^^Nit, but is the word "then" intended to be there? Almost makes it sound as if wr-protected was a result of the previous 3 conditions being met.
Not intended.. Since this is not fixup-able, I'll reword here when I'll need to repost.
[...]
Quoted 1 line
Reviewed-by Mike Kravetz <[email protected]>
Thanks!
Signature · 1 line
Peter Xu