Skip to contentSkip to conversation

userfaultfd: fix a crash in UFFDIO_MOVE with some non-present PMDs

PATCH In review Suren Baghdasaryan 2 participants 6 messages Aug 6, 2025

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

PATCH v3 1/1 userfaultfd: fix a crash in UFFDIO_MOVE with some non-present PMDs

When UFFDIO_MOVE is used with UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES and it encounters a non-present PMD (migration entry), it proceeds with folio access even though the folio is not present. Add the missing check and let split_huge_pmd() handle migration entries.

Fixes adef440691ba ("userfaultfd: UFFDIO_MOVE uABI")
Reported-by [email protected]

---
Changes since v2 [1]
- Updated the title and changelog, per David Hildenbrand
- Removed extra checks for non-present not-migration PMD entries, 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
reviewer Reviewed-by on v3 1/1
#1 lore raw
Quoted 1 line · Suren Baghdasaryan

When UFFDIO_MOVE is used with UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES and it

The migration entry can appear with/without ALLOW_SRC_HOLES, right? Maybe drop this line?

If we need another repost, the subject can further be tailored to mention migration entry too rather than non-present. IMHO that's clearer on explaining the issue this patch is fixing (e.g. a valid transhuge THP can also have present bit cleared).

Quoted 2 lines

encounters a non-present PMD (migration entry), it proceeds with folio access even though the folio is not present. Add the missing check and

IMHO "... even though folio is not present" is pretty vague. Maybe "... even though it's a swap entry"? Fundamentally it's because of the different layouts of normal THP v.s. a swap entry, hence pmd_folio() should not be used on top of swap entries.

Quoted 34 lines

let split_huge_pmd() handle migration entries.

Fixes adef440691ba ("userfaultfd: UFFDIO_MOVE uABI")
Reported-by [email protected]

---
Changes since v2 [1]
- Updated the title and changelog, per David Hildenbrand
- Removed extra checks for non-present not-migration PMD entries, 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 */

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

Quoted 10 lines
mm/userfaultfd.c +9 -0
+				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;
+					}
 				}

The change itself looks all correct, thanks. If you agree with above commit message / subject updates, feel free to take this after some amendment of the commit message:

Quoted 7 lines
mm/userfaultfd.c +0 -0
 				spin_unlock(ptl);
 

base-commit: 8e7e0c6d09502e44aa7a8fce0821e042a6ec03d1

Signature · 1 line
2.50.1.565.gc32cd1483b-goog
Signature · 1 line
Peter Xu
#2 lore raw
Quoted 6 lines · Peter Xu
Quoted 1 line · Suren Baghdasaryan

When UFFDIO_MOVE is used with UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES and it

The migration entry can appear with/without ALLOW_SRC_HOLES, right? Maybe drop this line?

Yes, you are right. I'll update.

Quoted 6 lines

If we need another repost, the subject can further be tailored to mention migration entry too rather than non-present. IMHO that's clearer on explaining the issue this patch is fixing (e.g. a valid transhuge THP can also have present bit cleared).

Quoted 7 lines
Quoted 2 lines

encounters a non-present PMD (migration entry), it proceeds with folio access even though the folio is not present. Add the missing check and

IMHO "... even though folio is not present" is pretty vague. Maybe "... even though it's a swap entry"? Fundamentally it's because of the different layouts of normal THP v.s. a swap entry, hence pmd_folio() should not be used on top of swap entries.

Well, technically a migration entry is a non_swap_entry(), so calling migration entries "swap entries" is confusing to me. Any better wording we can use or do you think that's ok?

Quoted 63 lines
Quoted 35 lines

let split_huge_pmd() handle migration entries.

Fixes adef440691ba ("userfaultfd: UFFDIO_MOVE uABI")
Reported-by [email protected]

---
Changes since v2 [1]
- Updated the title and changelog, per David Hildenbrand
- Removed extra checks for non-present not-migration PMD entries, 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 */

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

Quoted 10 lines
mm/userfaultfd.c +9 -0
+                             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;
+                                     }
                              }

The change itself looks all correct, thanks. If you agree with above commit message / subject updates, feel free to take this after some amendment of the commit message:

Quoted 7 lines
mm/userfaultfd.c +0 -0
                              spin_unlock(ptl);
 

base-commit: 8e7e0c6d09502e44aa7a8fce0821e042a6ec03d1 --
2.50.1.565.gc32cd1483b-goog

--
Peter Xu

reviewer
#3 lore raw
Quoted 19 lines · Suren Baghdasaryan
Quoted 6 lines · Peter Xu
Quoted 1 line · Suren Baghdasaryan

When UFFDIO_MOVE is used with UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES and it

The migration entry can appear with/without ALLOW_SRC_HOLES, right? Maybe drop this line?

Yes, you are right. I'll update.

Quoted 9 lines

If we need another repost, the subject can further be tailored to mention migration entry too rather than non-present. IMHO that's clearer on explaining the issue this patch is fixing (e.g. a valid transhuge THP can also have present bit cleared).

Quoted 2 lines

encounters a non-present PMD (migration entry), it proceeds with folio access even though the folio is not present. Add the missing check and

Quoted 8 lines
Quoted 4 lines

IMHO "... even though folio is not present" is pretty vague. Maybe "... even though it's a swap entry"? Fundamentally it's because of the different layouts of normal THP v.s. a swap entry, hence pmd_folio() should not be used on top of swap entries.

Well, technically a migration entry is a non_swap_entry(), so calling migration entries "swap entries" is confusing to me. Any better wording we can use or do you think that's ok?

The more general definition of "swap entry" should follow what swp_entry_t is defined, where, for example, is_migration_entry() itself takes swp_entry_t as input. So it should be fine, but I agree it's indeed confusing.

If we want to make it clearer, IMHO we could rename non_swap_entry() instead to is_swapfile_entry() / is_real_swap_entry() / ... but that can be discussed separately. Here, if we want to make it super accurate, we could also use "swp_entry_t" instead of "swap entry", that'll be 100% accurate.

Thanks,

Signature · 1 line
Peter Xu
#4 lore raw
Quoted 35 lines · Peter Xu
Quoted 19 lines · Suren Baghdasaryan
Quoted 6 lines · Peter Xu
Quoted 1 line · Suren Baghdasaryan

When UFFDIO_MOVE is used with UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES and it

The migration entry can appear with/without ALLOW_SRC_HOLES, right? Maybe drop this line?

Yes, you are right. I'll update.

Quoted 9 lines

If we need another repost, the subject can further be tailored to mention migration entry too rather than non-present. IMHO that's clearer on explaining the issue this patch is fixing (e.g. a valid transhuge THP can also have present bit cleared).

Quoted 2 lines

encounters a non-present PMD (migration entry), it proceeds with folio access even though the folio is not present. Add the missing check and

Quoted 8 lines
Quoted 4 lines

IMHO "... even though folio is not present" is pretty vague. Maybe "... even though it's a swap entry"? Fundamentally it's because of the different layouts of normal THP v.s. a swap entry, hence pmd_folio() should not be used on top of swap entries.

Well, technically a migration entry is a non_swap_entry(), so calling migration entries "swap entries" is confusing to me. Any better wording we can use or do you think that's ok?

The more general definition of "swap entry" should follow what swp_entry_t is defined, where, for example, is_migration_entry() itself takes swp_entry_t as input. So it should be fine, but I agree it's indeed confusing.

Quoted 4 lines

If we want to make it clearer, IMHO we could rename non_swap_entry() instead to is_swapfile_entry() / is_real_swap_entry() / ... but that can be discussed separately. Here, if we want to make it super accurate, we could also use "swp_entry_t" instead of "swap entry", that'll be 100% accurate.

Ok, that I think is our best option. I'll post an update shortly. Thanks!

Quoted 6 lines

Thanks,

--
Peter Xu

#5 lore raw
Quoted 37 lines · Suren Baghdasaryan
Quoted 35 lines · Peter Xu
Quoted 19 lines · Suren Baghdasaryan
Quoted 6 lines · Peter Xu
Quoted 1 line · Suren Baghdasaryan

When UFFDIO_MOVE is used with UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES and it

The migration entry can appear with/without ALLOW_SRC_HOLES, right? Maybe drop this line?

Yes, you are right. I'll update.

Quoted 9 lines

If we need another repost, the subject can further be tailored to mention migration entry too rather than non-present. IMHO that's clearer on explaining the issue this patch is fixing (e.g. a valid transhuge THP can also have present bit cleared).

Quoted 2 lines

encounters a non-present PMD (migration entry), it proceeds with folio access even though the folio is not present. Add the missing check and

Quoted 8 lines
Quoted 4 lines

IMHO "... even though folio is not present" is pretty vague. Maybe "... even though it's a swap entry"? Fundamentally it's because of the different layouts of normal THP v.s. a swap entry, hence pmd_folio() should not be used on top of swap entries.

Well, technically a migration entry is a non_swap_entry(), so calling migration entries "swap entries" is confusing to me. Any better wording we can use or do you think that's ok?

The more general definition of "swap entry" should follow what swp_entry_t is defined, where, for example, is_migration_entry() itself takes swp_entry_t as input. So it should be fine, but I agree it's indeed confusing.

Quoted 6 lines
Quoted 4 lines

If we want to make it clearer, IMHO we could rename non_swap_entry() instead to is_swapfile_entry() / is_real_swap_entry() / ... but that can be discussed separately. Here, if we want to make it super accurate, we could also use "swp_entry_t" instead of "swap entry", that'll be 100% accurate.

Ok, that I think is our best option. I'll post an update shortly.

Posted at https://lore.kernel.org/all/[email protected]/
Thanks!

Quoted 8 lines

Thanks!

Quoted 6 lines

Thanks,

--
Peter Xu