[PATCH 3/3] drm/xe/oa: Destroy dangling oa streams when xe_file is removed

José Roberto de Souza jose.souza at intel.com
Thu Aug 15 16:27:58 UTC 2024


If an application opens an oa stream and for whatever reason it don't
close the stream the oa unit associeted with it will never be release,
so no other application can use it.
The only workaround was to unload the driver or reboot.

So here adding a list of oa stream and and when a xe_file is closed
it will also destroy all oa stream with the same xe_file.

Signed-off-by: José Roberto de Souza <jose.souza at intel.com>
---
 drivers/gpu/drm/xe/xe_device.c   |  2 ++
 drivers/gpu/drm/xe/xe_oa.c       | 28 +++++++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_oa.h       |  1 +
 drivers/gpu/drm/xe/xe_oa_types.h |  8 ++++++++
 4 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/xe/xe_device.c b/drivers/gpu/drm/xe/xe_device.c
index 2063283871503..b098685ed3636 100644
--- a/drivers/gpu/drm/xe/xe_device.c
+++ b/drivers/gpu/drm/xe/xe_device.c
@@ -158,6 +158,8 @@ static void xe_file_close(struct drm_device *dev, struct drm_file *file)
 
 	xe_pm_runtime_get(xe);
 
+	xe_oa_file_destroy_stream(xef);
+
 	/*
 	 * No need for exec_queue.lock here as there is no contention for it
 	 * when FD is closing as IOCTLs presumably can't be modifying the
diff --git a/drivers/gpu/drm/xe/xe_oa.c b/drivers/gpu/drm/xe/xe_oa.c
index 4be722b92f90b..4a01a928dd8ee 100644
--- a/drivers/gpu/drm/xe/xe_oa.c
+++ b/drivers/gpu/drm/xe/xe_oa.c
@@ -70,6 +70,7 @@ struct flex {
 };
 
 struct xe_oa_open_param {
+	struct xe_file *xef;
 	u32 oa_unit_id;
 	bool sample;
 	u32 metric_set;
@@ -839,6 +840,9 @@ static void xe_oa_stream_destroy(struct xe_oa_stream *stream)
 		xe_gt_WARN_ON(gt, xe_guc_pc_unset_gucrc_mode(&gt->uc.guc.pc));
 
 	xe_oa_free_configs(stream);
+
+	lockdep_assert_held(&stream->oa->streams_lock);
+	list_del(&stream->list_node);
 }
 
 static int xe_oa_alloc_oa_buffer(struct xe_oa_stream *stream)
@@ -1480,11 +1484,15 @@ static int xe_oa_stream_open_ioctl_locked(struct xe_oa *oa,
 		goto exit;
 	}
 
+	stream->xef = param->xef;
 	stream->oa = oa;
 	ret = xe_oa_stream_init(stream, param);
 	if (ret)
 		goto err_free;
 
+	lockdep_assert_held(&oa->streams_lock);
+	list_add(&stream->list_node, &oa->streams);
+
 	if (!param->disabled) {
 		ret = xe_oa_enable_locked(stream);
 		if (ret)
@@ -1797,7 +1805,9 @@ int xe_oa_stream_open_ioctl(struct drm_device *dev, u64 data, struct drm_file *f
 	struct xe_device *xe = to_xe_device(dev);
 	struct xe_oa *oa = &xe->oa;
 	struct xe_file *xef = to_xe_file(file);
-	struct xe_oa_open_param param = {};
+	struct xe_oa_open_param param = {
+		.xef = xef,
+	};
 	const struct xe_oa_format *f;
 	bool privileged_op = true;
 	int ret;
@@ -2470,6 +2480,7 @@ int xe_oa_init(struct xe_device *xe)
 	oa->xe = xe;
 	oa->oa_formats = oa_formats;
 
+	INIT_LIST_HEAD(&oa->streams);
 	mutex_init(&oa->streams_lock);
 
 	drmm_mutex_init(&oa->xe->drm, &oa->metrics_lock);
@@ -2511,3 +2522,18 @@ void xe_oa_fini(struct xe_device *xe)
 	mutex_destroy(&oa->streams_lock);
 	oa->xe = NULL;
 }
+
+void
+xe_oa_file_destroy_stream(struct xe_file *xef)
+{
+	struct xe_device *xe = xef->xe;
+	struct xe_oa *oa = &xe->oa;
+	struct xe_oa_stream *stream, *n;
+
+	mutex_lock(&oa->streams_lock);
+	list_for_each_entry_safe(stream, n, &oa->streams, list_node) {
+		if (stream->xef == xef)
+			xe_oa_destroy_locked(stream);
+	}
+	mutex_unlock(&oa->streams_lock);
+}
diff --git a/drivers/gpu/drm/xe/xe_oa.h b/drivers/gpu/drm/xe/xe_oa.h
index 87a38820c317d..7d6ee9ff89c74 100644
--- a/drivers/gpu/drm/xe/xe_oa.h
+++ b/drivers/gpu/drm/xe/xe_oa.h
@@ -16,6 +16,7 @@ struct xe_hw_engine;
 
 int xe_oa_init(struct xe_device *xe);
 void xe_oa_fini(struct xe_device *xe);
+void xe_oa_file_destroy_stream(struct xe_file *xef);
 void xe_oa_register(struct xe_device *xe);
 void xe_oa_unregister(struct xe_device *xe);
 int xe_oa_stream_open_ioctl(struct drm_device *dev, u64 data, struct drm_file *file);
diff --git a/drivers/gpu/drm/xe/xe_oa_types.h b/drivers/gpu/drm/xe/xe_oa_types.h
index 17e17b5b93640..a035786e101c9 100644
--- a/drivers/gpu/drm/xe/xe_oa_types.h
+++ b/drivers/gpu/drm/xe/xe_oa_types.h
@@ -148,6 +148,8 @@ struct xe_oa {
 	u16 oa_unit_ids;
 
 	struct mutex streams_lock;
+
+	struct list_head streams;
 };
 
 /** @xe_oa_buffer: State of the stream OA buffer */
@@ -178,9 +180,15 @@ struct xe_oa_buffer {
  * struct xe_oa_stream - state for a single open stream FD
  */
 struct xe_oa_stream {
+	/** @node: node of the list of stream  */
+	struct list_head list_node;
+
 	/** @oa: xe_oa backpointer */
 	struct xe_oa *oa;
 
+	/** @xe_file: xe_file that created this stream */
+	struct xe_file *xef;
+
 	/** @gt: gt associated with the oa stream */
 	struct xe_gt *gt;
 
-- 
2.46.0



More information about the Intel-xe mailing list