Commit 39388d53 authored by Dave Airlie's avatar Dave Airlie
Browse files

Merge tag 'cgroup-dmem-drm-v2' of...

Merge tag 'cgroup-dmem-drm-v2' of git://git.kernel.org/pub/scm/linux/kernel/git/mripard/linux into drm-next

DMEM cgroup pull request

This introduces a new cgroup controller to limit the device memory.
Notable users would be DRM, dma-buf heaps, or v4l2.

This pull request is based on the series developped by Maarten
Lankhorst, Friedrich Vock, and I:
https://lore.kernel.org/all/20241204134410.1161769-1-dev@lankhorst.se/



Signed-off-by: default avatarDave Airlie <airlied@redhat.com>

From: Maxime Ripard <mripard@redhat.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20250110-cryptic-warm-mandrill-b71f5d@houat
parents f6001870 dfe6aa16
Loading
Loading
Loading
Loading
+51 −7
Original line number Diff line number Diff line
@@ -64,13 +64,14 @@ v1 is available under :ref:`Documentation/admin-guide/cgroup-v1/index.rst <cgrou
     5-6. Device
     5-7. RDMA
       5-7-1. RDMA Interface Files
     5-8. HugeTLB
       5.8-1. HugeTLB Interface Files
     5-9. Misc
       5.9-1 Miscellaneous cgroup Interface Files
       5.9-2 Migration and Ownership
     5-10. Others
       5-10-1. perf_event
     5-8. DMEM
     5-9. HugeTLB
       5.9-1. HugeTLB Interface Files
     5-10. Misc
       5.10-1 Miscellaneous cgroup Interface Files
       5.10-2 Migration and Ownership
     5-11. Others
       5-11-1. perf_event
     5-N. Non-normative information
       5-N-1. CPU controller root cgroup process behaviour
       5-N-2. IO controller root cgroup process behaviour
@@ -2626,6 +2627,49 @@ RDMA Interface Files
	  mlx4_0 hca_handle=1 hca_object=20
	  ocrdma1 hca_handle=1 hca_object=23

DMEM
----

The "dmem" controller regulates the distribution and accounting of
device memory regions. Because each memory region may have its own page size,
which does not have to be equal to the system page size, the units are always bytes.

DMEM Interface Files
~~~~~~~~~~~~~~~~~~~~

  dmem.max, dmem.min, dmem.low
	A readwrite nested-keyed file that exists for all the cgroups
	except root that describes current configured resource limit
	for a region.

	An example for xe follows::

	  drm/0000:03:00.0/vram0 1073741824
	  drm/0000:03:00.0/stolen max

	The semantics are the same as for the memory cgroup controller, and are
	calculated in the same way.

  dmem.capacity
	A read-only file that describes maximum region capacity.
	It only exists on the root cgroup. Not all memory can be
	allocated by cgroups, as the kernel reserves some for
	internal use.

	An example for xe follows::

	  drm/0000:03:00.0/vram0 8514437120
	  drm/0000:03:00.0/stolen 67108864

  dmem.current
	A read-only file that describes current resource usage.
	It exists for all the cgroup except root.

	An example for xe follows::

	  drm/0000:03:00.0/vram0 12550144
	  drm/0000:03:00.0/stolen 8650752

HugeTLB
-------

+9 −0
Original line number Diff line number Diff line
==================
Cgroup Kernel APIs
==================

Device Memory Cgroup API (dmemcg)
=========================
.. kernel-doc:: kernel/cgroup/dmem.c
   :export:
+1 −0
Original line number Diff line number Diff line
@@ -109,6 +109,7 @@ more memory-management documentation in Documentation/mm/index.rst.
   dma-isa-lpc
   swiotlb
   mm-api
   cgroup
   genalloc
   pin_user_pages
   boot-time-mm
+54 −0
Original line number Diff line number Diff line
==================================
Long running workloads and compute
==================================

Long running workloads (compute) are workloads that will not complete in 10
seconds. (The time let the user wait before he reaches for the power button).
This means that other techniques need to be used to manage those workloads,
that cannot use fences.

Some hardware may schedule compute jobs, and have no way to pre-empt them, or
have their memory swapped out from them. Or they simply want their workload
not to be preempted or swapped out at all.

This means that it differs from what is described in driver-api/dma-buf.rst.

As with normal compute jobs, dma-fence may not be used at all. In this case,
not even to force preemption. The driver with is simply forced to unmap a BO
from the long compute job's address space on unbind immediately, not even
waiting for the workload to complete. Effectively this terminates the workload
when there is no hardware support to recover.

Since this is undesirable, there need to be mitigations to prevent a workload
from being terminated. There are several possible approach, all with their
advantages and drawbacks.

The first approach you will likely try is to pin all buffers used by compute.
This guarantees that the job will run uninterrupted, but also allows a very
denial of service attack by pinning as much memory as possible, hogging the
all GPU memory, and possibly a huge chunk of CPU memory.

A second approach that will work slightly better on its own is adding an option
not to evict when creating a new job (any kind). If all of userspace opts in
to this flag, it would prevent cooperating userspace from forced terminating
older compute jobs to start a new one.

If job preemption and recoverable pagefaults are not available, those are the
only approaches possible. So even with those, you want a separate way of
controlling resources. The standard kernel way of doing so is cgroups.

This creates a third option, using cgroups to prevent eviction. Both GPU and
driver-allocated CPU memory would be accounted to the correct cgroup, and
eviction would be made cgroup aware. This allows the GPU to be partitioned
into cgroups, that will allow jobs to run next to each other without
interference.

The interface to the cgroup would be similar to the current CPU memory
interface, with similar semantics for min/low/high/max, if eviction can
be made cgroup aware.

What should be noted is that each memory region (tiled memory for example)
should have its own accounting.

The key is set to the regionid set by the driver, for example "tile0".
For the value of $card, we use drmGetUnique().
+32 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@
 * DEALINGS IN THE SOFTWARE.
 */

#include <linux/cgroup_dmem.h>
#include <linux/debugfs.h>
#include <linux/fs.h>
#include <linux/module.h>
@@ -820,6 +821,37 @@ void drm_dev_put(struct drm_device *dev)
}
EXPORT_SYMBOL(drm_dev_put);

static void drmm_cg_unregister_region(struct drm_device *dev, void *arg)
{
	dmem_cgroup_unregister_region(arg);
}

/**
 * drmm_cgroup_register_region - Register a region of a DRM device to cgroups
 * @dev: device for region
 * @region_name: Region name for registering
 * @size: Size of region in bytes
 *
 * This decreases the ref-count of @dev by one. The device is destroyed if the
 * ref-count drops to zero.
 */
struct dmem_cgroup_region *drmm_cgroup_register_region(struct drm_device *dev, const char *region_name, u64 size)
{
	struct dmem_cgroup_region *region;
	int ret;

	region = dmem_cgroup_register_region(size, "drm/%s/%s", dev->unique, region_name);
	if (IS_ERR_OR_NULL(region))
		return region;

	ret = drmm_add_action_or_reset(dev, drmm_cg_unregister_region, region);
	if (ret)
		return ERR_PTR(ret);

	return region;
}
EXPORT_SYMBOL_GPL(drmm_cgroup_register_region);

static int create_compat_control_link(struct drm_device *dev)
{
	struct drm_minor *minor;
Loading