Skip to contentSkip to conversation

x86/tsc: update vcpu time info on guest TSC adjustments

PATCH In review Roger Pau Monne 4 participants 6 messages Oct 23, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/tsc: update vcpu time info on guest TSC adjustments

If a HVM/PVH guest writes to MSR_IA32_TSC{_ADJUST} and thus changes the value of the time stamp counter the vcpu time info must also be updated, or the time calculated by the guest using the Xen PV clock interface will be skewed.

Update the vcpu time info when the guest writes to either MSR_IA32_TSC or MSR_IA32_TSC_ADJUST. This fixes lockups seen when running the pv-shim on AMD hardware, since the shim will aggressively try to keep TSCs in sync by periodically writing to MSR_IA32_TSC if the TSC is not reliable.

---
Cc: Jürgen Groß <[email protected]>

---
 xen/arch/x86/hvm/hvm.c | 4 ++++
 1 file changed, 4 insertions(+)
xen/arch/x86/hvm/hvm.c +4 -0
index 9acd359c99..e4c0425330 100644
@@ -442,6 +442,8 @@ static void hvm_set_guest_tsc_msr(struct vcpu *v, u64 guest_tsc)
 
     hvm_set_guest_tsc(v, guest_tsc);
     v->arch.hvm.msr_tsc_adjust += v->arch.hvm.cache_tsc_offset - tsc_offset;
+    if ( v == current )
+        update_vcpu_system_time(v);
 }
 
 static void hvm_set_guest_tsc_adjust(struct vcpu *v, u64 tsc_adjust)
@@ -449,6 +451,8 @@ static void hvm_set_guest_tsc_adjust(struct vcpu *v, u64 tsc_adjust)
     v->arch.hvm.cache_tsc_offset += tsc_adjust - v->arch.hvm.msr_tsc_adjust;
     hvm_set_tsc_offset(v, v->arch.hvm.cache_tsc_offset, 0);
     v->arch.hvm.msr_tsc_adjust = tsc_adjust;
+    if ( v == current )
+        update_vcpu_system_time(v);
 }
 
 u64 hvm_get_guest_tsc_fixed(struct vcpu *v, uint64_t at_tsc)
Signature · 7 lines
2.23.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 5 lines · Roger Pau Monne

If a HVM/PVH guest writes to MSR_IA32_TSC{_ADJUST} and thus changes the value of the time stamp counter the vcpu time info must also be updated, or the time calculated by the guest using the Xen PV clock interface will be skewed.

Quoted 7 lines

Update the vcpu time info when the guest writes to either MSR_IA32_TSC or MSR_IA32_TSC_ADJUST. This fixes lockups seen when running the pv-shim on AMD hardware, since the shim will aggressively try to keep TSCs in sync by periodically writing to MSR_IA32_TSC if the TSC is not reliable.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
In reply to Roger Pau Monne
Quoted 23 lines · Roger Pau Monne

If a HVM/PVH guest writes to MSR_IA32_TSC{_ADJUST} and thus changes the value of the time stamp counter the vcpu time info must also be updated, or the time calculated by the guest using the Xen PV clock interface will be skewed.

Update the vcpu time info when the guest writes to either MSR_IA32_TSC or MSR_IA32_TSC_ADJUST. This fixes lockups seen when running the pv-shim on AMD hardware, since the shim will aggressively try to keep TSCs in sync by periodically writing to MSR_IA32_TSC if the TSC is not reliable.

---
Cc: Jürgen Groß <[email protected]>

---
 xen/arch/x86/hvm/hvm.c | 4 ++++
 1 file changed, 4 insertions(+)
xen/arch/x86/hvm/hvm.c +0 -0
index 9acd359c99..e4c0425330 100644

xen/arch/x86/hvm/hvm.c static void hvm_set_guest_tsc_msr(struct vcpu *v, u64 guest_tsc) · line 446

Quoted 5 lines
xen/arch/x86/hvm/hvm.c +2 -0
@@ -442,6 +442,8 @@ static void hvm_set_guest_tsc_msr(struct vcpu *v, u64 guest_tsc)
 
     hvm_set_guest_tsc(v, guest_tsc);
     v->arch.hvm.msr_tsc_adjust += v->arch.hvm.cache_tsc_offset - tsc_offset;
+    if ( v == current )
+        update_vcpu_system_time(v);

Why not get rid of the test and always call update_vcpu_system_time(v)?

Wei.

Quoted 15 lines
xen/arch/x86/hvm/hvm.c +2 -0
 }
 
 static void hvm_set_guest_tsc_adjust(struct vcpu *v, u64 tsc_adjust)
@@ -449,6 +451,8 @@ static void hvm_set_guest_tsc_adjust(struct vcpu *v, u64 tsc_adjust)
     v->arch.hvm.cache_tsc_offset += tsc_adjust - v->arch.hvm.msr_tsc_adjust;
     hvm_set_tsc_offset(v, v->arch.hvm.cache_tsc_offset, 0);
     v->arch.hvm.msr_tsc_adjust = tsc_adjust;
+    if ( v == current )
+        update_vcpu_system_time(v);
 }
 
 u64 hvm_get_guest_tsc_fixed(struct vcpu *v, uint64_t at_tsc)
Signature · 1 line
2.23.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 25 lines · Wei Liu
Quoted 24 lines · Roger Pau Monne

If a HVM/PVH guest writes to MSR_IA32_TSC{_ADJUST} and thus changes the value of the time stamp counter the vcpu time info must also be updated, or the time calculated by the guest using the Xen PV clock interface will be skewed.

Update the vcpu time info when the guest writes to either MSR_IA32_TSC or MSR_IA32_TSC_ADJUST. This fixes lockups seen when running the pv-shim on AMD hardware, since the shim will aggressively try to keep TSCs in sync by periodically writing to MSR_IA32_TSC if the TSC is not reliable.

---
Cc: Jürgen Groß <[email protected]>

---
 xen/arch/x86/hvm/hvm.c | 4 ++++
 1 file changed, 4 insertions(+)
xen/arch/x86/hvm/hvm.c +0 -0
index 9acd359c99..e4c0425330 100644
@@ -442,6 +442,8 @@ static void hvm_set_guest_tsc_msr(struct vcpu *v, u64 guest_tsc)
 
Quoted 6 lines

xen/arch/x86/hvm/hvm.c static void hvm_set_guest_tsc_msr(struct vcpu *v, u64 guest_tsc) · line 446

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +2 -0
     hvm_set_guest_tsc(v, guest_tsc);
     v->arch.hvm.msr_tsc_adjust += v->arch.hvm.cache_tsc_offset - tsc_offset;
+    if ( v == current )
+        update_vcpu_system_time(v);

Why not get rid of the test and always call update_vcpu_system_time(v)?

If the vcpu is not running the update will be done by the scheduling code in schedule_tail. Calling update_vcpu_system_time unconditionally would work, I was just trying to avoid the overhead if it's not really needed.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by
#4 lore raw
Quoted 33 lines · Roger Pau Monné
Quoted 25 lines · Wei Liu
Quoted 24 lines · Roger Pau Monne

If a HVM/PVH guest writes to MSR_IA32_TSC{_ADJUST} and thus changes the value of the time stamp counter the vcpu time info must also be updated, or the time calculated by the guest using the Xen PV clock interface will be skewed.

Update the vcpu time info when the guest writes to either MSR_IA32_TSC or MSR_IA32_TSC_ADJUST. This fixes lockups seen when running the pv-shim on AMD hardware, since the shim will aggressively try to keep TSCs in sync by periodically writing to MSR_IA32_TSC if the TSC is not reliable.

---
Cc: Jürgen Groß <[email protected]>

---
 xen/arch/x86/hvm/hvm.c | 4 ++++
 1 file changed, 4 insertions(+)
xen/arch/x86/hvm/hvm.c +0 -0
index 9acd359c99..e4c0425330 100644
@@ -442,6 +442,8 @@ static void hvm_set_guest_tsc_msr(struct vcpu *v, u64 guest_tsc)
 
Quoted 6 lines

xen/arch/x86/hvm/hvm.c static void hvm_set_guest_tsc_msr(struct vcpu *v, u64 guest_tsc) · line 446

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +2 -0
     hvm_set_guest_tsc(v, guest_tsc);
     v->arch.hvm.msr_tsc_adjust += v->arch.hvm.cache_tsc_offset - tsc_offset;
+    if ( v == current )
+        update_vcpu_system_time(v);

Why not get rid of the test and always call update_vcpu_system_time(v)?

Quoted 5 lines

If the vcpu is not running the update will be done by the scheduling code in schedule_tail. Calling update_vcpu_system_time unconditionally would work, I was just trying to avoid the overhead if it's not really needed.

Fair enough.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by on v1 1/1
#5 lore raw
In reply to Roger Pau Monne
Quoted 5 lines · Roger Pau Monne

If a HVM/PVH guest writes to MSR_IA32_TSC{_ADJUST} and thus changes the value of the time stamp counter the vcpu time info must also be updated, or the time calculated by the guest using the Xen PV clock interface will be skewed.

Quoted 7 lines

Update the vcpu time info when the guest writes to either MSR_IA32_TSC or MSR_IA32_TSC_ADJUST. This fixes lockups seen when running the pv-shim on AMD hardware, since the shim will aggressively try to keep TSCs in sync by periodically writing to MSR_IA32_TSC if the TSC is not reliable.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel