Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp1662784rwl; Thu, 5 Jan 2023 17:42:22 -0800 (PST) X-Google-Smtp-Source: AMrXdXvDXSoWPZsTvHZxtY+7VNMQGBeSEMi09EUcKSWz5mzOxjEAh7RAZHjlu10DPv1T2Ky+bx38 X-Received: by 2002:a17:902:efc2:b0:192:e4da:ad9a with SMTP id ja2-20020a170902efc200b00192e4daad9amr9039994plb.11.1672969342590; Thu, 05 Jan 2023 17:42:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672969342; cv=none; d=google.com; s=arc-20160816; b=LaK3Gridxy03DtPczFMFFhx/qPSXUP9Qnr2KMzHeK76ukefkY9XrkmBkk8iIq4HM3m BiBw/65AUhhFW5Z+sinp7bFSxNsAt/yJGqkN46gGgaOcYxm3GHBpzoKBZLKdu9HNZRRU RQBVDTswZ8kAgfeq4tC5lwiiI9VAaY7pwQCNsp41lmgI9QxxMN4tuYrQAN2PNFYTi8rR OllPELl2UDr2UP/lwaK04wyWZxpR5h8SkJqJIhG0tx6ibJgPd90mi+CF5/zH3mg5K3yo J95wlH6GlUrsxHkOk5f9qGqBOW5VCbMjhBakRcEPI/2lY8XkPIC4DonQJO7730wAmxpq /NqQ== 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=kgKvdTfQ27wv6seVSF5aAgO210OBrcuZvgQgigmAgek=; b=kJtBGLC6OimQ+LXR1EKui1tAcL/JbwqeaE+GhMsLdvX2fTn6sVXQw7oKhMwl1MM9P3 N9jFRl2JIG8erCB88dHJ4INimQh0gLjG819x7GxCFFUOKBvhw3Byx5Ucfn9MhkV/VJZi HElNQ4XWYTNd5ifHRtBKlFg/VPdKj6JZIe3wFpGLhqMecl90PKmygmIypgpNjALpqIP0 c9CiQUEZ2iVQILiE68JxcyrcxbGVTh83fSXkJ1EdisrlatuejaaTzPWHogglpDD29ir1 RMLgR0B4XOyt5HBwiCom0//7wroLc5O96hqAZESnD+jXNT37P7xnZhLy8Hsm84fj0vms 2+uA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=WJv9DzW7; 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 m14-20020a170902f64e00b001929101a655si27019112plg.248.2023.01.05.17.42.15; Thu, 05 Jan 2023 17:42:22 -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=WJv9DzW7; 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 S232228AbjAFBRX (ORCPT + 56 others); Thu, 5 Jan 2023 20:17:23 -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 S232168AbjAFBQ2 (ORCPT ); Thu, 5 Jan 2023 20:16:28 -0500 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C669D65AEB for ; Thu, 5 Jan 2023 17:14:21 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id bx21-20020a056a00429500b00582d85bb03bso35761pfb.16 for ; Thu, 05 Jan 2023 17:14:21 -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=kgKvdTfQ27wv6seVSF5aAgO210OBrcuZvgQgigmAgek=; b=WJv9DzW7OiR5WxhCTMswF2puPwUNWRNLasJ4h3JJ3mwxe13GYAmZ+MCwjs0kXjBg0v Mv0JiXikj5UXUyf3Q7VFTI2RxiiDCS1Aq09YHScmHzn6YK2l0qRFB9713Vqmf/At92QU VPHkLv78SiClzvEy/3z0eV1D60+5F5LDoiilczrSvFYkD9QMY8xo+LGRGsiTe9hsxoWp U2Z8DnSXNNc0TXoqRwMqEuldIle0PtrgK4nbuO8TK4nvH+DBn6o0uAMPPiU4ZIWgCCOe 0ucXFRf10VCnvSydh5GnIZD2vQJCE6fnMFBK+e5WubcHBAvjx8hJUemWLu/Fwlgdffie Qnxw== 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=kgKvdTfQ27wv6seVSF5aAgO210OBrcuZvgQgigmAgek=; b=XBSoV6r9CIjxH/qAThfRr2oMYH7Pjs1zD2hrcDtmcTZFgkVHryxfe9NoEk5flUPwev 7ttxp2EsJ8AQjcg+lJdLJAStwxm+SJKvVUKY1yTOeqxioh1GsSg8ONWArOO2zp2i+ZZ+ bMw2NG/zCGSEaYZAUQCzKhTpQTWrJjcuQO20gBhnAZFGl+dOeByvk7G21TyclUbbHO3K E18pTkKj4Sp97Hxe9lPptVvXPv02ZgTKoMLm52c3uHZ5g7ieo56XU6GkHRP+zz4vZy0K TviYQFe3FCAP2yMiRMINRAL2DoVactlhviS+Niq9UFQAWFhdRXxGpsrWNMWrX1+T4peo erlw== X-Gm-Message-State: AFqh2krOnLFT7flA70kPZkif+2Cy2lSgecC0lF/zIbe7uyxkblj4PLcm U3zA57AHfIMkGDpEi3EXnz9MRxhWvLI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:bd12:b0:225:b164:8874 with SMTP id y18-20020a17090abd1200b00225b1648874mr4020777pjr.87.1672967641623; Thu, 05 Jan 2023 17:14:01 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 6 Jan 2023 01:13:01 +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-29-seanjc@google.com> Subject: [PATCH v5 28/33] KVM: SVM: Handle multiple logical targets in AVIC kick fastpath 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 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 Iterate over all target logical IDs in the AVIC kick fastpath instead of bailing if there is more than one target. Now that KVM inhibits AVIC if vCPUs aren't mapped 1:1 with logical IDs, each bit in the destination is guaranteed to match to at most one vCPU, i.e. iterating over the bitmap is guaranteed to kick each valid target exactly once. Reviewed-by: Maxim Levitsky Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 112 ++++++++++++++++++++++------------------ 1 file changed, 63 insertions(+), 49 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index eb979695e7d8..2c6737f72bd4 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -327,6 +327,50 @@ static void avic_kick_vcpu(struct kvm_vcpu *vcpu, u32 icrl) icrl & APIC_VECTOR_MASK); } +static void avic_kick_vcpu_by_physical_id(struct kvm *kvm, u32 physical_id, + u32 icrl) +{ + /* + * KVM inhibits AVIC if any vCPU ID diverges from the vCPUs APIC ID, + * i.e. APIC ID == vCPU ID. + */ + struct kvm_vcpu *target_vcpu = kvm_get_vcpu_by_id(kvm, physical_id); + + /* Once again, nothing to do if the target vCPU doesn't exist. */ + if (unlikely(!target_vcpu)) + return; + + avic_kick_vcpu(target_vcpu, icrl); +} + +static void avic_kick_vcpu_by_logical_id(struct kvm *kvm, u32 *avic_logical_id_table, + u32 logid_index, u32 icrl) +{ + u32 physical_id; + + if (avic_logical_id_table) { + u32 logid_entry = avic_logical_id_table[logid_index]; + + /* Nothing to do if the logical destination is invalid. */ + if (unlikely(!(logid_entry & AVIC_LOGICAL_ID_ENTRY_VALID_MASK))) + return; + + physical_id = logid_entry & + AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK; + } else { + /* + * For x2APIC, the logical APIC ID is a read-only value that is + * derived from the x2APIC ID, thus the x2APIC ID can be found + * by reversing the calculation (stored in logid_index). Note, + * bits 31:20 of the x2APIC ID aren't propagated to the logical + * ID, but KVM limits the x2APIC ID limited to KVM_MAX_VCPU_IDS. + */ + physical_id = logid_index; + } + + avic_kick_vcpu_by_physical_id(kvm, physical_id, icrl); +} + /* * A fast-path version of avic_kick_target_vcpus(), which attempts to match * destination APIC ID to vCPU without looping through all vCPUs. @@ -334,11 +378,10 @@ static void avic_kick_vcpu(struct kvm_vcpu *vcpu, u32 icrl) static int avic_kick_target_vcpus_fast(struct kvm *kvm, struct kvm_lapic *source, u32 icrl, u32 icrh, u32 index) { - u32 l1_physical_id, dest; - struct kvm_vcpu *target_vcpu; int dest_mode = icrl & APIC_DEST_MASK; int shorthand = icrl & APIC_SHORT_MASK; struct kvm_svm *kvm_svm = to_kvm_svm(kvm); + u32 dest; if (shorthand != APIC_DEST_NOSHORT) return -EINVAL; @@ -355,14 +398,14 @@ static int avic_kick_target_vcpus_fast(struct kvm *kvm, struct kvm_lapic *source if (!apic_x2apic_mode(source) && dest == APIC_BROADCAST) return -EINVAL; - l1_physical_id = dest; - - if (WARN_ON_ONCE(l1_physical_id != index)) + if (WARN_ON_ONCE(dest != index)) return -EINVAL; + avic_kick_vcpu_by_physical_id(kvm, dest, icrl); } else { - u32 bitmap, cluster; - int logid_index; + u32 *avic_logical_id_table; + unsigned long bitmap, i; + u32 cluster; if (apic_x2apic_mode(source)) { /* 16 bit dest mask, 16 bit cluster id */ @@ -382,50 +425,21 @@ static int avic_kick_target_vcpus_fast(struct kvm *kvm, struct kvm_lapic *source if (unlikely(!bitmap)) return 0; - if (!is_power_of_2(bitmap)) - /* multiple logical destinations, use slow path */ - return -EINVAL; - - logid_index = cluster + __ffs(bitmap); - - if (apic_x2apic_mode(source)) { - /* - * For x2APIC, the logical APIC ID is a read-only value - * that is derived from the x2APIC ID, thus the x2APIC - * ID can be found by reversing the calculation (done - * above). Note, bits 31:20 of the x2APIC ID are not - * propagated to the logical ID, but KVM limits the - * x2APIC ID limited to KVM_MAX_VCPU_IDS. - */ - l1_physical_id = logid_index; - } else { - u32 *avic_logical_id_table = - page_address(kvm_svm->avic_logical_id_table_page); - - u32 logid_entry = avic_logical_id_table[logid_index]; - - if (WARN_ON_ONCE(index != logid_index)) - return -EINVAL; - - /* Nothing to do if the logical destination is invalid. */ - if (unlikely(!(logid_entry & AVIC_LOGICAL_ID_ENTRY_VALID_MASK))) - return 0; - - l1_physical_id = logid_entry & - AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK; - } + if (apic_x2apic_mode(source)) + avic_logical_id_table = NULL; + else + avic_logical_id_table = page_address(kvm_svm->avic_logical_id_table_page); + + /* + * AVIC is inhibited if vCPUs aren't mapped 1:1 with logical + * IDs, thus each bit in the destination is guaranteed to map + * to at most one vCPU. + */ + for_each_set_bit(i, &bitmap, 16) + avic_kick_vcpu_by_logical_id(kvm, avic_logical_id_table, + cluster + i, icrl); } - /* - * KVM inhibits AVIC if any vCPU ID diverges from the vCPUs APIC ID, - * i.e. APIC ID == vCPU ID. Once again, nothing to do if the target - * vCPU doesn't exist. - */ - target_vcpu = kvm_get_vcpu_by_id(kvm, l1_physical_id); - if (unlikely(!target_vcpu)) - return 0; - - avic_kick_vcpu(target_vcpu, icrl); return 0; } -- 2.39.0.314.g84b9a713c41-goog