[tip: perf/core] perf/x86/amd/uncore: Free counter slot by index
From: tip-bot2 for Usama Arif
Date: Fri Sep 25 2026 - 06:50:52 EST
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 6350de8671b94afb7691d110f63bcda42f658a69
Gitweb: https://git.kernel.org/tip/6350de8671b94afb7691d110f63bcda42f658a69
Author: Usama Arif <usama.arif@xxxxxxxxx>
AuthorDate: Mon, 21 Sep 2026 08:30:15 -07:00
Committer: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
CommitterDate: Fri, 25 Sep 2026 12:45:55 +02:00
perf/x86/amd/uncore: Free counter slot by index
amd_uncore_del() scans ctx->events[] from slot zero and attempts a
compare-exchange until it finds the event.
amd_uncore_add() records the slot it claimed in event->hw.idx. Perf
calls ->del() only after a successful ->add(), and the driver never
moves an installed event between slots. The index therefore remains
valid until deletion.
Use the recorded index directly and retain the compare-exchange
ownership check. Warn if it fails, as that means the driver lost track
of the slot.
Deleting all events from a full PMU with N counters now requires N
compare-exchanges instead of N * (N + 1) / 2.
On a host running a production workload in the Meta fleet, amd_uncore_del()
was called 56,575 times per second from mux rotation.
Signed-off-by: Usama Arif <usama.arif@xxxxxxxxx>
Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
Reviewed-by: Sandipan Das <sandipan.das@xxxxxxx>
Link: https://patch.msgid.link/20260921153108.2713179-3-usama.arif@xxxxxxxxx
---
arch/x86/events/amd/uncore.c | 10 +++-------
1 file changed, 3 insertions(+), 7 deletions(-)
diff --git a/arch/x86/events/amd/uncore.c b/arch/x86/events/amd/uncore.c
index c1158f3..7aa5a5b 100644
--- a/arch/x86/events/amd/uncore.c
+++ b/arch/x86/events/amd/uncore.c
@@ -246,19 +246,15 @@ out:
static void amd_uncore_del(struct perf_event *event, int flags)
{
- int i;
struct amd_uncore_pmu *pmu = event_to_amd_uncore_pmu(event);
struct amd_uncore_ctx *ctx = *per_cpu_ptr(pmu->ctx, event->cpu);
struct hw_perf_event *hwc = &event->hw;
+ struct perf_event *old = event;
event->pmu->stop(event, PERF_EF_UPDATE);
- for (i = 0; i < pmu->num_counters; i++) {
- struct perf_event *tmp = event;
-
- if (try_cmpxchg(&ctx->events[i], &tmp, NULL))
- break;
- }
+ /* ->del() follows a successful ->add(), so hwc->idx owns this slot. */
+ WARN_ON_ONCE(!try_cmpxchg(&ctx->events[hwc->idx], &old, NULL));
hwc->idx = -1;
}