[PATCH slab/for-next 14/14] mm/slab: do not set SLAB_MAY_ACCOUNT for all caches

From: Harry Yoo (Meta)

Date: Wed Oct 21 2026 - 07:57:23 EST


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@xxxxxxxxxx>
---
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