From patchwork Tue Jul 18 09:13:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 13316903 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 36934EB64DA for ; Tue, 18 Jul 2023 09:14:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231200AbjGRJOM (ORCPT ); Tue, 18 Jul 2023 05:14:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229778AbjGRJOK (ORCPT ); Tue, 18 Jul 2023 05:14:10 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA12EE55 for ; Tue, 18 Jul 2023 02:13:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689671600; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4WlIkr5Df/8dGsk772CbAon6bnUBTqjKXV9qoX9zRiI=; b=UrXO1J0d/C0L9J7/HHwr2Agf8bcj7drQnEqRJrt955NF7p4539XnVO6k5IJGhGnd5WZS8o xMqXIZUN9z6ng/Yr26V7Z7v0czEhbVOasr9QTr1QKxE4yh6lskiR24+CRsCMfakcZ0T/NK UGLll2WnmqIxGQmjQZSCQf9+JudBA04= Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-14-dSfb6Q6kOYqqc21jhdQidA-1; Tue, 18 Jul 2023 05:13:18 -0400 X-MC-Unique: dSfb6Q6kOYqqc21jhdQidA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 685113803916; Tue, 18 Jul 2023 09:13:18 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.224.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id ECCAC1454143; Tue, 18 Jul 2023 09:13:15 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: "H. Peter Anvin" , Sean Christopherson , linux-kernel@vger.kernel.org, Thomas Gleixner , Paolo Bonzini , Ingo Molnar , Dave Hansen , x86@kernel.org, Borislav Petkov , Maxim Levitsky Subject: [PATCH 1/3] KVM: x86: VMX: __kvm_apic_update_irr must update the IRR atomically Date: Tue, 18 Jul 2023 12:13:08 +0300 Message-Id: <20230718091310.119672-2-mlevitsk@redhat.com> In-Reply-To: <20230718091310.119672-1-mlevitsk@redhat.com> References: <20230718091310.119672-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org If APICv is inhibited, then IPIs from peer vCPUs are done by atomically setting bits in IRR. This means, that when __kvm_apic_update_irr copies PIR to IRR, it has to modify IRR atomically as well. Signed-off-by: Maxim Levitsky --- arch/x86/kvm/lapic.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index e542cf285b5184..b3f57e0f0d64ae 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -627,15 +627,19 @@ bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr) for (i = vec = 0; i <= 7; i++, vec += 32) { pir_val = READ_ONCE(pir[i]); - irr_val = *((u32 *)(regs + APIC_IRR + i * 0x10)); + irr_val = READ_ONCE(*((u32 *)(regs + APIC_IRR + i * 0x10))); + if (pir_val) { + pir_val = xchg(&pir[i], 0); + + while (!try_cmpxchg(((u32 *)(regs + APIC_IRR + i * 0x10)), + &irr_val, irr_val | pir_val)); + prev_irr_val = irr_val; - irr_val |= xchg(&pir[i], 0); - *((u32 *)(regs + APIC_IRR + i * 0x10)) = irr_val; - if (prev_irr_val != irr_val) { - max_updated_irr = - __fls(irr_val ^ prev_irr_val) + vec; - } + irr_val |= pir_val; + + if (prev_irr_val != irr_val) + max_updated_irr = __fls(irr_val ^ prev_irr_val) + vec; } if (irr_val) *max_irr = __fls(irr_val) + vec; From patchwork Tue Jul 18 09:13:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 13316904 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8D28EB64DC for ; Tue, 18 Jul 2023 09:14:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231845AbjGRJON (ORCPT ); Tue, 18 Jul 2023 05:14:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230260AbjGRJOL (ORCPT ); Tue, 18 Jul 2023 05:14:11 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7F8310C2 for ; Tue, 18 Jul 2023 02:13:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689671605; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4C0D24d2FC1TYs50FyhHTmpHwv4hCv9vozKRBzYFkPk=; b=UwJ/38koG/XjnlPzIYTGcjLcmuqZkHWffD0fR1IheDPe7qKDTGo7ib3Zizg6DR9ABBpMv2 C94dD44chJiMV0Wniv1F9OO2+RKZPfUyPry9WzmKN1Y7YwCUqYVl41kAZfXUF2DK0DMan3 bSKVkUz+7h9jSM7QlJmu5PKuhCWLtfE= Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-423-Hs3Upf_oN22Bel_I6Zw-CQ-1; Tue, 18 Jul 2023 05:13:21 -0400 X-MC-Unique: Hs3Upf_oN22Bel_I6Zw-CQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3D0082A59542; Tue, 18 Jul 2023 09:13:21 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.224.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id C14791454142; Tue, 18 Jul 2023 09:13:18 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: "H. Peter Anvin" , Sean Christopherson , linux-kernel@vger.kernel.org, Thomas Gleixner , Paolo Bonzini , Ingo Molnar , Dave Hansen , x86@kernel.org, Borislav Petkov , Maxim Levitsky Subject: [PATCH 2/3] KVM: x86: VMX: set irr_pending in kvm_apic_update_irr Date: Tue, 18 Jul 2023 12:13:09 +0300 Message-Id: <20230718091310.119672-3-mlevitsk@redhat.com> In-Reply-To: <20230718091310.119672-1-mlevitsk@redhat.com> References: <20230718091310.119672-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org When the APICv is inhibited, the irr_pending optimization is used. Therefore, when kvm_apic_update_irr sets bits in the IRR, it must set irr_pending to true as well. Signed-off-by: Maxim Levitsky --- arch/x86/kvm/lapic.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index b3f57e0f0d64ae..613830ba2d8cef 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -653,8 +653,11 @@ EXPORT_SYMBOL_GPL(__kvm_apic_update_irr); bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir, int *max_irr) { struct kvm_lapic *apic = vcpu->arch.apic; + bool irr_updated = __kvm_apic_update_irr(pir, apic->regs, max_irr); - return __kvm_apic_update_irr(pir, apic->regs, max_irr); + if (unlikely(!apic->apicv_active && irr_updated)) + apic->irr_pending = true; + return irr_updated; } EXPORT_SYMBOL_GPL(kvm_apic_update_irr); From patchwork Tue Jul 18 09:13:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 13316906 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 65EABEB64DC for ; Tue, 18 Jul 2023 09:14:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232236AbjGRJOc (ORCPT ); Tue, 18 Jul 2023 05:14:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232168AbjGRJOS (ORCPT ); Tue, 18 Jul 2023 05:14:18 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D30210E2 for ; Tue, 18 Jul 2023 02:13:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689671609; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=i0Za5rKgUBZTxkAktgjwNM5+Kehhc/iwdSkhd6KTsmE=; b=Q/76L6BYY7bdqDmgKfQnTKf6nsSFOovLhiq34fuFQJ+BeAM4OAPg+81/uLe8djhh46zJRF nQ+yE4gspvJlvnFYUVYBso4fxjE3S5QqRzsd2+Fj7EZzuWYYB2oPsgko/fdZ5VNIPV88th 2lZ8XXiUk7FrKqmmXlkGWej98I4iBLo= Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-611-q8j76lraNJGR3zgugEfZsA-1; Tue, 18 Jul 2023 05:13:24 -0400 X-MC-Unique: q8j76lraNJGR3zgugEfZsA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 12E972A59543; Tue, 18 Jul 2023 09:13:24 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.224.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id 95ACD1454142; Tue, 18 Jul 2023 09:13:21 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: "H. Peter Anvin" , Sean Christopherson , linux-kernel@vger.kernel.org, Thomas Gleixner , Paolo Bonzini , Ingo Molnar , Dave Hansen , x86@kernel.org, Borislav Petkov , Maxim Levitsky Subject: [PATCH 3/3] KVM: x86: check the kvm_cpu_get_interrupt result before using it Date: Tue, 18 Jul 2023 12:13:10 +0300 Message-Id: <20230718091310.119672-4-mlevitsk@redhat.com> In-Reply-To: <20230718091310.119672-1-mlevitsk@redhat.com> References: <20230718091310.119672-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The code was blindly assuming that kvm_cpu_get_interrupt never returns -1 when there is a pending interrupt. While this should be true, a bug in KVM can still cause this. If -1 is returned, the code before this patch was converting it to 0xFF, and 0xFF interrupt was injected to the guest, which results in an issue which was hard to debug. Add WARN_ON_ONCE to catch this case and skip the injection if this happens again. Signed-off-by: Maxim Levitsky --- arch/x86/kvm/x86.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c0778ca39650f4..37162c589e8d0e 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10190,9 +10190,13 @@ static int kvm_check_and_inject_events(struct kvm_vcpu *vcpu, if (r < 0) goto out; if (r) { - kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), false); - static_call(kvm_x86_inject_irq)(vcpu, false); - WARN_ON(static_call(kvm_x86_interrupt_allowed)(vcpu, true) < 0); + int irq = kvm_cpu_get_interrupt(vcpu); + + if (!WARN_ON_ONCE(irq == -1)) { + kvm_queue_interrupt(vcpu, irq, false); + static_call(kvm_x86_inject_irq)(vcpu, false); + WARN_ON(static_call(kvm_x86_interrupt_allowed)(vcpu, true) < 0); + } } if (kvm_cpu_has_injectable_intr(vcpu)) static_call(kvm_x86_enable_irq_window)(vcpu);