Skip to content

Commit caf8b76

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
perf/core: Simplify perf_init_event()
Use the <linux/cleanup.h> guard() and scoped_guard() infrastructure to simplify the control flow. Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Reviewed-by: Ravi Bangoria <[email protected]> Cc: Thomas Gleixner <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 6c8b0b8 commit caf8b76

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

kernel/events/core.c

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12101,10 +12101,10 @@ static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
1210112101
static struct pmu *perf_init_event(struct perf_event *event)
1210212102
{
1210312103
bool extended_type = false;
12104-
int idx, type, ret;
1210512104
struct pmu *pmu;
12105+
int type, ret;
1210612106

12107-
idx = srcu_read_lock(&pmus_srcu);
12107+
guard(srcu)(&pmus_srcu);
1210812108

1210912109
/*
1211012110
* Save original type before calling pmu->event_init() since certain
@@ -12117,7 +12117,7 @@ static struct pmu *perf_init_event(struct perf_event *event)
1211712117
pmu = event->parent->pmu;
1211812118
ret = perf_try_init_event(pmu, event);
1211912119
if (!ret)
12120-
goto unlock;
12120+
return pmu;
1212112121
}
1212212122

1212312123
/*
@@ -12136,13 +12136,12 @@ static struct pmu *perf_init_event(struct perf_event *event)
1213612136
}
1213712137

1213812138
again:
12139-
rcu_read_lock();
12140-
pmu = idr_find(&pmu_idr, type);
12141-
rcu_read_unlock();
12139+
scoped_guard (rcu)
12140+
pmu = idr_find(&pmu_idr, type);
1214212141
if (pmu) {
1214312142
if (event->attr.type != type && type != PERF_TYPE_RAW &&
1214412143
!(pmu->capabilities & PERF_PMU_CAP_EXTENDED_HW_TYPE))
12145-
goto fail;
12144+
return ERR_PTR(-ENOENT);
1214612145

1214712146
ret = perf_try_init_event(pmu, event);
1214812147
if (ret == -ENOENT && event->attr.type != type && !extended_type) {
@@ -12151,27 +12150,21 @@ static struct pmu *perf_init_event(struct perf_event *event)
1215112150
}
1215212151

1215312152
if (ret)
12154-
pmu = ERR_PTR(ret);
12153+
return ERR_PTR(ret);
1215512154

12156-
goto unlock;
12155+
return pmu;
1215712156
}
1215812157

1215912158
list_for_each_entry_rcu(pmu, &pmus, entry, lockdep_is_held(&pmus_srcu)) {
1216012159
ret = perf_try_init_event(pmu, event);
1216112160
if (!ret)
12162-
goto unlock;
12161+
return pmu;
1216312162

12164-
if (ret != -ENOENT) {
12165-
pmu = ERR_PTR(ret);
12166-
goto unlock;
12167-
}
12163+
if (ret != -ENOENT)
12164+
return ERR_PTR(ret);
1216812165
}
12169-
fail:
12170-
pmu = ERR_PTR(-ENOENT);
12171-
unlock:
12172-
srcu_read_unlock(&pmus_srcu, idx);
1217312166

12174-
return pmu;
12167+
return ERR_PTR(-ENOENT);
1217512168
}
1217612169

1217712170
static void attach_sb_event(struct perf_event *event)

0 commit comments

Comments
 (0)