KVM: Disable preemption in kvm_get_running_vcpu()
Marc Zyngier
maintainerPATCH 1/1 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.
--- virt/kvm/arm/vgic/vgic-mmio.c | 12 ------------ virt/kvm/kvm_main.c | 16 +++++++++++++--- 2 files changed, 13 insertions(+), 15 deletions(-)
virt/kvm/arm/vgic/vgic-mmio.c +0 -12
@@ -179,18 +179,6 @@ unsigned long vgic_mmio_read_pending(struct kvm_vcpu *vcpu,return value; } -/* -* This function will return the VCPU that performed the MMIO access and -* trapped from within the VM, and will return NULL if this is a userspace -* access. -* -* 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. -*/ -/* Must be called with irq->irq_lock held */ static void vgic_hw_irq_spending (struct kvm_vcpu *vcpu,struct vgic_irq *irq,bool is_uaccess)
virt/kvm/kvm_main.c +13 -3
@@ -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; + +preempt_disable (); + vcpu =__this_cpu_read (kvm_running_vcpu); +preempt_enable (); + +return vcpu; }/**
Signature · 1 line
2.20.1
Quoted 12 lines · Marc Zyngier
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.
Quoted 6 lines
Fixes 7495e22bb165 ("KVM: Move running VCPU from ARM to common code").Cc Peter Xu <[email protected]>Cc Paolo Bonzini <[email protected]>Reported-by Zenghui Yu <[email protected]>Signed-off-by Marc Zyngier <[email protected]>
Signature · 1 line
Peter Xu
Quoted 12 lines · Marc Zyngier
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.
Quoted 6 lines
Fixes 7495e22bb165 ("KVM: Move running VCPU from ARM to common code").Cc Peter Xu <[email protected]>Cc Paolo Bonzini <[email protected]>Reported-by Zenghui Yu <[email protected]>Signed-off-by Marc Zyngier <[email protected]>
on top of v5.6-rc1.
Thanks
Quoted 12 lines · Marc Zyngier
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.
Quoted 6 lines
Fixes 7495e22bb165 ("KVM: Move running VCPU from ARM to common code").Cc Peter Xu <[email protected]>Cc Paolo Bonzini <[email protected]>Reported-by Zenghui Yu <[email protected]>Signed-off-by Marc Zyngier <[email protected]>
Queued, thanks.
Paolo
Quoted 59 lines
--- virt/kvm/arm/vgic/vgic-mmio.c | 12 ------------ virt/kvm/kvm_main.c | 16 +++++++++++++--- 2 files changed, 13 insertions(+), 15 deletions(-)virt/kvm/arm/vgic/vgic-mmio.c +0
-12@@ -179,18 +179,6 @@ unsigned long vgic_mmio_read_pending(struct kvm_vcpu *vcpu,return value; } -/* -* This function will return the VCPU that performed the MMIO access and -* trapped from within the VM, and will return NULL if this is a userspace -* access. -* -* 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. -*/ -/* Must be called with irq->irq_lock held */ static void vgic_hw_irq_spending (struct kvm_vcpu *vcpu,struct vgic_irq *irq,bool is_uaccess)virt/kvm/kvm_main.c +13
-3@@ -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; + +preempt_disable (); + vcpu =__this_cpu_read (kvm_running_vcpu); +preempt_enable (); + +return vcpu; }/**