From mboxrd@z Thu Jan 1 00:00:00 1970 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=1796360238; b=MlbU9hbkmjtFaCmvgBySod+R2C7gf1BxFpSh1oDY0jbrLwHnL8d0J1jo8O5KXoUKuay7 KJ/7pwwI327OFAoABSGLm0oO0/wNumvnPtPjbZEGWx5GGO76fe0HjaFA7ABMGTSVSvvl+ 86HjGgQUzaL+z/yKeOi7jw+gjEGLXwstJQPThF5c16IpRB/j0d9IZ1Bia3efWq6O6ejek pqu9IzDijmJ3tUd9oX1eahuHiZa1c2Mjb0Is3BBLeEbsPuOVPPpBVYEQXN3EJQg7PTSxh JS+4Jh4ykLmYjQAHSJhSaWhbrrn4OrBTk8Ljr2z05YY2Y+FJ1nzVpfsiFdg0x5yQ7Pw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796360238; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=MZqZK8LpR4deoXTi3mbRozlBfdF00SIZWM+/p8V+e6E=; b=2MjkkUBl0HRyMfiNktGKF/6gjUiNFrIMzNADz5p8/Ok9cwZccWTL5o72hZ2Xktr2zUpP b9KS1+vR8iLYC9Cf7K5fyEjwMIRoot5QqHiVyYdhlJvEB6n62/fsB4aSpPkdSCVGVvuOm HnmaMEOyykhIWpq3+orjLAthV9+SamJUaN9Jb0vmG6ZkwQgm4AT1AjlIYB0LovvfLPfEY TiF/vB7CF6uc+aqFN0nqUScpY+PVgyMC2mISvjx/eAoDNJ44szWELLKYkd3w30d8Dx10u z/byKW5okB749Yg2Xs5pml+gfrlkVbsASkgqSoF2CG8WYittG9I9w2umZAl8SixmQEw== 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=RZOd6mN4; 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 1F7A4606A322 for ; Fri, 4 Dec 2026 04:57:17 +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=RZOd6mN4 Received: by smtp.kernel.org (Postfix) with ESMTPSA id C83F61F000C2; Fri, 4 Dec 2026 04:57:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796360237; bh=MZqZK8LpR4deoXTi3mbRozlBfdF00SIZWM+/p8V+e6E=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=RZOd6mN4bQgwhm2KL1pH01R/0qIZnNtmVPSOKuJL6Ezsuw2Al9dpXVwGZ3EE14iOL K7rIUGNOmq2a+sE0rzDlIgIwVNrNKMr0rWZa622wUXru8uPDI2cWb+l4xubvCOywPp pOePd+f8SYQ0kGLS8AX6wDosnKETU/E1P28LvmcUSCTCN3ukwdhO01RXo4pV60SXUO Wfli11w5pEUEzrKZo+pvKTtlQfV0j+Wp/QT720Zo9Roy6JKaZw3rl90UtaFVwm+plf bPiSf3EVhx1cjvsyFdtkYsWYm1b+mB0QV+kM2Dzv21hr6k7N7mgjIstoFcCHXSMesO PN5lBX1ALzK5A== From: Sasha Levin To: stable@vger.kernel.org Cc: "Mike Rapoport (Microsoft)" , Andrew Morton , "Jose A. Perez de Azpillaga" , Tal Zussman , Peter Xu , Sasha Levin Subject: [PATCH 6.12.y] userfaultfd: clear uffd state in child PTEs if uffd is released during fork Date: Thu, 3 Dec 2026 23:57:15 -0500 Message-ID: <20261204045648.2693679-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026120249-emblaze-hatbox-baf0@gregkh> References: <2026120249-emblaze-hatbox-baf0@gregkh> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: "Mike Rapoport (Microsoft)" [ Upstream commit 111894c595a078d8481db34f3355e8531990668e ] With UFFD_FEATURE_EVENT_FORK the userfaultfd context is duplicated for the child process, the child VMAs are registered with the new context, and uffd protection state from the parent PTEs is copied into the child. If the parent's userfaultfd is released before the fork event is read, userfaultfd_release_new() resets the VMA flags but keeps uffd state in the child PTEs. This leaves the child's VMAs in an inconsistent state: they are not registered with userfaultfd but their mappings carry uffd protection state. If the child registers the range with userfaultfd again, the stale state turns into protection the child never asked for. When a write fault makes such a PTE writable without copying the page, the uffd bit remains set and page_table_check_pte_flags() warns about it. Split resolving of the uffd PTE state out of userfaultfd_clear_vma() and call it from userfaultfd_release_new() before resetting the VMA. Link: https://lore.kernel.org/20261031-uffd-prot-bits-fork-event-v2-1-c448e57df417@kernel.org Fixes: f369b07c8614 ("mm/uffd: reset write protection when unregister with wp-mode") Signed-off-by: Mike Rapoport (Microsoft) Signed-off-by: Andrew Morton Reviewed-by: Jose A. Perez de Azpillaga Tested-by: Jose A. Perez de Azpillaga Reviewed-by: Tal Zussman Tested-by: Tal Zussman Cc: Peter Xu Cc: [ sashal: Replaced the change_protection() based clearing, which also handles RWP, with a userfaultfd_clear_ptes() helper that calls uffd_wp_range() for WP-only VMAs, and the flags use vm_flags instead of new_vma_flags. ] Signed-off-by: Sasha Levin --- mm/userfaultfd.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index f592f92c74079..f6be2a4134b96 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -1918,6 +1918,18 @@ void userfaultfd_reset_ctx(struct vm_area_struct *vma) userfaultfd_set_ctx(vma, NULL, 0); } +/* + * Clear uffd state from a WP range. Call this before dropping the + * registration, as it needs the VMA flags to know what to resolve. + */ +static void userfaultfd_clear_ptes(struct vm_area_struct *vma, + unsigned long start, unsigned long end) +{ + /* Reset ptes for the whole vma range if wr-protected */ + if (userfaultfd_wp(vma)) + uffd_wp_range(vma, start, end - start, false); +} + struct vm_area_struct *userfaultfd_clear_vma(struct vma_iterator *vmi, struct vm_area_struct *prev, struct vm_area_struct *vma, @@ -1934,9 +1946,7 @@ struct vm_area_struct *userfaultfd_clear_vma(struct vma_iterator *vmi, if (start == vma->vm_start && end == vma->vm_end) give_up_on_oom = true; - /* Reset ptes for the whole vma range if wr-protected */ - if (userfaultfd_wp(vma)) - uffd_wp_range(vma, start, end - start, false); + userfaultfd_clear_ptes(vma, start, end); ret = vma_modify_flags_uffd(vmi, prev, vma, start, end, vma->vm_flags & ~__VM_UFFD_FLAGS, @@ -2035,8 +2045,10 @@ void userfaultfd_release_new(struct userfaultfd_ctx *ctx) /* the various vma->vm_userfaultfd_ctx still points to it */ mmap_write_lock(mm); for_each_vma(vmi, vma) { - if (vma->vm_userfaultfd_ctx.ctx == ctx) + if (vma->vm_userfaultfd_ctx.ctx == ctx) { + userfaultfd_clear_ptes(vma, vma->vm_start, vma->vm_end); userfaultfd_reset_ctx(vma); + } } mmap_write_unlock(mm); } -- 2.53.0