Re: [PATCH v2] mm/userfaultfd: detect VMA replacement after copy retry in mfill_copy_folio_retry()
From: Mike Rapoport
Date: Tue Mar 31 2026 - 08:00:57 EST
(added VMA folks)
Hi,
On Mon, Mar 30, 2026 at 09:29:09PM +0100, David Carlier wrote:
> 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.
Is it possible at all that after all that dance vma pointer will remain the
same?
> 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 inode and flags before
> dropping locks, and compare after re-acquiring them. If anything
> changed, bail out with -EAGAIN.
>
> Suggested-by: Peter Xu <peterx@xxxxxxxxxx>
> Signed-off-by: David Carlier <devnexen@xxxxxxxxx>
Sashiko has comments and they seem quite relevant to me:
https://sashiko.dev/#/patchset/20260330214948.148349-1-devnexen%40gmail.com
> ---
> mm/userfaultfd.c | 64 ++++++++++++++++++++++++++++++++++++++++++------
> 1 file changed, 57 insertions(+), 7 deletions(-)
...
> + if (vma_snapshot_changed(state->vma, &s)) {
> + err = -EAGAIN;
Whatever we do verify the VMA this should not be EAGAIN. EINVAL or ENOENT
like mfill_get_vma() returns seem more appropriate.
> + goto out;
> + }
--
Sincerely yours,
Mike.