KVM: x86: hyper-v: Don't auto-enable stimer during deserialization
Subsystems (4)
PATCH 1/1 KVM: x86: hyper-v: Don't auto-enable stimer during deserialization
By not honoring the 'stimer->config.enable' state during stimer deserialization we might introduce spurious timer interrupts. For example through the following events:
- The stimer is configured in auto-enable mode.
- The stimer's count is set and the timer enabled.
- The stimer expires, an interrupt is injected.
- We live migrate the VM.
- The stimer config and count are deserialized, auto-enable is ON, the stimer is re-enabled.
- The stimer expires right away, and injects an unwarranted interrupt.
So let's not change the stimer's enable state if the MSR write comes from user-space.
--- arch/x86/kvm/hyperv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/hyperv.c +1 -1
@@ -729,7 +729,7 @@ static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count, stimer->count = count;if (stimer->count ==0 ) stimer->config.enable =0 ; -else if (stimer->config.auto_enable) +else if (stimer->config.auto_enable && !host) stimer->config.enable =1 ;if (stimer->config.enable)
Signature · 1 line
2.40.1
Vitaly Kuznetsov
maintainerQuoted 25 lines · Nicolas Saenz Julienne
By not honoring the '
stimer->config.enable' state during stimer deserialization we might introduce spurious timer interrupts. For example through the following events:
- The stimer is configured in auto-enable mode.
- The stimer's count is set and the timer enabled.
- The stimer expires, an interrupt is injected.
- We live migrate the VM.
- The stimer config and count are deserialized, auto-enable is ON, the stimer is re-enabled.
- The stimer expires right away, and injects an unwarranted interrupt.So let's not change the stimer's enable state if the MSR write comes from user-space.
Fixes 1f4b34f825e8 ("kvm/x86: Hyper-V SynIC timers")Signed-off-by Nicolas Saenz Julienne <[email protected]>--- arch/x86/kvm/hyperv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/hyperv.c +0
-0
arch/x86/kvm/hyperv.c static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count, · line 731
Quoted 3 lines
arch/x86/kvm/hyperv.c +0
-0@@ -729,7 +729,7 @@ static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count, stimer->count = count;if (stimer->count ==0 ) stimer->config.enable =0 ;
Can this branch be problematic too? E.g. if STIMER[X]_CONFIG is deserialized after STIMER[X]_COUNT we may erroneously reset 'enable' to 0, right? In fact, when MSRs are ordered like this:
#define HV_X64_MSR_STIMER0_CONFIG 0x400000B0
#define HV_X64_MSR_STIMER0_COUNT 0x400000B1
I would guess that we always de-serialize 'config' first. With auto-enable, the timer will get enabled when writing 'count' but what happens in other cases?
Maybe the whole block needs to go under 'if (!host)' instead?
Quoted 5 lines
arch/x86/kvm/hyperv.c +1
-1-else if (stimer->config.auto_enable) +else if (stimer->config.auto_enable && !host) stimer->config.enable =1 ;if (stimer->config.enable)
Signature · 1 line
Vitaly
Hi Vitaly,
Quoted 35 lines · Vitaly Kuznetsov
Quoted 25 lines · Nicolas Saenz Julienne
By not honoring the '
stimer->config.enable' state during stimer deserialization we might introduce spurious timer interrupts. For example through the following events:
- The stimer is configured in auto-enable mode.
- The stimer's count is set and the timer enabled.
- The stimer expires, an interrupt is injected.
- We live migrate the VM.
- The stimer config and count are deserialized, auto-enable is ON, the stimer is re-enabled.
- The stimer expires right away, and injects an unwarranted interrupt.So let's not change the stimer's enable state if the MSR write comes from user-space.
Fixes 1f4b34f825e8 ("kvm/x86: Hyper-V SynIC timers")Signed-off-by Nicolas Saenz Julienne <[email protected]>--- arch/x86/kvm/hyperv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/hyperv.c +0
-0arch/x86/kvm/hyperv.c static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count, · line 731
Quoted 3 lines
arch/x86/kvm/hyperv.c +0
-0@@ -729,7 +729,7 @@ static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count, stimer->count = count;if (stimer->count ==0 ) stimer->config.enable =0 ;Can this branch be problematic too? E.g. if STIMER[X]_CONFIG is deserialized after STIMER[X]_COUNT we may erroneously reset 'enable' to 0, right? In fact, when MSRs are ordered like this:
Quoted 8 lines
#define
HV_X64_MSR_STIMER0_CONFIG0x400000B0
#defineHV_X64_MSR_STIMER0_COUNT0x400000B1I would guess that we always de-serialize 'config' first. With auto-enable, the timer will get enabled when writing 'count' but what happens in other cases?
Maybe the whole block needs to go under 'if (!host)' instead?
In either case, with 'enable == 1' && 'count == 0' we'll reset the timer in 'kvm_hv_process_stimers()'. So it's unlikely to cause any weirdness. That said, I think covering both cases is more correct. Will send a v2.
Nicolas
Sean Christopherson
maintainerI'd prefer the shortlog be more explicit about the write coming from userspace, e.g.
KVM: x86: hyper-v: Don't auto-enable stimer on write from userspace
A non-zero number of KVM's "deserialization" ioctls are used to stuff state without a paired "serialization". I doubt anyone is doing that with the Hyper-V ioctls, but keeping things consistent is helpful for readers.
Quoted 7 lines · Nicolas Saenz Julienne
Hi Vitaly,
Quoted 4 lines · Vitaly Kuznetsov
Quoted 2 lines · Nicolas Saenz Julienne
By not honoring the '
stimer->config.enable' state during stimer deserialization we might introduce spurious timer interrupts. For
Avoid pronouns please.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
example through the following events:
- The stimer is configured in auto-enable mode.
- The stimer's count is set and the timer enabled.
- The stimer expires, an interrupt is injected.
- We live migrate the VM.
Same here. "We" is already ambiguous, because the first usage is largely about KVM, and the second usage here is much more about userspace and/or the actual user.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
- The stimer config and count are deserialized, auto-enable is ON, the stimer is re-enabled.
- The stimer expires right away, and injects an unwarranted interrupt.So let's not change the stimer's enable state if the MSR write comes from user-space.
Don't hedge, firmly state what the patch does and why the change is necessary and correct. If it turns out the change is wrong, then the follow-up patch can explain the situation. But in the happy case where the change is correct, using language that isn't assertive can result in
Quoted 1 line
Quoted 1 line
Quoted 1 line
Fixes 1f4b34f825e8 ("kvm/x86: Hyper-V SynIC timers")
Does this need a?
Quoted 25 lines
Quoted 25 lines
arch/x86/kvm/hyperv.c static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count, · line 731
Quoted 13 lines
Signed-off-by Nicolas Saenz Julienne <[email protected]>--- arch/x86/kvm/hyperv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/hyperv.c +0
-0@@ -729,7 +729,7 @@ static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count, stimer->count = count;if (stimer->count ==0 ) stimer->config.enable =0 ;Can this branch be problematic too? E.g. if STIMER[X]_CONFIG is deserialized after STIMER[X]_COUNT we may erroneously reset 'enable' to 0, right? In fact, when MSRs are ordered like this:
#define
HV_X64_MSR_STIMER0_CONFIG0x400000B0
#defineHV_X64_MSR_STIMER0_COUNT0x400000B1I would guess that we always de-serialize 'config' first. With auto-enable, the timer will get enabled when writing 'count' but what happens in other cases?
Quoted 5 lines
Quoted 1 line
Maybe the whole block needs to go under 'if (!host)' instead?
In either case, with 'enable == 1' && 'count == 0' we'll reset the timer in '
kvm_hv_process_stimers()'. So it's unlikely to cause any weirdness. That said, I think covering both cases is more correct. Will send a v2.
Agreed, I think it needs to be all or nothing, i.e. either process all side effects of writing the count, or don't process any.
Hi Sean,
Quoted 41 lines · Sean Christopherson
I'd prefer the shortlog be more explicit about the write coming from userspace, e.g.
KVM: x86: hyper-v: Don't auto-enable stimer on write from userspace
A non-zero number of KVM's "deserialization" ioctls are used to stuff state without a paired "serialization". I doubt anyone is doing that with the Hyper-V ioctls, but keeping things consistent is helpful for readers.
Quoted 7 lines · Nicolas Saenz Julienne
Hi Vitaly,
Quoted 4 lines · Vitaly Kuznetsov
Quoted 2 lines · Nicolas Saenz Julienne
By not honoring the '
stimer->config.enable' state during stimer deserialization we might introduce spurious timer interrupts. ForAvoid pronouns please.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
example through the following events:
- The stimer is configured in auto-enable mode.
- The stimer's count is set and the timer enabled.
- The stimer expires, an interrupt is injected.
- We live migrate the VM.Same here. "We" is already ambiguous, because the first usage is largely about KVM, and the second usage here is much more about userspace and/or the actual user.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
- The stimer config and count are deserialized, auto-enable is ON, the stimer is re-enabled.
- The stimer expires right away, and injects an unwarranted interrupt.So let's not change the stimer's enable state if the MSR write comes from user-space.
Don't hedge, firmly state what the patch does and why the change is necessary and correct. If it turns out the change is wrong, then the follow-up patch can explain the situation. But in the happy case where the change is correct, using language that isn't assertive can result in
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Fixes 1f4b34f825e8 ("kvm/x86: Hyper-V SynIC timers")Does this need a?
Your reply raced with my v2. I'll rework the commit message, and send a third revision.
Nicolas