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=1794065366; b=qjktS+3r2+xC2Mgw6ArTkqmLFeOakPiPLxeEhmCkZ/TaUZ/rPv0byf445s1dXWSfV03b sRsPznxGE0O930Xct4GFUAR6oFVTtFmcGUM6qwLLl1nfaKsnSrWmlxMwyvEmAGtBEvnt6 EOBizPze0DrQSOc+GO7dT7vH2Ft5x49m9zCwKm3LQe5hW6kEItgY1udJrdd9p5N/FP3En CfkKTExuU8g54M81P9GrUIs1T1Nsvg8SHbxfDsFRc2h8NZEDXtB1jVuGDnCXIN6TrqAXY 5w+AjHAzrB6RIu5lNEBXCiQ1yXNvUPMP5gSr/KAZfa9RQthKuJwkpns2F/Fcb5O6VYQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794065366; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=4kKOvlOBvPGB7C20yNXGUWWzG5b9TJUQ9fANqN6e82o=; b=kQ7MvxKY7QmnIGcHVxRdWiDbt8p2k1P7YBXa+/duVtDMlAjOBTUyiLCbyJvkNjuBxU9W 0e7IRTJrPHFGqs+h9rBsevMsqtOBwf0YjbYOP1CG7luypWZoqmiIboWWjQn/LEyVZ3y3T TRh3zFLhSWKHf8DXcpAu2HJjh4dvUm+nWCdKhQY6p6lilj6ArtTzjRqE9jPV/jbQw9F2s eIKn6x66aJuTVcGNy9Z+zhsWPFoDSfSYz++h9YxYgFERKgMTM9yL3+Rj9VyKdpT6l/+Pr mrRPvZMGJL86Y/RlPhhsk2gLGPvZtK2B5BDSouS32h3S5mBqcTsfm5lnMVQAdY11kDg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=qBqZrSrt; 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 7F8A1606A326; Sat, 7 Nov 2026 15:29:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=qBqZrSrt Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE0271F0089F; Sat, 7 Nov 2026 15:29:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1794065366; bh=4kKOvlOBvPGB7C20yNXGUWWzG5b9TJUQ9fANqN6e82o=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=qBqZrSrtSZF2FacOZaASC1TLWlr3Lp4bM/yBG/DuwgrExuQAw+csAJ2Al1TRtv/Ys Ed5o2N6uBoFVadGI+ufJBx9T4+DiwjNw/h46AT1Sq8io+ArfkXIjeyB1MIKCchXG3A Ns9+gbJcy8ksiy9wgC3MzCfWUeJEGK9chtjYMnTU= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, "Lorenzo Stoakes (ARM)" , Andrew Morton , shaos , "Kiryl Shutsemau (Meta)" , "Gregory Price (Meta)" , Xueyuan Chen , "Liam R. Howlett" , Vlastimil Babka , Jann Horn , Pedro Falcato , Mike Rapoport , Peter Xu , David Hildenbrand , Sasha Levin Subject: [PATCH 7.3 1099/1257] mm/vma: avoid broken uffd merge on mremap Date: Sat, 7 Nov 2026 08:56:35 -0500 Message-ID: <20261107135009.611389179@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261107134939.434104957@linuxfoundation.org> References: <20261107134939.434104957@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Lorenzo Stoakes (ARM) [ Upstream commit 68cb098eadda190783f8f8bdf9fe764df59f7d32 ] Userfaultfd often causes unexpected interactions within mm, and does so at the intersection of uffd, mremap() and VMA merging. When an mremap of a VMA occurs, copy_vma_and_data() calls mremap_userfaultfd_prep() to notify uffd of this. However, mremap_userfaultfd_prep() simply drops the UFFD context for the VMA if the user did not register it for remap, i.e. didn't specify the UFFD_FEATURE_EVENT_REMAP flag. This is OK for the VMA itself, as upon page table move, move_ptes() clears UFFD page table flags, using vma_has_uffd_without_event_remap() to determine whether to do so. However, this doesn't account for merging with adjacent VMAs, whose page tables won't have received this treatment, and thus latent UFFD markers will be left around. Worse, it'll break UFFD for any VMAs it is merged with. This is very surprising behaviour for a user, who will then simply not receive UFFD events for VMAs they ought to. Resolve the issue by explicitly checking for this case via vma_has_uffd_without_event_remap() in vma_merge_copied_range(), the merge function explicitly designated for mremap() operations. Also update the userland VMA tests accordingly. Link: https://lore.kernel.org/20261020-uffd-remap-fix-v1-1-0f34fc181f0a@kernel.org Fixes: 3cfd22be0ad6 ("userfaultfd: clear flag if remap event not enabled") Signed-off-by: Lorenzo Stoakes (ARM) Signed-off-by: Andrew Morton Reported-by: shaos Closes: https://lore.kernel.org/all/20261020110748.959870-1-shawnq1@163.com/ Reviewed-by: Kiryl Shutsemau (Meta) Reviewed-by: Gregory Price (Meta) Reviewed-by: Xueyuan Chen Cc: Liam R. Howlett Cc: Vlastimil Babka Cc: Jann Horn Cc: Pedro Falcato Cc: Mike Rapoport Cc: Peter Xu Cc: David Hildenbrand Cc: Signed-off-by: Sasha Levin --- mm/vma.c | 5 +++++ tools/testing/vma/include/stubs.h | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/mm/vma.c b/mm/vma.c index 81e2c93a1cc52..3d63f73e723b9 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -1225,6 +1225,11 @@ static struct vm_area_struct *vma_merge_copied_range(struct vma_merge_struct *vm vmg->copied_from = vmg->middle; vmg->middle = NULL; + + /* If not registered for remap, uffd context will be cleared. */ + if (vma_has_uffd_without_event_remap(vmg->copied_from)) + return NULL; + return vma_merge_new_range(vmg); } diff --git a/tools/testing/vma/include/stubs.h b/tools/testing/vma/include/stubs.h index d6136e19a8af3..ab441162ac10e 100644 --- a/tools/testing/vma/include/stubs.h +++ b/tools/testing/vma/include/stubs.h @@ -356,6 +356,11 @@ static inline bool is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct *vma, return true; } +static inline bool vma_has_uffd_without_event_remap(struct vm_area_struct *vma) +{ + return false; +} + static inline bool anon_vma_name_eq(struct anon_vma_name *anon_name1, struct anon_vma_name *anon_name2) { -- 2.53.0