Skip to content

RFC dma: Enable dmem cgroup tracking

Maxime Ripard dri-devel linux-iommu linux-media linux-mm lkml 1 version In review

Get series with b4
b4 am [email protected] change-id 20250307-dmem-cgroups-73febced0989

Versions newest first

  1. v1 RFC Latest12 patches 18 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/12cma: Register dmem region for each cma regionmm/cma.c mm/cma.h+16 -10 replies
2/12cma: Provide accessor to cma dmem regioninclude/linux/cma.h mm/cma.c+16 -00 replies
3/12dma: coherent: Register dmem region for each coherent regionkernel/dma/coherent.c+12 -00 replies
4/12dma: coherent: Provide accessor to dmem regioninclude/linux/dma-map-ops.h kernel/dma/coherent.c+25 -00 replies
5/12dma: contiguous: Provide accessor to dmem regioninclude/linux/dma-map-ops.h+21 -00 replies
6/12dma: direct: Provide accessor to dmem regioninclude/linux/dma-direct.h kernel/dma/direct.c+10 -04 replies
7/12dma: Create default dmem region for DMA allocationskernel/dma/mapping.c+23 -00 replies
8/12dma: Provide accessor to dmem regioninclude/linux/dma-mapping.h kernel/dma/mapping.c+27 -00 replies
9/12dma-buf: Clear cgroup accounting on releasedrivers/dma-buf/dma-buf.c include/linux/dma-buf.h+12 -00 replies
10/12dma-buf: cma: Account for allocations in dmem cgroupdrivers/dma-buf/heaps/cma_heap.c+16 -20 replies
11/12drm/gem: Add cgroup memory accountingdrivers/gpu/drm/drm_gem.c drivers/gpu/drm/drm_gem_dma_helper.c include/drm/drm_device.h +1 more+14 -01 reply
12/12media: videobuf2: Track buffer allocations through the dmem cgroupdrivers/media/common/videobuf2/videobuf2-dma-contig.c+19 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH RFC 00/12] dma: Enable dmem cgroup tracking

Hi,

Here's preliminary work to enable dmem tracking for heavy users of DMA allocations on behalf of userspace: v4l2, DRM, and dma-buf heaps.

It's not really meant for inclusion at the moment, because I really don't like it that much, and would like to discuss solutions on how to make it nicer.

In particular, the dma dmem region accessors don't feel that great to me. It duplicates the logic to select the proper accessor in dma_alloc_attrs(), and it looks fragile and potentially buggy to me.

One solution I tried is to do the accounting in dma_alloc_attrs() directly, depending on a flag being set, similar to what __GFP_ACCOUNT is doing.

It didn't work because dmem initialises a state pointer when charging an allocation to a region, and expects that state pointer to be passed back when uncharging. Since dma_alloc_attrs() returns a void pointer to the allocated buffer, we need to put that state into a higher-level structure, such as drm_gem_object, or dma_buf.

Since we can't share the region selection logic, we need to get the region through some other mean. Another thing I consider was to return the region as part of the allocated buffer (through struct page or folio), but those are lost across the calls and dma_alloc_attrs() will only get a void pointer. So that's not doable without some heavy rework, if it's a good idea at all.

So yeah, I went for the dumbest possible solution with the accessors, hoping you could suggest a much smarter idea :)

Thanks,
Maxime

---
Maxime Ripard (12):
cma: Register dmem region for each cma region
cma: Provide accessor to cma dmem region
dma: coherent: Register dmem region for each coherent region
dma: coherent: Provide accessor to dmem region
dma: contiguous: Provide accessor to dmem region
dma: direct: Provide accessor to dmem region
dma: Create default dmem region for DMA allocations
dma: Provide accessor to dmem region
dma-buf: Clear cgroup accounting on release
dma-buf: cma: Account for allocations in dmem cgroup drm/gem: Add cgroup memory accounting
media: videobuf2: Track buffer allocations through the dmem cgroup

 drivers/dma-buf/dma-buf.c                          |  7 ++++
 drivers/dma-buf/heaps/cma_heap.c                   | 18 ++++++++--
 drivers/gpu/drm/drm_gem.c                          |  5 +++
 drivers/gpu/drm/drm_gem_dma_helper.c               |  6 ++++
 .../media/common/videobuf2/videobuf2-dma-contig.c  | 19 +++++++++++
 include/drm/drm_device.h                           |  1 +
 include/drm/drm_gem.h                              |  2 ++
 include/linux/cma.h                                |  9 +++++
 include/linux/dma-buf.h                            |  5 +++
 include/linux/dma-direct.h                         |  2 ++
 include/linux/dma-map-ops.h                        | 32 ++++++++++++++++++
 include/linux/dma-mapping.h                        | 11 ++++++
 kernel/dma/coherent.c                              | 26 +++++++++++++++
 kernel/dma/direct.c                                |  8 +++++
 kernel/dma/mapping.c                               | 39 ++++++++++++++++++++++
 mm/cma.c                                           | 21 +++++++++++-
 mm/cma.h                                           |  3 ++
 17 files changed, 211 insertions(+), 3 deletions(-)

---
base-commit: 55a2aa61ba59c138bd956afe0376ec412a7004cf
change-id: 20250307-dmem-cgroups-73febced0989

Best regards,

Signature · 1 line
Maxime Ripard <[email protected]>