From nobody Thu Feb 12 00:03:15 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7D050C7EE22 for ; Wed, 3 May 2023 09:00:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229793AbjECJAX (ORCPT ); Wed, 3 May 2023 05:00:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229461AbjECJAR (ORCPT ); Wed, 3 May 2023 05:00:17 -0400 Received: from mblankhorst.nl (lankhorst.se [141.105.120.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C10B410D9; Wed, 3 May 2023 02:00:14 -0700 (PDT) From: Maarten Lankhorst To: dri-devel@lists.freedesktop.org, cgroups@vger.kernel.org, intel-xe@lists.freedesktop.org Cc: intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, Tejun Heo , Zefan Li , Johannes Weiner , David Airlie , Daniel Vetter , amd-gfx@lists.freedesktop.org, Maxime Ripard , Thomas Zimmermann , Maarten Lankhorst , Tvrtko Ursulin Subject: [RFC PATCH 4/4] drm/xe: Add support for the drm cgroup Date: Wed, 3 May 2023 10:35:00 +0200 Message-Id: <20230503083500.645848-5-maarten.lankhorst@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230503083500.645848-1-maarten.lankhorst@linux.intel.com> References: <20230503083500.645848-1-maarten.lankhorst@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add some code to implement basic support for the vram0, vram1 and stolen memory regions. I fear the try_charge code should probably be done inside TTM. This code should interact with the shrinker, but for a simple RFC it's good enough. Signed-off-by: Maarten Lankhorst --- drivers/gpu/drm/xe/xe_device.c | 4 ++++ drivers/gpu/drm/xe/xe_device_types.h | 4 ++++ drivers/gpu/drm/xe/xe_ttm_vram_mgr.c | 21 +++++++++++++++++++-- drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h | 5 +++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/xe/xe_device.c b/drivers/gpu/drm/xe/xe_device.c index 45d6e5ff47fd..f0a5af15a662 100644 --- a/drivers/gpu/drm/xe/xe_device.c +++ b/drivers/gpu/drm/xe/xe_device.c @@ -291,6 +291,10 @@ int xe_device_probe(struct xe_device *xe) /* Allocate and map stolen after potential VRAM resize */ xe_ttm_stolen_mgr_init(xe); =20 + err =3D drmmcg_register_device(&xe->drm, &xe->cg); + if (err) + goto err_irq_shutdown; + /* * Now that GT is initialized (TTM in particular), * we can try to init display, and inherit the initial fb. diff --git a/drivers/gpu/drm/xe/xe_device_types.h b/drivers/gpu/drm/xe/xe_d= evice_types.h index 1cb404e48aaa..04b85060cbec 100644 --- a/drivers/gpu/drm/xe/xe_device_types.h +++ b/drivers/gpu/drm/xe/xe_device_types.h @@ -12,6 +12,8 @@ #include #include =20 +#include + #include "xe_gt_types.h" #include "xe_platform_types.h" #include "xe_step_types.h" @@ -55,6 +57,8 @@ struct xe_device { /** @drm: drm device */ struct drm_device drm; =20 + struct drmcgroup_device cg; + /** @info: device info */ struct intel_device_info { /** @graphics_name: graphics IP name */ diff --git a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c b/drivers/gpu/drm/xe/xe_t= tm_vram_mgr.c index 73836b9b7fed..263cd4ef7b6d 100644 --- a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c +++ b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c @@ -50,6 +50,7 @@ static int xe_ttm_vram_mgr_new(struct ttm_resource_manage= r *man, struct ttm_resource **res) { struct xe_ttm_vram_mgr *mgr =3D to_xe_ttm_vram_mgr(man); + struct xe_device *xe =3D ttm_to_xe_device(tbo->bdev); struct xe_ttm_vram_mgr_resource *vres; struct drm_buddy *mm =3D &mgr->mm; u64 size, remaining_size, min_page_size; @@ -116,9 +117,8 @@ static int xe_ttm_vram_mgr_new(struct ttm_resource_mana= ger *man, =20 mutex_lock(&mgr->lock); if (lpfn <=3D mgr->visible_size >> PAGE_SHIFT && size > mgr->visible_avai= l) { - mutex_unlock(&mgr->lock); err =3D -ENOSPC; - goto error_fini; + goto error_unlock; } =20 if (place->fpfn + (size >> PAGE_SHIFT) !=3D place->lpfn && @@ -129,6 +129,10 @@ static int xe_ttm_vram_mgr_new(struct ttm_resource_man= ager *man, lpfn =3D max_t(unsigned long, place->fpfn + (size >> PAGE_SHIFT), lpfn); } =20 + err =3D drmcg_try_charge(&vres->cg, &xe->cg, mgr->mem_type, vres->base.si= ze); + if (err) + goto error_unlock; + remaining_size =3D size; do { /* @@ -197,6 +201,8 @@ static int xe_ttm_vram_mgr_new(struct ttm_resource_mana= ger *man, =20 error_free_blocks: drm_buddy_free_list(mm, &vres->blocks); + drmcg_uncharge(vres->cg, &xe->cg, mgr->mem_type, vres->base.size); +error_unlock: mutex_unlock(&mgr->lock); error_fini: ttm_resource_fini(man, &vres->base); @@ -211,6 +217,7 @@ static void xe_ttm_vram_mgr_del(struct ttm_resource_man= ager *man, struct xe_ttm_vram_mgr_resource *vres =3D to_xe_ttm_vram_mgr_resource(res); struct xe_ttm_vram_mgr *mgr =3D to_xe_ttm_vram_mgr(man); + struct xe_device *xe =3D ttm_to_xe_device(man->bdev); struct drm_buddy *mm =3D &mgr->mm; =20 mutex_lock(&mgr->lock); @@ -218,6 +225,7 @@ static void xe_ttm_vram_mgr_del(struct ttm_resource_man= ager *man, mgr->visible_avail +=3D vres->used_visible_size; mutex_unlock(&mgr->lock); =20 + drmcg_uncharge(vres->cg, &xe->cg, mgr->mem_type, vres->base.size); ttm_resource_fini(man, res); =20 kfree(vres); @@ -337,6 +345,15 @@ int __xe_ttm_vram_mgr_init(struct xe_device *xe, struc= t xe_ttm_vram_mgr *mgr, struct ttm_resource_manager *man =3D &mgr->manager; int err; =20 + xe->cg.regions[mem_type].size =3D size; + + if (mem_type =3D=3D XE_PL_STOLEN) { + xe->cg.regions[mem_type].name =3D "stolen"; + } else { + xe->cg.regions[mem_type].name =3D + mem_type =3D=3D XE_PL_VRAM0 ? "vram0" : "vram1"; + } + man->func =3D &xe_ttm_vram_mgr_func; mgr->mem_type =3D mem_type; mutex_init(&mgr->lock); diff --git a/drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h b/drivers/gpu/drm/x= e/xe_ttm_vram_mgr_types.h index 3d9417ff7434..232585d7ae69 100644 --- a/drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h +++ b/drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h @@ -9,6 +9,8 @@ #include #include =20 +struct drmcgroup_state; + struct xe_gt; =20 /** @@ -47,6 +49,9 @@ struct xe_ttm_vram_mgr_resource { u64 used_visible_size; /** @flags: flags associated with the resource */ unsigned long flags; + + /** @cg: cgroup this resource is charged to */ + struct drmcgroup_state *cg; }; =20 #endif --=20 2.34.1