Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1627025pxj; Wed, 19 May 2021 10:05:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwHRfl9YYL685RhOSSFZh5RrUVqBuD0g1qJvfzowmosqKHRNA6E+OwhO/Hj4AIBdBPPYtMF X-Received: by 2002:a17:906:7c9:: with SMTP id m9mr202117ejc.98.1621443956323; Wed, 19 May 2021 10:05:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621443956; cv=none; d=google.com; s=arc-20160816; b=O/SLl1oSvGCQrx+T+nNTnUZHrxOt57rDtPsXQ8BrJtqTxr//wuM9dTnzkdIpres3sE BXKJc1COVfBXJGnKfZxSKAPEc5XeZlFCmMazn3d6s2LI5Hen+7UHmkVfZHl6xXVibjjh eqrrj9o/ipS+Nap6y7TkaLsRVrz5f7oSFcM+FjgxOCe7Ds9voQQgeaOj4IPWUJCH+Qr2 yYgjsByV4efuqMgZb7Mqwl/knnY6ifReiXj+N3jDqrUn9p+RumF5Kd2PNEwd2F7o+nrt mn1hUdcJW/Ty7sacejqZr9oJrIn1+zsaz7w/Tu29XGRyem6H0oG7OSQS7UXQzZ+8nhVg Aclg== 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=jVjiDpy8xD+Y7h09uKCwxo95CfsG/bIKI6L5+1bm+P8=; b=ZtS8l2bDRoObjt9PQfT+Dl7dq0WyuTD7MR79G0mr12cCqJLmOgxKTvaUb99RboJCJA rQ4lv9xUpvo74zKHJgyjyomea/VX/KJnr1aa7hP5qkUiHnLRPFlP/WoOyVQs7fhvO5NC 3PGEgjG9qsCpEcqarBe5ZyLvYa4lx3huGsE/oKJ/7YX0xngwIC2R1QqZtq6x4Zms1Lco PlQ1ZqNQi+eosNrDHLTP2JMdlGKu81ijcgMxzXo7ajWDCEhMujTtz+wF+wXIj7ZCc7Ok ZLR3vG7t/oRsZMkT+PJlIqdDXOJQM1INWaJ3IDcrGSHYWTBhgZGJWixjc+rS6ng489Ym 7fPw== 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 f23si1436891edx.85.2021.05.19.10.05.32; Wed, 19 May 2021 10:05:56 -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 S1346877AbhERIpj (ORCPT + 99 others); Tue, 18 May 2021 04:45:39 -0400 Received: from mga06.intel.com ([134.134.136.31]:36400 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241784AbhERIpi (ORCPT ); Tue, 18 May 2021 04:45:38 -0400 IronPort-SDR: MD08VHyxG+2r+hhI7auytOMogNKJ/jIFlKmvrkNdn9wv3IXFDKY+7kmW3ZNkbx8vwsOF5C1LJY lalX//3RLzKQ== X-IronPort-AV: E=McAfee;i="6200,9189,9987"; a="261890459" X-IronPort-AV: E=Sophos;i="5.82,309,1613462400"; d="scan'208";a="261890459" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 May 2021 01:44:20 -0700 IronPort-SDR: mN2MH/8odEPxWFWlnT7H5kVTDuv9JWn3pna63zpSihh8KJi6Augh17Ii7ugWq7Xhj2OVQ3V3Ai 5JkvABga9d6Q== X-IronPort-AV: E=Sophos;i="5.82,309,1613462400"; d="scan'208";a="472852491" Received: from likexu-mobl1.ccr.corp.intel.com (HELO [10.238.4.93]) ([10.238.4.93]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 May 2021 01:44:15 -0700 Subject: Re: [PATCH v6 06/16] KVM: x86/pmu: Add IA32_PEBS_ENABLE MSR emulation for extended PEBS To: Peter Zijlstra Cc: Paolo Bonzini , Borislav Petkov , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , weijiang.yang@intel.com, Kan Liang , ak@linux.intel.com, wei.w.wang@intel.com, eranian@google.com, liuxiangdong5@huawei.com, linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, Luwei Kang , Like Xu References: <20210511024214.280733-1-like.xu@linux.intel.com> <20210511024214.280733-7-like.xu@linux.intel.com> From: "Xu, Like" Message-ID: Date: Tue, 18 May 2021 16:44:13 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.10.1 MIME-Version: 1.0 In-Reply-To: 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/5/17 16:32, Peter Zijlstra wrote: > On Tue, May 11, 2021 at 10:42:04AM +0800, Like Xu wrote: >> diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c >> index 2f89fd599842..c791765f4761 100644 >> --- a/arch/x86/events/intel/core.c >> +++ b/arch/x86/events/intel/core.c >> @@ -3898,31 +3898,49 @@ static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data) >> struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); >> struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs; >> u64 intel_ctrl = hybrid(cpuc->pmu, intel_ctrl); >> + u64 pebs_mask = (x86_pmu.flags & PMU_FL_PEBS_ALL) ? >> + cpuc->pebs_enabled : (cpuc->pebs_enabled & PEBS_COUNTER_MASK); >> + >> + *nr = 0; >> + arr[(*nr)++] = (struct perf_guest_switch_msr){ >> + .msr = MSR_CORE_PERF_GLOBAL_CTRL, >> + .host = intel_ctrl & ~cpuc->intel_ctrl_guest_mask, >> + .guest = intel_ctrl & (~cpuc->intel_ctrl_host_mask | ~pebs_mask), >> + }; >> >> + if (!x86_pmu.pebs) >> + return arr; >> >> + /* >> + * If PMU counter has PEBS enabled it is not enough to >> + * disable counter on a guest entry since PEBS memory >> + * write can overshoot guest entry and corrupt guest >> + * memory. Disabling PEBS solves the problem. >> + * >> + * Don't do this if the CPU already enforces it. >> + */ >> + if (x86_pmu.pebs_no_isolation) { >> + arr[(*nr)++] = (struct perf_guest_switch_msr){ >> + .msr = MSR_IA32_PEBS_ENABLE, >> + .host = cpuc->pebs_enabled, >> + .guest = 0, >> + }; >> + return arr; >> } >> >> + if (!x86_pmu.pebs_vmx) >> + return arr; >> + >> + arr[*nr] = (struct perf_guest_switch_msr){ >> + .msr = MSR_IA32_PEBS_ENABLE, >> + .host = cpuc->pebs_enabled & ~cpuc->intel_ctrl_guest_mask, >> + .guest = pebs_mask & ~cpuc->intel_ctrl_host_mask, >> + }; >> + >> + /* Set hw GLOBAL_CTRL bits for PEBS counter when it runs for guest */ >> + arr[0].guest |= arr[*nr].guest; >> + >> + ++(*nr); >> return arr; >> } > ISTR saying I was confused as heck by this function, I still don't see > clarifying comments :/ > > What's .host and .guest ? Will adding the following comments help you ? +/* + * Currently, the only caller of this function is the atomic_switch_perf_msrs(). + * The host perf conext helps to prepare the values of the real hardware for + * a set of msrs that need to be switched atomically in a vmx transaction. + * + * For example, the pseudocode needed to add a new msr should look like: + * + * arr[(*nr)++] = (struct perf_guest_switch_msr){ + *     .msr = the hardware msr address, + *     .host = the value the hardware has when it doesn't run a guest, + *     .guest = the value the hardware has when it runs a guest, + * }; + * + * These values have nothing to do with the emulated values the guest sees + * when it uses {RD,WR}MSR, which should be handled in the KVM context. + */  static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *data)