perf pmu: Remove logic for PMU name being NULL
authorIan Rogers <irogers@google.com>
Fri, 25 Aug 2023 02:39:58 +0000 (19:39 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Fri, 25 Aug 2023 13:22:16 +0000 (10:22 -0300)
The PMU name could be NULL in the case of the fake_pmu. Initialize the
name for the fake_pmu to "fake" so that all other logic can assume it
is initialized. Add a const to the type of name so that a literal can
be used to avoid additional initialization code. Propagate the cost
through related routines and remove now unnecessary "(char *)"
casts. Doing this located a bug in builtin-list for the pmu_glob that
was missing a strdup.

Signed-off-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20230825024002.801955-3-irogers@google.com
Cc: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: James Clark <james.clark@arm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Suzuki Poulouse <suzuki.poulose@arm.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Wei Li <liwei391@huawei.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Huacai Chen <chenhuacai@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Will Deacon <will@kernel.org>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: linux-arm-kernel@lists.infradead.org
Cc: Ming Wang <wangming01@loongson.cn>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: linux-kernel@vger.kernel.org
Cc: linux-perf-users@vger.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
15 files changed:
tools/perf/arch/arm64/util/mem-events.c
tools/perf/arch/powerpc/util/mem-events.c
tools/perf/arch/x86/util/mem-events.c
tools/perf/arch/x86/util/pmu.c
tools/perf/builtin-list.c
tools/perf/tests/pmu-events.c
tools/perf/util/env.c
tools/perf/util/header.c
tools/perf/util/mem-events.c
tools/perf/util/mem-events.h
tools/perf/util/parse-events.c
tools/perf/util/parse-events.h
tools/perf/util/parse-events.y
tools/perf/util/pmu.c
tools/perf/util/pmu.h

index df817d1..3bcc5c7 100644 (file)
@@ -20,7 +20,7 @@ struct perf_mem_event *perf_mem_events__ptr(int i)
        return &perf_mem_events[i];
 }
 
-char *perf_mem_events__name(int i, char *pmu_name __maybe_unused)
+const char *perf_mem_events__name(int i, const char *pmu_name __maybe_unused)
 {
        struct perf_mem_event *e = perf_mem_events__ptr(i);
 
index 4120faf..78b986e 100644 (file)
@@ -3,10 +3,10 @@
 #include "mem-events.h"
 
 /* PowerPC does not support 'ldlat' parameter. */
-char *perf_mem_events__name(int i, char *pmu_name __maybe_unused)
+const char *perf_mem_events__name(int i, const char *pmu_name __maybe_unused)
 {
        if (i == PERF_MEM_EVENTS__LOAD)
-               return (char *) "cpu/mem-loads/";
+               return "cpu/mem-loads/";
 
-       return (char *) "cpu/mem-stores/";
+       return "cpu/mem-stores/";
 }
index a8a782b..191b372 100644 (file)
@@ -52,7 +52,7 @@ bool is_mem_loads_aux_event(struct evsel *leader)
        return leader->core.attr.config == MEM_LOADS_AUX;
 }
 
-char *perf_mem_events__name(int i, char *pmu_name)
+const char *perf_mem_events__name(int i, const char *pmu_name)
 {
        struct perf_mem_event *e = perf_mem_events__ptr(i);
 
@@ -65,7 +65,7 @@ char *perf_mem_events__name(int i, char *pmu_name)
 
                if (!pmu_name) {
                        mem_loads_name__init = true;
-                       pmu_name = (char *)"cpu";
+                       pmu_name = "cpu";
                }
 
                if (perf_pmus__have_event(pmu_name, "mem-loads-aux")) {
@@ -82,12 +82,12 @@ char *perf_mem_events__name(int i, char *pmu_name)
 
        if (i == PERF_MEM_EVENTS__STORE) {
                if (!pmu_name)
-                       pmu_name = (char *)"cpu";
+                       pmu_name = "cpu";
 
                scnprintf(mem_stores_name, sizeof(mem_stores_name),
                          e->name, pmu_name);
                return mem_stores_name;
        }
 
-       return (char *)e->name;
+       return e->name;
 }
index 65d8cdf..f428cff 100644 (file)
@@ -126,7 +126,7 @@ close_dir:
        return ret;
 }
 
-static char *__pmu_find_real_name(const char *name)
+static const char *__pmu_find_real_name(const char *name)
 {
        struct pmu_alias *pmu_alias;
 
@@ -135,10 +135,10 @@ static char *__pmu_find_real_name(const char *name)
                        return pmu_alias->name;
        }
 
-       return (char *)name;
+       return name;
 }
 
-char *pmu_find_real_name(const char *name)
+const char *pmu_find_real_name(const char *name)
 {
        if (cached_list)
                return __pmu_find_real_name(name);
@@ -149,7 +149,7 @@ char *pmu_find_real_name(const char *name)
        return __pmu_find_real_name(name);
 }
 
-static char *__pmu_find_alias_name(const char *name)
+static const char *__pmu_find_alias_name(const char *name)
 {
        struct pmu_alias *pmu_alias;
 
@@ -160,7 +160,7 @@ static char *__pmu_find_alias_name(const char *name)
        return NULL;
 }
 
-char *pmu_find_alias_name(const char *name)
+const char *pmu_find_alias_name(const char *name)
 {
        if (cached_list)
                return __pmu_find_alias_name(name);
index d8b9f60..8ac97a8 100644 (file)
@@ -513,7 +513,7 @@ int cmd_list(int argc, const char **argv)
                                ret = -1;
                                goto out;
                        }
-                       default_ps.pmu_glob = pmu->name;
+                       default_ps.pmu_glob = strdup(pmu->name);
                }
        }
        print_cb.print_start(ps);
index 28c8789..d92de3d 100644 (file)
@@ -522,7 +522,7 @@ static int test_core_pmu_event_aliases_cb(void *state, struct pmu_event_info *al
 }
 
 /* Verify aliases are as expected */
-static int __test_core_pmu_event_aliases(char *pmu_name, int *count)
+static int __test_core_pmu_event_aliases(const char *pmu_name, int *count)
 {
        struct perf_pmu_test_event const **test_event_table;
        struct perf_pmu *pmu;
@@ -635,7 +635,7 @@ static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
 static struct perf_pmu_test_pmu test_pmus[] = {
        {
                .pmu = {
-                       .name = (char *)"hisi_sccl1_ddrc2",
+                       .name = "hisi_sccl1_ddrc2",
                        .is_uncore = 1,
                },
                .aliases = {
@@ -644,7 +644,7 @@ static struct perf_pmu_test_pmu test_pmus[] = {
        },
        {
                .pmu = {
-                       .name = (char *)"uncore_cbox_0",
+                       .name = "uncore_cbox_0",
                        .is_uncore = 1,
                },
                .aliases = {
@@ -655,7 +655,7 @@ static struct perf_pmu_test_pmu test_pmus[] = {
        },
        {
                .pmu = {
-                       .name = (char *)"hisi_sccl3_l3c7",
+                       .name = "hisi_sccl3_l3c7",
                        .is_uncore = 1,
                },
                .aliases = {
@@ -664,7 +664,7 @@ static struct perf_pmu_test_pmu test_pmus[] = {
        },
        {
                .pmu = {
-                       .name = (char *)"uncore_imc_free_running_0",
+                       .name = "uncore_imc_free_running_0",
                        .is_uncore = 1,
                },
                .aliases = {
@@ -673,7 +673,7 @@ static struct perf_pmu_test_pmu test_pmus[] = {
        },
        {
                .pmu = {
-                       .name = (char *)"uncore_imc_0",
+                       .name = "uncore_imc_0",
                        .is_uncore = 1,
                },
                .aliases = {
@@ -682,7 +682,7 @@ static struct perf_pmu_test_pmu test_pmus[] = {
        },
        {
                .pmu = {
-                       .name = (char *)"uncore_sys_ddr_pmu0",
+                       .name = "uncore_sys_ddr_pmu0",
                        .is_uncore = 1,
                        .id = (char *)"v8",
                },
@@ -692,7 +692,7 @@ static struct perf_pmu_test_pmu test_pmus[] = {
        },
        {
                .pmu = {
-                       .name = (char *)"uncore_sys_ccn_pmu4",
+                       .name = "uncore_sys_ccn_pmu4",
                        .is_uncore = 1,
                        .id = (char *)"0x01",
                },
index 9eabf3e..a164164 100644 (file)
@@ -324,11 +324,9 @@ int perf_env__read_pmu_mappings(struct perf_env *env)
        u32 pmu_num = 0;
        struct strbuf sb;
 
-       while ((pmu = perf_pmus__scan(pmu))) {
-               if (!pmu->name)
-                       continue;
+       while ((pmu = perf_pmus__scan(pmu)))
                pmu_num++;
-       }
+
        if (!pmu_num) {
                pr_debug("pmu mappings not available\n");
                return -ENOENT;
@@ -339,8 +337,6 @@ int perf_env__read_pmu_mappings(struct perf_env *env)
                return -ENOMEM;
 
        while ((pmu = perf_pmus__scan(pmu))) {
-               if (!pmu->name)
-                       continue;
                if (strbuf_addf(&sb, "%u:%s", pmu->type, pmu->name) < 0)
                        goto error;
                /* include a NULL character at the end */
index 13c71d2..9f35d86 100644 (file)
@@ -746,20 +746,14 @@ static int write_pmu_mappings(struct feat_fd *ff,
         * Do a first pass to count number of pmu to avoid lseek so this
         * works in pipe mode as well.
         */
-       while ((pmu = perf_pmus__scan(pmu))) {
-               if (!pmu->name)
-                       continue;
+       while ((pmu = perf_pmus__scan(pmu)))
                pmu_num++;
-       }
 
        ret = do_write(ff, &pmu_num, sizeof(pmu_num));
        if (ret < 0)
                return ret;
 
        while ((pmu = perf_pmus__scan(pmu))) {
-               if (!pmu->name)
-                       continue;
-
                ret = do_write(ff, &pmu->type, sizeof(pmu->type));
                if (ret < 0)
                        return ret;
index c07fe3a..39ffe8c 100644 (file)
@@ -37,7 +37,7 @@ struct perf_mem_event * __weak perf_mem_events__ptr(int i)
        return &perf_mem_events[i];
 }
 
-char * __weak perf_mem_events__name(int i, char *pmu_name  __maybe_unused)
+const char * __weak perf_mem_events__name(int i, const char *pmu_name  __maybe_unused)
 {
        struct perf_mem_event *e = perf_mem_events__ptr(i);
 
@@ -53,7 +53,7 @@ char * __weak perf_mem_events__name(int i, char *pmu_name  __maybe_unused)
                return mem_loads_name;
        }
 
-       return (char *)e->name;
+       return e->name;
 }
 
 __weak bool is_mem_loads_aux_event(struct evsel *leader __maybe_unused)
@@ -186,7 +186,6 @@ int perf_mem_events__record_args(const char **rec_argv, int *argv_nr,
        int i = *argv_nr, k = 0;
        struct perf_mem_event *e;
        struct perf_pmu *pmu;
-       char *s;
 
        for (int j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
                e = perf_mem_events__ptr(j);
@@ -209,15 +208,16 @@ int perf_mem_events__record_args(const char **rec_argv, int *argv_nr,
                        }
 
                        while ((pmu = perf_pmus__scan(pmu)) != NULL) {
+                               const char *s = perf_mem_events__name(j, pmu->name);
+
                                rec_argv[i++] = "-e";
-                               s = perf_mem_events__name(j, pmu->name);
                                if (s) {
-                                       s = strdup(s);
-                                       if (!s)
+                                       char *copy = strdup(s);
+                                       if (!copy)
                                                return -1;
 
-                                       rec_argv[i++] = s;
-                                       rec_tmp[k++] = s;
+                                       rec_argv[i++] = copy;
+                                       rec_tmp[k++] = copy;
                                }
                        }
                }
index 1237230..b40ad6e 100644 (file)
@@ -38,7 +38,7 @@ extern unsigned int perf_mem_events__loads_ldlat;
 int perf_mem_events__parse(const char *str);
 int perf_mem_events__init(void);
 
-char *perf_mem_events__name(int i, char *pmu_name);
+const char *perf_mem_events__name(int i, const char *pmu_name);
 struct perf_mem_event *perf_mem_events__ptr(int i);
 bool is_mem_loads_aux_event(struct evsel *leader);
 
index c71e135..621bdd1 100644 (file)
@@ -262,7 +262,7 @@ __add_event(struct list_head *list, int *idx,
        evsel->core.is_pmu_core = pmu ? pmu->is_core : false;
        evsel->auto_merge_stats = auto_merge_stats;
        evsel->pmu = pmu;
-       evsel->pmu_name = pmu && pmu->name ? strdup(pmu->name) : NULL;
+       evsel->pmu_name = pmu ? strdup(pmu->name) : NULL;
 
        if (name)
                evsel->name = strdup(name);
@@ -437,9 +437,6 @@ bool parse_events__filter_pmu(const struct parse_events_state *parse_state,
        if (parse_state->pmu_filter == NULL)
                return false;
 
-       if (pmu->name == NULL)
-               return true;
-
        return strcmp(parse_state->pmu_filter, pmu->name) != 0;
 }
 
@@ -1292,7 +1289,7 @@ static bool config_term_percore(struct list_head *config_terms)
 }
 
 int parse_events_add_pmu(struct parse_events_state *parse_state,
-                        struct list_head *list, char *name,
+                        struct list_head *list, const char *name,
                         struct list_head *head_config,
                         bool auto_merge_stats, void *loc_)
 {
index c7f7794..c4e8d8e 100644 (file)
@@ -176,7 +176,7 @@ int parse_events_add_breakpoint(struct parse_events_state *parse_state,
                                u64 addr, char *type, u64 len,
                                struct list_head *head_config);
 int parse_events_add_pmu(struct parse_events_state *parse_state,
-                        struct list_head *list, char *name,
+                        struct list_head *list, const char *name,
                         struct list_head *head_config,
                        bool auto_merge_stats, void *loc);
 
index 00da1f8..3a9d4e2 100644 (file)
@@ -307,7 +307,7 @@ PE_NAME opt_pmu_config
                }
 
                while ((pmu = perf_pmus__scan(pmu)) != NULL) {
-                       char *name = pmu->name;
+                       const char *name = pmu->name;
 
                        if (parse_events__filter_pmu(parse_state, pmu))
                                continue;
index bb2ca29..a96ccc5 100644 (file)
@@ -29,7 +29,9 @@
 #include "fncache.h"
 #include "util/evsel_config.h"
 
-struct perf_pmu perf_pmu__fake;
+struct perf_pmu perf_pmu__fake = {
+       .name = "fake",
+};
 
 #define UNIT_MAX_LEN   31 /* max length for event unit name */
 
@@ -967,13 +969,13 @@ perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
        return NULL;
 }
 
-char * __weak
+const char * __weak
 pmu_find_real_name(const char *name)
 {
-       return (char *)name;
+       return name;
 }
 
-char * __weak
+const char * __weak
 pmu_find_alias_name(const char *name __maybe_unused)
 {
        return NULL;
@@ -991,8 +993,8 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char
 {
        struct perf_pmu *pmu;
        __u32 type;
-       char *name = pmu_find_real_name(lookup_name);
-       char *alias_name;
+       const char *name = pmu_find_real_name(lookup_name);
+       const char *alias_name;
 
        pmu = zalloc(sizeof(*pmu));
        if (!pmu)
@@ -1974,7 +1976,7 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
                   name ?: "N/A", buf, config_name, config);
 }
 
-int perf_pmu__match(char *pattern, char *name, char *tok)
+int perf_pmu__match(const char *pattern, const char *name, const char *tok)
 {
        if (!name)
                return -1;
index bae0de3..e90169a 100644 (file)
@@ -39,7 +39,7 @@ struct perf_pmu_caps {
  */
 struct perf_pmu {
        /** @name: The name of the PMU such as "cpu". */
-       char *name;
+       const char *name;
        /**
         * @alias_name: Optional alternate name for the PMU determined in
         * architecture specific code.
@@ -249,10 +249,10 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
                                   const char *config_name);
 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu);
 
-int perf_pmu__match(char *pattern, char *name, char *tok);
+int perf_pmu__match(const char *pattern, const char *name, const char *tok);
 
-char *pmu_find_real_name(const char *name);
-char *pmu_find_alias_name(const char *name);
+const char *pmu_find_real_name(const char *name);
+const char *pmu_find_alias_name(const char *name);
 double perf_pmu__cpu_slots_per_cycle(void);
 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size);
 int perf_pmu__pathname_scnprintf(char *buf, size_t size,