Re: [PATCH v3 18/33] drm/xe: Handle defrag moves in xe_bo_move()

From: Matthew Brost

Date: Tue Nov 24 2026 - 10:38:01 EST


On Fri, Nov 06, 2026 at 09:12:46PM -0800, Niranjana Vishwanathapura wrote:
> On Fri, Jul 10, 2026 at 07:56:04PM -0700, Matthew Brost wrote:
> > Teach xe_bo_move() to service a defrag move, where TTM reallocates a
> > BO's backing at the device's beneficial order and hands the old, still
> > populated tt to the move via the BO's defrag_old_tt.
> >
> > A defrag move always has existing contents to copy, so force
> > move_lacks_source false and fall through the XE_PL_TT -> XE_PL_TT no-op
> > shortcut so the copy path runs. The relocation is performed entirely on
> > the GPU via xe_migrate_copy_defrag(), reading the old pages from the
> > stashed tt's sg table and writing the freshly reallocated backing.
> >
> > Both copy passes run in order on the same migrate queue and are captured
> > by the returned fence, so the teardown is pipelined via
> > ttm_bo_move_accel_cleanup(): the old tt is handed to a ghost object and
> > only unpopulated and freed once the fence signals, so the move need not
> > block. The VF CCS attach/detach handling is extended to treat a defrag
> > move (ctx->defrag) like the XE_PL_SYSTEM <-> XE_PL_TT transitions it
> > mirrors.
> >
> > Cc: Carlos Santa <carlos.santa@xxxxxxxxx>
> > Cc: Ryan Neph <ryanneph@xxxxxxxxxx>
> > Cc: Christian Koenig <christian.koenig@xxxxxxx>
> > Cc: Huang Rui <ray.huang@xxxxxxx>
> > Cc: Matthew Auld <matthew.auld@xxxxxxxxx>
> > Cc: Maarten Lankhorst <maarten.lankhorst@xxxxxxxxxxxxxxx>
> > Cc: Maxime Ripard <mripard@xxxxxxxxxx>
> > Cc: Thomas Zimmermann <tzimmermann@xxxxxxx>
> > Cc: David Airlie <airlied@xxxxxxxxx>
> > Cc: Simona Vetter <simona@xxxxxxxx>
> > Cc: dri-devel@xxxxxxxxxxxxxxxxxxxxx
> > Cc: linux-kernel@xxxxxxxxxxxxxxx
> > Cc: Thomas Hellström <thomas.hellstrom@xxxxxxxxxxxxxxx>
> > Assisted-by: GitHub_Copilot:claude-opus-4.8
> > Signed-off-by: Matthew Brost <matthew.brost@xxxxxxxxx>
> > ---
> > drivers/gpu/drm/xe/xe_bo.c | 24 ++++++++++++++++++++----
> > 1 file changed, 20 insertions(+), 4 deletions(-)
> >
> > diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
> > index 02395968d613..bdcc9bed5d36 100644
> > --- a/drivers/gpu/drm/xe/xe_bo.c
> > +++ b/drivers/gpu/drm/xe/xe_bo.c
> > @@ -1170,6 +1170,13 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
> > move_lacks_source = !old_mem || (handle_system_ccs ? (!bo->ccs_cleared) :
> > (!mem_type_is_vram(old_mem_type) && !tt_has_data));
> >
> > + /*
> > + * A defrag move always copies the existing contents from the stashed
> > + * old tt into the freshly (re)allocated backing.
> > + */
> > + if (ttm_bo->defrag_old_tt)
> > + move_lacks_source = false;
> > +
> > needs_clear = (ttm && ttm->page_flags & TTM_TT_FLAG_ZERO_ALLOC) ||
> > (!ttm && ttm_bo->type == ttm_bo_type_device);
> >
> > @@ -1206,9 +1213,12 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
> > /*
> > * Failed multi-hop where the old_mem is still marked as
> > * TTM_PL_FLAG_TEMPORARY, should just be a dummy move.
> > + *
> > + * For a defrag move the old and new tt differ, so fallthrough to the
> > + * copy path instead of treating it as a no-op.
> > */
> > if (old_mem_type == XE_PL_TT &&
> > - new_mem->mem_type == XE_PL_TT) {
> > + new_mem->mem_type == XE_PL_TT && !ttm_bo->defrag_old_tt) {
> > ttm_bo_move_null(ttm_bo, new_mem);
> > goto out;
> > }
> > @@ -1279,6 +1289,12 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
> > flags |= XE_MIGRATE_CLEAR_FLAG_CCS_DATA;
> >
> > fence = xe_migrate_clear(migrate, bo, new_mem, flags);
> > + } else if (ttm_bo->defrag_old_tt) {
> > + struct xe_ttm_tt *old_xe_tt =
> > + container_of(ttm_bo->defrag_old_tt, struct xe_ttm_tt, ttm);
> > +
> > + fence = xe_migrate_copy_defrag(migrate, bo, old_mem, new_mem,
> > + old_xe_tt->sg, handle_system_ccs);
> > } else {
> > fence = xe_migrate_copy(migrate, bo, bo, old_mem, new_mem,
> > handle_system_ccs);
> > @@ -1315,13 +1331,13 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
> > * BBs from BO as it is no longer needed.
> > */
> > if (IS_VF_CCS_READY(xe) && old_mem_type == XE_PL_TT &&
> > - new_mem->mem_type == XE_PL_SYSTEM)
> > + (new_mem->mem_type == XE_PL_SYSTEM || ctx->defrag))
> > xe_sriov_vf_ccs_detach_bo(bo);
> >
> > if (IS_VF_CCS_READY(xe) &&
> > ((move_lacks_source && new_mem->mem_type == XE_PL_TT) ||
> > - (old_mem_type == XE_PL_SYSTEM && new_mem->mem_type == XE_PL_TT)) &&
> > - handle_system_ccs)
> > + ((old_mem_type == XE_PL_SYSTEM || ctx->defrag) &&
> > + new_mem->mem_type == XE_PL_TT)) && handle_system_ccs)
> > ret = xe_sriov_vf_ccs_attach_bo(bo);
>
> Assisted-by: LLM
>
> Looks like code has changed in drm-tip and sriov_vf_ccs_attach/detach changes here
> needs to be rebased.
>
> There are also some sashiko reported bugs around this sriov_vf_ccs_attach_bo which
> looks legitimate (like move() function returning earlier in error case while move
> is in progress).
>

Yea, I believe I fixed VF CCS issues here:

https://patchwork.freedesktop.org/series/170380/

Matt

> Niranjana
>
> >
> > out:
> > --
> > 2.34.1
> >