Skip to content

Commit 628eaa4

Browse files
captain5050namhyung
authored andcommitted
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/[email protected]/ Reported-by: Yang Jihong <[email protected]> Tested-by: Yang Jihong <[email protected]> Signed-off-by: Ian Rogers <[email protected]> Cc: Ravi Bangoria <[email protected]> Cc: James Clark <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Suzuki Poulouse <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Arnaldo Carvalho de Melo <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Rob Herring <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Kan Liang <[email protected]> Cc: Ingo Molnar <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Namhyung Kim <[email protected]>
1 parent ad5f604 commit 628eaa4

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

tools/perf/util/pmu.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,31 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char
928928
return NULL;
929929
}
930930

931+
/* Creates the PMU when sysfs scanning fails. */
932+
struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus)
933+
{
934+
struct perf_pmu *pmu = zalloc(sizeof(*pmu));
935+
936+
if (!pmu)
937+
return NULL;
938+
939+
pmu->name = strdup("cpu");
940+
if (!pmu->name) {
941+
free(pmu);
942+
return NULL;
943+
}
944+
945+
pmu->is_core = true;
946+
pmu->type = PERF_TYPE_RAW;
947+
pmu->cpus = cpu_map__online();
948+
949+
INIT_LIST_HEAD(&pmu->format);
950+
INIT_LIST_HEAD(&pmu->aliases);
951+
INIT_LIST_HEAD(&pmu->caps);
952+
list_add_tail(&pmu->list, core_pmus);
953+
return pmu;
954+
}
955+
931956
void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
932957
{
933958
struct perf_pmu_format *format;

tools/perf/util/pmu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ int perf_pmu__event_source_devices_fd(void);
286286
int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags);
287287

288288
struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name);
289+
struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus);
289290
void perf_pmu__delete(struct perf_pmu *pmu);
290291

291292
#endif /* __PMU_H */

tools/perf/util/pmus.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,12 @@ static void pmu_read_sysfs(bool core_only)
153153

154154
closedir(dir);
155155
if (core_only) {
156-
read_sysfs_core_pmus = true;
156+
if (!list_empty(&core_pmus))
157+
read_sysfs_core_pmus = true;
158+
else {
159+
if (perf_pmu__create_placeholder_core_pmu(&core_pmus))
160+
read_sysfs_core_pmus = true;
161+
}
157162
} else {
158163
read_sysfs_core_pmus = true;
159164
read_sysfs_all_pmus = true;

0 commit comments

Comments
 (0)