mm/ksm: move disabling KSM from s390/gmap code to KSM code
authorDavid Hildenbrand <david@redhat.com>
Sat, 22 Apr 2023 21:01:56 +0000 (23:01 +0200)
committerAndrew Morton <akpm@linux-foundation.org>
Wed, 3 May 2023 00:21:50 +0000 (17:21 -0700)
Let's factor out actual disabling of KSM.  The existing "mm->def_flags &=
~VM_MERGEABLE;" was essentially a NOP and can be dropped, because
def_flags should never include VM_MERGEABLE.  Note that we don't currently
prevent re-enabling KSM.

This should now be faster in case KSM was never enabled, because we only
conditionally iterate all VMAs.  Further, it certainly looks cleaner.

Link: https://lkml.kernel.org/r/20230422210156.33630-1-david@redhat.com
Signed-off-by: David Hildenbrand <david@redhat.com>
Acked-by: Janosch Frank <frankja@linux.ibm.com>
Acked-by: Stefan Roesch <shr@devkernel.io>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Claudio Imbrenda <imbrenda@linux.ibm.com>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Rik van Riel <riel@surriel.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
arch/s390/mm/gmap.c
include/linux/ksm.h
mm/ksm.c

index 0949811761e6ce7a9a1a46773399aa8fe4966b26..dfe905c7bd8e0bd0234cf24f7bc924834831cf07 100644 (file)
@@ -2585,30 +2585,12 @@ EXPORT_SYMBOL_GPL(s390_enable_sie);
 
 int gmap_mark_unmergeable(void)
 {
-       struct mm_struct *mm = current->mm;
-       struct vm_area_struct *vma;
-       unsigned long vm_flags;
-       int ret;
-       VMA_ITERATOR(vmi, mm, 0);
-
        /*
         * Make sure to disable KSM (if enabled for the whole process or
         * individual VMAs). Note that nothing currently hinders user space
         * from re-enabling it.
         */
-       clear_bit(MMF_VM_MERGE_ANY, &mm->flags);
-
-       for_each_vma(vmi, vma) {
-               /* Copy vm_flags to avoid partial modifications in ksm_madvise */
-               vm_flags = vma->vm_flags;
-               ret = ksm_madvise(vma, vma->vm_start, vma->vm_end,
-                                 MADV_UNMERGEABLE, &vm_flags);
-               if (ret)
-                       return ret;
-               vm_flags_reset(vma, vm_flags);
-       }
-       mm->def_flags &= ~VM_MERGEABLE;
-       return 0;
+       return ksm_disable(current->mm);
 }
 EXPORT_SYMBOL_GPL(gmap_mark_unmergeable);
 
index 429efa6ff4ae85b699857fb14b38f4ddb1494b6b..899a314bc48720ea7624c09827768be07536e75a 100644 (file)
@@ -22,6 +22,7 @@ int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
 void ksm_add_vma(struct vm_area_struct *vma);
 int ksm_enable_merge_any(struct mm_struct *mm);
 int ksm_disable_merge_any(struct mm_struct *mm);
+int ksm_disable(struct mm_struct *mm);
 
 int __ksm_enter(struct mm_struct *mm);
 void __ksm_exit(struct mm_struct *mm);
@@ -80,6 +81,11 @@ static inline void ksm_add_vma(struct vm_area_struct *vma)
 {
 }
 
+static inline int ksm_disable(struct mm_struct *mm)
+{
+       return 0;
+}
+
 static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
 {
        return 0;
index 823bb3475a680ee7c6238e9950ee3fe8c65370f5..0156bded3a66c5a29223aed5218477783668a674 100644 (file)
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -2628,6 +2628,17 @@ int ksm_disable_merge_any(struct mm_struct *mm)
        return 0;
 }
 
+int ksm_disable(struct mm_struct *mm)
+{
+       mmap_assert_write_locked(mm);
+
+       if (!test_bit(MMF_VM_MERGEABLE, &mm->flags))
+               return 0;
+       if (test_bit(MMF_VM_MERGE_ANY, &mm->flags))
+               return ksm_disable_merge_any(mm);
+       return ksm_del_vmas(mm);
+}
+
 int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
                unsigned long end, int advice, unsigned long *vm_flags)
 {