Fix doorbell emulation for nested KVM guests in V1 API
Subsystems (3)
cover Fix doorbell emulation for nested KVM guests in V1 API
Doorbell emulation for nested KVM guests in V1 API is broken because of 2 reasons:
1. L0 presenting H_EMUL_ASSIST to L1 instead of H_FAC_UNAVAIL
2. Broken plumbing for passing around doorbell state.
Fix the trap passed to L1 and the plumbing for maintaining doorbell state.
Gautam Menghani (3):
Revert "KVM: PPC: Book3S HV Nested: Stop forwarding all HFUs to L1"
KVM: PPC: Book3S HV: Stop using vc->dpdes for nested KVM guests
KVM: PPC: Book3S HV: Avoid returning to nested hypervisor on pending
doorbells
arch/powerpc/kvm/book3s_hv.c | 41 ++++++++--------------------- arch/powerpc/kvm/book3s_hv_nested.c | 14 +++++++--- 2 files changed, 21 insertions(+), 34 deletions(-)
Signature · 1 line
2.45.2
PATCH 1/3 Revert "KVM: PPC: Book3S HV Nested: Stop forwarding all HFUs to L1"
This reverts commit 7c3ded5735141ff4d049747c9f76672a8b737c49.
On PowerNV, when a nested guest tries to use a feature prohibited by HFSCR, the nested hypervisor (L1) should get a H_FAC_UNAVAILABLE trap so that L1 can emulate the feature. But with the change introduced by commit 7c3ded573514 ("KVM: PPC: Book3S HV Nested: Stop forwarding all HFUs to L1") the L1 ends up getting a H_EMUL_ASSIST because of which, the L1 ends up injecting a SIGILL when L2 (nested guest) tries to use doorbells.
--- arch/powerpc/kvm/book3s_hv.c | 31 ++----------------------------- 1 file changed, 2 insertions(+), 29 deletions(-)
arch/powerpc/kvm/book3s_hv.c +2 -29
@@ -2060,36 +2060,9 @@ static int kvmppc_handle_nested_exit(struct kvm_vcpu *vcpu) fallthrough;/* go to facility unavailable handler */ #endif -case BOOK3S_INTERRUPT_H_FAC_UNAVAIL : { -u64 cause = vcpu->arch.hfscr >>56 ; - -/* -* Only pass HFU interrupts to the L1 if the facility is -* permitted but disabled by the L1's HFSCR, otherwise -* the interrupt does not make sense to the L1 so turn -* it into a HEAI. -*/ -if (!(vcpu->arch.hfscr_permitted & (1UL << cause)) || - (vcpu->arch.nested_hfscr & (1UL << cause))) { -ppc_inst_t pinst; - vcpu->arch.trap =BOOK3S_INTERRUPT_H_EMUL_ASSIST ; - -/* -* If the fetch failed, return to guest and -* try executing it again. -*/ - r =kvmppc_get_last_inst (vcpu,INST_GENERIC , &pinst); - vcpu->arch.emul_inst =ppc_inst_val (pinst); -if (r !=EMULATE_DONE ) - r =RESUME_GUEST ; -else - r =RESUME_HOST ; - }else { - r =RESUME_HOST ; - } - +case BOOK3S_INTERRUPT_H_FAC_UNAVAIL : + r =RESUME_HOST ;break ; - }case BOOK3S_INTERRUPT_HV_RM_HARD : vcpu->arch.trap =0 ;
Signature · 1 line
2.45.2
PATCH 2/3 KVM: PPC: Book3S HV: Stop using vc->dpdes for nested KVM guests
In reply to Gautam Menghanicommit 6398326b9ba1 ("KVM: PPC: Book3S HV P9: Stop using vc->dpdes") introduced an optimization to use only vcpu->doorbell_request for SMT emulation for Power9 and above guests, but the code for nested guests still relies on the old way of handling doorbells, due to which an L2 guest (see [1]) cannot be booted with XICS with SMT>1. The command to repro this issue is:
// To be run in L1
qemu-system-ppc64 \ -drive file=rhel.qcow2,format=qcow2 \ -m 20G \ -smp 8,cores=1,threads=8 \ -cpu host \ -nographic \ -machine pseries,ic-mode=xics -accel kvm
Fix the plumbing to utilize vcpu->doorbell_request instead of vcore->dpdes for nested KVM guests on P9 and above.
[1] Terminology
1. L0 : PowerNV linux running with HV privileges
2. L1 : Pseries KVM guest running on top of L0
2. L2 : Nested KVM guest running on top of L1
--- arch/powerpc/kvm/book3s_hv.c | 9 +++++++++ arch/powerpc/kvm/book3s_hv_nested.c | 14 ++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-)
arch/powerpc/kvm/book3s_hv.c +9 -0
@@ -4282,6 +4282,15 @@ static int kvmhv_vcpu_entry_p9_nested(struct kvm_vcpu *vcpu, u64 time_limit, uns } hvregs.hdec_expiry = time_limit; +/* +* hvregs has the doorbell status, so zero it here which +* enables us to receive doorbells when H_ENTER_NESTED is +* in progress for this vCPU +*/ + +if (vcpu->arch.doorbell_request) + vcpu->arch.doorbell_request =0 ; +/* * When setting DEC, we must always deal with irq_work_raise * via NMI vs setting DEC. The problem occurs right as we
arch/powerpc/kvm/book3s_hv_nested.c +10 -4
@@ -32,7 +32,7 @@ void kvmhv_save_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr)struct kvmppc_vcore *vc = vcpu->arch.vcore; hr->pcr = vc->pcr |PCR_MASK ; - hr->dpdes = vc->dpdes; + hr->dpdes = vcpu->arch.doorbell_request; hr->hfscr = vcpu->arch.hfscr; hr->tb_offset = vc->tb_offset; hr->dawr0 = vcpu->arch.dawr0;
@@ -105,7 +105,7 @@ static void save_hv_return_state(struct kvm_vcpu *vcpu, {struct kvmppc_vcore *vc = vcpu->arch.vcore; - hr->dpdes = vc->dpdes; + hr->dpdes = vcpu->arch.doorbell_request; hr->purr = vcpu->arch.purr; hr->spurr = vcpu->arch.spurr; hr->ic = vcpu->arch.ic;
@@ -143,7 +143,7 @@ static void restore_hv_regs(struct kvm_vcpu *vcpu, const struct hv_guest_state *struct kvmppc_vcore *vc = vcpu->arch.vcore; vc->pcr = hr->pcr |PCR_MASK ; - vc->dpdes = hr->dpdes; + vcpu->arch.doorbell_request = hr->dpdes; vcpu->arch.hfscr = hr->hfscr; vcpu->arch.dawr0 = hr->dawr0; vcpu->arch.dawrx0 = hr->dawrx0;
@@ -170,7 +170,13 @@ void kvmhv_restore_hv_return_state(struct kvm_vcpu *vcpu, {struct kvmppc_vcore *vc = vcpu->arch.vcore; - vc->dpdes = hr->dpdes; +/* +* This L2 vCPU might have received a doorbell while H_ENTER_NESTED was being handled. +* Make sure we preserve the doorbell if it was either: +* a) Sent after H_ENTER_NESTED was called on this vCPU (arch.doorbell_request would be 1) +* b) Doorbell was not handled and L2 exited for some other reason (hr->dpdes would be 1) +*/ + vcpu->arch.doorbell_request = vcpu->arch.doorbell_request | hr->dpdes; vcpu->arch.hfscr = hr->hfscr; vcpu->arch.purr = hr->purr; vcpu->arch.spurr = hr->spurr;
Signature · 1 line
2.45.2
PATCH 3/3 KVM: PPC: Book3S HV: Avoid returning to nested hypervisor on pending doorbells
In reply to Gautam MenghaniCommit 6398326b9ba1 ("KVM: PPC: Book3S HV P9: Stop using vc->dpdes") dropped the use of vcore->dpdes for msgsndp / SMT emulation. Prior to that commit, the below code at L1 level (see [1] for terminology) was responsible for setting vc->dpdes for the respective L2 vCPU:
if (!nested) {
kvmppc_core_prepare_to_enter(vcpu);
if (vcpu->arch.doorbell_request) {
vc->dpdes = 1;
smp_wmb();
vcpu->arch.doorbell_request = 0;
}L1 then sent vc->dpdes to L0 via kvmhv_save_hv_regs(), and while servicing H_ENTER_NESTED at L0, the below condition at L0 level made sure to abort and go back to L1 if vcpu->arch.doorbell_request = 1 so that L1 sets vc->dpdes as per above if condition:
} else if (vcpu->arch.pending_exceptions ||
vcpu->arch.doorbell_request ||
xive_interrupt_pending(vcpu)) {
vcpu->arch.ret = RESUME_HOST;
goto out;
}This worked fine since vcpu->arch.doorbell_request was used more like a flag and vc->dpdes was used to pass around the doorbell state. But after Commit 6398326b9ba1 ("KVM: PPC: Book3S HV P9: Stop using vc->dpdes"), vcpu->arch.doorbell_request is the only variable used to pass around doorbell state.
With the plumbing for handling doorbells for nested guests updated to use vcpu->arch.doorbell_request over vc->dpdes, the above "else if" stops doorbells from working correctly as L0 aborts execution of L2 and instead goes back to L1.
Remove vcpu->arch.doorbell_request from the above "else if" condition as it is no longer needed for L0 to correctly handle the doorbell status while running L2.
[1] Terminology
1. L0 : PowerNV linux running with HV privileges
2. L1 : Pseries KVM guest running on top of L0
2. L2 : Nested KVM guest running on top of L1
--- arch/powerpc/kvm/book3s_hv.c | 1 - 1 file changed, 1 deletion(-)
arch/powerpc/kvm/book3s_hv.c +0 -1
@@ -4894,7 +4894,6 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit, lpcr &= ~LPCR_MER ; } }else if (vcpu->arch.pending_exceptions || - vcpu->arch.doorbell_request ||xive_interrupt_pending (vcpu)) { vcpu->arch.ret =RESUME_HOST ; goto out;
Signature · 1 line
2.45.2
Michael Ellerman
AppliedQuoted 9 lines · Gautam Menghani
Doorbell emulation for nested KVM guests in V1 API is broken because of 2 reasons:
1. L0 presentingH_EMUL_ASSISTto L1 instead ofH_FAC_UNAVAIL
2. Broken plumbing for passing around doorbell state.Fix the trap passed to L1 and the plumbing for maintaining doorbell state.
[...]
Applied to powerpc/topic/ppc-kvm.
[1/3] Revert "KVM: PPC: Book3S HV Nested: Stop forwarding all HFUs to L1"
https://git.kernel.org/powerpc/c/ed351c57432122c4499be4f4aee8711d6fa93f3b [2/3] KVM: PPC: Book3S HV: Stop using vc->dpdes for nested KVM guests https://git.kernel.org/powerpc/c/0d3c6b28896f9889c8864dab469e0343a0ad1c0c [3/3] KVM: PPC: Book3S HV: Avoid returning to nested hypervisor on pending doorbells https://git.kernel.org/powerpc/c/26686db69917399fa30e3b3135360771e90f83ec
cheers