Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796388760; b=WkFCR+yvN7xAInMKHdFezgefnCNyqkLIBjn+jB9XBJJ6NSYQpGonKtAsNbjK9jPQuowQ bGWm6fbBK6v3CRKFCuryyBBLgZAx5f9I2eyX15lYJ/97dY+lBKNYYNsLWc5K8vltE+GZk nAt2S3k6oh/HiJYC4YVenL8y716PSmNcBCQL2qnMGEtSDsEjBdSdI3MmomuoXjCM2UuEe 7ERTmqkg9O37/HwpiRmrHN7EnKgcHaVHo8hvGyGSPAR9YnfFDa+I5FHsB72s5jAc/xC8J o8SE9Hg0lC91jgXO76lGoFzJwDsVUGjuBH5/L0t7zczsyk1VF5NwlwCw8WuvyggY2vg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796388760; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=SzLeZsMdFKsKNBEnrpFMJsH4znUk4HMiE9+V2XZzhLA=; b=4fMZt80z7OZy8jRrGY5cwiWNzDZGOKflnq1zqffyf3gmwftZZarcbrSWRFEYkuTriBPa W+ZXZ5D1nBZhvSRfEQ+zKMBsk5oHgbYsGoEeUym0+KyxstcmRZo4epYpG3AoGdnjsEniz BZIEzJ5MOLTXZQF+TPj+PYz0aqaIuncUnOCC0NY6/Uf04cwk2GUpIHmyIZ2Olk3ll5F/3 FVyv2rsnVEdJb4CZWffnOriOA9GKv3dJtyBQjznb61gH7XkT7LADPbnQGp+VXQhKa5q3A qJgHci9M6/BH9Kqcj86DfGE60T6Xz4ulKjKDe0iBjUI9t+M1f5zdA4Gsn6mCm1LRJ+w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="V/fTZdYt"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 41E8B606A32D for ; Fri, 4 Dec 2026 12:52:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=V/fTZdYt Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0C1401F00A10; Fri, 4 Dec 2026 12:52:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796388760; bh=SzLeZsMdFKsKNBEnrpFMJsH4znUk4HMiE9+V2XZzhLA=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=V/fTZdYt//yD9rq+kVjeQcBkx2Vmf+/h6wV0zbkzVIT+ujRUi6UYE4wPiaGdFxwzN F0qGxTYpUaT8croCOslbn9hoBA3Ol1hoC8bXBjepa/NkHmDzrQwKoVgCLhZ9hhC7B/ 82djfmy2JbXzUgZsxZTXyymMNUIMbicZUZuYlDF8+Y9vmaI/koOMJHYIWf7+ryHHT5 bDdM+1EHvIzOAGG+p49YphbM/A8e6nTcNblRji00ACJqBnpzFA+GcXZYpFTUnWz2pr HZH5AP2rQQQRTdq8HHTVx8lPzOJrnO+xQjOWzM3n9/gyfSM4DW6p1h+lTCaNZOO4Po b3AT3/fUMsovA== Date: Fri, 4 Dec 2026 12:52:32 +0000 From: "Lorenzo Stoakes (ARM)" To: Zi Yan Cc: David Hildenbrand , Baolin Wang , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Kiryl Shutsemau , Rik van Riel , Vlastimil Babka , Harry Yoo , Jann Horn , Kyle Zeng , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrea Arcangeli , Suren Baghdasaryan , Peter Xu Subject: Re: [PATCH v2 9/9] userfaultfd: avoid transient none PMDs during UFFDIO_MOVE Message-ID: References: <20261203-anon_vma_uaf_fix-v2-0-3d4c746e8839@nvidia.com> <20261203-anon_vma_uaf_fix-v2-9-3d4c746e8839@nvidia.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261203-anon_vma_uaf_fix-v2-9-3d4c746e8839@nvidia.com> On Thu, Dec 03, 2026 at 11:17:57PM -0500, Zi Yan wrote: > UFFDIO_MOVE under the page table lock clears a PMD, checks if the folio is > DMA pinned, and restores it if that is the case. A concurrent zap can skip > a none PMD without taking the page table lock. Since UFFDIO_MOVE holds the > VMA or mmap read lock, those taking the read lock can race with it, e.g., > MADV_DONTNEED. In the MADV_DONTNEED case, the affected memory can be > neither discarded nor zeroed. > > Fix it by invalidating the PMD with pmdp_invalidate() instead. Also add > smp_mb() between pmdp_invalidate() and reading folio pincount to pair with > GUP-fast. > > Fixes: adef440691bab ("userfaultfd: UFFDIO_MOVE uABI") No Cc: stable, on purpose? > Signed-off-by: Zi Yan With nits addressed, LGTM, so: Reviewed-by: Lorenzo Stoakes (ARM) > To: David Hildenbrand > To: Zi Yan > To: Andrew Morton > To: Andrea Arcangeli > To: Suren Baghdasaryan > Cc: Baolin Wang > Cc: "Liam R. Howlett" > Cc: Nico Pache > Cc: Ryan Roberts > Cc: Dev Jain > Cc: Barry Song > Cc: Lance Yang > Cc: Usama Arif > Cc: Kiryl Shutsemau > Cc: Peter Xu > Cc: linux-mm@kvack.org > Cc: linux-kernel@vger.kernel.org > --- > mm/huge_memory.c | 13 ++++++++++++- > 1 file changed, 12 insertions(+), 1 deletion(-) > > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > index 72322bb86ff4f..6a9635c7e0b48 100644 > --- a/mm/huge_memory.c > +++ b/mm/huge_memory.c > @@ -2993,7 +2993,17 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm > goto unlock_ptls; > } > > - src_pmdval = pmdp_huge_clear_flush(src_vma, src_addr, src_pmd); > + /* > + * Invalidate the PMD so that a concurrent zap does not skip it > + * in case a restore is needed afterwards. > + */ OK had to read it a few times to get it - it being cleared would result in zap skipping it (+ thus not blocking on PMD PTL) + potentially a restore would then leave an unzapped entry. Maybe slightly rephrase like: /* * A concurrent zap skips none entries without taking the PTL, so * invalidate rather than clear. The zap then blocks on the PTL until * the move has either completed or been aborted and the entry restored. */ > + src_pmdval = pmdp_invalidate(src_vma, src_addr, src_pmd); > + /* > + * Syncing against concurrent GUP-fast: > + * - invalidate PMD; barrier; read pincount > + * - inc pincount; barrier; read PMD Worth saying specifically 'pairs with' and the relevant function by convention. > + */ > + smp_mb(); Real nitty but maybe newline here to highlight that you have a fence here :) > /* Folio got pinned from under us. Put it back and fail the move. */ > if (folio_maybe_dma_pinned(src_folio)) { > set_pmd_at(mm, src_addr, src_pmd, src_pmdval); > @@ -3004,6 +3014,7 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm > folio_move_anon_rmap(src_folio, dst_vma); > src_folio->index = linear_anon_page_index(dst_vma, dst_addr); > > + pmdp_huge_get_and_clear(mm, src_addr, src_pmd); > _dst_pmd = folio_mk_pmd(src_folio, dst_vma->vm_page_prot); > /* Follow mremap() behavior and treat the entry dirty after the move */ > _dst_pmd = pmd_mkwrite(pmd_mkdirty(_dst_pmd), dst_vma); > > -- > 2.53.0 > -- Cheers, Lorenzo