On Nov 17, 2026 Albert Esteve aesteve@redhat.com wrote:
Shared-memory allocator services (e.g., Android's gralloc) allocate dma-bufs on behalf of client processes in different cgroups. Today those allocations are charged to the allocator's own cgroup, leaving client cgroups with no visibility into their actual memory consumption and defeating resource limits.
This series adds memory cgroup accounting for dma-buf allocations, building on T.J. Mercier's earlier series [1] and incorporating feedback from previous versions and LPC discussions [2].
Patch 1 introduces the MEMCG_DMABUF stat and the obj_cgroup_reserve_dmabuf()/obj_cgroup_cancel_dmabuf() and mem_cgroup_commit_dmabuf()/mem_cgroup_uncharge_dmabuf() helpers in mm/memcontrol.c
Patch 2 wires up the charging policy: dma_buf_export() stores exp_info.objcg and updates the stat, a new charge_fd field in DMA_HEAP_IOCTL_ALLOC accepts a pidfd or cgroupfd to identify the target cgroup, and __GFP_ACCOUNT is removed from system-heap allocations to avoid double counting.
Patch 3 adds DMA_BUF_IOCTL_XFER_CHARGE, a general ioctl on any dma-buf fd to transfer charges post-allocation. This covers the double-hop allocator case (SurfaceFlinger -> gralloc -> kernel) and works for buffers from any exporter that opts in. Charging is gated by dmabuf->objcg being set at export time, ensuring that transfer is only applied for buffers backed by system memory where size reflects actual bytes. If the receiving cgroup lacks capacity for the charge, the ioctl returns -ENOMEM and the original charge is preserved.
Patches 4-7 add access control for cross-cgroup charging. Letting userspace decide who to charge needs to be a privileged operation because it has material impact on OOM decision and resource limits. Charging outside the caller's own memcg subtree requires CAP_SYS_RESOURCE. On top of that, LSM hooks gate the two charge_fd flavours: security_memcg_charge_task() gates the pidfd path (target identified by task credentials) and security_memcg_charge_cgroup() gates the cgroupfd path (target identified by cgroup directory label), with SELinux implementations using process2:charge_to and cgroup:charge_to permissions.
I'm probably misunderstanding the description here, but in both cases (the pidfd or cgroupfd case), ultimately the cgroup is charged, yes? In the cgroupfd case it is more direct, but in the pidfd case it is still the cgroup associated with the process identified by pidfd, yes?
If that's the case - and it very well may not be (!) - it probably makes more sense to combine the two SELinux access controls into one. Yes, the LSM hooks and callbacks would likely need to be different due to the mechanics of it, but the permission check "cgroup/charge" could be the same.
Patch 8 adds selftests. The tests and vmtest script were mostly written by an LLM, closely following the style of the surrounding tests and the virtme-ng setup. All code has been reviewed and tested.
I think we need to have selinux-testsuite tests to go along with this, especially as this would be the first patchset to add cgroup controls. Historically I've pushed back on piecemeal cgroup additions, pushing for some basic design of how to manage access controls for resource management first, but since evidently no one wants to do that, we really do need tests.
https://github.com/SELinuxProject/selinux-testsuite
Known limitations:
- Single-owner model: a dma-buf carries one memcg charge regardless of how many processes share it.
Once again, I'm sure I'm misunderstanding things, but are you saying that multiple cgroups can end up sharing the same dma-buf despite only one of the cgroups taking the accounting hit?
Hopefully the charged cgroup is deterministic?
- Only memcg accounting. CMA heaps would additionally need dmem controller integration.
Do you have a rough idea of how that would work? I ask only because I'm interested in seeing if that would require an additional LSM hook or if there is some way we could leverage the same hook(s) used here.
- If a transfer of an uncharged buffer fails, the buffer remains charged to the original cgroup. The allocator (userspace) is expected to handle this (retry, free, ...).
- The LSM hooks are intentionally narrow. Broader cgroup access controls (e.g. for the memfd/kexec handover case [2]) could be a separate effort reusing the same pattern.
- Only system heap sets exp_info.objcg today. Other exporters (DRM, V4L2, etc.) can opt in by setting it, but are not covered by this series.
- Related to the above: lazy backing-store allocation is not supported. This can be extended later, e.g., by adding a flag on the dma-buf struct for explicitly marking when memory is committed.
Similar to the CMA case, any rough ideas on what this would look like?
Note on TTM/GPU accounting [3]: that effort charges system memory allocated for GPU buffers per page, with its own stats (gpu_active and gpu_reclaim). This series charges at the dma-buf level, for memory with no memory.stat category of its own; exporters whose memory is already charged by their allocator must not opt in, so the two do not overlap.
[1] https://lore.kernel.org/cgroups/20230109213809.418135-1-tjmercier@google.com... [2] https://lpc.events/event/20/contributions/2416/ [3] https://lore.kernel.org/all/20260706052330.1110909-1-airlied@gmail.com/
Signed-off-by: Albert Esteve aesteve@redhat.com
-- paul-moore.com