From patchwork Thu Mar 7 02:47:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xu Yang X-Patchwork-Id: 13584993 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 2337AC5475B for ; Thu, 7 Mar 2024 02:49:00 +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:References:In-Reply-To: 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: List-Owner; bh=VmyX3NOmaiaI9DggeMTagR9ve5cqRlfPjFDPRJT9bwc=; b=HyK38jJ5LpygEV zifx09GputKxzT9cnnBYPAi/jvHvlah1m/K/zjqlw1CNdIxVvEE1rnwvji/hPekqsDCk906Zb2+Ox 0RCCb/XooHZ+nUOjHGbpu0730BLPt6L72tMs0Y+az0dYswJw7kXMJULTyxtppIGEJqKX0pMpFu8Xq ONAgyFFjE4YWOEfSEfoIgOluZzad97TigYykVUEdHqGrdJ585W+ocaDvD0XGpSP/SZI4LPaybD+44 +N0uZpbdiT88aiMHrtJPvYcqe04O27UHUZoNPZB8w3NanaZYXGtej616uqMYZVj7Pvjl7wASntZH+ TMV+5iseN49PaJDdPvXA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1ri3oK-00000002iOB-0pVf; Thu, 07 Mar 2024 02:48:44 +0000 Received: from mail-am6eur05on20601.outbound.protection.outlook.com ([2a01:111:f403:2612::601] helo=EUR05-AM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1ri3oF-00000002iHr-1ARk for linux-arm-kernel@lists.infradead.org; Thu, 07 Mar 2024 02:48:41 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JHxvCLlwcxiun+Wn+FHPWZhaibjpZ2jzXxV3z6HwBfzf8+TQSKCawyF3o5LFUwuvjlHNbBho4zzdyhJrNWiDU5cI2saVWo2zAusERNue3SKSv0/uTB3EunFM1DY+prJ3vutSTsdOC24crrNSVwhDSKoHLvGt2lrF8gztDdIgXjnFZQ2GouPlP2Dmiloi7HwlpZDx598IvlDhI00ILOmSFVfZi+DWKLA5q0F0djrEnwFv6lw0HfeeElWujL/ORIZDVmfRkTl9WYZs3/yvSx2CTTcMNknFMgO+X+6ay6I53PEb07L+Q7bm1Zx3x8Jls37ANawKpgkeW790wg+6fRy4lQ== 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=7M79/0HHkY19uCUfaVW/Jw9qmF8Jv/I+pmHhBdAOois=; b=K7a3tjGTySGeAKvS3ySZzd4CS/JBJD31xZ4VC9CZqdWfXNsDt91M+91KnnrCGWdYTk42HqqeiNnskWS8ut09m9xzf5ipnAFrSlhFV9Mp0Gchn3ze1MGMXKT2yWs/kWpzQ2X284oUgQwOL2wnxnkFQNhkiH6aiXnA/eMwqNqyIoeEbNLEnm3oTUeq0WwNmBgeiVZiVqP2AQLcoBrvNRpOkz5oWHtM0axIXQn3mmXZXMVXPZk3HX1IpxXab+F4PHHjZjKX0tr8gyIREwFLYfICjWGNEZKemRlMzSZ/MQB8AsD5nbs5vNWLnjOe7egNt1SFTf+zSGprDaDVTLhH7e6E0A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=7M79/0HHkY19uCUfaVW/Jw9qmF8Jv/I+pmHhBdAOois=; b=Cyw5IXcnrGYWc+131u/GXswfUQmy9bQUklJ29gOj6T6cxshjC/5+2y3sOsyQo+QQJAIMZtXWv8AeUDnwaATdx2LHI+1d6iVLGKkDoCfYT3Hwn6i0EfBAaCpWXBLUasrLnq39jKJESeMg/DwWTFcUk3Qq8VDaq7ruKA7TC2T+qvE= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from DU2PR04MB8822.eurprd04.prod.outlook.com (2603:10a6:10:2e1::11) by AS8PR04MB8022.eurprd04.prod.outlook.com (2603:10a6:20b:28a::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7339.39; Thu, 7 Mar 2024 02:48:35 +0000 Received: from DU2PR04MB8822.eurprd04.prod.outlook.com ([fe80::d45f:4483:c11:68b0]) by DU2PR04MB8822.eurprd04.prod.outlook.com ([fe80::d45f:4483:c11:68b0%7]) with mapi id 15.20.7339.035; Thu, 7 Mar 2024 02:48:35 +0000 From: Xu Yang To: Frank.li@nxp.com, will@kernel.org, mark.rutland@arm.com, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, conor+dt@kernel.org, shawnguo@kernel.org, s.hauer@pengutronix.de, kernel@pengutronix.de, festevam@gmail.com, john.g.garry@oracle.com, jolsa@kernel.org, namhyung@kernel.org, irogers@google.com Cc: mike.leach@linaro.org, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, alexander.shishkin@linux.intel.com, adrian.hunter@intel.com, linux-arm-kernel@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, imx@lists.linux.dev Subject: [PATCH v5 6/7] perf: imx_perf: limit counter ID from user space and optimize counter usage Date: Thu, 7 Mar 2024 10:47:53 +0800 Message-Id: <20240307024754.3469810-6-xu.yang_2@nxp.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240307024754.3469810-1-xu.yang_2@nxp.com> References: <20240307024754.3469810-1-xu.yang_2@nxp.com> X-ClientProxiedBy: SI2PR02CA0037.apcprd02.prod.outlook.com (2603:1096:4:196::8) To DU2PR04MB8822.eurprd04.prod.outlook.com (2603:10a6:10:2e1::11) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU2PR04MB8822:EE_|AS8PR04MB8022:EE_ X-MS-Office365-Filtering-Correlation-Id: 853ae16e-5bf8-405b-a1be-08dc3e511583 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: wuUfUVGe/orPlHN5D132NA4VBZu/CtIGtbG6U2gl7VOQ+ZmVpUJh841INLm/GCui/Ypb5Cf5tK4oOKVP0/7V9TPoMfodcFh/xrB4devtt3OUUXROrpG8NvHLzwx8YWbrKn68woCjl1r4dlRnpnnmFJmb18U0qgv3n99FHYQaa1gGY2SIIKcSM03fI4LFw+rVtVWVcXeCkEnTdjdeKKqzg1MM2oYGipDm/d7RO9wq1wasGOUHVVH9nLYiVTkMdc1aLIhcx1gRDqdRC6NXj6DvQnu0cx35wItcHKi7yK4+5/O4NomzKxA5PgrKefHWLdlutT3c7rkVKxoO2W3XVSe3wj7LErFBL2z2rSY5v9Ot9h4v4i1Mp68oUHrq6Bu4loMJPkJ2S1J6rh6KTIeebMsoHfqbqhHEQQy3GYAN/ddJaBKgYd/gupRNdvI/McPn+JUtJaOZSI7jf/iHEnAK6A8E30KiwYQUAP2CUwi2PNpPNDAwU8tV4DuRz3yJW16XBhIg+QSiFl8VAjIEsUa1+c3eU8knNNjph1pk1RC+tgVk93KXtrcVkM9jVa3dcQyxL2Z0PxAp3whWJTYkFLWkfSUyRDnEmtzEvWSgfgn4UgvazAolyHEzsN633LXUg0Sop8WxSLhSd3C/pJ9WqQrwvSCbHnZflCO/XcYfcy95y0vwF2hrnSyLvgo6I/LlRK5IIVA2IyvFulIwEFVQreOa2xw4Xtk2u78lreU9n+T4LKKe9CY= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DU2PR04MB8822.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(376005)(38350700005)(921011);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: SR1aj2xQk6XOJg/YWKpGnBIJpTO4qiBRi6Ubg7S9G+uff2yOvrdvoigoRKFfnEqEEpB9B9QrE6I811G+QmNUqOrWXI3lWqI/Hi0Fkxj7LvY6Ft5lYS/u8Ovy1zD5ipQSRf3JgDKue3UdJv3CUIbJPWCuYSYzaJh+ZbEBrBz7I8rvr/7hLXDdvV/3kUyB4+ZXsAslZHmjZUjT1tY/et9jzuwOhmv8KEk7Onh9XGXfgnj+a5Q+rIXAIjEDrzLVu1B+bUAOAMCsCGWYCfL/q5+ANSaWjKpWjRIrYb9wEmvqx/0sFAAgb+fDR8p/m9JI5/8cf+oLuoTdJsswMgrLwfAFsfR2SdqqD4klzFVkcqHLYQRUnn+y41vpdb+FDPjX6vuzCRYp34wxD4VHKUXuENdhu02wFNKu6wzRo3ndhO/k12Rl1bFIEDphRlLXj+RXx87Bu3L+WoQ1Z1OjDMvMms6TyUnlCrD2gmdL3TPFsUnBmYwn0uYdmhU/8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OoWPPQAZzqInVe X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 853ae16e-5bf8-405b-a1be-08dc3e511583 X-MS-Exchange-CrossTenant-AuthSource: DU2PR04MB8822.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Mar 2024 02:48:35.5993 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: ino6kFf8ig9qMaa7yYumW/VNaMFDGZrbSXXZ21jPX8brMGxFbQ4K8sEMerjU/irzayxIMOZhoZc/itmjTJWE+g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR04MB8022 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240306_184839_426963_A7F13620 X-CRM114-Status: GOOD ( 24.96 ) 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 The user can pass any counter ID to perf app. However, current pmu driver doesn't judge the validity of the counter ID. This will add necessary check for counter ID from user space. Besides, this pmu has 10 counters except cycle counter which can be used to count reference events and counter specific evnets. This will also add supports to auto allocate counter if the user doesn't pass it the perf. Then, the usage of counter will be optimized. Signed-off-by: Xu Yang --- Changes in v2: - limit counter ID from user to 0-10 - combine dynamic and static allocation of counter Changes in v3: - no changes Changes in v4: - rename ddr_perf_is_specific_event() - use macro definitions to parse config attr Changes in v5: - improve ddr_perf_is_counter_specific_event() --- drivers/perf/fsl_imx9_ddr_perf.c | 69 +++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/drivers/perf/fsl_imx9_ddr_perf.c b/drivers/perf/fsl_imx9_ddr_perf.c index f25f55126004..0ac680ee2505 100644 --- a/drivers/perf/fsl_imx9_ddr_perf.c +++ b/drivers/perf/fsl_imx9_ddr_perf.c @@ -51,6 +51,7 @@ #define NUM_COUNTERS 11 #define CYCLES_COUNTER 0 +#define CYCLES_EVENT_ID 0 #define CONFIG_EVENT_MASK 0x00FF #define CONFIG_EVENT_OFFSET 0 @@ -271,6 +272,16 @@ static struct attribute *ddr_perf_events_attrs[] = { NULL, }; +/* + * An event is either reference evnet or counter specific event. + * For counter specific event, the event count will only be incremented + * on the corresponding counter. + */ +static bool ddr_perf_is_counter_specific_event(int event) +{ + return event >= 64 && event <= 73; +} + static umode_t ddr_perf_events_attrs_is_visible(struct kobject *kobj, struct attribute *attr, int unused) @@ -529,6 +540,7 @@ static int ddr_perf_event_init(struct perf_event *event) struct ddr_pmu *pmu = to_ddr_pmu(event->pmu); struct hw_perf_event *hwc = &event->hw; struct perf_event *sibling; + int event_id, counter; if (event->attr.type != event->pmu->type) return -ENOENT; @@ -541,6 +553,18 @@ static int ddr_perf_event_init(struct perf_event *event) return -EOPNOTSUPP; } + counter = (event->attr.config & CONFIG_COUNTER_MASK) >> CONFIG_COUNTER_OFFSET; + if (counter > NUM_COUNTERS) { + dev_warn(pmu->dev, "Only counter 0-10 is supported!\n"); + return -EINVAL; + } + + event_id = (event->attr.config & CONFIG_EVENT_MASK) >> CONFIG_EVENT_OFFSET; + if (ddr_perf_is_counter_specific_event(event_id) && counter == 0) { + dev_err(pmu->dev, "Need specify counter for counter specific events!\n"); + return -EINVAL; + } + /* * We must NOT create groups containing mixed PMUs, although software * events are acceptable (for example to create a CCN group @@ -574,6 +598,39 @@ static void ddr_perf_event_start(struct perf_event *event, int flags) hwc->state = 0; } +static int ddr_perf_alloc_counter(struct ddr_pmu *pmu, int event, int counter) +{ + int i; + + if (event == CYCLES_EVENT_ID) { + /* + * Always map cycle event to counter 0. + * Cycles counter is dedicated for cycle event + * can't used for the other counters. + */ + if (pmu->events[CYCLES_COUNTER] == NULL) + return CYCLES_COUNTER; + } else if (counter != 0) { + /* + * 1. ddr_perf_event_init() will make sure counter + * is not 0 for counter specific events. + * 2. Allow specify counter for referene event too. + */ + if (pmu->events[counter] == NULL) + return counter; + } else { + /* + * Counter may be 0 if user doesn't specify it. + * Auto allocate counter for referene event. + */ + for (i = 1; i < NUM_COUNTERS; i++) + if (pmu->events[i] == NULL) + return i; + } + + return -ENOENT; +} + static int ddr_perf_event_add(struct perf_event *event, int flags) { struct ddr_pmu *pmu = to_ddr_pmu(event->pmu); @@ -581,10 +638,18 @@ static int ddr_perf_event_add(struct perf_event *event, int flags) int cfg = event->attr.config; int cfg1 = event->attr.config1; int cfg2 = event->attr.config2; - int counter; + int event_id, counter; + event_id = (cfg & CONFIG_EVENT_MASK) >> CONFIG_EVENT_OFFSET; counter = (cfg & CONFIG_COUNTER_MASK) >> CONFIG_COUNTER_OFFSET; + /* check if counter is available or needs to allocate one */ + counter = ddr_perf_alloc_counter(pmu, event_id, counter); + if (counter < 0) { + dev_dbg(pmu->dev, "There are not enough counters\n"); + return -EOPNOTSUPP; + } + pmu->events[counter] = event; pmu->active_events++; hwc->idx = counter; @@ -620,9 +685,11 @@ static void ddr_perf_event_del(struct perf_event *event, int flags) { struct ddr_pmu *pmu = to_ddr_pmu(event->pmu); struct hw_perf_event *hwc = &event->hw; + int counter = hwc->idx; ddr_perf_event_stop(event, PERF_EF_UPDATE); + pmu->events[counter] = NULL; pmu->active_events--; hwc->idx = -1; }