accel/qaic: Synchronize access to dbc->bo_lists
Subsystems (3)
PATCH 1/1 accel/qaic: Synchronize access to dbc->bo_lists
Each struct dma_bridge_chan maintains a bo_lists linked list of all sliced BOs attached to that DBC, which is modified in qaic_attach_slice_bo_ioctl() via list_add_tail() and in detach_slice_bo() (called from qaic_detach_slice_bo_ioctl() and release_dbc()) via list_del().
However, dbc->bo_lists is not protected by any lock. Both qaic_attach_slice_bo_ioctl() and qaic_detach_slice_bo_ioctl() only hold the per-BO bo->lock mutex and shared SRCU read locks (usr->qddev_lock, qdev->dev_lock, and dbc->ch_lock). Two threads from the same user concurrently attaching or detaching different BOs on the same DBC can race on dbc->bo_lists, causing linked-list corruption and potential use-after-free when release_dbc() later iterates dbc->bo_lists.
Add a dedicated spinlock_t bo_lists_lock to struct dma_bridge_chan, initialize it in create_qdev(), and hold it around list_add_tail() in qaic_attach_slice_bo_ioctl() and list_del() in detach_slice_bo().
Found by manual code inspection.
--- drivers/accel/qaic/qaic.h | 2 ++ drivers/accel/qaic/qaic_data.c | 9 +++++++++ drivers/accel/qaic/qaic_drv.c | 1 + 3 files changed, 12 insertions(+)
drivers/accel/qaic/qaic.h +2 -0
@@ -125,6 +125,8 @@ struct dma_bridge_chan {* woken up */ wait_queue_head_t dbc_release; +/* Synchronizes access to bo_lists */ +spinlock_t bo_lists_lock;/* Head of list where each node is a bo associated with this DBC */ struct list_head bo_lists;/* The irq line for this DBC. Used for polling */
drivers/accel/qaic/qaic_data.c +9 -1
@@ -1089,7 +1089,9 @@ int qaic_attach_slice_bo_ioctl(struct drm_device *dev, void *data, struct drm_fidma_sync_sgtable_for_cpu (&qdev->pdev->dev, bo->sgt, args->hdr.dir); bo->sliced = true; +spin_lock (&dbc->bo_lists_lock);list_add_tail (&bo->bo_list, &bo->dbc->bo_lists); +spin_unlock (&dbc->bo_lists_lock);srcu_read_unlock (&dbc->ch_lock, rcu_id);mutex_unlock (&bo->lock);kfree (slice_ent);
@@ -1903,10 +1905,16 @@ int qaic_perf_stats_bo_ioctl(struct drm_device *dev, void *data, struct drm_filestatic void detach_slice_bo (struct qaic_device *qdev,struct qaic_bo *bo) { +struct dma_bridge_chan *dbc = bo->dbc; +qaic_free_slices_bo (bo);qaic_unprepare_bo (qdev, bo);qaic_init_bo (bo, true); -list_del (&bo->bo_list); +if (dbc) { +spin_lock (&dbc->bo_lists_lock); +list_del (&bo->bo_list); +spin_unlock (&dbc->bo_lists_lock); + }drm_gem_object_put (&bo->base); }
drivers/accel/qaic/qaic_drv.c +1 -0
@@ -477,6 +477,7 @@ static struct qaic_device *create_qdev(struct pci_dev *pdev,if (ret)return NULL ;init_waitqueue_head (&qdev->dbc[i].dbc_release); +spin_lock_init (&qdev->dbc[i].bo_lists_lock);INIT_LIST_HEAD (&qdev->dbc[i].bo_lists); ret =drmm_mutex_init (drm, &qdev->dbc[i].req_lock);if (ret)
Signature · 1 line
2.43.0
PATCH 1/1 accel/qaic: Hold bo->lock when checking bo->sliced and bo->dbc in qaic_wait_bo_ioctl()
In qaic_wait_bo_ioctl(), bo->sliced and bo->dbc->id are read without holding bo->lock.
Concurrently, qaic_detach_slice_bo_ioctl() calls detach_slice_bo(), which invokes qaic_unprepare_bo() (setting bo->dbc = NULL) before qaic_init_bo(bo, true) clears bo->sliced = false. A thread in qaic_wait_bo_ioctl() can observe bo->sliced == true and then dereference bo->dbc->id after bo->dbc has been cleared to NULL, triggering a NULL pointer dereference.
Look up the GEM object and validate bo->sliced and bo->dbc->id under bo->lock before acquiring dbc->ch_lock (preserving lock ordering with qaic_attach_slice_bo_ioctl() and qaic_detach_slice_bo_ioctl()), then release bo->lock before sleeping in wait_for_completion_interruptible_timeout().
Found by manual code inspection.
---
drivers/accel/qaic/qaic_data.c +20 -21
@@ -1754,6 +1754,23 @@ int qaic_wait_bo_ioctl(struct drm_device goto unlock_dev_srcu; } + obj =drm_gem_object_lookup (file_priv, args->handle); +if (!obj) { + ret = -ENOENT ; + goto unlock_dev_srcu; + } + + bo =to_qaic_bo (obj); + ret =mutex_lock_interruptible (&bo->lock); +if (ret) + goto put_obj; +if (!bo->sliced || bo->dbc->id != args->dbc_id) { +mutex_unlock (&bo->lock); + ret = -EINVAL ; + goto put_obj; + } +mutex_unlock (&bo->lock); + dbc = &qdev->dbc[args->dbc_id]; rcu_id =srcu_read_lock (&dbc->ch_lock);
@@ -1767,30 +1784,12 @@ int qaic_wait_bo_ioctl(struct drm_device goto unlock_ch_srcu; } - obj =drm_gem_object_lookup (file_priv, args->handle); -if (!obj) { - ret = -ENOENT ; - goto unlock_ch_srcu; - } - - bo =to_qaic_bo (obj); - -if (!bo->sliced) { - ret = -EINVAL ; - goto put_obj; - } - -if (bo->dbc->id != dbc->id) { - ret = -EINVAL ; - goto put_obj; - } - timeout = args->timeout ? args->timeout : wait_exec_default_timeout_ms; timeout =msecs_to_jiffies (timeout); ret =wait_for_completion_interruptible_timeout (&bo->xfer_done, timeout);if (!ret) { ret = -ETIMEDOUT ; - goto put_obj; + goto unlock_ch_srcu; }if (ret >0 ) ret =0 ;
@@ -1801,10 +1800,10 @@ int qaic_wait_bo_ioctl(struct drm_deviceif (dbc->id == qdev->ssr_dbc) ret = -EPIPE ; -put_obj: -drm_gem_object_put (obj); unlock_ch_srcu:srcu_read_unlock (&dbc->ch_lock, rcu_id); +put_obj: +drm_gem_object_put (obj); unlock_dev_srcu:srcu_read_unlock (&qdev->dev_lock, qdev_rcu_id); unlock_usr_srcu:
[email protected]
AI reviewRe: [PATCH] accel/qaic: Hold bo->lock when checking bo->sliced and bo->dbc in qaic_wait_bo_ioctl()
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] accel/qaic: missing Fixes trailer for commit d48ea8691943 in qaic_wait_bo_ioctl() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] accel/qaic: missing Fixes trailer for commit d48ea8691943 in qaic_wait_bo_ioctl()
--
commit 24be64e6b07142c24440e91100eb34ff1186347a
Author: Bilal Hashim <[email protected]>
Quoted 18 lines
accel/qaic: Hold
bo->lockwhen checkingbo->slicedandbo->dbcinqaic_wait_bo_ioctl()In
qaic_wait_bo_ioctl(),bo->slicedandbo->dbc->idare read without holdingbo->lock.Concurrently,
qaic_detach_slice_bo_ioctl()callsdetach_slice_bo(), which invokesqaic_unprepare_bo()(settingbo->dbc = NULL) beforeqaic_init_bo(bo, true)clearsbo->sliced = false. A thread inqaic_wait_bo_ioctl()can observebo->sliced == trueand then dereferencebo->dbc->idafterbo->dbchas been cleared toNULL, triggering a NULL pointer dereference.Look up the GEM object and validate
bo->slicedandbo->dbc->idunderbo->lockbefore acquiringdbc->ch_lock(preserving lock ordering withqaic_attach_slice_bo_ioctl()andqaic_detach_slice_bo_ioctl()), then releasebo->lockbefore sleeping inwait_for_completion_interruptible_timeout().
Quoted 3 lines
Found by manual code inspection.
Signed-off-by Bilal Hashim <[email protected]>
[Severity: Low]
This fixes the unlocked checks of bo->sliced and bo->dbc->id added in qaic_wait_bo_ioctl() by commit d48ea8691943 ("accel/qaic: Harden checks in BO execute and wait IOCTLs"). Should this include:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] accel/qaic: missing Fixes trailer for dbc->bo_lists concurrency fix It also noticed 1 related…
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] accel/qaic: missing Fixes trailer for dbc->bo_lists concurrency fix
It also noticed 1 related pre-existing issue this patch does not introduce:
- [High] accel/qaic: missing dma_resv lock in qaic_prepare_import_bo() and qaic_unprepare_import_bo() [1]
--
commit 119c19ad9fdb0134dda384da400bc1108d8356d8
Author: Bilal Hashim <[email protected]>
Quoted 17 lines
accel/qaic: Synchronize access to
dbc->bo_listsEach
struct dma_bridge_chanmaintains abo_listslinked list of all sliced BOs attached to that DBC, which is modified inqaic_attach_slice_bo_ioctl()vialist_add_tail()and indetach_slice_bo()(called fromqaic_detach_slice_bo_ioctl()andrelease_dbc()) vialist_del().However,
dbc->bo_listsis not protected by any lock. Bothqaic_attach_slice_bo_ioctl()andqaic_detach_slice_bo_ioctl()only hold the per-BObo->lockmutex and shared SRCU read locks (usr->qddev_lock,qdev->dev_lock, anddbc->ch_lock). Two threads from the same user concurrently attaching or detaching different BOs on the same DBC can race ondbc->bo_lists, causing linked-list corruption and potential use-after-free whenrelease_dbc()later iteratesdbc->bo_lists.
Quoted 8 lines
Add a dedicated
spinlock_t bo_lists_locktostruct dma_bridge_chan, initialize it increate_qdev(), and hold it aroundlist_add_tail()inqaic_attach_slice_bo_ioctl()andlist_del()indetach_slice_bo().Found by manual code inspection.
Signed-off-by Bilal Hashim <[email protected]>
[Severity: Low]
Should this bug fix include Fixes tags for the commits that introduced the unlocked list insertion and removal on dbc->bo_lists?
Quoted 4 lines
drivers/accel/qaic/qaic_data.c +0
-0
[ ... ]
drivers/accel/qaic/qaic_data.c int qaic_perf_stats_bo_ioctl(struct drm_device *dev, void *data, struct drm_file · line 1912
Quoted 9 lines
drivers/accel/qaic/qaic_data.c +2
-0@@ -1903,10 +1905,16 @@ int qaic_perf_stats_bo_ioctl(struct drm_device *dev, void *data, struct drm_filestatic void detach_slice_bo (struct qaic_device *qdev,struct qaic_bo *bo) { +struct dma_bridge_chan *dbc = bo->dbc; +qaic_free_slices_bo (bo);qaic_unprepare_bo (qdev, bo);qaic_init_bo (bo, true);
[Severity: High]
This problem wasn't introduced by this patch, but do qaic_prepare_bo() and qaic_unprepare_bo() call dma_buf_map_sgt() and dma_buf_unmap_sgt() on imported DMA-BUFs without holding attach->dmabuf->resv [1]?
drivers/accel/qaic/qaic_data.c:qaic_prepare_import_bo() {
...
sgt = dma_buf_map_sgt(obj->import_attach, hdr->dir);
...
}drivers/accel/qaic/qaic_data.c:qaic_unprepare_import_bo() {
dma_buf_unmap_sgt(bo->base.import_attach, bo->sgt, bo->dir);
bo->sgt = NULL;
}Both dma_buf_map_sgt() and dma_buf_unmap_sgt() assert dma_resv_assert_held(attach->dmabuf->resv), whereas qaic_attach_slice_bo_ioctl() and the detach paths only hold bo->lock and SRCU read locks. This triggers a lockdep warning and leaves buffer pinning and exporter map/unmap callbacks unsynchronized across drivers sharing the DMA-BUF.
In addition, qaic_prepare_bo() overwrites bo->dir, bo->dbc, and bo->nr_slice even when qaic_prepare_import_bo() or qaic_prepare_export_bo() fails, leaving stale metadata on bo.
[1] https://sashiko.dev/#/bug/linux-9761c613-05ad-4c51-acc6-da4c08a070fd
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1