From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 9C06B8F4A for ; Sun, 12 Apr 2026 15:36:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776008207; cv=none; b=IBhouR4+DpmByJY7EYsu4L564p69ASN6T7YwQ2KQ5lDRstGyVSkpq2BjhSfqFxNzMYdTs7XAjQlGLwRbUz9OOcexjzBXQOeNFYTBsAfp27J8scNdgx1dHWz/7FQRj4iGX9zrTyqD1QgF+hp8DYL3A43K6z76Ma+ePlv15ZVJYZQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776008207; c=relaxed/simple; bh=JqsLH+Zb+NtHMqOAf4UeimGqJTVIjcjxFFcutVJhpAI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=NFouwUGOdS426sA5rBU052PEoK0sG0hq1qmg6LOaerc6WqsxZcQJOHj3072EgLs8at+heM7LJFeP5FDHeaQkhl9B0UHsGvMh0+JtwfyOqNN5Wr/52fGfCiBVU6NP3lQ3WGmmBD4sRfmsUZj4Lj9W0FBSw2vrZwp00dMruSSgRcg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TgLepv1m; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="TgLepv1m" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DED54C19424; Sun, 12 Apr 2026 15:36:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776008207; bh=JqsLH+Zb+NtHMqOAf4UeimGqJTVIjcjxFFcutVJhpAI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=TgLepv1mSZVG7YhtjLNhGH/h79jbc/xCvLnMYmzhDtREE/996RhdUkGfFe5CEGSdH iB+b22DN70NUh16Y1iQM2pyCHmo4y8yGb7F7ht3gJyArGmAQgoW5yewdxG0TI6ncn7 MzFEc6njTNvDsB8OAThrIgN8KQId9pBalE7Ylcfi4/ueb74fSH2ZSrjlyKilK9+p14 6F4dcWdOR7Iy7pht5nZKMoePy2rE0dN9kEaL2b3k/nZtXp8z9T2PcIjR8YGjwdsWvo FWpsglhxfJqfSjUq+cvPaAg33DYqPGNTb5C9ruPg4v4H7ll33zArYdCUv6Zb1nrJ1B MNw6uc/Y+YU4A== Date: Sun, 12 Apr 2026 18:36:40 +0300 From: Mike Rapoport To: Usama Arif Cc: David Carlier , Andrew Morton , Peter Xu , "Liam R . Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v5] mm/userfaultfd: detect VMA type change after copy retry in mfill_copy_folio_retry() Message-ID: References: <20260409120653.290386-1-devnexen@gmail.com> <20260410114809.3592720-1-usama.arif@linux.dev> 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: <20260410114809.3592720-1-usama.arif@linux.dev> Hi Usama, On Fri, Apr 10, 2026 at 04:48:08AM -0700, Usama Arif wrote: > On Thu, 9 Apr 2026 13:06:53 +0100 David Carlier wrote: > > > mfill_copy_folio_retry() drops mmap_lock for the copy_from_user() call. > > During this window, the VMA can be replaced with a different type (e.g. > > hugetlb), making the caller's ops pointer stale. Subsequent use of the > > stale ops can lead to incorrect folio handling or a kernel crash. > > > > Pass the caller's ops into mfill_copy_folio_retry() and compare against > > the current vma_uffd_ops() after re-acquiring the lock. Return -EAGAIN > > if they differ so the operation can be retried. > > > > Fixes: 59da5c32ffa3 ("userfaultfd: mfill_atomic(): remove retry logic") > > Signed-off-by: David Carlier > > --- > > mm/userfaultfd.c | 14 ++++++++++++-- > > 1 file changed, 12 insertions(+), 2 deletions(-) > > > > diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c > > index 481ec7eb4442..214923a411c1 100644 > > --- a/mm/userfaultfd.c > > +++ b/mm/userfaultfd.c > > @@ -443,7 +443,9 @@ 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, struct folio *folio) > > +static int mfill_copy_folio_retry(struct mfill_state *state, > > + const struct vm_uffd_ops *ops, > > + struct folio *folio) > > { > > unsigned long src_addr = state->src_addr; > > void *kaddr; > > @@ -465,6 +467,14 @@ static int mfill_copy_folio_retry(struct mfill_state *state, struct folio *folio > > 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) != ops) > > + return -EAGAIN; > > + > > hmm I am not sure if this is correct for shmem MAP_PRIVATE. > > mfill_atomic_pte_copy() overrides ops to &anon_uffd_ops for MAP_PRIVATE > mappings: > > if (!(state->vma->vm_flags & VM_SHARED)) > ops = &anon_uffd_ops; > > This overridden ops pointer propagates through __mfill_atomic_pte() into > mfill_copy_folio_retry(). But the new check here calls vma_uffd_ops() > which returns the original file-backed ops (e.g. &shmem_uffd_ops). > For shmem MAP_PRIVATE VMAs, the comparison always fails even when > the VMA type has not changed. Good catch. @Andrew, can you please drop the patch for now? > Maybe save the original (non-overridden) ops before the MAP_PRIVATE override > and compare against that? -- Sincerely yours, Mike.