Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp4294313pxu; Tue, 1 Dec 2020 01:04:20 -0800 (PST) X-Google-Smtp-Source: ABdhPJxa54ZT1toRUmnooym0HVzLiFTI2gPEBfQfQjXj+osgNaz7D1vYcP02DKDqx+nt9Gyq5rgp X-Received: by 2002:aa7:d593:: with SMTP id r19mr1240291edq.246.1606813460394; Tue, 01 Dec 2020 01:04:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606813460; cv=none; d=google.com; s=arc-20160816; b=cZBDr5dYrY6X0GKMMG7+OsPmfZU1nH2SfXPcc+BW28Fn9ieAtgbQDER0MiESsOBFcV UK2WymoQx5nFhJTgjidoyoDCOkmzStR8aiA4nFIPSvnj+MMwxEml1I5B0uRDVN1POUkQ h+VB3idstA0YWiMqNSPDJq0lnfUwnZT5HFmB138ThiZMGVbyA/9CLNbnR+iMUAisgT/j Er+MxkyehkgGipWl9j3mhW+J6exPC7ZfT3CDsruEmXM6Lq7EHWp7QF4rcSFutQJKxF9R 2Ox6lNBF5y+0DyAnnQ81nboLd9KVVR3ph+xvPzpXL0MHgloAVvWne89d5MegYdVC979q AX9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ZnJpZsJnHHTpB2HeK6dfeMQVY2hP5uxHNBlZbLKua2k=; b=NKD7TioEdGXDxOOsvAUAzV2Jztqrq1bL+Mo54u9mJ5s3tEFl1FXrxgqOr+0vEcnT9n R+R4h5FQ3aOdenDtWQ7DaRjO55EtYJbOoZH8N/4eFw+Skfu09S0yOco/90hI2jOG/Vde 863KGPji/zzoW9gwHUMqKxM7f7Jm9Sh2texi8C1fofLDcbwrenSNWWMqCVvVppgaTrIV xvadUDAP6rYCkackSe1aY5o1FoMyPJkRuv+Q+AAXXcq1Uteb8aJ5WCzfI4rt+vlR9ii/ bks09bLAKvQKG746d1mLCwnO80dP48gNG8eu9bZs1o+t/ApzRywfEWn1GxMAEvHk0+ZV DvOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Yy6NEPg1; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bf22si702716edb.560.2020.12.01.01.03.57; Tue, 01 Dec 2020 01:04:20 -0800 (PST) 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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Yy6NEPg1; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387982AbgLAI6o (ORCPT + 99 others); Tue, 1 Dec 2020 03:58:44 -0500 Received: from mail.kernel.org ([198.145.29.99]:34166 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387971AbgLAI6h (ORCPT ); Tue, 1 Dec 2020 03:58:37 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 68BB422241; Tue, 1 Dec 2020 08:57:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1606813076; bh=N/C1JiIj6VQne11zfGbWAwPrbPCiG77eVWMcMavvBug=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Yy6NEPg1foXdWd0BhdfZ4r2hr/N3WGUVELguFC92pmzRXl1zbpJIg3jvIWCcT2LTu lgQtYacnDiSNPLxlf8O2BOLvrgqVZJ4cqV6ML43dvPMXnYMzzMAfC8kEwGDhHLeG9n IgDBl8t51YfC4sVCK3C5gjK4ilE1uBl5Xrl/8PNI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, David Woodhouse , Paolo Bonzini , Nikos Tsironis Subject: [PATCH 4.14 09/50] KVM: x86: Fix split-irqchip vs interrupt injection window request Date: Tue, 1 Dec 2020 09:53:08 +0100 Message-Id: <20201201084646.008889795@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201201084644.803812112@linuxfoundation.org> References: <20201201084644.803812112@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Paolo Bonzini commit 71cc849b7093bb83af966c0e60cb11b7f35cd746 upstream. kvm_cpu_accept_dm_intr and kvm_vcpu_ready_for_interrupt_injection are a hodge-podge of conditions, hacked together to get something that more or less works. But what is actually needed is much simpler; in both cases the fundamental question is, do we have a place to stash an interrupt if userspace does KVM_INTERRUPT? In userspace irqchip mode, that is !vcpu->arch.interrupt.injected. Currently kvm_event_needs_reinjection(vcpu) covers it, but it is unnecessarily restrictive. In split irqchip mode it's a bit more complicated, we need to check kvm_apic_accept_pic_intr(vcpu) (the IRQ window exit is basically an INTACK cycle and thus requires ExtINTs not to be masked) as well as !pending_userspace_extint(vcpu). However, there is no need to check kvm_event_needs_reinjection(vcpu), since split irqchip keeps pending ExtINT state separate from event injection state, and checking kvm_cpu_has_interrupt(vcpu) is wrong too since ExtINT has higher priority than APIC interrupts. In fact the latter fixes a bug: when userspace requests an IRQ window vmexit, an interrupt in the local APIC can cause kvm_cpu_has_interrupt() to be true and thus kvm_vcpu_ready_for_interrupt_injection() to return false. When this happens, vcpu_run does not exit to userspace but the interrupt window vmexits keep occurring. The VM loops without any hope of making progress. Once we try to fix these with something like return kvm_arch_interrupt_allowed(vcpu) && - !kvm_cpu_has_interrupt(vcpu) && - !kvm_event_needs_reinjection(vcpu) && - kvm_cpu_accept_dm_intr(vcpu); + (!lapic_in_kernel(vcpu) + ? !vcpu->arch.interrupt.injected + : (kvm_apic_accept_pic_intr(vcpu) + && !pending_userspace_extint(v))); we realize two things. First, thanks to the previous patch the complex conditional can reuse !kvm_cpu_has_extint(vcpu). Second, the interrupt window request in vcpu_enter_guest() bool req_int_win = dm_request_for_irq_injection(vcpu) && kvm_cpu_accept_dm_intr(vcpu); should be kept in sync with kvm_vcpu_ready_for_interrupt_injection(): it is unnecessary to ask the processor for an interrupt window if we would not be able to return to userspace. Therefore, kvm_cpu_accept_dm_intr(vcpu) is basically !kvm_cpu_has_extint(vcpu) ANDed with the existing check for masked ExtINT. It all makes sense: - we can accept an interrupt from userspace if there is a place to stash it (and, for irqchip split, ExtINTs are not masked). Interrupts from userspace _can_ be accepted even if right now EFLAGS.IF=0. - in order to tell userspace we will inject its interrupt ("IRQ window open" i.e. kvm_vcpu_ready_for_interrupt_injection), both KVM and the vCPU need to be ready to accept the interrupt. ... and this is what the patch implements. Reported-by: David Woodhouse Analyzed-by: David Woodhouse Cc: stable@vger.kernel.org Signed-off-by: Paolo Bonzini Reviewed-by: Nikos Tsironis Reviewed-by: David Woodhouse Tested-by: David Woodhouse Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/irq.c | 2 +- arch/x86/kvm/x86.c | 18 ++++++++++-------- 3 files changed, 12 insertions(+), 9 deletions(-) --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1395,6 +1395,7 @@ int kvm_test_age_hva(struct kvm *kvm, un void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte); int kvm_cpu_has_injectable_intr(struct kvm_vcpu *v); int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu); +int kvm_cpu_has_extint(struct kvm_vcpu *v); int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu); int kvm_cpu_get_interrupt(struct kvm_vcpu *v); void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event); --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -52,7 +52,7 @@ static int pending_userspace_extint(stru * check if there is pending interrupt from * non-APIC source without intack. */ -static int kvm_cpu_has_extint(struct kvm_vcpu *v) +int kvm_cpu_has_extint(struct kvm_vcpu *v) { u8 accept = kvm_apic_accept_pic_intr(v); --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3144,21 +3144,23 @@ static int kvm_vcpu_ioctl_set_lapic(stru static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu) { + /* + * We can accept userspace's request for interrupt injection + * as long as we have a place to store the interrupt number. + * The actual injection will happen when the CPU is able to + * deliver the interrupt. + */ + if (kvm_cpu_has_extint(vcpu)) + return false; + + /* Acknowledging ExtINT does not happen if LINT0 is masked. */ return (!lapic_in_kernel(vcpu) || kvm_apic_accept_pic_intr(vcpu)); } -/* - * if userspace requested an interrupt window, check that the - * interrupt window is open. - * - * No need to exit to userspace if we already have an interrupt queued. - */ static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu) { return kvm_arch_interrupt_allowed(vcpu) && - !kvm_cpu_has_interrupt(vcpu) && - !kvm_event_needs_reinjection(vcpu) && kvm_cpu_accept_dm_intr(vcpu); }