[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <cover.1748537921.git.lorenzo.stoakes@oracle.com>
Date: Thu, 29 May 2025 18:15:44 +0100
From: Lorenzo Stoakes <lorenzo.stoakes@...cle.com>
To: Andrew Morton <akpm@...ux-foundation.org>
Cc: Alexander Viro <viro@...iv.linux.org.uk>,
Christian Brauner <brauner@...nel.org>, Jan Kara <jack@...e.cz>,
"Liam R . Howlett" <Liam.Howlett@...cle.com>,
Vlastimil Babka <vbabka@...e.cz>, Jann Horn <jannh@...gle.com>,
Pedro Falcato <pfalcato@...e.de>, David Hildenbrand <david@...hat.com>,
Xu Xin <xu.xin16@....com.cn>,
Chengming Zhou <chengming.zhou@...ux.dev>, linux-mm@...ck.org,
linux-kernel@...r.kernel.org, linux-fsdevel@...r.kernel.org,
Stefan Roesch <shr@...kernel.io>
Subject: [PATCH v3 0/4] mm: ksm: prevent KSM from breaking merging of new VMAs
When KSM-by-default is established using prctl(PR_SET_MEMORY_MERGE), this
defaults all newly mapped VMAs to having VM_MERGEABLE set, and thus makes
them available to KSM for samepage merging. It also sets VM_MERGEABLE in
all existing VMAs.
However this causes an issue upon mapping of new VMAs - the initial flags
will never have VM_MERGEABLE set when attempting a merge with adjacent VMAs
(this is set later in the mmap() logic), and adjacent VMAs will ALWAYS have
VM_MERGEABLE set.
This renders all newly mapped VMAs unmergeable.
To avoid this, this series performs the check for PR_SET_MEMORY_MERGE far
earlier in the mmap() logic, prior to the merge being attempted.
However we run into complexity with the depreciated .mmap() callback - if a
driver hooks this, it might change flags which adjust KSM merge
eligibility.
We have to worry about this because, while KSM is only applicable to
private mappings, this includes both anonymous and MAP_PRIVATE-mapped
file-backed mappings.
This isn't a problem for brk(), where the VMA must be anonymous. However in
mmap() we must be conservative - if the VMA is anonymous then we can always
proceed, however if not, we permit only shmem mappings (whose .mmap hook
does not affect KSM eligibility) and drivers which implement
.mmap_prepare() (invoked prior to the KSM eligibility check).
If we can't be sure of the driver changing things, then we maintain the
same behaviour of performing the KSM check later in the mmap() logic (and
thus losing new VMA mergeability).
A great many use-cases for this logic will use anonymous mappings any rate,
so this change should already cover the majority of actual KSM use-cases.
v3:
* Propagated tags (thanks everyone!)
* Updated cover letter to be explicit that we're fixing merging for new
VMAs only as per Vlastimil, and further cleaned it up for clarity.
* Corrected reference to shmem being permitted by KSM in 3/4 commit message
as per Xu.
* Clarified reference to MAP_SHARED vs MAP_PRIVATE file-backed mappings in
3/4 commit message as per Vlastimil.
* Updated comment around mmap_prepare check in can_set_ksm_flags_early() as
per Xu.
* Reduced can_set_ksm_flags_early() comment to avoid confusion - the
overloaded use of 'mergeable' is confusing (as raised by Vlastimil) and
the first paragraph is sufficient.
* Rearranged comments and code ordering in can_set_ksm_flags_early() for
clarity based on discussion with Vlastimil.
* Reduced and improved commit 3/4 commit message further for clarity.
* Added missing stubs to VMA userland tests.
v2:
* Removed unnecessary ret local variable in ksm_vma_flags() as per David.
* added Stefan Roesch in cc and added Fixes tag as per Andrew, David.
* Propagated tags (thanks everyone!)
* Removed unnecessary !CONFIG_KSM ksm_add_vma() stub from
include/linux/ksm.h.
* Added missing !CONFIG_KSM ksm_vma_flags() stub in
include/linux/ksm.h.
* After discussion with David, I've decided to defer removing the
VM_SPECIAL case for KSM, we can address this in a follow-up series.
* Expanded 3/4 commit message to reference KSM eligibility vs. merging and
referenced future plans to permit KSM for VM_SPECIAL VMAs.
https://lore.kernel.org/all/cover.1747844463.git.lorenzo.stoakes@oracle.com/
v1:
https://lore.kernel.org/all/cover.1747431920.git.lorenzo.stoakes@oracle.com/
Lorenzo Stoakes (4):
mm: ksm: have KSM VMA checks not require a VMA pointer
mm: ksm: refer to special VMAs via VM_SPECIAL in ksm_compatible()
mm: prevent KSM from breaking VMA merging for new VMAs
tools/testing/selftests: add VMA merge tests for KSM merge
include/linux/fs.h | 7 ++-
include/linux/ksm.h | 8 +--
mm/ksm.c | 49 ++++++++++++-------
mm/vma.c | 44 ++++++++++++++++-
tools/testing/selftests/mm/merge.c | 78 ++++++++++++++++++++++++++++++
tools/testing/vma/vma_internal.h | 11 +++++
6 files changed, 173 insertions(+), 24 deletions(-)
--
2.49.0
Powered by blists - more mailing lists