All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/7] Trim unused exported kernel symbols
@ 2016-02-18 20:06 Nicolas Pitre
  2016-02-18 20:06 ` [PATCH v3 1/7] kbuild: record needed exported symbols for modules Nicolas Pitre
                   ` (7 more replies)
  0 siblings, 8 replies; 11+ messages in thread
From: Nicolas Pitre @ 2016-02-18 20:06 UTC (permalink / raw)
  To: Michal Marek, linux-kbuild; +Cc: linux-kernel, Al Viro, Rusty Russell

This patch series provides the option to omit exported symbols from
the kernel and modules that are never referenced by any of the selected
modules in the current kernel configuration.

The original cover letter with lots of test results can be found here:

        https://lkml.org/lkml/2016/2/8/813

A git branch with those patches is also available here:

        http://git.linaro.org/people/nicolas.pitre/linux.git autoksyms

Please consider for merging.

Changes from v2:

- Generating the build dependencies by parsing the source with fixdep
  turned out to be unreliable due to all the EXPORT_SYMBOL() variants,
  and especially their use within macros where the actual symbol name
  is known only after running the preprocessor. This list of symbol names
  is now obtained from the preprocessor directly, fixing allmodconfig
  builds.

Changes from v1:

- Replaced "exp" that doesn't convey the right meaning as noted by
  Sam Ravnborg. The "ksym" identifier is actually what the kernel
  already uses for this. Therefore:
  - CONFIG_TRIM_UNUSED_EXPSYMS --> CONFIG_TRIM_UNUSED_KSYMS
  - include/generated/expsyms.h --> include/generated/autoksyms.h
  - #define __EXPSYM_* --> #define __KSYM_*

- Some sed regexp improvements as suggested by Al Viro.

- Renamed vmlinux_recursive target to autoksyms_recursive.

- Accept EXPORT_SYMBOL variants with a prefix, e.g. ACPI_EXPORT_SYMBOL.

- Minor commit log clarifications.

- Added Rusty's ACK.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v3 1/7] kbuild: record needed exported symbols for modules
  2016-02-18 20:06 [PATCH v3 0/7] Trim unused exported kernel symbols Nicolas Pitre
@ 2016-02-18 20:06 ` Nicolas Pitre
  2016-02-18 20:06 ` [PATCH v3 2/7] allow for per-symbol configurable EXPORT_SYMBOL() Nicolas Pitre
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Nicolas Pitre @ 2016-02-18 20:06 UTC (permalink / raw)
  To: Michal Marek, linux-kbuild; +Cc: linux-kernel, Al Viro, Rusty Russell

Kernel modules are partially linked object files with some undefined
symbols that are expected to be matched with EXPORT_SYMBOL() entries
from elsewhere.

Each .tmp_versions/*.mod file currently contains two line of text
separated by a newline character. The first line has the actual module
file name while the second line has a list of object files constituting
that module. Those files are parsed by modpost (scripts/mod/sumversion.c),
scripts/Makefile.modpost, scripts/Makefile.modsign, etc.  Only the
modpost utility cares about the second line while the others retrieve
only the first line.

Therefore we can add a third line to record the list of undefined symbols
aka required EXPORT_SYMBOL() entries for each module into that file
without breaking anything. Like for the second line, symbols are separated
by a blank and the list is terminated with a newline character.

To avoid needless build overhead, the undefined symbols extraction is
performed only when CONFIG_TRIM_UNUSED_KSYMS is selected.

Signed-off-by: Nicolas Pitre <nico@linaro.org>
Acked-by: Rusty Russell <rusty@rustcorp.com.au>
---
 scripts/Makefile.build | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 2c47f9c305..f4b4320e0d 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -253,6 +253,13 @@ define rule_cc_o_c
 	mv -f $(dot-target).tmp $(dot-target).cmd
 endef
 
+# List module undefined symbols (or empty line if not enabled)
+ifdef CONFIG_TRIM_UNUSED_KSYMS
+cmd_undef_syms = $(NM) $@ | sed -n 's/^ \+U //p' | xargs echo
+else
+cmd_undef_syms = echo
+endif
+
 # Built-in and composite module parts
 $(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE
 	$(call cmd,force_checksrc)
@@ -263,7 +270,8 @@ $(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE
 $(single-used-m): $(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE
 	$(call cmd,force_checksrc)
 	$(call if_changed_rule,cc_o_c)
-	@{ echo $(@:.o=.ko); echo $@; } > $(MODVERDIR)/$(@F:.o=.mod)
+	@{ echo $(@:.o=.ko); echo $@; \
+	   $(cmd_undef_syms); } > $(MODVERDIR)/$(@F:.o=.mod)
 
 quiet_cmd_cc_lst_c = MKLST   $@
       cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
@@ -393,7 +401,8 @@ $(call multi_depend, $(multi-used-y), .o, -objs -y)
 
 $(multi-used-m): FORCE
 	$(call if_changed,link_multi-m)
-	@{ echo $(@:.o=.ko); echo $(link_multi_deps); } > $(MODVERDIR)/$(@F:.o=.mod)
+	@{ echo $(@:.o=.ko); echo $(link_multi_deps); \
+	   $(cmd_undef_syms); } > $(MODVERDIR)/$(@F:.o=.mod)
 $(call multi_depend, $(multi-used-m), .o, -objs -y -m)
 
 targets += $(multi-used-y) $(multi-used-m)
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v3 2/7] allow for per-symbol configurable EXPORT_SYMBOL()
  2016-02-18 20:06 [PATCH v3 0/7] Trim unused exported kernel symbols Nicolas Pitre
  2016-02-18 20:06 ` [PATCH v3 1/7] kbuild: record needed exported symbols for modules Nicolas Pitre
@ 2016-02-18 20:06 ` Nicolas Pitre
  2016-02-18 20:06 ` [PATCH v3 3/7] fixdep: accept extra dependencies on stdin Nicolas Pitre
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Nicolas Pitre @ 2016-02-18 20:06 UTC (permalink / raw)
  To: Michal Marek, linux-kbuild; +Cc: linux-kernel, Al Viro, Rusty Russell

Similar to include/generated/autoconf.h, include/generated/autoksyms.h
will contain a list of defines for each EXPORT_SYMBOL() that we want
active. The format is:

  #define __KSYM_<symbol_name> 1

This list will be auto-generated with another patch.  For now we only
include the preprocessor magic to automatically create or omit the
corresponding struct kernel_symbol declaration.

Given the content of include/generated/autoksyms.h may not be known in
advance, an empty file is created early on to let the build proceed.

Signed-off-by: Nicolas Pitre <nico@linaro.org>
Acked-by: Rusty Russell <rusty@rustcorp.com.au>
---
 Makefile               |  2 ++
 include/linux/export.h | 22 ++++++++++++++++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index 6c1a3c2479..e916428cf7 100644
--- a/Makefile
+++ b/Makefile
@@ -986,6 +986,8 @@ prepare2: prepare3 outputmakefile asm-generic
 prepare1: prepare2 $(version_h) include/generated/utsrelease.h \
                    include/config/auto.conf
 	$(cmd_crmodverdir)
+	$(Q)test -e include/generated/autoksyms.h || \
+	    touch   include/generated/autoksyms.h
 
 archprepare: archheaders archscripts prepare1 scripts_basic
 
diff --git a/include/linux/export.h b/include/linux/export.h
index 96e45ea463..77afdb2a25 100644
--- a/include/linux/export.h
+++ b/include/linux/export.h
@@ -38,7 +38,7 @@ extern struct module __this_module;
 
 #ifdef CONFIG_MODULES
 
-#ifndef __GENKSYMS__
+#if defined(__KERNEL__) && !defined(__GENKSYMS__)
 #ifdef CONFIG_MODVERSIONS
 /* Mark the CRC weak since genksyms apparently decides not to
  * generate a checksums for some symbols */
@@ -53,7 +53,7 @@ extern struct module __this_module;
 #endif
 
 /* For every exported symbol, place a struct in the __ksymtab section */
-#define __EXPORT_SYMBOL(sym, sec)				\
+#define ___EXPORT_SYMBOL(sym, sec)				\
 	extern typeof(sym) sym;					\
 	__CRC_SYMBOL(sym, sec)					\
 	static const char __kstrtab_##sym[]			\
@@ -65,6 +65,24 @@ extern struct module __this_module;
 	__attribute__((section("___ksymtab" sec "+" #sym), unused))	\
 	= { (unsigned long)&sym, __kstrtab_##sym }
 
+#ifdef CONFIG_TRIM_UNUSED_KSYMS
+
+#include <linux/kconfig.h>
+#include <generated/autoksyms.h>
+
+#define __EXPORT_SYMBOL(sym, sec)				\
+	__cond_export_sym(sym, sec, config_enabled(__KSYM_##sym))
+#define __cond_export_sym(sym, sec, conf)			\
+	___cond_export_sym(sym, sec, conf)
+#define ___cond_export_sym(sym, sec, enabled)			\
+	__cond_export_sym_##enabled(sym, sec)
+#define __cond_export_sym_1(sym, sec) ___EXPORT_SYMBOL(sym, sec)
+#define __cond_export_sym_0(sym, sec) /* nothing */
+
+#else
+#define __EXPORT_SYMBOL ___EXPORT_SYMBOL
+#endif
+
 #define EXPORT_SYMBOL(sym)					\
 	__EXPORT_SYMBOL(sym, "")
 
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v3 3/7] fixdep: accept extra dependencies on stdin
  2016-02-18 20:06 [PATCH v3 0/7] Trim unused exported kernel symbols Nicolas Pitre
  2016-02-18 20:06 ` [PATCH v3 1/7] kbuild: record needed exported symbols for modules Nicolas Pitre
  2016-02-18 20:06 ` [PATCH v3 2/7] allow for per-symbol configurable EXPORT_SYMBOL() Nicolas Pitre
@ 2016-02-18 20:06 ` Nicolas Pitre
  2016-02-18 20:07 ` [PATCH v3 4/7] kbuild: de-duplicate fixdep usage Nicolas Pitre
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Nicolas Pitre @ 2016-02-18 20:06 UTC (permalink / raw)
  To: Michal Marek, linux-kbuild; +Cc: linux-kernel, Al Viro, Rusty Russell

... and merge them in the list of parsed dependencies.

Signed-off-by: Nicolas Pitre <nico@linaro.org>
---
 scripts/basic/fixdep.c | 60 +++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 45 insertions(+), 15 deletions(-)

diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c
index 5b327c67a8..d984deb120 100644
--- a/scripts/basic/fixdep.c
+++ b/scripts/basic/fixdep.c
@@ -120,13 +120,15 @@
 #define INT_NFIG ntohl(0x4e464947)
 #define INT_FIG_ ntohl(0x4649475f)
 
+int insert_extra_deps;
 char *target;
 char *depfile;
 char *cmdline;
 
 static void usage(void)
 {
-	fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
+	fprintf(stderr, "Usage: fixdep [-e] <depfile> <target> <cmdline>\n");
+	fprintf(stderr, " -e  insert extra dependencies given on stdin\n");
 	exit(1);
 }
 
@@ -138,6 +140,40 @@ static void print_cmdline(void)
 	printf("cmd_%s := %s\n\n", target, cmdline);
 }
 
+/*
+ * Print out a dependency path from a symbol name
+ */
+static void print_config(const char *m, int slen)
+{
+	int c, i;
+
+	printf("    $(wildcard include/config/");
+	for (i = 0; i < slen; i++) {
+		c = m[i];
+		if (c == '_')
+			c = '/';
+		else
+			c = tolower(c);
+		putchar(c);
+	}
+	printf(".h) \\\n");
+}
+
+static void do_extra_deps(void)
+{
+	if (insert_extra_deps) {
+		char buf[80];
+		while(fgets(buf, sizeof(buf), stdin)) {
+			int len = strlen(buf);
+			if (len < 2 || buf[len-1] != '\n') {
+				fprintf(stderr, "fixdep: bad data on stdin\n");
+				exit(1);
+			}
+			print_config(buf, len-1);
+		}
+	}
+}
+
 struct item {
 	struct item	*next;
 	unsigned int	len;
@@ -197,23 +233,12 @@ static void define_config(const char *name, int len, unsigned int hash)
 static void use_config(const char *m, int slen)
 {
 	unsigned int hash = strhash(m, slen);
-	int c, i;
 
 	if (is_defined_config(m, slen, hash))
 	    return;
 
 	define_config(m, slen, hash);
-
-	printf("    $(wildcard include/config/");
-	for (i = 0; i < slen; i++) {
-		c = m[i];
-		if (c == '_')
-			c = '/';
-		else
-			c = tolower(c);
-		putchar(c);
-	}
-	printf(".h) \\\n");
+	print_config(m, slen);
 }
 
 static void parse_config_file(const char *map, size_t len)
@@ -250,7 +275,7 @@ static void parse_config_file(const char *map, size_t len)
 	}
 }
 
-/* test is s ends in sub */
+/* test if s ends in sub */
 static int strrcmp(const char *s, const char *sub)
 {
 	int slen = strlen(s);
@@ -374,6 +399,8 @@ static void parse_dep_file(void *map, size_t len)
 		exit(1);
 	}
 
+	do_extra_deps();
+
 	printf("\n%s: $(deps_%s)\n\n", target, target);
 	printf("$(deps_%s):\n", target);
 }
@@ -430,7 +457,10 @@ int main(int argc, char *argv[])
 {
 	traps();
 
-	if (argc != 4)
+	if (argc == 5 && !strcmp(argv[1], "-e")) {
+		insert_extra_deps = 1;
+		argv++;
+	} else if (argc != 4)
 		usage();
 
 	depfile = argv[1];
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v3 4/7] kbuild: de-duplicate fixdep usage
  2016-02-18 20:06 [PATCH v3 0/7] Trim unused exported kernel symbols Nicolas Pitre
                   ` (2 preceding siblings ...)
  2016-02-18 20:06 ` [PATCH v3 3/7] fixdep: accept extra dependencies on stdin Nicolas Pitre
@ 2016-02-18 20:07 ` Nicolas Pitre
  2016-02-18 20:07 ` [PATCH v3 5/7] kbuild: add fine grained build dependencies for exported symbols Nicolas Pitre
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Nicolas Pitre @ 2016-02-18 20:07 UTC (permalink / raw)
  To: Michal Marek, linux-kbuild; +Cc: linux-kernel, Al Viro, Rusty Russell

The generation and postprocessing of automatic dependency rules is
duplicated in rule_cc_o_c and if_changed_dep. Since this is not a
trivial one-liner action, it is now abstracted under cmd_and_fixdep
to simplify things and make future changes easier.

In the rule_cc_o_c case that means the order of some commands has been
altered, namely fixdep and related file manipulations are executed
earlier, but they didn't depend on those commands that now execute later.

Signed-off-by: Nicolas Pitre <nico@linaro.org>
---
 scripts/Kbuild.include | 5 ++++-
 scripts/Makefile.build | 9 ++-------
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index 1db6d73c8d..8a257fa663 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -256,10 +256,13 @@ if_changed = $(if $(strip $(any-prereq) $(arg-check)),                       \
 # Execute the command and also postprocess generated .d dependencies file.
 if_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ),                  \
 	@set -e;                                                             \
+	$(cmd_and_fixdep))
+
+cmd_and_fixdep =                                                             \
 	$(echo-cmd) $(cmd_$(1));                                             \
 	scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\
 	rm -f $(depfile);                                                    \
-	mv -f $(dot-target).tmp $(dot-target).cmd)
+	mv -f $(dot-target).tmp $(dot-target).cmd;
 
 # Usage: $(call if_changed_rule,foo)
 # Will check if $(cmd_foo) or any of the prerequisites changed,
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index f4b4320e0d..8134ee81ad 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -243,14 +243,9 @@ endif
 
 define rule_cc_o_c
 	$(call echo-cmd,checksrc) $(cmd_checksrc)			  \
-	$(call echo-cmd,cc_o_c) $(cmd_cc_o_c);				  \
+	$(call cmd_and_fixdep,cc_o_c)					  \
 	$(cmd_modversions)						  \
-	$(call echo-cmd,record_mcount)					  \
-	$(cmd_record_mcount)						  \
-	scripts/basic/fixdep $(depfile) $@ '$(call make-cmd,cc_o_c)' >    \
-	                                              $(dot-target).tmp;  \
-	rm -f $(depfile);						  \
-	mv -f $(dot-target).tmp $(dot-target).cmd
+	$(call echo-cmd,record_mcount) $(cmd_record_mcount)
 endef
 
 # List module undefined symbols (or empty line if not enabled)
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v3 5/7] kbuild: add fine grained build dependencies for exported symbols
  2016-02-18 20:06 [PATCH v3 0/7] Trim unused exported kernel symbols Nicolas Pitre
                   ` (3 preceding siblings ...)
  2016-02-18 20:07 ` [PATCH v3 4/7] kbuild: de-duplicate fixdep usage Nicolas Pitre
@ 2016-02-18 20:07 ` Nicolas Pitre
  2016-02-18 20:07 ` [PATCH v3 6/7] create/adjust generated/autoksyms.h Nicolas Pitre
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Nicolas Pitre @ 2016-02-18 20:07 UTC (permalink / raw)
  To: Michal Marek, linux-kbuild; +Cc: linux-kernel, Al Viro, Rusty Russell

Like with kconfig options, we now have the ability to compile in and
out individual EXPORT_SYMBOL() declarations based on the content of
include/generated/autoksyms.h.  However we don't want the entire
world to be rebuilt whenever that file is touched.

Let's apply the same build dependency trick used for CONFIG_* symbols
where the time stamp of empty files whose paths matching those symbols
is used to trigger fine grained rebuilds. In our case the key is the
symbol name passed to EXPORT_SYMBOL().

However, unlike config options, we cannot just use fixdep to parse
the source code for EXPORT_SYMBOL(ksym) because several variants exist
and parsing them all in a separate tool, and keeping it in synch, is
not trivially maintainable.  Furthermore, there are variants such as

	EXPORT_SYMBOL_GPL(pci_user_read_config_##size);

that are instanciated via a macro for which we can't easily determine
the actual exported symbol name(s) short of actually running the
preprocessor on them.

Storing the symbol name string in a special ELF section doesn't work
for targets that output assembly or preprocessed source.

So the best way is really to leverage the preprocessor by having it emit
a warning for each EXPORT_SYMBOL() instance and filtering those apart
from stderr by the build system. Then the list of symbols is simply fed
to fixdep to be merged with the other dependencies.

Because of the lowercasing performed by fixdep, there might be name
collisions triggering spurious rebuilds for similar symbols. But this
shouldn't be a big issue in practice. (This is the case for CONFIG_*
symbols and I didn't want to be different here, whatever the original
reason for doing so.)

To avoid needless build overhead, the exported symbol name gathering is
performed only when CONFIG_TRIM_UNUSED_KSYMS is selected.

Signed-off-by: Nicolas Pitre <nico@linaro.org>
Acked-by: Rusty Russell <rusty@rustcorp.com.au>
---
 include/linux/export.h | 16 ++++++++++++++--
 scripts/Kbuild.include | 28 ++++++++++++++++++++++++++++
 scripts/basic/fixdep.c |  1 +
 3 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/include/linux/export.h b/include/linux/export.h
index 77afdb2a25..794392102d 100644
--- a/include/linux/export.h
+++ b/include/linux/export.h
@@ -76,8 +76,20 @@ extern struct module __this_module;
 	___cond_export_sym(sym, sec, conf)
 #define ___cond_export_sym(sym, sec, enabled)			\
 	__cond_export_sym_##enabled(sym, sec)
-#define __cond_export_sym_1(sym, sec) ___EXPORT_SYMBOL(sym, sec)
-#define __cond_export_sym_0(sym, sec) /* nothing */
+#define __cond_export_sym_1(sym, sec)				\
+	__KSYM_DEP(sym) ___EXPORT_SYMBOL(sym, sec)
+#define __cond_export_sym_0(sym, sec)				\
+	__KSYM_DEP(sym) /* nothing */
+
+/*
+ * For fine grained build dependencies, we want to tell the build system
+ * about each possible exported symbol even if they're not actually exported.
+ * This is accomplished with a preprocessor warning that gets captured by
+ * the make rule (see ksym_dep_filter in scripts/Kbuild.include).
+ */
+#define __KSYM_DEP(sym) __pragma_string( KBUILD_AUTOKSYM_DEP: sym )
+#define __pragma_string(x) __emit_pragma( GCC warning #x )
+#define __emit_pragma(x) _Pragma(#x)
 
 #else
 #define __EXPORT_SYMBOL ___EXPORT_SYMBOL
diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index 8a257fa663..0b69479310 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -258,12 +258,40 @@ if_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ),                  \
 	@set -e;                                                             \
 	$(cmd_and_fixdep))
 
+ifndef CONFIG_TRIM_UNUSED_KSYMS
+
 cmd_and_fixdep =                                                             \
 	$(echo-cmd) $(cmd_$(1));                                             \
 	scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\
 	rm -f $(depfile);                                                    \
 	mv -f $(dot-target).tmp $(dot-target).cmd;
 
+else
+
+# Filter out exported kernel symbol names advertised as warning pragmas
+# by the preprocessor and write them to $(1). We must consider continuation
+# lines as well: they start with a blank, or the preceeding line ends with
+# a ':'. Anything else is passed through as is.
+# See also __KSYM_DEP() in include/linux/export.h.
+ksym_dep_filter = sed -n \
+	-e '1 {x; $$!d}' \
+	-e '/^ / {H; $$!d}' \
+	-e 'x; /:$$/ {x; H; $$!d; s/^/ /; x}' \
+	-e ':filter; /^.*KBUILD_AUTOKSYM_DEP: /! {p; b next}' \
+	-e 's//KSYM_/; s/\n.*//; w $(1)' \
+	-e ':next; $$!d' \
+	-e '1 q; s/^/ /; x; /^ /! b filter'
+
+cmd_and_fixdep =                                                             \
+	$(echo-cmd)                                                          \
+	$(cmd_$(1)) 2>&1 | $(call ksym_dep_filter,$(dot-target).ksym.tmp) >&2;\
+	scripts/basic/fixdep -e $(depfile) $@ '$(make-cmd)'                  \
+	    < $(dot-target).ksym.tmp > $(dot-target).tmp;	             \
+	rm -f $(dot-target).ksym.tmp $(depfile);                             \
+	mv -f $(dot-target).tmp $(dot-target).cmd;
+
+endif
+
 # Usage: $(call if_changed_rule,foo)
 # Will check if $(cmd_foo) or any of the prerequisites changed,
 # and if so will execute $(rule_foo).
diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c
index d984deb120..2dc6bf754a 100644
--- a/scripts/basic/fixdep.c
+++ b/scripts/basic/fixdep.c
@@ -354,6 +354,7 @@ static void parse_dep_file(void *map, size_t len)
 
 			/* Ignore certain dependencies */
 			if (strrcmp(s, "include/generated/autoconf.h") &&
+			    strrcmp(s, "include/generated/autoksyms.h") &&
 			    strrcmp(s, "arch/um/include/uml-config.h") &&
 			    strrcmp(s, "include/linux/kconfig.h") &&
 			    strrcmp(s, ".ver")) {
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v3 6/7] create/adjust generated/autoksyms.h
  2016-02-18 20:06 [PATCH v3 0/7] Trim unused exported kernel symbols Nicolas Pitre
                   ` (4 preceding siblings ...)
  2016-02-18 20:07 ` [PATCH v3 5/7] kbuild: add fine grained build dependencies for exported symbols Nicolas Pitre
@ 2016-02-18 20:07 ` Nicolas Pitre
  2016-02-28 12:11   ` Zev Weiss
  2016-02-18 20:07 ` [PATCH v3 7/7] kconfig option for TRIM_UNUSED_KSYMS Nicolas Pitre
  2016-02-24 15:19 ` [PATCH v3 0/7] Trim unused exported kernel symbols Nicolas Pitre
  7 siblings, 1 reply; 11+ messages in thread
From: Nicolas Pitre @ 2016-02-18 20:07 UTC (permalink / raw)
  To: Michal Marek, linux-kbuild; +Cc: linux-kernel, Al Viro, Rusty Russell

Given the list of exported symbols needed by all modules, we can create
a header file containing preprocessor defines for each of those symbols.
Also, when some symbols are added and/or removed from the list, we can
update the time on the corresponding files used as build dependencies for
those symbols. And finally, if any symbol did change state, the
corresponding source files must be rebuilt.

The insertion or removal of an EXPORT_SYMBOL() entry within a module may
create or remove the need for another exported symbol.  This is why this
operation has to be repeated until the list of needed exported symbols
becomes stable. Only then the final kernel and modules link take place.

Signed-off-by: Nicolas Pitre <nico@linaro.org>
Acked-by: Rusty Russell <rusty@rustcorp.com.au>
---
 Makefile                    | 13 ++++++
 scripts/adjust_autoksyms.sh | 97 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 110 insertions(+)
 create mode 100755 scripts/adjust_autoksyms.sh

diff --git a/Makefile b/Makefile
index e916428cf7..bb865095ca 100644
--- a/Makefile
+++ b/Makefile
@@ -921,6 +921,10 @@ quiet_cmd_link-vmlinux = LINK    $@
 # Include targets which we want to
 # execute if the rest of the kernel build went well.
 vmlinux: scripts/link-vmlinux.sh $(vmlinux-deps) FORCE
+ifdef CONFIG_TRIM_UNUSED_KSYMS
+	$(Q)$(CONFIG_SHELL) scripts/adjust_autoksyms.sh \
+	  "$(MAKE) KBUILD_MODULES=1 -f $(srctree)/Makefile autoksyms_recursive"
+endif
 ifdef CONFIG_HEADERS_CHECK
 	$(Q)$(MAKE) -f $(srctree)/Makefile headers_check
 endif
@@ -935,6 +939,15 @@ ifdef CONFIG_GDB_SCRIPTS
 endif
 	+$(call if_changed,link-vmlinux)
 
+autoksyms_recursive: $(vmlinux-deps)
+	$(Q)$(CONFIG_SHELL) scripts/adjust_autoksyms.sh \
+	  "$(MAKE) KBUILD_MODULES=1 -f $(srctree)/Makefile autoksyms_recursive"
+PHONY += autoksyms_recursive
+
+# standalone target for easier testing
+include/generated/autoksyms.h: FORCE
+	$(Q)$(CONFIG_SHELL) scripts/adjust_autoksyms.sh true
+
 # The actual objects are generated when descending,
 # make sure no implicit rule kicks in
 $(sort $(vmlinux-deps)): $(vmlinux-dirs) ;
diff --git a/scripts/adjust_autoksyms.sh b/scripts/adjust_autoksyms.sh
new file mode 100755
index 0000000000..898a3ca1b2
--- /dev/null
+++ b/scripts/adjust_autoksyms.sh
@@ -0,0 +1,97 @@
+#!/bin/sh
+
+# Script to create/update include/generated/autoksyms.h and dependency files
+#
+# Copyright:	(C) 2016  Linaro Limited
+# Created by:	Nicolas Pitre, January 2016
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 as
+# published by the Free Software Foundation.
+
+# Create/update the include/generated/autoksyms.h file from the list
+# of all module's needed symbols as recorded on the third line of
+# .tmp_versions/*.mod files.
+#
+# For each symbol being added or removed, the corresponding dependency
+# file's timestamp is updated to force a rebuild of the affected source
+# file. All arguments passed to this script are assumed to be a command
+# to be exec'd to trigger a rebuild of those files.
+
+set -e
+
+cur_ksyms_file="include/generated/autoksyms.h"
+new_ksyms_file="include/generated/autoksyms.h.tmpnew"
+
+info() { [ "$quiet" != "silent_" ] && printf "  %-7s %s\n" "$1" "$2"; }
+
+info "CHK" "$cur_ksyms_file"
+
+# Use "make V=1" to debug this script.
+case "$KBUILD_VERBOSE" in
+*1*)
+	set -x
+	;;
+esac
+
+# We need access to CONFIG_ symbols
+case "${KCONFIG_CONFIG}" in
+*/*)
+	. "${KCONFIG_CONFIG}"
+	;;
+*)
+	# Force using a file from the current directory
+	. "./${KCONFIG_CONFIG}"
+esac
+
+# In case it doesn't exist yet...
+[ -e "$cur_ksyms_file" ] || touch "$cur_ksyms_file"
+
+# Generate a new ksym list file with symbols needed by the current
+# set of modules.
+cat > "$new_ksyms_file" << EOT
+/*
+ * Automatically generated file; DO NOT EDIT.
+ */
+
+EOT
+sed -ns -e '3s/ /\n/gp' "$MODVERDIR"/*.mod | sort -u |
+while read sym; do
+	if [ -n "$CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX" ]; then
+		sym=$(sed 's/^_//' <<< "$sym")
+	fi
+	echo "#define __KSYM_${sym} 1"
+done >> "$new_ksyms_file"
+
+# Special case for modversions (see modpost.c)
+if [ -n "$CONFIG_MODVERSIONS" ]; then
+	echo "#define __KSYM_module_layout 1" >> "$new_ksyms_file"
+fi
+
+# Extract changes between old and new list and touch corresponding
+# dependency files.
+# Note: sort -m doesn't work well with underscore prefixed symbols so we
+# use 'cat ... | sort' instead.
+changed=0
+while read sympath; do
+	[ -z "$sympath" ] && continue
+	depfile="include/config/ksym/${sympath}.h"
+	mkdir -p "$(dirname "$depfile")"
+	touch "$depfile"
+	changed=$((changed + 1))
+done <<< "$(
+	cat "$cur_ksyms_file" "$new_ksyms_file" | sort | uniq -u |
+	sed -n 's/^#define __KSYM_\(.*\) 1/\1/p' | tr "A-Z_" "a-z/"  )"
+
+if [ $changed -gt 0 ]; then
+	# Replace the old list with tne new one
+	old=$(grep -c "^#define __KSYM_" "$cur_ksyms_file" || true)
+	new=$(grep -c "^#define __KSYM_" "$new_ksyms_file" || true)
+	info "KSYMS" "symbols: before=$old, after=$new, changed=$changed"
+	info "UPD" "$cur_ksyms_file"
+	mv -f "$new_ksyms_file" "$cur_ksyms_file"
+	# Then trigger a rebuild of affected source files
+	exec $@
+else
+	rm -f "$new_ksyms_file"
+fi
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v3 7/7] kconfig option for TRIM_UNUSED_KSYMS
  2016-02-18 20:06 [PATCH v3 0/7] Trim unused exported kernel symbols Nicolas Pitre
                   ` (5 preceding siblings ...)
  2016-02-18 20:07 ` [PATCH v3 6/7] create/adjust generated/autoksyms.h Nicolas Pitre
@ 2016-02-18 20:07 ` Nicolas Pitre
  2016-02-24 15:19 ` [PATCH v3 0/7] Trim unused exported kernel symbols Nicolas Pitre
  7 siblings, 0 replies; 11+ messages in thread
From: Nicolas Pitre @ 2016-02-18 20:07 UTC (permalink / raw)
  To: Michal Marek, linux-kbuild; +Cc: linux-kernel, Al Viro, Rusty Russell

The config option to enable it all.

Signed-off-by: Nicolas Pitre <nico@linaro.org>
Acked-by: Rusty Russell <rusty@rustcorp.com.au>
---
 init/Kconfig | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/init/Kconfig b/init/Kconfig
index 22320804fb..e6f666331b 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1990,6 +1990,22 @@ config MODULE_COMPRESS_XZ
 
 endchoice
 
+config TRIM_UNUSED_KSYMS
+	bool "Trim unused exported kernel symbols"
+	depends on MODULES && !UNUSED_SYMBOLS
+	help
+	  The kernel and some modules make many symbols available for
+	  other modules to use via EXPORT_SYMBOL() and variants. Depending
+	  on the set of modules being selected in your kernel configuration,
+	  many of those exported symbols might never be used.
+
+	  This option allows for unused exported symbols to be dropped from
+	  the build. In turn, this provides the compiler more opportunities
+	  (especially when using LTO) for optimizing the code and reducing
+	  binary size.  This might have some security advantages as well.
+
+	  If unsure say N.
+
 endif # MODULES
 
 config MODULES_TREE_LOOKUP
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v3 0/7] Trim unused exported kernel symbols
  2016-02-18 20:06 [PATCH v3 0/7] Trim unused exported kernel symbols Nicolas Pitre
                   ` (6 preceding siblings ...)
  2016-02-18 20:07 ` [PATCH v3 7/7] kconfig option for TRIM_UNUSED_KSYMS Nicolas Pitre
@ 2016-02-24 15:19 ` Nicolas Pitre
  7 siblings, 0 replies; 11+ messages in thread
From: Nicolas Pitre @ 2016-02-24 15:19 UTC (permalink / raw)
  To: Michal Marek, linux-kbuild; +Cc: linux-kernel


Gentle ping.


On Thu, 18 Feb 2016, Nicolas Pitre wrote:

> This patch series provides the option to omit exported symbols from
> the kernel and modules that are never referenced by any of the selected
> modules in the current kernel configuration.
> 
> The original cover letter with lots of test results can be found here:
> 
>         https://lkml.org/lkml/2016/2/8/813
> 
> A git branch with those patches is also available here:
> 
>         http://git.linaro.org/people/nicolas.pitre/linux.git autoksyms
> 
> Please consider for merging.
> 
> Changes from v2:
> 
> - Generating the build dependencies by parsing the source with fixdep
>   turned out to be unreliable due to all the EXPORT_SYMBOL() variants,
>   and especially their use within macros where the actual symbol name
>   is known only after running the preprocessor. This list of symbol names
>   is now obtained from the preprocessor directly, fixing allmodconfig
>   builds.
> 
> Changes from v1:
> 
> - Replaced "exp" that doesn't convey the right meaning as noted by
>   Sam Ravnborg. The "ksym" identifier is actually what the kernel
>   already uses for this. Therefore:
>   - CONFIG_TRIM_UNUSED_EXPSYMS --> CONFIG_TRIM_UNUSED_KSYMS
>   - include/generated/expsyms.h --> include/generated/autoksyms.h
>   - #define __EXPSYM_* --> #define __KSYM_*
> 
> - Some sed regexp improvements as suggested by Al Viro.
> 
> - Renamed vmlinux_recursive target to autoksyms_recursive.
> 
> - Accept EXPORT_SYMBOL variants with a prefix, e.g. ACPI_EXPORT_SYMBOL.
> 
> - Minor commit log clarifications.
> 
> - Added Rusty's ACK.
> 
> 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v3 6/7] create/adjust generated/autoksyms.h
  2016-02-18 20:07 ` [PATCH v3 6/7] create/adjust generated/autoksyms.h Nicolas Pitre
@ 2016-02-28 12:11   ` Zev Weiss
  2016-02-28 19:31     ` Nicolas Pitre
  0 siblings, 1 reply; 11+ messages in thread
From: Zev Weiss @ 2016-02-28 12:11 UTC (permalink / raw)
  To: Nicolas Pitre
  Cc: Michal Marek, linux-kbuild, linux-kernel, Al Viro, Rusty Russell

On Thu, Feb 18, 2016 at 15:07:02 -0500, Nicolas Pitre wrote:
>diff --git a/scripts/adjust_autoksyms.sh b/scripts/adjust_autoksyms.sh
>new file mode 100755
>index 0000000000..898a3ca1b2
>--- /dev/null
>+++ b/scripts/adjust_autoksyms.sh
>@@ -0,0 +1,97 @@
>+#!/bin/sh

Given the here-strings in this script, this should probably be /bin/bash 
or breakage will ensue on Debian and suchlike where /bin/sh != bash.  
However...

>+sed -ns -e '3s/ /\n/gp' "$MODVERDIR"/*.mod | sort -u |
>+while read sym; do
>+	if [ -n "$CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX" ]; then
>+		sym=$(sed 's/^_//' <<< "$sym")

...this one could (shell-portably and more efficiently) be a parameter 
expansion (sym="${sym#_}").

>+
>+# Extract changes between old and new list and touch corresponding
>+# dependency files.
>+# Note: sort -m doesn't work well with underscore prefixed symbols so we
>+# use 'cat ... | sort' instead.
>+changed=0
>+while read sympath; do
>+	[ -z "$sympath" ] && continue
>+	depfile="include/config/ksym/${sympath}.h"
>+	mkdir -p "$(dirname "$depfile")"
>+	touch "$depfile"
>+	changed=$((changed + 1))
>+done <<< "$(
>+	cat "$cur_ksyms_file" "$new_ksyms_file" | sort | uniq -u |
>+	sed -n 's/^#define __KSYM_\(.*\) 1/\1/p' | tr "A-Z_" "a-z/"  )"

Nothing super-obvious springs to mind as a pure-sh equivalent for this 
one though, so assuming the script remains bash-specific, a process 
substitution like

  done < <(cat ...)

might slightly cleaner than the here-string + command substitution 
(doesn't need quoting, and just streams through a pipe rather than bash 
snarfing it all up into a string).

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v3 6/7] create/adjust generated/autoksyms.h
  2016-02-28 12:11   ` Zev Weiss
@ 2016-02-28 19:31     ` Nicolas Pitre
  0 siblings, 0 replies; 11+ messages in thread
From: Nicolas Pitre @ 2016-02-28 19:31 UTC (permalink / raw)
  To: Zev Weiss
  Cc: Michal Marek, linux-kbuild, linux-kernel, Al Viro, Rusty Russell

On Sun, 28 Feb 2016, Zev Weiss wrote:

> On Thu, Feb 18, 2016 at 15:07:02 -0500, Nicolas Pitre wrote:
> >diff --git a/scripts/adjust_autoksyms.sh b/scripts/adjust_autoksyms.sh
> >new file mode 100755
> >index 0000000000..898a3ca1b2
> >--- /dev/null
> >+++ b/scripts/adjust_autoksyms.sh
> > @@ -0,0 +1,97 @@
> >+#!/bin/sh
> 
> Given the here-strings in this script, this should probably be /bin/bash or
> breakage will ensue on Debian and suchlike where /bin/sh != bash.  However...
> 
> >+sed -ns -e '3s/ /\n/gp' "$MODVERDIR"/*.mod | sort -u |
> >+while read sym; do
> >+	if [ -n "$CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX" ]; then
> >+		sym=$(sed 's/^_//' <<< "$sym")
> 
> ...this one could (shell-portably and more efficiently) be a parameter
> expansion (sym="${sym#_}").

Indeed.

> >+
> >+# Extract changes between old and new list and touch corresponding
> >+# dependency files.
> >+# Note: sort -m doesn't work well with underscore prefixed symbols so we
> >+# use 'cat ... | sort' instead.
> >+changed=0
> >+while read sympath; do
> >+	[ -z "$sympath" ] && continue
> >+	depfile="include/config/ksym/${sympath}.h"
> >+	mkdir -p "$(dirname "$depfile")"
> >+	touch "$depfile"
> >+	changed=$((changed + 1))
> >+done <<< "$(
> >+	cat "$cur_ksyms_file" "$new_ksyms_file" | sort | uniq -u |
> >+	sed -n 's/^#define __KSYM_\(.*\) 1/\1/p' | tr "A-Z_" "a-z/"  )"
> 
> Nothing super-obvious springs to mind as a pure-sh equivalent for this one
> though, so assuming the script remains bash-specific, a process substitution
> like
> 
>  done < <(cat ...)
> 
> might slightly cleaner than the here-string + command substitution (doesn't
> need quoting, and just streams through a pipe rather than bash snarfing it all
> up into a string).

Right.  Still, I decided to make it compatible with a simpler shell. I 
applied the following changes and tested it with dash.

diff --git a/scripts/adjust_autoksyms.sh b/scripts/adjust_autoksyms.sh
index 898a3ca1b2..a145a24cd8 100755
--- a/scripts/adjust_autoksyms.sh
+++ b/scripts/adjust_autoksyms.sh
@@ -57,9 +57,7 @@ cat > "$new_ksyms_file" << EOT
 EOT
 sed -ns -e '3s/ /\n/gp' "$MODVERDIR"/*.mod | sort -u |
 while read sym; do
-	if [ -n "$CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX" ]; then
-		sym=$(sed 's/^_//' <<< "$sym")
-	fi
+	[ -n "$CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX" ] && sym="${sym#_}"
 	echo "#define __KSYM_${sym} 1"
 done >> "$new_ksyms_file"
 
@@ -72,16 +70,18 @@ fi
 # dependency files.
 # Note: sort -m doesn't work well with underscore prefixed symbols so we
 # use 'cat ... | sort' instead.
-changed=0
+changed=$(
+count=0
+cat "$cur_ksyms_file" "$new_ksyms_file" | sort | uniq -u |
+sed -n 's/^#define __KSYM_\(.*\) 1/\1/p' | tr "A-Z_" "a-z/" |
 while read sympath; do
 	[ -z "$sympath" ] && continue
 	depfile="include/config/ksym/${sympath}.h"
 	mkdir -p "$(dirname "$depfile")"
 	touch "$depfile"
-	changed=$((changed + 1))
-done <<< "$(
-	cat "$cur_ksyms_file" "$new_ksyms_file" | sort | uniq -u |
-	sed -n 's/^#define __KSYM_\(.*\) 1/\1/p' | tr "A-Z_" "a-z/"  )"
+	echo $((count += 1))
+done | tail -1 )
+changed=${changed:-0}
 
 if [ $changed -gt 0 ]; then
 	# Replace the old list with tne new one

Nicolas

^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2016-02-28 19:31 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-02-18 20:06 [PATCH v3 0/7] Trim unused exported kernel symbols Nicolas Pitre
2016-02-18 20:06 ` [PATCH v3 1/7] kbuild: record needed exported symbols for modules Nicolas Pitre
2016-02-18 20:06 ` [PATCH v3 2/7] allow for per-symbol configurable EXPORT_SYMBOL() Nicolas Pitre
2016-02-18 20:06 ` [PATCH v3 3/7] fixdep: accept extra dependencies on stdin Nicolas Pitre
2016-02-18 20:07 ` [PATCH v3 4/7] kbuild: de-duplicate fixdep usage Nicolas Pitre
2016-02-18 20:07 ` [PATCH v3 5/7] kbuild: add fine grained build dependencies for exported symbols Nicolas Pitre
2016-02-18 20:07 ` [PATCH v3 6/7] create/adjust generated/autoksyms.h Nicolas Pitre
2016-02-28 12:11   ` Zev Weiss
2016-02-28 19:31     ` Nicolas Pitre
2016-02-18 20:07 ` [PATCH v3 7/7] kconfig option for TRIM_UNUSED_KSYMS Nicolas Pitre
2016-02-24 15:19 ` [PATCH v3 0/7] Trim unused exported kernel symbols Nicolas Pitre

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.