KSM: numa awareness sysfs knob
PATCH v3 1/1 KSM: numa awareness sysfs knob
Introduces new sysfs boolean knob /sys/kernel/mm/ksm/merge_nodes which control merging pages across different numa nodes.
When it is set to zero only pages from the same node are merged, otherwise pages from all nodes can be merged together (default behavior).
Typical use-case could be a lot of KVM guests on NUMA machine and cpus from more distant nodes would have significant increase of access latency to the merged ksm page. Sysfs knob was choosen for higher scalability.
Every numa node has its own stable & unstable trees because of faster searching and inserting. Changing of merge_nodes value is possible only when there are not any ksm shared pages in system.
This patch also adds share_all sysfs knob which can be used for adding all anon vmas of all processes in system to ksmd scan queue. Knob can be triggered only when run knob is set to zero.
I've tested this patch on numa machines with 2, 4 and 8 nodes and measured speed of memory access inside of KVM guests with memory pinned to one of nodes with this benchmark:
http://pholasek.fedorapeople.org/alloc_pg.c
Population standard deviations of access times in percentage of average were following:
merge_nodes=1
2 nodes 1.4%
4 nodes 1.6%
8 nodes 1.7%
merge_nodes=0
2 nodes 1%
4 nodes 0.32%
8 nodes 0.018%
RFC: https://lkml.org/lkml/2011/11/30/91
v1: https://lkml.org/lkml/2012/1/23/46
v2: https://lkml.org/lkml/2012/6/29/105
Changelog:
v2: Andrew's objections were reflected:
- value of merge_nodes can't be changed while there are some ksm pages in system
- merge_nodes sysfs entry appearance depends on CONFIG_NUMA
- more verbose documentation
- added some performance testing results
v3: - more verbose documentation
- fixed race in merge_nodes store function
- introduced share_all debugging knob proposed by Andrew
- minor cleanups
--- Documentation/vm/ksm.txt | 12 ++ mm/ksm.c | 326 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 270 insertions(+), 68 deletions(-)
Documentation/vm/ksm.txt +12 -0
@@ -58,6 +58,18 @@ sleep_millisecs - how many milliseconds ksmd should sleep before next scan e.g. "echo 20 > /sys/kernel/mm/ksm/sleep_millisecs" Default: 20 (chosen for demonstration purposes) +merge_nodes - specifies if pages from different numa nodes can be merged. + When set to 0, ksm merges only pages which physically + reside in the memory area of same NUMA node. It brings + lower latency to access to shared page. Value can be + changed only when there is no ksm shared pages in system. + Default: 1 + +share_all - when user write 1 to this file, MADV_MERGEABLE is set for + all possible current memory mappings of all processes. It + can be used only when content of run file is zero. + Default: 0 + run - set 0 to stop ksmd from running but keep merged pages, set 1 to run ksmd e.g. "echo 1 > /sys/kernel/mm/ksm/run", set 2 to stop ksmd and unmerge all pages currently merged,
mm/ksm.c +258 -68
456 lines not on this page · raw message
Signature · 7 lines
1.7.11.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerQuoted 1 line · On Fri, 14 Sep 2012 23:22:47 +0200 Petr Holasek <[email protected]> wrote:
Introduces new sysfs boolean knob
/sys/kernel/mm/ksm/merge_nodes
I wonder if merge_across_nodes would be a better name.
Quoted 8 lines
which control merging pages across different numa nodes.
When it is set to zero only pages from the same node are merged, otherwise pages from all nodes can be merged together (default behavior).Typical use-case could be a lot of KVM guests on NUMA machine and cpus from more distant nodes would have significant increase of access latency to the merged ksm page. Sysfs knob was choosen for higher scalability.
Well... what is the use case for merge_nodes=0? IOW, why shouldn't we make this change non-optional and avoid the sysfs knob?
Quoted 7 lines
Every numa node has its own stable & unstable trees because of faster searching and inserting. Changing of
merge_nodesvalue is possible only when there are not any ksm shared pages in system.This patch also adds
share_allsysfs knob which can be used for adding all anon vmas of all processes in system to ksmd scan queue. Knob can be triggered only when run knob is set to zero.
I really don't understand this share_all thing. From reading the code, it is a once-off self-resetting trigger thing. Why? How is one to use this? What's the benefit? What's the effect?
Quoted 23 lines
I've tested this patch on numa machines with 2, 4 and 8 nodes and measured speed of memory access inside of KVM guests with memory pinned to one of nodes with this benchmark:
http://pholasek.fedorapeople.org/alloc_pg.c
Population standard deviations of access times in percentage of average were following:
merge_nodes=1
2 nodes 1.4%
4 nodes 1.6%
8 nodes 1.7%merge_nodes=0
2 nodes 1%
4 nodes 0.32%
8 nodes 0.018%...
mm/ksm.c static void remove_node_from_stable_tree(struct stable_node *stable_node) · line 479
Quoted 8 lines
mm/ksm.c +4
-1@@ -462,7 +473,13 @@ static void remove_node_from_stable_tree(struct stable_node *stable_node)cond_resched (); } -rb_erase (&stable_node->node, &root_stable_tree); +if (ksm_merge_nodes) + nid =0 ; +else + nid =pfn_to_nid (stable_node->kpfn);
This sequence happens three times - it might be a little tidier to capture the above into a separate helper function. Or not bother ;) One benefit of the standalone function is that it provides a nice site for a comment
Quoted 14 lines
mm/ksm.c +8
-0+rb_erase (&stable_node->node, + &root_stable_tree[nid]);free_stable_node (stable_node); } +static int ksmd_should_run (void ) +{ +return (ksm_run &KSM_RUN_MERGE ) && !list_empty (&ksm_mm_head.mm_list); +} + +static int ksmd_should_madvise (void )
mm/ksm.c void __ksm_exit(struct mm_struct *mm) · line 1610
Quoted 10 lines
mm/ksm.c +10
-0+{ +return ksm_share_all; +} + +static int ksm_madvise_all (void ) +{ +struct task_struct *p; +int err; + +for_each_process (p) {
what, what. We can't just go waltzing across the task list without taking any locks. Needs rcu_read_lock(), methinks.
Also... I've forgotten how threads/processes are arranged. Will this walk across all the threads in the system? If so, that would be terribly inefficient walking 1000 task structs which share a single mm, manipulating that mm 1000 times. It might be better to walk the mm's instead - see mm_struct.mmlist.
mm/ksm.c void __ksm_exit(struct mm_struct *mm) · line 1616
Quoted 6 lines
mm/ksm.c +6
-0+read_lock (&tasklist_lock); + +if (!p->mm) + goto out; + +down_write (&p->mm->mmap_sem);
whoa, you can't do down_write() inside read_lock().
Please, immediately put down your mail client, read Documentation/SubmitChecklist section 12 and go make the appropriate changes to your kernel .config.
Quoted 9 lines
mm/ksm.c +9
-0+ err =ksm_madvise_mm (p->mm); +up_write (&p->mm->mmap_sem); +out: +read_unlock (&tasklist_lock); +if (err) +break ; +cond_resched (); + } +return err;
mm/ksm.c void __ksm_exit(struct mm_struct *mm) · line 1635
Quoted 10 lines
mm/ksm.c +10
-0+} + +/** +* ksm_do_scan - the ksm scanner main worker function. +* @scan_npages - number of pages we want to scan before we return. +*/ +static void ksm_do_scan (unsigned int scan_npages) +{ +struct rmap_item *rmap_item; +struct page *uninitialized_var (page );
gcc is silly. I think that got fixed in more recent versions.
Quoted 5 lines
mm/ksm.c +5
-0+while (scan_npages-- &&likely (!freezing (current))) { +cond_resched (); + rmap_item =scan_get_next_rmap_item (&page); +if (!rmap_item) +return ;
mm/ksm.c void __ksm_exit(struct mm_struct *mm) · line 1651
Quoted 10 lines
mm/ksm.c +10
-0+if (!PageKsm (page) || !in_stable_tree (rmap_item)) +cmp_and_merge_page (page, rmap_item); +put_page (page); + } +} + +static int ksm_scan_thread (void *nothing) +{ +set_freezable (); +set_user_nice (current,5 );
The reason for the set_user_nice() is a total mystery to this and any other reader. Hence it needs a comment.
Quoted 41 lines
mm/ksm.c +35
-0+while (!kthread_should_stop ()) { +mutex_lock (&ksm_thread_mutex); +if (ksmd_should_madvise ()) { +ksm_madvise_all (); + ksm_share_all =0 ; + } +if (ksmd_should_run ()) +ksm_do_scan (ksm_thread_pages_to_scan); +mutex_unlock (&ksm_thread_mutex); + +try_to_freeze (); + +if (ksmd_should_run ()) { +schedule_timeout_interruptible ( +msecs_to_jiffies (ksm_thread_sleep_millisecs)); + }else { +wait_event_freezable (ksm_thread_wait, +ksmd_should_run () || +ksmd_should_madvise () || +kthread_should_stop ()); + } + } +return 0 ; +} +struct page *ksm_does_need_to_copy (struct page *page,struct vm_area_struct *vma,unsigned long address) { +static ssize_t merge_nodes_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count) +{ +int err; +unsigned long knob; + + err =kstrtoul (buf,10 , &knob); +if (err) +return err;
mm/ksm.c void __ksm_exit(struct mm_struct *mm)
Quoted 10 lines
mm/ksm.c +10
-0+if (knob >1 ) +return -EINVAL ; + +mutex_lock (&ksm_thread_mutex); +if (ksm_run &KSM_RUN_MERGE ) { + err = -EBUSY ; + }else { +if (ksm_merge_nodes != knob) { +if (ksm_pages_shared >0 ) + err = -EBUSY ;
What's happening here? The attempt to set merge_nodes can randomly fail due to internal transient state within ksm? That sounds rather user-hostile.
What did the user do wrong and how should he correct the situation?
What documentation should he have read to avoid this mistake?
Quoted 26 lines
mm/ksm.c +26
-0+ else + ksm_merge_nodes = knob; + } + } + +if (err) + count = err; +mutex_unlock (&ksm_thread_mutex); + +return count; +} +KSM_ATTR (merge_nodes); +#endif + +static ssize_t share_all_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +return sprintf (buf,"%u\n" , ksm_share_all); +} + +static ssize_t share_all_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count) +{ +int err; +unsigned long knob;
mm/ksm.c static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr, · line 2102
Quoted 10 lines
mm/ksm.c +10
-0+ + err =kstrtoul (buf,10 , &knob); +if (err) +return err; +if (knob >1 ) +return -EINVAL ; + +mutex_lock (&ksm_thread_mutex); +if (ksm_run &KSM_RUN_MERGE ) { + err = -EBUSY ;
OK, this one makes more sense: the user most stop KSM before altering share_all. Document this?
Quoted 4 lines
mm/ksm.c +3
-0+ }else { +if (ksm_share_all != knob) + ksm_share_all = knob;
mm/ksm.c static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr,
Quoted 10 lines
mm/ksm.c +8
-0+ } +if (err) + count = err; +mutex_unlock (&ksm_thread_mutex); + +return count; +} +KSM_ATTR (share_all);
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
On Fri, 14 Sep 2012, Andrew Morton wrote:
Hi Andrew,
at first thanks for your review!
Quoted 7 lines
Quoted 1 line · On Fri, 14 Sep 2012 23:22:47 +0200 Petr Holasek <[email protected]> wrote:
Introduces new sysfs boolean knob
/sys/kernel/mm/ksm/merge_nodesI wonder if
merge_across_nodeswould be a better name.
Agreed.
Quoted 11 lines
Quoted 8 lines
which control merging pages across different numa nodes.
When it is set to zero only pages from the same node are merged, otherwise pages from all nodes can be merged together (default behavior).Typical use-case could be a lot of KVM guests on NUMA machine and cpus from more distant nodes would have significant increase of access latency to the merged ksm page. Sysfs knob was choosen for higher scalability.
Well... what is the use case for merge_nodes=0? IOW, why shouldn't we make this change non-optional and avoid the sysfs knob?
I assume that there are still some users who want to use KSM mainly for saving of physical memory and access latency is not priority for them.
Quoted 8 lines
Quoted 4 lines
This patch also adds
share_allsysfs knob which can be used for adding all anon vmas of all processes in system to ksmd scan queue. Knob can be triggered only when run knob is set to zero.I really don't understand this
share_allthing. From reading the code, it is a once-off self-resetting trigger thing. Why? How is one to use this? What's the benefit? What's the effect?
I introduced it on the basis of our discussion about v2 patch
https://lkml.org/lkml/2012/6/29/426 as some knob which can madvise all anon mappings with MADV_MERGEABLE. But I might have misunderstood your idea. If you don't like current self-resetting trigger we either can implement it as stable 0/1 knob that would madvise all current and future anon mappings when set to 1 or completely exclude this share_all thing from the patch.
I am going to fix all all mistakes you pointed out in your review as well as add more verbose documentation and comments in next version.
thanks,
Petr H
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>