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=1796221392; b=COwFDCXM3BV2l4CekDksUq8t8r9fyR2K/CVc5J1lyZtc0GmZTFQt96OWwV8xNN408c8L GkBe6mb9mW4SegBwQlzWtg5gpz/d/gWHuT6oI/naRk2pH4qfyPlre+0lGmLLj1ye4Lggf m/XDxCxVmxpssiQJIhkWr+J72E0TCWGxO1E5YhQzQ/YHGCA0J8F+FKzsKDIVVx9knSENS QM/FqIxWgxa9AueXZ02tf5gnbRr4IA6FoZSxS31QasGpeOpc++G89uUiNxYjq2GCzRsfL r7t8fsM+szfZtIbWBUK4lZpnEdf1Spxl0xNJCv7fql0cg/bruDEkZf9S6J8dV+MFMhA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796221392; h=Received:DKIM-Signature:Subject:To:Cc:From:Date:Message-ID: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=G7RMzHtbKsG3nZJ01qrxTCzo16xY4BOB+Yokm7WqKW8=; b=jpGx0YoQyD7Hp3sgFqdwWGMGlnOW1QD4Rq22l6D34Lxgj6kr8SAiAAlqoRLPJQMiVN82 k5LOXW1XA5FvM88fnmm33JCZ/D/JnhnES6DfVlJoIpWikSNev+ZJQ3461ZCEQQDTdbqgP 3WLGbhP9YcTe2MsNONjMNaQntDNRrycsQbRshTCTrA9+ylVHEZHuSHRgiCsVrJahan6cY 2e4tA4Zqqs2ApyasCduRIn+d+4Ri3wEeiH2QtBFcAjZrZig6ql8w/bDS4Cm01hXbwQkJh v7fndIR1l5CuYccgz37GSzLgEe5SCK3umqKWkMGYhV7mhJVZ7vXBR6FfsDeqzrfSQCA== 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=KE1liD92; 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 385C7606A31A for ; Wed, 2 Dec 2026 14:23:12 +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=KE1liD92 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8211D1F00896; Wed, 2 Dec 2026 14:23:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796221392; bh=G7RMzHtbKsG3nZJ01qrxTCzo16xY4BOB+Yokm7WqKW8=; h=Subject:To:Cc:From:Date; b=KE1liD92SrchOfxlTOuT3vRs0/UZ78uktlgSM7Bi5mVBtRlSBKGOCfA03ZN3pHWEX xLVEKzrxL+DYCDLZdz9zi/pNfWOUJGzYfnJ9qVFE+uVvgwR/dXxJcjh3GRNZE413Bv 8K9NnW/W6sYevw7JNSToTB7GBmATwBa9bl7CzaR8= Subject: FAILED: patch "[PATCH] userfaultfd: clear uffd state in child PTEs if uffd is" failed to apply to 6.18-stable tree To: rppt@kernel.org,akpm@linux-foundation.org,azpijr@gmail.com,peterx@redhat.com,stable@vger.kernel.org,tz2294@columbia.edu Cc: From: Date: Wed, 02 Dec 2026 14:45:48 +0100 Message-ID: <2026120248-disparity-emphases-d460@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-Type: text/plain; charset=ANSI_X3.4-1968 Content-Transfer-Encoding: 8bit The patch below does not apply to the 6.18-stable tree. If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to . To reproduce the conflict and resubmit, you may use the following commands: git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.18.y git checkout FETCH_HEAD git cherry-pick -x 111894c595a078d8481db34f3355e8531990668e # git commit -s git send-email --to '' --in-reply-to '2026120248-disparity-emphases-d460@gregkh' --subject-prefix 'PATCH 6.18.y' 'HEAD^..' Possible dependencies: thanks, greg k-h ------------------ original commit in Linus's tree ------------------ >From 111894c595a078d8481db34f3355e8531990668e Mon Sep 17 00:00:00 2001 From: "Mike Rapoport (Microsoft)" Date: Sat, 31 Oct 2026 20:14:03 +0200 Subject: [PATCH] userfaultfd: clear uffd state in child PTEs if uffd is released during fork 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: diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 169f681f9849..83ae65394cd4 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -2192,6 +2192,32 @@ static void userfaultfd_reset_ctx(struct vm_area_struct *vma) 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, @@ -2211,22 +2237,7 @@ static 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; - /* 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, @@ -2335,8 +2346,10 @@ static 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); }