From patchwork Fri Oct 11 22:54:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas De Marchi X-Patchwork-Id: 13833238 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A5864D1A429 for ; Fri, 11 Oct 2024 22:55:18 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 940DF10EB27; Fri, 11 Oct 2024 22:55:15 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="c0ZgyYg7"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by gabe.freedesktop.org (Postfix) with ESMTPS id F308C10E2F1 for ; Fri, 11 Oct 2024 22:55:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1728687313; x=1760223313; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bCFYcrWX0SgVUaFsI3Uobc+i2ISz46PTsXMxUz3KOi0=; b=c0ZgyYg7+xvTOrKn4q5yH8Q+qVf6PPso3lphA8bTszamSXOMs5E6FraJ lbT50kP2bCSFVbqN9FiZf0MIMuP71loNvwL8edO5tyN0ci58ANIQMEiQz u05KeRDnvrQFY+f2nIADdOsrc90myLRJp3a9K8nQ1O1cXyQbK0rwRjAw5 /tpvDdJxSmye/Sz8lFhukG1mzLFes80XsEN5pfnu39gL5ARbrw4mX8eFp ieMMNMXTVrVNSnSZYh2S/SWwN/yfJ9MfBlVU7PYwJgWl8hsLNSEQnzJRJ yMw98bjTtHADlrMlJlmHkD4f+rGmDJchcDO7+LWn0tNDkifY96eZ2UJxR g==; X-CSE-ConnectionGUID: 2HGdbTvIQ/iGRgPJg/uzxA== X-CSE-MsgGUID: u3Ph+IDkTmuagOKBGRjtqw== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="53519743" X-IronPort-AV: E=Sophos;i="6.11,196,1725346800"; d="scan'208";a="53519743" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Oct 2024 15:55:12 -0700 X-CSE-ConnectionGUID: FHPKlllzQo25t7/Urux1HQ== X-CSE-MsgGUID: amuRU+xYRSG7WgmU8m1PAQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,196,1725346800"; d="scan'208";a="77040404" Received: from lucas-s2600cw.jf.intel.com ([10.165.21.196]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Oct 2024 15:55:12 -0700 From: Lucas De Marchi To: intel-gfx@lists.freedesktop.org Cc: Tvrtko Ursulin , Umesh Nerlige Ramappa , Lucas De Marchi Subject: [PATCH 1/8] squashed series - 20241008183501.1354695-1-lucas.demarchi@intel.com Date: Fri, 11 Oct 2024 15:54:23 -0700 Message-ID: <20241011225430.1219345-2-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241011225430.1219345-1-lucas.demarchi@intel.com> References: <20241011225430.1219345-1-lucas.demarchi@intel.com> MIME-Version: 1.0 X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" Patches 2 and 3 of https://lore.kernel.org/all/20241008183501.1354695-1-lucas.demarchi@intel.com/ with the unused module var removed (pending for v2 after review). 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 fb908843f2092..d6983dbf5a45b 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/unbind. When provided, + * pmu::put() is mandatory and it's driver responsibility to call perf_pmu_free() when + * resources can be released. + */ + struct pmu *(*get) (struct pmu *pmu); + + /* Optional: put a reference. See pmu::get() */ + void (*put) (struct pmu *pmu); }; enum perf_addr_filter_action_t { @@ -1104,6 +1115,7 @@ extern void perf_event_itrace_started(struct perf_event *event); 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); 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 3b8b85adb10a9..2d39005ba6d9c 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -5319,6 +5319,20 @@ static void perf_pending_task_sync(struct perf_event *event) rcuwait_wait_event(&event->pending_work_wait, !event->pending_work, TASK_UNINTERRUPTIBLE); } +static void pmu_module_put(struct pmu **ppmu) +{ + struct pmu *pmu = *ppmu; + struct module *module = pmu->module; + + if (pmu->put) + pmu->put(pmu); + + module_put(module); + + /* Can't touch pmu anymore/ */ + *ppmu = NULL; +} + static void _free_event(struct perf_event *event) { irq_work_sync(&event->pending_irq); @@ -5374,7 +5388,8 @@ static void _free_event(struct perf_event *event) put_ctx(event->ctx); exclusive_event_destroy(event); - module_put(event->pmu->module); + + pmu_module_put(&event->pmu); call_rcu(&event->rcu_head, free_event_rcu); } @@ -11512,10 +11527,12 @@ static int perf_event_idx_default(struct perf_event *event) return 0; } -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); /* * Let userspace know that this PMU supports address range filtering: @@ -11749,6 +11766,11 @@ int perf_pmu_register(struct pmu *pmu, const char *name, int type) goto free_pdc; } + if (WARN_ONCE((!!pmu->get) ^ (!!pmu->put), "Can not register a pmu with only get or put defined.\n")) { + ret = -EINVAL; + goto free_pdc; + } + pmu->name = name; if (type >= 0) @@ -11845,7 +11867,6 @@ 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) @@ -11853,8 +11874,11 @@ void perf_pmu_unregister(struct pmu *pmu) device_del(pmu->dev); put_device(pmu->dev); } - free_pmu_context(pmu); + mutex_unlock(&pmus_lock); + + if (!pmu->put) + perf_pmu_free(pmu); } EXPORT_SYMBOL_GPL(perf_pmu_unregister); @@ -11888,6 +11912,9 @@ static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) BUG_ON(!ctx); } + if (pmu->get) + pmu->get(pmu); + event->pmu = pmu; ret = pmu->event_init(event); @@ -11924,7 +11951,7 @@ static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) } if (ret) - module_put(pmu->module); + pmu_module_put(&pmu); return ret; } From patchwork Fri Oct 11 22:54:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas De Marchi X-Patchwork-Id: 13833236 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 35AD9D1A427 for ; Fri, 11 Oct 2024 22:55:17 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 70AC610EB22; Fri, 11 Oct 2024 22:55:15 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="XZHTmcuA"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1DFE810E11A for ; Fri, 11 Oct 2024 22:55:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1728687313; x=1760223313; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pF2qXNkY1hVYmlbykSV3HxAh3c6pyPlUOAG2KflXh4c=; b=XZHTmcuAzxraTW0dOAehVli7nUafbTIleTdLHb3fXFWcMdsVQiXaSbWJ ObBFcJGsSLLrNI37QLKKLJX2sVswUu2pwFz7Q23UWegHSh4bAQQv+/m4/ 35orBouDx7MQv+Y2JDl8mnK1pkVjbBzCul1qRdFRueVVVUYkfl0d3f5dI 6uPZF99hqV2xob1vE8ZI50GZup99Rv1T2n3oziJ7G6ov7xIHTSnHVb1IN YWEFG/MT+uKzdJ/w1+RQ3osIybMe3PBpf3d+sHsp+adZErpVA7RGH+oLf aKZnhlxeCaBi51OHHkpimMa3TMQOlK85Njgvw2IACeHu9/pSGyUMMcoCX w==; X-CSE-ConnectionGUID: Ob82vq5ERBecJySa7kuHxw== X-CSE-MsgGUID: YT6gjsJETjaTuC75ALiV6Q== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="53519745" X-IronPort-AV: E=Sophos;i="6.11,196,1725346800"; d="scan'208";a="53519745" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Oct 2024 15:55:13 -0700 X-CSE-ConnectionGUID: 3De7zyThQ+G+2EEZeHfb6Q== X-CSE-MsgGUID: HGruK5teT36RWoFEtNAT0Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,196,1725346800"; d="scan'208";a="77040407" Received: from lucas-s2600cw.jf.intel.com ([10.165.21.196]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Oct 2024 15:55:13 -0700 From: Lucas De Marchi To: intel-gfx@lists.freedesktop.org Cc: Tvrtko Ursulin , Umesh Nerlige Ramappa , Lucas De Marchi Subject: [PATCH 2/8] drm/i915/pmu: Let resource survive unbind Date: Fri, 11 Oct 2024 15:54:24 -0700 Message-ID: <20241011225430.1219345-3-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241011225430.1219345-1-lucas.demarchi@intel.com> References: <20241011225430.1219345-1-lucas.demarchi@intel.com> MIME-Version: 1.0 X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" There's no need to free the resources during unbind. Since perf events may still access them due to open events, it's safer to free them when dropping the last i915 reference. It will also allow to ask perf to release its own resources when dropping the last reference in a follow up change. Signed-off-by: Lucas De Marchi Reviewed-by: Matt Roper --- drivers/gpu/drm/i915/i915_pmu.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/i915/i915_pmu.c b/drivers/gpu/drm/i915/i915_pmu.c index 67b6cbdeff1da..4d05d98f51b8e 100644 --- a/drivers/gpu/drm/i915/i915_pmu.c +++ b/drivers/gpu/drm/i915/i915_pmu.c @@ -5,6 +5,7 @@ */ #include +#include #include "gt/intel_engine.h" #include "gt/intel_engine_pm.h" @@ -1148,6 +1149,17 @@ static void free_event_attributes(struct i915_pmu *pmu) pmu->pmu_attr = NULL; } +static void free_pmu(struct drm_device *dev, void *res) +{ + struct i915_pmu *pmu = res; + struct drm_i915_private *i915 = pmu_to_i915(pmu); + + free_event_attributes(pmu); + kfree(pmu->base.attr_groups); + if (IS_DGFX(i915)) + kfree(pmu->name); +} + static int i915_pmu_cpu_online(unsigned int cpu, struct hlist_node *node) { struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), cpuhp.node); @@ -1296,6 +1308,9 @@ void i915_pmu_register(struct drm_i915_private *i915) if (ret) goto err_unreg; + if (drmm_add_action(&i915->drm, free_pmu, pmu)) + goto err_unreg; + return; err_unreg: @@ -1330,11 +1345,7 @@ void i915_pmu_unregister(struct drm_i915_private *i915) hrtimer_cancel(&pmu->timer); i915_pmu_unregister_cpuhp_state(pmu); - perf_pmu_unregister(&pmu->base); + pmu->base.event_init = NULL; - kfree(pmu->base.attr_groups); - if (IS_DGFX(i915)) - kfree(pmu->name); - free_event_attributes(pmu); } From patchwork Fri Oct 11 22:54:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas De Marchi X-Patchwork-Id: 13833234 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1FC6CD0EE3F for ; Fri, 11 Oct 2024 22:55:15 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8AF9D10E2F1; Fri, 11 Oct 2024 22:55:14 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="Cc0vY/LE"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by gabe.freedesktop.org (Postfix) with ESMTPS id 3CCC210E2F1 for ; Fri, 11 Oct 2024 22:55:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1728687313; x=1760223313; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=JiwuPzjgKOW+98aSOHdvXirT8c2zPb6r2BSYy66ajaA=; b=Cc0vY/LEIwDXaZTRqZXSFKxQhUVAv0O3O+TNQ8GIlP2VjF8M9jRSm4tB bYGHcpCNdT+i8lyMUshyK39UXJ1kJ2Wav5wbJLyVP30W4eDrzy+TwIs14 niKSjn2Obdxb4wRe/YFjkE5oGE8i8o81/536C9NOpfEWwAw1vcpeBj1Kx Yx5n8L+HATzLpcLRy8o85UYKmNn+NHUM0x+Cy8SqsRl+th5VH4/n0Lne5 0D2lfLPEnQk54hEnwLs7vkVg3xPCkJ3o8wBbiBSEGj8/5p+F5Y2KFelrd FX9336hpgS/2KMlc6TLD4Cr3aYCxtiZfcercJ4LkvThgZ1B/N6w0cCOEW g==; X-CSE-ConnectionGUID: 6I9e9J8qTauYGg9/N/49hA== X-CSE-MsgGUID: ZNiYo10oQ8CY900dcbwxAg== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="53519748" X-IronPort-AV: E=Sophos;i="6.11,196,1725346800"; d="scan'208";a="53519748" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Oct 2024 15:55:13 -0700 X-CSE-ConnectionGUID: Y7C13T72STSMZ8qD41yjrA== X-CSE-MsgGUID: kst7psRUTM+NYYIBoLTDGA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,196,1725346800"; d="scan'208";a="77040410" Received: from lucas-s2600cw.jf.intel.com ([10.165.21.196]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Oct 2024 15:55:13 -0700 From: Lucas De Marchi To: intel-gfx@lists.freedesktop.org Cc: Tvrtko Ursulin , Umesh Nerlige Ramappa , Lucas De Marchi , stable@vger.kernel.org Subject: [PATCH 3/8] drm/i915/pmu: Fix crash due to use-after-free Date: Fri, 11 Oct 2024 15:54:25 -0700 Message-ID: <20241011225430.1219345-4-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241011225430.1219345-1-lucas.demarchi@intel.com> References: <20241011225430.1219345-1-lucas.demarchi@intel.com> MIME-Version: 1.0 X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" When an i915 PMU counter is enabled and the driver is then unbound, the PMU will be unregistered via perf_pmu_unregister(), however the event will still be alive. i915 currently tries to deal with this situation by: a) Marking the pmu as "closed" and shortcut the calls from perf b) Taking a reference from i915, that is put back when the event is destroyed. c) Setting event_init to NULL to avoid any further event (a) is ugly, but may be left as is since it protects not trying to access the HW that is now gone. Unless a pmu driver can call perf_pmu_unregister() and not receive any more calls, it's a necessary ugliness. (b) doesn't really work: when the event is destroyed and the i915 ref is put it may free the i915 object, that contains the pmu, not only the event. After event->destroy() callback, perf still expects the pmu object to be alive. Instead of pigging back on the event->destroy() to take and put the device reference, implement the new get()/put() on the pmu object for that purpose. (c) is only done to have a flag to avoid some function entrypoints when pmu is unregistered. Cc: stable@vger.kernel.org # 5.11+ Signed-off-by: Lucas De Marchi Reviewed-by: Matt Roper --- drivers/gpu/drm/i915/i915_pmu.c | 36 ++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/drivers/gpu/drm/i915/i915_pmu.c b/drivers/gpu/drm/i915/i915_pmu.c index 4d05d98f51b8e..dc9f753369170 100644 --- a/drivers/gpu/drm/i915/i915_pmu.c +++ b/drivers/gpu/drm/i915/i915_pmu.c @@ -515,15 +515,6 @@ static enum hrtimer_restart i915_sample(struct hrtimer *hrtimer) return HRTIMER_RESTART; } -static void i915_pmu_event_destroy(struct perf_event *event) -{ - struct i915_pmu *pmu = event_to_pmu(event); - struct drm_i915_private *i915 = pmu_to_i915(pmu); - - drm_WARN_ON(&i915->drm, event->parent); - - drm_dev_put(&i915->drm); -} static int engine_event_status(struct intel_engine_cs *engine, @@ -629,11 +620,6 @@ static int i915_pmu_event_init(struct perf_event *event) if (ret) return ret; - if (!event->parent) { - drm_dev_get(&i915->drm); - event->destroy = i915_pmu_event_destroy; - } - return 0; } @@ -872,6 +858,24 @@ static int i915_pmu_event_event_idx(struct perf_event *event) return 0; } +static struct pmu *i915_pmu_get(struct pmu *base) +{ + struct i915_pmu *pmu = container_of(base, struct i915_pmu, base); + struct drm_i915_private *i915 = pmu_to_i915(pmu); + + drm_dev_get(&i915->drm); + + return base; +} + +static void i915_pmu_put(struct pmu *base) +{ + struct i915_pmu *pmu = container_of(base, struct i915_pmu, base); + struct drm_i915_private *i915 = pmu_to_i915(pmu); + + drm_dev_put(&i915->drm); +} + struct i915_str_attribute { struct device_attribute attr; const char *str; @@ -1154,6 +1158,8 @@ static void free_pmu(struct drm_device *dev, void *res) struct i915_pmu *pmu = res; struct drm_i915_private *i915 = pmu_to_i915(pmu); + perf_pmu_free(&pmu->base); + free_event_attributes(pmu); kfree(pmu->base.attr_groups); if (IS_DGFX(i915)) @@ -1299,6 +1305,8 @@ void i915_pmu_register(struct drm_i915_private *i915) pmu->base.stop = i915_pmu_event_stop; pmu->base.read = i915_pmu_event_read; pmu->base.event_idx = i915_pmu_event_event_idx; + pmu->base.get = i915_pmu_get; + pmu->base.put = i915_pmu_put; ret = perf_pmu_register(&pmu->base, pmu->name, -1); if (ret) From patchwork Fri Oct 11 22:54:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas De Marchi X-Patchwork-Id: 13833235 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 584A2D1A428 for ; Fri, 11 Oct 2024 22:55:16 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 28F1510E2F2; Fri, 11 Oct 2024 22:55:15 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="c9DrT64V"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by gabe.freedesktop.org (Postfix) with ESMTPS id 5A89E10E11A for ; Fri, 11 Oct 2024 22:55:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1728687313; x=1760223313; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=i/3B0Cdj7yIU4EmqRdsbO1j9+et3Yx1ls3gqcDEX1f4=; b=c9DrT64VEnilt9iygfCp/A//R4IaU7JfbLgmHBcp/cjb9yByy0uumDib CHNd29wz2VsBSvJhph3lo0zBpWj4ahfts3jVkxLtTLrSTdhOQaL96Ggc0 DL8R48nt7ohmAuS5Q4azteX4zLi+XnBRyllB50GkGJIf8+/5mGu5uFSQt opxixFCsXyGqLmA1FF7dHUrNR9pqWI6YCPbQ7fRsqWxJEY8w8Bjck3bCH GMhFK20IcPuseg58e8nnXxAeQfRBmiKnabYY7uS7rM1D0lmLWL0xDdgpb P14r8MNAiBUgdvtWdpUlmPr6yJjoei/8A9psluN+SBfucLw15Ig17Fw4q g==; X-CSE-ConnectionGUID: rp2NDO4/R26ebZEf8LD3qg== X-CSE-MsgGUID: GW7TWRAPQh2O9hR27Redrw== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="53519751" X-IronPort-AV: E=Sophos;i="6.11,196,1725346800"; d="scan'208";a="53519751" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Oct 2024 15:55:13 -0700 X-CSE-ConnectionGUID: 9Pk7UY+mQvGq/AN6XWv00w== X-CSE-MsgGUID: uuDwm8fnSeCz6jSV6GCAGg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,196,1725346800"; d="scan'208";a="77040413" Received: from lucas-s2600cw.jf.intel.com ([10.165.21.196]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Oct 2024 15:55:13 -0700 From: Lucas De Marchi To: intel-gfx@lists.freedesktop.org Cc: Tvrtko Ursulin , Umesh Nerlige Ramappa , Lucas De Marchi Subject: [PATCH 4/8] drm/i915/pmu: Rename cpuhp_slot to cpuhp_state Date: Fri, 11 Oct 2024 15:54:26 -0700 Message-ID: <20241011225430.1219345-5-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241011225430.1219345-1-lucas.demarchi@intel.com> References: <20241011225430.1219345-1-lucas.demarchi@intel.com> MIME-Version: 1.0 X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" Both the documentation and most of other users call the return of cpuhp_setup_state_multi() as "state". Follow that. Signed-off-by: Lucas De Marchi Reviewed-by: Matt Roper --- drivers/gpu/drm/i915/i915_pmu.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/i915/i915_pmu.c b/drivers/gpu/drm/i915/i915_pmu.c index dc9f753369170..2e435f51867db 100644 --- a/drivers/gpu/drm/i915/i915_pmu.c +++ b/drivers/gpu/drm/i915/i915_pmu.c @@ -1211,7 +1211,7 @@ static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node) return 0; } -static enum cpuhp_state cpuhp_slot = CPUHP_INVALID; +static enum cpuhp_state cpuhp_state = CPUHP_INVALID; int i915_pmu_init(void) { @@ -1225,28 +1225,28 @@ int i915_pmu_init(void) pr_notice("Failed to setup cpuhp state for i915 PMU! (%d)\n", ret); else - cpuhp_slot = ret; + cpuhp_state = ret; return 0; } void i915_pmu_exit(void) { - if (cpuhp_slot != CPUHP_INVALID) - cpuhp_remove_multi_state(cpuhp_slot); + if (cpuhp_state != CPUHP_INVALID) + cpuhp_remove_multi_state(cpuhp_state); } static int i915_pmu_register_cpuhp_state(struct i915_pmu *pmu) { - if (cpuhp_slot == CPUHP_INVALID) + if (cpuhp_state == CPUHP_INVALID) return -EINVAL; - return cpuhp_state_add_instance(cpuhp_slot, &pmu->cpuhp.node); + return cpuhp_state_add_instance(cpuhp_state, &pmu->cpuhp.node); } static void i915_pmu_unregister_cpuhp_state(struct i915_pmu *pmu) { - cpuhp_state_remove_instance(cpuhp_slot, &pmu->cpuhp.node); + cpuhp_state_remove_instance(cpuhp_state, &pmu->cpuhp.node); } void i915_pmu_register(struct drm_i915_private *i915) From patchwork Fri Oct 11 22:54:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas De Marchi X-Patchwork-Id: 13833240 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7CE36D1A427 for ; Fri, 11 Oct 2024 22:55:19 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id AF73D10EB32; Fri, 11 Oct 2024 22:55:16 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="fRpg/FYO"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by gabe.freedesktop.org (Postfix) with ESMTPS id 7849A10E2F1 for ; Fri, 11 Oct 2024 22:55:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1728687313; x=1760223313; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Ca7AtEKUPmJo1ZukGXqDsWgvwFDw9CL3+eatjYRyMMs=; b=fRpg/FYOEDeRDaavl40Qabm6p3I6SArT/0fh5oarJNGQ6CVzppt/u393 Nzb3LNs+sKHVzaWGd/CWTkyL+UuRRnZ/vDwX0d4fE3BNPxyX7a8sFp4O4 NMlSmvVkS2GmBcujvhz2jMY9GMXPioXxT2qJvG58k/rSXsReo8GPQUOc+ mqAceEfexn/Z9bczYyO3DqTsYO8rwTMhBp3pC1WBZJlC1hpK1FUq4Hvfi amHIUdVh3CRdG3RSwrGgEUqAkJrUyh+PVVRteeTPeJpfUpsNu4T/GnZry 7rR+RZmxPxqnT4QU2w/cZayrBj++xDLnV6jDWN0Rqq9uDHjwL7tBn/zDi Q==; X-CSE-ConnectionGUID: 655cxpaDSXWNoJwA2g9JNQ== X-CSE-MsgGUID: Tyxf/fWyQ5eZ1c6JI2f1tw== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="53519753" X-IronPort-AV: E=Sophos;i="6.11,196,1725346800"; d="scan'208";a="53519753" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Oct 2024 15:55:13 -0700 X-CSE-ConnectionGUID: kJTEofXuRkWtYWY3Dfq6GQ== X-CSE-MsgGUID: ZKIYbwAZSyCGb0eclPzBSQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,196,1725346800"; d="scan'208";a="77040418" Received: from lucas-s2600cw.jf.intel.com ([10.165.21.196]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Oct 2024 15:55:13 -0700 From: Lucas De Marchi To: intel-gfx@lists.freedesktop.org Cc: Tvrtko Ursulin , Umesh Nerlige Ramappa , Lucas De Marchi Subject: [PATCH 5/8] drm/i915/pmu: Stop setting event_init to NULL Date: Fri, 11 Oct 2024 15:54:27 -0700 Message-ID: <20241011225430.1219345-6-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241011225430.1219345-1-lucas.demarchi@intel.com> References: <20241011225430.1219345-1-lucas.demarchi@intel.com> MIME-Version: 1.0 X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" Setting event_init to NULL is mostly done to detect when the driver is partially working: i915 probed, but pmu is not registered. However, checking for event_init is odd as it was supposed to always be set and kernel/events/ would just crash if it found it set to NULL. Since there's already a "closed" boolean, use that instead and extend it's meaning to unregistered/unregistering. Signed-off-by: Lucas De Marchi Reviewed-by: Matt Roper --- drivers/gpu/drm/i915/i915_pmu.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/drivers/gpu/drm/i915/i915_pmu.c b/drivers/gpu/drm/i915/i915_pmu.c index 2e435f51867db..409e10d8190a8 100644 --- a/drivers/gpu/drm/i915/i915_pmu.c +++ b/drivers/gpu/drm/i915/i915_pmu.c @@ -303,7 +303,7 @@ void i915_pmu_gt_parked(struct intel_gt *gt) { struct i915_pmu *pmu = >->i915->pmu; - if (!pmu->base.event_init) + if (pmu->closed) return; spin_lock_irq(&pmu->lock); @@ -325,7 +325,7 @@ void i915_pmu_gt_unparked(struct intel_gt *gt) { struct i915_pmu *pmu = >->i915->pmu; - if (!pmu->base.event_init) + if (pmu->closed) return; spin_lock_irq(&pmu->lock); @@ -1170,8 +1170,6 @@ static int i915_pmu_cpu_online(unsigned int cpu, struct hlist_node *node) { struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), cpuhp.node); - GEM_BUG_ON(!pmu->base.event_init); - /* Select the first online CPU as a designated reader. */ if (cpumask_empty(&i915_pmu_cpumask)) cpumask_set_cpu(cpu, &i915_pmu_cpumask); @@ -1184,8 +1182,6 @@ static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node) struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), cpuhp.node); unsigned int target = i915_pmu_target_cpu; - GEM_BUG_ON(!pmu->base.event_init); - /* * Unregistering an instance generates a CPU offline event which we must * ignore to avoid incorrectly modifying the shared i915_pmu_cpumask. @@ -1258,9 +1254,10 @@ void i915_pmu_register(struct drm_i915_private *i915) &i915_pmu_cpumask_attr_group, NULL }; - int ret = -ENOMEM; + pmu->closed = true; + if (GRAPHICS_VER(i915) <= 2) { drm_info(&i915->drm, "PMU not supported for this GPU."); return; @@ -1319,6 +1316,8 @@ void i915_pmu_register(struct drm_i915_private *i915) if (drmm_add_action(&i915->drm, free_pmu, pmu)) goto err_unreg; + pmu->closed = false; + return; err_unreg: @@ -1326,7 +1325,6 @@ void i915_pmu_register(struct drm_i915_private *i915) err_groups: kfree(pmu->base.attr_groups); err_attr: - pmu->base.event_init = NULL; free_event_attributes(pmu); err_name: if (IS_DGFX(i915)) @@ -1339,9 +1337,6 @@ void i915_pmu_unregister(struct drm_i915_private *i915) { struct i915_pmu *pmu = &i915->pmu; - if (!pmu->base.event_init) - return; - /* * "Disconnect" the PMU callbacks - since all are atomic synchronize_rcu * ensures all currently executing ones will have exited before we @@ -1354,6 +1349,4 @@ void i915_pmu_unregister(struct drm_i915_private *i915) i915_pmu_unregister_cpuhp_state(pmu); perf_pmu_unregister(&pmu->base); - - pmu->base.event_init = NULL; } From patchwork Fri Oct 11 22:54:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas De Marchi X-Patchwork-Id: 13833237 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DA811D0EE3F for ; Fri, 11 Oct 2024 22:55:17 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 71CDF10EB23; Fri, 11 Oct 2024 22:55:15 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="di57rFUQ"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by gabe.freedesktop.org (Postfix) with ESMTPS id 9884E10E11A for ; Fri, 11 Oct 2024 22:55:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1728687313; x=1760223313; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=m5dS+5k6nHDeYRuUHFtSHoO9mhFNZbi4+tik9ygdPL0=; b=di57rFUQ9cbzqUUZp5vAaF+wuvNqiu2SW2Uxq5dNlj8IN8nt0D/ibkWD VgR5c5jkuWVu7iKULYu6IKxZGmv4dvGYmhzJ02aijCNBOjXOJJpF5MOi9 xVjf7/xCMeHqMstdTvRsvYNK9ewfe2EWbb6UMS2kY3LtPfajzmiNHk8Zw UmXtvhZNwZOtzDY6hXbn6duxogsnnCHxmLDy3E/+S3sgHZVHD+s5KSRI4 mTNtn8O7HugSacLjH8OtZBmuXgXjISQ7FEedbIhGQLzPL3PhZiPeojSYy DduQV57A/dmzm92d/gpDfnFx4Px6EYqMhpiSKyDwbVXJX/2saUbPHdfV3 Q==; X-CSE-ConnectionGUID: Ngv5FwT4T1ScRkodgVA/7A== X-CSE-MsgGUID: sCQLgMGvTbGYeenA7a4Akg== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="53519755" X-IronPort-AV: E=Sophos;i="6.11,196,1725346800"; d="scan'208";a="53519755" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Oct 2024 15:55:13 -0700 X-CSE-ConnectionGUID: /jfS2qWIRfO1mMwRE6Nt7g== X-CSE-MsgGUID: aypwYGc/QmWbsaaMtfwDCA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,196,1725346800"; d="scan'208";a="77040421" Received: from lucas-s2600cw.jf.intel.com ([10.165.21.196]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Oct 2024 15:55:13 -0700 From: Lucas De Marchi To: intel-gfx@lists.freedesktop.org Cc: Tvrtko Ursulin , Umesh Nerlige Ramappa , Lucas De Marchi Subject: [PATCH 6/8] drm/i915/pmu: Replace closed with registered Date: Fri, 11 Oct 2024 15:54:28 -0700 Message-ID: <20241011225430.1219345-7-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241011225430.1219345-1-lucas.demarchi@intel.com> References: <20241011225430.1219345-1-lucas.demarchi@intel.com> MIME-Version: 1.0 X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" Since i915 calls perf_pmu_register/perf_pmu_unregister, let's call the variable "registered" so we can flip the logic and rely on it being false by default. Looking at other drivers, it's also more common. Examples: arch/x86/events/intel/uncore.c and drivers/powercap/intel_rapl_common.c. Signed-off-by: Lucas De Marchi --- drivers/gpu/drm/i915/i915_pmu.c | 25 +++++++++++++------------ drivers/gpu/drm/i915/i915_pmu.h | 4 ++-- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/drivers/gpu/drm/i915/i915_pmu.c b/drivers/gpu/drm/i915/i915_pmu.c index 409e10d8190a8..e28c29bae2d9a 100644 --- a/drivers/gpu/drm/i915/i915_pmu.c +++ b/drivers/gpu/drm/i915/i915_pmu.c @@ -303,7 +303,7 @@ void i915_pmu_gt_parked(struct intel_gt *gt) { struct i915_pmu *pmu = >->i915->pmu; - if (pmu->closed) + if (!pmu->registered) return; spin_lock_irq(&pmu->lock); @@ -325,7 +325,7 @@ void i915_pmu_gt_unparked(struct intel_gt *gt) { struct i915_pmu *pmu = >->i915->pmu; - if (pmu->closed) + if (!pmu->registered) return; spin_lock_irq(&pmu->lock); @@ -593,7 +593,7 @@ static int i915_pmu_event_init(struct perf_event *event) struct drm_i915_private *i915 = pmu_to_i915(pmu); int ret; - if (pmu->closed) + if (!pmu->registered) return -ENODEV; if (event->attr.type != event->pmu->type) @@ -686,7 +686,7 @@ static void i915_pmu_event_read(struct perf_event *event) struct hw_perf_event *hwc = &event->hw; u64 prev, new; - if (pmu->closed) { + if (!pmu->registered) { event->hw.state = PERF_HES_STOPPED; return; } @@ -812,7 +812,7 @@ static void i915_pmu_event_start(struct perf_event *event, int flags) { struct i915_pmu *pmu = event_to_pmu(event); - if (pmu->closed) + if (!pmu->registered) return; i915_pmu_enable(event); @@ -823,7 +823,7 @@ static void i915_pmu_event_stop(struct perf_event *event, int flags) { struct i915_pmu *pmu = event_to_pmu(event); - if (pmu->closed) + if (!pmu->registered) goto out; if (flags & PERF_EF_UPDATE) @@ -839,7 +839,7 @@ static int i915_pmu_event_add(struct perf_event *event, int flags) { struct i915_pmu *pmu = event_to_pmu(event); - if (pmu->closed) + if (!pmu->registered) return -ENODEV; if (flags & PERF_EF_START) @@ -1186,7 +1186,7 @@ static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node) * Unregistering an instance generates a CPU offline event which we must * ignore to avoid incorrectly modifying the shared i915_pmu_cpumask. */ - if (pmu->closed) + if (!pmu->registered) return 0; if (cpumask_test_and_clear_cpu(cpu, &i915_pmu_cpumask)) { @@ -1256,8 +1256,6 @@ void i915_pmu_register(struct drm_i915_private *i915) }; int ret = -ENOMEM; - pmu->closed = true; - if (GRAPHICS_VER(i915) <= 2) { drm_info(&i915->drm, "PMU not supported for this GPU."); return; @@ -1316,7 +1314,7 @@ void i915_pmu_register(struct drm_i915_private *i915) if (drmm_add_action(&i915->drm, free_pmu, pmu)) goto err_unreg; - pmu->closed = false; + pmu->registered = true; return; @@ -1337,12 +1335,15 @@ void i915_pmu_unregister(struct drm_i915_private *i915) { struct i915_pmu *pmu = &i915->pmu; + if (!pmu->registered) + return; + /* * "Disconnect" the PMU callbacks - since all are atomic synchronize_rcu * ensures all currently executing ones will have exited before we * proceed with unregistration. */ - pmu->closed = true; + pmu->registered = false; synchronize_rcu(); hrtimer_cancel(&pmu->timer); diff --git a/drivers/gpu/drm/i915/i915_pmu.h b/drivers/gpu/drm/i915/i915_pmu.h index 41af038c37388..3c1cf594954d9 100644 --- a/drivers/gpu/drm/i915/i915_pmu.h +++ b/drivers/gpu/drm/i915/i915_pmu.h @@ -68,9 +68,9 @@ struct i915_pmu { */ struct pmu base; /** - * @closed: i915 is unregistering. + * @closed: PMU is registered and not in the unregistering process. */ - bool closed; + bool registered; /** * @name: Name as registered with perf core. */ From patchwork Fri Oct 11 22:54:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas De Marchi X-Patchwork-Id: 13833239 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F2817D1A42D for ; Fri, 11 Oct 2024 22:55:18 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2060B10EB2F; Fri, 11 Oct 2024 22:55:16 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="RhB0YOzz"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by gabe.freedesktop.org (Postfix) with ESMTPS id B64A110E2F1 for ; Fri, 11 Oct 2024 22:55:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1728687314; x=1760223314; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DKCTEAUKcc+m3zQPFPKjJibAT+1v6z2TBQ64gUoCZZM=; b=RhB0YOzz5cexcKalDxuEV14B/EKvvXVz541UbM/saiwfaXbJcC+lSiBA 2d/0Z7iVkl67nhYHUhtF12nLF+n68peyYCU4q6qtlBVQjA6jH/7KhBecl xawIjqHd4LNibytGJhKy7NiPmS9SdhJ9ZJoLQtVFr8J1SJh2kyN7IloPP q5xXSS5Za7ZNnCnpkpg7jgnrFjtpQ9Zm/QbAaH8Y4WNjSccu7suwzIX1K d0i0k0cNeyqK0lr+LHSB66M9LGuQEbcJJjC2viWKl67RCylHXw3skt1HJ r0tNr1vKJRoIlkFcqNT8mNb2dYxIb/KzPnIkwO7h7ql5WoSLv4rKa+m1E g==; X-CSE-ConnectionGUID: bpqZ4jicQnWqIXR5DU5lZw== X-CSE-MsgGUID: TcJigtnJQ1q/kHr9sU5B5A== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="53519757" X-IronPort-AV: E=Sophos;i="6.11,196,1725346800"; d="scan'208";a="53519757" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Oct 2024 15:55:13 -0700 X-CSE-ConnectionGUID: nyXh3GAGTRqA0F5pa0VyyA== X-CSE-MsgGUID: DNeaGxsUTmWfqzV5vTf3Ug== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,196,1725346800"; d="scan'208";a="77040425" Received: from lucas-s2600cw.jf.intel.com ([10.165.21.196]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Oct 2024 15:55:13 -0700 From: Lucas De Marchi To: intel-gfx@lists.freedesktop.org Cc: Tvrtko Ursulin , Umesh Nerlige Ramappa , Lucas De Marchi Subject: [PATCH 7/8] drm/i915/pmu: Remove pointless synchronize_rcu() call Date: Fri, 11 Oct 2024 15:54:29 -0700 Message-ID: <20241011225430.1219345-8-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241011225430.1219345-1-lucas.demarchi@intel.com> References: <20241011225430.1219345-1-lucas.demarchi@intel.com> MIME-Version: 1.0 X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" This is already done inside perf_pmu_unregister() - no need to do it before. Signed-off-by: Lucas De Marchi Reviewed-by: Matt Roper --- drivers/gpu/drm/i915/i915_pmu.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/drivers/gpu/drm/i915/i915_pmu.c b/drivers/gpu/drm/i915/i915_pmu.c index e28c29bae2d9a..2f8004ade6d36 100644 --- a/drivers/gpu/drm/i915/i915_pmu.c +++ b/drivers/gpu/drm/i915/i915_pmu.c @@ -1338,13 +1338,8 @@ void i915_pmu_unregister(struct drm_i915_private *i915) if (!pmu->registered) return; - /* - * "Disconnect" the PMU callbacks - since all are atomic synchronize_rcu - * ensures all currently executing ones will have exited before we - * proceed with unregistration. - */ + /* Disconnect the PMU callbacks */ pmu->registered = false; - synchronize_rcu(); hrtimer_cancel(&pmu->timer); From patchwork Fri Oct 11 22:54:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas De Marchi X-Patchwork-Id: 13833241 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EC995D1A427 for ; Fri, 11 Oct 2024 22:55:23 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 84F2910EB31; Fri, 11 Oct 2024 22:55:23 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="NktBmcbv"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by gabe.freedesktop.org (Postfix) with ESMTPS id C8F8710E11A for ; Fri, 11 Oct 2024 22:55:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1728687314; x=1760223314; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mutcSKtA888TEHP2IQEy7CNsZbPqnLXOfTeQqEdl6w4=; b=NktBmcbvrSSiT86MP5nusVmlRgnrdU0Rkfbij4ZZfYpznY+GLL4FW1CG M0qxTkOQJVrDI4PP5ZTUwt/LNsbEwshp+3G3lve9Cfj+V/Bs4JxJ0462c 67NT0UlzYutbprZDsWQ2wTFpQ4eC7ANS2dDV4dc640ijhjFMSJFnDQulS R/Y8CcWCqSv3zAscZ36o73Jn/qzPGQsTYfDOMH3SfbUnUQX1jKMV2xj4J FU4t2zBp06WPDKUsanNv1v0OwOu4kCeNlyjaaMw33TkguMipNVwA9sBBt cIlc7qxBX1f2LSn+HG6IYekXjdBXVLsU852yTe4NR64+JJQ2HlfuwQ+O4 w==; X-CSE-ConnectionGUID: Ac8M9+aUSia/ANk2sycnWA== X-CSE-MsgGUID: TDyKBbnVRBeFNcTZE2GNnQ== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="53519760" X-IronPort-AV: E=Sophos;i="6.11,196,1725346800"; d="scan'208";a="53519760" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Oct 2024 15:55:13 -0700 X-CSE-ConnectionGUID: Bak/NmrVR8awEN1n2G2Ftw== X-CSE-MsgGUID: s9R9AxFaQBW9Y71OB7ihxA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,196,1725346800"; d="scan'208";a="77040428" Received: from lucas-s2600cw.jf.intel.com ([10.165.21.196]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Oct 2024 15:55:13 -0700 From: Lucas De Marchi To: intel-gfx@lists.freedesktop.org Cc: Tvrtko Ursulin , Umesh Nerlige Ramappa , Lucas De Marchi Subject: [PATCH 8/8] drm/i915/pmu: Release open events when unregistering Date: Fri, 11 Oct 2024 15:54:30 -0700 Message-ID: <20241011225430.1219345-9-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241011225430.1219345-1-lucas.demarchi@intel.com> References: <20241011225430.1219345-1-lucas.demarchi@intel.com> MIME-Version: 1.0 X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" When unregistering the PMU, disable the active events. This allows userspace to see the change and possibly react on it, like reopening the fd. With perf-stat, "" starts to be printed: 2.007597571 500,909,500 ns i915_0000_03_00.0/rc6-residency/ 2.508569383 0 ns i915_0000_03_00.0/rc6-residency/ 3.009405012 ns i915_0000_03_00.0/rc6-residency/ Signed-off-by: Lucas De Marchi Reviewed-by: Matt Roper --- drivers/gpu/drm/i915/i915_pmu.c | 35 ++++++++++++++++++++++++++++++++- drivers/gpu/drm/i915/i915_pmu.h | 6 ++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/i915/i915_pmu.c b/drivers/gpu/drm/i915/i915_pmu.c index 2f8004ade6d36..1466464c119df 100644 --- a/drivers/gpu/drm/i915/i915_pmu.c +++ b/drivers/gpu/drm/i915/i915_pmu.c @@ -515,6 +515,18 @@ static enum hrtimer_restart i915_sample(struct hrtimer *hrtimer) return HRTIMER_RESTART; } +static void i915_pmu_event_destroy(struct perf_event *event) +{ + struct i915_pmu *pmu = event_to_pmu(event); + unsigned long idx; + struct perf_event *e; + + xa_for_each(&pmu->active_events, idx, e) + if (e == event) { + xa_erase(&pmu->active_events, idx); + break; + } +} static int engine_event_status(struct intel_engine_cs *engine, @@ -539,7 +551,6 @@ static int config_status(struct drm_i915_private *i915, u64 config) { struct intel_gt *gt = to_gt(i915); - unsigned int gt_id = config_gt_id(config); unsigned int max_gt_id = HAS_EXTRA_GT_LIST(i915) ? 1 : 0; @@ -591,6 +602,7 @@ static int i915_pmu_event_init(struct perf_event *event) { struct i915_pmu *pmu = event_to_pmu(event); struct drm_i915_private *i915 = pmu_to_i915(pmu); + u32 event_id; int ret; if (!pmu->registered) @@ -620,6 +632,13 @@ static int i915_pmu_event_init(struct perf_event *event) if (ret) return ret; + ret = xa_alloc(&pmu->active_events, &event_id, event, + xa_limit_32b, GFP_KERNEL); + if (ret) + return ret; + + event->destroy = i915_pmu_event_destroy; + return 0; } @@ -1263,6 +1282,7 @@ void i915_pmu_register(struct drm_i915_private *i915) spin_lock_init(&pmu->lock); hrtimer_init(&pmu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); + xa_init_flags(&pmu->active_events, XA_FLAGS_ALLOC); pmu->timer.function = i915_sample; pmu->cpuhp.cpu = -1; init_rc6(pmu); @@ -1331,6 +1351,17 @@ void i915_pmu_register(struct drm_i915_private *i915) drm_notice(&i915->drm, "Failed to register PMU!\n"); } +static void release_active_events(struct i915_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); + } +} + void i915_pmu_unregister(struct drm_i915_private *i915) { struct i915_pmu *pmu = &i915->pmu; @@ -1341,6 +1372,8 @@ void i915_pmu_unregister(struct drm_i915_private *i915) /* Disconnect the PMU callbacks */ pmu->registered = false; + release_active_events(pmu); + hrtimer_cancel(&pmu->timer); i915_pmu_unregister_cpuhp_state(pmu); diff --git a/drivers/gpu/drm/i915/i915_pmu.h b/drivers/gpu/drm/i915/i915_pmu.h index 3c1cf594954d9..dd4cac95d032b 100644 --- a/drivers/gpu/drm/i915/i915_pmu.h +++ b/drivers/gpu/drm/i915/i915_pmu.h @@ -10,6 +10,7 @@ #include #include #include +#include #include struct drm_i915_private; @@ -152,6 +153,11 @@ struct i915_pmu { * @pmu_attr: Memory block holding device attributes. */ void *pmu_attr; + + /** + * @active_events: Events currently open by perf. + */ + struct xarray active_events; }; #ifdef CONFIG_PERF_EVENTS