Skip to content

Commit 2c281f5

Browse files
davidhildenbrandakpm00
authored andcommitted
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>
1 parent 1150ea9 commit 2c281f5

3 files changed

Lines changed: 18 additions & 19 deletions

File tree

arch/s390/mm/gmap.c

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,30 +2585,12 @@ EXPORT_SYMBOL_GPL(s390_enable_sie);
25852585

25862586
int gmap_mark_unmergeable(void)
25872587
{
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-
25942588
/*
25952589
* Make sure to disable KSM (if enabled for the whole process or
25962590
* individual VMAs). Note that nothing currently hinders user space
25972591
* from re-enabling it.
25982592
*/
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);
26122594
}
26132595
EXPORT_SYMBOL_GPL(gmap_mark_unmergeable);
26142596

include/linux/ksm.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
2222
void ksm_add_vma(struct vm_area_struct *vma);
2323
int ksm_enable_merge_any(struct mm_struct *mm);
2424
int ksm_disable_merge_any(struct mm_struct *mm);
25+
int ksm_disable(struct mm_struct *mm);
2526

2627
int __ksm_enter(struct mm_struct *mm);
2728
void __ksm_exit(struct mm_struct *mm);
@@ -80,6 +81,11 @@ static inline void ksm_add_vma(struct vm_area_struct *vma)
8081
{
8182
}
8283

84+
static inline int ksm_disable(struct mm_struct *mm)
85+
{
86+
return 0;
87+
}
88+
8389
static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
8490
{
8591
return 0;

mm/ksm.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,6 +2628,17 @@ int ksm_disable_merge_any(struct mm_struct *mm)
26282628
return 0;
26292629
}
26302630

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+
26312642
int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
26322643
unsigned long end, int advice, unsigned long *vm_flags)
26332644
{

0 commit comments

Comments
 (0)