From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.98 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796365989; b=bps76OlNv6GpvVaC3W6IKxhuBlRqA5m5DyLrdwnd0624DNxab84k6EXVMkRMBlE2fCyh CsVntZbxqQOct7K43ieLInwebS1k6WRtOwJ0zYGPOvX4oJbddSL5VfziGRlE03vtz7E/D hVEAMfyZwI4MEVosp1rIiEw6KGM+e9WwJf3MMLXB6H2eeP1F7kPzTFwoTG6S4qrRFXbrZ vN41plr2RXS4jMQC0WuPeiHDViB8hw/rCZRKzuDBLZ+vxRmkiEaLCQ7Ip+GOXtogUD6LO frZ9XDXNPQPV3ATjA1G8uofnR2pkEsgMdZ0vTkw8ji7CygpHN5M3Ue2DarWwAcKbNtQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796365989; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-Id: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=89ph9MkyhWkeVwe61ccqhTx0Kovjo4a+BhHEgmsgv0k=; b=qr9RDyv2sfJueYXoLczVKV5m/BEL/RFMLNDsqgNjk/cUsgQ6PsbH4U/sHQKbcH5efJBL one11hMnRCgNg6Yd0/kRcTkgINZm7aLHgBlbCFDh0Igco6OJovM8wuGY54199paFbV2Ne dFS6aXGHmcpgbDETLtheFrmUES3k8g60d+HfK/Jm8maFnTRoOwdrjcE86LQMzpC57JQDE t5hCeuepnCXRN7/95sryQCekI79Ad1dcfrqE+IZqhbtmqut6WX4UHMH307SGM7QW6Dquu BgsLVVpW4eOCko8XkL9VuPUi/2FzAVgoxPt9lnXVh12CXdeYGhFRS2rcQtdQ4J4LdkA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=NTZTF6gw; arc=none smtp.remote-ip=91.218.175.98 Received: from mta0.migadu.com (out-98.mta0.migadu.com [91.218.175.98]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC079606A32F for ; Fri, 4 Dec 2026 06:33:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CC079606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev 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 X-Envelope-To: linux-kernel@vger.kernel.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-kernel@vger.kernel.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-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit > 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(-) >