accel/qaic: drain the control workqueue before the device is freed
Subsystems (3)
PATCH 1/1 accel/qaic: drain the control workqueue before the device is freed
qaic_pci_remove() tears the MHI controller down and destroys the DRM device, but nothing drains qdev->cntl_wq. qdev itself is allocated with devm_kzalloc_flex() and is freed as soon as the PCI device is unbound, while cntl_wq is a DRM-managed resource that survives until the last open file drops its reference to the DRM device.
resp_work is queued from qaic_mhi_dl_xfer_cb() and resp_worker() dereferences qdev, so a work item that is still queued, or still running, when the driver instance goes away touches freed memory.
mhi_power_down() moves the controller to the shutdown state, disables the interrupt and flushes the state worker that destroys the channel devices, and mhi_unregister_controller() removes the controller, so no data event can reach qaic_mhi_dl_xfer_cb() once qaic_mhi_free_controller() has returned. Flushing the workqueue after it therefore covers every work item that can still be queued.
---
Prepared against v7.4-rc1-1969-g666539eecdf4.
AI assistance: the analysis, the change and its review notes were prepared with Claude (Anthropic) as a coding assistant. The author reviewed the reasoning and takes responsibility for the content.
Testing: compile tested only (arm64, gcc 11.4). No QAIC hardware was available, so the teardown race was not run.
drivers/accel/qaic/qaic_drv.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/accel/qaic/qaic_drv.c +9 -0
@@ -625,6 +625,15 @@ static void qaic_pci_remove(struct pci_dev *pdev)qaic_dev_reset_clean_local_state (qdev);qaic_mhi_free_controller (qdev->mhi_cntrl, link_up); + +/* +* The control channel is gone, so nothing can queue resp_work any +* more. Drain it before qdev is freed: the workqueue belongs to the +* DRM device and may outlive this driver instance, and resp_worker() +* dereferences qdev. +*/ +flush_workqueue (qdev->cntl_wq); +qaic_destroy_drm_device (qdev,QAIC_NO_PARTITION ); }
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] accel/qaic: Fixes trailer cites c501ca23a6a3 instead of 22ec9a3b73dd --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] accel/qaic: Fixes trailer cites c501ca23a6a3 instead of 22ec9a3b73dd
--
commit 4e36def68e52df8a1a1cb9f08c76e856b9e13fb0
Author: Yaohan Li <[email protected]>
Quoted 19 lines
accel/qaic: drain the control workqueue before the device is freed
qaic_pci_remove()tears the MHI controller down and destroys the DRM device, but nothing drainsqdev->cntl_wq. qdev itself is allocated withdevm_kzalloc_flex()and is freed as soon as the PCI device is unbound, whilecntl_wqis a DRM-managed resource that survives until the last open file drops its reference to the DRM device.
resp_workis queued fromqaic_mhi_dl_xfer_cb()andresp_worker()dereferences qdev, so a work item that is still queued, or still running, when the driver instance goes away touches freed memory.
mhi_power_down()moves the controller to the shutdown state, disables the interrupt and flushes the state worker that destroys the channel devices, andmhi_unregister_controller()removes the controller, so no data event can reachqaic_mhi_dl_xfer_cb()onceqaic_mhi_free_controller()has returned. Flushing the workqueue after it therefore covers every work item that can still be queued.
Quoted 2 lines
Fixes c501ca23a6a3 ("accel/qaic: Add uapi and core driver file")Signed-off-by Yaohan Li <[email protected]>
[Severity: Low]
In c501ca23a6a3 ("accel/qaic: Add uapi and core driver file"), qaic_pci_remove() called cleanup_qdev(), which destroyed qdev->cntl_wq synchronously via destroy_workqueue() before qdev was freed. The lifetime mismatch was introduced in 22ec9a3b73dd ("accel/qaic: Leverage DRM managed APIs to release resources"), which switched qdev->cntl_wq to qaicm_wq_init() and dropped cleanup_qdev() from qaic_pci_remove().
Should the Fixes tag reference that commit instead:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 1/1 accel/qaic: drain the control workqueue before the device is freed
In reply to Yaohan Liqaic_pci_remove() tears the MHI controller down and destroys the DRM device, but nothing drains qdev->cntl_wq. qdev itself is allocated with devm_kzalloc_flex() and is freed as soon as the PCI device is unbound, while cntl_wq is a DRM-managed resource that survives until the last open file drops its reference to the DRM device.
resp_work is queued from qaic_mhi_dl_xfer_cb() and resp_worker() dereferences qdev, so a work item that is still queued, or still running, when the driver instance goes away touches freed memory.
mhi_power_down() moves the controller to the shutdown state, disables the interrupt and flushes the state worker that destroys the channel devices, and mhi_unregister_controller() removes the controller, so no data event can reach qaic_mhi_dl_xfer_cb() once qaic_mhi_free_controller() has returned. Flushing the workqueue after it therefore covers every work item that can still be queued.
---
Prepared against v7.4-rc1-1969-g666539eecdf4.
Compile tested only: arm64, aarch64-linux-gnu-gcc 11.4, CONFIG_DRM_ACCEL=y and CONFIG_DRM_ACCEL_QAIC=m. No hardware was available, so the removal path was not exercised and no race injection was run.
AI assistance: prepared with Claude (Anthropic) as a coding assistant; the author reviewed the reasoning and takes responsibility for the content.
Changes in v2:
- Correct Fixes: to 22ec9a3b73dd, the commit that moved the device release to DRM managed resources. No code change.
--- drivers/accel/qaic/qaic_drv.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/accel/qaic/qaic_drv.c +9 -0
@@ -625,6 +625,15 @@ static void qaic_pci_remove(struct pci_dev *pdev)qaic_dev_reset_clean_local_state (qdev);qaic_mhi_free_controller (qdev->mhi_cntrl, link_up); + +/* +* The control channel is gone, so nothing can queue resp_work any +* more. Drain it before qdev is freed: the workqueue belongs to the +* DRM device and may outlive this driver instance, and resp_worker() +* dereferences qdev. +*/ +flush_workqueue (qdev->cntl_wq); +qaic_destroy_drm_device (qdev,QAIC_NO_PARTITION ); }
Signature · 1 line
2.34.1
Jeff Hugo
maintainerQuoted 47 lines · Yaohan Li
qaic_pci_remove()tears the MHI controller down and destroys the DRM device, but nothing drainsqdev->cntl_wq. qdev itself is allocated withdevm_kzalloc_flex()and is freed as soon as the PCI device is unbound, whilecntl_wqis a DRM-managed resource that survives until the last open file drops its reference to the DRM device.
resp_workis queued fromqaic_mhi_dl_xfer_cb()andresp_worker()dereferences qdev, so a work item that is still queued, or still running, when the driver instance goes away touches freed memory.
mhi_power_down()moves the controller to the shutdown state, disables the interrupt and flushes the state worker that destroys the channel devices, andmhi_unregister_controller()removes the controller, so no data event can reachqaic_mhi_dl_xfer_cb()onceqaic_mhi_free_controller()has returned. Flushing the workqueue after it therefore covers every work item that can still be queued.Fixes 22ec9a3b73dd ("accel/qaic: Leverage DRM managed APIs to release resources")Signed-off-by Yaohan Li <[email protected]>---
Prepared against v7.4-rc1-1969-g666539eecdf4.Compile tested only: arm64, aarch64-linux-gnu-gcc 11.4,
CONFIG_DRM_ACCEL=yandCONFIG_DRM_ACCEL_QAIC=m. No hardware was available, so the removal path was not exercised and no race injection was run.AI assistance: prepared with Claude (Anthropic) as a coding assistant; the author reviewed the reasoning and takes responsibility for the content.
Changes in v2:
- Correct Fixes: to22ec9a3b73dd, the commit that moved the device release to DRM managed resources. No code change.--- drivers/accel/qaic/qaic_drv.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/accel/qaic/qaic_drv.c +2
-0@@ -625,6 +625,15 @@ static void qaic_pci_remove(struct pci_dev *pdev)qaic_dev_reset_clean_local_state (qdev);qaic_mhi_free_controller (qdev->mhi_cntrl, link_up); + +/*
drivers/accel/qaic/qaic_drv.c static void qaic_pci_remove(struct pci_dev *pdev) · line 639
Quoted 10 lines
drivers/accel/qaic/qaic_drv.c +7
-0+* The control channel is gone, so nothing can queue resp_work any +* more. Drain it before qdev is freed: the workqueue belongs to the +* DRM device and may outlive this driver instance, and resp_worker() +* dereferences qdev. +*/ +flush_workqueue (qdev->cntl_wq); +qaic_destroy_drm_device (qdev,QAIC_NO_PARTITION ); }
It seems like this should belong in the MHI channel remove.
Also, why is the cntl_wq unique in this regard?
Hi Jeff,
Thanks for the review. I agree the MHI channel remove callback is a much better place to drain the control response work than the PCI remove path.
Looking over the rest of the driver, I also missed two related workqueues in my original analysis: qts_wq and ssr_wq. Their MHI remove callbacks currently unprepare the channels without draining those workqueues, while the workers can still touch qdev and channel-related state, so those paths need to be checked as well.
Rather than reworking workqueue ownership across the driver, I'd keep the existing DRM-managed allocation and synchronize the work at each channel's teardown. Before sending a revision, though, there is one ordering detail I need to sort out: qaic_dev_reset_clean_local_state() can run ahead of MHI channel removal and releases SSR-related state, so I want to make sure an SSR worker cannot race with that cleanup.
I'll go through the producer shutdown and teardown ordering for all three queues and keep any follow-up narrowly scoped. I've only compile-tested the patch so far and don't have QAIC hardware, so I don't want to claim hardware validation.
Thanks again for pointing this out,
Yaohan