From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795953487; b=U95500r/Wa9yloWdPGXoC4/BC547QkPuNlDPtBHr/rQZsas7b/MJGHfSmiJfKBThmY1+ JMqeDdHnrWGK79IvG0wLpeSox1IHWRg7QKo7oiPHAMLB2lklNjZgkdY2aggSm6zNgOMb8 zhXhLvncDv/i7c4Y6DL7agrunJSIEKRJVC5j3NwGCfGnvvxzZ8vzj+y1J+esOtocEKfdp gp2fJNg0QbogBIvaMDO2Gqmcct/UJzPLPHwp6teLDB0REkynFcpc0gGj1QXTrdGw2Q2Vh CgHJMXiL7WYHzl/q5JRYshsVjFBJSauZRo7vsdlXn4cKipGP9W+35buvx1LgY+HzvXQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795953487; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=iZouImZSuy5cAajB8ztXjeUPmnZBTrIANGFacZiJ+DI=; b=SN6U+5pUx3ZmhiVC3nzp93FsR3dtBmRr/KB0neSz+0eORaO4KF4gLcyZxaE6CvCVNZVY DQJSrSP19SUUImpWcyWFvByzWY76/0CZC5v32ls1KvTOonvZedXF3ie/gN6/H5ALoNttN 15g0s4ribr5FjlNN3nDOaWKYXuvLUPwQ3OEk4+gv3EXJCFcQd2FmG8mqsV4iMXtU0WfrW NM2k/zvM4y9ldUAdPsAjBrAthWodfLOVDM/kfMEoz8XWyx1W957xB5IVoHaZLVWJGmu1w G9EvJKgjVTnF3si5to25vfphWO3r6K4rGOyFq2Cba4GkMD8z71zFeXaKnbbzRGve4XA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="UBo1HFt/"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A523A606A32E for ; Sun, 29 Nov 2026 11:58:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=UBo1HFt/ Received: by smtp.kernel.org (Postfix) with ESMTPSA id F2A7D1F00896; Sun, 29 Nov 2026 11:58:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795953487; bh=iZouImZSuy5cAajB8ztXjeUPmnZBTrIANGFacZiJ+DI=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=UBo1HFt/MZLFYxHOYHhHEErvyOgmm0xcCp3k79lYIBOfABtjMaXYUOFjQFqtgmKqE 5flHec8aGNua5Ngvdb2H87q4XTcbZlm2hbhAXQTNc/86uPYEkWG82dnwmg9W5wuGYo /aQLj4SGfO560sIwezb3I+X8Dlvwj8FiYJXUy5iyBc2NUnr5Lu6ZgJ7jpXSm+MEAU7 ZSGxAYcXg8pFX8ByRghh1JNbhgYznXrA8dO4RPxajy8J5SGyEydxusylHXiOf/AF9p 4hSS1oS0ye2PCScek6ulwpCCGowR+8rGnKbfQuoXwNdKHwG/PbgDRH5b7/mk2332TP U74ChJMkiyHZw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v4 08/14] accel/thames: Add IOCTLs for job creation and submission Reply-To: sashiko-reviews@lists.linux.dev To: "Tomeu Vizoso" Cc: devicetree@vger.kernel.org, dri-devel@lists.freedesktop.org, robh@kernel.org, conor+dt@kernel.org, media-ci@linuxtv.org In-Reply-To: <20261129-thames-v4-8-003b1e3365bf@tomeuvizoso.net> References: <20261129-thames-v4-0-003b1e3365bf@tomeuvizoso.net> <20261129-thames-v4-8-003b1e3365bf@tomeuvizoso.net> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sun, 29 Nov 2026 11:58:06 +0000 X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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/th= ames_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 =3D NULL; > } > =20 > +/* With every core offline all stay ready, so an entity keeps a run queu= e. */ > +static void thames_core_update_sched_ready(struct thames_device *tdev) > +{ > + bool any_online =3D false; > + unsigned int i; > + > + guard(mutex)(&tdev->sched_lock); > + > + for (i =3D 0; i < tdev->num_cores; i++) > + any_online |=3D !thames_core_is_dead(&tdev->cores[i]); > + > + for (i =3D 0; i < tdev->num_cores; i++) > + tdev->cores[i].sched.ready =3D !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 =3D 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 = =3D 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 =3D NULL; > } > =20 > +/* > + * The system firmware accepts a stop only of a core in IDLE. The firmw= are > + * idles once it has taken every entry on the Ring, and after its own fa= ult, > + * which leaves the head on the faulting entry. > + */ > +bool thames_core_idles(struct thames_core *core) > +{ > + u32 head =3D READ_ONCE(core->ipc->ring.head); > + u32 slot =3D head % THAMES_RING_DEPTH; > + struct thames_fault_record *record =3D &core->ipc->fault_records[slot]; > + > + if (head =3D=3D core->ring_tail) > + return true; > + > + /* Read the fault record after the head that says it is there. */ > + rmb(); > + return READ_ONCE(record->kind) =3D=3D DRM_THAMES_FAULT_FIRMWARE; > +} > + > /* > * rproc_shutdown() stops the core only when it drops the last power cou= nt. The > * core holds one: the count remoteproc took to boot or attach it, or th= e one > @@ -606,6 +660,10 @@ int thames_core_suspend(struct thames_core *core) > struct rproc *rproc =3D core->rproc; > int err; > =20 > + err =3D 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 =3D=3D 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 =3D=3D core->ring_tail before fence.seqno is written, thames_job_suspe= nd() 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/tha= mes_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 =3D READ_ONCE(core->ipc->ring.head); > + tail =3D READ_ONCE(core->ipc->ring.tail); > + before =3D (head - 1) % THAMES_RING_DEPTH; > + > + if (tail !=3D 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 =3D READ_ONCE(core->ipc->fence.seqno); > + before_seqno =3D core->ipc->ring.entries[before].host.seqno; > + last =3D max(fence, before_seqno); > + > + core->ring_tail =3D head; > + core->emit_seqno =3D 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 !=3D ring.head (after a prior firmware fault or stop, or before t= he 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 =3D U64_MAX, last =3D 0; > + size_t kept; > + > + drm_sched_fini(&core->sched); > + > + guard(mutex)(&core->job_lock); > + > + kept =3D list_count_nodes(&core->held); > + if (!kept) > + return; > + > + list_for_each_entry(job, &core->held, node) { > + first =3D min(first, job->seqno); > + last =3D 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 d= rm_file *file) > +{ > + struct drm_thames_job_create *args =3D data; > + struct thames_file_priv *file_priv =3D file->driver_priv; > + struct thames_device *tdev =3D to_thames_device(dev); > + struct thames_job *job; > + u32 handle; > + int ret; > + > + if (args->flags || args->pad) > + return -EINVAL; > + > + if (args->type !=3D 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 =3D xa_alloc(&file_priv->jobs, &handle, job, xa_limit_32b, GFP_KERN= EL); > + if (ret) > + goto err_put; > + > + job->handle =3D handle; > + args->handle =3D 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 =3D xa_erase(&file_priv->jobs, handle) thames_job_put(job) kfree(job) job->handle =3D 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 d= rm_file *file) > +{ [ ... ] > + /* > + * Only the first Job waits: the entity runs the call's Jobs in order. > + */ > + ret =3D thames_acquire_object_fences(bos->bos, bos->read_count, &subs[0= ]->base, false); > + if (ret) > + goto out_unlock; > + > + ret =3D thames_acquire_object_fences(&bos->bos[bos->read_count], bos->c= ount - bos->read_count, > + &subs[0]->base, true); > + if (ret) > + goto out_unlock; > + > + for (pushed =3D 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]. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261129-thames-v4-= 0-003b1e3365bf@tomeuvizoso.net?part=3D8