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=1796381208; b=G6Xl6yds3a506bFWa7FsWq9c9o5Cg9lLqNnHB8VdwUWoZ0WxNtx6EqB/RPvHO2EvrKbm ZdxVor4c0XL+JDX07RFcIwTOh7tx4Bvun+Ti8vuGQxaJh7IwZOFrdu1fVIEKEtkCoq1R2 C9GFBwoJiCUAD7JImSEsqWruU/cGynTMrLcrqdYYz+6ymJwcCFCqnEu4c4lHfrkP7wMbm LmiVSKfNJG/aH/Rc/5j37WHmfwGtGNttdg5CB2M4jaRoOSGj5vnrHWBq+7IjTRVNfxg1u +gCN56B/GjSPmptb6SdFb/X/h3AnDu5CRh/OPRx41bkVhNSna89t2OyQW7AaZuVrTsA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381208; 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=FOOaUtA6bGqHyVC0o3iC/O7N3Og/YQkoPdoY3LNKNFY=; b=gYUaiKrPquGqZGuvAiqqIaPJczA1EORFzTOBHEo7kb8JZKYRAtByeuochnqAsahDhWJM F+CP1gbdWcn5Md0igV8VPKFfdliAaX43h16CYD+p0pkhLpbMrFGOvm1VJC6coGFPcAiQy ptsd7grFiyoi70g2Mrknr4rUUwxdz70n3+mQSxc3Tish4p1NQj9kG8GvOXWOvhcMnc/KT 978SOwzt3WJFC5YD34GnPlnHtEs5DC6xFx5x53jQrUl5x/mlEiC8sEAYl4IwkjfeshKV3 bLT7LxmxdHKjUW7/hMcniaRlP1oyTwhjwqrQ1o2hFu/HS88MSYGCN2R+M+daTbrqSRA== 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=P+RRpIY2; 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 EA3D9606A322; Fri, 4 Dec 2026 10:46:47 +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=P+RRpIY2 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2F58C1F000C2; Fri, 4 Dec 2026 10:46:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796381207; bh=FOOaUtA6bGqHyVC0o3iC/O7N3Og/YQkoPdoY3LNKNFY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=P+RRpIY2jbL1fiKDLNWcUe6D3RIRXSVC2Od0BRYVi/FHws64rfWzM7U6LBfrUoWuc PlT6DJZRV7Cxrf9jlFQwnBh/HGc1ibcIJeMimRWgU3D19I/FNvq/Oq3co0G8lFJqgC NIfAAz8l2r81MOSKO7PSWQ/MagBx5fFeZ2xOYqkI= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, "Mike Rapoport (Microsoft)" , Andrew Morton , "Jose A. Perez de Azpillaga" , Tal Zussman , Peter Xu Subject: [PATCH 7.3 604/742] userfaultfd: clear uffd state in child PTEs if uffd is released during fork Date: Fri, 4 Dec 2026 19:07:48 +0900 Message-ID: <20261204100651.165985628@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore 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 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Mike Rapoport (Microsoft) commit 111894c595a078d8481db34f3355e8531990668e upstream. 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: Signed-off-by: Greg Kroah-Hartman --- mm/userfaultfd.c | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -2239,6 +2239,32 @@ static void userfaultfd_reset_ctx(struct userfaultfd_set_ctx(vma, NULL, 0); } +/* + * Clear uffd state from a WP or RWP range: the uffd bit, WP markers and + * RWP PAGE_NONE entries. 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) +{ + unsigned int mm_cp_flags = 0; + struct mmu_gather tlb; + + if (!userfaultfd_protected(vma)) + return; + + if (userfaultfd_wp(vma)) + mm_cp_flags |= MM_CP_UFFD_WP_RESOLVE; + if (userfaultfd_rwp(vma)) + mm_cp_flags |= MM_CP_UFFD_RWP_RESOLVE; + if (vma_wants_manual_pte_write_upgrade(vma)) + mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE; + + tlb_gather_mmu(&tlb, vma->vm_mm); + change_protection(&tlb, vma, start, end, mm_cp_flags); + tlb_finish_mmu(&tlb); +} + static struct vm_area_struct *userfaultfd_clear_vma(struct vma_iterator *vmi, struct vm_area_struct *prev, struct vm_area_struct *vma, @@ -2258,22 +2284,7 @@ static struct vm_area_struct *userfaultf if (start == vma->vm_start && end == vma->vm_end) give_up_on_oom = true; - /* Clear the uffd bit and/or restore protnone PTEs */ - if (userfaultfd_protected(vma)) { - unsigned int mm_cp_flags = 0; - struct mmu_gather tlb; - - if (userfaultfd_wp(vma)) - mm_cp_flags |= MM_CP_UFFD_WP_RESOLVE; - if (userfaultfd_rwp(vma)) - mm_cp_flags |= MM_CP_UFFD_RWP_RESOLVE; - if (vma_wants_manual_pte_write_upgrade(vma)) - mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE; - - tlb_gather_mmu(&tlb, vma->vm_mm); - change_protection(&tlb, vma, start, end, mm_cp_flags); - tlb_finish_mmu(&tlb); - } + userfaultfd_clear_ptes(vma, start, end); ret = vma_modify_flags_uffd(vmi, prev, vma, start, end, &new_vma_flags, NULL_VM_UFFD_CTX, @@ -2382,8 +2393,10 @@ static void userfaultfd_release_new(stru /* 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); }