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 73BEE3E0C56 for ; Wed, 20 May 2026 12:53:40 +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=1779281621; cv=none; b=H3mcTT3HpGsW/7K4fWn2gOcwObFcyogEBc5pCH/45XZOZZHeZLVLC7iCbs+xiv580dr5ByXJETpqDj4awUITWsQI3bdKbzKEfWXbDJSNul29klgNXqWioH93yXunVMwHXjsM9Yw/AVfz8FdVZ38WEKEjlCi8mdjnC1VC316rn0E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779281621; c=relaxed/simple; bh=CwpfNdCvJ1+iEqASpkAgQVecVQ8h1sIzcdrdvy/3N2g=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=i/unHBLRrrCpxYh9G7/tOxcW1E8WOsAMoEjGub2S4hR3m45d0yMnjhbjS38Kf6LaQQy3ZxvjSQGPphPObyXBZ+ZITOLsWmnf0WYYw5kW4W1/vKtScCHb+wEFxgv57ZWAV78DGiksYlbSP2Olp+HqEf41z8dOf17thqqfNmNgpIY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LH71BuEI; 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="LH71BuEI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE97F1F000E9; Wed, 20 May 2026 12:53:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1779281620; bh=DQ5FPHELtgn1sRAnAV+E5ESjcFvLQBKexCF9mafR+bo=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=LH71BuEIN6ruGldi7EGQ0oh/DhaPnQ1NvZSV/E0cFyuIrYHlxxi3LJvNgi1MDtV/z jS1ZyKeNgPgrcMrwXq9N6nam3E80jpS9DHv3AJbNvD6BY5FEOkKWCTnPma8g8ASPtO 6y9qyGroMWALaqjvh7zW5TQadRAJ9gJIrQTI4iFEzisCqxUDJ5a7oslmOrGmmy85w7 bz+Lppwq2HXYK90bh3HWiXMWyHGkYZM90OBi0be43dJpW3FKbwxw+qTX+5AHPSh+jQ /wD2jIaUV43OQ4z6YAiVWHTRvE9bblLXvLp/SvCfLa44rblGMQ3sm25zkfwP8FZytN wZtOo2EmL0/lw== Date: Wed, 20 May 2026 15:53:33 +0300 From: Mike Rapoport To: "David Hildenbrand (Arm)" Cc: Andrew Morton , David Carlier , Heechan Kang , "Liam R. Howlett" , Lorenzo Stoakes , Michael Bommarito , Peter Xu , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH RESEND] userfaultfd: snapshot VMA state across UFFDIO_COPY retry Message-ID: References: <20260519052516.3315196-1-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: On Wed, May 20, 2026 at 01:09:04PM +0200, David Hildenbrand (Arm) wrote: > On 5/19/26 07:25, 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. > > > > Introduce helpers for more comprehensive comparison of VMA state: > > - vma_snapshot_get() to save the relevant VMA state into a struct > > vma_snapshot (original uffd_ops, actual uffd_ops, relevant VMA flags, > > vm_file and pgoff) before dropping the lock > > - vma_snapshot_changed() to compare the saved state with the state of the > > VMA acquired after retaking the locks > > - vma_snapshot_put() to release vm_file pinning. > > > > Use DEFINE_FREE() cleanup to wrap vma_snapshot_put() to avoid complicating > > error handling paths in mfill_copy_folio_retry(). > > > > Add vma_uffd_copy_ops() to avoid code duplication when original ops of > > shmem VMA with MAP_PRIVATE are replaced with anon_uffd_ops. > > > > Fixes: 292411fda25b ("mm/userfaultfd: detect VMA type change after copy retry in mfill_copy_folio_retry()") > > Fixes: 6ab703034f14 ("userfaultfd: mfill_atomic(): remove retry logic") > > Tested-by: Heechan Kang > > 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) > > --- > > mm/userfaultfd.c | 99 ++++++++++++++++++++++++++++++++++++++---------- > > 1 file changed, 79 insertions(+), 20 deletions(-) > > > > diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c > > index 180bad42fc79..b70b84776a79 100644 > > --- a/mm/userfaultfd.c > > +++ b/mm/userfaultfd.c > > @@ -14,6 +14,8 @@ > > #include > > #include > > #include > > +#include > > +#include > > #include > > #include > > #include "internal.h" > > @@ -69,6 +71,24 @@ static const struct vm_uffd_ops *vma_uffd_ops(struct vm_area_struct *vma) > > return vma->vm_ops ? vma->vm_ops->uffd_ops : NULL; > > } > > > > +static const struct vm_uffd_ops *vma_uffd_copy_ops(struct vm_area_struct *vma) > > +{ > > + const struct vm_uffd_ops *ops = vma_uffd_ops(vma); > > + > > + if (!ops) > > + return NULL; > > + > > + /* > > + * UFFDIO_COPY fills MAP_PRIVATE file-backed mappings as anonymous > > + * memory. This is an effective ops override, so retry validation must > > + * compare the override result, not just vma->vm_ops->uffd_ops. > > + */ > > + if (!(vma->vm_flags & VM_SHARED)) > > + return &anon_uffd_ops; > > + > > + return ops; > > +} > > + > > static __always_inline > > bool validate_dst_vma(struct vm_area_struct *dst_vma, unsigned long dst_end) > > { > > @@ -443,14 +463,70 @@ static int mfill_copy_folio_locked(struct folio *folio, unsigned long src_addr) > > return ret; > > } > > > > +#define VMA_SNAPSHOT_FLAGS append_vma_flags(__VMA_UFFD_FLAGS, VMA_SHARED_BIT) > > + > > +struct vma_snapshot { > > + const struct vm_uffd_ops *copy_ops; > > + const struct vm_uffd_ops *ops; > > + struct file *file; > > + vma_flags_t flags; > > + pgoff_t pgoff; > > +}; > > As this is all uffd specific, I wonder whether that should be "struct > uffd_vma_snapshot"/"vma_uffd_snapshot" etc. It's local to this file, I wouldn't worry about namespacing until there's an actual need if it will ever arise. > From a high level, this LGTM. > > I wish we could identify relevant VMA changes more easily. Like, using a per-MM > sequence counter that we simply increment on any VMA changes. Do you mean per-VMA? Per-MM counter would capture unrelated changes, e.g. an masvise() for unrelated range. We kinda have an infrastructure to detect VMA changes that affect uffd operation for non-cooperative, we can also hook on that even if UFFD_EVENT_* are not requested, but that's way more involved that this VMA snapshot. > -- > Cheers, > > David -- Sincerely yours, Mike.