Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp1403619pxy; Fri, 23 Apr 2021 07:20:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcoUesWS9er0e44/oVoCnshD8Z0LKt1yRW6ZKS9hgziE4mLE99X1HdWP1WipWdE4b+W9iU X-Received: by 2002:aa7:c049:: with SMTP id k9mr4812880edo.56.1619187627737; Fri, 23 Apr 2021 07:20:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619187627; cv=none; d=google.com; s=arc-20160816; b=b85KPoQHQkTLhlAgqsktzBWKdXj/24XQyQKyobJdF1Iwrr5ITOnEJoIrFQ0Ej6Vffu OQSvp5qP66WCNYG4UAP+WsBkd6rAF19tBC6ffppBOotv4x0vlqgR7yuSKjVU+qAWuCS8 lVPuO9wS3WdVTvPITWPYFJ1ZJxdaeyxNo5VxXd5H6R29W4iGGgQwLndgX1NEf6hwphUM EAFs0wo4g8YJIYxR5c1kA+whfh9l/Qp8hYMMb1jW8JvMfEXHHQyA755Qdk1s7bOxMu1h Ig+TA/pE21DBmBGhTDLJrLhCcYEKtr3eQE7k3M7kWjdkXfvcfmKGP1B/+YlHPTs6I6Vh ihnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=UsLHXkZihTU6VaR2mOY6eO9xp9LBBo1wxPeRGitgkmA=; b=ejRqBEsQVQxKMN2xAkc4uOjQbsVDnFq0bt1+D2NSFsTvbt8egp+nU7LdNcVbVNhXAJ Srg1Sflqbb7p9gxOn7FMC5CyxBtRQqxCcIXhfU4GYm95Vsdpfoltbld8lVrInXygZAqW t9ATmUSBXhXe1UxXLq8rk7tvHBheioff35dD3bxbJjYPSRu00qr/2cxyb6KvB+XMdTwZ GfBpkob4zOikglHNakkEjimG1IP/bR+RymlvbauR/Y83SXjzw2Wu9P4NwoVPjg9XrwhQ o0aFKNAWhBz5g1cXL3FDdi8fFpcM+V645hr6kZDyUFkkIwsxtDhtof1aqkGcOSiEtSnm M3zA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WDnZSRbN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y15si5560941edm.338.2021.04.23.07.20.02; Fri, 23 Apr 2021 07:20:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WDnZSRbN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231274AbhDWOTc (ORCPT + 99 others); Fri, 23 Apr 2021 10:19:32 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:34142 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229939AbhDWOTb (ORCPT ); Fri, 23 Apr 2021 10:19:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1619187534; 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=UsLHXkZihTU6VaR2mOY6eO9xp9LBBo1wxPeRGitgkmA=; b=WDnZSRbN0sjuUNXTchZjvv6sgtpJCR41QDcbN65Moybrr8tyTFB1ixMiCRsDO+XzxoEFuj sc0QRr2HK+1xN1zqGurPZ7oFSKJSyGo4rz8mi0abbE8Ks1k908lEm4hZDuErZ8wfw0R9TJ P7ueznGxEXhh9DAj4bs0nEOXwEt6Fvg= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-11-vU7cy8XCPwy6lyucCyYF1A-1; Fri, 23 Apr 2021 10:18:52 -0400 X-MC-Unique: vU7cy8XCPwy6lyucCyYF1A-1 Received: by mail-ed1-f70.google.com with SMTP id bf25-20020a0564021a59b0290385169cebf8so11336684edb.8 for ; Fri, 23 Apr 2021 07:18:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=UsLHXkZihTU6VaR2mOY6eO9xp9LBBo1wxPeRGitgkmA=; b=EgpzTgQ94gahpjFkODyX/VADu7+jZeK+CqndVqlUXRCtXGBj6gmnstqWbfmSkybWc9 o3RZJ4VMH3gM2I1mMi+HWoQklLZNQK2iHgzqIAgHXqzgIkWeZ/XeCGHeqdH3mMC9Aghb JCy93OBKaPmMb467vjuTd+yyYY3vAilXwxmsTmVJn2Q7JwwJwFrK/xnNq0uR6Nx6BSNn jwUIK6yG3vr1A8CLnUPCFao0RrcfjNlEOa91sbj//9do6g1ijPQQSeYi4L3mETzq9qYI QycZIReeUI4sm7YRnOsaA0ohEbNvigWMolWDzdVUHOvgvhf9TDw/koGKvHRhiu+P67cU kN+Q== X-Gm-Message-State: AOAM531HMrL+8buA4Z4q/1fxUR2Ckns13nOKBp1+HgBdcArw5uAFg7Bc Yc4i2nDxuQlmih2UzOJHvJRp8R+NGcB9FJ+mEfhZ4tXbvQqCBoS6hCT7qbIeebxkmlTK65nsVj3 NqB0FCOTW7tbkapht0VBoAMFX X-Received: by 2002:a05:6402:698:: with SMTP id f24mr4742290edy.217.1619187531219; Fri, 23 Apr 2021 07:18:51 -0700 (PDT) X-Received: by 2002:a05:6402:698:: with SMTP id f24mr4742264edy.217.1619187530997; Fri, 23 Apr 2021 07:18:50 -0700 (PDT) Received: from ?IPv6:2001:b07:6468:f312:c8dd:75d4:99ab:290a? ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id q25sm4992019edt.51.2021.04.23.07.18.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 23 Apr 2021 07:18:50 -0700 (PDT) Subject: Re: [PATCH v2] KVM: SVM: Delay restoration of host MSR_TSC_AUX until return to userspace To: Sean Christopherson Cc: Reiji Watanabe , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Tom Lendacky References: <20210422001736.3255735-1-seanjc@google.com> <8cc2bb9a-167e-598c-6a9e-c23e943b1248@redhat.com> From: Paolo Bonzini Message-ID: Date: Fri, 23 Apr 2021 16:18:49 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 23/04/21 16:12, Sean Christopherson wrote: > On Fri, Apr 23, 2021, Paolo Bonzini wrote: >> On 22/04/21 22:12, Sean Christopherson wrote: >>> case MSR_TSC_AUX: >>> if (!boot_cpu_has(X86_FEATURE_RDTSCP)) >>> return 1; >>> >>> if (!msr_info->host_initiated && >>> !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) >>> return 1; >>> >>> /* >>> * TSC_AUX is usually changed only during boot and never read >>> * directly. Intercept TSC_AUX instead of exposing it to the >>> * guest via direct_access_msrs, and switch it via user return. >>> */ >>> preempt_disable(); >>> r = kvm_set_user_return_msr(TSC_AUX_URET_SLOT, data, -1ull); >>> preempt_enable(); >>> if (r) >>> return 1; >>> >>> /* >>> * Bits 63:32 are dropped by AMD CPUs, but are reserved on >>> * Intel CPUs. AMD's APM has incomplete and conflicting info >>> * on the architectural behavior; emulate current hardware as >>> * doing so ensures migrating from AMD to Intel won't explode. >>> */ >>> svm->tsc_aux = (u32)data; >>> break; >>> >> >> Ok, squashed in the following: > > Too fast! The below won't compile (s/msr_info/msr and 'r' needs to be defined), > and the get_msr() path needs the guest_cpuid_has() check. Oops I missed the get_msr(). (I modify my local tree very aggressively, often without even compiling, so that I can use "git range-diff kvm/next..kvm/queue kvm/next.." as a reminder of things that are pending). Paolo I'll spin a v3. > >> diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c >> index 14ff7f0963e9..00e9680969a2 100644 >> --- a/arch/x86/kvm/svm/svm.c >> +++ b/arch/x86/kvm/svm/svm.c >> @@ -2875,16 +2875,28 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) >> if (!boot_cpu_has(X86_FEATURE_RDTSCP)) >> return 1; >> + if (!msr_info->host_initiated && >> + !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) >> + return 1; >> + >> /* >> * TSC_AUX is usually changed only during boot and never read >> * directly. Intercept TSC_AUX instead of exposing it to the >> * guest via direct_access_msrs, and switch it via user return. >> */ >> - svm->tsc_aux = data; >> - >> preempt_disable(); >> - kvm_set_user_return_msr(TSC_AUX_URET_SLOT, data, -1ull); >> + r = kvm_set_user_return_msr(TSC_AUX_URET_SLOT, data, -1ull); >> preempt_enable(); >> + if (r) >> + return 1; >> + >> + /* >> + * Bits 63:32 are dropped by AMD CPUs, but are reserved on >> + * Intel CPUs. AMD's APM has incomplete and conflicting info >> + * on the architectural behavior; emulate current hardware as >> + * doing so ensures migrating from AMD to Intel won't explode. >> + */ >> + svm->tsc_aux = (u32)data; >> break; >> case MSR_IA32_DEBUGCTLMSR: >> if (!boot_cpu_has(X86_FEATURE_LBRV)) { >> >> Paolo >> >