Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751636AbcLEKZg (ORCPT ); Mon, 5 Dec 2016 05:25:36 -0500 Received: from bombadil.infradead.org ([198.137.202.9]:51258 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750928AbcLEKZQ (ORCPT ); Mon, 5 Dec 2016 05:25:16 -0500 Date: Mon, 5 Dec 2016 11:25:09 +0100 From: Peter Zijlstra To: "Odzioba, Lukasz" Cc: "Liang, Kan" , Stephane Eranian , "mingo@redhat.com" , LKML , Alexander Shishkin , "ak@linux.intel.com" Subject: Re: [PATCH] perf/x86: fix event counter update issue Message-ID: <20161205102509.GH3124@twins.programming.kicks-ass.net> References: <1480361206-1702-1-git-send-email-kan.liang@intel.com> <20161129092520.GB3092@twins.programming.kicks-ass.net> <20161129173055.GP3092@twins.programming.kicks-ass.net> <37D7C6CF3E00A74B8858931C1DB2F07750CA4225@SHSMSX103.ccr.corp.intel.com> <20161129193201.GE3045@worktop.programming.kicks-ass.net> <37D7C6CF3E00A74B8858931C1DB2F07750CA42A3@SHSMSX103.ccr.corp.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23.1 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2447 Lines: 70 On Fri, Dec 02, 2016 at 12:58:17PM +0000, Odzioba, Lukasz wrote: > On Tuesday, November 29, 2016 9:33 PM, Liang, Kan wrote: > > Yes, the patch as below fixes the issue on my SLM. > > It works for me as well. > Can we still have it in 4.9? I'll certainly, try. I've queued it as per the below. --- Subject: perf,x86: Fix full width counter, counter overflow Date: Tue, 29 Nov 2016 20:33:28 +0000 Lukasz reported that perf stat counters overflow is broken on KNL/SLM. Both these parts have full_width_write set, and that does indeed have a problem. In order to deal with counter wrap, we must sample the counter at at least half the counter period (see also the sampling theorem) such that we can unambiguously reconstruct the count. However commit: 069e0c3c4058 ("perf/x86/intel: Support full width counting") sets the sampling interval to the full period, not half. Fixing that exposes another issue, in that we must not sign extend the delta value when we shift it right; the counter cannot have decremented after all. With both these issues fixed, counter overflow functions correctly again. Cc: "mingo@redhat.com" Cc: "ak@linux.intel.com" Cc: Stephane Eranian Cc: Alexander Shishkin Cc: stable@vger.kernel.org Reported-by: Lukasz Odzioba Tested-by: "Liang, Kan" Tested-by: "Odzioba, Lukasz" Fixes: 069e0c3c4058 ("perf/x86/intel: Support full width counting") Signed-off-by: Peter Zijlstra (Intel) --- arch/x86/events/core.c | 2 +- arch/x86/events/intel/core.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -69,7 +69,7 @@ u64 x86_perf_event_update(struct perf_ev int shift = 64 - x86_pmu.cntval_bits; u64 prev_raw_count, new_raw_count; int idx = hwc->idx; - s64 delta; + u64 delta; if (idx == INTEL_PMC_IDX_FIXED_BTS) return 0; --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -4034,7 +4034,7 @@ __init int intel_pmu_init(void) /* Support full width counters using alternative MSR range */ if (x86_pmu.intel_cap.full_width_write) { - x86_pmu.max_period = x86_pmu.cntval_mask; + x86_pmu.max_period = x86_pmu.cntval_mask >> 1; x86_pmu.perfctr = MSR_IA32_PMC0; pr_cont("full-width counters, "); }