Skip to content

Commit 36ee41d

Browse files
committed
KVM: PPC: Book3S HV: Drop locks before reading guest memory
Running with CONFIG_DEBUG_ATOMIC_SLEEP reveals that HV KVM tries to read guest memory, in order to emulate guest instructions, while preempt is disabled and a vcore lock is held. This occurs in kvmppc_handle_exit_hv(), called from post_guest_process(), when emulating guest doorbell instructions on POWER9 systems, and also when checking whether we have hit a hypervisor breakpoint. Reading guest memory can cause a page fault and thus cause the task to sleep, so we need to avoid reading guest memory while holding a spinlock or when preempt is disabled. To fix this, we move the preempt_enable() in kvmppc_run_core() to before the loop that calls post_guest_process() for each vcore that has just run, and we drop and re-take the vcore lock around the calls to kvmppc_emulate_debug_inst() and kvmppc_emulate_doorbell_instr(). Dropping the lock is safe with respect to the iteration over the runnable vcpus in post_guest_process(); for_each_runnable_thread is actually safe to use locklessly. It is possible for a vcpu to become runnable and add itself to the runnable_threads array (code near the beginning of kvmppc_run_vcpu()) and then get included in the iteration in post_guest_process despite the fact that it has not just run. This is benign because vcpu->arch.trap and vcpu->arch.ceded will be zero. Cc: [email protected] # v4.13+ Fixes: 5790069 ("KVM: PPC: Book3S HV: Virtualize doorbell facility on POWER9") Signed-off-by: Paul Mackerras <[email protected]>
1 parent 9b9b13a commit 36ee41d

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

arch/powerpc/kvm/book3s_hv.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,8 +1008,6 @@ static int kvmppc_emulate_doorbell_instr(struct kvm_vcpu *vcpu)
10081008
struct kvm *kvm = vcpu->kvm;
10091009
struct kvm_vcpu *tvcpu;
10101010

1011-
if (!cpu_has_feature(CPU_FTR_ARCH_300))
1012-
return EMULATE_FAIL;
10131011
if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &inst) != EMULATE_DONE)
10141012
return RESUME_GUEST;
10151013
if (get_op(inst) != 31)
@@ -1059,6 +1057,7 @@ static int kvmppc_emulate_doorbell_instr(struct kvm_vcpu *vcpu)
10591057
return RESUME_GUEST;
10601058
}
10611059

1060+
/* Called with vcpu->arch.vcore->lock held */
10621061
static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
10631062
struct task_struct *tsk)
10641063
{
@@ -1179,7 +1178,10 @@ static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
11791178
swab32(vcpu->arch.emul_inst) :
11801179
vcpu->arch.emul_inst;
11811180
if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
1181+
/* Need vcore unlocked to call kvmppc_get_last_inst */
1182+
spin_unlock(&vcpu->arch.vcore->lock);
11821183
r = kvmppc_emulate_debug_inst(run, vcpu);
1184+
spin_lock(&vcpu->arch.vcore->lock);
11831185
} else {
11841186
kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
11851187
r = RESUME_GUEST;
@@ -1194,8 +1196,13 @@ static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
11941196
*/
11951197
case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
11961198
r = EMULATE_FAIL;
1197-
if ((vcpu->arch.hfscr >> 56) == FSCR_MSGP_LG)
1199+
if (((vcpu->arch.hfscr >> 56) == FSCR_MSGP_LG) &&
1200+
cpu_has_feature(CPU_FTR_ARCH_300)) {
1201+
/* Need vcore unlocked to call kvmppc_get_last_inst */
1202+
spin_unlock(&vcpu->arch.vcore->lock);
11981203
r = kvmppc_emulate_doorbell_instr(vcpu);
1204+
spin_lock(&vcpu->arch.vcore->lock);
1205+
}
11991206
if (r == EMULATE_FAIL) {
12001207
kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
12011208
r = RESUME_GUEST;
@@ -2946,13 +2953,14 @@ static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
29462953
/* make sure updates to secondary vcpu structs are visible now */
29472954
smp_mb();
29482955

2956+
preempt_enable();
2957+
29492958
for (sub = 0; sub < core_info.n_subcores; ++sub) {
29502959
pvc = core_info.vc[sub];
29512960
post_guest_process(pvc, pvc == vc);
29522961
}
29532962

29542963
spin_lock(&vc->lock);
2955-
preempt_enable();
29562964

29572965
out:
29582966
vc->vcore_state = VCORE_INACTIVE;

0 commit comments

Comments
 (0)