mm: ksm: prevent KSM from breaking merging of new VMAs
Get series with b4
b4 am [email protected]Versions newest first · linked by author and title
- v3 Latest4 patches 8 repliesIn review 3 of 4 applied
- v24 patches 14 repliesSuperseded
- v14 patches 26 repliesSuperseded
Changes in v3 from the cover letter
* 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.
Patches in v3 3 of 4 applied
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/4 | mm: ksm: have KSM VMA checks not require a VMA pointerinclude/ mm/ | +26 | 0 replies | Applied |
| 2/4 | mm: ksm: refer to special VMAs via VM_SPECIAL in ksm_compatible()mm/ | +2 | 0 replies | Applied |
| 3/4 | mm: prevent KSM from breaking VMA merging for new VMAsinclude/ mm/ mm/ +1 more | +70 | 7 replies | |
| 4/4 | tools/testing/selftests: add VMA merge tests for KSM mergetools/ | +78 | 1 reply | Applied |
Reviews trailers per patch
All 4 patches have a Reviewed-by or Acked-by.
| Patch | David Hildenbrand | Chengming Zhou | Vlastimil Babka | Xu Xin | Liam R. Howlett |
|---|---|---|---|---|---|
| 1/4 | A-b | R-b | R-b | R-b | R-b |
| 2/4 | A-b | R-b | R-b | R-b | R-b |
| 3/4 | A-b | R-b | R-b R-b | R-b | R-b |
| 4/4 | R-b T-b | A-b | R-b |
Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 5 reviewers.
Cover letter [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/[email protected]/
v1:
https://lore.kernel.org/all/[email protected]/
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