Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp170904ybb; Thu, 9 Apr 2020 20:11:40 -0700 (PDT) X-Google-Smtp-Source: APiQypKFo+cU+zGRbOjU5iwKt+DePXy+4P8RyxaK/TFxDKVL9r6dVRwwIfZHfjj6YKWij9aDo0LB X-Received: by 2002:a37:e103:: with SMTP id c3mr657712qkm.7.1586488300100; Thu, 09 Apr 2020 20:11:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586488300; cv=none; d=google.com; s=arc-20160816; b=vnp5ej7lzjksukIuWwvdWVEmJPw2HlFwU/zR6lyQufow03LfKSsqozCVwaybgHHYnR EBut/u0He78nhGc0/4+n/cZ9Ql5vwh7BTErjYXpZMs9UjwzPZLnT5i/AwojNd++pq1+M 0BwR/RjJyx1Z5PSqlqvJC8nXo5NVzSn241Kg+0imwEGemNdGOxr0LZOwpgAbVYGlXntF 5934Vw4pEQZUlW0Qv7qwZ0wxjgugKmGerEJFCkqkY063DovAT6cEyX/8Uk407B7Tf9Ca pvfAw02xnfEt9JoEAor4uk4YppgwVOBz4CSvYtbetSa5hf0AgYtCSdk/sAYARZeIpk8l BxFw== 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=S+84uG9+maXrAFEIm1MRtstDotAw4bmQiqYN8x57BME=; b=s7qmvVGz99b9V4naRC6EXH+nEz5aq0onGG4daQ/NO75Jx55xiqPdwPlFmQmRtEPT0a KOIS8vnI8qrpX1dTTG9s95kLkL9oKzbd02leE1HAEyD2VgeFvuPyRbSA6rZ91NdsM42X AZbkHb2rhcggVQIOf08cjxctca7nQ/Hoj8k0zvNWjew7EDCptLPlgm8l50E8SBYXvhDw LP/rU7C8V6GTowj4uuRc4nS/M0XtJr8mfloDYHVNbDDk0FDZqvqaM0sWzMCvytBtjVMa D/OCEk9FFMfR/X4PiBNMYuVg7UQI/j4VljDb9Lt8kuEp5VD0y0zVTNYmmh3xWQ9D9xUX Hz5A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id l14si400444qtu.233.2020.04.09.20.11.24; Thu, 09 Apr 2020 20:11:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1726650AbgDJDKo (ORCPT + 99 others); Thu, 9 Apr 2020 23:10:44 -0400 Received: from mga17.intel.com ([192.55.52.151]:3010 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726594AbgDJDKo (ORCPT ); Thu, 9 Apr 2020 23:10:44 -0400 IronPort-SDR: ZcahRtawOFDT6eFMABk0RReiRug2Yq0kp88YdayXVAa9NYDSWnrWi7qm3Bot3M+UmMA0Yic6cT bA+jZlaX8osw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Apr 2020 20:10:44 -0700 IronPort-SDR: HXq/IUet9O+rMRtfb71iu/Z5PWBJGwtw0CRzb5s1veEmdOt3EU6LBZS6yZixcyw8nrJgJPodv1 heD13yhYH3AA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,364,1580803200"; d="scan'208";a="270283308" Received: from likexu-mobl1.ccr.corp.intel.com (HELO [10.238.4.236]) ([10.238.4.236]) by orsmga002.jf.intel.com with ESMTP; 09 Apr 2020 20:10:39 -0700 Reply-To: like.xu@intel.com Subject: Re: [PATCH v9 04/10] perf/x86: Keep LBR stack unchanged on the host for guest LBR event To: Peter Zijlstra , Like Xu Cc: Paolo Bonzini , kvm@vger.kernel.org, Andi Kleen , Jim Mattson , Wanpeng Li , Sean Christopherson , Joerg Roedel , Liran Alon , Thomas Gleixner , Ingo Molnar , Arnaldo Carvalho de Melo , Liang Kan , Wei Wang , linux-kernel@vger.kernel.org, Ingo Molnar References: <20200313021616.112322-1-like.xu@linux.intel.com> <20200313021616.112322-5-like.xu@linux.intel.com> <20200409164545.GE20713@hirez.programming.kicks-ass.net> From: "Xu, Like" Organization: Intel OTC Message-ID: <97059994-cf3c-2991-a0e2-02d02c344f1f@intel.com> Date: Fri, 10 Apr 2020 11:10:38 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: <20200409164545.GE20713@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/4/10 0:45, Peter Zijlstra wrote: > On Fri, Mar 13, 2020 at 10:16:10AM +0800, Like Xu wrote: >> When a guest wants to use the LBR stack, its hypervisor creates a guest >> LBR event and let host perf schedules it. A new 'int guest_lbr_enabled' >> field in the "struct cpu_hw_events", is marked as true when perf adds >> a guest LBR event and false on deletion. >> >> The LBR stack msrs are accessible to the guest when its guest LBR event >> is scheduled in by the perf subsystem. Before scheduling out the event, >> we should avoid host changes on IA32_DEBUGCTLMSR or LBR_SELECT. Otherwise, >> some unexpected branch operations may interfere with guest behavior, >> pollute LBR records, and even cause host branch data leakage. In addition, >> the intel_pmu_lbr_read() on the host is also avoidable for guest usage. >> >> On v4 PMU or later, the LBR stack are frozen on the overflowed condition >> if Freeze_LBR_On_PMI is true and resume recording via acking LBRS_FROZEN >> to global status msr instead of re-enabling IA32_DEBUGCTL.LBR. So when a >> guest LBR event is running, the host PMI handler has to keep LBRS_FROZEN >> bit set (thus LBR being frozen) until the guest enables it. Otherwise, >> when the guest enters non-root mode, the LBR will start recording and >> the guest PMI handler code will also pollute the LBR stack. >> >> To ensure that guest LBR records are not lost during the context switch, >> the BRANCH_CALL_STACK flag should be configured in the 'branch_sample_type' >> for a guest LBR event because a callstack event could save/restore guest >> unread records with the help of intel_pmu_lbr_sched_task() naturally. >> >> However, the regular host LBR perf event doesn't save/restore LBR_SELECT, >> because it's configured in the LBR_enable() based on branch_sample_type. >> So when a guest LBR is running, the guest LBR_SELECT may changes for its >> own use and we have to add the LBR_SELECT save/restore to ensure what the >> guest LBR_SELECT value doesn't get lost during the context switching. > I had to read the patch before that made sense; I think it's mostly > there, but it can use a little help. Ah, thanks for your patient. This is good news for me that you did read the main part of the proposal changes in this version. > > >> @@ -691,8 +714,12 @@ void intel_pmu_lbr_read(void) >> * >> * This could be smarter and actually check the event, >> * but this simple approach seems to work for now. >> + * >> + * And there is no need to read lbr here if a guest LBR event > There's 'lbr' and 'LBR' in the same sentence Yes, l'll fix it. > >> + * is using it, because the guest will read them on its own. >> */ >> - if (!cpuc->lbr_users || cpuc->lbr_users == cpuc->lbr_pebs_users) >> + if (!cpuc->lbr_users || cpuc->guest_lbr_enabled || >> + cpuc->lbr_users == cpuc->lbr_pebs_users) > indent fail Yes, l'll fix it. > >> return; >> >> if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_32)