mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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>
Subject: [PATCH slab/for-next 14/14] mm/slab: do not set SLAB_MAY_ACCOUNT for all caches
Date: Wed, 21 Oct 2026 12:46:55 +0100	[thread overview]
Message-ID: <20261021-memalloc-prof-dont-waste-slabobj-ext-metadata-v1-14-096800aa3330@kernel.org> (raw)
In-Reply-To: <20261021-memalloc-prof-dont-waste-slabobj-ext-metadata-v1-0-096800aa3330@kernel.org>

Now that all caches whose objects may be allocated with __GFP_ACCOUNT
specify SLAB_MAY_ACCOUNT (or SLAB_ACCOUNT, which implies it), stop
assuming any cache may be used with __GFP_ACCOUNT. This avoids wasting
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 | 16 +++++++++++++---
 mm/slab_common.c     |  8 +++-----
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/include/linux/slab.h b/include/linux/slab.h
index cda126def67a..d69445346494 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -200,14 +200,24 @@ enum _slab_flag_bits {
 /**
  * define SLAB_ACCOUNT - Account allocations to memcg.
  *
- * All object allocations from this cache will be memcg accounted, regardless of
- * __GFP_ACCOUNT being or not being passed to individual allocations.
+ * All object allocations from this cache will be memcg accounted.
+ * SLAB_ACCOUNT implies SLAB_MAY_ACCOUNT.
  */
 #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
+#endif
+
+/**
+ * define SLAB_MAY_ACCOUNT
+ *
+ * Objects will be memcg accounted when __GFP_ACCOUNT flag is specified.
+ * Requesting __GFP_ACCOUNT allocations without SLAB_MAY_ACCOUNT is not allowed.
+ */
+#ifdef CONFIG_MEMCG
+# define SLAB_MAY_ACCOUNT	__SLAB_FLAG_BIT(_SLAB_MAY_ACCOUNT)
+#else
 # define SLAB_MAY_ACCOUNT	__SLAB_FLAG_UNUSED
 #endif
 
diff --git a/mm/slab_common.c b/mm/slab_common.c
index e66c0778ee98..c69aac8bb0e4 100644
--- a/mm/slab_common.c
+++ b/mm/slab_common.c
@@ -359,12 +359,10 @@ struct kmem_cache *__kmem_cache_create_args(const char *name,
 		goto out_unlock;
 	}
 
-	/*
-	 * For now we assume any cache can be used with __GFP_ACCOUNT and thus
-	 * may need to store objcg pointers for objects
-	 */
-	if (!mem_cgroup_kmem_disabled())
+	if (flags & SLAB_ACCOUNT)
 		flags |= SLAB_MAY_ACCOUNT;
+	if (mem_cgroup_kmem_disabled())
+		flags &= ~SLAB_MAY_ACCOUNT;
 
 	/* Fail closed on bad usersize of useroffset values. */
 	if (!IS_ENABLED(CONFIG_HARDENED_USERCOPY) ||

-- 
2.54.0


      parent reply	other threads:[~2026-10-21 11:48 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 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 ` Harry Yoo (Meta) [this message]

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-14-096800aa3330@kernel.org \
    --to=harry@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=cl@gentwo.org \
    --cc=hannes@cmpxchg.org \
    --cc=hao.ge@linux.dev \
    --cc=hao.li@linux.dev \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=muchun.song@linux.dev \
    --cc=pfalcato@suse.de \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.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®