Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm/ksm: move disabling KSM from s390/gmap code to KSM code

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>

authored by

David Hildenbrand and committed by
Andrew Morton
2c281f54 1150ea93

+18 -19
+1 -19
arch/s390/mm/gmap.c
··· 2585 2585 2586 2586 int gmap_mark_unmergeable(void) 2587 2587 { 2588 - struct mm_struct *mm = current->mm; 2589 - struct vm_area_struct *vma; 2590 - unsigned long vm_flags; 2591 - int ret; 2592 - VMA_ITERATOR(vmi, mm, 0); 2593 - 2594 2588 /* 2595 2589 * Make sure to disable KSM (if enabled for the whole process or 2596 2590 * individual VMAs). Note that nothing currently hinders user space 2597 2591 * from re-enabling it. 2598 2592 */ 2599 - clear_bit(MMF_VM_MERGE_ANY, &mm->flags); 2600 - 2601 - for_each_vma(vmi, vma) { 2602 - /* Copy vm_flags to avoid partial modifications in ksm_madvise */ 2603 - vm_flags = vma->vm_flags; 2604 - ret = ksm_madvise(vma, vma->vm_start, vma->vm_end, 2605 - MADV_UNMERGEABLE, &vm_flags); 2606 - if (ret) 2607 - return ret; 2608 - vm_flags_reset(vma, vm_flags); 2609 - } 2610 - mm->def_flags &= ~VM_MERGEABLE; 2611 - return 0; 2593 + return ksm_disable(current->mm); 2612 2594 } 2613 2595 EXPORT_SYMBOL_GPL(gmap_mark_unmergeable); 2614 2596
+6
include/linux/ksm.h
··· 22 22 void ksm_add_vma(struct vm_area_struct *vma); 23 23 int ksm_enable_merge_any(struct mm_struct *mm); 24 24 int ksm_disable_merge_any(struct mm_struct *mm); 25 + int ksm_disable(struct mm_struct *mm); 25 26 26 27 int __ksm_enter(struct mm_struct *mm); 27 28 void __ksm_exit(struct mm_struct *mm); ··· 79 78 80 79 static inline void ksm_add_vma(struct vm_area_struct *vma) 81 80 { 81 + } 82 + 83 + static inline int ksm_disable(struct mm_struct *mm) 84 + { 85 + return 0; 82 86 } 83 87 84 88 static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
+11
mm/ksm.c
··· 2628 2628 return 0; 2629 2629 } 2630 2630 2631 + int ksm_disable(struct mm_struct *mm) 2632 + { 2633 + mmap_assert_write_locked(mm); 2634 + 2635 + if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) 2636 + return 0; 2637 + if (test_bit(MMF_VM_MERGE_ANY, &mm->flags)) 2638 + return ksm_disable_merge_any(mm); 2639 + return ksm_del_vmas(mm); 2640 + } 2641 + 2631 2642 int ksm_madvise(struct vm_area_struct *vma, unsigned long start, 2632 2643 unsigned long end, int advice, unsigned long *vm_flags) 2633 2644 {