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 13/33] drm/xe: Flush L2 asynchronously in xe_bo_trigger_rebind()
Date: Wed, 02 Dec 2026 00:19:37 +0000	[thread overview]
Message-ID: <sashiko-outbox-244486@kernel.org> (raw)
In-Reply-To: <20261201234915.3305331-14-matthew.brost@intel.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] drm/xe: wrong exec queue in xe_vma_tlb_flush_client() causes
  crash and race
- [High] drm/xe: GFP_KERNEL allocation under fs_reclaim in
  xe_bo_trigger_rebind()
- [Medium] drm/xe: missing tile offset when indexing tlb_inval[] in
  async TLB flush
- [Medium] drm/xe: sync waits in xe_bo_move() defeat async flush and
  drop intr flag
--

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

drm/xe: Flush L2 asynchronously in xe_bo_trigger_rebind()

This commit replaces the synchronous idle wait and TLB invalidation in
xe_bo_trigger_rebind() on L2-flush-optimized hardware with asynchronous TLB
invalidation jobs installed into the BO's dma-resv as kernel fences.

> diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
> index ff5610ba4b740..920b7b6094582 100644
> --- a/drivers/gpu/drm/xe/xe_bo.c
> +++ b/drivers/gpu/drm/xe/xe_bo.c
> @@ -857,6 +857,19 @@ static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
>  			 */
>  			if (!xe_device_is_l2_flush_optimized(xe))
>  				continue;
> +
> +			/*
> +			 * Attempt to flush L2 async, fallback to sync flush on
> +			 * failure. Skip the async attempt under no_wait_gpu
> +			 * (e.g. the shrinker's reclaim pass): job creation
> +			 * allocates with GFP_KERNEL, which must not run while
> +			 * holding the dma-resv lock under fs_reclaim. The sync
> +			 * fallback below honors no_wait_gpu (returns -EBUSY if
> +			 * the BO is not idle).
> +			 */
> +			if (!ctx->no_wait_gpu &&
> +			    !xe_vm_flush_vm_bo_tlb_async(vm, bo, vm_bo))
> +				continue;
>  		}

[Severity: High]
Is checking !ctx->no_wait_gpu enough to prevent GFP_KERNEL allocations in
xe_vm_flush_vm_bo_tlb_async() under fs_reclaim while holding the BO dma-resv
lock?

When kswapd runs xe_shrinker_scan(), ttm_bo_shrink_avoid_wait() returns false
(!current_is_kswapd()), so passes 2 and 3 in xe_shrinker_walk() call
__xe_shrinker_walk() -> xe_bo_shrink() -> xe_bo_move_notify() ->
xe_bo_trigger_rebind() with ctx->no_wait_gpu == false inside fs_reclaim.
Similarly, xe_ttm_bo_swap_notify() calls xe_ttm_bo_purge() with
ctx.no_wait_gpu == false during swapout.  Both paths hit the GFP_KERNEL
allocations and might_alloc(GFP_KERNEL) in xe_tlb_inval_job_alloc_dep(),
inverting the fs_reclaim -> dma_resv lock order and risking reclaim deadlock.

[Severity: Medium]
Does xe_bo_move() still block synchronously on the new flush fence right
after xe_bo_move_notify() returns?

xe_device_is_l2_flush_optimized() is only true on !IS_DGFX(xe), where BO
eviction moves from XE_PL_TT to XE_PL_SYSTEM.  Immediately after
xe_bo_move_notify(), xe_bo_move() waits uninterruptibly on
DMA_RESV_USAGE_BOOKKEEP before reaching xe_migrate_copy():

drivers/gpu/drm/xe/xe_bo.c:xe_bo_move() {
    ...
	if (old_mem_type == XE_PL_TT &&
	    new_mem->mem_type == XE_PL_SYSTEM) {
		long timeout = dma_resv_wait_timeout(ttm_bo->base.resv,
						     DMA_RESV_USAGE_BOOKKEEP,
						     false,
						     MAX_SCHEDULE_TIMEOUT);
    ...
}

Because DMA_RESV_USAGE_BOOKKEEP includes the DMA_RESV_USAGE_KERNEL fence
installed by xe_vm_flush_vm_bo_tlb_async(), the calling thread still stalls
under the BO dma-resv lock for all in-flight GPU work plus the extra
dep_scheduler and job_wq hop, and the wait is no longer interruptible when
ctx->interruptible is true.

> diff --git a/drivers/gpu/drm/xe/xe_vm.c b/drivers/gpu/drm/xe/xe_vm.c
> index c774804ca5567..842d99a6ae1a3 100644
> --- a/drivers/gpu/drm/xe/xe_vm.c
> +++ b/drivers/gpu/drm/xe/xe_vm.c
[ ... ]
> @@ -4668,6 +4669,130 @@ int xe_vm_invalidate_vma(struct xe_vma *vma)
>  	return ret;
>  }
>  
> +/*
> + * xe_vma_tlb_flush_client - Queue an async TLB flush for one VMA on one client
> + *
> + * Create and push a TLB invalidation job on the tile migrate (kernel) exec
> + * queue covering @vma's range, depending on @dep (the BO's in-flight GPU work)
> + * so the flush only fires once the GPU is done with the current mapping. The
> + * job's completion fence is installed into @resv as a KERNEL fence so the
> + * subsequent migration waits on the flush. No PTEs are zapped; this only
> + * flushes L2 via the TLB invalidation.
> + */
> +static int xe_vma_tlb_flush_client(struct xe_vm *vm, struct xe_vma *vma,
> +				   struct xe_tile *tile, struct xe_gt *gt,
> +				   struct dma_resv *resv, struct dma_fence *dep,
> +				   int type)
> +{
> +	struct xe_exec_queue *q = xe_migrate_exec_queue(tile->migrate);
> +	struct xe_tlb_inval_job *job;
> +	struct dma_fence *fence;
> +	int err;
> +
> +	job = xe_tlb_inval_job_create(q, &gt->tlb_inval,
> +				      q->tlb_inval[type].dep_scheduler, vm,
> +				      xe_vma_start(vma), xe_vma_end(vma), type);
> +	if (IS_ERR(job))
> +		return PTR_ERR(job);
> +
> +	err = xe_tlb_inval_job_alloc_dep(job);
> +	if (err)
> +		goto out_put;
> +
> +	err = dma_resv_reserve_fences(resv, 1);
> +	if (err)
> +		goto out_put;
> +
> +	/* Cannot fail; takes its own ref on @dep and returns a referenced fence. */
> +	fence = xe_tlb_inval_job_push(job, dep);

[Severity: High]
Should xe_vma_tlb_flush_client() use xe_cpu_bind_queue(vm->xe->cpu_bind)
instead of xe_migrate_exec_queue(tile->migrate)?

Since commit f5708e761ed6 ("drm/xe: Add CPU bind layer"),
xe_tlb_inval_job_push() serializes kernel TLB invalidation jobs via
xe_cpu_bind_job_lock(cpu_bind, job->q):

drivers/gpu/drm/xe/xe_cpu_bind.c:xe_cpu_bind_job_lock() {
	bool is_cpu_bind = is_cpu_bind_queue(cpu_bind, q);

	if (is_cpu_bind)
		mutex_lock(&cpu_bind->job_mutex);
	else
		xe_vm_assert_held(q->user_vm);	/* User queues VM's should be locked */
}

Because tile->migrate->q is not cpu_bind->q and has q->user_vm == NULL,
xe_cpu_bind_job_lock() skips cpu_bind->job_mutex and dereferences a NULL
q->user_vm in xe_vm_assert_held() when CONFIG_LOCKDEP is enabled, followed by
failing xe_assert(xe, q == cpu_bind->q) in xe_cpu_bind_job_lock_assert().
With lockdep and debug assertions disabled, no lock is taken at all, so
concurrent BO evictions race on the shared dep_scheduler entity and on
q->tlb_inval[idx].last_fence in xe_exec_queue_tlb_inval_last_fence_set().

> +	dma_resv_add_fence(resv, fence, DMA_RESV_USAGE_KERNEL);
> +	dma_fence_put(fence);
> +
> +out_put:
> +	/* Drop the creation reference (destroys the job if it was not pushed). */
> +	xe_tlb_inval_job_put(job);
> +	return err;
> +}
[ ... ]
> +		for_each_tile(tile, xe, id) {
> +			if (!(vma->tile_present & BIT(id)))
> +				continue;
> +
> +			err = xe_vma_tlb_flush_client(vm, vma, tile,
> +						      tile->primary_gt, resv, dep,
> +						      XE_EXEC_QUEUE_TLB_INVAL_PRIMARY_GT);
> +			if (err)
> +				goto out;
> +
> +			if (tile->media_gt) {
> +				err = xe_vma_tlb_flush_client(vm, vma, tile,
> +							      tile->media_gt, resv, dep,
> +							      XE_EXEC_QUEUE_TLB_INVAL_MEDIA_GT);
> +				if (err)
> +					goto out;
> +			}
> +		}

[Severity: Medium]
Does indexing q->tlb_inval[type] without a tile offset use tile 0's slots for
every tile?

Since commit 366365b7489f ("drm/xe: Make bind queues operate cross-tile"),
alloc_dep_schedulers() populates q->tlb_inval[] across all tiles at
id * (XE_EXEC_QUEUE_TLB_INVAL_MEDIA_GT + 1) + type.  Passing the unshifted
type (0 or 1) for tile->id > 0 selects tile 0's dep_scheduler and last_fence
slot, which gives fences from different tiles the same dma_fence context so
dma_resv_add_fence() overwrites tile 0's unsignaled flush fence in resv.

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

  reply	other threads:[~2026-12-02  0:19 UTC|newest]

Thread overview: 52+ 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 [this message]
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-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-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

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-244486@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