Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp3465402ybk; Tue, 19 May 2020 05:29:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy4lmHMCtaSg01DKnKvgoqXbZ7cA2xEAZl6XhJL3fGwF2NBYzAGmX/YhBps0q3AfIPwXhBq X-Received: by 2002:a17:907:4066:: with SMTP id nl6mr17701748ejb.129.1589891386601; Tue, 19 May 2020 05:29:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589891386; cv=none; d=google.com; s=arc-20160816; b=ME4NcxDwFTpfAINmYIVkd26b7c6NUNIXD+Fq2zd96SRB8MeXiNjHqQ2NyFYAOgz0NT sr2CQAjtsWiMHbFFL/aNX22kYGKzp7Ygt/iPVglkNAinWKV9woAuE8dqVrnmxoB0U7xa 6ztBvytIfSj+6uEfsbpB6IL7JEndWTe5bPz35dNB9/jF4yBGCJajKNwIfqUM89f65e+T ir1483l4+F/2mE5HzpKAB8etNBgL52Lf3TfV/THtsLRTXhCi6cu/edgfvHfe0p7gHVCs VHy620avrwB60IEJD/nyVGLW/eDhOpWuODqz4vNUo2zFF8M6kiTYMtZciDnfPDRCFc58 aATA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:organization:from:references:cc:to:subject:reply-to :ironport-sdr:ironport-sdr; bh=RAxhjvMGzaxb+ak0u+Jd5ugLdBEecxZ/+7Nteu4utXo=; b=oqgXTsL0ouWIHxEN4tNicpkl5BKCuHp1DnqBwXPa4uAFW7d1QUpJWCM8QfmHHOvsYm NLOBmx4rj4rJQGmeEm8mN2xY/BvHgoD7sghUjTcg4ktjLagOEL6CdkG0hA2M+RHlXnWt xNF3lcgkhRQuO1NoI6hQur8UTvdKDtbJjAH+7j4lDezosDdi1CVjS/pb77tMElFA6bmx wxbWPyYKYqIYV21XAb+VH59rwN7HCpkYOhITw2d+U1bIjA65davuFqc+uuaMByAGpnKQ 2uSSZk7KdBcI0Y00cOVh8D8w5ZYh047A5rAs5yq6sPmVm6IyKICWFgMbtThE+KgC936K jodQ== 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 92si9733729edk.74.2020.05.19.05.29.23; Tue, 19 May 2020 05:29:46 -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; 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 S1728805AbgESMYn (ORCPT + 99 others); Tue, 19 May 2020 08:24:43 -0400 Received: from mga03.intel.com ([134.134.136.65]:25136 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726196AbgESMYm (ORCPT ); Tue, 19 May 2020 08:24:42 -0400 IronPort-SDR: 496qYSdewMGhLUJgzF22dMFgIYgDXyLavxGIjOPvu462z31+XdfR1qv5ovBQgLU4odrg7fLgO3 CtlKTN0X5LFQ== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 May 2020 05:24:42 -0700 IronPort-SDR: uXcqt5cgS8vziQXjvTsLjF60PBfq6Ka4H9MGh3uVo2aJdrct40suUlGS/XoeJbWge4pX/d/ycB QBf/rUZ/rccQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,410,1583222400"; d="scan'208";a="288940600" Received: from likexu-mobl1.ccr.corp.intel.com (HELO [10.249.171.98]) ([10.249.171.98]) by fmsmga004.fm.intel.com with ESMTP; 19 May 2020 05:24:38 -0700 Reply-To: like.xu@intel.com Subject: Re: [PATCH v11 08/11] KVM: x86/pmu: Emulate LBR feature via guest LBR event To: Peter Zijlstra , Like Xu Cc: Paolo Bonzini , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , ak@linux.intel.com, wei.w.wang@intel.com References: <20200514083054.62538-1-like.xu@linux.intel.com> <20200514083054.62538-9-like.xu@linux.intel.com> <20200519110011.GG279861@hirez.programming.kicks-ass.net> From: "Xu, Like" Organization: Intel OTC Message-ID: <1fd08161-b3d2-1731-37c5-6c9fe0e06233@intel.com> Date: Tue, 19 May 2020 20:24:38 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 In-Reply-To: <20200519110011.GG279861@hirez.programming.kicks-ass.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020/5/19 19:00, Peter Zijlstra wrote: > On Thu, May 14, 2020 at 04:30:51PM +0800, Like Xu wrote: >> +static inline bool event_is_oncpu(struct perf_event *event) >> +{ >> + return event && event->oncpu != -1; >> +} > >> +/* >> + * It's safe to access LBR msrs from guest when they have not >> + * been passthrough since the host would help restore or reset >> + * the LBR msrs records when the guest LBR event is scheduled in. >> + */ >> +static bool intel_pmu_access_lbr_msr(struct kvm_vcpu *vcpu, >> + struct msr_data *msr_info, bool read) >> +{ >> + struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); >> + u32 index = msr_info->index; >> + >> + if (!intel_is_valid_lbr_msr(vcpu, index)) >> + return false; >> + >> + if (!msr_info->host_initiated && !pmu->lbr_event) >> + intel_pmu_create_lbr_event(vcpu); >> + >> + /* >> + * Disable irq to ensure the LBR feature doesn't get reclaimed by the >> + * host at the time the value is read from the msr, and this avoids the >> + * host LBR value to be leaked to the guest. If LBR has been reclaimed, >> + * return 0 on guest reads. >> + */ >> + local_irq_disable(); >> + if (event_is_oncpu(pmu->lbr_event)) { >> + if (read) >> + rdmsrl(index, msr_info->data); >> + else >> + wrmsrl(index, msr_info->data); >> + } else if (read) >> + msr_info->data = 0; >> + local_irq_enable(); >> + >> + return true; >> +} > So this runs in the vCPU thread in host context to emulate the MSR > access, right? Yes, it's called to emulate MSR accesses when the guest LBR event is scheduled on while the LBR stack MSRs have not been passthrough to the vCPU. Thanks, Like Xu >