Capture dmabuf system heap allocations in memcg following prior conversations[1][2]. Disable this behavior by default unless configured by "dma_heap.mem_accounting" module parameter.
[1] https://lore.kernel.org/dri-devel/Z-5GZ3kJDbhgVBPG@phenom.ffwll.local/ [2] https://lore.kernel.org/all/CABdmKX2_UOENujpW0dXe0Z0x+4V3onfGDmHf1DMOXfDha6d...
Changes in v2: - Add a module parameter to enable dma-buf cgroup accounting, disabled by default. - Split system_heap logic in its own commit. - Link to v1: https://lore.kernel.org/lkml/20251211193106.755485-2-echanude@redhat.com/
Signed-off-by: Eric Chanudet echanude@redhat.com --- Eric Chanudet (2): dma-buf: heaps: add parameter to account allocations using cgroup dma-buf: system_heap: account for system heap allocation in memcg
drivers/dma-buf/dma-heap.c | 5 +++++ drivers/dma-buf/heaps/system_heap.c | 9 +++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) --- base-commit: b71e635feefc852405b14620a7fc58c4c80c0f73 change-id: 20260102-dmabuf-heap-system-memcg-c86a381d663a
Best regards,
Add a parameter to enable dma-buf heaps allocation accounting using cgroup for heaps that implement it. It is disabled by default as doing so incurs caveats based on how memcg currently accounts for shared buffers.
Signed-off-by: Eric Chanudet echanude@redhat.com --- drivers/dma-buf/dma-heap.c | 5 +++++ 1 file changed, 5 insertions(+)
diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c index 8ab49924f8b71a0272dc89a609539a429feaf6c8..f72e93cb8367fa196e5b87e762a60463023887df 100644 --- a/drivers/dma-buf/dma-heap.c +++ b/drivers/dma-buf/dma-heap.c @@ -49,6 +49,11 @@ static dev_t dma_heap_devt; static struct class *dma_heap_class; static DEFINE_XARRAY_ALLOC(dma_heap_minors);
+bool mem_accounting; +module_param(mem_accounting, bool, 0444); +MODULE_PARM_DESC(mem_accounting, + "Enable cgroup-based memory accounting for dma-buf heap allocations (default=false)."); + static int dma_heap_buffer_alloc(struct dma_heap *heap, size_t len, u32 fd_flags, u64 heap_flags)
The system dma-buf heap lets userspace allocate buffers from the page allocator. However, these allocations are not accounted for in memcg, allowing processes to escape limits that may be configured.
Pass __GFP_ACCOUNT for system heap allocations, based on the dma_heap.mem_accounting parameter, to use memcg and account for them.
Signed-off-by: Eric Chanudet echanude@redhat.com --- drivers/dma-buf/heaps/system_heap.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c index 4c782fe33fd497a74eb5065797259576f9b651b6..139b50df64ed4c4a6fdd69f25fe48324fbe2c481 100644 --- a/drivers/dma-buf/heaps/system_heap.c +++ b/drivers/dma-buf/heaps/system_heap.c @@ -52,6 +52,8 @@ static gfp_t order_flags[] = {HIGH_ORDER_GFP, HIGH_ORDER_GFP, LOW_ORDER_GFP}; static const unsigned int orders[] = {8, 4, 0}; #define NUM_ORDERS ARRAY_SIZE(orders)
+extern bool mem_accounting; + static int dup_sg_table(struct sg_table *from, struct sg_table *to) { struct scatterlist *sg, *new_sg; @@ -320,14 +322,17 @@ static struct page *alloc_largest_available(unsigned long size, { struct page *page; int i; + gfp_t flags;
for (i = 0; i < NUM_ORDERS; i++) { if (size < (PAGE_SIZE << orders[i])) continue; if (max_order < orders[i]) continue; - - page = alloc_pages(order_flags[i], orders[i]); + flags = order_flags[i]; + if (mem_accounting) + flags |= __GFP_ACCOUNT; + page = alloc_pages(flags, orders[i]); if (!page) continue; return page;
On 1/13/26 22:32, Eric Chanudet wrote:
The system dma-buf heap lets userspace allocate buffers from the page allocator. However, these allocations are not accounted for in memcg, allowing processes to escape limits that may be configured.
Pass __GFP_ACCOUNT for system heap allocations, based on the dma_heap.mem_accounting parameter, to use memcg and account for them.
Signed-off-by: Eric Chanudet echanude@redhat.com
drivers/dma-buf/heaps/system_heap.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c index 4c782fe33fd497a74eb5065797259576f9b651b6..139b50df64ed4c4a6fdd69f25fe48324fbe2c481 100644 --- a/drivers/dma-buf/heaps/system_heap.c +++ b/drivers/dma-buf/heaps/system_heap.c @@ -52,6 +52,8 @@ static gfp_t order_flags[] = {HIGH_ORDER_GFP, HIGH_ORDER_GFP, LOW_ORDER_GFP}; static const unsigned int orders[] = {8, 4, 0}; #define NUM_ORDERS ARRAY_SIZE(orders) +extern bool mem_accounting;
Please define that in some header. Apart from that looks good technically.
But after the discussion it sounds more and more like we don't want to account device driver allocated memory in memcg at all.
Regards, Christian.
static int dup_sg_table(struct sg_table *from, struct sg_table *to) { struct scatterlist *sg, *new_sg; @@ -320,14 +322,17 @@ static struct page *alloc_largest_available(unsigned long size, { struct page *page; int i;
- gfp_t flags;
for (i = 0; i < NUM_ORDERS; i++) { if (size < (PAGE_SIZE << orders[i])) continue; if (max_order < orders[i]) continue;
page = alloc_pages(order_flags[i], orders[i]);
flags = order_flags[i];if (mem_accounting)flags |= __GFP_ACCOUNT; if (!page) continue; return page;page = alloc_pages(flags, orders[i]);
On Wed, Jan 14, 2026 at 11:38:27AM +0100, Christian König wrote:
On 1/13/26 22:32, Eric Chanudet wrote:
The system dma-buf heap lets userspace allocate buffers from the page allocator. However, these allocations are not accounted for in memcg, allowing processes to escape limits that may be configured.
Pass __GFP_ACCOUNT for system heap allocations, based on the dma_heap.mem_accounting parameter, to use memcg and account for them.
Signed-off-by: Eric Chanudet echanude@redhat.com
drivers/dma-buf/heaps/system_heap.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c index 4c782fe33fd497a74eb5065797259576f9b651b6..139b50df64ed4c4a6fdd69f25fe48324fbe2c481 100644 --- a/drivers/dma-buf/heaps/system_heap.c +++ b/drivers/dma-buf/heaps/system_heap.c @@ -52,6 +52,8 @@ static gfp_t order_flags[] = {HIGH_ORDER_GFP, HIGH_ORDER_GFP, LOW_ORDER_GFP}; static const unsigned int orders[] = {8, 4, 0}; #define NUM_ORDERS ARRAY_SIZE(orders) +extern bool mem_accounting;
Please define that in some header. Apart from that looks good technically.
Thank you for the review, I can move it to linux/dma-heap.h in a v3 since it's intended for other heaps as well.
But after the discussion it sounds more and more like we don't want to account device driver allocated memory in memcg at all.
From the threads in v1 I thought adding the switch left open a consideration to use memcg with driver allocated memory for userspace, even with the known caveats that implies. Re-reading your last reply[1], that's not quite the case it sounds like.
Best,
[1] https://lore.kernel.org/all/e38d87d3-a114-43f9-be93-03e9b9f40844@amd.com/
Regards, Christian.
static int dup_sg_table(struct sg_table *from, struct sg_table *to) { struct scatterlist *sg, *new_sg; @@ -320,14 +322,17 @@ static struct page *alloc_largest_available(unsigned long size, { struct page *page; int i;
- gfp_t flags;
for (i = 0; i < NUM_ORDERS; i++) { if (size < (PAGE_SIZE << orders[i])) continue; if (max_order < orders[i]) continue;
page = alloc_pages(order_flags[i], orders[i]);
flags = order_flags[i];if (mem_accounting)flags |= __GFP_ACCOUNT; if (!page) continue; return page;page = alloc_pages(flags, orders[i]);
On Tue, Jan 13, 2026 at 1:33 PM Eric Chanudet echanude@redhat.com wrote:
Capture dmabuf system heap allocations in memcg following prior conversations[1][2]. Disable this behavior by default unless configured by "dma_heap.mem_accounting" module parameter.
[1] https://lore.kernel.org/dri-devel/Z-5GZ3kJDbhgVBPG@phenom.ffwll.local/ [2] https://lore.kernel.org/all/CABdmKX2_UOENujpW0dXe0Z0x+4V3onfGDmHf1DMOXfDha6d...
Changes in v2:
- Add a module parameter to enable dma-buf cgroup accounting, disabled by default.
- Split system_heap logic in its own commit.
- Link to v1: https://lore.kernel.org/lkml/20251211193106.755485-2-echanude@redhat.com/
Signed-off-by: Eric Chanudet echanude@redhat.com
Eric Chanudet (2): dma-buf: heaps: add parameter to account allocations using cgroup dma-buf: system_heap: account for system heap allocation in memcg
drivers/dma-buf/dma-heap.c | 5 +++++ drivers/dma-buf/heaps/system_heap.c | 9 +++++++-- 2 files changed, 12 insertions(+), 2 deletions(-)
base-commit: b71e635feefc852405b14620a7fc58c4c80c0f73 change-id: 20260102-dmabuf-heap-system-memcg-c86a381d663a
Best regards,
Eric Chanudet echanude@redhat.com
Thanks for adding the param, LGTM. Looking forward to v3.
linaro-mm-sig@lists.linaro.org