From: Harry Yoo <harry@kernel.org>
To: Marco Elver <elver@google.com>, Pedro Falcato <pfalcato@suse.de>
Cc: Vlastimil Babka <vbabka@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>,
"Liam R. Howlett" <Liam.Howlett@oracle.com>,
Andrey Konovalov <andreyknvl@gmail.com>,
Bill Wendling <morbo@google.com>,
David Hildenbrand <david@kernel.org>,
David Rientjes <rientjes@google.com>,
Dmitry Vyukov <dvyukov@google.com>, Jann Horn <jannh@google.com>,
Justin Stitt <justinstitt@google.com>,
KP Singh <kpsingh@kernel.org>, Kees Cook <kees@kernel.org>,
Lorenzo Stoakes <ljs@kernel.org>,
Matteo Rizzo <matteorizzo@google.com>,
Michal Hocko <mhocko@suse.com>, Mike Rapoport <rppt@kernel.org>,
Nathan Chancellor <nathan@kernel.org>,
Nick Desaulniers <nick.desaulniers+lkml@gmail.com>,
Roman Gushchin <roman.gushchin@linux.dev>,
Suren Baghdasaryan <surenb@google.com>,
linux-hardening@vger.kernel.org, Nicolas Schier <nsc@kernel.org>,
Dennis Zhou <dennis@kernel.org>, Tejun Heo <tj@kernel.org>,
Christoph Lameter <cl@gentwo.org>, Hao Li <hao.li@linux.dev>,
"Liam R. Howlett" <liam@infradead.org>,
Alexander Potapenko <glider@google.com>,
Miguel Ojeda <ojeda@kernel.org>,
linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-mm@kvack.org, kasan-dev@googlegroups.com,
llvm@lists.linux.dev, GONG Ruiqi <gongruiqi1@huawei.com>
Subject: Re: [PATCH v4 1/3] slab: support for compiler-assisted type-based slab cache partitioning
Date: Wed, 20 May 2026 08:57:42 +0900 [thread overview]
Message-ID: <d0cea3c8-f1ce-4dfe-a116-8f36d5b4308e@kernel.org> (raw)
In-Reply-To: <CANpmjNOYd9RnJARZ7qUDifsftwWOhhakaWSu926PKA6-SepTwQ@mail.gmail.com>
On 5/18/26 11:08 PM, Marco Elver wrote:
> On Fri, 15 May 2026 at 16:28, Pedro Falcato <pfalcato@suse.de> wrote:
>>
>> On Mon, May 11, 2026 at 10:00:48PM +0200, Marco Elver wrote:
>>> Rework the general infrastructure around RANDOM_KMALLOC_CACHES into more
>>> flexible KMALLOC_PARTITION_CACHES, with the former being a partitioning
>>> mode of the latter.
>>>
>>> Introduce a new mode, KMALLOC_PARTITION_TYPED, which leverages a feature
>>> available in Clang 22 and later, called "allocation tokens" via
>>> __builtin_infer_alloc_token() [1]. Unlike KMALLOC_PARTITION_RANDOM
>>> (formerly RANDOM_KMALLOC_CACHES), this mode deterministically assigns a
>>> slab cache to an allocation of type T, regardless of allocation site.
>>>
>>> The builtin __builtin_infer_alloc_token(<malloc-args>, ...) instructs
>>> the compiler to infer an allocation type from arguments commonly passed
>>> to memory-allocating functions and returns a type-derived token ID. The
>>> implementation passes kmalloc-args to the builtin: the compiler performs
>>> best-effort type inference, and then recognizes common patterns such as
>>> `kmalloc(sizeof(T), ...)`, `kmalloc(sizeof(T) * n, ...)`, but also
>>> `(T *)kmalloc(...)`. Where the compiler fails to infer a type the
>>> fallback token (default: 0) is chosen.
>>>
>>> Note: kmalloc_obj(..) APIs fix the pattern how size and result type are
>>> expressed, and therefore ensures there's not much drift in which
>>> patterns the compiler needs to recognize. Specifically, kmalloc_obj()
>>> and friends expand to `(TYPE *)KMALLOC(__obj_size, GFP)`, which the
>>> compiler recognizes via the cast to TYPE*.
>>>
>>> Clang's default token ID calculation is described as [1]:
>>>
>>> typehashpointersplit: This mode assigns a token ID based on the hash
>>> of the allocated type's name, where the top half ID-space is reserved
>>> for types that contain pointers and the bottom half for types that do
>>> not contain pointers.
>>>
>>> Separating pointer-containing objects from pointerless objects and data
>>> allocations can help mitigate certain classes of memory corruption
>>> exploits [2]: attackers who gains a buffer overflow on a primitive
>>> buffer cannot use it to directly corrupt pointers or other critical
>>> metadata in an object residing in a different, isolated heap region.
>>>
>>> It is important to note that heap isolation strategies offer a
>>> best-effort approach, and do not provide a 100% security guarantee,
>>> albeit achievable at relatively low performance cost. Note that this
>>> also does not prevent cross-cache attacks: while waiting for future
>>> features like SLAB_VIRTUAL [3] to provide physical page isolation, this
>>> feature should be deployed alongside SHUFFLE_PAGE_ALLOCATOR and
>>> init_on_free=1 to mitigate cross-cache attacks and page-reuse attacks as
>>> much as possible today.
>>>
>>> With all that, my kernel (x86 defconfig) shows me a histogram of slab
>>> cache object distribution per /proc/slabinfo (after boot):
>>>
>>> <slab cache> <objs> <hist>
>>> kmalloc-part-15 1465 ++++++++++++++
>>> kmalloc-part-14 2988 +++++++++++++++++++++++++++++
>>> kmalloc-part-13 1656 ++++++++++++++++
>>> kmalloc-part-12 1045 ++++++++++
>>> kmalloc-part-11 1697 ++++++++++++++++
>>> kmalloc-part-10 1489 ++++++++++++++
>>> kmalloc-part-09 965 +++++++++
>>> kmalloc-part-08 710 +++++++
>>> kmalloc-part-07 100 +
>>> kmalloc-part-06 217 ++
>>> kmalloc-part-05 105 +
>>> kmalloc-part-04 4047 ++++++++++++++++++++++++++++++++++++++++
>>> kmalloc-part-03 183 +
>>> kmalloc-part-02 283 ++
>>> kmalloc-part-01 316 +++
>>> kmalloc 1422 ++++++++++++++
>>
>> Hi,
>>
>> A couple of questions (I apologise if this was asked before, I wasn't involved
>> in this thread):
>>
>> 1) What's the object behind kmalloc-part-04? I imagine it's a single type
>> getting allocated a lot?
>
> That's from __kmemdup_nul().
__kmemdup_nul() is probably a good fit for SLAB_BUCKETS?
--
Cheers,
Harry / Hyeonggon
prev parent reply other threads:[~2026-05-19 23:57 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-11 20:00 [PATCH v4 1/3] slab: support for compiler-assisted type-based slab cache partitioning Marco Elver
2026-05-11 20:00 ` [PATCH v4 2/3] slab: improve KMALLOC_PARTITION_RANDOM randomness Marco Elver
2026-05-12 5:13 ` Harry Yoo (Oracle)
2026-05-12 9:51 ` Marco Elver
2026-05-12 10:36 ` Vlastimil Babka (SUSE)
2026-05-12 12:54 ` Marco Elver
2026-05-14 7:10 ` Vlastimil Babka (SUSE)
2026-05-14 8:22 ` David Laight
2026-05-14 23:01 ` Marco Elver
2026-05-11 20:00 ` [PATCH v4 3/3] slab: fix kernel-docs for mm-api Marco Elver
2026-05-12 4:57 ` [PATCH v4 1/3] slab: support for compiler-assisted type-based slab cache partitioning Harry Yoo (Oracle)
2026-05-14 9:01 ` Vlastimil Babka (SUSE)
2026-05-14 10:13 ` Harry Yoo (Oracle)
2026-05-15 13:24 ` Marco Elver
2026-05-15 14:28 ` Pedro Falcato
[not found] ` <CANpmjNOYd9RnJARZ7qUDifsftwWOhhakaWSu926PKA6-SepTwQ@mail.gmail.com>
2026-05-19 23:57 ` Harry Yoo [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=d0cea3c8-f1ce-4dfe-a116-8f36d5b4308e@kernel.org \
--to=harry@kernel.org \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=andreyknvl@gmail.com \
--cc=cl@gentwo.org \
--cc=david@kernel.org \
--cc=dennis@kernel.org \
--cc=dvyukov@google.com \
--cc=elver@google.com \
--cc=glider@google.com \
--cc=gongruiqi1@huawei.com \
--cc=gustavoars@kernel.org \
--cc=hao.li@linux.dev \
--cc=jannh@google.com \
--cc=justinstitt@google.com \
--cc=kasan-dev@googlegroups.com \
--cc=kees@kernel.org \
--cc=kpsingh@kernel.org \
--cc=liam@infradead.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=llvm@lists.linux.dev \
--cc=matteorizzo@google.com \
--cc=mhocko@suse.com \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=nick.desaulniers+lkml@gmail.com \
--cc=nsc@kernel.org \
--cc=ojeda@kernel.org \
--cc=pfalcato@suse.de \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=rppt@kernel.org \
--cc=surenb@google.com \
--cc=tj@kernel.org \
--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