Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp4614654rwb; Tue, 20 Sep 2022 17:11:12 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5BlV9leIoKqkK9rZPvIumcrcw6/Dm8gnr/KXPA0CeCCoy3GgbJRgUbSxywpvlk9khJOSI0 X-Received: by 2002:a17:902:dac4:b0:178:42d4:dcc9 with SMTP id q4-20020a170902dac400b0017842d4dcc9mr2047473plx.167.1663719072046; Tue, 20 Sep 2022 17:11:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663719072; cv=none; d=google.com; s=arc-20160816; b=bpCDIlVptcKxRGze/Lp+Og6NvRmNqTUwNrx+sh+bN3d/M3ApFi48f4yj0/UNmY+9Om EEOAG6HA0EGaO+blspcdYPfdB/ftzrOLBDOdQBLX1TGgXbPaYHI9RU+0KAUL1iJMvLXl rcWI9tOrsST8BsTPRnMR3nB941NMQ6ryzP+f8DO6KAEdBNVKZr+4K/S0jPX9tcNViKt2 EV+H88f44toqWMJH+h0EuvncPvieL+NlpjKgOGvPc6MpCuS7isPfilZvgn4xhUuqrguK qa6DF2yjybICKPuy+OtHHvseeIaLc9sjKmZcDRDCCIKyijA/HhPuuSqIDK4bnpmqBDbc pbfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=g79qUsYaWi35oGsfhRv6mmj8mg5DWdHzyNJhsg4wv1U=; b=R3wLq89tzzSvjXCs6A4vihXkhbq2GnhpCa9VvRgJ1G1spTluJLTgrV+5cMqPmPDt7A xJboDlPQfUvtmLCvBQk/CHSw1w5lL0zg+743rEO1bGCkRsAXANiJLsPQXmaY03DE5K3X 0549S2q3e5Olri5O4W36fwzySGymH9AHfJ557y8e4QFJiPL3M9oOrXxkQAp3K8elBuUX vA8+jeClHBOLoiF4Rfi1xYqZoL4nI5QIYTBkfgwHtcwOOI0pp9khO0pfOixTTH8PPGXG InGkWDhkdsqc7rOAZrmDL/VfSU7pR7WvVijanWj3NGZBwu+SSfPQWkQ3PoDVpSGoJTkZ Ydmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=hK3090xN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h9-20020a17090aa88900b00203969e07e8si1160949pjq.75.2022.09.20.17.11.00; Tue, 20 Sep 2022 17:11:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=hK3090xN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231573AbiITXd0 (ORCPT + 99 others); Tue, 20 Sep 2022 19:33:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231232AbiITXce (ORCPT ); Tue, 20 Sep 2022 19:32:34 -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 82FE8786F6 for ; Tue, 20 Sep 2022 16:32:12 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id k19-20020a056a00135300b0054096343fc6so2505973pfu.10 for ; Tue, 20 Sep 2022 16:32:12 -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=g79qUsYaWi35oGsfhRv6mmj8mg5DWdHzyNJhsg4wv1U=; b=hK3090xNbEEAnH2ylBAx4vDXE98QTnHl+P7+5k+euLTCeFTJSirCLZhWk9JDzQ9I8Y S+FsgUh+YE//NEaNGciDy7xPDaKvtjALTFBaeLcvlbD1Lef1S+5UX40jzItqVf3GlYq5 ilS9sudcfDC+B7t/Ck/O+5qseLAKjJ4ZzSivJLJYubWpm21tSXHfc4FTpHiVPxLRezLJ Tw55NDd4PTuKyZXndJkDXcmk7+r5BUtQs9BczQkNxwoav4SLqmx5It3z8K4b09YN8gPZ HhQ7p1CJ/69KF7fm6MT5VfC6hfzet5ZSWc6gldJH2mkWTgyk/ncjTqyaOHFlhyLm3PS9 vjQA== 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=g79qUsYaWi35oGsfhRv6mmj8mg5DWdHzyNJhsg4wv1U=; b=B6U/U+znOfLy9C/qo3anccsihFo4K+Zz31mJmMnuqbkyMuWEnTb7dUCVzAUGEylVg4 lGQzXeF8GuAiAjs0TcSKV95NDYgyIZV1T9hmhShAemsnD6OKAVSCXtsR2AxR4YvfBlA/ tTZX6NxrNn1MhYWArCZ3iBus3NzVZzDw3jhPDF0/HBvcKJXcA1Ue0RB85M5/LwvcG6Sr MKal4VqZVUSUR+MZZUOBe9lbfWEIk4HL1VGNDvUvsgkZVEMVpkDDDogyFEfNEaOLmcLC c9WEtWEUyg45g+kUrMPiMGQy46AL5DxEXx12ARXDagOYV8sVZiIxj+q0Ek2G2cg7Jnxp FrTQ== X-Gm-Message-State: ACrzQf2VWcDO91+uYxmBY92XdgrVwE0/jibrunS+8X7Ul4GceHsQ8R0A vM89X9z5ym4KnKYtzNetf5bybgUA0cQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:3d82:0:b0:43b:d646:1bb5 with SMTP id k124-20020a633d82000000b0043bd6461bb5mr3432423pga.620.1663716731774; Tue, 20 Sep 2022 16:32:11 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 20 Sep 2022 23:31:27 +0000 In-Reply-To: <20220920233134.940511-1-seanjc@google.com> Mime-Version: 1.0 References: <20220920233134.940511-1-seanjc@google.com> X-Mailer: git-send-email 2.37.3.968.ga6b4b080e4-goog Message-ID: <20220920233134.940511-22-seanjc@google.com> Subject: [PATCH v3 21/28] KVM: x86: Inhibit APICv/AVIC if the optimized physical map is disabled From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Alejandro Jimenez , Suravee Suthikulpanit , Maxim Levitsky , Li RongQing Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Inhibit APICv/AVIC if the optimized physical map is disabled so that KVM KVM provides consistent APIC behavior if xAPIC IDs are aliased due to vcpu_id being truncated and the x2APIC hotplug hack isn't enabled. If the hotplug hack is disabled, events that are emulated by KVM will follow architectural behavior (all matching vCPUs receive events, even if the "match" is due to truncation), whereas APICv and AVIC will deliver events only to the first matching vCPU, i.e. the vCPU that matches without truncation. Note, the "extra" inhibit is needed because KVM deliberately ignores mismatches due to truncation when applying the APIC_ID_MODIFIED inhibit so that large VMs (>255 vCPUs) can run with APICv/AVIC. Fixes: TDB Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 6 ++++++ arch/x86/kvm/lapic.c | 13 ++++++++++++- arch/x86/kvm/svm/avic.c | 1 + arch/x86/kvm/vmx/vmx.c | 1 + 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 6570b5d728ef..594674eefe59 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1097,6 +1097,12 @@ enum kvm_apicv_inhibit { */ APICV_INHIBIT_REASON_BLOCKIRQ, + /* + * APICv is disabled because not all vCPUs have a 1:1 mapping between + * APIC ID and vCPU, _and_ KVM is not applying its x2APIC hotplug hack. + */ + APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED, + /* * For simplicity, the APIC acceleration is inhibited * first time either APIC ID or APIC base are changed by the guest diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index b344ab52556e..4db162b1f0b1 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -381,6 +381,16 @@ void kvm_recalculate_apic_map(struct kvm *kvm) cluster[ldr] = apic; } out: + /* + * The optimized map is effectively KVM's internal version of APICv, + * and all unwanted aliasing that results in disabling the optimized + * map also applies to APICv. + */ + if (!new) + kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED); + else + kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED); + old = rcu_dereference_protected(kvm->arch.apic_map, lockdep_is_held(&kvm->arch.apic_map_lock)); rcu_assign_pointer(kvm->arch.apic_map, new); @@ -2150,7 +2160,8 @@ static void kvm_lapic_xapic_id_updated(struct kvm_lapic *apic) /* * Deliberately truncate the vCPU ID when detecting a modified APIC ID * to avoid false positives if the vCPU ID, i.e. x2APIC ID, is a 32-bit - * value. + * value. If the wrap/truncation results in unwatned aliasing, APICv + * will be inhibited as part of updating KVM's optimized APIC maps. */ if (kvm_xapic_id(apic) == (u8)apic->vcpu->vcpu_id) return; diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index dd0e41d454a7..2908adc79ea6 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -965,6 +965,7 @@ bool avic_check_apicv_inhibit_reasons(enum kvm_apicv_inhibit reason) BIT(APICV_INHIBIT_REASON_PIT_REINJ) | BIT(APICV_INHIBIT_REASON_BLOCKIRQ) | BIT(APICV_INHIBIT_REASON_SEV) | + BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED) | BIT(APICV_INHIBIT_REASON_APIC_ID_MODIFIED) | BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED) | BIT(APICV_INHIBIT_REASON_X2APIC); diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index b39095ef9bd7..0f9f8ae59f85 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7963,6 +7963,7 @@ static bool vmx_check_apicv_inhibit_reasons(enum kvm_apicv_inhibit reason) BIT(APICV_INHIBIT_REASON_ABSENT) | BIT(APICV_INHIBIT_REASON_HYPERV) | BIT(APICV_INHIBIT_REASON_BLOCKIRQ) | + BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED) | BIT(APICV_INHIBIT_REASON_APIC_ID_MODIFIED) | BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED); -- 2.37.3.968.ga6b4b080e4-goog