Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4727236rdb; Fri, 15 Sep 2023 10:21:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGAbmhhnxbG/g6zdX821Phidx3nOK7pzyl+U10K6+amd0ORJ8c6SK6mpDrnaUCDk7TOcQwc X-Received: by 2002:a05:6a20:e10b:b0:155:1221:a3d5 with SMTP id kr11-20020a056a20e10b00b001551221a3d5mr2983597pzb.5.1694798482201; Fri, 15 Sep 2023 10:21:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694798482; cv=none; d=google.com; s=arc-20160816; b=rn7Tv7/cEr3xbiqq8HuoZJ4gkqDvM6tW0EPz20abnQG8ug5PHfrjOOGq2zCxvUhdLG P1SH9qs4VYRHV6QxuC7noJCMo/NqAed18lTDe49/AJZBTKcmWFVev0plGDqiW7fEVqy+ 6FkJCJwFpdNNDTK+WNWUDGFLmMLO84kYj+Gx05YtbtzrpqCRmyIPPBOdXykf5Hi5NAhF smZ2qDy/lbOEsrU6P2MpTxi1YYVXwxRnofwWbp0IKQDCFtskADMZ3Ape5zjN2RWGalu1 Vx4CKL3siZn/TiHM0+JGEoc3f7OMHFbQ6E/SmCj6/sa7x0c2w7ztMYGnv1tHRM6Q/dfi W5KA== 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=pxBgjf4cFGqduUhZ9Ofgvxr6N7enlbl7a5N9uijxEC4=; fh=OT24FjEwwODiSXbG1bLxfFmr8v5jULddwRt+0A0b4Mc=; b=PZHpjNSeKJlYIQocc2p2kTS6RN0ikRnXlxCwgcOOtyya9KG42BxXFFrYRKm1QwCIpu SkrS1fBytSJfdASgkyugvvm23lVGcJSt2J/drZP4dQ08bG84Ylg3L+rxR48QUjp/n2Fw 1knimOf2rN3h1p5/LXlipoRz+pAu8Q4YmejaRcul6BecPpHBNLZ/d4+Kf+lIkarQxPqP wu+viHBvCO0+P/kQCaCa5crnnDJdcAFxQVHaMqa/ir3TOcYWrPCs9JgeacF5OwzTBrsS UHru6cbOV/wbvADzbNCMMppL66ZQpUKDTAQb/IwYLQUzwcefdCon7N04T+v0rzTTW5XV cunw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=lRIoQDzj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id c5-20020a6566c5000000b0057832accf0esi780053pgw.77.2023.09.15.10.21.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 10:21:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=lRIoQDzj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (Postfix) with ESMTP id A0C23803388B; Fri, 15 Sep 2023 07:52:11 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235862AbjIOOwI (ORCPT + 99 others); Fri, 15 Sep 2023 10:52:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235733AbjIOOwH (ORCPT ); Fri, 15 Sep 2023 10:52:07 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6200B1BE6 for ; Fri, 15 Sep 2023 07:52:02 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-59b5d4a8242so28190937b3.0 for ; Fri, 15 Sep 2023 07:52:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694789521; x=1695394321; 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=pxBgjf4cFGqduUhZ9Ofgvxr6N7enlbl7a5N9uijxEC4=; b=lRIoQDzjFLY03ilV7tTmj67bO7QNiEYa0foiClX7X9p0ylZEMBPmxWKJBoM4nbbeaC vrRDd+3t5VnSD1YTU96pFV1FH6p6HEqTMvqLLGqbvQu5BbNhXXfKbfCQ0jzsPhBuX0MA aT3wrYRGS2tI2OqjFufYWBFo7P/8cNYxks4Bd9Gao2CKyDOxkx5O/89C9ZUuhn65zKDd Jobj3xrGpMFT/lr7ImUjFHnIeBw/1y32IWAl2xlk2HOMcZ8ohiXzwmjZKBPWRigFQ71L 5CfzYqNdru/FY7KKfD8kQC7RpLw6i8627LZGjLSjabQqCxN8Cv6WfB4gDxJcINAQhBYg lA8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694789521; x=1695394321; 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=pxBgjf4cFGqduUhZ9Ofgvxr6N7enlbl7a5N9uijxEC4=; b=D8q5mhUYPYtS+oP78AN1HBeJAdD1hVxF8kz9ZHwBbRbdad7vnP1VuI5FuYx5ZaTAxL I0rGBGA2CezeLiTvN+oqT1hmsa1uRoUuPPUeJo1ebAYheZ4bGj8pa5N/IM5/VopcAs35 Y+wC/XFSA3XVimZFXV7eHwlA8jar4WngVVjSrwnOtVcpkzwFsCbDEoeffLIjpx6h2wCM b61VTumUVzS6KAV3hS+JHpqEoyqSfpdvrmuLMVofAvRE5ltgwBoA2ttPk3lJw2ieiC2A AlxcBBOWRTSe7rJu7O2Hhu6bmo6KtSSaAFBTxPOy8zPTY4BH976EfwLTgZkj4+NtSgDr keKA== X-Gm-Message-State: AOJu0YwxfpkL+jc71AlPdk1BT3OWu7/yHj4lgsgl8r4p1/jPPZ3sw8Tm oOX67MGaD/kcmWnWXAV3VDpzZsT4630= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:af28:0:b0:584:3d8f:a425 with SMTP id n40-20020a81af28000000b005843d8fa425mr51269ywh.10.1694789521683; Fri, 15 Sep 2023 07:52:01 -0700 (PDT) Date: Fri, 15 Sep 2023 07:51:59 -0700 In-Reply-To: Mime-Version: 1.0 References: <025fd734d35acbbbbca74c4b3ed671a02d4af628.1694721045.git.thomas.lendacky@amd.com> Message-ID: Subject: Re: [PATCH 2/2] KVM: SVM: Do not use user return MSR support for virtualized TSC_AUX 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" 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 pete.vger.email 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 (pete.vger.email [0.0.0.0]); Fri, 15 Sep 2023 07:52:12 -0700 (PDT) On Fri, Sep 15, 2023, Sean Christopherson wrote: > On Thu, Sep 14, 2023, Tom Lendacky wrote: > > When the TSC_AUX MSR is virtualized, the TSC_AUX value is swap type "B" > > within the VMSA. This means that the guest value is loaded on VMRUN and > > the host value is restored from the host save area on #VMEXIT. > > > > Since the value is restored on #VMEXIT, the KVM user return MSR support > > for TSC_AUX can be replaced by populating the host save area with current > > host value of TSC_AUX. This replaces two WRMSR instructions with a single > > RDMSR instruction. > > > > Signed-off-by: Tom Lendacky > > --- > > arch/x86/kvm/svm/sev.c | 14 +++++++++++++- > > arch/x86/kvm/svm/svm.c | 26 ++++++++++++++++---------- > > arch/x86/kvm/svm/svm.h | 4 +++- > > 3 files changed, 32 insertions(+), 12 deletions(-) > > > > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c > > index 565c9de87c6d..1bbaae2fed96 100644 > > --- a/arch/x86/kvm/svm/sev.c > > +++ b/arch/x86/kvm/svm/sev.c > > @@ -2969,6 +2969,7 @@ static void sev_es_init_vmcb_after_set_cpuid(struct vcpu_svm *svm) > > if (boot_cpu_has(X86_FEATURE_V_TSC_AUX) && > > (guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP) || > > guest_cpuid_has(vcpu, X86_FEATURE_RDPID))) { > > + svm->v_tsc_aux = true; > > set_msr_interception(vcpu, svm->msrpm, MSR_TSC_AUX, 1, 1); > > if (guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) > > svm_clr_intercept(svm, INTERCEPT_RDTSCP); > > @@ -3071,8 +3072,10 @@ void sev_es_vcpu_reset(struct vcpu_svm *svm) > > sev_enc_bit)); > > } > > > > -void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa) > > +void sev_es_prepare_switch_to_guest(struct vcpu_svm *svm, struct sev_es_save_area *hostsa) > > { > > + u32 msr_hi; > > + > > /* > > * All host state for SEV-ES guests is categorized into three swap types > > * based on how it is handled by hardware during a world switch: > > @@ -3109,6 +3112,15 @@ void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa) > > hostsa->dr2_addr_mask = amd_get_dr_addr_mask(2); > > hostsa->dr3_addr_mask = amd_get_dr_addr_mask(3); > > } > > + > > + /* > > + * If TSC_AUX virtualization is enabled, MSR_TSC_AUX is loaded but NOT > > + * saved by the CPU (Type-B). If TSC_AUX is not virtualized, the user > > + * return MSR support takes care of restoring MSR_TSC_AUX. This > > + * exchanges two WRMSRs for one RDMSR. > > + */ > > + if (svm->v_tsc_aux) > > + rdmsr(MSR_TSC_AUX, hostsa->tsc_aux, msr_hi); > > IIUC, when V_TSC_AUX is supported, SEV-ES guests context switch MSR_TSC_AUX > regardless of what has been exposed to the guest. So rather than condition the > hostsa->tsc_aux update on guest CPUID, just do it if V_TSC_AUX is supported. > > And then to avoid the RDMSR, which is presumably the motivation for checking > guest CPUID, grab the host value from user return framework. The host values > are per-CPU, but constant after boot, so the only requirement is that KVM sets > up MSR_TSC_AUX in the user return framework. Actually, duh. The save area is also per-CPU, so just fill hostsa->tsc_aux in svm_hardware_setup() and then sev_es_prepare_switch_to_guest() never has to do anything.