Skip to contentSkip to conversation

mm/userfaultfd: Allow hugetlb change protection upon poison entry

PATCH Applied [email protected] 3 participants 3 messages Apr 5, 2024 – Apr 8, 2024

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

PATCH 1/1 mm/userfaultfd: Allow hugetlb change protection upon poison entry

From: Peter Xu <[email protected]>

After UFFDIO_POISON, there can be two kinds of hugetlb pte markers, either the POISON one or UFFD_WP one.

Allow change protection to run on a poisoned marker just like !hugetlb cases, ignoring the marker irrelevant of the permission.

Here the two bits are mutual exclusive. For example, when install a poisoned entry it must not be UFFD_WP already (by checking pte_none() before such install). And it also means if UFFD_WP is set there must have no POISON bit set. It makes sense because UFFD_WP is a bit to reflect permission, and permissions do not apply if the pte is poisoned and destined to sigbus.

So here we simply check uffd_wp bit set first, do nothing otherwise.

Attach the Fixes to UFFDIO_POISON work, as before that it should not be possible to have poison entry for hugetlb (e.g., hugetlb doesn't do swap, so no chance of swapin errors).

Cc Axel Rasmussen <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc linux-stable <[email protected]> # 6.6+
Reported-by [email protected]
Fixes fc71884a5f59 ("mm: userfaultfd: add new UFFDIO_POISON ioctl")
---
 mm/hugetlb.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
mm/hugetlb.c +7 -3
index 8267e221ca5d..ba7162441adf 100644
@@ -6960,9 +6960,13 @@ long hugetlb_change_protection(struct vm_area_struct *vma,
 			if (!pte_same(pte, newpte))
 				set_huge_pte_at(mm, address, ptep, newpte, psize);
 		} else if (unlikely(is_pte_marker(pte))) {
-			/* No other markers apply for now. */
-			WARN_ON_ONCE(!pte_marker_uffd_wp(pte));
-			if (uffd_wp_resolve)
+			/*
+			 * Do nothing on a poison marker; page is
+			 * corrupted, permissons do not apply.  Here
+			 * pte_marker_uffd_wp()==true implies !poison
+			 * because they're mutual exclusive.
+			 */
+			if (pte_marker_uffd_wp(pte) && uffd_wp_resolve)
 				/* Safe to modify directly (non-present->none). */
 				huge_pte_clear(mm, address, ptep, psize);
 		} else if (!huge_pte_none(pte)) {
Signature · 1 line
2.44.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 43 lines · On 06.04.24 01:19, [email protected] wrote:

From: Peter Xu <[email protected]>

After UFFDIO_POISON, there can be two kinds of hugetlb pte markers, either the POISON one or UFFD_WP one.

Allow change protection to run on a poisoned marker just like !hugetlb cases, ignoring the marker irrelevant of the permission.

Here the two bits are mutual exclusive. For example, when install a poisoned entry it must not be UFFD_WP already (by checking pte_none() before such install). And it also means if UFFD_WP is set there must have no POISON bit set. It makes sense because UFFD_WP is a bit to reflect permission, and permissions do not apply if the pte is poisoned and destined to sigbus.

So here we simply check uffd_wp bit set first, do nothing otherwise.

Attach the Fixes to UFFDIO_POISON work, as before that it should not be possible to have poison entry for hugetlb (e.g., hugetlb doesn't do swap, so no chance of swapin errors).

Cc Axel Rasmussen <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc linux-stable <[email protected]> # 6.6+
Reported-by [email protected]
Fixes fc71884a5f59 ("mm: userfaultfd: add new UFFDIO_POISON ioctl")
---
  mm/hugetlb.c | 10 +++++++---
  1 file changed, 7 insertions(+), 3 deletions(-)
mm/hugetlb.c +0 -3
index 8267e221ca5d..ba7162441adf 100644
@@ -6960,9 +6960,13 @@ long hugetlb_change_protection(struct vm_area_struct *vma,
  			if (!pte_same(pte, newpte))
  				set_huge_pte_at(mm, address, ptep, newpte, psize);
  		} else if (unlikely(is_pte_marker(pte))) {
-			/* No other markers apply for now. */
-			WARN_ON_ONCE(!pte_marker_uffd_wp(pte));
-			if (uffd_wp_resolve)

mm/hugetlb.c long hugetlb_change_protection(struct vm_area_struct *vma, · line 6972

Quoted 10 lines
mm/hugetlb.c +7 -0
+			/*
+			 * Do nothing on a poison marker; page is
+			 * corrupted, permissons do not apply.  Here
+			 * pte_marker_uffd_wp()==true implies !poison
+			 * because they're mutual exclusive.
+			 */
+			if (pte_marker_uffd_wp(pte) && uffd_wp_resolve)
  				/* Safe to modify directly (non-present->none). */
  				huge_pte_clear(mm, address, ptep, psize);
  		} else if (!huge_pte_none(pte)) {
Signature · 3 lines
Cheers,

David / dhildenb
Reviewed-by
#2 lore raw

Thanks for the fix, Peter!

Quoted 63 lines · David Hildenbrand
Quoted 43 lines · On 06.04.24 01:19, [email protected] wrote:

From: Peter Xu <[email protected]>

After UFFDIO_POISON, there can be two kinds of hugetlb pte markers, either the POISON one or UFFD_WP one.

Allow change protection to run on a poisoned marker just like !hugetlb cases, ignoring the marker irrelevant of the permission.

Here the two bits are mutual exclusive. For example, when install a poisoned entry it must not be UFFD_WP already (by checking pte_none() before such install). And it also means if UFFD_WP is set there must have no POISON bit set. It makes sense because UFFD_WP is a bit to reflect permission, and permissions do not apply if the pte is poisoned and destined to sigbus.

So here we simply check uffd_wp bit set first, do nothing otherwise.

Attach the Fixes to UFFDIO_POISON work, as before that it should not be possible to have poison entry for hugetlb (e.g., hugetlb doesn't do swap, so no chance of swapin errors).

Cc Axel Rasmussen <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc linux-stable <[email protected]> # 6.6+
Reported-by [email protected]
Fixes fc71884a5f59 ("mm: userfaultfd: add new UFFDIO_POISON ioctl")
---
  mm/hugetlb.c | 10 +++++++---
  1 file changed, 7 insertions(+), 3 deletions(-)
mm/hugetlb.c +0 -3
index 8267e221ca5d..ba7162441adf 100644
@@ -6960,9 +6960,13 @@ long hugetlb_change_protection(struct vm_area_struct *vma,
                      if (!pte_same(pte, newpte))
                              set_huge_pte_at(mm, address, ptep, newpte, psize);
              } else if (unlikely(is_pte_marker(pte))) {
-                     /* No other markers apply for now. */
-                     WARN_ON_ONCE(!pte_marker_uffd_wp(pte));
-                     if (uffd_wp_resolve)

mm/hugetlb.c long hugetlb_change_protection(struct vm_area_struct *vma, · line 6972

Quoted 10 lines
mm/hugetlb.c +7 -0
+                     /*
+                      * Do nothing on a poison marker; page is
+                      * corrupted, permissons do not apply.  Here
+                      * pte_marker_uffd_wp()==true implies !poison
+                      * because they're mutual exclusive.
+                      */
+                     if (pte_marker_uffd_wp(pte) && uffd_wp_resolve)
                              /* Safe to modify directly (non-present->none). */
                              huge_pte_clear(mm, address, ptep, psize);
              } else if (!huge_pte_none(pte)) {

--
Cheers,

David / dhildenb