Skip to contentSkip to conversation

: Add GPU work period support for Xe driver

PATCH Superseded Aakash Deep Sarkar 4 participants 14 messages Sep 12, 2025 – Sep 15, 2025

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 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 (9):
Add a new xe_user structure
Add xe_gt_clock_interval_to_ns function Add a trace point for GPU work period Modify xe_exec_queue_update_run_ticks Handle xe_user creation and removal Implement xe_work_period_worker
Add a Kconfig option for GPU work period Handle xe_work_period destruction
Fix runtime PM handling of Xe work period worker

 drivers/gpu/drm/xe/Makefile                   |   2 +
 drivers/gpu/drm/xe/xe_device.c                |  90 +++++++++
 drivers/gpu/drm/xe/xe_device_types.h          |  19 ++
 drivers/gpu/drm/xe/xe_exec_queue.c            |  10 +
 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                    |   4 +
 .../drm/xe/xe_power_gpu_work_period_trace.h   |  61 ++++++
 drivers/gpu/drm/xe/xe_user.c                  | 188 ++++++++++++++++++
 drivers/gpu/drm/xe/xe_user.h                  | 121 +++++++++++
 drivers/gpu/trace/Kconfig                     |  12 ++
 11 files changed, 522 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/xe_power_gpu_work_period_trace.h
 create mode 100644 drivers/gpu/drm/xe/xe_user.c
 create mode 100644 drivers/gpu/drm/xe/xe_user.h
Signature · 1 line
2.49.0
#1 lore raw

PATCH v2 1/9 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 8e0c3412a757..89212fc7ef44 100644
@@ -325,6 +325,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 v2 2/9 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 v2 3/9 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;

---
 .../drm/xe/xe_power_gpu_work_period_trace.h   | 61 +++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/xe_power_gpu_work_period_trace.h
drivers/gpu/drm/xe/xe_power_gpu_work_period_trace.h new file +61 -0
new file mode 100644
index 000000000000..2de05f1b64f3
@@ -0,0 +1,61 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#ifndef _TRACE_POWER_GPU_WORK_PERIOD_INTEL
+#define _TRACE_POWER_GPU_WORK_PERIOD_INTEL
+#endif
+
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM power
+#undef TRACE_INCLUDE_FILE
+#define TRACE_INCLUDE_FILE xe_power_gpu_work_period_trace
+#undef TRACE_INCLUDE_PATH
+#define TRACE_INCLUDE_PATH .
+
+#if !defined(_TRACE_POWER_GPU_WORK_PERIOD_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_POWER_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_POWER_GPU_WORK_PERIOD_H */
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
Signature · 1 line
2.49.0
#4 lore raw

PATCH v2 4/9 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 01e8fa0d2f9f..c4f076a26291 100644
@@ -656,6 +656,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 2d10a53f701d..2ee8a475f5ed 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"
@@ -879,6 +880,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;
 
@@ -904,6 +907,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 v2 5/9 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       | 57 ++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_device_types.h | 16 ++++++++
 drivers/gpu/drm/xe/xe_user.c         | 28 +++++++++++++-
 drivers/gpu/drm/xe/xe_user.h         | 10 +++++
 4 files changed, 110 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_device.c +57 -0
index 3e0402dff423..bd4a1c5c57ca 100644
@@ -62,6 +62,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_vram.h"
 #include "xe_vram_types.h"
@@ -76,9 +77,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)
@@ -105,11 +110,53 @@ static int xe_file_open(struct drm_device *dev, struct drm_file *file)
 
 	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;
+			put_cred(cred);
+		}
 		xef->process_name = kstrdup(task->comm, GFP_KERNEL);
 		xef->pid = task->pid;
 		put_task_struct(task);
 	}
 
+	if (uid < 0)
+		return -ENOENT;
+
+	INIT_LIST_HEAD(&xef->user_link);
+	/*
+	 * Check if the calling process/uid has already been registered
+	 * with the xe device during a previous open call. If so then
+	 * take a reference to this xe user and add this xe file to the
+	 * filelist belonging to this xe user
+	 */
+	user = xe_user_lookup(xe, uid);
+	if (!user) {
+		/*
+		 * We couldn't find an existing xe user for the calling process.
+		 * Allocate a new struct xe_user and register it with this xe
+		 * device
+		 */
+		user = xe_user_alloc();
+		if (!user)
+			return -ENOMEM;
+
+		user->uid = uid;
+		user->last_timestamp_ns = ktime_get_raw_ns();
+		user->xe = xe;
+
+		ret = xa_alloc(&xe->work_period.users, &idx, user, xa_limit_32b, GFP_KERNEL);
+		if (ret < 0)
+			return ret;
+
+		user->id = idx;
+		drm_dev_get(&xe->drm);
+	}
+	mutex_lock(&user->filelist_lock);
+	list_add(&xef->user_link, &user->filelist);
+	mutex_unlock(&user->filelist_lock);
+	xef->user = user;
+
 	return 0;
 }
 
@@ -124,6 +171,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);
 }
 
@@ -458,6 +511,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 c4f076a26291..b375c4928a09 100644
@@ -576,6 +576,16 @@ struct xe_device {
 	atomic64_t global_total_pages;
 #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)
@@ -659,6 +669,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 +27 -1
index 8c285a68115a..5c7d21dfcc45 100644
@@ -4,6 +4,7 @@
  */
 
 #include <linux/slab.h>
+#include <drm/drm_drv.h>
 
 #include "xe_user.h"
 
@@ -39,7 +40,6 @@ struct xe_user *xe_user_alloc(void)
 	kref_init(&user->refcount);
 	mutex_init(&user->filelist_lock);
 	INIT_LIST_HEAD(&user->filelist);
-	//TODO: Add a hook into xe device
 	INIT_WORK(&user->work, work_period_worker);
 	return user;
 }
@@ -54,6 +54,32 @@ void __xe_user_free(struct kref *kref)
 {
 	struct xe_user *user =
 		container_of(kref, struct xe_user, refcount);
+	struct xe_device *xe = user->xe;
+	void *lookup;
 
+	mutex_lock(&xe->work_period.lock);
+	lookup = xa_erase(&xe->work_period.users, user->id);
+	xe_assert(xe, lookup == user);
+	mutex_unlock(&xe->work_period.lock);
+
+	drm_dev_put(&user->xe->drm);
 	kfree(user);
 }
+
+struct xe_user *xe_user_lookup(struct xe_device *xe, u32 uid)
+{
+	struct xe_user *user = NULL;
+	unsigned long i;
+
+	mutex_lock(&xe->work_period.lock);
+	xa_for_each(&xe->work_period.users, i, user) {
+		if (user->uid == uid) {
+			xe_user_get(user);
+			mutex_unlock(&xe->work_period.lock);
+			return user;
+		}
+	}
+	mutex_unlock(&xe->work_period.lock);
+
+	return NULL;
+}
drivers/gpu/drm/xe/xe_user.h +10 -0
index e52f66d3f3b0..55035a9c2c4c 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
 	 */
@@ -62,6 +71,7 @@ struct xe_user {
 };
 
 struct xe_user *xe_user_alloc(void);
+struct xe_user *xe_user_lookup(struct xe_device *xe, u32 uid);
 
 static inline struct xe_user *
 xe_user_get(struct xe_user *user)
Signature · 1 line
2.49.0
#6 lore raw

Re: [PATCH v2 5/9] Handle xe_user creation and removal

Quoted 98 lines · 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       | 57 ++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_device_types.h | 16 ++++++++
 drivers/gpu/drm/xe/xe_user.c         | 28 +++++++++++++-
 drivers/gpu/drm/xe/xe_user.h         | 10 +++++
 4 files changed, 110 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_device.c +35 -0
index 3e0402dff423..bd4a1c5c57ca 100644
@@ -62,6 +62,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_vram.h"
 #include "xe_vram_types.h"
@@ -76,9 +77,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)
@@ -105,11 +110,53 @@ static int xe_file_open(struct drm_device *dev, struct drm_file *file)
 
 	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;
+			put_cred(cred);
+		}
 		xef->process_name = kstrdup(task->comm, GFP_KERNEL);
 		xef->pid = task->pid;
 		put_task_struct(task);
 	}
 
+	if (uid < 0)
+		return -ENOENT;
+
+	INIT_LIST_HEAD(&xef->user_link);
+	/*
+	 * Check if the calling process/uid has already been registered
+	 * with the xe device during a previous open call. If so then
+	 * take a reference to this xe user and add this xe file to the
+	 * filelist belonging to this xe user
+	 */
+	user = xe_user_lookup(xe, uid);
+	if (!user) {
+		/*
+		 * We couldn't find an existing xe user for the calling process.
+		 * Allocate a new struct xe_user and register it with this xe
+		 * device
+		 */
+		user = xe_user_alloc();
+		if (!user)
+			return -ENOMEM;
+
+		user->uid = uid;
+		user->last_timestamp_ns = ktime_get_raw_ns();
+		user->xe = xe;
+

drivers/gpu/drm/xe/xe_device.c static int xe_file_open(struct drm_device *dev, struct drm_file *file) · line 157

Quoted 10 lines
drivers/gpu/drm/xe/xe_device.c +10 -0
+		ret = xa_alloc(&xe->work_period.users, &idx, user, xa_limit_32b, GFP_KERNEL);
+		if (ret < 0)
+			return ret;
+
+		user->id = idx;
+		drm_dev_get(&xe->drm);
+	}
+	mutex_lock(&user->filelist_lock);
+	list_add(&xef->user_link, &user->filelist);
+	mutex_unlock(&user->filelist_lock);

I think that's a lot of struct xe_user implementation detail being handled outside of xe_user.c.

I've been doing a lot of refactoring in i915 hiding exactly this type of thing. It gets really complex to identify what belongs where after some time has passed.

IMO it's not a bad guideline to try make struct xe_user an opaque type, i.e. have it defined in xe_user.c and everyone else needs to operate on it through functions. It really forces you to think about the interactions and the operations.

As always, up to the xe maintainers.

BR,
Jani.

Quoted 153 lines
drivers/gpu/drm/xe/xe_device.c +12 -0
+	xef->user = user;
+
 	return 0;
 }
 
@@ -124,6 +171,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);
 }
 
@@ -458,6 +511,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 c4f076a26291..b375c4928a09 100644
@@ -576,6 +576,16 @@ struct xe_device {
 	atomic64_t global_total_pages;
 #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)
@@ -659,6 +669,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 +27 -1
index 8c285a68115a..5c7d21dfcc45 100644
@@ -4,6 +4,7 @@
  */
 
 #include <linux/slab.h>
+#include <drm/drm_drv.h>
 
 #include "xe_user.h"
 
@@ -39,7 +40,6 @@ struct xe_user *xe_user_alloc(void)
 	kref_init(&user->refcount);
 	mutex_init(&user->filelist_lock);
 	INIT_LIST_HEAD(&user->filelist);
-	//TODO: Add a hook into xe device
 	INIT_WORK(&user->work, work_period_worker);
 	return user;
 }
@@ -54,6 +54,32 @@ void __xe_user_free(struct kref *kref)
 {
 	struct xe_user *user =
 		container_of(kref, struct xe_user, refcount);
+	struct xe_device *xe = user->xe;
+	void *lookup;
 
+	mutex_lock(&xe->work_period.lock);
+	lookup = xa_erase(&xe->work_period.users, user->id);
+	xe_assert(xe, lookup == user);
+	mutex_unlock(&xe->work_period.lock);
+
+	drm_dev_put(&user->xe->drm);
 	kfree(user);
 }
+
+struct xe_user *xe_user_lookup(struct xe_device *xe, u32 uid)
+{
+	struct xe_user *user = NULL;
+	unsigned long i;
+
+	mutex_lock(&xe->work_period.lock);
+	xa_for_each(&xe->work_period.users, i, user) {
+		if (user->uid == uid) {
+			xe_user_get(user);
+			mutex_unlock(&xe->work_period.lock);
+			return user;
+		}
+	}
+	mutex_unlock(&xe->work_period.lock);
+
+	return NULL;
+}
drivers/gpu/drm/xe/xe_user.h +10 -0
index e52f66d3f3b0..55035a9c2c4c 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
 	 */
@@ -62,6 +71,7 @@ struct xe_user {
 };
 
 struct xe_user *xe_user_alloc(void);
+struct xe_user *xe_user_lookup(struct xe_device *xe, u32 uid);
 
 static inline struct xe_user *
 xe_user_get(struct xe_user *user)
Signature · 1 line
Jani Nikula, Intel
#7 lore raw

PATCH v2 6/9 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 | 28 +++++++----
 drivers/gpu/drm/xe/xe_user.c   | 85 ++++++++++++++++++++++++++++++++--
 drivers/gpu/drm/xe/xe_user.h   | 18 +++++--
 3 files changed, 115 insertions(+), 16 deletions(-)
drivers/gpu/drm/xe/xe_device.c +20 -8
index bd4a1c5c57ca..b4692d45c7e9 100644
@@ -151,12 +151,23 @@ static int xe_file_open(struct drm_device *dev, struct drm_file *file)
 
 		user->id = idx;
 		drm_dev_get(&xe->drm);
+
+		xe_user_get(user);
+		if (!schedule_delayed_work(&user->delay_work,
+					msecs_to_jiffies(XE_WORK_PERIOD_INTERVAL)))
+			xe_user_put(user);
 	}
-	mutex_lock(&user->filelist_lock);
+
+	mutex_lock(&user->lock);
 	list_add(&xef->user_link, &user->filelist);
-	mutex_unlock(&user->filelist_lock);
-	xef->user = user;
+	mutex_unlock(&user->lock);
 
+	/*
+	 * We have already taken a reference to the xe_user in
+	 * xe_user_lookup in case this xe file doesn't own the
+	 * pointer to the xe_user.
+	 */
+	xef->user = user;
 	return 0;
 }
 
@@ -172,11 +183,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_user.c +81 -4

107 lines not on this page · raw message

drivers/gpu/drm/xe/xe_user.h +14 -4
index 55035a9c2c4c..80948199e743 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
@@ -73,6 +75,14 @@ struct xe_user {
 struct xe_user *xe_user_alloc(void);
 struct xe_user *xe_user_lookup(struct xe_device *xe, u32 uid);
 
+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
#8 lore raw

PATCH v2 7/9 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     | 116 +++++++++++++++--------------
 drivers/gpu/drm/xe/xe_exec_queue.c |   9 ++-
 drivers/gpu/drm/xe/xe_user.h       |  13 ++++
 drivers/gpu/trace/Kconfig          |  12 +++
 5 files changed, 94 insertions(+), 58 deletions(-)
drivers/gpu/drm/xe/Makefile +1 -1
index 89212fc7ef44..9cc8743b8d7f 100644
@@ -325,7 +325,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 +62 -54

146 lines not on this page · raw message

drivers/gpu/drm/xe/xe_exec_queue.c +6 -3
index 2ee8a475f5ed..6a24463e13a4 100644
@@ -907,9 +907,12 @@ 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));
+	if (IS_ENABLED(CONFIG_TRACE_GPU_WORK_PERIOD)) {
+
+		// Accumulate the runtime in ns for this queue
+		q->xef->active_duration_ns +=
+			xe_gt_clock_interval_to_ns(gt, (new_ts - old_ts));
+	}
 
 	drm_dev_exit(idx);
 }
drivers/gpu/drm/xe/xe_user.h +13 -0
index 80948199e743..7c30bf470aa7 100644
@@ -72,8 +72,21 @@ struct xe_user {
 	u64 last_timestamp_ns;
 };
 
+#if IS_ENABLED(CONFIG_TRACE_GPU_WORK_PERIOD)
+
 struct xe_user *xe_user_alloc(void);
 struct xe_user *xe_user_lookup(struct xe_device *xe, u32 uid);
+#else
+static inline struct xe_user *xe_user_alloc(void)
+{
+	return NULL;
+}
+
+static inline struct xe_user *xe_user_lookup(struct xe_device *xe, u32 uid)
+{
+	return NULL;
+}
+#endif // CONFIG_TRACE_GPU_WORK_PERIOD
 
 static inline struct xe_user *
 xe_user_get_unless_zero(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
#9 lore raw

Re: [PATCH v2 7/9] Add a Kconfig option for GPU work period

Quoted 5 lines · 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.

I assume we will at least want this enabled in CI? Or is this already the case? There is quite a lot of stuff here so I think makes sense that this is part of CI going forward. For now it might be that you need to add a hack patch at the last patch in the series to turn this on so CI will test this. But might then need some way to ensure that this is always turned on by default in: https://gitlab.freedesktop.org/drm/xe/ci

Quoted 240 lines
---
  drivers/gpu/drm/xe/Makefile        |   2 +-
  drivers/gpu/drm/xe/xe_device.c     | 116 +++++++++++++++--------------
  drivers/gpu/drm/xe/xe_exec_queue.c |   9 ++-
  drivers/gpu/drm/xe/xe_user.h       |  13 ++++
  drivers/gpu/trace/Kconfig          |  12 +++
  5 files changed, 94 insertions(+), 58 deletions(-)
drivers/gpu/drm/xe/Makefile +1 -1
index 89212fc7ef44..9cc8743b8d7f 100644
@@ -325,7 +325,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 +62 -54
index b4692d45c7e9..569f3890bde2 100644
@@ -110,64 +110,69 @@ static int xe_file_open(struct drm_device *dev, struct drm_file *file)
 
  	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;
-			put_cred(cred);
+		if (IS_ENABLED(CONFIG_TRACE_GPU_WORK_PERIOD)) {
+			cred = get_task_cred(task);
+			if (cred) {
+				uid = (unsigned int) cred->euid.val;
+				put_cred(cred);
+			}
  		}
  		xef->process_name = kstrdup(task->comm, GFP_KERNEL);
  		xef->pid = task->pid;
  		put_task_struct(task);
  	}
 
-	if (uid < 0)
-		return -ENOENT;
+	if (IS_ENABLED(CONFIG_TRACE_GPU_WORK_PERIOD)) {
+		if (uid < 0)
+			return -ENOENT;
 
-	INIT_LIST_HEAD(&xef->user_link);
-	/*
-	 * Check if the calling process/uid has already been registered
-	 * with the xe device during a previous open call. If so then
-	 * take a reference to this xe user and add this xe file to the
-	 * filelist belonging to this xe user
-	 */
-	user = xe_user_lookup(xe, uid);
-	if (!user) {
+		INIT_LIST_HEAD(&xef->user_link);
  		/*
-		 * We couldn't find an existing xe user for the calling process.
-		 * Allocate a new struct xe_user and register it with this xe
-		 * device
+		 * Check if the calling process/uid has already been registered
+		 * with the xe device during a previous open call. If so then
+		 * take a reference to this xe user and add this xe file to the
+		 * filelist belonging to this xe user
  		 */
-		user = xe_user_alloc();
-		if (!user)
-			return -ENOMEM;
-
-		user->uid = uid;
-		user->last_timestamp_ns = ktime_get_raw_ns();
-		user->xe = xe;
-
-		ret = xa_alloc(&xe->work_period.users, &idx, user, xa_limit_32b, GFP_KERNEL);
-		if (ret < 0)
-			return ret;
-
-		user->id = idx;
-		drm_dev_get(&xe->drm);
-
-		xe_user_get(user);
-		if (!schedule_delayed_work(&user->delay_work,
+		user = xe_user_lookup(xe, uid);
+		if (!user) {
+			/*
+			 * We couldn't find an existing xe user for the calling process.
+			 * Allocate a new struct xe_user and register it with this xe
+			 * device
+			 */
+			user = xe_user_alloc();
+			if (!user)
+				return -ENOMEM;
+
+			user->uid = uid;
+			user->last_timestamp_ns = ktime_get_raw_ns();
+			user->xe = xe;
+
+			ret = xa_alloc(&xe->work_period.users, &idx, user,
+					xa_limit_32b, GFP_KERNEL);
+			if (ret < 0)
+				return ret;
+
+			user->id = idx;
+			drm_dev_get(&xe->drm);
+
+			xe_user_get(user);
+			if (!schedule_delayed_work(&user->delay_work,
  					msecs_to_jiffies(XE_WORK_PERIOD_INTERVAL)))
-			xe_user_put(user);
-	}
+				xe_user_put(user);
+		}
 
-	mutex_lock(&user->lock);
-	list_add(&xef->user_link, &user->filelist);
-	mutex_unlock(&user->lock);
+		mutex_lock(&user->lock);
+		list_add(&xef->user_link, &user->filelist);
+		mutex_unlock(&user->lock);
 
-	/*
-	 * We have already taken a reference to the xe_user in
-	 * xe_user_lookup in case this xe file doesn't own the
-	 * pointer to the xe_user.
-	 */
-	xef->user = user;
+		/*
+		 * We have already taken a reference to the xe_user in
+		 * xe_user_lookup, in case this xe file doesn't own the
+		 * pointer to the xe_user.
+		 */
+		xef->user = user;
+	} // CONFIG_TRACE_GPU_WORK_PERIOD
  	return 0;
  }
 
@@ -183,11 +188,13 @@ static void xe_file_destroy(struct kref *ref)
  	xe_drm_client_put(xef->client);
  	kfree(xef->process_name);
 
-	if (xef->user) {
-		mutex_lock(&xef->user->lock);
-		list_del(&xef->user_link);
-		xe_user_put(xef->user);
-		mutex_unlock(&xef->user->lock);
+	if (IS_ENABLED(CONFIG_TRACE_GPU_WORK_PERIOD)) {
+		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);
  }
@@ -523,9 +530,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_TRACE_GPU_WORK_PERIOD)) {
+		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. */
drivers/gpu/drm/xe/xe_exec_queue.c +6 -3
index 2ee8a475f5ed..6a24463e13a4 100644
@@ -907,9 +907,12 @@ 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));
+	if (IS_ENABLED(CONFIG_TRACE_GPU_WORK_PERIOD)) {
+
+		// Accumulate the runtime in ns for this queue
+		q->xef->active_duration_ns +=
+			xe_gt_clock_interval_to_ns(gt, (new_ts - old_ts));
+	}
 
  	drm_dev_exit(idx);
  }
drivers/gpu/drm/xe/xe_user.h +13 -0
index 80948199e743..7c30bf470aa7 100644
@@ -72,8 +72,21 @@ struct xe_user {
  	u64 last_timestamp_ns;
  };
 
+#if IS_ENABLED(CONFIG_TRACE_GPU_WORK_PERIOD)
+
  struct xe_user *xe_user_alloc(void);
  struct xe_user *xe_user_lookup(struct xe_device *xe, u32 uid);
+#else
+static inline struct xe_user *xe_user_alloc(void)
+{
+	return NULL;
+}
+
+static inline struct xe_user *xe_user_lookup(struct xe_device *xe, u32 uid)
+{
+	return NULL;
+}
+#endif // CONFIG_TRACE_GPU_WORK_PERIOD
 
  static inline struct xe_user *
  xe_user_get_unless_zero(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".
maintainer
#10 lore raw

Re: [PATCH v2 7/9] Add a Kconfig option for GPU work period

In reply to Aakash Deep Sarkar
Quoted 31 lines · 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     | 116 +++++++++++++++--------------
 drivers/gpu/drm/xe/xe_exec_queue.c |   9 ++-
 drivers/gpu/drm/xe/xe_user.h       |  13 ++++
 drivers/gpu/trace/Kconfig          |  12 +++
 5 files changed, 94 insertions(+), 58 deletions(-)
drivers/gpu/drm/xe/Makefile +1 -1
index 89212fc7ef44..9cc8743b8d7f 100644
@@ -325,7 +325,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 -0
index b4692d45c7e9..569f3890bde2 100644

drivers/gpu/drm/xe/xe_device.c static int xe_file_open(struct drm_device *dev, struct drm_file *file) · line 113

Quoted 8 lines
drivers/gpu/drm/xe/xe_device.c +1 -4
@@ -110,64 +110,69 @@ static int xe_file_open(struct drm_device *dev, struct drm_file *file)
 
 	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;
-			put_cred(cred);
+		if (IS_ENABLED(CONFIG_TRACE_GPU_WORK_PERIOD)) {

Can you try to avoid conditionals based on Kconfigs in functions? i.e., Add helpers which compile out if CONFIG_TRACE_GPU_WORK_PERIOD is unset? That is typically the style for Kconfigs. Anything more than a single line or variable set I'd say stick in a function.

Matt

Quoted 209 lines
drivers/gpu/drm/xe/xe_device.c +61 -50
+			cred = get_task_cred(task);
+			if (cred) {
+				uid = (unsigned int) cred->euid.val;
+				put_cred(cred);
+			}
 		}
 		xef->process_name = kstrdup(task->comm, GFP_KERNEL);
 		xef->pid = task->pid;
 		put_task_struct(task);
 	}
 
-	if (uid < 0)
-		return -ENOENT;
+	if (IS_ENABLED(CONFIG_TRACE_GPU_WORK_PERIOD)) {
+		if (uid < 0)
+			return -ENOENT;
 
-	INIT_LIST_HEAD(&xef->user_link);
-	/*
-	 * Check if the calling process/uid has already been registered
-	 * with the xe device during a previous open call. If so then
-	 * take a reference to this xe user and add this xe file to the
-	 * filelist belonging to this xe user
-	 */
-	user = xe_user_lookup(xe, uid);
-	if (!user) {
+		INIT_LIST_HEAD(&xef->user_link);
 		/*
-		 * We couldn't find an existing xe user for the calling process.
-		 * Allocate a new struct xe_user and register it with this xe
-		 * device
+		 * Check if the calling process/uid has already been registered
+		 * with the xe device during a previous open call. If so then
+		 * take a reference to this xe user and add this xe file to the
+		 * filelist belonging to this xe user
 		 */
-		user = xe_user_alloc();
-		if (!user)
-			return -ENOMEM;
-
-		user->uid = uid;
-		user->last_timestamp_ns = ktime_get_raw_ns();
-		user->xe = xe;
-
-		ret = xa_alloc(&xe->work_period.users, &idx, user, xa_limit_32b, GFP_KERNEL);
-		if (ret < 0)
-			return ret;
-
-		user->id = idx;
-		drm_dev_get(&xe->drm);
-
-		xe_user_get(user);
-		if (!schedule_delayed_work(&user->delay_work,
+		user = xe_user_lookup(xe, uid);
+		if (!user) {
+			/*
+			 * We couldn't find an existing xe user for the calling process.
+			 * Allocate a new struct xe_user and register it with this xe
+			 * device
+			 */
+			user = xe_user_alloc();
+			if (!user)
+				return -ENOMEM;
+
+			user->uid = uid;
+			user->last_timestamp_ns = ktime_get_raw_ns();
+			user->xe = xe;
+
+			ret = xa_alloc(&xe->work_period.users, &idx, user,
+					xa_limit_32b, GFP_KERNEL);
+			if (ret < 0)
+				return ret;
+
+			user->id = idx;
+			drm_dev_get(&xe->drm);
+
+			xe_user_get(user);
+			if (!schedule_delayed_work(&user->delay_work,
 					msecs_to_jiffies(XE_WORK_PERIOD_INTERVAL)))
-			xe_user_put(user);
-	}
+				xe_user_put(user);
+		}
 
-	mutex_lock(&user->lock);
-	list_add(&xef->user_link, &user->filelist);
-	mutex_unlock(&user->lock);
+		mutex_lock(&user->lock);
+		list_add(&xef->user_link, &user->filelist);
+		mutex_unlock(&user->lock);
 
-	/*
-	 * We have already taken a reference to the xe_user in
-	 * xe_user_lookup in case this xe file doesn't own the
-	 * pointer to the xe_user.
-	 */
-	xef->user = user;
+		/*
+		 * We have already taken a reference to the xe_user in
+		 * xe_user_lookup, in case this xe file doesn't own the
+		 * pointer to the xe_user.
+		 */
+		xef->user = user;
+	} // CONFIG_TRACE_GPU_WORK_PERIOD
 	return 0;
 }
 
@@ -183,11 +188,13 @@ static void xe_file_destroy(struct kref *ref)
 	xe_drm_client_put(xef->client);
 	kfree(xef->process_name);
 
-	if (xef->user) {
-		mutex_lock(&xef->user->lock);
-		list_del(&xef->user_link);
-		xe_user_put(xef->user);
-		mutex_unlock(&xef->user->lock);
+	if (IS_ENABLED(CONFIG_TRACE_GPU_WORK_PERIOD)) {
+		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);
 }
@@ -523,9 +530,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_TRACE_GPU_WORK_PERIOD)) {
+		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. */
drivers/gpu/drm/xe/xe_exec_queue.c +6 -3
index 2ee8a475f5ed..6a24463e13a4 100644
@@ -907,9 +907,12 @@ 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));
+	if (IS_ENABLED(CONFIG_TRACE_GPU_WORK_PERIOD)) {
+
+		// Accumulate the runtime in ns for this queue
+		q->xef->active_duration_ns +=
+			xe_gt_clock_interval_to_ns(gt, (new_ts - old_ts));
+	}
 
 	drm_dev_exit(idx);
 }
drivers/gpu/drm/xe/xe_user.h +13 -0
index 80948199e743..7c30bf470aa7 100644
@@ -72,8 +72,21 @@ struct xe_user {
 	u64 last_timestamp_ns;
 };
 
+#if IS_ENABLED(CONFIG_TRACE_GPU_WORK_PERIOD)
+
 struct xe_user *xe_user_alloc(void);
 struct xe_user *xe_user_lookup(struct xe_device *xe, u32 uid);
+#else
+static inline struct xe_user *xe_user_alloc(void)
+{
+	return NULL;
+}
+
+static inline struct xe_user *xe_user_lookup(struct xe_device *xe, u32 uid)
+{
+	return NULL;
+}
+#endif // CONFIG_TRACE_GPU_WORK_PERIOD
 
 static inline struct xe_user *
 xe_user_get_unless_zero(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
#11 lore raw

PATCH v2 8/9 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 | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/gpu/drm/xe/xe_device.c +13 -0
index 569f3890bde2..e07bc2f2d0c1 100644
@@ -481,6 +481,19 @@ static void xe_device_destroy(struct drm_device *dev, void *dummy)
 	if (xe->destroy_wq)
 		destroy_workqueue(xe->destroy_wq);
 
+	if (IS_ENABLED(CONFIG_TRACE_GPU_WORK_PERIOD)) {
+		struct xe_user *user = NULL;
+		unsigned long i;
+
+		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
#12 lore raw

PATCH v2 9/9 Fix runtime PM handling of Xe work period worker

In reply to Aakash Deep Sarkar

The autosuspend delay for the xe device is set to 1000 msecs. This means that the RPM framework would wait for 1000 msecs before initiating the runtime suspend. Since the Xe work period kworker runs approximately every 500 msecs, taking a full runtime PM reference from our kworker would cancel any pending suspend requests and would resume the device synchronously from within our kworker. In other words the opportunistic runtime suspend would never come to pass with our kworker running every 500 msecs.

To address this issue, we only take a runtime PM reference in our work period kworker, if the device is already awake. Otherwise we mark the kworker as suspended and skip the rescheduling of our kworker.

In the runtime resume callback for our xe device, we check which of the xe users are in a suspended state and reschedule the work for these xe users.

---
 drivers/gpu/drm/xe/xe_pm.c   |  4 ++++
 drivers/gpu/drm/xe/xe_user.c | 28 +++++++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_user.h |  7 +++++++
 3 files changed, 38 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_pm.c +4 -0
index 5e8126ca8e27..0e4dbf9880e4 100644
@@ -25,6 +25,7 @@
 #include "xe_pxp.h"
 #include "xe_sriov_vf_ccs.h"
 #include "xe_trace.h"
+#include "xe_user.h"
 #include "xe_wa.h"
 
 /**
@@ -558,6 +559,9 @@ int xe_pm_runtime_resume(struct xe_device *xe)
 
 	xe_pxp_pm_resume(xe->pxp);
 
+	if (IS_ENABLED(CONFIG_TRACE_GPU_WORK_PERIOD))
+		xe_users_runtime_resume(xe);
+
 	if (IS_SRIOV_VF(xe))
 		xe_sriov_vf_ccs_register_context(xe);
 
drivers/gpu/drm/xe/xe_user.c +27 -1
index 50fb43d03b7b..3fa3a590ca9e 100644
@@ -60,7 +60,12 @@ static void xe_work_period_worker(struct work_struct *work)
 	last_active_duration = user->active_duration_ns;
 	last_timestamp = user->last_timestamp_ns;
 
-	xe_pm_runtime_get(xe);
+	if (!xe_pm_runtime_get_if_active(xe)) {
+		WRITE_ONCE(user->suspended, true);
+		xe_user_put(user);
+		mutex_unlock(&user->lock);
+		return;
+	}
 
 	list_for_each_entry(xef, &user->filelist, user_link) {
 
@@ -118,6 +123,7 @@ struct xe_user *xe_user_alloc(void)
 	mutex_init(&user->lock);
 	INIT_LIST_HEAD(&user->filelist);
 	INIT_DELAYED_WORK(&user->delay_work, xe_work_period_worker);
+	WRITE_ONCE(user->suspended, false);
 	return user;
 }
 
@@ -160,3 +166,23 @@ struct xe_user *xe_user_lookup(struct xe_device *xe, u32 uid)
 
 	return NULL;
 }
+
+void xe_users_runtime_resume(struct xe_device* xe)
+{
+	struct xe_user *user = NULL;
+	unsigned long i = 0;
+
+	mutex_lock(&xe->work_period.lock);
+	xa_for_each(&xe->work_period.users, i, user) {
+		if (READ_ONCE(user->suspended) == true) {
+			xe_user_get(user);
+
+			if (!schedule_delayed_work(&user->delay_work,
+					msecs_to_jiffies(XE_WORK_PERIOD_INTERVAL)))
+				xe_user_put(user);
+
+			WRITE_ONCE(user->suspended, false);
+		}
+	}
+	mutex_unlock(&xe->work_period.lock);
+}
\ No newline at end of file
drivers/gpu/drm/xe/xe_user.h +7 -0
index 7c30bf470aa7..cd6cf3e15329 100644
@@ -49,6 +49,11 @@ struct xe_user {
 	 */
 	struct delayed_work delay_work;
 
+	/**
+	 * @suspended: is the work period suspended for this user
+	 */
+	bool suspended;
+
 	/**
 	 * @id: index of this user into the xe device users array
 	 */
@@ -110,5 +115,7 @@ static inline void xe_user_put(struct xe_user *user)
 	kref_put(&user->refcount, __xe_user_free);
 }
 
+void xe_users_runtime_resume(struct xe_device* xe);
+
 #endif // _XE_USER_H_
 
Signature · 1 line
2.49.0
#13 lore raw

Re: [PATCH v2 9/9] Fix runtime PM handling of Xe work period worker

Quoted 51 lines · Aakash Deep Sarkar

The autosuspend delay for the xe device is set to 1000 msecs. This means that the RPM framework would wait for 1000 msecs before initiating the runtime suspend. Since the Xe work period kworker runs approximately every 500 msecs, taking a full runtime PM reference from our kworker would cancel any pending suspend requests and would resume the device synchronously from within our kworker. In other words the opportunistic runtime suspend would never come to pass with our kworker running every 500 msecs.

To address this issue, we only take a runtime PM reference in our work period kworker, if the device is already awake. Otherwise we mark the kworker as suspended and skip the rescheduling of our kworker.

In the runtime resume callback for our xe device, we check which of the xe users are in a suspended state and reschedule the work for these xe users.

---
  drivers/gpu/drm/xe/xe_pm.c   |  4 ++++
  drivers/gpu/drm/xe/xe_user.c | 28 +++++++++++++++++++++++++++-
  drivers/gpu/drm/xe/xe_user.h |  7 +++++++
  3 files changed, 38 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_pm.c +4 -0
index 5e8126ca8e27..0e4dbf9880e4 100644
@@ -25,6 +25,7 @@
  #include "xe_pxp.h"
  #include "xe_sriov_vf_ccs.h"
  #include "xe_trace.h"
+#include "xe_user.h"
  #include "xe_wa.h"
 
  /**
@@ -558,6 +559,9 @@ int xe_pm_runtime_resume(struct xe_device *xe)
 
  	xe_pxp_pm_resume(xe->pxp);
 
+	if (IS_ENABLED(CONFIG_TRACE_GPU_WORK_PERIOD))
+		xe_users_runtime_resume(xe);
+
  	if (IS_SRIOV_VF(xe))
  		xe_sriov_vf_ccs_register_context(xe);
 
drivers/gpu/drm/xe/xe_user.c +0 -0
index 50fb43d03b7b..3fa3a590ca9e 100644

drivers/gpu/drm/xe/xe_user.c static void xe_work_period_worker(struct work_struct *work) · line 66

Quoted 8 lines
drivers/gpu/drm/xe/xe_user.c +4 -1
@@ -60,7 +60,12 @@ static void xe_work_period_worker(struct work_struct *work)
  	last_active_duration = user->active_duration_ns;
  	last_timestamp = user->last_timestamp_ns;
 
-	xe_pm_runtime_get(xe);
+	if (!xe_pm_runtime_get_if_active(xe)) {
+		WRITE_ONCE(user->suspended, true);
+		xe_user_put(user);
+		mutex_unlock(&user->lock);

I think this might lead to strange behaviour when !if_active() triggers, and suspend is later cancelled, so the suspend never actually runs but here we just returned and never re-scheduled the worker so it might never run again, like say something grabbing a new rpm ref before the 1000ms grace period? Maybe we can just re-schedule the worker as normal here, skipping the other stuff, and then add xe_users_runtime_suspend() which cancels the worker instead, and is then only run if suspend actually triggers?

Also this patch should be squashed with the patch that introduces the worker. Each patch in the series should be self contained, and ideally ~functional. Also patch prefix should be "drm/xe:".

Quoted 62 lines
drivers/gpu/drm/xe/xe_user.c +23 -0
+		return;
+	}
 
  	list_for_each_entry(xef, &user->filelist, user_link) {
 
@@ -118,6 +123,7 @@ struct xe_user *xe_user_alloc(void)
  	mutex_init(&user->lock);
  	INIT_LIST_HEAD(&user->filelist);
  	INIT_DELAYED_WORK(&user->delay_work, xe_work_period_worker);
+	WRITE_ONCE(user->suspended, false);
  	return user;
  }
 
@@ -160,3 +166,23 @@ struct xe_user *xe_user_lookup(struct xe_device *xe, u32 uid)
 
  	return NULL;
  }
+
+void xe_users_runtime_resume(struct xe_device* xe)
+{
+	struct xe_user *user = NULL;
+	unsigned long i = 0;
+
+	mutex_lock(&xe->work_period.lock);
+	xa_for_each(&xe->work_period.users, i, user) {
+		if (READ_ONCE(user->suspended) == true) {
+			xe_user_get(user);
+
+			if (!schedule_delayed_work(&user->delay_work,
+					msecs_to_jiffies(XE_WORK_PERIOD_INTERVAL)))
+				xe_user_put(user);
+
+			WRITE_ONCE(user->suspended, false);
+		}
+	}
+	mutex_unlock(&xe->work_period.lock);
+}
\ No newline at end of file
drivers/gpu/drm/xe/xe_user.h +7 -0
index 7c30bf470aa7..cd6cf3e15329 100644
@@ -49,6 +49,11 @@ struct xe_user {
  	 */
  	struct delayed_work delay_work;
 
+	/**
+	 * @suspended: is the work period suspended for this user
+	 */
+	bool suspended;
+
  	/**
  	 * @id: index of this user into the xe device users array
  	 */
@@ -110,5 +115,7 @@ static inline void xe_user_put(struct xe_user *user)
  	kref_put(&user->refcount, __xe_user_free);
  }
 
+void xe_users_runtime_resume(struct xe_device* xe);
+
  #endif // _XE_USER_H_