Skip to contentSkip to conversation

KSM: numa awareness sysfs knob

PATCH Superseded Petr Holasek 6 participants 14 messages Jun 29, 2012 – Jul 3, 2012

Subsystems (3)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

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: 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

---
 Documentation/vm/ksm.txt |    6 +++
 mm/ksm.c                 |  117 ++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 104 insertions(+), 19 deletions(-)
Documentation/vm/ksm.txt +6 -0
index b392e49..79e5760 100644
@@ -58,6 +58,12 @@ 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
+                   resides in the memory area of same NUMA node. It brings
+                   lower latency to access to shared page.
+                   Default: 1
+
 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 +98 -19

239 lines not on this page · raw message

Signature · 7 lines
1.7.10.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>
#1 lore raw

mm/ksm.c static struct rmap_item *scan_get_next_rmap_item(struct page **page) · line 1338

Quoted 3 lines · Petr Holasek
mm/ksm.c +2 -1
-		root_unstable_tree = RB_ROOT;
+		for (i = 0; i < MAX_NUMNODES; i++)
+			root_unstable_tree[i] = RB_ROOT;

This is not aware of memory-hotplug, right?

--
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>

#2 lore raw
Quoted 8 lines · Cong Wang

mm/ksm.c static struct rmap_item *scan_get_next_rmap_item(struct page **page) · line 1338

Quoted 3 lines · Petr Holasek
mm/ksm.c +2 -1
-		root_unstable_tree = RB_ROOT;
+		for (i = 0; i < MAX_NUMNODES; i++)
+			root_unstable_tree[i] = RB_ROOT;

This is not aware of memory-hotplug, right?

What makes you think so?

--
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>

#3 lore raw
In reply to Petr Holasek
Quoted 4 lines · Petr Holasek

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).

Is it conceivable that admins may (in the future) want to merge only across nodes that are below a given distance threshold?

I'm not asking to implement this, just whether the knob can be introduced such that it's future-compatible. Make it default to a Very High Number and only allow setting it to 0 for now e.g.? And name it max_node_merge_distance (I'm bad at names)?

What do you think?

#4 lore raw
Quoted 9 lines · Johannes Weiner
Quoted 4 lines · Petr Holasek

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).

Is it conceivable that admins may (in the future) want to merge only across nodes that are below a given distance threshold?

Quoted 6 lines

I'm not asking to implement this, just whether the knob can be introduced such that it's future-compatible. Make it default to a Very High Number and only allow setting it to 0 for now e.g.? And name it max_node_merge_distance (I'm bad at names)?

What do you think?

I started with exactly same idea as you described above in the first RFC, link: https://lkml.org/lkml/2011/11/30/91
But this approach turned out to be more complicated than it looked (see two last emails in thread) and complexity of solution would rise a lot.

Regards,
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>

#5 lore raw
Quoted 15 lines · Petr Holasek
Quoted 14 lines · Johannes Weiner
Quoted 4 lines · Petr Holasek

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).

Is it conceivable that admins may (in the future) want to merge only across nodes that are below a given distance threshold?

I'm not asking to implement this, just whether the knob can be introduced such that it's future-compatible. Make it default to a Very High Number and only allow setting it to 0 for now e.g.? And name it max_node_merge_distance (I'm bad at names)?

Quoted 7 lines
Quoted 1 line

What do you think?

I started with exactly same idea as you described above in the first RFC, link: https://lkml.org/lkml/2011/11/30/91
But this approach turned out to be more complicated than it looked (see two last emails in thread) and complexity of solution would rise a lot.

Oh, I should have checked the archives given that it's v2. I expected it to get complex but didn't put enough thought into it to see /that/ amount of complexity. Sorry.

Carry on, then :-)

--
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>

#6 lore raw
Quoted 6 lines · Johannes Weiner
Quoted 5 lines

I started with exactly same idea as you described above in the first RFC, link: https://lkml.org/lkml/2011/11/30/91
But this approach turned out to be more complicated than it looked (see two last emails in thread) and complexity of solution would rise a lot.

Quoted 6 lines

Oh, I should have checked the archives given that it's v2. I expected it to get complex but didn't put enough thought into it to see /that/ amount of complexity. Sorry.

Carry on, then :-)

I don't think it's an unfair amount of complexity to ask for, and I don't see the problem with ksm merging two pages that have a distance under the configured threshold and leaving the third page unmerged; by configuring the threshold (which should be a char, not an int) the admin has specified the locality that is necessary for optimal performance so has knowingly restricted ksm in that way.

I'd rename it to ksm_merge_distance, which is more similar to reclaim_distance, and return to the first version of this patch.

--
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>

#7 lore raw
Quoted 22 lines · David Rientjes
Quoted 6 lines · Johannes Weiner
Quoted 5 lines

I started with exactly same idea as you described above in the first RFC, link: https://lkml.org/lkml/2011/11/30/91
But this approach turned out to be more complicated than it looked (see two last emails in thread) and complexity of solution would rise a lot.

Quoted 6 lines

Oh, I should have checked the archives given that it's v2. I expected it to get complex but didn't put enough thought into it to see /that/ amount of complexity. Sorry.

Carry on, then :-)

I don't think it's an unfair amount of complexity to ask for, and I don't see the problem with ksm merging two pages that have a distance under the configured threshold and leaving the third page unmerged; by configuring the threshold (which should be a char, not an int) the admin has specified the locality that is necessary for optimal performance so has knowingly restricted ksm in that way.

Quoted 2 lines

I'd rename it to ksm_merge_distance, which is more similar to reclaim_distance, and return to the first version of this patch.

The problem of the first patch/RFC was that merging algorithm was unstable and could merge pages with distance higher than was set up (described by Nai Xia in RFC thread [1]). Sure, this instability could be solved, but for ksm pages shared by many other pages on different nodes we would have to still recalculate which page is "in the middle" and in case of change migrate it between nodes every time when ksmd reach new shareable page or when some sharing page is removed.

But please correct me if I understand you wrong.

[1] https://lkml.org/lkml/2011/12/1/167

--
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>

#8 lore raw
Quoted 8 lines · Petr Holasek

The problem of the first patch/RFC was that merging algorithm was unstable and could merge pages with distance higher than was set up (described by Nai Xia in RFC thread [1]). Sure, this instability could be solved, but for ksm pages shared by many other pages on different nodes we would have to still recalculate which page is "in the middle" and in case of change migrate it between nodes every time when ksmd reach new shareable page or when some sharing page is removed.

Or you could simply refuse to ever merge any page that is identical to a page on a node with a distance greater than the threshold, i.e. never merge pages even under the threshold if a page exists on a node higher than the threshold.

--
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>

#9 lore raw
Quoted 10 lines · David Rientjes
Quoted 9 lines

The problem of the first patch/RFC was that merging algorithm was unstable and could merge pages with distance higher than was set up (described by Nai Xia in RFC thread [1]). Sure, this instability could be solved, but for ksm pages shared by many other pages on different nodes we would have to still
recalculate which page is "in the middle" and in case of change migrate it between nodes every time when ksmd reach new shareable page or when some sharing page is removed.

Quoted 4 lines

Or you could simply refuse to ever merge any page that is identical to a page on a node with a distance greater than the threshold, i.e. never merge pages even under the threshold if a page exists on a node higher than the threshold.

Position of the shared page would be really dependent on order of scanning. We could have shared page in the ideal middle as well as on the edge where it would block merging of other pages even under the threshold.

--
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>

maintainer
#10 lore raw
In reply to Petr Holasek
Quoted 5 lines · On Fri, 29 Jun 2012 13:49:52 +0200 Petr Holasek <[email protected]> wrote:

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).

Quoted 8 lines

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.

It would be neat to have a knob which enables KSM for all anon mappings. ie: pretend that MADV_MERGEABLE is always set. For testing coverage purposes.

Quoted 14 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%

Quoted 4 lines

merge_nodes=0
2 nodes 1%
4 nodes 0.32%
8 nodes 0.018%

ooh, numbers! Thanks.

Documentation/vm/ksm.txt sleep_millisecs - how many milliseconds ksmd should sleep before next scan · line 65

Quoted 11 lines
Documentation/vm/ksm.txt +5 -0
@@ -58,6 +58,12 @@ 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
+                   resides in the memory area of same NUMA node. It brings
+                   lower latency to access to shared page.
+                   Default: 1

s/resides/reside/.

This doc should mention that /sys/kernel/mm/ksm/run should be zeroed to alter merge_nodes. Otherwise confusion will reign.

Quoted 20 lines

...

Documentation/vm/ksm.txt +17 -0
+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;
+	if (knob > 1)
+		return -EINVAL;
+
+	if (ksm_run & KSM_RUN_MERGE)
+		return -EBUSY;
+
+	mutex_lock(&ksm_thread_mutex);

Documentation/vm/ksm.txt

Quoted 10 lines
Documentation/vm/ksm.txt +10 -0
+	if (ksm_merge_nodes != knob) {
+		if (ksm_pages_shared > 0)
+			return -EBUSY;
+		else
+			ksm_merge_nodes = knob;
+	}
+	mutex_unlock(&ksm_thread_mutex);
+
+	return count;
+}

Seems a bit racy. Shouldn't the test of ksm_run be inside the locked region?

Documentation/vm/ksm.txt

Quoted 5 lines
Documentation/vm/ksm.txt +2 -0
+KSM_ATTR(merge_nodes);
+#endif
 
...

--
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>

#11 lore raw
Quoted 14 lines · Andrew Morton
Quoted 14 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%

Quoted 7 lines
Quoted 4 lines

merge_nodes=0
2 nodes 1%
4 nodes 0.32%
8 nodes 0.018%

ooh, numbers! Thanks.

Ok, the standard deviation increases when merging pages from nodes with remote distance, that makes sense. But if that's true, then you would restrict either the entire application to local memory with mempolicies or cpusets, or you would use mbind() to restrict this memory to that set of nodes already so that accesses, even with ksm merging, would have affinity.

--
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>

#12 lore raw
Quoted 20 lines · David Rientjes
Quoted 18 lines · Andrew Morton
Quoted 18 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%

Quoted 8 lines
Quoted 2 lines

ooh, numbers! Thanks.

Ok, the standard deviation increases when merging pages from nodes with remote distance, that makes sense. But if that's true, then you would restrict either the entire application to local memory with mempolicies or cpusets, or you would use mbind() to restrict this memory to that set of nodes already so that accesses, even with ksm merging, would have affinity.

While you are right for case you write your own custom application,
but I think the KVM guest case is little bit more problomatic in case the guest memory must be splitted across serval nodes.

--
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>

#13 lore raw
In reply to Andrew Morton
Quoted 13 lines · Andrew Morton
Quoted 10 lines · On Fri, 29 Jun 2012 13:49:52 +0200 Petr Holasek <[email protected]> wrote:

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.

Quoted 7 lines
Quoted 3 lines

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.

It would be neat to have a knob which enables KSM for all anon mappings. ie: pretend that MADV_MERGEABLE is always set. For testing coverage purposes.

Interesting idea, I'll try to add it in next release if /sys/kernel/mm/ksm directory is the right place for such debug knob.

Quoted 14 lines

Documentation/vm/ksm.txt sleep_millisecs - how many milliseconds ksmd should sleep before next scan · line 65

Quoted 11 lines
Documentation/vm/ksm.txt +5 -0
@@ -58,6 +58,12 @@ 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
+                   resides in the memory area of same NUMA node. It brings
+                   lower latency to access to shared page.
+                   Default: 1

s/resides/reside/.

Quoted 3 lines

This doc should mention that /sys/kernel/mm/ksm/run should be zeroed to alter merge_nodes. Otherwise confusion will reign.

Oh, forgot to mention it. I'll fix it.

Quoted 25 lines
Quoted 25 lines

...

Documentation/vm/ksm.txt +22 -0
+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;
+	if (knob > 1)
+		return -EINVAL;
+
+	if (ksm_run & KSM_RUN_MERGE)
+		return -EBUSY;
+
+	mutex_lock(&ksm_thread_mutex);
+	if (ksm_merge_nodes != knob) {
+		if (ksm_pages_shared > 0)
+			return -EBUSY;
+		else
+			ksm_merge_nodes = knob;
Quoted 9 lines

Documentation/vm/ksm.txt

Quoted 5 lines
Documentation/vm/ksm.txt +5 -0
+	}
+	mutex_unlock(&ksm_thread_mutex);
+
+	return count;
+}

Seems a bit racy. Shouldn't the test of ksm_run be inside the locked region?

Agreed.

Thanks for your review!

--
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>