From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796380733; b=YzG7trdOG3dwgmVSOadTbBZtCBTnRouE7fmE0G/poxaxpGcjn2W1Z2BaH4FzSMY8fStI tJIs/eY2bqXWcLRy8RSj+QGGjoeQC1FN2P7NoxGaFqJcrRQwzrIaCS4BryRJQRy5hA7+U qHp8w/H2RpsPrX2WdAFpVUcFSNBexh+j5tqLHpO5jsjlCHStAyKXRfLWUnuGoelPzFcsz l5zBHRebQtbG5HYjMs4TR3v2PbK6CzzO8Tm3qE+M9K8qfdZbFZqht5qU6a6X0DlylCtav N0JSsLFPknjJiztAUNRNNsPTv8HRjX/rZB32nGJ6eGVZVWrvrmgaSrHyVEIVOa807Vw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380733; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=mhqivwAHodOFog+epoUUkHjUyTfNkLB3Um5RbdDQzVw=; b=I0iImHCE6P1oug9M4VZ0N9AonGWr/15K/6vQj18+veUnw3BCbXDbO2MSpV2ec5v58GcW LA+IQ3EHpP5LfSJys8PvUDcfRzCoLWNZgGPtuaUJzeINchM0MLIIKGjS3wOizieIss90q 7Nx/Rh59gmcJZ7dJU1HapYAaXewynKZHLm7UH+7NI9syML+I+XtNh0FUWA83P3sHa2yyf hyFOnQ/Fhu4fvbWceLgKkOnunEQVimStD1Wsf6x4HiJagmNDz/RywOIpEcQBTzxtLsULn GzGLw5ZkZlBtEmCFjCEHHknOoj7YDq9zbl/SM++AugnEGSo8/VcbzweoibeK6LuBaVQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=mBJYEH45; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA4DB606A32C; Fri, 4 Dec 2026 10:38:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=mBJYEH45 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 60CA31F000CE; Fri, 4 Dec 2026 10:38:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380733; bh=mhqivwAHodOFog+epoUUkHjUyTfNkLB3Um5RbdDQzVw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=mBJYEH45j9extaoeO9OV9saw7vU+EPurK72BPdky+BOPVhdF2JjWavlUzXOJyaEW2 mCYNtLrCpM3/q9RJOw7thDioffD4hfMetIgn8RBfwI8BVS3iFCGFT4VVRMFl4XIzut M7MIiQVjPLmdS7YOvlM6a78EDMVc7vq/m1X3TAr0= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Matthew Brost , Matthew Auld , Dave Airlie , Rodrigo Vivi , intel-xe@lists.freedesktop.org, =?UTF-8?q?Thomas=20Hellstr=C3=B6m?= Subject: [PATCH 7.3 361/742] drm/xe/dma-buf: Validate BOs on export to charge the exporter Date: Fri, 4 Dec 2026 19:03:45 +0900 Message-ID: <20261204100644.669500284@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Thomas Hellström commit 22e05ee854e1bdacebcfa36ecd3f33ec80cf897a upstream. xe_gem_prime_export() calls ttm_bo_setup_export() to make sure the exporter is charged for the memory backing an exported BO. However, that only populates a TTM_PL_SYSTEM tt. A BO that has been evicted, or that has deferred backing, will instead get its memory allocated when the importer maps it. For p2p imports this happens through xe_bo_validate() in xe_dma_buf_map(), so the importer, not the exporter, is charged for the allocation. Call xe_bo_validate() at export time instead, so that the BO is moved into its preferred placement and the exporter is charged. Do the purgeable checks and the validation in the same xe_validation_guard() transaction, to avoid locking the BO twice. The commit message and code was drafted using LLM. Link: https://patchwork.freedesktop.org/patch/747480/?series=172524&rev=1 Fixes: 91494dee1091 ("xe: populate buffers before exporting them.") Cc: Matthew Brost Cc: Matthew Auld Cc: Dave Airlie Cc: Rodrigo Vivi Cc: intel-xe@lists.freedesktop.org Cc: stable@vger.kernel.org # v6.18+ Reviewed-by: Matthew Auld Link: https://patch.msgid.link/20261111100929.2228-1-thomas.hellstrom@linux.intel.com Signed-off-by: Thomas Hellström (cherry picked from commit 451162b9f6570d19da1c5944aa0a0e3e8f17773d) Signed-off-by: Matthew Brost Signed-off-by: Greg Kroah-Hartman --- drivers/gpu/drm/xe/xe_dma_buf.c | 80 ++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 39 deletions(-) --- a/drivers/gpu/drm/xe/xe_dma_buf.c +++ b/drivers/gpu/drm/xe/xe_dma_buf.c @@ -222,60 +222,62 @@ static const struct dma_buf_ops xe_dmabu struct dma_buf *xe_gem_prime_export(struct drm_gem_object *obj, int flags) { struct xe_bo *bo = gem_to_xe_bo(obj); + struct xe_validation_ctx ctx; + struct drm_exec exec; struct dma_buf *buf; - struct ttm_operation_ctx ctx = { - .interruptible = true, - .no_wait_gpu = true, - /* We opt to avoid OOM on system pages allocations */ - .gfp_retry_mayfail = true, - .allow_res_evict = false, - }; - int ret; + int ret = 0; if (bo->vm) return ERR_PTR(-EPERM); - /* - * Reject exporting purgeable BOs. DONTNEED BOs can be purged - * at any time, making the exported dma-buf unusable. Purged BOs - * have no backing store and are permanently invalid. - */ - ret = xe_bo_lock(bo, true); - if (ret) - return ERR_PTR(ret); - - if (xe_bo_madv_is_dontneed(bo)) { - ret = -EBUSY; - goto out_unlock; - } + xe_validation_guard(&ctx, &xe_bo_device(bo)->val, &exec, + (struct xe_val_flags) {.interruptible = true}, ret) { + ret = drm_exec_lock_obj(&exec, &bo->ttm.base); + drm_exec_retry_on_contention(&exec); + if (ret) + break; + + /* + * Reject exporting purgeable BOs. DONTNEED BOs can be purged + * at any time, making the exported dma-buf unusable. Purged BOs + * have no backing store and are permanently invalid. + */ + if (xe_bo_madv_is_dontneed(bo)) { + ret = -EBUSY; + break; + } + + if (xe_bo_is_purged(bo)) { + ret = -EINVAL; + break; + } + + /* + * Make sure the BO is backed in its preferred placement so that + * the exporter, rather than the importer, is charged for the + * memory allocation. + */ + ret = xe_bo_validate(bo, NULL, false, &exec); + drm_exec_retry_on_contention(&exec); + xe_validation_retry_on_oom(&ctx, &ret); + if (ret) + break; - if (xe_bo_is_purged(bo)) { - ret = -EINVAL; - goto out_unlock; + xe_bo_willneed_get_locked(bo); } - - xe_bo_willneed_get_locked(bo); - xe_bo_unlock(bo); - - ret = ttm_bo_setup_export(&bo->ttm, &ctx); if (ret) - goto out_put; + return ERR_PTR(ret); buf = drm_gem_prime_export(obj, flags); if (IS_ERR(buf)) { - ret = PTR_ERR(buf); - goto out_put; + xe_bo_lock(bo, false); + xe_bo_willneed_put_locked(bo); + xe_bo_unlock(bo); + return buf; } buf->ops = &xe_dmabuf_ops; return buf; - -out_put: - xe_bo_lock(bo, false); - xe_bo_willneed_put_locked(bo); -out_unlock: - xe_bo_unlock(bo); - return ERR_PTR(ret); } static struct drm_gem_object *