Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp1357798ybz; Wed, 22 Apr 2020 19:29:13 -0700 (PDT) X-Google-Smtp-Source: APiQypImClDEdAPvjdQqZpcxBJQzw5O+msAO4dGwI4GgkDgSbQbh/6A9Y9Npu1hJHtya87gKE5pz X-Received: by 2002:a17:906:3e96:: with SMTP id a22mr960970ejj.232.1587608953584; Wed, 22 Apr 2020 19:29:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587608953; cv=none; d=google.com; s=arc-20160816; b=rVluVrDTC/t8nWcj8JK1sf0vdxkCZtcfYOI78eQGKFgKRPA+w0QZetzeD8zqaHUwEA TKOjEb9K/mzK46HV/gIslk6uMWVxUasFePHflYPhAl8owZUJhCIxUSyzzk98xpyr3a2I pduwe38lLJFFy4EqK6SLqGBHmybR80Uk6rJi649+BQj7aoTbcZz/z4p1VU4a89z8v27t OOSWASXuzn86OsxRgqPaQFlQ7BqZScLP+pHweB91CMSxuECDuyWxlZLdpeibVEJwxisa Y/++EJkOFaMaxHN6N9N2KqW6bH41vFslrNwi+NE3Tq6p3XCXnaQSWnyj5B8gBFfOvQBo MTjw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=rB/CNmQaU7InvK2a8zoXkruU/UCmIwfT3XIio7Xe8/s=; b=Vbce4c1uvRI7uEt9exRrullA/R7z30bJQZSoTFmpfeZtjjOeD9U45c4CSOyyo5I4Oi q/tUbWgN8CagId5Iy5CGoQE8lKdhU8hO+zBmw+KfyAO4cOaa5K0KagVJTpwh4gi1Tn6D /VABgiLir6foffA4kkffy73lDo8MlLRq9Fz8eF9j6KeYtxS5h6no3vtVoY6L0uLGHG+V tttJtnmvgyua1oM0l/2D+YGHVGeCDks5eBN5EZYvxzDdfpipI630pHuhiQ5slR7gU+TB 8mlvsRMInz8990NiwS4IF6eyNRHe4j/hvg4z9HlUuS1ITD76FCwbxs48Dq+e9ba4Pzdc mcwA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u16si499239edd.86.2020.04.22.19.28.51; Wed, 22 Apr 2020 19:29:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726760AbgDWC0z (ORCPT + 99 others); Wed, 22 Apr 2020 22:26:55 -0400 Received: from mga06.intel.com ([134.134.136.31]:33065 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726541AbgDWCZ4 (ORCPT ); Wed, 22 Apr 2020 22:25:56 -0400 IronPort-SDR: 4pefOQtHnSjUXkk6rEKj5Z2PuGaHLh0pZSCrCDfVy0pe1ZoS/PsE9iSDVij3lynAdEZ3hiTnmZ 0NGBFI1RvJkw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Apr 2020 19:25:55 -0700 IronPort-SDR: UjKYGvDlp6USocJXccboeEzgMSiDuFUu03zbK1ScOPedwGhoa2Uc+X5D2L4fy9uMoFKq5Nm4+3 rmyc3bUVzv3Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,305,1583222400"; d="scan'208";a="259273967" Received: from sjchrist-coffee.jf.intel.com ([10.54.74.202]) by orsmga006.jf.intel.com with ESMTP; 22 Apr 2020 19:25:54 -0700 From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Oliver Upton , Peter Shier Subject: [PATCH 12/13] KVM: x86: Replace late check_nested_events() hack with more precise fix Date: Wed, 22 Apr 2020 19:25:49 -0700 Message-Id: <20200423022550.15113-13-sean.j.christopherson@intel.com> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200423022550.15113-1-sean.j.christopherson@intel.com> References: <20200423022550.15113-1-sean.j.christopherson@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a separate hook for checking if interrupt injection is blocked and use the hook to handle the case where an interrupt arrives between check_nested_events() and the injection logic. Drop the retry of check_nested_events() that hack-a-fixed the same condition. Blocking injection is also a bit of a hack, e.g. KVM should do exiting and non-exiting interrupt processing in a single pass, but it's a more precise hack. The old comment is also misleading, e.g. KVM_REQ_EVENT is purely an optimization, setting it on every run loop (which KVM doesn't do) should not affect functionality, only performance. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/vmx/vmx.c | 13 +++++++++++++ arch/x86/kvm/x86.c | 22 ++++------------------ 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 787636acd648..16fdeddb4a65 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1140,6 +1140,7 @@ struct kvm_x86_ops { void (*queue_exception)(struct kvm_vcpu *vcpu); void (*cancel_injection)(struct kvm_vcpu *vcpu); bool (*interrupt_allowed)(struct kvm_vcpu *vcpu); + bool (*interrupt_injection_allowed)(struct kvm_vcpu *vcpu); bool (*nmi_allowed)(struct kvm_vcpu *vcpu); bool (*get_nmi_mask)(struct kvm_vcpu *vcpu); void (*set_nmi_mask)(struct kvm_vcpu *vcpu, bool masked); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index f21f734861dd..6d3ccbfc9e6a 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3993,6 +3993,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .queue_exception = svm_queue_exception, .cancel_injection = svm_cancel_injection, .interrupt_allowed = svm_interrupt_allowed, + .interrupt_injection_allowed = svm_interrupt_allowed, .nmi_allowed = svm_nmi_allowed, .get_nmi_mask = svm_get_nmi_mask, .set_nmi_mask = svm_set_nmi_mask, diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 2f8cacb3aa9b..68b3748b5383 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4550,6 +4550,18 @@ static bool vmx_interrupt_allowed(struct kvm_vcpu *vcpu) return !vmx_interrupt_blocked(vcpu); } +static bool vmx_interrupt_injection_allowed(struct kvm_vcpu *vcpu) +{ + /* + * An IRQ must not be injected into L2 if it's supposed to VM-Exit, + * e.g. if the IRQ arrived asynchronously after checking nested events. + */ + if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) + return false; + + return vmx_interrupt_allowed(vcpu); +} + static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) { int ret; @@ -7823,6 +7835,7 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .queue_exception = vmx_queue_exception, .cancel_injection = vmx_cancel_injection, .interrupt_allowed = vmx_interrupt_allowed, + .interrupt_injection_allowed = vmx_interrupt_injection_allowed, .nmi_allowed = vmx_nmi_allowed, .get_nmi_mask = vmx_get_nmi_mask, .set_nmi_mask = vmx_set_nmi_mask, diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 7c49a7dc601f..d9d6028a77e0 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7755,24 +7755,10 @@ static int inject_pending_event(struct kvm_vcpu *vcpu) --vcpu->arch.nmi_pending; vcpu->arch.nmi_injected = true; kvm_x86_ops.set_nmi(vcpu); - } else if (kvm_cpu_has_injectable_intr(vcpu)) { - /* - * Because interrupts can be injected asynchronously, we are - * calling check_nested_events again here to avoid a race condition. - * See https://lkml.org/lkml/2014/7/2/60 for discussion about this - * proposal and current concerns. Perhaps we should be setting - * KVM_REQ_EVENT only on certain events and not unconditionally? - */ - if (is_guest_mode(vcpu) && kvm_x86_ops.check_nested_events) { - r = kvm_x86_ops.check_nested_events(vcpu); - if (r != 0) - return r; - } - if (kvm_x86_ops.interrupt_allowed(vcpu)) { - kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), - false); - kvm_x86_ops.set_irq(vcpu); - } + } else if (kvm_cpu_has_injectable_intr(vcpu) && + kvm_x86_ops.interrupt_injection_allowed(vcpu)) { + kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), false); + kvm_x86_ops.set_irq(vcpu); } return 0; -- 2.26.0