linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Harry Yoo <harry.yoo@oracle.com>
To: vbabka@suse.cz, rientjes@google.com, cl@gentwo.org,
	akpm@linux-foundation.org
Cc: roman.gushchin@linux.dev, willy@infradead.org, linux-mm@kvack.org
Subject: Re: [PATCH] mm/slab: fix folio_test_{anon,ksm}() false positive on slabs
Date: Mon, 9 Jun 2025 23:12:34 +0900	[thread overview]
Message-ID: <aEbr0gXynDY__Br-@hyeyoo> (raw)
In-Reply-To: <20250609132723.13118-1-harry.yoo@oracle.com>

On Mon, Jun 09, 2025 at 10:27:23PM +0900, Harry Yoo wrote:
> When running tools/mm/page-types (with flags, page-counts, MB omitted),
> it reports that some slabs have KPF_ANON and KPF_KSM set:
> 
>   $ sudo ./page-types | grep slab
>   _______S___________________________________	slab
>   _______S____a________x_____________________	slab,anonymous,ksm
> 
> This is unexpected, as slab memory should never be treated as anonymous
> memory. This is because slab->slabs shares the same offset as
> page->mapping and setting the lower two bits of ->slabs field means
> folio_test_anon() and folio_test_ksm() will return true on the slab:
> 
>   [ field ]		[ offset ] [ size ]
>   page->mapping		24         8
>   page->lru		8	   16
> 
>   slab->next		16	   8
>   slab->slabs		24	   4
> 
> Reorder ->slabs and ->next, so that the layout will be:
> 
>   slab->slabs		16	   4
>   slab->next		24	   8
> 
> After reordering, slab->slabs shares its offset with page->lru.prev,
> which is not a problem. slab->next now shares the offset with
> page->mapping, but that's fine, as a slab is double-word aligned.
> 
> With the change, the nonsense slab pages disappear:
> 
>   $ sudo ./page-types | grep slab
>   _______S___________________________________	slab
> 
> Fixes: 130d4df57390 ("mm/sl[au]b: rearrange struct slab fields to allow larger rcu_head")
> Signed-off-by: Harry Yoo <harry.yoo@oracle.com>
> ---

Oops, I should have mentioned that this may not _completely_ fix
the false positive.

Three years ago, there was a report [1] saying that CC_OPTIMIZE_FOR_SIZE
causes the function rcu_free_slab()'s alignment to be less than 4 bytes,
which can also lead to false positives :(

I don't know how the minimum function alignement has changed since then,
need to check.

[1] https://lore.kernel.org/all/ea96c78c-e1dc-1364-e91-51909f82388b@google.com/

> No Cc: stable because we don't usually check folio_test_anon() on slabs.
> 
>  mm/slab.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/mm/slab.h b/mm/slab.h
> index 05a21dc796e0..2e7064f7709e 100644
> --- a/mm/slab.h
> +++ b/mm/slab.h
> @@ -59,8 +59,8 @@ struct slab {
>  				struct list_head slab_list;
>  #ifdef CONFIG_SLUB_CPU_PARTIAL
>  				struct {
> -					struct slab *next;
>  					int slabs;	/* Nr of slabs left */
> +					struct slab *next;
>  				};
>  #endif
>  			};
> -- 
> 2.43.0
> 

-- 
Cheers,
Harry / Hyeonggon


  reply	other threads:[~2025-06-09 14:12 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-09 13:27 Harry Yoo
2025-06-09 14:12 ` Harry Yoo [this message]
2025-06-09 14:22 ` Vlastimil Babka
2025-06-10 12:38   ` Harry Yoo
2025-06-10 13:03     ` Vlastimil Babka
2025-06-10 13:17       ` Harry Yoo
2025-06-12 12:44         ` David Hildenbrand
2025-06-09 16:06 ` Christoph Lameter (Ampere)
2025-06-10 12:39   ` Harry Yoo

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=aEbr0gXynDY__Br-@hyeyoo \
    --to=harry.yoo@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=cl@gentwo.org \
    --cc=linux-mm@kvack.org \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=vbabka@suse.cz \
    --cc=willy@infradead.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