From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 00CE4CD4851 for ; Wed, 13 May 2026 12:41:50 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 5D4DA10E2FF; Wed, 13 May 2026 12:41:50 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="glLcNvJA"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id EE62610E2FF for ; Wed, 13 May 2026 12:41:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1778676108; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zOEhaYXxHcDCT/VWabJKKAhll1HKQfDPQhsQeGsErgM=; b=glLcNvJAbuHXqjFH+IUwikIaFk6ryv03dLyEoBu+0uZzJsVl5RhfTvQyJGhrrlfKdY71E0 RHKHlfKzZHRXAvI+C66XebmYW9eJRYPDDBKTdlc7uGvPtj7xondSBhT/LMs721Uk7EMJ3q KcwUzIh3G9O9gObhLHUHmhFU6nUpwjs= Received: from mail-yw1-f200.google.com (mail-yw1-f200.google.com [209.85.128.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-223-lcIM0ckfMX6gGn1taCaE1w-1; Wed, 13 May 2026 08:41:47 -0400 X-MC-Unique: lcIM0ckfMX6gGn1taCaE1w-1 X-Mimecast-MFC-AGG-ID: lcIM0ckfMX6gGn1taCaE1w_1778676106 Received: by mail-yw1-f200.google.com with SMTP id 00721157ae682-7bd795b6288so134717767b3.0 for ; Wed, 13 May 2026 05:41:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778676106; x=1779280906; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=zOEhaYXxHcDCT/VWabJKKAhll1HKQfDPQhsQeGsErgM=; b=OrvivbGOQLXyy+veDhtLA9Vs/s7VJve2xEm4ziUwicHATKKMFonpPc3w0FCr+TeKbh QaRoGx0KzAgeaKHGxeqLdXqak2fk0eZnuVfoLRJT+pWCxH8DtV/aVQaiNz/+ujt9w+vN 8Wykt9yEahoVaMzr9Q2YZridZr1c+dZLNBwGpBZaUSr1DjfLT5mb+dhgsuMrf8gd+JqZ A6Gn1J2xJY50B5BVgRgPQzmdDcA7EHK4awXwM+sc/ADIijST6SAPuvchLgrSChmawXzn yyqzGQafeGLcyad2IGq4KWAJeKxUMlpLJNKnuysOfxUdQ+MQNoxiKgLix9lerBzBcOUv osMg== X-Forwarded-Encrypted: i=1; AFNElJ9L15v6PqZPdwt9wfYBctZNXQ+/F/XNh1VwARid2NSaccyJ+YRZBatuM6+AAMgyYtB9qSLtxOdNFVI=@lists.freedesktop.org X-Gm-Message-State: AOJu0Yx5bPk3YftHuoC0Xj8IGbaVfYL3IXTQ5JoBH2rtVdSq6sFU1pv+ KifTGKFS21BKTGF11Q17GH1JxkZ2Qzz6zz1oDbonmCOS1OWT7zC/7kDTdDnx3hgMCsZ0pTycEey //myyswZ7UbSa7pQ575K1dynfdflBDRqxqi2vlzWp4WBmIvz4AgDylX39GZNlNODGMn9hDJzf41 9eOky5YTlkWi0NtAq7JyYTqD4WB3b/TkrZb1viBMwnlpDH X-Gm-Gg: Acq92OE/gOBmN7cPmfbA9f7omTkFvrY8G1nN9BMbc2oou234zFjWWfXtldKGH9j3/Lg xAKQN5L10DcH4QyLCi42QznZRCEkJwykOBCV4CPhwPGwrEhJe0I4QD9cyEFfuHf16Pbk0oEp7FF eJlWOai7HMfV4rd2xGF7mJaEBTim2teOlpG0kmvZreZoR7qxejVqK5Rzz7gbH2W2MOBFBvvAmnA JvCqw== X-Received: by 2002:a05:690c:348a:b0:7bf:ff7:ea71 with SMTP id 00721157ae682-7c69ae49726mr25053337b3.17.1778676106259; Wed, 13 May 2026 05:41:46 -0700 (PDT) X-Received: by 2002:a05:690c:348a:b0:7bf:ff7:ea71 with SMTP id 00721157ae682-7c69ae49726mr25053037b3.17.1778676105682; Wed, 13 May 2026 05:41:45 -0700 (PDT) MIME-Version: 1.0 References: <20260512-v2_20230123_tjmercier_google_com-v1-0-6326701c3691@redhat.com> <20260512-v2_20230123_tjmercier_google_com-v1-2-6326701c3691@redhat.com> <8ef38815-6ae9-4359-86d4-042554357639@amd.com> In-Reply-To: <8ef38815-6ae9-4359-86d4-042554357639@amd.com> From: Albert Esteve Date: Wed, 13 May 2026 14:41:33 +0200 X-Gm-Features: AVHnY4I5gWDDEj4SX_HuB44fjlMWKZYdMLtmbKqRDSeay77iEzEWghV7sNPOxmY Message-ID: Subject: Re: [PATCH RFC 2/5] dma-heap: charge dma-buf memory via explicit memcg To: =?UTF-8?Q?Christian_K=C3=B6nig?= Cc: Tejun Heo , Johannes Weiner , =?UTF-8?Q?Michal_Koutn=C3=BD?= , Jonathan Corbet , Shuah Khan , Sumit Semwal , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , Benjamin Gaignard , Brian Starkey , John Stultz , "T.J. Mercier" , Christian Brauner , Paul Moore , James Morris , "Serge E. Hallyn" , Stephen Smalley , Ondrej Mosnacek , Shuah Khan , cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, linux-kselftest@vger.kernel.org, mripard@kernel.org, echanude@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 4fWyRqHtIimpv4fanTqMeO-WC9qqTQ5zV4vHjuSAgb0_1778676106 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" On Tue, May 12, 2026 at 12:14=E2=80=AFPM Christian K=C3=B6nig wrote: > > On 5/12/26 11:10, Albert Esteve wrote: > > On embedded platforms a central process often allocates dma-buf > > memory on behalf of client applications. Without a way to > > attribute the charge to the requesting client's cgroup, the > > cost lands on the allocator, making per-cgroup memory limits > > ineffective for the actual consumers. > > > > Add charge_pid_fd to struct dma_heap_allocation_data. When set to > > a valid pidfd, DMA_HEAP_IOCTL_ALLOC resolves the target task's > > memcg and charges the buffer there via mem_cgroup_charge_dmabuf() > > inside dma_heap_buffer_alloc(). Without charge_pid_fd, and with > > the mem_accounting module parameter enabled, the buffer is charged > > to the allocator's own cgroup. > > > > Additionally, commit 3c227be90659 ("dma-buf: system_heap: account for > > system heap allocation in memcg") adds __GFP_ACCOUNT to system-heap > > page allocations. Keeping __GFP_ACCOUNT would charge the same pages > > twice (once to kmem, once to MEMCG_DMABUF), thus remove it and route > > all accounting through a single MEMCG_DMABUF path. > > > > Usage examples: > > > > 1. Central allocator charging to a client at allocation time. > > The allocator knows the client's PID (e.g., from binder's > > sender_pid) and uses pidfd to attribute the charge: > > > > pid_t client_pid =3D txn->sender_pid; > > int pidfd =3D pidfd_open(client_pid, 0); > > > > struct dma_heap_allocation_data alloc =3D { > > .len =3D buffer_size, > > .fd_flags =3D O_RDWR | O_CLOEXEC, > > .charge_pid_fd =3D pidfd, > > }; > > ioctl(heap_fd, DMA_HEAP_IOCTL_ALLOC, &alloc); > > close(pidfd); > > /* alloc.fd is now charged to client's cgroup */ > > > > 2. Default allocation (no pidfd, mem_accounting=3D1). > > When charge_pid_fd is not set and the mem_accounting module > > parameter is enabled, the buffer is charged to the allocator's > > own cgroup: > > > > struct dma_heap_allocation_data alloc =3D { > > .len =3D buffer_size, > > .fd_flags =3D O_RDWR | O_CLOEXEC, > > }; > > ioctl(heap_fd, DMA_HEAP_IOCTL_ALLOC, &alloc); > > /* charged to current process's cgroup */ > > > > Current limitations: > > > > - Single-owner model: a dma-buf carries one memcg charge regardless of > > how many processes share it. Means only the first owner (and exporte= r) > > of the shared buffer bears the charge. > > - Only memcg accounting supported. While this makes sense for system > > heap buffers, other heaps (e.g., CMA heaps) will require selectively > > charging also for the dmem controller. > > Well that doesn't looks soo bad, it at least seems to tackle the problem = at hand for Android and some of other embedded use cases. > > I'm just not sure if this is future prove and will work for all use cases= , e.g. cloud gaming, native context for automotive etc... > > Essentially the problem boils down to two limitations: > 1) a piece of memory can only be charged to one cgroup, the framework doe= sn't has a concept of charging shared memory to multiple groups > 2) when memory references in the form of file descriptors are passed betw= een applications we have no way of changing the accounting to a different c= group > > The passing of the memory reference already has a well defined uAPI and i= f we could solve those two limitations we not only solve the problem withou= t introducing new uAPI (with potential new security risks) but also solve i= t for all other use cases which uses file descriptors as well as. E.g. memf= d, accel and GPU drivers etc... Honestly, adding a hook to fd-passing uAPI to manage charge transfers sounds like a promising solution requiring no uAPI changes. However, it still does not cover all paths, e.g., dup() or fork(). And shared memory sounds like a hard one to tackle, where deciding the best policy is more a per-usecase thing and would probably require userspace configuration. All in all, charge_pid_fd covers a well-defined and immediately practical subset. The UAPI cost is small and the mechanism is explicit about what it does and doesn't solve. A general solution, if it ever converges, would likely supersede charge_pid_fd for most cases, which is a fine outcome if it solves the problem more completely. Either way, if you have a specific approach in mind for solving any of the above limitations, I'd be happy to look into it further. BR, Albert. > > On the other hand it is really nice to finally see this tackled for at le= ast DMA-buf heaps. On the GPU side I have seen just another try of a driver= doing some kind of special driver specific accounting to solve this just a= few weeks ago. And to be honest such single driver island approach have th= e tendency to break more often that they are working correctly. > > Regards, > Christian. > > > > > Signed-off-by: Albert Esteve > > --- > > Documentation/admin-guide/cgroup-v2.rst | 5 ++-- > > drivers/dma-buf/dma-buf.c | 16 ++++--------- > > drivers/dma-buf/dma-heap.c | 42 +++++++++++++++++++++++++= +++++--- > > drivers/dma-buf/heaps/system_heap.c | 2 -- > > include/uapi/linux/dma-heap.h | 6 +++++ > > 5 files changed, 53 insertions(+), 18 deletions(-) > > > > diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/ad= min-guide/cgroup-v2.rst > > index 8bdbc2e866430..824d269531eb1 100644 > > --- a/Documentation/admin-guide/cgroup-v2.rst > > +++ b/Documentation/admin-guide/cgroup-v2.rst > > @@ -1636,8 +1636,9 @@ The following nested keys are defined. > > structures. > > > > dmabuf (npn) > > - Amount of memory used for exported DMA buffers allocated = by the cgroup. > > - Stays with the allocating cgroup regardless of how the bu= ffer is shared. > > + Amount of memory used for exported DMA buffers allocated = by or on > > + behalf of the cgroup. Stays with the allocating cgroup re= gardless > > + of how the buffer is shared. > > > > workingset_refault_anon > > Number of refaults of previously evicted anonymous pages. > > diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c > > index ce02377f48908..23fb758b78297 100644 > > --- a/drivers/dma-buf/dma-buf.c > > +++ b/drivers/dma-buf/dma-buf.c > > @@ -181,8 +181,11 @@ static void dma_buf_release(struct dentry *dentry) > > */ > > BUG_ON(dmabuf->cb_in.active || dmabuf->cb_out.active); > > > > - mem_cgroup_uncharge_dmabuf(dmabuf->memcg, PAGE_ALIGN(dmabuf->size= ) / PAGE_SIZE); > > - mem_cgroup_put(dmabuf->memcg); > > + if (dmabuf->memcg) { > > + mem_cgroup_uncharge_dmabuf(dmabuf->memcg, > > + PAGE_ALIGN(dmabuf->size) / PAGE= _SIZE); > > + mem_cgroup_put(dmabuf->memcg); > > + } > > > > dmabuf->ops->release(dmabuf); > > > > @@ -764,13 +767,6 @@ struct dma_buf *dma_buf_export(const struct dma_bu= f_export_info *exp_info) > > dmabuf->resv =3D resv; > > } > > > > - dmabuf->memcg =3D get_mem_cgroup_from_mm(current->mm); > > - if (!mem_cgroup_charge_dmabuf(dmabuf->memcg, PAGE_ALIGN(dmabuf->s= ize) / PAGE_SIZE, > > - GFP_KERNEL)) { > > - ret =3D -ENOMEM; > > - goto err_memcg; > > - } > > - > > file->private_data =3D dmabuf; > > file->f_path.dentry->d_fsdata =3D dmabuf; > > dmabuf->file =3D file; > > @@ -781,8 +777,6 @@ struct dma_buf *dma_buf_export(const struct dma_buf= _export_info *exp_info) > > > > return dmabuf; > > > > -err_memcg: > > - mem_cgroup_put(dmabuf->memcg); > > err_file: > > fput(file); > > err_module: > > diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c > > index ac5f8685a6494..ff6e259afcdc0 100644 > > --- a/drivers/dma-buf/dma-heap.c > > +++ b/drivers/dma-buf/dma-heap.c > > @@ -7,13 +7,17 @@ > > */ > > > > #include > > +#include > > #include > > #include > > #include > > +#include > > +#include > > #include > > #include > > #include > > #include > > +#include > > #include > > #include > > #include > > @@ -55,10 +59,12 @@ MODULE_PARM_DESC(mem_accounting, > > "Enable cgroup-based memory accounting for dma-buf heap = allocations (default=3Dfalse)."); > > > > static int dma_heap_buffer_alloc(struct dma_heap *heap, size_t len, > > - u32 fd_flags, > > - u64 heap_flags) > > + u32 fd_flags, u64 heap_flags, > > + struct mem_cgroup *charge_to) > > { > > struct dma_buf *dmabuf; > > + unsigned int nr_pages; > > + struct mem_cgroup *memcg =3D charge_to; > > int fd; > > > > /* > > @@ -73,6 +79,22 @@ static int dma_heap_buffer_alloc(struct dma_heap *he= ap, size_t len, > > if (IS_ERR(dmabuf)) > > return PTR_ERR(dmabuf); > > > > + nr_pages =3D len / PAGE_SIZE; > > + > > + if (memcg) > > + css_get(&memcg->css); > > + else if (mem_accounting) > > + memcg =3D get_mem_cgroup_from_mm(current->mm); > > + > > + if (memcg) { > > + if (!mem_cgroup_charge_dmabuf(memcg, nr_pages, GFP_KERNEL= )) { > > + mem_cgroup_put(memcg); > > + dma_buf_put(dmabuf); > > + return -ENOMEM; > > + } > > + dmabuf->memcg =3D memcg; > > + } > > + > > fd =3D dma_buf_fd(dmabuf, fd_flags); > > if (fd < 0) { > > dma_buf_put(dmabuf); > > @@ -102,6 +124,9 @@ static long dma_heap_ioctl_allocate(struct file *fi= le, void *data) > > { > > struct dma_heap_allocation_data *heap_allocation =3D data; > > struct dma_heap *heap =3D file->private_data; > > + struct mem_cgroup *memcg =3D NULL; > > + struct task_struct *task; > > + unsigned int pidfd_flags; > > int fd; > > > > if (heap_allocation->fd) > > @@ -113,9 +138,20 @@ static long dma_heap_ioctl_allocate(struct file *f= ile, void *data) > > if (heap_allocation->heap_flags & ~DMA_HEAP_VALID_HEAP_FLAGS) > > return -EINVAL; > > > > + if (heap_allocation->charge_pid_fd) { > > + task =3D pidfd_get_task(heap_allocation->charge_pid_fd, &= pidfd_flags); > > + if (IS_ERR(task)) > > + return PTR_ERR(task); > > + > > + memcg =3D get_mem_cgroup_from_mm(task->mm); > > + put_task_struct(task); > > + } > > + > > fd =3D dma_heap_buffer_alloc(heap, heap_allocation->len, > > heap_allocation->fd_flags, > > - heap_allocation->heap_flags); > > + heap_allocation->heap_flags, > > + memcg); > > + mem_cgroup_put(memcg); > > if (fd < 0) > > return fd; > > > > diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heap= s/system_heap.c > > index 03c2b87cb1112..95d7688167b93 100644 > > --- a/drivers/dma-buf/heaps/system_heap.c > > +++ b/drivers/dma-buf/heaps/system_heap.c > > @@ -385,8 +385,6 @@ static struct page *alloc_largest_available(unsigne= d long size, > > if (max_order < orders[i]) > > continue; > > flags =3D order_flags[i]; > > - if (mem_accounting) > > - flags |=3D __GFP_ACCOUNT; > > page =3D alloc_pages(flags, orders[i]); > > if (!page) > > continue; > > diff --git a/include/uapi/linux/dma-heap.h b/include/uapi/linux/dma-hea= p.h > > index a4cf716a49fa6..e02b0f8cbc6a1 100644 > > --- a/include/uapi/linux/dma-heap.h > > +++ b/include/uapi/linux/dma-heap.h > > @@ -29,6 +29,10 @@ > > * handle to the allocated dma-buf > > * @fd_flags: file descriptor flags used when allocatin= g > > * @heap_flags: flags passed to heap > > + * @charge_pid_fd: optional pidfd of the process whose cgroup should= be > > + * charged for this allocation; 0 means charge the c= alling > > + * process's cgroup > > + * @__padding: reserved, must be zero > > * > > * Provided by userspace as an argument to the ioctl > > */ > > @@ -37,6 +41,8 @@ struct dma_heap_allocation_data { > > __u32 fd; > > __u32 fd_flags; > > __u64 heap_flags; > > + __u32 charge_pid_fd; > > + __u32 __padding; > > }; > > > > #define DMA_HEAP_IOC_MAGIC 'H' > > >