Skip to contentSkip to conversation

perf header: Fix missing PMU caps

PATCH Superseded Ian Rogers 3 participants 5 messages Aug 18, 2023 – Aug 25, 2023 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf header: Fix missing PMU caps

PMU caps are written as HEADER_PMU_CAPS or for the special case of the PMU "cpu" as HEADER_CPU_PMU_CAPS. As the PMU "cpu" is special, and not any "core" PMU, the logic had become broken and core PMUs not called "cpu" were not having their caps written. This affects ARM and s390 non-hybrid PMUs.

Simplify the PMU caps writing logic to scan one fewer time and to be more explicit in its behavior.

Fixes 178ddf3bad98 ("perf header: Avoid hybrid PMU list in write_pmu_caps")
---
 tools/perf/util/header.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
tools/perf/util/header.c +16 -15
index 52fbf526fe74..13c71d28e0eb 100644
@@ -1605,8 +1605,15 @@ static int write_pmu_caps(struct feat_fd *ff,
 	int ret;
 
 	while ((pmu = perf_pmus__scan(pmu))) {
-		if (!pmu->name || !strcmp(pmu->name, "cpu") ||
-		    perf_pmu__caps_parse(pmu) <= 0)
+		if (!strcmp(pmu->name, "cpu")) {
+			/*
+			 * The "cpu" PMU is special and covered by
+			 * HEADER_CPU_PMU_CAPS. Note, core PMUs are
+			 * counted/written here for ARM, s390 and Intel hybrid.
+			 */
+			continue;
+		}
+		if (perf_pmu__caps_parse(pmu) <= 0)
 			continue;
 		nr_pmu++;
 	}
@@ -1619,23 +1626,17 @@ static int write_pmu_caps(struct feat_fd *ff,
 		return 0;
 
 	/*
-	 * Write hybrid pmu caps first to maintain compatibility with
-	 * older perf tool.
+	 * Note older perf tools assume core PMUs come first, this is a property
+	 * of perf_pmus__scan.
 	 */
-	if (perf_pmus__num_core_pmus() > 1) {
-		pmu = NULL;
-		while ((pmu = perf_pmus__scan_core(pmu))) {
-			ret = __write_pmu_caps(ff, pmu, true);
-			if (ret < 0)
-				return ret;
-		}
-	}
-
 	pmu = NULL;
 	while ((pmu = perf_pmus__scan(pmu))) {
-		if (pmu->is_core || !pmu->nr_caps)
+		if (!strcmp(pmu->name, "cpu")) {
+			/* Skip as above. */
+			continue;
+		}
+		if (perf_pmu__caps_parse(pmu) <= 0)
 			continue;
-
 		ret = __write_pmu_caps(ff, pmu, true);
 		if (ret < 0)
 			return ret;
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
#1 lore raw

Hi Ian:

Quoted 21 lines · Ian Rogers

PMU caps are written as HEADER_PMU_CAPS or for the special case of the PMU "cpu" as HEADER_CPU_PMU_CAPS. As the PMU "cpu" is special, and not any "core" PMU, the logic had become broken and core PMUs not called "cpu" were not having their caps written. This affects ARM and s390 non-hybrid PMUs.

Simplify the PMU caps writing logic to scan one fewer time and to be more explicit in its behavior.

Fixes 178ddf3bad98 ("perf header: Avoid hybrid PMU list in write_pmu_caps")
---
 tools/perf/util/header.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
tools/perf/util/header.c +0 -0
index 52fbf526fe74..13c71d28e0eb 100644

tools/perf/util/header.c static int write_pmu_caps(struct feat_fd *ff, · line 1608

Quoted 6 lines
tools/perf/util/header.c +1 -2
@@ -1605,8 +1605,15 @@ static int write_pmu_caps(struct feat_fd *ff,
 	int ret;
 
 	while ((pmu = perf_pmus__scan(pmu))) {
-		if (!pmu->name || !strcmp(pmu->name, "cpu") ||
-		    perf_pmu__caps_parse(pmu) <= 0)
+		if (!strcmp(pmu->name, "cpu")) {

So you removed the check of 'pmu->name', does this check really redundant? since we can find such checks in many places in the perf code. If not, i think it is necessary for strcmp().

Quoted 23 lines
tools/perf/util/header.c +10 -5
+			/*
+			 * The "cpu" PMU is special and covered by
+			 * HEADER_CPU_PMU_CAPS. Note, core PMUs are
+			 * counted/written here for ARM, s390 and Intel hybrid.
+			 */
+			continue;
+		}
+		if (perf_pmu__caps_parse(pmu) <= 0)
 			continue;
 		nr_pmu++;
 	}
@@ -1619,23 +1626,17 @@ static int write_pmu_caps(struct feat_fd *ff,
 		return 0;
 
 	/*
-	 * Write hybrid pmu caps first to maintain compatibility with
-	 * older perf tool.
+	 * Note older perf tools assume core PMUs come first, this is a property
+	 * of perf_pmus__scan.
 	 */
-	if (perf_pmus__num_core_pmus() > 1) {
-		pmu = NULL;
-		while ((pmu = perf_pmus__scan_core(pmu))) {

tools/perf/util/header.c static int write_pmu_caps(struct feat_fd *ff, · line 1634

Quoted 10 lines
tools/perf/util/header.c +1 -7
-			ret = __write_pmu_caps(ff, pmu, true);
-			if (ret < 0)
-				return ret;
-		}
-	}
-
 	pmu = NULL;
 	while ((pmu = perf_pmus__scan(pmu))) {
-		if (pmu->is_core || !pmu->nr_caps)
+		if (!strcmp(pmu->name, "cpu")) {

same here

Thanks,
Wei

Quoted 9 lines
tools/perf/util/header.c +4 -1
+			/* Skip as above. */
+			continue;
+		}
+		if (perf_pmu__caps_parse(pmu) <= 0)
 			continue;
-
 		ret = __write_pmu_caps(ff, pmu, true);
 		if (ret < 0)
 			return ret;
#2 lore raw

Em Sat, Aug 19, 2023 at 12:16:09PM +0800, liwei (GF) escreveu:

Quoted 26 lines

Hi Ian:

Quoted 23 lines · Ian Rogers

PMU caps are written as HEADER_PMU_CAPS or for the special case of the PMU "cpu" as HEADER_CPU_PMU_CAPS. As the PMU "cpu" is special, and not any "core" PMU, the logic had become broken and core PMUs not called "cpu" were not having their caps written. This affects ARM and s390 non-hybrid PMUs.

Simplify the PMU caps writing logic to scan one fewer time and to be more explicit in its behavior.

Fixes 178ddf3bad98 ("perf header: Avoid hybrid PMU list in write_pmu_caps")
---
 tools/perf/util/header.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
tools/perf/util/header.c +0 -0
index 52fbf526fe74..13c71d28e0eb 100644
@@ -1605,8 +1605,15 @@ static int write_pmu_caps(struct feat_fd *ff,
 	int ret;
 
Quoted 8 lines

tools/perf/util/header.c static int write_pmu_caps(struct feat_fd *ff, · line 1608

Quoted 4 lines
tools/perf/util/header.c +1 -2
 	while ((pmu = perf_pmus__scan(pmu))) {
-		if (!pmu->name || !strcmp(pmu->name, "cpu") ||
-		    perf_pmu__caps_parse(pmu) <= 0)
+		if (!strcmp(pmu->name, "cpu")) {

So you removed the check of 'pmu->name', does this check really redundant? since we can find such checks in many places in the perf code. If not, i think it is necessary for strcmp().

Indeed, when sorting in tools/perf/util/pmus.c in cmp_sevent() we have:

        /* Order by PMU name. */
        if (as->pmu != bs->pmu) {
                a_pmu_name = a_pmu_name ?: (as->pmu->name ?: "");
                b_pmu_name = b_pmu_name ?: (bs->pmu->name ?: "");
                ret = strcmp(a_pmu_name, b_pmu_name);
                if (ret)
                        return ret;
        }

And even if in this specific case, for some reason, we could guarantee that pmu->name isn't NULL, then removing that check should be best left for a separate patch with an explanation as to why that is safe.

Having it as:

while ((pmu = perf_pmus__scan(pmu))) {
- if (!pmu->name || !strcmp(pmu->name, "cpu") ||
- perf_pmu__caps_parse(pmu) <= 0)
+ if (!pmu->name || !strcmp(pmu->name, "cpu")) {

even eases a bit reviewing, as we see we're just removing that perf_pmu__caps_parse(pmu) line.

Ian?

- Arnaldo

Quoted 48 lines
Quoted 23 lines
tools/perf/util/header.c +10 -5
+			/*
+			 * The "cpu" PMU is special and covered by
+			 * HEADER_CPU_PMU_CAPS. Note, core PMUs are
+			 * counted/written here for ARM, s390 and Intel hybrid.
+			 */
+			continue;
+		}
+		if (perf_pmu__caps_parse(pmu) <= 0)
 			continue;
 		nr_pmu++;
 	}
@@ -1619,23 +1626,17 @@ static int write_pmu_caps(struct feat_fd *ff,
 		return 0;
 
 	/*
-	 * Write hybrid pmu caps first to maintain compatibility with
-	 * older perf tool.
+	 * Note older perf tools assume core PMUs come first, this is a property
+	 * of perf_pmus__scan.
 	 */
-	if (perf_pmus__num_core_pmus() > 1) {
-		pmu = NULL;
-		while ((pmu = perf_pmus__scan_core(pmu))) {

tools/perf/util/header.c static int write_pmu_caps(struct feat_fd *ff, · line 1634

Quoted 10 lines
tools/perf/util/header.c +1 -7
-			ret = __write_pmu_caps(ff, pmu, true);
-			if (ret < 0)
-				return ret;
-		}
-	}
-
 	pmu = NULL;
 	while ((pmu = perf_pmus__scan(pmu))) {
-		if (pmu->is_core || !pmu->nr_caps)
+		if (!strcmp(pmu->name, "cpu")) {

same here

Thanks,
Wei

Quoted 9 lines
tools/perf/util/header.c +4 -1
+			/* Skip as above. */
+			continue;
+		}
+		if (perf_pmu__caps_parse(pmu) <= 0)
 			continue;
-
 		ret = __write_pmu_caps(ff, pmu, true);
 		if (ret < 0)
 			return ret;
Signature · 1 line
- Arnaldo
author reviewer
#3 lore raw
Quoted 60 lines · Arnaldo Carvalho de Melo

Em Sat, Aug 19, 2023 at 12:16:09PM +0800, liwei (GF) escreveu:

Quoted 26 lines

Hi Ian:

Quoted 23 lines · Ian Rogers

PMU caps are written as HEADER_PMU_CAPS or for the special case of the PMU "cpu" as HEADER_CPU_PMU_CAPS. As the PMU "cpu" is special, and not any "core" PMU, the logic had become broken and core PMUs not called "cpu" were not having their caps written. This affects ARM and s390 non-hybrid PMUs.

Simplify the PMU caps writing logic to scan one fewer time and to be more explicit in its behavior.

Fixes 178ddf3bad98 ("perf header: Avoid hybrid PMU list in write_pmu_caps")
---
 tools/perf/util/header.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
tools/perf/util/header.c +0 -0
index 52fbf526fe74..13c71d28e0eb 100644
@@ -1605,8 +1605,15 @@ static int write_pmu_caps(struct feat_fd *ff,
    int ret;
 
Quoted 8 lines

tools/perf/util/header.c static int write_pmu_caps(struct feat_fd *ff, · line 1608

Quoted 4 lines
tools/perf/util/header.c +1 -2
    while ((pmu = perf_pmus__scan(pmu))) {
-           if (!pmu->name || !strcmp(pmu->name, "cpu") ||
-               perf_pmu__caps_parse(pmu) <= 0)
+           if (!strcmp(pmu->name, "cpu")) {

So you removed the check of 'pmu->name', does this check really redundant? since we can find such checks in many places in the perf code. If not, i think it is necessary for strcmp().

Indeed, when sorting in tools/perf/util/pmus.c in cmp_sevent() we have:

        /* Order by PMU name. */
        if (as->pmu != bs->pmu) {
                a_pmu_name = a_pmu_name ?: (as->pmu->name ?: "");
                b_pmu_name = b_pmu_name ?: (bs->pmu->name ?: "");
                ret = strcmp(a_pmu_name, b_pmu_name);
                if (ret)
                        return ret;
        }

And even if in this specific case, for some reason, we could guarantee that pmu->name isn't NULL, then removing that check should be best left for a separate patch with an explanation as to why that is safe.

Having it as:

        while ((pmu = perf_pmus__scan(pmu))) {
tools/perf/util/header.c +1 -2
-               if (!pmu->name || !strcmp(pmu->name, "cpu") ||
-                   perf_pmu__caps_parse(pmu) <= 0)
+               if (!pmu->name || !strcmp(pmu->name, "cpu")) {
Quoted 4 lines

even eases a bit reviewing, as we see we're just removing that perf_pmu__caps_parse(pmu) line.

Ian?

The pmu name is initialized with:

https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/pmu.c?h=perf-tools-next#n1001
pmu->name = strdup(name);
if (!pmu->name)
goto err;

so name can't be NULL, strdup of NULL is segv, as if it were pmu would be NULL. I'll clean this up in an additional patch on top of this one.

Thanks,
Ian

Quoted 55 lines

- Arnaldo

Quoted 36 lines
Quoted 23 lines
tools/perf/util/header.c +10 -5
+                   /*
+                    * The "cpu" PMU is special and covered by
+                    * HEADER_CPU_PMU_CAPS. Note, core PMUs are
+                    * counted/written here for ARM, s390 and Intel hybrid.
+                    */
+                   continue;
+           }
+           if (perf_pmu__caps_parse(pmu) <= 0)
                    continue;
            nr_pmu++;
    }
@@ -1619,23 +1626,17 @@ static int write_pmu_caps(struct feat_fd *ff,
            return 0;
 
    /*
-    * Write hybrid pmu caps first to maintain compatibility with
-    * older perf tool.
+    * Note older perf tools assume core PMUs come first, this is a property
+    * of perf_pmus__scan.
     */
-   if (perf_pmus__num_core_pmus() > 1) {
-           pmu = NULL;
-           while ((pmu = perf_pmus__scan_core(pmu))) {

tools/perf/util/header.c static int write_pmu_caps(struct feat_fd *ff, · line 1634

Quoted 10 lines
tools/perf/util/header.c +1 -7
-                   ret = __write_pmu_caps(ff, pmu, true);
-                   if (ret < 0)
-                           return ret;
-           }
-   }
-
    pmu = NULL;
    while ((pmu = perf_pmus__scan(pmu))) {
-           if (pmu->is_core || !pmu->nr_caps)
+           if (!strcmp(pmu->name, "cpu")) {

same here

Quoted 12 lines

Thanks,
Wei

Quoted 9 lines
tools/perf/util/header.c +4 -1
+                   /* Skip as above. */
+                   continue;
+           }
+           if (perf_pmu__caps_parse(pmu) <= 0)
                    continue;
-
            ret = __write_pmu_caps(ff, pmu, true);
            if (ret < 0)
                    return ret;

--

- Arnaldo

#4 lore raw

Em Thu, Aug 24, 2023 at 03:54:00PM -0700, Ian Rogers escreveu:

Quoted 69 lines
Quoted 60 lines · Arnaldo Carvalho de Melo

Em Sat, Aug 19, 2023 at 12:16:09PM +0800, liwei (GF) escreveu:

Quoted 26 lines

Hi Ian:

Quoted 23 lines · Ian Rogers

PMU caps are written as HEADER_PMU_CAPS or for the special case of the PMU "cpu" as HEADER_CPU_PMU_CAPS. As the PMU "cpu" is special, and not any "core" PMU, the logic had become broken and core PMUs not called "cpu" were not having their caps written. This affects ARM and s390 non-hybrid PMUs.

Simplify the PMU caps writing logic to scan one fewer time and to be more explicit in its behavior.

Fixes 178ddf3bad98 ("perf header: Avoid hybrid PMU list in write_pmu_caps")
---
 tools/perf/util/header.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
tools/perf/util/header.c +0 -0
index 52fbf526fe74..13c71d28e0eb 100644
@@ -1605,8 +1605,15 @@ static int write_pmu_caps(struct feat_fd *ff,
    int ret;
 
Quoted 8 lines

tools/perf/util/header.c static int write_pmu_caps(struct feat_fd *ff, · line 1608

Quoted 4 lines
tools/perf/util/header.c +1 -2
    while ((pmu = perf_pmus__scan(pmu))) {
-           if (!pmu->name || !strcmp(pmu->name, "cpu") ||
-               perf_pmu__caps_parse(pmu) <= 0)
+           if (!strcmp(pmu->name, "cpu")) {

So you removed the check of 'pmu->name', does this check really redundant? since we can find such checks in many places in the perf code. If not, i think it is necessary for strcmp().

Indeed, when sorting in tools/perf/util/pmus.c in cmp_sevent() we have:

        /* Order by PMU name. */
        if (as->pmu != bs->pmu) {
                a_pmu_name = a_pmu_name ?: (as->pmu->name ?: "");
                b_pmu_name = b_pmu_name ?: (bs->pmu->name ?: "");
                ret = strcmp(a_pmu_name, b_pmu_name);
                if (ret)
                        return ret;
        }

And even if in this specific case, for some reason, we could guarantee that pmu->name isn't NULL, then removing that check should be best left for a separate patch with an explanation as to why that is safe.

Having it as:

        while ((pmu = perf_pmus__scan(pmu))) {
tools/perf/util/header.c +1 -2
-               if (!pmu->name || !strcmp(pmu->name, "cpu") ||
-                   perf_pmu__caps_parse(pmu) <= 0)
+               if (!pmu->name || !strcmp(pmu->name, "cpu")) {
Quoted 4 lines

even eases a bit reviewing, as we see we're just removing that perf_pmu__caps_parse(pmu) line.

Ian?

The pmu name is initialized with:

Quoted 7 lines

https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/pmu.c?h=perf-tools-next#n1001
pmu->name = strdup(name);
if (!pmu->name)
goto err;

so name can't be NULL, strdup of NULL is segv, as if it were pmu would be NULL. I'll clean this up in an additional patch on top of this one.

Ok, perhaps at some point we can introduce a perf_pmu__name_is(pmu, "cpu") and then have an assert for it not being NULL, as for some time those tests were performed.

But thanks for checking, I'll apply the patches.

- Arnaldo

Quoted 58 lines

Thanks,
Ian

Quoted 55 lines

- Arnaldo

Quoted 36 lines
Quoted 23 lines
tools/perf/util/header.c +10 -5
+                   /*
+                    * The "cpu" PMU is special and covered by
+                    * HEADER_CPU_PMU_CAPS. Note, core PMUs are
+                    * counted/written here for ARM, s390 and Intel hybrid.
+                    */
+                   continue;
+           }
+           if (perf_pmu__caps_parse(pmu) <= 0)
                    continue;
            nr_pmu++;
    }
@@ -1619,23 +1626,17 @@ static int write_pmu_caps(struct feat_fd *ff,
            return 0;
 
    /*
-    * Write hybrid pmu caps first to maintain compatibility with
-    * older perf tool.
+    * Note older perf tools assume core PMUs come first, this is a property
+    * of perf_pmus__scan.
     */
-   if (perf_pmus__num_core_pmus() > 1) {
-           pmu = NULL;
-           while ((pmu = perf_pmus__scan_core(pmu))) {

tools/perf/util/header.c static int write_pmu_caps(struct feat_fd *ff, · line 1634

Quoted 10 lines
tools/perf/util/header.c +1 -7
-                   ret = __write_pmu_caps(ff, pmu, true);
-                   if (ret < 0)
-                           return ret;
-           }
-   }
-
    pmu = NULL;
    while ((pmu = perf_pmus__scan(pmu))) {
-           if (pmu->is_core || !pmu->nr_caps)
+           if (!strcmp(pmu->name, "cpu")) {

same here

Quoted 12 lines

Thanks,
Wei

Quoted 9 lines
tools/perf/util/header.c +4 -1
+                   /* Skip as above. */
+                   continue;
+           }
+           if (perf_pmu__caps_parse(pmu) <= 0)
                    continue;
-
            ret = __write_pmu_caps(ff, pmu, true);
            if (ret < 0)
                    return ret;

--

- Arnaldo

Signature · 1 line
- Arnaldo