Skip to contentSkip to conversation

mm/userfaultfd: check exclusivity of the page being moved

PATCH Superseded nakixii 4 participants 5 messages Nov 17 – Nov 18

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

nakixii

author
#0 lore raw

PATCH 1/1 mm/userfaultfd: check exclusivity of the page being moved

move_pages_ptes() gets the folio of a present source PTE and then checks PageAnonExclusive() on the first page of that folio. PG_anon_exclusive is per page, so for a PTE-mapped large folio this can give the wrong answer for the page that is actually being moved.

This happens after fork(). fork() clears the flag on every page of the folio. Once the child exits, a write fault in the parent can reuse the large folio, and do_wp_page() sets the flag again only on the faulting page. Reclaim can leave the same state behind when try_to_unmap() replaces the earlier pages with swap entries and then fails on a later one.

Moving that page with UFFDIO_MOVE then fails with -EBUSY, even though the folio could be split and the page moved. Callers that fall back to UFFDIO_COPY on -EBUSY end up copying instead.

Check the flag on the page mapped by the source PTE and get the folio from that page. Locking, refcounting and the split-and-retry path stay the same. The batched path only handles small folios and the PMD path already checks the page the PMD maps, so they don't need this.

Tested in an x86_64 KVM guest on mm-unstable with only 64K anonymous mTHP enabled: map a 64K-aligned region backed by one large folio, fork, let the child exit, write to the sixth page and move it. Without this patch all 20 moves failed with -EBUSY. With it all 20 worked and the data was correct. Moving the first page right after fork() still fails with -EBUSY, as it should. The move tests in uffd-unit-tests pass with and without the patch (the two swap tests were skipped, there is no swap in the guest). I didn't reproduce the reclaim case and didn't test arm64.

I found this with Claude Code while debugging an out-of-tree swap backend, and it wrote the first version of the fix. This version, the changelog and the reproducer were also done with AI help. I've reviewed all of it.

Fixes adef440691ba ("userfaultfd: UFFDIO_MOVE uABI")

---

Notes:
Split out of the RFC linked above, as Kairui suggested, and rebased onto mm-unstable (81188836663676f8). The code is the same as in the RFC; I rewrote the comment and the changelog.

 mm/userfaultfd.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
mm/userfaultfd.c +9 -2
index d65184d9a..ae9ee210b 100644
@@ -1557,6 +1557,7 @@ static long move_pages_ptes(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd
 		 */
 		if (!src_folio) {
 			struct folio *folio;
+			struct page *page;
 			bool locked;
 
 			/*
@@ -1570,8 +1571,14 @@ static long move_pages_ptes(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd
 				goto out;
 			}
 
-			folio = vm_normal_folio(src_vma, src_addr, orig_src_pte);
-			if (!folio || !PageAnonExclusive(&folio->page)) {
+			/*
+			 * PG_anon_exclusive is per page. The first page of a
+			 * large folio can be shared while the page mapped here
+			 * is still exclusive, so check the page being moved.
+			 */
+			page = vm_normal_page(src_vma, src_addr, orig_src_pte);
+			folio = page ? page_folio(page) : NULL;
+			if (!folio || !PageAnonExclusive(page)) {
 				spin_unlock(src_ptl);
 				ret = -EBUSY;
 				goto out;

base-commit: 81188836663676f8503f3603cfe56e88264c4d50

Signature · 1 line
2.43.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · nakixii

move_pages_ptes() gets the folio of a present source PTE and then checks PageAnonExclusive() on the first page of that folio. PG_anon_exclusive is per page, so for a PTE-mapped large folio this can give the wrong answer for the page that is actually being moved.

This happens after fork(). fork() clears the flag on every page of the folio. Once the child exits, a write fault in the parent can reuse the large folio, and do_wp_page() sets the flag again only on the faulting page.

COW reuse of PTE-mapped large folios only appeared with commit 1da190f4d0a6 ("mm: Copy-on-Write (COW) reuse support for PTE-mapped THP"), so this scenario didn't exist when UFFDIO_MOVE was added.

Quoted 3 lines

Reclaim can leave the same state behind when try_to_unmap() replaces the earlier pages with swap entries and then fails on a later one.

This one, and fork() with a single page of a PTE-mapped large folio pinned, could produce a mix of exclusive and shared pages in a folio before that, so the Fixes tag is right, but please make it clear in the changelog that the fork() + reuse case is a later addition.

Quoted 3 lines

Moving that page with UFFDIO_MOVE then fails with -EBUSY, even though the folio could be split and the page moved. Callers that fall back to UFFDIO_COPY on -EBUSY end up copying instead.

It also works the other way around: if the first page is exclusive and the page being moved is not, we needlessly split the folio before the retry notices the page is shared and returns -EBUSY.

Worth mentioning that as well.

...

Quoted 1 line

Please use a known identity in Signed-off-by rather than a nickname, see "Developer's Certificate of Origin" in Documentation/process/submitting-patches.rst.

...

mm/userfaultfd.c static long move_pages_ptes(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd · line 1581

Quoted 10 lines
mm/userfaultfd.c +8 -2
-			folio = vm_normal_folio(src_vma, src_addr, orig_src_pte);
-			if (!folio || !PageAnonExclusive(&folio->page)) {
+			/*
+			 * PG_anon_exclusive is per page. The first page of a
+			 * large folio can be shared while the page mapped here
+			 * is still exclusive, so check the page being moved.
+			 */
+			page = vm_normal_page(src_vma, src_addr, orig_src_pte);
+			folio = page ? page_folio(page) : NULL;
+			if (!folio || !PageAnonExclusive(page)) {

This would be simpler as

			page = vm_normal_page(src_vma, src_addr, orig_src_pte);
			if (!page || !PageAnonExclusive(page)) {
				spin_unlock(src_ptl);
				ret = -EBUSY;
				goto out;
			}

			folio = page_folio(page);

mm/userfaultfd.c static long move_pages_ptes(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd · line 1584

Quoted 3 lines
mm/userfaultfd.c +0 -0
 				spin_unlock(src_ptl);
 				ret = -EBUSY;
 				goto out;

With that addressed

Signature · 2 lines
Sincerely yours,
Mike.
#2 lore raw
Quoted 6 lines · Mike Rapoport
Quoted 5 lines · nakixii

move_pages_ptes() gets the folio of a present source PTE and then checks PageAnonExclusive() on the first page of that folio. PG_anon_exclusive is per page, so for a PTE-mapped large folio this can give the wrong answer for the page that is actually being moved.

Quoted 8 lines
Quoted 4 lines

This happens after fork(). fork() clears the flag on every page of the folio. Once the child exits, a write fault in the parent can reuse the large folio, and do_wp_page() sets the flag again only on the faulting page.

COW reuse of PTE-mapped large folios only appeared with commit 1da190f4d0a6 ("mm: Copy-on-Write (COW) reuse support for PTE-mapped THP"), so this scenario didn't exist when UFFDIO_MOVE was added.

You could construct something odd even without that by using MADV_DONTFORK on parts of the folio to then fork().

Signature · 3 lines
Cheers,

David
#3 lore raw
In reply to nakixii
Quoted 12 lines · nakixii

move_pages_ptes() gets the folio of a present source PTE and then checks PageAnonExclusive() on the first page of that folio. PG_anon_exclusive is per page, so for a PTE-mapped large folio this can give the wrong answer for the page that is actually being moved.

This happens after fork(). fork() clears the flag on every page of the folio. Once the child exits, a write fault in the parent can reuse the large folio, and do_wp_page() sets the flag again only on the faulting page. Reclaim can leave the same state behind when try_to_unmap() replaces the earlier pages with swap entries and then fails on a later one.

Quoted 3 lines

Moving that page with UFFDIO_MOVE then fails with -EBUSY, even though the folio could be split and the page moved. Callers that fall back to UFFDIO_COPY on -EBUSY end up copying instead.

Ah, I was about to say, nothing bad will happen because we reject to move large folios.

So this is pretty harmless and pretty uncommon to hit.

Quoted 4 lines
mm/userfaultfd.c +0 -0
@@ -1570,8 +1571,14 @@ static long move_pages_ptes(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd
 				goto out;
 			}
 

mm/userfaultfd.c static long move_pages_ptes(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd · line 1581

Quoted 10 lines
mm/userfaultfd.c +8 -2
-			folio = vm_normal_folio(src_vma, src_addr, orig_src_pte);
-			if (!folio || !PageAnonExclusive(&folio->page)) {
+			/*
+			 * PG_anon_exclusive is per page. The first page of a
+			 * large folio can be shared while the page mapped here
+			 * is still exclusive, so check the page being moved.
+			 */
+			page = vm_normal_page(src_vma, src_addr, orig_src_pte);
+			folio = page ? page_folio(page) : NULL;
+			if (!folio || !PageAnonExclusive(page)) {

Yeah, we could have also moved it after the split and test for an early folio_likely_mapped_shared() first, but doesn't really matter.

Signature · 3 lines
Cheers,

David
#4 lore raw
In reply to nakixii
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] mm/userfaultfd: Signed-off-by trailer uses handle instead of real name --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] mm/userfaultfd: Signed-off-by trailer uses handle instead of real name
--

commit 888d38f960fc9868b79456db2818f9a7357f85a5
Author: nakixii <[email protected]>

Quoted 33 lines

mm/userfaultfd: check exclusivity of the page being moved

move_pages_ptes() gets the folio of a present source PTE and then checks PageAnonExclusive() on the first page of that folio. PG_anon_exclusive is per page, so for a PTE-mapped large folio this can give the wrong answer for the page that is actually being moved.

This happens after fork(). fork() clears the flag on every page of the folio. Once the child exits, a write fault in the parent can reuse the large folio, and do_wp_page() sets the flag again only on the faulting page. Reclaim can leave the same state behind when try_to_unmap() replaces the earlier pages with swap entries and then fails on a later one.

Moving that page with UFFDIO_MOVE then fails with -EBUSY, even though the folio could be split and the page moved. Callers that fall back to UFFDIO_COPY on -EBUSY end up copying instead.

Check the flag on the page mapped by the source PTE and get the folio from that page. Locking, refcounting and the split-and-retry path stay the same. The batched path only handles small folios and the PMD path already checks the page the PMD maps, so they don't need this.

Tested in an x86_64 KVM guest on mm-unstable with only 64K anonymous mTHP enabled: map a 64K-aligned region backed by one large folio, fork, let the child exit, write to the sixth page and move it. Without this patch all 20 moves failed with -EBUSY. With it all 20 worked and the data was correct. Moving the first page right after fork() still fails with -EBUSY, as it should. The move tests in uffd-unit-tests pass with and without the patch (the two swap tests were skipped, there is no swap in the guest). I didn't reproduce the reclaim case and didn't test arm64.

Quoted 8 lines

I found this with Claude Code while debugging an out-of-tree swap backend, and it wrote the first version of the fix. This version, the changelog and the reproducer were also done with AI help. I've reviewed all of it.

Fixes adef440691ba ("userfaultfd: UFFDIO_MOVE uABI")

[Severity: High]
Per Documentation/process/submitting-patches.rst, the Author header and Signed-off-by trailer must use a real name rather than a handle or username ("nakixii") to satisfy the Developer's Certificate of Origin.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1