dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] drm/ttm: Fix GPU MM stats during pool shrinking
@ 2026-05-02  6:53 Matthew Brost
  2026-05-02  8:14 ` Kenneth Crudup
  0 siblings, 1 reply; 4+ messages in thread
From: Matthew Brost @ 2026-05-02  6:53 UTC (permalink / raw)
  To: intel-xe, dri-devel
  Cc: Kenneth Crudup, Christian Koenig, Huang Rui, Matthew Auld,
	David Airlie

TTM pool shrinking frees pages by calling __free_pages() directly,
which bypasses updates to NR_GPU_ACTIVE and leaves GPU MM accounting
out of sync.

Introduce a helper, __free_pages_gpu_account(), and use it for all page
frees in ttm_pool.c so GPU MM statistics are updated consistently.

Reported-by: Kenneth Crudup <kenny@panix.com>
Fixes: ae80122f3896 ("drm/ttm: use gpu mm stats to track gpu memory allocations. (v4)")
Cc: Christian Koenig <christian.koenig@amd.com>
Cc: Huang Rui <ray.huang@amd.com>
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: David Airlie <airlied@gmail.com>
Cc: dri-devel@lists.freedesktop.org
Signed-off-by: Matthew Brost <matthew.brost@intel.com>

---

v2:
 - Replace put_page() with __free_pages_gpu_account() (Kenneth)
---
 drivers/gpu/drm/ttm/ttm_pool.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
index 26a3689e5fd9..278bbe7a11ad 100644
--- a/drivers/gpu/drm/ttm/ttm_pool.c
+++ b/drivers/gpu/drm/ttm/ttm_pool.c
@@ -206,6 +206,14 @@ static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags,
 	return NULL;
 }
 
+static void __free_pages_gpu_account(struct page *p, unsigned int order,
+				     bool reclaim)
+{
+	mod_lruvec_page_state(p, reclaim ? NR_GPU_RECLAIM : NR_GPU_ACTIVE,
+			      -(1 << order));
+	__free_pages(p, order);
+}
+
 /* Reset the caching and pages of size 1 << order */
 static void ttm_pool_free_page(struct ttm_pool *pool, enum ttm_caching caching,
 			       unsigned int order, struct page *p, bool reclaim)
@@ -223,9 +231,7 @@ static void ttm_pool_free_page(struct ttm_pool *pool, enum ttm_caching caching,
 #endif
 
 	if (!pool || !ttm_pool_uses_dma_alloc(pool)) {
-		mod_lruvec_page_state(p, reclaim ? NR_GPU_RECLAIM : NR_GPU_ACTIVE,
-				      -(1 << order));
-		__free_pages(p, order);
+		__free_pages_gpu_account(p, order, reclaim);
 		return;
 	}
 
@@ -606,7 +612,7 @@ static int ttm_pool_restore_commit(struct ttm_pool_tt_restore *restore,
 			 */
 			ttm_pool_split_for_swap(restore->pool, p);
 			copy_highpage(restore->alloced_page + i, p);
-			__free_pages(p, 0);
+			__free_pages_gpu_account(p, 0, false);
 		}
 
 		restore->restored_pages++;
@@ -1068,7 +1074,7 @@ long ttm_pool_backup(struct ttm_pool *pool, struct ttm_tt *tt,
 			if (flags->purge) {
 				shrunken += num_pages;
 				page->private = 0;
-				__free_pages(page, order);
+				__free_pages_gpu_account(page, order, false);
 				memset(tt->pages + i, 0,
 				       num_pages * sizeof(*tt->pages));
 			}
@@ -1109,7 +1115,7 @@ long ttm_pool_backup(struct ttm_pool *pool, struct ttm_tt *tt,
 		}
 		handle = shandle;
 		tt->pages[i] = ttm_backup_handle_to_page_ptr(handle);
-		put_page(page);
+		__free_pages_gpu_account(page, 0, false);
 		shrunken++;
 	}
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] drm/ttm: Fix GPU MM stats during pool shrinking
  2026-05-02  6:53 [PATCH v2] drm/ttm: Fix GPU MM stats during pool shrinking Matthew Brost
@ 2026-05-02  8:14 ` Kenneth Crudup
  2026-05-04  3:56   ` Dave Airlie
  0 siblings, 1 reply; 4+ messages in thread
From: Kenneth Crudup @ 2026-05-02  8:14 UTC (permalink / raw)
  To: Matthew Brost, intel-xe, dri-devel
  Cc: Christian Koenig, Huang Rui, Matthew Auld, David Airlie



On 5/1/26 23:53, Matthew Brost wrote:
> TTM pool shrinking frees pages by calling __free_pages() directly,
> which bypasses updates to NR_GPU_ACTIVE and leaves GPU MM accounting
> out of sync.
> 
> Introduce a helper, __free_pages_gpu_account(), and use it for all page
> frees in ttm_pool.c so GPU MM statistics are updated consistently.
> 
> Reported-by: Kenneth Crudup <kenny@panix.com>
> Fixes: ae80122f3896 ("drm/ttm: use gpu mm stats to track gpu memory allocations. (v4)")
> Cc: Christian Koenig <christian.koenig@amd.com>
> Cc: Huang Rui <ray.huang@amd.com>
> Cc: Matthew Auld <matthew.auld@intel.com>
> Cc: David Airlie <airlied@gmail.com>
> Cc: dri-devel@lists.freedesktop.org
> Signed-off-by: Matthew Brost <matthew.brost@intel.com>
> 
> ---
> 
> v2:

Tested-By: Kenneth R. Crudup <kenny@panix.com>

I think this one got it right ... this is after two bonnie++ runs:

----
SwapFree:       33554428 kB
MemTotal:       32345768 kB
GPUActive:        676092 kB
GPUReclaim:       584272 kB

SwapFree:       33554428 kB
MemTotal:       32345768 kB
GPUActive:        750396 kB
GPUReclaim:       516348 kB

SwapFree:       33554428 kB
MemTotal:       32345768 kB
GPUActive:        801996 kB
GPUReclaim:       478756 kB

SwapFree:       33554428 kB
MemTotal:       32345768 kB
GPUActive:        794224 kB
GPUReclaim:       481624 kB

SwapFree:       33554428 kB
MemTotal:       32345768 kB
GPUActive:        351336 kB
GPUReclaim:          400 kB

SwapFree:       33554428 kB
MemTotal:       32345768 kB
GPUActive:       1015624 kB
GPUReclaim:           72 kB

SwapFree:       33554428 kB
MemTotal:       32345768 kB
GPUActive:        583176 kB
GPUReclaim:           48 kB

SwapFree:       33554428 kB
MemTotal:       32345768 kB
GPUActive:        737852 kB
GPUReclaim:         2524 kB

SwapFree:       33554428 kB
MemTotal:       32345768 kB
GPUActive:       1041056 kB
GPUReclaim:          308 kB

SwapFree:       33554428 kB
MemTotal:       32345768 kB
GPUActive:       1356568 kB
GPUReclaim:       109992 kB

SwapFree:       33554428 kB
MemTotal:       32345768 kB
GPUActive:       1181732 kB
GPUReclaim:        39000 kB

SwapFree:       33554428 kB
MemTotal:       32345768 kB
GPUActive:        989900 kB
GPUReclaim:           68 kB

SwapFree:       33554428 kB
MemTotal:       32345768 kB
GPUActive:        486720 kB
GPUReclaim:          544 kB

SwapFree:       33554428 kB
MemTotal:       32345768 kB
GPUActive:        687628 kB
GPUReclaim:         2280 kB

SwapFree:       33554428 kB
MemTotal:       32345768 kB
GPUActive:        634060 kB
GPUReclaim:         1000 kB

SwapFree:       33554428 kB
MemTotal:       32345768 kB
GPUActive:       1030168 kB
GPUReclaim:         1696 kB

SwapFree:       33554428 kB
MemTotal:       32345768 kB
GPUActive:       1323464 kB
GPUReclaim:         1392 kB

SwapFree:       33554428 kB
MemTotal:       32345768 kB
GPUActive:       1452340 kB
GPUReclaim:        20024 kB
----



-Kenny "yeah, I don't sleep much either" Crudup

-- 
Kenneth R. Crudup / Sr. SW Engineer, Scott County Consulting, Orange 
County CA


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] drm/ttm: Fix GPU MM stats during pool shrinking
  2026-05-02  8:14 ` Kenneth Crudup
@ 2026-05-04  3:56   ` Dave Airlie
  2026-05-04  4:09     ` Matthew Brost
  0 siblings, 1 reply; 4+ messages in thread
From: Dave Airlie @ 2026-05-04  3:56 UTC (permalink / raw)
  To: Kenneth Crudup
  Cc: Matthew Brost, intel-xe, dri-devel, Christian Koenig, Huang Rui,
	Matthew Auld

On Sat, 2 May 2026 at 18:14, Kenneth Crudup <kenny@panix.com> wrote:
>
>
>
> On 5/1/26 23:53, Matthew Brost wrote:
> > TTM pool shrinking frees pages by calling __free_pages() directly,
> > which bypasses updates to NR_GPU_ACTIVE and leaves GPU MM accounting
> > out of sync.
> >
> > Introduce a helper, __free_pages_gpu_account(), and use it for all page
> > frees in ttm_pool.c so GPU MM statistics are updated consistently.
> >
> > Reported-by: Kenneth Crudup <kenny@panix.com>
> > Fixes: ae80122f3896 ("drm/ttm: use gpu mm stats to track gpu memory allocations. (v4)")
> > Cc: Christian Koenig <christian.koenig@amd.com>
> > Cc: Huang Rui <ray.huang@amd.com>
> > Cc: Matthew Auld <matthew.auld@intel.com>
> > Cc: David Airlie <airlied@gmail.com>
> > Cc: dri-devel@lists.freedesktop.org
> > Signed-off-by: Matthew Brost <matthew.brost@intel.com>
> >
> > ---
> >
> > v2:

Oops, good catch,

Reviewed-by: Dave Airlie <airlied@redhat.com>

Dave.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] drm/ttm: Fix GPU MM stats during pool shrinking
  2026-05-04  3:56   ` Dave Airlie
@ 2026-05-04  4:09     ` Matthew Brost
  0 siblings, 0 replies; 4+ messages in thread
From: Matthew Brost @ 2026-05-04  4:09 UTC (permalink / raw)
  To: Dave Airlie
  Cc: Kenneth Crudup, intel-xe, dri-devel, Christian Koenig, Huang Rui,
	Matthew Auld

On Mon, May 04, 2026 at 01:56:25PM +1000, Dave Airlie wrote:
> On Sat, 2 May 2026 at 18:14, Kenneth Crudup <kenny@panix.com> wrote:
> >
> >
> >
> > On 5/1/26 23:53, Matthew Brost wrote:
> > > TTM pool shrinking frees pages by calling __free_pages() directly,
> > > which bypasses updates to NR_GPU_ACTIVE and leaves GPU MM accounting
> > > out of sync.
> > >
> > > Introduce a helper, __free_pages_gpu_account(), and use it for all page
> > > frees in ttm_pool.c so GPU MM statistics are updated consistently.
> > >
> > > Reported-by: Kenneth Crudup <kenny@panix.com>
> > > Fixes: ae80122f3896 ("drm/ttm: use gpu mm stats to track gpu memory allocations. (v4)")
> > > Cc: Christian Koenig <christian.koenig@amd.com>
> > > Cc: Huang Rui <ray.huang@amd.com>
> > > Cc: Matthew Auld <matthew.auld@intel.com>
> > > Cc: David Airlie <airlied@gmail.com>
> > > Cc: dri-devel@lists.freedesktop.org
> > > Signed-off-by: Matthew Brost <matthew.brost@intel.com>
> > >
> > > ---
> > >
> > > v2:
> 
> Oops, good catch,
> 
> Reviewed-by: Dave Airlie <airlied@redhat.com>

Thanks! Merged to drm-misc-fixes.

Matt

> 
> Dave.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-05-05  7:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-02  6:53 [PATCH v2] drm/ttm: Fix GPU MM stats during pool shrinking Matthew Brost
2026-05-02  8:14 ` Kenneth Crudup
2026-05-04  3:56   ` Dave Airlie
2026-05-04  4:09     ` Matthew Brost

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox