Skip to contentSkip to conversation

perf pmus: Add placeholder core PMU

PATCH Applied Ian Rogers 3 participants 3 messages Jun 27, 2023 – Jun 28, 2023 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 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]/

---
 tools/perf/util/pmu.c  | 25 +++++++++++++++++++++++++
 tools/perf/util/pmu.h  |  1 +
 tools/perf/util/pmus.c |  7 ++++++-
 3 files changed, 32 insertions(+), 1 deletion(-)
tools/perf/util/pmu.c +25 -0
index 8d5ecd4ff1a9..7f984a7f16ca 100644
@@ -928,6 +928,31 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char
 	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)
 {
 	struct perf_pmu_format *format;
tools/perf/util/pmu.h +1 -0
index 8807a624e918..203b92860e3c 100644
@@ -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);
 
 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);
 
 #endif /* __PMU_H */
tools/perf/util/pmus.c +6 -1
index 0866dee3fc62..3cd9de42139e 100644
@@ -153,7 +153,12 @@ static void pmu_read_sysfs(bool core_only)
 
 	closedir(dir);
 	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 {
 		read_sysfs_core_pmus = true;
 		read_sysfs_all_pmus = true;
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
Tested-by on v1 1/1
#1 lore raw

Hello,

Quoted 70 lines · Ian Rogers

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]/

---
  tools/perf/util/pmu.c  | 25 +++++++++++++++++++++++++
  tools/perf/util/pmu.h  |  1 +
  tools/perf/util/pmus.c |  7 ++++++-
  3 files changed, 32 insertions(+), 1 deletion(-)
tools/perf/util/pmu.c +25 -0
index 8d5ecd4ff1a9..7f984a7f16ca 100644
@@ -928,6 +928,31 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char
  	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)
  {
  	struct perf_pmu_format *format;
tools/perf/util/pmu.h +1 -0
index 8807a624e918..203b92860e3c 100644
@@ -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);
 
  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);
 
  #endif /* __PMU_H */
tools/perf/util/pmus.c +0 -0
index 0866dee3fc62..3cd9de42139e 100644
@@ -153,7 +153,12 @@ static void pmu_read_sysfs(bool core_only)
 
  	closedir(dir);
  	if (core_only) {

tools/perf/util/pmus.c static void pmu_read_sysfs(bool core_only) · line 164

Quoted 10 lines
tools/perf/util/pmus.c +6 -1
-		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 {
  		read_sysfs_core_pmus = true;
  		read_sysfs_all_pmus = true;

Great. This patch solves the problem, the test results are shown in [1].

[1]:

# perf --debug verbose=2 record true
<SNIP>
------------------------------------------------------------
perf_event_attr:
  size                             136
  { sample_period, sample_freq }   4000
  sample_type                      IP|TID|TIME|PERIOD
  read_format                      ID|LOST
  disabled                         1
  inherit                          1
  mmap                             1
  comm                             1
  freq                             1
  enable_on_exec                   1
  task                             1
  precise_ip                       3
  sample_id_all                    1
  mmap2                            1
  comm_exec                        1
  ksymbol                          1
  bpf_event                        1
------------------------------------------------------------
sys_perf_event_open: pid 703  cpu 0  group_fd -1  flags 0x8
sys_perf_event_open failed, error -2
decreasing precise_ip by one (2)
------------------------------------------------------------
perf_event_attr:
  size                             136
  { sample_period, sample_freq }   4000
  sample_type                      IP|TID|TIME|PERIOD
  read_format                      ID|LOST
  disabled                         1
  inherit                          1
  mmap                             1
  comm                             1
  freq                             1
  enable_on_exec                   1
  task                             1
  precise_ip                       2
  sample_id_all                    1
  mmap2                            1
  comm_exec                        1
  ksymbol                          1
  bpf_event                        1
------------------------------------------------------------
sys_perf_event_open: pid 703  cpu 0  group_fd -1  flags 0x8
sys_perf_event_open failed, error -2
decreasing precise_ip by one (1)
------------------------------------------------------------
perf_event_attr:
  size                             136
  { sample_period, sample_freq }   4000
  sample_type                      IP|TID|TIME|PERIOD
  read_format                      ID|LOST
  disabled                         1
  inherit                          1
  mmap                             1
  comm                             1
  freq                             1
  enable_on_exec                   1
  task                             1
  precise_ip                       1
  sample_id_all                    1
  mmap2                            1
  comm_exec                        1
  ksymbol                          1
  bpf_event                        1
------------------------------------------------------------
sys_perf_event_open: pid 703  cpu 0  group_fd -1  flags 0x8
sys_perf_event_open failed, error -2
decreasing precise_ip by one (0)
------------------------------------------------------------
perf_event_attr:
  size                             136
  { sample_period, sample_freq }   4000
  sample_type                      IP|TID|TIME|PERIOD
  read_format                      ID|LOST
  disabled                         1
  inherit                          1
  mmap                             1
  comm                             1
  freq                             1
  enable_on_exec                   1
  task                             1
  sample_id_all                    1
  mmap2                            1
  comm_exec                        1
  ksymbol                          1
  bpf_event                        1
------------------------------------------------------------
sys_perf_event_open: pid 703  cpu 0  group_fd -1  flags 0x8
sys_perf_event_open failed, error -2
Warning:
The cycles event is not supported, trying to fall back to cpu-clock-ticks
------------------------------------------------------------
perf_event_attr:
  type                             1
  size                             136
  { sample_period, sample_freq }   4000
  sample_type                      IP|TID|TIME|PERIOD
  read_format                      ID|LOST
  disabled                         1
  inherit                          1
  mmap                             1
  comm                             1
  freq                             1
  enable_on_exec                   1
  task                             1
  precise_ip                       3
  sample_id_all                    1
  mmap2                            1
  comm_exec                        1
  ksymbol                          1
  bpf_event                        1
------------------------------------------------------------
sys_perf_event_open: pid 703  cpu 0  group_fd -1  flags 0x8 = 5
sys_perf_event_open: pid 703  cpu 1  group_fd -1  flags 0x8 = 6
sys_perf_event_open: pid 703  cpu 2  group_fd -1  flags 0x8 = 7
sys_perf_event_open: pid 703  cpu 3  group_fd -1  flags 0x8 = 9
sys_perf_event_open: pid 703  cpu 4  group_fd -1  flags 0x8 = 10
sys_perf_event_open: pid 703  cpu 5  group_fd -1  flags 0x8 = 11
sys_perf_event_open: pid 703  cpu 6  group_fd -1  flags 0x8 = 12
sys_perf_event_open: pid 703  cpu 7  group_fd -1  flags 0x8 = 13
<SNIP>
[ perf record: Captured and wrote 0.006 MB perf.data (116 samples) ]

Thanks,
Yang

maintainer Applied
#2 lore raw
Quoted 86 lines · Yang Jihong

Hello,

Quoted 70 lines · Ian Rogers

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]/

---
  tools/perf/util/pmu.c  | 25 +++++++++++++++++++++++++
  tools/perf/util/pmu.h  |  1 +
  tools/perf/util/pmus.c |  7 ++++++-
  3 files changed, 32 insertions(+), 1 deletion(-)
tools/perf/util/pmu.c +25 -0
index 8d5ecd4ff1a9..7f984a7f16ca 100644
@@ -928,6 +928,31 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char
      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)
  {
      struct perf_pmu_format *format;
tools/perf/util/pmu.h +1 -0
index 8807a624e918..203b92860e3c 100644
@@ -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);
 
  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);
 
  #endif /* __PMU_H */
tools/perf/util/pmus.c +0 -0
index 0866dee3fc62..3cd9de42139e 100644
@@ -153,7 +153,12 @@ static void pmu_read_sysfs(bool core_only)
 
      closedir(dir);
      if (core_only) {

tools/perf/util/pmus.c static void pmu_read_sysfs(bool core_only) · line 164

Quoted 10 lines
tools/perf/util/pmus.c +6 -1
-             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 {
              read_sysfs_core_pmus = true;
              read_sysfs_all_pmus = true;

Great. This patch solves the problem, the test results are shown in [1].

Quoted 1 line

Applied to perf-tools-next, thanks!