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 30/33] drm/xe: Finalize defrag-IOVA moves with post-copy job
Date: Wed, 02 Dec 2026 01:03:31 +0000	[thread overview]
Message-ID: <sashiko-outbox-244562@kernel.org> (raw)
In-Reply-To: <20261201234915.3305331-31-matthew.brost@intel.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] drm/xe: missing kernel fence wait for preempt VMs in
  xe_bo_trigger_rebind()
- [High] drm/xe: missing dma-resv fence dependencies in
  xe_bo_defrag_iova_copy()
- [Medium] drm/xe: premature defrag list removal in xe_bo_move() on
  defrag move error
--

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

drm/xe: Finalize defrag-IOVA moves with post-copy job

This commit wires up end-to-end defragmentation for IOVA-mapped BOs by
copying only changed pages into a packed temporary mapping, transferring
the IOVA reservation to the new TT, and running a post-copy job to relink
changed pages at their natural offsets without marking the BO evicted.

> diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
> index 1a02fe407d78a..db9a0c7bd44e0 100644
> --- a/drivers/gpu/drm/xe/xe_bo.c
> +++ b/drivers/gpu/drm/xe/xe_bo.c

[ ... ]

> @@ -1181,6 +1302,159 @@ static void xe_tt_iova_transfer(struct xe_device *xe,
>  	old_tt->use_iova = false;
>  }
>  
> +/*
> + * xe_bo_defrag_iova_copy - Run an IOVA defrag move with a packed copy mapping.
> + *
> + * Only the changed (non-borrowed) pages of @new_tt are DMA mapped, packed
> + * contiguously into the front of its reservation, the GPU copy blits them from
> + * the old tt at natural offsets, and a post-copy finalize job adopts the old
> + * tt's full reservation and relinks the changed runs. Returns the finalize
> + * job's finished fence (the fence the move waits on), or an ERR_PTR on failure
> + * with all partial state unwound.
> + */
> +static struct dma_fence *
> +xe_bo_defrag_iova_copy(struct xe_device *xe, struct xe_migrate *migrate,
> +		       struct xe_bo *bo, struct ttm_resource *old_mem,
> +		       struct ttm_resource *new_mem, struct ttm_tt *old_tt,
> +		       bool handle_system_ccs)
> +{
> +	struct ttm_tt *new_tt = bo->ttm.ttm;
> +	struct xe_ttm_tt *new_xe_tt = container_of(new_tt, struct xe_ttm_tt, ttm);
> +	struct xe_ttm_tt *old_xe_tt = container_of(old_tt, struct xe_ttm_tt, ttm);
> +	struct drm_sched_entity *entity =
> +		xe_dep_scheduler_entity(xe->mem.defrag.iova_sched);
> +	struct xe_iova_defrag_job *job;
> +	struct dma_fence *copy_fence;
> +	struct dma_fence *fence;
> +	struct xe_tt_iova_copy copy;
> +	bool packed = false;
> +	int err;
> +
> +	xe_assert(xe, new_xe_tt->iova_eligible && !new_xe_tt->use_iova);
> +	xe_assert(xe, old_xe_tt->use_iova && old_xe_tt->iova_linked);
> +
> +	err = xe_tt_iova_copy_init(new_tt, old_tt, &copy);
> +	if (err)
> +		return ERR_PTR(err);
> +
> +	if (copy.nr_changed) {
> +		err = xe_tt_map_iova_copy(xe, new_tt, &copy);
> +		if (err)
> +			goto err_copy;
> +		packed = true;
> +
> +		copy_fence = xe_migrate_copy_defrag_iova(migrate, bo, old_mem,
> +							 new_mem, old_tt,
> +							 copy.nr_changed,
> +							 handle_system_ccs);
> +	} else {
> +		copy_fence = dma_fence_get_stub();
> +	}
> +	if (IS_ERR(copy_fence)) {
> +		err = PTR_ERR(copy_fence);
> +		goto err_unmap;
> +	}
> +
> +	job = kzalloc(sizeof(*job), GFP_KERNEL);
> +	if (!job) {
> +		err = -ENOMEM;
> +		goto err_fence;
> +	}
> +
> +	job->dep.ops = &xe_iova_defrag_job_ops;
> +	job->xe = xe;
> +	job->new_tt = new_tt;
> +	if (packed)
> +		job->copy_iova = new_xe_tt->iova;
> +	job->copy_bytes = (size_t)copy.nr_changed << PAGE_SHIFT;
> +	job->copy = copy;
> +
> +	err = drm_sched_job_init(&job->dep.drm, entity, 1, NULL, 0);
> +	if (err)
> +		goto err_job;
> +
> +	err = drm_sched_job_add_dependency(&job->dep.drm,
> +					   dma_fence_get(copy_fence));
> +	if (err)
> +		goto err_job_init;

[Severity: High]
Does job->dep.drm also need dependencies on bo->ttm.base.resv
(DMA_RESV_USAGE_BOOKKEEP) when copy_fence is dma_fence_get_stub()?

For non-fault-mode VMs, xe_bo_trigger_rebind() does not synchronously
wait on DMA_RESV_USAGE_BOOKKEEP fences.  When copy.nr_changed is 0 (or on
integrated platforms with Flat-CCS when all data pages are borrowed and
only the trailing CCS backup extra_pages changed, so __xe_migrate_copy()
creates no migration job and returns dma_fence_get_stub()), copy_fence is
already signaled and carries no bo->ttm.base.resv dependencies.

With no unsignaled dependency on job->dep.drm, xe_iova_defrag_job_run()
unlinks and relinks xe_tt->iova immediately, and the ghost object created
by ttm_bo_move_accel_cleanup() tears down old_tt and frees its
non-borrowed pages while prior GPU work in bo->ttm.base.resv is still in
flight.

[ ... ]

> @@ -1574,12 +1849,25 @@ static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
>  		dma_resv_iter_end(&cursor);
>  	}
>  
> +	/*
> +	 * A defrag move that reallocates IOVA-mapped backing preserves the
> +	 * device-visible addresses: the IOVA reservation is transferred from
> +	 * the old to the new tt, so the existing GPU PTEs stay valid and the
> +	 * BO must not be marked evicted (the rebind paths in xe_exec_ioctl()
> +	 * and preempt_rebind_work_func() would otherwise needlessly rebind).
> +	 * Preempt fences still need to be triggered, and fault-mode / L2
> +	 * flush-optimized configs still need a TLB invalidation below, so only
> +	 * the eviction marking is skipped.
> +	 */
> +	defrag_iova = xe_bo_defrag_use_iova(&bo->ttm);
> +
>  	drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
>  		struct xe_vm *vm = gpuvm_to_vm(vm_bo->vm);
>  		struct drm_gpuva *gpuva;
>  
>  		if (!xe_vm_in_fault_mode(vm)) {
> -			drm_gpuvm_bo_evict(vm_bo, true);
> +			if (!defrag_iova)
> +				drm_gpuvm_bo_evict(vm_bo, true);

[Severity: High]
Can skipping drm_gpuvm_bo_evict() here allow a preempt-fence-mode VM to
resume or start GPU execution while the IOVA defrag copy and
xe_iova_defrag_job_run() are still unlinking and relinking IOMMU mappings?

For an external BO (bo->vm == NULL), ttm_bo_move_accel_cleanup() installs
the finalize job's fence into bo->ttm.base.resv (DMA_RESV_USAGE_KERNEL),
not xe_vm_resv(vm).  Previously, marking vm_bo evicted caused
preempt_rebind_work_func() to rebind the VMA, which chained
bo->ttm.base.resv's kernel fence into xe_vm_resv(vm).  When
drm_gpuvm_bo_evict() is skipped, no rebind runs, and
preempt_rebind_work_func() only waits on xe_vm_resv(vm) before resuming
execution:

drivers/gpu/drm/xe/xe_vm.c:preempt_rebind_work_func() {
    ...
	/* Wait on rebinds and munmap style VM unbinds */
	wait = dma_resv_wait_timeout(xe_vm_resv(vm),
				     DMA_RESV_USAGE_KERNEL,
				     false, MAX_SCHEDULE_TIMEOUT);
    ...
	err = resume_and_reinstall_preempt_fences(vm, &exec);
}

Similarly, if the IOVA defrag move starts before the first compute queue
is attached to the VM, leaving vm->gpuvm.evict.list empty means a
concurrent xe_vm_add_compute_exec_queue() sees no pending eviction or
rebind and does not signal the new queue's preempt fence to wait for the
in-flight move.

[ ... ]

> @@ -2602,9 +2898,16 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
>  		fence = xe_migrate_clear(migrate, bo, new_mem, flags,
>  					 ctx->interruptible);
>  	} else if (has_defrag_old_tt) {
> -		fence = xe_migrate_copy_defrag(migrate, bo, old_mem, new_mem,
> -					       ttm_bo->defrag_old_tt,
> -					       handle_system_ccs);
> +		if (defrag_iova)
> +			fence = xe_bo_defrag_iova_copy(xe, migrate, bo, old_mem,
> +						       new_mem,
> +						       ttm_bo->defrag_old_tt,
> +						       handle_system_ccs);
> +		else
> +			fence = xe_migrate_copy_defrag(migrate, bo, old_mem,
> +						       new_mem,
> +						       ttm_bo->defrag_old_tt,
> +						       handle_system_ccs);
>  	} else {
>  		fence = xe_migrate_copy(migrate, bo, bo, old_mem, new_mem,
>  					handle_system_ccs, ctx->interruptible);

[Severity: Medium]
If xe_bo_defrag_iova_copy() fails and jumps to out, does the unconditional
xe_bo_defrag_update(bo) at the end of xe_bo_move() drop the still-fragmented
BO from xe->mem.defrag.list?

At the out label of xe_bo_move(), bo->ttm.ttm still points to the newly
populated beneficial-order TT, while ttm_bo_handle_move_mem() only
destroys that TT and restores bo->ttm = bo->defrag_old_tt after
xe_bo_move() returns:

drivers/gpu/drm/ttm/ttm_bo.c:ttm_bo_handle_move_mem() {
    ...
out_err:
	if (bo->defrag_old_tt) {
		ttm_tt_defrag_disown_borrowed(bo->ttm, bo->defrag_old_tt);
		ttm_bo_tt_destroy(bo);
		bo->ttm = bo->defrag_old_tt;
		bo->defrag_old_tt = NULL;
	}
}

Because ttm_tt_is_beneficial_order_failed(bo->ttm.ttm) is false on the
new TT, xe_bo_defrag_update(bo) sees xe_bo_needs_defrag(bo) == false and
removes the BO from xe->mem.defrag.list before defrag_old_tt is restored.

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

  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
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 [this message]
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-244562@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