Thank you for your contribution! Sashiko AI review found 10 potential
issues to consider:
- [High] accel/thames: stale fault_records read in thames_core_idles()
  and timeout
- [High] accel/thames: race between thames_core_subdev_stop() and
  timeout recovery
- [High] accel/thames: race between thames_job_run() and
  thames_core_subdev_stop()
- [High] accel/thames: false completion_stuck on seqno 0 in
  thames_job_timedout()
- [High] accel/thames: race in thames_job_complete() re-arming TDR
  during suspend/drain
- [Medium] accel/thames: missing ring check and table rewrites in
  thames_core_reset()
- [Medium] accel/thames: lost immediate fault recovery in
  thames_job_timedout()
- [Medium] accel/thames: premature release of bad job before reset in
  thames_job_recover()
- [Medium] accel/thames: missing completion_stuck check in
  thames_job_cancel()/complete()
- [Low] accel/thames: lost error code on resubmission failure in
  thames_job_recover()
--

commit 868a9169441152079b6e90fccf0327d93f948e02
Author: Tomeu Vizoso <[email protected]>

accel/thames: Recover from faults and timeouts

This commit adds fault record queries, context banning after repeated job
faults, and timeout and firmware fault recovery via remoteproc reset.

> diff --git a/drivers/accel/thames/thames_core.c 
> b/drivers/accel/thames/thames_core.c
> index 73350784979ec..512de3d880d01 100644
> --- a/drivers/accel/thames/thames_core.c
> +++ b/drivers/accel/thames/thames_core.c
[ ... ]
> @@ -430,6 +433,19 @@ static void thames_core_subdev_stop(struct rproc_subdev 
> *subdev, bool crashed)
>               dev_warn(core->dev, THAMES_CORE_FMT
>                        " is stopped through remoteproc; it is offline until 
> remoteproc starts it again\n",
>                        THAMES_CORE_ARG(core));
> +
> +     /* Its fence lock may be held by a thread that never returns. */
> +     if (core->completion_stuck)
> +             return;
> +
> +     if (!crashed &&
> +         read_poll_timeout(thames_core_idles, idle, idle, USEC_PER_MSEC,
> +                           THAMES_IDLE_TIMEOUT_MS * USEC_PER_MSEC, false, 
> core))
> +             dev_crit(core->dev, THAMES_CORE_FMT " is not known to idle; 
> remoteproc stops it all the same\n",
> +                      THAMES_CORE_ARG(core));
> +
> +     thames_job_complete(core);
> +     thames_job_fail_submitted(core);
>  }

[Severity: High]
Can thames_core_subdev_stop() race with thames_job_timedout() and
thames_job_recover() on tdev->reset_wq?

Unlike thames_job_suspend() and thames_job_drain(),
thames_core_subdev_stop() does not stop the scheduler workqueues or
cancel core->sched.work_tdr.  In thames_job_timedout(), core->job_lock is
released after looking up oldest and before calling
thames_job_recover(core, sched_job, oldest, &record):

CPU0 (remoteproc stop)            CPU1 (reset_wq)
thames_core_subdev_stop()         thames_job_timedout()
                                    lock(&core->job_lock)
                                    oldest = list_first_entry_or_null(...)
                                    unlock(&core->job_lock)
  thames_job_fail_submitted()
    lock(&core->job_lock)
    list_splice_init(&core->submitted,
                     &lost)
    unlock(&core->job_lock)
    list_for_each_entry(job, &lost,
                        node)
                                    thames_job_recover(..., oldest, ...)
                                      lock(&core->job_lock)
                                      list_del_init(&bad->node)
                                        <-- corrupts CPU0's unlocked
                                            lost list traversal
                                      unlock(&core->job_lock)
      thames_job_fail_done_fence()
                                      dma_fence_set_error(bad->done_fence,
                                                          -EIO)
                                        <-- WARN_ON on already-signaled
                                            fence
                                      thames_sched_job_put(bad)
                                        <-- double put / use-after-free

Even without a concurrent remoteproc stop, if thames_mbox_rx() ->
thames_job_complete() completes the job after thames_job_timedout() drops
core->job_lock, thames_job_timedout() still takes the healthy core
offline.

[ ... ]

> @@ -649,11 +675,50 @@ bool thames_core_idles(struct thames_core *core)
>       return READ_ONCE(record->kind) == DRM_THAMES_FAULT_FIRMWARE;
>  }
>  
> +/* Runs on reset_wq, the ordered queue the timeout handler runs on. */
> +int thames_core_reset(struct thames_core *core)
> +{
> +     struct thames_caps caps = {};
> +     struct rproc *rproc = core->rproc;
> +     bool idle;
> +     int err;
> +
> +     err = read_poll_timeout(thames_core_idles, idle, idle, USEC_PER_MSEC,
> +                             THAMES_IDLE_TIMEOUT_MS * USEC_PER_MSEC, false, 
> core);
> +     if (err) {
> +             dev_crit(core->dev, THAMES_CORE_FMT " is not known to idle; not 
> stopping it\n",
> +                      THAMES_CORE_ARG(core));
> +             return -EBUSY;
> +     }
> +
> +     dev_warn(core->dev, "Resetting " THAMES_CORE_FMT "\n", 
> THAMES_CORE_ARG(core));
> +
> +     err = thames_core_rproc_shutdown(core);
> +     if (!err && rproc->state != RPROC_OFFLINE) {
> +             dev_err(core->dev, THAMES_CORE_FMT " is kept running by another 
> power count; not resetting it\n",
> +                     THAMES_CORE_ARG(core));
> +             thames_core_rproc_boot(core);
> +             return -EBUSY;
> +     }
> +     if (err)
> +             return err;
> +
> +     err = thames_core_rproc_boot(core);
> +     if (err)
> +             return err;
> +
> +     err = thames_core_wait_handshake(core);
> +     if (err)
> +             return err;
> +
> +     return thames_core_read_handshake(core, &caps);
> +}

[Severity: Medium]
Should thames_core_reset() verify that the ring is empty, update
core->ring_tail, and rewrite the context and transfer tables after
reading the handshake?

Because thames_core_rproc_boot() sets core->rproc_caller = current,
thames_core_subdev_start() returns early without calling
thames_core_attach().  Unlike thames_core_attach() and
thames_core_resume(), thames_core_reset() does not check that head ==
tail, does not set core->ring_tail = head, does not call
thames_context_rewrite_entries(core) (which bumps mapping_generation so
the restarted firmware invalidates its TLB) or
thames_transfers_rewrite(core), and does not reset
core->unfinished_seqno = 0.

> diff --git a/drivers/accel/thames/thames_job.c 
> b/drivers/accel/thames/thames_job.c
> index ee0a883065f97..33c9063782acd 100644
> --- a/drivers/accel/thames/thames_job.c
> +++ b/drivers/accel/thames/thames_job.c
[ ... ]
> +/*
> + * The firmware faulted with its Ring empty: it writes the record at its 
> head's
> + * slot under that slot's stale seqno and takes no doorbell after.
> + */
> +static bool thames_job_read_empty_ring_fault(struct thames_core *core, 
> struct thames_sched_job *job,
> +                                          struct thames_fault_record *record)
> +{
> +     u32 head = READ_ONCE(core->ipc->ring.head);
> +
> +     if (head % THAMES_RING_DEPTH != job->ring_index)
> +             return false;
> +
> +     /* Read the fault record after the head that says it is there. */
> +     rmb();
> +     memcpy(record, &core->ipc->fault_records[job->ring_index], 
> sizeof(*record));
> +
> +     return record->kind == DRM_THAMES_FAULT_FIRMWARE;
> +}

[Severity: High]
Can stale DRM_THAMES_FAULT_FIRMWARE records in core->ipc->fault_records[]
cause thames_core_idles() and thames_job_read_empty_ring_fault() to
misclassify later jobs when the ring wraps around?

Nothing clears core->ipc->fault_records[slot].kind after a fault record is
consumed or before a new job is written to that slot, and neither
thames_core_idles() nor thames_job_read_empty_ring_fault() checks
record->seqno:

drivers/accel/thames/thames_core.c:thames_core_idles() {
        u32 head = READ_ONCE(core->ipc->ring.head);
        u32 slot = head % THAMES_RING_DEPTH;
        struct thames_fault_record *record = &core->ipc->fault_records[slot];

        if (head == core->ring_tail)
                return true;

        /* Read the fault record after the head that says it is there. */
        rmb();
        return READ_ONCE(record->kind) == DRM_THAMES_FAULT_FIRMWARE;
}

Once a slot holds a DRM_THAMES_FAULT_FIRMWARE record, when the 64-entry
ring wraps back to that slot, thames_core_idles() returns true while the
core is actively executing the new job (causing thames_core_subdev_stop()
to stop waiting early or thames_job_suspend() to fail system suspend with
-EBUSY).  If a later job at that slot hangs without writing a fault
record, thames_job_read_fault() rejects the stale seqno and
thames_job_timedout() falls through to thames_job_read_empty_ring_fault(),
which matches the old DRM_THAMES_FAULT_FIRMWARE record and calls
thames_job_recover(core, sched_job, NULL, &record), resetting the core and
re-running the hung job without blaming it or counting it towards the
context ban.

Clearing core->ipc->fault_records[index].kind in thames_job_hw_submit()
and after consuming an empty-ring fault in thames_job_recover() would
prevent stale matches.

[ ... ]

> @@ -183,6 +314,19 @@ void thames_job_complete(struct thames_core *core)
>               trace_thames_job_done(core, job, signalled);
>               thames_sched_job_put(job);
>       }
> +
> +     /*
> +      * The firmware's own fault moves neither the Fence nor the head: it
> +      * rings the doorbell and parks in IDLE.
> +      */
> +     scoped_guard(mutex, &core->job_lock) {
> +             job = list_first_entry_or_null(&core->submitted, struct 
> thames_sched_job, node);
> +             if (job && !thames_core_is_dead(core) && 
> thames_job_read_fault(core, job, &record))
> +                     firmware_fault = record.kind == 
> DRM_THAMES_FAULT_FIRMWARE;
> +     }
> +
> +     if (firmware_fault)
> +             drm_sched_fault(&core->sched);
>  }

[Severity: High]
Can thames_job_complete() re-arm sched->work_tdr via drm_sched_fault()
after thames_job_suspend() or thames_job_drain() has already cancelled it?

Both thames_job_suspend() and thames_job_drain() stop the scheduler
workqueues and call cancel_delayed_work_sync(&sched->work_tdr) before
waiting for pending jobs on core->submitted.  During that wait,
thames_job_complete() runs (from thames_mbox_rx(), at the end of
thames_job_suspend(), or in every poll of thames_job_ring_drained()) and
calls drm_sched_fault(&core->sched) if the head job faulted with
DRM_THAMES_FAULT_FIRMWARE:

CPU0 (unbind / thames_core_fini)  CPU1 (reset_wq)
thames_job_drain()
  drm_sched_wqueue_stop(sched)
  cancel_delayed_work_sync(&work_tdr)
  read_poll_timeout(thames_job_ring_drained, ...)
    thames_job_complete()
      drm_sched_fault()
                                  thames_job_timedout()
                                    thames_job_recover()
                                      list_del_init(&bad->node)
                                      list_splice_init(&core->submitted,
                                                       &lost)
    list_empty(&core->submitted)
      --> true, poll exits
  cancel_delayed_work_sync(&work_tdr)
                                      thames_core_reset()
                                      thames_job_run()
                                        <-- resubmits pending jobs back
                                            onto core->submitted
  drm_sched_wqueue_stop(sched)
  returns to thames_core_fini()   <-- resubmitted jobs still active on
                                      the hardware Ring

In thames_job_suspend(), the newly queued work_tdr is never cancelled at
all, so thames_job_recover() can run concurrently with
thames_core_rproc_shutdown() or with thames_job_resume() restarting
submit_wq while thames_job_recover() walks sched->pending_list unlocked.

[ ... ]

> @@ -272,6 +422,12 @@ static struct dma_fence *thames_job_run(struct 
> drm_sched_job *sched_job)
>       if (unlikely(job->base.s_fence->finished.error))
>               return NULL;
>  
> +     if (thames_core_is_dead(core))
> +             return ERR_PTR(-ENODEV);
> +
> +     if (thames_context_banned(job->file_priv))
> +             return ERR_PTR(-ECANCELED);
> +
>       /* The done fence keeps the seqno of the Job's first run. */
>       job->seqno = ++core->emit_seqno;
>       if (!dma_fence_was_initialized(job->done_fence))

[Severity: High]
Can thames_job_run() push a job onto core->submitted and advance
ring->tail after thames_core_subdev_stop() has already failed submitted
jobs?

thames_job_run() checks thames_core_is_dead(core) without holding
core->job_lock, and thames_core_subdev_stop() does not stop
core->sched.submit_wq:

CPU0 (submit_wq)                  CPU1 (remoteproc stop)
thames_job_run()
  if (thames_core_is_dead(core))
    --> false
                                  thames_core_subdev_stop()
                                    thames_core_test_and_set_offline(core)
                                    thames_job_complete(core)
                                    thames_job_fail_submitted(core)
  thames_job_hw_submit()
    lock(&core->job_lock)
    list_add_tail(&sched_job->node,
                  &core->submitted)
    WRITE_ONCE(ring->tail, tail + 1)
      <-- job stranded on stopped core

The job is left on core->submitted with an unsignaled done_fence on an
offline core, and when remoteproc starts the core again, either
thames_core_attach() fails with -EIO (seeing head != tail) or the
stranded job's 2000 ms timeout fires and takes the newly started core
offline.

[ ... ]

> +static void thames_job_recover(struct thames_core *core, struct 
> drm_sched_job *sched_job,
> +                            struct thames_sched_job *bad,
> +                            const struct thames_fault_record *record)
> +{
> +     struct drm_gpu_scheduler *sched = &core->sched;
> +     struct thames_sched_job *job;
> +     struct drm_sched_job *s_job;
> +     unsigned int resubmitted = 0;
> +     LIST_HEAD(lost);
> +     int err;
> +
> +     drm_sched_wqueue_stop(sched);
> +
> +     /* Failed before drm_sched_stop(), so the stop finds it complete. */
> +     if (bad) {
> +             scoped_guard(mutex, &core->job_lock)
> +                     list_del_init(&bad->node);
> +
> +             dev_warn(core->dev, "Job seqno %llu of Context %u: the firmware 
> faulted\n",
> +                      bad->seqno, bad->file_priv->context_id);
> +             thames_job_set_fault(bad, record);
> +             thames_context_count_job(core, bad, true);
> +             dma_fence_set_error(bad->done_fence, -EIO);
> +             dma_fence_signal(bad->done_fence);
> +             thames_sched_job_put(bad);
> +     } else {
> +             dev_warn(core->dev, THAMES_CORE_FMT
> +                      ": the firmware faulted with its Ring empty (IERR 
> 0x%llx, NRP 0x%llx)\n",
> +                      THAMES_CORE_ARG(core), 
> record->registers[THAMES_FAULT_REG_IERR],
> +                      record->registers[THAMES_FAULT_REG_NRP]);
> +     }
> +
> +     drm_sched_stop(sched, sched_job);
> +
> +     scoped_guard(mutex, &core->job_lock)
> +             list_splice_init(&core->submitted, &lost);
> +
> +     /*
> +      * The Jobs on lost stay allocated until the reset has stopped the core,
> +      * which may still be running one of their Kernels.
> +      */
> +     err = thames_core_reset(core);
> +     if (err) {
> +             thames_core_test_and_set_offline(core);
> +             dev_crit(core->dev, THAMES_CORE_FMT
> +                      " could not be reset (%d); it is offline until it is 
> stopped and started again through remoteproc\n",
> +                      THAMES_CORE_ARG(core), err);
> +
> +             list_for_each_entry(job, &lost, node)
> +                     thames_job_fail_done_fence(job, -ENODEV);
> +
> +             scoped_guard(mutex, &core->job_lock)
> +                     list_splice_tail_init(&lost, &core->held);
> +
> +             drm_sched_start(sched, -ENODEV);
> +             return;
> +     }

[Severity: Medium]
If thames_core_reset() fails without stopping the core, does releasing
bad before the reset free bad's buffers and page tables while the core is
still powered on?

thames_job_recover() signals bad->done_fence and drops bad's
core->submitted reference with thames_sched_job_put(bad) before calling
drm_sched_stop() and thames_core_reset().  If thames_core_reset() fails
because thames_core_idles() times out, rproc_shutdown() fails, or another
power count keeps the remoteproc running, only the remaining jobs in lost
are moved to core->held, while bad is freed immediately when
drm_sched_job_timedout() finishes.  Conversely, if thames_core_reset()
fails in thames_core_rproc_boot() or handshake after
thames_core_rproc_shutdown() has already powered off the core, lost is
spliced onto core->held instead of being released.

> +
> +     scoped_guard(mutex, &core->job_lock)
> +             list_splice_tail_init(&lost, &core->held);
> +
> +     thames_job_release_held(core);
> +
> +     drm_sched_for_each_pending_job(s_job, sched, NULL) {
> +             struct dma_fence *fence = thames_job_run(s_job);
> +
> +             if (IS_ERR_OR_NULL(fence)) {
> +                     thames_job_fail_done_fence(to_thames_sched_job(s_job),
> +                                                fence ? PTR_ERR(fence) : 
> -ECANCELED);
> +                     continue;
> +             }
> +
> +             s_job->s_fence->parent = fence;
> +             resubmitted++;
> +     }
> +
> +     dev_info(core->dev, THAMES_CORE_FMT " restarted, %u Jobs resubmitted\n",
> +              THAMES_CORE_ARG(core), resubmitted);
> +
> +     drm_sched_start(sched, 0);
> +}

[Severity: Low]
Does a resubmission error in thames_job_recover() get overwritten with
-ECANCELED on s_job->s_fence->finished?

drm_sched_stop() detaches and clears s_job->s_fence->parent.  When
thames_job_run(s_job) returns an ERR_PTR (such as ERR_PTR(-ENODEV) or
ERR_PTR(-ENOSPC)), thames_job_fail_done_fence() sets the error on the
detached done_fence and leaves s_job->s_fence->parent NULL, so
drm_sched_start(sched, 0) calls drm_sched_job_done(s_job, -ECANCELED) and
signals the user-visible finished fence with -ECANCELED instead of
PTR_ERR(fence).

> +
> +/*
> + * Only job_lock: the core's fence lock may be held by a completion thread 
> that
> + * died.
> + */
> +static bool thames_job_in_completion(struct thames_core *core, struct 
> thames_sched_job *job)
> +{
> +     struct thames_sched_job *pending;
> +
> +     if (dma_fence_is_signaled(&job->base.s_fence->finished))
> +             return false;
> +
> +     guard(mutex)(&core->job_lock);
> +     list_for_each_entry(pending, &core->submitted, node) {
> +             if (pending == job)
> +                     return false;
> +     }
> +
> +     return true;
> +}
> +
> +static enum drm_gpu_sched_stat thames_job_timedout(struct drm_sched_job 
> *sched_job)
> +{
> +     struct thames_sched_job *job = to_thames_sched_job(sched_job);
> +     struct thames_sched_job *pending, *oldest = NULL;
> +     struct thames_core *core = container_of(sched_job->sched, struct 
> thames_core, sched);
> +     struct thames_fault_record record;
> +     LIST_HEAD(dead_jobs);
> +     bool found = false;
> +
> +     if (core->completion_stuck)
> +             return DRM_GPU_SCHED_STAT_ENODEV;
> +
> +     if (thames_job_in_completion(core, job)) {
> +             if (core->unfinished_seqno != job->seqno) {
> +                     core->unfinished_seqno = job->seqno;
> +                     return DRM_GPU_SCHED_STAT_NO_HANG;
> +             }
> +
> +             core->completion_stuck = true;
> +             thames_core_test_and_set_offline(core);
> +             dev_crit(core->dev, "Job seqno %llu on " THAMES_CORE_FMT
> +                      ": its completion never finished; taking the core 
> offline, a reboot is needed to use it again\n",
> +                      job->seqno, THAMES_CORE_ARG(core));
> +             return DRM_GPU_SCHED_STAT_ENODEV;
> +     }

[Severity: High]
Can thames_job_timedout() falsely trigger core->completion_stuck on the
first timeout if a job has not been added to core->submitted yet?

In drm_sched_run_job_work(), drm_sched_job_begin() puts sched_job on
sched->pending_list before calling thames_job_run().  In thames_job_run(),
job->seqno is 0 until ++core->emit_seqno, and remains 0 if
thames_job_run() returns early (for instance because the context was just
banned), while job is not added to core->submitted until
thames_job_hw_submit().  If work_tdr runs during that window,
thames_job_in_completion() returns true because s_fence->finished is not
yet signaled and job is not on core->submitted.  Since
core->unfinished_seqno is also initialized to 0,
core->unfinished_seqno != job->seqno (0 != 0) is false on the very first
pass, permanently setting core->completion_stuck = true, taking the core
offline until reboot, and returning DRM_GPU_SCHED_STAT_ENODEV (which
also leaks sched_job because drm_sched_job_timedout() neither reinserts
nor frees it on DRM_GPU_SCHED_STAT_ENODEV).

> +
> +     thames_job_complete(core);
> +
> +     scoped_guard(mutex, &core->job_lock) {
> +             oldest = list_first_entry_or_null(&core->submitted, struct 
> thames_sched_job, node);
> +             list_for_each_entry(pending, &core->submitted, node) {
> +                     if (pending == job) {
> +                             found = true;
> +                             break;
> +                     }
> +             }
> +     }
> +
> +     if (!found)
> +             return DRM_GPU_SCHED_STAT_NO_HANG;

[Severity: Medium]
Does returning DRM_GPU_SCHED_STAT_NO_HANG here delay firmware fault
recovery by 2000 ms when an earlier job completes in the same pass?

When job 1 completes and job 2 (now at the head of core->submitted) faults
with DRM_THAMES_FAULT_FIRMWARE, thames_job_complete() signals
job1->done_fence (which queues work_free_job on submit_wq, leaving job 1
at the head of sched->pending_list until work_free_job runs) and calls
drm_sched_fault(&core->sched) to schedule work_tdr with delay 0.  When
thames_job_timedout() runs with sched_job = &job1->base, job 1 has already
left core->submitted, so found is false and thames_job_timedout() returns
DRM_GPU_SCHED_STAT_NO_HANG without checking oldest (job 2) for
DRM_THAMES_FAULT_FIRMWARE.  drm_sched_job_timedout() then restarts
work_tdr with the full 2000 ms timeout, and because the firmware sends no
more doorbells after parking in IDLE, recovery stalls for 2 seconds.

[ ... ]

> @@ -308,6 +682,7 @@ static void thames_job_cancel(struct drm_sched_job 
> *sched_job)
>  
>  static const struct drm_sched_backend_ops thames_sched_ops = {
>       .run_job = thames_job_run,
> +     .timedout_job = thames_job_timedout,
>       .free_job = thames_sched_job_free,
>       .cancel_job = thames_job_cancel,
>  };

[Severity: Medium]
Should thames_job_complete() and thames_job_cancel() also bail out when
core->completion_stuck is set?

thames_core_subdev_stop() and thames_job_timedout() check
core->completion_stuck to avoid acquiring core->fence_lock when a
completion thread never returned while holding it.  However,
thames_job_complete() (called from thames_mbox_rx() and
thames_job_drain()) and thames_job_cancel() (called during
thames_core_fini() -> thames_job_fini() -> drm_sched_fini()) do not check
core->completion_stuck before calling dma_fence_signal(job->done_fence),
so they will spin forever on core->fence_lock.

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

Reply via email to