From: sashiko-bot@kernel.org
To: "Philipp Stanner" <phasta@kernel.org>
Cc: dri-devel@lists.freedesktop.org, media-ci@linuxtv.org,
ojeda@kernel.org, gary@garyguo.net
Subject: Re: [PATCH v2 09/10] rust: drm: Add JobQueue
Date: Tue, 24 Nov 2026 12:55:22 +0000 [thread overview]
Message-ID: <sashiko-outbox-231183@kernel.org> (raw)
In-Reply-To: <20261124122951.878199-11-phasta@kernel.org>
Thank you for your contribution! Sashiko AI review found 12 potential
issues to consider:
- [High] rust: dma_fence: payload destructor skipped in
DriverFence::drop()
- [High] rust: drm: unsound lifetime transmute, aliasing, and teardown
in JobQueue
- [High] rust: drm: fence signaling and job drop under SpinLockIrq in
JobQueue
- [High] rust: drm: missing Signed-off-by trailer for Co-developed-by
author
- [Medium] rust: drm: unscheduled DropWorker in RunWorker::run() on
completion race
- [Medium] rust: drm: unsignaled waiting_jobs and serial RCU waits in
JobQueue::drop()
- [Medium] rust: drm: RunWorker::run() does not re-check parked inside
job loop
- [Medium] rust: drm: signed i64 cast of u64 capacity in
RunWorker::run()
- [Medium] drm: rust: missing Release-Acquire ordering on nr_of_deps in
job_queue
- [Low] rust: drm: spurious worker wakeups in
complete_jobs_up_to_seqno()
- [Low] rust: drm: JobQueue doctest passes PinInit closure to
KBox::new()
- [Low] rust: drm: non-vertical imports, broken doc links, and missing
#[inline]
--
commit 1aa6ded0089a768fb2d0e487607dde049d292e4e
Author: Philipp Stanner <phasta@kernel.org>
rust: drm: Add JobQueue
This commit adds JobQueue, a Rust component for managing GPU job
dependencies, credit accounting, and dma_fence lifetimes for GPUs with
firmware scheduling.
> Signed-off-by: Philipp Stanner <phasta@kernel.org>
> Co-developed-by: Gary Guo <gary@garyguo.net>
>
> (^ waiting for Gary to sign-off)
[Severity: High]
Per Documentation/process/submitting-patches.rst, a Co-developed-by
trailer must be immediately followed by the co-author's Signed-off-by
trailer, with the submitter's Signed-off-by last, and the draft note
removed before merging.
> diff --git a/rust/kernel/drm/job_queue.rs b/rust/kernel/drm/job_queue.rs
> new file mode 100644
> index 0000000000000..3d3d78fda5514
> --- /dev/null
> +++ b/rust/kernel/drm/job_queue.rs
> @@ -0,0 +1,863 @@
[ ... ]
> +//! Since jobs might contain data that must not drop in atomic context, and
> +//! since jobs' [`dma_buf::DriverFence`]s perform a call to
> +//! [`rcu::synchronize_rcu()`], they are dropped through a work item if [`Drop`]
> +//! is implemented for the job's payload data.
[ ... ]
> +use kernel::{
> + dma_buf::dma_fence::{
> + DriverFence,
> + DriverFenceAllocation,
> + Fence,
> + FenceCallback,
> + FenceCallbackRegistration,
> + FenceContext,
> + FenceContextOps, //
> + },
> + list::{
> + impl_list_arc_safe,
> + impl_list_item,
> + List,
> + ListItem,
> + ListLinks, //
> + },
> + new_srcu,
> + prelude::*,
> + str::CString,
> + sync::{
> + aref::ARef,
> + atomic::{
> + Atomic,
> + Relaxed, //
> + },
> + new_spinlock_irq,
> + SpinLockIrq, //
> + }, //
> + sync::{
> + atomic::AtomicFlag,
> + Srcu, //
> + },
> + sync::{Arc, UniqueArc},
> + workqueue::{
> + new_scoped_work,
> + OwnedQueue,
> + Queue,
> + ScopedWork,
> + ScopedWorkItem, //
> + }, //
> +};
[Severity: Low]
This isn't a bug, but the kernel::sync imports are split across three
separate sync:: blocks with sync::{Arc, UniqueArc} on a single line rather
than following the vertical import style in
Documentation/rust/coding-guidelines.rst.
Also, the rustdoc intra-doc links to dma_buf::FenceContext,
dma_buf::DriverFence, dma_buf::Fence, and rcu::synchronize_rcu() do not
resolve because dma_buf and rcu are not in scope (and JobQueue::park()'s
doc comment has a single-colon typo in [JobQueue:unpark()]). Finally, the
small wrapper and accessor methods in this module (such as UniqueList's
methods, DriverDataWrapper::deref, DriverDataWrapper::new,
JobQueue::deref, JobQueue::id, and JobQueue::set_error) are missing
#[inline].
[ ... ]
> +impl<'b, T: JobQueueOps + Send + Sync> FenceCallback for DependencyWaker<'b, '_, T> {
> + fn on_signal(&mut self, _res: Result) {
> + // Last dependency kicks the worker. This cannot cause UAF on JQ if the
> + // associated job was forgotten because only submitted jobs can reach 0.
> + if self.nr_of_deps.fetch_sub(1, Relaxed) == 1 {
> + // The drop-order of JobQueue ensures that all DependencyWakers are
> + // cancelled before the JobQueue itself is dropped.
> + self.jq.check_start_run_worker();
> + }
> + }
> +}
[ ... ]
> + pub fn submit(self) -> ARef<Fence> {
> + let jq = self.inner.jq;
> + jq.with_inner(|inner| {
> + // SAFETY: this is a lifetime transmute from `Job<'queue, T>` to
> + // `Job<'fctx, T>`.
> + //
> + // `job` internally holds a reference to `JobQueue`, while we are
> + // now moving `job` into `inner`, which may borrow from `fctx` but
> + // not the full `JobQueue`. So on surface this would be incorrect.
> + // However, as we drain `inner` inside `JobQueue::drop`, the
> + // reference is gone before any field is destructed.
> + let mut job = unsafe { core::mem::transmute::<Job<'_, T>, Job<'_, T>>(self) };
> +
> + // The lock orders setting of the sequence number, so no driver lock is needed.
> + let mut inner = inner.lock();
> + let fence = job.fence_allocation.new_fence();
> + let ret_fence = ARef::from(fence.as_fence());
> +
> + *job.inner.as_mut().project().fence = Some(fence);
> +
> + // Job is submitted now. Signaling dependencies are now allowed to
> + // kick the worker.
> + let _ = job.inner.nr_of_deps.fetch_sub(1, Relaxed);
> +
> + inner.waiting_jobs.push_back(job.inner);
> + drop(inner);
> + jq.check_start_run_worker();
> +
> + ret_fence
> + })
> + }
> +}
[Severity: High]
Is erasing the &JobQueue<T> borrow lifetime in Job::submit() sound while
JobInternal and DependencyWaker still hold jq: &JobQueue<T>?
Because Job::submit() consumes Job<'queue, T>, the caller can obtain
Pin<&mut JobQueue<T>> for JobQueue::set_error() or PinnedDrop::drop()
while DependencyWaker callbacks in waiting_jobs are still active and can
dereference self.jq (&JobQueue<T>) concurrently on another CPU, violating
Rust's aliasing rules (and re-enqueuing run_worker during drop after
srcu.synchronize() has already completed).
In addition, DropWorker::run() pops a job from drop_list and releases
inner.lock() before calling drop(job), without taking srcu.read_lock() and
without JobQueue::drop() flushing drop_worker:
CPU0 (DropWorker::run) CPU1 (JobQueue::drop)
---------------------- ---------------------
lock(inner)
job = drop_list.pop_front()
unlock(inner)
parked.store(true)
srcu.synchronize()
drain inner lists
// PinnedDrop::drop() returns;
// JobQueue fields are dropped
drop(job) <-- job still holds &JobQueue and &FenceContext
Two other lifetime issues affect teardown: in struct JobQueue,
drop_queue and run_queue (OwnedQueue) are declared before drop_worker and
run_worker (ScopedWork), so Rust drops the queues before cancelling their
ScopedWork items; and if an unsubmitted Job with a registered dependency
is forgotten via core::mem::forget (as discussed in Job::add_dependency())
and the JobQueue is later dropped, a signal on that fence still invokes
DependencyWaker::on_signal(&mut self) while self.jq is a dangling
&JobQueue<T>.
[ ... ]
> +impl<T: JobQueueOps + Send + Sync> ScopedWorkItem for RunWorker<'_, '_, T> {
> + fn run(work: &ScopedWork<Self>) {
> + // Synchronize against revocation in `JobQueue::drop`.
> + let _lock_guard = work.srcu.read_lock();
> + if work.parked.load(Relaxed) {
> + return;
> + }
> +
> + let mut jq = work.inner.lock();
> +
> + while let Some(job) = jq.waiting_jobs.pop_front() {
[Severity: Medium]
Should RunWorker::run() also check work.parked inside the while loop?
work.parked is only checked once before entering the loop, and the lock is
released around T::run_job(). If JobQueue::park() or JobQueue::drop() is
called while T::run_job() is running, it sets self.parked to true and
waits in self.srcu.synchronize(), but RunWorker::run() will continue
popping and running all remaining ready jobs in waiting_jobs before
exiting the loop and releasing the SRCU read lock.
> + // While we didn't hold the lock below for `run_job()`, it could be
> + // that a new job entered the waiting list which has an unfulfilled
> + // dependency.
> + if job.nr_of_deps.load(Relaxed) > 0 {
> + jq.waiting_jobs.push_front(job);
> + break;
> + }
[Severity: Medium]
Does Relaxed ordering on nr_of_deps guarantee that T::run_job() observes
memory writes made before dependency fences were signaled?
If RunWorker::run() is already looping when the last dependency fence
signals on another CPU (or if a dependency signals before Job::submit()
performs the final decrement to 0), RunWorker::run() can observe
job.nr_of_deps.load(Relaxed) == 0, unlock jq (a release operation), and
call T::run_job() without any acquire barrier synchronizing with the
dependency signal:
CPU0 (dependency signal) CPU1 (RunWorker::run)
------------------------ ---------------------
// writes before fence signal
nr_of_deps.fetch_sub(1, Relaxed)
// reaches 0
lock(inner)
nr_of_deps.load(Relaxed) == 0
unlock(inner)
T::run_job() <-- no acquire barrier
Using Release ordering on nr_of_deps.fetch_sub() in
DependencyWaker::on_signal() and Job::submit() paired with Acquire
ordering on job.nr_of_deps.load() in RunWorker::run() would establish a
release-acquire chain before T::run_job() runs.
> + let new_capacity = jq.capacity as i64 - i64::from(job.cost);
> + if new_capacity < 0 {
> + jq.waiting_jobs.push_front(job);
> + break;
> + }
> + *jq.as_mut().project().capacity = new_capacity as u64;
[Severity: Medium]
Can casting jq.capacity (u64) to i64 wrap negative or underflow when
capacity_param exceeds i64::MAX?
JobQueue::new() accepts any u64 for capacity_param, so if jq.capacity is
greater than i64::MAX as u64, jq.capacity as i64 becomes negative and
either permanently stalls the queue (new_capacity < 0) or underflows
i64::MIN. Could this use jq.capacity.checked_sub(u64::from(job.cost))
instead?
> +
> + let fence = job.fence.as_ref().expect("fence not present");
> + let seqno = fence.as_fence().seqno();
> + let runnable_job = JobRunnable {
> + seqno,
> + data: fence.deref(),
> + };
> +
> + drop(jq); // Release lock.
> + T::run_job(work.fctx, runnable_job);
> + jq = work.inner.lock(); // Retake lock.
> +
> + // run_job() above needs to be called without lock protection. Thus,
> + // it is possible that the driver instructed completion right before
> + // we could re-take the lock.
> + if jq.last_completed >= seqno {
> + *jq.as_mut().project().capacity += u64::from(job.cost);
> + fence.signal(Ok(()));
> +
> + // Drop here directly if possible. Otherwise, let the DropWorker
> + // take care of it.
> + if core::mem::needs_drop::<T::JobDataType>() {
> + jq.drop_list.push_back(job);
> + }
> + } else {
> + jq.running_jobs.push_back(job);
> + }
> + }
> + }
> +}
[Severity: Medium]
As reported by Younes Akhouayri on v1 [1], when a job completes before
RunWorker::run() retakes work.inner.lock(), does pushing the job onto
jq.drop_list here without scheduling drop_worker leave the job stranded?
In that race, complete_jobs_up_to_seqno() calls check_start_drop_worker()
before the job is on drop_list, so DropWorker::run() can see an empty list
and exit before RunWorker::run() pushes the job onto jq.drop_list. The
job and its payload then remain unfreed in drop_list until a later job
completes or the JobQueue is dropped.
> +
> +struct DropWorker<'a, 'fctx, T: JobQueueOps + Send + Sync> {
> + inner: &'a SpinLockIrq<JobQueueInner<'fctx, T>>,
> +}
> +
> +impl<T: JobQueueOps + Send + Sync> ScopedWorkItem for DropWorker<'_, '_, T> {
> + fn run(work: &ScopedWork<Self>) {
> + let mut jq_locked = work.inner.lock();
> +
> + while let Some(job) = jq_locked.drop_list.pop_front() {
> + drop(jq_locked);
> + drop(job);
> + jq_locked = work.inner.lock();
> + }
> + }
> +}
[Severity: High]
When DropWorker::run() (or JobQueue::drop()) drops a job whose
T::JobDataType implements Drop, does DriverFence::drop() actually run the
payload's destructor?
In DriverFence::drop() (updated earlier in this series by commit "rust:
DmaFence: Replace call_rcu() with cond_synchronize_rcu()"), drop_in_place
is called on &raw mut self.data rather than on the inner payload field:
rust/kernel/dma_buf/dma_fence.rs:DriverFence::drop() {
...
// SAFETY: Valid because `self` is valid. RCU above ensures that all
// accessors are gone.
unsafe { drop_in_place(&raw mut self.data) };
// SAFETY: Valid because `self` is valid. This is the initial reference
// from when this `DriverFence` was created.
unsafe { bindings::dma_fence_put(self.as_raw()) };
}
Because DriverFence::data is a NonNull<DriverFenceData<'a, T>> pointer and
the user payload lives at (*self.data.as_ptr()).data,
drop_in_place(&raw mut self.data) is a no-op on a pointer wrapper and
dma_fence_put() frees the allocation without running T::JobDataType's
Drop implementation, leaking any resources owned by the job payload.
[ ... ]
> +/// let cb = Callback { seqno: done_fence2.seqno() };
> +/// // SAFETY: The registration is not leaked.
> +/// let reg = unsafe { FenceCallbackRegistration::new(&done_fence2, cb) };
> +/// let reg = KBox::new(reg, GFP_KERNEL)?;
[Severity: Low]
In this doctest example, FenceCallbackRegistration::new() returns an
impl PinInit, so KBox::new(reg, GFP_KERNEL)? boxes the unexecuted
initializer closure instead of registering the callback. Should this be
KBox::pin_init(reg, GFP_KERNEL)?
[ ... ]
> + pub fn complete_jobs_up_to_seqno(&self, seqno: u64) {
> + self.with_inner(|inner| {
> + let needs_drop = core::mem::needs_drop::<T::JobDataType>();
> + let mut jq_locked = inner.lock();
> + let mut jq = jq_locked.as_mut().project();
> +
> + if seqno < *jq.last_completed {
> + return;
> + }
> +
> + while let Some(mut job) = jq.running_jobs.pop_front() {
> + if job
> + .as_mut()
> + .project()
> + .fence
> + .as_mut()
> + .expect("fence not present")
> + .as_fence()
> + .seqno()
> + > seqno
> + {
> + jq.running_jobs.push_front(job);
> + break;
> + }
> +
> + *jq.capacity += u64::from(job.cost);
> +
> + let fence = job.fence.as_ref().expect("fence not present");
> + fence.signal(Ok(()));
> +
> + if needs_drop {
> + jq.drop_list.push_back(job);
> + }
> + // Otherwise, job is dropped here directly. Its fence won't perform
> + // an RCU grace period wait.
> + }
[Severity: High]
Can calling fence.signal(Ok(())) and dropping job while holding
inner.lock() (SpinLockIrq) deadlock in complete_jobs_up_to_seqno() and
RunWorker::run()?
DriverFence::signal() acquires the fence spinlock and synchronously runs
registered FenceCallback::on_signal() callbacks while inner.lock() is
held. If a completion callback submits another job via Job::submit() on
the same JobQueue, it will try to acquire inner.lock() on the same CPU:
complete_jobs_up_to_seqno()
inner.lock()
fence.signal(Ok(()))
dma_fence_signal_locked()
on_signal()
Job::submit()
inner.lock() <-- self-deadlock
Even across different fences, acquiring fence->lock inside inner.lock()
(during both fence.signal() and, when !needs_drop, dropping
job.dependencies -> FenceCallbackRegistration::drop() ->
dma_fence_remove_callback()) inverts the fence->lock -> inner.lock()
ordering taken whenever a fence callback calls into JobQueue, creating an
ABBA deadlock.
> +
> + // This enables the RunWorker to complete a job that is not yet
> + // in 'running_jobs', in case of a race against the lock.
> + *jq.last_completed = seqno;
> + drop(jq_locked);
> +
> + self.check_start_run_worker();
> + if needs_drop {
> + self.check_start_drop_worker();
> + }
> + })
> + }
[Severity: Low]
If complete_jobs_up_to_seqno() is called with seqno == *jq.last_completed,
or when no jobs in running_jobs had seqno <= the argument, this still
wakes both run_worker and drop_worker (even when drop_list is empty).
Checking seqno <= *jq.last_completed and only starting the workers when at
least one job was completed (and clearing job.dependencies once a job is
popped in RunWorker::run()) would avoid the extra workqueue wakeups and
retain fewer allocations while jobs are running.
> +
> + /// Set the error with which this JobQueue will signal all running jobs'
> + /// fences. (The jobs on the waiting_list will all complete with ECANCELED
> + /// as their error)
> + pub fn set_error(mut self: Pin<&mut Self>, error: Error) {
> + *self.as_mut().project().err = error;
> + }
> +}
> +
> +#[pinned_drop]
> +impl<T: JobQueueOps + Send + Sync> PinnedDrop for JobQueue<T> {
> + fn drop(self: Pin<&mut Self>) {
> + // Prevent the RunWorker from moving more jobs to `running_jobs`.
> + self.parked.store(true, Relaxed);
> + self.srcu.synchronize();
> + self.with_inner(|inner| {
> + let mut inner_locked = inner.lock();
> + let drop_list = core::mem::take(&mut inner_locked.drop_list);
> + let waiting_jobs = core::mem::take(&mut inner_locked.waiting_jobs);
> + let mut running_jobs = core::mem::take(&mut inner_locked.running_jobs);
> +
> + drop(inner_locked);
> +
> + while let Some(job) = running_jobs.pop_front() {
> + let fence = job.fence.as_ref().expect("fence not present");
> + fence.signal(Err(self.err));
> + }
> +
> + drop(drop_list);
> + drop(waiting_jobs);
> + drop(running_jobs);
> + })
> + }
> +}
[Severity: Medium]
Does JobQueue::drop() leave waiting_jobs unsignaled and wait for a
separate RCU grace period on every job?
Job::submit() already creates a DriverFence for each job in waiting_jobs,
and the doc comments state that dropping a JobQueue signals waiting jobs'
fences with ECANCELED. Because JobQueue::drop() drops waiting_jobs
without calling fence.signal(Err(ECANCELED)), each waiting job hits
DriverFence::drop()'s unsignaled error path, logging
pr_err!("DriverFence drops unsignaled. Danger of memory corruption!\n")
and calling an unconditional synchronize_rcu().
In addition, while let Some(job) = running_jobs.pop_front() drops each job
at the end of the loop body right after fence.signal(Err(self.err)), which
calls cond_synchronize_rcu() immediately per job instead of signaling all
running and waiting fences first so they can share a single grace period.
[1] https://lore.kernel.org/r/kw-1794059738.215694434-299782-0@younes.io
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124122951.878199-2-phasta@kernel.org?part=9
next prev parent reply other threads:[~2026-11-24 12:55 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 12:29 [PATCH v2 00/10] rust: Add drm::JobQueue Philipp Stanner
2026-11-24 12:29 ` [PATCH v2 01/10] rust: rcu: Add get_state_synchronize_rcu() Philipp Stanner
2026-11-25 16:47 ` Gary Guo
2026-11-24 12:29 ` [PATCH v2 02/10] rust: rcu: Add cond_synchronize_rcu() Philipp Stanner
2026-11-24 12:29 ` [PATCH v2 03/10] rust: DmaFence: Implement Deref for FenceContext Philipp Stanner
2026-11-24 12:29 ` [PATCH v2 04/10] rust: DmaFence: Add ID accessor " Philipp Stanner
2026-11-24 12:29 ` [PATCH v2 05/10] rust: DmaFence: Don't drop on signal Philipp Stanner
2026-11-24 12:29 ` [PATCH v2 06/10] rust: DmaFence: Replace call_rcu() with cond_synchronize_rcu() Philipp Stanner
2026-11-24 12:43 ` sashiko-bot
2026-11-24 12:29 ` [PATCH v2 07/10] rust: DmaFence: Replace is_signaled() with status() Philipp Stanner
2026-11-24 12:44 ` sashiko-bot
2026-11-25 16:52 ` Gary Guo
2026-11-24 12:29 ` [PATCH v2 08/10] rust: DmaFence: Pass result in callbacks Philipp Stanner
2026-11-24 12:29 ` [PATCH v2 09/10] rust: drm: Add JobQueue Philipp Stanner
2026-11-24 12:55 ` sashiko-bot [this message]
2026-11-24 12:29 ` [PATCH v2 10/10] MAINTAINERS: Add drm::JobQueue to drm_sched's entry Philipp Stanner
2026-11-25 16:53 ` Gary Guo
2026-11-24 23:53 ` [PATCH v2 00/10] rust: Add drm::JobQueue Daniel Almeida
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-231183@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=gary@garyguo.net \
--cc=media-ci@linuxtv.org \
--cc=ojeda@kernel.org \
--cc=phasta@kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.