From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1D8253F4DC7 for ; Thu, 28 May 2026 13:31:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779975068; cv=none; b=XmucX4DMxjQMLMoUgP1q3JnXy1Kl5WDt/WiHLvu993WzlDC4t7kfQSfJTicc3VofWWt1JXWNyXyq5CJ9oyq6s2GhPkJ6hEGq47qW1JrJGXRSQRdV0U0Ba7pA/UEp6m8NPPoF9iZJD5dutSJi1XfZlvQYOyDkiaM6cGrLSjIIi/Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779975068; c=relaxed/simple; bh=4WLFcOXUIMi8KIj97XL49eyn1HevDC+6vFyvWFCjwFA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=pZxThFCUXVIt3283y+RSSOLQVRxTyA2AliKSg7HTaepnyrm5zfdvOwTLxbOTtf1LpO34nT3bcJpndEJ9HUAjNFtjn9RLtXqge5PiosHHrKLPZSmrZZko7DEmLlR+0eUepD2JQS0x3D1PnbzbAHyV7ByKMO7Vd9915S0LEU6+x7Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=aXU606BI; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="aXU606BI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2B12A1F000E9; Thu, 28 May 2026 13:31:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1779975066; bh=JhXiBf+O7T5aPY633KPfCzBwz2LzPoL8tTWNGFtRuzc=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=aXU606BIRfidIdFQV8dcq6jyRlyghIcQQSCfe8VTGM5kU/a6xdpkMPVNI90FoXm4n xjUb7J3wB9pAs+j59mubjzRigHk+eMXjf4VOyCiJJ/MSoPoRSdmlfUfKsz4WbP+yOP uz5UixRLIngrGl2ZwgXrOkcqk8Qt2L0D1q6EahG7bxXDUqDVmGaYJNun6Ekz4KwQfY R/tt43ahvcbclUXYVV6w/v7IZ5jtv9E68qPAWZPHcCQntUJ39WToWpExpyBp7ZGSQ9 JCC4EYUp5qEX4vcIrvxJsDDxp3pRPwZpN4/7xb/styrhv5Rx4p5dJDeBSb2eL0s+5A QcP3XVYvvPJ7w== Date: Thu, 28 May 2026 14:31:00 +0100 From: Lorenzo Stoakes To: Mike Rapoport Cc: Andrew Morton , David Carlier , David Hildenbrand , Heechan Kang , "Liam R. Howlett" , Michael Bommarito , Peter Xu , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 1/3] userfaultfd: verify VMA state across UFFDIO_COPY retry Message-ID: References: <20260527184751.4147364-1-rppt@kernel.org> <20260527184751.4147364-2-rppt@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260527184751.4147364-2-rppt@kernel.org> On Wed, May 27, 2026 at 09:47:49PM +0300, Mike Rapoport wrote: > From: "Mike Rapoport (Microsoft)" > > 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") Did we want a Cc: Stable? > Suggested-by: Peter Xu > Co-developed-by: David Carlier > Signed-off-by: David Carlier > Co-developed-by: Michael Bommarito > Signed-off-by: Michael Bommarito > Signed-off-by: Mike Rapoport (Microsoft) 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: Reviewed-by: Lorenzo Stoakes > --- > mm/userfaultfd.c | 85 +++++++++++++++++++++++++++++++++++++++++------- > 1 file changed, 73 insertions(+), 12 deletions(-) > > diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c > index 180bad42fc79..e5d2fb3ce2c1 100644 > --- a/mm/userfaultfd.c > +++ b/mm/userfaultfd.c > @@ -14,6 +14,8 @@ > #include > #include > #include > +#include > +#include > #include > #include > #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; > +}; Much better thanks! > + > +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 :) > + > +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; Doesn't state->file != vma->vm_file render the inode check redundant? > + > + 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 >