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=1796382954; b=YF9lspl0WsKhH9dPMZ8Zh5/jti9PpfrUkgb2s50LWjpz2vVbzk96fRZp9MLnqYWOg6iX iKCEVmvcG3Aoj4ut9wh1qLmieZoDF5NRthGR7yvP6q9GHPd7Ak/SsAaD3CYX3qiGdEtRj IQAmPKJ1sEqJm38aNPZw0ejqjMrYD4MdoKKQtceNSXTBIZrnnVcjNS8UZ1XBWl5z5QX9A UqE5qp55e4VPaszicv42rjLmqQYdgOGScf25y2WgLKQJ8xkTI6dAgjTi7Poot4ZqxmzE9 9zxCn11SP5Un4KecClFxHDKJlcikORJ0jVXrlU7HXednhTi+l8Xsqc3JJjop/X1pFfA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382954; 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=auR7DLOl8BAneCkhljaBB574nXJBv2xEz+ERwmERYuM=; b=ohXlK6LjPRcE5/BtE8tfEwp9ENQ5rTAUdIitM8K9YvE720tieIOdZH7caCtB4tLUXdPT Yi82bmyryqedxNI13xrDuzZuPPAAyIoHFyt9ox1U9lu8b2YTGYefLhC9MOV8lSqE7qRt4 gGtWi6ervO6q9fc5WElchlT+7IrR8BgnFI+sH+VI7YwzH+mJQm1GwP22m2Imo0+sxTWiK FZgvzP/A30dYp6SyK5DhRtZIecZtLNWL5YsnrBy6jFM3gyTFITlXVORbd9H5EPgc7sLBp SNB5rB+31CRHbCPhTjKcFOvLmSmDnhHFQ17NZF/wEyGwgbvHsVcuIn+ztR/o2UKVn3A== 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=ZWsL37nX; 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 13462606A333; Fri, 4 Dec 2026 11:15:54 +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=ZWsL37nX Received: by smtp.kernel.org (Postfix) with ESMTPSA id 75CD31F00935; Fri, 4 Dec 2026 11:15:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382953; bh=auR7DLOl8BAneCkhljaBB574nXJBv2xEz+ERwmERYuM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=ZWsL37nXtdJHpQnxVNNaTgGMSs8N+KUYhk5/kER0EHVbL12Pi+z7+wD5vGENYtuGh 9Sa1Ne6regOrgQpwK+d0gXAOkgBrdZienYNTys/iWY0c74RHgXb9dH5BT8LE4CyP6A 6Bkc2f2nwOovXKtU8d9PfdI/ZWnaw0seMSpmYJxc= 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 , Sasha Levin Subject: [PATCH 6.18 663/703] userfaultfd: clear uffd state in child PTEs if uffd is released during fork Date: Fri, 4 Dec 2026 19:09:22 +0900 Message-ID: <20261204100645.424330165@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@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 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ 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 RWP-based PTE reset in the new helper with this tree's WP-only uffd_wp_range() reset, since RWP helpers and flags don't exist here. ] Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- mm/userfaultfd.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -1994,6 +1994,18 @@ void userfaultfd_reset_ctx(struct vm_are 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, @@ -2010,9 +2022,7 @@ struct vm_area_struct *userfaultfd_clear 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, @@ -2111,8 +2121,10 @@ void userfaultfd_release_new(struct user /* 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); }