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 09/33] drm/ttm: Preallocate beneficial-order defrag pages outside the lock
Date: Wed, 02 Dec 2026 00:23:07 +0000	[thread overview]
Message-ID: <sashiko-outbox-244495@kernel.org> (raw)
In-Reply-To: <20261201234915.3305331-10-matthew.brost@intel.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] drm/ttm: lost nr_suboptimal_pages in restore->snapshot_alloc
  on restore retry
- [Medium] drm/ttm: nr_suboptimal_pages counts unupgradable unaligned
  tail chunks
- [Medium] drm/ttm: ttm_pool_iter_harvest_rest() succeeds with zero
  allocated pages
- [Medium] drm/ttm: ttm_pool_prealloc_fill() and fini() bypass WC/UC
  page pools
--

commit 9332f88fafed52e6cb7db307cefb6601ea48bda1
Author: Matthew Brost <matthew.brost@intel.com>

drm/ttm: Preallocate beneficial-order defrag pages outside the lock

This commit adds out-of-lock preallocation of beneficial-order pages with
bulk CPU caching updates for TTM defragmentation moves, and tracks the
number of sub-optimal pages on each ttm_tt to size the preallocation.

> diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
> index 9e95e368ff829..7dcc79458b5ee 100644
> --- a/drivers/gpu/drm/ttm/ttm_pool.c
> +++ b/drivers/gpu/drm/ttm/ttm_pool.c

[ ... ]

> @@ -903,6 +909,9 @@ static int ttm_pool_harvest_page(struct ttm_tt *old_tt, unsigned int order,
>  
>  	alloc->caching_divide = alloc->pages;
>  
> +	if (order < beneficial_order)
> +		alloc->nr_suboptimal_pages += 0x1 << order;
> +
>  	return 0;
>  }

[Severity: Medium]
Does counting every chunk with order < beneficial_order here (and in
__ttm_pool_alloc() and ttm_pool_restore_and_alloc()) include the trailing
unaligned tail of a BO whose size is not a multiple of
1 << beneficial_order?

Chunks in the final partial region beyond
round_down(tt->num_pages, 1UL << beneficial_order) can never be upgraded to
beneficial_order because ttm_pool_alloc_find_order() caps the order to
__fls(alloc->remaining_pages).  Including those tail pages in
tt->nr_suboptimal_pages causes callers that round
ttm_tt_suboptimal_pages() up to beneficial-order chunks to preallocate,
remap caching on, and then tear down one extra beneficial-order chunk on
every defrag pass that __ttm_pool_alloc() can never consume.

[ ... ]

> @@ -961,6 +971,173 @@ static int ttm_pool_harvest_remaining(struct ttm_pool *pool,
>  	return 0;
>  }
>  

[ ... ]

> +	while (pp->count < count) {
> +		struct page *p = ttm_pool_alloc_page(pool, gfp, order, false);
> +
> +		if (!p)
> +			break;
> +
> +		pp->pages[pp->count++] = p;
> +		if (cpages)
> +			ttm_pool_prealloc_stage_caching(cpages, &ncpages, p,
> +							order);
> +	}
> +
> +	/*
> +	 * Apply the requested caching to every collected page in one shot. On
> +	 * failure the pages' caching is indeterminate, so drop the whole bag
> +	 * (freeing restores write-back) and let the consumer allocate in-line.
> +	 */
> +	r = ttm_pool_prealloc_apply_caching(tt_caching, cpages, ncpages);
> +	kvfree(cpages);
> +	if (r) {
> +		unsigned int i;
> +
> +		for (i = 0; i < pp->count; i++)
> +			ttm_pool_free_page(pool, tt_caching, order,
> +					   pp->pages[i], false);
> +		pp->count = 0;
> +	}
> +
> +	return 0;
> +}
> +EXPORT_SYMBOL(ttm_pool_prealloc_fill);
> +
> +/**
> + * ttm_pool_prealloc_fini() - Release unconsumed preallocated pages
> + * @pool: The pool the pages came from.
> + * @pp: Prealloc bag to drain. Consumed pages (< @used) are now owned by the tt.
> + */
> +void ttm_pool_prealloc_fini(struct ttm_pool *pool, struct ttm_pool_prealloc *pp)
> +{
> +	unsigned int i;
> +
> +	for (i = pp->used; i < pp->count; ++i)
> +		ttm_pool_free_page(pool, pp->caching, pp->order, pp->pages[i],
> +				   false);
> +	kvfree(pp->pages);
> +	pp->pages = NULL;
> +	pp->count = 0;
> +	pp->used = 0;
> +}
> +EXPORT_SYMBOL(ttm_pool_prealloc_fini);

[Severity: Medium]
Do ttm_pool_prealloc_fill() and ttm_pool_prealloc_fini() intentionally
bypass the TTM page pools for write-combined and uncached allocations?

Unlike ttm_pool_iter_acquire_page(), ttm_pool_prealloc_fill() allocates
fresh write-back pages from the buddy allocator via ttm_pool_alloc_page()
and runs set_pages_array_wc() or set_pages_array_uc() instead of first
taking already-cached beneficial-order pages from ttm_pool_select_type() /
ttm_pool_type_take().  In addition, when the prealloc bag is short or
exhausted, ttm_pool_iter_defrag_exhausted() stops the loop before
__ttm_pool_alloc() can take available beneficial-order pages from the pool.
On teardown, ttm_pool_prealloc_fini() frees unconsumed chunks through
ttm_pool_free_page(), which calls set_pages_wb() individually per chunk on
x86 rather than returning them to the pool via ttm_pool_type_give().

[ ... ]

> @@ -1006,16 +1184,20 @@ struct ttm_pool_alloc_iter {
>  };
>  
>  /*
> - * Defrag move budget exhausted: the upgrade can make no further progress this
> - * pass. Snapshot @defrag_capped is set only when a byte budget was in force at
> - * entry.
> + * Defrag move budget exhausted, or the out-of-lock prealloc bag ran dry: the
> + * upgrade can make no further progress this pass. Snapshot @defrag_capped is
> + * set only when a byte budget was in force at entry.
>   */
>  static bool ttm_pool_iter_defrag_exhausted(const struct ttm_pool_alloc_iter *it)
>  {
> +	const struct ttm_pool_prealloc *pp = it->prealloc;
> +
>  	if (!it->defrag_old_tt)
>  		return false;
> +	if (it->defrag_capped && it->alloc->defrag_bytes_remaining <= 0)
> +		return true;
>  
> -	return it->defrag_capped && it->alloc->defrag_bytes_remaining <= 0;
> +	return pp && pp->used >= pp->count;
>  }

[Severity: Medium]
If ctx->prealloc is passed with pp->count == 0 (or is exhausted before any
new chunk is allocated, so it.alloc_count == 0), does
ttm_pool_iter_defrag_exhausted() cause __ttm_pool_alloc() to succeed after
borrowing 100% of defrag_old_tt?

When ttm_pool_iter_defrag_exhausted() returns true on the first iteration,
__ttm_pool_alloc() calls ttm_pool_iter_harvest_rest() and returns 0 with
it.alloc_count == 0, unlike ttm_pool_iter_lower_order() which returns
-ENOMEM when it->alloc_count == 0.  Committing a zero-progress defrag move
still unmaps userspace mappings, runs driver move/rebind callbacks, and
re-flags TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED.  This also contradicts the
ttm_pool_prealloc_fill() and ctx->prealloc comments stating that the pool
falls back to in-line allocation when a fill falls short.

[ ... ]

> @@ -1262,6 +1459,8 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
>  			alloc->defrag_bytes_remaining -=
>  				(s64)(1UL << it.order) << PAGE_SHIFT;
>  
> +		if (it.order < it.beneficial_order)
> +			alloc->nr_suboptimal_pages += 0x1 << it.order;
>  		it.alloc_count++;
>  	}
>  
> @@ -1326,6 +1525,9 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
>  	/* Report the unused defrag budget back to the caller. */
>  	ctx->defrag_bytes_remaining = alloc.defrag_bytes_remaining;
>  
> +	if (!ret)
> +		tt->nr_suboptimal_pages = alloc.nr_suboptimal_pages;
> +
>  	return ret;
>  }
>  EXPORT_SYMBOL(ttm_pool_alloc);
> @@ -1378,6 +1580,15 @@ int ttm_pool_restore_and_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
>  
>  			if (ret)
>  				return ret;
> +
> +			/*
> +			 * __ttm_pool_alloc() counts each freshly (re)allocated
> +			 * chunk against nr_suboptimal_pages, but a chunk whose
> +			 * backup copy was interrupted and is finished here
> +			 * resumes past that loop, so account for it directly.
> +			 */
> +			if (restore->order < ttm_pool_beneficial_order(pool))
> +				alloc.nr_suboptimal_pages += 1U << restore->order;
>  		}
>  		if (!alloc.remaining_pages) {
>  			ret = ttm_pool_apply_caching(&alloc);

[Severity: Medium]
Does updating alloc.nr_suboptimal_pages after ttm_pool_restore_commit() in
__ttm_pool_alloc() and ttm_pool_restore_and_alloc() lose that count from
restore->snapshot_alloc if a later step in the restore fails?

ttm_pool_restore_commit() saves the post-commit state before returning:

drivers/gpu/drm/ttm/ttm_pool.c:ttm_pool_restore_commit() {
	...
	ttm_pool_allocated_page_commit(restore->alloced_page, restore->first_dma,
				       alloc, nr);
	if (restore->page_caching == alloc->tt_caching || PageHighMem(restore->alloced_page))
		alloc->caching_divide = alloc->pages;
	restore->snapshot_alloc = *alloc;
	restore->alloced_pages += nr;
	...
}

Because both callers increment alloc.nr_suboptimal_pages only after
ttm_pool_restore_commit() returns, restore->snapshot_alloc retains the old
nr_suboptimal_pages value.  If a subsequent chunk fails allocation (or is
interrupted on its first page in ttm_pool_restore_commit()), retrying
ttm_pool_restore_and_alloc() reloads alloc = restore->snapshot_alloc and
drops the committed chunk's suboptimal page count, which can leave
tt->nr_suboptimal_pages undercounted or 0 even when
TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED is set.

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

  reply	other threads:[~2026-12-02  0:23 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 [this message]
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
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-244495@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