Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp109433imn; Fri, 29 Jul 2022 01:02:44 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vgmm3x6Zr0bJPBA7tDHAolAfCiVgv5DOwPmTqTUSUD3kpFSI2Rt8miIaSXLtjJgFuWCuv9 X-Received: by 2002:a05:6402:51d4:b0:43c:1742:430b with SMTP id r20-20020a05640251d400b0043c1742430bmr2419299edd.212.1659081764242; Fri, 29 Jul 2022 01:02:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659081764; cv=none; d=google.com; s=arc-20160816; b=Nq+ZMkJpwxJoGzvoJjeYYJucIfD4RjlhSQFHFLx6MJRjjAwzhBMt1N4rvD0hP6ZLH8 RO9b3NiKFbVihe+Q0M/qMDN2lTw3esJs7bfAmWqZsKzGWgfXFfSqB3VjY9c6lcn77lgy 1sMYqxWhTj37/f1MKHdJTrKJX2IS9nAMBz1HtLHKxC7rFY1UwrAynahGuwOjMKlq1obU LQVe1XOtyq+6qq/G6hmdDIyodkgYYl6jtORcyYO/XFvKm7Ie76l1J+8vxdG5pEz8i3aF XQaoFRNqnfTSF7MZ9wwoh9WhOKtEXRXTMrLBA7PYKfInGAfRW4oHXXuKWKVnnX/Rz0qJ dW3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=Erk4wRaNj75eOg4s5q1IE3PwAROzueG5Y9xXx5fRq5Q=; b=dWCfs+t7UfVamCMY8HgwPu/IejwukU8ut0XhrdEc7bleFUxoVGIyaGeUEJRlSAD3vi itMJ62i02dHidOhjRArWVYCTIy6fDNDvHDyIw0Me+OJD7qFh8DBgHbFjUiqxNUqNKBDh sQSQV8/1AUKmp72M63c5AfI8Bnl9SK/xOCGoq7Juex1QDpqCaoyiJyylO715KtlF0mQC k184mb8zscDlnEXYfOARIOA43Qh6JYJ01bH9v9jI6wJoVGGAfEMIHeOPGhQkIWBcaxF7 H1jQOXVsx6vlwov8ECDra//TInqKnhG2LTY6VcSxcQkSFeuX1kFQOtttcU34m7RplkXw FXLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=ceYcCHEt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p4-20020a17090628c400b0072b64ce28b4si2445669ejd.394.2022.07.29.01.02.19; Fri, 29 Jul 2022 01:02:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=ceYcCHEt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235380AbiG2Hpb (ORCPT + 99 others); Fri, 29 Jul 2022 03:45:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234988AbiG2Ho5 (ORCPT ); Fri, 29 Jul 2022 03:44:57 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D306820E3 for ; Fri, 29 Jul 2022 00:44:41 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id a6-20020a25bac6000000b00672f6e71f15so3328001ybk.13 for ; Fri, 29 Jul 2022 00:44:41 -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=ceYcCHEt7cwQaf9fLFr6VwUHtuxA9NHhZDIFt4KvpdG/SeMYeJPImEw0TanZvYTkSO jxaYirq2ZfEhQN4eL6O7B494n5LDZqNi3ksV3esyIHxLlRwdNuHXt0C7VsHp4kogIIFw d/Id+nA+3IB1S9Yua+EMBvoGJ+VYKvhLZ/LNfHKbURQIO2jtj3BLHo98MOs4o42FrlyD cVgmPQ0brptiV3OAElYPAPf0rQzWbLQDDEivGL/N8NiTvYuD+Z3myQ/FBSGQvZB3twvl Xei2Q34PCWYF7BbzHnwodGeINBAiTH6WeEhnnWqYbi9AMWD3W0+IjeN2SVm5+Ev+JMTM eXFw== 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=P82/KxMwem55SDdX69nMkH32cKfDxHhS5VkXVWahv1Bc6fJ+sT6tR8AD9cGNBPOHjk 5LFjESgS7p/BTrNiuLDG8ku/dH/pE431uantxIBWSbWSZHuge1VTaXMpmOj5pl2hh/58 Vl6ddPI9rrCdyB0lDxsSfuSBMv5TCG/cfNC1jlnNQi9W8FQkH4MsZphmjRR4Yu+FsfAY A6Tl2gTOtR4EUfcjlwOMhLOFWzOSveWZXRXmNLIvr0DYLar44CXWlEHMYTCpUSaFNv4+ 0f26UEKN9gRqiC2UKmIi/bRfxeVi+HlqxqCfaaF3Q4//85zSr1qvNIJGanqUaY+QNE2Y sMaQ== X-Gm-Message-State: ACgBeo0W4zA7GNWSp7qdAAygh6ThNR3CelRMQZ/+L3pAaZQZsGK4JNJt GEaavAprTR8kiWisVWb9WFpXFDm1+/96 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:524b:47b4:2aeb:1b49]) (user=irogers job=sendgmr) by 2002:a0d:e004:0:b0:31f:311d:2351 with SMTP id j4-20020a0de004000000b0031f311d2351mr1986253ywe.10.1659080680507; Fri, 29 Jul 2022 00:44:40 -0700 (PDT) Date: Fri, 29 Jul 2022 00:43:49 -0700 In-Reply-To: <20220729074351.138260-1-irogers@google.com> Message-Id: <20220729074351.138260-16-irogers@google.com> Mime-Version: 1.0 References: <20220729074351.138260-1-irogers@google.com> X-Mailer: git-send-email 2.37.1.455.g008518b4e5-goog Subject: [PATCH v3 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" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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.455.g008518b4e5-goog