Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4216711rdb; Thu, 14 Sep 2023 15:58:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHufDFcrNFBwlrvsU512G/v8xMpGnMmkBt2WbUX9wfbfySdgPIv2l1Zx+UP8VvbrUQ+VgHu X-Received: by 2002:a05:6a00:3014:b0:68f:cdb8:ae33 with SMTP id ay20-20020a056a00301400b0068fcdb8ae33mr4826376pfb.10.1694732302314; Thu, 14 Sep 2023 15:58:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694732302; cv=none; d=google.com; s=arc-20160816; b=NQ6PWQSAs7ogCne8/huoGcI5V/FbDvlSP6yaOma8g937EeaJxGIfI/fpIgdorS6fzA LTk+5zYAW3ABT0CSDoV3lHXK0mYTTyV9noFf7BLiOqHo3N5XXeSAfmnu5TUYNxGOkvV7 DlMHFHWkZnvvCyFqHzRoBJi9qJ3FhsPTffs55aXRyQ08pPDOeQTzjg3e3IWVzhRfrwEp woE8gDhw5KGhtLQ2nkd/li4bDvEHWi7A6E4PENavUJpLf3GxxKMQL7cEC6cLF+8qxFDN 9mH00AsS+5F4CioHTUhTRp1X9fn3Im40BGnp0FRGzMJAIbHBPOgjIVvLfkH59e2WkvLR XVig== 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:dkim-signature; bh=nzJvstdCVNLuwr9+zSVIDyjCzM8g0GZ73rr7XEH2PE0=; fh=OT24FjEwwODiSXbG1bLxfFmr8v5jULddwRt+0A0b4Mc=; b=TRWLo6tD5ue7yaEfLZ1TkKe+rx68sLd89L0Wcgg11QN0Z5Wjy3Dg9hJ5uDRsTcUNV0 c+KaijXSAOZvpJe+8VKgDhq6K/4ieuFyFaVhcRtAVIEzsZ+aApfQ1SuX4ZYJWBvDIWTH GN+JZroCA2XGP0yulRiGcX7uV+WUW9szqg0O8VrFsfz5nmNU29VTuERV0Aas2zsyq1x+ WqnvTeAd6Qko9LHbu6KC2zkbjDNW9MaWfhiiS55iYypvgOqi2L36d1vbjkz5+Jf/qxl5 gjsjabEO5okcHAa4crxJ2KYexCrjNRWIpmu8AM7W9LN8at4SdWx9DIJCsgL/dIzcEuE7 g/+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="VM5gsZe/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id w67-20020a636246000000b00565f617a486si2038728pgb.212.2023.09.14.15.58.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 15:58:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="VM5gsZe/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 11563836C0D4; Thu, 14 Sep 2023 13:28:48 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229804AbjINU2h (ORCPT + 99 others); Thu, 14 Sep 2023 16:28:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229655AbjINU2g (ORCPT ); Thu, 14 Sep 2023 16:28:36 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B355226BD for ; Thu, 14 Sep 2023 13:28:32 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-570428954b9so1161594a12.3 for ; Thu, 14 Sep 2023 13:28:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694723312; x=1695328112; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=nzJvstdCVNLuwr9+zSVIDyjCzM8g0GZ73rr7XEH2PE0=; b=VM5gsZe/AgVse75tZYcyCKOyRqXe9vZ5DPrqFkVSglfXiscr1hxp93GyX99Kgi+JTS 7V8UdmOrHrZdtgUMOkItvaGUqeuSQsZPV49z5U4UQ3OZnQi/QeNp5anv2ebFd7zlUtAY eF94rc+l4cRHjGkWQyxaTKtOK5BoaDuOkOGwN1jcxFguCcPhrqOa1J9zaunV1hreJX7+ 6O4abtxrL+Ms6E3o6U+Mq6tsQsVePvpRDlSRmySTauwAczqRYORzqoZFdnrtGofJ09k6 PAIdN/Fnhd3QhAl9G3A1BUE+KgDdsXB4ooR33utIsBCiG4QB+rtdLHc2tlhw26ZUooQ+ CPJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694723312; x=1695328112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nzJvstdCVNLuwr9+zSVIDyjCzM8g0GZ73rr7XEH2PE0=; b=M+oxQ3Un5ioJ7FoQJ3/A8vaVPKyi/Ym7KAwtlGLOKovSLyC4C6UK6Ye/PY+Es0tUz4 bwDVnF6g963fyViPifqF4QKG1esyXXAvBwJqhBTRqZ8orGySG3Ol8miLoAnBXhs3ohxJ f0GhwCJc8KS1pEYvdzCE61YUTZljj+vi6Rqp9eSX9PYAvvzM04bCNsCGICr69Cz+KKnJ ZXShNHWm23xaOaxXp5g7K+xMqXSs+VW2fqeflZXIROp8NNjEw2TI6iGkQmFXQ44s+8YT bFNPIpBtsM8C/4FiGWqoofovxO/GeXLlLl11ukUvm9Xvmg7E9XCAg46cVdesCgj2sk3U NMrQ== X-Gm-Message-State: AOJu0Yw3w/LW6cIMHw6ZWkx0E8PY/T9u2KmPHodewIYOGtiubhHF6M8d FbditHCsFQoO4RFpOx6On/osZtl76fE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:77c6:0:b0:563:962e:e912 with SMTP id s189-20020a6377c6000000b00563962ee912mr162430pgc.0.1694723312190; Thu, 14 Sep 2023 13:28:32 -0700 (PDT) Date: Thu, 14 Sep 2023 13:28:30 -0700 In-Reply-To: <8a5c1d2637475c7fb9657cdd6cb0e86f2bb3bab6.1694721045.git.thomas.lendacky@amd.com> Mime-Version: 1.0 References: <8a5c1d2637475c7fb9657cdd6cb0e86f2bb3bab6.1694721045.git.thomas.lendacky@amd.com> Message-ID: Subject: Re: [PATCH 1/2] KVM: SVM: Fix TSC_AUX virtualization setup From: Sean Christopherson To: Tom Lendacky Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, Paolo Bonzini , Borislav Petkov , Dave Hansen , Ingo Molnar , Thomas Gleixner , Babu Moger Content-Type: text/plain; charset="us-ascii" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 14 Sep 2023 13:28:48 -0700 (PDT) X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,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 groat.vger.email On Thu, Sep 14, 2023, Tom Lendacky wrote: > The checks for virtualizing TSC_AUX occur during the vCPU reset processing > path. However, at the time of initial vCPU reset processing, when the vCPU > is first created, not all of the guest CPUID information has been set. In > this case the RDTSCP and RDPID feature support for the guest is not in > place and so TSC_AUX virtualization is not established. > > This continues for each vCPU created for the guest. On the first boot of > an AP, vCPU reset processing is executed as a result of an APIC INIT > event, this time with all of the guest CPUID information set, resulting > in TSC_AUX virtualization being enabled, but only for the APs. The BSP > always sees a TSC_AUX value of 0 which probably went unnoticed because, > at least for Linux, the BSP TSC_AUX value is 0. > > Move the TSC_AUX virtualization enablement into the vcpu_after_set_cpuid() > path to allow for proper initialization of the support after the guest > CPUID information has been set. > > Fixes: 296d5a17e793 ("KVM: SEV-ES: Use V_TSC_AUX if available instead of RDTSC/MSR_TSC_AUX intercepts") > Signed-off-by: Tom Lendacky > --- > arch/x86/kvm/svm/sev.c | 27 +++++++++++++++++++-------- > arch/x86/kvm/svm/svm.c | 3 +++ > arch/x86/kvm/svm/svm.h | 1 + > 3 files changed, 23 insertions(+), 8 deletions(-) > > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c > index b9a0a939d59f..565c9de87c6d 100644 > --- a/arch/x86/kvm/svm/sev.c > +++ b/arch/x86/kvm/svm/sev.c > @@ -2962,6 +2962,25 @@ int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in) > count, in); > } > > +static void sev_es_init_vmcb_after_set_cpuid(struct vcpu_svm *svm) I would rather name this sev_es_after_set_cpuid() and call it directly from svm_vcpu_after_set_cpuid(). Or I suppose bounce through sev_after_set_cpuid(), but that seems gratuitous. AFAICT, there's no point in calling this from init_vmcb(); guest_cpuid_has() is guaranteed to be false when called during vCPU creation and so the intercept behavior will be correct, and even if SEV-ES called init_vmcb() from shutdown_interception(), which it doesn't, guest_cpuid_has() wouldn't change, i.e. the intercepts wouldn't need to be changed. init_vmcb_after_set_cpuid() is a special snowflake because it handles both SVM's true defaults *and* guest CPUID updates. > +{ > + struct kvm_vcpu *vcpu = &svm->vcpu; > + > + if (boot_cpu_has(X86_FEATURE_V_TSC_AUX) && > + (guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP) || > + guest_cpuid_has(vcpu, X86_FEATURE_RDPID))) { > + set_msr_interception(vcpu, svm->msrpm, MSR_TSC_AUX, 1, 1); This needs to toggled interception back on if RDTSCP and RDPID are hidden from the guest. KVM's wonderful ABI doesn't disallow multiple calls to KVM_SET_CPUID2 before KVM_RUN. > + if (guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) > + svm_clr_intercept(svm, INTERCEPT_RDTSCP); Same thing here. > + } > +}