Re: [PATCH RFC 11/12] drm/gem: Add cgroup memory accounting

Robin Murphy
2025-03-10T15:06:07Z

On 2025-03-10 12:06 pm, Maxime Ripard wrote:
> In order to support any device using the GEM support, let's charge any
> GEM DMA allocation into the dmem cgroup.
>
> Signed-off-by: Maxime Ripard <mripard@kernel.org>
> ---
> drivers/gpu/drm/drm_gem.c | 5 +++++
> drivers/gpu/drm/drm_gem_dma_helper.c | 6 ++++++
> include/drm/drm_device.h | 1 +
> include/drm/drm_gem.h | 2 ++
> 4 files changed, 14 insertions(+)
>
> diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c
> index ee811764c3df4b4e9c377a66afd4967512ba2001..e04733cb49353cf3ff9672d883b106a083f80d86 100644
> --- a/drivers/gpu/drm/drm_gem.c
> +++ b/drivers/gpu/drm/drm_gem.c
> @@ -108,10 +108,11 @@ drm_gem_init(struct drm_device *dev)
> dev->vma_offset_manager = vma_offset_manager;
> drm_vma_offset_manager_init(vma_offset_manager,
> DRM_FILE_PAGE_OFFSET_START,
> DRM_FILE_PAGE_OFFSET_SIZE);
>
> +
> return drmm_add_action(dev, drm_gem_init_release, NULL);
> }
>
> /**
> * drm_gem_object_init_with_mnt - initialize an allocated shmem-backed GEM
> @@ -973,10 +974,14 @@ drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
> * drm_gem_object_init().
> */
> void
> drm_gem_object_release(struct drm_gem_object *obj)
> {
> +
> + if (obj->cgroup_pool_state)
> + dmem_cgroup_uncharge(obj->cgroup_pool_state, obj->size);
> +
> if (obj->filp)
> fput(obj->filp);
>
> drm_gem_private_object_fini(obj);
>
> diff --git a/drivers/gpu/drm/drm_gem_dma_helper.c b/drivers/gpu/drm/drm_gem_dma_helper.c
> index 16988d316a6dc702310fa44c15c92dc67b82802b..6236feb67ddd6338f0f597a0606377e0352ca6ed 100644
> --- a/drivers/gpu/drm/drm_gem_dma_helper.c
> +++ b/drivers/gpu/drm/drm_gem_dma_helper.c
> @@ -104,10 +104,16 @@ __drm_gem_dma_create(struct drm_device *drm, size_t size, bool private)
> if (ret) {
> drm_gem_object_release(gem_obj);
> goto error;
> }
>
> + ret = dmem_cgroup_try_charge(dma_get_dmem_cgroup_region(drm->dev),
> + size,
> + &dma_obj->base.cgroup_pool_state, NULL);
> + if (ret)
> + goto error;

Doesn't that miss cleaning up gem_obj? However, surely you want the
accounting before the allocation anyway, like in the other cases.
Otherwise userspace is still able to allocate massive amounts of memory
and incur some of the associated side-effects of that, it just doesn't
get to keep said memory for very long :)

Thanks,
Robin.

> +
> return dma_obj;
>
> error:
> kfree(dma_obj);
> return ERR_PTR(ret);
> diff --git a/include/drm/drm_device.h b/include/drm/drm_device.h
> index c91f87b5242d7a499917eb4aeb6ca8350f856eb3..58987f39ba8718eb768f6261fb0a1fbf16b38549 100644
> --- a/include/drm/drm_device.h
> +++ b/include/drm/drm_device.h
> @@ -1,8 +1,9 @@
> #ifndef _DRM_DEVICE_H_
> #define _DRM_DEVICE_H_
>
> +#include <linux/cgroup_dmem.h>
> #include <linux/list.h>
> #include <linux/kref.h>
> #include <linux/mutex.h>
> #include <linux/idr.h>
>
> diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h
> index fdae947682cd0b7b06db5e35e120f049a0f30179..95fe8ed48a26204020bb47d6074689829c410465 100644
> --- a/include/drm/drm_gem.h
> +++ b/include/drm/drm_gem.h
> @@ -430,10 +430,12 @@ struct drm_gem_object {
> * @lru:
> *
> * The current LRU list that the GEM object is on.
> */
> struct drm_gem_lru *lru;
> +
> + struct dmem_cgroup_pool_state *cgroup_pool_state;
> };
>
> /**
> * DRM_GEM_FOPS - Default drm GEM file operations
> *
>