From patchwork Fri Jan 6 01:12:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 39884 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp578309wrt; Thu, 5 Jan 2023 17:19:47 -0800 (PST) X-Google-Smtp-Source: AMrXdXskMT4Prrq9mAwfuiLK+mYOvbCKa6x1ssVpiu6ncgszhE9og41rZjorMTImMCcia19Ze/qx X-Received: by 2002:a05:6a20:c906:b0:b4:f66a:99a9 with SMTP id gx6-20020a056a20c90600b000b4f66a99a9mr11348101pzb.60.1672967986882; Thu, 05 Jan 2023 17:19:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672967986; cv=none; d=google.com; s=arc-20160816; b=ETyOdFI0roFDc1qQXBnRJnocigd8qqL9J7R6p2yXbZ8UUvuqcUAgloBk2RTfpZOKpb /wseQMkKVP+wtKEdYvGzp493nHkZBMSsSHyWVaM59BJgihqSvkIUwh0SHyh5jMYgLNE5 yVpMKgZZsr4iYCJOTIBRd4OBJxfjAnBN7LYTaCZklId+uxPUVsN4WpBPptWh1I7Pq+j3 P+6NRbOjIwd+Tj6UxUPO+bqNTvLhw2nSitMsL2fuWVeudkOM3E6ocp2obIseAoCggy4N bdDzyI+WZGhGS/taeFIOD7guMASydc+2BsY5m1X3FqbpUhsLmi4uxZLuimGxGEB0U/Em iLIg== 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=ZYN39wk6a5NRPTtVgQ2fmBkBZpoYFPy36qtJQgrzBbQ=; b=Ii1GL1Nt7tqbYn2HdlvQplEqgr2D/S4NTHB+4Juun2g4qycj3iCkazeDxAgF23a12J iwp0yaxLfPZ03dClXvqrCK6W8CjJ1emK6sbMQxWfgAkAeyqZwvJuQC3gR9ZYJFJtmesc zQVXtfvIwatctud24vg+1/t4vy6PbCIoOyB52n6MIJ69XeCV6AOOXrx8+Wx/HsBx+fID BdfOr7YbH56/y5DAhyrvO25vOJFAnLNbV/0TMoDcOJ1ZbMa0cCDnHs0Pph2RlieiDNkq JqzN+LJS15LqG+x6Z8DsXDlXgz+hSi6qL/tyzE0EVblgRaFP9EI8yR8d1M/JGbVpk08l PxMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=S4tMZcsl; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x8-20020a654548000000b0049d7f3cc5b9si23199390pgr.335.2023.01.05.17.19.33; Thu, 05 Jan 2023 17:19:46 -0800 (PST) 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=S4tMZcsl; 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 S229688AbjAFBQe (ORCPT + 99 others); Thu, 5 Jan 2023 20:16:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232228AbjAFBP5 (ORCPT ); Thu, 5 Jan 2023 20:15:57 -0500 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 3FD333E85E for ; Thu, 5 Jan 2023 17:14:09 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id n1-20020a170902e54100b00192cc6850ffso108804plf.18 for ; Thu, 05 Jan 2023 17:14:09 -0800 (PST) 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:message-id:reply-to; bh=ZYN39wk6a5NRPTtVgQ2fmBkBZpoYFPy36qtJQgrzBbQ=; b=S4tMZcslkr51udzOZhWe5rKJnCQOlvWCj/gy1+r0cmExiw+cnm625YYiIx8qAy+rxD WvQgMxjEoJ9RMz74DDzoYV0cEZd9fIWBvu19nxlEKwAH50Xx4FuNpoFVUcgflwQJEmrS eJ6h18W2yIpdYB+fSPatYOOy2+0JgVN81vNGq4AmpfL1sQJ+L+DzVIA4kaxrkDdk+Kxs yX3KZ6hxCMxxepCxPmR0I5QKTDhwjTUuZ5zXzQk1rkP4kdAB8qBwDUmsRn4cQl61qejp GXqhyHZjijzndqJQIuKbqe8zAn+FYcY7F4/TpJ4UFQoxhUpsZcfqCaYEyvGEMbo4XoTB 8oKQ== 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:message-id :reply-to; bh=ZYN39wk6a5NRPTtVgQ2fmBkBZpoYFPy36qtJQgrzBbQ=; b=yPFSaMK8U9bDujjnA0KK3mVkm7uZ8n4WdNUVAjWUeIvfPABdiS8wG2LuyAZujT2Sq/ FS4J4P70lcghZiLrhUUW5qmiO7Ckxv0pnGhVvXf2fKHvICZ4P7ZcsHwjl70SOhXvHnRa VpKoK6R3dPG3Xrfzat1IJrBksIRyXskhNLsDOtSGMU0rO9Fjq2DxIssKXNMpLgTjtQV8 QshrOEBcOa/HslFQFKmgF8t26JnOnLfI18YNdKvAXKLRnKHFxujl6ac7DssNAXhSs8PH Cq1QeKoIoBaWbNMjR/54Qj3s9+ekHJ512MpLFPWkVpv5DSrB+c9Z5eczuboMsmABFy1k QRyQ== X-Gm-Message-State: AFqh2kpRuDppNztpkmQgDM83KhH824W9mzMndx2CiFCh3WSLzrPOEOi4 ZYXlazbTLOHfdlBjsC2gDpMhe1e/fPE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:a50c:b0:226:744:d464 with SMTP id a12-20020a17090aa50c00b002260744d464mr2481575pjq.62.1672967632838; Thu, 05 Jan 2023 17:13:52 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 6 Jan 2023 01:12:56 +0000 In-Reply-To: <20230106011306.85230-1-seanjc@google.com> Mime-Version: 1.0 References: <20230106011306.85230-1-seanjc@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Message-ID: <20230106011306.85230-24-seanjc@google.com> Subject: [PATCH v5 23/33] 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 , Maxim Levitsky , Suravee Suthikulpanit , Li RongQing , Greg Edwards 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754234079784826874?= X-GMAIL-MSGID: =?utf-8?q?1754234079784826874?= 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. Reviewed-by: Maxim Levitsky 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 732421a0ad02..5865bad08a23 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1174,6 +1174,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 e9f258de91bd..297da684c25f 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -386,6 +386,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); @@ -2158,7 +2168,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 unwanted 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 76da9f19272e..d1ac19f053ce 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); diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 8ed14fcfe870..ef84d11a0fe4 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -8029,6 +8029,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);