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(-)
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
Signed-off-by: Maxime Ripard <mripard@kernel.org>
---
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,
--
Maxime Ripard <mripard@kernel.org>
[Adding Ben since we are currently in the middle of a discussion regarding exactly that problem] Just for my understanding before I deep dive into the code: This uses a separate dmem cgroup and does not account against memcg, don't it? Thanks, Christian. Am 10.03.25 um 13:06 schrieb Maxime Ripard: > 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 > > Signed-off-by: Maxime Ripard <mripard@kernel.org> > --- > 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,
Hi, On Mon, Mar 10, 2025 at 03:16:53PM +0100, Christian König wrote: > [Adding Ben since we are currently in the middle of a discussion > regarding exactly that problem] > > Just for my understanding before I deep dive into the code: This uses > a separate dmem cgroup and does not account against memcg, don't it? Yes. The main rationale being that it doesn't always make sense to register against memcg: a lot of devices are going to allocate from dedicated chunks of memory that are either carved out from the main memory allocator, or not under Linux supervision at all. And if there's no way to make it consistent across drivers, it's not the right tool. Maxime
On Tue, 11 Mar 2025 at 00:26, Maxime Ripard <mripard@kernel.org> wrote: > > Hi, > > On Mon, Mar 10, 2025 at 03:16:53PM +0100, Christian König wrote: > > [Adding Ben since we are currently in the middle of a discussion > > regarding exactly that problem] > > > > Just for my understanding before I deep dive into the code: This uses > > a separate dmem cgroup and does not account against memcg, don't it? > > Yes. The main rationale being that it doesn't always make sense to > register against memcg: a lot of devices are going to allocate from > dedicated chunks of memory that are either carved out from the main > memory allocator, or not under Linux supervision at all. > > And if there's no way to make it consistent across drivers, it's not the > right tool. > While I agree on that, if a user can cause a device driver to allocate memory that is also memory that memcg accounts, then we have to interface with memcg to account that memory. The pathological case would be a single application wanting to use 90% of RAM for device allocations, freeing it all, then using 90% of RAM for normal usage. How to create a policy that would allow that with dmem and memcg is difficult, since if you say you can do 90% on both then the user can easily OOM the system. Dave. > Maxime
Am 31.03.25 um 22:43 schrieb Dave Airlie: > On Tue, 11 Mar 2025 at 00:26, Maxime Ripard <mripard@kernel.org> wrote: >> Hi, >> >> On Mon, Mar 10, 2025 at 03:16:53PM +0100, Christian König wrote: >>> [Adding Ben since we are currently in the middle of a discussion >>> regarding exactly that problem] >>> >>> Just for my understanding before I deep dive into the code: This uses >>> a separate dmem cgroup and does not account against memcg, don't it? >> Yes. The main rationale being that it doesn't always make sense to >> register against memcg: a lot of devices are going to allocate from >> dedicated chunks of memory that are either carved out from the main >> memory allocator, or not under Linux supervision at all. >> >> And if there's no way to make it consistent across drivers, it's not the >> right tool. >> > While I agree on that, if a user can cause a device driver to allocate > memory that is also memory that memcg accounts, then we have to > interface with memcg to account that memory. This assumes that memcg should be in control of device driver allocated memory. Which in some cases is intentionally not done. E.g. a server application which allocates buffers on behalves of clients gets a nice deny of service problem if we suddenly start to account those buffers. That was one of the reasons why my OOM killer improvement patches never landed (e.g. you could trivially kill X/Wayland or systemd with that). > The pathological case would be a single application wanting to use 90% > of RAM for device allocations, freeing it all, then using 90% of RAM > for normal usage. How to create a policy that would allow that with > dmem and memcg is difficult, since if you say you can do 90% on both > then the user can easily OOM the system. Yeah, completely agree. That's why the GTT size limit we already have per device and the global 50% TTM limit doesn't work as expected. People also didn't liked those limits and because of that we even have flags to circumvent them, see AMDGPU_GEM_CREATE_PREEMPTIBLE and TTM_TT_FLAG_EXTERNAL. Another problem is when and to which process we account things when eviction happens? For example process A wants to use VRAM that process B currently occupies. In this case we would give both processes a mix of VRAM and system memory, but how do we account that? If we account to process B then it can be that process A fails because of process Bs memcg limit. This creates a situation which is absolutely not traceable for a system administrator. But process A never asked for system memory in the first place, so we can't account the memory to it either or otherwise we make the process responsible for things it didn't do. There are good argument for all solutions and there are a couple of blocks which rule out one solution or another for a certain use case. To summarize I think the whole situation is a complete mess. Maybe there is not this one solution and we need to make it somehow configurable? Regards, Christian. > > Dave. >> Maxime
On Tue, 1 Apr 2025 at 21:03, Christian König <christian.koenig@amd.com> wrote: > > Am 31.03.25 um 22:43 schrieb Dave Airlie: > > On Tue, 11 Mar 2025 at 00:26, Maxime Ripard <mripard@kernel.org> wrote: > >> Hi, > >> > >> On Mon, Mar 10, 2025 at 03:16:53PM +0100, Christian König wrote: > >>> [Adding Ben since we are currently in the middle of a discussion > >>> regarding exactly that problem] > >>> > >>> Just for my understanding before I deep dive into the code: This uses > >>> a separate dmem cgroup and does not account against memcg, don't it? > >> Yes. The main rationale being that it doesn't always make sense to > >> register against memcg: a lot of devices are going to allocate from > >> dedicated chunks of memory that are either carved out from the main > >> memory allocator, or not under Linux supervision at all. > >> > >> And if there's no way to make it consistent across drivers, it's not the > >> right tool. > >> > > While I agree on that, if a user can cause a device driver to allocate > > memory that is also memory that memcg accounts, then we have to > > interface with memcg to account that memory. > > This assumes that memcg should be in control of device driver allocated memory. Which in some cases is intentionally not done. > > E.g. a server application which allocates buffers on behalves of clients gets a nice deny of service problem if we suddenly start to account those buffers. Yes we definitely need the ability to transfer an allocation between cgroups for this case. > > That was one of the reasons why my OOM killer improvement patches never landed (e.g. you could trivially kill X/Wayland or systemd with that). > > > The pathological case would be a single application wanting to use 90% > > of RAM for device allocations, freeing it all, then using 90% of RAM > > for normal usage. How to create a policy that would allow that with > > dmem and memcg is difficult, since if you say you can do 90% on both > > then the user can easily OOM the system. > > Yeah, completely agree. > > That's why the GTT size limit we already have per device and the global 50% TTM limit doesn't work as expected. People also didn't liked those limits and because of that we even have flags to circumvent them, see AMDGPU_GEM_CREATE_PREEMPTIBLE and TTM_TT_FLAG_EXTERNAL. > > Another problem is when and to which process we account things when eviction happens? For example process A wants to use VRAM that process B currently occupies. In this case we would give both processes a mix of VRAM and system memory, but how do we account that? > > If we account to process B then it can be that process A fails because of process Bs memcg limit. This creates a situation which is absolutely not traceable for a system administrator. > > But process A never asked for system memory in the first place, so we can't account the memory to it either or otherwise we make the process responsible for things it didn't do. > > There are good argument for all solutions and there are a couple of blocks which rule out one solution or another for a certain use case. To summarize I think the whole situation is a complete mess. > > Maybe there is not this one solution and we need to make it somehow configurable? My feeling is that we can't solve the VRAM eviction problem super effectively, but it's also probably not going to be a major common case, I don't think we should double account memcg/dmem just in case we have to evict all of a users dmem at some point, maybe if there was some kind of soft memcg limit we could add as an accounting but not enforced overhead it might be useful to track evictions, but yes we can't have A allocating memory causing B to fall over because we evict memory into it's memcg space and it fails to allocate the next time it tries, or having A fail in that case. For the UMA GPU case where there is no device memory or eviction problem, perhaps a configurable option to just say account memory in memcg for all allocations done by this process, and state yes you can work around it with allocation servers or whatever but the behaviour for well behaved things is at least somewhat defined. Dave.
On Mon, Mar 10, 2025 at 01:06:06PM +0100, Maxime Ripard wrote: > 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. One thing I forgot to mention is that it makes it harder than it could for subsystems that can allocate from multiple allocators (like... all the ones included in this series at least). I only added proper tracking in the backends using dma_alloc_attrs(), but they also support vmalloc. In what region vmalloc allocations should be tracked (if any) is an open-question to me. Similarly, some use dma_alloc_noncontiguous(). Also, I've set the size of the "default" DMA allocation region to U64_MAX, but that's obviously wrong and will break any relative metric. I'm not sure what would be the correct size though. Maxime
On Mon, Mar 10, 2025 at 01:06:06PM +0100, Maxime Ripard wrote: > 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 :) I've had a private chat with Maxime to get him up to speed on hopefully a lot of the past discussions, but probably best I put my notes here too. Somewhat unstructured list of challenges with trying to account all the memory for gpu/isp/camera/whatever: - At LPC in Dublin I think we've pretty much reached the conclusion that normal struct page memory should be just accounted in memcg. Otherwise you just get really nasty double-accounting chaos or issues where you can exhaust reserves. - We did not figure out what to do with mixed stuff like CMA, where we probably want to account it both into memcg (because it's struct page) but also separately into dmem (because the CMA region is a limited resource and only using memcg will not help us manage it). - There's the entire chaos of carve-out vs CMA and how userspace can figure out how to set reasonable limits automatically. Maxime brought the issue that limits need to be adjusted if carve-out/CMA/shmem aren't accounted the same, which I think is a valid concern. But due to the above conclusion around memcg accounting I think that's unavoidable, so we need some means for userspace to autoconfigure reasonable limits. Then that autoconfig can be done on each boot, and kernel (or dt or whatever) changes between these three allocators don't matter anymore. - Autoconfiguration challenges also exist for split display/render SoC. It gets even more fun if you also throw in camera and media codecs, and even more fun if you have multiple CMA regions. - Discrete gpu also has a very fun autoconfiguration issue because you have dmem limits for vram, and memcg limits for system memory. Vram might be swapped out to system memory, so naively you might want to assume that you need higher memcg limits than dmem limits. But there's systems with more dmem and smem (because the cpu with its memory is essentially just the co-processor that orchestrates the real compute machine, which is all gpus). - We need a charge transfer, least for Android since there all memory is allocated through binder. TJ Mercier did some patches: https://lore.kernel.org/dri-devel/20230123191728.2928839-3-tjmercier@google.com/ Ofc with dmem this would need to work for both dmem and memcg charges, since with CMA and discrete gpu we'll have bo that are tracked in both. - Hard limits for shmem/ttm drivers need a memcg-aware shrinker. TTM doesn't even have a shrinker yet, but with xe we now have a helper-library approach to enabling shrinking for TTM drivers. memcg-aware shrinking will be a large step up in complexity on top (and probably a good reason to switch over to the common shrinker lru instead of hand-rolling). See the various attempts at ttm shrinkers by Christian König and Thomas Hellstrom over the past years on dri-devel. This also means that most likely cgroup limit enforcement for ttm based drivers will be per-driver or at least very uneven. - Hard limits for dmem vram means ttm eviction needs to be able to account the evicted bo against the right memcg. Because this can happen in random other threads (cs ioctl of another process, kernel threads) accounting this correctly is going to be "fun". Plus I haven't thought through interactions with memcg-aware shrinkers, which might cause some really fundamental issues. - We also ideally need pin account, but I don't think we have any consensus on how to do that for memcg memory. Thus far it's all functionality-specific limits (e.g. mlock, rdma has its own for long-term pinned memory), not sure it makes sense to push for a unified tracking in memcg here? For dmem I think it's pretty easy, but there the question is how to differentiate between dmem that's always pinned (cma, I don't think anyone bothered with a shrinker for cma memory, vc4 maybe?) and dmem that generally has a shrinker and really wants a separate pin limit (vram/ttm drivers). - Unfortunately on top of the sometimes very high individual complexity these issues also all interact. Which means that we won't be able to roll this out in one go, and we need to cope with very uneven enforcement. I think trying to allow userspace to cope with changing cgroup support through autoconfiguration is the most feasible way out of this challenge. tldr; cgroup for device memory is a really complex mess Cheers, Sima > Thanks, > Maxime > > Signed-off-by: Maxime Ripard <mripard@kernel.org> > --- > 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, > -- > Maxime Ripard <mripard@kernel.org> > -- Simona Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch
© 2016 - 2026 Red Hat, Inc.