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 55AC9CD4F21 for ; Wed, 13 May 2026 16:39:26 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id A05DD10EF4E; Wed, 13 May 2026 16:39:25 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="RHdnZKqL"; dkim-atps=neutral Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) by gabe.freedesktop.org (Postfix) with ESMTPS id BE48510EF4E for ; Wed, 13 May 2026 16:39:24 +0000 (UTC) Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-48d1c670255so4715e9.0 for ; Wed, 13 May 2026 09:39:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1778690363; cv=none; d=google.com; s=arc-20240605; b=cp8hdUSxrrFZgcmaWj3UWvsBHSmG+4fGZx2GXEQYyO1zMZPHxiMYkqs391psJyfS5l LZkE/787eytl5Fr8yMBt8SlmompZFCdjoODm8/p4ynhNQXPE1OnDdu2wu2gcJjhmdN5Y p33pyr7ytaZUWPN6oER85rOX8G7L/BliUq7aVbvljEfSKjSvfYyqHmtWt9h493Zjw6I+ AAb6+BmntGUPgvpgsEI6JXpp6AfrGzx5zSk0V2zqRmtR0f/rOJ2rbzQ4mKyfNw/sCBz7 nruh9GY8VygJW+8OYYcqxqQChkK+fFESUIkyF6cHvO91XOSlghygXYOP437JW6urUYO1 9m7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=hOVJmPkMMtutEBgPE85iBoY/7XAmkTfBx4YFHq1zHJQ=; fh=NIDlzpY41yIL4JKSB8H4IXoUc136BQtRll8m++tVv+w=; b=ERlevgwjCsIlvglK6sfsG51S+dSHBOCE5khtiFzYsQYjf3uMPTJNPxWW42rokG+KS4 QQ24nBHLgXBlWUeP2SxAK2ri11zq9ZW2DOclCw6FUBAKbxxBlwA3QTLlEoBBHUmvKnUx 8opD/V8jlQ1eBPG7ZPyE4ydM5EXsitdTMgVjKFSh9Ewdnik3Km8ED+Hr/dmi/nWc8nZS gED59Od8tBXAB6vecjD0gptk9M1/3vgcIdGu+ODTIpVsFnn26LelmoDU0rWt3g0Eo0om L5NwjXUhfC08FIGQZPxiAJDrpQapdCGG6NCNg3JqU/hZyTvzj/sPEvt2pSsDV/5v+1ob SShw==; darn=lists.freedesktop.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778690363; x=1779295163; darn=lists.freedesktop.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=hOVJmPkMMtutEBgPE85iBoY/7XAmkTfBx4YFHq1zHJQ=; b=RHdnZKqLwc7GD7mY0TQXWhJU2KT3AM/zbU0NfoZsMjI+bfB7Eu6vBtQxq3XBpqTABj /bBe33LR371tdx5VU7bqHZFzjt7JYrpDQ5ZWCZrCiKflmkn+af2u1xG6NOi9oHca6Hw3 V5d7BRYOMh5MIvXHav+evlCQ9kUoo7l1GRDK0ZJ20WTTn8EuahrG8QJoeXuZHvQD6h7C MNoZaL61Ks3IDFkSBm6kdClEm+co/noMQMxATlyxrpDj22wnpn6ux0HY8SalDXDRT4Hk xnrW8y+6e1MyUUu0MOOgikh+IzikQO8Vr/bbi4xOdTemk9NrMW+sm4iWPPJ33MtdMsV7 v/QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778690363; x=1779295163; 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=hOVJmPkMMtutEBgPE85iBoY/7XAmkTfBx4YFHq1zHJQ=; b=JvlOCytg96OyUipvQjPn9tevkgHNZV8DacP/6JHBoCZZRQJUCpMDAnpRIxIOxvUi8h G3Ada6o6yE2h0gxQ+K77GD+djf8DncvXnqfxOke5HbpuvJMreRpfGYqj7ce12Nq00Hcx ITS8na3xoY1FCFqwMavw6bAxSvOvI/+RopLGjZWFLueM/eo8XkDEx5klZgA+Px6B6bX8 7/hxEygN6t9S3NDJIivlwBrIPF5Ir3k302L/NLfkyvEtg2b4amhDW3xfATjOKbrUKFXs yNUEShMvbf6b+VCaMzTK2jKQ2HA5YuKtpQ5awVeHbm+omfKqb8vmy1OFa45o8BSD/dwR B09w== X-Forwarded-Encrypted: i=1; AFNElJ8ZqhXLY8rwaKIFkRMZjcA2+9kUIXsoTNhQWrszAlskK6kWWQjIiSKyqhFu2JV43nrc2RACEgXHTao=@lists.freedesktop.org X-Gm-Message-State: AOJu0YzqLytkm1EoT2oOGXutVyr92y8nY1aMa0tRMVROME5RiEtz2u2K blLHTsE1qoJlyCwDGFNOHKK+K1GeA0XzGY2wY7ER6Dbp4a3wK2MJMT6jcXi6L7R0LjIMgDYIBl8 BCpibS+fr1KFVgVffN3TndotB6V/2s/nbtXsNm1VA X-Gm-Gg: Acq92OEtPNqZd1XviJkH7daFt+oSgROwfbfriDzPl2NfLPYLkv2tM+lN12Mv7MCO69t TBIzREFnLwMXEbOK1INN8gEwzU+EJQHQh6D3GyKLktTGmunOsfDXKq7jzi1PY7h0okMXrwzI5pd 7Zh+flSD/DnVsDMWsO36WMS9TWx+9ITB/lVeC75ZD8WvVuoeMx87u6CyC65dPoTf9sPJBafj22z M8WKkjw5L0RsoC96649oBIUsx92cr0cO9sZ5pbUUJ8R5HlqJXVJMB2mU1CKDVS7NJFX6XZJj00F NIWYspQqOnEwmkPzU17TYDVagtuViuMm6trg5l2PeGa12Z7I2XIH8676dNsjIBEKNZpM+v7wX2D +z+Gn X-Received: by 2002:a05:600d:8486:20b0:477:86fd:fb49 with SMTP id 5b1f17b1804b1-48fc919a43dmr965705e9.10.1778690362738; Wed, 13 May 2026 09:39:22 -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: From: "T.J. Mercier" Date: Wed, 13 May 2026 09:39:10 -0700 X-Gm-Features: AVHnY4Le2GUGE4TjoBj6HdQ1GbUQChjLautyZmM4nJTx4rLM4Wl1vLSY-yCAZrQ Message-ID: Subject: Re: [PATCH RFC 2/5] dma-heap: charge dma-buf memory via explicit memcg To: Albert Esteve Cc: =?UTF-8?Q?Christian_K=C3=B6nig?= , 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 , 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 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 Wed, May 13, 2026 at 5:41=E2=80=AFAM Albert Esteve = wrote: > > 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 expor= ter) > > > 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 selective= ly > > > charging also for the dmem controller. > > > > Well that doesn't looks soo bad, it at least seems to tackle the proble= m 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 cas= es, 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 d= oesn't has a concept of charging shared memory to multiple groups > > 2) when memory references in the form of file descriptors are passed be= tween applications we have no way of changing the accounting to a different= cgroup > > > > The passing of the memory reference already has a well defined uAPI and= if we could solve those two limitations we not only solve the problem with= out introducing new uAPI (with potential new security risks) but also solve= it for all other use cases which uses file descriptors as well as. E.g. me= mfd, 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. I'm curious if anyone knows of a use case where FDs aren't involved at all? It's possible to fork() or clone() with only a dmabuf mapping and no FD. That sounds strange, and I'm not sure there's a real usecase for transferring ownership with that approach, but figured I'd at least pose the question. > 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 = least DMA-buf heaps. On the GPU side I have seen just another try of a driv= er 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 = the 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/= admin-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 allocate= d by the cgroup. > > > - Stays with the allocating cgroup regardless of how the = buffer is shared. > > > + Amount of memory used for exported DMA buffers allocate= d by or on > > > + behalf of the cgroup. Stays with the allocating cgroup = regardless > > > + of how the buffer is shared. > > > > > > workingset_refault_anon > > > Number of refaults of previously evicted anonymous page= s. > > > 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 *dentr= y) > > > */ > > > BUG_ON(dmabuf->cb_in.active || dmabuf->cb_out.active); > > > > > > - mem_cgroup_uncharge_dmabuf(dmabuf->memcg, PAGE_ALIGN(dmabuf->si= ze) / PAGE_SIZE); > > > - mem_cgroup_put(dmabuf->memcg); > > > + if (dmabuf->memcg) { > > > + mem_cgroup_uncharge_dmabuf(dmabuf->memcg, > > > + PAGE_ALIGN(dmabuf->size) / PA= GE_SIZE); > > > + mem_cgroup_put(dmabuf->memcg); > > > + } > > > > > > dmabuf->ops->release(dmabuf); > > > > > > @@ -764,13 +767,6 @@ struct dma_buf *dma_buf_export(const struct dma_= buf_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-= >size) / 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_b= uf_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 hea= p 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 *= heap, 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_KERN= EL)) { > > > + 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 *= file, 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 = *file, 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/he= aps/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(unsig= ned 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-h= eap.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 allocat= ing > > > * @heap_flags: flags passed to heap > > > + * @charge_pid_fd: optional pidfd of the process whose cgroup shou= ld be > > > + * charged for this allocation; 0 means charge the= calling > > > + * 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' > > > > > >