Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp3808092pxy; Tue, 4 May 2021 10:20:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwKqXTbWJbcXLBFhUIg0L5mwbZELQ/lvwGbXY+YC+i7V4Mq5JZDMEKkHvt5fFI0OpVY6mGW X-Received: by 2002:a17:90b:34a:: with SMTP id fh10mr6525326pjb.123.1620148806732; Tue, 04 May 2021 10:20:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620148806; cv=none; d=google.com; s=arc-20160816; b=tDxf+Fuye16Ljjdi411v3v7fHBciwc4kZHFzVTgppsPFd/hOPS/HZO1LsnquVTwdP8 eSruQLsUvpB9QbL20AXk9oCXyoVKSrw1nnRY7OZwGHlRB6CjQAWElsCb5nGeUbRhTB3n qXK71czMmdVdUk2j9yxbulNwxLHHQAXc/92r6J0uoijPRGjf+YjwtQ2gUXuzR2kCKbXS 0MKx/UMtqV+wqzgTiVrjMtId19k4EIoujo0aItuIDngyysRFvuQqB8Qrx3rY/ISJvNDg lR/AbXP/9+A61AD70u5kMR5hYMiwRNOUqQ5yJLZ7bihJ1Hpr13lK5QDMd094mYNi24PF sKzg== 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:references:mime-version :message-id:in-reply-to:date:reply-to:dkim-signature; bh=x+NCaDzYaJB+NMadtQ9JUPGNk0j6WFjhbI1X+ndrCYA=; b=NAn4HWzOnLaTBmGBbfI1Bs0ZJUy7GQOidZ/iIne3JK8M3RZUYNGOB9xeJOAh6aPlWs 0Ym/7k8G4XzWpYk3wz6N6SAAsSIH1DKvFgBy1MDINBVtFZcz1hoXBDpKJ/wpVLOo6Zuj ATmhG3LpFOMuye4+c4qZ490vXT6flox1cfmUrmLc3XI8wUdb9bKLerASWMlTS75yYt8O gDGpR+F3Zm4eegx7RYRlWurb5kMbtWOOCJfmMqSIScCsvL74cnFoq4mhitJWLs7Sq5KR vLSz7v9HE9iUl1WhA+8DMvjknfhL6rFkxQsUugSo1OLc5b961pfZwOzGpf+l2q8DT8j4 07Uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=GYxeu56p; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y5si1572624pll.69.2021.05.04.10.19.54; Tue, 04 May 2021 10:20:06 -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=@google.com header.s=20161025 header.b=GYxeu56p; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232272AbhEDRTJ (ORCPT + 99 others); Tue, 4 May 2021 13:19:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232164AbhEDRSx (ORCPT ); Tue, 4 May 2021 13:18:53 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90C44C0613ED for ; Tue, 4 May 2021 10:17:57 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id g29-20020a25b11d0000b02904f44adeb480so12526958ybj.13 for ; Tue, 04 May 2021 10:17:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=x+NCaDzYaJB+NMadtQ9JUPGNk0j6WFjhbI1X+ndrCYA=; b=GYxeu56pESpHnymBBR37hcoL+erzIi1Cs2Fcie4S/JXxyz05HzWd8tfYRy2rUHxVD6 eJtObUcSxC1FczqzDMUZNwHyfxJ21cyb6kmOP30GXeH7chJrTDIyg1/pQ8A4ErT237jt 2e2nygFpJqozYZiDrgVQKjorHBF3s7zyrtYOS40HX12yiEZsf7gbjcIrrnB8RRb2Sb/1 IreqbcFdIOSmU8aPz9ZhCeTCCRo0nmbb1RMCNevhOVOLeKFsE+rMuzbbGGOlpMIq7REm Vdn5utrZ4eXb4F5kpCx95zET+f3Sbv23kgnex9h7n5Y4dcZuPqjKfCZY9I6OvmHqqdb1 /eGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=x+NCaDzYaJB+NMadtQ9JUPGNk0j6WFjhbI1X+ndrCYA=; b=kFG1VP+8pS5kOoHzJmBRoPWdHg/wXa/DLC0wJ8N1DFDNSeuS7leDLmTBi+RvGQ7BS2 Nyaf1b3pSbUayR2Gu0Pf0+w+UfmDwHSyUBaN0HXnYXg3ffkH1LGaAWxFmLIPHo8Vbi5Y uHhd1y+Kf0jGfh7Vq+gaLI8pRJyR4JMTeDSrbA+IbdmIMPTdlaM1FuA3lqAO8DoT+kFH V3BUD/cb2IovrC1HseWnTl5dlXZyR6PLgJXYP30/abF7GPRlE1lrfuj3EUX60sDo3t9M li2MXRwKlKPVKzjvr4Vf2bLmRpLW8NIMS3xkc4TGIJQNT2F7AzxzRkQMDvHmF5M9Nwit oBpg== X-Gm-Message-State: AOAM530hm5IWHsYofEV4fcMr0FfuB3MpeDyE9wv3oVAehUyl1SBlt38r JI+n+X0kHkzK0MNRv1UaO6VyQlZ02Qs= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:f:10:df57:48cb:ea33:a156]) (user=seanjc job=sendgmr) by 2002:a25:3103:: with SMTP id x3mr34207809ybx.8.1620148676838; Tue, 04 May 2021 10:17:56 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 4 May 2021 10:17:25 -0700 In-Reply-To: <20210504171734.1434054-1-seanjc@google.com> Message-Id: <20210504171734.1434054-7-seanjc@google.com> Mime-Version: 1.0 References: <20210504171734.1434054-1-seanjc@google.com> X-Mailer: git-send-email 2.31.1.527.g47e6f16901-goog Subject: [PATCH 06/15] KVM: SVM: Probe and load MSR_TSC_AUX regardless of RDTSCP support in host From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Reiji Watanabe Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Probe MSR_TSC_AUX whether or not RDTSCP is supported in the host, and if probing succeeds, load the guest's MSR_TSC_AUX into hardware prior to VMRUN. Because SVM doesn't support interception of RDPID, RDPID cannot be disallowed in the guest (without resorting to binary translation). Leaving the host's MSR_TSC_AUX in hardware would leak the host's value to the guest if RDTSCP is not supported. Note, there is also a kernel bug that prevents leaking the host's value. The host kernel initializes MSR_TSC_AUX if and only if RDTSCP is supported, even though the vDSO usage consumes MSR_TSC_AUX via RDPID. I.e. if RDTSCP is not supported, there is no host value to leak. But, if/when the host kernel bug is fixed, KVM would start leaking MSR_TSC_AUX in the case where hardware supports RDPID but RDTSCP is unavailable for whatever reason. Probing MSR_TSC_AUX will also allow consolidating the probe and define logic in common x86, and will make it simpler to condition the existence of MSR_TSX_AUX (from the guest's perspective) on RDTSCP *or* RDPID. Fixes: AMD CPUs Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 8f2b184270c0..b3153d40cc4d 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -212,7 +212,7 @@ DEFINE_PER_CPU(struct svm_cpu_data *, svm_data); * RDTSCP and RDPID are not used in the kernel, specifically to allow KVM to * defer the restoration of TSC_AUX until the CPU returns to userspace. */ -#define TSC_AUX_URET_SLOT 0 +static int tsc_aux_uret_slot __read_mostly = -1; static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000}; @@ -959,8 +959,10 @@ static __init int svm_hardware_setup(void) kvm_tsc_scaling_ratio_frac_bits = 32; } - if (boot_cpu_has(X86_FEATURE_RDTSCP)) - kvm_define_user_return_msr(TSC_AUX_URET_SLOT, MSR_TSC_AUX); + if (!kvm_probe_user_return_msr(MSR_TSC_AUX)) { + tsc_aux_uret_slot = 0; + kvm_define_user_return_msr(tsc_aux_uret_slot, MSR_TSC_AUX); + } /* Check for pause filtering support */ if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) { @@ -1454,8 +1456,8 @@ static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu) } } - if (static_cpu_has(X86_FEATURE_RDTSCP)) - kvm_set_user_return_msr(TSC_AUX_URET_SLOT, svm->tsc_aux, -1ull); + if (likely(tsc_aux_uret_slot >= 0)) + kvm_set_user_return_msr(tsc_aux_uret_slot, svm->tsc_aux, -1ull); svm->guest_state_loaded = true; } @@ -2664,7 +2666,7 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data |= (u64)svm->sysenter_esp_hi << 32; break; case MSR_TSC_AUX: - if (!boot_cpu_has(X86_FEATURE_RDTSCP)) + if (tsc_aux_uret_slot < 0) return 1; if (!msr_info->host_initiated && !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) @@ -2885,7 +2887,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) svm->sysenter_esp_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0; break; case MSR_TSC_AUX: - if (!boot_cpu_has(X86_FEATURE_RDTSCP)) + if (tsc_aux_uret_slot < 0) return 1; if (!msr->host_initiated && @@ -2908,7 +2910,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) * 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); + r = kvm_set_user_return_msr(tsc_aux_uret_slot, data, -1ull); preempt_enable(); if (r) return 1; -- 2.31.1.527.g47e6f16901-goog