|author||Marc Zyngier <email@example.com>||2020-02-07 16:34:10 +0000|
|committer||Paolo Bonzini <firstname.lastname@example.org>||2020-02-12 12:19:35 +0100|
|parent||KVM: x86: do not reset microcode version on INIT or RESET (diff)|
KVM: Disable preemption in kvm_get_running_vcpu()
Accessing a per-cpu variable only makes sense when preemption is disabled (and the kernel does check this when the right debug options are switched on). For kvm_get_running_vcpu(), it is fine to return the value after re-enabling preemption, as the preempt notifiers will make sure that this is kept consistent across task migration (the comment above the function hints at it, but lacks the crucial preemption management). While we're at it, move the comment from the ARM code, which explains why the whole thing works. Fixes: 7495e22bb165 ("KVM: Move running VCPU from ARM to common code"). Cc: Paolo Bonzini <email@example.com> Reported-by: Zenghui Yu <firstname.lastname@example.org> Tested-by: Zenghui Yu <email@example.com> Reviewed-by: Peter Xu <firstname.lastname@example.org> Signed-off-by: Marc Zyngier <email@example.com> Link: https://firstname.lastname@example.org Message-id: <email@example.com> Signed-off-by: Paolo Bonzini <firstname.lastname@example.org>
Diffstat (limited to 'virt/kvm/kvm_main.c')
1 files changed, 13 insertions, 3 deletions
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 67ae2d5c37b2..70f03ce0e5c1 100644
@@ -4409,12 +4409,22 @@ static void kvm_sched_out(struct preempt_notifier *pn,
* kvm_get_running_vcpu - get the vcpu running on the current CPU.
- * Thanks to preempt notifiers, this can also be called from
- * preemptible context.
+ * We can disable preemption locally around accessing the per-CPU variable,
+ * and use the resolved vcpu pointer after enabling preemption again,
+ * because even if the current thread is migrated to another CPU, reading
+ * the per-CPU value later will give us the same value as we update the
+ * per-CPU variable in the preempt notifier handlers.
struct kvm_vcpu *kvm_get_running_vcpu(void)
- return __this_cpu_read(kvm_running_vcpu);
+ struct kvm_vcpu *vcpu;
+ vcpu = __this_cpu_read(kvm_running_vcpu);
+ return vcpu;