From patchwork Thu Jul 28 22:28:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12931782 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 D405AC04A68 for ; Thu, 28 Jul 2022 22:32:36 +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=0XqvJoTIDk5B2j4zP/l3IswlV9VQ+BUZFLSWam0mbAs=; b=vFmYo40Xsy3oAEquuKkevbP2N/ /ADxKoR7Ig/ujfwueuhuuRxTOk0BpJd7aKljtxjI4FQZ3JXZsF9n966mr2VtM1qIAmtoUB/tGYaU0 QBgM4qMEtQCdtuwNercseYx90z6DfMavXOg4wgIvepdD/4XoHNhhWzuE/xpj2wZUPj0rn3pacH5L/ yEmPNgNJe8BWI8DPAVXJyUg6H08tMzMuOxBbR3JmWJr7wF1GH9yqbylzB7LV8b6wDyzZnu2gA5Goy rsoZSiV5AAIfrE158D0suusMug7y869HweW1hLfilnIkEDtDoyx8wpMaNLaEFsP4/QXNVlxU5Bgde /OUvEGpQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oHC2R-00FYwK-Rg; Thu, 28 Jul 2022 22:31:28 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oHC0K-00FXkT-N0 for linux-arm-kernel@lists.infradead.org; Thu, 28 Jul 2022 22:29:18 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id k16-20020a252410000000b006718984ef63so2548111ybk.3 for ; Thu, 28 Jul 2022 15:29:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=Erk4wRaNj75eOg4s5q1IE3PwAROzueG5Y9xXx5fRq5Q=; b=XtmayGwU5OL2zmP9czAT974wt1YfZO1aJOykG49YJceu5n3zZhDvFAyozMUiNKLNQf MulMtliDhYhhNLnYPls1Es93XMRfr4sr1uzbFSLELeTaGfU69wpju202YgSDxJWtQH7P YiHDOtYbN+JSa+gXf9qecNlAiOtHew0amP5Vegx5ofXdd64K1fp+wH83iX1pF/AZWfQC cOxHGLFSwjMji6cDh3SR8mwT1JMYj9YZaHZifYubk7KwnPFkdBQNMM3Ml5IRqPcx+8E3 7qCz0drTXoAejFJn4suXeH4ZBSEr6qKHCOoulyn6a01AddUeC2W/+qcXi9zZNdMqLxEu Nb1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Erk4wRaNj75eOg4s5q1IE3PwAROzueG5Y9xXx5fRq5Q=; b=HdfkZdxYgNS4HnypgEQ3P5D74kK/dwqV85YhaNfG5omiCo1VKke/q5CAlT0Dp2pUsK 1OHDtVFkCcBz6nNa2hHVZW/k+1AqYb0GDbKIFTqt759hD9vCIxqu6x7XzIlBYHVODmUv mW+fxJrjjuXpzHGhMQV0aeQNiZ22T8NQG6zQXKBxAsotX4NWxEFuOd/GjiWWSvm5CSVS Qu+LPiTcvCOtscnHGAYal3393PS18LC+7olFmO7ROMJM+5HNCVlQ5owfB6CVPHOtNc/U o9RUVEzyfkw4OQdxvI9isdV8uUAeMtzqIFW1O0C/1PXD+7lrq6mfacZDg/l24UZ00NH6 7KCw== X-Gm-Message-State: ACgBeo39KY3vanH8YMA0/cgJ/1ZQ/xGGM5R2BAJmoLc0U1+1R8DC4ETT il2HSnOSrbeQ/LDhQIlypT6so5Kc+3fs X-Google-Smtp-Source: AA6agR4CiA+pjpUDn30/aHyPng5ixCYUaDZ0rB8aYOi/UcXPWJnPycLOfEf6jS7Gf1nMbBpE34tJ20qqUKiz X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:fd09:96c3:28af:b08f]) (user=irogers job=sendgmr) by 2002:a25:e090:0:b0:676:abb1:9458 with SMTP id x138-20020a25e090000000b00676abb19458mr297716ybg.319.1659047355640; Thu, 28 Jul 2022 15:29:15 -0700 (PDT) Date: Thu, 28 Jul 2022 15:28:33 -0700 In-Reply-To: <20220728222835.3254224-1-irogers@google.com> Message-Id: <20220728222835.3254224-15-irogers@google.com> Mime-Version: 1.0 References: <20220728222835.3254224-1-irogers@google.com> X-Mailer: git-send-email 2.37.1.455.g008518b4e5-goog Subject: [PATCH v2 14/16] 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-20220728_152916_773280_B4FCE55C X-CRM114-Status: GOOD ( 18.95 ) 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 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,