[PATCH 02/17] drm/ttm: use gpu mm stats to track gpu memory allocations. (v2)

Shakeel Butt shakeel.butt at linux.dev
Wed Jul 2 16:08:23 UTC 2025


On Mon, Jun 30, 2025 at 02:49:21PM +1000, Dave Airlie wrote:
> From: Dave Airlie <airlied at redhat.com>
> 
> This uses the newly introduced per-node gpu tracking stats,
> to track GPU memory allocated via TTM and reclaimable memory in
> the TTM page pools.
> 
> These stats will be useful later for system information and
> later when mem cgroups are integrated.
> 
> Cc: Christian Koenig <christian.koenig at amd.com>
> Cc: Matthew Brost <matthew.brost at intel.com>
> Cc: Johannes Weiner <hannes at cmpxchg.org>
> Cc: linux-mm at kvack.org
> Cc: Andrew Morton <akpm at linux-foundation.org>
> Signed-off-by: Dave Airlie <airlied at redhat.com>
> 
> ---
> v2: add reclaim parameters and adjust the right counters.
> ---
>  drivers/gpu/drm/ttm/ttm_pool.c | 34 ++++++++++++++++++++++++++++------
>  1 file changed, 28 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
> index baf27c70a419..11a5777b4a85 100644
> --- a/drivers/gpu/drm/ttm/ttm_pool.c
> +++ b/drivers/gpu/drm/ttm/ttm_pool.c
> @@ -131,6 +131,16 @@ static struct list_head shrinker_list;
>  static struct shrinker *mm_shrinker;
>  static DECLARE_RWSEM(pool_shrink_rwsem);
>  
> +/* helper to get a current valid node id from a pool */
> +static int ttm_pool_nid(struct ttm_pool *pool) {
> +	int nid = NUMA_NO_NODE;
> +	if (pool)
> +		nid = pool->nid;
> +	if (nid == NUMA_NO_NODE)
> +		nid = numa_node_id();
> +	return nid;
> +}
> +
>  /* Allocate pages of size 1 << order with the given gfp_flags */
>  static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags,
>  					unsigned int order)
> @@ -150,8 +160,10 @@ static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags,
>  
>  	if (!pool->use_dma_alloc) {
>  		p = alloc_pages_node(pool->nid, gfp_flags, order);
> -		if (p)
> +		if (p) {
>  			p->private = order;
> +			mod_node_page_state(NODE_DATA(ttm_pool_nid(pool)), NR_GPU_ACTIVE, (1 << order));

Use mod_lruvec_page_state(), it will handle both cases of charged and
uncharged pages.



More information about the dri-devel mailing list