Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1014507imm; Wed, 19 Sep 2018 10:30:41 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZLyLbY3XgLWdmYXaVIYlR0WL4jRtDlfFMVHJmA8XFHNb5lEBduPdAnkJGAvjdjXK48sV6k X-Received: by 2002:a63:7e1c:: with SMTP id z28-v6mr32923852pgc.246.1537378241513; Wed, 19 Sep 2018 10:30:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537378241; cv=none; d=google.com; s=arc-20160816; b=Yulnt7OKbGDqywYmty5YU9oSrq59kk0Olfhs1p5DJCqoxHgqjLjEESVyc0hA+qgzc0 5x6xn6/0D4B1X19VB4fPvtq3IGGYd4/0bessQ9VD21qISDir64/oKrHePhQmFt+vfOgg WBI1fajaEWX1XyZO8niGMrsI1RhEj6DjkaxBK0JI4amt+PyZRiFRb71yi13fF3ARrB35 oUfO0Ctg8jaQ1EuqTrM06PYGZ4i8NyPW4RPOb9LWZyCXZcg31qZqp7W3dClWQwMUExZg W5SQcp90Wc72gBN7ZPsnHxseNZoDy9zYyvMs8x1aZiOd5XceAC2vRATBDO1JIZYjl6W4 J74Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:cc:to:from; bh=R7I9XIfBtrCDLjMrtDwz8l1bCnT457nlSuhtYqemH9A=; b=l9lmLlaQxdiIOj/bsnexQ4moZAap0hhfrPkLHNn6fyFSLv9p/4LhuvuG1cHq9/Ac6c LIH7IDHVZmFDzI+pjN83JokASAJ9JSM7dGQG2xvNVsmgoNAUqZ558w7wFq4MP5pPPWD+ Wo3h4G/rM22LpZztBjUl8XPcTl/jCB8TnxwQvjdR7eV7yivW/Qx6erQEpduL+1icMzH4 jzbbeP8ut6QX4+JPrvsXDEdPDs9yctZZSsf1998fKTBe9LEuf0G9Wp/gjKsMKXlmEC1Y BuIOMMzttWx1EsSQrWbtXLZL50r4O628/7hTKdl1U5ykiOyroR5YaggE5KmB6ca3tLVF Swyg== 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 b7-v6si23480753plm.250.2018.09.19.10.30.26; Wed, 19 Sep 2018 10:30:41 -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 S1733048AbeISXJB (ORCPT + 99 others); Wed, 19 Sep 2018 19:09:01 -0400 Received: from mga05.intel.com ([192.55.52.43]:33318 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733038AbeISXJA (ORCPT ); Wed, 19 Sep 2018 19:09:00 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Sep 2018 10:30:03 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,394,1531810800"; d="scan'208";a="87551429" Received: from rchatre-s.jf.intel.com ([10.54.70.76]) by fmsmga002.fm.intel.com with ESMTP; 19 Sep 2018 10:30:01 -0700 From: Reinette Chatre To: tglx@linutronix.de, fenghua.yu@intel.com, tony.luck@intel.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org Cc: gavin.hindman@intel.com, jithu.joseph@intel.com, dave.hansen@intel.com, hpa@zytor.com, x86@kernel.org, linux-kernel@vger.kernel.org, Reinette Chatre Subject: [PATCH V5 2/6] perf/x86: Add helper to obtain performance counter index Date: Wed, 19 Sep 2018 10:29:07 -0700 Message-Id: X-Mailer: git-send-email 2.17.0 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org perf_event_read_local() is the safest way to obtain measurements associated with performance events. In some cases the overhead introduced by perf_event_read_local() affects the measurements and the use of rdpmcl() is needed. rdpmcl() requires the index of the performance counter used so a helper is introduced to determine the index used by a provided performance event. The index used by a performance event may change when interrupts are enabled. A check is added to ensure that the index is only accessed with interrupts disabled. Even with this check the use of this counter needs to be done with care to ensure it is queried and used within the same disabled interrupts section. This change introduces a new checkpatch warning: CHECK: extern prototypes should be avoided in .h files +extern int x86_perf_rdpmc_index(struct perf_event *event); This warning was discussed and designated as a false positive in http://lkml.kernel.org/r/20180919091759.GZ24124@hirez.programming.kicks-ass.net Link: http://lkml.kernel.org/r/20180917230758.GA3117@worktop.programming.kicks-ass.net Suggested-by: Peter Zijlstra Signed-off-by: Reinette Chatre --- arch/x86/events/core.c | 21 +++++++++++++++++++++ arch/x86/include/asm/perf_event.h | 1 + 2 files changed, 22 insertions(+) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index dfb2f7c0d019..3550d800b030 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -1033,6 +1033,27 @@ static inline void x86_assign_hw_event(struct perf_event *event, } } +/** + * x86_perf_rdpmc_index - Return PMC counter used for event + * @event: the perf_event to which the PMC counter was assigned + * + * The counter assigned to this performance event may change if interrupts + * are enabled. This counter should thus never be used while interrupts are + * enabled. Before this function is used to obtain the assigned counter the + * event should be checked for validity using, for example, + * perf_event_read_local(), within the same interrupt disabled section in + * which this counter is planned to be used. + * + * Return: The index of the performance monitoring counter assigned to + * @perf_event. + */ +int x86_perf_rdpmc_index(struct perf_event *event) +{ + lockdep_assert_irqs_disabled(); + + return event->hw.event_base_rdpmc; +} + static inline int match_prev_assignment(struct hw_perf_event *hwc, struct cpu_hw_events *cpuc, int i) diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_event.h index 12f54082f4c8..b2cf84c35a6d 100644 --- a/arch/x86/include/asm/perf_event.h +++ b/arch/x86/include/asm/perf_event.h @@ -270,6 +270,7 @@ struct perf_guest_switch_msr { extern struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr); extern void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap); extern void perf_check_microcode(void); +extern int x86_perf_rdpmc_index(struct perf_event *event); #else static inline struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr) { -- 2.17.0