Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D76A7606A31A for ; Wed, 2 Dec 2026 01:03:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=RlUm7E5I Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 3D77510FE4E; Wed, 2 Dec 2026 01:03:45 +0000 (UTC) Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by gabe.freedesktop.org (Postfix) with ESMTPS id F18C610FE21; Wed, 2 Dec 2026 01:03:38 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 183A0609EC; Wed, 2 Dec 2026 01:03:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A62861F000C1; Wed, 2 Dec 2026 01:03:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796173417; bh=gT3mMSCJ2aTUAfUFWouGAXsTlTF6EU0s33ioUmz272E=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=RlUm7E5I+19dFs0uFQSaVRlmUdZ8gLU5JqzOQ47LvN0PBrLpR0FC+SaoPLGATIKqW yuwprYltKpJOJNJRqFdKsYZKEXp4hNB+hy7IouRagAMJb1OVnXeaRnZDrRSl2qyIzu ujwoWs+7Ad4vRVvowWP2QPY+ykdPbX9WHhY0OSP0b8938bX6+HunUlcjDQQCBsRbSY bOARkfqZE7Tomseywx796nRTlLc9UB/GQUwa8YKitaigtPOLy51fwSqotmE8ETQoPg fofqwwGxChKFraKT7B5zQDIB5qDi5G87fvLNxsApWBiADOG5hzBBky6ek69i8wofc3 n+6dXcB6m1SlA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v4 26/33] drm/xe: Use IOVA-based DMA mapping for eligible tt BOs To: "Matthew Brost" , intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org In-Reply-To: <20261201234915.3305331-27-matthew.brost@intel.com> References: <20261201234915.3305331-1-matthew.brost@intel.com> <20261201234915.3305331-27-matthew.brost@intel.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 01:03:36 +0000 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: sashiko-reviews@lists.linux.dev Sender: "dri-devel" X-Spam-Status: No, score=-10.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: 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 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 o= wn > mapping and GGTT bindings are programmed directly from the sg list. > dma_iova_try_alloc() may still fail, in which case the tt transparent= ly > falls back to the sg list, which remains the default. > =20 > 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 I= OVA > 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, s= truct ttm_tt *tt) > } > } > =20 > -struct sg_table *xe_bo_sg(struct xe_bo *bo) > +/* > + * xe_tt_iova_reserve - Reserve the IOVA range backing a tt's DMA mappin= g. > + * > + * The reservation is taken when the tt is DMA mapped rather than when i= t 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 ca= ller > + * 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 =3D bo->ttm.ttm; > struct xe_ttm_tt *xe_tt =3D container_of(tt, struct xe_ttm_tt, ttm); > =20 > - 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 =3D 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 =3D 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_b= uffer_object *ttm_bo, > } > } > =20 > + /* > + * 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 =3D ttm_bo->type !=3D 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 =3D NULL; > xe_tt->sg =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201234915.3305= 331-1-matthew.brost@intel.com?part=3D26