mm: process/cgroup ksm support
Subsystems (9)
v9 cover mm: process/cgroup ksm support
So far KSM can only be enabled by calling madvise for memory regions. To be able to use KSM for more workloads, KSM needs to have the ability to be enabled / disabled at the process / cgroup level.
Use case 1:
The madvise call is not available in the programming language. An example for this are programs with forked workloads using a garbage collected language without pointers. In such a language madvise cannot be made available.
In addition the addresses of objects get moved around as they are garbage collected. KSM sharing needs to be enabled "from the outside" for these type of workloads.
Use case 2:
The same interpreter can also be used for workloads where KSM brings no benefit or even has overhead. We'd like to be able to enable KSM on a workload by workload basis.
Use case 3:
With the madvise call sharing opportunities are only enabled for the current
process: it is a workload-local decision. A considerable number of sharing opportunities may exist across multiple workloads or jobs (if they are part of the same security domain). Only a higler level entity like a job scheduler or container can know for certain if its running one or more instances of a job. That job scheduler however doesn't have the necessary internal workload knowledge to make targeted madvise calls.
Security concerns:
In previous discussions security concerns have been brought up. The problem is that an individual workload does not have the knowledge about what else is running on a machine. Therefore it has to be very conservative in what memory areas can be shared or not. However, if the system is dedicated to running multiple jobs within the same security domain, its the job scheduler that has the knowledge that sharing can be safely enabled and is even desirable.
Performance:
Experiments with using UKSM have shown a capacity increase of around 20%.
Here are the metrics from an instagram workload (taken from a machine with 64GB main memory):
full_scans: 445
general_profit: 20158298048
max_page_sharing: 256
merge_across_nodes: 1
pages_shared: 129547
pages_sharing: 5119146
pages_to_scan: 4000
pages_unshared: 1760924
pages_volatile: 10761341
run: 1
sleep_millisecs: 20
stable_node_chains: 167
stable_node_chains_prune_millisecs: 2000
stable_node_dups: 2751
use_zero_pages: 0
zero_pages_sharing: 0
After the service is running for 30 minutes to an hour, 4 to 5 million shared pages are common for this workload when using KSM.
Detailed changes:
1. New options for prctl system command
This patch series adds two new options to the prctl system call. The first one allows to enable KSM at the process level and the second one to query the setting.
The setting will be inherited by child processes.
With the above setting, KSM can be enabled for the seed process of a cgroup and all processes in the cgroup will inherit the setting.
2. Changes to KSM processing
When KSM is enabled at the process level, the KSM code will iterate over all the VMA's and enable KSM for the eligible VMA's.
When forking a process that has KSM enabled, the setting will be inherited by the new child process.
3. Add general_profit metric
The general_profit metric of KSM is specified in the documentation, but not calculated. This adds the general profit metric to /sys/kernel/debug/mm/ksm.
4. Add more metrics to ksm_stat
This adds the process profit metric to /proc/<pid>/ksm_stat.
5. Add more tests to ksm_tests and ksm_functional_tests
This adds an option to specify the merge type to the ksm_tests. This allows to test madvise and prctl KSM.
It also adds a two new tests to ksm_functional_tests: one to test the new prctl options and the other one is a fork test to verify that the KSM process setting is inherited by client processes.
Changes:
- V9:
- simplify __ksm_add_vma()
- Adding return to test_prctl() in ksm_functional_tests.c
- Removing assignment in defining debug variable in ksm_tests.c
- V8:
- Refreshed to latest mm-unstable
- Added check for arg3 - arg5 in prctl function
- Don't return an error ksm_enable_merge_any if MMF_VM_MERGE_ANY bit is already set
- unmap after merge in ksm_tests program
- use tmp variable in main function in ksm_tests program
- Specify all 5 parameters in call to prctl in test programs
- Rename test_ksm_prctl() to test_prctl()
- Skip if first test in test_prctl failed with skip result
- Exit early for failures in test_prctl
- Rename test_ksm_fork() to test_prctl_fork()
- Similar changes to test_prctl also for test_prctl_fork()
- Change number of test
- V7:
- Removed ksm_add_mm() function
- added ksm_enable_merge_any() function
- Made ksm_add_vmas() function static
- Simplified ksm_fork function to only MMF_VM_MERGE_ANY bit
- Moved setting of bit MMF_VM_MERGE_ANY to ksm_enable_merge_any()
- Removed flag parameter from __ksm_enter
- Removed flag parameter from __ksm_exit
- Clear bit MMF_VM_MERGE_ANY in __ksm_exit
- call ksm_add_vma only in mmap_region() and do_brk_flags()
- Removed check_ksm_fork() and check_ksm_merge_type from ksm_tests
- Removed -F and -G command line options
- Removed enum options for above tests
- Added -d option to enable debug mode
- Added debug variable for storing debug option
- V6:
- Fix error condition in prctl call
- Remove ksm_merge_type function and ksm_stat output
- Some minor changes like whitespace and removing a cast.
- V5:
- When the prctl system call is invoked, mark all compatible VMA as mergeable
- Instead of checcking during scan if VMA is mergeable, mark the VMA mergeable when the VMA is created (in case the VMA is compatible)
- Remove earlier changes, they are no longer necessary
- Unset the flag MMF_VM_MERGE_ANY in gmap_mark_unmergeable().
- When unsetting the MMF_VM_MERGE_ANY flag with prctl, only unset the flag
- Remove pages_volatile function (with the simplar general_profit calculation, the function is no longer needed)
- Use simpler formula for calculation of general_profit
- V4:
- removing check in prctl for MMF_VM_MERGEABLE in PR_SET_MEMORY_MERGE handling
- Checking for VM_MERGEABLE AND MMF_VM_MERGE_ANY to avoid chaning vm_flags
- This requires also checking that the vma is compatible. The compatibility check is provided by a new helper
- processes which have set MMF_VM_MERGE_ANY, only need to call the helper and not madvise.
- removed unmerge_vmas function, this function is no longer necessary, clearing the MMF_VM_MERGE_ANY bit is sufficient
- V3:
- folded patch 1 - 6
- folded patch 7 - 14
- folded patch 15 - 19
- Expanded on the use cases in the cover letter
- Added a section on security concerns to the cover letter
- V2:
- Added use cases to the cover letter
- Removed the tracing patch from the patch series and posted it as an individual patch
- Refreshed repo
Stefan Roesch (3):
mm: add new api to enable ksm per process
mm: add new KSM process and sysfs knobs
selftests/mm: add new selftests for KSM
Documentation/ABI/testing/sysfs-kernel-mm-ksm | 8 + Documentation/admin-guide/mm/ksm.rst | 5 +- arch/s390/mm/gmap.c | 7 + fs/proc/base.c | 3 + include/linux/ksm.h | 25 ++- include/linux/sched/coredump.h | 1 + include/uapi/linux/prctl.h | 2 + kernel/sys.c | 27 +++ mm/ksm.c | 125 +++++++++++-- mm/mmap.c | 3 + tools/include/uapi/linux/prctl.h | 2 + tools/testing/selftests/mm/Makefile | 2 +- .../selftests/mm/ksm_functional_tests.c | 91 ++++++++- tools/testing/selftests/mm/ksm_tests.c | 172 ++++++++++++++---- 14 files changed, 414 insertions(+), 59 deletions(-)
base-commit: f80a6c7a37be043f7b074d1e19638675315e3566
Signature · 1 line
2.31.1
PATCH v9 1/3 mm: add new api to enable ksm per process
So far KSM can only be enabled by calling madvise for memory regions. To be able to use KSM for more workloads, KSM needs to have the ability to be enabled / disabled at the process / cgroup level.
1. New options for prctl system command
This patch series adds two new options to the prctl system call. The first one allows to enable KSM at the process level and the second one to query the setting.
The setting will be inherited by child processes.
With the above setting, KSM can be enabled for the seed process of a cgroup and all processes in the cgroup will inherit the setting.
2. Changes to KSM processing
When KSM is enabled at the process level, the KSM code will iterate over all the VMA's and enable KSM for the eligible VMA's.
When forking a process that has KSM enabled, the setting will be inherited by the new child process.
1) Introduce new MMF_VM_MERGE_ANY flag
This introduces the new flag MMF_VM_MERGE_ANY flag. When this flag is set, kernel samepage merging (ksm) gets enabled for all vma's of a process.
2) Setting VM_MERGEABLE on VMA creation
When a VMA is created, if the MMF_VM_MERGE_ANY flag is set, the VM_MERGEABLE flag will be set for this VMA.
3) support disabling of ksm for a process
This adds the ability to disable ksm for a process if ksm has been enabled for the process with prctl.
4) add new prctl option to get and set ksm for a process
This adds two new options to the prctl system call
- enable ksm for all vmas of a process (if the vmas support it).
- query if ksm has been enabled for a process.
3. Disabling MMF_VM_MERGE_ANY for storage keys in s390
In the s390 architecture when storage keys are used, the MMF_VM_MERGE_ANY will be disabled.
--- arch/s390/mm/gmap.c | 7 +++ include/linux/ksm.h | 21 ++++++- include/linux/sched/coredump.h | 1 + include/uapi/linux/prctl.h | 2 + kernel/sys.c | 27 +++++++++ mm/ksm.c | 104 +++++++++++++++++++++++++++------ mm/mmap.c | 3 + 7 files changed, 146 insertions(+), 19 deletions(-)
arch/s390/mm/gmap.c +7 -0
@@ -2591,6 +2591,13 @@ int gmap_mark_unmergeable(void)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;
include/linux/ksm.h +19 -2
@@ -18,13 +18,26 @@#ifdef CONFIG_KSM int ksm_madvise (struct vm_area_struct *vma,unsigned long start,unsigned long end,int advice,unsigned long *vm_flags); + +void ksm_add_vma (struct vm_area_struct *vma); +int ksm_enable_merge_any (struct mm_struct *mm); +int __ksm_enter (struct mm_struct *mm);void __ksm_exit (struct mm_struct *mm);static inline int ksm_fork (struct mm_struct *mm,struct mm_struct *oldmm) { -if (test_bit (MMF_VM_MERGEABLE , &oldmm->flags)) -return __ksm_enter (mm); +int ret; + +if (test_bit (MMF_VM_MERGEABLE , &oldmm->flags)) { + ret =__ksm_enter (mm); +if (ret) +return ret; + } + +if (test_bit (MMF_VM_MERGE_ANY , &oldmm->flags)) +set_bit (MMF_VM_MERGE_ANY , &mm->flags); +return 0 ; }
@@ -53,6 +66,10 @@ void folio_migrate_ksm(struct folio *newfolio, struct folio *folio);#else /* !CONFIG_KSM */ +static inline void ksm_add_vma (struct vm_area_struct *vma) +{ +} +static inline int ksm_fork (struct mm_struct *mm,struct mm_struct *oldmm) {return 0 ;
include/linux/sched/coredump.h +1 -0
@@ -90,4 +90,5 @@ static inline int get_dumpable(struct mm_struct *mm)#define MMF_INIT_MASK (MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK |\ MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK) +#define MMF_VM_MERGE_ANY 29#endif /* _LINUX_SCHED_COREDUMP_H */
include/uapi/linux/prctl.h +2 -0
@@ -292,4 +292,6 @@ struct prctl_mm_map {#define PR_GET_AUXV 0x41555856 +#define PR_SET_MEMORY_MERGE 67 +#define PR_GET_MEMORY_MERGE 68#endif /* _LINUX_PRCTL_H */
kernel/sys.c +27 -0
@@ -15,6 +15,7 @@#include <linux/highuid.h> #include <linux/fs.h> #include <linux/kmod.h> +#include <linux/ksm.h> #include <linux/perf_event.h> #include <linux/resource.h> #include <linux/kernel.h>
@@ -2687,6 +2688,32 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,case PR_SET_VMA : error =prctl_set_vma (arg2, arg3, arg4, arg5);break ; +#ifdef CONFIG_KSM +case PR_SET_MEMORY_MERGE : +if (arg3 || arg4 || arg5) +return -EINVAL ; +if (mmap_write_lock_killable (me->mm)) +return -EINTR ; + +if (arg2) { + error =ksm_enable_merge_any (me->mm); + }else { +/* +* TODO: we might want disable KSM on all VMAs and +* trigger unsharing to completely disable KSM. +*/ +clear_bit (MMF_VM_MERGE_ANY , &me->mm->flags); + error =0 ; + } +mmap_write_unlock (me->mm); +break ; +case PR_GET_MEMORY_MERGE : +if (arg2 || arg3 || arg4 || arg5) +return -EINVAL ; + + error = !!test_bit (MMF_VM_MERGE_ANY , &me->mm->flags); +break ; +#endif default : error = -EINVAL ;break ;
mm/ksm.c +87 -17
142 lines not on this page · raw message
mm/mmap.c +3 -0
@@ -46,6 +46,7 @@#include <linux/pkeys.h> #include <linux/oom.h> #include <linux/sched/mm.h> +#include <linux/ksm.h> #include <linux/uaccess.h> #include <asm/cacheflush.h>
@@ -2691,6 +2692,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,if (file && vm_flags &VM_SHARED )mapping_unmap_writable (file->f_mapping); file = vma->vm_file; +ksm_add_vma (vma); expanded:perf_event_mmap (vma);
@@ -2963,6 +2965,7 @@ static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, goto mas_store_fail; mm->map_count++; +ksm_add_vma (vma); out:perf_event_mmap (vma); mm->total_vm += len >>PAGE_SHIFT ;
Signature · 1 line
2.31.1
PATCH v9 2/3 mm: add new KSM process and sysfs knobs
In reply to Stefan RoeschThis adds the general_profit KSM sysfs knob and the process profit metric knobs to ksm_stat.
1) expose general_profit metric
The documentation mentions a general profit metric, however this metric is not calculated. In addition the formula depends on the size of internal structures, which makes it more difficult for an administrator to make the calculation. Adding the metric for a better user experience.
2) document general_profit sysfs knob
3) calculate ksm process profit metric
The ksm documentation mentions the process profit metric and how to calculate it. This adds the calculation of the metric.
4) mm: expose ksm process profit metric in ksm_stat
This exposes the ksm process profit metric in /proc/<pid>/ksm_stat. The documentation mentions the formula for the ksm process profit metric, however it does not calculate it. In addition the formula depends on the size of internal structures. So it makes sense to expose it.
5) document new procfs ksm knobs
--- Documentation/ABI/testing/sysfs-kernel-mm-ksm | 8 +++++++ Documentation/admin-guide/mm/ksm.rst | 5 ++++- fs/proc/base.c | 3 +++ include/linux/ksm.h | 4 ++++ mm/ksm.c | 21 +++++++++++++++++++ 5 files changed, 40 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-kernel-mm-ksm +8 -0
@@ -51,3 +51,11 @@ Description: Control merging pages across different NUMA nodes. When it is set to 0 only pages from the same node are merged, otherwise pages from all nodes can be merged together (default). + +What: /sys/kernel/mm/ksm/general_profit +Date: April 2023 +KernelVersion: 6.4 +Contact: Linux memory management mailing list <[email protected]> +Description: Measure how effective KSM is. + general_profit: how effective is KSM. The formula for the + calculation is in Documentation/admin-guide/mm/ksm.rst.
Documentation/admin-guide/mm/ksm.rst +4 -1
@@ -157,6 +157,8 @@ stable_node_chains_prune_millisecs The effectiveness of KSM and MADV_MERGEABLE is shown in ``/sys/kernel/mm/ksm/``: +general_profit + how effective is KSM. The calculation is explained below. pages_shared how many shared pages are being used pages_sharing
@@ -207,7 +209,8 @@ several times, which are unprofitable memory consumed. ksm_rmap_items * sizeof(rmap_item). where ksm_merging_pages is shown under the directory ``/proc/<pid>/``, - and ksm_rmap_items is shown in ``/proc/<pid>/ksm_stat``. + and ksm_rmap_items is shown in ``/proc/<pid>/ksm_stat``. The process profit + is also shown in ``/proc/<pid>/ksm_stat`` as ksm_process_profit. From the perspective of application, a high ratio of ``ksm_rmap_items`` to ``ksm_merging_pages`` means a bad madvise-applied policy, so developers or
fs/proc/base.c +3 -0
@@ -96,6 +96,7 @@#include <linux/time_namespace.h> #include <linux/resctrl.h> #include <linux/cn_proc.h> +#include <linux/ksm.h> #include <trace/events/oom.h> #include "internal.h" #include "fd.h"
@@ -3207,6 +3208,8 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns, mm =get_task_mm (task);if (mm) {seq_printf (m,"ksm_rmap_items %lu\n" , mm->ksm_rmap_items); +seq_printf (m,"ksm_merging_pages %lu\n" , mm->ksm_merging_pages); +seq_printf (m,"ksm_process_profit %ld\n" ,ksm_process_profit (mm));mmput (mm); }
include/linux/ksm.h +4 -0
@@ -64,6 +64,10 @@ struct page *ksm_might_need_to_copy(struct page *page,void rmap_walk_ksm (struct folio *folio,struct rmap_walk_control *rwc);void folio_migrate_ksm (struct folio *newfolio,struct folio *folio); +#ifdef CONFIG_PROC_FS +long ksm_process_profit (struct mm_struct *); +#endif /* CONFIG_PROC_FS */ +#else /* !CONFIG_KSM */ static inline void ksm_add_vma (struct vm_area_struct *vma)
mm/ksm.c +21 -0
@@ -2962,6 +2962,14 @@ static void wait_while_offlining(void) }#endif /* CONFIG_MEMORY_HOTREMOVE */ +#ifdef CONFIG_PROC_FS +long ksm_process_profit (struct mm_struct *mm) +{ +return mm->ksm_merging_pages *PAGE_SIZE - + mm->ksm_rmap_items *sizeof (struct ksm_rmap_item ); +} +#endif /* CONFIG_PROC_FS */ +#ifdef CONFIG_SYSFS /* * This all compiles without CONFIG_SYSFS, but is a waste of space.
@@ -3226,6 +3234,18 @@ static ssize_t pages_volatile_show(struct kobject *kobj, }KSM_ATTR_RO (pages_volatile); +static ssize_t general_profit_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +long general_profit; + + general_profit = ksm_pages_sharing *PAGE_SIZE - + ksm_rmap_items *sizeof (struct ksm_rmap_item ); + +return sysfs_emit (buf,"%ld\n" , general_profit); +} +KSM_ATTR_RO (general_profit); +static ssize_t stable_node_dups_show (struct kobject *kobj,struct kobj_attribute *attr,char *buf) {
@@ -3290,6 +3310,7 @@ static struct attribute *ksm_attrs[] = { &stable_node_dups_attr.attr, &stable_node_chains_prune_millisecs_attr.attr, &use_zero_pages_attr.attr, + &general_profit_attr.attr, NULL, };
Signature · 1 line
2.31.1
PATCH v9 2/3 mm: add new KSM process and sysfs knobs
Quoted 18 lines
This adds the
general_profitKSM sysfs knob and the process profit metric knobs toksm_stat.1) expose
general_profitmetricThe documentation mentions a general profit metric, however this metric is not calculated. In addition the formula depends on the size of internal structures, which makes it more difficult for an administrator to make the calculation. Adding the metric for a better user experience.
2) document
general_profitsysfs knob3) calculate ksm process profit metric
The ksm documentation mentions the process profit metric and how to calculate it. This adds the calculation of the metric.
Quoted 8 lines
4) mm: expose ksm process profit metric in
ksm_statThis exposes the ksm process profit metric in /proc/<pid>/ksm_stat. The documentation mentions the formula for the ksm process profit metric, however it does not calculate it. In addition the formula depends on the size of internal structures. So it makes sense to expose it.
Hi, Stefan, I think you should give some credits to me about my contributions on the concept and formula of ksm profit (process wide and system wide), it's kind of idea stealing.
Besides, the idea of Process control KSM was proposed by me last year although you use prctl instead of /proc fs. you even didn't CC my email. I think you should CC my email ([email protected]) as least.
Quoted 35 lines
5) document new procfs ksm knobs
Signed-off-by Stefan Roesch <[email protected]>Reviewed-by Bagas Sanjaya <[email protected]>Cc David Hildenbrand <[email protected]>Cc Johannes Weiner <[email protected]>Cc Michal Hocko <[email protected]>Cc Rik van Riel <[email protected]>Signed-off-by Andrew Morton <[email protected]>--- Documentation/ABI/testing/sysfs-kernel-mm-ksm | 8 +++++++ Documentation/admin-guide/mm/ksm.rst | 5 ++++- fs/proc/base.c | 3 +++ include/linux/ksm.h | 4 ++++ mm/ksm.c | 21 +++++++++++++++++++ 5 files changed, 40 insertions(+), 1 deletion(-)Documentation/ABI/testing/sysfs-kernel-mm-ksm +8
-0@@ -51,3 +51,11 @@ Description: Control merging pages across different NUMA nodes. When it is set to 0 only pages from the same node are merged, otherwise pages from all nodes can be merged together (default). + +What: /sys/kernel/mm/ksm/general_profit +Date: April 2023 +KernelVersion: 6.4 +Contact: Linux memory management mailing list <[email protected]> +Description: Measure how effective KSM is. + general_profit: how effective is KSM. The formula for the + calculation is in Documentation/admin-guide/mm/ksm.rst.Documentation/admin-guide/mm/ksm.rst +0
-0
Re: [PATCH v9 2/3] mm: add new KSM process and sysfs knobs
Quoted 27 lines · xu xin
Quoted 18 lines
This adds the
general_profitKSM sysfs knob and the process profit metric knobs toksm_stat.1) expose
general_profitmetricThe documentation mentions a general profit metric, however this metric is not calculated. In addition the formula depends on the size of internal structures, which makes it more difficult for an administrator to make the calculation. Adding the metric for a better user experience.
2) document
general_profitsysfs knob3) calculate ksm process profit metric
The ksm documentation mentions the process profit metric and how to calculate it. This adds the calculation of the metric.
Quoted 8 lines
4) mm: expose ksm process profit metric in
ksm_statThis exposes the ksm process profit metric in /proc/<pid>/ksm_stat. The documentation mentions the formula for the ksm process profit metric, however it does not calculate it. In addition the formula depends on the size of internal structures. So it makes sense to expose it.
Quoted 5 lines
Hi, Stefan, I think you should give some credits to me about my contributions on the concept and formula of ksm profit (process wide and system wide), it's kind of idea stealing.
I wasn't aware the formula that is described in the documentation is from you. I simply implemented the documented metrics. If you came up with it, happy to give you credit.
Quoted 39 lines
Besides, the idea of Process control KSM was proposed by me last year although you use prctl instead of /proc fs. you even didn't CC my email. I think you should CC my email ([email protected]) as least.
Quoted 35 lines
5) document new procfs ksm knobs
Signed-off-by Stefan Roesch <[email protected]>Reviewed-by Bagas Sanjaya <[email protected]>Cc David Hildenbrand <[email protected]>Cc Johannes Weiner <[email protected]>Cc Michal Hocko <[email protected]>Cc Rik van Riel <[email protected]>Signed-off-by Andrew Morton <[email protected]>--- Documentation/ABI/testing/sysfs-kernel-mm-ksm | 8 +++++++ Documentation/admin-guide/mm/ksm.rst | 5 ++++- fs/proc/base.c | 3 +++ include/linux/ksm.h | 4 ++++ mm/ksm.c | 21 +++++++++++++++++++ 5 files changed, 40 insertions(+), 1 deletion(-)Documentation/ABI/testing/sysfs-kernel-mm-ksm +8
-0@@ -51,3 +51,11 @@ Description: Control merging pages across different NUMA nodes. When it is set to 0 only pages from the same node are merged, otherwise pages from all nodes can be merged together (default). + +What: /sys/kernel/mm/ksm/general_profit +Date: April 2023 +KernelVersion: 6.4 +Contact: Linux memory management mailing list <[email protected]> +Description: Measure how effective KSM is. + general_profit: how effective is KSM. The formula for the + calculation is in Documentation/admin-guide/mm/ksm.rst.Documentation/admin-guide/mm/ksm.rst +0
-0
PATCH v9 3/3 selftests/mm: add new selftests for KSM
In reply to Stefan RoeschThis adds three new tests to the selftests for KSM. These tests use the new prctl API's to enable and disable KSM.
1) add new prctl flags to prctl header file in tools dir
This adds the new prctl flags to the include file prct.h in the tools directory. This makes sure they are available for testing.
2) add KSM prctl merge test to ksm_tests
This adds the -t option to the ksm_tests program. The -t flag allows to specify if it should use madvise or prctl ksm merging.
3) add two functions for debugging merge outcome for ksm_tests
This adds two functions to report the metrics in /proc/self/ksm_stat and /sys/kernel/debug/mm/ksm. The debug output is enabled with the -d option.
4) add KSM prctl test to ksm_functional_tests
This adds a test to the ksm_functional_test that verifies that the prctl system call to enable / disable KSM works.
5) add KSM fork test to ksm_functional_test
Add fork test to verify that the MMF_VM_MERGE_ANY flag is inherited by the child process.
--- tools/include/uapi/linux/prctl.h | 2 + tools/testing/selftests/mm/Makefile | 2 +- .../selftests/mm/ksm_functional_tests.c | 91 ++++++++- tools/testing/selftests/mm/ksm_tests.c | 172 ++++++++++++++---- 4 files changed, 228 insertions(+), 39 deletions(-)
tools/include/uapi/linux/prctl.h +2 -0
@@ -290,4 +290,6 @@ struct prctl_mm_map {#define PR_SET_VMA 0x53564d41# define PR_SET_VMA_ANON_NAME 0 +#define PR_SET_MEMORY_MERGE 67 +#define PR_GET_MEMORY_MERGE 68#endif /* _LINUX_PRCTL_H */
tools/testing/selftests/mm/Makefile +1 -1
@@ -29,7 +29,7 @@ MACHINE ?= $(shell echo $(uname_M) | sed -e 's/aarch64.*/arm64/' -e 's/ppc64.*/p # LDLIBS. MAKEFLAGS += --no-builtin-rules -CFLAGS = -Wall -I $(top_srcdir) $(EXTRA_CFLAGS) $(KHDR_INCLUDES) +CFLAGS = -Wall -I $(top_srcdir) -I $(top_srcdir)/tools/include/uapi $(EXTRA_CFLAGS) $(KHDR_INCLUDES) LDLIBS = -lrt -lpthread TEST_GEN_PROGS = cow
tools/testing/selftests/mm/ksm_functional_tests.c +90 -1
113 lines not on this page · raw message
tools/testing/selftests/mm/ksm_tests.c +135 -37
386 lines not on this page · raw message
Signature · 1 line
2.31.1
cover mm/ksm: improve PR_SET_MEMORY_MERGE=0 handling and cleanup disabling KSM
In reply to Stefan RoeschThis is a follow-up to [1]:
[PATCH v9 0/3] mm: process/cgroup ksm support
which is not in mm-unstable yet (but soon? :) ). I'll be on vacation for ~2 weeks, so sending it out now as reply to [1].
(1) Make PR_SET_MEMORY_MERGE=0 unmerge pages like setting MADV_UNMERGEABLE does, (2) add a selftest for it and (3) factor out disabling of KSM from s390/gmap code.
[1] https://lkml.kernel.org/r/[email protected]
David Hildenbrand (3):
mm/ksm: unmerge and clear VM_MERGEABLE when setting
PR_SET_MEMORY_MERGE=0
selftests/ksm: ksm_functional_tests: add prctl unmerge test mm/ksm: move disabling KSM from s390/gmap code to KSM code
arch/s390/mm/gmap.c | 20 +------ include/linux/ksm.h | 7 +++ kernel/sys.c | 7 +-- mm/ksm.c | 58 +++++++++++++++++++ .../selftests/mm/ksm_functional_tests.c | 46 +++++++++++++-- 5 files changed, 107 insertions(+), 31 deletions(-)
Signature · 1 line
2.39.2
PATCH 1/3 mm/ksm: unmerge and clear VM_MERGEABLE when setting PR_SET_MEMORY_MERGE=0
Let's unmerge any KSM pages when setting PR_SET_MEMORY_MERGE=0, and clear the VM_MERGEABLE flag from all VMAs -- just like KSM would. Of course, only do that if we previously set PR_SET_MEMORY_MERGE=1.
--- include/linux/ksm.h | 1 + kernel/sys.c | 7 +------ mm/ksm.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 6 deletions(-)
include/linux/ksm.h +1 -0
@@ -21,6 +21,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_enter (struct mm_struct *mm);void __ksm_exit (struct mm_struct *mm);
kernel/sys.c +1 -6
@@ -2698,12 +2698,7 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,if (arg2) { error =ksm_enable_merge_any (me->mm); }else { -/* -* TODO: we might want disable KSM on all VMAs and -* trigger unsharing to completely disable KSM. -*/ -clear_bit (MMF_VM_MERGE_ANY , &me->mm->flags); - error =0 ; + error =ksm_disable_merge_any (me->mm); }mmap_write_unlock (me->mm);break ;
mm/ksm.c +47 -0
65 lines not on this page · raw message
Signature · 1 line
2.39.2
Re: [PATCH v1 1/3] mm/ksm: unmerge and clear VM_MERGEABLE when setting PR_SET_MEMORY_MERGE=0
Quoted 30 lines · David Hildenbrand
Let's unmerge any KSM pages when setting PR_SET_MEMORY_MERGE=0, and clear the
VM_MERGEABLEflag from all VMAs -- just like KSM would. Of course, only do that if we previously set PR_SET_MEMORY_MERGE=1.Signed-off-by David Hildenbrand <[email protected]>--- include/linux/ksm.h | 1 + kernel/sys.c | 7 +------ mm/ksm.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 6 deletions(-)include/linux/ksm.h +1
-0@@ -21,6 +21,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_enter (struct mm_struct *mm);void __ksm_exit (struct mm_struct *mm);kernel/sys.c +0
-0@@ -2698,12 +2698,7 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,if (arg2) { error =ksm_enable_merge_any (me->mm);
kernel/sys.c SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, · line 2703
Quoted 10 lines
kernel/sys.c +1
-6}else { -/* -* TODO: we might want disable KSM on all VMAs and -* trigger unsharing to completely disable KSM. -*/ -clear_bit (MMF_VM_MERGE_ANY , &me->mm->flags); - error =0 ; + error =ksm_disable_merge_any (me->mm); }
nit:
can we do:
if (arg2) error = ksm_enable_merge_any(me->mm); else error = ksm_disable_merge_any(me->mm); mmap_write_unlock(me->mm); break;
Quoted 49 lines
mm/ksm.c +30
-0@@ -2520,6 +2520,22 @@ static void __ksm_add_vma(struct vm_area_struct *vma)vm_flags_set (vma,VM_MERGEABLE ); } +static int __ksm_del_vma (struct vm_area_struct *vma) +{ +int err; + +if (!(vma->vm_flags &VM_MERGEABLE )) +return 0 ; + +if (vma->anon_vma) { + err =unmerge_ksm_pages (vma, vma->vm_start, vma->vm_end); +if (err) +return err; + } + +vm_flags_clear (vma,VM_MERGEABLE ); +return 0 ; +}/** * ksm_add_vma - Mark vma as mergeable if compatible * @@ -2542,6 +2558,20 @@ static void ksm_add_vmas(struct mm_struct *mm)__ksm_add_vma (vma); } +static int ksm_del_vmas (struct mm_struct *mm) +{ +struct vm_area_struct *vma; +int err; + +VMA_ITERATOR (vmi, mm,0 ); +for_each_vma (vmi, vma) { + err =__ksm_del_vma (vma); +if (err) +return err; + } +return 0 ; +} +/** * ksm_enable_merge_any - Add mm to mm ksm list and enable merging on all * compatible VMA's
mm/ksm.c int ksm_enable_merge_any(struct mm_struct *mm) · line 2603
Quoted 5 lines
mm/ksm.c +1
-0@@ -2569,6 +2599,23 @@ int ksm_enable_merge_any(struct mm_struct *mm)return 0 ; } +int ksm_disable_merge_any (struct mm_struct *mm)
I understand we want to keep the name "symmetric" with ksm_enable_merge_any, but it also unmerges the ksm pages. Do we want to reflect that in the function name?
Can we add a comment for the function?
Quoted 4 lines
mm/ksm.c +4
-0+{ +int err; + +if (!test_bit (MMF_VM_MERGE_ANY , &mm->flags))
mm/ksm.c int ksm_enable_merge_any(struct mm_struct *mm)
Quoted 10 lines
mm/ksm.c +9
-0+return 0 ; + + err =ksm_del_vmas (mm); +if (err) { +ksm_add_vmas (mm); +return err; + } + +clear_bit (MMF_VM_MERGE_ANY , &mm->flags);
We only clear the MMF_VM_MERGE_ANY flag if there are no errors. Is this what we want? This means that if the process creates new memory regions they would still be marked as mergeable.
Quoted 6 lines
mm/ksm.c +3
-0+return 0 ; +} + int ksm_madvise(struct vm_area_struct *vma, unsigned long start,unsigned long end, int advice, unsigned long
*vm_flags)
{
Re: [PATCH v1 1/3] mm/ksm: unmerge and clear VM_MERGEABLE when setting PR_SET_MEMORY_MERGE=0
[...]
Quoted 10 lines
Quoted 1 line
nit:
can we do:if (arg2) error = ksm_enable_merge_any(me->mm); else error = ksm_disable_merge_any(me->mm); mmap_write_unlock(me->mm); break;
Indeed, thanks.
Quoted 53 lines
Quoted 53 lines
mm/ksm.c +30
-0@@ -2520,6 +2520,22 @@ static void __ksm_add_vma(struct vm_area_struct *vma)vm_flags_set (vma,VM_MERGEABLE ); } +static int __ksm_del_vma (struct vm_area_struct *vma) +{ +int err; + +if (!(vma->vm_flags &VM_MERGEABLE )) +return 0 ; + +if (vma->anon_vma) { + err =unmerge_ksm_pages (vma, vma->vm_start, vma->vm_end); +if (err) +return err; + } + +vm_flags_clear (vma,VM_MERGEABLE ); +return 0 ; +}/** * ksm_add_vma - Mark vma as mergeable if compatible * @@ -2542,6 +2558,20 @@ static void ksm_add_vmas(struct mm_struct *mm)__ksm_add_vma (vma); } +static int ksm_del_vmas (struct mm_struct *mm) +{ +struct vm_area_struct *vma; +int err; + +VMA_ITERATOR (vmi, mm,0 ); +for_each_vma (vmi, vma) { + err =__ksm_del_vma (vma); +if (err) +return err; + } +return 0 ; +} +/** * ksm_enable_merge_any - Add mm to mm ksm list and enable merging on all * compatible VMA's @@ -2569,6 +2599,23 @@ int ksm_enable_merge_any(struct mm_struct *mm)return 0 ; }
Quoted 6 lines
mm/ksm.c int ksm_enable_merge_any(struct mm_struct *mm) · line 2603
Quoted 2 lines
mm/ksm.c +1
-0+int ksm_disable_merge_any (struct mm_struct *mm)I understand we want to keep the name "symmetric" with
ksm_enable_merge_any, but it also unmerges the ksm pages. Do we want to reflect that in the function name?
ksm_disable_merge_any_umerge() is suboptimal.
As ksm_disable_merge_any() now reverts what ksm_enable_merge_any() ended up doing, I think it's just fine.
(it would be a different story if we'd be using "set" / "clear" terminology instead of "enable" / "disable").
We can describe that in the comment.
Quoted 2 lines
Can we add a comment for the function?
Can do for symmetry with ksm_enable_merge_any().
But note that I don't think documentation for functions is of any help when it takes longer to read the documentation than to read+understand the actual code.
Quoted 17 lines
Quoted 5 lines
mm/ksm.c +4
-0+{ +int err; + +if (!test_bit (MMF_VM_MERGE_ANY , &mm->flags))mm/ksm.c int ksm_enable_merge_any(struct mm_struct *mm)
Quoted 10 lines
mm/ksm.c +9
-0+return 0 ; + + err =ksm_del_vmas (mm); +if (err) { +ksm_add_vmas (mm); +return err; + } + +clear_bit (MMF_VM_MERGE_ANY , &mm->flags);We only clear the
MMF_VM_MERGE_ANYflag if there are no errors. Is this
I think this is the behavior we want. We tried to disable KSM for the process (previously enabled via the prctl), but cannot disable KSM. So we rollback our changes and return an error.
This is similar to trying to set MADV_UNMERGEABLE but failing. We leave the bit set.
Quoted 2 lines
what we want? This means that if the process creates new memory regions they would still be marked as mergeable.
Yes, we failed the operation so we keep everything unchanged.
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v1 1/3] mm/ksm: unmerge and clear VM_MERGEABLE when setting PR_SET_MEMORY_MERGE=0
Quoted 12 lines
Quoted 3 lines
I understand we want to keep the name "symmetric" with
ksm_enable_merge_any, but it also unmerges the ksm pages. Do we want to reflect that in the function name?
ksm_disable_merge_any_umerge()is suboptimal.As
ksm_disable_merge_any()now reverts whatksm_enable_merge_any()ended up doing, I think it's just fine.(it would be a different story if we'd be using "set" / "clear" terminology instead of "enable" / "disable").
Quoted 7 lines
We can describe that in the comment.
Quoted 2 lines
Can we add a comment for the function?
Can do for symmetry with
ksm_enable_merge_any().
+/**
+ * ksm_disable_merge_any - Disable merging on all compatible VMA's of the mm,
+ * previously enabled via ksm_enable_merge_any().
+ *
+ * Disabling merging implies unmerging any merged pages, like setting
+ * MADV_UNMERGEABLE would. If unmerging fails, the whole operation fails and
+ * merging on all compatible VMA's remains enabled.
+ *
+ * @mm: Pointer to mm
+ *
+ * Returns 0 on success, otherwise error code
+ */
Signature · 3 lines
Thanks, David / dhildenb
Stefan Roesch
Acked-byRe: [PATCH v1 1/3] mm/ksm: unmerge and clear VM_MERGEABLE when setting PR_SET_MEMORY_MERGE=0
Quoted 15 lines · David Hildenbrand
Quoted 11 lines
Quoted 3 lines
I understand we want to keep the name "symmetric" with
ksm_enable_merge_any, but it also unmerges the ksm pages. Do we want to reflect that in the function name?
ksm_disable_merge_any_umerge()is suboptimal.
Asksm_disable_merge_any()now reverts whatksm_enable_merge_any()ended up doing, I think it's just fine.
(it would be a different story if we'd be using "set" / "clear" terminology instead of "enable" / "disable").
We can describe that in the comment.Quoted 1 line
Quoted 3 lines
Quoted 1 line
Can we add a comment for the function?
Can do for symmetry with
ksm_enable_merge_any().
Quoted 12 lines
+/**
+ *ksm_disable_merge_any- Disable merging on all compatible VMA's of the mm,
+ * previously enabled viaksm_enable_merge_any().
+ *
+ * Disabling merging implies unmerging any merged pages, like setting
+ *MADV_UNMERGEABLEwould. If unmerging fails, the whole operation fails and
+ * merging on all compatible VMA's remains enabled.
+ *
+ * @mm: Pointer to mm
+ *
+ * Returns 0 on success, otherwise error code
+ */
LGTM
PATCH 2/3 selftests/ksm: ksm_functional_tests: add prctl unmerge test
In reply to David HildenbrandLet's test whether setting PR_SET_MEMORY_MERGE to 0 after setting it to 1 will unmerge pages, similar to how setting MADV_UNMERGEABLE after setting MADV_MERGEABLE would.
--- .../selftests/mm/ksm_functional_tests.c | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-)
tools/testing/selftests/mm/ksm_functional_tests.c +40 -6
@@ -91,9 +91,10 @@ static int ksm_merge(void)return 0 ; } -static char *mmap_and_merge_range (char val,unsigned long size) +static char *mmap_and_merge_range (char val,unsigned long size,bool use_prctl) {char *map; +int ret; map =mmap (NULL , size,PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_ANON ,-1 ,0 );
@@ -110,7 +111,17 @@ static char *mmap_and_merge_range(char val, unsigned long size)/* Make sure each page contains the same values to merge them. */ memset (map, val, size); -if (madvise (map, size,MADV_MERGEABLE )) { + +if (use_prctl) { + ret =prctl (PR_SET_MEMORY_MERGE ,1 ,0 ,0 ,0 ); +if (ret <0 && errno ==EINVAL ) { +ksft_test_result_skip ("PR_SET_MEMORY_MERGE not supported\n" ); + goto unmap; + }else if (ret) { +ksft_test_result_fail ("PR_SET_MEMORY_MERGE=1 failed\n" ); + goto unmap; + } + }else if (madvise (map, size,MADV_MERGEABLE )) {ksft_test_result_fail ("MADV_MERGEABLE failed\n" ); goto unmap; }
@@ -133,7 +144,7 @@ static void test_unmerge(void)ksft_print_msg ("[RUN] %s\n" , __func__); - map =mmap_and_merge_range (0xcf , size); + map =mmap_and_merge_range (0xcf , size, false);if (map ==MAP_FAILED )return ;
@@ -155,7 +166,7 @@ static void test_unmerge_discarded(void)ksft_print_msg ("[RUN] %s\n" , __func__); - map =mmap_and_merge_range (0xcf , size); + map =mmap_and_merge_range (0xcf , size, false);if (map ==MAP_FAILED )return ;
@@ -187,7 +198,7 @@ static void test_unmerge_uffd_wp(void)ksft_print_msg ("[RUN] %s\n" , __func__); - map =mmap_and_merge_range (0xcf , size); + map =mmap_and_merge_range (0xcf , size, false);if (map ==MAP_FAILED )return ;
@@ -323,9 +334,31 @@ static void test_prctl_fork(void)ksft_test_result_pass ("PR_SET_MEMORY_MERGE value is inherited\n" ); } +static void test_prctl_unmerge (void ) +{ +const unsigned int size =2 * MiB; +char *map; + +ksft_print_msg ("[RUN] %s\n" , __func__); + + map =mmap_and_merge_range (0xcf , size, true); +if (map ==MAP_FAILED ) +return ; + +if (prctl (PR_SET_MEMORY_MERGE ,0 ,0 ,0 ,0 )) { +ksft_test_result_fail ("PR_SET_MEMORY_MERGE=0 failed\n" ); + goto unmap; + } + +ksft_test_result (!range_maps_duplicates (map, size), +"Pages were unmerged\n" ); +unmap: +munmap (map, size); +} +int main (int argc,char **argv) { -unsigned int tests =4 ; +unsigned int tests =5 ;int err;#ifdef __NR_userfaultfd
@@ -355,6 +388,7 @@ int main(int argc, char **argv)test_prctl ();test_prctl_fork (); +test_prctl_unmerge (); err =ksft_get_fail_cnt ();if (err)
Signature · 1 line
2.39.2
Re: [PATCH v1 2/3] selftests/ksm: ksm_functional_tests: add prctl unmerge test
Quoted 105 lines · David Hildenbrand
Let's test whether setting
PR_SET_MEMORY_MERGEto 0 after setting it to 1 will unmerge pages, similar to how settingMADV_UNMERGEABLEafter settingMADV_MERGEABLEwould.Signed-off-by David Hildenbrand <[email protected]>--- .../selftests/mm/ksm_functional_tests.c | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-)tools/testing/selftests/mm/ksm_functional_tests.c +39
-6@@ -91,9 +91,10 @@ static int ksm_merge(void)return 0 ; } -static char *mmap_and_merge_range (char val,unsigned long size) +static char *mmap_and_merge_range (char val,unsigned long size,bool use_prctl) {char *map; +int ret; map =mmap (NULL , size,PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_ANON ,-1 ,0 );@@ -110,7 +111,17 @@ static char *mmap_and_merge_range(char val, unsigned long size)/* Make sure each page contains the same values to merge them. */ memset (map, val, size); -if (madvise (map, size,MADV_MERGEABLE )) { + +if (use_prctl) { + ret =prctl (PR_SET_MEMORY_MERGE ,1 ,0 ,0 ,0 ); +if (ret <0 && errno ==EINVAL ) { +ksft_test_result_skip ("PR_SET_MEMORY_MERGE not supported\n" ); + goto unmap; + }else if (ret) { +ksft_test_result_fail ("PR_SET_MEMORY_MERGE=1 failed\n" ); + goto unmap; + } + }else if (madvise (map, size,MADV_MERGEABLE )) {ksft_test_result_fail ("MADV_MERGEABLE failed\n" ); goto unmap; }@@ -133,7 +144,7 @@ static void test_unmerge(void)ksft_print_msg ("[RUN] %s\n" , __func__); - map =mmap_and_merge_range (0xcf , size); + map =mmap_and_merge_range (0xcf , size, false);if (map ==MAP_FAILED )return ;@@ -155,7 +166,7 @@ static void test_unmerge_discarded(void)ksft_print_msg ("[RUN] %s\n" , __func__); - map =mmap_and_merge_range (0xcf , size); + map =mmap_and_merge_range (0xcf , size, false);if (map ==MAP_FAILED )return ;@@ -187,7 +198,7 @@ static void test_unmerge_uffd_wp(void)ksft_print_msg ("[RUN] %s\n" , __func__); - map =mmap_and_merge_range (0xcf , size); + map =mmap_and_merge_range (0xcf , size, false);if (map ==MAP_FAILED )return ;@@ -323,9 +334,31 @@ static void test_prctl_fork(void)ksft_test_result_pass ("PR_SET_MEMORY_MERGE value is inherited\n" ); } +static void test_prctl_unmerge (void ) +{ +const unsigned int size =2 * MiB; +char *map; + +ksft_print_msg ("[RUN] %s\n" , __func__); + + map =mmap_and_merge_range (0xcf , size, true); +if (map ==MAP_FAILED ) +return ; + +if (prctl (PR_SET_MEMORY_MERGE ,0 ,0 ,0 ,0 )) { +ksft_test_result_fail ("PR_SET_MEMORY_MERGE=0 failed\n" ); + goto unmap; + } + +ksft_test_result (!range_maps_duplicates (map, size), +"Pages were unmerged\n" ); +unmap: +munmap (map, size); +} +int main (int argc,char **argv) { -unsigned int tests =4 ; +unsigned int tests =5 ;int err;#ifdef __NR_userfaultfd
tools/testing/selftests/mm/ksm_functional_tests.c int main(int argc, char **argv) · line 394
Quoted 7 lines
tools/testing/selftests/mm/ksm_functional_tests.c +1
-0@@ -355,6 +388,7 @@ int main(int argc, char **argv)test_prctl ();test_prctl_fork (); +test_prctl_unmerge (); err =ksft_get_fail_cnt ();if (err)
PATCH 3/3 mm/ksm: move disabling KSM from s390/gmap code to KSM code
In reply to David HildenbrandLet'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.
--- arch/s390/mm/gmap.c | 20 +------------------- include/linux/ksm.h | 6 ++++++ mm/ksm.c | 11 +++++++++++ 3 files changed, 18 insertions(+), 19 deletions(-)
arch/s390/mm/gmap.c +1 -19
@@ -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);
include/linux/ksm.h +6 -0
@@ -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);
@@ -75,6 +76,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 ;
mm/ksm.c +11 -0
@@ -2616,6 +2616,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) {
Signature · 1 line
2.39.2
Re: [PATCH v1 3/3] mm/ksm: move disabling KSM from s390/gmap code to KSM code
Quoted 24 lines · David Hildenbrand
Let's factor out actual disabling of KSM. The existing "
mm->def_flags&= ~VM_MERGEABLE;" was essentially a NOP and can be dropped, becausedef_flagsshould never includeVM_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.
Signed-off-by David Hildenbrand <[email protected]>--- arch/s390/mm/gmap.c | 20 +------------------- include/linux/ksm.h | 6 ++++++ mm/ksm.c | 11 +++++++++++ 3 files changed, 18 insertions(+), 19 deletions(-)arch/s390/mm/gmap.c +0
-1@@ -2585,30 +2585,12 @@ EXPORT_SYMBOL_GPL(s390_enable_sie);int gmap_mark_unmergeable (void ) { -struct mm_struct *mm = current->mm;
arch/s390/mm/gmap.c EXPORT_SYMBOL_GPL(s390_enable_sie); · line 2592
Quoted 10 lines
arch/s390/mm/gmap.c +0
-5-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. */
Is that still true?
My KSM knowledge is nearly zero but from what I can see the patch looks ok to me:
Re: [PATCH v1 3/3] mm/ksm: move disabling KSM from s390/gmap code to KSM code
Quoted 22 lines · Janosch Frank
Quoted 21 lines · David Hildenbrand
Let's factor out actual disabling of KSM. The existing "
mm->def_flags&= ~VM_MERGEABLE;" was essentially a NOP and can be dropped, becausedef_flagsshould never includeVM_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.
Signed-off-by David Hildenbrand <[email protected]>--- arch/s390/mm/gmap.c | 20 +------------------- include/linux/ksm.h | 6 ++++++ mm/ksm.c | 11 +++++++++++ 3 files changed, 18 insertions(+), 19 deletions(-)arch/s390/mm/gmap.c +0
-0@@ -2585,30 +2585,12 @@ EXPORT_SYMBOL_GPL(s390_enable_sie);
Quoted 15 lines
arch/s390/mm/gmap.c EXPORT_SYMBOL_GPL(s390_enable_sie); · line 2592
Quoted 13 lines
arch/s390/mm/gmap.c +0
-6int 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. */ Is that still true?
Yes. We'd need another per-MM bit to stop it from getting re-enabled.
Quoted 4 lines
My KSM knowledge is nearly zero but from what I can see the patch looks ok to me:
Acked-by Janosch Frank <[email protected]>
Thanks!
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v1 3/3] mm/ksm: move disabling KSM from s390/gmap code to KSM code
In reply to David HildenbrandQuoted 37 lines · David Hildenbrand
Let's factor out actual disabling of KSM. The existing "
mm->def_flags&= ~VM_MERGEABLE;" was essentially a NOP and can be dropped, becausedef_flagsshould never includeVM_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.
Signed-off-by David Hildenbrand <[email protected]>--- arch/s390/mm/gmap.c | 20 +------------------- include/linux/ksm.h | 6 ++++++ mm/ksm.c | 11 +++++++++++ 3 files changed, 18 insertions(+), 19 deletions(-)arch/s390/mm/gmap.c +0
-9@@ -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) {
arch/s390/mm/gmap.c EXPORT_SYMBOL_GPL(s390_enable_sie); · line 2593
Quoted 10 lines
arch/s390/mm/gmap.c +0
-9-/* 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 ;
This clears the def_flags struct member, however, in ksm_disable() we clear the __flags struct member. Is this a problem?
Quoted 51 lines
arch/s390/mm/gmap.c +1
-1-return 0 ; +return ksm_disable (current->mm); }EXPORT_SYMBOL_GPL (gmap_mark_unmergeable);include/linux/ksm.h +6
-0@@ -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);@@ -75,6 +76,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 ;mm/ksm.c +11
-0@@ -2616,6 +2616,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) {
Re: [PATCH v1 3/3] mm/ksm: move disabling KSM from s390/gmap code to KSM code
[...]
Quoted 24 lines
Quoted 24 lines
arch/s390/mm/gmap.c +0
-11@@ -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;
Quoted 9 lines
arch/s390/mm/gmap.c EXPORT_SYMBOL_GPL(s390_enable_sie); · line 2596
Quoted 8 lines
arch/s390/mm/gmap.c +0
-7- 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 ;
Hi Stefan,
Quoted 2 lines
This clears the
def_flagsstruct member, however, inksm_disable()we clear the__flagsstruct member. Is this a problem?
The patch description contains a comment regarding def_flags: "The existing "mm->def_flags &= ~VM_MERGEABLE;" was essentially a NOP and can be dropped, because def_flags should never include VM_MERGEABLE."
We keep clearing the MADV_UNMERGEABLE flag from MADV_UNMERGEABLE. In the old code, ksm_madvise() would have cleared it from local vm_flags and vm_flags_reset() would have modified vma->vm_flags. Now we clear it directly via vm_flags_clear(vma, VM_MERGEABLE);
Long story short, the mm->def_flags code as wrong and most probably copied from thp_split_mm() where we do:
mm->def_flags |= VM_NOHUGEPAGE;
Which makes more sense.
Thanks!
Signature · 3 lines
Thanks, David / dhildenb
Stefan Roesch
Acked-byRe: [PATCH v1 3/3] mm/ksm: move disabling KSM from s390/gmap code to KSM code
Quoted 51 lines · David Hildenbrand
[...]
Quoted 24 lines
Quoted 24 lines
arch/s390/mm/gmap.c +0
-11@@ -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;Quoted 9 lines
arch/s390/mm/gmap.c EXPORT_SYMBOL_GPL(s390_enable_sie); · line 2596
Quoted 8 lines
arch/s390/mm/gmap.c +0
-7- 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 ;Hi Stefan,
Quoted 2 lines
This clears the
def_flagsstruct member, however, inksm_disable()we clear the__flagsstruct member. Is this a problem?The patch description contains a comment regarding
def_flags: "The existing "mm->def_flags&= ~VM_MERGEABLE;" was essentially a NOP and can be dropped, becausedef_flagsshould never includeVM_MERGEABLE."We keep clearing the
MADV_UNMERGEABLEflag fromMADV_UNMERGEABLE. In the old code,ksm_madvise()would have cleared it from localvm_flagsandvm_flags_reset()would have modifiedvma->vm_flags. Now we clear it directly viavm_flags_clear(vma, VM_MERGEABLE);
Quoted 6 lines
Long story short, the
mm->def_flagscode as wrong and most probably copied fromthp_split_mm()where we do:mm->def_flags |= VM_NOHUGEPAGE;Which makes more sense.
Thanks!
Thanks for the explanation.