Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp3083683rwe; Mon, 29 Aug 2022 05:40:27 -0700 (PDT) X-Google-Smtp-Source: AA6agR5iOoOXD8BBsd0Qo+gDFtRz+aHWj3V42ms6PFQ6pVGBcuPIzyqkInuV/tfClI9qwm71PlrM X-Received: by 2002:a17:90b:4b04:b0:1f5:2da0:b2f6 with SMTP id lx4-20020a17090b4b0400b001f52da0b2f6mr17663754pjb.195.1661776827126; Mon, 29 Aug 2022 05:40:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661776827; cv=none; d=google.com; s=arc-20160816; b=QyVzXCatsFr6rSXTnYg+Z4NuZ5ha+591zC3rTMq1Mlo5H/uNldnAFVdZu+VBlVy8wR ldLJlQll6V4p+B4eYGUr/V2CvntrJywbO+RJLXM1VlA+tVxMvTRwI9YMBpjhEtYVTAym O9rm34rTe0I4f0/3WmHZTlxCxuVAHPtLvazFxWvhk0SAR/Lon8d3rxxF7FbU2XaMNrfb AU1SSYdNITNCArAEsqN6qNAUWd8f8jkRxXm6xSTO1dnWFos3uqmnAeoIYt/fx6St54e2 Fc4hf2KwjOrsOizKOli3bpTU0rqzVsWBDMF9R2JyvRP78hxqQzX5fUZJkD2283jcsxpG Aeuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=HwKeim9rFKekZEGWlZB1+DWfwJIeop9QJpmd4Kv6oAM=; b=DFf2yqUkmYJrEFkuJtxIMlIqYV8c3gf8YEu5hBD0sS0HDnhi4ql18kGZKVL616k5ys 9MkzJguwVZpJXhr6c73Ltw17APg0ERBz7gozUty+8E8nnwDy9KixnuGMYy4xMJJUyts7 ewR0q8Z84TlvqkFEgTkmlM1tvhFYzfFQ+C542iFEW5wa7G2fIFMoOAww0vxU53CZy0kv lTJXwC05tZh8GUI7YjpTP4asIcj8g3Xp++CramFW9oAx/MoJRHoaEJNYgi4Lh7D9y3dv PH77RBCxxQIpb0dxBEKA2x7pam7nU35qWxcH4KyiV44ZN85Jj5Zk0uPFWaQwoBjzQakQ wqDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Xf5dNjOK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x184-20020a6386c1000000b0042b9b3beffdsi7026811pgd.1.2022.08.29.05.40.16; Mon, 29 Aug 2022 05:40:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Xf5dNjOK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232109AbiH2L1r (ORCPT + 99 others); Mon, 29 Aug 2022 07:27:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231627AbiH2LZs (ORCPT ); Mon, 29 Aug 2022 07:25:48 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3841177E8F; Mon, 29 Aug 2022 04:16:05 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 9C11DB80F79; Mon, 29 Aug 2022 11:13:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 00494C433D6; Mon, 29 Aug 2022 11:13:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661771628; bh=YN/Yc5vBxwdnysywUhNDn9acqzeEVELKrTo2kUZ9MVE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Xf5dNjOKnYrz5DLBfYVfcamtwOKa1JhVdXDRcpZT9v5I5BzipKZhMZM3xDQAKYXCL LRkc+lUyJGKBH/5Qs5B2tt5PFxnN4i2248dYsMupTo0kaeP+4FPnJqfvHoFu48uYdc KmF0/zcDHi7kEzhdhycBEVss2BTR6kQ4rjAGJs68= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Stephane Eranian , "Peter Zijlstra (Intel)" , Kan Liang Subject: [PATCH 5.10 83/86] perf/x86/intel/uncore: Fix broken read_counter() for SNB IMC PMU Date: Mon, 29 Aug 2022 12:59:49 +0200 Message-Id: <20220829105759.919082263@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220829105756.500128871@linuxfoundation.org> References: <20220829105756.500128871@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Stephane Eranian commit 11745ecfe8fea4b4a4c322967a7605d2ecbd5080 upstream. Existing code was generating bogus counts for the SNB IMC bandwidth counters: $ perf stat -a -I 1000 -e uncore_imc/data_reads/,uncore_imc/data_writes/ 1.000327813 1,024.03 MiB uncore_imc/data_reads/ 1.000327813 20.73 MiB uncore_imc/data_writes/ 2.000580153 261,120.00 MiB uncore_imc/data_reads/ 2.000580153 23.28 MiB uncore_imc/data_writes/ The problem was introduced by commit: 07ce734dd8ad ("perf/x86/intel/uncore: Clean up client IMC") Where the read_counter callback was replace to point to the generic uncore_mmio_read_counter() function. The SNB IMC counters are freerunnig 32-bit counters laid out contiguously in MMIO. But uncore_mmio_read_counter() is using a readq() call to read from MMIO therefore reading 64-bit from MMIO. Although this is okay for the uncore_perf_event_update() function because it is shifting the value based on the actual counter width to compute a delta, it is not okay for the uncore_pmu_event_start() which is simply reading the counter and therefore priming the event->prev_count with a bogus value which is responsible for causing bogus deltas in the perf stat command above. The fix is to reintroduce the custom callback for read_counter for the SNB IMC PMU and use readl() instead of readq(). With the change the output of perf stat is back to normal: $ perf stat -a -I 1000 -e uncore_imc/data_reads/,uncore_imc/data_writes/ 1.000120987 296.94 MiB uncore_imc/data_reads/ 1.000120987 138.42 MiB uncore_imc/data_writes/ 2.000403144 175.91 MiB uncore_imc/data_reads/ 2.000403144 68.50 MiB uncore_imc/data_writes/ Fixes: 07ce734dd8ad ("perf/x86/intel/uncore: Clean up client IMC") Signed-off-by: Stephane Eranian Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Kan Liang Link: https://lore.kernel.org/r/20220803160031.1379788-1-eranian@google.com Signed-off-by: Greg Kroah-Hartman --- arch/x86/events/intel/uncore_snb.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) --- a/arch/x86/events/intel/uncore_snb.c +++ b/arch/x86/events/intel/uncore_snb.c @@ -657,6 +657,22 @@ int snb_pci2phy_map_init(int devid) return 0; } +static u64 snb_uncore_imc_read_counter(struct intel_uncore_box *box, struct perf_event *event) +{ + struct hw_perf_event *hwc = &event->hw; + + /* + * SNB IMC counters are 32-bit and are laid out back to back + * in MMIO space. Therefore we must use a 32-bit accessor function + * using readq() from uncore_mmio_read_counter() causes problems + * because it is reading 64-bit at a time. This is okay for the + * uncore_perf_event_update() function because it drops the upper + * 32-bits but not okay for plain uncore_read_counter() as invoked + * in uncore_pmu_event_start(). + */ + return (u64)readl(box->io_addr + hwc->event_base); +} + static struct pmu snb_uncore_imc_pmu = { .task_ctx_nr = perf_invalid_context, .event_init = snb_uncore_imc_event_init, @@ -676,7 +692,7 @@ static struct intel_uncore_ops snb_uncor .disable_event = snb_uncore_imc_disable_event, .enable_event = snb_uncore_imc_enable_event, .hw_config = snb_uncore_imc_hw_config, - .read_counter = uncore_mmio_read_counter, + .read_counter = snb_uncore_imc_read_counter, }; static struct intel_uncore_type snb_uncore_imc = {