From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (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 66BF62D7DF2 for ; Tue, 25 Nov 2025 15:17:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764083870; cv=none; b=aB87nfa3/HtA32OJLQ6tG1bNo5njKom7ev2XGrtE3cDHDBOaPf6JByS6D2ypRuJUIfvYLI329Ezg80BF7mEYyGUJcakvULq2B0yaaU3Y9mY4KyhqC9ATj+yGr+YHkCMbV9QFxaGcjVPL6SsVmHVrl/X8lu92kQ+g0qAh/yM09L4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764083870; c=relaxed/simple; bh=U3JYq8lsYBjBt5EY1dD6uJaGVH+Majz0vUDZ7X0Mcc0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=FkAxa07q3PjZEDk1tGI8okwvLXefuaJCpVf5BA1I/2jOCrZNhic1pxB0XaSF3UsTP/Fv5IpC/U6JSzF8d1XrRbt6HsM+A4GEKhTCZKT07bsPyPhj2Fnf0CJ1waj07N2Vr3ZMBEb8hmQyLbel+AAqZb6Rcl/UhospgHOh7g06X10= 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=Ok4yFb6l; arc=none smtp.client-ip=90.155.50.34 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="Ok4yFb6l" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; 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=rYD736qbB5uFQ4cTGdCNcBhD9vreTYCtxO2fk9Isq6M=; b=Ok4yFb6ly8ykHl/IIUKXQ18eTs ByhFOBpLh9JYwJNBgrKh+zQpp+eOJWF9laKXjmbP7jBcC6ikL/EAyFE6UjWJcGCgbuEwybDKzXMM0 VSvLcnyiOGkSN6/TYVJ3jB0ytmTjEhKMhKdNGeioVMqA3T4hUn7VrLFWRg8dYfsbT+ZhlL50eFRIH asouOBM9nAJTPSBBk+k4KztT3wC0oueEsOeb1R02J8Xng0wki4rV9xSl7g4e9igWZ8Xye/OTcMiNj btCOU8cPOWq06aVvlg18jccManr+/QAMAwXIk2nkZAUa0SZ7QsjVXwJzgXi1DZBmYavmmVh4ZV9cI LprOBw1A==; Received: from 77-249-17-252.cable.dynamic.v4.ziggo.nl ([77.249.17.252] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.98.2 #2 (Red Hat Linux)) id 1vNunU-00000008ksd-3EU8; Tue, 25 Nov 2025 15:17:40 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id 47C5B3002A6; Tue, 25 Nov 2025 16:17:39 +0100 (CET) Date: Tue, 25 Nov 2025 16:17:39 +0100 From: Peter Zijlstra To: x86@kernel.org, ardb@kernel.org Cc: linux-kernel@vger.kernel.org, kees@kernel.org, acarmina@redhat.com, jpoimboe@kernel.org, mark.rutland@arm.com, torvalds@linuxfoundation.org, maciej.wieczor-retman@intel.com Subject: Re: [PATCH v2 08/12] x86/bug: Add BUG_FORMAT basics Message-ID: <20251125151739.GP4068168@noisy.programming.kicks-ass.net> References: <20251110114633.202485143@infradead.org> <20251110115757.980264454@infradead.org> <20251125111750.GS4067720@noisy.programming.kicks-ass.net> <20251125123301.GO4068168@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: <20251125123301.GO4068168@noisy.programming.kicks-ass.net> On Tue, Nov 25, 2025 at 01:33:01PM +0100, Peter Zijlstra wrote: > > > + asm_inline volatile(_BUG_FLAGS_ASM(ins, "%c[fmt]", "%c[file]", \ > > > + "%c[line]", "%c[fl]", \ > > > + "%c[size]", extra) \ > > > + : : [fmt] "i" (NULL), \ > > > > This doesn't work right with KASLR on -- and I hadn't noticed because > > most of my machines have nokaslr because of debugability :/ > > > > When we relocate the kernel, everything shifts by kaslr_offset(), and > > that works just fine when both the __bug_table and the target string is > > shifted, because then the relative position is the same and so the > > relocation keeps working. > > > > However, when the target is the absolute value 0, this breaks, because 0 > > isn't shifted by kaslr_offset() but the __bug_table itself is. > > > > So the relative entry: > > > > .long 0 - . > > > > and its inverse: > > > > format = (const char *)&bug->format_disp + bug->format_disp; > > > > then end up at kaslr_offset() and things are sad. > > > > The relative entry has a SHN_UNDEF relocation, which is ignored by the > > relocs tool. > > > > How is this supposed to be fixed? > > This seems to work. Is this something we can all live with? It feels a > bit like a hack, but there doesn't appear to be anything better at hand. Ard came up with this glorious hack :-) --- diff --git a/arch/x86/include/asm/bug.h b/arch/x86/include/asm/bug.h index d0a96ff5c02c..812ec8932734 100644 --- a/arch/x86/include/asm/bug.h +++ b/arch/x86/include/asm/bug.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #ifndef __ASSEMBLY__ @@ -59,7 +60,7 @@ extern void __WARN_trap(struct bug_entry *bug, ...); #if defined(CONFIG_X86_64) || defined(CONFIG_DEBUG_BUGVERBOSE_DETAILED) #define HAVE_ARCH_BUG_FORMAT #define __BUG_ENTRY_FORMAT(format) \ - "\t" __BUG_REL(format) "\t# bug_entry::format\n" + "3:\t" __BUG_REL(format) "\t# bug_entry::format\n" #else #define __BUG_ENTRY_FORMAT(format) #endif @@ -84,18 +85,20 @@ extern void __WARN_trap(struct bug_entry *bug, ...); extra #ifdef CONFIG_DEBUG_BUGVERBOSE_DETAILED -#define WARN_CONDITION_STR(cond_str) cond_str +#define WARN_CONDITION_STR(cond_str, null_str) cond_str #else -#define WARN_CONDITION_STR(cond_str) NULL +#define WARN_CONDITION_STR(cond_str, null_str) null_str #endif -#define _BUG_FLAGS(cond_str, ins, flags, extra) \ +#define _BUG_FLAGS(cond_str, ins, flags, extra, id) \ do { \ + extern typeof(cond_str) id; \ asm_inline volatile("1:\t" ins "\n" \ _BUG_FLAGS_ASM("%c[fmt]", "%c[file]", \ "%c[line]", "%c[fl]", \ "%c[size]", extra) \ - : : [fmt] "i" (WARN_CONDITION_STR(cond_str)), \ + ".set " __stringify(id) ", 3b\n" \ + : : [fmt] "i" (WARN_CONDITION_STR(cond_str, id)), \ [file] "i" (__FILE__), \ [line] "i" (__LINE__), \ [fl] "i" (flags), \ @@ -104,11 +107,11 @@ do { \ #define ARCH_WARN_ASM(file, line, flags, size) \ "1:\t " ASM_UD2 "\n" \ - _BUG_FLAGS_ASM("0", file, line, flags, size, "") + _BUG_FLAGS_ASM("3b", file, line, flags, size, "") #else -#define _BUG_FLAGS(cond_str, ins, flags, extra) asm volatile(ins) +#define _BUG_FLAGS(cond_str, ins, flags, extra, id) asm volatile(ins) #endif /* CONFIG_GENERIC_BUG */ @@ -116,7 +119,7 @@ do { \ #define BUG() \ do { \ instrumentation_begin(); \ - _BUG_FLAGS("", ASM_UD2, 0, ""); \ + _BUG_FLAGS("", ASM_UD2, 0, "", __UNIQUE_ID(bug)); \ __builtin_unreachable(); \ } while (0) @@ -133,7 +136,8 @@ do { \ do { \ __auto_type __flags = BUGFLAG_WARNING|(flags); \ instrumentation_begin(); \ - _BUG_FLAGS(cond_str, ASM_UD2, __flags, ARCH_WARN_REACHABLE); \ + _BUG_FLAGS(cond_str, ASM_UD2, __flags, ARCH_WARN_REACHABLE, \ + __UNIQUE_ID(warn)); \ instrumentation_end(); \ } while (0) diff --git a/lib/bug.c b/lib/bug.c index 581a66b88c5c..9a598b7dfc11 100644 --- a/lib/bug.c +++ b/lib/bug.c @@ -144,7 +145,8 @@ static const char *bug_get_format(struct bug_entry *bug) const char *format = NULL; #ifdef HAVE_ARCH_BUG_FORMAT #ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS - format = (const char *)&bug->format_disp + bug->format_disp; + if (bug->format_disp) + format = (const char *)&bug->format_disp + bug->format_disp; #else format = bug->format; #endif