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 84C3230BF6D for ; Thu, 9 Apr 2026 15:20:24 +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=1775748024; cv=none; b=ec0GW2n4jTQ0w7bG+/EVrDyp5oHzNczpclsyzmu8It1fiHR7bI8qORme10GMl+Wd8ztCvI5HV4ApCPlHyxEmZ7iklCYJzwNoUCuQuh2WpqXFSOMbrYnIYXhusBpiMq83lLAJu4470WzMiasxZssDDE5e/IewlOsiY7T7Y4Nlx0c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775748024; c=relaxed/simple; bh=CYBSGM9SzwH7P635l4C7PjyfDosC033edsSUrRg6c3o=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=VLMXgsRiTCFY35qn2uAOE60jmUQhDPqHPqVeV0i/UwdqhQJpA6gqBF9/1FtHAAcxcaTNHpeBbBGsYWluGrk4uipyap+lSO0qu8L3W5zqBpQjwfS/kaR7+XcOWe/IAGAT0k/FRufIqXne7Pl1qBLnVJ5VRLC8VRVSL5U3QRzK1pc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pLgfQ2wd; 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="pLgfQ2wd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0A679C2BC9E; Thu, 9 Apr 2026 15:20:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775748024; bh=CYBSGM9SzwH7P635l4C7PjyfDosC033edsSUrRg6c3o=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=pLgfQ2wdyqZd657n0SMW+WOSBGOr1xPSqAtP5HFmvpDMOoKKIIbck1g+bgKqcjIAE DPnUWWXg72eXz5SdFII7wophQDgGHOqV2OKX6V9qmr3jxzqD7M6Nuv47v2Mf8AGPEF SVlcLqGdLjK9xJQvsViIFUoUtYPbD/94vx6Ny57yPDSiIFIhNmepn31jw5auTTqB9q hBe2hDTfPyO0TM5T1as6KN5Bvu6AesM64rZEFN65ovEn9ekq5eg9AIYUufSjL1w1ma vNsD42lGcJjC92Z0Kfbw4mEGK3EPQFpVILJdzwdexJ67TpfoFZ9FYvyvGDTkoQOi/P bm6us5A5h1BXw== Date: Thu, 9 Apr 2026 18:20:17 +0300 From: Mike Rapoport To: David Carlier Cc: 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> 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: <20260409120653.290386-1-devnexen@gmail.com> On Thu, Apr 09, 2026 at 01:06:53PM +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 Acked-by: Mike Rapoport (Microsoft) > --- > 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; > + > err = mfill_establish_pmd(state); > if (err) > return err; > @@ -495,7 +505,7 @@ static int __mfill_atomic_pte(struct mfill_state *state, > * will take care of unlocking if needed. > */ > if (unlikely(ret)) { > - ret = mfill_copy_folio_retry(state, folio); > + ret = mfill_copy_folio_retry(state, ops, folio); > if (ret) > goto err_folio_put; > } > -- > 2.53.0 > -- Sincerely yours, Mike.