From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1CF59606A320 for ; Fri, 20 Nov 2026 05:43:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=HnwNyYEh Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 7660C10F0C5; Fri, 20 Nov 2026 05:43:05 +0000 (UTC) Received: from mta1.migadu.com (out-207.mta1.migadu.com [95.215.58.207]) by gabe.freedesktop.org (Postfix) with ESMTPS id A6DE710F0C5 for ; Fri, 20 Nov 2026 05:42:59 +0000 (UTC) X-Envelope-To: dri-devel@lists.freedesktop.org DKIM-Signature: a=rsa-sha256; bh=sygBjDYX2IBEsh5RgXxZv4L/2uT7TB2ul0lFCsAHpAY=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795153377; v=1; x=1795758177; b=HnwNyYEhKKOlUdgeUYJUJL4JYe667WQ34/ki58BF47Qx6FMrzuJzi/koQHY+tSl9fG6h2AKC 4jhvrHpMYNsO34J5V4AC+PVlfXiIS59hS21wgPH1wAZWjsXy+y5kxYSZhry1uDm3q+d6qSAuMmG rq2jRXrp8iKbnNfr5iXFW5/A= X-Envelope-To: dri-devel@lists.freedesktop.org Received: by smtp.migadu.com with ESMTPS id 5caa3697f8970349; Fri, 20 Nov 2026 05:42:57 +0000 X-Mizu-Trace-ID: 5caa3697f8970349 X-Migadu-Flow: FLOW_OUT From: Tao Cui To: Vivek Kasireddy , Gerd Hoffmann , "T.J. Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Albert Esteve Cc: Sumit Semwal , Philipp Stanner , Karl Mehltretter , dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, linux-mm@kvack.org, akpm@linux-foundation.org, cuitao@kylinos.cn Subject: [PATCH v2] dma-buf/udmabuf: account the page array metadata to memcg Date: Fri, 20 Nov 2026 13:42:41 +0800 Message-ID: <20261120054241.525109-1-cui.tao@linux.dev> X-Mailer: git-send-email 2.53.0 X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "dri-devel" X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Tao Cui 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 Signed-off-by: Tao Cui --- 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