[PATCH v3 23/23] x86_64: use relative pointers with dynamic debug

From: Rasmus Villemoes
Date: Fri Nov 09 2018 - 18:11:34 EST


Similar to how x86_64 uses bug_entry-relative pointers to reduce
sizeof(struct bug_entry), the same thing can now be done for struct
_ddebug, saving 16 bytes for each of those (i.e., each pr_debug, dev_dbg
etc. in a CONFIG_DYNAMIC_DEBUG kernel).

Note the use of .ifndef/.endif in asm to avoid

fs/aio.c:1382: Error: symbol `__UNIQUE_ID_ddebug112' is already defined

This is due to uses of pr_debug et al in functions that get inlined. In
such a case, __COUNTER__ is obviously only expanded once, but the asm()
is repeated once for every inlined instance. Letting all instances share
the same descriptor object is the right thing to do; that is also what
happens when it is the compiler that defines a static object inside an
inline(d) function.

That, however, leads to another problem if the same identifier is used
in two different DEFINE_DYNAMIC_DEBUG_METADATA() in the same translation
unit: They would both end up referring to the same (first occuring)
instance. So the second one would end up using the wrong file, line,
function etc. information. All current in-tree macro users of
DEFINE_DYNAMIC_DEBUG_METADATA() have been updated to ensure they use
UNIQUE_ID to generate the identifier.

To prevent such a problem from going unnoticed, we use a guard symbol in
assembly whose value is unique per DEFINE_DYNAMIC_DEBUG_METADATA
invocation.

Cc: x86@xxxxxxxxxx
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: Jason Baron <jbaron@xxxxxxxxxx>
Signed-off-by: Rasmus Villemoes <linux@xxxxxxxxxxxxxxxxxx>
---
arch/x86/Kconfig | 1 +
arch/x86/include/asm/dynamic_debug.h | 58 ++++++++++++++++++++++++++++
arch/x86/kernel/macros.S | 1 +
3 files changed, 60 insertions(+)
create mode 100644 arch/x86/include/asm/dynamic_debug.h

diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index ba7e3464ee92..c7cb31f39e5a 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -91,6 +91,7 @@ config X86
select CLOCKSOURCE_WATCHDOG
select DCACHE_WORD_ACCESS
select DMA_DIRECT_OPS
+ select DYNAMIC_DEBUG_RELATIVE_POINTERS if X86_64
select EDAC_ATOMIC_SCRUB
select EDAC_SUPPORT
select GENERIC_CLOCKEVENTS
diff --git a/arch/x86/include/asm/dynamic_debug.h b/arch/x86/include/asm/dynamic_debug.h
new file mode 100644
index 000000000000..80730409183f
--- /dev/null
+++ b/arch/x86/include/asm/dynamic_debug.h
@@ -0,0 +1,58 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_X86_DYNAMIC_DEBUG_H
+#define _ASM_X86_DYNAMIC_DEBUG_H
+
+#ifndef __ASSEMBLY__
+
+#ifdef DEBUG
+#define _DPRINTK_INIT_TRUE 1
+#else
+#define _DPRINTK_INIT_TRUE 0
+#endif
+
+#define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \
+ extern struct _ddebug name; \
+ asm volatile("DYNAMIC_DEBUG_METADATA" \
+ " ident="__stringify(name) \
+ " modname=%c0" \
+ " func=%c1" \
+ " file=%c2" \
+ " fmt=%c3" \
+ " flag_line=%c4" \
+ " init_true=%c5" \
+ " cnt=%c6" \
+ : : "i" (KBUILD_MODNAME), "i" (__func__), \
+ "i" (__FILE__), "i" (fmt), \
+ "i" (_DPRINTK_FLAGS_LINENO_INIT), \
+ "i" (_DPRINTK_INIT_TRUE), \
+ "i" (__COUNTER__) \
+ )
+
+#else /* __ASSEMBLY__ */
+
+.macro DYNAMIC_DEBUG_METADATA ident modname func file fmt flag_line init_true cnt
+.ifndef \ident
+ .pushsection __verbose,"aw"
+\ident :
+ .long \modname - \ident
+ .long \func - \ident
+ .long \file - \ident
+ .long \fmt - \ident
+ .long \flag_line
+ .long 0
+ .if \init_true
+ STATIC_KEY_INIT_TRUE
+ .else
+ STATIC_KEY_INIT_FALSE
+ .endif
+ .popsection
+ .set \ident\().ddebug.once, \cnt
+.elseif \ident\().ddebug.once - \cnt
+ .error "'\ident' used as _ddebug identifier more than once"
+.endif
+.endm
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* _ASM_X86_DYNAMIC_DEBUG_H */
+
diff --git a/arch/x86/kernel/macros.S b/arch/x86/kernel/macros.S
index 161c95059044..2c2b8e5229a9 100644
--- a/arch/x86/kernel/macros.S
+++ b/arch/x86/kernel/macros.S
@@ -14,3 +14,4 @@
#include <asm/asm.h>
#include <asm/cpufeature.h>
#include <asm/jump_label.h>
+#include <asm/dynamic_debug.h>
--
2.19.1.6.gbde171bbf5