From nobody Sat Feb 7 17:42:20 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 E5C9CEB64DA for ; Wed, 12 Jul 2023 11:51:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233567AbjGLLvN (ORCPT ); Wed, 12 Jul 2023 07:51:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233431AbjGLLup (ORCPT ); Wed, 12 Jul 2023 07:50:45 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E39C11998; Wed, 12 Jul 2023 04:49:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1689162554; x=1720698554; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mbUDZguU3/3ubuVhjLVfeypXgwnwUhCLLRTeHmZzUr0=; b=XWTNZXE1szVvhFIaW0J5kGvKLIRB7WdwHG/Rsasn38lyC6o1BTIfUNX7 58WOqVGCuxvWtsJKaYJfYuQ9RqUZQ0xssFByvzrUtQnPTWFtK3mKECRMk FJhd3ghG2Hnyysvwe3w93L7dQEpH8mSP89q8FC/yj/TaLhHLZIDZMCR1o vLbtIiIjJ4d92o1RlXzyEC+GZJFG1nUR0lB2XwCTv/wNAZwYlqgc8Yh/i QSXKYGINJfUtwm4VZiaYsJXqFpwMgAMnK9dvOj6kC3ySJv5bNWasn+vES vtS/o9lLAblXx9icYDfJaBu0IvIiGNzc4lIY3VrUMLgQp5P4epVwn4bDm Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10768"; a="344469362" X-IronPort-AV: E=Sophos;i="6.01,199,1684825200"; d="scan'208";a="344469362" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jul 2023 04:47:06 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10768"; a="866094156" X-IronPort-AV: E=Sophos;i="6.01,199,1684825200"; d="scan'208";a="866094156" Received: from eamonnob-mobl1.ger.corp.intel.com (HELO localhost.localdomain) ([10.213.237.202]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jul 2023 04:47:02 -0700 From: Tvrtko Ursulin To: Intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org Cc: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Tejun Heo , Johannes Weiner , Zefan Li , Dave Airlie , Daniel Vetter , Rob Clark , =?UTF-8?q?St=C3=A9phane=20Marchesin?= , "T . J . Mercier" , Kenny.Ho@amd.com, =?UTF-8?q?Christian=20K=C3=B6nig?= , Brian Welty , Tvrtko Ursulin Subject: [PATCH 13/17] drm/i915: Wire up with drm controller GPU time query Date: Wed, 12 Jul 2023 12:46:01 +0100 Message-Id: <20230712114605.519432-14-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230712114605.519432-1-tvrtko.ursulin@linux.intel.com> References: <20230712114605.519432-1-tvrtko.ursulin@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" From: Tvrtko Ursulin Implement the drm_cgroup_ops->active_time_us callback. Signed-off-by: Tvrtko Ursulin --- drivers/gpu/drm/i915/i915_driver.c | 8 ++ drivers/gpu/drm/i915/i915_drm_client.c | 116 ++++++++++++++++++------- drivers/gpu/drm/i915/i915_drm_client.h | 2 + 3 files changed, 94 insertions(+), 32 deletions(-) diff --git a/drivers/gpu/drm/i915/i915_driver.c b/drivers/gpu/drm/i915/i915= _driver.c index 294b022de22b..62a544d17659 100644 --- a/drivers/gpu/drm/i915/i915_driver.c +++ b/drivers/gpu/drm/i915/i915_driver.c @@ -1791,6 +1791,12 @@ static const struct drm_ioctl_desc i915_ioctls[] =3D= { DRM_IOCTL_DEF_DRV(I915_GEM_VM_DESTROY, i915_gem_vm_destroy_ioctl, DRM_REN= DER_ALLOW), }; =20 +#ifdef CONFIG_CGROUP_DRM +static const struct drm_cgroup_ops i915_drm_cgroup_ops =3D { + .active_time_us =3D i915_drm_cgroup_get_active_time_us, +}; +#endif + /* * Interface history: * @@ -1820,6 +1826,8 @@ static const struct drm_driver i915_drm_driver =3D { .postclose =3D i915_driver_postclose, .show_fdinfo =3D PTR_IF(IS_ENABLED(CONFIG_PROC_FS), i915_drm_client_fdinf= o), =20 + .cg_ops =3D PTR_IF(IS_ENABLED(CONFIG_CGROUP_DRM), &i915_drm_cgroup_ops), + .gem_prime_import =3D i915_gem_prime_import, =20 .dumb_create =3D i915_gem_dumb_create, diff --git a/drivers/gpu/drm/i915/i915_drm_client.c b/drivers/gpu/drm/i915/= i915_drm_client.c index 9e7a6075ee25..c3298beb094a 100644 --- a/drivers/gpu/drm/i915/i915_drm_client.c +++ b/drivers/gpu/drm/i915/i915_drm_client.c @@ -44,6 +44,86 @@ void __i915_drm_client_free(struct kref *kref) kfree(client); } =20 +#if defined(CONFIG_PROC_FS) || defined(CONFIG_CGROUP_DRM) +static const char * const uabi_class_names[] =3D { + [I915_ENGINE_CLASS_RENDER] =3D "render", + [I915_ENGINE_CLASS_COPY] =3D "copy", + [I915_ENGINE_CLASS_VIDEO] =3D "video", + [I915_ENGINE_CLASS_VIDEO_ENHANCE] =3D "video-enhance", + [I915_ENGINE_CLASS_COMPUTE] =3D "compute", +}; + +static u64 busy_add(struct i915_gem_context *ctx, unsigned int class) +{ + struct i915_gem_engines_iter it; + struct intel_context *ce; + u64 total =3D 0; + + for_each_gem_engine(ce, rcu_dereference(ctx->engines), it) { + if (ce->engine->uabi_class !=3D class) + continue; + + total +=3D intel_context_get_total_runtime_ns(ce); + } + + return total; +} + +static u64 get_class_active_ns(struct i915_drm_client *client, + struct drm_i915_private *i915, + unsigned int class, + unsigned int *capacity) +{ + struct i915_gem_context *ctx; + u64 total; + + *capacity =3D i915->engine_uabi_class_count[class]; + if (!*capacity) + return 0; + + total =3D atomic64_read(&client->past_runtime[class]); + + rcu_read_lock(); + list_for_each_entry_rcu(ctx, &client->ctx_list, client_link) + total +=3D busy_add(ctx, class); + rcu_read_unlock(); + + return total; +} + +static bool supports_stats(struct drm_i915_private *i915) +{ + return GRAPHICS_VER(i915) >=3D 8; +} +#endif + +#if defined(CONFIG_CGROUP_DRM) +u64 i915_drm_cgroup_get_active_time_us(struct drm_file *file) +{ + struct drm_i915_file_private *fpriv =3D file->driver_priv; + struct i915_drm_client *client =3D fpriv->client; + struct drm_i915_private *i915 =3D fpriv->i915; + unsigned int i; + u64 busy =3D 0; + + if (!supports_stats(i915)) + return 0; + + for (i =3D 0; i < ARRAY_SIZE(uabi_class_names); i++) { + unsigned int capacity; + u64 b; + + b =3D get_class_active_ns(client, i915, i, &capacity); + if (capacity) { + b =3D DIV_ROUND_UP_ULL(b, capacity * 1000); + busy +=3D b; + } + } + + return busy; +} +#endif + #ifdef CONFIG_PROC_FS static void obj_meminfo(struct drm_i915_gem_object *obj, @@ -128,44 +208,16 @@ static void show_meminfo(struct drm_printer *p, struc= t drm_file *file) mr->name); } =20 -static const char * const uabi_class_names[] =3D { - [I915_ENGINE_CLASS_RENDER] =3D "render", - [I915_ENGINE_CLASS_COPY] =3D "copy", - [I915_ENGINE_CLASS_VIDEO] =3D "video", - [I915_ENGINE_CLASS_VIDEO_ENHANCE] =3D "video-enhance", - [I915_ENGINE_CLASS_COMPUTE] =3D "compute", -}; - -static u64 busy_add(struct i915_gem_context *ctx, unsigned int class) -{ - struct i915_gem_engines_iter it; - struct intel_context *ce; - u64 total =3D 0; - - for_each_gem_engine(ce, rcu_dereference(ctx->engines), it) { - if (ce->engine->uabi_class !=3D class) - continue; - - total +=3D intel_context_get_total_runtime_ns(ce); - } - - return total; -} - static void show_client_class(struct drm_printer *p, struct drm_i915_private *i915, struct i915_drm_client *client, unsigned int class) { - const unsigned int capacity =3D i915->engine_uabi_class_count[class]; - u64 total =3D atomic64_read(&client->past_runtime[class]); - struct i915_gem_context *ctx; + unsigned int capacity; + u64 total; =20 - rcu_read_lock(); - list_for_each_entry_rcu(ctx, &client->ctx_list, client_link) - total +=3D busy_add(ctx, class); - rcu_read_unlock(); + total =3D get_class_active_ns(client, i915, class, &capacity); =20 if (capacity) drm_printf(p, "drm-engine-%s:\t%llu ns\n", @@ -191,7 +243,7 @@ void i915_drm_client_fdinfo(struct drm_printer *p, stru= ct drm_file *file) =20 show_meminfo(p, file); =20 - if (GRAPHICS_VER(i915) < 8) + if (!supports_stats(i915)) return; =20 for (i =3D 0; i < ARRAY_SIZE(uabi_class_names); i++) diff --git a/drivers/gpu/drm/i915/i915_drm_client.h b/drivers/gpu/drm/i915/= i915_drm_client.h index 69cedfcd3d69..e0b143890e69 100644 --- a/drivers/gpu/drm/i915/i915_drm_client.h +++ b/drivers/gpu/drm/i915/i915_drm_client.h @@ -90,4 +90,6 @@ i915_drm_client_add_context_objects(struct i915_drm_clien= t *client, } #endif =20 +u64 i915_drm_cgroup_get_active_time_us(struct drm_file *file); + #endif /* !__I915_DRM_CLIENT_H__ */ --=20 2.39.2