From nobody Wed Nov 27 16:39:37 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0DE60212D09 for ; Tue, 8 Oct 2024 18:35:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412540; cv=none; b=GB/MPaY7b13gndyhkSAkJkmIQMC2d9H6ZWKYF4eUiQoZS+xr6vLTZFIposDrvrC0KcpExI7BhZlVMWAQQ0gtS3lTJEdLamHVTurfD8vJo+EWz9DUl5hLS5KerPmSKbqRnhnYy/nBFrbG0GzWACTANDEv36hO1FmLkQt3ecoUFVk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412540; c=relaxed/simple; bh=wcVQHgu0ANeHR4UYpWHs2sNLqJA/YG+05getslSrXfE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=l4rJeuQEqxqvHEAuD5/v2Ic6vBZJrXdooDOOQ7hIxYTEkSWafYCvdKkmCyITdkiF+hjuWkZJEJqStxdJErr997BiFZIqkI36/rvFANzr0XeyWuUCdqnfddfgDC/1gMIt7E7UGEyXXngU8+haoWYTkvZBant1ZzDxkslJ5v0NlQo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=DYPUdSkI; arc=none smtp.client-ip=192.198.163.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="DYPUdSkI" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1728412539; x=1759948539; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=wcVQHgu0ANeHR4UYpWHs2sNLqJA/YG+05getslSrXfE=; b=DYPUdSkIyfIbvdpAIDk4EljVAhmzvejXD6yvIgJkhPbh32Gs1TzZPpKj 01gWZ7xhR5+Jm4V1oLr6TfY1KOY32O42NO3sI7gfqntptAJfzwsU3xksl ud7zJjBn+Jx5k3GpJS8nDhi+6RPvsWeRDnwRgwRF8Ghw2SJr1TS3NflqF EXKn9ZpNqNLWvEUC+fLKYrAIepeYDRTy9b/cPMqwSSKq+QgZljGpf398g fpSVtTkUPWRvKz3JKgl/PZ+aIDVSXrOsxJoPVG9nboEoQGeI26SnxAjdo KkJMi6RwTGYljRfiU16By3QcnMjCZ0aP2WB/s32TysqJY6Gl8SBIu76RW g==; X-CSE-ConnectionGUID: yFrKv/EYS7+JP6ji4IsyRQ== X-CSE-MsgGUID: WR49J3dgSjO64aCX36Jmqw== X-IronPort-AV: E=McAfee;i="6700,10204,11219"; a="38295333" X-IronPort-AV: E=Sophos;i="6.11,187,1725346800"; d="scan'208";a="38295333" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2024 11:35:38 -0700 X-CSE-ConnectionGUID: A9Fd8u2BQsyq0fW1dgvljw== X-CSE-MsgGUID: dkoM+u6tRHGfQ3RHWy9y6w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,187,1725346800"; d="scan'208";a="80530907" Received: from ldmartin-desk2.corp.intel.com (HELO ldmartin-desk2.intel.com) ([10.125.110.138]) by fmviesa004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2024 11:35:37 -0700 From: Lucas De Marchi To: linux-kernel@vger.kernel.org Cc: dri-devel@lists.freedesktop.org, Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Umesh Nerlige Ramappa , Ian Rogers , Tvrtko Ursulin , Lucas De Marchi Subject: [PATCH 1/5] perf: Add dummy pmu module Date: Tue, 8 Oct 2024 13:34:57 -0500 Message-ID: <20241008183501.1354695-2-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241008183501.1354695-1-lucas.demarchi@intel.com> References: <20241008183501.1354695-1-lucas.demarchi@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Simple dummy module that mimics what can be done with drivers to bind/unbind them from the bus, which should trigger resource release. This is mostly based on how i915 and (pending changes for) xe drivers are interacting with perf pmu. A few differences due to not having backing hardware or for simplicity: - Instead of using BDF for bind/unbind, use a single number. - Unbind is triggered either via debugfs or when removing the module. - event::destroy() is always assigned as there should only be a few additional calls Signed-off-by: Lucas De Marchi --- kernel/events/Makefile | 1 + kernel/events/dummy_pmu.c | 426 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 427 insertions(+) create mode 100644 kernel/events/dummy_pmu.c diff --git a/kernel/events/Makefile b/kernel/events/Makefile index 91a62f566743..2993fed2d091 100644 --- a/kernel/events/Makefile +++ b/kernel/events/Makefile @@ -4,3 +4,4 @@ obj-y :=3D core.o ring_buffer.o callchain.o obj-$(CONFIG_HAVE_HW_BREAKPOINT) +=3D hw_breakpoint.o obj-$(CONFIG_HW_BREAKPOINT_KUNIT_TEST) +=3D hw_breakpoint_test.o obj-$(CONFIG_UPROBES) +=3D uprobes.o +obj-m +=3D dummy_pmu.o diff --git a/kernel/events/dummy_pmu.c b/kernel/events/dummy_pmu.c new file mode 100644 index 000000000000..cdba3a831e4a --- /dev/null +++ b/kernel/events/dummy_pmu.c @@ -0,0 +1,426 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright =C2=A9 2024 Intel Corporation + */ + +#define pr_fmt(fmt) "%s: " fmt, KBUILD_MODNAME + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct dummy_mod { + struct dentry *debugfs_root; + + struct list_head device_list; + struct mutex mutex; +}; + +struct dummy_pmu { + struct pmu base; + char *name; + bool registered; +}; + +struct dummy_device { + unsigned int instance; + struct kref refcount; + struct list_head mod_entry; + struct dummy_pmu pmu; +}; + +static struct dummy_mod dm; + +static void device_release(struct kref *ref); + +static struct dummy_pmu *event_to_pmu(struct perf_event *event) +{ + return container_of(event->pmu, struct dummy_pmu, base); +} + +static struct dummy_device *pmu_to_device(struct dummy_pmu *pmu) +{ + return container_of(pmu, struct dummy_device, pmu); +} + +static ssize_t dummy_pmu_events_sysfs_show(struct device *dev, + struct device_attribute *attr, + char *page) +{ + struct perf_pmu_events_attr *pmu_attr; + + pmu_attr =3D container_of(attr, struct perf_pmu_events_attr, attr); + + return sprintf(page, "event=3D0x%04llx\n", pmu_attr->id); +} + +#define DUMMY_PMU_EVENT_ATTR(name, config) \ + PMU_EVENT_ATTR_ID(name, dummy_pmu_events_sysfs_show, config) + +PMU_FORMAT_ATTR(event, "config:0-63"); + +#define DUMMY1_CONFIG 0x01 +#define DUMMY2_CONFIG 0x02 + +static struct attribute *dummy_pmu_event_attrs[] =3D { + DUMMY_PMU_EVENT_ATTR(test-event-1, DUMMY1_CONFIG), + DUMMY_PMU_EVENT_ATTR(test-event-2, DUMMY2_CONFIG), + NULL, +}; + +static struct attribute *dummy_pmu_format_attrs[] =3D { + &format_attr_event.attr, + NULL, +}; +static const struct attribute_group dummy_pmu_events_attr_group =3D { + .name =3D "events", + .attrs =3D dummy_pmu_event_attrs, +}; +static const struct attribute_group dummy_pmu_format_attr_group =3D { + .name =3D "format", + .attrs =3D dummy_pmu_format_attrs, +}; +static const struct attribute_group *attr_groups[] =3D { + &dummy_pmu_format_attr_group, + &dummy_pmu_events_attr_group, + NULL, +}; + +static void dummy_pmu_event_destroy(struct perf_event *event) +{ + struct dummy_pmu *pmu =3D event_to_pmu(event); + struct dummy_device *d =3D pmu_to_device(pmu); + + kref_put(&d->refcount, device_release); +} + +static int dummy_pmu_event_init(struct perf_event *event) +{ + struct dummy_pmu *pmu =3D event_to_pmu(event); + struct dummy_device *d =3D pmu_to_device(pmu); + + if (!pmu->registered) + return -ENODEV; + + if (event->attr.type !=3D event->pmu->type) + return -ENOENT; + + /* unsupported modes and filters */ + if (event->attr.sample_period) /* no sampling */ + return -EINVAL; + + if (has_branch_stack(event)) + return -EOPNOTSUPP; + + 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 =3D dummy_pmu_event_destroy; + + return 0; +} + +static void dummy_pmu_event_start(struct perf_event *event, int flags) +{ + struct dummy_pmu *pmu =3D event_to_pmu(event); + + if (!pmu->registered) + return; + + event->hw.state =3D 0; +} + +static void dummy_pmu_event_read(struct perf_event *event) +{ + struct dummy_pmu *pmu =3D event_to_pmu(event); + u8 buf; + + if (!pmu->registered) { + event->hw.state =3D PERF_HES_STOPPED; + return; + } + + get_random_bytes(&buf, 1); + buf %=3D 10; + + switch (event->attr.config & 0xf) { + case DUMMY1_CONFIG: + break; + case DUMMY2_CONFIG: + buf *=3D 2; + break; + } + + local64_add(buf, &event->count); +} + +static void dummy_pmu_event_stop(struct perf_event *event, int flags) +{ + struct dummy_pmu *pmu =3D event_to_pmu(event); + + if (!pmu->registered) + goto out; + + if (flags & PERF_EF_UPDATE) + dummy_pmu_event_read(event); + +out: + event->hw.state =3D PERF_HES_STOPPED; +} + +static int dummy_pmu_event_add(struct perf_event *event, int flags) +{ + struct dummy_pmu *pmu =3D event_to_pmu(event); + + if (!pmu->registered) + return -ENODEV; + + if (flags & PERF_EF_START) + dummy_pmu_event_start(event, flags); + + return 0; + +} + +static void dummy_pmu_event_del(struct perf_event *event, int flags) +{ + dummy_pmu_event_stop(event, PERF_EF_UPDATE); +} + +static int device_init(struct dummy_device *d) +{ + int ret; + + d->pmu.base =3D (struct pmu){ + .attr_groups =3D attr_groups, + .module =3D THIS_MODULE, + .task_ctx_nr =3D perf_invalid_context, + .event_init =3D dummy_pmu_event_init, + .add =3D dummy_pmu_event_add, + .del =3D dummy_pmu_event_del, + .start =3D dummy_pmu_event_start, + .stop =3D dummy_pmu_event_stop, + .read =3D dummy_pmu_event_read, + }; + + d->pmu.name =3D kasprintf(GFP_KERNEL, "dummy_pmu_%u", d->instance); + if (!d->pmu.name) + return -ENOMEM; + + ret =3D perf_pmu_register(&d->pmu.base, d->pmu.name, -1); + if (ret) + return ret; + + d->pmu.registered =3D true; + pr_info("Device registered: %s\n", d->pmu.name); + + return 0; +} + +static void device_exit(struct dummy_device *d) +{ + d->pmu.registered =3D false; + perf_pmu_unregister(&d->pmu.base); + + pr_info("Device released: %s\n", d->pmu.name); +} + +static void device_release(struct kref *ref) +{ + struct dummy_device *d =3D container_of(ref, struct dummy_device, refcoun= t); + + kfree(d->pmu.name); + kfree(d); +} + +static struct dummy_device *find_device_locked(struct dummy_mod *m, unsign= ed int instance) +{ + struct dummy_device *d; + + list_for_each_entry(d, &m->device_list, mod_entry) + if (d->instance =3D=3D instance) + return d; + + return NULL; +} + +static int dummy_add_device(struct dummy_mod *m, unsigned int instance) +{ + struct dummy_device *d, *d2; + int ret =3D 0; + + mutex_lock(&m->mutex); + d =3D find_device_locked(m, instance); + mutex_unlock(&m->mutex); + if (d) + return -EINVAL; + + d =3D kcalloc(1, sizeof(*d), GFP_KERNEL); + if (!d) + return -ENOMEM; + + kref_init(&d->refcount); + d->instance =3D instance; + + ret =3D device_init(d); + if (ret < 0) + goto fail_put; + + mutex_lock(&m->mutex); + d2 =3D find_device_locked(m, instance); + if (d2) { + mutex_unlock(&m->mutex); + ret =3D -EINVAL; + goto fail_exit; + } + list_add(&d->mod_entry, &m->device_list); + mutex_unlock(&m->mutex); + + return 0; + +fail_exit: + device_exit(d); +fail_put: + kref_put(&d->refcount, device_release); + return ret; +} + +static int dummy_del_device(struct dummy_mod *m, unsigned int instance) +{ + struct dummy_device *d, *found =3D NULL; + + mutex_lock(&m->mutex); + list_for_each_entry(d, &m->device_list, mod_entry) { + if (d->instance =3D=3D instance) { + list_del(&d->mod_entry); + found =3D d; + break; + } + } + mutex_unlock(&m->mutex); + + if (!found) + return -EINVAL; + + device_exit(found); + kref_put(&found->refcount, device_release); + + return 0; +} + +static int parse_device(const char __user *ubuf, size_t size, u32 *instanc= e) +{ + char buf[16]; + ssize_t len; + + if (size > sizeof(buf) - 1) + return -E2BIG; + + len =3D strncpy_from_user(buf, ubuf, sizeof(buf)); + if (len < 0 || len >=3D sizeof(buf) - 1) + return -E2BIG; + + if (kstrtou32(buf, 0, instance)) + return -EINVAL; + + return size; +} + +static int bind_show(struct seq_file *s, void *unused) +{ + struct dummy_mod *m =3D s->private; + struct dummy_device *d; + + mutex_lock(&m->mutex); + list_for_each_entry(d, &m->device_list, mod_entry) + seq_printf(s, "%u\n", d->instance); + mutex_unlock(&m->mutex); + + return 0; +} + +static ssize_t bind_write(struct file *f, const char __user *ubuf, + size_t size, loff_t *pos) +{ + struct dummy_mod *m =3D file_inode(f)->i_private; + u32 instance; + ssize_t ret; + + ret =3D parse_device(ubuf, size, &instance); + if (ret < 0) + return ret; + + ret =3D dummy_add_device(m, instance); + if (ret < 0) + return ret; + + return size; +} +DEFINE_SHOW_STORE_ATTRIBUTE(bind); + +static int unbind_show(struct seq_file *s, void *unused) +{ + return -EPERM; +} + +static ssize_t unbind_write(struct file *f, const char __user *ubuf, + size_t size, loff_t *pos) +{ + struct dummy_mod *m =3D file_inode(f)->i_private; + unsigned int instance; + ssize_t ret; + + ret =3D parse_device(ubuf, size, &instance); + if (ret < 0) + return ret; + + ret =3D dummy_del_device(m, instance); + if (ret < 0) + return ret; + + return size; +} +DEFINE_SHOW_STORE_ATTRIBUTE(unbind); + +static int __init dummy_init(void) +{ + struct dentry *dir; + + dir =3D debugfs_create_dir(KBUILD_MODNAME, NULL); + debugfs_create_file("bind", 0600, dir, &dm, &bind_fops); + debugfs_create_file("unbind", 0200, dir, &dm, &unbind_fops); + + dm.debugfs_root =3D dir; + INIT_LIST_HEAD(&dm.device_list); + mutex_init(&dm.mutex); + + return 0; +} + +static void dummy_exit(void) +{ + struct dummy_device *d, *tmp; + + debugfs_remove_recursive(dm.debugfs_root); + + mutex_lock(&dm.mutex); + list_for_each_entry_safe(d, tmp, &dm.device_list, mod_entry) { + device_exit(d); + kref_put(&d->refcount, device_release); + } + mutex_unlock(&dm.mutex); +} + +module_init(dummy_init); +module_exit(dummy_exit); + +MODULE_AUTHOR("Lucas De Marchi "); +MODULE_LICENSE("GPL"); --=20 2.46.2 From nobody Wed Nov 27 16:39:37 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A942213ED7 for ; Tue, 8 Oct 2024 18:35:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412541; cv=none; b=IVGR2LmYVUDYKsARIt79dR8xAIHrPRL9JKs8xEjkNxC7uqy6NrUFUeIX70E3IDgSE9qVP29+HU9xWYXAcMb9GnK6IDJSTnOKZIhlGnAMAeGh9fzTDzHhqAX+NM2lexzgP6Ofw/eQ0euGBNeOb1oUZ+0QnUFGFM2mJGhlgBtTNGk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412541; c=relaxed/simple; bh=NF8wAROMOzFMcdExLwjtr/ohn+EHVISxj9yvB+SoDkg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ksrmb6CqjH4G8ofxBzjokZOAAZccXkBn+r1VFZdZLCisrhgk5NbAdBEEamATgg2EPkzYD+nx8sOLFLxnKQ5MrAdyyYbetg5W9REtXMwChuBx3mut3EauYfwfkO7XYM2iPcHva3duJDL+PGfQbmj+SoQ3wgwLSxoO3DVYFFWFapE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=TJ2pfb3o; arc=none smtp.client-ip=192.198.163.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="TJ2pfb3o" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1728412540; x=1759948540; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=NF8wAROMOzFMcdExLwjtr/ohn+EHVISxj9yvB+SoDkg=; b=TJ2pfb3ocJRzE0VAUpg/5GkRoahQUIY2fll2kAdyK4FqzZd/c2Zm+LAN Iz93S+G9SysqOq9CbU6602oPGToUucyPOuLy94ZYRRWIxw21ZxUkNxVos h7rt8ImHFoWSpxUHOKdJBmxNr2OPht3KLR4A5lLBH2iBlPizCAbpreWlU vIqaqq+pvm4e+039hcKVCYTOF2WUCGc/WdcjYQSraka//dZeFc81kT/kK Lu560X1MbUIXFCqmFZQ/FtIoWAWV4haOKDQ/ZSdKoJfPOHP+2N+1xLLJa nN9KtJip6uRL6nxyIMij8xhajF16w6r6F6V2aqECVAkgwcHfxL6I3WGY3 w==; X-CSE-ConnectionGUID: qP5xhnqyRiGhJAwS0jdeaQ== X-CSE-MsgGUID: z7J5eEOjSwCGXPWGVbKeAw== X-IronPort-AV: E=McAfee;i="6700,10204,11219"; a="38295344" X-IronPort-AV: E=Sophos;i="6.11,187,1725346800"; d="scan'208";a="38295344" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2024 11:35:39 -0700 X-CSE-ConnectionGUID: uW9SEAZ9SHi/ZAaJsRS5Sw== X-CSE-MsgGUID: wmi9BkTXSzOHjlW7L/92sQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,187,1725346800"; d="scan'208";a="80530912" Received: from ldmartin-desk2.corp.intel.com (HELO ldmartin-desk2.intel.com) ([10.125.110.138]) by fmviesa004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2024 11:35:38 -0700 From: Lucas De Marchi To: linux-kernel@vger.kernel.org Cc: dri-devel@lists.freedesktop.org, Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Umesh Nerlige Ramappa , Ian Rogers , Tvrtko Ursulin , Lucas De Marchi Subject: [PATCH 2/5] perf: Move free outside of the mutex Date: Tue, 8 Oct 2024 13:34:58 -0500 Message-ID: <20241008183501.1354695-3-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241008183501.1354695-1-lucas.demarchi@intel.com> References: <20241008183501.1354695-1-lucas.demarchi@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It's not needed to hold the mutex to free the percpu variables stored in pmu. Move them outside of the mutex protection in preparation for possibly allowing them to live longer, according to the lifecycle of the object owning/containing the pmu. Signed-off-by: Lucas De Marchi --- kernel/events/core.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 3b8b85adb10a..6395dbf67671 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -11845,7 +11845,6 @@ void perf_pmu_unregister(struct pmu *pmu) synchronize_srcu(&pmus_srcu); synchronize_rcu(); =20 - free_percpu(pmu->pmu_disable_count); idr_remove(&pmu_idr, pmu->type); if (pmu_bus_running && pmu->dev && pmu->dev !=3D PMU_NULL_DEV) { if (pmu->nr_addr_filters) @@ -11853,8 +11852,11 @@ void perf_pmu_unregister(struct pmu *pmu) device_del(pmu->dev); put_device(pmu->dev); } - free_pmu_context(pmu); + mutex_unlock(&pmus_lock); + + free_percpu(pmu->pmu_disable_count); + free_pmu_context(pmu); } EXPORT_SYMBOL_GPL(perf_pmu_unregister); =20 --=20 2.46.2 From nobody Wed Nov 27 16:39:37 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 99FBD213EDB for ; Tue, 8 Oct 2024 18:35:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412543; cv=none; b=nN6JM/b1bq4mYROWbbSv1jsh6FlBw97iNEZRXf3QdmDMZX/HGGTd9nOYRcIUJQSbBIOA0KcDvRnQzsMmJ51NLzrLKIoYchYCBr0cLaZBIk57GJJJFLk4kPzCWMC6LxIt4UxhTKJODXIazm78Jd6Tm/mrb1TNhexrrc4AoO8j3CM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412543; c=relaxed/simple; bh=/JJZCwajkVb0QLSNq1aYtA8Fx0IU33m7ID0rexUyFaY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YQNv6CsjyzyEGzb3pn3jmsNF1N5znn6kHYMMqAGPSoS+J4Y+4MXbS0CNv6MjU+HnooLUi303SAfaPf5zwpmms/AdGKuIVYyK++ekJMmxyLekrtGZlY3OJlJwx9E7LX2EGaHlrKZFhyLh18TPQMsBVsG1BVF9QVBTi0vy52kPrfw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=joKeo7mX; arc=none smtp.client-ip=192.198.163.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="joKeo7mX" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1728412541; x=1759948541; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/JJZCwajkVb0QLSNq1aYtA8Fx0IU33m7ID0rexUyFaY=; b=joKeo7mXVXJDqhOIHOFTsllLkQ762cVCNJxIpAKoTZuxiUunuKmcdD1g drnH24uNxX3s01niojed0jhNAuCZyKh821CV0w2ibGtxcY3KK4Aw0PNPN 6ltHaIzxnXlRD9ow2tz19w6+bczgXTMTb03lWFW5242P59DzhOkIO1/iB pcymGoovdyuGl7iyA4ibCUJlh8DOwf+fukb0F5VdwQx23yR9Y+GBUqT7l SPWSQdIxLyHHXgsDEXHFj6Ix9n/WvezsJOJyTUAl7lv1s4j2zFhOT2PBx +nFe15UQi6trrinLaRefKUXFe+TR1Yv2k6dyrDKwhW+1Hbgikmie0Mz5Q g==; X-CSE-ConnectionGUID: i/pXcpqURJCGhhmAbKW1Rg== X-CSE-MsgGUID: bAqiaAe/ToKdCKL2u5dcrA== X-IronPort-AV: E=McAfee;i="6700,10204,11219"; a="38295355" X-IronPort-AV: E=Sophos;i="6.11,187,1725346800"; d="scan'208";a="38295355" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2024 11:35:40 -0700 X-CSE-ConnectionGUID: m3qTCs16TxumPMmgGt5IFQ== X-CSE-MsgGUID: wQYexEUqQ5+vLU9t7EOaLQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,187,1725346800"; d="scan'208";a="80530917" Received: from ldmartin-desk2.corp.intel.com (HELO ldmartin-desk2.intel.com) ([10.125.110.138]) by fmviesa004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2024 11:35:39 -0700 From: Lucas De Marchi To: linux-kernel@vger.kernel.org Cc: dri-devel@lists.freedesktop.org, Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Umesh Nerlige Ramappa , Ian Rogers , Tvrtko Ursulin , Lucas De Marchi Subject: [PATCH 3/5] perf: Add pmu get/put Date: Tue, 8 Oct 2024 13:34:59 -0500 Message-ID: <20241008183501.1354695-4-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241008183501.1354695-1-lucas.demarchi@intel.com> References: <20241008183501.1354695-1-lucas.demarchi@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If a pmu is unregistered while there's an active event, perf will still access the pmu via event->pmu, even after the event is destroyed. This makes it difficult for drivers like i915 that can be unbound from the HW. BUG: KASAN: use-after-free in exclusive_event_destroy+0xd8/0xf0 Read of size 4 at addr ffff88816e2bb63c by task perf/7748 i915 tries to cope with it by installing a event->destroy, but that is not sufficient: if pmu is released by the driver, it will still crash since event->pmu is still used. Moreover, even with that use-after-free fixed by adjusting the order in _free_event() or delaying the free by the driver, kernel still oops when closing the event fd related to a unregistered pmu: the percpu variables allocated on perf_pmu_register() would already be released. One such crash is: BUG: KASAN: user-memory-access in _raw_spin_lock_irqsave+0x88/0x100 Write of size 4 at addr 00000000ffffffff by task perf/727 CPU: 0 UID: 0 PID: 727 Comm: perf Not tainted 6.12.0-rc1-DEMARCHI-dxnf+ #9 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS unknown 2/2/20= 22 Call Trace: dump_stack_lvl+0x5f/0x90 print_report+0x4d3/0x50a ? __pfx__raw_spin_lock_irqsave+0x10/0x10 ? kasan_addr_to_slab+0xd/0xb0 kasan_report+0xe2/0x170 ? _raw_spin_lock_irqsave+0x88/0x100 ? _raw_spin_lock_irqsave+0x88/0x100 kasan_check_range+0x125/0x230 __kasan_check_write+0x14/0x30 _raw_spin_lock_irqsave+0x88/0x100 ? __pfx__raw_spin_lock_irqsave+0x10/0x10 _atomic_dec_and_raw_lock_irqsave+0x89/0x110 ? __kasan_check_write+0x14/0x30 put_pmu_ctx+0x98/0x330 The fix here is to provide a set of get/put hooks that drivers can implement to piggy back the perf's pmu lifecycle to the driver's instance lifecycle. With this, perf_pmu_unregister() can be called by the driver, which is then responsible for freeing the resources. Signed-off-by: Lucas De Marchi --- include/linux/perf_event.h | 12 ++++++++++++ kernel/events/core.c | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index fb908843f209..d6983dbf5a45 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -561,6 +561,17 @@ struct pmu { * Check period value for PERF_EVENT_IOC_PERIOD ioctl. */ int (*check_period) (struct perf_event *event, u64 value); /* optional */ + + /* + * Optional: get a reference. Typically needed by PMUs that are bound to = a device + * that can be hotplugged, either physically or through sysfs' bind/unbin= d. When provided, + * pmu::put() is mandatory and it's driver responsibility to call perf_pm= u_free() when + * resources can be released. + */ + struct pmu *(*get) (struct pmu *pmu); + + /* Optional: put a reference. See pmu::get() */ + void (*put) (struct pmu *pmu); }; =20 enum perf_addr_filter_action_t { @@ -1104,6 +1115,7 @@ extern void perf_event_itrace_started(struct perf_eve= nt *event); =20 extern int perf_pmu_register(struct pmu *pmu, const char *name, int type); extern void perf_pmu_unregister(struct pmu *pmu); +extern void perf_pmu_free(struct pmu *pmu); =20 extern void __perf_event_task_sched_in(struct task_struct *prev, struct task_struct *task); diff --git a/kernel/events/core.c b/kernel/events/core.c index 6395dbf67671..bf5b8fc8979e 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -5319,8 +5319,24 @@ static void perf_pending_task_sync(struct perf_event= *event) rcuwait_wait_event(&event->pending_work_wait, !event->pending_work, TASK_= UNINTERRUPTIBLE); } =20 +static void pmu_module_put(struct pmu **ppmu) +{ + struct pmu *pmu =3D *ppmu; + struct module *module =3D pmu->module; + + if (pmu->put) + pmu->put(pmu); + + module_put(module); + + /* Can't touch pmu anymore/ */ + *ppmu =3D NULL; +} + static void _free_event(struct perf_event *event) { + struct module *module; + irq_work_sync(&event->pending_irq); irq_work_sync(&event->pending_disable_irq); perf_pending_task_sync(event); @@ -5374,7 +5390,8 @@ static void _free_event(struct perf_event *event) put_ctx(event->ctx); =20 exclusive_event_destroy(event); - module_put(event->pmu->module); + + pmu_module_put(&event->pmu); =20 call_rcu(&event->rcu_head, free_event_rcu); } @@ -11512,10 +11529,12 @@ static int perf_event_idx_default(struct perf_eve= nt *event) return 0; } =20 -static void free_pmu_context(struct pmu *pmu) +void perf_pmu_free(struct pmu *pmu) { free_percpu(pmu->cpu_pmu_context); + free_percpu(pmu->pmu_disable_count); } +EXPORT_SYMBOL_GPL(perf_pmu_free); =20 /* * Let userspace know that this PMU supports address range filtering: @@ -11749,6 +11768,11 @@ int perf_pmu_register(struct pmu *pmu, const char = *name, int type) goto free_pdc; } =20 + if (WARN_ONCE((!!pmu->get) ^ (!!pmu->put), "Can not register a pmu with o= nly get or put defined.\n")) { + ret =3D -EINVAL; + goto free_pdc; + } + pmu->name =3D name; =20 if (type >=3D 0) @@ -11855,8 +11879,8 @@ void perf_pmu_unregister(struct pmu *pmu) =20 mutex_unlock(&pmus_lock); =20 - free_percpu(pmu->pmu_disable_count); - free_pmu_context(pmu); + if (!pmu->put) + perf_pmu_free(pmu); } EXPORT_SYMBOL_GPL(perf_pmu_unregister); =20 @@ -11890,6 +11914,9 @@ static int perf_try_init_event(struct pmu *pmu, str= uct perf_event *event) BUG_ON(!ctx); } =20 + if (pmu->get) + pmu->get(pmu); + event->pmu =3D pmu; ret =3D pmu->event_init(event); =20 @@ -11926,7 +11953,7 @@ static int perf_try_init_event(struct pmu *pmu, str= uct perf_event *event) } =20 if (ret) - module_put(pmu->module); + pmu_module_put(&pmu); =20 return ret; } --=20 2.46.2 From nobody Wed Nov 27 16:39:37 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F1E972141D0 for ; Tue, 8 Oct 2024 18:35:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412543; cv=none; b=W+cjHUHNthoqlaYS+zW4uKZD70yOkhftzBDdzTm+J2mMoiNvmfumQaXev9TdAJRnhnyda3ZX3poz3iU0B5f3zqc6B8/W8f+ki8I004eFAZh5MztPSOi16p9xTEFGbJ3AbIOTHGDZ8X9h4lzNxEhLMTWqpr6NurmI4sDiNAFs9FM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412543; c=relaxed/simple; bh=9NC942Tnu9eotfuiQ8yTEZBW0BsoN3zsxEknTk/dLpA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=g3E7RrGEMvPhhIxMWIPtH0wuHmn3O3UUDai37zbIYLgvzCwaIiRlxi38ebfGF102oZD3tM6v54Ayxfb0RaSWCiKCXMcOu3fNRRF7BKHYgbziLYtCUmp/1KD6Au8c+CtBgoGQWRT3M2GgxFc7RBEtuyRiPpXcoYk4YTKumOqA2HY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=BOWb547T; arc=none smtp.client-ip=192.198.163.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="BOWb547T" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1728412542; x=1759948542; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9NC942Tnu9eotfuiQ8yTEZBW0BsoN3zsxEknTk/dLpA=; b=BOWb547TP/V0TvNa2disxv7ByD0NIVvQaI6tQj8RFmlcMnlQhpM5UTA+ 1T1eL7/22i03PwlbvecRFfVoG2xel/6govgohqjPNKjk/6d9tn7TPzJiD RDxxVwpq8VO7NYnLHHJADJqG0Vh774jsK1FBJeQQMRbfqlWXlv7B2kHbV Z37AtwVlitx1YkuGqx3ndPj3k9+cB/FgA9gcb6RzuRDy5RpfYjVfg7lGG 1l/7ykaSVmoR8ySThBlEtoYRklC+hsm4t/OhwQBwoeWr4hwXFXSFvvjJj iD4baZojPwlhOe0FTR+XlLJL7msiu9+a7qA0SeT99T6ZRQPALCQDlMEmh Q==; X-CSE-ConnectionGUID: qU8ep5+4Tn+tsk2k/oMbLA== X-CSE-MsgGUID: q5dZcY8VTXyJbvZ2+bdEdQ== X-IronPort-AV: E=McAfee;i="6700,10204,11219"; a="38295365" X-IronPort-AV: E=Sophos;i="6.11,187,1725346800"; d="scan'208";a="38295365" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2024 11:35:42 -0700 X-CSE-ConnectionGUID: 1sJW7vDLTn64kfNX0ZMYPg== X-CSE-MsgGUID: k5K8oGCjTxiFbh0EcnRngQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,187,1725346800"; d="scan'208";a="80530929" Received: from ldmartin-desk2.corp.intel.com (HELO ldmartin-desk2.intel.com) ([10.125.110.138]) by fmviesa004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2024 11:35:40 -0700 From: Lucas De Marchi To: linux-kernel@vger.kernel.org Cc: dri-devel@lists.freedesktop.org, Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Umesh Nerlige Ramappa , Ian Rogers , Tvrtko Ursulin , Lucas De Marchi Subject: [PATCH 4/5] perf/dummy_pmu: Tie pmu to device lifecycle Date: Tue, 8 Oct 2024 13:35:00 -0500 Message-ID: <20241008183501.1354695-5-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241008183501.1354695-1-lucas.demarchi@intel.com> References: <20241008183501.1354695-1-lucas.demarchi@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- 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_p= mu *pmu) return container_of(pmu, struct dummy_device, pmu); } =20 +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[] =3D { NULL, }; =20 -static void dummy_pmu_event_destroy(struct perf_event *event) -{ - struct dummy_pmu *pmu =3D event_to_pmu(event); - struct dummy_device *d =3D pmu_to_device(pmu); - - kref_put(&d->refcount, device_release); -} - static int dummy_pmu_event_init(struct perf_event *event) { struct dummy_pmu *pmu =3D event_to_pmu(event); - struct dummy_device *d =3D pmu_to_device(pmu); =20 if (!pmu->registered) return -ENODEV; @@ -121,10 +117,6 @@ static int dummy_pmu_event_init(struct perf_event *eve= nt) if (event->cpu < 0) return -EINVAL; =20 - /* Event keeps a ref to maintain PMU allocated, even if it's unregistered= */ - kref_get(&d->refcount); - event->destroy =3D dummy_pmu_event_destroy; - return 0; } =20 @@ -195,10 +187,29 @@ static void dummy_pmu_event_del(struct perf_event *ev= ent, int flags) dummy_pmu_event_stop(event, PERF_EF_UPDATE); } =20 +static struct pmu *dummy_pmu_get(struct pmu *pmu) +{ + struct dummy_device *d =3D 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 =3D pmu_to_device(pmu_to_dummy(pmu)); + + kref_put(&d->refcount, device_release); +} + static int device_init(struct dummy_device *d) { int ret; =20 + if (WARN_ONCE(d->pmu.name, "Cannot re-register pmu.\n")) + return -EINVAL; + d->pmu.base =3D (struct pmu){ .attr_groups =3D attr_groups, .module =3D THIS_MODULE, @@ -209,6 +220,8 @@ static int device_init(struct dummy_device *d) .start =3D dummy_pmu_event_start, .stop =3D dummy_pmu_event_stop, .read =3D dummy_pmu_event_read, + .get =3D dummy_pmu_get, + .put =3D dummy_pmu_put, }; =20 d->pmu.name =3D kasprintf(GFP_KERNEL, "dummy_pmu_%u", d->instance); @@ -217,12 +230,22 @@ static int device_init(struct dummy_device *d) =20 ret =3D perf_pmu_register(&d->pmu.base, d->pmu.name, -1); if (ret) - return ret; + goto fail; =20 d->pmu.registered =3D true; pr_info("Device registered: %s\n", d->pmu.name); =20 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 =3D NULL; + + return ret; } =20 static void device_exit(struct dummy_device *d) @@ -237,7 +260,10 @@ static void device_release(struct kref *ref) { struct dummy_device *d =3D container_of(ref, struct dummy_device, refcoun= t); =20 - kfree(d->pmu.name); + if (d->pmu.name) { + perf_pmu_free(&d->pmu.base); + kfree(d->pmu.name); + } kfree(d); } =20 --=20 2.46.2 From nobody Wed Nov 27 16:39:37 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0F359215012 for ; Tue, 8 Oct 2024 18:35:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412544; cv=none; b=qO2QxvUc+RBHcDxW6RKHErmDO49RYM47yXmL2a3pIeXXZVH7HyG4Lx7y1YFY28lX+bZ4ed98G6gbFUFeihJk9sLgsFj8MX0MLqTupfogtQSH7X1gE/Ecfp2QvAh7+aulw2mO5txIAfgu8LVjiOUyh1mlKRukFmiy+jKxRfbFb1M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412544; c=relaxed/simple; bh=nHrNeNJsF/XXWidU3270UWoFA4O2wgRb70EVT2SYX5E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Hg+iYMDD2yd79kQbJih8QDL228XzQPCIQqDkiILL9gbid7m8WdngkRp6vs76AQFjIsW845akGxW50hrfsjoMel8BCcofI1pIAc6uKHjVPKahOzl1K7kWARgv852WUIlF8CLTwbDQUnDlpG7u7g91WU6G8OJMZUfjvSigZVR8LtE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=F4wjOeTG; arc=none smtp.client-ip=192.198.163.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="F4wjOeTG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1728412543; x=1759948543; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=nHrNeNJsF/XXWidU3270UWoFA4O2wgRb70EVT2SYX5E=; b=F4wjOeTGcX+BosAtfRymoqppJ3gSPg9vbillr7fNHsBgSM2hZPbOuRQE 0lBrMB5AJ8i8FriKZ7xhp9wkV55fMf3xzDfCOLlkn1lAoxeNRk7sXA2GY qf5hLSXvpfeWhL6Qsx55yINgjpII3l36FHuRDm1S5D97Puvnaqzry08HR crg44iyQIyeUno/nxYqKYN0ZOhjtwFsZntqvRmMWL+BCcifqTjxAOA1P1 hFwNhVajLq3MLR2tG69iUHgkr6CCoBU4W1umA6SVfTvWqaTRLLicXHPVW cjNQ9O//YTAuZjMitlDx9vrClm4wSPghM8F6dtix1R/1/GqkApl4Z1UC2 A==; X-CSE-ConnectionGUID: /u3iDqsLTtajkFwHaMdIQQ== X-CSE-MsgGUID: 4wByFPtdTf+Vxz/KZv990A== X-IronPort-AV: E=McAfee;i="6700,10204,11219"; a="38295375" X-IronPort-AV: E=Sophos;i="6.11,187,1725346800"; d="scan'208";a="38295375" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2024 11:35:43 -0700 X-CSE-ConnectionGUID: 75lAtwCFTgu/OxF3Np5Inw== X-CSE-MsgGUID: lzkJKALHSFeawfpQajiarQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,187,1725346800"; d="scan'208";a="80530939" Received: from ldmartin-desk2.corp.intel.com (HELO ldmartin-desk2.intel.com) ([10.125.110.138]) by fmviesa004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2024 11:35:41 -0700 From: Lucas De Marchi To: linux-kernel@vger.kernel.org Cc: dri-devel@lists.freedesktop.org, Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Umesh Nerlige Ramappa , Ian Rogers , Tvrtko Ursulin , Lucas De Marchi Subject: [PATCH 5/5] perf/dummy_pmu: Track and disable active events Date: Tue, 8 Oct 2024 13:35:01 -0500 Message-ID: <20241008183501.1354695-6-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241008183501.1354695-1-lucas.demarchi@intel.com> References: <20241008183501.1354695-1-lucas.demarchi@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When unregistering the PMU, disable the currently active events. This allows userspace to see the change and possibly reacting on it, like reopening the fd. With perf-stat, "" starts to be printed: $ stat -e dummy_pmu_0/test-event-1/ -I1000 1.001227905 12 dummy_pmu_0/test-event-1/ 2.004009349 11 dummy_pmu_0/test-event-1/ 3.005785067 0 dummy_pmu_0/test-event-1/ 4.008565935 dummy_pmu_0/test-event-1/ 5.010446891 dummy_pmu_0/test-event-1/ Signed-off-by: Lucas De Marchi --- kernel/events/dummy_pmu.c | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/kernel/events/dummy_pmu.c b/kernel/events/dummy_pmu.c index c07e111bff01..496cb8469a05 100644 --- a/kernel/events/dummy_pmu.c +++ b/kernel/events/dummy_pmu.c @@ -14,6 +14,7 @@ #include #include #include +#include =20 struct dummy_mod { struct dentry *debugfs_root; @@ -25,6 +26,7 @@ struct dummy_mod { struct dummy_pmu { struct pmu base; char *name; + struct xarray active_events; bool registered; }; =20 @@ -97,9 +99,25 @@ static const struct attribute_group *attr_groups[] =3D { NULL, }; =20 +static void dummy_pmu_event_destroy(struct perf_event *event) +{ + struct dummy_pmu *pmu =3D event_to_pmu(event); + unsigned long idx; + struct perf_event *e; + + /* Event not active anymore */ + xa_for_each(&pmu->active_events, idx, e) + if (e =3D=3D event) { + xa_erase(&pmu->active_events, idx); + break; + } +} + static int dummy_pmu_event_init(struct perf_event *event) { struct dummy_pmu *pmu =3D event_to_pmu(event); + u32 event_id; + int ret; =20 if (!pmu->registered) return -ENODEV; @@ -117,6 +135,13 @@ static int dummy_pmu_event_init(struct perf_event *eve= nt) if (event->cpu < 0) return -EINVAL; =20 + ret =3D xa_alloc(&pmu->active_events, &event_id, event, + xa_limit_32b, GFP_KERNEL); + if (ret) + return ret; + + event->destroy =3D dummy_pmu_event_destroy; + return 0; } =20 @@ -232,6 +257,8 @@ static int device_init(struct dummy_device *d) if (ret) goto fail; =20 + xa_init_flags(&d->pmu.active_events, XA_FLAGS_ALLOC); + d->pmu.registered =3D true; pr_info("Device registered: %s\n", d->pmu.name); =20 @@ -248,9 +275,22 @@ static int device_init(struct dummy_device *d) return ret; } =20 +static void disable_active_events(struct dummy_pmu *pmu) +{ + struct perf_event *event; + unsigned long idx; + + xa_for_each(&pmu->active_events, idx, event) { + xa_erase(&pmu->active_events, idx); + perf_event_disable(event); + } +} + static void device_exit(struct dummy_device *d) { d->pmu.registered =3D false; + + disable_active_events(&d->pmu); perf_pmu_unregister(&d->pmu.base); =20 pr_info("Device released: %s\n", d->pmu.name); --=20 2.46.2