On 09/11/2026 12:46, Philipp Stanner wrote:
On Fri, 2026-11-06 at 14:57 +0000, Tvrtko Ursulin wrote:
-/*
- * This is a duplicate function from DRM scheduler sched_internal.h.
- * Plan is to remove it when amdgpu_job_stop_all_jobs_on_sched is removed, due
- * latter being incorrect and racy.
- *
- * See
https://lore.kernel.org/amd-gfx/[email protected]/
- */
static struct drm_sched_job *
drm_sched_entity_queue_pop(struct drm_sched_entity *entity)
Why remove that comment? A reader will still wonder why a drm_sched
function is defined here.
It effectively moved down in the diff.
{
@@ -476,28 +469,66 @@ drm_sched_entity_queue_pop(struct drm_sched_entity
*entity)
return container_of(node, struct drm_sched_job, queue_node);
}
-void amdgpu_job_stop_all_jobs_on_sched(struct drm_gpu_scheduler *sched)
+static struct drm_sched_entity *
+get_entity_with_jobs(struct drm_gpu_scheduler *sched) __acquires(&entity->lock)
[…]
+retry:
for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) {
struct drm_sched_rq *rq = sched->sched_rq[i];
+
spin_lock(&rq->lock);
- list_for_each_entry(s_entity, &rq->entities, list) {
- while ((s_job = drm_sched_entity_queue_pop(s_entity))) {
- struct drm_sched_fence *s_fence =
s_job->s_fence;
+ list_for_each_entry(entity, &rq->entities, list) {
+ /*
+ * This can false negative, but so could the previous
+ * approach of scanning the run-queues once when
+ * nothing prevents userspace to queue more jobs.
+ */
+ if (!spsc_queue_count(&entity->job_queue))
+ continue;
- dma_fence_signal(&s_fence->scheduled);
- dma_fence_set_error(&s_fence->finished,
-EHWPOISON);
- dma_fence_signal(&s_fence->finished);
+ /*
+ * We must trylock because normal locking order is
+ * entity then rq and here we already have rq locked
+ * in order to be able to walk the entities.
+ */
+ if (spin_trylock(&entity->lock)) {
+ /* Returns with a locked entity. */
+ spin_unlock(&rq->lock);
+ return entity;
Ah, now I understand.
Hmm. I guess considering the circumstances we're in that's the best-ish
option we have?
I hope the AMD folks can't see technical blockers.
Yeah I think it's the best before AMD follows up with the promised
complete rework of how this is handled.
I am hoping this hacky rework, assuming it is no more broken by the
original, and that AMD can test it (I don't have the required hardware),
can be a motivator. :)
+ } else {
+ /*
+ * Busy entity - we will try again looking for a
+ * window where trylock will succeed.
+ */
+ spin_unlock(&rq->lock);
+ goto retry;
In classic anti-deadlock strategies, wouldn't you want some random
delay here to avoid cycling?
I did not see a reason to not make this thread the most greedy it can
be. Because the longer it runs the more potential for livelock has.
Same is true for the existing solution to be clear, but this patch does
increase the window a bit by adding trylock and restart. I think. Or
perhaps it is the same potential because all that is required in the
current code is for an entity to keep submitting and
amdgpu_job_stop_all_jobs_on_sched() never exits the pop loop. With this
patch any entity doing a submit can trigger a re-scan of all entities,
so any submitting anything new will be handled, until the time this
loops needs to finish the re-scan is lower than the submit frequency.
Hence I thought the faster it goes the better.
Regards,
Tvrtko