Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp94026rwb; Fri, 30 Sep 2022 18:04:30 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7hAgyjJ93+n/kijZz+CKX8cjR+1qnuAXl4SqxXzt6YOhgCpCuLn9FMj2uKUuBhpmLTQzXp X-Received: by 2002:a63:d145:0:b0:443:c25f:5dd5 with SMTP id c5-20020a63d145000000b00443c25f5dd5mr2214227pgj.554.1664586270347; Fri, 30 Sep 2022 18:04:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664586270; cv=none; d=google.com; s=arc-20160816; b=ra5nvrdC8bAOyKCiIq5CxmfNyYD+lcPYqf0basSJ0nEYDZehrgS5PyfiN69jJ3PM6I sANw7x6TdGMbuD93+MAdfXQ+cwHWGlHpXH+tq5DCl3LdoP0i4jPTT3sDADMh9OutROSX EjtCpDwdDatXq1DJCTtuJEpabvgfeCCtQMcvpB2sRqxBl5NfCXdok8v6NWr46eR9hx5p fapB2jF9cWjky37gnIAjuyR4ZDkf7zGknxPJBbptAbI3/YLmIkm/wmrwTDzDxBgdyZTg LdvDCpQfIY8SgBAiGAsfpFx9ScwCPxDiqkI2/MRpTDGMhg9lF8NMEEW8J7QdWljzrw3A txvg== 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=ImF7jtGIWsEQFmZLHvw2ZrcuCkowN+9OcwQZDkOS8Jw=; b=G1iloB7bHvcKJL8Ev03Xra7tlf4Vefncx3cxO1K048KiC9kMecJyI1ScMfDZXX3zTz I4srykQ/Oly4SVaXWp2qegMb5O/ghvavRqXJym7uHjmIy5VZ1prJFJEBQKj14JdElxpE nQSNAzt9MXot44qT7ojxZpXobQ5fw9lmW2UN2M0X/llX/7DvZoWeqdo9s8s4O6ueW/Ul f8V+sJhim5vPlEXAGYsepVYOxkQbZMAOsgwxlIfyLP4JmOUJl9jbvtrCv0wEAdJBM0Br BpGkbs4xUY0b4zPYbTVJoilRmkypKB09GL25PrL5viojyQgVoI7a8UFpFuIuPnMeJAoR D75A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Tn5wogln; 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 a72-20020a621a4b000000b00528d69a048csi3419826pfa.284.2022.09.30.18.03.56; Fri, 30 Sep 2022 18:04:30 -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=Tn5wogln; 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 S233121AbiJABDB (ORCPT + 99 others); Fri, 30 Sep 2022 21:03:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233040AbiJABCP (ORCPT ); Fri, 30 Sep 2022 21:02:15 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12C07F1628 for ; Fri, 30 Sep 2022 18:00:18 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-3553d2d9d7fso52358617b3.5 for ; Fri, 30 Sep 2022 18:00:18 -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=ImF7jtGIWsEQFmZLHvw2ZrcuCkowN+9OcwQZDkOS8Jw=; b=Tn5woglnp5SZq3nLO2ixOB3tUyQGkXWs8upDL1g76RMOiLpG8acsE5ufPXN0hKCDOa Sn7Rc/DEQw/PjSwRu7K+V5+QDpeISlhIamISw5HWMSb3pVUFHm2ix37ueq2JhLsKN4Ig ihQ1Q1qkKkR5cOXm//rspQHc3jrSkee+CA68y/rDNYXaalL+ZX8A6M92b437e9qNFqxy TbSNPtmkYDw9SJGidznnG6V33Ba+xtuKWvqxbcpfNFr8aOf8DBC80ZvD476gVCn873OX sNlv+QZ0dt8a2nuGHdBq0LJ0GpxdYZ2PlXwdnyaqFqzs2iTMO9UGCIdL6kKIQroOL4wc q3qQ== 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=ImF7jtGIWsEQFmZLHvw2ZrcuCkowN+9OcwQZDkOS8Jw=; b=MeTHdUtPxB/pq0ys7MA9q4a7+SaIclPu0u7aMLWm1arSB0jvY+cSJXepvRGqTVp92H RHX/yUA/1GuCxnNCiQ2orsOE8BWEn2QVBFOXyGG2heJ8rgIJLJHWlR8aIa/x0ZimT+uk 8f4E66GG8pwNQS+uZ0kTHFcAma+KHg6Z7X9ebDV9lXjCT1zjpLQDjKSZh3D8gF/8F9Ss 7yLdbmR+dTtKnRUqeSayZe8wqCWrLqMFzW+9O2rqKrSVsv/GwlaMPcMDotHZKTKYkQxr 5Lp18bsc4QPMZNpsFeUQFz002F4TIglL99kfVDNhFsXXzkPrB5e4G4GKJDXy9YOpcIvY laDg== X-Gm-Message-State: ACrzQf0MpmGNW5mqHcxKGrpqDziBXyIRn4NLyXak07kRl0ZWXyAI+jLI SlhHett9VxVsK3jnV14j7jp/tdplvj0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:24d:0:b0:6bd:2936:a191 with SMTP id 74-20020a25024d000000b006bd2936a191mr1724809ybc.112.1664585997792; Fri, 30 Sep 2022 17:59:57 -0700 (PDT) Reply-To: Sean Christopherson Date: Sat, 1 Oct 2022 00:59:06 +0000 In-Reply-To: <20221001005915.2041642-1-seanjc@google.com> Mime-Version: 1.0 References: <20221001005915.2041642-1-seanjc@google.com> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog Message-ID: <20221001005915.2041642-24-seanjc@google.com> Subject: [PATCH v4 23/32] 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, 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 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 0xff") 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 ------------------------------ TBD + +x2APIC +------ +When KVM_X2APIC_API_USE_32BIT_IDS is enabled, KVM activates a hack/quirk that +allows sending events to a single vCPU using its x2APIC ID even if the target +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 APIC 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 14f03e654de4..340c2d3e781b 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -274,10 +274,10 @@ void kvm_recalculate_apic_map(struct kvm *kvm) struct kvm_lapic *apic = vcpu->arch.apic; struct kvm_lapic **cluster; enum kvm_apic_logical_mode logical_mode; + u32 x2apic_id, physical_id; u16 mask; u32 ldr; u8 xapic_id; - u32 x2apic_id; if (!kvm_apic_present(vcpu)) continue; @@ -285,16 +285,48 @@ void kvm_recalculate_apic_map(struct kvm *kvm) xapic_id = kvm_xapic_id(apic); x2apic_id = kvm_x2apic_id(apic); - /* Hotplug hack: see kvm_apic_match_physical_addr(), ... */ - if ((apic_x2apic_mode(apic) || x2apic_id > 0xff) && - x2apic_id <= new->max_apic_id) - new->phys_map[x2apic_id] = apic; /* - * ... xAPIC ID of VCPUs with APIC ID > 0xff will wrap-around, - * prevent them from masking VCPUs with APIC ID <= 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] = apic; + if (kvm->arch.x2apic_format) { + /* See also kvm_apic_match_physical_addr(). */ + if ((apic_x2apic_mode(apic) || x2apic_id > 0xff) && + x2apic_id <= new->max_apic_id) + new->phys_map[x2apic_id] = apic; + + if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id]) + new->phys_map[xapic_id] = 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 = x2apic_id; + else + physical_id = xapic_id; + + if (new->phys_map[physical_id]) { + kvfree(new); + new = NULL; + goto out; + } + new->phys_map[physical_id] = apic; + } if (new->logical_mode == KVM_APIC_MODE_MAP_DISABLED || !kvm_apic_sw_enabled(apic)) -- 2.38.0.rc1.362.ged0d419d3c-goog