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=1796379590; b=2oDwtZJizPMA9i+V/yQ/mrU3YMf2tiEI/OFBVAw2//LCXR9X4LlAn10k/qMTHXXCzOwf QZRUNy0tzAv/QpFmV8nVXJirRLxequ1J8dRNZRzsCtcP8PhU24AyYDVGrWhjleLRAQnvw f3JAXaoCASQCWMDW2fjilm0+d2JE/f14ZdhhZZ3i3LSVtAKU78/+ytwoa9VOAcJAbeShe R3vTa2jSTb594daInGn2Tq756GwIfd3aaCMs1iFEWHN40/Vw6QVCURYl2BcG0m7n44j2g tNc9kAtO4rjHd83zQflF3NBDilIJZRj+yG3QNvw5Wnae/pgls5g9LcEtpRn6dj+hhLQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796379590; 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=LGZqx7RLTeXsO9cQS/UiuOSUFjF3K5T0bDNsQln23rQ=; b=vMyxUPJZb6gbwJCY79p1sgSeDtWT87DqHdEwFzP0jjNUavCQ8jryx4lnzsyWaKXPkzVf iSMUqw3dM1j2t6xj7hRb9UOC5r/gcpn+XYWnpSmyXcr/QEuDqj/W3a/2c5QL1WbbL/ce1 unO9FPjxOVmjceROtB0QymeWVPyGj8+hd+57ngcKZ7xHtdQI4CVQVdo1SlF54EP2rwJaf VPzPPKa125XHdEb1Kq6/YuEk5FN2qaciTDmYvUE7dfi8BOP8PfGPvjCpVA5rs5Fx+dKdS quGnBI5NPgcElTTmBHSqStKTeq0AJQ3e3+yeZRdu6B1mC0Q/l41SJoWIMN4LhCJGNQg== 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=n9nRoaCa; 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 B4B55606A68F; Fri, 4 Dec 2026 10:19:50 +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=n9nRoaCa Received: by smtp.kernel.org (Postfix) with ESMTPSA id 349851F00931; Fri, 4 Dec 2026 10:19:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796379590; bh=LGZqx7RLTeXsO9cQS/UiuOSUFjF3K5T0bDNsQln23rQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=n9nRoaCaNxxpe1HRzwfJeVGjhI+eouGQ5FD5PIbtnKVIWVMdDFuzMZoez5SvgSWjw oUz7PVPXXt4CTDHQX3VrZqyD5kSV3+LS/f/0+NNTXOyQiUSwLhMkTrntxhEB4Hiuhg oNqHkgQCaa/Q+HX2SaYe7nxqAmOZRMNxUAYaDbQw= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, David Hildenbrand , Peter Xu , Andrea Arcangeli , Hugh Dickins , Jason Gunthorpe , John Hubbard , "Matthew Wilcox (Oracle)" , Shuah Khan , Vlastimil Babka , Andrew Morton , Lance Yang , Sasha Levin Subject: [PATCH 6.1 0013/1059] mm/ksm: simplify break_ksm() to not rely on VM_FAULT_WRITE Date: Fri, 4 Dec 2026 18:52:24 +0900 Message-ID: <20261204100644.359347255@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: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: David Hildenbrand [ Upstream commit 58f595c6659198e1ad0ed431a408ddd79b21e579 ] Now that GUP no longer requires VM_FAULT_WRITE, break_ksm() is the sole remaining user of VM_FAULT_WRITE. As we also want to stop triggering a fake write fault and instead use FAULT_FLAG_UNSHARE -- similar to GUP-triggered unsharing when taking a R/O pin on a shared anonymous page (including KSM pages), let's stop relying on VM_FAULT_WRITE. Let's rework break_ksm() to not rely on the return value of handle_mm_fault() anymore to figure out whether COW-breaking was successful. Simply perform another follow_page() lookup to verify the result. While this makes break_ksm() slightly less efficient, we can simplify handle_mm_fault() a little and easily switch to FAULT_FLAG_UNSHARE without introducing similar KSM-specific behavior for FAULT_FLAG_UNSHARE. In my setup (AMD Ryzen 9 3900X), running the KSM selftest to test unmerge performance on 2 GiB (taskset 0x8 ./ksm_tests -D -s 2048), this results in a performance degradation of ~4% -- 5% (old: ~5250 MiB/s, new: ~5010 MiB/s). I don't think that we particularly care about that performance drop when unmerging. If it ever turns out to be an actual performance issue, we can think about a better alternative for FAULT_FLAG_UNSHARE -- let's just keep it simple for now. Link: https://lkml.kernel.org/r/20221021101141.84170-3-david@redhat.com Signed-off-by: David Hildenbrand Acked-by: Peter Xu Cc: Andrea Arcangeli Cc: Hugh Dickins Cc: Jason Gunthorpe Cc: John Hubbard Cc: Matthew Wilcox (Oracle) Cc: Shuah Khan Cc: Vlastimil Babka Signed-off-by: Andrew Morton Signed-off-by: Lance Yang Signed-off-by: Sasha Levin --- mm/ksm.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 22b991218f403..b35485868d5b8 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -442,26 +442,27 @@ static int break_ksm(struct vm_area_struct *vma, unsigned long addr) vm_fault_t ret = 0; do { + bool ksm_page = false; + cond_resched(); page = follow_page(vma, addr, FOLL_GET | FOLL_MIGRATION | FOLL_REMOTE); if (IS_ERR_OR_NULL(page)) break; if (PageKsm(page)) - ret = handle_mm_fault(vma, addr, - FAULT_FLAG_WRITE | FAULT_FLAG_REMOTE, - NULL); - else - ret = VM_FAULT_WRITE; + ksm_page = true; put_page(page); - } while (!(ret & (VM_FAULT_WRITE | VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV | VM_FAULT_OOM))); + + if (!ksm_page) + return 0; + ret = handle_mm_fault(vma, addr, + FAULT_FLAG_WRITE | FAULT_FLAG_REMOTE, + NULL); + } while (!(ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV | VM_FAULT_OOM))); /* - * We must loop because handle_mm_fault() may back out if there's - * any difficulty e.g. if pte accessed bit gets updated concurrently. - * - * VM_FAULT_WRITE is what we have been hoping for: it indicates that - * COW has been broken, even if the vma does not permit VM_WRITE; - * but note that a concurrent fault might break PageKsm for us. + * We must loop until we no longer find a KSM page because + * handle_mm_fault() may back out if there's any difficulty e.g. if + * pte accessed bit gets updated concurrently. * * VM_FAULT_SIGBUS could occur if we race with truncation of the * backing file, which also invalidates anonymous pages: that's -- 2.53.0