From mboxrd@z Thu Jan 1 00:00:00 1970 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 CA05AC25B06 for ; Thu, 4 Aug 2022 22:20:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240224AbiHDWU3 (ORCPT ); Thu, 4 Aug 2022 18:20:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240066AbiHDWT3 (ORCPT ); Thu, 4 Aug 2022 18:19:29 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4D5172EDF for ; Thu, 4 Aug 2022 15:19:03 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-328c683e73aso6850817b3.23 for ; Thu, 04 Aug 2022 15:19:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=hKdXC3Dql13mKwUfJGBQkfcpnljE9PToPeTdbYeGres=; b=NDRI2Uda5JWymD6je14uVr3Oj4wbWv42Eh9kGopbOo78wrOR9Oky1i9ioN+9H1zn00 dOC4UJ7VRmSvi3YjHZ5kzjze2CeL6EbBuDwQpkR0+tv/agAoM1F0f9HZuZZ+5RxMKPau HifNxlWUlMcadc5JZszNLd97aYlUMEt4VfXYen+3xCeahJiQgSICQNiLedF0cVTz0ZgQ S1KfQvP2C0JXUzZPzgZlQ2uBGilDfyUAU6pUIIjh+3wgRhRs7QnW+mSdoLZ4csitiVur h/8UsGxCykGIbAyGE8zvqPeOIAIvXTyoQsv4cmktpWORHT1XxnjaaQ9FyWKrovNNWCQv yv/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=hKdXC3Dql13mKwUfJGBQkfcpnljE9PToPeTdbYeGres=; b=TeHmtyiKmgTohQyiyUf7AU7GrIC0peiFwVgy2QRIkfeengz5k/7jAjgVJuddd3xeLx u4okAAZiZ9GgM8HaqHk1TuCncSzJGuKa27NkdFHfpmpI9RcmtfFLMtXHm6W+CHnPPktx G+P3+GM92GxifET2Pc43qzTb+5ZGKs2Z2tNahO52LjIsenE4SBeIw46B0iR8agfReVMC +2VnkGrz/YYWnMoniPzATtBgePu3wghcwGdJo1z4Nbx8bBqxc1ChqEGGj+arCmjkhRgf IoYVMl1os+C/hdNClg/KoRkkZw5HiSTkhl5JGBcaizEVtozo1E7bAgiBYUPm17pLB9IL mq3w== X-Gm-Message-State: ACgBeo2UPxG7RPANu13BIeI+aDfKVDKj9eGjc8otIX+jyVbmFuealKyA xcHEFHdSMBgTGjtrT3MMHDzERIimxGe7 X-Google-Smtp-Source: AA6agR6YILN9N5BqtIbXvPAjFAUAHzL2OQeguWlk9vwwJWB+OjVa+SHBovChrFea7YNP+B1FzQKcXEFzMDp4 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:f5e1:5bc5:7dab:2b7c]) (user=irogers job=sendgmr) by 2002:a0d:ebc8:0:b0:31f:4f08:63aa with SMTP id u191-20020a0debc8000000b0031f4f0863aamr3495227ywe.145.1659651543035; Thu, 04 Aug 2022 15:19:03 -0700 (PDT) Date: Thu, 4 Aug 2022 15:18:14 -0700 In-Reply-To: <20220804221816.1802790-1-irogers@google.com> Message-Id: <20220804221816.1802790-16-irogers@google.com> Mime-Version: 1.0 References: <20220804221816.1802790-1-irogers@google.com> X-Mailer: git-send-email 2.37.1.559.g78731f0fdb-goog Subject: [PATCH v4 15/17] perf metrics: Copy entire pmu_event in find metric From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Andi Kleen , Zhengjun Xing , Ravi Bangoria , Kan Liang , Adrian Hunter , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org Cc: Stephane Eranian , Ian Rogers Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The pmu_event passed to the pmu_events_table_for_each_event is invalid after the loop. Copy the entire struct in metricgroup__find_metric. Reduce the scope of this function to static. Signed-off-by: Ian Rogers --- tools/perf/util/metricgroup.c | 33 ++++++++++++++++++--------------- tools/perf/util/metricgroup.h | 2 -- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index 8ef44f4b5e9a..95f4b46dba09 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -879,6 +879,10 @@ struct metricgroup_add_iter_data { const struct pmu_events_table *table; }; +static bool metricgroup__find_metric(const char *metric, + const struct pmu_events_table *table, + struct pmu_event *pe); + static int add_metric(struct list_head *metric_list, const struct pmu_event *pe, const char *modifier, @@ -914,7 +918,7 @@ static int resolve_metric(struct list_head *metric_list, size_t bkt; struct to_resolve { /* The metric to resolve. */ - const struct pmu_event *pe; + struct pmu_event pe; /* * The key in the IDs map, this may differ from in case, * etc. from pe->metric_name. @@ -928,16 +932,15 @@ static int resolve_metric(struct list_head *metric_list, * the pending array. */ hashmap__for_each_entry(root_metric->pctx->ids, cur, bkt) { - const struct pmu_event *pe; + struct pmu_event pe; - pe = metricgroup__find_metric(cur->key, table); - if (pe) { + if (metricgroup__find_metric(cur->key, table, &pe)) { pending = realloc(pending, (pending_cnt + 1) * sizeof(struct to_resolve)); if (!pending) return -ENOMEM; - pending[pending_cnt].pe = pe; + memcpy(&pending[pending_cnt].pe, &pe, sizeof(pe)); pending[pending_cnt].key = cur->key; pending_cnt++; } @@ -952,7 +955,7 @@ static int resolve_metric(struct list_head *metric_list, * context. */ for (i = 0; i < pending_cnt; i++) { - ret = add_metric(metric_list, pending[i].pe, modifier, metric_no_group, + ret = add_metric(metric_list, &pending[i].pe, modifier, metric_no_group, root_metric, visited, table); if (ret) break; @@ -1073,7 +1076,7 @@ static int __add_metric(struct list_head *metric_list, struct metricgroup__find_metric_data { const char *metric; - const struct pmu_event *pe; + struct pmu_event *pe; }; static int metricgroup__find_metric_callback(const struct pmu_event *pe, @@ -1085,21 +1088,21 @@ static int metricgroup__find_metric_callback(const struct pmu_event *pe, if (!match_metric(pe->metric_name, data->metric)) return 0; - data->pe = pe; - return -1; + memcpy(data->pe, pe, sizeof(*pe)); + return 1; } -const struct pmu_event *metricgroup__find_metric(const char *metric, - const struct pmu_events_table *table) +static bool metricgroup__find_metric(const char *metric, + const struct pmu_events_table *table, + struct pmu_event *pe) { struct metricgroup__find_metric_data data = { .metric = metric, - .pe = NULL, + .pe = pe, }; - pmu_events_table_for_each_event(table, metricgroup__find_metric_callback, &data); - - return data.pe; + return pmu_events_table_for_each_event(table, metricgroup__find_metric_callback, &data) + ? true : false; } static int add_metric(struct list_head *metric_list, diff --git a/tools/perf/util/metricgroup.h b/tools/perf/util/metricgroup.h index f54d170043e9..016b3b1a289a 100644 --- a/tools/perf/util/metricgroup.h +++ b/tools/perf/util/metricgroup.h @@ -69,8 +69,6 @@ int metricgroup__parse_groups(const struct option *opt, bool metric_no_group, bool metric_no_merge, struct rblist *metric_events); -const struct pmu_event *metricgroup__find_metric(const char *metric, - const struct pmu_events_table *table); int metricgroup__parse_groups_test(struct evlist *evlist, const struct pmu_events_table *table, const char *str, -- 2.37.1.559.g78731f0fdb-goog From mboxrd@z Thu Jan 1 00:00:00 1970 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 010A9C25B06 for ; Thu, 4 Aug 2022 22:39:47 +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:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=fG1+aTVi72DVsgU3IMg0HoQk9kIBGzubik6wR+j28TY=; b=mlCDkC/2scEapVEXaZb87KUk5c j8LGhQh4BSwqAkDKFD7KMzlCTBvyfFL//1ZQ/vcZnu00TagwTQvDlV9Y5LN4hOsrlqB4H40m0RuEF ol7MJQsiTdvBAnYvIyiHpSwcwcfd76YK/pkcGHTbVXUcVVhXsL2TOm53Duwc5cLoM1MI5v0qvhCmo LC0S6Hmx4ArSeXez3O782tajmn+hukOw5xhXMQo/3/BRLIOx3tpnq6yxg0zYMPsTQHxzbaiTAlG5c siNBb54bZfod1s12roDv7o05fga0pidA9jrbH/XaJuKwZlO9M5MhE0DJEb86MCaR7KNenrf8lT3rz 1wWe7jRA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oJjTv-009dup-7G; Thu, 04 Aug 2022 22:38:19 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oJjCD-009WgM-BV for linux-arm-kernel@bombadil.infradead.org; Thu, 04 Aug 2022 22:20:01 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Cc:To:From:Subject: References:Mime-Version:Message-Id:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=hKdXC3Dql13mKwUfJGBQkfcpnljE9PToPeTdbYeGres=; b=on3+YFeaO6T1DAwlNA3sivtLCK XRR1CFcTEacaTqK/Hh6e8CXMlnr0DUJ6QGDc/+H+7A8RyD0oEiOO3rYWkRk+kKG1Ztgrq66WX6OBt TA9mjDhBhvNvCDYILGZADRa7TXcq0+wqSblJgA+AdH7DQNp1eUJ2B2qKbXGDjJUbH0usaQtTMXOcR 0hkHJTKaNMkvaHsQxpLVofni2PMMgf/V389/UrhcqZ0eUlLk892HBBNqd7Mf7oY9AW4v2GsMRvsdG 2b4u9cYqgha1jBXKTlym9cYiJOgPbFeXAuXci2ddd2GzMasx1z6jOtJOv9TEbRrQUZxWm8kVpeNti A7/VCZUg==; Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by desiato.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oJjBs-0034IW-77 for linux-arm-kernel@lists.infradead.org; Thu, 04 Aug 2022 22:19:53 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-31dfe25bd47so6959337b3.18 for ; Thu, 04 Aug 2022 15:19:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=hKdXC3Dql13mKwUfJGBQkfcpnljE9PToPeTdbYeGres=; b=NDRI2Uda5JWymD6je14uVr3Oj4wbWv42Eh9kGopbOo78wrOR9Oky1i9ioN+9H1zn00 dOC4UJ7VRmSvi3YjHZ5kzjze2CeL6EbBuDwQpkR0+tv/agAoM1F0f9HZuZZ+5RxMKPau HifNxlWUlMcadc5JZszNLd97aYlUMEt4VfXYen+3xCeahJiQgSICQNiLedF0cVTz0ZgQ S1KfQvP2C0JXUzZPzgZlQ2uBGilDfyUAU6pUIIjh+3wgRhRs7QnW+mSdoLZ4csitiVur h/8UsGxCykGIbAyGE8zvqPeOIAIvXTyoQsv4cmktpWORHT1XxnjaaQ9FyWKrovNNWCQv yv/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=hKdXC3Dql13mKwUfJGBQkfcpnljE9PToPeTdbYeGres=; b=44gmi0B8Lgbxt+9qwn0RdOkLWO2cAyR+3KDZBx7z3AItLHrcxWE3JsF5w4g8OB8i+T zscaNu/a/zCYBOOJBgXNPqp0je+1UUyik/e6jkNiUE8l5IVeP3LEJvSWq2pLbnuRftkw +rks2lbzRo2D5HMvNBbYGAVyGCIFb/B+sVuR0C7fW7VeTWM8VEddhAftKVSPmP2RXYDv 4lUjdmmPupLgtKKwuKPobutfIjwLvMPepA/973apT4+9A9DihKIegqXRdNzSDDKqro16 CH1G3Q+F97sC2x7IX9NcHEV/KJJ7yqPQFK+kZyyFRzcKTtTJJ75lDIwrj4Rz6dRUYgyD CQ9g== X-Gm-Message-State: ACgBeo0SJv0pStK0iGWVCD66xjLKm6YSnc5v6xCCxuaSf7IfqE9Us5Al iyWPEUurPSpyqM1YiZPt6hk+NWE1CYhS X-Google-Smtp-Source: AA6agR6YILN9N5BqtIbXvPAjFAUAHzL2OQeguWlk9vwwJWB+OjVa+SHBovChrFea7YNP+B1FzQKcXEFzMDp4 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:f5e1:5bc5:7dab:2b7c]) (user=irogers job=sendgmr) by 2002:a0d:ebc8:0:b0:31f:4f08:63aa with SMTP id u191-20020a0debc8000000b0031f4f0863aamr3495227ywe.145.1659651543035; Thu, 04 Aug 2022 15:19:03 -0700 (PDT) Date: Thu, 4 Aug 2022 15:18:14 -0700 In-Reply-To: <20220804221816.1802790-1-irogers@google.com> Message-Id: <20220804221816.1802790-16-irogers@google.com> Mime-Version: 1.0 References: <20220804221816.1802790-1-irogers@google.com> X-Mailer: git-send-email 2.37.1.559.g78731f0fdb-goog Subject: [PATCH v4 15/17] perf metrics: Copy entire pmu_event in find metric From: Ian Rogers To: John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Andi Kleen , Zhengjun Xing , Ravi Bangoria , Kan Liang , Adrian Hunter , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org Cc: Stephane Eranian , Ian Rogers X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220804_231951_439869_B687A4FB X-CRM114-Status: GOOD ( 19.22 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The pmu_event passed to the pmu_events_table_for_each_event is invalid after the loop. Copy the entire struct in metricgroup__find_metric. Reduce the scope of this function to static. Signed-off-by: Ian Rogers --- tools/perf/util/metricgroup.c | 33 ++++++++++++++++++--------------- tools/perf/util/metricgroup.h | 2 -- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index 8ef44f4b5e9a..95f4b46dba09 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -879,6 +879,10 @@ struct metricgroup_add_iter_data { const struct pmu_events_table *table; }; +static bool metricgroup__find_metric(const char *metric, + const struct pmu_events_table *table, + struct pmu_event *pe); + static int add_metric(struct list_head *metric_list, const struct pmu_event *pe, const char *modifier, @@ -914,7 +918,7 @@ static int resolve_metric(struct list_head *metric_list, size_t bkt; struct to_resolve { /* The metric to resolve. */ - const struct pmu_event *pe; + struct pmu_event pe; /* * The key in the IDs map, this may differ from in case, * etc. from pe->metric_name. @@ -928,16 +932,15 @@ static int resolve_metric(struct list_head *metric_list, * the pending array. */ hashmap__for_each_entry(root_metric->pctx->ids, cur, bkt) { - const struct pmu_event *pe; + struct pmu_event pe; - pe = metricgroup__find_metric(cur->key, table); - if (pe) { + if (metricgroup__find_metric(cur->key, table, &pe)) { pending = realloc(pending, (pending_cnt + 1) * sizeof(struct to_resolve)); if (!pending) return -ENOMEM; - pending[pending_cnt].pe = pe; + memcpy(&pending[pending_cnt].pe, &pe, sizeof(pe)); pending[pending_cnt].key = cur->key; pending_cnt++; } @@ -952,7 +955,7 @@ static int resolve_metric(struct list_head *metric_list, * context. */ for (i = 0; i < pending_cnt; i++) { - ret = add_metric(metric_list, pending[i].pe, modifier, metric_no_group, + ret = add_metric(metric_list, &pending[i].pe, modifier, metric_no_group, root_metric, visited, table); if (ret) break; @@ -1073,7 +1076,7 @@ static int __add_metric(struct list_head *metric_list, struct metricgroup__find_metric_data { const char *metric; - const struct pmu_event *pe; + struct pmu_event *pe; }; static int metricgroup__find_metric_callback(const struct pmu_event *pe, @@ -1085,21 +1088,21 @@ static int metricgroup__find_metric_callback(const struct pmu_event *pe, if (!match_metric(pe->metric_name, data->metric)) return 0; - data->pe = pe; - return -1; + memcpy(data->pe, pe, sizeof(*pe)); + return 1; } -const struct pmu_event *metricgroup__find_metric(const char *metric, - const struct pmu_events_table *table) +static bool metricgroup__find_metric(const char *metric, + const struct pmu_events_table *table, + struct pmu_event *pe) { struct metricgroup__find_metric_data data = { .metric = metric, - .pe = NULL, + .pe = pe, }; - pmu_events_table_for_each_event(table, metricgroup__find_metric_callback, &data); - - return data.pe; + return pmu_events_table_for_each_event(table, metricgroup__find_metric_callback, &data) + ? true : false; } static int add_metric(struct list_head *metric_list, diff --git a/tools/perf/util/metricgroup.h b/tools/perf/util/metricgroup.h index f54d170043e9..016b3b1a289a 100644 --- a/tools/perf/util/metricgroup.h +++ b/tools/perf/util/metricgroup.h @@ -69,8 +69,6 @@ int metricgroup__parse_groups(const struct option *opt, bool metric_no_group, bool metric_no_merge, struct rblist *metric_events); -const struct pmu_event *metricgroup__find_metric(const char *metric, - const struct pmu_events_table *table); int metricgroup__parse_groups_test(struct evlist *evlist, const struct pmu_events_table *table, const char *str, -- 2.37.1.559.g78731f0fdb-goog _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel