From: Lucas De Marchi <lucas.demarchi@intel.com>
To: linux-kernel@vger.kernel.org
Cc: dri-devel@lists.freedesktop.org,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Umesh Nerlige Ramappa <umesh.nerlige.ramappa@intel.com>,
Ian Rogers <irogers@google.com>,
Tvrtko Ursulin <tvrtko.ursulin@igalia.com>,
Lucas De Marchi <lucas.demarchi@intel.com>
Subject: [PATCH 4/5] perf/dummy_pmu: Tie pmu to device lifecycle
Date: Tue, 8 Oct 2024 13:35:00 -0500 [thread overview]
Message-ID: <20241008183501.1354695-5-lucas.demarchi@intel.com> (raw)
In-Reply-To: <20241008183501.1354695-1-lucas.demarchi@intel.com>
Allow to unregister the PMU from perf with active events. When driver is
being accessed perf keeps a reference that when released triggers the
device cleanup.
Signed-off-by: Lucas De Marchi <lucas.demarchi@intel.com>
---
kernel/events/dummy_pmu.c | 56 ++++++++++++++++++++++++++++-----------
1 file changed, 41 insertions(+), 15 deletions(-)
diff --git a/kernel/events/dummy_pmu.c b/kernel/events/dummy_pmu.c
index cdba3a831e4a..c07e111bff01 100644
--- a/kernel/events/dummy_pmu.c
+++ b/kernel/events/dummy_pmu.c
@@ -49,6 +49,11 @@ static struct dummy_device *pmu_to_device(struct dummy_pmu *pmu)
return container_of(pmu, struct dummy_device, pmu);
}
+static struct dummy_pmu *pmu_to_dummy(struct pmu *pmu)
+{
+ return container_of(pmu, struct dummy_pmu, base);
+}
+
static ssize_t dummy_pmu_events_sysfs_show(struct device *dev,
struct device_attribute *attr,
char *page)
@@ -92,18 +97,9 @@ static const struct attribute_group *attr_groups[] = {
NULL,
};
-static void dummy_pmu_event_destroy(struct perf_event *event)
-{
- struct dummy_pmu *pmu = event_to_pmu(event);
- struct dummy_device *d = pmu_to_device(pmu);
-
- kref_put(&d->refcount, device_release);
-}
-
static int dummy_pmu_event_init(struct perf_event *event)
{
struct dummy_pmu *pmu = event_to_pmu(event);
- struct dummy_device *d = pmu_to_device(pmu);
if (!pmu->registered)
return -ENODEV;
@@ -121,10 +117,6 @@ static int dummy_pmu_event_init(struct perf_event *event)
if (event->cpu < 0)
return -EINVAL;
- /* Event keeps a ref to maintain PMU allocated, even if it's unregistered */
- kref_get(&d->refcount);
- event->destroy = dummy_pmu_event_destroy;
-
return 0;
}
@@ -195,10 +187,29 @@ static void dummy_pmu_event_del(struct perf_event *event, int flags)
dummy_pmu_event_stop(event, PERF_EF_UPDATE);
}
+static struct pmu *dummy_pmu_get(struct pmu *pmu)
+{
+ struct dummy_device *d = pmu_to_device(pmu_to_dummy(pmu));
+
+ kref_get(&d->refcount);
+
+ return pmu;
+}
+
+static void dummy_pmu_put(struct pmu *pmu)
+{
+ struct dummy_device *d = pmu_to_device(pmu_to_dummy(pmu));
+
+ kref_put(&d->refcount, device_release);
+}
+
static int device_init(struct dummy_device *d)
{
int ret;
+ if (WARN_ONCE(d->pmu.name, "Cannot re-register pmu.\n"))
+ return -EINVAL;
+
d->pmu.base = (struct pmu){
.attr_groups = attr_groups,
.module = THIS_MODULE,
@@ -209,6 +220,8 @@ static int device_init(struct dummy_device *d)
.start = dummy_pmu_event_start,
.stop = dummy_pmu_event_stop,
.read = dummy_pmu_event_read,
+ .get = dummy_pmu_get,
+ .put = dummy_pmu_put,
};
d->pmu.name = kasprintf(GFP_KERNEL, "dummy_pmu_%u", d->instance);
@@ -217,12 +230,22 @@ static int device_init(struct dummy_device *d)
ret = perf_pmu_register(&d->pmu.base, d->pmu.name, -1);
if (ret)
- return ret;
+ goto fail;
d->pmu.registered = true;
pr_info("Device registered: %s\n", d->pmu.name);
return 0;
+
+fail:
+ /*
+ * See device_release: if name is non-NULL, dummy_pmu was registered
+ * with perf and needs cleanup
+ */
+ kfree(d->pmu.name);
+ d->pmu.name = NULL;
+
+ return ret;
}
static void device_exit(struct dummy_device *d)
@@ -237,7 +260,10 @@ static void device_release(struct kref *ref)
{
struct dummy_device *d = container_of(ref, struct dummy_device, refcount);
- kfree(d->pmu.name);
+ if (d->pmu.name) {
+ perf_pmu_free(&d->pmu.base);
+ kfree(d->pmu.name);
+ }
kfree(d);
}
--
2.46.2
next prev parent reply other threads:[~2024-10-08 18:35 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-08 18:34 [PATCH 0/5] perf: Fix pmu for drivers with bind/unbind Lucas De Marchi
2024-10-08 18:34 ` [PATCH 1/5] perf: Add dummy pmu module Lucas De Marchi
2024-10-15 0:26 ` Jeff Johnson
2024-10-15 4:23 ` Lucas De Marchi
2024-10-16 8:51 ` Peter Zijlstra
2024-10-18 19:30 ` Lucas De Marchi
2024-10-08 18:34 ` [PATCH 2/5] perf: Move free outside of the mutex Lucas De Marchi
2024-10-08 18:34 ` [PATCH 3/5] perf: Add pmu get/put Lucas De Marchi
2024-10-09 10:44 ` kernel test robot
2024-10-14 17:32 ` Peter Zijlstra
2024-10-14 18:20 ` Lucas De Marchi
2024-10-14 19:25 ` Peter Zijlstra
2024-10-14 20:12 ` Lucas De Marchi
2024-10-16 12:03 ` Peter Zijlstra
2024-10-18 19:46 ` Lucas De Marchi
2024-10-22 21:52 ` Peter Zijlstra
2024-10-23 5:07 ` Lucas De Marchi
2024-10-31 5:07 ` Lucas De Marchi
2024-10-31 9:45 ` Peter Zijlstra
2024-10-08 18:35 ` Lucas De Marchi [this message]
2024-10-08 18:35 ` [PATCH 5/5] perf/dummy_pmu: Track and disable active events Lucas De Marchi
2024-10-11 22:21 ` [PATCH 0/5] perf: Fix pmu for drivers with bind/unbind Umesh Nerlige Ramappa
2024-10-11 23:03 ` Lucas De Marchi
2024-10-11 22:59 ` Lucas De Marchi
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=20241008183501.1354695-5-lucas.demarchi@intel.com \
--to=lucas.demarchi@intel.com \
--cc=acme@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=irogers@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=tvrtko.ursulin@igalia.com \
--cc=umesh.nerlige.ramappa@intel.com \
/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
all inboxes | Powered by JetHome®