From: Ian Rogers Date: Thu, 10 Jul 2025 23:51:17 +0000 (-0700) Subject: perf pmu: Tolerate failure to read the type for wellknown PMUs X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=8c75dc742089c702cab6d0f21be80c5ddd3c6067;p=users%2Fjedix%2Flinux-maple.git perf pmu: Tolerate failure to read the type for wellknown PMUs If sysfs isn't mounted then we may fail to read a PMU's type. In this situation resort to lookup of wellknown types. Only applies to software, tracepoint and breakpoint PMUs. Signed-off-by: Ian Rogers Link: https://lore.kernel.org/r/20250710235126.1086011-5-irogers@google.com Signed-off-by: Namhyung Kim --- diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index f795883c233f..23666883049d 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -1182,6 +1182,32 @@ int perf_pmu__init(struct perf_pmu *pmu, __u32 type, const char *name) return 0; } +static __u32 wellknown_pmu_type(const char *pmu_name) +{ + struct { + const char *pmu_name; + __u32 type; + } wellknown_pmus[] = { + { + "software", + PERF_TYPE_SOFTWARE + }, + { + "tracepoint", + PERF_TYPE_TRACEPOINT + }, + { + "breakpoint", + PERF_TYPE_BREAKPOINT + }, + }; + for (size_t i = 0; i < ARRAY_SIZE(wellknown_pmus); i++) { + if (!strcmp(wellknown_pmus[i].pmu_name, pmu_name)) + return wellknown_pmus[i].type; + } + return PERF_TYPE_MAX; +} + struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *name, bool eager_load) { @@ -1201,8 +1227,12 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char * that type value is successfully assigned (return 1). */ if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &pmu->type) != 1) { - perf_pmu__delete(pmu); - return NULL; + /* Double check the PMU's name isn't wellknown. */ + pmu->type = wellknown_pmu_type(name); + if (pmu->type == PERF_TYPE_MAX) { + perf_pmu__delete(pmu); + return NULL; + } } /*