Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] drm/sched: unbalanced work disable depth in wqueue stop and
  start
- [High] drm/sched: deadlock on pause_lock and work_tdr in
  drm_sched_fini()
- [Medium] drm/sched: lockdep inversion acquiring pause_lock in
  drm_sched_wqueue_start()
- [Low] drm/sched: missing kernel-doc entry for pause_lock in
  drm_gpu_scheduler
--

commit a76a12f06c945a6e5b2e2b77f19934547861ccb1
Author: Nicolas Frattaroli <[email protected]>

drm/sched: Sanitise wqueue_start/wqueue_stop/is_stopped interleavings

This commit serializes GPU scheduler workqueue start and stop operations with
a mutex, switches from cancel_work_sync() to disable_work_sync() /
enable_work(), and orders pause_submit accesses with release/acquire barriers.

> diff --git a/drivers/gpu/drm/scheduler/sched_main.c 
> b/drivers/gpu/drm/scheduler/sched_main.c
> index 99252674e2668..4662f024ad885 100644
> --- a/drivers/gpu/drm/scheduler/sched_main.c
> +++ b/drivers/gpu/drm/scheduler/sched_main.c
[ ... ]
> @@ -1190,6 +1189,20 @@ static void drm_sched_cancel_remaining_jobs(struct 
> drm_gpu_scheduler *sched)
>       }
>  }
>  
> +static void drm_sched_wqueue_stop_locked(struct drm_gpu_scheduler *sched)
> +{
> +     lockdep_assert_held(&sched->pause_lock);
> +
> +     disable_work_sync(&sched->work_run_job);
> +     disable_work_sync(&sched->work_free_job);
> +
> +     /*
> +      * Anything that observes pause_submit will also want to see the effects
> +      * of any of the previous memory operations.
> +      */
> +     smp_store_release(&sched->pause_submit, true);
> +}

[Severity: High]
Can unbalanced drm_sched_wqueue_stop() and drm_sched_wqueue_start() calls leave
work_run_job and work_free_job permanently disabled or underflow their disable
count?

Previously, drm_sched_wqueue_stop() and drm_sched_wqueue_start() were
idempotent, whereas disable_work_sync() and enable_work() increment and
decrement a reference-counted disable depth on each work item. Neither
drm_sched_wqueue_stop_locked() nor drm_sched_wqueue_start() checks
sched->pause_submit before changing the disable depth.

Several drivers do not pair stop and start 1:1. For example,
amdgpu_job_timedout() calls drm_sched_wqueue_stop(&ring->sched) before
attempting a per-ring reset; if amdgpu_ring_reset() fails and falls through to
amdgpu_device_gpu_recover() -> amdgpu_device_halt_activities(),
drm_sched_wqueue_stop(&ring->sched) is called a second time while
amdgpu_device_sched_resume() only calls drm_sched_wqueue_start(&ring->sched)
once. That leaves the work disable depth at 1 while pause_submit is set back
to false, wedging the scheduler ring. Conversely, if amdgpu_ring_reset()
succeeds via amdgpu_sdma_reset_engine(), the helper already restarts
ring->sched before amdgpu_job_timedout() calls drm_sched_wqueue_start() a
second time, triggering the disable count underflow warning in
work_offqd_enable().

Guarding disable_work_sync() and enable_work() on sched->pause_submit under
sched->pause_lock would keep stop and start idempotent.

>  /**
>   * drm_sched_fini - Destroy a gpu scheduler
>   *
> @@ -1212,13 +1225,15 @@ void drm_sched_fini(struct drm_gpu_scheduler *sched)
>  {
>       int i;
>  
> -     drm_sched_wqueue_stop(sched);
> +     /* Guarantee queue is paused for the entirety of the teardown */
> +     guard(mutex)(&sched->pause_lock);
> +     drm_sched_wqueue_stop_locked(sched);
>  
>       /* Wakeup everyone stuck in drm_sched_entity_flush for this scheduler */
>       wake_up_all(&sched->job_scheduled);
>  
>       /* Confirm no work left behind accessing device structures */
> -     cancel_delayed_work_sync(&sched->work_tdr);
> +     disable_delayed_work_sync(&sched->work_tdr);

[Severity: High]
Does holding sched->pause_lock across 
disable_delayed_work_sync(&sched->work_tdr)
cause an ABBA deadlock when a timeout work item is running?

drm_sched_job_timedout() runs on sched->work_tdr and invokes
sched->ops->timedout_job(), where drivers call drm_sched_stop() /
drm_sched_wqueue_stop() and drm_sched_start() / drm_sched_wqueue_start(). Both
now take sched->pause_lock:

CPU0                              CPU1 (work_tdr)
----                              ---------------
drm_sched_fini()                  drm_sched_job_timedout()
  mutex_lock(&sched->pause_lock)    ops->timedout_job()
  ...                                 drm_sched_stop()
  disable_delayed_work_sync(            drm_sched_wqueue_stop()
    &sched->work_tdr)                     mutex_lock(&sched->pause_lock)

 *** DEADLOCK ***

[ ... ]
> @@ -1320,9 +1335,19 @@ EXPORT_SYMBOL(drm_sched_wqueue_stop);
>   */
>  void drm_sched_wqueue_start(struct drm_gpu_scheduler *sched)
>  {
> -     WRITE_ONCE(sched->pause_submit, false);
> +     guard(mutex)(&sched->pause_lock);
> +
> +     enable_work(&sched->work_run_job);
> +     enable_work(&sched->work_free_job);
> +
>       queue_work(sched->submit_wq, &sched->work_run_job);
>       queue_work(sched->submit_wq, &sched->work_free_job);
> +
> +     /*
> +      * Anything that observes pause_submit will also want to see the effects
> +      * of any of the previous memory operations.
> +      */
> +     smp_store_release(&sched->pause_submit, false);
>  }

[Severity: Medium]
Does taking sched->pause_lock in drm_sched_wqueue_start() introduce lockdep
inversions with dma_fence signalling and with work items running on
sched->submit_wq?

In drm_sched_wqueue_stop_locked() and drm_sched_fini(), sched->pause_lock is
held across disable_work_sync(&sched->work_run_job),
disable_work_sync(&sched->work_free_job), drm_sched_cancel_remaining_jobs(),
and destroy_workqueue(sched->submit_wq), chaining sched->pause_lock above the
submit_wq lockdep map and the free_job() / fs_reclaim paths.

In panfrost_reset(), drm_sched_start() -> drm_sched_wqueue_start() is called
inside a dma_fence_begin_signalling() critical section, which chains
dma_fence_signalling -> sched->pause_lock -> work_free_job / fs_reclaim. In
xe's disable_scheduling_deregister(), xe_sched_submission_start() ->
drm_sched_wqueue_start() runs from xe_sched_process_msg_work() on
sched->submit_wq, inverting the sched->pause_lock -> submit_wq order and
risking deadlock if a concurrent drm_sched_wqueue_stop() holds
sched->pause_lock while waiting for work queued behind work_process_msg on the
same ordered submit_wq.

> diff --git a/include/drm/gpu_scheduler.h b/include/drm/gpu_scheduler.h
> index 3b0978fa8cf08..259960fd584ae 100644
> --- a/include/drm/gpu_scheduler.h
> +++ b/include/drm/gpu_scheduler.h
> @@ -627,6 +627,7 @@ struct drm_gpu_scheduler {
>       bool                            free_guilty;
>       bool                            pause_submit;
>       bool                            own_submit_wq;
> +     struct mutex                    pause_lock;
>       struct device                   *dev;
>  };

[Severity: Low]
struct drm_gpu_scheduler has a kernel-doc comment block describing all of its
members, so adding pause_lock without a matching @pause_lock entry will
trigger a kernel-doc warning.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=1

Reply via email to