From nobody Sun Feb 8 15:57:40 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 C0A10C3DA7A for ; Fri, 6 Jan 2023 01:17:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232119AbjAFBRQ (ORCPT ); Thu, 5 Jan 2023 20:17:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229643AbjAFBQ1 (ORCPT ); Thu, 5 Jan 2023 20:16:27 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9922265ADD for ; Thu, 5 Jan 2023 17:14:20 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id k18-20020a25e812000000b0077cc9ab9dd9so425086ybd.8 for ; Thu, 05 Jan 2023 17:14:20 -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=G/xMBTyIGc3/xkevi67fvj8wyXuCW/OQud7CJpQaZ3I=; b=d6QlI5kw/H8GG/AjdSQEB/z5oht/Dlvzb9pO6xzHuZuAfEpDPK0ubjoADbbBmfnpip lWPSmAFPSaimE6lYMZceH/Dq8aCshL0Uyq6vA0W4P5OLUz3h5Vf5ONtVay7T/m59CFem DMJdWK90qXXkUTFbGZuPDdtyzYWxe8hii8trgtihyNeKBPcmz5CtIFQ/23aaAfs94xsl Sg4uVP8j8Dy9Mt38n+KhVJN6fk52HJx9m33qQD1GeqhFFIKqoZkU98q5/5BQS5/EonhA +b0dawmxW7RqY0D6xUNS6b67hvED4rvw20R73gddAGkTQezXNkdFYleIciX/WQAf7W+I ZeYw== 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=G/xMBTyIGc3/xkevi67fvj8wyXuCW/OQud7CJpQaZ3I=; b=rwFw9brKjPKs/mOaWeTNJyZeCZ1JWYXpuuMwPUGWcKySTR433tEvHqhpea5FnfHfs9 RBQ/HF2k1XRFjLxFNeA+zY0od3CdymzjO6ds1/gWdCRVoGo2pOZqNnLv8UwZohmKj0bO FQbmyUeuNDwtm84F3xpskIgq2cKtaz3wwV18Y9rQihl1gXWJbotJ92R+buy9x+mxdtlX DyLx4eXHI1tpJneeSr/fK7HRhvOjfvr4k0HZ6blX89x8x+VeAiSPyM/V/6miQqsA3SlD amGYtAf5eCZPtG57q1fJHkm7UCkd2DKfphymWoSCjufrcwnAdCnV8sRw5i4jUvOk5u65 /SfA== X-Gm-Message-State: AFqh2kodHBgwBEFPQ5YSU2le0cRBNQ8Sug1qMkAfLVGI4JZfMNzoit5q QeMFwQIRoeozrP6AT7wNZtVZyPLRyY8= X-Google-Smtp-Source: AMrXdXtJ8A+dklY8RabbB87iQeh3UphH07RFupduopzL4/GE/67V7Z6y5SX3TO8pDN528R2ZOzCaLSqapgs= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:bd0a:0:b0:7b3:f488:ea6d with SMTP id f10-20020a25bd0a000000b007b3f488ea6dmr674590ybk.311.1672967640100; Thu, 05 Jan 2023 17:14:00 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 6 Jan 2023 01:13:00 +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-28-seanjc@google.com> Subject: [PATCH v5 27/33] KVM: SVM: Require logical ID to be power-of-2 for AVIC entry 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Do not modify AVIC's logical ID table if the logical ID portion of the LDR is not a power-of-2, i.e. if the LDR has multiple bits set. Taking only the first bit means that KVM will fail to match MDAs that intersect with "higher" bits in the "ID" The "ID" acts as a bitmap, but is referred to as an ID because there's an implicit, unenforced "requirement" that software only set one bit. This edge case is arguably out-of-spec behavior, but KVM cleanly handles it in all other cases, e.g. the optimized logical map (and AVIC!) is also disabled in this scenario. Refactor the code to consolidate the checks, and so that the code looks more like avic_kick_target_vcpus_fast(). Fixes: 18f40c53e10f ("svm: Add VMEXIT handlers for AVIC") Cc: Suravee Suthikulpanit Cc: Maxim Levitsky Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 5b33f91b701c..eb979695e7d8 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -513,26 +513,26 @@ unsigned long avic_vcpu_get_apicv_inhibit_reasons(str= uct kvm_vcpu *vcpu) static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool= flat) { struct kvm_svm *kvm_svm =3D to_kvm_svm(vcpu->kvm); - int index; u32 *logical_apic_id_table; - int dlid =3D GET_APIC_LOGICAL_ID(ldr); + u32 cluster, index; =20 - if (!dlid) - return NULL; + ldr =3D GET_APIC_LOGICAL_ID(ldr); =20 - if (flat) { /* flat */ - index =3D ffs(dlid) - 1; - if (index > 7) + if (flat) { + cluster =3D 0; + } else { + cluster =3D (ldr >> 4); + if (cluster >=3D 0xf) return NULL; - } else { /* cluster */ - int cluster =3D (dlid & 0xf0) >> 4; - int apic =3D ffs(dlid & 0x0f) - 1; - - if ((apic < 0) || (apic > 7) || - (cluster >=3D 0xf)) - return NULL; - index =3D (cluster << 2) + apic; + ldr &=3D 0xf; } + if (!ldr || !is_power_of_2(ldr)) + return NULL; + + index =3D __ffs(ldr); + if (WARN_ON_ONCE(index > 7)) + return NULL; + index +=3D (cluster << 2); =20 logical_apic_id_table =3D (u32 *) page_address(kvm_svm->avic_logical_id_t= able_page); =20 --=20 2.39.0.314.g84b9a713c41-goog