* [PATCH RFC] KVM MMU: fix hashing for TDP and non-paging modes
@ 2010-04-22 21:15 Eric Northup
2010-04-23 11:11 ` Avi Kivity
2010-04-26 21:30 ` Marcelo Tosatti
0 siblings, 2 replies; 7+ messages in thread
From: Eric Northup @ 2010-04-22 21:15 UTC (permalink / raw)
To: kvm
I've been reading the x86's mmu.c recently and had been wondering
about something. Avi's recent mmu documentation (thanks!) seems to
have confirmed my understanding of how the shadow paging is supposed
to be working. In TDP mode, when mmu_alloc_roots() calls
kvm_mmu_get_page(), why does it pass (vcpu->arch.cr3 >> PAGE_SHIFT) or
(vcpu->arch.mmu.pae_root[i]) as gfn?
It seems to me that in TDP mode, gfn should be either zero for the
root page table, or 0/1GB/2GB/3GB (for PAE page tables).
The existing behavior can lead to multiple, semantically-identical TDP
roots being created by mmu_alloc_roots, depending on the VCPU's CR3 at
the time that mmu_alloc_roots was called. But the nested page tables
should be* independent of the VCPU state. That wastes some memory and
causes extra page faults while populating the extra copies of the page
tables.
*assuming that we aren't modeling per-VCPU state that might change the
physical address map as seen by that VCPU, such as setting the APIC
base to an address overlapping RAM.
All feedback would be welcome, since I'm new to this system! A
strawman patch follows.
thanks,
-Eric
--
For TDP mode, avoid creating multiple page table roots for the single
guest-to-host physical address map by fixing the inputs used for the
shadow page table hash in mmu_alloc_roots().
Signed-off-by: Eric Northup <digitaleric@google.com>
---
arch/x86/kvm/mmu.c | 12 ++++++++----
1 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
index ddfa865..9696d65 100644
--- a/arch/x86/kvm/mmu.c
+++ b/arch/x86/kvm/mmu.c
@@ -2059,10 +2059,12 @@ static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
hpa_t root = vcpu->arch.mmu.root_hpa;
ASSERT(!VALID_PAGE(root));
- if (tdp_enabled)
- direct = 1;
if (mmu_check_root(vcpu, root_gfn))
return 1;
+ if (tdp_enabled) {
+ direct = 1;
+ root_gfn = 0;
+ }
sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
PT64_ROOT_LEVEL, direct,
ACC_ALL, NULL);
@@ -2072,8 +2074,6 @@ static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
return 0;
}
direct = !is_paging(vcpu);
- if (tdp_enabled)
- direct = 1;
for (i = 0; i < 4; ++i) {
hpa_t root = vcpu->arch.mmu.pae_root[i];
@@ -2089,6 +2089,10 @@ static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
root_gfn = 0;
if (mmu_check_root(vcpu, root_gfn))
return 1;
+ if (tdp_enabled) {
+ direct = 1;
+ root_gfn = i << 30;
+ }
sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
PT32_ROOT_LEVEL, direct,
ACC_ALL, NULL);
--
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH RFC] KVM MMU: fix hashing for TDP and non-paging modes
2010-04-22 21:15 [PATCH RFC] KVM MMU: fix hashing for TDP and non-paging modes Eric Northup
@ 2010-04-23 11:11 ` Avi Kivity
2010-04-26 9:24 ` Avi Kivity
2010-04-26 21:30 ` Marcelo Tosatti
1 sibling, 1 reply; 7+ messages in thread
From: Avi Kivity @ 2010-04-23 11:11 UTC (permalink / raw)
To: Eric Northup; +Cc: kvm
On 04/23/2010 12:15 AM, Eric Northup wrote:
> I've been reading the x86's mmu.c recently and had been wondering
> about something. Avi's recent mmu documentation (thanks!) seems to
> have confirmed my understanding of how the shadow paging is supposed
> to be working.
Wasn't it a lot more fun to understand it from the code? reading the
documentation is way to easy.
> In TDP mode, when mmu_alloc_roots() calls
> kvm_mmu_get_page(), why does it pass (vcpu->arch.cr3>> PAGE_SHIFT) or
> (vcpu->arch.mmu.pae_root[i]) as gfn?
>
Historical accident. Luckily, no harmful effects other than wasting a
bit of cpu cycles every now and then.
> It seems to me that in TDP mode, gfn should be either zero for the
> root page table, or 0/1GB/2GB/3GB (for PAE page tables).
>
> The existing behavior can lead to multiple, semantically-identical TDP
> roots being created by mmu_alloc_roots, depending on the VCPU's CR3 at
> the time that mmu_alloc_roots was called. But the nested page tables
> should be* independent of the VCPU state. That wastes some memory and
> causes extra page faults while populating the extra copies of the page
> tables.
>
Yeah.
> *assuming that we aren't modeling per-VCPU state that might change the
> physical address map as seen by that VCPU, such as setting the APIC
> base to an address overlapping RAM.
>
We don't actually support that.
> All feedback would be welcome, since I'm new to this system! A
> strawman patch follows.
>
>
Patch is correct, but I have already fixed this in a more extensive
patch set (that also folds the 32-bit and 64-bit cases together, etc.)
and I'm too lazy to rebase on top of yours.
--
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH RFC] KVM MMU: fix hashing for TDP and non-paging modes
2010-04-23 11:11 ` Avi Kivity
@ 2010-04-26 9:24 ` Avi Kivity
0 siblings, 0 replies; 7+ messages in thread
From: Avi Kivity @ 2010-04-26 9:24 UTC (permalink / raw)
To: Eric Northup; +Cc: kvm, Marcelo Tosatti
On 04/23/2010 02:11 PM, Avi Kivity wrote:
>
>> All feedback would be welcome, since I'm new to this system! A
>> strawman patch follows.
>>
>
> Patch is correct, but I have already fixed this in a more extensive
> patch set (that also folds the 32-bit and 64-bit cases together, etc.)
> and I'm too lazy to rebase on top of yours.
Well my patchset is broken. Marcelo, please apply this and I'll rebase
and fix.
--
error compiling committee.c: too many arguments to function
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH RFC] KVM MMU: fix hashing for TDP and non-paging modes
2010-04-22 21:15 [PATCH RFC] KVM MMU: fix hashing for TDP and non-paging modes Eric Northup
2010-04-23 11:11 ` Avi Kivity
@ 2010-04-26 21:30 ` Marcelo Tosatti
2010-04-26 21:46 ` Marcelo Tosatti
1 sibling, 1 reply; 7+ messages in thread
From: Marcelo Tosatti @ 2010-04-26 21:30 UTC (permalink / raw)
To: Eric Northup; +Cc: kvm
On Thu, Apr 22, 2010 at 02:15:14PM -0700, Eric Northup wrote:
> I've been reading the x86's mmu.c recently and had been wondering
> about something. Avi's recent mmu documentation (thanks!) seems to
> have confirmed my understanding of how the shadow paging is supposed
> to be working. In TDP mode, when mmu_alloc_roots() calls
> kvm_mmu_get_page(), why does it pass (vcpu->arch.cr3 >> PAGE_SHIFT) or
> (vcpu->arch.mmu.pae_root[i]) as gfn?
>
> It seems to me that in TDP mode, gfn should be either zero for the
> root page table, or 0/1GB/2GB/3GB (for PAE page tables).
>
> The existing behavior can lead to multiple, semantically-identical TDP
> roots being created by mmu_alloc_roots, depending on the VCPU's CR3 at
> the time that mmu_alloc_roots was called. But the nested page tables
> should be* independent of the VCPU state. That wastes some memory and
> causes extra page faults while populating the extra copies of the page
> tables.
>
> *assuming that we aren't modeling per-VCPU state that might change the
> physical address map as seen by that VCPU, such as setting the APIC
> base to an address overlapping RAM.
>
> All feedback would be welcome, since I'm new to this system! A
> strawman patch follows.
>
> thanks,
> -Eric
>
> --
>
> For TDP mode, avoid creating multiple page table roots for the single
> guest-to-host physical address map by fixing the inputs used for the
> shadow page table hash in mmu_alloc_roots().
>
> Signed-off-by: Eric Northup <digitaleric@google.com>
> ---
> arch/x86/kvm/mmu.c | 12 ++++++++----
> 1 files changed, 8 insertions(+), 4 deletions(-)
>
> diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
> index ddfa865..9696d65 100644
> --- a/arch/x86/kvm/mmu.c
> +++ b/arch/x86/kvm/mmu.c
> @@ -2059,10 +2059,12 @@ static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
> hpa_t root = vcpu->arch.mmu.root_hpa;
>
> ASSERT(!VALID_PAGE(root));
> - if (tdp_enabled)
> - direct = 1;
> if (mmu_check_root(vcpu, root_gfn))
> return 1;
> + if (tdp_enabled) {
> + direct = 1;
> + root_gfn = 0;
> + }
> sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
> PT64_ROOT_LEVEL, direct,
> ACC_ALL, NULL);
> @@ -2072,8 +2074,6 @@ static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
> return 0;
> }
> direct = !is_paging(vcpu);
> - if (tdp_enabled)
> - direct = 1;
> for (i = 0; i < 4; ++i) {
> hpa_t root = vcpu->arch.mmu.pae_root[i];
>
> @@ -2089,6 +2089,10 @@ static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
> root_gfn = 0;
> if (mmu_check_root(vcpu, root_gfn))
> return 1;
> + if (tdp_enabled) {
> + direct = 1;
> + root_gfn = i << 30;
> + }
> sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
> PT32_ROOT_LEVEL, direct,
> ACC_ALL, NULL);
There is no need to allocate 4 different roots for TDP tables if
kvm_x86_ops->get_tdp_level() == PT64_ROOT_LEVEL.
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH RFC] KVM MMU: fix hashing for TDP and non-paging modes
2010-04-26 21:30 ` Marcelo Tosatti
@ 2010-04-26 21:46 ` Marcelo Tosatti
2010-04-27 0:00 ` Eric Northup
0 siblings, 1 reply; 7+ messages in thread
From: Marcelo Tosatti @ 2010-04-26 21:46 UTC (permalink / raw)
To: Eric Northup; +Cc: kvm
On Mon, Apr 26, 2010 at 06:30:00PM -0300, Marcelo Tosatti wrote:
> > @@ -2089,6 +2089,10 @@ static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
> > root_gfn = 0;
> > if (mmu_check_root(vcpu, root_gfn))
> > return 1;
> > + if (tdp_enabled) {
> > + direct = 1;
> > + root_gfn = i << 30;
> > + }
> > sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
> > PT32_ROOT_LEVEL, direct,
> > ACC_ALL, NULL);
>
> There is no need to allocate 4 different roots for TDP tables if
> kvm_x86_ops->get_tdp_level() == PT64_ROOT_LEVEL.
Doh, and your patch does not. But it does not apply to kvm.git -next
branch, can you regenerate please?
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH RFC] KVM MMU: fix hashing for TDP and non-paging modes
2010-04-26 21:46 ` Marcelo Tosatti
@ 2010-04-27 0:00 ` Eric Northup
2010-04-27 12:31 ` Marcelo Tosatti
0 siblings, 1 reply; 7+ messages in thread
From: Eric Northup @ 2010-04-27 0:00 UTC (permalink / raw)
To: Marcelo Tosatti; +Cc: kvm
On Mon, Apr 26, 2010 at 2:46 PM, Marcelo Tosatti <mtosatti@redhat.com> wrote:
> Doh, and your patch does not. But it does not apply to kvm.git -next
> branch, can you regenerate please?
--
For TDP mode, avoid creating multiple page table roots for the single
guest-to-host physical address map by fixing the inputs used for the
shadow page table hash in mmu_alloc_roots().
Signed-off-by: Eric Northup <digitaleric@google.com>
---
diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
index ddfa865..9696d65 100644
--- a/arch/x86/kvm/mmu.c
+++ b/arch/x86/kvm/mmu.c
@@ -2059,10 +2059,12 @@ static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
hpa_t root = vcpu->arch.mmu.root_hpa;
ASSERT(!VALID_PAGE(root));
- if (tdp_enabled)
- direct = 1;
if (mmu_check_root(vcpu, root_gfn))
return 1;
+ if (tdp_enabled) {
+ direct = 1;
+ root_gfn = 0;
+ }
sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
PT64_ROOT_LEVEL, direct,
ACC_ALL, NULL);
@@ -2072,8 +2074,6 @@ static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
return 0;
}
direct = !is_paging(vcpu);
- if (tdp_enabled)
- direct = 1;
for (i = 0; i < 4; ++i) {
hpa_t root = vcpu->arch.mmu.pae_root[i];
@@ -2089,6 +2089,10 @@ static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
root_gfn = 0;
if (mmu_check_root(vcpu, root_gfn))
return 1;
+ if (tdp_enabled) {
+ direct = 1;
+ root_gfn = i << 30;
+ }
sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
PT32_ROOT_LEVEL, direct,
ACC_ALL, NULL);
--
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH RFC] KVM MMU: fix hashing for TDP and non-paging modes
2010-04-27 0:00 ` Eric Northup
@ 2010-04-27 12:31 ` Marcelo Tosatti
0 siblings, 0 replies; 7+ messages in thread
From: Marcelo Tosatti @ 2010-04-27 12:31 UTC (permalink / raw)
To: Eric Northup; +Cc: kvm
On Mon, Apr 26, 2010 at 05:00:05PM -0700, Eric Northup wrote:
> On Mon, Apr 26, 2010 at 2:46 PM, Marcelo Tosatti <mtosatti@redhat.com> wrote:
> > Doh, and your patch does not. But it does not apply to kvm.git -next
> > branch, can you regenerate please?
>
> --
>
> For TDP mode, avoid creating multiple page table roots for the single
> guest-to-host physical address map by fixing the inputs used for the
> shadow page table hash in mmu_alloc_roots().
>
> Signed-off-by: Eric Northup <digitaleric@google.com>
Applied, thanks.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2010-04-27 13:01 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-04-22 21:15 [PATCH RFC] KVM MMU: fix hashing for TDP and non-paging modes Eric Northup
2010-04-23 11:11 ` Avi Kivity
2010-04-26 9:24 ` Avi Kivity
2010-04-26 21:30 ` Marcelo Tosatti
2010-04-26 21:46 ` Marcelo Tosatti
2010-04-27 0:00 ` Eric Northup
2010-04-27 12:31 ` Marcelo Tosatti
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox