[PATCH v6 07/35] drm/ttm/pool: Keep chunks self-aligned when resuming an interrupted restore

From: Matthew Brost

Date: Thu Dec 03 2026 - 15:25:02 EST


__ttm_pool_alloc() fills a tt with a non-increasing order walk starting
at MAX_PAGE_ORDER, so every order-k chunk lands at a 2^k-aligned offset
in the tt. ttm_pool_restore_and_alloc() however may be interrupted
(e.g. -EINTR from ttm_pool_restore_commit()) and later resume
__ttm_pool_alloc() at a non-zero offset. The walk then restarts at
MAX_PAGE_ORDER, bounded only by the number of remaining pages, and can
place a large chunk at an unaligned offset (e.g. an order-9 chunk at
offset 256 after an order-8 fallback).

That is harmless today, but upcoming defragmentation support relies on
chunks being self-aligned to borrow whole chunks from an old tt at the
same offset. Cap the starting order by the alignment of the offset being
resumed at, which keeps the invariant at negligible cost: at worst an
interrupted restore misses a single larger order.

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>
---
drivers/gpu/drm/ttm/ttm_pool.c | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
index 4e146798848a..5c7fec62aa30 100644
--- a/drivers/gpu/drm/ttm/ttm_pool.c
+++ b/drivers/gpu/drm/ttm/ttm_pool.c
@@ -927,6 +927,8 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
.page_caching = tt->caching,
.allow_pools = true,
};
+ pgoff_t off = tt->num_pages - alloc->remaining_pages;
+ unsigned int start_order = MAX_PAGE_ORDER;
gfp_t gfp_flags = GFP_USER;
pgoff_t caching_divide;
int r;
@@ -947,7 +949,17 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,

it.gfp_flags = gfp_flags;

- for (it.order = ttm_pool_alloc_find_order(MAX_PAGE_ORDER, alloc);
+ /*
+ * Keep chunks self-aligned (an order-k chunk at a 2^k-aligned offset):
+ * the order walk below is non-increasing, so that holds as long as it
+ * 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.
+ */
+ if (off)
+ start_order = min_t(unsigned int, start_order, __ffs(off));
+
+ for (it.order = ttm_pool_alloc_find_order(start_order, alloc);
alloc->remaining_pages;
it.order = ttm_pool_alloc_find_order(it.order, alloc)) {
/* Acquire a page (pool / system) for this order. */
--
2.34.1