mm/userfaultfd: detect VMA replacement after copy retry in mfill_copy_folio_retry()
Subsystems (2)
PATCH v4 1/1 mm/userfaultfd: detect VMA replacement after copy retry in mfill_copy_folio_retry()
In mfill_copy_folio_retry(), all locks are dropped to retry copy_from_user() with page faults enabled. During this window, the VMA can be replaced entirely (e.g. munmap + mmap + UFFDIO_REGISTER by another thread), but the caller proceeds with a folio allocated from the original VMA's backing store.
Checking ops alone is insufficient: the replacement VMA could be the same type (e.g. shmem -> shmem) with identical flags but a different backing inode. Take a snapshot of the VMA's file and flags before dropping locks, and compare after re-acquiring them. If anything changed, bail out with -EINVAL.
Use get_file()/fput() rather than ihold()/iput() to hold the file reference across the lock-dropped window, avoiding potential deadlocks from filesystem eviction under mmap_lock.
--- mm/userfaultfd.c | 63 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 7 deletions(-)
mm/userfaultfd.c +56 -7
@@ -443,33 +443,82 @@ static int mfill_copy_folio_locked(struct folio *folio, unsigned long src_addr)return ret; } +struct vma_snapshot { +struct file *file; +vma_flags_t flags; +}; + +static void vma_snapshot_take (struct vm_area_struct *vma, +struct vma_snapshot *s) +{ +memcpy (&s->flags, &vma->flags,sizeof (s->flags)); +if (vma->vm_file) + s->file =get_file (vma->vm_file); +else + s->file =NULL ; +} + +static bool vma_snapshot_changed (struct vm_area_struct *vma, +struct vma_snapshot *s) +{ +if (memcmp (&s->flags, &vma->flags,sizeof (s->flags))) +return true; + +if (s->file && (!vma->vm_file || + vma->vm_file->f_inode != s->file->f_inode)) +return true; + +if (!s->file && !vma_is_anonymous (vma)) +return true; + +return false; +} + +static void vma_snapshot_release (struct vma_snapshot *s) +{ +if (s->file) { +fput (s->file); + s->file =NULL ; + } +} +static int mfill_copy_folio_retry (struct mfill_state *state,struct folio *folio) {unsigned long src_addr = state->src_addr; +struct vma_snapshot s;void *kaddr;int err; +/* Take a quick snapshot of the current vma */ +vma_snapshot_take (state->vma, &s); +/* retry copying with mm_lock dropped */ mfill_put_vma (state); kaddr =kmap_local_folio (folio,0 ); err =copy_from_user (kaddr, (const void __user *) src_addr,PAGE_SIZE );kunmap_local (kaddr); -if (unlikely (err)) -return -EFAULT ; +if (unlikely (err)) { + err = -EFAULT ; + goto out; + }flush_dcache_folio (folio);/* reget VMA and PMD, they could change underneath us */ err =mfill_get_vma (state);if (err) -return err; + goto out; - err =mfill_establish_pmd (state); -if (err) -return err; +if (vma_snapshot_changed (state->vma, &s)) { + err = -EINVAL ; + goto out; + } -return 0 ; + err =mfill_establish_pmd (state); +out: +vma_snapshot_release (&s); +return err; } static int __mfill_atomic_pte(struct mfill_state *state,
Signature · 1 line
2.53.0
Andrew Morton
maintainerQuoted 12 lines · On Tue, 31 Mar 2026 14:41:58 +0100 David Carlier <[email protected]> wrote:
In
mfill_copy_folio_retry(), all locks are dropped to retrycopy_from_user()with page faults enabled. During this window, the VMA can be replaced entirely (e.g. munmap + mmap +UFFDIO_REGISTERby another thread), but the caller proceeds with a folio allocated from the original VMA's backing store.Checking ops alone is insufficient: the replacement VMA could be the same type (e.g. shmem -> shmem) with identical flags but a different backing inode. Take a snapshot of the VMA's file and flags before dropping locks, and compare after re-acquiring them. If anything changed, bail out with
-EINVAL.
Quoted 3 lines
Use get_file()/fput() rather than ihold()/iput() to hold the file reference across the lock-dropped window, avoiding potential deadlocks from filesystem eviction under
mmap_lock.
Thanks, I've queued this as a squashable fix against mm-unstable's "shmem, userfaultfd: implement shmem uffd operations using vm_uffd_ops ongoing".
I've fumbled the ball on your [2/2] unlikely() fix ;). Please resend that after -rc1.
Mike Rapoport
maintainerHi Andrew,
Quoted 7 lines · Andrew Morton
Quoted 5 lines · On Tue, 31 Mar 2026 14:41:58 +0100 David Carlier <[email protected]> wrote:
In
mfill_copy_folio_retry(), all locks are dropped to retrycopy_from_user()with page faults enabled. During this window, the VMA can be replaced entirely (e.g. munmap + mmap +UFFDIO_REGISTERby another thread), but the caller proceeds with a folio allocated from the original VMA's backing store.
What does "folio allocated from the original VMA's backing store" exactly mean? Why is this a problem?
Quoted 6 lines
Quoted 6 lines
Checking ops alone is insufficient: the replacement VMA could be the same type (e.g. shmem -> shmem) with identical flags but a different backing inode. Take a snapshot of the VMA's file and flags before dropping locks, and compare after re-acquiring them. If anything changed, bail out with
-EINVAL.
Quoted 7 lines
Quoted 3 lines
Use get_file()/fput() rather than ihold()/iput() to hold the file reference across the lock-dropped window, avoiding potential deadlocks from filesystem eviction under
mmap_lock.Thanks, I've queued this as a squashable fix against mm-unstable's "shmem, userfaultfd: implement shmem uffd operations using
vm_uffd_opsongoing".
First, this a pre-existing and TBH quite theoretical bug and it was there since the very beginning, so it should not be added as a fixup for the uffd+guestmemfd series.
Second, I have reservations about vma_snapshot implementation. What invariant does it exactly enforce?
Quoted 2 lines
I've fumbled the ball on your [2/2]
unlikely()fix ;). Please resend that after -rc1.
This one should go the same route IMO.
Signature · 2 lines
Sincerely yours, Mike.
Hi Mike,
Quoted 2 lines · Mike Rapoport
What does "folio allocated from the original VMA's backing store" exactly mean? Why is this a problem?
Fair point, the commit message was vague here. What I meant is:
mfill_atomic_pte_copy() captures ops = vma_uffd_ops(state->vma) and passes it to __mfill_atomic_pte(). There, ops->alloc_folio() allocates a folio for the original VMA's inode (e.g. a shmem folio for that specific shmem inode). Then mfill_copy_folio_retry() drops all locks for the copy_from_user retry. After mfill_get_vma() re-acquires them, state->vma may now point to a replacement VMA, but ops is still the stale pointer from before the drop.
The code then calls ops->filemap_add(folio, state->vma, ...) which would insert a folio allocated for the old inode into the new VMA's backing store. If the VMA changed type entirely (e.g. shmem -> anon), ops->filemap_add could be operating on a VMA that has no business receiving this folio.
Quoted 3 lines
First, this a pre-existing and TBH quite theoretical bug and it was there since the very beginning, so it should not be added as a fixup for the uffd+guestmemfd series.
You're right. The race window (VMA replacement during the lock-dropped copy retry) existed in the original mcopy_atomic_pte() code long before the vm_uffd_ops refactoring. The Fixes tag pointing at 56a3706fd7f9 was wrong. I'll drop it and resend as a standalone fix against the original retry logic.
Quoted 2 lines
Second, I have reservations about
vma_snapshotimplementation. What invariant does it exactly enforce?
The invariant I was going for: "the folio we allocated is still compatible with the VMA we're about to install it into." Since alloc_folio() allocates from the VMA's backing file (inode), checking that vm_file is still the same after re-acquiring locks ensures the folio matches the inode. The vm_flags comparison was a secondary guard against permission/type changes during the window.
That said, I can see the vma_snapshot abstraction is doing too much for what's really needed. Would a simpler approach work better — just saving vm_file (with get_file/fput) before the drop and comparing it directly after re-acquiring? That makes the invariant explicit: "same backing file means the folio is valid for this VMA."
Happy to rework along those lines, or if you have a different approach in mind I'm open to suggestions.
Quoted 4 lines
Quoted 2 lines
I've fumbled the ball on your [2/2]
unlikely()fix ;). Please resend that after -rc1.This one should go the same route IMO.
Agreed, I'll resend both after -rc1.
Peter Xu
reviewerQuoted 7 lines · David CARLIER
The invariant I was going for: "the folio we allocated is still compatible with the VMA we're about to install it into." Since
alloc_folio()allocates from the VMA's backing file (inode), checking thatvm_fileis still the same after re-acquiring locks ensures the folio matches the inode. Thevm_flagscomparison was a secondary guard against permission/type changes during the window.
Quoted 5 lines
That said, I can see the
vma_snapshotabstraction is doing too much for what's really needed. Would a simpler approach work better — just savingvm_file(with get_file/fput) before the drop and comparing it directly after re-acquiring? That makes the invariant explicit: "same backing file means the folio is valid for this VMA."
IMHO the flags is needed, consider a shared shmem vma remapped to a private shmem vma. That needs to be covered in the fix.
Actually instead of reducing checks, maybe we also need to check the offset of the mapping too, that is: vma->vm_pgoff can't change otherwise it may also affect how the back store would behave on this UFFDIO_COPY request.
For that, see the example of shmem_get_pgoff_policy() where it seems we can apply different policies to different ranges of the back store.
Thanks,
Signature · 1 line
Peter Xu
Hi Peter,
Quoted 2 lines · Peter Xu
IMHO the flags is needed, consider a shared shmem vma remapped to a private shmem vma. That needs to be covered in the fix.
Right, I hadn't considered that case. Shared->private changes how
the
folio gets handled even with the same backing file. I'll keep the
flags
check.
Quoted 1 line
Actually instead of reducing checks, maybe we also need to check
the offset
Quoted 2 lines
of the mapping too, that is:
vma->vm_pgoffcan't change otherwise it may also affect how the back store would behave on thisUFFDIO_COPY
request.
Quoted 2 lines
For that, see the example of
shmem_get_pgoff_policy()where it
seems we can
Quoted 1 line
apply different policies to different ranges of the back store.
Good point. If vm_pgoff changes, linear_page_index() derives a different page cache offset for the same virtual address, and shmem_get_pgoff_policy() could apply a different NUMA policy to that range. So the folio could end up at the wrong offset or with the wrong placement.
I'll add vm_pgoff to the snapshot. So the full set of checks after re-acquiring locks would be: vm_file, vm_flags, and vm_pgoff — ensuring the folio was allocated for the right backing file, at the right
offset,
with the right VMA type.
Thanks
Peter Xu
reviewerQuoted 28 lines · David CARLIER
Hi Peter,
Quoted 2 lines · Peter Xu
IMHO the flags is needed, consider a shared shmem vma remapped to a private shmem vma. That needs to be covered in the fix.
Right, I hadn't considered that case.
Shared->privatechanges how
the
folio gets handled even with the same backing file. I'll keep the
flags
check.Quoted 1 line
Actually instead of reducing checks, maybe we also need to check
the offset
Quoted 2 lines
of the mapping too, that is:
vma->vm_pgoffcan't change otherwise it may also affect how the back store would behave on thisUFFDIO_COPYrequest.
Quoted 2 lines
For that, see the example of
shmem_get_pgoff_policy()where itseems we can
Quoted 1 line
apply different policies to different ranges of the back store.
Good point. If
vm_pgoffchanges,linear_page_index()derives a different page cache offset for the same virtual address, andshmem_get_pgoff_policy()could apply a different NUMA policy to that range. So the folio could end up at the wrong offset or with the wrong placement.
Quoted 5 lines
I'll add
vm_pgoffto the snapshot. So the full set of checks after re-acquiring locks would be:vm_file,vm_flags, andvm_pgoff— ensuring the folio was allocated for the right backing file, at the right
offset,
with the right VMA type.
When caching the offset, we should likely use linear_page_index() with the address provided rather than caching vma->vm_pgoff directly, then it'll avoid same vm_pgoff while VMA mapping shifted like this:
VMA1: vm_pgoff=0x10000, vm_start=0x10000
VMA2: vm_pgoff=0x10000, vm_start=0xf000
So VMA1 unmapped, then the app mappped VMA2 at different VA but still cover the address we're requesting for UFFDIO_COPY, even if the VMA will still have the same vm_pgoff, the VA to access the same offset might change. Using linear_page_index() will be accurate, IIUC.
The other thing is I just noticed the err code was changed to -EINVAL for snapshot changed cases, sorry I didn't follow previously as closely on the discussion. I think it should be -EAGAIN. It's because the userapp can't resolve -EINVAL failures and app will crash. In a VMA change use case, we should return -EAGAIN to imply the app to retry, rather than crashing.
Thanks,
Signature · 1 line
Peter Xu
Quoted 2 lines · Peter Xu
When caching the offset, we should likely use
linear_page_index()with the address provided rather than cachingvma->vm_pgoffdirectly, then
it'll
Quoted 4 lines
avoid same
vm_pgoffwhile VMA mapping shifted like this:VMA1: vm_pgoff=0x10000, vm_start=0x10000
VMA2: vm_pgoff=0x10000, vm_start=0xf000
Makes sense. linear_page_index() folds in vm_start so it catches the shifted mapping case where raw vm_pgoff alone wouldn't. I'll snapshot the computed pgoff instead.
Quoted 4 lines
The other thing is I just noticed the err code was changed to
-EINVALfor snapshot changed cases, sorry I didn't follow previously as closely on the discussion. I think it should be-EAGAIN. It's because the userapp can't resolve-EINVALfailures and app will crash. In a VMA change use
case, we
Quoted 1 line
should return
-EAGAINto imply the app to retry, rather than crashing.
Right, -EAGAIN is the correct choice here. The VMA changed underneath us, but that's a transient condition the app can recover from by retrying. Will fix.
So v5 will snapshot: vm_file (with get_file/fput), vm_flags, and linear_page_index(vma, dst_addr). Return -EAGAIN if any changed.
Thanks for the detailed review
Mike Rapoport
maintainerHi David,
It feels that you use an LLM for correspondence. Please tune it down to produce more laconic and to the point responses.
Quoted 10 lines · David CARLIER
Quoted 2 lines · Mike Rapoport
What does "folio allocated from the original VMA's backing store" exactly mean? Why is this a problem?
Fair point, the commit message was vague here. What I meant is:
mfill_atomic_pte_copy()captures ops =vma_uffd_ops(state->vma)and passes it to__mfill_atomic_pte(). There,ops->alloc_folio()allocates a folio for the original VMA's inode (e.g. a shmem folio for that specific shmem inode).
I wouldn't say ->alloc_folio() allocates a folio _for_ the inode, it allocates it with inode's memory policy. Worst can happen without any changes is that the allocated folio will end up in a wrong node.
This is still a footgun, but I don't see it as a big deal. Let's revisit it after -rc1 and please make sure to cc "MEMORY MAPPING" folks for insights about how to better track VMA changes or their absence.
Quoted 4 lines
Then
mfill_copy_folio_retry()drops all locks for
thecopy_from_userretry. Aftermfill_get_vma()re-acquires them,state->vmamay now point to a replacement VMA, but ops is still the stale pointer from before the drop.
And this is a bug in my uffd refactoring, and it needs to be fixed ASAP with a simple comparison of old ops and new ops.
Quoted 8 lines
Quoted 2 lines
Second, I have reservations about
vma_snapshotimplementation. What invariant does it exactly enforce?The invariant I was going for: "the folio we allocated is still compatible with the VMA we're about to install it into." Since
alloc_folio()allocates from the VMA's backing file (inode), checking thatvm_fileis still the same after re-acquiring locks ensures the folio matches the inode.
Again, it's not that folio matches the inode, but folio is allocated using the correct mempolicy.
Quoted 2 lines
The
vm_flagscomparison was a secondary guard against permission/type changes during the window.
Permissions should be fine, they are checked in userfaultfd_register. Some other flags that don't matter to uffd operation may change during the window, though and then a comparison of vm_flags will give a false positive.
Signature · 2 lines
Sincerely yours, Mike.
Mike Rapoport
maintainerQuoted 6 lines · Peter Xu
The other thing is I just noticed the err code was changed to
-EINVALfor snapshot changed cases, sorry I didn't follow previously as closely on the discussion. I think it should be-EAGAIN. It's because the userapp can't resolve-EINVALfailures and app will crash. In a VMA change use case, we should return-EAGAINto imply the app to retry, rather than crashing.
No. The return value should express that the VMA is invalid. -EINVAL could work, but looking now at the manual -ENOENT would be even better:
ENOENT (since Linux 4.11)
The faulting process has changed its virtual memory layout simultaneously with an outstanding UFFDIO_COPY operation.
Quoted 3 lines
Thanks,
Signature · 1 line
Peter Xu
Signature · 2 lines
Sincerely yours, Mike.
Understood on all points. Will rework patch 1 to a simple ops
comparison with -ENOENT, drop vma_snapshot entirely. Will cc MEMORY MAPPING folks.
Holding off until after -rc1, will resend both then.
Cheers.
Quoted 23 lines · Mike Rapoport
Quoted 6 lines · Peter Xu
The other thing is I just noticed the err code was changed to
-EINVALfor snapshot changed cases, sorry I didn't follow previously as closely on the discussion. I think it should be-EAGAIN. It's because the userapp can't resolve-EINVALfailures and app will crash. In a VMA change use case, we should return-EAGAINto imply the app to retry, rather than crashing.No. The return value should express that the VMA is invalid.
-EINVALcould work, but looking now at the manual-ENOENTwould be even better:ENOENT (since Linux 4.11)
The faulting process has changed its virtual memory layout simultaneously with an outstandingUFFDIO_COPYoperation.Quoted 3 lines
Thanks,
--
Peter Xu--
Sincerely yours,
Mike.
Peter Xu
reviewerHi, Mike,
Quoted 8 lines · Mike Rapoport
Quoted 6 lines · Peter Xu
The other thing is I just noticed the err code was changed to
-EINVALfor snapshot changed cases, sorry I didn't follow previously as closely on the discussion. I think it should be-EAGAIN. It's because the userapp can't resolve-EINVALfailures and app will crash. In a VMA change use case, we should return-EAGAINto imply the app to retry, rather than crashing.
Quoted 6 lines
No. The return value should express that the VMA is invalid.
-EINVALcould work, but looking now at the manual-ENOENTwould be even better:ENOENT (since Linux 4.11)
The faulting process has changed its virtual memory layout simultaneously with an outstandingUFFDIO_COPYoperation.
The VMA changed, but it doesn't mean the UFFDIO_COPY becomes illegal, am I right?
For example, I wonder if it's possible someone runs soft-dirty concurrently with userfaultfd, we shouldn't fail the userapp if there's a concurrent thread collecting dirty information, which IIUC can cause VMA flag changes, and should be benign, and I think there can be other things causing the interruption too.
-EAGAIN essentially requested the user to retry. If the VMA is not legal to be operated anymore, it will fail later. However we should allow legal users to pass.
Thanks,
Signature · 1 line
Peter Xu
Peter Xu
reviewerHi, Mike,
Let me also leave my comments inline just for you to consider.
Quoted 12 lines · Mike Rapoport
Hi David,
It feels that you use an LLM for correspondence. Please tune it down to produce more laconic and to the point responses.
Quoted 6 lines · David CARLIER
Quoted 2 lines · Mike Rapoport
What does "folio allocated from the original VMA's backing store" exactly mean? Why is this a problem?
Fair point, the commit message was vague here. What I meant is:
Quoted 8 lines
Quoted 4 lines
mfill_atomic_pte_copy()captures ops =vma_uffd_ops(state->vma)and passes it to__mfill_atomic_pte(). There,ops->alloc_folio()allocates a folio for the original VMA's inode (e.g. a shmem folio for that specific shmem inode).I wouldn't say ->alloc_folio() allocates a folio _for_ the inode, it allocates it with inode's memory policy. Worst can happen without any changes is that the allocated folio will end up in a wrong node.
For shmem it's only about mempolicy indeed, but since we're trying to export it as an API in the series, IMHO it would be nice to be generic. So we shouldn't assume it's only about mempolicy, we should rely on detecting any context change and bail out with -EAGAIN, relying all rest checks to the next UFFDIO_COPY ioctl done on top of the new mapping topology.
Quoted 2 lines
This is still a footgun, but I don't see it as a big deal.
IIUC this is a real bug reported. Actually, if my understanding is correct, we should be able to easily write a reproducer by registering the src addr of UFFDIO_COPY to userfaultfd too, then the ioctl(UFFDIO_COPY) thread will get blocked faulting in the src_addr. During that, we can change the VMA layout in another thread to test whatever setup we want.
Quoted 2 lines
Let's revisit it after -rc1 and please make sure to cc "MEMORY MAPPING" folks for insights about how to better track VMA changes or their absence.
No strong feeling here if we want to slightly postpone this fix. It looks like not easy to happen as it looks to be a bug present for a while, indeed.
It's just that if my understanding is correct, with above reproducer we can crash the kernel easily without a proper fix.
Quoted 21 lines
Quoted 5 lines
Then
mfill_copy_folio_retry()drops all locks for
thecopy_from_userretry. Aftermfill_get_vma()re-acquires them,state->vmamay now point to a replacement VMA, but ops is still the stale pointer from before the drop.And this is a bug in my uffd refactoring, and it needs to be fixed ASAP with a simple comparison of old ops and new ops.
Quoted 8 lines
Quoted 2 lines
Second, I have reservations about
vma_snapshotimplementation. What invariant does it exactly enforce?The invariant I was going for: "the folio we allocated is still compatible with the VMA we're about to install it into." Since
alloc_folio()allocates from the VMA's backing file (inode), checking thatvm_fileis still the same after re-acquiring locks ensures the folio matches the inode.Again, it's not that folio matches the inode, but folio is allocated using the correct mempolicy.
Quoted 7 lines
Quoted 2 lines
The
vm_flagscomparison was a secondary guard against permission/type changes during the window.Permissions should be fine, they are checked in
userfaultfd_register. Some other flags that don't matter to uffd operation may change during the window, though and then a comparison ofvm_flagswill give a false positive.
IMHO false positive is fine in this case when -EAGAIN will be used (which I still think we should), if it only causes a retry.
Thanks,
Signature · 1 line
Peter Xu
Quoted 28 lines · Mike Rapoport
Hi Andrew,
Quoted 7 lines · Andrew Morton
Quoted 5 lines · On Tue, 31 Mar 2026 14:41:58 +0100 David Carlier <[email protected]> wrote:
In
mfill_copy_folio_retry(), all locks are dropped to retrycopy_from_user()with page faults enabled. During this window, the VMA can be replaced entirely (e.g. munmap + mmap +UFFDIO_REGISTERby another thread), but the caller proceeds with a folio allocated from the original VMA's backing store.What does "folio allocated from the original VMA's backing store" exactly mean? Why is this a problem?
Quoted 6 lines
Quoted 6 lines
Checking ops alone is insufficient: the replacement VMA could be the same type (e.g. shmem -> shmem) with identical flags but a different backing inode. Take a snapshot of the VMA's file and flags before dropping locks, and compare after re-acquiring them. If anything changed, bail out with
-EINVAL.Quoted 7 lines
Quoted 3 lines
Use get_file()/fput() rather than ihold()/iput() to hold the file reference across the lock-dropped window, avoiding potential deadlocks from filesystem eviction under
mmap_lock.Thanks, I've queued this as a squashable fix against mm-unstable's "shmem, userfaultfd: implement shmem uffd operations using
vm_uffd_opsongoing".
Quoted 6 lines
First, this a pre-existing and TBH quite theoretical bug and it was there since the very beginning, so it should not be added as a fixup for the uffd+guestmemfd series.
Second, I have reservations about
vma_snapshotimplementation. What invariant does it exactly enforce?
Yeah me too.
Unfortunately my bandwidth is a bit limited at the moment, but if you're comparing VMAs like this it seems something is fundamentally broken.
We should definitely at least delay this until next cycle for consideration I think until we can figure out a sensible approach.
Quoted 5 lines
Quoted 3 lines
I've fumbled the ball on your [2/2]
unlikely()fix ;). Please resend that after -rc1.This one should go the same route IMO.
Agreed, let's delay until next cycle please.
Quoted 4 lines
--
Sincerely yours,
Mike.
Thanks, Lorenzo
Mike Rapoport
maintainerQuoted 14 lines · Peter Xu
Hi, Mike,
Quoted 11 lines · Mike Rapoport
Quoted 6 lines · Peter Xu
The other thing is I just noticed the err code was changed to
-EINVALfor snapshot changed cases, sorry I didn't follow previously as closely on the discussion. I think it should be-EAGAIN. It's because the userapp can't resolve-EINVALfailures and app will crash. In a VMA change use case, we should return-EAGAINto imply the app to retry, rather than crashing.No. The return value should express that the VMA is invalid.
-EINVALcould work, but looking now at the manual-ENOENTwould be even better:
Quoted 6 lines
Quoted 3 lines
ENOENT (since Linux 4.11)
The faulting process has changed its virtual memory layout simultaneously with an outstandingUFFDIO_COPYoperation.The VMA changed, but it doesn't mean the
UFFDIO_COPYbecomes illegal, am I right?
I don't think that "munmap + mmap + userfault_register"
during an outstanding UFFDIO_COPY to the same range is, hmm, the smartest thing to do, and I think aborting the outstanding UFFDIO_COPY in such case is better than allowing it to continue.
Quoted 5 lines
For example, I wonder if it's possible someone runs soft-dirty concurrently with userfaultfd, we shouldn't fail the userapp if there's a concurrent thread collecting dirty information, which IIUC can cause VMA flag changes, and should be benign, and I think there can be other things causing the interruption too.
Right, we shouldn't fail if some of the VMA flags changed, but we are talking about of complete change of the mapping, with potentially completely different backing store.
Quoted 3 lines
Thanks,
Signature · 1 line
Peter Xu
Signature · 2 lines
Sincerely yours, Mike.
Mike Rapoport
maintainerHi Peter,
Quoted 26 lines · Peter Xu
Hi, Mike,
Let me also leave my comments inline just for you to consider.
Quoted 12 lines · Mike Rapoport
Hi David,
It feels that you use an LLM for correspondence. Please tune it down to produce more laconic and to the point responses.
Quoted 6 lines · David CARLIER
Quoted 2 lines · Mike Rapoport
What does "folio allocated from the original VMA's backing store" exactly mean? Why is this a problem?
Fair point, the commit message was vague here. What I meant is:
Quoted 8 lines
Quoted 4 lines
mfill_atomic_pte_copy()captures ops =vma_uffd_ops(state->vma)and passes it to__mfill_atomic_pte(). There,ops->alloc_folio()allocates a folio for the original VMA's inode (e.g. a shmem folio for that specific shmem inode).I wouldn't say ->alloc_folio() allocates a folio _for_ the inode, it allocates it with inode's memory policy. Worst can happen without any changes is that the allocated folio will end up in a wrong node.
Quoted 5 lines
For shmem it's only about mempolicy indeed, but since we're trying to export it as an API in the series, IMHO it would be nice to be generic. So we shouldn't assume it's only about mempolicy, we should rely on detecting any context change and bail out with
-EAGAIN, relying all rest checks to the nextUFFDIO_COPYioctl done on top of the new mapping topology.
My point was that this is preexisting bug and that we don't need to rush with the complete fix that will extensively compare VMA compatibility...
Quoted 8 lines
Quoted 1 line
This is still a footgun, but I don't see it as a big deal.
IIUC this is a real bug reported. Actually, if my understanding is correct, we should be able to easily write a reproducer by registering the src addr of
UFFDIO_COPYto userfaultfd too, then theioctl(UFFDIO_COPY)thread will get blocked faulting in thesrc_addr. During that, we can change the VMA layout in another thread to test whatever setup we want.
Quoted 8 lines
Quoted 2 lines
Let's revisit it after -rc1 and please make sure to cc "MEMORY MAPPING" folks for insights about how to better track VMA changes or their absence.
No strong feeling here if we want to slightly postpone this fix. It looks like not easy to happen as it looks to be a bug present for a while, indeed.
It's just that if my understanding is correct, with above reproducer we can crash the kernel easily without a proper fix.
... but we do need a more urgent fix for the case when a VMA suddenly becomes hugetlb, because that could not happen before the refactoring.
For that, it would be enough to check that ->ops are the same before and after copy_from_user().
@David, do you mind to send a patch for this without waiting for rc1?
Quoted 10 lines
Quoted 7 lines
Quoted 2 lines
The
vm_flagscomparison was a secondary guard against permission/type changes during the window.Permissions should be fine, they are checked in
userfaultfd_register. Some other flags that don't matter to uffd operation may change during the window, though and then a comparison ofvm_flagswill give a false positive.IMHO false positive is fine in this case when
-EAGAINwill be used (which I still think we should), if it only causes a retry.
I still disagree, but let's postpone this discussion for later, when David resends the patch that compares VMA properties.
Quoted 3 lines
Thanks,
Signature · 1 line
Peter Xu
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerQuoted 19 lines · Mike Rapoport
Quoted 18 lines · Peter Xu
Hi, Mike,
Quoted 14 lines · Mike Rapoport
Quoted 6 lines · Peter Xu
The other thing is I just noticed the err code was changed to
-EINVALfor snapshot changed cases, sorry I didn't follow previously as closely on the discussion. I think it should be-EAGAIN. It's because the userapp can't resolve-EINVALfailures and app will crash. In a VMA change use case, we should return-EAGAINto imply the app to retry, rather than crashing.No. The return value should express that the VMA is invalid.
-EINVALcould work, but looking now at the manual-ENOENTwould be even better:ENOENT (since Linux 4.11)
The faulting process has changed its virtual memory layout simultaneously with an outstandingUFFDIO_COPYoperation.
Quoted 7 lines
Quoted 2 lines
The VMA changed, but it doesn't mean the
UFFDIO_COPYbecomes illegal, am I right?I don't think that "munmap + mmap +
userfault_register"
during an outstandingUFFDIO_COPYto the same range is, hmm, the smartest thing to do, and I think aborting the outstandingUFFDIO_COPYin such case is better than allowing it to continue.
It doesn't need to be unmap+map+register. As mentioned below, I believe writting 4 to clear_refs will already change VMA flags. There're also many other ways to change, IIUC, like mprotect() on top of uffd MISSING registered ranges.
Meanwhile, I also don't think it's about whether it's a smart move.. I agree most apps shouldn't do complex operations on VMAs when having userfaultfd involved. Said that, IMHO the whole point of kernel uAPI is to make sure it works with every (even malicious) userapps, and it shouldn't crash kernel. So even if the reproducer will require complex VMA setups, we should still close the gap.
Quoted 10 lines
Quoted 6 lines
For example, I wonder if it's possible someone runs soft-dirty concurrently with userfaultfd, we shouldn't fail the userapp if there's a concurrent thread collecting dirty information, which IIUC can cause VMA flag changes, and should be benign, and I think there can be other things causing the interruption too.
Right, we shouldn't fail if some of the VMA flags changed, but we are talking about of complete change of the mapping, with potentially completely different backing store.
I don't know how to define "complete change of the mapping". Here, IMHO what we should do is to be strict on vma checks, either using the vma snapshot or anything that can achieve the same goal, then returning -EAGAIN is the safest because it won't crash a good citizen userapp. The re-evaluation will only be done later.
Thanks,
Signature · 1 line
Peter Xu
Peter Xu
reviewerQuoted 1 line · Mike Rapoport
Hi Peter,
Hi, Mike,
Quoted 28 lines · Peter Xu
Quoted 26 lines
Hi, Mike,
Let me also leave my comments inline just for you to consider.
Quoted 20 lines · Mike Rapoport
Hi David,
It feels that you use an LLM for correspondence. Please tune it down to produce more laconic and to the point responses.
Quoted 10 lines · David CARLIER
Quoted 2 lines · Mike Rapoport
What does "folio allocated from the original VMA's backing store" exactly mean? Why is this a problem?
Fair point, the commit message was vague here. What I meant is:
mfill_atomic_pte_copy()captures ops =vma_uffd_ops(state->vma)and passes it to__mfill_atomic_pte(). There,ops->alloc_folio()allocates a folio for the original VMA's inode (e.g. a shmem folio for that specific shmem inode).I wouldn't say ->alloc_folio() allocates a folio _for_ the inode, it allocates it with inode's memory policy. Worst can happen without any changes is that the allocated folio will end up in a wrong node.
Quoted 8 lines
Quoted 5 lines
For shmem it's only about mempolicy indeed, but since we're trying to export it as an API in the series, IMHO it would be nice to be generic. So we shouldn't assume it's only about mempolicy, we should rely on detecting any context change and bail out with
-EAGAIN, relying all rest checks to the nextUFFDIO_COPYioctl done on top of the new mapping topology.My point was that this is preexisting bug and that we don't need to rush with the complete fix that will extensively compare VMA compatibility...
Yes, I fully agree it was pre-existing. My guess is we only didn't reach a consensus yet on how to completely fix it, and whether we need an intermediate fix for "a VMA suddenly changed to hugetlb" only.
Quoted 12 lines
Quoted 12 lines
Quoted 2 lines
This is still a footgun, but I don't see it as a big deal.
IIUC this is a real bug reported. Actually, if my understanding is correct, we should be able to easily write a reproducer by registering the src addr of
UFFDIO_COPYto userfaultfd too, then theioctl(UFFDIO_COPY)thread will get blocked faulting in thesrc_addr. During that, we can change the VMA layout in another thread to test whatever setup we want.Quoted 2 lines
Let's revisit it after -rc1 and please make sure to cc "MEMORY MAPPING" folks for insights about how to better track VMA changes or their absence.
Quoted 8 lines
Quoted 5 lines
No strong feeling here if we want to slightly postpone this fix. It looks like not easy to happen as it looks to be a bug present for a while, indeed.
It's just that if my understanding is correct, with above reproducer we can crash the kernel easily without a proper fix.
... but we do need a more urgent fix for the case when a VMA suddenly becomes hugetlb, because that could not happen before the refactoring.
Personally this is least of a concern to me. Hugetlbfs is so specially managed in userapps, so it is even less likely to trigger the same bug with VM_SOFTDIRTY changes or other ways.
But I understand your point. You want to cover what your series changed on this. If you think this is the right way to go, I'll follow your decision.
Quoted 14 lines
For that, it would be enough to check that ->ops are the same before and after
copy_from_user().@David, do you mind to send a patch for this without waiting for rc1?
Quoted 8 lines
Quoted 7 lines
Quoted 2 lines
The
vm_flagscomparison was a secondary guard against permission/type changes during the window.Permissions should be fine, they are checked in
userfaultfd_register. Some other flags that don't matter to uffd operation may change during the window, though and then a comparison ofvm_flagswill give a false positive.
Quoted 5 lines
Quoted 2 lines
IMHO false positive is fine in this case when
-EAGAINwill be used (which I still think we should), if it only causes a retry.I still disagree, but let's postpone this discussion for later, when David resends the patch that compares VMA properties.
Maybe it's because we are having different checks in mind, where you wanted to check only the "invalid cases" but I am trying to make it fallback for all detectable changes.
IMHO it's hard to define "invalid case" in this case, and it's also unnecessary when a EAGAIN will be processed with another UFFDIO_COPY attempt and we'll simply redo all the checks. Hence relying on VMA change would be the simplest, safest to me.
Again, I'm open to any suggestion on replacing the vma snapshot logic as long as all possible issues got reported will be properly fixed, especially in the latest mainline. I don't worry much on backporting yet; if this bug existed for 10 years and nobody yet reported, to me we can always evaluate the effort to backport or skip. However, a proper fix in mainline is IMHO more important.
Thanks,
Signature · 1 line
Peter Xu
Mike Rapoport
maintainerQuoted 9 lines · Peter Xu
Quoted 4 lines · Mike Rapoport
On Thu, Apr 02, 2026 at 09:42:01AM -0400, Peter Xu wrote:
My point was that this is preexisting bug and that we don't need to rush with the complete fix that will extensively compare VMA compatibility...
Yes, I fully agree it was pre-existing. My guess is we only didn't reach a consensus yet on how to completely fix it, and whether we need an intermediate fix for "a VMA suddenly changed to hugetlb" only.
Such intermediate fix will keep bug backwards compatibility :)
Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
Quoted 1 line
This is still a footgun, but I don't see it as a big deal.
IIUC this is a real bug reported. Actually, if my understanding is correct, we should be able to easily write a reproducer by registering the src addr of
UFFDIO_COPYto userfaultfd too, then theioctl(UFFDIO_COPY)thread will get blocked faulting in thesrc_addr. During that, we can change the VMA layout in another thread to test whatever setup we want.Quoted 2 lines
Let's revisit it after -rc1 and please make sure to cc "MEMORY MAPPING" folks for insights about how to better track VMA changes or their absence.
No strong feeling here if we want to slightly postpone this fix. It looks like not easy to happen as it looks to be a bug present for a while, indeed.
It's just that if my understanding is correct, with above reproducer we can crash the kernel easily without a proper fix.
Quoted 6 lines
Quoted 2 lines
... but we do need a more urgent fix for the case when a VMA suddenly becomes hugetlb, because that could not happen before the refactoring.
Personally this is least of a concern to me. Hugetlbfs is so specially managed in userapps, so it is even less likely to trigger the same bug with
VM_SOFTDIRTYchanges or other ways.
I'm not sure I follow you here. How changes of VM_SOFTDIRTY can crash the kernel in UFFDIO_COPY?
Quoted 6 lines
Again, I'm open to any suggestion on replacing the vma snapshot logic as long as all possible issues got reported will be properly fixed, especially in the latest mainline. I don't worry much on backporting yet; if this bug existed for 10 years and nobody yet reported, to me we can always evaluate the effort to backport or skip. However, a proper fix in mainline is IMHO more important.
Totally agree, a fix in mainline is much more important than backporting.
Quoted 4 lines
Thanks,
Signature · 1 line
Peter Xu
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerQuoted 6 lines · Mike Rapoport
Quoted 3 lines
Personally this is least of a concern to me. Hugetlbfs is so specially managed in userapps, so it is even less likely to trigger the same bug with
VM_SOFTDIRTYchanges or other ways.I'm not sure I follow you here. How changes of
VM_SOFTDIRTYcan crash the kernel inUFFDIO_COPY?
It was confusing indeed that I used as example, sorry. SOFTDIRTY only case isn't a bug, even if it'll also be captured by "vma changes" when we detect that.
I just wanted to say I concerned much less on arbitrary hugetlbfs vmas appearing than most of the rest, where it can crash the kernel even after the change (e.g. mapped one shmem inode, remapped to another different shmem inode, or SHARED->PRIVATE, as others pointed out elsewhere).
Quoted 9 lines
Quoted 7 lines
Again, I'm open to any suggestion on replacing the vma snapshot logic as long as all possible issues got reported will be properly fixed, especially in the latest mainline. I don't worry much on backporting yet; if this bug existed for 10 years and nobody yet reported, to me we can always evaluate the effort to backport or skip. However, a proper fix in mainline is IMHO more important.
Totally agree, a fix in mainline is much more important than backporting.
Thanks,
Signature · 1 line
Peter Xu