Skip to contentSkip to conversation

x86/time: update TSC stamp on restore from deep C-state

PATCH In review Igor Druzhinin 4 participants 23 messages Jan 14, 2020 – Jan 16, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/time: update TSC stamp on restore from deep C-state

If ITSC is not available on CPU (e.g if running nested as PV shim) then X86_FEATURE_NONSTOP_TSC is not advertised in certain cases, i.e. all AMD and some old Intel processors. In which case TSC would need to be restored on CPU from platform time by Xen upon exiting deep C-states.

As platform time might be behind the last TSC stamp recorded for the current CPU, invariant of TSC stamp being always behind local TSC counter is violated. This has an effect of get_s_time() going negative resulting in eventual system hang or crash.

Fix this issue by updating local TSC stamp along with TSC counter write.

---
This caused reliable hangs of shim domains with multiple vCPUs on all AMD systems. The problem got also reproduced on bare-metal by artifically masking ITSC feature bit. The proposed fix has been verified for both cases.

---
 xen/arch/x86/time.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
xen/arch/x86/time.c +7 -1
index e79cb4d..f6b26f8 100644
@@ -955,10 +955,16 @@ u64 stime2tsc(s_time_t stime)
 
 void cstate_restore_tsc(void)
 {
+    struct cpu_time *t = &this_cpu(cpu_time);
+
     if ( boot_cpu_has(X86_FEATURE_NONSTOP_TSC) )
         return;
 
-    write_tsc(stime2tsc(read_platform_stime(NULL)));
+    t->stamp.master_stime = read_platform_stime(NULL);
+    t->stamp.local_tsc = stime2tsc(t->stamp.master_stime);
+    t->stamp.local_stime = t->stamp.master_stime;
+
+    write_tsc(t->stamp.local_tsc);
 }
 
 /***************************************************************************
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 11 lines · Igor Druzhinin

If ITSC is not available on CPU (e.g if running nested as PV shim) then X86_FEATURE_NONSTOP_TSC is not advertised in certain cases, i.e. all AMD and some old Intel processors. In which case TSC would need to be restored on CPU from platform time by Xen upon exiting deep C-states.

As platform time might be behind the last TSC stamp recorded for the current CPU, invariant of TSC stamp being always behind local TSC counter is violated. This has an effect of get_s_time() going negative resulting in eventual system hang or crash.

Fix this issue by updating local TSC stamp along with TSC counter write.

Thanks! I haven't seen such issue because I've been running the shim with nomigrate in order to prevent the vTSC overhead.

Quoted 20 lines

---
This caused reliable hangs of shim domains with multiple vCPUs on all AMD systems. The problem got also reproduced on bare-metal by artifically masking ITSC feature bit. The proposed fix has been verified for both cases.

---
 xen/arch/x86/time.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
xen/arch/x86/time.c +1 -0
index e79cb4d..f6b26f8 100644
@@ -955,10 +955,16 @@ u64 stime2tsc(s_time_t stime)
 
 void cstate_restore_tsc(void)
 {
+    struct cpu_time *t = &this_cpu(cpu_time);

xen/arch/x86/time.c u64 stime2tsc(s_time_t stime) · line 967

Quoted 10 lines
xen/arch/x86/time.c +6 -1
+
     if ( boot_cpu_has(X86_FEATURE_NONSTOP_TSC) )
         return;
 
-    write_tsc(stime2tsc(read_platform_stime(NULL)));
+    t->stamp.master_stime = read_platform_stime(NULL);
+    t->stamp.local_tsc = stime2tsc(t->stamp.master_stime);
+    t->stamp.local_stime = t->stamp.master_stime;
+
+    write_tsc(t->stamp.local_tsc);

In order to avoid the TSC write (and the likely associated vmexit), could you instead do:

t->stamp.local_stime = t->stamp.master_stime = read_platform_stime(NULL); t->stamp.local_tsc = rdtsc_ordered();

I think it should achieve the same as it syncs the local TSC stamp and times, would avoid the TSC write and slightly simplifies the logic.

Thanks, Roger.

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

#2 lore raw
Quoted 22 lines · Roger Pau Monné
Quoted 7 lines · Igor Druzhinin
xen/arch/x86/time.c +1 -0
@@ -955,10 +955,16 @@ u64 stime2tsc(s_time_t stime)
 
 void cstate_restore_tsc(void)
 {
+    struct cpu_time *t = &this_cpu(cpu_time);

xen/arch/x86/time.c u64 stime2tsc(s_time_t stime) · line 967

Quoted 10 lines
xen/arch/x86/time.c +6 -1
+
     if ( boot_cpu_has(X86_FEATURE_NONSTOP_TSC) )
         return;
 
-    write_tsc(stime2tsc(read_platform_stime(NULL)));
+    t->stamp.master_stime = read_platform_stime(NULL);
+    t->stamp.local_tsc = stime2tsc(t->stamp.master_stime);
+    t->stamp.local_stime = t->stamp.master_stime;
+
+    write_tsc(t->stamp.local_tsc);

In order to avoid the TSC write (and the likely associated vmexit), could you instead do:

Quoted 5 lines

t->stamp.local_stime = t->stamp.master_stime = read_platform_stime(NULL); t->stamp.local_tsc = rdtsc_ordered();

I think it should achieve the same as it syncs the local TSC stamp and times, would avoid the TSC write and slightly simplifies the logic.

Wouldn't this result in guests possibly observing the TSC moving backwards?

Jan

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

#3 lore raw
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Roger Pau Monné
Quoted 7 lines · Igor Druzhinin
xen/arch/x86/time.c +1 -0
@@ -955,10 +955,16 @@ u64 stime2tsc(s_time_t stime)
 
 void cstate_restore_tsc(void)
 {
+    struct cpu_time *t = &this_cpu(cpu_time);

xen/arch/x86/time.c u64 stime2tsc(s_time_t stime) · line 967

Quoted 10 lines
xen/arch/x86/time.c +6 -1
+
     if ( boot_cpu_has(X86_FEATURE_NONSTOP_TSC) )
         return;
 
-    write_tsc(stime2tsc(read_platform_stime(NULL)));
+    t->stamp.master_stime = read_platform_stime(NULL);
+    t->stamp.local_tsc = stime2tsc(t->stamp.master_stime);
+    t->stamp.local_stime = t->stamp.master_stime;
+
+    write_tsc(t->stamp.local_tsc);

In order to avoid the TSC write (and the likely associated vmexit), could you instead do:

Quoted 8 lines
Quoted 5 lines

t->stamp.local_stime = t->stamp.master_stime = read_platform_stime(NULL); t->stamp.local_tsc = rdtsc_ordered();

I think it should achieve the same as it syncs the local TSC stamp and times, would avoid the TSC write and slightly simplifies the logic.

Wouldn't this result in guests possibly observing the TSC moving backwards?

Isn't local_tsc storing a TSC value read from the same CPU always, and hence could only go backwards if rdtsc actually goes backwards?

Ie: cpu_frequency_change seems to do something similar, together with a re-adjusting of the time scale, but doesn't perform any TSC write.

Thanks, Roger.

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

#4 lore raw
Quoted 27 lines · Roger Pau Monné
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Roger Pau Monné
Quoted 7 lines · Igor Druzhinin
xen/arch/x86/time.c +1 -0
@@ -955,10 +955,16 @@ u64 stime2tsc(s_time_t stime)
 
 void cstate_restore_tsc(void)
 {
+    struct cpu_time *t = &this_cpu(cpu_time);

xen/arch/x86/time.c u64 stime2tsc(s_time_t stime) · line 967

Quoted 10 lines
xen/arch/x86/time.c +6 -1
+
     if ( boot_cpu_has(X86_FEATURE_NONSTOP_TSC) )
         return;
 
-    write_tsc(stime2tsc(read_platform_stime(NULL)));
+    t->stamp.master_stime = read_platform_stime(NULL);
+    t->stamp.local_tsc = stime2tsc(t->stamp.master_stime);
+    t->stamp.local_stime = t->stamp.master_stime;
+
+    write_tsc(t->stamp.local_tsc);

In order to avoid the TSC write (and the likely associated vmexit), could you instead do:

t->stamp.local_stime = t->stamp.master_stime = read_platform_stime(NULL); t->stamp.local_tsc = rdtsc_ordered();

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I think it should achieve the same as it syncs the local TSC stamp and times, would avoid the TSC write and slightly simplifies the logic.

Wouldn't this result in guests possibly observing the TSC moving backwards?

Isn't local_tsc storing a TSC value read from the same CPU always, and hence could only go backwards if rdtsc actually goes backwards?

For one I have to admit I was (mistakenly) thinking of wakeup from S states more than that from C states. So assuming the TSC indeed only stops (but won't get e.g. restarted), backwards moves ought to be excluded. What I'm then worried about is too little progress observable by guests. The PV time protocol ought to be fine in this regard (and consumers of raw TSC values are on their own anyway), but wouldn't you need to update TSC offsets of HVM guests in order to compensate for the elapsed time?

Quoted 2 lines

Ie: cpu_frequency_change seems to do something similar, together with a re-adjusting of the time scale, but doesn't perform any TSC write.

A P-state change at most alters the the tick rate, but wouldn't stop or even reset the TSC (afaict).

Jan

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

#5 lore raw
Quoted 34 lines · Jan Beulich
Quoted 33 lines · Roger Pau Monné
Quoted 31 lines · Jan Beulich
Quoted 22 lines · Roger Pau Monné
Quoted 7 lines · Igor Druzhinin
xen/arch/x86/time.c +1 -0
@@ -955,10 +955,16 @@ u64 stime2tsc(s_time_t stime)
 
 void cstate_restore_tsc(void)
 {
+    struct cpu_time *t = &this_cpu(cpu_time);

xen/arch/x86/time.c u64 stime2tsc(s_time_t stime) · line 967

Quoted 10 lines
xen/arch/x86/time.c +6 -1
+
     if ( boot_cpu_has(X86_FEATURE_NONSTOP_TSC) )
         return;
 
-    write_tsc(stime2tsc(read_platform_stime(NULL)));
+    t->stamp.master_stime = read_platform_stime(NULL);
+    t->stamp.local_tsc = stime2tsc(t->stamp.master_stime);
+    t->stamp.local_stime = t->stamp.master_stime;
+
+    write_tsc(t->stamp.local_tsc);

In order to avoid the TSC write (and the likely associated vmexit), could you instead do:

Quoted 5 lines

t->stamp.local_stime = t->stamp.master_stime = read_platform_stime(NULL); t->stamp.local_tsc = rdtsc_ordered();

I think it should achieve the same as it syncs the local TSC stamp and times, would avoid the TSC write and slightly simplifies the logic.

Wouldn't this result in guests possibly observing the TSC moving backwards?

Quoted 7 lines
Quoted 2 lines

Isn't local_tsc storing a TSC value read from the same CPU always, and hence could only go backwards if rdtsc actually goes backwards?

For one I have to admit I was (mistakenly) thinking of wakeup from S states more than that from C states. So assuming the TSC indeed only stops (but won't get e.g. restarted), backwards moves ought to be excluded.

Even if the TSC was restarted I think my proposed approach should be fine. The only requirement is that the stored TSC stamp must always be behind than the value returned by rdtsc. See get_s_time_fixed: as long as the delta is positive the returned time should be correct.

Quoted 6 lines

What I'm then worried about is too
little progress observable by guests. The PV time protocol ought to be fine in this regard (and consumers of raw TSC values are on their own anyway), but wouldn't you need to update TSC offsets of HVM guests in order to compensate for the elapsed time?

That will be done when the HVM vCPU gets scheduled in as part of the update_vcpu_system_time call AFAICT. cstate_restore_tsc will always be called with the idle vCPU context, and hence there's always going to be a vCPU switch before scheduling anything else.

Quoted 5 lines
Quoted 2 lines

Ie: cpu_frequency_change seems to do something similar, together with a re-adjusting of the time scale, but doesn't perform any TSC write.

A P-state change at most alters the the tick rate, but wouldn't stop or even reset the TSC (afaict).

Right, just wanted to point out that the cpu_time stamp can be updated without having to write to the TSC. Anyway, not sure it's very relevant or useful, so forget this reference.

Roger.

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

#6 lore raw
Quoted 8 lines · Roger Pau Monné
Quoted 6 lines · Jan Beulich

What I'm then worried about is too
little progress observable by guests. The PV time protocol ought to be fine in this regard (and consumers of raw TSC values are on their own anyway), but wouldn't you need to update TSC offsets of HVM guests in order to compensate for the elapsed time?

Quoted 4 lines

That will be done when the HVM vCPU gets scheduled in as part of the update_vcpu_system_time call AFAICT. cstate_restore_tsc will always be called with the idle vCPU context, and hence there's always going to be a vCPU switch before scheduling anything else.

Which step would this be? All I see is a call to hvm_scale_tsc(). In time.c only tsc_set_info() calls hvm_set_tsc_offset().

Jan

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

Reviewed-by
#7 lore raw
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Roger Pau Monné
Quoted 6 lines · Jan Beulich

What I'm then worried about is too
little progress observable by guests. The PV time protocol ought to be fine in this regard (and consumers of raw TSC values are on their own anyway), but wouldn't you need to update TSC offsets of HVM guests in order to compensate for the elapsed time?

Quoted 7 lines
Quoted 4 lines

That will be done when the HVM vCPU gets scheduled in as part of the update_vcpu_system_time call AFAICT. cstate_restore_tsc will always be called with the idle vCPU context, and hence there's always going to be a vCPU switch before scheduling anything else.

Which step would this be? All I see is a call to hvm_scale_tsc(). In time.c only tsc_set_info() calls hvm_set_tsc_offset().

My bad, I've mistaken the scaling with the offset.

Accounting for the offset in update_vcpu_system_time seems quite more complicated that just updating the TSC here, so:

I also wonder whether it would be interesting to add at least an assert to get_s_time_fixed in order to assure that the TSC stamp is always behind the current TSC value, this would have likely helped catch this issue earlier.

Thanks, Roger.

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

Acked-by
#8 lore raw
Quoted 18 lines · Roger Pau Monné
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Roger Pau Monné
Quoted 6 lines · Jan Beulich

What I'm then worried about is too
little progress observable by guests. The PV time protocol ought to be fine in this regard (and consumers of raw TSC values are on their own anyway), but wouldn't you need to update TSC offsets of HVM guests in order to compensate for the elapsed time?

Quoted 7 lines
Quoted 4 lines

That will be done when the HVM vCPU gets scheduled in as part of the update_vcpu_system_time call AFAICT. cstate_restore_tsc will always be called with the idle vCPU context, and hence there's always going to be a vCPU switch before scheduling anything else.

Which step would this be? All I see is a call to hvm_scale_tsc(). In time.c only tsc_set_info() calls hvm_set_tsc_offset().

Quoted 6 lines

My bad, I've mistaken the scaling with the offset.

Accounting for the offset in update_vcpu_system_time seems quite more complicated that just updating the TSC here, so:

And then (preferably with "deep" dropped from the description, if you, Igor, agree, and which can be done while committing)

Jan

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

#9 lore raw
Quoted 21 lines · Jan Beulich
Quoted 20 lines · Roger Pau Monné
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Roger Pau Monné
Quoted 6 lines · Jan Beulich

What I'm then worried about is too
little progress observable by guests. The PV time protocol ought to be fine in this regard (and consumers of raw TSC values are on their own anyway), but wouldn't you need to update TSC offsets of HVM guests in order to compensate for the elapsed time?

Quoted 7 lines
Quoted 4 lines

That will be done when the HVM vCPU gets scheduled in as part of the update_vcpu_system_time call AFAICT. cstate_restore_tsc will always be called with the idle vCPU context, and hence there's always going to be a vCPU switch before scheduling anything else.

Which step would this be? All I see is a call to hvm_scale_tsc(). In time.c only tsc_set_info() calls hvm_set_tsc_offset().

My bad, I've mistaken the scaling with the offset.

Quoted 8 lines
Quoted 4 lines

Accounting for the offset in update_vcpu_system_time seems quite more complicated that just updating the TSC here, so:

And then (preferably with "deep" dropped from the description, if you, Igor, agree, and which can be done while committing)

No objection. FYI I tested Roger's approach this night and it seems to work at least in sense of fixing the original issue.

Igor

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

#10 lore raw
Quoted 25 lines · Igor Druzhinin
Quoted 24 lines · Jan Beulich
Quoted 23 lines · Roger Pau Monné
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Roger Pau Monné
Quoted 6 lines · Jan Beulich

What I'm then worried about is too
little progress observable by guests. The PV time protocol ought to be fine in this regard (and consumers of raw TSC values are on their own anyway), but wouldn't you need to update TSC offsets of HVM guests in order to compensate for the elapsed time?

Quoted 7 lines
Quoted 4 lines

That will be done when the HVM vCPU gets scheduled in as part of the update_vcpu_system_time call AFAICT. cstate_restore_tsc will always be called with the idle vCPU context, and hence there's always going to be a vCPU switch before scheduling anything else.

Which step would this be? All I see is a call to hvm_scale_tsc(). In time.c only tsc_set_info() calls hvm_set_tsc_offset().

My bad, I've mistaken the scaling with the offset.

Accounting for the offset in update_vcpu_system_time seems quite more complicated that just updating the TSC here, so:

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

And then (preferably with "deep" dropped from the description, if you, Igor, agree, and which can be done while committing)

No objection. FYI I tested Roger's approach this night and it seems to work at least in sense of fixing the original issue.

Right, but HVM guests using a timekeeping approach similar to what Xen does (read the platform timer periodically and calculate time based on the last read plus the TSC delta) would get wrong (behind the real time) results AFAICT.

Roger.

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

#11 lore raw
In reply to Jan Beulich
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Roger Pau Monné
Quoted 7 lines · Igor Druzhinin
xen/arch/x86/time.c +1 -0
@@ -955,10 +955,16 @@ u64 stime2tsc(s_time_t stime)
 
 void cstate_restore_tsc(void)
 {
+    struct cpu_time *t = &this_cpu(cpu_time);

xen/arch/x86/time.c u64 stime2tsc(s_time_t stime) · line 967

Quoted 10 lines
xen/arch/x86/time.c +6 -1
+
     if ( boot_cpu_has(X86_FEATURE_NONSTOP_TSC) )
         return;
 
-    write_tsc(stime2tsc(read_platform_stime(NULL)));
+    t->stamp.master_stime = read_platform_stime(NULL);
+    t->stamp.local_tsc = stime2tsc(t->stamp.master_stime);
+    t->stamp.local_stime = t->stamp.master_stime;
+
+    write_tsc(t->stamp.local_tsc);

In order to avoid the TSC write (and the likely associated vmexit), could you instead do:

Quoted 8 lines
Quoted 5 lines

t->stamp.local_stime = t->stamp.master_stime = read_platform_stime(NULL); t->stamp.local_tsc = rdtsc_ordered();

I think it should achieve the same as it syncs the local TSC stamp and times, would avoid the TSC write and slightly simplifies the logic.

Wouldn't this result in guests possibly observing the TSC moving backwards?

Yes, I think so. Would restoring from TSC stamp if it's higher than platform time better you think?

Igor

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

#12 lore raw
Quoted 27 lines · Igor Druzhinin
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Roger Pau Monné
Quoted 7 lines · Igor Druzhinin
xen/arch/x86/time.c +1 -0
@@ -955,10 +955,16 @@ u64 stime2tsc(s_time_t stime)
 
 void cstate_restore_tsc(void)
 {
+    struct cpu_time *t = &this_cpu(cpu_time);

xen/arch/x86/time.c u64 stime2tsc(s_time_t stime) · line 967

Quoted 10 lines
xen/arch/x86/time.c +6 -1
+
     if ( boot_cpu_has(X86_FEATURE_NONSTOP_TSC) )
         return;
 
-    write_tsc(stime2tsc(read_platform_stime(NULL)));
+    t->stamp.master_stime = read_platform_stime(NULL);
+    t->stamp.local_tsc = stime2tsc(t->stamp.master_stime);
+    t->stamp.local_stime = t->stamp.master_stime;
+
+    write_tsc(t->stamp.local_tsc);

In order to avoid the TSC write (and the likely associated vmexit), could you instead do:

t->stamp.local_stime = t->stamp.master_stime = read_platform_stime(NULL); t->stamp.local_tsc = rdtsc_ordered();

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

I think it should achieve the same as it syncs the local TSC stamp and times, would avoid the TSC write and slightly simplifies the logic.

Wouldn't this result in guests possibly observing the TSC moving backwards?

Yes, I think so. Would restoring from TSC stamp if it's higher than platform time better you think?

Ignore my reply. I was thinking you're asking whether the original code would do such a thing. Although I'm concerned if what you say actually applies to the original code as well. Would you think the existing logic handles it already?

Igor

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

#13 lore raw
Quoted 34 lines · Igor Druzhinin
Quoted 33 lines · Igor Druzhinin
Quoted 31 lines · Jan Beulich
Quoted 22 lines · Roger Pau Monné
Quoted 7 lines · Igor Druzhinin
xen/arch/x86/time.c +1 -0
@@ -955,10 +955,16 @@ u64 stime2tsc(s_time_t stime)
 
 void cstate_restore_tsc(void)
 {
+    struct cpu_time *t = &this_cpu(cpu_time);

xen/arch/x86/time.c u64 stime2tsc(s_time_t stime) · line 967

Quoted 10 lines
xen/arch/x86/time.c +6 -1
+
     if ( boot_cpu_has(X86_FEATURE_NONSTOP_TSC) )
         return;
 
-    write_tsc(stime2tsc(read_platform_stime(NULL)));
+    t->stamp.master_stime = read_platform_stime(NULL);
+    t->stamp.local_tsc = stime2tsc(t->stamp.master_stime);
+    t->stamp.local_stime = t->stamp.master_stime;
+
+    write_tsc(t->stamp.local_tsc);

In order to avoid the TSC write (and the likely associated vmexit), could you instead do:

Quoted 5 lines

t->stamp.local_stime = t->stamp.master_stime = read_platform_stime(NULL); t->stamp.local_tsc = rdtsc_ordered();

I think it should achieve the same as it syncs the local TSC stamp and times, would avoid the TSC write and slightly simplifies the logic.

Wouldn't this result in guests possibly observing the TSC moving backwards?

Quoted 8 lines
Quoted 3 lines

Yes, I think so. Would restoring from TSC stamp if it's higher than platform time better you think?

Ignore my reply. I was thinking you're asking whether the original code would do such a thing. Although I'm concerned if what you say actually applies to the original code as well. Would you think the existing logic handles it already?

I think the original code won't guarantee to backwards move at all, but it also wouldn't produce large backwards jumps. It's large ones I was mainly thinking of when replying to Roger. But let me also reply back to him...

Jan

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

#14 lore raw
In reply to Roger Pau Monné
Quoted 50 lines · Roger Pau Monné
Quoted 11 lines · Igor Druzhinin

If ITSC is not available on CPU (e.g if running nested as PV shim) then X86_FEATURE_NONSTOP_TSC is not advertised in certain cases, i.e. all AMD and some old Intel processors. In which case TSC would need to be restored on CPU from platform time by Xen upon exiting deep C-states.

As platform time might be behind the last TSC stamp recorded for the current CPU, invariant of TSC stamp being always behind local TSC counter is violated. This has an effect of get_s_time() going negative resulting in eventual system hang or crash.

Fix this issue by updating local TSC stamp along with TSC counter write.

Thanks! I haven't seen such issue because I've been running the shim with nomigrate in order to prevent the vTSC overhead.

Quoted 20 lines

---
This caused reliable hangs of shim domains with multiple vCPUs on all AMD systems. The problem got also reproduced on bare-metal by artifically masking ITSC feature bit. The proposed fix has been verified for both cases.

---
 xen/arch/x86/time.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
xen/arch/x86/time.c +1 -0
index e79cb4d..f6b26f8 100644
@@ -955,10 +955,16 @@ u64 stime2tsc(s_time_t stime)
 
 void cstate_restore_tsc(void)
 {
+    struct cpu_time *t = &this_cpu(cpu_time);

xen/arch/x86/time.c u64 stime2tsc(s_time_t stime) · line 967

Quoted 10 lines
xen/arch/x86/time.c +6 -1
+
     if ( boot_cpu_has(X86_FEATURE_NONSTOP_TSC) )
         return;
 
-    write_tsc(stime2tsc(read_platform_stime(NULL)));
+    t->stamp.master_stime = read_platform_stime(NULL);
+    t->stamp.local_tsc = stime2tsc(t->stamp.master_stime);
+    t->stamp.local_stime = t->stamp.master_stime;
+
+    write_tsc(t->stamp.local_tsc);

In order to avoid the TSC write (and the likely associated vmexit), could you instead do:

Quoted 2 lines

t->stamp.local_stime = t->stamp.master_stime = read_platform_stime(NULL); t->stamp.local_tsc = rdtsc_ordered();

I think in that case RDTSC might return something behind platform time which is not right I guess.

Igor

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

#15 lore raw
Quoted 51 lines · Igor Druzhinin
Quoted 50 lines · Roger Pau Monné
Quoted 11 lines · Igor Druzhinin

If ITSC is not available on CPU (e.g if running nested as PV shim) then X86_FEATURE_NONSTOP_TSC is not advertised in certain cases, i.e. all AMD and some old Intel processors. In which case TSC would need to be restored on CPU from platform time by Xen upon exiting deep C-states.

As platform time might be behind the last TSC stamp recorded for the current CPU, invariant of TSC stamp being always behind local TSC counter is violated. This has an effect of get_s_time() going negative resulting in eventual system hang or crash.

Fix this issue by updating local TSC stamp along with TSC counter write.

Thanks! I haven't seen such issue because I've been running the shim with nomigrate in order to prevent the vTSC overhead.

Quoted 20 lines

---
This caused reliable hangs of shim domains with multiple vCPUs on all AMD systems. The problem got also reproduced on bare-metal by artifically masking ITSC feature bit. The proposed fix has been verified for both cases.

---
 xen/arch/x86/time.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
xen/arch/x86/time.c +1 -0
index e79cb4d..f6b26f8 100644
@@ -955,10 +955,16 @@ u64 stime2tsc(s_time_t stime)
 
 void cstate_restore_tsc(void)
 {
+    struct cpu_time *t = &this_cpu(cpu_time);

xen/arch/x86/time.c u64 stime2tsc(s_time_t stime) · line 967

Quoted 10 lines
xen/arch/x86/time.c +6 -1
+
     if ( boot_cpu_has(X86_FEATURE_NONSTOP_TSC) )
         return;
 
-    write_tsc(stime2tsc(read_platform_stime(NULL)));
+    t->stamp.master_stime = read_platform_stime(NULL);
+    t->stamp.local_tsc = stime2tsc(t->stamp.master_stime);
+    t->stamp.local_stime = t->stamp.master_stime;
+
+    write_tsc(t->stamp.local_tsc);

In order to avoid the TSC write (and the likely associated vmexit), could you instead do:

Quoted 5 lines
Quoted 2 lines

t->stamp.local_stime = t->stamp.master_stime = read_platform_stime(NULL); t->stamp.local_tsc = rdtsc_ordered();

I think in that case RDTSC might return something behind platform time which is not right I guess.

The TSC and the platform time are completely independent from Xen's PoV, you can have a platform time not based on the TSC (ie: PIT, HPET or PM), and hence there's no direct relation between both.

The TSC is used as a way to get an approximate platform time based on the last platform time value and the TSC delta between than value and the current TSC value, I assume that's done because reading the TSC is much cheaper than reading the platform time.

As long as the platform time and the TSC stamps are both updated at the same time it should be fine.

Roger.

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

#16 lore raw
Quoted 62 lines · Roger Pau Monné
Quoted 51 lines · Igor Druzhinin
Quoted 50 lines · Roger Pau Monné
Quoted 11 lines · Igor Druzhinin

If ITSC is not available on CPU (e.g if running nested as PV shim) then X86_FEATURE_NONSTOP_TSC is not advertised in certain cases, i.e. all AMD and some old Intel processors. In which case TSC would need to be restored on CPU from platform time by Xen upon exiting deep C-states.

As platform time might be behind the last TSC stamp recorded for the current CPU, invariant of TSC stamp being always behind local TSC counter is violated. This has an effect of get_s_time() going negative resulting in eventual system hang or crash.

Fix this issue by updating local TSC stamp along with TSC counter write.

Thanks! I haven't seen such issue because I've been running the shim with nomigrate in order to prevent the vTSC overhead.

Quoted 20 lines

---
This caused reliable hangs of shim domains with multiple vCPUs on all AMD systems. The problem got also reproduced on bare-metal by artifically masking ITSC feature bit. The proposed fix has been verified for both cases.

---
 xen/arch/x86/time.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
xen/arch/x86/time.c +1 -0
index e79cb4d..f6b26f8 100644
@@ -955,10 +955,16 @@ u64 stime2tsc(s_time_t stime)
 
 void cstate_restore_tsc(void)
 {
+    struct cpu_time *t = &this_cpu(cpu_time);

xen/arch/x86/time.c u64 stime2tsc(s_time_t stime) · line 967

Quoted 10 lines
xen/arch/x86/time.c +6 -1
+
     if ( boot_cpu_has(X86_FEATURE_NONSTOP_TSC) )
         return;
 
-    write_tsc(stime2tsc(read_platform_stime(NULL)));
+    t->stamp.master_stime = read_platform_stime(NULL);
+    t->stamp.local_tsc = stime2tsc(t->stamp.master_stime);
+    t->stamp.local_stime = t->stamp.master_stime;
+
+    write_tsc(t->stamp.local_tsc);

In order to avoid the TSC write (and the likely associated vmexit), could you instead do:

Quoted 5 lines
Quoted 2 lines

t->stamp.local_stime = t->stamp.master_stime = read_platform_stime(NULL); t->stamp.local_tsc = rdtsc_ordered();

I think in that case RDTSC might return something behind platform time which is not right I guess.

The TSC and the platform time are completely independent from Xen's PoV, you can have a platform time not based on the TSC (ie: PIT, HPET or PM), and hence there's no direct relation between both.

Quoted 7 lines

The TSC is used as a way to get an approximate platform time based on the last platform time value and the TSC delta between than value and the current TSC value, I assume that's done because reading the TSC is much cheaper than reading the platform time.

As long as the platform time and the TSC stamps are both updated at the same time it should be fine.

I see your point. I'll test your approach and get back here with the results.

Igor

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

#17 lore raw
In reply to Igor Druzhinin
Quoted 4 lines · Igor Druzhinin

If ITSC is not available on CPU (e.g if running nested as PV shim) then X86_FEATURE_NONSTOP_TSC is not advertised in certain cases, i.e. all AMD and some old Intel processors. In which case TSC would need to be restored on CPU from platform time by Xen upon exiting deep C-states.

How does waking from deep C states correspond to the PV shim? I notice that cstate_restore_tsc() gets called irrespective of the C state being exited, so I wonder whether there's room for improvement there independent of the issue at hand. As far as this change is concerned, I think you want to drop the notion of "deep" from the description.

Jan

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

#18 lore raw
Quoted 11 lines · Jan Beulich
Quoted 4 lines · Igor Druzhinin

If ITSC is not available on CPU (e.g if running nested as PV shim) then X86_FEATURE_NONSTOP_TSC is not advertised in certain cases, i.e. all AMD and some old Intel processors. In which case TSC would need to be restored on CPU from platform time by Xen upon exiting deep C-states.

How does waking from deep C states correspond to the PV shim? I notice that cstate_restore_tsc() gets called irrespective of the C state being exited, so I wonder whether there's room for improvement there independent of the issue at hand. As far as this change is concerned, I think you want to drop the notion of "deep" from the description.

I'm not familiar with what to call "deep C-state" so for me it was anything higher than C1. If you prefer "deep" to be dropped - so be it.

Igor

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

#19 lore raw
Quoted 7 lines · Igor Druzhinin
Quoted 6 lines · Jan Beulich
Quoted 4 lines · Igor Druzhinin

If ITSC is not available on CPU (e.g if running nested as PV shim) then X86_FEATURE_NONSTOP_TSC is not advertised in certain cases, i.e. all AMD and some old Intel processors. In which case TSC would need to be restored on CPU from platform time by Xen upon exiting deep C-states.

Quoted 8 lines
Quoted 5 lines

How does waking from deep C states correspond to the PV shim? I notice that cstate_restore_tsc() gets called irrespective of the C state being exited, so I wonder whether there's room for improvement there independent of the issue at hand. As far as this change is concerned, I think you want to drop the notion of "deep" from the description.

I'm not familiar with what to call "deep C-state" so for me it was anything higher than C1. If you prefer "deep" to be dropped - so be it.

"Higher than C1" may be fine (albeit I vaguely recall TSC issues starting with C3 only), but at least mwait_idle() calls the function even for C1. As to the PV shim - does it know about any C-states at all (beyond HLT-invoked C1)?

Jan

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

#20 lore raw
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Igor Druzhinin
Quoted 11 lines · Jan Beulich
Quoted 4 lines · Igor Druzhinin

If ITSC is not available on CPU (e.g if running nested as PV shim) then X86_FEATURE_NONSTOP_TSC is not advertised in certain cases, i.e. all AMD and some old Intel processors. In which case TSC would need to be restored on CPU from platform time by Xen upon exiting deep C-states.

How does waking from deep C states correspond to the PV shim? I notice that cstate_restore_tsc() gets called irrespective of the C state being exited, so I wonder whether there's room for improvement there independent of the issue at hand. As far as this change is concerned, I think you want to drop the notion of "deep" from the description.

Quoted 7 lines
Quoted 2 lines

I'm not familiar with what to call "deep C-state" so for me it was anything higher than C1. If you prefer "deep" to be dropped - so be it.

"Higher than C1" may be fine (albeit I vaguely recall TSC issues starting with C3 only), but at least mwait_idle() calls the function even for C1. As to the PV shim - does it know about any C-states at all (beyond HLT-invoked C1)?

Yes, PV-shim knows about C states as it looks they are tied to processor ID in some cases. For AMD specifically C2 is HLT.

Igor

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

#21 lore raw
Quoted 18 lines · Igor Druzhinin
Quoted 17 lines · Jan Beulich
Quoted 15 lines · Igor Druzhinin
Quoted 11 lines · Jan Beulich
Quoted 4 lines · Igor Druzhinin

If ITSC is not available on CPU (e.g if running nested as PV shim) then X86_FEATURE_NONSTOP_TSC is not advertised in certain cases, i.e. all AMD and some old Intel processors. In which case TSC would need to be restored on CPU from platform time by Xen upon exiting deep C-states.

How does waking from deep C states correspond to the PV shim? I notice that cstate_restore_tsc() gets called irrespective of the C state being exited, so I wonder whether there's room for improvement there independent of the issue at hand. As far as this change is concerned, I think you want to drop the notion of "deep" from the description.

I'm not familiar with what to call "deep C-state" so for me it was anything higher than C1. If you prefer "deep" to be dropped - so be it.

Quoted 7 lines
Quoted 4 lines

"Higher than C1" may be fine (albeit I vaguely recall TSC issues starting with C3 only), but at least mwait_idle() calls the function even for C1. As to the PV shim - does it know about any C-states at all (beyond HLT-invoked C1)?

Yes, PV-shim knows about C states as it looks they are tied to processor ID in some cases. For AMD specifically C2 is HLT.

The AMD part is pretty new, and is - afaict - an exception compared to everything else. Under PVH there's no respective ACPI data (iirc), and we also don't surface MONITOR/MWAIT to HVM/PVH guests, making mwait_idle_probe() bail early. I wonder whether this special behavior on AMD Fam17 should be suppressed in this case.

Jan

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

#22 lore raw
Quoted 20 lines · Jan Beulich
Quoted 19 lines · Igor Druzhinin
Quoted 18 lines · Jan Beulich
Quoted 6 lines · Igor Druzhinin
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Igor Druzhinin

If ITSC is not available on CPU (e.g if running nested as PV shim) then X86_FEATURE_NONSTOP_TSC is not advertised in certain cases, i.e. all AMD and some old Intel processors. In which case TSC would need to be restored on CPU from platform time by Xen upon exiting deep C-states.

Quoted 7 lines
Quoted 5 lines

How does waking from deep C states correspond to the PV shim? I notice that cstate_restore_tsc() gets called irrespective of the C state being exited, so I wonder whether there's room for improvement there independent of the issue at hand. As far as this change is concerned, I think you want to drop the notion of "deep" from the description.

I'm not familiar with what to call "deep C-state" so for me it was anything higher than C1. If you prefer "deep" to be dropped - so be it.

"Higher than C1" may be fine (albeit I vaguely recall TSC issues starting with C3 only), but at least mwait_idle() calls the function even for C1. As to the PV shim - does it know about any C-states at all (beyond HLT-invoked C1)?

Quoted 7 lines
Quoted 2 lines

Yes, PV-shim knows about C states as it looks they are tied to processor ID in some cases. For AMD specifically C2 is HLT.

The AMD part is pretty new, and is - afaict - an exception compared to everything else. Under PVH there's no respective ACPI data (iirc), and we also don't surface MONITOR/MWAIT to HVM/PVH guests, making mwait_idle_probe() bail early. I wonder whether this special behavior on AMD Fam17 should be suppressed in this case.

Anything which knows it is virtualised should use hypercalls to yield, and failing that, hlt.

A VM isn't going to get a plausible idea of C/P states (give or take our dom0 special case seeing the real APCI tables which is still an open problem.)

~Andrew

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