Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [High] accel/thames: jobs scheduled and submitted to offline cores in thames_job_run() - [High] accel/thames: use-after-free write to job->handle in thames_ioctl_job_create() - [High] accel/thames: use-after-free of tdev in async thames_sched_job_cleanup() - [Medium] accel/thames: missing seqno check and fence race in thames_core_idles() - [Medium] accel/thames: premature ring validation before attach in thames_job_init() - [Medium] accel/thames: per-job sched_lock in thames_job_push() interleaves batches - [Low] accel/thames: wrong errno for zero BO handle in thames_ioctl_job_create() --
commit 7d6bd35f5090915009121f7b7696eb28f979bf94 Author: Tomeu Vizoso <[email protected]> accel/thames: Add IOCTLs for job creation and submission This commit adds DRM_IOCTL_THAMES_JOB_CREATE, DRM_IOCTL_THAMES_JOB_SUBMIT, and DRM_IOCTL_THAMES_JOB_DESTROY along with per-core DRM GPU scheduler instances to validate and run jobs on the DSP cores. > diff --git a/drivers/accel/thames/thames_core.c > b/drivers/accel/thames/thames_core.c > index 63eabbfaf4966..73350784979ec 100644 > --- a/drivers/accel/thames/thames_core.c > +++ b/drivers/accel/thames/thames_core.c [ ... ] > @@ -357,14 +359,36 @@ static void thames_core_free_doorbell(struct > thames_core *core) > core->mbox_chan = NULL; > } > > +/* With every core offline all stay ready, so an entity keeps a run queue. */ > +static void thames_core_update_sched_ready(struct thames_device *tdev) > +{ > + bool any_online = false; > + unsigned int i; > + > + guard(mutex)(&tdev->sched_lock); > + > + for (i = 0; i < tdev->num_cores; i++) > + any_online |= !thames_core_is_dead(&tdev->cores[i]); > + > + for (i = 0; i < tdev->num_cores; i++) > + tdev->cores[i].sched.ready = !any_online || > + > !thames_core_is_dead(&tdev->cores[i]); > +} [Severity: High] Can jobs still be scheduled and pushed onto the hardware ring of an offline core? drm_sched_entity_select_rq() only re-runs drm_sched_pick_best() when the entity's job_queue is empty and entity->last_scheduled has already signaled. If a core goes offline while an entity has a job in flight or queued, or when all cores are offline (!any_online sets sched.ready = true on every core), new submissions stay on the dead core's scheduler. Because thames_job_run() does not check thames_core_is_dead(core), it writes the job into core->ipc->ring, advances ring->tail and core->ring_tail, and queues the job on core->submitted, where it hangs until reboot (or is discarded without running when the firmware empties the ring on a later remoteproc boot, only to have thames_job_complete() signal its fence as succeeded once a later job advances fence.seqno). Also, when one core is offline and another is online, setting sched.ready = false while leaving the scheduler in tdev->sched_list causes drm_sched_pick_best() to emit an un-ratelimited DRM_WARN("scheduler %s is not ready, skipping") on every job submission from an idle entity. Note that a later patch in this series (commit "accel/thames: Recover from faults and timeouts") adds a thames_core_is_dead() check in thames_job_run() and fails core->submitted on stop, but thames_core_update_sched_ready() still triggers the DRM_WARN in drm_sched_pick_best() and still leaves entities with in-flight or queued jobs bound to the offline core's run queue. [ ... ] > @@ -595,6 +630,25 @@ void thames_core_fini(struct thames_core *core) > core->of_node = NULL; > } > > +/* > + * The system firmware accepts a stop only of a core in IDLE. The firmware > + * idles once it has taken every entry on the Ring, and after its own fault, > + * which leaves the head on the faulting entry. > + */ > +bool thames_core_idles(struct thames_core *core) > +{ > + 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; > +} > + > /* > * rproc_shutdown() stops the core only when it drops the last power count. > The > * core holds one: the count remoteproc took to boot or attach it, or the one > @@ -606,6 +660,10 @@ int thames_core_suspend(struct thames_core *core) > struct rproc *rproc = core->rproc; > int err; > > + err = thames_job_suspend(core); > + if (err) > + return err; > + > if (core->suspended) > return 0; [Severity: Medium] Does thames_core_idles() match a stale fault record from an earlier ring wrap, or return true before core->ipc->fence.seqno has caught up? In thames_abi.h, struct thames_fault_record notes that @seqno distinguishes a fault record from one an earlier job left in the same ring slot, but thames_core_idles() checks record->kind == DRM_THAMES_FAULT_FIRMWARE without verifying record->seqno against core->ipc->ring.entries[slot].host.seqno. If an earlier job left DRM_THAMES_FAULT_FIRMWARE in that slot, thames_core_idles() returns true while the current job is still running, and even on a current DRM_THAMES_FAULT_FIRMWARE fault the faulting job remains on core->submitted, causing thames_job_suspend() to fail suspend with -EBUSY. In addition, as noted in thames_job_init(), the firmware advances ring.head before writing core->ipc->fence.seqno. If thames_core_idles() observes head == core->ring_tail before fence.seqno is written, thames_job_suspend() stops polling, runs thames_job_complete() once, sees core->submitted non-empty, and fails suspend with -EBUSY. Calling thames_job_suspend() before checking core->suspended or thames_core_is_dead() also causes suspend to poll and fail on an offline core (though commit "accel/thames: Recover from faults and timeouts" later in the series adds a dead-core check inside thames_job_suspend()). > diff --git a/drivers/accel/thames/thames_job.c > b/drivers/accel/thames/thames_job.c > new file mode 100644 > index 0000000000000..ee0a883065f97 > --- /dev/null > +++ b/drivers/accel/thames/thames_job.c [ ... ] > +int thames_job_init(struct thames_core *core) > +{ [ ... ] > + /* > + * Both indices run free, so only a tail not at the head is wrong: the > + * previous instance's entries are still pending. The Fence may trail > + * the head by one entry, the firmware moving the head first. > + */ > + head = READ_ONCE(core->ipc->ring.head); > + tail = READ_ONCE(core->ipc->ring.tail); > + before = (head - 1) % THAMES_RING_DEPTH; > + > + if (tail != head) { > + dev_err(core->dev, "Ring on " THAMES_CORE_FMT > + " has %u pending entries (tail %u, head %u); not > attaching\n", > + THAMES_CORE_ARG(core), tail - head, tail, head); > + return -EBUSY; > + } > + > + /* Read the Fence and the entry after the head and the tail. */ > + rmb(); > + fence = READ_ONCE(core->ipc->fence.seqno); > + before_seqno = core->ipc->ring.entries[before].host.seqno; > + last = max(fence, before_seqno); > + > + core->ring_tail = head; > + core->emit_seqno = last; [Severity: Medium] Should the ring head/tail check and core->emit_seqno initialization happen in thames_core_attach() rather than thames_job_init()? thames_core_init() calls thames_job_init() during probe before thames_core_add_subdev() checks whether the remoteproc is running. Because core->ipc is persistent reserved memory and the firmware only empties the ring when it boots, probing while a core is RPROC_OFFLINE with stale ring.tail != ring.head (after a prior firmware fault or stop, or before the first boot) fails thames_core_init() with -EBUSY and permanently drops the core. Conversely, if an offline core passes thames_job_init() and boots later, thames_core_attach() updates core->ring_tail but never refreshes core->emit_seqno from the IPC ring and fence. [ ... ] > +void thames_job_fini(struct thames_core *core) > +{ > + struct thames_sched_job *job; > + u64 first = U64_MAX, last = 0; > + size_t kept; > + > + drm_sched_fini(&core->sched); > + > + guard(mutex)(&core->job_lock); > + > + kept = list_count_nodes(&core->held); > + if (!kept) > + return; > + > + list_for_each_entry(job, &core->held, node) { > + first = min(first, job->seqno); > + last = max(last, job->seqno); > + } > + > + drm_dev_get(&core->tdev->ddev); > + dev_warn(core->dev, THAMES_CORE_FMT > + " may still run %zu Jobs (seqno %llu to %llu); keeping them > and their buffers until reboot\n", > + THAMES_CORE_ARG(core), kept, first, last); > +} [ ... ] > +void thames_job_close(struct thames_file_priv *thames_priv) > +{ > + struct thames_job *job; > + unsigned long handle; > + > + drm_sched_entity_destroy(&thames_priv->sched_entity); > + > + xa_for_each(&thames_priv->jobs, handle, job) > + thames_job_put(job); > + xa_destroy(&thames_priv->jobs); > +} [Severity: High] Can asynchronous entity job teardown use tdev after it has been freed on device unbind? When a job in entity->job_queue is waiting on an unsignaled dependency fence (or entity->last_scheduled) and the device is unbound and closed, drm_sched_entity_destroy() kills the unscheduled job asynchronously via drm_sched_entity_kill_jobs_cb() -> drm_sched_entity_kill_jobs_work() on system_wq: CPU0 CPU1 (system_wq) thames_platform_remove() thames_job_close(priv) drm_sched_entity_destroy() ... -> dma_fence_add_callback( ..., drm_sched_entity_kill_jobs_cb) thames_core_fini() thames_job_fini() drm_sched_fini(&core->sched) /* core->held is empty: * no drm_dev_get() */ thames_postclose() /* last drm_dev_put(&tdev->ddev): * drm_managed_release() and * kfree(tdev) run */ drm_sched_entity_kill_jobs_work() job->sched->ops->free_job(job) thames_sched_job_cleanup() thames_file_priv_put() thames_context_destroy() thames_job_put() thames_transfers_free() <-- use-after-free of tdev Because killed entity jobs never entered core->sched.pending_list or core->held, drm_sched_fini() does not wait for them and thames_job_fini() does not take a drm_dev_get() reference. When drm_sched_entity_kill_jobs_work() runs, it dereferences job->sched inside the freed tdev and invokes thames_context_destroy() and thames_transfers_free() on destroyed drmm mutexes, IDAs, and tdev->transfer_table_pool_map. Holding a drm_dev_get() reference per thames_file_priv (or per thames_job / thames_sched_job) until its release would keep tdev alive until all asynchronous cleanup completes. [ ... ] > +int thames_ioctl_job_create(struct drm_device *dev, void *data, struct > drm_file *file) > +{ > + struct drm_thames_job_create *args = data; > + struct thames_file_priv *file_priv = file->driver_priv; > + struct thames_device *tdev = to_thames_device(dev); > + struct thames_job *job; > + u32 handle; > + int ret; > + > + if (args->flags || args->pad) > + return -EINVAL; > + > + if (args->type != DRM_THAMES_JOB_DISPATCH) > + return -EINVAL; > + > + if (!args->program || !args->params) > + return -EINVAL; [Severity: Low] Should a zero args->program or args->params handle return -ENOENT instead of -EINVAL? The struct drm_thames_job_create uAPI documentation states that a handle naming no buffer fails with -ENOENT, which is what drm_gem_object_lookup() below returns when this check is omitted. [ ... ] > + ret = xa_alloc(&file_priv->jobs, &handle, job, xa_limit_32b, > GFP_KERNEL); > + if (ret) > + goto err_put; > + > + job->handle = handle; > + args->handle = handle; > + > + return 0; [Severity: High] Can assigning job->handle after xa_alloc() write to a freed struct thames_job? Once xa_alloc() publishes job in file_priv->jobs and drops the XArray lock, a second thread sharing the DRM file can guess the handle and destroy it before job->handle is written: CPU0 CPU1 thames_ioctl_job_create() xa_alloc(&file_priv->jobs, &handle, job, ...) thames_ioctl_job_destroy() job = xa_erase(&file_priv->jobs, handle) thames_job_put(job) kfree(job) job->handle = handle; <-- use-after-free job->handle is never read anywhere in the driver, so the field and this assignment can be removed (or set under the XArray lock before publishing the entry). [ ... ] > +int thames_ioctl_job_submit(struct drm_device *dev, void *data, struct > drm_file *file) > +{ [ ... ] > + /* > + * Only the first Job waits: the entity runs the call's Jobs in order. > + */ > + ret = thames_acquire_object_fences(bos->bos, bos->read_count, > &subs[0]->base, false); > + if (ret) > + goto out_unlock; > + > + ret = thames_acquire_object_fences(&bos->bos[bos->read_count], > bos->count - bos->read_count, > + &subs[0]->base, true); > + if (ret) > + goto out_unlock; > + > + for (pushed = 0; pushed < count; pushed++) > + thames_job_push(subs[pushed]); [Severity: Medium] Can concurrent multi-job submissions on the same DRM file interleave their jobs in file_priv->sched_entity? thames_job_push() takes and releases tdev->sched_lock around each individual job rather than holding it across the entire loop. If two threads submit multi-job batches concurrently on the same file with disjoint or empty buffer lists (so drm_gem_lock_reservations() does not serialize them), their thames_job_push() calls can interleave on file_priv->sched_entity, breaking the batch ordering that thames_ioctl_job_submit() relies on when it attaches input dependencies only to subs[0] and output fences only from subs[count - 1]. -- Sashiko AI review ยท https://sashiko.dev/#/patchset/[email protected]?part=8
