Skip to content

Commit 3d0f5f4

Browse files
James-A-Clarknamhyung
authored andcommitted
perf pmu: Move pmu__find_core_pmu() to pmus.c
pmu__find_core_pmu() more logically belongs in pmus.c because it iterates over all PMUs, so move it to pmus.c At the same time rename it to perf_pmus__find_core_pmu() to match the naming convention in this file. list_prepare_entry() can't be used in perf_pmus__scan_core() anymore now that it's called from the same compilation unit. This is with -O2 (specifically -O1 -ftree-vrp -finline-functions -finline-small-functions) which allow the bounds of the array access to be determined at compile time. list_prepare_entry() subtracts the offset of the 'list' member in struct perf_pmu from &core_pmus, which isn't a struct perf_pmu. The compiler sees that pmu results in &core_pmus - 8 and refuses to compile. At runtime this works because list_for_each_entry_continue() always adds the offset back again before dereferencing ->next, but it's technically undefined behavior. With -fsanitize=undefined an additional warning is generated. Using list_first_entry_or_null() to get the first entry here avoids doing &core_pmus - 8 but has the same result and fixes both the compile warning and the undefined behavior warning. There are other uses of list_prepare_entry() in pmus.c, but the compiler doesn't seem to be able to see that they can also be called with &core_pmus, so I won't change any at this time. Signed-off-by: James Clark <[email protected]> Reviewed-by: Ian Rogers <[email protected]> Reviewed-by: John Garry <[email protected]> Cc: Ravi Bangoria <[email protected]> Cc: Eduard Zingerman <[email protected]> Cc: Will Deacon <[email protected]> Cc: Leo Yan <[email protected]> Cc: Mike Leach <[email protected]> Cc: Jing Zhang <[email protected]> Cc: Haixin Yu <[email protected]> Cc: Kan Liang <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Namhyung Kim <[email protected]>
1 parent 21ce931 commit 3d0f5f4

File tree

6 files changed

+25
-24
lines changed

6 files changed

+25
-24
lines changed

tools/perf/arch/arm64/util/pmu.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
const struct pmu_metrics_table *pmu_metrics_table__find(void)
1212
{
13-
struct perf_pmu *pmu = pmu__find_core_pmu();
13+
struct perf_pmu *pmu = perf_pmus__find_core_pmu();
1414

1515
if (pmu)
1616
return perf_pmu__find_metrics_table(pmu);
@@ -20,7 +20,7 @@ const struct pmu_metrics_table *pmu_metrics_table__find(void)
2020

2121
const struct pmu_events_table *pmu_events_table__find(void)
2222
{
23-
struct perf_pmu *pmu = pmu__find_core_pmu();
23+
struct perf_pmu *pmu = perf_pmus__find_core_pmu();
2424

2525
if (pmu)
2626
return perf_pmu__find_events_table(pmu);
@@ -32,7 +32,7 @@ double perf_pmu__cpu_slots_per_cycle(void)
3232
{
3333
char path[PATH_MAX];
3434
unsigned long long slots = 0;
35-
struct perf_pmu *pmu = pmu__find_core_pmu();
35+
struct perf_pmu *pmu = perf_pmus__find_core_pmu();
3636

3737
if (pmu) {
3838
perf_pmu__pathname_scnprintf(path, sizeof(path),

tools/perf/tests/expr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static int test__expr(struct test_suite *t __maybe_unused, int subtest __maybe_u
7676
struct expr_parse_ctx *ctx;
7777
bool is_intel = false;
7878
char strcmp_cpuid_buf[256];
79-
struct perf_pmu *pmu = pmu__find_core_pmu();
79+
struct perf_pmu *pmu = perf_pmus__find_core_pmu();
8080
char *cpuid = perf_pmu__getcpuid(pmu);
8181
char *escaped_cpuid1, *escaped_cpuid2;
8282

tools/perf/util/expr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ double expr__strcmp_cpuid_str(const struct expr_parse_ctx *ctx __maybe_unused,
509509
bool compute_ids __maybe_unused, const char *test_id)
510510
{
511511
double ret;
512-
struct perf_pmu *pmu = pmu__find_core_pmu();
512+
struct perf_pmu *pmu = perf_pmus__find_core_pmu();
513513
char *cpuid = perf_pmu__getcpuid(pmu);
514514

515515
if (!cpuid)

tools/perf/util/pmu.c

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,20 +2050,3 @@ void perf_pmu__delete(struct perf_pmu *pmu)
20502050
zfree(&pmu->id);
20512051
free(pmu);
20522052
}
2053-
2054-
struct perf_pmu *pmu__find_core_pmu(void)
2055-
{
2056-
struct perf_pmu *pmu = NULL;
2057-
2058-
while ((pmu = perf_pmus__scan_core(pmu))) {
2059-
/*
2060-
* The cpumap should cover all CPUs. Otherwise, some CPUs may
2061-
* not support some events or have different event IDs.
2062-
*/
2063-
if (RC_CHK_ACCESS(pmu->cpus)->nr != cpu__max_cpu().cpu)
2064-
return NULL;
2065-
2066-
return pmu;
2067-
}
2068-
return NULL;
2069-
}

tools/perf/util/pmu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,6 @@ int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename,
264264
struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name);
265265
struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus);
266266
void perf_pmu__delete(struct perf_pmu *pmu);
267-
struct perf_pmu *pmu__find_core_pmu(void);
267+
struct perf_pmu *perf_pmus__find_core_pmu(void);
268268

269269
#endif /* __PMU_H */

tools/perf/util/pmus.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <pthread.h>
1111
#include <string.h>
1212
#include <unistd.h>
13+
#include "cpumap.h"
1314
#include "debug.h"
1415
#include "evsel.h"
1516
#include "pmus.h"
@@ -268,7 +269,7 @@ struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu)
268269
{
269270
if (!pmu) {
270271
pmu_read_sysfs(/*core_only=*/true);
271-
pmu = list_prepare_entry(pmu, &core_pmus, list);
272+
return list_first_entry_or_null(&core_pmus, typeof(*pmu), list);
272273
}
273274
list_for_each_entry_continue(pmu, &core_pmus, list)
274275
return pmu;
@@ -592,3 +593,20 @@ struct perf_pmu *evsel__find_pmu(const struct evsel *evsel)
592593
}
593594
return pmu;
594595
}
596+
597+
struct perf_pmu *perf_pmus__find_core_pmu(void)
598+
{
599+
struct perf_pmu *pmu = NULL;
600+
601+
while ((pmu = perf_pmus__scan_core(pmu))) {
602+
/*
603+
* The cpumap should cover all CPUs. Otherwise, some CPUs may
604+
* not support some events or have different event IDs.
605+
*/
606+
if (RC_CHK_ACCESS(pmu->cpus)->nr != cpu__max_cpu().cpu)
607+
return NULL;
608+
609+
return pmu;
610+
}
611+
return NULL;
612+
}

0 commit comments

Comments
 (0)