From patchwork Thu Jan 12 16:56:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 42672 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp4013228wrt; Thu, 12 Jan 2023 09:38:11 -0800 (PST) X-Google-Smtp-Source: AMrXdXvZWSICkfhnU0L+CaaCr+ssclLugEdJ0mQ4L/bZR8V79QLJ7Q/PxAxpIVze7KaKJVrNjOpm X-Received: by 2002:a17:90a:fa04:b0:226:7a48:e644 with SMTP id cm4-20020a17090afa0400b002267a48e644mr7386832pjb.8.1673545091220; Thu, 12 Jan 2023 09:38:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673545091; cv=none; d=google.com; s=arc-20160816; b=ITAuc6AYbLEJk1D3YvsraiVTdhUj4Ytf0DAUBmWx6ol5JddNGfh03IS1qAPrdlzwrO 0jy7xjcXFd41XlIkzTEIJnSO5YiB77C0cyvepre4t4/tZIrtknJpy1ktkkT5zyMplq+u 5inT2vgufLfhTFTiLBlNP4kvHgto4MpWj1LTu38/yN9XwC1DFpVMMQ1dYIJSou62xJAu J8PHqI8TbycytaWGlX/ODSQcMxzotYdLY9CPGlQYOriFLXujirABKO3Zf8TTAD35b/ya qsf6m0alno/qH2myFuOusuwHQnFjy0jVH0fBMxpKY3/KkKDTnL8Wscsc5BNKW74gEhDw u/Yg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=HfC6zi2yz5FpXfRBLE51PCPYGZsv5yFJwA79DWmOd8k=; b=yg31mjJVJRqQBLWxD73oCqz0GWjg4Pt8y+Z2CUXzl1dk1V8JtzjaRb6ebxJ2vnpoTA 2iP+yMdnjrDaJi8pzzssL9WoFWJNea9DjNObnbrPQuNfkquBIqYCn2bePaHQiA3L9xvZ ZPfzqtgH2LkLPmQT3gA9p6Ik0auBI7pYFrlzW/nF8hrfbjvDOEb4XS1nChGYKjBFVe0p O7RrB/cStBOXVOg5W6EmxA2RL974J6Cp/v8YN2XEAj4KO+u/6MzX067oF7upg7FDyDl3 ePwZGRTdvGpXJdacJjzdlJ93iMpxJu1YmEWJ6lu2trg2PWofqS3HYOOBfylOUaBacE7J KZEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GxkZWRAl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b5-20020a17090a7ac500b0022695223cdcsi20064936pjl.178.2023.01.12.09.37.57; Thu, 12 Jan 2023 09:38:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GxkZWRAl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231699AbjALRhT (ORCPT + 99 others); Thu, 12 Jan 2023 12:37:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54456 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233079AbjALRgV (ORCPT ); Thu, 12 Jan 2023 12:36:21 -0500 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8EABB7825F; Thu, 12 Jan 2023 08:58:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673542680; x=1705078680; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bAsuGvW+cjJ47gR3odvUtOpS3YhJhQdU2PbgaMP59V0=; b=GxkZWRAlTs5zpThtXT6ic6nm7QhNW6YC+bQUoHI2Le7UzVa31mHe90om vj2pzLLgzcyPRSU8kTwAQCtwwgiwzyIHU25EbI77C48BF81yNIshYeHr6 XYS/qWoH3WnOWoiiCqNX1qkr19QzIhp/qe513QqccINPmaAogw2DfSyuk DzucEY3WOk9WlxigIuCuBI1jINRDiMSYJbQxDrigqPLJeoVz5wz3qiJOf wox6rkHgf/lu1OnDsOKntygLfpnajLzv4TuR7ATBuDfIWvLchEGVG6F1P 4j9P8qwVyPmVtn7P6hgPQYVjXR/cZ9YDIKfbhdabru9VRn2A0MM6LwzrB A==; X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="325016465" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="325016465" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:56:50 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="651232782" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="651232782" Received: from jacton-mobl.ger.corp.intel.com (HELO localhost.localdomain) ([10.213.195.171]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:56:46 -0800 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_Marchesin?= , "T . J . Mercier" , Kenny.Ho@amd.com, =?utf-8?q?Chris?= =?utf-8?q?tian_K=C3=B6nig?= , Brian Welty , Tvrtko Ursulin Subject: [RFC 06/12] drm/cgroup: Add ability to query drm cgroup GPU time Date: Thu, 12 Jan 2023 16:56:03 +0000 Message-Id: <20230112165609.1083270-7-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230112165609.1083270-1-tvrtko.ursulin@linux.intel.com> References: <20230112165609.1083270-1-tvrtko.ursulin@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,HK_RANDOM_ENVFROM,HK_RANDOM_FROM, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754839217686452367?= X-GMAIL-MSGID: =?utf-8?q?1754839217686452367?= From: Tvrtko Ursulin Add a driver callback and core helper which allow querying the time spent on GPUs for processes belonging to a group. Signed-off-by: Tvrtko Ursulin --- drivers/gpu/drm/drm_cgroup.c | 24 ++++++++++++++++++++++++ include/drm/drm_clients.h | 2 ++ include/drm/drm_drv.h | 28 ++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/drivers/gpu/drm/drm_cgroup.c b/drivers/gpu/drm/drm_cgroup.c index 46b012d2be42..bc1e34f1a552 100644 --- a/drivers/gpu/drm/drm_cgroup.c +++ b/drivers/gpu/drm/drm_cgroup.c @@ -138,3 +138,27 @@ drm_clients_migrate(struct drm_file *file_priv, pid_nr(old), pid_nr(new)); rcu_read_unlock(); } + +u64 drm_pid_get_active_time_us(struct pid *pid) +{ + struct drm_pid_clients *clients; + u64 total = 0; + + rcu_read_lock(); + clients = xa_load(&drm_pid_clients, (unsigned long)pid); + if (clients) { + struct drm_file *fpriv; + + list_for_each_entry_rcu(fpriv, &clients->file_list, clink) { + const struct drm_cgroup_ops *cg_ops = + fpriv->minor->dev->driver->cg_ops; + + if (cg_ops && cg_ops->active_time_us) + total += cg_ops->active_time_us(fpriv); + } + } + rcu_read_unlock(); + + return total; +} +EXPORT_SYMBOL_GPL(drm_pid_get_active_time_us); diff --git a/include/drm/drm_clients.h b/include/drm/drm_clients.h index 2732fffab3f0..7e0c0cf14f25 100644 --- a/include/drm/drm_clients.h +++ b/include/drm/drm_clients.h @@ -41,4 +41,6 @@ drm_clients_migrate(struct drm_file *file_priv, } #endif +u64 drm_pid_get_active_time_us(struct pid *pid); + #endif diff --git a/include/drm/drm_drv.h b/include/drm/drm_drv.h index d7c521e8860f..f5f0e088e1fe 100644 --- a/include/drm/drm_drv.h +++ b/include/drm/drm_drv.h @@ -158,6 +158,24 @@ enum drm_driver_feature { DRIVER_KMS_LEGACY_CONTEXT = BIT(31), }; +/** + * struct drm_cgroup_ops + * + * This structure contains a number of callbacks that drivers can provide if + * they are able to support one or more of the functionalities implemented by + * the DRM cgroup controller. + */ +struct drm_cgroup_ops { + /** + * @active_time_us: + * + * Optional callback for reporting the GPU time consumed by this client. + * + * Used by the DRM core when queried by the DRM cgroup controller. + */ + u64 (*active_time_us) (struct drm_file *); +}; + /** * struct drm_driver - DRM driver structure * @@ -469,6 +487,16 @@ struct drm_driver { */ const struct file_operations *fops; +#ifdef CONFIG_CGROUP_DRM + /** + * @cg_ops: + * + * Optional pointer to driver callbacks facilitating integration with + * the DRM cgroup controller. + */ + const struct drm_cgroup_ops *cg_ops; +#endif + #ifdef CONFIG_DRM_LEGACY /* Everything below here is for legacy driver, never use! */ /* private: */