Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1612613imm; Sat, 29 Sep 2018 00:02:43 -0700 (PDT) X-Google-Smtp-Source: ACcGV62KMrmeKwYhx65fhbjoPu7GzaG8gW2O1Bgxt0bBL2+PpGYTE1SAvAE2Acbym8UMKMHOxJWz X-Received: by 2002:a62:5882:: with SMTP id m124-v6mr1884175pfb.249.1538204563213; Sat, 29 Sep 2018 00:02:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538204563; cv=none; d=google.com; s=arc-20160816; b=nLgPQ8pe0RvHQk+ykXX5cFfOJ6Z5ui5+8ojBQDK3+Wkdv7FZ7llngmQ1d/PZYypd79 qqtz8yxMZagV3bcdW21rp6h3Do9ktBZENOFl6K8dSAIJnQppfLfCBfQYsAS5OwXUPleJ EGio55yh0SPRrffvA7Mnf9MkRPQxALs47lyt1RYAN9M2bQgb7nVxTxwJhjt3gUisaVMY POOzk/MYjpaooKolnBWt0WuUfpanhTPFtf+hxuUSGEGdELfReOamQEROcMCQyeTYewkr a7FaJsRqkNdpHmIrKmwYaufEoQgH6RymARGN6ZkJTYwf+CAhpxeavxTlMmWWxgzP4FmA fHQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date; bh=1Woy75Alnmg4KxvAY+qu/F4Ko5HyCCSfDDjVH4DEW8o=; b=NjKJ1XCRYcB3q/seEtShaXShKeciJgpsD2KPRZKCvIE58x1Z8GQmBU/bbwzTae9rG4 76XLfvntW8UctuqJxuEE2EwhpSiCCwz5PSq/t80/jnoiUY3Fz05dGR3tdWs0Ce9BdhxO YPZ3BmyR4eUa76q5fiHXQwfIvbvWqvU53mIX74rPBlmNCwF1Q29pjk92HLQcQK4JaQ+F g+/YxYKszQvRcWnqbrDaU5FanvAfuNrq9FZhI6hXfn1XbpmWYbtv/935i7fIj/CdJ2+O kVMxOeF5JuuXvwGO0kLAnqsItP0mePPpadu2ObpdKoq6SOTI6MyEjP1NCBbQ+K1Cb8Nr l58w== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d7-v6si6998223pln.68.2018.09.29.00.01.59; Sat, 29 Sep 2018 00:02:43 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727595AbeI2N2l (ORCPT + 99 others); Sat, 29 Sep 2018 09:28:41 -0400 Received: from terminus.zytor.com ([198.137.202.136]:50713 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727393AbeI2N2l (ORCPT ); Sat, 29 Sep 2018 09:28:41 -0400 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTPS id w8T70qeJ726300 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Sat, 29 Sep 2018 00:00:52 -0700 Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id w8T70q46726297; Sat, 29 Sep 2018 00:00:52 -0700 Date: Sat, 29 Sep 2018 00:00:52 -0700 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for Reinette Chatre Message-ID: Cc: hpa@zytor.com, mingo@kernel.org, peterz@infradead.org, tglx@linutronix.de, reinette.chatre@intel.com, linux-kernel@vger.kernel.org Reply-To: hpa@zytor.com, linux-kernel@vger.kernel.org, reinette.chatre@intel.com, tglx@linutronix.de, mingo@kernel.org, peterz@infradead.org In-Reply-To: References: To: linux-tip-commits@vger.kernel.org Subject: [tip:x86/cache] perf/x86: Add helper to obtain performance counter index Git-Commit-ID: 1182a49529edde899be4b4f0e1ab76e626976eb6 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=0.3 required=5.0 tests=ALL_TRUSTED,BAYES_00, DATE_IN_FUTURE_12_24 autolearn=no autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 1182a49529edde899be4b4f0e1ab76e626976eb6 Gitweb: https://git.kernel.org/tip/1182a49529edde899be4b4f0e1ab76e626976eb6 Author: Reinette Chatre AuthorDate: Wed, 19 Sep 2018 10:29:07 -0700 Committer: Thomas Gleixner CommitDate: Fri, 28 Sep 2018 22:48:26 +0200 perf/x86: Add helper to obtain performance counter index 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 Suggested-by: Peter Zijlstra Signed-off-by: Reinette Chatre Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Cc: fenghua.yu@intel.com Cc: tony.luck@intel.com Cc: acme@kernel.org Cc: gavin.hindman@intel.com Cc: jithu.joseph@intel.com Cc: dave.hansen@intel.com Cc: hpa@zytor.com Link: https://lkml.kernel.org/r/b277ffa78a51254f5414f7b1bc1923826874566e.1537377064.git.reinette.chatre@intel.com --- 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) {