Skip to contentSkip to conversation

Patch "drm/ttm: Account for NULL and handle pages in ttm_pool_backup" has been added to the 6.18-stable tree

PATCH [email protected] 2 participants 2 messages Jul 29

Subsystems (3)
lore
Share & tools
lore

[email protected]

author Applied
#0 lore raw

This is a note to let you know that I've just added the patch titled

drm/ttm: Account for NULL and handle pages in ttm_pool_backup

to the 6.18-stable tree which can be found at:
http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:
drm-ttm-account-for-null-and-handle-pages-in-ttm_pool_backup.patch
and it can be found in the queue-6.18 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree, please let <[email protected]> know about it.

From 5b7b3b6595ee77d01c7463757baed114786094dd Mon Sep 17 00:00:00 2001
From: Matthew Brost <[email protected]>
Date: Thu, 2 Jul 2026 14:48:15 -0700
Subject: drm/ttm: Account for NULL and handle pages in ttm_pool_backup
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

From: Matthew Brost <[email protected]>

commit 5b7b3b6595ee77d01c7463757baed114786094dd upstream.

Pages in ttm_pool_backup can be NULL or backup handles (ttm_backup_page_ptr_is_handle()), neither of which can be passed to set_pages_array_wb() or freed. Add a dedicated WB pass before the dma/purge loop that walks allocations using the same i += num_pages stride, skipping NULL and handle entries, and calls set_pages_array_wb() once per contiguous run of real pages. Apply the same NULL/handle guard to the dma/purge loop.

Fixes the following oops:

Oops: general protection fault, kernel NULL pointer dereference 0x0: 0000 [#1] SMP NOPTI
RIP: 0010:__cpa_process_fault+0xf8/0x770
RSP: 0018:ffffc90000a87718 EFLAGS: 00010287
RAX: 0000000000000000 RBX: ffffc90000a87868 RCX: 0000000000000000
RDX: 0000000000001000 RSI: 0005088000000000 RDI: ffffffff827c5f34
RBP: 0005088000000000 R08: ffffc90000a877cb R09: ffffc90000a877d0
R10: 0000000000000000 R11: 000000000000001b R12: 000ffffffffff000
R13: ffffc90000a87868 R14: ffffc90000a87868 R15: ffff88815b882ae0
FS:  0000000000000000(0000) GS:ffff8884ec840000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007f930b844000 CR3: 000000000262e003 CR4: 0000000008f70ef0
PKRU: 55555554
Call Trace:
 <TASK>
 __change_page_attr_set_clr+0x989/0xe90
 ? __purge_vmap_area_lazy+0x6c/0x3a0
 ? _vm_unmap_aliases+0x250/0x2a0
 set_pages_array_wb+0x7f/0x120
 ttm_pool_backup+0x4c9/0x5b0 [ttm]
 ? dma_resv_wait_timeout+0x3b/0xf0
 ttm_tt_backup+0x32/0x60 [ttm]
 ttm_bo_shrink+0x66/0x110 [ttm]
 xe_bo_shrink_purge+0x12b/0x1b0 [xe]
 xe_bo_shrink+0xbb/0x270 [xe]
 __xe_shrinker_walk+0xf7/0x160 [xe]
 xe_shrinker_walk+0x9d/0xc0 [xe]
 xe_shrinker_scan+0x11f/0x210 [xe]
 do_shrink_slab+0x13b/0x270
 shrink_slab+0xf1/0x400
 shrink_node+0x352/0x8a0
 balance_pgdat+0x32c/0x700
 kswapd+0x205/0x2f0
 ? __pfx_autoremove_wake_function+0x10/0x10
 ? __pfx_kswapd+0x10/0x10
 kthread+0xd1/0x110
 ? __pfx_kthread+0x10/0x10
 ret_from_fork+0x1b1/0x200
 ? __pfx_kthread+0x10/0x10
 ret_from_fork_asm+0x1a/0x30
 </TASK>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Fixes b63d715b8090 ("drm/ttm/pool, drm/ttm/tt: Provide a helper to shrink pages")
Assisted-by GitHub_Copilot:claude-opus-4.8
---
 drivers/gpu/drm/ttm/ttm_pool.c |   34 ++++++++++++++++++++++++++++++----
 1 file changed, 30 insertions(+), 4 deletions(-)
drivers/gpu/drm/ttm/ttm_pool.c +30 -4
@@ -981,9 +981,31 @@ long ttm_pool_backup(struct ttm_pool *po
 		return -EBUSY;
 
 #ifdef CONFIG_X86
-	/* Anything returned to the system needs to be cached. */
-	if (tt->caching != ttm_cached)
-		set_pages_array_wb(tt->pages, tt->num_pages);
+	/* Anything returned to the system needs to be cached. Walk allocations
+	 * skipping NULL pages and issue set_pages_array_wb() per contiguous run.
+	 */
+	if (tt->caching != ttm_cached) {
+		pgoff_t run_start = 0, run_count = 0;
+
+		for (i = 0; i < tt->num_pages; i += num_pages) {
+			page = tt->pages[i];
+			if (unlikely(!page || ttm_backup_page_ptr_is_handle(page))) {
+				if (run_count) {
+					set_pages_array_wb(&tt->pages[run_start],
+							   run_count);
+					run_count = 0;
+				}
+				num_pages = 1;
+				continue;
+			}
+			num_pages = 1UL << ttm_pool_page_order(pool, page);
+			if (!run_count)
+				run_start = i;
+			run_count += num_pages;
+		}
+		if (run_count)
+			set_pages_array_wb(&tt->pages[run_start], run_count);
+	}
 #endif
 
 	if (tt->dma_address || flags->purge) {
@@ -991,7 +1013,7 @@ long ttm_pool_backup(struct ttm_pool *po
 			unsigned int order;
 
 			page = tt->pages[i];
-			if (unlikely(!page)) {
+			if (unlikely(!page || ttm_backup_page_ptr_is_handle(page))) {
 				num_pages = 1;
 				continue;
 			}
@@ -1034,6 +1056,10 @@ long ttm_pool_backup(struct ttm_pool *po
 		if (unlikely(!page))
 			continue;
 
+		/* Already-handled entry from a previous attempt. */
+		if (unlikely(ttm_backup_page_ptr_is_handle(page)))
+			continue;
+
 		ttm_pool_split_for_swap(pool, page);
 
 		shandle = ttm_backup_backup_page(backup, page, flags->writeback, i,

Patches currently in stable-queue which might be from [email protected] are

queue-6.18/drm-i915-gem-add-missing-nospec-on-parallel-submit-slot.patch queue-6.18/drm-ttm-account-for-null-and-handle-pages-in-ttm_pool_backup.patch queue-6.18/drm-xe-vf-shadow-buffer-management-for-ccs-read-writ.patch queue-6.18/drm-xe-return-error-on-non-migratable-faults-requiring-devmem.patch queue-6.18/drm-xe-vm-fix-svm-leak-on-resv-obj-alloc-failure-in-.patch queue-6.18/drm-xe-fix-pte-index-in-xe_vm_populate_pgtable-for-chunked-binds.patch queue-6.18/drm-xe-sa-shadow-buffer-support-in-the-sub-allocator.patch queue-6.18/drm-xe-vf-fix-vf-ccs-attach-detach-race-with-in-flig.patch

#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] Shmem backing pages (handles) are leaked during a purge operation.

Pre-existing issues:
- [High] If ttm_pool_backup fails partially, it results in a double unmap of DMA addresses and mismatched unmap sizes, violating DMA API constraints.
- [High] Backing up a TT via ttm_tt_backup frees its physical pages to the system but fails to decrement the TTM global ttm_pages_allocated counter, causing a permanent accounting leak. --

--- Patch [1]: Patch "drm/ttm: Account for NULL and handle pages in ttm_pool_backup" has been added to the 6.18-stable tree --- commit b4738a9fdee6c750a497cb7d46bafb43babc2b86
Author: [email protected] <[email protected]>
Subject: Patch "drm/ttm: Account for NULL and handle pages in ttm_pool_backup" has been added to the 6.18-stable tree

This commit updates ttm_pool_backup() to account for NULL pages and backup handles when shrinking pages. It adds a dedicated writeback pass that skips these entries and applies the same guard to the DMA unmap and purge loop.

Quoted 4 lines
drivers/gpu/drm/ttm/ttm_pool.c +0 -0
index baf27c70a4193..01083720e4e7f 100644

[ ... ]

drivers/gpu/drm/ttm/ttm_pool.c long ttm_pool_backup(struct ttm_pool *pool, struct ttm_tt *tt, · line 1019

Quoted 9 lines
drivers/gpu/drm/ttm/ttm_pool.c +1 -1
@@ -991,7 +1013,7 @@ long ttm_pool_backup(struct ttm_pool *pool, struct ttm_tt *tt,
 			unsigned int order;
 
 			page = tt->pages[i];
-			if (unlikely(!page)) {
+			if (unlikely(!page || ttm_backup_page_ptr_is_handle(page))) {
 				num_pages = 1;
 				continue;
 			}

[Severity: High]
If this operation is a purge and ttm_backup_page_ptr_is_handle(page) is true, does this skip the handle without freeing its underlying backup resources (shmem folios)?

When ttm_pool_backup() is called by the shrinker with flags->purge = true on a TT that has been partially or fully swapped to shmem, the new ttm_backup_page_ptr_is_handle(page) check unconditionally skips the handles.

It seems it fails to call ttm_backup_drop(backup, handle) to free the underlying shmem folio, leaving the handles stranded in tt->pages until the BO is eventually destroyed. Will this leak shmem folios?

drivers/gpu/drm/ttm/ttm_pool.c

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_pool.c +0 -0
 			order = ttm_pool_page_order(pool, page);
 			num_pages = 1UL << order;
 			if (tt->dma_address)
 				ttm_pool_unmap(pool, tt->dma_address[i],
 					       num_pages);

[Severity: High]
This is a pre-existing issue, but does this partial failure path lead to double DMA unmaps?

If ttm_pool_backup() fails partially (e.g., ttm_backup_backup_page() fails on the first page), the function aborts and returns an error. Because shrunken == 0, ttm_tt_backup() does not set TTM_TT_FLAG_BACKED_UP.

The loop here unmaps DMA addresses for all valid pages but does not clear tt->dma_address[i] or tt->pages[i] unless flags->purge is true. The pages remain in tt->pages with uncleared DMA addresses.

When the TT is subsequently destroyed via ttm_tt_unpopulate(), it calls ttm_pool_free_range(), which again calls ttm_pool_unmap() on the already unmapped DMA addresses:

drivers/gpu/drm/ttm/ttm_pool.c:ttm_pool_unmap_and_free() {
    ...
    if (dma_addr)
        ttm_pool_unmap(pool, *dma_addr, nr);
    ...
}

Additionally, because the first page's order was cleared via ttm_pool_split_for_swap(), will it be unmapped as 1 page instead of its original mapped size, causing a mismatched unmap size?

[Severity: High]
This is a pre-existing issue, but does backing up a TT via ttm_tt_backup() leak the TTM global ttm_pages_allocated counter?

When ttm_tt_backup() successfully backs up pages, it clears TTM_TT_FLAG_PRIV_POPULATED and sets TTM_TT_FLAG_BACKED_UP without decrementing the ttm_pages_allocated counter:

drivers/gpu/drm/ttm/ttm_tt.c:ttm_tt_backup() {
    ...
    tt->page_flags &= ~TTM_TT_FLAG_PRIV_POPULATED;
    tt->page_flags |= TTM_TT_FLAG_BACKED_UP;
    ...
}

Later, when the TT is destroyed, ttm_tt_unpopulate() checks ttm_tt_is_populated():

drivers/gpu/drm/ttm/ttm_tt.c:ttm_tt_unpopulate() {
    ...
    if (!ttm_tt_is_populated(ttm))
        return;
    ...
}

Because the PRIV_POPULATED flag is clear, this function returns early and fails to execute the atomic_long_sub(tt->num_pages, &ttm_pages_allocated) decrement at the end of the function. The backup cleanup path (ttm_pool_drop_backed_up()) also never decrements this counter. Does this cause a permanent memory accounting leak?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/2026072924-debate-parched-01e5@gregkh?part=1