Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp4495760ybc; Fri, 15 Nov 2019 05:41:44 -0800 (PST) X-Google-Smtp-Source: APXvYqxB2jceKFgLy+H11NcdNiJHOgTxhCbRQozTKmCcq6Bmpo6cZevmOqDchU1+Nnm4K2JwCu5F X-Received: by 2002:a1c:7e91:: with SMTP id z139mr14808819wmc.15.1573825304584; Fri, 15 Nov 2019 05:41:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573825304; cv=none; d=google.com; s=arc-20160816; b=mCsk3jxsoGTNvwCCujGc8LhAFlFEL7/ySmkGjyu4I3BBPu92Mcv69Fh2tR29HdpSeo gNHL3EIC7/AzrdwNqF0jbfUgoRB5Www7MHY4fjvDEdmI1NKuUkTWhkU232+al1QpIo0c BcJO6U5Uy7E/B1K1eXLKircZ2D3sVjRX1pbXX1RnN7FGDjpppPZUGw6JSV+o3QAo343V 8Ca1+Zx2wwLcv5y9jQTetduvDlrkUefZNUQyn+x10/S+nzbT8RjmQ0ab0H4cmPHH+Oal WPIPajpGbnQyOEvJoqwAYO7Cr02fbs2yGU1/EOcl9/OhQv5BP605iTF70u0pADAeYoT1 f6qQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=5LioITj+jw8aUHPbjpM//RUu4t7nU0cpeRgybK/tCZ8=; b=NJ5/qHmJLCclaMZzU/vHj4DPhyvNJ6hEhRMMqVUeMV+08jPdlvs1zJeQLUFTLnFiMF yeRNWK0xiCeOM6PHvV2eG+n5HuCVlkO7+ndWTF5dWvBsv5vkDxz4j32Eu1iEQOtB9TKN hma81UR2mdOfBPOuExtOUp91XmK8zFjwqTX5i0AWr6ZyhJ3OqX0/4q18RDeF2Y3gPbTH DQgyMUKO+ZOqxovKyrfkjt/T5ZR/G69NW9P2sp7zucYl9y/zo7YcLdEUhbMXAXMXwszR 4rfD3p/+iu+V/EoNungQD+/AOMYxhA7vxyX6To/+8QAMqxO0fuBKv/Es5NnnHSlHF/E8 9LSg== 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 m13si7220905edc.243.2019.11.15.05.41.19; Fri, 15 Nov 2019 05:41:44 -0800 (PST) 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 S1727640AbfKONj4 (ORCPT + 99 others); Fri, 15 Nov 2019 08:39:56 -0500 Received: from mga17.intel.com ([192.55.52.151]:22156 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727401AbfKONjz (ORCPT ); Fri, 15 Nov 2019 08:39:55 -0500 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/DHE-RSA-AES256-GCM-SHA384; 15 Nov 2019 05:39:55 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,308,1569308400"; d="scan'208";a="217099332" Received: from labuser-ice-lake-client-platform.jf.intel.com ([10.54.55.25]) by orsmga002.jf.intel.com with ESMTP; 15 Nov 2019 05:39:55 -0800 From: kan.liang@linux.intel.com To: peterz@infradead.org, acme@redhat.com, mingo@kernel.org, linux-kernel@vger.kernel.org Cc: ak@linux.intel.com, eranian@google.com, Kan Liang Subject: [PATCH] perf/x86/intel: Avoid PEBS_ENABLE MSR access in PMI Date: Fri, 15 Nov 2019 05:39:17 -0800 Message-Id: <20191115133917.24424-1-kan.liang@linux.intel.com> X-Mailer: git-send-email 2.17.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kan Liang The perf PMI handler, intel_pmu_handle_irq(), currently does unnecessary MSR accesses when PEBS is enabled. When entering the handler, global ctrl is explicitly disabled. All counters do not count anymore. It doesn't matter if the PEBS is enabled or disabled. Furthermore, cpuc->pebs_enabled is not changed in PMI. The PEBS status doesn't change. The PEBS_ENABLE MSR doesn't need to be changed either. When exiting the handler, only the active PMU will be restore. For active PMU, PEBS status is unchanged during the PMI handler. Avoiding PEBS MSR access is harmless. For inactive PMU, disable_all() will be called right after PMI handler, which will eventually disable PEBS. During the period between PMI handler exit and PEBS finally disabled, the global ctrl is always disabled since we don't restore PMU state for inactive PMU. This case is also harmless. Use ftrace to measure the duration of intel_pmu_handle_irq() on BDX. #perf record -e cycles:P -- ./tchain_edit The average duration of intel_pmu_handle_irq() Without the patch 1.144 us With the patch 1.025 us Signed-off-by: Kan Liang --- arch/x86/events/intel/core.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index dc64b16e6b71..d715eb966334 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -1945,6 +1945,11 @@ static __initconst const u64 knl_hw_cache_extra_regs * intel_bts events don't coexist with intel PMU's BTS events because of * x86_add_exclusive(x86_lbr_exclusive_lbr); there's no need to keep them * disabled around intel PMU's event batching etc, only inside the PMI handler. + * + * Avoid PEBS_ENABLE MSR access in PMIs + * The GLOBAL_CTRL has been disabled. All counters do not count anymore. + * It doesn't matter if the PEBS is enabled or disabled. + * Furthermore, PEBS status doesn't change in PMI. */ static void __intel_pmu_disable_all(void) { @@ -1954,13 +1959,12 @@ static void __intel_pmu_disable_all(void) if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask)) intel_pmu_disable_bts(); - - intel_pmu_pebs_disable_all(); } static void intel_pmu_disable_all(void) { __intel_pmu_disable_all(); + intel_pmu_pebs_disable_all(); intel_pmu_lbr_disable_all(); } @@ -1968,7 +1972,6 @@ static void __intel_pmu_enable_all(int added, bool pmi) { struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); - intel_pmu_pebs_enable_all(); intel_pmu_lbr_enable_all(pmi); wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_guest_mask); @@ -1986,6 +1989,7 @@ static void __intel_pmu_enable_all(int added, bool pmi) static void intel_pmu_enable_all(int added) { + intel_pmu_pebs_enable_all(); __intel_pmu_enable_all(added, false); } -- 2.17.1