From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7E298C43334 for ; Tue, 7 Jun 2022 15:26:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344027AbiFGP0T (ORCPT ); Tue, 7 Jun 2022 11:26:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243190AbiFGP0R (ORCPT ); Tue, 7 Jun 2022 11:26:17 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 01522B0D33 for ; Tue, 7 Jun 2022 08:26:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1654615576; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HuCZf+EG4tjlRlQ6eyt+b3Zz/g4UZvXSEMkRpd1yJXs=; b=OIxYaR2rzdacvahLzz/GJg8ZIuu2k4kmSJKvMyVj9lRY3LZE/ymRtom4tyirlABS2CD8dW FZ1DVvHO9vI71O81NIU7i9IURuKsOkoyydqNhv6NKPRCon7SM1jaGvSwd1ScB61nMJPAlP mJa4PwNZ5WmlEQAu4bHVEvfIzhQNQmw= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-38-1fHV4yufMzyUckri1xuqPQ-1; Tue, 07 Jun 2022 11:26:10 -0400 X-MC-Unique: 1fHV4yufMzyUckri1xuqPQ-1 Received: by mail-wr1-f69.google.com with SMTP id d9-20020adfe849000000b00213375a746aso3543704wrn.18 for ; Tue, 07 Jun 2022 08:26:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=HuCZf+EG4tjlRlQ6eyt+b3Zz/g4UZvXSEMkRpd1yJXs=; b=IPmLu98tTfMe5tvyiinzWuG7mxJuTC1veS3Eipktl0/GNeCFmiYEv/FUB3NMQOF8Hi TK98rRwM3ZS1Bg/cyBi1ysHABt07IqF0QO1J5MYvPQRrMnE8VOUC9Aoqlny+vwV+5qCp 21BJQ3N5BGzAhABV4YF+XPXWD5ep3xJP7xwXu6iyN2/e3uLje1mRm/tOrd2QcAlaS9dR pnGQpwBbXMl/S2n6VcFRkYGJm3SddzI9Xe+84SIWIq1LenTGGCtSMfdGLN94675BnNMm aLqCybMOBbyMxw50esfVjZUNfx5PmDWbOhHYm0T2bzmPt6KZRoZL85IEOS4437RDDiL9 bgLw== X-Gm-Message-State: AOAM533iGxk/gxiKsNupyZHR7UseZ+2XwOawZ7MPwos5AnWfEsugg+uW jZTnVZgjCFLSYbboIGsmNMTAb31QOtzvdSTo2SsEqS2v92kGGN08Lo6Rh0YEfS5WbanJ6bxt8ZH +vKt3yCEMD8Mlewf64e6CQIR2 X-Received: by 2002:a05:600c:2e48:b0:39c:55ba:e4e9 with SMTP id q8-20020a05600c2e4800b0039c55bae4e9mr7834500wmf.180.1654615568693; Tue, 07 Jun 2022 08:26:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzURo0IvLNNJyb01U71GmD/UPNpY1M0FxVGqPYO6LI3DeN+Bfbg+IXG3677DlNpT4wuUumYJw== X-Received: by 2002:a05:600c:2e48:b0:39c:55ba:e4e9 with SMTP id q8-20020a05600c2e4800b0039c55bae4e9mr7834445wmf.180.1654615568293; Tue, 07 Jun 2022 08:26:08 -0700 (PDT) Received: from ?IPV6:2001:b07:6468:f312:9af8:e5f5:7516:fa89? ([2001:b07:6468:f312:9af8:e5f5:7516:fa89]) by smtp.googlemail.com with ESMTPSA id h1-20020a05600c414100b0039c5cecf206sm1000625wmm.4.2022.06.07.08.26.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 07 Jun 2022 08:26:07 -0700 (PDT) Message-ID: Date: Tue, 7 Jun 2022 17:26:04 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.0 Subject: Re: [PATCH] KVM: SVM: fix tsc scaling cache logic Content-Language: en-US To: Maxim Levitsky , kvm@vger.kernel.org Cc: Joerg Roedel , Sean Christopherson , Thomas Gleixner , linux-kernel@vger.kernel.org, Wanpeng Li , Jim Mattson , Borislav Petkov , Ilias Stamatis , Vitaly Kuznetsov , "H. Peter Anvin" , Dave Hansen , x86@kernel.org, Ingo Molnar References: <20220606181149.103072-1-mlevitsk@redhat.com> From: Paolo Bonzini In-Reply-To: <20220606181149.103072-1-mlevitsk@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 6/6/22 20:11, Maxim Levitsky wrote: > SVM uses a per-cpu variable to cache the current value of the > tsc scaling multiplier msr on each cpu. > > Commit 1ab9287add5e2 > ("KVM: X86: Add vendor callbacks for writing the TSC multiplier") > broke this caching logic. > > Refactor the code so that all TSC scaling multiplier writes go through > a single function which checks and updates the cache. > > This fixes the following scenario: > > 1. A CPU runs a guest with some tsc scaling ratio. > > 2. New guest with different tsc scaling ratio starts on this CPU > and terminates almost immediately. > > This ensures that the short running guest had set the tsc scaling ratio just > once when it was set via KVM_SET_TSC_KHZ. Due to the bug, > the per-cpu cache is not updated. > > 3. The original guest continues to run, it doesn't restore the msr > value back to its own value, because the cache matches, > and thus continues to run with a wrong tsc scaling ratio. > > > Fixes: 1ab9287add5e2 ("KVM: X86: Add vendor callbacks for writing the TSC multiplier") > Signed-off-by: Maxim Levitsky > --- > arch/x86/kvm/svm/nested.c | 4 ++-- > arch/x86/kvm/svm/svm.c | 32 ++++++++++++++++++++------------ > arch/x86/kvm/svm/svm.h | 2 +- > 3 files changed, 23 insertions(+), 15 deletions(-) > > diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c > index 88da8edbe1e1f..83bae1f2eeb8a 100644 > --- a/arch/x86/kvm/svm/nested.c > +++ b/arch/x86/kvm/svm/nested.c > @@ -1037,7 +1037,7 @@ int nested_svm_vmexit(struct vcpu_svm *svm) > if (svm->tsc_ratio_msr != kvm_caps.default_tsc_scaling_ratio) { > WARN_ON(!svm->tsc_scaling_enabled); > vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio; > - svm_write_tsc_multiplier(vcpu, vcpu->arch.tsc_scaling_ratio); > + __svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio); > } > > svm->nested.ctl.nested_cr3 = 0; > @@ -1442,7 +1442,7 @@ void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu) > vcpu->arch.tsc_scaling_ratio = > kvm_calc_nested_tsc_multiplier(vcpu->arch.l1_tsc_scaling_ratio, > svm->tsc_ratio_msr); > - svm_write_tsc_multiplier(vcpu, vcpu->arch.tsc_scaling_ratio); > + __svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio); > } > > /* Inverse operation of nested_copy_vmcb_control_to_cache(). asid is copied too. */ > diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c > index 4aea82f668fb1..5c873db9432e5 100644 > --- a/arch/x86/kvm/svm/svm.c > +++ b/arch/x86/kvm/svm/svm.c > @@ -512,11 +512,24 @@ static int has_svm(void) > return 1; > } > > +void __svm_write_tsc_multiplier(u64 multiplier) > +{ > + preempt_disable(); > + > + if (multiplier == __this_cpu_read(current_tsc_ratio)) > + goto out; > + > + wrmsrl(MSR_AMD64_TSC_RATIO, multiplier); > + __this_cpu_write(current_tsc_ratio, multiplier); > +out: > + preempt_enable(); > +} > + > static void svm_hardware_disable(void) > { > /* Make sure we clean up behind us */ > if (tsc_scaling) > - wrmsrl(MSR_AMD64_TSC_RATIO, SVM_TSC_RATIO_DEFAULT); > + __svm_write_tsc_multiplier(SVM_TSC_RATIO_DEFAULT); > > cpu_svm_disable(); > > @@ -562,8 +575,7 @@ static int svm_hardware_enable(void) > * Set the default value, even if we don't use TSC scaling > * to avoid having stale value in the msr > */ > - wrmsrl(MSR_AMD64_TSC_RATIO, SVM_TSC_RATIO_DEFAULT); > - __this_cpu_write(current_tsc_ratio, SVM_TSC_RATIO_DEFAULT); > + __svm_write_tsc_multiplier(SVM_TSC_RATIO_DEFAULT); > } > > > @@ -1046,11 +1058,12 @@ static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) > vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS); > } > > -void svm_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 multiplier) > +static void svm_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 multiplier) > { > - wrmsrl(MSR_AMD64_TSC_RATIO, multiplier); > + __svm_write_tsc_multiplier(multiplier); > } > > + > /* Evaluate instruction intercepts that depend on guest CPUID features. */ > static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu, > struct vcpu_svm *svm) > @@ -1410,13 +1423,8 @@ static void svm_prepare_switch_to_guest(struct kvm_vcpu *vcpu) > sev_es_prepare_switch_to_guest(hostsa); > } > > - if (tsc_scaling) { > - u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio; > - if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) { > - __this_cpu_write(current_tsc_ratio, tsc_ratio); > - wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio); > - } > - } > + if (tsc_scaling) > + __svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio); > > if (likely(tsc_aux_uret_slot >= 0)) > kvm_set_user_return_msr(tsc_aux_uret_slot, svm->tsc_aux, -1ull); > diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h > index cd92f43437539..2495fe548b5e9 100644 > --- a/arch/x86/kvm/svm/svm.h > +++ b/arch/x86/kvm/svm/svm.h > @@ -594,7 +594,7 @@ int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, > bool has_error_code, u32 error_code); > int nested_svm_exit_special(struct vcpu_svm *svm); > void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu); > -void svm_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 multiplier); > +void __svm_write_tsc_multiplier(u64 multiplier); > void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm, > struct vmcb_control_area *control); > void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm, Queued, thanks. Paolo