From patchwork Wed Jul 26 13:59:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 13328071 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 27459C001B0 for ; Wed, 26 Jul 2023 14:01:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233431AbjGZOBS (ORCPT ); Wed, 26 Jul 2023 10:01:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59686 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232740AbjGZOBL (ORCPT ); Wed, 26 Jul 2023 10:01: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 CFDDC2688 for ; Wed, 26 Jul 2023 07:00:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1690380022; 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=3ArnVy5ynfW5TPp7Ho0vkusYDFjjSaHrT1uTnmW5QUE=; b=ijCzmdRHdaq61Xa41Yi9aJULKO/JGIzzjyBhYgD3NRclLKqZogpvsWupM+LqExCpKibA5K sJcnR613INBhMg40D+NjCNGU2S9P5NqkbAbkwbeFeKVF2FQTNV+YTJvL7NZI4g2XmuRE/j JHgtDicWaSYQYZQ3Mq8NVOy4xSG4ztg= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-561-V_st_u7pN5SV1v_VwPI4fQ-1; Wed, 26 Jul 2023 10:00:16 -0400 X-MC-Unique: V_st_u7pN5SV1v_VwPI4fQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4C8A0800CAC; Wed, 26 Jul 2023 14:00:02 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.224.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1B1A240C2063; Wed, 26 Jul 2023 13:59:55 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, x86@kernel.org, Borislav Petkov , Paolo Bonzini , Sean Christopherson , Dave Hansen , Thomas Gleixner , "H. Peter Anvin" , Ingo Molnar , Maxim Levitsky Subject: [PATCH v2 1/3] KVM: x86: VMX: __kvm_apic_update_irr must update the IRR atomically Date: Wed, 26 Jul 2023 16:59:43 +0300 Message-Id: <20230726135945.260841-2-mlevitsk@redhat.com> In-Reply-To: <20230726135945.260841-1-mlevitsk@redhat.com> References: <20230726135945.260841-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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 | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 113ca9661ab21d..b17b37e4d4fcd1 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -637,16 +637,22 @@ bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr) *max_irr = -1; for (i = vec = 0; i <= 7; i++, vec += 32) { + u32 *p_irr = (u32 *)(regs + APIC_IRR + i * 0x10); + + irr_val = *p_irr; pir_val = READ_ONCE(pir[i]); - irr_val = *((u32 *)(regs + APIC_IRR + i * 0x10)); + if (pir_val) { + pir_val = xchg(&pir[i], 0); + 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; - } + do { + irr_val = prev_irr_val | pir_val; + } while (prev_irr_val != irr_val && + !try_cmpxchg(p_irr, &prev_irr_val, irr_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 Wed Jul 26 13:59:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 13328072 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 5558CC001B0 for ; Wed, 26 Jul 2023 14:01:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233996AbjGZOBm (ORCPT ); Wed, 26 Jul 2023 10:01:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234313AbjGZOBU (ORCPT ); Wed, 26 Jul 2023 10:01:20 -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 3DE682696 for ; Wed, 26 Jul 2023 07:00:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1690380039; 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=tbgdp6vazrgBiQOoNU+XwHpUdW8J49pFS7u9g5v72P0=; b=J3OG1tFS+MwiF2GlxBJIDTPlNka3R/w4pAy7PX6LV1yPT3swluCCc9XocerYv4mdsKXfAu 9A2zWwGrOju84gzIucuVlCcY9VWlipv8pzOzJnT1inCHUeMzSAMIxODM8m6CpqyIHRQTnU dRI0vWPqKzCyvZ64YSREzrOumoAFH8w= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-356-nLfhup3aMnSU8fi3piZYvA-1; Wed, 26 Jul 2023 10:00:33 -0400 X-MC-Unique: nLfhup3aMnSU8fi3piZYvA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CDF24830DB5; Wed, 26 Jul 2023 14:00:05 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.224.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9C25B40C2063; Wed, 26 Jul 2023 14:00:02 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, x86@kernel.org, Borislav Petkov , Paolo Bonzini , Sean Christopherson , Dave Hansen , Thomas Gleixner , "H. Peter Anvin" , Ingo Molnar , Maxim Levitsky Subject: [PATCH v2 2/3] KVM: x86: VMX: set irr_pending in kvm_apic_update_irr Date: Wed, 26 Jul 2023 16:59:44 +0300 Message-Id: <20230726135945.260841-3-mlevitsk@redhat.com> In-Reply-To: <20230726135945.260841-1-mlevitsk@redhat.com> References: <20230726135945.260841-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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 b17b37e4d4fcd1..a983a16163b137 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -666,8 +666,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 Wed Jul 26 13:59:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 13328074 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 81EF5C0015E for ; Wed, 26 Jul 2023 14:02:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233856AbjGZOBq (ORCPT ); Wed, 26 Jul 2023 10:01:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234355AbjGZOBX (ORCPT ); Wed, 26 Jul 2023 10:01:23 -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 DC31E2132 for ; Wed, 26 Jul 2023 07:00:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1690380037; 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=8NMiTq/NJ8G71mTK+kun4ROve/cQ69SRajLeV7++tZE=; b=L+RnbcRUI0oTgb8e7N2Qlrua+Pz2CW/Sz3gv+hL8L+aZSCgah6pES4Wy6BSrDqta5BPe0J 3JRCejXGJC85P3JQjXYJQ4A+xQnRBTBRHd8m9rCf0RV+HQujtldgFORuTsC9GHCjXYNAwK npa8xcaIGP6XgZALcNU3iVbHHskFKao= 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-696-NU3wojVyMBiKj4ZDe-OLeg-1; Wed, 26 Jul 2023 10:00:33 -0400 X-MC-Unique: NU3wojVyMBiKj4ZDe-OLeg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5E7343C11A09; Wed, 26 Jul 2023 14:00:11 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.224.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2CC5E40C2063; Wed, 26 Jul 2023 14:00:07 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, x86@kernel.org, Borislav Petkov , Paolo Bonzini , Sean Christopherson , Dave Hansen , Thomas Gleixner , "H. Peter Anvin" , Ingo Molnar , Maxim Levitsky Subject: [PATCH v2 3/3] KVM: x86: check the kvm_cpu_get_interrupt result before using it Date: Wed, 26 Jul 2023 16:59:45 +0300 Message-Id: <20230726135945.260841-4-mlevitsk@redhat.com> In-Reply-To: <20230726135945.260841-1-mlevitsk@redhat.com> References: <20230726135945.260841-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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 a6b9bea62fb8ac..00b87fcf6da4af 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10203,9 +10203,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);