From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA596606A33D for ; Wed, 2 Dec 2026 12:05:01 +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=JdlUH3QS Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 15B1F10E9D3; Wed, 2 Dec 2026 12:04:56 +0000 (UTC) Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by gabe.freedesktop.org (Postfix) with ESMTPS id BE04710E9D3 for ; Wed, 2 Dec 2026 12:04:49 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 68F0B42E51; Wed, 2 Dec 2026 12:04:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 17DFA1F00925; Wed, 2 Dec 2026 12:04:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796213089; bh=PeKMZ02xlqfMdNTj1WzJJwL4JUAHSmmFbycPqyBJeSA=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=JdlUH3QSfLWexGo7NJRrZtW49QdgH3Wto57C0irwPVCJUEoqcQaMaI+zIWLf47rlA rbpRe7Dh73qSWTUYgz6BWlmcNLAWLrwjFTB95EqEkdviqZ2m8EUMlW/hW+U737S/RD sAehuANimjWbxKKSn/23PR0zjLYoKSkS1uhvr7p+iO7zu5lVuZco0zXR4B/p7K5J1I C+8NiBW9rpk4JlfyUxXH48QDStq0aDnJaD5pPqEUhu4HlhU8U3q62xo8rw23nGjVJa oNZOt9eekE1vD5rZ3Dg5rz0MTkbXRkQQmTKE0J17axinnPbvLM2jxCwSp2uWxOO/CS f0+0nkLwJqVDA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v3 8/9] rust: drm: Add JobQueue To: "Philipp Stanner" Cc: dri-devel@lists.freedesktop.org, media-ci@linuxtv.org, ojeda@kernel.org, gary@garyguo.net In-Reply-To: <20261202113914.1660411-10-phasta@kernel.org> References: <20261202113914.1660411-2-phasta@kernel.org> <20261202113914.1660411-10-phasta@kernel.org> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 12:04:48 +0000 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: sashiko-reviews@lists.linux.dev Sender: "dri-devel" X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: Thank you for your contribution! Sashiko AI review found 11 potential issues to consider: - [High] rust: dma_fence: payload destructor skipped in DriverFence::drop() - [High] rust: drm: missing Signed-off-by trailer for Co-developed-by author - [High] rust: drm: unsound lifetime transmute, aliasing, and teardown in JobQueue - [High] rust: drm: fence signaling and job drop under SpinLockIrq in JobQueue - [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: missing Release-Acquire ordering on nr_of_deps in job_queue - [Medium] rust: drm: signed i64 cast of u64 capacity in RunWorker::run() - [Low] rust: drm: spurious and missed worker wakeups in JobQueue completion - [Low] rust: drm: broken rustdoc links and missing #[inline] in job_queue.rs -- commit e74c12be9636b8996c8f70b342962e1ee1b25813 Author: Philipp Stanner > rust: drm: Add JobQueue > > JobQueue is a pure Rust component that handles job submissions for GPUs > with firmware scheduling, i.e., GPUs that support one exclusive ring per > execution context. > > JobQueue's primary task is dependency handling, where the user can set > dependencies, each in the form of a `dma_buf::Fence`, on a job and then > submit it to the queue. > > Jobs are being executed in FIFO order. Only jobs whose dependencies are > all fulfilled get executed, otherwise the JobQueue pauses. > > A core design principle, just like with DmaFence, is to avoid all > allocations in job submission paths, since this runs danger of > deadlocking. For the same reason JobQueue's run_job workqueue is > reclaim-safe. > > Another design principle, new when compared to the older drafts, is the > ownership relationship with fences: > > 1. The JobQueue owns the FenceContext exclusively. > 2. Jobs are created on the JobQueue, ensuring correct fence sequence > number ordering. > 3. Jobs own the respective DriverFence. > 4. Jobs are owned by the JobQueue. > > Consequently, a driver can only interact with fences and FenceContext > indirectly through the JobQueue, and can also only complete jobs (and, > hence, signal fences) through the JobQueue. > > Like drm_sched, JobQueue currently does not take errors set on job > dependency fences into account. The reasons are that only all > dependencies being signaled is decisive for memory safety, and that we > would first have to establish what the semantic meaning of fence errors > actually is. That's especially difficult when you consider that fences > can be shared across drivers. Nevertheless, it would be doable to inform > drivers through their run_job callback that this particular jobs has > failed (but signaled) dependencies, and then allow the driver to make a > decision on how to proceed (i.e., ignore or kill JQ). > > When a job shall be run, both its JobQueue::data and Job::data get > borrowed back to the driver, together with the sequence number of this > job, so that it can perform the appropriate interaction with the > hardware. > > The RunWorker runs all ready jobs based on a credit count system, so > that run_job() is infallible. A separate worker drops jobs (and > their fences) that are already completed, but only if the user's payload > data actually requires Drop. Otherwise, jobs are dropped by the > RunWorker directly. > > Data left over by the workers drops together with JobQueue. For this > reason, and because a JobQueue owns a FenceContext, it must only ever > be dropped in process context. > > Signed-off-by: Philipp Stanner > Co-developed-by: Gary Guo > > (^ waiting for Gary to sign-off) [Severity: High] Co-developed-by: Gary Guo is missing a matching Signed-off-by: trailer right after it, the submitter's Signed-off-by: trailer should come last, and the draft note at the bottom is still present. > diff --git a/rust/kernel/drm/job_queue.rs b/rust/kernel/drm/job_queue.rs > new file mode 100644 > index 0000000000000..063a04d684b14 > --- /dev/null > +++ b/rust/kernel/drm/job_queue.rs [ ... ] > +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) =3D=3D 1 { > + // The drop-order of JobQueue ensures that all DependencyWak= ers are > + // cancelled before the JobQueue itself is dropped. > + self.jq.check_start_run_worker(); > + } > + } > +} [Severity: Medium] Can RunWorker::run() invoke T::run_job() on weakly-ordered architectures before memory writes from dependency fence completions are visible? DependencyWaker::on_signal() and Job::submit() decrement nr_of_deps with Relaxed ordering, and RunWorker::run() loads nr_of_deps with Relaxed orderi= ng before dropping the spinlock and calling T::run_job(). If RunWorker::run() is already looping when the last dependency signals, or if a dependency signals before Job::submit() performs the final decrement to 0, nothing establishes a happens-before relationship between the fence completion and T::run_job(). Using Release on the fetch_sub() calls paired with Acquire on the load in RunWorker::run() would synchronize all dependency completions. [ ... ] > +impl ScopedWorkItem for RunWorker<'_, '_, = T> { > + fn run(work: &ScopedWork) { > + // Synchronize against revocation in `JobQueue::drop`. > + let _lock_guard =3D work.srcu.read_lock(); > + if work.parked.load(Relaxed) { > + return; > + } > + > + let mut jq =3D work.inner.lock(); > + > + while let Some(job) =3D jq.waiting_jobs.pop_front() { [Severity: Medium] If JobQueue::park() or JobQueue::drop() runs while T::run_job() is executing unlocked inside this loop, doesn't RunWorker::run() keep submitting the remaining ready jobs in waiting_jobs? work.parked is only checked once before the loop, while the SRCU read lock is held across all iterations. > + // While we didn't hold the lock below for `run_job()`, it c= ould be > + // that a new job entered the waiting list which has an unfu= lfilled > + // dependency. > + if job.nr_of_deps.load(Relaxed) > 0 { > + jq.waiting_jobs.push_front(job); > + break; > + } > + let new_capacity =3D jq.capacity as i64 - i64::from(job.cost= ); > + if new_capacity < 0 { > + jq.waiting_jobs.push_front(job); > + break; > + } > + *jq.as_mut().project().capacity =3D new_capacity as u64; [Severity: Medium] Can casting jq.capacity to i64 wrap negative or underflow when JobQueue::ne= w() is given a capacity above i64::MAX as u64? Using jq.capacity.checked_sub(u64::from(job.cost)) avoids the signed conversion. > + > + let fence =3D job.fence.as_ref().expect("fence not present"); > + let seqno =3D fence.as_fence().seqno(); > + let runnable_job =3D JobRunnable { > + seqno, > + data: fence.deref(), > + }; > + > + drop(jq); // Release lock. > + T::run_job(work.fctx, runnable_job); > + jq =3D work.inner.lock(); // Retake lock. > + > + // run_job() above needs to be called without lock protectio= n. Thus, > + // it is possible that the driver instructed completion righ= t before > + // we could re-take the lock. > + if jq.last_completed >=3D seqno { > + *jq.as_mut().project().capacity +=3D u64::from(job.cost); > + fence.signal(Ok(())); > + > + // Drop here directly if possible. Otherwise, let the Dr= opWorker > + // take care of it. > + if core::mem::needs_drop::() { > + 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 while T::run_job() is running unlocked, does pushing it onto jq.drop_list here le= ave the job and its resources stranded until a later completion or queue teardo= wn? RunWorker / DropWorker T::run_job() complete_jobs_up_to_seqno(seqno) *jq.last_completed =3D seqno check_start_drop_worker() DropWorker::run() pop_front() -> None jq =3D work.inner.lock() if jq.last_completed >=3D seqno jq.drop_list.push_back(job) <-- drop_worker never re-scheduled > + > +struct DropWorker<'a, 'fctx, T: JobQueueOps + Send + Sync> { > + inner: &'a SpinLockIrq>, > +} > + > +impl ScopedWorkItem for DropWorker<'_, '_,= T> { > + fn run(work: &ScopedWork) { > + let mut jq_locked =3D work.inner.lock(); > + > + while let Some(job) =3D jq_locked.drop_list.pop_front() { > + drop(jq_locked); > + drop(job); > + jq_locked =3D work.inner.lock(); > + } > + } > +} [Severity: High] When DropWorker::run() or JobQueue::drop() drops a job, does T::JobDataType= 's destructor ever run? JobQueue::new_job() places the payload in DriverFenceData::data via FenceContext::new_fence_allocation(), so dropping the job relies on DriverFence::drop() introduced in commit "rust: DmaFence: Replace call_rcu() with RcuState": 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 refer= ence // from when this `DriverFence` was created. unsafe { bindings::dma_fence_put(self.as_raw()) }; } self.data in DriverFence is a NonNull> pointer wrapp= er rather than (*self.data.as_ptr()).data, so drop_in_place(&raw mut self.data) is a no-op and dma_fence_put() frees the backing memory without running the payload's Drop implementation. [ ... ] > + /// Give the unique identifier for this JobQueue. > + /// > + /// This corresponds to the JobQueue's underlying [`FenceContext`]. > + /// It is unique for all JobQueues in the entire system. > + pub fn id(&self) -> u64 { > + self.fctx.id() > + } [ ... ] > + /// Prevent the JobQueue from further calling [`JobQueueOps::run_job= ()`]. > + /// > + /// The JobQueue will not call [`JobQueueOps::run_job()`] anymore af= ter this > + /// function returned, until you call [`JobQueue:unpark()`]. [Severity: Low] This doc comment has a broken single-colon intra-doc link ([`JobQueue:unpark()`]), and the [`dma_buf::DriverFence`] and [`dma_buf::Fence`] links earlier in this file do not resolve because dma_buf is not in scope. Also, small wrapper and accessor functions in the kernel crate (JobQueue::id, JobQueue::deref, JobQueue::set_error, DriverDataWrappe= r's methods, and UniqueList's methods) should be marked #[inline]. [ ... ] > + pub fn complete_jobs_up_to_seqno(&self, seqno: u64) { > + self.with_inner(|inner| { > + let needs_drop =3D core::mem::needs_drop::(); > + let mut jq_locked =3D inner.lock(); > + let mut jq =3D jq_locked.as_mut().project(); > + > + if seqno < *jq.last_completed { > + return; > + } > + > + while let Some(mut job) =3D 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 +=3D u64::from(job.cost); > + > + let fence =3D job.fence.as_ref().expect("fence not prese= nt"); > + fence.signal(Ok(())); > + > + if needs_drop { > + jq.drop_list.push_back(job); > + } > + // Otherwise, job is dropped here directly. Its fence wo= n't > + // perform an RCU grace period wait. > + } [Severity: High] Can calling fence.signal(Ok(())) under inner.lock() (and in RunWorker::run(= )) deadlock if a fence callback submits or completes a job? complete_jobs_up_to_seqno() jq_locked =3D inner.lock() fence.signal(Ok(())) dma_fence_signal_locked() FenceCallback::on_signal() Job::submit() inner.lock() <-- spins forever Even across different queues, signaling a fence under inner.lock() nests dma_fence->lock inside JobQueue::inner's lockdep class, while a fence callb= ack calling Job::submit() or complete_jobs_up_to_seqno() acquires JobQueue::inn= er under dma_fence->lock. When !needs_drop, dropping job at the end of the lo= op body also runs under SpinLockIrq, acquiring each dependency fence's lock via dma_fence_remove_callback() and freeing the job and dependency allocations with interrupts disabled. > + > + // 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 =3D seqno; > + drop(jq_locked); > + > + self.check_start_run_worker(); > + if needs_drop { > + self.check_start_drop_worker(); > + } > + }) > + } [Severity: Low] As reported by Younes Akhouayri on v1 [2], if seqno =3D=3D *jq.last_complet= ed (or when no jobs in running_jobs had a sequence number <=3D seqno), this does n= ot return early and unconditionally schedules both run_worker and drop_worker, while the raced completion path in RunWorker::run() pushes to jq.drop_list without calling check_start_drop_worker(). In addition, RunWorker::run() keeps job.dependencies populated after nr_of_deps reaches 0, holding the Dependency allocations and upstream fence references until the job finishes= on hardware. > + > + /// Set the error with which this JobQueue will signal all running j= obs' > + /// fences. (The jobs on the waiting_list will all complete with ECA= NCELED > + /// as their error) > + pub fn set_error(mut self: Pin<&mut Self>, error: Error) { > + *self.as_mut().project().err =3D error; > + } > +} > + > +#[pinned_drop] > +impl PinnedDrop for JobQueue { > + fn drop(self: Pin<&mut Self>) { > + // Prevent the RunWorker from moving more jobs to `running_jobs`. > + self.park(); > + self.with_inner(|inner| { > + let mut inner_locked =3D inner.lock(); > + let drop_list =3D core::mem::take(&mut inner_locked.drop_lis= t); > + let waiting_jobs =3D core::mem::take(&mut inner_locked.waiti= ng_jobs); > + let mut running_jobs =3D core::mem::take(&mut inner_locked.r= unning_jobs); > + > + drop(inner_locked); > + > + while let Some(job) =3D running_jobs.pop_front() { > + let fence =3D job.fence.as_ref().expect("fence not prese= nt"); > + fence.signal(Err(self.err)); > + } > + > + drop(drop_list); > + drop(waiting_jobs); > + drop(running_jobs); > + }) > + } > +} [Severity: High] Does JobQueue::drop() guarantee that all transmuted &JobQueue and &FenceContext references in jobs are gone before JobQueue's fields are destroyed? CPU0 CPU1 (DropWorker) DropWorker::run() job =3D jq_locked.drop_list.pop_= front() drop(jq_locked) JobQueue::drop() self.park() take(&mut inner_locked.drop_list) ... // struct fields drop: // drop_queue dropped before drop_worker // fctx dropped drop(job) <-- accesses dangling= fctx/jq There are three related lifetime and teardown issues here: 1. In struct JobQueue, drop_queue and run_queue (OwnedQueue) are declared before drop_worker and run_worker (ScopedWork), so the workqueues are destroyed before the ScopedWork items that run on them are cancelled and dropped (and #[uses('_: invariant, 'parked: invariant, 'srcu: invariant)] is attached to drop_worker instead of run_worker). 2. Because Job::submit() erases the shared borrow of JobQueue, set_error() = and PinnedDrop::drop() take Pin<&mut Self> while DependencyWaker callbacks in waiting_jobs still hold &JobQueue; a dependency fence signaling before drop(waiting_jobs) dereferences self.jq concurrently with Pin<&mut Self> and calls check_start_run_worker() after self.park() has returned. 3. If a caller forgets an unsubmitted Job after Job::add_dependency(), fctx.nr_of_unsignaled_fences was never incremented (new_fence() is only called in submit()), so dropping JobQueue leaves the leaked DependencyWaker registered with a dangling &JobQueue. [Severity: Medium] Are waiting_jobs' fences ever signaled with Err(ECANCELED) before drop(waiting_jobs)? Job::submit() already creates each job's DriverFence before pushing it to waiting_jobs, so dropping waiting_jobs without signaling them triggers DriverFence::drop()'s unsignaled error path (logging "DriverFence drops unsignaled. Danger of memory corruption!" and calling an unconditional synchronize_rcu() for every waiting job). Also, in the running_jobs loop above, each popped job is dropped at the end of the loop body right after fence.signal(Err(self.err)), which waits for a full RCU grace period per running job sequentially instead of signaling all fences before dropping the jobs. [1] https://lore.kernel.org/r/kw-1794059738.215694434-299782-0@younes.io [2] https://lore.kernel.org/r/kw-1794059738.215694434-299782-0@younes.io --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261202113914.1660= 411-2-phasta@kernel.org?part=3D8