[PATCH 31/34] drivers/perf: optimize m1_pmu_get_event_idx() by using find_bit() API

From: Yury Norov
Date: Sat Nov 18 2023 - 10:54:55 EST


The function searches used_mask for a bit in a for-loop bit by bit.
We can do it faster by using atomic find_and_set_bit().

The comment to the function says that it searches for the first free
counter, but obviously for_each_set_bit() searches for the first set
counter. The following test_and_set_bit() tries to enable already set
bit, which is weird.

This patch, by using find_and_set_bit(), fixes this automatically.

Fixes: a639027a1be1 ("drivers/perf: Add Apple icestorm/firestorm CPU PMU driver")
Signed-off-by: Yury Norov <yury.norov@xxxxxxxxx>
---
drivers/perf/apple_m1_cpu_pmu.c | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/drivers/perf/apple_m1_cpu_pmu.c b/drivers/perf/apple_m1_cpu_pmu.c
index cd2de44b61b9..2d50670ffb01 100644
--- a/drivers/perf/apple_m1_cpu_pmu.c
+++ b/drivers/perf/apple_m1_cpu_pmu.c
@@ -447,12 +447,8 @@ static int m1_pmu_get_event_idx(struct pmu_hw_events *cpuc,
* counting on the PMU at any given time, and by placing the
* most constraining events first.
*/
- for_each_set_bit(idx, &affinity, M1_PMU_NR_COUNTERS) {
- if (!test_and_set_bit(idx, cpuc->used_mask))
- return idx;
- }
-
- return -EAGAIN;
+ idx = find_and_set_bit(cpuc->used_mask, M1_PMU_NR_COUNTERS);
+ return idx < M1_PMU_NR_COUNTERS ? idx : -EAGAIN;
}

static void m1_pmu_clear_event_idx(struct pmu_hw_events *cpuc,
--
2.39.2