From: Peter Zijlstra <peterz@infradead.org>
To: mingo@kernel.org
Cc: linux-kernel@vger.kernel.org, peterz@infradead.org,
acme@kernel.org, mark.rutland@arm.com,
alexander.shishkin@linux.intel.com, jolsa@kernel.org,
namhyung@kernel.org, irogers@google.com, adrian.hunter@intel.com
Subject: [PATCH 02/13] perf: Simplify perf_pmu_register() error path
Date: Thu, 02 Nov 2023 16:09:21 +0100 [thread overview]
Message-ID: <20231102152017.957609849@infradead.org> (raw)
In-Reply-To: 20231102150919.719936610@infradead.org
The error path of perf_pmu_register() is of course very similar to a
subset of perf_pmu_unregister(). Extract this common part in
perf_pmu_free() and simplify things.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
kernel/events/core.c | 51 ++++++++++++++++++++++++---------------------------
1 file changed, 24 insertions(+), 27 deletions(-)
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -11426,20 +11426,35 @@ static int pmu_dev_alloc(struct pmu *pmu
static struct lock_class_key cpuctx_mutex;
static struct lock_class_key cpuctx_lock;
+static void perf_pmu_free(struct pmu *pmu)
+{
+ free_percpu(pmu->pmu_disable_count);
+ if (pmu->type >= 0)
+ idr_remove(&pmu_idr, pmu->type);
+ if (pmu_bus_running && pmu->dev && pmu->dev != PMU_NULL_DEV) {
+ if (pmu->nr_addr_filters)
+ device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
+ device_del(pmu->dev);
+ put_device(pmu->dev);
+ }
+ free_pmu_context(pmu);
+}
+
int perf_pmu_register(struct pmu *pmu, const char *name, int type)
{
int cpu, ret, max = PERF_TYPE_MAX;
+ pmu->type = -1;
+
mutex_lock(&pmus_lock);
ret = -ENOMEM;
pmu->pmu_disable_count = alloc_percpu(int);
if (!pmu->pmu_disable_count)
goto unlock;
- pmu->type = -1;
if (WARN_ONCE(!name, "Can not register anonymous pmu.\n")) {
ret = -EINVAL;
- goto free_pdc;
+ goto free;
}
pmu->name = name;
@@ -11449,23 +11464,22 @@ int perf_pmu_register(struct pmu *pmu, c
ret = idr_alloc(&pmu_idr, pmu, max, 0, GFP_KERNEL);
if (ret < 0)
- goto free_pdc;
+ goto free;
WARN_ON(type >= 0 && ret != type);
- type = ret;
- pmu->type = type;
+ pmu->type = ret;
if (pmu_bus_running && !pmu->dev) {
ret = pmu_dev_alloc(pmu);
if (ret)
- goto free_idr;
+ goto free;
}
ret = -ENOMEM;
pmu->cpu_pmu_context = alloc_percpu(struct perf_cpu_pmu_context);
if (!pmu->cpu_pmu_context)
- goto free_dev;
+ goto free;
for_each_possible_cpu(cpu) {
struct perf_cpu_pmu_context *cpc;
@@ -11511,17 +11525,8 @@ int perf_pmu_register(struct pmu *pmu, c
return ret;
-free_dev:
- if (pmu->dev && pmu->dev != PMU_NULL_DEV) {
- device_del(pmu->dev);
- put_device(pmu->dev);
- }
-
-free_idr:
- idr_remove(&pmu_idr, pmu->type);
-
-free_pdc:
- free_percpu(pmu->pmu_disable_count);
+free:
+ perf_pmu_free(pmu);
goto unlock;
}
EXPORT_SYMBOL_GPL(perf_pmu_register);
@@ -11538,15 +11543,7 @@ void perf_pmu_unregister(struct pmu *pmu
synchronize_srcu(&pmus_srcu);
synchronize_rcu();
- free_percpu(pmu->pmu_disable_count);
- idr_remove(&pmu_idr, pmu->type);
- if (pmu_bus_running && pmu->dev && pmu->dev != PMU_NULL_DEV) {
- if (pmu->nr_addr_filters)
- device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
- device_del(pmu->dev);
- put_device(pmu->dev);
- }
- free_pmu_context(pmu);
+ perf_pmu_free(pmu);
mutex_unlock(&pmus_lock);
}
EXPORT_SYMBOL_GPL(perf_pmu_unregister);
next prev parent reply other threads:[~2023-11-02 15:33 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-02 15:09 [PATCH 00/13] perf: Employ SBRM to simplify error handling -- batch 1/3 Peter Zijlstra
2023-11-02 15:09 ` [PATCH 01/13] perf: Simplify perf_event_alloc() error path Peter Zijlstra
2023-11-03 12:38 ` Jiri Olsa
2023-11-03 19:50 ` Namhyung Kim
2023-11-15 9:58 ` Peter Zijlstra
2023-11-15 15:12 ` Namhyung Kim
2023-11-15 9:31 ` Peter Zijlstra
2023-11-02 15:09 ` Peter Zijlstra [this message]
2023-11-02 15:09 ` [PATCH 03/13] perf: Simplify perf_fget_light() Peter Zijlstra
2023-11-02 15:09 ` [PATCH 04/13] perf: Simplify event_function*() Peter Zijlstra
2023-11-02 15:09 ` [PATCH 05/13] perf: Simplify perf_cgroup_connect() Peter Zijlstra
2023-11-02 15:09 ` [PATCH 06/13] perf; Simplify event_sched_in() Peter Zijlstra
2023-11-02 15:09 ` [PATCH 07/13] perf: Simplify: __perf_install_in_context() Peter Zijlstra
2023-11-04 1:08 ` Namhyung Kim
2023-11-15 9:24 ` Peter Zijlstra
2023-11-02 15:09 ` [PATCH 08/13] perf: Simplify: *perf_event_{dis,en}able*() Peter Zijlstra
2023-11-02 15:09 ` [PATCH 09/13] perf: Simplify perf_event_modify_attr() Peter Zijlstra
2023-11-02 15:09 ` [PATCH 10/13] perf: Simplify perf_event_context_sched_in() Peter Zijlstra
2023-11-02 15:09 ` [PATCH 11/13] perf: Simplify perf_adjust_freq_unthr_context() Peter Zijlstra
2023-11-04 1:14 ` Namhyung Kim
2023-11-15 10:31 ` Peter Zijlstra
2023-11-02 15:09 ` [PATCH 12/13] perf: Simplify perf_event_*_on_exec() Peter Zijlstra
2023-11-02 15:09 ` [PATCH 13/13] perf: Simplify *perf_event_read*() Peter Zijlstra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231102152017.957609849@infradead.org \
--to=peterz@infradead.org \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=irogers@google.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@kernel.org \
--cc=namhyung@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox