drm/sched: run free_job work on timeout workqueue
Subsystems (3)
PATCH 1/1 drm/sched: run free_job work on timeout workqueue
From: Vitaly Prosyak <[email protected]>
Currently drm_sched runs run_job and free_job on the per-scheduler ordered submit workqueue, while timeouts (drm_sched_job_timedout()) run on sched->timeout_wq (e.g. amdgpu reset_domain->wq).
For drivers like amdgpu the reset path entered from timeout_wq may still access the guilty job while free_job, running on submit_wq, frees it. This allows a use-after-free when recovery continues to touch job fields after drm_sched_free_job_work() has called ops->free_job().
Queue work_free_job on sched->timeout_wq instead of submit_wq, both from __drm_sched_run_free_queue() and drm_sched_wqueue_start(), so timeout/reset and free_job are always serialized on the same workqueue.
Behavior changes:
- work_run_job stays on sched->submit_wq (ordered).
- work_free_job moves to sched->timeout_wq (timeout/reset queue).
- Submission and freeing may now run in parallel on different workqueues, but all shared state is already protected by job_list_lock and atomics.
Pros:
- Eliminates reset vs free_job race (no freeing while reset still uses the job).
- Matches the logical model: timeout selects guilty job and recovery, including freeing, is handled on one queue.
Cons / considerations:
- For users that don’t provide timeout_wq, free_job moves from the per-sched ordered queue to system_wq, which slightly changes scheduling behaviour but keeps correctness.
--- drivers/gpu/drm/scheduler/sched_main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +2 -2
@@ -355,7 +355,7 @@ static void drm_sched_run_job_queue(struct drm_gpu_scheduler *sched)static void __drm_sched_run_free_queue (struct drm_gpu_scheduler *sched) {if (!READ_ONCE (sched->pause_submit)) -queue_work (sched->submit_wq, &sched->work_free_job); +queue_work (sched->timeout_wq, &sched->work_free_job); }/**
@@ -1493,6 +1493,6 @@ void drm_sched_wqueue_start(struct drm_gpu_scheduler *sched) {WRITE_ONCE (sched->pause_submit, false);queue_work (sched->submit_wq, &sched->work_run_job); -queue_work (sched->submit_wq, &sched->work_free_job); +queue_work (sched->timeout_wq, &sched->work_free_job); }EXPORT_SYMBOL (drm_sched_wqueue_start);
Signature · 1 line
2.51.2
Quoted 31 lines · On 11/28/25 19:22, [email protected] wrote:
From: Vitaly Prosyak <[email protected]>
Currently
drm_schedrunsrun_jobandfree_jobon the per-scheduler ordered submit workqueue, while timeouts (drm_sched_job_timedout()) run onsched->timeout_wq(e.g. amdgpureset_domain->wq).For drivers like amdgpu the reset path entered from
timeout_wqmay still access the guilty job whilefree_job, running onsubmit_wq, frees it. This allows a use-after-free when recovery continues to touch job fields afterdrm_sched_free_job_work()has calledops->free_job().Queue
work_free_jobonsched->timeout_wqinstead ofsubmit_wq, both from__drm_sched_run_free_queue()anddrm_sched_wqueue_start(), so timeout/reset andfree_jobare always serialized on the same workqueue.Behavior changes:
-
work_run_jobstays onsched->submit_wq(ordered).
-work_free_jobmoves tosched->timeout_wq(timeout/reset queue).
- Submission and freeing may now run in parallel on different workqueues, but all shared state is already protected byjob_list_lockand atomics.Pros:
- Eliminates reset vsfree_jobrace (no freeing while reset still uses the job).
- Matches the logical model: timeout selects guilty job and recovery, including freeing, is handled on one queue.
Quoted 4 lines
Cons / considerations:
- For users that don’t providetimeout_wq,free_jobmoves from the per-sched ordered queue tosystem_wq, which slightly changes scheduling behaviour but keeps correctness.
We should probably avoid that and use a single ordered wq for submit, timeout, free when the driver doesn't provide one.
We should potentially also add a warning/error when the driver supplied wq isn't ordered.
Apart from that the change looks sane to me and avoid all the hacky workarounds around job lifetime.
But removing those workarounds is should probably be a second step.
Regards,
Christian.
Quoted 32 lines
Cc Philipp Stanner <[email protected]>Cc Alex Deucher <[email protected]>Cc Christian König <[email protected]>Suggested-by Mathew from Intel during XDCSuggested-by Christian König <[email protected]>Signed-off-by Vitaly Prosyak <[email protected]>--- drivers/gpu/drm/scheduler/sched_main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/gpu/drm/scheduler/sched_main.c +2
-2@@ -355,7 +355,7 @@ static void drm_sched_run_job_queue(struct drm_gpu_scheduler *sched)static void __drm_sched_run_free_queue (struct drm_gpu_scheduler *sched) {if (!READ_ONCE (sched->pause_submit)) -queue_work (sched->submit_wq, &sched->work_free_job); +queue_work (sched->timeout_wq, &sched->work_free_job); }/** @@ -1493,6 +1493,6 @@ void drm_sched_wqueue_start(struct drm_gpu_scheduler *sched) {WRITE_ONCE (sched->pause_submit, false);queue_work (sched->submit_wq, &sched->work_run_job); -queue_work (sched->submit_wq, &sched->work_free_job); +queue_work (sched->timeout_wq, &sched->work_free_job); }EXPORT_SYMBOL (drm_sched_wqueue_start);
+Cc Matthew, Danilo, who are also drm/sched maintainers
The get_maintainer script provides you with a list of maintainers.
Quoted 26 lines · Christian König
Quoted 25 lines · On 11/28/25 19:22, [email protected] wrote:
From: Vitaly Prosyak <[email protected]>
Currently
drm_schedrunsrun_jobandfree_jobon the per-scheduler ordered submit workqueue, while timeouts (drm_sched_job_timedout()) run onsched->timeout_wq(e.g. amdgpureset_domain->wq).For drivers like amdgpu the reset path entered from
timeout_wqmay still access the guilty job whilefree_job, running onsubmit_wq, frees it. This allows a use-after-free when recovery continues to touch job fields afterdrm_sched_free_job_work()has calledops->free_job().Queue
work_free_jobonsched->timeout_wqinstead ofsubmit_wq, both from__drm_sched_run_free_queue()anddrm_sched_wqueue_start(), so timeout/reset andfree_jobare always serialized on the same workqueue.Behavior changes:
-
work_run_jobstays onsched->submit_wq(ordered).
-work_free_jobmoves tosched->timeout_wq(timeout/reset queue).
- Submission and freeing may now run in parallel on different workqueues, but all shared state is already protected byjob_list_lockand atomics.
Quoted 3 lines
Quoted 3 lines
Pros:
- Eliminates reset vsfree_jobrace (no freeing while reset still uses the job).
It eliminates such a race *in amdgpu*. Other drivers might not have that problem. I think Intel/Xe is refcounting jobs? Matthew?
Quoted 9 lines
Quoted 7 lines
- Matches the logical model: timeout selects guilty job and recovery, including freeing, is handled on one queue.
Cons / considerations:
- For users that don’t providetimeout_wq,free_jobmoves from the per-sched ordered queue tosystem_wq, which slightly changes scheduling behaviour but keeps correctness.We should probably avoid that and use a single ordered wq for submit, timeout, free when the driver doesn't provide one.
AFAICS this fix doesn't fix anything for certain at all, because you just don't know what kind of workqueue the other drivers have passed for timeout_wq.
Quoted 4 lines
We should potentially also add a warning/error when the driver supplied wq isn't ordered.
Apart from that the change looks sane to me and avoid all the hacky workarounds around job lifetime.
I'm not convinced that this is the right path forward.
The fundamental issue here is the gross design problem within drm/sched that drivers *create* jobs, but the scheduler *frees* them at an unpredictable point in time.
This entire fix idea seems to circle around the concept of relying yet again on the scheduler's internal behavior (i.e., when it schedules the call to free_job()).
I think we discussed that at XDC, but how I see it if drivers have strange job life time requirements where a job shall outlive drm_sched's free_job() call, they must solve that with a proper synchronization mechanism.
The first question would be: what does amdgpu need the job for after free_job() ran? What do you even need a job for still after there was a timeout?
And if you really still need its contents, can't you memcpy() the job or something?
Assuming that it really needs it and that that cannot easily be solved, I suppose the obvious answer for differing memory life times is refcounting. So amdgpu could just let drm_sched drop its reference in free_job(), and from then onward it's amdgpu's problem.
I hope Matthew can educate us on how Xe does it.
AFAIK Nouveau doesn't have that problem, because on timeout we just terminate the channel.
Would also be interesting to hear whether other driver folks have the problem of free_job() being racy.
+Cc Boris, Lucas.
P.
Quoted 39 lines
But removing those workarounds is should probably be a second step.
Regards,
Christian.Quoted 32 lines
Cc Philipp Stanner <[email protected]>Cc Alex Deucher <[email protected]>Cc Christian König <[email protected]>Suggested-by Mathew from Intel during XDCSuggested-by Christian König <[email protected]>Signed-off-by Vitaly Prosyak <[email protected]>---
drivers/gpu/drm/scheduler/sched_main.c | 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)drivers/gpu/drm/scheduler/sched_main.c +1
-1@@ -355,7 +355,7 @@ static void drm_sched_run_job_queue(struct drm_gpu_scheduler *sched) static void __drm_sched_run_free_queue(struct drm_gpu_scheduler *sched) { if (!READ_ONCE(sched->pause_submit)) -queue_work (sched->submit_wq, &sched->work_free_job); +queue_work (sched->timeout_wq, &sched->work_free_job); }
/**drivers/gpu/drm/scheduler/sched_main.c +1
-1@@ -1493,6 +1493,6 @@ void drm_sched_wqueue_start(struct drm_gpu_scheduler *sched) { WRITE_ONCE(sched->pause_submit, false); queue_work(sched->submit_wq, &sched->work_run_job); -queue_work (sched->submit_wq, &sched->work_free_job); +queue_work (sched->timeout_wq, &sched->work_free_job); } EXPORT_SYMBOL(drm_sched_wqueue_start);
Quoted 55 lines · Philipp Stanner
+Cc Matthew, Danilo, who are also drm/sched maintainers
The
get_maintainerscript provides you with a list of maintainers.Quoted 26 lines · Christian König
Quoted 25 lines · On 11/28/25 19:22, [email protected] wrote:
From: Vitaly Prosyak <[email protected]>
Currently
drm_schedrunsrun_jobandfree_jobon the per-scheduler ordered submit workqueue, while timeouts (drm_sched_job_timedout()) run onsched->timeout_wq(e.g. amdgpureset_domain->wq).For drivers like amdgpu the reset path entered from
timeout_wqmay still access the guilty job whilefree_job, running onsubmit_wq, frees it. This allows a use-after-free when recovery continues to touch job fields afterdrm_sched_free_job_work()has calledops->free_job().Queue
work_free_jobonsched->timeout_wqinstead ofsubmit_wq, both from__drm_sched_run_free_queue()anddrm_sched_wqueue_start(), so timeout/reset andfree_jobare always serialized on the same workqueue.Behavior changes:
-
work_run_jobstays onsched->submit_wq(ordered).
-work_free_jobmoves tosched->timeout_wq(timeout/reset queue).
- Submission and freeing may now run in parallel on different workqueues, but all shared state is already protected byjob_list_lockand atomics.Quoted 3 lines
Quoted 3 lines
Pros:
- Eliminates reset vsfree_jobrace (no freeing while reset still uses the job).It eliminates such a race *in amdgpu*. Other drivers might not have that problem. I think Intel/Xe is refcounting jobs? Matthew?
Quoted 9 lines
Quoted 7 lines
- Matches the logical model: timeout selects guilty job and recovery, including freeing, is handled on one queue.
Cons / considerations:
- For users that don’t providetimeout_wq,free_jobmoves from the per-sched ordered queue tosystem_wq, which slightly changes scheduling behaviour but keeps correctness.We should probably avoid that and use a single ordered wq for submit, timeout, free when the driver doesn't provide one.
AFAICS this fix doesn't fix anything for certain at all, because you just don't know what kind of workqueue the other drivers have passed for
timeout_wq.Quoted 3 lines
We should potentially also add a warning/error when the driver supplied wq isn't ordered.
Quoted 7 lines
Quoted 1 line
Apart from that the change looks sane to me and avoid all the hacky workarounds around job lifetime.
I'm not convinced that this is the right path forward.
The fundamental issue here is the gross design problem within drm/sched that drivers *create* jobs, but the scheduler *frees* them at an unpredictable point in time.
Yeah, can't agree more!
Quoted 8 lines
This entire fix idea seems to circle around the concept of relying yet again on the scheduler's internal behavior (i.e., when it schedules the call to
free_job()).I think we discussed that at XDC, but how I see it if drivers have strange job life time requirements where a job shall outlive drm_sched's
free_job()call, they must solve that with a proper synchronization mechanism.
Well that is not correct as far as I can see.
The problem here is rather that the scheduler gives the job as parameter to the timedout_job() callback, but doesn't guarantee that ->free_job() callback isn't called while timedout_job() runs.
This should be prevented by removing the job in question from the pending list (see drm_sched_job_timedout), but for some reason that doesn't seem to work correctly.
Quoted 3 lines
The first question would be: what does amdgpu need the job for after
free_job()ran? What do you even need a job for still after there was a timeout?
No, we just need the job structure alive as long as the timedout_job() callback is running.
Quoted 9 lines
And if you really still need its contents, can't you
memcpy()the job or something?Assuming that it really needs it and that that cannot easily be solved, I suppose the obvious answer for differing memory life times is refcounting. So amdgpu could just let
drm_scheddrop its reference infree_job(), and from then onward it's amdgpu's problem.I hope Matthew can educate us on how Xe does it.
We discussed this on XDC and it was Matthew who brought up that this can be solved by running timeout and free worker on the same single threaded wq.
Quoted 6 lines
AFAIK Nouveau doesn't have that problem, because on timeout we just terminate the channel.
Would also be interesting to hear whether other driver folks have the problem of
free_job()being racy.
I think that this is still a general problem with the drm scheduler and not driver specific at all.
Regards,
Christian.
Quoted 47 lines
+Cc Boris, Lucas.
P.
Quoted 39 lines
But removing those workarounds is should probably be a second step.
Regards,
Christian.Quoted 32 lines
Cc Philipp Stanner <[email protected]>Cc Alex Deucher <[email protected]>Cc Christian König <[email protected]>Suggested-by Mathew from Intel during XDCSuggested-by Christian König <[email protected]>Signed-off-by Vitaly Prosyak <[email protected]>---
drivers/gpu/drm/scheduler/sched_main.c | 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)drivers/gpu/drm/scheduler/sched_main.c +1
-1@@ -355,7 +355,7 @@ static void drm_sched_run_job_queue(struct drm_gpu_scheduler *sched) static void __drm_sched_run_free_queue(struct drm_gpu_scheduler *sched) { if (!READ_ONCE(sched->pause_submit)) -queue_work (sched->submit_wq, &sched->work_free_job); +queue_work (sched->timeout_wq, &sched->work_free_job); }
/**drivers/gpu/drm/scheduler/sched_main.c +1
-1@@ -1493,6 +1493,6 @@ void drm_sched_wqueue_start(struct drm_gpu_scheduler *sched) { WRITE_ONCE(sched->pause_submit, false); queue_work(sched->submit_wq, &sched->work_run_job); -queue_work (sched->submit_wq, &sched->work_free_job); +queue_work (sched->timeout_wq, &sched->work_free_job); } EXPORT_SYMBOL(drm_sched_wqueue_start);
Matthew Brost
maintainerQuoted 32 lines · Christian König
Quoted 31 lines · Philipp Stanner
+Cc Matthew, Danilo, who are also drm/sched maintainers
The
get_maintainerscript provides you with a list of maintainers.Quoted 26 lines · Christian König
Quoted 25 lines · On 11/28/25 19:22, [email protected] wrote:
From: Vitaly Prosyak <[email protected]>
Currently
drm_schedrunsrun_jobandfree_jobon the per-scheduler ordered submit workqueue, while timeouts (drm_sched_job_timedout()) run onsched->timeout_wq(e.g. amdgpureset_domain->wq).For drivers like amdgpu the reset path entered from
timeout_wqmay still access the guilty job whilefree_job, running onsubmit_wq, frees it. This allows a use-after-free when recovery continues to touch job fields afterdrm_sched_free_job_work()has calledops->free_job().Queue
work_free_jobonsched->timeout_wqinstead ofsubmit_wq, both from__drm_sched_run_free_queue()anddrm_sched_wqueue_start(), so timeout/reset andfree_jobare always serialized on the same workqueue.Behavior changes:
-
work_run_jobstays onsched->submit_wq(ordered).
-work_free_jobmoves tosched->timeout_wq(timeout/reset queue).
- Submission and freeing may now run in parallel on different workqueues, but all shared state is already protected byjob_list_lockand atomics.
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Pros:
- Eliminates reset vsfree_jobrace (no freeing while reset still uses the job).It eliminates such a race *in amdgpu*. Other drivers might not have that problem. I think Intel/Xe is refcounting jobs? Matthew?
We schedule device resets on the same work queue as the job timeout queue, as well as operations like VF migrations, since conceptually they are quite similar to device resets. In both cases, we have the capability to stop all schedulers on the device and prevent any future schedules from being created while these processes are ongoing.
Putting it all together: when either of these device-wide operations is running, we know that no job timeouts will occur and no scheduler operations (e.g., run_job, free_job, etc.) will race. I suggest that all other drivers requiring device-wide operations follow this approach, as it seems to work quite well. In other words, even if free_job is moved to the timeout work queue, I’m fairly certain you would still schedule device-wide operations on the timeout work queue and still stop all schedulers before any device operation touches scheduler or queue state.
Therefore, I don’t believe Xe actually has a problem here.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
- Matches the logical model: timeout selects guilty job and recovery, including freeing, is handled on one queue.
Cons / considerations:
- For users that don’t providetimeout_wq,free_jobmoves from the per-sched ordered queue tosystem_wq, which slightly changes
s/system_wq/system_percpu_wq
Ok, the system_percpu_wq doesn't actually for timeout_wq as that work queue is reclaim unsafe. We probably should just remove that fallback in the scheduler.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
scheduling behaviour but keeps correctness.
We should probably avoid that and use a single ordered wq for submit, timeout, free when the driver doesn't provide one.
Ah, yes agree. I typed the same thing above before reading this.
Quoted 9 lines
Quoted 9 lines
AFAICS this fix doesn't fix anything for certain at all, because you just don't know what kind of workqueue the other drivers have passed for
timeout_wq.Quoted 4 lines
We should potentially also add a warning/error when the driver supplied wq isn't ordered.
Apart from that the change looks sane to me and avoid all the hacky workarounds around job lifetime.
A step further: the work queue also needs to be reclaim-safe. I started a series [1] to enforce this policy but inevitably got distracted by other priorities. Eventually, I’ll revive it—or if someone else wants to take it on, feel free.
[1] https://patchwork.freedesktop.org/series/156284/
Quoted 9 lines
Quoted 6 lines
I'm not convinced that this is the right path forward.
The fundamental issue here is the gross design problem within drm/sched that drivers *create* jobs, but the scheduler *frees* them at an unpredictable point in time.
Yeah, can't agree more!
The scheduler doesn’t free jobs; it simply drops a reference count. free_job should be renamed, in my opinion, to reflect this. Once that misnomer is fixed, the design actually makes sense. The scheduler holds a reference to the job until its fence signals and until it is removed from internal tracking. Transferring ownership via reference counting is actually quite common and well understood. The scheduler takes ownership of a ref when the job is pushed to the scheduler.
Quoted 9 lines
Quoted 8 lines
This entire fix idea seems to circle around the concept of relying yet again on the scheduler's internal behavior (i.e., when it schedules the call to
free_job()).I think we discussed that at XDC, but how I see it if drivers have strange job life time requirements where a job shall outlive drm_sched's
free_job()call, they must solve that with a proper synchronization mechanism.
Quoted 6 lines
Well that is not correct as far as I can see.
The problem here is rather that the scheduler gives the job as parameter to the
timedout_job()callback, but doesn't guarantee that ->free_job() callback isn't called whiletimedout_job()runs.This should be prevented by removing the job in question from the pending list (see
drm_sched_job_timedout), but for some reason that doesn't seem to work correctly.
Are you sure this is happening? It doesn’t seem possible, nor have I observed it.
What actually looks like a problem is that in drm_sched_job_timedout, free_job can be called. Look at [2]—if you’re using free_guilty (Xe isn’t, but [2] was Xe trying to do the same thing), this is actually unsafe. The free_guilty code should likely be removed as that definitely can explode under the right conditions.
[2] git format-patch -1 ea2f6a77d0c40
Quoted 6 lines
Quoted 3 lines
The first question would be: what does amdgpu need the job for after
free_job()ran? What do you even need a job for still after there was a timeout?No, we just need the job structure alive as long as the
timedout_job()callback is running.
Yes, I agree.
Quoted 12 lines
Quoted 9 lines
And if you really still need its contents, can't you
memcpy()the job or something?Assuming that it really needs it and that that cannot easily be solved, I suppose the obvious answer for differing memory life times is refcounting. So amdgpu could just let
drm_scheddrop its reference infree_job(), and from then onward it's amdgpu's problem.I hope Matthew can educate us on how Xe does it.
We discussed this on XDC and it was Matthew who brought up that this can be solved by running timeout and free worker on the same single threaded wq.
No, see my explainations above. This is not my suggestion.
Quoted 9 lines
Quoted 6 lines
AFAIK Nouveau doesn't have that problem, because on timeout we just terminate the channel.
Would also be interesting to hear whether other driver folks have the problem of
free_job()being racy.I think that this is still a general problem with the drm scheduler and not driver specific at all.
Maybe the free_guilty is likely a scheduler problem but I'm not seeing an issue aside from that.
Matt
Quoted 51 lines
Regards,
Christian.Quoted 47 lines
+Cc Boris, Lucas.
P.
Quoted 39 lines
But removing those workarounds is should probably be a second step.
Regards,
Christian.Quoted 32 lines
Cc Philipp Stanner <[email protected]>Cc Alex Deucher <[email protected]>Cc Christian König <[email protected]>Suggested-by Mathew from Intel during XDCSuggested-by Christian König <[email protected]>Signed-off-by Vitaly Prosyak <[email protected]>---
drivers/gpu/drm/scheduler/sched_main.c | 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)drivers/gpu/drm/scheduler/sched_main.c +1
-1@@ -355,7 +355,7 @@ static void drm_sched_run_job_queue(struct drm_gpu_scheduler *sched) static void __drm_sched_run_free_queue(struct drm_gpu_scheduler *sched) { if (!READ_ONCE(sched->pause_submit)) -queue_work (sched->submit_wq, &sched->work_free_job); +queue_work (sched->timeout_wq, &sched->work_free_job); }
/**drivers/gpu/drm/scheduler/sched_main.c +1
-1@@ -1493,6 +1493,6 @@ void drm_sched_wqueue_start(struct drm_gpu_scheduler *sched) { WRITE_ONCE(sched->pause_submit, false); queue_work(sched->submit_wq, &sched->work_run_job); -queue_work (sched->submit_wq, &sched->work_free_job); +queue_work (sched->timeout_wq, &sched->work_free_job); } EXPORT_SYMBOL(drm_sched_wqueue_start);
Quoted 3 lines · Matthew Brost
Quoted 2 lines · Christian König
Quoted 1 line · Philipp Stanner
[…]
Quoted 10 lines
Quoted 3 lines
Quoted 3 lines
It eliminates such a race *in amdgpu*. Other drivers might not have that problem. I think Intel/Xe is refcounting jobs? Matthew?
We schedule device resets on the same work queue as the job timeout queue, as well as operations like VF migrations, since conceptually they are quite similar to device resets. In both cases, we have the capability to stop all schedulers on the device and prevent any future schedules from being created while these processes are ongoing.
Quoted 8 lines
Putting it all together: when either of these device-wide operations is running, we know that no job timeouts will occur and no scheduler operations (e.g.,
run_job,free_job, etc.) will race. I suggest that all other drivers requiring device-wide operations follow this approach, as it seems to work quite well. In other words, even iffree_jobis moved to the timeout work queue, I’m fairly certain you would still schedule device-wide operations on the timeout work queue and still stop all schedulers before any device operation touches scheduler or queue state.
Thx for the explanation.
As far as I'm aware, everyone concerned already follows the (by now officially documented) approach of stopping drm_sched on reset.
We have never really documented very well when and why one should share the timeout_wq, though.
Someone who thinks he understands that really well should document that in drm_sched_init_args.
Quoted 10 lines
Therefore, I don’t believe Xe actually has a problem here.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
- Matches the logical model: timeout selects guilty job and recovery, including freeing, is handled on one queue.
Cons / considerations:
- For users that don’t providetimeout_wq,free_jobmoves from the per-sched ordered queue tosystem_wq, which slightly changes
Quoted 5 lines
s/system_wq/system_percpu_wq
Ok, the
system_percpu_wqdoesn't actually fortimeout_wqas that work queue is reclaim unsafe. We probably should just remove that fallback in the scheduler.
Which one, exactly?
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
scheduling behaviour but keeps correctness.
We should probably avoid that and use a single ordered wq for submit, timeout, free when the driver doesn't provide one.
Ah, yes agree. I typed the same thing above before reading this.
Why do we even provide a submit_wq?
The timeout_wq I still understand somewhat, since it can help ordering racing timeouts. But submit?
Quoted 1 line
[…]
Quoted 11 lines
Quoted 9 lines
Quoted 6 lines
I'm not convinced that this is the right path forward.
The fundamental issue here is the gross design problem within drm/sched that drivers *create* jobs, but the scheduler *frees* them at an unpredictable point in time.
Yeah, can't agree more!
The scheduler doesn’t free jobs; it simply drops a reference count.
In our discussions, it is astonishing me at times with what naturalness you make that statement.
Who has ever defined, who has ever documented that this is how the scheduler is supposed to be used?
Let's look at the docu:
/** * @free_job: Called once the job's finished fence has been signaled * and it's time to clean it up. */ void (*free_job)(struct drm_sched_job *sched_job);
"it's time to clean it up"
Quoted 3 lines
free_jobshould be renamed, in my opinion, to reflect this. Once that misnomer is fixed, the design actually makes sense.
How do you know that this is the design idea? Since when do you know? Why was it never documented? It's kind of important.
Quoted 4 lines
The scheduler holds
a reference to the job until its fence signals and until it is removed from internal tracking.
Let's look at tho documentation for drm_sched_job:
* @entity: the entity to which this job belongs.
* @cb: the callback for the parent fence in s_fence.
*
* A job is created by the driver using drm_sched_job_init(), and
* should call drm_sched_entity_push_job() once it wants the scheduler
* to schedule the job.
*/
Do you see any clue anywhere at all that jobs are supposed to be refcounted? Where is the scheduler's reference (refcount++) taken? That could maybe even be taken race-free by drm_sched (likely in drm_sched_entity_push_job()), but I strongly suspect that what you're hinting at is that the driver (i.e., Xe) takes and frees the refcount for drm_sched.
Quoted 3 lines
Transferring ownership via reference counting is
actually quite common and well understood. The scheduler takes ownership of a ref when the job is pushed to the scheduler.
s/takes/should take
Don't get me wrong, I *think* that your design suggestion to refcount jobs is probably (considering the circumstances we got ourselves into) correct. I think I would support porting drm_sched to refcounting jobs.
But your way to formulate it as an absolute truth is plainly wrong. It is not the design reality, many drivers don't just drop a refcount in free_job; and it's not documented.
Moreover, I think if refcounting jobs were a reality free_job() could just be deleted very easily, since the scheduler could drop its refcount whenever it pleases, without calling back into the driver.
Quoted 12 lines
Quoted 12 lines
Quoted 9 lines
This entire fix idea seems to circle around the concept of relying yet again on the scheduler's internal behavior (i.e., when it schedules the call to
free_job()).I think we discussed that at XDC, but how I see it if drivers have strange job life time requirements where a job shall outlive drm_sched's
free_job()call, they must solve that with a proper synchronization mechanism.Well that is not correct as far as I can see.
Quoted 7 lines
Quoted 4 lines
The problem here is rather that the scheduler gives the job as parameter to the
timedout_job()callback, but doesn't guarantee that ->free_job() callback isn't called whiletimedout_job()runs.This should be prevented by removing the job in question from the pending list (see
drm_sched_job_timedout), but for some reason that doesn't seem to work correctly.Are you sure this is happening? It doesn’t seem possible, nor have I observed it.
It's impossible, isn't it?
static void drm_sched_job_timedout(struct work_struct *work) { struct drm_gpu_scheduler *sched; struct drm_sched_job *job; enum drm_gpu_sched_stat status = DRM_GPU_SCHED_STAT_RESET; sched = container_of(work, struct drm_gpu_scheduler, work_tdr.work); /* Protects against concurrent deletion in drm_sched_get_finished_job */ spin_lock(&sched->job_list_lock); job = list_first_entry_or_null(&sched->pending_list, struct drm_sched_job, list); if (job) { /* * Remove the bad job so it cannot be freed by a concurrent * &struct drm_sched_backend_ops.free_job. It will be * reinserted after the scheduler's work items have been * cancelled, at which point it's safe. */ list_del_init(&job->list); spin_unlock(&sched->job_list_lock); status = job->sched->ops->timedout_job(job);
1. scheduler takes list lock
2. removes job from list
3. unlocks
4. calls timedout_job callback
How can free_job_work, through drm_sched_get_finished_job(), get and free the same job?
The pending_list is probably the one place where we actually lock consistently and sanely.
I think this needs to be debugged more intensively, Christian.
Quoted 6 lines
What actually looks like a problem is that in
drm_sched_job_timedout,free_jobcan be called. Look at [2]—if you’re usingfree_guilty(Xe isn’t, but [2] was Xe trying to do the same thing), this is actually unsafe. Thefree_guiltycode should likely be removed as that definitely can explode under the right conditions.
I'm right now not even sure why free_guilty exists, but I don't see how it's illegal for drm_sched to call free_job in drm_sched_job_timedout?
free_job can be called at any point in time, drivers must expect that. No lock is being held, and timedout_job already ran. So what's the problem?
For drivers with additional refcounting it would be even less of a problem.
Quoted 11 lines
[2] git format-patch -1
ea2f6a77d0c40Quoted 6 lines
Quoted 3 lines
The first question would be: what does amdgpu need the job for after
free_job()ran? What do you even need a job for still after there was a timeout?No, we just need the job structure alive as long as the
timedout_job()callback is running.Yes, I agree.
As far as I can see that's how it's already implemented? No one can free that job while timedout_job() is running in drm_sched_job_timedout().
Quoted 20 lines
Quoted 4 lines
Quoted 4 lines
And if you really still need its contents, can't you
memcpy()the job or something?Quoted 9 lines
Quoted 6 lines
Assuming that it really needs it and that that cannot easily be solved, I suppose the obvious answer for differing memory life times is refcounting. So amdgpu could just let
drm_scheddrop its reference infree_job(), and from then onward it's amdgpu's problem.I hope Matthew can educate us on how Xe does it.
We discussed this on XDC and it was Matthew who brought up that this can be solved by running timeout and free worker on the same single threaded wq.
No, see my explainations above. This is not my suggestion.
Quoted 4 lines
Quoted 4 lines
AFAIK Nouveau doesn't have that problem, because on timeout we just terminate the channel.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Would also be interesting to hear whether other driver folks have the problem of
free_job()being racy.I think that this is still a general problem with the drm scheduler and not driver specific at all.
Maybe the
free_guiltyis likely a scheduler problem but I'm not seeing an issue aside from that.
I also can't see the bug. I fail to see how drm_sched can free a job that's currently in use in timedout_job(). If that can happen, Christian, Vitaly, please point us to where and how. Only then can we decide on how to fix it properly.
P.
Quoted 7 lines · Philipp Stanner
Quoted 6 lines · Matthew Brost
Quoted 2 lines · Christian König
Quoted 1 line · Philipp Stanner
[…]
Quoted 7 lines
Quoted 2 lines
Are you sure this is happening? It doesn’t seem possible, nor have I observed it.
It's impossible, isn't it?
static void
drm_sched_job_timedout(struct work_struct *work){ structdrm_gpu_scheduler*sched; structdrm_sched_job*job; enumdrm_gpu_sched_statstatus =DRM_GPU_SCHED_STAT_RESET; sched =container_of(work, struct drm_gpu_scheduler, work_tdr.work); /* Protects against concurrent deletion indrm_sched_get_finished_job*/spin_lock(&sched->job_list_lock); job =list_first_entry_or_null(&sched->pending_list, struct drm_sched_job, list); if (job) { /* * Remove the bad job so it cannot be freed by a concurrent * &struct drm_sched_backend_ops.free_job. It will be * reinserted after the scheduler's work items have been * cancelled, at which point it's safe. */list_del_init(&job->list);spin_unlock(&sched->job_list_lock); status =job->sched->ops->timedout_job(job);
sry guys, seems I really need to switch to Mutt. When I clicked "send" the formatting definitely was OK, but in my outbox it's now also broken. No idea why.
Please take a look at drm_sched_job_timedout() in your tree. That's the code I'm referring to.
P.
Matthew Brost
maintainerQuoted 26 lines · Philipp Stanner
Quoted 3 lines · Matthew Brost
Quoted 2 lines · Christian König
Quoted 1 line · Philipp Stanner
[…]
Quoted 10 lines
Quoted 3 lines
Quoted 3 lines
It eliminates such a race *in amdgpu*. Other drivers might not have that problem. I think Intel/Xe is refcounting jobs? Matthew?
We schedule device resets on the same work queue as the job timeout queue, as well as operations like VF migrations, since conceptually they are quite similar to device resets. In both cases, we have the capability to stop all schedulers on the device and prevent any future schedules from being created while these processes are ongoing.
Quoted 8 lines
Putting it all together: when either of these device-wide operations is running, we know that no job timeouts will occur and no scheduler operations (e.g.,
run_job,free_job, etc.) will race. I suggest that all other drivers requiring device-wide operations follow this approach, as it seems to work quite well. In other words, even iffree_jobis moved to the timeout work queue, I’m fairly certain you would still schedule device-wide operations on the timeout work queue and still stop all schedulers before any device operation touches scheduler or queue state.
Quoted 8 lines
Thx for the explanation.
As far as I'm aware, everyone concerned already follows the (by now officially documented) approach of stopping
drm_schedon reset.We have never really documented very well when and why one should share the
timeout_wq, though.
I'm not sure if it's documented. When I started working on Xe, Boris had recently added the timedout_wq, which, from my understanding, was intended to schedule all reset-type events (I believe Sima suggested this to me).
A lot of the things I did early in Xe weren’t the best from an upstream community perspective. To be honest, I really didn’t know what I was doing and typed a lot.
Quoted 3 lines
Someone who thinks he understands that really well should document that in
drm_sched_init_args.
I can take a pass at this. Did you documentation ever land? Building upon that would be good.
Quoted 10 lines
Quoted 10 lines
Therefore, I don’t believe Xe actually has a problem here.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
- Matches the logical model: timeout selects guilty job and recovery, including freeing, is handled on one queue.
Cons / considerations:
- For users that don’t providetimeout_wq,free_jobmoves from the per-sched ordered queue tosystem_wq, which slightly changes
Quoted 8 lines
Quoted 5 lines
s/system_wq/system_percpu_wq
Ok, the
system_percpu_wqdoesn't actually fortimeout_wqas that work queue is reclaim unsafe. We probably should just remove that fallback in the scheduler.Which one, exactly?
System work queues don’t work for either timedout_wq or submit_wq. In both cases, we need the workqueue marked with WQ_RECLAIM and must ensure that no GFP_KERNEL allocations are performed by any work item scheduled on the workqueue, which is impossible to guarantee with any current system workqueue.
Quoted 12 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
scheduling behaviour but keeps correctness.
We should probably avoid that and use a single ordered wq for submit, timeout, free when the driver doesn't provide one.
Ah, yes agree. I typed the same thing above before reading this.
Why do we even provide a
submit_wq?The
timeout_wqI still understand somewhat, since it can help ordering racing timeouts. But submit?
We could probably get rid of submit_wq, but I don’t see a significant upside in doing so.
Quoted 20 lines
Quoted 2 lines
[…]
Quoted 11 lines
Quoted 9 lines
Quoted 6 lines
I'm not convinced that this is the right path forward.
The fundamental issue here is the gross design problem within drm/sched that drivers *create* jobs, but the scheduler *frees* them at an unpredictable point in time.
Yeah, can't agree more!
The scheduler doesn’t free jobs; it simply drops a reference count.
In our discussions, it is astonishing me at times with what naturalness you make that statement.
Quoted 9 lines
Who has ever defined, who has ever documented that this is how the scheduler is supposed to be used?
Let's look at the docu:
/** * @free_job: Called once the job's finished fence has been signaled * and it's time to clean it up. */ void (*free_job)(struct
drm_sched_job*sched_job);"it's time to clean it up"
Again, this is definitely not documented. I quickly found that if you directly free jobs in free_job(), and the job pusher touches it after the push, things can explode with the right race condition. Thus, I reasoned that jobs need to be reference-counted. Pretty much every driver actually does this, with AMD being a notable exception, perhaps a few others.
Quoted 8 lines
Quoted 4 lines
free_jobshould be renamed, in my opinion, to reflect this. Once that misnomer is fixed, the design actually makes sense.How do you know that this is the design idea? Since when do you know? Why was it never documented? It's kind of important.
See above—I reasoned this one on my own and wasn’t a great upstream citizen with respect to cleaning up DRM scheduler code or documenting it.
Quoted 15 lines
Quoted 4 lines
The scheduler holds
a reference to the job until its fence signals and until it is removed from internal tracking.Let's look at tho documentation for
drm_sched_job:* @entity: the entity to which this job belongs.
* @cb: the callback for the parent fence ins_fence.
*
* A job is created by the driver usingdrm_sched_job_init(), and
* should calldrm_sched_entity_push_job()once it wants the scheduler
* to schedule the job.
*/
Quoted 7 lines
Do you see any clue anywhere at all that jobs are supposed to be refcounted? Where is the scheduler's reference (refcount++) taken? That could maybe even be taken race-free by
drm_sched(likely indrm_sched_entity_push_job()), but I strongly suspect that what you're hinting at is that the driver (i.e., Xe) takes and frees the refcount fordrm_sched.
No, agree this should documented something like this:
A job is created by the driver using drm_sched_job_init() and should call drm_sched_entity_push_job() when it wants the scheduler to schedule the job. Once drm_sched_entity_push_job() is called, the DRM scheduler owns a driver-side reference to the job, which is released in the put_job() driver callback.
The above is akin to the fence returned from run_job() transferring a reference to the DRM scheduler, which it releases when the scheduler fence is done with the parent. So, the concept was understood—albeit one that wasn’t documented for a very long time.
Quoted 7 lines
Quoted 4 lines
Transferring ownership via reference counting is
actually quite common and well understood. The scheduler takes ownership of a ref when the job is pushed to the scheduler.s/takes/should take
Quoted 8 lines
Don't get me wrong, I *think* that your design suggestion to refcount jobs is probably (considering the circumstances we got ourselves into) correct. I think I would support porting
drm_schedto refcounting jobs.But your way to formulate it as an absolute truth is plainly wrong. It is not the design reality, many drivers don't just drop a refcount in
free_job; and it's not documented.
If it’s coming off as the absolute truth—that’s not my intent. I’m trying to help make sense of the DRM scheduler design so that it works for everyone.
Quoted 4 lines
Moreover, I think if refcounting jobs were a reality
free_job()could just be deleted very easily, since the scheduler could drop its refcount whenever it pleases, without calling back into the driver.
Right now, all the reference counting of jobs is embedded in the driver-side job objects. However, if we moved the reference count into the base DRM scheduler object, this would work. Of course, drivers would need an option to override the destruction function, as the final put on the object could have other side effects—for example in Xe, putting other objects and dropping the RPM reference. So, we’d need a vfunc somewhere, and I’m unsure if surgery across subsystems and drivers is worth it.
Quoted 25 lines
Quoted 12 lines
Quoted 12 lines
Quoted 9 lines
This entire fix idea seems to circle around the concept of relying yet again on the scheduler's internal behavior (i.e., when it schedules the call to
free_job()).I think we discussed that at XDC, but how I see it if drivers have strange job life time requirements where a job shall outlive drm_sched's
free_job()call, they must solve that with a proper synchronization mechanism.Well that is not correct as far as I can see.
Quoted 7 lines
Quoted 4 lines
The problem here is rather that the scheduler gives the job as parameter to the
timedout_job()callback, but doesn't guarantee that ->free_job() callback isn't called whiletimedout_job()runs.This should be prevented by removing the job in question from the pending list (see
drm_sched_job_timedout), but for some reason that doesn't seem to work correctly.Are you sure this is happening? It doesn’t seem possible, nor have I observed it.
It's impossible, isn't it?
static void
drm_sched_job_timedout(struct work_struct *work){ structdrm_gpu_scheduler*sched; structdrm_sched_job*job; enumdrm_gpu_sched_statstatus =DRM_GPU_SCHED_STAT_RESET; sched =container_of(work, struct drm_gpu_scheduler, work_tdr.work); /* Protects against concurrent deletion indrm_sched_get_finished_job*/spin_lock(&sched->job_list_lock); job =list_first_entry_or_null(&sched->pending_list, struct drm_sched_job, list); if (job) { /* * Remove the bad job so it cannot be freed by a concurrent * &struct drm_sched_backend_ops.free_job. It will be * reinserted after the scheduler's work items have been * cancelled, at which point it's safe. */list_del_init(&job->list);spin_unlock(&sched->job_list_lock); status =job->sched->ops->timedout_job(job);
Quoted 9 lines
1. scheduler takes list lock
2. removes job from list
3. unlocks
4. callstimedout_jobcallbackHow can
free_job_work, throughdrm_sched_get_finished_job(), get and free the same job?
It can't.
Quoted 7 lines
The
pending_listis probably the one place where we actually lock consistently and sanely.I think this needs to be debugged more intensively, Christian.
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
What actually looks like a problem is that in
drm_sched_job_timedout,free_jobcan be called. Look at [2]—if you’re usingfree_guilty(Xe isn’t, but [2] was Xe trying to do the same thing), this is actually unsafe. Thefree_guiltycode should likely be removed as that definitely can explode under the right conditions.I'm right now not even sure why
free_guiltyexists, but I don't see how
I'm sure why free_guilty exists either. If the fence is signaled in timedout job free_job will get scheduled on another work_item.
Quoted 9 lines
it's illegal for
drm_schedto callfree_jobindrm_sched_job_timedout?
free_jobcan be called at any point in time, drivers must expect that. No lock is being held, andtimedout_jobalready ran. So what's the problem?For drivers with additional refcounting it would be even less of a problem.
No, the scheduler can still reference the job.
1265 fence = sched->ops->run_job(sched_job); 1266 complete_all(&entity->entity_idle); 1267 drm_sched_fence_scheduled(s_fence, fence);
1268
1269 if (!IS_ERR_OR_NULL(fence)) {
1270 r = dma_fence_add_callback(fence, &sched_job->cb,
1271 drm_sched_job_done_cb);
1272 if (r == -ENOENT)
1273 drm_sched_job_done(sched_job, fence->error);
1274 else if (r)
1275 DRM_DEV_ERROR(sched->dev, "fence add callback failed (%d)\n", r);
1276
1277 dma_fence_put(fence);
1278 } else {
1279 drm_sched_job_done(sched_job, IS_ERR(fence) ?
1280 PTR_ERR(fence) : 0);
1281 }
1282
1283 wake_up(&sched->job_scheduled);
1284 drm_sched_run_job_queue(sched);
At line 1269, the run_job work item is interrupted. Timed-out jobs run, call free_job, which performs the final put. Then the run_job work item resumes—and boom, UAF. Using the same reasoning, I think moving free_job to the timed-out work queue could also cause issues.
If run_job work item took a reference to the job before adding it to the pending list and dropped it after it was done touching it in this function, then yes, that would be safe. This is an argument for moving reference counting into the base DRM scheduler class, it would make ownership clear rather than relying on ordered work queues to keep everything safe.
Quoted 7 lines
Quoted 7 lines
[2] git format-patch -1
ea2f6a77d0c40Quoted 4 lines
Quoted 3 lines
The first question would be: what does amdgpu need the job for after
free_job()ran? What do you even need a job for still after there was a timeout?
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
No, we just need the job structure alive as long as the
timedout_job()callback is running.Yes, I agree.
As far as I can see that's how it's already implemented? No one can free that job while
timedout_job()is running indrm_sched_job_timedout().
See above, free guility is still problematic.
Quoted 26 lines
Quoted 26 lines
Quoted 4 lines
Quoted 4 lines
And if you really still need its contents, can't you
memcpy()the job or something?Quoted 9 lines
Quoted 6 lines
Assuming that it really needs it and that that cannot easily be solved, I suppose the obvious answer for differing memory life times is refcounting. So amdgpu could just let
drm_scheddrop its reference infree_job(), and from then onward it's amdgpu's problem.I hope Matthew can educate us on how Xe does it.
We discussed this on XDC and it was Matthew who brought up that this can be solved by running timeout and free worker on the same single threaded wq.
No, see my explainations above. This is not my suggestion.
Quoted 9 lines
Quoted 6 lines
AFAIK Nouveau doesn't have that problem, because on timeout we just terminate the channel.
Would also be interesting to hear whether other driver folks have the problem of
free_job()being racy.I think that this is still a general problem with the drm scheduler and not driver specific at all.
Quoted 8 lines
Quoted 2 lines
Maybe the
free_guiltyis likely a scheduler problem but I'm not seeing an issue aside from that.I also can't see the bug. I fail to see how
drm_schedcan free a job that's currently in use intimedout_job(). If that can happen, Christian, Vitaly, please point us to where and how. Only then can we decide on how to fix it properly.
Again see above.
Matt
Quoted 4 lines
P.
Matthew Brost
maintainerQuoted 306 lines · Matthew Brost
Quoted 26 lines · Philipp Stanner
Quoted 3 lines · Matthew Brost
Quoted 2 lines · Christian König
Quoted 1 line · Philipp Stanner
[…]
Quoted 10 lines
Quoted 3 lines
Quoted 3 lines
It eliminates such a race *in amdgpu*. Other drivers might not have that problem. I think Intel/Xe is refcounting jobs? Matthew?
We schedule device resets on the same work queue as the job timeout queue, as well as operations like VF migrations, since conceptually they are quite similar to device resets. In both cases, we have the capability to stop all schedulers on the device and prevent any future schedules from being created while these processes are ongoing.
Quoted 8 lines
Putting it all together: when either of these device-wide operations is running, we know that no job timeouts will occur and no scheduler operations (e.g.,
run_job,free_job, etc.) will race. I suggest that all other drivers requiring device-wide operations follow this approach, as it seems to work quite well. In other words, even iffree_jobis moved to the timeout work queue, I’m fairly certain you would still schedule device-wide operations on the timeout work queue and still stop all schedulers before any device operation touches scheduler or queue state.Quoted 8 lines
Thx for the explanation.
As far as I'm aware, everyone concerned already follows the (by now officially documented) approach of stopping
drm_schedon reset.We have never really documented very well when and why one should share the
timeout_wq, though.I'm not sure if it's documented. When I started working on Xe, Boris had recently added the
timedout_wq, which, from my understanding, was intended to schedule all reset-type events (I believe Sima suggested this to me).A lot of the things I did early in Xe weren’t the best from an upstream community perspective. To be honest, I really didn’t know what I was doing and typed a lot.
Quoted 3 lines
Someone who thinks he understands that really well should document that in
drm_sched_init_args.I can take a pass at this. Did you documentation ever land? Building upon that would be good.
Quoted 10 lines
Quoted 10 lines
Therefore, I don’t believe Xe actually has a problem here.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
- Matches the logical model: timeout selects guilty job and recovery, including freeing, is handled on one queue.
Cons / considerations:
- For users that don’t providetimeout_wq,free_jobmoves from the per-sched ordered queue tosystem_wq, which slightly changesQuoted 8 lines
Quoted 5 lines
s/system_wq/system_percpu_wq
Ok, the
system_percpu_wqdoesn't actually fortimeout_wqas that work queue is reclaim unsafe. We probably should just remove that fallback in the scheduler.Which one, exactly?
System work queues don’t work for either
timedout_wqorsubmit_wq. In both cases, we need the workqueue marked withWQ_RECLAIMand must ensure that noGFP_KERNELallocations are performed by any work item scheduled on the workqueue, which is impossible to guarantee with any current system workqueue.Quoted 12 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
scheduling behaviour but keeps correctness.
We should probably avoid that and use a single ordered wq for submit, timeout, free when the driver doesn't provide one.
Ah, yes agree. I typed the same thing above before reading this.
Why do we even provide a
submit_wq?The
timeout_wqI still understand somewhat, since it can help ordering racing timeouts. But submit?We could probably get rid of
submit_wq, but I don’t see a significant upside in doing so.Quoted 20 lines
Quoted 2 lines
[…]
Quoted 11 lines
Quoted 9 lines
Quoted 6 lines
I'm not convinced that this is the right path forward.
The fundamental issue here is the gross design problem within drm/sched that drivers *create* jobs, but the scheduler *frees* them at an unpredictable point in time.
Yeah, can't agree more!
The scheduler doesn’t free jobs; it simply drops a reference count.
In our discussions, it is astonishing me at times with what naturalness you make that statement.
Quoted 9 lines
Who has ever defined, who has ever documented that this is how the scheduler is supposed to be used?
Let's look at the docu:
/** * @free_job: Called once the job's finished fence has been signaled * and it's time to clean it up. */ void (*free_job)(struct
drm_sched_job*sched_job);"it's time to clean it up"
Again, this is definitely not documented. I quickly found that if you directly free jobs in
free_job(), and the job pusher touches it after the push, things can explode with the right race condition. Thus, I reasoned that jobs need to be reference-counted. Pretty much every driver actually does this, with AMD being a notable exception, perhaps a few others.Quoted 8 lines
Quoted 4 lines
free_jobshould be renamed, in my opinion, to reflect this. Once that misnomer is fixed, the design actually makes sense.How do you know that this is the design idea? Since when do you know? Why was it never documented? It's kind of important.
See above—I reasoned this one on my own and wasn’t a great upstream citizen with respect to cleaning up DRM scheduler code or documenting it.
Quoted 15 lines
Quoted 4 lines
The scheduler holds
a reference to the job until its fence signals and until it is removed from internal tracking.Let's look at tho documentation for
drm_sched_job:* @entity: the entity to which this job belongs.
* @cb: the callback for the parent fence ins_fence.
*
* A job is created by the driver usingdrm_sched_job_init(), and
* should calldrm_sched_entity_push_job()once it wants the scheduler
* to schedule the job.
*/Quoted 7 lines
Do you see any clue anywhere at all that jobs are supposed to be refcounted? Where is the scheduler's reference (refcount++) taken? That could maybe even be taken race-free by
drm_sched(likely indrm_sched_entity_push_job()), but I strongly suspect that what you're hinting at is that the driver (i.e., Xe) takes and frees the refcount fordrm_sched.No, agree this should documented something like this:
A job is created by the driver using
drm_sched_job_init()and should calldrm_sched_entity_push_job()when it wants the scheduler to schedule the job. Oncedrm_sched_entity_push_job()is called, the DRM scheduler owns a driver-side reference to the job, which is released in theput_job()driver callback.The above is akin to the fence returned from
run_job()transferring a reference to the DRM scheduler, which it releases when the scheduler fence is done with the parent. So, the concept was understood—albeit one that wasn’t documented for a very long time.Quoted 7 lines
Quoted 4 lines
Transferring ownership via reference counting is
actually quite common and well understood. The scheduler takes ownership of a ref when the job is pushed to the scheduler.s/takes/should take
Quoted 8 lines
Don't get me wrong, I *think* that your design suggestion to refcount jobs is probably (considering the circumstances we got ourselves into) correct. I think I would support porting
drm_schedto refcounting jobs.But your way to formulate it as an absolute truth is plainly wrong. It is not the design reality, many drivers don't just drop a refcount in
free_job; and it's not documented.If it’s coming off as the absolute truth—that’s not my intent. I’m trying to help make sense of the DRM scheduler design so that it works for everyone.
Quoted 4 lines
Moreover, I think if refcounting jobs were a reality
free_job()could just be deleted very easily, since the scheduler could drop its refcount whenever it pleases, without calling back into the driver.Right now, all the reference counting of jobs is embedded in the driver-side job objects. However, if we moved the reference count into the base DRM scheduler object, this would work. Of course, drivers would need an option to override the destruction function, as the final put on the object could have other side effects—for example in Xe, putting other objects and dropping the RPM reference. So, we’d need a vfunc somewhere, and I’m unsure if surgery across subsystems and drivers is worth it.
Quoted 25 lines
Quoted 12 lines
Quoted 12 lines
Quoted 9 lines
This entire fix idea seems to circle around the concept of relying yet again on the scheduler's internal behavior (i.e., when it schedules the call to
free_job()).I think we discussed that at XDC, but how I see it if drivers have strange job life time requirements where a job shall outlive drm_sched's
free_job()call, they must solve that with a proper synchronization mechanism.Well that is not correct as far as I can see.
Quoted 7 lines
Quoted 4 lines
The problem here is rather that the scheduler gives the job as parameter to the
timedout_job()callback, but doesn't guarantee that ->free_job() callback isn't called whiletimedout_job()runs.This should be prevented by removing the job in question from the pending list (see
drm_sched_job_timedout), but for some reason that doesn't seem to work correctly.Are you sure this is happening? It doesn’t seem possible, nor have I observed it.
It's impossible, isn't it?
static void
drm_sched_job_timedout(struct work_struct *work){ structdrm_gpu_scheduler*sched; structdrm_sched_job*job; enumdrm_gpu_sched_statstatus =DRM_GPU_SCHED_STAT_RESET; sched =container_of(work, struct drm_gpu_scheduler, work_tdr.work); /* Protects against concurrent deletion indrm_sched_get_finished_job*/spin_lock(&sched->job_list_lock); job =list_first_entry_or_null(&sched->pending_list, struct drm_sched_job, list); if (job) { /* * Remove the bad job so it cannot be freed by a concurrent * &struct drm_sched_backend_ops.free_job. It will be * reinserted after the scheduler's work items have been * cancelled, at which point it's safe. */list_del_init(&job->list);spin_unlock(&sched->job_list_lock); status =job->sched->ops->timedout_job(job);Quoted 9 lines
1. scheduler takes list lock
2. removes job from list
3. unlocks
4. callstimedout_jobcallbackHow can
free_job_work, throughdrm_sched_get_finished_job(), get and free the same job?It can't.
Quoted 7 lines
The
pending_listis probably the one place where we actually lock consistently and sanely.I think this needs to be debugged more intensively, Christian.
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
What actually looks like a problem is that in
drm_sched_job_timedout,free_jobcan be called. Look at [2]—if you’re usingfree_guilty(Xe isn’t, but [2] was Xe trying to do the same thing), this is actually unsafe. Thefree_guiltycode should likely be removed as that definitely can explode under the right conditions.I'm right now not even sure why
free_guiltyexists, but I don't see howI'm sure why
free_guiltyexists either. If the fence is signaled in timedout jobfree_jobwill get scheduled on anotherwork_item.Quoted 9 lines
it's illegal for
drm_schedto callfree_jobindrm_sched_job_timedout?
free_jobcan be called at any point in time, drivers must expect that. No lock is being held, andtimedout_jobalready ran. So what's the problem?For drivers with additional refcounting it would be even less of a problem.
No, the scheduler can still reference the job.
1265 fence = sched->ops->run_job(sched_job); 1266 complete_all(&entity->entity_idle); 1267 drm_sched_fence_scheduled(s_fence, fence);1268
1269 if (!IS_ERR_OR_NULL(fence)) {
1270 r = dma_fence_add_callback(fence,&sched_job->cb,
1271drm_sched_job_done_cb);
1272 if (r ==-ENOENT)
1273drm_sched_job_done(sched_job, fence->error);
1274 else if (r)
1275DRM_DEV_ERROR(sched->dev, "fence add callback failed (%d)\n", r);
1276
1277dma_fence_put(fence);
1278 } else {
1279 drm_sched_job_done(sched_job,IS_ERR(fence)?
1280PTR_ERR(fence): 0);
1281 }
1282
1283wake_up(&sched->job_scheduled);
1284drm_sched_run_job_queue(sched);At line 1269, the
run_jobwork item is interrupted. Timed-out jobs run, callfree_job, which performs the final put. Then therun_jobwork item resumes—and boom, UAF. Using the same reasoning, I think movingfree_jobto the timed-out work queue could also cause issues.
Quoted 4 lines
If
run_jobwork item took a reference to the job before adding it to the pending list and dropped it after it was done touching it in this function, then yes, that would be safe. This is an argument for moving reference counting into the base DRM scheduler class, it would make
typo: s/DRM scheduler class/DRM job class
Matt
Quoted 65 lines
ownership clear rather than relying on ordered work queues to keep everything safe.
Quoted 7 lines
Quoted 7 lines
[2] git format-patch -1
ea2f6a77d0c40Quoted 4 lines
Quoted 3 lines
The first question would be: what does amdgpu need the job for after
free_job()ran? What do you even need a job for still after there was a timeout?Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
No, we just need the job structure alive as long as the
timedout_job()callback is running.Yes, I agree.
As far as I can see that's how it's already implemented? No one can free that job while
timedout_job()is running indrm_sched_job_timedout().See above, free guility is still problematic.
Quoted 26 lines
Quoted 26 lines
Quoted 4 lines
Quoted 4 lines
And if you really still need its contents, can't you
memcpy()the job or something?Quoted 9 lines
Quoted 6 lines
Assuming that it really needs it and that that cannot easily be solved, I suppose the obvious answer for differing memory life times is refcounting. So amdgpu could just let
drm_scheddrop its reference infree_job(), and from then onward it's amdgpu's problem.I hope Matthew can educate us on how Xe does it.
We discussed this on XDC and it was Matthew who brought up that this can be solved by running timeout and free worker on the same single threaded wq.
No, see my explainations above. This is not my suggestion.
Quoted 9 lines
Quoted 6 lines
AFAIK Nouveau doesn't have that problem, because on timeout we just terminate the channel.
Would also be interesting to hear whether other driver folks have the problem of
free_job()being racy.I think that this is still a general problem with the drm scheduler and not driver specific at all.
Quoted 8 lines
Quoted 2 lines
Maybe the
free_guiltyis likely a scheduler problem but I'm not seeing an issue aside from that.I also can't see the bug. I fail to see how
drm_schedcan free a job that's currently in use intimedout_job(). If that can happen, Christian, Vitaly, please point us to where and how. Only then can we decide on how to fix it properly.Again see above.
Matt
Quoted 4 lines
P.
Quoted 5 lines · Matthew Brost
Quoted 4 lines · Matthew Brost
Quoted 3 lines · Philipp Stanner
Quoted 2 lines · Matthew Brost
Quoted 1 line · Christian König
On 12/1/25 10:04, Philipp Stanner wrote:
....
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 11 lines
Quoted 11 lines
Quoted 8 lines
This entire fix idea seems to circle around the concept of relying yet again on the scheduler's internal behavior (i.e., when it schedules the call to
free_job()).I think we discussed that at XDC, but how I see it if drivers have strange job life time requirements where a job shall outlive drm_sched's
free_job()call, they must solve that with a proper synchronization mechanism.Well that is not correct as far as I can see.
Quoted 7 lines
Quoted 4 lines
The problem here is rather that the scheduler gives the job as parameter to the
timedout_job()callback, but doesn't guarantee that ->free_job() callback isn't called whiletimedout_job()runs.This should be prevented by removing the job in question from the pending list (see
drm_sched_job_timedout), but for some reason that doesn't seem to work correctly.Are you sure this is happening? It doesn’t seem possible, nor have I observed it.
It's impossible, isn't it?
static void
drm_sched_job_timedout(struct work_struct *work){ structdrm_gpu_scheduler*sched; structdrm_sched_job*job; enumdrm_gpu_sched_statstatus =DRM_GPU_SCHED_STAT_RESET; sched =container_of(work, struct drm_gpu_scheduler, work_tdr.work); /* Protects against concurrent deletion indrm_sched_get_finished_job*/spin_lock(&sched->job_list_lock); job =list_first_entry_or_null(&sched->pending_list, struct drm_sched_job, list); if (job) { /* * Remove the bad job so it cannot be freed by a concurrent * &struct drm_sched_backend_ops.free_job. It will be * reinserted after the scheduler's work items have been * cancelled, at which point it's safe. */list_del_init(&job->list);spin_unlock(&sched->job_list_lock); status =job->sched->ops->timedout_job(job);1. scheduler takes list lock
2. removes job from list
3. unlocks
4. callstimedout_jobcallback
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
How can
free_job_work, throughdrm_sched_get_finished_job(), get and free the same job?It can't.
But exactly that happens somehow. Don't ask me how, I have no idea.
My educated guess is that the job somehow ends up on the pending list again.
Quoted 57 lines
Quoted 57 lines
Quoted 8 lines
The
pending_listis probably the one place where we actually lock consistently and sanely.I think this needs to be debugged more intensively, Christian.
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
What actually looks like a problem is that in
drm_sched_job_timedout,free_jobcan be called. Look at [2]—if you’re usingfree_guilty(Xe isn’t, but [2] was Xe trying to do the same thing), this is actually unsafe. Thefree_guiltycode should likely be removed as that definitely can explode under the right conditions.I'm right now not even sure why
free_guiltyexists, but I don't see howI'm sure why
free_guiltyexists either. If the fence is signaled in timedout jobfree_jobwill get scheduled on anotherwork_item.Quoted 9 lines
it's illegal for
drm_schedto callfree_jobindrm_sched_job_timedout?
free_jobcan be called at any point in time, drivers must expect that. No lock is being held, andtimedout_jobalready ran. So what's the problem?For drivers with additional refcounting it would be even less of a problem.
No, the scheduler can still reference the job.
1265 fence = sched->ops->run_job(sched_job); 1266 complete_all(&entity->entity_idle); 1267 drm_sched_fence_scheduled(s_fence, fence);1268
1269 if (!IS_ERR_OR_NULL(fence)) {
1270 r = dma_fence_add_callback(fence,&sched_job->cb,
1271drm_sched_job_done_cb);
1272 if (r ==-ENOENT)
1273drm_sched_job_done(sched_job, fence->error);
1274 else if (r)
1275DRM_DEV_ERROR(sched->dev, "fence add callback failed (%d)\n", r);
1276
1277dma_fence_put(fence);
1278 } else {
1279 drm_sched_job_done(sched_job,IS_ERR(fence)?
1280PTR_ERR(fence): 0);
1281 }
1282
1283wake_up(&sched->job_scheduled);
1284drm_sched_run_job_queue(sched);At line 1269, the
run_jobwork item is interrupted. Timed-out jobs run, callfree_job, which performs the final put. Then therun_jobwork item resumes—and boom, UAF. Using the same reasoning, I think movingfree_jobto the timed-out work queue could also cause issues.
Quoted 6 lines
Quoted 4 lines
If
run_jobwork item took a reference to the job before adding it to the pending list and dropped it after it was done touching it in this function, then yes, that would be safe. This is an argument for moving reference counting into the base DRM scheduler class, it would maketypo: s/DRM scheduler class/DRM job class
That strongly sounds like re-inventing the scheduler fence.
What if we completely drop the job object? Or merge it into the scheduler fence?
The fence has reference counting, proper state transitions and a well defined lifetime.
We would just need ->schedule and ->finished functions instead of ->run_job and ->free_job. Those callbacks would then still be called by the scheduler in work item context instead of the irq context of the dma_fence callbacks.
The job can then be a void* in the scheduler fence where drivers can put anything they want and also drivers control the lifetime of that. E.g. they can free it during ->schedule as well as during ->finished.
Christian.
Quoted 68 lines
Matt
Quoted 65 lines
ownership clear rather than relying on ordered work queues to keep everything safe.
Quoted 7 lines
Quoted 7 lines
[2] git format-patch -1
ea2f6a77d0c40Quoted 4 lines
Quoted 3 lines
The first question would be: what does amdgpu need the job for after
free_job()ran? What do you even need a job for still after there was a timeout?Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
No, we just need the job structure alive as long as the
timedout_job()callback is running.Yes, I agree.
As far as I can see that's how it's already implemented? No one can free that job while
timedout_job()is running indrm_sched_job_timedout().See above, free guility is still problematic.
Quoted 26 lines
Quoted 26 lines
Quoted 4 lines
Quoted 4 lines
And if you really still need its contents, can't you
memcpy()the job or something?Quoted 9 lines
Quoted 6 lines
Assuming that it really needs it and that that cannot easily be solved, I suppose the obvious answer for differing memory life times is refcounting. So amdgpu could just let
drm_scheddrop its reference infree_job(), and from then onward it's amdgpu's problem.I hope Matthew can educate us on how Xe does it.
We discussed this on XDC and it was Matthew who brought up that this can be solved by running timeout and free worker on the same single threaded wq.
No, see my explainations above. This is not my suggestion.
Quoted 9 lines
Quoted 6 lines
AFAIK Nouveau doesn't have that problem, because on timeout we just terminate the channel.
Would also be interesting to hear whether other driver folks have the problem of
free_job()being racy.I think that this is still a general problem with the drm scheduler and not driver specific at all.
Quoted 8 lines
Quoted 2 lines
Maybe the
free_guiltyis likely a scheduler problem but I'm not seeing an issue aside from that.I also can't see the bug. I fail to see how
drm_schedcan free a job that's currently in use intimedout_job(). If that can happen, Christian, Vitaly, please point us to where and how. Only then can we decide on how to fix it properly.Again see above.
Matt
Quoted 4 lines
P.
Matthew Brost
maintainerQuoted 104 lines · Christian König
Quoted 5 lines · Matthew Brost
Quoted 4 lines · Matthew Brost
Quoted 3 lines · Philipp Stanner
Quoted 2 lines · Matthew Brost
Quoted 1 line · Christian König
On 12/1/25 10:04, Philipp Stanner wrote:
....
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 11 lines
Quoted 11 lines
Quoted 8 lines
This entire fix idea seems to circle around the concept of relying yet again on the scheduler's internal behavior (i.e., when it schedules the call to
free_job()).I think we discussed that at XDC, but how I see it if drivers have strange job life time requirements where a job shall outlive drm_sched's
free_job()call, they must solve that with a proper synchronization mechanism.Well that is not correct as far as I can see.
Quoted 7 lines
Quoted 4 lines
The problem here is rather that the scheduler gives the job as parameter to the
timedout_job()callback, but doesn't guarantee that ->free_job() callback isn't called whiletimedout_job()runs.This should be prevented by removing the job in question from the pending list (see
drm_sched_job_timedout), but for some reason that doesn't seem to work correctly.Are you sure this is happening? It doesn’t seem possible, nor have I observed it.
It's impossible, isn't it?
static void
drm_sched_job_timedout(struct work_struct *work){ structdrm_gpu_scheduler*sched; structdrm_sched_job*job; enumdrm_gpu_sched_statstatus =DRM_GPU_SCHED_STAT_RESET; sched =container_of(work, struct drm_gpu_scheduler, work_tdr.work); /* Protects against concurrent deletion indrm_sched_get_finished_job*/spin_lock(&sched->job_list_lock); job =list_first_entry_or_null(&sched->pending_list, struct drm_sched_job, list); if (job) { /* * Remove the bad job so it cannot be freed by a concurrent * &struct drm_sched_backend_ops.free_job. It will be * reinserted after the scheduler's work items have been * cancelled, at which point it's safe. */list_del_init(&job->list);spin_unlock(&sched->job_list_lock); status =job->sched->ops->timedout_job(job);1. scheduler takes list lock
2. removes job from list
3. unlocks
4. callstimedout_jobcallbackQuoted 5 lines
Quoted 5 lines
Quoted 3 lines
How can
free_job_work, throughdrm_sched_get_finished_job(), get and free the same job?It can't.
But exactly that happens somehow. Don't ask me how, I have no idea.
My educated guess is that the job somehow ends up on the pending list again.
Quoted 57 lines
Quoted 57 lines
Quoted 8 lines
The
pending_listis probably the one place where we actually lock consistently and sanely.I think this needs to be debugged more intensively, Christian.
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
What actually looks like a problem is that in
drm_sched_job_timedout,free_jobcan be called. Look at [2]—if you’re usingfree_guilty(Xe isn’t, but [2] was Xe trying to do the same thing), this is actually unsafe. Thefree_guiltycode should likely be removed as that definitely can explode under the right conditions.I'm right now not even sure why
free_guiltyexists, but I don't see howI'm sure why
free_guiltyexists either. If the fence is signaled in timedout jobfree_jobwill get scheduled on anotherwork_item.Quoted 9 lines
it's illegal for
drm_schedto callfree_jobindrm_sched_job_timedout?
free_jobcan be called at any point in time, drivers must expect that. No lock is being held, andtimedout_jobalready ran. So what's the problem?For drivers with additional refcounting it would be even less of a problem.
No, the scheduler can still reference the job.
1265 fence = sched->ops->run_job(sched_job); 1266 complete_all(&entity->entity_idle); 1267 drm_sched_fence_scheduled(s_fence, fence);1268
1269 if (!IS_ERR_OR_NULL(fence)) {
1270 r = dma_fence_add_callback(fence,&sched_job->cb,
1271drm_sched_job_done_cb);
1272 if (r ==-ENOENT)
1273drm_sched_job_done(sched_job, fence->error);
1274 else if (r)
1275DRM_DEV_ERROR(sched->dev, "fence add callback failed (%d)\n", r);
1276
1277dma_fence_put(fence);
1278 } else {
1279 drm_sched_job_done(sched_job,IS_ERR(fence)?
1280PTR_ERR(fence): 0);
1281 }
1282
1283wake_up(&sched->job_scheduled);
1284drm_sched_run_job_queue(sched);At line 1269, the
run_jobwork item is interrupted. Timed-out jobs run, callfree_job, which performs the final put. Then therun_jobwork item resumes—and boom, UAF. Using the same reasoning, I think movingfree_jobto the timed-out work queue could also cause issues.
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
If
run_jobwork item took a reference to the job before adding it to the pending list and dropped it after it was done touching it in this function, then yes, that would be safe. This is an argument for moving reference counting into the base DRM scheduler class, it would maketypo: s/DRM scheduler class/DRM job class
That strongly sounds like re-inventing the scheduler fence.
Perhaps.
Quoted 5 lines
What if we completely drop the job object? Or merge it into the scheduler fence?
The fence has reference counting, proper state transitions and a well defined lifetime.
We would just need ->schedule and ->finished functions instead of ->run_job and ->free_job. Those callbacks would then still be called by the scheduler in work item context instead of the irq context of the
dma_fencecallbacks.
Yes, definitely no IRQ contexts.
Quoted 3 lines
The job can then be a void* in the scheduler fence where drivers can put anything they want and also drivers control the lifetime of that. E.g. they can free it during ->schedule as well as during ->finished.
I think this is a reasonable idea, but it would require major surgery across the subsystem plus the 11 upstream drivers I’m counting that use DRM scheduler. This would be a huge coordinated effort.
So I see three options:
1. Rename free_job to put_job and document usage. Rip out free_guilty. Likely the easiest and least invasive.
2. Move reference counting to the base DRM scheduler job object, provide a vfunc for the final job put, and document usage. Medium invasive.
3. Move job (driver) side tracking to the scheduler fence and let it control the lifetime. Very invasive.
I’ll support any option, but my personal bandwidth to dive into something like #3 just isn’t there (of course, I can help review scheduler changes and fix up Xe, etc.).
Matt
Quoted 71 lines
Christian.
Quoted 68 lines
Matt
Quoted 65 lines
ownership clear rather than relying on ordered work queues to keep everything safe.
Quoted 7 lines
Quoted 7 lines
[2] git format-patch -1
ea2f6a77d0c40Quoted 4 lines
Quoted 3 lines
The first question would be: what does amdgpu need the job for after
free_job()ran? What do you even need a job for still after there was a timeout?Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
No, we just need the job structure alive as long as the
timedout_job()callback is running.Yes, I agree.
As far as I can see that's how it's already implemented? No one can free that job while
timedout_job()is running indrm_sched_job_timedout().See above, free guility is still problematic.
Quoted 26 lines
Quoted 26 lines
Quoted 4 lines
Quoted 4 lines
And if you really still need its contents, can't you
memcpy()the job or something?Quoted 9 lines
Quoted 6 lines
Assuming that it really needs it and that that cannot easily be solved, I suppose the obvious answer for differing memory life times is refcounting. So amdgpu could just let
drm_scheddrop its reference infree_job(), and from then onward it's amdgpu's problem.I hope Matthew can educate us on how Xe does it.
We discussed this on XDC and it was Matthew who brought up that this can be solved by running timeout and free worker on the same single threaded wq.
No, see my explainations above. This is not my suggestion.
Quoted 9 lines
Quoted 6 lines
AFAIK Nouveau doesn't have that problem, because on timeout we just terminate the channel.
Would also be interesting to hear whether other driver folks have the problem of
free_job()being racy.I think that this is still a general problem with the drm scheduler and not driver specific at all.
Quoted 8 lines
Quoted 2 lines
Maybe the
free_guiltyis likely a scheduler problem but I'm not seeing an issue aside from that.I also can't see the bug. I fail to see how
drm_schedcan free a job that's currently in use intimedout_job(). If that can happen, Christian, Vitaly, please point us to where and how. Only then can we decide on how to fix it properly.Again see above.
Matt
Quoted 4 lines
P.
Quoted 38 lines · Matthew Brost
Quoted 37 lines · Christian König
Quoted 5 lines · Matthew Brost
Quoted 4 lines · Matthew Brost
Quoted 3 lines · Philipp Stanner
Quoted 2 lines · Matthew Brost
Quoted 1 line · Christian König
On 12/1/25 10:04, Philipp Stanner wrote:
....
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 11 lines
Quoted 11 lines
Quoted 8 lines
This entire fix idea seems to circle around the concept of relying yet again on the scheduler's internal behavior (i.e., when it schedules the call to
free_job()).I think we discussed that at XDC, but how I see it if drivers have strange job life time requirements where a job shall outlive drm_sched's
free_job()call, they must solve that with a proper synchronization mechanism.Well that is not correct as far as I can see.
Quoted 7 lines
Quoted 4 lines
The problem here is rather that the scheduler gives the job as parameter to the
timedout_job()callback, but doesn't guarantee that ->free_job() callback isn't called whiletimedout_job()runs.This should be prevented by removing the job in question from the pending list (see
drm_sched_job_timedout), but for some reason that doesn't seem to work correctly.Are you sure this is happening? It doesn’t seem possible, nor have I observed it.
It's impossible, isn't it?
static void
drm_sched_job_timedout(struct work_struct *work){ structdrm_gpu_scheduler*sched; structdrm_sched_job*job; enumdrm_gpu_sched_statstatus =DRM_GPU_SCHED_STAT_RESET; sched =container_of(work, struct drm_gpu_scheduler, work_tdr.work); /* Protects against concurrent deletion indrm_sched_get_finished_job*/spin_lock(&sched->job_list_lock); job =list_first_entry_or_null(&sched->pending_list, struct drm_sched_job, list); if (job) { /* * Remove the bad job so it cannot be freed by a concurrent * &struct drm_sched_backend_ops.free_job. It will be * reinserted after the scheduler's work items have been * cancelled, at which point it's safe. */list_del_init(&job->list);spin_unlock(&sched->job_list_lock); status =job->sched->ops->timedout_job(job);1. scheduler takes list lock
2. removes job from list
3. unlocks
4. callstimedout_jobcallback
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
How can
free_job_work, throughdrm_sched_get_finished_job(), get and free the same job?It can't.
But exactly that happens somehow. Don't ask me how, I have no idea.
*Philipp refuses to elaborate and asks Christian*
How are you so sure about that's what's happening? Anyways, assuming it is true:
Quoted 2 lines
Quoted 2 lines
My educated guess is that the job somehow ends up on the pending list again.
then the obvious question would be: does amdgpu touch the pending_list itself, or does it only ever modify it through proper scheduler APIs?
Quoted 82 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 9 lines
The
pending_listis probably the one place where we actually lock consistently and sanely.I think this needs to be debugged more intensively, Christian.
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
What actually looks like a problem is that in
drm_sched_job_timedout,free_jobcan be called. Look at [2]—if you’re usingfree_guilty(Xe isn’t, but [2] was Xe trying to do the same thing), this is actually unsafe. Thefree_guiltycode should likely be removed as that definitely can explode under the right conditions.I'm right now not even sure why
free_guiltyexists, but I don't see howI'm sure why
free_guiltyexists either. If the fence is signaled in timedout jobfree_jobwill get scheduled on anotherwork_item.Quoted 9 lines
it's illegal for
drm_schedto callfree_jobindrm_sched_job_timedout?
free_jobcan be called at any point in time, drivers must expect that. No lock is being held, andtimedout_jobalready ran. So what's the problem?For drivers with additional refcounting it would be even less of a problem.
No, the scheduler can still reference the job.
1265 fence =
sched->ops->run_job(sched_job);
1266complete_all(&entity->entity_idle);
1267drm_sched_fence_scheduled(s_fence, fence);
1268
1269 if (!IS_ERR_OR_NULL(fence)) {
1270 r = dma_fence_add_callback(fence,&sched_job->cb,
1271drm_sched_job_done_cb);
1272 if (r ==-ENOENT)
1273drm_sched_job_done(sched_job, fence->error);
1274 else if (r)
1275DRM_DEV_ERROR(sched->dev, "fence add callback failed (%d)\n", r); 1276
1277dma_fence_put(fence);
1278 } else {
1279 drm_sched_job_done(sched_job,IS_ERR(fence)?
1280PTR_ERR(fence): 0);
1281 }
1282
1283wake_up(&sched->job_scheduled);
1284drm_sched_run_job_queue(sched);At line 1269, the
run_jobwork item is interrupted. Timed-out jobs run, callfree_job, which performs the final put. Then therun_jobwork item resumes—and boom, UAF. Using the same reasoning, I think movingfree_jobto the timed-out work queue could also cause issues.Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
If
run_jobwork item took a reference to the job before adding it to the pending list and dropped it after it was done touching it in this function, then yes, that would be safe. This is an argument for moving reference counting into the base DRM scheduler class, it would maketypo: s/DRM scheduler class/DRM job class
That strongly sounds like re-inventing the scheduler fence.
Perhaps.
Quoted 5 lines
What if we completely drop the job object? Or merge it into the scheduler fence?
The fence has reference counting, proper state transitions and a well defined lifetime.
We would just need ->schedule and ->finished functions instead of ->run_job and ->free_job. Those callbacks would then still be called by the scheduler in work item context instead of the irq context of the
dma_fencecallbacks.Yes, definitely no IRQ contexts.
Quoted 3 lines
The job can then be a void* in the scheduler fence where drivers can put anything they want and also drivers control the lifetime of that. E.g. they can free it during ->schedule as well as during ->finished.
Quoted 8 lines
I think this is a reasonable idea, but it would require major surgery across the subsystem plus the 11 upstream drivers I’m counting that use DRM scheduler. This would be a huge coordinated effort.
So I see three options:
1. Rename
free_jobtoput_joband document usage. Rip outfree_guilty. Likely the easiest and least invasive.
I think I can live with that. It's work-effort wise the best we can do right now. However, that does need proper documentation.
Let me respin to my documentation series and upstream that soonish, than we can build on top of that.
P.
Quoted 84 lines
2. Move reference counting to the base DRM scheduler job object, provide a vfunc for the final job put, and document usage. Medium invasive.
3. Move job (driver) side tracking to the scheduler fence and let it control the lifetime. Very invasive.
I’ll support any option, but my personal bandwidth to dive into something like #3 just isn’t there (of course, I can help review scheduler changes and fix up Xe, etc.).
Matt
Quoted 71 lines
Christian.
Quoted 68 lines
Matt
Quoted 65 lines
ownership clear rather than relying on ordered work queues to keep everything safe.
Quoted 7 lines
Quoted 7 lines
[2] git format-patch -1
ea2f6a77d0c40Quoted 4 lines
Quoted 3 lines
The first question would be: what does amdgpu need the job for after
free_job()ran? What do you even need a job for still after there was a timeout?Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
No, we just need the job structure alive as long as the
timedout_job()callback is running.Yes, I agree.
As far as I can see that's how it's already implemented? No one can free that job while
timedout_job()is running indrm_sched_job_timedout().See above, free guility is still problematic.
Quoted 26 lines
Quoted 26 lines
Quoted 4 lines
Quoted 4 lines
And if you really still need its contents, can't you
memcpy()the job or something?Quoted 9 lines
Quoted 6 lines
Assuming that it really needs it and that that cannot easily be solved, I suppose the obvious answer for differing memory life times is refcounting. So amdgpu could just let
drm_scheddrop its reference infree_job(), and from then onward it's amdgpu's problem.I hope Matthew can educate us on how Xe does it.
We discussed this on XDC and it was Matthew who brought up that this can be solved by running timeout and free worker on the same single threaded wq.
No, see my explainations above. This is not my suggestion.
Quoted 9 lines
Quoted 6 lines
AFAIK Nouveau doesn't have that problem, because on timeout we just terminate the channel.
Would also be interesting to hear whether other driver folks have the problem of
free_job()being racy.I think that this is still a general problem with the drm scheduler and not driver specific at all.
Quoted 8 lines
Quoted 2 lines
Maybe the
free_guiltyis likely a scheduler problem but I'm not seeing an issue aside from that.I also can't see the bug. I fail to see how
drm_schedcan free a job that's currently in use intimedout_job(). If that can happen, Christian, Vitaly, please point us to where and how. Only then can we decide on how to fix it properly.Again see above.
Matt
Quoted 4 lines
P.
Quoted 43 lines · Philipp Stanner
Quoted 42 lines · Matthew Brost
Quoted 41 lines · Christian König
Quoted 5 lines · Matthew Brost
Quoted 4 lines · Matthew Brost
Quoted 3 lines · Philipp Stanner
Quoted 2 lines · Matthew Brost
Quoted 1 line · Christian König
On 12/1/25 10:04, Philipp Stanner wrote:
....
Quoted 34 lines
Quoted 34 lines
Quoted 33 lines
Quoted 11 lines
Quoted 11 lines
Quoted 8 lines
This entire fix idea seems to circle around the concept of relying yet again on the scheduler's internal behavior (i.e., when it schedules the call to
free_job()).I think we discussed that at XDC, but how I see it if drivers have strange job life time requirements where a job shall outlive drm_sched's
free_job()call, they must solve that with a proper synchronization mechanism.Well that is not correct as far as I can see.
Quoted 7 lines
Quoted 4 lines
The problem here is rather that the scheduler gives the job as parameter to the
timedout_job()callback, but doesn't guarantee that ->free_job() callback isn't called whiletimedout_job()runs.This should be prevented by removing the job in question from the pending list (see
drm_sched_job_timedout), but for some reason that doesn't seem to work correctly.Are you sure this is happening? It doesn’t seem possible, nor have I observed it.
It's impossible, isn't it?
static void
drm_sched_job_timedout(struct work_struct *work){ structdrm_gpu_scheduler*sched; structdrm_sched_job*job; enumdrm_gpu_sched_statstatus =DRM_GPU_SCHED_STAT_RESET; sched =container_of(work, struct drm_gpu_scheduler, work_tdr.work); /* Protects against concurrent deletion indrm_sched_get_finished_job*/spin_lock(&sched->job_list_lock); job =list_first_entry_or_null(&sched->pending_list, struct drm_sched_job, list); if (job) { /* * Remove the bad job so it cannot be freed by a concurrent * &struct drm_sched_backend_ops.free_job. It will be * reinserted after the scheduler's work items have been * cancelled, at which point it's safe. */list_del_init(&job->list);spin_unlock(&sched->job_list_lock); status =job->sched->ops->timedout_job(job);1. scheduler takes list lock
2. removes job from list
3. unlocks
4. callstimedout_jobcallbackHow can
free_job_work, throughdrm_sched_get_finished_job(), get and free the same job?
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
It can't.
But exactly that happens somehow. Don't ask me how, I have no idea.
*Philipp refuses to elaborate and asks Christian*
How are you so sure about that's what's happening? Anyways, assuming it is true:
[ 489.134585] ================================================================== [ 489.141949] BUG: KASAN: slab-use-after-free in amdgpu_device_gpu_recover+0x968/0x990 [amdgpu] [ 489.151339] Read of size 4 at addr ffff88a0d5f4214c by task kworker/u128:0/12 [ 489.158686] [ 489.160277] CPU: 11 UID: 0 PID: 12 Comm: kworker/u128:0 Tainted: G E 6.16.0-1289896.3.zuul.0ec208edc00d48a9bae1719675cb777f #1 PREEMPT(voluntary) [ 489.160285] Tainted: [E]=UNSIGNED_MODULE [ 489.160288] Hardware name: TYAN B8021G88V2HR-2T/S8021GM2NR-2T, BIOS V1.03.B10 04/01/2019 [ 489.160292] Workqueue: amdgpu-reset-dev drm_sched_job_timedout [gpu_sched] [ 489.160306] Call Trace: [ 489.160308] <TASK> [ 489.160311] dump_stack_lvl+0x64/0x80 [ 489.160321] print_report+0xce/0x630 [ 489.160328] ? _raw_spin_lock_irqsave+0x86/0xd0 [ 489.160333] ? __pfx__raw_spin_lock_irqsave+0x10/0x10 [ 489.160337] ? amdgpu_device_gpu_recover+0x968/0x990 [amdgpu] [ 489.161044] kasan_report+0xb8/0xf0 [ 489.161049] ? amdgpu_device_gpu_recover+0x968/0x990 [amdgpu] [ 489.161756] amdgpu_device_gpu_recover+0x968/0x990 [amdgpu] [ 489.162464] ? __pfx_amdgpu_device_gpu_recover+0x10/0x10 [amdgpu] [ 489.163170] ? amdgpu_coredump+0x1fd/0x4c0 [amdgpu] [ 489.163904] amdgpu_job_timedout+0x642/0x1400 [amdgpu] [ 489.164698] ? __pfx__raw_spin_lock+0x10/0x10 [ 489.164703] ? __pfx_amdgpu_job_timedout+0x10/0x10 [amdgpu] [ 489.165496] ? _raw_spin_lock+0x75/0xc0 [ 489.165499] ? __pfx__raw_spin_lock+0x10/0x10 [ 489.165503] drm_sched_job_timedout+0x1b0/0x4b0 [gpu_sched]
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
My educated guess is that the job somehow ends up on the pending list again.
then the obvious question would be: does amdgpu touch the
pending_listitself, or does it only ever modify it through proper scheduler APIs?
My educated guess is that drm_sched_stop() inserted the job back into the pending list, but I still have no idea how it is possible that free_job is running after the scheduler is stopped.
Quoted 86 lines
Quoted 86 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 9 lines
The
pending_listis probably the one place where we actually lock consistently and sanely.I think this needs to be debugged more intensively, Christian.
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
What actually looks like a problem is that in
drm_sched_job_timedout,free_jobcan be called. Look at [2]—if you’re usingfree_guilty(Xe isn’t, but [2] was Xe trying to do the same thing), this is actually unsafe. Thefree_guiltycode should likely be removed as that definitely can explode under the right conditions.I'm right now not even sure why
free_guiltyexists, but I don't see howI'm sure why
free_guiltyexists either. If the fence is signaled in timedout jobfree_jobwill get scheduled on anotherwork_item.Quoted 9 lines
it's illegal for
drm_schedto callfree_jobindrm_sched_job_timedout?
free_jobcan be called at any point in time, drivers must expect that. No lock is being held, andtimedout_jobalready ran. So what's the problem?For drivers with additional refcounting it would be even less of a problem.
No, the scheduler can still reference the job.
1265 fence =
sched->ops->run_job(sched_job);
1266complete_all(&entity->entity_idle);
1267drm_sched_fence_scheduled(s_fence, fence);
1268
1269 if (!IS_ERR_OR_NULL(fence)) {
1270 r = dma_fence_add_callback(fence,&sched_job->cb,
1271drm_sched_job_done_cb);
1272 if (r ==-ENOENT)
1273drm_sched_job_done(sched_job, fence->error);
1274 else if (r)
1275DRM_DEV_ERROR(sched->dev, "fence add callback failed (%d)\n", r); 1276
1277dma_fence_put(fence);
1278 } else {
1279 drm_sched_job_done(sched_job,IS_ERR(fence)?
1280PTR_ERR(fence): 0);
1281 }
1282
1283wake_up(&sched->job_scheduled);
1284drm_sched_run_job_queue(sched);At line 1269, the
run_jobwork item is interrupted. Timed-out jobs run, callfree_job, which performs the final put. Then therun_jobwork item resumes—and boom, UAF. Using the same reasoning, I think movingfree_jobto the timed-out work queue could also cause issues.Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
If
run_jobwork item took a reference to the job before adding it to the pending list and dropped it after it was done touching it in this function, then yes, that would be safe. This is an argument for moving reference counting into the base DRM scheduler class, it would maketypo: s/DRM scheduler class/DRM job class
That strongly sounds like re-inventing the scheduler fence.
Perhaps.
Quoted 5 lines
What if we completely drop the job object? Or merge it into the scheduler fence?
The fence has reference counting, proper state transitions and a well defined lifetime.
We would just need ->schedule and ->finished functions instead of ->run_job and ->free_job. Those callbacks would then still be called by the scheduler in work item context instead of the irq context of the
dma_fencecallbacks.Yes, definitely no IRQ contexts.
Quoted 3 lines
The job can then be a void* in the scheduler fence where drivers can put anything they want and also drivers control the lifetime of that. E.g. they can free it during ->schedule as well as during ->finished.
I think this is a reasonable idea, but it would require major surgery across the subsystem plus the 11 upstream drivers I’m counting that use DRM scheduler. This would be a huge coordinated effort.
Quoted 7 lines
Quoted 4 lines
So I see three options:
1. Rename
free_jobtoput_joband document usage. Rip outfree_guilty. Likely the easiest and least invasive.I think I can live with that. It's work-effort wise the best we can do right now. However, that does need proper documentation.
I think that is the worst of all options.
It basically says to the driver that the job lifetime problems created by the scheduler is the driver problem and need to be worked around there.
Quoted 10 lines
Let me respin to my documentation series and upstream that soonish, than we can build on top of that.
P.
Quoted 3 lines
2. Move reference counting to the base DRM scheduler job object, provide a vfunc for the final job put, and document usage. Medium invasive.
I strongly think that reference counting the job object just because the scheduler needs it is a bad idea.
With that we are just moving the hot potato from one side of our mouth to the other without really solving the issue.
If a driver like XE needs that for some reason than that is perfectly fine.
Quoted 2 lines
Quoted 2 lines
3. Move job (driver) side tracking to the scheduler fence and let it control the lifetime. Very invasive.
Thinking about it more that is actually not so much of a problem.
Let me try to code something together by the end of next week or so.
Regards,
Christian.
Quoted 79 lines
Quoted 78 lines
I’ll support any option, but my personal bandwidth to dive into something like #3 just isn’t there (of course, I can help review scheduler changes and fix up Xe, etc.).
Matt
Quoted 71 lines
Christian.
Quoted 68 lines
Matt
Quoted 65 lines
ownership clear rather than relying on ordered work queues to keep everything safe.
Quoted 7 lines
Quoted 7 lines
[2] git format-patch -1
ea2f6a77d0c40Quoted 4 lines
Quoted 3 lines
The first question would be: what does amdgpu need the job for after
free_job()ran? What do you even need a job for still after there was a timeout?Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
No, we just need the job structure alive as long as the
timedout_job()callback is running.Yes, I agree.
As far as I can see that's how it's already implemented? No one can free that job while
timedout_job()is running indrm_sched_job_timedout().See above, free guility is still problematic.
Quoted 26 lines
Quoted 26 lines
Quoted 4 lines
Quoted 4 lines
And if you really still need its contents, can't you
memcpy()the job or something?Quoted 9 lines
Quoted 6 lines
Assuming that it really needs it and that that cannot easily be solved, I suppose the obvious answer for differing memory life times is refcounting. So amdgpu could just let
drm_scheddrop its reference infree_job(), and from then onward it's amdgpu's problem.I hope Matthew can educate us on how Xe does it.
We discussed this on XDC and it was Matthew who brought up that this can be solved by running timeout and free worker on the same single threaded wq.
No, see my explainations above. This is not my suggestion.
Quoted 9 lines
Quoted 6 lines
AFAIK Nouveau doesn't have that problem, because on timeout we just terminate the channel.
Would also be interesting to hear whether other driver folks have the problem of
free_job()being racy.I think that this is still a general problem with the drm scheduler and not driver specific at all.
Quoted 8 lines
Quoted 2 lines
Maybe the
free_guiltyis likely a scheduler problem but I'm not seeing an issue aside from that.I also can't see the bug. I fail to see how
drm_schedcan free a job that's currently in use intimedout_job(). If that can happen, Christian, Vitaly, please point us to where and how. Only then can we decide on how to fix it properly.Again see above.
Matt
Quoted 4 lines
P.
Quoted 71 lines · Christian König
Quoted 43 lines · Philipp Stanner
Quoted 42 lines · Matthew Brost
Quoted 41 lines · Christian König
Quoted 5 lines · Matthew Brost
Quoted 4 lines · Matthew Brost
Quoted 3 lines · Philipp Stanner
Quoted 2 lines · Matthew Brost
Quoted 1 line · Christian König
On 12/1/25 10:04, Philipp Stanner wrote:
....
Quoted 34 lines
Quoted 34 lines
Quoted 33 lines
Quoted 11 lines
Quoted 11 lines
Quoted 8 lines
This entire fix idea seems to circle around the concept of relying yet again on the scheduler's internal behavior (i.e., when it schedules the call to
free_job()).I think we discussed that at XDC, but how I see it if drivers have strange job life time requirements where a job shall outlive drm_sched's
free_job()call, they must solve that with a proper synchronization mechanism.Well that is not correct as far as I can see.
Quoted 7 lines
Quoted 4 lines
The problem here is rather that the scheduler gives the job as parameter to the
timedout_job()callback, but doesn't guarantee that ->free_job() callback isn't called whiletimedout_job()runs.This should be prevented by removing the job in question from the pending list (see
drm_sched_job_timedout), but for some reason that doesn't seem to work correctly.Are you sure this is happening? It doesn’t seem possible, nor have I observed it.
It's impossible, isn't it?
static void
drm_sched_job_timedout(struct work_struct *work){ structdrm_gpu_scheduler*sched; structdrm_sched_job*job; enumdrm_gpu_sched_statstatus =DRM_GPU_SCHED_STAT_RESET; sched =container_of(work, struct drm_gpu_scheduler, work_tdr.work); /* Protects against concurrent deletion indrm_sched_get_finished_job*/spin_lock(&sched->job_list_lock); job =list_first_entry_or_null(&sched->pending_list, struct drm_sched_job, list); if (job) { /* * Remove the bad job so it cannot be freed by a concurrent * &struct drm_sched_backend_ops.free_job. It will be * reinserted after the scheduler's work items have been * cancelled, at which point it's safe. */list_del_init(&job->list);spin_unlock(&sched->job_list_lock); status =job->sched->ops->timedout_job(job);1. scheduler takes list lock
2. removes job from list
3. unlocks
4. callstimedout_jobcallbackHow can
free_job_work, throughdrm_sched_get_finished_job(), get and free the same job?Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
It can't.
But exactly that happens somehow. Don't ask me how, I have no idea.
*Philipp refuses to elaborate and asks Christian*
How are you so sure about that's what's happening? Anyways, assuming it is true:
[ 489.134585] ==================================================================
[ 489.141949] BUG: KASAN: slab-use-after-free inamdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.151339] Read of size 4 at addrffff88a0d5f4214cby task kworker/u128:0/12
[ 489.158686]
[ 489.160277] CPU: 11 UID: 0 PID: 12 Comm: kworker/u128:0 Tainted: G E 6.16.0-1289896.3.zuul.0ec208edc00d48a9bae1719675cb777f #1 PREEMPT(voluntary) [ 489.160285] Tainted: [E]=UNSIGNED_MODULE
[ 489.160288] Hardware name: TYAN B8021G88V2HR-2T/S8021GM2NR-2T, BIOS V1.03.B10 04/01/2019
[ 489.160292] Workqueue: amdgpu-reset-devdrm_sched_job_timedout[gpu_sched]
[ 489.160306] Call Trace:
[ 489.160308] <TASK>
[ 489.160311]dump_stack_lvl+0x64/0x80
[ 489.160321]print_report+0xce/0x630
[ 489.160328] ?_raw_spin_lock_irqsave+0x86/0xd0
[ 489.160333] ?__pfx__raw_spin_lock_irqsave+0x10/0x10
[ 489.160337] ?amdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.161044]kasan_report+0xb8/0xf0
[ 489.161049] ?amdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.161756]amdgpu_device_gpu_recover+0x968/0x990[amdgpu]
Quoted 8 lines
[ 489.162464] ?
__pfx_amdgpu_device_gpu_recover+0x10/0x10[amdgpu] [ 489.163170] ?amdgpu_coredump+0x1fd/0x4c0[amdgpu]
[ 489.163904]amdgpu_job_timedout+0x642/0x1400[amdgpu] [ 489.164698] ?__pfx__raw_spin_lock+0x10/0x10
[ 489.164703] ?__pfx_amdgpu_job_timedout+0x10/0x10[amdgpu] [ 489.165496] ?_raw_spin_lock+0x75/0xc0
[ 489.165499] ?__pfx__raw_spin_lock+0x10/0x10
[ 489.165503]drm_sched_job_timedout+0x1b0/0x4b0[gpu_sched]
That doesn't show that it's free_job() who freed the memory.
@Vitaly: Can you reproduce the bug? If yes, adding debug prints printing the jobs' addresses when allocated and when freed in free_job() could be a solution.
I repeat, we need more info :)
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
My educated guess is that the job somehow ends up on the pending list again.
then the obvious question would be: does amdgpu touch the
pending_listitself, or does it only ever modify it through proper scheduler APIs?My educated guess is that
drm_sched_stop()inserted the job back into the pending list, but I still have no idea how it is possible thatfree_jobis running after the scheduler is stopped.
And my uneducated guess is that it's happening in amdgpu. It seems a sched_job lives inside an amdgpu_job. Can the latter be freed at other places than free_job()?
timedout_job() and free_job() cannot race against each other regarding jobs. It's locked.
But maybe investigate Matthew's suggestion and look into the guilty mechanism, too.
Quoted 91 lines
Quoted 91 lines
Quoted 90 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 9 lines
The
pending_listis probably the one place where we actually lock consistently and sanely.I think this needs to be debugged more intensively, Christian.
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
What actually looks like a problem is that in
drm_sched_job_timedout,free_jobcan be called. Look at [2]—if you’re usingfree_guilty(Xe isn’t, but [2] was Xe trying to do the same thing), this is actually unsafe. Thefree_guiltycode should likely be removed as that definitely can explode under the right conditions.I'm right now not even sure why
free_guiltyexists, but I don't see howI'm sure why
free_guiltyexists either. If the fence is signaled in timedout jobfree_jobwill get scheduled on anotherwork_item.Quoted 9 lines
it's illegal for
drm_schedto callfree_jobindrm_sched_job_timedout?
free_jobcan be called at any point in time, drivers must expect that. No lock is being held, andtimedout_jobalready ran. So what's the problem?For drivers with additional refcounting it would be even less of a problem.
No, the scheduler can still reference the job.
1265 fence =
sched->ops->run_job(sched_job);
1266complete_all(&entity->entity_idle);
1267drm_sched_fence_scheduled(s_fence, fence);
1268
1269 if (!IS_ERR_OR_NULL(fence)) {
1270 r = dma_fence_add_callback(fence,&sched_job->cb,
1271drm_sched_job_done_cb);
1272 if (r ==-ENOENT)
1273drm_sched_job_done(sched_job, fence->error);
1274 else if (r)
1275DRM_DEV_ERROR(sched->dev, "fence add callback failed (%d)\n", r); 1276
1277dma_fence_put(fence);
1278 } else {
1279 drm_sched_job_done(sched_job,IS_ERR(fence)?
1280PTR_ERR(fence): 0);
1281 }
1282
1283wake_up(&sched->job_scheduled);
1284drm_sched_run_job_queue(sched);At line 1269, the
run_jobwork item is interrupted. Timed-out jobs run, callfree_job, which performs the final put. Then therun_jobwork item resumes—and boom, UAF. Using the same reasoning, I think movingfree_jobto the timed-out work queue could also cause issues.Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
If
run_jobwork item took a reference to the job before adding it to the pending list and dropped it after it was done touching it in this function, then yes, that would be safe. This is an argument for moving reference counting into the base DRM scheduler class, it would maketypo: s/DRM scheduler class/DRM job class
That strongly sounds like re-inventing the scheduler fence.
Perhaps.
Quoted 5 lines
What if we completely drop the job object? Or merge it into the scheduler fence?
The fence has reference counting, proper state transitions and a well defined lifetime.
We would just need ->schedule and ->finished functions instead of ->run_job and ->free_job. Those callbacks would then still be called by the scheduler in work item context instead of the irq context of the
dma_fencecallbacks.Yes, definitely no IRQ contexts.
Quoted 3 lines
The job can then be a void* in the scheduler fence where drivers can put anything they want and also drivers control the lifetime of that. E.g. they can free it during ->schedule as well as during ->finished.
I think this is a reasonable idea, but it would require major surgery across the subsystem plus the 11 upstream drivers I’m counting that use DRM scheduler. This would be a huge coordinated effort.
So I see three options:
1. Rename
free_jobtoput_joband document usage. Rip outfree_guilty. Likely the easiest and least invasive.
Quoted 7 lines
Quoted 2 lines
I think I can live with that. It's work-effort wise the best we can do right now. However, that does need proper documentation.
I think that is the worst of all options.
It basically says to the driver that the job lifetime problems created by the scheduler is the driver problem and need to be worked around there.
My POV still mostly is that (with the current design) the driver must not use jobs after free_job() was invoked. And when that happens is unpredictable.
To be unfair, we already have strange refcount expectations already. But I sort of agree that there is no objectively good solution in sight.
Quoted 9 lines
Quoted 9 lines
Let me respin to my documentation series and upstream that soonish, than we can build on top of that.
P.
Quoted 1 line
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
2. Move reference counting to the base DRM scheduler job object, provide a vfunc for the final job put, and document usage. Medium invasive.
I strongly think that reference counting the job object just because the scheduler needs it is a bad idea.
With that we are just moving the hot potato from one side of our mouth to the other without really solving the issue.
If a driver like XE needs that for some reason than that is perfectly fine.
Nouveau doesn't need it either.
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
3. Move job (driver) side tracking to the scheduler fence and let it control the lifetime. Very invasive.
Thinking about it more that is actually not so much of a problem.
Let me try to code something together by the end of next week or so.
The hero Gotham needs :)
P.
On 12/9/25 14:51, Philipp Stanner wrote:
...
Quoted 40 lines
Quoted 31 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
How can
free_job_work, throughdrm_sched_get_finished_job(), get and free the same job?Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
It can't.
But exactly that happens somehow. Don't ask me how, I have no idea.
*Philipp refuses to elaborate and asks Christian*
How are you so sure about that's what's happening? Anyways, assuming it is true:
[ 489.134585] ==================================================================
[ 489.141949] BUG: KASAN: slab-use-after-free inamdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.151339] Read of size 4 at addrffff88a0d5f4214cby task kworker/u128:0/12
[ 489.158686]
[ 489.160277] CPU: 11 UID: 0 PID: 12 Comm: kworker/u128:0 Tainted: G E 6.16.0-1289896.3.zuul.0ec208edc00d48a9bae1719675cb777f #1 PREEMPT(voluntary) [ 489.160285] Tainted: [E]=UNSIGNED_MODULE
[ 489.160288] Hardware name: TYAN B8021G88V2HR-2T/S8021GM2NR-2T, BIOS V1.03.B10 04/01/2019
[ 489.160292] Workqueue: amdgpu-reset-devdrm_sched_job_timedout[gpu_sched]
[ 489.160306] Call Trace:
[ 489.160308] <TASK>
[ 489.160311]dump_stack_lvl+0x64/0x80
[ 489.160321]print_report+0xce/0x630
[ 489.160328] ?_raw_spin_lock_irqsave+0x86/0xd0
[ 489.160333] ?__pfx__raw_spin_lock_irqsave+0x10/0x10
[ 489.160337] ?amdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.161044]kasan_report+0xb8/0xf0
[ 489.161049] ?amdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.161756]amdgpu_device_gpu_recover+0x968/0x990[amdgpu]Quoted 8 lines
[ 489.162464] ?
__pfx_amdgpu_device_gpu_recover+0x10/0x10[amdgpu] [ 489.163170] ?amdgpu_coredump+0x1fd/0x4c0[amdgpu]
[ 489.163904]amdgpu_job_timedout+0x642/0x1400[amdgpu] [ 489.164698] ?__pfx__raw_spin_lock+0x10/0x10
[ 489.164703] ?__pfx_amdgpu_job_timedout+0x10/0x10[amdgpu] [ 489.165496] ?_raw_spin_lock+0x75/0xc0
[ 489.165499] ?__pfx__raw_spin_lock+0x10/0x10
[ 489.165503]drm_sched_job_timedout+0x1b0/0x4b0[gpu_sched]
Quoted 1 line
That doesn't show that it's
free_job()who freed the memory.
[ 489.405936] Freed by task 2501: [ 489.409175] kasan_save_stack+0x20/0x40 [ 489.413122] kasan_save_track+0x14/0x30 [ 489.417064] kasan_save_free_info+0x3b/0x60 [ 489.421355] __kasan_slab_free+0x37/0x50 [ 489.425384] kfree+0x1fe/0x3f0 [ 489.428547] drm_sched_free_job_work+0x50e/0x930 [gpu_sched] [ 489.434326] process_one_work+0x679/0xff0
Quoted 3 lines
@Vitaly: Can you reproduce the bug? If yes, adding debug prints printing the jobs' addresses when allocated and when freed in
free_job()could be a solution.
We can reproduce this pretty reliable in our CI now.
Quoted 10 lines
I repeat, we need more info :)
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
My educated guess is that the job somehow ends up on the pending list again.
then the obvious question would be: does amdgpu touch the
pending_listitself, or does it only ever modify it through proper scheduler APIs?
Quoted 6 lines
Quoted 2 lines
My educated guess is that
drm_sched_stop()inserted the job back into the pending list, but I still have no idea how it is possible thatfree_jobis running after the scheduler is stopped.And my uneducated guess is that it's happening in amdgpu. It seems a
sched_joblives inside anamdgpu_job. Can the latter be freed at other places thanfree_job()?
Nope, except for error handling during creation and initialization.
Quoted 5 lines
timedout_job()andfree_job()cannot race against each other regarding jobs. It's locked.But maybe investigate Matthew's suggestion and look into the guilty mechanism, too.
That looks just like a leftover from earlier attempts to fix the same problem.
I mean look at the git history of how often that problem came up...
Regards,
Christian.
Quoted 86 lines
Quoted 38 lines
Quoted 38 lines
Quoted 37 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
At line 1269, the
run_jobwork item is interrupted. Timed-out jobs run, callfree_job, which performs the final put. Then therun_jobwork item resumes—and boom, UAF. Using the same reasoning, I think movingfree_jobto the timed-out work queue could also cause issues.Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
If
run_jobwork item took a reference to the job before adding it to the pending list and dropped it after it was done touching it in this function, then yes, that would be safe. This is an argument for moving reference counting into the base DRM scheduler class, it would maketypo: s/DRM scheduler class/DRM job class
That strongly sounds like re-inventing the scheduler fence.
Perhaps.
Quoted 5 lines
What if we completely drop the job object? Or merge it into the scheduler fence?
The fence has reference counting, proper state transitions and a well defined lifetime.
We would just need ->schedule and ->finished functions instead of ->run_job and ->free_job. Those callbacks would then still be called by the scheduler in work item context instead of the irq context of the
dma_fencecallbacks.Yes, definitely no IRQ contexts.
Quoted 3 lines
The job can then be a void* in the scheduler fence where drivers can put anything they want and also drivers control the lifetime of that. E.g. they can free it during ->schedule as well as during ->finished.
I think this is a reasonable idea, but it would require major surgery across the subsystem plus the 11 upstream drivers I’m counting that use DRM scheduler. This would be a huge coordinated effort.
So I see three options:
1. Rename
free_jobtoput_joband document usage. Rip outfree_guilty. Likely the easiest and least invasive.Quoted 7 lines
Quoted 2 lines
I think I can live with that. It's work-effort wise the best we can do right now. However, that does need proper documentation.
I think that is the worst of all options.
It basically says to the driver that the job lifetime problems created by the scheduler is the driver problem and need to be worked around there.
My POV still mostly is that (with the current design) the driver must not use jobs after
free_job()was invoked. And when that happens is unpredictable.To be unfair, we already have strange refcount expectations already. But I sort of agree that there is no objectively good solution in sight.
Quoted 9 lines
Quoted 9 lines
Let me respin to my documentation series and upstream that soonish, than we can build on top of that.
P.
Quoted 1 line
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
2. Move reference counting to the base DRM scheduler job object, provide a vfunc for the final job put, and document usage. Medium invasive.
I strongly think that reference counting the job object just because the scheduler needs it is a bad idea.
With that we are just moving the hot potato from one side of our mouth to the other without really solving the issue.
If a driver like XE needs that for some reason than that is perfectly fine.
Nouveau doesn't need it either.
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
3. Move job (driver) side tracking to the scheduler fence and let it control the lifetime. Very invasive.
Thinking about it more that is actually not so much of a problem.
Let me try to code something together by the end of next week or so.
The hero Gotham needs :)
P.
Quoted 44 lines · Christian König
On 12/9/25 14:51, Philipp Stanner wrote:
...Quoted 40 lines
Quoted 31 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
How can
free_job_work, throughdrm_sched_get_finished_job(), get and free the same job?Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
It can't.
But exactly that happens somehow. Don't ask me how, I have no idea.
*Philipp refuses to elaborate and asks Christian*
How are you so sure about that's what's happening? Anyways, assuming it is true:
[ 489.134585] ==================================================================
[ 489.141949] BUG: KASAN: slab-use-after-free inamdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.151339] Read of size 4 at addrffff88a0d5f4214cby task kworker/u128:0/12
[ 489.158686]
[ 489.160277] CPU: 11 UID: 0 PID: 12 Comm: kworker/u128:0 Tainted: G E 6.16.0-1289896.3.zuul.0ec208edc00d48a9bae1719675cb777f #1 PREEMPT(voluntary) [ 489.160285] Tainted: [E]=UNSIGNED_MODULE
[ 489.160288] Hardware name: TYAN B8021G88V2HR-2T/S8021GM2NR-2T, BIOS V1.03.B10 04/01/2019
[ 489.160292] Workqueue: amdgpu-reset-devdrm_sched_job_timedout[gpu_sched]
[ 489.160306] Call Trace:
[ 489.160308] <TASK>
[ 489.160311]dump_stack_lvl+0x64/0x80
[ 489.160321]print_report+0xce/0x630
[ 489.160328] ?_raw_spin_lock_irqsave+0x86/0xd0
[ 489.160333] ?__pfx__raw_spin_lock_irqsave+0x10/0x10
[ 489.160337] ?amdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.161044]kasan_report+0xb8/0xf0
[ 489.161049] ?amdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.161756]amdgpu_device_gpu_recover+0x968/0x990[amdgpu]Quoted 8 lines
[ 489.162464] ?
__pfx_amdgpu_device_gpu_recover+0x10/0x10[amdgpu] [ 489.163170] ?amdgpu_coredump+0x1fd/0x4c0[amdgpu]
[ 489.163904]amdgpu_job_timedout+0x642/0x1400[amdgpu] [ 489.164698] ?__pfx__raw_spin_lock+0x10/0x10
[ 489.164703] ?__pfx_amdgpu_job_timedout+0x10/0x10[amdgpu] [ 489.165496] ?_raw_spin_lock+0x75/0xc0
[ 489.165499] ?__pfx__raw_spin_lock+0x10/0x10
[ 489.165503]drm_sched_job_timedout+0x1b0/0x4b0[gpu_sched]Quoted 1 line
That doesn't show that it's
free_job()who freed the memory.
Quoted 8 lines
[ 489.405936] Freed by task 2501:
[ 489.409175]kasan_save_stack+0x20/0x40
[ 489.413122]kasan_save_track+0x14/0x30
[ 489.417064]kasan_save_free_info+0x3b/0x60
[ 489.421355]__kasan_slab_free+0x37/0x50
[ 489.425384]kfree+0x1fe/0x3f0
[ 489.428547]drm_sched_free_job_work+0x50e/0x930[gpu_sched] [ 489.434326]process_one_work+0x679/0xff0
The time stamp shows that this free here took place after the UAF occurred :D
Quoted 29 lines
Quoted 3 lines
@Vitaly: Can you reproduce the bug? If yes, adding debug prints printing the jobs' addresses when allocated and when freed in
free_job()could be a solution.We can reproduce this pretty reliable in our CI now.
Quoted 10 lines
I repeat, we need more info :)
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
My educated guess is that the job somehow ends up on the pending list again.
then the obvious question would be: does amdgpu touch the
pending_listitself, or does it only ever modify it through proper scheduler APIs?Quoted 6 lines
Quoted 2 lines
My educated guess is that
drm_sched_stop()inserted the job back into the pending list, but I still have no idea how it is possible thatfree_jobis running after the scheduler is stopped.And my uneducated guess is that it's happening in amdgpu. It seems a
sched_joblives inside anamdgpu_job. Can the latter be freed at other places thanfree_job()?Nope, except for error handling during creation and initialization.
Quoted 3 lines
timedout_job()andfree_job()cannot race against each other regarding jobs. It's locked.
Quoted 6 lines
Quoted 2 lines
But maybe investigate Matthew's suggestion and look into the guilty mechanism, too.
That looks just like a leftover from earlier attempts to fix the same problem.
I mean look at the git history of how often that problem came up...
If that's the case, then we don't want to yet add another solution to a problem we don't fully understand and which, apparently, only occurs in amdgpu today.
What we need is an analysis of what's happening. Only then can we decide what to do.
Just switching the workqueues without such good justification receives a NACK from me; also because of the unforseeable consequences – free_job() is invoked extremely frequently, timedout_job() very rarely. Drivers will not expect that their timeout_wq will be flooded with so many work items. That could very certainly change behavior, cause performance regressions and so on.
P.
Quoted 54 lines · Philipp Stanner
Quoted 44 lines · Christian König
On 12/9/25 14:51, Philipp Stanner wrote:
...Quoted 40 lines
Quoted 31 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
How can
free_job_work, throughdrm_sched_get_finished_job(), get and free the same job?Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
It can't.
But exactly that happens somehow. Don't ask me how, I have no idea.
*Philipp refuses to elaborate and asks Christian*
How are you so sure about that's what's happening? Anyways, assuming it is true:
[ 489.134585] ==================================================================
[ 489.141949] BUG: KASAN: slab-use-after-free inamdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.151339] Read of size 4 at addrffff88a0d5f4214cby task kworker/u128:0/12
[ 489.158686]
[ 489.160277] CPU: 11 UID: 0 PID: 12 Comm: kworker/u128:0 Tainted: G E 6.16.0-1289896.3.zuul.0ec208edc00d48a9bae1719675cb777f #1 PREEMPT(voluntary) [ 489.160285] Tainted: [E]=UNSIGNED_MODULE
[ 489.160288] Hardware name: TYAN B8021G88V2HR-2T/S8021GM2NR-2T, BIOS V1.03.B10 04/01/2019
[ 489.160292] Workqueue: amdgpu-reset-devdrm_sched_job_timedout[gpu_sched]
[ 489.160306] Call Trace:
[ 489.160308] <TASK>
[ 489.160311]dump_stack_lvl+0x64/0x80
[ 489.160321]print_report+0xce/0x630
[ 489.160328] ?_raw_spin_lock_irqsave+0x86/0xd0
[ 489.160333] ?__pfx__raw_spin_lock_irqsave+0x10/0x10
[ 489.160337] ?amdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.161044]kasan_report+0xb8/0xf0
[ 489.161049] ?amdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.161756]amdgpu_device_gpu_recover+0x968/0x990[amdgpu]Quoted 8 lines
[ 489.162464] ?
__pfx_amdgpu_device_gpu_recover+0x10/0x10[amdgpu] [ 489.163170] ?amdgpu_coredump+0x1fd/0x4c0[amdgpu]
[ 489.163904]amdgpu_job_timedout+0x642/0x1400[amdgpu] [ 489.164698] ?__pfx__raw_spin_lock+0x10/0x10
[ 489.164703] ?__pfx_amdgpu_job_timedout+0x10/0x10[amdgpu] [ 489.165496] ?_raw_spin_lock+0x75/0xc0
[ 489.165499] ?__pfx__raw_spin_lock+0x10/0x10
[ 489.165503]drm_sched_job_timedout+0x1b0/0x4b0[gpu_sched]Quoted 1 line
That doesn't show that it's
free_job()who freed the memory.Quoted 8 lines
[ 489.405936] Freed by task 2501:
[ 489.409175]kasan_save_stack+0x20/0x40
[ 489.413122]kasan_save_track+0x14/0x30
[ 489.417064]kasan_save_free_info+0x3b/0x60
[ 489.421355]__kasan_slab_free+0x37/0x50
[ 489.425384]kfree+0x1fe/0x3f0
[ 489.428547]drm_sched_free_job_work+0x50e/0x930[gpu_sched] [ 489.434326]process_one_work+0x679/0xff0
Quoted 2 lines
The time stamp shows that this free here took place after the UAF occurred :D
No, that is just the way KASAN prints it.
E.g. KASAN detects that something is wrong, starts printing the current backtrace and then the backtrace of when the memory was freed.
Quoted 43 lines
Quoted 29 lines
Quoted 3 lines
@Vitaly: Can you reproduce the bug? If yes, adding debug prints printing the jobs' addresses when allocated and when freed in
free_job()could be a solution.We can reproduce this pretty reliable in our CI now.
Quoted 10 lines
I repeat, we need more info :)
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
My educated guess is that the job somehow ends up on the pending list again.
then the obvious question would be: does amdgpu touch the
pending_listitself, or does it only ever modify it through proper scheduler APIs?Quoted 6 lines
Quoted 2 lines
My educated guess is that
drm_sched_stop()inserted the job back into the pending list, but I still have no idea how it is possible thatfree_jobis running after the scheduler is stopped.And my uneducated guess is that it's happening in amdgpu. It seems a
sched_joblives inside anamdgpu_job. Can the latter be freed at other places thanfree_job()?Nope, except for error handling during creation and initialization.
Quoted 3 lines
timedout_job()andfree_job()cannot race against each other regarding jobs. It's locked.Quoted 6 lines
Quoted 2 lines
But maybe investigate Matthew's suggestion and look into the guilty mechanism, too.
That looks just like a leftover from earlier attempts to fix the same problem.
I mean look at the git history of how often that problem came up...
If that's the case, then we don't want to yet add another solution to a problem we don't fully understand and which, apparently, only occurs in amdgpu today.
What we need is an analysis of what's happening. Only then can we decide what to do.
Quoted 6 lines
Just switching the workqueues without such good justification receives a NACK from me; also because of the unforseeable consequences –
free_job()is invoked extremely frequently,timedout_job()very rarely. Drivers will not expect that theirtimeout_wqwill be flooded with so many work items. That could very certainly change behavior, cause performance regressions and so on.
Yeah, I was fearing that this could be problematic.
Regards,
Christian.
Quoted 6 lines
P.
Matthew Brost
maintainerQuoted 55 lines · Christian König
Quoted 54 lines · Philipp Stanner
Quoted 52 lines · Christian König
On 12/9/25 14:51, Philipp Stanner wrote:
...Quoted 40 lines
Quoted 31 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
How can
free_job_work, throughdrm_sched_get_finished_job(), get and free the same job?Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
It can't.
But exactly that happens somehow. Don't ask me how, I have no idea.
*Philipp refuses to elaborate and asks Christian*
How are you so sure about that's what's happening? Anyways, assuming it is true:
[ 489.134585] ==================================================================
[ 489.141949] BUG: KASAN: slab-use-after-free inamdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.151339] Read of size 4 at addrffff88a0d5f4214cby task kworker/u128:0/12
[ 489.158686]
[ 489.160277] CPU: 11 UID: 0 PID: 12 Comm: kworker/u128:0 Tainted: G E 6.16.0-1289896.3.zuul.0ec208edc00d48a9bae1719675cb777f #1 PREEMPT(voluntary) [ 489.160285] Tainted: [E]=UNSIGNED_MODULE
[ 489.160288] Hardware name: TYAN B8021G88V2HR-2T/S8021GM2NR-2T, BIOS V1.03.B10 04/01/2019
[ 489.160292] Workqueue: amdgpu-reset-devdrm_sched_job_timedout[gpu_sched]
[ 489.160306] Call Trace:
[ 489.160308] <TASK>
[ 489.160311]dump_stack_lvl+0x64/0x80
[ 489.160321]print_report+0xce/0x630
[ 489.160328] ?_raw_spin_lock_irqsave+0x86/0xd0
[ 489.160333] ?__pfx__raw_spin_lock_irqsave+0x10/0x10
[ 489.160337] ?amdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.161044]kasan_report+0xb8/0xf0
[ 489.161049] ?amdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.161756]amdgpu_device_gpu_recover+0x968/0x990[amdgpu]Quoted 8 lines
[ 489.162464] ?
__pfx_amdgpu_device_gpu_recover+0x10/0x10[amdgpu] [ 489.163170] ?amdgpu_coredump+0x1fd/0x4c0[amdgpu]
[ 489.163904]amdgpu_job_timedout+0x642/0x1400[amdgpu] [ 489.164698] ?__pfx__raw_spin_lock+0x10/0x10
[ 489.164703] ?__pfx_amdgpu_job_timedout+0x10/0x10[amdgpu] [ 489.165496] ?_raw_spin_lock+0x75/0xc0
[ 489.165499] ?__pfx__raw_spin_lock+0x10/0x10
[ 489.165503]drm_sched_job_timedout+0x1b0/0x4b0[gpu_sched]Quoted 1 line
That doesn't show that it's
free_job()who freed the memory.[ 489.405936] Freed by task 2501:
[ 489.409175]kasan_save_stack+0x20/0x40
[ 489.413122]kasan_save_track+0x14/0x30
[ 489.417064]kasan_save_free_info+0x3b/0x60
[ 489.421355]__kasan_slab_free+0x37/0x50
[ 489.425384]kfree+0x1fe/0x3f0
[ 489.428547]drm_sched_free_job_work+0x50e/0x930[gpu_sched] [ 489.434326]process_one_work+0x679/0xff0
Quoted 56 lines
Quoted 2 lines
The time stamp shows that this free here took place after the UAF occurred :D
No, that is just the way KASAN prints it.
E.g. KASAN detects that something is wrong, starts printing the current backtrace and then the backtrace of when the memory was freed.
Quoted 49 lines
Quoted 29 lines
Quoted 3 lines
@Vitaly: Can you reproduce the bug? If yes, adding debug prints printing the jobs' addresses when allocated and when freed in
free_job()could be a solution.We can reproduce this pretty reliable in our CI now.
Quoted 10 lines
I repeat, we need more info :)
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
My educated guess is that the job somehow ends up on the pending list again.
then the obvious question would be: does amdgpu touch the
pending_listitself, or does it only ever modify it through proper scheduler APIs?Quoted 6 lines
Quoted 2 lines
My educated guess is that
drm_sched_stop()inserted the job back into the pending list, but I still have no idea how it is possible thatfree_jobis running after the scheduler is stopped.And my uneducated guess is that it's happening in amdgpu. It seems a
sched_joblives inside anamdgpu_job. Can the latter be freed at other places thanfree_job()?Nope, except for error handling during creation and initialization.
Quoted 3 lines
timedout_job()andfree_job()cannot race against each other regarding jobs. It's locked.Quoted 6 lines
Quoted 2 lines
But maybe investigate Matthew's suggestion and look into the guilty mechanism, too.
That looks just like a leftover from earlier attempts to fix the same problem.
I mean look at the git history of how often that problem came up...
If that's the case, then we don't want to yet add another solution to a problem we don't fully understand and which, apparently, only occurs in amdgpu today.
What we need is an analysis of what's happening. Only then can we decide what to do.
Just switching the workqueues without such good justification receives a NACK from me; also because of the unforseeable consequences –
free_job()is invoked extremely frequently,timedout_job()very rarely. Drivers will not expect that theirtimeout_wqwill be flooded with so many work items. That could very certainly change behavior, cause performance regressions and so on.
Not mention this will 100% explode the work item which calls run_job under the right thread interrupt conditions that I detailed in previous reply why free guilty doesn't is dangerous. Free job can only safely be executed after the work work item which calls run_job exits.
Matt
Quoted 13 lines
Yeah, I was fearing that this could be problematic.
Regards,
Christian.Quoted 6 lines
P.
Matthew Brost
maintainerQuoted 54 lines · Philipp Stanner
Quoted 44 lines · Christian König
On 12/9/25 14:51, Philipp Stanner wrote:
...Quoted 40 lines
Quoted 31 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
How can
free_job_work, throughdrm_sched_get_finished_job(), get and free the same job?Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
It can't.
But exactly that happens somehow. Don't ask me how, I have no idea.
*Philipp refuses to elaborate and asks Christian*
How are you so sure about that's what's happening? Anyways, assuming it is true:
[ 489.134585] ==================================================================
[ 489.141949] BUG: KASAN: slab-use-after-free inamdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.151339] Read of size 4 at addrffff88a0d5f4214cby task kworker/u128:0/12
[ 489.158686]
[ 489.160277] CPU: 11 UID: 0 PID: 12 Comm: kworker/u128:0 Tainted: G E 6.16.0-1289896.3.zuul.0ec208edc00d48a9bae1719675cb777f #1 PREEMPT(voluntary) [ 489.160285] Tainted: [E]=UNSIGNED_MODULE
[ 489.160288] Hardware name: TYAN B8021G88V2HR-2T/S8021GM2NR-2T, BIOS V1.03.B10 04/01/2019
[ 489.160292] Workqueue: amdgpu-reset-devdrm_sched_job_timedout[gpu_sched]
[ 489.160306] Call Trace:
[ 489.160308] <TASK>
[ 489.160311]dump_stack_lvl+0x64/0x80
[ 489.160321]print_report+0xce/0x630
[ 489.160328] ?_raw_spin_lock_irqsave+0x86/0xd0
[ 489.160333] ?__pfx__raw_spin_lock_irqsave+0x10/0x10
[ 489.160337] ?amdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.161044]kasan_report+0xb8/0xf0
[ 489.161049] ?amdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.161756]amdgpu_device_gpu_recover+0x968/0x990[amdgpu]Quoted 8 lines
[ 489.162464] ?
__pfx_amdgpu_device_gpu_recover+0x10/0x10[amdgpu] [ 489.163170] ?amdgpu_coredump+0x1fd/0x4c0[amdgpu]
[ 489.163904]amdgpu_job_timedout+0x642/0x1400[amdgpu] [ 489.164698] ?__pfx__raw_spin_lock+0x10/0x10
[ 489.164703] ?__pfx_amdgpu_job_timedout+0x10/0x10[amdgpu] [ 489.165496] ?_raw_spin_lock+0x75/0xc0
[ 489.165499] ?__pfx__raw_spin_lock+0x10/0x10
[ 489.165503]drm_sched_job_timedout+0x1b0/0x4b0[gpu_sched]Quoted 1 line
That doesn't show that it's
free_job()who freed the memory.Quoted 8 lines
[ 489.405936] Freed by task 2501:
[ 489.409175]kasan_save_stack+0x20/0x40
[ 489.413122]kasan_save_track+0x14/0x30
[ 489.417064]kasan_save_free_info+0x3b/0x60
[ 489.421355]__kasan_slab_free+0x37/0x50
[ 489.425384]kfree+0x1fe/0x3f0
[ 489.428547]drm_sched_free_job_work+0x50e/0x930[gpu_sched] [ 489.434326]process_one_work+0x679/0xff0
Quoted 22 lines
The time stamp shows that this free here took place after the UAF occurred :D
Quoted 18 lines
Quoted 3 lines
@Vitaly: Can you reproduce the bug? If yes, adding debug prints printing the jobs' addresses when allocated and when freed in
free_job()could be a solution.We can reproduce this pretty reliable in our CI now.
Quoted 11 lines
I repeat, we need more info :)
Quoted 9 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
My educated guess is that the job somehow ends up on the pending list again.
then the obvious question would be: does amdgpu touch the
pending_listitself, or does it only ever modify it through proper scheduler APIs?My educated guess is that
drm_sched_stop()inserted the job back into the pending list, but I still have no idea how it is possible thatfree_jobis running after the scheduler is stopped.
I believe I found your problem, referencing amdgpu/amdgpu_device.c here.
6718 if (job)
6719 ti = amdgpu_vm_get_task_info_pasid(adev, job->pasid);
Which is after:
6695 skip_hw_reset:
6696 r = amdgpu_device_sched_resume(&device_list, reset_context, job_signaled);
6697 if (r)
6698 goto reset_unlock;
The job is likely added back into this free list here:
6676 amdgpu_device_halt_activities(adev, job, reset_context, &device_list,
6677 hive, need_emergency_restart);
So free_job runs and 'job->pasid' explodes.
Save off the pasid on the stack at top of this function and I suspect your UAF goes away. This won't untangle this hair ball of code but I believe this at least prevent explosions.
Matt
Quoted 37 lines
Quoted 11 lines
Quoted 5 lines
Quoted 1 line
And my uneducated guess is that it's happening in amdgpu. It seems a
sched_joblives inside anamdgpu_job. Can the latter be freed at other places thanfree_job()?Nope, except for error handling during creation and initialization.
Quoted 3 lines
timedout_job()andfree_job()cannot race against each other regarding jobs. It's locked.Quoted 6 lines
Quoted 2 lines
But maybe investigate Matthew's suggestion and look into the guilty mechanism, too.
That looks just like a leftover from earlier attempts to fix the same problem.
I mean look at the git history of how often that problem came up...
If that's the case, then we don't want to yet add another solution to a problem we don't fully understand and which, apparently, only occurs in amdgpu today.
What we need is an analysis of what's happening. Only then can we decide what to do.
Just switching the workqueues without such good justification receives a NACK from me; also because of the unforseeable consequences –
free_job()is invoked extremely frequently,timedout_job()very rarely. Drivers will not expect that theirtimeout_wqwill be flooded with so many work items. That could very certainly change behavior, cause performance regressions and so on.P.
Matthew Brost
maintainerOn Tue, Dec 09, 2025 at 03:19:40PM +0100, Christian König wrote:
Hey all, apologies for the stream of replies today. I looked into this a bit more after finishing up my day job and having several “WTF” moments. Trying to unify from this reply [1].
[1] https://patchwork.freedesktop.org/patch/691101/?series=158224&rev=1#comment_1273453
Quoted 53 lines · Philipp Stanner
...
Quoted 40 lines
Quoted 31 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
How can
free_job_work, throughdrm_sched_get_finished_job(), get and free the same job?Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
It can't.
But exactly that happens somehow. Don't ask me how, I have no idea.
*Philipp refuses to elaborate and asks Christian*
How are you so sure about that's what's happening? Anyways, assuming it is true:
[ 489.134585] ==================================================================
[ 489.141949] BUG: KASAN: slab-use-after-free inamdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.151339] Read of size 4 at addrffff88a0d5f4214cby task kworker/u128:0/12
[ 489.158686]
[ 489.160277] CPU: 11 UID: 0 PID: 12 Comm: kworker/u128:0 Tainted: G E 6.16.0-1289896.3.zuul.0ec208edc00d48a9bae1719675cb777f #1 PREEMPT(voluntary) [ 489.160285] Tainted: [E]=UNSIGNED_MODULE
[ 489.160288] Hardware name: TYAN B8021G88V2HR-2T/S8021GM2NR-2T, BIOS V1.03.B10 04/01/2019
[ 489.160292] Workqueue: amdgpu-reset-devdrm_sched_job_timedout[gpu_sched]
[ 489.160306] Call Trace:
[ 489.160308] <TASK>
[ 489.160311]dump_stack_lvl+0x64/0x80
[ 489.160321]print_report+0xce/0x630
[ 489.160328] ?_raw_spin_lock_irqsave+0x86/0xd0
[ 489.160333] ?__pfx__raw_spin_lock_irqsave+0x10/0x10
[ 489.160337] ?amdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.161044]kasan_report+0xb8/0xf0
[ 489.161049] ?amdgpu_device_gpu_recover+0x968/0x990[amdgpu]
[ 489.161756]amdgpu_device_gpu_recover+0x968/0x990[amdgpu]Quoted 8 lines
[ 489.162464] ?
__pfx_amdgpu_device_gpu_recover+0x10/0x10[amdgpu] [ 489.163170] ?amdgpu_coredump+0x1fd/0x4c0[amdgpu]
[ 489.163904]amdgpu_job_timedout+0x642/0x1400[amdgpu] [ 489.164698] ?__pfx__raw_spin_lock+0x10/0x10
[ 489.164703] ?__pfx_amdgpu_job_timedout+0x10/0x10[amdgpu] [ 489.165496] ?_raw_spin_lock+0x75/0xc0
[ 489.165499] ?__pfx__raw_spin_lock+0x10/0x10
[ 489.165503]drm_sched_job_timedout+0x1b0/0x4b0[gpu_sched]Quoted 1 line
That doesn't show that it's
free_job()who freed the memory.[ 489.405936] Freed by task 2501: [ 489.409175] kasan_save_stack+0x20/0x40 [ 489.413122] kasan_save_track+0x14/0x30 [ 489.417064] kasan_save_free_info+0x3b/0x60 [ 489.421355] __kasan_slab_free+0x37/0x50 [ 489.425384] kfree+0x1fe/0x3f0 [ 489.428547] drm_sched_free_job_work+0x50e/0x930 [gpu_sched] [ 489.434326] process_one_work+0x679/0xff0
Quoted 17 lines
Quoted 3 lines
@Vitaly: Can you reproduce the bug? If yes, adding debug prints printing the jobs' addresses when allocated and when freed in
free_job()could be a solution.We can reproduce this pretty reliable in our CI now.
Quoted 11 lines
I repeat, we need more info :)
Quoted 9 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
My educated guess is that the job somehow ends up on the pending list again.
then the obvious question would be: does amdgpu touch the
pending_listitself, or does it only ever modify it through proper scheduler APIs?My educated guess is that
drm_sched_stop()inserted the job back into the pending list, but I still have no idea how it is possible thatfree_jobis running after the scheduler is stopped.
I believe I found your problem, referencing amdgpu/amdgpu_device.c here.
6718 if (job)
6719 ti = amdgpu_vm_get_task_info_pasid(adev, job->pasid);
Which is after:
6695 skip_hw_reset:
6696 r = amdgpu_device_sched_resume(&device_list, reset_context, job_signaled);
6697 if (r)
6698 goto reset_unlock;
The job is likely added back into this free list here:
6676 amdgpu_device_halt_activities(adev, job, reset_context, &device_list,
6677 hive, need_emergency_restart);
So free_job runs and 'job->pasid' explodes.
Save off the pasid on the stack at top of this function and I suspect your UAF goes away. This won't untangle this hair ball of code but I believe this at least prevent explosions.
But let’s dig in further—amdgpu_device_halt_activities calls drm_sched_stop (Xe just calls drm_sched_wqueue_stop for reference). This function stops the work items, then adds the offending job back to the pending list, iterates over each job, removes the CB, leaving the job in the pending list. If the CB can be removed, it removes the job from pending, maybe calls free_job if it’s not a guilty job, and if it is a guilty job, defers the free_job to the timed-out job so it doesn’t disappear. Like WTF?
Oh, it gets better—amdgpu_device_sched_resume calls drm_sched_start, which iterates over the pending list and reinserts the same CB that drm_sched_stop removed, then starts the scheduler. So the guilty job had its CB successfully removed, and now it can immediately disappear—also like WTF?
Free_guilty is clearly a hack around the job not being reference counted, and it doesn’t even work in some cases. Putting that aside, I think calling free_job shouldn’t really ever happen in TDR. Imagine if drm_sched_job_timedout just took a reference to the job like normal kernel code—free_guilty could be dropped, and immediately this all becomes safe. Likewise, if the run_job work item had a reference to the job, which it takes before adding to the pending list and drops after it’s done touching it in this function, then run_job and free_job work items could safely execute in parallel rather than relying on an ordered workqueue to keep that part of the code safe.
Quoted 14 lines
Quoted 5 lines
Quoted 1 line
And my uneducated guess is that it's happening in amdgpu. It seems a
sched_joblives inside anamdgpu_job. Can the latter be freed at other places thanfree_job()?Nope, except for error handling during creation and initialization.
Quoted 5 lines
timedout_job()andfree_job()cannot race against each other regarding jobs. It's locked.But maybe investigate Matthew's suggestion and look into the guilty mechanism, too.
Quoted 57 lines
That looks just like a leftover from earlier attempts to fix the same problem.
I mean look at the git history of how often that problem came up...
Regards,
Christian.Quoted 50 lines
Quoted 38 lines
Quoted 38 lines
Quoted 37 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
At line 1269, the
run_jobwork item is interrupted. Timed-out jobs run, callfree_job, which performs the final put. Then therun_jobwork item resumes—and boom, UAF. Using the same reasoning, I think movingfree_jobto the timed-out work queue could also cause issues.Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
If
run_jobwork item took a reference to the job before adding it to the pending list and dropped it after it was done touching it in this function, then yes, that would be safe. This is an argument for moving reference counting into the base DRM scheduler class, it would maketypo: s/DRM scheduler class/DRM job class
That strongly sounds like re-inventing the scheduler fence.
Perhaps.
Quoted 5 lines
What if we completely drop the job object? Or merge it into the scheduler fence?
The fence has reference counting, proper state transitions and a well defined lifetime.
We would just need ->schedule and ->finished functions instead of ->run_job and ->free_job. Those callbacks would then still be called by the scheduler in work item context instead of the irq context of the
dma_fencecallbacks.Yes, definitely no IRQ contexts.
Quoted 3 lines
The job can then be a void* in the scheduler fence where drivers can put anything they want and also drivers control the lifetime of that. E.g. they can free it during ->schedule as well as during ->finished.
I think this is a reasonable idea, but it would require major surgery across the subsystem plus the 11 upstream drivers I’m counting that use DRM scheduler. This would be a huge coordinated effort.
So I see three options:
1. Rename
free_jobtoput_joband document usage. Rip outfree_guilty. Likely the easiest and least invasive.Quoted 7 lines
Quoted 2 lines
I think I can live with that. It's work-effort wise the best we can do right now. However, that does need proper documentation.
I think that is the worst of all options.
It basically says to the driver that the job lifetime problems created by the scheduler is the driver problem and need to be worked around there.
My POV still mostly is that (with the current design) the driver must not use jobs after
free_job()was invoked. And when that happens is unpredictable.
This is somewhat of an absurd statement from my point of view. I have a valid job pointer, then I call another function (see above for an example of how drm_sched_start/stop is unsafe) and it disappears behind my back. The safe way to handle this is to take a local reference before doing anything that could make it disappear. That is much more reasonable than saying, “we have five things you can do in the scheduler, and if you do any of them it isn’t safe to touch the job afterward.”
Quoted 13 lines
Quoted 13 lines
To be unfair, we already have strange refcount expectations already. But I sort of agree that there is no objectively good solution in sight.
Quoted 9 lines
Quoted 9 lines
Let me respin to my documentation series and upstream that soonish, than we can build on top of that.
P.
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
2. Move reference counting to the base DRM scheduler job object, provide a vfunc for the final job put, and document usage. Medium invasive.
I strongly think that reference counting the job object just because the scheduler needs it is a bad idea.
With that we are just moving the hot potato from one side of our mouth to the other without really solving the issue.
See above—I can’t say I agree with this assessment. I think the lack of reference counting is exactly the problem. I don’t really understand the pushback on a very well-understood concept (reference counting) in Linux. I would sign up to fix the entire subsystem if we go this route.
Quoted 8 lines
Quoted 8 lines
Quoted 1 line
If a driver like XE needs that for some reason than that is perfectly fine.
Nouveau doesn't need it either.
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
3. Move job (driver) side tracking to the scheduler fence and let it control the lifetime. Very invasive.
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Thinking about it more that is actually not so much of a problem.
Let me try to code something together by the end of next week or so.
The hero Gotham needs :)
Are you sure about this one? I think unless the problems around drm_sched_start/stop and free_guilty are fixed, my feeling is this entire thing is still badly broken for anyone who wants to use those.
To sum up this whole email: I strongly disagree with option #3, but if that is the consensus, I will, of course, support the effort.
Matt
Quoted 3 lines
Quoted 2 lines
P.
Quoted 10 lines · Matthew Brost
On Tue, Dec 09, 2025 at 03:19:40PM +0100, Christian König wrote:
Hey all, apologies for the stream of replies today. I looked into this a bit more after finishing up my day job and having several “WTF” moments. Trying to unify from this reply [1].
[1] https://patchwork.freedesktop.org/patch/691101/?series=158224&rev=1#comment_1273453
Quoted 2 lines · Philipp Stanner
[…]
Quoted 41 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
My educated guess is that
drm_sched_stop()inserted the job back into the pending list, but I still have no idea how it is possible thatfree_jobis running after the scheduler is stopped.I believe I found your problem, referencing
amdgpu/amdgpu_device.chere.6718 if (job)
6719 ti =amdgpu_vm_get_task_info_pasid(adev, job->pasid);Which is after:
6695
skip_hw_reset:
6696 r =amdgpu_device_sched_resume(&device_list, reset_context, job_signaled); 6697 if (r)
6698 gotoreset_unlock;The job is likely added back into this free list here:
6676 amdgpu_device_halt_activities(adev, job,
reset_context,&device_list, 6677 hive,need_emergency_restart);So
free_jobruns and 'job->pasid' explodes.Save off the pasid on the stack at top of this function and I suspect your UAF goes away. This won't untangle this hair ball of code but I believe this at least prevent explosions.
But let’s dig in further—amdgpu_device_halt_activities calls
drm_sched_stop(Xe just callsdrm_sched_wqueue_stopfor reference). This function stops the work items, then adds the offending job back to the pending list, iterates over each job, removes the CB, leaving the job in the pending list. If the CB can be removed, it removes the job from pending, maybe callsfree_jobif it’s not a guilty job, and if it is a guilty job, defers thefree_jobto the timed-out job so it doesn’t disappear. Like WTF?Oh, it gets better—amdgpu_device_sched_resume calls
drm_sched_start, which iterates over the pending list and reinserts the same CB thatdrm_sched_stopremoved, then starts the scheduler. So the guilty job had its CB successfully removed, and now it can immediately disappear—also like WTF?
Quoted 10 lines
Free_guiltyis clearly a hack around the job not being reference counted, and it doesn’t even work in some cases. Putting that aside, I think callingfree_jobshouldn’t really ever happen in TDR. Imagine ifdrm_sched_job_timedoutjust took a reference to the job like normal kernel code—free_guilty could be dropped, and immediately this all becomes safe. Likewise, if therun_jobwork item had a reference to the job, which it takes before adding to the pending list and drops after it’s done touching it in this function, thenrun_jobandfree_jobwork items could safely execute in parallel rather than relying on an ordered workqueue to keep that part of the code safe.
I can tell you how I design it in our Rust jobqueue:
Drivers create jobs, and in submit_job() the pass ownership over the job to the jobqueue – IOW after pushing a job, a driver can't access it anymore. In the run_job() callback, the jobqueue either passes the job back by value (ownership) or borrows the job to the driver so that it can be copied (this is done so that the JQ can hypothetically do resubmits).
This way there is no need for refcounting (in Rust / jobqueue).
Maybe the core of the problem is not so much the lack of refcounting, but the lack of ownership rules. Why even would the driver need the job still after it got pushed? It should be fire-and-forget.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines
And my uneducated guess is that it's happening in amdgpu. It seems a
sched_joblives inside anamdgpu_job. Can the latter be freed at other places thanfree_job()?Quoted 1 line
[…]
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
It basically says to the driver that the job lifetime problems created by the scheduler is the driver problem and need to be worked around there.
My POV still mostly is that (with the current design) the driver must not use jobs after
free_job()was invoked. And when that happens is unpredictable.
Quoted 5 lines
This is somewhat of an absurd statement from my point of view. I have a valid job pointer, then I call another function (see above for an example of how drm_sched_start/stop is unsafe) and it disappears behind my back.
The statement is absurd because reality (the code) is absurd. We all are basically Alice in Wonderland, running as fast as we can just to remain on the same spot ^_^
What I am stating is not that this is *good*, but this is what it currently is like. Whether we like it or not.
The misunderstanding you and I might have is that for me jobs having to be refcounted is not a reality until it's reflected in code, documentation and, ideally, drivers.
Quoted 5 lines
The safe way to handle this is to take a local reference before
doing anything that could make it disappear. That is much more reasonable than saying, “we have five things you can do in the scheduler, and if you do any of them it isn’t safe to touch the job afterward.”
Yeah, but that's drm_sched being drm_scheddy. Before I documented it there were also these implicit refcounting rules in run_job(), where the driver needs to take the reference for the scheduler for it to be non-racy.
It also wasn't documented for a long time that drm_sched (through spsc_queue) will explode if you don't use entities with a single producer thread.
drm_sched got here because of gross design mistakes, lots of hacks for few drivers, and, particularly, a strange aversion¹ against writing documentation. If Xe came, back in the day, to the conclusion that job lifetimes are fundamentally broken and that the objectively correct way to solve this is refcounting, then why wasn't that pushed into drm_sched back then?
Quoted 42 lines
Quoted 14 lines
Quoted 14 lines
To be unfair, we already have strange refcount expectations already. But I sort of agree that there is no objectively good solution in sight.
Quoted 9 lines
Quoted 9 lines
Let me respin to my documentation series and upstream that soonish, than we can build on top of that.
P.
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
2. Move reference counting to the base DRM scheduler job object, provide a vfunc for the final job put, and document usage. Medium invasive.
I strongly think that reference counting the job object just because the scheduler needs it is a bad idea.
With that we are just moving the hot potato from one side of our mouth to the other without really solving the issue.
See above—I can’t say I agree with this assessment. I think the lack of reference counting is exactly the problem. I don’t really understand the pushback on a very well-understood concept (reference counting) in Linux. I would sign up to fix the entire subsystem if we go this route.
Quoted 8 lines
Quoted 8 lines
Quoted 1 line
If a driver like XE needs that for some reason than that is perfectly fine.
Nouveau doesn't need it either.
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
3. Move job (driver) side tracking to the scheduler fence and let it control the lifetime. Very invasive.
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Thinking about it more that is actually not so much of a problem.
Let me try to code something together by the end of next week or so.
The hero Gotham needs :)
Quoted 6 lines
Are you sure about this one? I think unless the problems around drm_sched_start/stop and
free_guiltyare fixed, my feeling is this entire thing is still badly broken for anyone who wants to use those.To sum up this whole email: I strongly disagree with option #3, but if that is the consensus, I will, of course, support the effort.
I would like to discuss those topics with Danilo, too, who returns from LPC soonish. Also to get some more insights into Nouveau and our use- cases.
My suggestion is that we pick the conversation up again soonish. Christmas is around the corner, and I suppose we can't fix this all up in 2025 anyways, so we might want to give it a fresh re-spin in '26.
Greetings,
P.
[¹] The strangest aversion in drm_sched, however, is the one against locking. Locks were only taken when *absolutely* necessary. It's as if the entire code was designed by some gamers who want to show their youtube subscribers that they can get 1fps more by changing RAM timings in the bios.drm_sched might be the component in the kernel using the most synchronization mechanisms: Spinlocks, RCU, atomic integers, atomic instructions, refcounting, READ_ONCE(), just accessing locklessly… even Paul McKenney would get wide eyes here. The only thing we're still missing is seqlocks and rw_locks, but maybe we can add those /s
That's likely sth we can't get repaired at all anymore.
Quoted 2 lines · Philipp Stanner
Quoted 1 line · Matthew Brost
On Tue, Dec 09, 2025 at 03:19:40PM +0100, Christian König wrote:
..
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
My educated guess is that
drm_sched_stop()inserted the job back into the pending list, but I still have no idea how it is possible thatfree_jobis running after the scheduler is stopped.I believe I found your problem, referencing
amdgpu/amdgpu_device.chere.6718 if (job)
6719 ti =amdgpu_vm_get_task_info_pasid(adev, job->pasid);
WTF! There it is! Thanks a lot for pointing that out!
Quoted 8 lines
Quoted 8 lines
Which is after:
6695
skip_hw_reset:
6696 r =amdgpu_device_sched_resume(&device_list, reset_context, job_signaled); 6697 if (r)
6698 gotoreset_unlock;
Quoted 6 lines
Quoted 6 lines
The job is likely added back into this free list here:
6676 amdgpu_device_halt_activities(adev, job,
reset_context,&device_list, 6677 hive,need_emergency_restart);So
free_jobruns and 'job->pasid' explodes.
I've read over that code like a hundred times and didn't realized that the job is accessed after the scheduler resumes.
Quoted 39 lines
Quoted 20 lines
Save off the pasid on the stack at top of this function and I suspect your UAF goes away. This won't untangle this hair ball of code but I believe this at least prevent explosions.
But let’s dig in further—amdgpu_device_halt_activities calls
drm_sched_stop(Xe just callsdrm_sched_wqueue_stopfor reference). This function stops the work items, then adds the offending job back to the pending list, iterates over each job, removes the CB, leaving the job in the pending list. If the CB can be removed, it removes the job from pending, maybe callsfree_jobif it’s not a guilty job, and if it is a guilty job, defers thefree_jobto the timed-out job so it doesn’t disappear. Like WTF?Oh, it gets better—amdgpu_device_sched_resume calls
drm_sched_start, which iterates over the pending list and reinserts the same CB thatdrm_sched_stopremoved, then starts the scheduler. So the guilty job had its CB successfully removed, and now it can immediately disappear—also like WTF?Quoted 10 lines
Free_guiltyis clearly a hack around the job not being reference counted, and it doesn’t even work in some cases. Putting that aside, I think callingfree_jobshouldn’t really ever happen in TDR. Imagine ifdrm_sched_job_timedoutjust took a reference to the job like normal kernel code—free_guilty could be dropped, and immediately this all becomes safe. Likewise, if therun_jobwork item had a reference to the job, which it takes before adding to the pending list and drops after it’s done touching it in this function, thenrun_jobandfree_jobwork items could safely execute in parallel rather than relying on an ordered workqueue to keep that part of the code safe.I can tell you how I design it in our Rust jobqueue:
Drivers create jobs, and insubmit_job()the pass ownership over the job to the jobqueue – IOW after pushing a job, a driver can't access it anymore. In therun_job()callback, the jobqueue either passes the job back by value (ownership) or borrows the job to the driver so that it can be copied (this is done so that the JQ can hypothetically do resubmits).
Quoted 5 lines
This way there is no need for refcounting (in Rust / jobqueue).
Maybe the core of the problem is not so much the lack of refcounting, but the lack of ownership rules. Why even would the driver need the job still after it got pushed? It should be fire-and-forget.
Yeah, that sounds sane to me as well and is exactly how it was initially designed in the drm_scheduler as well.
The job is basically just the information the driver needs for the submission which it gives to the scheduler on push, and the scheduler gives back to the driver on pop.
The full execution time is represented by the scheduler fence and not the job. And the scheduler fence is reference counted exactly because of the reasons Mathew brought up here.
I'm absolutely not against reference counting, what I'm pushing back is abusing the job object as something it was never designed for while we already have an object which implements exactly the needed functionality.
Quoted 39 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
And my uneducated guess is that it's happening in amdgpu. It seems a
sched_joblives inside anamdgpu_job. Can the latter be freed at other places thanfree_job()?Quoted 1 line
[…]
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
It basically says to the driver that the job lifetime problems created by the scheduler is the driver problem and need to be worked around there.
My POV still mostly is that (with the current design) the driver must not use jobs after
free_job()was invoked. And when that happens is unpredictable.Quoted 5 lines
This is somewhat of an absurd statement from my point of view. I have a valid job pointer, then I call another function (see above for an example of how drm_sched_start/stop is unsafe) and it disappears behind my back.
The statement is absurd because reality (the code) is absurd. We all are basically Alice in Wonderland, running as fast as we can just to remain on the same spot ^_^
What I am stating is not that this is *good*, but this is what it currently is like. Whether we like it or not.
The misunderstanding you and I might have is that for me jobs having to be refcounted is not a reality until it's reflected in code, documentation and, ideally, drivers.
Quoted 5 lines
The safe way to handle this is to take a local reference before
doing anything that could make it disappear. That is much more reasonable than saying, “we have five things you can do in the scheduler, and if you do any of them it isn’t safe to touch the job afterward.”
Quoted 8 lines
Yeah, but that's
drm_schedbeingdrm_scheddy. Before I documented it there were also these implicit refcounting rules inrun_job(), where the driver needs to take the reference for the scheduler for it to be non-racy.It also wasn't documented for a long time that
drm_sched(throughspsc_queue) will explode if you don't use entities with a single producer thread.
That is actually documented, but not on the scheduler but rather the dma_fence.
And that you can only have a single producer is a requirement inherited from the dma_fence and not scheduler specific at all.
Quoted 57 lines
drm_schedgot here because of gross design mistakes, lots of hacks for few drivers, and, particularly, a strange aversion¹ against writing documentation. If Xe came, back in the day, to the conclusion that job lifetimes are fundamentally broken and that the objectively correct way to solve this is refcounting, then why wasn't that pushed intodrm_schedback then?Quoted 42 lines
Quoted 14 lines
Quoted 14 lines
To be unfair, we already have strange refcount expectations already. But I sort of agree that there is no objectively good solution in sight.
Quoted 9 lines
Quoted 9 lines
Let me respin to my documentation series and upstream that soonish, than we can build on top of that.
P.
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
2. Move reference counting to the base DRM scheduler job object, provide a vfunc for the final job put, and document usage. Medium invasive.
I strongly think that reference counting the job object just because the scheduler needs it is a bad idea.
With that we are just moving the hot potato from one side of our mouth to the other without really solving the issue.
See above—I can’t say I agree with this assessment. I think the lack of reference counting is exactly the problem. I don’t really understand the pushback on a very well-understood concept (reference counting) in Linux. I would sign up to fix the entire subsystem if we go this route.
Quoted 8 lines
Quoted 8 lines
Quoted 1 line
If a driver like XE needs that for some reason than that is perfectly fine.
Nouveau doesn't need it either.
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
3. Move job (driver) side tracking to the scheduler fence and let it control the lifetime. Very invasive.
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Thinking about it more that is actually not so much of a problem.
Let me try to code something together by the end of next week or so.
The hero Gotham needs :)
Quoted 6 lines
Are you sure about this one? I think unless the problems around drm_sched_start/stop and
free_guiltyare fixed, my feeling is this entire thing is still badly broken for anyone who wants to use those.To sum up this whole email: I strongly disagree with option #3, but if that is the consensus, I will, of course, support the effort.
Quoted 7 lines
I would like to discuss those topics with Danilo, too, who returns from LPC soonish. Also to get some more insights into Nouveau and our use- cases.
My suggestion is that we pick the conversation up again soonish. Christmas is around the corner, and I suppose we can't fix this all up in 2025 anyways, so we might want to give it a fresh re-spin in '26.
Since we finally found the root cause I'm all in postponing that till next year.
Christian.
Quoted 19 lines
Greetings,
P.[¹] The strangest aversion in
drm_sched, however, is the one against locking. Locks were only taken when *absolutely* necessary. It's as if the entire code was designed by some gamers who want to show their youtube subscribers that they can get 1fps more by changing RAM timings in the bios.drm_schedmight be the component in the kernel using the most synchronization mechanisms: Spinlocks, RCU, atomic integers, atomic instructions, refcounting,READ_ONCE(), just accessing locklessly… even Paul McKenney would get wide eyes here. The only thing we're still missing is seqlocks andrw_locks, but maybe we can add those /sThat's likely sth we can't get repaired at all anymore.
Quoted 4 lines · Christian König
Quoted 2 lines · Philipp Stanner
Quoted 1 line · Matthew Brost
On Tue, Dec 09, 2025 at 03:19:40PM +0100, Christian König wrote:
..
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
My educated guess is that
drm_sched_stop()inserted the job back into the pending list, but I still have no idea how it is possible thatfree_jobis running after the scheduler is stopped.I believe I found your problem, referencing
amdgpu/amdgpu_device.chere.6718 if (job)
6719 ti =amdgpu_vm_get_task_info_pasid(adev, job->pasid);WTF! There it is! Thanks a lot for pointing that out!
scripts/decode_stacktrace.sh should be able to find the exact location of a UAF. Requires manual debugging with the kernel build tree at hands, though. So that's difficult in CIs.
Quoted 63 lines
Quoted 9 lines
Quoted 9 lines
Which is after:
6695
skip_hw_reset:
6696 r =amdgpu_device_sched_resume(&device_list, reset_context, job_signaled); 6697 if (r)
6698 gotoreset_unlock;Quoted 6 lines
Quoted 6 lines
The job is likely added back into this free list here:
6676 amdgpu_device_halt_activities(adev, job,
reset_context,&device_list, 6677 hive,need_emergency_restart);So
free_jobruns and 'job->pasid' explodes.I've read over that code like a hundred times and didn't realized that the job is accessed after the scheduler resumes.
Quoted 39 lines
Quoted 20 lines
Save off the pasid on the stack at top of this function and I suspect your UAF goes away. This won't untangle this hair ball of code but I believe this at least prevent explosions.
But let’s dig in further—amdgpu_device_halt_activities calls
drm_sched_stop(Xe just callsdrm_sched_wqueue_stopfor reference). This function stops the work items, then adds the offending job back to the pending list, iterates over each job, removes the CB, leaving the job in the pending list. If the CB can be removed, it removes the job from pending, maybe callsfree_jobif it’s not a guilty job, and if it is a guilty job, defers thefree_jobto the timed-out job so it doesn’t disappear. Like WTF?Oh, it gets better—amdgpu_device_sched_resume calls
drm_sched_start, which iterates over the pending list and reinserts the same CB thatdrm_sched_stopremoved, then starts the scheduler. So the guilty job had its CB successfully removed, and now it can immediately disappear—also like WTF?Quoted 10 lines
Free_guiltyis clearly a hack around the job not being reference counted, and it doesn’t even work in some cases. Putting that aside, I think callingfree_jobshouldn’t really ever happen in TDR. Imagine ifdrm_sched_job_timedoutjust took a reference to the job like normal kernel code—free_guilty could be dropped, and immediately this all becomes safe. Likewise, if therun_jobwork item had a reference to the job, which it takes before adding to the pending list and drops after it’s done touching it in this function, thenrun_jobandfree_jobwork items could safely execute in parallel rather than relying on an ordered workqueue to keep that part of the code safe.I can tell you how I design it in our Rust jobqueue:
Drivers create jobs, and insubmit_job()the pass ownership over the job to the jobqueue – IOW after pushing a job, a driver can't access it anymore. In therun_job()callback, the jobqueue either passes the job back by value (ownership) or borrows the job to the driver so that it can be copied (this is done so that the JQ can hypothetically do resubmits).Quoted 5 lines
This way there is no need for refcounting (in Rust / jobqueue).
Maybe the core of the problem is not so much the lack of refcounting, but the lack of ownership rules. Why even would the driver need the job still after it got pushed? It should be fire-and-forget.
Quoted 5 lines
Yeah, that sounds sane to me as well and is exactly how it was initially designed in the
drm_scheduleras well.The job is basically just the information the driver needs for the submission which it gives to the scheduler on push, and the scheduler gives back to the driver on pop.
The full execution time is represented by the scheduler fence and not the job. And the scheduler fence is reference counted exactly because of the reasons Mathew brought up here.
Would be interesting to hear where Xe would still need the job. If only the backend_ops give a driver access to a job again after it got pushed, then it should be safe.
Quoted 46 lines
I'm absolutely not against reference counting, what I'm pushing back is abusing the job object as something it was never designed for while we already have an object which implements exactly the needed functionality.
Quoted 44 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
And my uneducated guess is that it's happening in amdgpu. It seems a
sched_joblives inside anamdgpu_job. Can the latter be freed at other places thanfree_job()?Quoted 1 line
[…]
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
It basically says to the driver that the job lifetime problems created by the scheduler is the driver problem and need to be worked around there.
My POV still mostly is that (with the current design) the driver must not use jobs after
free_job()was invoked. And when that happens is unpredictable.Quoted 5 lines
This is somewhat of an absurd statement from my point of view. I have a valid job pointer, then I call another function (see above for an example of how drm_sched_start/stop is unsafe) and it disappears behind my back.
The statement is absurd because reality (the code) is absurd. We all are basically Alice in Wonderland, running as fast as we can just to remain on the same spot ^_^
What I am stating is not that this is *good*, but this is what it currently is like. Whether we like it or not.
The misunderstanding you and I might have is that for me jobs having to be refcounted is not a reality until it's reflected in code, documentation and, ideally, drivers.
Quoted 5 lines
The safe way to handle this is to take a local reference before doing anything that could make it disappear. That is much more reasonable than saying, “we have five things you can do in the scheduler, and if you do any of them it isn’t safe to touch the job afterward.”
Yeah, but that's
drm_schedbeingdrm_scheddy. Before I documented it there were also these implicit refcounting rules inrun_job(), where the driver needs to take the reference for the scheduler for it to be non-racy.
Quoted 7 lines
Quoted 3 lines
It also wasn't documented for a long time that
drm_sched(throughspsc_queue) will explode if you don't use entities with a single producer thread.That is actually documented, but not on the scheduler but rather the
dma_fence.And that you can only have a single producer is a requirement inherited from the
dma_fenceand not scheduler specific at all.
What does dma_fence have to do with it? It's about the spsc_queue being racy like mad. You can access and modify dma_fence's in parallel however you want – they are refcounted and locked.
P.
Quoted 90 lines
Quoted 58 lines
drm_schedgot here because of gross design mistakes, lots of hacks for few drivers, and, particularly, a strange aversion¹ against writing documentation. If Xe came, back in the day, to the conclusion that job lifetimes are fundamentally broken and that the objectively correct way to solve this is refcounting, then why wasn't that pushed intodrm_schedback then?Quoted 42 lines
Quoted 14 lines
Quoted 14 lines
To be unfair, we already have strange refcount expectations already. But I sort of agree that there is no objectively good solution in sight.
Quoted 9 lines
Quoted 9 lines
Let me respin to my documentation series and upstream that soonish, than we can build on top of that.
P.
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
2. Move reference counting to the base DRM scheduler job object, provide a vfunc for the final job put, and document usage. Medium invasive.
I strongly think that reference counting the job object just because the scheduler needs it is a bad idea.
With that we are just moving the hot potato from one side of our mouth to the other without really solving the issue.
See above—I can’t say I agree with this assessment. I think the lack of reference counting is exactly the problem. I don’t really understand the pushback on a very well-understood concept (reference counting) in Linux. I would sign up to fix the entire subsystem if we go this route.
Quoted 8 lines
Quoted 8 lines
Quoted 1 line
If a driver like XE needs that for some reason than that is perfectly fine.
Nouveau doesn't need it either.
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
3. Move job (driver) side tracking to the scheduler fence and let it control the lifetime. Very invasive.
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Thinking about it more that is actually not so much of a problem.
Let me try to code something together by the end of next week or so.
The hero Gotham needs :)
Quoted 6 lines
Are you sure about this one? I think unless the problems around drm_sched_start/stop and
free_guiltyare fixed, my feeling is this entire thing is still badly broken for anyone who wants to use those.To sum up this whole email: I strongly disagree with option #3, but if that is the consensus, I will, of course, support the effort.
Quoted 7 lines
I would like to discuss those topics with Danilo, too, who returns from LPC soonish. Also to get some more insights into Nouveau and our use- cases.
My suggestion is that we pick the conversation up again soonish. Christmas is around the corner, and I suppose we can't fix this all up in 2025 anyways, so we might want to give it a fresh re-spin in '26.
Since we finally found the root cause I'm all in postponing that till next year.
Christian.
Quoted 19 lines
Greetings,
P.[¹] The strangest aversion in
drm_sched, however, is the one against locking. Locks were only taken when *absolutely* necessary. It's as if the entire code was designed by some gamers who want to show their youtube subscribers that they can get 1fps more by changing RAM timings in the bios.drm_schedmight be the component in the kernel using the most synchronization mechanisms: Spinlocks, RCU, atomic integers, atomic instructions, refcounting,READ_ONCE(), just accessing locklessly… even Paul McKenney would get wide eyes here. The only thing we're still missing is seqlocks andrw_locks, but maybe we can add those /sThat's likely sth we can't get repaired at all anymore.
Quoted 9 lines · Philipp Stanner
Quoted 8 lines · Christian König
Quoted 2 lines · Philipp Stanner
Quoted 1 line · Matthew Brost
On Tue, Dec 09, 2025 at 03:19:40PM +0100, Christian König wrote:
..
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
My educated guess is that
drm_sched_stop()inserted the job back into the pending list, but I still have no idea how it is possible thatfree_jobis running after the scheduler is stopped.I believe I found your problem, referencing
amdgpu/amdgpu_device.chere.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
6718 if (job)
6719 ti =amdgpu_vm_get_task_info_pasid(adev, job->pasid);WTF! There it is! Thanks a lot for pointing that out!
scripts/decode_stacktrace.shshould be able to find the exact location of a UAF. Requires manual debugging with the kernel build tree at hands, though. So that's difficult in CIs.
The debugging info was actually pointing to the return of the function. My guess is that it just optimized away something.
Quoted 11 lines
Quoted 7 lines
Quoted 3 lines
It also wasn't documented for a long time that
drm_sched(throughspsc_queue) will explode if you don't use entities with a single producer thread.That is actually documented, but not on the scheduler but rather the
dma_fence.And that you can only have a single producer is a requirement inherited from the
dma_fenceand not scheduler specific at all.What does
dma_fencehave to do with it? It's about thespsc_queuebeing racy like mad. You can access and modify dma_fence's in parallel however you want – they are refcounted and locked.
The problem is that the driver needs to guarantee that drm_sched_job_arm() and drm_sched_entity_push_job() can only be called by a single producer.
Otherwise you violate the ordering rules of the underlying dma_fence.
That is completely independent and comes even before the spsc queue comes into the picture.
Regards,
Christian.
Quoted 3 lines
P.
Matthew Brost
maintainerQuoted 5 lines · Philipp Stanner
Quoted 4 lines · Christian König
Quoted 2 lines · Philipp Stanner
Quoted 1 line · Matthew Brost
On Tue, Dec 09, 2025 at 03:19:40PM +0100, Christian König wrote:
..
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
My educated guess is that
drm_sched_stop()inserted the job back into the pending list, but I still have no idea how it is possible thatfree_jobis running after the scheduler is stopped.I believe I found your problem, referencing
amdgpu/amdgpu_device.chere.6718 if (job)
6719 ti =amdgpu_vm_get_task_info_pasid(adev, job->pasid);WTF! There it is! Thanks a lot for pointing that out!
No problem.
Quoted 5 lines
scripts/decode_stacktrace.shshould be able to find the exact location of a UAF. Requires manual debugging with the kernel build tree at hands, though. So that's difficult in CIs.
I wasn't aware of this. Usually I do below after rebuilding kernel with debug symbols.
gdb <offending_file.o>
list *(<last stack trace line>)
Here I just saw 'last stack trace line' was at the very end of amdgpu_device_gpu_recover and happend to spot this.
Quoted 57 lines
Quoted 57 lines
Quoted 9 lines
Quoted 9 lines
Which is after:
6695
skip_hw_reset:
6696 r =amdgpu_device_sched_resume(&device_list, reset_context, job_signaled); 6697 if (r)
6698 gotoreset_unlock;Quoted 6 lines
Quoted 6 lines
The job is likely added back into this free list here:
6676 amdgpu_device_halt_activities(adev, job,
reset_context,&device_list, 6677 hive,need_emergency_restart);So
free_jobruns and 'job->pasid' explodes.I've read over that code like a hundred times and didn't realized that the job is accessed after the scheduler resumes.
Quoted 39 lines
Quoted 20 lines
Save off the pasid on the stack at top of this function and I suspect your UAF goes away. This won't untangle this hair ball of code but I believe this at least prevent explosions.
But let’s dig in further—amdgpu_device_halt_activities calls
drm_sched_stop(Xe just callsdrm_sched_wqueue_stopfor reference). This function stops the work items, then adds the offending job back to the pending list, iterates over each job, removes the CB, leaving the job in the pending list. If the CB can be removed, it removes the job from pending, maybe callsfree_jobif it’s not a guilty job, and if it is a guilty job, defers thefree_jobto the timed-out job so it doesn’t disappear. Like WTF?Oh, it gets better—amdgpu_device_sched_resume calls
drm_sched_start, which iterates over the pending list and reinserts the same CB thatdrm_sched_stopremoved, then starts the scheduler. So the guilty job had its CB successfully removed, and now it can immediately disappear—also like WTF?Quoted 10 lines
Free_guiltyis clearly a hack around the job not being reference counted, and it doesn’t even work in some cases. Putting that aside, I think callingfree_jobshouldn’t really ever happen in TDR. Imagine ifdrm_sched_job_timedoutjust took a reference to the job like normal kernel code—free_guilty could be dropped, and immediately this all becomes safe. Likewise, if therun_jobwork item had a reference to the job, which it takes before adding to the pending list and drops after it’s done touching it in this function, thenrun_jobandfree_jobwork items could safely execute in parallel rather than relying on an ordered workqueue to keep that part of the code safe.I can tell you how I design it in our Rust jobqueue:
Drivers create jobs, and insubmit_job()the pass ownership over the job to the jobqueue – IOW after pushing a job, a driver can't access it anymore. In therun_job()callback, the jobqueue either passes the job back by value (ownership) or borrows the job to the driver so that it can be copied (this is done so that the JQ can hypothetically do resubmits).
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
This way there is no need for refcounting (in Rust / jobqueue).
See below. If you need to resubmit for any reason, where will the information for resubmission be stored? Likewise, if you want to drop additional references on fence signaling, how are you going to retrieve that?
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Maybe the core of the problem is not so much the lack of refcounting, but the lack of ownership rules. Why even would the driver need the job still after it got pushed? It should be fire-and-forget.
Yeah, that sounds sane to me as well and is exactly how it was initially designed in the
drm_scheduleras well.
Quoted 8 lines
Quoted 3 lines
The job is basically just the information the driver needs for the submission which it gives to the scheduler on push, and the scheduler gives back to the driver on pop.
The full execution time is represented by the scheduler fence and not the job. And the scheduler fence is reference counted exactly because of the reasons Mathew brought up here.
Would be interesting to hear where Xe would still need the job. If only the
backend_opsgive a driver access to a job again after it got pushed, then it should be safe.
Xe needs a subset of the job after submission to handle tasks like resubmission after a device reset. It’s questionable whether we need this, as it shouldn’t happen in practice—only individual queues should fail with a working KMD and hardware. It likely doesn’t work anyway if queues have interdependencies. This is really an opportunistic approach.
However, we absolutely need this for VF migration resubmission. Again, this requires only a very small subset of driver job information. I believe it’s just the starting point in the ring, batch address(es), and a pointer to the driver-side queue object.
We also build a reference-counting model around jobs, where the final put releases other objects and runtime power management references. This assumes that the job’s final put means the scheduler fence is signaled. Again, this is really just a small subset of information we need here.
So if we add hooks to store the subset of information Xe needs for everything above in the scheduler fence and a non-IRQ, pausable callback (i.e., won’t execute when the scheduler is stopped, like free_job), this could be made to work. We really don’t need about 90% of the information in the job and certainly nothing in the base object.
This would be major surgery, though. I suspect most drivers have a subset of information in the job that needs to stick around until it signals, so this means surgery across 11 drivers.
Quoted 2 lines
Quoted 2 lines
I'm absolutely not against reference counting, what I'm pushing back is abusing the job object as something it was never designed for while we already have an object which implements exactly the needed functionality.
Well, oops. Having free_job called after the fence is signaled is how I arrived at the implementation in Xe. I agree that if we can move driver info into the scheduler fence, this could work for likely everyone.
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
And my uneducated guess is that it's happening in amdgpu. It seems a
sched_joblives inside anamdgpu_job. Can the latter be freed at other places thanfree_job()?Quoted 1 line
[…]
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
It basically says to the driver that the job lifetime problems created by the scheduler is the driver problem and need to be worked around there.
My POV still mostly is that (with the current design) the driver must not use jobs after
free_job()was invoked. And when that happens is unpredictable.Quoted 5 lines
This is somewhat of an absurd statement from my point of view. I have a valid job pointer, then I call another function (see above for an example of how drm_sched_start/stop is unsafe) and it disappears behind my back.
The statement is absurd because reality (the code) is absurd. We all are basically Alice in Wonderland, running as fast as we can just to remain on the same spot ^_^
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
What I am stating is not that this is *good*, but this is what it currently is like. Whether we like it or not.
The misunderstanding you and I might have is that for me jobs having to be refcounted is not a reality until it's reflected in code, documentation and, ideally, drivers.
I agree this part is badly misdesigned. In the timedout job callback, you’re handed a job, and if you perform certain actions, it can just disappear— even all the way back to the caller of timedout_job. That’s not great. Then we have this free_guilty mechanism to avoid it disappearing, but sometimes it still does, which is horrible.
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 5 lines
The safe way to handle this is to take a local reference before doing anything that could make it disappear. That is much more reasonable than saying, “we have five things you can do in the scheduler, and if you do any of them it isn’t safe to touch the job afterward.”
Yeah, but that's
drm_schedbeingdrm_scheddy. Before I documented it there were also these implicit refcounting rules inrun_job(), where the driver needs to take the reference for the scheduler for it to be non-racy.
Yes, agreed. This is my fault for not being more responsible in fixing issues rather than just backing away from these really scary parts of the code (e.g., drm_sched_stop, drm_sched_start, drm_sched_resubmit_jobs, etc.) and doing something sane in Xe by using only a subset of the scheduler.
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
It also wasn't documented for a long time that
drm_sched(throughspsc_queue) will explode if you don't use entities with a single producer thread.That is actually documented, but not on the scheduler but rather the
dma_fence.
spsc - "Single producer, Single consumer". So it is in the name.
Quoted 77 lines
Quoted 1 line
And that you can only have a single producer is a requirement inherited from the
dma_fenceand not scheduler specific at all.What does
dma_fencehave to do with it? It's about thespsc_queuebeing racy like mad. You can access and modify dma_fence's in parallel however you want – they are refcounted and locked.P.
Quoted 68 lines
Quoted 58 lines
drm_schedgot here because of gross design mistakes, lots of hacks for few drivers, and, particularly, a strange aversion¹ against writing documentation. If Xe came, back in the day, to the conclusion that job lifetimes are fundamentally broken and that the objectively correct way to solve this is refcounting, then why wasn't that pushed intodrm_schedback then?Quoted 42 lines
Quoted 14 lines
Quoted 14 lines
To be unfair, we already have strange refcount expectations already. But I sort of agree that there is no objectively good solution in sight.
Quoted 9 lines
Quoted 9 lines
Let me respin to my documentation series and upstream that soonish, than we can build on top of that.
P.
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
2. Move reference counting to the base DRM scheduler job object, provide a vfunc for the final job put, and document usage. Medium invasive.
I strongly think that reference counting the job object just because the scheduler needs it is a bad idea.
With that we are just moving the hot potato from one side of our mouth to the other without really solving the issue.
See above—I can’t say I agree with this assessment. I think the lack of reference counting is exactly the problem. I don’t really understand the pushback on a very well-understood concept (reference counting) in Linux. I would sign up to fix the entire subsystem if we go this route.
Quoted 8 lines
Quoted 8 lines
Quoted 1 line
If a driver like XE needs that for some reason than that is perfectly fine.
Nouveau doesn't need it either.
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
3. Move job (driver) side tracking to the scheduler fence and let it control the lifetime. Very invasive.
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Thinking about it more that is actually not so much of a problem.
Let me try to code something together by the end of next week or so.
The hero Gotham needs :)
Quoted 6 lines
Are you sure about this one? I think unless the problems around drm_sched_start/stop and
free_guiltyare fixed, my feeling is this entire thing is still badly broken for anyone who wants to use those.To sum up this whole email: I strongly disagree with option #3, but if that is the consensus, I will, of course, support the effort.
Quoted 7 lines
I would like to discuss those topics with Danilo, too, who returns from LPC soonish. Also to get some more insights into Nouveau and our use- cases.
My suggestion is that we pick the conversation up again soonish. Christmas is around the corner, and I suppose we can't fix this all up in 2025 anyways, so we might want to give it a fresh re-spin in '26.
Since we finally found the root cause I'm all in postponing that till next year.
Ok, glad we found the root cause. I’m not as opposed to option #3 as stated—this was a bit of angry typing—but if we go in that direction, we really need clear rules, for example:
- A job cannot be referenced driver-side after the initial drm_sched_entity_push_job call, aside from a single run_job callback. Maybe run_job is actually replaced by a scheduler fence input?
- Drivers can attach information to the scheduler fence and control its lifetime.
- Drivers can iterate over pending scheduler fences when the scheduler is stopped.
- Drivers receive a pausable callback in a non-IRQ context when the scheduler fence signals.
etc...
Again, this is a pretty major change. I personally wouldn’t feel comfortable hacking 11 drivers—10 of which aren’t mine—to do something like this. Refcounting the job would be less invasive and would make the existing hairball of code safe.
Matt
Quoted 23 lines
Quoted 22 lines
Christian.
Quoted 19 lines
Greetings,
P.[¹] The strangest aversion in
drm_sched, however, is the one against locking. Locks were only taken when *absolutely* necessary. It's as if the entire code was designed by some gamers who want to show their youtube subscribers that they can get 1fps more by changing RAM timings in the bios.drm_schedmight be the component in the kernel using the most synchronization mechanisms: Spinlocks, RCU, atomic integers, atomic instructions, refcounting,READ_ONCE(), just accessing locklessly… even Paul McKenney would get wide eyes here. The only thing we're still missing is seqlocks andrw_locks, but maybe we can add those /sThat's likely sth we can't get repaired at all anymore.
Quoted 35 lines · Matthew Brost
Quoted 5 lines · Philipp Stanner
Quoted 4 lines · Christian König
Quoted 2 lines · Philipp Stanner
Quoted 1 line · Matthew Brost
On Tue, Dec 09, 2025 at 03:19:40PM +0100, Christian König wrote:
..
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
My educated guess is that
drm_sched_stop()inserted the job back into the pending list, but I still have no idea how it is possible thatfree_jobis running after the scheduler is stopped.I believe I found your problem, referencing
amdgpu/amdgpu_device.chere.6718 if (job)
6719 ti =amdgpu_vm_get_task_info_pasid(adev, job->pasid);WTF! There it is! Thanks a lot for pointing that out!
No problem.
Quoted 5 lines
scripts/decode_stacktrace.shshould be able to find the exact location of a UAF. Requires manual debugging with the kernel build tree at hands, though. So that's difficult in CIs.I wasn't aware of this. Usually I do below after rebuilding kernel with debug symbols.
gdb <offending_file.o>
list *(<last stack trace line>)Here I just saw 'last stack trace line' was at the very end of
amdgpu_device_gpu_recoverand happend to spot this.Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
[…]
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
I can tell you how I design it in our Rust jobqueue:
Drivers create jobs, and insubmit_job()the pass ownership over the job to the jobqueue – IOW after pushing a job, a driver can't access it anymore. In therun_job()callback, the jobqueue either passes the job back by value (ownership) or borrows the job to the driver so that it can be copied (this is done so that the JQ can hypothetically do resubmits).
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
This way there is no need for refcounting (in Rust / jobqueue).
See below. If you need to resubmit for any reason, where will the information for resubmission be stored? Likewise, if you want to drop additional references on fence signaling, how are you going to retrieve that?
Well yes, as I just stated, it is, unfortunately, always necessary to have a list of running jobs. The jobs inside of it don't need to be shared with the driver, though.
Jobqueue could do resubmits through help with that list, by running the desired jobs again. While run_job() executes, jobs are loaned to the driver, who only needs them temporarily, not permanently.
One could have that loaning in C, too, but would not be able to enforce it.
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 4 lines
Maybe the core of the problem is not so much the lack of refcounting, but the lack of ownership rules. Why even would the driver need the job still after it got pushed? It should be fire-and-forget.
Yeah, that sounds sane to me as well and is exactly how it was initially designed in the
drm_scheduleras well.The job is basically just the information the driver needs for the submission which it gives to the scheduler on push, and the scheduler gives back to the driver on pop.
The full execution time is represented by the scheduler fence and not the job. And the scheduler fence is reference counted exactly because of the reasons Mathew brought up here.
Quoted 8 lines
Quoted 4 lines
Would be interesting to hear where Xe would still need the job. If only the
backend_opsgive a driver access to a job again after it got pushed, then it should be safe.Xe needs a subset of the job after submission to handle tasks like resubmission after a device reset.
the job or the jobS?
Because you get the job that caused the timeout by the scheduler, through timedout_job().
And the rest you need will soonish be obtainable through the new iterator. So what else do you need?
Quoted 9 lines
It’s questionable whether we need
this, as it shouldn’t happen in practice—only individual queues should fail with a working KMD and hardware. It likely doesn’t work anyway if queues have interdependencies. This is really an opportunistic approach.However, we absolutely need this for VF migration resubmission. Again, this requires only a very small subset of driver job information. I believe it’s just the starting point in the ring, batch address(es), and a pointer to the driver-side queue object.
In Rust, I borrow the job to the driver. So if it really needs something about it permanently, it can copy it into some object with a decoupled life-time.
Or maybe have the job-struct's generic data-field contain something refcounted, IDK.
Quoted 12 lines
We also build a reference-counting model around jobs, where the final put releases other objects and runtime power management references. This assumes that the job’s final put means the scheduler fence is signaled. Again, this is really just a small subset of information we need here.
So if we add hooks to store the subset of information Xe needs for everything above in the scheduler fence and a non-IRQ, pausable callback (i.e., won’t execute when the scheduler is stopped, like
free_job), this could be made to work. We really don’t need about 90% of the information in the job and certainly nothing in the base object.
Quoted 3 lines
This would be major surgery, though. I suspect most drivers have a subset of information in the job that needs to stick around until it signals, so this means surgery across 11 drivers.
Not sure if that's worth it. My hope would more be that interested users with firmware scheduling can switch to jobqueue and start over with a fresh, clean design with proper memory life times.
Quoted 42 lines
Quoted 3 lines
Quoted 3 lines
I'm absolutely not against reference counting, what I'm pushing back is abusing the job object as something it was never designed for while we already have an object which implements exactly the needed functionality.
Well, oops. Having
free_jobcalled after the fence is signaled is how I arrived at the implementation in Xe. I agree that if we can move driver info into the scheduler fence, this could work for likely everyone.Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
And my uneducated guess is that it's happening in amdgpu. It seems a
sched_joblives inside anamdgpu_job. Can the latter be freed at other places thanfree_job()?Quoted 1 line
[…]
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
It basically says to the driver that the job lifetime problems created by the scheduler is the driver problem and need to be worked around there.
My POV still mostly is that (with the current design) the driver must not use jobs after
free_job()was invoked. And when that happens is unpredictable.Quoted 5 lines
This is somewhat of an absurd statement from my point of view. I have a valid job pointer, then I call another function (see above for an example of how drm_sched_start/stop is unsafe) and it disappears behind my back.
The statement is absurd because reality (the code) is absurd. We all are basically Alice in Wonderland, running as fast as we can just to remain on the same spot ^_^
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
What I am stating is not that this is *good*, but this is what it currently is like. Whether we like it or not.
The misunderstanding you and I might have is that for me jobs having to be refcounted is not a reality until it's reflected in code, documentation and, ideally, drivers.
Quoted 5 lines
I agree this part is badly misdesigned. In the timedout job callback, you’re handed a job, and if you perform certain actions, it can just disappear— even all the way back to the caller of
timedout_job. That’s not great. Then we have thisfree_guiltymechanism to avoid it disappearing, but sometimes it still does, which is horrible.
Who makes it disappear, the driver callback? Because that free_guilty mechanism is what frees jobs in the first place.
The more you think about it, the more astonished you become how this could ever have been designed and merged that way. There was no clean design anywhere, neither with APIs, nor life times, nor locking.
Quoted 13 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
The safe way to handle this is to take a local reference before doing anything that could make it disappear. That is much more reasonable than saying, “we have five things you can do in the scheduler, and if you do any of them it isn’t safe to touch the job afterward.”
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Yeah, but that's
drm_schedbeingdrm_scheddy. Before I documented it there were also these implicit refcounting rules inrun_job(), where the driver needs to take the reference for the scheduler for it to be non-racy.
Quoted 5 lines
Yes, agreed. This is my fault for not being more responsible in fixing issues rather than just backing away from these really scary parts of the code (e.g.,
drm_sched_stop,drm_sched_start,drm_sched_resubmit_jobs, etc.) and doing something sane in Xe by using only a subset of the scheduler.
It's a bit like writing C++: no one can ever agree which feature subset is safe to use.
That's why we want to do a fresh restart for firmware-schedulers, since they allow us to drastically simplify things. Timeout? Close the ring. Job-pushing is fire and forget. Resets? Rings aren't shared.
Quoted 9 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
It also wasn't documented for a long time that
drm_sched(throughspsc_queue) will explode if you don't use entities with a single producer thread.That is actually documented, but not on the scheduler but rather the
dma_fence.spsc - "Single producer, Single consumer". So it is in the name.
Ah, NTOTM.
What's obvious for one party is a mystery to someone with a different mind. I recognized the meaning after months, after work one day.
But don't get me started on that queue……
Quoted 79 lines
Quoted 78 lines
Quoted 2 lines
And that you can only have a single producer is a requirement inherited from the
dma_fenceand not scheduler specific at all.What does
dma_fencehave to do with it? It's about thespsc_queuebeing racy like mad. You can access and modify dma_fence's in parallel however you want – they are refcounted and locked.P.
Quoted 68 lines
Quoted 58 lines
drm_schedgot here because of gross design mistakes, lots of hacks for few drivers, and, particularly, a strange aversion¹ against writing documentation. If Xe came, back in the day, to the conclusion that job lifetimes are fundamentally broken and that the objectively correct way to solve this is refcounting, then why wasn't that pushed intodrm_schedback then?Quoted 42 lines
Quoted 14 lines
Quoted 14 lines
To be unfair, we already have strange refcount expectations already. But I sort of agree that there is no objectively good solution in sight.
Quoted 9 lines
Quoted 9 lines
Let me respin to my documentation series and upstream that soonish, than we can build on top of that.
P.
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
2. Move reference counting to the base DRM scheduler job object, provide a vfunc for the final job put, and document usage. Medium invasive.
I strongly think that reference counting the job object just because the scheduler needs it is a bad idea.
With that we are just moving the hot potato from one side of our mouth to the other without really solving the issue.
See above—I can’t say I agree with this assessment. I think the lack of reference counting is exactly the problem. I don’t really understand the pushback on a very well-understood concept (reference counting) in Linux. I would sign up to fix the entire subsystem if we go this route.
Quoted 8 lines
Quoted 8 lines
Quoted 1 line
If a driver like XE needs that for some reason than that is perfectly fine.
Nouveau doesn't need it either.
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
3. Move job (driver) side tracking to the scheduler fence and let it control the lifetime. Very invasive.
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Thinking about it more that is actually not so much of a problem.
Let me try to code something together by the end of next week or so.
The hero Gotham needs :)
Quoted 6 lines
Are you sure about this one? I think unless the problems around drm_sched_start/stop and
free_guiltyare fixed, my feeling is this entire thing is still badly broken for anyone who wants to use those.To sum up this whole email: I strongly disagree with option #3, but if that is the consensus, I will, of course, support the effort.
Quoted 7 lines
I would like to discuss those topics with Danilo, too, who returns from LPC soonish. Also to get some more insights into Nouveau and our use- cases.
My suggestion is that we pick the conversation up again soonish. Christmas is around the corner, and I suppose we can't fix this all up in 2025 anyways, so we might want to give it a fresh re-spin in '26.
Since we finally found the root cause I'm all in postponing that till next year.
Quoted 6 lines
Ok, glad we found the root cause. I’m not as opposed to option #3 as stated—this was a bit of angry typing—but if we go in that direction, we really need clear rules, for example:
- A job cannot be referenced driver-side after the initial
drm_sched_entity_push_jobcall, aside from a singlerun_jobcallback.
That's what the current code and documentation demand, yes.
Quoted 1 line
Maybe
run_jobis actually replaced by a scheduler fence input?
fence input?
Quoted 4 lines
- Drivers can attach information to the scheduler fence and control its lifetime.
- Drivers can iterate over pending scheduler fences when the scheduler is stopped.
That sounds as if we're about to make a mistake with the job-iterator.
Quoted 9 lines
- Drivers receive a pausable callback in a non-IRQ context when the scheduler fence signals.
etc...
Again, this is a pretty major change. I personally wouldn’t feel comfortable hacking 11 drivers—10 of which aren’t mine—to do something like this. Refcounting the job would be less invasive and would make the existing hairball of code safe.
See my firmware-scheduler comment above. The issue is that getting systems with lax rules back under control in hindsight is 10x as expensive as carefully designing strict rules from the get-go.
My experience so far is that a maintainer's primary job is actually keeping APIs consistent and forcing people to document everything properly.
P.
Quoted 26 lines
Matt
Quoted 23 lines
Quoted 22 lines
Christian.
Quoted 19 lines
Greetings,
P.[¹] The strangest aversion in
drm_sched, however, is the one against locking. Locks were only taken when *absolutely* necessary. It's as if the entire code was designed by some gamers who want to show their youtube subscribers that they can get 1fps more by changing RAM timings in the bios.drm_schedmight be the component in the kernel using the most synchronization mechanisms: Spinlocks, RCU, atomic integers, atomic instructions, refcounting,READ_ONCE(), just accessing locklessly… even Paul McKenney would get wide eyes here. The only thing we're still missing is seqlocks andrw_locks, but maybe we can add those /sThat's likely sth we can't get repaired at all anymore.
Quoted 6 lines · Philipp Stanner
Quoted 5 lines · Matthew Brost
Quoted 4 lines · Philipp Stanner
Quoted 3 lines · Christian König
Quoted 2 lines · Philipp Stanner
Quoted 1 line · Matthew Brost
On Tue, Dec 09, 2025 at 03:19:40PM +0100, Christian König wrote:
...
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
I can tell you how I design it in our Rust jobqueue:
Drivers create jobs, and insubmit_job()the pass ownership over the job to the jobqueue – IOW after pushing a job, a driver can't access it anymore. In therun_job()callback, the jobqueue either passes the job back by value (ownership) or borrows the job to the driver so that it can be copied (this is done so that the JQ can hypothetically do resubmits).This way there is no need for refcounting (in Rust / jobqueue).
Quoted 8 lines
Quoted 4 lines
See below. If you need to resubmit for any reason, where will the information for resubmission be stored? Likewise, if you want to drop additional references on fence signaling, how are you going to retrieve that?
Well yes, as I just stated, it is, unfortunately, always necessary to have a list of running jobs. The jobs inside of it don't need to be shared with the driver, though.
Actually the initially implementation of the scheduler didn't had a list of running jobs.
The original idea was that you just call run_job() which returns a reference to the HW fence and then the scheduler installs a callback to this HW fence which wakes up the scheduler again to push the next job.
The idea of the scheduler being responsible to track what's in flight on the HW came much later and to be honest is actually not a functionality the scheduler actually needs.
Quoted 66 lines
Jobqueue could do resubmits through help with that list, by running the desired jobs again. While
run_job()executes, jobs are loaned to the driver, who only needs them temporarily, not permanently.One could have that loaning in C, too, but would not be able to enforce it.
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 4 lines
Maybe the core of the problem is not so much the lack of refcounting, but the lack of ownership rules. Why even would the driver need the job still after it got pushed? It should be fire-and-forget.
Yeah, that sounds sane to me as well and is exactly how it was initially designed in the
drm_scheduleras well.The job is basically just the information the driver needs for the submission which it gives to the scheduler on push, and the scheduler gives back to the driver on pop.
The full execution time is represented by the scheduler fence and not the job. And the scheduler fence is reference counted exactly because of the reasons Mathew brought up here.
Quoted 8 lines
Quoted 4 lines
Would be interesting to hear where Xe would still need the job. If only the
backend_opsgive a driver access to a job again after it got pushed, then it should be safe.Xe needs a subset of the job after submission to handle tasks like resubmission after a device reset.
the job or the jobS?
Because you get the job that caused the timeout by the scheduler, through
timedout_job().And the rest you need will soonish be obtainable through the new iterator. So what else do you need?
Quoted 9 lines
It’s questionable whether we need
this, as it shouldn’t happen in practice—only individual queues should fail with a working KMD and hardware. It likely doesn’t work anyway if queues have interdependencies. This is really an opportunistic approach.However, we absolutely need this for VF migration resubmission. Again, this requires only a very small subset of driver job information. I believe it’s just the starting point in the ring, batch address(es), and a pointer to the driver-side queue object.
In Rust, I borrow the job to the driver. So if it really needs something about it permanently, it can copy it into some object with a decoupled life-time.
Or maybe have the job-struct's generic data-field contain something refcounted, IDK.Quoted 12 lines
We also build a reference-counting model around jobs, where the final put releases other objects and runtime power management references. This assumes that the job’s final put means the scheduler fence is signaled. Again, this is really just a small subset of information we need here.
So if we add hooks to store the subset of information Xe needs for everything above in the scheduler fence and a non-IRQ, pausable callback (i.e., won’t execute when the scheduler is stopped, like
free_job), this could be made to work. We really don’t need about 90% of the information in the job and certainly nothing in the base object.
Quoted 7 lines
Quoted 3 lines
This would be major surgery, though. I suspect most drivers have a subset of information in the job that needs to stick around until it signals, so this means surgery across 11 drivers.
Not sure if that's worth it. My hope would more be that interested users with firmware scheduling can switch to jobqueue and start over with a fresh, clean design with proper memory life times.
That sounds reasonable to me. I was more than once at the point of wanting to nuke the scheduler and starting from scratch again.
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
I'm absolutely not against reference counting, what I'm pushing back is abusing the job object as something it was never designed for while we already have an object which implements exactly the needed functionality.
Well, oops. Having
free_jobcalled after the fence is signaled is how I arrived at the implementation in Xe. I agree that if we can move driver info into the scheduler fence, this could work for likely everyone.
Ok in that case I'm going to give that a try.
Quoted 34 lines
Quoted 34 lines
Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
And my uneducated guess is that it's happening in amdgpu. It seems a
sched_joblives inside anamdgpu_job. Can the latter be freed at other places thanfree_job()?Quoted 1 line
[…]
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
It basically says to the driver that the job lifetime problems created by the scheduler is the driver problem and need to be worked around there.
My POV still mostly is that (with the current design) the driver must not use jobs after
free_job()was invoked. And when that happens is unpredictable.Quoted 5 lines
This is somewhat of an absurd statement from my point of view. I have a valid job pointer, then I call another function (see above for an example of how drm_sched_start/stop is unsafe) and it disappears behind my back.
The statement is absurd because reality (the code) is absurd. We all are basically Alice in Wonderland, running as fast as we can just to remain on the same spot ^_^
What I am stating is not that this is *good*, but this is what it currently is like. Whether we like it or not.
The misunderstanding you and I might have is that for me jobs having to be refcounted is not a reality until it's reflected in code, documentation and, ideally, drivers.
Quoted 8 lines
Quoted 5 lines
I agree this part is badly misdesigned. In the timedout job callback, you’re handed a job, and if you perform certain actions, it can just disappear— even all the way back to the caller of
timedout_job. That’s not great. Then we have thisfree_guiltymechanism to avoid it disappearing, but sometimes it still does, which is horrible.Who makes it disappear, the driver callback? Because that
free_guiltymechanism is what frees jobs in the first place.
I think so yes.
Quoted 3 lines
The more you think about it, the more astonished you become how this could ever have been designed and merged that way. There was no clean design anywhere, neither with APIs, nor life times, nor locking.
Yeah, which is exactly the reason why I said I'm not going to maintain that stuff.
I mean I still feel guilty that it came this far, but yeah.
Quoted 18 lines
Quoted 12 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 5 lines
The safe way to handle this is to take a local reference before doing anything that could make it disappear. That is much more reasonable than saying, “we have five things you can do in the scheduler, and if you do any of them it isn’t safe to touch the job afterward.”
Yeah, but that's
drm_schedbeingdrm_scheddy. Before I documented it there were also these implicit refcounting rules inrun_job(), where the driver needs to take the reference for the scheduler for it to be non-racy.Quoted 5 lines
Yes, agreed. This is my fault for not being more responsible in fixing issues rather than just backing away from these really scary parts of the code (e.g.,
drm_sched_stop,drm_sched_start,drm_sched_resubmit_jobs, etc.) and doing something sane in Xe by using only a subset of the scheduler.
Quoted 6 lines
It's a bit like writing C++: no one can ever agree which feature subset is safe to use.
That's why we want to do a fresh restart for firmware-schedulers, since they allow us to drastically simplify things. Timeout? Close the ring. Job-pushing is fire and forget. Resets? Rings aren't shared.
+1
Quoted 99 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
It also wasn't documented for a long time that
drm_sched(throughspsc_queue) will explode if you don't use entities with a single producer thread.That is actually documented, but not on the scheduler but rather the
dma_fence.spsc - "Single producer, Single consumer". So it is in the name.
Ah, NTOTM.
What's obvious for one party is a mystery to someone with a different mind. I recognized the meaning after months, after work one day.
But don't get me started on that queue……
Quoted 83 lines
Quoted 78 lines
Quoted 2 lines
And that you can only have a single producer is a requirement inherited from the
dma_fenceand not scheduler specific at all.What does
dma_fencehave to do with it? It's about thespsc_queuebeing racy like mad. You can access and modify dma_fence's in parallel however you want – they are refcounted and locked.P.
Quoted 68 lines
Quoted 58 lines
drm_schedgot here because of gross design mistakes, lots of hacks for few drivers, and, particularly, a strange aversion¹ against writing documentation. If Xe came, back in the day, to the conclusion that job lifetimes are fundamentally broken and that the objectively correct way to solve this is refcounting, then why wasn't that pushed intodrm_schedback then?Quoted 42 lines
Quoted 14 lines
Quoted 14 lines
To be unfair, we already have strange refcount expectations already. But I sort of agree that there is no objectively good solution in sight.
Quoted 9 lines
Quoted 9 lines
Let me respin to my documentation series and upstream that soonish, than we can build on top of that.
P.
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
2. Move reference counting to the base DRM scheduler job object, provide a vfunc for the final job put, and document usage. Medium invasive.
I strongly think that reference counting the job object just because the scheduler needs it is a bad idea.
With that we are just moving the hot potato from one side of our mouth to the other without really solving the issue.
See above—I can’t say I agree with this assessment. I think the lack of reference counting is exactly the problem. I don’t really understand the pushback on a very well-understood concept (reference counting) in Linux. I would sign up to fix the entire subsystem if we go this route.
Quoted 8 lines
Quoted 8 lines
Quoted 1 line
If a driver like XE needs that for some reason than that is perfectly fine.
Nouveau doesn't need it either.
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
3. Move job (driver) side tracking to the scheduler fence and let it control the lifetime. Very invasive.
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Thinking about it more that is actually not so much of a problem.
Let me try to code something together by the end of next week or so.
The hero Gotham needs :)
Quoted 6 lines
Are you sure about this one? I think unless the problems around drm_sched_start/stop and
free_guiltyare fixed, my feeling is this entire thing is still badly broken for anyone who wants to use those.To sum up this whole email: I strongly disagree with option #3, but if that is the consensus, I will, of course, support the effort.
Quoted 7 lines
I would like to discuss those topics with Danilo, too, who returns from LPC soonish. Also to get some more insights into Nouveau and our use- cases.
My suggestion is that we pick the conversation up again soonish. Christmas is around the corner, and I suppose we can't fix this all up in 2025 anyways, so we might want to give it a fresh re-spin in '26.
Since we finally found the root cause I'm all in postponing that till next year.
Ok, glad we found the root cause. I’m not as opposed to option #3 as stated—this was a bit of angry typing—but if we go in that direction, we really need clear rules, for example:
Quoted 8 lines
Quoted 2 lines
- A job cannot be referenced driver-side after the initial
drm_sched_entity_push_jobcall, aside from a singlerun_jobcallback.That's what the current code and documentation demand, yes.
Quoted 1 line
Maybe
run_jobis actually replaced by a scheduler fence input?fence input?
ops->schedule(job) ?
Quoted 17 lines
Quoted 4 lines
- Drivers can attach information to the scheduler fence and control its lifetime.
- Drivers can iterate over pending scheduler fences when the scheduler is stopped.That sounds as if we're about to make a mistake with the job-iterator.
Quoted 9 lines
- Drivers receive a pausable callback in a non-IRQ context when the scheduler fence signals.
etc...
Again, this is a pretty major change. I personally wouldn’t feel comfortable hacking 11 drivers—10 of which aren’t mine—to do something like this. Refcounting the job would be less invasive and would make the existing hairball of code safe.
Quoted 7 lines
See my firmware-scheduler comment above. The issue is that getting systems with lax rules back under control in hindsight is 10x as expensive as carefully designing strict rules from the get-go.
My experience so far is that a maintainer's primary job is actually keeping APIs consistent and forcing people to document everything properly.
I would rather say that a maintainers job is to reject bad ideas and push for good ones.
Christian.
Quoted 3 lines
P.
Quoted 25 lines · Christian König
Quoted 6 lines · Philipp Stanner
Quoted 5 lines · Matthew Brost
Quoted 4 lines · Philipp Stanner
Quoted 3 lines · Christian König
Quoted 2 lines · Philipp Stanner
Quoted 1 line · Matthew Brost
On Tue, Dec 09, 2025 at 03:19:40PM +0100, Christian König wrote:
...
Quoted 17 lines
Quoted 16 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
I can tell you how I design it in our Rust jobqueue:
Drivers create jobs, and insubmit_job()the pass ownership over the job to the jobqueue – IOW after pushing a job, a driver can't access it anymore. In therun_job()callback, the jobqueue either passes the job back by value (ownership) or borrows the job to the driver so that it can be copied (this is done so that the JQ can hypothetically do resubmits).This way there is no need for refcounting (in Rust / jobqueue).
See below. If you need to resubmit for any reason, where will the information for resubmission be stored? Likewise, if you want to drop additional references on fence signaling, how are you going to retrieve that?
Quoted 8 lines
Quoted 3 lines
Well yes, as I just stated, it is, unfortunately, always necessary to have a list of running jobs. The jobs inside of it don't need to be shared with the driver, though.
Actually the initially implementation of the scheduler didn't had a list of running jobs.
The original idea was that you just call
run_job()which returns a reference to the HW fence and then the scheduler installs a callback to this HW fence which wakes up the scheduler again to push the next job.The idea of the scheduler being responsible to track what's in flight on the HW came much later and to be honest is actually not a functionality the scheduler actually needs.
As far as I understand GPU driver design so far it's unavoidable to have at the very least a list of finished fences in the scheduler, so you can signal them once the hardware fences signal.
And at the very least once you have to do resubmissions you need to remember which jobs / fences are in flight.
Unless it were possible to use the hardware_fence for everything.
Quoted 71 lines
Quoted 71 lines
Jobqueue could do resubmits through help with that list, by running the desired jobs again. While
run_job()executes, jobs are loaned to the driver, who only needs them temporarily, not permanently.One could have that loaning in C, too, but would not be able to enforce it.
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 4 lines
Maybe the core of the problem is not so much the lack of refcounting, but the lack of ownership rules. Why even would the driver need the job still after it got pushed? It should be fire-and-forget.
Yeah, that sounds sane to me as well and is exactly how it was initially designed in the
drm_scheduleras well.The job is basically just the information the driver needs for the submission which it gives to the scheduler on push, and the scheduler gives back to the driver on pop.
The full execution time is represented by the scheduler fence and not the job. And the scheduler fence is reference counted exactly because of the reasons Mathew brought up here.
Quoted 8 lines
Quoted 4 lines
Would be interesting to hear where Xe would still need the job. If only the
backend_opsgive a driver access to a job again after it got pushed, then it should be safe.Xe needs a subset of the job after submission to handle tasks like resubmission after a device reset.
the job or the jobS?
Because you get the job that caused the timeout by the scheduler, through
timedout_job().And the rest you need will soonish be obtainable through the new iterator. So what else do you need?
Quoted 9 lines
It’s questionable whether we need
this, as it shouldn’t happen in practice—only individual queues should fail with a working KMD and hardware. It likely doesn’t work anyway if queues have interdependencies. This is really an opportunistic approach.However, we absolutely need this for VF migration resubmission. Again, this requires only a very small subset of driver job information. I believe it’s just the starting point in the ring, batch address(es), and a pointer to the driver-side queue object.
In Rust, I borrow the job to the driver. So if it really needs something about it permanently, it can copy it into some object with a decoupled life-time.
Or maybe have the job-struct's generic data-field contain something refcounted, IDK.Quoted 15 lines
We also build a reference-counting model around jobs, where the final put releases other objects and runtime power management references. This assumes that the job’s final put means the scheduler fence is signaled. Again, this is really just a small subset of information we need here.
So if we add hooks to store the subset of information Xe needs for everything above in the scheduler fence and a non-IRQ, pausable callback (i.e., won’t execute when the scheduler is stopped, like
free_job), this could be made to work. We really don’t need about 90% of the information in the job and certainly nothing in the base object.This would be major surgery, though. I suspect most drivers have a subset of information in the job that needs to stick around until it signals, so this means surgery across 11 drivers.
Quoted 6 lines
Quoted 3 lines
Not sure if that's worth it. My hope would more be that interested users with firmware scheduling can switch to jobqueue and start over with a fresh, clean design with proper memory life times.
That sounds reasonable to me. I was more than once at the point of wanting to nuke the scheduler and starting from scratch again.
Actually, since the 1950s, you don't want to rewrite software at all anymore. Yet we again and again end up at that point.
Only works if you *really* get it right. Even with jobqueue that's already becoming more complicated, because we might have to support resubmits, which I hoped we could 100% avoid.
drm_sched's primary design mistake is having two job lists with different life times AND on top of that resubmits.
Quoted 48 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
I'm absolutely not against reference counting, what I'm pushing back is abusing the job object as something it was never designed for while we already have an object which implements exactly the needed functionality.
Well, oops. Having
free_jobcalled after the fence is signaled is how I arrived at the implementation in Xe. I agree that if we can move driver info into the scheduler fence, this could work for likely everyone.Ok in that case I'm going to give that a try.
Quoted 40 lines
Quoted 39 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
And my uneducated guess is that it's happening in amdgpu. It seems a
sched_joblives inside anamdgpu_job. Can the latter be freed at other places thanfree_job()?Quoted 1 line
[…]
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
It basically says to the driver that the job lifetime problems created by the scheduler is the driver problem and need to be worked around there.
My POV still mostly is that (with the current design) the driver must not use jobs after
free_job()was invoked. And when that happens is unpredictable.Quoted 5 lines
This is somewhat of an absurd statement from my point of view. I have a valid job pointer, then I call another function (see above for an example of how drm_sched_start/stop is unsafe) and it disappears behind my back.
The statement is absurd because reality (the code) is absurd. We all are basically Alice in Wonderland, running as fast as we can just to remain on the same spot ^_^
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
What I am stating is not that this is *good*, but this is what it currently is like. Whether we like it or not.
The misunderstanding you and I might have is that for me jobs having to be refcounted is not a reality until it's reflected in code, documentation and, ideally, drivers.
I agree this part is badly misdesigned. In the timedout job callback, you’re handed a job, and if you perform certain actions, it can just disappear— even all the way back to the caller of
timedout_job. That’s not great. Then we have thisfree_guiltymechanism to avoid it disappearing, but sometimes it still does, which is horrible.
Quoted 4 lines
Quoted 2 lines
Who makes it disappear, the driver callback? Because that
free_guiltymechanism is what frees jobs in the first place.I think so yes.
The driver must not free the job in timedout_job. That's completely against the design, be it bad or good. free_job() is the point where you can free. That's why it's called that.
Quoted 8 lines
Quoted 4 lines
The more you think about it, the more astonished you become how this could ever have been designed and merged that way. There was no clean design anywhere, neither with APIs, nor life times, nor locking.
Yeah, which is exactly the reason why I said I'm not going to maintain that stuff.
I mean I still feel guilty that it came this far, but yeah.
You've said so frequently, don't worry too much. I just bring it up out of surprise because, you know, some design mistakes are unavoidable. It's just very difficult, GPU resubmits, hangs and all that. But other things and certain hacks (drm_sched_fini()) are so *obviously* broken that you wonder..
P.
Quoted 170 lines
Quoted 19 lines
Quoted 13 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
The safe way to handle this is to take a local reference before doing anything that could make it disappear. That is much more reasonable than saying, “we have five things you can do in the scheduler, and if you do any of them it isn’t safe to touch the job afterward.”
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Yeah, but that's
drm_schedbeingdrm_scheddy. Before I documented it there were also these implicit refcounting rules inrun_job(), where the driver needs to take the reference for the scheduler for it to be non-racy.Quoted 5 lines
Yes, agreed. This is my fault for not being more responsible in fixing issues rather than just backing away from these really scary parts of the code (e.g.,
drm_sched_stop,drm_sched_start,drm_sched_resubmit_jobs, etc.) and doing something sane in Xe by using only a subset of the scheduler.Quoted 6 lines
It's a bit like writing C++: no one can ever agree which feature subset is safe to use.
That's why we want to do a fresh restart for firmware-schedulers, since they allow us to drastically simplify things. Timeout? Close the ring. Job-pushing is fire and forget. Resets? Rings aren't shared.
+1
Quoted 99 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
It also wasn't documented for a long time that
drm_sched(throughspsc_queue) will explode if you don't use entities with a single producer thread.That is actually documented, but not on the scheduler but rather the
dma_fence.spsc - "Single producer, Single consumer". So it is in the name.
Ah, NTOTM.
What's obvious for one party is a mystery to someone with a different mind. I recognized the meaning after months, after work one day.
But don't get me started on that queue……
Quoted 83 lines
Quoted 78 lines
Quoted 2 lines
And that you can only have a single producer is a requirement inherited from the
dma_fenceand not scheduler specific at all.What does
dma_fencehave to do with it? It's about thespsc_queuebeing racy like mad. You can access and modify dma_fence's in parallel however you want – they are refcounted and locked.P.
Quoted 68 lines
Quoted 58 lines
drm_schedgot here because of gross design mistakes, lots of hacks for few drivers, and, particularly, a strange aversion¹ against writing documentation. If Xe came, back in the day, to the conclusion that job lifetimes are fundamentally broken and that the objectively correct way to solve this is refcounting, then why wasn't that pushed intodrm_schedback then?Quoted 42 lines
Quoted 14 lines
Quoted 14 lines
To be unfair, we already have strange refcount expectations already. But I sort of agree that there is no objectively good solution in sight.
Quoted 9 lines
Quoted 9 lines
Let me respin to my documentation series and upstream that soonish, than we can build on top of that.
P.
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
2. Move reference counting to the base DRM scheduler job object, provide a vfunc for the final job put, and document usage. Medium invasive.
I strongly think that reference counting the job object just because the scheduler needs it is a bad idea.
With that we are just moving the hot potato from one side of our mouth to the other without really solving the issue.
See above—I can’t say I agree with this assessment. I think the lack of reference counting is exactly the problem. I don’t really understand the pushback on a very well-understood concept (reference counting) in Linux. I would sign up to fix the entire subsystem if we go this route.
Quoted 8 lines
Quoted 8 lines
Quoted 1 line
If a driver like XE needs that for some reason than that is perfectly fine.
Nouveau doesn't need it either.
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
3. Move job (driver) side tracking to the scheduler fence and let it control the lifetime. Very invasive.
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Thinking about it more that is actually not so much of a problem.
Let me try to code something together by the end of next week or so.
The hero Gotham needs :)
Quoted 6 lines
Are you sure about this one? I think unless the problems around drm_sched_start/stop and
free_guiltyare fixed, my feeling is this entire thing is still badly broken for anyone who wants to use those.To sum up this whole email: I strongly disagree with option #3, but if that is the consensus, I will, of course, support the effort.
Quoted 7 lines
I would like to discuss those topics with Danilo, too, who returns from LPC soonish. Also to get some more insights into Nouveau and our use- cases.
My suggestion is that we pick the conversation up again soonish. Christmas is around the corner, and I suppose we can't fix this all up in 2025 anyways, so we might want to give it a fresh re-spin in '26.
Since we finally found the root cause I'm all in postponing that till next year.
Ok, glad we found the root cause. I’m not as opposed to option #3 as stated—this was a bit of angry typing—but if we go in that direction, we really need clear rules, for example:
Quoted 8 lines
Quoted 2 lines
- A job cannot be referenced driver-side after the initial
drm_sched_entity_push_jobcall, aside from a singlerun_jobcallback.That's what the current code and documentation demand, yes.
Quoted 1 line
Maybe
run_jobis actually replaced by a scheduler fence input?fence input?
ops->schedule(job)?Quoted 17 lines
Quoted 4 lines
- Drivers can attach information to the scheduler fence and control its lifetime.
- Drivers can iterate over pending scheduler fences when the scheduler is stopped.That sounds as if we're about to make a mistake with the job-iterator.
Quoted 9 lines
- Drivers receive a pausable callback in a non-IRQ context when the scheduler fence signals.
etc...
Again, this is a pretty major change. I personally wouldn’t feel comfortable hacking 11 drivers—10 of which aren’t mine—to do something like this. Refcounting the job would be less invasive and would make the existing hairball of code safe.
Quoted 7 lines
See my firmware-scheduler comment above. The issue is that getting systems with lax rules back under control in hindsight is 10x as expensive as carefully designing strict rules from the get-go.
My experience so far is that a maintainer's primary job is actually keeping APIs consistent and forcing people to document everything properly.
I would rather say that a maintainers job is to reject bad ideas and push for good ones.
Christian.
Quoted 3 lines
P.
Matthew Brost
maintainerQuoted 33 lines · Philipp Stanner
Quoted 32 lines · Christian König
Quoted 6 lines · Philipp Stanner
Quoted 5 lines · Matthew Brost
Quoted 4 lines · Philipp Stanner
Quoted 3 lines · Christian König
Quoted 2 lines · Philipp Stanner
Quoted 1 line · Matthew Brost
On Tue, Dec 09, 2025 at 03:19:40PM +0100, Christian König wrote:
...
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
I can tell you how I design it in our Rust jobqueue:
Drivers create jobs, and insubmit_job()the pass ownership over the job to the jobqueue – IOW after pushing a job, a driver can't access it anymore. In therun_job()callback, the jobqueue either passes the job back by value (ownership) or borrows the job to the driver so that it can be copied (this is done so that the JQ can hypothetically do resubmits).This way there is no need for refcounting (in Rust / jobqueue).
Quoted 8 lines
Quoted 4 lines
See below. If you need to resubmit for any reason, where will the information for resubmission be stored? Likewise, if you want to drop additional references on fence signaling, how are you going to retrieve that?
Well yes, as I just stated, it is, unfortunately, always necessary to have a list of running jobs. The jobs inside of it don't need to be shared with the driver, though.
Actually the initially implementation of the scheduler didn't had a list of running jobs.
The original idea was that you just call
run_job()which returns a reference to the HW fence and then the scheduler installs a callback to this HW fence which wakes up the scheduler again to push the next job.
Quoted 8 lines
Quoted 1 line
The idea of the scheduler being responsible to track what's in flight on the HW came much later and to be honest is actually not a functionality the scheduler actually needs.
As far as I understand GPU driver design so far it's unavoidable to have at the very least a list of finished fences in the scheduler, so you can signal them once the hardware fences signal.
And at the very least once you have to do resubmissions you need to remember which jobs / fences are in flight.
Yes.
Quoted 3 lines
Unless it were possible to use the
hardware_fencefor everything.
Disconnecting the hardware fence from the scheduler fence is actually a very good idea. If your hardware submission spans multiple resources, each with its own seqno, the hardware fence becomes a composite fence (either a chain or an array). You can’t install those in dma-resv or use chains in syncobjs. In Xe, this use case is called parallel submission to multiple engines; I believe AMD has something similar called gangs.
If we didn't disconnect the scheduler fence from the hardware fence, Xe would break—or we’d have to implement a convoluted reinvention of a chain or array for the hardware fence.
Quoted 72 lines
Quoted 72 lines
Quoted 72 lines
Jobqueue could do resubmits through help with that list, by running the desired jobs again. While
run_job()executes, jobs are loaned to the driver, who only needs them temporarily, not permanently.One could have that loaning in C, too, but would not be able to enforce it.
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 4 lines
Maybe the core of the problem is not so much the lack of refcounting, but the lack of ownership rules. Why even would the driver need the job still after it got pushed? It should be fire-and-forget.
Yeah, that sounds sane to me as well and is exactly how it was initially designed in the
drm_scheduleras well.The job is basically just the information the driver needs for the submission which it gives to the scheduler on push, and the scheduler gives back to the driver on pop.
The full execution time is represented by the scheduler fence and not the job. And the scheduler fence is reference counted exactly because of the reasons Mathew brought up here.
Quoted 8 lines
Quoted 4 lines
Would be interesting to hear where Xe would still need the job. If only the
backend_opsgive a driver access to a job again after it got pushed, then it should be safe.Xe needs a subset of the job after submission to handle tasks like resubmission after a device reset.
the job or the jobS?
Because you get the job that caused the timeout by the scheduler, through
timedout_job().And the rest you need will soonish be obtainable through the new iterator. So what else do you need?
Quoted 9 lines
It’s questionable whether we need
this, as it shouldn’t happen in practice—only individual queues should fail with a working KMD and hardware. It likely doesn’t work anyway if queues have interdependencies. This is really an opportunistic approach.However, we absolutely need this for VF migration resubmission. Again, this requires only a very small subset of driver job information. I believe it’s just the starting point in the ring, batch address(es), and a pointer to the driver-side queue object.
In Rust, I borrow the job to the driver. So if it really needs something about it permanently, it can copy it into some object with a decoupled life-time.
Or maybe have the job-struct's generic data-field contain something refcounted, IDK.Quoted 15 lines
We also build a reference-counting model around jobs, where the final put releases other objects and runtime power management references. This assumes that the job’s final put means the scheduler fence is signaled. Again, this is really just a small subset of information we need here.
So if we add hooks to store the subset of information Xe needs for everything above in the scheduler fence and a non-IRQ, pausable callback (i.e., won’t execute when the scheduler is stopped, like
free_job), this could be made to work. We really don’t need about 90% of the information in the job and certainly nothing in the base object.This would be major surgery, though. I suspect most drivers have a subset of information in the job that needs to stick around until it signals, so this means surgery across 11 drivers.
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Not sure if that's worth it. My hope would more be that interested users with firmware scheduling can switch to jobqueue and start over with a fresh, clean design with proper memory life times.
That sounds reasonable to me. I was more than once at the point of wanting to nuke the scheduler and starting from scratch again.
I think we can unwind drm sched.
Quoted 8 lines
Actually, since the 1950s, you don't want to rewrite software at all anymore. Yet we again and again end up at that point.
Only works if you *really* get it right. Even with jobqueue that's already becoming more complicated, because we might have to support resubmits, which I hoped we could 100% avoid.
You probably need resubmission for VF migration cases. For resets, we could likely avoid resubmission support, assuming all devices provide proper process isolation.
Quoted 51 lines
drm_sched's primary design mistake is having two job lists with different life times AND on top of that resubmits.
Quoted 48 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
I'm absolutely not against reference counting, what I'm pushing back is abusing the job object as something it was never designed for while we already have an object which implements exactly the needed functionality.
Well, oops. Having
free_jobcalled after the fence is signaled is how I arrived at the implementation in Xe. I agree that if we can move driver info into the scheduler fence, this could work for likely everyone.Ok in that case I'm going to give that a try.
Quoted 40 lines
Quoted 39 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
And my uneducated guess is that it's happening in amdgpu. It seems a
sched_joblives inside anamdgpu_job. Can the latter be freed at other places thanfree_job()?Quoted 1 line
[…]
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
It basically says to the driver that the job lifetime problems created by the scheduler is the driver problem and need to be worked around there.
My POV still mostly is that (with the current design) the driver must not use jobs after
free_job()was invoked. And when that happens is unpredictable.Quoted 5 lines
This is somewhat of an absurd statement from my point of view. I have a valid job pointer, then I call another function (see above for an example of how drm_sched_start/stop is unsafe) and it disappears behind my back.
The statement is absurd because reality (the code) is absurd. We all are basically Alice in Wonderland, running as fast as we can just to remain on the same spot ^_^
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
What I am stating is not that this is *good*, but this is what it currently is like. Whether we like it or not.
The misunderstanding you and I might have is that for me jobs having to be refcounted is not a reality until it's reflected in code, documentation and, ideally, drivers.
I agree this part is badly misdesigned. In the timedout job callback, you’re handed a job, and if you perform certain actions, it can just disappear— even all the way back to the caller of
timedout_job. That’s not great. Then we have thisfree_guiltymechanism to avoid it disappearing, but sometimes it still does, which is horrible.
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
Who makes it disappear, the driver callback? Because that
free_guiltymechanism is what frees jobs in the first place.I think so yes.
The driver must not free the job in
timedout_job. That's completely against the design, be it bad or good.free_job()is the point where you can free. That's why it's called that.
drm_sched_start/stop, which is called during AMD’s (and possibly others’) TDR, can also free jobs. Having a single point—the work item that calls free_job—makes everything much safer. So just signal the fences in the TDR after kicking the job off the hardware and let the known work item handle cleanup. Not to harp on this, but this is what Xe does.
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines
The more you think about it, the more astonished you become how this could ever have been designed and merged that way. There was no clean design anywhere, neither with APIs, nor life times, nor locking.
Yeah, which is exactly the reason why I said I'm not going to maintain that stuff.
I mean I still feel guilty that it came this far, but yeah.
I played a part too, but let me give you a different perspective. When I worked on submission in i915, getting anything functional in the existing code was nearly impossible. For about a year, all I did was fight bugs after implementing the GuC backend—and I’m fairly certain it still doesn’t work in all cases. In Xe, my initial implementation on top of drm_sched has more or less just worked. Yes, I’m using a subset of drm_sched, but the concepts—avoiding submission in IRQ contexts, using work queues for ordering, disconnecting the scheduler fence from the hardware fence, and being able to stop/start—are all very good ideas. We need some cleanups, but all is not lost.
Quoted 5 lines
You've said so frequently, don't worry too much. I just bring it up out of surprise because, you know, some design mistakes are unavoidable. It's just very difficult, GPU resubmits, hangs and all that. But other things and certain hacks (
drm_sched_fini()) are so *obviously* broken that you wonder..
Yes, see above. A subset works quite well, but there are a lot of scary things in drm_sched.
Quoted 141 lines
P.
Quoted 137 lines
Quoted 19 lines
Quoted 13 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
The safe way to handle this is to take a local reference before doing anything that could make it disappear. That is much more reasonable than saying, “we have five things you can do in the scheduler, and if you do any of them it isn’t safe to touch the job afterward.”
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Yeah, but that's
drm_schedbeingdrm_scheddy. Before I documented it there were also these implicit refcounting rules inrun_job(), where the driver needs to take the reference for the scheduler for it to be non-racy.Quoted 5 lines
Yes, agreed. This is my fault for not being more responsible in fixing issues rather than just backing away from these really scary parts of the code (e.g.,
drm_sched_stop,drm_sched_start,drm_sched_resubmit_jobs, etc.) and doing something sane in Xe by using only a subset of the scheduler.Quoted 6 lines
It's a bit like writing C++: no one can ever agree which feature subset is safe to use.
That's why we want to do a fresh restart for firmware-schedulers, since they allow us to drastically simplify things. Timeout? Close the ring. Job-pushing is fire and forget. Resets? Rings aren't shared.
+1
Quoted 99 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
It also wasn't documented for a long time that
drm_sched(throughspsc_queue) will explode if you don't use entities with a single producer thread.That is actually documented, but not on the scheduler but rather the
dma_fence.spsc - "Single producer, Single consumer". So it is in the name.
Ah, NTOTM.
What's obvious for one party is a mystery to someone with a different mind. I recognized the meaning after months, after work one day.
But don't get me started on that queue……
Quoted 83 lines
Quoted 78 lines
Quoted 2 lines
And that you can only have a single producer is a requirement inherited from the
dma_fenceand not scheduler specific at all.What does
dma_fencehave to do with it? It's about thespsc_queuebeing racy like mad. You can access and modify dma_fence's in parallel however you want – they are refcounted and locked.P.
Quoted 68 lines
Quoted 58 lines
drm_schedgot here because of gross design mistakes, lots of hacks for few drivers, and, particularly, a strange aversion¹ against writing documentation. If Xe came, back in the day, to the conclusion that job lifetimes are fundamentally broken and that the objectively correct way to solve this is refcounting, then why wasn't that pushed intodrm_schedback then?Quoted 42 lines
Quoted 14 lines
Quoted 14 lines
To be unfair, we already have strange refcount expectations already. But I sort of agree that there is no objectively good solution in sight.
Quoted 9 lines
Quoted 9 lines
Let me respin to my documentation series and upstream that soonish, than we can build on top of that.
P.
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
2. Move reference counting to the base DRM scheduler job object, provide a
vfunc for the final job put, and document usage. Medium invasive.I strongly think that reference counting the job object just because the scheduler needs it is a bad idea.
With that we are just moving the hot potato from one side of our mouth to the other without really solving the issue.
See above—I can’t say I agree with this assessment. I think the lack of reference counting is exactly the problem. I don’t really understand the pushback on a very well-understood concept (reference counting) in Linux. I would sign up to fix the entire subsystem if we go this route.
Quoted 8 lines
Quoted 8 lines
Quoted 1 line
If a driver like XE needs that for some reason than that is perfectly fine.
Nouveau doesn't need it either.
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
3. Move job (driver) side tracking to the scheduler fence and let it
control the lifetime. Very invasive.Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Thinking about it more that is actually not so much of a problem.
Let me try to code something together by the end of next week or so.
The hero Gotham needs :)
Quoted 6 lines
Are you sure about this one? I think unless the problems around drm_sched_start/stop and
free_guiltyare fixed, my feeling is this entire thing is still badly broken for anyone who wants to use those.To sum up this whole email: I strongly disagree with option #3, but if that is the consensus, I will, of course, support the effort.
Quoted 7 lines
I would like to discuss those topics with Danilo, too, who returns from LPC soonish. Also to get some more insights into Nouveau and our use- cases.
My suggestion is that we pick the conversation up again soonish. Christmas is around the corner, and I suppose we can't fix this all up in 2025 anyways, so we might want to give it a fresh re-spin in '26.
Since we finally found the root cause I'm all in postponing that till next year.
Ok, glad we found the root cause. I’m not as opposed to option #3 as stated—this was a bit of angry typing—but if we go in that direction, we really need clear rules, for example:
Quoted 8 lines
Quoted 2 lines
- A job cannot be referenced driver-side after the initial
drm_sched_entity_push_jobcall, aside from a singlerun_jobcallback.That's what the current code and documentation demand, yes.
Quoted 1 line
Maybe
run_jobis actually replaced by a scheduler fence input?fence input?
ops->schedule(job)?
ops->schedule(fence) ?
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
- Drivers can attach information to the scheduler fence and control its lifetime.
- Drivers can iterate over pending scheduler fences when the scheduler is stopped.That sounds as if we're about to make a mistake with the job-iterator.
No. See resubmission. Something this is required.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
- Drivers receive a pausable callback in a non-IRQ context when the scheduler fence signals.
etc...
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Again, this is a pretty major change. I personally wouldn’t feel comfortable hacking 11 drivers—10 of which aren’t mine—to do something like this. Refcounting the job would be less invasive and would make the existing hairball of code safe.
See my firmware-scheduler comment above. The issue is that getting systems with lax rules back under control in hindsight is 10x as expensive as carefully designing strict rules from the get-go.
Getting things wrong and learning from it is, unfortunately, part of the process.
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
My experience so far is that a maintainer's primary job is actually keeping APIs consistent and forcing people to document everything properly.
I would rather say that a maintainers job is to reject bad ideas and push for good ones.
+1
Matt
Quoted 6 lines
Quoted 5 lines
Christian.
Quoted 3 lines
P.