From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.219.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794956091; b=GxKY0L2bVyjIf/swIm+uqflVJcFxJFiGU2kuBcGQ0VWVptefZwrNaFxHMB77f9ZMfsmb KdjkjV4uesNyReuoPXwze1LMOY8Hu/Sae+zBdEzOX1SQ98if2W53sjZMO+6wYsTugai00 L0VAht2z8aE6xRU9I3Q2njKSB22EAoRJ/g0lIcArWGhF3FrQQ5mWGNrheceqGgnk3ggwZ fKbwvToUiO+gDN6dgXiO9/MIbTlULOXA1OOlY7RiSeepbvse5hrraLIinNpv+9+7tYiwv MFShmaDvnCmQ+sdFr01jekAxABcVZ9n9VB0L2vhQDzSJIgVSueuGQ/NjXI2pcDn2rgA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794956091; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding: X-Mailer:From:To:Cc:Subject:References:In-Reply-To; bh=k7/QLUvYDzH8xZxy3OOm+8SYpFBmpzd+eC/ISXSLxiA=; b=VFGS2wU1zxEWNg3p5tjwgyV6TL0BVtnimwW6745ZH0J5eFIM4EphatVPigxEu04lVij2 N/6uLDjM5N/93QPyp5qdU8JVMpnJHwFx80PQstyL1FskuZNN9JX9F9D/AMTAXRAbTISl1 Ws2cr9S/SWFu2N2X78FpjmAepAOsvcM0NnI3VnyIjtMg89FK3z0ZXJEIyqOFAfFxp34zQ lnlbKXSGYfEcPHgqSyqgrP32RSaYWVl5ERQPxkoDAaV2fthTPLsW1pkyLvZ8ocmD1GgK/ 3ZT10lXtW4stX/KspZxC3SmpVGym9r1Ddg5sFQIqU09x2YwtJX5qBFAn9M2ehX/bjvA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=paul-moore.com; spf=pass smtp.mailfrom=paul-moore.com; dkim=pass header.d=paul-moore.com header.i=@paul-moore.com header.a=rsa-sha256 header.s=google header.b=DIOqvTtw; arc=none smtp.remote-ip=209.85.219.42 Received: from mail-qv1-f42.google.com (mail-qv1-f42.google.com [209.85.219.42]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2AE52606A332 for ; Tue, 17 Nov 2026 22:54:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2AE52606A332 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=paul-moore.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=paul-moore.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=paul-moore.com header.i=@paul-moore.com header.a=rsa-sha256 header.s=google header.b=DIOqvTtw Received: by mail-qv1-f42.google.com with SMTP id 6a1803df08f44-91f11161831so1737566d6.1 for ; Tue, 17 Nov 2026 14:54:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paul-moore.com; s=google; t=1794956089; x=1795560889; darn=vger.kernel.org; h=in-reply-to:references:subject:cc:to:from:content-transfer-encoding :content-type:mime-version:message-id:date:from:to:cc:subject:date :message-id:reply-to:content-type; bh=k7/QLUvYDzH8xZxy3OOm+8SYpFBmpzd+eC/ISXSLxiA=; b=DIOqvTtwgZ5ItvYEoe2KZqcsY3ZGLQ+HaihmoLRuvlSTEm54jSBGFp7446G3/I8rQV jdaaePJOPptbJB/iycrO1+C9CK7f1Lzi0/ZtjLgb3g74xLYezSMe33/lJcnXDXHCH2QR nPspoy5WRs3Kr/RdCST5lRUxeJijzn91kv7ip8tpuZvGiqXVwaGEyqsdCZ4UzGopcOig odhAaVLPfkteT28BObGagg1mfMyZi1OKOhsJXcZRCsolfwMYJPBhlTEF26Wy/fGeH2g/ 6GB9fJ+t+azZKqhhqQ/sH3UiAz8tKQMSv7Y+PqcaopnbJ1qBu03DBRydnqkMiL70S4pM 2u+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794956089; x=1795560889; h=in-reply-to:references:subject:cc:to:from:content-transfer-encoding :content-type:mime-version:message-id:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=k7/QLUvYDzH8xZxy3OOm+8SYpFBmpzd+eC/ISXSLxiA=; b=CfDP/oOCqtJ6XrJ43VMdEUGcGd0iy1jurTuOn1XLaPUfwlluR04vkZwrzi+OtEMmRI L+ARZfxKuQWTakfW6wUEf5oULUJdbfsxdrJQE3++quTd8BOyBwKRMLw4IMqNDeAh4wON Vjx0QlZp/2ULVaKVKF33nwwZd99DJ+SRQitnfSegdQpMEEzQrTTgsFXEdGMzMpu/NfDH +YZJslC3DiKUzf8ge0hXMG3KswZm3zkykJrt2UwFNwRL1v2r5IzpDtlOubWQ3ktiGl27 Jc0hLGDemqbWVQ9hcAl3ah9ePODHcQNtlCnQIXgfuzEv5GkQ3RAMZzuaEBK5+oFwC2z1 WqbQ== X-Forwarded-Encrypted: i=1; AL+hErHwNtf35KDREh19y5rPukzHpcO2AuZFxs/IBppuWY4lY4H3ca0zzN//kdu+bwJ+mAaK5I06lSZ2f5goN/afg7/yPLm682M=@vger.kernel.org X-Gm-Message-State: APuU4Z/dLI4OrFqahiY72JIxoLtn9UywiF2NAuG3uSFaTBHix/KwsdOt QGhwWDMujwHjdqECmRVc+fxII/25hkBaSujY9EnouzNgyZq96bJsHNIiFJ4xBiJkhQ== X-Gm-Gg: AZFdTZeRYoO73qijJHOfuHP3c/8C8VfRZtMvQxllSnijqoyLjU/WF3d879w/grLcwe8 zao2qc6Q6sZnwrEcu0ghXAUr9mmvotISDqkviKAVoJbnKnQXTMf6EteM6fNmOkxeCbkMzPFXGh3 +t5A39MYe0lBcpG/6RwCYvc1CPj0FScMXPT5STlpRx0XxiylpDzRFjmbQ2eb7W4Do2+9QPRFRXN fKQW6uBuznNDyLYmgiEzBHpTpbztZr1MR9dHaVFH+w6JJEGlcGctTAExfAzbk6hfyMXb+5rsBOS eRuX8EU7s4zh6gG3em7VafxvNV7IFj/1jyY395FIFLhOi4wXrulhbdDMTzaoPJ6kAxYY7+obN4f PXxH2yi//aB8ZAFbL8w4WGjlgunMaFYFteLCwz0+qa3dJPX++DUxnUOotR/cjrt9dJp2dxbgP+D nKTejAbXd3D2SLcKeAIAt/GDUs9E64BlCuZCUo980+/KP189ayLJc9KWzH0bpxWDAy0jQ5bmlNV W9Zgrb4Jppcc+Fuco7ruwiayGubyPBEd/xtKOiU2K+8Gvoy3cmWp5kb6dUkJTPd8oCE49p6GkH7 pqPFvCvmLRyvOTbwP16NQPxJ2RGLDBffeLt59hEJGPmd7DGb6fKk4y+rpQ0nge7DQRwVQgbkAZS 5QKRUl00cBWC/N7qY5Ac4NZH4QXtTJA== X-Received: by 2002:a05:6214:4981:b0:921:9ed:ee6 with SMTP id 6a1803df08f44-92114dd0d06mr10225486d6.20.1794956088668; Tue, 17 Nov 2026 14:54:48 -0800 (PST) Received: from localhost (pool-71-126-255-178.bstnma.fios.verizon.net. [71.126.255.178]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-92114d53b7fsm5495766d6.26.2026.11.17.14.54.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Nov 2026 14:54:48 -0800 (PST) Date: Tue, 17 Nov 2026 17:54:47 -0500 Message-ID: <4356992ac4fb7876fb5759189ba4b492@paul-moore.com> X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Mailer: pstg-pwork:20261117_1552/pstg-lib:20261117_1552/pstg-pwork:20261117_1552 From: Paul Moore To: Albert Esteve , Tejun Heo , Johannes Weiner , =?UTF-8?Q?Michal_Koutn=C3=BD?= , Jonathan Corbet , Shuah Khan , Sumit Semwal , =?UTF-8?Q?Christian_K=C3=B6nig?= , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , Benjamin Gaignard , Brian Starkey , John Stultz , "T.J. Mercier" , Christian Brauner , James Morris , "Serge E. Hallyn" , Stephen Smalley , Shuah Khan , Randy Dunlap , =?UTF-8?B?T25kcmVqIE1vc27DocSNZWs=?= , Jens Wiklander , Sumit Garg , Philipp Stanner Cc: airlied@gmail.com, 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, op-tee@lists.trustedfirmware.org, Albert Esteve , echanude@redhat.com, mripard@kernel.org Subject: Re: [PATCH v5 0/8] memcg: dma-buf cgroup accounting and charge transfer References: <20261117-v2_20230123_tjmercier_google_com-v5-0-1ce975f2a9ad@redhat.com> In-Reply-To: <20261117-v2_20230123_tjmercier_google_com-v5-0-1ce975f2a9ad@redhat.com> On Nov 17, 2026 Albert Esteve wrote: > > Shared-memory allocator services (e.g., Android's gralloc) > allocate dma-bufs on behalf of client processes in different > cgroups. Today those allocations are charged to the allocator's > own cgroup, leaving client cgroups with no visibility into their > actual memory consumption and defeating resource limits. > > This series adds memory cgroup accounting for dma-buf allocations, > building on T.J. Mercier's earlier series [1] and incorporating > feedback from previous versions and LPC discussions [2]. > > Patch 1 introduces the MEMCG_DMABUF stat and the > obj_cgroup_reserve_dmabuf()/obj_cgroup_cancel_dmabuf() and > mem_cgroup_commit_dmabuf()/mem_cgroup_uncharge_dmabuf() helpers > in mm/memcontrol.c > > Patch 2 wires up the charging policy: dma_buf_export() stores > exp_info.objcg and updates the stat, a new charge_fd field in > DMA_HEAP_IOCTL_ALLOC accepts a pidfd or cgroupfd to identify > the target cgroup, and __GFP_ACCOUNT is removed from system-heap > allocations to avoid double counting. > > Patch 3 adds DMA_BUF_IOCTL_XFER_CHARGE, a general ioctl on any > dma-buf fd to transfer charges post-allocation. This covers the > double-hop allocator case (SurfaceFlinger -> gralloc -> kernel) > and works for buffers from any exporter that opts in. Charging > is gated by dmabuf->objcg being set at export time, ensuring that > transfer is only applied for buffers backed by system memory where > size reflects actual bytes. If the receiving cgroup lacks capacity > for the charge, the ioctl returns -ENOMEM and the original charge > is preserved. > > Patches 4-7 add access control for cross-cgroup charging. Letting > userspace decide who to charge needs to be a privileged operation > because it has material impact on OOM decision and resource limits. > Charging outside the caller's own memcg subtree requires > CAP_SYS_RESOURCE. On top of that, LSM hooks gate the two charge_fd > flavours: security_memcg_charge_task() gates the pidfd path (target > identified by task credentials) and security_memcg_charge_cgroup() > gates the cgroupfd path (target identified by cgroup directory label), > with SELinux implementations using process2:charge_to and > cgroup:charge_to permissions. I'm probably misunderstanding the description here, but in both cases (the pidfd or cgroupfd case), ultimately the cgroup is charged, yes? In the cgroupfd case it is more direct, but in the pidfd case it is still the cgroup associated with the process identified by pidfd, yes? If that's the case - and it very well may not be (!) - it probably makes more sense to combine the two SELinux access controls into one. Yes, the LSM hooks and callbacks would likely need to be different due to the mechanics of it, but the permission check "cgroup/charge" could be the same. > Patch 8 adds selftests. The tests and vmtest script were mostly > written by an LLM, closely following the style of the surrounding > tests and the virtme-ng setup. All code has been reviewed and tested. I think we need to have selinux-testsuite tests to go along with this, especially as this would be the first patchset to add cgroup controls. Historically I've pushed back on piecemeal cgroup additions, pushing for some basic design of how to manage access controls for resource management first, but since evidently no one wants to do that, we really do need tests. https://github.com/SELinuxProject/selinux-testsuite > Known limitations: > > - Single-owner model: a dma-buf carries one memcg charge regardless > of how many processes share it. Once again, I'm sure I'm misunderstanding things, but are you saying that multiple cgroups can end up sharing the same dma-buf despite only one of the cgroups taking the accounting hit? Hopefully the charged cgroup is deterministic? > - Only memcg accounting. CMA heaps would additionally need dmem > controller integration. Do you have a rough idea of how that would work? I ask only because I'm interested in seeing if that would require an additional LSM hook or if there is some way we could leverage the same hook(s) used here. > - If a transfer of an uncharged buffer fails, the buffer remains > charged to the original cgroup. The allocator (userspace) is > expected to handle this (retry, free, ...). > - The LSM hooks are intentionally narrow. Broader cgroup access controls > (e.g. for the memfd/kexec handover case [2]) could be a separate effort > reusing the same pattern. > - Only system heap sets exp_info.objcg today. Other exporters > (DRM, V4L2, etc.) can opt in by setting it, but are not > covered by this series. > - Related to the above: lazy backing-store allocation is not supported. > This can be extended later, e.g., by adding a flag on the dma-buf > struct for explicitly marking when memory is committed. Similar to the CMA case, any rough ideas on what this would look like? > Note on TTM/GPU accounting [3]: that effort charges system memory > allocated for GPU buffers per page, with its own stats (gpu_active > and gpu_reclaim). This series charges at the dma-buf level, for > memory with no memory.stat category of its own; exporters whose > memory is already charged by their allocator must not opt in, so > the two do not overlap. > > [1] https://lore.kernel.org/cgroups/20230109213809.418135-1-tjmercier@google.com/ > [2] https://lpc.events/event/20/contributions/2416/ > [3] https://lore.kernel.org/all/20260706052330.1110909-1-airlied@gmail.com/ > > Signed-off-by: Albert Esteve -- paul-moore.com