Skip to contentSkip to conversation

KVM: x86: do not retain disabled or invalid pvclock address

PATCH Marcelo Tosatti 2 participants 3 messages Aug 23, 2012 – Sep 9, 2012

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

In case an invalid or disabled gpa is written to the SYSTEM_TIME MSR, do not retain its value. This is not documented behaviour, nor should be supported.

Also clear it on system reset. Not doing so can hide bugs.

arch/x86/kvm/x86.c +7 -2
index e00050c..ed4bfb7 100644
@@ -1528,6 +1528,7 @@ static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
 
 static void kvmclock_reset(struct kvm_vcpu *vcpu)
 {
+	vcpu->arch.time = 0;
 	if (vcpu->arch.time_page) {
 		kvm_release_page_dirty(vcpu->arch.time_page);
 		vcpu->arch.time_page = NULL;
@@ -1632,8 +1633,10 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 		kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
 
 		/* we verify if the enable bit is set... */
-		if (!(data & 1))
+		if (!(data & 1)) {
+			vcpu->arch.time = 0;
 			break;
+		}
 
 		/* ...but clean it before doing the actual write */
 		vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
@@ -1641,8 +1644,10 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 		vcpu->arch.time_page =
 				gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
 
-		if (is_error_page(vcpu->arch.time_page))
+		if (is_error_page(vcpu->arch.time_page)) {
 			vcpu->arch.time_page = NULL;
+			vcpu->arch.time = 0;
+		}
 
 		break;
 	}
#1 lore raw
Quoted 22 lines · Marcelo Tosatti

In case an invalid or disabled gpa is written to the SYSTEM_TIME MSR, do not retain its value. This is not documented behaviour, nor should be supported.

Also clear it on system reset. Not doing so can hide bugs.

arch/x86/kvm/x86.c +1 -0
index e00050c..ed4bfb7 100644
@@ -1528,6 +1528,7 @@ static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
 
 static void kvmclock_reset(struct kvm_vcpu *vcpu)
 {
+	vcpu->arch.time = 0;
 	if (vcpu->arch.time_page) {
 		kvm_release_page_dirty(vcpu->arch.time_page);
 		vcpu->arch.time_page = NULL;

arch/x86/kvm/x86.c int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1637

Quoted 6 lines
arch/x86/kvm/x86.c +2 -1
@@ -1632,8 +1633,10 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 		kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
 
 		/* we verify if the enable bit is set... */
-		if (!(data & 1))
+		if (!(data & 1)) {
+			vcpu->arch.time = 0;

Should we not just assign data to vcpu->arch.time? That's how the real MSRs work.

Quoted 6 lines
arch/x86/kvm/x86.c +1 -0
 			break;
+		}
 
 		/* ...but clean it before doing the actual write */
 		vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);

arch/x86/kvm/x86.c int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1651

Quoted 9 lines
arch/x86/kvm/x86.c +3 -1
@@ -1641,8 +1644,10 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 		vcpu->arch.time_page =
 				gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
 
-		if (is_error_page(vcpu->arch.time_page))
+		if (is_error_page(vcpu->arch.time_page)) {
 			vcpu->arch.time_page = NULL;
+			vcpu->arch.time = 0;
+		}
 

Don't see why.

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
Quoted 42 lines · Avi Kivity
Quoted 22 lines · Marcelo Tosatti

In case an invalid or disabled gpa is written to the SYSTEM_TIME MSR, do not retain its value. This is not documented behaviour, nor should be supported.

Also clear it on system reset. Not doing so can hide bugs.

arch/x86/kvm/x86.c +1 -0
index e00050c..ed4bfb7 100644
@@ -1528,6 +1528,7 @@ static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
 
 static void kvmclock_reset(struct kvm_vcpu *vcpu)
 {
+	vcpu->arch.time = 0;
 	if (vcpu->arch.time_page) {
 		kvm_release_page_dirty(vcpu->arch.time_page);
 		vcpu->arch.time_page = NULL;

arch/x86/kvm/x86.c int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1637

Quoted 6 lines
arch/x86/kvm/x86.c +2 -1
@@ -1632,8 +1633,10 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 		kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
 
 		/* we verify if the enable bit is set... */
-		if (!(data & 1))
+		if (!(data & 1)) {
+			vcpu->arch.time = 0;

Should we not just assign data to vcpu->arch.time? That's how the real MSRs work.

Quoted 9 lines
arch/x86/kvm/x86.c +1 -0
 			break;
+		}
 
 		/* ...but clean it before doing the actual write */
 		vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
@@ -1641,8 +1644,10 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 		vcpu->arch.time_page =
 				gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
 
Quoted 8 lines

arch/x86/kvm/x86.c int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1651

Quoted 6 lines
arch/x86/kvm/x86.c +3 -1
-		if (is_error_page(vcpu->arch.time_page))
+		if (is_error_page(vcpu->arch.time_page)) {
 			vcpu->arch.time_page = NULL;
+			vcpu->arch.time = 0;
+		}
 

Don't see why.

This would cause problems later with the new msr. But you're right, there is nothing incorrect in the current behaviour.

Will keep these changes where they have more context and meaning.