DRM scheduling cgroup controller
Subsystems (8)
v2 cover DRM scheduling cgroup controller
From: Tvrtko Ursulin <[email protected]>
*** vvv Read this first please vvv ***
I am re-sending this to dri-devel directly, having realized neither v1 or v2 have reached dri-devel due possible SMTP server issues. Other recipients and lists however did get both v1 in October and v2 two days ago.
Hence this is a re-post for the benefit of dri-devel only. And I do understand any replies would create a fork where not all lists, recipients would be up to date with discussions. But I thought dri-devel needed to be made aware of the proposal, while the SMTP server issues are being investigated.
Full v1, with some follow up discussion, can be found at these links:
https://lists.freedesktop.org/archives/intel-gfx/2022-October/309547.html
https://marc.info/?t=166620059900003&r=1&w=4
https://marc.info/?l=linux-cgroups&m=166620044012970&w=2
v2:
https://lists.freedesktop.org/archives/intel-gfx/2022-November/311844.html
https://marc.info/?l=linux-cgroups&m=166800992215296&w=2
https://marc.info/?l=linux-kernel&m=166800993415302&w=4
*** ^^^ Read this first please ^^^ ***
This series contains a proposal for a DRM scheduling cgroup controller which implements a weight based hierarchical GPU usage budget based controller similar in concept to some of the existing controllers.
Motivation mostly comes from my earlier proposal where I identified that GPU scheduling lags significantly behind what is available for CPU and IO. Whereas back then I was proposing to somehow tie this with process nice, feedback mostly was that people wanted cgroups. So here it is - in the world of heterogenous computing pipelines I think it is time to do something about this gap.
Code is not finished but should survive some light experimenting with. I am sharing it early since the topic has been controversial in the past. I hope to demonstrate there are gains to be had in real world usage(*), today, and that the concepts the proposal relies are well enough established and stable.
*) Specifically under ChromeOS which uses cgroups to control CPU bandwith for
VMs based on the window focused status. It can be demonstrated how GPU scheduling control can easily be integrated into that setup.
There should be no conflict with this proposal and any efforts to implement memory usage based controller. Skeleton DRM cgroup controller is deliberatly purely a skeleton patch where any further functionality can be added with no real conflicts. [In fact, perhaps scheduling is even easier to deal with than memory accounting.]
Structure of the series is as follows:
1-3) Improve client ownership tracking in DRM core.
4) Adds a skeleton DRM cgroup controller with no functionality.
5-10) Laying down some infrastructure to enable the controller.
11) The controller itself.
12-13) i915 support for the controller.
The proposals defines a delegation of duties between the tree parties: cgroup controller, DRM core and individual drivers. Two way communication interfaces are then defined to enable the delegation to work.
DRM scheduling soft limits
~~~~~~~~~~~~~~~~~~~~~~~~~~
Because of the heterogenous hardware and driver DRM capabilities, soft limits are implemented as a loose co-operative (bi-directional) interface between the controller and DRM core.
The controller configures the GPU time allowed per group and periodically scans the belonging tasks to detect the over budget condition, at which point it invokes a callback notifying the DRM core of the condition.
DRM core provides an API to query per process GPU utilization and 2nd API to receive notification from the cgroup controller when the group enters or exits the over budget condition.
Individual DRM drivers which implement the interface are expected to act on this in the best-effort manner only. There are no guarantees that the soft limits will be respected.
DRM scheduling soft limits interface files
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drm.weight
Standard cgroup weight based control [1, 10000] used to configure the relative distributing of GPU time between the sibling groups.
drm.period_us (Most probably only a debugging aid during RFC phase.)
An integer representing the period with which the controller should look at the GPU usage by the group and potentially send the over/under budget signal.
Value of zero (defaul) disables the soft limit checking.
This builds upon the per client GPU utilisation work which landed recently for a few drivers. My thinking is that in principle, an intersect of drivers which support both that and some sort of scheduling control, like priorities, could also in theory support this controller.
Another really interesting angle for this controller is that it mimics the same control menthod used by the CPU scheduler. That is the proportional/weight based GPU time budgeting. Which makes it easy to configure and does not need a new mental model.
However, as the introduction mentions, GPUs are much more heterogenous and therefore the controller uses very "soft" wording as to what it promises. The general statement is that it can define budgets, notify clients when they are over them, and let individual drivers implement best effort handling of those conditions.
Delegation of duties in the implementation goes likes this:
* DRM cgroup controller implements the control files and the scanning loop.
* DRM core is required to track all DRM clients belonging to processes so it can answer when asked how much GPU time is a process using.
* DRM core also provides a call back which the controller will call when a certain process is over budget.
* Individual drivers need to implement two similar hooks, but which work for a single DRM client. Over budget callback and GPU utilisation query.
What I have demonstrated in practice is that when wired to i915, in a really primitive way where the over-budget condition simply lowers the scheduling priority, the concept can be almost equally effective as the static priority control. I say almost because the design where budget control depends on the periodic usage scanning has a fundamental delay, so responsiveness will depend on the scanning period, which may or may not be a problem for a particular use case.
There are also interesting conversations to be had around mental models for what is GPU usage as a single number when faced with GPUs which have different execution engines. To an extent this is similar to the multi-core and cgroup CPU controller problems, but definitely goes further than that.
I deliberately did not want to include any such complications in the controller itself and left the individual drivers to handle it. For instance in the i915 over-budget callback it will not do anything unless client's GPU usage is on a physical engine which is oversubscribed. This enables multiple clients to be harmlessly over budget, as long as they are not competing for the same GPU resource.
This much for now, hope some good discussion will follow.
P.S.
A disclaimer of a kind - I was not familiar with how to implement a cgroup controller at all when I started this prototype therefore it is quite possible there are many bugs and misunderstandings on how it should be done.
v2:
* Prefaced the series with some core DRM work as suggested by Christian.
* Dropped the priority based controller for now.
* Dropped the introspection cgroup controller file.
* Implemented unused budget sharing/propagation.
* Some small fixes/tweak as per review feedback and in general.
Tvrtko Ursulin (13):
drm: Replace DRM_DEBUG with drm_dbg_core in file and ioctl handling
drm: Track clients by tgid and not tid
drm: Update file owner during use
cgroup: Add the DRM cgroup controller
drm/cgroup: Track clients per owning process
drm/cgroup: Allow safe external access to file_priv drm/cgroup: Add ability to query drm cgroup GPU time drm/cgroup: Add over budget signalling callback drm/cgroup: Only track clients which are providing drm_cgroup_ops cgroup/drm: Client exit hook
cgroup/drm: Introduce weight based drm cgroup control drm/i915: Wire up with drm controller GPU time query drm/i915: Implement cgroup controller over budget throttling
Documentation/admin-guide/cgroup-v2.rst | 37 ++ drivers/gpu/drm/Kconfig | 1 + drivers/gpu/drm/Makefile | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 6 +- drivers/gpu/drm/drm_auth.c | 3 +- drivers/gpu/drm/drm_cgroup.c | 192 ++++++ drivers/gpu/drm/drm_debugfs.c | 12 +- drivers/gpu/drm/drm_file.c | 74 ++- drivers/gpu/drm/drm_ioc32.c | 13 +- drivers/gpu/drm/drm_ioctl.c | 28 +- .../gpu/drm/i915/gem/i915_gem_execbuffer.c | 37 +- drivers/gpu/drm/i915/i915_driver.c | 11 + drivers/gpu/drm/i915/i915_drm_client.c | 169 +++++- drivers/gpu/drm/i915/i915_drm_client.h | 13 + drivers/gpu/drm/nouveau/nouveau_drm.c | 5 +- drivers/gpu/drm/vmwgfx/vmwgfx_gem.c | 6 +- include/drm/drm_clients.h | 47 ++ include/drm/drm_drv.h | 36 ++ include/drm/drm_file.h | 17 +- include/linux/cgroup_drm.h | 13 + include/linux/cgroup_subsys.h | 4 + init/Kconfig | 8 + kernel/cgroup/Makefile | 1 + kernel/cgroup/drm.c | 572 ++++++++++++++++++ 24 files changed, 1249 insertions(+), 57 deletions(-) create mode 100644 drivers/gpu/drm/drm_cgroup.c create mode 100644 include/drm/drm_clients.h create mode 100644 include/linux/cgroup_drm.h create mode 100644 kernel/cgroup/drm.c
Signature · 1 line
2.34.1
RFC v2 1/13 drm: Replace DRM_DEBUG with drm_dbg_core in file and ioctl handling
From: Tvrtko Ursulin <[email protected]>
Replace the deprecated macro with the per-device one.
--- drivers/gpu/drm/drm_file.c | 21 +++++++++++---------- drivers/gpu/drm/drm_ioc32.c | 13 +++++++------ drivers/gpu/drm/drm_ioctl.c | 25 +++++++++++++------------ 3 files changed, 31 insertions(+), 28 deletions(-)
drivers/gpu/drm/drm_file.c +11 -10
@@ -245,10 +245,10 @@ void drm_file_free(struct drm_file *file) dev = file->minor->dev; -DRM_DEBUG ("comm=\"%s\", pid=%d, dev=0x%lx, open_count=%d\n" , - current->comm,task_pid_nr (current), - (long )old_encode_dev (file->minor->kdev->devt), -atomic_read (&dev->open_count)); +drm_dbg_core (dev,"comm=\"%s\", pid=%d, dev=0x%lx, open_count=%d\n" , + current->comm,task_pid_nr (current), + (long )old_encode_dev (file->minor->kdev->devt), +atomic_read (&dev->open_count));#ifdef CONFIG_DRM_LEGACY if (drm_core_check_feature(dev, DRIVER_LEGACY) &&
@@ -340,8 +340,8 @@ static int drm_open_helper(struct file *filp, struct drm_minor *minor) dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)return -EINVAL ; -DRM_DEBUG ("comm=\"%s\", pid=%d, minor=%d\n" , current->comm, -task_pid_nr (current), minor->index); +drm_dbg_core (dev,"comm=\"%s\", pid=%d, minor=%d\n" , + current->comm,task_pid_nr (current), minor->index); priv =drm_file_alloc (minor);if (IS_ERR (priv))
@@ -450,11 +450,12 @@ EXPORT_SYMBOL(drm_open);void drm_lastclose (struct drm_device * dev) { -DRM_DEBUG ("\n" ); +drm_dbg_core (dev,"\n" ); -if (dev->driver->lastclose) +if (dev->driver->lastclose) { dev->driver->lastclose (dev); -DRM_DEBUG ("driver lastclose completed\n" ); +drm_dbg_core (dev,"driver lastclose completed\n" ); + }if (drm_core_check_feature (dev,DRIVER_LEGACY ))drm_legacy_dev_reinit (dev);
@@ -485,7 +486,7 @@ int drm_release(struct inode *inode, struct file *filp)if (drm_dev_needs_global_mutex (dev))mutex_lock (&drm_global_mutex); -DRM_DEBUG ("open_count = %d\n" ,atomic_read (&dev->open_count)); +drm_dbg_core (dev,"open_count = %d\n" ,atomic_read (&dev->open_count));drm_close_helper (filp);
drivers/gpu/drm/drm_ioc32.c +7 -6
@@ -972,6 +972,7 @@ long drm_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) {unsigned int nr =DRM_IOCTL_NR (cmd);struct drm_file *file_priv = filp->private_data; +struct drm_device *dev = file_priv->minor->dev;drm_ioctl_compat_t *fn;int ret;
@@ -986,14 +987,14 @@ long drm_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)if (!fn)return drm_ioctl (filp, cmd, arg); -DRM_DEBUG ("comm=\"%s\", pid=%d, dev=0x%lx, auth=%d, %s\n" , - current->comm,task_pid_nr (current), - (long )old_encode_dev (file_priv->minor->kdev->devt), - file_priv->authenticated, - drm_compat_ioctls[nr].name); +drm_dbg_core (dev,"comm=\"%s\", pid=%d, dev=0x%lx, auth=%d, %s\n" , + current->comm,task_pid_nr (current), + (long )old_encode_dev (file_priv->minor->kdev->devt), + file_priv->authenticated, + drm_compat_ioctls[nr].name); ret = (*fn)(filp, cmd, arg);if (ret) -DRM_DEBUG ("ret = %d\n" , ret); +drm_dbg_core (dev,"ret = %d\n" , ret);return ret; }EXPORT_SYMBOL (drm_compat_ioctl);
drivers/gpu/drm/drm_ioctl.c +13 -12
@@ -440,7 +440,7 @@ static int drm_setversion(struct drm_device *dev, void *data, struct drm_file *fint drm_noop (struct drm_device *dev,void *data,struct drm_file *file_priv) { -DRM_DEBUG ("\n" ); +drm_dbg_core (dev,"\n" );return 0 ; }EXPORT_SYMBOL (drm_noop);
@@ -856,16 +856,16 @@ long drm_ioctl(struct file *filp, out_size =0 ; ksize =max (max (in_size, out_size), drv_size); -DRM_DEBUG ("comm=\"%s\" pid=%d, dev=0x%lx, auth=%d, %s\n" , - current->comm,task_pid_nr (current), - (long )old_encode_dev (file_priv->minor->kdev->devt), - file_priv->authenticated, ioctl->name); +drm_dbg_core (dev,"comm=\"%s\" pid=%d, dev=0x%lx, auth=%d, %s\n" , + current->comm,task_pid_nr (current), + (long )old_encode_dev (file_priv->minor->kdev->devt), + file_priv->authenticated, ioctl->name);/* Do not trust userspace, use our own definition */ func = ioctl->func;if (unlikely (!func)) { -DRM_DEBUG ("no function\n" ); +drm_dbg_core (dev,"no function\n" ); retcode = -EINVAL ; goto err_i1; }
@@ -894,16 +894,17 @@ long drm_ioctl(struct file *filp, err_i1:if (!ioctl) -DRM_DEBUG ("invalid ioctl: comm=\"%s\", pid=%d, dev=0x%lx, auth=%d, cmd=0x%02x, nr=0x%02x\n" , - current->comm,task_pid_nr (current), - (long )old_encode_dev (file_priv->minor->kdev->devt), - file_priv->authenticated, cmd, nr); +drm_dbg_core (dev, +"invalid ioctl: comm=\"%s\", pid=%d, dev=0x%lx, auth=%d, cmd=0x%02x, nr=0x%02x\n" , + current->comm,task_pid_nr (current), + (long )old_encode_dev (file_priv->minor->kdev->devt), + file_priv->authenticated, cmd, nr);if (kdata != stack_kdata)kfree (kdata);if (retcode) -DRM_DEBUG ("comm=\"%s\", pid=%d, ret=%d\n" , current->comm, -task_pid_nr (current), retcode); +drm_dbg_core (dev,"comm=\"%s\", pid=%d, ret=%d\n" , + current->comm,task_pid_nr (current), retcode);return retcode; }EXPORT_SYMBOL (drm_ioctl);
Signature · 1 line
2.34.1
RFC v2 2/13 drm: Track clients by tgid and not tid
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Thread group id (aka pid from userspace point of view) is a more interesting thing to show as an owner of a DRM fd, so track and show that instead of the thread id.
In the next patch we will make the owner updated post file descriptor handover, which will also be tgid based to avoid ping-pong when multiple threads access the fd.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 2 +- drivers/gpu/drm/drm_debugfs.c | 4 ++-- drivers/gpu/drm/drm_file.c | 2 +- drivers/gpu/drm/vmwgfx/vmwgfx_gem.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1 -1
@@ -967,7 +967,7 @@ static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)* Therefore, we need to protect this ->comm access using RCU. */ rcu_read_lock (); - task =pid_task (file->pid,PIDTYPE_PID ); + task =pid_task (file->pid,PIDTYPE_TGID );seq_printf (m,"pid %8d command %s:\n" ,pid_nr (file->pid), task ? task->comm :"<unknown>" );rcu_read_unlock ();
drivers/gpu/drm/drm_debugfs.c +2 -2
@@ -80,7 +80,7 @@ static int drm_clients_info(struct seq_file *m, void *data)seq_printf (m,"%20s %5s %3s master a %5s %10s\n" ,"command" , -"pid" , +"tgid" ,"dev" ,"uid" ,"magic" );
@@ -94,7 +94,7 @@ static int drm_clients_info(struct seq_file *m, void *data)bool is_current_master =drm_is_current_master (priv);rcu_read_lock ();/* locks pid_task()->comm */ - task =pid_task (priv->pid,PIDTYPE_PID ); + task =pid_task (priv->pid,PIDTYPE_TGID ); uid = task ?__task_cred (task)->euid :GLOBAL_ROOT_UID ; seq_printf(m, "%20s %5d %3d %c %c %5d %10u\n", task ? task->comm : "<unknown>",
drivers/gpu/drm/drm_file.c +1 -1
@@ -156,7 +156,7 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)if (!file)return ERR_PTR (-ENOMEM ); - file->pid =get_pid (task_pid (current)); + file->pid =get_pid (task_tgid (current)); file->minor = minor;/* for compatibility root is always authenticated */
drivers/gpu/drm/vmwgfx/vmwgfx_gem.c +1 -1
@@ -260,7 +260,7 @@ static int vmw_debugfs_gem_info_show(struct seq_file *m, void *unused)* Therefore, we need to protect this ->comm access using RCU. */ rcu_read_lock (); - task =pid_task (file->pid,PIDTYPE_PID ); + task =pid_task (file->pid,PIDTYPE_TGID );seq_printf (m,"pid %8d command %s:\n" ,pid_nr (file->pid), task ? task->comm :"<unknown>" );rcu_read_unlock ();
Signature · 1 line
2.34.1
Re: [RFC 02/13] drm: Track clients by tgid and not tid
Quoted 86 lines · Tvrtko Ursulin
From: Tvrtko Ursulin <[email protected]>
Thread group id (aka pid from userspace point of view) is a more interesting thing to show as an owner of a DRM fd, so track and show that instead of the thread id.
In the next patch we will make the owner updated post file descriptor handover, which will also be tgid based to avoid ping-pong when multiple threads access the fd.
Signed-off-by Tvrtko Ursulin <[email protected]>Cc Zack Rusin <[email protected]>Cc Alex Deucher <[email protected]>Cc "Christian König" <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 2 +-
drivers/gpu/drm/drm_debugfs.c | 4 ++--
drivers/gpu/drm/drm_file.c | 2 +-
drivers/gpu/drm/vmwgfx/vmwgfx_gem.c | 2 +-4 files changed, 5 insertions(+), 5 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1
-1@@ -967,7 +967,7 @@ static int amdgpu_debugfs_gem_info_show(struct seq_file *m,void *unused) * Therefore, we need to protect this ->comm access using RCU. */ rcu_read_lock(); - task = pid_task(file->pid, PIDTYPE_PID); + task = pid_task(file->pid, PIDTYPE_TGID); seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid), task ? task->comm : "<unknown>");rcu_read_unlock();
drivers/gpu/drm/drm_debugfs.c +1
-1@@ -80,7 +80,7 @@ static int drm_clients_info(struct seq_file *m, void *data) seq_printf(m, "%20s %5s %3s master a %5s %10s\n", "command", - "pid", + "tgid", "dev", "uid", "magic");@@ -94,7 +94,7 @@ static int drm_clients_info(struct seq_file *m, void *data) bool is_current_master = drm_is_current_master(priv);
rcu_read_lock(); /* locks pid_task()->comm */ - task =pid_task(priv->pid, PIDTYPE_PID);
+ task =pid_task(priv->pid, PIDTYPE_TGID); uid = task ? __task_cred(task)->euid :GLOBAL_ROOT_UID; seq_printf(m, "%20s %5d %3d %c %c %5d %10u\n", task ?task->comm: "<unknown>",drivers/gpu/drm/drm_file.c +0
-0@@ -156,7 +156,7 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor) if (!file) return ERR_PTR(-ENOMEM);
- file->pid = get_pid(task_pid(current));
+ file->pid = get_pid(task_tgid(current));file->minor= minor;
/* for compatibility root is always authenticated */drivers/gpu/drm/vmwgfx/vmwgfx_gem.c +1
-1@@ -260,7 +260,7 @@ static int vmw_debugfs_gem_info_show(struct seq_file *m, void*unused) * Therefore, we need to protect this ->comm access using RCU. */ rcu_read_lock(); - task = pid_task(file->pid, PIDTYPE_PID); + task = pid_task(file->pid, PIDTYPE_TGID); seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid), task ? task->comm : "<unknown>");
Quoted 1 line
rcu_read_unlock();
Yea, that probably makes a lot more sense. Looks good.
RFC v2 3/13 drm: Update file owner during use
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
With the typical model where the display server opends the file descriptor and then hands it over to the client we were showing stale data in debugfs.
Fix it by updating the drm_file->pid on ioctl access from a different process.
The field is also made RCU protected to allow for lockless readers. Update side is protected with dev->filelist_mutex.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 6 +++-- drivers/gpu/drm/drm_auth.c | 3 ++- drivers/gpu/drm/drm_debugfs.c | 10 +++++---- drivers/gpu/drm/drm_file.c | 30 +++++++++++++++++++++++++ drivers/gpu/drm/drm_ioctl.c | 3 +++ drivers/gpu/drm/nouveau/nouveau_drm.c | 5 ++++- drivers/gpu/drm/vmwgfx/vmwgfx_gem.c | 6 +++-- include/drm/drm_file.h | 13 +++++++++-- 8 files changed, 64 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +4 -2
@@ -958,6 +958,7 @@ static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)list_for_each_entry (file, &dev->filelist, lhead) {struct task_struct *task;struct drm_gem_object *gobj; +struct pid *pid;int id;/*
@@ -967,8 +968,9 @@ static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)* Therefore, we need to protect this ->comm access using RCU. */ rcu_read_lock (); - task =pid_task (file->pid,PIDTYPE_TGID ); -seq_printf (m,"pid %8d command %s:\n" ,pid_nr (file->pid), + pid =rcu_dereference (file->pid); + task =pid_task (pid,PIDTYPE_TGID ); +seq_printf (m,"pid %8d command %s:\n" ,pid_nr (pid), task ? task->comm :"<unknown>" );rcu_read_unlock ();
drivers/gpu/drm/drm_auth.c +2 -1
@@ -235,7 +235,8 @@ static int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)static int drm_master_check_perm (struct drm_device *dev,struct drm_file *file_priv) { -if (file_priv->pid ==task_pid (current) && file_priv->was_master) +if (file_priv->was_master && +rcu_access_pointer (file_priv->pid) ==task_pid (current))return 0 ;if (!capable (CAP_SYS_ADMIN ))
drivers/gpu/drm/drm_debugfs.c +6 -4
@@ -90,15 +90,17 @@ static int drm_clients_info(struct seq_file *m, void *data)*/ mutex_lock (&dev->filelist_mutex);list_for_each_entry_reverse (priv, &dev->filelist, lhead) { -struct task_struct *task;bool is_current_master =drm_is_current_master (priv); +struct task_struct *task; +struct pid *pid; -rcu_read_lock ();/* locks pid_task()->comm */ - task =pid_task (priv->pid,PIDTYPE_TGID ); +rcu_read_lock ();/* Locks priv->pid and pid_task()->comm! */ + pid =rcu_dereference (priv->pid); + task =pid_task (pid,PIDTYPE_PID ); uid = task ?__task_cred (task)->euid :GLOBAL_ROOT_UID ; seq_printf(m, "%20s %5d %3d %c %c %5d %10u\n", task ? task->comm : "<unknown>", - pid_vnr(priv->pid), + pid_vnr(pid), priv->minor->index, is_current_master ? 'y' : 'n', priv->authenticated ? 'y' : 'n',
drivers/gpu/drm/drm_file.c +30 -0
@@ -502,6 +502,36 @@ int drm_release(struct inode *inode, struct file *filp) }EXPORT_SYMBOL (drm_release); +void drm_file_update_pid (struct drm_file *filp) +{ +struct drm_device *dev; +struct pid *pid, *old; + +/* Master nodes are not expected to be passed between processes. */ +if (filp->was_master) +return ; + + pid =task_tgid (current); + +/* +* Quick unlocked check since the model is a single handover followed by +* exclusive repeated use. +*/ +if (pid ==rcu_access_pointer (filp->pid)) +return ; + + dev = filp->minor->dev; +mutex_lock (&dev->filelist_mutex); + old =rcu_replace_pointer (filp->pid, pid,1 ); +mutex_unlock (&dev->filelist_mutex); + +if (pid != old) { +get_pid (pid); +synchronize_rcu (); +put_pid (old); + } +} +/** * drm_release_noglobal - release method for DRM file * @inode: device inode
drivers/gpu/drm/drm_ioctl.c +3 -0
@@ -775,6 +775,9 @@ long drm_ioctl_kernel(struct file *file, drm_ioctl_t *func, void *kdata,struct drm_device *dev = file_priv->minor->dev;int retcode; +/* Update drm_file owner if fd was passed along. */ +drm_file_update_pid (file_priv); +if (drm_dev_is_unplugged (dev))return -ENODEV ;
drivers/gpu/drm/nouveau/nouveau_drm.c +4 -1
@@ -1111,7 +1111,10 @@ nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv) }get_task_comm (tmpname, current); -snprintf (name,sizeof (name),"%s[%d]" , tmpname,pid_nr (fpriv->pid)); +rcu_read_lock (); +snprintf (name,sizeof (name),"%s[%d]" , + tmpname,pid_nr (rcu_dereference (fpriv->pid))); +rcu_read_unlock ();if (!(cli =kzalloc (sizeof (*cli),GFP_KERNEL ))) { ret = -ENOMEM ;
drivers/gpu/drm/vmwgfx/vmwgfx_gem.c +4 -2
@@ -251,6 +251,7 @@ static int vmw_debugfs_gem_info_show(struct seq_file *m, void *unused)list_for_each_entry (file, &dev->filelist, lhead) {struct task_struct *task;struct drm_gem_object *gobj; +struct pid *pid;int id;/*
@@ -260,8 +261,9 @@ static int vmw_debugfs_gem_info_show(struct seq_file *m, void *unused)* Therefore, we need to protect this ->comm access using RCU. */ rcu_read_lock (); - task =pid_task (file->pid,PIDTYPE_TGID ); -seq_printf (m,"pid %8d command %s:\n" ,pid_nr (file->pid), + pid =rcu_dereference (file->pid); + task =pid_task (pid,PIDTYPE_TGID ); +seq_printf (m,"pid %8d command %s:\n" ,pid_nr (pid), task ? task->comm :"<unknown>" );rcu_read_unlock ();
include/drm/drm_file.h +11 -2
@@ -251,8 +251,15 @@ struct drm_file {/** @master_lookup_lock: Serializes @master. */ spinlock_t master_lookup_lock; -/** @pid: Process that opened this file. */ -struct pid *pid; +/** +* @pid: Process that is using this file. +* +* Must only be dereferenced under a rcu_read_lock or equivalent. +* +* Updates are guarded with dev->filelist_mutex and reference must be +* dropped after a RCU grace period to accommodate lockless readers. +*/ +struct pid __rcu *pid;/** @magic: Authentication magic, see @authenticated. */ drm_magic_t magic;
@@ -397,6 +404,8 @@ static inline bool drm_is_render_client(const struct drm_file *file_priv)return file_priv->minor->type ==DRM_MINOR_RENDER ; } +void drm_file_update_pid (struct drm_file *); +int drm_open (struct inode *inode,struct file *filp);ssize_t drm_read (struct file *filp,char __user *buffer,size_t count,loff_t *offset);
Signature · 1 line
2.34.1
RFC v2 4/13 cgroup: Add the DRM cgroup controller
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Skeleton controller without any functionality.
--- include/linux/cgroup_drm.h | 9 ++++++ include/linux/cgroup_subsys.h | 4 +++ init/Kconfig | 7 +++++ kernel/cgroup/Makefile | 1 + kernel/cgroup/drm.c | 54 +++++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 include/linux/cgroup_drm.h create mode 100644 kernel/cgroup/drm.c
include/linux/cgroup_drm.h new file +9 -0
@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: MIT */ +/* +* Copyright © 2022 Intel Corporation +*/ + +#ifndef _CGROUP_DRM_H +#define _CGROUP_DRM_H + +#endif /* _CGROUP_DRM_H */
include/linux/cgroup_subsys.h +4 -0
@@ -65,6 +65,10 @@ SUBSYS(rdma) SUBSYS(misc)#endif +#if IS_ENABLED (CONFIG_CGROUP_DRM ) +SUBSYS(drm) +#endif +/* * The following subsystems are not supported on the default hierarchy. */
init/Kconfig +7 -0
@@ -1087,6 +1087,13 @@ config CGROUP_RDMA Attaching processes with active RDMA resources to the cgroup hierarchy is allowed even if can cross the hierarchy's limit. +config CGROUP_DRM + bool "DRM controller" + help + Provides the DRM subsystem controller. + + ... + config CGROUP_FREEZER bool "Freezer controller" help
kernel/cgroup/Makefile +1 -0
@@ -6,4 +6,5 @@ obj-$(CONFIG_CGROUP_PIDS) += pids.o obj-$(CONFIG_CGROUP_RDMA) += rdma.o obj-$(CONFIG_CPUSETS) += cpuset.o obj-$(CONFIG_CGROUP_MISC) += misc.o +obj-$(CONFIG_CGROUP_DRM) += drm.o obj-$(CONFIG_CGROUP_DEBUG) += debug.o
kernel/cgroup/drm.c new file +54 -0
54 lines not on this page · raw message
Signature · 1 line
2.34.1
RFC v2 5/13 drm/cgroup: Track clients per owning process
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
To enable propagation of settings from the cgroup drm controller to drm we need to start tracking which processes own which drm clients.
Implement that by tracking the struct pid pointer of the owning process in a new XArray, pointing to a structure containing a list of associated struct drm_file pointers.
Clients are added and removed under the filelist mutex and RCU list operations are used below it to allow for lockless lookup.
--- drivers/gpu/drm/Makefile | 1 + drivers/gpu/drm/drm_cgroup.c | 123 +++++++++++++++++++++++++++++++++++ drivers/gpu/drm/drm_file.c | 21 ++++-- include/drm/drm_clients.h | 44 +++++++++++++ include/drm/drm_file.h | 4 ++ 5 files changed, 189 insertions(+), 4 deletions(-) create mode 100644 drivers/gpu/drm/drm_cgroup.c create mode 100644 include/drm/drm_clients.h
drivers/gpu/drm/Makefile +1 -0
@@ -59,6 +59,7 @@ drm-$(CONFIG_DRM_LEGACY) += \ drm_scatter.o \ drm_vm.o drm-$(CONFIG_DRM_LIB_RANDOM) += lib/drm_random.o +drm-$(CONFIG_CGROUP_DRM) += drm_cgroup.o drm-$(CONFIG_COMPAT) += drm_ioc32.o drm-$(CONFIG_DRM_PANEL) += drm_panel.o drm-$(CONFIG_OF) += drm_of.o
drivers/gpu/drm/drm_cgroup.c new file +123 -0
123 lines not on this page · raw message
drivers/gpu/drm/drm_file.c +17 -4
@@ -40,6 +40,7 @@#include <linux/slab.h> #include <drm/drm_client.h> +#include <drm/drm_clients.h> #include <drm/drm_drv.h> #include <drm/drm_file.h> #include <drm/drm_print.h>
@@ -298,6 +299,7 @@ static void drm_close_helper(struct file *filp)mutex_lock (&dev->filelist_mutex);list_del (&file_priv->lhead); +drm_clients_close (file_priv);mutex_unlock (&dev->filelist_mutex);drm_file_free (file_priv);
@@ -349,10 +351,8 @@ static int drm_open_helper(struct file *filp, struct drm_minor *minor)if (drm_is_primary_client (priv)) { ret =drm_master_open (priv); -if (ret) { -drm_file_free (priv); -return ret; - } +if (ret) + goto err_free; } filp->private_data = priv;
@@ -360,6 +360,9 @@ static int drm_open_helper(struct file *filp, struct drm_minor *minor) priv->filp = filp;mutex_lock (&dev->filelist_mutex); + ret =drm_clients_open (priv); +if (ret) + goto err_unlock;list_add (&priv->lhead, &dev->filelist);mutex_unlock (&dev->filelist_mutex);
@@ -387,6 +390,13 @@ static int drm_open_helper(struct file *filp, struct drm_minor *minor)#endif return 0 ; + +err_unlock: +mutex_unlock (&dev->filelist_mutex); +err_free: +drm_file_free (priv); + +return ret; }/**
@@ -523,6 +533,9 @@ void drm_file_update_pid(struct drm_file *filp) dev = filp->minor->dev;mutex_lock (&dev->filelist_mutex); old =rcu_replace_pointer (filp->pid, pid,1 ); +if (pid != old) +drm_clients_migrate (filp, + (unsigned long )old, (unsigned long )pid);mutex_unlock (&dev->filelist_mutex);if (pid != old) {
include/drm/drm_clients.h new file +44 -0
44 lines not on this page · raw message
include/drm/drm_file.h +4 -0
@@ -275,6 +275,10 @@ struct drm_file {/** @minor: &struct drm_minor for this file. */ struct drm_minor *minor; +#if IS_ENABLED (CONFIG_CGROUP_DRM ) +struct list_head clink; +#endif +/** * @object_idr: *
Signature · 1 line
2.34.1
RFC v2 6/13 drm/cgroup: Allow safe external access to file_priv
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Entry points from the cgroup subsystem into the drm cgroup controller will need to walk the file_priv structures associated with registered clients and since those are not RCU protected lets add a hack for now to make this safe.
--- drivers/gpu/drm/drm_cgroup.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/gpu/drm/drm_cgroup.c +7 -0
@@ -17,6 +17,13 @@ __del_clients(struct drm_pid_clients *clients,if (atomic_dec_and_test (&clients->num)) {xa_erase (&drm_pid_clients, pid);kfree_rcu (clients, rcu); + +/* +* FIXME: file_priv is not RCU protected so we add this hack +* to avoid any races with code which walks clients->file_list +* and accesses file_priv. +*/ +synchronize_rcu (); } }
Signature · 1 line
2.34.1
RFC v2 7/13 drm/cgroup: Add ability to query drm cgroup GPU time
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Add a driver callback and core helper which allow querying the time spent on GPUs for processes belonging to a group.
--- drivers/gpu/drm/drm_cgroup.c | 24 ++++++++++++++++++++++++ include/drm/drm_clients.h | 2 ++ include/drm/drm_drv.h | 28 ++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+)
drivers/gpu/drm/drm_cgroup.c +24 -0
@@ -128,3 +128,27 @@ drm_clients_migrate(struct drm_file *file_priv,atomic_inc (&clients->num);list_add_tail_rcu (&file_priv->clink, &clients->file_list); } + +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);
include/drm/drm_clients.h +2 -0
@@ -41,4 +41,6 @@ drm_clients_migrate(struct drm_file *file_priv, }#endif +u64 drm_pid_get_active_time_us (struct pid *pid); +#endif
include/drm/drm_drv.h +28 -0
@@ -148,6 +148,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 *
@@ -459,6 +477,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: */
Signature · 1 line
2.34.1
RFC v2 8/13 drm/cgroup: Add over budget signalling callback
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Add a new callback via which the drm cgroup controller is notifying the drm core that a certain process is above its allotted GPU time.
--- drivers/gpu/drm/drm_cgroup.c | 21 +++++++++++++++++++++ include/drm/drm_clients.h | 1 + include/drm/drm_drv.h | 8 ++++++++ 3 files changed, 30 insertions(+)
drivers/gpu/drm/drm_cgroup.c +21 -0
@@ -152,3 +152,24 @@ u64 drm_pid_get_active_time_us(struct pid *pid)return total; }EXPORT_SYMBOL_GPL (drm_pid_get_active_time_us); + +void drm_pid_signal_budget (struct pid *pid,u64 usage,u64 budget) +{ +struct drm_pid_clients *clients; + +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->signal_budget) + cg_ops->signal_budget (fpriv, usage, budget); + } + } +rcu_read_unlock (); +} +EXPORT_SYMBOL_GPL (drm_pid_signal_budget);
include/drm/drm_clients.h +1 -0
@@ -42,5 +42,6 @@ drm_clients_migrate(struct drm_file *file_priv,#endif u64 drm_pid_get_active_time_us (struct pid *pid); +void drm_pid_signal_budget (struct pid *pid,u64 usage,u64 budget);#endif
include/drm/drm_drv.h +8 -0
@@ -164,6 +164,14 @@ struct drm_cgroup_ops {* Used by the DRM core when queried by the DRM cgroup controller. */ u64 (*active_time_us) (struct drm_file *); + +/** +* @signal_budget: +* +* Optional callback used by the DRM core to forward over/under GPU time +* messages sent by the DRM cgroup controller. +*/ +int (*signal_budget) (struct drm_file *,u64 used,u64 budget); };/**
Signature · 1 line
2.34.1
RFC v2 9/13 drm/cgroup: Only track clients which are providing drm_cgroup_ops
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
To reduce the number of tracking going on, especially with drivers which will not support any sort of control from the drm cgroup controller side, lets express the funcionality as opt-in and use the presence of drm_cgroup_ops as activation criteria.
--- drivers/gpu/drm/drm_cgroup.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/gpu/drm/drm_cgroup.c +9 -0
@@ -35,6 +35,9 @@ void drm_clients_close(struct drm_file *file_priv)lockdep_assert_held (&dev->filelist_mutex); +if (!dev->driver->cg_ops) +return ; + pid =rcu_access_pointer (file_priv->pid); clients =xa_load (&drm_pid_clients, (unsigned long )pid);if (WARN_ON_ONCE (!clients))
@@ -66,6 +69,9 @@ int drm_clients_open(struct drm_file *file_priv)lockdep_assert_held (&dev->filelist_mutex); +if (!dev->driver->cg_ops) +return 0 ; + pid = (unsigned long )rcu_access_pointer (file_priv->pid); clients =xa_load (&drm_pid_clients, pid);if (!clients) {
@@ -101,6 +107,9 @@ drm_clients_migrate(struct drm_file *file_priv,lockdep_assert_held (&dev->filelist_mutex); +if (!dev->driver->cg_ops) +return ; + existing_clients =xa_load (&drm_pid_clients, new); clients =xa_load (&drm_pid_clients, old);
Signature · 1 line
2.34.1
RFC v2 10/13 cgroup/drm: Client exit hook
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
We need the ability for DRM core to inform the cgroup controller when a client has closed a DRM file descriptor. This will allow us not needing to keep state relating to GPU time usage by tasks sets in the cgroup controller itself.
--- drivers/gpu/drm/drm_cgroup.c | 8 ++++++++ include/linux/cgroup_drm.h | 4 ++++ kernel/cgroup/drm.c | 13 +++++++++++++ 3 files changed, 25 insertions(+)
drivers/gpu/drm/drm_cgroup.c +8 -0
@@ -5,6 +5,7 @@#include <drm/drm_drv.h> #include <drm/drm_clients.h> +#include <linux/cgroup_drm.h> static DEFINE_XARRAY (drm_pid_clients);
@@ -31,6 +32,7 @@ void drm_clients_close(struct drm_file *file_priv) {struct drm_device *dev = file_priv->minor->dev;struct drm_pid_clients *clients; +struct task_struct *task;struct pid *pid;lockdep_assert_held (&dev->filelist_mutex);
@@ -43,6 +45,12 @@ void drm_clients_close(struct drm_file *file_priv)if (WARN_ON_ONCE (!clients))return ; + task =get_pid_task (pid,PIDTYPE_PID ); +if (task) { +drmcgroup_client_exited (task); +put_task_struct (task); + } +__del_clients (clients, file_priv, (unsigned long )pid); }
include/linux/cgroup_drm.h +4 -0
@@ -6,4 +6,8 @@#ifndef _CGROUP_DRM_H#define _CGROUP_DRM_H +struct task_struct ; + +void drmcgroup_client_exited (struct task_struct *task); +#endif /* _CGROUP_DRM_H */
kernel/cgroup/drm.c +13 -0
@@ -18,11 +18,24 @@ css_to_drmcs(struct cgroup_subsys_state *css) return container_of(css, struct drm_cgroup_state, css); } +static inline struct drm_cgroup_state *get_task_drmcs (struct task_struct *task) +{ +return css_to_drmcs (task_get_css (task, drm_cgrp_id)); +} +static void drmcs_free (struct cgroup_subsys_state *css) {kfree (css_to_drmcs (css)); } +void drmcgroup_client_exited (struct task_struct *task) +{ +struct drm_cgroup_state *drmcs =get_task_drmcs (task); + +css_put (&drmcs->css); +} +EXPORT_SYMBOL_GPL (drmcgroup_client_exited); +static struct drm_cgroup_state root_drmcs = { };
Signature · 1 line
2.34.1
RFC v2 11/13 cgroup/drm: Introduce weight based drm cgroup control
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Similar to CPU scheduling, implement a concept of weight in the drm cgroup controller.
Uses the same range and default as the CPU controller - CGROUP_WEIGHT_MIN, CGROUP_WEIGHT_DFL and CGROUP_WEIGHT_MAX.
Later each cgroup is assigned a time budget proportionaly based on the relative weights of it's siblings. This time budget is in turn split by the group's children and so on.
Children of the root cgroup will be exempt from split budgets and therefore compete for the GPU time independently and without weight based control.
This will be used to implement a soft, or best effort signal from drm cgroup to drm core notifying about groups which are over their allotted budget.
No guarantees that the limit can be enforced are provided or implied.
--- Documentation/admin-guide/cgroup-v2.rst | 37 ++ drivers/gpu/drm/Kconfig | 1 + init/Kconfig | 1 + kernel/cgroup/drm.c | 507 +++++++++++++++++++++++- 4 files changed, 545 insertions(+), 1 deletion(-)
Documentation/admin-guide/cgroup-v2.rst +37 -0
@@ -2398,6 +2398,43 @@ HugeTLB Interface Files hugetlb pages of <hugepagesize> in this cgroup. Only active in use hugetlb pages are included. The per-node values are in bytes. +DRM +--- + +The DRM controller allows configuring scheduling soft limits. + +DRM scheduling soft limits +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Because of the heterogenous hardware and driver DRM capabilities, soft limits +are implemented as a loose co-operative (bi-directional) interface between the +controller and DRM core. + +The controller configures the GPU time allowed per group and periodically scans +the belonging tasks to detect the over budget condition, at which point it +invokes a callback notifying the DRM core of the condition. + +DRM core provides an API to query per process GPU utilization and 2nd API to +receive notification from the cgroup controller when the group enters or exits +the over budget condition. + +Individual DRM drivers which implement the interface are expected to act on this +in the best-effort manner only. There are no guarantees that the soft limits +will be respected. + +DRM scheduling soft limits interface files +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + drm.weight + Standard cgroup weight based control [1, 10000] used to configure the + relative distributing of GPU time between the sibling groups. + + drm.period_us (debugging aid during RFC only) + An integer representing the period with which the controller should look + at the GPU usage by the group and potentially send the over/under budget + signal. + Value of zero (defaul) disables the soft limit checking. + Misc ----
drivers/gpu/drm/Kconfig +1 -0
@@ -7,6 +7,7 @@ # menuconfig DRM tristate "Direct Rendering Manager (XFree86 4.1.0 and higher DRI support)" + default y if CGROUP_DRM=y depends on (AGP || AGP=n) && !EMULATED_CMPXCHG && HAS_DMA select DRM_NOMODESET select DRM_PANEL_ORIENTATION_QUIRKS
init/Kconfig +1 -0
@@ -1089,6 +1089,7 @@ config CGROUP_RDMA config CGROUP_DRM bool "DRM controller" + select DRM help Provides the DRM subsystem controller.
kernel/cgroup/drm.c +506 -1
552 lines not on this page · raw message
Signature · 1 line
2.34.1
RFC v2 12/13 drm/i915: Wire up with drm controller GPU time query
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Implement the drm_cgroup_ops->active_time_us callback.
--- drivers/gpu/drm/i915/i915_driver.c | 10 ++++ drivers/gpu/drm/i915/i915_drm_client.c | 76 ++++++++++++++++++++++---- drivers/gpu/drm/i915/i915_drm_client.h | 2 + 3 files changed, 78 insertions(+), 10 deletions(-)
drivers/gpu/drm/i915/i915_driver.c +10 -0
@@ -1894,6 +1894,12 @@ static const struct drm_ioctl_desc i915_ioctls[] = { DRM_IOCTL_DEF_DRV(I915_GEM_VM_DESTROY, i915_gem_vm_destroy_ioctl, DRM_RENDER_ALLOW), }; +#ifdef CONFIG_CGROUP_DRM +static const struct drm_cgroup_ops i915_drm_cgroup_ops = { + .active_time_us = i915_drm_cgroup_get_active_time_us, +}; +#endif +/* * Interface history: *
@@ -1922,6 +1928,10 @@ static const struct drm_driver i915_drm_driver = { .lastclose = i915_driver_lastclose, .postclose = i915_driver_postclose, +#ifdef CONFIG_CGROUP_DRM + .cg_ops = &i915_drm_cgroup_ops, +#endif + .prime_handle_to_fd = drm_gem_prime_handle_to_fd, .prime_fd_to_handle = drm_gem_prime_fd_to_handle, .gem_prime_import = i915_gem_prime_import,
drivers/gpu/drm/i915/i915_drm_client.c +66 -10
95 lines not on this page · raw message
drivers/gpu/drm/i915/i915_drm_client.h +2 -0
@@ -65,4 +65,6 @@ void i915_drm_client_fdinfo(struct seq_file *m, struct file *f);void i915_drm_clients_fini (struct i915_drm_clients *clients); +u64 i915_drm_cgroup_get_active_time_us (struct drm_file *file); +#endif /* !__I915_DRM_CLIENT_H__ */
Signature · 1 line
2.34.1
RFC v2 13/13 drm/i915: Implement cgroup controller over budget throttling
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
When notified by the drm core we are over our allotted time budget, i915 instance will check if any of the GPU engines it is reponsible for is fully saturated. If it is, and the client in question is using that engine, it will throttle it.
For now throttling is done simplistically by lowering the scheduling priority while client is throttled.
--- .../gpu/drm/i915/gem/i915_gem_execbuffer.c | 37 +++++++- drivers/gpu/drm/i915/i915_driver.c | 1 + drivers/gpu/drm/i915/i915_drm_client.c | 93 +++++++++++++++++++ drivers/gpu/drm/i915/i915_drm_client.h | 11 +++ 4 files changed, 141 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +36 -1
@@ -6,6 +6,7 @@#include <linux/dma-resv.h> #include <linux/highmem.h> +#include <linux/prandom.h> #include <linux/sync_file.h> #include <linux/uaccess.h>
@@ -3015,6 +3016,40 @@ static void retire_requests(struct intel_timeline *tl, struct i915_request *end)break ; } +#ifdef CONFIG_CGROUP_DRM +static unsigned int +__get_class (struct drm_i915_file_private *fpriv,const struct i915_request *rq) +{ +unsigned int class; + + class = rq->context->engine->uabi_class; + +if (WARN_ON_ONCE (class >=ARRAY_SIZE (fpriv->client->throttle))) + class =0 ; + +return class; +} + +static void copy_priority (struct i915_sched_attr *attr, +const struct i915_execbuffer *eb, +const struct i915_request *rq) +{ +struct drm_i915_file_private *file_priv = eb->file->driver_priv; + + *attr = eb->gem_context->sched; +if (file_priv->client->throttle[__get_class (file_priv, rq)]) + attr->priority -= +1 +prandom_u32_max (-I915_CONTEXT_MIN_USER_PRIORITY /2 ); +} +#else +static void copy_priority (struct i915_sched_attr *attr, +const struct i915_execbuffer *eb, +const struct i915_request *rq) +{ + *attr = eb->gem_context->sched; +} +#endif +static int eb_request_add (struct i915_execbuffer *eb,struct i915_request *rq,int err,bool last_parallel) {
@@ -3031,7 +3066,7 @@ static int eb_request_add(struct i915_execbuffer *eb, struct i915_request *rq,/* Check that the context wasn't destroyed before submission */ if (likely (!intel_context_is_closed (eb->context))) { - attr = eb->gem_context->sched; +copy_priority (&attr, eb, rq); }else {/* Serialise with context_close via the add_to_timeline */ i915_request_set_error_once (rq, -ENOENT );
drivers/gpu/drm/i915/i915_driver.c +1 -0
@@ -1897,6 +1897,7 @@ static const struct drm_ioctl_desc i915_ioctls[] = {#ifdef CONFIG_CGROUP_DRM static const struct drm_cgroup_ops i915_drm_cgroup_ops = { .active_time_us = i915_drm_cgroup_get_active_time_us, + .signal_budget = i915_drm_cgroup_signal_budget, };#endif
drivers/gpu/drm/i915/i915_drm_client.c +93 -0
105 lines not on this page · raw message
drivers/gpu/drm/i915/i915_drm_client.h +11 -0
@@ -15,6 +15,8 @@#define I915_LAST_UABI_ENGINE_CLASS I915_ENGINE_CLASS_COMPUTE +struct drm_file ; +struct drm_i915_private ;struct i915_drm_clients {
@@ -38,6 +40,13 @@ struct i915_drm_client {* @past_runtime: Accumulation of pphwsp runtimes from closed contexts. */ atomic64_t past_runtime[I915_LAST_UABI_ENGINE_CLASS +1 ]; + +#ifdef CONFIG_CGROUP_DRM +bool throttle[I915_LAST_UABI_ENGINE_CLASS +1 ]; +unsigned int over_budget; +u64 last; +u64 class_last[I915_LAST_UABI_ENGINE_CLASS +1 ]; +#endif }; void i915_drm_clients_init(struct i915_drm_clients *clients,
@@ -66,5 +75,7 @@ void i915_drm_client_fdinfo(struct seq_file *m, struct file *f);void i915_drm_clients_fini (struct i915_drm_clients *clients);u64 i915_drm_cgroup_get_active_time_us (struct drm_file *file); +int i915_drm_cgroup_signal_budget (struct drm_file *file, +u64 usage,u64 budget);#endif /* !__I915_DRM_CLIENT_H__ */
Signature · 1 line
2.34.1