From: Brian Welty <brian.welty-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
To: "Brian Welty"
<brian.welty-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>,
cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
"Tejun Heo" <tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>,
dri-devel-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org,
"David Airlie" <airlied-cv59FeDIM0c@public.gmane.org>,
"Daniel Vetter" <daniel-/w4YWyX8dFk@public.gmane.org>,
"Christian König" <christian.koenig-5C7GfCeVMHo@public.gmane.org>,
"Kenny Ho" <Kenny.Ho-5C7GfCeVMHo@public.gmane.org>,
amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org,
"Chris Wilson"
<chris-Y6uKTt2uX1cEflXRtASbqLVCufUGDwFn@public.gmane.org>,
"Tvrtko Ursulin"
<tvrtko.ursulin-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>,
intel-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org,
"Joonas Lahtinen"
<joonas.lahtinen-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>,
"Eero Tamminen"
<eero.t.tamminen-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
Subject: [RFC PATCH 3/9] drm, cgroup: Initialize drmcg properties
Date: Tue, 26 Jan 2021 13:46:20 -0800 [thread overview]
Message-ID: <20210126214626.16260-4-brian.welty@intel.com> (raw)
In-Reply-To: <20210126214626.16260-1-brian.welty-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
From: Kenny Ho <Kenny.Ho@amd.com>
drmcg initialization involves allocating a per cgroup, per device data
structure and setting the defaults. There are two entry points for
drmcg init:
1) When struct drmcg is created via css_alloc, initialization is done
for each device
2) When DRM devices are created after drmcgs are created, per
device drmcg data structure is allocated at the beginning of
DRM device creation such that drmcg can begin tracking usage
statistics
Entry point #2 usually applies to the root cgroup since it can be
created before DRM devices are available. The drmcg controller will go
through all existing drm cgroups and initialize them with the new device
accordingly.
Extending Kenny's original work, this has been simplified some and
the custom_init callback has been removed. (Brian)
Signed-off-by Kenny Ho <Kenny.Ho@amd.com>
Signed-off-by: Brian Welty <brian.welty@intel.com>
---
drivers/gpu/drm/drm_drv.c | 3 ++
include/drm/drm_cgroup.h | 17 +++++++
include/drm/drm_device.h | 7 +++
include/linux/cgroup_drm.h | 13 ++++++
kernel/cgroup/drm.c | 95 +++++++++++++++++++++++++++++++++++++-
5 files changed, 134 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c
index 3b940926d672..dac742445b38 100644
--- a/drivers/gpu/drm/drm_drv.c
+++ b/drivers/gpu/drm/drm_drv.c
@@ -570,6 +570,7 @@ static void drm_dev_init_release(struct drm_device *dev, void *res)
/* Prevent use-after-free in drm_managed_release when debugging is
* enabled. Slightly awkward, but can't really be helped. */
dev->dev = NULL;
+ mutex_destroy(&dev->drmcg_mutex);
mutex_destroy(&dev->master_mutex);
mutex_destroy(&dev->clientlist_mutex);
mutex_destroy(&dev->filelist_mutex);
@@ -612,6 +613,7 @@ static int drm_dev_init(struct drm_device *dev,
mutex_init(&dev->filelist_mutex);
mutex_init(&dev->clientlist_mutex);
mutex_init(&dev->master_mutex);
+ mutex_init(&dev->drmcg_mutex);
ret = drmm_add_action(dev, drm_dev_init_release, NULL);
if (ret)
@@ -652,6 +654,7 @@ static int drm_dev_init(struct drm_device *dev,
if (ret)
goto err;
+ drmcg_device_early_init(dev);
return 0;
err:
diff --git a/include/drm/drm_cgroup.h b/include/drm/drm_cgroup.h
index 530c9a0b3238..43caf1b6a0de 100644
--- a/include/drm/drm_cgroup.h
+++ b/include/drm/drm_cgroup.h
@@ -4,6 +4,17 @@
#ifndef __DRM_CGROUP_H__
#define __DRM_CGROUP_H__
+#include <drm/drm_file.h>
+
+struct drm_device;
+
+/**
+ * Per DRM device properties for DRM cgroup controller for the purpose
+ * of storing per device defaults
+ */
+struct drmcg_props {
+};
+
#ifdef CONFIG_CGROUP_DRM
void drmcg_bind(struct drm_minor (*(*acq_dm)(unsigned int minor_id)),
@@ -15,6 +26,8 @@ void drmcg_register_dev(struct drm_device *dev);
void drmcg_unregister_dev(struct drm_device *dev);
+void drmcg_device_early_init(struct drm_device *device);
+
#else
static inline void drmcg_bind(
@@ -35,5 +48,9 @@ static inline void drmcg_unregister_dev(struct drm_device *dev)
{
}
+static inline void drmcg_device_early_init(struct drm_device *device)
+{
+}
+
#endif /* CONFIG_CGROUP_DRM */
#endif /* __DRM_CGROUP_H__ */
diff --git a/include/drm/drm_device.h b/include/drm/drm_device.h
index d647223e8390..1cdccc9a653c 100644
--- a/include/drm/drm_device.h
+++ b/include/drm/drm_device.h
@@ -8,6 +8,7 @@
#include <drm/drm_hashtab.h>
#include <drm/drm_mode_config.h>
+#include <drm/drm_cgroup.h>
struct drm_driver;
struct drm_minor;
@@ -318,6 +319,12 @@ struct drm_device {
*/
struct drm_fb_helper *fb_helper;
+ /** \name DRM Cgroup */
+ /*@{ */
+ struct mutex drmcg_mutex;
+ struct drmcg_props drmcg_props;
+ /*@} */
+
/* Everything below here is for legacy driver, never use! */
/* private: */
#if IS_ENABLED(CONFIG_DRM_LEGACY)
diff --git a/include/linux/cgroup_drm.h b/include/linux/cgroup_drm.h
index 307bb75db248..50f055804400 100644
--- a/include/linux/cgroup_drm.h
+++ b/include/linux/cgroup_drm.h
@@ -12,11 +12,19 @@
#ifdef CONFIG_CGROUP_DRM
+/**
+ * Per DRM cgroup, per device resources (such as statistics and limits)
+ */
+struct drmcg_device_resource {
+ /* for per device stats */
+};
+
/**
* The DRM cgroup controller data structure.
*/
struct drmcg {
struct cgroup_subsys_state css;
+ struct drmcg_device_resource *dev_resources[MAX_DRM_DEV];
};
/**
@@ -40,6 +48,8 @@ static inline struct drmcg *css_to_drmcg(struct cgroup_subsys_state *css)
*/
static inline struct drmcg *drmcg_get(struct task_struct *task)
{
+ if (!cgroup_subsys_enabled(gpu_cgrp_subsys))
+ return NULL;
return css_to_drmcg(task_get_css(task, gpu_cgrp_id));
}
@@ -70,6 +80,9 @@ static inline struct drmcg *drmcg_parent(struct drmcg *cg)
#else /* CONFIG_CGROUP_DRM */
+struct drmcg_device_resource {
+};
+
struct drmcg {
};
diff --git a/kernel/cgroup/drm.c b/kernel/cgroup/drm.c
index 061bb9c458e4..836929c27de8 100644
--- a/kernel/cgroup/drm.c
+++ b/kernel/cgroup/drm.c
@@ -1,11 +1,15 @@
// SPDX-License-Identifier: MIT
-// Copyright 2019 Advanced Micro Devices, Inc.
+/*
+ * Copyright 2019 Advanced Micro Devices, Inc.
+ * Copyright © 2021 Intel Corporation
+ */
#include <linux/bitmap.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/cgroup.h>
#include <linux/cgroup_drm.h>
#include <drm/drm_file.h>
+#include <drm/drm_drv.h>
#include <drm/drm_device.h>
#include <drm/drm_cgroup.h>
@@ -54,6 +58,26 @@ void drmcg_unbind(void)
}
EXPORT_SYMBOL(drmcg_unbind);
+/* caller must hold dev->drmcg_mutex */
+static inline int init_drmcg_single(struct drmcg *drmcg, struct drm_device *dev)
+{
+ int minor = dev->primary->index;
+ struct drmcg_device_resource *ddr = drmcg->dev_resources[minor];
+
+ if (ddr == NULL) {
+ ddr = kzalloc(sizeof(struct drmcg_device_resource),
+ GFP_KERNEL);
+
+ if (!ddr)
+ return -ENOMEM;
+ }
+
+ /* set defaults here */
+ drmcg->dev_resources[minor] = ddr;
+
+ return 0;
+}
+
/**
* drmcg_register_dev - register a DRM device for usage in drm cgroup
* @dev: DRM device
@@ -137,23 +161,61 @@ static int drm_minor_for_each(int (*fn)(int id, void *p, void *data),
return rc;
}
+static int drmcg_css_free_fn(int id, void *ptr, void *data)
+{
+ struct drm_minor *minor = ptr;
+ struct drmcg *drmcg = data;
+
+ if (minor->type != DRM_MINOR_PRIMARY)
+ return 0;
+
+ kfree(drmcg->dev_resources[minor->index]);
+
+ return 0;
+}
+
static void drmcg_css_free(struct cgroup_subsys_state *css)
{
struct drmcg *drmcg = css_to_drmcg(css);
+ drm_minor_for_each(&drmcg_css_free_fn, drmcg);
+
kfree(drmcg);
}
+static int init_drmcg_fn(int id, void *ptr, void *data)
+{
+ struct drm_minor *minor = ptr;
+ struct drmcg *drmcg = data;
+ int rc;
+
+ if (minor->type != DRM_MINOR_PRIMARY)
+ return 0;
+
+ mutex_lock(&minor->dev->drmcg_mutex);
+ rc = init_drmcg_single(drmcg, minor->dev);
+ mutex_unlock(&minor->dev->drmcg_mutex);
+
+ return rc;
+}
+
static struct cgroup_subsys_state *
drmcg_css_alloc(struct cgroup_subsys_state *parent_css)
{
struct drmcg *parent = css_to_drmcg(parent_css);
struct drmcg *drmcg;
+ int rc;
drmcg = kzalloc(sizeof(struct drmcg), GFP_KERNEL);
if (!drmcg)
return ERR_PTR(-ENOMEM);
+ rc = drm_minor_for_each(&init_drmcg_fn, drmcg);
+ if (rc) {
+ drmcg_css_free(&drmcg->css);
+ return ERR_PTR(rc);
+ }
+
if (!parent)
root_drmcg = drmcg;
@@ -171,3 +233,34 @@ struct cgroup_subsys gpu_cgrp_subsys = {
.legacy_cftypes = files,
.dfl_cftypes = files,
};
+
+/**
+ * drmcg_device_early_init - initialize device specific resources for DRM cgroups
+ * @dev: the target DRM device
+ *
+ * Allocate and initialize device specific resources for existing DRM cgroups.
+ * Typically only the root cgroup exists before the initialization of @dev.
+ */
+void drmcg_device_early_init(struct drm_device *dev)
+{
+ struct cgroup_subsys_state *pos;
+
+ if (root_drmcg == NULL)
+ return;
+
+ /* init cgroups created before registration (i.e. root cgroup) */
+ rcu_read_lock();
+ css_for_each_descendant_pre(pos, &root_drmcg->css) {
+ css_get(pos);
+ rcu_read_unlock();
+
+ mutex_lock(&dev->drmcg_mutex);
+ init_drmcg_single(css_to_drmcg(pos), dev);
+ mutex_unlock(&dev->drmcg_mutex);
+
+ rcu_read_lock();
+ css_put(pos);
+ }
+ rcu_read_unlock();
+}
+EXPORT_SYMBOL(drmcg_device_early_init);
--
2.20.1
_______________________________________________
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
next prev parent reply other threads:[~2021-01-26 21:46 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-26 21:46 [RFC PATCH 0/9] cgroup support for GPU devices Brian Welty
2021-01-26 21:46 ` [RFC PATCH 1/9] cgroup: Introduce cgroup for drm subsystem Brian Welty
2021-01-26 21:46 ` [RFC PATCH 2/9] drm, cgroup: Bind drm and cgroup subsystem Brian Welty
2021-01-26 21:46 ` [RFC PATCH 4/9] drmcg: Add skeleton seq_show and write for drmcg files Brian Welty
2021-01-26 21:46 ` [RFC PATCH 5/9] drmcg: Add support for device memory accounting via page counter Brian Welty
2021-01-29 2:45 ` [RFC PATCH 0/9] cgroup support for GPU devices Xingyou Chen
[not found] ` <20210126214626.16260-1-brian.welty-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2021-01-26 21:46 ` Brian Welty [this message]
2021-01-26 21:46 ` [RFC PATCH 6/9] drmcg: Add memory.total file Brian Welty
2021-01-26 21:46 ` [RFC PATCH 7/9] drmcg: Add initial support for tracking gpu time usage Brian Welty
[not found] ` <161235875541.15744.14541970842808007912@jlahtine-mobl.ger.corp.intel.com>
2021-02-04 2:23 ` Brian Welty
2021-01-26 21:46 ` [RFC PATCH 8/9] drm/gem: Associate GEM objects with drm cgroup Brian Welty
2021-02-09 10:54 ` Daniel Vetter
[not found] ` <YCJp//kMC7YjVMXv-dv86pmgwkMBes7Z6vYuT8azUEOm+Xw19@public.gmane.org>
2021-02-10 7:52 ` Thomas Zimmermann
2021-02-10 12:45 ` Daniel Vetter
2021-02-10 22:00 ` Brian Welty
[not found] ` <dffeb6a7-90f1-e17c-9695-44678e7a39cb-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2021-02-11 15:34 ` Daniel Vetter
2021-03-06 0:44 ` Brian Welty
2021-03-18 10:16 ` Daniel Vetter
[not found] ` <CAKMK7uG2PFMWXa9o4LzsF1r0Mc-M8KqD-PKZkCj+m7XeO5wCyg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2021-03-18 19:20 ` Brian Welty
[not found] ` <67867078-4f4b-0a6a-e55d-453b973d8b7c-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2021-05-10 15:36 ` Daniel Vetter
[not found] ` <CAKMK7uG7EWv93EbRcMRCm+opi=7fQPMOv2z1R6GBhJXb6--28w-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2021-05-10 16:06 ` Tamminen, Eero T
2021-01-26 21:46 ` [RFC PATCH 9/9] drm/i915: Use memory cgroup for enforcing device memory limit Brian Welty
2021-01-29 3:00 ` [RFC PATCH 0/9] cgroup support for GPU devices Xingyou Chen
[not found] ` <84b79978-84c9-52aa-b761-3f4be929064e-hTEXxzOs8fRg9hUCZPvPmw@public.gmane.org>
2021-02-01 23:21 ` Brian Welty
[not found] ` <5307d21b-7494-858c-30f0-cb5fe1d86004-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2021-02-03 10:18 ` Daniel Vetter
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210126214626.16260-4-brian.welty@intel.com \
--to=brian.welty-ral2jqcrhueavxtiumwx3w@public.gmane.org \
--cc=Kenny.Ho-5C7GfCeVMHo@public.gmane.org \
--cc=airlied-cv59FeDIM0c@public.gmane.org \
--cc=amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org \
--cc=cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=chris-Y6uKTt2uX1cEflXRtASbqLVCufUGDwFn@public.gmane.org \
--cc=christian.koenig-5C7GfCeVMHo@public.gmane.org \
--cc=daniel-/w4YWyX8dFk@public.gmane.org \
--cc=dri-devel-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org \
--cc=eero.t.tamminen-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org \
--cc=intel-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org \
--cc=joonas.lahtinen-VuQAYsv1563Yd54FQh9/CA@public.gmane.org \
--cc=tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
--cc=tvrtko.ursulin-VuQAYsv1563Yd54FQh9/CA@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).