Skip to contentSkip to conversation

perf/x86/amd: Use try_cmpxchg() in events/amd/{un,}core.c

PATCH Applied Uros Bizjak 3 participants 3 messages Apr 25, 2024 – May 18, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf/x86/amd: Use try_cmpxchg() in events/amd/{un,}core.c

Replace this pattern in events/amd/{un,}core.c:

    cmpxchg(*ptr, old, new) == old

... with the simpler and faster:

try_cmpxchg(*ptr, &old, new)

The x86 CMPXCHG instruction returns success in the ZF flag, so this change saves a compare after the CMPXCHG.

No functional change intended.

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Adrian Hunter <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/events/amd/core.c   | 3 ++-
 arch/x86/events/amd/uncore.c | 6 ++++--
 2 files changed, 6 insertions(+), 3 deletions(-)
arch/x86/events/amd/core.c +2 -1
index 1fc4ce44e743..c1b8aeff7bc0 100644
@@ -433,7 +433,8 @@ static void __amd_put_nb_event_constraints(struct cpu_hw_events *cpuc,
 	 * when we come here
 	 */
 	for (i = 0; i < x86_pmu.num_counters; i++) {
-		if (cmpxchg(nb->owners + i, event, NULL) == event)
+		struct perf_event *tmp = event;
+		if (try_cmpxchg(nb->owners + i, &tmp, NULL))
 			break;
 	}
 }
arch/x86/events/amd/uncore.c +4 -2
index 4ccb8fa483e6..cac7c20d1aee 100644
@@ -162,7 +162,8 @@ static int amd_uncore_add(struct perf_event *event, int flags)
 	/* if not, take the first available counter */
 	hwc->idx = -1;
 	for (i = 0; i < pmu->num_counters; i++) {
-		if (cmpxchg(&ctx->events[i], NULL, event) == NULL) {
+		struct perf_event *tmp = NULL;
+		if (try_cmpxchg(&ctx->events[i], &tmp, event)) {
 			hwc->idx = i;
 			break;
 		}
@@ -196,7 +197,8 @@ static void amd_uncore_del(struct perf_event *event, int flags)
 	event->pmu->stop(event, PERF_EF_UPDATE);
 
 	for (i = 0; i < pmu->num_counters; i++) {
-		if (cmpxchg(&ctx->events[i], event, NULL) == event)
+		struct perf_event *tmp = event;
+		if (try_cmpxchg(&ctx->events[i], &tmp, NULL))
 			break;
 	}
 
Signature · 1 line
2.44.0
#1 lore raw
Quoted 61 lines · Uros Bizjak

Replace this pattern in events/amd/{un,}core.c:

    cmpxchg(*ptr, old, new) == old

... with the simpler and faster:

try_cmpxchg(*ptr, &old, new)

The x86 CMPXCHG instruction returns success in the ZF flag, so this change saves a compare after the CMPXCHG.

No functional change intended.

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Adrian Hunter <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/events/amd/core.c   | 3 ++-
 arch/x86/events/amd/uncore.c | 6 ++++--
 2 files changed, 6 insertions(+), 3 deletions(-)
arch/x86/events/amd/core.c +2 -1
index 1fc4ce44e743..c1b8aeff7bc0 100644
@@ -433,7 +433,8 @@ static void __amd_put_nb_event_constraints(struct cpu_hw_events *cpuc,
 	 * when we come here
 	 */
 	for (i = 0; i < x86_pmu.num_counters; i++) {
-		if (cmpxchg(nb->owners + i, event, NULL) == event)
+		struct perf_event *tmp = event;
+		if (try_cmpxchg(nb->owners + i, &tmp, NULL))
 			break;
 	}
 }
arch/x86/events/amd/uncore.c +2 -1
index 4ccb8fa483e6..cac7c20d1aee 100644
@@ -162,7 +162,8 @@ static int amd_uncore_add(struct perf_event *event, int flags)
 	/* if not, take the first available counter */
 	hwc->idx = -1;
 	for (i = 0; i < pmu->num_counters; i++) {
-		if (cmpxchg(&ctx->events[i], NULL, event) == NULL) {
+		struct perf_event *tmp = NULL;
+		if (try_cmpxchg(&ctx->events[i], &tmp, event)) {
 			hwc->idx = i;
 			break;
 		}

arch/x86/events/amd/uncore.c static void amd_uncore_del(struct perf_event *event, int flags) · line 203

Quoted 8 lines
arch/x86/events/amd/uncore.c +2 -1
@@ -196,7 +197,8 @@ static void amd_uncore_del(struct perf_event *event, int flags)
 	event->pmu->stop(event, PERF_EF_UPDATE);
 
 	for (i = 0; i < pmu->num_counters; i++) {
-		if (cmpxchg(&ctx->events[i], event, NULL) == event)
+		struct perf_event *tmp = event;
+		if (try_cmpxchg(&ctx->events[i], &tmp, NULL))
 			break;
 	}

Note that's a newline missing between local variable declaration block and the first statement after that. I've added all 3 to this patch (see below), but please keep an eye on this in future patches.

Thanks,

Ingo

================>

 arch/x86/events/amd/core.c   | 1 +
 arch/x86/events/amd/uncore.c | 2 ++
 2 files changed, 3 insertions(+)
arch/x86/events/amd/core.c +1 -0
index c1b8aeff7bc0..18bfe3451f3a 100644
@@ -434,6 +434,7 @@ static void __amd_put_nb_event_constraints(struct cpu_hw_events *cpuc,
 	 */
 	for (i = 0; i < x86_pmu.num_counters; i++) {
 		struct perf_event *tmp = event;
+
 		if (try_cmpxchg(nb->owners + i, &tmp, NULL))
 			break;
 	}
arch/x86/events/amd/uncore.c +2 -0
index cac7c20d1aee..0fafe233bba4 100644
@@ -163,6 +163,7 @@ static int amd_uncore_add(struct perf_event *event, int flags)
 	hwc->idx = -1;
 	for (i = 0; i < pmu->num_counters; i++) {
 		struct perf_event *tmp = NULL;
+
 		if (try_cmpxchg(&ctx->events[i], &tmp, event)) {
 			hwc->idx = i;
 			break;
@@ -198,6 +199,7 @@ static void amd_uncore_del(struct perf_event *event, int flags)
 
 	for (i = 0; i < pmu->num_counters; i++) {
 		struct perf_event *tmp = event;
+
 		if (try_cmpxchg(&ctx->events[i], &tmp, NULL))
 			break;
 	}
merge bot Applied
#2 lore raw
In reply to Uros Bizjak
The following commit has been merged into the perf/urgent branch of tip:

The following commit has been merged into the perf/urgent branch of tip:

Commit-ID:     cd84351c8c1baec86342d784feb884ace007d51c
Gitweb:        https://git.kernel.org/tip/cd84351c8c1baec86342d784feb884ace007d51c
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Thu, 25 Apr 2024 12:16:14 +02:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Sat, 18 May 2024 11:15:13 +02:00

perf/x86/amd: Use try_cmpxchg() in events/amd/{un,}core.c

Replace this pattern in events/amd/{un,}core.c:

    cmpxchg(*ptr, old, new) == old

... with the simpler and faster:

try_cmpxchg(*ptr, &old, new)

The x86 CMPXCHG instruction returns success in the ZF flag, so this change saves a compare after the CMPXCHG.

No functional change intended.

---
 arch/x86/events/amd/core.c   | 4 +++-
 arch/x86/events/amd/uncore.c | 8 ++++++--
 2 files changed, 9 insertions(+), 3 deletions(-)
arch/x86/events/amd/core.c +3 -1
index 1fc4ce4..18bfe34 100644
@@ -433,7 +433,9 @@ static void __amd_put_nb_event_constraints(struct cpu_hw_events *cpuc,
 	 * when we come here
 	 */
 	for (i = 0; i < x86_pmu.num_counters; i++) {
-		if (cmpxchg(nb->owners + i, event, NULL) == event)
+		struct perf_event *tmp = event;
+
+		if (try_cmpxchg(nb->owners + i, &tmp, NULL))
 			break;
 	}
 }
arch/x86/events/amd/uncore.c +6 -2
index 4ccb8fa..0fafe23 100644
@@ -162,7 +162,9 @@ static int amd_uncore_add(struct perf_event *event, int flags)
 	/* if not, take the first available counter */
 	hwc->idx = -1;
 	for (i = 0; i < pmu->num_counters; i++) {
-		if (cmpxchg(&ctx->events[i], NULL, event) == NULL) {
+		struct perf_event *tmp = NULL;
+
+		if (try_cmpxchg(&ctx->events[i], &tmp, event)) {
 			hwc->idx = i;
 			break;
 		}
@@ -196,7 +198,9 @@ static void amd_uncore_del(struct perf_event *event, int flags)
 	event->pmu->stop(event, PERF_EF_UPDATE);
 
 	for (i = 0; i < pmu->num_counters; i++) {
-		if (cmpxchg(&ctx->events[i], event, NULL) == event)
+		struct perf_event *tmp = event;
+
+		if (try_cmpxchg(&ctx->events[i], &tmp, NULL))
 			break;
 	}