accel/rocket: Fix file close and reset with a job on the NPU
Subsystems (5)
cover accel/rocket: Fix file close and reset with a job on the NPU
Patch 2 is the same change as Midgy Balon's RK3568 RFC; see the patch.
Tested on an Orange Pi 5 (RK3588), 7.3 with both patches, KASAN: 70 SIGKILLs during inference, no job timeout (before: 12 in 10 kills); 10 faulting jobs, no IOMMU stall timeout or warning (before: 75 and 10), also with the next-20261127 rockchip-iommu.c.
Developed with Claude (Anthropic) as a coding assistant; I reviewed and tested it.
---
Arun Iyengar (2):
accel/rocket: Wait for in-flight jobs before releasing the file's BOs accel/rocket: Reset the core before detaching its IOMMU
drivers/accel/rocket/rocket_drv.c | 28 ++++++++++++++++++++++++++-- drivers/accel/rocket/rocket_drv.h | 2 ++ drivers/accel/rocket/rocket_job.c | 24 ++++++++++++++++++------ 3 files changed, 46 insertions(+), 8 deletions(-)
---
base-commit: 9f333c38eac95cef6e1d54681b010c6531a7dcb2
change-id: 20261130-rocket-fixes-e1438ca23bee
Best regards,
Signature · 1 line
Arun Iyengar <[email protected]>
PATCH 1/2 accel/rocket: Wait for in-flight jobs before releasing the file's BOs
drm_release() drops the GEM handles before calling postclose, where the scheduler entity is torn down. Jobs already on the NPU can use any BO of the file by address, not only the ones they list, so a killed process gets its BOs unmapped under a running job. The job then times out.
Tear the entity down from the release fop instead, and wait for the file's last job to finish there.
--- drivers/accel/rocket/rocket_drv.c | 28 ++++++++++++++++++++++++++-- drivers/accel/rocket/rocket_drv.h | 2 ++ drivers/accel/rocket/rocket_job.c | 14 ++++++++++++-- 3 files changed, 40 insertions(+), 4 deletions(-)
drivers/accel/rocket/rocket_drv.c +26 -2
@@ -3,6 +3,7 @@#include <drm/drm_accel.h> #include <drm/drm_drv.h> +#include <drm/drm_file.h> #include <drm/drm_gem.h> #include <drm/drm_ioctl.h> #include <drm/rocket_accel.h>
@@ -122,7 +123,6 @@ rocket_postclose(struct drm_device *dev, struct drm_file *file) {struct rocket_file_priv *rocket_priv = file->driver_priv; -rocket_job_close (rocket_priv);rocket_vm_put (rocket_priv->vm);kfree (rocket_priv);module_put (THIS_MODULE );
@@ -138,7 +138,31 @@ static const struct drm_ioctl_desc rocket_drm_driver_ioctls[] = { ROCKET_IOCTL(FINI_BO, fini_bo), }; -DEFINE_DRM_ACCEL_FOPS (rocket_accel_driver_fops); +/* +* drm_release() drops the GEM handles before calling postclose. Stop the +* file's jobs first, as they still use those BOs. +*/ +static int rocket_release (struct inode *inode,struct file *filp) +{ +struct drm_file *file = filp->private_data; + +rocket_job_close (file->driver_priv); + +return drm_release (inode, filp); +} + +static const struct file_operations rocket_accel_driver_fops = { + .owner =THIS_MODULE , + .open = accel_open, + .release = rocket_release, + .unlocked_ioctl = drm_ioctl, + .compat_ioctl = drm_compat_ioctl, + .poll = drm_poll, + .read = drm_read, + .llseek = noop_llseek, + .mmap = drm_gem_mmap, + .fop_flags =FOP_UNSIGNED_OFFSET , +};/* * Rocket driver version:
drivers/accel/rocket/rocket_drv.h +2 -0
@@ -26,6 +26,8 @@ struct rocket_file_priv {struct drm_gpu_scheduler **scheds;struct drm_sched_entity sched_entity; + +struct dma_fence *last_job_fence; };struct rocket_vm *rocket_vm_get (struct rocket_file_priv *rocket_priv);
drivers/accel/rocket/rocket_job.c +12 -2
@@ -210,7 +210,8 @@ static void rocket_attach_object_fences(struct drm_gem_object **bos,dma_resv_add_fence (bos[i]->resv, fence, usage); } -static int rocket_job_push (struct rocket_job *job) +static int rocket_job_push (struct rocket_file_priv *file_priv, +struct rocket_job *job) {struct rocket_device *rdev = job->rdev;struct drm_gem_object **bos;
@@ -246,6 +247,9 @@ static int rocket_job_push(struct rocket_job *job)kref_get (&job->refcount);/* put by scheduler job completion */ +dma_fence_put (file_priv->last_job_fence); + file_priv->last_job_fence =dma_fence_get (job->inference_done_fence); +drm_sched_entity_push_job (&job->base); }
@@ -585,6 +589,12 @@ void rocket_job_close(struct rocket_file_priv *rocket_priv)struct drm_sched_entity *entity = &rocket_priv->sched_entity;drm_sched_entity_destroy (entity); + +if (rocket_priv->last_job_fence) { +dma_fence_wait (rocket_priv->last_job_fence, false); +dma_fence_put (rocket_priv->last_job_fence); + } +kfree (rocket_priv->scheds); }
@@ -641,7 +651,7 @@ static int rocket_ioctl_submit_job(struct drm_device *dev, struct drm_file *file rjob->out_bo_count = job->out_bo_handle_count; - ret =rocket_job_push (rjob); + ret =rocket_job_push (file_priv, rjob);if (ret) goto out_cleanup_job;
Signature · 1 line
2.55.0
PATCH 2/2 accel/rocket: Reset the core before detaching its IOMMU
In reply to Arun IyengarMidgy Balon posted the same change in an RK3568 RFC series [1].
A timed-out job leaves the NPU master hung on the bus. The IOMMU then cannot enter stall mode, the detach fails and the IOMMU core warns:
rk_iommu fdab9000.iommu: Enable stall request timed out, status: 0x0001cb
WARNING: drivers/iommu/iommu.c:157 at __iommu_group_set_core_domain+0x5c/0x98
Reset the core first, as the vendor driver does.
--- drivers/accel/rocket/rocket_job.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/accel/rocket/rocket_job.c +6 -4
@@ -427,15 +427,17 @@ rocket_reset(struct rocket_core *core, struct drm_sched_job *bad)*/ scoped_guard(mutex, &core->job_lock) { put_job_ref = core->in_flight_job; - -iommu_detach_group (NULL , core->iommu_group); - core->in_flight_job =NULL ; } -/* Proceed with reset now. */ +/* +* Reset before detaching: the IOMMU cannot stall while a hung job +* keeps the bus busy, and then the detach fails. +*/ rocket_core_reset (core); +iommu_detach_group (NULL , core->iommu_group); +if (put_job_ref)pm_runtime_put_autosuspend (core->dev);
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 2/2] accel/rocket: Reset the core before detaching its IOMMU
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] accel/rocket: use-after-free and…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] accel/rocket: use-after-free and NULL pointer dereference in rocket_core_fini() [1]
--
commit 3c0ae1c947c1bfa587406e1f23468f81ff1b2fca
Author: Arun Iyengar <[email protected]>
accel/rocket: Reset the core before detaching its IOMMU
This commit moves the rocket_core_reset() call in rocket_reset() ahead of iommu_detach_group() so that a hung NPU job does not block the IOMMU from entering stall mode during detach.
Quoted 15 lines
drivers/accel/rocket/rocket_job.c +0
-4@@ -427,15 +427,17 @@ rocket_reset(struct rocket_core *core, struct drm_sched_job *bad)*/ scoped_guard(mutex, &core->job_lock) { put_job_ref = core->in_flight_job; - -iommu_detach_group (NULL , core->iommu_group); - core->in_flight_job =NULL ; } -/* Proceed with reset now. */
drivers/accel/rocket/rocket_job.c rocket_reset(struct rocket_core *core, struct drm_sched_job *bad) · line 442
Quoted 10 lines
drivers/accel/rocket/rocket_job.c +6
-0+/* +* Reset before detaching: the IOMMU cannot stall while a hung job +* keeps the bus busy, and then the detach fails. +*/ rocket_core_reset (core); +iommu_detach_group (NULL , core->iommu_group); +if (put_job_ref)pm_runtime_put_autosuspend (core->dev);
[Severity: Critical]
This problem wasn't introduced by this patch, but can rocket_reset() hit a NULL pointer dereference in iommu_detach_group() and operate on disabled runtime PM during teardown [1]?
In rocket_core_fini(), runtime PM is disabled and core->iommu_group is released and cleared before rocket_job_fini() stops the scheduler:
drivers/accel/rocket/rocket_core.c:rocket_core_fini() {
pm_runtime_dont_use_autosuspend(core->dev);
pm_runtime_disable(core->dev);
iommu_group_put(core->iommu_group);
core->iommu_group = NULL;
rocket_job_fini(core);
}If an in-flight job times out before or during rocket_job_fini(), rocket_reset() passes a NULL core->iommu_group to iommu_detach_group() and calls pm_runtime_put_autosuspend() after runtime PM has been disabled. Calling rocket_job_fini() first in rocket_core_fini() would avoid this.
[1] https://sashiko.dev/#/bug/linux-ef3ef432-292a-4ee1-addd-1b0440571e2d
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH 1/2] accel/rocket: Wait for in-flight jobs before releasing the file's BOs
In reply to Arun IyengarThank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] accel/rocket: ignored return value in…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] accel/rocket: ignored return value in rocket_ioctl_submit() [1] --
commit 56d4424db2bb4cd4fc68f8a260274639d95da94e
Author: Arun Iyengar <[email protected]>
accel/rocket: Wait for in-flight jobs before releasing the file's BOs
This commit tears down the scheduler entity and waits for the file's last submitted job in the release file operation before drm_release() drops the file's GEM handles.
Quoted 4 lines
drivers/accel/rocket/rocket_job.c +0
-0
[ ... ]
drivers/accel/rocket/rocket_job.c static int rocket_ioctl_submit_job(struct drm_device *dev, struct drm_file *file · line 657
Quoted 9 lines
drivers/accel/rocket/rocket_job.c +1
-1@@ -641,7 +651,7 @@ static int rocket_ioctl_submit_job(struct drm_device *dev, struct drm_file *file rjob->out_bo_count = job->out_bo_handle_count; - ret =rocket_job_push (rjob); + ret =rocket_job_push (file_priv, rjob);if (ret) goto out_cleanup_job;
[Severity: High]
This problem wasn't introduced by this patch, but does rocket_ioctl_submit() ignore the return value of rocket_ioctl_submit_job() [1]?
drivers/accel/rocket/rocket_job.c:rocket_ioctl_submit() {
...
for (i = 0; i < args->job_count; i++)
rocket_ioctl_submit_job(dev, file, &jobs[i]);exit:
kvfree(jobs); return ret; }
When rocket_ioctl_submit_job() fails (for example, due to an invalid GEM handle or an allocation failure in rocket_job_push()), ret stays 0 and the ioctl reports success to userspace for a job that was never queued.
[1] https://sashiko.dev/#/bug/linux-78e2eefa-f18b-4b54-bd1e-b3dcf1458b36
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1