From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B6499C43458 for ; Mon, 6 Jul 2026 05:24:37 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 73E8410E2F7; Mon, 6 Jul 2026 05:24:37 +0000 (UTC) Received: from us-smtp-delivery-44.mimecast.com (us-smtp-delivery-44.mimecast.com [205.139.111.44]) by gabe.freedesktop.org (Postfix) with ESMTPS id 7AD5610E2F7 for ; Mon, 6 Jul 2026 05:24:36 +0000 (UTC) Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-106-j1Q8F2wJM5erWIPkDQhRew-1; Mon, 06 Jul 2026 01:24:29 -0400 X-MC-Unique: j1Q8F2wJM5erWIPkDQhRew-1 X-Mimecast-MFC-AGG-ID: j1Q8F2wJM5erWIPkDQhRew_1783315467 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E3C8618052C7; Mon, 6 Jul 2026 05:24:26 +0000 (UTC) Received: from dreadlord.redhat.com (unknown [10.67.32.13]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6CD4E1956096; Mon, 6 Jul 2026 05:24:18 +0000 (UTC) From: Dave Airlie To: dri-devel@lists.freedesktop.org, tj@kernel.org, christian.koenig@amd.com, Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song Cc: cgroups@vger.kernel.org, Thomas Hellstrom , Waiman Long , simona@ffwll.ch, intel-xe@lists.freedesktop.org Subject: [PATCH 05/10] ttm/pool: enable memcg tracking and shrinker. (v3) Date: Mon, 6 Jul 2026 15:22:34 +1000 Message-ID: <20260706052330.1110909-6-airlied@gmail.com> In-Reply-To: <20260706052330.1110909-1-airlied@gmail.com> References: <20260706052330.1110909-1-airlied@gmail.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: YOrJOWkhvC6S2ZUt2Ue5EXnxNvH4NjUI_UrSs4foYSQ_1783315467 X-Mimecast-Originator: gmail.com Content-Transfer-Encoding: quoted-printable content-type: text/plain; charset=WINDOWS-1252; x-default=true X-BeenThere: intel-xe@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel Xe graphics driver List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-xe-bounces@lists.freedesktop.org Sender: "Intel-xe" From: Dave Airlie This enables all the backend code to use the list lru in memcg mode, and set the shrinker to be memcg aware. It adds the loop case for when pooled pages end up being reparented to a higher memcg group, that newer memcg can search for them there and take them back. Signed-off-by: Dave Airlie --- v2: just use the proper stats. v3: fix objcg check to return void --- drivers/gpu/drm/ttm/ttm_pool.c | 124 +++++++++++++++++++++++++++------ mm/list_lru.c | 1 + 2 files changed, 102 insertions(+), 23 deletions(-) diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.= c index f12b68812081..01b6ff2d8144 100644 --- a/drivers/gpu/drm/ttm/ttm_pool.c +++ b/drivers/gpu/drm/ttm/ttm_pool.c @@ -144,7 +144,9 @@ static int ttm_pool_nid(struct ttm_pool *pool) } =20 /* 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_f= lags, +static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, +=09=09=09=09=09struct obj_cgroup *objcg, +=09=09=09=09=09gfp_t gfp_flags, =09=09=09=09=09unsigned int order) { =09const unsigned int beneficial_order =3D ttm_pool_beneficial_order(pool)= ; @@ -172,7 +174,10 @@ static struct page *ttm_pool_alloc_page(struct ttm_poo= l *pool, gfp_t gfp_flags, =09=09p =3D alloc_pages_node(pool->nid, gfp_flags, order); =09=09if (p) { =09=09=09p->private =3D order; -=09=09=09mod_lruvec_page_state(p, NR_GPU_ACTIVE, 1 << order); +=09=09=09if (!mem_cgroup_charge_gpu_page(objcg, p, order, gfp_flags, false= )) { +=09=09=09=09__free_pages(p, order); +=09=09=09=09return NULL; +=09=09=09} =09=09} =09=09return p; =09} @@ -209,8 +214,7 @@ static struct page *ttm_pool_alloc_page(struct ttm_pool= *pool, gfp_t gfp_flags, static void __free_pages_gpu_account(struct page *p, unsigned int order, =09=09=09=09 bool reclaim) { -=09mod_lruvec_page_state(p, reclaim ? NR_GPU_RECLAIM : NR_GPU_ACTIVE, -=09=09=09 -(1 << order)); +=09mem_cgroup_uncharge_gpu_page(p, order, reclaim); =09__free_pages(p, order); } =20 @@ -317,12 +321,11 @@ static void ttm_pool_type_give(struct ttm_pool_type *= pt, struct page *p) =20 =09INIT_LIST_HEAD(&p->lru); =09rcu_read_lock(); -=09list_lru_add(&pt->pages, &p->lru, nid, NULL); +=09list_lru_add(&pt->pages, &p->lru, nid, page_memcg_check(p)); =09rcu_read_unlock(); =20 =09atomic_long_add(num_pages, &allocated_pages[nid]); -=09mod_lruvec_page_state(p, NR_GPU_ACTIVE, -num_pages); -=09mod_lruvec_page_state(p, NR_GPU_RECLAIM, num_pages); +=09mem_cgroup_move_gpu_page_reclaim(NULL, p, pt->order, true); } =20 static enum lru_status take_one_from_lru(struct list_head *item, @@ -337,20 +340,56 @@ static enum lru_status take_one_from_lru(struct list_= head *item, =09return LRU_REMOVED; } =20 -/* Take pages from a specific pool_type, return NULL when nothing availabl= e */ -static struct page *ttm_pool_type_take(struct ttm_pool_type *pt, int nid) +static int pool_lru_get_page(struct ttm_pool_type *pt, int nid, +=09=09=09 struct page **page_out, +=09=09=09 struct obj_cgroup *objcg, +=09=09=09 struct mem_cgroup *memcg) { =09int ret; =09struct page *p =3D NULL; =09unsigned long nr_to_walk =3D 1; +=09unsigned int num_pages =3D 1 << pt->order; =20 -=09ret =3D list_lru_walk_node(&pt->pages, nid, take_one_from_lru, (void *)= &p, &nr_to_walk); +=09ret =3D list_lru_walk_one(&pt->pages, nid, memcg, take_one_from_lru, (v= oid *)&p, &nr_to_walk); =09if (ret =3D=3D 1 && p) { -=09=09atomic_long_sub(1 << pt->order, &allocated_pages[nid]); -=09=09mod_lruvec_page_state(p, NR_GPU_ACTIVE, (1 << pt->order)); -=09=09mod_lruvec_page_state(p, NR_GPU_RECLAIM, -(1 << pt->order)); +=09=09atomic_long_sub(num_pages, &allocated_pages[nid]); + +=09=09if (!mem_cgroup_move_gpu_page_reclaim(objcg, p, pt->order, false)) { +=09=09=09__free_pages(p, pt->order); +=09=09=09p =3D NULL; +=09=09} =09} -=09return p; +=09*page_out =3D p; +=09return ret; +} + +/* Take pages from a specific pool_type, return NULL when nothing availabl= e */ +static struct page *ttm_pool_type_take(struct ttm_pool_type *pt, int nid, +=09=09=09=09 struct obj_cgroup *orig_objcg) +{ +=09struct page *page_out =3D NULL; +=09int ret; +=09struct mem_cgroup *orig_memcg =3D orig_objcg ? get_mem_cgroup_from_objc= g(orig_objcg) : NULL; +=09struct mem_cgroup *memcg =3D orig_memcg; + +=09/* +=09 * Attempt to get a page from the current memcg, but if it hasn't got a= ny in it's level, +=09 * go up to the parent and check there. This helps the scenario where m= ultiple apps get +=09 * started into their own cgroup from a common parent and want to reuse= the pools. +=09 */ +=09while (!page_out) { +=09=09ret =3D pool_lru_get_page(pt, nid, &page_out, orig_objcg, memcg); +=09=09if (ret =3D=3D 1) +=09=09=09break; +=09=09if (!memcg) +=09=09=09break; +=09=09memcg =3D parent_mem_cgroup(memcg); +=09=09if (!memcg) +=09=09=09break; +=09} + +=09mem_cgroup_put(orig_memcg); +=09return page_out; } =20 /* Initialize and add a pool type to the global shrinker list */ @@ -360,7 +399,7 @@ static void ttm_pool_type_init(struct ttm_pool_type *pt= , struct ttm_pool *pool, =09pt->pool =3D pool; =09pt->caching =3D caching; =09pt->order =3D order; -=09list_lru_init(&pt->pages); +=09list_lru_init_memcg(&pt->pages, mm_shrinker); =20 =09spin_lock(&shrinker_lock); =09list_add_tail(&pt->shrinker_list, &shrinker_list); @@ -403,6 +442,31 @@ static void ttm_pool_type_fini(struct ttm_pool_type *p= t) =09ttm_pool_dispose_list(pt, &dispose); } =20 +/* + * This function doesn't currently check dma32, because no driver using th= is + * support dma32. This should be added and debugged when that changes. + */ +static void ttm_pool_check_objcg(struct obj_cgroup *objcg) +{ +#ifdef CONFIG_MEMCG +=09int r =3D 0; +=09struct mem_cgroup *memcg; +=09if (!objcg) +=09=09return; + +=09memcg =3D get_mem_cgroup_from_objcg(objcg); +=09for (unsigned i =3D 0; i < NR_PAGE_ORDERS; i++) { +=09=09r =3D memcg_list_lru_alloc(memcg, &global_write_combined[i].pages, G= FP_KERNEL); +=09=09if (r) +=09=09=09break; +=09=09r =3D memcg_list_lru_alloc(memcg, &global_uncached[i].pages, GFP_KER= NEL); +=09=09if (r) +=09=09=09break; +=09} +=09mem_cgroup_put(memcg); +#endif +} + /* Return the pool_type to use for the given caching and order */ static struct ttm_pool_type *ttm_pool_select_type(struct ttm_pool *pool, =09=09=09=09=09=09 enum ttm_caching caching, @@ -432,7 +496,9 @@ static struct ttm_pool_type *ttm_pool_select_type(struc= t ttm_pool *pool, } =20 /* Free pages using the per-node shrinker list */ -static unsigned int ttm_pool_shrink(int nid, unsigned long num_to_free) +static unsigned int ttm_pool_shrink(int nid, +=09=09=09=09 struct mem_cgroup *memcg, +=09=09=09=09 unsigned long num_to_free) { =09LIST_HEAD(dispose); =09struct ttm_pool_type *pt; @@ -448,7 +514,11 @@ static unsigned int ttm_pool_shrink(int nid, unsigned = long num_to_free) =09if (!pt) =09=09return 0; =20 -=09num_pages =3D list_lru_walk_node(&pt->pages, nid, pool_move_to_dispose_= list, &dispose, &num_to_free); +=09if (!memcg) { +=09=09num_pages =3D list_lru_walk_node(&pt->pages, nid, pool_move_to_dispo= se_list, &dispose, &num_to_free); +=09} else { +=09=09num_pages =3D list_lru_walk_one(&pt->pages, nid, memcg, pool_move_to= _dispose_list, &dispose, &num_to_free); +=09} =09num_pages *=3D 1 << pt->order; =20 =09ttm_pool_dispose_list(pt, &dispose); @@ -777,6 +847,7 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, stru= ct ttm_tt *tt, =09bool allow_pools; =09struct page *p; =09int r; +=09struct obj_cgroup *objcg =3D memcg_account ? tt->objcg : NULL; =20 =09WARN_ON(!alloc->remaining_pages || ttm_tt_is_populated(tt)); =09WARN_ON(alloc->dma_addr && !pool->dev); @@ -794,6 +865,9 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, stru= ct ttm_tt *tt, =20 =09page_caching =3D tt->caching; =09allow_pools =3D true; + +=09ttm_pool_check_objcg(objcg); + =09for (order =3D ttm_pool_alloc_find_order(MAX_PAGE_ORDER, alloc); =09 alloc->remaining_pages; =09 order =3D ttm_pool_alloc_find_order(order, alloc)) { @@ -803,7 +877,7 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, stru= ct ttm_tt *tt, =09=09p =3D NULL; =09=09pt =3D ttm_pool_select_type(pool, page_caching, order); =09=09if (pt && allow_pools) -=09=09=09p =3D ttm_pool_type_take(pt, ttm_pool_nid(pool)); +=09=09=09p =3D ttm_pool_type_take(pt, ttm_pool_nid(pool), objcg); =20 =09=09/* =09=09 * If that fails or previously failed, allocate from system. @@ -814,7 +888,7 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, stru= ct ttm_tt *tt, =09=09if (!p) { =09=09=09page_caching =3D ttm_cached; =09=09=09allow_pools =3D false; -=09=09=09p =3D ttm_pool_alloc_page(pool, gfp_flags, order); +=09=09=09p =3D ttm_pool_alloc_page(pool, objcg, gfp_flags, order); =09=09} =09=09/* If that fails, lower the order if possible and retry. */ =09=09if (!p) { @@ -960,7 +1034,7 @@ void ttm_pool_free(struct ttm_pool *pool, struct ttm_t= t *tt) =20 =09while (atomic_long_read(&allocated_pages[nid]) > pool_node_limit[nid]) = { =09=09unsigned long diff =3D atomic_long_read(&allocated_pages[nid]) - poo= l_node_limit[nid]; -=09=09ttm_pool_shrink(nid, diff); +=09=09ttm_pool_shrink(nid, NULL, diff); =09} } EXPORT_SYMBOL(ttm_pool_free); @@ -1214,10 +1288,14 @@ static unsigned long ttm_pool_shrinker_scan(struct = shrinker *shrink, =09=09=09=09=09 struct shrink_control *sc) { =09unsigned long num_freed =3D 0; +=09int num_pools; +=09spin_lock(&shrinker_lock); +=09num_pools =3D list_count_nodes(&shrinker_list); +=09spin_unlock(&shrinker_lock); =20 =09do -=09=09num_freed +=3D ttm_pool_shrink(sc->nid, sc->nr_to_scan); -=09while (num_freed < sc->nr_to_scan && +=09=09num_freed +=3D ttm_pool_shrink(sc->nid, sc->memcg, sc->nr_to_scan); +=09while (num_pools-- >=3D 0 && num_freed < sc->nr_to_scan && =09 atomic_long_read(&allocated_pages[sc->nid])); =20 =09sc->nr_scanned =3D num_freed; @@ -1406,7 +1484,7 @@ int ttm_pool_mgr_init(unsigned long num_pages) =09spin_lock_init(&shrinker_lock); =09INIT_LIST_HEAD(&shrinker_list); =20 -=09mm_shrinker =3D shrinker_alloc(SHRINKER_NUMA_AWARE, "drm-ttm_pool"); +=09mm_shrinker =3D shrinker_alloc(SHRINKER_MEMCG_AWARE | SHRINKER_NUMA_AWA= RE, "drm-ttm_pool"); =09if (!mm_shrinker) =09=09return -ENOMEM; =20 diff --git a/mm/list_lru.c b/mm/list_lru.c index 36662d02ff96..2ccc3317cff9 100644 --- a/mm/list_lru.c +++ b/mm/list_lru.c @@ -627,6 +627,7 @@ int memcg_list_lru_alloc(struct mem_cgroup *memcg, stru= ct list_lru *lru, =09=09return 0; =09return __memcg_list_lru_alloc(memcg, lru, gfp); } +EXPORT_SYMBOL_GPL(memcg_list_lru_alloc); =20 int folio_memcg_list_lru_alloc(struct folio *folio, struct list_lru *lru, =09=09=09 gfp_t gfp) --=20 2.54.0