From patchwork Wed Jan 31 05:58:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xu Yang X-Patchwork-Id: 13538769 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 29E95C4828C for ; Wed, 31 Jan 2024 05:52:55 +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=pocbavTBEj+q1UKUNJnb2HYfcxLmj+ThKu6IqLyl5NY=; b=Kq9c2n/mhqhjzG ZNd9ajexA9YKXRTtKSAUGgtrsDbqql+trrwZptXD8KMK+ZnhJfUV9ItL9Ly6EpJ1KPj7TB2x/Kdd8 I9o23Q4rDlw5x57Z4eKfbxjvEVvYWQK09kCRuhtenn9j3ILbRjsSAL5SCkBEyF5AZgRIDUNjbs/yg ynSr6ADgUP4chRsqt3UuFfyh8qYPBJFPyWgD0pqSe06wdOO4DVXedk0y6YBu4t+uhfRwi+AjmF60O Bc++0e3jtjsLSybrtz+LwrNMAd8NrQzYA5OX1YRRXZ9jLjJmECcVMxQ/LoIoZxzbNiJPgRL4qaIx0 ZIBAV7YSqcF8YtKRDARQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rV3Wa-00000001a8G-4BRy; Wed, 31 Jan 2024 05:52:41 +0000 Received: from mail-db8eur05on20603.outbound.protection.outlook.com ([2a01:111:f400:7e1a::603] helo=EUR05-DB8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rV3WN-00000001ZvH-2YF5 for linux-arm-kernel@lists.infradead.org; Wed, 31 Jan 2024 05:52:30 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OED9MNAHo553+XSYURE0q4Kz+A0hHnePjcb0Gv93LQMQ1PT66/tR5dYh3fUuU2OlcOJ0d+rx1XEzdTnsXox7i0WQjcpiPOb5W0wkOeC6RvYVD5TMZarWKUsQ9bTbQj1K9BYwF/tzXEAqtJzmhGNBqsTbdiM/eYA2cuHkkvbrybVDh89j30EhiHpK7rLCmL7G6lcktwBjHEe51kTdS2ysfRfjEjyGnjbmFQRutUJ2BPI4SPMKhDIJiQxCbtmJxty33Mh+Z03qzRkHBocN9ZTgFIIjovmfynKCrMMfvb4c9NppLu+s3xCRyRjiCpOoY9YFrnwc/2155gju1aiGDCETXQ== 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=JxE+kbGqvwFdqguUylkKC9s3foHLtB9G/N8s8DJ5+Z8=; b=PlucF3x0eDoVhPMCfJk+dv+4XT/ZODRtegc8jOLb7qQ55mf6SJmXWZ/BDZgB7H5onC6UlK7uaQ5dhawba0OiFy6wesHqAVYcaYFCMBAjKeIKZUabXy6SSR7SUOQF4ENzz8QkFFMWGq0BnFuJMs1qRmud5lCAO9HEsdtnduBsy5XHos0o0VnwTOPSc15WpqXqP6ur3hnd69hnYa+Y2EpeFbiASeJ2dYhHLNAc0cnREY7I2h30b4QOh5YTjJa9jXkl22arv8hi/23SosQQJ9E+oUWsmczoadXxkbKNb/5hw5k4vWMMaxvl+9J0t3Ac3jY+b9lvDjRzLjDSxQFArjFCmQ== 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=JxE+kbGqvwFdqguUylkKC9s3foHLtB9G/N8s8DJ5+Z8=; b=g1a8a6ov6/a9up0vgc7CYF7SHGL7PAI7rO6EU0RR7f00Y0P2P84zCDItOTXqyqOvTkui0t5mJAzIxhUYDbO7HzeQW3/NislU/n+toK4JOM7uzC/QJvPua/hoSDy9lxqiEDS7NEb8Lni8SJmWDEUUW5xUiWP1/SxH/zSf5bhWKEE= 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 DU2PR04MB8917.eurprd04.prod.outlook.com (2603:10a6:10:2e0::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.24; Wed, 31 Jan 2024 05:52:15 +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.7228.029; Wed, 31 Jan 2024 05:52:15 +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: linux-imx@nxp.com, mike.leach@linaro.org, leo.yan@linaro.org, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, alexander.shishkin@linux.intel.com, adrian.hunter@intel.com, xu.yang_2@nxp.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 v4 5/6] perf: imx_perf: limit counter ID from user space and optimize counter usage Date: Wed, 31 Jan 2024 13:58:10 +0800 Message-Id: <20240131055811.3035741-5-xu.yang_2@nxp.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240131055811.3035741-1-xu.yang_2@nxp.com> References: <20240131055811.3035741-1-xu.yang_2@nxp.com> X-ClientProxiedBy: SI2PR02CA0041.apcprd02.prod.outlook.com (2603:1096:4:196::20) To DU2PR04MB8822.eurprd04.prod.outlook.com (2603:10a6:10:2e1::11) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU2PR04MB8822:EE_|DU2PR04MB8917:EE_ X-MS-Office365-Filtering-Correlation-Id: b34f514c-3b21-4c2e-7fba-08dc2220c732 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: W2zP3yQPR++QiuuQ6qe6Q/D/0Bx8xFOp6I0eZ1QDImrXdRTnXMtfGiCTc8ROPlbNd1b32uDV5Sir7lIFFGuPgPY2iI2x0XUJfnLWtaSf8Js49yyYOBf8T7CwbgzmHULaeuRjPke600d2HTF5Io2qHlHwSwUcEr9n63xU4tLylCH16litDLYMe89oCQRusYNq+845JpHLcSDzt+RKpTK31WM6qiVQNdDFt/bRHwiaThO/aGtH60V6cGV0Y5KJVANhyn1fK8oMsBWHitxPzKHY1JsT5kk75c4cZhKbSRwkLCr7X2wf4u3hyWZQ1jLBTD67x6uZozLJ7UZJv4npPmOzfA8kdxmMrndmR3PNFUePeYkpGDlp/2x6g04HPyimA7TUN5xisIRZL/M8U8PRi45sjElnm0qWe2xHaus5Oi/OwI/oxA8k84hat31t4gVPMI50Tgxi4KCNLvhANXAqkCezn1vZXcoJphuJTBzbYLpR/CSwYX1XCmof3rmAfsKMm6xsy7SW0EXX9sW+p8LfiHgDJ26mOd8UDy8XF/Hc/E2Bb0sDcNWS784na2P2p6WubbQxYMtFGOLq70W1y2If3EuQWjBNA4IY8GwZyNFs7ilJlhAVZcBDNWpla0QTWXsfsV6Swk5GlKCgfV6Juo3AF+Keew== 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)(136003)(39860400002)(376002)(396003)(366004)(346002)(230922051799003)(64100799003)(186009)(451199024)(1800799012)(38350700005)(2906002)(36756003)(41300700001)(921011)(66946007)(66556008)(66476007)(316002)(86362001)(6666004)(2616005)(1076003)(6512007)(52116002)(6506007)(26005)(478600001)(83380400001)(6486002)(5660300002)(38100700002)(8936002)(8676002)(7416002)(4326008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Ufg9od1pygUSCJAeNbBaQDJmM//ZUKpr06D3FQw52XHVShsbwFArt11ZVNivGV8EIDrMUzcGFvaKJaKlzd57S5anuvJ+XKaFqMYZ/h8ZlhS9gFd4eP48jK6e1TogpCg4oQAO1rFNRK0mwqUaEg86d9dEpVDjUGDwZhw9VW8VMyKJoztwcF59PeIt2Y5QqZrUNL6pO3f70Ti4FglrWzdXZrxKEviAacUNtjkrWruOudGt9XmdQ9M8HllbUk45MwTrfSd4kVHssYZnj6IvqIt9j3fvaAU7BctEhUFuY/MLTmDnZGMjMmpSeayOJ7wSBuTE5B5OC647A7hXCpl11FC6YJasD7muYW32elrqH7wfxLGjqC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CgKdLOyPGpojdDGvE/4HULk+EWnDB6vEvpTz8XixQNDPGjZe5lzM9yliFBhQ8FNxWTomhPRsFv7BPWGaQs1XOt3g8ZSs3TLjAwskNsQg8jZa4lHv0IxDcgI7xd2keBjy5wXsYWy7OHVeN9gvWt4xYt X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: b34f514c-3b21-4c2e-7fba-08dc2220c732 X-MS-Exchange-CrossTenant-AuthSource: DU2PR04MB8822.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Jan 2024 05:52:15.7731 (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: tg2VMKrPwC7vjfs25GieTCub/iNKYUpz8kd8ncHyf6fHG+reCeoJE/J5SXxj2BLDA/HFB/9zh4xxjUvY0Z64eg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU2PR04MB8917 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240130_215227_736797_15A708C0 X-CRM114-Status: GOOD ( 25.56 ) 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 --- drivers/perf/fsl_imx9_ddr_perf.c | 72 +++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/drivers/perf/fsl_imx9_ddr_perf.c b/drivers/perf/fsl_imx9_ddr_perf.c index 94041f06c152..e71496809c52 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 @@ -240,6 +241,19 @@ 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) +{ + if (event >= 64 && event <= 73) + return true; + else + return false; +} + static const struct attribute_group ddr_perf_events_attr_group = { .name = "events", .attrs = ddr_perf_events_attrs, @@ -514,6 +528,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; @@ -526,6 +541,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 @@ -559,6 +586,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); @@ -566,10 +626,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; @@ -604,9 +672,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; }