Skip to contentSkip to conversation

x86/vtsc: update vcpu_time after hvm_set_guest_time

PATCH Superseded Roger Pau Monne 4 participants 14 messages Jun 4, 2013

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/vtsc: update vcpu_time after hvm_set_guest_time

When using a vtsc, hvm_set_guest_time changes hvm_vcpu.stime_offset, which is used in the vcpu time structure to calculate the tsc_timestamp, so after updating stime_offset we need to propagate the change to vcpu_time in order for the guest to get the right time if using the PV clock.

This was not done correctly, since in context_switch update_vcpu_system_time was called before vmx_do_resume, which caused the vcpu_info time structure to be updated with the wrong values. This patch fixes this by calling update_vcpu_system_time after the call to hvm_set_guest_time has happened.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc George Dunlap <[email protected]>
---
 xen/arch/x86/hvm/hvm.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
xen/arch/x86/hvm/hvm.c +6 -0
index a962ce2..e257dcf 100644
@@ -343,6 +343,12 @@ void hvm_do_resume(struct vcpu *v)
     ioreq_t *p;
 
     pt_restore_timer(v);
+    /*
+     * Update vcpu_info, since the call to pt_restore_timer can change
+     * the value in v->arch.hvm_vcpu.stime_offset that is used
+     * to calculate the TSC in vcpu_info->time.
+     */
+    update_vcpu_system_time(v);
 
     check_wakeup_from_wait();
 
Signature · 7 lines
1.7.7.5 (Apple Git-26)


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#1 lore raw
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Roger Pau Monne

When using a vtsc, hvm_set_guest_time changes hvm_vcpu.stime_offset, which is used in the vcpu time structure to calculate the tsc_timestamp, so after updating stime_offset we need to propagate the change to vcpu_time in order for the guest to get the right time if using the PV clock.

Quoted 5 lines

This was not done correctly, since in context_switch update_vcpu_system_time was called before vmx_do_resume, which caused the vcpu_info time structure to be updated with the wrong values. This patch fixes this by calling update_vcpu_system_time after the call to hvm_set_guest_time has happened.

So at the first glance I was thinking this would be fixing a regression from commit ae5092f420e87a4a6b541bf581378c8cc0ee3a99, but after a closer look it looks like this was done even earlier before. Can you confirm this (not the least because this would have implications on the need to backport this change)?

xen/arch/x86/hvm/hvm.c void hvm_do_resume(struct vcpu *v) · line 351

Quoted 12 lines
xen/arch/x86/hvm/hvm.c +6 -0
@@ -343,6 +343,12 @@ void hvm_do_resume(struct vcpu *v)
     ioreq_t *p;
 
     pt_restore_timer(v);
+    /*
+     * Update vcpu_info, since the call to pt_restore_timer can change
+     * the value in v->arch.hvm_vcpu.stime_offset that is used
+     * to calculate the TSC in vcpu_info->time.
+     */
+    update_vcpu_system_time(v);

Adding it here means, unless I'm mistaken, the one in context_switch() is now pointless, so I'd encourage you to gate that one on !is_hvm_vcpu() (with a comment saying that in this case it's being done in hvm_do_resume()).

Jan

#2 lore raw
Quoted 13 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Roger Pau Monne

When using a vtsc, hvm_set_guest_time changes hvm_vcpu.stime_offset, which is used in the vcpu time structure to calculate the tsc_timestamp, so after updating stime_offset we need to propagate the change to vcpu_time in order for the guest to get the right time if using the PV clock.

Quoted 5 lines

This was not done correctly, since in context_switch update_vcpu_system_time was called before vmx_do_resume, which caused the vcpu_info time structure to be updated with the wrong values. This patch fixes this by calling update_vcpu_system_time after the call to hvm_set_guest_time has happened.

Quoted 5 lines

So at the first glance I was thinking this would be fixing a regression from commit ae5092f420e87a4a6b541bf581378c8cc0ee3a99, but after a closer look it looks like this was done even earlier before. Can you confirm this (not the least because this would have implications on the need to backport this change)?

I've took a look at the commit, and I don't think it introduced a regression, a call to update_vcpu_system_time was removed, but this call was also made before calling context_switch, which wouldn't fix the problem at hand. This should be backported to all the versions that expose the XENFEAT_hvm_safe_pvclock feature.

Quoted 6 lines
Quoted 6 lines
xen/arch/x86/hvm/hvm.c +0 -0
@@ -343,6 +343,12 @@ void hvm_do_resume(struct vcpu *v)
     ioreq_t *p;
 
Quoted 12 lines

xen/arch/x86/hvm/hvm.c void hvm_do_resume(struct vcpu *v) · line 351

Quoted 7 lines
xen/arch/x86/hvm/hvm.c +6 -0
     pt_restore_timer(v);
+    /*
+     * Update vcpu_info, since the call to pt_restore_timer can change
+     * the value in v->arch.hvm_vcpu.stime_offset that is used
+     * to calculate the TSC in vcpu_info->time.
+     */
+    update_vcpu_system_time(v);

Adding it here means, unless I'm mistaken, the one in context_switch() is now pointless, so I'd encourage you to gate that one on !is_hvm_vcpu() (with a comment saying that in this case it's being done in hvm_do_resume()).

Yes, the call in context_switch is now superseded by the one in hvm_do_resume for the HVM case. I will change it and resend the patch, thanks for the review.

#3 lore raw
Quoted 45 lines · Roger Pau Monné
Quoted 13 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Roger Pau Monne

When using a vtsc, hvm_set_guest_time changes hvm_vcpu.stime_offset, which is used in the vcpu time structure to calculate the tsc_timestamp, so after updating stime_offset we need to propagate the change to vcpu_time in order for the guest to get the right time if using the PV clock.

Quoted 5 lines

This was not done correctly, since in context_switch update_vcpu_system_time was called before vmx_do_resume, which caused the vcpu_info time structure to be updated with the wrong values. This patch fixes this by calling update_vcpu_system_time after the call to hvm_set_guest_time has happened.

Quoted 5 lines

So at the first glance I was thinking this would be fixing a regression from commit ae5092f420e87a4a6b541bf581378c8cc0ee3a99, but after a closer look it looks like this was done even earlier before. Can you confirm this (not the least because this would have implications on the need to backport this change)?

I've took a look at the commit, and I don't think it introduced a regression, a call to update_vcpu_system_time was removed, but this call was also made before calling context_switch, which wouldn't fix the problem at hand. This should be backported to all the versions that expose the XENFEAT_hvm_safe_pvclock feature.

Quoted 6 lines
Quoted 6 lines
xen/arch/x86/hvm/hvm.c +0 -0
@@ -343,6 +343,12 @@ void hvm_do_resume(struct vcpu *v)
      ioreq_t *p;
 
Quoted 12 lines

xen/arch/x86/hvm/hvm.c void hvm_do_resume(struct vcpu *v) · line 351

Quoted 7 lines
xen/arch/x86/hvm/hvm.c +6 -0
      pt_restore_timer(v);
+    /*
+     * Update vcpu_info, since the call to pt_restore_timer can change
+     * the value in v->arch.hvm_vcpu.stime_offset that is used
+     * to calculate the TSC in vcpu_info->time.
+     */
+    update_vcpu_system_time(v);

Adding it here means, unless I'm mistaken, the one in context_switch() is now pointless, so I'd encourage you to gate that one on !is_hvm_vcpu() (with a comment saying that in this case it's being done in hvm_do_resume()).

Quoted 3 lines

Yes, the call in context_switch is now superseded by the one in hvm_do_resume for the HVM case. I will change it and resend the patch, thanks for the review.

I agree that it's worth trying to avoid calling the same function twice; but since the common case is for pt_restore_timer() to not actually make any substantial changes to hvm_vcpu.stime_offset, I think it would be better if we had the basic "update the system time" call shared between HVM and PV codepaths, and have the uncommon case where hvm_vcpu.stime_offset does change just call it twice.

Updating it at hvm_set_guest_time() will also make sure that there wont' be any problems between the update at pt_intr_post() and the next time the vcpu is scheduled.

-George

#4 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line · George Dunlap
Quoted 3 lines · Roger Pau Monné

Yes, the call in context_switch is now superseded by the one in hvm_do_resume for the HVM case. I will change it and resend the patch, thanks for the review.

Quoted 6 lines

I agree that it's worth trying to avoid calling the same function twice; but since the common case is for pt_restore_timer() to not actually make any substantial changes to hvm_vcpu.stime_offset, I think it would be better if we had the basic "update the system time" call shared between HVM and PV codepaths, and have the uncommon case where hvm_vcpu.stime_offset does change just call it twice.

Makes sense to me.

Jan

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

#5 lore raw
In reply to Roger Pau Monne
Quoted 11 lines · Roger Pau Monne

When using a vtsc, hvm_set_guest_time changes hvm_vcpu.stime_offset, which is used in the vcpu time structure to calculate the tsc_timestamp, so after updating stime_offset we need to propagate the change to vcpu_time in order for the guest to get the right time if using the PV clock.

This was not done correctly, since in context_switch update_vcpu_system_time was called before vmx_do_resume, which caused the vcpu_info time structure to be updated with the wrong values. This patch fixes this by calling update_vcpu_system_time after the call to hvm_set_guest_time has happened.

Would it make more sense to actually do this in hvm_set_guest_time() instead, so that this window where the vcpu system_time is closed for all callers, not just hvm_do_resume?

You could gate calling update_vcpu_system_time on:
1. Whether stime_offset actually changed
2. Whether v is currently running

-George

#6 lore raw
Quoted 13 lines · George Dunlap
Quoted 11 lines · Roger Pau Monne

When using a vtsc, hvm_set_guest_time changes hvm_vcpu.stime_offset, which is used in the vcpu time structure to calculate the tsc_timestamp, so after updating stime_offset we need to propagate the change to vcpu_time in order for the guest to get the right time if using the PV clock.

This was not done correctly, since in context_switch update_vcpu_system_time was called before vmx_do_resume, which caused the vcpu_info time structure to be updated with the wrong values. This patch fixes this by calling update_vcpu_system_time after the call to hvm_set_guest_time has happened.

Quoted 7 lines

Would it make more sense to actually do this in hvm_set_guest_time() instead, so that this window where the vcpu system_time is closed for all callers, not just hvm_do_resume?

You could gate calling update_vcpu_system_time on:
1. Whether stime_offset actually changed
2. Whether v is currently running

This was my first approach, but I thought it was better to fix the actual call to update_vcpu_system_time to happen in the right place (before the context switch), rather than adding more calls to update_vcpu_system_time all over the place.

If later we decide for whatever reason to add more offsets to the vtsc, we will also have to add more calls to update_vcpu_system_time in every place that we modify those offsets, which doesn't seem right.

#7 lore raw
Quoted 22 lines · Roger Pau Monné
Quoted 13 lines · George Dunlap
Quoted 11 lines · Roger Pau Monne

When using a vtsc, hvm_set_guest_time changes hvm_vcpu.stime_offset, which is used in the vcpu time structure to calculate the tsc_timestamp, so after updating stime_offset we need to propagate the change to vcpu_time in order for the guest to get the right time if using the PV clock.

This was not done correctly, since in context_switch update_vcpu_system_time was called before vmx_do_resume, which caused the vcpu_info time structure to be updated with the wrong values. This patch fixes this by calling update_vcpu_system_time after the call to hvm_set_guest_time has happened.

Quoted 7 lines

Would it make more sense to actually do this in hvm_set_guest_time() instead, so that this window where the vcpu system_time is closed for all callers, not just hvm_do_resume?

You could gate calling update_vcpu_system_time on:
1. Whether stime_offset actually changed
2. Whether v is currently running

Quoted 8 lines

This was my first approach, but I thought it was better to fix the actual call to update_vcpu_system_time to happen in the right place (before the context switch), rather than adding more calls to update_vcpu_system_time all over the place.

If later we decide for whatever reason to add more offsets to the vtsc, we will also have to add more calls to update_vcpu_system_time in every place that we modify those offsets, which doesn't seem right.

But you will have to add those anyway if the guest is running. Otherwise you'll have this same window, where vcpu_info.system_time is out of sync with hvm_vcpu.stime_offset, and this same kind of bug can happen.

-George

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

#8 lore raw
Quoted 28 lines · George Dunlap
Quoted 27 lines · Roger Pau Monné
Quoted 13 lines · George Dunlap
Quoted 11 lines · Roger Pau Monne

When using a vtsc, hvm_set_guest_time changes hvm_vcpu.stime_offset, which is used in the vcpu time structure to calculate the tsc_timestamp, so after updating stime_offset we need to propagate the change to vcpu_time in order for the guest to get the right time if using the PV clock.

This was not done correctly, since in context_switch update_vcpu_system_time was called before vmx_do_resume, which caused the vcpu_info time structure to be updated with the wrong values. This patch fixes this by calling update_vcpu_system_time after the call to hvm_set_guest_time has happened.

Quoted 7 lines

Would it make more sense to actually do this in hvm_set_guest_time() instead, so that this window where the vcpu system_time is closed for all callers, not just hvm_do_resume?

You could gate calling update_vcpu_system_time on:
1. Whether stime_offset actually changed
2. Whether v is currently running

This was my first approach, but I thought it was better to fix the actual call to update_vcpu_system_time to happen in the right place (before the context switch), rather than adding more calls to update_vcpu_system_time all over the place.

Quoted 7 lines
Quoted 3 lines

If later we decide for whatever reason to add more offsets to the vtsc, we will also have to add more calls to update_vcpu_system_time in every place that we modify those offsets, which doesn't seem right.

But you will have to add those anyway if the guest is running. Otherwise you'll have this same window, where vcpu_info.system_time is out of sync with hvm_vcpu.stime_offset, and this same kind of bug can happen.

This could be solved by also adding a call to update_vcpu_system_time in pt_intr_post, but given that I'm not sure any more if it's not best to just do the call to update_vcpu_system_time in hvm_set_guest_time and get done with it.

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

#9 lore raw
In reply to George Dunlap
Quoted 13 lines · George Dunlap
Quoted 11 lines · Roger Pau Monne

When using a vtsc, hvm_set_guest_time changes hvm_vcpu.stime_offset, which is used in the vcpu time structure to calculate the tsc_timestamp, so after updating stime_offset we need to propagate the change to vcpu_time in order for the guest to get the right time if using the PV clock.

This was not done correctly, since in context_switch update_vcpu_system_time was called before vmx_do_resume, which caused the vcpu_info time structure to be updated with the wrong values. This patch fixes this by calling update_vcpu_system_time after the call to hvm_set_guest_time has happened.

Quoted 7 lines

Would it make more sense to actually do this in hvm_set_guest_time() instead, so that this window where the vcpu system_time is closed for all callers, not just hvm_do_resume?

You could gate calling update_vcpu_system_time on:
1. Whether stime_offset actually changed
2. Whether v is currently running

I'm not sure if only updating the vcpu time if the vCPU is running can lead to a slip in the vcpu time update. As an example, if we call hvm_set_guest_time with the vCPU not running and change the offset, and then call it again on resume, but the offset has not changed this time the vcpu time will not be updated. I would rather update the vcpu time info every time there's a change in the offset.

#10 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line · Roger Pau Monné
Quoted 3 lines · George Dunlap

You could gate calling update_vcpu_system_time on:
1. Whether stime_offset actually changed
2. Whether v is currently running

I'm not sure if only updating the vcpu time if the vCPU is running can lead to a slip in the vcpu time update. As an example, if we call hvm_set_guest_time with the vCPU not running and change the offset, and then call it again on resume, but the offset has not changed this time the vcpu time will not be updated.

But in that case the call from context_switch() would take care of it, wouldn't it?

Jan

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

#11 lore raw
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · Roger Pau Monné
Quoted 3 lines · George Dunlap

You could gate calling update_vcpu_system_time on:
1. Whether stime_offset actually changed
2. Whether v is currently running

Quoted 8 lines
Quoted 5 lines

I'm not sure if only updating the vcpu time if the vCPU is running can lead to a slip in the vcpu time update. As an example, if we call hvm_set_guest_time with the vCPU not running and change the offset, and then call it again on resume, but the offset has not changed this time the vcpu time will not be updated.

But in that case the call from context_switch() would take care of it, wouldn't it?

Sure, I just got messed up with what we discussed before about gating the call to update_vcpu_system_time in context_switch for the HVM case. Will resend shortly.

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

#12 lore raw
In reply to Roger Pau Monne

Roger,

Quoted 11 lines · --On 4 June 2013 11:10:50 +0200 Roger Pau Monne <[email protected]> wrote:

When using a vtsc, hvm_set_guest_time changes hvm_vcpu.stime_offset, which is used in the vcpu time structure to calculate the tsc_timestamp, so after updating stime_offset we need to propagate the change to vcpu_time in order for the guest to get the right time if using the PV clock.

This was not done correctly, since in context_switch update_vcpu_system_time was called before vmx_do_resume, which caused the vcpu_info time structure to be updated with the wrong values. This patch fixes this by calling update_vcpu_system_time after the call to hvm_set_guest_time has happened.

I think this is the 'proper' fix to the bug Diana reported about stuck clock - can you confirm? If so, we're happy to test this on 4.3 and/or do try our hand at a backport to 4.2.

Signature · 1 line
Alex Bligh
#13 lore raw
Quoted 17 lines · Alex Bligh

Roger,

--On 4 June 2013 11:10:50 +0200 Roger Pau Monne <[email protected]>

Quoted 11 lines · wrote:

When using a vtsc, hvm_set_guest_time changes hvm_vcpu.stime_offset, which is used in the vcpu time structure to calculate the tsc_timestamp, so after updating stime_offset we need to propagate the change to vcpu_time in order for the guest to get the right time if using the PV clock.

This was not done correctly, since in context_switch update_vcpu_system_time was called before vmx_do_resume, which caused the vcpu_info time structure to be updated with the wrong values. This patch fixes this by calling update_vcpu_system_time after the call to hvm_set_guest_time has happened.

Quoted 3 lines

I think this is the 'proper' fix to the bug Diana reported about stuck clock - can you confirm? If so, we're happy to test this on 4.3 and/or do try our hand at a backport to 4.2.

Yes, and I was going to reply cc'ing you and Diana, but we're still having a technical discussion about where the best place is to put the fix. You may wish to wait until we have that resolved before testing the patch.

-George