Skip to contentSkip to conversation

x86: reduce redundancy in tsc_[gs]et_info()

PATCH Superseded Jan Beulich 3 participants 4 messages Apr 30, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: reduce redundancy in tsc_[gs]et_info()

- some of the case statements are effectively or mostly special cases of others, so there's no good reason not to share the code
- in the "get" function, a variable can be made case-wide instead of having multiple instance of it (and those even with a pointless initializer)
- minor formatting adjustments

xen/arch/x86/time.c +25 -34
@@ -1786,26 +1786,22 @@ void tsc_get_info(struct domain *d, uint
 
     switch ( *tsc_mode )
     {
+        uint64_t tsc;
+
     case TSC_MODE_NEVER_EMULATE:
-        *elapsed_nsec =  *gtsc_khz = 0;
+        *elapsed_nsec = *gtsc_khz = 0;
         break;
-    case TSC_MODE_ALWAYS_EMULATE:
-        *elapsed_nsec = get_s_time() - d->arch.vtsc_offset;
-        *gtsc_khz =  d->arch.tsc_khz;
-         break;
     case TSC_MODE_DEFAULT:
         if ( d->arch.vtsc )
         {
+    case TSC_MODE_ALWAYS_EMULATE:
             *elapsed_nsec = get_s_time() - d->arch.vtsc_offset;
             *gtsc_khz =  d->arch.tsc_khz;
+            break;
         }
-        else
-        {
-            uint64_t tsc = 0;
-            rdtscll(tsc);
-            *elapsed_nsec = scale_delta(tsc,&d->arch.vtsc_to_ns);
-            *gtsc_khz =  cpu_khz;
-        }
+        rdtscll(tsc);
+        *elapsed_nsec = scale_delta(tsc, &d->arch.vtsc_to_ns);
+        *gtsc_khz =  cpu_khz;
         break;
     case TSC_MODE_PVRDTSCP:
         if ( d->arch.vtsc )
@@ -1815,10 +1811,9 @@ void tsc_get_info(struct domain *d, uint
         }
         else
         {
-            uint64_t tsc = 0;
             rdtscll(tsc);
-            *elapsed_nsec = (scale_delta(tsc,&d->arch.vtsc_to_ns) -
-                             d->arch.vtsc_offset);
+            *elapsed_nsec = scale_delta(tsc, &d->arch.vtsc_to_ns) -
+                            d->arch.vtsc_offset;
             *gtsc_khz = 0; /* ignored by tsc_set_info */
         }
         break;
@@ -1875,28 +1870,24 @@ void tsc_set_info(struct domain *d,
 
     switch ( d->arch.tsc_mode = tsc_mode )
     {
-    case TSC_MODE_NEVER_EMULATE:
-        d->arch.vtsc = 0;
-        break;
-    case TSC_MODE_ALWAYS_EMULATE:
-        d->arch.vtsc = 1;
-        d->arch.vtsc_offset = get_s_time() - elapsed_nsec;
-        d->arch.tsc_khz = gtsc_khz ? gtsc_khz : cpu_khz;
-        set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * 1000 );
-        d->arch.ns_to_vtsc = scale_reciprocal(d->arch.vtsc_to_ns);
-        break;
     case TSC_MODE_DEFAULT:
-        d->arch.vtsc = 1;
+    case TSC_MODE_ALWAYS_EMULATE:
         d->arch.vtsc_offset = get_s_time() - elapsed_nsec;
-        d->arch.tsc_khz = gtsc_khz ? gtsc_khz : cpu_khz;
-        set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * 1000 );
-        /* use native TSC if initial host has safe TSC, has not migrated
-         * yet and tsc_khz == cpu_khz */
-        if ( host_tsc_is_safe() && incarnation == 0 &&
-                d->arch.tsc_khz == cpu_khz )
+        d->arch.tsc_khz = gtsc_khz ?: cpu_khz;
+        set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * 1000);
+        /*
+         * In default mode use native TSC if the host has safe TSC,
+         * the VM has not migrated yet, and tsc_khz == cpu_khz.
+         */
+        if ( tsc_mode == TSC_MODE_DEFAULT && host_tsc_is_safe() &&
+             incarnation == 0 && d->arch.tsc_khz == cpu_khz )
+        {
+    case TSC_MODE_NEVER_EMULATE:
             d->arch.vtsc = 0;
-        else 
-            d->arch.ns_to_vtsc = scale_reciprocal(d->arch.vtsc_to_ns);
+            break;
+        }
+        d->arch.vtsc = 1;
+        d->arch.ns_to_vtsc = scale_reciprocal(d->arch.vtsc_to_ns);
         break;
     case TSC_MODE_PVRDTSCP:
         d->arch.vtsc =  boot_cpu_has(X86_FEATURE_RDTSCP) &&

x86: reduce redundancy in tsc_[gs]et_info()

- some of the case statements are effectively or mostly special cases of others, so there's no good reason not to share the code
- in the "get" function, a variable can be made case-wide instead of having multiple instance of it (and those even with a pointless initializer)
- minor formatting adjustments

xen/arch/x86/time.c +25 -34

89 lines not on this page · raw message

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#1 lore raw
Quoted 21 lines · Jan Beulich

- some of the case statements are effectively or mostly special cases of others, so there's no good reason not to share the code
- in the "get" function, a variable can be made case-wide instead of having multiple instance of it (and those even with a pointless initializer)
- minor formatting adjustments

xen/arch/x86/time.c +3 -1
@@ -1786,26 +1786,22 @@ void tsc_get_info(struct domain *d, uint
 
     switch ( *tsc_mode )
     {
+        uint64_t tsc;
+
     case TSC_MODE_NEVER_EMULATE:
-        *elapsed_nsec =  *gtsc_khz = 0;
+        *elapsed_nsec = *gtsc_khz = 0;
         break;

xen/arch/x86/time.c void tsc_get_info(struct domain *d, uint · line 1799

Quoted 10 lines
xen/arch/x86/time.c +1 -4
-    case TSC_MODE_ALWAYS_EMULATE:
-        *elapsed_nsec = get_s_time() - d->arch.vtsc_offset;
-        *gtsc_khz =  d->arch.tsc_khz;
-         break;
     case TSC_MODE_DEFAULT:
         if ( d->arch.vtsc )
         {
+    case TSC_MODE_ALWAYS_EMULATE:
             *elapsed_nsec = get_s_time() - d->arch.vtsc_offset;
             *gtsc_khz =  d->arch.tsc_khz;

double space after =

Quoted 18 lines
xen/arch/x86/time.c +4 -7
+            break;
         }
-        else
-        {
-            uint64_t tsc = 0;
-            rdtscll(tsc);
-            *elapsed_nsec = scale_delta(tsc,&d->arch.vtsc_to_ns);
-            *gtsc_khz =  cpu_khz;
-        }
+        rdtscll(tsc);
+        *elapsed_nsec = scale_delta(tsc, &d->arch.vtsc_to_ns);
+        *gtsc_khz =  cpu_khz;
         break;
     case TSC_MODE_PVRDTSCP:
         if ( d->arch.vtsc )
@@ -1815,10 +1811,9 @@ void tsc_get_info(struct domain *d, uint
         }
         else

xen/arch/x86/time.c void tsc_get_info(struct domain *d, uint · line 1819

Quoted 10 lines
xen/arch/x86/time.c +2 -3
         {
-            uint64_t tsc = 0;
             rdtscll(tsc);
-            *elapsed_nsec = (scale_delta(tsc,&d->arch.vtsc_to_ns) -
-                             d->arch.vtsc_offset);
+            *elapsed_nsec = scale_delta(tsc, &d->arch.vtsc_to_ns) -
+                            d->arch.vtsc_offset;
             *gtsc_khz = 0; /* ignored by tsc_set_info */
         }
         break;

There is a coverity complaint that following this switch statement, there is a read of *elapsed_nsec which might be uninitialised if tsc_mode missed on of the case statements. While I believe this is currently impossible, it might we wise to have a "default: BUG();" in case half a new tsc_mode appears.

Quoted 14 lines
xen/arch/x86/time.c +0 -10
@@ -1875,28 +1870,24 @@ void tsc_set_info(struct domain *d,
 
     switch ( d->arch.tsc_mode = tsc_mode )
     {
-    case TSC_MODE_NEVER_EMULATE:
-        d->arch.vtsc = 0;
-        break;
-    case TSC_MODE_ALWAYS_EMULATE:
-        d->arch.vtsc = 1;
-        d->arch.vtsc_offset = get_s_time() - elapsed_nsec;
-        d->arch.tsc_khz = gtsc_khz ? gtsc_khz : cpu_khz;
-        set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * 1000 );
-        d->arch.ns_to_vtsc = scale_reciprocal(d->arch.vtsc_to_ns);
-        break;

xen/arch/x86/time.c void tsc_set_info(struct domain *d, · line 1896

Quoted 10 lines
xen/arch/x86/time.c +1 -7
     case TSC_MODE_DEFAULT:
-        d->arch.vtsc = 1;
+    case TSC_MODE_ALWAYS_EMULATE:
         d->arch.vtsc_offset = get_s_time() - elapsed_nsec;
-        d->arch.tsc_khz = gtsc_khz ? gtsc_khz : cpu_khz;
-        set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * 1000 );
-        /* use native TSC if initial host has safe TSC, has not migrated
-         * yet and tsc_khz == cpu_khz */
-        if ( host_tsc_is_safe() && incarnation == 0 &&
-                d->arch.tsc_khz == cpu_khz )

This wont apply cleanly on top of staging. Boris made some changed in this area with his HVM guest TSC series.

~Andrew

Quoted 25 lines
xen/arch/x86/time.c +14 -2
+        d->arch.tsc_khz = gtsc_khz ?: cpu_khz;
+        set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * 1000);
+        /*
+         * In default mode use native TSC if the host has safe TSC,
+         * the VM has not migrated yet, and tsc_khz == cpu_khz.
+         */
+        if ( tsc_mode == TSC_MODE_DEFAULT && host_tsc_is_safe() &&
+             incarnation == 0 && d->arch.tsc_khz == cpu_khz )
+        {
+    case TSC_MODE_NEVER_EMULATE:
             d->arch.vtsc = 0;
-        else 
-            d->arch.ns_to_vtsc = scale_reciprocal(d->arch.vtsc_to_ns);
+            break;
+        }
+        d->arch.vtsc = 1;
+        d->arch.ns_to_vtsc = scale_reciprocal(d->arch.vtsc_to_ns);
         break;
     case TSC_MODE_PVRDTSCP:
         d->arch.vtsc =  boot_cpu_has(X86_FEATURE_RDTSCP) &&
Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#2 lore raw
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 30.04.14 at 16:48, <[email protected]> wrote:
Quoted 16 lines · Jan Beulich
xen/arch/x86/time.c +3 -5
@@ -1786,26 +1786,22 @@ void tsc_get_info(struct domain *d, uint
 
     switch ( *tsc_mode )
     {
+        uint64_t tsc;
+
     case TSC_MODE_NEVER_EMULATE:
-        *elapsed_nsec =  *gtsc_khz = 0;
+        *elapsed_nsec = *gtsc_khz = 0;
         break;
-    case TSC_MODE_ALWAYS_EMULATE:
-        *elapsed_nsec = get_s_time() - d->arch.vtsc_offset;
-        *gtsc_khz =  d->arch.tsc_khz;
-         break;
Quoted 8 lines

xen/arch/x86/time.c void tsc_get_info(struct domain *d, uint · line 1799

Quoted 6 lines
xen/arch/x86/time.c +1 -0
     case TSC_MODE_DEFAULT:
         if ( d->arch.vtsc )
         {
+    case TSC_MODE_ALWAYS_EMULATE:
             *elapsed_nsec = get_s_time() - d->arch.vtsc_offset;
             *gtsc_khz =  d->arch.tsc_khz;

double space after =

I left them untouched on lines I didn't need to touch anyway, but of course I can as well clean them up uniformly (there were two or three more of them).

Quoted 11 lines
Quoted 11 lines
xen/arch/x86/time.c +2 -3
@@ -1815,10 +1811,9 @@ void tsc_get_info(struct domain *d, uint
         }
         else
         {
-            uint64_t tsc = 0;
             rdtscll(tsc);
-            *elapsed_nsec = (scale_delta(tsc,&d->arch.vtsc_to_ns) -
-                             d->arch.vtsc_offset);
+            *elapsed_nsec = scale_delta(tsc, &d->arch.vtsc_to_ns) -
+                            d->arch.vtsc_offset;
             *gtsc_khz = 0; /* ignored by tsc_set_info */
Quoted 8 lines

xen/arch/x86/time.c void tsc_get_info(struct domain *d, uint · line 1819

Quoted 2 lines
xen/arch/x86/time.c +0 -0
         }
         break;

There is a coverity complaint that following this switch statement, there is a read of *elapsed_nsec which might be uninitialised if tsc_mode missed on of the case statements. While I believe this is currently impossible, it might we wise to have a "default: BUG();" in case half a new tsc_mode appears.

I'm not in favor of such constructs when it's sufficiently obvious that all cases are being handled.

Quoted 19 lines
Quoted 19 lines
xen/arch/x86/time.c +1 -12
@@ -1875,28 +1870,24 @@ void tsc_set_info(struct domain *d,
 
     switch ( d->arch.tsc_mode = tsc_mode )
     {
-    case TSC_MODE_NEVER_EMULATE:
-        d->arch.vtsc = 0;
-        break;
-    case TSC_MODE_ALWAYS_EMULATE:
-        d->arch.vtsc = 1;
-        d->arch.vtsc_offset = get_s_time() - elapsed_nsec;
-        d->arch.tsc_khz = gtsc_khz ? gtsc_khz : cpu_khz;
-        set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * 1000 );
-        d->arch.ns_to_vtsc = scale_reciprocal(d->arch.vtsc_to_ns);
-        break;
     case TSC_MODE_DEFAULT:
-        d->arch.vtsc = 1;
+    case TSC_MODE_ALWAYS_EMULATE:
         d->arch.vtsc_offset = get_s_time() - elapsed_nsec;
-        d->arch.tsc_khz = gtsc_khz ? gtsc_khz : cpu_khz;
Quoted 8 lines

xen/arch/x86/time.c void tsc_set_info(struct domain *d, · line 1896

Quoted 5 lines
xen/arch/x86/time.c +0 -5
-        set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * 1000 );
-        /* use native TSC if initial host has safe TSC, has not migrated
-         * yet and tsc_khz == cpu_khz */
-        if ( host_tsc_is_safe() && incarnation == 0 &&
-                d->arch.tsc_khz == cpu_khz )

This wont apply cleanly on top of staging. Boris made some changed in this area with his HVM guest TSC series.

Right - I forgot I need to re-sync before submitting. Will be a v2 with the above formatting changes also taken care of.

Jan

Reviewed-by on v1 1/1
#3 lore raw
In reply to Jan Beulich
Quoted 8 lines · Jan Beulich

- some of the case statements are effectively or mostly special cases of others, so there's no good reason not to share the code
- in the "get" function, a variable can be made case-wide instead of having multiple instance of it (and those even with a pointless initializer)
- minor formatting adjustments

Quoted 100 lines
xen/arch/x86/time.c +25 -34
@@ -1786,26 +1786,22 @@ void tsc_get_info(struct domain *d, uint
 
      switch ( *tsc_mode )
      {
+        uint64_t tsc;
+
      case TSC_MODE_NEVER_EMULATE:
-        *elapsed_nsec =  *gtsc_khz = 0;
+        *elapsed_nsec = *gtsc_khz = 0;
          break;
-    case TSC_MODE_ALWAYS_EMULATE:
-        *elapsed_nsec = get_s_time() - d->arch.vtsc_offset;
-        *gtsc_khz =  d->arch.tsc_khz;
-         break;
      case TSC_MODE_DEFAULT:
          if ( d->arch.vtsc )
          {
+    case TSC_MODE_ALWAYS_EMULATE:
              *elapsed_nsec = get_s_time() - d->arch.vtsc_offset;
              *gtsc_khz =  d->arch.tsc_khz;
+            break;
          }
-        else
-        {
-            uint64_t tsc = 0;
-            rdtscll(tsc);
-            *elapsed_nsec = scale_delta(tsc,&d->arch.vtsc_to_ns);
-            *gtsc_khz =  cpu_khz;
-        }
+        rdtscll(tsc);
+        *elapsed_nsec = scale_delta(tsc, &d->arch.vtsc_to_ns);
+        *gtsc_khz =  cpu_khz;
          break;
      case TSC_MODE_PVRDTSCP:
          if ( d->arch.vtsc )
@@ -1815,10 +1811,9 @@ void tsc_get_info(struct domain *d, uint
          }
          else
          {
-            uint64_t tsc = 0;
              rdtscll(tsc);
-            *elapsed_nsec = (scale_delta(tsc,&d->arch.vtsc_to_ns) -
-                             d->arch.vtsc_offset);
+            *elapsed_nsec = scale_delta(tsc, &d->arch.vtsc_to_ns) -
+                            d->arch.vtsc_offset;
              *gtsc_khz = 0; /* ignored by tsc_set_info */
          }
          break;
@@ -1875,28 +1870,24 @@ void tsc_set_info(struct domain *d,
 
      switch ( d->arch.tsc_mode = tsc_mode )
      {
-    case TSC_MODE_NEVER_EMULATE:
-        d->arch.vtsc = 0;
-        break;
-    case TSC_MODE_ALWAYS_EMULATE:
-        d->arch.vtsc = 1;
-        d->arch.vtsc_offset = get_s_time() - elapsed_nsec;
-        d->arch.tsc_khz = gtsc_khz ? gtsc_khz : cpu_khz;
-        set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * 1000 );
-        d->arch.ns_to_vtsc = scale_reciprocal(d->arch.vtsc_to_ns);
-        break;
      case TSC_MODE_DEFAULT:
-        d->arch.vtsc = 1;
+    case TSC_MODE_ALWAYS_EMULATE:
          d->arch.vtsc_offset = get_s_time() - elapsed_nsec;
-        d->arch.tsc_khz = gtsc_khz ? gtsc_khz : cpu_khz;
-        set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * 1000 );
-        /* use native TSC if initial host has safe TSC, has not migrated
-         * yet and tsc_khz == cpu_khz */
-        if ( host_tsc_is_safe() && incarnation == 0 &&
-                d->arch.tsc_khz == cpu_khz )
+        d->arch.tsc_khz = gtsc_khz ?: cpu_khz;
+        set_time_scale(&d->arch.vtsc_to_ns, d->arch.tsc_khz * 1000);
+        /*
+         * In default mode use native TSC if the host has safe TSC,
+         * the VM has not migrated yet, and tsc_khz == cpu_khz.
+         */
+        if ( tsc_mode == TSC_MODE_DEFAULT && host_tsc_is_safe() &&
+             incarnation == 0 && d->arch.tsc_khz == cpu_khz )
+        {
+    case TSC_MODE_NEVER_EMULATE:
              d->arch.vtsc = 0;
-        else
-            d->arch.ns_to_vtsc = scale_reciprocal(d->arch.vtsc_to_ns);
+            break;
+        }
+        d->arch.vtsc = 1;
+        d->arch.ns_to_vtsc = scale_reciprocal(d->arch.vtsc_to_ns);
          break;
      case TSC_MODE_PVRDTSCP:
          d->arch.vtsc =  boot_cpu_has(X86_FEATURE_RDTSCP) &&
Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel