Re: [PATCH v6 05/35] drm/ttm: Support defragmentation moves
From: Natalie Vock
Date: Fri Dec 04 2026 - 09:05:03 EST
On Thursday, 3 December 2026 21:23 Matthew Brost wrote:
> Add infrastructure to let a driver re-back an already-resident BO with
> fresh pages (e.g. at a more beneficial order) by reusing the normal move
> machinery.
>
> Add ctx->defrag, which makes ttm_bo_validate() skip the "current
> resource already compatible" short-circuit so a move is forced even when
> placement is unchanged. In ttm_bo_handle_move_mem(), when defragging a
> populated BO, stash the old tt, create and populate a fresh one
> (allocating new backing), and hand the old tt to the driver move
> callback via bo->defrag_old_tt for copying. On success the old tt is
> unpopulated and freed; if populate or the move fails, the freshly
> allocated backing is discharged and the BO is restored to its original
> tt and placement.
>
> The old tt is handed off through bo->defrag_old_tt (reservation-lock
> protected) rather than the operation context. The pointer describes BO
> state for the duration of the move, so keeping it on the buffer object
> is better layering and lets ttm_bo_move_accel_cleanup() pick it up.
>
> Because a defrag move commonly fails (beneficial-order pages are often
> unavailable) and then leaves the BO untouched on its original backing,
> defer ttm_bo_unmap_virtual() for the defrag case until the new backing
> has been populated and the move is about to commit. Ordinary moves still
> unmap up front. This avoids needlessly zapping and refaulting every
> userspace mapping on the common failure path where nothing moved.
>
> Teach ttm_bo_move_accel_cleanup()'s ghost path about defrag: instead of
> tearing the move's source tt down synchronously, hand bo->defrag_old_tt
> to the ghost object so it is kept populated until the copy fence signals
> and then unbound and destroyed asynchronously, while the BO keeps its
> new tt. This lets a driver pipeline defrag teardown behind the GPU copy
> rather than blocking on it.
>
> This is driver-opt-in and a no-op unless ctx->defrag is set.
>
> v4:
> - Single unmap (Niranjana)
> - Warn on defrag_old_tt in TTM memcpy (Niranjana)
> - comment around -ENOMEM in ttm_pool_iter_lower_order (Niranjana)
> - warn on defrag in ttm_bo_bounce_temp_buffer (Niranjana)
>
> Cc: Carlos Santa <carlos.santa@xxxxxxxxx>
> Cc: Ryan Neph <ryanneph@xxxxxxxxxx>
> Cc: Natalie Vock <nat@xxxxxxxxxxxxxxxx>
> Cc: Arun Pravin <arunpravin.paneerselvam@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>
> Signed-off-by: Matthew Brost <matthew.brost@xxxxxxxxx>
> Reviewed-by: Niranjana Vishwanathapura <niranjana.vishwanathapura@xxxxxxxxx>
> ---
> drivers/gpu/drm/ttm/ttm_bo.c | 90 ++++++++++++++++++++++++++++---
> drivers/gpu/drm/ttm/ttm_bo_util.c | 18 +++++--
> drivers/gpu/drm/ttm/ttm_pool.c | 10 +++-
> include/drm/ttm/ttm_bo.h | 17 ++++++
> 4 files changed, 123 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
> index 53c68e29ebc2..a32b78c80989 100644
> --- a/drivers/gpu/drm/ttm/ttm_bo.c
> +++ b/drivers/gpu/drm/ttm/ttm_bo.c
> @@ -125,49 +125,119 @@ static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
> {
> struct ttm_device *bdev = bo->bdev;
> bool old_use_tt, new_use_tt;
> + bool defrag;
> int ret;
>
> old_use_tt = !bo->resource || ttm_manager_type(bdev, bo->resource->mem_type)->use_tt;
> new_use_tt = ttm_manager_type(bdev, mem->mem_type)->use_tt;
>
> - ttm_bo_unmap_virtual(bo);
> + /*
> + * Only the BO that is the actual defrag target (moved in place via
> + * ttm_bo_validate(), evict == false) is being defragmented. The same
> + * ctx->defrag is threaded into evictions triggered while allocating
> + * that target's new backing (ttm_bo_evict_alloc()), so guard against
> + * an unrelated victim BO (evict == true) being misclassified as a
> + * defrag move and needlessly reallocating its backing.
> + */
> + defrag = ctx->defrag && !evict && new_use_tt && bo->ttm &&
> + ttm_tt_is_populated(bo->ttm);
If ctx->defrag && !evict, any other part of this condition failing is
invalid and means a driver bug, doesn't it?
defrag = ctx->defrag && !evict;
if (defrag && WARN_ON(!new_use_tt || !bo->ttm || !ttm_tt_is_populated(bo->ttm))
return -EINVAL;
(or WARN_ON_ONCE, see below)
>
> /*
> * Create and bind a ttm if required.
> */
>
> if (new_use_tt) {
> + struct ttm_tt *defrag_old_tt = NULL;
> +
> + /*
> + * For a defrag move, stash the current populated tt and force a
> + * fresh one to be created and populated (at beneficial order).
> + * The old tt is handed to the driver move callback to copy the
> + * contents from, and freed once the move completes.
> + */
> + if (defrag) {
> + defrag_old_tt = bo->ttm;
> + bo->ttm = NULL;
> + }
> +
> /* Zero init the new TTM structure if the old location should
> * have used one as well.
> */
> - ret = ttm_tt_create(bo, old_use_tt);
> - if (ret)
> + ret = ttm_tt_create(bo, old_use_tt && !defrag_old_tt);
> + if (ret) {
> + if (defrag_old_tt)
> + bo->ttm = defrag_old_tt;
> goto out_err;
> + }
>
> - if (mem->mem_type != TTM_PL_SYSTEM) {
> + if (defrag_old_tt || mem->mem_type != TTM_PL_SYSTEM) {
> ret = ttm_bo_populate(bo, ctx);
> - if (ret)
> + if (ret) {
> + /*
> + * Discharge the freshly allocated backing and
> + * leave the BO on its original tt/placement.
> + */
> + if (defrag_old_tt) {
> + ttm_tt_destroy(bo->bdev, bo->ttm);
> + bo->ttm = defrag_old_tt;
> + }
> goto out_err;
> + }
> }
> +
> + bo->defrag_old_tt = defrag_old_tt;
> }
>
> ret = dma_resv_reserve_fences(bo->base.resv, 1);
> if (ret)
> goto out_err;
>
> + /*
> + * Only tear down mappings after populate succeeds, as population can
> + * commonly fail during defragmentation moves.
> + */
> + ttm_bo_unmap_virtual(bo);
> +
> ret = bdev->funcs->move(bo, evict, ctx, mem, hop);
> if (ret) {
> - if (ret == -EMULTIHOP)
> + if (ret == -EMULTIHOP) {
> + /*
> + * A defrag move targets the BO's current placement and
> + * must complete in a single hop. A driver requesting a
> + * bounce here is unsupported: restore the original
> + * backing and fail the defrag rather than retrying the
> + * multihop with ctx->defrag still set, which would
> + * misclassify the bounce as another defrag move and
> + * overwrite/leak bo->defrag_old_tt.
> + */
> + if (unlikely(defrag)) {
> + WARN_ON_ONCE(1);
Here you use WARN_ON_ONCE (which, btw, could probably be folded into the if like so:)
if (WARN_ON_ONCE(defrag))
which should imply unlikely()
> + ret = -EINVAL;
> + goto out_err;
> + }
> return ret;
> + }
> goto out_err;
> }
>
> + if (bo->defrag_old_tt) {
> + ttm_tt_unpopulate(bo->bdev, bo->defrag_old_tt);
> + ttm_tt_destroy(bo->bdev, bo->defrag_old_tt);
> + bo->defrag_old_tt = NULL;
> + }
> +
> ctx->bytes_moved += bo->base.size;
> return 0;
>
> out_err:
> - if (!old_use_tt)
> + if (bo->defrag_old_tt) {
> + /* Failed defrag move: restore the original backing. */
> ttm_bo_tt_destroy(bo);
> + bo->ttm = bo->defrag_old_tt;
> + bo->defrag_old_tt = NULL;
> + } else if (!old_use_tt) {
> + ttm_bo_tt_destroy(bo);
> + }
>
> return ret;
> }
> @@ -339,6 +409,10 @@ static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
> struct ttm_resource *hop_mem;
> int ret;
>
> + /* We shouldn't be bouncing temp buffers during a defrag */
> + if (WARN_ON(ctx->defrag))
> + return -EINVAL;
but here you use WARN_ON without ONCE. Any reason for that mismatch?
I get not wanting to spam the journal on every move if something in the
driver is busted, but then we should be consistent.
> +
> hop_placement.num_placement = 1;
> hop_placement.placement = hop;
>
> @@ -1090,7 +1164,7 @@ int ttm_bo_validate(struct ttm_buffer_object *bo,
> force_space = false;
> do {
> /* Check whether we need to move buffer. */
> - if (bo->resource &&
> + if (bo->resource && !ctx->defrag &&
> ttm_resource_compatible(bo->resource, placement,
> force_space))
> return 0;
> diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c b/drivers/gpu/drm/ttm/ttm_bo_util.c
> index 8c5de3a0506f..21a25048d64d 100644
> --- a/drivers/gpu/drm/ttm/ttm_bo_util.c
> +++ b/drivers/gpu/drm/ttm/ttm_bo_util.c
> @@ -161,7 +161,7 @@ int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
> bool clear;
> int ret = 0;
>
> - if (WARN_ON(!src_mem))
> + if (WARN_ON(!src_mem) || WARN_ON(bo->defrag_old_tt))
> return -EINVAL;
>
> src_man = ttm_manager_type(bdev, src_mem->mem_type);
> @@ -653,10 +653,22 @@ static int ttm_bo_move_to_ghost(struct ttm_buffer_object *bo,
> * bo to be unbound and destroyed.
> */
>
> - if (dst_use_tt)
> + if (bo->defrag_old_tt) {
> + /*
> + * Defrag move: @bo already points at the freshly allocated,
> + * beneficial-order tt that the move copied into. Hand the old
> + * tt (the move's source) to the ghost so it is kept populated
> + * until @fence signals and then unbound and destroyed, instead
> + * of being torn down synchronously. @bo keeps its new tt.
> + */
> + ghost_obj->ttm = bo->defrag_old_tt;
> + ghost_obj->defrag_old_tt = NULL;
ghost_obj->defrag_old_tt needs to be in ttm_bo_transfer.
(I really need to get around to the per-member-copy TODO there, I don't
like this footgun at all)
> + bo->defrag_old_tt = NULL;
> + } else if (dst_use_tt) {
> ghost_obj->ttm = NULL;
> - else
> + } else {
> bo->ttm = NULL;
> + }
>
> dma_resv_unlock(&ghost_obj->base._resv);
> ttm_bo_put(ghost_obj);
> diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
> index a053d78191f6..182e84efcd92 100644
> --- a/drivers/gpu/drm/ttm/ttm_pool.c
> +++ b/drivers/gpu/drm/ttm/ttm_pool.c
> @@ -875,9 +875,17 @@ static int ttm_pool_iter_lower_order(struct ttm_pool_alloc_iter *it)
> * pages. Record it so the driver can later try to defragment the object
> * back to beneficial order.
> */
> - if (it->beneficial_order && it->order == it->beneficial_order)
> + if (it->beneficial_order && it->order == it->beneficial_order) {
> it->tt->page_flags |= TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED;
>
> + /*
> + * If we are defragmenting, we only want beneficial_order, so
> + * any failure is treated as -ENOMEM.
> + */
> + if (it->ctx->defrag)
> + return -ENOMEM;
> + }
> +
> it->order--;
> it->page_caching = it->tt->caching;
> it->allow_pools = true;
> diff --git a/include/drm/ttm/ttm_bo.h b/include/drm/ttm/ttm_bo.h
> index cd3d3980dbce..c5205b8e8f10 100644
> --- a/include/drm/ttm/ttm_bo.h
> +++ b/include/drm/ttm/ttm_bo.h
> @@ -135,6 +135,16 @@ struct ttm_buffer_object {
> * reservation lock.
> */
> struct sg_table *sg;
> +
> + /**
> + * @defrag_old_tt: Used internally during a defrag move to hand the BO's
> + * previous, populated struct ttm_tt to the driver's move callback so
> + * that it can copy the contents into the freshly allocated
> + * beneficial-order backing. Set by TTM, consumed by the driver; the old
> + * tt is freed (or restored on failure) by TTM once the move completes.
> + * Protected by the reservation lock.
> + */
> + struct ttm_tt *defrag_old_tt;
FWIW, I don't exactly love stashing temporary state in BO members like
this.
We already have the "evict" being passed around in a similar way as
defrag_old_tt needs to be passed around, i.e. given to the move_notify
callback and then passed to accel_cleanup.
Maybe we could refactor the move_notify callback to take a some kind of
"notify data" struct that contains these kinds of parameters and then
add defrag_old_tt there?
Natalie
> };
>
> #define TTM_BO_MAP_IOMEM_MASK 0x80
> @@ -194,6 +204,13 @@ struct ttm_operation_ctx {
> * reclaim/compaction before falling back to a smaller order anyway.
> */
> bool beneficial_reclaim_backoff;
> + /**
> + * @defrag: Defragmentation pass. Attempt a move even when the bo's
> + * current resource is already compatible with the requested placement,
> + * so that the backing store can be reallocated at the pool's beneficial
> + * order.
> + */
> + bool defrag;
> /**
> * @resv: Reservation object to be used together with
> * @allow_res_evict.
>