Introduce the initify gcc plugin
Subsystems (12)
cover Introduce the initify gcc plugin
I would like to introduce the initify gcc plugin. The kernel already has a mechanism to free up code and data memory that is only used during kernel or module initialization.
This plugin will teach the compiler to find more such code and data that can be freed after initialization. It reduces memory usage.
The initify gcc plugin can be useful for embedded systems.
It is a CII project supported by the Linux Foundation.
This plugin is the part of grsecurity/PaX.
The plugin supports all gcc versions from 4.5 to 6.0.
I made some changes on top of the PaX version (since March 6.). These are the important ones:
* move all local strings to init.rodata.str and exit.rodata.str (not just __func__)
* report all initified strings and functions (GCC_PLUGIN_INITIFY_VERBOSE config option)
* automatically discover init/exit functions and apply the __init or __exit attributes on them
You can find more about the changes here:
https://github.com/ephox-gcc-plugins/initify
This patch set is based on the "Add support for complex gcc plugins that don't fit in a single file" patch set (git/kees/linux.git#kspp HEAD:e5d4798b284cd192c8b).
Some statistics about the plugin:
On allyes config (amd64, gcc-6):
* 7731 initified strings
* 231 initified functions
On allmod config (i386, gcc-6):
* 8846 initified strings
* 252 initified functions
On allyes config (amd64, gcc-6):
section vanilla vanilla + initify change
-----------------------------------------------------------------------
.rodata 39059688 (0x25400e8) 38527210 (0x24be0ea) -532478 .data 45744128 (0x2ba0000) 45404160 (0x2b4d000) -339968 .init.data 1361144 (0x14c4f8) 1674200 (0x198bd8) +313056
.text 77615128 (0x4a05018) 77576664 (0x49fb9d8) -38464
.init.text 1108455 (0x10e9e7) 1137618 (0x115bd2) +29163
Emese Revfy (2):
Add the initify gcc plugin
Mark functions with the __nocapture attribute
--- arch/Kconfig | 23 + arch/arm/include/asm/string.h | 10 +- arch/arm64/include/asm/string.h | 23 +- arch/powerpc/include/asm/string.h | 19 +- arch/x86/boot/string.h | 4 +- arch/x86/include/asm/string_32.h | 21 +- arch/x86/include/asm/string_64.h | 18 +- arch/x86/kernel/hpet.c | 2 +- include/asm-generic/bug.h | 6 +- include/asm-generic/vmlinux.lds.h | 2 + include/linux/compiler-gcc.h | 10 +- include/linux/compiler.h | 4 + include/linux/fs.h | 5 +- include/linux/printk.h | 2 +- include/linux/string.h | 73 +-- scripts/Makefile.gcc-plugins | 4 + scripts/gcc-plugins/initify_plugin.c | 1147 ++++++++++++++++++++++++++++++++++ 17 files changed, 1283 insertions(+), 90 deletions(-)
PATCH 1/2 Add the initify gcc plugin
The kernel already has a mechanism to free up code and data memory that is only used during kernel or module initialization.
This plugin will teach the compiler to find more such code and data that can be freed after initialization.
It has two passes. The first one tries to find all functions that can be become __init/__exit. The second one moves string constants (local variables and function string arguments marked by
the nocapture attribute) only referenced in __init/__exit functions to the __initconst/__exitconst sections.
It reduces memory usage. This plugin can be useful for embedded systems.
The instrumentation pass of the latent_entropy plugin must run after the initify plugin to increase coverage.
--- arch/Kconfig | 23 + include/asm-generic/vmlinux.lds.h | 2 + scripts/Makefile.gcc-plugins | 4 + scripts/gcc-plugins/initify_plugin.c | 1147 ++++++++++++++++++++++++++++++++++ 4 files changed, 1176 insertions(+) create mode 100644 scripts/gcc-plugins/initify_plugin.c
arch/Kconfig +23 -0
@@ -417,6 +417,29 @@ config GCC_PLUGIN_LATENT_ENTROPY * https://grsecurity.net/ * https://pax.grsecurity.net/ +config GCC_PLUGIN_INITIFY + bool "Free more kernel memory after init" + depends on GCC_PLUGINS + help + The kernel has a mechanism to free up code and data memory that is + only used during kernel or module initialization. Enabling this + feature will teach the compiler to find more such code and data + that can be freed after initialization. + + This plugin is the part of grsecurity/PaX. More information at: + * https://grsecurity.net/ + * https://pax.grsecurity.net/ + +config GCC_PLUGIN_INITIFY_VERBOSE + bool "Verbose" + depends on GCC_PLUGIN_INITIFY + help + Print all initified strings and all functions which should be + __init/__exit. + Note that the candidates identified for __init/__exit markings + depend on the current kernel configuration and thus should be verified + manually before the source code is patched. + config HAVE_CC_STACKPROTECTOR bool help
include/asm-generic/vmlinux.lds.h +2 -0
@@ -523,6 +523,7 @@ MEM_DISCARD(init.data) \ KERNEL_CTORS() \MCOUNT_REC () \ + *(.init.rodata.str) \ *(.init.rodata) \ FTRACE_EVENTS() \TRACE_SYSCALLS () \
@@ -547,6 +548,7 @@#define EXIT_DATA \ *(.exit.data) \ MEM_DISCARD(exit.data) \ + *(.exit.rodata.str) \ MEM_DISCARD(exit.rodata)#define EXIT_TEXT \
scripts/Makefile.gcc-plugins +4 -0
@@ -12,6 +12,10 @@ ifdef CONFIG_GCC_PLUGINS DISABLE_LATENT_ENTROPY_PLUGIN += -fplugin-arg-latent_entropy_plugin-disable endif + gcc-plugin-$(CONFIG_GCC_PLUGIN_INITIFY) += initify_plugin.so + gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_INITIFY) += -DINITIFY_PLUGIN -fplugin-arg-initify_plugin-search_init_exit_functions + gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_INITIFY_VERBOSE)+= -fplugin-arg-initify_plugin-verbose + ifdef CONFIG_GCC_PLUGIN_SANCOV ifeq ($(CFLAGS_KCOV),) # It is needed because of the gcc-plugin.sh and gcc version checks.
scripts/gcc-plugins/initify_plugin.c new file +1147 -0
1,147 lines not on this page · raw message
Signature · 1 line
2.8.1
Re: [PATCH v1 1/2] Add the initify gcc plugin
Quoted 10 lines · Emese Revfy
The kernel already has a mechanism to free up code and data memory that is only used during kernel or module initialization.
This plugin will teach the compiler to find more such code and data that can be freed after initialization.
It has two passes. The first one tries to find all functions that can be become __init/__exit. The second one moves string constants (local variables and function string arguments marked by
the nocapture attribute) only referenced in __init/__exit functions to the __initconst/__exitconst sections.
It reduces memory usage. This plugin can be useful for embedded systems.
May I suggest, as a followup patch, a debug option/plugin parameter to put the strings in a section which will not be reaped after init, but just marked inaccessible, with graceful handling of bad accesses (print a big fat warning, make the page(s) readable, continue)?
Rasmus
Re: [PATCH v1 1/2] Add the initify gcc plugin
Quoted 13 lines · Rasmus Villemoes
Quoted 10 lines · Emese Revfy
The kernel already has a mechanism to free up code and data memory that is only used during kernel or module initialization.
This plugin will teach the compiler to find more such code and data that can be freed after initialization.
It has two passes. The first one tries to find all functions that can be become __init/__exit. The second one moves string constants (local variables and function string arguments marked by
the nocapture attribute) only referenced in __init/__exit functions to the __initconst/__exitconst sections.
It reduces memory usage. This plugin can be useful for embedded systems.
Quoted 4 lines
May I suggest, as a followup patch, a debug option/plugin parameter to put the strings in a section which will not be reaped after init, but just marked inaccessible, with graceful handling of bad accesses (print a big fat warning, make the page(s) readable, continue)?
Is there a clean way to do this that isn't arch-specific?
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [PATCH v1 1/2] Add the initify gcc plugin
In reply to Rasmus VillemoesQuoted 13 lines · On Tue, 28 Jun 2016 23:05:56 +0200 Rasmus Villemoes <[email protected]>…
Quoted 10 lines · Emese Revfy
The kernel already has a mechanism to free up code and data memory that is only used during kernel or module initialization.
This plugin will teach the compiler to find more such code and data that can be freed after initialization.
It has two passes. The first one tries to find all functions that can be become __init/__exit. The second one moves string constants (local variables and function string arguments marked by
the nocapture attribute) only referenced in __init/__exit functions to the __initconst/__exitconst sections.
It reduces memory usage. This plugin can be useful for embedded systems.
Quoted 4 lines
May I suggest, as a followup patch, a debug option/plugin parameter to put the strings in a section which will not be reaped after init, but just marked inaccessible, with graceful handling of bad accesses (print a big fat warning, make the page(s) readable, continue)?
I think even better would be to verify the whole init section. Unfortunately, I won't implement it anytime soon because my project ends this week.
Signature · 1 line
Emese
PATCH 2/2 Mark functions with the __nocapture attribute
In reply to Emese RevfyThe nocapture gcc attribute can be on functions only.
The attribute takes one or more unsigned integer constants as parameters that specify the function argument(s) of const char* type to initify. If the marked argument is a vararg then the plugin initifies all vararg arguments.
I couldn't test the arm, arm64 and powerpc architectures.
--- arch/arm/include/asm/string.h | 10 +++--- arch/arm64/include/asm/string.h | 23 ++++++------ arch/powerpc/include/asm/string.h | 19 +++++----- arch/x86/boot/string.h | 4 +-- arch/x86/include/asm/string_32.h | 21 +++++------ arch/x86/include/asm/string_64.h | 18 +++++----- arch/x86/kernel/hpet.c | 2 +- include/asm-generic/bug.h | 6 ++-- include/linux/compiler-gcc.h | 10 ++++-- include/linux/compiler.h | 4 +++ include/linux/fs.h | 5 +-- include/linux/printk.h | 2 +- include/linux/string.h | 73 ++++++++++++++++++++------------------- 13 files changed, 107 insertions(+), 90 deletions(-)
arch/arm/include/asm/string.h +5 -5
@@ -7,19 +7,19 @@*/ #define __HAVE_ARCH_STRRCHR -extern char *strrchr (const char * s,int c); +extern char *strrchr (const char * s,int c)__nocapture (1 );#define __HAVE_ARCH_STRCHR -extern char *strchr (const char * s,int c); +extern char *strchr (const char * s,int c)__nocapture (1 );#define __HAVE_ARCH_MEMCPY -extern void *memcpy (void *,const void *,__kernel_size_t ); +extern void *memcpy (void *,const void *,__kernel_size_t )__nocapture (2 );#define __HAVE_ARCH_MEMMOVE -extern void *memmove (void *,const void *,__kernel_size_t ); +extern void *memmove (void *,const void *,__kernel_size_t )__nocapture (2 );#define __HAVE_ARCH_MEMCHR -extern void *memchr (const void *,int ,__kernel_size_t ); +extern void *memchr (const void *,int ,__kernel_size_t )__nocapture (1 );#define __HAVE_ARCH_MEMSETextern void *memset (void *,int ,__kernel_size_t );
arch/arm64/include/asm/string.h +12 -11
@@ -17,40 +17,41 @@#define __ASM_STRING_H#define __HAVE_ARCH_STRRCHR -extern char *strrchr (const char *,int c); +extern char *strrchr (const char *,int c)__nocapture (1 );#define __HAVE_ARCH_STRCHR -extern char *strchr (const char *,int c); +extern char *strchr (const char *,int c)__nocapture (1 );#define __HAVE_ARCH_STRCMP -extern int strcmp (const char *,const char *); +extern int strcmp(const char *, const char *) __nocapture(1, 2);#define __HAVE_ARCH_STRNCMP -extern int strncmp (const char *,const char *,__kernel_size_t ); +extern int +strncmp(const char *, const char *, __kernel_size_t) __nocapture(1, 2);#define __HAVE_ARCH_STRLENextern __kernel_size_t strlen (const char *);#define __HAVE_ARCH_STRNLEN -extern __kernel_size_t strnlen (const char *,__kernel_size_t ); +extern __kernel_size_t strnlen(const char *, __kernel_size_t) __nocapture(1);#define __HAVE_ARCH_MEMCPY -extern void *memcpy (void *,const void *,__kernel_size_t ); -extern void *__memcpy (void *,const void *,__kernel_size_t ); +extern void *memcpy (void *,const void *,__kernel_size_t )__nocapture (2 ); +extern void *__memcpy (void *,const void *,__kernel_size_t )__nocapture (2 );#define __HAVE_ARCH_MEMMOVE -extern void *memmove (void *,const void *,__kernel_size_t ); -extern void *__memmove (void *,const void *,__kernel_size_t ); +extern void *memmove (void *,const void *,__kernel_size_t )__nocapture (2 ); +extern void *__memmove (void *,const void *,__kernel_size_t )__nocapture (2 );#define __HAVE_ARCH_MEMCHR -extern void *memchr (const void *,int ,__kernel_size_t ); +extern void *memchr (const void *,int ,__kernel_size_t )__nocapture (1 );#define __HAVE_ARCH_MEMSETextern void *memset (void *,int ,__kernel_size_t );extern void *__memset (void *,int ,__kernel_size_t );#define __HAVE_ARCH_MEMCMP -extern int memcmp (const void *,const void *,size_t ); +extern int memcmp(const void *, const void *, size_t) __nocapture(1, 2);#if defined(CONFIG_KASAN ) && !defined(__SANITIZE_ADDRESS__)
arch/powerpc/include/asm/string.h +10 -9
@@ -15,17 +15,18 @@#define __HAVE_ARCH_MEMCMP#define __HAVE_ARCH_MEMCHR -extern char *strcpy (char *,const char *); -extern char *strncpy (char *,const char *,__kernel_size_t ); +extern char *strcpy (char *,const char *)__nocapture (2 ); +extern char *strncpy (char *,const char *,__kernel_size_t )__nocapture (2 );extern __kernel_size_t strlen (const char *); -extern int strcmp (const char *,const char *); -extern int strncmp (const char *,const char *,__kernel_size_t ); -extern char *strcat (char *,const char *); +extern int strcmp(const char *,const char *) __nocapture(1, 2); +extern int +strncmp(const char *, const char *, __kernel_size_t) __nocapture(1, 2); +extern char *strcat (char *,const char *)__nocapture (2 );extern void *memset (void *,int ,__kernel_size_t ); -extern void *memcpy (void *,const void *,__kernel_size_t ); -extern void *memmove (void *,const void *,__kernel_size_t ); -extern int memcmp (const void *,const void *,__kernel_size_t ); -extern void *memchr (const void *,int ,__kernel_size_t ); +extern void *memcpy (void *,const void *,__kernel_size_t )__nocapture (2 ); +extern void *memmove (void *,const void *,__kernel_size_t )__nocapture (2 ); +extern int memcmp(const void *,const void *,__kernel_size_t) __nocapture(1, 2); +extern void *memchr (const void *,int ,__kernel_size_t )__nocapture (1 );#endif /* __KERNEL__ */
arch/x86/boot/string.h +2 -2
@@ -6,9 +6,9 @@#undef memset#undef memcmp -void *memcpy (void *dst,const void *src,size_t len); +void *memcpy (void *dst,const void *src,size_t len)__nocapture (2 );void *memset (void *dst,int c,size_t len); -int memcmp (const void *s1,const void *s2,size_t len); +int memcmp(const void *s1, const void *s2, size_t len) __nocapture(1, 2);/* * Access builtin version by default. If one needs to use optimized version,
arch/x86/include/asm/string_32.h +11 -10
@@ -6,25 +6,26 @@/* Let gcc decide whether to inline or use the out of line functions */ #define __HAVE_ARCH_STRCPY -extern char *strcpy (char *dest,const char *src); +extern char *strcpy (char *dest,const char *src)__nocapture (2 );#define __HAVE_ARCH_STRNCPY -extern char *strncpy (char *dest,const char *src,size_t count); +extern char *strncpy (char *dest,const char *src,size_t count)__nocapture (2 );#define __HAVE_ARCH_STRCAT -extern char *strcat (char *dest,const char *src); +extern char *strcat (char *dest,const char *src)__nocapture (2 );#define __HAVE_ARCH_STRNCAT -extern char *strncat (char *dest,const char *src,size_t count); +extern char *strncat (char *dest,const char *src,size_t count)__nocapture (2 );#define __HAVE_ARCH_STRCMP -extern int strcmp (const char *cs,const char *ct); +extern int strcmp(const char *cs, const char *ct) __nocapture(1, 2);#define __HAVE_ARCH_STRNCMP -extern int strncmp (const char *cs,const char *ct,size_t count); +extern int +strncmp(const char *cs, const char *ct, size_t count) __nocapture(1, 2);#define __HAVE_ARCH_STRCHR -extern char *strchr (const char *s,int c); +extern char *strchr (const char *s,int c)__nocapture (1 );#define __HAVE_ARCH_STRLENextern size_t strlen (const char *s);
@@ -197,12 +198,12 @@ static inline void *__memcpy3d(void *to, const void *from, size_t len)#endif #define __HAVE_ARCH_MEMMOVE -void *memmove (void *dest,const void *src,size_t n); +void *memmove (void *dest,const void *src,size_t n)__nocapture (2 );#define memcmp __builtin_memcmp#define __HAVE_ARCH_MEMCHR -extern void *memchr (const void *cs,int c,size_t count); +extern void *memchr (const void *cs,int c,size_t count)__nocapture (1 );static inline void *__memset_generic (void *s,char c,size_t count) {
@@ -247,7 +248,7 @@ extern size_t strnlen(const char *s, size_t count);/* end of additional stuff */ #define __HAVE_ARCH_STRSTR -extern char *strstr (const char *cs,const char *ct); +extern char *strstr (const char *cs,const char *ct)__nocapture (1 ,2 );/* * This looks horribly ugly, but the compiler can optimize it totally,
arch/x86/include/asm/string_64.h +9 -9
@@ -27,8 +27,8 @@ static __always_inline void *__inline_memcpy(void *to, const void *from, size_tfunction. */ #define __HAVE_ARCH_MEMCPY 1 -extern void *memcpy (void *to,const void *from,size_t len); -extern void *__memcpy (void *to,const void *from,size_t len); +extern void *memcpy (void *to,const void *from,size_t len)__nocapture (2 ); +extern void *__memcpy (void *to,const void *from,size_t len)__nocapture (2 );#ifndef CONFIG_KMEMCHECK #if (__GNUC__ ==4 && __GNUC_MINOR__ <3 ) || __GNUC__ <4
@@ -56,14 +56,14 @@ void *memset(void *s, int c, size_t n);void *__memset (void *s,int c,size_t n);#define __HAVE_ARCH_MEMMOVE -void *memmove (void *dest,const void *src,size_t count); -void *__memmove (void *dest,const void *src,size_t count); +void *memmove (void *dest,const void *src,size_t count)__nocapture (2 ); +void *__memmove (void *dest,const void *src,size_t count)__nocapture (2 ); -int memcmp (const void *cs,const void *ct,size_t count); +int memcmp(const void *cs, const void *ct, size_t count) __nocapture(1, 2);size_t strlen (const char *s); -char *strcpy (char *dest,const char *src); -char *strcat (char *dest,const char *src); -int strcmp (const char *cs,const char *ct); +char *strcpy (char *dest,const char *src)__nocapture (2 ); +char *strcat (char *dest,const char *src)__nocapture (2 ); +int strcmp(const char *cs, const char *ct) __nocapture(1, 2);#if defined(CONFIG_KASAN ) && !defined(__SANITIZE_ADDRESS__)
@@ -89,7 +89,7 @@ int strcmp(const char *cs, const char *ct);* * Return 0 for success, -EFAULT for fail */ -int memcpy_mcsafe (void *dst,const void *src,size_t cnt); +int memcpy_mcsafe(void *dst, const void *src, size_t cnt) __nocapture(2);#endif /* __KERNEL__ */
arch/x86/kernel/hpet.c +1 -1
@@ -136,7 +136,7 @@ int is_hpet_enabled(void) }EXPORT_SYMBOL_GPL (is_hpet_enabled); -static void _hpet_print_config (const char *function,int line) +static void __nocapture(1) _hpet_print_config(const char *function, int line) { u32 i, timers, l, h; printk(KERN_INFO "hpet: %s(%d):\n", function, line);
include/asm-generic/bug.h +3 -3
@@ -62,13 +62,13 @@ struct bug_entry {* to provide better diagnostics. */ #ifndef __WARN_TAINT -extern __printf(3, 4) +extern __printf(3, 4) __nocapture(1, 3, 4) void warn_slowpath_fmt(const char *file, const int line, const char *fmt, ...); -extern __printf(4, 5) +extern __printf(4, 5) __nocapture(1, 4, 5) void warn_slowpath_fmt_taint(const char *file, const int line, unsigned taint, const char *fmt, ...); -extern void warn_slowpath_null(const char *file, const int line); +extern __nocapture(1) void warn_slowpath_null(const char *file, const int line); #define WANT_WARN_ON_SLOWPATH #define __WARN() warn_slowpath_null(__FILE__, __LINE__) #define __WARN_printf(arg...) warn_slowpath_fmt(__FILE__, __LINE__, arg)
include/linux/compiler-gcc.h +8 -2
@@ -116,8 +116,10 @@*/ #define __pure __attribute__((pure))#define __aligned (x) __attribute__((aligned(x))) -#define __printf (a, b) __attribute__((format(printf, a, b))) -#define __scanf (a, b) __attribute__((format(scanf, a, b))) +#define __printf (a, b) __attribute__((format(printf, a, b))) \ + __nocapture(a, b) +#define __scanf (a, b) __attribute__((format(scanf, a, b))) \ + __nocapture(a, b)#define __attribute_const__ __attribute__((__const__))#define __maybe_unused __attribute__((unused))#define __always_unused __attribute__((unused))
@@ -193,6 +195,10 @@# define __latent_entropy __attribute__((latent_entropy))#endif +#ifdef INITIFY_PLUGIN +#define __nocapture (...) __attribute__((nocapture(__VA_ARGS__))) +#endif +/* * Mark a position in code as unreachable. This can be used to * suppress control flow warnings after asm blocks that transfer
include/linux/compiler.h +4 -0
@@ -429,6 +429,10 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s# define __latent_entropy#endif +#ifndef __nocapture +# define __nocapture (...) +#endif +/* * Tell gcc if a function is cold. The compiler will assume any path * directly leading to the call is unlikely.
include/linux/fs.h +3 -2
@@ -2426,8 +2426,9 @@ extern int register_chrdev_region(dev_t, unsigned, const char *);extern int __register_chrdev (unsigned int major,unsigned int baseminor,unsigned int count,const char *name,const struct file_operations *fops); -extern void __unregister_chrdev (unsigned int major,unsigned int baseminor, -unsigned int count,const char *name); +extern __nocapture(4) void __unregister_chrdev(unsigned int major, + unsigned int baseminor, unsigned int count, + const char *name); extern void unregister_chrdev_region(dev_t, unsigned); extern void chrdev_show(struct seq_file *,off_t);
include/linux/printk.h +1 -1
@@ -163,7 +163,7 @@ __printf(1, 2) __cold int printk_deferred(const char *fmt, ...);* with all other unrelated printk_ratelimit() callsites. Instead use * printk_ratelimited() or plain old __ratelimit(). */ -extern int __printk_ratelimit (const char *func); +extern int __printk_ratelimit(const char *func) __nocapture(1);#define printk_ratelimit () __printk_ratelimit(__func__)extern bool printk_timed_ratelimit (unsigned long *caller_jiffies,unsigned int interval_msec);
include/linux/string.h +38 -35
158 lines not on this page · raw message
Signature · 1 line
2.8.1
Re: [PATCH v1 2/2] Mark functions with the __nocapture attribute
Quoted 3 lines · Emese Revfy
The nocapture gcc attribute can be on functions only.
The attribute takes one or more unsigned integer constants as parameters that specify the function argument(s) of const char* type to initify.
Perhaps this should be const <type>*
arch/arm/include/asm/string.h
Quoted 1 line
arch/arm/include/asm/string.h +0
-0
[]
arch/arm/include/asm/string.h
Quoted 1 line
arch/arm/include/asm/string.h +0
-0@@ -7,19 +7,19 @@
[]
Quoted 3 lines
arch/arm/include/asm/string.h +1
-1#define __HAVE_ARCH_MEMCPY -extern void *memcpy (void *,const void *,__kernel_size_t ); +extern void *memcpy (void *,const void *,__kernel_size_t )__nocapture (2 );
Re: [PATCH v1 2/2] Mark functions with the __nocapture attribute
Quoted 6 lines · On Tue, 28 Jun 2016 09:43:31 -0700 Joe Perches <[email protected]> wrote:
Quoted 3 lines · Emese Revfy
The nocapture gcc attribute can be on functions only.
The attribute takes one or more unsigned integer constants as parameters that specify the function argument(s) of const char* type to initify.Perhaps this should be const <type>*
For me function arguments are the values passed to a function call so the const char* type is good because this is the only one that the plugin handles (for now at least).
Signature · 1 line
Emese
Re: [PATCH v1 2/2] Mark functions with the __nocapture attribute
Quoted 9 lines · Emese Revfy
Quoted 5 lines · On Tue, 28 Jun 2016 09:43:31 -0700 Joe Perches <[email protected]> wrote:
Quoted 3 lines · Emese Revfy
The nocapture gcc attribute can be on functions only.
The attribute takes one or more unsigned integer constants as parameters that specify the function argument(s) of const char* type to initify.Perhaps this should be const *
For me function arguments are the values passed to a function call so the const char* type is good because this is the only one that the plugin handles (for now at least).
OK, but this function prototype specified takes a const void *
+extern void * memcpy(void *, const void *, __kernel_size_t) __nocapture(2);
Re: [PATCH v1 2/2] Mark functions with the __nocapture attribute
Quoted 6 lines · On Tue, 28 Jun 2016 14:00:57 -0700 Joe Perches <[email protected]> wrote:
Quoted 5 lines · Emese Revfy
Quoted 4 lines · On Tue, 28 Jun 2016 09:43:31 -0700 Joe Perches <[email protected]> wrote:
Quoted 3 lines · Emese Revfy
The nocapture gcc attribute can be on functions only.
The attribute takes one or more unsigned integer constants as parameters that specify the function argument(s) of const char* type to initify.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Perhaps this should be const *
For me function arguments are the values passed to a function call so the const char* type is good because this is the only one that the plugin handles (for now at least).
OK, but this function prototype specified takes a const void *
+extern void *
memcpy(void *, const void *, __kernel_size_t)__nocapture(2);
What matters for the plugin is the type of the passed arguments (which can be const char* in the current implementation), not that of the parameters.
Signature · 1 line
Emese
Re: [PATCH v1 2/2] Mark functions with the __nocapture attribute
Quoted 8 lines · Emese Revfy
Quoted 7 lines · On Tue, 28 Jun 2016 14:00:57 -0700 Joe Perches <[email protected]> wrote:
Quoted 6 lines · Emese Revfy
Quoted 5 lines · On Tue, 28 Jun 2016 09:43:31 -0700 Joe Perches <[email protected]> wrote:
Quoted 3 lines · Emese Revfy
The nocapture gcc attribute can be on functions only.
The attribute takes one or more unsigned integer constants as parameters that specify the function argument(s) of const char* type to initify.Perhaps this should be const <void>*
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
For me function arguments are the values passed to a function call so the const char* type is good because this is the only one that the plugin handles (for now at least).
OK, but this function prototype specified takes a const void *
+extern void *
memcpy(void *, const void *, __kernel_size_t)__nocapture(2);What matters for the plugin is the type of the passed arguments (which can be const char* in the current implementation), not that of the parameters.
And how does this work when the prototype requires the compiler to implicit cast to const void * before calling the function?
Re: [PATCH v1 2/2] Mark functions with the __nocapture attribute
Quoted 12 lines · On Wed, 29 Jun 2016 17:12:45 -0700 Joe Perches <[email protected]> wrote:
Quoted 11 lines · Emese Revfy
Quoted 10 lines · On Tue, 28 Jun 2016 14:00:57 -0700 Joe Perches <[email protected]> wrote:
Quoted 9 lines · Emese Revfy
Quoted 5 lines · On Tue, 28 Jun 2016 09:43:31 -0700 Joe Perches <[email protected]> wrote:
Quoted 3 lines · Emese Revfy
The nocapture gcc attribute can be on functions only.
The attribute takes one or more unsigned integer constants as parameters that specify the function argument(s) of const char* type to initify.Perhaps this should be const <void>*
For me function arguments are the values passed to a function call so the const char* type is good because this is the only one that the plugin handles (for now at least).
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
OK, but this function prototype specified takes a const void *
+extern void *
memcpy(void *, const void *, __kernel_size_t)__nocapture(2);What matters for the plugin is the type of the passed arguments (which can be const char* in the current implementation), not that of the parameters.
And how does this work when the prototype requires the compiler to implicit cast to const void * before calling the function?
The plugin searches for the nocapture attribute that does not depend on the type. If the function argument is not a string constant just a pointer then the plugin walks the data flow (use-def chain) and tries to find a string constant. If there is a cast to void * then the use-def chain will walk across it.
Signature · 1 line
Emese
Re: [PATCH v1 2/2] Mark functions with the __nocapture attribute
In reply to Emese RevfyQuoted 34 lines · Emese Revfy
The nocapture gcc attribute can be on functions only.
The attribute takes one or more unsigned integer constants as parameters that specify the function argument(s) of const char* type to initify. If the marked argument is a vararg then the plugin initifies all vararg arguments.I couldn't test the arm, arm64 and powerpc architectures.
Signed-off-by Emese Revfy <[email protected]>--- arch/arm/include/asm/string.h | 10 +++--- arch/arm64/include/asm/string.h | 23 ++++++------ arch/powerpc/include/asm/string.h | 19 +++++----- arch/x86/boot/string.h | 4 +-- arch/x86/include/asm/string_32.h | 21 +++++------ arch/x86/include/asm/string_64.h | 18 +++++----- arch/x86/kernel/hpet.c | 2 +- include/asm-generic/bug.h | 6 ++-- include/linux/compiler-gcc.h | 10 ++++-- include/linux/compiler.h | 4 +++ include/linux/fs.h | 5 +-- include/linux/printk.h | 2 +- include/linux/string.h | 73 ++++++++++++++++++++------------------- 13 files changed, 107 insertions(+), 90 deletions(-)arch/arm/include/asm/string.h +0
-1@@ -7,19 +7,19 @@*/ #define __HAVE_ARCH_STRRCHR -extern char *strrchr (const char * s,int c);
arch/arm/include/asm/string.h line 17
Quoted 10 lines
arch/arm/include/asm/string.h +3
-2+extern char *strrchr (const char * s,int c)__nocapture (1 );#define __HAVE_ARCH_STRCHR -extern char *strchr (const char * s,int c); +extern char *strchr (const char * s,int c)__nocapture (1 );#define __HAVE_ARCH_MEMCPY -extern void *memcpy (void *,const void *,__kernel_size_t ); +extern void *memcpy (void *,const void *,__kernel_size_t )__nocapture (2 );
Why not also mark he dest argument of the various strcpy/memcpy/memset functions as nocapture? It's no use for the initify plugin, but a static analysis tool could for example use it to (better) diagnose resource leaks (e.g., a buffer is kmalloc'ed and initialized with some *cpy, but then something goes wrong, and the buffer isn't freed on the error path).
arch/arm/include/asm/string.h line 19
Quoted 3 lines
arch/arm/include/asm/string.h +0
-0#define __HAVE_ARCH_STRLENextern __kernel_size_t strlen (const char *);
Why not also mark strlen? I know that gcc optimizes strlen("literal") away, but, again, there might be other uses even when the argument is not a literal. One example is the plugin itself, which could deduce (or suggest) __nocapture for a given function parameter if the parameter is only passed on as __nocapture arguments.
Quoted 6 lines
include/asm-generic/bug.h +0
-0@@ -62,13 +62,13 @@ struct bug_entry {* to provide better diagnostics.
include/asm-generic/bug.h struct bug_entry { · line 70
Quoted 10 lines
include/asm-generic/bug.h +2
-2*/ #ifndef __WARN_TAINT -extern __printf(3, 4) +extern __printf(3, 4) __nocapture(1, 3, 4) void warn_slowpath_fmt(const char *file, const int line, const char *fmt, ...); -extern __printf(4, 5) +extern __printf(4, 5) __nocapture(1, 4, 5) void warn_slowpath_fmt_taint(const char *file, const int line, unsigned taint, const char *fmt, ...);
The 3,4 and 4,5 parts seem redundant when __printf automatically supplies those.
Quoted 10 lines
include/asm-generic/bug.h +1
-1-extern void warn_slowpath_null (const char *file,const int line); +extern __nocapture(1) void warn_slowpath_null(const char *file, const int line); #define WANT_WARN_ON_SLOWPATH#define __WARN () warn_slowpath_null(__FILE__, __LINE__) #define __WARN_printf(arg...) warn_slowpath_fmt(__FILE__, __LINE__, arg)include/linux/compiler-gcc.h +0
-0
include/linux/compiler-gcc.h line 122
Quoted 9 lines
include/linux/compiler-gcc.h +4
-2@@ -116,8 +116,10 @@*/ #define __pure __attribute__((pure))#define __aligned (x) __attribute__((aligned(x))) -#define __printf (a, b) __attribute__((format(printf, a, b))) -#define __scanf (a, b) __attribute__((format(scanf, a, b))) +#define __printf (a, b) __attribute__((format(printf, a, b))) \ + __nocapture(a, b) +#define __scanf (a, b) __attribute__((format(scanf, a, b))) \ + __nocapture(a, b)
So obviously the output parameters for scanf are never going to be string literals, but I've already argued that one might as well put the __nocapture on all relevant pointer arguments while we're churning the headers, so keep this.
include/linux/compiler-gcc.h
Quoted 7 lines
include/linux/compiler-gcc.h +2
-4-extern char *kstrdup (const char *s,gfp_t gfp) __malloc; -extern const char *kstrdup_const (const char *s,gfp_t gfp); -extern char *kstrndup (const char *s,size_t len,gfp_t gfp); -extern void *kmemdup (const void *src,size_t len,gfp_t gfp); +extern char *kstrdup (const char *s,gfp_t gfp) __malloc__nocapture (1 ); +extern const char *kstrdup_const (const char *s,gfp_t gfp)__nocapture (1 );
OK, so this one is pretty dangerous, and probably wrong. If one does
foo->bar = kstrdup_const(a-macro-that-might-be-a-string-literal)
in an .init function, foo->bar will very likely become dangling. Come to think of it, I guess this means that __nocapture arguments must not only not be stashed anywhere, the return value cannot point (in)to the same object, which in turn then also disqualifies at least strchr(), memchr() and the haystack parameter of strstr(). And bummer, that kills my suggestion to add __nocapture to the dst parameters of *cpy, since they return that argument.
Rasmus
Re: [PATCH v1 2/2] Mark functions with the __nocapture attribute
Quoted 7 lines · Rasmus Villemoes
Quoted 1 line
+extern const char *kstrdup_const(const char *s,
gfp_tgfp)__nocapture(1);OK, so this one is pretty dangerous, and probably wrong. If one does
foo->bar=kstrdup_const(a-macro-that-might-be-a-string-literal)in an .init function,
foo->barwill very likely become dangling.
doesn't kstrdup_const omit the copy only for arguments that are stored in .rodata (which doesn't include .init.rodata* and other init sections)?
cheers,
PaX Team
Re: [PATCH v1 2/2] Mark functions with the __nocapture attribute
Quoted 4 lines · PaX Team
Quoted 2 lines · Rasmus Villemoes
Quoted 1 line
+extern const char *kstrdup_const(const char *s,
gfp_tgfp)__nocapture(1);
Quoted 9 lines
Quoted 5 lines
OK, so this one is pretty dangerous, and probably wrong. If one does
foo->bar=kstrdup_const(a-macro-that-might-be-a-string-literal)in an .init function,
foo->barwill very likely become dangling.doesn't
kstrdup_constomit the copy only for arguments that are stored in .rodata (which doesn't include .init.rodata* and other init sections)?
Ah, right. But that's a little subtle. Also, it kind of defeats the purpose of kstrdup_const - but it's probably not actually called with a string literal all that often.
In any case, I think there's still a problem with strchr() and friends.
Rasmus
Re: [PATCH v1 2/2] Mark functions with the __nocapture attribute
In reply to Rasmus VillemoesQuoted 11 lines · On Tue, 28 Jun 2016 22:50:55 +0200 Rasmus Villemoes <[email protected]>…
Quoted 10 lines · Emese Revfy
include/asm-generic/bug.h +1
-1@@ -62,13 +62,13 @@ struct bug_entry {* to provide better diagnostics. */ #ifndef __WARN_TAINT -extern __printf(3, 4) +extern __printf(3, 4) __nocapture(1, 3, 4)
Quoted 8 lines
include/asm-generic/bug.h struct bug_entry { · line 70
Quoted 6 lines
include/asm-generic/bug.h +1
-1void warn_slowpath_fmt (const char *file,const int line,const char *fmt, ...); -extern __printf(4, 5) +extern __printf(4, 5) __nocapture(1, 4, 5) void warn_slowpath_fmt_taint(const char *file, const int line, unsigned taint, const char *fmt, ...);The 3,4 and 4,5 parts seem redundant when
__printfautomatically supplies those.
Thanks, I'll fix them in the next patch set.
Signature · 1 line
Emese
Quoted 2 lines · On Tue, 28 Jun 2016 13:34:07 +0200 Emese Revfy <[email protected]> wrote:
* automatically discover init/exit functions and apply the
__initor__exitattributes on them
Hi,
I have a question about this. If a function is called by __init and __exit functions as well then I move it to the __exit section. I think this is correct because such a function is available to both __init and __exit functions as well at runtime.
However this generates a section mismatch (from scripts/mod/modpost.c) e.g.,
WARNING: vmlinux.o(.init.text+0x196849): Section mismatch in reference from the function sctp_init() to the function .exit.text:sctp_v4_del_protocol() The function __init sctp_init() references
a function __exit sctp_v4_del_protocol().
This is often seen when error handling in the init function
uses functionality in the exit path.
The fix is often to remove the __exit annotation of
sctp_v4_del_protocol() so it may be used outside an exit section.
This check was introduced by this commit (588ccd732ba2d):
kbuild: add verbose option to Section mismatch reporting in modpost
If this move doesn't cause a problem then I would like to keep it because there are a lot of functions that can become __exit and I would like to remove this warning.
Thanks
Signature · 1 line
Emese
Mark Rutland
reviewerQuoted 22 lines · Emese Revfy
I would like to introduce the initify gcc plugin. The kernel already has a mechanism to free up code and data memory that is only used during kernel or module initialization.
This plugin will teach the compiler to find more such code and data that can be freed after initialization. It reduces memory usage.
The initify gcc plugin can be useful for embedded systems.It is a CII project supported by the Linux Foundation.
This plugin is the part of grsecurity/PaX.
The plugin supports all gcc versions from 4.5 to 6.0.
I made some changes on top of the PaX version (since March 6.). These are the important ones:
* move all local strings to init.rodata.str and exit.rodata.str (not just__func__)
* report all initified strings and functions (GCC_PLUGIN_INITIFY_VERBOSEconfig option)
* automatically discover init/exit functions and apply the__initor__exitattributes on them
Quoted 6 lines
You can find more about the changes here:
https://github.com/ephox-gcc-plugins/initifyThis patch set is based on the "Add support for complex gcc plugins that don't fit in a single file" patch set (git/kees/linux.git#kspp HEAD:
e5d4798b284cd192c8b).
I was hoping to give this a spin on arm/arm64, but I couldn't find the prerequisite branch/tag/commit in that tree:
https://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/commit/?id=e5d4798b284cd192c8b
Where should I be looking?
Thanks,
Mark.
Quoted 11 lines · On Tue, 28 Jun 2016 13:57:49 +0100 Mark Rutland <[email protected]> wrote:
Quoted 3 lines · Emese Revfy
This patch set is based on the "Add support for complex gcc plugins that don't fit in a single file" patch set (git/kees/linux.git#kspp HEAD:
e5d4798b284cd192c8b).I was hoping to give this a spin on arm/arm64, but I couldn't find the prerequisite branch/tag/commit in that tree:
https://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/commit/?id=e5d4798b284cd192c8b
Where should I be looking?
Sorry, this commit was disappear. You can apply these patches to this tree:
https://github.com/ephox-gcc-plugins/gcc-plugins_linux-next.git (initify branch)
Signature · 1 line
Emese
Quoted 11 lines · Emese Revfy
Quoted 8 lines · On Tue, 28 Jun 2016 13:57:49 +0100 Mark Rutland <[email protected]> wrote:
Quoted 3 lines · Emese Revfy
This patch set is based on the "Add support for complex gcc plugins that don't fit in a single file" patch set (git/kees/linux.git#kspp HEAD:
e5d4798b284cd192c8b).I was hoping to give this a spin on arm/arm64, but I couldn't find the prerequisite branch/tag/commit in that tree:
Quoted 6 lines
Quoted 3 lines
https://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/commit/?id=e5d4798b284cd192c8b
Where should I be looking?
Sorry, this commit was disappear. You can apply these patches to this tree:
https://github.com/ephox-gcc-plugins/gcc-plugins_linux-next.git (initify branch)
The linux-next tree should have what you need for the plugin support. This is merging from my KSPP tree:
https://git.kernel.org/linux/kernel/git/kees/linux.git for-next/kspp
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Mark Rutland
reviewerQuoted 16 lines · Kees Cook
Quoted 15 lines · Emese Revfy
Quoted 11 lines · On Tue, 28 Jun 2016 13:57:49 +0100 Mark Rutland <[email protected]> wrote:
Quoted 3 lines · Emese Revfy
This patch set is based on the "Add support for complex gcc plugins that don't fit in a single file" patch set (git/kees/linux.git#kspp HEAD:
e5d4798b284cd192c8b).I was hoping to give this a spin on arm/arm64, but I couldn't find the prerequisite branch/tag/commit in that tree:
https://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/commit/?id=e5d4798b284cd192c8b
Where should I be looking?
Quoted 7 lines
Quoted 2 lines
Sorry, this commit was disappear. You can apply these patches to this tree:
https://github.com/ephox-gcc-plugins/gcc-plugins_linux-next.git (initify branch)The linux-next tree should have what you need for the plugin support. This is merging from my KSPP tree:
https://git.kernel.org/linux/kernel/git/kees/linux.git for-next/kspp
I didn't see the complex gcc plugins patch in that branch or linux-next, so I take it you mean a a base for applying the complex plugin patch and initify patches.
I'll try to give that a go shortly.
Thanks,
Mark.
Mark Rutland
reviewerQuoted 23 lines · Mark Rutland
Quoted 22 lines · Kees Cook
Quoted 11 lines · Emese Revfy
Quoted 8 lines · On Tue, 28 Jun 2016 13:57:49 +0100 Mark Rutland <[email protected]> wrote:
Quoted 3 lines · Emese Revfy
This patch set is based on the "Add support for complex gcc plugins that don't fit in a single file" patch set (git/kees/linux.git#kspp HEAD:
e5d4798b284cd192c8b).I was hoping to give this a spin on arm/arm64, but I couldn't find the prerequisite branch/tag/commit in that tree:
Quoted 6 lines
Quoted 3 lines
https://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/commit/?id=e5d4798b284cd192c8b
Where should I be looking?
Sorry, this commit was disappear. You can apply these patches to this tree:
https://github.com/ephox-gcc-plugins/gcc-plugins_linux-next.git (initify branch)The linux-next tree should have what you need for the plugin support. This is merging from my KSPP tree:
Quoted 7 lines
Quoted 1 line
https://git.kernel.org/linux/kernel/git/kees/linux.git for-next/kspp
I didn't see the complex gcc plugins patch in that branch or linux-next, so I take it you mean a a base for applying the complex plugin patch and initify patches.
I'll try to give that a go shortly.
I tried to give this a go atop of kees's for-next/kspp branch, but this doesn't seem to build. With the Linaro 15.08 AArch64 toolchain, I see:
[mark@leverpostej:~/src/linux]% uselinaro 15.08 make V=1 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
set -e; : ' CHK include/config/kernel.release'; mkdir -p include/config/; echo "4.7.0-rc1$(/bin/bash ./scripts/setlocalversion .)" < include/config/auto.conf > include/config/kernel.release.tmp; if [ -r include/config/kernel.release ] && cmp -s include/config/kernel.release include/config/kernel.release.tmp; then rm -f include/config/kernel.release.tmp; else : ' UPD include/config/kernel.release'; mv -f include/config/kernel.release.tmp include/config/kernel.release; fi
make -f ./scripts/Makefile.asm-generic \
src=asm obj=arch/arm64/include/generated/asm
make -f ./scripts/Makefile.asm-generic \
src=uapi/asm obj=arch/arm64/include/generated/uapi/asm
set -e; : ' CHK include/generated/uapi/linux/version.h'; mkdir -p include/generated/uapi/linux/; (echo \#define LINUX_VERSION_CODE 263936; echo '#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))';) < Makefile > include/generated/uapi/linux/version.h.tmp; if [ -r include/generated/uapi/linux/version.h ] && cmp -s include/generated/uapi/linux/version.h include/generated/uapi/linux/version.h.tmp; then rm -f include/generated/uapi/linux/version.h.tmp; else : ' UPD include/generated/uapi/linux/version.h'; mv -f include/generated/uapi/linux/version.h.tmp include/generated/uapi/linux/version.h; fi
rm -f include/linux/version.h
set -e; : ' CHK include/generated/utsrelease.h'; mkdir -p include/generated/; if [ echo -n "4.7.0-rc1+" | wc -c -gt 64 ]; then echo '"4.7.0-rc1+" exceeds 64 characters' >&2; exit 1; fi; (echo \#define UTS_RELEASE \"4.7.0-rc1+\";) < include/config/kernel.release > include/generated/utsrelease.h.tmp; if [ -r include/generated/utsrelease.h ] && cmp -s include/generated/utsrelease.h include/generated/utsrelease.h.tmp; then rm -f include/generated/utsrelease.h.tmp; else : ' UPD include/generated/utsrelease.h'; mv -f include/generated/utsrelease.h.tmp include/generated/utsrelease.h; fi
mkdir -p .tmp_versions ; rm -f .tmp_versions/*
test -e include/generated/autoksyms.h || \
touch include/generated/autoksyms.h
make -f ./scripts/Makefile.build obj=scripts/basic
(cat /dev/null; ) > scripts/basic/modules.order
rm -f .tmp_quiet_recordmcount
make -f ./scripts/Makefile.build obj=scripts/gcc-plugins
(cat /dev/null; ) > scripts/gcc-plugins/modules.order
g++ -shared -o scripts/gcc-plugins/initify_plugin.so
g++: fatal error: no input files
compilation terminated.
make[1]: *** [scripts/gcc-plugins/initify_plugin.so] Error 4
make: *** [gcc-plugins] Error 2
Any ideas?
Thanks,
Mark.
Quoted 18 lines · On Wed, 29 Jun 2016 18:52:27 +0100 Mark Rutland <[email protected]> wrote:
Quoted 17 lines · Mark Rutland
Quoted 16 lines · Kees Cook
Quoted 15 lines · Emese Revfy
Quoted 11 lines · On Tue, 28 Jun 2016 13:57:49 +0100 Mark Rutland <[email protected]> wrote:
Quoted 3 lines · Emese Revfy
This patch set is based on the "Add support for complex gcc plugins that don't fit in a single file" patch set (git/kees/linux.git#kspp HEAD:
e5d4798b284cd192c8b).I was hoping to give this a spin on arm/arm64, but I couldn't find the prerequisite branch/tag/commit in that tree:
https://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/commit/?id=e5d4798b284cd192c8b
Where should I be looking?
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Sorry, this commit was disappear. You can apply these patches to this tree:
https://github.com/ephox-gcc-plugins/gcc-plugins_linux-next.git (initify branch)g++: fatal error: no input files
compilation terminated.
make[1]: *** [scripts/gcc-plugins/initify_plugin.so] Error 4
make: *** [gcc-plugins] Error 2
Hi,
There are some missing patches from Kees tree that's why I suggested my tree above.
Signature · 1 line
Emese
(adding Mathias Krause who did something similar via macros in 2014)
https://lkml.org/lkml/2014/6/22/149
Quoted 6 lines · Emese Revfy
I would like to introduce the initify gcc plugin. The kernel already has a mechanism to free up code and data memory that is only used during kernel or module initialization.
This plugin will teach the compiler to find more such code and data that can be freed after initialization. It reduces memory usage.
The initify gcc plugin can be useful for embedded systems.
What happens to string deduplication when one string is in an init function and the same string is also used in a non-init function in the same compilation unit?
foo.c
__init void initfunc(void)
{
pr_info("%s: I'm here\n", __func__);
}void runtimefunc(void)
{
pr_info("I'm here: %s\n", __func__);
}In what section does the string "I'm here: %s\n" get placed or does it get placed into multiple sections?
Quoted 26 lines · Joe Perches
(adding Mathias Krause who did something similar via macros in 2014)
https://lkml.org/lkml/2014/6/22/149
Quoted 10 lines · Emese Revfy
I would like to introduce the initify gcc plugin. The kernel already has
a mechanism to free up code and data memory that is only used during kernel
or module initialization.
This plugin will teach the compiler to find more such code and data that
can be freed after initialization. It reduces memory usage. The initify gcc plugin can be useful for embedded systems.What happens to string deduplication when one string is in an init function and the same string is also used in a non-init function in the same compilation unit?
foo.c
__initvoid initfunc(void){ pr_info("%s: I'm here\n", __func__); }
Quoted 7 lines
void runtimefunc(void)
{ pr_info("I'm here: %s\n", __func__); }In what section does the string "I'm here: %s\n" get placed or does it get placed into multiple sections?
Sorry, Joe can't type.
That was meant to be the identical string in both functions.
Quoted 11 lines · Joe Perches
Quoted 11 lines
What happens to string deduplication when one string is in an init function and the same string is also used in a non-init function in the same compilation unit?
foo.c
__initvoid initfunc(void){ pr_info("%s: I'm here\n", __func__); }
Quoted 7 lines
Quoted 7 lines
void runtimefunc(void)
{ pr_info("I'm here: %s\n", __func__); }In what section does the string "I'm here: %s\n" get placed or does it get placed into multiple sections?
It'll get placed in multiple sections by the compiler, and nothing bad happens. String deduplication is something the linker does to sections equipped with appropriate flags. So in this case that of course means that the kernel image itself would be slightly bigger, while the used data after init would be the same. But I don't think there's a lot of these cases. (Also, "initfunc" would at least vanish).
Quoted 3 lines · On Tue, 28 Jun 2016 21:02:54 +0200 Rasmus Villemoes <[email protected]>…
It'll get placed in multiple sections by the compiler, and nothing bad happens. String deduplication is something the linker does to sections equipped with appropriate flags. So in this case that of course means
Note that I didn't add such flags to my sections yet, but I have it on my todo list.
Quoted 3 lines
that the kernel image itself would be slightly bigger, while the used data after init would be the same. But I don't think there's a lot of these cases. (Also, "initfunc" would at least vanish).
Signature · 1 line
Emese
Hi Emese,
Quoted 41 lines · Emese Revfy
I would like to introduce the initify gcc plugin. The kernel already has a mechanism to free up code and data memory that is only used during kernel or module initialization.
This plugin will teach the compiler to find more such code and data that can be freed after initialization. It reduces memory usage.
The initify gcc plugin can be useful for embedded systems.It is a CII project supported by the Linux Foundation.
This plugin is the part of grsecurity/PaX.
The plugin supports all gcc versions from 4.5 to 6.0.
I made some changes on top of the PaX version (since March 6.). These are the important ones:
* move all local strings to init.rodata.str and exit.rodata.str (not just__func__)
* report all initified strings and functions (GCC_PLUGIN_INITIFY_VERBOSEconfig option)
* automatically discover init/exit functions and apply the__initor__exitattributes on themYou can find more about the changes here:
https://github.com/ephox-gcc-plugins/initifyThis patch set is based on the "Add support for complex gcc plugins that don't fit in a single file" patch set (git/kees/linux.git#kspp HEAD:
e5d4798b284cd192c8b).Some statistics about the plugin:
On allyes config (amd64, gcc-6):
* 7731 initified strings
* 231 initified functionsOn allmod config (i386, gcc-6):
* 8846 initified strings
* 252 initified functionsOn allyes config (amd64, gcc-6):
Quoted 7 lines
section vanilla vanilla + initify change
-----------------------------------------------------------------------.rodata 39059688 (0x25400e8) 38527210 (0x24be0ea) -532478 .data 45744128 (0x2ba0000) 45404160 (0x2b4d000) -339968 .init.data 1361144 (0x14c4f8) 1674200 (0x198bd8) +313056.text 77615128 (
0x4a05018) 77576664 (0x49fb9d8) -38464
.init.text 1108455 (0x10e9e7) 1137618 (0x115bd2) +29163
You should probably provide numbers for .init.rodata.str, .exit.rodata.str and .exit.text as well. Otherwise this delta calculation suggests a rather gigantic image size reduction which is probably not the case ;)
Also a comparison of the final kernel image size would be nice to see if the string duplication issue mentioned in [1] is actually an issue.
[1] http://marc.info/?l=linux-kernel&m=140364632417795&w=2
Thanks,
Mathias
Quoted 25 lines
Emese Revfy (2):
Add the initify gcc plugin
Mark functions with the__nocaptureattribute--- arch/Kconfig | 23 + arch/arm/include/asm/string.h | 10 +- arch/arm64/include/asm/string.h | 23 +- arch/powerpc/include/asm/string.h | 19 +- arch/x86/boot/string.h | 4 +- arch/x86/include/asm/string_32.h | 21 +- arch/x86/include/asm/string_64.h | 18 +- arch/x86/kernel/hpet.c | 2 +- include/asm-generic/bug.h | 6 +- include/asm-generic/vmlinux.lds.h | 2 + include/linux/compiler-gcc.h | 10 +- include/linux/compiler.h | 4 + include/linux/fs.h | 5 +- include/linux/printk.h | 2 +- include/linux/string.h | 73 +-- scripts/Makefile.gcc-plugins | 4 + scripts/gcc-plugins/initify_plugin.c | 1147 ++++++++++++++++++++++++++++++++++ 17 files changed, 1283 insertions(+), 90 deletions(-)
Quoted 11 lines · On Tue, 28 Jun 2016 19:00:22 +0200 Mathias Krause <[email protected]> wrote:
Quoted 7 lines
section vanilla vanilla + initify change
-----------------------------------------------------------------------.rodata 39059688 (0x25400e8) 38527210 (0x24be0ea) -532478 .data 45744128 (0x2ba0000) 45404160 (0x2b4d000) -339968 .init.data 1361144 (0x14c4f8) 1674200 (0x198bd8) +313056.text 77615128 (
0x4a05018) 77576664 (0x49fb9d8) -38464
.init.text 1108455 (0x10e9e7) 1137618 (0x115bd2) +29163You should probably provide numbers for .init.rodata.str, .exit.rodata.str and .exit.text as well. Otherwise this delta calculation suggests a rather gigantic image size reduction which is probably not the case ;)
init.rodata.str is an input section that merges into the INIT_DATA output section. I didn't make statistics for exit* yet because I have a problem with that (I asked for help in a previous e-mail).
Quoted 1 line
Also a comparison of the final kernel image size would be nice to see if
I no longer have the vmlinux images but I saved away the readelf outputs, here's the PT_LOAD size data:
FileSiz (vanilla) FileSiz (vanilla+initify) change
-----------------------------------------------------------------------
00 119189504 (0x71ab000) 118657024 (0x7129000) -532480 01 45838336 (0x2bb7000) 45498368 (0x2b64000) -339968 03 2830336 (0x2b3000) 3198976 (0x30d000) +368640
00 .text .text.exit .text.startup .notes __ex_table .rodata __bug_table .pci_fixup
.builtin_fw .tracedata __ksymtab __ksymtab_gpl __ksymtab_strings __init_rodata __param __modver
01 .data .fini_array .vvar
03 .init.text .altinstr_aux .init.data .x86_cpu_dev.init .parainstructions .altinstructions
.altinstr_replacement .iommu_table .apicdrivers .exit.text .exit.data .smp_locks .bss .brk
I think the smaller .data section is wrong, so I'll look into it.
Signature · 1 line
Emese
Quoted 6 lines · Emese Revfy
I would like to introduce the initify gcc plugin. The kernel already has a mechanism to free up code and data memory that is only used during kernel or module initialization.
This plugin will teach the compiler to find more such code and data that can be freed after initialization. It reduces memory usage.
The initify gcc plugin can be useful for embedded systems.
Another potentially useful plugin, especially for embedded systems, would be to compress any string literal marked with
__attribute__((format(printf, string-index,)))
and decompress the compressed format on the stack in lib/vsprintf.c vsnprintf just before use.
I've started to experiment with that.
Quoted 7 lines · Joe Perches
Another potentially useful plugin, especially for embedded systems, would be to compress any string literal marked with
__attribute__((format(printf, string-index,)))and decompress the compressed format on the stack in
lib/vsprintf.cvsnprintf just before use.
Are there enough such strings in the kernel to make it worth the effort? I'm assuming that the string literals in printk("some string here") are automatically so marked?
Is there a minimum length under which the compression overhead actually makes it larger? For instance, "a" can't be compressed, because you need to indicate there's 1 "a" and 1 \0, while "aaaaaa" can, because you can express it as '6*"a" 1 \0 in only 4 bytes not 7. But exactly where the cutoff is, I have no idea (and is probably *very* text-dependent, as "aaaaaa" and "abcdef" will compress differently....)
Quoted 6 lines · On Tue, 2016-06-28 at 18:07 -0400, [email protected] wrote:
Quoted 4 lines · Joe Perches
Another potentially useful plugin, especially for embedded systems, would be to compress any string literal marked with
Quoted 7 lines
Quoted 4 lines
__attribute__((format(printf, string-index,)))and decompress the compressed format on the stack in
lib/vsprintf.cvsnprintf just before use.Are there enough such strings in the kernel to make it worth the effort? I'm assuming that the string literals in
printk("some string here")are automatically so marked?
Yes, that's the concept.
Quoted 2 lines
Is there a minimum length under which the compression overhead actually makes it larger?
No, compression would have to be possible, otherwise it'd be stored directly. Compression would use a special "compressed string" header with a 2 byte overhead and then stored with no trailing \0.
Something like struct compressed_format_header {
u8 flag; /* Must be ASCII STX or "\b" */
u8 length;
}Depends on the config of course, but it could reduce total image size ~50k on an x86-32 defconfig