[RESEND RFC PATCH v1 05/20] mm: support disabling of ksm for a process

From: Stefan Roesch
Date: Mon Jan 23 2023 - 12:59:10 EST


This adds the ability to disable ksm for a process if ksm has been
enabled for the process.

Signed-off-by: Stefan Roesch <shr@xxxxxxxxxxxx>
---
mm/ksm.c | 25 ++++++++++++++++++++++++-
1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/mm/ksm.c b/mm/ksm.c
index 967eda719fab..5fa6b46dfa3b 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -2588,6 +2588,27 @@ int __ksm_enter(struct mm_struct *mm, int flag)
return 0;
}

+static void unmerge_vmas(struct mm_struct *mm)
+{
+ struct vm_area_struct *vma;
+ struct vma_iterator vmi;
+
+ vma_iter_init(&vmi, mm, 0);
+
+ mmap_read_lock(mm);
+ for_each_vma(vmi, vma) {
+ if (vma->vm_flags & VM_MERGEABLE) {
+ unsigned long flags = vma->vm_flags;
+
+ if (ksm_madvise(vma, vma->vm_start, vma->vm_end, MADV_UNMERGEABLE, &flags))
+ continue;
+
+ vma->vm_flags = flags;
+ }
+ }
+ mmap_read_unlock(mm);
+}
+
void __ksm_exit(struct mm_struct *mm, int flag)
{
struct ksm_mm_slot *mm_slot;
@@ -2595,8 +2616,10 @@ void __ksm_exit(struct mm_struct *mm, int flag)
int easy_to_free = 0;

if (!(current->flags & PF_EXITING) && flag == MMF_VM_MERGE_ANY &&
- test_bit(MMF_VM_MERGE_ANY, &mm->flags))
+ test_bit(MMF_VM_MERGE_ANY, &mm->flags)) {
clear_bit(MMF_VM_MERGE_ANY, &mm->flags);
+ unmerge_vmas(mm);
+ }

/*
* This process is exiting: if it's straightforward (as is the
--
2.30.2