Skip to contentSkip to conversation

Add GPU work period support for Xe driver

PATCH Superseded Aakash Deep Sarkar 2 participants 11 messages Sep 25, 2025

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover Add GPU work period support for Xe driver

This patch series implements the Android VSR requirement GPU work period event for the Intel Xe driver.

|GpuWorkPeriodEvent| defines a non-overlapping, non-zero period
of time from |start_time_ns| (inclusive) until |end_time_ns|
(exclusive) for a given |uid|, and includes details of how much
work the GPU was performing for |uid| during the period. When
GPU work for a given |uid| runs on the GPU, the driver must track

one or more periods that cover the time where the work was running, and emit events soon after.

Full requirement is defined in the following file:
https://cs.android.com/android/platform/superproject/main/+\
main:frameworks/native/services/gpuservice/gpuwork/bpfprogs/gpuWork.c;l=35

The requirement is implemented using a delayed worker thread per user id instance to accumulate its runtime on the gpu and emit the event. Each user id instance is tracked using an xe_user structure and the runtime is updated every time the kworker is executed for this uid. The delay period is hardcoded to 500 msecs.

The runtime on the gpu is collected for each xe file individually inside the function xe_exec_queue_update_run_ticks and accumulated into the corresponding xe_user active_duration_ns field. The HW Context timestamp field in the GTT is used to derive the runtime in clock ticks and then converted into nanosecs before updating the active duration.

Aakash Deep Sarkar (8):
drm/xe: Add a new xe_user structure drm/xe: Add xe_gt_clock_interval_to_ns function drm/xe: Add a trace point for GPU work period drm/xe: Modify xe_exec_queue_update_run_ticks drm/xe: Handle xe_user creation and removal drm/xe: Implement xe_work_period_worker drm/xe: Add a Kconfig option for GPU work period drm/xe: Handle xe_work_period destruction

 drivers/gpu/drm/xe/Makefile          |   2 +
 drivers/gpu/drm/xe/xe_device.c       |  32 ++++
 drivers/gpu/drm/xe/xe_device_types.h |  19 +++
 drivers/gpu/drm/xe/xe_exec_queue.c   |   8 +
 drivers/gpu/drm/xe/xe_gt_clock.c     |  14 ++
 drivers/gpu/drm/xe/xe_gt_clock.h     |   1 +
 drivers/gpu/drm/xe/xe_pm.c           |   5 +
 drivers/gpu/drm/xe/xe_user.c         | 246 +++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_user.h         | 129 ++++++++++++++
 drivers/gpu/trace/Kconfig            |  12 ++
 include/trace/gpu_work_period.h      |  59 +++++++
 11 files changed, 527 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/xe_user.c
 create mode 100644 drivers/gpu/drm/xe/xe_user.h
 create mode 100644 include/trace/gpu_work_period.h
Signature · 1 line
2.49.0
#1 lore raw

PATCH v4 1/8 drm/xe: Add a new xe_user structure

For Android GPU work period event we need to track the runtime on the GPU for each user id. This means we can have multiple xe files opened by different processes/threads belonging to the same user id. All these xe files need to be grouped together so that one can easily identify these while calculating the run time for the given user id.

Currently, the xe driver doesn't record the user id of the calling process. Also, all the xe files created using open call are clubbed together inside the xe device structure with no way to distinguish between them based on the user id of the calling process.

To remedy these limitations we are adding another layer of indirection between xe device and xe file. xe device will now have a list of xe users each with a given user id; and each xe user will have a list of xe files each of which is created by a process that is associated with this user id.

The lifetime of the xe user structure should be between when a process with a new user id has opened the xe device; and when the last xe file belonging to this user id is closed.

---
 drivers/gpu/drm/xe/Makefile  |  2 +
 drivers/gpu/drm/xe/xe_user.c | 59 ++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_user.h | 81 ++++++++++++++++++++++++++++++++++++
 3 files changed, 142 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/xe_user.c
 create mode 100644 drivers/gpu/drm/xe/xe_user.h
drivers/gpu/drm/xe/Makefile +2 -0
index d9c6cf0f189e..ff6b584f3293 100644
@@ -333,6 +333,8 @@ ifeq ($(CONFIG_DEBUG_FS),y)
 
 	xe-$(CONFIG_PCI_IOV) += xe_gt_sriov_pf_debugfs.o
 
+	xe-y += xe_user.o
+
 	xe-$(CONFIG_DRM_XE_DISPLAY) += \
 		i915-display/intel_display_debugfs.o \
 		i915-display/intel_display_debugfs_params.o \
drivers/gpu/drm/xe/xe_user.c new file +59 -0

59 lines not on this page · raw message

drivers/gpu/drm/xe/xe_user.h new file +81 -0

81 lines not on this page · raw message

Signature · 1 line
2.49.0
#2 lore raw

PATCH v4 2/8 drm/xe: Add xe_gt_clock_interval_to_ns function

In reply to Aakash Deep Sarkar

The runtime of a user id in the GPU work period event are required to be given in nanosec unit. Since we want to use the HW Context timestamp register to derive the runtime for a context, we need a way to convert from GT clock ticks to nano seconds.

---
 drivers/gpu/drm/xe/xe_gt_clock.c | 14 ++++++++++++++
 drivers/gpu/drm/xe/xe_gt_clock.h |  1 +
 2 files changed, 15 insertions(+)
drivers/gpu/drm/xe/xe_gt_clock.c +14 -0
index 4f011d1573c6..17c1cc6bff5a 100644
@@ -110,3 +110,17 @@ u64 xe_gt_clock_interval_to_ms(struct xe_gt *gt, u64 count)
 {
 	return div_u64_roundup(count * MSEC_PER_SEC, gt->info.reference_clock);
 }
+
+/**
+ * xe_gt_clock_interval_to_ns - Convert sampled GT clock ticks to nanosec
+ *
+ * @gt: the &xe_gt
+ * @count: count of GT clock ticks
+ *
+ * Returns: time in nanosec
+ */
+u64 xe_gt_clock_interval_to_ns(struct xe_gt *gt, u64 count)
+{
+	return div_u64_roundup(count * NSEC_PER_SEC, gt->info.reference_clock);
+}
+
drivers/gpu/drm/xe/xe_gt_clock.h +1 -0
index 3adeb7baaca4..bd87971bce97 100644
@@ -12,5 +12,6 @@ struct xe_gt;
 
 int xe_gt_clock_init(struct xe_gt *gt);
 u64 xe_gt_clock_interval_to_ms(struct xe_gt *gt, u64 count);
+u64 xe_gt_clock_interval_to_ns(struct xe_gt *gt, u64 count);
 
 #endif
Signature · 1 line
2.49.0
#3 lore raw

PATCH v4 3/8 drm/xe: Add a trace point for GPU work period

In reply to Aakash Deep Sarkar

The GPU work period event is required to have the following format:

Defines the structure of the kernel tracepoint:
/sys/kernel/tracing/events/power/gpu_work_period

A value that uniquely identifies the GPU within the system.
uint32_t gpu_id;

The UID of the application (i.e. persistent, unique ID of the Android app) that submitted work to the GPU.
uint32_t uid;

The start time of the period in nanoseconds. The clock must be CLOCK_MONOTONIC_RAW, as returned by the ktime_get_raw_ns(void) function.
uint64_t start_time_ns;

The end time of the period in nanoseconds. The clock must be CLOCK_MONOTONIC_RAW, as returned by the ktime_get_raw_ns(void) function.
uint64_t end_time_ns;

The amount of time the GPU was running GPU work for |uid| during the period, in nanoseconds, without double-counting parallel GPU work for the same |uid|. For example, this might include the amount of time the GPU spent performing shader work (vertex work, fragment work, etc.) for |uid|.
uint64_t total_active_duration_ns;

---
 include/trace/gpu_work_period.h | 59 +++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)
 create mode 100644 include/trace/gpu_work_period.h
include/trace/gpu_work_period.h new file +59 -0
new file mode 100644
index 000000000000..e06467625705
@@ -0,0 +1,59 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2025 Intel Corporation
+ */
+
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM power
+
+#if !defined(_TRACE_GPU_WORK_PERIOD_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_GPU_WORK_PERIOD_H
+
+#include <linux/tracepoint.h>
+
+TRACE_EVENT(gpu_work_period,
+
+	TP_PROTO(
+		u32 gpu_id,
+		u32 uid,
+		u64 start_time_ns,
+		u64 end_time_ns,
+		u64 total_active_duration_ns
+	),
+
+	TP_ARGS(gpu_id, uid, start_time_ns, end_time_ns, total_active_duration_ns),
+
+	TP_STRUCT__entry(
+		__field(u32, gpu_id)
+		__field(u32, uid)
+		__field(u64, start_time_ns)
+		__field(u64, end_time_ns)
+		__field(u64, total_active_duration_ns)
+	),
+
+	TP_fast_assign(
+		__entry->gpu_id = gpu_id;
+		__entry->uid = uid;
+		__entry->start_time_ns = start_time_ns;
+		__entry->end_time_ns = end_time_ns;
+		__entry->total_active_duration_ns = total_active_duration_ns;
+	),
+
+	TP_printk("gpu_id=%u uid=%u start_time_ns=%llu end_time_ns=%llu total_active_duration_ns=%llu",
+		__entry->gpu_id,
+		__entry->uid,
+		__entry->start_time_ns,
+		__entry->end_time_ns,
+		__entry->total_active_duration_ns)
+);
+
+#endif /* _TRACE_GPU_WORK_PERIOD_H */
+
+/* This part must be outside protection */
+
+#undef TRACE_INCLUDE_FILE
+#define TRACE_INCLUDE_FILE gpu_work_period
+#undef TRACE_INCLUDE_PATH
+#define TRACE_INCLUDE_PATH .
+
+#include <trace/define_trace.h>
Signature · 1 line
2.49.0
#4 lore raw

PATCH v4 4/8 drm/xe: Modify xe_exec_queue_update_run_ticks

In reply to Aakash Deep Sarkar

For GPU work period event we need to record the run time of a context on the GPU in nanosecs. In the present xe driver code, we only record the run time in clock ticks and separately for each engine class.

So, we are adding a uint64 variable |active_duration_ns| in the xe file structure where we can record the cumulative run time in ns of all the engines for this context. The intent here is to add up the |active_duration_ns| in all the xe files belonging to a given user id to derive the run time for that user id.

---
 drivers/gpu/drm/xe/xe_device_types.h | 3 +++
 drivers/gpu/drm/xe/xe_exec_queue.c   | 7 +++++++
 2 files changed, 10 insertions(+)
drivers/gpu/drm/xe/xe_device_types.h +3 -0
index a6c361db11d9..e6ecfb3f7f38 100644
@@ -678,6 +678,9 @@ struct xe_file {
 	/** @run_ticks: hw engine class run time in ticks for this drm client */
 	u64 run_ticks[XE_ENGINE_CLASS_MAX];
 
+	/** @active_duration_ns: total run time in ns for this xe file */
+	u64 active_duration_ns;
+
 	/** @client: drm client */
 	struct xe_drm_client *client;
 
drivers/gpu/drm/xe/xe_exec_queue.c +7 -0
index 37b2b93b73d6..6eb34c62c779 100644
@@ -15,6 +15,7 @@
 #include "xe_dep_scheduler.h"
 #include "xe_device.h"
 #include "xe_gt.h"
+#include "xe_gt_clock.h"
 #include "xe_hw_engine_class_sysfs.h"
 #include "xe_hw_engine_group.h"
 #include "xe_hw_fence.h"
@@ -887,6 +888,8 @@ void xe_exec_queue_update_run_ticks(struct xe_exec_queue *q)
 {
 	struct xe_device *xe = gt_to_xe(q->gt);
 	struct xe_lrc *lrc;
+	struct xe_gt *gt = q->gt;
+
 	u64 old_ts, new_ts;
 	int idx;
 
@@ -912,6 +915,10 @@ void xe_exec_queue_update_run_ticks(struct xe_exec_queue *q)
 	new_ts = xe_lrc_update_timestamp(lrc, &old_ts);
 	q->xef->run_ticks[q->class] += (new_ts - old_ts) * q->width;
 
+	// Accumulate the runtime in nanosec for this queue into the xe file.
+	q->xef->active_duration_ns +=
+		xe_gt_clock_interval_to_ns(gt, (new_ts - old_ts));
+
 	drm_dev_exit(idx);
 }
 
Signature · 1 line
2.49.0
#5 lore raw

PATCH v4 5/8 drm/xe: Handle xe_user creation and removal

In reply to Aakash Deep Sarkar

We want our xe user structure to be created when a new user id opens the xe device node and to be destroyed when the final xe file with this uid is closed. In other words the xe_user structure for a uid should remain in scope as long as any process with this uid has an open xe file descriptor.

To implement this we maintain an xarray of xe user structures inside our xe device instance. Whenever a new xe file is created via an open call, we check if the calling process' uid is already present in our xarray. If so, we increment the refcount for the associated xe user and add this xe file to the list of xe files belonging to this xe user. Otherwise, we allocate a new xe user structure for this uid and initialize its file list with this xe file.

Whenever an xe file is destroyed, we decrement the refcount of the associated xe user. When the last xe file in the xe user's file list is destroyed, the xe user refcount should drop to zero and the xe user should be cleaned up. During the cleanup path we remove the xarray entry for this xe user in our xe device and free up its memory.

---
 drivers/gpu/drm/xe/xe_device.c       | 23 +++++++++
 drivers/gpu/drm/xe/xe_device_types.h | 16 ++++++
 drivers/gpu/drm/xe/xe_user.c         | 76 +++++++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_user.h         | 12 ++++-
 4 files changed, 124 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_device.c +23 -0
index fdb7b7498920..258b87403596 100644
@@ -64,6 +64,7 @@
 #include "xe_tile.h"
 #include "xe_ttm_stolen_mgr.h"
 #include "xe_ttm_sys_mgr.h"
+#include "xe_user.h"
 #include "xe_vm.h"
 #include "xe_vm_madvise.h"
 #include "xe_vram.h"
@@ -79,9 +80,13 @@ static int xe_file_open(struct drm_device *dev, struct drm_file *file)
 {
 	struct xe_device *xe = to_xe_device(dev);
 	struct xe_drm_client *client;
+	struct xe_user *user;
 	struct xe_file *xef;
 	int ret = -ENOMEM;
+	int uid = -EINVAL;
+	u32 idx;
 	struct task_struct *task = NULL;
+	const struct cred *cred = NULL;
 
 	xef = kzalloc(sizeof(*xef), GFP_KERNEL);
 	if (!xef)
@@ -106,8 +111,16 @@ static int xe_file_open(struct drm_device *dev, struct drm_file *file)
 	file->driver_priv = xef;
 	kref_init(&xef->refcount);
 
+	INIT_LIST_HEAD(&xef->user_link);
+
 	task = get_pid_task(rcu_access_pointer(file->pid), PIDTYPE_PID);
 	if (task) {
+		cred = get_task_cred(task);
+		if (cred) {
+			uid = (unsigned int) cred->euid.val;
+			xe_user_init(xe, xef, uid);
+			put_cred(cred);
+		}
 		xef->process_name = kstrdup(task->comm, GFP_KERNEL);
 		xef->pid = task->pid;
 		put_task_struct(task);
@@ -127,6 +140,12 @@ static void xe_file_destroy(struct kref *ref)
 
 	xe_drm_client_put(xef->client);
 	kfree(xef->process_name);
+
+	mutex_lock(&xef->user->filelist_lock);
+	list_del(&xef->user_link);
+	mutex_unlock(&xef->user->filelist_lock);
+
+	xe_user_put(xef->user);
 	kfree(xef);
 }
 
@@ -466,6 +485,10 @@ struct xe_device *xe_device_create(struct pci_dev *pdev,
 
 	xa_init_flags(&xe->usm.asid_to_vm, XA_FLAGS_ALLOC);
 
+	xa_init_flags(&xe->work_period.users, XA_FLAGS_ALLOC1);
+
+	mutex_init(&xe->work_period.lock);
+
 	if (IS_ENABLED(CONFIG_DRM_XE_DEBUG)) {
 		/* Trigger a large asid and an early asid wrap. */
 		u32 asid;
drivers/gpu/drm/xe/xe_device_types.h +16 -0
index e6ecfb3f7f38..e42b15aa4449 100644
@@ -608,6 +608,16 @@ struct xe_device {
 	atomic_t g2g_test_count;
 #endif
 
+	/**
+	 * @xe_work_period: Support for GPU work period tracepoint
+	 */
+	struct xe_work_period {
+		/** @users: list of users that have opened this xe device */
+		struct xarray users;
+		/** @lock: lock protecting this structure */
+		struct mutex lock;
+	} work_period;
+
 	/* private: */
 
 #if IS_ENABLED(CONFIG_DRM_XE_DISPLAY)
@@ -681,6 +691,12 @@ struct xe_file {
 	/** @active_duration_ns: total run time in ns for this xe file */
 	u64 active_duration_ns;
 
+	/** @user: pointer to struct xe_user associated with this xe file */
+	struct xe_user *user;
+
+	/** @user_link: link into xe_user::filelist */
+	struct list_head user_link;
+
 	/** @client: drm client */
 	struct xe_drm_client *client;
 
drivers/gpu/drm/xe/xe_user.c +74 -2

100 lines not on this page · raw message

drivers/gpu/drm/xe/xe_user.h +11 -1
index e52f66d3f3b0..ec4c0f2b862c 100644
@@ -8,8 +8,12 @@
 
 #include <linux/kref.h>
 #include <linux/list.h>
+#include <linux/mutex.h>
 #include <linux/workqueue.h>
 
+#include "xe_device.h"
+
+
 /**
  * This is a per process/user id structure for a xe device
  * client. It is allocated when a new process/app opens the
@@ -43,6 +47,11 @@ struct xe_user {
 	 */
 	struct work_struct work;
 
+	/**
+	 * @id: index of this user into the xe device users array
+	 */
+	u32 id;
+
 	/**
 	 * @uid: user id for this xe_user
 	 */
@@ -61,7 +70,8 @@ struct xe_user {
 	u64 last_timestamp_ns;
 };
 
-struct xe_user *xe_user_alloc(void);
+int xe_user_init(struct xe_device* xe, struct xe_file* xef, unsigned int uid);
+
 
 static inline struct xe_user *
 xe_user_get(struct xe_user *user)
Signature · 1 line
2.49.0
#6 lore raw

PATCH v4 6/8 drm/xe: Implement xe_work_period_worker

In reply to Aakash Deep Sarkar

The work of collecting the GPU run time for a given xe_user and emitting its event, is done by the xe_work_period_worker kworker. At the time of creation of a new xe_user, we simultaneously start a delayed kworker thread. The delay of execution is set to be 500 ms. After the completion of the work, the kworker schedules itself for the next execution. This is done as long as the reference to the xe_user pointer is valid.

During each execution cycle the xe_work_period_worker iterates over all the xe files in the xe_user::filelist and accumulate their corresponding GPU runtime into the xe_user::active_duration_ns; while also updating each of the xe_file::active_duration_ns. The total runtime for this uid in the current sampling period is the delta between the previous xe_user::active_duration_ns and the current xe_user::active_duration_ns.

We also record the current timestamp at the end of each invocation to xe_work_period_worker function in the xe_user::last_timestamp_ns. The sampling period for this uid is the delta between the previous timestamp and the current timestamp.

---
 drivers/gpu/drm/xe/xe_device.c |  13 ++--
 drivers/gpu/drm/xe/xe_pm.c     |   5 ++
 drivers/gpu/drm/xe/xe_user.c   | 127 +++++++++++++++++++++++++++++++--
 drivers/gpu/drm/xe/xe_user.h   |  21 ++++--
 4 files changed, 149 insertions(+), 17 deletions(-)
drivers/gpu/drm/xe/xe_device.c +6 -7
index 258b87403596..8e368346b6d4 100644
@@ -80,11 +80,9 @@ static int xe_file_open(struct drm_device *dev, struct drm_file *file)
 {
 	struct xe_device *xe = to_xe_device(dev);
 	struct xe_drm_client *client;
-	struct xe_user *user;
 	struct xe_file *xef;
 	int ret = -ENOMEM;
 	int uid = -EINVAL;
-	u32 idx;
 	struct task_struct *task = NULL;
 	const struct cred *cred = NULL;
 
@@ -141,11 +139,12 @@ static void xe_file_destroy(struct kref *ref)
 	xe_drm_client_put(xef->client);
 	kfree(xef->process_name);
 
-	mutex_lock(&xef->user->filelist_lock);
-	list_del(&xef->user_link);
-	mutex_unlock(&xef->user->filelist_lock);
-
-	xe_user_put(xef->user);
+	if (xef->user) {
+		mutex_lock(&xef->user->lock);
+		list_del(&xef->user_link);
+		xe_user_put(xef->user);
+		mutex_unlock(&xef->user->lock);
+	}
 	kfree(xef);
 }
 
drivers/gpu/drm/xe/xe_pm.c +5 -0
index 2b61a3b8257c..35d5433a9e0e 100644
@@ -26,6 +26,7 @@
 #include "xe_pxp.h"
 #include "xe_sriov_vf_ccs.h"
 #include "xe_trace.h"
+#include "xe_user.h"
 #include "xe_vm.h"
 #include "xe_wa.h"
 
@@ -532,6 +533,8 @@ int xe_pm_runtime_suspend(struct xe_device *xe)
 
 	xe_i2c_pm_suspend(xe);
 
+	xe_user_cancel_workers(xe);
+
 	xe_rpm_lockmap_release(xe);
 	xe_pm_write_callback_task(xe, NULL);
 	return 0;
@@ -584,6 +587,8 @@ int xe_pm_runtime_resume(struct xe_device *xe)
 
 	xe_i2c_pm_resume(xe, xe->d3cold.allowed);
 
+	xe_user_resume_workers(xe);
+
 	xe_irq_resume(xe);
 
 	for_each_gt(gt, xe, id)
drivers/gpu/drm/xe/xe_user.c +121 -6

159 lines not on this page · raw message

drivers/gpu/drm/xe/xe_user.h +17 -4
index ec4c0f2b862c..fc976beed2ad 100644
@@ -11,9 +11,11 @@
 #include <linux/mutex.h>
 #include <linux/workqueue.h>
 
-#include "xe_device.h"
+#include "xe_device_types.h"
 
 
+#define XE_WORK_PERIOD_INTERVAL 500
+
 /**
  * This is a per process/user id structure for a xe device
  * client. It is allocated when a new process/app opens the
@@ -32,9 +34,9 @@ struct xe_user {
 	struct xe_device *xe;
 
 	/**
-	 * @filelist_lock: lock protecting the filelist
+	 * @filelist_lock: lock protecting this structure
 	 */
-	struct mutex filelist_lock;
+	struct mutex lock;
 
 	/**
 	 * @filelist: list of xe files belonging to this xe user
@@ -45,7 +47,7 @@ struct xe_user {
 	 * @work: work to emit the gpu work period event for this
 	 * xe user
 	 */
-	struct work_struct work;
+	struct delayed_work delay_work;
 
 	/**
 	 * @id: index of this user into the xe device users array
@@ -72,6 +74,17 @@ struct xe_user {
 
 int xe_user_init(struct xe_device* xe, struct xe_file* xef, unsigned int uid);
 
+void xe_user_cancel_workers(struct xe_device* xe);
+
+void xe_user_resume_workers(struct xe_device* xe);
+
+static inline struct xe_user *
+xe_user_get_unless_zero(struct xe_user *user)
+{
+	if (kref_get_unless_zero(&user->refcount))
+		return user;
+	return NULL;
+}
 
 static inline struct xe_user *
 xe_user_get(struct xe_user *user)
Signature · 1 line
2.49.0
#7 lore raw

PATCH v4 7/8 drm/xe: Add a Kconfig option for GPU work period

In reply to Aakash Deep Sarkar

Since this requirement is intended only for Android, there's no reason to have it enabled by default in other distributions. So, better to have it guarded by a Kconfig option.

---
 drivers/gpu/drm/xe/Makefile        |  2 +-
 drivers/gpu/drm/xe/xe_device.c     |  1 -
 drivers/gpu/drm/xe/xe_exec_queue.c |  5 +++--
 drivers/gpu/drm/xe/xe_user.h       | 27 ++++++++++++++++++++++++++-
 drivers/gpu/trace/Kconfig          | 12 ++++++++++++
 5 files changed, 42 insertions(+), 5 deletions(-)
drivers/gpu/drm/xe/Makefile +1 -1
index ff6b584f3293..6fc23367bdfe 100644
@@ -333,7 +333,7 @@ ifeq ($(CONFIG_DEBUG_FS),y)
 
 	xe-$(CONFIG_PCI_IOV) += xe_gt_sriov_pf_debugfs.o
 
-	xe-y += xe_user.o
+	xe-$(CONFIG_TRACE_GPU_WORK_PERIOD) += xe_user.o
 
 	xe-$(CONFIG_DRM_XE_DISPLAY) += \
 		i915-display/intel_display_debugfs.o \
drivers/gpu/drm/xe/xe_device.c +0 -1
index 8e368346b6d4..30d9f2747eab 100644
@@ -485,7 +485,6 @@ struct xe_device *xe_device_create(struct pci_dev *pdev,
 	xa_init_flags(&xe->usm.asid_to_vm, XA_FLAGS_ALLOC);
 
 	xa_init_flags(&xe->work_period.users, XA_FLAGS_ALLOC1);
-
 	mutex_init(&xe->work_period.lock);
 
 	if (IS_ENABLED(CONFIG_DRM_XE_DEBUG)) {
drivers/gpu/drm/xe/xe_exec_queue.c +3 -2
index 6eb34c62c779..d5013d546348 100644
@@ -915,9 +915,10 @@ void xe_exec_queue_update_run_ticks(struct xe_exec_queue *q)
 	new_ts = xe_lrc_update_timestamp(lrc, &old_ts);
 	q->xef->run_ticks[q->class] += (new_ts - old_ts) * q->width;
 
-	// Accumulate the runtime in nanosec for this queue into the xe file.
+
+	// Accumulate the runtime in ns for this queue
 	q->xef->active_duration_ns +=
-		xe_gt_clock_interval_to_ns(gt, (new_ts - old_ts));
+			xe_gt_clock_interval_to_ns(gt, (new_ts - old_ts));
 
 	drm_dev_exit(idx);
 }
drivers/gpu/drm/xe/xe_user.h +26 -1
index fc976beed2ad..cf665995ddb8 100644
@@ -72,12 +72,38 @@ struct xe_user {
 	u64 last_timestamp_ns;
 };
 
+#if IS_ENABLED(CONFIG_TRACE_GPU_WORK_PERIOD)
+
 int xe_user_init(struct xe_device* xe, struct xe_file* xef, unsigned int uid);
 
 void xe_user_cancel_workers(struct xe_device* xe);
 
 void xe_user_resume_workers(struct xe_device* xe);
 
+void __xe_user_free(struct kref *kref);
+
+#else
+
+static inline
+int xe_user_init(struct xe_device* xe, struct xe_file* xef, unsigned int uid)
+{
+	return 0;
+}
+
+static inline void __xe_user_free(struct kref *kref)
+{
+}
+
+void xe_user_cancel_workers(struct xe_device* xe)
+{
+}
+
+void xe_user_resume_workers(struct xe_device* xe)
+{
+}
+
+#endif // CONFIG_TRACE_GPU_WORK_PERIOD
+
 static inline struct xe_user *
 xe_user_get_unless_zero(struct xe_user *user)
 {
@@ -93,7 +119,6 @@ xe_user_get(struct xe_user *user)
 	return user;
 }
 
-void __xe_user_free(struct kref *kref);
 
 static inline void xe_user_put(struct xe_user *user)
 {
drivers/gpu/trace/Kconfig +12 -0
index cd3d19c4a201..34f2e08cf1be 100644
@@ -11,3 +11,15 @@ config TRACE_GPU_MEM
 	  Tracepoint availability varies by GPU driver.
 
 	  If in doubt, say "N".
+
+config TRACE_GPU_WORK_PERIOD
+	bool "Enable GPU work period tracepoint"
+	default n
+	help
+	  Choose this option to enable tracepoint for tracking
+	  GPU usage based on the UID. Intended for performance
+	  profiling and required for Android.
+
+	  Tracepoint availability varies by GPU driver.
+
+	  If in doubt, say "N".
Signature · 1 line
2.49.0
#8 lore raw

PATCH v4 8/8 drm/xe: Handle xe_work_period destruction

In reply to Aakash Deep Sarkar

This adds the xe_work_period destruction procedure. We iterate over all entries in the xe::work_period::users xarray and cancel any pending delayed work. Then destroy the xarray itself.

---
 drivers/gpu/drm/xe/xe_device.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/gpu/drm/xe/xe_device.c +11 -0
index 30d9f2747eab..3b546d92fa03 100644
@@ -418,6 +418,8 @@ static struct drm_driver driver = {
 static void xe_device_destroy(struct drm_device *dev, void *dummy)
 {
 	struct xe_device *xe = to_xe_device(dev);
+	struct xe_user *user = NULL;
+	unsigned long i;
 
 	xe_bo_dev_fini(&xe->bo_device);
 
@@ -433,6 +435,15 @@ static void xe_device_destroy(struct drm_device *dev, void *dummy)
 	if (xe->destroy_wq)
 		destroy_workqueue(xe->destroy_wq);
 
+
+	mutex_lock(&xe->work_period.lock);
+	xa_for_each(&xe->work_period.users, i, user) {
+		if (cancel_delayed_work_sync(&user->delay_work))
+			xe_user_put(user);
+	}
+	xa_destroy(&xe->work_period.users);
+	mutex_unlock(&xe->work_period.lock);
+
 	ttm_device_fini(&xe->ttm);
 }
 
Signature · 1 line
2.49.0
#9 lore raw

✗ CI.checkpatch: warning for Add GPU work period support for Xe driver

In reply to Aakash Deep Sarkar
== Series Details ==

== Series Details ==

Series: Add GPU work period support for Xe driver
URL : https://patchwork.freedesktop.org/series/155039/
State : warning

== Summary ==

+ KERNEL=/kernel
+ git clone https://gitlab.freedesktop.org/drm/maintainer-tools mt Cloning into 'mt'...
warning: redirecting to https://gitlab.freedesktop.org/drm/maintainer-tools.git/
+ git -C mt rev-list -n1 origin/master fbd08a78c3a3bb17964db2a326514c69c1dca660
+ cd /kernel
+ git config --global --add safe.directory /kernel
+ git log -n1
commit 53d310324da6feee4fe665b7c08797cc4b474749

Author: Aakash Deep Sarkar <[email protected]>
Date:   Thu Sep 25 11:46:58 2025 +0000

drm/xe: Handle xe_work_period destruction

This adds the xe_work_period destruction procedure. We iterate over all entries in the xe::work_period::users xarray and cancel any pending delayed work. Then destroy the xarray itself.

+ /mt/dim checkpatch 7644974e648c270a56954b0344b1d0330698e1fa drm-intel 8968b14a78eb drm/xe: Add a new xe_user structure
-:45: WARNING:FILE_PATH_CHANGES: added, moved or deleted file(s), does MAINTAINERS need updating? #45:
new file mode 100644

total: 0 errors, 1 warnings, 0 checks, 148 lines checked
b1738739ca5e drm/xe: Add xe_gt_clock_interval_to_ns function
d3036ceba97d drm/xe: Add a trace point for GPU work period
-:36: WARNING:FILE_PATH_CHANGES: added, moved or deleted file(s), does MAINTAINERS need updating? #36:
new file mode 100644

-:55: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #55: FILE: include/trace/gpu_work_period.h:15:
+TRACE_EVENT(gpu_work_period,
+

-:56: CHECK:OPEN_ENDED_LINE: Lines should not end with a '(' #56: FILE: include/trace/gpu_work_period.h:16:
+ TP_PROTO(

-:66: CHECK:OPEN_ENDED_LINE: Lines should not end with a '(' #66: FILE: include/trace/gpu_work_period.h:26:
+ TP_STRUCT__entry(

-:74: CHECK:OPEN_ENDED_LINE: Lines should not end with a '(' #74: FILE: include/trace/gpu_work_period.h:34:
+ TP_fast_assign(

-:83: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #83: FILE: include/trace/gpu_work_period.h:43:
+ TP_printk("gpu_id=%u uid=%u start_time_ns=%llu end_time_ns=%llu total_active_duration_ns=%llu",
+ __entry->gpu_id,

total: 0 errors, 1 warnings, 5 checks, 59 lines checked d16b7945db6f drm/xe: Modify xe_exec_queue_update_run_ticks 1dd4d26bd60f drm/xe: Handle xe_user creation and removal -:69: CHECK:SPACING: No space is necessary after a cast #69: FILE: drivers/gpu/drm/xe/xe_device.c:121:
+ uid = (unsigned int) cred->euid.val;

-:197: ERROR:POINTER_LOCATION: "foo* bar" should be "foo *bar" #197: FILE: drivers/gpu/drm/xe/xe_user.c:87:
+int xe_user_init(struct xe_device* xe, struct xe_file* xef, unsigned int uid)

-:197: ERROR:POINTER_LOCATION: "foo* bar" should be "foo *bar" #197: FILE: drivers/gpu/drm/xe/xe_user.c:87:
+int xe_user_init(struct xe_device* xe, struct xe_file* xef, unsigned int uid)

-:199: ERROR:POINTER_LOCATION: "foo* bar" should be "foo *bar" #199: FILE: drivers/gpu/drm/xe/xe_user.c:89:
+ struct xe_user* user = NULL;

-:219: CHECK:LINE_SPACING: Please don't use multiple blank lines #219: FILE: drivers/gpu/drm/xe/xe_user.c:109:
+
+

-:255: CHECK:LINE_SPACING: Please don't use multiple blank lines #255: FILE: drivers/gpu/drm/xe/xe_user.h:16:
+
+

-:276: ERROR:POINTER_LOCATION: "foo* bar" should be "foo *bar" #276: FILE: drivers/gpu/drm/xe/xe_user.h:73:
+int xe_user_init(struct xe_device* xe, struct xe_file* xef, unsigned int uid);

-:276: ERROR:POINTER_LOCATION: "foo* bar" should be "foo *bar" #276: FILE: drivers/gpu/drm/xe/xe_user.h:73:
+int xe_user_init(struct xe_device* xe, struct xe_file* xef, unsigned int uid);

total: 5 errors, 0 warnings, 3 checks, 218 lines checked 6796cb7f5ea4 drm/xe: Implement xe_work_period_worker
-:122: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #122: FILE: drivers/gpu/drm/xe/xe_user.c:26:
+ schedule_delayed_work(&user->delay_work,
+ msecs_to_jiffies(XE_WORK_PERIOD_INTERVAL));

-:162: CHECK:BRACES: Blank lines aren't necessary after an open brace '{' #162: FILE: drivers/gpu/drm/xe/xe_user.c:64:
+ if (xe_pm_runtime_get_if_active(xe)) {
+

-:164: CHECK:BRACES: Blank lines aren't necessary after an open brace '{' #164: FILE: drivers/gpu/drm/xe/xe_user.c:66:
+ list_for_each_entry(xef, &user->filelist, user_link) {
+

-:166: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #166: FILE: drivers/gpu/drm/xe/xe_user.c:68:
+ wait_var_event(&xef->exec_queue.pending_removal,
+ !atomic_read(&xef->exec_queue.pending_removal));

-:218: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #218: FILE: drivers/gpu/drm/xe/xe_user.c:204:
+ if (!schedule_delayed_work(&user->delay_work,
+ msecs_to_jiffies(XE_WORK_PERIOD_INTERVAL)))

-:232: ERROR:POINTER_LOCATION: "foo* bar" should be "foo *bar" #232: FILE: drivers/gpu/drm/xe/xe_user.c:216:
+void xe_user_cancel_workers(struct xe_device* xe)

-:247: ERROR:POINTER_LOCATION: "foo* bar" should be "foo *bar" #247: FILE: drivers/gpu/drm/xe/xe_user.c:231:
+void xe_user_resume_workers(struct xe_device* xe)

-:256: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #256: FILE: drivers/gpu/drm/xe/xe_user.c:240:
+ if (!schedule_delayed_work(&user->delay_work,
+ msecs_to_jiffies(XE_WORK_PERIOD_INTERVAL)))

-:288: CHECK:UNCOMMENTED_DEFINITION: struct mutex definition without comment #288: FILE: drivers/gpu/drm/xe/xe_user.h:39:
+ struct mutex lock;

-:305: ERROR:POINTER_LOCATION: "foo* bar" should be "foo *bar" #305: FILE: drivers/gpu/drm/xe/xe_user.h:77:
+void xe_user_cancel_workers(struct xe_device* xe);

-:307: ERROR:POINTER_LOCATION: "foo* bar" should be "foo *bar" #307: FILE: drivers/gpu/drm/xe/xe_user.h:79:
+void xe_user_resume_workers(struct xe_device* xe);

total: 4 errors, 0 warnings, 7 checks, 258 lines checked aa9ad33380ef drm/xe: Add a Kconfig option for GPU work period -:46: CHECK:LINE_SPACING: Please don't use multiple blank lines #46: FILE: drivers/gpu/drm/xe/xe_exec_queue.c:918:

+

-:75: ERROR:POINTER_LOCATION: "foo* bar" should be "foo *bar" #75: FILE: drivers/gpu/drm/xe/xe_user.h:88:
+int xe_user_init(struct xe_device* xe, struct xe_file* xef, unsigned int uid)

-:75: ERROR:POINTER_LOCATION: "foo* bar" should be "foo *bar" #75: FILE: drivers/gpu/drm/xe/xe_user.h:88:
+int xe_user_init(struct xe_device* xe, struct xe_file* xef, unsigned int uid)

-:84: ERROR:POINTER_LOCATION: "foo* bar" should be "foo *bar" #84: FILE: drivers/gpu/drm/xe/xe_user.h:97:
+void xe_user_cancel_workers(struct xe_device* xe)

-:88: ERROR:POINTER_LOCATION: "foo* bar" should be "foo *bar" #88: FILE: drivers/gpu/drm/xe/xe_user.h:101:
+void xe_user_resume_workers(struct xe_device* xe)

total: 4 errors, 0 warnings, 1 checks, 87 lines checked 53d310324da6 drm/xe: Handle xe_work_period destruction -:30: CHECK:LINE_SPACING: Please don't use multiple blank lines #30: FILE: drivers/gpu/drm/xe/xe_device.c:439:

+

total: 0 errors, 0 warnings, 1 checks, 23 lines checked

#10 lore raw

✗ CI.KUnit: failure for Add GPU work period support for Xe driver

In reply to Aakash Deep Sarkar
== Series Details ==

== Series Details ==

Series: Add GPU work period support for Xe driver
URL : https://patchwork.freedesktop.org/series/155039/
State : failure

== Summary ==

+ trap cleanup EXIT
+ /kernel/tools/testing/kunit/kunit.py run --kunitconfig /kernel/drivers/gpu/drm/xe/.kunitconfig ERROR:root:In file included from ../drivers/gpu/drm/xe/xe_device.c:68:
../drivers/gpu/drm/xe/xe_user.h:97:6: warning: no previous prototype for ‘xe_user_cancel_workers’ [-Wmissing-prototypes]
97 | void xe_user_cancel_workers(struct xe_device* xe)

      |      ^~~~~~~~~~~~~~~~~~~~~~
../drivers/gpu/drm/xe/xe_user.h:101:6: warning: no previous prototype for ‘xe_user_resume_workers’ [-Wmissing-prototypes]
  101 | void xe_user_resume_workers(struct xe_device* xe)
      |      ^~~~~~~~~~~~~~~~~~~~~~
In file included from ../drivers/gpu/drm/xe/xe_pm.c:29:
../drivers/gpu/drm/xe/xe_user.h:97:6: warning: no previous prototype for ‘xe_user_cancel_workers’ [-Wmissing-prototypes]
   97 | void xe_user_cancel_workers(struct xe_device* xe)
      |      ^~~~~~~~~~~~~~~~~~~~~~
../drivers/gpu/drm/xe/xe_user.h:101:6: warning: no previous prototype for ‘xe_user_resume_workers’ [-Wmissing-prototypes]
  101 | void xe_user_resume_workers(struct xe_device* xe)
      |      ^~~~~~~~~~~~~~~~~~~~~~

ld: drivers/gpu/drm/xe/xe_pm.o: in function `xe_user_cancel_workers':
xe_pm.c:(.text+0x0): multiple definition of `xe_user_cancel_workers'; drivers/gpu/drm/xe/xe_device.o:xe_device.c:(.text+0xee0): first defined here
ld: drivers/gpu/drm/xe/xe_pm.o: in function `xe_user_resume_workers':
xe_pm.c:(.text+0x10): multiple definition of `xe_user_resume_workers'; drivers/gpu/drm/xe/xe_device.o:xe_device.c:(.text+0xef0): first defined here make[3]: *** [../scripts/Makefile.vmlinux_o:72: vmlinux.o] Error 1
make[2]: *** [/kernel/Makefile:1225: vmlinux_o] Error 2
make[1]: *** [/kernel/Makefile:248: __sub-make] Error 2
make: *** [Makefile:248: __sub-make] Error 2

[12:27:01] Configuring KUnit Kernel ...
Generating .config ...
Populating config with:

$ make ARCH=um O=.kunit olddefconfig
[12:27:05] Building KUnit Kernel ...
Populating config with:
$ make ARCH=um O=.kunit olddefconfig
Building with:
$ make all compile_commands.json scripts_gdb ARCH=um O=.kunit --jobs=48
+ cleanup
++ stat -c %u:%g /kernel
+ chown -R 1003:1003 /kernel