Skip to content

Commit cd84351

Browse files
ubizjakIngo Molnar
authored andcommitted
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. Signed-off-by: Uros Bizjak <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 9d35113 commit cd84351

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

arch/x86/events/amd/core.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,9 @@ static void __amd_put_nb_event_constraints(struct cpu_hw_events *cpuc,
433433
* when we come here
434434
*/
435435
for (i = 0; i < x86_pmu.num_counters; i++) {
436-
if (cmpxchg(nb->owners + i, event, NULL) == event)
436+
struct perf_event *tmp = event;
437+
438+
if (try_cmpxchg(nb->owners + i, &tmp, NULL))
437439
break;
438440
}
439441
}

arch/x86/events/amd/uncore.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ static int amd_uncore_add(struct perf_event *event, int flags)
162162
/* if not, take the first available counter */
163163
hwc->idx = -1;
164164
for (i = 0; i < pmu->num_counters; i++) {
165-
if (cmpxchg(&ctx->events[i], NULL, event) == NULL) {
165+
struct perf_event *tmp = NULL;
166+
167+
if (try_cmpxchg(&ctx->events[i], &tmp, event)) {
166168
hwc->idx = i;
167169
break;
168170
}
@@ -196,7 +198,9 @@ static void amd_uncore_del(struct perf_event *event, int flags)
196198
event->pmu->stop(event, PERF_EF_UPDATE);
197199

198200
for (i = 0; i < pmu->num_counters; i++) {
199-
if (cmpxchg(&ctx->events[i], event, NULL) == event)
201+
struct perf_event *tmp = event;
202+
203+
if (try_cmpxchg(&ctx->events[i], &tmp, NULL))
200204
break;
201205
}
202206

0 commit comments

Comments
 (0)