[PATCH v4 10/40] dmaengine: idxd: optimize perfmon_assign_event()

Yury Norov
2024-06-20T17:57:37Z

The function searches used_mask for a set bit in a for-loop bit by
bit. Simplify it by using atomic find_and_set_bit(), and make a nice
one-liner.

Signed-off-by: Yury Norov <yury.norov@gmail.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Acked-by: Vinod Koul <vkoul@kernel.org>
Reviewed-by: Fenghua Yu <fenghua.yu@intel.com>
---
drivers/dma/idxd/perfmon.c | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/drivers/dma/idxd/perfmon.c b/drivers/dma/idxd/perfmon.c
index 5e94247e1ea7..063ee78fb132 100644
--- a/drivers/dma/idxd/perfmon.c
+++ b/drivers/dma/idxd/perfmon.c
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright(c) 2020 Intel Corporation. All rights rsvd. */

+#include <linux/find_atomic.h>
#include <linux/sched/task.h>
#include <linux/io-64-nonatomic-lo-hi.h>
#include "idxd.h"
@@ -134,13 +135,9 @@ static void perfmon_assign_hw_event(struct idxd_pmu *idxd_pmu,
static int perfmon_assign_event(struct idxd_pmu *idxd_pmu,
struct perf_event *event)
{
- int i;
-
- for (i = 0; i < IDXD_PMU_EVENT_MAX; i++)
- if (!test_and_set_bit(i, idxd_pmu->used_mask))
- return i;
+ int i = find_and_set_bit(idxd_pmu->used_mask, IDXD_PMU_EVENT_MAX);

- return -EINVAL;
+ return i < IDXD_PMU_EVENT_MAX ? i : -EINVAL;
}

/*
--
2.43.0