From nobody Tue Apr 7 05:43:29 2026 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 87915ECAAD5 for ; Wed, 31 Aug 2022 00:37:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231774AbiHaAhG (ORCPT ); Tue, 30 Aug 2022 20:37:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231829AbiHaAgN (ORCPT ); Tue, 30 Aug 2022 20:36:13 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9E66A00D0 for ; Tue, 30 Aug 2022 17:35:22 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id p8-20020a170902e74800b0017307429ca3so8946029plf.17 for ; Tue, 30 Aug 2022 17:35:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=Loe3MlTGcMk80vSZPHjHmMjavqy1PId/3gp5j3Ud0uU=; b=B+TprnwR0pgPPbE76syVYpuZqhC1waR/eOXBEcoJkGVOWULQh6Z3lQadB1ByrLmS8r OxnmBpGiT+g6/soGVFO18dm2EY7FONP7o6ts3u1yMAVXctVTCy/rXTNEWdihDYn5dTFM v9LU3DTRxwQlQQqr7tKQAtF4L5HHUpbrUJr+WOdFUU6ZQaXIX9YNBZIdVhRvwRNvPWoi N5DGrsUfQTJGitQYuDBOotavc1C1MqjBqiwFSt0j9tUhJn6IS5lrdhCnUFmv9hd/sltO s4DZBu45EZx7NN4jTnSrlhJwP/DXyzPx1SWrxbjA2TZ9Q1tDp0nl5c1ZeXWaYWZxzAC/ 1GDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=Loe3MlTGcMk80vSZPHjHmMjavqy1PId/3gp5j3Ud0uU=; b=EqWQvVNkHq7R5sKucyfIpKHJeGDVhDMg1JaxWi1SyLrRsUXieAaP+WVWfEnCgK+cxv ouawgBfN3gcTCArUN7ZlWFkqMYJrG9CXUd+Cq/WssNXoSj4IF2gZ/3KHRYap8Wo+ABq7 0rIEnniOTE4hkqFJXpe2ajWDnuXYp9lJJtRdODfByF5l7Z4nu3DmzpqlnK7SHFfj7n3n ePt1bC1r0AG0Q/vri/Dz2RFo2pTkYIbEYclEw2ksglUADwlcaxx2RC9Y1W5gd4C4/qQN qtbGWo3mJO1ADO6kcjt46F1gbqPYkwBB8Ez9LE7wh2oYSf37obgTgsgk1IAjpq7n9pD8 K+6A== X-Gm-Message-State: ACgBeo3yHN8XiHhHY4HD6bIXGugebgXYDs5bfkvt8s2YronLyTxjRIzr Jv0GZnU5mqxjlEY/zIbhH19hKh6YUgU= X-Google-Smtp-Source: AA6agR5yKeNCnlQO1qCPyK52ww70HZJLlWgT1/q2p5pwB2YtIp3VwcOzeBTGE/6CVfenoQJvr0ZqRa21OmM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:4c8d:b0:1f5:409b:b017 with SMTP id my13-20020a17090b4c8d00b001f5409bb017mr659921pjb.52.1661906110868; Tue, 30 Aug 2022 17:35:10 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:34:48 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-2-seanjc@google.com> Subject: [PATCH 01/19] KVM: SVM: Process ICR on AVIC IPI delivery failure due to invalid target From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Emulate ICR writes on AVIC IPI failures due to invalid targets using the same logic as failures due to invalid types. AVIC acceleration fails if _any_ of the targets are invalid, and crucially VM-Exits before sending IPIs to targets that _are_ valid. In logical mode, the destination is a bitmap, i.e. a single IPI can target multiple logical IDs. Doing nothing causes KVM to drop IPIs if at least one target is valid and at least one target is invalid. Fixes: 18f40c53e10f ("svm: Add VMEXIT handlers for AVIC") Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Reviewed-by: Maxim Levitsky --- arch/x86/kvm/svm/avic.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 6919dee69f18..b1ade555e8d0 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -496,14 +496,18 @@ int avic_incomplete_ipi_interception(struct kvm_vcpu = *vcpu) trace_kvm_avic_incomplete_ipi(vcpu->vcpu_id, icrh, icrl, id, index); =20 switch (id) { + case AVIC_IPI_FAILURE_INVALID_TARGET: case AVIC_IPI_FAILURE_INVALID_INT_TYPE: /* * Emulate IPIs that are not handled by AVIC hardware, which - * only virtualizes Fixed, Edge-Triggered INTRs. The exit is - * a trap, e.g. ICR holds the correct value and RIP has been - * advanced, KVM is responsible only for emulating the IPI. - * Sadly, hardware may sometimes leave the BUSY flag set, in - * which case KVM needs to emulate the ICR write as well in + * only virtualizes Fixed, Edge-Triggered INTRs, and falls over + * if _any_ targets are invalid, e.g. if the logical mode mask + * is a superset of running vCPUs. + * + * The exit is a trap, e.g. ICR holds the correct value and RIP + * has been advanced, KVM is responsible only for emulating the + * IPI. Sadly, hardware may sometimes leave the BUSY flag set, + * in which case KVM needs to emulate the ICR write as well in * order to clear the BUSY flag. */ if (icrl & APIC_ICR_BUSY) @@ -519,8 +523,6 @@ int avic_incomplete_ipi_interception(struct kvm_vcpu *v= cpu) */ avic_kick_target_vcpus(vcpu->kvm, apic, icrl, icrh, index); break; - case AVIC_IPI_FAILURE_INVALID_TARGET: - break; case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE: WARN_ONCE(1, "Invalid backing page\n"); break; --=20 2.37.2.672.g94769d06f0-goog From nobody Tue Apr 7 05:43:29 2026 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 01CC9ECAAD4 for ; Wed, 31 Aug 2022 00:37:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231641AbiHaAhP (ORCPT ); Tue, 30 Aug 2022 20:37:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231846AbiHaAgs (ORCPT ); Tue, 30 Aug 2022 20:36:48 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5F8EA59A8 for ; Tue, 30 Aug 2022 17:35:29 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id f16-20020a17090a4a9000b001f234757bbbso5296470pjh.6 for ; Tue, 30 Aug 2022 17:35:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=tqSFf2c/aR0xKLJBDtSO/L4uHcT204aNH3nZsuo4w3I=; b=KErlEMAFoPTtFRi6Bojf/EJxZz4t2LYfQhsYhAPlw+jdz4eBgRUBZEhO6Hyc41ujO1 sQEGPDQXJ0Bdy/9xAdVxhOjhkikMSI6dEBsVrw+M3bXKGR+EunIcRvOrh3xYizbSJtcG ziV0rcRn6eOA1jcEjUgc8yyP7GSfzC9A2LRJCCK9wHN/Wb1sz3EC5Etu+GJkqNCflwJ2 bEYGOoqjLnJHngLHPQsyfoiaIr2GuihYJM3gouUKIyzLhgdFyTRQdfcOe1+pLIFXcsnl lxfjvcVvnSimrlJfiY73E9do/E0IxHmEsrR6nOU5AWsO66/ijfjeYvAq2c5TFToMWaAQ V4vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=tqSFf2c/aR0xKLJBDtSO/L4uHcT204aNH3nZsuo4w3I=; b=Juga2VkSB6j3qpShpyiOhAfEeXuyd/oKg3pxtsGhYfvow0/yWcioS1Pw9+nz2tWOAV nKWamG8a8LhBuOYNYDSno7qlP02Es6G5bXImo5L7/elH+iCzHHYl3/K4zUYGl2H4BysA vEOutjUIaHjhYefi5GS/KP6luKc+cp71URzU6WTxDLzHPqNDy65pIwC+iCFIlWRNTCtr RRSaR1cSM6IitGTO++FgjH/raV7j5Eky3fxMaJZsVbtPGhkmT2TLlqzkOmFDoxu6FEdm aAhvn8V97emRkfEvyEc46iiiJixyAkEiOamGyMwGknxWMQP2TFi9jqdn+0PwRa7r797Q hhIw== X-Gm-Message-State: ACgBeo0LjEtJNXFy+UaXHCGF/E2UX6WeP0quLPCIdMYGRnLEVnySnaLw Idv8+VuYg5lYk2sC92mmdUPOI2u8Wb8= X-Google-Smtp-Source: AA6agR4NV6ilNxju28gGAtvFvCbwtdHUFc7GAdPfKwMLLiKqvqvTVqp9b9JTlBsJQYJm3qq2uA/+hIhhY2U= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:249:b0:1e0:a8a3:3c6c with SMTP id t9-20020a17090a024900b001e0a8a33c6cmr26559pje.0.1661906112763; Tue, 30 Aug 2022 17:35:12 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:34:49 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-3-seanjc@google.com> Subject: [PATCH 02/19] KVM: SVM: Don't put/load AVIC when setting virtual APIC mode From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the VMCB updates from avic_refresh_apicv_exec_ctrl() into avic_set_virtual_apic_mode() and invert the dependency being said functions to avoid calling avic_vcpu_{load,put}() and avic_set_pi_irte_mode() when "only" setting the virtual APIC mode. avic_set_virtual_apic_mode() is invoked from common x86 with preemption enabled, which makes avic_vcpu_{load,put}() unhappy. Luckily, calling those and updating IRTE stuff is unnecessary as the only reason avic_set_virtual_apic_mode() is called is to handle transitions between xAPIC and x2APIC that don't also toggle APICv activation. And if activation doesn't change, there's no need to fiddle with the physical APIC ID table or update IRTE. The "full" refresh is guaranteed to be called if activation changes in this case as the only call to the "set" path is: kvm_vcpu_update_apicv(vcpu); static_call_cond(kvm_x86_set_virtual_apic_mode)(vcpu); and kvm_vcpu_update_apicv() invokes the refresh if activation changes: if (apic->apicv_active =3D=3D activate) goto out; apic->apicv_active =3D activate; kvm_apic_update_apicv(vcpu); static_call(kvm_x86_refresh_apicv_exec_ctrl)(vcpu); WARNING: CPU: 183 PID: 49186 at arch/x86/kvm/svm/avic.c:1081 avic_vcpu_pu= t+0xde/0xf0 [kvm_amd] CPU: 183 PID: 49186 Comm: stable Tainted: G O 6.0.0-smp--= fcddbca45f0a-sink #34 Hardware name: Google, Inc. Arcadia_IT_80/Arcadia_IT_80, BIOS 10.48.0 01/= 27/2022 RIP: 0010:avic_vcpu_put+0xde/0xf0 [kvm_amd] avic_refresh_apicv_exec_ctrl+0x142/0x1c0 [kvm_amd] avic_set_virtual_apic_mode+0x5a/0x70 [kvm_amd] kvm_lapic_set_base+0x149/0x1a0 [kvm] kvm_set_apic_base+0x8f/0xd0 [kvm] kvm_set_msr_common+0xa3a/0xdc0 [kvm] svm_set_msr+0x364/0x6b0 [kvm_amd] __kvm_set_msr+0xb8/0x1c0 [kvm] kvm_emulate_wrmsr+0x58/0x1d0 [kvm] msr_interception+0x1c/0x30 [kvm_amd] svm_invoke_exit_handler+0x31/0x100 [kvm_amd] svm_handle_exit+0xfc/0x160 [kvm_amd] vcpu_enter_guest+0x21bb/0x23e0 [kvm] vcpu_run+0x92/0x450 [kvm] kvm_arch_vcpu_ioctl_run+0x43e/0x6e0 [kvm] kvm_vcpu_ioctl+0x559/0x620 [kvm] Fixes: 05c4fe8c1bd9 ("KVM: SVM: Refresh AVIC configuration when changing AP= IC mode") Cc: stable@vger.kernel.org Cc: Suravee Suthikulpanit Cc: Maxim Levitsky Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index b1ade555e8d0..f3a74c8284cb 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -741,18 +741,6 @@ void avic_apicv_post_state_restore(struct kvm_vcpu *vc= pu) avic_handle_ldr_update(vcpu); } =20 -void avic_set_virtual_apic_mode(struct kvm_vcpu *vcpu) -{ - if (!lapic_in_kernel(vcpu) || avic_mode =3D=3D AVIC_MODE_NONE) - return; - - if (kvm_get_apic_mode(vcpu) =3D=3D LAPIC_MODE_INVALID) { - WARN_ONCE(true, "Invalid local APIC state (vcpu_id=3D%d)", vcpu->vcpu_id= ); - return; - } - avic_refresh_apicv_exec_ctrl(vcpu); -} - static int avic_set_pi_irte_mode(struct kvm_vcpu *vcpu, bool activate) { int ret =3D 0; @@ -1094,17 +1082,18 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) WRITE_ONCE(*(svm->avic_physical_id_cache), entry); } =20 - -void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) +void avic_set_virtual_apic_mode(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm =3D to_svm(vcpu); struct vmcb *vmcb =3D svm->vmcb01.ptr; - bool activated =3D kvm_vcpu_apicv_active(vcpu); + + if (!lapic_in_kernel(vcpu) || avic_mode =3D=3D AVIC_MODE_NONE) + return; =20 if (!enable_apicv) return; =20 - if (activated) { + if (kvm_vcpu_apicv_active(vcpu)) { /** * During AVIC temporary deactivation, guest could update * APIC ID, DFR and LDR registers, which would not be trapped @@ -1118,6 +1107,16 @@ void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *v= cpu) avic_deactivate_vmcb(svm); } vmcb_mark_dirty(vmcb, VMCB_AVIC); +} + +void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) +{ + bool activated =3D kvm_vcpu_apicv_active(vcpu); + + if (!enable_apicv) + return; + + avic_set_virtual_apic_mode(vcpu); =20 if (activated) avic_vcpu_load(vcpu, vcpu->cpu); --=20 2.37.2.672.g94769d06f0-goog From nobody Tue Apr 7 05:43:29 2026 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 9C1EFECAAD4 for ; Wed, 31 Aug 2022 00:37:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232084AbiHaAhU (ORCPT ); Tue, 30 Aug 2022 20:37:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231886AbiHaAgu (ORCPT ); Tue, 30 Aug 2022 20:36:50 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C161FA6AD5 for ; Tue, 30 Aug 2022 17:35:30 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id y9-20020a17090322c900b00174c881abaeso4607506plg.6 for ; Tue, 30 Aug 2022 17:35:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=nNU9Bjqf9y2XiX7CZGxt5LtQZtYEQ0fRI0M+3kQYHd8=; b=nsUDSLmwiz2YIjGoJDWf0sAGexBLRtBGLNj+U0a0dxwdRSnDUJLZ/s81wWgXfPmfZC rcingM4u7HvJFWTBgWJYoy47ughU9OZmQ69i4DqRXTjNGW5TUGJAtBm24vA2koy20Z+W WFEt7S4TUlpmlrrMirREsdRXHPp3qTniXB67PD3UFg+egTtgi3vDgy/lBQtl6fgp7K4x na1upEaIRDzqldfvv3xrkoqoEluOHkX+4ctoSit8dSJRwikX9HoU6EP+/3rhGXZRoUT0 VfANzt47NFBIqN5xekmNQ7zAiDzyfupSXNkPPIW9bVZcUW6gXapvsysItmRXJxYhCoqs hACQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=nNU9Bjqf9y2XiX7CZGxt5LtQZtYEQ0fRI0M+3kQYHd8=; b=ah0COkVJRMzQ1g0SIeRrqnW89NP2oU0e0rKzuwE+DIGZxgC/nIFmUtRoxmiL8oKtSx VxaLaut5n7tzuiIOx6ZOf19iW4SZ+DaLVAiajPjRBfnW2Iy4XDEJLC4KIdXIz62Ek8R0 x6pu/RZxicopnQkLVNgz2BCAVy0uu4bIpp4m5WUBO7yNb0d/e+moJT0Gx+b9md4a14nS Rg/3vfEStoMblrqEVgpmMvQJ00zUBCVf83EruLZqFYi99RJjGutn7BdOvgImSd3yCX9s GnQ/EFQppUgR5aXbjBg06JDkPeXCBo1wLDY7No5Ar3ujaLHn+xDPDiDMTL6wWzaS8nhq QsCw== X-Gm-Message-State: ACgBeo1K5WbXdMd7ZrOv5xdsNEHEgrfqTNCWiIvI1EKX44EMheIOpEue 8nvnF7nbs/vvJQy1wkKosH6yG4gUDK0= X-Google-Smtp-Source: AA6agR6iq5WY+NgNU9ERXKVNHlEQ6M1HBHY81ehzHMF+0tkMrIik4W+FhStuptJ4GRhrcOxrXoHTiMQFa3I= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:124d:b0:538:295a:1c47 with SMTP id u13-20020a056a00124d00b00538295a1c47mr13357367pfi.5.1661906114650; Tue, 30 Aug 2022 17:35:14 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:34:50 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-4-seanjc@google.com> Subject: [PATCH 03/19] Revert "KVM: SVM: Introduce hybrid-AVIC mode" From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Remove SVM's so called "hybrid-AVIC mode" and reinstate the restriction where AVIC is disabled if x2APIC is enabled. The argument that the "guest is not supposed to access xAPIC mmio when uses x2APIC" is flat out wrong. Activating x2APIC completely disables the xAPIC MMIO region, there is nothing that says the guest must not access that address. Concretely, KVM-Unit-Test's existing "apic" test fails the subtests that expect accesses to the APIC base region to not be emulated when x2APIC is enabled. Furthermore, allowing the guest to trigger MMIO emulation in a mode where KVM doesn't expect such emulation to occur is all kinds of dangerous. Tweak the restriction so that it only inhibits AVIC if x2APIC is actually enabled instead of inhibiting AVIC is x2APIC is exposed to the guest. This reverts commit 0e311d33bfbef86da130674e8528cc23e6acfe16. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 6 ++++++ arch/x86/kvm/svm/avic.c | 21 ++++++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 2c96c43c313a..1f51411f3112 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1128,6 +1128,12 @@ enum kvm_apicv_inhibit { */ APICV_INHIBIT_REASON_PIT_REINJ, =20 + /* + * AVIC is inhibited because the vCPU has x2apic enabled and x2AVIC is + * not supported. + */ + APICV_INHIBIT_REASON_X2APIC, + /* * AVIC is disabled because SEV doesn't support it. */ diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index f3a74c8284cb..1d516d658f9a 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -71,22 +71,12 @@ static void avic_activate_vmcb(struct vcpu_svm *svm) vmcb->control.avic_physical_id &=3D ~AVIC_PHYSICAL_MAX_INDEX_MASK; =20 vmcb->control.int_ctl |=3D AVIC_ENABLE_MASK; - - /* Note: - * KVM can support hybrid-AVIC mode, where KVM emulates x2APIC - * MSR accesses, while interrupt injection to a running vCPU - * can be achieved using AVIC doorbell. The AVIC hardware still - * accelerate MMIO accesses, but this does not cause any harm - * as the guest is not supposed to access xAPIC mmio when uses x2APIC. - */ - if (apic_x2apic_mode(svm->vcpu.arch.apic) && - avic_mode =3D=3D AVIC_MODE_X2) { + if (apic_x2apic_mode(svm->vcpu.arch.apic)) { vmcb->control.int_ctl |=3D X2APIC_MODE_MASK; vmcb->control.avic_physical_id |=3D X2AVIC_MAX_PHYSICAL_ID; /* Disabling MSR intercept for x2APIC registers */ svm_set_x2apic_msr_interception(svm, false); } else { - /* For xAVIC and hybrid-xAVIC modes */ vmcb->control.avic_physical_id |=3D AVIC_MAX_PHYSICAL_ID; /* Enabling MSR intercept for x2APIC registers */ svm_set_x2apic_msr_interception(svm, true); @@ -537,6 +527,14 @@ unsigned long avic_vcpu_get_apicv_inhibit_reasons(stru= ct kvm_vcpu *vcpu) { if (is_guest_mode(vcpu)) return APICV_INHIBIT_REASON_NESTED; + + /* + * AVIC must be disabled if x2AVIC isn't supported and the guest has + * x2APIC enabled. + */ + if (avic_mode !=3D AVIC_MODE_X2 && apic_x2apic_mode(vcpu->arch.apic)) + return APICV_INHIBIT_REASON_X2APIC; + return 0; } =20 @@ -993,6 +991,7 @@ bool avic_check_apicv_inhibit_reasons(enum kvm_apicv_in= hibit reason) BIT(APICV_INHIBIT_REASON_NESTED) | BIT(APICV_INHIBIT_REASON_IRQWIN) | BIT(APICV_INHIBIT_REASON_PIT_REINJ) | + BIT(APICV_INHIBIT_REASON_X2APIC) | BIT(APICV_INHIBIT_REASON_BLOCKIRQ) | BIT(APICV_INHIBIT_REASON_SEV) | BIT(APICV_INHIBIT_REASON_APIC_ID_MODIFIED) | --=20 2.37.2.672.g94769d06f0-goog From nobody Tue Apr 7 05:43:29 2026 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 BE8FAECAAD5 for ; Wed, 31 Aug 2022 00:37:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232131AbiHaAhe (ORCPT ); Tue, 30 Aug 2022 20:37:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231906AbiHaAgx (ORCPT ); Tue, 30 Aug 2022 20:36:53 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DCCEB6403 for ; Tue, 30 Aug 2022 17:35:34 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id v65-20020a626144000000b0052f89472f54so5297323pfb.11 for ; Tue, 30 Aug 2022 17:35:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date; bh=mX7gyqny3Tfi3l5wYUlNtjVwLiCm3GoN1j+pJxyWtuI=; b=aOIR+bnOBR/m2wjECFEj/WCR6Iex06DA6bl6u0ay9A3rH0yaiB3Gwrw0c0JdUAg4pk a5Zsf8lm7I5opc/Fe3Ch4FIhUdzGmApI5POeo/awiCPP+3c1CDeYCz5cVHiCA+1XR8SN 6zUtraj3lDDLRZg47Qt+Mocy8EOhj1WcrJnztogOCLT2ikJjI+LBAPZTFF/K7a01zxO3 tTaHyifeS/YrDdYSTzsvKcek2bAM3k2OxS18qQLZbgwKiU3WVssXUyRN4WvU8mZd4JVX 037n3XcFQtEzkHW99gt6+1a9tYIBe2xv9fdJ3vuPvSLUxgebsTugWcJGmQajg0Xd28rh zAow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date; bh=mX7gyqny3Tfi3l5wYUlNtjVwLiCm3GoN1j+pJxyWtuI=; b=BsT3+6234y7OL7vmxJE2XsN44jisNzMyQrcpdJ/1hvtbJ/VcaDPYovFfElZEzva2Ne 2j1qi2A49981QYHCxa8HVP9s9K70cyt1v8TTvh4QiuubHXTaoQWNWRuLYb85W/o6ltDl 1kt7N2Gg5xDBebMoxxfK4ptuxoVAIFPBWBLSi7MwBPFJ5gKSIIj+fpI04+ZLp1Dl/uZK 2yn7+Ge+sLY6daQXyzIoGLD605J2qg0jz+bilS6XVsTVstGxQUPnYeU9JlX+H3Ch/0Yu Tc82TpuWLUXwGqWLZaCrCoJZaSsC7ZBgm6pDA0tPeeIlzFOLcD7KYeK3D6dtITigpOAC VU9g== X-Gm-Message-State: ACgBeo3IPT1KLj9JKi8Ndls2Si+lZVUZK1qP+bTvqwyuuWPFMa46i4Fr 7Zlhkp3pbD1xUMIiCNEMQf5NF/6NBD0= X-Google-Smtp-Source: AA6agR621EBsP9klM5mRGg0nudFLN7q4PGXuQMjyAE/iNkBpVhHcd2OyCcViTcGilbEelZDISSKI0nrGQFY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:f782:b0:173:1206:cee0 with SMTP id q2-20020a170902f78200b001731206cee0mr23385654pln.130.1661906116336; Tue, 30 Aug 2022 17:35:16 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:34:51 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-5-seanjc@google.com> Subject: [PATCH 04/19] KVM: SVM: Replace "avic_mode" enum with "x2avic_enabled" boolean From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Replace the "avic_mode" enum with a single bool to track whether or not x2AVIC is enabled. KVM already has "apicv_enabled" that tracks if any flavor of AVIC is enabled, i.e. AVIC_MODE_NONE and AVIC_MODE_X1 are redundant and unnecessary noise. No functional change intended. Signed-off-by: Sean Christopherson Reviewed-by: Maxim Levitsky --- arch/x86/kvm/svm/avic.c | 45 +++++++++++++++++++---------------------- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/svm/svm.h | 9 +-------- 3 files changed, 23 insertions(+), 33 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 1d516d658f9a..b59f8ee2671f 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -53,7 +53,7 @@ static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HAS= H_BITS); static u32 next_vm_id =3D 0; static bool next_vm_id_wrapped =3D 0; static DEFINE_SPINLOCK(svm_vm_data_hash_lock); -enum avic_modes avic_mode; +bool x2avic_enabled; =20 /* * This is a wrapper of struct amd_iommu_ir_data. @@ -231,8 +231,8 @@ static u64 *avic_get_physical_id_entry(struct kvm_vcpu = *vcpu, u64 *avic_physical_id_table; struct kvm_svm *kvm_svm =3D to_kvm_svm(vcpu->kvm); =20 - if ((avic_mode =3D=3D AVIC_MODE_X1 && index > AVIC_MAX_PHYSICAL_ID) || - (avic_mode =3D=3D AVIC_MODE_X2 && index > X2AVIC_MAX_PHYSICAL_ID)) + if ((!x2avic_enabled && index > AVIC_MAX_PHYSICAL_ID) || + (index > X2AVIC_MAX_PHYSICAL_ID)) return NULL; =20 avic_physical_id_table =3D page_address(kvm_svm->avic_physical_id_table_p= age); @@ -279,8 +279,8 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) int id =3D vcpu->vcpu_id; struct vcpu_svm *svm =3D to_svm(vcpu); =20 - if ((avic_mode =3D=3D AVIC_MODE_X1 && id > AVIC_MAX_PHYSICAL_ID) || - (avic_mode =3D=3D AVIC_MODE_X2 && id > X2AVIC_MAX_PHYSICAL_ID)) + if ((!x2avic_enabled && id > AVIC_MAX_PHYSICAL_ID) || + (id > X2AVIC_MAX_PHYSICAL_ID)) return -EINVAL; =20 if (!vcpu->arch.apic->regs) @@ -532,7 +532,7 @@ unsigned long avic_vcpu_get_apicv_inhibit_reasons(struc= t kvm_vcpu *vcpu) * AVIC must be disabled if x2AVIC isn't supported and the guest has * x2APIC enabled. */ - if (avic_mode !=3D AVIC_MODE_X2 && apic_x2apic_mode(vcpu->arch.apic)) + if (!x2avic_enabled && apic_x2apic_mode(vcpu->arch.apic)) return APICV_INHIBIT_REASON_X2APIC; =20 return 0; @@ -1086,10 +1086,7 @@ void avic_set_virtual_apic_mode(struct kvm_vcpu *vcp= u) struct vcpu_svm *svm =3D to_svm(vcpu); struct vmcb *vmcb =3D svm->vmcb01.ptr; =20 - if (!lapic_in_kernel(vcpu) || avic_mode =3D=3D AVIC_MODE_NONE) - return; - - if (!enable_apicv) + if (!lapic_in_kernel(vcpu) || !enable_apicv) return; =20 if (kvm_vcpu_apicv_active(vcpu)) { @@ -1165,32 +1162,32 @@ bool avic_hardware_setup(struct kvm_x86_ops *x86_op= s) if (!npt_enabled) return false; =20 + /* AVIC is a prerequisite for x2AVIC. */ + if (!boot_cpu_has(X86_FEATURE_AVIC) && !force_avic) { + if (boot_cpu_has(X86_FEATURE_X2AVIC)) { + pr_warn(FW_BUG "Cannot support x2AVIC due to AVIC is disabled"); + pr_warn(FW_BUG "Try enable AVIC using force_avic option"); + } + return false; + } + if (boot_cpu_has(X86_FEATURE_AVIC)) { - avic_mode =3D AVIC_MODE_X1; pr_info("AVIC enabled\n"); } else if (force_avic) { /* * Some older systems does not advertise AVIC support. * See Revision Guide for specific AMD processor for more detail. */ - avic_mode =3D AVIC_MODE_X1; pr_warn("AVIC is not supported in CPUID but force enabled"); pr_warn("Your system might crash and burn"); } =20 /* AVIC is a prerequisite for x2AVIC. */ - if (boot_cpu_has(X86_FEATURE_X2AVIC)) { - if (avic_mode =3D=3D AVIC_MODE_X1) { - avic_mode =3D AVIC_MODE_X2; - pr_info("x2AVIC enabled\n"); - } else { - pr_warn(FW_BUG "Cannot support x2AVIC due to AVIC is disabled"); - pr_warn(FW_BUG "Try enable AVIC using force_avic option"); - } - } + x2avic_enabled =3D boot_cpu_has(X86_FEATURE_X2AVIC); + if (x2avic_enabled) + pr_info("x2AVIC enabled\n"); =20 - if (avic_mode !=3D AVIC_MODE_NONE) - amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier); + amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier); =20 - return !!avic_mode; + return true; } diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index f3813dbacb9f..b25c89069128 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -821,7 +821,7 @@ void svm_set_x2apic_msr_interception(struct vcpu_svm *s= vm, bool intercept) if (intercept =3D=3D svm->x2avic_msrs_intercepted) return; =20 - if (avic_mode !=3D AVIC_MODE_X2 || + if (!x2avic_enabled || !apic_x2apic_mode(svm->vcpu.arch.apic)) return; =20 diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 6a7686bf6900..cee79ade400a 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -35,14 +35,7 @@ extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly; extern bool npt_enabled; extern int vgif; extern bool intercept_smi; - -enum avic_modes { - AVIC_MODE_NONE =3D 0, - AVIC_MODE_X1, - AVIC_MODE_X2, -}; - -extern enum avic_modes avic_mode; +extern bool x2avic_enabled; =20 /* * Clean bits in VMCB. --=20 2.37.2.672.g94769d06f0-goog From nobody Tue Apr 7 05:43:29 2026 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 9B3BCECAAA1 for ; Wed, 31 Aug 2022 00:37:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231878AbiHaAhJ (ORCPT ); Tue, 30 Aug 2022 20:37:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231873AbiHaAgs (ORCPT ); Tue, 30 Aug 2022 20:36:48 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88640A8950 for ; Tue, 30 Aug 2022 17:35:38 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id v202-20020a6361d3000000b004300a4a30dbso829565pgb.20 for ; Tue, 30 Aug 2022 17:35:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=0HXY0EEA1Ha5zVQSoYds7krJoiBKDZH41KEpFRGKQBk=; b=tJJWk6gHdA3Q/3/ppn1buDVs74SZMkiSxm9h9DAGV4S3Ojwz0SYC89VGuOsrtzS4Oc TAkzd0myieatN1aT5PFhwdSIN8hjqBrxtcL8Ct8Kk66vbmp3qi4Kk9q6AU8UlaMktG6m vWal4FN12VXiLg+Kl8GGR8MMnh5KaZZFamxs+uYum7eQNlir8Y6rZxQDor6AQHSEhCny 3v5VvOJ1apVC4AA5eC8ZviJ9p0WzC/AlTdiUlTVS0a05I69UUew5sNcWBt/Hueu3x+1K yNpnDcKTd3VlEbtLisO5TpzfssAdwvnwqRGOSLWnJ6y6n6c9ZdPsUWa+ZwhbslYgEPnA GTxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=0HXY0EEA1Ha5zVQSoYds7krJoiBKDZH41KEpFRGKQBk=; b=RyI0s8ekjVBlaBL4n3pVazO/uwes/RVOcLzmo8pA2Vekx7oFAQR8qEljUI9TBZygga xApwzVuerrRgDDTNLvUnYul+pnPdBJKUQIa727ngoS4nui1HWysfenDyridmQnxAewox hnAmcmuHldgUQ5WXRxy/KM37sVNa1sSpto8FE4/lB9+6mdw1+gyDiXXKzlJ27fXap32g +nqQNvH4JL0xxmIO+dI2VOS/LIlscR/EvAgfBx0/qvG9WjD29P0E9he/5KPdr7p7y+7U c8PbP4s3QMxpqS0zA96ZwmLrZ2dFu9NcvOJBiz2eVIryeIFamota8NxUSMXHr0YUYsOW IYBw== X-Gm-Message-State: ACgBeo2fj5NUYhSOlWe5ncPMOUkTK0t9B98e1ijkB/uWav3wgsNphq23 ZEaPGHa7BBsRGP2iwaCXTemyFcG6H5E= X-Google-Smtp-Source: AA6agR5nPKcjBevp91WFt9YRwA6+l0+inVdDs1vL1ZnLIyKe5/LKdV7S/tmoziioGqnNYaClWetxEoFjK4g= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:445b:0:b0:42c:7b67:287e with SMTP id t27-20020a63445b000000b0042c7b67287emr6195583pgk.166.1661906118100; Tue, 30 Aug 2022 17:35:18 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:34:52 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-6-seanjc@google.com> Subject: [PATCH 05/19] KVM: SVM: Compute dest based on sender's x2APIC status for AVIC kick From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Compute the destination from ICRH using the sender's x2APIC status, not each (potential) target's x2APIC status. Fixes: c514d3a348ac ("KVM: SVM: Update avic_kick_target_vcpus to support 32= -bit APIC ID") Cc: Li RongQing Signed-off-by: Sean Christopherson Reviewed-by: Li RongQing Reviewed-by: Maxim Levitsky --- arch/x86/kvm/svm/avic.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index b59f8ee2671f..3ace0f2f52f0 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -441,6 +441,7 @@ static int avic_kick_target_vcpus_fast(struct kvm *kvm,= struct kvm_lapic *source static void avic_kick_target_vcpus(struct kvm *kvm, struct kvm_lapic *sour= ce, u32 icrl, u32 icrh, u32 index) { + u32 dest =3D apic_x2apic_mode(source) ? icrh : GET_XAPIC_DEST_FIELD(icrh); unsigned long i; struct kvm_vcpu *vcpu; =20 @@ -456,13 +457,6 @@ static void avic_kick_target_vcpus(struct kvm *kvm, st= ruct kvm_lapic *source, * since entered the guest will have processed pending IRQs at VMRUN. */ kvm_for_each_vcpu(i, vcpu, kvm) { - u32 dest; - - if (apic_x2apic_mode(vcpu->arch.apic)) - dest =3D icrh; - else - dest =3D GET_XAPIC_DEST_FIELD(icrh); - if (kvm_apic_match_dest(vcpu, source, icrl & APIC_SHORT_MASK, dest, icrl & APIC_DEST_MASK)) { vcpu->arch.apic->irr_pending =3D true; --=20 2.37.2.672.g94769d06f0-goog From nobody Tue Apr 7 05:43:29 2026 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 C136AECAAD4 for ; Wed, 31 Aug 2022 00:37:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231931AbiHaAhb (ORCPT ); Tue, 30 Aug 2022 20:37:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231898AbiHaAgx (ORCPT ); Tue, 30 Aug 2022 20:36:53 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 21570A98DE for ; Tue, 30 Aug 2022 17:35:41 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id cj15-20020a056a00298f00b0053a700f1178so1525578pfb.14 for ; Tue, 30 Aug 2022 17:35:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=08A/F7QHFvdlbnKklqADdddOKG9On7dolttTZc9ut/0=; b=CkX2HuJEwn2lrV3MfDjQU7bSjgXyWBqH1zLOhoXEALnjWvgnh9NnoXVN3W8W2Yiabx 8Xh0QTllzSGgDxYS/zmh7UMnqFS8gOaI/RAXjl4mzy0Ejd7hST5Tmd5hnq7dK1vCmlup 5xGMpyQHyV+sZMw8vLXbnTFOtU1DX3moFs6DahywnQfnNozyD3jmeJ/wCV+LUVv9mCLX 1ipFPq78hArYCly/jtVv/Q4NDHnLwdOw4/RLyzHla5Joj2WPCz/xYhvaThEFqxbJ+SSh NjKLj4SrpMc1BAGIgnbRUZedkIsMBCm3T66SZFvstg01GTohs/Y4bOTllL3WP3M6aXXJ k9Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=08A/F7QHFvdlbnKklqADdddOKG9On7dolttTZc9ut/0=; b=HBEfAMPIfYasrUQe8NXypf/uH2uE+4gVws4aMOZasPeLKb5BpDa/W8y6z9FRucuLmc K0aEiskuIqULVBzIBLb1jG0P1dw82UgOkF48bfTFdN0DtY22BQRZcTgLJ/sZnra3oIgJ gbRtG/HMBbLYNxfCN9tU5SpKpckrMFoHvvcLk0AnPRliMQI0iCHbxKpSJCfDHED6gdIE jn123eF4gYAUL/AF2SxC76gQcLeBSADjhRldkKV4BjlIwrY3cWem2oLdH0FTFNS7hSSy yLob0ckNmf/OJvco4/Xgxka2BumTOdCzwBZtlcYSA8vK5yjyIqhFHuLHk0Mx2/gXTnp/ R/kQ== X-Gm-Message-State: ACgBeo1FsvJ4qKM6S3Uts10ElqjCYeNxprrrbQ435uX/MgErOJXJpp8P t/K62grvM044OeEnLtrswd1+Vqey//M= X-Google-Smtp-Source: AA6agR7hbcOraGRxxAHcuZ5giMtcBel+k20eBMC4aqh6XQFuibRrMfc16BSRALwulHnNm7n29kP7LixwI8E= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:aa7:804c:0:b0:536:80bb:8270 with SMTP id y12-20020aa7804c000000b0053680bb8270mr23509612pfm.13.1661906119769; Tue, 30 Aug 2022 17:35:19 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:34:53 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-7-seanjc@google.com> Subject: [PATCH 06/19] KVM: SVM: Get x2APIC logical dest bitmap from ICRH[15:0], not ICHR[31:16] From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When attempting a fast kick for x2AVIC, get the destination bitmap from ICR[15:0], not ICHR[31:16]. The upper 16 bits contain the cluster, the lower 16 bits hold the bitmap. Fixes: 603ccef42ce9 ("KVM: x86: SVM: fix avic_kick_target_vcpus_fast") Cc: Maxim Levitsky Signed-off-by: Sean Christopherson Reviewed-by: Maxim Levitsky --- arch/x86/kvm/svm/avic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 3ace0f2f52f0..3c333cd2e752 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -368,7 +368,7 @@ static int avic_kick_target_vcpus_fast(struct kvm *kvm,= struct kvm_lapic *source =20 if (apic_x2apic_mode(source)) { /* 16 bit dest mask, 16 bit cluster id */ - bitmap =3D dest & 0xFFFF0000; + bitmap =3D dest & 0xFFFF; cluster =3D (dest >> 16) << 4; } else if (kvm_lapic_get_reg(source, APIC_DFR) =3D=3D APIC_DFR_FLAT) { /* 8 bit dest mask*/ --=20 2.37.2.672.g94769d06f0-goog From nobody Tue Apr 7 05:43:29 2026 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 CF254ECAAD4 for ; Wed, 31 Aug 2022 00:37:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231971AbiHaAhi (ORCPT ); Tue, 30 Aug 2022 20:37:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231954AbiHaAg4 (ORCPT ); Tue, 30 Aug 2022 20:36:56 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9942FAA3D3 for ; Tue, 30 Aug 2022 17:35:46 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id r74-20020a632b4d000000b0041bc393913eso6261533pgr.10 for ; Tue, 30 Aug 2022 17:35:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=/40liOollMBH1ZRbQ+xHU8ilIurWaxIwKYg1b454SxY=; b=aZWV7k7C0KKRCKubddCf7VMQ7bDDSvLw+GK5/Mbn2MjsfrZNnShKbKB7/WZF/1d4+g RSS9cgiDfUkGsw9+THoUZXFeDvZG3WUzFqKpkR1es1FzfsDltZfj8VnUjOA/qkwlElKK kCjYrqmwNno3LS1nUXtDEPBlP8D65C7ZxDOlYg2v13ynC5yh2/6U6BSusIfgvHtqCytA CZaTkKk1LzUdIPyq4Qcly6LPmsjBAnsHrK1+Z42tfnAyQA6gGDnxvtBG5L7Kd/WNZaS2 BXAPCd67Krn93Dh380aNgu9fHk5I+vP35KdYhdN/2Hxisnvxm4V2vn6vRQ0ipYApwsMd jYvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=/40liOollMBH1ZRbQ+xHU8ilIurWaxIwKYg1b454SxY=; b=aHiy5z1vQ7+q/WrAJPi2e0Hux4Wieq/XHlo/4DD8DfyqZRG9EiJIuQPHIdgZb5sPEb Ud2i/31VgKHTIx33gHjT0od84NMMWUQArKnvxghyWpgrERj+Dp4cAaeh+69ZAv+Jzi3Y MnY/I/SEWwUGdhJtqFKJiPj9mU9tneJMHEDZVVCJQ2xwutROXRYlUTFb1Z8i4oy9GoI9 gF4iCrkTZYCxqecTpj7EWCqX8Kc4N8BgcYs34XoGvj6n/Z8qzOT7LYPFWZ/b3ymR5oLj J3taX/AgaUUTkZGZIDbtxOrjlujKz01UJinMWDRQPFTLTGbR9qy4WIdF9cvGUgGfnVKz RHaw== X-Gm-Message-State: ACgBeo3uiJl/2KntOyisSGCb19SHyOC4L/R2j7jDLX8ZXP3EJD9JrPSl SihfFN8f3aG5N1Gd/jA3KilEoNlSR1E= X-Google-Smtp-Source: AA6agR73Eip8P/5C6mv8DnUy/A1DO/pS8BJ7VyLRc7c1lmsipWaiHse5sS9+hxiWtTXaWi/MpZvwYHIr0bg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:10cf:b0:528:48c3:79e0 with SMTP id d15-20020a056a0010cf00b0052848c379e0mr23441121pfu.18.1661906121153; Tue, 30 Aug 2022 17:35:21 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:34:54 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-8-seanjc@google.com> Subject: [PATCH 07/19] KVM: SVM: Drop buggy and redundant AVIC "single logical dest" check From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use the already-calculated-and-sanity-checked destination bitmap when processing a fast AVIC kick in logical mode, and drop the logical path's flawed logic. The intent of the check is to ensure the bitmap is a power of two, whereas "icrh !=3D (1 << avic)" effectively checks that the bitmap is a power of two _and_ the target cluster is '0'. Note, the flawed check isn't a functional issue, it simply means that KVM will go down the slow path if the target cluster is non-zero. Fixes: 8c9e639da435 ("KVM: SVM: Use target APIC ID to complete x2AVIC IRQs = when possible") Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 3c333cd2e752..14f567550a1e 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -411,15 +411,7 @@ static int avic_kick_target_vcpus_fast(struct kvm *kvm= , struct kvm_lapic *source * Instead, calculate physical ID from logical ID in ICRH. */ int cluster =3D (icrh & 0xffff0000) >> 16; - int apic =3D ffs(icrh & 0xffff) - 1; - - /* - * If the x2APIC logical ID sub-field (i.e. icrh[15:0]) - * contains anything but a single bit, we cannot use the - * fast path, because it is limited to a single vCPU. - */ - if (apic < 0 || icrh !=3D (1 << apic)) - return -EINVAL; + int apic =3D ffs(bitmap) - 1; =20 l1_physical_id =3D (cluster << 4) + apic; } --=20 2.37.2.672.g94769d06f0-goog From nobody Tue Apr 7 05:43:29 2026 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 DB0B7ECAAA1 for ; Wed, 31 Aug 2022 00:37:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232141AbiHaAhs (ORCPT ); Tue, 30 Aug 2022 20:37:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231811AbiHaAhA (ORCPT ); Tue, 30 Aug 2022 20:37:00 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03D3DAB050 for ; Tue, 30 Aug 2022 17:35:50 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id i191-20020a6387c8000000b0042b44ad723bso6231291pge.19 for ; Tue, 30 Aug 2022 17:35:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=vs+/VgNPbXQDyp+lFpPyuzFJGuQv6/mfWtb6IRoNofc=; b=cvWzuF/pUn7dcLNtyrR5ZfO7DpFQ4TMlPUqGc3ivk0ghfq2DY3XadZH3z7R78K+3e0 maM0SJLY6f2+4rvRYvaFogaghRu2HpktMrb8ohbADFAO0jaO5fy2+oZ9GaLpL48XPz6q uKjZu2+/RlKcBXZhvB+wbKcxzwHv1xJ29PKHH0E+GS/yJ8FzwYKDz9PHxbWVLlokwHJF Wp3CyTZqcZNxxeeD13DanuKngWPcc2ljnts6fPhOKvbEJxBt7Qtp13eNoezrBqjs+viX q1NeXWzk59DSQ5cLYreO3Qjm8NC/iylmjThcNHEvB5HkcO8VAggD/2ojYHMHfwZfNL+1 zWfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=vs+/VgNPbXQDyp+lFpPyuzFJGuQv6/mfWtb6IRoNofc=; b=CRjihmwzg8EYUYizyW1jPkmYkxM3c6CcE2GAwpkla5FPbWcVXI1OibEzDOjksPlz/H Hbco0mmmy4ABpk4FXdvJjulVflnBczIVQPf6JNPpzS+GpolnG2zWJtHssG7wH3AC8JEU dXp+H5JAcppfb0WTAUGFk8pljkk14ldS/mANCeKT6kzMc8Hs1YOrOR+3egNnXsswRBWD aKBSvd7pUOyFRRtW9US5OPqAlj3OTZWAkEmQzEHawIDHpNtrw8cgq+y9svlqlwDAm1Da gTpZ4iIGIOQ6othRwTuOYwkQhw7bkKh3oWw0ITQrINX+LS+ag1aWQbrHXG6XZhVczCyu Fk0g== X-Gm-Message-State: ACgBeo23GlokEaWk9G/Tc335/gPk+xAF58DXVX5zgedCd5GUd37QWPuy ITo0FtGvjAXdKjQ8tz9PvlJCb0dQ3HU= X-Google-Smtp-Source: AA6agR5OjPqtHiqPZMJX6QZ8XK4pECvI+wrTckRQpR97VYnRYJQo0TfwPxQCWcEahmapGJAM35xUkVnM5bc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:cb87:b0:1fd:d261:f87c with SMTP id a7-20020a17090acb8700b001fdd261f87cmr628971pju.43.1661906123392; Tue, 30 Aug 2022 17:35:23 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:34:55 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-9-seanjc@google.com> Subject: [PATCH 08/19] KVM: SVM: Remove redundant cluster calculation that also creates a shadow From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop the redundant "cluster" calculation and its horrific shadowing in the x2avic logical mode path. The "cluster" that is declared at an outer scope is derived using the exact same calculation and has performed the left-shift. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 14f567550a1e..8c9cad96008e 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -410,10 +410,9 @@ static int avic_kick_target_vcpus_fast(struct kvm *kvm= , struct kvm_lapic *source * For x2APIC logical mode, cannot leverage the index. * Instead, calculate physical ID from logical ID in ICRH. */ - int cluster =3D (icrh & 0xffff0000) >> 16; int apic =3D ffs(bitmap) - 1; =20 - l1_physical_id =3D (cluster << 4) + apic; + l1_physical_id =3D cluster + apic; } } =20 --=20 2.37.2.672.g94769d06f0-goog From nobody Tue Apr 7 05:43:29 2026 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 633D9ECAAD5 for ; Wed, 31 Aug 2022 00:38:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232050AbiHaAh6 (ORCPT ); Tue, 30 Aug 2022 20:37:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232035AbiHaAhG (ORCPT ); Tue, 30 Aug 2022 20:37:06 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26422A925D for ; Tue, 30 Aug 2022 17:35:56 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id 15-20020a63020f000000b0041b578f43f9so6274131pgc.11 for ; Tue, 30 Aug 2022 17:35:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=4TG9Pw7tLSyGALSM6JTWSHx8b3lurYVthYrmdEh1p4g=; b=BJ9OXvL27Yj6rdDyCiAw19+HrndK/auzwWJGMQw7nqbI+yAeXdHOSXXSh1EfNSX0bs GrBeIsk/6KGSGmQGSosFyUcK8FQMUORi8sx6axKAT0TEQcAqXBgm/SNdLqstO2E+U+zD Wz/J4KL/BYF7uFXu/tIRMYoe5cF/51L5Gz2bZRRePjmhZV/gilJwvDoePyIiK9GNPXNW XjqArH6J5opNkN7KUq+47sVH/3YH7Iaz95Rzjb6T8BSzTZn7+P7Wm+FZx5+FU5uBAMRp 0E6QpDIyq0qpLQ1nFP9sJMb99YUAePRQtj5gamUj/inEo4QVVSenCoPu1Wie4AodG6hX xBUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=4TG9Pw7tLSyGALSM6JTWSHx8b3lurYVthYrmdEh1p4g=; b=mSSnBw84ayG3KV+5y9ndwh14XaBwHD/KyjFfJ1PFoAS7WCrxvW+VHJA+bXp+5/QlDP HyuXktCaKEVjFJArAXWOAN4V5YvIHXccVYqduDauEB4I8K2lh4g9sQLJ9bGD89FkQlC+ LsOUFZflv2cat7DhyB662O+pcAAXgb3/jVmieuDk/g/6N/6tY+zY8GKHgRA9FEBnEpSa cLoW+VaY32jHns06dlIO3QheCi0JPKFLNoZinIRF7lxuaLnKphskZ2Z+FmdgpKRj0ocV nHV/MQIUfOKCXhHByRXKxWBjGlnFUF7m9KcazXmtsqciAEjAlsgVxgDZf/KJjG350W3/ Ba6w== X-Gm-Message-State: ACgBeo1tvQei45Cp2CduKd2qL+N4WgDbYTMdMYYkFkE+NqOwZL8Oa7pE 9wKNuLxbn06N1tp1gCTkRdJFwCvWJHs= X-Google-Smtp-Source: AA6agR41WRDmT9Kt+fXLTMjq+WmuvHa/9yx5PXfX9AgYEa/iF8hdt5K8Sq67P15XocA2UjnehilOpHT/B8M= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:7b95:b0:172:9dc3:6c12 with SMTP id w21-20020a1709027b9500b001729dc36c12mr23747496pll.94.1661906124977; Tue, 30 Aug 2022 17:35:24 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:34:56 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-10-seanjc@google.com> Subject: [PATCH 09/19] KVM: SVM: Drop duplicate calcuation of AVIC/x2AVIC "logical index" From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop the duplicate calculation of the logical "index", which ends up being the same for x2APIC vs. xAPIC: cluster + bit number. Note, the existing code is a mess and uses ffs(), which is 1-based, and __ffs(), which is 0-based, for the exact same calculation, i.e. replacing "ffs(bitmap) - 1" with "__ffs(bitmap)" is intentional. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 8c9cad96008e..05a1cde8175c 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -407,12 +407,14 @@ static int avic_kick_target_vcpus_fast(struct kvm *kv= m, struct kvm_lapic *source AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK; } else { /* - * For x2APIC logical mode, cannot leverage the index. - * Instead, calculate physical ID from logical ID in ICRH. + * For x2APIC, the logical APIC ID is a read-only value + * that is derived from the x2APIC ID, thus the x2APIC + * ID can be found by reversing the calculation (done + * above). Note, bits 31:20 of the x2APIC ID are not + * propagated to the logical ID, but KVM limits the + * x2APIC ID limited to KVM_MAX_VCPU_IDS. */ - int apic =3D ffs(bitmap) - 1; - - l1_physical_id =3D cluster + apic; + l1_physical_id =3D logid_index; } } =20 --=20 2.37.2.672.g94769d06f0-goog From nobody Tue Apr 7 05:43:29 2026 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 CD0FDECAAD4 for ; Wed, 31 Aug 2022 00:38:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232255AbiHaAiQ (ORCPT ); Tue, 30 Aug 2022 20:38:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229883AbiHaAh3 (ORCPT ); Tue, 30 Aug 2022 20:37:29 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA593AB18F for ; Tue, 30 Aug 2022 17:35:59 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id z14-20020a170903018e00b00174fff57d17so3064588plg.14 for ; Tue, 30 Aug 2022 17:35:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=wFaEqzZozhtqmNn0e7C55y4Mw9837tqb72zXdm7HEXg=; b=m47lnlh0bMmxDPeqAWzcjqHjbiSUGITV4W62KnmVkp+F4sJvmaiZr0PaxYPdNcRASE nfyxSE+Ok0LjqmRY6SHPU55wwO7mJD4DlI6mrWXAR8UZCaYwjMrapALQRt8BG4Yw6xaZ y1/mdec3bxaxkPKLwau1WZIhrjbO83hQK8PnryVscEJuYA6A/BkInijkJKJb38Be4u7u ZVNV3NugVSVZel2HPBphgnE8Tx+oN00qjydm+OLGKmkjA9GU4Poc/plMsVhqIM1SDDUq /ZduO/vVAG1EWinEnK52wdNUkeI7eGcLjIFlwgKbrdJ5v0MR/z3xqd740f7ccQl5D31h Txhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=wFaEqzZozhtqmNn0e7C55y4Mw9837tqb72zXdm7HEXg=; b=cl93LWypCBmrBRJeq47T8/EPZ6vd3X1Xyg58qyBW3igTpHJgh2y1vEc/FT6+XdciG8 liEYN/kf0HwiOi79xnVzgdD+y5WSZ/P+AaxBIvMqQ+xCrWcTsGV6AOsuh0/muIA2Stpu 3X3oYGSDsKFnKUa4v0CuNMZmIt1UXGT+DL2woGEvnxymOTUHqC6pCfDdQ+3f8PPGtZbY +WfyzuFGOuqOGWNPbYyfrpAheja1xwqhTgmdx94cReKJ9WLD3WOmC9FxKDI+6FnncZC+ Nib1+ABqM6g0bEqTf5Qpe+QpwnovxWoeMQhqQ20otHjK2iL8Mhgvch2whuF8HBqbdC0v fMNg== X-Gm-Message-State: ACgBeo1aUDRfQS+KLW8GxLcRKTJ6Pp/MmPOL3e5YRuomVUtRVZ5IYt/K TIiy5lR2uaGRZSigD6n8bTMUzluW5Yw= X-Google-Smtp-Source: AA6agR6LhPKafXpIoPCYpbT1uDPrPxv+h9/n6jWQhQwWZHcFZ4OoDPz98WMIfVahlZOQnfqziyguccqBl2s= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:760c:b0:172:adc5:fc9 with SMTP id k12-20020a170902760c00b00172adc50fc9mr23592405pll.102.1661906126398; Tue, 30 Aug 2022 17:35:26 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:34:57 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-11-seanjc@google.com> Subject: [PATCH 10/19] KVM: SVM: Document that vCPU ID == APIC ID in AVIC kick fastpatch From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Document that AVIC is inhibited if any vCPU's APIC ID diverges from its vCPU ID, i.e. that there's no need to check for a destination match in the AVIC kick fast path. Opportunistically tweak comments to remove "guest bug", as that suggests KVM is punting on error handling, which is not the case. Targeting a non-existent vCPU or no vCPUs _may_ be a guest software bug, but whether or not it's a guest bug is irrelevant. Such behavior is architecturally legal and thus needs to faithfully emulated by KVM (and it is). Signed-off-by: Sean Christopherson Reviewed-by: Maxim Levitsky --- arch/x86/kvm/svm/avic.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 05a1cde8175c..3959d4766911 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -380,8 +380,8 @@ static int avic_kick_target_vcpus_fast(struct kvm *kvm,= struct kvm_lapic *source cluster =3D (dest >> 4) << 2; } =20 + /* Nothing to do if there are no destinations in the cluster. */ if (unlikely(!bitmap)) - /* guest bug: nobody to send the logical interrupt to */ return 0; =20 if (!is_power_of_2(bitmap)) @@ -399,7 +399,7 @@ static int avic_kick_target_vcpus_fast(struct kvm *kvm,= struct kvm_lapic *source if (WARN_ON_ONCE(index !=3D logid_index)) return -EINVAL; =20 - /* guest bug: non existing/reserved logical destination */ + /* Nothing to do if the logical destination is invalid. */ if (unlikely(!(logid_entry & AVIC_LOGICAL_ID_ENTRY_VALID_MASK))) return 0; =20 @@ -418,9 +418,13 @@ static int avic_kick_target_vcpus_fast(struct kvm *kvm= , struct kvm_lapic *source } } =20 + /* + * KVM inhibits AVIC if any vCPU ID diverges from the vCPUs APIC ID, + * i.e. APIC ID =3D=3D vCPU ID. Once again, nothing to do if the target + * vCPU doesn't exist. + */ target_vcpu =3D kvm_get_vcpu_by_id(kvm, l1_physical_id); if (unlikely(!target_vcpu)) - /* guest bug: non existing vCPU is a target of this IPI*/ return 0; =20 target_vcpu->arch.apic->irr_pending =3D true; --=20 2.37.2.672.g94769d06f0-goog From nobody Tue Apr 7 05:43:29 2026 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 EA853C0502A for ; Wed, 31 Aug 2022 00:38:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231886AbiHaAiC (ORCPT ); Tue, 30 Aug 2022 20:38:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232069AbiHaAhJ (ORCPT ); Tue, 30 Aug 2022 20:37:09 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2151AB1A6 for ; Tue, 30 Aug 2022 17:36:01 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id rj3-20020a17090b3e8300b001fae0be4dc4so11947405pjb.7 for ; Tue, 30 Aug 2022 17:36:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=+YMlCYc3ExFhBvYYADHjoE7ZsXqjL3nmZXjZ12ZiBNw=; b=sLNACwIJjMr1ibfCQ93pmBhaT79sm158HWrLH3PRFrFvzygtTWVigpI2OlQRDw5QXN MO8uT4BKABOGwX5vGOO7BzoHeg9gQfVjB3OJ1uvfMPL3H4SKJPw1b6yW3Fh5jY1WUgVf BrLOzeUWMB7f5TGHjJZ6bOwp0ev/MiX7H841oomE+B8NatX77H99VbBLNTEJ+krbs6iT ANxXOacClU8ppUO6qCOnpsLDNgn1dcFvH2ePriX7+itvU0eiNcK/u8eirQmOr+DsGvJm 1SLH0/D83wpAQSE6opsCLSpuRK967tzQZ+PYFLK7IUA/T1Qopj9L1BBFJIQy0UqmLW2A ovIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=+YMlCYc3ExFhBvYYADHjoE7ZsXqjL3nmZXjZ12ZiBNw=; b=Dac+Bh6acKsXrmfeY2hfs26kF+dwRMoFdYxmJfUl6elfcFJOAAmIG/hfKf5h+nuJPc 3uE4ZTk8D3iSYCoCGCzYVSIavzL0/zeUoXg3DKafqRL+8VjnX+kor2A2UT93AOLHAOl0 iv07zKKu1tOqG1dqVBr4eftAqNAWMHyDJnH8k6UERDAKMHU/DoL5xmnWASyQ+GJysPj4 6Z9fYqs8zyUkKC5yLL46UlTgII2Tw5wKbBfCPu5Ead8mdng9jf3b0Eh4UNRBEEZqZLlL XpdS4uEKmb38F+1MWWMoeiCJ6h6h/I4Nf2AExpw3U3FFm52+jSnphPOyUP7/TdYlzXSY La0Q== X-Gm-Message-State: ACgBeo1PZpCgM5gBLjVooYqTFU9I4a9vjuutrqBlI4a9hKse57B8TobL UTw8+tHn/c0ppYS05qdeX30SoldVk90= X-Google-Smtp-Source: AA6agR6TMGjcK254MJkJTONU/65Ct6ilW9/zfFyM5frT68x5QoUsxITnrV6TghN0fRA891jz32a/NaI1yxQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:aa7:9242:0:b0:536:f215:4f4f with SMTP id 2-20020aa79242000000b00536f2154f4fmr23764791pfp.45.1661906127770; Tue, 30 Aug 2022 17:35:27 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:34:58 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-12-seanjc@google.com> Subject: [PATCH 11/19] KVM: SVM: Add helper to perform final AVIC "kick" of single vCPU From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a helper to perform the final kick, two instances of the ICR decoding is one too many. No functional change intended. Signed-off-by: Sean Christopherson Reviewed-by: Maxim Levitsky --- arch/x86/kvm/svm/avic.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 3959d4766911..2095ece70712 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -329,6 +329,16 @@ void avic_ring_doorbell(struct kvm_vcpu *vcpu) put_cpu(); } =20 + +static void avic_kick_vcpu(struct kvm_vcpu *vcpu, u32 icrl) +{ + vcpu->arch.apic->irr_pending =3D true; + svm_complete_interrupt_delivery(vcpu, + icrl & APIC_MODE_MASK, + icrl & APIC_INT_LEVELTRIG, + icrl & APIC_VECTOR_MASK); +} + /* * A fast-path version of avic_kick_target_vcpus(), which attempts to match * destination APIC ID to vCPU without looping through all vCPUs. @@ -427,11 +437,7 @@ static int avic_kick_target_vcpus_fast(struct kvm *kvm= , struct kvm_lapic *source if (unlikely(!target_vcpu)) return 0; =20 - target_vcpu->arch.apic->irr_pending =3D true; - svm_complete_interrupt_delivery(target_vcpu, - icrl & APIC_MODE_MASK, - icrl & APIC_INT_LEVELTRIG, - icrl & APIC_VECTOR_MASK); + avic_kick_vcpu(target_vcpu, icrl); return 0; } =20 @@ -455,13 +461,8 @@ static void avic_kick_target_vcpus(struct kvm *kvm, st= ruct kvm_lapic *source, */ kvm_for_each_vcpu(i, vcpu, kvm) { if (kvm_apic_match_dest(vcpu, source, icrl & APIC_SHORT_MASK, - dest, icrl & APIC_DEST_MASK)) { - vcpu->arch.apic->irr_pending =3D true; - svm_complete_interrupt_delivery(vcpu, - icrl & APIC_MODE_MASK, - icrl & APIC_INT_LEVELTRIG, - icrl & APIC_VECTOR_MASK); - } + dest, icrl & APIC_DEST_MASK)) + avic_kick_vcpu(vcpu, icrl); } } =20 --=20 2.37.2.672.g94769d06f0-goog From nobody Tue Apr 7 05:43:29 2026 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 CCF09ECAAD4 for ; Wed, 31 Aug 2022 00:38:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232305AbiHaAic (ORCPT ); Tue, 30 Aug 2022 20:38:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231975AbiHaAhm (ORCPT ); Tue, 30 Aug 2022 20:37:42 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 842B6AA3FA for ; Tue, 30 Aug 2022 17:36:03 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id s129-20020a625e87000000b005383388a6bcso3117331pfb.2 for ; Tue, 30 Aug 2022 17:36:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=rkBfsy5vecrqHl4IR9aqERIJtU+S3QBzoIPSLUPWWcQ=; b=osChJHQCjyEjzZh+u4eXClqsGeReKzrEX47qOzWatIMtwD1ntPeaDvlnJXz+JRgg0Y hlsrXPhUAZfKdTSclu9qwojxkwN+IMSy2hdu2SD9S7ORQDbkFlds6CHb4gJkhBQPApcF AmIollHjpJl9UwKBxRzUQ1nwzgJ02lQn568OL0boEXQjeNE2oAauPJxderHfcuC7P5Ap MVTdDFeC1JCaQnKq5ijQrIRDC5SKzphoVzQCosh3kguSpVG/vTuI5LX5gR55F21cnQa0 Zu24Rchbnw4eJi5I1noq7AJ+ED43KvyNzYgGW10zSOxP/5frwNXsgQHiNPiYh6hm+LFh An5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=rkBfsy5vecrqHl4IR9aqERIJtU+S3QBzoIPSLUPWWcQ=; b=baBMmZ+pTQ/9NVgVeZMr+4CV/OPg7QMzZJWnCvADuXiHqp+1z9so3B8GfqZbq5lHq9 axdQf32bBFXqZ1IrD7AHb+4okEJIp4RZe7GyTb2XAlncobdtnFBjKEGB37XE9MgsMGdl RKs4viTZNijzKIjG+z2bHE6HXcHjCApRJ7MWfZgJ6G0fyS4/8v3Ac3ee9VRn1vxJtUqU iWrKhgSXSDZ73uQsX3AreIFgAONe+A07RivVtlo73OkoQ96ao8eq+yDMkIMqFt4d1/Fm Cn7cZEK5CIKmt1oUcvcVfPeowZNsHDuwnDIArRw9Kv6tdK01ck3X26y20mFfz7Oz2wYe HSDA== X-Gm-Message-State: ACgBeo04C9kFyttmfsJgxG9Qts3szL8SHCh4lQ0238az+PQRNWX3rgwz ZX1MYiY/kBc2fXw7vxa3i4Eq5EGkvq4= X-Google-Smtp-Source: AA6agR42zIyRkCvEj3ckgNwXaZ7y80vJrHd+DtX8xLAytvxHt7YF3iajhVwjSTrnJwHXW5NikzyVP9pxwIg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:d2c1:b0:172:c1af:fd26 with SMTP id n1-20020a170902d2c100b00172c1affd26mr23311620plc.4.1661906129448; Tue, 30 Aug 2022 17:35:29 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:34:59 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-13-seanjc@google.com> Subject: [PATCH 12/19] KVM: x86: Disable APIC logical map if logical ID covers multiple MDAs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Disable the optimized APIC logical map if a logical ID covers multiple MDAs, i.e. if a vCPU has multiple bits set in its ID. In logical mode, events match if "ID & MDA !=3D 0", i.e. creating an entry for only the first bit can cause interrupts to be missed. Note, creating an entry for every bit is also wrong as KVM would generate IPIs for every matching bit. It would be possible to teach KVM to play nice with this edge case, but it is very much an edge case and probably not used in any real world OS, i.e. it's not worth optimizing. Use an impossible value for the "mode" to effectively designate that it's disabled. Don't bother adding a dedicated "invalid" value, the mode handling will be cleaned up in the future and it would take just as much effort to explain what value is "safe" at this time. Fixes: 1e08ec4a130e ("KVM: optimize apic interrupt delivery") Signed-off-by: Sean Christopherson Reviewed-by: Maxim Levitsky --- arch/x86/kvm/lapic.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 9dda989a1cf0..82278acae95b 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -300,8 +300,15 @@ void kvm_recalculate_apic_map(struct kvm *kvm) if (!kvm_apic_map_get_logical_dest(new, ldr, &cluster, &mask)) continue; =20 - if (mask) - cluster[ffs(mask) - 1] =3D apic; + if (!mask) + continue; + + if (!is_power_of_2(mask)) { + new->mode =3D KVM_APIC_MODE_XAPIC_FLAT | + KVM_APIC_MODE_XAPIC_CLUSTER; + continue; + } + cluster[ffs(mask) - 1] =3D apic; } out: old =3D rcu_dereference_protected(kvm->arch.apic_map, --=20 2.37.2.672.g94769d06f0-goog From nobody Tue Apr 7 05:43:29 2026 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 6FC31ECAAD5 for ; Wed, 31 Aug 2022 00:38:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232233AbiHaAiG (ORCPT ); Tue, 30 Aug 2022 20:38:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231604AbiHaAhO (ORCPT ); Tue, 30 Aug 2022 20:37:14 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44429AB1BF for ; Tue, 30 Aug 2022 17:36:03 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id 14-20020a056a00072e00b0053689e4d0e5so5206526pfm.5 for ; Tue, 30 Aug 2022 17:36:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=jKorNpXhvwAGsXROivEBSj6hwWRnB+Rxe5sOrtAqphg=; b=tIU13tu3TrRZIwVjv3NBqNY45MhfLObBL6gThmPiOhUD6+etBUdAZjTHH2Bd9lYATB mm3B0SIcYf9WsoH4X+DNkzgG0ymiFjKf1dhwKuhKL7+DbEf1731yMvENCFd8Y2rOCqLX 0NfwwTsjoiy6K40RaNI/f3p/JN1vVV9LgEN7ez756T3aQvTU/ln2o4jBeW8T/dteao5w lELNQ1P6djW8X4RFC1e/w1a+VZ6BI1RA6yX2YixQqGUyYIpXwRgH9G27UQNl2EFMPGRB MQ4noN2nDGKNGasEYXlxnYPZh7xlR/0dImt5IH7XdJWUg4VEgZMYS9sXuO5nMQYQIzan nD4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=jKorNpXhvwAGsXROivEBSj6hwWRnB+Rxe5sOrtAqphg=; b=HOEwaigQQCMFKM8CiEAEbEEKo/ckaq5FcJpbDsmIhf0J5Vdyz+/dv0dc0XMxCSURdz AHEh1nuvLfaw0H5G+fPFtdEQCAGNQ63KNwM9jq4/GDH8OaupKjccnXR3okdWGoBbhtwR Out1ogl4vYNxb8KMs9h8ovlXlb/qYvQNyIAJ/8q4BChnlMG/4ZvJ6WpOf4bigJKW1OfR pJerB8w07NRzWygt4QEhepGAYwxtbZRqY1lflUbgZFj5BKzNgAAuriJ7nZwpMvavqz45 9fOChGgYYKmhLvxeC+j8TuD9dgD7WxgGBOuhjpvoQirfMkzBBUK6viWo7eKc+RM4CIHM zUVQ== X-Gm-Message-State: ACgBeo3CUI6TMdw//8oARwilKtEEFUu8blw0O/7w9zqJpJQQ+tQRWt/4 Hm6dFAN/fjXRbouVSxi1neWiJNCmdV4= X-Google-Smtp-Source: AA6agR7yN+Sp/Y+nZ4AuoglDHrI+LXd3f1PmIguhTRo3PXqVoSa11JZpxTjMBfll0T0+/fbcDbejWHARNus= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:4b0c:b0:1f4:ffac:699a with SMTP id lx12-20020a17090b4b0c00b001f4ffac699amr600872pjb.145.1661906131231; Tue, 30 Aug 2022 17:35:31 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:35:00 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-14-seanjc@google.com> Subject: [PATCH 13/19] KVM: x86: Disable APIC logical map if vCPUs are aliased in logical mode From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Disable the optimized APIC logical map if multiple vCPUs are aliased to the same logical ID. Architecturally, all CPUs whose logical ID matches the MDA are supposed to receive the interrupt; overwriting existing map entries can result in missed IPIs. Fixes: 1e08ec4a130e ("KVM: optimize apic interrupt delivery") Signed-off-by: Sean Christopherson Reviewed-by: Maxim Levitsky --- arch/x86/kvm/lapic.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 82278acae95b..d537b51295d6 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -303,12 +303,13 @@ void kvm_recalculate_apic_map(struct kvm *kvm) if (!mask) continue; =20 - if (!is_power_of_2(mask)) { + ldr =3D ffs(mask) - 1; + if (!is_power_of_2(mask) || cluster[ldr]) { new->mode =3D KVM_APIC_MODE_XAPIC_FLAT | KVM_APIC_MODE_XAPIC_CLUSTER; continue; } - cluster[ffs(mask) - 1] =3D apic; + cluster[ldr] =3D apic; } out: old =3D rcu_dereference_protected(kvm->arch.apic_map, --=20 2.37.2.672.g94769d06f0-goog From nobody Tue Apr 7 05:43:29 2026 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 A7E14ECAAA1 for ; Wed, 31 Aug 2022 00:38:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232134AbiHaAiM (ORCPT ); Tue, 30 Aug 2022 20:38:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232105AbiHaAh3 (ORCPT ); Tue, 30 Aug 2022 20:37:29 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 598B2AA34A for ; Tue, 30 Aug 2022 17:36:07 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-33dce8cae71so191091337b3.8 for ; Tue, 30 Aug 2022 17:36:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=QaLgmkEnN9+A5u4GUHQI07FnvwFD/IlymWseYRdNvp0=; b=DHgj3KslBJ8pdFPLfnvf3nzZmARiy57oq095IOiAFVbcjACBqCtX15rLMCa7C9e4Yb KI3B+6AeK9aBgC9Z4ImMJ1cI3LYCYmo16HFsSxTNRtJk1fzKa9vOQ9QlXGfUP4Cfdeke +EiyiHFwROBxPPLVr2EI8oO7bDOwccTfQcg/TXaLxN9Kx6+dEr8gQsbU7H0pn72Z68K4 HoHN4oRs/b+VodKla3vPTRvdNawq65Ph5s50mDtlc/AgAd+BH6Yzlwch7juIejCf6Up8 e0qHBEIjIMUn1T32Y9eoLpj9mGR3hsUpwx9CPjq5g/a+o/CV+9eej30H6cRt9pQFKupZ 1YwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=QaLgmkEnN9+A5u4GUHQI07FnvwFD/IlymWseYRdNvp0=; b=ycUCkGmKE5CrIugP/kglz4LYDD9EWGv66vBGHhRLOeCpUtnpSKKhMaPfAcgkPVAA/I eo3qwAPkXRDrKyY85R5ZzsIDLhTppBK8rxCybw2EH508GmASKCtp8fPqPEkpbXoD62vu m2ReINAIA/4AXaJQiJ6snIyHjFXse9njbURYSPQZbifM26miPDmRuRNpfQZ/M7rOX7I3 G+n3b4MWuWzwvoj/Ni85sj53xDPV/QbBRRuIVxnnoDow5QKRSMSQWA3uE+OxvqI0Dp9Y JNTy5FTILo6M1sAWCDV+zARhxATEHGnEcx1iQxSt9ghmJ4fcU7JhUVQJvQsxuWif73oM gmWA== X-Gm-Message-State: ACgBeo3M7avPy8frdOC+RU4U59SgDELYpWSNXY8PtOxHzQEFIqZBXx8L XhSYObdVR2/ttANHDIoGXYdfZJ5USGM= X-Google-Smtp-Source: AA6agR6nw3/aZ8dH+24DR3hV06JClWeERaJaQicAEgle3ggPZPi3w/g2OMCS3BoVYP+RPmk62KQ5lAIrWRQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a0d:c981:0:b0:330:dc03:7387 with SMTP id l123-20020a0dc981000000b00330dc037387mr16390494ywd.380.1661906132855; Tue, 30 Aug 2022 17:35:32 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:35:01 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-15-seanjc@google.com> Subject: [PATCH 14/19] KVM: x86: Honor architectural behavior for aliased 8-bit APIC IDs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Apply KVM's hotplug hack if and only if userspace has enabled 32-bit IDs for x2APIC. If 32-bit IDs are not enabled, disable the optimized map to honor x86 architectural behavior if multiple vCPUs shared a physical APIC ID. As called out in the changelog that added the hack, all CPUs whose (possibly truncated) APIC ID matches the target are supposed to receive the IPI. KVM intentionally differs from real hardware, because real hardware (Knights Landing) does just "x2apic_id & 0xff" to decide whether to accept the interrupt in xAPIC mode and it can deliver one interrupt to more than one physical destination, e.g. 0x123 to 0x123 and 0x23. Applying the hack even when x2APIC is not fully enabled means KVM doesn't correctly handle scenarios where the guest has aliased xAPIC IDs across multiple vCPUs, as only the vCPU with the lowest vCPU ID will receive any interrupts. It's extremely unlikely any real world guest aliase APIC IDs, or even modifies APIC IDs, but KVM's behavior is arbitrary, e.g. the lowest vCPU ID "wins" regardless of which vCPU is "aliasing" and which vCPU is "normal". Furthermore, the hack is _not_ guaranteed to work! The hack works if and only if the optimized APIC map is successfully allocated. If the map allocation fails (unlikely), KVM will fall back to its unoptimized behavior, which _does_ honor the architectural behavior. Pivot on 32-bit x2APIC IDs being enabled as that is required to take advantage of the hotplug hack (see kvm_apic_state_fixup()), i.e. won't break existing setups unless they are way, way off in the weeds. And an entry in KVM's errata to document the hack. Alternatively, KVM could provide an actual x2APIC quirk and document the hack that way, but there's unlikely to ever be a use case for disabling the quirk. Go the errata route to avoid having to validate a quirk no one cares about. Fixes: 5bd5db385b3e ("KVM: x86: allow hotplug of VCPU with APIC ID over 0xf= f") Signed-off-by: Sean Christopherson --- Documentation/virt/kvm/x86/errata.rst | 11 ++++++ arch/x86/kvm/lapic.c | 50 ++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/Documentation/virt/kvm/x86/errata.rst b/Documentation/virt/kvm= /x86/errata.rst index 410e0aa63493..49a05f24747b 100644 --- a/Documentation/virt/kvm/x86/errata.rst +++ b/Documentation/virt/kvm/x86/errata.rst @@ -37,3 +37,14 @@ Nested virtualization features ------------------------------ =20 TBD + +x2APIC +------ +When KVM_X2APIC_API_USE_32BIT_IDS is enabled, KVM activates a hack/quirk t= hat +allows sending events to a single vCPU using its x2APIC ID even if the tar= get +vCPU has legacy xAPIC enabled, e.g. to bring up hotplugged vCPUs via INIT-= SIPI +on VMs with > 255 vCPUs. A side effect of the quirk is that, if multiple = vCPUs +have the same physical APIC ID, KVM will deliver events targeting that API= C ID +only to the vCPU with the lowest vCPU ID. If KVM_X2APIC_API_USE_32BIT_IDS= is +not enabled, KVM follows x86 architecture when processing interrupts (all = vCPUs +matching the target APIC ID receive the interrupt). diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index d537b51295d6..c224b5c7cd92 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -260,10 +260,10 @@ void kvm_recalculate_apic_map(struct kvm *kvm) kvm_for_each_vcpu(i, vcpu, kvm) { struct kvm_lapic *apic =3D vcpu->arch.apic; struct kvm_lapic **cluster; + u32 x2apic_id, physical_id; u16 mask; u32 ldr; u8 xapic_id; - u32 x2apic_id; =20 if (!kvm_apic_present(vcpu)) continue; @@ -271,16 +271,48 @@ void kvm_recalculate_apic_map(struct kvm *kvm) xapic_id =3D kvm_xapic_id(apic); x2apic_id =3D kvm_x2apic_id(apic); =20 - /* Hotplug hack: see kvm_apic_match_physical_addr(), ... */ - if ((apic_x2apic_mode(apic) || x2apic_id > 0xff) && - x2apic_id <=3D new->max_apic_id) - new->phys_map[x2apic_id] =3D apic; /* - * ... xAPIC ID of VCPUs with APIC ID > 0xff will wrap-around, - * prevent them from masking VCPUs with APIC ID <=3D 0xff. + * Apply KVM's hotplug hack if userspace has enable 32-bit APIC + * IDs. Allow sending events to vCPUs by their x2APIC ID even + * if the target vCPU is in legacy xAPIC mode, and silently + * ignore aliased xAPIC IDs (the x2APIC ID is truncated to 8 + * bits, causing IDs > 0xff to wrap and collide). + * + * Honor the architectural (and KVM's non-optimized) behavior + * if userspace has not enabled 32-bit x2APIC IDs. Each APIC + * is supposed to process messages independently. If multiple + * vCPUs have the same effective APIC ID, e.g. due to the + * x2APIC wrap or because the guest manually modified its xAPIC + * IDs, events targeting that ID are supposed to be recognized + * by all vCPUs with said ID. */ - if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id]) - new->phys_map[xapic_id] =3D apic; + if (kvm->arch.x2apic_format) { + /* See also kvm_apic_match_physical_addr(). */ + if ((apic_x2apic_mode(apic) || x2apic_id > 0xff) && + x2apic_id <=3D new->max_apic_id) + new->phys_map[x2apic_id] =3D apic; + + if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id]) + new->phys_map[xapic_id] =3D apic; + } else { + /* + * Disable the optimized map if the physical APIC ID is + * already mapped, i.e. is aliased to multiple vCPUs. + * The optimized map requires a strict 1:1 mapping + * between IDs and vCPUs. + */ + if (apic_x2apic_mode(apic)) + physical_id =3D x2apic_id; + else + physical_id =3D xapic_id; + + if (new->phys_map[physical_id]) { + kvfree(new); + new =3D NULL; + goto out; + } + new->phys_map[physical_id] =3D apic; + } =20 if (!kvm_apic_sw_enabled(apic)) continue; --=20 2.37.2.672.g94769d06f0-goog From nobody Tue Apr 7 05:43:29 2026 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 3A658ECAAD5 for ; Wed, 31 Aug 2022 00:38:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231811AbiHaAi2 (ORCPT ); Tue, 30 Aug 2022 20:38:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232135AbiHaAhe (ORCPT ); Tue, 30 Aug 2022 20:37:34 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64628AC268 for ; Tue, 30 Aug 2022 17:36:17 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id bu13-20020a056902090d00b00671743601f1so968633ybb.0 for ; Tue, 30 Aug 2022 17:36:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=mC7usvSs3qaM/1NjRUjnPt+Y8usO4FtgvRtSkKXmhAg=; b=UjASSDAxTG4PE1j3D0JlQ0/fdhz4Hk48LTfPjtmcsIEHn2woxsrAql9G7dVL6V/Mjw 32JZ0PjptfKpCTHBOMjdUtEi0Wubx6ju2M5wFAeZwxyp4mRCJwYPOayd7xwJfjxVWPaO Ib9mZir94VBaEEMnw4ZIijzxODYF8bxr3U8UUqEm9pvNXNmhC8mBmOYuWwTxeliPCoWM i2mq9WA8ijvCJQeK494hAX0pVi/Eq4ZALx1a+5KapA6N4UAQQ4naz3Kd4OkH2SbCQcT2 w30EL8JvpniYrH2eee5HmU5YIfGtik9oWGxzlN2qJGLku1+QIW5+8iGDfTzz2FUzrq3L vZuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=mC7usvSs3qaM/1NjRUjnPt+Y8usO4FtgvRtSkKXmhAg=; b=61K58Fte2chsRkouobkilKpXVVrKCzonrvlbffuKhVk3R2vSYWuQcSHDJW8T7icf3h P/R5W0zeke35qasED2+gYGKlc9425egsJPFavBBJhNVTVzEC5cLyIuC8OQYyNCO8ZGJr L0Qsm7+KTZnotrDL7J0Wc1audDvQR6I8AdiBTKKhpDguf/o5D87ad4gu0kRirSyMEsIx IQUurdv6yKT9LPxEjhR6q0Uy6REFxKLL0vfJETj5PhnlEyredtNs0TPQZitKClpQtn9P 8vyOl/jR4QxVBWEX8zC/kHJnlatZsdylL/+/I0tGMOHBlxLSkrlwf8w95/IenEhAOULX 4e+A== X-Gm-Message-State: ACgBeo0YGltsOWgh9tSqVES6KP3zw8TpgJ+vq/AnX8Dua53xvrtLLwC9 V+RWlzMqolPjkIkNA14QjJMUQfjhzrU= X-Google-Smtp-Source: AA6agR6pODNwn0E6mm4IQc6Uil8iSxn+UpHt2TrVO6ngOsFdoeXGC83cnibjIXnoJytq16bQaCNEDEuvN9Y= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:54c4:0:b0:329:d0e1:cfcf with SMTP id i187-20020a8154c4000000b00329d0e1cfcfmr16162287ywb.451.1661906134392; Tue, 30 Aug 2022 17:35:34 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:35:02 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-16-seanjc@google.com> Subject: [PATCH 15/19] KVM: x86: Explicitly skip optimized logical map setup if vCPU's LDR==0 From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Explicitly skip the optimized map setup if the vCPU's LDR is '0', i.e. if the vCPU will never response to logical mode interrupts. KVM already skips setup in this case, but relies on kvm_apic_map_get_logical_dest() to generate mask=3D=3D0. KVM still needs the mask=3D0 check as a non-zero = LDR can yield mask=3D=3D0 depending on the mode, but explicitly handling the LDR will make it simpler to clean up the logical mode tracking in the future. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index c224b5c7cd92..8209caffe3ab 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -318,10 +318,12 @@ void kvm_recalculate_apic_map(struct kvm *kvm) continue; =20 ldr =3D kvm_lapic_get_reg(apic, APIC_LDR); + if (!ldr) + continue; =20 if (apic_x2apic_mode(apic)) { new->mode |=3D KVM_APIC_MODE_X2APIC; - } else if (ldr) { + } else { ldr =3D GET_APIC_LOGICAL_ID(ldr); if (kvm_lapic_get_reg(apic, APIC_DFR) =3D=3D APIC_DFR_FLAT) new->mode |=3D KVM_APIC_MODE_XAPIC_FLAT; --=20 2.37.2.672.g94769d06f0-goog From nobody Tue Apr 7 05:43:29 2026 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 5A4CAECAAD5 for ; Wed, 31 Aug 2022 00:38:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232273AbiHaAiY (ORCPT ); Tue, 30 Aug 2022 20:38:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231933AbiHaAhb (ORCPT ); Tue, 30 Aug 2022 20:37:31 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BCB4ADCCB for ; Tue, 30 Aug 2022 17:36:20 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id r6-20020a17090a2e8600b001fbb51e5cc1so9844302pjd.5 for ; Tue, 30 Aug 2022 17:36:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=FEhIkuDQLTbhRgVY6gxGRT46rdlXqm8xEd2WlujPlPw=; b=C3Jvhnth10yD5RbGbE4OnTHfMpzbKLPlep3eopnVwcvyjqfctzeThj//Ph1qD1y8OL qw9TQyRwuTRnJXYvCX5ukpbnFxwQXyGSq0w9Fq1YTGnr1ByLfMK1WGZCElQXQ5tTbfY9 Y9NJhVw2GAp2N6Rr9q6qG3KglgOR0ryWSCkgDM60t4x31chuG5aamaHUMO5M1GV5GoZe QPnlXa08Wa7rWD60+Uu5j9e6SbcRdHSx2LC+RYWgXmLMtJ0FoSsYVhlOUf5xSIX5BbRR DOznjFBqs8f6sEjF+RO2Rew5mO43wTBsRkbqcRrfuYSf+WryThV+E4muiRE628KnhcRV fjZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=FEhIkuDQLTbhRgVY6gxGRT46rdlXqm8xEd2WlujPlPw=; b=tZ90cxVIYW7BFGDLDQUkQDW5Gmi+te7745FHCv9E2onzrdNYMNqpJowhsxzSWIwC2G dIxmF1YxXW1rDGp2+1b3nXXRp9CIi+GzQr0Ivp7c+Zc6+CGew9iyEkKighyzkIMLQORv tI3awTNTWE3UCPz7fCqd6wzjapAvAgs2eaPGa8/O6WQOAngyOiRQ5gjDPKfDEP3mzS/r bGh68SwhHFpEZHil45QV0rnZXaziGlPI2xDgWsEOLuTIS5g7+5tNJMVVr6OvER65rQrx 0ZRsO02wEjydigLY4kxjqtLw2ixo/c2px8hko9sEtHftolpbeBZmE0HKe/tZ4gV2ZpPU sIsg== X-Gm-Message-State: ACgBeo127Xy8H3RMOawUCG330j+tqz7iH4yK/BaAh3Uh+jFdVLAN8TJV dsJW20MuPqdurkbTPMok1PAGwKWEfUI= X-Google-Smtp-Source: AA6agR6TzP+FIywrqeQqCZzSFVPsDmgyLFfh2V3lEmwUZq63KVkLk4QrRpb+1AKCQPIZKXEtbItpe/uflhI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a62:168a:0:b0:535:ff3d:b68d with SMTP id 132-20020a62168a000000b00535ff3db68dmr23683340pfw.86.1661906136120; Tue, 30 Aug 2022 17:35:36 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:35:03 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-17-seanjc@google.com> Subject: [PATCH 16/19] KVM: x86: Explicitly track all possibilities for APIC map's logical modes From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Track all possibilities for the optimized APIC map's logical modes instead of overloading the pseudo-bitmap and treating any "unknown" value as "invalid". As documented by the now-stale comment above the mode values, the values did have meaning when the optimized map was originally added. That dependent logical was removed by commit e45115b62f9a ("KVM: x86: use physical LAPIC array for logical x2APIC"), but the obfuscated behavior and its comment were left behind. Opportunistically rename "mode" to "logical_mode", partly to make it clear that the "disabled" case applies only to the logical map, but also to prove that there is no lurking code that expects "mode" to be a bitmap. Functionally, this is a glorified nop. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 21 ++++++++++-------- arch/x86/kvm/lapic.c | 38 ++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 1f51411f3112..0184e64ab555 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -955,19 +955,22 @@ struct kvm_arch_memory_slot { }; =20 /* - * We use as the mode the number of bits allocated in the LDR for the - * logical processor ID. It happens that these are all powers of two. - * This makes it is very easy to detect cases where the APICs are - * configured for multiple modes; in that case, we cannot use the map and - * hence cannot use kvm_irq_delivery_to_apic_fast either. + * Track the mode of the optimized logical map, as the rules for decoding = the + * destination vary per mode. Enabling the optimized logical map requires= all + * software-enabled local APIs to be in the same mode, each addressable AP= IC to + * be mapped to only one MDA, and each MDA to map to at most one APIC. */ -#define KVM_APIC_MODE_XAPIC_CLUSTER 4 -#define KVM_APIC_MODE_XAPIC_FLAT 8 -#define KVM_APIC_MODE_X2APIC 16 +enum kvm_apic_logical_mode { + KVM_APIC_MODE_SW_DISABLED, + KVM_APIC_MODE_XAPIC_CLUSTER, + KVM_APIC_MODE_XAPIC_FLAT, + KVM_APIC_MODE_X2APIC, + KVM_APIC_MODE_MAP_DISABLED, +}; =20 struct kvm_apic_map { struct rcu_head rcu; - u8 mode; + enum kvm_apic_logical_mode logical_mode; u32 max_apic_id; union { struct kvm_lapic *xapic_flat_map[8]; diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 8209caffe3ab..3b6ef36b3963 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -168,7 +168,12 @@ static bool kvm_use_posted_timer_interrupt(struct kvm_= vcpu *vcpu) =20 static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map, u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) { - switch (map->mode) { + switch (map->logical_mode) { + case KVM_APIC_MODE_SW_DISABLED: + /* Arbitrarily use the flat map so that @cluster isn't NULL. */ + *cluster =3D map->xapic_flat_map; + *mask =3D 0; + return true; case KVM_APIC_MODE_X2APIC: { u32 offset =3D (dest_id >> 16) * 16; u32 max_apic_id =3D map->max_apic_id; @@ -193,8 +198,10 @@ static inline bool kvm_apic_map_get_logical_dest(struc= t kvm_apic_map *map, *cluster =3D map->xapic_cluster_map[(dest_id >> 4) & 0xf]; *mask =3D dest_id & 0xf; return true; + case KVM_APIC_MODE_MAP_DISABLED: + return false; default: - /* Not optimized. */ + WARN_ON_ONCE(1); return false; } } @@ -256,10 +263,12 @@ void kvm_recalculate_apic_map(struct kvm *kvm) goto out; =20 new->max_apic_id =3D max_id; + new->logical_mode =3D KVM_APIC_MODE_SW_DISABLED; =20 kvm_for_each_vcpu(i, vcpu, kvm) { struct kvm_lapic *apic =3D vcpu->arch.apic; struct kvm_lapic **cluster; + enum kvm_apic_logical_mode logical_mode; u32 x2apic_id, physical_id; u16 mask; u32 ldr; @@ -314,7 +323,8 @@ void kvm_recalculate_apic_map(struct kvm *kvm) new->phys_map[physical_id] =3D apic; } =20 - if (!kvm_apic_sw_enabled(apic)) + if (new->logical_mode =3D=3D KVM_APIC_MODE_MAP_DISABLED || + !kvm_apic_sw_enabled(apic)) continue; =20 ldr =3D kvm_lapic_get_reg(apic, APIC_LDR); @@ -322,25 +332,33 @@ void kvm_recalculate_apic_map(struct kvm *kvm) continue; =20 if (apic_x2apic_mode(apic)) { - new->mode |=3D KVM_APIC_MODE_X2APIC; + logical_mode =3D KVM_APIC_MODE_X2APIC; } else { ldr =3D GET_APIC_LOGICAL_ID(ldr); if (kvm_lapic_get_reg(apic, APIC_DFR) =3D=3D APIC_DFR_FLAT) - new->mode |=3D KVM_APIC_MODE_XAPIC_FLAT; + logical_mode =3D KVM_APIC_MODE_XAPIC_FLAT; else - new->mode |=3D KVM_APIC_MODE_XAPIC_CLUSTER; + logical_mode =3D KVM_APIC_MODE_XAPIC_CLUSTER; } + if (new->logical_mode !=3D KVM_APIC_MODE_SW_DISABLED && + new->logical_mode !=3D logical_mode) { + new->logical_mode =3D KVM_APIC_MODE_MAP_DISABLED; + continue; + } + new->logical_mode =3D logical_mode; =20 - if (!kvm_apic_map_get_logical_dest(new, ldr, &cluster, &mask)) + if (WARN_ON_ONCE(!kvm_apic_map_get_logical_dest(new, ldr, + &cluster, &mask))) { + new->logical_mode =3D KVM_APIC_MODE_MAP_DISABLED; continue; + } =20 if (!mask) continue; =20 ldr =3D ffs(mask) - 1; if (!is_power_of_2(mask) || cluster[ldr]) { - new->mode =3D KVM_APIC_MODE_XAPIC_FLAT | - KVM_APIC_MODE_XAPIC_CLUSTER; + new->logical_mode =3D KVM_APIC_MODE_MAP_DISABLED; continue; } cluster[ldr] =3D apic; @@ -993,7 +1011,7 @@ static bool kvm_apic_is_broadcast_dest(struct kvm *kvm= , struct kvm_lapic **src, { if (kvm->arch.x2apic_broadcast_quirk_disabled) { if ((irq->dest_id =3D=3D APIC_BROADCAST && - map->mode !=3D KVM_APIC_MODE_X2APIC)) + map->logical_mode !=3D KVM_APIC_MODE_X2APIC)) return true; if (irq->dest_id =3D=3D X2APIC_BROADCAST) return true; --=20 2.37.2.672.g94769d06f0-goog From nobody Tue Apr 7 05:43:29 2026 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 697A1ECAAD5 for ; Wed, 31 Aug 2022 00:38:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232319AbiHaAii (ORCPT ); Tue, 30 Aug 2022 20:38:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232151AbiHaAhu (ORCPT ); Tue, 30 Aug 2022 20:37:50 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93CB4AA3C9 for ; Tue, 30 Aug 2022 17:36:22 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id d6-20020a170902cec600b00174be1616c4so4941931plg.22 for ; Tue, 30 Aug 2022 17:36:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=9lPsDa4uoIpB1TtG0KKHt7fahMUYRjr92cuUCEQolSA=; b=LlD9FTSgHb6L5Zv8qjIw+XDeJxa4BQaZ58WNL3BSpHwcUi8O8RkncM1V+t7kiJ106g 1zUqCPBcnIZfBu90aA8Ckd6Ps3a1cyvDQJCvx6dPlPQAnITZrVcgWaNm8Qg9iRKb2M09 jNppD5V0BqocNd+wVCeDYAH3e6WtlRhDTcnhmeDcLA2OUfkpIVF9KEGMJ8l16QuGvuZN COSSD46FEd5zuLR+LzH/kG5elBK4Rx+jG7GkJ0sAyH0cNnMUgSH2w4PQyLbBM0pyPXO1 MugeGJI8v+/V5MHQ3s9vQjWbYpfeOmXkMKOTrmh5/OJp0Dkc1rJl+pkzXmirdjWDcTcj ARgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=9lPsDa4uoIpB1TtG0KKHt7fahMUYRjr92cuUCEQolSA=; b=YHo4qy53O/XR/aYmTAIGwtcCQt2IMbreI02I17srfB5rpNtSOXrW2xvszy7GC5CJX7 udEIbRL3/kCQl1gS97IB2ZJAXGMRi4UmHtyqpSMhlLZYLy+Aq8dPWSgAtAJkflNj9KuC Cf388iB/gieO9rgwlu7FFMGZLBxlMarrGZ7w2tNr5LjgmSFlKAgvJ9e5gDKLfv4+XCC/ e60RPZyvui9KHSNmfmDrryiCIyDm368n/TpeW5/J8US6qxAE+6uCnUhFNV+SsVtAGppG EUnKetifbaMyyueuF8TtWUNpiFIfR4uysf1m4GRREH2ExmZGw2ZT/7fx0yty4eFMWXNH PN1A== X-Gm-Message-State: ACgBeo2k1X06ciLU3iix24091cXl6gNCwsWFKUE3q5d43V+8nZv2npNV uerMk1FUk1hG3JokQ4XItqDkv4DxnMo= X-Google-Smtp-Source: AA6agR6O/f1DFJZk6NAJUI6x8V5vzDybX6/EAvFj0v0O/b3Oy9wZI6KKeW14bvX07ZUrqHNogRQE4/P9jEo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:27a0:b0:52f:8766:82ec with SMTP id bd32-20020a056a0027a000b0052f876682ecmr23973929pfb.17.1661906137953; Tue, 30 Aug 2022 17:35:37 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:35:04 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-18-seanjc@google.com> Subject: [PATCH 17/19] KVM: SVM: Handle multiple logical targets in AVIC kick fastpath From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Iterate over all target logical IDs in the AVIC kick fastpath instead of bailing if there is more than one target and KVM's optimized APIC map is enabled for logical mode. If the optimized map is enabled, all vCPUs are guaranteed to be mapped 1:1 to a logical ID or effectively have logical mode disabled, i.e. iterating over the bitmap is guaranteed to kick each target exactly once. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 126 +++++++++++++++++++++++++--------------- 1 file changed, 79 insertions(+), 47 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 2095ece70712..dad5affe44c1 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -339,6 +339,62 @@ static void avic_kick_vcpu(struct kvm_vcpu *vcpu, u32 = icrl) icrl & APIC_VECTOR_MASK); } =20 +static void avic_kick_vcpu_by_physical_id(struct kvm *kvm, u32 physical_id, + u32 icrl) +{ + /* + * KVM inhibits AVIC if any vCPU ID diverges from the vCPUs APIC ID, + * i.e. APIC ID =3D=3D vCPU ID. + */ + struct kvm_vcpu *target_vcpu =3D kvm_get_vcpu_by_id(kvm, physical_id); + + /* Once again, nothing to do if the target vCPU doesn't exist. */ + if (unlikely(!target_vcpu)) + return; + + avic_kick_vcpu(target_vcpu, icrl); +} + +static void avic_kick_vcpu_by_logical_id(struct kvm *kvm, u32 *avic_logica= l_id_table, + u32 logid_index, u32 icrl) +{ + u32 physical_id; + + if (!avic_logical_id_table) { + u32 logid_entry =3D avic_logical_id_table[logid_index]; + + /* Nothing to do if the logical destination is invalid. */ + if (unlikely(!(logid_entry & AVIC_LOGICAL_ID_ENTRY_VALID_MASK))) + return; + + physical_id =3D logid_entry & + AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK; + } else { + /* + * For x2APIC, the logical APIC ID is a read-only value that is + * derived from the x2APIC ID, thus the x2APIC ID can be found + * by reversing the calculation (stored in logid_index). Note, + * bits 31:20 of the x2APIC ID aren't propagated to the logical + * ID, but KVM limits the x2APIC ID limited to KVM_MAX_VCPU_IDS. + */ + physical_id =3D logid_index; + } + + avic_kick_vcpu_by_physical_id(kvm, physical_id, icrl); +} + +static bool is_optimized_logical_map_enabled(struct kvm *kvm) +{ + struct kvm_apic_map *map; + bool enabled; + + rcu_read_lock(); + map =3D rcu_dereference(kvm->arch.apic_map); + enabled =3D map && map->logical_mode !=3D KVM_APIC_MODE_MAP_DISABLED; + rcu_read_unlock(); + return enabled; +} + /* * A fast-path version of avic_kick_target_vcpus(), which attempts to match * destination APIC ID to vCPU without looping through all vCPUs. @@ -346,11 +402,10 @@ static void avic_kick_vcpu(struct kvm_vcpu *vcpu, u32= icrl) static int avic_kick_target_vcpus_fast(struct kvm *kvm, struct kvm_lapic *= source, u32 icrl, u32 icrh, u32 index) { - u32 l1_physical_id, dest; - struct kvm_vcpu *target_vcpu; int dest_mode =3D icrl & APIC_DEST_MASK; int shorthand =3D icrl & APIC_SHORT_MASK; struct kvm_svm *kvm_svm =3D to_kvm_svm(kvm); + u32 dest; =20 if (shorthand !=3D APIC_DEST_NOSHORT) return -EINVAL; @@ -367,14 +422,14 @@ static int avic_kick_target_vcpus_fast(struct kvm *kv= m, struct kvm_lapic *source if (!apic_x2apic_mode(source) && dest =3D=3D APIC_BROADCAST) return -EINVAL; =20 - l1_physical_id =3D dest; - - if (WARN_ON_ONCE(l1_physical_id !=3D index)) + if (WARN_ON_ONCE(dest !=3D index)) return -EINVAL; =20 + avic_kick_vcpu_by_physical_id(kvm, dest, icrl); } else { - u32 bitmap, cluster; - int logid_index; + u32 *avic_logical_id_table; + unsigned long bitmap, i; + u32 cluster; =20 if (apic_x2apic_mode(source)) { /* 16 bit dest mask, 16 bit cluster id */ @@ -394,50 +449,27 @@ static int avic_kick_target_vcpus_fast(struct kvm *kv= m, struct kvm_lapic *source if (unlikely(!bitmap)) return 0; =20 - if (!is_power_of_2(bitmap)) - /* multiple logical destinations, use slow path */ + /* + * Use the slow path if more than one bit is set in the bitmap + * and KVM's optimized logical map is disabled to avoid kicking + * a vCPU multiple times. If the optimized map is disabled, a + * vCPU _may_ have multiple bits set in its logical ID, i.e. + * may have multiple entries in the logical table. + */ + if (!is_power_of_2(bitmap) && + !is_optimized_logical_map_enabled(kvm)) return -EINVAL; =20 - logid_index =3D cluster + __ffs(bitmap); - - if (!apic_x2apic_mode(source)) { - u32 *avic_logical_id_table =3D - page_address(kvm_svm->avic_logical_id_table_page); - - u32 logid_entry =3D avic_logical_id_table[logid_index]; - - if (WARN_ON_ONCE(index !=3D logid_index)) - return -EINVAL; - - /* Nothing to do if the logical destination is invalid. */ - if (unlikely(!(logid_entry & AVIC_LOGICAL_ID_ENTRY_VALID_MASK))) - return 0; - - l1_physical_id =3D logid_entry & - AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK; - } else { - /* - * For x2APIC, the logical APIC ID is a read-only value - * that is derived from the x2APIC ID, thus the x2APIC - * ID can be found by reversing the calculation (done - * above). Note, bits 31:20 of the x2APIC ID are not - * propagated to the logical ID, but KVM limits the - * x2APIC ID limited to KVM_MAX_VCPU_IDS. - */ - l1_physical_id =3D logid_index; - } + if (apic_x2apic_mode(source)) + avic_logical_id_table =3D NULL; + else + avic_logical_id_table =3D page_address(kvm_svm->avic_logical_id_table_p= age); + + for_each_set_bit(i, &bitmap, 16) + avic_kick_vcpu_by_logical_id(kvm, avic_logical_id_table, + cluster + i, icrl); } =20 - /* - * KVM inhibits AVIC if any vCPU ID diverges from the vCPUs APIC ID, - * i.e. APIC ID =3D=3D vCPU ID. Once again, nothing to do if the target - * vCPU doesn't exist. - */ - target_vcpu =3D kvm_get_vcpu_by_id(kvm, l1_physical_id); - if (unlikely(!target_vcpu)) - return 0; - - avic_kick_vcpu(target_vcpu, icrl); return 0; } =20 --=20 2.37.2.672.g94769d06f0-goog From nobody Tue Apr 7 05:43:29 2026 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 BF977ECAAA1 for ; Wed, 31 Aug 2022 00:38:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232341AbiHaAim (ORCPT ); Tue, 30 Aug 2022 20:38:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232013AbiHaAhx (ORCPT ); Tue, 30 Aug 2022 20:37:53 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1502AE232 for ; Tue, 30 Aug 2022 17:36:23 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id e187-20020a6369c4000000b0041c8dfb8447so6289993pgc.23 for ; Tue, 30 Aug 2022 17:36:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=COtQNDmyFMKiUnG0r6DP8wnDX53zfdZyDRmbDYIlONY=; b=lv67W+znzN/46x7oLehYcakVQyriJLYBfxgch3FDtG3+6DalnO4gEwMnCOeXlh3RsV QL1VXkz2olPy3veCJ8XWGpnGwzQLCt8bKiqiAZQ6pjj0ImImEt0lD+qsuj/KSitJmtrt r+Yq7rr7LFmnXuHZbUAYDmwESgLTHohJHspdlcwms+caDyxgUS2/RgIuiH4VrW0Ry/bM 6PMkkR3Ose6LKPEoEZfmTGpmfkOAO2HmAmLmGUZL2xHfpb4vCJE9eeoAUpuMOLJknLaH Hm6X+HjPjR+JCRhkDKOdrunS5EEPGwg6wzsQN4OIRJYOvPtBDFFkENzCfVWNMs17qam3 nJmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=COtQNDmyFMKiUnG0r6DP8wnDX53zfdZyDRmbDYIlONY=; b=20G9i8Iwv5QC6mmU5C3p4MQc0hDx8PiLxhl2MAUbJa50bwptaobcphTF1GIn6RNCsA 6+gPpwZyp17QFGh+7ensGrdCrrmmjLPG1GU98B/+q9uHPJaI3iahQQ6eJu3V+8DyFEdo 8eSXNkF3eKS34iPB5SjheUlRJh1QSUE/og4laMiXrXYpjbOcZdWVnopNxFhmB5fIP/R/ /cBREUx5XV6WI4Rm2R+O/GlvLzgBW/V4vkfC3H4LO7ZDF2bmxzDUvFT+1yJMbrbg+Ea7 GpH3ipqWSNtpk+m5kIApsKy1COzGCr9XfqkAj3Z4yYHYtyRunyR5ElJpVEvRAlqrgwKC aSRw== X-Gm-Message-State: ACgBeo3kXL7sVcw8bssHykq+jwY4nqHRkA5/7PIbXG5oH1chBSkD5Bya iSRDDsOhbvtCiV1DLeSD2mlxBfUvsYw= X-Google-Smtp-Source: AA6agR53jSxsmlnw8Vqq0VWR0l6XXD330eDCt8c05joJrhe9CCBHlg9c14qWUc1wmSjGkMR7ljsa45TWru4= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:a19:b0:535:49ee:da43 with SMTP id p25-20020a056a000a1900b0053549eeda43mr23874432pfh.53.1661906139729; Tue, 30 Aug 2022 17:35:39 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:35:05 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-19-seanjc@google.com> Subject: [PATCH 18/19] KVM: SVM: Ignore writes to Remote Read Data on AVIC write traps From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop writes to APIC_RRR, a.k.a. Remote Read Data Register, on AVIC unaccelerated write traps. The register is read-only and isn't emulated by KVM. Sending the register through kvm_apic_write_nodecode() will result in screaming when x2APIC is enabled due to the unexpected failure to retrieve the MSR (KVM expects that only "legal" accesses will trap). Fixes: 4d1d7942e36a ("KVM: SVM: Introduce logic to (de)activate x2AVIC mode= ") Signed-off-by: Sean Christopherson Reviewed-by: Maxim Levitsky --- arch/x86/kvm/svm/avic.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index dad5affe44c1..b2033a56010c 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -675,6 +675,9 @@ static int avic_unaccel_trap_write(struct kvm_vcpu *vcp= u) case APIC_DFR: avic_handle_dfr_update(vcpu); break; + case APIC_RRR: + /* Ignore writes to Read Remote Data, it's read-only. */ + return 1; default: break; } --=20 2.37.2.672.g94769d06f0-goog From nobody Tue Apr 7 05:43:29 2026 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 4FBF3ECAAA1 for ; Wed, 31 Aug 2022 00:38:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231894AbiHaAiw (ORCPT ); Tue, 30 Aug 2022 20:38:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232191AbiHaAh7 (ORCPT ); Tue, 30 Aug 2022 20:37:59 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BAF4AE9C2 for ; Tue, 30 Aug 2022 17:36:36 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id k3-20020a170902c40300b001743aafd6c6so8982943plk.20 for ; Tue, 30 Aug 2022 17:36:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=g3L7Nx3h3Wnl/8etnteeOlF7DoJAlFFq718LJDW6wr4=; b=kHswN3zBoCOnCESvdwJre0Gz76boSxHrEIE7McE2aLOB8cZBTx6/AOmoq/hpHwNehr 9MLuP6xXxV4a6kEZQ3Pi1RJkh7q945nZFyA0X9XicVd/K3e0HY2qVaI6EYIgqw+naog/ Gk7pV0I1xV1BX0elxQdq/of5qSDLSv5JqIem2idLS+gijibV2w9WVZbBk/ZseUk+7wiO BCIdFOyt1us80s1YhqIA4wJt8EJZTq9EspRP6s2j2zC9/5VDD0P6Ne7NveM4tnaPoW7G sxcNB6CprsftG1WKLyg6iwXkpXlAEOc5QVrGvFCDTMVyM2UXz0Mk8nlTsJneDFBWy3tQ ivOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=g3L7Nx3h3Wnl/8etnteeOlF7DoJAlFFq718LJDW6wr4=; b=TKq26HFqi4lNyltxzV8SG/PVU59er04/K22QvdPJjdgqTnm4bqgL4in11Cwq604AEA zfp4yFgS7Nf2xtxXAlXvlpN7G9wZnLka1SzrQeiepOSEMGfAsC1zgBSaQDjYjwIV1aH5 1ihkDPGfTEhzRVgWAkozBPaB/COONk+6Ehoh89GM+qgCr3AVLicsQIRTutgyOnmkAlV5 HCyo2OJbP2KWesIhs0fNA2+bUkEWuzCH0ZN+7zG1SzD5nlyEIZcIitKLiPrAT5WR9ViW ZFQa2xQVHbZDXxUHOcAu9XF++75Cic6pOR+pnWc+Frx4QGOvTOya3gZVUEyJWnMjcouO 4Llw== X-Gm-Message-State: ACgBeo3Ubyzdv/3YgoIVGE3UkYKXTt2g0dMz/MmtgRMDGGYAt84ZJCxc KmYIqn91e4IbaB4e5jpY+9hsO12lDJw= X-Google-Smtp-Source: AA6agR6IN1LAnfH5bBLeBScqkCwKN870seY3uAaGKDqblKRdwC8ifgww1hgXKL+ypQuO4vOwOYo86F/EPu0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:9308:b0:174:9dff:716b with SMTP id bc8-20020a170902930800b001749dff716bmr14644974plb.75.1661906141530; Tue, 30 Aug 2022 17:35:41 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 31 Aug 2022 00:35:06 +0000 In-Reply-To: <20220831003506.4117148-1-seanjc@google.com> Mime-Version: 1.0 References: <20220831003506.4117148-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220831003506.4117148-20-seanjc@google.com> Subject: [PATCH 19/19] Revert "KVM: SVM: Do not throw warning when calling avic_vcpu_load on a running vcpu" From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Turns out that some warnings exist for good reasons. Restore the warning in avic_vcpu_load() that guards against calling avic_vcpu_load() on a running vCPU now that KVM avoids doing so when switching between x2APIC and xAPIC. The entire point of the WARN is to highlight that KVM should not be reloading an AVIC. Opportunistically convert the WARN_ON() to WARN_ON_ONCE() to avoid spamming the kernel if it does fire. This reverts commit c0caeee65af3944b7b8abbf566e7cc1fae15c775. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index b2033a56010c..3c300113d40b 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -1080,6 +1080,7 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) return; =20 entry =3D READ_ONCE(*(svm->avic_physical_id_cache)); + WARN_ON_ONCE(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK); =20 entry &=3D ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK; entry |=3D (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK); --=20 2.37.2.672.g94769d06f0-goog