Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Gustavo Sousa <gustavo.sousa@intel.com>
To: Matt Roper <matthew.d.roper@intel.com>, <intel-xe@lists.freedesktop.org>
Cc: <matthew.d.roper@intel.com>
Subject: Re: [PATCH v2 17/30] drm/xe: Create scoped cleanup class for force_wake_get_any_engine()
Date: Thu, 13 Nov 2025 14:39:49 -0300	[thread overview]
Message-ID: <176305558905.3698.17600506635320107892@intel.com> (raw)
In-Reply-To: <20251110232017.1475869-49-matthew.d.roper@intel.com>

Quoting Matt Roper (2025-11-10 20:20:35-03:00)
>force_wake_get_any_engine() is a single-use function to pick any engine
>present on the platform and grab its forcewake.  The signature
>(returning a boolean success and both the engine pointer and a forcewake
>ref by reference) is a bit awkward.  Rewrite it such that the
>forcewake ref is the function's return value and the caller can
>determine success/failure by checking the engine pointer against NULL.
>
>With this new signature, the function can serve as a scoped cleanup
>class constructor, so define the corresponding class.  Note that if we
>fail to obtain forcewake (or if the platform somehow has no engines),
>the constructor can fail, returning an invalid fw_ref.  In such cases,
>fw_ref.fw will be NULL, making it clear that the reference is invalid;
>this fact can be used to create a thin wrapper around xe_force_wake_put
>that can be used as a destructor for this class.
>
>Signed-off-by: Matt Roper <matthew.d.roper@intel.com>
>---
> drivers/gpu/drm/xe/xe_drm_client.c | 52 +++++++++++++++++++-----------
> 1 file changed, 34 insertions(+), 18 deletions(-)
>
>diff --git a/drivers/gpu/drm/xe/xe_drm_client.c b/drivers/gpu/drm/xe/xe_drm_client.c
>index f931ff9b1ec0..9deb258ba204 100644
>--- a/drivers/gpu/drm/xe/xe_drm_client.c
>+++ b/drivers/gpu/drm/xe/xe_drm_client.c
>@@ -6,6 +6,7 @@
> 
> #include <drm/drm_print.h>
> #include <uapi/drm/xe_drm.h>
>+#include <linux/cleanup.h>
> #include <linux/kernel.h>
> #include <linux/slab.h>
> #include <linux/types.h>
>@@ -285,34 +286,48 @@ static struct xe_hw_engine *any_engine(struct xe_device *xe)
>         return NULL;
> }
> 
>-static bool force_wake_get_any_engine(struct xe_device *xe,
>-                                      struct xe_hw_engine **phwe,
>-                                      unsigned int *pfw_ref)
>+/*
>+ * Pick any engine and grab its forcewake.  On error phwe will be NULL and
>+ * the returned forcewake reference will be invalid.  Callers should check
>+ * phwe against NULL.
>+ */
>+static struct xe_force_wake_ref force_wake_get_any_engine(struct xe_device *xe,
>+                                                          struct xe_hw_engine **phwe)
> {
>         enum xe_force_wake_domains domain;
>-        unsigned int fw_ref;
>+        struct xe_force_wake_ref fw_ref = {};
>         struct xe_hw_engine *hwe;
>-        struct xe_force_wake *fw;
>+
>+        *phwe = NULL;
> 
>         hwe = any_engine(xe);
>         if (!hwe)
>-                return false;
>+                return fw_ref;        /* will be invalid */
> 
>         domain = xe_hw_engine_to_fw_domain(hwe);
>-        fw = gt_to_fw(hwe->gt);
> 
>-        fw_ref = xe_force_wake_get(fw, domain);
>-        if (!xe_force_wake_ref_has_domain(fw_ref, domain)) {
>-                xe_force_wake_put(fw, fw_ref);
>-                return false;
>-        }
>+        fw_ref.fw = gt_to_fw(hwe->gt);
>+        fw_ref.domains = xe_force_wake_get(fw_ref.fw, domain);

I think we should use xe_force_wake_constructor() here, to future-proof
this for any modification that we might decide to do in the way we force
wake CLASS constructors are implemented.

>+        if (xe_force_wake_ref_has_domain(fw_ref.domains, domain))
>+                *phwe = hwe;        /* valid forcewake */
> 
>-        *phwe = hwe;
>-        *pfw_ref = fw_ref;
>+        return fw_ref;
>+}
> 
>-        return true;
>+static void drop_fw_if_valid(struct xe_force_wake_ref fw_ref)
>+{
>+        /*
>+         * If force_wake_get_any_engine() fails, there's no real forcewake
>+         * reference to drop, and fw_ref.fw will be NULL.
>+         */
>+        if (fw_ref.fw)
>+                xe_force_wake_put(fw_ref.fw, fw_ref.domains);
> }
> 
>+DEFINE_CLASS(xe_force_wake_any_engine, struct xe_force_wake_ref,
>+             drop_fw_if_valid(_T), force_wake_get_any_engine(xe, phwe),
>+             struct xe_device *xe, struct xe_hw_engine **phwe);
>+

An alternative approach could be for xe_force_wake.h to have:

    DEFINE_CLASS(xe_force_wake_put_only, struct xe_force_wake_ref,
                 xe_force_wake_put(_T.fw, _T.domains),
                 fw_ref,
                 struct xe_force_wake_ref fw_ref);

Then, in this file, we would have something like:

    static void show_run_ticks(struct drm_printer *p, struct drm_file *file)
    {
        ...
        ...
        ...
        ...
        CLASS(xe_force_wake_put_only, fw_ref)(force_wake_get_any_engine(xe, &hwe));
        ...
        ...
        ...
    }

With that, we wouldn't need to create custom classes for special one-off
cases like this one.  What do you think?

PS: I also thought of using a DEFINE_FREE(), but I don't like the fact that
    the variable declaration would be explicit in the middle of
    show_run_ticks(). Using DEFINE_CLASS() we can hide that.

--
Gustavo Sousa

> static void show_run_ticks(struct drm_printer *p, struct drm_file *file)
> {
>         unsigned long class, i, gt_id, capacity[XE_ENGINE_CLASS_MAX] = { };
>@@ -322,7 +337,7 @@ static void show_run_ticks(struct drm_printer *p, struct drm_file *file)
>         struct xe_hw_engine *hwe;
>         struct xe_exec_queue *q;
>         u64 gpu_timestamp;
>-        unsigned int fw_ref;
>+        struct xe_force_wake_ref fw_ref;
> 
>         /*
>          * RING_TIMESTAMP registers are inaccessible in VF mode.
>@@ -340,7 +355,8 @@ static void show_run_ticks(struct drm_printer *p, struct drm_file *file)
>                        !atomic_read(&xef->exec_queue.pending_removal));
> 
>         xe_pm_runtime_get(xe);
>-        if (!force_wake_get_any_engine(xe, &hwe, &fw_ref)) {
>+        fw_ref = force_wake_get_any_engine(xe, &hwe);
>+        if (!hwe) {
>                 xe_pm_runtime_put(xe);
>                 return;
>         }
>@@ -360,7 +376,7 @@ static void show_run_ticks(struct drm_printer *p, struct drm_file *file)
> 
>         gpu_timestamp = xe_hw_engine_read_timestamp(hwe);
> 
>-        xe_force_wake_put(gt_to_fw(hwe->gt), fw_ref);
>+        xe_force_wake_put(gt_to_fw(hwe->gt), fw_ref.domains);
>         xe_pm_runtime_put(xe);
> 
>         for (class = 0; class < XE_ENGINE_CLASS_MAX; class++) {
>-- 
>2.51.1
>

  reply	other threads:[~2025-11-13 17:40 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-10 23:20 [PATCH v2 00/30] Scope-based forcewake and runtime PM Matt Roper
2025-11-10 23:20 ` [PATCH v2 01/30] drm/xe/forcewake: Improve kerneldoc Matt Roper
2025-11-12 14:04   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 02/30] drm/xe/eustall: Store forcewake reference in stream structure Matt Roper
2025-11-12 15:36   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 03/30] drm/xe/oa: " Matt Roper
2025-11-12 16:11   ` Gustavo Sousa
2025-11-13 17:10   ` Dixit, Ashutosh
2025-11-10 23:20 ` [PATCH v2 04/30] drm/xe/forcewake: Add scope-based cleanup for forcewake Matt Roper
2025-11-12 20:00   ` Gustavo Sousa
2025-11-12 21:01     ` Matt Roper
2025-11-12 21:16     ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 05/30] drm/xe/pm: Add scope-based cleanup helper for runtime PM Matt Roper
2025-11-12 19:53   ` Michal Wajdeczko
2025-11-12 21:48     ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 06/30] drm/xe/gt: Use scope-based cleanup Matt Roper
2025-11-13 12:26   ` Gustavo Sousa
2025-11-13 22:58     ` Matt Roper
2025-11-10 23:20 ` [PATCH v2 07/30] drm/xe/gt_idle: " Matt Roper
2025-11-13 12:39   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 08/30] drm/xe/guc: " Matt Roper
2025-11-13 12:46   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 09/30] drm/xe/guc_pc: " Matt Roper
2025-11-13 13:00   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 10/30] drm/xe/mocs: " Matt Roper
2025-11-13 13:30   ` Gustavo Sousa
2025-11-13 23:28     ` Matt Roper
2025-11-10 23:20 ` [PATCH v2 11/30] drm/xe/pat: Use scope-based forcewake Matt Roper
2025-11-13 13:37   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 12/30] drm/xe/pxp: Use scope-based cleanup Matt Roper
2025-11-13 13:40   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 13/30] drm/xe/gsc: " Matt Roper
2025-11-13 13:46   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 14/30] drm/xe/device: " Matt Roper
2025-11-13 14:04   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 15/30] drm/xe/devcoredump: " Matt Roper
2025-11-13 14:14   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 16/30] drm/xe/display: Use scoped-cleanup Matt Roper
2025-11-13 14:25   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 17/30] drm/xe: Create scoped cleanup class for force_wake_get_any_engine() Matt Roper
2025-11-13 17:39   ` Gustavo Sousa [this message]
2025-11-10 23:20 ` [PATCH v2 18/30] drm/xe/drm_client: Use scope-based cleanup Matt Roper
2025-11-10 23:20 ` [PATCH v2 19/30] drm/xe/gt_debugfs: " Matt Roper
2025-11-13 17:45   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 20/30] drm/xe/huc: Use scope-based forcewake Matt Roper
2025-11-13 17:46   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 21/30] drm/xe/query: " Matt Roper
2025-11-13 17:50   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 22/30] drm/xe/reg_sr: " Matt Roper
2025-11-13 17:51   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 23/30] drm/xe/vram: " Matt Roper
2025-11-10 23:57   ` [PATCH v2.1 " Matt Roper
2025-11-13 17:52     ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 24/30] drm/xe/bo: Use scope-based runtime PM Matt Roper
2025-11-13 17:54   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 25/30] drm/xe/ggtt: Use scope-based runtime pm Matt Roper
2025-11-13 17:55   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 26/30] drm/xe/hwmon: Use scope-based runtime PM Matt Roper
2025-11-13 18:01   ` Gustavo Sousa
2025-11-13 18:05     ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 27/30] drm/xe/sriov: " Matt Roper
2025-11-13 18:09   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 28/30] drm/xe/tests: " Matt Roper
2025-11-13 18:15   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 29/30] drm/xe/sysfs: Use scope-based runtime power management Matt Roper
2025-11-13 18:25   ` Gustavo Sousa
2025-11-10 23:20 ` [PATCH v2 30/30] drm/xe/debugfs: Use scope-based runtime PM Matt Roper
2025-11-13 18:30   ` Gustavo Sousa
2025-11-11  0:20 ` ✓ CI.KUnit: success for Scope-based forcewake and runtime PM (rev3) Patchwork
2025-11-11  0:57 ` ✓ Xe.CI.BAT: " Patchwork
2025-11-11 10:50 ` ✗ Xe.CI.Full: failure " Patchwork
2025-11-11 10:57 ` [PATCH v2 00/30] Scope-based forcewake and runtime PM Jani Nikula
2025-11-12 16:01   ` Matt Roper
2025-11-13 22:11 ` Matt Roper

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=176305558905.3698.17600506635320107892@intel.com \
    --to=gustavo.sousa@intel.com \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=matthew.d.roper@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