Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp2153245ybh; Sun, 4 Aug 2019 19:04:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqw7Je4CXzUvacpYUOBLZON+v5Zs1UmiUyvnEf3ZmuKI1hKN2Qa0HfZTfw9qxIuK5sEyrA57 X-Received: by 2002:aa7:8f24:: with SMTP id y4mr69855182pfr.36.1564970665306; Sun, 04 Aug 2019 19:04:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564970665; cv=none; d=google.com; s=arc-20160816; b=GL0MzwbV2JSUjJfYa0PV4h74rg7HvgcuS5h0MYGktXCrEFQDwUSGYQOaG9BOP8BRSt 9gJziD/wgkZ3yQ8AsJRppV2KYXBu4WiHRIvPzH4U6IIPLCpcjPrTLymUtzBuK7ncNkKg l72mcpQzFGPK3RrQZjpv0ZxMXVQkEXln40U9ClYDNg5T7derx03D5SA7QYEkMtn7ozNG MWRASvfyiETiDUZaHZehEvwDGkgUvtmjDVLIyHwHTWCvthb1+BHAVeMoACr5ktfTt8X5 I4lCKpOcLsFk9y1Dpp5LMryWkYWLEIaJiTBObvAz4MFA5dXtE6NI6PNeD2XjEQzW2CiR vnBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=jiBgil2/CkGQonyAGH+YMpxsvKYd1DL9LetvX2PzPhw=; b=xJbYj3J7MdCp7MfxcDDYhPC6ZttEn522DU7LCk2BAChCcrVKBIALEcG0aqSQmpVbVd XjRRyaSUL/L1LUAc92oEBNBAXva09n3e9tJcmcbZe3hUlBOYHGuKA4Wtcatd+tGpKTsY L/S1axR6NRUnAaQSUipPDE2bxY9rBhMQssUv3kOdo7qpfBIZQ/fRpVjrdnxdPBhaLsO1 bpjUnulwTHnI3GL0V8ICPtFHybp/tg5vJ+CZv5aLXNFHvN0QvJ89EDNTJzujoD6dSAXn fzHk7aAQLiJEL48sAg0UMAyFcczSKanSmlxdDhIxRxz7jylKAAufTf9i9AfIiZrsyTYb Wk7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Sri3eDfc; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j187si43190123pge.591.2019.08.04.19.04.09; Sun, 04 Aug 2019 19:04:25 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Sri3eDfc; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726976AbfHECDe (ORCPT + 99 others); Sun, 4 Aug 2019 22:03:34 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:45301 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726561AbfHECDd (ORCPT ); Sun, 4 Aug 2019 22:03:33 -0400 Received: by mail-pf1-f195.google.com with SMTP id r1so38758706pfq.12; Sun, 04 Aug 2019 19:03:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=jiBgil2/CkGQonyAGH+YMpxsvKYd1DL9LetvX2PzPhw=; b=Sri3eDfc55rI/wpwxTRkj+wYpuXeNOp1RNaGXWAtZwYN2OSDgwA1vmavxFv5ksAwrI 4dQtipdp0Dcnh06Hnoq53v+zCyNVgRa44voGibej/Jbefpn29PqGcMjqezq0O2DGe1X1 A15jqQvx/GpcghTEiftmrQlt3jrqN74mEPBJ3nokf9EKm049NisKhPuv+KGC6q32cyQM PdAowMOLod1qYJ27TLW/+qjNNNoanboDzG+JbneWMBDHLG+rI+618ftP4Fq1gDwuwLKG pO+fwW775gdlwvyGZ3SyWlqobd5CyJtZYfB9EUJojPwr2byWtz7/ZKgOX8f0CoPlBpLm 5BdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=jiBgil2/CkGQonyAGH+YMpxsvKYd1DL9LetvX2PzPhw=; b=DFG1J+PX7Pid4tvWwdE6isRkAIJRA0A+822Q+FCVtqOSsWFMs0RaBQS8DvhkhIDP1L bNMF7XyNrkqFJHORRR68cHTtuQOLmHriAG+KrfrWnhla1x6vxB+/j9s0e3G81Lya0std uySFe5F9PeZ0Ijd0l7bvYVez1K9SJOYcLM5OvejqUflKYCvDTyIy5erJvwuWTQfRmVvr laQ4ctV8dl4LhzFJZ6CDPEbSinh0W1Bc91o/ZV45qQ9yyFc4W9NrJnMLCS1HxiqCaByy 3If/u24ggKZjGdoelhyLpjzeOgugNVAUzllBUAPNxl/uFPTtGIaMfq8maNlunu7fn0CX p8PA== X-Gm-Message-State: APjAAAWMXU/to7V3gIEv+K/EbBxgAnrAGHfjwChwwFjgfH8rz8wxgRqv cyWMAKB5KSTso8PTlLLglwicO4U7 X-Received: by 2002:a63:7709:: with SMTP id s9mr18114256pgc.296.1564970612644; Sun, 04 Aug 2019 19:03:32 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.123]) by smtp.googlemail.com with ESMTPSA id o32sm14739365pje.9.2019.08.04.19.03.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 04 Aug 2019 19:03:32 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Christian Borntraeger , Marc Zyngier , stable@vger.kernel.org Subject: [PATCH v4 1/6] KVM: Fix leak vCPU's VMCS value into other pCPU Date: Mon, 5 Aug 2019 10:03:19 +0800 Message-Id: <1564970604-10044-1-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wanpeng Li After commit d73eb57b80b (KVM: Boost vCPUs that are delivering interrupts), a five years old bug is exposed. Running ebizzy benchmark in three 80 vCPUs VMs on one 80 pCPUs Skylake server, a lot of rcu_sched stall warning splatting in the VMs after stress testing: INFO: rcu_sched detected stalls on CPUs/tasks: { 4 41 57 62 77} (detected by 15, t=60004 jiffies, g=899, c=898, q=15073) Call Trace: flush_tlb_mm_range+0x68/0x140 tlb_flush_mmu.part.75+0x37/0xe0 tlb_finish_mmu+0x55/0x60 zap_page_range+0x142/0x190 SyS_madvise+0x3cd/0x9c0 system_call_fastpath+0x1c/0x21 swait_active() sustains to be true before finish_swait() is called in kvm_vcpu_block(), voluntarily preempted vCPUs are taken into account by kvm_vcpu_on_spin() loop greatly increases the probability condition kvm_arch_vcpu_runnable(vcpu) is checked and can be true, when APICv is enabled the yield-candidate vCPU's VMCS RVI field leaks(by vmx_sync_pir_to_irr()) into spinning-on-a-taken-lock vCPU's current VMCS. This patch fixes it by checking conservatively a subset of events. Cc: Paolo Bonzini Cc: Radim Krčmář Cc: Christian Borntraeger Cc: Marc Zyngier Cc: stable@vger.kernel.org Fixes: 98f4a1467 (KVM: add kvm_arch_vcpu_runnable() test to kvm_vcpu_on_spin() loop) Signed-off-by: Wanpeng Li --- v3 -> v4: * just test KVM_REQ_* * rename the hook to apicv_has_pending_interrupt * wrap with #ifdef CONFIG_KVM_ASYNC_PF v2 -> v3: * check conservatively a subset of events v1 -> v2: * checking swait_active(&vcpu->wq) for involuntary preemption arch/mips/kvm/mips.c | 5 +++++ arch/powerpc/kvm/powerpc.c | 5 +++++ arch/s390/kvm/kvm-s390.c | 5 +++++ arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm.c | 6 ++++++ arch/x86/kvm/vmx/vmx.c | 6 ++++++ arch/x86/kvm/x86.c | 16 ++++++++++++++++ include/linux/kvm_host.h | 1 + virt/kvm/arm/arm.c | 5 +++++ virt/kvm/kvm_main.c | 16 +++++++++++++++- 10 files changed, 65 insertions(+), 1 deletion(-) diff --git a/arch/mips/kvm/mips.c b/arch/mips/kvm/mips.c index 2cfe839..95a4642 100644 --- a/arch/mips/kvm/mips.c +++ b/arch/mips/kvm/mips.c @@ -98,6 +98,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) return !!(vcpu->arch.pending_exceptions); } +bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu) +{ + return kvm_arch_vcpu_runnable(vcpu); +} + bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) { return false; diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c index 0dba7eb..3e34d5f 100644 --- a/arch/powerpc/kvm/powerpc.c +++ b/arch/powerpc/kvm/powerpc.c @@ -50,6 +50,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) return !!(v->arch.pending_exceptions) || kvm_request_pending(v); } +bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu) +{ + return kvm_arch_vcpu_runnable(vcpu); +} + bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) { return false; diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c index 3f520cd8..5623b23 100644 --- a/arch/s390/kvm/kvm-s390.c +++ b/arch/s390/kvm/kvm-s390.c @@ -3102,6 +3102,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) return kvm_s390_vcpu_has_irq(vcpu, 0); } +bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu) +{ + return kvm_arch_vcpu_runnable(vcpu); +} + bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) { return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE); diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 7b0a4ee..25ffa7c 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1175,6 +1175,7 @@ struct kvm_x86_ops { int (*update_pi_irte)(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, bool set); void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu); + bool (*apicv_has_pending_interrupt)(struct kvm_vcpu *vcpu); int (*set_hv_timer)(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc, bool *expired); diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index 7eafc69..1b4384f 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -5190,6 +5190,11 @@ static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec) kvm_vcpu_wake_up(vcpu); } +static bool svm_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu) +{ + return false; +} + static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi) { unsigned long flags; @@ -7314,6 +7319,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .pmu_ops = &amd_pmu_ops, .deliver_posted_interrupt = svm_deliver_avic_intr, + .apicv_has_pending_interrupt = svm_apicv_has_pending_interrupt, .update_pi_irte = svm_update_pi_irte, .setup_mce = svm_setup_mce, diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 074385c..59871b6 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6117,6 +6117,11 @@ static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) return max_irr; } +static bool vmx_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu) +{ + return pi_test_on(vcpu_to_pi_desc(vcpu)); +} + static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap) { if (!kvm_vcpu_apicv_active(vcpu)) @@ -7726,6 +7731,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt, .sync_pir_to_irr = vmx_sync_pir_to_irr, .deliver_posted_interrupt = vmx_deliver_posted_interrupt, + .apicv_has_pending_interrupt = vmx_apicv_has_pending_interrupt, .set_tss_addr = vmx_set_tss_addr, .set_identity_map_addr = vmx_set_identity_map_addr, diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c6d951c..f715efb 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9698,6 +9698,22 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu); } +bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu) +{ + if (READ_ONCE(vcpu->arch.pv.pv_unhalted)) + return true; + + if (kvm_test_request(KVM_REQ_NMI, vcpu) || + kvm_test_request(KVM_REQ_SMI, vcpu) || + kvm_test_request(KVM_REQ_EVENT, vcpu)) + return true; + + if (vcpu->arch.apicv_active && kvm_x86_ops->apicv_has_pending_interrupt(vcpu)) + return true; + + return false; +} + bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) { return vcpu->arch.preempted_in_kernel; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 5c5b586..9e4c2bb 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -872,6 +872,7 @@ int kvm_arch_check_processor_compat(void); int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu); bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu); int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu); +bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu); #ifndef __KVM_HAVE_ARCH_VM_ALLOC /* diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c index acc4324..2927895 100644 --- a/virt/kvm/arm/arm.c +++ b/virt/kvm/arm/arm.c @@ -444,6 +444,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) && !v->arch.power_off && !v->arch.pause); } +bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu) +{ + return kvm_arch_vcpu_runnable(vcpu); +} + bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) { return vcpu_mode_priv(vcpu); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 887f3b0..e121423 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2477,6 +2477,20 @@ static bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu) #endif } +static bool vcpu_runnable(struct kvm_vcpu *vcpu) +{ + /* It is called outside vcpu_load/vcpu_put */ + if (kvm_arch_dy_runnable(vcpu)) + return true; + +#ifdef CONFIG_KVM_ASYNC_PF + if (!list_empty_careful(&vcpu->async_pf.done)) + return true; +#endif + + return false; +} + void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode) { struct kvm *kvm = me->kvm; @@ -2506,7 +2520,7 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode) continue; if (vcpu == me) continue; - if (swait_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu)) + if (swait_active(&vcpu->wq) && !vcpu_runnable(vcpu)) continue; if (yield_to_kernel_mode && !kvm_arch_vcpu_in_kernel(vcpu)) continue; -- 2.7.4