Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] dma-buf/udmabuf: account the page array metadata to memcg
@ 2026-11-20  5:42 Tao Cui
  0 siblings, 0 replies; only message in thread
From: Tao Cui @ 2026-11-20  5:42 UTC (permalink / raw)
  To: Vivek Kasireddy, Gerd Hoffmann, T.J. Mercier,
	Christian König, Albert Esteve
  Cc: Sumit Semwal, Philipp Stanner, Karl Mehltretter, dri-devel,
	linux-media, linaro-mm-sig, linux-mm, akpm, cuitao

From: Tao Cui <cuitao@kylinos.cn>

udmabuf pins the pages of a memfd and hands them out as a dma-buf.  The
data pages keep the accounting of the memfd they were instantiated in,
but the bookkeeping around them does not: the page pointer array and
the pinned_folios array, both sized by the buffer page count, plus the
transient folios array sized by the memfd folio count, are plain
GFP_KERNEL.  A 1GB buffer costs about 4MB of uncharged SUnreclaim
memory, and size_limit_mb defaults to INT_MAX.

Charge the three kvmalloc allocations so the creator pays for the
metadata it induces.  The data pages stay with the memfd they were
instantiated in, including hugetlb memfds, which remain uncharged.

The system heap in the same subsystem accounts its buffer allocation
only when the mem_accounting module param is set; Android runs with it
off because heap buffers change ownership mid lifetime, and explicit
charge transfer for dma-buf memory is being worked on.  The metadata
here is charged to the creator directly, which sidesteps the transfer
problem for a resource that does keep a single owner: the memfd.

Reviewed-by: T.J. Mercier <tjmercier@google.com>
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
Changes in v2:
- commit message: note that the system heap accounting is gated by the
  mem_accounting module param, which Android keeps off, and that
  hugetlb memfds stay uncharged.  No functional change.
Link to v1: https://lore.kernel.org/r/20261106131829.3228926-3-cui.tao@linux.dev

 drivers/dma-buf/udmabuf.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index df6dd0046242..40182c82df6b 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -190,11 +190,12 @@ static void unpin_all_folios(struct udmabuf *ubuf)
 
 static __always_inline int init_udmabuf(struct udmabuf *ubuf, pgoff_t pgcnt)
 {
-	ubuf->pages = kvmalloc_objs(*ubuf->pages, pgcnt);
+	ubuf->pages = kvmalloc_objs(*ubuf->pages, pgcnt, GFP_KERNEL_ACCOUNT);
 	if (!ubuf->pages)
 		return -ENOMEM;
 
-	ubuf->pinned_folios = kvmalloc_objs(*ubuf->pinned_folios, pgcnt);
+	ubuf->pinned_folios = kvmalloc_objs(*ubuf->pinned_folios, pgcnt,
+					    GFP_KERNEL_ACCOUNT);
 	if (!ubuf->pinned_folios)
 		return -ENOMEM;
 
@@ -387,7 +388,7 @@ static long udmabuf_create(struct miscdevice *device,
 	if (ret)
 		goto err;
 
-	folios = kvmalloc_objs(*folios, max_nr_folios);
+	folios = kvmalloc_objs(*folios, max_nr_folios, GFP_KERNEL_ACCOUNT);
 	if (!folios) {
 		ret = -ENOMEM;
 		goto err;
-- 
2.43.0



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-20  5:43 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20  5:42 [PATCH v2] dma-buf/udmabuf: account the page array metadata to memcg Tao Cui

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