Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH slab/for-next v2 00/14] mm/slab: mandate SLAB_MAY_ACCOUNT for __GFP_ACCOUNT allocations to avoid wasting memory for slab metadata
@ 2026-11-25 21:37 Harry Yoo (Meta)
  2026-11-25 21:37 ` [PATCH slab/for-next v2 01/14] mm/slab: allow passing SLAB_MAY_ACCOUNT to kmem_cache_create() Harry Yoo (Meta)
                   ` (14 more replies)
  0 siblings, 15 replies; 20+ messages in thread
From: Harry Yoo (Meta) @ 2026-11-25 21:37 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,
	Matthew Wilcox, linux-fsdevel, Alexander Viro, Christian Brauner,
	Jan Kara, David S. Miller, Jakub Kicinski,
	Sebastian Andrzej Siewior, Jiayuan Chen, netdev, Kees Cook,
	David Hildenbrand, Lorenzo Stoakes, Catalin Marinas, Will Deacon,
	Mark Rutland, Madhavan Srinivasan, Michael Ellerman,
	Nicholas Piggin, Robin Murphy, Joerg Roedel (AMD), Amir Goldstein,
	Casey Schaufler, Paul Moore, James Morris, Serge E. Hallyn,
	linux-security-module, Richard Weinberger, Anton Ivanov,
	Johannes Berg, Eric Dumazet, maple-tree, Paolo Abeni,
	Simon Horman, Jason Xing, Willem de Bruijn, Mina Almasry,
	Björn Töpel, 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, Anshuman Khandual, linux-arm-kernel,
	Christophe Leroy (CS GROUP), linuxppc-dev, Niklas Schnelle,
	Matthew Rosato, Gerald Schaefer, Chen-Yu Tsai, Jernej Skrabec,
	Samuel Holland, iommu, linux-s390, linux-sunxi, linux-um, cgroups

TL;DR
-----

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.

Longer version of the cover letter
----------------------------------

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.

Memory savings and testing
--------------------------

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>
---
Changes in v2:
- Rebased onto the latest slab/for-next.
- Collected Acked-by from Jakub and Reviewed-by from Casey. Thanks!
- Patch 1
  - Resolve "sysfs: cannot create duplicate filename" warning by
    encoding SLAB_MAY_ACCOUNT in create_unique_id() (kernel test robot,
    Sashiko).
  - Define SLAB_MAY_ACCOUNT in tools/include/linux/slab.h to fix
    the build of tools/testing/{radix-tree,vma} (Mike, Sashiko).
- Swept the tree again on the new base. No new cache needs
  SLAB_MAY_ACCOUNT.
- Re-tested all the conversions on the new base.
- Link to v1: https://patch.msgid.link/20261021-memalloc-prof-dont-waste-slabobj-ext-metadata-v1-0-096800aa3330@kernel.org

Range-diff against v1:

 1:  f749199b4fde !  1:  31425f1e4a92 mm/slab: allow passing SLAB_MAY_ACCOUNT to kmem_cache_create()
    @@ Commit message
         Callers will specify SLAB_MAY_ACCOUNT for caches whose objects may be
         allocated with __GFP_ACCOUNT. Accept the flag in SLAB_CORE_FLAGS.
     
    +    SLAB_MAY_ACCOUNT prevents merging but is not part of the sysfs ID. This
    +    was fine while all mergeable caches had the flag. Now that callers may
    +    specify it, encode it in create_unique_id() to keep the ID unique.
    +
    +    Define the flag in tools/ as well to avoid breaking the userspace tests.
    +
         Signed-off-by: Harry Yoo (Meta) <harry@kernel.org>
     
      ## mm/slab.h ##
    @@ mm/slab.h: void flush_rcu_sheaves_on_cache(struct kmem_cache *s);
      			 SLAB_NO_USER_FLAGS | SLAB_KMALLOC | SLAB_NO_MERGE)
      
      #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
    +
    + ## mm/slub.c ##
    +@@ mm/slub.c: static char *create_unique_id(struct kmem_cache *s)
    + 		*p++ = 'a';
    + 	if (s->flags & SLAB_ACCOUNT)
    + 		*p++ = 'A';
    ++	if (s->flags & SLAB_MAY_ACCOUNT)
    ++		*p++ = 'M';
    + 	if (s->flags & SLAB_DEBUG_NOOP)
    + 		*p++ = 'N';
    + 	if (p != name + 1)
    +
    + ## tools/include/linux/slab.h ##
    +@@ tools/include/linux/slab.h: enum _slab_flag_bits {
    + 	_SLAB_PANIC,
    + 	_SLAB_TYPESAFE_BY_RCU,
    + 	_SLAB_ACCOUNT,
    ++	_SLAB_MAY_ACCOUNT,
    + 	_SLAB_FLAGS_LAST_BIT
    + };
    + 
    +@@ tools/include/linux/slab.h: enum _slab_flag_bits {
    + #define SLAB_TYPESAFE_BY_RCU	__SLAB_FLAG_BIT(_SLAB_TYPESAFE_BY_RCU)
    + #ifdef CONFIG_MEMCG
    + # define SLAB_ACCOUNT		__SLAB_FLAG_BIT(_SLAB_ACCOUNT)
    ++# define SLAB_MAY_ACCOUNT	__SLAB_FLAG_BIT(_SLAB_MAY_ACCOUNT)
    + #else
    + # define SLAB_ACCOUNT		__SLAB_FLAG_UNUSED
    ++# define SLAB_MAY_ACCOUNT	__SLAB_FLAG_UNUSED
    + #endif
    + 
    + void *kmalloc(size_t size, gfp_t gfp);
 2:  7bde3ab6533f !  2:  9323ceca452f lib/maple_tree: specify SLAB_MAY_ACCOUNT for maple_node cache
    @@ Commit message
         Path: mtree_insert_range(GFP_KERNEL_ACCOUNT) -> mas_nomem()
           -> mas_alloc_nodes() -> kmem_cache_alloc(maple_node_cache)
     
    -    Assisted-by: LLM
         Cc: "Liam R. Howlett" <liam@infradead.org>
         Cc: Alice Ryhl <aliceryhl@google.com>
         Cc: Andrew Ballance <andrewjballance@gmail.com>
 3:  1c34ea46a76e !  3:  0558bdf4936a lib/xarray: specify SLAB_MAY_ACCOUNT for radix_tree_node cache
    @@ Commit message
           -> 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>
 4:  d9e638c3320d !  4:  96f41e2fa6db fs/buffer: specify SLAB_MAY_ACCOUNT for buffer_head cache
    @@ Commit message
         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>
 5:  c986fd19aacd !  5:  4f6b86b850c7 net: skbuff: specify SLAB_MAY_ACCOUNT for skbuff_head_cache
    @@ Commit message
           -> alloc_skb_with_frags() -> __alloc_skb()
           -> kmem_cache_alloc_node(skbuff_head_cache)
     
    -    Assisted-by: LLM
         Cc: "David S. Miller" <davem@davemloft.net>
         Cc: Eric Dumazet <edumazet@google.com>
         Cc: Jakub Kicinski <kuba@kernel.org>
    @@ Commit message
         Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
         Cc: Jiayuan Chen <jiayuan.chen@linux.dev>
         Cc: netdev@vger.kernel.org
    +    Acked-by: Jakub Kicinski <kuba@kernel.org>
         Signed-off-by: Harry Yoo (Meta) <harry@kernel.org>
     
      ## net/core/skbuff.c ##
 6:  c86eb6c221f7 !  6:  c6f61c47e801 kernel/fork: specify SLAB_MAY_ACCOUNT for thread_stack cache
    @@ Commit message
           -> dup_task_struct() -> alloc_thread_stack_node()
           -> kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP)
     
    -    Assisted-by: LLM
         Cc: Kees Cook <kees@kernel.org>
         Cc: David Hildenbrand <david@kernel.org>
         Cc: Lorenzo Stoakes <ljs@kernel.org>
 7:  a37935e9f1d8 !  7:  7c448f3688f4 arm64/mm: specify SLAB_MAY_ACCOUNT for pgd_cache
    @@ Commit message
         Path (e.g. 64K pages): copy_process() -> mm_init() -> pgd_alloc()
           -> kmem_cache_alloc(pgd_cache, GFP_PGTABLE_USER)
     
    -    Assisted-by: LLM
         Cc: Catalin Marinas <catalin.marinas@arm.com>
         Cc: Will Deacon <will@kernel.org>
         Cc: Mark Rutland <mark.rutland@arm.com>
 8:  948c9aa21dbf !  8:  8d5c29a8d6d6 powerpc/mm: specify SLAB_MAY_ACCOUNT for pgtable caches
    @@ Commit message
           -> kmem_cache_alloc(PGT_CACHE(PGD_INDEX_SIZE),
                               pgtable_gfp_flags(mm, GFP_KERNEL))
     
    -    Assisted-by: LLM
         Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
         Cc: Michael Ellerman <mpe@ellerman.id.au>
         Cc: Nicholas Piggin <npiggin@gmail.com>
 9:  0b2d6a632aeb !  9:  9e68e442c86b iommu: specify SLAB_MAY_ACCOUNT for slab-backed page table caches
    @@ Commit message
           iommu_map(GFP_KERNEL_ACCOUNT) -> sun50i_iommu_map()
             -> sun50i_dte_get_page_table() -> kmem_cache_zalloc(iommu->pt_pool)
     
    -    Assisted-by: LLM
         Cc: Will Deacon <will@kernel.org>
         Cc: Robin Murphy <robin.murphy@arm.com>
         Cc: "Joerg Roedel (AMD)" <joro@8bytes.org>
10:  df629ca9f8a3 ! 10:  0fb6578f9d32 fanotify: specify SLAB_MAY_ACCOUNT for event caches
    @@ Commit message
           -> 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
11:  d3fd8833b207 ! 11:  cd123bafe26d smack: specify SLAB_MAY_ACCOUNT for smack_rule cache
    @@ Commit message
           -> security_prepare_creds(GFP_KERNEL_ACCOUNT) -> smack_cred_prepare()
           -> smk_copy_rules() -> kmem_cache_zalloc(smack_rule_cache)
     
    -    Assisted-by: LLM
         Cc: Casey Schaufler <casey@schaufler-ca.com>
         Cc: Paul Moore <paul@paul-moore.com>
         Cc: James Morris <jmorris@namei.org>
         Cc: "Serge E. Hallyn" <serge@hallyn.com>
         Cc: linux-security-module@vger.kernel.org
    +    Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
         Signed-off-by: Harry Yoo (Meta) <harry@kernel.org>
     
      ## security/smack/smack_lsm.c ##
12:  e491d907f0da ! 12:  33a8d0abf54e hostfs: specify SLAB_MAY_ACCOUNT for inode cache
    @@ Commit message
         Path: lookup -> hostfs_iget() -> hostfs_alloc_inode()
           -> alloc_inode_sb(sb, hostfs_inode_cache, GFP_KERNEL_ACCOUNT)
     
    -    Assisted-by: LLM
         Cc: Richard Weinberger <richard@nod.at>
         Cc: Anton Ivanov <anton.ivanov@cambridgegreys.com>
         Cc: Johannes Berg <johannes@sipsolutions.net>
13:  495470a3e8c2 ! 13:  3dd416de21c1 mm/memcontrol: do not allow charging objects without SLAB_MAY_ACCOUNT
    @@ Commit message
         is never charged does not access the objcg slot again, and the free
         path already skips such caches via slab_needs_objcg().
     
    -    Assisted-by: LLM # Changelog
         Cc: Johannes Weiner <hannes@cmpxchg.org>
         Cc: Michal Hocko <mhocko@kernel.org>
         Cc: Shakeel Butt <shakeel.butt@linux.dev>
14:  84b9be2d2d42 ! 14:  533a72d59074 mm/slab: do not set SLAB_MAY_ACCOUNT for all caches
    @@ Commit message
         the objcg slot in slabobj_ext for caches that are never charged to a
         memcg.
     
    -    Assisted-by: LLM # Changelog
         Signed-off-by: Harry Yoo (Meta) <harry@kernel.org>
     
      ## include/linux/slab.h ##

---
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 +++-----
 mm/slub.c                          |  2 ++
 net/core/skbuff.c                  |  2 +-
 security/smack/smack_lsm.c         |  2 +-
 tools/include/linux/slab.h         |  3 +++
 19 files changed, 50 insertions(+), 26 deletions(-)
---
base-commit: 0b0985aeb9979c5fa5c3b872cecdf97ed4ed2b3e
change-id: 20261014-memalloc-prof-dont-waste-slabobj-ext-metadata-34f943ef9658

Best regards,
--  
Cheers,
Harry / Hyeonggon



^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2026-11-26 15:25 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 21:37 [PATCH slab/for-next v2 00/14] mm/slab: mandate SLAB_MAY_ACCOUNT for __GFP_ACCOUNT allocations to avoid wasting memory for slab metadata Harry Yoo (Meta)
2026-11-25 21:37 ` [PATCH slab/for-next v2 01/14] mm/slab: allow passing SLAB_MAY_ACCOUNT to kmem_cache_create() Harry Yoo (Meta)
2026-11-25 21:37 ` [PATCH slab/for-next v2 02/14] lib/maple_tree: specify SLAB_MAY_ACCOUNT for maple_node cache Harry Yoo (Meta)
2026-11-25 21:37 ` [PATCH slab/for-next v2 03/14] lib/xarray: specify SLAB_MAY_ACCOUNT for radix_tree_node cache Harry Yoo (Meta)
2026-11-25 21:37 ` [PATCH slab/for-next v2 04/14] fs/buffer: specify SLAB_MAY_ACCOUNT for buffer_head cache Harry Yoo (Meta)
2026-11-26  9:05   ` Jan Kara
2026-11-26 14:23     ` Harry Yoo
2026-11-25 21:37 ` [PATCH slab/for-next v2 05/14] net: skbuff: specify SLAB_MAY_ACCOUNT for skbuff_head_cache Harry Yoo (Meta)
2026-11-25 21:37 ` [PATCH slab/for-next v2 06/14] kernel/fork: specify SLAB_MAY_ACCOUNT for thread_stack cache Harry Yoo (Meta)
2026-11-25 21:37 ` [PATCH slab/for-next v2 07/14] arm64/mm: specify SLAB_MAY_ACCOUNT for pgd_cache Harry Yoo (Meta)
2026-11-25 21:37 ` [PATCH slab/for-next v2 08/14] powerpc/mm: specify SLAB_MAY_ACCOUNT for pgtable caches Harry Yoo (Meta)
2026-11-25 21:37 ` [PATCH slab/for-next v2 09/14] iommu: specify SLAB_MAY_ACCOUNT for slab-backed page table caches Harry Yoo (Meta)
2026-11-25 21:37 ` [PATCH slab/for-next v2 10/14] fanotify: specify SLAB_MAY_ACCOUNT for event caches Harry Yoo (Meta)
2026-11-26  9:06   ` Jan Kara
2026-11-25 21:37 ` [PATCH slab/for-next v2 11/14] smack: specify SLAB_MAY_ACCOUNT for smack_rule cache Harry Yoo (Meta)
2026-11-25 21:37 ` [PATCH slab/for-next v2 12/14] hostfs: specify SLAB_MAY_ACCOUNT for inode cache Harry Yoo (Meta)
2026-11-25 21:37 ` [PATCH slab/for-next v2 13/14] mm/memcontrol: do not allow charging objects without SLAB_MAY_ACCOUNT Harry Yoo (Meta)
2026-11-25 21:37 ` [PATCH slab/for-next v2 14/14] mm/slab: do not set SLAB_MAY_ACCOUNT for all caches Harry Yoo (Meta)
2026-11-26 11:10   ` sashiko-bot
2026-11-26 15:24 ` [PATCH slab/for-next v2 00/14] mm/slab: mandate SLAB_MAY_ACCOUNT for __GFP_ACCOUNT allocations to avoid wasting memory for slab metadata Shakeel Butt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox