Skip to contentSkip to conversation

KVM: SVM: obey guest PAT

PATCH Applied Paolo Bonzini 4 participants 4 messages Oct 26, 2017 – Nov 10, 2017

Subsystems (4)
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

PATCH 1/1 KVM: SVM: obey guest PAT

For many years some users of assigned devices have reported worse performance on AMD processors with NPT than on AMD without NPT, Intel or bare metal.

The reason turned out to be that SVM is discarding the guest PAT setting and uses the default (PA0=PA4=WB, PA1=PA5=WT, PA2=PA6=UC-, PA3=UC). The guest might be using a different setting, and especially might want write combining but isn't getting it (instead getting slow UC or UC- accesses).

Thanks a lot to [email protected] for noticing the relation to the g_pat setting. The patch has been tested also by a bunch of people on VFIO users forums.

Fixes 709ddebf81cb40e3c36c6109a7892e8b93a09464
---
 arch/x86/kvm/svm.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/kvm/svm.c +7 -0
index af256b786a70..af09baa3d736 100644
@@ -3626,6 +3626,13 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
 	u32 ecx = msr->index;
 	u64 data = msr->data;
 	switch (ecx) {
+	case MSR_IA32_CR_PAT:
+		if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
+			return 1;
+		vcpu->arch.pat = data;
+		svm->vmcb->save.g_pat = data;
+		mark_dirty(svm->vmcb, VMCB_NPT);
+		break;
 	case MSR_IA32_TSC:
 		kvm_write_tsc(vcpu, msr);
 		break;
Signature · 1 line
2.14.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 31 lines · Paolo Bonzini

For many years some users of assigned devices have reported worse performance on AMD processors with NPT than on AMD without NPT, Intel or bare metal.

The reason turned out to be that SVM is discarding the guest PAT setting and uses the default (PA0=PA4=WB, PA1=PA5=WT, PA2=PA6=UC-, PA3=UC). The guest might be using a different setting, and especially might want write combining but isn't getting it (instead getting slow UC or UC- accesses).

Thanks a lot to [email protected] for noticing the relation to the g_pat setting. The patch has been tested also by a bunch of people on VFIO users forums.

Fixes 709ddebf81cb40e3c36c6109a7892e8b93a09464
---
 arch/x86/kvm/svm.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/kvm/svm.c +0 -0
index af256b786a70..af09baa3d736 100644
@@ -3626,6 +3626,13 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
 	u32 ecx = msr->index;
 	u64 data = msr->data;
 	switch (ecx) {

arch/x86/kvm/svm.c static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) · line 3638

Quoted 10 lines
arch/x86/kvm/svm.c +7 -0
+	case MSR_IA32_CR_PAT:
+		if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
+			return 1;
+		vcpu->arch.pat = data;
+		svm->vmcb->save.g_pat = data;
+		mark_dirty(svm->vmcb, VMCB_NPT);
+		break;
 	case MSR_IA32_TSC:
 		kvm_write_tsc(vcpu, msr);
 		break;

Although no SVM expert, looking at the way it is handled on VMX, this looks good to me.

Signature · 3 lines
Thanks,

David
Tested-by
#2 lore raw
Quoted 46 lines · David Hildenbrand
Quoted 31 lines · Paolo Bonzini

For many years some users of assigned devices have reported worse performance on AMD processors with NPT than on AMD without NPT, Intel or bare metal.

The reason turned out to be that SVM is discarding the guest PAT setting and uses the default (PA0=PA4=WB, PA1=PA5=WT, PA2=PA6=UC-, PA3=UC). The guest might be using a different setting, and especially might want write combining but isn't getting it (instead getting slow UC or UC- accesses).

Thanks a lot to [email protected] for noticing the relation to the g_pat setting. The patch has been tested also by a bunch of people on VFIO users forums.

Fixes 709ddebf81cb40e3c36c6109a7892e8b93a09464
---
 arch/x86/kvm/svm.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/kvm/svm.c +0 -0
index af256b786a70..af09baa3d736 100644
@@ -3626,6 +3626,13 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
      u32 ecx = msr->index;
      u64 data = msr->data;
      switch (ecx) {

arch/x86/kvm/svm.c static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) · line 3638

Quoted 10 lines
arch/x86/kvm/svm.c +7 -0
+     case MSR_IA32_CR_PAT:
+             if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
+                     return 1;
+             vcpu->arch.pat = data;
+             svm->vmcb->save.g_pat = data;
+             mark_dirty(svm->vmcb, VMCB_NPT);
+             break;
      case MSR_IA32_TSC:
              kvm_write_tsc(vcpu, msr);
              break;

Although no SVM expert, looking at the way it is handled on VMX, this looks good to me.

Quoted 7 lines

--

Thanks,

David

You're a legend.

Thanks,
Sarnex

#3 lore raw
In reply to Paolo Bonzini

2017-10-26 09:13+0200, Paolo Bonzini:

Quoted 10 lines

For many years some users of assigned devices have reported worse performance on AMD processors with NPT than on AMD without NPT, Intel or bare metal.

The reason turned out to be that SVM is discarding the guest PAT setting and uses the default (PA0=PA4=WB, PA1=PA5=WT, PA2=PA6=UC-, PA3=UC). The guest might be using a different setting, and especially might want write combining but isn't getting it (instead getting slow UC or UC- accesses).

Quoted 8 lines

Thanks a lot to [email protected] for noticing the relation to the g_pat setting. The patch has been tested also by a bunch of people on VFIO users forums.

Fixes 709ddebf81cb40e3c36c6109a7892e8b93a09464

Applied, thanks.