Skip to contentSkip to conversation

perf tests: mmap: skip rdpmc tests when sysfs disabled

PATCH In review Qiao Zhao 3 participants 8 messages Feb 3 – Feb 12 perf events

lore
Share & tools
lore

Qiao Zhao

author
#0 lore raw

PATCH v2 1/1 perf tests: mmap: skip rdpmc tests when sysfs disabled

Some architectures control user-space rdpmc access via sysfs or sysctl, for example /proc/sys/kernel/perf_user_access on arm64/riscv or the rdpmc attribute under event_source devices on x86_64.

The mmap-based rdpmc tests currently assume rdpmc availability purely based on perf_event configuration, which leads to false failures when the system globally disables rdpmc access.

Introduce a helper to detect rdpmc availability via sysfs and skip user space counter reading tests when rdpmc is disabled by the system.

The check is only applied to USER_READ_ENABLED tests. User-read disabled tests continue to run unconditionally, as they validate perf_event mmap semantics independent of rdpmc availability.

Additionally, tighten the unsupported rdpmc check to require a valid counter index, avoiding false failures when cap_user_rdpmc is set but no counter is assigned.

v1->v2
- Check rdpmc availability via sysfs before running USER_READ_ENABLED tests
- Handle both perf_user_access and rdpmc sysfs interfaces
- Avoid false failures when cap_user_rdpmc is set but no counter index exists

Testing:
- arm64: rdpmc disabled by perf_user_access (tests correctly skipped)
- x86_64: no behavior change compared to v1

---
 tools/perf/tests/mmap-basic.c | 45 +++++++++++++++++++++++++++++++++--
 1 file changed, 43 insertions(+), 2 deletions(-)
tools/perf/tests/mmap-basic.c +43 -2
index 3313c236104e..67d6d83dbedb 100644
@@ -220,6 +220,37 @@ static enum user_read_state set_user_read(struct perf_pmu *pmu, enum user_read_s
 	return old_user_read;
 }
 
+static bool rdpmc_sysfs_enabled(struct perf_pmu *pmu)
+{
+	char buf[8];
+	int events_fd, fd;
+	ssize_t len;
+
+	/* ARM/RISC-V style or similar */
+	fd = open("/proc/sys/kernel/perf_user_access", O_RDONLY);
+	if (fd > 0) {
+		len = read(fd, buf, sizeof(buf));
+		close(fd);
+		if (len > 0)
+			return buf[0] == '1';
+	}
+
+	/* x86_64 style rdpmc file or similar */
+	events_fd = perf_pmu__event_source_devices_fd();
+	if (events_fd >= 0) {
+		fd = perf_pmu__pathname_fd(events_fd, pmu->name, "rdpmc", O_RDONLY);
+		close(events_fd);
+		if (fd >= 0) {
+			len = read(fd, buf, sizeof(buf));
+			close(fd);
+			if (len > 0)
+				return buf[0] == '1';
+		}
+	}
+
+	return false;
+}
+
 static int test_stat_user_read(u64 event, enum user_read_state enabled)
 {
 	struct perf_pmu *pmu = NULL;
@@ -247,7 +278,7 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
 		struct perf_evsel *evsel = NULL;
 		int err;
 		struct perf_event_mmap_page *pc;
-		bool mapped = false, opened = false, rdpmc_supported;
+		bool mapped = false, opened = false, rdpmc_supported, sysfs_enabled;
 		struct perf_counts_values counts = { .val = 0 };
 
 
@@ -300,6 +331,16 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
 		else
 			rdpmc_supported = (enabled == USER_READ_ENABLED);
 
+		if (enabled == USER_READ_ENABLED) {
+			sysfs_enabled = rdpmc_sysfs_enabled(pmu);
+			if (!sysfs_enabled) {
+				pr_debug("User space counter reading for PMU %s [Skip rdpmc disabled by sysfs]\n",
+					pmu->name);
+				ret = TEST_SKIP;
+				goto cleanup;
+			}
+		}
+
 		if (rdpmc_supported && (!pc->cap_user_rdpmc || !pc->index)) {
 			pr_err("User space counter reading for PMU %s [Failed unexpected supported counter access %d %d]\n",
 				pmu->name, pc->cap_user_rdpmc, pc->index);
@@ -307,7 +348,7 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
 			goto cleanup;
 		}
 
-		if (!rdpmc_supported && pc->cap_user_rdpmc) {
+		if (!rdpmc_supported && pc->cap_user_rdpmc && pc->index) {
 			pr_err("User space counter reading for PMU %s [Failed unexpected unsupported counter access %d]\n",
 				pmu->name, pc->cap_user_rdpmc);
 			ret = TEST_FAIL;
Signature · 1 line
2.49.0
reviewer Tested-by on v2 1/1
#1 lore raw
Quoted 25 lines · Qiao Zhao

Some architectures control user-space rdpmc access via sysfs or sysctl, for example /proc/sys/kernel/perf_user_access on arm64/riscv or the rdpmc attribute under event_source devices on x86_64.

The mmap-based rdpmc tests currently assume rdpmc availability purely based on perf_event configuration, which leads to false failures when the system globally disables rdpmc access.

Introduce a helper to detect rdpmc availability via sysfs and skip user space counter reading tests when rdpmc is disabled by the system.

The check is only applied to USER_READ_ENABLED tests. User-read disabled tests continue to run unconditionally, as they validate perf_event mmap semantics independent of rdpmc availability.

Additionally, tighten the unsupported rdpmc check to require a valid counter index, avoiding false failures when cap_user_rdpmc is set but no counter is assigned.

v1->v2
- Check rdpmc availability via sysfs before running USER_READ_ENABLED tests
- Handle both perf_user_access and rdpmc sysfs interfaces
- Avoid false failures when cap_user_rdpmc is set but no counter index exists

Quoted 5 lines

Testing:
- arm64: rdpmc disabled by perf_user_access (tests correctly skipped)
- x86_64: no behavior change compared to v1

On x86:

Thanks,
Ian

Quoted 84 lines
---
 tools/perf/tests/mmap-basic.c | 45 +++++++++++++++++++++++++++++++++--
 1 file changed, 43 insertions(+), 2 deletions(-)
tools/perf/tests/mmap-basic.c +43 -3
index 3313c236104e..67d6d83dbedb 100644
@@ -220,6 +220,37 @@ static enum user_read_state set_user_read(struct perf_pmu *pmu, enum user_read_s
        return old_user_read;
 }
 
+static bool rdpmc_sysfs_enabled(struct perf_pmu *pmu)
+{
+       char buf[8];
+       int events_fd, fd;
+       ssize_t len;
+
+       /* ARM/RISC-V style or similar */
+       fd = open("/proc/sys/kernel/perf_user_access", O_RDONLY);
+       if (fd > 0) {
+               len = read(fd, buf, sizeof(buf));
+               close(fd);
+               if (len > 0)
+                       return buf[0] == '1';
+       }
+
+       /* x86_64 style rdpmc file or similar */
+       events_fd = perf_pmu__event_source_devices_fd();
+       if (events_fd >= 0) {
+               fd = perf_pmu__pathname_fd(events_fd, pmu->name, "rdpmc", O_RDONLY);
+               close(events_fd);
+               if (fd >= 0) {
+                       len = read(fd, buf, sizeof(buf));
+                       close(fd);
+                       if (len > 0)
+                               return buf[0] == '1';
+               }
+       }
+
+       return false;
+}
+
 static int test_stat_user_read(u64 event, enum user_read_state enabled)
 {
        struct perf_pmu *pmu = NULL;
@@ -247,7 +278,7 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
                struct perf_evsel *evsel = NULL;
                int err;
                struct perf_event_mmap_page *pc;
-               bool mapped = false, opened = false, rdpmc_supported;
+               bool mapped = false, opened = false, rdpmc_supported, sysfs_enabled;
                struct perf_counts_values counts = { .val = 0 };
 
 
@@ -300,6 +331,16 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
                else
                        rdpmc_supported = (enabled == USER_READ_ENABLED);
 
+               if (enabled == USER_READ_ENABLED) {
+                       sysfs_enabled = rdpmc_sysfs_enabled(pmu);
+                       if (!sysfs_enabled) {
+                               pr_debug("User space counter reading for PMU %s [Skip rdpmc disabled by sysfs]\n",
+                                       pmu->name);
+                               ret = TEST_SKIP;
+                               goto cleanup;
+                       }
+               }
+
                if (rdpmc_supported && (!pc->cap_user_rdpmc || !pc->index)) {
                        pr_err("User space counter reading for PMU %s [Failed unexpected supported counter access %d %d]\n",
                                pmu->name, pc->cap_user_rdpmc, pc->index);
@@ -307,7 +348,7 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
                        goto cleanup;
                }
 
-               if (!rdpmc_supported && pc->cap_user_rdpmc) {
+               if (!rdpmc_supported && pc->cap_user_rdpmc && pc->index) {
                        pr_err("User space counter reading for PMU %s [Failed unexpected unsupported counter access %d]\n",
                                pmu->name, pc->cap_user_rdpmc);
                        ret = TEST_FAIL;
--

2.49.0

reviewer Tested-by on v2 1/1
#2 lore raw
In reply to Qiao Zhao
Quoted 102 lines · Qiao Zhao

Some architectures control user-space rdpmc access via sysfs or sysctl, for example /proc/sys/kernel/perf_user_access on arm64/riscv or the rdpmc attribute under event_source devices on x86_64.

The mmap-based rdpmc tests currently assume rdpmc availability purely based on perf_event configuration, which leads to false failures when the system globally disables rdpmc access.

Introduce a helper to detect rdpmc availability via sysfs and skip user space counter reading tests when rdpmc is disabled by the system.

The check is only applied to USER_READ_ENABLED tests. User-read disabled tests continue to run unconditionally, as they validate perf_event mmap semantics independent of rdpmc availability.

Additionally, tighten the unsupported rdpmc check to require a valid counter index, avoiding false failures when cap_user_rdpmc is set but no counter is assigned.

v1->v2
- Check rdpmc availability via sysfs before running USER_READ_ENABLED tests
- Handle both perf_user_access and rdpmc sysfs interfaces
- Avoid false failures when cap_user_rdpmc is set but no counter index exists

Testing:
- arm64: rdpmc disabled by perf_user_access (tests correctly skipped)
- x86_64: no behavior change compared to v1

---
  tools/perf/tests/mmap-basic.c | 45 +++++++++++++++++++++++++++++++++--
  1 file changed, 43 insertions(+), 2 deletions(-)
tools/perf/tests/mmap-basic.c +42 -1
index 3313c236104e..67d6d83dbedb 100644
@@ -220,6 +220,37 @@ static enum user_read_state set_user_read(struct perf_pmu *pmu, enum user_read_s
  	return old_user_read;
  }
 
+static bool rdpmc_sysfs_enabled(struct perf_pmu *pmu)
+{
+	char buf[8];
+	int events_fd, fd;
+	ssize_t len;
+
+	/* ARM/RISC-V style or similar */
+	fd = open("/proc/sys/kernel/perf_user_access", O_RDONLY);
+	if (fd > 0) {
+		len = read(fd, buf, sizeof(buf));
+		close(fd);
+		if (len > 0)
+			return buf[0] == '1';
+	}
+
+	/* x86_64 style rdpmc file or similar */
+	events_fd = perf_pmu__event_source_devices_fd();
+	if (events_fd >= 0) {
+		fd = perf_pmu__pathname_fd(events_fd, pmu->name, "rdpmc", O_RDONLY);
+		close(events_fd);
+		if (fd >= 0) {
+			len = read(fd, buf, sizeof(buf));
+			close(fd);
+			if (len > 0)
+				return buf[0] == '1';
+		}
+	}
+
+	return false;
+}
+
  static int test_stat_user_read(u64 event, enum user_read_state enabled)
  {
  	struct perf_pmu *pmu = NULL;
@@ -247,7 +278,7 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
  		struct perf_evsel *evsel = NULL;
  		int err;
  		struct perf_event_mmap_page *pc;
-		bool mapped = false, opened = false, rdpmc_supported;
+		bool mapped = false, opened = false, rdpmc_supported, sysfs_enabled;
  		struct perf_counts_values counts = { .val = 0 };
 
 
@@ -300,6 +331,16 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
  		else
  			rdpmc_supported = (enabled == USER_READ_ENABLED);
 
+		if (enabled == USER_READ_ENABLED) {
+			sysfs_enabled = rdpmc_sysfs_enabled(pmu);
+			if (!sysfs_enabled) {
+				pr_debug("User space counter reading for PMU %s [Skip rdpmc disabled by sysfs]\n",
+					pmu->name);
+				ret = TEST_SKIP;
+				goto cleanup;
+			}
+		}
+
  		if (rdpmc_supported && (!pc->cap_user_rdpmc || !pc->index)) {
  			pr_err("User space counter reading for PMU %s [Failed unexpected supported counter access %d %d]\n",
  				pmu->name, pc->cap_user_rdpmc, pc->index);

tools/perf/tests/mmap-basic.c static int test_stat_user_read(u64 event, enum user_read_state enabled) · line 354

Quoted 8 lines
tools/perf/tests/mmap-basic.c +1 -1
@@ -307,7 +348,7 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
  			goto cleanup;
  		}
 
-		if (!rdpmc_supported && pc->cap_user_rdpmc) {
+		if (!rdpmc_supported && pc->cap_user_rdpmc && pc->index) {
  			pr_err("User space counter reading for PMU %s [Failed unexpected unsupported counter access %d]\n",
  				pmu->name, pc->cap_user_rdpmc);
  			ret = TEST_FAIL;

Not sure I understand this test, shouldn't it be (pc->cap_user_rdpmc || pc->index)? As in, you don't expect either of them to be set if !rdpmc_supported. But then it's the exact opposite of the one above so I don't think it does anything.

Either way the test now passes or skips properly on Arm:

author
#3 lore raw

Hi James,

Thanks for the review and the question.

I did try modifying the check according to your suggestion, i.e. changing the condition like:
+ if (!rdpmc_supported && (pc->cap_user_rdpmc || pc->index)) {
However, with this change the tests regress on ARM, the results are:
--- start ---
User space counter reading 1
User space counter reading for PMU armv8_pmuv3_0
User space counter reading for PMU armv8_pmuv3_0 [Failed unexpected unsupported counter access 1]
---- end ----
126.4: User space counter disabling instructions : FAILED!
--- start ---
User space counter reading 0
User space counter reading for PMU armv8_pmuv3_0
User space counter reading for PMU armv8_pmuv3_0 [Failed unexpected unsupported counter access 1]
---- end ----
126.5: User space counter disabling cycles : FAILED!

On ARM, cap_user_rdpmc represents a PMU capability and may remain set even when user rdpmc access is disabled via sysfs/sysctl. In that case the mmap page reports: cap_user_rdpmc = 1 and index = 0, which is an expected and valid state when rdpmc is disabled.
The intention of the existing check: if (!rdpmc_supported && pc->cap_user_rdpmc && pc->index) is only to catch the inconsistent case where rdpmc is reported as unsupported, but the mmap page still exposes a fully usable rdpmc state (i.e. both capability and a valid PMC index). This avoids flagging the ARM case above while still detecting real inconsistencies.

With the current logic, the tests now either pass or are skipped as expected on ARM, and there is no behavior change observed on x86_64.

Thanks,
Qiao

Quoted 125 lines · James Clark
Quoted 102 lines · Qiao Zhao

Some architectures control user-space rdpmc access via sysfs or sysctl, for example /proc/sys/kernel/perf_user_access on arm64/riscv or the rdpmc attribute under event_source devices on x86_64.

The mmap-based rdpmc tests currently assume rdpmc availability purely based on perf_event configuration, which leads to false failures when the system globally disables rdpmc access.

Introduce a helper to detect rdpmc availability via sysfs and skip user space counter reading tests when rdpmc is disabled by the system.

The check is only applied to USER_READ_ENABLED tests. User-read disabled tests continue to run unconditionally, as they validate perf_event mmap semantics independent of rdpmc availability.

Additionally, tighten the unsupported rdpmc check to require a valid counter index, avoiding false failures when cap_user_rdpmc is set but no counter is assigned.

v1->v2
- Check rdpmc availability via sysfs before running USER_READ_ENABLED tests
- Handle both perf_user_access and rdpmc sysfs interfaces
- Avoid false failures when cap_user_rdpmc is set but no counter index exists

Testing:
- arm64: rdpmc disabled by perf_user_access (tests correctly skipped)
- x86_64: no behavior change compared to v1

---
  tools/perf/tests/mmap-basic.c | 45 +++++++++++++++++++++++++++++++++--
  1 file changed, 43 insertions(+), 2 deletions(-)
tools/perf/tests/mmap-basic.c +42 -1
index 3313c236104e..67d6d83dbedb 100644
@@ -220,6 +220,37 @@ static enum user_read_state set_user_read(struct perf_pmu *pmu, enum user_read_s
      return old_user_read;
  }
 
+static bool rdpmc_sysfs_enabled(struct perf_pmu *pmu)
+{
+     char buf[8];
+     int events_fd, fd;
+     ssize_t len;
+
+     /* ARM/RISC-V style or similar */
+     fd = open("/proc/sys/kernel/perf_user_access", O_RDONLY);
+     if (fd > 0) {
+             len = read(fd, buf, sizeof(buf));
+             close(fd);
+             if (len > 0)
+                     return buf[0] == '1';
+     }
+
+     /* x86_64 style rdpmc file or similar */
+     events_fd = perf_pmu__event_source_devices_fd();
+     if (events_fd >= 0) {
+             fd = perf_pmu__pathname_fd(events_fd, pmu->name, "rdpmc", O_RDONLY);
+             close(events_fd);
+             if (fd >= 0) {
+                     len = read(fd, buf, sizeof(buf));
+                     close(fd);
+                     if (len > 0)
+                             return buf[0] == '1';
+             }
+     }
+
+     return false;
+}
+
  static int test_stat_user_read(u64 event, enum user_read_state enabled)
  {
      struct perf_pmu *pmu = NULL;
@@ -247,7 +278,7 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
              struct perf_evsel *evsel = NULL;
              int err;
              struct perf_event_mmap_page *pc;
-             bool mapped = false, opened = false, rdpmc_supported;
+             bool mapped = false, opened = false, rdpmc_supported, sysfs_enabled;
              struct perf_counts_values counts = { .val = 0 };
 
 
@@ -300,6 +331,16 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
              else
                      rdpmc_supported = (enabled == USER_READ_ENABLED);
 
+             if (enabled == USER_READ_ENABLED) {
+                     sysfs_enabled = rdpmc_sysfs_enabled(pmu);
+                     if (!sysfs_enabled) {
+                             pr_debug("User space counter reading for PMU %s [Skip rdpmc disabled by sysfs]\n",
+                                     pmu->name);
+                             ret = TEST_SKIP;
+                             goto cleanup;
+                     }
+             }
+
              if (rdpmc_supported && (!pc->cap_user_rdpmc || !pc->index)) {
                      pr_err("User space counter reading for PMU %s [Failed unexpected supported counter access %d %d]\n",
                              pmu->name, pc->cap_user_rdpmc, pc->index);

tools/perf/tests/mmap-basic.c static int test_stat_user_read(u64 event, enum user_read_state enabled) · line 354

Quoted 8 lines
tools/perf/tests/mmap-basic.c +1 -1
@@ -307,7 +348,7 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
                      goto cleanup;
              }
 
-             if (!rdpmc_supported && pc->cap_user_rdpmc) {
+             if (!rdpmc_supported && pc->cap_user_rdpmc && pc->index) {
                      pr_err("User space counter reading for PMU %s [Failed unexpected unsupported counter access %d]\n",
                              pmu->name, pc->cap_user_rdpmc);
                      ret = TEST_FAIL;

Not sure I understand this test, shouldn't it be (pc->cap_user_rdpmc || pc->index)? As in, you don't expect either of them to be set if !rdpmc_supported. But then it's the exact opposite of the one above so I don't think it does anything.

Either way the test now passes or skips properly on Arm:

reviewer
#4 lore raw
Quoted 23 lines · Qiao Zhao

Hi James,

Thanks for the review and the question.

I did try modifying the check according to your suggestion, i.e. changing the condition like:
+ if (!rdpmc_supported && (pc->cap_user_rdpmc || pc->index)) {
However, with this change the tests regress on ARM, the results are:
--- start ---
User space counter reading 1
User space counter reading for PMU armv8_pmuv3_0
User space counter reading for PMU armv8_pmuv3_0 [Failed unexpected unsupported counter access 1]
---- end ----
126.4: User space counter disabling instructions : FAILED!
--- start ---
User space counter reading 0
User space counter reading for PMU armv8_pmuv3_0
User space counter reading for PMU armv8_pmuv3_0 [Failed unexpected unsupported counter access 1]
---- end ----
126.5: User space counter disabling cycles : FAILED!

Quoted 3 lines

On ARM, cap_user_rdpmc represents a PMU capability and may remain set even when user rdpmc access is disabled via sysfs/sysctl. In that case the mmap page reports: cap_user_rdpmc = 1 and index = 0, which is an

That's a slightly annoying difference to x86 then, which makes it a bit harder to test.

Quoted 7 lines

expected and valid state when rdpmc is disabled.
The intention of the existing check: if (!rdpmc_supported && pc->cap_user_rdpmc && pc->index) is only to catch the inconsistent case where rdpmc is reported as unsupported, but the mmap page still exposes a fully usable rdpmc state (i.e. both capability and a valid PMC index). This avoids flagging the ARM case above while still detecting real inconsistencies.

Quoted 6 lines

With the current logic, the tests now either pass or are skipped as expected on ARM, and there is no behavior change observed on x86_64.

Thanks,
Qiao

But "rdpmc_supported = pc->cap_user_rdpmc && pc->index" here:

  if (saved_user_read_state == USER_READ_UNKNOWN)
      rdpmc_supported = pc->cap_user_rdpmc && pc->index;

So I'm still not sure I get checking rdpmc_supported with those exact two things again.

In fact I looked more closely and the test is already setup to write to <pmu>/rdmpc to run the test twice with it both enabled and disabled in
set_user_read(). We only return USER_READ_UNKNOWN in that function on perf_user_access platforms, but only because that function doesn't know the place to enable it is /proc/sys/kernel/perf_user_access.

So actually we should modify set_user_read() to try enabling in both places, and then the test can test enabled and disabled outcomes on Arm as well as x86. Now that we know there is this other file we can make the original intention of the test work with it, rather that just adding a skip and not testing anything.

Quoted 34 lines · James Clark
Quoted 33 lines · Qiao Zhao
Quoted 29 lines

Some architectures control user-space rdpmc access via sysfs or sysctl, for example /proc/sys/kernel/perf_user_access on arm64/riscv or the rdpmc attribute under event_source devices on x86_64.

The mmap-based rdpmc tests currently assume rdpmc availability purely based on perf_event configuration, which leads to false failures when the system globally disables rdpmc access.

Introduce a helper to detect rdpmc availability via sysfs and skip user space counter reading tests when rdpmc is disabled by the system.

The check is only applied to USER_READ_ENABLED tests. User-read disabled tests continue to run unconditionally, as they validate perf_event mmap semantics independent of rdpmc availability.

Additionally, tighten the unsupported rdpmc check to require a valid counter index, avoiding false failures when cap_user_rdpmc is set but no counter is assigned.

v1->v2
- Check rdpmc availability via sysfs before running USER_READ_ENABLED tests
- Handle both perf_user_access and rdpmc sysfs interfaces
- Avoid false failures when cap_user_rdpmc is set but no counter index exists

Testing:
- arm64: rdpmc disabled by perf_user_access (tests correctly skipped)
- x86_64: no behavior change compared to v1

[...]

Quoted 9 lines
Quoted 9 lines

tools/perf/tests/mmap-basic.c static int test_stat_user_read(u64 event, enum user_read_state enabled) · line 351

Quoted 9 lines
               if (rdpmc_supported && (!pc->cap_user_rdpmc || !pc->index)) {
                       pr_err("User space counter reading for PMU %s [Failed unexpected supported counter access %d %d]\n",
                               pmu->name, pc->cap_user_rdpmc, pc->index);
tools/perf/tests/mmap-basic.c +1 -1
@@ -307,7 +348,7 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
                       goto cleanup;
               }
 
-             if (!rdpmc_supported && pc->cap_user_rdpmc) {
+             if (!rdpmc_supported && pc->cap_user_rdpmc && pc->index) {

This weakens the test on x86 though. If they behave differently you might want to just skip it on platforms with /proc/sys/kernel/perf_user_access and add another test for them that pc->index shouldn't be set if it's not supported, but ignore cap_user_rdpmc.

Quoted 15 lines
Quoted 14 lines

tools/perf/tests/mmap-basic.c static int test_stat_user_read(u64 event, enum user_read_state enabled) · line 354

Quoted 3 lines
tools/perf/tests/mmap-basic.c +0 -0
                       pr_err("User space counter reading for PMU %s [Failed unexpected unsupported counter access %d]\n",
                               pmu->name, pc->cap_user_rdpmc);
                       ret = TEST_FAIL;

Not sure I understand this test, shouldn't it be (pc->cap_user_rdpmc || pc->index)? As in, you don't expect either of them to be set if !rdpmc_supported. But then it's the exact opposite of the one above so I don't think it does anything.

Either way the test now passes or skips properly on Arm:

author
#5 lore raw
Quoted 62 lines · James Clark
Quoted 23 lines · Qiao Zhao

Hi James,

Thanks for the review and the question.

I did try modifying the check according to your suggestion, i.e. changing the condition like:
+ if (!rdpmc_supported && (pc->cap_user_rdpmc || pc->index)) {
However, with this change the tests regress on ARM, the results are:
--- start ---
User space counter reading 1
User space counter reading for PMU armv8_pmuv3_0
User space counter reading for PMU armv8_pmuv3_0 [Failed unexpected unsupported counter access 1]
---- end ----
126.4: User space counter disabling instructions : FAILED!
--- start ---
User space counter reading 0
User space counter reading for PMU armv8_pmuv3_0
User space counter reading for PMU armv8_pmuv3_0 [Failed unexpected unsupported counter access 1]
---- end ----
126.5: User space counter disabling cycles : FAILED!

Quoted 3 lines

On ARM, cap_user_rdpmc represents a PMU capability and may remain set even when user rdpmc access is disabled via sysfs/sysctl. In that case the mmap page reports: cap_user_rdpmc = 1 and index = 0, which is an

That's a slightly annoying difference to x86 then, which makes it a bit harder to test.

Quoted 7 lines

expected and valid state when rdpmc is disabled.
The intention of the existing check: if (!rdpmc_supported && pc->cap_user_rdpmc && pc->index) is only to catch the inconsistent case where rdpmc is reported as unsupported, but the mmap page still exposes a fully usable rdpmc state (i.e. both capability and a valid PMC index). This avoids flagging the ARM case above while still detecting real inconsistencies.

Quoted 6 lines

With the current logic, the tests now either pass or are skipped as expected on ARM, and there is no behavior change observed on x86_64.

Thanks,
Qiao

But "rdpmc_supported = pc->cap_user_rdpmc && pc->index" here:

   if (saved_user_read_state == USER_READ_UNKNOWN)
       rdpmc_supported = pc->cap_user_rdpmc && pc->index;

So I'm still not sure I get checking rdpmc_supported with those exact two things again.

In fact I looked more closely and the test is already setup to write to <pmu>/rdmpc to run the test twice with it both enabled and disabled in set_user_read(). We only return USER_READ_UNKNOWN in that function on perf_user_access platforms, but only because that function doesn't know the place to enable it is /proc/sys/kernel/perf_user_access.

Quoted 5 lines

So actually we should modify set_user_read() to try enabling in both places, and then the test can test enabled and disabled outcomes on Arm as well as x86. Now that we know there is this other file we can make the original intention of the test work with it, rather that just adding a skip and not testing anything.

Thanks for suggesting to modify set_user_read() so that it can try enabling rdpmc
both locations (PMU <pmu>/rdpmc and sysfs file /proc/sys/kernel/perf_user_access).

Before preparing the next version, I would like to confirm the expected direction.

My current understanding is that the intended behavior of the mmap rdpmc test is to actively toggle the user-space counter access (enable/disable) and verify both
outcomes, rather than skipping the test when rdpmc is unavailable. Based on this,
the next step would be to update set_user_read() so that it attempts to control rdpmc
through <pmu>/rdpmc first and falls back to /proc/sys/kernel/perf_user_access when
the PMU interface is not present.

Hi Ian,
Could you please confirm if this is the preferred approach for the next revision, or if we
should instead keep the current skip logic for platforms where neither interface can be
modified?

Thanks for the guidance.

- Qiao

Quoted 69 lines
Quoted 35 lines · James Clark
Quoted 33 lines · Qiao Zhao
Quoted 29 lines

Some architectures control user-space rdpmc access via sysfs or sysctl, for example /proc/sys/kernel/perf_user_access on arm64/riscv or the rdpmc attribute under event_source devices on x86_64.

The mmap-based rdpmc tests currently assume rdpmc availability purely based on perf_event configuration, which leads to false failures when the system globally disables rdpmc access.

Introduce a helper to detect rdpmc availability via sysfs and skip user space counter reading tests when rdpmc is disabled by the system.

The check is only applied to USER_READ_ENABLED tests. User-read disabled tests continue to run unconditionally, as they validate perf_event mmap semantics independent of rdpmc availability.

Additionally, tighten the unsupported rdpmc check to require a valid counter index, avoiding false failures when cap_user_rdpmc is set but no counter is assigned.

v1->v2
- Check rdpmc availability via sysfs before running USER_READ_ENABLED tests
- Handle both perf_user_access and rdpmc sysfs interfaces
- Avoid false failures when cap_user_rdpmc is set but no counter index exists

Testing:
- arm64: rdpmc disabled by perf_user_access (tests correctly skipped)
- x86_64: no behavior change compared to v1

[...]

Quoted 9 lines
Quoted 9 lines

tools/perf/tests/mmap-basic.c static int test_stat_user_read(u64 event, enum user_read_state enabled) · line 351

Quoted 9 lines
               if (rdpmc_supported && (!pc->cap_user_rdpmc || !pc->index)) {
                       pr_err("User space counter reading for PMU %s [Failed unexpected supported counter access %d %d]\n",
                               pmu->name, pc->cap_user_rdpmc, pc->index);
tools/perf/tests/mmap-basic.c +1 -1
@@ -307,7 +348,7 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
                       goto cleanup;
               }
 
-             if (!rdpmc_supported && pc->cap_user_rdpmc) {
+             if (!rdpmc_supported && pc->cap_user_rdpmc && pc->index) {

This weakens the test on x86 though. If they behave differently you might want to just skip it on platforms with /proc/sys/kernel/perf_user_access and add another test for them that pc->index shouldn't be set if it's not supported, but ignore cap_user_rdpmc.

Quoted 15 lines
Quoted 14 lines

tools/perf/tests/mmap-basic.c static int test_stat_user_read(u64 event, enum user_read_state enabled) · line 354

Quoted 3 lines
tools/perf/tests/mmap-basic.c +0 -0
                       pr_err("User space counter reading for PMU %s [Failed unexpected unsupported counter access %d]\n",
                               pmu->name, pc->cap_user_rdpmc);
                       ret = TEST_FAIL;

Not sure I understand this test, shouldn't it be (pc->cap_user_rdpmc || pc->index)? As in, you don't expect either of them to be set if !rdpmc_supported. But then it's the exact opposite of the one above so I don't think it does anything.

Either way the test now passes or skips properly on Arm:

reviewer
#6 lore raw
Quoted 89 lines · Qiao Zhao
Quoted 62 lines · James Clark
Quoted 23 lines · Qiao Zhao

Hi James,

Thanks for the review and the question.

I did try modifying the check according to your suggestion, i.e. changing the condition like:
+ if (!rdpmc_supported && (pc->cap_user_rdpmc || pc->index)) {
However, with this change the tests regress on ARM, the results are:
--- start ---
User space counter reading 1
User space counter reading for PMU armv8_pmuv3_0
User space counter reading for PMU armv8_pmuv3_0 [Failed unexpected unsupported counter access 1]
---- end ----
126.4: User space counter disabling instructions : FAILED!
--- start ---
User space counter reading 0
User space counter reading for PMU armv8_pmuv3_0
User space counter reading for PMU armv8_pmuv3_0 [Failed unexpected unsupported counter access 1]
---- end ----
126.5: User space counter disabling cycles : FAILED!

Quoted 3 lines

On ARM, cap_user_rdpmc represents a PMU capability and may remain set even when user rdpmc access is disabled via sysfs/sysctl. In that case the mmap page reports: cap_user_rdpmc = 1 and index = 0, which is an

That's a slightly annoying difference to x86 then, which makes it a bit harder to test.

Quoted 7 lines

expected and valid state when rdpmc is disabled.
The intention of the existing check: if (!rdpmc_supported && pc->cap_user_rdpmc && pc->index) is only to catch the inconsistent case where rdpmc is reported as unsupported, but the mmap page still exposes a fully usable rdpmc state (i.e. both capability and a valid PMC index). This avoids flagging the ARM case above while still detecting real inconsistencies.

Quoted 6 lines

With the current logic, the tests now either pass or are skipped as expected on ARM, and there is no behavior change observed on x86_64.

Thanks,
Qiao

But "rdpmc_supported = pc->cap_user_rdpmc && pc->index" here:

   if (saved_user_read_state == USER_READ_UNKNOWN)
       rdpmc_supported = pc->cap_user_rdpmc && pc->index;

So I'm still not sure I get checking rdpmc_supported with those exact two things again.

In fact I looked more closely and the test is already setup to write to <pmu>/rdmpc to run the test twice with it both enabled and disabled in set_user_read(). We only return USER_READ_UNKNOWN in that function on perf_user_access platforms, but only because that function doesn't know the place to enable it is /proc/sys/kernel/perf_user_access.

Quoted 5 lines

So actually we should modify set_user_read() to try enabling in both places, and then the test can test enabled and disabled outcomes on Arm as well as x86. Now that we know there is this other file we can make the original intention of the test work with it, rather that just adding a skip and not testing anything.

Thanks for suggesting to modify set_user_read() so that it can try enabling rdpmc
both locations (PMU <pmu>/rdpmc and sysfs file /proc/sys/kernel/perf_user_access).

Before preparing the next version, I would like to confirm the expected direction.

My current understanding is that the intended behavior of the mmap rdpmc test is to actively toggle the user-space counter access (enable/disable) and verify both
outcomes, rather than skipping the test when rdpmc is unavailable. Based on this,
the next step would be to update set_user_read() so that it attempts to control rdpmc
through <pmu>/rdpmc first and falls back to /proc/sys/kernel/perf_user_access when
the PMU interface is not present.

Quoted 8 lines

Hi Ian,
Could you please confirm if this is the preferred approach for the next revision, or if we
should instead keep the current skip logic for platforms where neither interface can be
modified?

Thanks for the guidance.

Hi Qiao,

I'm not the gate keeper on how things should look but my goal in having the test, and extending its coverage, was that the implementation should match the description in perf_event.h:
https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/perf_event.h#n601
this doesn't cover the sysfs/procfs files but the man page does at least for sysfs. None of these sources cover the affinity/rseq issues with using rdpmc that if not handled lead to SIGILLs on x86 unless we land this kernel patch:
https://lore.kernel.org/linux-perf-users/[email protected]/
I'm not sure of the state of this on ARM where there is much greater consistency between performance counters, we may be pessimizing ARM code to work around this on x86.

My suggestion would be to make the test work, James happy, etc. and then send out updates to perf_event.h commands and the man page. Most likely the perf_event.h comments will receive good review and we can iterate from there, for example, the /proc/sys/kernel/perf_user_access slipped under my radar. My understanding from working in this area is that perf_event.h is supposed to describe how users use the rdpmc API and given the Linux syscall exception on the file, users can freely reimplement it (as the point of having a kernel API is people use it). The point of the test is to make sure the perf_event.h comment is honest. The man page has more space for more detail. As the man page doesn't cover /proc/sys/kernel/perf_user_access and perf_event.h covers neither the sysfs or procfs files, as well as not mentioning the SIGILL issue, I think there's quite a bit of work to do.

Thanks,
Ian

Quoted 72 lines

- Qiao

Quoted 69 lines
Quoted 35 lines · James Clark
Quoted 33 lines · Qiao Zhao
Quoted 29 lines

Some architectures control user-space rdpmc access via sysfs or sysctl, for example /proc/sys/kernel/perf_user_access on arm64/riscv or the rdpmc attribute under event_source devices on x86_64.

The mmap-based rdpmc tests currently assume rdpmc availability purely based on perf_event configuration, which leads to false failures when the system globally disables rdpmc access.

Introduce a helper to detect rdpmc availability via sysfs and skip user space counter reading tests when rdpmc is disabled by the system.

The check is only applied to USER_READ_ENABLED tests. User-read disabled tests continue to run unconditionally, as they validate perf_event mmap semantics independent of rdpmc availability.

Additionally, tighten the unsupported rdpmc check to require a valid counter index, avoiding false failures when cap_user_rdpmc is set but no counter is assigned.

v1->v2
- Check rdpmc availability via sysfs before running USER_READ_ENABLED tests
- Handle both perf_user_access and rdpmc sysfs interfaces
- Avoid false failures when cap_user_rdpmc is set but no counter index exists

Testing:
- arm64: rdpmc disabled by perf_user_access (tests correctly skipped)
- x86_64: no behavior change compared to v1

[...]

Quoted 9 lines
Quoted 9 lines

tools/perf/tests/mmap-basic.c static int test_stat_user_read(u64 event, enum user_read_state enabled) · line 351

Quoted 9 lines
               if (rdpmc_supported && (!pc->cap_user_rdpmc || !pc->index)) {
                       pr_err("User space counter reading for PMU %s [Failed unexpected supported counter access %d %d]\n",
                               pmu->name, pc->cap_user_rdpmc, pc->index);
tools/perf/tests/mmap-basic.c +1 -1
@@ -307,7 +348,7 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
                       goto cleanup;
               }
 
-             if (!rdpmc_supported && pc->cap_user_rdpmc) {
+             if (!rdpmc_supported && pc->cap_user_rdpmc && pc->index) {

This weakens the test on x86 though. If they behave differently you might want to just skip it on platforms with /proc/sys/kernel/perf_user_access and add another test for them that pc->index shouldn't be set if it's not supported, but ignore cap_user_rdpmc.

Quoted 15 lines
Quoted 14 lines

tools/perf/tests/mmap-basic.c static int test_stat_user_read(u64 event, enum user_read_state enabled) · line 354

Quoted 3 lines
tools/perf/tests/mmap-basic.c +0 -0
                       pr_err("User space counter reading for PMU %s [Failed unexpected unsupported counter access %d]\n",
                               pmu->name, pc->cap_user_rdpmc);
                       ret = TEST_FAIL;

Not sure I understand this test, shouldn't it be (pc->cap_user_rdpmc || pc->index)? As in, you don't expect either of them to be set if !rdpmc_supported. But then it's the exact opposite of the one above so I don't think it does anything.

Either way the test now passes or skips properly on Arm:

author
#7 lore raw

Hi Ian,

Thanks for the detailed explanation.

Based on your and James' suggestions, I will focus first on making the mmap rdpmc test behave consistently across platforms by ensuring the set_user_read() can actively control the user-space counter access through both <pmu>/rdpmc and /proc/sys/kernel/perf_user_access, so the test can exercise both enabled and disabled paths rather than relying on skip logic.

Once the functional behavior is aligned and the test reflects the intended rdpmc API semantics, I agree that the next useful step would be to review and propose updates to the perf_event.h comments and the man page so that the documented behavior better matches current kernel behavior.

I will prepare the next revision along these lines and send it for review.

- Qiao

Quoted 206 lines · Ian Rogers
Quoted 89 lines · Qiao Zhao
Quoted 62 lines · James Clark
Quoted 23 lines · Qiao Zhao

Hi James,

Thanks for the review and the question.

I did try modifying the check according to your suggestion, i.e. changing the condition like:
+ if (!rdpmc_supported && (pc->cap_user_rdpmc || pc->index)) {
However, with this change the tests regress on ARM, the results are:
--- start ---
User space counter reading 1
User space counter reading for PMU armv8_pmuv3_0
User space counter reading for PMU armv8_pmuv3_0 [Failed unexpected unsupported counter access 1]
---- end ----
126.4: User space counter disabling instructions : FAILED!
--- start ---
User space counter reading 0
User space counter reading for PMU armv8_pmuv3_0
User space counter reading for PMU armv8_pmuv3_0 [Failed unexpected unsupported counter access 1]
---- end ----
126.5: User space counter disabling cycles : FAILED!

Quoted 3 lines

On ARM, cap_user_rdpmc represents a PMU capability and may remain set even when user rdpmc access is disabled via sysfs/sysctl. In that case the mmap page reports: cap_user_rdpmc = 1 and index = 0, which is an

That's a slightly annoying difference to x86 then, which makes it a bit harder to test.

Quoted 7 lines

expected and valid state when rdpmc is disabled.
The intention of the existing check: if (!rdpmc_supported && pc->cap_user_rdpmc && pc->index) is only to catch the inconsistent case where rdpmc is reported as unsupported, but the mmap page still exposes a fully usable rdpmc state (i.e. both capability and a valid PMC index). This avoids flagging the ARM case above while still detecting real inconsistencies.

Quoted 6 lines

With the current logic, the tests now either pass or are skipped as expected on ARM, and there is no behavior change observed on x86_64.

Thanks,
Qiao

But "rdpmc_supported = pc->cap_user_rdpmc && pc->index" here:

   if (saved_user_read_state == USER_READ_UNKNOWN)
       rdpmc_supported = pc->cap_user_rdpmc && pc->index;

So I'm still not sure I get checking rdpmc_supported with those exact two things again.

In fact I looked more closely and the test is already setup to write to <pmu>/rdmpc to run the test twice with it both enabled and disabled in set_user_read(). We only return USER_READ_UNKNOWN in that function on perf_user_access platforms, but only because that function doesn't know the place to enable it is /proc/sys/kernel/perf_user_access.

Quoted 5 lines

So actually we should modify set_user_read() to try enabling in both places, and then the test can test enabled and disabled outcomes on Arm as well as x86. Now that we know there is this other file we can make the original intention of the test work with it, rather that just adding a skip and not testing anything.

Thanks for suggesting to modify set_user_read() so that it can try enabling rdpmc
both locations (PMU <pmu>/rdpmc and sysfs file /proc/sys/kernel/perf_user_access).

Before preparing the next version, I would like to confirm the expected direction.

My current understanding is that the intended behavior of the mmap rdpmc test is to actively toggle the user-space counter access (enable/disable) and verify both
outcomes, rather than skipping the test when rdpmc is unavailable. Based on this,
the next step would be to update set_user_read() so that it attempts to control rdpmc
through <pmu>/rdpmc first and falls back to /proc/sys/kernel/perf_user_access when
the PMU interface is not present.

Quoted 8 lines

Hi Ian,
Could you please confirm if this is the preferred approach for the next revision, or if we
should instead keep the current skip logic for platforms where neither interface can be
modified?

Thanks for the guidance.

Hi Qiao,

I'm not the gate keeper on how things should look but my goal in having the test, and extending its coverage, was that the implementation should match the description in perf_event.h:
https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/perf_event.h#n601
this doesn't cover the sysfs/procfs files but the man page does at least for sysfs. None of these sources cover the affinity/rseq issues with using rdpmc that if not handled lead to SIGILLs on x86 unless we land this kernel patch:
https://lore.kernel.org/linux-perf-users/[email protected]/
I'm not sure of the state of this on ARM where there is much greater consistency between performance counters, we may be pessimizing ARM code to work around this on x86.

My suggestion would be to make the test work, James happy, etc. and then send out updates to perf_event.h commands and the man page. Most likely the perf_event.h comments will receive good review and we can iterate from there, for example, the /proc/sys/kernel/perf_user_access slipped under my radar. My understanding from working in this area is that perf_event.h is supposed to describe how users use the rdpmc API and given the Linux syscall exception on the file, users can freely reimplement it (as the point of having a kernel API is people use it). The point of the test is to make sure the perf_event.h comment is honest. The man page has more space for more detail. As the man page doesn't cover /proc/sys/kernel/perf_user_access and perf_event.h covers neither the sysfs or procfs files, as well as not mentioning the SIGILL issue, I think there's quite a bit of work to do.

Thanks,
Ian

Quoted 72 lines

- Qiao

Quoted 69 lines
Quoted 35 lines · James Clark
Quoted 33 lines · Qiao Zhao
Quoted 29 lines

Some architectures control user-space rdpmc access via sysfs or sysctl, for example /proc/sys/kernel/perf_user_access on arm64/riscv or the rdpmc attribute under event_source devices on x86_64.

The mmap-based rdpmc tests currently assume rdpmc availability purely based on perf_event configuration, which leads to false failures when the system globally disables rdpmc access.

Introduce a helper to detect rdpmc availability via sysfs and skip user space counter reading tests when rdpmc is disabled by the system.

The check is only applied to USER_READ_ENABLED tests. User-read disabled tests continue to run unconditionally, as they validate perf_event mmap semantics independent of rdpmc availability.

Additionally, tighten the unsupported rdpmc check to require a valid counter index, avoiding false failures when cap_user_rdpmc is set but no counter is assigned.

v1->v2
- Check rdpmc availability via sysfs before running USER_READ_ENABLED tests
- Handle both perf_user_access and rdpmc sysfs interfaces
- Avoid false failures when cap_user_rdpmc is set but no counter index exists

Testing:
- arm64: rdpmc disabled by perf_user_access (tests correctly skipped)
- x86_64: no behavior change compared to v1

[...]

Quoted 9 lines
Quoted 9 lines

tools/perf/tests/mmap-basic.c static int test_stat_user_read(u64 event, enum user_read_state enabled) · line 351

Quoted 9 lines
               if (rdpmc_supported && (!pc->cap_user_rdpmc || !pc->index)) {
                       pr_err("User space counter reading for PMU %s [Failed unexpected supported counter access %d %d]\n",
                               pmu->name, pc->cap_user_rdpmc, pc->index);
tools/perf/tests/mmap-basic.c +1 -1
@@ -307,7 +348,7 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
                       goto cleanup;
               }
 
-             if (!rdpmc_supported && pc->cap_user_rdpmc) {
+             if (!rdpmc_supported && pc->cap_user_rdpmc && pc->index) {

This weakens the test on x86 though. If they behave differently you might want to just skip it on platforms with /proc/sys/kernel/perf_user_access and add another test for them that pc->index shouldn't be set if it's not supported, but ignore cap_user_rdpmc.

Quoted 15 lines
Quoted 14 lines

tools/perf/tests/mmap-basic.c static int test_stat_user_read(u64 event, enum user_read_state enabled) · line 354

Quoted 3 lines
tools/perf/tests/mmap-basic.c +0 -0
                       pr_err("User space counter reading for PMU %s [Failed unexpected unsupported counter access %d]\n",
                               pmu->name, pc->cap_user_rdpmc);
                       ret = TEST_FAIL;

Not sure I understand this test, shouldn't it be (pc->cap_user_rdpmc || pc->index)? As in, you don't expect either of them to be set if !rdpmc_supported. But then it's the exact opposite of the one above so I don't think it does anything.

Either way the test now passes or skips properly on Arm: