DRM scheduling cgroup controller
Subsystems (8)
v4 cover DRM scheduling cgroup controller
From: Tvrtko Ursulin <[email protected]>
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.
*) Another real world example later in the cover letter.
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-2) Improve client ownership tracking in DRM core.
3) Adds a skeleton DRM cgroup controller with no functionality.
4-7) Laying down some infrastructure to enable the controller.
8) The controller itself.
9-10) 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.
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, the scanning loop and tracks the DRM clients associated with each cgroup. It provides API DRM core needs to call to (de)register and migrate clients.
* DRM core defines two call-backs which the core calls directly: First for querying GPU time by a client and second for notifying the client that it is over budget. It calls controller API for (de)registering clients and migrating then between tasks on file descriptor hand over.
* Individual drivers implement the above mentiopned callbacks and register them with the DRM core.
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.
Example usage from within a Linux desktop
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Standard Linux distributions like Ubuntu already uses cgroups heavily for session management and that could easily be extended with the DRM controller.
After logging into the system graphically we can enable the DRM controller throughout the cgroups hierarchy:
echo +drm > /sys/fs/cgroup/cgroup.subtree_control
echo +drm > /sys/fs/cgroup/user.slice/cgroup.subtree_control
echo +drm > /sys/fs/cgroup/user.slice/user-$(id -u).slice/cgroup.subtree_control
Next we will open two SSH sessions, just so separate cgroups are handily created by systemd for this experiment.
Roughly simultaneously we run the following two benchmarks in each session respectively:
1)
./GpuTest /test=pixmark_julia_fp32 /width=1920 /height=1080 /fullscreen /no_scorebox /benchmark /benchmark_duration_ms=60000
2)
vblank_mode=0 bin/testfw_app --gl_api=desktop_core --width=1920 --height=1080 --fullscreen 1 --gfx=glfw -t gl_manhattan
(The only reason for vsync off here is because I struggled to find an easily runnable and demanding enough benchmark, or to run on a screen large enough to make even a simpler ones demanding.)
With this test we get 252fps from GpuTest and 96fps from GfxBenchmark.
Premise here is that one of these GPU intensive benchmarks is intended to be ran by the user with lower priority. Imagine kicking off some background compute processing and continuing to use the UI for other tasks. Hence the user will now re-run the test by first lowering the weight control of the first session (DRM cgroup):
1)
echo 50 | sudo tee /sys/fs/cgroup/cut -d':' -f3 /proc/self/cgroup/drm.weight
./GpuTest /test=pixmark_julia_fp32 /width=1920 /height=1080 /fullscreen /no_scorebox /benchmark /benchmark_duration_ms=60000
2)
vblank_mode=0 bin/testfw_app --gl_api=desktop_core --width=1920 --height=1080 --fullscreen 1 --gfx=glfw -t gl_manhattan
In this case we will see that GpuTest has recorded 208fps (~18% down) and GfxBenchmark 114fps (18% up), demonstrating that even a very simple approach of wiring up i915 to the DRM cgroup controller can enable external GPU scheduling control.
* Note here that default weight is 100, so setting 50 for the background session is asking the controller to give it half as much GPU bandwidth.
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.
v3:
* Dropped one upstreamed patch.
* Logging cleanup (use DRM macros where available).
v4:
* Dropped the struct pid tracking indirection in favour of tracking individual DRM clients directly in the controller. (Michal Koutný)
* Added boot time param for configuring the scanning period. (Tejun Heo)
* Improved spreading of unused budget to over budget clients, regardless of their location in the tree so that all unused budget can be utilized.
* Made scanning more robust by not re-starting it on every client de- registration and removal. Instead new baseline GPU activity data is simply collected on those events and next scan invocation can proceed as scheduled.
* Dropped the debugging aids from the series.
TODOs/Opens:
* For now (RFC) I haven't implemented the 2nd suggestion from Tejun of having a shadow tree which would only contain groups with DRM clients. (Purpose being less nodes to traverse in the scanning loop.)
* Is the global state passing from can_attach to attach really okay? (I need source and destination css.)
Tvrtko Ursulin (10):
drm: Track clients by tgid and not tid
drm: Update file owner during use
cgroup: Add the DRM cgroup controller
drm/cgroup: Track DRM clients per cgroup
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: 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 | 31 + drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 6 +- drivers/gpu/drm/drm_auth.c | 3 +- drivers/gpu/drm/drm_debugfs.c | 12 +- drivers/gpu/drm/drm_file.c | 46 +- drivers/gpu/drm/drm_ioctl.c | 3 + .../gpu/drm/i915/gem/i915_gem_execbuffer.c | 38 +- drivers/gpu/drm/i915/i915_driver.c | 11 + drivers/gpu/drm/i915/i915_drm_client.c | 209 ++++++- 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_drv.h | 36 ++ include/drm/drm_file.h | 19 +- include/linux/cgroup_drm.h | 29 + include/linux/cgroup_subsys.h | 4 + init/Kconfig | 7 + kernel/cgroup/Makefile | 1 + kernel/cgroup/drm.c | 568 ++++++++++++++++++ 19 files changed, 1020 insertions(+), 27 deletions(-) create mode 100644 include/linux/cgroup_drm.h create mode 100644 kernel/cgroup/drm.c
Signature · 1 line
2.37.2
RFC v4 1/10 drm: Track clients by tgid and not tid
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.
--- 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
@@ -969,7 +969,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
@@ -241,7 +241,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.37.2
Re: [RFC 01/10] drm: Track clients by tgid and not tid
Am 14.03.23 um 15:18 schrieb Tvrtko Ursulin:
Quoted 10 lines
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.
Quoted 6 lines
Signed-off-by Tvrtko Ursulin <[email protected]>Cc Zack Rusin <[email protected]>Cc Alex Deucher <[email protected]>Cc "Christian König" <[email protected]>Reviewed-by Zack Rusin <[email protected]>
Should we push the already reviewed cleanups like this one to drm-misc-next? That makes sense even without the rest of the functionality and reduce the amount of patches re-send.
Christian.
Quoted 68 lines
--- 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@@ -969,7 +969,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@@ -241,7 +241,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 ();
Re: [RFC 01/10] drm: Track clients by tgid and not tid
Hi,
Quoted 18 lines · Christian König
Am 14.03.23 um 15:18 schrieb Tvrtko Ursulin:
Quoted 10 lines
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.
Quoted 6 lines
Signed-off-by Tvrtko Ursulin <[email protected]>Cc Zack Rusin <[email protected]>Cc Alex Deucher <[email protected]>Cc "Christian König" <[email protected]>Reviewed-by Zack Rusin <[email protected]>
Quoted 3 lines
Reviewed-by Christian König <[email protected]>Should we push the already reviewed cleanups like this one to drm-misc-next? That makes sense even without the rest of the functionality and reduce the amount of patches re-send.
I don't have the commit rights so if you could do that I certainly would not mind, thanks!
Regards,
Tvrtko
Quoted 71 lines
Quoted 70 lines
---
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@@ -969,7 +969,7 @@ static int amdgpu_debugfs_gem_info_show(structseq_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");
drivers/gpu/drm/drm_debugfs.c +0
-0@@ -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@@ -241,7 +241,7 @@ static int vmw_debugfs_gem_info_show(structseq_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();
Christian König
maintainerRe: [RFC 01/10] drm: Track clients by tgid and not tid
Am 15.03.23 um 10:52 schrieb Tvrtko Ursulin:
Quoted 22 lines
Hi,
Quoted 18 lines · Christian König
Am 14.03.23 um 15:18 schrieb Tvrtko Ursulin:
Quoted 16 lines
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]>Reviewed-by Zack Rusin <[email protected]>
Quoted 5 lines
Quoted 3 lines
Reviewed-by Christian König <[email protected]>Should we push the already reviewed cleanups like this one to drm-misc-next? That makes sense even without the rest of the functionality and reduce the amount of patches re-send.
I don't have the commit rights so if you could do that I certainly would not mind, thanks!
I've just pushed this patch here to drm-misc-next. As soon as Daniel or Dave gives his ok as well I'm going to also push the second one.
Regards,
Christian.
Quoted 76 lines
Regards,
Tvrtko
Quoted 71 lines
Quoted 70 lines
---
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@@ -969,7 +969,7 @@ static int amdgpu_debugfs_gem_info_show(structseq_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");
drivers/gpu/drm/drm_debugfs.c +0
-0@@ -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@@ -241,7 +241,7 @@ static int vmw_debugfs_gem_info_show(structseq_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();
RFC v4 2/10 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.
Before:
$ cat /sys/kernel/debug/dri/0/clients
command pid dev master a uid magic
Xorg 2344 0 y y 0 0
Xorg 2344 0 n y 0 2
Xorg 2344 0 n y 0 3
Xorg 2344 0 n y 0 4After:
$ cat /sys/kernel/debug/dri/0/clients
command tgid dev master a uid magic
Xorg 830 0 y y 0 0
xfce4-session 880 0 n y 0 1
xfwm4 943 0 n y 0 2
neverball 1095 0 n y 0 3--- 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 | 40 +++++++++++++++++++++++-- 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, 71 insertions(+), 15 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +4 -2
@@ -960,6 +960,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;/*
@@ -969,8 +970,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_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>", - 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 +37 -3
@@ -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_tgid (current)); +rcu_assign_pointer (file->pid,get_pid (task_tgid (current))); file->minor = minor;/* for compatibility root is always authenticated */
@@ -196,7 +196,7 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)drm_syncobj_release (file);if (drm_core_check_feature (dev,DRIVER_GEM ))drm_gem_release (dev, file); -put_pid (file->pid); +put_pid (rcu_access_pointer (file->pid));kfree (file);return ERR_PTR (ret);
@@ -287,7 +287,7 @@ void drm_file_free(struct drm_file *file)WARN_ON (!list_empty (&file->event_list)); -put_pid (file->pid); +put_pid (rcu_access_pointer (file->pid));kfree (file); }
@@ -501,6 +501,40 @@ 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 need to keep the original ownership in order for +* drm_master_check_perm to keep working correctly. (See comment in +* drm_auth.c.) +*/ +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
@@ -1095,7 +1095,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
@@ -232,6 +232,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;/*
@@ -241,8 +242,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
@@ -255,8 +255,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;
@@ -415,6 +422,8 @@ static inline bool drm_is_accel_client(const struct drm_file *file_priv)return file_priv->minor->type ==DRM_MINOR_ACCEL ; } +void drm_file_update_pid (struct drm_file *); +int drm_open (struct inode *inode,struct file *filp);int drm_open_helper (struct file *filp,struct drm_minor *minor); ssize_t drm_read(struct file *filp, char __user *buffer,
Signature · 1 line
2.37.2
Christian König
maintainerRe: [RFC 02/10] drm: Update file owner during use
Am 14.03.23 um 15:18 schrieb Tvrtko Ursulin:
Quoted 30 lines
From: 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->pidon 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.Before:
$ cat /sys/kernel/debug/dri/0/clients command pid dev master a uid magic Xorg 2344 0 y y 0 0 Xorg 2344 0 n y 0 2 Xorg 2344 0 n y 0 3 Xorg 2344 0 n y 0 4After:
$ cat /sys/kernel/debug/dri/0/clients command tgid dev master a uid magic Xorg 830 0 y y 0 0 xfce4-session 880 0 n y 0 1 xfwm4 943 0 n y 0 2 neverball 1095 0 n y 0 3
Quoted 3 lines
Signed-off-by Tvrtko Ursulin <[email protected]>Cc "Christian König" <[email protected]>Cc Daniel Vetter <[email protected]>
Looks completely correct to me, but I can't claim that I understand all those nasty details around drm_master handling.
So only Acked-by: Christian König <[email protected]>
Quoted 232 lines
--- 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 | 40 +++++++++++++++++++++++-- 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, 71 insertions(+), 15 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +4
-2@@ -960,6 +960,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;/* @@ -969,8 +970,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_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>", - 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 +37
-3@@ -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_tgid (current)); +rcu_assign_pointer (file->pid,get_pid (task_tgid (current))); file->minor = minor;/* for compatibility root is always authenticated */ @@ -196,7 +196,7 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)drm_syncobj_release (file);if (drm_core_check_feature (dev,DRIVER_GEM ))drm_gem_release (dev, file); -put_pid (file->pid); +put_pid (rcu_access_pointer (file->pid));kfree (file);return ERR_PTR (ret);@@ -287,7 +287,7 @@ void drm_file_free(struct drm_file *file)WARN_ON (!list_empty (&file->event_list)); -put_pid (file->pid); +put_pid (rcu_access_pointer (file->pid));kfree (file); }@@ -501,6 +501,40 @@ 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 need to keep the original ownership in order for +* drm_master_check_perm to keep working correctly. (See comment in +* drm_auth.c.) +*/ +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@@ -1095,7 +1095,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@@ -232,6 +232,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;/* @@ -241,8 +242,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@@ -255,8 +255,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;@@ -415,6 +422,8 @@ static inline bool drm_is_accel_client(const struct drm_file *file_priv)return file_priv->minor->type ==DRM_MINOR_ACCEL ; } +void drm_file_update_pid (struct drm_file *); +int drm_open (struct inode *inode,struct file *filp);int drm_open_helper (struct file *filp,struct drm_minor *minor); ssize_t drm_read(struct file *filp, char __user *buffer,
Re: [RFC 02/10] drm: Update file owner during use
In reply to Tvrtko UrsulinGreetings everyone,
Above all - hell yeah. Thank you Tvrtko, this has been annoying the hell out of me for ages.
Quoted 6 lines · Tvrtko Ursulin
From: 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.
s/opends/opens/
But as a whole the sentence is fairly misleading. Story time:
The traditional model, the server was the orchestrator managing the primary device node. From the fd, to the master status and authentication. But looking at the fd alone, this has varied across the years.
IIRC in the DRI1 days, Xorg (libdrm really) would have a list of open fd(s) and reuse those whenever needed, DRI2 the client was responsible for open() themselves and with DRI3 the fd was passed to the client.
Around the inception of DRI3 and systemd-logind, the latter became another possible orchestrator. Whereby Xorg and Wayland compositors could ask it for the fd. For various reasons (hysterical and genuine ones) Xorg has a fallback path going the open(), whereas Wayland compositors are moving to solely relying on logind... some never had fallback even.
Over the past few years, more projects have emerged which provide functionality similar (be that on API level, Dbus, or otherwise) to systemd-logind.
Apart from that, the commit is spot on. I like the use of rcu and the was_master handling is correct. With some message polish this commit is:
I also had a brief look at 01/10, although I cannot find many references for the pid <> tguid mappings. Be that on the kernel side or userspace - do you have any links that I can educate myself?
Thanks
Emil
Re: [RFC 02/10] drm: Update file owner during use
Quoted 4 lines · Emil Velikov
Greetings everyone,
Above all - hell yeah. Thank you Tvrtko, this has been annoying the hell out of me for ages.
Yay!
Quoted 10 lines
Quoted 6 lines · Tvrtko Ursulin
From: 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.
s/opends/opens/
Thanks!
Quoted 23 lines
But as a whole the sentence is fairly misleading. Story time:
The traditional model, the server was the orchestrator managing the primary device node. From the fd, to the master status and authentication. But looking at the fd alone, this has varied across the years.
IIRC in the DRI1 days, Xorg (libdrm really) would have a list of open fd(s) and reuse those whenever needed, DRI2 the client was responsible for
open()themselves and with DRI3 the fd was passed to the client.Around the inception of DRI3 and systemd-logind, the latter became another possible orchestrator. Whereby Xorg and Wayland compositors could ask it for the fd. For various reasons (hysterical and genuine ones) Xorg has a fallback path going the
open(), whereas Wayland compositors are moving to solely relying on logind... some never had fallback even.Over the past few years, more projects have emerged which provide functionality similar (be that on API level, Dbus, or otherwise) to systemd-logind.
Quoted 4 lines
Apart from that, the commit is spot on. I like the use of rcu and the
was_masterhandling is correct. With some message polish this commit is:Reviewed-by Emil Velikov <[email protected]>
Are you okay if I just paste your very fine explanation verbatim, with credits?
Quoted 3 lines
I also had a brief look at 01/10, although I cannot find many references for the pid <> tguid mappings. Be that on the kernel side or userspace - do you have any links that I can educate myself?
TGID or thread group leader. For single threaded userspace TGID equals to PID, while for multi-threaded first thread TGID equals PID/TID, while additional threads PID/TID does not equal TGID. Clear, as mud? :) My POSIX book is misplaced somewhere having not consulted it years... :)
Regards,
Tvrtko
Re: [RFC 02/10] drm: Update file owner during use
Hi Tvrtko
Sorry for the delay, real life and other obligations got in the way.
Quoted 5 lines · Tvrtko Ursulin
On 21/04/2023 13:13, Emil Velikov wrote:
Are you okay if I just paste your very fine explanation verbatim, with credits?
Yes, feel free to use as much of if as you see reasonable.
Quoted 9 lines
Quoted 3 lines
I also had a brief look at 01/10, although I cannot find many references for the pid <> tguid mappings. Be that on the kernel side or userspace - do you have any links that I can educate myself?
TGID or thread group leader. For single threaded userspace TGID equals to PID, while for multi-threaded first thread TGID equals PID/TID, while additional threads PID/TID does not equal TGID. Clear, as mud? :) My POSIX book is misplaced somewhere having not consulted it years... :)
Ack. /me looks into actually buying one, perhaps
Thanks
Emil
RFC v4 3/10 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 | 60 +++++++++++++++++++++++++++++++++++ 5 files changed, 81 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 © 2023 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
@@ -1093,6 +1093,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 +60 -0
60 lines not on this page · raw message
Signature · 1 line
2.37.2
RFC v4 4/10 drm/cgroup: Track DRM clients per cgroup
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
To enable propagation of settings from the cgroup DRM controller to DRM and vice-versa, we need to start tracking to which cgroups DRM clients belong.
--- drivers/gpu/drm/drm_file.c | 6 ++++ include/drm/drm_file.h | 6 ++++ include/linux/cgroup_drm.h | 20 ++++++++++++ kernel/cgroup/drm.c | 62 +++++++++++++++++++++++++++++++++++++- 4 files changed, 93 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_file.c +6 -0
@@ -32,6 +32,7 @@*/ #include <linux/anon_inodes.h> +#include <linux/cgroup_drm.h> #include <linux/dma-fence.h> #include <linux/file.h> #include <linux/module.h>
@@ -300,6 +301,8 @@ static void drm_close_helper(struct file *filp)list_del (&file_priv->lhead);mutex_unlock (&dev->filelist_mutex); +drmcgroup_client_close (file_priv); +drm_file_free (file_priv); }
@@ -363,6 +366,8 @@ int drm_open_helper(struct file *filp, struct drm_minor *minor)list_add (&priv->lhead, &dev->filelist);mutex_unlock (&dev->filelist_mutex); +drmcgroup_client_open (priv); +#ifdef CONFIG_DRM_LEGACY #ifdef __alpha__/*
@@ -529,6 +534,7 @@ void drm_file_update_pid(struct drm_file *filp)mutex_unlock (&dev->filelist_mutex);if (pid != old) { +drmcgroup_client_migrate (filp);get_pid (pid);synchronize_rcu ();put_pid (old);
include/drm/drm_file.h +6 -0
@@ -30,6 +30,7 @@#ifndef _DRM_FILE_H_#define _DRM_FILE_H_ +#include <linux/cgroup.h> #include <linux/types.h> #include <linux/completion.h> #include <linux/idr.h>
@@ -279,6 +280,11 @@ struct drm_file {/** @minor: &struct drm_minor for this file. */ struct drm_minor *minor; +#if IS_ENABLED (CONFIG_CGROUP_DRM ) +struct cgroup_subsys_state *__css; +struct list_head clink; +#endif +/** * @object_idr: *
include/linux/cgroup_drm.h +20 -0
@@ -6,4 +6,24 @@#ifndef _CGROUP_DRM_H#define _CGROUP_DRM_H +#include <drm/drm_file.h> + +#if IS_ENABLED (CONFIG_CGROUP_DRM ) +void drmcgroup_client_open (struct drm_file *file_priv); +void drmcgroup_client_close (struct drm_file *file_priv); +void drmcgroup_client_migrate (struct drm_file *file_priv); +#else +static inline void drmcgroup_client_open (struct drm_file *file_priv) +{ +} + +static inline void drmcgroup_client_close (struct drm_file *file_priv) +{ +} + +static void drmcgroup_client_migrate (struct drm_file *file_priv) +{ +} +#endif +#endif /* _CGROUP_DRM_H */
kernel/cgroup/drm.c +61 -1
89 lines not on this page · raw message
Signature · 1 line
2.37.2
RFC v4 5/10 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.
--- include/drm/drm_drv.h | 28 ++++++++++++++++++++++++++++ kernel/cgroup/drm.c | 20 ++++++++++++++++++++ 2 files changed, 48 insertions(+)
include/drm/drm_drv.h +28 -0
@@ -151,6 +151,24 @@ enum drm_driver_feature { DRIVER_HAVE_IRQ = BIT(30), }; +/** +* 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 *
@@ -443,6 +461,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: */
kernel/cgroup/drm.c +20 -0
@@ -9,6 +9,8 @@#include <linux/mutex.h> #include <linux/slab.h> +#include <drm/drm_drv.h> +struct drm_cgroup_state {struct cgroup_subsys_state css;
@@ -31,6 +33,24 @@ css_to_drmcs(struct cgroup_subsys_state *css) return container_of(css, struct drm_cgroup_state, css); } +static u64 drmcs_get_active_time_us (struct drm_cgroup_state *drmcs) +{ +struct drm_file *fpriv; +u64 total =0 ; + +lockdep_assert_held (&drmcg_mutex); + +list_for_each_entry (fpriv, &drmcs->clients, 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); + } + +return total; +} +static void drmcs_free (struct cgroup_subsys_state *css) {struct drm_cgroup_state *drmcs =css_to_drmcs (css);
Signature · 1 line
2.37.2
RFC v4 6/10 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.
--- include/drm/drm_drv.h | 8 ++++++++ kernel/cgroup/drm.c | 16 ++++++++++++++++ 2 files changed, 24 insertions(+)
include/drm/drm_drv.h +8 -0
@@ -167,6 +167,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); };/**
kernel/cgroup/drm.c +16 -0
@@ -51,6 +51,22 @@ static u64 drmcs_get_active_time_us(struct drm_cgroup_state *drmcs)return total; } +static void +drmcs_signal_budget (struct drm_cgroup_state *drmcs,u64 usage,u64 budget) +{ +struct drm_file *fpriv; + +lockdep_assert_held (&drmcg_mutex); + +list_for_each_entry (fpriv, &drmcs->clients, 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); + } +} +static void drmcs_free (struct cgroup_subsys_state *css) {struct drm_cgroup_state *drmcs =css_to_drmcs (css);
Signature · 1 line
2.37.2
RFC v4 7/10 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.
--- kernel/cgroup/drm.c | 9 +++++++++ 1 file changed, 9 insertions(+)
kernel/cgroup/drm.c +9 -0
@@ -97,6 +97,9 @@ void drmcgroup_client_open(struct drm_file *file_priv) {struct drm_cgroup_state *drmcs; +if (!file_priv->minor->dev->driver->cg_ops) +return ; + drmcs =css_to_drmcs (task_get_css (current, drm_cgrp_id));mutex_lock (&drmcg_mutex);
@@ -112,6 +115,9 @@ void drmcgroup_client_close(struct drm_file *file_priv) drmcs =css_to_drmcs (file_priv->__css); +if (!file_priv->minor->dev->driver->cg_ops) +return ; +mutex_lock (&drmcg_mutex);list_del (&file_priv->clink); file_priv->__css =NULL ;
@@ -126,6 +132,9 @@ void drmcgroup_client_migrate(struct drm_file *file_priv)struct drm_cgroup_state *src, *dst;struct cgroup_subsys_state *old; +if (!file_priv->minor->dev->driver->cg_ops) +return ; +mutex_lock (&drmcg_mutex); old = file_priv->__css;
Signature · 1 line
2.37.2
RFC v4 8/10 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.
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.
Checking of GPU usage is done periodically by the controller which can be configured via drmcg_period_ms kernel boot parameter and which defaults to 2s.
--- Documentation/admin-guide/cgroup-v2.rst | 31 ++ kernel/cgroup/drm.c | 409 +++++++++++++++++++++++- 2 files changed, 437 insertions(+), 3 deletions(-)
Documentation/admin-guide/cgroup-v2.rst +31 -0
@@ -2407,6 +2407,37 @@ 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. + Misc ----
kernel/cgroup/drm.c +406 -3
480 lines not on this page · raw message
Signature · 1 line
2.37.2
RFC v4 9/10 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
@@ -1777,6 +1777,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: *
@@ -1805,6 +1811,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.37.2
RFC v4 10/10 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 clients are throttled.
--- .../gpu/drm/i915/gem/i915_gem_execbuffer.c | 38 ++++- drivers/gpu/drm/i915/i915_driver.c | 1 + drivers/gpu/drm/i915/i915_drm_client.c | 133 ++++++++++++++++++ drivers/gpu/drm/i915/i915_drm_client.h | 11 ++ 4 files changed, 182 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +37 -1
@@ -3065,6 +3065,42 @@ 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; +int prio; + + *attr = eb->gem_context->sched; + + prio = file_priv->client->throttle[__get_class (file_priv, rq)]; +if (prio) + attr->priority = prio; +} +#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) {
@@ -3081,7 +3117,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
@@ -1780,6 +1780,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 +133 -0
145 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 +int 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.37.2
Tejun Heo
maintainerHello, Tvrtko.
Quoted 15 lines · Tvrtko Ursulin
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.
Quoted 6 lines
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In general, I'm in favor of your approach but can you please stop using the term "soft limit". That's a term with a specific historical meaning in cgroup, so it gets really confusing when you use the term for hierarchical weighted control. If you need a term to refer to how the weighted control is implemented by throttling cgroups at target rates, please just come up with a different term "usage threshold based control", "usage throttling based control" or whichever you may like.
Quoted 9 lines
drm.weight
Standard cgroup weight based control [1, 10000] used to configure the relative distributing of GPU time between the sibling groups.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.
Quoted 4 lines
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.
FWIW, the hierarchical weighted distribution is also implemented by IO control.
Quoted 5 lines
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.
Maybe "best effort" is more suited than "soft"?
...
Quoted 25 lines
Roughly simultaneously we run the following two benchmarks in each session respectively:
1)
./GpuTest /test=pixmark_julia_fp32 /width=1920 /height=1080 /fullscreen /no_scorebox /benchmark /benchmark_duration_ms=600002)
vblank_mode=0 bin/testfw_app --gl_api=desktop_core --width=1920 --height=1080 --fullscreen 1 --gfx=glfw -tgl_manhattan(The only reason for vsync off here is because I struggled to find an easily runnable and demanding enough benchmark, or to run on a screen large enough to make even a simpler ones demanding.)
With this test we get 252fps from GpuTest and 96fps from GfxBenchmark.
Premise here is that one of these GPU intensive benchmarks is intended to be ran by the user with lower priority. Imagine kicking off some background compute processing and continuing to use the UI for other tasks. Hence the user will now re-run the test by first lowering the weight control of the first session (DRM cgroup):
1)
echo 50 | sudo tee/sys/fs/cgroup/cut -d':' -f3 /proc/self/cgroup/drm.weight
./GpuTest /test=pixmark_julia_fp32 /width=1920 /height=1080 /fullscreen /no_scorebox /benchmark /benchmark_duration_ms=60000
Quoted 7 lines
2)
vblank_mode=0 bin/testfw_app --gl_api=desktop_core --width=1920 --height=1080 --fullscreen 1 --gfx=glfw -tgl_manhattanIn this case we will see that GpuTest has recorded 208fps (~18% down) and GfxBenchmark 114fps (18% up), demonstrating that even a very simple approach of wiring up i915 to the DRM cgroup controller can enable external GPU scheduling control.
It's really nice to see it working pretty intuitively.
Quoted 6 lines
* For now (RFC) I haven't implemented the 2nd suggestion from Tejun of having a shadow tree which would only contain groups with DRM clients. (Purpose being less nodes to traverse in the scanning loop.)
* Is the global state passing from
can_attachto attach really okay? (I need source and destination css.)
Right now, it is and there are other places that depend on it. Obviously, it's not great and we probably want to add explicit context passed around instead in the future, but for now, it should be okay.
While not fully polished, from cgroup POV, the series looks pretty good and I'd be happy to see it merged.
Thanks.
Signature · 1 line
tejun