Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Harry Yoo (Meta)" <harry@kernel.org>,
	"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, kernel-team@meta.com
Subject: Re: [PATCH slab/for-next 01/14] mm/slab: allow passing SLAB_MAY_ACCOUNT to kmem_cache_create()
Date: Thu, 22 Oct 2026 11:48:34 +0000	[thread overview]
Message-ID: <sashiko-outbox-181382@kernel.org> (raw)
In-Reply-To: <20261021-memalloc-prof-dont-waste-slabobj-ext-metadata-v1-1-096800aa3330@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] mm/slab: unhandled SLAB_MAY_ACCOUNT in create_unique_id() and nokmem path
--

commit 6d48a33523fd53d7ecd1bf1fa6ab70ec7126128f
Author: Harry Yoo (Meta) <harry@kernel.org>

mm/slab: allow passing SLAB_MAY_ACCOUNT to kmem_cache_create()

This commit adds SLAB_MAY_ACCOUNT to SLAB_CORE_FLAGS so callers of
kmem_cache_create() can explicitly specify SLAB_MAY_ACCOUNT for caches whose
objects may be allocated with __GFP_ACCOUNT.

> diff --git a/mm/slab.h b/mm/slab.h
> index e94ac63d10dc0..31783311e23ea 100644
> --- a/mm/slab.h
> +++ b/mm/slab.h
> @@ -472,7 +472,7 @@ void flush_rcu_sheaves_on_cache(struct kmem_cache *s);
>  			 SLAB_CACHE_DMA32 | SLAB_PANIC | \
>  			 SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS | \
>  			 SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
> -			 SLAB_TEMPORARY | SLAB_ACCOUNT | \
> +			 SLAB_TEMPORARY | SLAB_ACCOUNT | SLAB_MAY_ACCOUNT | \
>  			 SLAB_NO_USER_FLAGS | SLAB_KMALLOC | SLAB_NO_MERGE)
>  
>  #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \

[Severity: Medium]
When callers pass SLAB_MAY_ACCOUNT to kmem_cache_create(), could this cause a
sysfs kobject naming collision in create_unique_id() as well as unhandled
SLAB_MAY_ACCOUNT state when mem_cgroup_kmem_disabled() is true?

In mm/slab_common.c, SLAB_MAY_ACCOUNT is included in SLAB_MERGE_SAME:

#define SLAB_MERGE_SAME (SLAB_RECLAIM_ACCOUNT | SLAB_CACHE_DMA | \
			 SLAB_CACHE_DMA32 | SLAB_ACCOUNT | SLAB_MAY_ACCOUNT)

so find_mergeable() will not merge two aliasable caches of the same size if
only one of them has SLAB_MAY_ACCOUNT set.

However, in mm/slub.c, when do_kmem_cache_create() -> sysfs_slab_add() ->
create_unique_id() generates the sysfs identifier for an aliasable cache,
SLAB_MAY_ACCOUNT is not encoded into the name:

static char *create_unique_id(struct kmem_cache *s)
{
	...
	if (s->flags & SLAB_CACHE_DMA)
		*p++ = 'd';
	if (s->flags & SLAB_CACHE_DMA32)
		*p++ = 'D';
	if (s->flags & SLAB_RECLAIM_ACCOUNT)
		*p++ = 'a';
	if (s->flags & SLAB_CONSISTENCY_CHECKS)
		*p++ = 'F';
	if (s->flags & SLAB_ACCOUNT)
		*p++ = 'A';
	if (p != name + 1)
		*p++ = '-';
	p += snprintf(p, ID_STR_LENGTH - (p - name), "%07u", s->size);

Because create_unique_id() omits SLAB_MAY_ACCOUNT, two unmerged aliasable
caches of the same size that differ only in SLAB_MAY_ACCOUNT will produce the
same sysfs ID string, causing kobject_init_and_add() in sysfs_slab_add() to
fail with -EEXIST and trigger a sysfs_warn_dup() warning.

In addition, in mm/slab_common.c, __kmem_cache_create_args() currently does:

	if (!mem_cgroup_kmem_disabled())
		flags |= SLAB_MAY_ACCOUNT;

If mem_cgroup_kmem_disabled() is true (for example, with
cgroup.memory=nokmem) and a caller passes SLAB_MAY_ACCOUNT, the flag is not
cleared. That prevents find_mergeable() from merging the cache with caches
that lack SLAB_MAY_ACCOUNT (triggering the create_unique_id() collision
above), and causes cache_needs_objcg() in mm/slab.h:

static inline bool cache_needs_objcg(struct kmem_cache *cache)
{
	return (cache->flags & SLAB_MAY_ACCOUNT);
}

to return true, reserving unused _objcg metadata space via
cache_obj_ext_size() and slab_obj_ext_size() when slab object extensions are
allocated.

Note that a later change in the series, commit 51cadd40978b ("mm/slab: do not
set SLAB_MAY_ACCOUNT for all caches"), clears SLAB_MAY_ACCOUNT in
__kmem_cache_create_args() when mem_cgroup_kmem_disabled() is true, but it
does not update create_unique_id(), where the sysfs ID collision will then
also occur on default boots once SLAB_MAY_ACCOUNT is no longer set on all
caches.

Should create_unique_id() encode SLAB_MAY_ACCOUNT, and should
__kmem_cache_create_args() strip SLAB_MAY_ACCOUNT when
mem_cgroup_kmem_disabled() is true here as well?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261021-memalloc-prof-dont-waste-slabobj-ext-metadata-v1-0-096800aa3330@kernel.org?part=1


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

Thread overview: 27+ 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-22 11:48   ` sashiko-bot [this message]
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-22 11:48   ` sashiko-bot
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-22 11:48   ` sashiko-bot
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 19:19   ` Jakub Kicinski
2026-10-22 13:43     ` Harry Yoo
2026-10-23 17:56       ` Jakub Kicinski
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 16:59   ` Casey Schaufler
2026-10-21 18:48     ` Harry Yoo
2026-10-21 19:14       ` Casey Schaufler
2026-10-22 12:54         ` Harry Yoo
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)
2026-10-22 11:48   ` sashiko-bot
2026-10-26  1:02   ` kernel test robot

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=sashiko-outbox-181382@kernel.org \
    --to=sashiko-bot@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=harry@kernel.org \
    --cc=kernel-team@meta.com \
    --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=sashiko-reviews@lists.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