drm/xe: Do not access xe file when updating exec queue runtime
Subsystems (2)
PATCH 1/1 drm/xe: Do not access xe file when updating exec queue runtime
The current code is running into a use after free case where xe file is closed before the exec queue runtime can be updated. This is occuring in the xe_file_close path. To fix that, do not access xe file when updating the exec queue runtime. Instead store the exec queue runtime locally in the exec queue object and accumulate it when the user dumps the drm client stats. We know that the xe file is valid when user is dumping the runtime stats for the drm client, so this effectively removes the dependency on xe file object in xe_exec_queue_update_runtime().
--- drivers/gpu/drm/xe/xe_drm_client.c | 4 +++- drivers/gpu/drm/xe/xe_exec_queue.c | 5 +---- drivers/gpu/drm/xe/xe_exec_queue_types.h | 2 ++ 3 files changed, 6 insertions(+), 5 deletions(-)
drivers/gpu/drm/xe/xe_drm_client.c +3 -1
@@ -251,8 +251,10 @@ static void show_runtime(struct drm_printer *p, struct drm_file *file)/* Accumulate all the exec queues from this client */ mutex_lock (&xef->exec_queue.lock); -xa_for_each (&xef->exec_queue.xa, i, q) +xa_for_each (&xef->exec_queue.xa, i, q) {xe_exec_queue_update_runtime (q); + xef->runtime[q->class] += q->runtime - xef->runtime[q->class]; + }mutex_unlock (&xef->exec_queue.lock);/* Get the total GPU cycles */
drivers/gpu/drm/xe/xe_exec_queue.c +1 -4
@@ -765,7 +765,6 @@ bool xe_exec_queue_is_idle(struct xe_exec_queue *q)*/ void xe_exec_queue_update_runtime (struct xe_exec_queue *q) { -struct xe_file *xef;struct xe_lrc *lrc;u32 old_ts, new_ts;
@@ -777,8 +776,6 @@ void xe_exec_queue_update_runtime(struct xe_exec_queue *q)if (!q->vm || !q->vm->xef)return ; - xef = q->vm->xef; -/* * Only sample the first LRC. For parallel submission, all of them are * scheduled together and we compensate that below by multiplying by
@@ -789,7 +786,7 @@ void xe_exec_queue_update_runtime(struct xe_exec_queue *q)*/ lrc = &q->lrc[0 ]; new_ts =xe_lrc_update_timestamp (lrc, &old_ts); - xef->runtime[q->class] += (new_ts - old_ts) * q->width; + q->runtime += (new_ts - old_ts) * q->width; }void xe_exec_queue_kill (struct xe_exec_queue *q)
drivers/gpu/drm/xe/xe_exec_queue_types.h +2 -0
@@ -151,6 +151,8 @@ struct xe_exec_queue {* Protected by @vm's resv. Unused if @vm == NULL. */ u64 tlb_flush_seqno; +/** @runtime: hw engine class runtime in ticks for this exec queue*/ +u64 runtime;/** @lrc: logical ring context for this exec queue */ struct xe_lrc lrc[]; };
Signature · 1 line
2.34.1
Quoted 23 lines · Umesh Nerlige Ramappa
The current code is running into a use after free case where xe file is closed before the exec queue runtime can be updated. This is occuring in the
xe_file_closepath. To fix that, do not access xe file when updating the exec queue runtime. Instead store the exec queue runtime locally in the exec queue object and accumulate it when the user dumps the drm client stats. We know that the xe file is valid when user is dumping the runtime stats for the drm client, so this effectively removes the dependency on xe file object inxe_exec_queue_update_runtime().Fixes 6109f24f87d7 ("drm/xe: Add helper to accumulate exec queue runtime")Signed-off-by Umesh Nerlige Ramappa <[email protected]>---
drivers/gpu/drm/xe/xe_drm_client.c | 4 +++- drivers/gpu/drm/xe/xe_exec_queue.c | 5 +---- drivers/gpu/drm/xe/xe_exec_queue_types.h | 2 ++ 3 files changed, 6 insertions(+), 5 deletions(-)drivers/gpu/drm/xe/xe_drm_client.c +0
-0
drivers/gpu/drm/xe/xe_drm_client.c static void show_runtime(struct drm_printer *p, struct drm_file *file) · line 256
Quoted 7 lines
drivers/gpu/drm/xe/xe_drm_client.c +2
-1@@ -251,8 +251,10 @@ static void show_runtime(struct drm_printer *p, struct drm_file *file)/* Accumulate all the exec queues from this client */ mutex_lock (&xef->exec_queue.lock); -xa_for_each (&xef->exec_queue.xa, i, q) +xa_for_each (&xef->exec_queue.xa, i, q) {xe_exec_queue_update_runtime (q); + xef->runtime[q->class] += q->runtime - xef->runtime[q->class];
This needs to be delta for the specific queue. Will post another rev.
Umesh
Quoted 50 lines
drivers/gpu/drm/xe/xe_drm_client.c +1
-0+ }mutex_unlock (&xef->exec_queue.lock);/* Get the total GPU cycles */ drivers/gpu/drm/xe/xe_exec_queue.c +1
-4@@ -765,7 +765,6 @@ bool xe_exec_queue_is_idle(struct xe_exec_queue *q)*/ void xe_exec_queue_update_runtime (struct xe_exec_queue *q) { -struct xe_file *xef;struct xe_lrc *lrc;u32 old_ts, new_ts;@@ -777,8 +776,6 @@ void xe_exec_queue_update_runtime(struct xe_exec_queue *q)if (!q->vm || !q->vm->xef)return ; - xef = q->vm->xef; -/* * Only sample the first LRC. For parallel submission, all of them are * scheduled together and we compensate that below by multiplying by @@ -789,7 +786,7 @@ void xe_exec_queue_update_runtime(struct xe_exec_queue *q)*/ lrc = &q->lrc[0 ]; new_ts =xe_lrc_update_timestamp (lrc, &old_ts); - xef->runtime[q->class] += (new_ts - old_ts) * q->width; + q->runtime += (new_ts - old_ts) * q->width; }void xe_exec_queue_kill (struct xe_exec_queue *q)drivers/gpu/drm/xe/xe_exec_queue_types.h +2
-0@@ -151,6 +151,8 @@ struct xe_exec_queue {* Protected by @vm's resv. Unused if @vm == NULL. */ u64 tlb_flush_seqno; +/** @runtime: hw engine class runtime in ticks for this exec queue*/ +u64 runtime;/** @lrc: logical ring context for this exec queue */ struct xe_lrc lrc[]; };Signature · 1 line
2.34.1
Rodrigo Vivi
maintainerQuoted 69 lines · Umesh Nerlige Ramappa
The current code is running into a use after free case where xe file is closed before the exec queue runtime can be updated. This is occuring in the
xe_file_closepath. To fix that, do not access xe file when updating the exec queue runtime. Instead store the exec queue runtime locally in the exec queue object and accumulate it when the user dumps the drm client stats. We know that the xe file is valid when user is dumping the runtime stats for the drm client, so this effectively removes the dependency on xe file object inxe_exec_queue_update_runtime().Fixes 6109f24f87d7 ("drm/xe: Add helper to accumulate exec queue runtime")Signed-off-by Umesh Nerlige Ramappa <[email protected]>--- drivers/gpu/drm/xe/xe_drm_client.c | 4 +++- drivers/gpu/drm/xe/xe_exec_queue.c | 5 +---- drivers/gpu/drm/xe/xe_exec_queue_types.h | 2 ++ 3 files changed, 6 insertions(+), 5 deletions(-)drivers/gpu/drm/xe/xe_drm_client.c +3
-1@@ -251,8 +251,10 @@ static void show_runtime(struct drm_printer *p, struct drm_file *file)/* Accumulate all the exec queues from this client */ mutex_lock (&xef->exec_queue.lock); -xa_for_each (&xef->exec_queue.xa, i, q) +xa_for_each (&xef->exec_queue.xa, i, q) {xe_exec_queue_update_runtime (q); + xef->runtime[q->class] += q->runtime - xef->runtime[q->class]; + }mutex_unlock (&xef->exec_queue.lock);/* Get the total GPU cycles */ drivers/gpu/drm/xe/xe_exec_queue.c +1
-4@@ -765,7 +765,6 @@ bool xe_exec_queue_is_idle(struct xe_exec_queue *q)*/ void xe_exec_queue_update_runtime (struct xe_exec_queue *q) { -struct xe_file *xef;struct xe_lrc *lrc;u32 old_ts, new_ts;@@ -777,8 +776,6 @@ void xe_exec_queue_update_runtime(struct xe_exec_queue *q)if (!q->vm || !q->vm->xef)return ; - xef = q->vm->xef; -/* * Only sample the first LRC. For parallel submission, all of them are * scheduled together and we compensate that below by multiplying by @@ -789,7 +786,7 @@ void xe_exec_queue_update_runtime(struct xe_exec_queue *q)*/ lrc = &q->lrc[0 ]; new_ts =xe_lrc_update_timestamp (lrc, &old_ts); - xef->runtime[q->class] += (new_ts - old_ts) * q->width; + q->runtime += (new_ts - old_ts) * q->width; }void xe_exec_queue_kill (struct xe_exec_queue *q)drivers/gpu/drm/xe/xe_exec_queue_types.h +0
-0
drivers/gpu/drm/xe/xe_exec_queue_types.h struct xe_exec_queue { · line 154
Quoted 4 lines
drivers/gpu/drm/xe/xe_exec_queue_types.h +1
-0@@ -151,6 +151,8 @@ struct xe_exec_queue {* Protected by @vm's resv. Unused if @vm == NULL. */ u64 tlb_flush_seqno; +/** @runtime: hw engine class runtime in ticks for this exec queue*/
^
missing space ^
good idea to move to the exec_queue
drivers/gpu/drm/xe/xe_exec_queue_types.h struct xe_exec_queue { · line 155
Quoted 1 line
drivers/gpu/drm/xe/xe_exec_queue_types.h +1
-0+u64 runtime;
could we please find a better name for this? runtime what?
or do you mean run_time?
Is this the total run time of the exec queue expressed in ticks of the eu clock?
My poor brain parses runtime as the phase of the operation, vs run_time as the time spent running.
or perhaps 'run_ticks'?
Quoted 6 lines
drivers/gpu/drm/xe/xe_exec_queue_types.h +0
-0/** @lrc: logical ring context for this exec queue */ struct xe_lrc lrc[]; };Signature · 1 line
2.34.1
Quoted 85 lines · Rodrigo Vivi
Quoted 69 lines · Umesh Nerlige Ramappa
The current code is running into a use after free case where xe file is closed before the exec queue runtime can be updated. This is occuring in the
xe_file_closepath. To fix that, do not access xe file when updating the exec queue runtime. Instead store the exec queue runtime locally in the exec queue object and accumulate it when the user dumps the drm client stats. We know that the xe file is valid when user is dumping the runtime stats for the drm client, so this effectively removes the dependency on xe file object inxe_exec_queue_update_runtime().Fixes 6109f24f87d7 ("drm/xe: Add helper to accumulate exec queue runtime")Signed-off-by Umesh Nerlige Ramappa <[email protected]>--- drivers/gpu/drm/xe/xe_drm_client.c | 4 +++- drivers/gpu/drm/xe/xe_exec_queue.c | 5 +---- drivers/gpu/drm/xe/xe_exec_queue_types.h | 2 ++ 3 files changed, 6 insertions(+), 5 deletions(-)drivers/gpu/drm/xe/xe_drm_client.c +3
-1@@ -251,8 +251,10 @@ static void show_runtime(struct drm_printer *p, struct drm_file *file)/* Accumulate all the exec queues from this client */ mutex_lock (&xef->exec_queue.lock); -xa_for_each (&xef->exec_queue.xa, i, q) +xa_for_each (&xef->exec_queue.xa, i, q) {xe_exec_queue_update_runtime (q); + xef->runtime[q->class] += q->runtime - xef->runtime[q->class]; + }mutex_unlock (&xef->exec_queue.lock);/* Get the total GPU cycles */ drivers/gpu/drm/xe/xe_exec_queue.c +1
-4@@ -765,7 +765,6 @@ bool xe_exec_queue_is_idle(struct xe_exec_queue *q)*/ void xe_exec_queue_update_runtime (struct xe_exec_queue *q) { -struct xe_file *xef;struct xe_lrc *lrc;u32 old_ts, new_ts;@@ -777,8 +776,6 @@ void xe_exec_queue_update_runtime(struct xe_exec_queue *q)if (!q->vm || !q->vm->xef)return ; - xef = q->vm->xef; -/* * Only sample the first LRC. For parallel submission, all of them are * scheduled together and we compensate that below by multiplying by @@ -789,7 +786,7 @@ void xe_exec_queue_update_runtime(struct xe_exec_queue *q)*/ lrc = &q->lrc[0 ]; new_ts =xe_lrc_update_timestamp (lrc, &old_ts); - xef->runtime[q->class] += (new_ts - old_ts) * q->width; + q->runtime += (new_ts - old_ts) * q->width; }void xe_exec_queue_kill (struct xe_exec_queue *q)drivers/gpu/drm/xe/xe_exec_queue_types.h +0
-0drivers/gpu/drm/xe/xe_exec_queue_types.h struct xe_exec_queue { · line 154
Quoted 4 lines
drivers/gpu/drm/xe/xe_exec_queue_types.h +1
-0@@ -151,6 +151,8 @@ struct xe_exec_queue {* Protected by @vm's resv. Unused if @vm == NULL. */ u64 tlb_flush_seqno; +/** @runtime: hw engine class runtime in ticks for this exec queue*/ ^missing space ^
good idea to move to the
exec_queuedrivers/gpu/drm/xe/xe_exec_queue_types.h struct xe_exec_queue { · line 155
Quoted 1 line
drivers/gpu/drm/xe/xe_exec_queue_types.h +1
-0+u64 runtime;could we please find a better name for this? runtime what?
or do you meanrun_time?
Is this the total run time of the exec queue expressed in ticks of the eu clock?
Quoted 4 lines
My poor brain parses runtime as the phase of the operation, vs
run_timeas the time spent running.or perhaps '
run_ticks'?
It was xef->runtime earlier, so just carried over the name to exec queue as is. xef still has a runtime field that will accumulate the q->runtime, so I would need to change it there as well. I will pick run_ticks for now since it is indeed ticks.
Thanks,
Umesh
Quoted 7 lines
Quoted 7 lines
drivers/gpu/drm/xe/xe_exec_queue_types.h +0
-1/** @lrc: logical ring context for this exec queue */ struct xe_lrc lrc[]; }; --2.34.1