RE: [RFC PATCH 13/12] Retpoline vs. CONFIG_TRIM_UNUSED_SYMBOLS
From: Lu, Hongjiu
Date: Sun Jan 07 2018 - 10:09:55 EST
Sure, I can use __x86_indirect_thunk_rax.
H.J.
> -----Original Message-----
> From: David Woodhouse [mailto:dwmw2@xxxxxxxxxxxxx]
> Sent: Sunday, January 07, 2018 12:03 AM
> To: Andi Kleen <ak@xxxxxxxxxxxxxxx>; Van De Ven, Arjan
> <arjan.van.de.ven@xxxxxxxxx>; Lu, Hongjiu <hongjiu.lu@xxxxxxxxx>;
> Tsimbalist, Igor V <igor.v.tsimbalist@xxxxxxxxx>
> Cc: Paul Turner <pjt@xxxxxxxxxx>; LKML <linux-kernel@xxxxxxxxxxxxxxx>;
> Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>; Greg Kroah-Hartman
> <gregkh@xxxxxxxxxxxxxxxxxxxx>; Tim Chen <tim.c.chen@xxxxxxxxxxxxxxx>;
> Hansen, Dave <dave.hansen@xxxxxxxxx>; tglx@xxxxxxxxxxxxx; Kees Cook
> <keescook@xxxxxxxxxx>; Rik van Riel <riel@xxxxxxxxxx>; Peter Zijlstra
> <peterz@xxxxxxxxxxxxx>; Andy Lutomirski <luto@xxxxxxxxxxxxxx>; Jiri
> Kosina <jikos@xxxxxxxxxx>; gnomes@xxxxxxxxxxxxxxxxxxx
> Subject: Re: [RFC PATCH 13/12] Retpoline vs.
> CONFIG_TRIM_UNUSED_SYMBOLS
>
> On Sun, 2018-01-07 at 00:10 +0000, David Woodhouse wrote:
> > Arjan pointed out that CONFIG_TRIM_UNUSED_SYMBOLS *really*
> doesn't like
> > the dot in the symbols that GCC uses for the thunks.
> >
> > This seems to work, although my eyes are bleeding just a little bit.
> >
> > Given this, and the hack we already needed for MODVERSIONS, I wonder if
> > a better approach might be to export the thunks using underscores in
> > place of the dots, which is a relatively simple abuse of
> >
> __EXPORT_SYMBOL(__x86_indirect_thunk_foo,__x86.indirect_thunk.foo,),
> > and then have a hack either when generating or loading modules to do
> > the same replacement.
>
> Alternatively, and much simpler... HJ (or Igor), please can we change
> the GCC patches so that the __x86.indirect_thunk.rxx symbols don't have
> those awful dots in them? They are causing *lots* of pain.
>
> Or we could build *modules* with the inline thunk and lose the ability
> to ALTERNATIVE it away, I suppose.
>
> > diff --git a/arch/x86/lib/retpoline.S b/arch/x86/lib/retpoline.S
> > index ccb117a4588b..64d7a45ea954 100644
> > --- a/arch/x86/lib/retpoline.S
> > +++ b/arch/x86/lib/retpoline.S
> > @@ -8,7 +8,13 @@
> > Â#include
> > Â#include
> >
> > -.macro THUNK reg
> > +#ifdef CONFIG_TRIM_UNUSED_KSYMS
> > +#define EXPORT_REG(reg)
> __is_defined(__KSYM___x86_indirect_thunk_ ## reg)
> > +#else
> > +#define EXPORT_REG(reg) 1
> > +#endif
> > +
> > +.macro THUNK reg export
> > Â .section .text.__x86.indirect_thunk.\reg
> >
> > ÂENTRY(__x86.indirect_thunk.\reg)
> > @@ -16,15 +22,33 @@ ENTRY(__x86.indirect_thunk.\reg)
> > Â NOSPEC_JMP %\reg
> > Â CFI_ENDPROC
> > ÂENDPROC(__x86.indirect_thunk.\reg)
> > -EXPORT_SYMBOL(__x86.indirect_thunk.\reg)
> > +
> > +.if \export
> > + EXPORT_SYMBOL_FORCE(__x86.indirect_thunk.\reg)
> > +.endif
> > Â.endm
> >
> > -#ifdef CONFIG_64BIT
> > -.irp reg rax rbx rcx rdx rsi rdi rbp r8 r9 r10 r11 r12 r13 r14 r15
> > - THUNK \reg
> > -.endr
> > +#ifdef __KSYM_DEPS__
> > +#define GENERATE_THUNK(reg) EXPORT_SYMBOL(__x86.indirect_thunk.
> ## reg)
> > Â#else
> > -.irp reg eax ebx ecx edx esi edi ebp
> > - THUNK \reg
> > -.endr
> > +#define GENERATE_THUNK(reg) THUNK reg EXPORT_REG(reg)
> > +#endif
> > +
> > +GENERATE_THUNK(_ASM_AX)
> > +GENERATE_THUNK(_ASM_BX)
> > +GENERATE_THUNK(_ASM_CX)
> > +GENERATE_THUNK(_ASM_DX)
> > +GENERATE_THUNK(_ASM_SI)
> > +GENERATE_THUNK(_ASM_DI)
> > +GENERATE_THUNK(_ASM_BP)
> > +GENERATE_THUNK(_ASM_SP)
> > +#ifdef CONFIG_64BIT
> > +GENERATE_THUNK(r8)
> > +GENERATE_THUNK(r9)
> > +GENERATE_THUNK(r10)
> > +GENERATE_THUNK(r11)
> > +GENERATE_THUNK(r12)
> > +GENERATE_THUNK(r13)
> > +GENERATE_THUNK(r14)
> > +GENERATE_THUNK(r15)
> > Â#endif
> > diff --git a/include/asm-generic/export.h b/include/asm-generic/export.h
> > index 719db1968d81..b13bb65e2530 100644
> > --- a/include/asm-generic/export.h
> > +++ b/include/asm-generic/export.h
> > @@ -63,33 +63,33 @@ KSYM(__kcrctab_\name):
> >
> > Â#if defined(__KSYM_DEPS__)
> >
> > -#define __EXPORT_SYMBOL(sym, val, sec) === __KSYM_##sym ===
> > +#define __EXPORT_SYMBOL(sym, val, sec, force) === __KSYM_##sym
> ===
> >
> > Â#elif defined(CONFIG_TRIM_UNUSED_KSYMS)
> >
> > Â#include
> > Â#include
> >
> > -#define __EXPORT_SYMBOL(sym, val, sec) \
> > - __cond_export_sym(sym, val, sec, __is_defined(__KSYM_##sym))
> > +#define __EXPORT_SYMBOL(sym, val, sec, force) \
> > + Â__cond_export_sym(sym, val, sec, __or(force,
> __is_defined(__KSYM_##sym)))
> > Â#define __cond_export_sym(sym, val, sec, conf) \
> > Â ___cond_export_sym(sym, val, sec, conf)
> > Â#define ___cond_export_sym(sym, val, sec, enabled) \
> > Â __cond_export_sym_##enabled(sym, val, sec)
> > Â#define __cond_export_sym_1(sym, val, sec) ___EXPORT_SYMBOL sym,
> val, sec
> > Â#define __cond_export_sym_0(sym, val, sec) /* nothing */
> > -
> > Â#else
> > -#define __EXPORT_SYMBOL(sym, val, sec) ___EXPORT_SYMBOL sym, val,
> sec
> > +#define __EXPORT_SYMBOL(sym, val, sec, force) ___EXPORT_SYMBOL
> sym, val, sec
> > Â#endif
> >
> > Â#define EXPORT_SYMBOL(name) \
> > - __EXPORT_SYMBOL(name, KSYM_FUNC(KSYM(name)),)
> > + __EXPORT_SYMBOL(name, KSYM_FUNC(KSYM(name)), , 0)
> > Â#define EXPORT_SYMBOL_GPL(name)Â \
> > - __EXPORT_SYMBOL(name, KSYM_FUNC(KSYM(name)), _gpl)
> > + __EXPORT_SYMBOL(name, KSYM_FUNC(KSYM(name)), _gpl, 0)
> > Â#define EXPORT_DATA_SYMBOL(name) \
> > - __EXPORT_SYMBOL(name, KSYM(name),)
> > + __EXPORT_SYMBOL(name, KSYM(name), , 0)
> > Â#define EXPORT_DATA_SYMBOL_GPL(name)
> \
> > - __EXPORT_SYMBOL(name, KSYM(name),_gpl)
> > -
> > + __EXPORT_SYMBOL(name, KSYM(name), _gpl, 0)
> > +#define EXPORT_SYMBOL_FORCE(name) \
> > + __EXPORT_SYMBOL(name, KSYM(name), , 1)
> > Â#endif
> > diff --git a/scripts/adjust_autoksyms.sh b/scripts/adjust_autoksyms.sh
> > index 513da1a4a2da..991cd136291b 100755
> > --- a/scripts/adjust_autoksyms.sh
> > +++ b/scripts/adjust_autoksyms.sh
> > @@ -60,7 +60,7 @@ cat > "$new_ksyms_file" << EOT
> >
> > ÂEOT
> > Â[ "$(ls -A "$MODVERDIR")" ] &&
> > -sed -ns -e '3{s/ /\n/g;/^$/!p;}' "$MODVERDIR"/*.mod | sort -u |
> > +sed -ns -e '3{s/ /\n/g;/^$/!p;}' "$MODVERDIR"/*.mod | sort -u | tr . _ |
> > Âwhile read sym; do
> > Â if [ -n "$CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX" ]; then
> > Â sym="${sym#_}"