From: "Harry Yoo (Meta)" <harry@kernel.org>
To: Vlastimil Babka <vbabka@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
Suren Baghdasaryan <surenb@google.com>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Hocko <mhocko@kernel.org>,
Shakeel Butt <shakeel.butt@linux.dev>,
Roman Gushchin <roman.gushchin@linux.dev>
Cc: "Hao Li" <hao.li@linux.dev>, "Christoph Lameter" <cl@gentwo.org>,
"David Rientjes" <rientjes@google.com>,
"Muchun Song" <muchun.song@linux.dev>,
"Hao Ge" <hao.ge@linux.dev>, "Pedro Falcato" <pfalcato@suse.de>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
kernel-team@meta.com, "Harry Yoo (Meta)" <harry@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Andrew Ballance" <andrewjballance@gmail.com>,
maple-tree@lists.infradead.org,
"Matthew Wilcox" <willy@infradead.org>,
linux-fsdevel@vger.kernel.org,
"Alexander Viro" <viro@zeniv.linux.org.uk>,
"Christian Brauner" <brauner@kernel.org>,
"Jan Kara" <jack@suse.cz>,
"David S. Miller" <davem@davemloft.net>,
"Eric Dumazet" <edumazet@google.com>,
"Jakub Kicinski" <kuba@kernel.org>,
"Paolo Abeni" <pabeni@redhat.com>,
"Simon Horman" <horms@kernel.org>,
"Jason Xing" <kerneljasonxing@gmail.com>,
"Willem de Bruijn" <willemb@google.com>,
"Mina Almasry" <almasrymina@google.com>,
"Björn Töpel" <bjorn@kernel.org>,
"Sebastian Andrzej Siewior" <bigeasy@linutronix.de>,
"Jiayuan Chen" <jiayuan.chen@linux.dev>,
netdev@vger.kernel.org, "Kees Cook" <kees@kernel.org>,
"David Hildenbrand" <david@kernel.org>,
"Lorenzo Stoakes" <ljs@kernel.org>,
"Mike Rapoport" <rppt@kernel.org>,
"Michal Hocko" <mhocko@suse.com>,
"Ingo Molnar" <mingo@redhat.com>,
"Peter Zijlstra" <peterz@infradead.org>,
"Juri Lelli" <juri.lelli@redhat.com>,
"Vincent Guittot" <vincent.guittot@linaro.org>,
"Dietmar Eggemann" <dietmar.eggemann@arm.com>,
"Steven Rostedt" <rostedt@goodmis.org>,
"Ben Segall" <bsegall@google.com>, "Mel Gorman" <mgorman@suse.de>,
"Valentin Schneider" <vschneid@redhat.com>,
"K Prateek Nayak" <kprateek.nayak@amd.com>,
"Catalin Marinas" <catalin.marinas@arm.com>,
"Will Deacon" <will@kernel.org>,
"Mark Rutland" <mark.rutland@arm.com>,
"Anshuman Khandual" <anshuman.khandual@arm.com>,
linux-arm-kernel@lists.infradead.org,
"Madhavan Srinivasan" <maddy@linux.ibm.com>,
"Michael Ellerman" <mpe@ellerman.id.au>,
"Nicholas Piggin" <npiggin@gmail.com>,
"Christophe Leroy (CS GROUP)" <chleroy@kernel.org>,
linuxppc-dev@lists.ozlabs.org,
"Robin Murphy" <robin.murphy@arm.com>,
"Joerg Roedel (AMD)" <joro@8bytes.org>,
"Niklas Schnelle" <schnelle@linux.ibm.com>,
"Matthew Rosato" <mjrosato@linux.ibm.com>,
"Gerald Schaefer" <gerald.schaefer@linux.ibm.com>,
"Chen-Yu Tsai" <wens@kernel.org>,
"Jernej Skrabec" <jernej.skrabec@gmail.com>,
"Samuel Holland" <samuel@sholland.org>,
iommu@lists.linux.dev, linux-s390@vger.kernel.org,
linux-sunxi@lists.linux.dev,
"Amir Goldstein" <amir73il@gmail.com>,
"Casey Schaufler" <casey@schaufler-ca.com>,
"Paul Moore" <paul@paul-moore.com>,
"James Morris" <jmorris@namei.org>,
"Serge E. Hallyn" <serge@hallyn.com>,
linux-security-module@vger.kernel.org,
"Richard Weinberger" <richard@nod.at>,
"Anton Ivanov" <anton.ivanov@cambridgegreys.com>,
"Johannes Berg" <johannes@sipsolutions.net>,
linux-um@lists.infradead.org, cgroups@vger.kernel.org
Subject: [PATCH slab/for-next 00/14] mm/slab: mandate SLAB_MAY_ACCOUNT for __GFP_ACCOUNT allocations to avoid wasting memory for slab metadata
Date: Wed, 21 Oct 2026 12:46:41 +0100 [thread overview]
Message-ID: <20261021-memalloc-prof-dont-waste-slabobj-ext-metadata-v1-0-096800aa3330@kernel.org> (raw)
The purpose of this series is to convert existing slab users that
specify __GFP_ACCOUNT on allocations to specify SLAB_MAY_ACCOUNT on
cache creation, to reduce memory waste for slab metadata with
CONFIG_MEMCG=y && CONFIG_MEM_ALLOC_PROFILING=y.
Currently, there are two ways to charge slab objects to memory
cgroups: 1) specify SLAB_ACCOUNT during cache creation, or 2) specify
__GFP_ACCOUNT on allocation. The latter method made it impossible to
assume that certain slab caches are never charged to memory cgroups.
Since commit d4404b0f5b8b ("mm/slab: stop allocating objcg pointers when
unnecessary"), slabobj_ext allocates the space for the objcg pointer
only for caches with SLAB_MAY_ACCOUNT. However, kmem_cache_create() sets
the flag on every cache, because any cache may be used with
__GFP_ACCOUNT.
As a result, sizeof(struct obj_cgroup *) bytes are wasted for each
unaccounted slab object when both memalloc profiling and memcg are
enabled. This is suboptimal when memalloc profiling is used to
profile memory allocations on production.
So, let's make memcg charging opt-in and stop wasting memory!
New rule for slab users: a cache must specify SLAB_MAY_ACCOUNT
(explicitly or implied by SLAB_ACCOUNT) for its objects to be charged
to a memcg. Otherwise __GFP_ACCOUNT is ignored and triggers a warning on
debug kernels (CONFIG_DEBUG_VM=y).
The approach is quite straightforward, but converting all caches that
may pass __GFP_ACCOUNT without SLAB_ACCOUNT at once is not.
As a best-effort approach, I swept the whole tree (with help of an
LLM) for paths that pass __GFP_ACCOUNT into slab allocations either
directly or indirectly.
While I tried to make sure the sweep is exhaustive, there can be a
missing conversion. However that is not a correctness problem:
the objects are just no longer charged to memcgs. Patch 13 adds
VM_WARN_ON_ONCE in __memcg_slab_post_alloc_hook() to catch such a
missing conversion.
Patch 1 allows passing SLAB_MAY_ACCOUNT to kmem_cache_create(),
patches 2-12 specify SLAB_MAY_ACCOUNT for the caches found by
the sweep, and patch 14 stops setting SLAB_MAY_ACCOUNT on all caches.
On an idle x86 VM with both memalloc profiling and memcg enabled,
slabobj_ext memory (the alloc_slab_obj_exts entry in /proc/allocinfo)
drops from 1,280KB to 910KB (-29%) after boot. The savings are expected
to be larger on real workloads, which allocate far more unaccounted slab
objects (8 bytes per unaccounted slab object).
Each tested cache was verified with the warning in patch 13:
I confirmed that 1) the warning is triggered without the conversion,
and 2) specifying SLAB_MAY_ACCOUNT resolves it. However I could not test
io-pgtable_armv7s_l2 and sun50i's pt_pool caches due to lack of the
hardware and emulation support. So they are compile-tested only.
cache tested environment
--------------------- ------ -----------------------
radix_tree_node yes x86
maple_node yes x86
buffer_head yes x86
skbuff_head_cache yes x86
fanotify_*_event yes x86
smack_rule yes x86
io-pgtable_armv7s_l2 no compile-tested only
thread_stack yes ppc64le (64K pages)
pgd_cache (arm64) yes arm64 (64K pages)
PGT_CACHE (powerpc) yes ppc64le
PCI_DMA_*_tables yes s390x (zPCI + vfio)
hostfs_inode_cache yes UML
sun50i pt_pool no compile-tested only
Signed-off-by: Harry Yoo (Meta) <harry@kernel.org>
---
Harry Yoo (Meta) (14):
mm/slab: allow passing SLAB_MAY_ACCOUNT to kmem_cache_create()
lib/maple_tree: specify SLAB_MAY_ACCOUNT for maple_node cache
lib/xarray: specify SLAB_MAY_ACCOUNT for radix_tree_node cache
fs/buffer: specify SLAB_MAY_ACCOUNT for buffer_head cache
net: skbuff: specify SLAB_MAY_ACCOUNT for skbuff_head_cache
kernel/fork: specify SLAB_MAY_ACCOUNT for thread_stack cache
arm64/mm: specify SLAB_MAY_ACCOUNT for pgd_cache
powerpc/mm: specify SLAB_MAY_ACCOUNT for pgtable caches
iommu: specify SLAB_MAY_ACCOUNT for slab-backed page table caches
fanotify: specify SLAB_MAY_ACCOUNT for event caches
smack: specify SLAB_MAY_ACCOUNT for smack_rule cache
hostfs: specify SLAB_MAY_ACCOUNT for inode cache
mm/memcontrol: do not allow charging objects without SLAB_MAY_ACCOUNT
mm/slab: do not set SLAB_MAY_ACCOUNT for all caches
arch/arm64/mm/pgd.c | 2 +-
arch/powerpc/mm/init-common.c | 3 ++-
drivers/iommu/io-pgtable-arm-v7s.c | 2 +-
drivers/iommu/s390-iommu.c | 4 ++--
drivers/iommu/sun50i-iommu.c | 3 ++-
fs/buffer.c | 2 +-
fs/hostfs/hostfs_kern.c | 2 +-
fs/notify/fanotify/fanotify_user.c | 10 ++++++----
include/linux/slab.h | 16 +++++++++++++---
kernel/fork.c | 3 ++-
lib/maple_tree.c | 2 +-
lib/radix-tree.c | 2 +-
mm/memcontrol.c | 6 ++++++
mm/slab.h | 2 +-
mm/slab_common.c | 8 +++-----
net/core/skbuff.c | 2 +-
security/smack/smack_lsm.c | 2 +-
17 files changed, 45 insertions(+), 26 deletions(-)
---
base-commit: d15d7703166daf7bd62c844a3cb1b3a6279adb4c
change-id: 20261014-memalloc-prof-dont-waste-slabobj-ext-metadata-34f943ef9658
Best regards,
--
Cheers,
Harry / Hyeonggon
next reply other threads:[~2026-10-21 11:47 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-21 11:46 Harry Yoo (Meta) [this message]
2026-10-21 11:46 ` [PATCH slab/for-next 01/14] mm/slab: allow passing SLAB_MAY_ACCOUNT to kmem_cache_create() Harry Yoo (Meta)
2026-10-21 11:46 ` [PATCH slab/for-next 02/14] lib/maple_tree: specify SLAB_MAY_ACCOUNT for maple_node cache Harry Yoo (Meta)
2026-10-21 11:46 ` [PATCH slab/for-next 03/14] lib/xarray: specify SLAB_MAY_ACCOUNT for radix_tree_node cache Harry Yoo (Meta)
2026-10-21 11:46 ` [PATCH slab/for-next 04/14] fs/buffer: specify SLAB_MAY_ACCOUNT for buffer_head cache Harry Yoo (Meta)
2026-10-21 11:46 ` [PATCH slab/for-next 05/14] net: skbuff: specify SLAB_MAY_ACCOUNT for skbuff_head_cache Harry Yoo (Meta)
2026-10-21 11:46 ` [PATCH slab/for-next 06/14] kernel/fork: specify SLAB_MAY_ACCOUNT for thread_stack cache Harry Yoo (Meta)
2026-10-21 11:46 ` [PATCH slab/for-next 07/14] arm64/mm: specify SLAB_MAY_ACCOUNT for pgd_cache Harry Yoo (Meta)
2026-10-21 11:46 ` [PATCH slab/for-next 08/14] powerpc/mm: specify SLAB_MAY_ACCOUNT for pgtable caches Harry Yoo (Meta)
2026-10-21 11:46 ` [PATCH slab/for-next 09/14] iommu: specify SLAB_MAY_ACCOUNT for slab-backed page table caches Harry Yoo (Meta)
2026-10-21 11:46 ` [PATCH slab/for-next 10/14] fanotify: specify SLAB_MAY_ACCOUNT for event caches Harry Yoo (Meta)
2026-10-21 11:46 ` [PATCH slab/for-next 11/14] smack: specify SLAB_MAY_ACCOUNT for smack_rule cache Harry Yoo (Meta)
2026-10-21 11:46 ` [PATCH slab/for-next 12/14] hostfs: specify SLAB_MAY_ACCOUNT for inode cache Harry Yoo (Meta)
2026-10-21 11:46 ` [PATCH slab/for-next 13/14] mm/memcontrol: do not allow charging objects without SLAB_MAY_ACCOUNT Harry Yoo (Meta)
2026-10-21 11:46 ` [PATCH slab/for-next 14/14] mm/slab: do not set SLAB_MAY_ACCOUNT for all caches Harry Yoo (Meta)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261021-memalloc-prof-dont-waste-slabobj-ext-metadata-v1-0-096800aa3330@kernel.org \
--to=harry@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=aliceryhl@google.com \
--cc=almasrymina@google.com \
--cc=amir73il@gmail.com \
--cc=andrewjballance@gmail.com \
--cc=anshuman.khandual@arm.com \
--cc=anton.ivanov@cambridgegreys.com \
--cc=bigeasy@linutronix.de \
--cc=bjorn@kernel.org \
--cc=brauner@kernel.org \
--cc=bsegall@google.com \
--cc=casey@schaufler-ca.com \
--cc=catalin.marinas@arm.com \
--cc=cgroups@vger.kernel.org \
--cc=chleroy@kernel.org \
--cc=cl@gentwo.org \
--cc=davem@davemloft.net \
--cc=david@kernel.org \
--cc=dietmar.eggemann@arm.com \
--cc=edumazet@google.com \
--cc=gerald.schaefer@linux.ibm.com \
--cc=hannes@cmpxchg.org \
--cc=hao.ge@linux.dev \
--cc=hao.li@linux.dev \
--cc=horms@kernel.org \
--cc=iommu@lists.linux.dev \
--cc=jack@suse.cz \
--cc=jernej.skrabec@gmail.com \
--cc=jiayuan.chen@linux.dev \
--cc=jmorris@namei.org \
--cc=johannes@sipsolutions.net \
--cc=joro@8bytes.org \
--cc=juri.lelli@redhat.com \
--cc=kees@kernel.org \
--cc=kernel-team@meta.com \
--cc=kerneljasonxing@gmail.com \
--cc=kprateek.nayak@amd.com \
--cc=kuba@kernel.org \
--cc=liam@infradead.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-s390@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=linux-sunxi@lists.linux.dev \
--cc=linux-um@lists.infradead.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=ljs@kernel.org \
--cc=maddy@linux.ibm.com \
--cc=maple-tree@lists.infradead.org \
--cc=mark.rutland@arm.com \
--cc=mgorman@suse.de \
--cc=mhocko@kernel.org \
--cc=mhocko@suse.com \
--cc=mingo@redhat.com \
--cc=mjrosato@linux.ibm.com \
--cc=mpe@ellerman.id.au \
--cc=muchun.song@linux.dev \
--cc=netdev@vger.kernel.org \
--cc=npiggin@gmail.com \
--cc=pabeni@redhat.com \
--cc=paul@paul-moore.com \
--cc=peterz@infradead.org \
--cc=pfalcato@suse.de \
--cc=richard@nod.at \
--cc=rientjes@google.com \
--cc=robin.murphy@arm.com \
--cc=roman.gushchin@linux.dev \
--cc=rostedt@goodmis.org \
--cc=rppt@kernel.org \
--cc=samuel@sholland.org \
--cc=schnelle@linux.ibm.com \
--cc=serge@hallyn.com \
--cc=shakeel.butt@linux.dev \
--cc=surenb@google.com \
--cc=vbabka@kernel.org \
--cc=vincent.guittot@linaro.org \
--cc=viro@zeniv.linux.org.uk \
--cc=vschneid@redhat.com \
--cc=wens@kernel.org \
--cc=will@kernel.org \
--cc=willemb@google.com \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®