Message ID | 20230106011306.85230-28-seanjc@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp577815wrt; Thu, 5 Jan 2023 17:18:28 -0800 (PST) X-Google-Smtp-Source: AMrXdXv2K8FLhPrwsBRSoV8tTf5nyWxxFPbcFXqPV6vG/jmIDzXUrdJkxhIlBixhZPzRy3YIaYoI X-Received: by 2002:a17:902:c9d2:b0:192:ee98:664c with SMTP id q18-20020a170902c9d200b00192ee98664cmr6280088pld.54.1672967908605; Thu, 05 Jan 2023 17:18:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672967908; cv=none; d=google.com; s=arc-20160816; b=ShKyaZbEU3od46X3wDd1p3T07t7/Toi2/Hof0HCxrXhuwp0urK06hpZk1igTv37X0P EfNtNqUnEstQLgoJ/FlWGuNqL2Fa/uMsfCfzxjjMwZ2VIu83FbewD9HlJftl2fsHmdxE Xx4biw26MXA8vlb/i3lVRMREhevJAX7rVD3OQ4JoYSwu50ezCEC2AnWTl3kWxBzSGVIE BNvzXDtSFEHHtN4rjM4cnEjtm2U97rJNHv2UqI8EaXMV3UQc+ACmET4uRcfrM9kg5O/7 hHtiSwyged6/SMwE9sHG7pIVDD/VZkWd6XvvMnVKY+fb5so2SfSWLvcHmTx5ixKfk4ZB nMkA== 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=G/xMBTyIGc3/xkevi67fvj8wyXuCW/OQud7CJpQaZ3I=; b=AreGkL6pIYl4CEHp4hbLVsdNKotOuMRZSeMt8TTvz/nxqrQhUMJLPfqSCofE6kl1RD mxerVwBRIoPvv2FUJpIWIwG0NaA0H+vS8lH094H4PE+Ec06/jSjXyGN9pYg7ml76rvGy BPETCcaDgUogE/jSGY9yhZSjNlP6rYlPdf5UZ2K9Jrun2JLAlI8TAQlJWNyosGDJav9j qn5S9RgvfKBy37FvDYTdV4WX5obU401PmDCw4jdH39m8DbSsfZl5xf78Uz/Ph8VJzso0 DH3gS1KbXfeuI8XjAq/Rqxlz+boJ51id5RSlQT3HSpaknQeL+wuFha8EBbdtDDJ3svzg 1zvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=d6QlI5kw; 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 e6-20020a170902784600b001890f5957c4si12750851pln.353.2023.01.05.17.18.16; Thu, 05 Jan 2023 17:18:28 -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=d6QlI5kw; 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 S233744AbjAFBRO (ORCPT <rfc822;tmhikaru@gmail.com> + 99 others); Thu, 5 Jan 2023 20:17:14 -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 <rfc822;linux-kernel@vger.kernel.org>); 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 <linux-kernel@vger.kernel.org>; Thu, 5 Jan 2023 17:14:20 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id k18-20020a25e812000000b0077cc9ab9dd9so425086ybd.8 for <linux-kernel@vger.kernel.org>; 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-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 <seanjc@google.com> 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 <seanjc@google.com> To: Sean Christopherson <seanjc@google.com>, Paolo Bonzini <pbonzini@redhat.com> Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Alejandro Jimenez <alejandro.j.jimenez@oracle.com>, Maxim Levitsky <mlevitsk@redhat.com>, Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>, Li RongQing <lirongqing@baidu.com>, Greg Edwards <gedwards@ddn.com> 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754233997652758959?= X-GMAIL-MSGID: =?utf-8?q?1754233997652758959?= |
Series |
KVM: x86: AVIC and local APIC fixes+cleanups
|
|
Commit Message
Sean Christopherson
Jan. 6, 2023, 1:13 a.m. UTC
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 <suravee.suthikulpanit@amd.com>
Cc: Maxim Levitsky <mlevitsk@redhat.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
arch/x86/kvm/svm/avic.c | 30 +++++++++++++++---------------
1 file changed, 15 insertions(+), 15 deletions(-)
Comments
On Fri, 2023-01-06 at 01:13 +0000, Sean Christopherson wrote: > 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(). Very minor nitpick about the subject, it feels like it just changes something minor but it pretty much rewrites the avic_get_logical_id_entry. When I bisected for the bug, this did confuse me a bit. I don't have a good idea on how to call this patch so I won't object to the current subject to stay as well. Best regards, Maxim Levitsky > > Fixes: 18f40c53e10f ("svm: Add VMEXIT handlers for AVIC") > Cc: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com> > Cc: Maxim Levitsky <mlevitsk@redhat.com> > Signed-off-by: Sean Christopherson <seanjc@google.com> > --- > 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(struct kvm_vcpu *vcpu) > static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat) > { > struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm); > - int index; > u32 *logical_apic_id_table; > - int dlid = GET_APIC_LOGICAL_ID(ldr); > + u32 cluster, index; > > - if (!dlid) > - return NULL; > + ldr = GET_APIC_LOGICAL_ID(ldr); > > - if (flat) { /* flat */ > - index = ffs(dlid) - 1; > - if (index > 7) > + if (flat) { > + cluster = 0; > + } else { > + cluster = (ldr >> 4); > + if (cluster >= 0xf) > return NULL; > - } else { /* cluster */ > - int cluster = (dlid & 0xf0) >> 4; > - int apic = ffs(dlid & 0x0f) - 1; > - > - if ((apic < 0) || (apic > 7) || > - (cluster >= 0xf)) > - return NULL; > - index = (cluster << 2) + apic; > + ldr &= 0xf; > } > + if (!ldr || !is_power_of_2(ldr)) > + return NULL; > + > + index = __ffs(ldr); > + if (WARN_ON_ONCE(index > 7)) > + return NULL; > + index += (cluster << 2); > > logical_apic_id_table = (u32 *) page_address(kvm_svm->avic_logical_id_table_page); > Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com> Best regards, Maxim Levitsky
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(struct kvm_vcpu *vcpu) static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat) { struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm); - int index; u32 *logical_apic_id_table; - int dlid = GET_APIC_LOGICAL_ID(ldr); + u32 cluster, index; - if (!dlid) - return NULL; + ldr = GET_APIC_LOGICAL_ID(ldr); - if (flat) { /* flat */ - index = ffs(dlid) - 1; - if (index > 7) + if (flat) { + cluster = 0; + } else { + cluster = (ldr >> 4); + if (cluster >= 0xf) return NULL; - } else { /* cluster */ - int cluster = (dlid & 0xf0) >> 4; - int apic = ffs(dlid & 0x0f) - 1; - - if ((apic < 0) || (apic > 7) || - (cluster >= 0xf)) - return NULL; - index = (cluster << 2) + apic; + ldr &= 0xf; } + if (!ldr || !is_power_of_2(ldr)) + return NULL; + + index = __ffs(ldr); + if (WARN_ON_ONCE(index > 7)) + return NULL; + index += (cluster << 2); logical_apic_id_table = (u32 *) page_address(kvm_svm->avic_logical_id_table_page);