Skip to contentSkip to conversation

PV shim timekeeping fixes

PATCH In review Igor Druzhinin 2 participants 5 messages Feb 4, 2020 – Feb 5, 2020

lore
Share & tools
lore
#0 lore raw

v2 cover PV shim timekeeping fixes

Igor Druzhinin (2):
x86/shim: suspend and resume platform time correctly x86/time: report correct frequency of Xen PV clocksource

 xen/arch/x86/pv/shim.c |  7 ++++++-
 xen/arch/x86/time.c    | 17 +++++++++++------
 2 files changed, 17 insertions(+), 7 deletions(-)
Signature · 7 lines
2.7.4


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

PATCH v2 1/2 x86/shim: suspend and resume platform time correctly

Similarly to S3, platform time needs to be saved on guest suspend and restored on resume respectively. This should account for expected jumps in PV clock counter value after resume. time_suspend/resume() are safe to use in PVH setting as is since any existing operations with PIT/HPET that they do would simply be ignored if PIT/HPET is not present.

Additionally, add resume callback for Xen PV clocksource to avoid its breakage on migration.

---
 xen/arch/x86/pv/shim.c |  7 ++++++-
 xen/arch/x86/time.c    | 12 +++++++++---
 2 files changed, 15 insertions(+), 4 deletions(-)
xen/arch/x86/pv/shim.c +6 -1
index 7a898fd..6b26eaa 100644
@@ -325,9 +325,13 @@ int pv_shim_shutdown(uint8_t reason)
         if ( v != current )
             vcpu_pause_by_systemcontroller(v);
 
+    /* Prepare timekeeping code to suspend.*/
+    time_suspend();
+
     rc = xen_hypercall_shutdown(SHUTDOWN_suspend);
     if ( rc )
     {
+        time_resume();
         for_each_vcpu ( d, v )
             if ( v != current )
                 vcpu_unpause_by_systemcontroller(v);
@@ -335,8 +339,9 @@ int pv_shim_shutdown(uint8_t reason)
         return rc;
     }
 
-    /* Resume the shim itself first. */
+    /* Resume the shim itself and timekeeping first. */
     hypervisor_resume();
+    time_resume();
 
     /*
      * ATM there's nothing Xen can do if the console/store pfn changes,
xen/arch/x86/time.c +9 -3
index f6b26f8..7e7a62e 100644
@@ -565,6 +565,7 @@ static struct platform_timesource __initdata plt_tsc =
  *
  * Xen clock source is a variant of TSC source.
  */
+static uint64_t xen_timer_last;
 
 static uint64_t xen_timer_cpu_frequency(void)
 {
@@ -610,7 +611,6 @@ static uint64_t read_xen_timer(void)
     uint32_t version;
     uint64_t ret;
     uint64_t last;
-    static uint64_t last_value;
 
     do {
         version = info->version & ~1;
@@ -626,20 +626,26 @@ static uint64_t read_xen_timer(void)
 
     /* Maintain a monotonic global value */
     do {
-        last = read_atomic(&last_value);
+        last = read_atomic(&xen_timer_last);
         if ( ret < last )
             return last;
-    } while ( unlikely(cmpxchg(&last_value, last, ret) != last) );
+    } while ( unlikely(cmpxchg(&xen_timer_last, last, ret) != last) );
 
     return ret;
 }
 
+static void resume_xen_timer(struct platform_timesource *pts)
+{
+    write_atomic(&xen_timer_last, 0);
+}
+
 static struct platform_timesource __initdata plt_xen_timer =
 {
     .id = "xen",
     .name = "XEN PV CLOCK",
     .read_counter = read_xen_timer,
     .init = init_xen_timer,
+    .resume = resume_xen_timer,
     .counter_bits = 63,
 };
 #endif
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v2 1/2
#2 lore raw

Re: [Xen-devel] [PATCH v2 1/2] x86/shim: suspend and resume platform time correctly

Quoted 11 lines · Igor Druzhinin

Similarly to S3, platform time needs to be saved on guest suspend and restored on resume respectively. This should account for expected jumps in PV clock counter value after resume. time_suspend/resume() are safe to use in PVH setting as is since any existing operations with PIT/HPET that they do would simply be ignored if PIT/HPET is not present.

Additionally, add resume callback for Xen PV clocksource to avoid its breakage on migration.

Thanks.

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

#3 lore raw

PATCH v2 2/2 x86/time: report correct frequency of Xen PV clocksource

In reply to Igor Druzhinin

The value of the counter represents the number of nanoseconds since host boot. That means the correct frequency is always 1GHz.

This inconsistency caused time to go slower in PV shim on most platforms.

---
 xen/arch/x86/time.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
xen/arch/x86/time.c +2 -3
index 7e7a62e..26f9bad 100644
@@ -587,9 +587,7 @@ static int64_t __init init_xen_timer(struct platform_timesource *pts)
     if ( !xen_guest )
         return 0;
 
-    pts->frequency = xen_timer_cpu_frequency();
-
-    return pts->frequency;
+    return xen_timer_cpu_frequency();
 }
 
 static always_inline uint64_t read_cycle(const struct vcpu_time_info *info,
@@ -643,6 +641,7 @@ static struct platform_timesource __initdata plt_xen_timer =
 {
     .id = "xen",
     .name = "XEN PV CLOCK",
+    .frequency = 1000000000ULL,
     .read_counter = read_xen_timer,
     .init = init_xen_timer,
     .resume = resume_xen_timer,
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v2 2/2
#4 lore raw

Re: [Xen-devel] [PATCH v2 2/2] x86/time: report correct frequency of Xen PV clocksource

Quoted 7 lines · Igor Druzhinin

The value of the counter represents the number of nanoseconds since host boot. That means the correct frequency is always 1GHz.

This inconsistency caused time to go slower in PV shim on most platforms.

Thanks!

Quoted 20 lines
---
 xen/arch/x86/time.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
xen/arch/x86/time.c +1 -3
index 7e7a62e..26f9bad 100644
@@ -587,9 +587,7 @@ static int64_t __init init_xen_timer(struct platform_timesource *pts)
     if ( !xen_guest )
         return 0;
 
-    pts->frequency = xen_timer_cpu_frequency();
-
-    return pts->frequency;
+    return xen_timer_cpu_frequency();
 }
 
 static always_inline uint64_t read_cycle(const struct vcpu_time_info *info,

xen/arch/x86/time.c static struct platform_timesource __initdata plt_xen_timer = · line 644

Quoted 4 lines
xen/arch/x86/time.c +1 -0
@@ -643,6 +641,7 @@ static struct platform_timesource __initdata plt_xen_timer =
 {
     .id = "xen",
     .name = "XEN PV CLOCK",
+    .frequency = 1000000000ULL,

I think you can drop the type literal here?

Roger.

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