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 84AE6CD98D7 for ; Thu, 11 Jun 2026 17:34:07 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 413BE10F059; Thu, 11 Jun 2026 17:34:07 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="ggNYd+uW"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.20]) by gabe.freedesktop.org (Postfix) with ESMTPS id 9243210F03F; Thu, 11 Jun 2026 17:34:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1781199245; x=1812735245; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FsvkI3LOasYuprXDVfPJVH1qnNqYfZwMGOjwjiJ3aL0=; b=ggNYd+uWOPzn1CmqWUJ4B6kgNYpgu07nuQFKbFi75lRvVMM+nA5UFGO0 XnjhtIz9bwqgIFJ2yg5hQmQS3ZENsbuRINS1/KZb1kopjXFbPDZzg0HaU 1Lb8O3TfiaOoB/rvINWL2cjTn/NwGruhKRKQJmbG+xjIv/WTMkDXx+9h3 y6eAUZJ8+cf+IxZEY64uVRc4zKbeAf1O5871DAa9NhnoWlFGhUVREOi6K l4KYJWJLwwAdtT7ItcfK3QtCzqocPYAJ2WaG8UJr/VCd4MEPy7cGBE72t c0gTs1k/hrdmjd7fV8PPTr9p8eeDCLv1QzOGW9YlJX/ywPUFUy2+blArT g==; X-CSE-ConnectionGUID: P5qfcUhTSNCmQurWuD0IKQ== X-CSE-MsgGUID: 4ec9Q7mUQKeIjh7OKjNm8w== X-IronPort-AV: E=McAfee;i="6800,10657,11813"; a="81762073" X-IronPort-AV: E=Sophos;i="6.24,199,1774335600"; d="scan'208";a="81762073" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by orvoesa112.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Jun 2026 10:33:59 -0700 X-CSE-ConnectionGUID: AJklfiNSRYuU23pnbC/Ing== X-CSE-MsgGUID: NOD8/J2WTLKesy7zniP1QQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,199,1774335600"; d="scan'208";a="240215035" Received: from amilburn-desk.amilburn-desk (HELO fedora) ([10.245.244.169]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Jun 2026 10:33:55 -0700 From: =?UTF-8?q?Thomas=20Hellstr=C3=B6m?= To: intel-xe@lists.freedesktop.org Cc: =?UTF-8?q?Thomas=20Hellstr=C3=B6m?= , Natalie Vock , Johannes Weiner , Tejun Heo , =?UTF-8?q?Michal=20Koutn=C3=BD?= , cgroups@vger.kernel.org, Huang Rui , Matthew Brost , Matthew Auld , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Simona Vetter , David Airlie , =?UTF-8?q?Christian=20K=C3=B6nig?= , Alex Deucher , Rodrigo Vivi , dri-devel@lists.freedesktop.org, amd-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 5/6] drm/xe: Wire up dmem cgroup reclaim for VRAM manager Date: Thu, 11 Jun 2026 19:33:00 +0200 Message-ID: <20260611173301.17473-6-thomas.hellstrom@linux.intel.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260611173301.17473-1-thomas.hellstrom@linux.intel.com> References: <20260611173301.17473-1-thomas.hellstrom@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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" Register the VRAM manager with the dmem cgroup reclaim infrastructure so that lowering dmem.max below current VRAM usage triggers TTM eviction rather than failing with -EBUSY. v4: - Rebased on drm-tip; dropped the XE_PL_STOLEN guard as stolen memory uses a separate TTM manager and never calls __xe_ttm_vram_mgr_init(). v5: - Rebased on the introduction of struct dmem_cgroup_init. - Register the fini drmm action before drmm_cgroup_register_region() so that devres LIFO teardown runs unregister_region() first (draining any in-flight reclaim callbacks via the rwsem) and xe_ttm_vram_mgr_fini() second, ensuring the manager is never accessed by a reclaim callback after teardown. (Sashiko-bot) - Wrap the reclaim callback in xe_ttm_vram_mgr_dmem_reclaim() using drm_dev_enter()/drm_dev_exit() to prevent TTM reclaim from running after driver unbind. Assisted-by: GitHub_Copilot:claude-sonnet-4.6 Signed-off-by: Thomas Hellström --- drivers/gpu/drm/xe/xe_ttm_vram_mgr.c | 54 +++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c index 308fda4248eb..b2500344cd57 100644 --- a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c +++ b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c @@ -276,6 +276,28 @@ static const struct ttm_resource_manager_func xe_ttm_vram_mgr_func = { .debug = xe_ttm_vram_mgr_debug }; +static const struct dmem_cgroup_ops xe_ttm_vram_mgr_dmem_ops; + +static int xe_ttm_vram_mgr_dmem_reclaim(struct dmem_cgroup_pool_state *pool, + u64 target_bytes, void *priv) +{ + struct ttm_resource_manager *man = priv; + struct xe_device *xe = ttm_to_xe_device(man->bdev); + int ret, idx; + + if (!drm_dev_enter(&xe->drm, &idx)) + return -ENODEV; + + ret = ttm_resource_manager_dmem_reclaim(pool, target_bytes, priv); + + drm_dev_exit(idx); + return ret; +} + +static const struct dmem_cgroup_ops xe_ttm_vram_mgr_dmem_ops = { + .reclaim = xe_ttm_vram_mgr_dmem_reclaim, +}; + static void xe_ttm_vram_mgr_fini(struct drm_device *dev, void *arg) { struct xe_device *xe = to_xe_device(dev); @@ -301,17 +323,10 @@ int __xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_ttm_vram_mgr *mgr, u64 default_page_size) { struct ttm_resource_manager *man = &mgr->manager; + struct dmem_cgroup_region *cg; const char *name; int err; - name = mem_type == XE_PL_VRAM0 ? "vram0" : "vram1"; - man->cg = drmm_cgroup_register_region(&xe->drm, name, - &(struct dmem_cgroup_init){ - .size = size, - }); - if (IS_ERR(man->cg)) - return PTR_ERR(man->cg); - man->func = &xe_ttm_vram_mgr_func; mgr->mem_type = mem_type; err = drmm_mutex_init(&xe->drm, &mgr->lock); @@ -330,7 +345,28 @@ int __xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_ttm_vram_mgr *mgr, ttm_set_driver_manager(&xe->ttm, mem_type, &mgr->manager); ttm_resource_manager_set_used(&mgr->manager, true); - return drmm_add_action_or_reset(&xe->drm, xe_ttm_vram_mgr_fini, mgr); + /* + * Register the fini action before the cgroup region so that devres + * LIFO teardown runs unregister_region first (draining any in-flight + * reclaim callbacks) and the manager fini second. + */ + err = drmm_add_action_or_reset(&xe->drm, xe_ttm_vram_mgr_fini, mgr); + if (err) + return err; + + name = mem_type == XE_PL_VRAM0 ? "vram0" : "vram1"; + cg = drmm_cgroup_register_region(&xe->drm, name, + &(struct dmem_cgroup_init){ + .size = size, + .ops = &xe_ttm_vram_mgr_dmem_ops, + .reclaim_priv = man, + }); + if (IS_ERR(cg)) + return PTR_ERR(cg); + + ttm_resource_manager_set_dmem_region(man, cg); + + return 0; } /** -- 2.54.0