From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-178.mta1.migadu.com (out-178.mta1.migadu.com [95.215.58.178]) (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 3A4373A1A5F for ; Fri, 10 Apr 2026 11:48:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775821700; cv=none; b=GLky3HPKbkUQAgbxxZIWQFRuzHKtLYdXvjus4kpuL6kCaEwbxn9yjbSvROSkgCwDVKoME9de42duGkbdy3XCuNU26BYG0QqY4/Emo+PNjTynkuRgg1V8Pbyyh9dlRq9OkN/Wz2TXsxIJPsq6H7zcPNUQPddH7m8UkDLhuGzOrig= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775821700; c=relaxed/simple; bh=OcUh01N/uz7VdPcMYqvM0+Sf6gTz5oYQDdSwSZI0aOw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fDya8kwY9yGGlrnhsjcFQhIbgu+5VoU+Ew/hFz/00SBB1KL2M8fszLy1KRSjjeNY9AV0rXT6uCR3p/SbFar+DOLWUbW51kmwsNMPU826/9A/0n/q2wZ1Ap3fCP/aFtPsxQ6KWT1n10+xc+d+6fm6zBQ2/z2otlj8XCAOLe+C/io= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=RxqZGCJ1; arc=none smtp.client-ip=95.215.58.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="RxqZGCJ1" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1775821695; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AqLOGr7u2EsVQKGgZ6etRKNa4B02J4pU4jvPoN42Sro=; b=RxqZGCJ1wN9svorxpul1HncGWj+1nZ9oxTLx6zY1X3eyB6kcomoWqOGw50cyEq8moEAfs5 1EQjpmfNsdV+RiNijymP+gL8dirx47NJry9Re1oXYZ+JxgKZ+ktRItfcy5rMw6uwCVbDKG QkhSldRhKHV+7XcSvJ4UcWu/WliNd5I= From: Usama Arif To: David Carlier Cc: Usama Arif , Andrew Morton , Mike Rapoport , 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() Date: Fri, 10 Apr 2026 04:48:08 -0700 Message-ID: <20260410114809.3592720-1-usama.arif@linux.dev> In-Reply-To: <20260409120653.290386-1-devnexen@gmail.com> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT 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. Maybe save the original (non-overridden) ops before the MAP_PRIVATE override and compare against that?