mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Li Sheng <wirsbf@gmail.com>
To: intel-xe@lists.freedesktop.org, matthew.brost@intel.com,
	thomas.hellstrom@linux.intel.com, rodrigo.vivi@intel.com
Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
	Li Sheng <wirsbf@gmail.com>
Subject: [PATCH] drm/xe: Lock BO in xe_bo_dma_unmap_pinned()
Date: Sun,  6 Dec 2026 11:46:13 +0800	[thread overview]
Message-ID: <20261206034613.558542-1-wirsbf@gmail.com> (raw)

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


                 reply	other threads:[~2026-12-06  3:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261206034613.558542-1-wirsbf@gmail.com \
    --to=wirsbf@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matthew.brost@intel.com \
    --cc=rodrigo.vivi@intel.com \
    --cc=thomas.hellstrom@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®