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=1796379561; b=vH57LexYHS0HYXEjf1meq44zIrdPODlMyRbIbr+D2YH3PneHJkQ9H0yIm46M8nYoV0Cx p6PqQy4WS+ZglUs3F8novojg05HobHkvR3CaoHjddX0WiL6W8tdmFFYJvIVLNA1sxHIiv EF4XUdu9PKh2QRtsw4A66c83NkOq8iABrRUQOW0DjDek5wtdtUaXIMBBkzseZp90zNsGd 2lfMI3WZHYxmMkoPZWqwyhKuN1jjRLK8H1Z4WS54rRBQM2orpsHQfSAi0Sj14l1YZpL7j zYkqREksTs/Q3Hl8aVKV2nq/xzZikaR8UjuGfO/FU3uPsCCMKpPwrlwQoUX8JbrEE+g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796379561; 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-Type:Content-Transfer-Encoding; bh=keO+4t1/DBDFaDlHCT2tBwiz5Oex0pWO5BW+e7CeBnI=; b=grGAswKWygx2Kq2kopg0GhcxygjF8SBtF+iX+wE2WCUG5oFKa5AWIjqltEgNHfHw2HBD SV++RAqYxIxcDe5HPHcEo/0AFMwF/aRzKxaeRgE/PKHoRtaGCY+rMuzTyTuJqPBsuvAFe BfbFT6lka+oDvlSzEgKYh4BsiCQMOFIDvNtE9fhzr9VC+6QsW9m48GpWx8JFXGmwdyBRo CK7OgA+0/dnLtF26h4hbDbZO1iogvKTdqrenNCyYLTbnM924AGCdTkiP9kW56Acllgy1L WbmgvTMRGjDTVs30jK6NKnnN8TRbotoMqOcJSsrk4IirW6iO5Yj2ZiE3fhyceZZQrZQ== 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=mKgGcCCa; 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 E8793606A322; Fri, 4 Dec 2026 10:19:20 +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=mKgGcCCa Received: by smtp.kernel.org (Postfix) with ESMTPSA id 42DDA1F0093A; Fri, 4 Dec 2026 10:19:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796379560; bh=keO+4t1/DBDFaDlHCT2tBwiz5Oex0pWO5BW+e7CeBnI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=mKgGcCCaqR4yxm1Y6kdb168q4k+3s60d7caI8ph6nr/AMN7rXChlS4P3uLZWkeit/ 4JGcAg/UVY8r6hsh4jXIKWg7+8lTJZvwEsxmHGSL1Husiw3f3pl+g9V7CBoXsEXKCC szdSyc0VGD6lk5oHF9bc/D2S6Ah+RmkA/ruEuXls= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Samarth Narang , Lance Yang , Andrew Morton , "Lorenzo Stoakes (ARM)" , "Kiryl Shutsemau (Meta)" , "David Hildenbrand (Arm)" , Mel Gorman , Chengming Zhou , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Andrea Arcangeli , Peter Xu , Bobby Powers , Brian Geffon , Denis Plotnikov , Doug Gilbert , Hugh Dickins , Johannes Weiner , Martin Cracauer , Marty McFadden , Maya Gokhale , Pavel Emelyanov , Rik van Riel , Shaohua Li , James Houghton , Jonathan Corbet , Paolo Bonzini , Sean Christopherson , SJ Park , Zi Yan , Mohammed EL Kadiri , Sasha Levin , Xu Xin Subject: [PATCH 6.1 0015/1059] mm/ksm: fix userfaultfd protection during merge and unmerge Date: Fri, 4 Dec 2026 18:52:26 +0900 Message-ID: <20261204100644.416021895@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@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-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Lance Yang [ Upstream commit 990d6c2194a938a19a8cf94c6cfd75171345089b ] KSM loses userfaultfd protection when replace_page() builds a new PTE from vma->vm_page_prot. Dropping its uffd bit lets a subsequent write go through ordinary COW instead of userfaultfd write protection handling. For RWP, a merged page also becomes readable without faulting. Keeping the uffd bit and restoring PAGE_NONE fixes merging, but we also have to handle unmerging. An existing KSM page can be protected with RWP before MADV_UNMERGEABLE, so unmerging already needs fixing independently of preserving RWP during merging. Unsharing replaces a shared page with a private one and should leave userfaultfd protection in place, as do_wp_page() already does for write protection. Currently, an unshare fault reaches do_uffd_rwp() before it can proceed to do_wp_page(). - With synchronous RWP, handle_userfault() returns VM_FAULT_SIGBUS. break_ksm() then reports success with a KSM page still merged. - With asynchronous RWP, protection is cleared before unsharing. Preserve the uffd bit when merging, and restore PAGE_NONE for RWP. Let FAULT_FLAG_UNSHARE skip RWP fault handling and keep PAGE_NONE on each private copy as well. Unsharing can then complete with RWP still armed for a subsequent access. Link: https://lore.kernel.org/20261026130054.91461-1-lance.yang@linux.dev Link: https://lore.kernel.org/all/20261019010934.697895-1-samarth.colleges@gmail.com/ Link: https://lore.kernel.org/all/20261020055518.36087-1-lance.yang@linux.dev/ Fixes: 63b2d4174c4a ("userfaultfd: wp: add the writeprotect API to userfaultfd ioctl") Fixes: 2d427bb01639 ("mm/userfaultfd: add RWP fault delivery and expose UFFDIO_REGISTER_MODE_RWP") Signed-off-by: Samarth Narang Signed-off-by: Lance Yang Signed-off-by: Andrew Morton Co-developed-by: Samarth Narang Acked-by: Lorenzo Stoakes (ARM) Reviewed-by: Kiryl Shutsemau (Meta) Acked-by: David Hildenbrand (Arm) Cc: Mel Gorman Cc: Xu Xin " Cc: Chengming Zhou Cc: Liam R. Howlett Cc: Vlastimil Babka Cc: Mike Rapoport Cc: Suren Baghdasaryan Cc: Michal Hocko Cc: Jérôme Glisse Cc: Andrea Arcangeli Cc: Peter Xu Cc: Bobby Powers Cc: Brian Geffon Cc: Denis Plotnikov Cc: Doug Gilbert Cc: Hugh Dickins Cc: Johannes Weiner Cc: Martin Cracauer Cc: Marty McFadden Cc: Maya Gokhale Cc: Pavel Emelyanov Cc: Rik van Riel Cc: Shaohua Li Cc: James Houghton Cc: Jonathan Corbet Cc: Paolo Bonzini Cc: Sean Christopherson Cc: SJ Park Cc: Zi Yan Cc: Mohammed EL Kadiri Cc: Signed-off-by: Sasha Levin --- mm/ksm.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mm/ksm.c b/mm/ksm.c index 99147df91a69c..6b38684ce4552 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1182,6 +1182,10 @@ static int replace_page(struct vm_area_struct *vma, struct page *page, if (pte_soft_dirty(orig_pte)) newpte = pte_mksoft_dirty(newpte); + /* Preserve uffd-wp when replacing the page. */ + if (pte_uffd_wp(orig_pte)) + newpte = pte_mkuffd_wp(newpte); + flush_cache_page(vma, addr, pte_pfn(*ptep)); /* * No need to notify as we are replacing a read only page with another -- 2.53.0