From patchwork Sat Jan 21 02:10:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilkka Koskinen X-Patchwork-Id: 13110808 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B2D30C05027 for ; Sat, 21 Jan 2023 02:12:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=kqHjqkvqXDllwnhG1lCJvno5/UPsOMRIdC/zH3Kh/WU=; b=Axi2Q5yxjIqIbY w4Ts1L4YvM05lgEmn6pSb+PAW9Ek7KaH7+51jzHdjD3c8S9kJqdBkywiV3iaPlJlLvaN4cU4YeScW Qtv6Xk02S/J//zLoh561X5S0wsOWtyUtb371Bs48jlT/f/EiVtbQHyaD49gRXu4WNJuedDUq7sHDV 6Cs7hdRtPCuG9yxc7o6g7ZJqL8W2R3vx0lABFSNhyq3mySlBCXUMxED7QasfCeOiMPkguVWkgXpHc UEV8/VkwmmbI4TS9qRdjqP2iuNE66WNBvV4tHVvh+9PfAl6oV/rldixwpZxneWT2nJ+I9gmv1MD0y 63fDXfT+4o0U6BAMk9MQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pJ3Lk-00D0Kx-6r; Sat, 21 Jan 2023 02:11:20 +0000 Received: from mail-mw2nam10on2132.outbound.protection.outlook.com ([40.107.94.132] helo=NAM10-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pJ3Lg-00D0JC-Ho for linux-arm-kernel@lists.infradead.org; Sat, 21 Jan 2023 02:11:18 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Hgx/ffxk8a/ASJynFJiDdp0vswjgw3HTWIBu/nhcoG9SyIXXzjm+a2bGaNhH4liQeUT4nEhlI++EK0zidHVU2qBznxoB7YjuDArX6IS/paXbf7m3vaoO8YlkzKAvYIz0LFkTivq8DMO54hHzgRuj8j8Sy53q61A4D4pc+7AfrhZxoC2rnPI1+R2vz4ssliecSw2hzirhfxBQHDUwoU8gLt/8jPZZTssXEiNjlB3pVOsomy2eabc3VWl53H2cUhUkyx9bnyLCJtAGnduqey725M0Q4uP/+PnqMzZ5tofNMSGIuMTFWN2O1uAhz+eda1kgTa9L4BN2PbAiMplaRJ2mfg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=0hsuTKHjsRniShAlfhx1Rzupn36hIMVLkma3EcUDWwo=; b=A/axhvSACVBOcS8BoZ6ljxejksuwdS0RdpapaCkMR5OpQhb2Io0IcLF66q+Jhe5Rks4oIMQYluTSXYkNUF7SwH8wQWV4c97ICWQ6/3qgCUnUkpaxNzUGAi3gKhCw9UcX5LnZ6EzGprNTxrlizIwivp9fCy2Ut2y+0db8EAfJ92Ig7oHanlleW2gO7r+FzruWK3TtdwVfv3275DwNI5bUjBTEVsM3bIzFMUatoROQvQsFQb3ssnN1wHnjt1o2kbKG59nRJTHh386Kp51WB822DLi9DGXPhJNs5LKfvOHH2AmTz1xuPedyJ0AoGpqwXJOgLxRKkH+xXKgArufs9iSr/w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=os.amperecomputing.com; dkim=pass header.d=os.amperecomputing.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=os.amperecomputing.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=0hsuTKHjsRniShAlfhx1Rzupn36hIMVLkma3EcUDWwo=; b=mYu4256iLxwwmJscje49LHidoEyqipwsUCjjCOCsCGssCxfjtETTULoU7NBMFqOhmAwFJ7IeUiSu5H283x9o/a1yQDwA0qgWTaIssLIrdBLWLhJHl2cZQzx1c55NxzJRB93Aa1DpP4hp8yLOPV6dt0DLo2k9QQ+Pi7BmQHo1qoI= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=os.amperecomputing.com; Received: from DM5PR0102MB3590.prod.exchangelabs.com (2603:10b6:4:a4::25) by LV2PR01MB7837.prod.exchangelabs.com (2603:10b6:408:171::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.24; Sat, 21 Jan 2023 02:11:06 +0000 Received: from DM5PR0102MB3590.prod.exchangelabs.com ([fe80::a307:bac5:2136:d4e5]) by DM5PR0102MB3590.prod.exchangelabs.com ([fe80::a307:bac5:2136:d4e5%7]) with mapi id 15.20.6002.013; Sat, 21 Jan 2023 02:11:06 +0000 From: Ilkka Koskinen To: ilkka@os.amperecomputing.com, will@kernel.org, mark.rutland@arm.com, robin.murphy@arm.com Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, patches@amperecomputing.com Subject: [PATCH] perf/arm-cmn: Check all the DTCs when reading global counters Date: Fri, 20 Jan 2023 18:10:45 -0800 Message-Id: <20230121021045.47595-1-ilkka@os.amperecomputing.com> X-Mailer: git-send-email 2.17.1 X-ClientProxiedBy: CH0PR03CA0245.namprd03.prod.outlook.com (2603:10b6:610:e5::10) To DM5PR0102MB3590.prod.exchangelabs.com (2603:10b6:4:a4::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM5PR0102MB3590:EE_|LV2PR01MB7837:EE_ X-MS-Office365-Filtering-Correlation-Id: 89c8e893-af99-4423-3ecc-08dafb54c054 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hc4Q/w5CdQrZFF8D6lgsZWk1xf4WCMPNK48MOZXRqufKVxHDJsg+nYNWaugOxJKE+8LwgFeKnENirnecwqBaErejcAbFDuiIiXEVBuaVNdSNA+WXZWDhYmdFty6buvr/KiQyWQxIh5SKPsSCgWnsODcK4zuwR/5bq2BlzoNF+PSFoNcgDFDXH9FZ/0Msfg+GaEu+GiCKRLVhvHQlNI+PHC6SIXdIGn2qT4ATck1Gek5K/iDx0xeTcXut78prBV69HrtrbTSAPQVDfzTZYQitUS9n5QJS/Fz/ZmD/K5piMUv+GnEca2BSzym+MeKSlNvoBu4ue027NyROqQWNzKneE+Wkg/H4iugsOUwI9gjLgepiDuRc1kBFVOAVISQbgtDfDSXy94THX4EPyFfth6htL03ATNZahnUDVLfBOgdMk00O9Gvp0MaA4ez/J0Jj7fyZ4LoppYAAOhGwau/ixlAGn0Dg7WYdG2DGnwQytg2LXs6SZcIVQoWM+j+6xWYCiIs6/37FHH+gxsJXasi+Y1rjEPwKx7+bO27LuzlJa8tWOiCA7OlDJqrdpe2+jREJaJ8WMu19KEwQxqiyUVrJnu15x6i8cpfZJZi6a0Z3cq+luepL3pYqb6ungKgcsd2X1OfaO+MRdnluCDeJIlXzIgUTWcOoncxD8e2fnpyWUvKEWTCCTIS6oseWjzf4XWYRyeIiqfPzgoUjJ2hbcdGL2aMynw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR0102MB3590.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(39850400004)(366004)(346002)(376002)(136003)(396003)(451199015)(6506007)(86362001)(6666004)(2906002)(66946007)(66476007)(8936002)(5660300002)(38100700002)(66556008)(38350700002)(316002)(6486002)(4326008)(107886003)(478600001)(83380400001)(41300700001)(26005)(52116002)(6512007)(8676002)(186003)(1076003)(2616005);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: y3zhV8mqB6M88/xrgq24FEfwdl2ZCKDTAYj5nSIEpWR/xRJxp9UcWhHzTmr2+d4ZfbuZFL38dVwGpaKgDij1wlVOtK1x/hLsdzE8QLpniCJEb5TRgzJg2ev7EalEJr/yb7B/NEXpiH7/u19V7yIdKeMJBWj7XtpnjE67Sn7eGfEPN5t4HMTq/a9e5SQQupni411JrUJoRTs1CKD9ScRPWLr568CTZBpemnkNJfOSjHPIpIJDgHsk4svOY2YyclbiN1mv2D9lxOLwQiIlRerVzf3PBOurHpHBv8zUg5cs83ICthfOYbLVIMBZcgpKxNHalx2MZazZV3ME3f36Q5HXZOyrrmeow4UJedMlGb0IH7MaBadaXOcaObs8uODk5Zdpa3kLKC1U1MbjXzGQ6r3nW4q6WXQJ5LHkneIf8Z1Z3uMIA9S8KVfUkwHEoPzsQL+Jh1x6OzDiG7IS+pyycrErNzWT6RrqNS95tQUIzldQ8a01oZ0HhmOfo4hGebYYSKAqAQvKcUI7yCiCQyU+be2SVYgY0BMfYAVWC7ErpF9ak03tD8wOaaEG5Fw9T4grjWmU/trD7OPPW5rz4xAVmWnzXgTpjX+qkeBWRlfOIvyHhpxFmImB9S/8ylHtXniSJLisu2xD+KsnKTgw6DdBAeRhOCOH4pIKfqAj/W0Kod86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DM5PR0102MB3590.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Jan 2023 02:11:06.0635 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: t2yyshhThKMG21Bb+TPxWmRc2l4FCoJ2ZtjBLz4hW1EpDMsTKMrMLNzxVbAeiCrn8UBUoC0BoAyo9cQQazfM8GqspogLi1Hd/sNJNGEOZVA1V91bxHooiWewRIvvXPic X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV2PR01MB7837 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230120_181116_855873_C5C26734 X-CRM114-Status: GOOD ( 19.12 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Some events may be available on nodes, none of which belongs to DTC0. When the driver reads the global counters, it stops as soon as it finds a DTC that's not being used and, thus, ignores the rest. As the driver doesn't read the paired global counters, overflowing local counters are regarded as overflowing global counters (assuming the new local counter value is smaller than the previous one) and therefore we can see values around 2^64. Fix the issue by checking all the used DTCs. The driver is still trying to find a counter that's available on all the DTCs rather than doing per-DTC allocation of global counters. We may need to change it in the future, if needed. Signed-off-by: Ilkka Koskinen --- drivers/perf/arm-cmn.c | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/drivers/perf/arm-cmn.c b/drivers/perf/arm-cmn.c index b80a9b74662b..c516f091a002 100644 --- a/drivers/perf/arm-cmn.c +++ b/drivers/perf/arm-cmn.c @@ -540,7 +540,7 @@ struct arm_cmn_hw_event { struct arm_cmn_node *dn; u64 dtm_idx[4]; unsigned int dtc_idx; - u8 dtcs_used; + unsigned long dtcs_used; u8 num_dns; u8 dtm_offset; bool wide_sel; @@ -550,6 +550,9 @@ struct arm_cmn_hw_event { #define for_each_hw_dn(hw, dn, i) \ for (i = 0, dn = hw->dn; i < hw->num_dns; i++, dn++) +#define for_each_used_dtc(hw, cmn, i) \ + for_each_set_bit(i, &(hw)->dtcs_used, (cmn)->num_dtcs) + static struct arm_cmn_hw_event *to_cmn_hw(struct perf_event *event) { BUILD_BUG_ON(sizeof(struct arm_cmn_hw_event) > offsetof(struct hw_perf_event, target)); @@ -1272,7 +1275,7 @@ static void arm_cmn_init_counter(struct perf_event *event) unsigned int i, pmevcnt = CMN_DT_PMEVCNT(hw->dtc_idx); u64 count; - for (i = 0; hw->dtcs_used & (1U << i); i++) { + for_each_used_dtc(hw, cmn, i) { writel_relaxed(CMN_COUNTER_INIT, cmn->dtc[i].base + pmevcnt); cmn->dtc[i].counters[hw->dtc_idx] = event; } @@ -1301,7 +1304,7 @@ static void arm_cmn_event_read(struct perf_event *event) delta = new - prev; local_irq_save(flags); - for (i = 0; hw->dtcs_used & (1U << i); i++) { + for_each_used_dtc(hw, cmn, i) { new = arm_cmn_read_counter(cmn->dtc + i, hw->dtc_idx); delta += new << 16; } @@ -1614,10 +1617,32 @@ static void arm_cmn_event_clear(struct arm_cmn *cmn, struct perf_event *event, } memset(hw->dtm_idx, 0, sizeof(hw->dtm_idx)); - for (i = 0; hw->dtcs_used & (1U << i); i++) + for_each_used_dtc(hw, cmn, i) cmn->dtc[i].counters[hw->dtc_idx] = NULL; } +static int arm_cmn_get_global_counter(struct arm_cmn *cmn, + struct arm_cmn_hw_event *hw) +{ + int dtc_idx, i; + bool available; + + for (dtc_idx = 0; dtc_idx < CMN_DT_NUM_COUNTERS; dtc_idx++) { + available = true; + for_each_used_dtc(hw, cmn, i) { + if (cmn->dtc[i].counters[dtc_idx]) { + available = false; + break; + } + } + + if (available) + return dtc_idx; + } + + return -ENOSPC; +} + static int arm_cmn_event_add(struct perf_event *event, int flags) { struct arm_cmn *cmn = to_cmn(event->pmu); @@ -1642,11 +1667,9 @@ static int arm_cmn_event_add(struct perf_event *event, int flags) return 0; } - /* Grab a free global counter first... */ - dtc_idx = 0; - while (dtc->counters[dtc_idx]) - if (++dtc_idx == CMN_DT_NUM_COUNTERS) - return -ENOSPC; + dtc_idx = arm_cmn_get_global_counter(cmn, hw); + if (dtc_idx < 0) + return dtc_idx; hw->dtc_idx = dtc_idx;