Skip to contentSkip to conversation

linux guests and ksm performance

DISCUSSION Peter Lieven 5 participants 17 messages Feb 23, 2012 – Feb 28, 2012 KVM

lore
Share & tools
lore
#0 lore raw

Hi,

i have recently been playing with an old idea (originally in grsecurity for security reasons) to change
the policy from zero on allocate to zero after free in the linux page allocator. My concern is that linux
leaves a lot of waste in the physical memory unlike Windows which per default zeros pages after
they are freed.

I have run some tests and I can confirm some old results that a hardware Linux machine
is approximately 2-3% slower with zero after free on big compilation jobs. This might be due
to either the fact that pages are only zeroed on allocate if GFP_ZERO is set or due to caching
benefits.

However, in a virtual machine I have not observed the above slow down to that extend
while the benefit of zero after free in a virtualisation environment is obvious:

1) zero pages can easily be merged by ksm or other technique.
2) zero (dup) pages are a lot faster to transfer in case of migration.

Therefore I would like to hear your thoughts if it would be a good idea to change
the strategy in the Linux kernel from zero on allocate to zero after free automatically
if the 'hypervisor' cpu feature is set? Or even have another technique to tell a linux
guest that ksm is running on the host.

If this is not feasible can someone think of a kernel module / userspace program that
zeroes out unused pages periodically.

Peter

#1 lore raw
Quoted 7 lines · Peter Lieven

However, in a virtual machine I have not observed the above slow down to that extend
while the benefit of zero after free in a virtualisation environment is obvious:

1) zero pages can easily be merged by ksm or other technique.
2) zero (dup) pages are a lot faster to transfer in case of migration.

The other approach is a memory page "discard" mechanism - which obviously requires more code changes than zeroing freed pages.

The advantage is that we don't take the brute-force and CPU intensive approach of zeroing pages. It would be like a fine-grained ballooning feature.

I hope someone will follow up saying this has already been done or prototyped :).

Stefan

#2 lore raw
Quoted 6 lines · Stefan Hajnoczi

The other approach is a memory page "discard" mechanism - which obviously requires more code changes than zeroing freed pages.

The advantage is that we don't take the brute-force and CPU intensive approach of zeroing pages.  It would be like a fine-grained ballooning feature.

(disclaimer: i don't know the code, i'm just guessing)

does KVM emulate the MMU? if so, is there any 'unmap page' primitive?

Signature · 1 line
Javier
#3 lore raw
In reply to Stefan Hajnoczi
Quoted 10 lines · Stefan Hajnoczi
Quoted 1 line · Peter Lieven

However, in a virtual machine I have not observed the above slow down

to

Quoted 2 lines

that extend
while the benefit of zero after free in a virtualisation environment

is

Quoted 4 lines

obvious:

1) zero pages can easily be merged by ksm or other technique.
2) zero (dup) pages are a lot faster to transfer in case of

Quoted 9 lines

migration.

The other approach is a memory page "discard" mechanism - which obviously requires more code changes than zeroing freed pages.

The advantage is that we don't take the brute-force and CPU intensive approach of zeroing pages. It would be like a fine-grained ballooning feature.

I dont think that it is cpu intense. All user pages are zeroed anyway, but at allocation time it shouldnt be a big difference in terms of cpu power.

Quoted 4 lines

I hope someone will follow up saying this has already been done or prototyped :).

Stefan

Signature · 1 line
Diese Nachricht wurde von meinem Android-Mobiltelefon mit K-9 Mail gesendet.
#4 lore raw
Quoted 21 lines · On Thu, Feb 23, 2012 at 7:08 PM, [email protected] <[email protected]> wrote:
Quoted 15 lines · Stefan Hajnoczi
Quoted 1 line · Peter Lieven

However, in a virtual machine I have not observed the above slow down

to

Quoted 2 lines

that extend
while the benefit of zero after free in a virtualisation environment

is

Quoted 4 lines

obvious:

1) zero pages can easily be merged by ksm or other technique.
2) zero (dup) pages are a lot faster to transfer in case of

migration.

The other approach is a memory page "discard" mechanism - which obviously requires more code changes than zeroing freed pages.

Quoted 6 lines
Quoted 4 lines

The advantage is that we don't take the brute-force and CPU intensive approach of zeroing pages.  It would be like a fine-grained ballooning feature.

I dont think that it is cpu intense. All user pages are zeroed anyway, but at allocation time it shouldnt be a big difference in terms of cpu power.

It's easy to find a scenario where eagerly zeroing pages is wasteful. Imagine a process that uses all of physical memory. Once it terminates the system is going to run processes that only use a small set of pages. It's pointless zeroing all those pages if we're not going to use them anymore.

Stefan

#5 lore raw
Quoted 27 lines · Stefan Hajnoczi
Quoted 26 lines · On Thu, Feb 23, 2012 at 7:08 PM, [email protected] <[email protected]> wrote:
Quoted 19 lines · Stefan Hajnoczi
Quoted 1 line · Peter Lieven

However, in a virtual machine I have not observed the above slow down

to

Quoted 2 lines

that extend
while the benefit of zero after free in a virtualisation environment

is

Quoted 4 lines

obvious:

1) zero pages can easily be merged by ksm or other technique.
2) zero (dup) pages are a lot faster to transfer in case of

migration.

The other approach is a memory page "discard" mechanism - which obviously requires more code changes than zeroing freed pages.

The advantage is that we don't take the brute-force and CPU intensive approach of zeroing pages.  It would be like a fine-grained ballooning feature.

Quoted 7 lines
Quoted 1 line

I dont think that it is cpu intense. All user pages are zeroed anyway, but at allocation time it shouldnt be a big difference in terms of cpu power.

It's easy to find a scenario where eagerly zeroing pages is wasteful. Imagine a process that uses all of physical memory.  Once it terminates the system is going to run processes that only use a small set of pages.  It's pointless zeroing all those pages if we're not going to use them anymore.

Perhaps the middle path is to zero pages but do it after a grace timeout. I wonder if this helps eliminate the 2-3% slowdown you noticed when compiling.

This requires no special host<->guest interfaces for discarding pages.

Stefan

#6 lore raw
Quoted 32 lines · Stefan Hajnoczi
Quoted 23 lines · Stefan Hajnoczi
Quoted 22 lines · On Thu, Feb 23, 2012 at 7:08 PM, [email protected] <[email protected]> wrote:
Quoted 19 lines · Stefan Hajnoczi
Quoted 1 line · Peter Lieven

However, in a virtual machine I have not observed the above slow down

to

Quoted 2 lines

that extend
while the benefit of zero after free in a virtualisation environment

is

Quoted 4 lines

obvious:

1) zero pages can easily be merged by ksm or other technique.
2) zero (dup) pages are a lot faster to transfer in case of

migration.

The other approach is a memory page "discard" mechanism - which obviously requires more code changes than zeroing freed pages.

The advantage is that we don't take the brute-force and CPU intensive approach of zeroing pages.  It would be like a fine-grained ballooning feature.

Quoted 7 lines
Quoted 1 line

I dont think that it is cpu intense. All user pages are zeroed anyway, but at allocation time it shouldnt be a big difference in terms of cpu power.

It's easy to find a scenario where eagerly zeroing pages is wasteful. Imagine a process that uses all of physical memory.  Once it terminates the system is going to run processes that only use a small set of pages.  It's pointless zeroing all those pages if we're not going to use them anymore.

Quoted 3 lines

Perhaps the middle path is to zero pages but do it after a grace timeout.  I wonder if this helps eliminate the 2-3% slowdown you noticed when compiling.

Gah, it's too early in the morning. I don't think this timer actually makes sense.

Stefan

#7 lore raw

Am 24.02.2012 um 08:23 schrieb Stefan Hajnoczi:

Quoted 33 lines · Stefan Hajnoczi
Quoted 32 lines · Stefan Hajnoczi
Quoted 30 lines · On Thu, Feb 23, 2012 at 7:08 PM, [email protected] <[email protected]> wrote:
Quoted 17 lines
Quoted 15 lines · Stefan Hajnoczi
Quoted 1 line · Peter Lieven

However, in a virtual machine I have not observed the above slow down

to

Quoted 2 lines

that extend
while the benefit of zero after free in a virtualisation environment

is

Quoted 4 lines

obvious:

1) zero pages can easily be merged by ksm or other technique.
2) zero (dup) pages are a lot faster to transfer in case of

migration.

The other approach is a memory page "discard" mechanism - which obviously requires more code changes than zeroing freed pages.

Quoted 6 lines
Quoted 4 lines

The advantage is that we don't take the brute-force and CPU intensive approach of zeroing pages. It would be like a fine-grained ballooning feature.

I dont think that it is cpu intense. All user pages are zeroed anyway, but at allocation time it shouldnt be a big difference in terms of cpu power.

It's easy to find a scenario where eagerly zeroing pages is wasteful. Imagine a process that uses all of physical memory. Once it terminates the system is going to run processes that only use a small set of pages. It's pointless zeroing all those pages if we're not going to use them anymore.

Quoted 6 lines
Quoted 3 lines

Perhaps the middle path is to zero pages but do it after a grace timeout. I wonder if this helps eliminate the 2-3% slowdown you noticed when compiling.

Gah, it's too early in the morning. I don't think this timer actually makes sense.

ok, that would be the idea of an ansynchronous page zeroing in the guest. i also think this is to complicated.

maybe the other idea is too simple:
is it possible to give the guest a hint that ksm is enabled on the host (lets say in a way like its done with kvmclock). if ksm is enabled on the host the administrator has already made the decision that performance is not so important and he/she is eager to save physical memory. what if and only if this flag is set switch from zero on allocate to zero after free. i think the whole thing is less than 10-20 lines of code. and its code that has been proven to be working well in grsecurity for ages.

this might introduce a little (2-3%) overhead, but only if there is a lot of non GFP_FREE memory is allocated, but its definitely faster than swapping.
of course, it has to be garanteed that this code does not slow down normal systems due to additionales branches (would it be enough to mark the if statements as unlikely?)

peter

peter

Quoted 2 lines

Stefan

#8 lore raw
In reply to Stefan Hajnoczi
Quoted 30 lines · Stefan Hajnoczi
Quoted 29 lines · Stefan Hajnoczi
Quoted 28 lines · Stefan Hajnoczi
Quoted 14 lines · On Thu, Feb 23, 2012 at 7:08 PM, [email protected]<[email protected]> wrote:
Quoted 12 lines · Stefan Hajnoczi
Quoted 1 line · Peter Lieven

However, in a virtual machine I have not observed the above slow down

to

Quoted 2 lines

that extend
while the benefit of zero after free in a virtualisation environment

is

Quoted 4 lines

obvious:

1) zero pages can easily be merged by ksm or other technique.
2) zero (dup) pages are a lot faster to transfer in case of

migration.

Quoted 8 lines
Quoted 7 lines

The other approach is a memory page "discard" mechanism - which obviously requires more code changes than zeroing freed pages.

The advantage is that we don't take the brute-force and CPU intensive approach of zeroing pages. It would be like a fine-grained ballooning feature.

I dont think that it is cpu intense. All user pages are zeroed anyway, but at allocation time it shouldnt be a big difference in terms of cpu power.

It's easy to find a scenario where eagerly zeroing pages is wasteful. Imagine a process that uses all of physical memory. Once it terminates the system is going to run processes that only use a small set of pages. It's pointless zeroing all those pages if we're not going to use them anymore.

Quoted 5 lines
Quoted 3 lines

Perhaps the middle path is to zero pages but do it after a grace timeout. I wonder if this helps eliminate the 2-3% slowdown you noticed when compiling.

Gah, it's too early in the morning. I don't think this timer actually makes sense.

do you think it makes then sense to make a patchset/proposal to notice a guest kernel about the presense of ksm in the host and switch to zero after free?

peter

Quoted 2 lines

Stefan

#9 lore raw
In reply to Stefan Hajnoczi
Quoted 30 lines · Stefan Hajnoczi
Quoted 29 lines · Stefan Hajnoczi
Quoted 28 lines · Stefan Hajnoczi
Quoted 14 lines · On Thu, Feb 23, 2012 at 7:08 PM, [email protected]<[email protected]> wrote:
Quoted 12 lines · Stefan Hajnoczi
Quoted 1 line · Peter Lieven

However, in a virtual machine I have not observed the above slow down

to

Quoted 2 lines

that extend
while the benefit of zero after free in a virtualisation environment

is

Quoted 4 lines

obvious:

1) zero pages can easily be merged by ksm or other technique.
2) zero (dup) pages are a lot faster to transfer in case of

migration.

Quoted 8 lines
Quoted 7 lines

The other approach is a memory page "discard" mechanism - which obviously requires more code changes than zeroing freed pages.

The advantage is that we don't take the brute-force and CPU intensive approach of zeroing pages. It would be like a fine-grained ballooning feature.

I dont think that it is cpu intense. All user pages are zeroed anyway, but at allocation time it shouldnt be a big difference in terms of cpu power.

It's easy to find a scenario where eagerly zeroing pages is wasteful. Imagine a process that uses all of physical memory. Once it terminates the system is going to run processes that only use a small set of pages. It's pointless zeroing all those pages if we're not going to use them anymore.

Quoted 5 lines
Quoted 3 lines

Perhaps the middle path is to zero pages but do it after a grace timeout. I wonder if this helps eliminate the 2-3% slowdown you noticed when compiling.

Gah, it's too early in the morning. I don't think this timer actually makes sense.

do you think it makes then sense to make a patchset/proposal to notice a guest kernel about the presense of ksm in the host and switch to zero after free?

peter

#10 lore raw
Quoted 52 lines · Peter Lieven
Quoted 51 lines · Stefan Hajnoczi
Quoted 47 lines · Stefan Hajnoczi
Quoted 35 lines · Stefan Hajnoczi
Quoted 24 lines · On Thu, Feb 23, 2012 at 7:08 PM, [email protected]<[email protected]> wrote:
Quoted 21 lines · Stefan Hajnoczi
Quoted 2 lines · Peter Lieven

However, in a virtual machine I have not observed the above slow down

to

Quoted 3 lines

that extend
while the benefit of zero after free in a virtualisation environment

is

Quoted 5 lines

obvious:

1) zero pages can easily be merged by ksm or other technique.
2) zero (dup) pages are a lot faster to transfer in case of

migration.

The other approach is a memory page "discard" mechanism - which obviously requires more code changes than zeroing freed pages.

Quoted 7 lines
Quoted 4 lines

The advantage is that we don't take the brute-force and CPU intensive approach of zeroing pages.  It would be like a fine-grained ballooning feature.

I dont think that it is cpu intense. All user pages are zeroed anyway, but at allocation time it shouldnt be a big difference in terms of cpu power.

Quoted 5 lines

It's easy to find a scenario where eagerly zeroing pages is wasteful. Imagine a process that uses all of physical memory.  Once it terminates the system is going to run processes that only use a small set of pages.  It's pointless zeroing all those pages if we're not going to use them anymore.

Perhaps the middle path is to zero pages but do it after a grace timeout.  I wonder if this helps eliminate the 2-3% slowdown you noticed when compiling.

Quoted 7 lines
Quoted 2 lines

Gah, it's too early in the morning.  I don't think this timer actually makes sense.

do you think it makes then sense to make a patchset/proposal to notice a guest
kernel about the presense of ksm in the host and switch to zero after free?

I think your idea is interesting - whether or not people are happy with it will depend on the performance impact. It seems reasonable to me.

Stefan

#11 lore raw
Quoted 43 lines · Stefan Hajnoczi
Quoted 42 lines · Peter Lieven
Quoted 40 lines · Stefan Hajnoczi
Quoted 28 lines · Stefan Hajnoczi
Quoted 26 lines · Stefan Hajnoczi
Quoted 24 lines · On Thu, Feb 23, 2012 at 7:08 PM, [email protected]<[email protected]> wrote:
Quoted 10 lines · Stefan Hajnoczi
Quoted 1 line · Peter Lieven

However, in a virtual machine I have not observed the above slow down

to

Quoted 2 lines

that extend
while the benefit of zero after free in a virtualisation environment

is

Quoted 4 lines

obvious:

1) zero pages can easily be merged by ksm or other technique.
2) zero (dup) pages are a lot faster to transfer in case of

Quoted 9 lines

migration.

The other approach is a memory page "discard" mechanism - which obviously requires more code changes than zeroing freed pages.

The advantage is that we don't take the brute-force and CPU intensive approach of zeroing pages. It would be like a fine-grained ballooning feature.

I dont think that it is cpu intense. All user pages are zeroed anyway, but at allocation time it shouldnt be a big difference in terms of cpu power.

Quoted 8 lines
Quoted 5 lines

It's easy to find a scenario where eagerly zeroing pages is wasteful. Imagine a process that uses all of physical memory. Once it terminates the system is going to run processes that only use a small set of pages. It's pointless zeroing all those pages if we're not going to use them anymore.

Perhaps the middle path is to zero pages but do it after a grace timeout. I wonder if this helps eliminate the 2-3% slowdown you noticed when compiling.

Gah, it's too early in the morning. I don't think this timer actually makes sense.

Quoted 6 lines
Quoted 3 lines

do you think it makes then sense to make a patchset/proposal to notice a guest
kernel about the presense of ksm in the host and switch to zero after free?

I think your idea is interesting - whether or not people are happy with it will depend on the performance impact. It seems reasonable to me.

could you support/help me in implementing and publishing this approach?

Peter

#12 lore raw
In reply to Stefan Hajnoczi
Quoted 8 lines · Stefan Hajnoczi
Quoted 2 lines

I dont think that it is cpu intense. All user pages are zeroed anyway, but at allocation time it shouldnt be a big difference in terms of cpu power.

It's easy to find a scenario where eagerly zeroing pages is wasteful. Imagine a process that uses all of physical memory. Once it terminates the system is going to run processes that only use a small set of pages. It's pointless zeroing all those pages if we're not going to use them anymore.

In the long term, we will use them, except if the guest is completely idle.

The scenario in which zeroing is expensive is when the page is refilled through DMA. In that case the zeroing was wasted. This is a pretty common scenario in pagecache intensive workloads.

Signature · 1 line
error compiling committee.c: too many arguments to function
#13 lore raw
Quoted 7 lines · Avi Kivity
Quoted 6 lines · Stefan Hajnoczi
Quoted 1 line

I dont think that it is cpu intense. All user pages are zeroed anyway, but at allocation time it shouldnt be a big difference in terms of cpu power.

It's easy to find a scenario where eagerly zeroing pages is wasteful. Imagine a process that uses all of physical memory. Once it terminates the system is going to run processes that only use a small set of pages. It's pointless zeroing all those pages if we're not going to use them anymore.

Quoted 6 lines

In the long term, we will use them, except if the guest is completely idle.

The scenario in which zeroing is expensive is when the page is refilled through DMA. In that case the zeroing was wasted. This is a pretty common scenario in pagecache intensive workloads.

Avi, what do you think of the proposal to give the guest vm a hint that the host is running ksm? In that case the administrator has already chosen that saving physical memory is more important than performance to him?

Peter

#14 lore raw
Quoted 13 lines · Peter Lieven
Quoted 12 lines · Avi Kivity
Quoted 8 lines · Stefan Hajnoczi
Quoted 3 lines

I dont think that it is cpu intense. All user pages are zeroed anyway, but at allocation time it shouldnt be a big difference in terms of cpu power.

It's easy to find a scenario where eagerly zeroing pages is wasteful. Imagine a process that uses all of physical memory. Once it terminates the system is going to run processes that only use a small set of pages. It's pointless zeroing all those pages if we're not going to use them anymore.

In the long term, we will use them, except if the guest is completely idle.

Quoted 8 lines
Quoted 4 lines

The scenario in which zeroing is expensive is when the page is refilled through DMA. In that case the zeroing was wasted. This is a pretty common scenario in pagecache intensive workloads.

Avi, what do you think of the proposal to give the guest vm a hint that the host is running ksm? In that case the administrator has already chosen that saving physical memory is more important than performance to him?

It makes some sense. Perhaps through the balloon device, a flag that indicates that voluntary ballooning will be gratefully accepted.

Signature · 1 line
error compiling committee.c: too many arguments to function
#15 lore raw
In reply to Stefan Hajnoczi
Quoted 12 lines · Stefan Hajnoczi
Quoted 7 lines · Peter Lieven

However, in a virtual machine I have not observed the above slow down to that extend
while the benefit of zero after free in a virtualisation environment is obvious:

1) zero pages can easily be merged by ksm or other technique.
2) zero (dup) pages are a lot faster to transfer in case of migration.

The other approach is a memory page "discard" mechanism - which obviously requires more code changes than zeroing freed pages.

Quoted 7 lines

The advantage is that we don't take the brute-force and CPU intensive approach of zeroing pages. It would be like a fine-grained ballooning feature.

I hope someone will follow up saying this has already been done or prototyped :).

That was attempted. It is called "page hinting", but AFAIK due to complex locking issue attempt was abandoned.

--
Gleb.

#16 lore raw
In reply to Stefan Hajnoczi
Quoted 12 lines · Stefan Hajnoczi
Quoted 7 lines · Peter Lieven

However, in a virtual machine I have not observed the above slow down to that extend
while the benefit of zero after free in a virtualisation environment is obvious:

1) zero pages can easily be merged by ksm or other technique.
2) zero (dup) pages are a lot faster to transfer in case of migration.

The other approach is a memory page "discard" mechanism - which obviously requires more code changes than zeroing freed pages.

Quoted 6 lines

The advantage is that we don't take the brute-force and CPU intensive approach of zeroing pages. It would be like a fine-grained ballooning feature.

I hope someone will follow up saying this has already been done or prototyped :).

It already exists - that's the balloon code. Right now it's host driven, but maybe we can modify it to allow the guest to initiate balloon inflations.

Signature · 1 line
error compiling committee.c: too many arguments to function