* [PATCH slab/for-next 00/14] mm/slab: mandate SLAB_MAY_ACCOUNT for __GFP_ACCOUNT allocations to avoid wasting memory for slab metadata
@ 2026-10-21 11:46 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)
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Harry Yoo (Meta) @ 2026-10-21 11:46 UTC (permalink / raw)
To: Vlastimil Babka, Andrew Morton, Suren Baghdasaryan,
Johannes Weiner, Michal Hocko, Shakeel Butt, Roman Gushchin
Cc: Hao Li, Christoph Lameter, David Rientjes, Muchun Song, Hao Ge,
Pedro Falcato, linux-mm, linux-kernel, kernel-team,
Harry Yoo (Meta), Liam R. Howlett, Alice Ryhl, Andrew Ballance,
maple-tree, Matthew Wilcox, linux-fsdevel, Alexander Viro,
Christian Brauner, Jan Kara, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Jason Xing,
Willem de Bruijn, Mina Almasry, Björn Töpel,
Sebastian Andrzej Siewior, Jiayuan Chen, netdev, Kees Cook,
David Hildenbrand, Lorenzo Stoakes, Mike Rapoport, Michal Hocko,
Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
Valentin Schneider, K Prateek Nayak, Catalin Marinas, Will Deacon,
Mark Rutland, Anshuman Khandual, linux-arm-kernel,
Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin,
Christophe Leroy (CS GROUP), linuxppc-dev, Robin Murphy,
Joerg Roedel (AMD), Niklas Schnelle, Matthew Rosato,
Gerald Schaefer, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland,
iommu, linux-s390, linux-sunxi, Amir Goldstein, Casey Schaufler,
Paul Moore, James Morris, Serge E. Hallyn, linux-security-module,
Richard Weinberger, Anton Ivanov, Johannes Berg, linux-um,
cgroups
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH slab/for-next 03/14] lib/xarray: specify SLAB_MAY_ACCOUNT for radix_tree_node cache
2026-10-21 11:46 [PATCH slab/for-next 00/14] mm/slab: mandate SLAB_MAY_ACCOUNT for __GFP_ACCOUNT allocations to avoid wasting memory for slab metadata Harry Yoo (Meta)
@ 2026-10-21 11:46 ` Harry Yoo (Meta)
2026-12-03 14:27 ` Hao Li
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 10/14] fanotify: specify SLAB_MAY_ACCOUNT for event caches Harry Yoo (Meta)
2 siblings, 1 reply; 6+ messages in thread
From: Harry Yoo (Meta) @ 2026-10-21 11:46 UTC (permalink / raw)
To: Vlastimil Babka, Andrew Morton, Suren Baghdasaryan,
Johannes Weiner, Michal Hocko, Shakeel Butt, Roman Gushchin
Cc: Hao Li, Christoph Lameter, David Rientjes, Muchun Song, Hao Ge,
Pedro Falcato, linux-mm, linux-kernel, kernel-team,
Harry Yoo (Meta), Matthew Wilcox, linux-fsdevel
XArrays with XA_FLAGS_ACCOUNT (e.g. the page cache) allocate nodes with
__GFP_ACCOUNT, and xarray/IDR callers can also pass GFP_KERNEL_ACCOUNT
directly. Specify SLAB_MAY_ACCOUNT when the object may be charged to a
memcg. __GFP_ACCOUNT without SLAB_MAY_ACCOUNT will not be allowed.
Path: __filemap_add_folio() (i_pages has XA_FLAGS_ACCOUNT)
-> xas_store() -> xas_alloc() (gfp |= __GFP_ACCOUNT)
-> kmem_cache_alloc_lru(radix_tree_node_cachep)
Assisted-by: LLM
Cc: Matthew Wilcox <willy@infradead.org>
Cc: linux-fsdevel@vger.kernel.org
Signed-off-by: Harry Yoo (Meta) <harry@kernel.org>
---
lib/radix-tree.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/radix-tree.c b/lib/radix-tree.c
index 976b9bd02a1b..2c7531deb0df 100644
--- a/lib/radix-tree.c
+++ b/lib/radix-tree.c
@@ -1600,7 +1600,7 @@ void __init radix_tree_init(void)
BUILD_BUG_ON(XA_CHUNK_SIZE > 255);
radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
sizeof(struct radix_tree_node), 0,
- SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
+ SLAB_PANIC | SLAB_RECLAIM_ACCOUNT | SLAB_MAY_ACCOUNT,
radix_tree_node_ctor);
ret = cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD, "lib/radix:dead",
NULL, radix_tree_cpu_dead);
--
2.54.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH slab/for-next 04/14] fs/buffer: specify SLAB_MAY_ACCOUNT for buffer_head cache
2026-10-21 11:46 [PATCH slab/for-next 00/14] mm/slab: mandate SLAB_MAY_ACCOUNT for __GFP_ACCOUNT allocations to avoid wasting memory for slab metadata 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 ` Harry Yoo (Meta)
2026-12-03 14:28 ` Hao Li
2026-10-21 11:46 ` [PATCH slab/for-next 10/14] fanotify: specify SLAB_MAY_ACCOUNT for event caches Harry Yoo (Meta)
2 siblings, 1 reply; 6+ messages in thread
From: Harry Yoo (Meta) @ 2026-10-21 11:46 UTC (permalink / raw)
To: Vlastimil Babka, Andrew Morton, Suren Baghdasaryan,
Johannes Weiner, Michal Hocko, Shakeel Butt, Roman Gushchin
Cc: Hao Li, Christoph Lameter, David Rientjes, Muchun Song, Hao Ge,
Pedro Falcato, linux-mm, linux-kernel, kernel-team,
Harry Yoo (Meta), Alexander Viro, Christian Brauner, Jan Kara,
linux-fsdevel
Buffer heads are allocated with __GFP_ACCOUNT to charge them to the
memcg of the folio they are attached to. Specify SLAB_MAY_ACCOUNT when
the object may be charged to a memcg. __GFP_ACCOUNT without
SLAB_MAY_ACCOUNT will not be allowed.
Path: ext4_getblk() -> folio_alloc_buffers(GFP_NOFS | __GFP_ACCOUNT)
-> alloc_buffer_head() -> kmem_cache_zalloc(bh_cachep)
Assisted-by: LLM
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Cc: linux-fsdevel@vger.kernel.org
Signed-off-by: Harry Yoo (Meta) <harry@kernel.org>
---
fs/buffer.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/buffer.c b/fs/buffer.c
index ed966fa73b1b..1cbbd065cab6 100644
--- a/fs/buffer.c
+++ b/fs/buffer.c
@@ -2993,7 +2993,7 @@ void __init buffer_init(void)
int ret;
bh_cachep = KMEM_CACHE(buffer_head,
- SLAB_RECLAIM_ACCOUNT|SLAB_PANIC);
+ SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MAY_ACCOUNT);
/*
* Limit the bh occupancy to 10% of ZONE_NORMAL
*/
--
2.54.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH slab/for-next 10/14] fanotify: specify SLAB_MAY_ACCOUNT for event caches
2026-10-21 11:46 [PATCH slab/for-next 00/14] mm/slab: mandate SLAB_MAY_ACCOUNT for __GFP_ACCOUNT allocations to avoid wasting memory for slab metadata 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 ` Harry Yoo (Meta)
2 siblings, 0 replies; 6+ messages in thread
From: Harry Yoo (Meta) @ 2026-10-21 11:46 UTC (permalink / raw)
To: Vlastimil Babka, Andrew Morton, Suren Baghdasaryan,
Johannes Weiner, Michal Hocko, Shakeel Butt, Roman Gushchin
Cc: Hao Li, Christoph Lameter, David Rientjes, Muchun Song, Hao Ge,
Pedro Falcato, linux-mm, linux-kernel, kernel-team,
Harry Yoo (Meta), Jan Kara, Amir Goldstein, linux-fsdevel
fanotify events are allocated with GFP_KERNEL_ACCOUNT and charged to
the memcg of the listener. Specify SLAB_MAY_ACCOUNT when the object
may be charged to a memcg. __GFP_ACCOUNT without SLAB_MAY_ACCOUNT
will not be allowed.
Path: fsnotify() -> fanotify_handle_event()
-> fanotify_alloc_event(GFP_KERNEL_ACCOUNT)
-> kmem_cache_alloc(fanotify_{fid,path,perm,mnt}_event_cachep)
Assisted-by: LLM
Cc: Jan Kara <jack@suse.cz>
Cc: Amir Goldstein <amir73il@gmail.com>
Cc: linux-fsdevel@vger.kernel.org
Signed-off-by: Harry Yoo (Meta) <harry@kernel.org>
---
fs/notify/fanotify/fanotify_user.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
index a32c6634d592..bc1227e521a8 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -2184,14 +2184,16 @@ static int __init fanotify_user_setup(void)
fanotify_mark_cache = KMEM_CACHE(fanotify_mark,
SLAB_PANIC|SLAB_ACCOUNT);
fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
- SLAB_PANIC);
+ SLAB_PANIC|SLAB_MAY_ACCOUNT);
fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
- SLAB_PANIC);
+ SLAB_PANIC|SLAB_MAY_ACCOUNT);
if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
fanotify_perm_event_cachep =
- KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
+ KMEM_CACHE(fanotify_perm_event,
+ SLAB_PANIC|SLAB_MAY_ACCOUNT);
}
- fanotify_mnt_event_cachep = KMEM_CACHE(fanotify_mnt_event, SLAB_PANIC);
+ fanotify_mnt_event_cachep = KMEM_CACHE(fanotify_mnt_event,
+ SLAB_PANIC|SLAB_MAY_ACCOUNT);
fanotify_max_queued_events = FANOTIFY_DEFAULT_MAX_EVENTS;
init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS] =
--
2.54.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH slab/for-next 03/14] lib/xarray: specify SLAB_MAY_ACCOUNT for radix_tree_node cache
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-12-03 14:27 ` Hao Li
0 siblings, 0 replies; 6+ messages in thread
From: Hao Li @ 2026-12-03 14:27 UTC (permalink / raw)
To: Harry Yoo (Meta)
Cc: Vlastimil Babka, Andrew Morton, Suren Baghdasaryan,
Johannes Weiner, Michal Hocko, Shakeel Butt, Roman Gushchin,
Christoph Lameter, David Rientjes, Muchun Song, Hao Ge,
Pedro Falcato, linux-mm, linux-kernel, kernel-team,
Matthew Wilcox, linux-fsdevel
On Wed, Oct 21, 2026 at 12:46:44PM +0100, Harry Yoo (Meta) wrote:
> XArrays with XA_FLAGS_ACCOUNT (e.g. the page cache) allocate nodes with
> __GFP_ACCOUNT, and xarray/IDR callers can also pass GFP_KERNEL_ACCOUNT
> directly. Specify SLAB_MAY_ACCOUNT when the object may be charged to a
> memcg. __GFP_ACCOUNT without SLAB_MAY_ACCOUNT will not be allowed.
>
> Path: __filemap_add_folio() (i_pages has XA_FLAGS_ACCOUNT)
> -> xas_store() -> xas_alloc() (gfp |= __GFP_ACCOUNT)
> -> kmem_cache_alloc_lru(radix_tree_node_cachep)
>
> Assisted-by: LLM
> Cc: Matthew Wilcox <willy@infradead.org>
> Cc: linux-fsdevel@vger.kernel.org
> Signed-off-by: Harry Yoo (Meta) <harry@kernel.org>
> ---
> lib/radix-tree.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
Looks good to me.
Reviewed-by: Hao Li <hao.li@linux.dev>
--
Thanks,
Hao
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH slab/for-next 04/14] fs/buffer: specify SLAB_MAY_ACCOUNT for buffer_head cache
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-12-03 14:28 ` Hao Li
0 siblings, 0 replies; 6+ messages in thread
From: Hao Li @ 2026-12-03 14:28 UTC (permalink / raw)
To: Harry Yoo (Meta)
Cc: Vlastimil Babka, Andrew Morton, Suren Baghdasaryan,
Johannes Weiner, Michal Hocko, Shakeel Butt, Roman Gushchin,
Christoph Lameter, David Rientjes, Muchun Song, Hao Ge,
Pedro Falcato, linux-mm, linux-kernel, kernel-team,
Alexander Viro, Christian Brauner, Jan Kara, linux-fsdevel
On Wed, Oct 21, 2026 at 12:46:45PM +0100, Harry Yoo (Meta) wrote:
> Buffer heads are allocated with __GFP_ACCOUNT to charge them to the
> memcg of the folio they are attached to. Specify SLAB_MAY_ACCOUNT when
> the object may be charged to a memcg. __GFP_ACCOUNT without
> SLAB_MAY_ACCOUNT will not be allowed.
>
> Path: ext4_getblk() -> folio_alloc_buffers(GFP_NOFS | __GFP_ACCOUNT)
> -> alloc_buffer_head() -> kmem_cache_zalloc(bh_cachep)
>
> Assisted-by: LLM
> Cc: Alexander Viro <viro@zeniv.linux.org.uk>
> Cc: Christian Brauner <brauner@kernel.org>
> Cc: Jan Kara <jack@suse.cz>
> Cc: linux-fsdevel@vger.kernel.org
> Signed-off-by: Harry Yoo (Meta) <harry@kernel.org>
> ---
> fs/buffer.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
Looks good to me.
Reviewed-by: Hao Li <hao.li@linux.dev>
--
Thanks,
Hao
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-03 14:29 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-21 11:46 [PATCH slab/for-next 00/14] mm/slab: mandate SLAB_MAY_ACCOUNT for __GFP_ACCOUNT allocations to avoid wasting memory for slab metadata 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-12-03 14:27 ` Hao Li
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-12-03 14:28 ` Hao Li
2026-10-21 11:46 ` [PATCH slab/for-next 10/14] fanotify: specify SLAB_MAY_ACCOUNT for event caches Harry Yoo (Meta)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox