Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Matthew Brost <matthew.brost@intel.com>
To: <sashiko-reviews@lists.linux.dev>
Cc: <intel-xe@lists.freedesktop.org>, <dri-devel@lists.freedesktop.org>
Subject: Re: [PATCH v4 18/33] drm/xe: Add a page defragmentation worker
Date: Wed, 2 Dec 2026 13:19:48 -0800	[thread overview]
Message-ID: <axCLdKYSvp67mTcB@gsse-cloud1.jf.intel.com> (raw)
In-Reply-To: <sashiko-outbox-244494@kernel.org>

On Wed, Dec 02, 2026 at 12:23:05AM +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 6 potential
> issues to consider:
> - [High] drm/xe: missing suspend and FLR synchronization in
>   xe_bo_defrag_worker()
> - [High] drm/xe: TTM_PL_FLAG_TEMPORARY left on resource in
>   xe_bo_defrag_one()
> - [High] drm/xe: defrag worker stalls on active device in
>   xe_bo_defrag_worker()
> - [Medium] drm/xe: BO lost from defrag list on move failure in
>   xe_bo_move()
> - [Medium] drm/xe: defrag worker not re-armed on xe_pm_runtime_suspend()
>   failure

Above here all real. Will fix.

> - [Low] drm/xe: unlocked interval_ms read and stale timer in
>   xe_bo_defrag_schedule()

This one not real.

Matt

> --
> 
> commit 1c62d5735db470f5d28289297de300836bbb6ef4
> Author: Matthew Brost <matthew.brost@intel.com>
> 
> drm/xe: Add a page defragmentation worker
> 
> This commit adds a per-device delayed worker that walks the list of buffer
> objects backed by sub-optimal TT page orders and forces a TTM move to
> reallocate their backing pages at the device's beneficial order, bounded
> by a per-run byte budget.
> 
> > diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
> > index 689804916d6ba..37d13c7a715c7 100644
> > --- a/drivers/gpu/drm/xe/xe_bo.c
> > +++ b/drivers/gpu/drm/xe/xe_bo.c
> 
> [ ... ]
> 
> > @@ -1201,17 +1238,85 @@ int xe_ttm_bo_purge(struct ttm_buffer_object *ttm_bo, struct ttm_operation_ctx *
> > +static void xe_bo_defrag_schedule(struct xe_device *xe)
> > +{
> > +	schedule_delayed_work(&xe->mem.defrag.worker,
> > +			      msecs_to_jiffies(xe->mem.defrag.interval_ms));
> > +}
> 
> [Severity: Low]
> Should xe_bo_defrag_schedule() read xe->mem.defrag.interval_ms under
> xe->mem.defrag.lock (or pass the interval in) and use mod_delayed_work()
> instead of schedule_delayed_work()?
> 
> All writes to interval_ms hold xe->mem.defrag.lock, whereas
> xe_bo_defrag_schedule() reads it after dropping the lock.  In addition, if
> a failed pass backs off interval_ms (up to 15 seconds) and schedules the
> delayed work, and all BOs are then removed before a new BO is added via
> xe_bo_defrag_add() (or xe_bo_defrag_resume() runs), interval_ms is reset
> to 25 ms under the lock, but schedule_delayed_work() is a no-op while the
> backed-off timer is still pending.
> 
> [ ... ]
> 
> > @@ -1301,6 +1415,211 @@ static void xe_bo_defrag_update(struct xe_bo *bo)
> > +static int xe_bo_defrag_one(struct xe_device *xe, struct xe_bo *bo,
> > +			    u64 budget, u64 *consumed, bool *needs_more)
> > +{
> > +	struct ttm_operation_ctx ctx = {
> > +		.gfp_retry_mayfail = true,
> > +		.defrag = true,
> > +		.defrag_bytes_remaining = budget,
> > +	};
> > +	struct ttm_buffer_object *ttm_bo = &bo->ttm;
> > +	struct ttm_placement placement;
> > +	struct ttm_place place;
> > +	int ret = 0;
> > +
> > +	*consumed = 0;
> > +	*needs_more = false;
> > +
> > +	xe_bo_lock(bo, false);
> > +
> > +	/* Re-check eligibility under the BO lock. */
> > +	if (!xe_bo_needs_defrag(bo)) {
> > +		xe_bo_defrag_remove(bo);
> > +		goto unlock;
> > +	}
> > +
> > +	xe_place_from_ttm_type(ttm_bo->resource->mem_type, &place);
> > +	place.flags |= TTM_PL_FLAG_TEMPORARY;
> > +	placement.num_placement = 1;
> > +	placement.placement = &place;
> > +
> > +	/*
> > +	 * On success the move reallocates the backing at beneficial order and
> > +	 * drops the BO from the defrag list. On failure the BO keeps its
> > +	 * original backing and stays on the list for a later retry.
> > +	 */
> > +	ret = ttm_bo_validate(ttm_bo, &placement, &ctx);
> 
> [Severity: High]
> Does passing TTM_PL_FLAG_TEMPORARY in place.flags leave
> TTM_PL_FLAG_TEMPORARY permanently set on ttm_bo->resource->placement after
> ttm_bo_validate() succeeds?
> 
> When xe_ttm_sys_mgr_new() allocates the replacement XE_PL_TT resource,
> ttm_resource_init() copies place->flags into res->placement.  Because the
> defrag move calls xe_bo_move_notify() -> xe_bo_trigger_rebind(), the BO is
> re-validated on the next submission or in preempt_rebind_work_func().
> ttm_resource_compatible() unconditionally returns false whenever
> res->placement & TTM_PL_FLAG_TEMPORARY is set, forcing ttm_bo_validate()
> to reject the freshly defragmented resource, tear down CPU mappings a
> second time, and temporarily double-charge the BO's size against
> xe->mem.sys_mgr while the old resource is still allocated (which can fail
> with -ENOSPC/-ENOMEM when usage exceeds man->size and kill an LR VM in
> preempt_rebind_work_func()).
> 
> [Severity: Medium]
> Can a BO be lost from xe->mem.defrag.list if ttm_bo_populate() succeeds
> at the beneficial order during a defrag move, but xe_bo_move() subsequently
> fails?
> 
> In ttm_bo_handle_move_mem(), TTM stashes the old sub-optimal ttm_tt in
> bo->defrag_old_tt, populates the new bo->ttm (with
> TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED clear), and calls xe_bo_move().  If
> xe_bo_move() fails (for example in xe_tt_map_sg(), xe_bo_move_notify(), or
> xe_migrate_copy_defrag()), its error exit runs xe_bo_defrag_update(bo)
> while bo->ttm.ttm still points to the new ttm_tt:
> 
> drivers/gpu/drm/ttm/ttm_bo.c:ttm_bo_handle_move_mem() {
> 	...
> 	ret = bdev->funcs->move(bo, evict, ctx, mem, hop);
> 	if (ret) {
> 		if (ret == -EMULTIHOP)
> 			return ret;
> 		goto out_err;
> 	}
> 	...
> 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 xe_bo_defrag_update(bo) sees the temporary new ttm_tt before
> out_err restores bo->ttm = bo->defrag_old_tt, it removes the BO from
> xe->mem.defrag.list, and xe_bo_defrag_one() does not re-run
> xe_bo_defrag_update(bo) after ttm_bo_validate() returns the error.
> 
> > +
> > +	/*
> > +	 * The pool allocator decremented defrag_bytes_remaining as it
> > +	 * (re)allocated pages, so the unused budget tells us how much was spent.
> > +	 * Only meaningful when the move succeeded. A successful move may spend
> > +	 * nothing - e.g. when the object was already fully upgraded and every
> > +	 * chunk is harvested (borrowed) from the old backing for free - so a
> > +	 * zero spend is legitimate and must be charged as zero, not the whole
> > +	 * budget.
> > +	 */
> > +	if (!ret) {
> > +		s64 spent = (s64)budget - ctx.defrag_bytes_remaining;
> > +
> > +		*consumed = spent > 0 ? spent : 0;
> > +	}
> > +
> > +	xe_dbg(xe, "Defrag attempt on BO size=%zu: ret=%pe consumed=%llu\n",
> > +	       xe_bo_size(bo), ERR_PTR(ret), *consumed);
> > +
> > +	if (!ret && ttm_tt_is_beneficial_order_failed(bo->ttm.ttm))
> > +		*needs_more = true;
> > +
> > +unlock:
> > +	xe_bo_unlock(bo);
> > +	return ret;
> > +}
> > +
> > +static void xe_bo_defrag_worker(struct work_struct *w)
> > +{
> > +	struct delayed_work *dwork = to_delayed_work(w);
> > +	struct xe_device *xe =
> > +		container_of(dwork, struct xe_device, mem.defrag.worker);
> > +	u64 defrag_bytes = 0;
> > +	bool requeue = false;
> > +	int idx;
> > +
> > +	if (!drm_dev_enter(&xe->drm, &idx))
> > +		return;
> > +
> > +	if (!xe_pm_runtime_get_if_in_use(xe)) {
> > +		/*
> > +		 * The device is (runtime) suspending/suspended. Do not spin a
> > +		 * wakeup timer here: the worker is re-armed from the PM resume
> > +		 * path (xe_bo_defrag_resume()) once the device is active again,
> > +		 * so tracked BOs are picked up without polling while suspended.
> > +		 */
> > +		drm_dev_exit(idx);
> > +		return;
> > +	}
> 
> [Severity: High]
> Can xe_bo_defrag_worker() run concurrently with or after
> xe_device_suspend() during system suspend, hibernation, or PCI FLR?
> 
> xe_bo_defrag_schedule() queues xe->mem.defrag.worker on system_percpu_wq
> (not WQ_FREEZABLE), and neither xe_pm_notifier_callback(),
> xe_pm_suspend(), nor xe_device_suspend() cancels or disables the worker,
> nor does xe_bo_defrag_worker() check try_wait_for_completion(&xe->pm_block).
> During system suspend/hibernation, device_prepare() and
> xe_pm_notifier_callback(PM_SUSPEND_PREPARE) hold runtime PM usage
> references while runtime_status remains RPM_ACTIVE until
> device_suspend_late(), and evicting user VRAM BOs to XE_PL_TT in
> xe_bo_evict_all_user() / xe_bo_evict_all() can call xe_bo_defrag_add() and
> arm the 25 ms timer:
> 
> CPU0 (system suspend / FLR)       CPU1 (system_percpu_wq)
> xe_pm_notifier_callback()
>   xe_pm_runtime_get(xe)
>   xe_bo_evict_all_user(xe)
>     ...
>       xe_bo_defrag_add()
>         xe_bo_defrag_schedule()
> xe_pm_suspend()
>   xe_device_suspend()
>     xe_bo_evict_all(xe)
>     xe_gt_suspend(gt)
>     xe_irq_suspend(xe)
>                                   xe_bo_defrag_worker()
>                                     xe_pm_runtime_get_if_in_use(xe) -> true
>                                     xe_bo_defrag_one()
>                                       ...
>                                         xe_migrate_copy_defrag()
>                                           <-- submits GPU job while GT
>                                               is suspended / kernel BOs
>                                               are evicted
> 
> In addition, xe_pci_reset_done() calls xe_device_resume(xe, true) directly
> rather than xe_pm_resume(), so xe_bo_defrag_resume(xe) is not called after
> an FLR.
> 
> [Severity: High]
> Does using xe_pm_runtime_get_if_in_use(xe) here stall background
> defragmentation while the device is active in its autosuspend window?
> 
> Normal DRM ioctls hold a runtime PM usage reference only for the duration
> of the syscall, leaving dev->power.runtime_status == RPM_ACTIVE with
> dev->power.usage_count == 0 during the 1000 ms autosuspend delay.  When
> xe_bo_defrag_worker() fires 25 ms after an ioctl adds a BO,
> xe_pm_runtime_get_if_in_use(xe) returns false whenever usage_count is 0
> (unlike xe_pm_runtime_get_if_active(xe)), and the worker exits without
> rescheduling.  Because the device is already RPM_ACTIVE, subsequent GPU
> activity before autosuspend does not trigger xe_pm_runtime_resume() ->
> xe_bo_defrag_resume(), and because xe->mem.defrag.list is already
> non-empty, subsequent calls to xe_bo_defrag_add() see
> list_empty(&xe->mem.defrag.list) == false and never re-kick the worker.
> 
> > diff --git a/drivers/gpu/drm/xe/xe_pm.c b/drivers/gpu/drm/xe/xe_pm.c
> > index f73f68bd449b8..f76ab97ca49e2 100644
> > --- a/drivers/gpu/drm/xe/xe_pm.c
> > +++ b/drivers/gpu/drm/xe/xe_pm.c
> 
> [ ... ]
> 
> > @@ -689,6 +691,8 @@ int xe_pm_runtime_resume(struct xe_device *xe)
> >  	if (xe->d3cold.allowed)
> >  		xe_late_bind_pm_resume(&xe->late_bind);
> >  
> > +	xe_bo_defrag_resume(xe);
> > +
> >  out:
> >  	xe_rpm_lockmap_release(xe);
> >  	xe_pm_write_callback_task(xe, NULL);
> 
> [Severity: Medium]
> If xe_bo_defrag_worker() fires while runtime_status is RPM_SUSPENDING and
> exits early, what re-arms it if xe_pm_runtime_suspend() subsequently fails?
> 
> When xe_pm_runtime_suspend() fails and unwinds through out_resume or out,
> rpm_suspend() reverts dev->power.runtime_status back to RPM_ACTIVE without
> calling xe_pm_runtime_resume(), leaving a non-empty xe->mem.defrag.list
> without a scheduled worker until the next full runtime suspend/resume
> cycle.
> 
> -- 
> Sashiko AI review · https://sashiko.dev/#/patchset/20261201234915.3305331-1-matthew.brost@intel.com?part=18

  reply	other threads:[~2026-12-02 21:20 UTC|newest]

Thread overview: 83+ 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-02  6:02   ` Niranjana Vishwanathapura
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-02  6:02   ` Niranjana Vishwanathapura
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-02  5:17   ` kernel test robot
2026-12-02  6:08   ` Niranjana Vishwanathapura
2026-12-01 23:48 ` [PATCH v4 05/33] drm/ttm: Support defragmentation moves Matthew Brost
2026-12-02  6:03   ` Niranjana Vishwanathapura
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-02  6:09   ` Niranjana Vishwanathapura
2026-12-02 15:41   ` kernel test robot
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-02 19:26     ` Matthew Brost
2026-12-02 20:00   ` kernel test robot
2026-12-01 23:48 ` [PATCH v4 08/33] drm/ttm: Bound page (re)allocation per defragmentation move Matthew Brost
2026-12-02  6:10   ` Niranjana Vishwanathapura
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-02 19:55     ` Matthew Brost
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-02 20:04     ` Matthew Brost
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-02 20:16     ` Matthew Brost
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-02 20:44     ` Matthew Brost
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-02  6:42   ` Niranjana Vishwanathapura
2026-12-02 20:52     ` 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-02 21:01     ` Matthew Brost
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-02 21:07     ` Matthew Brost
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-02 21:19     ` Matthew Brost [this message]
2026-12-01 23:49 ` [PATCH v4 19/33] drm/xe: Add defrag GT stats Matthew Brost
2026-12-02  6:54   ` Niranjana Vishwanathapura
2026-12-02 21:24     ` 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-02 21:27     ` Matthew Brost
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-02 21:01   ` Niranjana Vishwanathapura
2026-12-01 23:49 ` [PATCH v4 25/33] drm/xe: Add IOVA-based xe_res_cursor variant Matthew Brost
2026-12-02  7:35   ` Niranjana Vishwanathapura
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-02 19:29   ` Niranjana Vishwanathapura
2026-12-01 23:49 ` [PATCH v4 28/33] drm/xe: Add packed copy-step IOVA mapping for defrag Matthew Brost
2026-12-02 20:24   ` Niranjana Vishwanathapura
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-02 10:22   ` Matthew Auld
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
2026-12-02 18:50 ` ✓ Xe.CI.FULL: " 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=axCLdKYSvp67mTcB@gsse-cloud1.jf.intel.com \
    --to=matthew.brost@intel.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --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