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.

>  {
> @@ -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.

> +                     } 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?



P.

Reply via email to