Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: xu.xin@linux.dev
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, 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 13:44:04 +0100	[thread overview]
Message-ID: <d39209f4-7a58-41d4-aa15-9871d58f91fd@kernel.org> (raw)
In-Reply-To: <3234ff7c-e264-44c5-bd4f-14f8604e0cd8@kernel.org>

On 12/4/26 12:06, David Hildenbrand (Arm) wrote:
> On 12/4/26 07:32, xu.xin@linux.dev wrote:
>>> 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:
> 
> Hi,
> 
> thanks for taking a look!
> 
>>
>> 1. Dose refusing to merge two CoW-shared folios might reduce merging
>> efficiency in some workloads (e.g., QEMU)? 
> 
> At least not for QEMU. QEMU doesn't really share its guest memory with any
> children, and fork() is pretty rare in QEMU nowadays.
> 
> QEMU sets QEMU_MADV_DONTFORK, unless for qtest (which KSM doesn't really apply to).
> 
>> 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().
> 
> No, having KSM folios in processes without VM_MERGABLE is the real violation,
> and we shouldn't hack around that.
> 
> In this fix I keep it working as long as you don't have two pairs of COW-shared
> pages. If something is already COW-shared, it's effectively deduplicated already
> using a different mechanism.
> 
> So really only what's disallowed is converting a COW-shared page into a KSM page.
> 
>>
>> 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.
> 
> I think this was just one (of multiple, I'm afraid) major oversights by the
> authors of KVM.
> 
> Let me see if I can come up with something for the documentation that doesn't
> sound too specialized.
> 

I ended up with:

diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst
index 8968ae3f55c0a..079db28bfc5b4 100644
--- a/Documentation/admin-guide/mm/ksm.rst
+++ b/Documentation/admin-guide/mm/ksm.rst
@@ -27,6 +27,9 @@ KSM's merged pages were originally locked into kernel memory, but can now
 be swapped out just like other user pages (but sharing is broken when they
 are swapped back in: ksmd must rediscover their identity and merge again).

+Note that KSM might not merge identical pages if each of them is already
+shared between processes as a result of fork().
+
 Controlling KSM with madvise
 ============================


As announced, I'll resend this series in a bit.

-- 
Cheers,

David

  reply	other threads:[~2026-12-04 12:44 UTC|newest]

Thread overview: 105+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 10:06 [PATCH 00/32] mm: various (urgent?) fixes, part 3 David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 02/32] mm/shmem: don't skip parts of a split swap entry when punching holes David Hildenbrand (Arm)
2026-12-02 11:46   ` Baolin Wang
2026-12-02 21:25   ` Gregory Price (Meta)
2026-11-29 10:06 ` [PATCH 03/32] mm/shmem: reject userfaultfd fills during hole punch David Hildenbrand (Arm)
2026-11-29 19:47   ` Jose A. Perez de Azpillaga
2026-12-01  9:45     ` David Hildenbrand (Arm)
2026-12-02 11:25     ` Baolin Wang
2026-12-01 13:49   ` Gregory Price (Meta)
2026-11-29 10:06 ` [PATCH 04/32] mm/shmem: fix commit limit bypass with very large shmem sizes David Hildenbrand (Arm)
2026-11-30 22:39   ` Gregory Price (Meta)
2026-12-02  8:31   ` Baolin Wang
2026-11-29 10:06 ` [PATCH 05/32] mm/shmem: don't pin inodes with eviction held off in the huge shrinker David Hildenbrand (Arm)
2026-12-02  8:47   ` Baolin Wang
2026-12-02 21:29   ` Gregory Price (Meta)
2026-11-29 10:06 ` [PATCH 06/32] mm/shmem: seal the size of SysV and shared anon files David Hildenbrand (Arm)
2026-11-30 22:41   ` Gregory Price (Meta)
2026-12-02  8:58   ` Baolin Wang
2026-12-02 13:31   ` Christian Brauner
2026-11-29 10:06 ` [PATCH 07/32] mm/khugepaged: check the VMA before walking page tables when collapsing David Hildenbrand (Arm)
2026-11-29 19:55   ` Jose A. Perez de Azpillaga
2026-11-30 16:31   ` Zi Yan
2026-12-02  5:18   ` Dev Jain
2026-12-02 15:23     ` David Hildenbrand (Arm)
2026-12-02  5:29   ` Gregory Price (Meta)
2026-12-02  9:07   ` Baolin Wang
2026-11-29 10:06 ` [PATCH 08/32] mm/khugepaged: flush file after unlocking folios in collapse_file() David Hildenbrand (Arm)
2026-11-29 19:58   ` Jose A. Perez de Azpillaga
2026-11-30 17:02   ` Zi Yan
2026-12-02  9:13   ` Baolin Wang
2026-12-02 21:32   ` Gregory Price (Meta)
2026-12-02 21:39     ` David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 09/32] mm/khugepaged: check for uffd outside the i_pages lock David Hildenbrand (Arm)
2026-11-29 20:07   ` Jose A. Perez de Azpillaga
2026-12-02  5:36   ` Gregory Price (Meta)
2026-11-29 10:06 ` [PATCH 10/32] mm/khugepaged: don't wait for userfaultfd David Hildenbrand (Arm)
2026-11-29 20:16   ` Jose A. Perez de Azpillaga
2026-12-01  9:51     ` David Hildenbrand (Arm)
2026-12-02 21:57   ` Gregory Price (Meta)
2026-11-29 10:06 ` [PATCH 12/32] mm/khugepaged: check PMD support with the folios locked David Hildenbrand (Arm)
2026-11-29 20:27   ` Jose A. Perez de Azpillaga
2026-11-30 17:13   ` Zi Yan
2026-12-02  9:42   ` Baolin Wang
2026-12-02 15:29     ` David Hildenbrand (Arm)
2026-12-02 21:41   ` Gregory Price (Meta)
2026-11-29 10:06 ` [PATCH 15/32] mm/huge_memory: don't map huge PFN entries if the arch restricts PFNs David Hildenbrand (Arm)
2026-11-29 20:41   ` Jose A. Perez de Azpillaga
2026-12-01  9:54     ` David Hildenbrand (Arm)
2026-12-01 12:02       ` Jose A. Perez de Azpillaga
2026-11-29 10:06 ` [PATCH 16/32] mm/memory: don't clear a PUD leaf as bad in a zap David Hildenbrand (Arm)
2026-11-29 20:47   ` Jose A. Perez de Azpillaga
2026-11-29 10:06 ` [PATCH 17/32] mm/memory: drop the swap device ref before do_wp_page() David Hildenbrand (Arm)
2026-11-29 20:50   ` Jose A. Perez de Azpillaga
2026-12-01 13:52   ` Gregory Price (Meta)
2026-11-29 10:06 ` [PATCH 19/32] mm/gup: check for fatal signals in memfd_pin_folios() David Hildenbrand (Arm)
2026-12-02  5:52   ` Gregory Price (Meta)
2026-11-29 10:06 ` [PATCH 20/32] mm/migrate_device: don't collect secretmem pages David Hildenbrand (Arm)
2026-12-01 13:54   ` Gregory Price (Meta)
2026-12-02  4:03   ` Alistair Popple
2026-12-02 15:32     ` David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 21/32] mm/migrate_device: mark folios leaving device memory dirty David Hildenbrand (Arm)
2026-12-01 13:55   ` Gregory Price (Meta)
2026-12-02  4:15   ` Alistair Popple
2026-12-02 15:34     ` David Hildenbrand (Arm)
2026-12-02 22:31       ` Alistair Popple
2026-12-02  9:38   ` Balbir Singh
2026-11-29 10:06 ` [PATCH 22/32] mm: taint device sections before setting subsections valid David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 24/32] mm/mempolicy: reject an empty set of allowed nodes David Hildenbrand (Arm)
2026-11-30 15:46   ` Joshua Hahn
2026-11-30 18:05   ` Gregory Price (Meta)
2026-12-02  5:55   ` Gregory Price (Meta)
2026-12-02 15:36     ` David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 25/32] mm/mempolicy: don't wait for mems_allowed_seq in kmalloc_nolock() David Hildenbrand (Arm)
2026-11-29 21:27   ` Jose A. Perez de Azpillaga
2026-11-30 15:55   ` Joshua Hahn
2026-12-01  9:40     ` David Hildenbrand (Arm)
2026-12-02 18:57       ` Joshua Hahn
2026-11-30 18:12   ` Gregory Price (Meta)
2026-12-01  9:39     ` David Hildenbrand (Arm)
2026-12-01 11:44       ` Vlastimil Babka (SUSE)
2026-12-01 11:58         ` David Hildenbrand (Arm)
2026-12-01 14:00           ` Gregory Price (Meta)
2026-12-01 15:02             ` David Hildenbrand (Arm)
2026-12-02  8:10             ` Vlastimil Babka (SUSE)
2026-12-01 13:13         ` Harry Yoo
2026-12-01 13:59           ` Gregory Price (Meta)
2026-11-29 10:06 ` [PATCH 26/32] mm/ksm: look up the folio again in cmp_and_merge_page() David Hildenbrand (Arm)
2026-12-02 21:50   ` Gregory Price (Meta)
2026-12-04  3:08   ` xu.xin
2026-12-04 12:04   ` Lorenzo Stoakes (ARM)
2026-11-29 10:06 ` [PATCH 27/32] mm/ksm: copy KSM folios at swap-in without VM_MERGEABLE David Hildenbrand (Arm)
2026-12-01 14:56   ` Gregory Price (Meta)
2026-12-01 15:01     ` David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 28/32] mm/ksm: don't convert a shared anon folio into a KSM folio David Hildenbrand (Arm)
2026-12-04  6:32   ` xu.xin
2026-12-04 11:06     ` David Hildenbrand (Arm)
2026-12-04 12:44       ` David Hildenbrand (Arm) [this message]
2026-11-29 10:07 ` [PATCH 30/32] mm/migrate: fix KSM oddity by checking expected refcount without mappings David Hildenbrand (Arm)
2026-12-02 17:00   ` Zi Yan
2026-12-02 18:41     ` David Hildenbrand (Arm)
2026-11-29 10:07 ` [PATCH 31/32] mm/migrate_device: don't collect KSM folios David Hildenbrand (Arm)
2026-12-02  9:43   ` Balbir Singh
2026-12-02 21:55   ` Gregory Price (Meta)
2026-12-02 22:47     ` Alistair Popple
2026-12-02 15:37 ` [PATCH 00/32] mm: various (urgent?) fixes, part 3 David Hildenbrand (Arm)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d39209f4-7a58-41d4-aa15-9871d58f91fd@kernel.org \
    --to=david@kernel.org \
    --cc=Felix.Kuehling@amd.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=airlied@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=alex.sierra@amd.com \
    --cc=alex@shazbot.org \
    --cc=apopple@nvidia.com \
    --cc=ast@kernel.org \
    --cc=axelrasmussen@google.com \
    --cc=balbirs@nvidia.com \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=baoquan.he@linux.dev \
    --cc=bigeasy@linutronix.de \
    --cc=byungchul@sk.com \
    --cc=chengming.zhou@linux.dev \
    --cc=cl@gentwo.org \
    --cc=clrkwllms@kernel.org \
    --cc=dave.jiang@intel.com \
    --cc=dave@stgolabs.net \
    --cc=dev.jain@arm.com \
    --cc=djbw@kernel.org \
    --cc=gorcunov@gmail.com \
    --cc=gourry@gourry.net \
    --cc=hagen@jauu.net \
    --cc=hao.li@linux.dev \
    --cc=harry@kernel.org \
    --cc=hughd@google.com \
    --cc=jack@suse.cz \
    --cc=jannh@google.com \
    --cc=jgg@ziepe.ca \
    --cc=jhubbard@nvidia.com \
    --cc=joshua.hahnjy@gmail.com \
    --cc=kas@kernel.org \
    --cc=kasong@tencent.com \
    --cc=kraxel@redhat.com \
    --cc=lance.yang@linux.dev \
    --cc=liam@infradead.org \
    --cc=linmiaohe@huawei.com \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=ljs@kernel.org \
    --cc=manfred@colorfullife.com \
    --cc=mason@kernel.org \
    --cc=matthew.brost@intel.com \
    --cc=mgorman@suse.de \
    --cc=mhocko@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=nico.pache@linux.dev \
    --cc=osalvador@suse.de \
    --cc=peterx@redhat.com \
    --cc=qi.zheng@linux.dev \
    --cc=rakie.kim@sk.com \
    --cc=riel@surriel.com \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=rostedt@goodmis.org \
    --cc=rppt@kernel.org \
    --cc=ryan.roberts@arm.com \
    --cc=shakeel.butt@linux.dev \
    --cc=shivankg@amd.com \
    --cc=shy828301@gmail.com \
    --cc=sj@kernel.org \
    --cc=song@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=stevensd@chromium.org \
    --cc=surenb@google.com \
    --cc=usama.arif@linux.dev \
    --cc=vbabka@kernel.org \
    --cc=vivek.kasireddy@intel.com \
    --cc=wangkefeng.wang@huawei.com \
    --cc=weixugc@google.com \
    --cc=willy@infradead.org \
    --cc=xu.xin@linux.dev \
    --cc=ying.huang@linux.alibaba.com \
    --cc=yuanchu@google.com \
    --cc=ziy@nvidia.com \
    --cc=zokeefe@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox