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=1796379543; b=iFJZAmYFxB48os9j0zH3+NP/y+MUKRzUR/Hn02+2Tc/sRFABMyYwrSi1XdtK/eTIwFY1 GEOQZsaHhf3h92c8t9xETWbF0HxJ2+9VP80+krOIg3IjOfDJaMayoW6vpx1OXZdoBvmFy kN/yCizyyL4Ee6yY3gm9tTLLqYs4347ZVK7a2Z1K6NX5VLpfovsr0TMqYUHj5ozdcz5rm ChGW5zbGmOaPkmYQ8OGrET3J3ubHjnBfO0sKkcay8A6pvYjpeiQWjiVea7W/zQvGhJJzK SsOjxEvJ/JGB62JXn+W7WTiS0qtgDUG9i2dfbITDEPwvCNRCz2uOjV0voOavcemZpEQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796379543; 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=a6rv3+VYBGkySFsfuO4vLTWMY7NTSAwypW1o2PyHNwg=; b=UUr0/ecUMVAww4kS/ANAxdyUtkbZpXrql6bWgyUIDV/4iRgJaBZUEXuVsTMYHWn7bxDx 3TZU+EbXVqe1da7ZmQ+gSJCCvT8JgDcL6EqXb3JCITXQ9HSz00VWphxb9DiA8l3aftVik fV62WrOK9X8ZCYCcvOVqJELYzV8/4wKy7VZi/c0f/5TzX1c31mOSlOnEo5iAs3hqFezci lXdCFXRMOI0pdujzZKjLkqmc2zC90xWwh0+0JnDp/WQGajh+D+liaCwSVQm0gcdo/kCTs ImLnF9lYSm+sy/2qjVkejpETUG4srRdpMVaCZ50A2dpfWt/sPmi0ej/7iIU06yC/uUA== 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=VBItJc13; 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 F0FB6606A322; Fri, 4 Dec 2026 10:19:02 +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=VBItJc13 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 519771F000C2; Fri, 4 Dec 2026 10:19:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796379542; bh=a6rv3+VYBGkySFsfuO4vLTWMY7NTSAwypW1o2PyHNwg=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=VBItJc13y/UQvtyH63L3Lavg9MuFdaUyRzNssTHBrnKkVYc7DImx2eNjPizsydKes icHn+Fr0OdpSnyC8rPQq6C+9pHMzoMFINu5MXZspe0x8o+q8FLKWcKBwXHdQIBbvYz 7VhMaqtwm2LzXgJtL6HbfQbtrnxlrsMZD5QTPRiQ= 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 , Xu Xin , 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 Subject: [PATCH 6.6 0013/1167] mm/ksm: fix userfaultfd protection during merge and unmerge Date: Fri, 4 Dec 2026 18:50:40 +0900 Message-ID: <20261204100702.394118106@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev 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.6-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 cd68f44754af1..13208e214d234 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1258,6 +1258,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_get(ptep))); /* * No need to notify as we are replacing a read only page with another -- 2.53.0