From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E6FD6606A322 for ; Fri, 4 Dec 2026 06:33:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=NTZTF6gw Received: by kanga.kvack.org (Postfix) id EFC506B05BA; Fri, 4 Dec 2026 01:33:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E866B6B05BB; Fri, 4 Dec 2026 01:33:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D26966B05BC; Fri, 4 Dec 2026 01:33:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id A61F16B05BA for ; Fri, 4 Dec 2026 01:33:24 -0500 (EST) Received: from smtpin19.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 01FF3400FF for ; Fri, 4 Dec 2026 06:33:18 +0000 (UTC) X-FDA: 85505333718.19.1EA914E Received: from mta1.migadu.com (out-18.mta1.migadu.com [95.215.58.18]) by imf08.hostedemail.com (Postfix) with ESMTP id 766F716000E for ; Fri, 4 Dec 2026 06:33:14 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=NTZTF6gw; spf=pass (imf08.hostedemail.com: domain of xu.xin@linux.dev designates 95.215.58.18 as permitted sender) smtp.mailfrom=xu.xin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796365997; b=qIlZDyxrfbcD+rG2nRcKyhAWUw5DnflB6j440PmmCTtWMBMTCrS2+aW+XsXQJLvPnRxlP+ Tlmv8bH3mdN5MShsvrNGEXgLKcIMl+8W7cWMZbcyjeFsblPOO7FHwtqovkvEQ4DQfxT2lK T6OEud+JdW/XzN0/WeZtcL5xk1pkYbs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796365997; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=89ph9MkyhWkeVwe61ccqhTx0Kovjo4a+BhHEgmsgv0k=; b=dpLx40eoKoKKDK1ni34K/wMksjnEE8eUZnaX7V/F9n8pUhggAWGjXsKOJATiH+M3Kv3JBk p4PimIfflmKON4SaYkKwWfZhHLXYPoNQkx4RMiNsgRXqwMOk+DEx7lE0+VG9Zq5qgGyjZt PH3KwX1hmna/cFExvRH6c64X0+DULcM= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=NTZTF6gw; spf=pass (imf08.hostedemail.com: domain of xu.xin@linux.dev designates 95.215.58.18 as permitted sender) smtp.mailfrom=xu.xin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=OoyZ7olSwN4DATd1io9fWyDTvHY92Qzz10rLUwJhqCs=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796365985; v=1; x=1796970785; b=NTZTF6gwjeNht8UDZuzHKUM941+ISEt9S/Y/k9+WC3RFx5NOVnHjGfLcck13tqpAgh8w6Mi+ Q2iNZpU2OZjEOKBrdKg0sazX75mqrHfXH04B0DCh8Xuw3EPFTtY3PXgvbgc6o8SbHUjx0FdcGsT /BzhVloV5P43+FfPsfpsYtbY= X-Envelope-To: linux-mm@kvack.org Received: by mta12.migadu.com with ESMTPS id af2ea1f6b4bfda5e; Fri, 04 Dec 2026 06:33:04 +0000 X-Mizu-Trace-ID: af2ea1f6b4bfda5e X-Migadu-Flow: FLOW_OUT From: xu.xin@linux.dev To: david@kernel.org Cc: Felix.Kuehling@amd.com, James.Bottomley@HansenPartnership.com, airlied@redhat.com, akpm@linux-foundation.org, alex.sierra@amd.com, alex@shazbot.org, apopple@nvidia.com, ast@kernel.org, axelrasmussen@google.com, balbirs@nvidia.com, baohua@kernel.org, baolin.wang@linux.alibaba.com, baoquan.he@linux.dev, bigeasy@linutronix.de, byungchul@sk.com, chengming.zhou@linux.dev, cl@gentwo.org, clrkwllms@kernel.org, dave.jiang@intel.com, dave@stgolabs.net, dev.jain@arm.com, djbw@kernel.org, gorcunov@gmail.com, gourry@gourry.net, hagen@jauu.net, hao.li@linux.dev, harry@kernel.org, hughd@google.com, jack@suse.cz, jannh@google.com, jgg@ziepe.ca, jhubbard@nvidia.com, joshua.hahnjy@gmail.com, kas@kernel.org, kasong@tencent.com, kraxel@redhat.com, lance.yang@linux.dev, liam@infradead.org, linmiaohe@huawei.com, linux-cxl@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-rt-devel@lists.linux.dev, ljs@kernel.org, manfred@colorfullife.com, mason@kernel.org, matthew.brost@intel.com, mgorman@suse.de, mhocko@suse.com, muchun.song@linux.dev, nico.pache@linux.dev, osalvador@suse.de, peterx@redhat.com, qi.zheng@linux.dev, rakie.kim@sk.com, riel@surriel.com, rientjes@google.com, roman.gushchin@linux.dev, rostedt@goodmis.org, rppt@kernel.org, ryan.roberts@arm.com, shakeel.butt@linux.dev, shivankg@amd.com, shy828301@gmail.com, sj@kernel.org, song@kernel.org, stable@vger.kernel.org, stevensd@chromium.org, surenb@google.com, usama.arif@linux.dev, vbabka@kernel.org, vivek.kasireddy@intel.com, wangkefeng.wang@huawei.com, weixugc@google.com, willy@infradead.org, xu.xin@linux.dev, ying.huang@linux.alibaba.com, yuanchu@google.com, ziy@nvidia.com, zokeefe@google.com Subject: Re: [PATCH 28/32] mm/ksm: don't convert a shared anon folio into a KSM folio Date: Fri, 4 Dec 2026 14:32:27 +0800 Message-Id: <20261204063227.646753-1-xu.xin@linux.dev> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261129-fixes_core_3-v1-28-f66094116801@kernel.org> References: <20261129-fixes_core_3-v1-28-f66094116801@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 766F716000E X-Stat-Signature: dtmgttp9935kgc7y6fkazaytsjf3yerg X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796365994-34966 X-HE-Meta: U2FsdGVkX19gIQFkuvL9xZlu6KCOrX/YidcoCeYBL+l1FdYRrArcMErSEstkmNlDXpBxmVvoWPZw7sbqHRaWU3F8ZXWQHf0O4optglllpQTE5qFQuzZ+ckpGeTfp/8o2FSEZrn3mkICj7W/YAaNR05WBLbOjeUTu2tzHxr4uGLYHxLE9CK8C0lgQRmiFpYrsbWCH73k35b0fzhO2r92cN4g6XvycygWYz/H9zw7MpVu2XYGonlVa0jczg9nhH1uIoBuEZpoiDuQPLBoNn2w1AH0jdFTdSF4ZP3u+dKBadvm9kwLb9WTnA2LUvVnWK6x1c9JBbxBwllsZD4LYs6/j00NyxVUijLOx6B84GavdJpcFOuo+t0qr1HfxWksf+dSGI8wrYnbg/xaniG8Su+GTEuo/4Z0+5NZVFqdAaB5jjc5lzE7TcPZC1GakfNjoCQkcJHlIKuT45L1mM19P2PDaafXUsyGhDOvYTZT7VQ9QXsKZl3OwMW2iU268gxDEItsX/DepD72Q75dLKToh97ogDXMhXzWFR6bsinGnwHaI6tVAgky+WcH5eQ81/4d2ufHP6197hBdSbIaMyhcDW9k0noRCIySGIDCXdbHVw7vhX1spXlIXMequwOhU6HtL8y8cljyU2WMSrO5EeGUDzsGmLM6P9xLN3FhtpY4T1+lc0WzUr9jzDFIyW7EDOrCcdHT2fwqz9Ncl4rd8PWbjqdZSkUBG/+XeuFWILrNNCNdaSsHIbwNrjb9JXoDItUb7CrwS+iDpu+1iDt2H0ImPjSXjuhh+4K4rqIutjxIwK4ekipUE36+G7D87F7FlbGQAK8rCrjt3cXhOCLnPb3BfklbkgQMWJ+RxPbXjLfeBElA8pa9ZGHcKvmRha9KBzaPUF2RGpx3XBvnk5H1peEQS++yiI7wyk2cx6uIzgUcRVNlydSk0JYa9yz/5c4dLrEVx9dw6pwBEnnFjw18t17Deirz WFZUQC/m /dc0v3sNOx3HkHyxLSMo5RwXiWDbxo/HQLziDZG14Dt5w85hzq42VxZo8xb1ITlxcxoN4uifc+0PRWerTA4fBCzKcyTP5H6+T7pKpm8Gb9vZ0yEgJssCa6el8tBm27dlntjPCjVQJgyGV4mkoYRLWtF+mD+zGMDcKKYXprcUxcRqRM6rdG6USy6lzSfYN9T88RhNdLWgWwbad74yaE5CMpEPWTwzDA4IFUksfy2rZp0JLBoUnoKQoBjtou+wYs0N4ZnpYrYLG8fQbU3f4xuxGZ6GARthiAViWB+doNZ7IR3T4V38tpeCni+TdeiRD79XjtBselso/zUmDBh4Xt3YkSYSM53mXeebjbFs/W8urK6RWrdCKI8HmcqRdTUYUN+570Q7mdg3r/TsrMf7lw6dXLXpMFznAtfBvD5lTrVDMOzyo/K4V4o/4zE66B8MNO2lOddpHB/PIs1lKL+LLcURCRkIDZeMgNmhvB1n7oRPHORk98knKrLhcwYIRRTbscJcLu4g+WyHEv+uVElFMs7k0z+QhOg== Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 > So far we were under the assumption that only VM_MERGEABLE VMAs may > contain KSM folios, because we unmerge when clearing VM_MERGEABLE, and > KSM only merges within VM_MERGEABLE VMAs. Various infrastructure (e.g., > rmap walks) relies on related properties: for example that a KSM folio > is always mapped at rmap_item->address, also in child processes. > > We ignored one scenario: turning a COW-shared folio into a KSM folio in > one process will make all processes mapping it suddenly see a KSM > folio, even if VM_MERGEABLE is not set for them. > > Consequently, rmap walks can miss such folio mappings, resulting in > the KSM folio no longer being reclaimable or migratable. > > Worse, if such a process calls mremap() while the kernel is migrating a > KSM folio, mremap() moves the migration entry that try_to_migrate() > installed, and remove_migration_ptes() will not find it at its new > address. Once migration frees the source folio, the moved PTE still > holds a migration entry that refers to the freed, and possibly already > reused, folio: the next access through that PTE will hang, or worse. > > Further, if ksmd scans the child, rmap walks will not even find the > parent's VMA: the child's rmap_item references the child's anon_vma, > which the parent's VMA is not linked into. > > Let's fix it all by refusing to convert a folio into a KSM folio if it > might be shared with another mm. When merging two folios that are not > KSM folios yet, try both directions: as long as one folio is not shared, > we can merge. Hi David, two quick thoughts: 1. Dose refusing to merge two CoW-shared folios might reduce merging efficiency in some workloads (e.g., QEMU)? I' wondering if we have another approach instead of blocking it, could we store the original folio's folio_anon_vma() (and linear_page_index) in rmap_item under the folio lock and drop the addr check in rmap_walk_ksm()? That would allow rmap_walk_ksm() to find the parent's VMA even without VM_MERGEABLE or after mremap(). Or does exposing KSM folios to non-VM_MERGEABLE VMAs still break too many other mm assumptions? 2. If blocking two CoW-shared merges is the only viable option, it's deserved to explicitly document this limitation in Documentation/admin-guide/mm/ksm.rst. > > Shared folios can still be merged into an existing KSM folio, the shared > zeropage or an identical folio that is not shared. Only identical folios > that are all shared will no longer be merged, which should be fine for > the known use cases. > > Note that other processes might only hold a swap entry for the folio, > and map it after swapping it in. As the previous commit only copies KSM > folios at swap-in into VMAs without VM_MERGEABLE, also refuse to convert > a folio that is in the swap cache, unless we can free the swap entry. > > This has medium urgency, as an unprivileged process can trigger a > use-after-free if KSM is enabled. > > Fixes: 80e148226028 ("ksm: share anon page without allocating") > Fixes: 5beb49305251 ("mm: change anon_vma linking to fix multi-process server scalability issue") > Cc: stable@vger.kernel.org > Reported-by: Chris Mason > Signed-off-by: David Hildenbrand (Arm) > --- > mm/ksm.c | 47 ++++++++++++++++++++++++++++++++++++++++++----- > 1 file changed, 42 insertions(+), 5 deletions(-) >