dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] accel/qaic: Synchronize access to dbc->bo_lists
@ 2026-11-29  6:57 Bilal Hashim
  2026-11-29  6:57 ` [PATCH] accel/qaic: Hold bo->lock when checking bo->sliced and bo->dbc in qaic_wait_bo_ioctl() Bilal Hashim
  2026-11-29  7:10 ` [PATCH] accel/qaic: Synchronize access to dbc->bo_lists sashiko-bot
  0 siblings, 2 replies; 4+ messages in thread
From: Bilal Hashim @ 2026-11-29  6:57 UTC (permalink / raw)
  To: Jeff Hugo, Carl Vanderlip, Oded Gabbay
  Cc: linux-arm-msm, dri-devel, linux-kernel, Bilal Hashim

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.

Signed-off-by: Bilal Hashim <AeeSec@126.com>
---
 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(+)

diff --git a/drivers/accel/qaic/qaic.h b/drivers/accel/qaic/qaic.h
index 111111111111..222222222222 100644
--- a/drivers/accel/qaic/qaic.h
+++ b/drivers/accel/qaic/qaic.h
@@ -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 */
diff --git a/drivers/accel/qaic/qaic_data.c b/drivers/accel/qaic/qaic_data.c
index 333333333333..444444444444 100644
--- a/drivers/accel/qaic/qaic_data.c
+++ b/drivers/accel/qaic/qaic_data.c
@@ -1089,7 +1089,9 @@ int qaic_attach_slice_bo_ioctl(struct drm_device *dev, void *data, struct drm_fi
 		dma_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_file
 
 static 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);
 }
 
diff --git a/drivers/accel/qaic/qaic_drv.c b/drivers/accel/qaic/qaic_drv.c
index 555555555555..666666666666 100644
--- a/drivers/accel/qaic/qaic_drv.c
+++ b/drivers/accel/qaic/qaic_drv.c
@@ -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)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH] accel/qaic: Hold bo->lock when checking bo->sliced and bo->dbc in qaic_wait_bo_ioctl()
  2026-11-29  6:57 [PATCH] accel/qaic: Synchronize access to dbc->bo_lists Bilal Hashim
@ 2026-11-29  6:57 ` Bilal Hashim
  2026-11-29  7:04   ` sashiko-bot
  2026-11-29  7:10 ` [PATCH] accel/qaic: Synchronize access to dbc->bo_lists sashiko-bot
  1 sibling, 1 reply; 4+ messages in thread
From: Bilal Hashim @ 2026-11-29  6:57 UTC (permalink / raw)
  To: Jeff Hugo, Carl Vanderlip, Oded Gabbay
  Cc: linux-arm-msm, dri-devel, linux-kernel, Bilal Hashim

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.

Signed-off-by: Bilal Hashim <AeeSec@126.com>
---
--- a/drivers/accel/qaic/qaic_data.c	2026-11-29 14:54:02.057387256 +0800
+++ b/drivers/accel/qaic/qaic_data.c	2026-11-29 14:54:02.347387185 +0800
@@ -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_device
 	if (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:


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] accel/qaic: Hold bo->lock when checking bo->sliced and bo->dbc in qaic_wait_bo_ioctl()
  2026-11-29  6:57 ` [PATCH] accel/qaic: Hold bo->lock when checking bo->sliced and bo->dbc in qaic_wait_bo_ioctl() Bilal Hashim
@ 2026-11-29  7:04   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-29  7:04 UTC (permalink / raw)
  To: Bilal Hashim; +Cc: dri-devel

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 <AeeSec@126.com>

> 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.
>
> Signed-off-by: Bilal Hashim <AeeSec@126.com>

[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:

  Fixes: d48ea8691943 ("accel/qaic: Harden checks in BO execute and wait IOCTLs")

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261129065740.864-2-AeeSec@126.com?part=1

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] accel/qaic: Synchronize access to dbc->bo_lists
  2026-11-29  6:57 [PATCH] accel/qaic: Synchronize access to dbc->bo_lists Bilal Hashim
  2026-11-29  6:57 ` [PATCH] accel/qaic: Hold bo->lock when checking bo->sliced and bo->dbc in qaic_wait_bo_ioctl() Bilal Hashim
@ 2026-11-29  7:10 ` sashiko-bot
  1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-29  7:10 UTC (permalink / raw)
  To: Bilal Hashim; +Cc: dri-devel

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 <AeeSec@126.com>

> 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.
>
> Signed-off-by: Bilal Hashim <AeeSec@126.com>

[Severity: Low]
Should this bug fix include Fixes tags for the commits that introduced the
unlocked list insertion and removal on dbc->bo_lists?

  Fixes: ff13be830333 ("accel/qaic: Add datapath")
  Fixes: 217b812364d3 ("accel/qaic: Add QAIC_DETACH_SLICE_BO IOCTL")

> diff --git a/drivers/accel/qaic/qaic_data.c b/drivers/accel/qaic/qaic_data.c
> index 1f0b31fefc447..f65782f3333e2 100644
> --- a/drivers/accel/qaic/qaic_data.c
> +++ b/drivers/accel/qaic/qaic_data.c

[ ... ]

> @@ -1903,10 +1905,16 @@ int qaic_perf_stats_bo_ioctl(struct drm_device *dev, void *data, struct drm_file
>  
>  static 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

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261129065740.864-1-AeeSec@126.com?part=1

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-30 15:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29  6:57 [PATCH] accel/qaic: Synchronize access to dbc->bo_lists Bilal Hashim
2026-11-29  6:57 ` [PATCH] accel/qaic: Hold bo->lock when checking bo->sliced and bo->dbc in qaic_wait_bo_ioctl() Bilal Hashim
2026-11-29  7:04   ` sashiko-bot
2026-11-29  7:10 ` [PATCH] accel/qaic: Synchronize access to dbc->bo_lists sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox