From patchwork Fri Jan 8 10:15:01 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brendan Gregg X-Patchwork-Id: 7983741 Return-Path: X-Original-To: patchwork-xen-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 18D4EBEEE5 for ; Fri, 8 Jan 2016 10:18:05 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DE76A20107 for ; Fri, 8 Jan 2016 10:18:03 +0000 (UTC) Received: from lists.xen.org (lists.xenproject.org [50.57.142.19]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 121A02017D for ; Fri, 8 Jan 2016 10:18:02 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xen.org) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1aHU4j-0006lW-Rn; Fri, 08 Jan 2016 10:15:17 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1aHU4i-0006l0-3T for xen-devel@lists.xen.org; Fri, 08 Jan 2016 10:15:16 +0000 Received: from [85.158.137.68] by server-5.bemta-3.messagelabs.com id E1/69-07651-33C8F865; Fri, 08 Jan 2016 10:15:15 +0000 X-Env-Sender: bgregg@netflix.com X-Msg-Ref: server-10.tower-31.messagelabs.com!1452248109!14615173!1 X-Originating-IP: [209.85.192.170] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 7.35.1; banners=-,-,- X-VirusChecked: Checked Received: (qmail 50816 invoked from network); 8 Jan 2016 10:15:10 -0000 Received: from mail-pf0-f170.google.com (HELO mail-pf0-f170.google.com) (209.85.192.170) by server-10.tower-31.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 8 Jan 2016 10:15:10 -0000 Received: by mail-pf0-f170.google.com with SMTP id n128so7969795pfn.3 for ; Fri, 08 Jan 2016 02:15:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netflix.com; s=google; h=from:to:cc:subject:date:message-id; bh=WWqh6L/Fa6nVqCpl+uJcL/KsGVHyUHci2pxcz8cy+rA=; b=SRKGSFNsjaY7XcCXHuLFvCdhAJRJlJLkeeT16iVOHsY04dlkc+z9UE1hO2OAkcsN+R nax7MElypFVTIlCY4s0u7u+DFfqesvNq/UD2edqfq8jK0ZnhEdZ9ORiAzl70JiShDSsp QY9oP2ADHGDAchi1eyJDIPMD/Jk8OdyG3cKDs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=WWqh6L/Fa6nVqCpl+uJcL/KsGVHyUHci2pxcz8cy+rA=; b=FC7KPUr6aHW6fDhQa/Bg951XTlkHX+Y6keCT/B0QHYfMSoOnD9X16G18hANmWZCllO +EQrm/HuEwDfQWhXi702lggcyClYbJxP0CIau1agcTGPPdZK+5WH58I1QBQ3MAB3quxY 7BBuk3iJTOgmFeZlbklVSu5gLR++Ky6mwBZ+3Dh9S79nSZXMX9F/W7sxaM3KUlfVjCT1 kCK2cvTy2di3puukLlA9IRj2u1PSBt+7HQEQ9Nne+cVnPyElc/6IoQRREzvNHGdtPzrn 2D1ETymy0I0VdohfT57umjvie3t0l9PFKtT7hKntS/Lx8G8ICZZByjItCh+tcI9Yn5Sw ipfg== X-Gm-Message-State: ALoCoQl8+tejhXc33xT7sxkE/ISxpBYWa6aWeqy8MgkJemomJuZNL3BpcrUrwgSLknziQxRK/DMK6PLhsz9KNRTckj7WsUE/gA== X-Received: by 10.98.18.202 with SMTP id 71mr3156868pfs.103.1452248109162; Fri, 08 Jan 2016 02:15:09 -0800 (PST) Received: from lgud-bgregg.corp.netflix.com ([69.53.232.0]) by smtp.gmail.com with ESMTPSA id r83sm3622042pfr.92.2016.01.08.02.15.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 08 Jan 2016 02:15:08 -0800 (PST) From: Brendan Gregg To: xen-devel@lists.xen.org Date: Fri, 8 Jan 2016 02:15:01 -0800 Message-Id: <1452248101-4802-1-git-send-email-bgregg@netflix.com> X-Mailer: git-send-email 1.9.1 Cc: kevin.tian@intel.com, jbeulich@suse.com, andrew.cooper3@citrix.com, dietmar.hahn@ts.fujitsu.com, jun.nakajima@intel.com, boris.ostrovsky@oracle.com, Brendan Gregg Subject: [Xen-devel] [PATCH v6] x86/VPMU: implement ipc and arch filter flags X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This introduces a way to have a restricted VPMU, by specifying one of two predefined groups of PMCs to make available. For secure environments, this allows the VPMU to be used without needing to enable all PMCs. Signed-off-by: Brendan Gregg Reviewed-by: Boris Ostrovsky Acked-by: Kevin Tian --- Changes in v6: * addressing review comments from Jan: * code style changes * updated sysfs PMU interface flag check Changes in v5: * addressing review comments from Kevin: * simplified BTS DS_AREA filter * improvements to comments, incl. matching Intel SDM descriptions Changes in v4: * changed table reference in comments, suggested by Dietmar Changes in v3: * addressing review comments from Boris: * ensure final flag is validated * code tidy Changes in v2: * feature flags can now be combined (eg, "vpmu=ipc,bts") * addressing review comments from Boris: * restrict DS_AREA and PEBS_ENABLE access when filters are in use * better variable types * include MSR_IA32_CMT_EVTSEL_UE_MASK flag --- docs/misc/xen-command-line.markdown | 15 +++++++++- xen/arch/x86/cpu/vpmu.c | 55 +++++++++++++++++++++++++++++-------- xen/arch/x86/cpu/vpmu_intel.c | 44 +++++++++++++++++++++++++++++ xen/include/asm-x86/msr-index.h | 1 + xen/include/public/pmu.h | 14 ++++++++-- 5 files changed, 114 insertions(+), 15 deletions(-) diff --git a/docs/misc/xen-command-line.markdown b/docs/misc/xen-command-line.markdown index 467dc8f..5ed0730 100644 --- a/docs/misc/xen-command-line.markdown +++ b/docs/misc/xen-command-line.markdown @@ -1476,7 +1476,7 @@ Use Virtual Processor ID support if available. This prevents the need for TLB flushes on VM entry and exit, increasing performance. ### vpmu -> `= ( bts )` +> `= ( | { bts | ipc | arch [, ...] } )` > Default: `off` @@ -1492,6 +1492,19 @@ wrong behaviour (see handle\_pmc\_quirk()). If 'vpmu=bts' is specified the virtualisation of the Branch Trace Store (BTS) feature is switched on on Intel processors supporting this feature. +vpmu=ipc enables performance monitoring, but restricts the counters to the +most minimum set possible: instructions, cycles, and reference cycles. These +can be used to calculate instructions per cycle (IPC). + +vpmu=arch enables performance monitoring, but restricts the counters to the +pre-defined architectural events only. These are exposed by cpuid, and listed +in the Pre-Defined Architectural Performance Events table from the Intel 64 +and IA-32 Architectures Software Developer's Manual, Volume 3B, System +Programming Guide, Part 2. + +If a boolean is not used, combinations of flags are allowed, comma separated. +For example, vpmu=arch,bts. + Note that if **watchdog** option is also specified vpmu will be turned off. *Warning:* diff --git a/xen/arch/x86/cpu/vpmu.c b/xen/arch/x86/cpu/vpmu.c index 4856e98..5ca1db5 100644 --- a/xen/arch/x86/cpu/vpmu.c +++ b/xen/arch/x86/cpu/vpmu.c @@ -43,34 +43,59 @@ CHECK_pmu_data; CHECK_pmu_params; /* - * "vpmu" : vpmu generally enabled - * "vpmu=off" : vpmu generally disabled - * "vpmu=bts" : vpmu enabled and Intel BTS feature switched on. + * "vpmu" : vpmu generally enabled (all counters) + * "vpmu=off" : vpmu generally disabled + * "vpmu=bts" : vpmu enabled and Intel BTS feature switched on. + * "vpmu=ipc" : vpmu enabled for IPC counters only (most restrictive) + * "vpmu=arch" : vpmu enabled for predef arch counters only (restrictive) + * flag combinations are allowed, eg, "vpmu=ipc,bts". */ static unsigned int __read_mostly opt_vpmu_enabled; unsigned int __read_mostly vpmu_mode = XENPMU_MODE_OFF; unsigned int __read_mostly vpmu_features = 0; -static void parse_vpmu_param(char *s); -custom_param("vpmu", parse_vpmu_param); +static void parse_vpmu_params(char *s); +custom_param("vpmu", parse_vpmu_params); static DEFINE_SPINLOCK(vpmu_lock); static unsigned vpmu_count; static DEFINE_PER_CPU(struct vcpu *, last_vcpu); -static void __init parse_vpmu_param(char *s) +static int parse_vpmu_param(char *s, unsigned int len) { + if ( !*s || !len ) + return 0; + if ( !strncmp(s, "bts", len) ) + vpmu_features |= XENPMU_FEATURE_INTEL_BTS; + else if ( !strncmp(s, "ipc", len) ) + vpmu_features |= XENPMU_FEATURE_IPC_ONLY; + else if ( !strncmp(s, "arch", len) ) + vpmu_features |= XENPMU_FEATURE_ARCH_ONLY; + else + return 1; + return 0; +} + +static void __init parse_vpmu_params(char *s) +{ + char *sep, *p = s; + switch ( parse_bool(s) ) { case 0: break; default: - if ( !strcmp(s, "bts") ) - vpmu_features |= XENPMU_FEATURE_INTEL_BTS; - else if ( *s ) + while (1) { - printk("VPMU: unknown flag: %s - vpmu disabled!\n", s); - break; + sep = strchr(p, ','); + if ( sep == NULL ) + sep = strchr(p, 0); + if ( parse_vpmu_param(p, sep - p) ) + goto error; + if ( !*sep ) + /* reached end of flags */ + break; + p = sep + 1; } /* fall through */ case 1: @@ -79,6 +104,10 @@ static void __init parse_vpmu_param(char *s) opt_vpmu_enabled = 1; break; } + return; + + error: + printk("VPMU: unknown flags: %s - vpmu disabled!\n", s); } void vpmu_lvtpc_update(uint32_t val) @@ -708,7 +737,9 @@ long do_xenpmu_op(unsigned int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg) break; case XENPMU_feature_set: - if ( pmu_params.val & ~XENPMU_FEATURE_INTEL_BTS ) + if ( pmu_params.val & ~(XENPMU_FEATURE_INTEL_BTS | + XENPMU_FEATURE_IPC_ONLY | + XENPMU_FEATURE_ARCH_ONLY)) return -EINVAL; spin_lock(&vpmu_lock); diff --git a/xen/arch/x86/cpu/vpmu_intel.c b/xen/arch/x86/cpu/vpmu_intel.c index 3eff1ae..e59837a 100644 --- a/xen/arch/x86/cpu/vpmu_intel.c +++ b/xen/arch/x86/cpu/vpmu_intel.c @@ -604,12 +604,17 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content, "MSR_PERF_GLOBAL_STATUS(0x38E)!\n"); return -EINVAL; case MSR_IA32_PEBS_ENABLE: + if ( vpmu_features & (XENPMU_FEATURE_IPC_ONLY | + XENPMU_FEATURE_ARCH_ONLY) ) + return -EINVAL; if ( msr_content & 1 ) gdprintk(XENLOG_WARNING, "Guest is trying to enable PEBS, " "which is not supported.\n"); core2_vpmu_cxt->pebs_enable = msr_content; return 0; case MSR_IA32_DS_AREA: + if ( !(vpmu_features & XENPMU_FEATURE_INTEL_BTS) ) + return -EINVAL; if ( vpmu_is_set(vpmu, VPMU_CPU_HAS_DS) ) { if ( !(has_hvm_container_vcpu(v) @@ -656,12 +661,51 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content, tmp = msr - MSR_P6_EVNTSEL(0); if ( tmp >= 0 && tmp < arch_pmc_cnt ) { + bool_t blocked = 0; + uint64_t umaskevent; struct xen_pmu_cntr_pair *xen_pmu_cntr_pair = vpmu_reg_pointer(core2_vpmu_cxt, arch_counters); if ( msr_content & ARCH_CTRL_MASK ) return -EINVAL; + /* PMC filters */ + umaskevent = msr_content & MSR_IA32_CMT_EVTSEL_UE_MASK; + if ( vpmu_features & (XENPMU_FEATURE_IPC_ONLY || + XENPMU_FEATURE_ARCH_ONLY) ) + { + blocked = 1; + switch ( umaskevent ) + { + /* + * See the Pre-Defined Architectural Performance Events table + * from the Intel 64 and IA-32 Architectures Software + * Developer's Manual, Volume 3B, System Programming Guide, + * Part 2. + */ + case 0x003c: /* UnHalted Core Cycles */ + case 0x013c: /* UnHalted Reference Cycles */ + case 0x00c0: /* Instructions Retired */ + blocked = 0; + } + } + + if ( vpmu_features & XENPMU_FEATURE_ARCH_ONLY ) + { + /* Additional counters beyond IPC only; blocked already set. */ + switch ( umaskevent ) + { + case 0x4f2e: /* Last Level Cache References */ + case 0x412e: /* Last Level Cache Misses */ + case 0x00c4: /* Branch Instructions Retired */ + case 0x00c5: /* All Branch Mispredict Retired */ + blocked = 0; + } + } + + if ( blocked ) + return -EINVAL; + if ( has_hvm_container_vcpu(v) ) vmx_read_guest_msr(MSR_CORE_PERF_GLOBAL_CTRL, &core2_vpmu_cxt->global_ctrl); diff --git a/xen/include/asm-x86/msr-index.h b/xen/include/asm-x86/msr-index.h index 2b97861..84da631 100644 --- a/xen/include/asm-x86/msr-index.h +++ b/xen/include/asm-x86/msr-index.h @@ -329,6 +329,7 @@ /* Platform Shared Resource MSRs */ #define MSR_IA32_CMT_EVTSEL 0x00000c8d +#define MSR_IA32_CMT_EVTSEL_UE_MASK 0x0000ffff #define MSR_IA32_CMT_CTR 0x00000c8e #define MSR_IA32_PSR_ASSOC 0x00000c8f #define MSR_IA32_PSR_L3_QOS_CFG 0x00000c81 diff --git a/xen/include/public/pmu.h b/xen/include/public/pmu.h index 7753df0..0e1312c 100644 --- a/xen/include/public/pmu.h +++ b/xen/include/public/pmu.h @@ -84,9 +84,19 @@ DEFINE_XEN_GUEST_HANDLE(xen_pmu_params_t); /* * PMU features: - * - XENPMU_FEATURE_INTEL_BTS: Intel BTS support (ignored on AMD) + * - XENPMU_FEATURE_INTEL_BTS: Intel BTS support (ignored on AMD) + * - XENPMU_FEATURE_IPC_ONLY: Restrict PMCs to the most minimum set possible. + * Instructions, cycles, and ref cycles. Can be + * used to calculate instructions-per-cycle (IPC) + * (ignored on AMD). + * - XENPMU_FEATURE_ARCH_ONLY: Restrict PMCs to the Intel Pre-Defined + * Architectural Performance Events exposed by + * cpuid and listed in the Intel developer's manual + * (ignored on AMD). */ -#define XENPMU_FEATURE_INTEL_BTS 1 +#define XENPMU_FEATURE_INTEL_BTS (1<<0) +#define XENPMU_FEATURE_IPC_ONLY (1<<1) +#define XENPMU_FEATURE_ARCH_ONLY (1<<2) /* * Shared PMU data between hypervisor and PV(H) domains.