From patchwork Fri Jan 6 01:12:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 39875 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp577512wrt; Thu, 5 Jan 2023 17:17:33 -0800 (PST) X-Google-Smtp-Source: AMrXdXs2TBN509AKHRMGgo2ukrlGScbRux5263ooxfYgBD9egSUCIVcxNmxBNGp2VHdSI9CleVWn X-Received: by 2002:a17:906:6b97:b0:805:1e6e:6777 with SMTP id l23-20020a1709066b9700b008051e6e6777mr50205407ejr.23.1672967853106; Thu, 05 Jan 2023 17:17:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672967853; cv=none; d=google.com; s=arc-20160816; b=E0Rsad413HLQXKHimPb+zkvGjtaMKB7hEAy9Yro+4CXVa8g0ze2nBi3ysASNN0+uv9 6Og6qI0UDYhisjGfL1YCJgVZGc3MN5ARxZNZ8ELEg3w9ziruDiIcZzGo5ZqpimkBATXR PUEsUQwxvhlOAYZabjO+Yf21QAlVzjL+XvzIucujufO1RC8KIs/ghGgYlvS/atHRwxk7 +KIjuW7+atGbgYYR7ddTh6Gwj2cEPATqzX7jdpfITGQFtqCPZfwKbzXyxa4YdYgtM72R rYKqozyEFE1A8EE+Y/fyJ1kgOfasqXDB5ggx9nQEoEzxG/ZvdmX268g1GDKYPn3gcRh6 RNtQ== 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=6hlceM30Jbe/aO6Zo5aK3/yVy9oHh4ItTVnXmjKPlGU=; b=tqqQ5mQwanc6eRVWE0zHgOFS3NHqxX38RvYbeI2bWAP4Znr5u4KB87ZwspCcxFPEvS H48jMTA/iVV8KMv8cjOGtXPBeqUXkQ9ziG6LnKKnNCxbXlZld0SkIzspkCEQYNImJt4q crLdXwVuM+9RXAjKNbHOwk158W5FV8QcZIA9P1AtwsZHqiXV0a4rZlBgF7ret+6ycmVA eJsH80NmXQoWIzCjVsEPu5Rs3FSiEbdxfyjghvt9JvPPdNbH0ukxTfB48Pq+oKfXX7gm JeFHVjhzWUTJtD4WQQce3TDZz7fqMubUq+oeVWtilw+NLIuYZo8SRoSsyoZ8O2DNRn1a +CWg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=DcPb1qUm; 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 qf36-20020a1709077f2400b0084d197a89e7si1047826ejc.806.2023.01.05.17.17.06; Thu, 05 Jan 2023 17:17:33 -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=DcPb1qUm; 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 S232251AbjAFBP6 (ORCPT + 99 others); Thu, 5 Jan 2023 20:15:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232489AbjAFBPQ (ORCPT ); Thu, 5 Jan 2023 20:15:16 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C93DB32245 for ; Thu, 5 Jan 2023 17:13:52 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id h14-20020a17090adb8e00b002264c50f36aso44900pjv.4 for ; Thu, 05 Jan 2023 17:13:52 -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=6hlceM30Jbe/aO6Zo5aK3/yVy9oHh4ItTVnXmjKPlGU=; b=DcPb1qUmg7YoNAiRnrOTms0+X2InGNfOnaIJCUywCz0TjNATymUn0rZht9kPh1bYoy x/0b6nsj0j5zWG9+87qJ8O3dw7Ng+4VU7nw79ltSWrfsa4ykz+gZvepfAWFi6/Gq+x+j 2vaMZW4QDtV7w9AWz3y8QOBUISI41CXh8om3PjOBKC5y4b3nhif0kTHzsczzIQs/BEU1 QIlkfaEbAYJ7y+h9iONXkUhYExHOruVjj+fgVVfDZYJ00AUV16JtavxUTNBzsjKoEe1S yzDEWgFMLeddHqAK3R7IwL8VHrb6wVwlyo5f2wkZyuxPJaPEB5unpB/ic0NyE+doq2/p qD/Q== 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=6hlceM30Jbe/aO6Zo5aK3/yVy9oHh4ItTVnXmjKPlGU=; b=nfm2aHdSC03BwysUkexb4xBQs8g9F+cCX3pN/PdNTxHxECOUa4Rhnx9HUrdzO9dI/g w9N47t1wvSB0vi3hbj49ow1ikk3On4l+3G3JSpikW7piJg60g7kv0XTeJEfgO0axqc9Z +jgjceHxW1y8oQLX0Dm1dmOow9XGTbJp0PgFwiJkjs5vf6arBbOg6VG0SzJzweFkHFDo 0TB85pm3+BTLIHuiuy/cUi7ZmoipZ4XuWPNAVUEmXbBe2loG5elJfKeEPwbL0gKTc+3P sTSIr9UDUs6XMJKaYsWYtmwTHZTfcj0QjGWk7btRLzNsj4GCgMyesqfsBms7IqdiUV66 EhHQ== X-Gm-Message-State: AFqh2kr1l4k8WvClJDXjbMNLDznLOequxl1nISev0KGWkCD0szHNvEvy PN0arW0dTnCNMWzWQeMHf/PM+5Q1EBg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a62:1584:0:b0:581:1083:2cc5 with SMTP id 126-20020a621584000000b0058110832cc5mr2680623pfv.14.1672967626186; Thu, 05 Jan 2023 17:13:46 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 6 Jan 2023 01:12:52 +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-20-seanjc@google.com> Subject: [PATCH v5 19/33] KVM: x86: Skip redundant x2APIC logical mode optimized cluster setup 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?1754233939503839957?= X-GMAIL-MSGID: =?utf-8?q?1754233939503839957?= Skip the optimized cluster[] setup for x2APIC logical mode, as KVM reuses the optimized map's phys_map[] and doesn't actually need to insert the target apic into the cluster[]. The LDR is derived from the x2APIC ID, and both are read-only in KVM, thus the vCPU's cluster[ldr] is guaranteed to be the same entry as the vCPU's phys_map[x2apic_id] entry. Skipping the unnecessary setup will allow a future fix for aliased xAPIC logical IDs to simply require that cluster[ldr] is non-NULL, i.e. won't have to special case x2APIC. Alternatively, the future check could allow "cluster[ldr] == apic", but that ends up being terribly confusing because cluster[ldr] is only set at the very end, i.e. it's only possible due to x2APIC's shenanigans. Another alternative would be to send x2APIC down a separate path _after_ the calculation and then assert that all of the above, but the resulting code is rather messy, and it's arguably unnecessary since asserting that the actual LDR matches the expected LDR means that simply testing that interrupts are delivered correctly provides the same guarantees. Reported-by: Suravee Suthikulpanit Reviewed-by: Maxim Levitsky Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 2567998b692c..fd7726ff95c6 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -166,6 +166,11 @@ static bool kvm_use_posted_timer_interrupt(struct kvm_vcpu *vcpu) return kvm_can_post_timer_interrupt(vcpu) && vcpu->mode == IN_GUEST_MODE; } +static inline u32 kvm_apic_calc_x2apic_ldr(u32 id) +{ + return ((id >> 4) << 16) | (1 << (id & 0xf)); +} + static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map, u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) { switch (map->logical_mode) { @@ -320,6 +325,18 @@ void kvm_recalculate_apic_map(struct kvm *kvm) continue; } + /* + * In x2APIC mode, the LDR is read-only and derived directly + * from the x2APIC ID, thus is guaranteed to be addressable. + * KVM reuses kvm_apic_map.phys_map to optimize logical mode + * x2APIC interrupts by reversing the LDR calculation to get + * cluster of APICs, i.e. no additional work is required. + */ + if (apic_x2apic_mode(apic)) { + WARN_ON_ONCE(ldr != kvm_apic_calc_x2apic_ldr(x2apic_id)); + continue; + } + if (WARN_ON_ONCE(!kvm_apic_map_get_logical_dest(new, ldr, &cluster, &mask))) { new->logical_mode = KVM_APIC_MODE_MAP_DISABLED; @@ -386,11 +403,6 @@ static inline void kvm_apic_set_dfr(struct kvm_lapic *apic, u32 val) atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY); } -static inline u32 kvm_apic_calc_x2apic_ldr(u32 id) -{ - return ((id >> 4) << 16) | (1 << (id & 0xf)); -} - static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u32 id) { u32 ldr = kvm_apic_calc_x2apic_ldr(id);