Commit 628eaa4e authored by Ian Rogers's avatar Ian Rogers Committed by Namhyung Kim

perf pmus: Add placeholder core PMU

If loading a core PMU fails, legacy hardware/cache events may segv due
to there being no PMU. Create a placeholder empty PMU for this
case. This was discussed in:
https://lore.kernel.org/lkml/20230614151625.2077-1-yangjihong1@huawei.com/Reported-by: default avatarYang Jihong <yangjihong1@huawei.com>
Tested-by: default avatarYang Jihong <yangjihong1@huawei.com>
Signed-off-by: default avatarIan Rogers <irogers@google.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: Peter Zijlstra <peterz@infradead.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Rob Herring <robh@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Link: https://lore.kernel.org/r/20230627182834.117565-1-irogers@google.comSigned-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
parent ad5f604e
...@@ -928,6 +928,31 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char ...@@ -928,6 +928,31 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char
return NULL; return NULL;
} }
/* Creates the PMU when sysfs scanning fails. */
struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus)
{
struct perf_pmu *pmu = zalloc(sizeof(*pmu));
if (!pmu)
return NULL;
pmu->name = strdup("cpu");
if (!pmu->name) {
free(pmu);
return NULL;
}
pmu->is_core = true;
pmu->type = PERF_TYPE_RAW;
pmu->cpus = cpu_map__online();
INIT_LIST_HEAD(&pmu->format);
INIT_LIST_HEAD(&pmu->aliases);
INIT_LIST_HEAD(&pmu->caps);
list_add_tail(&pmu->list, core_pmus);
return pmu;
}
void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu) void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
{ {
struct perf_pmu_format *format; struct perf_pmu_format *format;
......
...@@ -286,6 +286,7 @@ int perf_pmu__event_source_devices_fd(void); ...@@ -286,6 +286,7 @@ int perf_pmu__event_source_devices_fd(void);
int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags); int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags);
struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name); struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name);
struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus);
void perf_pmu__delete(struct perf_pmu *pmu); void perf_pmu__delete(struct perf_pmu *pmu);
#endif /* __PMU_H */ #endif /* __PMU_H */
...@@ -153,7 +153,12 @@ static void pmu_read_sysfs(bool core_only) ...@@ -153,7 +153,12 @@ static void pmu_read_sysfs(bool core_only)
closedir(dir); closedir(dir);
if (core_only) { if (core_only) {
read_sysfs_core_pmus = true; if (!list_empty(&core_pmus))
read_sysfs_core_pmus = true;
else {
if (perf_pmu__create_placeholder_core_pmu(&core_pmus))
read_sysfs_core_pmus = true;
}
} else { } else {
read_sysfs_core_pmus = true; read_sysfs_core_pmus = true;
read_sysfs_all_pmus = true; read_sysfs_all_pmus = true;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment