Skip to contentSkip to conversation

userfaultfd: fix a crash in UFFDIO_MOVE when PMD is a migration entry

PATCH Superseded Suren Baghdasaryan 2 participants 6 messages Aug 6, 2025 – Aug 7, 2025

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 userfaultfd: fix a crash in UFFDIO_MOVE when PMD is a migration entry

When UFFDIO_MOVE encounters a migration PMD entry, it proceeds with obtaining a folio and accessing it even though the entry is swp_entry_t. Add the missing check and let split_huge_pmd() handle migration entries.

---
Changes since v3 [1]
- Updated the title and changelog, per Peter Xu
- Added Reviewed-by: per Peter Xu

[1] https://lore.kernel.org/all/[email protected]/

 mm/userfaultfd.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
mm/userfaultfd.c +10 -7
index 5431c9dd7fd7..116481606be8 100644
@@ -1826,13 +1826,16 @@ ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start,
 			/* Check if we can move the pmd without splitting it. */
 			if (move_splits_huge_pmd(dst_addr, src_addr, src_start + len) ||
 			    !pmd_none(dst_pmdval)) {
-				struct folio *folio = pmd_folio(*src_pmd);
-
-				if (!folio || (!is_huge_zero_folio(folio) &&
-					       !PageAnonExclusive(&folio->page))) {
-					spin_unlock(ptl);
-					err = -EBUSY;
-					break;
+				/* Can be a migration entry */
+				if (pmd_present(*src_pmd)) {
+					struct folio *folio = pmd_folio(*src_pmd);
+
+					if (!folio || (!is_huge_zero_folio(folio) &&
+						       !PageAnonExclusive(&folio->page))) {
+						spin_unlock(ptl);
+						err = -EBUSY;
+						break;
+					}
 				}
 
 				spin_unlock(ptl);

base-commit: 8e7e0c6d09502e44aa7a8fce0821e042a6ec03d1

Signature · 1 line
2.50.1.565.gc32cd1483b-goog
#1 lore raw
Quoted 30 lines · Suren Baghdasaryan

When UFFDIO_MOVE encounters a migration PMD entry, it proceeds with obtaining a folio and accessing it even though the entry is swp_entry_t. Add the missing check and let split_huge_pmd() handle migration entries.

---
Changes since v3 [1]
- Updated the title and changelog, per Peter Xu
- Added Reviewed-by: per Peter Xu

[1] https://lore.kernel.org/all/[email protected]/

  mm/userfaultfd.c | 17 ++++++++++-------
  1 file changed, 10 insertions(+), 7 deletions(-)
mm/userfaultfd.c +0 -2
index 5431c9dd7fd7..116481606be8 100644
@@ -1826,13 +1826,16 @@ ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start,
  			/* Check if we can move the pmd without splitting it. */
  			if (move_splits_huge_pmd(dst_addr, src_addr, src_start + len) ||
  			    !pmd_none(dst_pmdval)) {
-				struct folio *folio = pmd_folio(*src_pmd);
-

mm/userfaultfd.c ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start, · line 1833

Quoted 10 lines
mm/userfaultfd.c +5 -5
-				if (!folio || (!is_huge_zero_folio(folio) &&
-					       !PageAnonExclusive(&folio->page))) {
-					spin_unlock(ptl);
-					err = -EBUSY;
-					break;
+				/* Can be a migration entry */
+				if (pmd_present(*src_pmd)) {
+					struct folio *folio = pmd_folio(*src_pmd);
+
+					if (!folio

How could you get !folio here? That only makes sense when calling vm_normal_folio_pmd(), no?

Signature · 3 lines
Cheers,

David / dhildenb
#2 lore raw
Quoted 38 lines · David Hildenbrand
Quoted 36 lines · Suren Baghdasaryan

When UFFDIO_MOVE encounters a migration PMD entry, it proceeds with obtaining a folio and accessing it even though the entry is swp_entry_t. Add the missing check and let split_huge_pmd() handle migration entries.

---
Changes since v3 [1]
- Updated the title and changelog, per Peter Xu
- Added Reviewed-by: per Peter Xu

[1] https://lore.kernel.org/all/[email protected]/

  mm/userfaultfd.c | 17 ++++++++++-------
  1 file changed, 10 insertions(+), 7 deletions(-)
mm/userfaultfd.c +1 -7
index 5431c9dd7fd7..116481606be8 100644
@@ -1826,13 +1826,16 @@ ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start,
                      /* Check if we can move the pmd without splitting it. */
                      if (move_splits_huge_pmd(dst_addr, src_addr, src_start + len) ||
                          !pmd_none(dst_pmdval)) {
-                             struct folio *folio = pmd_folio(*src_pmd);
-
-                             if (!folio || (!is_huge_zero_folio(folio) &&
-                                            !PageAnonExclusive(&folio->page))) {
-                                     spin_unlock(ptl);
-                                     err = -EBUSY;
-                                     break;
+                             /* Can be a migration entry */
Quoted 8 lines

mm/userfaultfd.c ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start,

Quoted 4 lines
mm/userfaultfd.c +4 -0
+                             if (pmd_present(*src_pmd)) {
+                                     struct folio *folio = pmd_folio(*src_pmd);
+
+                                     if (!folio

How could you get !folio here? That only makes sense when calling vm_normal_folio_pmd(), no?

Yes, I think you are right, this check is not needed. I can fold it into this fix or post a separate cleanup patch. I'm guessing a separate patch would be better?

Quoted 7 lines

--
Cheers,

David / dhildenb

#3 lore raw
Quoted 48 lines · Suren Baghdasaryan
Quoted 38 lines · David Hildenbrand
Quoted 36 lines · Suren Baghdasaryan

When UFFDIO_MOVE encounters a migration PMD entry, it proceeds with obtaining a folio and accessing it even though the entry is swp_entry_t. Add the missing check and let split_huge_pmd() handle migration entries.

---
Changes since v3 [1]
- Updated the title and changelog, per Peter Xu
- Added Reviewed-by: per Peter Xu

[1] https://lore.kernel.org/all/[email protected]/

   mm/userfaultfd.c | 17 ++++++++++-------
   1 file changed, 10 insertions(+), 7 deletions(-)
mm/userfaultfd.c +1 -7
index 5431c9dd7fd7..116481606be8 100644
@@ -1826,13 +1826,16 @@ ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start,
                       /* Check if we can move the pmd without splitting it. */
                       if (move_splits_huge_pmd(dst_addr, src_addr, src_start + len) ||
                           !pmd_none(dst_pmdval)) {
-                             struct folio *folio = pmd_folio(*src_pmd);
-
-                             if (!folio || (!is_huge_zero_folio(folio) &&
-                                            !PageAnonExclusive(&folio->page))) {
-                                     spin_unlock(ptl);
-                                     err = -EBUSY;
-                                     break;
+                             /* Can be a migration entry */
Quoted 8 lines

mm/userfaultfd.c ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start,

Quoted 4 lines
mm/userfaultfd.c +4 -0
+                             if (pmd_present(*src_pmd)) {
+                                     struct folio *folio = pmd_folio(*src_pmd);
+
+                                     if (!folio

How could you get !folio here? That only makes sense when calling vm_normal_folio_pmd(), no?

Quoted 3 lines

Yes, I think you are right, this check is not needed. I can fold it into this fix or post a separate cleanup patch. I'm guessing a separate patch would be better?

I think you can just post a fixup inline here and ask Andrew to squash it. He will shout if he wants a completely new version :)

Signature · 3 lines
Cheers,

David / dhildenb
#4 lore raw
Quoted 50 lines · David Hildenbrand
Quoted 48 lines · Suren Baghdasaryan
Quoted 46 lines · David Hildenbrand
Quoted 30 lines · Suren Baghdasaryan

When UFFDIO_MOVE encounters a migration PMD entry, it proceeds with obtaining a folio and accessing it even though the entry is swp_entry_t. Add the missing check and let split_huge_pmd() handle migration entries.

---
Changes since v3 [1]
- Updated the title and changelog, per Peter Xu
- Added Reviewed-by: per Peter Xu

[1] https://lore.kernel.org/all/[email protected]/

   mm/userfaultfd.c | 17 ++++++++++-------
   1 file changed, 10 insertions(+), 7 deletions(-)
mm/userfaultfd.c +0 -2
index 5431c9dd7fd7..116481606be8 100644
@@ -1826,13 +1826,16 @@ ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start,
                       /* Check if we can move the pmd without splitting it. */
                       if (move_splits_huge_pmd(dst_addr, src_addr, src_start + len) ||
                           !pmd_none(dst_pmdval)) {
-                             struct folio *folio = pmd_folio(*src_pmd);
-

mm/userfaultfd.c ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start, · line 1833

Quoted 10 lines
mm/userfaultfd.c +5 -5
-                             if (!folio || (!is_huge_zero_folio(folio) &&
-                                            !PageAnonExclusive(&folio->page))) {
-                                     spin_unlock(ptl);
-                                     err = -EBUSY;
-                                     break;
+                             /* Can be a migration entry */
+                             if (pmd_present(*src_pmd)) {
+                                     struct folio *folio = pmd_folio(*src_pmd);
+
+                                     if (!folio

How could you get !folio here? That only makes sense when calling vm_normal_folio_pmd(), no?

Quoted 6 lines
Quoted 3 lines

Yes, I think you are right, this check is not needed. I can fold it into this fix or post a separate cleanup patch. I'm guessing a separate patch would be better?

I think you can just post a fixup inline here and ask Andrew to squash it. He will shout if he wants a completely new version :)

I wouldn't do that to him! :)
Let me quickly send an updated version instead.

Quoted 6 lines

--
Cheers,

David / dhildenb

#5 lore raw
Quoted 56 lines · Suren Baghdasaryan
Quoted 54 lines · David Hildenbrand
Quoted 51 lines · Suren Baghdasaryan
Quoted 38 lines · David Hildenbrand
Quoted 36 lines · Suren Baghdasaryan

When UFFDIO_MOVE encounters a migration PMD entry, it proceeds with obtaining a folio and accessing it even though the entry is swp_entry_t. Add the missing check and let split_huge_pmd() handle migration entries.

---
Changes since v3 [1]
- Updated the title and changelog, per Peter Xu
- Added Reviewed-by: per Peter Xu

[1] https://lore.kernel.org/all/[email protected]/

   mm/userfaultfd.c | 17 ++++++++++-------
   1 file changed, 10 insertions(+), 7 deletions(-)
mm/userfaultfd.c +1 -7
index 5431c9dd7fd7..116481606be8 100644
@@ -1826,13 +1826,16 @@ ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start,
                       /* Check if we can move the pmd without splitting it. */
                       if (move_splits_huge_pmd(dst_addr, src_addr, src_start + len) ||
                           !pmd_none(dst_pmdval)) {
-                             struct folio *folio = pmd_folio(*src_pmd);
-
-                             if (!folio || (!is_huge_zero_folio(folio) &&
-                                            !PageAnonExclusive(&folio->page))) {
-                                     spin_unlock(ptl);
-                                     err = -EBUSY;
-                                     break;
+                             /* Can be a migration entry */
Quoted 8 lines

mm/userfaultfd.c ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start,

Quoted 4 lines
mm/userfaultfd.c +4 -0
+                             if (pmd_present(*src_pmd)) {
+                                     struct folio *folio = pmd_folio(*src_pmd);
+
+                                     if (!folio

How could you get !folio here? That only makes sense when calling vm_normal_folio_pmd(), no?

Yes, I think you are right, this check is not needed. I can fold it into this fix or post a separate cleanup patch. I'm guessing a separate patch would be better?

Quoted 5 lines
Quoted 2 lines

I think you can just post a fixup inline here and ask Andrew to squash it. He will shout if he wants a completely new version :)

I wouldn't do that to him! :)
Let me quickly send an updated version instead.

Update posted at
https://lore.kernel.org/all/[email protected]/

Quoted 7 lines
Quoted 7 lines

--
Cheers,

David / dhildenb