public inbox for linux-media@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/2] dma-buf: system_heap: account for system heap allocation in memcg
@ 2026-01-16 20:05 Eric Chanudet
  2026-01-16 20:05 ` [PATCH v3 1/2] dma-buf: heaps: add parameter to account allocations using cgroup Eric Chanudet
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Eric Chanudet @ 2026-01-16 20:05 UTC (permalink / raw)
  To: Sumit Semwal, Benjamin Gaignard, Brian Starkey, John Stultz,
	T.J. Mercier, Christian König, Maxime Ripard
  Cc: linux-media, dri-devel, linaro-mm-sig, linux-kernel,
	Eric Chanudet

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+4V3onfGDmHf1DMOXfDha6ddOA@mail.gmail.com/

Changes in v3:
- Declare mem_accounting in include/linux/dma-heap.h, since it's
  intended for heap implementations. Make it __read_mostly while at it.
- Link to v2: https://lore.kernel.org/r/20260113-dmabuf-heap-system-memcg-v2-0-e85722cc2f24@redhat.com

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 | 7 +++++--
 include/linux/dma-heap.h            | 2 ++
 3 files changed, 12 insertions(+), 2 deletions(-)
---
base-commit: 983d014aafb14ee5e4915465bf8948e8f3a723b5
change-id: 20260102-dmabuf-heap-system-memcg-c86a381d663a

Best regards,
-- 
Eric Chanudet <echanude@redhat.com>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v3 1/2] dma-buf: heaps: add parameter to account allocations using cgroup
  2026-01-16 20:05 [PATCH v3 0/2] dma-buf: system_heap: account for system heap allocation in memcg Eric Chanudet
@ 2026-01-16 20:05 ` Eric Chanudet
  2026-01-16 22:36   ` T.J. Mercier
  2026-01-16 20:05 ` [PATCH v3 2/2] dma-buf: system_heap: account for system heap allocation in memcg Eric Chanudet
  2026-01-19 13:33 ` [PATCH v3 0/2] " Maxime Ripard
  2 siblings, 1 reply; 9+ messages in thread
From: Eric Chanudet @ 2026-01-16 20:05 UTC (permalink / raw)
  To: Sumit Semwal, Benjamin Gaignard, Brian Starkey, John Stultz,
	T.J. Mercier, Christian König, Maxime Ripard
  Cc: linux-media, dri-devel, linaro-mm-sig, linux-kernel,
	Eric Chanudet

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 +++++
 include/linux/dma-heap.h   | 2 ++
 2 files changed, 7 insertions(+)

diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c
index 8ab49924f8b71a0272dc89a609539a429feaf6c8..d230ddeb24e0fa1f2d51cb5d2868ec54fc8376a8 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 __read_mostly 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)
diff --git a/include/linux/dma-heap.h b/include/linux/dma-heap.h
index 27d15f60950a2093e592be1b961c02e672826e58..648328a64b27eaf25c8b18809a02c6410cbbffde 100644
--- a/include/linux/dma-heap.h
+++ b/include/linux/dma-heap.h
@@ -46,4 +46,6 @@ const char *dma_heap_get_name(struct dma_heap *heap);
 
 struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info);
 
+extern bool mem_accounting;
+
 #endif /* _DMA_HEAPS_H */

-- 
2.52.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v3 2/2] dma-buf: system_heap: account for system heap allocation in memcg
  2026-01-16 20:05 [PATCH v3 0/2] dma-buf: system_heap: account for system heap allocation in memcg Eric Chanudet
  2026-01-16 20:05 ` [PATCH v3 1/2] dma-buf: heaps: add parameter to account allocations using cgroup Eric Chanudet
@ 2026-01-16 20:05 ` Eric Chanudet
  2026-01-16 22:36   ` T.J. Mercier
  2026-01-19 13:33 ` [PATCH v3 0/2] " Maxime Ripard
  2 siblings, 1 reply; 9+ messages in thread
From: Eric Chanudet @ 2026-01-16 20:05 UTC (permalink / raw)
  To: Sumit Semwal, Benjamin Gaignard, Brian Starkey, John Stultz,
	T.J. Mercier, Christian König, Maxime Ripard
  Cc: linux-media, dri-devel, linaro-mm-sig, linux-kernel,
	Eric Chanudet

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 | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c
index 4c782fe33fd497a74eb5065797259576f9b651b6..4049d042afa14ec2f4d034f59f1740600a20c1e5 100644
--- a/drivers/dma-buf/heaps/system_heap.c
+++ b/drivers/dma-buf/heaps/system_heap.c
@@ -320,14 +320,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;

-- 
2.52.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 1/2] dma-buf: heaps: add parameter to account allocations using cgroup
  2026-01-16 20:05 ` [PATCH v3 1/2] dma-buf: heaps: add parameter to account allocations using cgroup Eric Chanudet
@ 2026-01-16 22:36   ` T.J. Mercier
  2026-01-19  9:49     ` Christian König
  0 siblings, 1 reply; 9+ messages in thread
From: T.J. Mercier @ 2026-01-16 22:36 UTC (permalink / raw)
  To: Eric Chanudet
  Cc: Sumit Semwal, Benjamin Gaignard, Brian Starkey, John Stultz,
	Christian König, Maxime Ripard, linux-media, dri-devel,
	linaro-mm-sig, linux-kernel

On Fri, Jan 16, 2026 at 12:06 PM Eric Chanudet <echanude@redhat.com> wrote:
>
> 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>

Reviewed-by: T.J. Mercier <tjmercier@google.com>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 2/2] dma-buf: system_heap: account for system heap allocation in memcg
  2026-01-16 20:05 ` [PATCH v3 2/2] dma-buf: system_heap: account for system heap allocation in memcg Eric Chanudet
@ 2026-01-16 22:36   ` T.J. Mercier
  2026-01-19 13:09     ` Sumit Semwal
  0 siblings, 1 reply; 9+ messages in thread
From: T.J. Mercier @ 2026-01-16 22:36 UTC (permalink / raw)
  To: Eric Chanudet
  Cc: Sumit Semwal, Benjamin Gaignard, Brian Starkey, John Stultz,
	Christian König, Maxime Ripard, linux-media, dri-devel,
	linaro-mm-sig, linux-kernel

On Fri, Jan 16, 2026 at 12:06 PM Eric Chanudet <echanude@redhat.com> 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>

Reviewed-by: T.J. Mercier <tjmercier@google.com>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 1/2] dma-buf: heaps: add parameter to account allocations using cgroup
  2026-01-16 22:36   ` T.J. Mercier
@ 2026-01-19  9:49     ` Christian König
  2026-01-19 13:10       ` Sumit Semwal
  0 siblings, 1 reply; 9+ messages in thread
From: Christian König @ 2026-01-19  9:49 UTC (permalink / raw)
  To: T.J. Mercier, Eric Chanudet
  Cc: Sumit Semwal, Benjamin Gaignard, Brian Starkey, John Stultz,
	Maxime Ripard, linux-media, dri-devel, linaro-mm-sig,
	linux-kernel

On 1/16/26 23:36, T.J. Mercier wrote:
> On Fri, Jan 16, 2026 at 12:06 PM Eric Chanudet <echanude@redhat.com> wrote:
>>
>> 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>
> 
> Reviewed-by: T.J. Mercier <tjmercier@google.com>

Reviewed-by: Christian König <christian.koenig@amd.com>

Should I push that into drm-misc-next or does anybody else has commit access?

Regards,
Christian.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 2/2] dma-buf: system_heap: account for system heap allocation in memcg
  2026-01-16 22:36   ` T.J. Mercier
@ 2026-01-19 13:09     ` Sumit Semwal
  0 siblings, 0 replies; 9+ messages in thread
From: Sumit Semwal @ 2026-01-19 13:09 UTC (permalink / raw)
  To: T.J. Mercier
  Cc: Eric Chanudet, Benjamin Gaignard, Brian Starkey, John Stultz,
	Christian König, Maxime Ripard, linux-media, dri-devel,
	linaro-mm-sig, linux-kernel

Hello Eric,

On Sat, 17 Jan 2026 at 04:07, T.J. Mercier <tjmercier@google.com> wrote:
>
> On Fri, Jan 16, 2026 at 12:06 PM Eric Chanudet <echanude@redhat.com> 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>
>
> Reviewed-by: T.J. Mercier <tjmercier@google.com>

Thank you for the series; LGTM, I'll push it tonight to drm-misc-next.

Best,
Sumit.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 1/2] dma-buf: heaps: add parameter to account allocations using cgroup
  2026-01-19  9:49     ` Christian König
@ 2026-01-19 13:10       ` Sumit Semwal
  0 siblings, 0 replies; 9+ messages in thread
From: Sumit Semwal @ 2026-01-19 13:10 UTC (permalink / raw)
  To: Christian König
  Cc: T.J. Mercier, Eric Chanudet, Benjamin Gaignard, Brian Starkey,
	John Stultz, Maxime Ripard, linux-media, dri-devel, linaro-mm-sig,
	linux-kernel

Hi Christian,

On Mon, 19 Jan 2026 at 15:19, Christian König <christian.koenig@amd.com> wrote:
>
> On 1/16/26 23:36, T.J. Mercier wrote:
> > On Fri, Jan 16, 2026 at 12:06 PM Eric Chanudet <echanude@redhat.com> wrote:
> >>
> >> 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>
> >
> > Reviewed-by: T.J. Mercier <tjmercier@google.com>
>
> Reviewed-by: Christian König <christian.koenig@amd.com>
LGTM as well.
>
> Should I push that into drm-misc-next or does anybody else has commit access?
>
I will push it to drm-misc-next tonight. Thanks for your review!
> Regards,
> Christian.

Best,
Sumit.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 0/2] dma-buf: system_heap: account for system heap allocation in memcg
  2026-01-16 20:05 [PATCH v3 0/2] dma-buf: system_heap: account for system heap allocation in memcg Eric Chanudet
  2026-01-16 20:05 ` [PATCH v3 1/2] dma-buf: heaps: add parameter to account allocations using cgroup Eric Chanudet
  2026-01-16 20:05 ` [PATCH v3 2/2] dma-buf: system_heap: account for system heap allocation in memcg Eric Chanudet
@ 2026-01-19 13:33 ` Maxime Ripard
  2 siblings, 0 replies; 9+ messages in thread
From: Maxime Ripard @ 2026-01-19 13:33 UTC (permalink / raw)
  To: Eric Chanudet
  Cc: dri-devel, linaro-mm-sig, linux-kernel, linux-media,
	Benjamin Gaignard, Brian Starkey, Christian König,
	John Stultz, Maxime Ripard, Maxime Ripard, Sumit Semwal,
	T.J. Mercier

On Fri, 16 Jan 2026 15:05:37 -0500, Eric Chanudet 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/
> 
> [ ... ]

Reviewed-by: Maxime Ripard <mripard@kernel.org>

Thanks!
Maxime

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-01-19 13:33 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-16 20:05 [PATCH v3 0/2] dma-buf: system_heap: account for system heap allocation in memcg Eric Chanudet
2026-01-16 20:05 ` [PATCH v3 1/2] dma-buf: heaps: add parameter to account allocations using cgroup Eric Chanudet
2026-01-16 22:36   ` T.J. Mercier
2026-01-19  9:49     ` Christian König
2026-01-19 13:10       ` Sumit Semwal
2026-01-16 20:05 ` [PATCH v3 2/2] dma-buf: system_heap: account for system heap allocation in memcg Eric Chanudet
2026-01-16 22:36   ` T.J. Mercier
2026-01-19 13:09     ` Sumit Semwal
2026-01-19 13:33 ` [PATCH v3 0/2] " Maxime Ripard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox