From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B37BD395AE6; Tue, 12 May 2026 10:36:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778582218; cv=none; b=GxjZUqFQ8U+nUc7xUJhptxyAvR5gfsRz+Ls8NZSa1IGZMsM0U5RVS5X+Ed+lmx0ivZImfW09EHo10Eqf+VTLJUU8+D3WghO3cAhsuJ6lMhaot4sJ8WexvmwawevigrESM+EOZ8nFQF8oTAt+7mDXOTmmC9gxW3TS4985h3P9D4A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778582218; c=relaxed/simple; bh=ifiiTIiRXCnHmOD37Bvgeqg4zW7Gr4q05xjPMaDkNRI=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=QErKpU5jcZxFENVwtdG273+moFQ4wv86uqHLeNG73LAQTwKX7Lcu9S6PD/LwAhPuCE9gMccEsq1nBK+gbVBXYUXuJCKbNEwGCjE+vRGg6gUaCoLrvE82zkUzE/VmDa10/v4elgDKq6da8qciNDjKqH1HXbnXUGDTYfSvcUsZWCg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lhlJ53dc; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lhlJ53dc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E1B7AC2BCB0; Tue, 12 May 2026 10:36:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778582218; bh=ifiiTIiRXCnHmOD37Bvgeqg4zW7Gr4q05xjPMaDkNRI=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=lhlJ53dcGZBBqoHiKDeEwdDXg71WNZ1BtgcUHaAh3matP6wkuRYkenuaV/9ZXg1jn aB9eHL7GlrhfxM8EskOYqOpN6xLo9j1jCf8rkShFmyTey/zx5kgDSLgevsTqGrcePz /4p4gjYs6F91D+dLdkR+fsAqOScbkzL4my5mU/PyvD/f1ZnMGkSJF3KHceIxRNMETo iWsaIrzNet9DI8ZOfoNHHBwuKUGXnuD6586FTF3R50iUoURNz7nUQLH3OBIggAvgiE x5dLanodJZ7DFC7OeYBrhuj4Bx5xs4YYhp3VnUCkGNyOBAm3rUbcYrFrXIk1fTNnZ3 1CgYM3SFRtTYw== Message-ID: Date: Tue, 12 May 2026 12:36:47 +0200 Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4 2/3] slab: improve KMALLOC_PARTITION_RANDOM randomness To: Marco Elver , Andrew Morton Cc: "Gustavo A. R. Silva" , "Liam R. Howlett" , Andrey Konovalov , Bill Wendling , David Hildenbrand , David Rientjes , Dmitry Vyukov , Jann Horn , Justin Stitt , KP Singh , Kees Cook , Lorenzo Stoakes , Matteo Rizzo , Michal Hocko , Mike Rapoport , Nathan Chancellor , Nick Desaulniers , Roman Gushchin , Suren Baghdasaryan , linux-hardening@vger.kernel.org, Nicolas Schier , Dennis Zhou , Tejun Heo , Christoph Lameter , Harry Yoo , Hao Li , "Liam R. Howlett" , Alexander Potapenko , Miguel Ojeda , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kasan-dev@googlegroups.com, llvm@lists.linux.dev References: <20260511200136.3201646-1-elver@google.com> <20260511200136.3201646-2-elver@google.com> From: "Vlastimil Babka (SUSE)" Content-Language: en-US In-Reply-To: <20260511200136.3201646-2-elver@google.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 5/11/26 22:00, Marco Elver wrote: > When using CONFIG_KMALLOC_PARTITION_RANDOM, _RET_IP_ was previously used > to identify the allocation site. _RET_IP_, however, evaluates to the > caller's parent's instruction pointer rather than the actual allocation > site; this would lead to collisions where a function performs multiple > allocations. > > With the generalization to kmalloc_token_t, we now generate the token at > the outermost macro, and using _THIS_IP_ would fix this for all cases. Hm but it means in patch 1 we make things even worse and then fix them again, and also improve what was suboptimal prior to the series. Would it be instead possible to reorder patches 1 and 2 so we improve the current state first, and then introduce typed partitioning without any changes to the randomized one? (aside from changing the previously correcly used cases _RET_IP_ to _CODE_LOCATION_). > Unfortunately, the generic implementation of _THIS_IP_ relies on taking > the address of a local label, which is considered broken by both GCC [1] > and Clang [2] because label addresses are only expected to be used with > computed gotos. While the generic version more or less works today, it > is known to be brittle. For example, Clang -O2 always returns 1 when > this function is inlined: > > static inline unsigned long get_ip(void) > { return ({ __label__ __here; __here: (unsigned long)&&__here; }); } > > To provide a reliable unique identifier without breaking architectures > relying on the generic _THIS_IP_, introduce _CODE_LOCATION_: it resolves > to _THIS_IP_ where architectures provide a safe implementation, and > falls back to a zero-cost static marker where _THIS_IP_ is broken. > > Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120071 [1] > Link: https://github.com/llvm/llvm-project/issues/138272 [2] > Signed-off-by: Marco Elver > --- > v4: > * New patch. > --- > include/linux/instruction_pointer.h | 24 ++++++++++++++++++++++++ > include/linux/slab.h | 2 +- > 2 files changed, 25 insertions(+), 1 deletion(-) > > diff --git a/include/linux/instruction_pointer.h b/include/linux/instruction_pointer.h > index aa0b3ffea935..ea5bc756bd99 100644 > --- a/include/linux/instruction_pointer.h > +++ b/include/linux/instruction_pointer.h > @@ -8,6 +8,30 @@ > > #ifndef _THIS_IP_ > #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) > +/* > + * The current generic definition of _THIS_IP_ is considered broken by GCC [1] > + * and Clang [2]. In particular, the address of a label is only expected to be > + * used with a computed goto. > + * > + * [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120071 > + * [2] https://github.com/llvm/llvm-project/issues/138272 > + * > + * Mark it as broken, so that appropriate fallback options can be implemented > + * for architectures that do not define their own _THIS_IP_. > + */ > +#define HAS_BROKEN_THIS_IP > +#endif > + > +/* > + * _CODE_LOCATION_ provides a unique identifier for the current code location. > + * When _THIS_IP_ is broken (generic version), we fall back to a static marker > + * which guarantees uniqueness and resolves to a constant address at link time, > + * avoiding runtime overhead and compiler optimizations breaking it. > + */ > +#ifdef HAS_BROKEN_THIS_IP > +#define _CODE_LOCATION_ ({ static const char __here; (unsigned long)&__here; }) > +#else > +#define _CODE_LOCATION_ _THIS_IP_ > #endif > > #endif /* _LINUX_INSTRUCTION_POINTER_H */ > diff --git a/include/linux/slab.h b/include/linux/slab.h > index c232f8a10af6..efab6b2ccf21 100644 > --- a/include/linux/slab.h > +++ b/include/linux/slab.h > @@ -503,7 +503,7 @@ int kmem_cache_shrink(struct kmem_cache *s); > typedef struct { unsigned long v; } kmalloc_token_t; > #ifdef CONFIG_KMALLOC_PARTITION_RANDOM > extern unsigned long random_kmalloc_seed; > -#define __kmalloc_token(...) ((kmalloc_token_t){ .v = _RET_IP_ }) > +#define __kmalloc_token(...) ((kmalloc_token_t){ .v = _CODE_LOCATION_ }) > #elif defined(CONFIG_KMALLOC_PARTITION_TYPED) > #define __kmalloc_token(...) ((kmalloc_token_t){ .v = __builtin_infer_alloc_token(__VA_ARGS__) }) > #endif