Skip to contentSkip to conversation

userfaultfd: verify VMA state across UFFDIO_COPY retry

PATCH Applied Mike Rapoport 4 participants 15 messages May 27 – Jun 8

Subsystems (3)
lore
Share & tools
lore

Mike Rapoport

author maintainer
#0 lore raw

v2 cover userfaultfd: verify VMA state across UFFDIO_COPY retry

From: "Mike Rapoport (Microsoft)" <[email protected]>

... and two more small fixes.

This applies on top of the current mm-unstable with "userfaultfd: snapshot VMA state across UFFDIO_COPY retry" reverted.

v2 changes:
* rename vma_snapshot to mfill_retry_state and related functions, variables and constants
* drop redundant check for effective ops
* drop vma_uffd_copy_ops()
* restore large comment about MAP_PRIVATE handling and massage other comments
* new patches:
- guard against bugs that could end up with NULL ops in

    __mfill_atomic_pte()

- remove redundant check in vm_uffd_ops()

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

Mike Rapoport (Microsoft) (3):
userfaultfd: verify VMA state across UFFDIO_COPY retry
userfaultfd: refuse to __mfill_atomic_pte() for unsupported VMAs
userfaultfd: remove redundant check in vm_uffd_ops()

 mm/userfaultfd.c | 92 +++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 79 insertions(+), 13 deletions(-)
Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH v2 1/3 userfaultfd: verify VMA state across UFFDIO_COPY retry

From: "Mike Rapoport (Microsoft)" <[email protected]>

mfill_copy_folio_retry() drops the VMA lock for copy_from_user() and reacquires it afterwards. The destination VMA can be replaced during that window.

The existing check compares vma_uffd_ops() before and after the retry, but if a shmem VMA with MAP_SHARED is replaced with a shmem VMA with MAP_PRIVATE (or vice versa) the replacement goes undetected.

The change from MAP_PRIVATE to MAP_SHARED will treat the folio allocated with shmem_alloc_folio() as anonymous and this will cause BUG() when mfill_atomic_install_pte() will try to folio_add_new_anon_rmap().

The change from MAP_SHARED to MAP_PRIVATE allows injection of folios into the page cache of the original VMA.

There is no need to change for hugetlb because it never uses mfill_copy_folio_retry().

Introduce helpers for more comprehensive comparison of VMA state:
- mfill_retry_state_save() to save the relevant VMA state into a struct mfill_retry_state (original uffd_ops, relevant VMA flags, vm_file and pgoff) before dropping the lock
- mfill_retry_state_changed() to compare the saved state with the state of the VMA acquired after retaking the locks
- mfill_retry_state_put() to release vm_file pinning.

Use DEFINE_FREE() cleanup to wrap mfill_retry_state_put() to avoid complicating error handling paths in mfill_copy_folio_retry().

Fixes 292411fda25b ("mm/userfaultfd: detect VMA type change after copy retry in mfill_copy_folio_retry()")
Fixes 6ab703034f14 ("userfaultfd: mfill_atomic(): remove retry logic")
---
 mm/userfaultfd.c | 85 +++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 73 insertions(+), 12 deletions(-)
mm/userfaultfd.c +73 -12
index 180bad42fc79..e5d2fb3ce2c1 100644
@@ -14,6 +14,8 @@
 #include <linux/userfaultfd_k.h>
 #include <linux/mmu_notifier.h>
 #include <linux/hugetlb.h>
+#include <linux/file.h>
+#include <linux/cleanup.h>
 #include <asm/tlbflush.h>
 #include <asm/tlb.h>
 #include "internal.h"
@@ -443,16 +445,80 @@ static int mfill_copy_folio_locked(struct folio *folio, unsigned long src_addr)
 	return ret;
 }
 
-static int mfill_copy_folio_retry(struct mfill_state *state,
+#define MFILL_RETRY_STATE_VMA_FLAGS \
+	append_vma_flags(__VMA_UFFD_FLAGS, VMA_SHARED_BIT)
+
+/*
+ * VMA state saved before dropping the locks in mfill_copy_folio_retry().
+ * Used to detect VMA replacement or incompatible changes after reacquiring the
+ * locks.
+ */
+struct mfill_retry_state {
+	const struct vm_uffd_ops *ops;
+	struct file *file;
+	vma_flags_t flags;
+	pgoff_t pgoff;
+};
+
+static void mfill_retry_state_save(struct mfill_retry_state *s,
+				   struct vm_area_struct *vma)
+{
+	s->flags = vma_flags_and_mask(&vma->flags, MFILL_RETRY_STATE_VMA_FLAGS);
+	s->ops = vma_uffd_ops(vma);
+	s->pgoff = vma->vm_pgoff;
+
+	if (vma->vm_file)
+		s->file = get_file(vma->vm_file);
+}
+
+static bool mfill_retry_state_changed(struct mfill_retry_state *state,
+				      struct vm_area_struct *vma)
+{
+	vma_flags_t flags = vma_flags_and_mask(&vma->flags,
+					       MFILL_RETRY_STATE_VMA_FLAGS);
+
+	/* Have any UFFD flags (missing, WP, minor) changed? */
+	if (!vma_flags_same_pair(&state->flags, &flags))
+		return true;
+
+	/* VMA type or effective uffd_ops changed while the lock was dropped */
+	if (state->ops != vma_uffd_ops(vma))
+		return true;
+
+	/* VMA was anonymous before; changed only if it no longer is */
+	if (!state->file)
+		return !vma_is_anonymous(vma);
+
+	/* VMA was file backed, but file, inode or offset has changed */
+	if (!vma->vm_file || vma->vm_file->f_inode != state->file->f_inode ||
+	    state->file != vma->vm_file || vma->vm_pgoff != state->pgoff)
+		return true;
+
+	return false;
+}
+
+static void mfill_retry_state_put(struct mfill_retry_state *s)
+{
+	if (s->file)
+		fput(s->file);
+}
+
+DEFINE_FREE(retry_put, struct mfill_retry_state *,
+	    if (_T) mfill_retry_state_put(_T));
+
+static int mfill_copy_folio_retry(struct mfill_state *mfill_state,
 				  struct folio *folio)
 {
-	const struct vm_uffd_ops *orig_ops = vma_uffd_ops(state->vma);
-	unsigned long src_addr = state->src_addr;
+	struct mfill_retry_state retry_state = { 0 };
+	struct mfill_retry_state *for_free __free(retry_put) = &retry_state;
+	unsigned long src_addr = mfill_state->src_addr;
 	void *kaddr;
 	int err;
 
+	mfill_retry_state_save(&retry_state, mfill_state->vma);
+
 	/* retry copying with mm_lock dropped */
-	mfill_put_vma(state);
+	mfill_put_vma(mfill_state);
 
 	kaddr = kmap_local_folio(folio, 0);
 	err = copy_from_user(kaddr, (const void __user *) src_addr, PAGE_SIZE);
@@ -463,19 +529,14 @@ static int mfill_copy_folio_retry(struct mfill_state *state,
 	flush_dcache_folio(folio);
 
 	/* reget VMA and PMD, they could change underneath us */
-	err = mfill_get_vma(state);
+	err = mfill_get_vma(mfill_state);
 	if (err)
 		return err;
 
-	/*
-	 * The VMA type may have changed while the lock was dropped
-	 * (e.g. replaced with a hugetlb mapping), making the caller's
-	 * ops pointer stale.
-	 */
-	if (vma_uffd_ops(state->vma) != orig_ops)
+	if (mfill_retry_state_changed(&retry_state, mfill_state->vma))
 		return -EAGAIN;
 
-	err = mfill_establish_pmd(state);
+	err = mfill_establish_pmd(mfill_state);
 	if (err)
 		return err;
 
Signature · 1 line
2.53.0
Reviewed-by on v2 1/3
#2 lore raw
Quoted 28 lines · Mike Rapoport

From: "Mike Rapoport (Microsoft)" <[email protected]>

mfill_copy_folio_retry() drops the VMA lock for copy_from_user() and reacquires it afterwards. The destination VMA can be replaced during that window.

The existing check compares vma_uffd_ops() before and after the retry, but if a shmem VMA with MAP_SHARED is replaced with a shmem VMA with MAP_PRIVATE (or vice versa) the replacement goes undetected.

The change from MAP_PRIVATE to MAP_SHARED will treat the folio allocated with shmem_alloc_folio() as anonymous and this will cause BUG() when mfill_atomic_install_pte() will try to folio_add_new_anon_rmap().

The change from MAP_SHARED to MAP_PRIVATE allows injection of folios into the page cache of the original VMA.

There is no need to change for hugetlb because it never uses mfill_copy_folio_retry().

Introduce helpers for more comprehensive comparison of VMA state:
- mfill_retry_state_save() to save the relevant VMA state into a struct mfill_retry_state (original uffd_ops, relevant VMA flags, vm_file and pgoff) before dropping the lock
- mfill_retry_state_changed() to compare the saved state with the state of the VMA acquired after retaking the locks
- mfill_retry_state_put() to release vm_file pinning.

Quoted 5 lines

Use DEFINE_FREE() cleanup to wrap mfill_retry_state_put() to avoid complicating error handling paths in mfill_copy_folio_retry().

Fixes 292411fda25b ("mm/userfaultfd: detect VMA type change after copy retry in mfill_copy_folio_retry()")
Fixes 6ab703034f14 ("userfaultfd: mfill_atomic(): remove retry logic")

Did we want a Cc: Stable?

Quoted 6 lines

OK the logic here looks good, thanks for the changes. I have one comment below
re: a redundant check, with that addressed feel free to add:

Quoted 26 lines
---
 mm/userfaultfd.c | 85 +++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 73 insertions(+), 12 deletions(-)
mm/userfaultfd.c +6 -1
index 180bad42fc79..e5d2fb3ce2c1 100644
@@ -14,6 +14,8 @@
 #include <linux/userfaultfd_k.h>
 #include <linux/mmu_notifier.h>
 #include <linux/hugetlb.h>
+#include <linux/file.h>
+#include <linux/cleanup.h>
 #include <asm/tlbflush.h>
 #include <asm/tlb.h>
 #include "internal.h"
@@ -443,16 +445,80 @@ static int mfill_copy_folio_locked(struct folio *folio, unsigned long src_addr)
 	return ret;
 }
 
-static int mfill_copy_folio_retry(struct mfill_state *state,
+#define MFILL_RETRY_STATE_VMA_FLAGS \
+	append_vma_flags(__VMA_UFFD_FLAGS, VMA_SHARED_BIT)
+
+/*

mm/userfaultfd.c static int mfill_copy_folio_locked(struct folio *folio, unsigned long src_addr) · line 461

Quoted 10 lines
mm/userfaultfd.c +10 -0
+ * VMA state saved before dropping the locks in mfill_copy_folio_retry().
+ * Used to detect VMA replacement or incompatible changes after reacquiring the
+ * locks.
+ */
+struct mfill_retry_state {
+	const struct vm_uffd_ops *ops;
+	struct file *file;
+	vma_flags_t flags;
+	pgoff_t pgoff;
+};

Much better thanks!

mm/userfaultfd.c static int mfill_copy_folio_locked(struct folio *folio, unsigned long src_addr) · line 472

Quoted 11 lines
mm/userfaultfd.c +11 -0
+
+static void mfill_retry_state_save(struct mfill_retry_state *s,
+				   struct vm_area_struct *vma)
+{
+	s->flags = vma_flags_and_mask(&vma->flags, MFILL_RETRY_STATE_VMA_FLAGS);
+	s->ops = vma_uffd_ops(vma);
+	s->pgoff = vma->vm_pgoff;
+
+	if (vma->vm_file)
+		s->file = get_file(vma->vm_file);
+}

Yeah can live with the s here :)

Quoted 13 lines
mm/userfaultfd.c +13 -0
+
+static bool mfill_retry_state_changed(struct mfill_retry_state *state,
+				      struct vm_area_struct *vma)
+{
+	vma_flags_t flags = vma_flags_and_mask(&vma->flags,
+					       MFILL_RETRY_STATE_VMA_FLAGS);
+
+	/* Have any UFFD flags (missing, WP, minor) changed? */
+	if (!vma_flags_same_pair(&state->flags, &flags))
+		return true;
+
+	/* VMA type or effective uffd_ops changed while the lock was dropped */
+	if (state->ops != vma_uffd_ops(vma))

mm/userfaultfd.c static int mfill_copy_folio_locked(struct folio *folio, unsigned long src_addr) · line 495

Quoted 10 lines
mm/userfaultfd.c +10 -0
+		return true;
+
+	/* VMA was anonymous before; changed only if it no longer is */
+	if (!state->file)
+		return !vma_is_anonymous(vma);
+
+	/* VMA was file backed, but file, inode or offset has changed */
+	if (!vma->vm_file || vma->vm_file->f_inode != state->file->f_inode ||
+	    state->file != vma->vm_file || vma->vm_pgoff != state->pgoff)
+		return true;

Doesn't state->file != vma->vm_file render the inode check redundant?

Quoted 58 lines
mm/userfaultfd.c +23 -12
+
+	return false;
+}
+
+static void mfill_retry_state_put(struct mfill_retry_state *s)
+{
+	if (s->file)
+		fput(s->file);
+}
+
+DEFINE_FREE(retry_put, struct mfill_retry_state *,
+	    if (_T) mfill_retry_state_put(_T));
+
+static int mfill_copy_folio_retry(struct mfill_state *mfill_state,
 				  struct folio *folio)
 {
-	const struct vm_uffd_ops *orig_ops = vma_uffd_ops(state->vma);
-	unsigned long src_addr = state->src_addr;
+	struct mfill_retry_state retry_state = { 0 };
+	struct mfill_retry_state *for_free __free(retry_put) = &retry_state;
+	unsigned long src_addr = mfill_state->src_addr;
 	void *kaddr;
 	int err;
 
+	mfill_retry_state_save(&retry_state, mfill_state->vma);
+
 	/* retry copying with mm_lock dropped */
-	mfill_put_vma(state);
+	mfill_put_vma(mfill_state);
 
 	kaddr = kmap_local_folio(folio, 0);
 	err = copy_from_user(kaddr, (const void __user *) src_addr, PAGE_SIZE);
@@ -463,19 +529,14 @@ static int mfill_copy_folio_retry(struct mfill_state *state,
 	flush_dcache_folio(folio);
 
 	/* reget VMA and PMD, they could change underneath us */
-	err = mfill_get_vma(state);
+	err = mfill_get_vma(mfill_state);
 	if (err)
 		return err;
 
-	/*
-	 * The VMA type may have changed while the lock was dropped
-	 * (e.g. replaced with a hugetlb mapping), making the caller's
-	 * ops pointer stale.
-	 */
-	if (vma_uffd_ops(state->vma) != orig_ops)
+	if (mfill_retry_state_changed(&retry_state, mfill_state->vma))
 		return -EAGAIN;
 
-	err = mfill_establish_pmd(state);
+	err = mfill_establish_pmd(mfill_state);
 	if (err)
 		return err;
 
--

2.53.0

author maintainer
#3 lore raw
Quoted 29 lines · Lorenzo Stoakes
Quoted 28 lines · Mike Rapoport

From: "Mike Rapoport (Microsoft)" <[email protected]>

mfill_copy_folio_retry() drops the VMA lock for copy_from_user() and reacquires it afterwards. The destination VMA can be replaced during that window.

The existing check compares vma_uffd_ops() before and after the retry, but if a shmem VMA with MAP_SHARED is replaced with a shmem VMA with MAP_PRIVATE (or vice versa) the replacement goes undetected.

The change from MAP_PRIVATE to MAP_SHARED will treat the folio allocated with shmem_alloc_folio() as anonymous and this will cause BUG() when mfill_atomic_install_pte() will try to folio_add_new_anon_rmap().

The change from MAP_SHARED to MAP_PRIVATE allows injection of folios into the page cache of the original VMA.

There is no need to change for hugetlb because it never uses mfill_copy_folio_retry().

Introduce helpers for more comprehensive comparison of VMA state:
- mfill_retry_state_save() to save the relevant VMA state into a struct mfill_retry_state (original uffd_ops, relevant VMA flags, vm_file and pgoff) before dropping the lock
- mfill_retry_state_changed() to compare the saved state with the state of the VMA acquired after retaking the locks
- mfill_retry_state_put() to release vm_file pinning.

Quoted 7 lines
Quoted 5 lines

Use DEFINE_FREE() cleanup to wrap mfill_retry_state_put() to avoid complicating error handling paths in mfill_copy_folio_retry().

Fixes 292411fda25b ("mm/userfaultfd: detect VMA type change after copy retry in mfill_copy_folio_retry()")
Fixes 6ab703034f14 ("userfaultfd: mfill_atomic(): remove retry logic")

Did we want a Cc: Stable?

Andrew adds it when applying.

Quoted 32 lines
Quoted 6 lines

OK the logic here looks good, thanks for the changes. I have one comment below
re: a redundant check, with that addressed feel free to add:

Quoted 20 lines
---
 mm/userfaultfd.c | 85 +++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 73 insertions(+), 12 deletions(-)
mm/userfaultfd.c +16 -0
+static bool mfill_retry_state_changed(struct mfill_retry_state *state,
+				      struct vm_area_struct *vma)
+{
+	vma_flags_t flags = vma_flags_and_mask(&vma->flags,
+					       MFILL_RETRY_STATE_VMA_FLAGS);
+
+	/* Have any UFFD flags (missing, WP, minor) changed? */
+	if (!vma_flags_same_pair(&state->flags, &flags))
+		return true;
+
+	/* VMA type or effective uffd_ops changed while the lock was dropped */
+	if (state->ops != vma_uffd_ops(vma))
+		return true;
+
+	/* VMA was anonymous before; changed only if it no longer is */
+	if (!state->file)
Quoted 8 lines

mm/userfaultfd.c static int mfill_copy_folio_locked(struct folio *folio, unsigned long src_addr) · line 495

Quoted 6 lines
mm/userfaultfd.c +6 -0
+		return !vma_is_anonymous(vma);
+
+	/* VMA was file backed, but file, inode or offset has changed */
+	if (!vma->vm_file || vma->vm_file->f_inode != state->file->f_inode ||
+	    state->file != vma->vm_file || vma->vm_pgoff != state->pgoff)
+		return true;

Doesn't state->file != vma->vm_file render the inode check redundant?

Nope, struct file is TYPESAFE_BY_RCU, it can be recycled with the same file * pointing to different objects.

Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#4 lore raw
Quoted 6 lines · On Thu, 28 May 2026 17:41:55 +0300 Mike Rapoport <[email protected]> wrote:
Quoted 4 lines
Quoted 2 lines
Fixes 292411fda25b ("mm/userfaultfd: detect VMA type change after copy retry in mfill_copy_folio_retry()")
Fixes 6ab703034f14 ("userfaultfd: mfill_atomic(): remove retry logic")

Did we want a Cc: Stable?

Andrew adds it when applying.

Both the above commits were added to 7.1-rc1, so I haven't added cc:stable.

#5 lore raw
In reply to Mike Rapoport
Quoted 33 lines · Mike Rapoport
Quoted 32 lines · Lorenzo Stoakes
Quoted 31 lines · Mike Rapoport

From: "Mike Rapoport (Microsoft)" <[email protected]>

mfill_copy_folio_retry() drops the VMA lock for copy_from_user() and reacquires it afterwards. The destination VMA can be replaced during that window.

The existing check compares vma_uffd_ops() before and after the retry, but if a shmem VMA with MAP_SHARED is replaced with a shmem VMA with MAP_PRIVATE (or vice versa) the replacement goes undetected.

The change from MAP_PRIVATE to MAP_SHARED will treat the folio allocated with shmem_alloc_folio() as anonymous and this will cause BUG() when mfill_atomic_install_pte() will try to folio_add_new_anon_rmap().

The change from MAP_SHARED to MAP_PRIVATE allows injection of folios into the page cache of the original VMA.

There is no need to change for hugetlb because it never uses mfill_copy_folio_retry().

Introduce helpers for more comprehensive comparison of VMA state:
- mfill_retry_state_save() to save the relevant VMA state into a struct mfill_retry_state (original uffd_ops, relevant VMA flags, vm_file and pgoff) before dropping the lock
- mfill_retry_state_changed() to compare the saved state with the state of the VMA acquired after retaking the locks
- mfill_retry_state_put() to release vm_file pinning.

Use DEFINE_FREE() cleanup to wrap mfill_retry_state_put() to avoid complicating error handling paths in mfill_copy_folio_retry().

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Fixes 292411fda25b ("mm/userfaultfd: detect VMA type change after copy retry in mfill_copy_folio_retry()")
Fixes 6ab703034f14 ("userfaultfd: mfill_atomic(): remove retry logic")

Did we want a Cc: Stable?

Andrew adds it when applying.

Hmm, I didn't think this always happened by default? :)

Quoted 42 lines
Quoted 33 lines
Quoted 7 lines

OK the logic here looks good, thanks for the changes. I have one comment below
re: a redundant check, with that addressed feel free to add:

Quoted 20 lines
---
 mm/userfaultfd.c | 85 +++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 73 insertions(+), 12 deletions(-)
mm/userfaultfd.c +16 -0
+static bool mfill_retry_state_changed(struct mfill_retry_state *state,
+				      struct vm_area_struct *vma)
+{
+	vma_flags_t flags = vma_flags_and_mask(&vma->flags,
+					       MFILL_RETRY_STATE_VMA_FLAGS);
+
+	/* Have any UFFD flags (missing, WP, minor) changed? */
+	if (!vma_flags_same_pair(&state->flags, &flags))
+		return true;
+
+	/* VMA type or effective uffd_ops changed while the lock was dropped */
+	if (state->ops != vma_uffd_ops(vma))
+		return true;
+
+	/* VMA was anonymous before; changed only if it no longer is */
+	if (!state->file)
Quoted 8 lines

mm/userfaultfd.c static int mfill_copy_folio_locked(struct folio *folio, unsigned long src_addr) · line 495

Quoted 6 lines
mm/userfaultfd.c +6 -0
+		return !vma_is_anonymous(vma);
+
+	/* VMA was file backed, but file, inode or offset has changed */
+	if (!vma->vm_file || vma->vm_file->f_inode != state->file->f_inode ||
+	    state->file != vma->vm_file || vma->vm_pgoff != state->pgoff)
+		return true;

Doesn't state->file != vma->vm_file render the inode check redundant?

Quoted 2 lines

Nope, struct file is TYPESAFE_BY_RCU, it can be recycled with the same file * pointing to different objects.

Oh lord :)

OK ack on that then!

Quoted 4 lines

--
Sincerely yours,
Mike.

Cheers, Lorenzo

maintainer
#6 lore raw
Quoted 8 lines · On Mon, 8 Jun 2026 14:03:11 +0100 Lorenzo Stoakes <[email protected]> wrote:
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Fixes 292411fda25b ("mm/userfaultfd: detect VMA type change after copy retry in mfill_copy_folio_retry()")
Fixes 6ab703034f14 ("userfaultfd: mfill_atomic(): remove retry logic")

Did we want a Cc: Stable?

Andrew adds it when applying.

Hmm, I didn't think this always happened by default? :)

Nope, adding cc:stable is manual, case-by-case and the -stable maintainers have been asked not to automatically backport MM patches which contain Fixes:.

So this one snuck into mainline without the cc:stable tag.

Thanks for noticing. Greg, Sasha: can we please add mainline's 85668fda932a ("userfaultfd: verify VMA state across UFFDIO_COPY retry") to the backporting pile?

author maintainer
#7 lore raw
Quoted 15 lines · Andrew Morton
Quoted 8 lines · On Mon, 8 Jun 2026 14:03:11 +0100 Lorenzo Stoakes <[email protected]> wrote:
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Fixes 292411fda25b ("mm/userfaultfd: detect VMA type change after copy retry in mfill_copy_folio_retry()")
Fixes 6ab703034f14 ("userfaultfd: mfill_atomic(): remove retry logic")

Did we want a Cc: Stable?

Andrew adds it when applying.

Hmm, I didn't think this always happened by default? :)

Nope, adding cc:stable is manual, case-by-case and the -stable maintainers have been asked not to automatically backport MM patches which contain Fixes:.

Quoted 5 lines

So this one snuck into mainline without the cc:stable tag.

Thanks for noticing. Greg, Sasha: can we please add mainline's 85668fda932a ("userfaultfd: verify VMA state across UFFDIO_COPY retry") to the backporting pile?

There is no need to backport, both commits that 85668fda932a fixes are in 7.1.

Signature · 2 lines
Sincerely yours,
Mike.
author maintainer
#8 lore raw

PATCH v2 2/3 userfaultfd: refuse to __mfill_atomic_pte() for unsupported VMAs

In reply to Mike Rapoport

From: "Mike Rapoport (Microsoft)" <[email protected]>

__mfill_atomic_pte() unconditionally dereferences ops because there is an assumption that VMAs that can undergo mfill_* operations are vetted on registration and must have valid vm_uffd_ops.

Add a guard against potential bugs and make sure __mfill_atomic_pte() bails out if ops is NULL.

Fixes ad9ac3081332 ("userfaultfd: introduce vm_uffd_ops->alloc_folio()")
---
 mm/userfaultfd.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/userfaultfd.c +5 -0
index e5d2fb3ce2c1..2872c71bbf36 100644
@@ -552,6 +552,11 @@ static int __mfill_atomic_pte(struct mfill_state *state,
 	struct folio *folio;
 	int ret;
 
+	if (!ops) {
+		VM_WARN_ONCE(1, "UFFDIO_COPY for unsupported VMA");
+		return -EOPNOTSUPP;
+	}
+
 	folio = ops->alloc_folio(state->vma, state->dst_addr);
 	if (!folio)
 		return -ENOMEM;
Signature · 1 line
2.53.0
#9 lore raw

Re: [PATCH v2 2/3] userfaultfd: refuse to __mfill_atomic_pte() for unsupported VMAs

Hi,

just chiming it

Quoted 22 lines · Mike Rapoport

From: "Mike Rapoport (Microsoft)" <[email protected]>

__mfill_atomic_pte() unconditionally dereferences ops because there is an assumption that VMAs that can undergo mfill_* operations are vetted on registration and must have valid vm_uffd_ops.

Add a guard against potential bugs and make sure __mfill_atomic_pte() bails out if ops is NULL.

Fixes ad9ac3081332 ("userfaultfd: introduce vm_uffd_ops->alloc_folio()")
---
 mm/userfaultfd.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/userfaultfd.c +0 -0
index e5d2fb3ce2c1..2872c71bbf36 100644

mm/userfaultfd.c static int __mfill_atomic_pte(struct mfill_state *state, · line 556

Quoted 5 lines
mm/userfaultfd.c +2 -0
@@ -552,6 +552,11 @@ static int __mfill_atomic_pte(struct mfill_state *state,
        struct folio *folio;
        int ret;
 
+       if (!ops) {
+               VM_WARN_ONCE(1, "UFFDIO_COPY for unsupported VMA");

nit: Is the warning message a bit too specific ? i.e. Also handle MFILL_ATOMIC_ZEROPAGE case.

Quoted 9 lines
mm/userfaultfd.c +3 -1
+               return -EOPNOTSUPP;
+       }
+
        folio = ops->alloc_folio(state->vma, state->dst_addr);
        if (!folio)
                return -ENOMEM;
--

2.53.0

Cheers.

author maintainer
#10 lore raw

Re: [PATCH v2 2/3] userfaultfd: refuse to __mfill_atomic_pte() for unsupported VMAs

Quoted 30 lines · David CARLIER

Hi,

just chiming it

Quoted 25 lines · Mike Rapoport

From: "Mike Rapoport (Microsoft)" <[email protected]>

__mfill_atomic_pte() unconditionally dereferences ops because there is an assumption that VMAs that can undergo mfill_* operations are vetted on registration and must have valid vm_uffd_ops.

Add a guard against potential bugs and make sure __mfill_atomic_pte() bails out if ops is NULL.

Fixes ad9ac3081332 ("userfaultfd: introduce vm_uffd_ops->alloc_folio()")
---
 mm/userfaultfd.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/userfaultfd.c +0 -0
index e5d2fb3ce2c1..2872c71bbf36 100644
@@ -552,6 +552,11 @@ static int __mfill_atomic_pte(struct mfill_state *state,
        struct folio *folio;
        int ret;
 
Quoted 5 lines

mm/userfaultfd.c static int __mfill_atomic_pte(struct mfill_state *state, · line 556

Quoted 2 lines
mm/userfaultfd.c +2 -0
+       if (!ops) {
+               VM_WARN_ONCE(1, "UFFDIO_COPY for unsupported VMA");

nit: Is the warning message a bit too specific ? i.e. Also handle MFILL_ATOMIC_ZEROPAGE case.

It handles ZEROPAGE case by actually copying a zeroed page there so in a sense it's still a copy.

Differentiating COPY and ZEROPAGE here will add complexity that is not justified for a message that should be never printed.

Quoted 12 lines
Quoted 9 lines
mm/userfaultfd.c +3 -1
+               return -EOPNOTSUPP;
+       }
+
        folio = ops->alloc_folio(state->vma, state->dst_addr);
        if (!folio)
                return -ENOMEM;
--

2.53.0

Cheers.

Signature · 2 lines
Sincerely yours,
Mike.
Reviewed-by
#11 lore raw

Re: [PATCH v2 2/3] userfaultfd: refuse to __mfill_atomic_pte() for unsupported VMAs

Quoted 54 lines · Mike Rapoport
Quoted 30 lines · David CARLIER

Hi,

just chiming it

Quoted 25 lines · Mike Rapoport

From: "Mike Rapoport (Microsoft)" <[email protected]>

__mfill_atomic_pte() unconditionally dereferences ops because there is an assumption that VMAs that can undergo mfill_* operations are vetted on registration and must have valid vm_uffd_ops.

Add a guard against potential bugs and make sure __mfill_atomic_pte() bails out if ops is NULL.

Fixes ad9ac3081332 ("userfaultfd: introduce vm_uffd_ops->alloc_folio()")
---
 mm/userfaultfd.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/userfaultfd.c +0 -0
index e5d2fb3ce2c1..2872c71bbf36 100644
@@ -552,6 +552,11 @@ static int __mfill_atomic_pte(struct mfill_state *state,
        struct folio *folio;
        int ret;
 
Quoted 5 lines

mm/userfaultfd.c static int __mfill_atomic_pte(struct mfill_state *state, · line 556

Quoted 2 lines
mm/userfaultfd.c +2 -0
+       if (!ops) {
+               VM_WARN_ONCE(1, "UFFDIO_COPY for unsupported VMA");

nit: Is the warning message a bit too specific ? i.e. Also handle MFILL_ATOMIC_ZEROPAGE case.

It handles ZEROPAGE case by actually copying a zeroed page there so in a sense it's still a copy.

Differentiating COPY and ZEROPAGE here will add complexity that is not justified for a message that should be never printed.

Quoted 10 lines
Quoted 9 lines
mm/userfaultfd.c +3 -1
+               return -EOPNOTSUPP;
+       }
+
        folio = ops->alloc_folio(state->vma, state->dst_addr);
        if (!folio)
                return -ENOMEM;
--

2.53.0

Quoted 6 lines
Quoted 2 lines

Cheers.

--
Sincerely yours,
Mike.

ACK.

Reviewed-by on v2 2/3
#12 lore raw

Re: [PATCH v2 2/3] userfaultfd: refuse to __mfill_atomic_pte() for unsupported VMAs

In reply to Mike Rapoport
Quoted 6 lines · Mike Rapoport

From: "Mike Rapoport (Microsoft)" <[email protected]>

__mfill_atomic_pte() unconditionally dereferences ops because there is an assumption that VMAs that can undergo mfill_* operations are vetted on registration and must have valid vm_uffd_ops.

Quoted 6 lines

Add a guard against potential bugs and make sure __mfill_atomic_pte() bails out if ops is NULL.

Fixes ad9ac3081332 ("userfaultfd: introduce vm_uffd_ops->alloc_folio()")

LGTM, so:

Quoted 23 lines
---
 mm/userfaultfd.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/userfaultfd.c +5 -1
index e5d2fb3ce2c1..2872c71bbf36 100644
@@ -552,6 +552,11 @@ static int __mfill_atomic_pte(struct mfill_state *state,
 	struct folio *folio;
 	int ret;
 
+	if (!ops) {
+		VM_WARN_ONCE(1, "UFFDIO_COPY for unsupported VMA");
+		return -EOPNOTSUPP;
+	}
+
 	folio = ops->alloc_folio(state->vma, state->dst_addr);
 	if (!folio)
 		return -ENOMEM;
--

2.53.0

author maintainer
#13 lore raw

PATCH v2 3/3 userfaultfd: remove redundant check in vm_uffd_ops()

In reply to Mike Rapoport

From: "Mike Rapoport (Microsoft)" <[email protected]>

Lorenzo says:

  static const struct vm_uffd_ops *vma_uffd_ops(struct vm_area_struct *vma)
  {
          if (vma_is_anonymous(vma))
                  return &anon_uffd_ops;
          return vma->vm_ops ? vma->vm_ops->uffd_ops : NULL;
  }

This is doing a redundant check _and_ making life confusing, as if !vma->vm_ops is a condition that can be reached there, it can't, as vma_is_anonymous() is literally a !vma->vm_ops check :)

Remove the redundant check.

Fixes 0f48947c4232 ("userfaultfd: introduce vm_uffd_ops")
---
 mm/userfaultfd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/userfaultfd.c +1 -1
index 2872c71bbf36..80cc8be5725f 100644
@@ -68,7 +68,7 @@ static const struct vm_uffd_ops *vma_uffd_ops(struct vm_area_struct *vma)
 {
 	if (vma_is_anonymous(vma))
 		return &anon_uffd_ops;
-	return vma->vm_ops ? vma->vm_ops->uffd_ops : NULL;
+	return vma->vm_ops->uffd_ops;
 }
 
 static __always_inline
Signature · 1 line
2.53.0
Reviewed-by on v2 3/3
#14 lore raw

Re: [PATCH v2 3/3] userfaultfd: remove redundant check in vm_uffd_ops()

Quoted 15 lines · Mike Rapoport

From: "Mike Rapoport (Microsoft)" <[email protected]>

Lorenzo says:

  static const struct vm_uffd_ops *vma_uffd_ops(struct vm_area_struct *vma)
  {
          if (vma_is_anonymous(vma))
                  return &anon_uffd_ops;
          return vma->vm_ops ? vma->vm_ops->uffd_ops : NULL;
  }

This is doing a redundant check _and_ making life confusing, as if !vma->vm_ops is a condition that can be reached there, it can't, as vma_is_anonymous() is literally a !vma->vm_ops check :)

Quoted 5 lines

Remove the redundant check.

Fixes 0f48947c4232 ("userfaultfd: introduce vm_uffd_ops")

:) thanks! LGTM so:

Quoted 18 lines
---
 mm/userfaultfd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/userfaultfd.c +1 -2
index 2872c71bbf36..80cc8be5725f 100644
@@ -68,7 +68,7 @@ static const struct vm_uffd_ops *vma_uffd_ops(struct vm_area_struct *vma)
 {
 	if (vma_is_anonymous(vma))
 		return &anon_uffd_ops;
-	return vma->vm_ops ? vma->vm_ops->uffd_ops : NULL;
+	return vma->vm_ops->uffd_ops;
 }
 
 static __always_inline
--
Quoted 2 lines

2.53.0

Cheers, Lorenzo