Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp4894482iog; Wed, 22 Jun 2022 07:51:14 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uUJwE+7IEyCLfQMu9CnP3D/XWBJis13SUeW3vK3kr25KP2I71TJ3mY7EUW96Cra+E4E2ZS X-Received: by 2002:a63:1411:0:b0:402:de25:6c0a with SMTP id u17-20020a631411000000b00402de256c0amr3203923pgl.501.1655909474210; Wed, 22 Jun 2022 07:51:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655909474; cv=none; d=google.com; s=arc-20160816; b=bjjxUayG3PRLlJoB77bHDx0D5SqzQE/FigpM6+vjdyp0l6g8qkR71xfK9G72gACH79 ZcopJe8HzG8CsRmjYDtHNQkeD1I6aGvwst3EIBp7NsfKcxMHJPMoQjzBLsrjvQOjxhDQ eA1OHVS8ze/v6BSUpl/kp8lUTx52tCLPpbb+G6tcmjPIrlfaUjOB5VagUcZR9ZNRKJaf xdNz1IrOGbGhynjsMaaJ3T0Eix7A8Poc1H+XHBomebMNWxx6wpo84rM2SRhAaFa2hZFl KbHHB1lx8SbZW4cAJY0qDnmAlvMKgC27BY9tL3Xv7u2PkWlcdwFmNYwh9OaC34GUFfkQ JTKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=mrZzVT+DaE4dzWmgVMu4jK9g1YT74nIx3X+44/NAXQg=; b=wE5J7wnHSct+yhg602Q7VtQYh0Zw7AudcYpPEv1Tg0LLezuvK9la0+9sWgh8LmrxVW yiLhZhEmlv8fC/MsDy2umTD2OKVld6Ul2TW1GEjRTXD2CXP+4GiaF1ZHhtDtlvZpCSBg EPtfenWqmUtg3hD+Fff6jCavICCyaxjts4/h4pHA3fW7GlaqrBqKpcymiWFYveeH951Y LCgZU1Rb4eBM85Uyk+SMY+qdKfYEPtbwKnmk3zCHPkbO1+ByIMTzUXkfqSIzVmOCHCQC SqnNWdM86bTwn+04SXBlibVm1ZqXfvsuEC4nyVgKpt0C/qJoq1/OihCAxIXSyUxhgej/ 7ulg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=IC6NzNNl; 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 q19-20020a63cc53000000b003c1ff3d56a4si20184202pgi.386.2022.06.22.07.50.59; Wed, 22 Jun 2022 07:51:14 -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=IC6NzNNl; 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 S1358561AbiFVOo3 (ORCPT + 99 others); Wed, 22 Jun 2022 10:44:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359016AbiFVOoM (ORCPT ); Wed, 22 Jun 2022 10:44:12 -0400 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6AA68A18B for ; Wed, 22 Jun 2022 07:44:11 -0700 (PDT) Received: by mail-pg1-x533.google.com with SMTP id e63so14790507pgc.5 for ; Wed, 22 Jun 2022 07:44:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=mrZzVT+DaE4dzWmgVMu4jK9g1YT74nIx3X+44/NAXQg=; b=IC6NzNNl5plJ+Mah/Ys88FlUV0HKR4UEDn+8EWcpGwszqNs2BCiMWrbslIcRr3Ye9p bskeqLhIOV4fRI5pqFF/KtQ62BimoINWtFRayWhif8S+17Oq+91t5AROfQvGt0NMd9qj SDt96e1VcQw00j/SpEZ9GxD9rag0iJrtw4BHrraKeK0UFbyc3XkhqcA37Khuw9scLhj1 J9h4Cbe+wQwktNBrLvovwyOYtB9uAtQwonLec5SuXU4hB4qdtTaFGFAiK6ESJVWnTjeh XHt8ukQkvuRO5Lj46bkGHDdhqgG1GPVH9aAwbQPHgfeUcxdjQFdRVehf5er9aSQydhje OIgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=mrZzVT+DaE4dzWmgVMu4jK9g1YT74nIx3X+44/NAXQg=; b=KZQRZBVRVrcZaOMrKM6WI2hzxQ+A/VxrXqW9JZ652OamTFrHetj4yKH0dvEvV3NoE2 rvCKvrFLchhu/A22ZC/vIDJclv2vjM+ecFlD5IRYi0p26BgWVc9qKRixLa2q8L/WNx+n 6Wd5Gn6snQHaK7C4Io4r51kLhxtWn0nR4nbb2jRoXK9yRicvTlrhP19XJkPZ43lf7M9A 3vKWMraOfB4K9xiTakNW/pAmli/0s0NlbeKuOWRUvZyyAUji2qyGTqYLSilsuHmc2/Gr WqRKwdhRVOSj8pNrJv+2wyPqE/kJOuE343yqqEPW9REUP3suL6lGD5Qn0kQjdoP2DMKz lljg== X-Gm-Message-State: AJIora+qI3VTFz/KZq7be0GzM7xjvNEmwQBGTEGMz0PXGC6byZMCZ20l equZ3y3GlcQZY3tLFuFfQVwMSw== X-Received: by 2002:a63:555d:0:b0:3fd:5d54:2708 with SMTP id f29-20020a63555d000000b003fd5d542708mr3176104pgm.92.1655909050732; Wed, 22 Jun 2022 07:44:10 -0700 (PDT) Received: from google.com (123.65.230.35.bc.googleusercontent.com. [35.230.65.123]) by smtp.gmail.com with ESMTPSA id ik15-20020a170902ab0f00b0015e8d4eb2d8sm12852644plb.290.2022.06.22.07.44.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Jun 2022 07:44:09 -0700 (PDT) Date: Wed, 22 Jun 2022 14:44:04 +0000 From: Sean Christopherson To: Paul Durrant Cc: x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: Re: [PATCH] KVM: x86/xen: Update Xen CPUID Leaf 4 (tsc info) sub-leaves, if present Message-ID: References: <20220622092202.15548-1-pdurrant@amazon.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220622092202.15548-1-pdurrant@amazon.com> X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham 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 On Wed, Jun 22, 2022, Paul Durrant wrote: > The scaling information in sub-leaf 1 should match the values in the > 'vcpu_info' sub-structure 'time_info' (a.k.a. pvclock_vcpu_time_info) which > is shared with the guest. The offset values are not set since a TSC offset > is already applied. > The host TSC frequency should also be set in sub-leaf 2. Explain why this is KVM's problem, i.e. why userspace is unable to set the correct values. > This patch adds a new kvm_xen_set_cpuid() function that scans for the Please avoid "This patch". > relevant CPUID leaf when the CPUID information is updated by the VMM and > stashes pointers to the sub-leaves in the kvm_vcpu_xen structure. > The values are then updated by a call to the, also new, > kvm_xen_setup_tsc_info() function made at the end of > kvm_guest_time_update() just before entering the guest. This is not a helpful paragraph, it provides zero information that isn't obvious from the code. The changelog should read something like: Update Xen CPUID leaves that expose TSC frequency and scaling information to the guest . Cache the leaves . > Signed-off-by: Paul Durrant > --- > arch/x86/include/asm/kvm_host.h | 2 ++ > arch/x86/kvm/cpuid.c | 2 ++ > arch/x86/kvm/x86.c | 1 + > arch/x86/kvm/xen.c | 41 +++++++++++++++++++++++++++++++++ > arch/x86/kvm/xen.h | 10 ++++++++ > 5 files changed, 56 insertions(+) > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h > index 1038ccb7056a..f77a4940542f 100644 > --- a/arch/x86/include/asm/kvm_host.h > +++ b/arch/x86/include/asm/kvm_host.h > @@ -638,6 +638,8 @@ struct kvm_vcpu_xen { > struct hrtimer timer; > int poll_evtchn; > struct timer_list poll_timer; > + struct kvm_cpuid_entry2 *tsc_info_1; > + struct kvm_cpuid_entry2 *tsc_info_2; > }; > > struct kvm_vcpu_arch { > diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c > index d47222ab8e6e..eb6cd88c974a 100644 > --- a/arch/x86/kvm/cpuid.c > +++ b/arch/x86/kvm/cpuid.c > @@ -25,6 +25,7 @@ > #include "mmu.h" > #include "trace.h" > #include "pmu.h" > +#include "xen.h" > > /* > * Unlike "struct cpuinfo_x86.x86_capability", kvm_cpu_caps doesn't need to be > @@ -310,6 +311,7 @@ static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) > __cr4_reserved_bits(guest_cpuid_has, vcpu); > > kvm_hv_set_cpuid(vcpu); > + kvm_xen_set_cpuid(vcpu); > > /* Invoke the vendor callback only after the above state is updated. */ > static_call(kvm_x86_vcpu_after_set_cpuid)(vcpu); > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index 00e23dc518e0..8b45f9975e45 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -3123,6 +3123,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) > if (vcpu->xen.vcpu_time_info_cache.active) > kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_time_info_cache, 0); > kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock); > + kvm_xen_setup_tsc_info(v); This can be called inside this if statement, no? if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) { } > return 0; > } > > diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c > index 610beba35907..a016ff85264d 100644 > --- a/arch/x86/kvm/xen.c > +++ b/arch/x86/kvm/xen.c > @@ -10,6 +10,9 @@ > #include "xen.h" > #include "hyperv.h" > #include "lapic.h" > +#include "cpuid.h" > + > +#include > > #include > #include > @@ -1855,3 +1858,41 @@ void kvm_xen_destroy_vm(struct kvm *kvm) > if (kvm->arch.xen_hvm_config.msr) > static_branch_slow_dec_deferred(&kvm_xen_enabled); > } > + > +void kvm_xen_set_cpuid(struct kvm_vcpu *vcpu) This is a very, very misleading name. It does not "set" anything. Given that this patch adds "set" and "setup", I expected the "set" to you know, set the CPUID leaves and the "setup" to prepar for that, not the other way around. If the leaves really do need to be cached, kvm_xen_after_set_cpuid() is probably the least awful name. > +{ > + u32 base = 0; > + u32 function; > + > + for_each_possible_hypervisor_cpuid_base(function) { > + struct kvm_cpuid_entry2 *entry = kvm_find_cpuid_entry(vcpu, function, 0); > + > + if (entry && > + entry->ebx == XEN_CPUID_SIGNATURE_EBX && > + entry->ecx == XEN_CPUID_SIGNATURE_ECX && > + entry->edx == XEN_CPUID_SIGNATURE_EDX) { > + base = function; > + break; > + } > + } > + if (!base) > + return; > + > + function = base | XEN_CPUID_LEAF(3); > + vcpu->arch.xen.tsc_info_1 = kvm_find_cpuid_entry(vcpu, function, 1); > + vcpu->arch.xen.tsc_info_2 = kvm_find_cpuid_entry(vcpu, function, 2); Is it really necessary to cache the leave? Guest CPUID isn't optimized, but it's not _that_ slow, and unless I'm missing something updating the TSC frequency and scaling info should be uncommon, i.e. not performance critical.