From: Niranjana Vishwanathapura <niranjana.vishwanathapura@intel.com>
To: Matthew Brost <matthew.brost@intel.com>
Cc: intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
"Carlos Santa" <carlos.santa@intel.com>,
"Ryan Neph" <ryanneph@google.com>,
"Natalie Vock" <nat@pixelcluster.dev>,
"Arun Pravin" <arunpravin.paneerselvam@amd.com>,
"Matthew Auld" <matthew.auld@intel.com>,
"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
"Maxime Ripard" <mripard@kernel.org>,
"Thomas Zimmermann" <tzimmermann@suse.de>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
linux-kernel@vger.kernel.org,
"Thomas Hellström" <thomas.hellstrom@linux.intel.com>
Subject: Re: [PATCH v6 08/35] drm/ttm: Harvest beneficial-order pages on defragmentation moves
Date: Thu, 3 Dec 2026 15:11:25 -0800 [thread overview]
Message-ID: <axH3Hex6J8RdK-Mp@nvishwa1-desk> (raw)
In-Reply-To: <20261203202352.4153159-9-matthew.brost@intel.com>
On Thu, Dec 03, 2026 at 12:23:25PM -0800, Matthew Brost wrote:
>A defragmentation move relocates a populated tt into a freshly allocated
>one so that sub-beneficial-order chunks can be upgraded to the device's
>beneficial order. Previously, if allocation could not complete entirely
>at beneficial order, the move failed and all progress was discarded.
>
>Instead, once allocation has successfully upgraded part of the object,
>the remainder of the old tt is harvested to complete the new tt. Thread
>the BO's old, still-populated tt into the page allocator via a new
>ttm_operation_ctx::defrag_old_tt. While allocation is still proceeding,
>any existing chunk already at (or above) the beneficial order is reused
>directly instead of being reallocated. If allocation later has to fall
>back below beneficial_order after making partial progress, the remainder
>of the old tt is harvested instead of failing the move. This preserves
>the beneficial-order upgrades already made while leaving the rest of the
>object unchanged, allowing subsequent defragmentation attempts to make
>additional forward progress.
>
>Harvesting only borrows the pages; they remain owned by the old tt until
>the move commits. This keeps every unwind path non-destructive: if the
>(re)allocation or the move later fails, the new tt is torn down without
>freeing the borrowed pages and the old tt is restored fully intact.
>Ownership of the shared pages is transferred to the new tt only once the
>move has committed. ttm_tt_defrag_disown_borrowed() implements both
>directions of the transfer by clearing a tt's slots for every page it
>shares with the other tt (same struct page at the same index), so that
>tt's teardown leaves the shared pages alone and each page is freed
>exactly once by its owner:
>
>- on the alloc unwind and the failed-move paths, the new tt disowns the
> borrowed pages, leaving the old tt as the sole owner;
>- on the successful-move paths (synchronous teardown and the pipelined
> ghost-object teardown), the old tt disowns the now-transferred pages,
> leaving the new tt as the sole owner.
>
>Detecting shared pages by same-index struct page identity needs no extra
>per-tt bookkeeping, since two distinct allocations never alias. This is
>safe with drivers, such as Xe, that build their own per-tt sg table and
>DMA-map it independently: each tt maps and unmaps its own sg, so sharing
>the underlying pages between tts is DMA-clean.
>
>This is a no-op unless ctx->defrag_old_tt is set, which only happens on a
>defragmentation move.
>
>v4:
> - Check for backup handle in ttm_pool_harvest_remaining (Niranjana)
>v5:
> - Harden harvest code around index / order assumptions (Sashiko)
>
>Cc: Carlos Santa <carlos.santa@intel.com>
>Cc: Ryan Neph <ryanneph@google.com>
>Cc: Natalie Vock <nat@pixelcluster.dev>
>Cc: Arun Pravin <arunpravin.paneerselvam@amd.com>
>Cc: Matthew Auld <matthew.auld@intel.com>
>Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
>Cc: Maxime Ripard <mripard@kernel.org>
>Cc: Thomas Zimmermann <tzimmermann@suse.de>
>Cc: David Airlie <airlied@gmail.com>
>Cc: Simona Vetter <simona@ffwll.ch>
>Cc: dri-devel@lists.freedesktop.org
>Cc: linux-kernel@vger.kernel.org
>Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
>Signed-off-by: Matthew Brost <matthew.brost@intel.com>
LGTM.
Reviewed-by: Niranjana Vishwanathapura <niranjana.vishwanathapura@intel.com>
>---
> drivers/gpu/drm/ttm/ttm_bo.c | 20 ++-
> drivers/gpu/drm/ttm/ttm_bo_util.c | 5 +
> drivers/gpu/drm/ttm/ttm_pool.c | 259 ++++++++++++++++++++++++++++--
> drivers/gpu/drm/ttm/ttm_tt.c | 67 ++++++++
> include/drm/ttm/ttm_bo.h | 12 ++
> include/drm/ttm/ttm_tt.h | 2 +
> 6 files changed, 348 insertions(+), 17 deletions(-)
>
>diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
>index a32b78c80989..67a97a5e0dfb 100644
>--- a/drivers/gpu/drm/ttm/ttm_bo.c
>+++ b/drivers/gpu/drm/ttm/ttm_bo.c
>@@ -171,7 +171,14 @@ static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
> }
>
> if (defrag_old_tt || mem->mem_type != TTM_PL_SYSTEM) {
>+ /*
>+ * Let the page allocator harvest already
>+ * beneficial-order chunks out of the old tt to back
>+ * the new one, rather than reallocating them.
>+ */
>+ ctx->defrag_old_tt = defrag_old_tt;
> ret = ttm_bo_populate(bo, ctx);
>+ ctx->defrag_old_tt = NULL;
> if (ret) {
> /*
> * Discharge the freshly allocated backing and
>@@ -221,6 +228,12 @@ static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
> }
>
> if (bo->defrag_old_tt) {
>+ /*
>+ * Successful defrag move: any pages the new tt borrowed from the
>+ * old tt are now owned by the new tt. Disown them from the old
>+ * tt before it is torn down so they are not freed.
>+ */
>+ ttm_tt_defrag_disown_borrowed(bo->defrag_old_tt, bo->ttm);
> ttm_tt_unpopulate(bo->bdev, bo->defrag_old_tt);
> ttm_tt_destroy(bo->bdev, bo->defrag_old_tt);
> bo->defrag_old_tt = NULL;
>@@ -231,7 +244,12 @@ static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
>
> out_err:
> if (bo->defrag_old_tt) {
>- /* Failed defrag move: restore the original backing. */
>+ /*
>+ * Failed defrag move: drop any pages the new tt borrowed from
>+ * the old tt (still owned by the old tt) before destroying the
>+ * new tt, then restore the original backing.
>+ */
>+ 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;
>diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c b/drivers/gpu/drm/ttm/ttm_bo_util.c
>index 21a25048d64d..4799b06fbb1a 100644
>--- a/drivers/gpu/drm/ttm/ttm_bo_util.c
>+++ b/drivers/gpu/drm/ttm/ttm_bo_util.c
>@@ -660,7 +660,12 @@ static int ttm_bo_move_to_ghost(struct ttm_buffer_object *bo,
> * 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.
>+ *
>+ * Any pages the new tt borrowed from the old tt are now owned by
>+ * the new tt; disown them from the old tt so the ghost teardown
>+ * leaves them alone.
> */
>+ ttm_tt_defrag_disown_borrowed(bo->defrag_old_tt, bo->ttm);
> ghost_obj->ttm = bo->defrag_old_tt;
> ghost_obj->defrag_old_tt = NULL;
> bo->defrag_old_tt = NULL;
>diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
>index 5c7fec62aa30..facff745cc7d 100644
>--- a/drivers/gpu/drm/ttm/ttm_pool.c
>+++ b/drivers/gpu/drm/ttm/ttm_pool.c
>@@ -808,6 +808,160 @@ static unsigned int ttm_pool_alloc_find_order(unsigned int highest,
> return min_t(unsigned int, highest, __fls(alloc->remaining_pages));
> }
>
>+/*
>+ * Defragmentation page harvesting.
>+ *
>+ * A defrag move relocates a populated tt into a freshly allocated one in order
>+ * to upgrade sub-beneficial-order chunks to the device's beneficial order.
>+ * Chunks in the old tt (ctx->defrag_old_tt) that are ALREADY at the beneficial
>+ * order do not need to be reallocated: their physical pages can simply be
>+ * transferred ("harvested") into the new tt at the same offset, avoiding the
>+ * alloc/free churn that is significant for large buffers.
>+ *
>+ * Harvesting only borrows the pages: the new tt simply references the old tt's
>+ * pages, which remain owned by the old tt. This keeps the unwind paths simple -
>+ * if the (re)allocation or the move later fails, the new tt is torn down without
>+ * freeing the borrowed pages and the old tt is restored fully intact. Ownership
>+ * of the shared pages is transferred to the new tt only once the move has
>+ * committed, via ttm_tt_defrag_disown_borrowed() (see ttm_tt.c).
>+ */
>+
>+/*
>+ * Return the allocation order of the old tt chunk whose head sits at @off, or 0
>+ * if the slot is empty / backed up. @cursor monotonically tracks the old chunk
>+ * boundary and must be advanced past every offset that has already been
>+ * queried; a page order may only be read from a chunk head page, hence the lazy
>+ * walk.
>+ *
>+ * Old tt chunks are self-aligned (see __ttm_pool_alloc()) and defrag only ever
>+ * advances @off by whole old chunks or by fresh chunks at least as large as the
>+ * old chunks they replace, so @off is always an old chunk boundary. Should that
>+ * invariant ever break, borrowing from @off would split an old chunk (treating
>+ * its tail pages as order-0 chunks) and corrupt page ownership between the old
>+ * and new tt, so fail the move instead.
>+ *
>+ * Returns: the old chunk order, or -EINVAL if @off is not an old chunk head.
>+ */
>+static int ttm_pool_defrag_old_order(struct ttm_pool *pool,
>+ struct ttm_tt *old_tt,
>+ pgoff_t off, pgoff_t *cursor)
>+{
>+ struct page *p;
>+
>+ while (*cursor < off) {
>+ p = old_tt->pages[*cursor];
>+ *cursor += (!p || ttm_backup_page_ptr_is_handle(p)) ? 1 :
>+ 1UL << ttm_pool_page_order(pool, p);
>+ }
>+
>+ if (WARN_ON_ONCE(*cursor != off))
>+ return -EINVAL;
>+
>+ p = old_tt->pages[off];
>+ if (!p || ttm_backup_page_ptr_is_handle(p))
>+ return 0;
>+
>+ return ttm_pool_page_order(pool, p);
>+}
>+
>+/*
>+ * Borrow an already beneficial-order chunk from the old tt into @alloc. The
>+ * pages stay owned by the old tt until the move commits.
>+ */
>+static int ttm_pool_harvest_page(struct ttm_tt *old_tt, unsigned int order,
>+ pgoff_t off, struct ttm_pool_alloc_state *alloc)
>+{
>+ pgoff_t nr = 1UL << order;
>+ int r;
>+
>+ /*
>+ * A harvested chunk keeps the (same) bo's caching, so the deferred
>+ * caching range is consistent here; flush it before committing, exactly
>+ * like the caching-consistent path in ttm_pool_page_allocated().
>+ */
>+ r = ttm_pool_apply_caching(alloc);
>+ if (r)
>+ return r;
>+
>+ /*
>+ * The borrowed chunk's page pointers (consecutive struct pages of one
>+ * compound allocation) and dma addresses already sit contiguously in
>+ * the old tt at @off. Copy the whole run with memcpy() instead of the
>+ * per-page increment loop in ttm_pool_allocated_page_commit(): for a
>+ * large defrag the borrowed pages dominate the new tt, so this turns
>+ * the populate fill from O(num_pages) individual stores into a handful
>+ * of bulk copies.
>+ */
>+ memcpy(alloc->pages, &old_tt->pages[off], nr * sizeof(*alloc->pages));
>+ alloc->pages += nr;
>+ alloc->remaining_pages -= nr;
>+
>+ if (alloc->dma_addr) {
>+ memcpy(alloc->dma_addr, &old_tt->dma_address[off],
>+ nr * sizeof(*alloc->dma_addr));
>+ alloc->dma_addr += nr;
>+ }
>+
>+ alloc->caching_divide = alloc->pages;
>+
>+ return 0;
>+}
>+
>+static int ttm_pool_harvest_remaining(struct ttm_pool *pool,
>+ struct ttm_tt *old_tt, pgoff_t off,
>+ pgoff_t *cursor,
>+ struct ttm_pool_alloc_state *alloc)
>+{
>+ /*
>+ * @off is always an old-tt chunk head here, never a tail page. This
>+ * holds because:
>+ * - Old chunks are self-aligned: the order walk in __ttm_pool_alloc()
>+ * is non-increasing and starts at an order the current offset is
>+ * aligned to, so an order-k chunk sits at a 2^k-aligned offset.
>+ * - Fresh defrag allocations are only made where the old chunk is
>+ * below the beneficial order, at an order >= the beneficial order
>+ * and aligned to it, so they cover whole old chunks and never stop
>+ * in the middle of one; old chunks at or above the beneficial order
>+ * are always harvested whole by ttm_pool_iter_reuse_old().
>+ * ttm_pool_defrag_old_order() verifies this against @cursor and fails
>+ * the move rather than ever borrowing a tail page.
>+ */
>+ while (alloc->remaining_pages) {
>+ int order = ttm_pool_defrag_old_order(pool, old_tt, off,
>+ cursor);
>+ pgoff_t nr;
>+ int r;
>+
>+ if (order < 0)
>+ return order;
>+ nr = 1UL << order;
>+
>+ r = ttm_pool_harvest_page(old_tt, order, off, alloc);
>+ if (r)
>+ return r;
>+
>+ off += nr;
>+ }
>+
>+ return 0;
>+}
>+
>+/**
>+ * enum ttm_pool_iter_action - Outcome of a per-order allocation phase
>+ * @TTM_POOL_ITER_FILL: A page (@it->p) was acquired; proceed to fill it in.
>+ * @TTM_POOL_ITER_RETRY: Loop state was adjusted (e.g. order lowered or a chunk
>+ * harvested); re-enter the loop body for the next order.
>+ * @TTM_POOL_ITER_STOP: The allocation is complete; stop the loop.
>+ *
>+ * Phase helpers return one of these (>= 0) on success, or a negative errno on
>+ * failure (the caller then unwinds via error_free_all).
>+ */
>+enum ttm_pool_iter_action {
>+ TTM_POOL_ITER_FILL,
>+ TTM_POOL_ITER_RETRY,
>+ TTM_POOL_ITER_STOP,
>+};
>+
> /*
> * struct ttm_pool_alloc_iter - Working state for the __ttm_pool_alloc() loop
> *
>@@ -821,6 +975,7 @@ struct ttm_pool_alloc_iter {
> struct ttm_tt *tt;
> const struct ttm_operation_ctx *ctx;
> struct ttm_pool_alloc_state *alloc;
>+ struct ttm_tt *defrag_old_tt;
> unsigned int beneficial_order;
> gfp_t gfp_flags;
>
>@@ -829,9 +984,52 @@ struct ttm_pool_alloc_iter {
> enum ttm_caching page_caching;
> bool allow_pools;
> bool fail_beneficial;
>+ unsigned int alloc_count;
>+ pgoff_t old_cursor;
> struct page *p;
> };
>
>+/*
>+ * Defrag move: reuse an already beneficial-order chunk from the old tt at this
>+ * offset rather than reallocating it. Harvest whatever order the old chunk has
>+ * (>= beneficial order), which may exceed the order the new tt would otherwise
>+ * pick. An order below beneficial_order is only reused for the unaligned tail
>+ * (where there is no benefit to reallocating). Only borrows the pages; the alloc
>+ * unwind path can drop them again.
>+ *
>+ * Returns TTM_POOL_ITER_RETRY when a chunk was harvested, TTM_POOL_ITER_FILL to
>+ * fall back to a fresh allocation, or a negative errno.
>+ */
>+static int ttm_pool_iter_reuse_old(struct ttm_pool_alloc_iter *it)
>+{
>+ int harvest_order;
>+ pgoff_t off;
>+ int r;
>+
>+ if (!it->defrag_old_tt || !it->beneficial_order)
>+ return TTM_POOL_ITER_FILL;
>+
>+ off = it->tt->num_pages - it->alloc->remaining_pages;
>+ harvest_order = ttm_pool_defrag_old_order(it->pool, it->defrag_old_tt,
>+ off, &it->old_cursor);
>+ if (harvest_order < 0)
>+ return harvest_order;
>+
>+ if (harvest_order < it->beneficial_order &&
>+ it->order >= it->beneficial_order)
>+ return TTM_POOL_ITER_FILL;
>+
>+ it->order = harvest_order;
>+ r = ttm_pool_harvest_page(it->defrag_old_tt, it->order, off, it->alloc);
>+ if (r)
>+ return r;
>+
>+ it->page_caching = it->tt->caching;
>+ it->allow_pools = true;
>+
>+ return TTM_POOL_ITER_RETRY;
>+}
>+
> /*
> * Acquire a single page for the current order, leaving it in @it->p (NULL on
> * failure). Tries a same-order pool page, then a fresh system allocation. Fault
>@@ -874,9 +1072,10 @@ static void ttm_pool_iter_acquire_page(struct ttm_pool_alloc_iter *it)
> /*
> * The current order could not be satisfied. Lower it and retry, recording a
> * sub-optimal backing (so the driver can later re-defrag) when we drop below
>- * the beneficial order.
>+ * the beneficial order. For a defrag move that has already made partial
>+ * progress, harvest the remaining old pages and stop instead.
> *
>- * Returns 0 to retry the (lowered) order, or a negative errno.
>+ * Returns TTM_POOL_ITER_RETRY, TTM_POOL_ITER_STOP, or a negative errno.
> */
> static int ttm_pool_iter_lower_order(struct ttm_pool_alloc_iter *it)
> {
>@@ -886,21 +1085,30 @@ static int ttm_pool_iter_lower_order(struct ttm_pool_alloc_iter *it)
> if (!it->order)
> return -ENOMEM;
>
>- /*
>- * Failing to allocate at the device's beneficial order means we are
>- * about to back this object with a sub-optimal (smaller order) set of
>- * pages. Record it so the driver can later try to defragment the object
>- * back to beneficial order.
>- */
> if (it->fail_beneficial || at_beneficial) {
> 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)
>+ if (it->alloc_count && it->defrag_old_tt) {
>+ pgoff_t off = it->tt->num_pages -
>+ it->alloc->remaining_pages;
>+ int r;
>+
>+ r = ttm_pool_harvest_remaining(it->pool,
>+ it->defrag_old_tt, off,
>+ &it->old_cursor,
>+ it->alloc);
>+ if (r)
>+ return r;
>+
>+ return TTM_POOL_ITER_STOP;
>+ } else if (it->defrag_old_tt) {
>+ /*
>+ * If we are defragmenting, we only want
>+ * beneficial_order, so any failure is treated as
>+ * -ENOMEM.
>+ */
> return -ENOMEM;
>+ }
> }
>
> if (it->fail_beneficial)
>@@ -910,7 +1118,7 @@ static int ttm_pool_iter_lower_order(struct ttm_pool_alloc_iter *it)
> it->page_caching = it->tt->caching;
> it->allow_pools = true;
>
>- return 0;
>+ return TTM_POOL_ITER_RETRY;
> }
>
> static int __ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
>@@ -923,6 +1131,7 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
> .tt = tt,
> .ctx = ctx,
> .alloc = alloc,
>+ .defrag_old_tt = restore ? NULL : ctx->defrag_old_tt,
> .beneficial_order = ttm_pool_beneficial_order(pool),
> .page_caching = tt->caching,
> .allow_pools = true,
>@@ -955,6 +1164,7 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
> * starts at an order @off is aligned to. @off is only non-zero when
> * resuming an interrupted restore, which would otherwise restart at
> * MAX_PAGE_ORDER and could place a large chunk at an unaligned offset.
>+ * Defrag relies on this to borrow whole old chunks.
> */
> if (off)
> start_order = min_t(unsigned int, start_order, __ffs(off));
>@@ -962,13 +1172,22 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
> for (it.order = ttm_pool_alloc_find_order(start_order, alloc);
> alloc->remaining_pages;
> it.order = ttm_pool_alloc_find_order(it.order, alloc)) {
>+ /* Reuse an already beneficial-order chunk from the old tt. */
>+ r = ttm_pool_iter_reuse_old(&it);
>+ if (r < 0)
>+ goto error_free_all;
>+ if (r == TTM_POOL_ITER_RETRY)
>+ continue;
>+
> /* Acquire a page (pool / system) for this order. */
> ttm_pool_iter_acquire_page(&it);
> if (!it.p) {
> r = ttm_pool_iter_lower_order(&it);
>- if (r)
>+ if (r < 0)
> goto error_free_all;
>- continue;
>+ if (r == TTM_POOL_ITER_RETRY)
>+ continue;
>+ break; /* TTM_POOL_ITER_STOP */
> }
>
> r = ttm_pool_page_allocated(pool, it.order, it.p,
>@@ -982,6 +1201,8 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
> if (r)
> goto error_free_all;
> }
>+
>+ it.alloc_count++;
> }
>
> r = ttm_pool_apply_caching(alloc);
>@@ -1000,6 +1221,12 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
> if (tt->restore)
> return r;
>
>+ /*
>+ * Drop any pages borrowed from the old tt during a defrag move: they are
>+ * still owned by the old tt and must not be freed here.
>+ */
>+ ttm_tt_defrag_disown_borrowed(tt, it.defrag_old_tt);
>+
> caching_divide = alloc->caching_divide - tt->pages;
> ttm_pool_free_range(pool, tt, tt->caching, 0, caching_divide);
> ttm_pool_free_range(pool, tt, ttm_cached, caching_divide,
>diff --git a/drivers/gpu/drm/ttm/ttm_tt.c b/drivers/gpu/drm/ttm/ttm_tt.c
>index b645a1818184..2388ff794a59 100644
>--- a/drivers/gpu/drm/ttm/ttm_tt.c
>+++ b/drivers/gpu/drm/ttm/ttm_tt.c
>@@ -450,6 +450,73 @@ void ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm)
> }
> EXPORT_SYMBOL_FOR_TESTS_ONLY(ttm_tt_unpopulate);
>
>+/**
>+ * ttm_tt_defrag_disown_borrowed() - Drop borrowed defrag pages from a tt
>+ * @tt: The tt to drop the borrowed page references from.
>+ * @src: The tt the pages may have been harvested from, or NULL.
>+ *
>+ * During a defragmentation move the freshly (re)allocated tt may harvest
>+ * already beneficial-order chunks from the BO's old tt, sharing the underlying
>+ * pages instead of reallocating them. Until the move commits these pages stay
>+ * owned by the old tt. Clearing a tt's slots for every page it shares with
>+ * @src (the same struct page at the same index) makes that tt's teardown leave
>+ * the shared pages alone, so they are freed exactly once by their owner.
>+ *
>+ * This is used both to unwind a failed defrag move (disown the borrowed pages
>+ * from the new tt, leaving the old tt intact) and to commit a successful one
>+ * (disown the now-transferred pages from the old tt, leaving them owned by the
>+ * new tt).
>+ */
>+void ttm_tt_defrag_disown_borrowed(struct ttm_tt *tt, const struct ttm_tt *src)
>+{
>+ pgoff_t i = 0, run_start = 0;
>+ bool in_run = false;
>+
>+ if (!tt || !src)
>+ return;
>+
>+ /*
>+ * Borrowing happens at chunk granularity: a borrowed chunk shares the
>+ * same struct page on both tts across its whole allocation order, while
>+ * a non-borrowed chunk differs across its whole order. Stride by @src's
>+ * chunk order instead of walking every page, coalescing the borrowed
>+ * (matching) chunks into runs and clearing each run with a single
>+ * memset rather than a per-page store. @src is a fully populated,
>+ * non-dma_alloc tt (the defrag path), so its page order is recorded in
>+ * page->private and read via ttm_pool_page_order_nodma().
>+ */
>+ while (i < tt->num_pages) {
>+ struct page *sp = src->pages[i];
>+ unsigned int order = sp ? ttm_pool_page_order_nodma(sp) : 0;
>+ pgoff_t nr = 1UL << order;
>+
>+ if (sp && tt->pages[i] == sp) {
>+ if (!in_run) {
>+ run_start = i;
>+ in_run = true;
>+ }
>+ } else if (in_run) {
>+ memset(&tt->pages[run_start], 0,
>+ (i - run_start) * sizeof(*tt->pages));
>+ if (tt->dma_address)
>+ memset(&tt->dma_address[run_start], 0,
>+ (i - run_start) *
>+ sizeof(*tt->dma_address));
>+ in_run = false;
>+ }
>+
>+ i += nr;
>+ }
>+
>+ if (in_run) {
>+ memset(&tt->pages[run_start], 0,
>+ (i - run_start) * sizeof(*tt->pages));
>+ if (tt->dma_address)
>+ memset(&tt->dma_address[run_start], 0,
>+ (i - run_start) * sizeof(*tt->dma_address));
>+ }
>+}
>+
> #ifdef CONFIG_DEBUG_FS
>
> /* Test the shrinker functions and dump the result */
>diff --git a/include/drm/ttm/ttm_bo.h b/include/drm/ttm/ttm_bo.h
>index c5205b8e8f10..22d12fea6f3c 100644
>--- a/include/drm/ttm/ttm_bo.h
>+++ b/include/drm/ttm/ttm_bo.h
>@@ -211,6 +211,18 @@ struct ttm_operation_ctx {
> * order.
> */
> bool defrag;
>+ /**
>+ * @defrag_old_tt: During a defragmentation move, the bo's existing
>+ * (populated) tt that is being relocated. The page allocator may
>+ * harvest already beneficial-order chunks from this tt to back the new
>+ * tt instead of reallocating them, avoiding alloc/free churn for large
>+ * buffers. Harvested pages are only borrowed: they stay owned by
>+ * @defrag_old_tt until the move commits, at which point
>+ * ttm_tt_defrag_disown_borrowed() clears the old tt's slots for the
>+ * transferred pages so they are not freed when the old tt is torn down.
>+ * NULL for non-defrag operations.
>+ */
>+ struct ttm_tt *defrag_old_tt;
> /**
> * @resv: Reservation object to be used together with
> * @allow_res_evict.
>diff --git a/include/drm/ttm/ttm_tt.h b/include/drm/ttm/ttm_tt.h
>index ce7533677d77..55c35bcf134d 100644
>--- a/include/drm/ttm/ttm_tt.h
>+++ b/include/drm/ttm/ttm_tt.h
>@@ -284,6 +284,8 @@ int ttm_tt_populate(struct ttm_device *bdev, struct ttm_tt *ttm,
> */
> void ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm);
>
>+void ttm_tt_defrag_disown_borrowed(struct ttm_tt *tt, const struct ttm_tt *src);
>+
> /**
> * ttm_tt_mark_for_clear - Mark pages for clearing on populate.
> *
>--
>2.34.1
>
next prev parent reply other threads:[~2026-12-03 23:11 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20261203202352.4153159-1-matthew.brost@intel.com>
2026-12-03 20:23 ` [PATCH v6 01/35] drm/ttm/pool: Allow backing off reclaim at the beneficial order Matthew Brost
2026-12-03 20:23 ` [PATCH v6 02/35] drm/ttm/pool: Add ttm_pool_page_order_nodma() helper Matthew Brost
2026-12-03 20:23 ` [PATCH v6 03/35] drm/ttm: Record sub-optimal page order allocations in ttm_tt Matthew Brost
2026-12-03 20:23 ` [PATCH v6 04/35] drm/ttm: Introduce ttm_pool_alloc_iter for __ttm_pool_alloc() Matthew Brost
2026-12-03 20:23 ` [PATCH v6 05/35] drm/ttm: Support defragmentation moves Matthew Brost
2026-12-03 20:23 ` [PATCH v6 06/35] drm/ttm: Add fault injection for beneficial-order allocation failures Matthew Brost
2026-12-03 20:23 ` [PATCH v6 07/35] drm/ttm/pool: Keep chunks self-aligned when resuming an interrupted restore Matthew Brost
2026-12-03 21:00 ` Niranjana Vishwanathapura
2026-12-03 20:23 ` [PATCH v6 08/35] drm/ttm: Harvest beneficial-order pages on defragmentation moves Matthew Brost
2026-12-03 23:11 ` Niranjana Vishwanathapura [this message]
2026-12-03 20:23 ` [PATCH v6 09/35] drm/ttm: Bound page (re)allocation per defragmentation move Matthew Brost
2026-12-03 20:23 ` [PATCH v6 10/35] drm/ttm: Preallocate beneficial-order defrag pages outside the lock Matthew Brost
2026-12-03 20:23 ` [PATCH v6 11/35] drm/ttm: Add full out-of-lock preallocation for ttm_pool_alloc() Matthew Brost
2026-12-03 20:23 ` [PATCH v6 14/35] drm/xe: Track BOs backed at a sub-optimal page order Matthew Brost
2026-12-03 20:23 ` [PATCH v6 15/35] drm/xe: Add xe_migrate_copy_defrag() for on-GPU defrag copies Matthew Brost
2026-12-03 20:23 ` [PATCH v6 16/35] drm/xe: Handle defrag moves in xe_bo_move() Matthew Brost
2026-12-03 20:23 ` [PATCH v6 17/35] drm/xe: Skip self-copies for borrowed pages on defrag moves Matthew Brost
2026-12-03 20:23 ` [PATCH v6 18/35] drm/xe: Add a page defragmentation worker Matthew Brost
2026-12-03 20:23 ` [PATCH v6 19/35] drm/xe: Add defrag GT stats Matthew Brost
2026-12-03 20:23 ` [PATCH v6 20/35] drm/xe: Add Kconfig.profile options for BO defrag configuration Matthew Brost
2026-12-03 20:23 ` [PATCH v6 21/35] drm/xe: Defrag using out-of-lock page preallocation Matthew Brost
2026-12-03 20:23 ` [PATCH v6 22/35] drm/xe: Add defrag profiling tracepoints Matthew Brost
2026-12-03 20:23 ` [PATCH v6 23/35] drm/xe: Preallocate system BO backing outside the dma-resv lock Matthew Brost
2026-12-03 20:23 ` [PATCH v6 24/35] drm/xe: Add tracepoint for xe_gem_create_ioctl Matthew Brost
2026-12-03 20:23 ` [PATCH v6 25/35] drm/xe: Add IOVA-based xe_res_cursor variant Matthew Brost
2026-12-03 20:23 ` [PATCH v6 28/35] drm/xe: Use IOVA-based DMA mapping for eligible tt BOs Matthew Brost
2026-12-03 20:23 ` [PATCH v6 29/35] drm/xe: Add per-device dependency scheduler for IOVA defrag finalize Matthew Brost
2026-12-03 20:23 ` [PATCH v6 30/35] drm/xe: Add packed copy-step IOVA mapping for defrag Matthew Brost
2026-12-03 20:23 ` [PATCH v6 31/35] drm/xe: Blit src-natural to dst-packed for defrag-IOVA copies Matthew Brost
2026-12-03 20:23 ` [PATCH v6 32/35] drm/xe: Finalize defrag-IOVA moves with post-copy job Matthew Brost
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=axH3Hex6J8RdK-Mp@nvishwa1-desk \
--to=niranjana.vishwanathapura@intel.com \
--cc=airlied@gmail.com \
--cc=arunpravin.paneerselvam@amd.com \
--cc=carlos.santa@intel.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=intel-xe@lists.freedesktop.org \
--cc=linux-kernel@vger.kernel.org \
--cc=maarten.lankhorst@linux.intel.com \
--cc=matthew.auld@intel.com \
--cc=matthew.brost@intel.com \
--cc=mripard@kernel.org \
--cc=nat@pixelcluster.dev \
--cc=ryanneph@google.com \
--cc=simona@ffwll.ch \
--cc=thomas.hellstrom@linux.intel.com \
--cc=tzimmermann@suse.de \
/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
all inboxes | Powered by JetHome®