Skip to contentSkip to conversation

perf tests: mmap-basic: fix user rdpmc detection logic

PATCH RESEND In review Qiao Zhao 2 participants 3 messages Mar 30 – Apr 1 perf events

lore
Share & tools
lore

Qiao Zhao

author
#0 lore raw

PATCH RESEND v3 1/1 perf tests: mmap-basic: fix user rdpmc detection logic

The mmap-basic test incorrectly determined rdpmc availability in several environments, leading to unexpected failures on arm64 and other architectures.

Previously the rdpmc capability decision lived in test_stat_user_read(), which caused inconsistent behaviour when:
- perf_user_access sysctl state is unknown
- architectures expose cap_user_rdpmc differently
- arm64 platforms where rdpmc semantics differ from x86

As suggested during review, move the rdpmc capability decision into set_user_read() so that the user_read state and expected behavior are decided in a single place.

Changes in v3:
- Moves perf_user_access handling into set_user_read()
- Simplify test_stat_user_read() expectation logic
- Use unified rdpmc_supported calculation
- Handle USER_READ_UNKNOWN consistently

v2:
https://lore.kernel.org/linux-perf-users/[email protected]/

v1:
https://lore.kernel.org/linux-perf-users/[email protected]/

Note:
A potential cleanup around perf_event.h mentioned during review is intentionally deferred and will be handled in a follow-up change to keep this fix minimal and focused.

Tested on:
- ARM64 (armv8_pmuv3): all mmap-basic user-space counter tests pass
- X86(include hybrid): all mmap-basic tests pass
- IBM Power9: all mmap-basic tests pass

---
 tools/perf/tests/mmap-basic.c | 55 +++++++++++++++++++++++++----------
 1 file changed, 39 insertions(+), 16 deletions(-)
tools/perf/tests/mmap-basic.c +39 -16
index 3313c236104e..3633e7c87dc1 100644
@@ -185,38 +185,57 @@ static enum user_read_state set_user_read(struct perf_pmu *pmu, enum user_read_s
 {
 	char buf[2] = {0, '\n'};
 	ssize_t len;
-	int events_fd, rdpmc_fd;
+	int events_fd, fd;
 	enum user_read_state old_user_read = USER_READ_UNKNOWN;
 
 	if (enabled == USER_READ_UNKNOWN)
 		return USER_READ_UNKNOWN;
 
+	// Try the PMU rdpmc sysfs interface or similar
 	events_fd = perf_pmu__event_source_devices_fd();
-	if (events_fd < 0)
-		return USER_READ_UNKNOWN;
-
-	rdpmc_fd = perf_pmu__pathname_fd(events_fd, pmu->name, "rdpmc", O_RDWR);
-	if (rdpmc_fd < 0) {
+	if (events_fd >= 0) {
+		fd = perf_pmu__pathname_fd(events_fd, pmu->name, "rdpmc", O_RDWR);
+		if (fd >= 0) {
+			len = read(fd, buf, sizeof(buf));
+			if (len == sizeof(buf))
+				old_user_read = (buf[0] == '1') ?
+					USER_READ_ENABLED :
+					USER_READ_DISABLED;
+
+			if (enabled != old_user_read) {
+				buf[0] = (enabled == USER_READ_ENABLED) ? '1' : '0';
+				len = write(fd, buf, sizeof(buf));
+				if (len != sizeof(buf))
+					pr_debug("%s write failed\n", __func__);
+			}
+			close(fd);
+			close(events_fd);
+			return old_user_read;
+		}
 		close(events_fd);
-		return USER_READ_UNKNOWN;
 	}
 
-	len = read(rdpmc_fd, buf, sizeof(buf));
-	if (len != sizeof(buf))
-		pr_debug("%s read failed\n", __func__);
+	// Fallback: perf_user_access interface (arm64, riscv, or similar)
+	fd = open("/proc/sys/kernel/perf_user_access", O_RDWR);
+	if (fd < 0)
+		return USER_READ_UNKNOWN;
 
 	// Note, on Intel hybrid disabling on 1 PMU will implicitly disable on
 	// all the core PMUs.
-	old_user_read = (buf[0] == '1') ? USER_READ_ENABLED : USER_READ_DISABLED;
+	len = read(fd, buf, sizeof(buf));
+	if (len == sizeof(buf))
+		old_user_read = (buf[0] == '1') ?
+			USER_READ_ENABLED :
+			USER_READ_DISABLED;
 
 	if (enabled != old_user_read) {
 		buf[0] = (enabled == USER_READ_ENABLED) ? '1' : '0';
-		len = write(rdpmc_fd, buf, sizeof(buf));
+		len = write(fd, buf, sizeof(buf));
 		if (len != sizeof(buf))
 			pr_debug("%s write failed\n", __func__);
 	}
-	close(rdpmc_fd);
-	close(events_fd);
+
+	close(fd);
 	return old_user_read;
 }
 
@@ -295,12 +314,16 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
 			goto cleanup;
 		}
 
+#if defined(__aarch64__) || defined(__riscv)
+		rdpmc_supported = pc->cap_user_rdpmc;
+#else
 		if (saved_user_read_state == USER_READ_UNKNOWN)
-			rdpmc_supported = pc->cap_user_rdpmc && pc->index;
+			rdpmc_supported = pc->cap_user_rdpmc;
 		else
 			rdpmc_supported = (enabled == USER_READ_ENABLED);
+#endif
 
-		if (rdpmc_supported && (!pc->cap_user_rdpmc || !pc->index)) {
+		if (rdpmc_supported && !pc->cap_user_rdpmc) {
 			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);
 			ret = TEST_FAIL;
Signature · 1 line
2.49.0
reviewer
#1 lore raw
Quoted 70 lines · Qiao Zhao

The mmap-basic test incorrectly determined rdpmc availability in several environments, leading to unexpected failures on arm64 and other architectures.

Previously the rdpmc capability decision lived in test_stat_user_read(), which caused inconsistent behaviour when:
- perf_user_access sysctl state is unknown
- architectures expose cap_user_rdpmc differently
- arm64 platforms where rdpmc semantics differ from x86

As suggested during review, move the rdpmc capability decision into set_user_read() so that the user_read state and expected behavior are decided in a single place.

Changes in v3:
- Moves perf_user_access handling into set_user_read()
- Simplify test_stat_user_read() expectation logic
- Use unified rdpmc_supported calculation
- Handle USER_READ_UNKNOWN consistently

v2:
https://lore.kernel.org/linux-perf-users/[email protected]/

v1:
https://lore.kernel.org/linux-perf-users/[email protected]/

Note:
A potential cleanup around perf_event.h mentioned during review is intentionally deferred and will be handled in a follow-up change to keep this fix minimal and focused.

Tested on:
- ARM64 (armv8_pmuv3): all mmap-basic user-space counter tests pass
- X86(include hybrid): all mmap-basic tests pass
- IBM Power9: all mmap-basic tests pass

---
  tools/perf/tests/mmap-basic.c | 55 +++++++++++++++++++++++++----------
  1 file changed, 39 insertions(+), 16 deletions(-)
tools/perf/tests/mmap-basic.c +9 -6
index 3313c236104e..3633e7c87dc1 100644
@@ -185,38 +185,57 @@ static enum user_read_state set_user_read(struct perf_pmu *pmu, enum user_read_s
  {
  	char buf[2] = {0, '\n'};
  	ssize_t len;
-	int events_fd, rdpmc_fd;
+	int events_fd, fd;
  	enum user_read_state old_user_read = USER_READ_UNKNOWN;
 
  	if (enabled == USER_READ_UNKNOWN)
  		return USER_READ_UNKNOWN;
 
+	// Try the PMU rdpmc sysfs interface or similar
  	events_fd = perf_pmu__event_source_devices_fd();
-	if (events_fd < 0)
-		return USER_READ_UNKNOWN;
-
-	rdpmc_fd = perf_pmu__pathname_fd(events_fd, pmu->name, "rdpmc", O_RDWR);
-	if (rdpmc_fd < 0) {
+	if (events_fd >= 0) {
+		fd = perf_pmu__pathname_fd(events_fd, pmu->name, "rdpmc", O_RDWR);
+		if (fd >= 0) {
+			len = read(fd, buf, sizeof(buf));
+			if (len == sizeof(buf))
+				old_user_read = (buf[0] == '1') ?
+					USER_READ_ENABLED :

tools/perf/tests/mmap-basic.c static enum user_read_state set_user_read(struct perf_pmu *pmu, enum user_read_s · line 212

Quoted 10 lines
tools/perf/tests/mmap-basic.c +10 -0
+					USER_READ_DISABLED;
+
+			if (enabled != old_user_read) {
+				buf[0] = (enabled == USER_READ_ENABLED) ? '1' : '0';
+				len = write(fd, buf, sizeof(buf));
+				if (len != sizeof(buf))
+					pr_debug("%s write failed\n", __func__);
+			}
+			close(fd);
+			close(events_fd);

Hi Qiao,

This block that writes to the file is duplicated with the fallback below. I think a function for this would be better.

Quoted 6 lines
tools/perf/tests/mmap-basic.c +2 -1
+			return old_user_read;
+		}
  		close(events_fd);
-		return USER_READ_UNKNOWN;
  	}
 

tools/perf/tests/mmap-basic.c static enum user_read_state set_user_read(struct perf_pmu *pmu, enum user_read_s · line 224

Quoted 10 lines
tools/perf/tests/mmap-basic.c +4 -3
-	len = read(rdpmc_fd, buf, sizeof(buf));
-	if (len != sizeof(buf))
-		pr_debug("%s read failed\n", __func__);
+	// Fallback: perf_user_access interface (arm64, riscv, or similar)
+	fd = open("/proc/sys/kernel/perf_user_access", O_RDWR);
+	if (fd < 0)
+		return USER_READ_UNKNOWN;
 
  	// Note, on Intel hybrid disabling on 1 PMU will implicitly disable on
  	// all the core PMUs.

This comment isn't in the right place anymore because the fallback never works for Intel.

Quoted 27 lines
tools/perf/tests/mmap-basic.c +10 -4
-	old_user_read = (buf[0] == '1') ? USER_READ_ENABLED : USER_READ_DISABLED;
+	len = read(fd, buf, sizeof(buf));
+	if (len == sizeof(buf))
+		old_user_read = (buf[0] == '1') ?
+			USER_READ_ENABLED :
+			USER_READ_DISABLED;
 
  	if (enabled != old_user_read) {
  		buf[0] = (enabled == USER_READ_ENABLED) ? '1' : '0';
-		len = write(rdpmc_fd, buf, sizeof(buf));
+		len = write(fd, buf, sizeof(buf));
  		if (len != sizeof(buf))
  			pr_debug("%s write failed\n", __func__);
  	}
-	close(rdpmc_fd);
-	close(events_fd);
+
+	close(fd);
  	return old_user_read;
  }
 
@@ -295,12 +314,16 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
  			goto cleanup;
  		}
 
+#if defined(__aarch64__) || defined(__riscv)
+		rdpmc_supported = pc->cap_user_rdpmc;

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

Quoted 10 lines
tools/perf/tests/mmap-basic.c +4 -2
+#else
  		if (saved_user_read_state == USER_READ_UNKNOWN)
-			rdpmc_supported = pc->cap_user_rdpmc && pc->index;
+			rdpmc_supported = pc->cap_user_rdpmc;
  		else
  			rdpmc_supported = (enabled == USER_READ_ENABLED);
+#endif
 
-		if (rdpmc_supported && (!pc->cap_user_rdpmc || !pc->index)) {
+		if (rdpmc_supported && !pc->cap_user_rdpmc) {

I still don't really get what this is testing. On Arm we do "rdpmc_supported = pc->cap_user_rdpmc" and then test that those variables are the same value? And removing the pc->index check has affected x86 but this fix is only for Arm and RISC-V.

pc->index needs to be checked on Arm as well for the test to make sense. When user reading is enabled "cap_user_rdpmc && pc->index" is true. So when we've enabled user reading that should always be true and it should always be false in the disabled test. That doesn't seem that different to x86.

Quoted 3 lines
tools/perf/tests/mmap-basic.c +0 -0
  			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);
  			ret = TEST_FAIL;
author
#2 lore raw

Hi James,

Thanks for the review.

You're right that removing the pc->index check makes the test lose its original meaning, and the current arm/risc-v logic becomes inconsistent.

My initial intention was to handle cases where pc->index is not set on arm, but I agree that this should not come at the cost of breaking the test semantics.

From my understanding, this test is meant to validate the correctness of user space counter reading via the mmap interface, not just the capability flag itself.
Specifically, this test covers multiple scenarios:
1. When user space reading is enabled:
- The counter should be readable from user space.
- This requires both the capability (cap_user_rdpmc) and a valid event (pc->index).
2. When user space reading is disabled:
- User space reading should not work.
- Either cap_user_rdpmc is cleared or the event is not accessible.
3. The instruction/cycle tests (enabled/disabled) verify that toggling user access actually changes the observalbe behavior from user space.
So the key point is that the test is not just checking cap_user_rdpmc, but whether user space reading is functionally working as expected.

I'll rework the patch to restore the pc->index check and keep the test logic consistent across architectures. If needed, I'll handle any arm-specific issues separately.
Also, I'll refactor the duplicated perf_user_access write logic into a helper function and fix the misplaced Intel comment.

Thanks again for pointing this out.

Thanks,
Qiao

Quoted 163 lines · James Clark
Quoted 70 lines · Qiao Zhao

The mmap-basic test incorrectly determined rdpmc availability in several environments, leading to unexpected failures on arm64 and other architectures.

Previously the rdpmc capability decision lived in test_stat_user_read(), which caused inconsistent behaviour when:
- perf_user_access sysctl state is unknown
- architectures expose cap_user_rdpmc differently
- arm64 platforms where rdpmc semantics differ from x86

As suggested during review, move the rdpmc capability decision into set_user_read() so that the user_read state and expected behavior are decided in a single place.

Changes in v3:
- Moves perf_user_access handling into set_user_read()
- Simplify test_stat_user_read() expectation logic
- Use unified rdpmc_supported calculation
- Handle USER_READ_UNKNOWN consistently

v2:
https://lore.kernel.org/linux-perf-users/[email protected]/

v1:
https://lore.kernel.org/linux-perf-users/[email protected]/

Note:
A potential cleanup around perf_event.h mentioned during review is intentionally deferred and will be handled in a follow-up change to keep this fix minimal and focused.

Tested on:
- ARM64 (armv8_pmuv3): all mmap-basic user-space counter tests pass
- X86(include hybrid): all mmap-basic tests pass
- IBM Power9: all mmap-basic tests pass

---
  tools/perf/tests/mmap-basic.c | 55 +++++++++++++++++++++++++----------
  1 file changed, 39 insertions(+), 16 deletions(-)
tools/perf/tests/mmap-basic.c +9 -6
index 3313c236104e..3633e7c87dc1 100644
@@ -185,38 +185,57 @@ static enum user_read_state set_user_read(struct perf_pmu *pmu, enum user_read_s
  {
      char buf[2] = {0, '\n'};
      ssize_t len;
-     int events_fd, rdpmc_fd;
+     int events_fd, fd;
      enum user_read_state old_user_read = USER_READ_UNKNOWN;
 
      if (enabled == USER_READ_UNKNOWN)
              return USER_READ_UNKNOWN;
 
+     // Try the PMU rdpmc sysfs interface or similar
      events_fd = perf_pmu__event_source_devices_fd();
-     if (events_fd < 0)
-             return USER_READ_UNKNOWN;
-
-     rdpmc_fd = perf_pmu__pathname_fd(events_fd, pmu->name, "rdpmc", O_RDWR);
-     if (rdpmc_fd < 0) {
+     if (events_fd >= 0) {
+             fd = perf_pmu__pathname_fd(events_fd, pmu->name, "rdpmc", O_RDWR);
+             if (fd >= 0) {
+                     len = read(fd, buf, sizeof(buf));
+                     if (len == sizeof(buf))
+                             old_user_read = (buf[0] == '1') ?
+                                     USER_READ_ENABLED :

tools/perf/tests/mmap-basic.c static enum user_read_state set_user_read(struct perf_pmu *pmu, enum user_read_s · line 212

Quoted 10 lines
tools/perf/tests/mmap-basic.c +10 -0
+                                     USER_READ_DISABLED;
+
+                     if (enabled != old_user_read) {
+                             buf[0] = (enabled == USER_READ_ENABLED) ? '1' : '0';
+                             len = write(fd, buf, sizeof(buf));
+                             if (len != sizeof(buf))
+                                     pr_debug("%s write failed\n", __func__);
+                     }
+                     close(fd);
+                     close(events_fd);

Hi Qiao,

This block that writes to the file is duplicated with the fallback below. I think a function for this would be better.

Quoted 6 lines
tools/perf/tests/mmap-basic.c +2 -1
+                     return old_user_read;
+             }
              close(events_fd);
-             return USER_READ_UNKNOWN;
      }
 

tools/perf/tests/mmap-basic.c static enum user_read_state set_user_read(struct perf_pmu *pmu, enum user_read_s · line 224

Quoted 10 lines
tools/perf/tests/mmap-basic.c +4 -3
-     len = read(rdpmc_fd, buf, sizeof(buf));
-     if (len != sizeof(buf))
-             pr_debug("%s read failed\n", __func__);
+     // Fallback: perf_user_access interface (arm64, riscv, or similar)
+     fd = open("/proc/sys/kernel/perf_user_access", O_RDWR);
+     if (fd < 0)
+             return USER_READ_UNKNOWN;
 
      // Note, on Intel hybrid disabling on 1 PMU will implicitly disable on
      // all the core PMUs.

This comment isn't in the right place anymore because the fallback never works for Intel.

Quoted 27 lines
tools/perf/tests/mmap-basic.c +10 -4
-     old_user_read = (buf[0] == '1') ? USER_READ_ENABLED : USER_READ_DISABLED;
+     len = read(fd, buf, sizeof(buf));
+     if (len == sizeof(buf))
+             old_user_read = (buf[0] == '1') ?
+                     USER_READ_ENABLED :
+                     USER_READ_DISABLED;
 
      if (enabled != old_user_read) {
              buf[0] = (enabled == USER_READ_ENABLED) ? '1' : '0';
-             len = write(rdpmc_fd, buf, sizeof(buf));
+             len = write(fd, buf, sizeof(buf));
              if (len != sizeof(buf))
                      pr_debug("%s write failed\n", __func__);
      }
-     close(rdpmc_fd);
-     close(events_fd);
+
+     close(fd);
      return old_user_read;
  }
 
@@ -295,12 +314,16 @@ static int test_stat_user_read(u64 event, enum user_read_state enabled)
                      goto cleanup;
              }
 
+#if defined(__aarch64__) || defined(__riscv)
+             rdpmc_supported = pc->cap_user_rdpmc;

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

Quoted 10 lines
tools/perf/tests/mmap-basic.c +4 -2
+#else
              if (saved_user_read_state == USER_READ_UNKNOWN)
-                     rdpmc_supported = pc->cap_user_rdpmc && pc->index;
+                     rdpmc_supported = pc->cap_user_rdpmc;
              else
                      rdpmc_supported = (enabled == USER_READ_ENABLED);
+#endif
 
-             if (rdpmc_supported && (!pc->cap_user_rdpmc || !pc->index)) {
+             if (rdpmc_supported && !pc->cap_user_rdpmc) {

I still don't really get what this is testing. On Arm we do "rdpmc_supported = pc->cap_user_rdpmc" and then test that those variables are the same value? And removing the pc->index check has affected x86 but this fix is only for Arm and RISC-V.

pc->index needs to be checked on Arm as well for the test to make sense. When user reading is enabled "cap_user_rdpmc && pc->index" is true. So when we've enabled user reading that should always be true and it should always be false in the disabled test. That doesn't seem that different to x86.

Quoted 3 lines
tools/perf/tests/mmap-basic.c +0 -0
                      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);
                      ret = TEST_FAIL;