The patch titled Subject: lib/dynamic_debug.c: add asm-generic implementation for DYNAMIC_DEBUG_RELATIVE_POINTERS has been added to the -mm tree. Its filename is dynamic_debug-add-asm-generic-implementation-for-dynamic_debug_relative_pointers.patch This patch should soon appear at http://ozlabs.org/~akpm/mmots/broken-out/dynamic_debug-add-asm-generic-implementation-for-dynamic_debug_relative_pointers.patch and later at http://ozlabs.org/~akpm/mmotm/broken-out/dynamic_debug-add-asm-generic-implementation-for-dynamic_debug_relative_pointers.patch Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next and is updated there every 3-4 working days ------------------------------------------------------ From: Rasmus Villemoes <linux@xxxxxxxxxxxxxxxxxx> Subject: lib/dynamic_debug.c: add asm-generic implementation for DYNAMIC_DEBUG_RELATIVE_POINTERS A 64 bit architecture can reduce the size of the kernel image by selecting CONFIG_DYNAMIC_DEBUG_RELATIVE_POINTERS, but it must provide a proper DEFINE_DYNAMIC_DEBUG_METADATA macro for emitting the struct _ddebug in assembly. However, since that does not involve any instructions, this generic implementation should be usable by most if not all. It relies on (1) standard assembly directives that should work on all architectures (2) the "i" constraint for an constant, and (3) %cN emitting the constant operand N without punctuation and of course the layout of _ddebug being what one expects. I've succesfully built x86-64, arm64 and ppc64 defconfig + CONFIG_DYNAMIC_DEBUG + patches to hook up this header, and boot-tested the x86-64 one. Link: http://lkml.kernel.org/r/20190409212517.7321-8-linux@xxxxxxxxxxxxxxxxxx Signed-off-by: Rasmus Villemoes <linux@xxxxxxxxxxxxxxxxxx> Cc: Benjamin Herrenschmidt <benh@xxxxxxxxxxxxxxxxxxx> Cc: Catalin Marinas <catalin.marinas@xxxxxxx> Cc: Christophe Leroy <christophe.leroy@xxxxxx> Cc: David Miller <davem@xxxxxxxxxxxxx> Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> Cc: "H. Peter Anvin" <hpa@xxxxxxxxx> Cc: Ingo Molnar <mingo@xxxxxxx> Cc: Jason Baron <jbaron@xxxxxxxxxx> Cc: Michael Ellerman <mpe@xxxxxxxxxxxxxx> Cc: Paul Mackerras <paulus@xxxxxxxxx> Cc: Petr Mladek <pmladek@xxxxxxxx> Cc: Sergey Senozhatsky <sergey.senozhatsky@xxxxxxxxx> Cc: Steven Rostedt <rostedt@xxxxxxxxxxx> Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx> Cc: Will Deacon <will.deacon@xxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- include/asm-generic/dynamic_debug.h | 116 ++++++++++++++++++++++++++ include/linux/jump_label.h | 2 2 files changed, 118 insertions(+) --- /dev/null +++ a/include/asm-generic/dynamic_debug.h @@ -0,0 +1,116 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_GENERIC_DYNAMIC_DEBUG_H +#define _ASM_GENERIC_DYNAMIC_DEBUG_H + +#ifndef _DYNAMIC_DEBUG_H +#error "don't include asm/dynamic_debug.h directly" +#endif + +#include <linux/build_bug.h> +#ifdef CONFIG_JUMP_LABEL +#include <linux/jump_label.h> +#endif + +/* + * We need to know the exact layout of struct _ddebug in order to + * initialize it in assembly. Check that all members are at expected + * offsets - if any of these fail, the arch cannot use this generic + * dynamic_debug.h. DYNAMIC_DEBUG_RELATIVE_POINTERS is pointless for + * !64BIT, so we expect the static_key to be at an 8-byte boundary + * since it contains stuff which is long-aligned. + */ + +static_assert(BITS_PER_LONG == 64); +static_assert(offsetof(struct _ddebug, modname_disp) == 0); +static_assert(offsetof(struct _ddebug, function_disp) == 4); +static_assert(offsetof(struct _ddebug, filename_disp) == 8); +static_assert(offsetof(struct _ddebug, format_disp) == 12); +static_assert(offsetof(struct _ddebug, flags_lineno) == 16); + +#ifdef CONFIG_JUMP_LABEL +static_assert(offsetof(struct _ddebug, key) == 24); +static_assert(offsetof(struct static_key, enabled) == 0); +static_assert(offsetof(struct static_key, type) == 8); + +/* <4 bytes padding>, .enabled, <4 bytes padding>, .type */ +# ifdef DEBUG +# define _DPRINTK_ASM_KEY_INIT \ + "\t.int 0\n" "\t.int 1\n" "\t.int 0\n" "\t.quad "__stringify(__JUMP_TYPE_TRUE)"\n" +# else +# define _DPRINTK_ASM_KEY_INIT \ + "\t.int 0\n" "\t.int 0\n" "\t.int 0\n" "\t.quad "__stringify(__JUMP_TYPE_FALSE)"\n" +# endif +#else /* !CONFIG_JUMP_LABEL */ +#define _DPRINTK_ASM_KEY_INIT "" +#endif + +/* + * There's a bit of magic involved here. + * + * First, unlike the bug table entries, we need to define an object in + * assembly which we can reference from C code (for use by the + * DYNAMIC_DEBUG_BRANCH macro), but we don't want 'name' to have + * external linkage (that would require use of globally unique + * identifiers, which we can't guarantee). Fortunately, the "extern" + * keyword just tells the compiler that _somebody_ provides that + * symbol - usually that somebody is the linker, but in this case it's + * the assembler, and since we do not do .globl name, the symbol gets + * internal linkage. + * + * So far so good. The next problem is that there's no scope in + * assembly, so the identifier 'name' has to be unique within each + * translation unit - otherwise all uses of that identifier end up + * referring to the same struct _ddebug instance. pr_debug and friends + * do this by use of indirection and __UNIQUE_ID(), and new users of + * DEFINE_DYNAMIC_DEBUG_METADATA() should do something similar. We + * need to catch cases where this is not done at build time. + * + * With assembly-level .ifndef we can ensure that we only define a + * given identifier once, preventing "symbol 'foo' already defined" + * errors. But we still need to detect and fail on multiple uses of + * the same identifer. The simplest, and wrong, solution to that is to + * add an .else .error branch to the .ifndef. The problem is that just + * because the DEFINE_DYNAMIC_DEBUG_METADATA() macro is only expanded + * once with a given identifier, the compiler may emit the assembly + * code multiple times, e.g. if the macro appears in an inline + * function. Now, in a normal case like + * + * static inline get_next_id(void) { static int v; return ++v; } + * + * all inlined copies of get_next_id are _supposed_ to refer to the + * same object 'v'. So we do need to allow this chunk of assembly to + * appear multiple times with the same 'name', as long as they all + * came from the same DEFINE_DYNAMIC_DEBUG_METADATA() instance. To do + * that, we pass __COUNTER__ to the asm(), and set an assembler symbol + * name.ddebug.once to that value when we first define 'name'. When we + * meet a second attempt at defining 'name', we compare + * name.ddebug.once to %6 and error out if they are different. + */ + +#define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) \ + extern struct _ddebug name; \ + asm volatile(".ifndef " __stringify(name) "\n" \ + ".pushsection __verbose,\"aw\"\n" \ + ".type "__stringify(name)", STT_OBJECT\n" \ + ".size "__stringify(name)", %c5\n" \ + "1:\n" \ + __stringify(name) ":\n" \ + "\t.int %c0 - 1b /* _ddebug::modname_disp */\n" \ + "\t.int %c1 - 1b /* _ddebug::function_disp */\n" \ + "\t.int %c2 - 1b /* _ddebug::filename_disp */\n" \ + "\t.int %c3 - 1b /* _ddebug::format_disp */\n" \ + "\t.int %c4 /* _ddebug::flags_lineno */\n" \ + _DPRINTK_ASM_KEY_INIT \ + "\t.org 1b+%c5\n" \ + ".popsection\n" \ + ".set "__stringify(name)".ddebug.once, %c6\n" \ + ".elseif "__stringify(name)".ddebug.once - %c6\n" \ + ".line "__stringify(__LINE__) " - 1\n" \ + ".error \"'"__stringify(name)"' used as _ddebug identifier more than once\"\n" \ + ".endif\n" \ + : : "i" (KBUILD_MODNAME), "i" (__func__), \ + "i" (__FILE__), "i" (fmt), \ + "i" (_DPRINTK_FLAGS_LINENO_INIT), \ + "i" (sizeof(struct _ddebug)), "i" (__COUNTER__)) + +#endif /* _ASM_GENERIC_DYNAMIC_DEBUG_H */ --- a/include/linux/jump_label.h~dynamic_debug-add-asm-generic-implementation-for-dynamic_debug_relative_pointers +++ a/include/linux/jump_label.h @@ -190,6 +190,8 @@ struct module; #ifdef CONFIG_JUMP_LABEL +#define __JUMP_TYPE_FALSE 0 +#define __JUMP_TYPE_TRUE 1 #define JUMP_TYPE_FALSE 0UL #define JUMP_TYPE_TRUE 1UL #define JUMP_TYPE_LINKED 2UL _ Patches currently in -mm which might be from linux@xxxxxxxxxxxxxxxxxx are linux-deviceh-use-unique-identifier-for-each-struct-_ddebug.patch linux-neth-use-unique-identifier-for-each-struct-_ddebug.patch linux-printkh-use-unique-identifier-for-each-struct-_ddebug.patch dynamic_debug-introduce-accessors-for-string-members-of-struct-_ddebug.patch dynamic_debug-drop-use-of-bitfields-in-struct-_ddebug.patch dynamic_debug-introduce-config_dynamic_debug_relative_pointers.patch dynamic_debug-add-asm-generic-implementation-for-dynamic_debug_relative_pointers.patch x86-64-select-dynamic_debug_relative_pointers.patch arm64-select-dynamic_debug_relative_pointers.patch powerpc-select-dynamic_debug_relative_pointers-for-ppc64.patch lib-bitmapc-remove-unused-export_symbols.patch lib-bitmapc-guard-exotic-bitmap-functions-by-config_numa.patch bitopsh-sanitize-rotate-primitives.patch