Skip to contentSkip to conversation

emulate accessed bit for EPT

PATCH Applied Rik van Riel 6 participants 12 messages Feb 3, 2010 – Feb 8, 2010

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 emulate accessed bit for EPT

Currently KVM pretends that pages with EPT mappings never got accessed. This has some side effects in the VM, like swapping out actively used guest pages and needlessly breaking up actively used hugepages.

We can avoid those very costly side effects by emulating the accessed bit for EPT PTEs, which should only be slightly costly because pages pass through page_referenced infrequently.

TLB flushing is taken care of by kvm_mmu_notifier_clear_flush_young().

This seems to help prevent KVM guests from being swapped out when they should not on my system.

---
Jeff, does this patch fix the issue you saw a few months ago, with a 256MB KVM guest in a cgroup limited to 128GB memory?

 arch/x86/kvm/mmu.c |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu.c +8 -2
index 89a49fb..6101615 100644
@@ -856,9 +856,15 @@ static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
 	u64 *spte;
 	int young = 0;
 
-	/* always return old for EPT */
+	/*
+	 * Emulate the accessed bit for EPT, by checking if this page has
+	 * an EPT mapping, and clearing it if it does. On the next access,
+	 * a new EPT mapping will be established.
+	 * This has some overhead, but not as much as the cost of swapping
+	 * out actively used pages or breaking up actively used hugepages.
+	 */
 	if (!shadow_accessed_mask)
-		return 0;
+		return kvm_unmap_rmapp(kvm, rmapp, data);
 
 	spte = rmap_next(kvm, rmapp, NULL);
 	while (spte) {
#1 lore raw
Quoted 32 lines · Rik van Riel

Currently KVM pretends that pages with EPT mappings never got accessed. This has some side effects in the VM, like swapping out actively used guest pages and needlessly breaking up actively used hugepages.

We can avoid those very costly side effects by emulating the accessed bit for EPT PTEs, which should only be slightly costly because pages pass through page_referenced infrequently.

TLB flushing is taken care of by kvm_mmu_notifier_clear_flush_young().

This seems to help prevent KVM guests from being swapped out when they should not on my system.

---
Jeff, does this patch fix the issue you saw a few months ago, with a 256MB KVM guest in a cgroup limited to 128GB memory?

 arch/x86/kvm/mmu.c |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu.c +1 -1
index 89a49fb..6101615 100644
@@ -856,9 +856,15 @@ static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
 	u64 *spte;
 	int young = 0;
 
-	/* always return old for EPT */
+	/*

arch/x86/kvm/mmu.c static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp, · line 868

Quoted 10 lines
arch/x86/kvm/mmu.c +7 -1
+	 * Emulate the accessed bit for EPT, by checking if this page has
+	 * an EPT mapping, and clearing it if it does. On the next access,
+	 * a new EPT mapping will be established.
+	 * This has some overhead, but not as much as the cost of swapping
+	 * out actively used pages or breaking up actively used hugepages.
+	 */
 	if (!shadow_accessed_mask)
-		return 0;
+		return kvm_unmap_rmapp(kvm, rmapp, data);
 

Quite a clever implementation, one side effect is that one would see a larger number of minor faults with EPT enabled and an increase in allocation/frees of rmap entries, but that can be easily explained.

Signature · 1 line
	Balbir
#2 lore raw
Quoted 7 lines · Balbir Singh
Quoted 5 lines · Rik van Riel

Currently KVM pretends that pages with EPT mappings never got accessed. This has some side effects in the VM, like swapping out actively used guest pages and needlessly breaking up actively used hugepages.

Quoted 7 lines
Quoted 3 lines

We can avoid those very costly side effects by emulating the accessed bit for EPT PTEs, which should only be slightly costly because pages pass through page_referenced infrequently.

Quite a clever implementation, one side effect is that one would see a larger number of minor faults with EPT enabled and an increase in allocation/frees of rmap entries, but that can be easily explained.

I suspect it won't be very many. I have been monitoring /proc/meminfo on my system while testing this patch, and it is quite typical that the size of the inactive anon list does not change for minutes at a time.

In other words, no pages are moved onto or off of the inactive anon list for several minutes. That corresponds to a very small number of minor faults introduced by my patch.

Of course, when the system is swapping, we will have more minor faults. However, minor faults should be less of a performance issue than major faults :)

Signature · 1 line
All rights reversed.
#3 lore raw
Quoted 21 lines · Rik van Riel
Quoted 7 lines · Balbir Singh
Quoted 5 lines · Rik van Riel

Currently KVM pretends that pages with EPT mappings never got accessed. This has some side effects in the VM, like swapping out actively used guest pages and needlessly breaking up actively used hugepages.

Quoted 7 lines
Quoted 3 lines

We can avoid those very costly side effects by emulating the accessed bit for EPT PTEs, which should only be slightly costly because pages pass through page_referenced infrequently.

Quite a clever implementation, one side effect is that one would see a larger number of minor faults with EPT enabled and an increase in allocation/frees of rmap entries, but that can be easily explained.

I suspect it won't be very many. I have been monitoring /proc/meminfo on my system while testing this patch, and it is quite typical that the size of the inactive anon list does not change for minutes at a time.

Quoted 9 lines

In other words, no pages are moved onto or off of the inactive anon list for several minutes. That corresponds to a very small number of minor faults introduced by my patch.

Of course, when the system is swapping, we will have more minor faults. However, minor faults should be less of a performance issue than major faults :)

I do agree with you.

Signature · 1 line
	Balbir
#4 lore raw
Quoted 26 lines · Balbir Singh
Quoted 24 lines · Rik van Riel
Quoted 7 lines · Balbir Singh
Quoted 5 lines · Rik van Riel

Currently KVM pretends that pages with EPT mappings never got accessed. This has some side effects in the VM, like swapping out actively used guest pages and needlessly breaking up actively used hugepages.

Quoted 6 lines
Quoted 3 lines

We can avoid those very costly side effects by emulating the accessed bit for EPT PTEs, which should only be slightly costly because pages pass through page_referenced infrequently.

Quite a clever implementation, one side effect is that one would see a larger number of minor faults with EPT enabled and an increase in allocation/frees of rmap entries, but that can be easily explained.

I suspect it won't be very many. I have been monitoring /proc/meminfo on my system while testing this patch, and it is quite typical that the size of the inactive anon list does not change for minutes at a time.

In other words, no pages are moved onto or off of the inactive anon list for several minutes. That corresponds to a very small number of minor faults introduced by my patch.

Quoted 6 lines
Quoted 4 lines

Of course, when the system is swapping, we will have more minor faults. However, minor faults should be less of a performance issue than major faults :)

I do agree with you.

After 20 hours of uptime, it appears that this patch has resolved the "KVM guests get swapped while buffer and page cache stay in memory" problem my home system was experiencing.

#5 lore raw
Quoted 32 lines · Rik van Riel
Quoted 31 lines · Balbir Singh
Quoted 28 lines · Rik van Riel
Quoted 7 lines · Balbir Singh
Quoted 5 lines · Rik van Riel

Currently KVM pretends that pages with EPT mappings never got accessed. This has some side effects in the VM, like swapping out actively used guest pages and needlessly breaking up actively used hugepages.

Quoted 6 lines
Quoted 3 lines

We can avoid those very costly side effects by emulating the accessed bit for EPT PTEs, which should only be slightly costly because pages pass through page_referenced infrequently.

Quite a clever implementation, one side effect is that one would see a larger number of minor faults with EPT enabled and an increase in allocation/frees of rmap entries, but that can be easily explained.

I suspect it won't be very many. I have been monitoring /proc/meminfo on my system while testing this patch, and it is quite typical that the size of the inactive anon list does not change for minutes at a time.

In other words, no pages are moved onto or off of the inactive anon list for several minutes. That corresponds to a very small number of minor faults introduced by my patch.

Of course, when the system is swapping, we will have more minor faults. However, minor faults should be less of a performance issue than major faults :)

Quoted 5 lines
Quoted 1 line

I do agree with you.

After 20 hours of uptime, it appears that this patch has resolved the "KVM guests get swapped while buffer and page cache stay in memory" problem my home system was experiencing.

Is this with cgroups enabled as defined by the setup Jeff had?

Signature · 1 line
	Balbir
#6 lore raw
In reply to Rik van Riel
Quoted 9 lines · Rik van Riel

I suspect it won't be very many. I have been monitoring /proc/meminfo on my system while testing this patch, and it is quite typical that the size of the inactive anon list does not change for minutes at a time.

In other words, no pages are moved onto or off of the inactive anon list for several minutes. That corresponds to a very small number of minor faults introduced by my patch.

When there's light VM pressure, ideally there should be zero overhead caused by the patch. When there is VM pressure this will avoid some unnecessary I/O which should outweight the minor faults. It should be a good default behavior.

#7 lore raw
Quoted 11 lines · Andrea Arcangeli
Quoted 9 lines · Rik van Riel

I suspect it won't be very many. I have been monitoring /proc/meminfo on my system while testing this patch, and it is quite typical that the size of the inactive anon list does not change for minutes at a time.

In other words, no pages are moved onto or off of the inactive anon list for several minutes. That corresponds to a very small number of minor faults introduced by my patch.

Quoted 4 lines

When there's light VM pressure, ideally there should be zero overhead caused by the patch. When there is VM pressure this will avoid some unnecessary I/O which should outweight the minor faults. It should be a good default behavior.

Agree.

But perhaps a module parameter to turn accessed bit emulation off might be handy in the future?

#8 lore raw
Quoted 2 lines · Marcelo Tosatti

But perhaps a module parameter to turn accessed bit emulation off might be handy in the future?

Maybe, but somebody should show that this can overall become a downside, which I doubt... I think if it does, the VM is to blame for calling page_referenced when there is no point to do so just yet.

#9 lore raw
Quoted 7 lines · Andrea Arcangeli
Quoted 2 lines · Marcelo Tosatti

But perhaps a module parameter to turn accessed bit emulation off might be handy in the future?

Maybe, but somebody should show that this can overall become a downside, which I doubt... I think if it does, the VM is to blame for calling page_referenced when there is no point to do so just yet.

Agreed. ACK.

#10 lore raw
In reply to Rik van Riel
Quoted 2 lines · Rik van Riel

Jeff, does this patch fix the issue you saw a few months ago, with a 256MB KVM guest in a cgroup limited to 128GB memory?

Hum, let me dust off that workload and give it a shot...

Jeff

Applied
#11 lore raw
In reply to Rik van Riel
Quoted 5 lines · Rik van Riel

Currently KVM pretends that pages with EPT mappings never got accessed. This has some side effects in the VM, like swapping out actively used guest pages and needlessly breaking up actively used hugepages.

Quoted 10 lines

We can avoid those very costly side effects by emulating the accessed bit for EPT PTEs, which should only be slightly costly because pages pass through page_referenced infrequently.

TLB flushing is taken care of by kvm_mmu_notifier_clear_flush_young().

This seems to help prevent KVM guests from being swapped out when they should not on my system.

Applied, thanks.

arch/x86/kvm/mmu.c static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp, · line 867

Quoted 13 lines
arch/x86/kvm/mmu.c +8 -2
-	/* always return old for EPT */
+	/*
+	 * Emulate the accessed bit for EPT, by checking if this page has
+	 * an EPT mapping, and clearing it if it does. On the next access,
+	 * a new EPT mapping will be established.
+	 * This has some overhead, but not as much as the cost of swapping
+	 * out actively used pages or breaking up actively used hugepages.
+	 */
  	if (!shadow_accessed_mask)
-		return 0;
+		return kvm_unmap_rmapp(kvm, rmapp, data);

This could be optimized by using a software-available bit for 'present' and the rwx bits for young, that is:

(present, rwx) -> the page is present and recently accessed, will not
cause EPT violation
(present, !rwx) -> page is present but old, will cause EPT violation
but not rmap games and get_user_pages_fast().

However that's best done later if ever.

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