Skip to contentSkip to conversation

x86/time: Don't use virtual TSC if host and guest frequencies are equal

PATCH Superseded Boris Ostrovsky 2 participants 8 messages Mar 16, 2017 – Mar 17, 2017

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/time: Don't use virtual TSC if host and guest frequencies are equal

Commit 82713ec8d2 ("x86: use native RDTSC(P) execution when guest and host frequencies are the same") left out optimization for PV guests when host and guest run at the same frequency.

For such a case we should be able not to use virtual TSC regardless of whether we are runing before or after a migration (i.e. regardless of incarnation value).

---
Changes in v2:
* Replaced unnecessary incarnation test with an ASSERT

 xen/arch/x86/time.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
xen/arch/x86/time.c +5 -10
index faa638b..46a00f6 100644
@@ -2051,17 +2051,12 @@ void tsc_set_info(struct domain *d,
         d->arch.vtsc_offset = get_s_time() - elapsed_nsec;
         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 and:
-         *  HVM/PVH: host and guest frequencies are the same (either
-         *           "naturally" or via TSC scaling)
-         *  PV: guest has not migrated yet (and thus arch.tsc_khz == cpu_khz)
-         */
+
+        ASSERT(incarnation || d->arch.tsc_khz == cpu_khz);
         if ( tsc_mode == TSC_MODE_DEFAULT && host_tsc_is_safe() &&
-             (has_hvm_container_domain(d) ?
-              (d->arch.tsc_khz == cpu_khz ||
-               hvm_get_tsc_scaling_ratio(d->arch.tsc_khz)) :
-              incarnation == 0) )
+             (d->arch.tsc_khz == cpu_khz ||
+              (has_hvm_container_domain(d) &&
+               hvm_get_tsc_scaling_ratio(d->arch.tsc_khz))) )
         {
     case TSC_MODE_NEVER_EMULATE:
             d->arch.vtsc = 0;
Signature · 7 lines
1.8.3.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 16.03.17 at 20:35, <[email protected]> wrote:
xen/arch/x86/time.c +0 -0
@@ -2051,17 +2051,12 @@ void tsc_set_info(struct domain *d,
         d->arch.vtsc_offset = get_s_time() - elapsed_nsec;

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

Quoted 10 lines
xen/arch/x86/time.c +2 -6
         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 and:
-         *  HVM/PVH: host and guest frequencies are the same (either
-         *           "naturally" or via TSC scaling)
-         *  PV: guest has not migrated yet (and thus arch.tsc_khz == cpu_khz)
-         */
+
+        ASSERT(incarnation || d->arch.tsc_khz == cpu_khz);

Hmm, is this valid for other than TSC_MODE_DEFAULT?

Jan

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

Quoted 11 lines
xen/arch/x86/time.c +3 -4
         if ( tsc_mode == TSC_MODE_DEFAULT && host_tsc_is_safe() &&
-             (has_hvm_container_domain(d) ?
-              (d->arch.tsc_khz == cpu_khz ||
-               hvm_get_tsc_scaling_ratio(d->arch.tsc_khz)) :
-              incarnation == 0) )
+             (d->arch.tsc_khz == cpu_khz ||
+              (has_hvm_container_domain(d) &&
+               hvm_get_tsc_scaling_ratio(d->arch.tsc_khz))) )
         {
     case TSC_MODE_NEVER_EMULATE:
             d->arch.vtsc = 0;

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

#2 lore raw
Quoted 16 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 16.03.17 at 20:35, <[email protected]> wrote:
xen/arch/x86/time.c +0 -0
@@ -2051,17 +2051,12 @@ void tsc_set_info(struct domain *d,
         d->arch.vtsc_offset = get_s_time() - elapsed_nsec;

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

Quoted 10 lines
xen/arch/x86/time.c +2 -6
         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 and:
-         *  HVM/PVH: host and guest frequencies are the same (either
-         *           "naturally" or via TSC scaling)
-         *  PV: guest has not migrated yet (and thus arch.tsc_khz == cpu_khz)
-         */
+
+        ASSERT(incarnation || d->arch.tsc_khz == cpu_khz);

Hmm, is this valid for other than TSC_MODE_DEFAULT?

It is valid for all modes but I thought that the ASSERT is really only "interesting" for DEFAULT and ALWAYS_EMULATE since this is when we decide whether or not to set vtsc.

Since I need to rebase this anyway (due to PVH1 removal) I can move this down right after the switch if you feel it would be useful.

-boris

Quoted 15 lines

Jan

Quoted 11 lines
xen/arch/x86/time.c +3 -4
         if ( tsc_mode == TSC_MODE_DEFAULT && host_tsc_is_safe() &&
-             (has_hvm_container_domain(d) ?
-              (d->arch.tsc_khz == cpu_khz ||
-               hvm_get_tsc_scaling_ratio(d->arch.tsc_khz)) :
-              incarnation == 0) )
+             (d->arch.tsc_khz == cpu_khz ||
+              (has_hvm_container_domain(d) &&
+               hvm_get_tsc_scaling_ratio(d->arch.tsc_khz))) )
         {
     case TSC_MODE_NEVER_EMULATE:
             d->arch.vtsc = 0;

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

#3 lore raw
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 17.03.17 at 14:36, <[email protected]> wrote:
Quoted 16 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 16.03.17 at 20:35, <[email protected]> wrote:
xen/arch/x86/time.c +0 -0
@@ -2051,17 +2051,12 @@ void tsc_set_info(struct domain *d,
         d->arch.vtsc_offset = get_s_time() - elapsed_nsec;

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

Quoted 10 lines
xen/arch/x86/time.c +2 -6
         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 and:
-         *  HVM/PVH: host and guest frequencies are the same (either
-         *           "naturally" or via TSC scaling)
-         *  PV: guest has not migrated yet (and thus arch.tsc_khz == cpu_khz)
-         */
+
+        ASSERT(incarnation || d->arch.tsc_khz == cpu_khz);

Hmm, is this valid for other than TSC_MODE_DEFAULT?

Quoted 6 lines

It is valid for all modes but I thought that the ASSERT is really only "interesting" for DEFAULT and ALWAYS_EMULATE since this is when we decide whether or not to set vtsc.

Since I need to rebase this anyway (due to PVH1 removal) I can move this down right after the switch if you feel it would be useful.

Actually I think the other way around: For ALWAYS_EMULATE as well as for PVRDTSCP I don't think the assertion is valid, the more that d->arch.tsc_khz gets set from input to the function. That last fact actually makes the ASSERT() dubious in all cases, I'm afraid.

Jan

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

#4 lore raw
Quoted 22 lines · Jan Beulich
Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 17.03.17 at 14:36, <[email protected]> wrote:
Quoted 16 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 16.03.17 at 20:35, <[email protected]> wrote:
xen/arch/x86/time.c +0 -0
@@ -2051,17 +2051,12 @@ void tsc_set_info(struct domain *d,
         d->arch.vtsc_offset = get_s_time() - elapsed_nsec;

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

Quoted 10 lines
xen/arch/x86/time.c +2 -6
         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 and:
-         *  HVM/PVH: host and guest frequencies are the same (either
-         *           "naturally" or via TSC scaling)
-         *  PV: guest has not migrated yet (and thus arch.tsc_khz == cpu_khz)
-         */
+
+        ASSERT(incarnation || d->arch.tsc_khz == cpu_khz);

Hmm, is this valid for other than TSC_MODE_DEFAULT?

It is valid for all modes but I thought that the ASSERT is really only "interesting" for DEFAULT and ALWAYS_EMULATE since this is when we decide whether or not to set vtsc.

Quoted 6 lines
Quoted 2 lines

Since I need to rebase this anyway (due to PVH1 removal) I can move this down right after the switch if you feel it would be useful.

Actually I think the other way around: For ALWAYS_EMULATE as well as for PVRDTSCP I don't think the assertion is valid, the more that d->arch.tsc_khz gets set from input to the function. That last fact actually makes the ASSERT() dubious in all cases, I'm afraid.

It is valid (in the sense that it will evaluate to true) because we always first call tsc_set_info with DEFAULT mode and with gtsc_khz=0 from arch_domain_create(). So d->arch.tsc_khz will be primed to cpu_khz.

-boris

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

#5 lore raw
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 17.03.17 at 15:50, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 17.03.17 at 14:36, <[email protected]> wrote:
Quoted 12 lines · Jan Beulich

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

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 16.03.17 at 20:35, <[email protected]> wrote:
xen/arch/x86/time.c +0 -5
@@ -2051,17 +2051,12 @@ void tsc_set_info(struct domain *d,
         d->arch.vtsc_offset = get_s_time() - elapsed_nsec;
         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 and:
-         *  HVM/PVH: host and guest frequencies are the same (either
-         *           "naturally" or via TSC scaling)
-         *  PV: guest has not migrated yet (and thus arch.tsc_khz == 

cpu_khz)

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

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

Quoted 3 lines
xen/arch/x86/time.c +2 -1
-         */
+
+        ASSERT(incarnation || d->arch.tsc_khz == cpu_khz);

Hmm, is this valid for other than TSC_MODE_DEFAULT?

It is valid for all modes but I thought that the ASSERT is really only "interesting" for DEFAULT and ALWAYS_EMULATE since this is when we decide whether or not to set vtsc.

Since I need to rebase this anyway (due to PVH1 removal) I can move this down right after the switch if you feel it would be useful.

Quoted 8 lines
Quoted 4 lines

Actually I think the other way around: For ALWAYS_EMULATE as well as for PVRDTSCP I don't think the assertion is valid, the more that d->arch.tsc_khz gets set from input to the function. That last fact actually makes the ASSERT() dubious in all cases, I'm afraid.

It is valid (in the sense that it will evaluate to true) because we always first call tsc_set_info with DEFAULT mode and with gtsc_khz=0 from arch_domain_create(). So d->arch.tsc_khz will be primed to cpu_khz.

It is valid for this specific call. A malicious tool stack could easily pass incarnation zero to the domctl together with a random gtsc_khz.

Jan

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

#6 lore raw
Quoted 31 lines · Jan Beulich
Quoted 31 lines
Quoted 1 line
Quoted 1 line · On 17.03.17 at 15:50, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 17.03.17 at 14:36, <[email protected]> wrote:
Quoted 12 lines · Jan Beulich

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

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 16.03.17 at 20:35, <[email protected]> wrote:
xen/arch/x86/time.c +0 -5
@@ -2051,17 +2051,12 @@ void tsc_set_info(struct domain *d,
         d->arch.vtsc_offset = get_s_time() - elapsed_nsec;
         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 and:
-         *  HVM/PVH: host and guest frequencies are the same (either
-         *           "naturally" or via TSC scaling)
-         *  PV: guest has not migrated yet (and thus arch.tsc_khz == 

cpu_khz)

Quoted 14 lines
Quoted 10 lines
Quoted 4 lines

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

Quoted 3 lines
xen/arch/x86/time.c +2 -1
-         */
+
+        ASSERT(incarnation || d->arch.tsc_khz == cpu_khz);

Hmm, is this valid for other than TSC_MODE_DEFAULT?

It is valid for all modes but I thought that the ASSERT is really only "interesting" for DEFAULT and ALWAYS_EMULATE since this is when we decide whether or not to set vtsc.

Since I need to rebase this anyway (due to PVH1 removal) I can move this down right after the switch if you feel it would be useful.

Actually I think the other way around: For ALWAYS_EMULATE as well as for PVRDTSCP I don't think the assertion is valid, the more that d->arch.tsc_khz gets set from input to the function. That last fact actually makes the ASSERT() dubious in all cases, I'm afraid.

Quoted 6 lines
Quoted 3 lines

It is valid (in the sense that it will evaluate to true) because we always first call tsc_set_info with DEFAULT mode and with gtsc_khz=0 from arch_domain_create(). So d->arch.tsc_khz will be primed to cpu_khz.

It is valid for this specific call. A malicious tool stack could easily pass incarnation zero to the domctl together with a random gtsc_khz.

So how do you want to go about this then? Original (but rebased) patch, remove incarnation check and add a comment stating that there is no need to check it?

-boris

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

#7 lore raw
Quoted 36 lines
Quoted 1 line
Quoted 1 line · On 17.03.17 at 16:13, <[email protected]> wrote:
Quoted 34 lines · Jan Beulich
Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 17.03.17 at 15:50, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 17.03.17 at 14:36, <[email protected]> wrote:
Quoted 12 lines · Jan Beulich

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

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 16.03.17 at 20:35, <[email protected]> wrote:
xen/arch/x86/time.c +0 -5
@@ -2051,17 +2051,12 @@ void tsc_set_info(struct domain *d,
         d->arch.vtsc_offset = get_s_time() - elapsed_nsec;
         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 and:
-         *  HVM/PVH: host and guest frequencies are the same (either
-         *           "naturally" or via TSC scaling)
-         *  PV: guest has not migrated yet (and thus arch.tsc_khz == 

cpu_khz)

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

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

Quoted 3 lines
xen/arch/x86/time.c +2 -1
-         */
+
+        ASSERT(incarnation || d->arch.tsc_khz == cpu_khz);

Hmm, is this valid for other than TSC_MODE_DEFAULT?

It is valid for all modes but I thought that the ASSERT is really only "interesting" for DEFAULT and ALWAYS_EMULATE since this is when we decide whether or not to set vtsc.

Quoted 6 lines
Quoted 2 lines

Since I need to rebase this anyway (due to PVH1 removal) I can move this down right after the switch if you feel it would be useful.

Actually I think the other way around: For ALWAYS_EMULATE as well as for PVRDTSCP I don't think the assertion is valid, the more that d->arch.tsc_khz gets set from input to the function. That last fact actually makes the ASSERT() dubious in all cases, I'm afraid.

It is valid (in the sense that it will evaluate to true) because we always first call tsc_set_info with DEFAULT mode and with gtsc_khz=0 from arch_domain_create(). So d->arch.tsc_khz will be primed to cpu_khz.

Quoted 8 lines
Quoted 3 lines

It is valid for this specific call. A malicious tool stack could easily pass incarnation zero to the domctl together with a random gtsc_khz.

So how do you want to go about this then? Original (but rebased) patch, remove incarnation check and add a comment stating that there is no need to check it?

v2 patch with ASSERT() changed to comment, I would say.

Jan

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