[PATCH v9 08/11] kbuild: implement CONFIG_TRIM_UNUSED_KSYMS without recursion

2023-06-11 Thread Masahiro Yamada
When CONFIG_TRIM_UNUSED_KSYMS is enabled, Kbuild recursively traverses
the directory tree to determine which EXPORT_SYMBOL to trim. If an
EXPORT_SYMBOL turns out to be unused by anyone, Kbuild begins the
second traverse, where some source files are recompiled with their
EXPORT_SYMBOL() tuned into a no-op.

Linus stated negative opinions about this slowness in commits:

 - 5cf0fd591f2e ("Kbuild: disable TRIM_UNUSED_KSYMS option")
 - a555bdd0c58c ("Kbuild: enable TRIM_UNUSED_KSYMS again, with some guarding")

We can do this better now. The final data structures of EXPORT_SYMBOL
are generated by the modpost stage, so modpost can selectively emit
KSYMTAB entries that are really used by modules.

Commit f73edc8951b2 ("kbuild: unify two modpost invocations") is another
ground-work to do this in a one-pass algorithm. With the list of modules,
modpost sets sym->used if it is used by a module. modpost emits KSYMTAB
only for symbols with sym->used==true.

BTW, Nicolas explained why the trimming was implemented with recursion:

  https://lore.kernel.org/all/2o2rpn97-79nq-p7s2-nq5-8p833914...@syhkavp.arg/

Actually, we never achieved that level of optimization where the chain
reaction of trimming comes into play because:

 - CONFIG_LTO_CLANG cannot remove any unused symbols
 - CONFIG_LD_DEAD_CODE_DATA_ELIMINATION is enabled only for vmlinux,
   but not modules

If deeper trimming is required, we need to revisit this, but I guess
that is unlikely to happen.

Signed-off-by: Masahiro Yamada 
---

Changes in v9:
  - fix short options in getopt()
  - Do not dump trimmed symbols into Module.symvers

Changes in v7:
  - Remove *.usyms

Changes in v5:
  - Clean up more

 .gitignore  |  2 -
 Makefile| 22 ++-
 include/linux/export.h  | 67 +-
 scripts/Makefile.build  | 15 +---
 scripts/Makefile.modpost|  7 
 scripts/adjust_autoksyms.sh | 73 -
 scripts/basic/fixdep.c  |  3 +-
 scripts/gen_autoksyms.sh| 62 ---
 scripts/gen_ksymdeps.sh | 30 ---
 scripts/mod/modpost.c   | 57 ++---
 scripts/remove-stale-files  |  4 ++
 11 files changed, 78 insertions(+), 264 deletions(-)
 delete mode 100755 scripts/adjust_autoksyms.sh
 delete mode 100755 scripts/gen_autoksyms.sh
 delete mode 100755 scripts/gen_ksymdeps.sh

diff --git a/.gitignore b/.gitignore
index 7f86e0837909..c3ce78ca20d2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -51,7 +51,6 @@
 *.symversions
 *.tab.[ch]
 *.tar
-*.usyms
 *.xz
 *.zst
 Module.symvers
@@ -112,7 +111,6 @@ modules.order
 #
 /include/config/
 /include/generated/
-/include/ksym/
 /arch/*/include/generated/
 
 # stgit generated dirs
diff --git a/Makefile b/Makefile
index f836936fb4d8..cc3fe09c4dec 100644
--- a/Makefile
+++ b/Makefile
@@ -1193,28 +1193,12 @@ endif
 export KBUILD_VMLINUX_LIBS
 export KBUILD_LDS  := arch/$(SRCARCH)/kernel/vmlinux.lds
 
-# Recurse until adjust_autoksyms.sh is satisfied
-PHONY += autoksyms_recursive
 ifdef CONFIG_TRIM_UNUSED_KSYMS
 # For the kernel to actually contain only the needed exported symbols,
 # we have to build modules as well to determine what those symbols are.
-# (this can be evaluated only once include/config/auto.conf has been included)
 KBUILD_MODULES := 1
-
-autoksyms_recursive: $(build-dir) modules.order
-   $(Q)$(CONFIG_SHELL) $(srctree)/scripts/adjust_autoksyms.sh \
- "$(MAKE) -f $(srctree)/Makefile autoksyms_recursive"
 endif
 
-autoksyms_h := $(if $(CONFIG_TRIM_UNUSED_KSYMS), include/generated/autoksyms.h)
-
-quiet_cmd_autoksyms_h = GEN $@
-  cmd_autoksyms_h = mkdir -p $(dir $@); \
-   $(CONFIG_SHELL) $(srctree)/scripts/gen_autoksyms.sh $@
-
-$(autoksyms_h):
-   $(call cmd,autoksyms_h)
-
 # '$(AR) mPi' needs 'T' to workaround the bug of llvm-ar <= 14
 quiet_cmd_ar_vmlinux.a = AR  $@
   cmd_ar_vmlinux.a = \
@@ -1223,7 +1207,7 @@ quiet_cmd_ar_vmlinux.a = AR  $@
$(AR) mPiT $$($(AR) t $@ | sed -n 1p) $@ $$($(AR) t $@ | grep -F -f 
$(srctree)/scripts/head-object-list.txt)
 
 targets += vmlinux.a
-vmlinux.a: $(KBUILD_VMLINUX_OBJS) scripts/head-object-list.txt 
autoksyms_recursive FORCE
+vmlinux.a: $(KBUILD_VMLINUX_OBJS) scripts/head-object-list.txt FORCE
$(call if_changed,ar_vmlinux.a)
 
 PHONY += vmlinux_o
@@ -1279,7 +1263,7 @@ scripts: scripts_basic scripts_dtc
 PHONY += prepare archprepare
 
 archprepare: outputmakefile archheaders archscripts scripts 
include/config/kernel.release \
-   asm-generic $(version_h) $(autoksyms_h) include/generated/utsrelease.h \
+   asm-generic $(version_h) include/generated/utsrelease.h \
include/generated/compile.h include/generated/autoconf.h 
remove-stale-files
 
 prepare0: archprepare
@@ -2039,7 +2023,7 @@ clean: $(clean-dirs)
-o -name '*.dtb.S' -o -name '*.dtbo.S' \
-o -name '*.dt.yaml' \
   

[PATCH v9 11/11] linux/export.h: rename 'sec' argument to 'license'

2023-06-11 Thread Masahiro Yamada
Now, EXPORT_SYMBOL() is populated in two stages. In the first stage,
all of EXPORT_SYMBOL/EXPORT_SYMBOL_GPL go into the same section,
'.export_symbol'.

'sec' does not make sense any more. Rename it to 'license'.

Signed-off-by: Masahiro Yamada 
Reviewed-by: Nick Desaulniers 
---

(no changes since v7)

Changes in v7:
 - New patch

 include/linux/export.h | 8 
 include/linux/pm.h | 6 +++---
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/include/linux/export.h b/include/linux/export.h
index 1de600734071..beed8387e0a4 100644
--- a/include/linux/export.h
+++ b/include/linux/export.h
@@ -57,11 +57,11 @@ extern struct module __this_module;
  * be reused in other execution contexts such as the UEFI stub or the
  * decompressor.
  */
-#define __EXPORT_SYMBOL(sym, sec, ns)
+#define __EXPORT_SYMBOL(sym, license, ns)
 
 #elif defined(__GENKSYMS__)
 
-#define __EXPORT_SYMBOL(sym, sec, ns)  __GENKSYMS_EXPORT_SYMBOL(sym)
+#define __EXPORT_SYMBOL(sym, license, ns)  __GENKSYMS_EXPORT_SYMBOL(sym)
 
 #elif defined(__ASSEMBLY__)
 
@@ -78,9 +78,9 @@ extern struct module __this_module;
 #endif /* CONFIG_MODULES */
 
 #ifdef DEFAULT_SYMBOL_NAMESPACE
-#define _EXPORT_SYMBOL(sym, sec)   __EXPORT_SYMBOL(sym, sec, 
__stringify(DEFAULT_SYMBOL_NAMESPACE))
+#define _EXPORT_SYMBOL(sym, license)   __EXPORT_SYMBOL(sym, license, 
__stringify(DEFAULT_SYMBOL_NAMESPACE))
 #else
-#define _EXPORT_SYMBOL(sym, sec)   __EXPORT_SYMBOL(sym, sec, "")
+#define _EXPORT_SYMBOL(sym, license)   __EXPORT_SYMBOL(sym, license, "")
 #endif
 
 #define EXPORT_SYMBOL(sym) _EXPORT_SYMBOL(sym, "")
diff --git a/include/linux/pm.h b/include/linux/pm.h
index f615193587d2..badad7d11f4f 100644
--- a/include/linux/pm.h
+++ b/include/linux/pm.h
@@ -375,14 +375,14 @@ const struct dev_pm_ops name = { \
 }
 
 #ifdef CONFIG_PM
-#define _EXPORT_DEV_PM_OPS(name, sec, ns)  \
+#define _EXPORT_DEV_PM_OPS(name, license, ns)  \
const struct dev_pm_ops name;   \
-   __EXPORT_SYMBOL(name, sec, ns); \
+   __EXPORT_SYMBOL(name, license, ns); \
const struct dev_pm_ops name
 #define EXPORT_PM_FN_GPL(name) EXPORT_SYMBOL_GPL(name)
 #define EXPORT_PM_FN_NS_GPL(name, ns)  EXPORT_SYMBOL_NS_GPL(name, ns)
 #else
-#define _EXPORT_DEV_PM_OPS(name, sec, ns)  \
+#define _EXPORT_DEV_PM_OPS(name, license, ns)  \
static __maybe_unused const struct dev_pm_ops __static_##name
 #define EXPORT_PM_FN_GPL(name)
 #define EXPORT_PM_FN_NS_GPL(name, ns)
-- 
2.39.2


___
linux-snps-arc mailing list
linux-snps-arc@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-snps-arc


[PATCH v9 09/11] modpost: merge two similar section mismatch warnings

2023-06-11 Thread Masahiro Yamada
In case of section mismatch, modpost shows slightly different messages.

For extable section mismatch:

 "%s(%s+0x%lx): Section mismatch in reference to the %s:%s\n"

For the other cases:

 "%s: section mismatch in reference: %s (section: %s) -> %s (section: %s)\n"

They are similar. Merge them.

Signed-off-by: Masahiro Yamada 
Reviewed-by: Nick Desaulniers 
---


 scripts/mod/modpost.c | 18 +++---
 1 file changed, 3 insertions(+), 15 deletions(-)

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 50d950e904a4..0adda14451fa 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -1147,21 +1147,10 @@ static void default_mismatch_handler(const char 
*modname, struct elf_info *elf,
 
sec_mismatch_count++;
 
-   switch (mismatch->mismatch) {
-   case TEXT_TO_ANY_INIT:
-   case DATA_TO_ANY_INIT:
-   case TEXTDATA_TO_ANY_EXIT:
-   case XXXINIT_TO_SOME_INIT:
-   case XXXEXIT_TO_SOME_EXIT:
-   case ANY_INIT_TO_ANY_EXIT:
-   case ANY_EXIT_TO_ANY_INIT:
-   warn("%s: section mismatch in reference: %s (section: %s) -> %s 
(section: %s)\n",
-modname, fromsym, fromsec, tosym, tosec);
-   break;
-   case EXTABLE_TO_NON_TEXT:
-   warn("%s(%s+0x%lx): Section mismatch in reference to the 
%s:%s\n",
-modname, fromsec, (long)faddr, tosec, tosym);
+   warn("%s: section mismatch in reference: %s (section: %s) -> %s 
(section: %s)\n",
+modname, fromsym, fromsec, tosym, tosec);
 
+   if (mismatch->mismatch == EXTABLE_TO_NON_TEXT) {
if (match(tosec, mismatch->bad_tosec))
fatal("The relocation at %s+0x%lx references\n"
  "section \"%s\" which is black-listed.\n"
@@ -1181,7 +1170,6 @@ static void default_mismatch_handler(const char *modname, 
struct elf_info *elf,
else
error("%s+0x%lx references non-executable section 
'%s'\n",
  fromsec, (long)faddr, tosec);
-   break;
}
 }
 
-- 
2.39.2


___
linux-snps-arc mailing list
linux-snps-arc@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-snps-arc


[PATCH v9 10/11] modpost: show offset from symbol for section mismatch warnings

2023-06-11 Thread Masahiro Yamada
Currently, modpost only shows the symbol names and section names, so it
repeats the same message if there are multiple relocations in the same
symbol. It is common the relocation spans across multiple instructions.

It is better to show the offset from the symbol.

Signed-off-by: Masahiro Yamada 
Reviewed-by: Nick Desaulniers 
---


 scripts/mod/modpost.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 0adda14451fa..f26d5050e6da 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -1147,8 +1147,8 @@ static void default_mismatch_handler(const char *modname, 
struct elf_info *elf,
 
sec_mismatch_count++;
 
-   warn("%s: section mismatch in reference: %s (section: %s) -> %s 
(section: %s)\n",
-modname, fromsym, fromsec, tosym, tosec);
+   warn("%s: section mismatch in reference: %s+0x%x (section: %s) -> %s 
(section: %s)\n",
+modname, fromsym, (unsigned int)(faddr - from->st_value), fromsec, 
tosym, tosec);
 
if (mismatch->mismatch == EXTABLE_TO_NON_TEXT) {
if (match(tosec, mismatch->bad_tosec))
-- 
2.39.2


___
linux-snps-arc mailing list
linux-snps-arc@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-snps-arc


[PATCH v9 07/11] modpost: use null string instead of NULL pointer for default namespace

2023-06-11 Thread Masahiro Yamada
The default namespace is the null string, "".

When set, the null string "" is converted to NULL:

  s->namespace = namespace[0] ? NOFAIL(strdup(namespace)) : NULL;

When printed, the NULL pointer is get back to the null string:

  sym->namespace ?: ""

This saves 1 byte memory allocated for "", but loses the readability.

In kernel-space, we strive to save memory, but modpost is a userspace
tool used to build the kernel. On modern systems, such small piece of
memory is not a big deal.

Handle the namespace string as is.

Signed-off-by: Masahiro Yamada 
Reviewed-by: Nick Desaulniers 
---


 scripts/mod/modpost.c | 17 +++--
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 79ad0a346a5c..051ff67875ec 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -300,6 +300,13 @@ static bool contains_namespace(struct list_head *head, 
const char *namespace)
 {
struct namespace_list *list;
 
+   /*
+* The default namespace is null string "", which is always implicitly
+* contained.
+*/
+   if (!namespace[0])
+   return true;
+
list_for_each_entry(list, head, list) {
if (!strcmp(list->namespace, namespace))
return true;
@@ -369,7 +376,7 @@ static struct symbol *sym_add_exported(const char *name, 
struct module *mod,
s = alloc_symbol(name);
s->module = mod;
s->is_gpl_only = gpl_only;
-   s->namespace = namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
+   s->namespace = NOFAIL(strdup(namespace));
list_add_tail(>list, >exported_symbols);
hash_add_symbol(s);
 
@@ -1829,8 +1836,7 @@ static void check_exports(struct module *mod)
else
basename = mod->name;
 
-   if (exp->namespace &&
-   !contains_namespace(>imported_namespaces, 
exp->namespace)) {
+   if (!contains_namespace(>imported_namespaces, 
exp->namespace)) {
modpost_log(allow_missing_ns_imports ? LOG_WARN : 
LOG_ERROR,
"module %s uses symbol %s from namespace 
%s, but does not import it.\n",
basename, exp->name, exp->namespace);
@@ -1916,8 +1922,7 @@ static void add_exported_symbols(struct buffer *buf, 
struct module *mod)
list_for_each_entry(sym, >exported_symbols, list)
buf_printf(buf, "KSYMTAB_%s(%s, \"%s\", \"%s\");\n",
   sym->is_func ? "FUNC" : "DATA", sym->name,
-  sym->is_gpl_only ? "_gpl" : "",
-  sym->namespace ?: "");
+  sym->is_gpl_only ? "_gpl" : "", sym->namespace);
 
if (!modversions)
return;
@@ -2185,7 +2190,7 @@ static void write_dump(const char *fname)
buf_printf(, 
"0x%08x\t%s\t%s\tEXPORT_SYMBOL%s\t%s\n",
   sym->crc, sym->name, mod->name,
   sym->is_gpl_only ? "_GPL" : "",
-  sym->namespace ?: "");
+  sym->namespace);
}
}
write_buf(, fname);
-- 
2.39.2


___
linux-snps-arc mailing list
linux-snps-arc@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-snps-arc


[PATCH v9 05/11] modpost: check static EXPORT_SYMBOL* by modpost again

2023-06-11 Thread Masahiro Yamada
Commit 31cb50b5590f ("kbuild: check static EXPORT_SYMBOL* by script
instead of modpost") moved the static EXPORT_SYMBOL* check from the
mostpost to a shell script because I thought it must be checked per
compilation unit to avoid false negatives.

I came up with an idea to do this in modpost, against combined ELF
files. The relocation entries in ELF will find the correct exported
symbol even if there exist symbols with the same name in different
compilation units.

Again, the same sample code.

  Makefile:

obj-y += foo1.o foo2.o

  foo1.c:

#include 
static void foo(void) {}
EXPORT_SYMBOL(foo);

  foo2.c:

void foo(void) {}

Then, modpost can catch it correctly.

MODPOST Module.symvers
  ERROR: modpost: vmlinux: local symbol 'foo' was exported

Signed-off-by: Masahiro Yamada 
Reviewed-by: Nick Desaulniers 
---

(no changes since v6)

Changes in v6:
  - Make the symbol name in the warning more precise

 scripts/Makefile.build |  4 ---
 scripts/check-local-export | 70 --
 scripts/mod/modpost.c  |  7 
 3 files changed, 7 insertions(+), 74 deletions(-)
 delete mode 100755 scripts/check-local-export

diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 4119e737fe87..210142c3ff00 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -222,8 +222,6 @@ cmd_gen_ksymdeps = \
$(CONFIG_SHELL) $(srctree)/scripts/gen_ksymdeps.sh $@ >> 
$(dot-target).cmd
 endif
 
-cmd_check_local_export = $(srctree)/scripts/check-local-export $@
-
 ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
 cmd_warn_shared_object = $(if $(word 2, $(modname-multi)),$(warning 
$(kbuild-file): $*.o is added to multiple modules: $(modname-multi)))
 endif
@@ -231,7 +229,6 @@ endif
 define rule_cc_o_c
$(call cmd_and_fixdep,cc_o_c)
$(call cmd,gen_ksymdeps)
-   $(call cmd,check_local_export)
$(call cmd,checksrc)
$(call cmd,checkdoc)
$(call cmd,gen_objtooldep)
@@ -243,7 +240,6 @@ endef
 define rule_as_o_S
$(call cmd_and_fixdep,as_o_S)
$(call cmd,gen_ksymdeps)
-   $(call cmd,check_local_export)
$(call cmd,gen_objtooldep)
$(call cmd,gen_symversions_S)
$(call cmd,warn_shared_object)
diff --git a/scripts/check-local-export b/scripts/check-local-export
deleted file mode 100755
index 86ad94647164..
--- a/scripts/check-local-export
+++ /dev/null
@@ -1,70 +0,0 @@
-#!/bin/sh
-# SPDX-License-Identifier: GPL-2.0-only
-#
-# Copyright (C) 2022 Masahiro Yamada 
-# Copyright (C) 2022 Owen Rafferty 
-#
-# Exit with error if a local exported symbol is found.
-# EXPORT_SYMBOL should be used for global symbols.
-
-set -e
-pid=$$
-
-# If there is no symbol in the object, ${NM} (both GNU nm and llvm-nm) shows
-# 'no symbols' diagnostic (but exits with 0). It is harmless and hidden by
-# '2>/dev/null'. However, it suppresses real error messages as well. Add a
-# hand-crafted error message here.
-#
-# TODO:
-# Use --quiet instead of 2>/dev/null when we upgrade the minimum version of
-# binutils to 2.37, llvm to 13.0.0.
-# Then, the following line will be simpler:
-#   { ${NM} --quiet ${1} || kill 0; } |
-
-{ ${NM} ${1} 2>/dev/null || { echo "${0}: ${NM} failed" >&2; kill $pid; } } |
-${AWK} -v "file=${1}" '
-BEGIN {
-   i = 0
-}
-
-# Skip the line if the number of fields is less than 3.
-#
-# case 1)
-#   For undefined symbols, the first field (value) is empty.
-#   The outout looks like this:
-# " U _printk"
-#   It is unneeded to record undefined symbols.
-#
-# case 2)
-#   For Clang LTO, llvm-nm outputs a line with type t but empty name:
-# " t"
-!length($3) {
-   next
-}
-
-# save (name, type) in the associative array
-{ symbol_types[$3]=$2 }
-
-# append the exported symbol to the array
-($3 ~ /^__export_symbol_.*/) {
-   export_symbols[i] = $3
-   sub(/^__export_symbol_/, "", export_symbols[i])
-   i++
-}
-
-END {
-   exit_code = 0
-   for (j = 0; j < i; ++j) {
-   name = export_symbols[j]
-   # nm(3) says "If lowercase, the symbol is usually local"
-   if (symbol_types[name] ~ /[a-z]/) {
-   printf "%s: error: local symbol %s was exported\n",
-   file, name | "cat 1>&2"
-   exit_code = 1
-   }
-   }
-
-   exit exit_code
-}'
-
-exit $?
diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index bdf4244da993..10da82ad5874 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -1210,6 +1210,13 @@ static void check_export_symbol(struct module *mod, 
struct elf_info *elf,
return;
}
 
+   if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
+   ELF_ST_BIND(sym->st_info) != STB_WEAK) {
+   error("%s: local symbol '%s' was exported\n", mod->name,
+ label_name + strlen(prefix));
+   return;
+   }
+

[PATCH v9 02/11] modpost: pass struct module pointer to check_section_mismatch()

2023-06-11 Thread Masahiro Yamada
The next commit will use it.

Signed-off-by: Masahiro Yamada 
Reviewed-by: Nick Desaulniers 
---


 scripts/mod/modpost.c | 22 +++---
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 8decf04633bc..403ba4d923f5 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -1211,7 +1211,7 @@ static void default_mismatch_handler(const char *modname, 
struct elf_info *elf,
}
 }
 
-static void check_section_mismatch(const char *modname, struct elf_info *elf,
+static void check_section_mismatch(struct module *mod, struct elf_info *elf,
   Elf_Sym *sym,
   unsigned int fsecndx, const char *fromsec,
   Elf_Addr faddr, Elf_Addr taddr)
@@ -1222,7 +1222,7 @@ static void check_section_mismatch(const char *modname, 
struct elf_info *elf,
if (!mismatch)
return;
 
-   default_mismatch_handler(modname, elf, mismatch, sym,
+   default_mismatch_handler(mod->name, elf, mismatch, sym,
 fsecndx, fromsec, faddr,
 tosec, taddr);
 }
@@ -1406,7 +1406,7 @@ static int addend_mips_rel(struct elf_info *elf, Elf_Shdr 
*sechdr, Elf_Rela *r)
 #define R_LARCH_SUB32  55
 #endif
 
-static void section_rela(const char *modname, struct elf_info *elf,
+static void section_rela(struct module *mod, struct elf_info *elf,
 Elf_Shdr *sechdr)
 {
Elf_Rela *rela;
@@ -1452,12 +1452,12 @@ static void section_rela(const char *modname, struct 
elf_info *elf,
break;
}
 
-   check_section_mismatch(modname, elf, elf->symtab_start + r_sym,
+   check_section_mismatch(mod, elf, elf->symtab_start + r_sym,
   fsecndx, fromsec, r.r_offset, 
r.r_addend);
}
 }
 
-static void section_rel(const char *modname, struct elf_info *elf,
+static void section_rel(struct module *mod, struct elf_info *elf,
Elf_Shdr *sechdr)
 {
Elf_Rel *rel;
@@ -1507,7 +1507,7 @@ static void section_rel(const char *modname, struct 
elf_info *elf,
fatal("Please add code to calculate addend for this 
architecture\n");
}
 
-   check_section_mismatch(modname, elf, elf->symtab_start + r_sym,
+   check_section_mismatch(mod, elf, elf->symtab_start + r_sym,
   fsecndx, fromsec, r.r_offset, 
r.r_addend);
}
 }
@@ -1524,19 +1524,19 @@ static void section_rel(const char *modname, struct 
elf_info *elf,
  * to find all references to a section that reference a section that will
  * be discarded and warns about it.
  **/
-static void check_sec_ref(const char *modname, struct elf_info *elf)
+static void check_sec_ref(struct module *mod, struct elf_info *elf)
 {
int i;
Elf_Shdr *sechdrs = elf->sechdrs;
 
/* Walk through all sections */
for (i = 0; i < elf->num_sections; i++) {
-   check_section(modname, elf, >sechdrs[i]);
+   check_section(mod->name, elf, >sechdrs[i]);
/* We want to process only relocation sections and not .init */
if (sechdrs[i].sh_type == SHT_RELA)
-   section_rela(modname, elf, >sechdrs[i]);
+   section_rela(mod, elf, >sechdrs[i]);
else if (sechdrs[i].sh_type == SHT_REL)
-   section_rel(modname, elf, >sechdrs[i]);
+   section_rel(mod, elf, >sechdrs[i]);
}
 }
 
@@ -1707,7 +1707,7 @@ static void read_symbols(const char *modname)
 sym_get_data(, sym));
}
 
-   check_sec_ref(modname, );
+   check_sec_ref(mod, );
 
if (!mod->is_vmlinux) {
version = get_modinfo(, "version");
-- 
2.39.2


___
linux-snps-arc mailing list
linux-snps-arc@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-snps-arc


[PATCH v9 06/11] modpost: squash sym_update_namespace() into sym_add_exported()

2023-06-11 Thread Masahiro Yamada
Pass a set of the name, license, and namespace to sym_add_exported().

sym_update_namespace() is unneeded.

Signed-off-by: Masahiro Yamada 
Reviewed-by: Nick Desaulniers 
---

 scripts/mod/modpost.c | 27 ---
 1 file changed, 4 insertions(+), 23 deletions(-)

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 10da82ad5874..79ad0a346a5c 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -355,26 +355,8 @@ static const char *sec_name(const struct elf_info *info, 
unsigned int secindex)
 
 #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
 
-static void sym_update_namespace(const char *symname, const char *namespace)
-{
-   struct symbol *s = find_symbol(symname);
-
-   /*
-* That symbol should have been created earlier and thus this is
-* actually an assertion.
-*/
-   if (!s) {
-   error("Could not update namespace(%s) for symbol %s\n",
- namespace, symname);
-   return;
-   }
-
-   free(s->namespace);
-   s->namespace = namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
-}
-
 static struct symbol *sym_add_exported(const char *name, struct module *mod,
-  bool gpl_only)
+  bool gpl_only, const char *namespace)
 {
struct symbol *s = find_symbol(name);
 
@@ -387,6 +369,7 @@ static struct symbol *sym_add_exported(const char *name, 
struct module *mod,
s = alloc_symbol(name);
s->module = mod;
s->is_gpl_only = gpl_only;
+   s->namespace = namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
list_add_tail(>list, >exported_symbols);
hash_add_symbol(s);
 
@@ -1236,8 +1219,7 @@ static void check_export_symbol(struct module *mod, 
struct elf_info *elf,
}
 
data += strlen(data) + 1;   /* namespace */
-   s = sym_add_exported(name, mod, is_gpl);
-   sym_update_namespace(name, data);
+   s = sym_add_exported(name, mod, is_gpl, data);
 
/*
 * We need to be aware whether we are exporting a function or
@@ -2180,9 +2162,8 @@ static void read_dump(const char *fname)
mod = new_module(modname, strlen(modname));
mod->from_dump = true;
}
-   s = sym_add_exported(symname, mod, gpl_only);
+   s = sym_add_exported(symname, mod, gpl_only, namespace);
sym_set_crc(s, crc);
-   sym_update_namespace(symname, namespace);
}
free(buf);
return;
-- 
2.39.2


___
linux-snps-arc mailing list
linux-snps-arc@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-snps-arc


[PATCH v9 04/11] ia64,export.h: replace EXPORT_DATA_SYMBOL* with EXPORT_SYMBOL*

2023-06-11 Thread Masahiro Yamada
With the previous refactoring, you can always use EXPORT_SYMBOL*.

Replace two instances in ia64, then remove EXPORT_DATA_SYMBOL*.

Signed-off-by: Masahiro Yamada 
Reviewed-by: Nick Desaulniers 
---


 arch/ia64/kernel/head.S  | 2 +-
 arch/ia64/kernel/ivt.S   | 2 +-
 include/asm-generic/export.h | 3 ---
 3 files changed, 2 insertions(+), 5 deletions(-)

diff --git a/arch/ia64/kernel/head.S b/arch/ia64/kernel/head.S
index f22469f1c1fc..c096500590e9 100644
--- a/arch/ia64/kernel/head.S
+++ b/arch/ia64/kernel/head.S
@@ -170,7 +170,7 @@ RestRR: 
\
__PAGE_ALIGNED_DATA
 
.global empty_zero_page
-EXPORT_DATA_SYMBOL_GPL(empty_zero_page)
+EXPORT_SYMBOL_GPL(empty_zero_page)
 empty_zero_page:
.skip PAGE_SIZE
 
diff --git a/arch/ia64/kernel/ivt.S b/arch/ia64/kernel/ivt.S
index d6d4229b28db..7a418e324d30 100644
--- a/arch/ia64/kernel/ivt.S
+++ b/arch/ia64/kernel/ivt.S
@@ -87,7 +87,7 @@
 
.align 32768// align on 32KB boundary
.global ia64_ivt
-   EXPORT_DATA_SYMBOL(ia64_ivt)
+   EXPORT_SYMBOL(ia64_ivt)
 ia64_ivt:
 
/
 // 0x Entry 0 (size 64 bundles) VHPT Translation (8,20,47)
diff --git a/include/asm-generic/export.h b/include/asm-generic/export.h
index 0ae9f38a904c..570cd4da7210 100644
--- a/include/asm-generic/export.h
+++ b/include/asm-generic/export.h
@@ -8,7 +8,4 @@
  */
 #include 
 
-#define EXPORT_DATA_SYMBOL(name)   EXPORT_SYMBOL(name)
-#define EXPORT_DATA_SYMBOL_GPL(name)   EXPORT_SYMBOL_GPL(name)
-
 #endif
-- 
2.39.2


___
linux-snps-arc mailing list
linux-snps-arc@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-snps-arc


[PATCH v9 03/11] kbuild: generate KSYMTAB entries by modpost

2023-06-11 Thread Masahiro Yamada
Commit 7b4537199a4a ("kbuild: link symbol CRCs at final link, removing
CONFIG_MODULE_REL_CRCS") made modpost output CRCs in the same way
whether the EXPORT_SYMBOL() is placed in *.c or *.S.

For further cleanups, this commit applies a similar approach to the
entire data structure of EXPORT_SYMBOL().

The EXPORT_SYMBOL() compilation is split into two stages.

When a source file is compiled, EXPORT_SYMBOL() is converted into a
dummy symbol in the .export_symbol section.

For example,

EXPORT_SYMBOL(foo);
EXPORT_SYMBOL_NS_GPL(bar, BAR_NAMESPACE);

will be encoded into the following assembly code:

.section ".export_symbol","a"
__export_symbol_foo:
.asciz ""
.asciz ""
.balign 8
.quad foo
.previous

.section ".export_symbol","a"
__export_symbol_bar:
.asciz "GPL"
.asciz "BAR_NAMESPACE"
.balign 8
.quad bar
.previous

They are mere markers to tell modpost the name, license, and namespace
of the symbols. They will be dropped from the final vmlinux and modules
because the *(.export_symbol) will go into /DISCARD/ in the linker script.

Then, modpost extracts all the information about EXPORT_SYMBOL() from the
.export_symbol section, and generates the final C code:

KSYMTAB_FUNC(foo, "", "");
KSYMTAB_FUNC(bar, "_gpl", "BAR_NAMESPACE");

KSYMTAB_FUNC() (or KSYMTAB_DATA() if it is data) is expanded to struct
kernel_symbol that will be linked to the vmlinux or a module.

With this change, EXPORT_SYMBOL() works in the same way for *.c and *.S
files, providing the following benefits.

[1] Deprecate EXPORT_DATA_SYMBOL()

In the old days, EXPORT_SYMBOL() was only available in C files. To export
a symbol in *.S, EXPORT_SYMBOL() was placed in a separate *.c file.
arch/arm/kernel/armksyms.c is one example written in the classic manner.

Commit 22823ab419d8 ("EXPORT_SYMBOL() for asm") removed this limitation.
Since then, EXPORT_SYMBOL() can be placed close to the symbol definition
in *.S files. It was a nice improvement.

However, as that commit mentioned, you need to use EXPORT_DATA_SYMBOL()
for data objects on some architectures.

In the new approach, modpost checks symbol's type (STT_FUNC or not),
and outputs KSYMTAB_FUNC() or KSYMTAB_DATA() accordingly.

There are only two users of EXPORT_DATA_SYMBOL:

  EXPORT_DATA_SYMBOL_GPL(empty_zero_page)(arch/ia64/kernel/head.S)
  EXPORT_DATA_SYMBOL(ia64_ivt)   (arch/ia64/kernel/ivt.S)

They are transformed as follows and output into .vmlinux.export.c

  KSYMTAB_DATA(empty_zero_page, "_gpl", "");
  KSYMTAB_DATA(ia64_ivt, "", "");

The other EXPORT_SYMBOL users in ia64 assembly are output as
KSYMTAB_FUNC().

EXPORT_DATA_SYMBOL() is now deprecated.

[2] merge  and 

There are two similar header implementations:

  include/linux/export.hfor .c files
  include/asm-generic/export.h  for .S files

Ideally, the functionality should be consistent between them, but they
tend to diverge.

Commit 8651ec01daed ("module: add support for symbol namespaces.") did
not support the namespace for *.S files.

This commit shifts the essential implementation part to C, which supports
EXPORT_SYMBOL_NS() for *.S files.

 and  will remain as a wrapper of
 for a while.

They will be removed after #include  directives are all
replaced with #include .

[3] Implement CONFIG_TRIM_UNUSED_KSYMS in one-pass algorithm (by a later commit)

When CONFIG_TRIM_UNUSED_KSYMS is enabled, Kbuild recursively traverses
the directory tree to determine which EXPORT_SYMBOL to trim. If an
EXPORT_SYMBOL turns out to be unused by anyone, Kbuild begins the
second traverse, where some source files are recompiled with their
EXPORT_SYMBOL() tuned into a no-op.

We can do this better now; modpost can selectively emit KSYMTAB entries
that are really used by modules.

Signed-off-by: Masahiro Yamada 
Reviewed-by: Nick Desaulniers 
---

Changes in v9:
  - Fix build error for ARC when CONFIG_DEBUG_INFO is enabled.
Use ASM_NL because hard-coding instruction separator does not work
  - Change the marker format to encode the license as a string

Changes in v8:
  - Fix the definition of EXPORT_SYMBOL() to v7.
It seems to cause a regression for xtensa. (reported by 0day bot)

Changes in v7:
  - Fix sparse warning reported by 0day bot
https://lore.kernel.org/linux-kbuild/202305280830.rj5ltc9m-...@intel.com/

Changes in v6:
  - Fix build error on UML

Changes in v5:
  - Fix build error on ARM

Changes in v4:
  - Version 3 did not work if a same name symbol exists in a different 
compilation unit
Fix it.

Changes in v3:
  - Move struct kernel_symbol to kernel/module/internal.h

Changes in v2:
  - Use KSYMTAB_FUNC and KSYMTAB_DATA for functions and data, respectively
This distinction is needed for ia64.

 arch/ia64/include/asm/Kbuild  |   1 +
 arch/ia64/include/asm/export.h|   3 -
 include/asm-generic/export.h  |  84 ++-
 

[PATCH v9 01/11] ARC: define ASM_NL and __ALIGN(_STR) outside #ifdef __ASSEMBLY__ guard

2023-06-11 Thread Masahiro Yamada
ASM_NL is useful not only in *.S files but also in .c files for using
inline assembler in C code.

On ARC, however, ASM_NL is evaluated inconsistently. It is expanded to
a backquote (`) in *.S files, but a semicolon (;) in *.c files because
arch/arc/include/asm/linkage.h defines it inside #ifdef __ASSEMBLY__,
so the definition for C code falls back to the default value defined in
include/linux/linkage.h.

If ASM_NL is used in inline assembler in .c files, it will result in
wrong assembly code because a semicolon is not an instruction separator,
but the start of a comment for ARC.

Move ASM_NL (also __ALIGN and __ALIGN_STR) out of the #ifdef.

Fixes: 9df62f054406 ("arch: use ASM_NL instead of ';' for assembler new line 
character in the macro")
Fixes: 8d92e992a785 ("ARC: define __ALIGN_STR and __ALIGN symbols for ARC")
Signed-off-by: Masahiro Yamada 
---

Changes in v9:
  - New patch

 arch/arc/include/asm/linkage.h | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/arch/arc/include/asm/linkage.h b/arch/arc/include/asm/linkage.h
index c9434ff3aa4c..8a3fb71e9cfa 100644
--- a/arch/arc/include/asm/linkage.h
+++ b/arch/arc/include/asm/linkage.h
@@ -8,6 +8,10 @@
 
 #include 
 
+#define ASM_NL  `  /* use '`' to mark new line in macro */
+#define __ALIGN.align 4
+#define __ALIGN_STR__stringify(__ALIGN)
+
 #ifdef __ASSEMBLY__
 
 .macro ST2 e, o, off
@@ -28,10 +32,6 @@
 #endif
 .endm
 
-#define ASM_NL  `  /* use '`' to mark new line in macro */
-#define __ALIGN.align 4
-#define __ALIGN_STR__stringify(__ALIGN)
-
 /* annotation for data we want in DCCM - if enabled in .config */
 .macro ARCFP_DATA nm
 #ifdef CONFIG_ARC_HAS_DCCM
-- 
2.39.2


___
linux-snps-arc mailing list
linux-snps-arc@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-snps-arc


[PATCH v9 00/11] Unify and , remove EXPORT_DATA_SYMBOL(), faster TRIM_UNUSED_KSYMS

2023-06-11 Thread Masahiro Yamada


My goals:

 - Refactors EXPORT_SYMBOL,  and .
   You can still put EXPORT_SYMBOL() in *.S file, very close to the definition,
   but you do not need to care about whether it is a function or a data.
   This removes EXPORT_DATA_SYMBOL().

 - Re-implement TRIM_UNUSED_KSYMS in one-pass.
   This makes the building faster.

 - Move the static EXPORT_SYMBOL check to modpost.
   This also makes the building faster.

This patch set is applicable to linux-next 20230609.

Previous version
v8: 
https://lore.kernel.org/linux-kbuild/cak7lnarbvqdwgspowgju87aknvzvnwjl-bbj3xj-2w+fpid...@mail.gmail.com/T/#t
v7: 
https://lore.kernel.org/linux-kbuild/20230608142428.256985-1-masahi...@kernel.org/T/#mbaddcee18c9a8cf0a9b1f3fc562d09526cb69540
v6: 
https://lore.kernel.org/linux-kbuild/CAK7LNARjzGnj+sYX=_5yQ+8qoOQ2KB5N-_Ye53Ru3=xicez...@mail.gmail.com/T/#t
v5: 
https://lore.kernel.org/linux-kbuild/CAK7LNARBiOywrMLbR=9n35sk19u0qm3xcpy7d1wqv-eyb4w...@mail.gmail.com/T/#t
v4: 
https://lore.kernel.org/linux-kbuild/CAK7LNASDzy9RERN6+q6WgR4ROYZQue=sbqgbcoyuvepbyht...@mail.gmail.com/T/#t
v3: https://lore.kernel.org/all/20220928063947.299333-1-masahi...@kernel.org/



Masahiro Yamada (11):
  ARC: define ASM_NL and __ALIGN(_STR) outside #ifdef __ASSEMBLY__ guard
  modpost: pass struct module pointer to check_section_mismatch()
  kbuild: generate KSYMTAB entries by modpost
  ia64,export.h: replace EXPORT_DATA_SYMBOL* with EXPORT_SYMBOL*
  modpost: check static EXPORT_SYMBOL* by modpost again
  modpost: squash sym_update_namespace() into sym_add_exported()
  modpost: use null string instead of NULL pointer for default namespace
  kbuild: implement CONFIG_TRIM_UNUSED_KSYMS without recursion
  modpost: merge two similar section mismatch warnings
  modpost: show offset from symbol for section mismatch warnings
  linux/export.h: rename 'sec' argument to 'license'

 .gitignore|   2 -
 Makefile  |  22 +--
 arch/arc/include/asm/linkage.h|   8 +-
 arch/ia64/include/asm/Kbuild  |   1 +
 arch/ia64/include/asm/export.h|   3 -
 arch/ia64/kernel/head.S   |   2 +-
 arch/ia64/kernel/ivt.S|   2 +-
 include/asm-generic/export.h  |  83 +-
 include/asm-generic/vmlinux.lds.h |   1 +
 include/linux/export-internal.h   |  49 ++
 include/linux/export.h| 128 
 include/linux/pm.h|  10 +-
 kernel/module/internal.h  |  12 ++
 scripts/Makefile.build|  27 +---
 scripts/Makefile.modpost  |   7 +
 scripts/adjust_autoksyms.sh   |  73 -
 scripts/basic/fixdep.c|   3 +-
 scripts/check-local-export|  70 -
 scripts/gen_autoksyms.sh  |  62 
 scripts/gen_ksymdeps.sh   |  30 
 scripts/mod/modpost.c | 242 +++---
 scripts/mod/modpost.h |   1 +
 scripts/remove-stale-files|   4 +
 23 files changed, 283 insertions(+), 559 deletions(-)
 delete mode 100644 arch/ia64/include/asm/export.h
 delete mode 100755 scripts/adjust_autoksyms.sh
 delete mode 100755 scripts/check-local-export
 delete mode 100755 scripts/gen_autoksyms.sh
 delete mode 100755 scripts/gen_ksymdeps.sh

-- 
2.39.2


___
linux-snps-arc mailing list
linux-snps-arc@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-snps-arc