Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp736419pxb; Wed, 3 Feb 2021 17:02:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJwBsqp8o6TXrdifDl4qhzjg9KUUJpV0bha2clrsSn576FLaAJnHcOp+wlEi9uQpxUT2nDR3 X-Received: by 2002:a17:907:f81:: with SMTP id kb1mr5665060ejc.466.1612400565255; Wed, 03 Feb 2021 17:02:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612400565; cv=none; d=google.com; s=arc-20160816; b=YL3c/0d3YsFP5KjovQQo/4chcuNJ2IPaGqeRcJgo3i6DApbNDfs7e9yqOGF2t/hKIo 6Jrwsd8gHAO5CLw0gHBUXd/472Ix04k7VYVHyKg4b1XwQkimybr24VdsZ8yjvxokftrA /aBLb7P4zzgxPM/nF/pv1hg36lWJcKOWx1FvwsVwqh7qkfNf05Ksexp+IWr0Aa/i6biV 9GQbfUN3gDmmPRKgFKSYUCvUSRjZj4hjDj05BxL4+jZHmpWbInD1KevDybRjlW1zjn2f BUDa74LJGjMnNntW8xSexqU66lyXLtwUJZGbh7es4B84cDyTphnlnnUMdwqa9AyTtLRu aQEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:ironport-sdr:ironport-sdr; bh=bETemjD0s+8/shiEMUb/Euw5bcFfftOAGyiGJKeb7So=; b=brouvR2w2VBF4jClvf+cVzjdnEYS6jPLn214x3rlD7uSs9aAS7fRJRzYzL59XyewCZ s9M23GdpxSs2GJd4nprcdmQNwxTwb81455ilHv/f/zP+hhdnFovUnI2Cz8a9cU8Oqlv+ JxhUsFt0Gtc9RpkdItxJjCqBTALlNH3GKunpluK0uThfoYdffzAnEjDNEMC78KRIEcQJ NTPa4KfIagHq06WmXjha3JdOn4dnyC0f7+jp1fr7E003XSUI63Xu3hRMaZTG2TQGbNQn asym0n/YugG686jYRzvSYh2o3J1BHjadsZQVxrWJ/LP8OyrSjug6mHOZqe5IUTSmO4zf 9tkA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t9si2192948edd.589.2021.02.03.17.02.20; Wed, 03 Feb 2021 17:02:45 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234282AbhBDBAg (ORCPT + 99 others); Wed, 3 Feb 2021 20:00:36 -0500 Received: from mga17.intel.com ([192.55.52.151]:18140 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234476AbhBDBAa (ORCPT ); Wed, 3 Feb 2021 20:00:30 -0500 IronPort-SDR: nOT0sMqXTCGVM2VTlplczWvdeozs7Uw26krNFHJ0n+iGfAe6MeHehpmj0CXcVX8v9yNj2xFbrH pXaDU5Lpsr7w== X-IronPort-AV: E=McAfee;i="6000,8403,9884"; a="160905784" X-IronPort-AV: E=Sophos;i="5.79,399,1602572400"; d="scan'208";a="160905784" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 16:59:43 -0800 IronPort-SDR: EEloAjSSKm981up7084LA9lMIkFYqpTru5pXdqkBuIiVEfCuVQ1qwp3/kxXba+TqP+OMBFpCeL 3NYycMJ0WOXw== X-IronPort-AV: E=Sophos;i="5.79,399,1602572400"; d="scan'208";a="392850295" Received: from likexu-mobl1.ccr.corp.intel.com (HELO [10.238.4.93]) ([10.238.4.93]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 16:59:39 -0800 Subject: Re: [PATCH v2 4/4] KVM: x86: Expose Architectural LBR CPUID and its XSAVES bit To: Paolo Bonzini , Sean Christopherson Cc: Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Like Xu References: <20210203135714.318356-1-like.xu@linux.intel.com> <20210203135714.318356-5-like.xu@linux.intel.com> <8321d54b-173b-722b-ddce-df2f9bd7abc4@redhat.com> From: "Xu, Like" Message-ID: <219d869b-0eeb-9e52-ea99-3444c6ab16a3@intel.com> Date: Thu, 4 Feb 2021 08:59:36 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: <8321d54b-173b-722b-ddce-df2f9bd7abc4@redhat.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021/2/3 22:37, Paolo Bonzini wrote: > On 03/02/21 14:57, Like Xu wrote: >> If CPUID.(EAX=07H, ECX=0):EDX[19] is exposed to 1, the KVM supports Arch >> LBRs and CPUID leaf 01CH indicates details of the Arch LBRs capabilities. >> As the first step, KVM only exposes the current LBR depth on the host for >> guest, which is likely to be the maximum supported value on the host. >> >> If KVM supports XSAVES, the CPUID.(EAX=0DH, ECX=1):EDX:ECX[bit 15] >> is also exposed to 1, which means the availability of support for Arch >> LBR configuration state save and restore. When available, guest software >> operating at CPL=0 can use XSAVES/XRSTORS manage supervisor state >> component Arch LBR for own purposes once IA32_XSS [bit 15] is set. >> XSAVE support for Arch LBRs is enumerated in CPUID.(EAX=0DH, ECX=0FH). >> >> Signed-off-by: Like Xu >> --- >>   arch/x86/kvm/cpuid.c   | 23 +++++++++++++++++++++++ >>   arch/x86/kvm/vmx/vmx.c |  2 ++ >>   arch/x86/kvm/x86.c     | 10 +++++++++- >>   3 files changed, 34 insertions(+), 1 deletion(-) >> >> diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c >> index 944f518ca91b..900149eec42d 100644 >> --- a/arch/x86/kvm/cpuid.c >> +++ b/arch/x86/kvm/cpuid.c >> @@ -778,6 +778,29 @@ static inline int __do_cpuid_func(struct >> kvm_cpuid_array *array, u32 function) >>               entry->edx = 0; >>           } >>           break; >> +    /* Architectural LBR */ >> +    case 0x1c: >> +    { >> +        u64 lbr_depth_mask = 0; >> + >> +        if (!kvm_cpu_cap_has(X86_FEATURE_ARCH_LBR)) { >> +            entry->eax = entry->ebx = entry->ecx = entry->edx = 0; >> +            break; >> +        } >> + >> +        /* >> +         * KVM only exposes the maximum supported depth, >> +         * which is also the fixed value used on the host. >> +         * >> +         * KVM doesn't allow VMM user sapce to adjust depth >> +         * per guest, because the guest LBR emulation depends >> +         * on the implementation of the host LBR driver. >> +         */ >> +        lbr_depth_mask = 1UL << fls(entry->eax & 0xff); >> +        entry->eax &= ~0xff; >> +        entry->eax |= lbr_depth_mask; >> +        break; >> +    } >>       /* Intel PT */ >>       case 0x14: >>           if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) { >> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c >> index 9ddf0a14d75c..c22175d9564e 100644 >> --- a/arch/x86/kvm/vmx/vmx.c >> +++ b/arch/x86/kvm/vmx/vmx.c >> @@ -7498,6 +7498,8 @@ static __init void vmx_set_cpu_caps(void) >>           kvm_cpu_cap_check_and_set(X86_FEATURE_INVPCID); >>       if (vmx_pt_mode_is_host_guest()) >>           kvm_cpu_cap_check_and_set(X86_FEATURE_INTEL_PT); >> +    if (cpu_has_vmx_arch_lbr()) >> +        kvm_cpu_cap_check_and_set(X86_FEATURE_ARCH_LBR); >>         if (vmx_umip_emulated()) >>           kvm_cpu_cap_set(X86_FEATURE_UMIP); >> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c >> index 667d0042d0b7..107f2e72f526 100644 >> --- a/arch/x86/kvm/x86.c >> +++ b/arch/x86/kvm/x86.c >> @@ -10385,8 +10385,16 @@ int kvm_arch_hardware_setup(void *opaque) >>         if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES)) >>           supported_xss = 0; >> -    else >> +    else { >>           supported_xss &= host_xss; >> +        /* >> +         * The host doesn't always set ARCH_LBR bit to hoss_xss since this >> +         * Arch_LBR component is used on demand in the Arch LBR driver. >> +         * Check e649b3f0188f "Support dynamic supervisor feature for >> LBR". >> +         */ >> +        if (kvm_cpu_cap_has(X86_FEATURE_ARCH_LBR)) >> +            supported_xss |= XFEATURE_MASK_LBR; >> +    } >>         /* Update CET features now that supported_xss is finalized. */ >>       if (!kvm_cet_supported()) { >> > > This requires some of the XSS patches that Weijang posted for CET, right? Yes, at least we need three of them for Arch LBR: 3009dfd6d61f KVM: x86: Load guest fpu state when accessing MSRs managed by XSAVES d39b0a16ad1f KVM: x86: Refresh CPUID on writes to MSR_IA32_XSS e98bf65e51c9 KVM: x86: Report XSS as an MSR to be saved if there are supported features > > Also, who takes care of saving/restoring the MSRs, if the host has not > added XFEATURE_MASK_LBR to MSR_IA32_XSS? I may not understand your concern on this. Let me try to explain: The guest Arch LBR driver will save the origin host_xss and mark the LBR bit only in the XSS and then save/restore MSRs in the extra specified guest memory, and restore the origin host_xss. On the host side, the same thing happens to vcpu thread due to the help of guest LBR event created by the vPMU and the hardware LBR MSRs are saved/restored in a exclusive way. > > Thanks, > > Paolo >