Skip to contentSkip to conversation

kernel/cgroups: Add "dev" memory accounting cgroup.

PATCH In review Maarten Lankhorst 9 participants 34 messages Oct 23, 2024 – Nov 14, 2024

Subsystems (10)
lore
Share & tools
lore

Maarten Lankhorst

author maintainer
#0 lore raw

cover kernel/cgroups: Add "dev" memory accounting cgroup.

New submission!
I've added documentation for each call, and integrated the renaming from drm cgroup to dev cgroup, based on maxime ripard's work.

Maxime has been testing this with dma-buf heaps and v4l2 too, and it seems to work. In the initial submission, I've decided to only add the smallest enablement possible, to have less chance of breaking things.

The API has been changed slightly, from "$name region.$regionname=$limit" in a file called dev.min/low/max to "$subsystem/$name $regionname=$limit" in a file called dev.region.min/low/max.

This hopefully allows us to perhaps extend the API later on with the possibility to set scheduler weights on the device, like in

https://blogs.igalia.com/tursulin/drm-scheduling-cgroup-controller/

Maarten Lankhorst (5):
kernel/cgroup: Add "dev" memory accounting cgroup drm/ttm: Handle cgroup based eviction in TTM
drm/xe: Implement cgroup for vram
drm/amdgpu: Add cgroups implementation
[HACK] drm/xe: Hack to test with mapped pages instead of vram.

Maxime Ripard (2):
drm/drv: Add drmm cgroup registration for dev cgroups. [DISCUSSION] drm/gem: Add cgroup memory accounting

 Documentation/admin-guide/cgroup-v2.rst       |  51 +
 Documentation/core-api/cgroup.rst             |   9 +
 Documentation/core-api/index.rst              |   1 +
 Documentation/gpu/drm-compute.rst             |  54 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu.h           |   2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       |   6 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c  |   6 +
 drivers/gpu/drm/drm_drv.c                     |  32 +-
 drivers/gpu/drm/drm_gem.c                     |   4 +
 drivers/gpu/drm/drm_gem_dma_helper.c          |   4 +
 drivers/gpu/drm/ttm/tests/ttm_bo_test.c       |  18 +-
 .../gpu/drm/ttm/tests/ttm_bo_validate_test.c  |   4 +-
 drivers/gpu/drm/ttm/tests/ttm_resource_test.c |   2 +-
 drivers/gpu/drm/ttm/ttm_bo.c                  |  57 +-
 drivers/gpu/drm/ttm/ttm_resource.c            |  24 +-
 drivers/gpu/drm/xe/xe_device.c                |   4 +
 drivers/gpu/drm/xe/xe_device_types.h          |   4 +
 drivers/gpu/drm/xe/xe_ttm_sys_mgr.c           |  14 +
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.c          |  10 +
 include/drm/drm_device.h                      |   4 +
 include/drm/drm_drv.h                         |   4 +
 include/drm/drm_gem.h                         |   2 +
 include/drm/ttm/ttm_resource.h                |  16 +-
 include/linux/cgroup_dev.h                    |  91 ++
 include/linux/cgroup_subsys.h                 |   4 +
 include/linux/page_counter.h                  |   2 +-
 init/Kconfig                                  |   7 +
 kernel/cgroup/Makefile                        |   1 +
 kernel/cgroup/dev.c                           | 893 ++++++++++++++++++
 mm/page_counter.c                             |   4 +-
 30 files changed, 1307 insertions(+), 27 deletions(-)
 create mode 100644 Documentation/core-api/cgroup.rst
 create mode 100644 Documentation/gpu/drm-compute.rst
 create mode 100644 include/linux/cgroup_dev.h
 create mode 100644 kernel/cgroup/dev.c
Signature · 1 line
2.45.2
author maintainer
#1 lore raw

PATCH 1/7 kernel/cgroup: Add "dev" memory accounting cgroup

The initial version was based roughly on the rdma and misc cgroup controllers, with a lot of the accounting code borrowed from rdma.

The current version is a complete rewrite with page counter; it uses the same min/low/max semantics as the memory cgroup as a result.

There's a small mismatch as TTM uses u64, and page_counter long pages. In practice it's not a problem. 32-bits systems don't really come with

Quoted 1 line

=4GB cards and as long as we're consistently wrong with units, it's

fine. The device page size may not be in the same units as kernel page size, and each region might also have a different page size (VRAM vs GART for example).

The interface is simple:
- populate dev_cgroup_try_charge->regions[..] name and size for each active region, set num_regions accordingly.
- Call (dev,drmm)_cgroup_register_device()
- Use dev_cgroup_try_charge to check if you can allocate a chunk of memory, use dev_cgroup__uncharge when freeing it. This may return an error code, or -EAGAIN when the cgroup limit is reached. In that case a reference to the limiting pool is returned.
- The limiting cs can be used as compare function for dev_cgroup_state_evict_valuable.
- After having evicted enough, drop reference to limiting cs with dev_cgroup_pool_state_put.

This API allows you to limit device resources with cgroups. You can see the supported cards in /sys/fs/cgroup/dev.region.capacity You need to echo +dev to cgroup.subtree_control, and then you can partition memory.

---
 Documentation/admin-guide/cgroup-v2.rst |  51 ++
 Documentation/core-api/cgroup.rst       |   9 +
 Documentation/core-api/index.rst        |   1 +
 Documentation/gpu/drm-compute.rst       |  54 ++
 include/linux/cgroup_dev.h              |  91 +++
 include/linux/cgroup_subsys.h           |   4 +
 include/linux/page_counter.h            |   2 +-
 init/Kconfig                            |   7 +
 kernel/cgroup/Makefile                  |   1 +
 kernel/cgroup/dev.c                     | 893 ++++++++++++++++++++++++
 mm/page_counter.c                       |   4 +-
 11 files changed, 1114 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/core-api/cgroup.rst
 create mode 100644 Documentation/gpu/drm-compute.rst
 create mode 100644 include/linux/cgroup_dev.h
 create mode 100644 kernel/cgroup/dev.c
Documentation/admin-guide/cgroup-v2.rst +51 -0
index 69af2173555fb..e8fe79244af9c 100644
@@ -2612,6 +2612,57 @@ RDMA Interface Files
 	  mlx4_0 hca_handle=1 hca_object=20
 	  ocrdma1 hca_handle=1 hca_object=23
 
+DEV
+----
+
+The "dev" controller regulates the distribution and accounting of
+device resources, currently only 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 in bytes.
+
+DEV Interface Files
+~~~~~~~~~~~~~~~~~~~~
+
+  dev.region.max, dev.region.min, dev.region.low
+	A readwrite nested-keyed file that exists for all the cgroups
+	except root that describes current configured resource limit
+	for a device.
+
+	Lines are keyed by device name and are not ordered.
+	Each line contains space separated resource name and its configured
+	limit that can be distributed.
+
+	The following nested keys are defined.
+
+	  ==========	=======================================================
+	  *	 	Maximum amount of bytes that allocatable in this region
+	  ==========	=======================================================
+
+	An example for xe follows::
+
+	  drm/0000:03:00.0 vram0=1073741824 stolen=max
+
+	The semantics are the same as for the memory cgroup controller, and are
+	calculated in the same way.
+
+  dev.region.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 stolen=67108864
+
+  dev.region.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 stolen=8650752
+
 HugeTLB
 -------
 
Documentation/core-api/cgroup.rst new file +9 -0
new file mode 100644
index 0000000000000..475b32255bd68
@@ -0,0 +1,9 @@
+==================
+Cgroup Kernel APIs
+==================
+
+Device Cgroup API (devcg)
+=========================
+.. kernel-doc:: kernel/cgroup/dev.c
+   :export:
+
Documentation/core-api/index.rst +1 -0
index 6a875743dd4b7..dbd6c4f9a6313 100644
@@ -108,6 +108,7 @@ more memory-management documentation in Documentation/mm/index.rst.
    dma-isa-lpc
    swiotlb
    mm-api
+   cgroup
    genalloc
    pin_user_pages
    boot-time-mm
Documentation/gpu/drm-compute.rst new file +54 -0

54 lines not on this page · raw message

include/linux/cgroup_dev.h new file +91 -0

91 lines not on this page · raw message

include/linux/cgroup_subsys.h +4 -0
index 4452354872307..898340cfe5843 100644
@@ -65,6 +65,10 @@ SUBSYS(rdma)
 SUBSYS(misc)
 #endif
 
+#if IS_ENABLED(CONFIG_CGROUP_DEV)
+SUBSYS(dev)
+#endif
+
 /*
  * The following subsystems are not supported on the default hierarchy.
  */
include/linux/page_counter.h +1 -1
index 79dbd8bc35a72..d75376a1694ee 100644
@@ -96,7 +96,7 @@ static inline void page_counter_reset_watermark(struct page_counter *counter)
 	counter->watermark = usage;
 }
 
-#ifdef CONFIG_MEMCG
+#if IS_ENABLED(CONFIG_MEMCG) || IS_ENABLED(CONFIG_CGROUP_DEVICE)
 void page_counter_calculate_protection(struct page_counter *root,
 				       struct page_counter *counter,
 				       bool recursive_protection);
init/Kconfig +7 -0
index 530a382ee0feb..2da595facd97f 100644
@@ -1123,6 +1123,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_DEV
+	bool "Device controller"
+	help
+	  Provides the device subsystem controller.
+
+	  ...
+
 config CGROUP_FREEZER
 	bool "Freezer controller"
 	help
kernel/cgroup/Makefile +1 -0
index a5c9359d516f8..441d346fdc51f 100644
@@ -7,4 +7,5 @@ obj-$(CONFIG_CGROUP_RDMA) += rdma.o
 obj-$(CONFIG_CPUSETS) += cpuset.o
 obj-$(CONFIG_CPUSETS_V1) += cpuset-v1.o
 obj-$(CONFIG_CGROUP_MISC) += misc.o
+obj-$(CONFIG_CGROUP_DEV) += dev.o
 obj-$(CONFIG_CGROUP_DEBUG) += debug.o
kernel/cgroup/dev.c new file +893 -0

893 lines not on this page · raw message

mm/page_counter.c +2 -2
index b249d15af9dd8..fbb0cb34415d7 100644
@@ -288,7 +288,7 @@ int page_counter_memparse(const char *buf, const char *max,
 }
 
 
-#ifdef CONFIG_MEMCG
+#if IS_ENABLED(CONFIG_MEMCG) || IS_ENABLED(CONFIG_CGROUP_DEVICE)
 /*
  * This function calculates an individual page counter's effective
  * protection which is derived from its own memory.min/low, its
@@ -460,4 +460,4 @@ void page_counter_calculate_protection(struct page_counter *root,
 			atomic_long_read(&parent->children_low_usage),
 			recursive_protection));
 }
-#endif /* CONFIG_MEMCG */
+#endif /* CONFIG_MEMCG || CONFIG_CGROUP_DEVICE */
Signature · 1 line
2.45.2
#2 lore raw

Re: [PATCH 1/7] kernel/cgroup: Add "dev" memory accounting cgroup

Quoted 49 lines · Maarten Lankhorst

The initial version was based roughly on the rdma and misc cgroup controllers, with a lot of the accounting code borrowed from rdma.

The current version is a complete rewrite with page counter; it uses the same min/low/max semantics as the memory cgroup as a result.

There's a small mismatch as TTM uses u64, and page_counter long pages. In practice it's not a problem. 32-bits systems don't really come with

Quoted 1 line

=4GB cards and as long as we're consistently wrong with units, it's

fine. The device page size may not be in the same units as kernel page size, and each region might also have a different page size (VRAM vs GART for example).

The interface is simple:
- populate dev_cgroup_try_charge->regions[..] name and size for each active region, set num_regions accordingly.
- Call (dev,drmm)_cgroup_register_device()
- Use dev_cgroup_try_charge to check if you can allocate a chunk of memory, use dev_cgroup__uncharge when freeing it. This may return an error code, or -EAGAIN when the cgroup limit is reached. In that case a reference to the limiting pool is returned.
- The limiting cs can be used as compare function for dev_cgroup_state_evict_valuable.
- After having evicted enough, drop reference to limiting cs with dev_cgroup_pool_state_put.

This API allows you to limit device resources with cgroups. You can see the supported cards in /sys/fs/cgroup/dev.region.capacity You need to echo +dev to cgroup.subtree_control, and then you can partition memory.

---
  Documentation/admin-guide/cgroup-v2.rst |  51 ++
  Documentation/core-api/cgroup.rst       |   9 +
  Documentation/core-api/index.rst        |   1 +
  Documentation/gpu/drm-compute.rst       |  54 ++
  include/linux/cgroup_dev.h              |  91 +++
  include/linux/cgroup_subsys.h           |   4 +
  include/linux/page_counter.h            |   2 +-
  init/Kconfig                            |   7 +
  kernel/cgroup/Makefile                  |   1 +
  kernel/cgroup/dev.c                     | 893 ++++++++++++++++++++++++
  mm/page_counter.c                       |   4 +-
  11 files changed, 1114 insertions(+), 3 deletions(-)
Quoted 4 lines

create mode 100644 Documentation/core-api/cgroup.rst create mode 100644 Documentation/gpu/drm-compute.rst create mode 100644 include/linux/cgroup_dev.h create mode 100644 kernel/cgroup/dev.c

Just a general comment.

Cgroup v1 has a legacy device controller in security/device_cgroup.c which is no longer available in cgroup v2. So if you use the name device controller, the documentation must be clear that it is completely different and have no relationship from the device controller in cgroup v1.

Cheers,
Longman

author maintainer
#3 lore raw

Re: [PATCH 1/7] kernel/cgroup: Add "dev" memory accounting cgroup

Den 2024-10-23 kl. 17:26, skrev Waiman Long:

Quoted 57 lines · Maarten Lankhorst
Quoted 50 lines

The initial version was based roughly on the rdma and misc cgroup controllers, with a lot of the accounting code borrowed from rdma.

The current version is a complete rewrite with page counter; it uses the same min/low/max semantics as the memory cgroup as a result.

There's a small mismatch as TTM uses u64, and page_counter long pages. In practice it's not a problem. 32-bits systems don't really come with

Quoted 1 line

=4GB cards and as long as we're consistently wrong with units, it's

fine. The device page size may not be in the same units as kernel page size, and each region might also have a different page size (VRAM vs GART for example).

The interface is simple:
- populate dev_cgroup_try_charge->regions[..] name and size for each active
   region, set num_regions accordingly.
- Call (dev,drmm)_cgroup_register_device()
- Use dev_cgroup_try_charge to check if you can allocate a chunk of memory,
   use dev_cgroup__uncharge when freeing it. This may return an error code, or -EAGAIN when the cgroup limit is reached. In that case a reference to the limiting pool is returned.
- The limiting cs can be used as compare function for dev_cgroup_state_evict_valuable.
- After having evicted enough, drop reference to limiting cs with dev_cgroup_pool_state_put.

This API allows you to limit device resources with cgroups. You can see the supported cards in /sys/fs/cgroup/dev.region.capacity You need to echo +dev to cgroup.subtree_control, and then you can partition memory.

---
  Documentation/admin-guide/cgroup-v2.rst |  51 ++ Documentation/core-api/cgroup.rst       |   9 + Documentation/core-api/index.rst        |   1 + Documentation/gpu/drm-compute.rst       |  54 ++ include/linux/cgroup_dev.h              |  91 +++ include/linux/cgroup_subsys.h           |   4 + include/linux/page_counter.h            |   2 +- init/Kconfig                            |   7 + kernel/cgroup/Makefile                  |   1 + kernel/cgroup/dev.c                     | 893 ++++++++++++++++++++++++ mm/page_counter.c                       |   4 +-

Quoted 5 lines
  11 files changed, 1114 insertions(+), 3 deletions(-)

  create mode 100644 Documentation/core-api/cgroup.rst create mode 100644 Documentation/gpu/drm-compute.rst create mode 100644 include/linux/cgroup_dev.h create mode 100644 kernel/cgroup/dev.c

Quoted 3 lines

Just a general comment.

Cgroup v1 has a legacy device controller in security/device_cgroup.c which is no longer available in cgroup v2. So if you use the name device controller, the documentation must be clear that it is completely different and have no relationship from the device controller in cgroup v1.

Hey,

Thanks for noticing. I didn't know there was such a controller. Seems weird to have one for managing access to opening devnodes instead of a security module.

I'll update the documentation in the next version to make it more clear.

Cheers,
~Maarten

#4 lore raw

Re: [PATCH 1/7] kernel/cgroup: Add "dev" memory accounting cgroup

In reply to Maarten Lankhorst
kernel test robot noticed the following build warnings:

Hi Maarten,

kernel test robot noticed the following build warnings:

[auto build test WARNING on drm/drm-next]
[also build test WARNING on drm-exynos/exynos-drm-next linus/master v6.12-rc4 next-20241024]
[cannot apply to tj-cgroup/for-next drm-xe/drm-xe-next akpm-mm/mm-everything drm-intel/for-linux-next drm-intel/for-linux-next-fixes drm-misc/drm-misc-next drm-tip/drm-tip] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Maarten-Lankhorst/kernel-cgroup-Add-dev-memory-accounting-cgroup/20241023-155504
base:   git://anongit.freedesktop.org/drm/drm drm-next
patch link:    https://lore.kernel.org/r/20241023075302.27194-2-maarten.lankhorst%40linux.intel.com
patch subject: [PATCH 1/7] kernel/cgroup: Add "dev" memory accounting cgroup
config: i386-randconfig-061-20241025 (https://download.01.org/0day-ci/archive/20241025/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241025/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

>> kernel/cgroup/dev.c:423:9: sparse: sparse: incompatible types in comparison expression (different address spaces):
   kernel/cgroup/dev.c:423:9: sparse:    struct list_head [noderef] __rcu *
   kernel/cgroup/dev.c:423:9: sparse:    struct list_head *
>> kernel/cgroup/dev.c:423:9: sparse: sparse: incompatible types in comparison expression (different address spaces):

kernel/cgroup/dev.c:423:9: sparse: struct list_head [noderef] __rcu *
kernel/cgroup/dev.c:423:9: sparse: struct list_head *
kernel/cgroup/dev.c: note: in included file (through include/linux/smp.h, include/linux/lockdep.h, include/linux/spinlock.h, ...):
include/linux/list.h:83:21: sparse: sparse: self-comparison always evaluates to true include/linux/list.h:83:21: sparse: sparse: self-comparison always evaluates to true include/linux/list.h:83:21: sparse: sparse: self-comparison always evaluates to true

vim +423 kernel/cgroup/dev.c

400
401 /**
402 * dev_cgroup_unregister_device() - Unregister a previously registered device.
403 * @cgdev: The device to unregister.
404 *
405 * This function undoes dev_cgroup_register_device.
406 */
407 void dev_cgroup_unregister_device(struct dev_cgroup_device *cgdev)
408 {
409 struct devcg_device *dev;
410 struct list_head *entry;
411
412 if (!cgdev || !cgdev->priv)
413 return;
414
415 dev = cgdev->priv;
416 cgdev->priv = NULL;
417
418 spin_lock(&devcg_lock);
419
420 /* Remove from global device list */
421 list_del_rcu(&dev->dev_node);
422

 > 423		list_for_each_rcu(entry, &dev->pools) {

424 struct dev_cgroup_pool_state *pool =
425 container_of(entry, typeof(*pool), dev_node);
426
427 list_del_rcu(&pool->css_node);
428 }
429
430 /*
431 * Ensure any RCU based lookups fail. Additionally,
432 * no new pools should be added to the dead device
433 * by get_cg_pool_unlocked.
434 */
435 dev->unregistered = true;
436 spin_unlock(&devcg_lock);
437
438 kref_put(&dev->ref, devcg_free_device);
439 }
440 EXPORT_SYMBOL_GPL(dev_cgroup_unregister_device);
441

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#5 lore raw

Re: [PATCH 1/7] kernel/cgroup: Add "dev" memory accounting cgroup

In reply to Maarten Lankhorst
Quoted 630 lines · Maarten Lankhorst

The initial version was based roughly on the rdma and misc cgroup controllers, with a lot of the accounting code borrowed from rdma.

The current version is a complete rewrite with page counter; it uses the same min/low/max semantics as the memory cgroup as a result.

There's a small mismatch as TTM uses u64, and page_counter long pages. In practice it's not a problem. 32-bits systems don't really come with

Quoted 1 line

=4GB cards and as long as we're consistently wrong with units, it's

fine. The device page size may not be in the same units as kernel page size, and each region might also have a different page size (VRAM vs GART for example).

The interface is simple:
- populate dev_cgroup_try_charge->regions[..] name and size for each active region, set num_regions accordingly.
- Call (dev,drmm)_cgroup_register_device()
- Use dev_cgroup_try_charge to check if you can allocate a chunk of memory, use dev_cgroup__uncharge when freeing it. This may return an error code, or -EAGAIN when the cgroup limit is reached. In that case a reference to the limiting pool is returned.
- The limiting cs can be used as compare function for dev_cgroup_state_evict_valuable.
- After having evicted enough, drop reference to limiting cs with dev_cgroup_pool_state_put.

This API allows you to limit device resources with cgroups. You can see the supported cards in /sys/fs/cgroup/dev.region.capacity You need to echo +dev to cgroup.subtree_control, and then you can partition memory.

---
  Documentation/admin-guide/cgroup-v2.rst |  51 ++
  Documentation/core-api/cgroup.rst       |   9 +
  Documentation/core-api/index.rst        |   1 +
  Documentation/gpu/drm-compute.rst       |  54 ++
  include/linux/cgroup_dev.h              |  91 +++
  include/linux/cgroup_subsys.h           |   4 +
  include/linux/page_counter.h            |   2 +-
  init/Kconfig                            |   7 +
  kernel/cgroup/Makefile                  |   1 +
  kernel/cgroup/dev.c                     | 893 ++++++++++++++++++++++++
  mm/page_counter.c                       |   4 +-
  11 files changed, 1114 insertions(+), 3 deletions(-)

create mode 100644 Documentation/core-api/cgroup.rst create mode 100644 Documentation/gpu/drm-compute.rst create mode 100644 include/linux/cgroup_dev.h create mode 100644 kernel/cgroup/dev.c

Documentation/admin-guide/cgroup-v2.rst +51 -0
index 69af2173555fb..e8fe79244af9c 100644
@@ -2612,6 +2612,57 @@ RDMA Interface Files
  	  mlx4_0 hca_handle=1 hca_object=20
  	  ocrdma1 hca_handle=1 hca_object=23
 
+DEV
+----
+
+The "dev" controller regulates the distribution and accounting of
+device resources, currently only 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 in bytes.
+
+DEV Interface Files
+~~~~~~~~~~~~~~~~~~~~
+
+  dev.region.max, dev.region.min, dev.region.low
+	A readwrite nested-keyed file that exists for all the cgroups
+	except root that describes current configured resource limit
+	for a device.
+
+	Lines are keyed by device name and are not ordered.
+	Each line contains space separated resource name and its configured
+	limit that can be distributed.
+
+	The following nested keys are defined.
+
+	  ==========	=======================================================
+	  *	 	Maximum amount of bytes that allocatable in this region
+	  ==========	=======================================================
+
+	An example for xe follows::
+
+	  drm/0000:03:00.0 vram0=1073741824 stolen=max
+
+	The semantics are the same as for the memory cgroup controller, and are
+	calculated in the same way.
+
+  dev.region.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 stolen=67108864
+
+  dev.region.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 stolen=8650752
+
  HugeTLB
  -------
 
Documentation/core-api/cgroup.rst new file +9 -0
new file mode 100644
index 0000000000000..475b32255bd68
@@ -0,0 +1,9 @@
+==================
+Cgroup Kernel APIs
+==================
+
+Device Cgroup API (devcg)
+=========================
+.. kernel-doc:: kernel/cgroup/dev.c
+   :export:
+
Documentation/core-api/index.rst +1 -0
index 6a875743dd4b7..dbd6c4f9a6313 100644
@@ -108,6 +108,7 @@ more memory-management documentation in Documentation/mm/index.rst.
     dma-isa-lpc
     swiotlb
     mm-api
+   cgroup
     genalloc
     pin_user_pages
     boot-time-mm
Documentation/gpu/drm-compute.rst new file +54 -0
new file mode 100644
index 0000000000000..116270976ef7a
@@ -0,0 +1,54 @@
+==================================
+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. For now only max is implemented.
+
+What should be noted is that each memory region (tiled memory for example)
+should have its own accounting, using $card key0 = value0 key1 = value1.
+
+The key is set to the regionid set by the driver, for example "tile0".
+For the value of $card, we use drmGetUnique().
include/linux/cgroup_dev.h new file +91 -0
new file mode 100644
index 0000000000000..c6311d1d3ce48
@@ -0,0 +1,91 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef _CGROUP_DEV_H
+#define _CGROUP_DEV_H
+
+#include <linux/types.h>
+#include <linux/llist.h>
+
+struct dev_cgroup_pool_state;
+
+/*
+ * Use 8 as max, because of N^2 lookup when setting things, can be bumped if needed
+ * Identical to TTM_NUM_MEM_TYPES to allow simplifying that code.
+ */
+#define DEVICE_CGROUP_MAX_REGIONS 8
+
+/* Public definition of cgroup device, should not be modified after _register() */
+struct dev_cgroup_device {
+	struct {
+		u64 size;
+		const char *name;
+	} regions[DEVICE_CGROUP_MAX_REGIONS];
+
+	int num_regions;
+
+	/* used by cgroups, do not use */
+	void *priv;
+};
+
+#if IS_ENABLED(CONFIG_CGROUP_DEV)
+int dev_cgroup_register_device(struct dev_cgroup_device *cgdev,
+			       const char *name);
+void dev_cgroup_unregister_device(struct dev_cgroup_device *cgdev);
+int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
+			  u32 index, u64 size,
+			  struct dev_cgroup_pool_state **ret_pool,
+			  struct dev_cgroup_pool_state **ret_limit_pool);
+void dev_cgroup_uncharge(struct dev_cgroup_pool_state *pool,
+			 u32 index, u64 size);
+bool dev_cgroup_state_evict_valuable(struct dev_cgroup_device *dev, int index,
+				     struct dev_cgroup_pool_state *limit_pool,
+				     struct dev_cgroup_pool_state *test_pool,
+				     bool ignore_low, bool *ret_hit_low);
+
+void dev_cgroup_pool_state_put(struct dev_cgroup_pool_state *pool);
+#else
+static inline int
+dev_cgroup_register_device(struct dev_cgroup_device *cgdev,
+			   const char *name)
+{
+	return 0;
+}
+
+static inline void dev_cgroup_unregister_device(struct dev_cgroup_device *cgdev)
+{
+}
+
+static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
+				     u32 index, u64 size,
+				     struct dev_cgroup_pool_state **ret_pool,
+				     struct dev_cgroup_pool_state **ret_limit_pool);
+{
+	*ret_pool = NULL;
+
+	if (ret_limit_pool)
+		*ret_limit_pool = NULL;
+
+	return 0;
+}
+
+static inline void dev_cgroup_uncharge(struct dev_cgroup_pool_state *pool,
+				       u32 index, u64 size)
+{ }
+
+static inline
+bool dev_cgroup_state_evict_valuable(struct dev_cgroup_device *dev, int index,
+				     struct dev_cgroup_pool_state *limit_pool,
+				     struct dev_cgroup_pool_state *test_pool,
+				     bool ignore_low, bool *ret_hit_low)
+{
+	return true;
+}
+
+static inline void dev_cgroup_pool_state_put(struct dev_cgroup_pool_state *pool)
+{ }
+
+#endif
+#endif	/* _CGROUP_DEV_H */
include/linux/cgroup_subsys.h +4 -0
index 4452354872307..898340cfe5843 100644
@@ -65,6 +65,10 @@ SUBSYS(rdma)
  SUBSYS(misc)
  #endif
 
+#if IS_ENABLED(CONFIG_CGROUP_DEV)
+SUBSYS(dev)
+#endif
+
  /*
   * The following subsystems are not supported on the default hierarchy.
   */
include/linux/page_counter.h +1 -1
index 79dbd8bc35a72..d75376a1694ee 100644
@@ -96,7 +96,7 @@ static inline void page_counter_reset_watermark(struct page_counter *counter)
  	counter->watermark = usage;
  }
 
-#ifdef CONFIG_MEMCG
+#if IS_ENABLED(CONFIG_MEMCG) || IS_ENABLED(CONFIG_CGROUP_DEVICE)
  void page_counter_calculate_protection(struct page_counter *root,
  				       struct page_counter *counter,
  				       bool recursive_protection);
init/Kconfig +7 -0
index 530a382ee0feb..2da595facd97f 100644
@@ -1123,6 +1123,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_DEV
+	bool "Device controller"
+	help
+	  Provides the device subsystem controller.
+
+	  ...
+
  config CGROUP_FREEZER
  	bool "Freezer controller"
  	help
kernel/cgroup/Makefile +1 -0
index a5c9359d516f8..441d346fdc51f 100644
@@ -7,4 +7,5 @@ obj-$(CONFIG_CGROUP_RDMA) += rdma.o
  obj-$(CONFIG_CPUSETS) += cpuset.o
  obj-$(CONFIG_CPUSETS_V1) += cpuset-v1.o
  obj-$(CONFIG_CGROUP_MISC) += misc.o
+obj-$(CONFIG_CGROUP_DEV) += dev.o
  obj-$(CONFIG_CGROUP_DEBUG) += debug.o
kernel/cgroup/dev.c new file +268 -0
new file mode 100644
index 0000000000000..e422ccbfbc444
@@ -0,0 +1,893 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2023-2024 Intel Corporation (Maarten Lankhorst <[email protected]>)
+ * Copyright 2024 Red Hat (Maxime Ripard <[email protected]>)
+ * Partially based on the rdma and misc controllers, which bear the following copyrights:
+ *
+ * Copyright 2020 Google LLC
+ * Copyright (C) 2016 Parav Pandit <[email protected]>
+ */
+
+#include <linux/cgroup.h>
+#include <linux/cgroup_dev.h>
+#include <linux/list.h>
+#include <linux/mutex.h>
+#include <linux/page_counter.h>
+#include <linux/parser.h>
+#include <linux/slab.h>
+
+struct devcg_device {
+	/**
+	 * @ref: References keeping the device alive.
+	 * Keeps the device reference alive after a succesful RCU lookup.
+	 */
+	struct kref ref;
+
+	/** @rcu: RCU head for freeing */
+	struct rcu_head rcu;
+
+	/**
+	 * @dev_node: Linked into &devcg_devices list.
+	 * Protected by RCU and global spinlock.
+	 */
+	struct list_head dev_node;
+
+	/**
+	 * @pools: List of pools linked to this device.
+	 * Protected by global spinlock only
+	 */
+	struct list_head pools;
+
+	/**
+	 * @base: Copy of the struct passed on register.
+	 * A copy is made to prevent lifetime issues. devcg_device may
+	 * be kept alive when changing cgroups values concurrently through
+	 * rcu lookups.
+	 */
+	struct dev_cgroup_device base;
+
+	/** @name: Name describing the node, set by dev_cgroup_register_device */
+	const char *name;
+
+	/**
+	 * @unregistered: Whether the device is unregistered by its caller.
+	 * No new pools should be added to the device afterwards.
+	 */
+	bool unregistered;
+};
+
+struct devcg_state {
+	struct cgroup_subsys_state css;
+
+	struct list_head pools;
+};
+
+struct dev_cgroup_pool_state {
+	struct devcg_device *device;
+	struct devcg_state *cs;
+
+	/* css node, RCU protected against device teardown */
+	struct list_head	css_node;
+
+	/* dev node, no RCU protection required */
+	struct list_head	dev_node;
+
+	int num_res, inited;
+	struct rcu_head rcu;
+
+	struct devcg_pool_res {
+		struct page_counter cnt;
+	} resources[];
+};
+
+/*
+ * 3 operations require locking protection:
+ * - Registering and unregistering device to/from list, requires global lock.
+ * - Adding a dev_cgroup_pool_state to a CSS, removing when CSS is freed.
+ * - Adding a dev_cgroup_pool_state to a device list.
+ *
+ * Since for the most common operations RCU provides enough protection, I
+ * do not think more granular locking makes sense. Most protection is offered
+ * by RCU and the lockless operating page_counter.
+ */
+static DEFINE_SPINLOCK(devcg_lock);
+static LIST_HEAD(devcg_devices);
+
+static inline struct devcg_state *
+css_to_devcs(struct cgroup_subsys_state *css)
+{
+	return container_of(css, struct devcg_state, css);
+}
+
+static inline struct devcg_state *get_current_devcs(void)
+{
+	return css_to_devcs(task_get_css(current, dev_cgrp_id));
+}
+
+static struct devcg_state *parent_devcs(struct devcg_state *cg)
+{
+	return cg->css.parent ? css_to_devcs(cg->css.parent) : NULL;
+}
+
+static void free_cg_pool(struct dev_cgroup_pool_state *pool)
+{
+	list_del(&pool->dev_node);
+	kfree(pool);
+}
+
+static void
+set_resource_min(struct dev_cgroup_pool_state *pool, int i, u64 val)
+{
+	page_counter_set_min(&pool->resources[i].cnt, val);
+}
+
+static void
+set_resource_low(struct dev_cgroup_pool_state *pool, int i, u64 val)
+{
+	page_counter_set_low(&pool->resources[i].cnt, val);
+}
+
+static void
+set_resource_max(struct dev_cgroup_pool_state *pool, int i, u64 val)
+{
+	page_counter_set_max(&pool->resources[i].cnt, val);
+}
+
+static u64 get_resource_low(struct dev_cgroup_pool_state *pool, int idx)
+{
+	return pool ? READ_ONCE(pool->resources[idx].cnt.low) : 0;
+}
+
+static u64 get_resource_min(struct dev_cgroup_pool_state *pool, int idx)
+{
+	return pool ? READ_ONCE(pool->resources[idx].cnt.min) : 0;
+}
+
+static u64 get_resource_max(struct dev_cgroup_pool_state *pool, int idx)
+{
+	return pool ? READ_ONCE(pool->resources[idx].cnt.max) : PAGE_COUNTER_MAX;
+}
+
+static u64 get_resource_current(struct dev_cgroup_pool_state *pool, int idx)
+{
+	return pool ? page_counter_read(&pool->resources[idx].cnt) : 0;
+}
+
+static void reset_all_resource_limits(struct dev_cgroup_pool_state *rpool)
+{
+	int i;
+
+	for (i = 0; i < rpool->num_res; i++) {
+		set_resource_min(rpool, i, 0);
+		set_resource_low(rpool, i, 0);
+		set_resource_max(rpool, i, PAGE_COUNTER_MAX);
+	}
+}
+
+static void devcs_offline(struct cgroup_subsys_state *css)
+{
+	struct devcg_state *devcs = css_to_devcs(css);
+	struct dev_cgroup_pool_state *pool;
+
+	rcu_read_lock();
+	list_for_each_entry_rcu(pool, &devcs->pools, css_node)
+		reset_all_resource_limits(pool);
+	rcu_read_unlock();
+}
+
+static void devcs_free(struct cgroup_subsys_state *css)
+{
+	struct devcg_state *devcs = css_to_devcs(css);
+	struct dev_cgroup_pool_state *pool, *next;
+
+	spin_lock(&devcg_lock);
+	list_for_each_entry_safe(pool, next, &devcs->pools, css_node) {
+		/*
+		 *The pool is dead and all references are 0,
+		 * no need for RCU protection with list_del_rcu or freeing.
+		 */
+		list_del(&pool->css_node);
+		free_cg_pool(pool);
+	}
+	spin_unlock(&devcg_lock);
+
+	kfree(devcs);
+}
+
+static struct cgroup_subsys_state *
+devcs_alloc(struct cgroup_subsys_state *parent_css)
+{
+	struct devcg_state *devcs = kzalloc(sizeof(*devcs), GFP_KERNEL);
+	if (!devcs)
+		return ERR_PTR(-ENOMEM);
+
+	INIT_LIST_HEAD(&devcs->pools);
+	return &devcs->css;
+}
+
+static struct dev_cgroup_pool_state *
+find_cg_pool_locked(struct devcg_state *devcs, struct devcg_device *dev)
+{
+	struct dev_cgroup_pool_state *pool;
+
+	list_for_each_entry_rcu(pool, &devcs->pools, css_node, spin_is_locked(&devcg_lock))
+		if (pool->device == dev)
+			return pool;
+
+	return NULL;
+}
+
+static struct dev_cgroup_pool_state *pool_parent(struct dev_cgroup_pool_state *pool)
+{
+	if (!pool->resources[0].cnt.parent)
+		return NULL;
+
+	return container_of(pool->resources[0].cnt.parent, typeof(*pool), resources[0].cnt);
+}
+
+/**
+ * dev_cgroup_state_evict_valuable() - Check if we should evict from test_pool
+ * @dev: &dev_cgroup_device
+ * @index: The index number of the region being tested.
+ * @limit_pool: The pool for which we hit limits
+ * @test_pool: The pool for which to test
+ * @ignore_low: Whether we have to respect low watermarks.
+ * @ret_hit_low: Pointer to whether it makes sense to consider low watermark.
+ *
+ * This function returns true if we can evict from @test_pool, false if not.
+ * When returning false and @ignore_low is false, @ret_hit_low may
+ * be set to true to indicate this function can be retried with @ignore_low
+ * set to true.
+ *
+ * Return: bool
+ */
+bool dev_cgroup_state_evict_valuable(struct dev_cgroup_device *dev, int index,
+				     struct dev_cgroup_pool_state *limit_pool,
+				     struct dev_cgroup_pool_state *test_pool,
+				     bool ignore_low, bool *ret_hit_low)
+{
+	struct dev_cgroup_pool_state *pool = test_pool;
+	struct page_counter *climit, *ctest;
+	u64 used, min, low;
+
+	/* Can always evict from current pool, despite limits */
+	if (limit_pool == test_pool)
+		return true;
+
+	if (limit_pool) {
+		if (!parent_devcs(limit_pool->cs))
+			return true;
+
+		for (pool = test_pool; pool && limit_pool != pool; pool = pool_parent(pool))
+			{}
+
+		if (!pool)
+			return false;
+	} else {
+		/*
+		 * If there is no cgroup limiting memory usage, use the root

kernel/cgroup/dev.c line 278

Quoted 10 lines
kernel/cgroup/dev.c new file +10 -0
+		 * cgroup instead for limit calculations.
+		 */
+		for (limit_pool = test_pool; pool_parent(limit_pool); limit_pool = pool_parent(limit_pool))
+			{}
+	}
+
+	climit = &limit_pool->resources[index].cnt;
+	ctest = &test_pool->resources[index].cnt;
+
+	page_counter_calculate_protection(climit, ctest, true);

I realized we can't do this. As the documentation for page_counter_calculate_protection states:

Quoted 2 lines

WARNING: This function is not stateless! It can only be used as part
of a top-down tree iteration, not for isolated queries.

I authored a fix with [1], though I'm not super happy with having to iterate through the entire (sub-)hierarchy like this every time we consider eviction. If anyone has a better idea, feel free to propose it.

This branch also contains another idea [2][3] I've been playing around with. Essentially, what I'm trying to solve is TTM preferring to use system memory over evicting VRAM, even if the new VRAM allocation would be protected from eviction by low/min memory protection. In my testing, it leads to a better experience to try evicting unprotected allocations immediately in that case. I'm fine with this being follow-up work, but given that the patchset is still in a rather early stage I thought I'd pitch this now.

Thanks,
Friedrich

[1] https://gitlab.freedesktop.org/pixelcluster/linux/-/commit/1adc35adc2a7d05260275d69f139450c8af5dc3b
[2] https://gitlab.freedesktop.org/pixelcluster/linux/-/commit/3d2cdd25d62c539d80055d615bd0912e5907f639
[3] https://gitlab.freedesktop.org/pixelcluster/linux/-/commit/54446cc696fb8b2cab27b3dcc992a6738f5392ad

author maintainer
#6 lore raw

Re: [PATCH 1/7] kernel/cgroup: Add "dev" memory accounting cgroup

Den 2024-10-28 kl. 15:53, skrev Friedrich Vock:

Quoted 709 lines · Maarten Lankhorst
Quoted 692 lines

The initial version was based roughly on the rdma and misc cgroup controllers, with a lot of the accounting code borrowed from rdma.

The current version is a complete rewrite with page counter; it uses the same min/low/max semantics as the memory cgroup as a result.

There's a small mismatch as TTM uses u64, and page_counter long pages. In practice it's not a problem. 32-bits systems don't really come with

Quoted 1 line

=4GB cards and as long as we're consistently wrong with units, it's

fine. The device page size may not be in the same units as kernel page size, and each region might also have a different page size (VRAM vs GART for example).

The interface is simple:
- populate dev_cgroup_try_charge->regions[..] name and size for each active
   region, set num_regions accordingly.
- Call (dev,drmm)_cgroup_register_device()
- Use dev_cgroup_try_charge to check if you can allocate a chunk of memory,
   use dev_cgroup__uncharge when freeing it. This may return an error code, or -EAGAIN when the cgroup limit is reached. In that case a reference to the limiting pool is returned.
- The limiting cs can be used as compare function for dev_cgroup_state_evict_valuable.
- After having evicted enough, drop reference to limiting cs with dev_cgroup_pool_state_put.

This API allows you to limit device resources with cgroups. You can see the supported cards in /sys/fs/cgroup/dev.region.capacity You need to echo +dev to cgroup.subtree_control, and then you can partition memory.

---
  Documentation/admin-guide/cgroup-v2.rst |  51 ++ Documentation/core-api/cgroup.rst       |   9 + Documentation/core-api/index.rst        |   1 + Documentation/gpu/drm-compute.rst       |  54 ++ include/linux/cgroup_dev.h              |  91 +++ include/linux/cgroup_subsys.h           |   4 + include/linux/page_counter.h            |   2 +- init/Kconfig                            |   7 + kernel/cgroup/Makefile                  |   1 + kernel/cgroup/dev.c                     | 893 ++++++++++++++++++++++++ mm/page_counter.c                       |   4 +-

  11 files changed, 1114 insertions(+), 3 deletions(-)

  create mode 100644 Documentation/core-api/cgroup.rst create mode 100644 Documentation/gpu/drm-compute.rst create mode 100644 include/linux/cgroup_dev.h create mode 100644 kernel/cgroup/dev.c

Documentation/admin-guide/cgroup-v2.rst +0 -0
index 69af2173555fb..e8fe79244af9c 100644
@@ -2612,6 +2612,57 @@ RDMA Interface Files

        mlx4_0 hca_handle=1 hca_object=20
        ocrdma1 hca_handle=1 hca_object=23

Documentation/admin-guide/cgroup-v2.rst +23 -0
+DEV
+----
+
+The "dev" controller regulates the distribution and accounting of
+device resources, currently only 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 in bytes.
+
+DEV Interface Files
+~~~~~~~~~~~~~~~~~~~~
+
+  dev.region.max, dev.region.min, dev.region.low
+    A readwrite nested-keyed file that exists for all the cgroups
+    except root that describes current configured resource limit
+    for a device.
+
+    Lines are keyed by device name and are not ordered.
+    Each line contains space separated resource name and its configured
+    limit that can be distributed.
+
+    The following nested keys are defined.
+
+      ==========    

=======================================================
+      *         Maximum amount of bytes that allocatable in this region +      ==========
=======================================================

Documentation/admin-guide/cgroup-v2.rst +6 -0
+
+    An example for xe follows::
+
+      drm/0000:03:00.0 vram0=1073741824 stolen=max
+
+    The semantics are the same as for the memory cgroup controller, 

and are
+    calculated in the same way.

Documentation/admin-guide/cgroup-v2.rst +19 -0
+
+  dev.region.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 stolen=67108864
+
+  dev.region.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 stolen=8650752
+

  HugeTLB
  -------

Documentation/core-api/cgroup.rst new file +9 -0
new file mode 100644
index 0000000000000..475b32255bd68
@@ -0,0 +1,9 @@
+==================
+Cgroup Kernel APIs
+==================
+
+Device Cgroup API (devcg)
+=========================
+.. kernel-doc:: kernel/cgroup/dev.c
+   :export:
+
Documentation/core-api/index.rst +0 -0
index 6a875743dd4b7..dbd6c4f9a6313 100644
@@ -108,6 +108,7 @@ more memory-management documentation in 

Documentation/mm/index.rst.
     dma-isa-lpc
     swiotlb
     mm-api
+   cgroup
     genalloc
     pin_user_pages
     boot-time-mm

Documentation/gpu/drm-compute.rst new file +5 -0
new file mode 100644
index 0000000000000..116270976ef7a
@@ -0,0 +1,54 @@
+==================================
+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,

Documentation/gpu/drm-compute.rst new file +3 -0
+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

Documentation/gpu/drm-compute.rst new file +3 -0
+not to be preempted or swapped out at all.
+
+This means that it differs from what is described in driver-api/dma- 

buf.rst.

Documentation/gpu/drm-compute.rst new file +2 -0
+
+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

Documentation/gpu/drm-compute.rst new file +3 -0
+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

Documentation/gpu/drm-compute.rst new file +3 -0
+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

Documentation/gpu/drm-compute.rst new file +3 -0
+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

Documentation/gpu/drm-compute.rst new file +3 -0
+older compute jobs to start a new one.
+
+If job preemption and recoverable pagefaults are not available, those 

are the

Documentation/gpu/drm-compute.rst new file +4 -0
+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

Documentation/gpu/drm-compute.rst new file +8 -0
+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. For now only max is implemented.
+
+What should be noted is that each memory region (tiled memory for 

example)

Documentation/gpu/drm-compute.rst new file +4 -0
+should have its own accounting, using $card key0 = value0 key1 = value1.
+
+The key is set to the regionid set by the driver, for example "tile0".
+For the value of $card, we use drmGetUnique().
include/linux/cgroup_dev.h new file +15 -0
new file mode 100644
index 0000000000000..c6311d1d3ce48
@@ -0,0 +1,91 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef _CGROUP_DEV_H
+#define _CGROUP_DEV_H
+
+#include <linux/types.h>
+#include <linux/llist.h>
+
+struct dev_cgroup_pool_state;
+
+/*
+ * Use 8 as max, because of N^2 lookup when setting things, can be 

bumped if needed
+ * Identical to TTM_NUM_MEM_TYPES to allow simplifying that code.
+ */

include/linux/cgroup_dev.h new file +3 -0
+#define DEVICE_CGROUP_MAX_REGIONS 8
+
+/* Public definition of cgroup device, should not be modified after 

_register() */

include/linux/cgroup_dev.h new file +23 -0
+struct dev_cgroup_device {
+    struct {
+        u64 size;
+        const char *name;
+    } regions[DEVICE_CGROUP_MAX_REGIONS];
+
+    int num_regions;
+
+    /* used by cgroups, do not use */
+    void *priv;
+};
+
+#if IS_ENABLED(CONFIG_CGROUP_DEV)
+int dev_cgroup_register_device(struct dev_cgroup_device *cgdev,
+                   const char *name);
+void dev_cgroup_unregister_device(struct dev_cgroup_device *cgdev);
+int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
+              u32 index, u64 size,
+              struct dev_cgroup_pool_state **ret_pool,
+              struct dev_cgroup_pool_state **ret_limit_pool);
+void dev_cgroup_uncharge(struct dev_cgroup_pool_state *pool,
+             u32 index, u64 size);
+bool dev_cgroup_state_evict_valuable(struct dev_cgroup_device *dev, 

int index,
+                     struct dev_cgroup_pool_state *limit_pool, +                     struct dev_cgroup_pool_state *test_pool, +                     bool ignore_low, bool *ret_hit_low);

include/linux/cgroup_dev.h new file +11 -0
+
+void dev_cgroup_pool_state_put(struct dev_cgroup_pool_state *pool);
+#else
+static inline int
+dev_cgroup_register_device(struct dev_cgroup_device *cgdev,
+               const char *name)
+{
+    return 0;
+}
+
+static inline void dev_cgroup_unregister_device(struct 

dev_cgroup_device *cgdev)

include/linux/cgroup_dev.h new file +17 -0
+{
+}
+
+static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
+                     u32 index, u64 size,
+                     struct dev_cgroup_pool_state **ret_pool,
+                     struct dev_cgroup_pool_state **ret_limit_pool);
+{
+    *ret_pool = NULL;
+
+    if (ret_limit_pool)
+        *ret_limit_pool = NULL;
+
+    return 0;
+}
+
+static inline void dev_cgroup_uncharge(struct dev_cgroup_pool_state 

*pool,
+                       u32 index, u64 size)

include/linux/cgroup_dev.h new file +4 -0
+{ }
+
+static inline
+bool dev_cgroup_state_evict_valuable(struct dev_cgroup_device *dev, 

int index,
+                     struct dev_cgroup_pool_state *limit_pool, +                     struct dev_cgroup_pool_state *test_pool, +                     bool ignore_low, bool *ret_hit_low)

include/linux/cgroup_dev.h new file +5 -0
+{
+    return true;
+}
+
+static inline void dev_cgroup_pool_state_put(struct 

dev_cgroup_pool_state *pool)

include/linux/cgroup_dev.h new file +4 -0
+{ }
+
+#endif
+#endif    /* _CGROUP_DEV_H */
include/linux/cgroup_subsys.h +4 -0
index 4452354872307..898340cfe5843 100644
@@ -65,6 +65,10 @@ SUBSYS(rdma)
   SUBSYS(misc)
   #endif
 
+#if IS_ENABLED(CONFIG_CGROUP_DEV)
+SUBSYS(dev)
+#endif
+
   /*
    * The following subsystems are not supported on the default hierarchy.
    */
include/linux/page_counter.h +1 -1
index 79dbd8bc35a72..d75376a1694ee 100644
@@ -96,7 +96,7 @@ static inline void 
 page_counter_reset_watermark(struct page_counter *counter)
       counter->watermark = usage;
   }
 
-#ifdef CONFIG_MEMCG
+#if IS_ENABLED(CONFIG_MEMCG) || IS_ENABLED(CONFIG_CGROUP_DEVICE)
   void page_counter_calculate_protection(struct page_counter *root,
                          struct page_counter *counter,

                         bool recursive_protection);

init/Kconfig +0 -0
index 530a382ee0feb..2da595facd97f 100644
@@ -1123,6 +1123,13 @@ config CGROUP_RDMA

        Attaching processes with active RDMA resources to the cgroup hierarchy is allowed even if can cross the hierarchy's limit.

init/Kconfig +7 -0
+config CGROUP_DEV
+    bool "Device controller"
+    help
+      Provides the device subsystem controller.
+
+      ...
+

  config CGROUP_FREEZER
      bool "Freezer controller"
      help

kernel/cgroup/Makefile +0 -0
index a5c9359d516f8..441d346fdc51f 100644
@@ -7,4 +7,5 @@ obj-$(CONFIG_CGROUP_RDMA) += rdma.o

  obj-$(CONFIG_CPUSETS) += cpuset.o
  obj-$(CONFIG_CPUSETS_V1) += cpuset-v1.o
  obj-$(CONFIG_CGROUP_MISC) += misc.o
+obj-$(CONFIG_CGROUP_DEV) += dev.o
  obj-$(CONFIG_CGROUP_DEBUG) += debug.o

kernel/cgroup/dev.c new file +3 -0
new file mode 100644
index 0000000000000..e422ccbfbc444
@@ -0,0 +1,893 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2023-2024 Intel Corporation (Maarten Lankhorst 

<[email protected]>)
+ * Copyright 2024 Red Hat (Maxime Ripard <[email protected]>)
+ * Partially based on the rdma and misc controllers, which bear the following copyrights:
+ *
+ * Copyright 2020 Google LLC
+ * Copyright (C) 2016 Parav Pandit <[email protected]>
+ */

kernel/cgroup/dev.c new file +40 -0
+
+#include <linux/cgroup.h>
+#include <linux/cgroup_dev.h>
+#include <linux/list.h>
+#include <linux/mutex.h>
+#include <linux/page_counter.h>
+#include <linux/parser.h>
+#include <linux/slab.h>
+
+struct devcg_device {
+    /**
+     * @ref: References keeping the device alive.
+     * Keeps the device reference alive after a succesful RCU lookup.
+     */
+    struct kref ref;
+
+    /** @rcu: RCU head for freeing */
+    struct rcu_head rcu;
+
+    /**
+     * @dev_node: Linked into &devcg_devices list.
+     * Protected by RCU and global spinlock.
+     */
+    struct list_head dev_node;
+
+    /**
+     * @pools: List of pools linked to this device.
+     * Protected by global spinlock only
+     */
+    struct list_head pools;
+
+    /**
+     * @base: Copy of the struct passed on register.
+     * A copy is made to prevent lifetime issues. devcg_device may
+     * be kept alive when changing cgroups values concurrently through
+     * rcu lookups.
+     */
+    struct dev_cgroup_device base;
+
+    /** @name: Name describing the node, set by 

dev_cgroup_register_device */
+    const char *name;

kernel/cgroup/dev.c new file +35 -0
+
+    /**
+     * @unregistered: Whether the device is unregistered by its caller.
+     * No new pools should be added to the device afterwards.
+     */
+    bool unregistered;
+};
+
+struct devcg_state {
+    struct cgroup_subsys_state css;
+
+    struct list_head pools;
+};
+
+struct dev_cgroup_pool_state {
+    struct devcg_device *device;
+    struct devcg_state *cs;
+
+    /* css node, RCU protected against device teardown */
+    struct list_head    css_node;
+
+    /* dev node, no RCU protection required */
+    struct list_head    dev_node;
+
+    int num_res, inited;
+    struct rcu_head rcu;
+
+    struct devcg_pool_res {
+        struct page_counter cnt;
+    } resources[];
+};
+
+/*
+ * 3 operations require locking protection:
+ * - Registering and unregistering device to/from list, requires 

global lock.
+ * - Adding a dev_cgroup_pool_state to a CSS, removing when CSS is freed.
+ * - Adding a dev_cgroup_pool_state to a device list.
+ *
+ * Since for the most common operations RCU provides enough protection, I
+ * do not think more granular locking makes sense. Most protection is offered
+ * by RCU and the lockless operating page_counter.
+ */

kernel/cgroup/dev.c new file +56 -0
+static DEFINE_SPINLOCK(devcg_lock);
+static LIST_HEAD(devcg_devices);
+
+static inline struct devcg_state *
+css_to_devcs(struct cgroup_subsys_state *css)
+{
+    return container_of(css, struct devcg_state, css);
+}
+
+static inline struct devcg_state *get_current_devcs(void)
+{
+    return css_to_devcs(task_get_css(current, dev_cgrp_id));
+}
+
+static struct devcg_state *parent_devcs(struct devcg_state *cg)
+{
+    return cg->css.parent ? css_to_devcs(cg->css.parent) : NULL;
+}
+
+static void free_cg_pool(struct dev_cgroup_pool_state *pool)
+{
+    list_del(&pool->dev_node);
+    kfree(pool);
+}
+
+static void
+set_resource_min(struct dev_cgroup_pool_state *pool, int i, u64 val)
+{
+    page_counter_set_min(&pool->resources[i].cnt, val);
+}
+
+static void
+set_resource_low(struct dev_cgroup_pool_state *pool, int i, u64 val)
+{
+    page_counter_set_low(&pool->resources[i].cnt, val);
+}
+
+static void
+set_resource_max(struct dev_cgroup_pool_state *pool, int i, u64 val)
+{
+    page_counter_set_max(&pool->resources[i].cnt, val);
+}
+
+static u64 get_resource_low(struct dev_cgroup_pool_state *pool, int idx)
+{
+    return pool ? READ_ONCE(pool->resources[idx].cnt.low) : 0;
+}
+
+static u64 get_resource_min(struct dev_cgroup_pool_state *pool, int idx)
+{
+    return pool ? READ_ONCE(pool->resources[idx].cnt.min) : 0;
+}
+
+static u64 get_resource_max(struct dev_cgroup_pool_state *pool, int idx)
+{
+    return pool ? READ_ONCE(pool->resources[idx].cnt.max) : 

PAGE_COUNTER_MAX;

kernel/cgroup/dev.c new file +3 -0
+}
+
+static u64 get_resource_current(struct dev_cgroup_pool_state *pool, 

int idx)

kernel/cgroup/dev.c new file +5 -0
+{
+    return pool ? page_counter_read(&pool->resources[idx].cnt) : 0;
+}
+
+static void reset_all_resource_limits(struct dev_cgroup_pool_state 

*rpool)

kernel/cgroup/dev.c new file +57 -0
+{
+    int i;
+
+    for (i = 0; i < rpool->num_res; i++) {
+        set_resource_min(rpool, i, 0);
+        set_resource_low(rpool, i, 0);
+        set_resource_max(rpool, i, PAGE_COUNTER_MAX);
+    }
+}
+
+static void devcs_offline(struct cgroup_subsys_state *css)
+{
+    struct devcg_state *devcs = css_to_devcs(css);
+    struct dev_cgroup_pool_state *pool;
+
+    rcu_read_lock();
+    list_for_each_entry_rcu(pool, &devcs->pools, css_node)
+        reset_all_resource_limits(pool);
+    rcu_read_unlock();
+}
+
+static void devcs_free(struct cgroup_subsys_state *css)
+{
+    struct devcg_state *devcs = css_to_devcs(css);
+    struct dev_cgroup_pool_state *pool, *next;
+
+    spin_lock(&devcg_lock);
+    list_for_each_entry_safe(pool, next, &devcs->pools, css_node) {
+        /*
+         *The pool is dead and all references are 0,
+         * no need for RCU protection with list_del_rcu or freeing.
+         */
+        list_del(&pool->css_node);
+        free_cg_pool(pool);
+    }
+    spin_unlock(&devcg_lock);
+
+    kfree(devcs);
+}
+
+static struct cgroup_subsys_state *
+devcs_alloc(struct cgroup_subsys_state *parent_css)
+{
+    struct devcg_state *devcs = kzalloc(sizeof(*devcs), GFP_KERNEL);
+    if (!devcs)
+        return ERR_PTR(-ENOMEM);
+
+    INIT_LIST_HEAD(&devcs->pools);
+    return &devcs->css;
+}
+
+static struct dev_cgroup_pool_state *
+find_cg_pool_locked(struct devcg_state *devcs, struct devcg_device *dev)
+{
+    struct dev_cgroup_pool_state *pool;
+
+    list_for_each_entry_rcu(pool, &devcs->pools, css_node, 

spin_is_locked(&devcg_lock))
+        if (pool->device == dev)
+            return pool;

kernel/cgroup/dev.c new file +5 -0
+
+    return NULL;
+}
+
+static struct dev_cgroup_pool_state *pool_parent(struct 

dev_cgroup_pool_state *pool)

kernel/cgroup/dev.c new file +5 -0
+{
+    if (!pool->resources[0].cnt.parent)
+        return NULL;
+
+    return container_of(pool->resources[0].cnt.parent, typeof(*pool), 

resources[0].cnt);

kernel/cgroup/dev.c new file +4 -0
+}
+
+/**
+ * dev_cgroup_state_evict_valuable() - Check if we should evict from 

test_pool
+ * @dev: &dev_cgroup_device
+ * @index: The index number of the region being tested.
+ * @limit_pool: The pool for which we hit limits
+ * @test_pool: The pool for which to test
+ * @ignore_low: Whether we have to respect low watermarks.
+ * @ret_hit_low: Pointer to whether it makes sense to consider low watermark.
+ *
+ * This function returns true if we can evict from @test_pool, false if not.
+ * When returning false and @ignore_low is false, @ret_hit_low may
+ * be set to true to indicate this function can be retried with @ignore_low
+ * set to true.
+ *
+ * Return: bool
+ */
+bool dev_cgroup_state_evict_valuable(struct dev_cgroup_device *dev, int index,
+                     struct dev_cgroup_pool_state *limit_pool, +                     struct dev_cgroup_pool_state *test_pool, +                     bool ignore_low, bool *ret_hit_low)

kernel/cgroup/dev.c new file +14 -0
+{
+    struct dev_cgroup_pool_state *pool = test_pool;
+    struct page_counter *climit, *ctest;
+    u64 used, min, low;
+
+    /* Can always evict from current pool, despite limits */
+    if (limit_pool == test_pool)
+        return true;
+
+    if (limit_pool) {
+        if (!parent_devcs(limit_pool->cs))
+            return true;
+
+        for (pool = test_pool; pool && limit_pool != pool; pool = 

pool_parent(pool))
+            {}
+
+        if (!pool)
+            return false;
+    } else {
+        /*
+         * If there is no cgroup limiting memory usage, use the root +         * cgroup instead for limit calculations.
+         */
+        for (limit_pool = test_pool; pool_parent(limit_pool); limit_pool = pool_parent(limit_pool))
+            {}
+    }

kernel/cgroup/dev.c line 278

Quoted 5 lines
kernel/cgroup/dev.c new file +5 -0
+
+    climit = &limit_pool->resources[index].cnt;
+    ctest = &test_pool->resources[index].cnt;
+
+    page_counter_calculate_protection(climit, ctest, true);

I realized we can't do this. As the documentation for page_counter_calculate_protection states:

Quoted 2 lines

WARNING: This function is not stateless! It can only be used as part of a top-down tree iteration, not for isolated queries.

I authored a fix with [1], though I'm not super happy with having to iterate through the entire (sub-)hierarchy like this every time we consider eviction. If anyone has a better idea, feel free to propose it.

Quoted 8 lines

This branch also contains another idea [2][3] I've been playing around with. Essentially, what I'm trying to solve is TTM preferring to use system memory over evicting VRAM, even if the new VRAM allocation would be protected from eviction by low/min memory protection. In my testing, it leads to a better experience to try evicting unprotected allocations immediately in that case. I'm fine with this being follow-up work, but given that the patchset is still in a rather early stage I thought I'd pitch this now.

Hey,

I don't know if an alternative implementation is needed here. I think the current code is correct. The caller ensures that limit_pool and test_pool are alive.

I believe it's roughly parallel to try_charge, but in 2 parts. As long as the caller serializes call to evict_valuable, current code should be fine?

Kind regards,
Maarten Lankhorst

#7 lore raw

Re: [PATCH 1/7] kernel/cgroup: Add "dev" memory accounting cgroup

Quoted 734 lines · Maarten Lankhorst

Den 2024-10-28 kl. 15:53, skrev Friedrich Vock:

Quoted 723 lines · Maarten Lankhorst
Quoted 706 lines

The initial version was based roughly on the rdma and misc cgroup controllers, with a lot of the accounting code borrowed from rdma.

The current version is a complete rewrite with page counter; it uses the same min/low/max semantics as the memory cgroup as a result.

There's a small mismatch as TTM uses u64, and page_counter long pages. In practice it's not a problem. 32-bits systems don't really come with

Quoted 1 line

=4GB cards and as long as we're consistently wrong with units, it's

fine. The device page size may not be in the same units as kernel page size, and each region might also have a different page size (VRAM vs GART
for example).

The interface is simple:
- populate dev_cgroup_try_charge->regions[..] name and size for each active
   region, set num_regions accordingly.
- Call (dev,drmm)_cgroup_register_device()
- Use dev_cgroup_try_charge to check if you can allocate a chunk of memory,
   use dev_cgroup__uncharge when freeing it. This may return an error code,
   or -EAGAIN when the cgroup limit is reached. In that case a reference to the limiting pool is returned.
- The limiting cs can be used as compare function for dev_cgroup_state_evict_valuable.
- After having evicted enough, drop reference to limiting cs with dev_cgroup_pool_state_put.

This API allows you to limit device resources with cgroups. You can see the supported cards in /sys/fs/cgroup/dev.region.capacity You need to echo +dev to cgroup.subtree_control, and then you can partition memory.

---
  Documentation/admin-guide/cgroup-v2.rst |  51 ++ Documentation/core-api/cgroup.rst       |   9 + Documentation/core-api/index.rst        |   1 + Documentation/gpu/drm-compute.rst       |  54 ++ include/linux/cgroup_dev.h              |  91 +++ include/linux/cgroup_subsys.h           |   4 + include/linux/page_counter.h            |   2 +- init/Kconfig                            |   7 + kernel/cgroup/Makefile                  |   1 + kernel/cgroup/dev.c                     | 893 ++++++++++++++++++++++++ mm/page_counter.c                       |   4 +-

  11 files changed, 1114 insertions(+), 3 deletions(-)

  create mode 100644 Documentation/core-api/cgroup.rst create mode 100644 Documentation/gpu/drm-compute.rst create mode 100644 include/linux/cgroup_dev.h create mode 100644 kernel/cgroup/dev.c

Documentation/ renamed from Documentation/admin-guide/cgroup-v2.rst +0 -0

admin-guide/cgroup-v2.rst
index 69af2173555fb..e8fe79244af9c 100644

Documentation/admin-guide/cgroup-v2.rst +0 -0
@@ -2612,6 +2612,57 @@ RDMA Interface Files

        mlx4_0 hca_handle=1 hca_object=20
        ocrdma1 hca_handle=1 hca_object=23

Documentation/admin-guide/cgroup-v2.rst +23 -0
+DEV
+----
+
+The "dev" controller regulates the distribution and accounting of
+device resources, currently only 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 in bytes.
+
+DEV Interface Files
+~~~~~~~~~~~~~~~~~~~~
+
+  dev.region.max, dev.region.min, dev.region.low
+    A readwrite nested-keyed file that exists for all the cgroups
+    except root that describes current configured resource limit
+    for a device.
+
+    Lines are keyed by device name and are not ordered.
+    Each line contains space separated resource name and its configured
+    limit that can be distributed.
+
+    The following nested keys are defined.
+
+      ==========

=======================================================
+      *         Maximum amount of bytes that allocatable in this region +      ==========
=======================================================

Documentation/admin-guide/cgroup-v2.rst +6 -0
+
+    An example for xe follows::
+
+      drm/0000:03:00.0 vram0=1073741824 stolen=max
+
+    The semantics are the same as for the memory cgroup controller,

and are
+    calculated in the same way.

Documentation/admin-guide/cgroup-v2.rst +19 -0
+
+  dev.region.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 stolen=67108864
+
+  dev.region.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 stolen=8650752
+

  HugeTLB
  -------

Documentation/core- renamed from Documentation/core-api/cgroup.rst +0 -0

api/cgroup.rst
new file mode 100644
index 0000000000000..475b32255bd68

Documentation/core-api/cgroup.rst new file +9 -0
@@ -0,0 +1,9 @@
+==================
+Cgroup Kernel APIs
+==================
+
+Device Cgroup API (devcg)
+=========================
+.. kernel-doc:: kernel/cgroup/dev.c
+   :export:
+
Documentation/core- renamed from Documentation/core-api/index.rst +0 -0

api/index.rst
index 6a875743dd4b7..dbd6c4f9a6313 100644

Documentation/core-api/index.rst +0 -0
@@ -108,6 +108,7 @@ more memory-management documentation in

Documentation/mm/index.rst.
     dma-isa-lpc
     swiotlb
     mm-api
+   cgroup
     genalloc
     pin_user_pages
     boot-time-mm

Documentation/gpu/ renamed from Documentation/gpu/drm-compute.rst +0 -0

drm-compute.rst
new file mode 100644
index 0000000000000..116270976ef7a

Documentation/gpu/drm-compute.rst new file +5 -0
@@ -0,0 +1,54 @@
+==================================
+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,

Documentation/gpu/drm-compute.rst new file +3 -0
+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

Documentation/gpu/drm-compute.rst new file +3 -0
+not to be preempted or swapped out at all.
+
+This means that it differs from what is described in driver-api/dma-

buf.rst.

Documentation/gpu/drm-compute.rst new file +2 -0
+
+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

Documentation/gpu/drm-compute.rst new file +3 -0
+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

Documentation/gpu/drm-compute.rst new file +3 -0
+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

Documentation/gpu/drm-compute.rst new file +3 -0
+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

Documentation/gpu/drm-compute.rst new file +3 -0
+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

Documentation/gpu/drm-compute.rst new file +3 -0
+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

Documentation/gpu/drm-compute.rst new file +8 -0
+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. For now only max is implemented.
+
+What should be noted is that each memory region (tiled memory for

example)
+should have its own accounting, using $card key0 = value0 key1 = value1.

Documentation/gpu/drm-compute.rst new file +3 -0
+
+The key is set to the regionid set by the driver, for example "tile0".
+For the value of $card, we use drmGetUnique().
include/linux/cgroup_dev.h new file +15 -0
new file mode 100644
index 0000000000000..c6311d1d3ce48
@@ -0,0 +1,91 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef _CGROUP_DEV_H
+#define _CGROUP_DEV_H
+
+#include <linux/types.h>
+#include <linux/llist.h>
+
+struct dev_cgroup_pool_state;
+
+/*
+ * Use 8 as max, because of N^2 lookup when setting things, can be

bumped if needed
+ * Identical to TTM_NUM_MEM_TYPES to allow simplifying that code.
+ */

include/linux/cgroup_dev.h new file +3 -0
+#define DEVICE_CGROUP_MAX_REGIONS 8
+
+/* Public definition of cgroup device, should not be modified after

_register() */

include/linux/cgroup_dev.h new file +23 -0
+struct dev_cgroup_device {
+    struct {
+        u64 size;
+        const char *name;
+    } regions[DEVICE_CGROUP_MAX_REGIONS];
+
+    int num_regions;
+
+    /* used by cgroups, do not use */
+    void *priv;
+};
+
+#if IS_ENABLED(CONFIG_CGROUP_DEV)
+int dev_cgroup_register_device(struct dev_cgroup_device *cgdev,
+                   const char *name);
+void dev_cgroup_unregister_device(struct dev_cgroup_device *cgdev);
+int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
+              u32 index, u64 size,
+              struct dev_cgroup_pool_state **ret_pool,
+              struct dev_cgroup_pool_state **ret_limit_pool);
+void dev_cgroup_uncharge(struct dev_cgroup_pool_state *pool,
+             u32 index, u64 size);
+bool dev_cgroup_state_evict_valuable(struct dev_cgroup_device *dev,

int index,
+                     struct dev_cgroup_pool_state *limit_pool, +                     struct dev_cgroup_pool_state *test_pool, +                     bool ignore_low, bool *ret_hit_low);

include/linux/cgroup_dev.h new file +11 -0
+
+void dev_cgroup_pool_state_put(struct dev_cgroup_pool_state *pool);
+#else
+static inline int
+dev_cgroup_register_device(struct dev_cgroup_device *cgdev,
+               const char *name)
+{
+    return 0;
+}
+
+static inline void dev_cgroup_unregister_device(struct

dev_cgroup_device *cgdev)

include/linux/cgroup_dev.h new file +17 -0
+{
+}
+
+static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
+                     u32 index, u64 size,
+                     struct dev_cgroup_pool_state **ret_pool,
+                     struct dev_cgroup_pool_state **ret_limit_pool);
+{
+    *ret_pool = NULL;
+
+    if (ret_limit_pool)
+        *ret_limit_pool = NULL;
+
+    return 0;
+}
+
+static inline void dev_cgroup_uncharge(struct dev_cgroup_pool_state

*pool,
+                       u32 index, u64 size)

include/linux/cgroup_dev.h new file +4 -0
+{ }
+
+static inline
+bool dev_cgroup_state_evict_valuable(struct dev_cgroup_device *dev,

int index,
+                     struct dev_cgroup_pool_state *limit_pool, +                     struct dev_cgroup_pool_state *test_pool, +                     bool ignore_low, bool *ret_hit_low)

include/linux/cgroup_dev.h new file +5 -0
+{
+    return true;
+}
+
+static inline void dev_cgroup_pool_state_put(struct

dev_cgroup_pool_state *pool)

include/linux/cgroup_dev.h new file +4 -0
+{ }
+
+#endif
+#endif    /* _CGROUP_DEV_H */
include/linux/ renamed from include/linux/cgroup_subsys.h +0 -0

cgroup_subsys.h
index 4452354872307..898340cfe5843 100644

include/linux/cgroup_subsys.h +4 -0
@@ -65,6 +65,10 @@ SUBSYS(rdma)
   SUBSYS(misc)
   #endif
 
+#if IS_ENABLED(CONFIG_CGROUP_DEV)
+SUBSYS(dev)
+#endif
+
   /*
    * The following subsystems are not supported on the default

hierarchy.
   */

include/linux/page_counter.h +1 -1
index 79dbd8bc35a72..d75376a1694ee 100644
@@ -96,7 +96,7 @@ static inline void
 page_counter_reset_watermark(struct page_counter *counter)
       counter->watermark = usage;
   }
 
-#ifdef CONFIG_MEMCG
+#if IS_ENABLED(CONFIG_MEMCG) || IS_ENABLED(CONFIG_CGROUP_DEVICE)
   void page_counter_calculate_protection(struct page_counter *root,
                          struct page_counter *counter,

                         bool recursive_protection);

init/Kconfig +0 -0
index 530a382ee0feb..2da595facd97f 100644
@@ -1123,6 +1123,13 @@ config CGROUP_RDMA

        Attaching processes with active RDMA resources to the cgroup hierarchy is allowed even if can cross the hierarchy's limit.

init/Kconfig +7 -0
+config CGROUP_DEV
+    bool "Device controller"
+    help
+      Provides the device subsystem controller.
+
+      ...
+

  config CGROUP_FREEZER
      bool "Freezer controller"
      help

kernel/cgroup/Makefile +0 -0
index a5c9359d516f8..441d346fdc51f 100644
@@ -7,4 +7,5 @@ obj-$(CONFIG_CGROUP_RDMA) += rdma.o

  obj-$(CONFIG_CPUSETS) += cpuset.o
  obj-$(CONFIG_CPUSETS_V1) += cpuset-v1.o
  obj-$(CONFIG_CGROUP_MISC) += misc.o
+obj-$(CONFIG_CGROUP_DEV) += dev.o
  obj-$(CONFIG_CGROUP_DEBUG) += debug.o

kernel/cgroup/dev.c new file +3 -0
new file mode 100644
index 0000000000000..e422ccbfbc444
@@ -0,0 +1,893 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2023-2024 Intel Corporation (Maarten Lankhorst

<[email protected]>)
+ * Copyright 2024 Red Hat (Maxime Ripard <[email protected]>)
+ * Partially based on the rdma and misc controllers, which bear the following copyrights:
+ *
+ * Copyright 2020 Google LLC
+ * Copyright (C) 2016 Parav Pandit <[email protected]>
+ */

kernel/cgroup/dev.c new file +40 -0
+
+#include <linux/cgroup.h>
+#include <linux/cgroup_dev.h>
+#include <linux/list.h>
+#include <linux/mutex.h>
+#include <linux/page_counter.h>
+#include <linux/parser.h>
+#include <linux/slab.h>
+
+struct devcg_device {
+    /**
+     * @ref: References keeping the device alive.
+     * Keeps the device reference alive after a succesful RCU lookup.
+     */
+    struct kref ref;
+
+    /** @rcu: RCU head for freeing */
+    struct rcu_head rcu;
+
+    /**
+     * @dev_node: Linked into &devcg_devices list.
+     * Protected by RCU and global spinlock.
+     */
+    struct list_head dev_node;
+
+    /**
+     * @pools: List of pools linked to this device.
+     * Protected by global spinlock only
+     */
+    struct list_head pools;
+
+    /**
+     * @base: Copy of the struct passed on register.
+     * A copy is made to prevent lifetime issues. devcg_device may
+     * be kept alive when changing cgroups values concurrently through
+     * rcu lookups.
+     */
+    struct dev_cgroup_device base;
+
+    /** @name: Name describing the node, set by

dev_cgroup_register_device */
+    const char *name;

kernel/cgroup/dev.c new file +35 -0
+
+    /**
+     * @unregistered: Whether the device is unregistered by its caller.
+     * No new pools should be added to the device afterwards.
+     */
+    bool unregistered;
+};
+
+struct devcg_state {
+    struct cgroup_subsys_state css;
+
+    struct list_head pools;
+};
+
+struct dev_cgroup_pool_state {
+    struct devcg_device *device;
+    struct devcg_state *cs;
+
+    /* css node, RCU protected against device teardown */
+    struct list_head    css_node;
+
+    /* dev node, no RCU protection required */
+    struct list_head    dev_node;
+
+    int num_res, inited;
+    struct rcu_head rcu;
+
+    struct devcg_pool_res {
+        struct page_counter cnt;
+    } resources[];
+};
+
+/*
+ * 3 operations require locking protection:
+ * - Registering and unregistering device to/from list, requires

global lock.
+ * - Adding a dev_cgroup_pool_state to a CSS, removing when CSS is freed.
+ * - Adding a dev_cgroup_pool_state to a device list.
+ *
+ * Since for the most common operations RCU provides enough protection, I
+ * do not think more granular locking makes sense. Most protection is offered
+ * by RCU and the lockless operating page_counter.
+ */

kernel/cgroup/dev.c new file +44 -0
+static DEFINE_SPINLOCK(devcg_lock);
+static LIST_HEAD(devcg_devices);
+
+static inline struct devcg_state *
+css_to_devcs(struct cgroup_subsys_state *css)
+{
+    return container_of(css, struct devcg_state, css);
+}
+
+static inline struct devcg_state *get_current_devcs(void)
+{
+    return css_to_devcs(task_get_css(current, dev_cgrp_id));
+}
+
+static struct devcg_state *parent_devcs(struct devcg_state *cg)
+{
+    return cg->css.parent ? css_to_devcs(cg->css.parent) : NULL;
+}
+
+static void free_cg_pool(struct dev_cgroup_pool_state *pool)
+{
+    list_del(&pool->dev_node);
+    kfree(pool);
+}
+
+static void
+set_resource_min(struct dev_cgroup_pool_state *pool, int i, u64 val)
+{
+    page_counter_set_min(&pool->resources[i].cnt, val);
+}
+
+static void
+set_resource_low(struct dev_cgroup_pool_state *pool, int i, u64 val)
+{
+    page_counter_set_low(&pool->resources[i].cnt, val);
+}
+
+static void
+set_resource_max(struct dev_cgroup_pool_state *pool, int i, u64 val)
+{
+    page_counter_set_max(&pool->resources[i].cnt, val);
+}
+
+static u64 get_resource_low(struct dev_cgroup_pool_state *pool, int

idx)

kernel/cgroup/dev.c new file +5 -0
+{
+    return pool ? READ_ONCE(pool->resources[idx].cnt.low) : 0;
+}
+
+static u64 get_resource_min(struct dev_cgroup_pool_state *pool, int

idx)

kernel/cgroup/dev.c new file +5 -0
+{
+    return pool ? READ_ONCE(pool->resources[idx].cnt.min) : 0;
+}
+
+static u64 get_resource_max(struct dev_cgroup_pool_state *pool, int

idx)
+{
+    return pool ? READ_ONCE(pool->resources[idx].cnt.max) :
PAGE_COUNTER_MAX;

kernel/cgroup/dev.c new file +3 -0
+}
+
+static u64 get_resource_current(struct dev_cgroup_pool_state *pool,

int idx)

kernel/cgroup/dev.c new file +5 -0
+{
+    return pool ? page_counter_read(&pool->resources[idx].cnt) : 0;
+}
+
+static void reset_all_resource_limits(struct dev_cgroup_pool_state

*rpool)

kernel/cgroup/dev.c new file +53 -0
+{
+    int i;
+
+    for (i = 0; i < rpool->num_res; i++) {
+        set_resource_min(rpool, i, 0);
+        set_resource_low(rpool, i, 0);
+        set_resource_max(rpool, i, PAGE_COUNTER_MAX);
+    }
+}
+
+static void devcs_offline(struct cgroup_subsys_state *css)
+{
+    struct devcg_state *devcs = css_to_devcs(css);
+    struct dev_cgroup_pool_state *pool;
+
+    rcu_read_lock();
+    list_for_each_entry_rcu(pool, &devcs->pools, css_node)
+        reset_all_resource_limits(pool);
+    rcu_read_unlock();
+}
+
+static void devcs_free(struct cgroup_subsys_state *css)
+{
+    struct devcg_state *devcs = css_to_devcs(css);
+    struct dev_cgroup_pool_state *pool, *next;
+
+    spin_lock(&devcg_lock);
+    list_for_each_entry_safe(pool, next, &devcs->pools, css_node) {
+        /*
+         *The pool is dead and all references are 0,
+         * no need for RCU protection with list_del_rcu or freeing.
+         */
+        list_del(&pool->css_node);
+        free_cg_pool(pool);
+    }
+    spin_unlock(&devcg_lock);
+
+    kfree(devcs);
+}
+
+static struct cgroup_subsys_state *
+devcs_alloc(struct cgroup_subsys_state *parent_css)
+{
+    struct devcg_state *devcs = kzalloc(sizeof(*devcs), GFP_KERNEL);
+    if (!devcs)
+        return ERR_PTR(-ENOMEM);
+
+    INIT_LIST_HEAD(&devcs->pools);
+    return &devcs->css;
+}
+
+static struct dev_cgroup_pool_state *
+find_cg_pool_locked(struct devcg_state *devcs, struct devcg_device

*dev)

kernel/cgroup/dev.c new file +4 -0
+{
+    struct dev_cgroup_pool_state *pool;
+
+    list_for_each_entry_rcu(pool, &devcs->pools, css_node,

spin_is_locked(&devcg_lock))
+        if (pool->device == dev)
+            return pool;

kernel/cgroup/dev.c new file +5 -0
+
+    return NULL;
+}
+
+static struct dev_cgroup_pool_state *pool_parent(struct

dev_cgroup_pool_state *pool)

kernel/cgroup/dev.c new file +5 -0
+{
+    if (!pool->resources[0].cnt.parent)
+        return NULL;
+
+    return container_of(pool->resources[0].cnt.parent,

typeof(*pool), resources[0].cnt);

kernel/cgroup/dev.c new file +4 -0
+}
+
+/**
+ * dev_cgroup_state_evict_valuable() - Check if we should evict from

test_pool
+ * @dev: &dev_cgroup_device
+ * @index: The index number of the region being tested.
+ * @limit_pool: The pool for which we hit limits
+ * @test_pool: The pool for which to test
+ * @ignore_low: Whether we have to respect low watermarks.
+ * @ret_hit_low: Pointer to whether it makes sense to consider low watermark.
+ *
+ * This function returns true if we can evict from @test_pool, false if not.
+ * When returning false and @ignore_low is false, @ret_hit_low may
+ * be set to true to indicate this function can be retried with @ignore_low
+ * set to true.
+ *
+ * Return: bool
+ */
+bool dev_cgroup_state_evict_valuable(struct dev_cgroup_device *dev, int index,
+                     struct dev_cgroup_pool_state *limit_pool, +                     struct dev_cgroup_pool_state *test_pool, +                     bool ignore_low, bool *ret_hit_low)

kernel/cgroup/dev.c new file +14 -0
+{
+    struct dev_cgroup_pool_state *pool = test_pool;
+    struct page_counter *climit, *ctest;
+    u64 used, min, low;
+
+    /* Can always evict from current pool, despite limits */
+    if (limit_pool == test_pool)
+        return true;
+
+    if (limit_pool) {
+        if (!parent_devcs(limit_pool->cs))
+            return true;
+
+        for (pool = test_pool; pool && limit_pool != pool; pool =

pool_parent(pool))
+            {}
+
+        if (!pool)
+            return false;
+    } else {
+        /*
+         * If there is no cgroup limiting memory usage, use the root +         * cgroup instead for limit calculations.
+         */
+        for (limit_pool = test_pool; pool_parent(limit_pool); limit_pool = pool_parent(limit_pool))
+            {}
+    }

kernel/cgroup/dev.c line 278

Quoted 5 lines
kernel/cgroup/dev.c new file +5 -0
+
+    climit = &limit_pool->resources[index].cnt;
+    ctest = &test_pool->resources[index].cnt;
+
+    page_counter_calculate_protection(climit, ctest, true);

I realized we can't do this. As the documentation for page_counter_calculate_protection states:

Quoted 2 lines

WARNING: This function is not stateless! It can only be used as part of a top-down tree iteration, not for isolated queries.

I authored a fix with [1], though I'm not super happy with having to iterate through the entire (sub-)hierarchy like this every time we consider eviction. If anyone has a better idea, feel free to propose it.

Quoted 8 lines

This branch also contains another idea [2][3] I've been playing around with. Essentially, what I'm trying to solve is TTM preferring to use system memory over evicting VRAM, even if the new VRAM allocation would be protected from eviction by low/min memory protection. In my testing, it leads to a better experience to try evicting unprotected allocations immediately in that case. I'm fine with this being follow-up work, but given that the patchset is still in a rather early stage I thought I'd pitch this now.

Quoted 6 lines

Hey,

I don't know if an alternative implementation is needed here. I think the current code is correct. The caller ensures that limit_pool and test_pool are alive.

The issue is not about lifetimes. If you look into the implementation of page_counter_calculate_protection, it uses the parent's elow as part of calculating the current node's elow. However, unless we do a top-down iteration, the parent's elow value may be outdated or might never have been calculated at all yet. This is why the comment about requiring a top-down iteration exists, and I don't see why it shouldn't apply to us.

Regards,
Friedrich

Quoted 6 lines

I believe it's roughly parallel to try_charge, but in 2 parts. As long as the caller serializes call to evict_valuable, current code should be fine?

Kind regards,
Maarten Lankhorst

author maintainer
#8 lore raw

PATCH 2/7 drm/drv: Add drmm cgroup registration for dev cgroups.

In reply to Maarten Lankhorst

From: Maxime Ripard <[email protected]>

Drivers will need to register a cgroup device at probe time, so let's give them a drm-managed helper.

---
 drivers/gpu/drm/drm_drv.c | 21 +++++++++++++++++++++
 include/drm/drm_drv.h     |  4 ++++
 2 files changed, 25 insertions(+)
drivers/gpu/drm/drm_drv.c +21 -0
index c2c172eb25df7..251d1d69b09c5 100644
@@ -26,6 +26,7 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
+#include <linux/cgroup_dev.h>
 #include <linux/debugfs.h>
 #include <linux/fs.h>
 #include <linux/module.h>
@@ -820,6 +821,26 @@ void drm_dev_put(struct drm_device *dev)
 }
 EXPORT_SYMBOL(drm_dev_put);
 
+static inline void drmm_cg_unregister_device(struct drm_device *dev, void *arg)
+{
+	dev_cgroup_unregister_device(arg);
+}
+
+int drmm_cgroup_register_device(struct drm_device *dev,
+				struct dev_cgroup_device *cgdev)
+{
+	int ret;
+	char dev_name[32];
+
+	snprintf(dev_name, sizeof(dev_name), "drm/%s", dev->unique);
+	ret = dev_cgroup_register_device(cgdev, dev_name);
+	if (ret)
+		return ret;
+
+	return drmm_add_action_or_reset(dev, drmm_cg_unregister_device, cgdev);
+}
+EXPORT_SYMBOL_GPL(drmm_cgroup_register_device);
+
 static int create_compat_control_link(struct drm_device *dev)
 {
 	struct drm_minor *minor;
include/drm/drm_drv.h +4 -0
index 1bbbcb8e2d231..3e83c7ce7f2d0 100644
@@ -34,6 +34,7 @@
 
 #include <drm/drm_device.h>
 
+struct dev_cgroup_device;
 struct drm_fb_helper;
 struct drm_fb_helper_surface_size;
 struct drm_file;
@@ -438,6 +439,9 @@ void *__devm_drm_dev_alloc(struct device *parent,
 			   const struct drm_driver *driver,
 			   size_t size, size_t offset);
 
+int drmm_cgroup_register_device(struct drm_device *dev,
+				struct dev_cgroup_device *cgdev);
+
 /**
  * devm_drm_dev_alloc - Resource managed allocation of a &drm_device instance
  * @parent: Parent device object
Signature · 1 line
2.45.2
#9 lore raw

Re: [PATCH 2/7] drm/drv: Add drmm cgroup registration for dev cgroups.

Quoted 25 lines · Maarten Lankhorst

From: Maxime Ripard <[email protected]>

Drivers will need to register a cgroup device at probe time, so let's give them a drm-managed helper.

---
 drivers/gpu/drm/drm_drv.c | 21 +++++++++++++++++++++
 include/drm/drm_drv.h     |  4 ++++
 2 files changed, 25 insertions(+)
drivers/gpu/drm/drm_drv.c +1 -0
index c2c172eb25df7..251d1d69b09c5 100644
@@ -26,6 +26,7 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
+#include <linux/cgroup_dev.h>
 #include <linux/debugfs.h>
 #include <linux/fs.h>
 #include <linux/module.h>

drivers/gpu/drm/drm_drv.c void drm_dev_put(struct drm_device *dev) · line 824

Quoted 4 lines
drivers/gpu/drm/drm_drv.c +1 -0
@@ -820,6 +821,26 @@ void drm_dev_put(struct drm_device *dev)
 }
 EXPORT_SYMBOL(drm_dev_put);
 
+static inline void drmm_cg_unregister_device(struct drm_device *dev, void *arg)

Nitpick, inline in .c files is mostly useless, just use static and let the compiler do its job.

BR,
Jani.

Quoted 44 lines
drivers/gpu/drm/drm_drv.c +19 -0
+{
+	dev_cgroup_unregister_device(arg);
+}
+
+int drmm_cgroup_register_device(struct drm_device *dev,
+				struct dev_cgroup_device *cgdev)
+{
+	int ret;
+	char dev_name[32];
+
+	snprintf(dev_name, sizeof(dev_name), "drm/%s", dev->unique);
+	ret = dev_cgroup_register_device(cgdev, dev_name);
+	if (ret)
+		return ret;
+
+	return drmm_add_action_or_reset(dev, drmm_cg_unregister_device, cgdev);
+}
+EXPORT_SYMBOL_GPL(drmm_cgroup_register_device);
+
 static int create_compat_control_link(struct drm_device *dev)
 {
 	struct drm_minor *minor;
include/drm/drm_drv.h +4 -0
index 1bbbcb8e2d231..3e83c7ce7f2d0 100644
@@ -34,6 +34,7 @@
 
 #include <drm/drm_device.h>
 
+struct dev_cgroup_device;
 struct drm_fb_helper;
 struct drm_fb_helper_surface_size;
 struct drm_file;
@@ -438,6 +439,9 @@ void *__devm_drm_dev_alloc(struct device *parent,
 			   const struct drm_driver *driver,
 			   size_t size, size_t offset);
 
+int drmm_cgroup_register_device(struct drm_device *dev,
+				struct dev_cgroup_device *cgdev);
+
 /**
  * devm_drm_dev_alloc - Resource managed allocation of a &drm_device instance
  * @parent: Parent device object
Signature · 1 line
Jani Nikula, Intel
#10 lore raw

Re: [PATCH 2/7] drm/drv: Add drmm cgroup registration for dev cgroups.

In reply to Maarten Lankhorst
kernel test robot noticed the following build errors:

Hi Maarten,

kernel test robot noticed the following build errors:

[auto build test ERROR on drm/drm-next]
[also build test ERROR on drm-exynos/exynos-drm-next next-20241024]
[cannot apply to tj-cgroup/for-next drm-xe/drm-xe-next akpm-mm/mm-everything drm-intel/for-linux-next drm-intel/for-linux-next-fixes drm-misc/drm-misc-next drm-tip/drm-tip linus/master v6.12-rc4] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Maarten-Lankhorst/kernel-cgroup-Add-dev-memory-accounting-cgroup/20241023-155504
base:   git://anongit.freedesktop.org/drm/drm drm-next
patch link:    https://lore.kernel.org/r/20241023075302.27194-3-maarten.lankhorst%40linux.intel.com
patch subject: [PATCH 2/7] drm/drv: Add drmm cgroup registration for dev cgroups.
config: x86_64-rhel-8.3 (https://download.01.org/0day-ci/archive/20241024/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241024/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All error/warnings (new ones prefixed by >>):

In file included from drivers/gpu/drm/drm_drv.c:29:

>> include/linux/cgroup_dev.h:61:12: error: two or more data types in declaration specifiers
      61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
         |            ^~~
>> include/linux/cgroup_dev.h:65:1: error: expected identifier or '(' before '{' token
      65 | {
         | ^
>> include/linux/cgroup_dev.h:61:16: warning: 'dev_cgroup_try_charge' declared 'static' but never defined [-Wunused-function]

61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
| ^~~~~~~~~~~~~~~~~~~~~

vim +61 include/linux/cgroup_dev.h

487073b1855ef4 Maarten Lankhorst 2024-10-23 60

487073b1855ef4 Maarten Lankhorst 2024-10-23 @61  static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
487073b1855ef4 Maarten Lankhorst 2024-10-23  62  				     u32 index, u64 size,
487073b1855ef4 Maarten Lankhorst 2024-10-23  63  				     struct dev_cgroup_pool_state **ret_pool,
487073b1855ef4 Maarten Lankhorst 2024-10-23  64  				     struct dev_cgroup_pool_state **ret_limit_pool);
487073b1855ef4 Maarten Lankhorst 2024-10-23 @65  {
487073b1855ef4 Maarten Lankhorst 2024-10-23  66  	*ret_pool = NULL;
487073b1855ef4 Maarten Lankhorst 2024-10-23  67  
487073b1855ef4 Maarten Lankhorst 2024-10-23  68  	if (ret_limit_pool)
487073b1855ef4 Maarten Lankhorst 2024-10-23  69  		*ret_limit_pool = NULL;
487073b1855ef4 Maarten Lankhorst 2024-10-23  70  
487073b1855ef4 Maarten Lankhorst 2024-10-23  71  	return 0;
487073b1855ef4 Maarten Lankhorst 2024-10-23  72  }
487073b1855ef4 Maarten Lankhorst 2024-10-23  73  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#11 lore raw

Re: [PATCH 2/7] drm/drv: Add drmm cgroup registration for dev cgroups.

In reply to Maarten Lankhorst
kernel test robot noticed the following build errors:

Hi Maarten,

kernel test robot noticed the following build errors:

[auto build test ERROR on drm/drm-next]
[also build test ERROR on drm-exynos/exynos-drm-next next-20241024]
[cannot apply to tj-cgroup/for-next drm-xe/drm-xe-next akpm-mm/mm-everything drm-intel/for-linux-next drm-intel/for-linux-next-fixes drm-misc/drm-misc-next drm-tip/drm-tip linus/master v6.12-rc4] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Maarten-Lankhorst/kernel-cgroup-Add-dev-memory-accounting-cgroup/20241023-155504
base:   git://anongit.freedesktop.org/drm/drm drm-next
patch link:    https://lore.kernel.org/r/20241023075302.27194-3-maarten.lankhorst%40linux.intel.com
patch subject: [PATCH 2/7] drm/drv: Add drmm cgroup registration for dev cgroups.
config: arm64-randconfig-002-20241024 (https://download.01.org/0day-ci/archive/20241024/[email protected]/config)
compiler: aarch64-linux-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241024/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from drivers/gpu/drm/drm_drv.c:29:
include/linux/cgroup_dev.h:61:12: error: two or more data types in declaration specifiers
61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
| ^~~
include/linux/cgroup_dev.h:65:1: error: expected identifier or '(' before '{' token

      65 | {
         | ^
>> include/linux/cgroup_dev.h:61:16: error: 'dev_cgroup_try_charge' declared 'static' but never defined [-Werror=unused-function]

61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
| ^~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

vim +61 include/linux/cgroup_dev.h

487073b1855ef4 Maarten Lankhorst 2024-10-23 60

487073b1855ef4 Maarten Lankhorst 2024-10-23 @61  static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
487073b1855ef4 Maarten Lankhorst 2024-10-23  62  				     u32 index, u64 size,
487073b1855ef4 Maarten Lankhorst 2024-10-23  63  				     struct dev_cgroup_pool_state **ret_pool,
487073b1855ef4 Maarten Lankhorst 2024-10-23  64  				     struct dev_cgroup_pool_state **ret_limit_pool);
487073b1855ef4 Maarten Lankhorst 2024-10-23  65  {
487073b1855ef4 Maarten Lankhorst 2024-10-23  66  	*ret_pool = NULL;
487073b1855ef4 Maarten Lankhorst 2024-10-23  67  
487073b1855ef4 Maarten Lankhorst 2024-10-23  68  	if (ret_limit_pool)
487073b1855ef4 Maarten Lankhorst 2024-10-23  69  		*ret_limit_pool = NULL;
487073b1855ef4 Maarten Lankhorst 2024-10-23  70  
487073b1855ef4 Maarten Lankhorst 2024-10-23  71  	return 0;
487073b1855ef4 Maarten Lankhorst 2024-10-23  72  }
487073b1855ef4 Maarten Lankhorst 2024-10-23  73  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#12 lore raw

PATCH 3/7 drm/ttm: Handle cgroup based eviction in TTM

In reply to Maarten Lankhorst

cgroup resource allocation has to be handled in TTM, so -EAGAIN from cgroups can be converted into -ENOSPC, and the limitcg can be properly evicted in ttm code.

When hitting a resource limit through -EAGAIN, the cgroup for which the limit is hit is also returned. This allows eviction to delete only from cgroups which are a subgroup of the current cgroup.

The returned CSS is used to determine if eviction is valuable for a given resource, and allows TTM to only target specific resources to lower memory usage.

---
 drivers/gpu/drm/ttm/tests/ttm_bo_test.c       | 18 +++---
 .../gpu/drm/ttm/tests/ttm_bo_validate_test.c  |  4 +-
 drivers/gpu/drm/ttm/tests/ttm_resource_test.c |  2 +-
 drivers/gpu/drm/ttm/ttm_bo.c                  | 57 ++++++++++++++++---
 drivers/gpu/drm/ttm/ttm_resource.c            | 24 +++++++-
 include/drm/ttm/ttm_resource.h                | 16 +++++-
 6 files changed, 98 insertions(+), 23 deletions(-)
drivers/gpu/drm/ttm/tests/ttm_bo_test.c +9 -9
index 3139fd9128d84..f8f20d2f61740 100644
@@ -258,13 +258,13 @@ static void ttm_bo_unreserve_basic(struct kunit *test)
 	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE, NULL);
 	bo->priority = bo_prio;
 
-	err = ttm_resource_alloc(bo, place, &res1);
+	err = ttm_resource_alloc(bo, place, &res1, NULL);
 	KUNIT_ASSERT_EQ(test, err, 0);
 
 	bo->resource = res1;
 
 	/* Add a dummy resource to populate LRU */
-	ttm_resource_alloc(bo, place, &res2);
+	ttm_resource_alloc(bo, place, &res2, NULL);
 
 	dma_resv_lock(bo->base.resv, NULL);
 	ttm_bo_unreserve(bo);
@@ -300,12 +300,12 @@ static void ttm_bo_unreserve_pinned(struct kunit *test)
 	dma_resv_lock(bo->base.resv, NULL);
 	ttm_bo_pin(bo);
 
-	err = ttm_resource_alloc(bo, place, &res1);
+	err = ttm_resource_alloc(bo, place, &res1, NULL);
 	KUNIT_ASSERT_EQ(test, err, 0);
 	bo->resource = res1;
 
 	/* Add a dummy resource to the pinned list */
-	err = ttm_resource_alloc(bo, place, &res2);
+	err = ttm_resource_alloc(bo, place, &res2, NULL);
 	KUNIT_ASSERT_EQ(test, err, 0);
 	KUNIT_ASSERT_EQ(test,
 			list_is_last(&res2->lru.link, &priv->ttm_dev->unevictable), 1);
@@ -355,7 +355,7 @@ static void ttm_bo_unreserve_bulk(struct kunit *test)
 	ttm_bo_set_bulk_move(bo1, &lru_bulk_move);
 	dma_resv_unlock(bo1->base.resv);
 
-	err = ttm_resource_alloc(bo1, place, &res1);
+	err = ttm_resource_alloc(bo1, place, &res1, NULL);
 	KUNIT_ASSERT_EQ(test, err, 0);
 	bo1->resource = res1;
 
@@ -363,7 +363,7 @@ static void ttm_bo_unreserve_bulk(struct kunit *test)
 	ttm_bo_set_bulk_move(bo2, &lru_bulk_move);
 	dma_resv_unlock(bo2->base.resv);
 
-	err = ttm_resource_alloc(bo2, place, &res2);
+	err = ttm_resource_alloc(bo2, place, &res2, NULL);
 	KUNIT_ASSERT_EQ(test, err, 0);
 	bo2->resource = res2;
 
@@ -401,7 +401,7 @@ static void ttm_bo_put_basic(struct kunit *test)
 	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE, NULL);
 	bo->type = ttm_bo_type_device;
 
-	err = ttm_resource_alloc(bo, place, &res);
+	err = ttm_resource_alloc(bo, place, &res, NULL);
 	KUNIT_ASSERT_EQ(test, err, 0);
 	bo->resource = res;
 
@@ -518,7 +518,7 @@ static void ttm_bo_pin_unpin_resource(struct kunit *test)
 
 	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE, NULL);
 
-	err = ttm_resource_alloc(bo, place, &res);
+	err = ttm_resource_alloc(bo, place, &res, NULL);
 	KUNIT_ASSERT_EQ(test, err, 0);
 	bo->resource = res;
 
@@ -569,7 +569,7 @@ static void ttm_bo_multiple_pin_one_unpin(struct kunit *test)
 
 	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE, NULL);
 
-	err = ttm_resource_alloc(bo, place, &res);
+	err = ttm_resource_alloc(bo, place, &res, NULL);
 	KUNIT_ASSERT_EQ(test, err, 0);
 	bo->resource = res;
 
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +2 -2
index 1adf18481ea05..3148f5d3dbd66 100644
@@ -542,7 +542,7 @@ static void ttm_bo_validate_no_placement_signaled(struct kunit *test)
 		bo->ttm = old_tt;
 	}
 
-	err = ttm_resource_alloc(bo, place, &bo->resource);
+	err = ttm_resource_alloc(bo, place, &bo->resource, NULL);
 	KUNIT_EXPECT_EQ(test, err, 0);
 	KUNIT_ASSERT_EQ(test, man->usage, size);
 
@@ -603,7 +603,7 @@ static void ttm_bo_validate_no_placement_not_signaled(struct kunit *test)
 	bo = ttm_bo_kunit_init(test, test->priv, size, NULL);
 	bo->type = params->bo_type;
 
-	err = ttm_resource_alloc(bo, place, &bo->resource);
+	err = ttm_resource_alloc(bo, place, &bo->resource, NULL);
 	KUNIT_EXPECT_EQ(test, err, 0);
 
 	placement = kunit_kzalloc(test, sizeof(*placement), GFP_KERNEL);
drivers/gpu/drm/ttm/tests/ttm_resource_test.c +1 -1
index a9f4b81921c3c..e6ea2bd01f07a 100644
@@ -302,7 +302,7 @@ static void ttm_sys_man_free_basic(struct kunit *test)
 	res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
 	KUNIT_ASSERT_NOT_NULL(test, res);
 
-	ttm_resource_alloc(bo, place, &res);
+	ttm_resource_alloc(bo, place, &res, NULL);
 
 	man = ttm_manager_type(priv->devs->ttm_dev, mem_type);
 	man->func->free(man, res);
drivers/gpu/drm/ttm/ttm_bo.c +49 -8

146 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_resource.c +22 -2
index a87665eb28a62..93190f7330b5c 100644
@@ -26,6 +26,7 @@
 #include <linux/io-mapping.h>
 #include <linux/iosys-map.h>
 #include <linux/scatterlist.h>
+#include <linux/cgroup_dev.h>
 
 #include <drm/ttm/ttm_bo.h>
 #include <drm/ttm/ttm_placement.h>
@@ -350,15 +351,29 @@ EXPORT_SYMBOL(ttm_resource_fini);
 
 int ttm_resource_alloc(struct ttm_buffer_object *bo,
 		       const struct ttm_place *place,
-		       struct ttm_resource **res_ptr)
+		       struct ttm_resource **res_ptr,
+		       struct dev_cgroup_pool_state **ret_limit_pool)
 {
 	struct ttm_resource_manager *man =
 		ttm_manager_type(bo->bdev, place->mem_type);
+	struct dev_cgroup_pool_state *pool = NULL;
 	int ret;
 
+	if (man->cgdev) {
+		ret = dev_cgroup_try_charge(man->cgdev, man->cgidx,
+					    bo->base.size, &pool, ret_limit_pool);
+		if (ret)
+			return ret;
+	}
+
 	ret = man->func->alloc(man, bo, place, res_ptr);
-	if (ret)
+	if (ret) {
+		if (pool)
+			dev_cgroup_uncharge(pool, man->cgidx, bo->base.size);
 		return ret;
+	}
+
+	(*res_ptr)->css = pool;
 
 	spin_lock(&bo->bdev->lru_lock);
 	ttm_resource_add_bulk_move(*res_ptr, bo);
@@ -370,6 +385,7 @@ EXPORT_SYMBOL_FOR_TESTS_ONLY(ttm_resource_alloc);
 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
 {
 	struct ttm_resource_manager *man;
+	struct dev_cgroup_pool_state *pool;
 
 	if (!*res)
 		return;
@@ -377,9 +393,13 @@ void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
 	spin_lock(&bo->bdev->lru_lock);
 	ttm_resource_del_bulk_move(*res, bo);
 	spin_unlock(&bo->bdev->lru_lock);
+
+	pool = (*res)->css;
 	man = ttm_manager_type(bo->bdev, (*res)->mem_type);
 	man->func->free(man, *res);
 	*res = NULL;
+	if (man->cgdev)
+		dev_cgroup_uncharge(pool, man->cgidx, bo->base.size);
 }
 EXPORT_SYMBOL(ttm_resource_free);
 
include/drm/ttm/ttm_resource.h +15 -1
index be034be56ba1b..12ff4b66c6f7d 100644
@@ -38,6 +38,7 @@
 #define TTM_MAX_BO_PRIORITY	4U
 #define TTM_NUM_MEM_TYPES 8
 
+struct dev_cgroup_device;
 struct ttm_device;
 struct ttm_resource_manager;
 struct ttm_resource;
@@ -211,6 +212,15 @@ struct ttm_resource_manager {
 	 * bdev->lru_lock.
 	 */
 	uint64_t usage;
+
+	/**
+	 * @cgdev: dev_cgroup_device used for memory accounting, if not NULL.
+	 */
+	struct dev_cgroup_device *cgdev;
+	/**
+	 * @cgidx: Resource index used by this resource manager for cgroup accounting
+	 */
+	u32 cgidx;
 };
 
 /**
@@ -239,6 +249,7 @@ struct ttm_bus_placement {
  * @placement: Placement flags.
  * @bus: Placement on io bus accessible to the CPU
  * @bo: weak reference to the BO, protected by ttm_device::lru_lock
+ * @css: cgroup state this resource is charged to
  *
  * Structure indicating the placement and space resources used by a
  * buffer object.
@@ -251,6 +262,8 @@ struct ttm_resource {
 	struct ttm_bus_placement bus;
 	struct ttm_buffer_object *bo;
 
+	struct dev_cgroup_pool_state *css;
+
 	/**
 	 * @lru: Least recently used list, see &ttm_resource_manager.lru
 	 */
@@ -432,7 +445,8 @@ void ttm_resource_fini(struct ttm_resource_manager *man,
 
 int ttm_resource_alloc(struct ttm_buffer_object *bo,
 		       const struct ttm_place *place,
-		       struct ttm_resource **res);
+		       struct ttm_resource **res,
+		       struct dev_cgroup_pool_state **ret_limit_pool);
 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res);
 bool ttm_resource_intersects(struct ttm_device *bdev,
 			     struct ttm_resource *res,
Signature · 1 line
2.45.2
#13 lore raw

Re: [PATCH 3/7] drm/ttm: Handle cgroup based eviction in TTM

kernel test robot noticed the following build warnings:

Hi Maarten,

kernel test robot noticed the following build warnings:

[auto build test WARNING on drm/drm-next]
[also build test WARNING on drm-exynos/exynos-drm-next next-20241024]
[cannot apply to tj-cgroup/for-next drm-xe/drm-xe-next akpm-mm/mm-everything drm-intel/for-linux-next drm-intel/for-linux-next-fixes drm-misc/drm-misc-next drm-tip/drm-tip linus/master v6.12-rc4] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Maarten-Lankhorst/kernel-cgroup-Add-dev-memory-accounting-cgroup/20241023-155504
base:   git://anongit.freedesktop.org/drm/drm drm-next
patch link:    https://lore.kernel.org/r/20241023075302.27194-4-maarten.lankhorst%40linux.intel.com
patch subject: [PATCH 3/7] drm/ttm: Handle cgroup based eviction in TTM
config: x86_64-rhel-8.3 (https://download.01.org/0day-ci/archive/20241024/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241024/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from drivers/gpu/drm/ttm/ttm_resource.c:29:
include/linux/cgroup_dev.h:61:12: error: two or more data types in declaration specifiers
61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
| ^~~
include/linux/cgroup_dev.h:65:1: error: expected identifier or '(' before '{' token

      65 | {
         | ^
>> include/linux/cgroup_dev.h:61:16: warning: 'dev_cgroup_try_charge' used but never defined

61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
| ^~~~~~~~~~~~~~~~~~~~~

vim +/dev_cgroup_try_charge +61 include/linux/cgroup_dev.h

487073b1855ef4 Maarten Lankhorst 2024-10-23 60

487073b1855ef4 Maarten Lankhorst 2024-10-23 @61  static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
487073b1855ef4 Maarten Lankhorst 2024-10-23  62  				     u32 index, u64 size,
487073b1855ef4 Maarten Lankhorst 2024-10-23  63  				     struct dev_cgroup_pool_state **ret_pool,
487073b1855ef4 Maarten Lankhorst 2024-10-23  64  				     struct dev_cgroup_pool_state **ret_limit_pool);
487073b1855ef4 Maarten Lankhorst 2024-10-23  65  {
487073b1855ef4 Maarten Lankhorst 2024-10-23  66  	*ret_pool = NULL;
487073b1855ef4 Maarten Lankhorst 2024-10-23  67  
487073b1855ef4 Maarten Lankhorst 2024-10-23  68  	if (ret_limit_pool)
487073b1855ef4 Maarten Lankhorst 2024-10-23  69  		*ret_limit_pool = NULL;
487073b1855ef4 Maarten Lankhorst 2024-10-23  70  
487073b1855ef4 Maarten Lankhorst 2024-10-23  71  	return 0;
487073b1855ef4 Maarten Lankhorst 2024-10-23  72  }
487073b1855ef4 Maarten Lankhorst 2024-10-23  73  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#14 lore raw

Re: [PATCH 3/7] drm/ttm: Handle cgroup based eviction in TTM

In reply to Maarten Lankhorst
kernel test robot noticed the following build errors:

Hi Maarten,

kernel test robot noticed the following build errors:

[auto build test ERROR on drm/drm-next]
[also build test ERROR on drm-exynos/exynos-drm-next next-20241024]
[cannot apply to tj-cgroup/for-next drm-xe/drm-xe-next akpm-mm/mm-everything drm-intel/for-linux-next drm-intel/for-linux-next-fixes drm-misc/drm-misc-next drm-tip/drm-tip linus/master v6.12-rc4] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Maarten-Lankhorst/kernel-cgroup-Add-dev-memory-accounting-cgroup/20241023-155504
base:   git://anongit.freedesktop.org/drm/drm drm-next
patch link:    https://lore.kernel.org/r/20241023075302.27194-4-maarten.lankhorst%40linux.intel.com
patch subject: [PATCH 3/7] drm/ttm: Handle cgroup based eviction in TTM
config: arm64-randconfig-002-20241024 (https://download.01.org/0day-ci/archive/20241025/[email protected]/config)
compiler: aarch64-linux-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241025/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from drivers/gpu/drm/ttm/ttm_resource.c:29:
include/linux/cgroup_dev.h:61:12: error: two or more data types in declaration specifiers
61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
| ^~~
include/linux/cgroup_dev.h:65:1: error: expected identifier or '(' before '{' token

      65 | {
         | ^
>> include/linux/cgroup_dev.h:61:16: error: 'dev_cgroup_try_charge' used but never defined [-Werror]

61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
| ^~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

vim +/dev_cgroup_try_charge +61 include/linux/cgroup_dev.h

487073b1855ef4 Maarten Lankhorst 2024-10-23 60

487073b1855ef4 Maarten Lankhorst 2024-10-23 @61  static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
487073b1855ef4 Maarten Lankhorst 2024-10-23  62  				     u32 index, u64 size,
487073b1855ef4 Maarten Lankhorst 2024-10-23  63  				     struct dev_cgroup_pool_state **ret_pool,
487073b1855ef4 Maarten Lankhorst 2024-10-23  64  				     struct dev_cgroup_pool_state **ret_limit_pool);
487073b1855ef4 Maarten Lankhorst 2024-10-23  65  {
487073b1855ef4 Maarten Lankhorst 2024-10-23  66  	*ret_pool = NULL;
487073b1855ef4 Maarten Lankhorst 2024-10-23  67  
487073b1855ef4 Maarten Lankhorst 2024-10-23  68  	if (ret_limit_pool)
487073b1855ef4 Maarten Lankhorst 2024-10-23  69  		*ret_limit_pool = NULL;
487073b1855ef4 Maarten Lankhorst 2024-10-23  70  
487073b1855ef4 Maarten Lankhorst 2024-10-23  71  	return 0;
487073b1855ef4 Maarten Lankhorst 2024-10-23  72  }
487073b1855ef4 Maarten Lankhorst 2024-10-23  73  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#15 lore raw

Re: [PATCH 3/7] drm/ttm: Handle cgroup based eviction in TTM

In reply to Maarten Lankhorst
kernel test robot noticed the following build warnings:

Hi Maarten,

kernel test robot noticed the following build warnings:

[auto build test WARNING on drm/drm-next]
[also build test WARNING on drm-exynos/exynos-drm-next next-20241024]
[cannot apply to tj-cgroup/for-next drm-xe/drm-xe-next akpm-mm/mm-everything drm-intel/for-linux-next drm-intel/for-linux-next-fixes drm-misc/drm-misc-next drm-tip/drm-tip linus/master v6.12-rc4] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Maarten-Lankhorst/kernel-cgroup-Add-dev-memory-accounting-cgroup/20241023-155504
base:   git://anongit.freedesktop.org/drm/drm drm-next
patch link:    https://lore.kernel.org/r/20241023075302.27194-4-maarten.lankhorst%40linux.intel.com
patch subject: [PATCH 3/7] drm/ttm: Handle cgroup based eviction in TTM
config: loongarch-allmodconfig (https://download.01.org/0day-ci/archive/20241025/[email protected]/config)
compiler: loongarch64-linux-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241025/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> drivers/gpu/drm/ttm/ttm_bo.c:1100: warning: Function parameter or struct member 'hit_low' not described in 'ttm_bo_swapout_walk'
>> drivers/gpu/drm/ttm/ttm_bo.c:1100: warning: Function parameter or struct member 'evict_low' not described in 'ttm_bo_swapout_walk'

vim +1100 drivers/gpu/drm/ttm/ttm_bo.c

a3185f91d0579b Christian König   2022-05-09  1089  
10efe34dae798c Thomas Hellström  2024-07-05  1090  /**
10efe34dae798c Thomas Hellström  2024-07-05  1091   * struct ttm_bo_swapout_walk - Parameters for the swapout walk
10efe34dae798c Thomas Hellström  2024-07-05  1092   */
10efe34dae798c Thomas Hellström  2024-07-05  1093  struct ttm_bo_swapout_walk {
10efe34dae798c Thomas Hellström  2024-07-05  1094  	/** @walk: The walk base parameters. */
10efe34dae798c Thomas Hellström  2024-07-05  1095  	struct ttm_lru_walk walk;
10efe34dae798c Thomas Hellström  2024-07-05  1096  	/** @gfp_flags: The gfp flags to use for ttm_tt_swapout() */
10efe34dae798c Thomas Hellström  2024-07-05  1097  	gfp_t gfp_flags;

e5a692062aad1c Maarten Lankhorst 2024-10-23 1098
e5a692062aad1c Maarten Lankhorst 2024-10-23 1099 bool hit_low, evict_low;
10efe34dae798c Thomas Hellström 2024-07-05 @1100 }; 10efe34dae798c Thomas Hellström 2024-07-05 1101

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#16 lore raw

PATCH 4/7 drm/xe: Implement cgroup for vram

In reply to Maarten Lankhorst

Add vram based cgroup eviction to Xe.
Most hardware with VRAM uses TTM for its management, and can be similarly trivially enabled.

---
 drivers/gpu/drm/xe/xe_device.c       |  4 ++++
 drivers/gpu/drm/xe/xe_device_types.h |  4 ++++
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.c | 10 ++++++++++
 3 files changed, 18 insertions(+)
drivers/gpu/drm/xe/xe_device.c +4 -0
index 51bb9d875268f..cb4b2013eb808 100644
@@ -726,6 +726,10 @@ int xe_device_probe(struct xe_device *xe)
 	/* Allocate and map stolen after potential VRAM resize */
 	xe_ttm_stolen_mgr_init(xe);
 
+	err = drmm_cgroup_register_device(&xe->drm, &xe->cg);
+	if (err)
+		goto err;
+
 	/*
 	 * Now that GT is initialized (TTM in particular),
 	 * we can try to init display, and inherit the initial fb.
drivers/gpu/drm/xe/xe_device_types.h +4 -0
index 85bede4dd6461..0401b4fe6f645 100644
@@ -7,6 +7,7 @@
 #define _XE_DEVICE_TYPES_H_
 
 #include <linux/pci.h>
+#include <linux/cgroup_dev.h>
 
 #include <drm/drm_device.h>
 #include <drm/drm_file.h>
@@ -257,6 +258,9 @@ struct xe_device {
 	/** @devcoredump: device coredump */
 	struct xe_devcoredump devcoredump;
 
+	/** @cg: device cgroup bookkeeping */
+	struct dev_cgroup_device cg;
+
 	/** @info: device info */
 	struct intel_device_info {
 		/** @info.platform_name: platform name */
drivers/gpu/drm/xe/xe_ttm_vram_mgr.c +10 -0
index 423b261ea7430..cb463be53f4bc 100644
@@ -339,6 +339,16 @@ int __xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_ttm_vram_mgr *mgr,
 	struct ttm_resource_manager *man = &mgr->manager;
 	int err;
 
+	if (mem_type != XE_PL_STOLEN) {
+		int cgregion = xe->cg.num_regions++;
+
+		xe->cg.regions[cgregion].size = size;
+		xe->cg.regions[cgregion].name =
+			mem_type == XE_PL_VRAM0 ? "vram0" : "vram1";
+		man->cgdev = &xe->cg;
+		man->cgidx = cgregion;
+	}
+
 	man->func = &xe_ttm_vram_mgr_func;
 	mgr->mem_type = mem_type;
 	mutex_init(&mgr->lock);
Signature · 1 line
2.45.2
author maintainer
#17 lore raw

PATCH 5/7 drm/amdgpu: Add cgroups implementation

In reply to Maarten Lankhorst

Similar to xe, enable some simple management of VRAM only.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h          | 2 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c      | 6 ++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 6 ++++++
 3 files changed, 14 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index 9b1e0ede05a45..27c11e43f8e9e 100644
@@ -43,6 +43,7 @@
 #include "amdgpu_ctx.h"
 
 #include <linux/atomic.h>
+#include <linux/cgroup_dev.h>
 #include <linux/wait.h>
 #include <linux/list.h>
 #include <linux/kref.h>
@@ -835,6 +836,7 @@ struct amdgpu_device {
 	struct device			*dev;
 	struct pci_dev			*pdev;
 	struct drm_device		ddev;
+	struct dev_cgroup_device		cg;
 
 #ifdef CONFIG_DRM_AMD_ACP
 	struct amdgpu_acp		acp;
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +6 -0
index 74adb983ab03e..3f6554c7aac2f 100644
@@ -1874,6 +1874,12 @@ int amdgpu_ttm_init(struct amdgpu_device *adev)
 		return r;
 	}
 
+	r = drmm_cgroup_register_device(adev_to_drm(adev), &adev->cg);
+	if (r) {
+		DRM_ERROR("Failed initializing cgroup allocator.\n");
+		return r;
+	}
+
 	/* Change the size here instead of the init above so only lpfn is affected */
 	amdgpu_ttm_set_buffer_funcs_status(adev, false);
 #ifdef CONFIG_64BIT
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +6 -0
index 7d26a962f811c..44d560bef5b7d 100644
@@ -927,6 +927,12 @@ int amdgpu_vram_mgr_init(struct amdgpu_device *adev)
 		DRM_INFO("Setup dummy vram mgr\n");
 	}
 
+	adev->cg.regions[0].size = adev->gmc.real_vram_size;
+	adev->cg.regions[0].name = "vram";
+	adev->cg.num_regions++;
+	man->cgdev = &adev->cg;
+	man->cgidx = 0;
+
 	ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_VRAM, &mgr->manager);
 	ttm_resource_manager_set_used(man, true);
 	return 0;
Signature · 1 line
2.45.2
author maintainer
#18 lore raw

PATCH 6/7 drm/xe: Hack to test with mapped pages instead of vram.

In reply to Maarten Lankhorst

We will probably want to make this a proper region in TTM for everything, so that we can charge VRAM twice, once for mapped in sysmem, once for mapped in vram. That way we don't need to deal with evict failing from lack of available memory in mapped.

---
 drivers/gpu/drm/xe/xe_ttm_sys_mgr.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
drivers/gpu/drm/xe/xe_ttm_sys_mgr.c +14 -0
index 9844a8edbfe19..20fa8ec8925ef 100644
@@ -101,6 +101,18 @@ static void ttm_sys_mgr_fini(struct drm_device *drm, void *arg)
 	ttm_set_driver_manager(&xe->ttm, XE_PL_TT, NULL);
 }
 
+static inline void apply_cg(struct xe_device *xe,
+			    struct ttm_resource_manager *man,
+			    u64 gtt_size)
+{
+	int cgregion = xe->cg.num_regions++;
+
+	xe->cg.regions[cgregion].size = gtt_size;
+	xe->cg.regions[cgregion].name = "mapped";
+	man->cgdev = &xe->cg;
+	man->cgidx = cgregion;
+
+}
 int xe_ttm_sys_mgr_init(struct xe_device *xe)
 {
 	struct ttm_resource_manager *man = &xe->mem.sys_mgr;
@@ -116,6 +128,8 @@ int xe_ttm_sys_mgr_init(struct xe_device *xe)
 	man->func = &xe_ttm_sys_mgr_func;
 	ttm_resource_manager_init(man, &xe->ttm, gtt_size >> PAGE_SHIFT);
 	ttm_set_driver_manager(&xe->ttm, XE_PL_TT, man);
+	apply_cg(xe, man, gtt_size);
+
 	ttm_resource_manager_set_used(man, true);
 	return drmm_add_action_or_reset(&xe->drm, ttm_sys_mgr_fini, xe);
 }
Signature · 1 line
2.45.2
#19 lore raw

Re: [PATCH 6/7] [HACK] drm/xe: Hack to test with mapped pages instead of vram.

Quoted 16 lines · Maarten Lankhorst

We will probably want to make this a proper region in TTM for everything, so that we can charge VRAM twice, once for mapped in sysmem, once for mapped in vram. That way we don't need to deal with evict failing from lack of available memory in mapped.

---
 drivers/gpu/drm/xe/xe_ttm_sys_mgr.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
drivers/gpu/drm/xe/xe_ttm_sys_mgr.c +0 -0
index 9844a8edbfe19..20fa8ec8925ef 100644

drivers/gpu/drm/xe/xe_ttm_sys_mgr.c static void ttm_sys_mgr_fini(struct drm_device *drm, void *arg) · line 106

Quoted 6 lines
drivers/gpu/drm/xe/xe_ttm_sys_mgr.c +3 -0
@@ -101,6 +101,18 @@ static void ttm_sys_mgr_fini(struct drm_device *drm, void *arg)
 	ttm_set_driver_manager(&xe->ttm, XE_PL_TT, NULL);
 }
 
+static inline void apply_cg(struct xe_device *xe,
+			    struct ttm_resource_manager *man,
+			    u64 gtt_size)

Ditto here about static inline in .c.

Quoted 21 lines
drivers/gpu/drm/xe/xe_ttm_sys_mgr.c +11 -0
+{
+	int cgregion = xe->cg.num_regions++;
+
+	xe->cg.regions[cgregion].size = gtt_size;
+	xe->cg.regions[cgregion].name = "mapped";
+	man->cgdev = &xe->cg;
+	man->cgidx = cgregion;
+
+}
 int xe_ttm_sys_mgr_init(struct xe_device *xe)
 {
 	struct ttm_resource_manager *man = &xe->mem.sys_mgr;
@@ -116,6 +128,8 @@ int xe_ttm_sys_mgr_init(struct xe_device *xe)
 	man->func = &xe_ttm_sys_mgr_func;
 	ttm_resource_manager_init(man, &xe->ttm, gtt_size >> PAGE_SHIFT);
 	ttm_set_driver_manager(&xe->ttm, XE_PL_TT, man);
+	apply_cg(xe, man, gtt_size);
+
 	ttm_resource_manager_set_used(man, true);
 	return drmm_add_action_or_reset(&xe->drm, ttm_sys_mgr_fini, xe);
 }
Signature · 1 line
Jani Nikula, Intel
author maintainer
#20 lore raw

PATCH 7/7 drm/gem: Add cgroup memory accounting

In reply to Maarten Lankhorst

From: Maxime Ripard <[email protected]>

In order to support any device using the GEM support, let's register a dev cgroup device in the drm_dev_register path, and account for allocated buffers in the buffer allocation path.

Marked discussion by Maarten Lankhorst:
This is only implemented for drm_gem_dma_helper.c, and breaks the other drivers. It's still here for discussion, as we need to figure out how to make something like this work for both TTM and GEM drivers.

---
 drivers/gpu/drm/drm_drv.c            | 11 ++++++++++-
 drivers/gpu/drm/drm_gem.c            |  4 ++++
 drivers/gpu/drm/drm_gem_dma_helper.c |  4 ++++
 include/drm/drm_device.h             |  4 ++++
 include/drm/drm_gem.h                |  2 ++
 5 files changed, 24 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_drv.c +10 -1
index 251d1d69b09c5..6b1cffd1f52c6 100644
@@ -930,6 +930,12 @@ int drm_dev_register(struct drm_device *dev, unsigned long flags)
 	if (drm_dev_needs_global_mutex(dev))
 		mutex_lock(&drm_global_mutex);
 
+	ret = drmm_cgroup_register_device(dev, &dev->cg);
+	if (ret) {
+		DRM_ERROR("Cannot create cgroup device.\n");
+		goto out_unlock;
+	}
+
 	if (drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL))
 		accel_debugfs_register(dev);
 	else
@@ -937,7 +943,7 @@ int drm_dev_register(struct drm_device *dev, unsigned long flags)
 
 	ret = drm_minor_register(dev, DRM_MINOR_RENDER);
 	if (ret)
-		goto err_minors;
+		goto out_unregister_cgroup;
 
 	ret = drm_minor_register(dev, DRM_MINOR_PRIMARY);
 	if (ret)
@@ -982,6 +988,8 @@ int drm_dev_register(struct drm_device *dev, unsigned long flags)
 	drm_minor_unregister(dev, DRM_MINOR_ACCEL);
 	drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
 	drm_minor_unregister(dev, DRM_MINOR_RENDER);
+out_unregister_cgroup:
+	dev_cgroup_unregister_device(&dev->cg);
 out_unlock:
 	if (drm_dev_needs_global_mutex(dev))
 		mutex_unlock(&drm_global_mutex);
@@ -1024,6 +1032,7 @@ void drm_dev_unregister(struct drm_device *dev)
 	drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
 	drm_minor_unregister(dev, DRM_MINOR_RENDER);
 	drm_debugfs_dev_fini(dev);
+	dev_cgroup_unregister_device(&dev->cg);
 }
 EXPORT_SYMBOL(drm_dev_unregister);
 
drivers/gpu/drm/drm_gem.c +4 -0
index ee811764c3df4..95af268d9dd9b 100644
@@ -110,6 +110,10 @@ drm_gem_init(struct drm_device *dev)
 				    DRM_FILE_PAGE_OFFSET_START,
 				    DRM_FILE_PAGE_OFFSET_SIZE);
 
+	dev->cg.regions[0].size = U64_MAX;
+	dev->cg.regions[0].name = "gem";
+	dev->cg.num_regions++;
+
 	return drmm_add_action(dev, drm_gem_init_release, NULL);
 }
 
drivers/gpu/drm/drm_gem_dma_helper.c +4 -0
index 870b90b78bc4e..32eafbf4aa814 100644
@@ -106,6 +106,10 @@ __drm_gem_dma_create(struct drm_device *drm, size_t size, bool private)
 		goto error;
 	}
 
+	ret = dev_cgroup_try_charge(&drm->cg, 0, size, &dma_obj->base.cgroup_pool_state, NULL);
+	if (ret)
+		goto error;
+
 	return dma_obj;
 
 error:
include/drm/drm_device.h +4 -0
index c91f87b5242d7..bdb7656e7db67 100644
@@ -1,6 +1,7 @@
 #ifndef _DRM_DEVICE_H_
 #define _DRM_DEVICE_H_
 
+#include <linux/cgroup_dev.h>
 #include <linux/list.h>
 #include <linux/kref.h>
 #include <linux/mutex.h>
@@ -317,6 +318,9 @@ struct drm_device {
 	 * Root directory for debugfs files.
 	 */
 	struct dentry *debugfs_root;
+
+	/** @cg: device cgroup bookkeeping */
+	struct dev_cgroup_device cg;
 };
 
 #endif
include/drm/drm_gem.h +2 -0
index 5b8b1b059d32c..cc998abea7924 100644
@@ -430,6 +430,8 @@ struct drm_gem_object {
 	 * The current LRU list that the GEM object is on.
 	 */
 	struct drm_gem_lru *lru;
+
+	struct dev_cgroup_pool_state *cgroup_pool_state;
 };
 
 /**
Signature · 1 line
2.45.2
#21 lore raw

Re: [PATCH 7/7] [DISCUSSION] drm/gem: Add cgroup memory accounting

kernel test robot noticed the following build errors:

Hi Maarten,

kernel test robot noticed the following build errors:

[auto build test ERROR on drm/drm-next]
[also build test ERROR on drm-exynos/exynos-drm-next next-20241024]
[cannot apply to tj-cgroup/for-next drm-xe/drm-xe-next akpm-mm/mm-everything drm-intel/for-linux-next drm-intel/for-linux-next-fixes drm-misc/drm-misc-next drm-tip/drm-tip linus/master v6.12-rc4] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Maarten-Lankhorst/kernel-cgroup-Add-dev-memory-accounting-cgroup/20241023-155504
base:   git://anongit.freedesktop.org/drm/drm drm-next
patch link:    https://lore.kernel.org/r/20241023075302.27194-8-maarten.lankhorst%40linux.intel.com
patch subject: [PATCH 7/7] [DISCUSSION] drm/gem: Add cgroup memory accounting
config: i386-buildonly-randconfig-002-20241024 (https://download.01.org/0day-ci/archive/20241024/[email protected]/config)
compiler: clang version 19.1.2 (https://github.com/llvm/llvm-project 7ba7d8e2f7b6445b60679da826210cdde29eaf8b)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241024/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from drivers/media/cec/core/cec-adap.c:15:
In file included from include/linux/mm.h:2213:
include/linux/vmstat.h:518:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]

     518 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
         |                               ~~~~~~~~~~~ ^ ~~~
   In file included from drivers/media/cec/core/cec-adap.c:20:
   In file included from include/drm/drm_device.h:4:
>> include/linux/cgroup_dev.h:61:12: error: cannot combine with previous 'int' declaration specifier
      61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
         |            ^
>> include/linux/cgroup_dev.h:65:1: error: expected identifier or '('
      65 | {
         | ^

1 warning and 2 errors generated.

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for GET_FREE_REGION Depends on [n]: SPARSEMEM [=n]
Selected by [y]:
- RESOURCE_KUNIT_TEST [=y] && RUNTIME_TESTING_MENU [=y] && KUNIT [=y]

vim +/int +61 include/linux/cgroup_dev.h

487073b1855ef4 Maarten Lankhorst 2024-10-23 60

487073b1855ef4 Maarten Lankhorst 2024-10-23 @61  static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
487073b1855ef4 Maarten Lankhorst 2024-10-23  62  				     u32 index, u64 size,
487073b1855ef4 Maarten Lankhorst 2024-10-23  63  				     struct dev_cgroup_pool_state **ret_pool,
487073b1855ef4 Maarten Lankhorst 2024-10-23  64  				     struct dev_cgroup_pool_state **ret_limit_pool);
487073b1855ef4 Maarten Lankhorst 2024-10-23 @65  {
487073b1855ef4 Maarten Lankhorst 2024-10-23  66  	*ret_pool = NULL;
487073b1855ef4 Maarten Lankhorst 2024-10-23  67  
487073b1855ef4 Maarten Lankhorst 2024-10-23  68  	if (ret_limit_pool)
487073b1855ef4 Maarten Lankhorst 2024-10-23  69  		*ret_limit_pool = NULL;
487073b1855ef4 Maarten Lankhorst 2024-10-23  70  
487073b1855ef4 Maarten Lankhorst 2024-10-23  71  	return 0;
487073b1855ef4 Maarten Lankhorst 2024-10-23  72  }
487073b1855ef4 Maarten Lankhorst 2024-10-23  73  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#22 lore raw

✓ CI.Patch_applied: success for kernel/cgroups: Add "dev" memory accounting cgroup.

In reply to Maarten Lankhorst
== Series Details ==

== Series Details ==

Series: kernel/cgroups: Add "dev" memory accounting cgroup.
URL : https://patchwork.freedesktop.org/series/140361/
State : success

== Summary ==

=== Applying kernel patches on branch 'drm-tip' with base: ===
Base commit: 0cd3ccdcf952 drm-tip: 2024y-10m-23d-07h-36m-40s UTC integration manifest === git am output follows ===
.git/rebase-apply/patch:95: new blank line at EOF.
+
warning: 1 line adds whitespace errors.
Applying: kernel/cgroup: Add "dev" memory accounting cgroup
Applying: drm/drv: Add drmm cgroup registration for dev cgroups.
Applying: drm/ttm: Handle cgroup based eviction in TTM
Applying: drm/xe: Implement cgroup for vram
Applying: drm/amdgpu: Add cgroups implementation
Applying: drm/xe: Hack to test with mapped pages instead of vram.
Applying: drm/gem: Add cgroup memory accounting

#23 lore raw

✗ CI.checkpatch: warning for kernel/cgroups: Add "dev" memory accounting cgroup.

In reply to Maarten Lankhorst
== Series Details ==

== Series Details ==

Series: kernel/cgroups: Add "dev" memory accounting cgroup.
URL : https://patchwork.freedesktop.org/series/140361/
State : warning

== Summary ==

+ KERNEL=/kernel
+ git clone https://gitlab.freedesktop.org/drm/maintainer-tools mt Cloning into 'mt'...
warning: redirecting to https://gitlab.freedesktop.org/drm/maintainer-tools.git/
+ git -C mt rev-list -n1 origin/master 30ab6715fc09baee6cc14cb3c89ad8858688d474
+ cd /kernel
+ git config --global --add safe.directory /kernel
+ git log -n1
commit d3c3bb202d972256d5f6bf2fb6c127a11b4d14f0

Author: Maxime Ripard <[email protected]>
Date:   Wed Oct 23 09:53:00 2024 +0200

drm/gem: Add cgroup memory accounting

In order to support any device using the GEM support, let's register a dev cgroup device in the drm_dev_register path, and account for allocated buffers in the buffer allocation path.

Marked discussion by Maarten Lankhorst:
This is only implemented for drm_gem_dma_helper.c, and breaks the other drivers. It's still here for discussion, as we need to figure out how to make something like this work for both TTM and GEM drivers.

+ /mt/dim checkpatch 0cd3ccdcf9525b9d2f822df5f8d16b552753cf88 drm-intel c2f417069c9c kernel/cgroup: Add "dev" memory accounting cgroup
-:106: WARNING:FILE_PATH_CHANGES: added, moved or deleted file(s), does MAINTAINERS need updating? #106:
new file mode 100644

-:111: WARNING:SPDX_LICENSE_TAG: Missing or malformed SPDX-License-Identifier tag in line 1 #111: FILE: Documentation/core-api/cgroup.rst:1:
+==================

-:138: WARNING:SPDX_LICENSE_TAG: Missing or malformed SPDX-License-Identifier tag in line 1 #138: FILE: Documentation/gpu/drm-compute.rst:1:
+==================================

-:258: WARNING:REPEATED_WORD: Possible repeated word: 'int' #258: FILE: include/linux/cgroup_dev.h:61:
+static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,

-:372: WARNING:TYPO_SPELLING: 'succesful' may be misspelled - perhaps 'successful'? #372: FILE: kernel/cgroup/dev.c:22:
+ * Keeps the device reference alive after a succesful RCU lookup.
^^^^^^^^^

-:551: WARNING:LINE_SPACING: Missing a blank line after declarations #551: FILE: kernel/cgroup/dev.c:201:
+ struct devcg_state *devcs = kzalloc(sizeof(*devcs), GFP_KERNEL);
+ if (!devcs)

-:563: WARNING:USE_LOCKDEP: Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked #563: FILE: kernel/cgroup/dev.c:213:
+ list_for_each_entry_rcu(pool, &devcs->pools, css_node, spin_is_locked(&devcg_lock))

-:611: ERROR:OPEN_BRACE: that open brace { should be on the previous line #611: FILE: kernel/cgroup/dev.c:261:
+ for (pool = test_pool; pool && limit_pool != pool; pool = pool_parent(pool))
+ {}

-:621: WARNING:LONG_LINE: line length of 107 exceeds 100 columns #621: FILE: kernel/cgroup/dev.c:271:
+ for (limit_pool = test_pool; pool_parent(limit_pool); limit_pool = pool_parent(limit_pool))

-:621: ERROR:OPEN_BRACE: that open brace { should be on the previous line #621: FILE: kernel/cgroup/dev.c:271:
+ for (limit_pool = test_pool; pool_parent(limit_pool); limit_pool = pool_parent(limit_pool))
+ {}

-:657: WARNING:LONG_LINE: line length of 117 exceeds 100 columns #657: FILE: kernel/cgroup/dev.c:307:
+ pool = kzalloc(offsetof(struct dev_cgroup_pool_state, resources[dev->base.num_regions]), GFP_NOWAIT);

-:714: CHECK:MULTIPLE_ASSIGNMENTS: multiple assignments should be avoided #714: FILE: kernel/cgroup/dev.c:364:
+ retpool = pool = find_cg_pool_locked(devcs, dev);

-:813: WARNING:SPACING: space prohibited between function name and open parenthesis '(' #813: FILE: kernel/cgroup/dev.c:463:
+ cgdev->priv = dev = kzalloc(sizeof (*dev), GFP_KERNEL);

-:813: CHECK:MULTIPLE_ASSIGNMENTS: multiple assignments should be avoided #813: FILE: kernel/cgroup/dev.c:463:
+ cgdev->priv = dev = kzalloc(sizeof (*dev), GFP_KERNEL);

-:841: WARNING:USE_LOCKDEP: Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked #841: FILE: kernel/cgroup/dev.c:491:
+ list_for_each_entry_rcu(dev, &devcg_devices, dev_node, spin_is_locked(&devcg_lock))

-:888: WARNING:LONG_LINE: line length of 130 exceeds 100 columns #888: FILE: kernel/cgroup/dev.c:538:
+ allocpool = kzalloc(offsetof(struct dev_cgroup_pool_state, resources[dev->base.num_regions]), GFP_KERNEL);

-:984: WARNING:LONG_LINE: line length of 113 exceeds 100 columns #984: FILE: kernel/cgroup/dev.c:634:
+ *ret_limit_pool = container_of(fail, struct dev_cgroup_pool_state, resources[index].cnt);

-:1066: CHECK:LINE_SPACING: Please don't use multiple blank lines #1066: FILE: kernel/cgroup/dev.c:716:
+
+

-:1135: CHECK:LINE_SPACING: Please don't use multiple blank lines #1135: FILE: kernel/cgroup/dev.c:785:
+
+

total: 2 errors, 13 warnings, 4 checks, 1160 lines checked 73a717e17624 drm/drv: Add drmm cgroup registration for dev cgroups. bcc345cd4b87 drm/ttm: Handle cgroup based eviction in TTM -:228: CHECK:MULTIPLE_ASSIGNMENTS: multiple assignments should be avoided #228: FILE: drivers/gpu/drm/ttm/ttm_bo.c:591:
+ evict_walk.try_low = evict_walk.hit_low = false;

total: 0 errors, 0 warnings, 1 checks, 343 lines checked 28dc4bcae4e7 drm/xe: Implement cgroup for vram
a3b33ad8006a drm/amdgpu: Add cgroups implementation ab5ea38d5277 drm/xe: Hack to test with mapped pages instead of vram. -:33: CHECK:BRACES: Blank lines aren't necessary before a close brace '}' #33: FILE: drivers/gpu/drm/xe/xe_ttm_sys_mgr.c:115:
+
+}

total: 0 errors, 0 warnings, 1 checks, 26 lines checked d3c3bb202d97 drm/gem: Add cgroup memory accounting

#24 lore raw

✗ CI.KUnit: failure for kernel/cgroups: Add "dev" memory accounting cgroup.

In reply to Maarten Lankhorst
== Series Details ==

== Series Details ==

Series: kernel/cgroups: Add "dev" memory accounting cgroup.
URL : https://patchwork.freedesktop.org/series/140361/
State : failure

== Summary ==

+ trap cleanup EXIT
+ /kernel/tools/testing/kunit/kunit.py run --kunitconfig /kernel/drivers/gpu/drm/xe/.kunitconfig [08:12:15] Configuring KUnit Kernel ...
Generating .config ...
Populating config with:

$ make ARCH=um O=.kunit olddefconfig
[08:12:19] Building KUnit Kernel ...
Populating config with:
$ make ARCH=um O=.kunit olddefconfig
Building with:
$ make all compile_commands.json ARCH=um O=.kunit --jobs=48
ERROR:root:In file included from ../include/drm/drm_device.h:4,
                 from ../include/drm/drm_crtc.h:33,
                 from ../include/drm/drm_atomic.h:31,
                 from ../drivers/gpu/drm/tests/drm_kunit_helpers.c:3:
../include/linux/cgroup_dev.h:61:12: error: two or more data types in declaration specifiers
   61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
      |            ^~~
../include/linux/cgroup_dev.h:65:1: error: expected identifier or ‘(’ before ‘{’ token
   65 | {
      | ^
../include/linux/cgroup_dev.h:61:16: warning: ‘dev_cgroup_try_charge’ declared ‘static’ but never defined [-Wunused-function]
   61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
      |                ^~~~~~~~~~~~~~~~~~~~~
make[7]: *** [../scripts/Makefile.build:229: drivers/gpu/drm/tests/drm_kunit_helpers.o] Error 1
make[6]: *** [../scripts/Makefile.build:478: drivers/gpu/drm/tests] Error 2
make[6]: *** Waiting for unfinished jobs....
In file included from ../include/drm/drm_device.h:4,
                 from ../drivers/gpu/drm/display/drm_dp_dual_mode_helper.c:31:
../include/linux/cgroup_dev.h:61:12: error: two or more data types in declaration specifiers
   61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
      |            ^~~
../include/linux/cgroup_dev.h:65:1: error: expected identifier or ‘(’ before ‘{’ token
   65 | {
      | ^
../include/linux/cgroup_dev.h:61:16: warning: ‘dev_cgroup_try_charge’ declared ‘static’ but never defined [-Wunused-function]
   61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
      |                ^~~~~~~~~~~~~~~~~~~~~
make[7]: *** [../scripts/Makefile.build:229: drivers/gpu/drm/display/drm_dp_dual_mode_helper.o] Error 1
make[7]: *** Waiting for unfinished jobs....
In file included from ../include/drm/drm_device.h:4,
                 from ../drivers/gpu/drm/ttm/ttm_tt.c:41:
../include/linux/cgroup_dev.h:61:12: error: two or more data types in declaration specifiers
   61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
      |            ^~~
../include/linux/cgroup_dev.h:65:1: error: expected identifier or ‘(’ before ‘{’ token
   65 | {
      | ^
../include/linux/cgroup_dev.h:61:16: warning: ‘dev_cgroup_try_charge’ declared ‘static’ but never defined [-Wunused-function]
   61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
      |                ^~~~~~~~~~~~~~~~~~~~~
make[7]: *** [../scripts/Makefile.build:229: drivers/gpu/drm/ttm/ttm_tt.o] Error 1
make[7]: *** Waiting for unfinished jobs....
In file included from ../drivers/gpu/drm/ttm/ttm_bo.c:45:
../include/linux/cgroup_dev.h:61:12: error: two or more data types in declaration specifiers
   61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
      |            ^~~
../include/linux/cgroup_dev.h:65:1: error: expected identifier or ‘(’ before ‘{’ token
   65 | {
      | ^
../include/linux/cgroup_dev.h:61:16: warning: ‘dev_cgroup_try_charge’ declared ‘static’ but never defined [-Wunused-function]
   61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
      |                ^~~~~~~~~~~~~~~~~~~~~
In file included from ../include/drm/drm_device.h:4,
                 from ../include/drm/drm_crtc.h:33,
                 from ../include/drm/drm_atomic.h:31,
                 from ../include/drm/display/drm_dp_mst_helper.h:27,
                 from ../drivers/gpu/drm/display/drm_dp_helper.c:36:
../include/linux/cgroup_dev.h:61:12: error: two or more data types in declaration specifiers
   61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
      |            ^~~
make[7]: *** [../scripts/Makefile.build:229: drivers/gpu/drm/ttm/ttm_bo.o] Error 1
../include/linux/cgroup_dev.h:65:1: error: expected identifier or ‘(’ before ‘{’ token
   65 | {
      | ^
../include/linux/cgroup_dev.h:61:16: warning: ‘dev_cgroup_try_charge’ declared ‘static’ but never defined [-Wunused-function]
   61 | static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev,
      |                ^~~~~~~~~~~~~~~~~~~~~
make[7]: *** [../scripts/Makefile.build:229: drivers/gpu/drm/display/drm_dp_helper.o] Error 1
make[6]: *** [../scripts/Makefile.build:478: drivers/gpu/drm/display] Error 2
make[6]: *** [../scripts/Makefile.build:478: drivers/gpu/drm/ttm] Error 2
make[5]: *** [../scripts/Makefile.build:478: drivers/gpu/drm] Error 2
make[4]: *** [../scripts/Makefile.build:478: drivers/gpu] Error 2
make[4]: *** Waiting for unfinished jobs....
make[3]: *** [../scripts/Makefile.build:478: drivers] Error 2
make[3]: *** Waiting for unfinished jobs....
../lib/iomap.c:156:5: warning: no previous prototype for ‘ioread64_lo_hi’ [-Wmissing-prototypes]
  156 | u64 ioread64_lo_hi(const void __iomem *addr)
      |     ^~~~~~~~~~~~~~
../lib/iomap.c:163:5: warning: no previous prototype for ‘ioread64_hi_lo’ [-Wmissing-prototypes]
  163 | u64 ioread64_hi_lo(const void __iomem *addr)
      |     ^~~~~~~~~~~~~~
../lib/iomap.c:170:5: warning: no previous prototype for ‘ioread64be_lo_hi’ [-Wmissing-prototypes]
  170 | u64 ioread64be_lo_hi(const void __iomem *addr)
      |     ^~~~~~~~~~~~~~~~
../lib/iomap.c:178:5: warning: no previous prototype for ‘ioread64be_hi_lo’ [-Wmissing-prototypes]
  178 | u64 ioread64be_hi_lo(const void __iomem *addr)
      |     ^~~~~~~~~~~~~~~~
../lib/iomap.c:264:6: warning: no previous prototype for ‘iowrite64_lo_hi’ [-Wmissing-prototypes]
  264 | void iowrite64_lo_hi(u64 val, void __iomem *addr)
      |      ^~~~~~~~~~~~~~~
../lib/iomap.c:272:6: warning: no previous prototype for ‘iowrite64_hi_lo’ [-Wmissing-prototypes]
  272 | void iowrite64_hi_lo(u64 val, void __iomem *addr)
      |      ^~~~~~~~~~~~~~~
../lib/iomap.c:280:6: warning: no previous prototype for ‘iowrite64be_lo_hi’ [-Wmissing-prototypes]
  280 | void iowrite64be_lo_hi(u64 val, void __iomem *addr)
      |      ^~~~~~~~~~~~~~~~~
../lib/iomap.c:288:6: warning: no previous prototype for ‘iowrite64be_hi_lo’ [-Wmissing-prototypes]
  288 | void iowrite64be_hi_lo(u64 val, void __iomem *addr)
      |      ^~~~~~~~~~~~~~~~~
make[2]: *** [/kernel/Makefile:1936: .] Error 2
make[1]: *** [/kernel/Makefile:224: __sub-make] Error 2
make: *** [Makefile:224: __sub-make] Error 2

+ cleanup
++ stat -c %u:%g /kernel
+ chown -R 1003:1003 /kernel

maintainer
#25 lore raw
In reply to Maarten Lankhorst

Hello,

Quoted 11 lines · Maarten Lankhorst

New submission!
I've added documentation for each call, and integrated the renaming from drm cgroup to dev cgroup, based on maxime ripard's work.

Maxime has been testing this with dma-buf heaps and v4l2 too, and it seems to work. In the initial submission, I've decided to only add the smallest enablement possible, to have less chance of breaking things.

The API has been changed slightly, from "$name region.$regionname=$limit" in a file called dev.min/low/max to "$subsystem/$name $regionname=$limit" in a file called dev.region.min/low/max.

Quoted 7 lines

This hopefully allows us to perhaps extend the API later on with the possibility to set scheduler weights on the device, like in

https://blogs.igalia.com/tursulin/drm-scheduling-cgroup-controller/

Maarten Lankhorst (5):
kernel/cgroup: Add "dev" memory accounting cgroup

Yeah, let's not use "dev" name for this. As Waiman pointed out, it conflicts with the devices controller from cgroup1. While cgroup1 is mostly deprecated, the same features are provided through BPF in systemd using the same terminologies, so this is going to be really confusing.

What happened with Tvrtko's weighted implementation? I've seen many proposed patchsets in this area but as far as I could see none could establish consensus among GPU crowd and that's one of the reasons why nothing ever landed. Is the aim of this patchset establishing such consensus?

If reaching consensus doesn't seem feasible in a predictable timeframe, my suggesstion is just extending the misc controller. If the only way forward here is fragmented vendor(s)-specific implementations, let's throw them into the misc controller.

Thanks.

Signature · 1 line
tejun
maintainer
#26 lore raw

Hi Tejun,

Thanks a lot for your review.

Quoted 17 lines · Tejun Heo
Quoted 16 lines · Maarten Lankhorst

New submission!
I've added documentation for each call, and integrated the renaming from drm cgroup to dev cgroup, based on maxime ripard's work.

Maxime has been testing this with dma-buf heaps and v4l2 too, and it seems to work. In the initial submission, I've decided to only add the smallest enablement possible, to have less chance of breaking things.

The API has been changed slightly, from "$name region.$regionname=$limit" in a file called dev.min/low/max to "$subsystem/$name $regionname=$limit" in a file called dev.region.min/low/max.

This hopefully allows us to perhaps extend the API later on with the possibility to set scheduler weights on the device, like in

https://blogs.igalia.com/tursulin/drm-scheduling-cgroup-controller/

Quoted 7 lines
Quoted 2 lines

Maarten Lankhorst (5):
kernel/cgroup: Add "dev" memory accounting cgroup

Yeah, let's not use "dev" name for this. As Waiman pointed out, it conflicts with the devices controller from cgroup1. While cgroup1 is mostly deprecated, the same features are provided through BPF in systemd using the same terminologies, so this is going to be really confusing.

Yeah, I agree. We switched to dev because we want to support more than just DRM, but all DMA-able memory. We have patches to support for v4l2 and dma-buf heaps, so using the name DRM didn't feel great either.

Do you have a better name in mind? "device memory"? "dma memory"?

Quoted 4 lines

What happened with Tvrtko's weighted implementation? I've seen many proposed patchsets in this area but as far as I could see none could establish consensus among GPU crowd and that's one of the reasons why nothing ever landed. Is the aim of this patchset establishing such consensus?

Yeah, we have a consensus by now I think. Valve, Intel, Google, and Red Hat have been involved in that series and we all agree on the implementation.

Tvrtko aims at a different feature set though: this one is about memory allocation limits, Tvrtko's about scheduling.

Scheduling doesn't make much sense for things outside of DRM (and even for a fraction of all DRM devices), and it's pretty much orthogonal. So i guess you can expect another series from Tvrtko, but I don't think they should be considered equivalent or dependent on each other.

Quoted 4 lines

If reaching consensus doesn't seem feasible in a predictable timeframe, my suggesstion is just extending the misc controller. If the only way forward here is fragmented vendor(s)-specific implementations, let's throw them into the misc controller.

I don't think we have a fragmented implementation here, at all. The last patch especially implements it for all devices implementing the GEM interface in DRM, which would be around 100 drivers from various vendors.

It's marked as a discussion because we don't quite know how to plumb it in for all drivers in the current DRM framework, but it's very much what we want to achieve.

Maxime

maintainer
#27 lore raw

Hello,

On Thu, Oct 24, 2024 at 09:20:43AM +0200, Maxime Ripard wrote:
...

Quoted 10 lines
Quoted 4 lines

Yeah, let's not use "dev" name for this. As Waiman pointed out, it conflicts with the devices controller from cgroup1. While cgroup1 is mostly deprecated, the same features are provided through BPF in systemd using the same terminologies, so this is going to be really confusing.

Yeah, I agree. We switched to dev because we want to support more than just DRM, but all DMA-able memory. We have patches to support for v4l2 and dma-buf heaps, so using the name DRM didn't feel great either.

Do you have a better name in mind? "device memory"? "dma memory"?

Maybe just dma (I think the term isn't used heavily anymore, so the word is kinda open)? But, hopefully, others have better ideas.

Quoted 7 lines
Quoted 4 lines

What happened with Tvrtko's weighted implementation? I've seen many proposed patchsets in this area but as far as I could see none could establish consensus among GPU crowd and that's one of the reasons why nothing ever landed. Is the aim of this patchset establishing such consensus?

Yeah, we have a consensus by now I think. Valve, Intel, Google, and Red Hat have been involved in that series and we all agree on the implementation.

That's great to hear.

Quoted 7 lines

Tvrtko aims at a different feature set though: this one is about memory allocation limits, Tvrtko's about scheduling.

Scheduling doesn't make much sense for things outside of DRM (and even for a fraction of all DRM devices), and it's pretty much orthogonal. So i guess you can expect another series from Tvrtko, but I don't think they should be considered equivalent or dependent on each other.

Yeah, I get that this is about memory and that is about processing capacity, so the plan is going for separate controllers for each? Or would it be better to present both under the same controller interface? Even if they're going to be separate controllers, we at least want to be aligned on how devices and their configurations are presented in the two controllers.

Thanks.

Signature · 1 line
tejun
maintainer
#28 lore raw
Quoted 36 lines · Tejun Heo

Hello,

On Thu, Oct 24, 2024 at 09:20:43AM +0200, Maxime Ripard wrote:
...

Quoted 10 lines
Quoted 4 lines

Yeah, let's not use "dev" name for this. As Waiman pointed out, it conflicts with the devices controller from cgroup1. While cgroup1 is mostly deprecated, the same features are provided through BPF in systemd using the same terminologies, so this is going to be really confusing.

Yeah, I agree. We switched to dev because we want to support more than just DRM, but all DMA-able memory. We have patches to support for v4l2 and dma-buf heaps, so using the name DRM didn't feel great either.

Do you have a better name in mind? "device memory"? "dma memory"?

Maybe just dma (I think the term isn't used heavily anymore, so the word is kinda open)? But, hopefully, others have better ideas.

Quoted 7 lines
Quoted 4 lines

What happened with Tvrtko's weighted implementation? I've seen many proposed patchsets in this area but as far as I could see none could establish consensus among GPU crowd and that's one of the reasons why nothing ever landed. Is the aim of this patchset establishing such consensus?

Yeah, we have a consensus by now I think. Valve, Intel, Google, and Red Hat have been involved in that series and we all agree on the implementation.

That's great to hear.

Quoted 7 lines

Tvrtko aims at a different feature set though: this one is about memory allocation limits, Tvrtko's about scheduling.

Scheduling doesn't make much sense for things outside of DRM (and even for a fraction of all DRM devices), and it's pretty much orthogonal. So i guess you can expect another series from Tvrtko, but I don't think they should be considered equivalent or dependent on each other.

Quoted 5 lines

Yeah, I get that this is about memory and that is about processing capacity, so the plan is going for separate controllers for each? Or would it be better to present both under the same controller interface? Even if they're going to be separate controllers, we at least want to be aligned on how devices and their configurations are presented in the two controllers.

It's still up in the air, I think.

My personal opinion is that there's only DRM (and accel) devices that really care about scheduling constraints anyway, so it wouldn't (have to) be as generic as this one.

And if we would call it dma, then the naming becomes a bit weird since DMA doesn't have much to do with scheduling.

But I guess it's just another instance of the "naming is hard" problem :)

Maxime

maintainer
#29 lore raw
Quoted 43 lines · Maxime Ripard
Quoted 36 lines · Tejun Heo

Hello,

On Thu, Oct 24, 2024 at 09:20:43AM +0200, Maxime Ripard wrote:
...

Quoted 10 lines
Quoted 4 lines

Yeah, let's not use "dev" name for this. As Waiman pointed out, it conflicts with the devices controller from cgroup1. While cgroup1 is mostly deprecated, the same features are provided through BPF in systemd using the same terminologies, so this is going to be really confusing.

Yeah, I agree. We switched to dev because we want to support more than just DRM, but all DMA-able memory. We have patches to support for v4l2 and dma-buf heaps, so using the name DRM didn't feel great either.

Do you have a better name in mind? "device memory"? "dma memory"?

Maybe just dma (I think the term isn't used heavily anymore, so the word is kinda open)? But, hopefully, others have better ideas.

Quoted 7 lines
Quoted 4 lines

What happened with Tvrtko's weighted implementation? I've seen many proposed patchsets in this area but as far as I could see none could establish consensus among GPU crowd and that's one of the reasons why nothing ever landed. Is the aim of this patchset establishing such consensus?

Yeah, we have a consensus by now I think. Valve, Intel, Google, and Red Hat have been involved in that series and we all agree on the implementation.

That's great to hear.

Quoted 7 lines

Tvrtko aims at a different feature set though: this one is about memory allocation limits, Tvrtko's about scheduling.

Scheduling doesn't make much sense for things outside of DRM (and even for a fraction of all DRM devices), and it's pretty much orthogonal. So i guess you can expect another series from Tvrtko, but I don't think they should be considered equivalent or dependent on each other.

Quoted 5 lines

Yeah, I get that this is about memory and that is about processing capacity, so the plan is going for separate controllers for each? Or would it be better to present both under the same controller interface? Even if they're going to be separate controllers, we at least want to be aligned on how devices and their configurations are presented in the two controllers.

Quoted 5 lines

It's still up in the air, I think.

My personal opinion is that there's only DRM (and accel) devices that really care about scheduling constraints anyway, so it wouldn't (have to) be as generic as this one.

If they represent different resources that aren't always controlled in conjunction, it makes sense to me to have separate controllers as well.

Especially if a merged version would have separate control files for each resource anyway (dev.region.*, dev.weight etc.)

Quoted 4 lines

And if we would call it dma, then the naming becomes a bit weird since DMA doesn't have much to do with scheduling.

But I guess it's just another instance of the "naming is hard" problem :)

Yes it would be good to have something catchy, easy on the eyes, and vaguely familiar. devcomp(ute), devproc, devcpu, devcycles all kind of suck. drm and gpu seem too specific for a set that includes npus and potentially other accelerators in the future.

I don't think we want to go full devspace & devtime, either, though.

How about dmem for this one, and dpu for the other one. For device memory and device processing unit, respectively.

maintainer
#30 lore raw
Quoted 66 lines · Johannes Weiner
Quoted 43 lines · Maxime Ripard
Quoted 36 lines · Tejun Heo

Hello,

On Thu, Oct 24, 2024 at 09:20:43AM +0200, Maxime Ripard wrote:
...

Quoted 10 lines
Quoted 4 lines

Yeah, let's not use "dev" name for this. As Waiman pointed out, it conflicts with the devices controller from cgroup1. While cgroup1 is mostly deprecated, the same features are provided through BPF in systemd using the same terminologies, so this is going to be really confusing.

Yeah, I agree. We switched to dev because we want to support more than just DRM, but all DMA-able memory. We have patches to support for v4l2 and dma-buf heaps, so using the name DRM didn't feel great either.

Do you have a better name in mind? "device memory"? "dma memory"?

Maybe just dma (I think the term isn't used heavily anymore, so the word is kinda open)? But, hopefully, others have better ideas.

Quoted 7 lines
Quoted 4 lines

What happened with Tvrtko's weighted implementation? I've seen many proposed patchsets in this area but as far as I could see none could establish consensus among GPU crowd and that's one of the reasons why nothing ever landed. Is the aim of this patchset establishing such consensus?

Yeah, we have a consensus by now I think. Valve, Intel, Google, and Red Hat have been involved in that series and we all agree on the implementation.

That's great to hear.

Quoted 7 lines

Tvrtko aims at a different feature set though: this one is about memory allocation limits, Tvrtko's about scheduling.

Scheduling doesn't make much sense for things outside of DRM (and even for a fraction of all DRM devices), and it's pretty much orthogonal. So i guess you can expect another series from Tvrtko, but I don't think they should be considered equivalent or dependent on each other.

Quoted 5 lines

Yeah, I get that this is about memory and that is about processing capacity, so the plan is going for separate controllers for each? Or would it be better to present both under the same controller interface? Even if they're going to be separate controllers, we at least want to be aligned on how devices and their configurations are presented in the two controllers.

Quoted 5 lines

It's still up in the air, I think.

My personal opinion is that there's only DRM (and accel) devices that really care about scheduling constraints anyway, so it wouldn't (have to) be as generic as this one.

If they represent different resources that aren't always controlled in conjunction, it makes sense to me to have separate controllers as well.

Especially if a merged version would have separate control files for each resource anyway (dev.region.*, dev.weight etc.)

Quoted 4 lines

And if we would call it dma, then the naming becomes a bit weird since DMA doesn't have much to do with scheduling.

But I guess it's just another instance of the "naming is hard" problem :)

Yes it would be good to have something catchy, easy on the eyes, and vaguely familiar. devcomp(ute), devproc, devcpu, devcycles all kind of suck. drm and gpu seem too specific for a set that includes npus and potentially other accelerators in the future.

Quoted 4 lines

I don't think we want to go full devspace & devtime, either, though.

How about dmem for this one, and dpu for the other one. For device memory and device processing unit, respectively.

dmem sounds great to me, does everyone agree?

Maxime

maintainer
#31 lore raw

On Wed, Nov 06, 2024 at 11:31:49AM +0100, Maxime Ripard wrote:
...

Quoted 4 lines
Quoted 2 lines

How about dmem for this one, and dpu for the other one. For device memory and device processing unit, respectively.

dmem sounds great to me, does everyone agree?

Sounds good to me.

Thanks.

Signature · 1 line
tejun
author maintainer
#32 lore raw

Hey,

Den 2024-11-06 kl. 19:20, skrev Tejun Heo:

Quoted 11 lines · Maxime Ripard

...

Quoted 4 lines
Quoted 2 lines

How about dmem for this one, and dpu for the other one. For device memory and device processing unit, respectively.

dmem sounds great to me, does everyone agree?

Sounds good to me.

Thanks.

Thanks for all feedback and discussion. I checked mostly on patchwork so I missed the discussion here. Fortunately it's only been about naming. :)

I'm thinking of adding a 'high' knob as well, that will work similarly to high in normal mem controller. (so not proportionally calculated like 'max', but (usage + allocated) < max = ok.

Recursively of course.

Cheers,
~Maarten

maintainer
#33 lore raw

Hello,

On Wed, Nov 13, 2024 at 03:58:25PM +0100, Maarten Lankhorst wrote:
...

Quoted 8 lines

Thanks for all feedback and discussion. I checked mostly on patchwork so I missed the discussion here. Fortunately it's only been about naming. :)

I'm thinking of adding a 'high' knob as well, that will work similarly to high in normal mem controller. (so not proportionally calculated like 'max', but (usage + allocated) < max = ok.

Recursively of course.

I'd be cautious about adding knobs. These being published API, it's easy to paint oneself into a corner. I suggest starting with what's essential.

Thanks.

Signature · 1 line
tejun