Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Matthew Brost" <matthew.brost@intel.com>,
	intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v4 26/33] drm/xe: Use IOVA-based DMA mapping for eligible tt BOs
Date: Wed, 02 Dec 2026 01:03:36 +0000	[thread overview]
Message-ID: <sashiko-outbox-244563@kernel.org> (raw)
In-Reply-To: <20261201234915.3305331-27-matthew.brost@intel.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] drm/xe: missing unplug check in xe_tt_iova_reserve() strands
  IOVA mappings
- [High] drm/xe: unlocked xe_tt_unmap_iova() call in
  xe_bo_dma_unmap_pinned()
- [High] drm/xe: NULL iommu_group deref on unplug for scanout BOs using
  IOVA mapping
- [Low] drm/xe: stale IOVA lifecycle and GGTT sg description in commit
  message
--

commit 15b77bea89609de9937bc608810c99df9047f5f4
Author: Matthew Brost <matthew.brost@intel.com>

drm/xe: Use IOVA-based DMA mapping for eligible tt BOs

This commit adds support for mapping eligible ttm_tt pages into a single
contiguous IOVA range using the IOVA-based DMA API instead of a
scatter-gather list.

>     Whether the IOVA API can be used depends on the IOMMU mode, so the
>     decision is made once per tt at xe_ttm_tt_create() time and recorded in
>     struct xe_ttm_tt::use_iova. To keep the scope limited, only device BOs
>     that are not GGTT-mapped are eligible: dma-buf imports manage their own
>     mapping and GGTT bindings are programmed directly from the sg list.
>     dma_iova_try_alloc() may still fail, in which case the tt transparently
>     falls back to the sg list, which remains the default.
>     
>     The IOVA reservation is allocated at tt creation and freed at tt
>     destroy. xe_tt_map_iova() links the tt pages (coalescing physically
>     contiguous runs) and syncs the IOTLB; xe_tt_unmap_iova() unlinks them
>     but keeps the reservation so the tt can be remapped without
>     re-allocating IOVA space. xe_tt_map()/xe_tt_unmap() dispatch between the
>     IOVA and sg backends, and xe_bo_dma_unmap_pinned() learns about the IOVA
>     case.

[Severity: Low]
Does the commit message still describe the pre-v4 IOVA reservation
lifecycle?

In the v4 code, eligibility is recorded in xe_ttm_tt::iova_eligible (and
excludes XE_BO_FLAG_PINNED), xe_tt_iova_reserve() allocates the
reservation at map time and sets use_iova, and xe_tt_unmap_iova() frees
both the mapping and the reservation.  Also, both here and in the
xe_ttm_tt_create() comment, it says GGTT bindings are programmed directly
from the sg list, whereas this patch converts xe_ggtt_map_bo() to use
xe_res_first_tt().

> diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
> index a3f5d204cd9de..f1cacef7064d2 100644
> --- a/drivers/gpu/drm/xe/xe_bo.c
> +++ b/drivers/gpu/drm/xe/xe_bo.c

[ ... ]

> @@ -533,28 +616,197 @@ static void xe_tt_unmap_sg(struct xe_device *xe, struct ttm_tt *tt)
>  	}
>  }
>  
> -struct sg_table *xe_bo_sg(struct xe_bo *bo)
> +/*
> + * xe_tt_iova_reserve - Reserve the IOVA range backing a tt's DMA mapping.
> + *
> + * The reservation is taken when the tt is DMA mapped rather than when it is
> + * created, so that it is always released by xe_tt_unmap_iova() while the
> + * device's IOMMU domain is still alive. A tt whose reservation outlived
> + * pci device removal would hit a NULL iommu group in dma_iova_free().
> + *
> + * Return: true if @xe_tt holds a reservation on return, false if the caller
> + * should fall back to the scatter-gather path.
> + */
> +static bool xe_tt_iova_reserve(struct xe_device *xe, struct ttm_tt *tt)
>  {
> -	struct ttm_tt *tt = bo->ttm.ttm;
>  	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
>  
> -	return xe_tt->sg;
> +	if (xe_tt->use_iova)
> +		return true;
> +
> +	if (!xe_tt->iova_eligible)
> +		return false;
> +
> +	if (!dma_iova_try_alloc(xe->drm.dev, &xe_tt->iova, 0,
> +				(size_t)tt->num_pages << PAGE_SHIFT))
> +		return false;
> +
> +	xe_tt->use_iova = true;
> +	return true;
> +}

[Severity: High]
Can xe_tt_iova_reserve() take a new IOVA reservation after
xe_bo_pci_dev_remove_all() has already run, stranding the mapping until
after the PCI device is detached from its IOMMU group?

During xe_device_remove(), drm_dev_unplug() runs followed by
xe_bo_pci_dev_remove_all().  For exported dma-buf BOs, xe_evict_flags()
moves them to XE_PL_SYSTEM rather than purging them, which unmaps their
initial IOVA reservation while keeping ttm_bo->ttm alive with
iova_eligible set:

drivers/gpu/drm/xe/xe_bo.c:xe_evict_flags() {
    ...
	if (device_unplugged && !tbo->base.dma_buf) {
		*placement = purge_placement;
		return;
	}
    ...
}

After xe_bo_pci_dev_remove_all() returns, dev->iommu_group remains valid
until PCI bus removal completes (or indefinitely after a sysfs driver
unbind).  If an importer or userspace triggers DMA_BUF_IOCTL_SYNC
(xe_dma_buf_begin_cpu_access() -> xe_bo_migrate(bo, XE_PL_TT, ...)),
neither xe_dma_buf_begin_cpu_access() nor xe_tt_iova_reserve() checks
drm_dev_is_unplugged().  Moving from XE_PL_SYSTEM to XE_PL_TT performs a
CPU-only null move that calls xe_tt_map() -> xe_tt_iova_reserve(),
allocating a fresh IOVA reservation after xe_bo_pci_dev_remove_all() has
finished.

Once PCI removal sets dev->iommu_group to NULL, closing the dma-buf calls
xe_ttm_tt_unpopulate() -> xe_tt_unmap_iova() -> dma_iova_destroy(), which
unconditionally dereferences iommu_get_dma_domain(dev) and causes a NULL
pointer dereference.

[ ... ]

> @@ -764,6 +1016,30 @@ static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo,
>  		}
>  	}
>  
> +	/*
> +	 * Eligible BOs are DMA mapped using the IOVA-based DMA API, which
> +	 * yields a single contiguous IOVA range instead of a scatter-gather
> +	 * list. Only evictable device BOs that are not GGTT-mapped qualify:
> +	 * dma-buf imports manage their own mapping and GGTT bindings are
> +	 * programmed directly from the sg list.
> +	 *
> +	 * Pinned BOs are excluded because they are invisible to the eviction
> +	 * done by xe_bo_pci_dev_remove_all(): a BO pinned in XE_PL_TT (e.g. a
> +	 * page table on integrated) keeps its mapping until the BO is
> +	 * destroyed, which may happen long after the pci device was removed
> +	 * (an open drm file outliving a hot-unplug). Releasing IOVA state at
> +	 * that point would dereference a NULL iommu group.
> +	 *
> +	 * The IOVA range itself is reserved when the tt is DMA mapped and
> +	 * released when it is unmapped, so it never outlives the device;
> +	 * dma_iova_try_alloc() may still fail there, in which case we
> +	 * transparently fall back to the sg list.
> +	 */
> +	xe_tt->iova_eligible = ttm_bo->type != ttm_bo_type_sg &&
> +		!(bo->flags & (XE_BO_FLAG_GGTT | XE_BO_FLAG_GGTT_ALL |
> +			       XE_BO_FLAG_PINNED)) &&
> +		use_dma_iommu(xe->drm.dev);
> +
>  	return tt;
>  }

[Severity: High]
Can a user scanout BO on an integrated GPU bypass
xe_bo_pci_dev_remove_all() with an active IOVA reservation and cause a
NULL pointer dereference in dma_iova_destroy() after hot-unplug?

User scanout BOs created via xe_gem_create_ioctl() or xe_bo_dumb_create()
do not have XE_BO_FLAG_GGTT, XE_BO_FLAG_GGTT_ALL or XE_BO_FLAG_PINNED at
creation time, so iova_eligible is true and they are mapped in XE_PL_TT
with use_iova set.  When pinned to a display plane, __xe_pin_fb_vma()
calls xe_bo_pin_account() -> ttm_bo_pin(), which moves the BO to
xe->ttm.unevictable without adding it to xe->pinned.late.external.

During xe_device_remove(), xe_display_unregister() calls
drm_atomic_helper_shutdown(), which queues plane unpinning
(intel_atomic_cleanup_work -> __xe_unpin_fb_vma()) asynchronously on
display->wq.cleanup.  Because display->wq.cleanup is not flushed until
the devm callback xe_display_fini() -> intel_display_driver_remove() runs
after xe_device_remove(), xe_bo_pci_dev_remove_all() can run while the
scanout BO is still pinned on xe->ttm.unevictable (skipped by
ttm_resource_manager_evict_all()) and absent from
xe->pinned.late.external (skipped by xe_bo_pci_dev_remove_pinned()).

When cleanup_work later unpins the BO and userspace closes the DRM fd
after PCI removal has cleared dev->iommu_group, xe_ttm_tt_unpopulate() ->
xe_tt_unmap_iova() -> dma_iova_destroy() dereferences a NULL
dev->iommu_group.

[ ... ]

> @@ -2524,6 +2813,8 @@ int xe_bo_dma_unmap_pinned(struct xe_bo *bo)
>  					  DMA_BIDIRECTIONAL);
>  			ttm_bo->sg = NULL;
>  			xe_tt->sg = NULL;
> +		} else if (xe_tt->use_iova) {
> +			xe_tt_unmap_iova(ttm_to_xe_device(ttm_bo->bdev), tt);
>  		} else if (xe_tt->sg) {
>  			xe_tt_unmap_sg(ttm_to_xe_device(ttm_bo->bdev),
>  				       &xe_tt->ttm);

[Severity: High]
Can calling xe_tt_unmap_iova() here without holding the BO reservation
lock race with a concurrent unpin and eviction or destruction of an
exported dma-buf BO?

Exported user BOs pinned via xe_dma_buf_pin() -> xe_bo_pin_external() are
iova_eligible and sit on xe->pinned.late.external.
xe_bo_apply_to_pinned() drops xe->pinned.lock before invoking
xe_bo_dma_unmap_pinned(bo) with only a GEM reference held, and unlike the
other xe_pinned_fn callbacks in xe_bo.c, xe_bo_dma_unmap_pinned() does
not call xe_bo_lock(bo):

CPU0                                  CPU1
xe_bo_pci_dev_remove_pinned()
  xe_bo_apply_to_pinned()
    spin_unlock(&xe->pinned.lock)
    xe_bo_dma_unmap_pinned(bo)
                                      dma_buf_unpin()
                                        xe_bo_unpin_external(bo)
                                      ...
                                      xe_ttm_tt_unpopulate()
                                        xe_tt_unmap_iova()
      xe_tt_unmap_iova()
        dma_iova_destroy(...)             dma_iova_destroy(...)
                                        ...
                                        kfree(tt)  <-- use-after-free

Because xe_tt_unmap_iova() clears iova_linked and use_iova only after
dma_iova_destroy() and xe_tt_unaccount_iova(), concurrent execution can
call dma_iova_destroy() twice on the same IOVA state, underflow
xe->mem.dma_mapped_pages[], or access tt after it is freed.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201234915.3305331-1-matthew.brost@intel.com?part=26

  reply	other threads:[~2026-12-02  1:03 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 23:48 [PATCH v4 00/33] drm/ttm, drm/xe: Avoid foreground high-order reclaim and defragment sub-optimally backed BOs Matthew Brost
2026-12-01 23:48 ` [PATCH v4 01/33] drm/ttm/pool: Allow backing off reclaim at the beneficial order Matthew Brost
2026-12-01 23:48 ` [PATCH v4 02/33] drm/ttm/pool: Add ttm_pool_page_order_nodma() helper Matthew Brost
2026-12-01 23:48 ` [PATCH v4 03/33] drm/ttm: Record sub-optimal page order allocations in ttm_tt Matthew Brost
2026-12-01 23:48 ` [PATCH v4 04/33] drm/ttm: Introduce ttm_pool_alloc_iter for __ttm_pool_alloc() Matthew Brost
2026-12-01 23:59   ` sashiko-bot
2026-12-01 23:48 ` [PATCH v4 05/33] drm/ttm: Support defragmentation moves Matthew Brost
2026-12-01 23:48 ` [PATCH v4 06/33] drm/ttm: Add fault injection for beneficial-order allocation failures Matthew Brost
2026-12-02  0:06   ` sashiko-bot
2026-12-01 23:48 ` [PATCH v4 07/33] drm/ttm: Harvest beneficial-order pages on defragmentation moves Matthew Brost
2026-12-02  0:27   ` sashiko-bot
2026-12-01 23:48 ` [PATCH v4 08/33] drm/ttm: Bound page (re)allocation per defragmentation move Matthew Brost
2026-12-01 23:48 ` [PATCH v4 09/33] drm/ttm: Preallocate beneficial-order defrag pages outside the lock Matthew Brost
2026-12-02  0:23   ` sashiko-bot
2026-12-01 23:48 ` [PATCH v4 10/33] drm/ttm: Add full out-of-lock preallocation for ttm_pool_alloc() Matthew Brost
2026-12-02  0:15   ` sashiko-bot
2026-12-01 23:48 ` [PATCH v4 11/33] drm/gpusvm: Add a DMA-mapping accounting callback Matthew Brost
2026-12-01 23:48 ` [PATCH v4 12/33] drm/xe: Add debugfs stats for DMA-mapped pages per order Matthew Brost
2026-12-01 23:48 ` [PATCH v4 13/33] drm/xe: Flush L2 asynchronously in xe_bo_trigger_rebind() Matthew Brost
2026-12-02  0:19   ` sashiko-bot
2026-12-01 23:48 ` [PATCH v4 14/33] drm/xe: Track BOs backed at a sub-optimal page order Matthew Brost
2026-12-02  0:17   ` sashiko-bot
2026-12-01 23:48 ` [PATCH v4 15/33] drm/xe: Add xe_migrate_copy_defrag() for on-GPU defrag copies Matthew Brost
2026-12-01 23:48 ` [PATCH v4 16/33] drm/xe: Handle defrag moves in xe_bo_move() Matthew Brost
2026-12-02  0:19   ` sashiko-bot
2026-12-01 23:48 ` [PATCH v4 17/33] drm/xe: Skip self-copies for borrowed pages on defrag moves Matthew Brost
2026-12-02  0:16   ` sashiko-bot
2026-12-01 23:49 ` [PATCH v4 18/33] drm/xe: Add a page defragmentation worker Matthew Brost
2026-12-02  0:23   ` sashiko-bot
2026-12-01 23:49 ` [PATCH v4 19/33] drm/xe: Add defrag GT stats Matthew Brost
2026-12-01 23:49 ` [PATCH v4 20/33] drm/xe: Add Kconfig.profile options for BO defrag configuration Matthew Brost
2026-12-02  0:12   ` sashiko-bot
2026-12-01 23:49 ` [PATCH v4 21/33] drm/xe: Defrag using out-of-lock page preallocation Matthew Brost
2026-12-02  0:24   ` sashiko-bot
2026-12-01 23:49 ` [PATCH v4 22/33] drm/xe: Add defrag profiling tracepoints Matthew Brost
2026-12-02  0:09   ` sashiko-bot
2026-12-01 23:49 ` [PATCH v4 23/33] drm/xe: Preallocate system BO backing outside the dma-resv lock Matthew Brost
2026-12-02  0:15   ` sashiko-bot
2026-12-01 23:49 ` [PATCH v4 24/33] drm/xe: Add tracepoint for xe_gem_create_ioctl Matthew Brost
2026-12-01 23:49 ` [PATCH v4 25/33] drm/xe: Add IOVA-based xe_res_cursor variant Matthew Brost
2026-12-01 23:49 ` [PATCH v4 26/33] drm/xe: Use IOVA-based DMA mapping for eligible tt BOs Matthew Brost
2026-12-02  1:03   ` sashiko-bot [this message]
2026-12-01 23:49 ` [PATCH v4 27/33] drm/xe: Add per-device dependency scheduler for IOVA defrag finalize Matthew Brost
2026-12-01 23:49 ` [PATCH v4 28/33] drm/xe: Add packed copy-step IOVA mapping for defrag Matthew Brost
2026-12-01 23:49 ` [PATCH v4 29/33] drm/xe: Blit src-natural to dst-packed for defrag-IOVA copies Matthew Brost
2026-12-02  0:17   ` sashiko-bot
2026-12-01 23:49 ` [PATCH v4 30/33] drm/xe: Finalize defrag-IOVA moves with post-copy job Matthew Brost
2026-12-02  1:03   ` sashiko-bot
2026-12-01 23:49 ` [PATCH v4 31/33] drm/xe: Grow the USM batch pool to 2M and map it via a single PDE Matthew Brost
2026-12-01 23:49 ` [PATCH v4 32/33] drm/xe: Add bo_defrag debugfs entry Matthew Brost
2026-12-01 23:49 ` [PATCH v4 33/33] drm/xe: Make BO defrag tunables configurable via sysfs Matthew Brost
2026-12-02  0:20   ` sashiko-bot
2026-12-01 23:58 ` ✗ CI.checkpatch: warning for drm/ttm, drm/xe: Avoid foreground high-order reclaim and defragment sub-optimally backed BOs Patchwork
2026-12-02  0:00 ` ✓ CI.KUnit: success " Patchwork
2026-12-02  0:38 ` ✓ Xe.CI.BAT: " Patchwork

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=sashiko-outbox-244563@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=matthew.brost@intel.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /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