From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (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 C7159366074 for ; Thu, 21 May 2026 08:54:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779353690; cv=none; b=Rl+RzhWoWOJo9GUgBrjgDtIDSV2IHiyYHnQCyxgMw0QJlMECwWJCos3t4ZyhDwH+6bFSMVimVif5jbtee7O3U73SXvB0bADv0Lm2fWiWgvAl9FRtlSnSPoEGKmrhz9zCaEAGq5Ge8/PLZuzwj5G+XQQGCzcok9ErCBnqJmlBrcw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779353690; c=relaxed/simple; bh=y1RWsQoZ5ciCLtcahHUse8lL2tUXM9DD0tmhk73yKso=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=oxGWtbUKV8bjAvVZ37kHtWyXxIZgFzC7o9zIRtQUQBYPdU8Z8XiLW06l8qdlYkOk3CUCSeefca428FKoamUx0eBmhWVNazuBMEOQQ8vKlBjtcger3ybiNDkG3vIOp3xf6Om8ZEI62tkHz9yly4Pn4+FUPwi+HC1my/N6U0GH1eQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=jltey2Zi; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="jltey2Zi" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=0Ilcv+3HFGycOcrbDAOV5wflbbgHhbB2LuQrXIWG8+w=; b=jltey2Zitu5+2cqgcABSy3jf8D /cAYc78VUt67BfZmD53CQk9jkVzVOOBAOFJ9T2XQEzDpeXd7G9UkA6mSh2GCFwGBk1swqKzhmak4/ Z+Ab5YIgSlIsSF4GX+SsQeh9/KKm/Kp9Kxhz7RPT/Lwb50HPhj99Qu1XapraBZVhUFIc/rIqCDspI N1trWOOEPwyB91bO5ZHFaz6nucOtuB4HCG6Nt7BMoM4U4xZYyKOZH111Ro1vdRPr5LDzx4wFEmqW3 Ii/bZKDfVOmxEYBfijMm9W2EBCWXq0WdV3YipH65mIkPuD9umRVnBmyhtDU2PEZ7ew8eJYJhFbZ7S +8FH2mcQ==; Received: from 77-249-17-252.cable.dynamic.v4.ziggo.nl ([77.249.17.252] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.99.1 #2 (Red Hat Linux)) id 1wPzAu-00000001Gwc-1lil; Thu, 21 May 2026 08:54:41 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id 5AA40300673; Thu, 21 May 2026 10:54:39 +0200 (CEST) Date: Thu, 21 May 2026 10:54:39 +0200 From: Peter Zijlstra To: Marco Elver Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org Subject: Re: [PATCH] x86: Implement _THIS_IP_ using inline asm for 32-bit Message-ID: <20260521085439.GG3126523@noisy.programming.kicks-ass.net> References: <20260521000011.3926424-1-elver@google.com> <20260521070801.GE3126523@noisy.programming.kicks-ass.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Thu, May 21, 2026 at 10:34:38AM +0200, Marco Elver wrote: > On Thu, 21 May 2026 at 09:09, Peter Zijlstra wrote: > > On Thu, May 21, 2026 at 02:00:09AM +0200, Marco Elver wrote: > > > Both GCC [1] and Clang [2] consider the generic version of _THIS_IP_ to > > > be broken: > > > > > > #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) > > > > > > In particular, the address of a label is only expected to be used with a > > > computed goto. > > > > > > While the generic version more or less works today, it is known to be > > > brittle and may break with current and future optimizations. 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; }); } > > > > > > > Oh gawd :/ > > > > > 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 > > > --- > > > arch/x86/include/asm/linkage.h | 3 ++- > > > 1 file changed, 2 insertions(+), 1 deletion(-) > > > > > > diff --git a/arch/x86/include/asm/linkage.h b/arch/x86/include/asm/linkage.h > > > index a7294656ad90..bce3c6f4b94f 100644 > > > --- a/arch/x86/include/asm/linkage.h > > > +++ b/arch/x86/include/asm/linkage.h > > > @@ -13,11 +13,12 @@ > > > * The generic version tends to create spurious ENDBR instructions under > > > * certain conditions. > > > */ > > > -#define _THIS_IP_ ({ unsigned long __here; asm ("lea 0(%%rip), %0" : "=r" (__here)); __here; }) > > > +#define _THIS_IP_ ({ unsigned long __here; asm volatile("lea 0(%%rip), %0" : "=r" (__here)); __here; }) > > > #endif > > > > > > #ifdef CONFIG_X86_32 > > > #define asmlinkage CPP_ASMLINKAGE __attribute__((regparm(0))) > > > +#define _THIS_IP_ ({ unsigned long __ip; asm volatile("call 1f\n1: pop %0" : "=r" (__ip)); __ip; }) > > > > This will mess up the RSB and cause bad performance ripple effects for a > > bit each use. Now, I don't think anybody still cares about performance > > on 32bit (I certainly don't), so perhaps this is fine. But urgh. > > Yeah - up to you. GCC appears to do the right thing still even for > 32-bit: https://godbolt.org/z/3PWPK8E4f > > Here's "Clang returns 1": https://godbolt.org/z/KjMvEWeM5 asm volatile ("1: mov $1b, %0" : "=r" (__here)) seems to work? And then there is this form: asm volatile ("1: lea 1b, %0" : "=r" (__here)) which should also work but be one more byte. Both are shorter than CALL+POP and neither mess up the RSB. Am I missing something?