All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/xe: Lock BO in xe_bo_dma_unmap_pinned()
@ 2026-12-06  3:46 Li Sheng
  2026-12-06  4:03 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Li Sheng @ 2026-12-06  3:46 UTC (permalink / raw)
  To: intel-xe, matthew.brost, thomas.hellstrom, rodrigo.vivi
  Cc: dri-devel, linux-kernel, Li Sheng

xe_bo_dma_unmap_pinned() is invoked via xe_bo_apply_to_pinned() during
PCI device removal (xe_bo_pci_dev_remove_pinned()) and driver teardown
(xe_bo_pinned_fini()). xe_bo_apply_to_pinned() drops xe->pinned.lock
before calling the callback without holding the BO reservation lock, so
every other xe_bo_*_pinned() callback acquires the BO lock internally.

xe_bo_dma_unmap_pinned() omits xe_bo_lock():

- For pinned external BOs on xe->pinned.late.external (pinned via
  xe_dma_buf_pin() -> xe_bo_pin_external()), reading ttm_bo->ttm and
  freeing xe_tt->sg without the BO lock races with concurrent TTM
  unpopulate/destroy paths (xe_ttm_tt_unpopulate() -> xe_tt_unmap_sg()
  and xe_ttm_tt_destroy()), leading to double-unmap, double-free of
  xe_tt->sg, or use-after-free of tt.
- In the ttm_bo_type_sg branch, calling dma_buf_unmap_sgt() without
  holding the BO reservation lock (aliasing import_attach->dmabuf->resv)
  violates dma_resv_assert_held().

Acquire xe_bo_lock(bo, false) before reading ttm_bo->ttm and unlock
afterwards, matching xe_bo_restore_pinned() and
xe_bo_notifier_unprepare_pinned().

Fixes: 3cbb651117ff ("drm/xe/bo: Add a bo remove callback")
Cc: stable@vger.kernel.org
Signed-off-by: Li Sheng <wirsbf@gmail.com>
---
 drivers/gpu/drm/xe/xe_bo.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
index c89b39b50..febfda241 100644
--- a/drivers/gpu/drm/xe/xe_bo.c
+++ b/drivers/gpu/drm/xe/xe_bo.c
@@ -1878,7 +1878,10 @@ int xe_bo_restore_pinned(struct xe_bo *bo)
 int xe_bo_dma_unmap_pinned(struct xe_bo *bo)
 {
 	struct ttm_buffer_object *ttm_bo = &bo->ttm;
-	struct ttm_tt *tt = ttm_bo->ttm;
+	struct ttm_tt *tt;
+
+	xe_bo_lock(bo, false);
+	tt = ttm_bo->ttm;
 
 	if (tt) {
 		struct xe_ttm_tt *xe_tt = container_of(tt, typeof(*xe_tt), ttm);
@@ -1897,6 +1900,7 @@ int xe_bo_dma_unmap_pinned(struct xe_bo *bo)
 			xe_tt->sg = NULL;
 		}
 	}
+	xe_bo_unlock(bo);
 
 	return 0;
 }
-- 
2.56.1


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

* Re: [PATCH] drm/xe: Lock BO in xe_bo_dma_unmap_pinned()
  2026-12-06  3:46 [PATCH] drm/xe: Lock BO in xe_bo_dma_unmap_pinned() Li Sheng
@ 2026-12-06  4:03 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-06  4:03 UTC (permalink / raw)
  To: Li Sheng, intel-xe; +Cc: dri-devel

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] drm/xe: stale xe_oa_quiesce_gt() comment and omitted caller in
  commit log
--

commit ea3e3665aedf57056b31951b4e309f08ebe463ae
Author: Li Sheng <wirsbf@gmail.com>

drm/xe: Lock BO in xe_bo_dma_unmap_pinned()

This commit acquires the BO reservation lock in xe_bo_dma_unmap_pinned()
before reading ttm_bo->ttm and unmapping its scatter-gather table.

> diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
> index c89b39b503fe1..febfda24163db 100644
> --- a/drivers/gpu/drm/xe/xe_bo.c
> +++ b/drivers/gpu/drm/xe/xe_bo.c
> @@ -1878,7 +1878,10 @@ int xe_bo_restore_pinned(struct xe_bo *bo)
>  int xe_bo_dma_unmap_pinned(struct xe_bo *bo)
>  {
>  	struct ttm_buffer_object *ttm_bo = &bo->ttm;
> -	struct ttm_tt *tt = ttm_bo->ttm;
> +	struct ttm_tt *tt;
> +
> +	xe_bo_lock(bo, false);
> +	tt = ttm_bo->ttm;
>  
>  	if (tt) {
>  		struct xe_ttm_tt *xe_tt = container_of(tt, typeof(*xe_tt), ttm);

[Severity: Low]
Should this patch also update the comment in xe_oa_quiesce_gt()?

drivers/gpu/drm/xe/xe_oa.c:xe_oa_quiesce_gt() {
	...
		/*
		 * The OA buffer stays pinned until the stream fd is released, an mmap may still
		 * hold a reference on it. Drop its DMA mapping now: the fd may be released, and
		 * the BO unpinned, while xe_bo_pinned_fini() DMA-unmaps it without the BO lock.
		 */
		xe_bo_dma_unmap_pinned(stream->oa_buffer.bo);
	...
}

Once xe_bo_dma_unmap_pinned() takes xe_bo_lock(), xe_bo_pinned_fini() no
longer DMA-unmaps the BO without the BO lock, which makes that comment
stale and makes the early call worth re-evaluating or re-documenting.

The commit message also omits xe_oa_quiesce_gt() when listing the callers
of xe_bo_dma_unmap_pinned().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206034613.558542-1-wirsbf@gmail.com?part=1

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

end of thread, other threads:[~2026-12-06  4:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  3:46 [PATCH] drm/xe: Lock BO in xe_bo_dma_unmap_pinned() Li Sheng
2026-12-06  4:03 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.