Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3FF4EC61DA4 for ; Thu, 9 Feb 2023 12:06:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230009AbjBIMGF (ORCPT ); Thu, 9 Feb 2023 07:06:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229809AbjBIMFp (ORCPT ); Thu, 9 Feb 2023 07:05:45 -0500 Received: from mail-sh.amlogic.com (mail-sh.amlogic.com [58.32.228.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 112AC518D3 for ; Thu, 9 Feb 2023 03:55:19 -0800 (PST) Received: from droid01-xa.amlogic.com (10.88.11.200) by mail-sh.amlogic.com (10.18.11.5) with Microsoft SMTP Server id 15.1.2507.13; Thu, 9 Feb 2023 19:54:12 +0800 From: Jiucheng Xu To: Jiucheng Xu , Will Deacon , Mark Rutland , Neil Armstrong , Kevin Hilman , Jerome Brunet , Martin Blumenstingl CC: Jianxin Pan , Kelvin Zhang , Chris Healy , Chris Healy , , , Subject: [PATCH 2/3] perf/amlogic: Fix large number of counter issue Date: Thu, 9 Feb 2023 19:54:02 +0800 Message-ID: <20230209115403.521868-2-jiucheng.xu@amlogic.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230209115403.521868-1-jiucheng.xu@amlogic.com> References: <20230209115403.521868-1-jiucheng.xu@amlogic.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.88.11.200] Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When use 1ms interval, very large number of counter happens once in a while as below: 25.968654513 281474976710655.84 MB meson_ddr_bw/chan_1_rw_bytes,arm=1/ 26.118657346 281474976710655.88 MB meson_ddr_bw/chan_1_rw_bytes,arm=1/ 26.180137180 281474976710655.66 MB meson_ddr_bw/chan_1_rw_bytes,arm=1/ Root cause is the race between irq handler and pmu.read callback. Use spin lock to protect the sw&hw counters. Signed-off-by: Jiucheng Xu --- drivers/perf/amlogic/meson_ddr_pmu_core.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/perf/amlogic/meson_ddr_pmu_core.c b/drivers/perf/amlogic/meson_ddr_pmu_core.c index 0b24dee1ed3c..9b2e5d5c0626 100644 --- a/drivers/perf/amlogic/meson_ddr_pmu_core.c +++ b/drivers/perf/amlogic/meson_ddr_pmu_core.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -23,6 +24,7 @@ struct ddr_pmu { struct pmu pmu; struct dmc_info info; struct dmc_counter counters; /* save counters from hw */ + spinlock_t lock; /* protect hw/sw counter */ bool pmu_enabled; struct device *dev; char *name; @@ -92,10 +94,12 @@ static void meson_ddr_perf_event_update(struct perf_event *event) int idx; int chann_nr = pmu->info.hw_info->chann_nr; + spin_lock(&pmu->lock); /* get the remain counters in register. */ pmu->info.hw_info->get_counters(&pmu->info, &dc); ddr_cnt_addition(&sum_dc, &pmu->counters, &dc, chann_nr); + spin_unlock(&pmu->lock); switch (event->attr.config) { case ALL_CHAN_COUNTER_ID: @@ -355,6 +359,7 @@ static irqreturn_t dmc_irq_handler(int irq, void *dev_id) pmu = dmc_info_to_pmu(info); + spin_lock(&pmu->lock); if (info->hw_info->irq_handler(info, &counters) != 0) goto out; @@ -372,6 +377,8 @@ static irqreturn_t dmc_irq_handler(int irq, void *dev_id) * it in ISR to support continue mode. */ info->hw_info->enable(info); +out: + spin_unlock(&pmu->lock); dev_dbg(pmu->dev, "counts: %llu %llu %llu, %llu, %llu, %llu\t\t" "sum: %llu %llu %llu, %llu, %llu, %llu\n", @@ -388,7 +395,7 @@ static irqreturn_t dmc_irq_handler(int irq, void *dev_id) pmu->counters.channel_cnt[1], pmu->counters.channel_cnt[2], pmu->counters.channel_cnt[3]); -out: + return IRQ_HANDLED; } @@ -539,6 +546,7 @@ int meson_ddr_pmu_create(struct platform_device *pdev) pmu->name = name; pmu->dev = &pdev->dev; pmu->pmu_enabled = false; + spin_lock_init(&pmu->lock); platform_set_drvdata(pdev, pmu); -- 2.25.1