KVM: MIPS: Precalculate MMIO load resume PC
Subsystems (3)
PATCH 23/319 KVM: MIPS: Precalculate MMIO load resume PC
From: James Hogan <[email protected]>
commit e1e575f6b026734be3b1f075e780e91ab08ca541 upstream.
The advancing of the PC when completing an MMIO load is done before re-entering the guest, i.e. before restoring the guest ASID. However if the load is in a branch delay slot it may need to access guest code to read the prior branch instruction. This isn't safe in TLB mapped code at the moment, nor in the future when we'll access unmapped guest segments using direct user accessors too, as it could read the branch from host user memory instead.
Therefore calculate the resume PC in advance while we're still in the right context and save it in the new vcpu->arch.io_pc (replacing the no longer needed vcpu->arch.pending_load_cause), and restore it on MMIO completion.
--- arch/mips/include/asm/kvm_host.h | 7 ++++--- arch/mips/kvm/kvm_mips_emul.c | 25 +++++++++++++++---------- 2 files changed, 19 insertions(+), 13 deletions(-)
arch/mips/include/asm/kvm_host.h +4 -3
@@ -375,7 +375,10 @@ struct kvm_vcpu_arch {/* Host KSEG0 address of the EI/DI offset */ void *kseg0_commpage; -u32 io_gpr;/* GPR used as IO source/target */ +/* Resume PC after MMIO completion */ +unsigned long io_pc; +/* GPR used as IO source/target */ +u32 io_gpr;/* Used to calibrate the virutal count register for the guest */ int32_t host_cp0_count;
@@ -386,8 +389,6 @@ struct kvm_vcpu_arch {/* Bitmask of pending exceptions to be cleared */ unsigned long pending_exceptions_clr; -unsigned long pending_load_cause; -/* Save/Restore the entryhi register when are are preempted/scheduled back in */ unsigned long preempt_entryhi;
arch/mips/kvm/kvm_mips_emul.c +15 -10
@@ -773,6 +773,7 @@ kvm_mips_emulate_load(uint32_t inst, uint32_t cause, struct kvm_run *run, struct kvm_vcpu *vcpu) { enum emulation_result er = EMULATE_DO_MMIO; +unsigned long curr_pc;int32_t op, base, rt, offset;uint32_t bytes;
@@ -781,7 +782,18 @@ kvm_mips_emulate_load(uint32_t inst, uint32_t cause, offset = inst &0xffff ; op = (inst >>26 ) &0x3f ; - vcpu->arch.pending_load_cause = cause; +/* +* Find the resume PC now while we have safe and easy access to the +* prior branch instruction, and save it for +* kvm_mips_complete_mmio_load() to restore later. +*/ + curr_pc = vcpu->arch.pc; + er =update_pc (vcpu, cause); +if (er ==EMULATE_FAIL ) +return er; + vcpu->arch.io_pc = vcpu->arch.pc; + vcpu->arch.pc = curr_pc; + vcpu->arch.io_gpr = rt;switch (op) {
@@ -1617,9 +1629,8 @@ kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run) goto done; } - er =update_pc (vcpu, vcpu->arch.pending_load_cause); -if (er ==EMULATE_FAIL ) -return er; +/* Restore saved resume PC */ + vcpu->arch.pc = vcpu->arch.io_pc;switch (run->mmio.len) {case 4 :
@@ -1641,12 +1652,6 @@ kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run)break ; } -if (vcpu->arch.pending_load_cause &CAUSEF_BD ) -kvm_debug - ("[%#lx] Completing %d byte BD Load to gpr %d (0x%08lx) type %d\n" , - vcpu->arch.pc, run->mmio.len, vcpu->arch.io_gpr, *gpr, - vcpu->mmio_needed); - done:return er; }
Signature · 1 line
2.8.0.rc2.1.gbe9624a
PATCH 24/319 KVM: MIPS: Drop other CPU ASIDs on guest MMU changes
From: James Hogan <[email protected]>
commit 91e4f1b6073dd680d86cdb7e42d7cccca9db39d8 upstream.
When a guest TLB entry is replaced by TLBWI or TLBWR, we only invalidate TLB entries on the local CPU. This doesn't work correctly on an SMP host when the guest is migrated to a different physical CPU, as it could pick up stale TLB mappings from the last time the vCPU ran on that physical CPU.
Therefore invalidate both user and kernel host ASIDs on other CPUs, which will cause new ASIDs to be generated when it next runs on those CPUs.
We're careful only to do this if the TLB entry was already valid, and only for the kernel ASID where the virtual address it mapped is outside of the guest user address range.
--- arch/mips/kvm/kvm_mips_emul.c | 61 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 8 deletions(-)
arch/mips/kvm/kvm_mips_emul.c +53 -8
@@ -310,6 +310,47 @@ enum emulation_result kvm_mips_emul_tlbr(struct kvm_vcpu *vcpu)return er; } +/** +* kvm_mips_invalidate_guest_tlb() - Indicates a change in guest MMU map. +* @vcpu: VCPU with changed mappings. +* @tlb: TLB entry being removed. +* +* This is called to indicate a single change in guest MMU mappings, so that we +* can arrange TLB flushes on this and other CPUs. +*/ +static void kvm_mips_invalidate_guest_tlb (struct kvm_vcpu *vcpu, +struct kvm_mips_tlb *tlb) +{ +int cpu, i; +bool user; + +/* No need to flush for entries which are already invalid */ +if (!((tlb->tlb_lo0 | tlb->tlb_lo1) &MIPS3_PG_V )) +return ; +/* User address space doesn't need flushing for KSeg2/3 changes */ + user = tlb->tlb_hi <KVM_GUEST_KSEG0 ; + +preempt_disable (); + +/* +* Probe the shadow host TLB for the entry being overwritten, if one +* matches, invalidate it +*/ +kvm_mips_host_tlb_inv (vcpu, tlb->tlb_hi); + +/* Invalidate the whole ASID on other CPUs */ + cpu =smp_processor_id (); +for_each_possible_cpu (i) { +if (i == cpu) +continue ; +if (user) + vcpu->arch.guest_user_asid[i] =0 ; + vcpu->arch.guest_kernel_asid[i] =0 ; + } + +preempt_enable (); +} +/* Write Guest TLB Entry @ Index */ enum emulation_result kvm_mips_emul_tlbwi (struct kvm_vcpu *vcpu) {
@@ -331,10 +372,8 @@ enum emulation_result kvm_mips_emul_tlbwi(struct kvm_vcpu *vcpu) } tlb = &vcpu->arch.guest_tlb[index]; -#if 1 -/* Probe the shadow host TLB for the entry being overwritten, if one matches, invalidate it */ -kvm_mips_host_tlb_inv (vcpu, tlb->tlb_hi); -#endif + +kvm_mips_invalidate_guest_tlb (vcpu, tlb); tlb->tlb_mask =kvm_read_c0_guest_pagemask (cop0); tlb->tlb_hi =kvm_read_c0_guest_entryhi (cop0);
@@ -373,10 +412,7 @@ enum emulation_result kvm_mips_emul_tlbwr(struct kvm_vcpu *vcpu) tlb = &vcpu->arch.guest_tlb[index]; -#if 1 -/* Probe the shadow host TLB for the entry being overwritten, if one matches, invalidate it */ -kvm_mips_host_tlb_inv (vcpu, tlb->tlb_hi); -#endif +kvm_mips_invalidate_guest_tlb (vcpu, tlb); tlb->tlb_mask =kvm_read_c0_guest_pagemask (cop0); tlb->tlb_hi =kvm_read_c0_guest_entryhi (cop0);
@@ -419,6 +455,7 @@ kvm_mips_emulate_CP0(uint32_t inst, uint32_t *opc, uint32_t cause,int32_t rt, rd, copz, sel, co_bit, op;uint32_t pc = vcpu->arch.pc;unsigned long curr_pc; +int cpu, i;/* * Update PC and hold onto current PC in case there is
@@ -538,8 +575,16 @@ kvm_mips_emulate_CP0(uint32_t inst, uint32_t *opc, uint32_t cause,ASID_MASK , vcpu->arch.gprs[rt] & ASID_MASK); +preempt_disable ();/* Blow away the shadow host TLBs */ kvm_mips_flush_host_tlb (1 ); + cpu =smp_processor_id (); +for_each_possible_cpu (i) +if (i != cpu) { + vcpu->arch.guest_user_asid[i] =0 ; + vcpu->arch.guest_kernel_asid[i] =0 ; + } +preempt_enable (); }kvm_write_c0_guest_entryhi (cop0, vcpu->arch.gprs[rt]);
Signature · 1 line
2.8.0.rc2.1.gbe9624a
PATCH 25/319 KVM: nVMX: postpone VMCS changes on MSR_IA32_APICBASE write
In reply to Willy TarreauFrom: Radim Kr�mář <[email protected]>
commit dccbfcf52cebb8963246eba5b177b77f26b34da0 upstream.
If vmcs12 does not intercept APIC_BASE writes, then KVM will handle the write with vmcs02 as the current VMCS.
This will incorrectly apply modifications intended for vmcs01 to vmcs02 and L2 can use it to gain access to L0's x2APIC registers by disabling virtualized x2APIC while using msr bitmap that assumes enabled.
Postpone execution of vmx_set_virtual_x2apic_mode until vmcs01 is the current VMCS. An alternative solution would temporarily make vmcs01 the current VMCS, but it requires more care.
--- arch/x86/kvm/vmx.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
arch/x86/kvm/vmx.c +13 -0
@@ -366,6 +366,7 @@ struct nested_vmx {struct list_head vmcs02_pool;int vmcs02_num;u64 vmcs01_tsc_offset; +bool change_vmcs01_virtual_x2apic_mode;/* L2 must run next, and mustn't decide to exit to L1. */ bool nested_run_pending;/*
@@ -6702,6 +6703,12 @@ static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set) {u32 sec_exec_control; +/* Postpone execution until vmcs01 is the current VMCS. */ +if (is_guest_mode (vcpu)) { +to_vmx (vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true; +return ; + } +/* * There is not point to enable virtualize x2apic without enable * apicv
@@ -8085,6 +8092,12 @@ static void nested_vmx_vmexit(struct kvm_vcpu *vcpu)/* Update TSC_OFFSET if TSC was changed while L2 ran */ vmcs_write64 (TSC_OFFSET , vmx->nested.vmcs01_tsc_offset); +if (vmx->nested.change_vmcs01_virtual_x2apic_mode) { + vmx->nested.change_vmcs01_virtual_x2apic_mode = false; +vmx_set_virtual_x2apic_mode (vcpu, + vcpu->arch.apic_base &X2APIC_ENABLE ); + } +/* This is needed for same reason as it was needed in prepare_vmcs02 */ vmx->host_rsp =0 ;
Signature · 1 line
2.8.0.rc2.1.gbe9624a
PATCH 26/319 KVM: MIPS: Make ERET handle ERL before EXL
In reply to Willy TarreauFrom: James Hogan <[email protected]>
commit ede5f3e7b54a4347be4d8525269eae50902bd7cd upstream.
The ERET instruction to return from exception is used for returning from exception level (Status.EXL) and error level (Status.ERL). If both bits are set however we should be returning from ERL first, as ERL can interrupt EXL, for example when an NMI is taken. KVM however checks EXL first.
Fix the order of the checks to match the pseudocode in the instruction set manual.
--- arch/mips/kvm/kvm_mips_emul.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/mips/kvm/kvm_mips_emul.c +4 -4
@@ -254,15 +254,15 @@ enum emulation_result kvm_mips_emul_eret(struct kvm_vcpu *vcpu)struct mips_coproc *cop0 = vcpu->arch.cop0;enum emulation_result er =EMULATE_DONE ; -if (kvm_read_c0_guest_status (cop0) &ST0_EXL ) { +if (kvm_read_c0_guest_status (cop0) &ST0_ERL ) { +kvm_clear_c0_guest_status (cop0,ST0_ERL ); + vcpu->arch.pc =kvm_read_c0_guest_errorepc (cop0); + }else if (kvm_read_c0_guest_status (cop0) &ST0_EXL ) {kvm_debug ("[%#lx] ERET to %#lx\n" , vcpu->arch.pc,kvm_read_c0_guest_epc (cop0));kvm_clear_c0_guest_status (cop0,ST0_EXL ); vcpu->arch.pc =kvm_read_c0_guest_epc (cop0); - }else if (kvm_read_c0_guest_status (cop0) &ST0_ERL ) { -kvm_clear_c0_guest_status (cop0,ST0_ERL ); - vcpu->arch.pc =kvm_read_c0_guest_errorepc (cop0); }else {printk ("[%#lx] ERET when MIPS_SR_EXL|MIPS_SR_ERL == 0\n" , vcpu->arch.pc);
Signature · 1 line
2.8.0.rc2.1.gbe9624a
PATCH 27/319 KVM: x86: fix wbinvd_dirty_mask use-after-free
In reply to Willy TarreauFrom: Ido Yariv <[email protected]>
commit bd768e146624cbec7122ed15dead8daa137d909d upstream.
vcpu->arch.wbinvd_dirty_mask may still be used after freeing it, corrupting memory. For example, the following call trace may set a bit in an already freed cpu mask:
kvm_arch_vcpu_load
vcpu_load
vmx_free_vcpu_nested
vmx_free_vcpu
kvm_arch_vcpu_free
Fix this by deferring freeing of wbinvd_dirty_mask.
--- arch/x86/kvm/x86.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +3 -1
@@ -6509,11 +6509,13 @@ void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)void kvm_arch_vcpu_free (struct kvm_vcpu *vcpu) { +void *wbinvd_dirty_mask = vcpu->arch.wbinvd_dirty_mask; +kvmclock_reset (vcpu); -free_cpumask_var (vcpu->arch.wbinvd_dirty_mask);fx_free (vcpu); kvm_x86_ops->vcpu_free (vcpu); +free_cpumask_var (wbinvd_dirty_mask); } struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
Signature · 1 line
2.8.0.rc2.1.gbe9624a
PATCH 28/319 KVM: x86: fix missed SRCU usage in kvm_lapic_set_vapic_addr
In reply to Willy TarreauFrom: Paolo Bonzini <[email protected]>
commit 7301d6abaea926d685832f7e1f0c37dd206b01f4 upstream.
Reported by syzkaller:
[ INFO: suspicious RCU usage. ]
4.9.0-rc4+ #47 Not tainted
-------------------------------
./include/linux/kvm_host.h:536 suspicious rcu_dereference_check() usage!
stack backtrace:
CPU: 1 PID: 6679 Comm: syz-executor Not tainted 4.9.0-rc4+ #47
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
ffff880039e2f6d0 ffffffff81c2e46b ffff88003e3a5b40 0000000000000000
0000000000000001 ffffffff83215600 ffff880039e2f700 ffffffff81334ea9
ffffc9000730b000 0000000000000004 ffff88003c4f8420 ffff88003d3f8000
Call Trace:
[< inline >] __dump_stack lib/dump_stack.c:15
[<ffffffff81c2e46b>] dump_stack+0xb3/0x118 lib/dump_stack.c:51
[<ffffffff81334ea9>] lockdep_rcu_suspicious+0x139/0x180 kernel/locking/lockdep.c:4445
[< inline >] __kvm_memslots include/linux/kvm_host.h:534
[< inline >] kvm_memslots include/linux/kvm_host.h:541
[<ffffffff8105d6ae>] kvm_gfn_to_hva_cache_init+0xa1e/0xce0 virt/kvm/kvm_main.c:1941
[<ffffffff8112685d>] kvm_lapic_set_vapic_addr+0xed/0x140 arch/x86/kvm/lapic.c:2217--- arch/x86/kvm/x86.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/x86.c +3 -0
@@ -3182,6 +3182,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp, };case KVM_SET_VAPIC_ADDR : {struct kvm_vapic_addr va; +int idx; r = -EINVAL ;if (!irqchip_in_kernel (vcpu->kvm))
@@ -3189,7 +3190,9 @@ long kvm_arch_vcpu_ioctl(struct file *filp, r = -EFAULT ;if (copy_from_user (&va, argp,sizeof va)) goto out; + idx =srcu_read_lock (&vcpu->kvm->srcu); r =kvm_lapic_set_vapic_addr (vcpu, va.vapic_addr); +srcu_read_unlock (&vcpu->kvm->srcu, idx);break ; }case KVM_X86_SETUP_MCE : {
Signature · 1 line
2.8.0.rc2.1.gbe9624a
PATCH 29/319 KVM: Disable irq while unregistering user notifier
In reply to Willy TarreauFrom: Ignacio Alvarado <[email protected]>
commit 1650b4ebc99da4c137bfbfc531be4a2405f951dd upstream.
Function user_notifier_unregister should be called only once for each registered user notifier.
Function kvm_arch_hardware_disable can be executed from an IPI context which could cause a race condition with a VCPU returning to user mode and attempting to unregister the notifier.
--- arch/x86/kvm/x86.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +11 -2
@@ -178,7 +178,18 @@ static void kvm_on_user_return(struct user_return_notifier *urn)struct kvm_shared_msrs *locals = container_of(urn, struct kvm_shared_msrs, urn);struct kvm_shared_msr_values *values; +unsigned long flags; +/* +* Disabling irqs at this point since the following code could be +* interrupted and executed through kvm_arch_hardware_disable() +*/ +local_irq_save (flags); +if (locals->registered) { + locals->registered = false; +user_return_notifier_unregister (urn); + } +local_irq_restore (flags);for (slot =0 ; slot < shared_msrs_global.nr; ++slot) { values = &locals->values[slot];if (values->host != values->curr) {
@@ -186,8 +197,6 @@ static void kvm_on_user_return(struct user_return_notifier *urn) values->curr = values->host; } } - locals->registered = false; -user_return_notifier_unregister (urn); }static void shared_msr_update (unsigned slot,u32 msr)
Signature · 1 line
2.8.0.rc2.1.gbe9624a